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