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