LLVM 22.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"
20#include "llvm/Support/Error.h"
23#include <memory>
24#include <utility>
25
26namespace llvm {
27
28class LLVMContext;
29class StringRef;
30
31namespace object {
32
34private:
35 unsigned int TypeID;
36
37protected:
39
40 Binary(unsigned int Type, MemoryBufferRef Source);
41
42 enum {
46 ID_IR, // LLVM IR
47 ID_TapiUniversal, // Text-based Dynamic Library Stub file.
48 ID_TapiFile, // Text-based Dynamic Library Stub file.
49
51
52 ID_WinRes, // Windows resource (.res) file.
53
54 ID_Offload, // Offloading binary file.
55
56 // Object and children.
59
60 ID_XCOFF32, // AIX XCOFF 32-bit
61 ID_XCOFF64, // AIX XCOFF 64-bit
62
63 ID_ELF32L, // ELF 32-bit, little endian
64 ID_ELF32B, // ELF 32-bit, big endian
65 ID_ELF64L, // ELF 64-bit, little endian
66 ID_ELF64B, // ELF 64-bit, big endian
67
68 ID_MachO32L, // MachO 32-bit, little endian
69 ID_MachO32B, // MachO 32-bit, big endian
70 ID_MachO64L, // MachO 64-bit, little endian
71 ID_MachO64B, // MachO 64-bit, big endian
72
76
77 ID_EndObjects
78 };
79
80 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
81 if (isLE)
82 return is64Bits ? ID_ELF64L : ID_ELF32L;
83 else
84 return is64Bits ? ID_ELF64B : ID_ELF32B;
85 }
86
87 static unsigned int getMachOType(bool isLE, bool is64Bits) {
88 if (isLE)
89 return is64Bits ? ID_MachO64L : ID_MachO32L;
90 else
91 return is64Bits ? ID_MachO64B : ID_MachO32B;
92 }
93
94public:
95 Binary() = delete;
96 Binary(const Binary &other) = delete;
97 virtual ~Binary();
98
99 virtual Error initContent() { return Error::success(); };
100
101 StringRef getData() const;
102 StringRef getFileName() const;
103 MemoryBufferRef getMemoryBufferRef() const;
104
105 // Cast methods.
106 unsigned int getType() const { return TypeID; }
107
108 // Convenience methods
109 bool isObject() const {
110 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
111 }
112
113 bool isSymbolic() const {
114 return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
115 }
116
117 bool isArchive() const { return TypeID == ID_Archive; }
118
120 return TypeID == ID_MachOUniversalBinary;
121 }
122
123 bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
124
125 bool isELF() const {
126 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
127 }
128
129 bool isMachO() const {
130 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
131 }
132
133 bool isCOFF() const {
134 return TypeID == ID_COFF;
135 }
136
137 bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
138
139 bool isWasm() const { return TypeID == ID_Wasm; }
140
141 bool isOffloadFile() const { return TypeID == ID_Offload; }
142
143 bool isCOFFImportFile() const {
144 return TypeID == ID_COFFImportFile;
145 }
146
147 bool isIR() const {
148 return TypeID == ID_IR;
149 }
150
151 bool isGOFF() const { return TypeID == ID_GOFF; }
152
153 bool isMinidump() const { return TypeID == ID_Minidump; }
154
155 bool isTapiFile() const { return TypeID == ID_TapiFile; }
156
157 bool isLittleEndian() const {
158 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
159 TypeID == ID_MachO32B || TypeID == ID_MachO64B ||
160 TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64);
161 }
162
163 bool isWinRes() const { return TypeID == ID_WinRes; }
164
165 bool isDXContainer() const { return TypeID == ID_DXContainer; }
166
168 if (isCOFF())
169 return Triple::COFF;
170 if (isMachO())
171 return Triple::MachO;
172 if (isELF())
173 return Triple::ELF;
174 if (isGOFF())
175 return Triple::GOFF;
176 return Triple::UnknownObjectFormat;
177 }
178
180 const uint64_t Size) {
181 if (Addr + Size < Addr || Addr + Size < Size ||
182 Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) ||
183 Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) {
184 return errorCodeToError(object_error::unexpected_eof);
185 }
186 return Error::success();
187 }
188};
189
190// Create wrappers for C Binding types (see CBindingWrapping.h).
192
193/// Create a Binary from Source, autodetecting the file type.
194///
195/// @param Source The data to create the Binary from.
197createBinary(MemoryBufferRef Source, LLVMContext *Context = nullptr,
198 bool InitContent = true);
199
200template <typename T> class OwningBinary {
201 std::unique_ptr<T> Bin;
202 std::unique_ptr<MemoryBuffer> Buf;
203
204public:
206 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
209
210 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
211
213 const T* getBinary() const;
214};
215
216template <typename T>
218 std::unique_ptr<MemoryBuffer> Buf)
219 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
220
221template <typename T> OwningBinary<T>::OwningBinary() = default;
222
223template <typename T>
225 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
226
227template <typename T>
229 Bin = std::move(Other.Bin);
230 Buf = std::move(Other.Buf);
231 return *this;
232}
233
234template <typename T>
235std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
237 return std::make_pair(std::move(Bin), std::move(Buf));
238}
239
240template <typename T> T* OwningBinary<T>::getBinary() {
241 return Bin.get();
242}
243
244template <typename T> const T* OwningBinary<T>::getBinary() const {
245 return Bin.get();
246}
247
249createBinary(StringRef Path, LLVMContext *Context = nullptr,
250 bool InitContent = true);
251
252} // end namespace object
253
254} // end namespace llvm
255
256#endif // LLVM_OBJECT_BINARY_H
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
static Expected< StringRef > getFileName(const DebugStringTableSubsectionRef &Strings, const DebugChecksumsSubsectionRef &Checksums, uint32_t FileID)
#define LLVM_ABI
Definition: Compiler.h:213
uint64_t Addr
uint64_t Size
Type::TypeID TypeID
Lightweight error class with error context and mandatory checking.
Definition: Error.h:159
Tagged union holding either a T or a Error.
Definition: Error.h:485
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
ObjectFormatType
Definition: Triple.h:314
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:139
bool isTapiUniversal() const
Definition: Binary.h:123
MemoryBufferRef Data
Definition: Binary.h:38
bool isIR() const
Definition: Binary.h:147
bool isMinidump() const
Definition: Binary.h:153
bool isMachOUniversalBinary() const
Definition: Binary.h:119
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:80
bool isXCOFF() const
Definition: Binary.h:137
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:167
bool isCOFFImportFile() const
Definition: Binary.h:143
unsigned int getType() const
Definition: Binary.h:106
bool isLittleEndian() const
Definition: Binary.h:157
virtual Error initContent()
Definition: Binary.h:99
bool isOffloadFile() const
Definition: Binary.h:141
bool isMachO() const
Definition: Binary.h:129
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:87
bool isArchive() const
Definition: Binary.h:117
bool isDXContainer() const
Definition: Binary.h:165
bool isCOFF() const
Definition: Binary.h:133
bool isTapiFile() const
Definition: Binary.h:155
bool isObject() const
Definition: Binary.h:109
bool isSymbolic() const
Definition: Binary.h:113
Binary(const Binary &other)=delete
bool isELF() const
Definition: Binary.h:125
bool isWinRes() const
Definition: Binary.h:163
bool isGOFF() const
Definition: Binary.h:151
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:179
const T * getBinary() const
Definition: Binary.h:244
OwningBinary< T > & operator=(OwningBinary< T > &&Other)
Definition: Binary.h:228
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:236
OwningBinary(std::unique_ptr< T > Bin, std::unique_ptr< MemoryBuffer > Buf)
Definition: Binary.h:217
OwningBinary(OwningBinary< T > &&Other)
Definition: Binary.h:224
struct LLVMOpaqueBinary * LLVMBinaryRef
Definition: Types.h:170
LLVM_ABI 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
@ Other
Any other memory.
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:1886
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:111
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856