]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/include/llvm/Object/ObjectFile.h
Merge ^/head r313644 through r313895.
[FreeBSD/FreeBSD.git] / contrib / llvm / 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/MC/SubtargetFeature.h"
19 #include "llvm/Object/SymbolicFile.h"
20 #include "llvm/Support/DataTypes.h"
21 #include "llvm/Support/ErrorHandling.h"
22 #include "llvm/Support/FileSystem.h"
23 #include "llvm/Support/MemoryBuffer.h"
24 #include <cstring>
25
26 namespace llvm {
27 namespace object {
28
29 class ObjectFile;
30 class COFFObjectFile;
31 class MachOObjectFile;
32 class WasmObjectFile;
33
34 class SymbolRef;
35 class symbol_iterator;
36 class SectionRef;
37 typedef content_iterator<SectionRef> section_iterator;
38
39 /// This is a value type class that represents a single relocation in the list
40 /// of relocations in the object file.
41 class RelocationRef {
42   DataRefImpl RelocationPimpl;
43   const ObjectFile *OwningObject;
44
45 public:
46   RelocationRef() : OwningObject(nullptr) { }
47
48   RelocationRef(DataRefImpl RelocationP, const ObjectFile *Owner);
49
50   bool operator==(const RelocationRef &Other) const;
51
52   void moveNext();
53
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 isCompressed() const;
95   bool isText() const;
96   bool isData() const;
97   bool isBSS() const;
98   bool isVirtual() const;
99   bool isBitcode() const;
100
101   bool containsSymbol(SymbolRef S) const;
102
103   relocation_iterator relocation_begin() const;
104   relocation_iterator relocation_end() const;
105   iterator_range<relocation_iterator> relocations() const {
106     return make_range(relocation_begin(), 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   Expected<StringRef> getName() const;
137   /// Returns the symbol virtual address (i.e. address at which it will be
138   /// mapped).
139   Expected<uint64_t> getAddress() 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   Expected<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   Expected<section_iterator> getSection() 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 Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
200   std::error_code printSymbolName(raw_ostream &OS,
201                                   DataRefImpl Symb) const override;
202   virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
203   virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
204   virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
205   virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
206   virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
207   virtual Expected<section_iterator>
208   getSymbolSection(DataRefImpl Symb) const = 0;
209
210   // Same as above for SectionRef.
211   friend class SectionRef;
212   virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
213   virtual std::error_code getSectionName(DataRefImpl Sec,
214                                          StringRef &Res) const = 0;
215   virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
216   virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
217   virtual std::error_code getSectionContents(DataRefImpl Sec,
218                                              StringRef &Res) const = 0;
219   virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
220   virtual bool isSectionCompressed(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 isSectionBitcode(DataRefImpl Sec) const;
227   virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
228   virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
229   virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
230
231   // Same as above for RelocationRef.
232   friend class RelocationRef;
233   virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
234   virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
235   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
236   virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
237   virtual void getRelocationTypeName(DataRefImpl Rel,
238                                      SmallVectorImpl<char> &Result) const = 0;
239
240   uint64_t getSymbolValue(DataRefImpl Symb) const;
241
242 public:
243   uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
244     assert(getSymbolFlags(Symb) & SymbolRef::SF_Common);
245     return getCommonSymbolSizeImpl(Symb);
246   }
247
248   typedef iterator_range<symbol_iterator> symbol_iterator_range;
249   symbol_iterator_range symbols() const {
250     return symbol_iterator_range(symbol_begin(), symbol_end());
251   }
252
253   virtual section_iterator section_begin() const = 0;
254   virtual section_iterator section_end() const = 0;
255
256   typedef iterator_range<section_iterator> section_iterator_range;
257   section_iterator_range sections() const {
258     return section_iterator_range(section_begin(), section_end());
259   }
260
261   /// @brief The number of bytes used to represent an address in this object
262   ///        file format.
263   virtual uint8_t getBytesInAddress() const = 0;
264
265   virtual StringRef getFileFormatName() const = 0;
266   virtual /* Triple::ArchType */ unsigned getArch() const = 0;
267   virtual SubtargetFeatures getFeatures() 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 Expected<OwningBinary<ObjectFile>>
283   createObjectFile(StringRef ObjectPath);
284
285   static Expected<std::unique_ptr<ObjectFile>>
286   createObjectFile(MemoryBufferRef Object, sys::fs::file_magic Type);
287   static Expected<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 Expected<std::unique_ptr<MachOObjectFile>>
304   createMachOObjectFile(MemoryBufferRef Object,
305                         uint32_t UniversalCputype = 0,
306                         uint32_t UniversalIndex = 0);
307
308   static Expected<std::unique_ptr<WasmObjectFile>>
309   createWasmObjectFile(MemoryBufferRef Object);
310 };
311
312 // Inline function definitions.
313 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
314     : BasicSymbolRef(SymbolP, Owner) {}
315
316 inline Expected<StringRef> SymbolRef::getName() const {
317   return getObject()->getSymbolName(getRawDataRefImpl());
318 }
319
320 inline Expected<uint64_t> SymbolRef::getAddress() const {
321   return getObject()->getSymbolAddress(getRawDataRefImpl());
322 }
323
324 inline uint64_t SymbolRef::getValue() const {
325   return getObject()->getSymbolValue(getRawDataRefImpl());
326 }
327
328 inline uint32_t SymbolRef::getAlignment() const {
329   return getObject()->getSymbolAlignment(getRawDataRefImpl());
330 }
331
332 inline uint64_t SymbolRef::getCommonSize() const {
333   return getObject()->getCommonSymbolSize(getRawDataRefImpl());
334 }
335
336 inline Expected<section_iterator> SymbolRef::getSection() const {
337   return getObject()->getSymbolSection(getRawDataRefImpl());
338 }
339
340 inline Expected<SymbolRef::Type> SymbolRef::getType() const {
341   return getObject()->getSymbolType(getRawDataRefImpl());
342 }
343
344 inline const ObjectFile *SymbolRef::getObject() const {
345   const SymbolicFile *O = BasicSymbolRef::getObject();
346   return cast<ObjectFile>(O);
347 }
348
349
350 /// SectionRef
351 inline SectionRef::SectionRef(DataRefImpl SectionP,
352                               const ObjectFile *Owner)
353   : SectionPimpl(SectionP)
354   , OwningObject(Owner) {}
355
356 inline bool SectionRef::operator==(const SectionRef &Other) const {
357   return SectionPimpl == Other.SectionPimpl;
358 }
359
360 inline bool SectionRef::operator!=(const SectionRef &Other) const {
361   return SectionPimpl != Other.SectionPimpl;
362 }
363
364 inline bool SectionRef::operator<(const SectionRef &Other) const {
365   return SectionPimpl < Other.SectionPimpl;
366 }
367
368 inline void SectionRef::moveNext() {
369   return OwningObject->moveSectionNext(SectionPimpl);
370 }
371
372 inline std::error_code SectionRef::getName(StringRef &Result) const {
373   return OwningObject->getSectionName(SectionPimpl, Result);
374 }
375
376 inline uint64_t SectionRef::getAddress() const {
377   return OwningObject->getSectionAddress(SectionPimpl);
378 }
379
380 inline uint64_t SectionRef::getSize() const {
381   return OwningObject->getSectionSize(SectionPimpl);
382 }
383
384 inline std::error_code SectionRef::getContents(StringRef &Result) const {
385   return OwningObject->getSectionContents(SectionPimpl, Result);
386 }
387
388 inline uint64_t SectionRef::getAlignment() const {
389   return OwningObject->getSectionAlignment(SectionPimpl);
390 }
391
392 inline bool SectionRef::isCompressed() const {
393   return OwningObject->isSectionCompressed(SectionPimpl);
394 }
395
396 inline bool SectionRef::isText() const {
397   return OwningObject->isSectionText(SectionPimpl);
398 }
399
400 inline bool SectionRef::isData() const {
401   return OwningObject->isSectionData(SectionPimpl);
402 }
403
404 inline bool SectionRef::isBSS() const {
405   return OwningObject->isSectionBSS(SectionPimpl);
406 }
407
408 inline bool SectionRef::isVirtual() const {
409   return OwningObject->isSectionVirtual(SectionPimpl);
410 }
411
412 inline bool SectionRef::isBitcode() const {
413   return OwningObject->isSectionBitcode(SectionPimpl);
414 }
415
416 inline relocation_iterator SectionRef::relocation_begin() const {
417   return OwningObject->section_rel_begin(SectionPimpl);
418 }
419
420 inline relocation_iterator SectionRef::relocation_end() const {
421   return OwningObject->section_rel_end(SectionPimpl);
422 }
423
424 inline section_iterator SectionRef::getRelocatedSection() const {
425   return OwningObject->getRelocatedSection(SectionPimpl);
426 }
427
428 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
429   return SectionPimpl;
430 }
431
432 inline const ObjectFile *SectionRef::getObject() const {
433   return OwningObject;
434 }
435
436 /// RelocationRef
437 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
438                               const ObjectFile *Owner)
439   : RelocationPimpl(RelocationP)
440   , OwningObject(Owner) {}
441
442 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
443   return RelocationPimpl == Other.RelocationPimpl;
444 }
445
446 inline void RelocationRef::moveNext() {
447   return OwningObject->moveRelocationNext(RelocationPimpl);
448 }
449
450 inline uint64_t RelocationRef::getOffset() const {
451   return OwningObject->getRelocationOffset(RelocationPimpl);
452 }
453
454 inline symbol_iterator RelocationRef::getSymbol() const {
455   return OwningObject->getRelocationSymbol(RelocationPimpl);
456 }
457
458 inline uint64_t RelocationRef::getType() const {
459   return OwningObject->getRelocationType(RelocationPimpl);
460 }
461
462 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
463   return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
464 }
465
466 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
467   return RelocationPimpl;
468 }
469
470 inline const ObjectFile *RelocationRef::getObject() const {
471   return OwningObject;
472 }
473
474
475 } // end namespace object
476 } // end namespace llvm
477
478 #endif