LLVM API Documentation

Binary.h
Go to the documentation of this file.
00001 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the Binary class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_OBJECT_BINARY_H
00015 #define LLVM_OBJECT_BINARY_H
00016 
00017 #include "llvm/Object/Error.h"
00018 #include "llvm/Support/ErrorOr.h"
00019 #include "llvm/Support/FileSystem.h"
00020 #include "llvm/Support/MemoryBuffer.h"
00021 
00022 namespace llvm {
00023 
00024 class LLVMContext;
00025 class StringRef;
00026 
00027 namespace object {
00028 
00029 class Binary {
00030 private:
00031   Binary() LLVM_DELETED_FUNCTION;
00032   Binary(const Binary &other) LLVM_DELETED_FUNCTION;
00033 
00034   unsigned int TypeID;
00035 
00036 protected:
00037   MemoryBufferRef Data;
00038 
00039   Binary(unsigned int Type, MemoryBufferRef Source);
00040 
00041   enum {
00042     ID_Archive,
00043     ID_MachOUniversalBinary,
00044     ID_IR, // LLVM IR
00045 
00046     // Object and children.
00047     ID_StartObjects,
00048     ID_COFF,
00049 
00050     ID_ELF32L, // ELF 32-bit, little endian
00051     ID_ELF32B, // ELF 32-bit, big endian
00052     ID_ELF64L, // ELF 64-bit, little endian
00053     ID_ELF64B, // ELF 64-bit, big endian
00054 
00055     ID_MachO32L, // MachO 32-bit, little endian
00056     ID_MachO32B, // MachO 32-bit, big endian
00057     ID_MachO64L, // MachO 64-bit, little endian
00058     ID_MachO64B, // MachO 64-bit, big endian
00059 
00060     ID_EndObjects
00061   };
00062 
00063   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
00064     if (isLE)
00065       return is64Bits ? ID_ELF64L : ID_ELF32L;
00066     else
00067       return is64Bits ? ID_ELF64B : ID_ELF32B;
00068   }
00069 
00070   static unsigned int getMachOType(bool isLE, bool is64Bits) {
00071     if (isLE)
00072       return is64Bits ? ID_MachO64L : ID_MachO32L;
00073     else
00074       return is64Bits ? ID_MachO64B : ID_MachO32B;
00075   }
00076 
00077 public:
00078   virtual ~Binary();
00079 
00080   StringRef getData() const;
00081   StringRef getFileName() const;
00082   MemoryBufferRef getMemoryBufferRef() const;
00083 
00084   // Cast methods.
00085   unsigned int getType() const { return TypeID; }
00086 
00087   // Convenience methods
00088   bool isObject() const {
00089     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
00090   }
00091 
00092   bool isSymbolic() const {
00093     return isIR() || isObject();
00094   }
00095 
00096   bool isArchive() const {
00097     return TypeID == ID_Archive;
00098   }
00099 
00100   bool isMachOUniversalBinary() const {
00101     return TypeID == ID_MachOUniversalBinary;
00102   }
00103 
00104   bool isELF() const {
00105     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
00106   }
00107 
00108   bool isMachO() const {
00109     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
00110   }
00111 
00112   bool isCOFF() const {
00113     return TypeID == ID_COFF;
00114   }
00115 
00116   bool isIR() const {
00117     return TypeID == ID_IR;
00118   }
00119 
00120   bool isLittleEndian() const {
00121     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
00122              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
00123   }
00124 };
00125 
00126 /// @brief Create a Binary from Source, autodetecting the file type.
00127 ///
00128 /// @param Source The data to create the Binary from.
00129 ErrorOr<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
00130                                               LLVMContext *Context = nullptr);
00131 
00132 template <typename T> class OwningBinary {
00133   std::unique_ptr<T> Bin;
00134   std::unique_ptr<MemoryBuffer> Buf;
00135 
00136 public:
00137   OwningBinary();
00138   OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
00139   OwningBinary(OwningBinary<T>&& Other);
00140   OwningBinary<T> &operator=(OwningBinary<T> &&Other);
00141 
00142   std::unique_ptr<T> &getBinary();
00143   std::unique_ptr<MemoryBuffer> &getBuffer();
00144 };
00145 
00146 template <typename T>
00147 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
00148                               std::unique_ptr<MemoryBuffer> Buf)
00149     : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
00150 
00151 template <typename T> OwningBinary<T>::OwningBinary() {}
00152 
00153 template <typename T>
00154 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
00155     : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
00156 
00157 template <typename T>
00158 OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
00159   Bin = std::move(Other.Bin);
00160   Buf = std::move(Other.Buf);
00161   return *this;
00162 }
00163 
00164 template <typename T> std::unique_ptr<T> &OwningBinary<T>::getBinary() {
00165   return Bin;
00166 }
00167 
00168 template <typename T>
00169 std::unique_ptr<MemoryBuffer> &OwningBinary<T>::getBuffer() {
00170   return Buf;
00171 }
00172 
00173 ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);
00174 }
00175 }
00176 
00177 #endif