LLVM  5.0.0
Binary.h
Go to the documentation of this file.
1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the Binary class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_OBJECT_BINARY_H
15 #define LLVM_OBJECT_BINARY_H
16 
17 #include "llvm/ADT/Triple.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  // Object and children.
49 
50  ID_ELF32L, // ELF 32-bit, little endian
51  ID_ELF32B, // ELF 32-bit, big endian
52  ID_ELF64L, // ELF 64-bit, little endian
53  ID_ELF64B, // ELF 64-bit, big endian
54 
55  ID_MachO32L, // MachO 32-bit, little endian
56  ID_MachO32B, // MachO 32-bit, big endian
57  ID_MachO64L, // MachO 64-bit, little endian
58  ID_MachO64B, // MachO 64-bit, big endian
59 
60  ID_WinRes, // Windows resource (.res) file.
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 {
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 {
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 ||
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 
148 /// @brief Create a Binary from Source, autodetecting the file type.
149 ///
150 /// @param Source The data to create the Binary from.
152  LLVMContext *Context = nullptr);
153 
154 template <typename T> class OwningBinary {
155  std::unique_ptr<T> Bin;
156  std::unique_ptr<MemoryBuffer> Buf;
157 
158 public:
159  OwningBinary();
160  OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
161  OwningBinary(OwningBinary<T>&& Other);
162  OwningBinary<T> &operator=(OwningBinary<T> &&Other);
163 
164  std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
165 
166  T* getBinary();
167  const T* getBinary() const;
168 };
169 
170 template <typename T>
171 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
172  std::unique_ptr<MemoryBuffer> Buf)
173  : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
174 
175 template <typename T> OwningBinary<T>::OwningBinary() = default;
176 
177 template <typename T>
179  : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
180 
181 template <typename T>
183  Bin = std::move(Other.Bin);
184  Buf = std::move(Other.Buf);
185  return *this;
186 }
187 
188 template <typename T>
189 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
191  return std::make_pair(std::move(Bin), std::move(Buf));
192 }
193 
194 template <typename T> T* OwningBinary<T>::getBinary() {
195  return Bin.get();
196 }
197 
198 template <typename T> const T* OwningBinary<T>::getBinary() const {
199  return Bin.get();
200 }
201 
203 
204 } // end namespace object
205 
206 } // end namespace llvm
207 
208 #endif // LLVM_OBJECT_BINARY_H
bool isELF() const
Definition: Binary.h:108
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:74
LLVMContext & Context
Type::TypeID TypeID
Expected< std::unique_ptr< Binary > > createBinary(MemoryBufferRef Source, LLVMContext *Context=nullptr)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
StringRef getData() const
Definition: Binary.cpp:39
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
bool isIR() const
Definition: Binary.h:126
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:663
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
bool isWinRes() const
Definition: Binary.h:135
bool isCOFFImportFile() const
Definition: Binary.h:122
bool isCOFF() const
Definition: Binary.h:116
unsigned int getType() const
Definition: Binary.h:91
bool isObject() const
Definition: Binary.h:94
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:67
StringRef getFileName() const
Definition: Binary.cpp:41
std::pair< std::unique_ptr< T >, std::unique_ptr< MemoryBuffer > > takeBinary()
Definition: Binary.h:190
bool isMachO() const
Definition: Binary.h:112
Triple::ObjectFormatType getTripleObjectFormat() const
Definition: Binary.h:137
MemoryBufferRef Data
Definition: Binary.h:36
OwningBinary< T > & operator=(OwningBinary< T > &&Other)
Definition: Binary.h:182
bool isLittleEndian() const
Definition: Binary.h:130
ObjectFormatType
Definition: Triple.h:207
bool isWasm() const
Definition: Binary.h:120
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
bool isMachOUniversalBinary() const
Definition: Binary.h:104
bool isSymbolic() const
Definition: Binary.h:98
bool isArchive() const
Definition: Binary.h:100