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"
32 // ELF needs this for relocations. This entire union should probably be a
33 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
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& increment(error_code &err) {
68 if (error_code ec = Current.getNext(next))
76 static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
77 // Check bitwise identical. This is the only legal way to compare a union w/o
78 // knowing which member is in use.
79 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
82 static bool operator <(const DataRefImpl &a, const DataRefImpl &b) {
83 // Check bitwise identical. This is the only legal way to compare a union w/o
84 // knowing which member is in use.
85 return std::memcmp(&a, &b, sizeof(DataRefImpl)) < 0;
90 /// RelocationRef - This is a value type class that represents a single
91 /// relocation in the list of relocations in the object file.
93 DataRefImpl RelocationPimpl;
94 const ObjectFile *OwningObject;
97 RelocationRef() : OwningObject(NULL) {
98 std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl));
101 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
103 bool operator==(const RelocationRef &Other) const;
105 error_code getNext(RelocationRef &Result) const;
107 error_code getAddress(uint64_t &Result) const;
108 error_code getOffset(uint64_t &Result) const;
109 error_code getSymbol(SymbolRef &Result) const;
110 error_code getType(uint64_t &Result) const;
112 /// @brief Indicates whether this relocation should hidden when listing
113 /// relocations, usually because it is the trailing part of a multipart
114 /// relocation that will be printed as part of the leading relocation.
115 error_code getHidden(bool &Result) const;
117 /// @brief Get a string that represents the type of this relocation.
119 /// This is for display purposes only.
120 error_code getTypeName(SmallVectorImpl<char> &Result) const;
121 error_code getAdditionalInfo(int64_t &Result) const;
123 /// @brief Get a string that represents the calculation of the value of this
126 /// This is for display purposes only.
127 error_code getValueString(SmallVectorImpl<char> &Result) const;
129 typedef content_iterator<RelocationRef> relocation_iterator;
131 /// SectionRef - This is a value type class that represents a single section in
132 /// the list of sections in the object file.
134 friend class SymbolRef;
135 DataRefImpl SectionPimpl;
136 const ObjectFile *OwningObject;
139 SectionRef() : OwningObject(NULL) {
140 std::memset(&SectionPimpl, 0, sizeof(SectionPimpl));
143 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
145 bool operator==(const SectionRef &Other) const;
146 bool operator <(const SectionRef &Other) const;
148 error_code getNext(SectionRef &Result) const;
150 error_code getName(StringRef &Result) const;
151 error_code getAddress(uint64_t &Result) const;
152 error_code getSize(uint64_t &Result) const;
153 error_code getContents(StringRef &Result) const;
155 /// @brief Get the alignment of this section as the actual value (not log 2).
156 error_code getAlignment(uint64_t &Result) const;
158 // FIXME: Move to the normalization layer when it's created.
159 error_code isText(bool &Result) const;
160 error_code isData(bool &Result) const;
161 error_code isBSS(bool &Result) const;
163 error_code containsSymbol(SymbolRef S, bool &Result) const;
165 relocation_iterator begin_relocations() const;
166 relocation_iterator end_relocations() const;
168 typedef content_iterator<SectionRef> section_iterator;
170 /// SymbolRef - This is a value type class that represents a single symbol in
171 /// the list of symbols in the object file.
173 friend class SectionRef;
174 DataRefImpl SymbolPimpl;
175 const ObjectFile *OwningObject;
178 SymbolRef() : OwningObject(NULL) {
179 std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl));
185 ST_External, // Defined in another object file
191 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
193 bool operator==(const SymbolRef &Other) const;
194 bool operator <(const SymbolRef &Other) const;
196 error_code getNext(SymbolRef &Result) const;
198 error_code getName(StringRef &Result) const;
199 error_code getAddress(uint64_t &Result) const;
200 error_code getFileOffset(uint64_t &Result) const;
201 error_code getSize(uint64_t &Result) const;
202 error_code getType(SymbolRef::Type &Result) const;
204 /// Returns the ascii char that should be displayed in a symbol table dump via
205 /// nm for this symbol.
206 error_code getNMTypeChar(char &Result) const;
208 /// Returns true for symbols that are internal to the object file format such
209 /// as section symbols.
210 error_code isInternal(bool &Result) const;
212 /// Returns true for symbols that can be used in another objects,
213 /// such as library functions
214 error_code isGlobal(bool &Result) const;
216 /// Returns true for weak symbols.
217 error_code isWeak(bool &Result) const;
219 /// @brief Return true for absolute symbols.
220 error_code isAbsolute(bool &Result) const;
222 /// @brief Get section this symbol is defined in reference to. Result is
223 /// end_sections() if it is undefined or is an absolute symbol.
224 error_code getSection(section_iterator &Result) const;
226 DataRefImpl getRawDataRefImpl() const;
228 typedef content_iterator<SymbolRef> symbol_iterator;
230 const uint64_t UnknownAddressOrSize = ~0ULL;
232 /// ObjectFile - This class is the base class for all object file types.
233 /// Concrete instances of this object are created by createObjectFile, which
234 /// figure out which type to create.
235 class ObjectFile : public Binary {
236 virtual void anchor();
237 ObjectFile(); // = delete
238 ObjectFile(const ObjectFile &other); // = delete
241 ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
243 const uint8_t *base() const {
244 return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
247 // These functions are for SymbolRef to call internally. The main goal of
248 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
249 // entry in the memory mapped object file. SymbolPimpl cannot contain any
250 // virtual functions because then it could not point into the memory mapped
253 // Implementations assume that the DataRefImpl is valid and has not been
254 // modified externally. It's UB otherwise.
255 friend class SymbolRef;
256 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
257 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
258 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
259 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const =0;
260 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
261 virtual error_code getSymbolType(DataRefImpl Symb,
262 SymbolRef::Type &Res) const = 0;
263 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
264 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
265 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const = 0;
266 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const = 0;
267 virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const = 0;
268 virtual error_code getSymbolSection(DataRefImpl Symb,
269 section_iterator &Res) const = 0;
271 // Same as above for SectionRef.
272 friend class SectionRef;
273 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
274 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
275 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
276 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
277 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
278 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
279 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
280 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
281 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
282 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
283 bool &Result) const = 0;
284 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
285 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
288 // Same as above for RelocationRef.
289 friend class RelocationRef;
290 virtual error_code getRelocationNext(DataRefImpl Rel,
291 RelocationRef &Res) const = 0;
292 virtual error_code getRelocationAddress(DataRefImpl Rel,
293 uint64_t &Res) const =0;
294 virtual error_code getRelocationOffset(DataRefImpl Rel,
295 uint64_t &Res) const =0;
296 virtual error_code getRelocationSymbol(DataRefImpl Rel,
297 SymbolRef &Res) const = 0;
298 virtual error_code getRelocationType(DataRefImpl Rel,
299 uint64_t &Res) const = 0;
300 virtual error_code getRelocationTypeName(DataRefImpl Rel,
301 SmallVectorImpl<char> &Result) const = 0;
302 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
303 int64_t &Res) const = 0;
304 virtual error_code getRelocationValueString(DataRefImpl Rel,
305 SmallVectorImpl<char> &Result) const = 0;
306 virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const {
308 return object_error::success;
313 virtual symbol_iterator begin_symbols() const = 0;
314 virtual symbol_iterator end_symbols() const = 0;
316 virtual symbol_iterator begin_dynamic_symbols() const = 0;
317 virtual symbol_iterator end_dynamic_symbols() const = 0;
319 virtual section_iterator begin_sections() const = 0;
320 virtual section_iterator end_sections() const = 0;
322 /// @brief The number of bytes used to represent an address in this object
324 virtual uint8_t getBytesInAddress() const = 0;
326 virtual StringRef getFileFormatName() const = 0;
327 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
329 /// @returns Pointer to ObjectFile subclass to handle this type of object.
330 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
332 /// @brief Create ObjectFile from path.
333 static ObjectFile *createObjectFile(StringRef ObjectPath);
334 static ObjectFile *createObjectFile(MemoryBuffer *Object);
336 static inline bool classof(const Binary *v) {
337 return v->getType() >= isObject &&
338 v->getType() < lastObject;
340 static inline bool classof(const ObjectFile *v) { return true; }
343 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
344 static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
345 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
348 // Inline function definitions.
349 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
350 : SymbolPimpl(SymbolP)
351 , OwningObject(Owner) {}
353 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
354 return SymbolPimpl == Other.SymbolPimpl;
357 inline bool SymbolRef::operator <(const SymbolRef &Other) const {
358 return SymbolPimpl < Other.SymbolPimpl;
361 inline error_code SymbolRef::getNext(SymbolRef &Result) const {
362 return OwningObject->getSymbolNext(SymbolPimpl, Result);
365 inline error_code SymbolRef::getName(StringRef &Result) const {
366 return OwningObject->getSymbolName(SymbolPimpl, Result);
369 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
370 return OwningObject->getSymbolAddress(SymbolPimpl, Result);
373 inline error_code SymbolRef::getFileOffset(uint64_t &Result) const {
374 return OwningObject->getSymbolFileOffset(SymbolPimpl, Result);
377 inline error_code SymbolRef::getSize(uint64_t &Result) const {
378 return OwningObject->getSymbolSize(SymbolPimpl, Result);
381 inline error_code SymbolRef::getNMTypeChar(char &Result) const {
382 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
385 inline error_code SymbolRef::isInternal(bool &Result) const {
386 return OwningObject->isSymbolInternal(SymbolPimpl, Result);
389 inline error_code SymbolRef::isGlobal(bool &Result) const {
390 return OwningObject->isSymbolGlobal(SymbolPimpl, Result);
393 inline error_code SymbolRef::isWeak(bool &Result) const {
394 return OwningObject->isSymbolWeak(SymbolPimpl, Result);
397 inline error_code SymbolRef::isAbsolute(bool &Result) const {
398 return OwningObject->isSymbolAbsolute(SymbolPimpl, Result);
401 inline error_code SymbolRef::getSection(section_iterator &Result) const {
402 return OwningObject->getSymbolSection(SymbolPimpl, Result);
405 inline error_code SymbolRef::getType(SymbolRef::Type &Result) const {
406 return OwningObject->getSymbolType(SymbolPimpl, Result);
409 inline DataRefImpl SymbolRef::getRawDataRefImpl() const {
415 inline SectionRef::SectionRef(DataRefImpl SectionP,
416 const ObjectFile *Owner)
417 : SectionPimpl(SectionP)
418 , OwningObject(Owner) {}
420 inline bool SectionRef::operator==(const SectionRef &Other) const {
421 return SectionPimpl == Other.SectionPimpl;
424 inline bool SectionRef::operator <(const SectionRef &Other) const {
425 return SectionPimpl < Other.SectionPimpl;
428 inline error_code SectionRef::getNext(SectionRef &Result) const {
429 return OwningObject->getSectionNext(SectionPimpl, Result);
432 inline error_code SectionRef::getName(StringRef &Result) const {
433 return OwningObject->getSectionName(SectionPimpl, Result);
436 inline error_code SectionRef::getAddress(uint64_t &Result) const {
437 return OwningObject->getSectionAddress(SectionPimpl, Result);
440 inline error_code SectionRef::getSize(uint64_t &Result) const {
441 return OwningObject->getSectionSize(SectionPimpl, Result);
444 inline error_code SectionRef::getContents(StringRef &Result) const {
445 return OwningObject->getSectionContents(SectionPimpl, Result);
448 inline error_code SectionRef::getAlignment(uint64_t &Result) const {
449 return OwningObject->getSectionAlignment(SectionPimpl, Result);
452 inline error_code SectionRef::isText(bool &Result) const {
453 return OwningObject->isSectionText(SectionPimpl, Result);
456 inline error_code SectionRef::isData(bool &Result) const {
457 return OwningObject->isSectionData(SectionPimpl, Result);
460 inline error_code SectionRef::isBSS(bool &Result) const {
461 return OwningObject->isSectionBSS(SectionPimpl, Result);
464 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
465 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
469 inline relocation_iterator SectionRef::begin_relocations() const {
470 return OwningObject->getSectionRelBegin(SectionPimpl);
473 inline relocation_iterator SectionRef::end_relocations() const {
474 return OwningObject->getSectionRelEnd(SectionPimpl);
479 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
480 const ObjectFile *Owner)
481 : RelocationPimpl(RelocationP)
482 , OwningObject(Owner) {}
484 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
485 return RelocationPimpl == Other.RelocationPimpl;
488 inline error_code RelocationRef::getNext(RelocationRef &Result) const {
489 return OwningObject->getRelocationNext(RelocationPimpl, Result);
492 inline error_code RelocationRef::getAddress(uint64_t &Result) const {
493 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
496 inline error_code RelocationRef::getOffset(uint64_t &Result) const {
497 return OwningObject->getRelocationOffset(RelocationPimpl, Result);
500 inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
501 return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
504 inline error_code RelocationRef::getType(uint64_t &Result) const {
505 return OwningObject->getRelocationType(RelocationPimpl, Result);
508 inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
510 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
513 inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
514 return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
517 inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
519 return OwningObject->getRelocationValueString(RelocationPimpl, Result);
522 inline error_code RelocationRef::getHidden(bool &Result) const {
523 return OwningObject->getRelocationHidden(RelocationPimpl, Result);
526 } // end namespace object
527 } // end namespace llvm