Change relocation API to be per section. This time without breaking GCC.
[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 /// SymbolRef - This is a value type class that represents a single symbol in
82 /// the list of symbols in the object file.
83 class SymbolRef {
84   friend class SectionRef;
85   DataRefImpl SymbolPimpl;
86   const ObjectFile *OwningObject;
87
88 public:
89   SymbolRef() : OwningObject(NULL) {
90     std::memset(&SymbolPimpl, 0, sizeof(SymbolPimpl));
91   }
92
93   enum SymbolType {
94     ST_Function,
95     ST_Data,
96     ST_External,    // Defined in another object file
97     ST_Other
98   };
99
100   SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
101
102   bool operator==(const SymbolRef &Other) const;
103
104   error_code getNext(SymbolRef &Result) const;
105
106   error_code getName(StringRef &Result) const;
107   error_code getAddress(uint64_t &Result) const;
108   error_code getOffset(uint64_t &Result) const;
109   error_code getSize(uint64_t &Result) const;
110   error_code getSymbolType(SymbolRef::SymbolType &Result) const;
111
112   /// Returns the ascii char that should be displayed in a symbol table dump via
113   /// nm for this symbol.
114   error_code getNMTypeChar(char &Result) const;
115
116   /// Returns true for symbols that are internal to the object file format such
117   /// as section symbols.
118   error_code isInternal(bool &Result) const;
119
120   /// Returns true for symbols that can be used in another objects,
121   /// such as library functions
122   error_code isGlobal(bool &Result) const;
123 };
124 typedef content_iterator<SymbolRef> symbol_iterator;
125
126 /// RelocationRef - This is a value type class that represents a single
127 /// relocation in the list of relocations in the object file.
128 class RelocationRef {
129   DataRefImpl RelocationPimpl;
130   const ObjectFile *OwningObject;
131
132 public:
133   RelocationRef() : OwningObject(NULL) {
134     std::memset(&RelocationPimpl, 0, sizeof(RelocationPimpl));
135   }
136
137   RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
138
139   bool operator==(const RelocationRef &Other) const;
140
141   error_code getNext(RelocationRef &Result) const;
142
143   error_code getAddress(uint64_t &Result) const;
144   error_code getSymbol(SymbolRef &Result) const;
145   error_code getType(uint32_t &Result) const;
146
147   /// @brief Get a string that represents the type of this relocation.
148   ///
149   /// This is for display purposes only.
150   error_code getTypeName(SmallVectorImpl<char> &Result) const;
151   error_code getAdditionalInfo(int64_t &Result) const;
152
153   /// @brief Get a string that represents the calculation of the value of this
154   ///        relocation.
155   ///
156   /// This is for display purposes only.
157   error_code getValueString(SmallVectorImpl<char> &Result) const;
158 };
159 typedef content_iterator<RelocationRef> relocation_iterator;
160
161 /// SectionRef - This is a value type class that represents a single section in
162 /// the list of sections in the object file.
163 class SectionRef {
164   friend class SymbolRef;
165   DataRefImpl SectionPimpl;
166   const ObjectFile *OwningObject;
167
168 public:
169   SectionRef() : OwningObject(NULL) {
170     std::memset(&SectionPimpl, 0, sizeof(SectionPimpl));
171   }
172
173   SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
174
175   bool operator==(const SectionRef &Other) const;
176
177   error_code getNext(SectionRef &Result) const;
178
179   error_code getName(StringRef &Result) const;
180   error_code getAddress(uint64_t &Result) const;
181   error_code getSize(uint64_t &Result) const;
182   error_code getContents(StringRef &Result) const;
183
184   // FIXME: Move to the normalization layer when it's created.
185   error_code isText(bool &Result) const;
186   error_code isData(bool &Result) const;
187   error_code isBSS(bool &Result) const;
188
189   error_code containsSymbol(SymbolRef S, bool &Result) const;
190
191   relocation_iterator begin_relocations() const;
192   relocation_iterator end_relocations() const;
193 };
194 typedef content_iterator<SectionRef> section_iterator;
195
196 const uint64_t UnknownAddressOrSize = ~0ULL;
197
198 /// ObjectFile - This class is the base class for all object file types.
199 /// Concrete instances of this object are created by createObjectFile, which
200 /// figure out which type to create.
201 class ObjectFile : public Binary {
202 private:
203   ObjectFile(); // = delete
204   ObjectFile(const ObjectFile &other); // = delete
205
206 protected:
207   ObjectFile(unsigned int Type, MemoryBuffer *source, error_code &ec);
208
209   const uint8_t *base() const {
210     return reinterpret_cast<const uint8_t *>(Data->getBufferStart());
211   }
212
213   // These functions are for SymbolRef to call internally. The main goal of
214   // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
215   // entry in the memory mapped object file. SymbolPimpl cannot contain any
216   // virtual functions because then it could not point into the memory mapped
217   // file.
218   //
219   // Implementations assume that the DataRefImpl is valid and has not been
220   // modified externally. It's UB otherwise.
221   friend class SymbolRef;
222   virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const = 0;
223   virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const = 0;
224   virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const =0;
225   virtual error_code getSymbolOffset(DataRefImpl Symb, uint64_t &Res) const =0;
226   virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const = 0;
227   virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const = 0;
228   virtual error_code isSymbolInternal(DataRefImpl Symb, bool &Res) const = 0;
229   virtual error_code isSymbolGlobal(DataRefImpl Symb, bool &Res) const = 0;
230   virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::SymbolType &Res) const = 0;
231
232   // Same as above for SectionRef.
233   friend class SectionRef;
234   virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const = 0;
235   virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const = 0;
236   virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const =0;
237   virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const = 0;
238   virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res)const=0;
239   virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const = 0;
240   virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const = 0;
241   virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const = 0;
242   virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
243                                            bool &Result) const = 0;
244   virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const = 0;
245   virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const = 0;
246
247
248   // Same as above for RelocationRef.
249   friend class RelocationRef;
250   virtual error_code getRelocationNext(DataRefImpl Rel,
251                                        RelocationRef &Res) const = 0;
252   virtual error_code getRelocationAddress(DataRefImpl Rel,
253                                           uint64_t &Res) const =0;
254   virtual error_code getRelocationSymbol(DataRefImpl Rel,
255                                          SymbolRef &Res) const = 0;
256   virtual error_code getRelocationType(DataRefImpl Rel,
257                                        uint32_t &Res) const = 0;
258   virtual error_code getRelocationTypeName(DataRefImpl Rel,
259                                        SmallVectorImpl<char> &Result) const = 0;
260   virtual error_code getRelocationAdditionalInfo(DataRefImpl Rel,
261                                                  int64_t &Res) const = 0;
262   virtual error_code getRelocationValueString(DataRefImpl Rel,
263                                        SmallVectorImpl<char> &Result) const = 0;
264
265 public:
266
267   virtual symbol_iterator begin_symbols() const = 0;
268   virtual symbol_iterator end_symbols() const = 0;
269
270   virtual section_iterator begin_sections() const = 0;
271   virtual section_iterator end_sections() const = 0;
272
273   /// @brief The number of bytes used to represent an address in this object
274   ///        file format.
275   virtual uint8_t getBytesInAddress() const = 0;
276
277   virtual StringRef getFileFormatName() const = 0;
278   virtual /* Triple::ArchType */ unsigned getArch() const = 0;
279
280   /// @returns Pointer to ObjectFile subclass to handle this type of object.
281   /// @param ObjectPath The path to the object file. ObjectPath.isObject must
282   ///        return true.
283   /// @brief Create ObjectFile from path.
284   static ObjectFile *createObjectFile(StringRef ObjectPath);
285   static ObjectFile *createObjectFile(MemoryBuffer *Object);
286
287   static inline bool classof(const Binary *v) {
288     return v->getType() >= isObject &&
289            v->getType() < lastObject;
290   }
291   static inline bool classof(const ObjectFile *v) { return true; }
292
293 public:
294   static ObjectFile *createCOFFObjectFile(MemoryBuffer *Object);
295   static ObjectFile *createELFObjectFile(MemoryBuffer *Object);
296   static ObjectFile *createMachOObjectFile(MemoryBuffer *Object);
297 };
298
299 // Inline function definitions.
300 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
301   : SymbolPimpl(SymbolP)
302   , OwningObject(Owner) {}
303
304 inline bool SymbolRef::operator==(const SymbolRef &Other) const {
305   return SymbolPimpl == Other.SymbolPimpl;
306 }
307
308 inline error_code SymbolRef::getNext(SymbolRef &Result) const {
309   return OwningObject->getSymbolNext(SymbolPimpl, Result);
310 }
311
312 inline error_code SymbolRef::getName(StringRef &Result) const {
313   return OwningObject->getSymbolName(SymbolPimpl, Result);
314 }
315
316 inline error_code SymbolRef::getAddress(uint64_t &Result) const {
317   return OwningObject->getSymbolAddress(SymbolPimpl, Result);
318 }
319
320 inline error_code SymbolRef::getOffset(uint64_t &Result) const {
321   return OwningObject->getSymbolOffset(SymbolPimpl, Result);
322 }
323
324 inline error_code SymbolRef::getSize(uint64_t &Result) const {
325   return OwningObject->getSymbolSize(SymbolPimpl, Result);
326 }
327
328 inline error_code SymbolRef::getNMTypeChar(char &Result) const {
329   return OwningObject->getSymbolNMTypeChar(SymbolPimpl, Result);
330 }
331
332 inline error_code SymbolRef::isInternal(bool &Result) const {
333   return OwningObject->isSymbolInternal(SymbolPimpl, Result);
334 }
335
336 inline error_code SymbolRef::isGlobal(bool &Result) const {
337   return OwningObject->isSymbolGlobal(SymbolPimpl, Result);
338 }
339
340 inline error_code SymbolRef::getSymbolType(SymbolRef::SymbolType &Result) const {
341   return OwningObject->getSymbolType(SymbolPimpl, Result);
342 }
343
344
345 /// SectionRef
346 inline SectionRef::SectionRef(DataRefImpl SectionP,
347                               const ObjectFile *Owner)
348   : SectionPimpl(SectionP)
349   , OwningObject(Owner) {}
350
351 inline bool SectionRef::operator==(const SectionRef &Other) const {
352   return SectionPimpl == Other.SectionPimpl;
353 }
354
355 inline error_code SectionRef::getNext(SectionRef &Result) const {
356   return OwningObject->getSectionNext(SectionPimpl, Result);
357 }
358
359 inline error_code SectionRef::getName(StringRef &Result) const {
360   return OwningObject->getSectionName(SectionPimpl, Result);
361 }
362
363 inline error_code SectionRef::getAddress(uint64_t &Result) const {
364   return OwningObject->getSectionAddress(SectionPimpl, Result);
365 }
366
367 inline error_code SectionRef::getSize(uint64_t &Result) const {
368   return OwningObject->getSectionSize(SectionPimpl, Result);
369 }
370
371 inline error_code SectionRef::getContents(StringRef &Result) const {
372   return OwningObject->getSectionContents(SectionPimpl, Result);
373 }
374
375 inline error_code SectionRef::isText(bool &Result) const {
376   return OwningObject->isSectionText(SectionPimpl, Result);
377 }
378
379 inline error_code SectionRef::isData(bool &Result) const {
380   return OwningObject->isSectionData(SectionPimpl, Result);
381 }
382
383 inline error_code SectionRef::isBSS(bool &Result) const {
384   return OwningObject->isSectionBSS(SectionPimpl, Result);
385 }
386
387 inline error_code SectionRef::containsSymbol(SymbolRef S, bool &Result) const {
388   return OwningObject->sectionContainsSymbol(SectionPimpl, S.SymbolPimpl,
389                                              Result);
390 }
391
392 inline relocation_iterator SectionRef::begin_relocations() const {
393   return OwningObject->getSectionRelBegin(SectionPimpl);
394 }
395
396 inline relocation_iterator SectionRef::end_relocations() const {
397   return OwningObject->getSectionRelEnd(SectionPimpl);
398 }
399
400
401 /// RelocationRef
402 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
403                               const ObjectFile *Owner)
404   : RelocationPimpl(RelocationP)
405   , OwningObject(Owner) {}
406
407 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
408   return RelocationPimpl == Other.RelocationPimpl;
409 }
410
411 inline error_code RelocationRef::getNext(RelocationRef &Result) const {
412   return OwningObject->getRelocationNext(RelocationPimpl, Result);
413 }
414
415 inline error_code RelocationRef::getAddress(uint64_t &Result) const {
416   return OwningObject->getRelocationAddress(RelocationPimpl, Result);
417 }
418
419 inline error_code RelocationRef::getSymbol(SymbolRef &Result) const {
420   return OwningObject->getRelocationSymbol(RelocationPimpl, Result);
421 }
422
423 inline error_code RelocationRef::getType(uint32_t &Result) const {
424   return OwningObject->getRelocationType(RelocationPimpl, Result);
425 }
426
427 inline error_code RelocationRef::getTypeName(SmallVectorImpl<char> &Result)
428   const {
429   return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
430 }
431
432 inline error_code RelocationRef::getAdditionalInfo(int64_t &Result) const {
433   return OwningObject->getRelocationAdditionalInfo(RelocationPimpl, Result);
434 }
435
436 inline error_code RelocationRef::getValueString(SmallVectorImpl<char> &Result)
437   const {
438   return OwningObject->getRelocationValueString(RelocationPimpl, Result);
439 }
440
441 } // end namespace object
442 } // end namespace llvm
443
444 #endif