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_OBJECT_FILE_H
15 #define LLVM_OBJECT_OBJECT_FILE_H
17 #include "llvm/Object/Binary.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/Support/ErrorHandling.h"
21 #include "llvm/Support/MemoryBuffer.h"
31 // ELF needs this for relocations. This entire union should probably be a
32 // char[max(8, sizeof(uintptr_t))] and require the impl to cast.
42 template<class content_type>
43 class content_iterator {
46 content_iterator(content_type symb)
49 const content_type* operator->() const {
53 const content_type &operator*() const {
57 bool operator==(const content_iterator &other) const {
58 return Current == other.Current;
61 bool operator!=(const content_iterator &other) const {
62 return !(*this == other);
65 content_iterator& increment(error_code &err) {
67 if (error_code ec = Current.getNext(next))
75 static bool operator ==(const DataRefImpl &a, const DataRefImpl &b) {
76 // Check bitwise identical. This is the only legal way to compare a union w/o
77 // knowing which member is in use.
78 return std::memcmp(&a, &b, sizeof(DataRefImpl)) == 0;
81 /// SymbolRef - This is a value type class that represents a single symbol in
82 /// the list of symbols in the object file.
84 friend class SectionRef;
85 DataRefImpl SymbolPimpl;
86 const ObjectFile *OwningObject;
89 SymbolRef() : OwningObject(NULL) {
90 std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl));
96 ST_External, // Defined in another object file
100 SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
102 bool operator==(const SymbolRef &Other) const;
104 error_code getNext(SymbolRef &Result) const;
106 error_code getName(StringRef &Result) const;
107 error_code getAddress(uint64_t &Result) const;
108 error_code getOffset(uint64_t &Result) const;
109 error_code getSize(uint64_t &Result) const;
110 error_code getType(SymbolRef::Type &Result) const;
112 /// Returns the ascii char that should be displayed in a symbol table dump via
113 /// nm for this symbol.
114 error_code getNMTypeChar(char &Result) const;
116 /// Returns true for symbols that are internal to the object file format such
117 /// as section symbols.
118 error_code isInternal(bool &Result) const;
120 /// Returns true for symbols that can be used in another objects,
121 /// such as library functions
122 error_code isGlobal(bool &Result) const;
124 /// Returns true for weak symbols.
125 error_code isWeak(bool &Result) const;
127 DataRefImpl getRawDataRefImpl() const;
129 typedef content_iterator<SymbolRef> symbol_iterator;
131 /// RelocationRef - This is a value type class that represents a single
132 /// relocation in the list of relocations in the object file.
133 class RelocationRef {
134 DataRefImpl RelocationPimpl;
135 const ObjectFile *OwningObject;
138 RelocationRef() : OwningObject(NULL) {
139 std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl));
142 RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
144 bool operator==(const RelocationRef &Other) const;
146 error_code getNext(RelocationRef &Result) const;
148 error_code getAddress(uint64_t &Result) const;
149 error_code getSymbol(SymbolRef &Result) const;
150 error_code getType(uint32_t &Result) const;
152 /// @brief Get a string that represents the type of this relocation.
154 /// This is for display purposes only.
155 error_code getTypeName(SmallVectorImpl<char> &Result) const;
156 error_code getAdditionalInfo(int64_t &Result) const;
158 /// @brief Get a string that represents the calculation of the value of this
161 /// This is for display purposes only.
162 error_code getValueString(SmallVectorImpl<char> &Result) const;
164 typedef content_iterator<RelocationRef> relocation_iterator;
166 /// SectionRef - This is a value type class that represents a single section in
167 /// the list of sections in the object file.
169 friend class SymbolRef;
170 DataRefImpl SectionPimpl;
171 const ObjectFile *OwningObject;
174 SectionRef() : OwningObject(NULL) {
175 std::memset(&SectionPimpl, 0, sizeof(SectionPimpl));
178 SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
180 bool operator==(const SectionRef &Other) const;
182 error_code getNext(SectionRef &Result) const;
184 error_code getName(StringRef &Result) const;
185 error_code getAddress(uint64_t &Result) const;
186 error_code getSize(uint64_t &Result) const;
187 error_code getContents(StringRef &Result) const;
189 /// @brief Get the alignment of this section as the actual value (not log 2).
190 error_code getAlignment(uint64_t &Result) const;
192 // FIXME: Move to the normalization layer when it's created.
193 error_code isText(bool &Result) const;
194 error_code isData(bool &Result) const;
195 error_code isBSS(bool &Result) const;
197 error_code containsSymbol(SymbolRef S, bool &Result) const;
199 relocation_iterator begin_relocations() const;
200 relocation_iterator end_relocations() const;
202 typedef content_iterator<SectionRef> section_iterator;
204 const uint64_t UnknownAddressOrSize = ~0ULL;
206 /// ObjectFile - This class is the base class for all object file types.
207 /// Concrete instances of this object are created by createObjectFile, which
208 /// figure out which type to create.
209 class ObjectFile : public Binary {
211 ObjectFile(); // = delete
212 ObjectFile(const ObjectFile &other); // = delete
215 ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
217 const uint8_t *base() const {
218 return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
221 // These functions are for SymbolRef to call internally. The main goal of
222 // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
223 // entry in the memory mapped object file. SymbolPimpl cannot contain any
224 // virtual functions because then it could not point into the memory mapped
227 // Implementations assume that the DataRefImpl is valid and has not been
228 // modified externally. It's UB otherwise.
229 friend class SymbolRef;
230 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
231 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
232 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
233 virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const =0;
234 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
235 virtual error_code getSymbolType(DataRefImpl Symb,
236 SymbolRef::Type &Res) const = 0;
237 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
238 virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
239 virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const = 0;
240 virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const = 0;
242 // Same as above for SectionRef.
243 friend class SectionRef;
244 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
245 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
246 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
247 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
248 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
249 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
250 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
251 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
252 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
253 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
254 bool &Result) const = 0;
255 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
256 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
259 // Same as above for RelocationRef.
260 friend class RelocationRef;
261 virtual error_code getRelocationNext(DataRefImpl Rel,
262 RelocationRef &Res) const = 0;
263 virtual error_code getRelocationAddress(DataRefImpl Rel,
264 uint64_t &Res) const =0;
265 virtual error_code getRelocationSymbol(DataRefImpl Rel,
266 SymbolRef &Res) const = 0;
267 virtual error_code getRelocationType(DataRefImpl Rel,
268 uint32_t &Res) const = 0;
269 virtual error_code getRelocationTypeName(DataRefImpl Rel,
270 SmallVectorImpl<char> &Result) const = 0;
271 virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
272 int64_t &Res) const = 0;
273 virtual error_code getRelocationValueString(DataRefImpl Rel,
274 SmallVectorImpl<char> &Result) const = 0;
278 virtual symbol_iterator begin_symbols() const = 0;
279 virtual symbol_iterator end_symbols() const = 0;
281 virtual section_iterator begin_sections() const = 0;
282 virtual section_iterator end_sections() const = 0;
284 /// @brief The number of bytes used to represent an address in this object
286 virtual uint8_t getBytesInAddress() const = 0;
288 virtual StringRef getFileFormatName() const = 0;
289 virtual /* Triple::ArchType */ unsigned getArch() const = 0;
291 /// @returns Pointer to ObjectFile subclass to handle this type of object.
292 /// @param ObjectPath The path to the object file. ObjectPath.isObject must
294 /// @brief Create ObjectFile from path.
295 static ObjectFile *createObjectFile(StringRef ObjectPath);
296 static ObjectFile *createObjectFile(MemoryBuffer *Object);
298 static inline bool classof(const Binary *v) {
299 return v->getType() >= isObject &&
300 v->getType() < lastObject;
302 static inline bool classof(const ObjectFile *v) { return true; }
305 static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
306 static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
307 static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
310 // Inline function definitions.
311 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
312 : SymbolPimpl(SymbolP)
313 , OwningObject(Owner) {}
315 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
316 return SymbolPimpl == Other.SymbolPimpl;
319 inline error_code SymbolRef::getNext(SymbolRef &Result) const {
320 return OwningObject->getSymbolNext(SymbolPimpl, Result);
323 inline error_code SymbolRef::getName(StringRef &Result) const {
324 return OwningObject->getSymbolName(SymbolPimpl, Result);
327 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
328 return OwningObject->getSymbolAddress(SymbolPimpl, Result);
331 inline error_code SymbolRef::getOffset(uint64_t &Result) const {
332 return OwningObject->getSymbolOffset(SymbolPimpl, Result);
335 inline error_code SymbolRef::getSize(uint64_t &Result) const {
336 return OwningObject->getSymbolSize(SymbolPimpl, Result);
339 inline error_code SymbolRef::getNMTypeChar(char &Result) const {
340 return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
343 inline error_code SymbolRef::isInternal(bool &Result) const {
344 return OwningObject->isSymbolInternal(SymbolPimpl, Result);
347 inline error_code SymbolRef::isGlobal(bool &Result) const {
348 return OwningObject->isSymbolGlobal(SymbolPimpl, Result);
351 inline error_code SymbolRef::isWeak(bool &Result) const {
352 return OwningObject->isSymbolWeak(SymbolPimpl, Result);
355 inline error_code SymbolRef::getType(SymbolRef::Type &Result) const {
356 return OwningObject->getSymbolType(SymbolPimpl, Result);
359 inline DataRefImpl SymbolRef::getRawDataRefImpl() const {
365 inline SectionRef::SectionRef(DataRefImpl SectionP,
366 const ObjectFile *Owner)
367 : SectionPimpl(SectionP)
368 , OwningObject(Owner) {}
370 inline bool SectionRef::operator==(const SectionRef &Other) const {
371 return SectionPimpl == Other.SectionPimpl;
374 inline error_code SectionRef::getNext(SectionRef &Result) const {
375 return OwningObject->getSectionNext(SectionPimpl, Result);
378 inline error_code SectionRef::getName(StringRef &Result) const {
379 return OwningObject->getSectionName(SectionPimpl, Result);
382 inline error_code SectionRef::getAddress(uint64_t &Result) const {
383 return OwningObject->getSectionAddress(SectionPimpl, Result);
386 inline error_code SectionRef::getSize(uint64_t &Result) const {
387 return OwningObject->getSectionSize(SectionPimpl, Result);
390 inline error_code SectionRef::getContents(StringRef &Result) const {
391 return OwningObject->getSectionContents(SectionPimpl, Result);
394 inline error_code SectionRef::getAlignment(uint64_t &Result) const {
395 return OwningObject->getSectionAlignment(SectionPimpl, Result);
398 inline error_code SectionRef::isText(bool &Result) const {
399 return OwningObject->isSectionText(SectionPimpl, Result);
402 inline error_code SectionRef::isData(bool &Result) const {
403 return OwningObject->isSectionData(SectionPimpl, Result);
406 inline error_code SectionRef::isBSS(bool &Result) const {
407 return OwningObject->isSectionBSS(SectionPimpl, Result);
410 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
411 return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
415 inline relocation_iterator SectionRef::begin_relocations() const {
416 return OwningObject->getSectionRelBegin(SectionPimpl);
419 inline relocation_iterator SectionRef::end_relocations() const {
420 return OwningObject->getSectionRelEnd(SectionPimpl);
425 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
426 const ObjectFile *Owner)
427 : RelocationPimpl(RelocationP)
428 , OwningObject(Owner) {}
430 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
431 return RelocationPimpl == Other.RelocationPimpl;
434 inline error_code RelocationRef::getNext(RelocationRef &Result) const {
435 return OwningObject->getRelocationNext(RelocationPimpl, Result);
438 inline error_code RelocationRef::getAddress(uint64_t &Result) const {
439 return OwningObject->getRelocationAddress(RelocationPimpl, Result);
442 inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
443 return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
446 inline error_code RelocationRef::getType(uint32_t &Result) const {
447 return OwningObject->getRelocationType(RelocationPimpl, Result);
450 inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
452 return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
455 inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
456 return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
459 inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
461 return OwningObject->getRelocationValueString(RelocationPimpl, Result);
464 } // end namespace object
465 } // end namespace llvm