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