1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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"
38 Binary(unsigned int Type
, MemoryBufferRef Source
);
42 ID_MachOUniversalBinary
,
46 ID_WinRes
, // Windows resource (.res) file.
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
67 static inline unsigned int getELFType(bool isLE
, bool is64Bits
) {
69 return is64Bits
? ID_ELF64L
: ID_ELF32L
;
71 return is64Bits
? ID_ELF64B
: ID_ELF32B
;
74 static unsigned int getMachOType(bool isLE
, bool is64Bits
) {
76 return is64Bits
? ID_MachO64L
: ID_MachO32L
;
78 return is64Bits
? ID_MachO64B
: ID_MachO32B
;
83 Binary(const Binary
&other
) = delete;
86 StringRef
getData() const;
87 StringRef
getFileName() const;
88 MemoryBufferRef
getMemoryBufferRef() const;
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
;
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
;
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 {
141 return Triple::MachO
;
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
;
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();
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
);
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() {
208 template <typename T
> const T
* OwningBinary
<T
>::getBinary() const {
212 Expected
<OwningBinary
<Binary
>> createBinary(StringRef Path
);
214 } // end namespace object
216 } // end namespace llvm
218 #endif // LLVM_OBJECT_BINARY_H