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/Object/Error.h"
     18 #include "llvm/Support/ErrorOr.h"
     19 #include "llvm/Support/FileSystem.h"
     20 #include "llvm/Support/MemoryBuffer.h"
     21 
     22 namespace llvm {
     23 
     24 class LLVMContext;
     25 class StringRef;
     26 
     27 namespace object {
     28 
     29 class Binary {
     30 private:
     31   Binary() = delete;
     32   Binary(const Binary &other) = delete;
     33 
     34   unsigned int TypeID;
     35 
     36 protected:
     37   MemoryBufferRef Data;
     38 
     39   Binary(unsigned int Type, MemoryBufferRef Source);
     40 
     41   enum {
     42     ID_Archive,
     43     ID_MachOUniversalBinary,
     44     ID_COFFImportFile,
     45     ID_IR,                 // LLVM IR
     46     ID_ModuleSummaryIndex, // Module summary index
     47 
     48     // Object and children.
     49     ID_StartObjects,
     50     ID_COFF,
     51 
     52     ID_ELF32L, // ELF 32-bit, little endian
     53     ID_ELF32B, // ELF 32-bit, big endian
     54     ID_ELF64L, // ELF 64-bit, little endian
     55     ID_ELF64B, // ELF 64-bit, big endian
     56 
     57     ID_MachO32L, // MachO 32-bit, little endian
     58     ID_MachO32B, // MachO 32-bit, big endian
     59     ID_MachO64L, // MachO 64-bit, little endian
     60     ID_MachO64B, // MachO 64-bit, big endian
     61 
     62     ID_Wasm,
     63 
     64     ID_EndObjects
     65   };
     66 
     67   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
     68     if (isLE)
     69       return is64Bits ? ID_ELF64L : ID_ELF32L;
     70     else
     71       return is64Bits ? ID_ELF64B : ID_ELF32B;
     72   }
     73 
     74   static unsigned int getMachOType(bool isLE, bool is64Bits) {
     75     if (isLE)
     76       return is64Bits ? ID_MachO64L : ID_MachO32L;
     77     else
     78       return is64Bits ? ID_MachO64B : ID_MachO32B;
     79   }
     80 
     81 public:
     82   virtual ~Binary();
     83 
     84   StringRef getData() const;
     85   StringRef getFileName() const;
     86   MemoryBufferRef getMemoryBufferRef() const;
     87 
     88   // Cast methods.
     89   unsigned int getType() const { return TypeID; }
     90 
     91   // Convenience methods
     92   bool isObject() const {
     93     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
     94   }
     95 
     96   bool isSymbolic() const {
     97     return isIR() || isObject();
     98   }
     99 
    100   bool isArchive() const {
    101     return TypeID == ID_Archive;
    102   }
    103 
    104   bool isMachOUniversalBinary() const {
    105     return TypeID == ID_MachOUniversalBinary;
    106   }
    107 
    108   bool isELF() const {
    109     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
    110   }
    111 
    112   bool isMachO() const {
    113     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
    114   }
    115 
    116   bool isCOFF() const {
    117     return TypeID == ID_COFF;
    118   }
    119 
    120   bool isWasm() const { return TypeID == ID_Wasm; }
    121 
    122   bool isCOFFImportFile() const {
    123     return TypeID == ID_COFFImportFile;
    124   }
    125 
    126   bool isIR() const {
    127     return TypeID == ID_IR;
    128   }
    129 
    130   bool isModuleSummaryIndex() const { return TypeID == ID_ModuleSummaryIndex; }
    131 
    132   bool isLittleEndian() const {
    133     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
    134              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
    135   }
    136 };
    137 
    138 /// @brief Create a Binary from Source, autodetecting the file type.
    139 ///
    140 /// @param Source The data to create the Binary from.
    141 Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
    142                                                LLVMContext *Context = nullptr);
    143 
    144 template <typename T> class OwningBinary {
    145   std::unique_ptr<T> Bin;
    146   std::unique_ptr<MemoryBuffer> Buf;
    147 
    148 public:
    149   OwningBinary();
    150   OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
    151   OwningBinary(OwningBinary<T>&& Other);
    152   OwningBinary<T> &operator=(OwningBinary<T> &&Other);
    153 
    154   std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
    155 
    156   T* getBinary();
    157   const T* getBinary() const;
    158 };
    159 
    160 template <typename T>
    161 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
    162                               std::unique_ptr<MemoryBuffer> Buf)
    163     : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
    164 
    165 template <typename T> OwningBinary<T>::OwningBinary() {}
    166 
    167 template <typename T>
    168 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
    169     : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
    170 
    171 template <typename T>
    172 OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
    173   Bin = std::move(Other.Bin);
    174   Buf = std::move(Other.Buf);
    175   return *this;
    176 }
    177 
    178 template <typename T>
    179 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
    180 OwningBinary<T>::takeBinary() {
    181   return std::make_pair(std::move(Bin), std::move(Buf));
    182 }
    183 
    184 template <typename T> T* OwningBinary<T>::getBinary() {
    185   return Bin.get();
    186 }
    187 
    188 template <typename T> const T* OwningBinary<T>::getBinary() const {
    189   return Bin.get();
    190 }
    191 
    192 Expected<OwningBinary<Binary>> createBinary(StringRef Path);
    193 }
    194 }
    195 
    196 #endif
    197