blob: 8ce2d5aa402bcd3f5f1ec52fbc61578154d5d2bc [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
16#include "llvm/ADT/Triple.h"
17#include "llvm/Object/Error.h"
18#include "llvm/Support/Error.h"
19#include "llvm/Support/MemoryBuffer.h"
20#include <algorithm>
21#include <memory>
22#include <utility>
23
24namespace llvm {
25
26class LLVMContext;
27class StringRef;
28
29namespace object {
30
31class Binary {
32private:
33 unsigned int TypeID;
34
35protected:
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
46 ID_WinRes, // Windows resource (.res) file.
47
48 // Object and children.
49 ID_StartObjects,
50 ID_COFF,
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
62 ID_Wasm,
63
64 ID_EndObjects
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
81public:
82 Binary() = delete;
83 Binary(const Binary &other) = delete;
84 virtual ~Binary();
85
86 StringRef getData() const;
87 StringRef getFileName() const;
88 MemoryBufferRef getMemoryBufferRef() 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
137 Triple::ObjectFormatType getTripleObjectFormat() const {
138 if (isCOFF())
139 return Triple::COFF;
140 if (isMachO())
141 return Triple::MachO;
142 if (isELF())
143 return Triple::ELF;
144 return Triple::UnknownObjectFormat;
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())) {
152 return object_error::unexpected_eof;
153 }
154 return std::error_code();
155 }
156};
157
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100158/// Create a Binary from Source, autodetecting the file type.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100159///
160/// @param Source The data to create the Binary from.
161Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
162 LLVMContext *Context = nullptr);
163
164template <typename T> class OwningBinary {
165 std::unique_ptr<T> Bin;
166 std::unique_ptr<MemoryBuffer> Buf;
167
168public:
169 OwningBinary();
170 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
171 OwningBinary(OwningBinary<T>&& Other);
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
180template <typename T>
181OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
182 std::unique_ptr<MemoryBuffer> Buf)
183 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
184
185template <typename T> OwningBinary<T>::OwningBinary() = default;
186
187template <typename T>
188OwningBinary<T>::OwningBinary(OwningBinary &&Other)
189 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
190
191template <typename T>
192OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
193 Bin = std::move(Other.Bin);
194 Buf = std::move(Other.Buf);
195 return *this;
196}
197
198template <typename T>
199std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
200OwningBinary<T>::takeBinary() {
201 return std::make_pair(std::move(Bin), std::move(Buf));
202}
203
204template <typename T> T* OwningBinary<T>::getBinary() {
205 return Bin.get();
206}
207
208template <typename T> const T* OwningBinary<T>::getBinary() const {
209 return Bin.get();
210}
211
212Expected<OwningBinary<Binary>> createBinary(StringRef Path);
213
214} // end namespace object
215
216} // end namespace llvm
217
218#endif // LLVM_OBJECT_BINARY_H