Remove getRelocationAddress.
[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/SymbolicFile.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 class COFFObjectFile;
31 class MachOObjectFile;
32
33 class SymbolRef;
34 class symbol_iterator;
35 class SectionRef;
36 typedef content_iterator<SectionRef> section_iterator;
37
38 /// This is a value type class that represents a single relocation in the list
39 /// of relocations in the object file.
40 class RelocationRef {
41   DataRefImpl RelocationPimpl;
42   const ObjectFile *OwningObject;
43
44 public:
45   RelocationRef() : OwningObject(nullptr) { }
46
47   RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
48
49   bool operator==(const RelocationRef &Other) const;
50
51   void moveNext();
52
53   uint64_t getOffset() const;
54   symbol_iterator getSymbol() const;
55   uint64_t getType() const;
56
57   /// @brief Get a string that represents the type of this relocation.
58   ///
59   /// This is for display purposes only.
60   void getTypeName(SmallVectorImpl<char> &Result) const;
61
62   DataRefImpl getRawDataRefImpl() const;
63   const ObjectFile *getObject() const;
64 };
65 typedef content_iterator<RelocationRef> relocation_iterator;
66
67 /// This is a value type class that represents a single section in the list of
68 /// sections in the object file.
69 class SectionRef {
70   friend class SymbolRef;
71   DataRefImpl SectionPimpl;
72   const ObjectFile *OwningObject;
73
74 public:
75   SectionRef() : OwningObject(nullptr) { }
76
77   SectionRef(DataRefImpl SectionP, const ObjectFile *Owner);
78
79   bool operator==(const SectionRef &Other) const;
80   bool operator!=(const SectionRef &Other) const;
81   bool operator<(const SectionRef &Other) const;
82
83   void moveNext();
84
85   std::error_code getName(StringRef &Result) const;
86   uint64_t getAddress() const;
87   uint64_t getSize() const;
88   std::error_code getContents(StringRef &Result) const;
89
90   /// @brief Get the alignment of this section as the actual value (not log 2).
91   uint64_t getAlignment() const;
92
93   bool isText() const;
94   bool isData() const;
95   bool isBSS() const;
96   bool isVirtual() const;
97
98   bool containsSymbol(SymbolRef S) const;
99
100   relocation_iterator relocation_begin() const;
101   relocation_iterator relocation_end() const;
102   iterator_range<relocation_iterator> relocations() const {
103     return iterator_range<relocation_iterator>(relocation_begin(),
104                                                relocation_end());
105   }
106   section_iterator getRelocatedSection() const;
107
108   DataRefImpl getRawDataRefImpl() const;
109   const ObjectFile *getObject() const;
110 };
111
112 /// This is a value type class that represents a single symbol in the list of
113 /// symbols in the object file.
114 class SymbolRef : public BasicSymbolRef {
115   friend class SectionRef;
116
117 public:
118   SymbolRef() : BasicSymbolRef() {}
119
120   enum Type {
121     ST_Unknown, // Type not specified
122     ST_Data,
123     ST_Debug,
124     ST_File,
125     ST_Function,
126     ST_Other
127   };
128
129   SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
130   SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
131     assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
132   }
133
134   ErrorOr<StringRef> getName() const;
135   /// Returns the symbol virtual address (i.e. address at which it will be
136   /// mapped).
137   ErrorOr<uint64_t> getAddress() const;
138
139   /// Return the value of the symbol depending on the object this can be an
140   /// offset or a virtual address.
141   uint64_t getValue() const;
142
143   /// @brief Get the alignment of this symbol as the actual value (not log 2).
144   uint32_t getAlignment() const;
145   uint64_t getCommonSize() const;
146   SymbolRef::Type getType() const;
147
148   /// @brief Get section this symbol is defined in reference to. Result is
149   /// end_sections() if it is undefined or is an absolute symbol.
150   std::error_code getSection(section_iterator &Result) const;
151
152   const ObjectFile *getObject() const;
153 };
154
155 class symbol_iterator : public basic_symbol_iterator {
156 public:
157   symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
158   symbol_iterator(const basic_symbol_iterator &B)
159       : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
160                                         cast<ObjectFile>(B->getObject()))) {}
161
162   const SymbolRef *operator->() const {
163     const BasicSymbolRef &P = basic_symbol_iterator::operator *();
164     return static_cast<const SymbolRef*>(&P);
165   }
166
167   const SymbolRef &operator*() const {
168     const BasicSymbolRef &P = basic_symbol_iterator::operator *();
169     return static_cast<const SymbolRef&>(P);
170   }
171 };
172
173 /// This class is the base class for all object file types. Concrete instances
174 /// of this object are created by createObjectFile, which figures out which type
175 /// to create.
176 class ObjectFile : public SymbolicFile {
177   virtual void anchor();
178   ObjectFile() = delete;
179   ObjectFile(const ObjectFile &other) = delete;
180
181 protected:
182   ObjectFile(unsigned int Type, MemoryBufferRef Source);
183
184   const uint8_t *base() const {
185     return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
186   }
187
188   // These functions are for SymbolRef to call internally. The main goal of
189   // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
190   // entry in the memory mapped object file. SymbolPimpl cannot contain any
191   // virtual functions because then it could not point into the memory mapped
192   // file.
193   //
194   // Implementations assume that the DataRefImpl is valid and has not been
195   // modified externally. It's UB otherwise.
196   friend class SymbolRef;
197   virtual ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
198   std::error_code printSymbolName(raw_ostream &OS,
199                                   DataRefImpl Symb) const override;
200   virtual ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
201   virtual uint64_t getSymbolValue(DataRefImpl Symb) const = 0;
202   virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
203   virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
204   virtual SymbolRef::Type getSymbolType(DataRefImpl Symb) const = 0;
205   virtual std::error_code getSymbolSection(DataRefImpl Symb,
206                                            section_iterator &Res) const = 0;
207
208   // Same as above for SectionRef.
209   friend class SectionRef;
210   virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
211   virtual std::error_code getSectionName(DataRefImpl Sec,
212                                          StringRef &Res) const = 0;
213   virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
214   virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
215   virtual std::error_code getSectionContents(DataRefImpl Sec,
216                                              StringRef &Res) const = 0;
217   virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
218   virtual bool isSectionText(DataRefImpl Sec) const = 0;
219   virtual bool isSectionData(DataRefImpl Sec) const = 0;
220   virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
221   // A section is 'virtual' if its contents aren't present in the object image.
222   virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
223   virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
224   virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
225   virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
226
227   // Same as above for RelocationRef.
228   friend class RelocationRef;
229   virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
230   virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
231   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
232   virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
233   virtual void getRelocationTypeName(DataRefImpl Rel,
234                                      SmallVectorImpl<char> &Result) const = 0;
235
236 public:
237   uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
238     assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
239     return getCommonSymbolSizeImpl(Symb);
240   }
241
242   typedef iterator_range<symbol_iterator> symbol_iterator_range;
243   symbol_iterator_range symbols() const {
244     return symbol_iterator_range(symbol_begin(), symbol_end());
245   }
246
247   virtual section_iterator section_begin() const = 0;
248   virtual section_iterator section_end() const = 0;
249
250   typedef iterator_range<section_iterator> section_iterator_range;
251   section_iterator_range sections() const {
252     return section_iterator_range(section_begin(), section_end());
253   }
254
255   /// @brief The number of bytes used to represent an address in this object
256   ///        file format.
257   virtual uint8_t getBytesInAddress() const = 0;
258
259   virtual StringRef getFileFormatName() const = 0;
260   virtual /* Triple::ArchType */ unsigned getArch() const = 0;
261
262   /// Returns platform-specific object flags, if any.
263   virtual std::error_code getPlatformFlags(unsigned &Result) const {
264     Result = 0;
265     return object_error::invalid_file_type;
266   }
267
268   /// True if this is a relocatable object (.o/.obj).
269   virtual bool isRelocatableObject() const = 0;
270
271   /// @returns Pointer to ObjectFile subclass to handle this type of object.
272   /// @param ObjectPath The path to the object file. ObjectPath.isObject must
273   ///        return true.
274   /// @brief Create ObjectFile from path.
275   static ErrorOr<OwningBinary<ObjectFile>>
276   createObjectFile(StringRef ObjectPath);
277
278   static ErrorOr<std::unique_ptr<ObjectFile>>
279   createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
280   static ErrorOr<std::unique_ptr<ObjectFile>>
281   createObjectFile(MemoryBufferRef Object) {
282     return createObjectFile(Object, sys::fs::file_magic::unknown);
283   }
284
285
286   static inline bool classof(const Binary *v) {
287     return v->isObject();
288   }
289
290   static ErrorOr<std::unique_ptr<COFFObjectFile>>
291   createCOFFObjectFile(MemoryBufferRef Object);
292
293   static ErrorOr<std::unique_ptr<ObjectFile>>
294   createELFObjectFile(MemoryBufferRef Object);
295
296   static ErrorOr<std::unique_ptr<MachOObjectFile>>
297   createMachOObjectFile(MemoryBufferRef Object);
298 };
299
300 // Inline function definitions.
301 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
302     : BasicSymbolRef(SymbolP, Owner) {}
303
304 inline ErrorOr<StringRef> SymbolRef::getName() const {
305   return getObject()->getSymbolName(getRawDataRefImpl());
306 }
307
308 inline ErrorOr<uint64_t> SymbolRef::getAddress() const {
309   return getObject()->getSymbolAddress(getRawDataRefImpl());
310 }
311
312 inline uint64_t SymbolRef::getValue() const {
313   return getObject()->getSymbolValue(getRawDataRefImpl());
314 }
315
316 inline uint32_t SymbolRef::getAlignment() const {
317   return getObject()->getSymbolAlignment(getRawDataRefImpl());
318 }
319
320 inline uint64_t SymbolRef::getCommonSize() const {
321   return getObject()->getCommonSymbolSize(getRawDataRefImpl());
322 }
323
324 inline std::error_code SymbolRef::getSection(section_iterator &Result) const {
325   return getObject()->getSymbolSection(getRawDataRefImpl(), Result);
326 }
327
328 inline SymbolRef::Type SymbolRef::getType() const {
329   return getObject()->getSymbolType(getRawDataRefImpl());
330 }
331
332 inline const ObjectFile *SymbolRef::getObject() const {
333   const SymbolicFile *O = BasicSymbolRef::getObject();
334   return cast<ObjectFile>(O);
335 }
336
337
338 /// SectionRef
339 inline SectionRef::SectionRef(DataRefImpl SectionP,
340                               const ObjectFile *Owner)
341   : SectionPimpl(SectionP)
342   , OwningObject(Owner) {}
343
344 inline bool SectionRef::operator==(const SectionRef &Other) const {
345   return SectionPimpl == Other.SectionPimpl;
346 }
347
348 inline bool SectionRef::operator!=(const SectionRef &Other) const {
349   return SectionPimpl != Other.SectionPimpl;
350 }
351
352 inline bool SectionRef::operator<(const SectionRef &Other) const {
353   return SectionPimpl < Other.SectionPimpl;
354 }
355
356 inline void SectionRef::moveNext() {
357   return OwningObject->moveSectionNext(SectionPimpl);
358 }
359
360 inline std::error_code SectionRef::getName(StringRef &Result) const {
361   return OwningObject->getSectionName(SectionPimpl, Result);
362 }
363
364 inline uint64_t SectionRef::getAddress() const {
365   return OwningObject->getSectionAddress(SectionPimpl);
366 }
367
368 inline uint64_t SectionRef::getSize() const {
369   return OwningObject->getSectionSize(SectionPimpl);
370 }
371
372 inline std::error_code SectionRef::getContents(StringRef &Result) const {
373   return OwningObject->getSectionContents(SectionPimpl, Result);
374 }
375
376 inline uint64_t SectionRef::getAlignment() const {
377   return OwningObject->getSectionAlignment(SectionPimpl);
378 }
379
380 inline bool SectionRef::isText() const {
381   return OwningObject->isSectionText(SectionPimpl);
382 }
383
384 inline bool SectionRef::isData() const {
385   return OwningObject->isSectionData(SectionPimpl);
386 }
387
388 inline bool SectionRef::isBSS() const {
389   return OwningObject->isSectionBSS(SectionPimpl);
390 }
391
392 inline bool SectionRef::isVirtual() const {
393   return OwningObject->isSectionVirtual(SectionPimpl);
394 }
395
396 inline relocation_iterator SectionRef::relocation_begin() const {
397   return OwningObject->section_rel_begin(SectionPimpl);
398 }
399
400 inline relocation_iterator SectionRef::relocation_end() const {
401   return OwningObject->section_rel_end(SectionPimpl);
402 }
403
404 inline section_iterator SectionRef::getRelocatedSection() const {
405   return OwningObject->getRelocatedSection(SectionPimpl);
406 }
407
408 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
409   return SectionPimpl;
410 }
411
412 inline const ObjectFile *SectionRef::getObject() const {
413   return OwningObject;
414 }
415
416 /// RelocationRef
417 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
418                               const ObjectFile *Owner)
419   : RelocationPimpl(RelocationP)
420   , OwningObject(Owner) {}
421
422 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
423   return RelocationPimpl == Other.RelocationPimpl;
424 }
425
426 inline void RelocationRef::moveNext() {
427   return OwningObject->moveRelocationNext(RelocationPimpl);
428 }
429
430 inline uint64_t RelocationRef::getOffset() const {
431   return OwningObject->getRelocationOffset(RelocationPimpl);
432 }
433
434 inline symbol_iterator RelocationRef::getSymbol() const {
435   return OwningObject->getRelocationSymbol(RelocationPimpl);
436 }
437
438 inline uint64_t RelocationRef::getType() const {
439   return OwningObject->getRelocationType(RelocationPimpl);
440 }
441
442 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
443   return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
444 }
445
446 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
447   return RelocationPimpl;
448 }
449
450 inline const ObjectFile *RelocationRef::getObject() const {
451   return OwningObject;
452 }
453
454
455 } // end namespace object
456 } // end namespace llvm
457
458 #endif