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"
31 // ELF needs this for relocations. This entire union should probably be a
32 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
42 template<class content_type>
43 class content_iterator {
46 content_iterator(content_type symb)
49 const content_type* operator->() const {
53 const content_type &operator*() const {
57 bool operator==(const content_iterator &other) const {
58 return Current == other.Current;
61 bool operator!=(const content_iterator &other) const {
62 return !(*this == other);
65 content_iterator& increment(error_code &err) {
67 if (error_code ec = Current.getNext(next))
75 static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
76 // Check bitwise identical. This is the only legal way to compare a union w/o
77 // knowing which member is in use.
78 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
81 static bool operator <(const DataRefImpl &a, const DataRefImpl &b) {
82 // Check bitwise identical. This is the only legal way to compare a union w/o
83 // knowing which member is in use.
84 return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
89 /// RelocationRef - This is a value type class that represents a single
90 /// relocation in the list of relocations in the object file.
92 DataRefImpl RelocationPimpl;
93 const ObjectFile *OwningObject;
96 RelocationRef() : OwningObject(NULL) {
97 std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl));
100 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
102 bool operator==(const RelocationRef &Other) const;
104 error_code getNext(RelocationRef &Result) const;
106 error_code getAddress(uint64_t &Result) const;
107 error_code getOffset(uint64_t &Result) const;
108 error_code getSymbol(SymbolRef &Result) const;
109 error_code getType(uint64_t &Result) const;
111 /// @brief Indicates whether this relocation should hidden when listing
112 /// relocations, usually because it is the trailing part of a multipart
113 /// relocation that will be printed as part of the leading relocation.
114 error_code getHidden(bool &Result) const;
116 /// @brief Get a string that represents the type of this relocation.
118 /// This is for display purposes only.
119 error_code getTypeName(SmallVectorImpl<char> &Result) const;
120 error_code getAdditionalInfo(int64_t &Result) const;
122 /// @brief Get a string that represents the calculation of the value of this
125 /// This is for display purposes only.
126 error_code getValueString(SmallVectorImpl<char> &Result) const;
128 typedef content_iterator<RelocationRef> relocation_iterator;
130 /// SectionRef - This is a value type class that represents a single section in
131 /// the list of sections in the object file.
133 friend class SymbolRef;
134 DataRefImpl SectionPimpl;
135 const ObjectFile *OwningObject;
138 SectionRef() : OwningObject(NULL) {
139 std::memset(&SectionPimpl, 0, sizeof(SectionPimpl));
142 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
144 bool operator==(const SectionRef &Other) const;
145 bool operator <(const SectionRef &Other) const;
147 error_code getNext(SectionRef &Result) const;
149 error_code getName(StringRef &Result) const;
150 error_code getAddress(uint64_t &Result) const;
151 error_code getSize(uint64_t &Result) const;
152 error_code getContents(StringRef &Result) const;
154 /// @brief Get the alignment of this section as the actual value (not log 2).
155 error_code getAlignment(uint64_t &Result) const;
157 // FIXME: Move to the normalization layer when it's created.
158 error_code isText(bool &Result) const;
159 error_code isData(bool &Result) const;
160 error_code isBSS(bool &Result) const;
162 error_code containsSymbol(SymbolRef S, bool &Result) const;
164 relocation_iterator begin_relocations() const;
165 relocation_iterator end_relocations() const;
167 typedef content_iterator<SectionRef> section_iterator;
169 /// SymbolRef - This is a value type class that represents a single symbol in
170 /// the list of symbols in the object file.
172 friend class SectionRef;
173 DataRefImpl SymbolPimpl;
174 const ObjectFile *OwningObject;
177 SymbolRef() : OwningObject(NULL) {
178 std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl));
184 ST_External, // Defined in another object file
190 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
192 bool operator==(const SymbolRef &Other) const;
193 bool operator <(const SymbolRef &Other) const;
195 error_code getNext(SymbolRef &Result) const;
197 error_code getName(StringRef &Result) const;
198 error_code getAddress(uint64_t &Result) const;
199 error_code getFileOffset(uint64_t &Result) const;
200 error_code getSize(uint64_t &Result) const;
201 error_code getType(SymbolRef::Type &Result) const;
203 /// Returns the ascii char that should be displayed in a symbol table dump via
204 /// nm for this symbol.
205 error_code getNMTypeChar(char &Result) const;
207 /// Returns true for symbols that are internal to the object file format such
208 /// as section symbols.
209 error_code isInternal(bool &Result) const;
211 /// Returns true for symbols that can be used in another objects,
212 /// such as library functions
213 error_code isGlobal(bool &Result) const;
215 /// Returns true for weak symbols.
216 error_code isWeak(bool &Result) const;
218 /// @brief Return true for absolute symbols.
219 error_code isAbsolute(bool &Result) const;
221 /// @brief Get section this symbol is defined in reference to. Result is
222 /// end_sections() if it is undefined or is an absolute symbol.
223 error_code getSection(section_iterator &Result) const;
225 DataRefImpl getRawDataRefImpl() const;
227 typedef content_iterator<SymbolRef> symbol_iterator;
229 const uint64_t UnknownAddressOrSize = ~0ULL;
231 /// ObjectFile - This class is the base class for all object file types.
232 /// Concrete instances of this object are created by createObjectFile, which
233 /// figure out which type to create.
234 class ObjectFile : public Binary {
236 ObjectFile(); // = delete
237 ObjectFile(const ObjectFile &other); // = delete
240 ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
242 const uint8_t *base() const {
243 return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
246 // These functions are for SymbolRef to call internally. The main goal of
247 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
248 // entry in the memory mapped object file. SymbolPimpl cannot contain any
249 // virtual functions because then it could not point into the memory mapped
252 // Implementations assume that the DataRefImpl is valid and has not been
253 // modified externally. It's UB otherwise.
254 friend class SymbolRef;
255 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
256 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
257 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
258 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const =0;
259 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
260 virtual error_code getSymbolType(DataRefImpl Symb,
261 SymbolRef::Type &Res) const = 0;
262 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
263 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
264 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const = 0;
265 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const = 0;
266 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const = 0;
267 virtual error_code getSymbolSection(DataRefImpl Symb,
268 section_iterator &Res) const = 0;
270 // Same as above for SectionRef.
271 friend class SectionRef;
272 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
273 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
274 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
275 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
276 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
277 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
278 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
279 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
280 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
281 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
282 bool &Result) const = 0;
283 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
284 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
287 // Same as above for RelocationRef.
288 friend class RelocationRef;
289 virtual error_code getRelocationNext(DataRefImpl Rel,
290 RelocationRef &Res) const = 0;
291 virtual error_code getRelocationAddress(DataRefImpl Rel,
292 uint64_t &Res) const =0;
293 virtual error_code getRelocationOffset(DataRefImpl Rel,
294 uint64_t &Res) const =0;
295 virtual error_code getRelocationSymbol(DataRefImpl Rel,
296 SymbolRef &Res) const = 0;
297 virtual error_code getRelocationType(DataRefImpl Rel,
298 uint64_t &Res) const = 0;
299 virtual error_code getRelocationTypeName(DataRefImpl Rel,
300 SmallVectorImpl<char> &Result) const = 0;
301 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
302 int64_t &Res) const = 0;
303 virtual error_code getRelocationValueString(DataRefImpl Rel,
304 SmallVectorImpl<char> &Result) const = 0;
305 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const {
307 return object_error::success;
312 virtual symbol_iterator begin_symbols() const = 0;
313 virtual symbol_iterator end_symbols() const = 0;
315 virtual section_iterator begin_sections() const = 0;
316 virtual section_iterator end_sections() const = 0;
318 /// @brief The number of bytes used to represent an address in this object
320 virtual uint8_t getBytesInAddress() const = 0;
322 virtual StringRef getFileFormatName() const = 0;
323 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
325 /// @returns Pointer to ObjectFile subclass to handle this type of object.
326 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
328 /// @brief Create ObjectFile from path.
329 static ObjectFile *createObjectFile(StringRef ObjectPath);
330 static ObjectFile *createObjectFile(MemoryBuffer *Object);
332 static inline bool classof(const Binary *v) {
333 return v->getType() >= isObject &&
334 v->getType() < lastObject;
336 static inline bool classof(const ObjectFile *v) { return true; }
339 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
340 static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
341 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
344 // Inline function definitions.
345 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
346 : SymbolPimpl(SymbolP)
347 , OwningObject(Owner) {}
349 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
350 return SymbolPimpl == Other.SymbolPimpl;
353 inline bool SymbolRef::operator <(const SymbolRef &Other) const {
354 return SymbolPimpl < Other.SymbolPimpl;
357 inline error_code SymbolRef::getNext(SymbolRef &Result) const {
358 return OwningObject->getSymbolNext(SymbolPimpl, Result);
361 inline error_code SymbolRef::getName(StringRef &Result) const {
362 return OwningObject->getSymbolName(SymbolPimpl, Result);
365 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
366 return OwningObject->getSymbolAddress(SymbolPimpl, Result);
369 inline error_code SymbolRef::getFileOffset(uint64_t &Result) const {
370 return OwningObject->getSymbolFileOffset(SymbolPimpl, Result);
373 inline error_code SymbolRef::getSize(uint64_t &Result) const {
374 return OwningObject->getSymbolSize(SymbolPimpl, Result);
377 inline error_code SymbolRef::getNMTypeChar(char &Result) const {
378 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
381 inline error_code SymbolRef::isInternal(bool &Result) const {
382 return OwningObject->isSymbolInternal(SymbolPimpl, Result);
385 inline error_code SymbolRef::isGlobal(bool &Result) const {
386 return OwningObject->isSymbolGlobal(SymbolPimpl, Result);
389 inline error_code SymbolRef::isWeak(bool &Result) const {
390 return OwningObject->isSymbolWeak(SymbolPimpl, Result);
393 inline error_code SymbolRef::isAbsolute(bool &Result) const {
394 return OwningObject->isSymbolAbsolute(SymbolPimpl, Result);
397 inline error_code SymbolRef::getSection(section_iterator &Result) const {
398 return OwningObject->getSymbolSection(SymbolPimpl, Result);
401 inline error_code SymbolRef::getType(SymbolRef::Type &Result) const {
402 return OwningObject->getSymbolType(SymbolPimpl, Result);
405 inline DataRefImpl SymbolRef::getRawDataRefImpl() const {
411 inline SectionRef::SectionRef(DataRefImpl SectionP,
412 const ObjectFile *Owner)
413 : SectionPimpl(SectionP)
414 , OwningObject(Owner) {}
416 inline bool SectionRef::operator==(const SectionRef &Other) const {
417 return SectionPimpl == Other.SectionPimpl;
420 inline bool SectionRef::operator <(const SectionRef &Other) const {
421 return SectionPimpl < Other.SectionPimpl;
424 inline error_code SectionRef::getNext(SectionRef &Result) const {
425 return OwningObject->getSectionNext(SectionPimpl, Result);
428 inline error_code SectionRef::getName(StringRef &Result) const {
429 return OwningObject->getSectionName(SectionPimpl, Result);
432 inline error_code SectionRef::getAddress(uint64_t &Result) const {
433 return OwningObject->getSectionAddress(SectionPimpl, Result);
436 inline error_code SectionRef::getSize(uint64_t &Result) const {
437 return OwningObject->getSectionSize(SectionPimpl, Result);
440 inline error_code SectionRef::getContents(StringRef &Result) const {
441 return OwningObject->getSectionContents(SectionPimpl, Result);
444 inline error_code SectionRef::getAlignment(uint64_t &Result) const {
445 return OwningObject->getSectionAlignment(SectionPimpl, Result);
448 inline error_code SectionRef::isText(bool &Result) const {
449 return OwningObject->isSectionText(SectionPimpl, Result);
452 inline error_code SectionRef::isData(bool &Result) const {
453 return OwningObject->isSectionData(SectionPimpl, Result);
456 inline error_code SectionRef::isBSS(bool &Result) const {
457 return OwningObject->isSectionBSS(SectionPimpl, Result);
460 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
461 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
465 inline relocation_iterator SectionRef::begin_relocations() const {
466 return OwningObject->getSectionRelBegin(SectionPimpl);
469 inline relocation_iterator SectionRef::end_relocations() const {
470 return OwningObject->getSectionRelEnd(SectionPimpl);
475 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
476 const ObjectFile *Owner)
477 : RelocationPimpl(RelocationP)
478 , OwningObject(Owner) {}
480 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
481 return RelocationPimpl == Other.RelocationPimpl;
484 inline error_code RelocationRef::getNext(RelocationRef &Result) const {
485 return OwningObject->getRelocationNext(RelocationPimpl, Result);
488 inline error_code RelocationRef::getAddress(uint64_t &Result) const {
489 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
492 inline error_code RelocationRef::getOffset(uint64_t &Result) const {
493 return OwningObject->getRelocationOffset(RelocationPimpl, Result);
496 inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
497 return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
500 inline error_code RelocationRef::getType(uint64_t &Result) const {
501 return OwningObject->getRelocationType(RelocationPimpl, Result);
504 inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
506 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
509 inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
510 return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
513 inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
515 return OwningObject->getRelocationValueString(RelocationPimpl, Result);
518 inline error_code RelocationRef::getHidden(bool &Result) const {
519 return OwningObject->getRelocationHidden(RelocationPimpl, Result);
522 } // end namespace object
523 } // end namespace llvm