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