LLVM  16.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"
20 #include "llvm/Support/Error.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:
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 
73 
75  };
76 
77  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
78  if (isLE)
79  return is64Bits ? ID_ELF64L : ID_ELF32L;
80  else
81  return is64Bits ? ID_ELF64B : ID_ELF32B;
82  }
83 
84  static unsigned int getMachOType(bool isLE, bool is64Bits) {
85  if (isLE)
86  return is64Bits ? ID_MachO64L : ID_MachO32L;
87  else
88  return is64Bits ? ID_MachO64B : ID_MachO32B;
89  }
90 
91 public:
92  Binary() = delete;
93  Binary(const Binary &other) = delete;
94  virtual ~Binary();
95 
96  virtual Error initContent() { return Error::success(); };
97 
98  StringRef getData() const;
99  StringRef getFileName() const;
101 
102  // Cast methods.
103  unsigned int getType() const { return TypeID; }
104 
105  // Convenience methods
106  bool isObject() const {
108  }
109 
110  bool isSymbolic() const {
111  return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
112  }
113 
114  bool isArchive() const { return TypeID == ID_Archive; }
115 
116  bool isMachOUniversalBinary() const {
118  }
119 
120  bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
121 
122  bool isELF() const {
123  return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
124  }
125 
126  bool isMachO() const {
127  return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
128  }
129 
130  bool isCOFF() const {
131  return TypeID == ID_COFF;
132  }
133 
134  bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
135 
136  bool isWasm() const { return TypeID == ID_Wasm; }
137 
138  bool isOffloadFile() const { return TypeID == ID_Offload; }
139 
140  bool isCOFFImportFile() const {
141  return TypeID == ID_COFFImportFile;
142  }
143 
144  bool isIR() const {
145  return TypeID == ID_IR;
146  }
147 
148  bool isMinidump() const { return TypeID == ID_Minidump; }
149 
150  bool isTapiFile() const { return TypeID == ID_TapiFile; }
151 
152  bool isLittleEndian() const {
153  return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
156  }
157 
158  bool isWinRes() const { return TypeID == ID_WinRes; }
159 
161  if (isCOFF())
162  return Triple::COFF;
163  if (isMachO())
164  return Triple::MachO;
165  if (isELF())
166  return Triple::ELF;
168  }
169 
170  static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
171  const uint64_t Size) {
172  if (Addr + Size < Addr || Addr + Size < Size ||
173  Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) ||
174  Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) {
176  }
177  return Error::success();
178  }
179 };
180 
181 // Create wrappers for C Binding types (see CBindingWrapping.h).
183 
184 /// Create a Binary from Source, autodetecting the file type.
185 ///
186 /// @param Source The data to create the Binary from.
188  LLVMContext *Context = nullptr,
189  bool InitContent = true);
190 
191 template <typename T> class OwningBinary {
192  std::unique_ptr<T> Bin;
193  std::unique_ptr<MemoryBuffer> Buf;
194 
195 public:
196  OwningBinary();
197  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
198  OwningBinary(OwningBinary<T>&& Other);
199  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
200 
201  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
202 
203  T* getBinary();
204  const T* getBinary() const;
205 };
206 
207 template <typename T>
208 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
209  std::unique_ptr<MemoryBuffer> Buf)
210  : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
211 
212 template <typename T> OwningBinary<T>::OwningBinary() = default;
213 
214 template <typename T>
216  : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
217 
218 template <typename T>
220  Bin = std::move(Other.Bin);
221  Buf = std::move(Other.Buf);
222  return *this;
223 }
224 
225 template <typename T>
226 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
228  return std::make_pair(std::move(Bin), std::move(Buf));
229 }
230 
231 template <typename T> T* OwningBinary<T>::getBinary() {
232  return Bin.get();
233 }
234 
235 template <typename T> const T* OwningBinary<T>::getBinary() const {
236  return Bin.get();
237 }
238 
240  LLVMContext *Context = nullptr,
241  bool InitContent = true);
242 
243 } // end namespace object
244 
245 } // end namespace llvm
246 
247 #endif // LLVM_OBJECT_BINARY_H
llvm::object::object_error::unexpected_eof
@ unexpected_eof
MemoryBuffer.h
llvm::object::Binary::ID_ELF32L
@ ID_ELF32L
Definition: Binary.h:62
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::object::Binary::ID_ELF64L
@ ID_ELF64L
Definition: Binary.h:64
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_XCOFF32
@ ID_XCOFF32
Definition: Binary.h:59
llvm::object::Binary::ID_XCOFF64
@ ID_XCOFF64
Definition: Binary.h:60
llvm::object::Binary::isXCOFF
bool isXCOFF() const
Definition: Binary.h:134
llvm::object::Binary::ID_IR
@ ID_IR
Definition: Binary.h:45
llvm::object::Binary::isLittleEndian
bool isLittleEndian() const
Definition: Binary.h:152
Types.h
llvm::object::Binary::ID_MachO64B
@ ID_MachO64B
Definition: Binary.h:70
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::object::OwningBinary::getBinary
T * getBinary()
Definition: Binary.h:231
Error.h
llvm::object::Binary::ID_Minidump
@ ID_Minidump
Definition: Binary.h:49
llvm::object::Binary::ID_MachO32B
@ ID_MachO32B
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::ID_ELF64B
@ ID_ELF64B
Definition: Binary.h:65
llvm::object::Binary::Data
MemoryBufferRef Data
Definition: Binary.h:37
T
#define T
Definition: Mips16ISelLowering.cpp:341
CBindingWrapping.h
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::object::createBinary
Expected< OwningBinary< Binary > > createBinary(StringRef Path, LLVMContext *Context=nullptr, bool InitContent=true)
Definition: Binary.cpp:103
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
llvm::object::Binary::ID_COFFImportFile
@ ID_COFFImportFile
Definition: Binary.h:44
llvm::SubDirectoryType::Bin
@ Bin
Context
LLVMContext & Context
Definition: NVVMIntrRange.cpp:66
DEFINE_ISA_CONVERSION_FUNCTIONS
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
Definition: CBindingWrapping.h:28
Error.h
llvm::object::Binary::getMemoryBufferRef
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
llvm::object::OwningBinary::OwningBinary
OwningBinary()
llvm::object::Binary::isArchive
bool isArchive() const
Definition: Binary.h:114
llvm::object::Binary::isWinRes
bool isWinRes() const
Definition: Binary.h:158
llvm::errorCodeToError
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:92
llvm::object::Binary::isMinidump
bool isMinidump() const
Definition: Binary.h:148
llvm::object::Binary::isTapiUniversal
bool isTapiUniversal() const
Definition: Binary.h:120
llvm::object::Binary::isCOFFImportFile
bool isCOFFImportFile() const
Definition: Binary.h:140
llvm::object::Binary::ID_Wasm
@ ID_Wasm
Definition: Binary.h:72
llvm::object::OwningBinary::takeBinary
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:227
llvm::object::Binary::getELFType
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:77
llvm::object::Binary::ID_EndObjects
@ ID_EndObjects
Definition: Binary.h:74
llvm::object::Binary::~Binary
virtual ~Binary()
llvm::object::Binary::getMachOType
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:84
TypeID
Type::TypeID TypeID
Definition: Mips16HardFloat.cpp:102
llvm::object::Binary::isSymbolic
bool isSymbolic() const
Definition: Binary.h:110
llvm::Triple::ELF
@ ELF
Definition: Triple.h:275
llvm::object::Binary::ID_StartObjects
@ ID_StartObjects
Definition: Binary.h:56
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:219
uint64_t
llvm::object::Binary::getType
unsigned int getType() const
Definition: Binary.h:103
Addr
uint64_t Addr
Definition: ELFObjHandler.cpp:78
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:67
llvm::object::Binary::ID_Offload
@ ID_Offload
Definition: Binary.h:53
llvm::object::Binary::ID_TapiFile
@ ID_TapiFile
Definition: Binary.h:47
llvm::object::Binary::ID_WinRes
@ ID_WinRes
Definition: Binary.h:51
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:1666
llvm::object::Binary::isELF
bool isELF() const
Definition: Binary.h:122
LLVMBinaryRef
struct LLVMOpaqueBinary * LLVMBinaryRef
Definition: Types.h:168
llvm::object::Binary::ID_MachO64L
@ ID_MachO64L
Definition: Binary.h:69
Triple.h
llvm::object::Binary::isOffloadFile
bool isOffloadFile() const
Definition: Binary.h:138
llvm::object::Binary::isCOFF
bool isCOFF() const
Definition: Binary.h:130
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:98
llvm::object::Binary::ID_ELF32B
@ ID_ELF32B
Definition: Binary.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
llvm::object::Binary::ID_TapiUniversal
@ ID_TapiUniversal
Definition: Binary.h:46
llvm::object::Binary::isWasm
bool isWasm() const
Definition: Binary.h:136
llvm::object::Binary
Definition: Binary.h:32
llvm::Triple::ObjectFormatType
ObjectFormatType
Definition: Triple.h:270
std
Definition: BitVector.h:851
llvm::object::Binary::ID_Archive
@ ID_Archive
Definition: Binary.h:42
llvm::object::OwningBinary
Definition: RuntimeDyld.h:36
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::Triple::MachO
@ MachO
Definition: Triple.h:277
llvm::object::Binary::isMachO
bool isMachO() const
Definition: Binary.h:126
llvm::object::Binary::getTripleObjectFormat
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:160
llvm::object::Binary::initContent
virtual Error initContent()
Definition: Binary.h:96
llvm::Triple::COFF
@ COFF
Definition: Triple.h:273
llvm::object::Binary::getFileName
StringRef getFileName() const
Definition: Binary.cpp:41
llvm::object::Binary::isIR
bool isIR() const
Definition: Binary.h:144
llvm::object::Binary::ID_COFF
@ ID_COFF
Definition: Binary.h:57
llvm::object::Binary::isMachOUniversalBinary
bool isMachOUniversalBinary() const
Definition: Binary.h:116
llvm::object::Binary::checkOffset
static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:170
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:45
llvm::object::Binary::ID_MachOUniversalBinary
@ ID_MachOUniversalBinary
Definition: Binary.h:43
llvm::object::Binary::getData
StringRef getData() const
Definition: Binary.cpp:39
llvm::object::Binary::Binary
Binary()=delete
llvm::object::Binary::isTapiFile
bool isTapiFile() const
Definition: Binary.h:150
llvm::object::Binary::isObject
bool isObject() const
Definition: Binary.h:106
Other
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1247
llvm::object::Binary::ID_MachO32L
@ ID_MachO32L
Definition: Binary.h:67
llvm::Triple::UnknownObjectFormat
@ UnknownObjectFormat
Definition: Triple.h:271