1 //===- ObjectFile.h - File format independent object file -------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares a file format independent ObjectFile class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_OBJECTFILE_H
14 #define LLVM_OBJECT_OBJECTFILE_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/Hashing.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/Triple.h"
20 #include "llvm/ADT/iterator_range.h"
21 #include "llvm/BinaryFormat/Magic.h"
22 #include "llvm/BinaryFormat/Swift.h"
23 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/Error.h"
25 #include "llvm/Object/SymbolicFile.h"
26 #include "llvm/Support/Casting.h"
27 #include "llvm/Support/Error.h"
28 #include "llvm/Support/MemoryBufferRef.h"
29 #include <cassert>
30 #include <cstdint>
31 #include <memory>
32 
33 namespace llvm {
34 
35 class SubtargetFeatures;
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   /// 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   Expected<StringRef> getName() const;
97   uint64_t getAddress() const;
98   uint64_t getIndex() const;
99   uint64_t getSize() const;
100   Expected<StringRef> getContents() const;
101 
102   /// Get the alignment of this section as the actual value (not log 2).
103   uint64_t getAlignment() const;
104 
105   bool isCompressed() const;
106   /// Whether this section contains instructions.
107   bool isText() const;
108   /// Whether this section contains data, not instructions.
109   bool isData() const;
110   /// Whether this section contains BSS uninitialized data.
111   bool isBSS() const;
112   bool isVirtual() const;
113   bool isBitcode() const;
114   bool isStripped() const;
115 
116   /// Whether this section will be placed in the text segment, according to the
117   /// Berkeley size format. This is true if the section is allocatable, and
118   /// contains either code or readonly data.
119   bool isBerkeleyText() const;
120   /// Whether this section will be placed in the data segment, according to the
121   /// Berkeley size format. This is true if the section is allocatable and
122   /// contains data (e.g. PROGBITS), but is not text.
123   bool isBerkeleyData() const;
124 
125   /// Whether this section is a debug section.
126   bool isDebugSection() const;
127 
128   bool containsSymbol(SymbolRef S) const;
129 
130   relocation_iterator relocation_begin() const;
131   relocation_iterator relocation_end() const;
132   iterator_range<relocation_iterator> relocations() const {
133     return make_range(relocation_begin(), relocation_end());
134   }
135 
136   /// Returns the related section if this section contains relocations. The
137   /// returned section may or may not have applied its relocations.
138   Expected<section_iterator> getRelocatedSection() const;
139 
140   DataRefImpl getRawDataRefImpl() const;
141   const ObjectFile *getObject() const;
142 };
143 
144 struct SectionedAddress {
145   const static uint64_t UndefSection = UINT64_MAX;
146 
147   uint64_t Address = 0;
148   uint64_t SectionIndex = UndefSection;
149 };
150 
151 inline bool operator<(const SectionedAddress &LHS,
152                       const SectionedAddress &RHS) {
153   return std::tie(LHS.SectionIndex, LHS.Address) <
154          std::tie(RHS.SectionIndex, RHS.Address);
155 }
156 
157 inline bool operator==(const SectionedAddress &LHS,
158                        const SectionedAddress &RHS) {
159   return std::tie(LHS.SectionIndex, LHS.Address) ==
160          std::tie(RHS.SectionIndex, RHS.Address);
161 }
162 
163 raw_ostream &operator<<(raw_ostream &OS, const SectionedAddress &Addr);
164 
165 /// This is a value type class that represents a single symbol in the list of
166 /// symbols in the object file.
167 class SymbolRef : public BasicSymbolRef {
168   friend class SectionRef;
169 
170 public:
171   enum Type {
172     ST_Unknown, // Type not specified
173     ST_Other,
174     ST_Data,
175     ST_Debug,
176     ST_File,
177     ST_Function,
178   };
179 
180   SymbolRef() = default;
181   SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner);
182   SymbolRef(const BasicSymbolRef &B) : BasicSymbolRef(B) {
183     assert(isa<ObjectFile>(BasicSymbolRef::getObject()));
184   }
185 
186   Expected<StringRef> getName() const;
187   /// Returns the symbol virtual address (i.e. address at which it will be
188   /// mapped).
189   Expected<uint64_t> getAddress() const;
190 
191   /// Return the value of the symbol depending on the object this can be an
192   /// offset or a virtual address.
193   Expected<uint64_t> getValue() const;
194 
195   /// Get the alignment of this symbol as the actual value (not log 2).
196   uint32_t getAlignment() const;
197   uint64_t getCommonSize() const;
198   Expected<SymbolRef::Type> getType() const;
199 
200   /// Get section this symbol is defined in reference to. Result is
201   /// end_sections() if it is undefined or is an absolute symbol.
202   Expected<section_iterator> getSection() const;
203 
204   const ObjectFile *getObject() const;
205 };
206 
207 class symbol_iterator : public basic_symbol_iterator {
208 public:
209   symbol_iterator(SymbolRef Sym) : basic_symbol_iterator(Sym) {}
210   symbol_iterator(const basic_symbol_iterator &B)
211       : basic_symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
212                                         cast<ObjectFile>(B->getObject()))) {}
213 
214   const SymbolRef *operator->() const {
215     const BasicSymbolRef &P = basic_symbol_iterator::operator *();
216     return static_cast<const SymbolRef*>(&P);
217   }
218 
219   const SymbolRef &operator*() const {
220     const BasicSymbolRef &P = basic_symbol_iterator::operator *();
221     return static_cast<const SymbolRef&>(P);
222   }
223 };
224 
225 /// This class is the base class for all object file types. Concrete instances
226 /// of this object are created by createObjectFile, which figures out which type
227 /// to create.
228 class ObjectFile : public SymbolicFile {
229   virtual void anchor();
230 
231 protected:
232   ObjectFile(unsigned int Type, MemoryBufferRef Source);
233 
234   const uint8_t *base() const {
235     return reinterpret_cast<const uint8_t *>(Data.getBufferStart());
236   }
237 
238   // These functions are for SymbolRef to call internally. The main goal of
239   // this is to allow SymbolRef::SymbolPimpl to point directly to the symbol
240   // entry in the memory mapped object file. SymbolPimpl cannot contain any
241   // virtual functions because then it could not point into the memory mapped
242   // file.
243   //
244   // Implementations assume that the DataRefImpl is valid and has not been
245   // modified externally. It's UB otherwise.
246   friend class SymbolRef;
247 
248   virtual Expected<StringRef> getSymbolName(DataRefImpl Symb) const = 0;
249   Error printSymbolName(raw_ostream &OS,
250                                   DataRefImpl Symb) const override;
251   virtual Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const = 0;
252   virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const = 0;
253   virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const;
254   virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const = 0;
255   virtual Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const = 0;
256   virtual Expected<section_iterator>
257   getSymbolSection(DataRefImpl Symb) const = 0;
258 
259   // Same as above for SectionRef.
260   friend class SectionRef;
261 
262   virtual void moveSectionNext(DataRefImpl &Sec) const = 0;
263   virtual Expected<StringRef> getSectionName(DataRefImpl Sec) const = 0;
264   virtual uint64_t getSectionAddress(DataRefImpl Sec) const = 0;
265   virtual uint64_t getSectionIndex(DataRefImpl Sec) const = 0;
266   virtual uint64_t getSectionSize(DataRefImpl Sec) const = 0;
267   virtual Expected<ArrayRef<uint8_t>>
268   getSectionContents(DataRefImpl Sec) const = 0;
269   virtual uint64_t getSectionAlignment(DataRefImpl Sec) const = 0;
270   virtual bool isSectionCompressed(DataRefImpl Sec) const = 0;
271   virtual bool isSectionText(DataRefImpl Sec) const = 0;
272   virtual bool isSectionData(DataRefImpl Sec) const = 0;
273   virtual bool isSectionBSS(DataRefImpl Sec) const = 0;
274   // A section is 'virtual' if its contents aren't present in the object image.
275   virtual bool isSectionVirtual(DataRefImpl Sec) const = 0;
276   virtual bool isSectionBitcode(DataRefImpl Sec) const;
277   virtual bool isSectionStripped(DataRefImpl Sec) const;
278   virtual bool isBerkeleyText(DataRefImpl Sec) const;
279   virtual bool isBerkeleyData(DataRefImpl Sec) const;
280   virtual bool isDebugSection(DataRefImpl Sec) const;
281   virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const = 0;
282   virtual relocation_iterator section_rel_end(DataRefImpl Sec) const = 0;
283   virtual Expected<section_iterator> getRelocatedSection(DataRefImpl Sec) const;
284 
285   // Same as above for RelocationRef.
286   friend class RelocationRef;
287   virtual void moveRelocationNext(DataRefImpl &Rel) const = 0;
288   virtual uint64_t getRelocationOffset(DataRefImpl Rel) const = 0;
289   virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const = 0;
290   virtual uint64_t getRelocationType(DataRefImpl Rel) const = 0;
291   virtual void getRelocationTypeName(DataRefImpl Rel,
292                                      SmallVectorImpl<char> &Result) const = 0;
293 
294   virtual llvm::binaryformat::Swift5ReflectionSectionKind
295   mapReflectionSectionNameToEnumValue(StringRef SectionName) const {
296     return llvm::binaryformat::Swift5ReflectionSectionKind::unknown;
297   };
298 
299   Expected<uint64_t> getSymbolValue(DataRefImpl Symb) const;
300 
301 public:
302   ObjectFile() = delete;
303   ObjectFile(const ObjectFile &other) = delete;
304 
305   uint64_t getCommonSymbolSize(DataRefImpl Symb) const {
306     Expected<uint32_t> SymbolFlagsOrErr = getSymbolFlags(Symb);
307     if (!SymbolFlagsOrErr)
308       // TODO: Actually report errors helpfully.
309       report_fatal_error(SymbolFlagsOrErr.takeError());
310     assert(*SymbolFlagsOrErr & SymbolRef::SF_Common);
311     return getCommonSymbolSizeImpl(Symb);
312   }
313 
314   virtual std::vector<SectionRef> dynamic_relocation_sections() const {
315     return std::vector<SectionRef>();
316   }
317 
318   using symbol_iterator_range = iterator_range<symbol_iterator>;
319   symbol_iterator_range symbols() const {
320     return symbol_iterator_range(symbol_begin(), symbol_end());
321   }
322 
323   virtual section_iterator section_begin() const = 0;
324   virtual section_iterator section_end() const = 0;
325 
326   using section_iterator_range = iterator_range<section_iterator>;
327   section_iterator_range sections() const {
328     return section_iterator_range(section_begin(), section_end());
329   }
330 
331   /// The number of bytes used to represent an address in this object
332   ///        file format.
333   virtual uint8_t getBytesInAddress() const = 0;
334 
335   virtual StringRef getFileFormatName() const = 0;
336   virtual Triple::ArchType getArch() const = 0;
337   virtual SubtargetFeatures getFeatures() const = 0;
338   virtual Optional<StringRef> tryGetCPUName() const { return None; };
339   virtual void setARMSubArch(Triple &TheTriple) const { }
340   virtual Expected<uint64_t> getStartAddress() const {
341     return errorCodeToError(object_error::parse_failed);
342   };
343 
344   /// Create a triple from the data in this object file.
345   Triple makeTriple() const;
346 
347   /// Maps a debug section name to a standard DWARF section name.
348   virtual StringRef mapDebugSectionName(StringRef Name) const { return Name; }
349 
350   /// True if this is a relocatable object (.o/.obj).
351   virtual bool isRelocatableObject() const = 0;
352 
353   /// True if the reflection section can be stripped by the linker.
354   bool isReflectionSectionStrippable(
355       llvm::binaryformat::Swift5ReflectionSectionKind ReflectionSectionKind)
356       const;
357 
358   /// @returns Pointer to ObjectFile subclass to handle this type of object.
359   /// @param ObjectPath The path to the object file. ObjectPath.isObject must
360   ///        return true.
361   /// Create ObjectFile from path.
362   static Expected<OwningBinary<ObjectFile>>
363   createObjectFile(StringRef ObjectPath);
364 
365   static Expected<std::unique_ptr<ObjectFile>>
366   createObjectFile(MemoryBufferRef Object, llvm::file_magic Type,
367                    bool InitContent = true);
368   static Expected<std::unique_ptr<ObjectFile>>
369   createObjectFile(MemoryBufferRef Object) {
370     return createObjectFile(Object, llvm::file_magic::unknown);
371   }
372 
373   static bool classof(const Binary *v) {
374     return v->isObject();
375   }
376 
377   static Expected<std::unique_ptr<COFFObjectFile>>
378   createCOFFObjectFile(MemoryBufferRef Object);
379 
380   static Expected<std::unique_ptr<ObjectFile>>
381   createXCOFFObjectFile(MemoryBufferRef Object, unsigned FileType);
382 
383   static Expected<std::unique_ptr<ObjectFile>>
384   createELFObjectFile(MemoryBufferRef Object, bool InitContent = true);
385 
386   static Expected<std::unique_ptr<MachOObjectFile>>
387   createMachOObjectFile(MemoryBufferRef Object,
388                         uint32_t UniversalCputype = 0,
389                         uint32_t UniversalIndex = 0);
390 
391   static Expected<std::unique_ptr<WasmObjectFile>>
392   createWasmObjectFile(MemoryBufferRef Object);
393 };
394 
395 // Inline function definitions.
396 inline SymbolRef::SymbolRef(DataRefImpl SymbolP, const ObjectFile *Owner)
397     : BasicSymbolRef(SymbolP, Owner) {}
398 
399 inline Expected<StringRef> SymbolRef::getName() const {
400   return getObject()->getSymbolName(getRawDataRefImpl());
401 }
402 
403 inline Expected<uint64_t> SymbolRef::getAddress() const {
404   return getObject()->getSymbolAddress(getRawDataRefImpl());
405 }
406 
407 inline Expected<uint64_t> SymbolRef::getValue() const {
408   return getObject()->getSymbolValue(getRawDataRefImpl());
409 }
410 
411 inline uint32_t SymbolRef::getAlignment() const {
412   return getObject()->getSymbolAlignment(getRawDataRefImpl());
413 }
414 
415 inline uint64_t SymbolRef::getCommonSize() const {
416   return getObject()->getCommonSymbolSize(getRawDataRefImpl());
417 }
418 
419 inline Expected<section_iterator> SymbolRef::getSection() const {
420   return getObject()->getSymbolSection(getRawDataRefImpl());
421 }
422 
423 inline Expected<SymbolRef::Type> SymbolRef::getType() const {
424   return getObject()->getSymbolType(getRawDataRefImpl());
425 }
426 
427 inline const ObjectFile *SymbolRef::getObject() const {
428   const SymbolicFile *O = BasicSymbolRef::getObject();
429   return cast<ObjectFile>(O);
430 }
431 
432 /// SectionRef
433 inline SectionRef::SectionRef(DataRefImpl SectionP,
434                               const ObjectFile *Owner)
435   : SectionPimpl(SectionP)
436   , OwningObject(Owner) {}
437 
438 inline bool SectionRef::operator==(const SectionRef &Other) const {
439   return OwningObject == Other.OwningObject &&
440          SectionPimpl == Other.SectionPimpl;
441 }
442 
443 inline bool SectionRef::operator!=(const SectionRef &Other) const {
444   return !(*this == Other);
445 }
446 
447 inline bool SectionRef::operator<(const SectionRef &Other) const {
448   assert(OwningObject == Other.OwningObject);
449   return SectionPimpl < Other.SectionPimpl;
450 }
451 
452 inline void SectionRef::moveNext() {
453   return OwningObject->moveSectionNext(SectionPimpl);
454 }
455 
456 inline Expected<StringRef> SectionRef::getName() const {
457   return OwningObject->getSectionName(SectionPimpl);
458 }
459 
460 inline uint64_t SectionRef::getAddress() const {
461   return OwningObject->getSectionAddress(SectionPimpl);
462 }
463 
464 inline uint64_t SectionRef::getIndex() const {
465   return OwningObject->getSectionIndex(SectionPimpl);
466 }
467 
468 inline uint64_t SectionRef::getSize() const {
469   return OwningObject->getSectionSize(SectionPimpl);
470 }
471 
472 inline Expected<StringRef> SectionRef::getContents() const {
473   Expected<ArrayRef<uint8_t>> Res =
474       OwningObject->getSectionContents(SectionPimpl);
475   if (!Res)
476     return Res.takeError();
477   return StringRef(reinterpret_cast<const char *>(Res->data()), Res->size());
478 }
479 
480 inline uint64_t SectionRef::getAlignment() const {
481   return OwningObject->getSectionAlignment(SectionPimpl);
482 }
483 
484 inline bool SectionRef::isCompressed() const {
485   return OwningObject->isSectionCompressed(SectionPimpl);
486 }
487 
488 inline bool SectionRef::isText() const {
489   return OwningObject->isSectionText(SectionPimpl);
490 }
491 
492 inline bool SectionRef::isData() const {
493   return OwningObject->isSectionData(SectionPimpl);
494 }
495 
496 inline bool SectionRef::isBSS() const {
497   return OwningObject->isSectionBSS(SectionPimpl);
498 }
499 
500 inline bool SectionRef::isVirtual() const {
501   return OwningObject->isSectionVirtual(SectionPimpl);
502 }
503 
504 inline bool SectionRef::isBitcode() const {
505   return OwningObject->isSectionBitcode(SectionPimpl);
506 }
507 
508 inline bool SectionRef::isStripped() const {
509   return OwningObject->isSectionStripped(SectionPimpl);
510 }
511 
512 inline bool SectionRef::isBerkeleyText() const {
513   return OwningObject->isBerkeleyText(SectionPimpl);
514 }
515 
516 inline bool SectionRef::isBerkeleyData() const {
517   return OwningObject->isBerkeleyData(SectionPimpl);
518 }
519 
520 inline bool SectionRef::isDebugSection() const {
521   return OwningObject->isDebugSection(SectionPimpl);
522 }
523 
524 inline relocation_iterator SectionRef::relocation_begin() const {
525   return OwningObject->section_rel_begin(SectionPimpl);
526 }
527 
528 inline relocation_iterator SectionRef::relocation_end() const {
529   return OwningObject->section_rel_end(SectionPimpl);
530 }
531 
532 inline Expected<section_iterator> SectionRef::getRelocatedSection() const {
533   return OwningObject->getRelocatedSection(SectionPimpl);
534 }
535 
536 inline DataRefImpl SectionRef::getRawDataRefImpl() const {
537   return SectionPimpl;
538 }
539 
540 inline const ObjectFile *SectionRef::getObject() const {
541   return OwningObject;
542 }
543 
544 /// RelocationRef
545 inline RelocationRef::RelocationRef(DataRefImpl RelocationP,
546                               const ObjectFile *Owner)
547   : RelocationPimpl(RelocationP)
548   , OwningObject(Owner) {}
549 
550 inline bool RelocationRef::operator==(const RelocationRef &Other) const {
551   return RelocationPimpl == Other.RelocationPimpl;
552 }
553 
554 inline void RelocationRef::moveNext() {
555   return OwningObject->moveRelocationNext(RelocationPimpl);
556 }
557 
558 inline uint64_t RelocationRef::getOffset() const {
559   return OwningObject->getRelocationOffset(RelocationPimpl);
560 }
561 
562 inline symbol_iterator RelocationRef::getSymbol() const {
563   return OwningObject->getRelocationSymbol(RelocationPimpl);
564 }
565 
566 inline uint64_t RelocationRef::getType() const {
567   return OwningObject->getRelocationType(RelocationPimpl);
568 }
569 
570 inline void RelocationRef::getTypeName(SmallVectorImpl<char> &Result) const {
571   return OwningObject->getRelocationTypeName(RelocationPimpl, Result);
572 }
573 
574 inline DataRefImpl RelocationRef::getRawDataRefImpl() const {
575   return RelocationPimpl;
576 }
577 
578 inline const ObjectFile *RelocationRef::getObject() const {
579   return OwningObject;
580 }
581 
582 } // end namespace object
583 
584 template <> struct DenseMapInfo<object::SectionRef> {
585   static bool isEqual(const object::SectionRef &A,
586                       const object::SectionRef &B) {
587     return A == B;
588   }
589   static object::SectionRef getEmptyKey() {
590     return object::SectionRef({}, nullptr);
591   }
592   static object::SectionRef getTombstoneKey() {
593     object::DataRefImpl TS;
594     TS.p = (uintptr_t)-1;
595     return object::SectionRef(TS, nullptr);
596   }
597   static unsigned getHashValue(const object::SectionRef &Sec) {
598     object::DataRefImpl Raw = Sec.getRawDataRefImpl();
599     return hash_combine(Raw.p, Raw.d.a, Raw.d.b);
600   }
601 };
602 
603 } // end namespace llvm
604 
605 #endif // LLVM_OBJECT_OBJECTFILE_H
606