Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- Binary.h - A generic binary file -------------------------*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame^] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 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/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 | |
| 24 | namespace llvm { |
| 25 | |
| 26 | class LLVMContext; |
| 27 | class StringRef; |
| 28 | |
| 29 | namespace object { |
| 30 | |
| 31 | class Binary { |
| 32 | private: |
| 33 | unsigned int TypeID; |
| 34 | |
| 35 | protected: |
| 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 | |
| 81 | public: |
| 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 Scull | cdfcccc | 2018-10-05 20:58:37 +0100 | [diff] [blame] | 158 | /// Create a Binary from Source, autodetecting the file type. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 159 | /// |
| 160 | /// @param Source The data to create the Binary from. |
| 161 | Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source, |
| 162 | LLVMContext *Context = nullptr); |
| 163 | |
| 164 | template <typename T> class OwningBinary { |
| 165 | std::unique_ptr<T> Bin; |
| 166 | std::unique_ptr<MemoryBuffer> Buf; |
| 167 | |
| 168 | public: |
| 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 | |
| 180 | template <typename T> |
| 181 | OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin, |
| 182 | std::unique_ptr<MemoryBuffer> Buf) |
| 183 | : Bin(std::move(Bin)), Buf(std::move(Buf)) {} |
| 184 | |
| 185 | template <typename T> OwningBinary<T>::OwningBinary() = default; |
| 186 | |
| 187 | template <typename T> |
| 188 | OwningBinary<T>::OwningBinary(OwningBinary &&Other) |
| 189 | : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {} |
| 190 | |
| 191 | template <typename T> |
| 192 | OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) { |
| 193 | Bin = std::move(Other.Bin); |
| 194 | Buf = std::move(Other.Buf); |
| 195 | return *this; |
| 196 | } |
| 197 | |
| 198 | template <typename T> |
| 199 | std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> |
| 200 | OwningBinary<T>::takeBinary() { |
| 201 | return std::make_pair(std::move(Bin), std::move(Buf)); |
| 202 | } |
| 203 | |
| 204 | template <typename T> T* OwningBinary<T>::getBinary() { |
| 205 | return Bin.get(); |
| 206 | } |
| 207 | |
| 208 | template <typename T> const T* OwningBinary<T>::getBinary() const { |
| 209 | return Bin.get(); |
| 210 | } |
| 211 | |
| 212 | Expected<OwningBinary<Binary>> createBinary(StringRef Path); |
| 213 | |
| 214 | } // end namespace object |
| 215 | |
| 216 | } // end namespace llvm |
| 217 | |
| 218 | #endif // LLVM_OBJECT_BINARY_H |