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