blob: dd98e1143e2593a52bd0d5572341c2a996ae64b2 [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,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020045 ID_IR, // LLVM IR
46 ID_TapiUniversal, // Text-based Dynamic Library Stub file.
47 ID_TapiFile, // Text-based Dynamic Library Stub file.
Andrew Walbran3d2c1972020-04-07 12:24:26 +010048
49 ID_Minidump,
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010050
51 ID_WinRes, // Windows resource (.res) file.
52
53 // Object and children.
54 ID_StartObjects,
55 ID_COFF,
56
Andrew Walbran3d2c1972020-04-07 12:24:26 +010057 ID_XCOFF32, // AIX XCOFF 32-bit
58 ID_XCOFF64, // AIX XCOFF 64-bit
59
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010060 ID_ELF32L, // ELF 32-bit, little endian
61 ID_ELF32B, // ELF 32-bit, big endian
62 ID_ELF64L, // ELF 64-bit, little endian
63 ID_ELF64B, // ELF 64-bit, big endian
64
65 ID_MachO32L, // MachO 32-bit, little endian
66 ID_MachO32B, // MachO 32-bit, big endian
67 ID_MachO64L, // MachO 64-bit, little endian
68 ID_MachO64B, // MachO 64-bit, big endian
69
70 ID_Wasm,
71
72 ID_EndObjects
73 };
74
75 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
76 if (isLE)
77 return is64Bits ? ID_ELF64L : ID_ELF32L;
78 else
79 return is64Bits ? ID_ELF64B : ID_ELF32B;
80 }
81
82 static unsigned int getMachOType(bool isLE, bool is64Bits) {
83 if (isLE)
84 return is64Bits ? ID_MachO64L : ID_MachO32L;
85 else
86 return is64Bits ? ID_MachO64B : ID_MachO32B;
87 }
88
89public:
90 Binary() = delete;
91 Binary(const Binary &other) = delete;
92 virtual ~Binary();
93
Olivier Deprezf4ef2d02021-04-20 13:36:24 +020094 virtual Error initContent() { return Error::success(); };
95
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 StringRef getData() const;
97 StringRef getFileName() const;
98 MemoryBufferRef getMemoryBufferRef() const;
99
100 // Cast methods.
101 unsigned int getType() const { return TypeID; }
102
103 // Convenience methods
104 bool isObject() const {
105 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
106 }
107
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200108 bool isSymbolic() const {
109 return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100110 }
111
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200112 bool isArchive() const { return TypeID == ID_Archive; }
113
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100114 bool isMachOUniversalBinary() const {
115 return TypeID == ID_MachOUniversalBinary;
116 }
117
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200118 bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
119
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100120 bool isELF() const {
121 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
122 }
123
124 bool isMachO() const {
125 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
126 }
127
128 bool isCOFF() const {
129 return TypeID == ID_COFF;
130 }
131
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100132 bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
133
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100134 bool isWasm() const { return TypeID == ID_Wasm; }
135
136 bool isCOFFImportFile() const {
137 return TypeID == ID_COFFImportFile;
138 }
139
140 bool isIR() const {
141 return TypeID == ID_IR;
142 }
143
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100144 bool isMinidump() const { return TypeID == ID_Minidump; }
145
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200146 bool isTapiFile() const { return TypeID == ID_TapiFile; }
147
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100148 bool isLittleEndian() const {
149 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
150 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
151 }
152
153 bool isWinRes() const { return TypeID == ID_WinRes; }
154
155 Triple::ObjectFormatType getTripleObjectFormat() const {
156 if (isCOFF())
157 return Triple::COFF;
158 if (isMachO())
159 return Triple::MachO;
160 if (isELF())
161 return Triple::ELF;
162 return Triple::UnknownObjectFormat;
163 }
164
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200165 static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
166 const uint64_t Size) {
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100167 if (Addr + Size < Addr || Addr + Size < Size ||
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200168 Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) ||
169 Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) {
170 return errorCodeToError(object_error::unexpected_eof);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100171 }
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200172 return Error::success();
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100173 }
174};
175
Andrew Walbran3d2c1972020-04-07 12:24:26 +0100176// Create wrappers for C Binding types (see CBindingWrapping.h).
177DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
178
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100179/// Create a Binary from Source, autodetecting the file type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100180///
181/// @param Source The data to create the Binary from.
182Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200183 LLVMContext *Context = nullptr,
184 bool InitContent = true);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100185
186template <typename T> class OwningBinary {
187 std::unique_ptr<T> Bin;
188 std::unique_ptr<MemoryBuffer> Buf;
189
190public:
191 OwningBinary();
192 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
193 OwningBinary(OwningBinary<T>&& Other);
194 OwningBinary<T> &operator=(OwningBinary<T> &&Other);
195
196 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
197
198 T* getBinary();
199 const T* getBinary() const;
200};
201
202template <typename T>
203OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
204 std::unique_ptr<MemoryBuffer> Buf)
205 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
206
207template <typename T> OwningBinary<T>::OwningBinary() = default;
208
209template <typename T>
210OwningBinary<T>::OwningBinary(OwningBinary &&Other)
211 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
212
213template <typename T>
214OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
215 Bin = std::move(Other.Bin);
216 Buf = std::move(Other.Buf);
217 return *this;
218}
219
220template <typename T>
221std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
222OwningBinary<T>::takeBinary() {
223 return std::make_pair(std::move(Bin), std::move(Buf));
224}
225
226template <typename T> T* OwningBinary<T>::getBinary() {
227 return Bin.get();
228}
229
230template <typename T> const T* OwningBinary<T>::getBinary() const {
231 return Bin.get();
232}
233
Olivier Deprezf4ef2d02021-04-20 13:36:24 +0200234Expected<OwningBinary<Binary>> createBinary(StringRef Path,
235 LLVMContext *Context = nullptr,
236 bool InitContent = true);
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100237
238} // end namespace object
239
240} // end namespace llvm
241
242#endif // LLVM_OBJECT_BINARY_H