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