LLVM  10.0.0svn
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  StringRef getData() const;
95  StringRef getFileName() const;
97 
98  // Cast methods.
99  unsigned int getType() const { return TypeID; }
100 
101  // Convenience methods
102  bool isObject() const {
103  return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
104  }
105 
106  bool isSymbolic() const {
107  return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
108  }
109 
110  bool isArchive() const { return TypeID == ID_Archive; }
111 
112  bool isMachOUniversalBinary() const {
113  return TypeID == ID_MachOUniversalBinary;
114  }
115 
116  bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
117 
118  bool isELF() const {
119  return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
120  }
121 
122  bool isMachO() const {
123  return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
124  }
125 
126  bool isCOFF() const {
127  return TypeID == ID_COFF;
128  }
129 
130  bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
131 
132  bool isWasm() const { return TypeID == ID_Wasm; }
133 
134  bool isCOFFImportFile() const {
135  return TypeID == ID_COFFImportFile;
136  }
137 
138  bool isIR() const {
139  return TypeID == ID_IR;
140  }
141 
142  bool isMinidump() const { return TypeID == ID_Minidump; }
143 
144  bool isTapiFile() const { return TypeID == ID_TapiFile; }
145 
146  bool isLittleEndian() const {
147  return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
148  TypeID == ID_MachO32B || TypeID == ID_MachO64B);
149  }
150 
151  bool isWinRes() const { return TypeID == ID_WinRes; }
152 
154  if (isCOFF())
155  return Triple::COFF;
156  if (isMachO())
157  return Triple::MachO;
158  if (isELF())
159  return Triple::ELF;
161  }
162 
163  static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
164  const uint64_t Size) {
165  if (Addr + Size < Addr || Addr + Size < Size ||
166  Addr + Size > uintptr_t(M.getBufferEnd()) ||
167  Addr < uintptr_t(M.getBufferStart())) {
169  }
170  return std::error_code();
171  }
172 };
173 
174 // Create wrappers for C Binding types (see CBindingWrapping.h).
176 
177 /// Create a Binary from Source, autodetecting the file type.
178 ///
179 /// @param Source The data to create the Binary from.
181  LLVMContext *Context = nullptr);
182 
183 template <typename T> class OwningBinary {
184  std::unique_ptr<T> Bin;
185  std::unique_ptr<MemoryBuffer> Buf;
186 
187 public:
188  OwningBinary();
189  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
191  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
192 
193  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
194 
195  T* getBinary();
196  const T* getBinary() const;
197 };
198 
199 template <typename T>
200 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
201  std::unique_ptr<MemoryBuffer> Buf)
202  : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
203 
204 template <typename T> OwningBinary<T>::OwningBinary() = default;
205 
206 template <typename T>
208  : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
209 
210 template <typename T>
212  Bin = std::move(Other.Bin);
213  Buf = std::move(Other.Buf);
214  return *this;
215 }
216 
217 template <typename T>
218 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
220  return std::make_pair(std::move(Bin), std::move(Buf));
221 }
222 
223 template <typename T> T* OwningBinary<T>::getBinary() {
224  return Bin.get();
225 }
226 
227 template <typename T> const T* OwningBinary<T>::getBinary() const {
228  return Bin.get();
229 }
230 
232 
233 } // end namespace object
234 
235 } // end namespace llvm
236 
237 #endif // LLVM_OBJECT_BINARY_H
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:82
LLVMContext & Context
This class represents lattice values for constants.
Definition: AllocatorList.h:23
const char * getBufferEnd() const
Definition: MemoryBuffer.h:277
StringRef getFileName() const
Definition: Binary.cpp:42
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:163
bool isSymbolic() const
Definition: Binary.h:106
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:46
bool isTapiUniversal() const
Definition: Binary.h:116
bool isCOFF() const
Definition: Binary.h:126
bool isTapiFile() const
Definition: Binary.h:144
Definition: BitVector.h:937
bool isCOFFImportFile() const
Definition: Binary.h:134
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:877
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:44
struct LLVMOpaqueBinary * LLVMBinaryRef
Definition: Types.h:169
bool isArchive() const
Definition: Binary.h:110
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
unsigned int getType() const
Definition: Binary.h:99
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
Expected< OwningBinary< Binary > > createBinary(StringRef Path)
Definition: Binary.cpp:96
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:75
Type::TypeID TypeID
bool isELF() const
Definition: Binary.h:118
bool isLittleEndian() const
Definition: Binary.h:146
bool isWinRes() const
Definition: Binary.h:151
bool isMachOUniversalBinary() const
Definition: Binary.h:112
bool isObject() const
Definition: Binary.h:102
MemoryBufferRef Data
Definition: Binary.h:37
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:153
bool isXCOFF() const
Definition: Binary.h:130
bool isIR() const
Definition: Binary.h:138
uint32_t Size
Definition: Profile.cpp:46
const char * getBufferStart() const
Definition: MemoryBuffer.h:276
ObjectFormatType
Definition: Triple.h:221
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool isMachO() const
Definition: Binary.h:122
bool isWasm() const
Definition: Binary.h:132
StringRef getData() const
Definition: Binary.cpp:40
bool isMinidump() const
Definition: Binary.h:142