1 //===- ObjectFile.h - File format independent object 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 a file format independent ObjectFile class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_OBJECT_FILE_H
15 #define LLVM_OBJECT_OBJECT_FILE_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
37 static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
38 // Check bitwise identical. This is the only legal way to compare a union w/o
39 // knowing which member is in use.
40 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
43 /// SymbolRef - This is a value type class that represents a single symbol in
44 /// the list of symbols in the object file.
46 DataRefImpl SymbolPimpl;
47 const ObjectFile *OwningObject;
50 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
52 bool operator==(const SymbolRef &Other) const;
54 SymbolRef getNext() const;
56 StringRef getName() const;
57 uint64_t getAddress() const;
58 uint64_t getSize() const;
60 /// Returns the ascii char that should be displayed in a symbol table dump via
61 /// nm for this symbol.
62 char getNMTypeChar() const;
64 /// Returns true for symbols that are internal to the object file format such
65 /// as section symbols.
66 bool isInternal() const;
69 /// SectionRef - This is a value type class that represents a single section in
70 /// the list of sections in the object file.
72 DataRefImpl SectionPimpl;
73 const ObjectFile *OwningObject;
76 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
78 bool operator==(const SectionRef &Other) const;
80 SectionRef getNext() const;
82 StringRef getName() const;
83 uint64_t getAddress() const;
84 uint64_t getSize() const;
85 StringRef getContents() const;
87 // FIXME: Move to the normalization layer when it's created.
91 const uint64_t UnknownAddressOrSize = ~0ULL;
93 /// ObjectFile - This class is the base class for all object file types.
94 /// Concrete instances of this object are created by createObjectFile, which
95 /// figure out which type to create.
98 ObjectFile(); // = delete
99 ObjectFile(const ObjectFile &other); // = delete
102 MemoryBuffer *MapFile;
105 ObjectFile(MemoryBuffer *Object);
107 // These functions are for SymbolRef to call internally. The main goal of
108 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
109 // entry in the memory mapped object file. SymbolPimpl cannot contain any
110 // virtual functions because then it could not point into the memory mapped
112 friend class SymbolRef;
113 virtual SymbolRef getSymbolNext(DataRefImpl Symb) const = 0;
114 virtual StringRef getSymbolName(DataRefImpl Symb) const = 0;
115 virtual uint64_t getSymbolAddress(DataRefImpl Symb) const = 0;
116 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
117 virtual char getSymbolNMTypeChar(DataRefImpl Symb) const = 0;
118 virtual bool isSymbolInternal(DataRefImpl Symb) const = 0;
120 // Same as above for SectionRef.
121 friend class SectionRef;
122 virtual SectionRef getSectionNext(DataRefImpl Sec) const = 0;
123 virtual StringRef getSectionName(DataRefImpl Sec) const = 0;
124 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
125 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
126 virtual StringRef getSectionContents(DataRefImpl Sec) const = 0;
127 virtual bool isSectionText(DataRefImpl Sec) const = 0;
131 template<class content_type>
132 class content_iterator {
133 content_type Current;
135 content_iterator(content_type symb)
138 const content_type* operator->() const {
142 bool operator==(const content_iterator &other) const {
143 return Current == other.Current;
146 bool operator!=(const content_iterator &other) const {
147 return !(*this == other);
150 content_iterator& operator++() { // Preincrement
151 Current = Current.getNext();
156 typedef content_iterator<SymbolRef> symbol_iterator;
157 typedef content_iterator<SectionRef> section_iterator;
159 virtual ~ObjectFile();
161 virtual symbol_iterator begin_symbols() const = 0;
162 virtual symbol_iterator end_symbols() const = 0;
164 virtual section_iterator begin_sections() const = 0;
165 virtual section_iterator end_sections() const = 0;
167 /// @brief The number of bytes used to represent an address in this object
169 virtual uint8_t getBytesInAddress() const = 0;
171 virtual StringRef getFileFormatName() const = 0;
172 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
174 StringRef getFilename() const;
176 /// @returns Pointer to ObjectFile subclass to handle this type of object.
177 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
179 /// @brief Create ObjectFile from path.
180 static ObjectFile *createObjectFile(StringRef ObjectPath);
181 static ObjectFile *createObjectFile(MemoryBuffer *Object);
184 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
185 static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
186 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
187 static ObjectFile *createArchiveObjectFile(MemoryBuffer *Object);
188 static ObjectFile *createLibObjectFile(MemoryBuffer *Object);
191 // Inline function definitions.
192 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
193 : SymbolPimpl(SymbolP)
194 , OwningObject(Owner) {}
196 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
197 return SymbolPimpl == Other.SymbolPimpl;
200 inline SymbolRef SymbolRef::getNext() const {
201 return OwningObject->getSymbolNext(SymbolPimpl);
204 inline StringRef SymbolRef::getName() const {
205 return OwningObject->getSymbolName(SymbolPimpl);
208 inline uint64_t SymbolRef::getAddress() const {
209 return OwningObject->getSymbolAddress(SymbolPimpl);
212 inline uint64_t SymbolRef::getSize() const {
213 return OwningObject->getSymbolSize(SymbolPimpl);
216 inline char SymbolRef::getNMTypeChar() const {
217 return OwningObject->getSymbolNMTypeChar(SymbolPimpl);
220 inline bool SymbolRef::isInternal() const {
221 return OwningObject->isSymbolInternal(SymbolPimpl);
226 inline SectionRef::SectionRef(DataRefImpl SectionP,
227 const ObjectFile *Owner)
228 : SectionPimpl(SectionP)
229 , OwningObject(Owner) {}
231 inline bool SectionRef::operator==(const SectionRef &Other) const {
232 return SectionPimpl == Other.SectionPimpl;
235 inline SectionRef SectionRef::getNext() const {
236 return OwningObject->getSectionNext(SectionPimpl);
239 inline StringRef SectionRef::getName() const {
240 return OwningObject->getSectionName(SectionPimpl);
243 inline uint64_t SectionRef::getAddress() const {
244 return OwningObject->getSectionAddress(SectionPimpl);
247 inline uint64_t SectionRef::getSize() const {
248 return OwningObject->getSectionSize(SectionPimpl);
251 inline StringRef SectionRef::getContents() const {
252 return OwningObject->getSectionContents(SectionPimpl);
255 inline bool SectionRef::isText() const {
256 return OwningObject->isSectionText(SectionPimpl);
259 } // end namespace object
260 } // end namespace llvm