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