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