1 | //===- Binary.h - A generic binary file -------------------------*- C++ -*-===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | // |
9 | // This file declares the Binary class. |
10 | // |
11 | //===----------------------------------------------------------------------===// |
12 | |
13 | #ifndef LLVM_OBJECT_BINARY_H |
14 | #define LLVM_OBJECT_BINARY_H |
15 | |
16 | #include "llvm-c/Types.h" |
17 | #include "llvm/Object/Error.h" |
18 | #include "llvm/Support/CBindingWrapping.h" |
19 | #include "llvm/Support/Error.h" |
20 | #include "llvm/Support/MemoryBuffer.h" |
21 | #include "llvm/TargetParser/Triple.h" |
22 | #include <memory> |
23 | #include <utility> |
24 | |
25 | namespace llvm { |
26 | |
27 | class LLVMContext; |
28 | class StringRef; |
29 | |
30 | namespace object { |
31 | |
32 | class Binary { |
33 | private: |
34 | unsigned int TypeID; |
35 | |
36 | protected: |
37 | MemoryBufferRef Data; |
38 | |
39 | Binary(unsigned int Type, MemoryBufferRef Source); |
40 | |
41 | enum { |
42 | ID_Archive, |
43 | ID_MachOUniversalBinary, |
44 | ID_COFFImportFile, |
45 | ID_IR, // LLVM IR |
46 | ID_TapiUniversal, // Text-based Dynamic Library Stub file. |
47 | ID_TapiFile, // Text-based Dynamic Library Stub file. |
48 | |
49 | ID_Minidump, |
50 | |
51 | ID_WinRes, // Windows resource (.res) file. |
52 | |
53 | ID_Offload, // Offloading binary file. |
54 | |
55 | // Object and children. |
56 | ID_StartObjects, |
57 | ID_COFF, |
58 | |
59 | ID_XCOFF32, // AIX XCOFF 32-bit |
60 | ID_XCOFF64, // AIX XCOFF 64-bit |
61 | |
62 | ID_ELF32L, // ELF 32-bit, little endian |
63 | ID_ELF32B, // ELF 32-bit, big endian |
64 | ID_ELF64L, // ELF 64-bit, little endian |
65 | ID_ELF64B, // ELF 64-bit, big endian |
66 | |
67 | ID_MachO32L, // MachO 32-bit, little endian |
68 | ID_MachO32B, // MachO 32-bit, big endian |
69 | ID_MachO64L, // MachO 64-bit, little endian |
70 | ID_MachO64B, // MachO 64-bit, big endian |
71 | |
72 | ID_GOFF, |
73 | ID_Wasm, |
74 | |
75 | ID_EndObjects |
76 | }; |
77 | |
78 | static inline unsigned int getELFType(bool isLE, bool is64Bits) { |
79 | if (isLE) |
80 | return is64Bits ? ID_ELF64L : ID_ELF32L; |
81 | else |
82 | return is64Bits ? ID_ELF64B : ID_ELF32B; |
83 | } |
84 | |
85 | static unsigned int getMachOType(bool isLE, bool is64Bits) { |
86 | if (isLE) |
87 | return is64Bits ? ID_MachO64L : ID_MachO32L; |
88 | else |
89 | return is64Bits ? ID_MachO64B : ID_MachO32B; |
90 | } |
91 | |
92 | public: |
93 | Binary() = delete; |
94 | Binary(const Binary &other) = delete; |
95 | virtual ~Binary(); |
96 | |
97 | virtual Error initContent() { return Error::success(); }; |
98 | |
99 | StringRef getData() const; |
100 | StringRef getFileName() const; |
101 | MemoryBufferRef getMemoryBufferRef() const; |
102 | |
103 | // Cast methods. |
104 | unsigned int getType() const { return TypeID; } |
105 | |
106 | // Convenience methods |
107 | bool isObject() const { |
108 | return TypeID > ID_StartObjects && TypeID < ID_EndObjects; |
109 | } |
110 | |
111 | bool isSymbolic() const { |
112 | return isIR() || isObject() || isCOFFImportFile() || isTapiFile(); |
113 | } |
114 | |
115 | bool isArchive() const { return TypeID == ID_Archive; } |
116 | |
117 | bool isMachOUniversalBinary() const { |
118 | return TypeID == ID_MachOUniversalBinary; |
119 | } |
120 | |
121 | bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; } |
122 | |
123 | bool isELF() const { |
124 | return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B; |
125 | } |
126 | |
127 | bool isMachO() const { |
128 | return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B; |
129 | } |
130 | |
131 | bool isCOFF() const { |
132 | return TypeID == ID_COFF; |
133 | } |
134 | |
135 | bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; } |
136 | |
137 | bool isWasm() const { return TypeID == ID_Wasm; } |
138 | |
139 | bool isOffloadFile() const { return TypeID == ID_Offload; } |
140 | |
141 | bool isCOFFImportFile() const { |
142 | return TypeID == ID_COFFImportFile; |
143 | } |
144 | |
145 | bool isIR() const { |
146 | return TypeID == ID_IR; |
147 | } |
148 | |
149 | bool isGOFF() const { return TypeID == ID_GOFF; } |
150 | |
151 | bool isMinidump() const { return TypeID == ID_Minidump; } |
152 | |
153 | bool isTapiFile() const { return TypeID == ID_TapiFile; } |
154 | |
155 | bool isLittleEndian() const { |
156 | return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B || |
157 | TypeID == ID_MachO32B || TypeID == ID_MachO64B || |
158 | TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64); |
159 | } |
160 | |
161 | bool isWinRes() const { return TypeID == ID_WinRes; } |
162 | |
163 | Triple::ObjectFormatType getTripleObjectFormat() const { |
164 | if (isCOFF()) |
165 | return Triple::COFF; |
166 | if (isMachO()) |
167 | return Triple::MachO; |
168 | if (isELF()) |
169 | return Triple::ELF; |
170 | if (isGOFF()) |
171 | return Triple::GOFF; |
172 | return Triple::UnknownObjectFormat; |
173 | } |
174 | |
175 | static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, |
176 | const uint64_t Size) { |
177 | if (Addr + Size < Addr || Addr + Size < Size || |
178 | Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) || |
179 | Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) { |
180 | return errorCodeToError(EC: object_error::unexpected_eof); |
181 | } |
182 | return Error::success(); |
183 | } |
184 | }; |
185 | |
186 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
187 | DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef) |
188 | |
189 | /// Create a Binary from Source, autodetecting the file type. |
190 | /// |
191 | /// @param Source The data to create the Binary from. |
192 | Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source, |
193 | LLVMContext *Context = nullptr, |
194 | bool InitContent = true); |
195 | |
196 | template <typename T> class OwningBinary { |
197 | std::unique_ptr<T> Bin; |
198 | std::unique_ptr<MemoryBuffer> Buf; |
199 | |
200 | public: |
201 | OwningBinary(); |
202 | OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf); |
203 | OwningBinary(OwningBinary<T>&& Other); |
204 | OwningBinary<T> &operator=(OwningBinary<T> &&Other); |
205 | |
206 | std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary(); |
207 | |
208 | T* getBinary(); |
209 | const T* getBinary() const; |
210 | }; |
211 | |
212 | template <typename T> |
213 | OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin, |
214 | std::unique_ptr<MemoryBuffer> Buf) |
215 | : Bin(std::move(Bin)), Buf(std::move(Buf)) {} |
216 | |
217 | template <typename T> OwningBinary<T>::OwningBinary() = default; |
218 | |
219 | template <typename T> |
220 | OwningBinary<T>::OwningBinary(OwningBinary &&Other) |
221 | : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {} |
222 | |
223 | template <typename T> |
224 | OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) { |
225 | Bin = std::move(Other.Bin); |
226 | Buf = std::move(Other.Buf); |
227 | return *this; |
228 | } |
229 | |
230 | template <typename T> |
231 | std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> |
232 | OwningBinary<T>::takeBinary() { |
233 | return std::make_pair(std::move(Bin), std::move(Buf)); |
234 | } |
235 | |
236 | template <typename T> T* OwningBinary<T>::getBinary() { |
237 | return Bin.get(); |
238 | } |
239 | |
240 | template <typename T> const T* OwningBinary<T>::getBinary() const { |
241 | return Bin.get(); |
242 | } |
243 | |
244 | Expected<OwningBinary<Binary>> createBinary(StringRef Path, |
245 | LLVMContext *Context = nullptr, |
246 | bool InitContent = true); |
247 | |
248 | } // end namespace object |
249 | |
250 | } // end namespace llvm |
251 | |
252 | #endif // LLVM_OBJECT_BINARY_H |
253 | |