LLVM  10.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 = 16;
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 uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
58  virtual uint8_t getSymbolBinding(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  virtual uint16_t getEMachine() const = 0;
94 
95  std::vector<std::pair<DataRefImpl, uint64_t>> getPltAddresses() const;
96 };
97 
98 class ELFSectionRef : public SectionRef {
99 public:
101  assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
102  }
103 
104  const ELFObjectFileBase *getObject() const {
105  return cast<ELFObjectFileBase>(SectionRef::getObject());
106  }
107 
108  uint32_t getType() const {
109  return getObject()->getSectionType(getRawDataRefImpl());
110  }
111 
112  uint64_t getFlags() const {
113  return getObject()->getSectionFlags(getRawDataRefImpl());
114  }
115 
116  uint64_t getOffset() const {
117  return getObject()->getSectionOffset(getRawDataRefImpl());
118  }
119 };
120 
122 public:
124  assert(isa<ELFObjectFileBase>(B->getObject()));
125  }
126 
127  const ELFSectionRef *operator->() const {
128  return static_cast<const ELFSectionRef *>(section_iterator::operator->());
129  }
130 
131  const ELFSectionRef &operator*() const {
132  return static_cast<const ELFSectionRef &>(section_iterator::operator*());
133  }
134 };
135 
136 class ELFSymbolRef : public SymbolRef {
137 public:
139  assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
140  }
141 
142  const ELFObjectFileBase *getObject() const {
143  return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
144  }
145 
146  uint64_t getSize() const {
147  return getObject()->getSymbolSize(getRawDataRefImpl());
148  }
149 
150  uint8_t getBinding() const {
151  return getObject()->getSymbolBinding(getRawDataRefImpl());
152  }
153 
154  uint8_t getOther() const {
155  return getObject()->getSymbolOther(getRawDataRefImpl());
156  }
157 
158  uint8_t getELFType() const {
159  return getObject()->getSymbolELFType(getRawDataRefImpl());
160  }
161 
163  uint8_t Type = getELFType();
164  for (auto &EE : ElfSymbolTypes) {
165  if (EE.Value == Type) {
166  return EE.AltName;
167  }
168  }
169  return "";
170  }
171 };
172 
174 public:
176  : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
177  cast<ELFObjectFileBase>(B->getObject()))) {}
178 
179  const ELFSymbolRef *operator->() const {
180  return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
181  }
182 
183  const ELFSymbolRef &operator*() const {
184  return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
185  }
186 };
187 
189 public:
191  assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
192  }
193 
194  const ELFObjectFileBase *getObject() const {
195  return cast<ELFObjectFileBase>(RelocationRef::getObject());
196  }
197 
199  return getObject()->getRelocationAddend(getRawDataRefImpl());
200  }
201 };
202 
204 public:
207  B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
208 
209  const ELFRelocationRef *operator->() const {
210  return static_cast<const ELFRelocationRef *>(
212  }
213 
214  const ELFRelocationRef &operator*() const {
215  return static_cast<const ELFRelocationRef &>(
217  }
218 };
219 
223 }
224 
225 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
226  uint16_t getEMachine() const override;
227  uint16_t getEType() const override;
228  uint64_t getSymbolSize(DataRefImpl Sym) const override;
229 
230 public:
232 
233  using uintX_t = typename ELFT::uint;
234 
235  using Elf_Sym = typename ELFT::Sym;
236  using Elf_Shdr = typename ELFT::Shdr;
237  using Elf_Ehdr = typename ELFT::Ehdr;
238  using Elf_Rel = typename ELFT::Rel;
239  using Elf_Rela = typename ELFT::Rela;
240  using Elf_Dyn = typename ELFT::Dyn;
241 
242  SectionRef toSectionRef(const Elf_Shdr *Sec) const {
243  return SectionRef(toDRI(Sec), this);
244  }
245 
246 private:
248  const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
249  ArrayRef<Elf_Word> ShndxTable);
250 
251 protected:
253 
254  const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
255  const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
257 
258  void moveSymbolNext(DataRefImpl &Symb) const override;
259  Expected<StringRef> getSymbolName(DataRefImpl Symb) const override;
260  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
261  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
262  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
263  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
264  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
265  uint8_t getSymbolBinding(DataRefImpl Symb) const override;
266  uint8_t getSymbolOther(DataRefImpl Symb) const override;
267  uint8_t getSymbolELFType(DataRefImpl Symb) const override;
270  const Elf_Shdr *SymTab) const;
272 
273  void moveSectionNext(DataRefImpl &Sec) const override;
274  Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
275  uint64_t getSectionAddress(DataRefImpl Sec) const override;
276  uint64_t getSectionIndex(DataRefImpl Sec) const override;
277  uint64_t getSectionSize(DataRefImpl Sec) const override;
279  getSectionContents(DataRefImpl Sec) const override;
280  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
281  bool isSectionCompressed(DataRefImpl Sec) const override;
282  bool isSectionText(DataRefImpl Sec) const override;
283  bool isSectionData(DataRefImpl Sec) const override;
284  bool isSectionBSS(DataRefImpl Sec) const override;
285  bool isSectionVirtual(DataRefImpl Sec) const override;
286  bool isBerkeleyText(DataRefImpl Sec) const override;
287  bool isBerkeleyData(DataRefImpl Sec) const override;
289  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
290  std::vector<SectionRef> dynamic_relocation_sections() const override;
292  getRelocatedSection(DataRefImpl Sec) const override;
293 
294  void moveRelocationNext(DataRefImpl &Rel) const override;
295  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
296  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
297  uint64_t getRelocationType(DataRefImpl Rel) const override;
299  SmallVectorImpl<char> &Result) const override;
300 
301  uint32_t getSectionType(DataRefImpl Sec) const override;
302  uint64_t getSectionFlags(DataRefImpl Sec) const override;
303  uint64_t getSectionOffset(DataRefImpl Sec) const override;
305 
306  /// Get the relocation section that contains \a Rel.
307  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
308  auto RelSecOrErr = EF.getSection(Rel.d.a);
309  if (!RelSecOrErr)
310  report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
311  return *RelSecOrErr;
312  }
313 
314  DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
315  DataRefImpl DRI;
316  if (!SymTable) {
317  DRI.d.a = 0;
318  DRI.d.b = 0;
319  return DRI;
320  }
321  assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
322  SymTable->sh_type == ELF::SHT_DYNSYM);
323 
324  auto SectionsOrErr = EF.sections();
325  if (!SectionsOrErr) {
326  DRI.d.a = 0;
327  DRI.d.b = 0;
328  return DRI;
329  }
330  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
331  unsigned SymTableIndex =
332  (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
333 
334  DRI.d.a = SymTableIndex;
335  DRI.d.b = SymbolNum;
336  return DRI;
337  }
338 
339  const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
340  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
341  }
342 
343  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
344  DataRefImpl DRI;
345  DRI.p = reinterpret_cast<uintptr_t>(Sec);
346  return DRI;
347  }
348 
349  DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
350  DataRefImpl DRI;
351  DRI.p = reinterpret_cast<uintptr_t>(Dyn);
352  return DRI;
353  }
354 
355  bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
356  unsigned char Binding = ESym->getBinding();
357  unsigned char Visibility = ESym->getVisibility();
358 
359  // A symbol is exported if its binding is either GLOBAL or WEAK, and its
360  // visibility is either DEFAULT or PROTECTED. All other symbols are not
361  // exported.
362  return (
363  (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
364  Binding == ELF::STB_GNU_UNIQUE) &&
365  (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
366  }
367 
368  Error getBuildAttributes(ARMAttributeParser &Attributes) const override {
369  auto SectionsOrErr = EF.sections();
370  if (!SectionsOrErr)
371  return SectionsOrErr.takeError();
372 
373  for (const Elf_Shdr &Sec : *SectionsOrErr) {
374  if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
375  auto ErrorOrContents = EF.getSectionContents(&Sec);
376  if (!ErrorOrContents)
377  return ErrorOrContents.takeError();
378 
379  auto Contents = ErrorOrContents.get();
380  if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
381  return Error::success();
382 
383  Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
384  break;
385  }
386  }
387  return Error::success();
388  }
389 
390  // This flag is used for classof, to distinguish ELFObjectFile from
391  // its subclass. If more subclasses will be created, this flag will
392  // have to become an enum.
394 
395 public:
397  static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
398 
399  const Elf_Rel *getRel(DataRefImpl Rel) const;
400  const Elf_Rela *getRela(DataRefImpl Rela) const;
401 
402  const Elf_Sym *getSymbol(DataRefImpl Sym) const {
403  auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
404  if (!Ret)
405  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
406  return *Ret;
407  }
408 
409  const Elf_Shdr *getSection(DataRefImpl Sec) const {
410  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
411  }
412 
413  basic_symbol_iterator symbol_begin() const override;
414  basic_symbol_iterator symbol_end() const override;
415 
416  elf_symbol_iterator dynamic_symbol_begin() const;
417  elf_symbol_iterator dynamic_symbol_end() const;
418 
419  section_iterator section_begin() const override;
420  section_iterator section_end() const override;
421 
423 
424  uint8_t getBytesInAddress() const override;
425  StringRef getFileFormatName() const override;
426  Triple::ArchType getArch() const override;
427  Expected<uint64_t> getStartAddress() const override;
428 
429  unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
430 
431  const ELFFile<ELFT> *getELFFile() const { return &EF; }
432 
433  bool isDyldType() const { return isDyldELFObject; }
434  static bool classof(const Binary *v) {
435  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
436  ELFT::Is64Bits);
437  }
438 
440 
441  bool isRelocatableObject() const override;
442 };
443 
448 
449 template <class ELFT>
451  ++Sym.d.b;
452 }
453 
454 template <class ELFT>
456  const Elf_Sym *ESym = getSymbol(Sym);
457  auto SymTabOrErr = EF.getSection(Sym.d.a);
458  if (!SymTabOrErr)
459  return SymTabOrErr.takeError();
460  const Elf_Shdr *SymTableSec = *SymTabOrErr;
461  auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
462  if (!StrTabOrErr)
463  return StrTabOrErr.takeError();
464  const Elf_Shdr *StringTableSec = *StrTabOrErr;
465  auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
466  if (!SymStrTabOrErr)
467  return SymStrTabOrErr.takeError();
468  Expected<StringRef> Name = ESym->getName(*SymStrTabOrErr);
469  if (Name && !Name->empty())
470  return Name;
471 
472  // If the symbol name is empty use the section name.
473  if (ESym->getType() == ELF::STT_SECTION) {
474  if (Expected<section_iterator> SecOrErr = getSymbolSection(Sym)) {
475  consumeError(Name.takeError());
476  return (*SecOrErr)->getName();
477  }
478  }
479  return Name;
480 }
481 
482 template <class ELFT>
484  return getSection(Sec)->sh_flags;
485 }
486 
487 template <class ELFT>
489  return getSection(Sec)->sh_type;
490 }
491 
492 template <class ELFT>
494  return getSection(Sec)->sh_offset;
495 }
496 
497 template <class ELFT>
499  const Elf_Sym *ESym = getSymbol(Symb);
500  uint64_t Ret = ESym->st_value;
501  if (ESym->st_shndx == ELF::SHN_ABS)
502  return Ret;
503 
504  const Elf_Ehdr *Header = EF.getHeader();
505  // Clear the ARM/Thumb or microMIPS indicator flag.
506  if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
507  ESym->getType() == ELF::STT_FUNC)
508  Ret &= ~1;
509 
510  return Ret;
511 }
512 
513 template <class ELFT>
516  uint64_t Result = getSymbolValue(Symb);
517  const Elf_Sym *ESym = getSymbol(Symb);
518  switch (ESym->st_shndx) {
519  case ELF::SHN_COMMON:
520  case ELF::SHN_UNDEF:
521  case ELF::SHN_ABS:
522  return Result;
523  }
524 
525  const Elf_Ehdr *Header = EF.getHeader();
526  auto SymTabOrErr = EF.getSection(Symb.d.a);
527  if (!SymTabOrErr)
528  return SymTabOrErr.takeError();
529  const Elf_Shdr *SymTab = *SymTabOrErr;
530 
531  if (Header->e_type == ELF::ET_REL) {
532  auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
533  if (!SectionOrErr)
534  return SectionOrErr.takeError();
535  const Elf_Shdr *Section = *SectionOrErr;
536  if (Section)
537  Result += Section->sh_addr;
538  }
539 
540  return Result;
541 }
542 
543 template <class ELFT>
545  const Elf_Sym *Sym = getSymbol(Symb);
546  if (Sym->st_shndx == ELF::SHN_COMMON)
547  return Sym->st_value;
548  return 0;
549 }
550 
551 template <class ELFT>
552 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
553  return EF.getHeader()->e_machine;
554 }
555 
556 template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
557  return EF.getHeader()->e_type;
558 }
559 
560 template <class ELFT>
562  return getSymbol(Sym)->st_size;
563 }
564 
565 template <class ELFT>
567  return getSymbol(Symb)->st_size;
568 }
569 
570 template <class ELFT>
572  return getSymbol(Symb)->getBinding();
573 }
574 
575 template <class ELFT>
577  return getSymbol(Symb)->st_other;
578 }
579 
580 template <class ELFT>
582  return getSymbol(Symb)->getType();
583 }
584 
585 template <class ELFT>
588  const Elf_Sym *ESym = getSymbol(Symb);
589 
590  switch (ESym->getType()) {
591  case ELF::STT_NOTYPE:
592  return SymbolRef::ST_Unknown;
593  case ELF::STT_SECTION:
594  return SymbolRef::ST_Debug;
595  case ELF::STT_FILE:
596  return SymbolRef::ST_File;
597  case ELF::STT_FUNC:
598  return SymbolRef::ST_Function;
599  case ELF::STT_OBJECT:
600  case ELF::STT_COMMON:
601  case ELF::STT_TLS:
602  return SymbolRef::ST_Data;
603  default:
604  return SymbolRef::ST_Other;
605  }
606 }
607 
608 template <class ELFT>
610  const Elf_Sym *ESym = getSymbol(Sym);
611 
612  uint32_t Result = SymbolRef::SF_None;
613 
614  if (ESym->getBinding() != ELF::STB_LOCAL)
615  Result |= SymbolRef::SF_Global;
616 
617  if (ESym->getBinding() == ELF::STB_WEAK)
618  Result |= SymbolRef::SF_Weak;
619 
620  if (ESym->st_shndx == ELF::SHN_ABS)
621  Result |= SymbolRef::SF_Absolute;
622 
623  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
625 
626  auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
627  if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
629  auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
630  if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
632 
633  if (EF.getHeader()->e_machine == ELF::EM_ARM) {
634  if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
635  StringRef Name = *NameOrErr;
636  if (Name.startswith("$d") || Name.startswith("$t") ||
637  Name.startswith("$a"))
639  } else {
640  // TODO: Actually report errors helpfully.
641  consumeError(NameOrErr.takeError());
642  }
643  if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
644  Result |= SymbolRef::SF_Thumb;
645  }
646 
647  if (ESym->st_shndx == ELF::SHN_UNDEF)
648  Result |= SymbolRef::SF_Undefined;
649 
650  if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
651  Result |= SymbolRef::SF_Common;
652 
653  if (isExportedToOtherDSO(ESym))
654  Result |= SymbolRef::SF_Exported;
655 
656  if (ESym->getVisibility() == ELF::STV_HIDDEN)
657  Result |= SymbolRef::SF_Hidden;
658 
659  return Result;
660 }
661 
662 template <class ELFT>
665  const Elf_Shdr *SymTab) const {
666  auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
667  if (!ESecOrErr)
668  return ESecOrErr.takeError();
669 
670  const Elf_Shdr *ESec = *ESecOrErr;
671  if (!ESec)
672  return section_end();
673 
674  DataRefImpl Sec;
675  Sec.p = reinterpret_cast<intptr_t>(ESec);
676  return section_iterator(SectionRef(Sec, this));
677 }
678 
679 template <class ELFT>
682  const Elf_Sym *Sym = getSymbol(Symb);
683  auto SymTabOrErr = EF.getSection(Symb.d.a);
684  if (!SymTabOrErr)
685  return SymTabOrErr.takeError();
686  const Elf_Shdr *SymTab = *SymTabOrErr;
687  return getSymbolSection(Sym, SymTab);
688 }
689 
690 template <class ELFT>
692  const Elf_Shdr *ESec = getSection(Sec);
693  Sec = toDRI(++ESec);
694 }
695 
696 template <class ELFT>
698  return EF.getSectionName(&*getSection(Sec));
699 }
700 
701 template <class ELFT>
703  return getSection(Sec)->sh_addr;
704 }
705 
706 template <class ELFT>
708  auto SectionsOrErr = EF.sections();
709  handleAllErrors(std::move(SectionsOrErr.takeError()),
710  [](const ErrorInfoBase &) {
711  llvm_unreachable("unable to get section index");
712  });
713  const Elf_Shdr *First = SectionsOrErr->begin();
714  return getSection(Sec) - First;
715 }
716 
717 template <class ELFT>
719  return getSection(Sec)->sh_size;
720 }
721 
722 template <class ELFT>
725  const Elf_Shdr *EShdr = getSection(Sec);
726  if (std::error_code EC =
728  (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
729  return errorCodeToError(EC);
730  return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
731  EShdr->sh_size);
732 }
733 
734 template <class ELFT>
736  return getSection(Sec)->sh_addralign;
737 }
738 
739 template <class ELFT>
741  return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
742 }
743 
744 template <class ELFT>
746  return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
747 }
748 
749 template <class ELFT>
751  const Elf_Shdr *EShdr = getSection(Sec);
752  return EShdr->sh_type == ELF::SHT_PROGBITS &&
753  EShdr->sh_flags & ELF::SHF_ALLOC &&
754  !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
755 }
756 
757 template <class ELFT>
759  const Elf_Shdr *EShdr = getSection(Sec);
760  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
761  EShdr->sh_type == ELF::SHT_NOBITS;
762 }
763 
764 template <class ELFT>
765 std::vector<SectionRef>
767  std::vector<SectionRef> Res;
768  std::vector<uintptr_t> Offsets;
769 
770  auto SectionsOrErr = EF.sections();
771  if (!SectionsOrErr)
772  return Res;
773 
774  for (const Elf_Shdr &Sec : *SectionsOrErr) {
775  if (Sec.sh_type != ELF::SHT_DYNAMIC)
776  continue;
777  Elf_Dyn *Dynamic =
778  reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
779  for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
780  if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
781  Dynamic->d_tag == ELF::DT_JMPREL) {
782  Offsets.push_back(Dynamic->d_un.d_val);
783  }
784  }
785  }
786  for (const Elf_Shdr &Sec : *SectionsOrErr) {
787  if (is_contained(Offsets, Sec.sh_addr))
788  Res.emplace_back(toDRI(&Sec), this);
789  }
790  return Res;
791 }
792 
793 template <class ELFT>
795  return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
796 }
797 
798 template <class ELFT>
800  return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
801  (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
802  !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
803 }
804 
805 template <class ELFT>
807  const Elf_Shdr *EShdr = getSection(Sec);
808  return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
809  EShdr->sh_flags & ELF::SHF_ALLOC;
810 }
811 
812 template <class ELFT>
815  DataRefImpl RelData;
816  auto SectionsOrErr = EF.sections();
817  if (!SectionsOrErr)
819  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
820  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
821  RelData.d.b = 0;
822  return relocation_iterator(RelocationRef(RelData, this));
823 }
824 
825 template <class ELFT>
828  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
830  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
831  return Begin;
832  DataRefImpl RelData = Begin->getRawDataRefImpl();
833  const Elf_Shdr *RelSec = getRelSection(RelData);
834 
835  // Error check sh_link here so that getRelocationSymbol can just use it.
836  auto SymSecOrErr = EF.getSection(RelSec->sh_link);
837  if (!SymSecOrErr)
838  report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
839 
840  RelData.d.b += S->sh_size / S->sh_entsize;
841  return relocation_iterator(RelocationRef(RelData, this));
842 }
843 
844 template <class ELFT>
847  if (EF.getHeader()->e_type != ELF::ET_REL)
848  return section_end();
849 
850  const Elf_Shdr *EShdr = getSection(Sec);
851  uintX_t Type = EShdr->sh_type;
852  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
853  return section_end();
854 
855  Expected<const Elf_Shdr *> SecOrErr = EF.getSection(EShdr->sh_info);
856  if (!SecOrErr)
857  return SecOrErr.takeError();
858  return section_iterator(SectionRef(toDRI(*SecOrErr), this));
859 }
860 
861 // Relocations
862 template <class ELFT>
864  ++Rel.d.b;
865 }
866 
867 template <class ELFT>
870  uint32_t symbolIdx;
871  const Elf_Shdr *sec = getRelSection(Rel);
872  if (sec->sh_type == ELF::SHT_REL)
873  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
874  else
875  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
876  if (!symbolIdx)
877  return symbol_end();
878 
879  // FIXME: error check symbolIdx
880  DataRefImpl SymbolData;
881  SymbolData.d.a = sec->sh_link;
882  SymbolData.d.b = symbolIdx;
883  return symbol_iterator(SymbolRef(SymbolData, this));
884 }
885 
886 template <class ELFT>
888  const Elf_Shdr *sec = getRelSection(Rel);
889  if (sec->sh_type == ELF::SHT_REL)
890  return getRel(Rel)->r_offset;
891 
892  return getRela(Rel)->r_offset;
893 }
894 
895 template <class ELFT>
897  const Elf_Shdr *sec = getRelSection(Rel);
898  if (sec->sh_type == ELF::SHT_REL)
899  return getRel(Rel)->getType(EF.isMips64EL());
900  else
901  return getRela(Rel)->getType(EF.isMips64EL());
902 }
903 
904 template <class ELFT>
906  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
907 }
908 
909 template <class ELFT>
911  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
913  EF.getRelocationTypeName(type, Result);
914 }
915 
916 template <class ELFT>
919  if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
920  return createError("Section is not SHT_RELA");
921  return (int64_t)getRela(Rel)->r_addend;
922 }
923 
924 template <class ELFT>
925 const typename ELFObjectFile<ELFT>::Elf_Rel *
927  assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
928  auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
929  if (!Ret)
930  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
931  return *Ret;
932 }
933 
934 template <class ELFT>
935 const typename ELFObjectFile<ELFT>::Elf_Rela *
937  assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
938  auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
939  if (!Ret)
940  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
941  return *Ret;
942 }
943 
944 template <class ELFT>
947  auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
948  if (Error E = EFOrErr.takeError())
949  return std::move(E);
950  auto EF = std::move(*EFOrErr);
951 
952  auto SectionsOrErr = EF.sections();
953  if (!SectionsOrErr)
954  return SectionsOrErr.takeError();
955 
956  const Elf_Shdr *DotDynSymSec = nullptr;
957  const Elf_Shdr *DotSymtabSec = nullptr;
958  ArrayRef<Elf_Word> ShndxTable;
959  for (const Elf_Shdr &Sec : *SectionsOrErr) {
960  switch (Sec.sh_type) {
961  case ELF::SHT_DYNSYM: {
962  if (!DotDynSymSec)
963  DotDynSymSec = &Sec;
964  break;
965  }
966  case ELF::SHT_SYMTAB: {
967  if (!DotSymtabSec)
968  DotSymtabSec = &Sec;
969  break;
970  }
971  case ELF::SHT_SYMTAB_SHNDX: {
972  auto TableOrErr = EF.getSHNDXTable(Sec);
973  if (!TableOrErr)
974  return TableOrErr.takeError();
975  ShndxTable = *TableOrErr;
976  break;
977  }
978  }
979  }
980  return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
981  ShndxTable);
982 }
983 
984 template <class ELFT>
986  const Elf_Shdr *DotDynSymSec,
987  const Elf_Shdr *DotSymtabSec,
988  ArrayRef<Elf_Word> ShndxTable)
990  getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
991  Object),
992  EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
993  ShndxTable(ShndxTable) {}
994 
995 template <class ELFT>
996 ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
997  : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
998  Other.DotSymtabSec, Other.ShndxTable) {}
999 
1000 template <class ELFT>
1002  DataRefImpl Sym =
1003  toDRI(DotSymtabSec,
1004  DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1005  return basic_symbol_iterator(SymbolRef(Sym, this));
1006 }
1007 
1008 template <class ELFT>
1010  const Elf_Shdr *SymTab = DotSymtabSec;
1011  if (!SymTab)
1012  return symbol_begin();
1013  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1014  return basic_symbol_iterator(SymbolRef(Sym, this));
1015 }
1016 
1017 template <class ELFT>
1019  DataRefImpl Sym = toDRI(DotDynSymSec, 0);
1020  return symbol_iterator(SymbolRef(Sym, this));
1021 }
1022 
1023 template <class ELFT>
1025  const Elf_Shdr *SymTab = DotDynSymSec;
1026  if (!SymTab)
1027  return dynamic_symbol_begin();
1028  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1029  return basic_symbol_iterator(SymbolRef(Sym, this));
1030 }
1031 
1032 template <class ELFT>
1034  auto SectionsOrErr = EF.sections();
1035  if (!SectionsOrErr)
1036  return section_iterator(SectionRef());
1037  return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1038 }
1039 
1040 template <class ELFT>
1042  auto SectionsOrErr = EF.sections();
1043  if (!SectionsOrErr)
1044  return section_iterator(SectionRef());
1045  return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1046 }
1047 
1048 template <class ELFT>
1050  return ELFT::Is64Bits ? 8 : 4;
1051 }
1052 
1053 template <class ELFT>
1055  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1056  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1057  case ELF::ELFCLASS32:
1058  switch (EF.getHeader()->e_machine) {
1059  case ELF::EM_386:
1060  return "ELF32-i386";
1061  case ELF::EM_IAMCU:
1062  return "ELF32-iamcu";
1063  case ELF::EM_X86_64:
1064  return "ELF32-x86-64";
1065  case ELF::EM_ARM:
1066  return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
1067  case ELF::EM_AVR:
1068  return "ELF32-avr";
1069  case ELF::EM_HEXAGON:
1070  return "ELF32-hexagon";
1071  case ELF::EM_LANAI:
1072  return "ELF32-lanai";
1073  case ELF::EM_MIPS:
1074  return "ELF32-mips";
1075  case ELF::EM_MSP430:
1076  return "ELF32-msp430";
1077  case ELF::EM_PPC:
1078  return "ELF32-ppc";
1079  case ELF::EM_RISCV:
1080  return "ELF32-riscv";
1081  case ELF::EM_SPARC:
1082  case ELF::EM_SPARC32PLUS:
1083  return "ELF32-sparc";
1084  case ELF::EM_AMDGPU:
1085  return "ELF32-amdgpu";
1086  default:
1087  return "ELF32-unknown";
1088  }
1089  case ELF::ELFCLASS64:
1090  switch (EF.getHeader()->e_machine) {
1091  case ELF::EM_386:
1092  return "ELF64-i386";
1093  case ELF::EM_X86_64:
1094  return "ELF64-x86-64";
1095  case ELF::EM_AARCH64:
1096  return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
1097  case ELF::EM_PPC64:
1098  return "ELF64-ppc64";
1099  case ELF::EM_RISCV:
1100  return "ELF64-riscv";
1101  case ELF::EM_S390:
1102  return "ELF64-s390";
1103  case ELF::EM_SPARCV9:
1104  return "ELF64-sparc";
1105  case ELF::EM_MIPS:
1106  return "ELF64-mips";
1107  case ELF::EM_AMDGPU:
1108  return "ELF64-amdgpu";
1109  case ELF::EM_BPF:
1110  return "ELF64-BPF";
1111  default:
1112  return "ELF64-unknown";
1113  }
1114  default:
1115  // FIXME: Proper error handling.
1116  report_fatal_error("Invalid ELFCLASS!");
1117  }
1118 }
1119 
1120 template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1121  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1122  switch (EF.getHeader()->e_machine) {
1123  case ELF::EM_386:
1124  case ELF::EM_IAMCU:
1125  return Triple::x86;
1126  case ELF::EM_X86_64:
1127  return Triple::x86_64;
1128  case ELF::EM_AARCH64:
1129  return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1130  case ELF::EM_ARM:
1131  return Triple::arm;
1132  case ELF::EM_AVR:
1133  return Triple::avr;
1134  case ELF::EM_HEXAGON:
1135  return Triple::hexagon;
1136  case ELF::EM_LANAI:
1137  return Triple::lanai;
1138  case ELF::EM_MIPS:
1139  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1140  case ELF::ELFCLASS32:
1141  return IsLittleEndian ? Triple::mipsel : Triple::mips;
1142  case ELF::ELFCLASS64:
1143  return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1144  default:
1145  report_fatal_error("Invalid ELFCLASS!");
1146  }
1147  case ELF::EM_MSP430:
1148  return Triple::msp430;
1149  case ELF::EM_PPC:
1150  return Triple::ppc;
1151  case ELF::EM_PPC64:
1152  return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1153  case ELF::EM_RISCV:
1154  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1155  case ELF::ELFCLASS32:
1156  return Triple::riscv32;
1157  case ELF::ELFCLASS64:
1158  return Triple::riscv64;
1159  default:
1160  report_fatal_error("Invalid ELFCLASS!");
1161  }
1162  case ELF::EM_S390:
1163  return Triple::systemz;
1164 
1165  case ELF::EM_SPARC:
1166  case ELF::EM_SPARC32PLUS:
1167  return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1168  case ELF::EM_SPARCV9:
1169  return Triple::sparcv9;
1170 
1171  case ELF::EM_AMDGPU: {
1172  if (!IsLittleEndian)
1173  return Triple::UnknownArch;
1174 
1175  unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
1176  if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1178  return Triple::r600;
1179  if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
1181  return Triple::amdgcn;
1182 
1183  return Triple::UnknownArch;
1184  }
1185 
1186  case ELF::EM_BPF:
1187  return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1188 
1189  default:
1190  return Triple::UnknownArch;
1191  }
1192 }
1193 
1194 template <class ELFT>
1196  return EF.getHeader()->e_entry;
1197 }
1198 
1199 template <class ELFT>
1203 }
1204 
1205 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1206  return EF.getHeader()->e_type == ELF::ET_REL;
1207 }
1208 
1209 } // end namespace object
1210 } // end namespace llvm
1211 
1212 #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:239
virtual bool isBerkeleyData(DataRefImpl Sec) const
Definition: ObjectFile.cpp:83
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:496
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:253
uint8_t getBinding() const
virtual Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const
Definition: ObjectFile.cpp:88
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:270
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:163
Optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:953
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:1136
virtual bool isSectionBSS(DataRefImpl Sec) const =0
This class is the base class for all object file types.
Definition: ObjectFile.h:221
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
const uint8_t * base() const
Definition: ObjectFile.h:227
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
const ObjectFile * getObject() const
Definition: ObjectFile.h:550
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
Expected< const Elf_Shdr * > getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, ArrayRef< Elf_Word > ShndxTable) const
Definition: ELF.h:353
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:505
StringRef getBuffer() const
Definition: MemoryBuffer.h:272
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
static Expected< const T * > getObject(MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
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: yaml2obj.h:21
Expected< ArrayRef< uint8_t > > getSectionContents(const Elf_Shdr *Sec) const
Definition: ELF.h:422
Expected< StringRef > getSectionName(DataRefImpl Sec) const override
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:311
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 Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
const ObjectFile * getObject() const
Definition: ObjectFile.h:403
static Error createError(const Twine &Err)
Definition: ELF.h:47
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:44
void moveSectionNext(DataRefImpl &Sec) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
virtual uint64_t getSectionAlignment(DataRefImpl Sec) const =0
virtual Expected< uint64_t > getStartAddress() const
Definition: ObjectFile.h:322
const llvm::EnumEntry< unsigned > ElfSymbolTypes[NumElfSymbolTypes]
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
constexpr int NumElfSymbolTypes
Definition: ELFObjectFile.h:44
ArrayRef< Elf_Word > ShndxTable
const ObjectFile * getObject() const
Definition: ObjectFile.h:512
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
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
uint8_t getSymbolBinding(DataRefImpl Symb) const override
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:46
unsigned int getType() const
Definition: Binary.h:99
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:75
const Elf_Rel * getRel(DataRefImpl Rel) const
virtual std::vector< SectionRef > dynamic_relocation_sections() const
Definition: ObjectFile.h:297
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:118
StringRef getFileFormatName() const override
const ELFSymbolRef & operator*() const
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:1001
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
struct llvm::object::DataRefImpl::@297 d
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:924
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:278
Expected< section_iterator > getRelocatedSection(DataRefImpl Sec) const override
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:212
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:160
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:79
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)
Expected< ArrayRef< uint8_t > > getSectionContents(DataRefImpl Sec) const override
const ELFSymbolRef * operator->() const
uint64_t getSectionOffset(DataRefImpl Sec) const override
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
virtual uint8_t getSymbolBinding(DataRefImpl Symb) const =0
const SymbolRef * operator->() const
Definition: ObjectFile.h:207
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:119
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:1224