LLVM  9.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 
48 
49  ID_WinRes, // Windows resource (.res) file.
50 
51  // Object and children.
54  ID_XCOFF32, // AIX XCOFF 32-bit
55 
56  ID_ELF32L, // ELF 32-bit, little endian
57  ID_ELF32B, // ELF 32-bit, big endian
58  ID_ELF64L, // ELF 64-bit, little endian
59  ID_ELF64B, // ELF 64-bit, big endian
60 
61  ID_MachO32L, // MachO 32-bit, little endian
62  ID_MachO32B, // MachO 32-bit, big endian
63  ID_MachO64L, // MachO 64-bit, little endian
64  ID_MachO64B, // MachO 64-bit, big endian
65 
67 
69  };
70 
71  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
72  if (isLE)
73  return is64Bits ? ID_ELF64L : ID_ELF32L;
74  else
75  return is64Bits ? ID_ELF64B : ID_ELF32B;
76  }
77 
78  static unsigned int getMachOType(bool isLE, bool is64Bits) {
79  if (isLE)
80  return is64Bits ? ID_MachO64L : ID_MachO32L;
81  else
82  return is64Bits ? ID_MachO64B : ID_MachO32B;
83  }
84 
85 public:
86  Binary() = delete;
87  Binary(const Binary &other) = delete;
88  virtual ~Binary();
89 
90  StringRef getData() const;
91  StringRef getFileName() const;
93 
94  // Cast methods.
95  unsigned int getType() const { return TypeID; }
96 
97  // Convenience methods
98  bool isObject() const {
99  return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
100  }
101 
102  bool isSymbolic() const { return isIR() || isObject() || isCOFFImportFile(); }
103 
104  bool isArchive() const {
105  return TypeID == ID_Archive;
106  }
107 
108  bool isMachOUniversalBinary() const {
109  return TypeID == ID_MachOUniversalBinary;
110  }
111 
112  bool isELF() const {
113  return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
114  }
115 
116  bool isMachO() const {
117  return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
118  }
119 
120  bool isCOFF() const {
121  return TypeID == ID_COFF;
122  }
123 
124  bool isXCOFF() const { return TypeID == ID_XCOFF32; }
125 
126  bool isWasm() const { return TypeID == ID_Wasm; }
127 
128  bool isCOFFImportFile() const {
129  return TypeID == ID_COFFImportFile;
130  }
131 
132  bool isIR() const {
133  return TypeID == ID_IR;
134  }
135 
136  bool isMinidump() const { return TypeID == ID_Minidump; }
137 
138  bool isLittleEndian() const {
139  return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
140  TypeID == ID_MachO32B || TypeID == ID_MachO64B);
141  }
142 
143  bool isWinRes() const { return TypeID == ID_WinRes; }
144 
146  if (isCOFF())
147  return Triple::COFF;
148  if (isMachO())
149  return Triple::MachO;
150  if (isELF())
151  return Triple::ELF;
153  }
154 
155  static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
156  const uint64_t Size) {
157  if (Addr + Size < Addr || Addr + Size < Size ||
158  Addr + Size > uintptr_t(M.getBufferEnd()) ||
159  Addr < uintptr_t(M.getBufferStart())) {
161  }
162  return std::error_code();
163  }
164 };
165 
166 // Create wrappers for C Binding types (see CBindingWrapping.h).
168 
169 /// Create a Binary from Source, autodetecting the file type.
170 ///
171 /// @param Source The data to create the Binary from.
173  LLVMContext *Context = nullptr);
174 
175 template <typename T> class OwningBinary {
176  std::unique_ptr<T> Bin;
177  std::unique_ptr<MemoryBuffer> Buf;
178 
179 public:
180  OwningBinary();
181  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
183  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
184 
185  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
186 
187  T* getBinary();
188  const T* getBinary() const;
189 };
190 
191 template <typename T>
192 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
193  std::unique_ptr<MemoryBuffer> Buf)
194  : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
195 
196 template <typename T> OwningBinary<T>::OwningBinary() = default;
197 
198 template <typename T>
200  : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
201 
202 template <typename T>
204  Bin = std::move(Other.Bin);
205  Buf = std::move(Other.Buf);
206  return *this;
207 }
208 
209 template <typename T>
210 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
212  return std::make_pair(std::move(Bin), std::move(Buf));
213 }
214 
215 template <typename T> T* OwningBinary<T>::getBinary() {
216  return Bin.get();
217 }
218 
219 template <typename T> const T* OwningBinary<T>::getBinary() const {
220  return Bin.get();
221 }
222 
224 
225 } // end namespace object
226 
227 } // end namespace llvm
228 
229 #endif // LLVM_OBJECT_BINARY_H
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:78
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:41
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:155
bool isSymbolic() const
Definition: Binary.h:102
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
bool isCOFF() const
Definition: Binary.h:120
Definition: BitVector.h:937
bool isCOFFImportFile() const
Definition: Binary.h:128
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
#define DEFINE_ISA_CONVERSION_FUNCTIONS(ty, ref)
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:870
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
struct LLVMOpaqueBinary * LLVMBinaryRef
Definition: Types.h:169
bool isArchive() const
Definition: Binary.h:104
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:95
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:92
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:71
Type::TypeID TypeID
bool isELF() const
Definition: Binary.h:112
bool isLittleEndian() const
Definition: Binary.h:138
bool isWinRes() const
Definition: Binary.h:143
bool isMachOUniversalBinary() const
Definition: Binary.h:108
bool isObject() const
Definition: Binary.h:98
MemoryBufferRef Data
Definition: Binary.h:37
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:145
bool isXCOFF() const
Definition: Binary.h:124
bool isIR() const
Definition: Binary.h:132
uint32_t Size
Definition: Profile.cpp:46
const char * getBufferStart() const
Definition: MemoryBuffer.h:276
ObjectFormatType
Definition: Triple.h:220
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool isMachO() const
Definition: Binary.h:116
bool isWasm() const
Definition: Binary.h:126
StringRef getData() const
Definition: Binary.cpp:39
bool isMinidump() const
Definition: Binary.h:136