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_ELFOBJECTFILE_H
15 #define LLVM_OBJECT_ELFOBJECTFILE_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 elf_symbol_iterator;
40 class ELFRelocationRef;
42 class ELFObjectFileBase : public ObjectFile {
43 friend class ELFSymbolRef;
44 friend class ELFSectionRef;
45 friend class ELFRelocationRef;
48 ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source);
50 virtual uint64_t getSymbolSize(DataRefImpl Symb) const = 0;
51 virtual uint8_t getSymbolOther(DataRefImpl Symb) const = 0;
52 virtual uint8_t getSymbolELFType(DataRefImpl Symb) const = 0;
54 virtual uint32_t getSectionType(DataRefImpl Sec) const = 0;
55 virtual uint64_t getSectionFlags(DataRefImpl Sec) const = 0;
57 virtual ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const = 0;
60 typedef iterator_range<elf_symbol_iterator> elf_symbol_iterator_range;
61 virtual elf_symbol_iterator_range getDynamicSymbolIterators() const = 0;
63 elf_symbol_iterator_range symbols() const;
65 static inline bool classof(const Binary *v) { return v->isELF(); }
68 class ELFSectionRef : public SectionRef {
70 ELFSectionRef(const SectionRef &B) : SectionRef(B) {
71 assert(isa<ELFObjectFileBase>(SectionRef::getObject()));
74 const ELFObjectFileBase *getObject() const {
75 return cast<ELFObjectFileBase>(SectionRef::getObject());
78 uint32_t getType() const {
79 return getObject()->getSectionType(getRawDataRefImpl());
82 uint64_t getFlags() const {
83 return getObject()->getSectionFlags(getRawDataRefImpl());
87 class elf_section_iterator : public section_iterator {
89 elf_section_iterator(const section_iterator &B) : section_iterator(B) {
90 assert(isa<ELFObjectFileBase>(B->getObject()));
93 const ELFSectionRef *operator->() const {
94 return static_cast<const ELFSectionRef *>(section_iterator::operator->());
97 const ELFSectionRef &operator*() const {
98 return static_cast<const ELFSectionRef &>(section_iterator::operator*());
102 class ELFSymbolRef : public SymbolRef {
104 ELFSymbolRef(const SymbolRef &B) : SymbolRef(B) {
105 assert(isa<ELFObjectFileBase>(SymbolRef::getObject()));
108 const ELFObjectFileBase *getObject() const {
109 return cast<ELFObjectFileBase>(BasicSymbolRef::getObject());
112 uint64_t getSize() const {
113 return getObject()->getSymbolSize(getRawDataRefImpl());
116 uint8_t getOther() const {
117 return getObject()->getSymbolOther(getRawDataRefImpl());
120 uint8_t getELFType() const {
121 return getObject()->getSymbolELFType(getRawDataRefImpl());
125 class elf_symbol_iterator : public symbol_iterator {
127 elf_symbol_iterator(const basic_symbol_iterator &B)
128 : symbol_iterator(SymbolRef(B->getRawDataRefImpl(),
129 cast<ELFObjectFileBase>(B->getObject()))) {}
131 const ELFSymbolRef *operator->() const {
132 return static_cast<const ELFSymbolRef *>(symbol_iterator::operator->());
135 const ELFSymbolRef &operator*() const {
136 return static_cast<const ELFSymbolRef &>(symbol_iterator::operator*());
140 class ELFRelocationRef : public RelocationRef {
142 ELFRelocationRef(const RelocationRef &B) : RelocationRef(B) {
143 assert(isa<ELFObjectFileBase>(RelocationRef::getObject()));
146 const ELFObjectFileBase *getObject() const {
147 return cast<ELFObjectFileBase>(RelocationRef::getObject());
150 ErrorOr<int64_t> getAddend() const {
151 return getObject()->getRelocationAddend(getRawDataRefImpl());
155 class elf_relocation_iterator : public relocation_iterator {
157 elf_relocation_iterator(const relocation_iterator &B)
158 : relocation_iterator(RelocationRef(
159 B->getRawDataRefImpl(), cast<ELFObjectFileBase>(B->getObject()))) {}
161 const ELFRelocationRef *operator->() const {
162 return static_cast<const ELFRelocationRef *>(
163 relocation_iterator::operator->());
166 const ELFRelocationRef &operator*() const {
167 return static_cast<const ELFRelocationRef &>(
168 relocation_iterator::operator*());
172 inline ELFObjectFileBase::elf_symbol_iterator_range
173 ELFObjectFileBase::symbols() const {
174 return elf_symbol_iterator_range(symbol_begin(), symbol_end());
177 template <class ELFT> class ELFObjectFile : public ELFObjectFileBase {
178 uint64_t getSymbolSize(DataRefImpl Sym) const override;
181 LLVM_ELF_IMPORT_TYPES_ELFT(ELFT)
183 typedef typename ELFFile<ELFT>::uintX_t uintX_t;
185 typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
186 typedef typename ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
187 typedef typename ELFFile<ELFT>::Elf_Ehdr Elf_Ehdr;
188 typedef typename ELFFile<ELFT>::Elf_Rel Elf_Rel;
189 typedef typename ELFFile<ELFT>::Elf_Rela Elf_Rela;
190 typedef typename ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
192 typedef typename ELFFile<ELFT>::Elf_Dyn_Iter Elf_Dyn_Iter;
197 void moveSymbolNext(DataRefImpl &Symb) const override;
198 ErrorOr<StringRef> getSymbolName(DataRefImpl Symb) const override;
199 std::error_code getSymbolAddress(DataRefImpl Symb,
200 uint64_t &Res) const override;
201 uint64_t getSymbolValue(DataRefImpl Symb) const override;
202 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
203 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
204 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
205 uint8_t getSymbolOther(DataRefImpl Symb) const override;
206 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
207 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
208 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
209 std::error_code getSymbolSection(DataRefImpl Symb,
210 section_iterator &Res) const override;
212 void moveSectionNext(DataRefImpl &Sec) const override;
213 std::error_code getSectionName(DataRefImpl Sec,
214 StringRef &Res) const override;
215 uint64_t getSectionAddress(DataRefImpl Sec) const override;
216 uint64_t getSectionSize(DataRefImpl Sec) const override;
217 std::error_code getSectionContents(DataRefImpl Sec,
218 StringRef &Res) const override;
219 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
220 bool isSectionText(DataRefImpl Sec) const override;
221 bool isSectionData(DataRefImpl Sec) const override;
222 bool isSectionBSS(DataRefImpl Sec) const override;
223 bool isSectionVirtual(DataRefImpl Sec) const override;
224 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
225 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
226 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
228 void moveRelocationNext(DataRefImpl &Rel) const override;
229 ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const override;
230 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
231 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
232 uint64_t getRelocationType(DataRefImpl Rel) const override;
233 void getRelocationTypeName(DataRefImpl Rel,
234 SmallVectorImpl<char> &Result) const override;
236 uint32_t getSectionType(DataRefImpl Sec) const override;
237 uint64_t getSectionFlags(DataRefImpl Sec) const override;
238 uint64_t getROffset(DataRefImpl Rel) const;
239 StringRef getRelocationTypeName(uint32_t Type) const;
241 /// \brief Get the relocation section that contains \a Rel.
242 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
243 return *EF.getSection(Rel.d.a);
246 const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
247 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
250 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
257 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
258 SymTable->sh_type == ELF::SHT_DYNSYM);
260 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
261 unsigned SymTableIndex =
262 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
264 DRI.d.a = SymTableIndex;
269 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
270 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
273 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
275 DRI.p = reinterpret_cast<uintptr_t>(Sec);
279 Elf_Dyn_Iter toELFDynIter(DataRefImpl Dyn) const {
280 return Elf_Dyn_Iter(EF.begin_dynamic_table().getEntSize(),
281 reinterpret_cast<const char *>(Dyn.p));
284 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
286 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
290 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
291 unsigned char Binding = ESym->getBinding();
292 unsigned char Visibility = ESym->getVisibility();
294 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
295 // visibility is either DEFAULT or PROTECTED. All other symbols are not
297 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
298 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
304 // This flag is used for classof, to distinguish ELFObjectFile from
305 // its subclass. If more subclasses will be created, this flag will
306 // have to become an enum.
307 bool isDyldELFObject;
310 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
312 const Elf_Rel *getRel(DataRefImpl Rel) const;
313 const Elf_Rela *getRela(DataRefImpl Rela) const;
315 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
317 basic_symbol_iterator symbol_begin_impl() const override;
318 basic_symbol_iterator symbol_end_impl() const override;
320 elf_symbol_iterator dynamic_symbol_begin() const;
321 elf_symbol_iterator dynamic_symbol_end() const;
323 section_iterator section_begin() const override;
324 section_iterator section_end() const override;
326 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
328 uint8_t getBytesInAddress() const override;
329 StringRef getFileFormatName() const override;
330 unsigned getArch() const override;
331 StringRef getLoadName() const;
333 std::error_code getPlatformFlags(unsigned &Result) const override {
334 Result = EF.getHeader()->e_flags;
335 return std::error_code();
338 const ELFFile<ELFT> *getELFFile() const { return &EF; }
340 bool isDyldType() const { return isDyldELFObject; }
341 static inline bool classof(const Binary *v) {
342 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
346 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
348 bool isRelocatableObject() const override;
351 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
352 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
353 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
354 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
356 template <class ELFT>
357 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
361 template <class ELFT>
362 ErrorOr<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
363 const Elf_Sym *ESym = toELFSymIter(Sym);
364 const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
365 const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
366 StringRef SymTable = *EF.getStringTable(StringTableSec);
367 return ESym->getName(SymTable);
370 template <class ELFT>
371 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
372 return toELFShdrIter(Sec)->sh_flags;
375 template <class ELFT>
376 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
377 return toELFShdrIter(Sec)->sh_type;
380 template <class ELFT>
381 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
382 const Elf_Sym *ESym = getSymbol(Symb);
383 switch (ESym->st_shndx) {
384 case ELF::SHN_COMMON:
386 return UnknownAddress;
388 return ESym->st_value;
391 const Elf_Ehdr *Header = EF.getHeader();
392 uint64_t Ret = ESym->st_value;
394 // Clear the ARM/Thumb or microMIPS indicator flag.
395 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
396 ESym->getType() == ELF::STT_FUNC)
402 template <class ELFT>
403 std::error_code ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb,
404 uint64_t &Result) const {
405 Result = getSymbolValue(Symb);
406 const Elf_Sym *ESym = getSymbol(Symb);
407 switch (ESym->st_shndx) {
408 case ELF::SHN_COMMON:
411 return std::error_code();
414 const Elf_Ehdr *Header = EF.getHeader();
416 if (Header->e_type == ELF::ET_REL) {
417 ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
418 if (std::error_code EC = SectionOrErr.getError())
420 const Elf_Shdr *Section = *SectionOrErr;
422 Result += Section->sh_addr;
425 return std::error_code();
428 template <class ELFT>
429 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
430 const Elf_Sym *Sym = toELFSymIter(Symb);
431 if (Sym->st_shndx == ELF::SHN_COMMON)
432 return Sym->st_value;
436 template <class ELFT>
437 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
438 return toELFSymIter(Sym)->st_size;
441 template <class ELFT>
442 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
443 return toELFSymIter(Symb)->st_size;
446 template <class ELFT>
447 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
448 return toELFSymIter(Symb)->st_other;
451 template <class ELFT>
452 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
453 return toELFSymIter(Symb)->getType();
456 template <class ELFT>
457 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
458 const Elf_Sym *ESym = getSymbol(Symb);
460 switch (ESym->getType()) {
461 case ELF::STT_NOTYPE:
462 return SymbolRef::ST_Unknown;
463 case ELF::STT_SECTION:
464 return SymbolRef::ST_Debug;
466 return SymbolRef::ST_File;
468 return SymbolRef::ST_Function;
469 case ELF::STT_OBJECT:
470 case ELF::STT_COMMON:
472 return SymbolRef::ST_Data;
474 return SymbolRef::ST_Other;
478 template <class ELFT>
479 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
480 const Elf_Sym *ESym = toELFSymIter(Sym);
482 uint32_t Result = SymbolRef::SF_None;
484 if (ESym->getBinding() != ELF::STB_LOCAL)
485 Result |= SymbolRef::SF_Global;
487 if (ESym->getBinding() == ELF::STB_WEAK)
488 Result |= SymbolRef::SF_Weak;
490 if (ESym->st_shndx == ELF::SHN_ABS)
491 Result |= SymbolRef::SF_Absolute;
493 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
494 ESym == EF.symbol_begin() || ESym == EF.dynamic_symbol_begin())
495 Result |= SymbolRef::SF_FormatSpecific;
497 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
498 if (ErrorOr<StringRef> NameOrErr = getSymbolName(Sym)) {
499 StringRef Name = *NameOrErr;
500 if (Name.startswith("$d") || Name.startswith("$t") ||
501 Name.startswith("$a"))
502 Result |= SymbolRef::SF_FormatSpecific;
506 if (ESym->st_shndx == ELF::SHN_UNDEF)
507 Result |= SymbolRef::SF_Undefined;
509 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
510 Result |= SymbolRef::SF_Common;
512 if (isExportedToOtherDSO(ESym))
513 Result |= SymbolRef::SF_Exported;
515 if (ESym->getVisibility() == ELF::STV_HIDDEN)
516 Result |= SymbolRef::SF_Hidden;
521 template <class ELFT>
523 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
524 ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
525 if (std::error_code EC = ESecOrErr.getError())
526 report_fatal_error(EC.message());
528 const Elf_Shdr *ESec = *ESecOrErr;
530 return section_end();
533 Sec.p = reinterpret_cast<intptr_t>(ESec);
534 return section_iterator(SectionRef(Sec, this));
537 template <class ELFT>
539 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
540 section_iterator &Res) const {
541 Res = getSymbolSection(getSymbol(Symb));
542 return std::error_code();
545 template <class ELFT>
546 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
547 const Elf_Shdr *ESec = toELFShdrIter(Sec);
551 template <class ELFT>
552 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
553 StringRef &Result) const {
554 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
556 return Name.getError();
558 return std::error_code();
561 template <class ELFT>
562 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
563 return toELFShdrIter(Sec)->sh_addr;
566 template <class ELFT>
567 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
568 return toELFShdrIter(Sec)->sh_size;
571 template <class ELFT>
573 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
574 StringRef &Result) const {
575 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
576 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
577 return std::error_code();
580 template <class ELFT>
581 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
582 return toELFShdrIter(Sec)->sh_addralign;
585 template <class ELFT>
586 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
587 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
590 template <class ELFT>
591 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
592 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
593 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
594 EShdr->sh_type == ELF::SHT_PROGBITS;
597 template <class ELFT>
598 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
599 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
600 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
601 EShdr->sh_type == ELF::SHT_NOBITS;
604 template <class ELFT>
605 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
606 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
609 template <class ELFT>
611 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
613 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
614 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
617 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
618 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
619 return relocation_iterator(RelocationRef(RelData, this));
621 const Elf_Shdr *RelSec = getRelSection(RelData);
622 ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
623 if (std::error_code EC = SymSecOrErr.getError())
624 report_fatal_error(EC.message());
625 const Elf_Shdr *SymSec = *SymSecOrErr;
626 uint32_t SymSecType = SymSec->sh_type;
627 if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
628 report_fatal_error("Invalid symbol table section type!");
629 if (SymSecType == ELF::SHT_DYNSYM)
632 return relocation_iterator(RelocationRef(RelData, this));
635 template <class ELFT>
637 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
638 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
639 relocation_iterator Begin = section_rel_begin(Sec);
640 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
642 DataRefImpl RelData = Begin->getRawDataRefImpl();
643 RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
644 return relocation_iterator(RelocationRef(RelData, this));
647 template <class ELFT>
649 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
650 if (EF.getHeader()->e_type != ELF::ET_REL)
651 return section_end();
653 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
654 uintX_t Type = EShdr->sh_type;
655 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
656 return section_end();
658 ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
659 if (std::error_code EC = R.getError())
660 report_fatal_error(EC.message());
661 return section_iterator(SectionRef(toDRI(*R), this));
665 template <class ELFT>
666 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
670 template <class ELFT>
672 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
674 const Elf_Shdr *sec = getRelSection(Rel);
675 if (sec->sh_type == ELF::SHT_REL)
676 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
678 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
682 bool IsDyn = Rel.d.b & 1;
683 DataRefImpl SymbolData;
685 SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx);
687 SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
688 return symbol_iterator(SymbolRef(SymbolData, this));
691 template <class ELFT>
693 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel) const {
694 uint64_t ROffset = getROffset(Rel);
695 const Elf_Ehdr *Header = EF.getHeader();
697 if (Header->e_type == ELF::ET_REL) {
698 const Elf_Shdr *RelocationSec = getRelSection(Rel);
699 ErrorOr<const Elf_Shdr *> RelocatedSec =
700 EF.getSection(RelocationSec->sh_info);
701 if (std::error_code EC = RelocatedSec.getError())
703 return ROffset + (*RelocatedSec)->sh_addr;
708 template <class ELFT>
709 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
710 assert(EF.getHeader()->e_type == ELF::ET_REL &&
711 "Only relocatable object files have relocation offsets");
712 return getROffset(Rel);
715 template <class ELFT>
716 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
717 const Elf_Shdr *sec = getRelSection(Rel);
718 if (sec->sh_type == ELF::SHT_REL)
719 return getRel(Rel)->r_offset;
721 return getRela(Rel)->r_offset;
724 template <class ELFT>
725 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
726 const Elf_Shdr *sec = getRelSection(Rel);
727 if (sec->sh_type == ELF::SHT_REL)
728 return getRel(Rel)->getType(EF.isMips64EL());
730 return getRela(Rel)->getType(EF.isMips64EL());
733 template <class ELFT>
734 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
735 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
738 template <class ELFT>
739 void ELFObjectFile<ELFT>::getRelocationTypeName(
740 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
741 uint32_t type = getRelocationType(Rel);
742 EF.getRelocationTypeName(type, Result);
745 template <class ELFT>
747 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
748 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
749 return object_error::parse_failed;
750 return (int64_t)getRela(Rel)->r_addend;
753 template <class ELFT>
754 const typename ELFFile<ELFT>::Elf_Sym *
755 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
756 return &*toELFSymIter(Symb);
759 template <class ELFT>
760 const typename ELFObjectFile<ELFT>::Elf_Rel *
761 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
762 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
763 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
766 template <class ELFT>
767 const typename ELFObjectFile<ELFT>::Elf_Rela *
768 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
769 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
770 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
773 template <class ELFT>
774 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
776 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
780 EF(Data.getBuffer(), EC) {}
782 template <class ELFT>
783 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
784 DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0);
785 return basic_symbol_iterator(SymbolRef(Sym, this));
788 template <class ELFT>
789 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
790 const Elf_Shdr *SymTab = EF.getDotSymtabSec();
792 return symbol_begin_impl();
793 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
794 return basic_symbol_iterator(SymbolRef(Sym, this));
797 template <class ELFT>
798 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
799 DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0);
800 return symbol_iterator(SymbolRef(Sym, this));
803 template <class ELFT>
804 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
805 const Elf_Shdr *SymTab = EF.getDotDynSymSec();
806 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
807 return basic_symbol_iterator(SymbolRef(Sym, this));
810 template <class ELFT>
811 section_iterator ELFObjectFile<ELFT>::section_begin() const {
812 return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
815 template <class ELFT>
816 section_iterator ELFObjectFile<ELFT>::section_end() const {
817 return section_iterator(SectionRef(toDRI(EF.section_end()), this));
820 template <class ELFT>
821 StringRef ELFObjectFile<ELFT>::getLoadName() const {
822 Elf_Dyn_Iter DI = EF.dynamic_table_begin();
823 Elf_Dyn_Iter DE = EF.dynamic_table_end();
825 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
829 return EF.getDynamicString(DI->getVal());
833 template <class ELFT>
834 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
835 return ELFT::Is64Bits ? 8 : 4;
838 template <class ELFT>
839 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
840 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
841 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
842 case ELF::ELFCLASS32:
843 switch (EF.getHeader()->e_machine) {
847 return "ELF32-x86-64";
849 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
850 case ELF::EM_HEXAGON:
851 return "ELF32-hexagon";
857 case ELF::EM_SPARC32PLUS:
858 return "ELF32-sparc";
860 return "ELF32-unknown";
862 case ELF::ELFCLASS64:
863 switch (EF.getHeader()->e_machine) {
867 return "ELF64-x86-64";
868 case ELF::EM_AARCH64:
869 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
871 return "ELF64-ppc64";
874 case ELF::EM_SPARCV9:
875 return "ELF64-sparc";
879 return "ELF64-unknown";
882 // FIXME: Proper error handling.
883 report_fatal_error("Invalid ELFCLASS!");
887 template <class ELFT>
888 unsigned ELFObjectFile<ELFT>::getArch() const {
889 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
890 switch (EF.getHeader()->e_machine) {
894 return Triple::x86_64;
895 case ELF::EM_AARCH64:
896 return Triple::aarch64;
899 case ELF::EM_HEXAGON:
900 return Triple::hexagon;
902 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
903 case ELF::ELFCLASS32:
904 return IsLittleEndian ? Triple::mipsel : Triple::mips;
905 case ELF::ELFCLASS64:
906 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
908 report_fatal_error("Invalid ELFCLASS!");
913 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
915 return Triple::systemz;
918 case ELF::EM_SPARC32PLUS:
919 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
920 case ELF::EM_SPARCV9:
921 return Triple::sparcv9;
924 return Triple::UnknownArch;
928 template <class ELFT>
929 ELFObjectFileBase::elf_symbol_iterator_range
930 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
931 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
934 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
935 return EF.getHeader()->e_type == ELF::ET_REL;