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 // Object and children.
45 ID_ELF32L, // ELF 32-bit, little endian
46 ID_ELF32B, // ELF 32-bit, big endian
47 ID_ELF64L, // ELF 64-bit, little endian
48 ID_ELF64B, // ELF 64-bit, big endian
50 ID_MachO32L, // MachO 32-bit, little endian
51 ID_MachO32B, // MachO 32-bit, big endian
52 ID_MachO64L, // MachO 64-bit, little endian
53 ID_MachO64B, // MachO 64-bit, big endian
58 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
60 return is64Bits ? ID_ELF64L : ID_ELF32L;
62 return is64Bits ? ID_ELF64B : ID_ELF32B;
65 static unsigned int getMachOType(bool isLE, bool is64Bits) {
67 return is64Bits ? ID_MachO64L : ID_MachO32L;
69 return is64Bits ? ID_MachO64B : ID_MachO32B;
75 StringRef getData() const;
76 StringRef getFileName() const;
79 unsigned int getType() const { return TypeID; }
81 // Convenience methods
82 bool isObject() const {
83 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
86 bool isArchive() const {
87 return TypeID == ID_Archive;
91 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
94 bool isMachO() const {
95 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
99 return TypeID == ID_COFF;
102 bool isLittleEndian() const {
103 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
104 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
108 /// @brief Create a Binary from Source, autodetecting the file type.
110 /// @param Source The data to create the Binary from. Ownership is transferred
111 /// to Result if successful. If an error is returned, Source is destroyed
112 /// by createBinary before returning.
113 /// @param Result A pointer to the resulting Binary if no error occured.
114 error_code createBinary(MemoryBuffer *Source, OwningPtr<Binary> &Result);
116 error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);