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