1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 // This file declares the Binary class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_OBJECT_BINARY_H
14 #define LLVM_OBJECT_BINARY_H
16 #include "llvm-c/Types.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/Object/Error.h"
19 #include "llvm/Support/Error.h"
20 #include "llvm/Support/MemoryBuffer.h"
39 Binary(unsigned int Type, MemoryBufferRef Source);
43 ID_MachOUniversalBinary,
46 ID_TapiUniversal, // Text-based Dynamic Library Stub file.
47 ID_TapiFile, // Text-based Dynamic Library Stub file.
51 ID_WinRes, // Windows resource (.res) file.
53 // Object and children.
57 ID_XCOFF32, // AIX XCOFF 32-bit
58 ID_XCOFF64, // AIX XCOFF 64-bit
60 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
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
75 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
77 return is64Bits ? ID_ELF64L : ID_ELF32L;
79 return is64Bits ? ID_ELF64B : ID_ELF32B;
82 static unsigned int getMachOType(bool isLE, bool is64Bits) {
84 return is64Bits ? ID_MachO64L : ID_MachO32L;
86 return is64Bits ? ID_MachO64B : ID_MachO32B;
91 Binary(const Binary &other) = delete;
94 StringRef getData() const;
95 StringRef getFileName() const;
96 MemoryBufferRef getMemoryBufferRef() const;
99 unsigned int getType() const { return TypeID; }
101 // Convenience methods
102 bool isObject() const {
103 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
106 bool isSymbolic() const {
107 return isIR() || isObject() || isCOFFImportFile() || isTapiFile();
110 bool isArchive() const { return TypeID == ID_Archive; }
112 bool isMachOUniversalBinary() const {
113 return TypeID == ID_MachOUniversalBinary;
116 bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; }
119 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
122 bool isMachO() const {
123 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
126 bool isCOFF() const {
127 return TypeID == ID_COFF;
130 bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; }
132 bool isWasm() const { return TypeID == ID_Wasm; }
134 bool isCOFFImportFile() const {
135 return TypeID == ID_COFFImportFile;
139 return TypeID == ID_IR;
142 bool isMinidump() const { return TypeID == ID_Minidump; }
144 bool isTapiFile() const { return TypeID == ID_TapiFile; }
146 bool isLittleEndian() const {
147 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
148 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
151 bool isWinRes() const { return TypeID == ID_WinRes; }
153 Triple::ObjectFormatType getTripleObjectFormat() const {
157 return Triple::MachO;
160 return Triple::UnknownObjectFormat;
163 static Error checkOffset(MemoryBufferRef M, uintptr_t Addr,
164 const uint64_t Size) {
165 if (Addr + Size < Addr || Addr + Size < Size ||
166 Addr + Size > uintptr_t(M.getBufferEnd()) ||
167 Addr < uintptr_t(M.getBufferStart())) {
168 return errorCodeToError(object_error::unexpected_eof);
170 return Error::success();
174 // Create wrappers for C Binding types (see CBindingWrapping.h).
175 DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef)
177 /// Create a Binary from Source, autodetecting the file type.
179 /// @param Source The data to create the Binary from.
180 Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
181 LLVMContext *Context = nullptr);
183 template <typename T> class OwningBinary {
184 std::unique_ptr<T> Bin;
185 std::unique_ptr<MemoryBuffer> Buf;
189 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
190 OwningBinary(OwningBinary<T>&& Other);
191 OwningBinary<T> &operator=(OwningBinary<T> &&Other);
193 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
196 const T* getBinary() const;
199 template <typename T>
200 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
201 std::unique_ptr<MemoryBuffer> Buf)
202 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
204 template <typename T> OwningBinary<T>::OwningBinary() = default;
206 template <typename T>
207 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
208 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
210 template <typename T>
211 OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
212 Bin = std::move(Other.Bin);
213 Buf = std::move(Other.Buf);
217 template <typename T>
218 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
219 OwningBinary<T>::takeBinary() {
220 return std::make_pair(std::move(Bin), std::move(Buf));
223 template <typename T> T* OwningBinary<T>::getBinary() {
227 template <typename T> const T* OwningBinary<T>::getBinary() const {
231 Expected<OwningBinary<Binary>> createBinary(StringRef Path);
233 } // end namespace object
235 } // end namespace llvm
237 #endif // LLVM_OBJECT_BINARY_H