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"
38 class ELFObjectFile : public ObjectFile {
40 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
42 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
44 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
45 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
46 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
47 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
48 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
50 typedef typename ELFFile<ELFT>::Elf_Sym_Iter Elf_Sym_Iter;
51 typedef typename ELFFile<ELFT>::Elf_Shdr_Iter Elf_Shdr_Iter;
52 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
57 virtual error_code getSymbolNext(DataRefImpl Symb, SymbolRef &Res) const;
58 virtual error_code getSymbolName(DataRefImpl Symb, StringRef &Res) const;
59 virtual error_code getSymbolFileOffset(DataRefImpl Symb, uint64_t &Res) const;
60 virtual error_code getSymbolAddress(DataRefImpl Symb, uint64_t &Res) const;
61 virtual error_code getSymbolAlignment(DataRefImpl Symb, uint32_t &Res) const;
62 virtual error_code getSymbolSize(DataRefImpl Symb, uint64_t &Res) const;
63 virtual error_code getSymbolNMTypeChar(DataRefImpl Symb, char &Res) const;
64 virtual error_code getSymbolFlags(DataRefImpl Symb, uint32_t &Res) const;
65 virtual error_code getSymbolType(DataRefImpl Symb,
66 SymbolRef::Type &Res) const;
67 virtual error_code getSymbolSection(DataRefImpl Symb,
68 section_iterator &Res) const;
69 virtual error_code getSymbolValue(DataRefImpl Symb, uint64_t &Val) const;
71 virtual error_code getLibraryNext(DataRefImpl Data, LibraryRef &Result) const;
72 virtual error_code getLibraryPath(DataRefImpl Data, StringRef &Res) const;
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;
83 virtual error_code isSectionRequiredForExecution(DataRefImpl Sec,
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;
88 virtual error_code sectionContainsSymbol(DataRefImpl Sec, DataRefImpl Symb,
90 virtual relocation_iterator getSectionRelBegin(DataRefImpl Sec) const;
91 virtual relocation_iterator getSectionRelEnd(DataRefImpl Sec) const;
92 virtual section_iterator getRelocatedSection(DataRefImpl Sec) const;
94 virtual error_code getRelocationNext(DataRefImpl Rel,
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;
98 virtual symbol_iterator getRelocationSymbol(DataRefImpl Rel) const;
99 virtual error_code getRelocationType(DataRefImpl Rel, uint64_t &Res) const;
100 virtual error_code getRelocationTypeName(DataRefImpl Rel,
101 SmallVectorImpl<char> &Result) const;
103 getRelocationValueString(DataRefImpl Rel,
104 SmallVectorImpl<char> &Result) const;
106 protected: // ELF specific protected members.
107 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
108 uint64_t getROffset(DataRefImpl Rel) const;
109 StringRef getRelocationTypeName(uint32_t Type) const;
111 /// \brief Get the relocation section that contains \a Rel.
112 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
113 return EF.getSection(Rel.d.a);
116 const Elf_Rel *getRel(DataRefImpl Rel) const;
117 const Elf_Rela *getRela(DataRefImpl Rela) const;
119 Elf_Sym_Iter toELFSymIter(DataRefImpl Symb) const {
120 bool IsDynamic = Symb.p & 1;
123 EF.begin_dynamic_symbols().getEntSize(),
124 reinterpret_cast<const char *>(Symb.p & ~uintptr_t(1)), IsDynamic);
125 return Elf_Sym_Iter(EF.begin_symbols().getEntSize(),
126 reinterpret_cast<const char *>(Symb.p), IsDynamic);
129 DataRefImpl toDRI(Elf_Sym_Iter Symb) const {
131 DRI.p = reinterpret_cast<uintptr_t>(Symb.get()) |
132 static_cast<uintptr_t>(Symb.isDynamic());
136 Elf_Shdr_Iter toELFShdrIter(DataRefImpl Sec) const {
137 return Elf_Shdr_Iter(EF.getHeader()->e_shentsize,
138 reinterpret_cast<const char *>(Sec.p));
141 DataRefImpl toDRI(Elf_Shdr_Iter Sec) const {
143 DRI.p = reinterpret_cast<uintptr_t>(Sec.get());
147 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
149 DRI.p = reinterpret_cast<uintptr_t>(Sec);
153 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
154 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
155 reinterpret_cast<const char *>(Dyn.p));
158 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
160 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
164 // This flag is used for classof, to distinguish ELFObjectFile from
165 // its subclass. If more subclasses will be created, this flag will
166 // have to become an enum.
167 bool isDyldELFObject;
170 ELFObjectFile(MemoryBuffer *Object, error_code &ec);
172 virtual symbol_iterator begin_symbols() const;
173 virtual symbol_iterator end_symbols() const;
175 virtual symbol_iterator begin_dynamic_symbols() const;
176 virtual symbol_iterator end_dynamic_symbols() const;
178 virtual section_iterator begin_sections() const;
179 virtual section_iterator end_sections() const;
181 virtual library_iterator begin_libraries_needed() const;
182 virtual library_iterator end_libraries_needed() const;
184 error_code getRelocationAddend(DataRefImpl Rel, int64_t &Res) const;
185 error_code getSymbolVersion(SymbolRef Symb, StringRef &Version,
186 bool &IsDefault) const;
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;
194 const ELFFile<ELFT> *getELFFile() const { return &EF; }
196 bool isDyldType() const { return isDyldELFObject; }
197 static inline bool classof(const Binary *v) {
198 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
203 // Use an alignment of 2 for the typedefs since that is the worst case for
204 // ELF files in archives.
205 typedef ELFObjectFile<ELFType<support::little, 2, false> > ELF32LEObjectFile;
206 typedef ELFObjectFile<ELFType<support::little, 2, true> > ELF64LEObjectFile;
207 typedef ELFObjectFile<ELFType<support::big, 2, false> > ELF32BEObjectFile;
208 typedef ELFObjectFile<ELFType<support::big, 2, true> > ELF64BEObjectFile;
210 template <class ELFT>
211 error_code ELFObjectFile<ELFT>::getSymbolNext(DataRefImpl Symb,
212 SymbolRef &Result) const {
213 Result = SymbolRef(toDRI(++toELFSymIter(Symb)), this);
214 return object_error::success;
217 template <class ELFT>
218 error_code ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Symb,
219 StringRef &Result) const {
220 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
224 return object_error::success;
227 template <class ELFT>
228 error_code ELFObjectFile<ELFT>::getSymbolVersion(SymbolRef SymRef,
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);
238 return object_error::success;
241 template <class ELFT>
242 error_code ELFObjectFile<ELFT>::getSymbolFileOffset(DataRefImpl Symb,
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
250 Result = UnknownAddressOrSize;
251 return object_error::success;
253 Result = ESym->st_value;
254 return object_error::success;
256 ESec = EF.getSection(ESym);
259 switch (ESym->getType()) {
260 case ELF::STT_SECTION:
261 Result = ESec ? ESec->sh_offset : UnknownAddressOrSize;
262 return object_error::success;
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;
269 Result = UnknownAddressOrSize;
270 return object_error::success;
274 template <class ELFT>
275 error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
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:
282 Result = UnknownAddressOrSize;
283 return object_error::success;
285 Result = ESym->st_value;
286 return object_error::success;
288 ESec = EF.getSection(ESym);
291 switch (ESym->getType()) {
292 case ELF::STT_SECTION:
293 Result = ESec ? ESec->sh_addr : UnknownAddressOrSize;
294 return object_error::success;
296 case ELF::STT_OBJECT:
297 case ELF::STT_NOTYPE:
299 switch (EF.getHeader()->e_type) {
302 IsRelocatable = false;
305 IsRelocatable = true;
307 Result = ESym->st_value;
309 // Clear the ARM/Thumb indicator flag.
310 if (EF.getHeader()->e_machine == ELF::EM_ARM)
313 if (IsRelocatable && ESec != 0)
314 Result += ESec->sh_addr;
315 return object_error::success;
317 Result = UnknownAddressOrSize;
318 return object_error::success;
322 template <class ELFT>
323 error_code ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb,
324 uint32_t &Res) const {
325 Elf_Sym_Iter Sym = toELFSymIter(Symb);
326 if (Sym->st_shndx == ELF::SHN_COMMON)
330 return object_error::success;
333 template <class ELFT>
334 error_code ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Symb,
335 uint64_t &Result) const {
336 Result = toELFSymIter(Symb)->st_size;
337 return object_error::success;
340 template <class ELFT>
341 error_code ELFObjectFile<ELFT>::getSymbolNMTypeChar(DataRefImpl Symb,
342 char &Result) const {
343 const Elf_Sym *ESym = getSymbol(Symb);
344 const Elf_Shdr *ESec = EF.getSection(ESym);
349 switch (ESec->sh_type) {
350 case ELF::SHT_PROGBITS:
351 case ELF::SHT_DYNAMIC:
352 switch (ESec->sh_flags) {
353 case (ELF::SHF_ALLOC | ELF::SHF_EXECINSTR):
356 case (ELF::SHF_ALLOC | ELF::SHF_WRITE):
360 case (ELF::SHF_ALLOC | ELF::SHF_MERGE):
361 case (ELF::SHF_ALLOC | ELF::SHF_MERGE | ELF::SHF_STRINGS):
366 case ELF::SHT_NOBITS:
371 switch (EF.getSymbolTableIndex(ESym)) {
379 case ELF::SHN_COMMON:
384 switch (ESym->getBinding()) {
385 case ELF::STB_GLOBAL:
386 ret = ::toupper(ret);
389 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
391 else if (ESym->getType() == ELF::STT_OBJECT)
397 if (ret == '?' && ESym->getType() == ELF::STT_SECTION) {
398 ErrorOr<StringRef> Name = EF.getSymbolName(toELFSymIter(Symb));
401 Result = StringSwitch<char>(*Name)
402 .StartsWith(".debug", 'N')
403 .StartsWith(".note", 'n')
405 return object_error::success;
409 return object_error::success;
412 template <class ELFT>
413 error_code ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb,
414 SymbolRef::Type &Result) const {
415 const Elf_Sym *ESym = getSymbol(Symb);
417 switch (ESym->getType()) {
418 case ELF::STT_NOTYPE:
419 Result = SymbolRef::ST_Unknown;
421 case ELF::STT_SECTION:
422 Result = SymbolRef::ST_Debug;
425 Result = SymbolRef::ST_File;
428 Result = SymbolRef::ST_Function;
430 case ELF::STT_OBJECT:
431 case ELF::STT_COMMON:
433 Result = SymbolRef::ST_Data;
436 Result = SymbolRef::ST_Other;
439 return object_error::success;
442 template <class ELFT>
443 error_code ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Symb,
444 uint32_t &Result) const {
445 const Elf_Sym *ESym = getSymbol(Symb);
447 Result = SymbolRef::SF_None;
449 if (ESym->getBinding() != ELF::STB_LOCAL)
450 Result |= SymbolRef::SF_Global;
452 if (ESym->getBinding() == ELF::STB_WEAK)
453 Result |= SymbolRef::SF_Weak;
455 if (ESym->st_shndx == ELF::SHN_ABS)
456 Result |= SymbolRef::SF_Absolute;
458 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
459 ESym == &*EF.begin_symbols())
460 Result |= SymbolRef::SF_FormatSpecific;
462 if (EF.getSymbolTableIndex(ESym) == ELF::SHN_UNDEF)
463 Result |= SymbolRef::SF_Undefined;
465 if (ESym->getType() == ELF::STT_COMMON ||
466 EF.getSymbolTableIndex(ESym) == ELF::SHN_COMMON)
467 Result |= SymbolRef::SF_Common;
469 if (ESym->getType() == ELF::STT_TLS)
470 Result |= SymbolRef::SF_ThreadLocal;
472 return object_error::success;
475 template <class ELFT>
476 error_code ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
477 section_iterator &Res) const {
478 const Elf_Sym *ESym = getSymbol(Symb);
479 const Elf_Shdr *ESec = EF.getSection(ESym);
481 Res = end_sections();
484 Sec.p = reinterpret_cast<intptr_t>(ESec);
485 Res = section_iterator(SectionRef(Sec, this));
487 return object_error::success;
490 template <class ELFT>
491 error_code ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb,
492 uint64_t &Val) const {
493 const Elf_Sym *ESym = getSymbol(Symb);
494 Val = ESym->st_value;
495 return object_error::success;
498 template <class ELFT>
499 error_code ELFObjectFile<ELFT>::getSectionNext(DataRefImpl Sec,
500 SectionRef &Result) const {
501 Result = SectionRef(toDRI(++toELFShdrIter(Sec)), this);
502 return object_error::success;
505 template <class ELFT>
506 error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
507 StringRef &Result) const {
508 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
512 return object_error::success;
515 template <class ELFT>
516 error_code ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec,
517 uint64_t &Result) const {
518 Result = toELFShdrIter(Sec)->sh_addr;
519 return object_error::success;
522 template <class ELFT>
523 error_code ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec,
524 uint64_t &Result) const {
525 Result = toELFShdrIter(Sec)->sh_size;
526 return object_error::success;
529 template <class ELFT>
530 error_code ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
531 StringRef &Result) const {
532 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
533 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
534 return object_error::success;
537 template <class ELFT>
538 error_code ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec,
539 uint64_t &Result) const {
540 Result = toELFShdrIter(Sec)->sh_addralign;
541 return object_error::success;
544 template <class ELFT>
545 error_code ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec,
546 bool &Result) const {
547 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
548 return object_error::success;
551 template <class ELFT>
552 error_code ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec,
553 bool &Result) const {
554 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
555 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
556 EShdr->sh_type == ELF::SHT_PROGBITS;
557 return object_error::success;
560 template <class ELFT>
561 error_code ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec,
562 bool &Result) const {
563 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
564 Result = EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
565 EShdr->sh_type == ELF::SHT_NOBITS;
566 return object_error::success;
569 template <class ELFT>
571 ELFObjectFile<ELFT>::isSectionRequiredForExecution(DataRefImpl Sec,
572 bool &Result) const {
573 Result = toELFShdrIter(Sec)->sh_flags & ELF::SHF_ALLOC;
574 return object_error::success;
577 template <class ELFT>
578 error_code ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec,
579 bool &Result) const {
580 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
581 return object_error::success;
584 template <class ELFT>
585 error_code ELFObjectFile<ELFT>::isSectionZeroInit(DataRefImpl Sec,
586 bool &Result) const {
587 Result = toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
588 return object_error::success;
591 template <class ELFT>
592 error_code ELFObjectFile<ELFT>::isSectionReadOnlyData(DataRefImpl Sec,
593 bool &Result) const {
594 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
595 Result = !(EShdr->sh_flags & (ELF::SHF_WRITE | ELF::SHF_EXECINSTR));
596 return object_error::success;
599 template <class ELFT>
600 error_code ELFObjectFile<ELFT>::sectionContainsSymbol(DataRefImpl Sec,
602 bool &Result) const {
603 Elf_Sym_Iter ESym = toELFSymIter(Symb);
605 uintX_t Index = ESym->st_shndx;
606 bool Reserved = Index >= ELF::SHN_LORESERVE && Index <= ELF::SHN_HIRESERVE;
608 Result = !Reserved && (&*toELFShdrIter(Sec) == EF.getSection(ESym->st_shndx));
609 return object_error::success;
612 template <class ELFT>
614 ELFObjectFile<ELFT>::getSectionRelBegin(DataRefImpl Sec) const {
616 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
617 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
619 return relocation_iterator(RelocationRef(RelData, this));
622 template <class ELFT>
624 ELFObjectFile<ELFT>::getSectionRelEnd(DataRefImpl Sec) const {
626 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.begin_sections().get());
627 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
628 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
629 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
632 RelData.d.b = S->sh_size / S->sh_entsize;
634 return relocation_iterator(RelocationRef(RelData, this));
637 template <class ELFT>
639 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
640 if (EF.getHeader()->e_type != ELF::ET_REL)
641 return end_sections();
643 Elf_Shdr_Iter EShdr = toELFShdrIter(Sec);
644 uintX_t Type = EShdr->sh_type;
645 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
646 return end_sections();
648 const Elf_Shdr *R = EF.getSection(EShdr->sh_info);
649 return section_iterator(SectionRef(toDRI(R), this));
653 template <class ELFT>
654 error_code ELFObjectFile<ELFT>::getRelocationNext(DataRefImpl Rel,
655 RelocationRef &Result) const {
657 Result = RelocationRef(Rel, this);
658 return object_error::success;
661 template <class ELFT>
663 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
665 const Elf_Shdr *sec = getRelSection(Rel);
666 switch (sec->sh_type) {
668 report_fatal_error("Invalid section type in Rel!");
670 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
673 case ELF::SHT_RELA: {
674 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
679 return end_symbols();
681 const Elf_Shdr *SymSec = EF.getSection(sec->sh_link);
683 DataRefImpl SymbolData;
684 switch (SymSec->sh_type) {
686 report_fatal_error("Invalid symbol table section type!");
687 case ELF::SHT_SYMTAB:
688 SymbolData = toDRI(EF.begin_symbols() + symbolIdx);
690 case ELF::SHT_DYNSYM:
691 SymbolData = toDRI(EF.begin_dynamic_symbols() + symbolIdx);
695 return symbol_iterator(SymbolRef(SymbolData, this));
698 template <class ELFT>
699 error_code ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel,
700 uint64_t &Result) const {
701 Result = getROffset(Rel);
702 return object_error::success;
705 template <class ELFT>
706 error_code ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel,
707 uint64_t &Result) const {
708 Result = getROffset(Rel);
709 return object_error::success;
712 template <class ELFT>
713 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
714 const Elf_Shdr *sec = getRelSection(Rel);
715 switch (sec->sh_type) {
717 report_fatal_error("Invalid section type in Rel!");
719 return getRel(Rel)->r_offset;
721 return getRela(Rel)->r_offset;
725 template <class ELFT>
726 error_code ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel,
727 uint64_t &Result) const {
728 const Elf_Shdr *sec = getRelSection(Rel);
729 switch (sec->sh_type) {
731 report_fatal_error("Invalid section type in Rel!");
733 Result = getRel(Rel)->getType(EF.isMips64EL());
736 case ELF::SHT_RELA: {
737 Result = getRela(Rel)->getType(EF.isMips64EL());
741 return object_error::success;
744 template <class ELFT>
745 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
746 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
749 template <class ELFT>
750 error_code ELFObjectFile<ELFT>::getRelocationTypeName(
751 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
752 const Elf_Shdr *sec = getRelSection(Rel);
754 switch (sec->sh_type) {
756 return object_error::parse_failed;
758 type = getRel(Rel)->getType(EF.isMips64EL());
761 case ELF::SHT_RELA: {
762 type = getRela(Rel)->getType(EF.isMips64EL());
767 EF.getRelocationTypeName(type, Result);
768 return object_error::success;
771 template <class ELFT>
772 error_code ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel,
773 int64_t &Result) const {
774 const Elf_Shdr *sec = getRelSection(Rel);
775 switch (sec->sh_type) {
777 report_fatal_error("Invalid section type in Rel!");
780 return object_error::success;
782 case ELF::SHT_RELA: {
783 Result = getRela(Rel)->r_addend;
784 return object_error::success;
789 template <class ELFT>
790 error_code ELFObjectFile<ELFT>::getRelocationValueString(
791 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
792 const Elf_Shdr *sec = getRelSection(Rel);
796 uint16_t symbol_index = 0;
797 switch (sec->sh_type) {
799 return object_error::parse_failed;
801 type = getRel(Rel)->getType(EF.isMips64EL());
802 symbol_index = getRel(Rel)->getSymbol(EF.isMips64EL());
803 // TODO: Read implicit addend from section data.
806 case ELF::SHT_RELA: {
807 type = getRela(Rel)->getType(EF.isMips64EL());
808 symbol_index = getRela(Rel)->getSymbol(EF.isMips64EL());
809 addend = getRela(Rel)->r_addend;
813 const Elf_Sym *symb =
814 EF.template getEntry<Elf_Sym>(sec->sh_link, symbol_index);
815 ErrorOr<StringRef> SymName =
816 EF.getSymbolName(EF.getSection(sec->sh_link), symb);
819 switch (EF.getHeader()->e_machine) {
822 case ELF::R_X86_64_PC8:
823 case ELF::R_X86_64_PC16:
824 case ELF::R_X86_64_PC32: {
826 raw_string_ostream fmt(fmtbuf);
827 fmt << *SymName << (addend < 0 ? "" : "+") << addend << "-P";
829 Result.append(fmtbuf.begin(), fmtbuf.end());
831 case ELF::R_X86_64_8:
832 case ELF::R_X86_64_16:
833 case ELF::R_X86_64_32:
834 case ELF::R_X86_64_32S:
835 case ELF::R_X86_64_64: {
837 raw_string_ostream fmt(fmtbuf);
838 fmt << *SymName << (addend < 0 ? "" : "+") << addend;
840 Result.append(fmtbuf.begin(), fmtbuf.end());
846 case ELF::EM_AARCH64: {
848 raw_string_ostream fmt(fmtbuf);
851 fmt << (addend < 0 ? "" : "+") << addend;
853 Result.append(fmtbuf.begin(), fmtbuf.end());
857 case ELF::EM_HEXAGON:
864 Result.append(res.begin(), res.end());
865 return object_error::success;
868 template <class ELFT>
869 const typename ELFFile<ELFT>::Elf_Sym *
870 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
871 return &*toELFSymIter(Symb);
874 template <class ELFT>
875 const typename ELFObjectFile<ELFT>::Elf_Rel *
876 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
877 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b);
880 template <class ELFT>
881 const typename ELFObjectFile<ELFT>::Elf_Rela *
882 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
883 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b);
886 template <class ELFT>
887 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBuffer *Object, error_code &ec)
888 : ObjectFile(getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
894 template <class ELFT>
895 symbol_iterator ELFObjectFile<ELFT>::begin_symbols() const {
896 return symbol_iterator(SymbolRef(toDRI(EF.begin_symbols()), this));
899 template <class ELFT>
900 symbol_iterator ELFObjectFile<ELFT>::end_symbols() const {
901 return symbol_iterator(SymbolRef(toDRI(EF.end_symbols()), this));
904 template <class ELFT>
905 symbol_iterator ELFObjectFile<ELFT>::begin_dynamic_symbols() const {
906 return symbol_iterator(SymbolRef(toDRI(EF.begin_dynamic_symbols()), this));
909 template <class ELFT>
910 symbol_iterator ELFObjectFile<ELFT>::end_dynamic_symbols() const {
911 return symbol_iterator(SymbolRef(toDRI(EF.end_dynamic_symbols()), this));
914 template <class ELFT>
915 section_iterator ELFObjectFile<ELFT>::begin_sections() const {
916 return section_iterator(SectionRef(toDRI(EF.begin_sections()), this));
919 template <class ELFT>
920 section_iterator ELFObjectFile<ELFT>::end_sections() const {
921 return section_iterator(SectionRef(toDRI(EF.end_sections()), this));
924 template <class ELFT>
925 StringRef ELFObjectFile<ELFT>::getLoadName() const {
926 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
927 Elf_Dyn_Iter DE = EF.end_dynamic_table();
929 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
933 return EF.getDynamicString(DI->getVal());
937 template <class ELFT>
938 library_iterator ELFObjectFile<ELFT>::begin_libraries_needed() const {
939 Elf_Dyn_Iter DI = EF.begin_dynamic_table();
940 Elf_Dyn_Iter DE = EF.end_dynamic_table();
942 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
945 return library_iterator(LibraryRef(toDRI(DI), this));
948 template <class ELFT>
949 error_code ELFObjectFile<ELFT>::getLibraryNext(DataRefImpl Data,
950 LibraryRef &Result) const {
951 Elf_Dyn_Iter DI = toELFDynIter(Data);
952 Elf_Dyn_Iter DE = EF.end_dynamic_table();
954 // Skip to the next DT_NEEDED entry.
957 while (DI != DE && DI->getTag() != ELF::DT_NEEDED);
959 Result = LibraryRef(toDRI(DI), this);
960 return object_error::success;
963 template <class ELFT>
964 error_code ELFObjectFile<ELFT>::getLibraryPath(DataRefImpl Data,
965 StringRef &Res) const {
966 Res = EF.getDynamicString(toELFDynIter(Data)->getVal());
967 return object_error::success;
970 template <class ELFT>
971 library_iterator ELFObjectFile<ELFT>::end_libraries_needed() const {
972 return library_iterator(LibraryRef(toDRI(EF.end_dynamic_table()), this));
975 template <class ELFT>
976 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
977 return ELFT::Is64Bits ? 8 : 4;
980 template <class ELFT>
981 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
982 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
983 case ELF::ELFCLASS32:
984 switch (EF.getHeader()->e_machine) {
988 return "ELF32-x86-64";
991 case ELF::EM_HEXAGON:
992 return "ELF32-hexagon";
998 return "ELF32-unknown";
1000 case ELF::ELFCLASS64:
1001 switch (EF.getHeader()->e_machine) {
1003 return "ELF64-i386";
1004 case ELF::EM_X86_64:
1005 return "ELF64-x86-64";
1006 case ELF::EM_AARCH64:
1007 return "ELF64-aarch64";
1009 return "ELF64-ppc64";
1011 return "ELF64-s390";
1013 return "ELF64-unknown";
1016 // FIXME: Proper error handling.
1017 report_fatal_error("Invalid ELFCLASS!");
1021 template <class ELFT>
1022 unsigned ELFObjectFile<ELFT>::getArch() const {
1023 switch (EF.getHeader()->e_machine) {
1026 case ELF::EM_X86_64:
1027 return Triple::x86_64;
1028 case ELF::EM_AARCH64:
1029 return Triple::aarch64;
1032 case ELF::EM_HEXAGON:
1033 return Triple::hexagon;
1035 return (ELFT::TargetEndianness == support::little) ? Triple::mipsel
1038 return (ELFT::TargetEndianness == support::little) ? Triple::ppc64le
1041 return Triple::systemz;
1043 return Triple::UnknownArch;
1047 /// FIXME: Maybe we should have a base ElfObjectFile that is not a template
1048 /// and make these member functions?
1049 static inline error_code getELFRelocationAddend(const RelocationRef R,
1051 const ObjectFile *Obj = R.getObjectFile();
1052 DataRefImpl DRI = R.getRawDataRefImpl();
1053 // Little-endian 32-bit
1054 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1055 return ELFObj->getRelocationAddend(DRI, Addend);
1057 // Big-endian 32-bit
1058 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1059 return ELFObj->getRelocationAddend(DRI, Addend);
1061 // Little-endian 64-bit
1062 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1063 return ELFObj->getRelocationAddend(DRI, Addend);
1065 // Big-endian 64-bit
1066 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1067 return ELFObj->getRelocationAddend(DRI, Addend);
1069 llvm_unreachable("Object passed to getELFRelocationAddend() is not ELF");
1072 /// This is a generic interface for retrieving GNU symbol version
1073 /// information from an ELFObjectFile.
1074 static inline error_code GetELFSymbolVersion(const ObjectFile *Obj,
1075 const SymbolRef &Sym,
1078 // Little-endian 32-bit
1079 if (const ELF32LEObjectFile *ELFObj = dyn_cast<ELF32LEObjectFile>(Obj))
1080 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1082 // Big-endian 32-bit
1083 if (const ELF32BEObjectFile *ELFObj = dyn_cast<ELF32BEObjectFile>(Obj))
1084 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1086 // Little-endian 64-bit
1087 if (const ELF64LEObjectFile *ELFObj = dyn_cast<ELF64LEObjectFile>(Obj))
1088 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1090 // Big-endian 64-bit
1091 if (const ELF64BEObjectFile *ELFObj = dyn_cast<ELF64BEObjectFile>(Obj))
1092 return ELFObj->getSymbolVersion(Sym, Version, IsDefault);
1094 llvm_unreachable("Object passed to GetELFSymbolVersion() is not ELF");