Home | History | Annotate | Download | only in Object
      1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file declares the Binary class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_OBJECT_BINARY_H
     15 #define LLVM_OBJECT_BINARY_H
     16 
     17 #include "llvm/ADT/OwningPtr.h"
     18 #include "llvm/Object/Error.h"
     19 
     20 namespace llvm {
     21 
     22 class MemoryBuffer;
     23 class StringRef;
     24 
     25 namespace object {
     26 
     27 class Binary {
     28 private:
     29   Binary() LLVM_DELETED_FUNCTION;
     30   Binary(const Binary &other) LLVM_DELETED_FUNCTION;
     31 
     32   unsigned int TypeID;
     33 
     34 protected:
     35   MemoryBuffer *Data;
     36 
     37   Binary(unsigned int Type, MemoryBuffer *Source);
     38 
     39   enum {
     40     ID_Archive,
     41     // Object and children.
     42     ID_StartObjects,
     43     ID_COFF,
     44     ID_ELF32L, // ELF 32-bit, little endian
     45     ID_ELF32B, // ELF 32-bit, big endian
     46     ID_ELF64L, // ELF 64-bit, little endian
     47     ID_ELF64B, // ELF 64-bit, big endian
     48     ID_MachO,
     49     ID_EndObjects
     50   };
     51 
     52   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
     53     if (isLE)
     54       return is64Bits ? ID_ELF64L : ID_ELF32L;
     55     else
     56       return is64Bits ? ID_ELF64B : ID_ELF32B;
     57   }
     58 
     59 public:
     60   virtual ~Binary();
     61 
     62   StringRef getData() const;
     63   StringRef getFileName() const;
     64 
     65   // Cast methods.
     66   unsigned int getType() const { return TypeID; }
     67 
     68   // Convenience methods
     69   bool isObject() const {
     70     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
     71   }
     72 
     73   bool isArchive() const {
     74     return TypeID == ID_Archive;
     75   }
     76 
     77   bool isELF() const {
     78     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
     79   }
     80 
     81   bool isMachO() const {
     82     return TypeID == ID_MachO;
     83   }
     84 
     85   bool isCOFF() const {
     86     return TypeID == ID_COFF;
     87   }
     88 
     89   bool isLittleEndian() const {
     90     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B);
     91   }
     92 };
     93 
     94 /// @brief Create a Binary from Source, autodetecting the file type.
     95 ///
     96 /// @param Source The data to create the Binary from. Ownership is transferred
     97 ///        to Result if successful. If an error is returned, Source is destroyed
     98 ///        by createBinary before returning.
     99 /// @param Result A pointer to the resulting Binary if no error occured.
    100 error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
    101 
    102 error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);
    103 
    104 }
    105 }
    106 
    107 #endif
    108