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/ADT/Triple.h"
17 #include "llvm/Object/Error.h"
18 #include "llvm/Support/Error.h"
20 #include <algorithm>
21 #include <memory>
22 #include <utility>
23 
24 namespace llvm {
25 
26 class LLVMContext;
27 class StringRef;
28 
29 namespace object {
30 
31 class Binary {
32 private:
33  unsigned int TypeID;
34 
35 protected:
37 
38  Binary(unsigned int Type, MemoryBufferRef Source);
39 
40  enum {
44  ID_IR, // LLVM IR
45 
46  ID_WinRes, // Windows resource (.res) file.
47 
48  // Object and children.
51 
52  ID_ELF32L, // ELF 32-bit, little endian
53  ID_ELF32B, // ELF 32-bit, big endian
54  ID_ELF64L, // ELF 64-bit, little endian
55  ID_ELF64B, // ELF 64-bit, big endian
56 
57  ID_MachO32L, // MachO 32-bit, little endian
58  ID_MachO32B, // MachO 32-bit, big endian
59  ID_MachO64L, // MachO 64-bit, little endian
60  ID_MachO64B, // MachO 64-bit, big endian
61 
63 
65  };
66 
67  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
68  if (isLE)
69  return is64Bits ? ID_ELF64L : ID_ELF32L;
70  else
71  return is64Bits ? ID_ELF64B : ID_ELF32B;
72  }
73 
74  static unsigned int getMachOType(bool isLE, bool is64Bits) {
75  if (isLE)
76  return is64Bits ? ID_MachO64L : ID_MachO32L;
77  else
78  return is64Bits ? ID_MachO64B : ID_MachO32B;
79  }
80 
81 public:
82  Binary() = delete;
83  Binary(const Binary &other) = delete;
84  virtual ~Binary();
85 
86  StringRef getData() const;
87  StringRef getFileName() const;
89 
90  // Cast methods.
91  unsigned int getType() const { return TypeID; }
92 
93  // Convenience methods
94  bool isObject() const {
95  return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
96  }
97 
98  bool isSymbolic() const { return isIR() || isObject() || isCOFFImportFile(); }
99 
100  bool isArchive() const {
101  return TypeID == ID_Archive;
102  }
103 
104  bool isMachOUniversalBinary() const {
105  return TypeID == ID_MachOUniversalBinary;
106  }
107 
108  bool isELF() const {
109  return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
110  }
111 
112  bool isMachO() const {
113  return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
114  }
115 
116  bool isCOFF() const {
117  return TypeID == ID_COFF;
118  }
119 
120  bool isWasm() const { return TypeID == ID_Wasm; }
121 
122  bool isCOFFImportFile() const {
123  return TypeID == ID_COFFImportFile;
124  }
125 
126  bool isIR() const {
127  return TypeID == ID_IR;
128  }
129 
130  bool isLittleEndian() const {
131  return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
132  TypeID == ID_MachO32B || TypeID == ID_MachO64B);
133  }
134 
135  bool isWinRes() const { return TypeID == ID_WinRes; }
136 
138  if (isCOFF())
139  return Triple::COFF;
140  if (isMachO())
141  return Triple::MachO;
142  if (isELF())
143  return Triple::ELF;
145  }
146 
147  static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
148  const uint64_t Size) {
149  if (Addr + Size < Addr || Addr + Size < Size ||
150  Addr + Size > uintptr_t(M.getBufferEnd()) ||
151  Addr < uintptr_t(M.getBufferStart())) {
153  }
154  return std::error_code();
155  }
156 };
157 
158 /// Create a Binary from Source, autodetecting the file type.
159 ///
160 /// @param Source The data to create the Binary from.
162  LLVMContext *Context = nullptr);
163 
164 template <typename T> class OwningBinary {
165  std::unique_ptr<T> Bin;
166  std::unique_ptr<MemoryBuffer> Buf;
167 
168 public:
169  OwningBinary();
170  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
172  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
173 
174  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
175 
176  T* getBinary();
177  const T* getBinary() const;
178 };
179 
180 template <typename T>
181 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
182  std::unique_ptr<MemoryBuffer> Buf)
183  : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
184 
185 template <typename T> OwningBinary<T>::OwningBinary() = default;
186 
187 template <typename T>
189  : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
190 
191 template <typename T>
193  Bin = std::move(Other.Bin);
194  Buf = std::move(Other.Buf);
195  return *this;
196 }
197 
198 template <typename T>
199 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
201  return std::make_pair(std::move(Bin), std::move(Buf));
202 }
203 
204 template <typename T> T* OwningBinary<T>::getBinary() {
205  return Bin.get();
206 }
207 
208 template <typename T> const T* OwningBinary<T>::getBinary() const {
209  return Bin.get();
210 }
211 
213 
214 } // end namespace object
215 
216 } // end namespace llvm
217 
218 #endif // LLVM_OBJECT_BINARY_H
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:74
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:40
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:147
bool isSymbolic() const
Definition: Binary.h:98
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:44
bool isCOFF() const
Definition: Binary.h:116
Definition: BitVector.h:937
bool isCOFFImportFile() const
Definition: Binary.h:122
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:810
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:42
bool isArchive() const
Definition: Binary.h:100
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:91
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
Expected< OwningBinary< Binary > > createBinary(StringRef Path)
Definition: Binary.cpp:88
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:67
Type::TypeID TypeID
bool isELF() const
Definition: Binary.h:108
bool isLittleEndian() const
Definition: Binary.h:130
bool isWinRes() const
Definition: Binary.h:135
bool isMachOUniversalBinary() const
Definition: Binary.h:104
bool isObject() const
Definition: Binary.h:94
MemoryBufferRef Data
Definition: Binary.h:36
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:137
bool isIR() const
Definition: Binary.h:126
uint32_t Size
Definition: Profile.cpp:46
const char * getBufferStart() const
Definition: MemoryBuffer.h:276
ObjectFormatType
Definition: Triple.h:215
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool isMachO() const
Definition: Binary.h:112
bool isWasm() const
Definition: Binary.h:120
StringRef getData() const
Definition: Binary.cpp:38