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     ID_MachOUniversalBinary,
     42     // Object and children.
     43     ID_StartObjects,
     44     ID_COFF,
     45 
     46     ID_ELF32L, // ELF 32-bit, little endian
     47     ID_ELF32B, // ELF 32-bit, big endian
     48     ID_ELF64L, // ELF 64-bit, little endian
     49     ID_ELF64B, // ELF 64-bit, big endian
     50 
     51     ID_MachO32L, // MachO 32-bit, little endian
     52     ID_MachO32B, // MachO 32-bit, big endian
     53     ID_MachO64L, // MachO 64-bit, little endian
     54     ID_MachO64B, // MachO 64-bit, big endian
     55 
     56     ID_EndObjects
     57   };
     58 
     59   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
     60     if (isLE)
     61       return is64Bits ? ID_ELF64L : ID_ELF32L;
     62     else
     63       return is64Bits ? ID_ELF64B : ID_ELF32B;
     64   }
     65 
     66   static unsigned int getMachOType(bool isLE, bool is64Bits) {
     67     if (isLE)
     68       return is64Bits ? ID_MachO64L : ID_MachO32L;
     69     else
     70       return is64Bits ? ID_MachO64B : ID_MachO32B;
     71   }
     72 
     73 public:
     74   virtual ~Binary();
     75 
     76   StringRef getData() const;
     77   StringRef getFileName() const;
     78 
     79   // Cast methods.
     80   unsigned int getType() const { return TypeID; }
     81 
     82   // Convenience methods
     83   bool isObject() const {
     84     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
     85   }
     86 
     87   bool isArchive() const {
     88     return TypeID == ID_Archive;
     89   }
     90 
     91   bool isMachOUniversalBinary() const {
     92     return TypeID == ID_MachOUniversalBinary;
     93   }
     94 
     95   bool isELF() const {
     96     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
     97   }
     98 
     99   bool isMachO() const {
    100     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
    101   }
    102 
    103   bool isCOFF() const {
    104     return TypeID == ID_COFF;
    105   }
    106 
    107   bool isLittleEndian() const {
    108     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
    109              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
    110   }
    111 };
    112 
    113 /// @brief Create a Binary from Source, autodetecting the file type.
    114 ///
    115 /// @param Source The data to create the Binary from. Ownership is transferred
    116 ///        to Result if successful. If an error is returned, Source is destroyed
    117 ///        by createBinary before returning.
    118 /// @param Result A pointer to the resulting Binary if no error occured.
    119 error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
    120 
    121 error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);
    122 
    123 }
    124 }
    125 
    126 #endif
    127