LLVM  14.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/ADT/Triple.h"
18 #include "llvm/Object/Error.h"
19 #include "llvm/Support/Error.h"
21 #include <algorithm>
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:
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  // Object and children.
56 
57  ID_XCOFF32, // AIX XCOFF 32-bit
58  ID_XCOFF64, // AIX XCOFF 64-bit
59 
60  ID_ELF32L, // ELF 32-bit, little endian
61  ID_ELF32B, // ELF 32-bit, big endian
62  ID_ELF64L, // ELF 64-bit, little endian
63  ID_ELF64B, // ELF 64-bit, big endian
64 
65  ID_MachO32L, // MachO 32-bit, little endian
66  ID_MachO32B, // MachO 32-bit, big endian
67  ID_MachO64L, // MachO 64-bit, little endian
68  ID_MachO64B, // MachO 64-bit, big endian
69 
71 
73  };
74 
75  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
76  if (isLE)
77  return is64Bits ? ID_ELF64L : ID_ELF32L;
78  else
79  return is64Bits ? ID_ELF64B : ID_ELF32B;
80  }
81 
82  static unsigned int getMachOType(bool isLE, bool is64Bits) {
83  if (isLE)
84  return is64Bits ? ID_MachO64L : ID_MachO32L;
85  else
86  return is64Bits ? ID_MachO64B : ID_MachO32B;
87  }
88 
89 public:
90  Binary() = delete;
91  Binary(const Binary &other) = delete;
92  virtual ~Binary();
93 
94  virtual Error initContent() { return Error::success(); };
95 
96  StringRef getData() const;
97  StringRef getFileName() const;
99 
100  // Cast methods.
101  unsigned int getType() const { return TypeID; }
102 
103  // Convenience methods
104  bool isObject() const {
106  }
107 
108  bool isSymbolic() const {
109  return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
110  }
111 
112  bool isArchive() const { return TypeID == ID_Archive; }
113 
114  bool isMachOUniversalBinary() const {
116  }
117 
118  bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
119 
120  bool isELF() const {
121  return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
122  }
123 
124  bool isMachO() const {
125  return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
126  }
127 
128  bool isCOFF() const {
129  return TypeID == ID_COFF;
130  }
131 
132  bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
133 
134  bool isWasm() const { return TypeID == ID_Wasm; }
135 
136  bool isCOFFImportFile() const {
137  return TypeID == ID_COFFImportFile;
138  }
139 
140  bool isIR() const {
141  return TypeID == ID_IR;
142  }
143 
144  bool isMinidump() const { return TypeID == ID_Minidump; }
145 
146  bool isTapiFile() const { return TypeID == ID_TapiFile; }
147 
148  bool isLittleEndian() const {
149  return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
152  }
153 
154  bool isWinRes() const { return TypeID == ID_WinRes; }
155 
157  if (isCOFF())
158  return Triple::COFF;
159  if (isMachO())
160  return Triple::MachO;
161  if (isELF())
162  return Triple::ELF;
164  }
165 
166  static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
167  const uint64_t Size) {
168  if (Addr + Size < Addr || Addr + Size < Size ||
169  Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) ||
170  Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) {
172  }
173  return Error::success();
174  }
175 };
176 
177 // Create wrappers for C Binding types (see CBindingWrapping.h).
179 
180 /// Create a Binary from Source, autodetecting the file type.
181 ///
182 /// @param Source The data to create the Binary from.
184  LLVMContext *Context = nullptr,
185  bool InitContent = true);
186 
187 template <typename T> class OwningBinary {
188  std::unique_ptr<T> Bin;
189  std::unique_ptr<MemoryBuffer> Buf;
190 
191 public:
192  OwningBinary();
193  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
194  OwningBinary(OwningBinary<T>&& Other);
195  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
196 
197  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
198 
199  T* getBinary();
200  const T* getBinary() const;
201 };
202 
203 template <typename T>
204 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
205  std::unique_ptr<MemoryBuffer> Buf)
206  : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
207 
208 template <typename T> OwningBinary<T>::OwningBinary() = default;
209 
210 template <typename T>
212  : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
213 
214 template <typename T>
216  Bin = std::move(Other.Bin);
217  Buf = std::move(Other.Buf);
218  return *this;
219 }
220 
221 template <typename T>
222 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
224  return std::make_pair(std::move(Bin), std::move(Buf));
225 }
226 
227 template <typename T> T* OwningBinary<T>::getBinary() {
228  return Bin.get();
229 }
230 
231 template <typename T> const T* OwningBinary<T>::getBinary() const {
232  return Bin.get();
233 }
234 
236  LLVMContext *Context = nullptr,
237  bool InitContent = true);
238 
239 } // end namespace object
240 
241 } // end namespace llvm
242 
243 #endif // LLVM_OBJECT_BINARY_H
llvm::Check::Size
@ Size
Definition: FileCheck.h:73
llvm::object::object_error::unexpected_eof
@ unexpected_eof
MemoryBuffer.h
llvm::object::Binary::ID_StartObjects
@ ID_StartObjects
Definition: Binary.h:54
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::object::Binary::ID_COFF
@ ID_COFF
Definition: Binary.h:55
llvm::object::Binary::isXCOFF
bool isXCOFF() const
Definition: Binary.h:132
llvm::object::Binary::isLittleEndian
bool isLittleEndian() const
Definition: Binary.h:148
Types.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::object::OwningBinary::getBinary
T * getBinary()
Definition: Binary.h:227
Error.h
llvm::object::Binary::ID_MachO64B
@ ID_MachO64B
Definition: Binary.h:68
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::object::Binary::Data
MemoryBufferRef Data
Definition: Binary.h:37
llvm::object::Binary::ID_WinRes
@ ID_WinRes
Definition: Binary.h:51
T
#define T
Definition: Mips16ISelLowering.cpp:341
llvm::object::Binary::ID_MachO32B
@ ID_MachO32B
Definition: Binary.h:66
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::object::Binary::ID_ELF32L
@ ID_ELF32L
Definition: Binary.h:60
llvm::object::createBinary
Expected< OwningBinary< Binary > > createBinary(StringRef Path, LLVMContext *Context=nullptr, bool InitContent=true)
Definition: Binary.cpp:99
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::Binary::ID_Archive
@ ID_Archive
Definition: Binary.h:42
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
llvm::object::Binary::ID_MachOUniversalBinary
@ ID_MachOUniversalBinary
Definition: Binary.h:43
DEFINE_ISA_CONVERSION_FUNCTIONS
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
Definition: CBindingWrapping.h:28
llvm::object::Binary::ID_TapiUniversal
@ ID_TapiUniversal
Definition: Binary.h:46
llvm::object::Binary::ID_Minidump
@ ID_Minidump
Definition: Binary.h:49
Error.h
llvm::object::Binary::getMemoryBufferRef
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:44
llvm::object::OwningBinary::OwningBinary
OwningBinary()
llvm::object::Binary::isArchive
bool isArchive() const
Definition: Binary.h:112
llvm::object::Binary::isWinRes
bool isWinRes() const
Definition: Binary.h:154
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
llvm::object::Binary::isMinidump
bool isMinidump() const
Definition: Binary.h:144
llvm::object::Binary::isTapiUniversal
bool isTapiUniversal() const
Definition: Binary.h:118
llvm::object::Binary::ID_ELF64L
@ ID_ELF64L
Definition: Binary.h:62
llvm::object::Binary::isCOFFImportFile
bool isCOFFImportFile() const
Definition: Binary.h:136
llvm::object::OwningBinary::takeBinary
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:223
llvm::object::Binary::ID_MachO32L
@ ID_MachO32L
Definition: Binary.h:65
llvm::object::Binary::getELFType
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:75
llvm::object::Binary::~Binary
virtual ~Binary()
llvm::object::Binary::ID_Wasm
@ ID_Wasm
Definition: Binary.h:70
llvm::object::Binary::getMachOType
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:82
TypeID
Type::TypeID TypeID
Definition: Mips16HardFloat.cpp:102
llvm::object::Binary::isSymbolic
bool isSymbolic() const
Definition: Binary.h:108
llvm::Triple::ELF
@ ELF
Definition: Triple.h:235
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::object::OwningBinary::operator=
OwningBinary< T > & operator=(OwningBinary< T > &&Other)
Definition: Binary.h:215
llvm::object::Binary::ID_COFFImportFile
@ ID_COFFImportFile
Definition: Binary.h:44
llvm::object::Binary::getType
unsigned int getType() const
Definition: Binary.h:101
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:80
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::object::Binary::ID_XCOFF32
@ ID_XCOFF32
Definition: Binary.h:57
llvm::move
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:1605
llvm::object::Binary::ID_XCOFF64
@ ID_XCOFF64
Definition: Binary.h:58
llvm::object::Binary::isELF
bool isELF() const
Definition: Binary.h:120
LLVMBinaryRef
struct LLVMOpaqueBinary * LLVMBinaryRef
Definition: Types.h:168
llvm::object::Binary::ID_MachO64L
@ ID_MachO64L
Definition: Binary.h:67
Triple.h
llvm::object::Binary::isCOFF
bool isCOFF() const
Definition: Binary.h:128
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::object::Binary::isWasm
bool isWasm() const
Definition: Binary.h:134
llvm::object::Binary
Definition: Binary.h:32
llvm::object::Binary::ID_IR
@ ID_IR
Definition: Binary.h:45
llvm::Triple::ObjectFormatType
ObjectFormatType
Definition: Triple.h:231
std
Definition: BitVector.h:838
llvm::object::Binary::ID_ELF32B
@ ID_ELF32B
Definition: Binary.h:61
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::Triple::MachO
@ MachO
Definition: Triple.h:237
llvm::object::Binary::isMachO
bool isMachO() const
Definition: Binary.h:124
llvm::object::Binary::getTripleObjectFormat
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:156
llvm::object::Binary::ID_TapiFile
@ ID_TapiFile
Definition: Binary.h:47
llvm::object::Binary::initContent
virtual Error initContent()
Definition: Binary.h:94
llvm::Triple::COFF
@ COFF
Definition: Triple.h:234
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition: Binary.cpp:42
llvm::object::Binary::isIR
bool isIR() const
Definition: Binary.h:140
llvm::object::Binary::ID_ELF64B
@ ID_ELF64B
Definition: Binary.h:63
llvm::object::Binary::isMachOUniversalBinary
bool isMachOUniversalBinary() const
Definition: Binary.h:114
llvm::object::Binary::checkOffset
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:166
llvm::object::createBinary
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:46
llvm::object::Binary::getData
StringRef getData() const
Definition: Binary.cpp:40
llvm::object::Binary::ID_EndObjects
@ ID_EndObjects
Definition: Binary.h:72
llvm::object::Binary::Binary
Binary()=delete
llvm::object::Binary::isTapiFile
bool isTapiFile() const
Definition: Binary.h:146
llvm::object::Binary::isObject
bool isObject() const
Definition: Binary.h:104
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1172
llvm::Triple::UnknownObjectFormat
@ UnknownObjectFormat
Definition: Triple.h:232