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 void getTypeName(SmallVectorImpl<char> &Result) const;
63 DataRefImpl getRawDataRefImpl() const;
64 const ObjectFile *getObject() const;
66 typedef content_iterator<RelocationRef> relocation_iterator;
68 /// This is a value type class that represents a single section in the list of
69 /// sections in the object file.
71 friend class SymbolRef;
72 DataRefImpl SectionPimpl;
73 const ObjectFile *OwningObject;
76 SectionRef() : OwningObject(nullptr) { }
78 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
80 bool operator==(const SectionRef &Other) const;
81 bool operator!=(const SectionRef &Other) const;
82 bool operator<(const SectionRef &Other) const;
86 std::error_code getName(StringRef &Result) const;
87 uint64_t getAddress() const;
88 uint64_t getSize() const;
89 std::error_code getContents(StringRef &Result) const;
91 /// @brief Get the alignment of this section as the actual value (not log 2).
92 uint64_t getAlignment() const;
97 bool isVirtual() const;
99 bool containsSymbol(SymbolRef S) const;
101 relocation_iterator relocation_begin() const;
102 relocation_iterator relocation_end() const;
103 iterator_range<relocation_iterator> relocations() const {
104 return iterator_range<relocation_iterator>(relocation_begin(),
107 section_iterator getRelocatedSection() const;
109 DataRefImpl getRawDataRefImpl() const;
110 const ObjectFile *getObject() const;
113 /// This is a value type class that represents a single symbol in the list of
114 /// symbols in the object file.
115 class SymbolRef : public BasicSymbolRef {
116 friend class SectionRef;
119 SymbolRef() : BasicSymbolRef() {}
122 ST_Unknown, // Type not specified
130 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
131 SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
132 assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
135 std::error_code getName(StringRef &Result) const;
136 /// Returns the symbol virtual address (i.e. address at which it will be
138 std::error_code getAddress(uint64_t &Result) const;
140 /// Return the value of the symbol depending on the object this can be an
141 /// offset or a virtual address.
142 uint64_t getValue() const;
144 /// @brief Get the alignment of this symbol as the actual value (not log 2).
145 uint32_t getAlignment() const;
146 uint64_t getCommonSize() const;
147 SymbolRef::Type getType() const;
149 /// @brief Get section this symbol is defined in reference to. Result is
150 /// end_sections() if it is undefined or is an absolute symbol.
151 std::error_code getSection(section_iterator &Result) const;
153 const ObjectFile *getObject() const;
156 class symbol_iterator : public basic_symbol_iterator {
158 symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
159 symbol_iterator(const basic_symbol_iterator &B)
160 : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
161 cast<ObjectFile>(B->getObject()))) {}
163 const SymbolRef *operator->() const {
164 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
165 return static_cast<const SymbolRef*>(&P);
168 const SymbolRef &operator*() const {
169 const BasicSymbolRef &P = basic_symbol_iterator::operator *();
170 return static_cast<const SymbolRef&>(P);
174 /// This class is the base class for all object file types. Concrete instances
175 /// of this object are created by createObjectFile, which figures out which type
177 class ObjectFile : public SymbolicFile {
178 virtual void anchor();
179 ObjectFile() = delete;
180 ObjectFile(const ObjectFile &other) = delete;
183 ObjectFile(unsigned int Type, MemoryBufferRef Source);
185 const uint8_t *base() const {
186 return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
189 // These functions are for SymbolRef to call internally. The main goal of
190 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
191 // entry in the memory mapped object file. SymbolPimpl cannot contain any
192 // virtual functions because then it could not point into the memory mapped
195 // Implementations assume that the DataRefImpl is valid and has not been
196 // modified externally. It's UB otherwise.
197 friend class SymbolRef;
198 virtual std::error_code getSymbolName(DataRefImpl Symb,
199 StringRef &Res) const = 0;
200 std::error_code printSymbolName(raw_ostream &OS,
201 DataRefImpl Symb) const override;
202 virtual std::error_code getSymbolAddress(DataRefImpl Symb,
203 uint64_t &Res) const = 0;
204 virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
205 virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
206 virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
207 virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
208 virtual std::error_code getSymbolSection(DataRefImpl Symb,
209 section_iterator &Res) const = 0;
211 // Same as above for SectionRef.
212 friend class SectionRef;
213 virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
214 virtual std::error_code getSectionName(DataRefImpl Sec,
215 StringRef &Res) const = 0;
216 virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
217 virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
218 virtual std::error_code getSectionContents(DataRefImpl Sec,
219 StringRef &Res) const = 0;
220 virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
221 virtual bool isSectionText(DataRefImpl Sec) const = 0;
222 virtual bool isSectionData(DataRefImpl Sec) const = 0;
223 virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
224 // A section is 'virtual' if its contents aren't present in the object image.
225 virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
226 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
227 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
228 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
230 // Same as above for RelocationRef.
231 friend class RelocationRef;
232 virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
233 virtual std::error_code getRelocationAddress(DataRefImpl Rel,
234 uint64_t &Res) const = 0;
235 virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
236 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
237 virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
238 virtual void getRelocationTypeName(DataRefImpl Rel,
239 SmallVectorImpl<char> &Result) const = 0;
242 uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
243 assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
244 return getCommonSymbolSizeImpl(Symb);
247 typedef iterator_range<symbol_iterator> symbol_iterator_range;
248 symbol_iterator_range symbols() const {
249 return symbol_iterator_range(symbol_begin(), symbol_end());
252 virtual section_iterator section_begin() const = 0;
253 virtual section_iterator section_end() const = 0;
255 typedef iterator_range<section_iterator> section_iterator_range;
256 section_iterator_range sections() const {
257 return section_iterator_range(section_begin(), section_end());
260 /// @brief The number of bytes used to represent an address in this object
262 virtual uint8_t getBytesInAddress() const = 0;
264 virtual StringRef getFileFormatName() const = 0;
265 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
267 /// Returns platform-specific object flags, if any.
268 virtual std::error_code getPlatformFlags(unsigned &Result) const {
270 return object_error::invalid_file_type;
273 /// True if this is a relocatable object (.o/.obj).
274 virtual bool isRelocatableObject() const = 0;
276 /// @returns Pointer to ObjectFile subclass to handle this type of object.
277 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
279 /// @brief Create ObjectFile from path.
280 static ErrorOr<OwningBinary<ObjectFile>>
281 createObjectFile(StringRef ObjectPath);
283 static ErrorOr<std::unique_ptr<ObjectFile>>
284 createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
285 static ErrorOr<std::unique_ptr<ObjectFile>>
286 createObjectFile(MemoryBufferRef Object) {
287 return createObjectFile(Object, sys::fs::file_magic::unknown);
291 static inline bool classof(const Binary *v) {
292 return v->isObject();
295 static ErrorOr<std::unique_ptr<COFFObjectFile>>
296 createCOFFObjectFile(MemoryBufferRef Object);
298 static ErrorOr<std::unique_ptr<ObjectFile>>
299 createELFObjectFile(MemoryBufferRef Object);
301 static ErrorOr<std::unique_ptr<MachOObjectFile>>
302 createMachOObjectFile(MemoryBufferRef Object);
305 // Inline function definitions.
306 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
307 : BasicSymbolRef(SymbolP, Owner) {}
309 inline std::error_code SymbolRef::getName(StringRef &Result) const {
310 return getObject()->getSymbolName(getRawDataRefImpl(), Result);
313 inline std::error_code SymbolRef::getAddress(uint64_t &Result) const {
314 return getObject()->getSymbolAddress(getRawDataRefImpl(), Result);
317 inline uint64_t SymbolRef::getValue() const {
318 return getObject()->getSymbolValue(getRawDataRefImpl());
321 inline uint32_t SymbolRef::getAlignment() const {
322 return getObject()->getSymbolAlignment(getRawDataRefImpl());
325 inline uint64_t SymbolRef::getCommonSize() const {
326 return getObject()->getCommonSymbolSize(getRawDataRefImpl());
329 inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
330 return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
333 inline SymbolRef::Type SymbolRef::getType() const {
334 return getObject()->getSymbolType(getRawDataRefImpl());
337 inline const ObjectFile *SymbolRef::getObject() const {
338 const SymbolicFile *O = BasicSymbolRef::getObject();
339 return cast<ObjectFile>(O);
344 inline SectionRef::SectionRef(DataRefImpl SectionP,
345 const ObjectFile *Owner)
346 : SectionPimpl(SectionP)
347 , OwningObject(Owner) {}
349 inline bool SectionRef::operator==(const SectionRef &Other) const {
350 return SectionPimpl == Other.SectionPimpl;
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 void SectionRef::moveNext() {
362 return OwningObject->moveSectionNext(SectionPimpl);
365 inline std::error_code SectionRef::getName(StringRef &Result) const {
366 return OwningObject->getSectionName(SectionPimpl, Result);
369 inline uint64_t SectionRef::getAddress() const {
370 return OwningObject->getSectionAddress(SectionPimpl);
373 inline uint64_t SectionRef::getSize() const {
374 return OwningObject->getSectionSize(SectionPimpl);
377 inline std::error_code SectionRef::getContents(StringRef &Result) const {
378 return OwningObject->getSectionContents(SectionPimpl, Result);
381 inline uint64_t SectionRef::getAlignment() const {
382 return OwningObject->getSectionAlignment(SectionPimpl);
385 inline bool SectionRef::isText() const {
386 return OwningObject->isSectionText(SectionPimpl);
389 inline bool SectionRef::isData() const {
390 return OwningObject->isSectionData(SectionPimpl);
393 inline bool SectionRef::isBSS() const {
394 return OwningObject->isSectionBSS(SectionPimpl);
397 inline bool SectionRef::isVirtual() const {
398 return OwningObject->isSectionVirtual(SectionPimpl);
401 inline relocation_iterator SectionRef::relocation_begin() const {
402 return OwningObject->section_rel_begin(SectionPimpl);
405 inline relocation_iterator SectionRef::relocation_end() const {
406 return OwningObject->section_rel_end(SectionPimpl);
409 inline section_iterator SectionRef::getRelocatedSection() const {
410 return OwningObject->getRelocatedSection(SectionPimpl);
413 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
417 inline const ObjectFile *SectionRef::getObject() const {
422 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
423 const ObjectFile *Owner)
424 : RelocationPimpl(RelocationP)
425 , OwningObject(Owner) {}
427 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
428 return RelocationPimpl == Other.RelocationPimpl;
431 inline void RelocationRef::moveNext() {
432 return OwningObject->moveRelocationNext(RelocationPimpl);
435 inline std::error_code RelocationRef::getAddress(uint64_t &Result) const {
436 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
439 inline uint64_t RelocationRef::getOffset() const {
440 return OwningObject->getRelocationOffset(RelocationPimpl);
443 inline symbol_iterator RelocationRef::getSymbol() const {
444 return OwningObject->getRelocationSymbol(RelocationPimpl);
447 inline uint64_t RelocationRef::getType() const {
448 return OwningObject->getRelocationType(RelocationPimpl);
451 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
452 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
455 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
456 return RelocationPimpl;
459 inline const ObjectFile *RelocationRef::getObject() const {
464 } // end namespace object
465 } // end namespace llvm