Recommit [NFC] Better encapsulation of llvm::Optional Storage
[llvm-complete.git] / include / llvm / Object / Binary.h
blob8ce2d5aa402bcd3f5f1ec52fbc61578154d5d2bc
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.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_OBJECT_BINARY_H
14 #define LLVM_OBJECT_BINARY_H
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>
24 namespace llvm {
26 class LLVMContext;
27 class StringRef;
29 namespace object {
31 class Binary {
32 private:
33 unsigned int TypeID;
35 protected:
36 MemoryBufferRef Data;
38 Binary(unsigned int Type, MemoryBufferRef Source);
40 enum {
41 ID_Archive,
42 ID_MachOUniversalBinary,
43 ID_COFFImportFile,
44 ID_IR, // LLVM IR
46 ID_WinRes, // Windows resource (.res) file.
48 // Object and children.
49 ID_StartObjects,
50 ID_COFF,
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
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
62 ID_Wasm,
64 ID_EndObjects
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;
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;
81 public:
82 Binary() = delete;
83 Binary(const Binary &other) = delete;
84 virtual ~Binary();
86 StringRef getData() const;
87 StringRef getFileName() const;
88 MemoryBufferRef getMemoryBufferRef() const;
90 // Cast methods.
91 unsigned int getType() const { return TypeID; }
93 // Convenience methods
94 bool isObject() const {
95 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
98 bool isSymbolic() const { return isIR() || isObject() || isCOFFImportFile(); }
100 bool isArchive() const {
101 return TypeID == ID_Archive;
104 bool isMachOUniversalBinary() const {
105 return TypeID == ID_MachOUniversalBinary;
108 bool isELF() const {
109 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
112 bool isMachO() const {
113 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
116 bool isCOFF() const {
117 return TypeID == ID_COFF;
120 bool isWasm() const { return TypeID == ID_Wasm; }
122 bool isCOFFImportFile() const {
123 return TypeID == ID_COFFImportFile;
126 bool isIR() const {
127 return TypeID == ID_IR;
130 bool isLittleEndian() const {
131 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
132 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
135 bool isWinRes() const { return TypeID == ID_WinRes; }
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;
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;
154 return std::error_code();
158 /// Create a Binary from Source, autodetecting the file type.
160 /// @param Source The data to create the Binary from.
161 Expected<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
162 LLVMContext *Context = nullptr);
164 template <typename T> class OwningBinary {
165 std::unique_ptr<T> Bin;
166 std::unique_ptr<MemoryBuffer> Buf;
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);
174 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
176 T* getBinary();
177 const T* getBinary() const;
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)) {}
185 template <typename T> OwningBinary<T>::OwningBinary() = default;
187 template <typename T>
188 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
189 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
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;
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));
204 template <typename T> T* OwningBinary<T>::getBinary() {
205 return Bin.get();
208 template <typename T> const T* OwningBinary<T>::getBinary() const {
209 return Bin.get();
212 Expected<OwningBinary<Binary>> createBinary(StringRef Path);
214 } // end namespace object
216 } // end namespace llvm
218 #endif // LLVM_OBJECT_BINARY_H