1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the Binary class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_BINARY_H
15 #define LLVM_OBJECT_BINARY_H
17 #include "llvm/Object/Error.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/FileSystem.h"
31 Binary() LLVM_DELETED_FUNCTION;
32 Binary(const Binary &other) LLVM_DELETED_FUNCTION;
40 Binary(unsigned int Type, MemoryBuffer *Source, bool BufferOwned = true);
44 ID_MachOUniversalBinary,
47 // Object and children.
51 ID_ELF32L, // ELF 32-bit, little endian
52 ID_ELF32B, // ELF 32-bit, big endian
53 ID_ELF64L, // ELF 64-bit, little endian
54 ID_ELF64B, // ELF 64-bit, big endian
56 ID_MachO32L, // MachO 32-bit, little endian
57 ID_MachO32B, // MachO 32-bit, big endian
58 ID_MachO64L, // MachO 64-bit, little endian
59 ID_MachO64B, // MachO 64-bit, big endian
64 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
66 return is64Bits ? ID_ELF64L : ID_ELF32L;
68 return is64Bits ? ID_ELF64B : ID_ELF32B;
71 static unsigned int getMachOType(bool isLE, bool is64Bits) {
73 return is64Bits ? ID_MachO64L : ID_MachO32L;
75 return is64Bits ? ID_MachO64B : ID_MachO32B;
81 StringRef getData() const;
82 StringRef getFileName() const;
85 unsigned int getType() const { return TypeID; }
87 // Convenience methods
88 bool isObject() const {
89 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
92 bool isSymbolic() const {
93 return isIR() || isObject();
96 bool isArchive() const {
97 return TypeID == ID_Archive;
100 bool isMachOUniversalBinary() const {
101 return TypeID == ID_MachOUniversalBinary;
105 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
108 bool isMachO() const {
109 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
112 bool isCOFF() const {
113 return TypeID == ID_COFF;
117 return TypeID == ID_IR;
120 bool isLittleEndian() const {
121 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
122 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
126 /// @brief Create a Binary from Source, autodetecting the file type.
128 /// @param Source The data to create the Binary from. Ownership is transferred
129 /// to the Binary if successful. If an error is returned,
130 /// Source is destroyed by createBinary before returning.
131 ErrorOr<Binary *> createBinary(MemoryBuffer *Source, LLVMContext *Context = 0);
133 ErrorOr<Binary *> createBinary(StringRef Path);