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