[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / include / llvm / Object / ObjectFile.h
blob63c556b7f0d9ab0121aba42121fdfa26b14092d2
1 //===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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"
29 #include <cassert>
30 #include <cstdint>
31 #include <memory>
32 #include <system_error>
34 namespace llvm {
36 class ARMAttributeParser;
38 namespace object {
40 class COFFObjectFile;
41 class MachOObjectFile;
42 class ObjectFile;
43 class SectionRef;
44 class SymbolRef;
45 class symbol_iterator;
46 class WasmObjectFile;
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.
52 class RelocationRef {
53 DataRefImpl RelocationPimpl;
54 const ObjectFile *OwningObject = nullptr;
56 public:
57 RelocationRef() = default;
58 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
60 bool operator==(const RelocationRef &Other) const;
62 void moveNext();
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.
69 ///
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.
81 class SectionRef {
82 friend class SymbolRef;
84 DataRefImpl SectionPimpl;
85 const ObjectFile *OwningObject = nullptr;
87 public:
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;
95 void moveNext();
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.
108 bool isText() const;
109 /// Whether this section contains data, not instructions.
110 bool isData() const;
111 /// Whether this section contains BSS uninitialized data.
112 bool isBSS() const;
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 const static uint64_t UndefSection = UINT64_MAX;
142 uint64_t Address = 0;
143 uint64_t SectionIndex = UndefSection;
146 inline bool operator<(const SectionedAddress &LHS,
147 const SectionedAddress &RHS) {
148 return std::tie(LHS.SectionIndex, LHS.Address) <
149 std::tie(RHS.SectionIndex, RHS.Address);
152 inline bool operator==(const SectionedAddress &LHS,
153 const SectionedAddress &RHS) {
154 return std::tie(LHS.SectionIndex, LHS.Address) ==
155 std::tie(RHS.SectionIndex, RHS.Address);
158 /// This is a value type class that represents a single symbol in the list of
159 /// symbols in the object file.
160 class SymbolRef : public BasicSymbolRef {
161 friend class SectionRef;
163 public:
164 enum Type {
165 ST_Unknown, // Type not specified
166 ST_Data,
167 ST_Debug,
168 ST_File,
169 ST_Function,
170 ST_Other
173 SymbolRef() = default;
174 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
175 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
176 assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
179 Expected<StringRef> getName() const;
180 /// Returns the symbol virtual address (i.e. address at which it will be
181 /// mapped).
182 Expected<uint64_t> getAddress() const;
184 /// Return the value of the symbol depending on the object this can be an
185 /// offset or a virtual address.
186 uint64_t getValue() const;
188 /// Get the alignment of this symbol as the actual value (not log 2).
189 uint32_t getAlignment() const;
190 uint64_t getCommonSize() const;
191 Expected<SymbolRef::Type> getType() const;
193 /// Get section this symbol is defined in reference to. Result is
194 /// end_sections() if it is undefined or is an absolute symbol.
195 Expected<section_iterator> getSection() const;
197 const ObjectFile *getObject() const;
200 class symbol_iterator : public basic_symbol_iterator {
201 public:
202 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
203 symbol_iterator(const basic_symbol_iterator &B)
204 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
205 cast<ObjectFile>(B->getObject()))) {}
207 const SymbolRef *operator->() const {
208 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
209 return static_cast<const SymbolRef*>(&P);
212 const SymbolRef &operator*() const {
213 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
214 return static_cast<const SymbolRef&>(P);
218 /// This class is the base class for all object file types. Concrete instances
219 /// of this object are created by createObjectFile, which figures out which type
220 /// to create.
221 class ObjectFile : public SymbolicFile {
222 virtual void anchor();
224 protected:
225 ObjectFile(unsigned int Type, MemoryBufferRef Source);
227 const uint8_t *base() const {
228 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
231 // These functions are for SymbolRef to call internally. The main goal of
232 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
233 // entry in the memory mapped object file. SymbolPimpl cannot contain any
234 // virtual functions because then it could not point into the memory mapped
235 // file.
237 // Implementations assume that the DataRefImpl is valid and has not been
238 // modified externally. It's UB otherwise.
239 friend class SymbolRef;
241 virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
242 Error printSymbolName(raw_ostream &OS,
243 DataRefImpl Symb) const override;
244 virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
245 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
246 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
247 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
248 virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
249 virtual Expected<section_iterator>
250 getSymbolSection(DataRefImpl Symb) const = 0;
252 // Same as above for SectionRef.
253 friend class SectionRef;
255 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
256 virtual Expected<StringRef> getSectionName(DataRefImpl Sec) const = 0;
257 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
258 virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
259 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
260 virtual Expected<ArrayRef<uint8_t>>
261 getSectionContents(DataRefImpl Sec) const = 0;
262 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
263 virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
264 virtual bool isSectionText(DataRefImpl Sec) const = 0;
265 virtual bool isSectionData(DataRefImpl Sec) const = 0;
266 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
267 // A section is 'virtual' if its contents aren't present in the object image.
268 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
269 virtual bool isSectionBitcode(DataRefImpl Sec) const;
270 virtual bool isSectionStripped(DataRefImpl Sec) const;
271 virtual bool isBerkeleyText(DataRefImpl Sec) const;
272 virtual bool isBerkeleyData(DataRefImpl Sec) const;
273 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
274 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
275 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
277 // Same as above for RelocationRef.
278 friend class RelocationRef;
279 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
280 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
281 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
282 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
283 virtual void getRelocationTypeName(DataRefImpl Rel,
284 SmallVectorImpl<char> &Result) const = 0;
286 uint64_t getSymbolValue(DataRefImpl Symb) const;
288 public:
289 ObjectFile() = delete;
290 ObjectFile(const ObjectFile &other) = delete;
292 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
293 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
294 return getCommonSymbolSizeImpl(Symb);
297 virtual std::vector<SectionRef> dynamic_relocation_sections() const {
298 return std::vector<SectionRef>();
301 using symbol_iterator_range = iterator_range<symbol_iterator>;
302 symbol_iterator_range symbols() const {
303 return symbol_iterator_range(symbol_begin(), symbol_end());
306 virtual section_iterator section_begin() const = 0;
307 virtual section_iterator section_end() const = 0;
309 using section_iterator_range = iterator_range<section_iterator>;
310 section_iterator_range sections() const {
311 return section_iterator_range(section_begin(), section_end());
314 /// The number of bytes used to represent an address in this object
315 /// file format.
316 virtual uint8_t getBytesInAddress() const = 0;
318 virtual StringRef getFileFormatName() const = 0;
319 virtual Triple::ArchType getArch() const = 0;
320 virtual SubtargetFeatures getFeatures() const = 0;
321 virtual void setARMSubArch(Triple &TheTriple) const { }
322 virtual Expected<uint64_t> getStartAddress() const {
323 return errorCodeToError(object_error::parse_failed);
326 /// Create a triple from the data in this object file.
327 Triple makeTriple() const;
329 /// Maps a debug section name to a standard DWARF section name.
330 virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
332 /// True if this is a relocatable object (.o/.obj).
333 virtual bool isRelocatableObject() const = 0;
335 /// @returns Pointer to ObjectFile subclass to handle this type of object.
336 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
337 /// return true.
338 /// Create ObjectFile from path.
339 static Expected<OwningBinary<ObjectFile>>
340 createObjectFile(StringRef ObjectPath);
342 static Expected<std::unique_ptr<ObjectFile>>
343 createObjectFile(MemoryBufferRef Object, llvm::file_magic Type);
344 static Expected<std::unique_ptr<ObjectFile>>
345 createObjectFile(MemoryBufferRef Object) {
346 return createObjectFile(Object, llvm::file_magic::unknown);
349 static bool classof(const Binary *v) {
350 return v->isObject();
353 static Expected<std::unique_ptr<COFFObjectFile>>
354 createCOFFObjectFile(MemoryBufferRef Object);
356 static Expected<std::unique_ptr<ObjectFile>>
357 createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType);
359 static Expected<std::unique_ptr<ObjectFile>>
360 createELFObjectFile(MemoryBufferRef Object);
362 static Expected<std::unique_ptr<MachOObjectFile>>
363 createMachOObjectFile(MemoryBufferRef Object,
364 uint32_t UniversalCputype = 0,
365 uint32_t UniversalIndex = 0);
367 static Expected<std::unique_ptr<WasmObjectFile>>
368 createWasmObjectFile(MemoryBufferRef Object);
371 // Inline function definitions.
372 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
373 : BasicSymbolRef(SymbolP, Owner) {}
375 inline Expected<StringRef> SymbolRef::getName() const {
376 return getObject()->getSymbolName(getRawDataRefImpl());
379 inline Expected<uint64_t> SymbolRef::getAddress() const {
380 return getObject()->getSymbolAddress(getRawDataRefImpl());
383 inline uint64_t SymbolRef::getValue() const {
384 return getObject()->getSymbolValue(getRawDataRefImpl());
387 inline uint32_t SymbolRef::getAlignment() const {
388 return getObject()->getSymbolAlignment(getRawDataRefImpl());
391 inline uint64_t SymbolRef::getCommonSize() const {
392 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
395 inline Expected<section_iterator> SymbolRef::getSection() const {
396 return getObject()->getSymbolSection(getRawDataRefImpl());
399 inline Expected<SymbolRef::Type> SymbolRef::getType() const {
400 return getObject()->getSymbolType(getRawDataRefImpl());
403 inline const ObjectFile *SymbolRef::getObject() const {
404 const SymbolicFile *O = BasicSymbolRef::getObject();
405 return cast<ObjectFile>(O);
408 /// SectionRef
409 inline SectionRef::SectionRef(DataRefImpl SectionP,
410 const ObjectFile *Owner)
411 : SectionPimpl(SectionP)
412 , OwningObject(Owner) {}
414 inline bool SectionRef::operator==(const SectionRef &Other) const {
415 return OwningObject == Other.OwningObject &&
416 SectionPimpl == Other.SectionPimpl;
419 inline bool SectionRef::operator!=(const SectionRef &Other) const {
420 return !(*this == Other);
423 inline bool SectionRef::operator<(const SectionRef &Other) const {
424 assert(OwningObject == Other.OwningObject);
425 return SectionPimpl < Other.SectionPimpl;
428 inline void SectionRef::moveNext() {
429 return OwningObject->moveSectionNext(SectionPimpl);
432 inline Expected<StringRef> SectionRef::getName() const {
433 return OwningObject->getSectionName(SectionPimpl);
436 inline uint64_t SectionRef::getAddress() const {
437 return OwningObject->getSectionAddress(SectionPimpl);
440 inline uint64_t SectionRef::getIndex() const {
441 return OwningObject->getSectionIndex(SectionPimpl);
444 inline uint64_t SectionRef::getSize() const {
445 return OwningObject->getSectionSize(SectionPimpl);
448 inline Expected<StringRef> SectionRef::getContents() const {
449 Expected<ArrayRef<uint8_t>> Res =
450 OwningObject->getSectionContents(SectionPimpl);
451 if (!Res)
452 return Res.takeError();
453 return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
456 inline uint64_t SectionRef::getAlignment() const {
457 return OwningObject->getSectionAlignment(SectionPimpl);
460 inline bool SectionRef::isCompressed() const {
461 return OwningObject->isSectionCompressed(SectionPimpl);
464 inline bool SectionRef::isText() const {
465 return OwningObject->isSectionText(SectionPimpl);
468 inline bool SectionRef::isData() const {
469 return OwningObject->isSectionData(SectionPimpl);
472 inline bool SectionRef::isBSS() const {
473 return OwningObject->isSectionBSS(SectionPimpl);
476 inline bool SectionRef::isVirtual() const {
477 return OwningObject->isSectionVirtual(SectionPimpl);
480 inline bool SectionRef::isBitcode() const {
481 return OwningObject->isSectionBitcode(SectionPimpl);
484 inline bool SectionRef::isStripped() const {
485 return OwningObject->isSectionStripped(SectionPimpl);
488 inline bool SectionRef::isBerkeleyText() const {
489 return OwningObject->isBerkeleyText(SectionPimpl);
492 inline bool SectionRef::isBerkeleyData() const {
493 return OwningObject->isBerkeleyData(SectionPimpl);
496 inline relocation_iterator SectionRef::relocation_begin() const {
497 return OwningObject->section_rel_begin(SectionPimpl);
500 inline relocation_iterator SectionRef::relocation_end() const {
501 return OwningObject->section_rel_end(SectionPimpl);
504 inline section_iterator SectionRef::getRelocatedSection() const {
505 return OwningObject->getRelocatedSection(SectionPimpl);
508 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
509 return SectionPimpl;
512 inline const ObjectFile *SectionRef::getObject() const {
513 return OwningObject;
516 /// RelocationRef
517 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
518 const ObjectFile *Owner)
519 : RelocationPimpl(RelocationP)
520 , OwningObject(Owner) {}
522 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
523 return RelocationPimpl == Other.RelocationPimpl;
526 inline void RelocationRef::moveNext() {
527 return OwningObject->moveRelocationNext(RelocationPimpl);
530 inline uint64_t RelocationRef::getOffset() const {
531 return OwningObject->getRelocationOffset(RelocationPimpl);
534 inline symbol_iterator RelocationRef::getSymbol() const {
535 return OwningObject->getRelocationSymbol(RelocationPimpl);
538 inline uint64_t RelocationRef::getType() const {
539 return OwningObject->getRelocationType(RelocationPimpl);
542 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
543 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
546 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
547 return RelocationPimpl;
550 inline const ObjectFile *RelocationRef::getObject() const {
551 return OwningObject;
554 } // end namespace object
556 template <> struct DenseMapInfo<object::SectionRef> {
557 static bool isEqual(const object::SectionRef &A,
558 const object::SectionRef &B) {
559 return A == B;
561 static object::SectionRef getEmptyKey() {
562 return object::SectionRef({}, nullptr);
564 static object::SectionRef getTombstoneKey() {
565 object::DataRefImpl TS;
566 TS.p = (uintptr_t)-1;
567 return object::SectionRef(TS, nullptr);
569 static unsigned getHashValue(const object::SectionRef &Sec) {
570 object::DataRefImpl Raw = Sec.getRawDataRefImpl();
571 return hash_combine(Raw.p, Raw.d.a, Raw.d.b);
575 } // end namespace llvm
577 #endif // LLVM_OBJECT_OBJECTFILE_H