LLVM 17.0.0git
Binary.h
Go to the documentation of this file.
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"
19#include "llvm/Support/Error.h"
22#include <memory>
23#include <utility>
24
25namespace llvm {
26
27class LLVMContext;
28class StringRef;
29
30namespace object {
31
32class Binary {
33private:
34 unsigned int TypeID;
35
36protected:
38
39 Binary(unsigned int Type, MemoryBufferRef Source);
40
41 enum {
45 ID_IR, // LLVM IR
46 ID_TapiUniversal, // Text-based Dynamic Library Stub file.
47 ID_TapiFile, // Text-based Dynamic Library Stub file.
48
50
51 ID_WinRes, // Windows resource (.res) file.
52
53 ID_Offload, // Offloading binary file.
54
55 // Object and children.
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
74
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
92public:
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;
102
103 // Cast methods.
104 unsigned int getType() const { return TypeID; }
105
106 // Convenience methods
107 bool isObject() const {
109 }
110
111 bool isSymbolic() const {
112 return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
113 }
114
115 bool isArchive() const { return TypeID == ID_Archive; }
116
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 ||
159 }
160
161 bool isWinRes() const { return TypeID == ID_WinRes; }
162
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;
173 }
174
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())) {
181 }
182 return Error::success();
183 }
184};
185
186// Create wrappers for C Binding types (see CBindingWrapping.h).
188
189/// Create a Binary from Source, autodetecting the file type.
190///
191/// @param Source The data to create the Binary from.
193 LLVMContext *Context = nullptr,
194 bool InitContent = true);
195
196template <typename T> class OwningBinary {
197 std::unique_ptr<T> Bin;
198 std::unique_ptr<MemoryBuffer> Buf;
199
200public:
202 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
205
206 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
207
209 const T* getBinary() const;
210};
211
212template <typename T>
214 std::unique_ptr<MemoryBuffer> Buf)
215 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
216
217template <typename T> OwningBinary<T>::OwningBinary() = default;
218
219template <typename T>
221 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
222
223template <typename T>
225 Bin = std::move(Other.Bin);
226 Buf = std::move(Other.Buf);
227 return *this;
228}
229
230template <typename T>
231std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
233 return std::make_pair(std::move(Bin), std::move(Buf));
234}
235
236template <typename T> T* OwningBinary<T>::getBinary() {
237 return Bin.get();
238}
239
240template <typename T> const T* OwningBinary<T>::getBinary() const {
241 return Bin.get();
242}
243
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
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
uint64_t Addr
uint64_t Size
Type::TypeID TypeID
Lightweight error class with error context and mandatory checking.
Definition: Error.h:156
static ErrorSuccess success()
Create a success value.
Definition: Error.h:330
Tagged union holding either a T or a Error.
Definition: Error.h:470
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
ObjectFormatType
Definition: Triple.h:281
@ UnknownObjectFormat
Definition: Triple.h:282
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isWasm() const
Definition: Binary.h:137
bool isTapiUniversal() const
Definition: Binary.h:121
MemoryBufferRef Data
Definition: Binary.h:37
bool isIR() const
Definition: Binary.h:145
bool isMinidump() const
Definition: Binary.h:151
bool isMachOUniversalBinary() const
Definition: Binary.h:117
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:78
bool isXCOFF() const
Definition: Binary.h:135
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:163
StringRef getData() const
Definition: Binary.cpp:39
bool isCOFFImportFile() const
Definition: Binary.h:141
unsigned int getType() const
Definition: Binary.h:104
bool isLittleEndian() const
Definition: Binary.h:155
virtual Error initContent()
Definition: Binary.h:97
bool isOffloadFile() const
Definition: Binary.h:139
bool isMachO() const
Definition: Binary.h:127
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:85
bool isArchive() const
Definition: Binary.h:115
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
bool isCOFF() const
Definition: Binary.h:131
StringRef getFileName() const
Definition: Binary.cpp:41
bool isTapiFile() const
Definition: Binary.h:153
bool isObject() const
Definition: Binary.h:107
bool isSymbolic() const
Definition: Binary.h:111
Binary(const Binary &other)=delete
bool isELF() const
Definition: Binary.h:123
bool isWinRes() const
Definition: Binary.h:161
bool isGOFF() const
Definition: Binary.h:149
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:175
const T * getBinary() const
Definition: Binary.h:240
OwningBinary< T > & operator=(OwningBinary< T > &&Other)
Definition: Binary.h:224
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:232
OwningBinary(std::unique_ptr< T > Bin, std::unique_ptr< MemoryBuffer > Buf)
Definition: Binary.h:213
OwningBinary(OwningBinary< T > &&Other)
Definition: Binary.h:220
struct LLVMOpaqueBinary * LLVMBinaryRef
Definition: Types.h:165
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr, bool InitContent=true)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1946
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:92
Definition: BitVector.h:858