]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Object/Binary.h
Merge compiler-rt trunk r300890, and update build glue.
[FreeBSD/FreeBSD.git] / contrib / llvm / include / llvm / Object / Binary.h
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"
19 #include "llvm/Support/MemoryBuffer.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:
36   MemoryBufferRef Data;
37
38   Binary(unsigned int Type, MemoryBufferRef Source);
39
40   enum {
41     ID_Archive,
42     ID_MachOUniversalBinary,
43     ID_COFFImportFile,
44     ID_IR,                 // LLVM IR
45     ID_ModuleSummaryIndex, // Module summary index
46
47     // Object and children.
48     ID_StartObjects,
49     ID_COFF,
50
51     ID_ELF32L, // ELF 32-bit, little endian
52     ID_ELF32B, // ELF 32-bit, big endian
53     ID_ELF64L, // ELF 64-bit, little endian
54     ID_ELF64B, // ELF 64-bit, big endian
55
56     ID_MachO32L, // MachO 32-bit, little endian
57     ID_MachO32B, // MachO 32-bit, big endian
58     ID_MachO64L, // MachO 64-bit, little endian
59     ID_MachO64B, // MachO 64-bit, big endian
60
61     ID_Wasm,
62
63     ID_EndObjects
64   };
65
66   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
67     if (isLE)
68       return is64Bits ? ID_ELF64L : ID_ELF32L;
69     else
70       return is64Bits ? ID_ELF64B : ID_ELF32B;
71   }
72
73   static unsigned int getMachOType(bool isLE, bool is64Bits) {
74     if (isLE)
75       return is64Bits ? ID_MachO64L : ID_MachO32L;
76     else
77       return is64Bits ? ID_MachO64B : ID_MachO32B;
78   }
79
80 public:
81   Binary() = delete;
82   Binary(const Binary &other) = delete;
83   virtual ~Binary();
84
85   StringRef getData() const;
86   StringRef getFileName() const;
87   MemoryBufferRef getMemoryBufferRef() const;
88
89   // Cast methods.
90   unsigned int getType() const { return TypeID; }
91
92   // Convenience methods
93   bool isObject() const {
94     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
95   }
96
97   bool isSymbolic() const {
98     return isIR() || isObject();
99   }
100
101   bool isArchive() const {
102     return TypeID == ID_Archive;
103   }
104
105   bool isMachOUniversalBinary() const {
106     return TypeID == ID_MachOUniversalBinary;
107   }
108
109   bool isELF() const {
110     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
111   }
112
113   bool isMachO() const {
114     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
115   }
116
117   bool isCOFF() const {
118     return TypeID == ID_COFF;
119   }
120
121   bool isWasm() const { return TypeID == ID_Wasm; }
122
123   bool isCOFFImportFile() const {
124     return TypeID == ID_COFFImportFile;
125   }
126
127   bool isIR() const {
128     return TypeID == ID_IR;
129   }
130
131   bool isModuleSummaryIndex() const { return TypeID == ID_ModuleSummaryIndex; }
132
133   bool isLittleEndian() const {
134     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
135              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
136   }
137
138   Triple::ObjectFormatType getTripleObjectFormat() const {
139     if (isCOFF())
140       return Triple::COFF;
141     if (isMachO())
142       return Triple::MachO;
143     if (isELF())
144       return Triple::ELF;
145     return Triple::UnknownObjectFormat;
146   }
147 };
148
149 /// @brief Create a Binary from Source, autodetecting the file type.
150 ///
151 /// @param Source The data to create the Binary from.
152 Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
153                                                LLVMContext *Context = nullptr);
154
155 template <typename T> class OwningBinary {
156   std::unique_ptr<T> Bin;
157   std::unique_ptr<MemoryBuffer> Buf;
158
159 public:
160   OwningBinary();
161   OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
162   OwningBinary(OwningBinary<T>&& Other);
163   OwningBinary<T> &operator=(OwningBinary<T> &&Other);
164
165   std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
166
167   T* getBinary();
168   const T* getBinary() const;
169 };
170
171 template <typename T>
172 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
173                               std::unique_ptr<MemoryBuffer> Buf)
174     : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
175
176 template <typename T> OwningBinary<T>::OwningBinary() = default;
177
178 template <typename T>
179 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
180     : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
181
182 template <typename T>
183 OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
184   Bin = std::move(Other.Bin);
185   Buf = std::move(Other.Buf);
186   return *this;
187 }
188
189 template <typename T>
190 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
191 OwningBinary<T>::takeBinary() {
192   return std::make_pair(std::move(Bin), std::move(Buf));
193 }
194
195 template <typename T> T* OwningBinary<T>::getBinary() {
196   return Bin.get();
197 }
198
199 template <typename T> const T* OwningBinary<T>::getBinary() const {
200   return Bin.get();
201 }
202
203 Expected<OwningBinary<Binary>> createBinary(StringRef Path);
204
205 } // end namespace object
206
207 } // end namespace llvm
208
209 #endif // LLVM_OBJECT_BINARY_H