blob: 3c3e977baff479087b338218a4dcfdf860454577 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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
Andrew Walbran3d2c1972020-04-07 12:24:26 +010016#include "llvm-c/Types.h"
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010017#include "llvm/ADT/Triple.h"
18#include "llvm/Object/Error.h"
19#include "llvm/Support/Error.h"
20#include "llvm/Support/MemoryBuffer.h"
21#include <algorithm>
22#include <memory>
23#include <utility>
24
25namespace llvm {
26
27class LLVMContext;
28class StringRef;
29
30namespace object {
31
32class Binary {
33private:
34 unsigned int TypeID;
35
36protected:
37 MemoryBufferRef Data;
38
39 Binary(unsigned int Type, MemoryBufferRef Source);
40
41 enum {
42 ID_Archive,
43 ID_MachOUniversalBinary,
44 ID_COFFImportFile,
Andrew Walbran3d2c1972020-04-07 12:24:26 +010045 ID_IR, // LLVM IR
46
47 ID_Minidump,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010048
49 ID_WinRes, // Windows resource (.res) file.
50
51 // Object and children.
52 ID_StartObjects,
53 ID_COFF,
54
Andrew Walbran3d2c1972020-04-07 12:24:26 +010055 ID_XCOFF32, // AIX XCOFF 32-bit
56 ID_XCOFF64, // AIX XCOFF 64-bit
57
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010058 ID_ELF32L, // ELF 32-bit, little endian
59 ID_ELF32B, // ELF 32-bit, big endian
60 ID_ELF64L, // ELF 64-bit, little endian
61 ID_ELF64B, // ELF 64-bit, big endian
62
63 ID_MachO32L, // MachO 32-bit, little endian
64 ID_MachO32B, // MachO 32-bit, big endian
65 ID_MachO64L, // MachO 64-bit, little endian
66 ID_MachO64B, // MachO 64-bit, big endian
67
68 ID_Wasm,
69
70 ID_EndObjects
71 };
72
73 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
74 if (isLE)
75 return is64Bits ? ID_ELF64L : ID_ELF32L;
76 else
77 return is64Bits ? ID_ELF64B : ID_ELF32B;
78 }
79
80 static unsigned int getMachOType(bool isLE, bool is64Bits) {
81 if (isLE)
82 return is64Bits ? ID_MachO64L : ID_MachO32L;
83 else
84 return is64Bits ? ID_MachO64B : ID_MachO32B;
85 }
86
87public:
88 Binary() = delete;
89 Binary(const Binary &other) = delete;
90 virtual ~Binary();
91
92 StringRef getData() const;
93 StringRef getFileName() const;
94 MemoryBufferRef getMemoryBufferRef() const;
95
96 // Cast methods.
97 unsigned int getType() const { return TypeID; }
98
99 // Convenience methods
100 bool isObject() const {
101 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
102 }
103
104 bool isSymbolic() const { return isIR() || isObject() || isCOFFImportFile(); }
105
106 bool isArchive() const {
107 return TypeID == ID_Archive;
108 }
109
110 bool isMachOUniversalBinary() const {
111 return TypeID == ID_MachOUniversalBinary;
112 }
113
114 bool isELF() const {
115 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
116 }
117
118 bool isMachO() const {
119 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
120 }
121
122 bool isCOFF() const {
123 return TypeID == ID_COFF;
124 }
125
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100126 bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
127
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100128 bool isWasm() const { return TypeID == ID_Wasm; }
129
130 bool isCOFFImportFile() const {
131 return TypeID == ID_COFFImportFile;
132 }
133
134 bool isIR() const {
135 return TypeID == ID_IR;
136 }
137
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100138 bool isMinidump() const { return TypeID == ID_Minidump; }
139
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100140 bool isLittleEndian() const {
141 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
142 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
143 }
144
145 bool isWinRes() const { return TypeID == ID_WinRes; }
146
147 Triple::ObjectFormatType getTripleObjectFormat() const {
148 if (isCOFF())
149 return Triple::COFF;
150 if (isMachO())
151 return Triple::MachO;
152 if (isELF())
153 return Triple::ELF;
154 return Triple::UnknownObjectFormat;
155 }
156
157 static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr,
158 const uint64_t Size) {
159 if (Addr + Size < Addr || Addr + Size < Size ||
160 Addr + Size > uintptr_t(M.getBufferEnd()) ||
161 Addr < uintptr_t(M.getBufferStart())) {
162 return object_error::unexpected_eof;
163 }
164 return std::error_code();
165 }
166};
167
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100168// Create wrappers for C Binding types (see CBindingWrapping.h).
169DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
170
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100171/// Create a Binary from Source, autodetecting the file type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100172///
173/// @param Source The data to create the Binary from.
174Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
175 LLVMContext *Context = nullptr);
176
177template <typename T> class OwningBinary {
178 std::unique_ptr<T> Bin;
179 std::unique_ptr<MemoryBuffer> Buf;
180
181public:
182 OwningBinary();
183 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
184 OwningBinary(OwningBinary<T>&& Other);
185 OwningBinary<T> &operator=(OwningBinary<T> &&Other);
186
187 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
188
189 T* getBinary();
190 const T* getBinary() const;
191};
192
193template <typename T>
194OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
195 std::unique_ptr<MemoryBuffer> Buf)
196 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
197
198template <typename T> OwningBinary<T>::OwningBinary() = default;
199
200template <typename T>
201OwningBinary<T>::OwningBinary(OwningBinary &&Other)
202 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
203
204template <typename T>
205OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
206 Bin = std::move(Other.Bin);
207 Buf = std::move(Other.Buf);
208 return *this;
209}
210
211template <typename T>
212std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
213OwningBinary<T>::takeBinary() {
214 return std::make_pair(std::move(Bin), std::move(Buf));
215}
216
217template <typename T> T* OwningBinary<T>::getBinary() {
218 return Bin.get();
219}
220
221template <typename T> const T* OwningBinary<T>::getBinary() const {
222 return Bin.get();
223}
224
225Expected<OwningBinary<Binary>> createBinary(StringRef Path);
226
227} // end namespace object
228
229} // end namespace llvm
230
231#endif // LLVM_OBJECT_BINARY_H