1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the Binary class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_BINARY_H
15 #define LLVM_OBJECT_BINARY_H
17 #include "llvm/Object/Error.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/FileSystem.h"
20 #include "llvm/Support/MemoryBuffer.h"
32 Binary(const Binary &other) = delete;
39 Binary(unsigned int Type, MemoryBufferRef Source);
43 ID_MachOUniversalBinary,
46 ID_FunctionIndex, // Function summary index
48 // Object and children.
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
65 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
67 return is64Bits ? ID_ELF64L : ID_ELF32L;
69 return is64Bits ? ID_ELF64B : ID_ELF32B;
72 static unsigned int getMachOType(bool isLE, bool is64Bits) {
74 return is64Bits ? ID_MachO64L : ID_MachO32L;
76 return is64Bits ? ID_MachO64B : ID_MachO32B;
82 StringRef getData() const;
83 StringRef getFileName() const;
84 MemoryBufferRef getMemoryBufferRef() const;
87 unsigned int getType() const { return TypeID; }
89 // Convenience methods
90 bool isObject() const {
91 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
94 bool isSymbolic() const {
95 return isIR() || isObject();
98 bool isArchive() const {
99 return TypeID == ID_Archive;
102 bool isMachOUniversalBinary() const {
103 return TypeID == ID_MachOUniversalBinary;
107 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
110 bool isMachO() const {
111 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
114 bool isCOFF() const {
115 return TypeID == ID_COFF;
118 bool isCOFFImportFile() const {
119 return TypeID == ID_COFFImportFile;
123 return TypeID == ID_IR;
126 bool isFunctionIndex() const {
127 return TypeID == ID_FunctionIndex;
130 bool isLittleEndian() const {
131 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
132 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
136 /// @brief Create a Binary from Source, autodetecting the file type.
138 /// @param Source The data to create the Binary from.
139 ErrorOr<std::unique_ptr<Binary>> createBinary(MemoryBufferRef Source,
140 LLVMContext *Context = nullptr);
142 template <typename T> class OwningBinary {
143 std::unique_ptr<T> Bin;
144 std::unique_ptr<MemoryBuffer> Buf;
148 OwningBinary(std::unique_ptr<T> Bin, std::unique_ptr<MemoryBuffer> Buf);
149 OwningBinary(OwningBinary<T>&& Other);
150 OwningBinary<T> &operator=(OwningBinary<T> &&Other);
152 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>> takeBinary();
155 const T* getBinary() const;
158 template <typename T>
159 OwningBinary<T>::OwningBinary(std::unique_ptr<T> Bin,
160 std::unique_ptr<MemoryBuffer> Buf)
161 : Bin(std::move(Bin)), Buf(std::move(Buf)) {}
163 template <typename T> OwningBinary<T>::OwningBinary() {}
165 template <typename T>
166 OwningBinary<T>::OwningBinary(OwningBinary &&Other)
167 : Bin(std::move(Other.Bin)), Buf(std::move(Other.Buf)) {}
169 template <typename T>
170 OwningBinary<T> &OwningBinary<T>::operator=(OwningBinary &&Other) {
171 Bin = std::move(Other.Bin);
172 Buf = std::move(Other.Buf);
176 template <typename T>
177 std::pair<std::unique_ptr<T>, std::unique_ptr<MemoryBuffer>>
178 OwningBinary<T>::takeBinary() {
179 return std::make_pair(std::move(Bin), std::move(Buf));
182 template <typename T> T* OwningBinary<T>::getBinary() {
186 template <typename T> const T* OwningBinary<T>::getBinary() const {
190 ErrorOr<OwningBinary<Binary>> createBinary(StringRef Path);