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(); // = delete
     30   Binary(const Binary &other); // = delete
     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 isLittleEndian, bool is64Bits) {
     53     if (isLittleEndian)
     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   static inline bool classof(const Binary *v) { return true; }
     68 
     69   // Convenience methods
     70   bool isObject() const {
     71     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
     72   }
     73 
     74   bool isArchive() const {
     75     return TypeID == ID_Archive;
     76   }
     77 
     78   bool isELF() const {
     79     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
     80   }
     81 
     82   bool isMachO() const {
     83     return TypeID == ID_MachO;
     84   }
     85 
     86   bool isCOFF() const {
     87     return TypeID == ID_COFF;
     88   }
     89 };
     90 
     91 /// @brief Create a Binary from Source, autodetecting the file type.
     92 ///
     93 /// @param Source The data to create the Binary from. Ownership is transfered
     94 ///        to Result if successful. If an error is returned, Source is destroyed
     95 ///        by createBinary before returning.
     96 /// @param Result A pointer to the resulting Binary if no error occured.
     97 error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
     98 
     99 error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);
    100 
    101 }
    102 }
    103 
    104 #endif
    105