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