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/ADT/OwningPtr.h"
18 #include "llvm/Object/Error.h"
29 Binary() LLVM_DELETED_FUNCTION;
30 Binary(const Binary &other) LLVM_DELETED_FUNCTION;
37 Binary(unsigned int Type, MemoryBuffer *Source);
41 ID_MachOUniversalBinary,
42 // Object and children.
46 ID_ELF32L, // ELF 32-bit, little endian
47 ID_ELF32B, // ELF 32-bit, big endian
48 ID_ELF64L, // ELF 64-bit, little endian
49 ID_ELF64B, // ELF 64-bit, big endian
51 ID_MachO32L, // MachO 32-bit, little endian
52 ID_MachO32B, // MachO 32-bit, big endian
53 ID_MachO64L, // MachO 64-bit, little endian
54 ID_MachO64B, // MachO 64-bit, big endian
59 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
61 return is64Bits ? ID_ELF64L : ID_ELF32L;
63 return is64Bits ? ID_ELF64B : ID_ELF32B;
66 static unsigned int getMachOType(bool isLE, bool is64Bits) {
68 return is64Bits ? ID_MachO64L : ID_MachO32L;
70 return is64Bits ? ID_MachO64B : ID_MachO32B;
76 StringRef getData() const;
77 StringRef getFileName() const;
80 unsigned int getType() const { return TypeID; }
82 // Convenience methods
83 bool isObject() const {
84 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
87 bool isArchive() const {
88 return TypeID == ID_Archive;
91 bool isMachOUniversalBinary() const {
92 return TypeID == ID_MachOUniversalBinary;
96 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
99 bool isMachO() const {
100 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
103 bool isCOFF() const {
104 return TypeID == ID_COFF;
107 bool isLittleEndian() const {
108 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
109 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
113 /// @brief Create a Binary from Source, autodetecting the file type.
115 /// @param Source The data to create the Binary from. Ownership is transferred
116 /// to Result if successful. If an error is returned, Source is destroyed
117 /// by createBinary before returning.
118 /// @param Result A pointer to the resulting Binary if no error occured.
119 error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
121 error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);