LLVM  3.7.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/DenseMap.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringSwitch.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Object/ELF.h"
23 #include "llvm/Object/ObjectFile.h"
24 #include "llvm/Support/Casting.h"
25 #include "llvm/Support/ELF.h"
26 #include "llvm/Support/Endian.h"
30 #include <algorithm>
31 #include <cctype>
32 #include <limits>
33 #include <utility>
34 
35 namespace llvm {
36 namespace object {
37 
38 class elf_symbol_iterator;
39 class ELFSymbolRef;
40 class ELFRelocationRef;
41 
42 class ELFObjectFileBase : public ObjectFile {
43  friend class ELFSymbolRef;
44  friend class ELFSectionRef;
45  friend class ELFRelocationRef;
46 
47 protected:
49 
50  virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
51  virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
52  virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
53 
54  virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
55  virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
56 
57  virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
58 public:
59 
62 
64 
65  static inline bool classof(const Binary *v) { return v->isELF(); }
66 };
67 
68 class ELFSectionRef : public SectionRef {
69 public:
71  assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
72  }
73 
74  const ELFObjectFileBase *getObject() const {
75  return cast<ELFObjectFileBase>(SectionRef::getObject());
76  }
77 
78  uint32_t getType() const {
80  }
81 
82  uint64_t getFlags() const {
84  }
85 };
86 
88 public:
90  assert(isa<ELFObjectFileBase>(B->getObject()));
91  }
92 
93  const ELFSectionRef *operator->() const {
94  return static_cast<const ELFSectionRef *>(section_iterator::operator->());
95  }
96 
97  const ELFSectionRef &operator*() const {
98  return static_cast<const ELFSectionRef &>(section_iterator::operator*());
99  }
100 };
101 
102 class ELFSymbolRef : public SymbolRef {
103 public:
105  assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
106  }
107 
108  const ELFObjectFileBase *getObject() const {
109  return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
110  }
111 
112  uint64_t getSize() const {
114  }
115 
116  uint8_t getOther() const {
118  }
119 
120  uint8_t getELFType() const {
122  }
123 };
124 
126 public:
128  : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
129  cast<ELFObjectFileBase>(B->getObject()))) {}
130 
131  const ELFSymbolRef *operator->() const {
132  return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
133  }
134 
135  const ELFSymbolRef &operator*() const {
136  return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
137  }
138 };
139 
141 public:
143  assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
144  }
145 
146  const ELFObjectFileBase *getObject() const {
147  return cast<ELFObjectFileBase>(RelocationRef::getObject());
148  }
149 
152  }
153 };
154 
156 public:
159  B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
160 
161  const ELFRelocationRef *operator->() const {
162  return static_cast<const ELFRelocationRef *>(
164  }
165 
166  const ELFRelocationRef &operator*() const {
167  return static_cast<const ELFRelocationRef &>(
169  }
170 };
171 
175 }
176 
177 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
178  uint64_t getSymbolSize(DataRefImpl Sym) const override;
179 
180 public:
182 
183  typedef typename ELFFile<ELFT>::uintX_t uintX_t;
184 
185  typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
186  typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
187  typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
188  typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
189  typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
190  typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
191 
192  typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
193 
194 protected:
195  ELFFile<ELFT> EF;
196 
197  void moveSymbolNext(DataRefImpl &Symb) const override;
199  ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
200  uint64_t getSymbolValueImpl(DataRefImpl Symb) const override;
201  uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
202  uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
203  uint32_t getSymbolFlags(DataRefImpl Symb) const override;
204  uint8_t getSymbolOther(DataRefImpl Symb) const override;
205  uint8_t getSymbolELFType(DataRefImpl Symb) const override;
206  SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
208  std::error_code getSymbolSection(DataRefImpl Symb,
209  section_iterator &Res) const override;
210 
211  void moveSectionNext(DataRefImpl &Sec) const override;
212  std::error_code getSectionName(DataRefImpl Sec,
213  StringRef &Res) const override;
214  uint64_t getSectionAddress(DataRefImpl Sec) const override;
215  uint64_t getSectionSize(DataRefImpl Sec) const override;
216  std::error_code getSectionContents(DataRefImpl Sec,
217  StringRef &Res) const override;
218  uint64_t getSectionAlignment(DataRefImpl Sec) const override;
219  bool isSectionText(DataRefImpl Sec) const override;
220  bool isSectionData(DataRefImpl Sec) const override;
221  bool isSectionBSS(DataRefImpl Sec) const override;
222  bool isSectionVirtual(DataRefImpl Sec) const override;
224  relocation_iterator section_rel_end(DataRefImpl Sec) const override;
226 
227  void moveRelocationNext(DataRefImpl &Rel) const override;
228  uint64_t getRelocationOffset(DataRefImpl Rel) const override;
229  symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
230  uint64_t getRelocationType(DataRefImpl Rel) const override;
232  SmallVectorImpl<char> &Result) const override;
233 
234  uint32_t getSectionType(DataRefImpl Sec) const override;
235  uint64_t getSectionFlags(DataRefImpl Sec) const override;
236  StringRef getRelocationTypeName(uint32_t Type) const;
237 
238  /// \brief Get the relocation section that contains \a Rel.
239  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
240  return *EF.getSection(Rel.d.a);
241  }
242 
243  const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
244  return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
245  }
246 
247  DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
248  DataRefImpl DRI;
249  if (!SymTable) {
250  DRI.d.a = 0;
251  DRI.d.b = 0;
252  return DRI;
253  }
254  assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
255  SymTable->sh_type == ELF::SHT_DYNSYM);
256 
257  uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
258  unsigned SymTableIndex =
259  (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
260 
261  DRI.d.a = SymTableIndex;
262  DRI.d.b = SymbolNum;
263  return DRI;
264  }
265 
266  const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
267  return reinterpret_cast<const Elf_Shdr *>(Sec.p);
268  }
269 
270  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
271  DataRefImpl DRI;
272  DRI.p = reinterpret_cast<uintptr_t>(Sec);
273  return DRI;
274  }
275 
277  DataRefImpl DRI;
278  DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
279  return DRI;
280  }
281 
282  bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
283  unsigned char Binding = ESym->getBinding();
284  unsigned char Visibility = ESym->getVisibility();
285 
286  // A symbol is exported if its binding is either GLOBAL or WEAK, and its
287  // visibility is either DEFAULT or PROTECTED. All other symbols are not
288  // exported.
289  if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
290  (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
291  return true;
292 
293  return false;
294  }
295 
296  // This flag is used for classof, to distinguish ELFObjectFile from
297  // its subclass. If more subclasses will be created, this flag will
298  // have to become an enum.
300 
301 public:
302  ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
303 
304  const Elf_Rel *getRel(DataRefImpl Rel) const;
305  const Elf_Rela *getRela(DataRefImpl Rela) const;
306 
307  const Elf_Sym *getSymbol(DataRefImpl Symb) const;
308 
309  basic_symbol_iterator symbol_begin_impl() const override;
310  basic_symbol_iterator symbol_end_impl() const override;
311 
314 
315  section_iterator section_begin() const override;
316  section_iterator section_end() const override;
317 
319 
320  uint8_t getBytesInAddress() const override;
321  StringRef getFileFormatName() const override;
322  unsigned getArch() const override;
323  StringRef getLoadName() const;
324 
325  std::error_code getPlatformFlags(unsigned &Result) const override {
326  Result = EF.getHeader()->e_flags;
327  return std::error_code();
328  }
329 
330  const ELFFile<ELFT> *getELFFile() const { return &EF; }
331 
332  bool isDyldType() const { return isDyldELFObject; }
333  static inline bool classof(const Binary *v) {
334  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
335  ELFT::Is64Bits);
336  }
337 
339 
340  bool isRelocatableObject() const override;
341 };
342 
347 
348 template <class ELFT>
350  ++Sym.d.b;
351 }
352 
353 template <class ELFT>
355  const Elf_Sym *ESym = toELFSymIter(Sym);
356  const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
357  const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
358  StringRef SymTable = *EF.getStringTable(StringTableSec);
359  return ESym->getName(SymTable);
360 }
361 
362 template <class ELFT>
364  return toELFShdrIter(Sec)->sh_flags;
365 }
366 
367 template <class ELFT>
369  return toELFShdrIter(Sec)->sh_type;
370 }
371 
372 template <class ELFT>
374  const Elf_Sym *ESym = getSymbol(Symb);
375  uint64_t Ret = ESym->st_value;
376  if (ESym->st_shndx == ELF::SHN_ABS)
377  return Ret;
378 
379  const Elf_Ehdr *Header = EF.getHeader();
380  // Clear the ARM/Thumb or microMIPS indicator flag.
381  if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
382  ESym->getType() == ELF::STT_FUNC)
383  Ret &= ~1;
384 
385  return Ret;
386 }
387 
388 template <class ELFT>
391  uint64_t Result = getSymbolValue(Symb);
392  const Elf_Sym *ESym = getSymbol(Symb);
393  switch (ESym->st_shndx) {
394  case ELF::SHN_COMMON:
395  case ELF::SHN_UNDEF:
396  case ELF::SHN_ABS:
397  return Result;
398  }
399 
400  const Elf_Ehdr *Header = EF.getHeader();
401 
402  if (Header->e_type == ELF::ET_REL) {
403  ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
404  if (std::error_code EC = SectionOrErr.getError())
405  return EC;
406  const Elf_Shdr *Section = *SectionOrErr;
407  if (Section)
408  Result += Section->sh_addr;
409  }
410 
411  return Result;
412 }
413 
414 template <class ELFT>
416  const Elf_Sym *Sym = toELFSymIter(Symb);
417  if (Sym->st_shndx == ELF::SHN_COMMON)
418  return Sym->st_value;
419  return 0;
420 }
421 
422 template <class ELFT>
424  return toELFSymIter(Sym)->st_size;
425 }
426 
427 template <class ELFT>
429  return toELFSymIter(Symb)->st_size;
430 }
431 
432 template <class ELFT>
434  return toELFSymIter(Symb)->st_other;
435 }
436 
437 template <class ELFT>
439  return toELFSymIter(Symb)->getType();
440 }
441 
442 template <class ELFT>
444  const Elf_Sym *ESym = getSymbol(Symb);
445 
446  switch (ESym->getType()) {
447  case ELF::STT_NOTYPE:
448  return SymbolRef::ST_Unknown;
449  case ELF::STT_SECTION:
450  return SymbolRef::ST_Debug;
451  case ELF::STT_FILE:
452  return SymbolRef::ST_File;
453  case ELF::STT_FUNC:
454  return SymbolRef::ST_Function;
455  case ELF::STT_OBJECT:
456  case ELF::STT_COMMON:
457  case ELF::STT_TLS:
458  return SymbolRef::ST_Data;
459  default:
460  return SymbolRef::ST_Other;
461  }
462 }
463 
464 template <class ELFT>
466  const Elf_Sym *ESym = toELFSymIter(Sym);
467 
468  uint32_t Result = SymbolRef::SF_None;
469 
470  if (ESym->getBinding() != ELF::STB_LOCAL)
471  Result |= SymbolRef::SF_Global;
472 
473  if (ESym->getBinding() == ELF::STB_WEAK)
474  Result |= SymbolRef::SF_Weak;
475 
476  if (ESym->st_shndx == ELF::SHN_ABS)
477  Result |= SymbolRef::SF_Absolute;
478 
479  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
480  ESym == EF.symbol_begin() || ESym == EF.dynamic_symbol_begin())
482 
483  if (EF.getHeader()->e_machine == ELF::EM_ARM) {
484  if (ErrorOr<StringRef> NameOrErr = getSymbolName(Sym)) {
485  StringRef Name = *NameOrErr;
486  if (Name.startswith("$d") || Name.startswith("$t") ||
487  Name.startswith("$a"))
489  }
490  }
491 
492  if (ESym->st_shndx == ELF::SHN_UNDEF)
493  Result |= SymbolRef::SF_Undefined;
494 
495  if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
496  Result |= SymbolRef::SF_Common;
497 
498  if (isExportedToOtherDSO(ESym))
499  Result |= SymbolRef::SF_Exported;
500 
501  if (ESym->getVisibility() == ELF::STV_HIDDEN)
502  Result |= SymbolRef::SF_Hidden;
503 
504  return Result;
505 }
506 
507 template <class ELFT>
510  ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
511  if (std::error_code EC = ESecOrErr.getError())
512  report_fatal_error(EC.message());
513 
514  const Elf_Shdr *ESec = *ESecOrErr;
515  if (!ESec)
516  return section_end();
517 
518  DataRefImpl Sec;
519  Sec.p = reinterpret_cast<intptr_t>(ESec);
520  return section_iterator(SectionRef(Sec, this));
521 }
522 
523 template <class ELFT>
524 std::error_code
526  section_iterator &Res) const {
527  Res = getSymbolSection(getSymbol(Symb));
528  return std::error_code();
529 }
530 
531 template <class ELFT>
533  const Elf_Shdr *ESec = toELFShdrIter(Sec);
534  Sec = toDRI(++ESec);
535 }
536 
537 template <class ELFT>
539  StringRef &Result) const {
540  ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
541  if (!Name)
542  return Name.getError();
543  Result = *Name;
544  return std::error_code();
545 }
546 
547 template <class ELFT>
549  return toELFShdrIter(Sec)->sh_addr;
550 }
551 
552 template <class ELFT>
554  return toELFShdrIter(Sec)->sh_size;
555 }
556 
557 template <class ELFT>
558 std::error_code
560  StringRef &Result) const {
561  const Elf_Shdr *EShdr = toELFShdrIter(Sec);
562  Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
563  return std::error_code();
564 }
565 
566 template <class ELFT>
568  return toELFShdrIter(Sec)->sh_addralign;
569 }
570 
571 template <class ELFT>
573  return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
574 }
575 
576 template <class ELFT>
578  const Elf_Shdr *EShdr = toELFShdrIter(Sec);
579  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
580  EShdr->sh_type == ELF::SHT_PROGBITS;
581 }
582 
583 template <class ELFT>
585  const Elf_Shdr *EShdr = toELFShdrIter(Sec);
586  return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
587  EShdr->sh_type == ELF::SHT_NOBITS;
588 }
589 
590 template <class ELFT>
592  return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
593 }
594 
595 template <class ELFT>
598  DataRefImpl RelData;
599  uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
600  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
601  RelData.d.b = 0;
602 
603  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
604  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
605  return relocation_iterator(RelocationRef(RelData, this));
606 
607  const Elf_Shdr *RelSec = getRelSection(RelData);
608  ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
609  if (std::error_code EC = SymSecOrErr.getError())
610  report_fatal_error(EC.message());
611  const Elf_Shdr *SymSec = *SymSecOrErr;
612  uint32_t SymSecType = SymSec->sh_type;
613  if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
614  report_fatal_error("Invalid symbol table section type!");
615  if (SymSecType == ELF::SHT_DYNSYM)
616  RelData.d.b = 1;
617 
618  return relocation_iterator(RelocationRef(RelData, this));
619 }
620 
621 template <class ELFT>
624  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
625  relocation_iterator Begin = section_rel_begin(Sec);
626  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
627  return Begin;
628  DataRefImpl RelData = Begin->getRawDataRefImpl();
629  RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
630  return relocation_iterator(RelocationRef(RelData, this));
631 }
632 
633 template <class ELFT>
636  if (EF.getHeader()->e_type != ELF::ET_REL)
637  return section_end();
638 
639  const Elf_Shdr *EShdr = toELFShdrIter(Sec);
640  uintX_t Type = EShdr->sh_type;
641  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
642  return section_end();
643 
644  ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
645  if (std::error_code EC = R.getError())
646  report_fatal_error(EC.message());
647  return section_iterator(SectionRef(toDRI(*R), this));
648 }
649 
650 // Relocations
651 template <class ELFT>
653  Rel.d.b += 2;
654 }
655 
656 template <class ELFT>
659  uint32_t symbolIdx;
660  const Elf_Shdr *sec = getRelSection(Rel);
661  if (sec->sh_type == ELF::SHT_REL)
662  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
663  else
664  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
665  if (!symbolIdx)
666  return symbol_end();
667 
668  bool IsDyn = Rel.d.b & 1;
669  DataRefImpl SymbolData;
670  if (IsDyn)
671  SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx);
672  else
673  SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
674  return symbol_iterator(SymbolRef(SymbolData, this));
675 }
676 
677 template <class ELFT>
679  assert(EF.getHeader()->e_type == ELF::ET_REL &&
680  "Only relocatable object files have relocation offsets");
681  const Elf_Shdr *sec = getRelSection(Rel);
682  if (sec->sh_type == ELF::SHT_REL)
683  return getRel(Rel)->r_offset;
684 
685  return getRela(Rel)->r_offset;
686 }
687 
688 template <class ELFT>
690  const Elf_Shdr *sec = getRelSection(Rel);
691  if (sec->sh_type == ELF::SHT_REL)
692  return getRel(Rel)->getType(EF.isMips64EL());
693  else
694  return getRela(Rel)->getType(EF.isMips64EL());
695 }
696 
697 template <class ELFT>
699  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
700 }
701 
702 template <class ELFT>
704  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
705  uint32_t type = getRelocationType(Rel);
706  EF.getRelocationTypeName(type, Result);
707 }
708 
709 template <class ELFT>
712  if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
714  return (int64_t)getRela(Rel)->r_addend;
715 }
716 
717 template <class ELFT>
718 const typename ELFFile<ELFT>::Elf_Sym *
720  return &*toELFSymIter(Symb);
721 }
722 
723 template <class ELFT>
724 const typename ELFObjectFile<ELFT>::Elf_Rel *
726  assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
727  return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
728 }
729 
730 template <class ELFT>
731 const typename ELFObjectFile<ELFT>::Elf_Rela *
733  assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
734  return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
735 }
736 
737 template <class ELFT>
740  getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
741  support::little,
742  ELFT::Is64Bits),
743  Object),
744  EF(Data.getBuffer(), EC) {}
745 
746 template <class ELFT>
748  DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0);
749  return basic_symbol_iterator(SymbolRef(Sym, this));
750 }
751 
752 template <class ELFT>
754  const Elf_Shdr *SymTab = EF.getDotSymtabSec();
755  if (!SymTab)
756  return symbol_begin_impl();
757  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
758  return basic_symbol_iterator(SymbolRef(Sym, this));
759 }
760 
761 template <class ELFT>
763  DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0);
764  return symbol_iterator(SymbolRef(Sym, this));
765 }
766 
767 template <class ELFT>
769  const Elf_Shdr *SymTab = EF.getDotDynSymSec();
770  DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
771  return basic_symbol_iterator(SymbolRef(Sym, this));
772 }
773 
774 template <class ELFT>
776  return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
777 }
778 
779 template <class ELFT>
781  return section_iterator(SectionRef(toDRI(EF.section_end()), this));
782 }
783 
784 template <class ELFT>
786  Elf_Dyn_Iter DI = EF.dynamic_table_begin();
787  Elf_Dyn_Iter DE = EF.dynamic_table_end();
788 
789  while (DI != DE && DI->getTag() != ELF::DT_SONAME)
790  ++DI;
791 
792  if (DI != DE)
793  return EF.getDynamicString(DI->getVal());
794  return "";
795 }
796 
797 template <class ELFT>
799  return ELFT::Is64Bits ? 8 : 4;
800 }
801 
802 template <class ELFT>
804  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
805  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
806  case ELF::ELFCLASS32:
807  switch (EF.getHeader()->e_machine) {
808  case ELF::EM_386:
809  return "ELF32-i386";
810  case ELF::EM_X86_64:
811  return "ELF32-x86-64";
812  case ELF::EM_ARM:
813  return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
814  case ELF::EM_HEXAGON:
815  return "ELF32-hexagon";
816  case ELF::EM_MIPS:
817  return "ELF32-mips";
818  case ELF::EM_PPC:
819  return "ELF32-ppc";
820  case ELF::EM_SPARC:
821  case ELF::EM_SPARC32PLUS:
822  return "ELF32-sparc";
823  default:
824  return "ELF32-unknown";
825  }
826  case ELF::ELFCLASS64:
827  switch (EF.getHeader()->e_machine) {
828  case ELF::EM_386:
829  return "ELF64-i386";
830  case ELF::EM_X86_64:
831  return "ELF64-x86-64";
832  case ELF::EM_AARCH64:
833  return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
834  case ELF::EM_PPC64:
835  return "ELF64-ppc64";
836  case ELF::EM_S390:
837  return "ELF64-s390";
838  case ELF::EM_SPARCV9:
839  return "ELF64-sparc";
840  case ELF::EM_MIPS:
841  return "ELF64-mips";
842  default:
843  return "ELF64-unknown";
844  }
845  default:
846  // FIXME: Proper error handling.
847  report_fatal_error("Invalid ELFCLASS!");
848  }
849 }
850 
851 template <class ELFT>
853  bool IsLittleEndian = ELFT::TargetEndianness == support::little;
854  switch (EF.getHeader()->e_machine) {
855  case ELF::EM_386:
856  return Triple::x86;
857  case ELF::EM_X86_64:
858  return Triple::x86_64;
859  case ELF::EM_AARCH64:
860  return Triple::aarch64;
861  case ELF::EM_ARM:
862  return Triple::arm;
863  case ELF::EM_HEXAGON:
864  return Triple::hexagon;
865  case ELF::EM_MIPS:
866  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
867  case ELF::ELFCLASS32:
868  return IsLittleEndian ? Triple::mipsel : Triple::mips;
869  case ELF::ELFCLASS64:
870  return IsLittleEndian ? Triple::mips64el : Triple::mips64;
871  default:
872  report_fatal_error("Invalid ELFCLASS!");
873  }
874  case ELF::EM_PPC:
875  return Triple::ppc;
876  case ELF::EM_PPC64:
877  return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
878  case ELF::EM_S390:
879  return Triple::systemz;
880 
881  case ELF::EM_SPARC:
882  case ELF::EM_SPARC32PLUS:
883  return IsLittleEndian ? Triple::sparcel : Triple::sparc;
884  case ELF::EM_SPARCV9:
885  return Triple::sparcv9;
886 
887  default:
888  return Triple::UnknownArch;
889  }
890 }
891 
892 template <class ELFT>
895  return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
896 }
897 
898 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
899  return EF.getHeader()->e_type == ELF::ET_REL;
900 }
901 
902 }
903 }
904 
905 #endif
unsigned char getType() const
Definition: ELFTypes.h:172
bool isELF() const
Definition: Binary.h:104
void getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const override
section_iterator getSymbolSection(const Elf_Sym *Symb) const
std::error_code getError() const
Definition: ErrorOr.h:178
uint64_t getSectionSize(DataRefImpl Sec) const override
Represents either an error or a value T.
Definition: ErrorOr.h:82
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:188
Iterate over constant sized entities.
Definition: Object/ELF.h:61
bool isSectionBSS(DataRefImpl Sec) const override
const ObjectFile * getObject() const
Definition: ObjectFile.h:452
bool isSectionText(DataRefImpl Sec) const override
elf_relocation_iterator(const relocation_iterator &B)
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).
This class is the base class for all object file types.
Definition: ObjectFile.h:176
const Elf_Shdr * toELFShdrIter(DataRefImpl Sec) const
ELFYAML::ELF_STV Visibility
Definition: ELFYAML.cpp:590
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:410
bool isSectionVirtual(DataRefImpl Sec) const override
uint32_t getSectionType(DataRefImpl Sec) const override
DataRefImpl toDRI(const Elf_Shdr *Sec) const
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
Definition: ELFTypes.h:312
const ELFObjectFileBase * getObject() const
ELFObjectFile< ELFType< support::little, true > > ELF64LEObjectFile
SymbolRef::Type getSymbolType(DataRefImpl Symb) const override
void moveRelocationNext(DataRefImpl &Rel) const override
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override
DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const
uint8_t getOther() const
ELFObjectFile< ELFType< support::big, false > > ELF32BEObjectFile
uint8_t getSymbolOther(DataRefImpl Symb) const override
ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const override
uint8_t getELFType() const
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: APInt.h:33
This is a value type class that represents a single relocation in the list of relocations in the obje...
Definition: ObjectFile.h:40
elf_section_iterator(const section_iterator &B)
Definition: ELFObjectFile.h:89
uint64_t getSize() const
const ELFRelocationRef * operator->() const
uint64_t getSymbolValueImpl(DataRefImpl Symb) const override
const Elf_Rela * getRela(DataRefImpl Rela) const
basic_symbol_iterator symbol_begin() const
Definition: SymbolicFile.h:136
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
elf_symbol_iterator dynamic_symbol_begin() const
const Elf_Rel * getRel(DataRefImpl Rel) const
uint32_t getType() const
Definition: ELFObjectFile.h:78
const ELFFile< ELFT > * getELFFile() const
const SymbolRef * operator->() const
Definition: ObjectFile.h:162
std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type cast(const Y &Val)
Definition: Casting.h:222
relocation_iterator section_rel_end(DataRefImpl Sec) const override
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:35
DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const
ELFRelocationRef(const RelocationRef &B)
unsigned int getType() const
Definition: Binary.h:85
static bool classof(const Binary *v)
Definition: ELFObjectFile.h:65
ErrorOr< StringRef > getName(StringRef StrTab) const
Definition: ELFTypes.h:216
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
unsigned char getVisibility() const
Access to the STV_xxx flag stored in the first two bits of st_other.
Definition: ELFTypes.h:185
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:63
section_iterator getRelocatedSection(DataRefImpl Sec) const override
uint32_t getSymbolAlignment(DataRefImpl Symb) const override
bool isSectionData(DataRefImpl Sec) const override
virtual ErrorOr< int64_t > getRelocationAddend(DataRefImpl Rel) const =0
const ELFSectionRef * operator->() const
Definition: ELFObjectFile.h:93
ErrorOr< StringRef > getSymbolName(DataRefImpl Symb) const override
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:65
uint8_t getSymbolELFType(DataRefImpl Symb) const override
StringRef getFileFormatName() const override
const ELFSectionRef & operator*() const
Definition: ELFObjectFile.h:97
std::error_code getPlatformFlags(unsigned &Result) const override
Returns platform-specific object flags, if any.
const ObjectFile * getObject() const
Definition: ObjectFile.h:414
uint64_t getSectionAddress(DataRefImpl Sec) const override
std::error_code getSectionName(DataRefImpl Sec, StringRef &Res) const override
uint64_t getRelocationOffset(DataRefImpl Rel) const override
basic_symbol_iterator symbol_end() const
Definition: SymbolicFile.h:139
content_iterator< BasicSymbolRef > basic_symbol_iterator
Definition: SymbolicFile.h:116
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
StringRef getLoadName() const
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:215
const ObjectFile * getObject() const
Definition: ObjectFile.h:334
ErrorOr< int64_t > getAddend() const
uint64_t getSectionFlags(DataRefImpl Sec) const override
const ELFRelocationRef & operator*() const
const Elf_Sym * getSymbol(DataRefImpl Symb) 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.
ErrorOr< uint64_t > getSymbolAddress(DataRefImpl Symb) const override
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:114
relocation_iterator section_rel_begin(DataRefImpl Sec) const override
iterator_range< elf_symbol_iterator > elf_symbol_iterator_range
Definition: ELFObjectFile.h:60
bool isExportedToOtherDSO(const Elf_Sym *ESym) const
ELFSectionRef(const SectionRef &B)
Definition: ELFObjectFile.h:70
struct llvm::object::DataRefImpl::@97 d
const SymbolicFile * getObject() const
Definition: SymbolicFile.h:192
basic_symbol_iterator symbol_end_impl() const override
virtual uint32_t getSectionType(DataRefImpl Sec) const =0
basic_symbol_iterator symbol_begin_impl() const override
ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
const SymbolRef & operator*() const
Definition: ObjectFile.h:167
section_iterator section_end() const override
const content_type & operator*() const
Definition: SymbolicFile.h:58
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:56
virtual elf_symbol_iterator_range getDynamicSymbolIterators() const =0
const Elf_Sym * toELFSymIter(DataRefImpl Sym) const
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:89
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
virtual uint8_t getSymbolELFType(DataRefImpl Symb) const =0
const ELFObjectFileBase * getObject() const
Definition: ELFObjectFile.h:74
virtual uint8_t getSymbolOther(DataRefImpl Symb) const =0
elf_symbol_iterator(const basic_symbol_iterator &B)
ELFFile< ELFT >::uintX_t uintX_t
unsigned char getBinding() const
Definition: ELFTypes.h:171
This is a value type class that represents a single section in the list of sections in the object fil...
Definition: ObjectFile.h:69
uint64_t getFlags() const
Definition: ELFObjectFile.h:82
ELFSymbolRef(const SymbolRef &B)
elf_symbol_iterator dynamic_symbol_end() const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:448