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