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_OBJECTFILE_H
15 #define LLVM_OBJECT_OBJECTFILE_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Object/Binary.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MemoryBuffer.h"
32 // This entire union should probably be a
33 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
39 std::memset(this, 0, sizeof(DataRefImpl));
43 template<class content_type>
44 class content_iterator {
47 content_iterator(content_type symb)
50 const content_type* operator->() const {
54 const content_type &operator*() const {
58 bool operator==(const content_iterator &other) const {
59 return Current == other.Current;
62 bool operator!=(const content_iterator &other) const {
63 return !(*this == other);
66 content_iterator &operator++() { // preincrement
72 inline bool operator==(const DataRefImpl &a, const DataRefImpl &b) {
73 // Check bitwise identical. This is the only legal way to compare a union w/o
74 // knowing which member is in use.
75 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
78 inline bool operator<(const DataRefImpl &a, const DataRefImpl &b) {
79 // Check bitwise identical. This is the only legal way to compare a union w/o
80 // knowing which member is in use.
81 return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
85 typedef content_iterator<SymbolRef> symbol_iterator;
87 /// RelocationRef - This is a value type class that represents a single
88 /// relocation in the list of relocations in the object file.
90 DataRefImpl RelocationPimpl;
91 const ObjectFile *OwningObject;
94 RelocationRef() : OwningObject(NULL) { }
96 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
98 bool operator==(const RelocationRef &Other) const;
102 error_code getAddress(uint64_t &Result) const;
103 error_code getOffset(uint64_t &Result) const;
104 symbol_iterator getSymbol() const;
105 error_code getType(uint64_t &Result) const;
107 /// @brief Indicates whether this relocation should hidden when listing
108 /// relocations, usually because it is the trailing part of a multipart
109 /// relocation that will be printed as part of the leading relocation.
110 error_code getHidden(bool &Result) const;
112 /// @brief Get a string that represents the type of this relocation.
114 /// This is for display purposes only.
115 error_code getTypeName(SmallVectorImpl<char> &Result) const;
117 /// @brief Get a string that represents the calculation of the value of this
120 /// This is for display purposes only.
121 error_code getValueString(SmallVectorImpl<char> &Result) const;
123 DataRefImpl getRawDataRefImpl() const;
124 const ObjectFile *getObjectFile() const;
126 typedef content_iterator<RelocationRef> relocation_iterator;
128 /// SectionRef - This is a value type class that represents a single section in
129 /// the list of sections in the object file.
131 typedef content_iterator<SectionRef> section_iterator;
133 friend class SymbolRef;
134 DataRefImpl SectionPimpl;
135 const ObjectFile *OwningObject;
138 SectionRef() : OwningObject(NULL) { }
140 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
142 bool operator==(const SectionRef &Other) const;
143 bool operator<(const SectionRef &Other) const;
147 error_code getName(StringRef &Result) const;
148 error_code getAddress(uint64_t &Result) const;
149 error_code getSize(uint64_t &Result) const;
150 error_code getContents(StringRef &Result) const;
152 /// @brief Get the alignment of this section as the actual value (not log 2).
153 error_code getAlignment(uint64_t &Result) const;
155 // FIXME: Move to the normalization layer when it's created.
156 error_code isText(bool &Result) const;
157 error_code isData(bool &Result) const;
158 error_code isBSS(bool &Result) const;
159 error_code isRequiredForExecution(bool &Result) const;
160 error_code isVirtual(bool &Result) const;
161 error_code isZeroInit(bool &Result) const;
162 error_code isReadOnlyData(bool &Result) const;
164 error_code containsSymbol(SymbolRef S, bool &Result) const;
166 relocation_iterator relocation_begin() const;
167 relocation_iterator relocation_end() const;
168 section_iterator getRelocatedSection() const;
170 DataRefImpl getRawDataRefImpl() const;
173 /// SymbolRef - This is a value type class that represents a single symbol in
174 /// the list of symbols in the object file.
176 friend class SectionRef;
177 DataRefImpl SymbolPimpl;
178 const ObjectFile *OwningObject;
181 SymbolRef() : OwningObject(NULL) { }
184 ST_Unknown, // Type not specified
192 enum Flags LLVM_ENUM_INT_TYPE(unsigned) {
194 SF_Undefined = 1U << 0, // Symbol is defined in another object file
195 SF_Global = 1U << 1, // Global symbol
196 SF_Weak = 1U << 2, // Weak symbol
197 SF_Absolute = 1U << 3, // Absolute symbol
198 SF_Common = 1U << 4, // Symbol has common linkage
199 SF_FormatSpecific = 1U << 5 // Specific to the object file format
200 // (e.g. section symbols)
203 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
205 bool operator==(const SymbolRef &Other) const;
206 bool operator<(const SymbolRef &Other) const;
210 error_code getName(StringRef &Result) const;
211 /// Returns the symbol virtual address (i.e. address at which it will be
213 error_code getAddress(uint64_t &Result) const;
214 error_code getFileOffset(uint64_t &Result) const;
215 /// @brief Get the alignment of this symbol as the actual value (not log 2).
216 error_code getAlignment(uint32_t &Result) const;
217 error_code getSize(uint64_t &Result) const;
218 error_code getType(SymbolRef::Type &Result) const;
220 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
221 uint32_t getFlags() const;
223 /// @brief Get section this symbol is defined in reference to. Result is
224 /// end_sections() if it is undefined or is an absolute symbol.
225 error_code getSection(section_iterator &Result) const;
227 /// @brief Get value of the symbol in the symbol table.
228 error_code getValue(uint64_t &Val) const;
230 DataRefImpl getRawDataRefImpl() const;
233 /// LibraryRef - This is a value type class that represents a single library in
234 /// the list of libraries needed by a shared or dynamic object.
236 friend class SectionRef;
237 DataRefImpl LibraryPimpl;
238 const ObjectFile *OwningObject;
241 LibraryRef() : OwningObject(NULL) { }
243 LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner);
245 bool operator==(const LibraryRef &Other) const;
246 bool operator<(const LibraryRef &Other) const;
248 error_code getNext(LibraryRef &Result) const;
250 // Get the path to this library, as stored in the object file.
251 error_code getPath(StringRef &Result) const;
253 DataRefImpl getRawDataRefImpl() const;
255 typedef content_iterator<LibraryRef> library_iterator;
257 const uint64_t UnknownAddressOrSize = ~0ULL;
259 /// ObjectFile - This class is the base class for all object file types.
260 /// Concrete instances of this object are created by createObjectFile, which
261 /// figures out which type to create.
262 class ObjectFile : public Binary {
263 virtual void anchor();
264 ObjectFile() LLVM_DELETED_FUNCTION;
265 ObjectFile(const ObjectFile &other) LLVM_DELETED_FUNCTION;
268 ObjectFile(unsigned int Type, MemoryBuffer *Source, bool BufferOwned = true);
270 const uint8_t *base() const {
271 return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
274 // These functions are for SymbolRef to call internally. The main goal of
275 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
276 // entry in the memory mapped object file. SymbolPimpl cannot contain any
277 // virtual functions because then it could not point into the memory mapped
280 // Implementations assume that the DataRefImpl is valid and has not been
281 // modified externally. It's UB otherwise.
282 friend class SymbolRef;
283 virtual void moveSymbolNext(DataRefImpl &Symb) const = 0;
284 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
285 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const = 0;
286 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res)const=0;
287 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
288 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
289 virtual error_code getSymbolType(DataRefImpl Symb,
290 SymbolRef::Type &Res) const = 0;
291 virtual uint32_t getSymbolFlags(DataRefImpl Symb) const = 0;
292 virtual error_code getSymbolSection(DataRefImpl Symb,
293 section_iterator &Res) const = 0;
294 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const = 0;
296 // Same as above for SectionRef.
297 friend class SectionRef;
298 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
299 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
300 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
301 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
302 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
303 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
304 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
305 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
306 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
307 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
308 bool &Res) const = 0;
309 // A section is 'virtual' if its contents aren't present in the object image.
310 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const = 0;
311 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const = 0;
312 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const =0;
313 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
314 bool &Result) const = 0;
315 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
316 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
317 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
319 // Same as above for RelocationRef.
320 friend class RelocationRef;
321 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
322 virtual error_code getRelocationAddress(DataRefImpl Rel,
323 uint64_t &Res) const =0;
324 virtual error_code getRelocationOffset(DataRefImpl Rel,
325 uint64_t &Res) const =0;
326 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
327 virtual error_code getRelocationType(DataRefImpl Rel,
328 uint64_t &Res) const = 0;
329 virtual error_code getRelocationTypeName(DataRefImpl Rel,
330 SmallVectorImpl<char> &Result) const = 0;
331 virtual error_code getRelocationValueString(DataRefImpl Rel,
332 SmallVectorImpl<char> &Result) const = 0;
333 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const {
335 return object_error::success;
338 // Same for LibraryRef
339 friend class LibraryRef;
340 virtual error_code getLibraryNext(DataRefImpl Lib, LibraryRef &Res) const = 0;
341 virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0;
345 virtual symbol_iterator symbol_begin() const = 0;
346 virtual symbol_iterator symbol_end() const = 0;
348 virtual section_iterator section_begin() const = 0;
349 virtual section_iterator section_end() const = 0;
351 virtual library_iterator needed_library_begin() const = 0;
352 virtual library_iterator needed_library_end() const = 0;
354 /// @brief The number of bytes used to represent an address in this object
356 virtual uint8_t getBytesInAddress() const = 0;
358 virtual StringRef getFileFormatName() const = 0;
359 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
361 /// For shared objects, returns the name which this object should be
362 /// loaded from at runtime. This corresponds to DT_SONAME on ELF and
363 /// LC_ID_DYLIB (install name) on MachO.
364 virtual StringRef getLoadName() const = 0;
366 /// @returns Pointer to ObjectFile subclass to handle this type of object.
367 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
369 /// @brief Create ObjectFile from path.
370 static ErrorOr<ObjectFile *> createObjectFile(StringRef ObjectPath);
371 static ErrorOr<ObjectFile *> createObjectFile(MemoryBuffer *Object,
373 sys::fs::file_magic Type);
374 static ErrorOr<ObjectFile *> createObjectFile(MemoryBuffer *Object) {
375 return createObjectFile(Object, true, sys::fs::file_magic::unknown);
379 static inline bool classof(const Binary *v) {
380 return v->isObject();
384 static ErrorOr<ObjectFile *> createCOFFObjectFile(MemoryBuffer *Object,
385 bool BufferOwned = true);
386 static ErrorOr<ObjectFile *> createELFObjectFile(MemoryBuffer *Object,
387 bool BufferOwned = true);
388 static ErrorOr<ObjectFile *> createMachOObjectFile(MemoryBuffer *Object,
389 bool BufferOwned = true);
392 // Inline function definitions.
393 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
394 : SymbolPimpl(SymbolP)
395 , OwningObject(Owner) {}
397 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
398 return SymbolPimpl == Other.SymbolPimpl;
401 inline bool SymbolRef::operator<(const SymbolRef &Other) const {
402 return SymbolPimpl < Other.SymbolPimpl;
405 inline void SymbolRef::moveNext() {
406 return OwningObject->moveSymbolNext(SymbolPimpl);
409 inline error_code SymbolRef::getName(StringRef &Result) const {
410 return OwningObject->getSymbolName(SymbolPimpl, Result);
413 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
414 return OwningObject->getSymbolAddress(SymbolPimpl, Result);
417 inline error_code SymbolRef::getFileOffset(uint64_t &Result) const {
418 return OwningObject->getSymbolFileOffset(SymbolPimpl, Result);
421 inline error_code SymbolRef::getAlignment(uint32_t &Result) const {
422 return OwningObject->getSymbolAlignment(SymbolPimpl, Result);
425 inline error_code SymbolRef::getSize(uint64_t &Result) const {
426 return OwningObject->getSymbolSize(SymbolPimpl, Result);
429 inline uint32_t SymbolRef::getFlags() const {
430 return OwningObject->getSymbolFlags(SymbolPimpl);
433 inline error_code SymbolRef::getSection(section_iterator &Result) const {
434 return OwningObject->getSymbolSection(SymbolPimpl, Result);
437 inline error_code SymbolRef::getType(SymbolRef::Type &Result) const {
438 return OwningObject->getSymbolType(SymbolPimpl, Result);
441 inline error_code SymbolRef::getValue(uint64_t &Val) const {
442 return OwningObject->getSymbolValue(SymbolPimpl, Val);
445 inline DataRefImpl SymbolRef::getRawDataRefImpl() const {
451 inline SectionRef::SectionRef(DataRefImpl SectionP,
452 const ObjectFile *Owner)
453 : SectionPimpl(SectionP)
454 , OwningObject(Owner) {}
456 inline bool SectionRef::operator==(const SectionRef &Other) const {
457 return SectionPimpl == Other.SectionPimpl;
460 inline bool SectionRef::operator<(const SectionRef &Other) const {
461 return SectionPimpl < Other.SectionPimpl;
464 inline void SectionRef::moveNext() {
465 return OwningObject->moveSectionNext(SectionPimpl);
468 inline error_code SectionRef::getName(StringRef &Result) const {
469 return OwningObject->getSectionName(SectionPimpl, Result);
472 inline error_code SectionRef::getAddress(uint64_t &Result) const {
473 return OwningObject->getSectionAddress(SectionPimpl, Result);
476 inline error_code SectionRef::getSize(uint64_t &Result) const {
477 return OwningObject->getSectionSize(SectionPimpl, Result);
480 inline error_code SectionRef::getContents(StringRef &Result) const {
481 return OwningObject->getSectionContents(SectionPimpl, Result);
484 inline error_code SectionRef::getAlignment(uint64_t &Result) const {
485 return OwningObject->getSectionAlignment(SectionPimpl, Result);
488 inline error_code SectionRef::isText(bool &Result) const {
489 return OwningObject->isSectionText(SectionPimpl, Result);
492 inline error_code SectionRef::isData(bool &Result) const {
493 return OwningObject->isSectionData(SectionPimpl, Result);
496 inline error_code SectionRef::isBSS(bool &Result) const {
497 return OwningObject->isSectionBSS(SectionPimpl, Result);
500 inline error_code SectionRef::isRequiredForExecution(bool &Result) const {
501 return OwningObject->isSectionRequiredForExecution(SectionPimpl, Result);
504 inline error_code SectionRef::isVirtual(bool &Result) const {
505 return OwningObject->isSectionVirtual(SectionPimpl, Result);
508 inline error_code SectionRef::isZeroInit(bool &Result) const {
509 return OwningObject->isSectionZeroInit(SectionPimpl, Result);
512 inline error_code SectionRef::isReadOnlyData(bool &Result) const {
513 return OwningObject->isSectionReadOnlyData(SectionPimpl, Result);
516 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
517 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
521 inline relocation_iterator SectionRef::relocation_begin() const {
522 return OwningObject->section_rel_begin(SectionPimpl);
525 inline relocation_iterator SectionRef::relocation_end() const {
526 return OwningObject->section_rel_end(SectionPimpl);
529 inline section_iterator SectionRef::getRelocatedSection() const {
530 return OwningObject->getRelocatedSection(SectionPimpl);
533 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
538 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
539 const ObjectFile *Owner)
540 : RelocationPimpl(RelocationP)
541 , OwningObject(Owner) {}
543 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
544 return RelocationPimpl == Other.RelocationPimpl;
547 inline void RelocationRef::moveNext() {
548 return OwningObject->moveRelocationNext(RelocationPimpl);
551 inline error_code RelocationRef::getAddress(uint64_t &Result) const {
552 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
555 inline error_code RelocationRef::getOffset(uint64_t &Result) const {
556 return OwningObject->getRelocationOffset(RelocationPimpl, Result);
559 inline symbol_iterator RelocationRef::getSymbol() const {
560 return OwningObject->getRelocationSymbol(RelocationPimpl);
563 inline error_code RelocationRef::getType(uint64_t &Result) const {
564 return OwningObject->getRelocationType(RelocationPimpl, Result);
567 inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
569 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
572 inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
574 return OwningObject->getRelocationValueString(RelocationPimpl, Result);
577 inline error_code RelocationRef::getHidden(bool &Result) const {
578 return OwningObject->getRelocationHidden(RelocationPimpl, Result);
581 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
582 return RelocationPimpl;
585 inline const ObjectFile *RelocationRef::getObjectFile() const {
589 // Inline function definitions.
590 inline LibraryRef::LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner)
591 : LibraryPimpl(LibraryP)
592 , OwningObject(Owner) {}
594 inline bool LibraryRef::operator==(const LibraryRef &Other) const {
595 return LibraryPimpl == Other.LibraryPimpl;
598 inline bool LibraryRef::operator<(const LibraryRef &Other) const {
599 return LibraryPimpl < Other.LibraryPimpl;
602 inline error_code LibraryRef::getNext(LibraryRef &Result) const {
603 return OwningObject->getLibraryNext(LibraryPimpl, Result);
606 inline error_code LibraryRef::getPath(StringRef &Result) const {
607 return OwningObject->getLibraryPath(LibraryPimpl, Result);
610 } // end namespace object
611 } // end namespace llvm