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