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/StringRef.h"
17 #include "llvm/ADT/Triple.h"
18 #include "llvm/ADT/iterator_range.h"
19 #include "llvm/BinaryFormat/Magic.h"
20 #include "llvm/MC/SubtargetFeature.h"
21 #include "llvm/Object/Binary.h"
22 #include "llvm/Object/Error.h"
23 #include "llvm/Object/SymbolicFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/Error.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/MemoryBuffer.h"
31 #include <system_error>
35 class ARMAttributeParser
;
40 class MachOObjectFile
;
44 class symbol_iterator
;
47 using section_iterator
= content_iterator
<SectionRef
>;
49 /// This is a value type class that represents a single relocation in the list
50 /// of relocations in the object file.
52 DataRefImpl RelocationPimpl
;
53 const ObjectFile
*OwningObject
= nullptr;
56 RelocationRef() = default;
57 RelocationRef(DataRefImpl RelocationP
, const ObjectFile
*Owner
);
59 bool operator==(const RelocationRef
&Other
) const;
63 uint64_t getOffset() const;
64 symbol_iterator
getSymbol() const;
65 uint64_t getType() const;
67 /// Get a string that represents the type of this relocation.
69 /// This is for display purposes only.
70 void getTypeName(SmallVectorImpl
<char> &Result
) const;
72 DataRefImpl
getRawDataRefImpl() const;
73 const ObjectFile
*getObject() const;
76 using relocation_iterator
= content_iterator
<RelocationRef
>;
78 /// This is a value type class that represents a single section in the list of
79 /// sections in the object file.
81 friend class SymbolRef
;
83 DataRefImpl SectionPimpl
;
84 const ObjectFile
*OwningObject
= nullptr;
87 SectionRef() = default;
88 SectionRef(DataRefImpl SectionP
, const ObjectFile
*Owner
);
90 bool operator==(const SectionRef
&Other
) const;
91 bool operator!=(const SectionRef
&Other
) const;
92 bool operator<(const SectionRef
&Other
) const;
96 std::error_code
getName(StringRef
&Result
) const;
97 uint64_t getAddress() const;
98 uint64_t getIndex() const;
99 uint64_t getSize() const;
100 std::error_code
getContents(StringRef
&Result
) const;
102 /// Get the alignment of this section as the actual value (not log 2).
103 uint64_t getAlignment() const;
105 bool isCompressed() const;
106 /// Whether this section contains instructions.
108 /// Whether this section contains data, not instructions.
110 /// Whether this section contains BSS uninitialized data.
112 bool isVirtual() const;
113 bool isBitcode() const;
114 bool isStripped() const;
116 /// Whether this section will be placed in the text segment, according to the
117 /// Berkeley size format. This is true if the section is allocatable, and
118 /// contains either code or readonly data.
119 bool isBerkeleyText() const;
120 /// Whether this section will be placed in the data segment, according to the
121 /// Berkeley size format. This is true if the section is allocatable and
122 /// contains data (e.g. PROGBITS), but is not text.
123 bool isBerkeleyData() const;
125 bool containsSymbol(SymbolRef S
) const;
127 relocation_iterator
relocation_begin() const;
128 relocation_iterator
relocation_end() const;
129 iterator_range
<relocation_iterator
> relocations() const {
130 return make_range(relocation_begin(), relocation_end());
132 section_iterator
getRelocatedSection() const;
134 DataRefImpl
getRawDataRefImpl() const;
135 const ObjectFile
*getObject() const;
138 /// This is a value type class that represents a single symbol in the list of
139 /// symbols in the object file.
140 class SymbolRef
: public BasicSymbolRef
{
141 friend class SectionRef
;
145 ST_Unknown
, // Type not specified
153 SymbolRef() = default;
154 SymbolRef(DataRefImpl SymbolP
, const ObjectFile
*Owner
);
155 SymbolRef(const BasicSymbolRef
&B
) : BasicSymbolRef(B
) {
156 assert(isa
<ObjectFile
>(BasicSymbolRef::getObject()));
159 Expected
<StringRef
> getName() const;
160 /// Returns the symbol virtual address (i.e. address at which it will be
162 Expected
<uint64_t> getAddress() const;
164 /// Return the value of the symbol depending on the object this can be an
165 /// offset or a virtual address.
166 uint64_t getValue() const;
168 /// Get the alignment of this symbol as the actual value (not log 2).
169 uint32_t getAlignment() const;
170 uint64_t getCommonSize() const;
171 Expected
<SymbolRef::Type
> getType() const;
173 /// Get section this symbol is defined in reference to. Result is
174 /// end_sections() if it is undefined or is an absolute symbol.
175 Expected
<section_iterator
> getSection() const;
177 const ObjectFile
*getObject() const;
180 class symbol_iterator
: public basic_symbol_iterator
{
182 symbol_iterator(SymbolRef Sym
) : basic_symbol_iterator(Sym
) {}
183 symbol_iterator(const basic_symbol_iterator
&B
)
184 : basic_symbol_iterator(SymbolRef(B
->getRawDataRefImpl(),
185 cast
<ObjectFile
>(B
->getObject()))) {}
187 const SymbolRef
*operator->() const {
188 const BasicSymbolRef
&P
= basic_symbol_iterator::operator *();
189 return static_cast<const SymbolRef
*>(&P
);
192 const SymbolRef
&operator*() const {
193 const BasicSymbolRef
&P
= basic_symbol_iterator::operator *();
194 return static_cast<const SymbolRef
&>(P
);
198 /// This class is the base class for all object file types. Concrete instances
199 /// of this object are created by createObjectFile, which figures out which type
201 class ObjectFile
: public SymbolicFile
{
202 virtual void anchor();
205 ObjectFile(unsigned int Type
, MemoryBufferRef Source
);
207 const uint8_t *base() const {
208 return reinterpret_cast<const uint8_t *>(Data
.getBufferStart());
211 // These functions are for SymbolRef to call internally. The main goal of
212 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
213 // entry in the memory mapped object file. SymbolPimpl cannot contain any
214 // virtual functions because then it could not point into the memory mapped
217 // Implementations assume that the DataRefImpl is valid and has not been
218 // modified externally. It's UB otherwise.
219 friend class SymbolRef
;
221 virtual Expected
<StringRef
> getSymbolName(DataRefImpl Symb
) const = 0;
222 std::error_code
printSymbolName(raw_ostream
&OS
,
223 DataRefImpl Symb
) const override
;
224 virtual Expected
<uint64_t> getSymbolAddress(DataRefImpl Symb
) const = 0;
225 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb
) const = 0;
226 virtual uint32_t getSymbolAlignment(DataRefImpl Symb
) const;
227 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb
) const = 0;
228 virtual Expected
<SymbolRef::Type
> getSymbolType(DataRefImpl Symb
) const = 0;
229 virtual Expected
<section_iterator
>
230 getSymbolSection(DataRefImpl Symb
) const = 0;
232 // Same as above for SectionRef.
233 friend class SectionRef
;
235 virtual void moveSectionNext(DataRefImpl
&Sec
) const = 0;
236 virtual std::error_code
getSectionName(DataRefImpl Sec
,
237 StringRef
&Res
) const = 0;
238 virtual uint64_t getSectionAddress(DataRefImpl Sec
) const = 0;
239 virtual uint64_t getSectionIndex(DataRefImpl Sec
) const = 0;
240 virtual uint64_t getSectionSize(DataRefImpl Sec
) const = 0;
241 virtual std::error_code
getSectionContents(DataRefImpl Sec
,
242 StringRef
&Res
) const = 0;
243 virtual uint64_t getSectionAlignment(DataRefImpl Sec
) const = 0;
244 virtual bool isSectionCompressed(DataRefImpl Sec
) const = 0;
245 virtual bool isSectionText(DataRefImpl Sec
) const = 0;
246 virtual bool isSectionData(DataRefImpl Sec
) const = 0;
247 virtual bool isSectionBSS(DataRefImpl Sec
) const = 0;
248 // A section is 'virtual' if its contents aren't present in the object image.
249 virtual bool isSectionVirtual(DataRefImpl Sec
) const = 0;
250 virtual bool isSectionBitcode(DataRefImpl Sec
) const;
251 virtual bool isSectionStripped(DataRefImpl Sec
) const;
252 virtual bool isBerkeleyText(DataRefImpl Sec
) const;
253 virtual bool isBerkeleyData(DataRefImpl Sec
) const;
254 virtual relocation_iterator
section_rel_begin(DataRefImpl Sec
) const = 0;
255 virtual relocation_iterator
section_rel_end(DataRefImpl Sec
) const = 0;
256 virtual section_iterator
getRelocatedSection(DataRefImpl Sec
) const;
258 // Same as above for RelocationRef.
259 friend class RelocationRef
;
260 virtual void moveRelocationNext(DataRefImpl
&Rel
) const = 0;
261 virtual uint64_t getRelocationOffset(DataRefImpl Rel
) const = 0;
262 virtual symbol_iterator
getRelocationSymbol(DataRefImpl Rel
) const = 0;
263 virtual uint64_t getRelocationType(DataRefImpl Rel
) const = 0;
264 virtual void getRelocationTypeName(DataRefImpl Rel
,
265 SmallVectorImpl
<char> &Result
) const = 0;
267 uint64_t getSymbolValue(DataRefImpl Symb
) const;
270 ObjectFile() = delete;
271 ObjectFile(const ObjectFile
&other
) = delete;
273 uint64_t getCommonSymbolSize(DataRefImpl Symb
) const {
274 assert(getSymbolFlags(Symb
) & SymbolRef::SF_Common
);
275 return getCommonSymbolSizeImpl(Symb
);
278 virtual std::vector
<SectionRef
> dynamic_relocation_sections() const {
279 return std::vector
<SectionRef
>();
282 using symbol_iterator_range
= iterator_range
<symbol_iterator
>;
283 symbol_iterator_range
symbols() const {
284 return symbol_iterator_range(symbol_begin(), symbol_end());
287 virtual section_iterator
section_begin() const = 0;
288 virtual section_iterator
section_end() const = 0;
290 using section_iterator_range
= iterator_range
<section_iterator
>;
291 section_iterator_range
sections() const {
292 return section_iterator_range(section_begin(), section_end());
295 /// The number of bytes used to represent an address in this object
297 virtual uint8_t getBytesInAddress() const = 0;
299 virtual StringRef
getFileFormatName() const = 0;
300 virtual Triple::ArchType
getArch() const = 0;
301 virtual SubtargetFeatures
getFeatures() const = 0;
302 virtual void setARMSubArch(Triple
&TheTriple
) const { }
303 virtual Expected
<uint64_t> getStartAddress() const {
304 return errorCodeToError(object_error::parse_failed
);
307 /// Create a triple from the data in this object file.
308 Triple
makeTriple() const;
310 virtual std::error_code
311 getBuildAttributes(ARMAttributeParser
&Attributes
) const {
312 return std::error_code();
315 /// Maps a debug section name to a standard DWARF section name.
316 virtual StringRef
mapDebugSectionName(StringRef Name
) const { return Name
; }
318 /// True if this is a relocatable object (.o/.obj).
319 virtual bool isRelocatableObject() const = 0;
321 /// @returns Pointer to ObjectFile subclass to handle this type of object.
322 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
324 /// Create ObjectFile from path.
325 static Expected
<OwningBinary
<ObjectFile
>>
326 createObjectFile(StringRef ObjectPath
);
328 static Expected
<std::unique_ptr
<ObjectFile
>>
329 createObjectFile(MemoryBufferRef Object
, llvm::file_magic Type
);
330 static Expected
<std::unique_ptr
<ObjectFile
>>
331 createObjectFile(MemoryBufferRef Object
) {
332 return createObjectFile(Object
, llvm::file_magic::unknown
);
335 static bool classof(const Binary
*v
) {
336 return v
->isObject();
339 static Expected
<std::unique_ptr
<COFFObjectFile
>>
340 createCOFFObjectFile(MemoryBufferRef Object
);
342 static Expected
<std::unique_ptr
<ObjectFile
>>
343 createELFObjectFile(MemoryBufferRef Object
);
345 static Expected
<std::unique_ptr
<MachOObjectFile
>>
346 createMachOObjectFile(MemoryBufferRef Object
,
347 uint32_t UniversalCputype
= 0,
348 uint32_t UniversalIndex
= 0);
350 static Expected
<std::unique_ptr
<WasmObjectFile
>>
351 createWasmObjectFile(MemoryBufferRef Object
);
354 // Inline function definitions.
355 inline SymbolRef::SymbolRef(DataRefImpl SymbolP
, const ObjectFile
*Owner
)
356 : BasicSymbolRef(SymbolP
, Owner
) {}
358 inline Expected
<StringRef
> SymbolRef::getName() const {
359 return getObject()->getSymbolName(getRawDataRefImpl());
362 inline Expected
<uint64_t> SymbolRef::getAddress() const {
363 return getObject()->getSymbolAddress(getRawDataRefImpl());
366 inline uint64_t SymbolRef::getValue() const {
367 return getObject()->getSymbolValue(getRawDataRefImpl());
370 inline uint32_t SymbolRef::getAlignment() const {
371 return getObject()->getSymbolAlignment(getRawDataRefImpl());
374 inline uint64_t SymbolRef::getCommonSize() const {
375 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
378 inline Expected
<section_iterator
> SymbolRef::getSection() const {
379 return getObject()->getSymbolSection(getRawDataRefImpl());
382 inline Expected
<SymbolRef::Type
> SymbolRef::getType() const {
383 return getObject()->getSymbolType(getRawDataRefImpl());
386 inline const ObjectFile
*SymbolRef::getObject() const {
387 const SymbolicFile
*O
= BasicSymbolRef::getObject();
388 return cast
<ObjectFile
>(O
);
392 inline SectionRef::SectionRef(DataRefImpl SectionP
,
393 const ObjectFile
*Owner
)
394 : SectionPimpl(SectionP
)
395 , OwningObject(Owner
) {}
397 inline bool SectionRef::operator==(const SectionRef
&Other
) const {
398 return SectionPimpl
== Other
.SectionPimpl
;
401 inline bool SectionRef::operator!=(const SectionRef
&Other
) const {
402 return SectionPimpl
!= Other
.SectionPimpl
;
405 inline bool SectionRef::operator<(const SectionRef
&Other
) const {
406 return SectionPimpl
< Other
.SectionPimpl
;
409 inline void SectionRef::moveNext() {
410 return OwningObject
->moveSectionNext(SectionPimpl
);
413 inline std::error_code
SectionRef::getName(StringRef
&Result
) const {
414 return OwningObject
->getSectionName(SectionPimpl
, Result
);
417 inline uint64_t SectionRef::getAddress() const {
418 return OwningObject
->getSectionAddress(SectionPimpl
);
421 inline uint64_t SectionRef::getIndex() const {
422 return OwningObject
->getSectionIndex(SectionPimpl
);
425 inline uint64_t SectionRef::getSize() const {
426 return OwningObject
->getSectionSize(SectionPimpl
);
429 inline std::error_code
SectionRef::getContents(StringRef
&Result
) const {
430 return OwningObject
->getSectionContents(SectionPimpl
, Result
);
433 inline uint64_t SectionRef::getAlignment() const {
434 return OwningObject
->getSectionAlignment(SectionPimpl
);
437 inline bool SectionRef::isCompressed() const {
438 return OwningObject
->isSectionCompressed(SectionPimpl
);
441 inline bool SectionRef::isText() const {
442 return OwningObject
->isSectionText(SectionPimpl
);
445 inline bool SectionRef::isData() const {
446 return OwningObject
->isSectionData(SectionPimpl
);
449 inline bool SectionRef::isBSS() const {
450 return OwningObject
->isSectionBSS(SectionPimpl
);
453 inline bool SectionRef::isVirtual() const {
454 return OwningObject
->isSectionVirtual(SectionPimpl
);
457 inline bool SectionRef::isBitcode() const {
458 return OwningObject
->isSectionBitcode(SectionPimpl
);
461 inline bool SectionRef::isStripped() const {
462 return OwningObject
->isSectionStripped(SectionPimpl
);
465 inline bool SectionRef::isBerkeleyText() const {
466 return OwningObject
->isBerkeleyText(SectionPimpl
);
469 inline bool SectionRef::isBerkeleyData() const {
470 return OwningObject
->isBerkeleyData(SectionPimpl
);
473 inline relocation_iterator
SectionRef::relocation_begin() const {
474 return OwningObject
->section_rel_begin(SectionPimpl
);
477 inline relocation_iterator
SectionRef::relocation_end() const {
478 return OwningObject
->section_rel_end(SectionPimpl
);
481 inline section_iterator
SectionRef::getRelocatedSection() const {
482 return OwningObject
->getRelocatedSection(SectionPimpl
);
485 inline DataRefImpl
SectionRef::getRawDataRefImpl() const {
489 inline const ObjectFile
*SectionRef::getObject() const {
494 inline RelocationRef::RelocationRef(DataRefImpl RelocationP
,
495 const ObjectFile
*Owner
)
496 : RelocationPimpl(RelocationP
)
497 , OwningObject(Owner
) {}
499 inline bool RelocationRef::operator==(const RelocationRef
&Other
) const {
500 return RelocationPimpl
== Other
.RelocationPimpl
;
503 inline void RelocationRef::moveNext() {
504 return OwningObject
->moveRelocationNext(RelocationPimpl
);
507 inline uint64_t RelocationRef::getOffset() const {
508 return OwningObject
->getRelocationOffset(RelocationPimpl
);
511 inline symbol_iterator
RelocationRef::getSymbol() const {
512 return OwningObject
->getRelocationSymbol(RelocationPimpl
);
515 inline uint64_t RelocationRef::getType() const {
516 return OwningObject
->getRelocationType(RelocationPimpl
);
519 inline void RelocationRef::getTypeName(SmallVectorImpl
<char> &Result
) const {
520 return OwningObject
->getRelocationTypeName(RelocationPimpl
, Result
);
523 inline DataRefImpl
RelocationRef::getRawDataRefImpl() const {
524 return RelocationPimpl
;
527 inline const ObjectFile
*RelocationRef::getObject() const {
531 } // end namespace object
533 } // end namespace llvm
535 #endif // LLVM_OBJECT_OBJECTFILE_H