Pass the computed magic to createBinary and createObjectFile if available.
[oota-llvm.git] / include / llvm / Object / Binary.h
1 //===- Binary.h - A generic binary file -------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the Binary class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_OBJECT_BINARY_H
15 #define LLVM_OBJECT_BINARY_H
16
17 #include "llvm/Object/Error.h"
18 #include "llvm/Support/ErrorOr.h"
19 #include "llvm/Support/FileSystem.h"
20
21 namespace llvm {
22
23 class MemoryBuffer;
24 class StringRef;
25
26 namespace object {
27
28 class Binary {
29 private:
30   Binary() LLVM_DELETED_FUNCTION;
31   Binary(const Binary &other) LLVM_DELETED_FUNCTION;
32
33   unsigned int TypeID;
34
35 protected:
36   MemoryBuffer *Data;
37
38   Binary(unsigned int Type, MemoryBuffer *Source);
39
40   enum {
41     ID_Archive,
42     ID_MachOUniversalBinary,
43     // Object and children.
44     ID_StartObjects,
45     ID_COFF,
46
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
51
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
56
57     ID_EndObjects
58   };
59
60   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
61     if (isLE)
62       return is64Bits ? ID_ELF64L : ID_ELF32L;
63     else
64       return is64Bits ? ID_ELF64B : ID_ELF32B;
65   }
66
67   static unsigned int getMachOType(bool isLE, bool is64Bits) {
68     if (isLE)
69       return is64Bits ? ID_MachO64L : ID_MachO32L;
70     else
71       return is64Bits ? ID_MachO64B : ID_MachO32B;
72   }
73
74 public:
75   virtual ~Binary();
76
77   StringRef getData() const;
78   StringRef getFileName() const;
79
80   // Cast methods.
81   unsigned int getType() const { return TypeID; }
82
83   // Convenience methods
84   bool isObject() const {
85     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
86   }
87
88   bool isArchive() const {
89     return TypeID == ID_Archive;
90   }
91
92   bool isMachOUniversalBinary() const {
93     return TypeID == ID_MachOUniversalBinary;
94   }
95
96   bool isELF() const {
97     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
98   }
99
100   bool isMachO() const {
101     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
102   }
103
104   bool isCOFF() const {
105     return TypeID == ID_COFF;
106   }
107
108   bool isLittleEndian() const {
109     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
110              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
111   }
112 };
113
114 /// @brief Create a Binary from Source, autodetecting the file type.
115 ///
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);
122
123 ErrorOr<Binary *> createBinary(StringRef Path);
124 }
125 }
126
127 #endif