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