Remove dead code.
[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   bool BufferOwned;
35
36 protected:
37   MemoryBuffer *Data;
38
39   Binary(unsigned int Type, MemoryBuffer *Source, bool BufferOwned = true);
40
41   enum {
42     ID_Archive,
43     ID_MachOUniversalBinary,
44     // Object and children.
45     ID_StartObjects,
46     ID_COFF,
47
48     ID_ELF32L, // ELF 32-bit, little endian
49     ID_ELF32B, // ELF 32-bit, big endian
50     ID_ELF64L, // ELF 64-bit, little endian
51     ID_ELF64B, // ELF 64-bit, big endian
52
53     ID_MachO32L, // MachO 32-bit, little endian
54     ID_MachO32B, // MachO 32-bit, big endian
55     ID_MachO64L, // MachO 64-bit, little endian
56     ID_MachO64B, // MachO 64-bit, big endian
57
58     ID_EndObjects
59   };
60
61   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
62     if (isLE)
63       return is64Bits ? ID_ELF64L : ID_ELF32L;
64     else
65       return is64Bits ? ID_ELF64B : ID_ELF32B;
66   }
67
68   static unsigned int getMachOType(bool isLE, bool is64Bits) {
69     if (isLE)
70       return is64Bits ? ID_MachO64L : ID_MachO32L;
71     else
72       return is64Bits ? ID_MachO64B : ID_MachO32B;
73   }
74
75 public:
76   virtual ~Binary();
77
78   StringRef getData() const;
79   StringRef getFileName() const;
80
81   // Cast methods.
82   unsigned int getType() const { return TypeID; }
83
84   // Convenience methods
85   bool isObject() const {
86     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
87   }
88
89   bool isArchive() const {
90     return TypeID == ID_Archive;
91   }
92
93   bool isMachOUniversalBinary() const {
94     return TypeID == ID_MachOUniversalBinary;
95   }
96
97   bool isELF() const {
98     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
99   }
100
101   bool isMachO() const {
102     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
103   }
104
105   bool isCOFF() const {
106     return TypeID == ID_COFF;
107   }
108
109   bool isLittleEndian() const {
110     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
111              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
112   }
113 };
114
115 /// @brief Create a Binary from Source, autodetecting the file type.
116 ///
117 /// @param Source The data to create the Binary from. Ownership is transferred
118 ///        to the Binary if successful. If an error is returned,
119 ///        Source is destroyed by createBinary before returning.
120 ErrorOr<Binary *> createBinary(MemoryBuffer *Source,
121                                sys::fs::file_magic Type =
122                                    sys::fs::file_magic::unknown);
123
124 ErrorOr<Binary *> createBinary(StringRef Path);
125 }
126 }
127
128 #endif