LLVM  5.0.0
ELFObjectFile.h
Go to the documentation of this file.
1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file declares the ELFObjectFile template class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef LLVM_OBJECT_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_H
16 
17 #include "llvm/ADT/ArrayRef.h"
18 #include "llvm/ADT/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"
36 #include "llvm/Support/ErrorOr.h"
38 #include <cassert>
39 #include <cstdint>
40 #include <system_error>
41 
42 namespace llvm {
43 namespace object {
44 
45 class elf_symbol_iterator;
46 
47 class ELFObjectFileBase : public ObjectFile {
48  friend class ELFRelocationRef;
49  friend class ELFSectionRef;
50  friend class ELFSymbolRef;
51 
52 protected:
54 
55  virtual uint16_t getEMachine() const = 0;
56  virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
57  virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
58  virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
59 
60  virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
61  virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
62  virtual uint64_t getSectionOffset(DataRefImpl Sec) const = 0;
63 
64  virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
65 
66 public:
68 
70 
72 
73  static bool classof(const Binary *v) { return v->isELF(); }
74 
75  SubtargetFeatures getFeatures() const override;
76 
78 
80 
81  void setARMSubArch(Triple &TheTriple) const override;
82 };
83 
84 class ELFSectionRef : public SectionRef {
85 public:
87  assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
88  }
89 
90  const ELFObjectFileBase *getObject() const {
91  return cast<ELFObjectFileBase>(SectionRef::getObject());
92  }
93 
94  uint32_t getType() const {
96  }
97 
98  uint64_t getFlags() const {
100  }
101 
102  uint64_t getOffset() const {
104  }
105 };
106 
108 public:
110  assert(isa<ELFObjectFileBase>(B->getObject()));
111  }
112 
113  const ELFSectionRef *operator->() const {
114  return static_cast<const ELFSectionRef *>(section_iterator::operator->());
115  }
116 
117  const ELFSectionRef &operator*() const {
118  return static_cast<const ELFSectionRef &>(section_iterator::operator*());
119  }
120 };
121 
122 class ELFSymbolRef : public SymbolRef {
123 public:
125  assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
126  }
127 
128  const ELFObjectFileBase *getObject() const {
129  return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
130  }
131 
132  uint64_t getSize() const {
134  }
135 
136  uint8_t getOther() const {
138  }
139 
140  uint8_t getELFType() const {
142  }
143 };
144 
146 public:
148  : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
149  cast<ELFObjectFileBase>(B->getObject()))) {}
150 
151  const ELFSymbolRef *operator->() const {
152  return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
153  }
154 
155  const ELFSymbolRef &operator*() const {
156  return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
157  }
158 };
159 
161 public:
163  assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
164  }
165 
166  const ELFObjectFileBase *getObject() const {
167  return cast<ELFObjectFileBase>(RelocationRef::getObject());
168  }
169 
172  }
173 };
174 
176 public:
179  B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
180 
181  const ELFRelocationRef *operator->() const {
182  return static_cast<const ELFRelocationRef *>(
184  }
185 
186  const ELFRelocationRef &operator*() const {
187  return static_cast<const ELFRelocationRef &>(
189  }
190 };
191 
195 }
196 
197 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
198  uint16_t getEMachine() const override;
199  uint64_t getSymbolSize(DataRefImpl Sym) const override;
200 
201 public:
203 
204  using uintX_t = typename ELFFile<ELFT>::uintX_t;
205 
206  using Elf_Sym = typename ELFFile<ELFT>::Elf_Sym;
207  using Elf_Shdr = typename ELFFile<ELFT>::Elf_Shdr;
208  using Elf_Ehdr = typename ELFFile<ELFT>::Elf_Ehdr;
209  using Elf_Rel = typename ELFFile<ELFT>::Elf_Rel;
210  using Elf_Rela = typename ELFFile<ELFT>::Elf_Rela;
211  using Elf_Dyn = typename ELFFile<ELFT>::Elf_Dyn;
212 
213 protected:
214  ELFFile<ELFT> EF;
215 
216  const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
217  const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
218  ArrayRef<Elf_Word> ShndxTable;
219 
220  void moveSymbolNext(DataRefImpl &Symb) const override;
222  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
223  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
225  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
226  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
227  uint8_t getSymbolOther(DataRefImpl Symb) const override;
228  uint8_t getSymbolELFType(DataRefImpl Symb) const override;
231  const Elf_Shdr *SymTab) const;
233 
234  void moveSectionNext(DataRefImpl &Sec) const override;
235  std::error_code getSectionName(DataRefImpl Sec,
236  StringRef &Res) const override;
237  uint64_t getSectionAddress(DataRefImpl Sec) const override;
238  uint64_t getSectionIndex(DataRefImpl Sec) const override;
239  uint64_t getSectionSize(DataRefImpl Sec) const override;
240  std::error_code getSectionContents(DataRefImpl Sec,
241  StringRef &Res) const override;
242  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
243  bool isSectionCompressed(DataRefImpl Sec) const override;
244  bool isSectionText(DataRefImpl Sec) const override;
245  bool isSectionData(DataRefImpl Sec) const override;
246  bool isSectionBSS(DataRefImpl Sec) const override;
247  bool isSectionVirtual(DataRefImpl Sec) const override;
249  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
251 
252  void moveRelocationNext(DataRefImpl &Rel) const override;
253  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
254  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
255  uint64_t getRelocationType(DataRefImpl Rel) const override;
257  SmallVectorImpl<char> &Result) const override;
258 
259  uint32_t getSectionType(DataRefImpl Sec) const override;
260  uint64_t getSectionFlags(DataRefImpl Sec) const override;
261  uint64_t getSectionOffset(DataRefImpl Sec) const override;
263 
264  /// \brief Get the relocation section that contains \a Rel.
265  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
266  auto RelSecOrErr = EF.getSection(Rel.d.a);
267  if (!RelSecOrErr)
268  report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
269  return *RelSecOrErr;
270  }
271 
272  DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
273  DataRefImpl DRI;
274  if (!SymTable) {
275  DRI.d.a = 0;
276  DRI.d.b = 0;
277  return DRI;
278  }
279  assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
280  SymTable->sh_type == ELF::SHT_DYNSYM);
281 
282  auto SectionsOrErr = EF.sections();
283  if (!SectionsOrErr) {
284  DRI.d.a = 0;
285  DRI.d.b = 0;
286  return DRI;
287  }
288  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
289  unsigned SymTableIndex =
290  (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
291 
292  DRI.d.a = SymTableIndex;
293  DRI.d.b = SymbolNum;
294  return DRI;
295  }
296 
297  const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
298  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
299  }
300 
301  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
302  DataRefImpl DRI;
303  DRI.p = reinterpret_cast<uintptr_t>(Sec);
304  return DRI;
305  }
306 
307  DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
308  DataRefImpl DRI;
309  DRI.p = reinterpret_cast<uintptr_t>(Dyn);
310  return DRI;
311  }
312 
313  bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
314  unsigned char Binding = ESym->getBinding();
315  unsigned char Visibility = ESym->getVisibility();
316 
317  // A symbol is exported if its binding is either GLOBAL or WEAK, and its
318  // visibility is either DEFAULT or PROTECTED. All other symbols are not
319  // exported.
320  return ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
321  (Visibility == ELF::STV_DEFAULT ||
322  Visibility == ELF::STV_PROTECTED));
323  }
324 
325  // This flag is used for classof, to distinguish ELFObjectFile from
326  // its subclass. If more subclasses will be created, this flag will
327  // have to become an enum.
329 
330 public:
331  ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
332 
333  const Elf_Rel *getRel(DataRefImpl Rel) const;
334  const Elf_Rela *getRela(DataRefImpl Rela) const;
335 
336  const Elf_Sym *getSymbol(DataRefImpl Sym) const {
337  auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
338  if (!Ret)
339  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
340  return *Ret;
341  }
342 
343  const Elf_Shdr *getSection(DataRefImpl Sec) const {
344  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
345  }
346 
347  basic_symbol_iterator symbol_begin() const override;
348  basic_symbol_iterator symbol_end() const override;
349 
352 
353  section_iterator section_begin() const override;
354  section_iterator section_end() const override;
355 
357 
358  uint8_t getBytesInAddress() const override;
359  StringRef getFileFormatName() const override;
360  unsigned getArch() const override;
361 
362  std::error_code getPlatformFlags(unsigned &Result) const override {
363  Result = EF.getHeader()->e_flags;
364  return std::error_code();
365  }
366 
367  std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override {
368  auto SectionsOrErr = EF.sections();
369  if (!SectionsOrErr)
370  return errorToErrorCode(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 errorToErrorCode(ErrorOrContents.takeError());
377 
378  auto Contents = ErrorOrContents.get();
379  if (Contents[0] != ARMBuildAttrs::Format_Version || Contents.size() == 1)
380  return std::error_code();
381 
382  Attributes.Parse(Contents, ELFT::TargetEndianness == support::little);
383  break;
384  }
385  }
386  return std::error_code();
387  }
388 
389  const ELFFile<ELFT> *getELFFile() const { return &EF; }
390 
391  bool isDyldType() const { return isDyldELFObject; }
392  static bool classof(const Binary *v) {
393  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
394  ELFT::Is64Bits);
395  }
396 
398 
399  bool isRelocatableObject() const override;
400 };
401 
406 
407 template <class ELFT>
409  ++Sym.d.b;
410 }
411 
412 template <class ELFT>
414  const Elf_Sym *ESym = getSymbol(Sym);
415  auto SymTabOrErr = EF.getSection(Sym.d.a);
416  if (!SymTabOrErr)
417  return SymTabOrErr.takeError();
418  const Elf_Shdr *SymTableSec = *SymTabOrErr;
419  auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
420  if (!StrTabOrErr)
421  return StrTabOrErr.takeError();
422  const Elf_Shdr *StringTableSec = *StrTabOrErr;
423  auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
424  if (!SymStrTabOrErr)
425  return SymStrTabOrErr.takeError();
426  return ESym->getName(*SymStrTabOrErr);
427 }
428 
429 template <class ELFT>
431  return getSection(Sec)->sh_flags;
432 }
433 
434 template <class ELFT>
436  return getSection(Sec)->sh_type;
437 }
438 
439 template <class ELFT>
441  return getSection(Sec)->sh_offset;
442 }
443 
444 template <class ELFT>
446  const Elf_Sym *ESym = getSymbol(Symb);
447  uint64_t Ret = ESym->st_value;
448  if (ESym->st_shndx == ELF::SHN_ABS)
449  return Ret;
450 
451  const Elf_Ehdr *Header = EF.getHeader();
452  // Clear the ARM/Thumb or microMIPS indicator flag.
453  if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
454  ESym->getType() == ELF::STT_FUNC)
455  Ret &= ~1;
456 
457  return Ret;
458 }
459 
460 template <class ELFT>
463  uint64_t Result = getSymbolValue(Symb);
464  const Elf_Sym *ESym = getSymbol(Symb);
465  switch (ESym->st_shndx) {
466  case ELF::SHN_COMMON:
467  case ELF::SHN_UNDEF:
468  case ELF::SHN_ABS:
469  return Result;
470  }
471 
472  const Elf_Ehdr *Header = EF.getHeader();
473  auto SymTabOrErr = EF.getSection(Symb.d.a);
474  if (!SymTabOrErr)
475  return SymTabOrErr.takeError();
476  const Elf_Shdr *SymTab = *SymTabOrErr;
477 
478  if (Header->e_type == ELF::ET_REL) {
479  auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
480  if (!SectionOrErr)
481  return SectionOrErr.takeError();
482  const Elf_Shdr *Section = *SectionOrErr;
483  if (Section)
484  Result += Section->sh_addr;
485  }
486 
487  return Result;
488 }
489 
490 template <class ELFT>
492  const Elf_Sym *Sym = getSymbol(Symb);
493  if (Sym->st_shndx == ELF::SHN_COMMON)
494  return Sym->st_value;
495  return 0;
496 }
497 
498 template <class ELFT>
499 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
500  return EF.getHeader()->e_machine;
501 }
502 
503 template <class ELFT>
504 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
505  return getSymbol(Sym)->st_size;
506 }
507 
508 template <class ELFT>
510  return getSymbol(Symb)->st_size;
511 }
512 
513 template <class ELFT>
515  return getSymbol(Symb)->st_other;
516 }
517 
518 template <class ELFT>
520  return getSymbol(Symb)->getType();
521 }
522 
523 template <class ELFT>
526  const Elf_Sym *ESym = getSymbol(Symb);
527 
528  switch (ESym->getType()) {
529  case ELF::STT_NOTYPE:
530  return SymbolRef::ST_Unknown;
531  case ELF::STT_SECTION:
532  return SymbolRef::ST_Debug;
533  case ELF::STT_FILE:
534  return SymbolRef::ST_File;
535  case ELF::STT_FUNC:
536  return SymbolRef::ST_Function;
537  case ELF::STT_OBJECT:
538  case ELF::STT_COMMON:
539  case ELF::STT_TLS:
540  return SymbolRef::ST_Data;
541  default:
542  return SymbolRef::ST_Other;
543  }
544 }
545 
546 template <class ELFT>
548  const Elf_Sym *ESym = getSymbol(Sym);
549 
550  uint32_t Result = SymbolRef::SF_None;
551 
552  if (ESym->getBinding() != ELF::STB_LOCAL)
553  Result |= SymbolRef::SF_Global;
554 
555  if (ESym->getBinding() == ELF::STB_WEAK)
556  Result |= SymbolRef::SF_Weak;
557 
558  if (ESym->st_shndx == ELF::SHN_ABS)
559  Result |= SymbolRef::SF_Absolute;
560 
561  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
563 
564  auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
565  if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
567  auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
568  if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
570 
571  if (EF.getHeader()->e_machine == ELF::EM_ARM) {
572  if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
573  StringRef Name = *NameOrErr;
574  if (Name.startswith("$d") || Name.startswith("$t") ||
575  Name.startswith("$a"))
577  } else {
578  // TODO: Actually report errors helpfully.
579  consumeError(NameOrErr.takeError());
580  }
581  if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
582  Result |= SymbolRef::SF_Thumb;
583  }
584 
585  if (ESym->st_shndx == ELF::SHN_UNDEF)
586  Result |= SymbolRef::SF_Undefined;
587 
588  if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
589  Result |= SymbolRef::SF_Common;
590 
591  if (isExportedToOtherDSO(ESym))
592  Result |= SymbolRef::SF_Exported;
593 
594  if (ESym->getVisibility() == ELF::STV_HIDDEN)
595  Result |= SymbolRef::SF_Hidden;
596 
597  return Result;
598 }
599 
600 template <class ELFT>
603  const Elf_Shdr *SymTab) const {
604  auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
605  if (!ESecOrErr)
606  return ESecOrErr.takeError();
607 
608  const Elf_Shdr *ESec = *ESecOrErr;
609  if (!ESec)
610  return section_end();
611 
612  DataRefImpl Sec;
613  Sec.p = reinterpret_cast<intptr_t>(ESec);
614  return section_iterator(SectionRef(Sec, this));
615 }
616 
617 template <class ELFT>
620  const Elf_Sym *Sym = getSymbol(Symb);
621  auto SymTabOrErr = EF.getSection(Symb.d.a);
622  if (!SymTabOrErr)
623  return SymTabOrErr.takeError();
624  const Elf_Shdr *SymTab = *SymTabOrErr;
625  return getSymbolSection(Sym, SymTab);
626 }
627 
628 template <class ELFT>
630  const Elf_Shdr *ESec = getSection(Sec);
631  Sec = toDRI(++ESec);
632 }
633 
634 template <class ELFT>
636  StringRef &Result) const {
637  auto Name = EF.getSectionName(&*getSection(Sec));
638  if (!Name)
639  return errorToErrorCode(Name.takeError());
640  Result = *Name;
641  return std::error_code();
642 }
643 
644 template <class ELFT>
646  return getSection(Sec)->sh_addr;
647 }
648 
649 template <class ELFT>
651  auto SectionsOrErr = EF.sections();
652  handleAllErrors(std::move(SectionsOrErr.takeError()),
653  [](const ErrorInfoBase &) {
654  llvm_unreachable("unable to get section index");
655  });
656  const Elf_Shdr *First = SectionsOrErr->begin();
657  return getSection(Sec) - First;
658 }
659 
660 template <class ELFT>
662  return getSection(Sec)->sh_size;
663 }
664 
665 template <class ELFT>
666 std::error_code
668  StringRef &Result) const {
669  const Elf_Shdr *EShdr = getSection(Sec);
670  Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
671  return std::error_code();
672 }
673 
674 template <class ELFT>
676  return getSection(Sec)->sh_addralign;
677 }
678 
679 template <class ELFT>
681  return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
682 }
683 
684 template <class ELFT>
686  return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
687 }
688 
689 template <class ELFT>
691  const Elf_Shdr *EShdr = getSection(Sec);
692  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
693  EShdr->sh_type == ELF::SHT_PROGBITS;
694 }
695 
696 template <class ELFT>
698  const Elf_Shdr *EShdr = getSection(Sec);
699  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
700  EShdr->sh_type == ELF::SHT_NOBITS;
701 }
702 
703 template <class ELFT>
705  return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
706 }
707 
708 template <class ELFT>
711  DataRefImpl RelData;
712  auto SectionsOrErr = EF.sections();
713  if (!SectionsOrErr)
715  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
716  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
717  RelData.d.b = 0;
718  return relocation_iterator(RelocationRef(RelData, this));
719 }
720 
721 template <class ELFT>
724  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
725  relocation_iterator Begin = section_rel_begin(Sec);
726  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
727  return Begin;
728  DataRefImpl RelData = Begin->getRawDataRefImpl();
729  const Elf_Shdr *RelSec = getRelSection(RelData);
730 
731  // Error check sh_link here so that getRelocationSymbol can just use it.
732  auto SymSecOrErr = EF.getSection(RelSec->sh_link);
733  if (!SymSecOrErr)
734  report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
735 
736  RelData.d.b += S->sh_size / S->sh_entsize;
737  return relocation_iterator(RelocationRef(RelData, this));
738 }
739 
740 template <class ELFT>
743  if (EF.getHeader()->e_type != ELF::ET_REL)
744  return section_end();
745 
746  const Elf_Shdr *EShdr = getSection(Sec);
747  uintX_t Type = EShdr->sh_type;
748  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
749  return section_end();
750 
751  auto R = EF.getSection(EShdr->sh_info);
752  if (!R)
753  report_fatal_error(errorToErrorCode(R.takeError()).message());
754  return section_iterator(SectionRef(toDRI(*R), this));
755 }
756 
757 // Relocations
758 template <class ELFT>
760  ++Rel.d.b;
761 }
762 
763 template <class ELFT>
766  uint32_t symbolIdx;
767  const Elf_Shdr *sec = getRelSection(Rel);
768  if (sec->sh_type == ELF::SHT_REL)
769  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
770  else
771  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
772  if (!symbolIdx)
773  return symbol_end();
774 
775  // FIXME: error check symbolIdx
776  DataRefImpl SymbolData;
777  SymbolData.d.a = sec->sh_link;
778  SymbolData.d.b = symbolIdx;
779  return symbol_iterator(SymbolRef(SymbolData, this));
780 }
781 
782 template <class ELFT>
784  assert(EF.getHeader()->e_type == ELF::ET_REL &&
785  "Only relocatable object files have relocation offsets");
786  const Elf_Shdr *sec = getRelSection(Rel);
787  if (sec->sh_type == ELF::SHT_REL)
788  return getRel(Rel)->r_offset;
789 
790  return getRela(Rel)->r_offset;
791 }
792 
793 template <class ELFT>
795  const Elf_Shdr *sec = getRelSection(Rel);
796  if (sec->sh_type == ELF::SHT_REL)
797  return getRel(Rel)->getType(EF.isMips64EL());
798  else
799  return getRela(Rel)->getType(EF.isMips64EL());
800 }
801 
802 template <class ELFT>
804  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
805 }
806 
807 template <class ELFT>
809  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
810  uint32_t type = getRelocationType(Rel);
811  EF.getRelocationTypeName(type, Result);
812 }
813 
814 template <class ELFT>
817  if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
819  return (int64_t)getRela(Rel)->r_addend;
820 }
821 
822 template <class ELFT>
823 const typename ELFObjectFile<ELFT>::Elf_Rel *
825  assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
826  auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
827  if (!Ret)
828  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
829  return *Ret;
830 }
831 
832 template <class ELFT>
833 const typename ELFObjectFile<ELFT>::Elf_Rela *
835  assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
836  auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
837  if (!Ret)
838  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
839  return *Ret;
840 }
841 
842 template <class ELFT>
845  getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
846  Object),
847  EF(Data.getBuffer()) {
848  auto SectionsOrErr = EF.sections();
849  if (!SectionsOrErr) {
850  EC = errorToErrorCode(SectionsOrErr.takeError());
851  return;
852  }
853  for (const Elf_Shdr &Sec : *SectionsOrErr) {
854  switch (Sec.sh_type) {
855  case ELF::SHT_DYNSYM: {
856  if (DotDynSymSec) {
857  // More than one .dynsym!
859  return;
860  }
861  DotDynSymSec = &Sec;
862  break;
863  }
864  case ELF::SHT_SYMTAB: {
865  if (DotSymtabSec) {
866  // More than one .dynsym!
868  return;
869  }
870  DotSymtabSec = &Sec;
871  break;
872  }
873  case ELF::SHT_SYMTAB_SHNDX: {
874  auto TableOrErr = EF.getSHNDXTable(Sec);
875  if (!TableOrErr) {
876  EC = errorToErrorCode(TableOrErr.takeError());
877  return;
878  }
879  ShndxTable = *TableOrErr;
880  break;
881  }
882  }
883  }
884 }
885 
886 template <class ELFT>
888  DataRefImpl Sym = toDRI(DotSymtabSec, 0);
889  return basic_symbol_iterator(SymbolRef(Sym, this));
890 }
891 
892 template <class ELFT>
894  const Elf_Shdr *SymTab = DotSymtabSec;
895  if (!SymTab)
896  return symbol_begin();
897  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
898  return basic_symbol_iterator(SymbolRef(Sym, this));
899 }
900 
901 template <class ELFT>
903  DataRefImpl Sym = toDRI(DotDynSymSec, 0);
904  return symbol_iterator(SymbolRef(Sym, this));
905 }
906 
907 template <class ELFT>
909  const Elf_Shdr *SymTab = DotDynSymSec;
910  if (!SymTab)
911  return dynamic_symbol_begin();
912  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
913  return basic_symbol_iterator(SymbolRef(Sym, this));
914 }
915 
916 template <class ELFT>
918  auto SectionsOrErr = EF.sections();
919  if (!SectionsOrErr)
920  return section_iterator(SectionRef());
921  return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
922 }
923 
924 template <class ELFT>
926  auto SectionsOrErr = EF.sections();
927  if (!SectionsOrErr)
928  return section_iterator(SectionRef());
929  return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
930 }
931 
932 template <class ELFT>
934  return ELFT::Is64Bits ? 8 : 4;
935 }
936 
937 template <class ELFT>
939  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
940  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
941  case ELF::ELFCLASS32:
942  switch (EF.getHeader()->e_machine) {
943  case ELF::EM_386:
944  return "ELF32-i386";
945  case ELF::EM_IAMCU:
946  return "ELF32-iamcu";
947  case ELF::EM_X86_64:
948  return "ELF32-x86-64";
949  case ELF::EM_ARM:
950  return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
951  case ELF::EM_AVR:
952  return "ELF32-avr";
953  case ELF::EM_HEXAGON:
954  return "ELF32-hexagon";
955  case ELF::EM_LANAI:
956  return "ELF32-lanai";
957  case ELF::EM_MIPS:
958  return "ELF32-mips";
959  case ELF::EM_PPC:
960  return "ELF32-ppc";
961  case ELF::EM_RISCV:
962  return "ELF32-riscv";
963  case ELF::EM_SPARC:
964  case ELF::EM_SPARC32PLUS:
965  return "ELF32-sparc";
966  case ELF::EM_WEBASSEMBLY:
967  return "ELF32-wasm";
968  case ELF::EM_AMDGPU:
969  return "ELF32-amdgpu";
970  default:
971  return "ELF32-unknown";
972  }
973  case ELF::ELFCLASS64:
974  switch (EF.getHeader()->e_machine) {
975  case ELF::EM_386:
976  return "ELF64-i386";
977  case ELF::EM_X86_64:
978  return "ELF64-x86-64";
979  case ELF::EM_AARCH64:
980  return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
981  case ELF::EM_PPC64:
982  return "ELF64-ppc64";
983  case ELF::EM_RISCV:
984  return "ELF64-riscv";
985  case ELF::EM_S390:
986  return "ELF64-s390";
987  case ELF::EM_SPARCV9:
988  return "ELF64-sparc";
989  case ELF::EM_MIPS:
990  return "ELF64-mips";
991  case ELF::EM_WEBASSEMBLY:
992  return "ELF64-wasm";
993  case ELF::EM_AMDGPU:
994  return (EF.getHeader()->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA
995  && IsLittleEndian) ?
996  "ELF64-amdgpu-hsacobj" : "ELF64-amdgpu";
997  case ELF::EM_BPF:
998  return "ELF64-BPF";
999  default:
1000  return "ELF64-unknown";
1001  }
1002  default:
1003  // FIXME: Proper error handling.
1004  report_fatal_error("Invalid ELFCLASS!");
1005  }
1006 }
1007 
1008 template <class ELFT>
1010  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
1011  switch (EF.getHeader()->e_machine) {
1012  case ELF::EM_386:
1013  case ELF::EM_IAMCU:
1014  return Triple::x86;
1015  case ELF::EM_X86_64:
1016  return Triple::x86_64;
1017  case ELF::EM_AARCH64:
1018  return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
1019  case ELF::EM_ARM:
1020  return Triple::arm;
1021  case ELF::EM_AVR:
1022  return Triple::avr;
1023  case ELF::EM_HEXAGON:
1024  return Triple::hexagon;
1025  case ELF::EM_LANAI:
1026  return Triple::lanai;
1027  case ELF::EM_MIPS:
1028  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1029  case ELF::ELFCLASS32:
1030  return IsLittleEndian ? Triple::mipsel : Triple::mips;
1031  case ELF::ELFCLASS64:
1032  return IsLittleEndian ? Triple::mips64el : Triple::mips64;
1033  default:
1034  report_fatal_error("Invalid ELFCLASS!");
1035  }
1036  case ELF::EM_PPC:
1037  return Triple::ppc;
1038  case ELF::EM_PPC64:
1039  return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
1040  case ELF::EM_RISCV:
1041  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1042  case ELF::ELFCLASS32:
1043  return Triple::riscv32;
1044  case ELF::ELFCLASS64:
1045  return Triple::riscv64;
1046  default:
1047  report_fatal_error("Invalid ELFCLASS!");
1048  }
1049  case ELF::EM_S390:
1050  return Triple::systemz;
1051 
1052  case ELF::EM_SPARC:
1053  case ELF::EM_SPARC32PLUS:
1054  return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1055  case ELF::EM_SPARCV9:
1056  return Triple::sparcv9;
1057  case ELF::EM_WEBASSEMBLY:
1058  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1059  case ELF::ELFCLASS32: return Triple::wasm32;
1060  case ELF::ELFCLASS64: return Triple::wasm64;
1061  default: return Triple::UnknownArch;
1062  }
1063 
1064  case ELF::EM_AMDGPU:
1065  return (EF.getHeader()->e_ident[ELF::EI_CLASS] == ELF::ELFCLASS64
1066  && EF.getHeader()->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA
1067  && IsLittleEndian) ?
1069 
1070  case ELF::EM_BPF:
1071  return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1072 
1073  default:
1074  return Triple::UnknownArch;
1075  }
1076 }
1077 
1078 template <class ELFT>
1081  return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1082 }
1083 
1084 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1085  return EF.getHeader()->e_type == ELF::ET_REL;
1086 }
1087 
1088 } // end namespace object
1089 } // end namespace llvm
1090 
1091 #endif // LLVM_OBJECT_ELFOBJECTFILE_H
bool isELF() const
Definition: Binary.h:108
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
uint64_t getSectionSize(DataRefImpl Sec) const override
Represents either an error or a value T.
Definition: ErrorOr.h:69
virtual uint16_t getEMachine() const =0
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
static bool classof(const Binary *v)
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:205
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:103
SubtargetFeatures getFeatures() const override
bool isSectionBSS(DataRefImpl Sec) const override
const ObjectFile * getObject() const
Definition: ObjectFile.h:496
bool isSectionText(DataRefImpl Sec) const override
elf_relocation_iterator(const relocation_iterator &B)
This provides a very simple, boring adaptor for a begin and end iterator into a range type...
static std::error_code getObject(const T *&Obj, MemoryBufferRef M, const void *Ptr, const uint64_t Size=sizeof(T))
void moveSymbolNext(DataRefImpl &Symb) const override
bool isRelocatableObject() const override
True if this is a relocatable object (.o/.obj).
Expected< StringRef > getSymbolName(DataRefImpl Symb) const override
This class is the base class for all object file types.
Definition: ObjectFile.h:189
typename ELFFile< ELFT >::Elf_Sym Elf_Sym
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:662
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:454
Base class for error info classes.
Definition: Error.h:47
bool isSectionVirtual(DataRefImpl Sec) const override
uint32_t getSectionType(DataRefImpl Sec) const override
SubtargetFeatures getARMFeatures() const
DataRefImpl toDRI(const Elf_Shdr *Sec) const
const ELFObjectFileBase * getObject() const
void moveRelocationNext(DataRefImpl &Rel) const override
basic_symbol_iterator symbol_begin() const override
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
uint8_t getOther() const
virtual basic_symbol_iterator symbol_begin() const =0
typename ELFFile< ELFT >::uintX_t uintX_t
void setARMSubArch(Triple &TheTriple) const override
uint8_t getSymbolOther(DataRefImpl Symb) const override
ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint8_t getELFType() const
const Elf_Shdr * DotSymtabSec
uint64_t getSectionAlignment(DataRefImpl Sec) const override
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: APFloat.h:42
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:52
ExternalFunctions * EF
elf_section_iterator(const section_iterator &B)
uint64_t getSize() const
const ELFRelocationRef * operator->() const
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: ELF.h:181
typename ELFFile< ELFT >::Elf_Rela Elf_Rela
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:48
SubtargetFeatures getMIPSFeatures() const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const Elf_Rela * getRela(DataRefImpl Rela) const
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
typename ELFFile< ELFT >::Elf_Ehdr Elf_Ehdr
section_iterator section_begin() const override
std::error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const override
void moveSectionNext(DataRefImpl &Sec) const override
elf_symbol_iterator_range getDynamicSymbolIterators() const override
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: APInt.h:33
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Rel * getRel(DataRefImpl Rel) const
const Elf_Sym * getSymbol(DataRefImpl Sym) const
uint32_t getType() const
Definition: ELFObjectFile.h:94
const ELFFile< ELFT > * getELFFile() const
std::error_code getBuildAttributes(ARMAttributeParser &Attributes) const override
typename ELFFile< ELFT >::Elf_Rel Elf_Rel
ArrayRef< Elf_Word > ShndxTable
typename ELFFile< ELFT >::Elf_Dyn Elf_Dyn
const SymbolRef * operator->() const
Definition: ObjectFile.h:175
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: ELF.h:243
const char * Name
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
Definition: Casting.h:240
relocation_iterator section_rel_end(DataRefImpl Sec) const override
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
ELFRelocationRef(const RelocationRef &B)
unsigned int getType() const
Definition: Binary.h:91
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:73
elf_symbol_iterator_range symbols() const
const ELFSymbolRef & operator*() const
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
uint32_t getSymbolFlags(DataRefImpl Symb) const override
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:67
const Elf_Shdr * DotDynSymSec
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:67
section_iterator getRelocatedSection(DataRefImpl Sec) const override
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
void handleAllErrors(Error E, HandlerTs &&...Handlers)
Behaves the same as handleErrors, except that it requires that all errors be handled by the given han...
Definition: Error.h:555
virtual basic_symbol_iterator symbol_end() const =0
virtual ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
const ELFSectionRef * operator->() const
uint8_t getSymbolELFType(DataRefImpl Symb) const override
uint64_t getSectionIndex(DataRefImpl Sec) const override
struct llvm::object::DataRefImpl::@230 d
StringRef getFileFormatName() const override
void consumeError(Error Err)
Consume a Error without doing anything.
Definition: Error.h:593
const ELFSectionRef & operator*() const
std::error_code getPlatformFlags(unsigned &Result) const override
Returns platform-specific object flags, if any.
const ObjectFile * getObject() const
Definition: ObjectFile.h:458
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
uint64_t getSectionAddress(DataRefImpl Sec) const override
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
const Elf_Shdr * getSection(DataRefImpl Sec) const
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
unsigned getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
#define B
Definition: LargeTest.cpp:24
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
const ObjectFile * getObject() const
Definition: ObjectFile.h:367
ErrorOr< int64_t > getAddend() const
uint64_t getSectionFlags(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
const ELFRelocationRef & operator*() const
const ELFSymbolRef * operator->() const
ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:139
A range adaptor for a pair of iterators.
Manages the enabling and disabling of subtarget specific features.
virtual uint64_t getSymbolSize(DataRefImpl Symb) const =0
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: ObjectFile.h:128
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
llvm_unreachable("Unknown RegBank!")
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
basic_symbol_iterator symbol_end() const override
bool isSectionCompressed(DataRefImpl Sec) const override
ELFSectionRef(const SectionRef &B)
Definition: ELFObjectFile.h:86
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:209
uint64_t getSectionOffset(DataRefImpl Sec) const override
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
typename ELFFile< ELFT >::Elf_Shdr Elf_Shdr
const SymbolRef & operator*() const
Definition: ObjectFile.h:180
section_iterator section_end() const override
Provides ErrorOr<T> smart pointer.
const content_type & operator*() const
Definition: SymbolicFile.h:79
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
aarch64 promote const
const content_type * operator->() const
Definition: SymbolicFile.h:77
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
const ELFObjectFileBase * getObject() const
void Parse(ArrayRef< uint8_t > Section, bool isLittle)
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:77
uint64_t getRelocationType(DataRefImpl Rel) const override
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:133
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
const ELFObjectFileBase * getObject() const
Definition: ELFObjectFile.h:90
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
uint64_t getOffset() const
elf_symbol_iterator(const basic_symbol_iterator &B)
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
Definition: Error.cpp:84
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:81
uint64_t getFlags() const
Definition: ELFObjectFile.h:98
ELFSymbolRef(const SymbolRef &B)
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:492