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/Object/SymbolicFile.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/FileSystem.h"
22 #include "llvm/Support/MemoryBuffer.h"
31 class MachOObjectFile;
34 class symbol_iterator;
36 typedef content_iterator<SectionRef> section_iterator;
38 /// This is a value type class that represents a single relocation in the list
39 /// of relocations in the object file.
41 DataRefImpl RelocationPimpl;
42 const ObjectFile *OwningObject;
45 RelocationRef() : OwningObject(nullptr) { }
47 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
49 bool operator==(const RelocationRef &Other) const;
53 std::error_code getAddress(uint64_t &Result) const;
54 uint64_t getOffset() const;
55 symbol_iterator getSymbol() const;
56 uint64_t getType() const;
58 /// @brief Get a string that represents the type of this relocation.
60 /// This is for display purposes only.
61 std::error_code getTypeName(SmallVectorImpl<char> &Result) const;
64 DataRefImpl getRawDataRefImpl() const;
65 const ObjectFile *getObject() const;
67 typedef content_iterator<RelocationRef> relocation_iterator;
69 /// This is a value type class that represents a single section in the list of
70 /// sections in the object file.
72 friend class SymbolRef;
73 DataRefImpl SectionPimpl;
74 const ObjectFile *OwningObject;
77 SectionRef() : OwningObject(nullptr) { }
79 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
81 bool operator==(const SectionRef &Other) const;
82 bool operator!=(const SectionRef &Other) const;
83 bool operator<(const SectionRef &Other) const;
87 std::error_code getName(StringRef &Result) const;
88 uint64_t getAddress() const;
89 uint64_t getSize() const;
90 std::error_code getContents(StringRef &Result) const;
92 /// @brief Get the alignment of this section as the actual value (not log 2).
93 uint64_t getAlignment() const;
98 bool isVirtual() const;
100 bool containsSymbol(SymbolRef S) const;
102 relocation_iterator relocation_begin() const;
103 relocation_iterator relocation_end() const;
104 iterator_range<relocation_iterator> relocations() const {
105 return iterator_range<relocation_iterator>(relocation_begin(),
108 section_iterator getRelocatedSection() const;
110 DataRefImpl getRawDataRefImpl() const;
111 const ObjectFile *getObject() const;
114 /// This is a value type class that represents a single symbol in the list of
115 /// symbols in the object file.
116 class SymbolRef : public BasicSymbolRef {
117 friend class SectionRef;
120 SymbolRef() : BasicSymbolRef() {}
123 ST_Unknown, // Type not specified
131 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
132 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
133 assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
136 std::error_code getName(StringRef &Result) const;
137 /// Returns the symbol virtual address (i.e. address at which it will be
139 std::error_code getAddress(uint64_t &Result) const;
141 /// Return the value of the symbol depending on the object this can be an
142 /// offset or a virtual address.
143 uint64_t getValue() const;
145 /// @brief Get the alignment of this symbol as the actual value (not log 2).
146 uint32_t getAlignment() const;
147 uint64_t getCommonSize() const;
148 SymbolRef::Type getType() const;
150 /// @brief Get section this symbol is defined in reference to. Result is
151 /// end_sections() if it is undefined or is an absolute symbol.
152 std::error_code getSection(section_iterator &Result) const;
154 const ObjectFile *getObject() const;
157 class symbol_iterator : public basic_symbol_iterator {
159 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
160 symbol_iterator(const basic_symbol_iterator &B)
161 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
162 cast<ObjectFile>(B->getObject()))) {}
164 const SymbolRef *operator->() const {
165 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
166 return static_cast<const SymbolRef*>(&P);
169 const SymbolRef &operator*() const {
170 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
171 return static_cast<const SymbolRef&>(P);
175 /// This class is the base class for all object file types. Concrete instances
176 /// of this object are created by createObjectFile, which figures out which type
178 class ObjectFile : public SymbolicFile {
179 virtual void anchor();
180 ObjectFile() = delete;
181 ObjectFile(const ObjectFile &other) = delete;
184 ObjectFile(unsigned int Type, MemoryBufferRef Source);
186 const uint8_t *base() const {
187 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
190 // These functions are for SymbolRef to call internally. The main goal of
191 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
192 // entry in the memory mapped object file. SymbolPimpl cannot contain any
193 // virtual functions because then it could not point into the memory mapped
196 // Implementations assume that the DataRefImpl is valid and has not been
197 // modified externally. It's UB otherwise.
198 friend class SymbolRef;
199 virtual std::error_code getSymbolName(DataRefImpl Symb,
200 StringRef &Res) const = 0;
201 std::error_code printSymbolName(raw_ostream &OS,
202 DataRefImpl Symb) const override;
203 virtual std::error_code getSymbolAddress(DataRefImpl Symb,
204 uint64_t &Res) const = 0;
205 virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
206 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
207 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
208 virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
209 virtual std::error_code getSymbolSection(DataRefImpl Symb,
210 section_iterator &Res) const = 0;
212 // Same as above for SectionRef.
213 friend class SectionRef;
214 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
215 virtual std::error_code getSectionName(DataRefImpl Sec,
216 StringRef &Res) const = 0;
217 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
218 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
219 virtual std::error_code getSectionContents(DataRefImpl Sec,
220 StringRef &Res) const = 0;
221 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
222 virtual bool isSectionText(DataRefImpl Sec) const = 0;
223 virtual bool isSectionData(DataRefImpl Sec) const = 0;
224 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
225 // A section is 'virtual' if its contents aren't present in the object image.
226 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
227 virtual bool sectionContainsSymbol(DataRefImpl Sec,
228 DataRefImpl Symb) const = 0;
229 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
230 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
231 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
233 // Same as above for RelocationRef.
234 friend class RelocationRef;
235 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
236 virtual std::error_code getRelocationAddress(DataRefImpl Rel,
237 uint64_t &Res) const = 0;
238 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
239 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
240 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
241 virtual std::error_code
242 getRelocationTypeName(DataRefImpl Rel,
243 SmallVectorImpl<char> &Result) const = 0;
246 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
247 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
248 return getCommonSymbolSizeImpl(Symb);
251 typedef iterator_range<symbol_iterator> symbol_iterator_range;
252 symbol_iterator_range symbols() const {
253 return symbol_iterator_range(symbol_begin(), symbol_end());
256 virtual section_iterator section_begin() const = 0;
257 virtual section_iterator section_end() const = 0;
259 typedef iterator_range<section_iterator> section_iterator_range;
260 section_iterator_range sections() const {
261 return section_iterator_range(section_begin(), section_end());
264 /// @brief The number of bytes used to represent an address in this object
266 virtual uint8_t getBytesInAddress() const = 0;
268 virtual StringRef getFileFormatName() const = 0;
269 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
271 /// Returns platform-specific object flags, if any.
272 virtual std::error_code getPlatformFlags(unsigned &Result) const {
274 return object_error::invalid_file_type;
277 /// True if this is a relocatable object (.o/.obj).
278 virtual bool isRelocatableObject() const = 0;
280 /// @returns Pointer to ObjectFile subclass to handle this type of object.
281 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
283 /// @brief Create ObjectFile from path.
284 static ErrorOr<OwningBinary<ObjectFile>>
285 createObjectFile(StringRef ObjectPath);
287 static ErrorOr<std::unique_ptr<ObjectFile>>
288 createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
289 static ErrorOr<std::unique_ptr<ObjectFile>>
290 createObjectFile(MemoryBufferRef Object) {
291 return createObjectFile(Object, sys::fs::file_magic::unknown);
295 static inline bool classof(const Binary *v) {
296 return v->isObject();
299 static ErrorOr<std::unique_ptr<COFFObjectFile>>
300 createCOFFObjectFile(MemoryBufferRef Object);
302 static ErrorOr<std::unique_ptr<ObjectFile>>
303 createELFObjectFile(MemoryBufferRef Object);
305 static ErrorOr<std::unique_ptr<MachOObjectFile>>
306 createMachOObjectFile(MemoryBufferRef Object);
309 // Inline function definitions.
310 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
311 : BasicSymbolRef(SymbolP, Owner) {}
313 inline std::error_code SymbolRef::getName(StringRef &Result) const {
314 return getObject()->getSymbolName(getRawDataRefImpl(), Result);
317 inline std::error_code SymbolRef::getAddress(uint64_t &Result) const {
318 return getObject()->getSymbolAddress(getRawDataRefImpl(), Result);
321 inline uint64_t SymbolRef::getValue() const {
322 return getObject()->getSymbolValue(getRawDataRefImpl());
325 inline uint32_t SymbolRef::getAlignment() const {
326 return getObject()->getSymbolAlignment(getRawDataRefImpl());
329 inline uint64_t SymbolRef::getCommonSize() const {
330 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
333 inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
334 return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
337 inline SymbolRef::Type SymbolRef::getType() const {
338 return getObject()->getSymbolType(getRawDataRefImpl());
341 inline const ObjectFile *SymbolRef::getObject() const {
342 const SymbolicFile *O = BasicSymbolRef::getObject();
343 return cast<ObjectFile>(O);
348 inline SectionRef::SectionRef(DataRefImpl SectionP,
349 const ObjectFile *Owner)
350 : SectionPimpl(SectionP)
351 , OwningObject(Owner) {}
353 inline bool SectionRef::operator==(const SectionRef &Other) const {
354 return SectionPimpl == Other.SectionPimpl;
357 inline bool SectionRef::operator!=(const SectionRef &Other) const {
358 return SectionPimpl != Other.SectionPimpl;
361 inline bool SectionRef::operator<(const SectionRef &Other) const {
362 return SectionPimpl < Other.SectionPimpl;
365 inline void SectionRef::moveNext() {
366 return OwningObject->moveSectionNext(SectionPimpl);
369 inline std::error_code SectionRef::getName(StringRef &Result) const {
370 return OwningObject->getSectionName(SectionPimpl, Result);
373 inline uint64_t SectionRef::getAddress() const {
374 return OwningObject->getSectionAddress(SectionPimpl);
377 inline uint64_t SectionRef::getSize() const {
378 return OwningObject->getSectionSize(SectionPimpl);
381 inline std::error_code SectionRef::getContents(StringRef &Result) const {
382 return OwningObject->getSectionContents(SectionPimpl, Result);
385 inline uint64_t SectionRef::getAlignment() const {
386 return OwningObject->getSectionAlignment(SectionPimpl);
389 inline bool SectionRef::isText() const {
390 return OwningObject->isSectionText(SectionPimpl);
393 inline bool SectionRef::isData() const {
394 return OwningObject->isSectionData(SectionPimpl);
397 inline bool SectionRef::isBSS() const {
398 return OwningObject->isSectionBSS(SectionPimpl);
401 inline bool SectionRef::isVirtual() const {
402 return OwningObject->isSectionVirtual(SectionPimpl);
405 inline bool SectionRef::containsSymbol(SymbolRef S) const {
406 return OwningObject->sectionContainsSymbol(SectionPimpl,
407 S.getRawDataRefImpl());
410 inline relocation_iterator SectionRef::relocation_begin() const {
411 return OwningObject->section_rel_begin(SectionPimpl);
414 inline relocation_iterator SectionRef::relocation_end() const {
415 return OwningObject->section_rel_end(SectionPimpl);
418 inline section_iterator SectionRef::getRelocatedSection() const {
419 return OwningObject->getRelocatedSection(SectionPimpl);
422 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
426 inline const ObjectFile *SectionRef::getObject() const {
431 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
432 const ObjectFile *Owner)
433 : RelocationPimpl(RelocationP)
434 , OwningObject(Owner) {}
436 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
437 return RelocationPimpl == Other.RelocationPimpl;
440 inline void RelocationRef::moveNext() {
441 return OwningObject->moveRelocationNext(RelocationPimpl);
444 inline std::error_code RelocationRef::getAddress(uint64_t &Result) const {
445 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
448 inline uint64_t RelocationRef::getOffset() const {
449 return OwningObject->getRelocationOffset(RelocationPimpl);
452 inline symbol_iterator RelocationRef::getSymbol() const {
453 return OwningObject->getRelocationSymbol(RelocationPimpl);
456 inline uint64_t RelocationRef::getType() const {
457 return OwningObject->getRelocationType(RelocationPimpl);
460 inline std::error_code
461 RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
462 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
465 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
466 return RelocationPimpl;
469 inline const ObjectFile *RelocationRef::getObject() const {
474 } // end namespace object
475 } // end namespace llvm