Add a constructor for DataRefImpl and remove excess initialization.
[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 typedef content_iterator<RelocationRef> relocation_iterator;
131
132 /// SectionRef - This is a value type class that represents a single section in
133 /// the list of sections in the object file.
134 class SectionRef {
135   friend class SymbolRef;
136   DataRefImpl SectionPimpl;
137   const ObjectFile *OwningObject;
138
139 public:
140   SectionRef() : OwningObject(NULL) { }
141
142   SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
143
144   bool operator==(const SectionRef &Other) const;
145   bool operator <(const SectionRef &Other) const;
146
147   error_code getNext(SectionRef &Result) const;
148
149   error_code getName(StringRef &Result) const;
150   error_code getAddress(uint64_t &Result) const;
151   error_code getSize(uint64_t &Result) const;
152   error_code getContents(StringRef &Result) const;
153
154   /// @brief Get the alignment of this section as the actual value (not log 2).
155   error_code getAlignment(uint64_t &Result) const;
156
157   // FIXME: Move to the normalization layer when it's created.
158   error_code isText(bool &Result) const;
159   error_code isData(bool &Result) const;
160   error_code isBSS(bool &Result) const;
161
162   error_code containsSymbol(SymbolRef S, bool &Result) const;
163
164   relocation_iterator begin_relocations() const;
165   relocation_iterator end_relocations() const;
166 };
167 typedef content_iterator<SectionRef> section_iterator;
168
169 /// SymbolRef - This is a value type class that represents a single symbol in
170 /// the list of symbols in the object file.
171 class SymbolRef {
172   friend class SectionRef;
173   DataRefImpl SymbolPimpl;
174   const ObjectFile *OwningObject;
175
176 public:
177   SymbolRef() : OwningObject(NULL) { }
178
179   enum Type {
180     ST_Unknown, // Type not specified
181     ST_Data,
182     ST_Debug,
183     ST_File,
184     ST_Function,
185     ST_Other
186   };
187
188   enum Flags {
189     SF_None            = 0,
190     SF_Undefined       = 1U << 0,  // Symbol is defined in another object file
191     SF_Global          = 1U << 1,  // Global symbol
192     SF_Weak            = 1U << 2,  // Weak symbol
193     SF_Absolute        = 1U << 3,  // Absolute symbol
194     SF_ThreadLocal     = 1U << 4,  // Thread local symbol
195     SF_Common          = 1U << 5,  // Symbol has common linkage
196     SF_FormatSpecific  = 1U << 31  // 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 };
226 typedef content_iterator<SymbolRef> symbol_iterator;
227
228 /// LibraryRef - This is a value type class that represents a single library in
229 /// the list of libraries needed by a shared or dynamic object.
230 class LibraryRef {
231   friend class SectionRef;
232   DataRefImpl LibraryPimpl;
233   const ObjectFile *OwningObject;
234
235 public:
236   LibraryRef() : OwningObject(NULL) { }
237
238   LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner);
239
240   bool operator==(const LibraryRef &Other) const;
241   bool operator <(const LibraryRef &Other) const;
242
243   error_code getNext(LibraryRef &Result) const;
244
245   // Get the path to this library, as stored in the object file.
246   error_code getPath(StringRef &Result) const;
247
248   DataRefImpl getRawDataRefImpl() const;
249 };
250 typedef content_iterator<LibraryRef> library_iterator;
251
252 const uint64_t UnknownAddressOrSize = ~0ULL;
253
254 /// ObjectFile - This class is the base class for all object file types.
255 /// Concrete instances of this object are created by createObjectFile, which
256 /// figure out which type to create.
257 class ObjectFile : public Binary {
258   virtual void anchor();
259   ObjectFile(); // = delete
260   ObjectFile(const ObjectFile &other); // = delete
261
262 protected:
263   ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
264
265   const uint8_t *base() const {
266     return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
267   }
268
269   // These functions are for SymbolRef to call internally. The main goal of
270   // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
271   // entry in the memory mapped object file. SymbolPimpl cannot contain any
272   // virtual functions because then it could not point into the memory mapped
273   // file.
274   //
275   // Implementations assume that the DataRefImpl is valid and has not been
276   // modified externally. It's UB otherwise.
277   friend class SymbolRef;
278   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
279   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
280   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
281   virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const =0;
282   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
283   virtual error_code getSymbolType(DataRefImpl Symb,
284                                    SymbolRef::Type &Res) const = 0;
285   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
286   virtual error_code getSymbolFlags(DataRefImpl Symb,
287                                     uint32_t &Res) const = 0;
288   virtual error_code getSymbolSection(DataRefImpl Symb,
289                                       section_iterator &Res) const = 0;
290
291   // Same as above for SectionRef.
292   friend class SectionRef;
293   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
294   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
295   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
296   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
297   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
298   virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res)const=0;
299   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
300   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
301   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
302   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
303                                            bool &Result) const = 0;
304   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
305   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
306
307
308   // Same as above for RelocationRef.
309   friend class RelocationRef;
310   virtual error_code getRelocationNext(DataRefImpl Rel,
311                                        RelocationRef &Res) const = 0;
312   virtual error_code getRelocationAddress(DataRefImpl Rel,
313                                           uint64_t &Res) const =0;
314   virtual error_code getRelocationOffset(DataRefImpl Rel,
315                                          uint64_t &Res) const =0;
316   virtual error_code getRelocationSymbol(DataRefImpl Rel,
317                                          SymbolRef &Res) const = 0;
318   virtual error_code getRelocationType(DataRefImpl Rel,
319                                        uint64_t &Res) const = 0;
320   virtual error_code getRelocationTypeName(DataRefImpl Rel,
321                                        SmallVectorImpl<char> &Result) const = 0;
322   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
323                                                  int64_t &Res) const = 0;
324   virtual error_code getRelocationValueString(DataRefImpl Rel,
325                                        SmallVectorImpl<char> &Result) const = 0;
326   virtual error_code getRelocationHidden(DataRefImpl Rel, bool &Result) const {
327     Result = false;
328     return object_error::success;
329   }
330
331   // Same for LibraryRef
332   friend class LibraryRef;
333   virtual error_code getLibraryNext(DataRefImpl Lib, LibraryRef &Res) const = 0;
334   virtual error_code getLibraryPath(DataRefImpl Lib, StringRef &Res) const = 0;
335
336 public:
337
338   virtual symbol_iterator begin_symbols() const = 0;
339   virtual symbol_iterator end_symbols() const = 0;
340
341   virtual symbol_iterator begin_dynamic_symbols() const = 0;
342   virtual symbol_iterator end_dynamic_symbols() const = 0;
343
344   virtual section_iterator begin_sections() const = 0;
345   virtual section_iterator end_sections() const = 0;
346
347   virtual library_iterator begin_libraries_needed() const = 0;
348   virtual library_iterator end_libraries_needed() const = 0;
349
350   /// @brief The number of bytes used to represent an address in this object
351   ///        file format.
352   virtual uint8_t getBytesInAddress() const = 0;
353
354   virtual StringRef getFileFormatName() const = 0;
355   virtual /* Triple::ArchType */ unsigned getArch() const = 0;
356
357   /// For shared objects, returns the name which this object should be
358   /// loaded from at runtime. This corresponds to DT_SONAME on ELF and
359   /// LC_ID_DYLIB (install name) on MachO.
360   virtual StringRef getLoadName() const = 0;
361
362   /// @returns Pointer to ObjectFile subclass to handle this type of object.
363   /// @param ObjectPath The path to the object file. ObjectPath.isObject must
364   ///        return true.
365   /// @brief Create ObjectFile from path.
366   static ObjectFile *createObjectFile(StringRef ObjectPath);
367   static ObjectFile *createObjectFile(MemoryBuffer *Object);
368
369   static inline bool classof(const Binary *v) {
370     return v->isObject();
371   }
372   static inline bool classof(const ObjectFile *v) { return true; }
373
374 public:
375   static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
376   static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
377   static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
378 };
379
380 // Inline function definitions.
381 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
382   : SymbolPimpl(SymbolP)
383   , OwningObject(Owner) {}
384
385 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
386   return SymbolPimpl == Other.SymbolPimpl;
387 }
388
389 inline bool SymbolRef::operator <(const SymbolRef &Other) const {
390   return SymbolPimpl < Other.SymbolPimpl;
391 }
392
393 inline error_code SymbolRef::getNext(SymbolRef &Result) const {
394   return OwningObject->getSymbolNext(SymbolPimpl, Result);
395 }
396
397 inline error_code SymbolRef::getName(StringRef &Result) const {
398   return OwningObject->getSymbolName(SymbolPimpl, Result);
399 }
400
401 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
402   return OwningObject->getSymbolAddress(SymbolPimpl, Result);
403 }
404
405 inline error_code SymbolRef::getFileOffset(uint64_t &Result) const {
406   return OwningObject->getSymbolFileOffset(SymbolPimpl, Result);
407 }
408
409 inline error_code SymbolRef::getSize(uint64_t &Result) const {
410   return OwningObject->getSymbolSize(SymbolPimpl, Result);
411 }
412
413 inline error_code SymbolRef::getNMTypeChar(char &Result) const {
414   return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
415 }
416
417 inline error_code SymbolRef::getFlags(uint32_t &Result) const {
418   return OwningObject->getSymbolFlags(SymbolPimpl, Result);
419 }
420
421 inline error_code SymbolRef::getSection(section_iterator &Result) const {
422   return OwningObject->getSymbolSection(SymbolPimpl, Result);
423 }
424
425 inline error_code SymbolRef::getType(SymbolRef::Type &Result) const {
426   return OwningObject->getSymbolType(SymbolPimpl, Result);
427 }
428
429 inline DataRefImpl SymbolRef::getRawDataRefImpl() const {
430   return SymbolPimpl;
431 }
432
433
434 /// SectionRef
435 inline SectionRef::SectionRef(DataRefImpl SectionP,
436                               const ObjectFile *Owner)
437   : SectionPimpl(SectionP)
438   , OwningObject(Owner) {}
439
440 inline bool SectionRef::operator==(const SectionRef &Other) const {
441   return SectionPimpl == Other.SectionPimpl;
442 }
443
444 inline bool SectionRef::operator <(const SectionRef &Other) const {
445   return SectionPimpl < Other.SectionPimpl;
446 }
447
448 inline error_code SectionRef::getNext(SectionRef &Result) const {
449   return OwningObject->getSectionNext(SectionPimpl, Result);
450 }
451
452 inline error_code SectionRef::getName(StringRef &Result) const {
453   return OwningObject->getSectionName(SectionPimpl, Result);
454 }
455
456 inline error_code SectionRef::getAddress(uint64_t &Result) const {
457   return OwningObject->getSectionAddress(SectionPimpl, Result);
458 }
459
460 inline error_code SectionRef::getSize(uint64_t &Result) const {
461   return OwningObject->getSectionSize(SectionPimpl, Result);
462 }
463
464 inline error_code SectionRef::getContents(StringRef &Result) const {
465   return OwningObject->getSectionContents(SectionPimpl, Result);
466 }
467
468 inline error_code SectionRef::getAlignment(uint64_t &Result) const {
469   return OwningObject->getSectionAlignment(SectionPimpl, Result);
470 }
471
472 inline error_code SectionRef::isText(bool &Result) const {
473   return OwningObject->isSectionText(SectionPimpl, Result);
474 }
475
476 inline error_code SectionRef::isData(bool &Result) const {
477   return OwningObject->isSectionData(SectionPimpl, Result);
478 }
479
480 inline error_code SectionRef::isBSS(bool &Result) const {
481   return OwningObject->isSectionBSS(SectionPimpl, Result);
482 }
483
484 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
485   return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
486                                              Result);
487 }
488
489 inline relocation_iterator SectionRef::begin_relocations() const {
490   return OwningObject->getSectionRelBegin(SectionPimpl);
491 }
492
493 inline relocation_iterator SectionRef::end_relocations() const {
494   return OwningObject->getSectionRelEnd(SectionPimpl);
495 }
496
497
498 /// RelocationRef
499 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
500                               const ObjectFile *Owner)
501   : RelocationPimpl(RelocationP)
502   , OwningObject(Owner) {}
503
504 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
505   return RelocationPimpl == Other.RelocationPimpl;
506 }
507
508 inline error_code RelocationRef::getNext(RelocationRef &Result) const {
509   return OwningObject->getRelocationNext(RelocationPimpl, Result);
510 }
511
512 inline error_code RelocationRef::getAddress(uint64_t &Result) const {
513   return OwningObject->getRelocationAddress(RelocationPimpl, Result);
514 }
515
516 inline error_code RelocationRef::getOffset(uint64_t &Result) const {
517   return OwningObject->getRelocationOffset(RelocationPimpl, Result);
518 }
519
520 inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
521   return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
522 }
523
524 inline error_code RelocationRef::getType(uint64_t &Result) const {
525   return OwningObject->getRelocationType(RelocationPimpl, Result);
526 }
527
528 inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
529   const {
530   return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
531 }
532
533 inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
534   return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
535 }
536
537 inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
538   const {
539   return OwningObject->getRelocationValueString(RelocationPimpl, Result);
540 }
541
542 inline error_code RelocationRef::getHidden(bool &Result) const {
543   return OwningObject->getRelocationHidden(RelocationPimpl, Result);
544 }
545 // Inline function definitions.
546 inline LibraryRef::LibraryRef(DataRefImpl LibraryP, const ObjectFile *Owner)
547   : LibraryPimpl(LibraryP)
548   , OwningObject(Owner) {}
549
550 inline bool LibraryRef::operator==(const LibraryRef &Other) const {
551   return LibraryPimpl == Other.LibraryPimpl;
552 }
553
554 inline bool LibraryRef::operator <(const LibraryRef &Other) const {
555   return LibraryPimpl < Other.LibraryPimpl;
556 }
557
558 inline error_code LibraryRef::getNext(LibraryRef &Result) const {
559   return OwningObject->getLibraryNext(LibraryPimpl, Result);
560 }
561
562 inline error_code LibraryRef::getPath(StringRef &Result) const {
563   return OwningObject->getLibraryPath(LibraryPimpl, Result);
564 }
565
566 } // end namespace object
567 } // end namespace llvm
568
569 #endif