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