@@ -357,8 +357,8 @@ class ELFFile {
357
357
uintX_t getStringTableIndex () const ;
358
358
ELF::Elf64_Word getExtendedSymbolTableIndex (const Elf_Sym *symb) const ;
359
359
const Elf_Ehdr *getHeader () const { return Header; }
360
- const Elf_Shdr *getSection (const Elf_Sym *symb) const ;
361
- const Elf_Shdr *getSection (uint32_t Index) const ;
360
+ ErrorOr< const Elf_Shdr *> getSection (const Elf_Sym *symb) const ;
361
+ ErrorOr< const Elf_Shdr *> getSection (uint32_t Index) const ;
362
362
const Elf_Sym *getSymbol (uint32_t index) const ;
363
363
364
364
ErrorOr<StringRef> getStaticSymbolName (const Elf_Sym *Symb) const ;
@@ -465,11 +465,12 @@ ELFFile<ELFT>::getExtendedSymbolTableIndex(const Elf_Sym *symb) const {
465
465
}
466
466
467
467
template <class ELFT >
468
- const typename ELFFile<ELFT>::Elf_Shdr *
468
+ ErrorOr< const typename ELFFile<ELFT>::Elf_Shdr *>
469
469
ELFFile<ELFT>::getSection(const Elf_Sym *symb) const {
470
- if (symb->st_shndx == ELF::SHN_XINDEX)
470
+ uint32_t Index = symb->st_shndx ;
471
+ if (Index == ELF::SHN_XINDEX)
471
472
return getSection (ExtendedSymbolTable.lookup (symb));
472
- if (symb-> st_shndx >= ELF::SHN_LORESERVE)
473
+ if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE)
473
474
return nullptr ;
474
475
return getSection (symb->st_shndx );
475
476
}
@@ -532,7 +533,10 @@ std::pair<const typename ELFFile<ELFT>::Elf_Shdr *,
532
533
ELFFile<ELFT>::getRelocationSymbol(const Elf_Shdr *Sec, const RelT *Rel) const {
533
534
if (!Sec->sh_link )
534
535
return std::make_pair (nullptr , nullptr );
535
- const Elf_Shdr *SymTable = getSection (Sec->sh_link );
536
+ ErrorOr<const Elf_Shdr *> SymTableOrErr = getSection (Sec->sh_link );
537
+ if (std::error_code EC = SymTableOrErr.getError ())
538
+ report_fatal_error (EC.message ());
539
+ const Elf_Shdr *SymTable = *SymTableOrErr;
536
540
return std::make_pair (
537
541
SymTable, getEntry<Elf_Sym>(SymTable, Rel->getSymbol (isMips64EL ())));
538
542
}
@@ -615,7 +619,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
615
619
return ;
616
620
}
617
621
dot_symtab_sec = &Sec;
618
- ErrorOr<StringRef> SymtabOrErr = getStringTable (getSection (Sec.sh_link ));
622
+ ErrorOr<const Elf_Shdr *> SectionOrErr = getSection (Sec.sh_link );
623
+ if ((EC = SectionOrErr.getError ()))
624
+ return ;
625
+ ErrorOr<StringRef> SymtabOrErr = getStringTable (*SectionOrErr);
619
626
if ((EC = SymtabOrErr.getError ()))
620
627
return ;
621
628
DotStrtab = *SymtabOrErr;
@@ -629,7 +636,10 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
629
636
DynSymRegion.Addr = base () + Sec.sh_offset ;
630
637
DynSymRegion.Size = Sec.sh_size ;
631
638
DynSymRegion.EntSize = Sec.sh_entsize ;
632
- const Elf_Shdr *DynStr = getSection (Sec.sh_link );
639
+ ErrorOr<const Elf_Shdr *> DynStrOrErr = getSection (Sec.sh_link );
640
+ if ((EC = DynStrOrErr.getError ()))
641
+ return ;
642
+ const Elf_Shdr *DynStr = *DynStrOrErr;
633
643
DynStrRegion.Addr = base () + DynStr->sh_offset ;
634
644
DynStrRegion.Size = DynStr->sh_size ;
635
645
DynStrRegion.EntSize = DynStr->sh_entsize ;
@@ -673,8 +683,11 @@ ELFFile<ELFT>::ELFFile(StringRef Object, std::error_code &EC)
673
683
}
674
684
675
685
// Get string table sections.
676
- ErrorOr<StringRef> SymtabOrErr =
677
- getStringTable (getSection (getStringTableIndex ()));
686
+ ErrorOr<const Elf_Shdr *> StrTabSecOrErr = getSection (getStringTableIndex ());
687
+ if ((EC = StrTabSecOrErr.getError ()))
688
+ return ;
689
+
690
+ ErrorOr<StringRef> SymtabOrErr = getStringTable (*StrTabSecOrErr);
678
691
if ((EC = SymtabOrErr.getError ()))
679
692
return ;
680
693
DotShstrtab = *SymtabOrErr;
@@ -825,7 +838,10 @@ StringRef ELFFile<ELFT>::getLoadName() const {
825
838
template <class ELFT >
826
839
template <typename T>
827
840
const T *ELFFile<ELFT>::getEntry(uint32_t Section, uint32_t Entry) const {
828
- return getEntry<T>(getSection (Section), Entry);
841
+ ErrorOr<const Elf_Shdr *> Sec = getSection (Section);
842
+ if (std::error_code EC = Sec.getError ())
843
+ report_fatal_error (EC.message ());
844
+ return getEntry<T>(*Sec, Entry);
829
845
}
830
846
831
847
template <class ELFT >
@@ -837,17 +853,15 @@ const T *ELFFile<ELFT>::getEntry(const Elf_Shdr *Section,
837
853
}
838
854
839
855
template <class ELFT >
840
- const typename ELFFile<ELFT>::Elf_Shdr *
841
- ELFFile<ELFT>::getSection(uint32_t index) const {
842
- if (index == 0 )
843
- return nullptr ;
844
- if (!SectionHeaderTable || index >= getNumSections ())
845
- // FIXME: Proper error handling.
846
- report_fatal_error (" Invalid section index!" );
856
+ ErrorOr<const typename ELFFile<ELFT>::Elf_Shdr *>
857
+ ELFFile<ELFT>::getSection(uint32_t Index) const {
858
+ assert (SectionHeaderTable && " SectionHeaderTable not initialized!" );
859
+ if (Index >= getNumSections ())
860
+ return object_error::invalid_section_index;
847
861
848
862
return reinterpret_cast <const Elf_Shdr *>(
849
- reinterpret_cast <const char *>(SectionHeaderTable)
850
- + ( index * Header->e_shentsize ));
863
+ reinterpret_cast <const char *>(SectionHeaderTable) +
864
+ (Index * Header->e_shentsize ));
851
865
}
852
866
853
867
template <class ELFT >
0 commit comments