1 //===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file declares a file format independent ObjectFile class.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_OBJECT_OBJECTFILE_H
14 #define LLVM_OBJECT_OBJECTFILE_H
16 #include "llvm/ADT/DenseMapInfo.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/ADT/Triple.h"
19 #include "llvm/ADT/iterator_range.h"
20 #include "llvm/BinaryFormat/Magic.h"
21 #include "llvm/MC/SubtargetFeature.h"
22 #include "llvm/Object/Binary.h"
23 #include "llvm/Object/Error.h"
24 #include "llvm/Object/SymbolicFile.h"
25 #include "llvm/Support/Casting.h"
26 #include "llvm/Support/Error.h"
27 #include "llvm/Support/FileSystem.h"
28 #include "llvm/Support/MemoryBuffer.h"
32 #include <system_error>
36 class ARMAttributeParser
;
41 class MachOObjectFile
;
45 class symbol_iterator
;
48 using section_iterator
= content_iterator
<SectionRef
>;
50 /// This is a value type class that represents a single relocation in the list
51 /// of relocations in the object file.
53 DataRefImpl RelocationPimpl
;
54 const ObjectFile
*OwningObject
= nullptr;
57 RelocationRef() = default;
58 RelocationRef(DataRefImpl RelocationP
, const ObjectFile
*Owner
);
60 bool operator==(const RelocationRef
&Other
) const;
64 uint64_t getOffset() const;
65 symbol_iterator
getSymbol() const;
66 uint64_t getType() const;
68 /// Get a string that represents the type of this relocation.
70 /// This is for display purposes only.
71 void getTypeName(SmallVectorImpl
<char> &Result
) const;
73 DataRefImpl
getRawDataRefImpl() const;
74 const ObjectFile
*getObject() const;
77 using relocation_iterator
= content_iterator
<RelocationRef
>;
79 /// This is a value type class that represents a single section in the list of
80 /// sections in the object file.
82 friend class SymbolRef
;
84 DataRefImpl SectionPimpl
;
85 const ObjectFile
*OwningObject
= nullptr;
88 SectionRef() = default;
89 SectionRef(DataRefImpl SectionP
, const ObjectFile
*Owner
);
91 bool operator==(const SectionRef
&Other
) const;
92 bool operator!=(const SectionRef
&Other
) const;
93 bool operator<(const SectionRef
&Other
) const;
97 Expected
<StringRef
> getName() const;
98 uint64_t getAddress() const;
99 uint64_t getIndex() const;
100 uint64_t getSize() const;
101 Expected
<StringRef
> getContents() const;
103 /// Get the alignment of this section as the actual value (not log 2).
104 uint64_t getAlignment() const;
106 bool isCompressed() const;
107 /// Whether this section contains instructions.
109 /// Whether this section contains data, not instructions.
111 /// Whether this section contains BSS uninitialized data.
113 bool isVirtual() const;
114 bool isBitcode() const;
115 bool isStripped() const;
117 /// Whether this section will be placed in the text segment, according to the
118 /// Berkeley size format. This is true if the section is allocatable, and
119 /// contains either code or readonly data.
120 bool isBerkeleyText() const;
121 /// Whether this section will be placed in the data segment, according to the
122 /// Berkeley size format. This is true if the section is allocatable and
123 /// contains data (e.g. PROGBITS), but is not text.
124 bool isBerkeleyData() const;
126 bool containsSymbol(SymbolRef S
) const;
128 relocation_iterator
relocation_begin() const;
129 relocation_iterator
relocation_end() const;
130 iterator_range
<relocation_iterator
> relocations() const {
131 return make_range(relocation_begin(), relocation_end());
133 section_iterator
getRelocatedSection() const;
135 DataRefImpl
getRawDataRefImpl() const;
136 const ObjectFile
*getObject() const;
139 struct SectionedAddress
{
140 // TODO: constructors could be removed when C++14 would be adopted.
141 SectionedAddress() {}
142 SectionedAddress(uint64_t Addr
, uint64_t SectIdx
)
143 : Address(Addr
), SectionIndex(SectIdx
) {}
145 const static uint64_t UndefSection
= UINT64_MAX
;
147 uint64_t Address
= 0;
148 uint64_t SectionIndex
= UndefSection
;
151 inline bool operator<(const SectionedAddress
&LHS
,
152 const SectionedAddress
&RHS
) {
153 return std::tie(LHS
.SectionIndex
, LHS
.Address
) <
154 std::tie(RHS
.SectionIndex
, RHS
.Address
);
157 inline bool operator==(const SectionedAddress
&LHS
,
158 const SectionedAddress
&RHS
) {
159 return std::tie(LHS
.SectionIndex
, LHS
.Address
) ==
160 std::tie(RHS
.SectionIndex
, RHS
.Address
);
163 /// This is a value type class that represents a single symbol in the list of
164 /// symbols in the object file.
165 class SymbolRef
: public BasicSymbolRef
{
166 friend class SectionRef
;
170 ST_Unknown
, // Type not specified
178 SymbolRef() = default;
179 SymbolRef(DataRefImpl SymbolP
, const ObjectFile
*Owner
);
180 SymbolRef(const BasicSymbolRef
&B
) : BasicSymbolRef(B
) {
181 assert(isa
<ObjectFile
>(BasicSymbolRef::getObject()));
184 Expected
<StringRef
> getName() const;
185 /// Returns the symbol virtual address (i.e. address at which it will be
187 Expected
<uint64_t> getAddress() const;
189 /// Return the value of the symbol depending on the object this can be an
190 /// offset or a virtual address.
191 uint64_t getValue() const;
193 /// Get the alignment of this symbol as the actual value (not log 2).
194 uint32_t getAlignment() const;
195 uint64_t getCommonSize() const;
196 Expected
<SymbolRef::Type
> getType() const;
198 /// Get section this symbol is defined in reference to. Result is
199 /// end_sections() if it is undefined or is an absolute symbol.
200 Expected
<section_iterator
> getSection() const;
202 const ObjectFile
*getObject() const;
205 class symbol_iterator
: public basic_symbol_iterator
{
207 symbol_iterator(SymbolRef Sym
) : basic_symbol_iterator(Sym
) {}
208 symbol_iterator(const basic_symbol_iterator
&B
)
209 : basic_symbol_iterator(SymbolRef(B
->getRawDataRefImpl(),
210 cast
<ObjectFile
>(B
->getObject()))) {}
212 const SymbolRef
*operator->() const {
213 const BasicSymbolRef
&P
= basic_symbol_iterator::operator *();
214 return static_cast<const SymbolRef
*>(&P
);
217 const SymbolRef
&operator*() const {
218 const BasicSymbolRef
&P
= basic_symbol_iterator::operator *();
219 return static_cast<const SymbolRef
&>(P
);
223 /// This class is the base class for all object file types. Concrete instances
224 /// of this object are created by createObjectFile, which figures out which type
226 class ObjectFile
: public SymbolicFile
{
227 virtual void anchor();
230 ObjectFile(unsigned int Type
, MemoryBufferRef Source
);
232 const uint8_t *base() const {
233 return reinterpret_cast<const uint8_t *>(Data
.getBufferStart());
236 // These functions are for SymbolRef to call internally. The main goal of
237 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
238 // entry in the memory mapped object file. SymbolPimpl cannot contain any
239 // virtual functions because then it could not point into the memory mapped
242 // Implementations assume that the DataRefImpl is valid and has not been
243 // modified externally. It's UB otherwise.
244 friend class SymbolRef
;
246 virtual Expected
<StringRef
> getSymbolName(DataRefImpl Symb
) const = 0;
247 Error
printSymbolName(raw_ostream
&OS
,
248 DataRefImpl Symb
) const override
;
249 virtual Expected
<uint64_t> getSymbolAddress(DataRefImpl Symb
) const = 0;
250 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb
) const = 0;
251 virtual uint32_t getSymbolAlignment(DataRefImpl Symb
) const;
252 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb
) const = 0;
253 virtual Expected
<SymbolRef::Type
> getSymbolType(DataRefImpl Symb
) const = 0;
254 virtual Expected
<section_iterator
>
255 getSymbolSection(DataRefImpl Symb
) const = 0;
257 // Same as above for SectionRef.
258 friend class SectionRef
;
260 virtual void moveSectionNext(DataRefImpl
&Sec
) const = 0;
261 virtual Expected
<StringRef
> getSectionName(DataRefImpl Sec
) const = 0;
262 virtual uint64_t getSectionAddress(DataRefImpl Sec
) const = 0;
263 virtual uint64_t getSectionIndex(DataRefImpl Sec
) const = 0;
264 virtual uint64_t getSectionSize(DataRefImpl Sec
) const = 0;
265 virtual Expected
<ArrayRef
<uint8_t>>
266 getSectionContents(DataRefImpl Sec
) const = 0;
267 virtual uint64_t getSectionAlignment(DataRefImpl Sec
) const = 0;
268 virtual bool isSectionCompressed(DataRefImpl Sec
) const = 0;
269 virtual bool isSectionText(DataRefImpl Sec
) const = 0;
270 virtual bool isSectionData(DataRefImpl Sec
) const = 0;
271 virtual bool isSectionBSS(DataRefImpl Sec
) const = 0;
272 // A section is 'virtual' if its contents aren't present in the object image.
273 virtual bool isSectionVirtual(DataRefImpl Sec
) const = 0;
274 virtual bool isSectionBitcode(DataRefImpl Sec
) const;
275 virtual bool isSectionStripped(DataRefImpl Sec
) const;
276 virtual bool isBerkeleyText(DataRefImpl Sec
) const;
277 virtual bool isBerkeleyData(DataRefImpl Sec
) const;
278 virtual relocation_iterator
section_rel_begin(DataRefImpl Sec
) const = 0;
279 virtual relocation_iterator
section_rel_end(DataRefImpl Sec
) const = 0;
280 virtual section_iterator
getRelocatedSection(DataRefImpl Sec
) const;
282 // Same as above for RelocationRef.
283 friend class RelocationRef
;
284 virtual void moveRelocationNext(DataRefImpl
&Rel
) const = 0;
285 virtual uint64_t getRelocationOffset(DataRefImpl Rel
) const = 0;
286 virtual symbol_iterator
getRelocationSymbol(DataRefImpl Rel
) const = 0;
287 virtual uint64_t getRelocationType(DataRefImpl Rel
) const = 0;
288 virtual void getRelocationTypeName(DataRefImpl Rel
,
289 SmallVectorImpl
<char> &Result
) const = 0;
291 uint64_t getSymbolValue(DataRefImpl Symb
) const;
294 ObjectFile() = delete;
295 ObjectFile(const ObjectFile
&other
) = delete;
297 uint64_t getCommonSymbolSize(DataRefImpl Symb
) const {
298 assert(getSymbolFlags(Symb
) & SymbolRef::SF_Common
);
299 return getCommonSymbolSizeImpl(Symb
);
302 virtual std::vector
<SectionRef
> dynamic_relocation_sections() const {
303 return std::vector
<SectionRef
>();
306 using symbol_iterator_range
= iterator_range
<symbol_iterator
>;
307 symbol_iterator_range
symbols() const {
308 return symbol_iterator_range(symbol_begin(), symbol_end());
311 virtual section_iterator
section_begin() const = 0;
312 virtual section_iterator
section_end() const = 0;
314 using section_iterator_range
= iterator_range
<section_iterator
>;
315 section_iterator_range
sections() const {
316 return section_iterator_range(section_begin(), section_end());
319 /// The number of bytes used to represent an address in this object
321 virtual uint8_t getBytesInAddress() const = 0;
323 virtual StringRef
getFileFormatName() const = 0;
324 virtual Triple::ArchType
getArch() const = 0;
325 virtual SubtargetFeatures
getFeatures() const = 0;
326 virtual void setARMSubArch(Triple
&TheTriple
) const { }
327 virtual Expected
<uint64_t> getStartAddress() const {
328 return errorCodeToError(object_error::parse_failed
);
331 /// Create a triple from the data in this object file.
332 Triple
makeTriple() const;
334 /// Maps a debug section name to a standard DWARF section name.
335 virtual StringRef
mapDebugSectionName(StringRef Name
) const { return Name
; }
337 /// True if this is a relocatable object (.o/.obj).
338 virtual bool isRelocatableObject() const = 0;
340 /// @returns Pointer to ObjectFile subclass to handle this type of object.
341 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
343 /// Create ObjectFile from path.
344 static Expected
<OwningBinary
<ObjectFile
>>
345 createObjectFile(StringRef ObjectPath
);
347 static Expected
<std::unique_ptr
<ObjectFile
>>
348 createObjectFile(MemoryBufferRef Object
, llvm::file_magic Type
);
349 static Expected
<std::unique_ptr
<ObjectFile
>>
350 createObjectFile(MemoryBufferRef Object
) {
351 return createObjectFile(Object
, llvm::file_magic::unknown
);
354 static bool classof(const Binary
*v
) {
355 return v
->isObject();
358 static Expected
<std::unique_ptr
<COFFObjectFile
>>
359 createCOFFObjectFile(MemoryBufferRef Object
);
361 static Expected
<std::unique_ptr
<ObjectFile
>>
362 createXCOFFObjectFile(MemoryBufferRef Object
, unsigned FileType
);
364 static Expected
<std::unique_ptr
<ObjectFile
>>
365 createELFObjectFile(MemoryBufferRef Object
);
367 static Expected
<std::unique_ptr
<MachOObjectFile
>>
368 createMachOObjectFile(MemoryBufferRef Object
,
369 uint32_t UniversalCputype
= 0,
370 uint32_t UniversalIndex
= 0);
372 static Expected
<std::unique_ptr
<WasmObjectFile
>>
373 createWasmObjectFile(MemoryBufferRef Object
);
376 // Inline function definitions.
377 inline SymbolRef::SymbolRef(DataRefImpl SymbolP
, const ObjectFile
*Owner
)
378 : BasicSymbolRef(SymbolP
, Owner
) {}
380 inline Expected
<StringRef
> SymbolRef::getName() const {
381 return getObject()->getSymbolName(getRawDataRefImpl());
384 inline Expected
<uint64_t> SymbolRef::getAddress() const {
385 return getObject()->getSymbolAddress(getRawDataRefImpl());
388 inline uint64_t SymbolRef::getValue() const {
389 return getObject()->getSymbolValue(getRawDataRefImpl());
392 inline uint32_t SymbolRef::getAlignment() const {
393 return getObject()->getSymbolAlignment(getRawDataRefImpl());
396 inline uint64_t SymbolRef::getCommonSize() const {
397 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
400 inline Expected
<section_iterator
> SymbolRef::getSection() const {
401 return getObject()->getSymbolSection(getRawDataRefImpl());
404 inline Expected
<SymbolRef::Type
> SymbolRef::getType() const {
405 return getObject()->getSymbolType(getRawDataRefImpl());
408 inline const ObjectFile
*SymbolRef::getObject() const {
409 const SymbolicFile
*O
= BasicSymbolRef::getObject();
410 return cast
<ObjectFile
>(O
);
414 inline SectionRef::SectionRef(DataRefImpl SectionP
,
415 const ObjectFile
*Owner
)
416 : SectionPimpl(SectionP
)
417 , OwningObject(Owner
) {}
419 inline bool SectionRef::operator==(const SectionRef
&Other
) const {
420 return OwningObject
== Other
.OwningObject
&&
421 SectionPimpl
== Other
.SectionPimpl
;
424 inline bool SectionRef::operator!=(const SectionRef
&Other
) const {
425 return !(*this == Other
);
428 inline bool SectionRef::operator<(const SectionRef
&Other
) const {
429 assert(OwningObject
== Other
.OwningObject
);
430 return SectionPimpl
< Other
.SectionPimpl
;
433 inline void SectionRef::moveNext() {
434 return OwningObject
->moveSectionNext(SectionPimpl
);
437 inline Expected
<StringRef
> SectionRef::getName() const {
438 return OwningObject
->getSectionName(SectionPimpl
);
441 inline uint64_t SectionRef::getAddress() const {
442 return OwningObject
->getSectionAddress(SectionPimpl
);
445 inline uint64_t SectionRef::getIndex() const {
446 return OwningObject
->getSectionIndex(SectionPimpl
);
449 inline uint64_t SectionRef::getSize() const {
450 return OwningObject
->getSectionSize(SectionPimpl
);
453 inline Expected
<StringRef
> SectionRef::getContents() const {
454 Expected
<ArrayRef
<uint8_t>> Res
=
455 OwningObject
->getSectionContents(SectionPimpl
);
457 return Res
.takeError();
458 return StringRef(reinterpret_cast<const char *>(Res
->data()), Res
->size());
461 inline uint64_t SectionRef::getAlignment() const {
462 return OwningObject
->getSectionAlignment(SectionPimpl
);
465 inline bool SectionRef::isCompressed() const {
466 return OwningObject
->isSectionCompressed(SectionPimpl
);
469 inline bool SectionRef::isText() const {
470 return OwningObject
->isSectionText(SectionPimpl
);
473 inline bool SectionRef::isData() const {
474 return OwningObject
->isSectionData(SectionPimpl
);
477 inline bool SectionRef::isBSS() const {
478 return OwningObject
->isSectionBSS(SectionPimpl
);
481 inline bool SectionRef::isVirtual() const {
482 return OwningObject
->isSectionVirtual(SectionPimpl
);
485 inline bool SectionRef::isBitcode() const {
486 return OwningObject
->isSectionBitcode(SectionPimpl
);
489 inline bool SectionRef::isStripped() const {
490 return OwningObject
->isSectionStripped(SectionPimpl
);
493 inline bool SectionRef::isBerkeleyText() const {
494 return OwningObject
->isBerkeleyText(SectionPimpl
);
497 inline bool SectionRef::isBerkeleyData() const {
498 return OwningObject
->isBerkeleyData(SectionPimpl
);
501 inline relocation_iterator
SectionRef::relocation_begin() const {
502 return OwningObject
->section_rel_begin(SectionPimpl
);
505 inline relocation_iterator
SectionRef::relocation_end() const {
506 return OwningObject
->section_rel_end(SectionPimpl
);
509 inline section_iterator
SectionRef::getRelocatedSection() const {
510 return OwningObject
->getRelocatedSection(SectionPimpl
);
513 inline DataRefImpl
SectionRef::getRawDataRefImpl() const {
517 inline const ObjectFile
*SectionRef::getObject() const {
522 inline RelocationRef::RelocationRef(DataRefImpl RelocationP
,
523 const ObjectFile
*Owner
)
524 : RelocationPimpl(RelocationP
)
525 , OwningObject(Owner
) {}
527 inline bool RelocationRef::operator==(const RelocationRef
&Other
) const {
528 return RelocationPimpl
== Other
.RelocationPimpl
;
531 inline void RelocationRef::moveNext() {
532 return OwningObject
->moveRelocationNext(RelocationPimpl
);
535 inline uint64_t RelocationRef::getOffset() const {
536 return OwningObject
->getRelocationOffset(RelocationPimpl
);
539 inline symbol_iterator
RelocationRef::getSymbol() const {
540 return OwningObject
->getRelocationSymbol(RelocationPimpl
);
543 inline uint64_t RelocationRef::getType() const {
544 return OwningObject
->getRelocationType(RelocationPimpl
);
547 inline void RelocationRef::getTypeName(SmallVectorImpl
<char> &Result
) const {
548 return OwningObject
->getRelocationTypeName(RelocationPimpl
, Result
);
551 inline DataRefImpl
RelocationRef::getRawDataRefImpl() const {
552 return RelocationPimpl
;
555 inline const ObjectFile
*RelocationRef::getObject() const {
559 } // end namespace object
561 template <> struct DenseMapInfo
<object::SectionRef
> {
562 static bool isEqual(const object::SectionRef
&A
,
563 const object::SectionRef
&B
) {
566 static object::SectionRef
getEmptyKey() {
567 return object::SectionRef({}, nullptr);
569 static object::SectionRef
getTombstoneKey() {
570 object::DataRefImpl TS
;
571 TS
.p
= (uintptr_t)-1;
572 return object::SectionRef(TS
, nullptr);
574 static unsigned getHashValue(const object::SectionRef
&Sec
) {
575 object::DataRefImpl Raw
= Sec
.getRawDataRefImpl();
576 return hash_combine(Raw
.p
, Raw
.d
.a
, Raw
.d
.b
);
580 } // end namespace llvm
582 #endif // LLVM_OBJECT_OBJECTFILE_H