LLVM  9.0.0svn
ELFObjectFile.h
Go to the documentation of this file.
1 //===- ELFObjectFile.h - ELF object file implementation ---------*- 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 the ELFObjectFile template class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
14 #define LLVM_OBJECT_ELFOBJECTFILE_H
15 
16 #include "llvm/ADT/ArrayRef.h"
17 #include "llvm/ADT/STLExtras.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "llvm/ADT/Triple.h"
22 #include "llvm/BinaryFormat/ELF.h"
24 #include "llvm/Object/Binary.h"
25 #include "llvm/Object/ELF.h"
26 #include "llvm/Object/ELFTypes.h"
27 #include "llvm/Object/Error.h"
28 #include "llvm/Object/ObjectFile.h"
32 #include "llvm/Support/Casting.h"
33 #include "llvm/Support/Endian.h"
34 #include "llvm/Support/Error.h"
37 #include <cassert>
38 #include <cstdint>
39 #include <system_error>
40 
41 namespace llvm {
42 namespace object {
43 
44 constexpr int NumElfSymbolTypes = 8;
46 
48 
49 class ELFObjectFileBase : public ObjectFile {
50  friend class ELFRelocationRef;
51  friend class ELFSectionRef;
52  friend class ELFSymbolRef;
53 
54 protected:
56 
57  virtual uint16_t getEMachine() const = 0;
58  virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
59  virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
60  virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
61 
62  virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
63  virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
64  virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
65 
66  virtual Expected<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
67  virtual Error getBuildAttributes(ARMAttributeParser &Attributes) const = 0;
68 
69 public:
71 
73 
74  /// Returns platform-specific object flags, if any.
75  virtual unsigned getPlatformFlags() const = 0;
76 
78 
79  static bool classof(const Binary *v) { return v->isELF(); }
80 
81  SubtargetFeatures getFeatures() const override;
82 
84 
86 
88 
89  void setARMSubArch(Triple &TheTriple) const override;
90 
91  virtual uint16_t getEType() const = 0;
92 
93  std::vector<std::pair<DataRefImpl, uint64_t>> getPltAddresses() const;
94 };
95 
96 class ELFSectionRef : public SectionRef {
97 public:
99  assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
100  }
101 
102  const ELFObjectFileBase *getObject() const {
103  return cast<ELFObjectFileBase>(SectionRef::getObject());
104  }
105 
106  uint32_t getType() const {
107  return getObject()->getSectionType(getRawDataRefImpl());
108  }
109 
110  uint64_t getFlags() const {
111  return getObject()->getSectionFlags(getRawDataRefImpl());
112  }
113 
114  uint64_t getOffset() const {
115  return getObject()->getSectionOffset(getRawDataRefImpl());
116  }
117 };
118 
120 public:
122  assert(isa<ELFObjectFileBase>(B->getObject()));
123  }
124 
125  const ELFSectionRef *operator->() const {
126  return static_cast<const ELFSectionRef *>(section_iterator::operator->());
127  }
128 
129  const ELFSectionRef &operator*() const {
130  return static_cast<const ELFSectionRef &>(section_iterator::operator*());
131  }
132 };
133 
134 class ELFSymbolRef : public SymbolRef {
135 public:
137  assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
138  }
139 
140  const ELFObjectFileBase *getObject() const {
141  return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
142  }
143 
144  uint64_t getSize() const {
145  return getObject()->getSymbolSize(getRawDataRefImpl());
146  }
147 
148  uint8_t getOther() const {
149  return getObject()->getSymbolOther(getRawDataRefImpl());
150  }
151 
152  uint8_t getELFType() const {
153  return getObject()->getSymbolELFType(getRawDataRefImpl());
154  }
155 
157  uint8_t Type = getELFType();
158  for (auto &EE : ElfSymbolTypes) {
159  if (EE.Value == Type) {
160  return EE.AltName;
161  }
162  }
163  return "";
164  }
165 };
166 
168 public:
170  : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
171  cast<ELFObjectFileBase>(B->getObject()))) {}
172 
173  const ELFSymbolRef *operator->() const {
174  return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
175  }
176 
177  const ELFSymbolRef &operator*() const {
178  return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
179  }
180 };
181 
183 public:
185  assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
186  }
187 
188  const ELFObjectFileBase *getObject() const {
189  return cast<ELFObjectFileBase>(RelocationRef::getObject());
190  }
191 
193  return getObject()->getRelocationAddend(getRawDataRefImpl());
194  }
195 };
196 
198 public:
201  B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
202 
203  const ELFRelocationRef *operator->() const {
204  return static_cast<const ELFRelocationRef *>(
206  }
207 
208  const ELFRelocationRef &operator*() const {
209  return static_cast<const ELFRelocationRef &>(
211  }
212 };
213 
217 }
218 
219 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
220  uint16_t getEMachine() const override;
221  uint16_t getEType() const override;
222  uint64_t getSymbolSize(DataRefImpl Sym) const override;
223 
224 public:
226 
227  using uintX_t = typename ELFT::uint;
228 
229  using Elf_Sym = typename ELFT::Sym;
230  using Elf_Shdr = typename ELFT::Shdr;
231  using Elf_Ehdr = typename ELFT::Ehdr;
232  using Elf_Rel = typename ELFT::Rel;
233  using Elf_Rela = typename ELFT::Rela;
234  using Elf_Dyn = typename ELFT::Dyn;
235 
236 private:
237  ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
238  const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
239  ArrayRef<Elf_Word> ShndxTable);
240 
241 protected:
242  ELFFile<ELFT> EF;
243 
244  const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
245  const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
246  ArrayRef<Elf_Word> ShndxTable;
247 
248  void moveSymbolNext(DataRefImpl &Symb) const override;
250  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
251  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
253  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
254  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
255  uint8_t getSymbolOther(DataRefImpl Symb) const override;
256  uint8_t getSymbolELFType(DataRefImpl Symb) const override;
259  const Elf_Shdr *SymTab) const;
261 
262  void moveSectionNext(DataRefImpl &Sec) const override;
263  Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
264  uint64_t getSectionAddress(DataRefImpl Sec) const override;
265  uint64_t getSectionIndex(DataRefImpl Sec) const override;
266  uint64_t getSectionSize(DataRefImpl Sec) const override;
267  Expected<ArrayRef<uint8_t>>
268  getSectionContents(DataRefImpl Sec) const override;
269  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
270  bool isSectionCompressed(DataRefImpl Sec) const override;
271  bool isSectionText(DataRefImpl Sec) const override;
272  bool isSectionData(DataRefImpl Sec) const override;
273  bool isSectionBSS(DataRefImpl Sec) const override;
274  bool isSectionVirtual(DataRefImpl Sec) const override;
275  bool isBerkeleyText(DataRefImpl Sec) const override;
276  bool isBerkeleyData(DataRefImpl Sec) const override;
278  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
279  std::vector<SectionRef> dynamic_relocation_sections() const override;
281 
282  void moveRelocationNext(DataRefImpl &Rel) const override;
283  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
284  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
285  uint64_t getRelocationType(DataRefImpl Rel) const override;
287  SmallVectorImpl<char> &Result) const override;
288 
289  uint32_t getSectionType(DataRefImpl Sec) const override;
290  uint64_t getSectionFlags(DataRefImpl Sec) const override;
291  uint64_t getSectionOffset(DataRefImpl Sec) const override;
293 
294  /// Get the relocation section that contains \a Rel.
295  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
296  auto RelSecOrErr = EF.getSection(Rel.d.a);
297  if (!RelSecOrErr)
298  report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
299  return *RelSecOrErr;
300  }
301 
302  DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
303  DataRefImpl DRI;
304  if (!SymTable) {
305  DRI.d.a = 0;
306  DRI.d.b = 0;
307  return DRI;
308  }
309  assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
310  SymTable->sh_type == ELF::SHT_DYNSYM);
311 
312  auto SectionsOrErr = EF.sections();
313  if (!SectionsOrErr) {
314  DRI.d.a = 0;
315  DRI.d.b = 0;
316  return DRI;
317  }
318  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
319  unsigned SymTableIndex =
320  (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
321 
322  DRI.d.a = SymTableIndex;
323  DRI.d.b = SymbolNum;
324  return DRI;
325  }
326 
327  const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
328  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
329  }
330 
331  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
332  DataRefImpl DRI;
333  DRI.p = reinterpret_cast<uintptr_t>(Sec);
334  return DRI;
335  }
336 
337  DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
338  DataRefImpl DRI;
339  DRI.p = reinterpret_cast<uintptr_t>(Dyn);
340  return DRI;
341  }
342 
343  bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
344  unsigned char Binding = ESym->getBinding();
345  unsigned char Visibility = ESym->getVisibility();
346 
347  // A symbol is exported if its binding is either GLOBAL or WEAK, and its
348  // visibility is either DEFAULT or PROTECTED. All other symbols are not
349  // exported.
350  return (
351  (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
352  Binding == ELF::STB_GNU_UNIQUE) &&
353  (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
354  }
355 
356  Error getBuildAttributes(ARMAttributeParser &Attributes) const override {
357  auto SectionsOrErr = EF.sections();
358  if (!SectionsOrErr)
359  return SectionsOrErr.takeError();
360 
361  for (const Elf_Shdr &Sec : *SectionsOrErr) {
362  if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
363  auto ErrorOrContents = EF.getSectionContents(&Sec);
364  if (!ErrorOrContents)
365  return ErrorOrContents.takeError();
366 
367  auto Contents = ErrorOrContents.get();
368  if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
369  return Error::success();
370 
371  Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
372  break;
373  }
374  }
375  return Error::success();
376  }
377 
378  // This flag is used for classof, to distinguish ELFObjectFile from
379  // its subclass. If more subclasses will be created, this flag will
380  // have to become an enum.
382 
383 public:
385  static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
386 
387  const Elf_Rel *getRel(DataRefImpl Rel) const;
388  const Elf_Rela *getRela(DataRefImpl Rela) const;
389 
390  const Elf_Sym *getSymbol(DataRefImpl Sym) const {
391  auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
392  if (!Ret)
393  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
394  return *Ret;
395  }
396 
397  const Elf_Shdr *getSection(DataRefImpl Sec) const {
398  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
399  }
400 
401  basic_symbol_iterator symbol_begin() const override;
402  basic_symbol_iterator symbol_end() const override;
403 
404  elf_symbol_iterator dynamic_symbol_begin() const;
405  elf_symbol_iterator dynamic_symbol_end() const;
406 
407  section_iterator section_begin() const override;
408  section_iterator section_end() const override;
409 
411 
412  uint8_t getBytesInAddress() const override;
413  StringRef getFileFormatName() const override;
414  Triple::ArchType getArch() const override;
415  Expected<uint64_t> getStartAddress() const override;
416 
417  unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
418 
419  const ELFFile<ELFT> *getELFFile() const { return &EF; }
420 
421  bool isDyldType() const { return isDyldELFObject; }
422  static bool classof(const Binary *v) {
423  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
424  ELFT::Is64Bits);
425  }
426 
428 
429  bool isRelocatableObject() const override;
430 };
431 
436 
437 template <class ELFT>
439  ++Sym.d.b;
440 }
441 
442 template <class ELFT>
444  const Elf_Sym *ESym = getSymbol(Sym);
445  auto SymTabOrErr = EF.getSection(Sym.d.a);
446  if (!SymTabOrErr)
447  return SymTabOrErr.takeError();
448  const Elf_Shdr *SymTableSec = *SymTabOrErr;
449  auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
450  if (!StrTabOrErr)
451  return StrTabOrErr.takeError();
452  const Elf_Shdr *StringTableSec = *StrTabOrErr;
453  auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
454  if (!SymStrTabOrErr)
455  return SymStrTabOrErr.takeError();
456  Expected<StringRef> Name = ESym->getName(*SymStrTabOrErr);
457 
458  // If the symbol name is empty use the section name.
459  if ((!Name || Name->empty()) && ESym->getType() == ELF::STT_SECTION) {
460  StringRef SecName;
462  if (Sec && !(*Sec)->getName(SecName))
463  return SecName;
464  }
465  return Name;
466 }
467 
468 template <class ELFT>
470  return getSection(Sec)->sh_flags;
471 }
472 
473 template <class ELFT>
475  return getSection(Sec)->sh_type;
476 }
477 
478 template <class ELFT>
480  return getSection(Sec)->sh_offset;
481 }
482 
483 template <class ELFT>
485  const Elf_Sym *ESym = getSymbol(Symb);
486  uint64_t Ret = ESym->st_value;
487  if (ESym->st_shndx == ELF::SHN_ABS)
488  return Ret;
489 
490  const Elf_Ehdr *Header = EF.getHeader();
491  // Clear the ARM/Thumb or microMIPS indicator flag.
492  if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
493  ESym->getType() == ELF::STT_FUNC)
494  Ret &= ~1;
495 
496  return Ret;
497 }
498 
499 template <class ELFT>
502  uint64_t Result = getSymbolValue(Symb);
503  const Elf_Sym *ESym = getSymbol(Symb);
504  switch (ESym->st_shndx) {
505  case ELF::SHN_COMMON:
506  case ELF::SHN_UNDEF:
507  case ELF::SHN_ABS:
508  return Result;
509  }
510 
511  const Elf_Ehdr *Header = EF.getHeader();
512  auto SymTabOrErr = EF.getSection(Symb.d.a);
513  if (!SymTabOrErr)
514  return SymTabOrErr.takeError();
515  const Elf_Shdr *SymTab = *SymTabOrErr;
516 
517  if (Header->e_type == ELF::ET_REL) {
518  auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
519  if (!SectionOrErr)
520  return SectionOrErr.takeError();
521  const Elf_Shdr *Section = *SectionOrErr;
522  if (Section)
523  Result += Section->sh_addr;
524  }
525 
526  return Result;
527 }
528 
529 template <class ELFT>
531  const Elf_Sym *Sym = getSymbol(Symb);
532  if (Sym->st_shndx == ELF::SHN_COMMON)
533  return Sym->st_value;
534  return 0;
535 }
536 
537 template <class ELFT>
538 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
539  return EF.getHeader()->e_machine;
540 }
541 
542 template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
543  return EF.getHeader()->e_type;
544 }
545 
546 template <class ELFT>
548  return getSymbol(Sym)->st_size;
549 }
550 
551 template <class ELFT>
553  return getSymbol(Symb)->st_size;
554 }
555 
556 template <class ELFT>
558  return getSymbol(Symb)->st_other;
559 }
560 
561 template <class ELFT>
563  return getSymbol(Symb)->getType();
564 }
565 
566 template <class ELFT>
569  const Elf_Sym *ESym = getSymbol(Symb);
570 
571  switch (ESym->getType()) {
572  case ELF::STT_NOTYPE:
573  return SymbolRef::ST_Unknown;
574  case ELF::STT_SECTION:
575  return SymbolRef::ST_Debug;
576  case ELF::STT_FILE:
577  return SymbolRef::ST_File;
578  case ELF::STT_FUNC:
579  return SymbolRef::ST_Function;
580  case ELF::STT_OBJECT:
581  case ELF::STT_COMMON:
582  case ELF::STT_TLS:
583  return SymbolRef::ST_Data;
584  default:
585  return SymbolRef::ST_Other;
586  }
587 }
588 
589 template <class ELFT>
591  const Elf_Sym *ESym = getSymbol(Sym);
592 
593  uint32_t Result = SymbolRef::SF_None;
594 
595  if (ESym->getBinding() != ELF::STB_LOCAL)
596  Result |= SymbolRef::SF_Global;
597 
598  if (ESym->getBinding() == ELF::STB_WEAK)
599  Result |= SymbolRef::SF_Weak;
600 
601  if (ESym->st_shndx == ELF::SHN_ABS)
602  Result |= SymbolRef::SF_Absolute;
603 
604  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
606 
607  auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
608  if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
610  auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
611  if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
613 
614  if (EF.getHeader()->e_machine == ELF::EM_ARM) {
615  if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
616  StringRef Name = *NameOrErr;
617  if (Name.startswith("$d") || Name.startswith("$t") ||
618  Name.startswith("$a"))
620  } else {
621  // TODO: Actually report errors helpfully.
622  consumeError(NameOrErr.takeError());
623  }
624  if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
625  Result |= SymbolRef::SF_Thumb;
626  }
627 
628  if (ESym->st_shndx == ELF::SHN_UNDEF)
629  Result |= SymbolRef::SF_Undefined;
630 
631  if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
632  Result |= SymbolRef::SF_Common;
633 
634  if (isExportedToOtherDSO(ESym))
635  Result |= SymbolRef::SF_Exported;
636 
637  if (ESym->getVisibility() == ELF::STV_HIDDEN)
638  Result |= SymbolRef::SF_Hidden;
639 
640  return Result;
641 }
642 
643 template <class ELFT>
646  const Elf_Shdr *SymTab) const {
647  auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
648  if (!ESecOrErr)
649  return ESecOrErr.takeError();
650 
651  const Elf_Shdr *ESec = *ESecOrErr;
652  if (!ESec)
653  return section_end();
654 
655  DataRefImpl Sec;
656  Sec.p = reinterpret_cast<intptr_t>(ESec);
657  return section_iterator(SectionRef(Sec, this));
658 }
659 
660 template <class ELFT>
663  const Elf_Sym *Sym = getSymbol(Symb);
664  auto SymTabOrErr = EF.getSection(Symb.d.a);
665  if (!SymTabOrErr)
666  return SymTabOrErr.takeError();
667  const Elf_Shdr *SymTab = *SymTabOrErr;
668  return getSymbolSection(Sym, SymTab);
669 }
670 
671 template <class ELFT>
673  const Elf_Shdr *ESec = getSection(Sec);
674  Sec = toDRI(++ESec);
675 }
676 
677 template <class ELFT>
679  return EF.getSectionName(&*getSection(Sec));
680 }
681 
682 template <class ELFT>
684  return getSection(Sec)->sh_addr;
685 }
686 
687 template <class ELFT>
689  auto SectionsOrErr = EF.sections();
690  handleAllErrors(std::move(SectionsOrErr.takeError()),
691  [](const ErrorInfoBase &) {
692  llvm_unreachable("unable to get section index");
693  });
694  const Elf_Shdr *First = SectionsOrErr->begin();
695  return getSection(Sec) - First;
696 }
697 
698 template <class ELFT>
700  return getSection(Sec)->sh_size;
701 }
702 
703 template <class ELFT>
706  const Elf_Shdr *EShdr = getSection(Sec);
707  if (std::error_code EC =
709  (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
710  return errorCodeToError(EC);
711  return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
712  EShdr->sh_size);
713 }
714 
715 template <class ELFT>
717  return getSection(Sec)->sh_addralign;
718 }
719 
720 template <class ELFT>
722  return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
723 }
724 
725 template <class ELFT>
727  return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
728 }
729 
730 template <class ELFT>
732  const Elf_Shdr *EShdr = getSection(Sec);
733  return EShdr->sh_type == ELF::SHT_PROGBITS &&
734  EShdr->sh_flags & ELF::SHF_ALLOC &&
735  !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
736 }
737 
738 template <class ELFT>
740  const Elf_Shdr *EShdr = getSection(Sec);
741  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
742  EShdr->sh_type == ELF::SHT_NOBITS;
743 }
744 
745 template <class ELFT>
746 std::vector<SectionRef>
748  std::vector<SectionRef> Res;
749  std::vector<uintptr_t> Offsets;
750 
751  auto SectionsOrErr = EF.sections();
752  if (!SectionsOrErr)
753  return Res;
754 
755  for (const Elf_Shdr &Sec : *SectionsOrErr) {
756  if (Sec.sh_type != ELF::SHT_DYNAMIC)
757  continue;
758  Elf_Dyn *Dynamic =
759  reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
760  for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
761  if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
762  Dynamic->d_tag == ELF::DT_JMPREL) {
763  Offsets.push_back(Dynamic->d_un.d_val);
764  }
765  }
766  }
767  for (const Elf_Shdr &Sec : *SectionsOrErr) {
768  if (is_contained(Offsets, Sec.sh_offset))
769  Res.emplace_back(toDRI(&Sec), this);
770  }
771  return Res;
772 }
773 
774 template <class ELFT>
776  return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
777 }
778 
779 template <class ELFT>
781  return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
782  (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
783  !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
784 }
785 
786 template <class ELFT>
788  const Elf_Shdr *EShdr = getSection(Sec);
789  return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
790  EShdr->sh_flags & ELF::SHF_ALLOC;
791 }
792 
793 template <class ELFT>
796  DataRefImpl RelData;
797  auto SectionsOrErr = EF.sections();
798  if (!SectionsOrErr)
800  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
801  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
802  RelData.d.b = 0;
803  return relocation_iterator(RelocationRef(RelData, this));
804 }
805 
806 template <class ELFT>
809  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
811  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
812  return Begin;
813  DataRefImpl RelData = Begin->getRawDataRefImpl();
814  const Elf_Shdr *RelSec = getRelSection(RelData);
815 
816  // Error check sh_link here so that getRelocationSymbol can just use it.
817  auto SymSecOrErr = EF.getSection(RelSec->sh_link);
818  if (!SymSecOrErr)
819  report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
820 
821  RelData.d.b += S->sh_size / S->sh_entsize;
822  return relocation_iterator(RelocationRef(RelData, this));
823 }
824 
825 template <class ELFT>
828  if (EF.getHeader()->e_type != ELF::ET_REL)
829  return section_end();
830 
831  const Elf_Shdr *EShdr = getSection(Sec);
832  uintX_t Type = EShdr->sh_type;
833  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
834  return section_end();
835 
836  auto R = EF.getSection(EShdr->sh_info);
837  if (!R)
838  report_fatal_error(errorToErrorCode(R.takeError()).message());
839  return section_iterator(SectionRef(toDRI(*R), this));
840 }
841 
842 // Relocations
843 template <class ELFT>
845  ++Rel.d.b;
846 }
847 
848 template <class ELFT>
851  uint32_t symbolIdx;
852  const Elf_Shdr *sec = getRelSection(Rel);
853  if (sec->sh_type == ELF::SHT_REL)
854  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
855  else
856  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
857  if (!symbolIdx)
858  return symbol_end();
859 
860  // FIXME: error check symbolIdx
861  DataRefImpl SymbolData;
862  SymbolData.d.a = sec->sh_link;
863  SymbolData.d.b = symbolIdx;
864  return symbol_iterator(SymbolRef(SymbolData, this));
865 }
866 
867 template <class ELFT>
869  const Elf_Shdr *sec = getRelSection(Rel);
870  if (sec->sh_type == ELF::SHT_REL)
871  return getRel(Rel)->r_offset;
872 
873  return getRela(Rel)->r_offset;
874 }
875 
876 template <class ELFT>
878  const Elf_Shdr *sec = getRelSection(Rel);
879  if (sec->sh_type == ELF::SHT_REL)
880  return getRel(Rel)->getType(EF.isMips64EL());
881  else
882  return getRela(Rel)->getType(EF.isMips64EL());
883 }
884 
885 template <class ELFT>
887  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
888 }
889 
890 template <class ELFT>
892  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
894  EF.getRelocationTypeName(type, Result);
895 }
896 
897 template <class ELFT>
900  if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
901  return createError("Section is not SHT_RELA");
902  return (int64_t)getRela(Rel)->r_addend;
903 }
904 
905 template <class ELFT>
906 const typename ELFObjectFile<ELFT>::Elf_Rel *
908  assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
909  auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
910  if (!Ret)
911  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
912  return *Ret;
913 }
914 
915 template <class ELFT>
916 const typename ELFObjectFile<ELFT>::Elf_Rela *
918  assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
919  auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
920  if (!Ret)
921  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
922  return *Ret;
923 }
924 
925 template <class ELFT>
928  auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
929  if (Error E = EFOrErr.takeError())
930  return std::move(E);
931  auto EF = std::move(*EFOrErr);
932 
933  auto SectionsOrErr = EF.sections();
934  if (!SectionsOrErr)
935  return SectionsOrErr.takeError();
936 
937  const Elf_Shdr *DotDynSymSec = nullptr;
938  const Elf_Shdr *DotSymtabSec = nullptr;
939  ArrayRef<Elf_Word> ShndxTable;
940  for (const Elf_Shdr &Sec : *SectionsOrErr) {
941  switch (Sec.sh_type) {
942  case ELF::SHT_DYNSYM: {
943  if (DotDynSymSec)
944  return createError("More than one dynamic symbol table!");
945  DotDynSymSec = &Sec;
946  break;
947  }
948  case ELF::SHT_SYMTAB: {
949  if (DotSymtabSec)
950  return createError("More than one static symbol table!");
951  DotSymtabSec = &Sec;
952  break;
953  }
954  case ELF::SHT_SYMTAB_SHNDX: {
955  auto TableOrErr = EF.getSHNDXTable(Sec);
956  if (!TableOrErr)
957  return TableOrErr.takeError();
958  ShndxTable = *TableOrErr;
959  break;
960  }
961  }
962  }
963  return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
964  ShndxTable);
965 }
966 
967 template <class ELFT>
969  const Elf_Shdr *DotDynSymSec,
970  const Elf_Shdr *DotSymtabSec,
971  ArrayRef<Elf_Word> ShndxTable)
973  getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
974  Object),
975  EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
976  ShndxTable(ShndxTable) {}
977 
978 template <class ELFT>
979 ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
980  : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
981  Other.DotSymtabSec, Other.ShndxTable) {}
982 
983 template <class ELFT>
985  DataRefImpl Sym = toDRI(DotSymtabSec, 0);
986  return basic_symbol_iterator(SymbolRef(Sym, this));
987 }
988 
989 template <class ELFT>
991  const Elf_Shdr *SymTab = DotSymtabSec;
992  if (!SymTab)
993  return symbol_begin();
994  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
995  return basic_symbol_iterator(SymbolRef(Sym, this));
996 }
997 
998 template <class ELFT>
1000  DataRefImpl Sym = toDRI(DotDynSymSec, 0);
1001  return symbol_iterator(SymbolRef(Sym, this));
1002 }
1003 
1004 template <class ELFT>
1006  const Elf_Shdr *SymTab = DotDynSymSec;
1007  if (!SymTab)
1008  return dynamic_symbol_begin();
1009  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1010  return basic_symbol_iterator(SymbolRef(Sym, this));
1011 }
1012 
1013 template <class ELFT>
1015  auto SectionsOrErr = EF.sections();
1016  if (!SectionsOrErr)
1017  return section_iterator(SectionRef());
1018  return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1019 }
1020 
1021 template <class ELFT>
1023  auto SectionsOrErr = EF.sections();
1024  if (!SectionsOrErr)
1025  return section_iterator(SectionRef());
1026  return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1027 }
1028 
1029 template <class ELFT>
1031  return ELFT::Is64Bits ? 8 : 4;
1032 }
1033 
1034 template <class ELFT>
1036  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1037  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1038  case ELF::ELFCLASS32:
1039  switch (EF.getHeader()->e_machine) {
1040  case ELF::EM_386:
1041  return "ELF32-i386";
1042  case ELF::EM_IAMCU:
1043  return "ELF32-iamcu";
1044  case ELF::EM_X86_64:
1045  return "ELF32-x86-64";
1046  case ELF::EM_ARM:
1047  return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
1048  case ELF::EM_AVR:
1049  return "ELF32-avr";
1050  case ELF::EM_HEXAGON:
1051  return "ELF32-hexagon";
1052  case ELF::EM_LANAI:
1053  return "ELF32-lanai";
1054  case ELF::EM_MIPS:
1055  return "ELF32-mips";
1056  case ELF::EM_MSP430:
1057  return "ELF32-msp430";
1058  case ELF::EM_PPC:
1059  return "ELF32-ppc";
1060  case ELF::EM_RISCV:
1061  return "ELF32-riscv";
1062  case ELF::EM_SPARC:
1063  case ELF::EM_SPARC32PLUS:
1064  return "ELF32-sparc";
1065  case ELF::EM_AMDGPU:
1066  return "ELF32-amdgpu";
1067  default:
1068  return "ELF32-unknown";
1069  }
1070  case ELF::ELFCLASS64:
1071  switch (EF.getHeader()->e_machine) {
1072  case ELF::EM_386:
1073  return "ELF64-i386";
1074  case ELF::EM_X86_64:
1075  return "ELF64-x86-64";
1076  case ELF::EM_AARCH64:
1077  return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
1078  case ELF::EM_PPC64:
1079  return "ELF64-ppc64";
1080  case ELF::EM_RISCV:
1081  return "ELF64-riscv";
1082  case ELF::EM_S390:
1083  return "ELF64-s390";
1084  case ELF::EM_SPARCV9:
1085  return "ELF64-sparc";
1086  case ELF::EM_MIPS:
1087  return "ELF64-mips";
1088  case ELF::EM_AMDGPU:
1089  return "ELF64-amdgpu";
1090  case ELF::EM_BPF:
1091  return "ELF64-BPF";
1092  default:
1093  return "ELF64-unknown";
1094  }
1095  default:
1096  // FIXME: Proper error handling.
1097  report_fatal_error("Invalid ELFCLASS!");
1098  }
1099 }
1100 
1101 template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1102  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1103  switch (EF.getHeader()->e_machine) {
1104  case ELF::EM_386:
1105  case ELF::EM_IAMCU:
1106  return Triple::x86;
1107  case ELF::EM_X86_64:
1108  return Triple::x86_64;
1109  case ELF::EM_AARCH64:
1110  return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1111  case ELF::EM_ARM:
1112  return Triple::arm;
1113  case ELF::EM_AVR:
1114  return Triple::avr;
1115  case ELF::EM_HEXAGON:
1116  return Triple::hexagon;
1117  case ELF::EM_LANAI:
1118  return Triple::lanai;
1119  case ELF::EM_MIPS:
1120  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1121  case ELF::ELFCLASS32:
1122  return IsLittleEndian ? Triple::mipsel : Triple::mips;
1123  case ELF::ELFCLASS64:
1124  return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1125  default:
1126  report_fatal_error("Invalid ELFCLASS!");
1127  }
1128  case ELF::EM_MSP430:
1129  return Triple::msp430;
1130  case ELF::EM_PPC:
1131  return Triple::ppc;
1132  case ELF::EM_PPC64:
1133  return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1134  case ELF::EM_RISCV:
1135  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1136  case ELF::ELFCLASS32:
1137  return Triple::riscv32;
1138  case ELF::ELFCLASS64:
1139  return Triple::riscv64;
1140  default:
1141  report_fatal_error("Invalid ELFCLASS!");
1142  }
1143  case ELF::EM_S390:
1144  return Triple::systemz;
1145 
1146  case ELF::EM_SPARC:
1147  case ELF::EM_SPARC32PLUS:
1148  return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1149  case ELF::EM_SPARCV9:
1150  return Triple::sparcv9;
1151 
1152  case ELF::EM_AMDGPU: {
1153  if (!IsLittleEndian)
1154  return Triple::UnknownArch;
1155 
1156  unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
1157  if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1159  return Triple::r600;
1160  if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
1162  return Triple::amdgcn;
1163 
1164  return Triple::UnknownArch;
1165  }
1166 
1167  case ELF::EM_BPF:
1168  return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1169 
1170  default:
1171  return Triple::UnknownArch;
1172  }
1173 }
1174 
1175 template <class ELFT>
1177  return EF.getHeader()->e_entry;
1178 }
1179 
1180 template <class ELFT>
1184 }
1185 
1186 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1187  return EF.getHeader()->e_type == ELF::ET_REL;
1188 }
1189 
1190 } // end namespace object
1191 } // end namespace llvm
1192 
1193 #endif // LLVM_OBJECT_ELFOBJECTFILE_H
const content_type & operator*() const
Definition: SymbolicFile.h:78
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
virtual uint16_t getEMachine() const =0
static bool classof(const Binary *v)
Expected< uint64_t > getStartAddress() const override
friend class SymbolRef
Definition: ObjectFile.h:244
virtual bool isBerkeleyData(DataRefImpl Sec) const
Definition: ObjectFile.cpp:81
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
Definition: Error.cpp:139
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static Expected< ELFFile > create(StringRef Object)
Definition: ELF.h:446
SubtargetFeatures getFeatures() const override
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
std::vector< SectionRef > dynamic_relocation_sections() const override
bool isSectionBSS(DataRefImpl Sec) const override
bool isSectionText(DataRefImpl Sec) const override
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
friend class SectionRef
Definition: ObjectFile.h:258
elf_relocation_iterator(const relocation_iterator &B)
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:256
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
const ELFObjectFileBase * getObject() const
void moveSymbolNext(DataRefImpl &Symb) const override
static std::error_code checkOffset(MemoryBufferRef M, uintptr_t Addr, const uint64_t Size)
Definition: Binary.h:155
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
Offsets
Offsets in bytes from the start of the input buffer.
Definition: SIInstrInfo.h:1045
virtual bool isSectionBSS(DataRefImpl Sec) const =0
This class is the base class for all object file types.
Definition: ObjectFile.h:226
elf_symbol_iterator_range symbols() const
virtual Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const =0
typename ELFT::Rel Elf_Rel
virtual uint64_t getRelocationOffset(DataRefImpl Rel) const =0
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
const uint8_t * base() const
Definition: ObjectFile.h:232
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:850
static Error createError(StringRef Err)
Definition: ELF.h:47
const ObjectFile * getObject() const
Definition: ObjectFile.h:559
virtual uint32_t getSymbolAlignment(DataRefImpl Symb) const
Definition: ObjectFile.cpp:67
Base class for error info classes.
Definition: Error.h:48
bool isSectionVirtual(DataRefImpl Sec) const override
virtual Expected< StringRef > getSymbolName(DataRefImpl Symb) const =0
uint32_t getSectionType(DataRefImpl Sec) const override
virtual Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
void moveRelocationNext(DataRefImpl &Rel) const override
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const =0
basic_symbol_iterator symbol_begin() const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
virtual basic_symbol_iterator symbol_begin() const =0
Definition: BitVector.h:937
void setARMSubArch(Triple &TheTriple) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
ArrayRef< T > makeArrayRef(const T &OneElt)
Construct an ArrayRef from a single element.
Definition: ArrayRef.h:450
const Elf_Shdr * DotSymtabSec
uint64_t getSectionAlignment(DataRefImpl Sec) const override
Expected< Elf_Shdr_Range > sections() const
Definition: ELF.h:453
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:41
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
elf_section_iterator(const section_iterator &B)
typename ELFT::Ehdr Elf_Ehdr
virtual uint64_t getSectionIndex(DataRefImpl Sec) const =0
const ELFObjectFileBase * getObject() const
Tagged union holding either a T or a Error.
Definition: CachePruning.h:22
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:851
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:272
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const =0
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const ObjectFile * getObject() const
Definition: ObjectFile.h:408
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
virtual Error getBuildAttributes(ARMAttributeParser &Attributes) const =0
virtual uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const =0
section_iterator section_begin() const override
MemoryBufferRef getMemoryBufferRef() const
Definition: Binary.cpp:43
void moveSectionNext(DataRefImpl &Sec) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
virtual uint64_t getSectionAlignment(DataRefImpl Sec) const =0
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:32
virtual Expected< uint64_t > getStartAddress() const
Definition: ObjectFile.h:327
const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
constexpr int NumElfSymbolTypes
Definition: ELFObjectFile.h:44
struct llvm::object::DataRefImpl::@286 d
const ObjectFile * getObject() const
Definition: ObjectFile.h:521
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:334
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
Definition: Casting.h:249
elf_symbol_iterator dynamic_symbol_begin() const
relocation_iterator section_rel_end(DataRefImpl Sec) const override
typename ELFT::Shdr Elf_Shdr
Expected< int64_t > getRelocationAddend(DataRefImpl Rel) const override
std::vector< std::pair< DataRefImpl, uint64_t > > getPltAddresses() const
const content_type * operator->() const
Definition: SymbolicFile.h:76
unsigned getPlatformFlags() const override
Returns platform-specific object flags, if any.
const Elf_Shdr * getSection(DataRefImpl Sec) const
virtual Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const =0
virtual uint32_t getSymbolFlags(DataRefImpl Symb) const =0
const Elf_Sym * getSymbol(DataRefImpl Sym) const
ELFRelocationRef(const RelocationRef &B)
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:79
const ELFObjectFileBase * getObject() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
const Elf_Rela * getRela(DataRefImpl Rela) const
virtual uint8_t getBytesInAddress() const =0
The number of bytes used to represent an address in this object file format.
virtual void moveRelocationNext(DataRefImpl &Rel) const =0
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
unsigned int getType() const
Definition: Binary.h:95
uint32_t getSymbolFlags(DataRefImpl Symb) const override
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:70
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:87
typename ELFT::Sym Elf_Sym
const Elf_Shdr * DotDynSymSec
Expected< int64_t > getAddend() const
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:71
section_iterator getRelocatedSection(DataRefImpl Sec) const override
const Elf_Rel * getRel(DataRefImpl Rel) const
virtual std::vector< SectionRef > dynamic_relocation_sections() const
Definition: ObjectFile.h:302
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
typename ELFT::Dyn Elf_Dyn
virtual Expected< StringRef > getSectionName(DataRefImpl Sec) const =0
virtual basic_symbol_iterator symbol_end() const =0
static Expected< ELFObjectFile< ELFT > > create(MemoryBufferRef Object)
StringRef getELFTypeName() const
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
bool isELF() const
Definition: Binary.h:112
StringRef getFileFormatName() const override
const ELFSymbolRef & operator*() const
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:981
const ELFSectionRef & operator*() const
DataRefImpl toDRI(const Elf_Shdr *Sec) const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
const ELFSectionRef * operator->() const
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:43
uint64_t getSectionAddress(DataRefImpl Sec) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
bool isBerkeleyText(DataRefImpl Sec) const override
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:22
typename ELFT::Rela Elf_Rela
void handleAllErrors(Error E, HandlerTs &&... Handlers)
Behaves the same as handleErrors, except that by contract all errors must be handled by the given han...
Definition: Error.h:904
virtual Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const =0
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
friend class RelocationRef
Definition: ObjectFile.h:283
const ELFFile< ELFT > * getELFFile() const
virtual section_iterator section_begin() const =0
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:207
const SymbolRef & operator*() const
Definition: ObjectFile.h:217
virtual uint64_t getSectionAddress(DataRefImpl Sec) const =0
virtual Triple::ArchType getArch() const =0
uint64_t getSymbolValue(DataRefImpl Symb) const
Definition: ObjectFile.cpp:50
virtual unsigned getPlatformFlags() const =0
Returns platform-specific object flags, if any.
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const =0
uint64_t getSectionFlags(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
Triple::ArchType getArch() const override
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:138
elf_symbol_iterator dynamic_symbol_end() const
A range adaptor for a pair of iterators.
MemoryBufferRef Data
Definition: Binary.h:37
Manages the enabling and disabling of subtarget specific features.
bool isBerkeleyData(DataRefImpl Sec) const override
uint64_t getOffset() const
virtual bool isSectionText(DataRefImpl Sec) const =0
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
SubtargetFeatures getMIPSFeatures() const
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:165
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
uint8_t getELFType() const
virtual void moveSectionNext(DataRefImpl &Sec) const =0
Error getBuildAttributes(ARMAttributeParser &Attributes) const override
virtual section_iterator section_end() const =0
virtual bool isBerkeleyText(DataRefImpl Sec) const
Definition: ObjectFile.cpp:77
basic_symbol_iterator symbol_end() const override
virtual uint16_t getEType() const =0
bool isSectionCompressed(DataRefImpl Sec) const override
virtual bool isSectionData(DataRefImpl Sec) const =0
ELFSectionRef(const SectionRef &B)
Definition: ELFObjectFile.h:98
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
const ELFSymbolRef * operator->() const
uint64_t getSectionOffset(DataRefImpl Sec) const override
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:85
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
virtual void moveSymbolNext(DataRefImpl &Symb) const =0
virtual uint64_t getSymbolValueImpl(DataRefImpl Symb) const =0
virtual bool isRelocatableObject() const =0
True if this is a relocatable object (.o/.obj).
section_iterator section_end() const override
const SymbolRef * operator->() const
Definition: ObjectFile.h:212
virtual bool isSectionCompressed(DataRefImpl Sec) const =0
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
virtual Expected< section_iterator > getSymbolSection(DataRefImpl Symb) const =0
uint64_t getSize() const
aarch64 promote const
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
virtual bool isSectionVirtual(DataRefImpl Sec) const =0
void Parse(ArrayRef< uint8_t > Section, bool isLittle)
virtual uint64_t getSectionSize(DataRefImpl Sec) const =0
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
virtual void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const =0
const ELFRelocationRef & operator*() const
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
SubtargetFeatures getARMFeatures() const
uint64_t getRelocationType(DataRefImpl Rel) const override
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:103
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
SubtargetFeatures getRISCVFeatures() const
const Elf_Ehdr * getHeader() const
Definition: ELF.h:92
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
const ELFRelocationRef * operator->() const
elf_symbol_iterator(const basic_symbol_iterator &B)
virtual StringRef getFileFormatName() const =0
virtual uint64_t getRelocationType(DataRefImpl Rel) const =0
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:93
typename ELFT::uint uintX_t
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
ELFSymbolRef(const SymbolRef &B)
bool is_contained(R &&Range, const E &Element)
Wrapper function around std::find to detect if an element exists in a container.
Definition: STLExtras.h:1244