LLVM  4.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"
23 #include "llvm/Object/Binary.h"
24 #include "llvm/Object/ELF.h"
25 #include "llvm/Object/ELFTypes.h"
26 #include "llvm/Object/Error.h"
27 #include "llvm/Object/ObjectFile.h"
29 #include "llvm/Support/Casting.h"
30 #include "llvm/Support/ELF.h"
31 #include "llvm/Support/Endian.h"
32 #include "llvm/Support/Error.h"
34 #include "llvm/Support/ErrorOr.h"
36 #include <cassert>
37 #include <cstdint>
38 #include <system_error>
39 
40 namespace llvm {
41 namespace object {
42 
43 class elf_symbol_iterator;
44 class ELFSymbolRef;
45 class ELFRelocationRef;
46 
47 class ELFObjectFileBase : public ObjectFile {
48  friend class ELFSymbolRef;
49  friend class ELFSectionRef;
50  friend class ELFRelocationRef;
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:
69 
71 
72  static inline bool classof(const Binary *v) { return v->isELF(); }
73 
74  SubtargetFeatures getFeatures() const override;
75 };
76 
77 class ELFSectionRef : public SectionRef {
78 public:
80  assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
81  }
82 
83  const ELFObjectFileBase *getObject() const {
84  return cast<ELFObjectFileBase>(SectionRef::getObject());
85  }
86 
87  uint32_t getType() const {
89  }
90 
91  uint64_t getFlags() const {
93  }
94 
95  uint64_t getOffset() const {
97  }
98 };
99 
101 public:
103  assert(isa<ELFObjectFileBase>(B->getObject()));
104  }
105 
106  const ELFSectionRef *operator->() const {
107  return static_cast<const ELFSectionRef *>(section_iterator::operator->());
108  }
109 
110  const ELFSectionRef &operator*() const {
111  return static_cast<const ELFSectionRef &>(section_iterator::operator*());
112  }
113 };
114 
115 class ELFSymbolRef : public SymbolRef {
116 public:
118  assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
119  }
120 
121  const ELFObjectFileBase *getObject() const {
122  return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
123  }
124 
125  uint64_t getSize() const {
127  }
128 
129  uint8_t getOther() const {
131  }
132 
133  uint8_t getELFType() const {
135  }
136 };
137 
139 public:
141  : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
142  cast<ELFObjectFileBase>(B->getObject()))) {}
143 
144  const ELFSymbolRef *operator->() const {
145  return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
146  }
147 
148  const ELFSymbolRef &operator*() const {
149  return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
150  }
151 };
152 
154 public:
156  assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
157  }
158 
159  const ELFObjectFileBase *getObject() const {
160  return cast<ELFObjectFileBase>(RelocationRef::getObject());
161  }
162 
165  }
166 };
167 
169 public:
172  B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
173 
174  const ELFRelocationRef *operator->() const {
175  return static_cast<const ELFRelocationRef *>(
177  }
178 
179  const ELFRelocationRef &operator*() const {
180  return static_cast<const ELFRelocationRef &>(
182  }
183 };
184 
188 }
189 
190 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
191  uint16_t getEMachine() const override;
192  uint64_t getSymbolSize(DataRefImpl Sym) const override;
193 
194 public:
196 
197  typedef typename ELFFile<ELFT>::uintX_t uintX_t;
198 
199  typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
200  typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
201  typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
202  typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
203  typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
204  typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
205 
206 protected:
207  ELFFile<ELFT> EF;
208 
209  const Elf_Shdr *DotDynSymSec = nullptr; // Dynamic symbol table section.
210  const Elf_Shdr *DotSymtabSec = nullptr; // Symbol table section.
211  ArrayRef<Elf_Word> ShndxTable;
212 
213  void moveSymbolNext(DataRefImpl &Symb) const override;
215  Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
216  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
218  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
219  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
220  uint8_t getSymbolOther(DataRefImpl Symb) const override;
221  uint8_t getSymbolELFType(DataRefImpl Symb) const override;
224  const Elf_Shdr *SymTab) const;
226 
227  void moveSectionNext(DataRefImpl &Sec) const override;
228  std::error_code getSectionName(DataRefImpl Sec,
229  StringRef &Res) const override;
230  uint64_t getSectionAddress(DataRefImpl Sec) const override;
231  uint64_t getSectionSize(DataRefImpl Sec) const override;
232  std::error_code getSectionContents(DataRefImpl Sec,
233  StringRef &Res) const override;
234  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
235  bool isSectionCompressed(DataRefImpl Sec) const override;
236  bool isSectionText(DataRefImpl Sec) const override;
237  bool isSectionData(DataRefImpl Sec) const override;
238  bool isSectionBSS(DataRefImpl Sec) const override;
239  bool isSectionVirtual(DataRefImpl Sec) const override;
241  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
243 
244  void moveRelocationNext(DataRefImpl &Rel) const override;
245  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
246  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
247  uint64_t getRelocationType(DataRefImpl Rel) const override;
249  SmallVectorImpl<char> &Result) const override;
250 
251  uint32_t getSectionType(DataRefImpl Sec) const override;
252  uint64_t getSectionFlags(DataRefImpl Sec) const override;
253  uint64_t getSectionOffset(DataRefImpl Sec) const override;
255 
256  /// \brief Get the relocation section that contains \a Rel.
257  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
258  auto RelSecOrErr = EF.getSection(Rel.d.a);
259  if (!RelSecOrErr)
260  report_fatal_error(errorToErrorCode(RelSecOrErr.takeError()).message());
261  return *RelSecOrErr;
262  }
263 
264  DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
265  DataRefImpl DRI;
266  if (!SymTable) {
267  DRI.d.a = 0;
268  DRI.d.b = 0;
269  return DRI;
270  }
271  assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
272  SymTable->sh_type == ELF::SHT_DYNSYM);
273 
274  auto SectionsOrErr = EF.sections();
275  if (!SectionsOrErr) {
276  DRI.d.a = 0;
277  DRI.d.b = 0;
278  return DRI;
279  }
280  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
281  unsigned SymTableIndex =
282  (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
283 
284  DRI.d.a = SymTableIndex;
285  DRI.d.b = SymbolNum;
286  return DRI;
287  }
288 
289  const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
290  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
291  }
292 
293  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
294  DataRefImpl DRI;
295  DRI.p = reinterpret_cast<uintptr_t>(Sec);
296  return DRI;
297  }
298 
299  DataRefImpl toDRI(const Elf_Dyn *Dyn) const {
300  DataRefImpl DRI;
301  DRI.p = reinterpret_cast<uintptr_t>(Dyn);
302  return DRI;
303  }
304 
305  bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
306  unsigned char Binding = ESym->getBinding();
307  unsigned char Visibility = ESym->getVisibility();
308 
309  // A symbol is exported if its binding is either GLOBAL or WEAK, and its
310  // visibility is either DEFAULT or PROTECTED. All other symbols are not
311  // exported.
312  return ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
313  (Visibility == ELF::STV_DEFAULT ||
314  Visibility == ELF::STV_PROTECTED));
315  }
316 
317  // This flag is used for classof, to distinguish ELFObjectFile from
318  // its subclass. If more subclasses will be created, this flag will
319  // have to become an enum.
321 
322 public:
323  ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
324 
325  const Elf_Rel *getRel(DataRefImpl Rel) const;
326  const Elf_Rela *getRela(DataRefImpl Rela) const;
327 
328  const Elf_Sym *getSymbol(DataRefImpl Sym) const {
329  auto Ret = EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
330  if (!Ret)
331  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
332  return *Ret;
333  }
334 
335  const Elf_Shdr *getSection(DataRefImpl Sec) const {
336  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
337  }
338 
339  basic_symbol_iterator symbol_begin() const override;
340  basic_symbol_iterator symbol_end() const override;
341 
344 
345  section_iterator section_begin() const override;
346  section_iterator section_end() const override;
347 
349 
350  uint8_t getBytesInAddress() const override;
351  StringRef getFileFormatName() const override;
352  unsigned getArch() const override;
353 
354  std::error_code getPlatformFlags(unsigned &Result) const override {
355  Result = EF.getHeader()->e_flags;
356  return std::error_code();
357  }
358 
359  const ELFFile<ELFT> *getELFFile() const { return &EF; }
360 
361  bool isDyldType() const { return isDyldELFObject; }
362  static inline bool classof(const Binary *v) {
363  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
364  ELFT::Is64Bits);
365  }
366 
368 
369  bool isRelocatableObject() const override;
370 };
371 
376 
377 template <class ELFT>
379  ++Sym.d.b;
380 }
381 
382 template <class ELFT>
384  const Elf_Sym *ESym = getSymbol(Sym);
385  auto SymTabOrErr = EF.getSection(Sym.d.a);
386  if (!SymTabOrErr)
387  return SymTabOrErr.takeError();
388  const Elf_Shdr *SymTableSec = *SymTabOrErr;
389  auto StrTabOrErr = EF.getSection(SymTableSec->sh_link);
390  if (!StrTabOrErr)
391  return StrTabOrErr.takeError();
392  const Elf_Shdr *StringTableSec = *StrTabOrErr;
393  auto SymStrTabOrErr = EF.getStringTable(StringTableSec);
394  if (!SymStrTabOrErr)
395  return SymStrTabOrErr.takeError();
396  return ESym->getName(*SymStrTabOrErr);
397 }
398 
399 template <class ELFT>
401  return getSection(Sec)->sh_flags;
402 }
403 
404 template <class ELFT>
406  return getSection(Sec)->sh_type;
407 }
408 
409 template <class ELFT>
411  return getSection(Sec)->sh_offset;
412 }
413 
414 template <class ELFT>
416  const Elf_Sym *ESym = getSymbol(Symb);
417  uint64_t Ret = ESym->st_value;
418  if (ESym->st_shndx == ELF::SHN_ABS)
419  return Ret;
420 
421  const Elf_Ehdr *Header = EF.getHeader();
422  // Clear the ARM/Thumb or microMIPS indicator flag.
423  if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
424  ESym->getType() == ELF::STT_FUNC)
425  Ret &= ~1;
426 
427  return Ret;
428 }
429 
430 template <class ELFT>
433  uint64_t Result = getSymbolValue(Symb);
434  const Elf_Sym *ESym = getSymbol(Symb);
435  switch (ESym->st_shndx) {
436  case ELF::SHN_COMMON:
437  case ELF::SHN_UNDEF:
438  case ELF::SHN_ABS:
439  return Result;
440  }
441 
442  const Elf_Ehdr *Header = EF.getHeader();
443  auto SymTabOrErr = EF.getSection(Symb.d.a);
444  if (!SymTabOrErr)
445  return SymTabOrErr.takeError();
446  const Elf_Shdr *SymTab = *SymTabOrErr;
447 
448  if (Header->e_type == ELF::ET_REL) {
449  auto SectionOrErr = EF.getSection(ESym, SymTab, ShndxTable);
450  if (!SectionOrErr)
451  return SectionOrErr.takeError();
452  const Elf_Shdr *Section = *SectionOrErr;
453  if (Section)
454  Result += Section->sh_addr;
455  }
456 
457  return Result;
458 }
459 
460 template <class ELFT>
462  const Elf_Sym *Sym = getSymbol(Symb);
463  if (Sym->st_shndx == ELF::SHN_COMMON)
464  return Sym->st_value;
465  return 0;
466 }
467 
468 template <class ELFT>
469 uint16_t ELFObjectFile<ELFT>::getEMachine() const {
470  return EF.getHeader()->e_machine;
471 }
472 
473 template <class ELFT>
475  return getSymbol(Sym)->st_size;
476 }
477 
478 template <class ELFT>
480  return getSymbol(Symb)->st_size;
481 }
482 
483 template <class ELFT>
485  return getSymbol(Symb)->st_other;
486 }
487 
488 template <class ELFT>
490  return getSymbol(Symb)->getType();
491 }
492 
493 template <class ELFT>
496  const Elf_Sym *ESym = getSymbol(Symb);
497 
498  switch (ESym->getType()) {
499  case ELF::STT_NOTYPE:
500  return SymbolRef::ST_Unknown;
501  case ELF::STT_SECTION:
502  return SymbolRef::ST_Debug;
503  case ELF::STT_FILE:
504  return SymbolRef::ST_File;
505  case ELF::STT_FUNC:
506  return SymbolRef::ST_Function;
507  case ELF::STT_OBJECT:
508  case ELF::STT_COMMON:
509  case ELF::STT_TLS:
510  return SymbolRef::ST_Data;
511  default:
512  return SymbolRef::ST_Other;
513  }
514 }
515 
516 template <class ELFT>
518  const Elf_Sym *ESym = getSymbol(Sym);
519 
520  uint32_t Result = SymbolRef::SF_None;
521 
522  if (ESym->getBinding() != ELF::STB_LOCAL)
523  Result |= SymbolRef::SF_Global;
524 
525  if (ESym->getBinding() == ELF::STB_WEAK)
526  Result |= SymbolRef::SF_Weak;
527 
528  if (ESym->st_shndx == ELF::SHN_ABS)
529  Result |= SymbolRef::SF_Absolute;
530 
531  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION)
533 
534  auto DotSymtabSecSyms = EF.symbols(DotSymtabSec);
535  if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin())
537  auto DotDynSymSecSyms = EF.symbols(DotDynSymSec);
538  if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin())
540 
541  if (EF.getHeader()->e_machine == ELF::EM_ARM) {
542  if (Expected<StringRef> NameOrErr = getSymbolName(Sym)) {
543  StringRef Name = *NameOrErr;
544  if (Name.startswith("$d") || Name.startswith("$t") ||
545  Name.startswith("$a"))
547  } else {
548  // TODO: Actually report errors helpfully.
549  consumeError(NameOrErr.takeError());
550  }
551  if (ESym->getType() == ELF::STT_FUNC && (ESym->st_value & 1) == 1)
552  Result |= SymbolRef::SF_Thumb;
553  }
554 
555  if (ESym->st_shndx == ELF::SHN_UNDEF)
556  Result |= SymbolRef::SF_Undefined;
557 
558  if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
559  Result |= SymbolRef::SF_Common;
560 
561  if (isExportedToOtherDSO(ESym))
562  Result |= SymbolRef::SF_Exported;
563 
564  if (ESym->getVisibility() == ELF::STV_HIDDEN)
565  Result |= SymbolRef::SF_Hidden;
566 
567  return Result;
568 }
569 
570 template <class ELFT>
573  const Elf_Shdr *SymTab) const {
574  auto ESecOrErr = EF.getSection(ESym, SymTab, ShndxTable);
575  if (!ESecOrErr)
576  return ESecOrErr.takeError();
577 
578  const Elf_Shdr *ESec = *ESecOrErr;
579  if (!ESec)
580  return section_end();
581 
582  DataRefImpl Sec;
583  Sec.p = reinterpret_cast<intptr_t>(ESec);
584  return section_iterator(SectionRef(Sec, this));
585 }
586 
587 template <class ELFT>
590  const Elf_Sym *Sym = getSymbol(Symb);
591  auto SymTabOrErr = EF.getSection(Symb.d.a);
592  if (!SymTabOrErr)
593  return SymTabOrErr.takeError();
594  const Elf_Shdr *SymTab = *SymTabOrErr;
595  return getSymbolSection(Sym, SymTab);
596 }
597 
598 template <class ELFT>
600  const Elf_Shdr *ESec = getSection(Sec);
601  Sec = toDRI(++ESec);
602 }
603 
604 template <class ELFT>
606  StringRef &Result) const {
607  auto Name = EF.getSectionName(&*getSection(Sec));
608  if (!Name)
609  return errorToErrorCode(Name.takeError());
610  Result = *Name;
611  return std::error_code();
612 }
613 
614 template <class ELFT>
616  return getSection(Sec)->sh_addr;
617 }
618 
619 template <class ELFT>
621  return getSection(Sec)->sh_size;
622 }
623 
624 template <class ELFT>
625 std::error_code
627  StringRef &Result) const {
628  const Elf_Shdr *EShdr = getSection(Sec);
629  Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
630  return std::error_code();
631 }
632 
633 template <class ELFT>
635  return getSection(Sec)->sh_addralign;
636 }
637 
638 template <class ELFT>
640  return getSection(Sec)->sh_flags & ELF::SHF_COMPRESSED;
641 }
642 
643 template <class ELFT>
645  return getSection(Sec)->sh_flags & ELF::SHF_EXECINSTR;
646 }
647 
648 template <class ELFT>
650  const Elf_Shdr *EShdr = getSection(Sec);
651  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
652  EShdr->sh_type == ELF::SHT_PROGBITS;
653 }
654 
655 template <class ELFT>
657  const Elf_Shdr *EShdr = getSection(Sec);
658  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
659  EShdr->sh_type == ELF::SHT_NOBITS;
660 }
661 
662 template <class ELFT>
664  return getSection(Sec)->sh_type == ELF::SHT_NOBITS;
665 }
666 
667 template <class ELFT>
670  DataRefImpl RelData;
671  auto SectionsOrErr = EF.sections();
672  if (!SectionsOrErr)
674  uintptr_t SHT = reinterpret_cast<uintptr_t>((*SectionsOrErr).begin());
675  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
676  RelData.d.b = 0;
677  return relocation_iterator(RelocationRef(RelData, this));
678 }
679 
680 template <class ELFT>
683  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
684  relocation_iterator Begin = section_rel_begin(Sec);
685  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
686  return Begin;
687  DataRefImpl RelData = Begin->getRawDataRefImpl();
688  const Elf_Shdr *RelSec = getRelSection(RelData);
689 
690  // Error check sh_link here so that getRelocationSymbol can just use it.
691  auto SymSecOrErr = EF.getSection(RelSec->sh_link);
692  if (!SymSecOrErr)
693  report_fatal_error(errorToErrorCode(SymSecOrErr.takeError()).message());
694 
695  RelData.d.b += S->sh_size / S->sh_entsize;
696  return relocation_iterator(RelocationRef(RelData, this));
697 }
698 
699 template <class ELFT>
702  if (EF.getHeader()->e_type != ELF::ET_REL)
703  return section_end();
704 
705  const Elf_Shdr *EShdr = getSection(Sec);
706  uintX_t Type = EShdr->sh_type;
707  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
708  return section_end();
709 
710  auto R = EF.getSection(EShdr->sh_info);
711  if (!R)
712  report_fatal_error(errorToErrorCode(R.takeError()).message());
713  return section_iterator(SectionRef(toDRI(*R), this));
714 }
715 
716 // Relocations
717 template <class ELFT>
719  ++Rel.d.b;
720 }
721 
722 template <class ELFT>
725  uint32_t symbolIdx;
726  const Elf_Shdr *sec = getRelSection(Rel);
727  if (sec->sh_type == ELF::SHT_REL)
728  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
729  else
730  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
731  if (!symbolIdx)
732  return symbol_end();
733 
734  // FIXME: error check symbolIdx
735  DataRefImpl SymbolData;
736  SymbolData.d.a = sec->sh_link;
737  SymbolData.d.b = symbolIdx;
738  return symbol_iterator(SymbolRef(SymbolData, this));
739 }
740 
741 template <class ELFT>
743  assert(EF.getHeader()->e_type == ELF::ET_REL &&
744  "Only relocatable object files have relocation offsets");
745  const Elf_Shdr *sec = getRelSection(Rel);
746  if (sec->sh_type == ELF::SHT_REL)
747  return getRel(Rel)->r_offset;
748 
749  return getRela(Rel)->r_offset;
750 }
751 
752 template <class ELFT>
754  const Elf_Shdr *sec = getRelSection(Rel);
755  if (sec->sh_type == ELF::SHT_REL)
756  return getRel(Rel)->getType(EF.isMips64EL());
757  else
758  return getRela(Rel)->getType(EF.isMips64EL());
759 }
760 
761 template <class ELFT>
763  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
764 }
765 
766 template <class ELFT>
768  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
769  uint32_t type = getRelocationType(Rel);
770  EF.getRelocationTypeName(type, Result);
771 }
772 
773 template <class ELFT>
776  if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
778  return (int64_t)getRela(Rel)->r_addend;
779 }
780 
781 template <class ELFT>
782 const typename ELFObjectFile<ELFT>::Elf_Rel *
784  assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
785  auto Ret = EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
786  if (!Ret)
787  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
788  return *Ret;
789 }
790 
791 template <class ELFT>
792 const typename ELFObjectFile<ELFT>::Elf_Rela *
794  assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
795  auto Ret = EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
796  if (!Ret)
797  report_fatal_error(errorToErrorCode(Ret.takeError()).message());
798  return *Ret;
799 }
800 
801 template <class ELFT>
804  getELFType(ELFT::TargetEndianness == support::little, ELFT::Is64Bits),
805  Object),
806  EF(Data.getBuffer()) {
807  auto SectionsOrErr = EF.sections();
808  if (!SectionsOrErr) {
809  EC = errorToErrorCode(SectionsOrErr.takeError());
810  return;
811  }
812  for (const Elf_Shdr &Sec : *SectionsOrErr) {
813  switch (Sec.sh_type) {
814  case ELF::SHT_DYNSYM: {
815  if (DotDynSymSec) {
816  // More than one .dynsym!
818  return;
819  }
820  DotDynSymSec = &Sec;
821  break;
822  }
823  case ELF::SHT_SYMTAB: {
824  if (DotSymtabSec) {
825  // More than one .dynsym!
827  return;
828  }
829  DotSymtabSec = &Sec;
830  break;
831  }
832  case ELF::SHT_SYMTAB_SHNDX: {
833  auto TableOrErr = EF.getSHNDXTable(Sec);
834  if (!TableOrErr) {
835  EC = errorToErrorCode(TableOrErr.takeError());
836  return;
837  }
838  ShndxTable = *TableOrErr;
839  break;
840  }
841  }
842  }
843 }
844 
845 template <class ELFT>
847  DataRefImpl Sym = toDRI(DotSymtabSec, 0);
848  return basic_symbol_iterator(SymbolRef(Sym, this));
849 }
850 
851 template <class ELFT>
853  const Elf_Shdr *SymTab = DotSymtabSec;
854  if (!SymTab)
855  return symbol_begin();
856  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
857  return basic_symbol_iterator(SymbolRef(Sym, this));
858 }
859 
860 template <class ELFT>
862  DataRefImpl Sym = toDRI(DotDynSymSec, 0);
863  return symbol_iterator(SymbolRef(Sym, this));
864 }
865 
866 template <class ELFT>
868  const Elf_Shdr *SymTab = DotDynSymSec;
869  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
870  return basic_symbol_iterator(SymbolRef(Sym, this));
871 }
872 
873 template <class ELFT>
875  auto SectionsOrErr = EF.sections();
876  if (!SectionsOrErr)
877  return section_iterator(SectionRef());
878  return section_iterator(SectionRef(toDRI((*SectionsOrErr).begin()), this));
879 }
880 
881 template <class ELFT>
883  auto SectionsOrErr = EF.sections();
884  if (!SectionsOrErr)
885  return section_iterator(SectionRef());
886  return section_iterator(SectionRef(toDRI((*SectionsOrErr).end()), this));
887 }
888 
889 template <class ELFT>
891  return ELFT::Is64Bits ? 8 : 4;
892 }
893 
894 template <class ELFT>
896  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
897  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
898  case ELF::ELFCLASS32:
899  switch (EF.getHeader()->e_machine) {
900  case ELF::EM_386:
901  return "ELF32-i386";
902  case ELF::EM_IAMCU:
903  return "ELF32-iamcu";
904  case ELF::EM_X86_64:
905  return "ELF32-x86-64";
906  case ELF::EM_ARM:
907  return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
908  case ELF::EM_AVR:
909  return "ELF32-avr";
910  case ELF::EM_HEXAGON:
911  return "ELF32-hexagon";
912  case ELF::EM_LANAI:
913  return "ELF32-lanai";
914  case ELF::EM_MIPS:
915  return "ELF32-mips";
916  case ELF::EM_PPC:
917  return "ELF32-ppc";
918  case ELF::EM_RISCV:
919  return "ELF32-riscv";
920  case ELF::EM_SPARC:
921  case ELF::EM_SPARC32PLUS:
922  return "ELF32-sparc";
923  case ELF::EM_WEBASSEMBLY:
924  return "ELF32-wasm";
925  case ELF::EM_AMDGPU:
926  return "ELF32-amdgpu";
927  default:
928  return "ELF32-unknown";
929  }
930  case ELF::ELFCLASS64:
931  switch (EF.getHeader()->e_machine) {
932  case ELF::EM_386:
933  return "ELF64-i386";
934  case ELF::EM_X86_64:
935  return "ELF64-x86-64";
936  case ELF::EM_AARCH64:
937  return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
938  case ELF::EM_PPC64:
939  return "ELF64-ppc64";
940  case ELF::EM_RISCV:
941  return "ELF64-riscv";
942  case ELF::EM_S390:
943  return "ELF64-s390";
944  case ELF::EM_SPARCV9:
945  return "ELF64-sparc";
946  case ELF::EM_MIPS:
947  return "ELF64-mips";
948  case ELF::EM_WEBASSEMBLY:
949  return "ELF64-wasm";
950  case ELF::EM_AMDGPU:
951  return (EF.getHeader()->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA
952  && IsLittleEndian) ?
953  "ELF64-amdgpu-hsacobj" : "ELF64-amdgpu";
954  case ELF::EM_BPF:
955  return "ELF64-BPF";
956  default:
957  return "ELF64-unknown";
958  }
959  default:
960  // FIXME: Proper error handling.
961  report_fatal_error("Invalid ELFCLASS!");
962  }
963 }
964 
965 template <class ELFT>
967  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
968  switch (EF.getHeader()->e_machine) {
969  case ELF::EM_386:
970  case ELF::EM_IAMCU:
971  return Triple::x86;
972  case ELF::EM_X86_64:
973  return Triple::x86_64;
974  case ELF::EM_AARCH64:
975  return IsLittleEndian ? Triple::aarch64 : Triple::aarch64_be;
976  case ELF::EM_ARM:
977  return Triple::arm;
978  case ELF::EM_AVR:
979  return Triple::avr;
980  case ELF::EM_HEXAGON:
981  return Triple::hexagon;
982  case ELF::EM_LANAI:
983  return Triple::lanai;
984  case ELF::EM_MIPS:
985  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
986  case ELF::ELFCLASS32:
987  return IsLittleEndian ? Triple::mipsel : Triple::mips;
988  case ELF::ELFCLASS64:
989  return IsLittleEndian ? Triple::mips64el : Triple::mips64;
990  default:
991  report_fatal_error("Invalid ELFCLASS!");
992  }
993  case ELF::EM_PPC:
994  return Triple::ppc;
995  case ELF::EM_PPC64:
996  return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
997  case ELF::EM_RISCV:
998  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
999  case ELF::ELFCLASS32:
1000  return Triple::riscv32;
1001  case ELF::ELFCLASS64:
1002  return Triple::riscv64;
1003  default:
1004  report_fatal_error("Invalid ELFCLASS!");
1005  }
1006  case ELF::EM_S390:
1007  return Triple::systemz;
1008 
1009  case ELF::EM_SPARC:
1010  case ELF::EM_SPARC32PLUS:
1011  return IsLittleEndian ? Triple::sparcel : Triple::sparc;
1012  case ELF::EM_SPARCV9:
1013  return Triple::sparcv9;
1014  case ELF::EM_WEBASSEMBLY:
1015  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
1016  case ELF::ELFCLASS32: return Triple::wasm32;
1017  case ELF::ELFCLASS64: return Triple::wasm64;
1018  default: return Triple::UnknownArch;
1019  }
1020 
1021  case ELF::EM_AMDGPU:
1022  return (EF.getHeader()->e_ident[ELF::EI_CLASS] == ELF::ELFCLASS64
1023  && EF.getHeader()->e_ident[ELF::EI_OSABI] == ELF::ELFOSABI_AMDGPU_HSA
1024  && IsLittleEndian) ?
1026 
1027  case ELF::EM_BPF:
1028  return IsLittleEndian ? Triple::bpfel : Triple::bpfeb;
1029 
1030  default:
1031  return Triple::UnknownArch;
1032  }
1033 }
1034 
1035 template <class ELFT>
1038  return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
1039 }
1040 
1041 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
1042  return EF.getHeader()->e_type == ELF::ET_REL;
1043 }
1044 
1045 } // end namespace object
1046 } // end namespace llvm
1047 
1048 #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:68
virtual uint16_t getEMachine() const =0
static uint64_t getSymbolValue(const MCSymbol &Symbol, const MCAsmLayout &Layout)
ELFObjectFile< ELFType< support::little, false > > ELF32LEObjectFile
static bool classof(const Binary *v)
DataRefImpl getRawDataRefImpl() const
Definition: SymbolicFile.h:192
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
bool isSectionBSS(DataRefImpl Sec) const override
const ObjectFile * getObject() const
Definition: ObjectFile.h:470
bool isSectionText(DataRefImpl Sec) const override
elf_relocation_iterator(const relocation_iterator &B)
SubtargetFeatures getFeatures() const override
ELFFile< ELFT >::Elf_Rela Elf_Rela
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:178
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:661
struct llvm::object::DataRefImpl::@119 d
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:428
bool isSectionVirtual(DataRefImpl Sec) const override
uint32_t getSectionType(DataRefImpl Sec) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
const ELFObjectFileBase * getObject() const
ELFObjectFile< ELFType< support::little, true > > ELF64LEObjectFile
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
ELFObjectFile< ELFType< support::big, false > > ELF32BEObjectFile
ExternalFunctions * EF
uint8_t getSymbolOther(DataRefImpl Symb) const override
ELFFile< ELFT >::Elf_Sym Elf_Sym
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:32
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:41
elf_section_iterator(const section_iterator &B)
uint64_t getSize() const
const ELFRelocationRef * operator->() const
Tagged union holding either a T or a Error.
Expected< const typename ELFT::Shdr * > getSection(typename ELFT::ShdrRange Sections, uint32_t Index)
Definition: Object/ELF.h:171
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:264
uint8_t getBytesInAddress() const override
The number of bytes used to represent an address in this object file format.
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
static GCRegistry::Add< OcamlGC > B("ocaml","ocaml 3.10-compatible GC")
const Elf_Rel * getRel(DataRefImpl Rel) const
const Elf_Sym * getSymbol(DataRefImpl Sym) const
uint32_t getType() const
Definition: ELFObjectFile.h:87
const ELFFile< ELFT > * getELFFile() const
ArrayRef< Elf_Word > ShndxTable
const SymbolRef * operator->() const
Definition: ObjectFile.h:164
virtual uint64_t getSectionOffset(DataRefImpl Sec) const =0
Expected< const typename ELFT::Sym * > getSymbol(typename ELFT::SymRange Symbols, uint32_t Index)
Definition: Object/ELF.h:236
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
Definition: Casting.h:221
relocation_iterator section_rel_end(DataRefImpl Sec) const override
DataRefImpl toDRI(const Elf_Dyn *Dyn) const
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:36
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
ELFRelocationRef(const RelocationRef &B)
unsigned int getType() const
Definition: Binary.h:89
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:72
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:45
uint32_t getSymbolFlags(DataRefImpl Symb) const override
virtual uint64_t getSectionFlags(DataRefImpl Sec) const =0
ELFFile< ELFT >::Elf_Ehdr Elf_Ehdr
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
virtual basic_symbol_iterator symbol_end() const =0
ELFFile< ELFT >::Elf_Dyn Elf_Dyn
virtual ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
const ELFSectionRef * operator->() const
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:66
uint8_t getSymbolELFType(DataRefImpl Symb) const override
StringRef getFileFormatName() const override
void consumeError(Error Err)
Consume a Error without doing anything.
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:432
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
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:126
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:19
unsigned getArch() const override
uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override
Expected< section_iterator > getSymbolSection(const Elf_Sym *Symb, const Elf_Shdr *SymTab) const
const ObjectFile * getObject() const
Definition: ObjectFile.h:344
ErrorOr< int64_t > getAddend() const
uint64_t getSectionFlags(DataRefImpl Sec) const override
Expected< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
const ELFRelocationRef & operator*() const
ELFObjectFile< ELFType< support::big, true > > ELF64BEObjectFile
const ELFSymbolRef * operator->() const
ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
A range adaptor for a pair of iterators.
SubtargetFeatures - 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:116
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
Expected< SymbolRef::Type > getSymbolType(DataRefImpl Symb) const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:67
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:79
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:196
uint64_t getSectionOffset(DataRefImpl Sec) const override
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
const SymbolRef & operator*() const
Definition: ObjectFile.h:169
section_iterator section_end() const override
Provides ErrorOr<T> smart pointer.
const content_type & operator*() const
Definition: SymbolicFile.h:66
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:64
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
const ELFObjectFileBase * getObject() const
ELFFile< ELFT >::Elf_Shdr Elf_Shdr
uint64_t getRelocationType(DataRefImpl Rel) const override
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:128
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
const ELFObjectFileBase * getObject() const
Definition: ELFObjectFile.h:83
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
uint64_t getOffset() const
Definition: ELFObjectFile.h:95
elf_symbol_iterator(const basic_symbol_iterator &B)
std::error_code errorToErrorCode(Error Err)
Helper for converting an ECError to a std::error_code.
ELFFile< ELFT >::uintX_t uintX_t
ELFFile< ELFT >::Elf_Rel Elf_Rel
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:70
uint64_t getFlags() const
Definition: ELFObjectFile.h:91
ELFSymbolRef(const SymbolRef &B)
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:466