LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Binary.h
Go to the documentation of this file.
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:
30  Binary(const Binary &other) LLVM_DELETED_FUNCTION;
31 
32  unsigned int TypeID;
33 
34 protected:
36 
37  Binary(unsigned int Type, MemoryBuffer *Source);
38 
39  enum {
42  // Object and children.
45 
46  ID_ELF32L, // ELF 32-bit, little endian
47  ID_ELF32B, // ELF 32-bit, big endian
48  ID_ELF64L, // ELF 64-bit, little endian
49  ID_ELF64B, // ELF 64-bit, big endian
50 
51  ID_MachO32L, // MachO 32-bit, little endian
52  ID_MachO32B, // MachO 32-bit, big endian
53  ID_MachO64L, // MachO 64-bit, little endian
54  ID_MachO64B, // MachO 64-bit, big endian
55 
57  };
58 
59  static inline unsigned int getELFType(bool isLE, bool is64Bits) {
60  if (isLE)
61  return is64Bits ? ID_ELF64L : ID_ELF32L;
62  else
63  return is64Bits ? ID_ELF64B : ID_ELF32B;
64  }
65 
66  static unsigned int getMachOType(bool isLE, bool is64Bits) {
67  if (isLE)
68  return is64Bits ? ID_MachO64L : ID_MachO32L;
69  else
70  return is64Bits ? ID_MachO64B : ID_MachO32B;
71  }
72 
73 public:
74  virtual ~Binary();
75 
76  StringRef getData() const;
77  StringRef getFileName() const;
78 
79  // Cast methods.
80  unsigned int getType() const { return TypeID; }
81 
82  // Convenience methods
83  bool isObject() const {
85  }
86 
87  bool isArchive() const {
88  return TypeID == ID_Archive;
89  }
90 
91  bool isMachOUniversalBinary() const {
93  }
94 
95  bool isELF() const {
96  return TypeID >= ID_ELF32L && TypeID <= ID_ELF64B;
97  }
98 
99  bool isMachO() const {
100  return TypeID >= ID_MachO32L && TypeID <= ID_MachO64B;
101  }
102 
103  bool isCOFF() const {
104  return TypeID == ID_COFF;
105  }
106 
107  bool isLittleEndian() const {
108  return !(TypeID == ID_ELF32B || TypeID == ID_ELF64B ||
110  }
111 };
112 
113 /// @brief Create a Binary from Source, autodetecting the file type.
114 ///
115 /// @param Source The data to create the Binary from. Ownership is transferred
116 /// to Result if successful. If an error is returned, Source is destroyed
117 /// by createBinary before returning.
118 /// @param Result A pointer to the resulting Binary if no error occured.
120 
122 
123 }
124 }
125 
126 #endif
bool isELF() const
Definition: Binary.h:95
static unsigned int getMachOType(bool isLE, bool is64Bits)
Definition: Binary.h:66
Type::TypeID TypeID
virtual ~Binary()
Definition: Binary.cpp:29
StringRef getData() const
Definition: Binary.cpp:37
bool isCOFF() const
Definition: Binary.h:103
unsigned int getType() const
Definition: Binary.h:80
error_code createBinary(MemoryBuffer *Source, OwningPtr< Binary > &Result)
Create a Binary from Source, autodetecting the file type.
Definition: Binary.cpp:45
bool isObject() const
Definition: Binary.h:83
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:59
StringRef getFileName() const
Definition: Binary.cpp:41
bool isMachO() const
Definition: Binary.h:99
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
bool isLittleEndian() const
Definition: Binary.h:107
bool isMachOUniversalBinary() const
Definition: Binary.h:91
MemoryBuffer * Data
Definition: Binary.h:35
bool isArchive() const
Definition: Binary.h:87