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_OBJECTFILE_H
15 #define LLVM_OBJECT_OBJECTFILE_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 /// @brief 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 std::error_code getName(StringRef &Result) const;
98 uint64_t getAddress() const;
99 uint64_t getIndex() const;
100 uint64_t getSize() const;
101 std::error_code getContents(StringRef &Result) const;
103 /// @brief Get the alignment of this section as the actual value (not log 2).
104 uint64_t getAlignment() const;
106 bool isCompressed() const;
110 bool isVirtual() const;
111 bool isBitcode() const;
112 bool isStripped() const;
114 bool containsSymbol(SymbolRef S) const;
116 relocation_iterator relocation_begin() const;
117 relocation_iterator relocation_end() const;
118 iterator_range<relocation_iterator> relocations() const {
119 return make_range(relocation_begin(), relocation_end());
121 section_iterator getRelocatedSection() const;
123 DataRefImpl getRawDataRefImpl() const;
124 const ObjectFile *getObject() const;
127 /// This is a value type class that represents a single symbol in the list of
128 /// symbols in the object file.
129 class SymbolRef : public BasicSymbolRef {
130 friend class SectionRef;
134 ST_Unknown, // Type not specified
142 SymbolRef() = default;
143 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
144 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
145 assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
148 Expected<StringRef> getName() const;
149 /// Returns the symbol virtual address (i.e. address at which it will be
151 Expected<uint64_t> getAddress() const;
153 /// Return the value of the symbol depending on the object this can be an
154 /// offset or a virtual address.
155 uint64_t getValue() const;
157 /// @brief Get the alignment of this symbol as the actual value (not log 2).
158 uint32_t getAlignment() const;
159 uint64_t getCommonSize() const;
160 Expected<SymbolRef::Type> getType() const;
162 /// @brief Get section this symbol is defined in reference to. Result is
163 /// end_sections() if it is undefined or is an absolute symbol.
164 Expected<section_iterator> getSection() const;
166 const ObjectFile *getObject() const;
169 class symbol_iterator : public basic_symbol_iterator {
171 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
172 symbol_iterator(const basic_symbol_iterator &B)
173 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
174 cast<ObjectFile>(B->getObject()))) {}
176 const SymbolRef *operator->() const {
177 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
178 return static_cast<const SymbolRef*>(&P);
181 const SymbolRef &operator*() const {
182 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
183 return static_cast<const SymbolRef&>(P);
187 /// This class is the base class for all object file types. Concrete instances
188 /// of this object are created by createObjectFile, which figures out which type
190 class ObjectFile : public SymbolicFile {
191 virtual void anchor();
194 ObjectFile(unsigned int Type, MemoryBufferRef Source);
196 const uint8_t *base() const {
197 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
200 // These functions are for SymbolRef to call internally. The main goal of
201 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
202 // entry in the memory mapped object file. SymbolPimpl cannot contain any
203 // virtual functions because then it could not point into the memory mapped
206 // Implementations assume that the DataRefImpl is valid and has not been
207 // modified externally. It's UB otherwise.
208 friend class SymbolRef;
210 virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
211 std::error_code printSymbolName(raw_ostream &OS,
212 DataRefImpl Symb) const override;
213 virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
214 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
215 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
216 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
217 virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
218 virtual Expected<section_iterator>
219 getSymbolSection(DataRefImpl Symb) const = 0;
221 // Same as above for SectionRef.
222 friend class SectionRef;
224 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
225 virtual std::error_code getSectionName(DataRefImpl Sec,
226 StringRef &Res) const = 0;
227 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
228 virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
229 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
230 virtual std::error_code getSectionContents(DataRefImpl Sec,
231 StringRef &Res) const = 0;
232 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
233 virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
234 virtual bool isSectionText(DataRefImpl Sec) const = 0;
235 virtual bool isSectionData(DataRefImpl Sec) const = 0;
236 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
237 // A section is 'virtual' if its contents aren't present in the object image.
238 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
239 virtual bool isSectionBitcode(DataRefImpl Sec) const;
240 virtual bool isSectionStripped(DataRefImpl Sec) const;
241 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
242 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
243 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
245 // Same as above for RelocationRef.
246 friend class RelocationRef;
247 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
248 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
249 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
250 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
251 virtual void getRelocationTypeName(DataRefImpl Rel,
252 SmallVectorImpl<char> &Result) const = 0;
254 uint64_t getSymbolValue(DataRefImpl Symb) const;
257 ObjectFile() = delete;
258 ObjectFile(const ObjectFile &other) = delete;
260 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
261 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
262 return getCommonSymbolSizeImpl(Symb);
265 using symbol_iterator_range = iterator_range<symbol_iterator>;
266 symbol_iterator_range symbols() const {
267 return symbol_iterator_range(symbol_begin(), symbol_end());
270 virtual section_iterator section_begin() const = 0;
271 virtual section_iterator section_end() const = 0;
273 using section_iterator_range = iterator_range<section_iterator>;
274 section_iterator_range sections() const {
275 return section_iterator_range(section_begin(), section_end());
278 /// @brief The number of bytes used to represent an address in this object
280 virtual uint8_t getBytesInAddress() const = 0;
282 virtual StringRef getFileFormatName() const = 0;
283 virtual Triple::ArchType getArch() const = 0;
284 virtual SubtargetFeatures getFeatures() const = 0;
285 virtual void setARMSubArch(Triple &TheTriple) const { }
287 /// @brief Create a triple from the data in this object file.
288 Triple makeTriple() const;
290 /// Returns platform-specific object flags, if any.
291 virtual std::error_code getPlatformFlags(unsigned &Result) const {
293 return object_error::invalid_file_type;
296 virtual std::error_code
297 getBuildAttributes(ARMAttributeParser &Attributes) const {
298 return std::error_code();
301 /// Maps a debug section name to a standard DWARF section name.
302 virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
304 /// True if this is a relocatable object (.o/.obj).
305 virtual bool isRelocatableObject() const = 0;
307 /// @returns Pointer to ObjectFile subclass to handle this type of object.
308 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
310 /// @brief Create ObjectFile from path.
311 static Expected<OwningBinary<ObjectFile>>
312 createObjectFile(StringRef ObjectPath);
314 static Expected<std::unique_ptr<ObjectFile>>
315 createObjectFile(MemoryBufferRef Object, llvm::file_magic Type);
316 static Expected<std::unique_ptr<ObjectFile>>
317 createObjectFile(MemoryBufferRef Object) {
318 return createObjectFile(Object, llvm::file_magic::unknown);
321 static bool classof(const Binary *v) {
322 return v->isObject();
325 static Expected<std::unique_ptr<COFFObjectFile>>
326 createCOFFObjectFile(MemoryBufferRef Object);
328 static Expected<std::unique_ptr<ObjectFile>>
329 createELFObjectFile(MemoryBufferRef Object);
331 static Expected<std::unique_ptr<MachOObjectFile>>
332 createMachOObjectFile(MemoryBufferRef Object,
333 uint32_t UniversalCputype = 0,
334 uint32_t UniversalIndex = 0);
336 static Expected<std::unique_ptr<WasmObjectFile>>
337 createWasmObjectFile(MemoryBufferRef Object);
340 // Inline function definitions.
341 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
342 : BasicSymbolRef(SymbolP, Owner) {}
344 inline Expected<StringRef> SymbolRef::getName() const {
345 return getObject()->getSymbolName(getRawDataRefImpl());
348 inline Expected<uint64_t> SymbolRef::getAddress() const {
349 return getObject()->getSymbolAddress(getRawDataRefImpl());
352 inline uint64_t SymbolRef::getValue() const {
353 return getObject()->getSymbolValue(getRawDataRefImpl());
356 inline uint32_t SymbolRef::getAlignment() const {
357 return getObject()->getSymbolAlignment(getRawDataRefImpl());
360 inline uint64_t SymbolRef::getCommonSize() const {
361 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
364 inline Expected<section_iterator> SymbolRef::getSection() const {
365 return getObject()->getSymbolSection(getRawDataRefImpl());
368 inline Expected<SymbolRef::Type> SymbolRef::getType() const {
369 return getObject()->getSymbolType(getRawDataRefImpl());
372 inline const ObjectFile *SymbolRef::getObject() const {
373 const SymbolicFile *O = BasicSymbolRef::getObject();
374 return cast<ObjectFile>(O);
378 inline SectionRef::SectionRef(DataRefImpl SectionP,
379 const ObjectFile *Owner)
380 : SectionPimpl(SectionP)
381 , OwningObject(Owner) {}
383 inline bool SectionRef::operator==(const SectionRef &Other) const {
384 return SectionPimpl == Other.SectionPimpl;
387 inline bool SectionRef::operator!=(const SectionRef &Other) const {
388 return SectionPimpl != Other.SectionPimpl;
391 inline bool SectionRef::operator<(const SectionRef &Other) const {
392 return SectionPimpl < Other.SectionPimpl;
395 inline void SectionRef::moveNext() {
396 return OwningObject->moveSectionNext(SectionPimpl);
399 inline std::error_code SectionRef::getName(StringRef &Result) const {
400 return OwningObject->getSectionName(SectionPimpl, Result);
403 inline uint64_t SectionRef::getAddress() const {
404 return OwningObject->getSectionAddress(SectionPimpl);
407 inline uint64_t SectionRef::getIndex() const {
408 return OwningObject->getSectionIndex(SectionPimpl);
411 inline uint64_t SectionRef::getSize() const {
412 return OwningObject->getSectionSize(SectionPimpl);
415 inline std::error_code SectionRef::getContents(StringRef &Result) const {
416 return OwningObject->getSectionContents(SectionPimpl, Result);
419 inline uint64_t SectionRef::getAlignment() const {
420 return OwningObject->getSectionAlignment(SectionPimpl);
423 inline bool SectionRef::isCompressed() const {
424 return OwningObject->isSectionCompressed(SectionPimpl);
427 inline bool SectionRef::isText() const {
428 return OwningObject->isSectionText(SectionPimpl);
431 inline bool SectionRef::isData() const {
432 return OwningObject->isSectionData(SectionPimpl);
435 inline bool SectionRef::isBSS() const {
436 return OwningObject->isSectionBSS(SectionPimpl);
439 inline bool SectionRef::isVirtual() const {
440 return OwningObject->isSectionVirtual(SectionPimpl);
443 inline bool SectionRef::isBitcode() const {
444 return OwningObject->isSectionBitcode(SectionPimpl);
447 inline bool SectionRef::isStripped() const {
448 return OwningObject->isSectionStripped(SectionPimpl);
451 inline relocation_iterator SectionRef::relocation_begin() const {
452 return OwningObject->section_rel_begin(SectionPimpl);
455 inline relocation_iterator SectionRef::relocation_end() const {
456 return OwningObject->section_rel_end(SectionPimpl);
459 inline section_iterator SectionRef::getRelocatedSection() const {
460 return OwningObject->getRelocatedSection(SectionPimpl);
463 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
467 inline const ObjectFile *SectionRef::getObject() const {
472 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
473 const ObjectFile *Owner)
474 : RelocationPimpl(RelocationP)
475 , OwningObject(Owner) {}
477 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
478 return RelocationPimpl == Other.RelocationPimpl;
481 inline void RelocationRef::moveNext() {
482 return OwningObject->moveRelocationNext(RelocationPimpl);
485 inline uint64_t RelocationRef::getOffset() const {
486 return OwningObject->getRelocationOffset(RelocationPimpl);
489 inline symbol_iterator RelocationRef::getSymbol() const {
490 return OwningObject->getRelocationSymbol(RelocationPimpl);
493 inline uint64_t RelocationRef::getType() const {
494 return OwningObject->getRelocationType(RelocationPimpl);
497 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
498 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
501 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
502 return RelocationPimpl;
505 inline const ObjectFile *RelocationRef::getObject() const {
509 } // end namespace object
511 } // end namespace llvm
513 #endif // LLVM_OBJECT_OBJECTFILE_H