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 private:
243  ELFObjectFile(MemoryBufferRef Object, ELFFile<ELFT> EF,
244  const Elf_Shdr *DotDynSymSec, const Elf_Shdr *DotSymtabSec,
245  ArrayRef<Elf_Word> ShndxTable);
246 
247 protected:
248  ELFFile<ELFT> EF;
249 
250  const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
251  const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
252  ArrayRef<Elf_Word> ShndxTable;
253 
254  void moveSymbolNext(DataRefImpl &Symb) const override;
256  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
257  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
259  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
260  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
261  uint8_t getSymbolBinding(DataRefImpl Symb) const override;
262  uint8_t getSymbolOther(DataRefImpl Symb) const override;
263  uint8_t getSymbolELFType(DataRefImpl Symb) const override;
266  const Elf_Shdr *SymTab) const;
268 
269  void moveSectionNext(DataRefImpl &Sec) const override;
270  Expected<StringRef> getSectionName(DataRefImpl Sec) const override;
271  uint64_t getSectionAddress(DataRefImpl Sec) const override;
272  uint64_t getSectionIndex(DataRefImpl Sec) const override;
273  uint64_t getSectionSize(DataRefImpl Sec) const override;
274  Expected<ArrayRef<uint8_t>>
275  getSectionContents(DataRefImpl Sec) const override;
276  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
277  bool isSectionCompressed(DataRefImpl Sec) const override;
278  bool isSectionText(DataRefImpl Sec) const override;
279  bool isSectionData(DataRefImpl Sec) const override;
280  bool isSectionBSS(DataRefImpl Sec) const override;
281  bool isSectionVirtual(DataRefImpl Sec) const override;
282  bool isBerkeleyText(DataRefImpl Sec) const override;
283  bool isBerkeleyData(DataRefImpl Sec) const override;
285  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
286  std::vector<SectionRef> dynamic_relocation_sections() const override;
288 
289  void moveRelocationNext(DataRefImpl &Rel) const override;
290  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
291  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
292  uint64_t getRelocationType(DataRefImpl Rel) const override;
294  SmallVectorImpl<char> &Result) const override;
295 
296  uint32_t getSectionType(DataRefImpl Sec) const override;
297  uint64_t getSectionFlags(DataRefImpl Sec) const override;
298  uint64_t getSectionOffset(DataRefImpl Sec) const override;
300 
301  /// Get the relocation section that contains \a Rel.
302  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
303  auto RelSecOrErr = EF.getSection(Rel.d.a);
304  if (!RelSecOrErr)
305  report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
306  return *RelSecOrErr;
307  }
308 
309  DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
310  DataRefImpl DRI;
311  if (!SymTable) {
312  DRI.d.a = 0;
313  DRI.d.b = 0;
314  return DRI;
315  }
316  assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
317  SymTable->sh_type == ELF::SHT_DYNSYM);
318 
319  auto SectionsOrErr = EF.sections();
320  if (!SectionsOrErr) {
321  DRI.d.a = 0;
322  DRI.d.b = 0;
323  return DRI;
324  }
325  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
326  unsigned SymTableIndex =
327  (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
328 
329  DRI.d.a = SymTableIndex;
330  DRI.d.b = SymbolNum;
331  return DRI;
332  }
333 
334  const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
335  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
336  }
337 
338  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
339  DataRefImpl DRI;
340  DRI.p = reinterpret_cast<uintptr_t>(Sec);
341  return DRI;
342  }
343 
344  DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
345  DataRefImpl DRI;
346  DRI.p = reinterpret_cast<uintptr_t>(Dyn);
347  return DRI;
348  }
349 
350  bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
351  unsigned char Binding = ESym->getBinding();
352  unsigned char Visibility = ESym->getVisibility();
353 
354  // A symbol is exported if its binding is either GLOBAL or WEAK, and its
355  // visibility is either DEFAULT or PROTECTED. All other symbols are not
356  // exported.
357  return (
358  (Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK ||
359  Binding == ELF::STB_GNU_UNIQUE) &&
360  (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED));
361  }
362 
363  Error getBuildAttributes(ARMAttributeParser &Attributes) const override {
364  auto SectionsOrErr = EF.sections();
365  if (!SectionsOrErr)
366  return SectionsOrErr.takeError();
367 
368  for (const Elf_Shdr &Sec : *SectionsOrErr) {
369  if (Sec.sh_type == ELF::SHT_ARM_ATTRIBUTES) {
370  auto ErrorOrContents = EF.getSectionContents(&Sec);
371  if (!ErrorOrContents)
372  return ErrorOrContents.takeError();
373 
374  auto Contents = ErrorOrContents.get();
375  if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
376  return Error::success();
377 
378  Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
379  break;
380  }
381  }
382  return Error::success();
383  }
384 
385  // This flag is used for classof, to distinguish ELFObjectFile from
386  // its subclass. If more subclasses will be created, this flag will
387  // have to become an enum.
389 
390 public:
392  static Expected<ELFObjectFile<ELFT>> create(MemoryBufferRef Object);
393 
394  const Elf_Rel *getRel(DataRefImpl Rel) const;
395  const Elf_Rela *getRela(DataRefImpl Rela) const;
396 
397  const Elf_Sym *getSymbol(DataRefImpl Sym) const {
398  auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
399  if (!Ret)
400  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
401  return *Ret;
402  }
403 
404  const Elf_Shdr *getSection(DataRefImpl Sec) const {
405  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
406  }
407 
408  basic_symbol_iterator symbol_begin() const override;
409  basic_symbol_iterator symbol_end() const override;
410 
411  elf_symbol_iterator dynamic_symbol_begin() const;
412  elf_symbol_iterator dynamic_symbol_end() const;
413 
414  section_iterator section_begin() const override;
415  section_iterator section_end() const override;
416 
418 
419  uint8_t getBytesInAddress() const override;
420  StringRef getFileFormatName() const override;
421  Triple::ArchType getArch() const override;
422  Expected<uint64_t> getStartAddress() const override;
423 
424  unsigned getPlatformFlags() const override { return EF.getHeader()->e_flags; }
425 
426  const ELFFile<ELFT> *getELFFile() const { return &EF; }
427 
428  bool isDyldType() const { return isDyldELFObject; }
429  static bool classof(const Binary *v) {
430  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
431  ELFT::Is64Bits);
432  }
433 
435 
436  bool isRelocatableObject() const override;
437 };
438 
443 
444 template <class ELFT>
446  ++Sym.d.b;
447 }
448 
449 template <class ELFT>
451  const Elf_Sym *ESym = getSymbol(Sym);
452  auto SymTabOrErr = EF.getSection(Sym.d.a);
453  if (!SymTabOrErr)
454  return SymTabOrErr.takeError();
455  const Elf_Shdr *SymTableSec = *SymTabOrErr;
456  auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
457  if (!StrTabOrErr)
458  return StrTabOrErr.takeError();
459  const Elf_Shdr *StringTableSec = *StrTabOrErr;
460  auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
461  if (!SymStrTabOrErr)
462  return SymStrTabOrErr.takeError();
463  Expected<StringRef> Name = ESym->getName(*SymStrTabOrErr);
464  if (Name && !Name->empty())
465  return Name;
466 
467  // If the symbol name is empty use the section name.
468  if (ESym->getType() == ELF::STT_SECTION) {
469  if (Expected<section_iterator> SecOrErr = getSymbolSection(Sym)) {
470  consumeError(Name.takeError());
471  return (*SecOrErr)->getName();
472  }
473  }
474  return Name;
475 }
476 
477 template <class ELFT>
479  return getSection(Sec)->sh_flags;
480 }
481 
482 template <class ELFT>
484  return getSection(Sec)->sh_type;
485 }
486 
487 template <class ELFT>
489  return getSection(Sec)->sh_offset;
490 }
491 
492 template <class ELFT>
494  const Elf_Sym *ESym = getSymbol(Symb);
495  uint64_t Ret = ESym->st_value;
496  if (ESym->st_shndx == ELF::SHN_ABS)
497  return Ret;
498 
499  const Elf_Ehdr *Header = EF.getHeader();
500  // Clear the ARM/Thumb or microMIPS indicator flag.
501  if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
502  ESym->getType() == ELF::STT_FUNC)
503  Ret &= ~1;
504 
505  return Ret;
506 }
507 
508 template <class ELFT>
511  uint64_t Result = getSymbolValue(Symb);
512  const Elf_Sym *ESym = getSymbol(Symb);
513  switch (ESym->st_shndx) {
514  case ELF::SHN_COMMON:
515  case ELF::SHN_UNDEF:
516  case ELF::SHN_ABS:
517  return Result;
518  }
519 
520  const Elf_Ehdr *Header = EF.getHeader();
521  auto SymTabOrErr = EF.getSection(Symb.d.a);
522  if (!SymTabOrErr)
523  return SymTabOrErr.takeError();
524  const Elf_Shdr *SymTab = *SymTabOrErr;
525 
526  if (Header->e_type == ELF::ET_REL) {
527  auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
528  if (!SectionOrErr)
529  return SectionOrErr.takeError();
530  const Elf_Shdr *Section = *SectionOrErr;
531  if (Section)
532  Result += Section->sh_addr;
533  }
534 
535  return Result;
536 }
537 
538 template <class ELFT>
540  const Elf_Sym *Sym = getSymbol(Symb);
541  if (Sym->st_shndx == ELF::SHN_COMMON)
542  return Sym->st_value;
543  return 0;
544 }
545 
546 template <class ELFT>
547 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
548  return EF.getHeader()->e_machine;
549 }
550 
551 template <class ELFT> uint16_t ELFObjectFile<ELFT>::getEType() const {
552  return EF.getHeader()->e_type;
553 }
554 
555 template <class ELFT>
557  return getSymbol(Sym)->st_size;
558 }
559 
560 template <class ELFT>
562  return getSymbol(Symb)->st_size;
563 }
564 
565 template <class ELFT>
567  return getSymbol(Symb)->getBinding();
568 }
569 
570 template <class ELFT>
572  return getSymbol(Symb)->st_other;
573 }
574 
575 template <class ELFT>
577  return getSymbol(Symb)->getType();
578 }
579 
580 template <class ELFT>
583  const Elf_Sym *ESym = getSymbol(Symb);
584 
585  switch (ESym->getType()) {
586  case ELF::STT_NOTYPE:
587  return SymbolRef::ST_Unknown;
588  case ELF::STT_SECTION:
589  return SymbolRef::ST_Debug;
590  case ELF::STT_FILE:
591  return SymbolRef::ST_File;
592  case ELF::STT_FUNC:
593  return SymbolRef::ST_Function;
594  case ELF::STT_OBJECT:
595  case ELF::STT_COMMON:
596  case ELF::STT_TLS:
597  return SymbolRef::ST_Data;
598  default:
599  return SymbolRef::ST_Other;
600  }
601 }
602 
603 template <class ELFT>
605  const Elf_Sym *ESym = getSymbol(Sym);
606 
607  uint32_t Result = SymbolRef::SF_None;
608 
609  if (ESym->getBinding() != ELF::STB_LOCAL)
610  Result |= SymbolRef::SF_Global;
611 
612  if (ESym->getBinding() == ELF::STB_WEAK)
613  Result |= SymbolRef::SF_Weak;
614 
615  if (ESym->st_shndx == ELF::SHN_ABS)
616  Result |= SymbolRef::SF_Absolute;
617 
618  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
620 
621  auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
622  if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
624  auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
625  if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
627 
628  if (EF.getHeader()->e_machine == ELF::EM_ARM) {
629  if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
630  StringRef Name = *NameOrErr;
631  if (Name.startswith("$d") || Name.startswith("$t") ||
632  Name.startswith("$a"))
634  } else {
635  // TODO: Actually report errors helpfully.
636  consumeError(NameOrErr.takeError());
637  }
638  if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
639  Result |= SymbolRef::SF_Thumb;
640  }
641 
642  if (ESym->st_shndx == ELF::SHN_UNDEF)
643  Result |= SymbolRef::SF_Undefined;
644 
645  if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
646  Result |= SymbolRef::SF_Common;
647 
648  if (isExportedToOtherDSO(ESym))
649  Result |= SymbolRef::SF_Exported;
650 
651  if (ESym->getVisibility() == ELF::STV_HIDDEN)
652  Result |= SymbolRef::SF_Hidden;
653 
654  return Result;
655 }
656 
657 template <class ELFT>
660  const Elf_Shdr *SymTab) const {
661  auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
662  if (!ESecOrErr)
663  return ESecOrErr.takeError();
664 
665  const Elf_Shdr *ESec = *ESecOrErr;
666  if (!ESec)
667  return section_end();
668 
669  DataRefImpl Sec;
670  Sec.p = reinterpret_cast<intptr_t>(ESec);
671  return section_iterator(SectionRef(Sec, this));
672 }
673 
674 template <class ELFT>
677  const Elf_Sym *Sym = getSymbol(Symb);
678  auto SymTabOrErr = EF.getSection(Symb.d.a);
679  if (!SymTabOrErr)
680  return SymTabOrErr.takeError();
681  const Elf_Shdr *SymTab = *SymTabOrErr;
682  return getSymbolSection(Sym, SymTab);
683 }
684 
685 template <class ELFT>
687  const Elf_Shdr *ESec = getSection(Sec);
688  Sec = toDRI(++ESec);
689 }
690 
691 template <class ELFT>
693  return EF.getSectionName(&*getSection(Sec));
694 }
695 
696 template <class ELFT>
698  return getSection(Sec)->sh_addr;
699 }
700 
701 template <class ELFT>
703  auto SectionsOrErr = EF.sections();
704  handleAllErrors(std::move(SectionsOrErr.takeError()),
705  [](const ErrorInfoBase &) {
706  llvm_unreachable("unable to get section index");
707  });
708  const Elf_Shdr *First = SectionsOrErr->begin();
709  return getSection(Sec) - First;
710 }
711 
712 template <class ELFT>
714  return getSection(Sec)->sh_size;
715 }
716 
717 template <class ELFT>
720  const Elf_Shdr *EShdr = getSection(Sec);
721  if (std::error_code EC =
723  (uintptr_t)base() + EShdr->sh_offset, EShdr->sh_size))
724  return errorCodeToError(EC);
725  return makeArrayRef((const uint8_t *)base() + EShdr->sh_offset,
726  EShdr->sh_size);
727 }
728 
729 template <class ELFT>
731  return getSection(Sec)->sh_addralign;
732 }
733 
734 template <class ELFT>
736  return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
737 }
738 
739 template <class ELFT>
741  return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
742 }
743 
744 template <class ELFT>
746  const Elf_Shdr *EShdr = getSection(Sec);
747  return EShdr->sh_type == ELF::SHT_PROGBITS &&
748  EShdr->sh_flags & ELF::SHF_ALLOC &&
749  !(EShdr->sh_flags & ELF::SHF_EXECINSTR);
750 }
751 
752 template <class ELFT>
754  const Elf_Shdr *EShdr = getSection(Sec);
755  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
756  EShdr->sh_type == ELF::SHT_NOBITS;
757 }
758 
759 template <class ELFT>
760 std::vector<SectionRef>
762  std::vector<SectionRef> Res;
763  std::vector<uintptr_t> Offsets;
764 
765  auto SectionsOrErr = EF.sections();
766  if (!SectionsOrErr)
767  return Res;
768 
769  for (const Elf_Shdr &Sec : *SectionsOrErr) {
770  if (Sec.sh_type != ELF::SHT_DYNAMIC)
771  continue;
772  Elf_Dyn *Dynamic =
773  reinterpret_cast<Elf_Dyn *>((uintptr_t)base() + Sec.sh_offset);
774  for (; Dynamic->d_tag != ELF::DT_NULL; Dynamic++) {
775  if (Dynamic->d_tag == ELF::DT_REL || Dynamic->d_tag == ELF::DT_RELA ||
776  Dynamic->d_tag == ELF::DT_JMPREL) {
777  Offsets.push_back(Dynamic->d_un.d_val);
778  }
779  }
780  }
781  for (const Elf_Shdr &Sec : *SectionsOrErr) {
782  if (is_contained(Offsets, Sec.sh_addr))
783  Res.emplace_back(toDRI(&Sec), this);
784  }
785  return Res;
786 }
787 
788 template <class ELFT>
790  return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
791 }
792 
793 template <class ELFT>
795  return getSection(Sec)->sh_flags & ELF::SHF_ALLOC &&
796  (getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR ||
797  !(getSection(Sec)->sh_flags & ELF::SHF_WRITE));
798 }
799 
800 template <class ELFT>
802  const Elf_Shdr *EShdr = getSection(Sec);
803  return !isBerkeleyText(Sec) && EShdr->sh_type != ELF::SHT_NOBITS &&
804  EShdr->sh_flags & ELF::SHF_ALLOC;
805 }
806 
807 template <class ELFT>
810  DataRefImpl RelData;
811  auto SectionsOrErr = EF.sections();
812  if (!SectionsOrErr)
814  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
815  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
816  RelData.d.b = 0;
817  return relocation_iterator(RelocationRef(RelData, this));
818 }
819 
820 template <class ELFT>
823  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
825  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
826  return Begin;
827  DataRefImpl RelData = Begin->getRawDataRefImpl();
828  const Elf_Shdr *RelSec = getRelSection(RelData);
829 
830  // Error check sh_link here so that getRelocationSymbol can just use it.
831  auto SymSecOrErr = EF.getSection(RelSec->sh_link);
832  if (!SymSecOrErr)
833  report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
834 
835  RelData.d.b += S->sh_size / S->sh_entsize;
836  return relocation_iterator(RelocationRef(RelData, this));
837 }
838 
839 template <class ELFT>
842  if (EF.getHeader()->e_type != ELF::ET_REL)
843  return section_end();
844 
845  const Elf_Shdr *EShdr = getSection(Sec);
846  uintX_t Type = EShdr->sh_type;
847  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
848  return section_end();
849 
850  auto R = EF.getSection(EShdr->sh_info);
851  if (!R)
852  report_fatal_error(errorToErrorCode(R.takeError()).message());
853  return section_iterator(SectionRef(toDRI(*R), this));
854 }
855 
856 // Relocations
857 template <class ELFT>
859  ++Rel.d.b;
860 }
861 
862 template <class ELFT>
865  uint32_t symbolIdx;
866  const Elf_Shdr *sec = getRelSection(Rel);
867  if (sec->sh_type == ELF::SHT_REL)
868  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
869  else
870  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
871  if (!symbolIdx)
872  return symbol_end();
873 
874  // FIXME: error check symbolIdx
875  DataRefImpl SymbolData;
876  SymbolData.d.a = sec->sh_link;
877  SymbolData.d.b = symbolIdx;
878  return symbol_iterator(SymbolRef(SymbolData, this));
879 }
880 
881 template <class ELFT>
883  const Elf_Shdr *sec = getRelSection(Rel);
884  if (sec->sh_type == ELF::SHT_REL)
885  return getRel(Rel)->r_offset;
886 
887  return getRela(Rel)->r_offset;
888 }
889 
890 template <class ELFT>
892  const Elf_Shdr *sec = getRelSection(Rel);
893  if (sec->sh_type == ELF::SHT_REL)
894  return getRel(Rel)->getType(EF.isMips64EL());
895  else
896  return getRela(Rel)->getType(EF.isMips64EL());
897 }
898 
899 template <class ELFT>
901  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
902 }
903 
904 template <class ELFT>
906  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
908  EF.getRelocationTypeName(type, Result);
909 }
910 
911 template <class ELFT>
914  if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
915  return createError("Section is not SHT_RELA");
916  return (int64_t)getRela(Rel)->r_addend;
917 }
918 
919 template <class ELFT>
920 const typename ELFObjectFile<ELFT>::Elf_Rel *
922  assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
923  auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
924  if (!Ret)
925  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
926  return *Ret;
927 }
928 
929 template <class ELFT>
930 const typename ELFObjectFile<ELFT>::Elf_Rela *
932  assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
933  auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
934  if (!Ret)
935  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
936  return *Ret;
937 }
938 
939 template <class ELFT>
942  auto EFOrErr = ELFFile<ELFT>::create(Object.getBuffer());
943  if (Error E = EFOrErr.takeError())
944  return std::move(E);
945  auto EF = std::move(*EFOrErr);
946 
947  auto SectionsOrErr = EF.sections();
948  if (!SectionsOrErr)
949  return SectionsOrErr.takeError();
950 
951  const Elf_Shdr *DotDynSymSec = nullptr;
952  const Elf_Shdr *DotSymtabSec = nullptr;
953  ArrayRef<Elf_Word> ShndxTable;
954  for (const Elf_Shdr &Sec : *SectionsOrErr) {
955  switch (Sec.sh_type) {
956  case ELF::SHT_DYNSYM: {
957  if (!DotDynSymSec)
958  DotDynSymSec = &Sec;
959  break;
960  }
961  case ELF::SHT_SYMTAB: {
962  if (!DotSymtabSec)
963  DotSymtabSec = &Sec;
964  break;
965  }
966  case ELF::SHT_SYMTAB_SHNDX: {
967  auto TableOrErr = EF.getSHNDXTable(Sec);
968  if (!TableOrErr)
969  return TableOrErr.takeError();
970  ShndxTable = *TableOrErr;
971  break;
972  }
973  }
974  }
975  return ELFObjectFile<ELFT>(Object, EF, DotDynSymSec, DotSymtabSec,
976  ShndxTable);
977 }
978 
979 template <class ELFT>
981  const Elf_Shdr *DotDynSymSec,
982  const Elf_Shdr *DotSymtabSec,
983  ArrayRef<Elf_Word> ShndxTable)
985  getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
986  Object),
987  EF(EF), DotDynSymSec(DotDynSymSec), DotSymtabSec(DotSymtabSec),
988  ShndxTable(ShndxTable) {}
989 
990 template <class ELFT>
991 ELFObjectFile<ELFT>::ELFObjectFile(ELFObjectFile<ELFT> &&Other)
992  : ELFObjectFile(Other.Data, Other.EF, Other.DotDynSymSec,
993  Other.DotSymtabSec, Other.ShndxTable) {}
994 
995 template <class ELFT>
997  DataRefImpl Sym =
998  toDRI(DotSymtabSec,
999  DotSymtabSec && DotSymtabSec->sh_size >= sizeof(Elf_Sym) ? 1 : 0);
1000  return basic_symbol_iterator(SymbolRef(Sym, this));
1001 }
1002 
1003 template <class ELFT>
1005  const Elf_Shdr *SymTab = DotSymtabSec;
1006  if (!SymTab)
1007  return symbol_begin();
1008  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1009  return basic_symbol_iterator(SymbolRef(Sym, this));
1010 }
1011 
1012 template <class ELFT>
1014  DataRefImpl Sym = toDRI(DotDynSymSec, 0);
1015  return symbol_iterator(SymbolRef(Sym, this));
1016 }
1017 
1018 template <class ELFT>
1020  const Elf_Shdr *SymTab = DotDynSymSec;
1021  if (!SymTab)
1022  return dynamic_symbol_begin();
1023  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
1024  return basic_symbol_iterator(SymbolRef(Sym, this));
1025 }
1026 
1027 template <class ELFT>
1029  auto SectionsOrErr = EF.sections();
1030  if (!SectionsOrErr)
1031  return section_iterator(SectionRef());
1032  return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
1033 }
1034 
1035 template <class ELFT>
1037  auto SectionsOrErr = EF.sections();
1038  if (!SectionsOrErr)
1039  return section_iterator(SectionRef());
1040  return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
1041 }
1042 
1043 template <class ELFT>
1045  return ELFT::Is64Bits ? 8 : 4;
1046 }
1047 
1048 template <class ELFT>
1050  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1051  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1052  case ELF::ELFCLASS32:
1053  switch (EF.getHeader()->e_machine) {
1054  case ELF::EM_386:
1055  return "ELF32-i386";
1056  case ELF::EM_IAMCU:
1057  return "ELF32-iamcu";
1058  case ELF::EM_X86_64:
1059  return "ELF32-x86-64";
1060  case ELF::EM_ARM:
1061  return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
1062  case ELF::EM_AVR:
1063  return "ELF32-avr";
1064  case ELF::EM_HEXAGON:
1065  return "ELF32-hexagon";
1066  case ELF::EM_LANAI:
1067  return "ELF32-lanai";
1068  case ELF::EM_MIPS:
1069  return "ELF32-mips";
1070  case ELF::EM_MSP430:
1071  return "ELF32-msp430";
1072  case ELF::EM_PPC:
1073  return "ELF32-ppc";
1074  case ELF::EM_RISCV:
1075  return "ELF32-riscv";
1076  case ELF::EM_SPARC:
1077  case ELF::EM_SPARC32PLUS:
1078  return "ELF32-sparc";
1079  case ELF::EM_AMDGPU:
1080  return "ELF32-amdgpu";
1081  default:
1082  return "ELF32-unknown";
1083  }
1084  case ELF::ELFCLASS64:
1085  switch (EF.getHeader()->e_machine) {
1086  case ELF::EM_386:
1087  return "ELF64-i386";
1088  case ELF::EM_X86_64:
1089  return "ELF64-x86-64";
1090  case ELF::EM_AARCH64:
1091  return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
1092  case ELF::EM_PPC64:
1093  return "ELF64-ppc64";
1094  case ELF::EM_RISCV:
1095  return "ELF64-riscv";
1096  case ELF::EM_S390:
1097  return "ELF64-s390";
1098  case ELF::EM_SPARCV9:
1099  return "ELF64-sparc";
1100  case ELF::EM_MIPS:
1101  return "ELF64-mips";
1102  case ELF::EM_AMDGPU:
1103  return "ELF64-amdgpu";
1104  case ELF::EM_BPF:
1105  return "ELF64-BPF";
1106  default:
1107  return "ELF64-unknown";
1108  }
1109  default:
1110  // FIXME: Proper error handling.
1111  report_fatal_error("Invalid ELFCLASS!");
1112  }
1113 }
1114 
1115 template <class ELFT> Triple::ArchType ELFObjectFile<ELFT>::getArch() const {
1116  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1117  switch (EF.getHeader()->e_machine) {
1118  case ELF::EM_386:
1119  case ELF::EM_IAMCU:
1120  return Triple::x86;
1121  case ELF::EM_X86_64:
1122  return Triple::x86_64;
1123  case ELF::EM_AARCH64:
1124  return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1125  case ELF::EM_ARM:
1126  return Triple::arm;
1127  case ELF::EM_AVR:
1128  return Triple::avr;
1129  case ELF::EM_HEXAGON:
1130  return Triple::hexagon;
1131  case ELF::EM_LANAI:
1132  return Triple::lanai;
1133  case ELF::EM_MIPS:
1134  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1135  case ELF::ELFCLASS32:
1136  return IsLittleEndian ? Triple::mipsel : Triple::mips;
1137  case ELF::ELFCLASS64:
1138  return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1139  default:
1140  report_fatal_error("Invalid ELFCLASS!");
1141  }
1142  case ELF::EM_MSP430:
1143  return Triple::msp430;
1144  case ELF::EM_PPC:
1145  return Triple::ppc;
1146  case ELF::EM_PPC64:
1147  return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1148  case ELF::EM_RISCV:
1149  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1150  case ELF::ELFCLASS32:
1151  return Triple::riscv32;
1152  case ELF::ELFCLASS64:
1153  return Triple::riscv64;
1154  default:
1155  report_fatal_error("Invalid ELFCLASS!");
1156  }
1157  case ELF::EM_S390:
1158  return Triple::systemz;
1159 
1160  case ELF::EM_SPARC:
1161  case ELF::EM_SPARC32PLUS:
1162  return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1163  case ELF::EM_SPARCV9:
1164  return Triple::sparcv9;
1165 
1166  case ELF::EM_AMDGPU: {
1167  if (!IsLittleEndian)
1168  return Triple::UnknownArch;
1169 
1170  unsigned MACH = EF.getHeader()->e_flags & ELF::EF_AMDGPU_MACH;
1171  if (MACH >= ELF::EF_AMDGPU_MACH_R600_FIRST &&
1173  return Triple::r600;
1174  if (MACH >= ELF::EF_AMDGPU_MACH_AMDGCN_FIRST &&
1176  return Triple::amdgcn;
1177 
1178  return Triple::UnknownArch;
1179  }
1180 
1181  case ELF::EM_BPF:
1182  return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1183 
1184  default:
1185  return Triple::UnknownArch;
1186  }
1187 }
1188 
1189 template <class ELFT>
1191  return EF.getHeader()->e_entry;
1192 }
1193 
1194 template <class ELFT>
1198 }
1199 
1200 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1201  return EF.getHeader()->e_type == ELF::ET_REL;
1202 }
1203 
1204 } // end namespace object
1205 } // end namespace llvm
1206 
1207 #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: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: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:163
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:1100
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
const uint8_t * base() const
Definition: ObjectFile.h:232
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:876
Error takeError()
Take ownership of the stored error.
Definition: Error.h:552
const ObjectFile * getObject() const
Definition: ObjectFile.h:555
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:504
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
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< StringRef > getSectionName(DataRefImpl Sec) const override
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:877
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 ObjectFile * getObject() const
Definition: ObjectFile.h:408
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
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
const ObjectFile * getObject() const
Definition: ObjectFile.h:517
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: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: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: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)
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: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