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/iterator_range.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/MC/SubtargetFeature.h"
20 #include "llvm/Object/Binary.h"
21 #include "llvm/Object/Error.h"
22 #include "llvm/Object/SymbolicFile.h"
23 #include "llvm/Support/Casting.h"
24 #include "llvm/Support/Error.h"
25 #include "llvm/Support/ErrorOr.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 /// @brief 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 getSize() const;
99 std::error_code getContents(StringRef &Result) const;
101 /// @brief Get the alignment of this section as the actual value (not log 2).
102 uint64_t getAlignment() const;
104 bool isCompressed() const;
108 bool isVirtual() const;
109 bool isBitcode() const;
111 bool containsSymbol(SymbolRef S) const;
113 relocation_iterator relocation_begin() const;
114 relocation_iterator relocation_end() const;
115 iterator_range<relocation_iterator> relocations() const {
116 return make_range(relocation_begin(), relocation_end());
118 section_iterator getRelocatedSection() const;
120 DataRefImpl getRawDataRefImpl() const;
121 const ObjectFile *getObject() const;
124 /// This is a value type class that represents a single symbol in the list of
125 /// symbols in the object file.
126 class SymbolRef : public BasicSymbolRef {
127 friend class SectionRef;
131 ST_Unknown, // Type not specified
139 SymbolRef() = default;
140 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
141 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
142 assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
145 Expected<StringRef> getName() const;
146 /// Returns the symbol virtual address (i.e. address at which it will be
148 Expected<uint64_t> getAddress() const;
150 /// Return the value of the symbol depending on the object this can be an
151 /// offset or a virtual address.
152 uint64_t getValue() const;
154 /// @brief Get the alignment of this symbol as the actual value (not log 2).
155 uint32_t getAlignment() const;
156 uint64_t getCommonSize() const;
157 Expected<SymbolRef::Type> getType() const;
159 /// @brief Get section this symbol is defined in reference to. Result is
160 /// end_sections() if it is undefined or is an absolute symbol.
161 Expected<section_iterator> getSection() const;
163 const ObjectFile *getObject() const;
166 class symbol_iterator : public basic_symbol_iterator {
168 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
169 symbol_iterator(const basic_symbol_iterator &B)
170 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
171 cast<ObjectFile>(B->getObject()))) {}
173 const SymbolRef *operator->() const {
174 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
175 return static_cast<const SymbolRef*>(&P);
178 const SymbolRef &operator*() const {
179 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
180 return static_cast<const SymbolRef&>(P);
184 /// This class is the base class for all object file types. Concrete instances
185 /// of this object are created by createObjectFile, which figures out which type
187 class ObjectFile : public SymbolicFile {
188 virtual void anchor();
191 ObjectFile(unsigned int Type, MemoryBufferRef Source);
193 const uint8_t *base() const {
194 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
197 // These functions are for SymbolRef to call internally. The main goal of
198 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
199 // entry in the memory mapped object file. SymbolPimpl cannot contain any
200 // virtual functions because then it could not point into the memory mapped
203 // Implementations assume that the DataRefImpl is valid and has not been
204 // modified externally. It's UB otherwise.
205 friend class SymbolRef;
207 virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
208 std::error_code printSymbolName(raw_ostream &OS,
209 DataRefImpl Symb) const override;
210 virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
211 virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
212 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
213 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
214 virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
215 virtual Expected<section_iterator>
216 getSymbolSection(DataRefImpl Symb) const = 0;
218 // Same as above for SectionRef.
219 friend class SectionRef;
221 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
222 virtual std::error_code getSectionName(DataRefImpl Sec,
223 StringRef &Res) const = 0;
224 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
225 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
226 virtual std::error_code getSectionContents(DataRefImpl Sec,
227 StringRef &Res) const = 0;
228 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
229 virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
230 virtual bool isSectionText(DataRefImpl Sec) const = 0;
231 virtual bool isSectionData(DataRefImpl Sec) const = 0;
232 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
233 // A section is 'virtual' if its contents aren't present in the object image.
234 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
235 virtual bool isSectionBitcode(DataRefImpl Sec) const;
236 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
237 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
238 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
240 // Same as above for RelocationRef.
241 friend class RelocationRef;
242 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
243 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
244 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
245 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
246 virtual void getRelocationTypeName(DataRefImpl Rel,
247 SmallVectorImpl<char> &Result) const = 0;
249 uint64_t getSymbolValue(DataRefImpl Symb) const;
252 ObjectFile() = delete;
253 ObjectFile(const ObjectFile &other) = delete;
255 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
256 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
257 return getCommonSymbolSizeImpl(Symb);
260 using symbol_iterator_range = iterator_range<symbol_iterator>;
261 symbol_iterator_range symbols() const {
262 return symbol_iterator_range(symbol_begin(), symbol_end());
265 virtual section_iterator section_begin() const = 0;
266 virtual section_iterator section_end() const = 0;
268 using section_iterator_range = iterator_range<section_iterator>;
269 section_iterator_range sections() const {
270 return section_iterator_range(section_begin(), section_end());
273 /// @brief The number of bytes used to represent an address in this object
275 virtual uint8_t getBytesInAddress() const = 0;
277 virtual StringRef getFileFormatName() const = 0;
278 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
279 virtual SubtargetFeatures getFeatures() const = 0;
280 virtual void setARMSubArch(Triple &TheTriple) const { }
282 /// Returns platform-specific object flags, if any.
283 virtual std::error_code getPlatformFlags(unsigned &Result) const {
285 return object_error::invalid_file_type;
288 virtual std::error_code
289 getBuildAttributes(ARMAttributeParser &Attributes) const {
290 return std::error_code();
293 /// True if this is a relocatable object (.o/.obj).
294 virtual bool isRelocatableObject() const = 0;
296 /// @returns Pointer to ObjectFile subclass to handle this type of object.
297 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
299 /// @brief Create ObjectFile from path.
300 static Expected<OwningBinary<ObjectFile>>
301 createObjectFile(StringRef ObjectPath);
303 static Expected<std::unique_ptr<ObjectFile>>
304 createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
305 static Expected<std::unique_ptr<ObjectFile>>
306 createObjectFile(MemoryBufferRef Object) {
307 return createObjectFile(Object, sys::fs::file_magic::unknown);
310 static inline bool classof(const Binary *v) {
311 return v->isObject();
314 static ErrorOr<std::unique_ptr<COFFObjectFile>>
315 createCOFFObjectFile(MemoryBufferRef Object);
317 static ErrorOr<std::unique_ptr<ObjectFile>>
318 createELFObjectFile(MemoryBufferRef Object);
320 static Expected<std::unique_ptr<MachOObjectFile>>
321 createMachOObjectFile(MemoryBufferRef Object,
322 uint32_t UniversalCputype = 0,
323 uint32_t UniversalIndex = 0);
325 static Expected<std::unique_ptr<WasmObjectFile>>
326 createWasmObjectFile(MemoryBufferRef Object);
329 // Inline function definitions.
330 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
331 : BasicSymbolRef(SymbolP, Owner) {}
333 inline Expected<StringRef> SymbolRef::getName() const {
334 return getObject()->getSymbolName(getRawDataRefImpl());
337 inline Expected<uint64_t> SymbolRef::getAddress() const {
338 return getObject()->getSymbolAddress(getRawDataRefImpl());
341 inline uint64_t SymbolRef::getValue() const {
342 return getObject()->getSymbolValue(getRawDataRefImpl());
345 inline uint32_t SymbolRef::getAlignment() const {
346 return getObject()->getSymbolAlignment(getRawDataRefImpl());
349 inline uint64_t SymbolRef::getCommonSize() const {
350 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
353 inline Expected<section_iterator> SymbolRef::getSection() const {
354 return getObject()->getSymbolSection(getRawDataRefImpl());
357 inline Expected<SymbolRef::Type> SymbolRef::getType() const {
358 return getObject()->getSymbolType(getRawDataRefImpl());
361 inline const ObjectFile *SymbolRef::getObject() const {
362 const SymbolicFile *O = BasicSymbolRef::getObject();
363 return cast<ObjectFile>(O);
367 inline SectionRef::SectionRef(DataRefImpl SectionP,
368 const ObjectFile *Owner)
369 : SectionPimpl(SectionP)
370 , OwningObject(Owner) {}
372 inline bool SectionRef::operator==(const SectionRef &Other) const {
373 return SectionPimpl == Other.SectionPimpl;
376 inline bool SectionRef::operator!=(const SectionRef &Other) const {
377 return SectionPimpl != Other.SectionPimpl;
380 inline bool SectionRef::operator<(const SectionRef &Other) const {
381 return SectionPimpl < Other.SectionPimpl;
384 inline void SectionRef::moveNext() {
385 return OwningObject->moveSectionNext(SectionPimpl);
388 inline std::error_code SectionRef::getName(StringRef &Result) const {
389 return OwningObject->getSectionName(SectionPimpl, Result);
392 inline uint64_t SectionRef::getAddress() const {
393 return OwningObject->getSectionAddress(SectionPimpl);
396 inline uint64_t SectionRef::getSize() const {
397 return OwningObject->getSectionSize(SectionPimpl);
400 inline std::error_code SectionRef::getContents(StringRef &Result) const {
401 return OwningObject->getSectionContents(SectionPimpl, Result);
404 inline uint64_t SectionRef::getAlignment() const {
405 return OwningObject->getSectionAlignment(SectionPimpl);
408 inline bool SectionRef::isCompressed() const {
409 return OwningObject->isSectionCompressed(SectionPimpl);
412 inline bool SectionRef::isText() const {
413 return OwningObject->isSectionText(SectionPimpl);
416 inline bool SectionRef::isData() const {
417 return OwningObject->isSectionData(SectionPimpl);
420 inline bool SectionRef::isBSS() const {
421 return OwningObject->isSectionBSS(SectionPimpl);
424 inline bool SectionRef::isVirtual() const {
425 return OwningObject->isSectionVirtual(SectionPimpl);
428 inline bool SectionRef::isBitcode() const {
429 return OwningObject->isSectionBitcode(SectionPimpl);
432 inline relocation_iterator SectionRef::relocation_begin() const {
433 return OwningObject->section_rel_begin(SectionPimpl);
436 inline relocation_iterator SectionRef::relocation_end() const {
437 return OwningObject->section_rel_end(SectionPimpl);
440 inline section_iterator SectionRef::getRelocatedSection() const {
441 return OwningObject->getRelocatedSection(SectionPimpl);
444 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
448 inline const ObjectFile *SectionRef::getObject() const {
453 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
454 const ObjectFile *Owner)
455 : RelocationPimpl(RelocationP)
456 , OwningObject(Owner) {}
458 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
459 return RelocationPimpl == Other.RelocationPimpl;
462 inline void RelocationRef::moveNext() {
463 return OwningObject->moveRelocationNext(RelocationPimpl);
466 inline uint64_t RelocationRef::getOffset() const {
467 return OwningObject->getRelocationOffset(RelocationPimpl);
470 inline symbol_iterator RelocationRef::getSymbol() const {
471 return OwningObject->getRelocationSymbol(RelocationPimpl);
474 inline uint64_t RelocationRef::getType() const {
475 return OwningObject->getRelocationType(RelocationPimpl);
478 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
479 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
482 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
483 return RelocationPimpl;
486 inline const ObjectFile *RelocationRef::getObject() const {
490 } // end namespace object
492 } // end namespace llvm
494 #endif // LLVM_OBJECT_OBJECTFILE_H