ObjectFile.h revision 0685e94895f26f96aa1032696e3150dd00aad1f3
1//===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file declares a file format independent ObjectFile class.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_OBJECT_OBJECT_FILE_H
15#define LLVM_OBJECT_OBJECT_FILE_H
16
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"
22#include <cstring>
23
24namespace llvm {
25namespace object {
26
27class ObjectFile;
28
29union DataRefImpl {
30  struct {
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.
33    uint16_t a, b;
34    uint32_t c;
35  } w;
36  struct {
37    uint32_t a, b;
38  } d;
39  uintptr_t p;
40};
41
42template<class content_type>
43class content_iterator {
44  content_type Current;
45public:
46  content_iterator(content_type symb)
47    : Current(symb) {}
48
49  const content_type* operator->() const {
50    return &Current;
51  }
52
53  const content_type &operator*() const {
54    return Current;
55  }
56
57  bool operator==(const content_iterator &other) const {
58    return Current == other.Current;
59  }
60
61  bool operator!=(const content_iterator &other) const {
62    return !(*this == other);
63  }
64
65  content_iterator& increment(error_code &err) {
66    content_type next;
67    if (error_code ec = Current.getNext(next))
68      err = ec;
69    else
70      Current = next;
71    return *this;
72  }
73};
74
75static 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;
79}
80
81class SymbolRef;
82
83/// RelocationRef - This is a value type class that represents a single
84/// relocation in the list of relocations in the object file.
85class RelocationRef {
86  DataRefImpl RelocationPimpl;
87  const ObjectFile *OwningObject;
88
89public:
90  RelocationRef() : OwningObject(NULL) {
91    std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl));
92  }
93
94  RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
95
96  bool operator==(const RelocationRef &Other) const;
97
98  error_code getNext(RelocationRef &Result) const;
99
100  error_code getAddress(uint64_t &Result) const;
101  error_code getSymbol(SymbolRef &Result) const;
102  error_code getType(uint32_t &Result) const;
103
104  /// @brief Indicates whether this relocation should hidden when listing
105  /// relocations, usually because it is the trailing part of a multipart
106  /// relocation that will be printed as part of the leading relocation.
107  error_code getHidden(bool &Result) const;
108
109  /// @brief Get a string that represents the type of this relocation.
110  ///
111  /// This is for display purposes only.
112  error_code getTypeName(SmallVectorImpl<char> &Result) const;
113  error_code getAdditionalInfo(int64_t &Result) const;
114
115  /// @brief Get a string that represents the calculation of the value of this
116  ///        relocation.
117  ///
118  /// This is for display purposes only.
119  error_code getValueString(SmallVectorImpl<char> &Result) const;
120};
121typedef content_iterator<RelocationRef> relocation_iterator;
122
123/// SectionRef - This is a value type class that represents a single section in
124/// the list of sections in the object file.
125class SectionRef {
126  friend class SymbolRef;
127  DataRefImpl SectionPimpl;
128  const ObjectFile *OwningObject;
129
130public:
131  SectionRef() : OwningObject(NULL) {
132    std::memset(&SectionPimpl, 0, sizeof(SectionPimpl));
133  }
134
135  SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
136
137  bool operator==(const SectionRef &Other) const;
138
139  error_code getNext(SectionRef &Result) const;
140
141  error_code getName(StringRef &Result) const;
142  error_code getAddress(uint64_t &Result) const;
143  error_code getSize(uint64_t &Result) const;
144  error_code getContents(StringRef &Result) const;
145
146  /// @brief Get the alignment of this section as the actual value (not log 2).
147  error_code getAlignment(uint64_t &Result) const;
148
149  // FIXME: Move to the normalization layer when it's created.
150  error_code isText(bool &Result) const;
151  error_code isData(bool &Result) const;
152  error_code isBSS(bool &Result) const;
153
154  error_code containsSymbol(SymbolRef S, bool &Result) const;
155
156  relocation_iterator begin_relocations() const;
157  relocation_iterator end_relocations() const;
158};
159typedef content_iterator<SectionRef> section_iterator;
160
161/// SymbolRef - This is a value type class that represents a single symbol in
162/// the list of symbols in the object file.
163class SymbolRef {
164  friend class SectionRef;
165  DataRefImpl SymbolPimpl;
166  const ObjectFile *OwningObject;
167
168public:
169  SymbolRef() : OwningObject(NULL) {
170    std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl));
171  }
172
173  enum Type {
174    ST_Data,
175    ST_Debug,
176    ST_External,    // Defined in another object file
177    ST_File,
178    ST_Function,
179    ST_Other
180  };
181
182  SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
183
184  bool operator==(const SymbolRef &Other) const;
185
186  error_code getNext(SymbolRef &Result) const;
187
188  error_code getName(StringRef &Result) const;
189  error_code getAddress(uint64_t &Result) const;
190  error_code getOffset(uint64_t &Result) const;
191  error_code getSize(uint64_t &Result) const;
192  error_code getType(SymbolRef::Type &Result) const;
193
194  /// Returns the ascii char that should be displayed in a symbol table dump via
195  /// nm for this symbol.
196  error_code getNMTypeChar(char &Result) const;
197
198  /// Returns true for symbols that are internal to the object file format such
199  /// as section symbols.
200  error_code isInternal(bool &Result) const;
201
202  /// Returns true for symbols that can be used in another objects,
203  /// such as library functions
204  error_code isGlobal(bool &Result) const;
205
206  /// Returns true for weak symbols.
207  error_code isWeak(bool &Result) const;
208
209  /// @brief Return true for absolute symbols.
210  error_code isAbsolute(bool &Result) const;
211
212  /// @brief Get section this symbol is defined in reference to. Result is
213  /// end_sections() if it is undefined or is an absolute symbol.
214  error_code getSection(section_iterator &Result) const;
215
216  DataRefImpl getRawDataRefImpl() const;
217};
218typedef content_iterator<SymbolRef> symbol_iterator;
219
220const uint64_t UnknownAddressOrSize = ~0ULL;
221
222/// ObjectFile - This class is the base class for all object file types.
223/// Concrete instances of this object are created by createObjectFile, which
224/// figure out which type to create.
225class ObjectFile : public Binary {
226private:
227  ObjectFile(); // = delete
228  ObjectFile(const ObjectFile &other); // = delete
229
230protected:
231  ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
232
233  const uint8_t *base() const {
234    return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
235  }
236
237  // These functions are for SymbolRef to call internally. The main goal of
238  // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
239  // entry in the memory mapped object file. SymbolPimpl cannot contain any
240  // virtual functions because then it could not point into the memory mapped
241  // file.
242  //
243  // Implementations assume that the DataRefImpl is valid and has not been
244  // modified externally. It's UB otherwise.
245  friend class SymbolRef;
246  virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
247  virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
248  virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
249  virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const =0;
250  virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
251  virtual error_code getSymbolType(DataRefImpl Symb,
252                                   SymbolRef::Type &Res) const = 0;
253  virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
254  virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
255  virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const = 0;
256  virtual error_code isSymbolWeak(DataRefImpl Symb, bool &Res) const = 0;
257  virtual error_code isSymbolAbsolute(DataRefImpl Symb, bool &Res) const = 0;
258  virtual error_code getSymbolSection(DataRefImpl Symb,
259                                      section_iterator &Res) const = 0;
260
261  // Same as above for SectionRef.
262  friend class SectionRef;
263  virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
264  virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
265  virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
266  virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
267  virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
268  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
269  virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
270  virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
271  virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
272  virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
273                                           bool &Result) const = 0;
274  virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
275  virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
276
277
278  // Same as above for RelocationRef.
279  friend class RelocationRef;
280  virtual error_code getRelocationNext(DataRefImpl Rel,
281                                       RelocationRef &Res) const = 0;
282  virtual error_code getRelocationAddress(DataRefImpl Rel,
283                                          uint64_t &Res) const =0;
284  virtual error_code getRelocationSymbol(DataRefImpl Rel,
285                                         SymbolRef &Res) const = 0;
286  virtual error_code getRelocationType(DataRefImpl Rel,
287                                       uint32_t &Res) const = 0;
288  virtual error_code getRelocationTypeName(DataRefImpl Rel,
289                                       SmallVectorImpl<char> &Result) const = 0;
290  virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
291                                                 int64_t &Res) const = 0;
292  virtual error_code getRelocationValueString(DataRefImpl Rel,
293                                       SmallVectorImpl<char> &Result) const = 0;
294  virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const {
295    Result = false;
296    return object_error::success;
297  }
298
299public:
300
301  virtual symbol_iterator begin_symbols() const = 0;
302  virtual symbol_iterator end_symbols() const = 0;
303
304  virtual section_iterator begin_sections() const = 0;
305  virtual section_iterator end_sections() const = 0;
306
307  /// @brief The number of bytes used to represent an address in this object
308  ///        file format.
309  virtual uint8_t getBytesInAddress() const = 0;
310
311  virtual StringRef getFileFormatName() const = 0;
312  virtual /* Triple::ArchType */ unsigned getArch() const = 0;
313
314  /// @returns Pointer to ObjectFile subclass to handle this type of object.
315  /// @param ObjectPath The path to the object file. ObjectPath.isObject must
316  ///        return true.
317  /// @brief Create ObjectFile from path.
318  static ObjectFile *createObjectFile(StringRef ObjectPath);
319  static ObjectFile *createObjectFile(MemoryBuffer *Object);
320
321  static inline bool classof(const Binary *v) {
322    return v->getType() >= isObject &&
323           v->getType() < lastObject;
324  }
325  static inline bool classof(const ObjectFile *v) { return true; }
326
327public:
328  static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
329  static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
330  static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
331};
332
333// Inline function definitions.
334inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
335  : SymbolPimpl(SymbolP)
336  , OwningObject(Owner) {}
337
338inline bool SymbolRef::operator==(const SymbolRef &Other) const {
339  return SymbolPimpl == Other.SymbolPimpl;
340}
341
342inline error_code SymbolRef::getNext(SymbolRef &Result) const {
343  return OwningObject->getSymbolNext(SymbolPimpl, Result);
344}
345
346inline error_code SymbolRef::getName(StringRef &Result) const {
347  return OwningObject->getSymbolName(SymbolPimpl, Result);
348}
349
350inline error_code SymbolRef::getAddress(uint64_t &Result) const {
351  return OwningObject->getSymbolAddress(SymbolPimpl, Result);
352}
353
354inline error_code SymbolRef::getOffset(uint64_t &Result) const {
355  return OwningObject->getSymbolOffset(SymbolPimpl, Result);
356}
357
358inline error_code SymbolRef::getSize(uint64_t &Result) const {
359  return OwningObject->getSymbolSize(SymbolPimpl, Result);
360}
361
362inline error_code SymbolRef::getNMTypeChar(char &Result) const {
363  return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
364}
365
366inline error_code SymbolRef::isInternal(bool &Result) const {
367  return OwningObject->isSymbolInternal(SymbolPimpl, Result);
368}
369
370inline error_code SymbolRef::isGlobal(bool &Result) const {
371  return OwningObject->isSymbolGlobal(SymbolPimpl, Result);
372}
373
374inline error_code SymbolRef::isWeak(bool &Result) const {
375  return OwningObject->isSymbolWeak(SymbolPimpl, Result);
376}
377
378inline error_code SymbolRef::isAbsolute(bool &Result) const {
379  return OwningObject->isSymbolAbsolute(SymbolPimpl, Result);
380}
381
382inline error_code SymbolRef::getSection(section_iterator &Result) const {
383  return OwningObject->getSymbolSection(SymbolPimpl, Result);
384}
385
386inline error_code SymbolRef::getType(SymbolRef::Type &Result) const {
387  return OwningObject->getSymbolType(SymbolPimpl, Result);
388}
389
390inline DataRefImpl SymbolRef::getRawDataRefImpl() const {
391  return SymbolPimpl;
392}
393
394
395/// SectionRef
396inline SectionRef::SectionRef(DataRefImpl SectionP,
397                              const ObjectFile *Owner)
398  : SectionPimpl(SectionP)
399  , OwningObject(Owner) {}
400
401inline bool SectionRef::operator==(const SectionRef &Other) const {
402  return SectionPimpl == Other.SectionPimpl;
403}
404
405inline error_code SectionRef::getNext(SectionRef &Result) const {
406  return OwningObject->getSectionNext(SectionPimpl, Result);
407}
408
409inline error_code SectionRef::getName(StringRef &Result) const {
410  return OwningObject->getSectionName(SectionPimpl, Result);
411}
412
413inline error_code SectionRef::getAddress(uint64_t &Result) const {
414  return OwningObject->getSectionAddress(SectionPimpl, Result);
415}
416
417inline error_code SectionRef::getSize(uint64_t &Result) const {
418  return OwningObject->getSectionSize(SectionPimpl, Result);
419}
420
421inline error_code SectionRef::getContents(StringRef &Result) const {
422  return OwningObject->getSectionContents(SectionPimpl, Result);
423}
424
425inline error_code SectionRef::getAlignment(uint64_t &Result) const {
426  return OwningObject->getSectionAlignment(SectionPimpl, Result);
427}
428
429inline error_code SectionRef::isText(bool &Result) const {
430  return OwningObject->isSectionText(SectionPimpl, Result);
431}
432
433inline error_code SectionRef::isData(bool &Result) const {
434  return OwningObject->isSectionData(SectionPimpl, Result);
435}
436
437inline error_code SectionRef::isBSS(bool &Result) const {
438  return OwningObject->isSectionBSS(SectionPimpl, Result);
439}
440
441inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
442  return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
443                                             Result);
444}
445
446inline relocation_iterator SectionRef::begin_relocations() const {
447  return OwningObject->getSectionRelBegin(SectionPimpl);
448}
449
450inline relocation_iterator SectionRef::end_relocations() const {
451  return OwningObject->getSectionRelEnd(SectionPimpl);
452}
453
454
455/// RelocationRef
456inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
457                              const ObjectFile *Owner)
458  : RelocationPimpl(RelocationP)
459  , OwningObject(Owner) {}
460
461inline bool RelocationRef::operator==(const RelocationRef &Other) const {
462  return RelocationPimpl == Other.RelocationPimpl;
463}
464
465inline error_code RelocationRef::getNext(RelocationRef &Result) const {
466  return OwningObject->getRelocationNext(RelocationPimpl, Result);
467}
468
469inline error_code RelocationRef::getAddress(uint64_t &Result) const {
470  return OwningObject->getRelocationAddress(RelocationPimpl, Result);
471}
472
473inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
474  return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
475}
476
477inline error_code RelocationRef::getType(uint32_t &Result) const {
478  return OwningObject->getRelocationType(RelocationPimpl, Result);
479}
480
481inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
482  const {
483  return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
484}
485
486inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
487  return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
488}
489
490inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
491  const {
492  return OwningObject->getRelocationValueString(RelocationPimpl, Result);
493}
494
495inline error_code RelocationRef::getHidden(bool &Result) const {
496  return OwningObject->getRelocationHidden(RelocationPimpl, Result);
497}
498
499} // end namespace object
500} // end namespace llvm
501
502#endif
503