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"
30 Binary() LLVM_DELETED_FUNCTION;
31 Binary(const Binary &other) LLVM_DELETED_FUNCTION;
38 Binary(unsigned int Type, MemoryBuffer *Source);
42 ID_MachOUniversalBinary,
43 // Object and children.
47 ID_ELF32L, // ELF 32-bit, little endian
48 ID_ELF32B, // ELF 32-bit, big endian
49 ID_ELF64L, // ELF 64-bit, little endian
50 ID_ELF64B, // ELF 64-bit, big endian
52 ID_MachO32L, // MachO 32-bit, little endian
53 ID_MachO32B, // MachO 32-bit, big endian
54 ID_MachO64L, // MachO 64-bit, little endian
55 ID_MachO64B, // MachO 64-bit, big endian
60 static inline unsigned int getELFType(bool isLE, bool is64Bits) {
62 return is64Bits ? ID_ELF64L : ID_ELF32L;
64 return is64Bits ? ID_ELF64B : ID_ELF32B;
67 static unsigned int getMachOType(bool isLE, bool is64Bits) {
69 return is64Bits ? ID_MachO64L : ID_MachO32L;
71 return is64Bits ? ID_MachO64B : ID_MachO32B;
77 StringRef getData() const;
78 StringRef getFileName() const;
81 unsigned int getType() const { return TypeID; }
83 // Convenience methods
84 bool isObject() const {
85 return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
88 bool isArchive() const {
89 return TypeID == ID_Archive;
92 bool isMachOUniversalBinary() const {
93 return TypeID == ID_MachOUniversalBinary;
97 return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
100 bool isMachO() const {
101 return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
104 bool isCOFF() const {
105 return TypeID == ID_COFF;
108 bool isLittleEndian() const {
109 return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
110 TypeID == ID_MachO32B || TypeID == ID_MachO64B);
114 /// @brief Create a Binary from Source, autodetecting the file type.
116 /// @param Source The data to create the Binary from. Ownership is transferred
117 /// to the Binary if successful. If an error is returned,
118 /// Source is destroyed by createBinary before returning.
119 ErrorOr<Binary *> createBinary(MemoryBuffer *Source,
120 sys::fs::file_magic Type =
121 sys::fs::file_magic::unknown);
123 ErrorOr<Binary *> createBinary(StringRef Path);