1 //===- ELFObjectFile.h - ELF object file implementation ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file declares the ELFObjectFile template class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_OBJECT_ELF_OBJECT_FILE_H
15 #define LLVM_OBJECT_ELF_OBJECT_FILE_H
17 #include "llvm/ADT/DenseMap.h"
18 #include "llvm/ADT/PointerIntPair.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"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/MemoryBuffer.h"
29 #include "llvm/Support/raw_ostream.h"
39 class ELFObjectFile : public ObjectFile {
41 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
43 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
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;
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;
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 getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
65 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
66 virtual error_code getSymbolType(DataRefImpl Symb,
67 SymbolRef::Type &Res) const;
68 virtual error_code getSymbolSection(DataRefImpl Symb,
69 section_iterator &Res) const;
70 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
72 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
73 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
75 virtual error_code getSectionNext(DataRefImpl Sec, SectionRef &Res) const;
76 virtual error_code getSectionName(DataRefImpl Sec, StringRef &Res) const;
77 virtual error_code getSectionAddress(DataRefImpl Sec, uint64_t &Res) const;
78 virtual error_code getSectionSize(DataRefImpl Sec, uint64_t &Res) const;
79 virtual error_code getSectionContents(DataRefImpl Sec, StringRef &Res) const;
80 virtual error_code getSectionAlignment(DataRefImpl Sec, uint64_t &Res) const;
81 virtual error_code isSectionText(DataRefImpl Sec, bool &Res) const;
82 virtual error_code isSectionData(DataRefImpl Sec, bool &Res) const;
83 virtual error_code isSectionBSS(DataRefImpl Sec, bool &Res) const;
84 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
86 virtual error_code isSectionVirtual(DataRefImpl Sec, bool &Res) const;
87 virtual error_code isSectionZeroInit(DataRefImpl Sec, bool &Res) const;
88 virtual error_code isSectionReadOnlyData(DataRefImpl Sec, bool &Res) const;
89 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
91 virtual relocation_iterator section_rel_begin(DataRefImpl Sec) const;
92 virtual relocation_iterator section_rel_end(DataRefImpl Sec) const;
93 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
95 virtual error_code getRelocationNext(DataRefImpl Rel,
96 RelocationRef &Res) const;
97 virtual error_code getRelocationAddress(DataRefImpl Rel, uint64_t &Res) const;
98 virtual error_code getRelocationOffset(DataRefImpl Rel, uint64_t &Res) const;
99 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
100 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
101 virtual error_code getRelocationTypeName(DataRefImpl Rel,
102 SmallVectorImpl<char> &Result) const;
104 getRelocationValueString(DataRefImpl Rel,
105 SmallVectorImpl<char> &Result) const;
107 protected: // ELF specific protected members.
108 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
109 uint64_t getROffset(DataRefImpl Rel) const;
110 StringRef getRelocationTypeName(uint32_t Type) const;
112 /// \brief Get the relocation section that contains \a Rel.
113 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
114 return EF.getSection(Rel.d.a);
117 const Elf_Rel *getRel(DataRefImpl Rel) const;
118 const Elf_Rela *getRela(DataRefImpl Rela) const;
120 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
121 bool IsDynamic = Symb.p & 1;
124 EF.begin_dynamic_symbols().getEntSize(),
125 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
126 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
127 reinterpret_cast<const char *>(Symb.p), IsDynamic);
130 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
132 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
133 static_cast<uintptr_t>(Symb.isDynamic());
137 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
138 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
139 reinterpret_cast<const char *>(Sec.p));
142 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
144 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
148 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
150 DRI.p = reinterpret_cast<uintptr_t>(Sec);
154 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
155 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
156 reinterpret_cast<const char *>(Dyn.p));
159 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
161 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
165 // This flag is used for classof, to distinguish ELFObjectFile from
166 // its subclass. If more subclasses will be created, this flag will
167 // have to become an enum.
168 bool isDyldELFObject;
171 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
173 virtual symbol_iterator begin_symbols() const;
174 virtual symbol_iterator end_symbols() const;
176 virtual symbol_iterator begin_dynamic_symbols() const;
177 virtual symbol_iterator end_dynamic_symbols() const;
179 virtual section_iterator begin_sections() const;
180 virtual section_iterator end_sections() const;
182 virtual library_iterator begin_libraries_needed() const;
183 virtual library_iterator end_libraries_needed() const;
185 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
186 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
187 bool &IsDefault) const;
189 virtual uint8_t getBytesInAddress() const;
190 virtual StringRef getFileFormatName() const;
191 virtual StringRef getObjectType() const { return "ELF"; }
192 virtual unsigned getArch() const;
193 virtual StringRef getLoadName() const;
195 const ELFFile<ELFT> *getELFFile() const { return &EF; }
197 bool isDyldType() const { return isDyldELFObject; }
198 static inline bool classof(const Binary *v) {
199 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
204 // Use an alignment of 2 for the typedefs since that is the worst case for
205 // ELF files in archives.
206 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
207 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
208 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
209 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
211 template <class ELFT>
212 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
213 SymbolRef &Result) const {
214 Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this);
215 return object_error::success;
218 template <class ELFT>
219 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
220 StringRef &Result) const {
221 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
225 return object_error::success;
228 template <class ELFT>
229 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
231 bool &IsDefault) const {
232 DataRefImpl Symb = SymRef.getRawDataRefImpl();
233 const Elf_Sym *symb = getSymbol(Symb);
234 ErrorOr<StringRef> Ver =
235 EF.getSymbolVersion(EF.getSection(Symb.d.b), symb, IsDefault);
239 return object_error::success;
242 template <class ELFT>
243 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
244 uint64_t &Result) const {
245 const Elf_Sym *ESym = getSymbol(Symb);
246 const Elf_Shdr *ESec;
247 switch (EF.getSymbolTableIndex(ESym)) {
248 case ELF::SHN_COMMON:
249 // Unintialized symbols have no offset in the object file
251 Result = UnknownAddressOrSize;
252 return object_error::success;
254 Result = ESym->st_value;
255 return object_error::success;
257 ESec = EF.getSection(ESym);
260 switch (ESym->getType()) {
261 case ELF::STT_SECTION:
262 Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
263 return object_error::success;
265 case ELF::STT_OBJECT:
266 case ELF::STT_NOTYPE:
267 Result = ESym->st_value + (ESec ? ESec->sh_offset : 0);
268 return object_error::success;
270 Result = UnknownAddressOrSize;
271 return object_error::success;
275 template <class ELFT>
276 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
277 uint64_t &Result) const {
278 const Elf_Sym *ESym = getSymbol(Symb);
279 const Elf_Shdr *ESec;
280 switch (EF.getSymbolTableIndex(ESym)) {
281 case ELF::SHN_COMMON:
283 Result = UnknownAddressOrSize;
284 return object_error::success;
286 Result = ESym->st_value;
287 return object_error::success;
289 ESec = EF.getSection(ESym);
292 switch (ESym->getType()) {
293 case ELF::STT_SECTION:
294 Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
295 return object_error::success;
297 case ELF::STT_OBJECT:
298 case ELF::STT_NOTYPE:
300 switch (EF.getHeader()->e_type) {
303 IsRelocatable = false;
306 IsRelocatable = true;
308 Result = ESym->st_value;
310 // Clear the ARM/Thumb indicator flag.
311 if (EF.getHeader()->e_machine == ELF::EM_ARM)
314 if (IsRelocatable && ESec != 0)
315 Result += ESec->sh_addr;
316 return object_error::success;
318 Result = UnknownAddressOrSize;
319 return object_error::success;
323 template <class ELFT>
324 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
325 uint32_t &Res) const {
326 Elf_Sym_Iter Sym = toELFSymIter(Symb);
327 if (Sym->st_shndx == ELF::SHN_COMMON)
331 return object_error::success;
334 template <class ELFT>
335 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
336 uint64_t &Result) const {
337 Result = toELFSymIter(Symb)->st_size;
338 return object_error::success;
341 template <class ELFT>
342 error_code ELFObjectFile<ELFT>::getSymbolNMTypeChar(DataRefImpl Symb,
343 char &Result) const {
344 const Elf_Sym *ESym = getSymbol(Symb);
345 const Elf_Shdr *ESec = EF.getSection(ESym);
350 switch (ESec->sh_type) {
351 case ELF::SHT_PROGBITS:
352 case ELF::SHT_DYNAMIC:
353 switch (ESec->sh_flags) {
354 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
357 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
361 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
362 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
367 case ELF::SHT_NOBITS:
372 switch (EF.getSymbolTableIndex(ESym)) {
380 case ELF::SHN_COMMON:
385 switch (ESym->getBinding()) {
386 case ELF::STB_GLOBAL:
387 ret = ::toupper(ret);
390 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
392 else if (ESym->getType() == ELF::STT_OBJECT)
398 if (ret == '?' && ESym->getType() == ELF::STT_SECTION) {
399 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
402 Result = StringSwitch<char>(*Name)
403 .StartsWith(".debug", 'N')
404 .StartsWith(".note", 'n')
406 return object_error::success;
410 return object_error::success;
413 template <class ELFT>
414 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
415 SymbolRef::Type &Result) const {
416 const Elf_Sym *ESym = getSymbol(Symb);
418 switch (ESym->getType()) {
419 case ELF::STT_NOTYPE:
420 Result = SymbolRef::ST_Unknown;
422 case ELF::STT_SECTION:
423 Result = SymbolRef::ST_Debug;
426 Result = SymbolRef::ST_File;
429 Result = SymbolRef::ST_Function;
431 case ELF::STT_OBJECT:
432 case ELF::STT_COMMON:
434 Result = SymbolRef::ST_Data;
437 Result = SymbolRef::ST_Other;
440 return object_error::success;
443 template <class ELFT>
444 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
445 uint32_t &Result) const {
446 const Elf_Sym *ESym = getSymbol(Symb);
448 Result = SymbolRef::SF_None;
450 if (ESym->getBinding() != ELF::STB_LOCAL)
451 Result |= SymbolRef::SF_Global;
453 if (ESym->getBinding() == ELF::STB_WEAK)
454 Result |= SymbolRef::SF_Weak;
456 if (ESym->st_shndx == ELF::SHN_ABS)
457 Result |= SymbolRef::SF_Absolute;
459 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
460 ESym == &*EF.begin_symbols())
461 Result |= SymbolRef::SF_FormatSpecific;
463 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
464 Result |= SymbolRef::SF_Undefined;
466 if (ESym->getType() == ELF::STT_COMMON ||
467 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
468 Result |= SymbolRef::SF_Common;
470 if (ESym->getType() == ELF::STT_TLS)
471 Result |= SymbolRef::SF_ThreadLocal;
473 return object_error::success;
476 template <class ELFT>
477 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
478 section_iterator &Res) const {
479 const Elf_Sym *ESym = getSymbol(Symb);
480 const Elf_Shdr *ESec = EF.getSection(ESym);
482 Res = end_sections();
485 Sec.p = reinterpret_cast<intptr_t>(ESec);
486 Res = section_iterator(SectionRef(Sec, this));
488 return object_error::success;
491 template <class ELFT>
492 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
493 uint64_t &Val) const {
494 const Elf_Sym *ESym = getSymbol(Symb);
495 Val = ESym->st_value;
496 return object_error::success;
499 template <class ELFT>
500 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
501 SectionRef &Result) const {
502 Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this);
503 return object_error::success;
506 template <class ELFT>
507 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
508 StringRef &Result) const {
509 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
513 return object_error::success;
516 template <class ELFT>
517 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
518 uint64_t &Result) const {
519 Result = toELFShdrIter(Sec)->sh_addr;
520 return object_error::success;
523 template <class ELFT>
524 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
525 uint64_t &Result) const {
526 Result = toELFShdrIter(Sec)->sh_size;
527 return object_error::success;
530 template <class ELFT>
531 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
532 StringRef &Result) const {
533 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
534 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
535 return object_error::success;
538 template <class ELFT>
539 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
540 uint64_t &Result) const {
541 Result = toELFShdrIter(Sec)->sh_addralign;
542 return object_error::success;
545 template <class ELFT>
546 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
547 bool &Result) const {
548 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
549 return object_error::success;
552 template <class ELFT>
553 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
554 bool &Result) const {
555 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
556 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
557 EShdr->sh_type == ELF::SHT_PROGBITS;
558 return object_error::success;
561 template <class ELFT>
562 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
563 bool &Result) const {
564 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
565 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
566 EShdr->sh_type == ELF::SHT_NOBITS;
567 return object_error::success;
570 template <class ELFT>
572 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
573 bool &Result) const {
574 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
575 return object_error::success;
578 template <class ELFT>
579 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
580 bool &Result) const {
581 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
582 return object_error::success;
585 template <class ELFT>
586 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
587 bool &Result) const {
588 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
589 return object_error::success;
592 template <class ELFT>
593 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
594 bool &Result) const {
595 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
596 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
597 return object_error::success;
600 template <class ELFT>
601 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
603 bool &Result) const {
604 Elf_Sym_Iter ESym = toELFSymIter(Symb);
606 uintX_t Index = ESym->st_shndx;
607 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
609 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
610 return object_error::success;
613 template <class ELFT>
615 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
617 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
618 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
620 return relocation_iterator(RelocationRef(RelData, this));
623 template <class ELFT>
625 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
627 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
628 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
629 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
630 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
633 RelData.d.b = S->sh_size / S->sh_entsize;
635 return relocation_iterator(RelocationRef(RelData, this));
638 template <class ELFT>
640 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
641 if (EF.getHeader()->e_type != ELF::ET_REL)
642 return end_sections();
644 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
645 uintX_t Type = EShdr->sh_type;
646 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
647 return end_sections();
649 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
650 return section_iterator(SectionRef(toDRI(R), this));
654 template <class ELFT>
655 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
656 RelocationRef &Result) const {
658 Result = RelocationRef(Rel, this);
659 return object_error::success;
662 template <class ELFT>
664 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
666 const Elf_Shdr *sec = getRelSection(Rel);
667 switch (sec->sh_type) {
669 report_fatal_error("Invalid section type in Rel!");
671 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
674 case ELF::SHT_RELA: {
675 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
680 return end_symbols();
682 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
684 DataRefImpl SymbolData;
685 switch (SymSec->sh_type) {
687 report_fatal_error("Invalid symbol table section type!");
688 case ELF::SHT_SYMTAB:
689 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
691 case ELF::SHT_DYNSYM:
692 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
696 return symbol_iterator(SymbolRef(SymbolData, this));
699 template <class ELFT>
700 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
701 uint64_t &Result) const {
702 Result = getROffset(Rel);
703 return object_error::success;
706 template <class ELFT>
707 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
708 uint64_t &Result) const {
709 Result = getROffset(Rel);
710 return object_error::success;
713 template <class ELFT>
714 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
715 const Elf_Shdr *sec = getRelSection(Rel);
716 switch (sec->sh_type) {
718 report_fatal_error("Invalid section type in Rel!");
720 return getRel(Rel)->r_offset;
722 return getRela(Rel)->r_offset;
726 template <class ELFT>
727 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
728 uint64_t &Result) const {
729 const Elf_Shdr *sec = getRelSection(Rel);
730 switch (sec->sh_type) {
732 report_fatal_error("Invalid section type in Rel!");
734 Result = getRel(Rel)->getType(EF.isMips64EL());
737 case ELF::SHT_RELA: {
738 Result = getRela(Rel)->getType(EF.isMips64EL());
742 return object_error::success;
745 template <class ELFT>
746 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
747 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
750 template <class ELFT>
751 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
752 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
753 const Elf_Shdr *sec = getRelSection(Rel);
755 switch (sec->sh_type) {
757 return object_error::parse_failed;
759 type = getRel(Rel)->getType(EF.isMips64EL());
762 case ELF::SHT_RELA: {
763 type = getRela(Rel)->getType(EF.isMips64EL());
768 EF.getRelocationTypeName(type, Result);
769 return object_error::success;
772 template <class ELFT>
773 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
774 int64_t &Result) const {
775 const Elf_Shdr *sec = getRelSection(Rel);
776 switch (sec->sh_type) {
778 report_fatal_error("Invalid section type in Rel!");
781 return object_error::success;
783 case ELF::SHT_RELA: {
784 Result = getRela(Rel)->r_addend;
785 return object_error::success;
790 template <class ELFT>
791 error_code ELFObjectFile<ELFT>::getRelocationValueString(
792 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
793 const Elf_Shdr *sec = getRelSection(Rel);
797 uint16_t symbol_index = 0;
798 switch (sec->sh_type) {
800 return object_error::parse_failed;
802 type = getRel(Rel)->getType(EF.isMips64EL());
803 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
804 // TODO: Read implicit addend from section data.
807 case ELF::SHT_RELA: {
808 type = getRela(Rel)->getType(EF.isMips64EL());
809 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
810 addend = getRela(Rel)->r_addend;
814 const Elf_Sym *symb =
815 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
816 ErrorOr<StringRef> SymName =
817 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
820 switch (EF.getHeader()->e_machine) {
823 case ELF::R_X86_64_PC8:
824 case ELF::R_X86_64_PC16:
825 case ELF::R_X86_64_PC32: {
827 raw_string_ostream fmt(fmtbuf);
828 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
830 Result.append(fmtbuf.begin(), fmtbuf.end());
832 case ELF::R_X86_64_8:
833 case ELF::R_X86_64_16:
834 case ELF::R_X86_64_32:
835 case ELF::R_X86_64_32S:
836 case ELF::R_X86_64_64: {
838 raw_string_ostream fmt(fmtbuf);
839 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
841 Result.append(fmtbuf.begin(), fmtbuf.end());
847 case ELF::EM_AARCH64: {
849 raw_string_ostream fmt(fmtbuf);
852 fmt << (addend < 0 ? "" : "+") << addend;
854 Result.append(fmtbuf.begin(), fmtbuf.end());
858 case ELF::EM_HEXAGON:
865 Result.append(res.begin(), res.end());
866 return object_error::success;
869 template <class ELFT>
870 const typename ELFFile<ELFT>::Elf_Sym *
871 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
872 return &*toELFSymIter(Symb);
875 template <class ELFT>
876 const typename ELFObjectFile<ELFT>::Elf_Rel *
877 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
878 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
881 template <class ELFT>
882 const typename ELFObjectFile<ELFT>::Elf_Rela *
883 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
884 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
887 template <class ELFT>
888 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
889 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
895 template <class ELFT>
896 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
897 return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
900 template <class ELFT>
901 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
902 return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
905 template <class ELFT>
906 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
907 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
910 template <class ELFT>
911 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
912 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
915 template <class ELFT>
916 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
917 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
920 template <class ELFT>
921 section_iterator ELFObjectFile<ELFT>::end_sections() const {
922 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
925 template <class ELFT>
926 StringRef ELFObjectFile<ELFT>::getLoadName() const {
927 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
928 Elf_Dyn_Iter DE = EF.end_dynamic_table();
930 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
934 return EF.getDynamicString(DI->getVal());
938 template <class ELFT>
939 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
940 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
941 Elf_Dyn_Iter DE = EF.end_dynamic_table();
943 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
946 return library_iterator(LibraryRef(toDRI(DI), this));
949 template <class ELFT>
950 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
951 LibraryRef &Result) const {
952 Elf_Dyn_Iter DI = toELFDynIter(Data);
953 Elf_Dyn_Iter DE = EF.end_dynamic_table();
955 // Skip to the next DT_NEEDED entry.
958 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
960 Result = LibraryRef(toDRI(DI), this);
961 return object_error::success;
964 template <class ELFT>
965 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
966 StringRef &Res) const {
967 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
968 return object_error::success;
971 template <class ELFT>
972 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
973 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
976 template <class ELFT>
977 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
978 return ELFT::Is64Bits ? 8 : 4;
981 template <class ELFT>
982 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
983 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
984 case ELF::ELFCLASS32:
985 switch (EF.getHeader()->e_machine) {
989 return "ELF32-x86-64";
992 case ELF::EM_HEXAGON:
993 return "ELF32-hexagon";
999 return "ELF32-unknown";
1001 case ELF::ELFCLASS64:
1002 switch (EF.getHeader()->e_machine) {
1004 return "ELF64-i386";
1005 case ELF::EM_X86_64:
1006 return "ELF64-x86-64";
1007 case ELF::EM_AARCH64:
1008 return "ELF64-aarch64";
1010 return "ELF64-ppc64";
1012 return "ELF64-s390";
1014 return "ELF64-unknown";
1017 // FIXME: Proper error handling.
1018 report_fatal_error("Invalid ELFCLASS!");
1022 template <class ELFT>
1023 unsigned ELFObjectFile<ELFT>::getArch() const {
1024 switch (EF.getHeader()->e_machine) {
1027 case ELF::EM_X86_64:
1028 return Triple::x86_64;
1029 case ELF::EM_AARCH64:
1030 return Triple::aarch64;
1033 case ELF::EM_HEXAGON:
1034 return Triple::hexagon;
1036 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
1039 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
1042 return Triple::systemz;
1044 return Triple::UnknownArch;
1048 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
1049 /// and make these member functions?
1050 static inline error_code getELFRelocationAddend(const RelocationRef R,
1052 const ObjectFile *Obj = R.getObjectFile();
1053 DataRefImpl DRI = R.getRawDataRefImpl();
1054 // Little-endian 32-bit
1055 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1056 return ELFObj->getRelocationAddend(DRI, Addend);
1058 // Big-endian 32-bit
1059 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1060 return ELFObj->getRelocationAddend(DRI, Addend);
1062 // Little-endian 64-bit
1063 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1064 return ELFObj->getRelocationAddend(DRI, Addend);
1066 // Big-endian 64-bit
1067 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1068 return ELFObj->getRelocationAddend(DRI, Addend);
1070 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1073 /// This is a generic interface for retrieving GNU symbol version
1074 /// information from an ELFObjectFile.
1075 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1076 const SymbolRef &Sym,
1079 // Little-endian 32-bit
1080 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1081 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1083 // Big-endian 32-bit
1084 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1085 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1087 // Little-endian 64-bit
1088 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1089 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1091 // Big-endian 64-bit
1092 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1093 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1095 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");