]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/llvm/include/llvm/Object/Binary.h
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / llvm / 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/ADT/OwningPtr.h"
18 #include "llvm/Object/Error.h"
19
20 namespace llvm {
21
22 class MemoryBuffer;
23 class StringRef;
24
25 namespace object {
26
27 class Binary {
28 private:
29   Binary() LLVM_DELETED_FUNCTION;
30   Binary(const Binary &other) LLVM_DELETED_FUNCTION;
31
32   unsigned int TypeID;
33
34 protected:
35   MemoryBuffer *Data;
36
37   Binary(unsigned int Type, MemoryBuffer *Source);
38
39   enum {
40     ID_Archive,
41     // Object and children.
42     ID_StartObjects,
43     ID_COFF,
44
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
49
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
54
55     ID_EndObjects
56   };
57
58   static inline unsigned int getELFType(bool isLE, bool is64Bits) {
59     if (isLE)
60       return is64Bits ? ID_ELF64L : ID_ELF32L;
61     else
62       return is64Bits ? ID_ELF64B : ID_ELF32B;
63   }
64
65   static unsigned int getMachOType(bool isLE, bool is64Bits) {
66     if (isLE)
67       return is64Bits ? ID_MachO64L : ID_MachO32L;
68     else
69       return is64Bits ? ID_MachO64B : ID_MachO32B;
70   }
71
72 public:
73   virtual ~Binary();
74
75   StringRef getData() const;
76   StringRef getFileName() const;
77
78   // Cast methods.
79   unsigned int getType() const { return TypeID; }
80
81   // Convenience methods
82   bool isObject() const {
83     return TypeID > ID_StartObjects && TypeID < ID_EndObjects;
84   }
85
86   bool isArchive() const {
87     return TypeID == ID_Archive;
88   }
89
90   bool isELF() const {
91     return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
92   }
93
94   bool isMachO() const {
95     return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
96   }
97
98   bool isCOFF() const {
99     return TypeID == ID_COFF;
100   }
101
102   bool isLittleEndian() const {
103     return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
104              TypeID == ID_MachO32B || TypeID == ID_MachO64B);
105   }
106 };
107
108 /// @brief Create a Binary from Source, autodetecting the file type.
109 ///
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);
115
116 error_code createBinary(StringRef Path, OwningPtr<Binary> &Result);
117
118 }
119 }
120
121 #endif