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