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.
42 std::memset(this, 0, sizeof(DataRefImpl
));
46 template<class content_type
>
47 class content_iterator
{
50 content_iterator(content_type symb
)
53 const content_type
* operator->() const {
57 const content_type
&operator*() const {
61 bool operator==(const content_iterator
&other
) const {
62 return Current
== other
.Current
;
65 bool operator!=(const content_iterator
&other
) const {
66 return !(*this == other
);
69 content_iterator
& increment(error_code
&err
) {
71 if (error_code ec
= Current
.getNext(next
))
79 inline bool operator ==(const DataRefImpl
&a
, const DataRefImpl
&b
) {
80 // Check bitwise identical. This is the only legal way to compare a union w/o
81 // knowing which member is in use.
82 return std::memcmp(&a
, &b
, sizeof(DataRefImpl
)) == 0;
85 inline bool operator <(const DataRefImpl
&a
, const DataRefImpl
&b
) {
86 // Check bitwise identical. This is the only legal way to compare a union w/o
87 // knowing which member is in use.
88 return std::memcmp(&a
, &b
, sizeof(DataRefImpl
)) < 0;
93 /// RelocationRef - This is a value type class that represents a single
94 /// relocation in the list of relocations in the object file.
96 DataRefImpl RelocationPimpl
;
97 const ObjectFile
*OwningObject
;
100 RelocationRef() : OwningObject(NULL
) { }
102 RelocationRef(DataRefImpl RelocationP
, const ObjectFile
*Owner
);
104 bool operator==(const RelocationRef
&Other
) const;
106 error_code
getNext(RelocationRef
&Result
) const;
108 error_code
getAddress(uint64_t &Result
) const;
109 error_code
getOffset(uint64_t &Result
) const;
110 error_code
getSymbol(SymbolRef
&Result
) const;
111 error_code
getType(uint64_t &Result
) const;
113 /// @brief Indicates whether this relocation should hidden when listing
114 /// relocations, usually because it is the trailing part of a multipart
115 /// relocation that will be printed as part of the leading relocation.
116 error_code
getHidden(bool &Result
) const;
118 /// @brief Get a string that represents the type of this relocation.
120 /// This is for display purposes only.
121 error_code
getTypeName(SmallVectorImpl
<char> &Result
) const;
122 error_code
getAdditionalInfo(int64_t &Result
) const;
124 /// @brief Get a string that represents the calculation of the value of this
127 /// This is for display purposes only.
128 error_code
getValueString(SmallVectorImpl
<char> &Result
) const;
130 DataRefImpl
getRawDataRefImpl() const;
132 typedef content_iterator
<RelocationRef
> relocation_iterator
;
134 /// SectionRef - This is a value type class that represents a single section in
135 /// the list of sections in the object file.
137 friend class SymbolRef
;
138 DataRefImpl SectionPimpl
;
139 const ObjectFile
*OwningObject
;
142 SectionRef() : OwningObject(NULL
) { }
144 SectionRef(DataRefImpl SectionP
, const ObjectFile
*Owner
);
146 bool operator==(const SectionRef
&Other
) const;
147 bool operator <(const SectionRef
&Other
) const;
149 error_code
getNext(SectionRef
&Result
) const;
151 error_code
getName(StringRef
&Result
) const;
152 error_code
getAddress(uint64_t &Result
) const;
153 error_code
getSize(uint64_t &Result
) const;
154 error_code
getContents(StringRef
&Result
) const;
156 /// @brief Get the alignment of this section as the actual value (not log 2).
157 error_code
getAlignment(uint64_t &Result
) const;
159 // FIXME: Move to the normalization layer when it's created.
160 error_code
isText(bool &Result
) const;
161 error_code
isData(bool &Result
) const;
162 error_code
isBSS(bool &Result
) const;
163 error_code
isRequiredForExecution(bool &Result
) const;
164 error_code
isVirtual(bool &Result
) const;
165 error_code
isZeroInit(bool &Result
) const;
167 error_code
containsSymbol(SymbolRef S
, bool &Result
) const;
169 relocation_iterator
begin_relocations() const;
170 relocation_iterator
end_relocations() const;
172 DataRefImpl
getRawDataRefImpl() const;
174 typedef content_iterator
<SectionRef
> section_iterator
;
176 /// SymbolRef - This is a value type class that represents a single symbol in
177 /// the list of symbols in the object file.
179 friend class SectionRef
;
180 DataRefImpl SymbolPimpl
;
181 const ObjectFile
*OwningObject
;
184 SymbolRef() : OwningObject(NULL
) { }
187 ST_Unknown
, // Type not specified
197 SF_Undefined
= 1U << 0, // Symbol is defined in another object file
198 SF_Global
= 1U << 1, // Global symbol
199 SF_Weak
= 1U << 2, // Weak symbol
200 SF_Absolute
= 1U << 3, // Absolute symbol
201 SF_ThreadLocal
= 1U << 4, // Thread local symbol
202 SF_Common
= 1U << 5, // Symbol has common linkage
203 SF_FormatSpecific
= 1U << 31 // Specific to the object file format
204 // (e.g. section symbols)
207 SymbolRef(DataRefImpl SymbolP
, const ObjectFile
*Owner
);
209 bool operator==(const SymbolRef
&Other
) const;
210 bool operator <(const SymbolRef
&Other
) const;
212 error_code
getNext(SymbolRef
&Result
) const;
214 error_code
getName(StringRef
&Result
) const;
215 /// Returns the symbol virtual address (i.e. address at which it will be
217 error_code
getAddress(uint64_t &Result
) const;
218 error_code
getFileOffset(uint64_t &Result
) const;
219 error_code
getSize(uint64_t &Result
) const;
220 error_code
getType(SymbolRef::Type
&Result
) const;
222 /// Returns the ascii char that should be displayed in a symbol table dump via
223 /// nm for this symbol.
224 error_code
getNMTypeChar(char &Result
) const;
226 /// Get symbol flags (bitwise OR of SymbolRef::Flags)
227 error_code
getFlags(uint32_t &Result
) const;
229 /// @brief Return true for common symbols such as uninitialized globals
230 error_code
isCommon(bool &Result
) const;
232 /// @brief Get section this symbol is defined in reference to. Result is
233 /// end_sections() if it is undefined or is an absolute symbol.
234 error_code
getSection(section_iterator
&Result
) const;
236 DataRefImpl
getRawDataRefImpl() const;
238 typedef content_iterator
<SymbolRef
> symbol_iterator
;
240 /// LibraryRef - This is a value type class that represents a single library in
241 /// the list of libraries needed by a shared or dynamic object.
243 friend class SectionRef
;
244 DataRefImpl LibraryPimpl
;
245 const ObjectFile
*OwningObject
;
248 LibraryRef() : OwningObject(NULL
) { }
250 LibraryRef(DataRefImpl LibraryP
, const ObjectFile
*Owner
);
252 bool operator==(const LibraryRef
&Other
) const;
253 bool operator <(const LibraryRef
&Other
) const;
255 error_code
getNext(LibraryRef
&Result
) const;
257 // Get the path to this library, as stored in the object file.
258 error_code
getPath(StringRef
&Result
) const;
260 DataRefImpl
getRawDataRefImpl() const;
262 typedef content_iterator
<LibraryRef
> library_iterator
;
264 const uint64_t UnknownAddressOrSize
= ~0ULL;
266 /// ObjectFile - This class is the base class for all object file types.
267 /// Concrete instances of this object are created by createObjectFile, which
268 /// figure out which type to create.
269 class ObjectFile
: public Binary
{
270 virtual void anchor();
271 ObjectFile() LLVM_DELETED_FUNCTION
;
272 ObjectFile(const ObjectFile
&other
) LLVM_DELETED_FUNCTION
;
275 ObjectFile(unsigned int Type
, MemoryBuffer
*source
, error_code
&ec
);
277 const uint8_t *base() const {
278 return reinterpret_cast<const uint8_t *>(Data
->getBufferStart());
281 // These functions are for SymbolRef to call internally. The main goal of
282 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
283 // entry in the memory mapped object file. SymbolPimpl cannot contain any
284 // virtual functions because then it could not point into the memory mapped
287 // Implementations assume that the DataRefImpl is valid and has not been
288 // modified externally. It's UB otherwise.
289 friend class SymbolRef
;
290 virtual error_code
getSymbolNext(DataRefImpl Symb
, SymbolRef
&Res
) const = 0;
291 virtual error_code
getSymbolName(DataRefImpl Symb
, StringRef
&Res
) const = 0;
292 virtual error_code
getSymbolAddress(DataRefImpl Symb
, uint64_t &Res
) const =0;
293 virtual error_code
getSymbolFileOffset(DataRefImpl Symb
, uint64_t &Res
) const =0;
294 virtual error_code
getSymbolSize(DataRefImpl Symb
, uint64_t &Res
) const = 0;
295 virtual error_code
getSymbolType(DataRefImpl Symb
,
296 SymbolRef::Type
&Res
) const = 0;
297 virtual error_code
getSymbolNMTypeChar(DataRefImpl Symb
, char &Res
) const = 0;
298 virtual error_code
getSymbolFlags(DataRefImpl Symb
,
299 uint32_t &Res
) const = 0;
300 virtual error_code
getSymbolSection(DataRefImpl Symb
,
301 section_iterator
&Res
) const = 0;
303 // Same as above for SectionRef.
304 friend class SectionRef
;
305 virtual error_code
getSectionNext(DataRefImpl Sec
, SectionRef
&Res
) const = 0;
306 virtual error_code
getSectionName(DataRefImpl Sec
, StringRef
&Res
) const = 0;
307 virtual error_code
getSectionAddress(DataRefImpl Sec
, uint64_t &Res
) const =0;
308 virtual error_code
getSectionSize(DataRefImpl Sec
, uint64_t &Res
) const = 0;
309 virtual error_code
getSectionContents(DataRefImpl Sec
, StringRef
&Res
)const=0;
310 virtual error_code
getSectionAlignment(DataRefImpl Sec
, uint64_t &Res
)const=0;
311 virtual error_code
isSectionText(DataRefImpl Sec
, bool &Res
) const = 0;
312 virtual error_code
isSectionData(DataRefImpl Sec
, bool &Res
) const = 0;
313 virtual error_code
isSectionBSS(DataRefImpl Sec
, bool &Res
) const = 0;
314 virtual error_code
isSectionRequiredForExecution(DataRefImpl Sec
,
315 bool &Res
) const = 0;
316 // A section is 'virtual' if its contents aren't present in the object image.
317 virtual error_code
isSectionVirtual(DataRefImpl Sec
, bool &Res
) const = 0;
318 virtual error_code
isSectionZeroInit(DataRefImpl Sec
, bool &Res
) const = 0;
319 virtual error_code
sectionContainsSymbol(DataRefImpl Sec
, DataRefImpl Symb
,
320 bool &Result
) const = 0;
321 virtual relocation_iterator
getSectionRelBegin(DataRefImpl Sec
) const = 0;
322 virtual relocation_iterator
getSectionRelEnd(DataRefImpl Sec
) const = 0;
325 // Same as above for RelocationRef.
326 friend class RelocationRef
;
327 virtual error_code
getRelocationNext(DataRefImpl Rel
,
328 RelocationRef
&Res
) const = 0;
329 virtual error_code
getRelocationAddress(DataRefImpl Rel
,
330 uint64_t &Res
) const =0;
331 virtual error_code
getRelocationOffset(DataRefImpl Rel
,
332 uint64_t &Res
) const =0;
333 virtual error_code
getRelocationSymbol(DataRefImpl Rel
,
334 SymbolRef
&Res
) const = 0;
335 virtual error_code
getRelocationType(DataRefImpl Rel
,
336 uint64_t &Res
) const = 0;
337 virtual error_code
getRelocationTypeName(DataRefImpl Rel
,
338 SmallVectorImpl
<char> &Result
) const = 0;
339 virtual error_code
getRelocationAdditionalInfo(DataRefImpl Rel
,
340 int64_t &Res
) const = 0;
341 virtual error_code
getRelocationValueString(DataRefImpl Rel
,
342 SmallVectorImpl
<char> &Result
) const = 0;
343 virtual error_code
getRelocationHidden(DataRefImpl Rel
, bool &Result
) const {
345 return object_error::success
;
348 // Same for LibraryRef
349 friend class LibraryRef
;
350 virtual error_code
getLibraryNext(DataRefImpl Lib
, LibraryRef
&Res
) const = 0;
351 virtual error_code
getLibraryPath(DataRefImpl Lib
, StringRef
&Res
) const = 0;
355 virtual symbol_iterator
begin_symbols() const = 0;
356 virtual symbol_iterator
end_symbols() const = 0;
358 virtual symbol_iterator
begin_dynamic_symbols() const = 0;
359 virtual symbol_iterator
end_dynamic_symbols() const = 0;
361 virtual section_iterator
begin_sections() const = 0;
362 virtual section_iterator
end_sections() const = 0;
364 virtual library_iterator
begin_libraries_needed() const = 0;
365 virtual library_iterator
end_libraries_needed() const = 0;
367 /// @brief The number of bytes used to represent an address in this object
369 virtual uint8_t getBytesInAddress() const = 0;
371 virtual StringRef
getFileFormatName() const = 0;
372 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
374 /// For shared objects, returns the name which this object should be
375 /// loaded from at runtime. This corresponds to DT_SONAME on ELF and
376 /// LC_ID_DYLIB (install name) on MachO.
377 virtual StringRef
getLoadName() const = 0;
379 /// @returns Pointer to ObjectFile subclass to handle this type of object.
380 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
382 /// @brief Create ObjectFile from path.
383 static ObjectFile
*createObjectFile(StringRef ObjectPath
);
384 static ObjectFile
*createObjectFile(MemoryBuffer
*Object
);
386 static inline bool classof(const Binary
*v
) {
387 return v
->isObject();
389 static inline bool classof(const ObjectFile
*v
) { return true; }
392 static ObjectFile
*createCOFFObjectFile(MemoryBuffer
*Object
);
393 static ObjectFile
*createELFObjectFile(MemoryBuffer
*Object
);
394 static ObjectFile
*createMachOObjectFile(MemoryBuffer
*Object
);
397 // Inline function definitions.
398 inline SymbolRef::SymbolRef(DataRefImpl SymbolP
, const ObjectFile
*Owner
)
399 : SymbolPimpl(SymbolP
)
400 , OwningObject(Owner
) {}
402 inline bool SymbolRef::operator==(const SymbolRef
&Other
) const {
403 return SymbolPimpl
== Other
.SymbolPimpl
;
406 inline bool SymbolRef::operator <(const SymbolRef
&Other
) const {
407 return SymbolPimpl
< Other
.SymbolPimpl
;
410 inline error_code
SymbolRef::getNext(SymbolRef
&Result
) const {
411 return OwningObject
->getSymbolNext(SymbolPimpl
, Result
);
414 inline error_code
SymbolRef::getName(StringRef
&Result
) const {
415 return OwningObject
->getSymbolName(SymbolPimpl
, Result
);
418 inline error_code
SymbolRef::getAddress(uint64_t &Result
) const {
419 return OwningObject
->getSymbolAddress(SymbolPimpl
, Result
);
422 inline error_code
SymbolRef::getFileOffset(uint64_t &Result
) const {
423 return OwningObject
->getSymbolFileOffset(SymbolPimpl
, Result
);
426 inline error_code
SymbolRef::getSize(uint64_t &Result
) const {
427 return OwningObject
->getSymbolSize(SymbolPimpl
, Result
);
430 inline error_code
SymbolRef::getNMTypeChar(char &Result
) const {
431 return OwningObject
->getSymbolNMTypeChar(SymbolPimpl
, Result
);
434 inline error_code
SymbolRef::getFlags(uint32_t &Result
) const {
435 return OwningObject
->getSymbolFlags(SymbolPimpl
, Result
);
438 inline error_code
SymbolRef::getSection(section_iterator
&Result
) const {
439 return OwningObject
->getSymbolSection(SymbolPimpl
, Result
);
442 inline error_code
SymbolRef::getType(SymbolRef::Type
&Result
) const {
443 return OwningObject
->getSymbolType(SymbolPimpl
, Result
);
446 inline DataRefImpl
SymbolRef::getRawDataRefImpl() const {
452 inline SectionRef::SectionRef(DataRefImpl SectionP
,
453 const ObjectFile
*Owner
)
454 : SectionPimpl(SectionP
)
455 , OwningObject(Owner
) {}
457 inline bool SectionRef::operator==(const SectionRef
&Other
) const {
458 return SectionPimpl
== Other
.SectionPimpl
;
461 inline bool SectionRef::operator <(const SectionRef
&Other
) const {
462 return SectionPimpl
< Other
.SectionPimpl
;
465 inline error_code
SectionRef::getNext(SectionRef
&Result
) const {
466 return OwningObject
->getSectionNext(SectionPimpl
, Result
);
469 inline error_code
SectionRef::getName(StringRef
&Result
) const {
470 return OwningObject
->getSectionName(SectionPimpl
, Result
);
473 inline error_code
SectionRef::getAddress(uint64_t &Result
) const {
474 return OwningObject
->getSectionAddress(SectionPimpl
, Result
);
477 inline error_code
SectionRef::getSize(uint64_t &Result
) const {
478 return OwningObject
->getSectionSize(SectionPimpl
, Result
);
481 inline error_code
SectionRef::getContents(StringRef
&Result
) const {
482 return OwningObject
->getSectionContents(SectionPimpl
, Result
);
485 inline error_code
SectionRef::getAlignment(uint64_t &Result
) const {
486 return OwningObject
->getSectionAlignment(SectionPimpl
, Result
);
489 inline error_code
SectionRef::isText(bool &Result
) const {
490 return OwningObject
->isSectionText(SectionPimpl
, Result
);
493 inline error_code
SectionRef::isData(bool &Result
) const {
494 return OwningObject
->isSectionData(SectionPimpl
, Result
);
497 inline error_code
SectionRef::isBSS(bool &Result
) const {
498 return OwningObject
->isSectionBSS(SectionPimpl
, Result
);
501 inline error_code
SectionRef::isRequiredForExecution(bool &Result
) const {
502 return OwningObject
->isSectionRequiredForExecution(SectionPimpl
, Result
);
505 inline error_code
SectionRef::isVirtual(bool &Result
) const {
506 return OwningObject
->isSectionVirtual(SectionPimpl
, Result
);
509 inline error_code
SectionRef::isZeroInit(bool &Result
) const {
510 return OwningObject
->isSectionZeroInit(SectionPimpl
, Result
);
513 inline error_code
SectionRef::containsSymbol(SymbolRef S
, bool &Result
) const {
514 return OwningObject
->sectionContainsSymbol(SectionPimpl
, S
.SymbolPimpl
,
518 inline relocation_iterator
SectionRef::begin_relocations() const {
519 return OwningObject
->getSectionRelBegin(SectionPimpl
);
522 inline relocation_iterator
SectionRef::end_relocations() const {
523 return OwningObject
->getSectionRelEnd(SectionPimpl
);
526 inline DataRefImpl
SectionRef::getRawDataRefImpl() const {
531 inline RelocationRef::RelocationRef(DataRefImpl RelocationP
,
532 const ObjectFile
*Owner
)
533 : RelocationPimpl(RelocationP
)
534 , OwningObject(Owner
) {}
536 inline bool RelocationRef::operator==(const RelocationRef
&Other
) const {
537 return RelocationPimpl
== Other
.RelocationPimpl
;
540 inline error_code
RelocationRef::getNext(RelocationRef
&Result
) const {
541 return OwningObject
->getRelocationNext(RelocationPimpl
, Result
);
544 inline error_code
RelocationRef::getAddress(uint64_t &Result
) const {
545 return OwningObject
->getRelocationAddress(RelocationPimpl
, Result
);
548 inline error_code
RelocationRef::getOffset(uint64_t &Result
) const {
549 return OwningObject
->getRelocationOffset(RelocationPimpl
, Result
);
552 inline error_code
RelocationRef::getSymbol(SymbolRef
&Result
) const {
553 return OwningObject
->getRelocationSymbol(RelocationPimpl
, Result
);
556 inline error_code
RelocationRef::getType(uint64_t &Result
) const {
557 return OwningObject
->getRelocationType(RelocationPimpl
, Result
);
560 inline error_code
RelocationRef::getTypeName(SmallVectorImpl
<char> &Result
)
562 return OwningObject
->getRelocationTypeName(RelocationPimpl
, Result
);
565 inline error_code
RelocationRef::getAdditionalInfo(int64_t &Result
) const {
566 return OwningObject
->getRelocationAdditionalInfo(RelocationPimpl
, Result
);
569 inline error_code
RelocationRef::getValueString(SmallVectorImpl
<char> &Result
)
571 return OwningObject
->getRelocationValueString(RelocationPimpl
, Result
);
574 inline error_code
RelocationRef::getHidden(bool &Result
) const {
575 return OwningObject
->getRelocationHidden(RelocationPimpl
, Result
);
578 inline DataRefImpl
RelocationRef::getRawDataRefImpl() const {
579 return RelocationPimpl
;
582 // Inline function definitions.
583 inline LibraryRef::LibraryRef(DataRefImpl LibraryP
, const ObjectFile
*Owner
)
584 : LibraryPimpl(LibraryP
)
585 , OwningObject(Owner
) {}
587 inline bool LibraryRef::operator==(const LibraryRef
&Other
) const {
588 return LibraryPimpl
== Other
.LibraryPimpl
;
591 inline bool LibraryRef::operator <(const LibraryRef
&Other
) const {
592 return LibraryPimpl
< Other
.LibraryPimpl
;
595 inline error_code
LibraryRef::getNext(LibraryRef
&Result
) const {
596 return OwningObject
->getLibraryNext(LibraryPimpl
, Result
);
599 inline error_code
LibraryRef::getPath(StringRef
&Result
) const {
600 return OwningObject
->getLibraryPath(LibraryPimpl
, Result
);
603 } // end namespace object
604 } // end namespace llvm