| 1 | //===- Binary.h - A generic binary file -------------------------*- C++ -*-===// |
| 2 | // |
| 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 |
| 6 | // |
| 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-c/Types.h" |
| 17 | #include "llvm/Object/Error.h" |
| 18 | #include "llvm/Support/CBindingWrapping.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include "llvm/Support/Error.h" |
| 21 | #include "llvm/Support/MemoryBuffer.h" |
| 22 | #include "llvm/TargetParser/Triple.h" |
| 23 | #include <memory> |
| 24 | #include <utility> |
| 25 | |
| 26 | namespace llvm { |
| 27 | |
| 28 | class LLVMContext; |
| 29 | class StringRef; |
| 30 | |
| 31 | namespace object { |
| 32 | |
| 33 | class LLVM_ABI Binary { |
| 34 | private: |
| 35 | unsigned int TypeID; |
| 36 | |
| 37 | protected: |
| 38 | MemoryBufferRef Data; |
| 39 | |
| 40 | Binary(unsigned int Type, MemoryBufferRef Source); |
| 41 | |
| 42 | enum { |
| 43 | ID_Archive, |
| 44 | ID_MachOUniversalBinary, |
| 45 | ID_COFFImportFile, |
| 46 | ID_IR, // LLVM IR |
| 47 | ID_TapiUniversal, // Text-based Dynamic Library Stub file. |
| 48 | ID_TapiFile, // Text-based Dynamic Library Stub file. |
| 49 | |
| 50 | ID_Minidump, |
| 51 | |
| 52 | ID_WinRes, // Windows resource (.res) file. |
| 53 | |
| 54 | ID_Offload, // Offloading binary file. |
| 55 | |
| 56 | // Object and children. |
| 57 | ID_StartObjects, |
| 58 | ID_COFF, |
| 59 | |
| 60 | ID_XCOFF32, // AIX XCOFF 32-bit |
| 61 | ID_XCOFF64, // AIX XCOFF 64-bit |
| 62 | |
| 63 | ID_ELF32L, // ELF 32-bit, little endian |
| 64 | ID_ELF32B, // ELF 32-bit, big endian |
| 65 | ID_ELF64L, // ELF 64-bit, little endian |
| 66 | ID_ELF64B, // ELF 64-bit, big endian |
| 67 | |
| 68 | ID_MachO32L, // MachO 32-bit, little endian |
| 69 | ID_MachO32B, // MachO 32-bit, big endian |
| 70 | ID_MachO64L, // MachO 64-bit, little endian |
| 71 | ID_MachO64B, // MachO 64-bit, big endian |
| 72 | |
| 73 | ID_GOFF, |
| 74 | ID_Wasm, |
| 75 | ID_DXContainer, |
| 76 | |
| 77 | ID_EndObjects |
| 78 | }; |
| 79 | |
| 80 | static inline unsigned int getELFType(bool isLE, bool is64Bits) { |
| 81 | if (isLE) |
| 82 | return is64Bits ? ID_ELF64L : ID_ELF32L; |
| 83 | else |
| 84 | return is64Bits ? ID_ELF64B : ID_ELF32B; |
| 85 | } |
| 86 | |
| 87 | static unsigned int getMachOType(bool isLE, bool is64Bits) { |
| 88 | if (isLE) |
| 89 | return is64Bits ? ID_MachO64L : ID_MachO32L; |
| 90 | else |
| 91 | return is64Bits ? ID_MachO64B : ID_MachO32B; |
| 92 | } |
| 93 | |
| 94 | public: |
| 95 | Binary() = delete; |
| 96 | Binary(const Binary &other) = delete; |
| 97 | virtual ~Binary(); |
| 98 | |
| 99 | virtual Error initContent() { return Error::success(); }; |
| 100 | |
| 101 | StringRef getData() const; |
| 102 | StringRef getFileName() const; |
| 103 | MemoryBufferRef getMemoryBufferRef() const; |
| 104 | |
| 105 | // Cast methods. |
| 106 | unsigned int getType() const { return TypeID; } |
| 107 | |
| 108 | // Convenience methods |
| 109 | bool isObject() const { |
| 110 | return TypeID > ID_StartObjects && TypeID < ID_EndObjects; |
| 111 | } |
| 112 | |
| 113 | bool isSymbolic() const { |
| 114 | return isIR() || isObject() || isCOFFImportFile() || isTapiFile(); |
| 115 | } |
| 116 | |
| 117 | bool isArchive() const { return TypeID == ID_Archive; } |
| 118 | |
| 119 | bool isMachOUniversalBinary() const { |
| 120 | return TypeID == ID_MachOUniversalBinary; |
| 121 | } |
| 122 | |
| 123 | bool isTapiUniversal() const { return TypeID == ID_TapiUniversal; } |
| 124 | |
| 125 | bool isELF() const { |
| 126 | return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B; |
| 127 | } |
| 128 | |
| 129 | bool isMachO() const { |
| 130 | return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B; |
| 131 | } |
| 132 | |
| 133 | bool isCOFF() const { |
| 134 | return TypeID == ID_COFF; |
| 135 | } |
| 136 | |
| 137 | bool isXCOFF() const { return TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64; } |
| 138 | |
| 139 | bool isWasm() const { return TypeID == ID_Wasm; } |
| 140 | |
| 141 | bool isOffloadFile() const { return TypeID == ID_Offload; } |
| 142 | |
| 143 | bool isCOFFImportFile() const { |
| 144 | return TypeID == ID_COFFImportFile; |
| 145 | } |
| 146 | |
| 147 | bool isIR() const { |
| 148 | return TypeID == ID_IR; |
| 149 | } |
| 150 | |
| 151 | bool isGOFF() const { return TypeID == ID_GOFF; } |
| 152 | |
| 153 | bool isMinidump() const { return TypeID == ID_Minidump; } |
| 154 | |
| 155 | bool isTapiFile() const { return TypeID == ID_TapiFile; } |
| 156 | |
| 157 | bool isLittleEndian() const { |
| 158 | return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B || |
| 159 | TypeID == ID_MachO32B || TypeID == ID_MachO64B || |
| 160 | TypeID == ID_XCOFF32 || TypeID == ID_XCOFF64); |
| 161 | } |
| 162 | |
| 163 | bool isWinRes() const { return TypeID == ID_WinRes; } |
| 164 | |
| 165 | bool isDXContainer() const { return TypeID == ID_DXContainer; } |
| 166 | |
| 167 | Triple::ObjectFormatType getTripleObjectFormat() const { |
| 168 | if (isCOFF()) |
| 169 | return Triple::COFF; |
| 170 | if (isMachO()) |
| 171 | return Triple::MachO; |
| 172 | if (isELF()) |
| 173 | return Triple::ELF; |
| 174 | if (isGOFF()) |
| 175 | return Triple::GOFF; |
| 176 | return Triple::UnknownObjectFormat; |
| 177 | } |
| 178 | |
| 179 | static Error checkOffset(MemoryBufferRef M, uintptr_t Addr, |
| 180 | const uint64_t Size) { |
| 181 | if (Addr + Size < Addr || Addr + Size < Size || |
| 182 | Addr + Size > reinterpret_cast<uintptr_t>(M.getBufferEnd()) || |
| 183 | Addr < reinterpret_cast<uintptr_t>(M.getBufferStart())) { |
| 184 | return errorCodeToError(EC: object_error::unexpected_eof); |
| 185 | } |
| 186 | return Error::success(); |
| 187 | } |
| 188 | }; |
| 189 | |
| 190 | // Create wrappers for C Binding types (see CBindingWrapping.h). |
| 191 | DEFINE_ISA_CONVERSION_FUNCTIONS(Binary, LLVMBinaryRef) |
| 192 | |
| 193 | /// Create a Binary from Source, autodetecting the file type. |
| 194 | /// |
| 195 | /// @param Source The data to create the Binary from. |
| 196 | LLVM_ABI Expected<std::unique_ptr<Binary>> |
| 197 | createBinary(MemoryBufferRef Source, LLVMContext *Context = nullptr, |
| 198 | bool InitContent = true); |
| 199 | |
| 200 | template <typename T> class OwningBinary { |
| 201 | std::unique_ptr<T> Bin; |
| 202 | std::unique_ptr<MemoryBuffer> Buf; |
| 203 | |
| 204 | public: |
| 205 | OwningBinary(); |
| 206 | OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf); |
| 207 | OwningBinary(OwningBinary<T>&& Other); |
| 208 | OwningBinary<T> &operator=(OwningBinary<T> &&Other); |
| 209 | |
| 210 | std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary(); |
| 211 | |
| 212 | T* getBinary(); |
| 213 | const T* getBinary() const; |
| 214 | }; |
| 215 | |
| 216 | template <typename T> |
| 217 | OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin, |
| 218 | std::unique_ptr<MemoryBuffer> Buf) |
| 219 | : Bin(std::move(Bin)), Buf(std::move(Buf)) {} |
| 220 | |
| 221 | template <typename T> OwningBinary<T>::OwningBinary() = default; |
| 222 | |
| 223 | template <typename T> |
| 224 | OwningBinary<T>::OwningBinary(OwningBinary &&Other) |
| 225 | : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {} |
| 226 | |
| 227 | template <typename T> |
| 228 | OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) { |
| 229 | Bin = std::move(Other.Bin); |
| 230 | Buf = std::move(Other.Buf); |
| 231 | return *this; |
| 232 | } |
| 233 | |
| 234 | template <typename T> |
| 235 | std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> |
| 236 | OwningBinary<T>::takeBinary() { |
| 237 | return std::make_pair(std::move(Bin), std::move(Buf)); |
| 238 | } |
| 239 | |
| 240 | template <typename T> T* OwningBinary<T>::getBinary() { |
| 241 | return Bin.get(); |
| 242 | } |
| 243 | |
| 244 | template <typename T> const T* OwningBinary<T>::getBinary() const { |
| 245 | return Bin.get(); |
| 246 | } |
| 247 | |
| 248 | LLVM_ABI Expected<OwningBinary<Binary>> |
| 249 | createBinary(StringRef Path, LLVMContext *Context = nullptr, |
| 250 | bool InitContent = true); |
| 251 | |
| 252 | } // end namespace object |
| 253 | |
| 254 | } // end namespace llvm |
| 255 | |
| 256 | #endif // LLVM_OBJECT_BINARY_H |
| 257 | |