Binary.h revision ed84062812c7b8a82d0e8128a22aa1aa07a14d79
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 20namespace llvm { 21 22class MemoryBuffer; 23class StringRef; 24 25namespace object { 26 27class Binary { 28private: 29 Binary() LLVM_DELETED_FUNCTION; 30 Binary(const Binary &other) LLVM_DELETED_FUNCTION; 31 32 unsigned int TypeID; 33 34protected: 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 59public: 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 90/// @brief Create a Binary from Source, autodetecting the file type. 91/// 92/// @param Source The data to create the Binary from. Ownership is transferred 93/// to Result if successful. If an error is returned, Source is destroyed 94/// by createBinary before returning. 95/// @param Result A pointer to the resulting Binary if no error occured. 96error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result); 97 98error_code createBinary(StringRef Path, OwningPtr<Binary> &Result); 99 100} 101} 102 103#endif 104