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 ErrorOr<uint64_t> getSymbolAddress(DataRefImpl Symb) const override;
200 uint64_t getSymbolValue(DataRefImpl Symb) const override;
201 uint32_t getSymbolAlignment(DataRefImpl Symb) const override;
202 uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override;
203 uint32_t getSymbolFlags(DataRefImpl Symb) const override;
204 uint8_t getSymbolOther(DataRefImpl Symb) const override;
205 uint8_t getSymbolELFType(DataRefImpl Symb) const override;
206 SymbolRef::Type getSymbolType(DataRefImpl Symb) const override;
207 section_iterator getSymbolSection(const Elf_Sym *Symb) const;
208 std::error_code getSymbolSection(DataRefImpl Symb,
209 section_iterator &Res) const override;
211 void moveSectionNext(DataRefImpl &Sec) const override;
212 std::error_code getSectionName(DataRefImpl Sec,
213 StringRef &Res) const override;
214 uint64_t getSectionAddress(DataRefImpl Sec) const override;
215 uint64_t getSectionSize(DataRefImpl Sec) const override;
216 std::error_code getSectionContents(DataRefImpl Sec,
217 StringRef &Res) const override;
218 uint64_t getSectionAlignment(DataRefImpl Sec) const override;
219 bool isSectionText(DataRefImpl Sec) const override;
220 bool isSectionData(DataRefImpl Sec) const override;
221 bool isSectionBSS(DataRefImpl Sec) const override;
222 bool isSectionVirtual(DataRefImpl Sec) const override;
223 relocation_iterator section_rel_begin(DataRefImpl Sec) const override;
224 relocation_iterator section_rel_end(DataRefImpl Sec) const override;
225 section_iterator getRelocatedSection(DataRefImpl Sec) const override;
227 void moveRelocationNext(DataRefImpl &Rel) const override;
228 ErrorOr<uint64_t> getRelocationAddress(DataRefImpl Rel) const override;
229 uint64_t getRelocationOffset(DataRefImpl Rel) const override;
230 symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override;
231 uint64_t getRelocationType(DataRefImpl Rel) const override;
232 void getRelocationTypeName(DataRefImpl Rel,
233 SmallVectorImpl<char> &Result) const override;
235 uint32_t getSectionType(DataRefImpl Sec) const override;
236 uint64_t getSectionFlags(DataRefImpl Sec) const override;
237 uint64_t getROffset(DataRefImpl Rel) const;
238 StringRef getRelocationTypeName(uint32_t Type) const;
240 /// \brief Get the relocation section that contains \a Rel.
241 const Elf_Shdr *getRelSection(DataRefImpl Rel) const {
242 return *EF.getSection(Rel.d.a);
245 const Elf_Sym *toELFSymIter(DataRefImpl Sym) const {
246 return EF.template getEntry<Elf_Sym>(Sym.d.a, Sym.d.b);
249 DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const {
256 assert(SymTable->sh_type == ELF::SHT_SYMTAB ||
257 SymTable->sh_type == ELF::SHT_DYNSYM);
259 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
260 unsigned SymTableIndex =
261 (reinterpret_cast<uintptr_t>(SymTable) - SHT) / sizeof(Elf_Shdr);
263 DRI.d.a = SymTableIndex;
268 const Elf_Shdr *toELFShdrIter(DataRefImpl Sec) const {
269 return reinterpret_cast<const Elf_Shdr *>(Sec.p);
272 DataRefImpl toDRI(const Elf_Shdr *Sec) const {
274 DRI.p = reinterpret_cast<uintptr_t>(Sec);
278 DataRefImpl toDRI(Elf_Dyn_Iter Dyn) const {
280 DRI.p = reinterpret_cast<uintptr_t>(Dyn.get());
284 bool isExportedToOtherDSO(const Elf_Sym *ESym) const {
285 unsigned char Binding = ESym->getBinding();
286 unsigned char Visibility = ESym->getVisibility();
288 // A symbol is exported if its binding is either GLOBAL or WEAK, and its
289 // visibility is either DEFAULT or PROTECTED. All other symbols are not
291 if ((Binding == ELF::STB_GLOBAL || Binding == ELF::STB_WEAK) &&
292 (Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_PROTECTED))
298 // This flag is used for classof, to distinguish ELFObjectFile from
299 // its subclass. If more subclasses will be created, this flag will
300 // have to become an enum.
301 bool isDyldELFObject;
304 ELFObjectFile(MemoryBufferRef Object, std::error_code &EC);
306 const Elf_Rel *getRel(DataRefImpl Rel) const;
307 const Elf_Rela *getRela(DataRefImpl Rela) const;
309 const Elf_Sym *getSymbol(DataRefImpl Symb) const;
311 basic_symbol_iterator symbol_begin_impl() const override;
312 basic_symbol_iterator symbol_end_impl() const override;
314 elf_symbol_iterator dynamic_symbol_begin() const;
315 elf_symbol_iterator dynamic_symbol_end() const;
317 section_iterator section_begin() const override;
318 section_iterator section_end() const override;
320 ErrorOr<int64_t> getRelocationAddend(DataRefImpl Rel) const override;
322 uint8_t getBytesInAddress() const override;
323 StringRef getFileFormatName() const override;
324 unsigned getArch() const override;
325 StringRef getLoadName() const;
327 std::error_code getPlatformFlags(unsigned &Result) const override {
328 Result = EF.getHeader()->e_flags;
329 return std::error_code();
332 const ELFFile<ELFT> *getELFFile() const { return &EF; }
334 bool isDyldType() const { return isDyldELFObject; }
335 static inline bool classof(const Binary *v) {
336 return v->getType() == getELFType(ELFT::TargetEndianness == support::little,
340 elf_symbol_iterator_range getDynamicSymbolIterators() const override;
342 bool isRelocatableObject() const override;
345 typedef ELFObjectFile<ELFType<support::little, false>> ELF32LEObjectFile;
346 typedef ELFObjectFile<ELFType<support::little, true>> ELF64LEObjectFile;
347 typedef ELFObjectFile<ELFType<support::big, false>> ELF32BEObjectFile;
348 typedef ELFObjectFile<ELFType<support::big, true>> ELF64BEObjectFile;
350 template <class ELFT>
351 void ELFObjectFile<ELFT>::moveSymbolNext(DataRefImpl &Sym) const {
355 template <class ELFT>
356 ErrorOr<StringRef> ELFObjectFile<ELFT>::getSymbolName(DataRefImpl Sym) const {
357 const Elf_Sym *ESym = toELFSymIter(Sym);
358 const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a);
359 const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link);
360 StringRef SymTable = *EF.getStringTable(StringTableSec);
361 return ESym->getName(SymTable);
364 template <class ELFT>
365 uint64_t ELFObjectFile<ELFT>::getSectionFlags(DataRefImpl Sec) const {
366 return toELFShdrIter(Sec)->sh_flags;
369 template <class ELFT>
370 uint32_t ELFObjectFile<ELFT>::getSectionType(DataRefImpl Sec) const {
371 return toELFShdrIter(Sec)->sh_type;
374 template <class ELFT>
375 uint64_t ELFObjectFile<ELFT>::getSymbolValue(DataRefImpl Symb) const {
376 const Elf_Sym *ESym = getSymbol(Symb);
377 switch (ESym->st_shndx) {
378 case ELF::SHN_COMMON:
380 return UnknownAddress;
382 return ESym->st_value;
385 const Elf_Ehdr *Header = EF.getHeader();
386 uint64_t Ret = ESym->st_value;
388 // Clear the ARM/Thumb or microMIPS indicator flag.
389 if ((Header->e_machine == ELF::EM_ARM || Header->e_machine == ELF::EM_MIPS) &&
390 ESym->getType() == ELF::STT_FUNC)
396 template <class ELFT>
398 ELFObjectFile<ELFT>::getSymbolAddress(DataRefImpl Symb) const {
399 uint64_t Result = getSymbolValue(Symb);
400 const Elf_Sym *ESym = getSymbol(Symb);
401 switch (ESym->st_shndx) {
402 case ELF::SHN_COMMON:
408 const Elf_Ehdr *Header = EF.getHeader();
410 if (Header->e_type == ELF::ET_REL) {
411 ErrorOr<const Elf_Shdr *> SectionOrErr = EF.getSection(ESym);
412 if (std::error_code EC = SectionOrErr.getError())
414 const Elf_Shdr *Section = *SectionOrErr;
416 Result += Section->sh_addr;
422 template <class ELFT>
423 uint32_t ELFObjectFile<ELFT>::getSymbolAlignment(DataRefImpl Symb) const {
424 const Elf_Sym *Sym = toELFSymIter(Symb);
425 if (Sym->st_shndx == ELF::SHN_COMMON)
426 return Sym->st_value;
430 template <class ELFT>
431 uint64_t ELFObjectFile<ELFT>::getSymbolSize(DataRefImpl Sym) const {
432 return toELFSymIter(Sym)->st_size;
435 template <class ELFT>
436 uint64_t ELFObjectFile<ELFT>::getCommonSymbolSizeImpl(DataRefImpl Symb) const {
437 return toELFSymIter(Symb)->st_size;
440 template <class ELFT>
441 uint8_t ELFObjectFile<ELFT>::getSymbolOther(DataRefImpl Symb) const {
442 return toELFSymIter(Symb)->st_other;
445 template <class ELFT>
446 uint8_t ELFObjectFile<ELFT>::getSymbolELFType(DataRefImpl Symb) const {
447 return toELFSymIter(Symb)->getType();
450 template <class ELFT>
451 SymbolRef::Type ELFObjectFile<ELFT>::getSymbolType(DataRefImpl Symb) const {
452 const Elf_Sym *ESym = getSymbol(Symb);
454 switch (ESym->getType()) {
455 case ELF::STT_NOTYPE:
456 return SymbolRef::ST_Unknown;
457 case ELF::STT_SECTION:
458 return SymbolRef::ST_Debug;
460 return SymbolRef::ST_File;
462 return SymbolRef::ST_Function;
463 case ELF::STT_OBJECT:
464 case ELF::STT_COMMON:
466 return SymbolRef::ST_Data;
468 return SymbolRef::ST_Other;
472 template <class ELFT>
473 uint32_t ELFObjectFile<ELFT>::getSymbolFlags(DataRefImpl Sym) const {
474 const Elf_Sym *ESym = toELFSymIter(Sym);
476 uint32_t Result = SymbolRef::SF_None;
478 if (ESym->getBinding() != ELF::STB_LOCAL)
479 Result |= SymbolRef::SF_Global;
481 if (ESym->getBinding() == ELF::STB_WEAK)
482 Result |= SymbolRef::SF_Weak;
484 if (ESym->st_shndx == ELF::SHN_ABS)
485 Result |= SymbolRef::SF_Absolute;
487 if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION ||
488 ESym == EF.symbol_begin() || ESym == EF.dynamic_symbol_begin())
489 Result |= SymbolRef::SF_FormatSpecific;
491 if (EF.getHeader()->e_machine == ELF::EM_ARM) {
492 if (ErrorOr<StringRef> NameOrErr = getSymbolName(Sym)) {
493 StringRef Name = *NameOrErr;
494 if (Name.startswith("$d") || Name.startswith("$t") ||
495 Name.startswith("$a"))
496 Result |= SymbolRef::SF_FormatSpecific;
500 if (ESym->st_shndx == ELF::SHN_UNDEF)
501 Result |= SymbolRef::SF_Undefined;
503 if (ESym->getType() == ELF::STT_COMMON || ESym->st_shndx == ELF::SHN_COMMON)
504 Result |= SymbolRef::SF_Common;
506 if (isExportedToOtherDSO(ESym))
507 Result |= SymbolRef::SF_Exported;
509 if (ESym->getVisibility() == ELF::STV_HIDDEN)
510 Result |= SymbolRef::SF_Hidden;
515 template <class ELFT>
517 ELFObjectFile<ELFT>::getSymbolSection(const Elf_Sym *ESym) const {
518 ErrorOr<const Elf_Shdr *> ESecOrErr = EF.getSection(ESym);
519 if (std::error_code EC = ESecOrErr.getError())
520 report_fatal_error(EC.message());
522 const Elf_Shdr *ESec = *ESecOrErr;
524 return section_end();
527 Sec.p = reinterpret_cast<intptr_t>(ESec);
528 return section_iterator(SectionRef(Sec, this));
531 template <class ELFT>
533 ELFObjectFile<ELFT>::getSymbolSection(DataRefImpl Symb,
534 section_iterator &Res) const {
535 Res = getSymbolSection(getSymbol(Symb));
536 return std::error_code();
539 template <class ELFT>
540 void ELFObjectFile<ELFT>::moveSectionNext(DataRefImpl &Sec) const {
541 const Elf_Shdr *ESec = toELFShdrIter(Sec);
545 template <class ELFT>
546 std::error_code ELFObjectFile<ELFT>::getSectionName(DataRefImpl Sec,
547 StringRef &Result) const {
548 ErrorOr<StringRef> Name = EF.getSectionName(&*toELFShdrIter(Sec));
550 return Name.getError();
552 return std::error_code();
555 template <class ELFT>
556 uint64_t ELFObjectFile<ELFT>::getSectionAddress(DataRefImpl Sec) const {
557 return toELFShdrIter(Sec)->sh_addr;
560 template <class ELFT>
561 uint64_t ELFObjectFile<ELFT>::getSectionSize(DataRefImpl Sec) const {
562 return toELFShdrIter(Sec)->sh_size;
565 template <class ELFT>
567 ELFObjectFile<ELFT>::getSectionContents(DataRefImpl Sec,
568 StringRef &Result) const {
569 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
570 Result = StringRef((const char *)base() + EShdr->sh_offset, EShdr->sh_size);
571 return std::error_code();
574 template <class ELFT>
575 uint64_t ELFObjectFile<ELFT>::getSectionAlignment(DataRefImpl Sec) const {
576 return toELFShdrIter(Sec)->sh_addralign;
579 template <class ELFT>
580 bool ELFObjectFile<ELFT>::isSectionText(DataRefImpl Sec) const {
581 return toELFShdrIter(Sec)->sh_flags & ELF::SHF_EXECINSTR;
584 template <class ELFT>
585 bool ELFObjectFile<ELFT>::isSectionData(DataRefImpl Sec) const {
586 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
587 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
588 EShdr->sh_type == ELF::SHT_PROGBITS;
591 template <class ELFT>
592 bool ELFObjectFile<ELFT>::isSectionBSS(DataRefImpl Sec) const {
593 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
594 return EShdr->sh_flags & (ELF::SHF_ALLOC | ELF::SHF_WRITE) &&
595 EShdr->sh_type == ELF::SHT_NOBITS;
598 template <class ELFT>
599 bool ELFObjectFile<ELFT>::isSectionVirtual(DataRefImpl Sec) const {
600 return toELFShdrIter(Sec)->sh_type == ELF::SHT_NOBITS;
603 template <class ELFT>
605 ELFObjectFile<ELFT>::section_rel_begin(DataRefImpl Sec) const {
607 uintptr_t SHT = reinterpret_cast<uintptr_t>(EF.section_begin());
608 RelData.d.a = (Sec.p - SHT) / EF.getHeader()->e_shentsize;
611 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
612 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
613 return relocation_iterator(RelocationRef(RelData, this));
615 const Elf_Shdr *RelSec = getRelSection(RelData);
616 ErrorOr<const Elf_Shdr *> SymSecOrErr = EF.getSection(RelSec->sh_link);
617 if (std::error_code EC = SymSecOrErr.getError())
618 report_fatal_error(EC.message());
619 const Elf_Shdr *SymSec = *SymSecOrErr;
620 uint32_t SymSecType = SymSec->sh_type;
621 if (SymSecType != ELF::SHT_SYMTAB && SymSecType != ELF::SHT_DYNSYM)
622 report_fatal_error("Invalid symbol table section type!");
623 if (SymSecType == ELF::SHT_DYNSYM)
626 return relocation_iterator(RelocationRef(RelData, this));
629 template <class ELFT>
631 ELFObjectFile<ELFT>::section_rel_end(DataRefImpl Sec) const {
632 const Elf_Shdr *S = reinterpret_cast<const Elf_Shdr *>(Sec.p);
633 relocation_iterator Begin = section_rel_begin(Sec);
634 if (S->sh_type != ELF::SHT_RELA && S->sh_type != ELF::SHT_REL)
636 DataRefImpl RelData = Begin->getRawDataRefImpl();
637 RelData.d.b += (S->sh_size / S->sh_entsize) << 1;
638 return relocation_iterator(RelocationRef(RelData, this));
641 template <class ELFT>
643 ELFObjectFile<ELFT>::getRelocatedSection(DataRefImpl Sec) const {
644 if (EF.getHeader()->e_type != ELF::ET_REL)
645 return section_end();
647 const Elf_Shdr *EShdr = toELFShdrIter(Sec);
648 uintX_t Type = EShdr->sh_type;
649 if (Type != ELF::SHT_REL && Type != ELF::SHT_RELA)
650 return section_end();
652 ErrorOr<const Elf_Shdr *> R = EF.getSection(EShdr->sh_info);
653 if (std::error_code EC = R.getError())
654 report_fatal_error(EC.message());
655 return section_iterator(SectionRef(toDRI(*R), this));
659 template <class ELFT>
660 void ELFObjectFile<ELFT>::moveRelocationNext(DataRefImpl &Rel) const {
664 template <class ELFT>
666 ELFObjectFile<ELFT>::getRelocationSymbol(DataRefImpl Rel) const {
668 const Elf_Shdr *sec = getRelSection(Rel);
669 if (sec->sh_type == ELF::SHT_REL)
670 symbolIdx = getRel(Rel)->getSymbol(EF.isMips64EL());
672 symbolIdx = getRela(Rel)->getSymbol(EF.isMips64EL());
676 bool IsDyn = Rel.d.b & 1;
677 DataRefImpl SymbolData;
679 SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx);
681 SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx);
682 return symbol_iterator(SymbolRef(SymbolData, this));
685 template <class ELFT>
687 ELFObjectFile<ELFT>::getRelocationAddress(DataRefImpl Rel) const {
688 uint64_t ROffset = getROffset(Rel);
689 const Elf_Ehdr *Header = EF.getHeader();
691 if (Header->e_type == ELF::ET_REL) {
692 const Elf_Shdr *RelocationSec = getRelSection(Rel);
693 ErrorOr<const Elf_Shdr *> RelocatedSec =
694 EF.getSection(RelocationSec->sh_info);
695 if (std::error_code EC = RelocatedSec.getError())
697 return ROffset + (*RelocatedSec)->sh_addr;
702 template <class ELFT>
703 uint64_t ELFObjectFile<ELFT>::getRelocationOffset(DataRefImpl Rel) const {
704 assert(EF.getHeader()->e_type == ELF::ET_REL &&
705 "Only relocatable object files have relocation offsets");
706 return getROffset(Rel);
709 template <class ELFT>
710 uint64_t ELFObjectFile<ELFT>::getROffset(DataRefImpl Rel) const {
711 const Elf_Shdr *sec = getRelSection(Rel);
712 if (sec->sh_type == ELF::SHT_REL)
713 return getRel(Rel)->r_offset;
715 return getRela(Rel)->r_offset;
718 template <class ELFT>
719 uint64_t ELFObjectFile<ELFT>::getRelocationType(DataRefImpl Rel) const {
720 const Elf_Shdr *sec = getRelSection(Rel);
721 if (sec->sh_type == ELF::SHT_REL)
722 return getRel(Rel)->getType(EF.isMips64EL());
724 return getRela(Rel)->getType(EF.isMips64EL());
727 template <class ELFT>
728 StringRef ELFObjectFile<ELFT>::getRelocationTypeName(uint32_t Type) const {
729 return getELFRelocationTypeName(EF.getHeader()->e_machine, Type);
732 template <class ELFT>
733 void ELFObjectFile<ELFT>::getRelocationTypeName(
734 DataRefImpl Rel, SmallVectorImpl<char> &Result) const {
735 uint32_t type = getRelocationType(Rel);
736 EF.getRelocationTypeName(type, Result);
739 template <class ELFT>
741 ELFObjectFile<ELFT>::getRelocationAddend(DataRefImpl Rel) const {
742 if (getRelSection(Rel)->sh_type != ELF::SHT_RELA)
743 return object_error::parse_failed;
744 return (int64_t)getRela(Rel)->r_addend;
747 template <class ELFT>
748 const typename ELFFile<ELFT>::Elf_Sym *
749 ELFObjectFile<ELFT>::getSymbol(DataRefImpl Symb) const {
750 return &*toELFSymIter(Symb);
753 template <class ELFT>
754 const typename ELFObjectFile<ELFT>::Elf_Rel *
755 ELFObjectFile<ELFT>::getRel(DataRefImpl Rel) const {
756 assert(getRelSection(Rel)->sh_type == ELF::SHT_REL);
757 return EF.template getEntry<Elf_Rel>(Rel.d.a, Rel.d.b >> 1);
760 template <class ELFT>
761 const typename ELFObjectFile<ELFT>::Elf_Rela *
762 ELFObjectFile<ELFT>::getRela(DataRefImpl Rela) const {
763 assert(getRelSection(Rela)->sh_type == ELF::SHT_RELA);
764 return EF.template getEntry<Elf_Rela>(Rela.d.a, Rela.d.b >> 1);
767 template <class ELFT>
768 ELFObjectFile<ELFT>::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC)
770 getELFType(static_cast<endianness>(ELFT::TargetEndianness) ==
774 EF(Data.getBuffer(), EC) {}
776 template <class ELFT>
777 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_begin_impl() const {
778 DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0);
779 return basic_symbol_iterator(SymbolRef(Sym, this));
782 template <class ELFT>
783 basic_symbol_iterator ELFObjectFile<ELFT>::symbol_end_impl() const {
784 const Elf_Shdr *SymTab = EF.getDotSymtabSec();
786 return symbol_begin_impl();
787 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
788 return basic_symbol_iterator(SymbolRef(Sym, this));
791 template <class ELFT>
792 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_begin() const {
793 DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0);
794 return symbol_iterator(SymbolRef(Sym, this));
797 template <class ELFT>
798 elf_symbol_iterator ELFObjectFile<ELFT>::dynamic_symbol_end() const {
799 const Elf_Shdr *SymTab = EF.getDotDynSymSec();
800 DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym));
801 return basic_symbol_iterator(SymbolRef(Sym, this));
804 template <class ELFT>
805 section_iterator ELFObjectFile<ELFT>::section_begin() const {
806 return section_iterator(SectionRef(toDRI(EF.section_begin()), this));
809 template <class ELFT>
810 section_iterator ELFObjectFile<ELFT>::section_end() const {
811 return section_iterator(SectionRef(toDRI(EF.section_end()), this));
814 template <class ELFT>
815 StringRef ELFObjectFile<ELFT>::getLoadName() const {
816 Elf_Dyn_Iter DI = EF.dynamic_table_begin();
817 Elf_Dyn_Iter DE = EF.dynamic_table_end();
819 while (DI != DE && DI->getTag() != ELF::DT_SONAME)
823 return EF.getDynamicString(DI->getVal());
827 template <class ELFT>
828 uint8_t ELFObjectFile<ELFT>::getBytesInAddress() const {
829 return ELFT::Is64Bits ? 8 : 4;
832 template <class ELFT>
833 StringRef ELFObjectFile<ELFT>::getFileFormatName() const {
834 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
835 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
836 case ELF::ELFCLASS32:
837 switch (EF.getHeader()->e_machine) {
841 return "ELF32-x86-64";
843 return (IsLittleEndian ? "ELF32-arm-little" : "ELF32-arm-big");
844 case ELF::EM_HEXAGON:
845 return "ELF32-hexagon";
851 case ELF::EM_SPARC32PLUS:
852 return "ELF32-sparc";
854 return "ELF32-unknown";
856 case ELF::ELFCLASS64:
857 switch (EF.getHeader()->e_machine) {
861 return "ELF64-x86-64";
862 case ELF::EM_AARCH64:
863 return (IsLittleEndian ? "ELF64-aarch64-little" : "ELF64-aarch64-big");
865 return "ELF64-ppc64";
868 case ELF::EM_SPARCV9:
869 return "ELF64-sparc";
873 return "ELF64-unknown";
876 // FIXME: Proper error handling.
877 report_fatal_error("Invalid ELFCLASS!");
881 template <class ELFT>
882 unsigned ELFObjectFile<ELFT>::getArch() const {
883 bool IsLittleEndian = ELFT::TargetEndianness == support::little;
884 switch (EF.getHeader()->e_machine) {
888 return Triple::x86_64;
889 case ELF::EM_AARCH64:
890 return Triple::aarch64;
893 case ELF::EM_HEXAGON:
894 return Triple::hexagon;
896 switch (EF.getHeader()->e_ident[ELF::EI_CLASS]) {
897 case ELF::ELFCLASS32:
898 return IsLittleEndian ? Triple::mipsel : Triple::mips;
899 case ELF::ELFCLASS64:
900 return IsLittleEndian ? Triple::mips64el : Triple::mips64;
902 report_fatal_error("Invalid ELFCLASS!");
907 return IsLittleEndian ? Triple::ppc64le : Triple::ppc64;
909 return Triple::systemz;
912 case ELF::EM_SPARC32PLUS:
913 return IsLittleEndian ? Triple::sparcel : Triple::sparc;
914 case ELF::EM_SPARCV9:
915 return Triple::sparcv9;
918 return Triple::UnknownArch;
922 template <class ELFT>
923 ELFObjectFileBase::elf_symbol_iterator_range
924 ELFObjectFile<ELFT>::getDynamicSymbolIterators() const {
925 return make_range(dynamic_symbol_begin(), dynamic_symbol_end());
928 template <class ELFT> bool ELFObjectFile<ELFT>::isRelocatableObject() const {
929 return EF.getHeader()->e_type == ELF::ET_REL;