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/Object/Binary.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MemoryBuffer.h"
36 static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
37 // Check bitwise identical. This is the only legal way to compare a union w/o
38 // knowing which member is in use.
39 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
43 DataRefImpl RelocationPimpl;
44 const ObjectFile *OwningObject;
47 RelocationRef() : OwningObject(NULL) {
48 std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl));
51 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
53 bool operator==(const RelocationRef &Other) const;
55 error_code getNext(RelocationRef &Result);
58 /// SymbolRef - This is a value type class that represents a single symbol in
59 /// the list of symbols in the object file.
61 friend class SectionRef;
62 DataRefImpl SymbolPimpl;
63 const ObjectFile *OwningObject;
66 SymbolRef() : OwningObject(NULL) {
67 std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl));
70 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
72 bool operator==(const SymbolRef &Other) const;
74 error_code getNext(SymbolRef &Result) const;
76 error_code getName(StringRef &Result) const;
77 error_code getAddress(uint64_t &Result) const;
78 error_code getSize(uint64_t &Result) const;
80 /// Returns the ascii char that should be displayed in a symbol table dump via
81 /// nm for this symbol.
82 error_code getNMTypeChar(char &Result) const;
84 /// Returns true for symbols that are internal to the object file format such
85 /// as section symbols.
86 error_code isInternal(bool &Result) const;
89 /// SectionRef - This is a value type class that represents a single section in
90 /// the list of sections in the object file.
92 friend class SymbolRef;
93 DataRefImpl SectionPimpl;
94 const ObjectFile *OwningObject;
97 SectionRef() : OwningObject(NULL) {
98 std::memset(&SectionPimpl, 0, sizeof(SectionPimpl));
101 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
103 bool operator==(const SectionRef &Other) const;
105 error_code getNext(SectionRef &Result) const;
107 error_code getName(StringRef &Result) const;
108 error_code getAddress(uint64_t &Result) const;
109 error_code getSize(uint64_t &Result) const;
110 error_code getContents(StringRef &Result) const;
112 // FIXME: Move to the normalization layer when it's created.
113 error_code isText(bool &Result) const;
115 error_code containsSymbol(SymbolRef S, bool &Result) const;
118 const uint64_t UnknownAddressOrSize = ~0ULL;
120 /// ObjectFile - This class is the base class for all object file types.
121 /// Concrete instances of this object are created by createObjectFile, which
122 /// figure out which type to create.
123 class ObjectFile : public Binary {
125 ObjectFile(); // = delete
126 ObjectFile(const ObjectFile &other); // = delete
129 ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
131 const uint8_t *base() const {
132 return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
135 // These functions are for SymbolRef to call internally. The main goal of
136 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
137 // entry in the memory mapped object file. SymbolPimpl cannot contain any
138 // virtual functions because then it could not point into the memory mapped
141 // Implementations assume that the DataRefImpl is valid and has not been
142 // modified externally. It's UB otherwise.
143 friend class SymbolRef;
144 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
145 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
146 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
147 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
148 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
149 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
151 // Same as above for SectionRef.
152 friend class SectionRef;
153 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
154 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
155 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
156 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
157 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
158 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
159 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
160 bool &Result) const = 0;
164 template<class content_type>
165 class content_iterator {
166 content_type Current;
168 content_iterator(content_type symb)
171 const content_type* operator->() const {
175 bool operator==(const content_iterator &other) const {
176 return Current == other.Current;
179 bool operator!=(const content_iterator &other) const {
180 return !(*this == other);
183 content_iterator& increment(error_code &err) {
185 if (error_code ec = Current.getNext(next))
193 typedef content_iterator<SymbolRef> symbol_iterator;
194 typedef content_iterator<SectionRef> section_iterator;
196 virtual symbol_iterator begin_symbols() const = 0;
197 virtual symbol_iterator end_symbols() const = 0;
199 virtual section_iterator begin_sections() const = 0;
200 virtual section_iterator end_sections() const = 0;
202 /// @brief The number of bytes used to represent an address in this object
204 virtual uint8_t getBytesInAddress() const = 0;
206 virtual StringRef getFileFormatName() const = 0;
207 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
209 /// @returns Pointer to ObjectFile subclass to handle this type of object.
210 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
212 /// @brief Create ObjectFile from path.
213 static ObjectFile *createObjectFile(StringRef ObjectPath);
214 static ObjectFile *createObjectFile(MemoryBuffer *Object);
216 static inline bool classof(const Binary *v) {
217 return v->getType() >= isObject &&
218 v->getType() < lastObject;
220 static inline bool classof(const ObjectFile *v) { return true; }
223 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
224 static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
225 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
228 // Inline function definitions.
229 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
230 : SymbolPimpl(SymbolP)
231 , OwningObject(Owner) {}
233 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
234 return SymbolPimpl == Other.SymbolPimpl;
237 inline error_code SymbolRef::getNext(SymbolRef &Result) const {
238 return OwningObject->getSymbolNext(SymbolPimpl, Result);
241 inline error_code SymbolRef::getName(StringRef &Result) const {
242 return OwningObject->getSymbolName(SymbolPimpl, Result);
245 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
246 return OwningObject->getSymbolAddress(SymbolPimpl, Result);
249 inline error_code SymbolRef::getSize(uint64_t &Result) const {
250 return OwningObject->getSymbolSize(SymbolPimpl, Result);
253 inline error_code SymbolRef::getNMTypeChar(char &Result) const {
254 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
257 inline error_code SymbolRef::isInternal(bool &Result) const {
258 return OwningObject->isSymbolInternal(SymbolPimpl, Result);
263 inline SectionRef::SectionRef(DataRefImpl SectionP,
264 const ObjectFile *Owner)
265 : SectionPimpl(SectionP)
266 , OwningObject(Owner) {}
268 inline bool SectionRef::operator==(const SectionRef &Other) const {
269 return SectionPimpl == Other.SectionPimpl;
272 inline error_code SectionRef::getNext(SectionRef &Result) const {
273 return OwningObject->getSectionNext(SectionPimpl, Result);
276 inline error_code SectionRef::getName(StringRef &Result) const {
277 return OwningObject->getSectionName(SectionPimpl, Result);
280 inline error_code SectionRef::getAddress(uint64_t &Result) const {
281 return OwningObject->getSectionAddress(SectionPimpl, Result);
284 inline error_code SectionRef::getSize(uint64_t &Result) const {
285 return OwningObject->getSectionSize(SectionPimpl, Result);
288 inline error_code SectionRef::getContents(StringRef &Result) const {
289 return OwningObject->getSectionContents(SectionPimpl, Result);
292 inline error_code SectionRef::isText(bool &Result) const {
293 return OwningObject->isSectionText(SectionPimpl, Result);
296 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
297 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
301 } // end namespace object
302 } // end namespace llvm