LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_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 template <class ELFT>
39 class ELFObjectFile : public ObjectFile {
40 public:
42 
43  typedef typename ELFFile<ELFT>::uintX_t uintX_t;
44 
45  typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
46  typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
47  typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
48  typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
49  typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
50 
51  typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
52  typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
53  typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
54 
55 protected:
56  ELFFile<ELFT> EF;
57 
58  virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
59  virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
60  virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
61  virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
62  virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
63  virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
64  virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
66  SymbolRef::Type &Res) const;
68  section_iterator &Res) const;
69  virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
70 
71  virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
72  virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
73 
74  virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
75  virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
76  virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
77  virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
78  virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
79  virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
80  virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
81  virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
82  virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
84  bool &Res) const;
85  virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
86  virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
87  virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
89  bool &Result) const;
93 
95  RelocationRef &Res) const;
96  virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
97  virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
99  virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
101  SmallVectorImpl<char> &Result) const;
102  virtual error_code
104  SmallVectorImpl<char> &Result) const;
105 
106  uint64_t getROffset(DataRefImpl Rel) const;
107  StringRef getRelocationTypeName(uint32_t Type) const;
108 
109  /// \brief Get the relocation section that contains \a Rel.
110  const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
111  return EF.getSection(Rel.d.a);
112  }
113 
114  const Elf_Rel *getRel(DataRefImpl Rel) const;
115  const Elf_Rela *getRela(DataRefImpl Rela) const;
116 
118  bool IsDynamic = Symb.p & 1;
119  if (IsDynamic)
120  return Elf_Sym_Iter(
121  EF.begin_dynamic_symbols().getEntSize(),
122  reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
123  return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
124  reinterpret_cast<const char *>(Symb.p), IsDynamic);
125  }
126 
128  DataRefImpl DRI;
129  DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
130  static_cast<uintptr_t>(Symb.isDynamic());
131  return DRI;
132  }
133 
135  return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
136  reinterpret_cast<const char *>(Sec.p));
137  }
138 
140  DataRefImpl DRI;
141  DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
142  return DRI;
143  }
144 
145  DataRefImpl toDRI(const Elf_Shdr *Sec) const {
146  DataRefImpl DRI;
147  DRI.p = reinterpret_cast<uintptr_t>(Sec);
148  return DRI;
149  }
150 
152  return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
153  reinterpret_cast<const char *>(Dyn.p));
154  }
155 
157  DataRefImpl DRI;
158  DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
159  return DRI;
160  }
161 
162  // This flag is used for classof, to distinguish ELFObjectFile from
163  // its subclass. If more subclasses will be created, this flag will
164  // have to become an enum.
166 
167 public:
168  ELFObjectFile(MemoryBuffer *Object, error_code &ec);
169 
170  const Elf_Sym *getSymbol(DataRefImpl Symb) const;
171 
172  virtual symbol_iterator begin_symbols() const;
173  virtual symbol_iterator end_symbols() const;
174 
175  virtual symbol_iterator begin_dynamic_symbols() const;
176  virtual symbol_iterator end_dynamic_symbols() const;
177 
178  virtual section_iterator begin_sections() const;
179  virtual section_iterator end_sections() const;
180 
182  virtual library_iterator end_libraries_needed() const;
183 
184  error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
186  bool &IsDefault) const;
187 
188  virtual uint8_t getBytesInAddress() const;
189  virtual StringRef getFileFormatName() const;
190  virtual StringRef getObjectType() const { return "ELF"; }
191  virtual unsigned getArch() const;
192  virtual StringRef getLoadName() const;
193 
194  const ELFFile<ELFT> *getELFFile() const { return &EF; }
195 
196  bool isDyldType() const { return isDyldELFObject; }
197  static inline bool classof(const Binary *v) {
198  return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
199  ELFT::Is64Bits);
200  }
201 };
202 
203 // Use an alignment of 2 for the typedefs since that is the worst case for
204 // ELF files in archives.
209 
210 template <class ELFT>
212  SymbolRef &Result) const {
213  Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this);
214  return object_error::success;
215 }
216 
217 template <class ELFT>
219  StringRef &Result) const {
220  ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
221  if (!Name)
222  return Name;
223  Result = *Name;
224  return object_error::success;
225 }
226 
227 template <class ELFT>
229  StringRef &Version,
230  bool &IsDefault) const {
231  DataRefImpl Symb = SymRef.getRawDataRefImpl();
232  const Elf_Sym *symb = getSymbol(Symb);
233  ErrorOr<StringRef> Ver =
234  EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
235  if (!Ver)
236  return Ver;
237  Version = *Ver;
238  return object_error::success;
239 }
240 
241 template <class ELFT>
243  uint64_t &Result) const {
244  const Elf_Sym *ESym = getSymbol(Symb);
245  const Elf_Shdr *ESec;
246  switch (EF.getSymbolTableIndex(ESym)) {
247  case ELF::SHN_COMMON:
248  // Unintialized symbols have no offset in the object file
249  case ELF::SHN_UNDEF:
250  Result = UnknownAddressOrSize;
251  return object_error::success;
252  case ELF::SHN_ABS:
253  Result = ESym->st_value;
254  return object_error::success;
255  default:
256  ESec = EF.getSection(ESym);
257  }
258 
259  switch (ESym->getType()) {
260  case ELF::STT_SECTION:
261  Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
262  return object_error::success;
263  case ELF::STT_FUNC:
264  case ELF::STT_OBJECT:
265  case ELF::STT_NOTYPE:
266  Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
267  return object_error::success;
268  default:
269  Result = UnknownAddressOrSize;
270  return object_error::success;
271  }
272 }
273 
274 template <class ELFT>
276  uint64_t &Result) const {
277  const Elf_Sym *ESym = getSymbol(Symb);
278  const Elf_Shdr *ESec;
279  switch (EF.getSymbolTableIndex(ESym)) {
280  case ELF::SHN_COMMON:
281  case ELF::SHN_UNDEF:
282  Result = UnknownAddressOrSize;
283  return object_error::success;
284  case ELF::SHN_ABS:
285  Result = ESym->st_value;
286  return object_error::success;
287  default:
288  ESec = EF.getSection(ESym);
289  }
290 
291  switch (ESym->getType()) {
292  case ELF::STT_SECTION:
293  Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
294  return object_error::success;
295  case ELF::STT_FUNC:
296  case ELF::STT_OBJECT:
297  case ELF::STT_NOTYPE:
298  bool IsRelocatable;
299  switch (EF.getHeader()->e_type) {
300  case ELF::ET_EXEC:
301  case ELF::ET_DYN:
302  IsRelocatable = false;
303  break;
304  default:
305  IsRelocatable = true;
306  }
307  Result = ESym->st_value;
308 
309  // Clear the ARM/Thumb indicator flag.
310  if (EF.getHeader()->e_machine == ELF::EM_ARM)
311  Result &= ~1;
312 
313  if (IsRelocatable && ESec != 0)
314  Result += ESec->sh_addr;
315  return object_error::success;
316  default:
317  Result = UnknownAddressOrSize;
318  return object_error::success;
319  }
320 }
321 
322 template <class ELFT>
324  uint32_t &Res) const {
325  Elf_Sym_Iter Sym = toELFSymIter(Symb);
326  if (Sym->st_shndx == ELF::SHN_COMMON)
327  Res = Sym->st_value;
328  else
329  Res = 0;
330  return object_error::success;
331 }
332 
333 template <class ELFT>
335  uint64_t &Result) const {
336  Result = toELFSymIter(Symb)->st_size;
337  return object_error::success;
338 }
339 
340 template <class ELFT>
342  SymbolRef::Type &Result) const {
343  const Elf_Sym *ESym = getSymbol(Symb);
344 
345  switch (ESym->getType()) {
346  case ELF::STT_NOTYPE:
347  Result = SymbolRef::ST_Unknown;
348  break;
349  case ELF::STT_SECTION:
350  Result = SymbolRef::ST_Debug;
351  break;
352  case ELF::STT_FILE:
353  Result = SymbolRef::ST_File;
354  break;
355  case ELF::STT_FUNC:
356  Result = SymbolRef::ST_Function;
357  break;
358  case ELF::STT_OBJECT:
359  case ELF::STT_COMMON:
360  case ELF::STT_TLS:
361  Result = SymbolRef::ST_Data;
362  break;
363  default:
364  Result = SymbolRef::ST_Other;
365  break;
366  }
367  return object_error::success;
368 }
369 
370 template <class ELFT>
372  uint32_t &Result) const {
373  const Elf_Sym *ESym = getSymbol(Symb);
374 
375  Result = SymbolRef::SF_None;
376 
377  if (ESym->getBinding() != ELF::STB_LOCAL)
378  Result |= SymbolRef::SF_Global;
379 
380  if (ESym->getBinding() == ELF::STB_WEAK)
381  Result |= SymbolRef::SF_Weak;
382 
383  if (ESym->st_shndx == ELF::SHN_ABS)
384  Result |= SymbolRef::SF_Absolute;
385 
386  if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
387  ESym == &*EF.begin_symbols())
388  Result |= SymbolRef::SF_FormatSpecific;
389 
390  if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
391  Result |= SymbolRef::SF_Undefined;
392 
393  if (ESym->getType() == ELF::STT_COMMON ||
394  EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
395  Result |= SymbolRef::SF_Common;
396 
397  if (ESym->getType() == ELF::STT_TLS)
398  Result |= SymbolRef::SF_ThreadLocal;
399 
400  return object_error::success;
401 }
402 
403 template <class ELFT>
405  section_iterator &Res) const {
406  const Elf_Sym *ESym = getSymbol(Symb);
407  const Elf_Shdr *ESec = EF.getSection(ESym);
408  if (!ESec)
409  Res = end_sections();
410  else {
411  DataRefImpl Sec;
412  Sec.p = reinterpret_cast<intptr_t>(ESec);
413  Res = section_iterator(SectionRef(Sec, this));
414  }
415  return object_error::success;
416 }
417 
418 template <class ELFT>
420  uint64_t &Val) const {
421  const Elf_Sym *ESym = getSymbol(Symb);
422  Val = ESym->st_value;
423  return object_error::success;
424 }
425 
426 template <class ELFT>
428  SectionRef &Result) const {
429  Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this);
430  return object_error::success;
431 }
432 
433 template <class ELFT>
435  StringRef &Result) const {
436  ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
437  if (!Name)
438  return Name;
439  Result = *Name;
440  return object_error::success;
441 }
442 
443 template <class ELFT>
445  uint64_t &Result) const {
446  Result = toELFShdrIter(Sec)->sh_addr;
447  return object_error::success;
448 }
449 
450 template <class ELFT>
452  uint64_t &Result) const {
453  Result = toELFShdrIter(Sec)->sh_size;
454  return object_error::success;
455 }
456 
457 template <class ELFT>
459  StringRef &Result) const {
460  Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
461  Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
462  return object_error::success;
463 }
464 
465 template <class ELFT>
467  uint64_t &Result) const {
468  Result = toELFShdrIter(Sec)->sh_addralign;
469  return object_error::success;
470 }
471 
472 template <class ELFT>
474  bool &Result) const {
475  Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
476  return object_error::success;
477 }
478 
479 template <class ELFT>
481  bool &Result) const {
482  Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
483  Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
484  EShdr->sh_type == ELF::SHT_PROGBITS;
485  return object_error::success;
486 }
487 
488 template <class ELFT>
490  bool &Result) const {
491  Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
492  Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
493  EShdr->sh_type == ELF::SHT_NOBITS;
494  return object_error::success;
495 }
496 
497 template <class ELFT>
500  bool &Result) const {
501  Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
502  return object_error::success;
503 }
504 
505 template <class ELFT>
507  bool &Result) const {
508  Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
509  return object_error::success;
510 }
511 
512 template <class ELFT>
514  bool &Result) const {
515  Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
516  return object_error::success;
517 }
518 
519 template <class ELFT>
521  bool &Result) const {
522  Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
523  Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
524  return object_error::success;
525 }
526 
527 template <class ELFT>
529  DataRefImpl Symb,
530  bool &Result) const {
531  Elf_Sym_Iter ESym = toELFSymIter(Symb);
532 
533  uintX_t Index = ESym->st_shndx;
534  bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
535 
536  Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
537  return object_error::success;
538 }
539 
540 template <class ELFT>
543  DataRefImpl RelData;
544  uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
545  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
546  RelData.d.b = 0;
547  return relocation_iterator(RelocationRef(RelData, this));
548 }
549 
550 template <class ELFT>
553  DataRefImpl RelData;
554  uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
555  const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
556  RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
557  if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
558  RelData.d.b = 0;
559  else
560  RelData.d.b = S->sh_size / S->sh_entsize;
561 
562  return relocation_iterator(RelocationRef(RelData, this));
563 }
564 
565 template <class ELFT>
568  if (EF.getHeader()->e_type != ELF::ET_REL)
569  return end_sections();
570 
571  Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
572  uintX_t Type = EShdr->sh_type;
573  if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
574  return end_sections();
575 
576  const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
577  return section_iterator(SectionRef(toDRI(R), this));
578 }
579 
580 // Relocations
581 template <class ELFT>
583  RelocationRef &Result) const {
584  ++Rel.d.b;
585  Result = RelocationRef(Rel, this);
586  return object_error::success;
587 }
588 
589 template <class ELFT>
592  uint32_t symbolIdx;
593  const Elf_Shdr *sec = getRelSection(Rel);
594  switch (sec->sh_type) {
595  default:
596  report_fatal_error("Invalid section type in Rel!");
597  case ELF::SHT_REL: {
598  symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
599  break;
600  }
601  case ELF::SHT_RELA: {
602  symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
603  break;
604  }
605  }
606  if (!symbolIdx)
607  return end_symbols();
608 
609  const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
610 
611  DataRefImpl SymbolData;
612  switch (SymSec->sh_type) {
613  default:
614  report_fatal_error("Invalid symbol table section type!");
615  case ELF::SHT_SYMTAB:
616  SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
617  break;
618  case ELF::SHT_DYNSYM:
619  SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
620  break;
621  }
622 
623  return symbol_iterator(SymbolRef(SymbolData, this));
624 }
625 
626 template <class ELFT>
628  uint64_t &Result) const {
629  Result = getROffset(Rel);
630  return object_error::success;
631 }
632 
633 template <class ELFT>
635  uint64_t &Result) const {
636  Result = getROffset(Rel);
637  return object_error::success;
638 }
639 
640 template <class ELFT>
642  const Elf_Shdr *sec = getRelSection(Rel);
643  switch (sec->sh_type) {
644  default:
645  report_fatal_error("Invalid section type in Rel!");
646  case ELF::SHT_REL:
647  return getRel(Rel)->r_offset;
648  case ELF::SHT_RELA:
649  return getRela(Rel)->r_offset;
650  }
651 }
652 
653 template <class ELFT>
655  uint64_t &Result) const {
656  const Elf_Shdr *sec = getRelSection(Rel);
657  switch (sec->sh_type) {
658  default:
659  report_fatal_error("Invalid section type in Rel!");
660  case ELF::SHT_REL: {
661  Result = getRel(Rel)->getType(EF.isMips64EL());
662  break;
663  }
664  case ELF::SHT_RELA: {
665  Result = getRela(Rel)->getType(EF.isMips64EL());
666  break;
667  }
668  }
669  return object_error::success;
670 }
671 
672 template <class ELFT>
674  return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
675 }
676 
677 template <class ELFT>
679  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
680  const Elf_Shdr *sec = getRelSection(Rel);
681  uint32_t type;
682  switch (sec->sh_type) {
683  default:
685  case ELF::SHT_REL: {
686  type = getRel(Rel)->getType(EF.isMips64EL());
687  break;
688  }
689  case ELF::SHT_RELA: {
690  type = getRela(Rel)->getType(EF.isMips64EL());
691  break;
692  }
693  }
694 
695  EF.getRelocationTypeName(type, Result);
696  return object_error::success;
697 }
698 
699 template <class ELFT>
701  int64_t &Result) const {
702  const Elf_Shdr *sec = getRelSection(Rel);
703  switch (sec->sh_type) {
704  default:
705  report_fatal_error("Invalid section type in Rel!");
706  case ELF::SHT_REL: {
707  Result = 0;
708  return object_error::success;
709  }
710  case ELF::SHT_RELA: {
711  Result = getRela(Rel)->r_addend;
712  return object_error::success;
713  }
714  }
715 }
716 
717 template <class ELFT>
719  DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
720  const Elf_Shdr *sec = getRelSection(Rel);
721  uint8_t type;
722  StringRef res;
723  int64_t addend = 0;
724  uint16_t symbol_index = 0;
725  switch (sec->sh_type) {
726  default:
728  case ELF::SHT_REL: {
729  type = getRel(Rel)->getType(EF.isMips64EL());
730  symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
731  // TODO: Read implicit addend from section data.
732  break;
733  }
734  case ELF::SHT_RELA: {
735  type = getRela(Rel)->getType(EF.isMips64EL());
736  symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
737  addend = getRela(Rel)->r_addend;
738  break;
739  }
740  }
741  const Elf_Sym *symb =
742  EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
743  ErrorOr<StringRef> SymName =
744  EF.getSymbolName(EF.getSection(sec->sh_link), symb);
745  if (!SymName)
746  return SymName;
747  switch (EF.getHeader()->e_machine) {
748  case ELF::EM_X86_64:
749  switch (type) {
750  case ELF::R_X86_64_PC8:
751  case ELF::R_X86_64_PC16:
752  case ELF::R_X86_64_PC32: {
753  std::string fmtbuf;
754  raw_string_ostream fmt(fmtbuf);
755  fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
756  fmt.flush();
757  Result.append(fmtbuf.begin(), fmtbuf.end());
758  } break;
759  case ELF::R_X86_64_8:
760  case ELF::R_X86_64_16:
761  case ELF::R_X86_64_32:
762  case ELF::R_X86_64_32S:
763  case ELF::R_X86_64_64: {
764  std::string fmtbuf;
765  raw_string_ostream fmt(fmtbuf);
766  fmt << *SymName << (addend < 0 ? "" : "+") << addend;
767  fmt.flush();
768  Result.append(fmtbuf.begin(), fmtbuf.end());
769  } break;
770  default:
771  res = "Unknown";
772  }
773  break;
774  case ELF::EM_AARCH64: {
775  std::string fmtbuf;
776  raw_string_ostream fmt(fmtbuf);
777  fmt << *SymName;
778  if (addend != 0)
779  fmt << (addend < 0 ? "" : "+") << addend;
780  fmt.flush();
781  Result.append(fmtbuf.begin(), fmtbuf.end());
782  break;
783  }
784  case ELF::EM_ARM:
785  case ELF::EM_HEXAGON:
786  res = *SymName;
787  break;
788  default:
789  res = "Unknown";
790  }
791  if (Result.empty())
792  Result.append(res.begin(), res.end());
793  return object_error::success;
794 }
795 
796 template <class ELFT>
797 const typename ELFFile<ELFT>::Elf_Sym *
799  return &*toELFSymIter(Symb);
800 }
801 
802 template <class ELFT>
803 const typename ELFObjectFile<ELFT>::Elf_Rel *
805  return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
806 }
807 
808 template <class ELFT>
809 const typename ELFObjectFile<ELFT>::Elf_Rela *
811  return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
812 }
813 
814 template <class ELFT>
816  : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
817  support::little,
818  ELFT::Is64Bits),
819  Object),
820  EF(Object, ec) {}
821 
822 template <class ELFT>
824  return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
825 }
826 
827 template <class ELFT>
829  return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
830 }
831 
832 template <class ELFT>
834  return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
835 }
836 
837 template <class ELFT>
839  return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
840 }
841 
842 template <class ELFT>
844  return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
845 }
846 
847 template <class ELFT>
849  return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
850 }
851 
852 template <class ELFT>
854  Elf_Dyn_Iter DI = EF.begin_dynamic_table();
855  Elf_Dyn_Iter DE = EF.end_dynamic_table();
856 
857  while (DI != DE && DI->getTag() != ELF::DT_SONAME)
858  ++DI;
859 
860  if (DI != DE)
861  return EF.getDynamicString(DI->getVal());
862  return "";
863 }
864 
865 template <class ELFT>
867  Elf_Dyn_Iter DI = EF.begin_dynamic_table();
868  Elf_Dyn_Iter DE = EF.end_dynamic_table();
869 
870  while (DI != DE && DI->getTag() != ELF::DT_SONAME)
871  ++DI;
872 
873  return library_iterator(LibraryRef(toDRI(DI), this));
874 }
875 
876 template <class ELFT>
878  LibraryRef &Result) const {
879  Elf_Dyn_Iter DI = toELFDynIter(Data);
880  Elf_Dyn_Iter DE = EF.end_dynamic_table();
881 
882  // Skip to the next DT_NEEDED entry.
883  do
884  ++DI;
885  while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
886 
887  Result = LibraryRef(toDRI(DI), this);
888  return object_error::success;
889 }
890 
891 template <class ELFT>
893  StringRef &Res) const {
894  Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
895  return object_error::success;
896 }
897 
898 template <class ELFT>
900  return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
901 }
902 
903 template <class ELFT>
905  return ELFT::Is64Bits ? 8 : 4;
906 }
907 
908 template <class ELFT>
910  switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
911  case ELF::ELFCLASS32:
912  switch (EF.getHeader()->e_machine) {
913  case ELF::EM_386:
914  return "ELF32-i386";
915  case ELF::EM_X86_64:
916  return "ELF32-x86-64";
917  case ELF::EM_ARM:
918  return "ELF32-arm";
919  case ELF::EM_HEXAGON:
920  return "ELF32-hexagon";
921  case ELF::EM_MIPS:
922  return "ELF32-mips";
923  case ELF::EM_PPC:
924  return "ELF32-ppc";
925  default:
926  return "ELF32-unknown";
927  }
928  case ELF::ELFCLASS64:
929  switch (EF.getHeader()->e_machine) {
930  case ELF::EM_386:
931  return "ELF64-i386";
932  case ELF::EM_X86_64:
933  return "ELF64-x86-64";
934  case ELF::EM_AARCH64:
935  return "ELF64-aarch64";
936  case ELF::EM_PPC64:
937  return "ELF64-ppc64";
938  case ELF::EM_S390:
939  return "ELF64-s390";
940  default:
941  return "ELF64-unknown";
942  }
943  default:
944  // FIXME: Proper error handling.
945  report_fatal_error("Invalid ELFCLASS!");
946  }
947 }
948 
949 template <class ELFT>
951  switch (EF.getHeader()->e_machine) {
952  case ELF::EM_386:
953  return Triple::x86;
954  case ELF::EM_X86_64:
955  return Triple::x86_64;
956  case ELF::EM_AARCH64:
957  return Triple::aarch64;
958  case ELF::EM_ARM:
959  return Triple::arm;
960  case ELF::EM_HEXAGON:
961  return Triple::hexagon;
962  case ELF::EM_MIPS:
963  return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
964  : Triple::mips;
965  case ELF::EM_PPC64:
966  return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
967  : Triple::ppc64;
968  case ELF::EM_S390:
969  return Triple::systemz;
970  default:
971  return Triple::UnknownArch;
972  }
973 }
974 
975 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
976 /// and make these member functions?
978  int64_t &Addend) {
979  const ObjectFile *Obj = R.getObjectFile();
980  DataRefImpl DRI = R.getRawDataRefImpl();
981  // Little-endian 32-bit
982  if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
983  return ELFObj->getRelocationAddend(DRI, Addend);
984 
985  // Big-endian 32-bit
986  if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
987  return ELFObj->getRelocationAddend(DRI, Addend);
988 
989  // Little-endian 64-bit
990  if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
991  return ELFObj->getRelocationAddend(DRI, Addend);
992 
993  // Big-endian 64-bit
994  if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
995  return ELFObj->getRelocationAddend(DRI, Addend);
996 
997  llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
998 }
999 
1000 /// This is a generic interface for retrieving GNU symbol version
1001 /// information from an ELFObjectFile.
1002 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1003  const SymbolRef &Sym,
1004  StringRef &Version,
1005  bool &IsDefault) {
1006  // Little-endian 32-bit
1007  if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1008  return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1009 
1010  // Big-endian 32-bit
1011  if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1012  return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1013 
1014  // Little-endian 64-bit
1015  if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1016  return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1017 
1018  // Big-endian 64-bit
1019  if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1020  return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1021 
1022  llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");
1023 }
1024 }
1025 }
1026 
1027 #endif
unsigned char getType() const
Definition: ELFTypes.h:183
COFF::RelocationTypeX86 Type
Definition: COFFYAML.cpp:227
Represents either an error or a value T.
Definition: ErrorOr.h:94
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:445
virtual symbol_iterator end_dynamic_symbols() const
virtual library_iterator begin_libraries_needed() const
static bool classof(const Binary *v)
virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const
Iterate over constant sized entities.
Definition: Object/ELF.h:59
ELFFile< ELFT >::Elf_Rela Elf_Rela
Definition: ELFObjectFile.h:48
Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const
virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const
virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const
virtual symbol_iterator begin_symbols() const
virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const
ELFFile< ELFT >::Elf_Dyn_Iter Elf_Dyn_Iter
Definition: ELFObjectFile.h:53
DataRefImpl toDRI(const Elf_Shdr *Sec) const
Elf_Dyn_Impl: This inherits from Elf_Dyn_Base, adding getters and setters.
Definition: ELFTypes.h:280
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const
DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const
virtual symbol_iterator begin_dynamic_symbols() const
#define llvm_unreachable(msg)
virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb, bool &Result) const
virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const
error_code getSymbolVersion(SymbolRef Symb, StringRef &Version, bool &IsDefault) const
virtual error_code getSymbolType(DataRefImpl Symb, SymbolRef::Type &Res) const
virtual StringRef getObjectType() const
virtual error_code getRelocationValueString(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const
virtual symbol_iterator end_symbols() const
const Elf_Rela * getRela(DataRefImpl Rela) const
bool LLVM_ATTRIBUTE_UNUSED_RESULT empty() const
Definition: SmallVector.h:56
const Elf_Rel * getRel(DataRefImpl Rel) const
const ELFFile< ELFT > * getELFFile() const
ELFObjectFile< ELFType< support::little, 2, true > > ELF64LEObjectFile
virtual section_iterator getRelocatedSection(DataRefImpl Sec) const
virtual section_iterator begin_sections() const
iterator begin() const
Definition: StringRef.h:97
content_iterator< SectionRef > section_iterator
Definition: ObjectFile.h:133
unsigned int getType() const
Definition: Binary.h:80
virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const
static error_code getELFRelocationAddend(const RelocationRef R, int64_t &Addend)
virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const
virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const
static error_code GetELFSymbolVersion(const ObjectFile *Obj, const SymbolRef &Sym, StringRef &Version, bool &IsDefault)
content_iterator< SymbolRef > symbol_iterator
Definition: ObjectFile.h:87
virtual uint8_t getBytesInAddress() const
The number of bytes used to represent an address in this object file format.
virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const
virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const
static unsigned int getELFType(bool isLE, bool is64Bits)
Definition: Binary.h:59
virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const
virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const
virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const
content_iterator< LibraryRef > library_iterator
Definition: ObjectFile.h:259
virtual error_code isSectionRequiredForExecution(DataRefImpl Sec, bool &Res) const
void append(in_iter in_start, in_iter in_end)
Definition: SmallVector.h:445
content_iterator< RelocationRef > relocation_iterator
Definition: ObjectFile.h:129
virtual section_iterator end_sections() const
ELFObjectFile< ELFType< support::big, 2, true > > ELF64BEObjectFile
virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const
Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const
virtual StringRef getFileFormatName() const
StringRef getELFRelocationTypeName(uint32_t Machine, uint32_t Type)
Definition: ELF.cpp:19
virtual StringRef getLoadName() const
uint64_t getROffset(DataRefImpl Rel) const
ELFObjectFile(MemoryBuffer *Object, error_code &ec)
virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const
virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const
virtual relocation_iterator section_rel_end(DataRefImpl Sec) const
const Elf_Sym * getSymbol(DataRefImpl Symb) const
const uint64_t UnknownAddressOrSize
Definition: ObjectFile.h:261
virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const
struct llvm::object::DataRefImpl::@77 d
virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const
virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const
virtual error_code getRelocationTypeName(DataRefImpl Rel, SmallVectorImpl< char > &Result) const
ELFFile< ELFT >::Elf_Shdr_Iter Elf_Shdr_Iter
Definition: ELFObjectFile.h:52
Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const
virtual error_code getRelocationNext(DataRefImpl Rel, RelocationRef &Res) const
virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const
ELFFile< ELFT >::Elf_Sym_Iter Elf_Sym_Iter
Definition: ELFObjectFile.h:51
const Elf_Shdr * getRelSection(DataRefImpl Rel) const
Get the relocation section that contains Rel.
virtual unsigned getArch() const
ELFObjectFile< ELFType< support::big, 2, false > > ELF32BEObjectFile
const ObjectFile * getObjectFile() const
Definition: ObjectFile.h:585
iterator end() const
Definition: StringRef.h:99
virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const
#define LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
Definition: ELFTypes.h:102
virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const
DataRefImpl toDRI(Elf_Shdr_Iter Sec) const
virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const
virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const
virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const
virtual library_iterator end_libraries_needed() const
MemoryBuffer * Data
Definition: Binary.h:35
virtual error_code getSymbolSection(DataRefImpl Symb, section_iterator &Res) const
error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const
ELFFile< ELFT >::uintX_t uintX_t
Definition: ELFObjectFile.h:43
ELFFile< ELFT >::Elf_Rel Elf_Rel
Definition: ELFObjectFile.h:47
unsigned char getBinding() const
Definition: ELFTypes.h:182
ELFObjectFile< ELFType< support::little, 2, false > > ELF32LEObjectFile
DataRefImpl toDRI(Elf_Sym_Iter Symb) const
DataRefImpl getRawDataRefImpl() const
Definition: ObjectFile.h:581