@@ -300,13 +300,15 @@ class ELFFile {
300300 }
301301 return *RealPhNum;
302302 }
303+
303304 Expected<uint64_t > getShNum () const {
304305 if (!RealShNum) {
305306 if (Error E = const_cast <ELFFile<ELFT> *>(this )->readShdrZero ())
306307 return std::move (E);
307308 }
308309 return *RealShNum;
309310 }
311+
310312 Expected<uint32_t > getShStrNdx () const {
311313 if (!RealShStrNdx) {
312314 if (Error E = const_cast <ELFFile<ELFT> *>(this )->readShdrZero ())
@@ -811,11 +813,12 @@ Expected<StringRef>
811813ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
812814 WarningHandler WarnHandler) const {
813815 Expected<uint32_t > ShStrNdxOrErr = getShStrNdx ();
814- if (!ShStrNdxOrErr || (*ShStrNdxOrErr == ELF::SHN_XINDEX && RealShNum == 0 )) {
815- consumeError (ShStrNdxOrErr.takeError ());
816+ if (!ShStrNdxOrErr)
817+ return ShStrNdxOrErr.takeError ();
818+
819+ if (*ShStrNdxOrErr == ELF::SHN_XINDEX && Sections.empty ())
816820 return createError (
817821 " e_shstrndx == SHN_XINDEX, but the section header table is empty" );
818- }
819822
820823 uint32_t Index = *ShStrNdxOrErr;
821824 // There is no section name string table. Return FakeSectionStrings which
@@ -930,9 +933,8 @@ template <class ELFT> Error ELFFile<ELFT>::readShdrZero() {
930933 if ((Header.e_phnum == ELF::PN_XNUM || Header.e_shnum == 0 ||
931934 Header.e_shstrndx == ELF::SHN_XINDEX) &&
932935 Header.e_shoff != 0 ) {
933-
934936 // Pretend we have section 0 or sections() would call getShNum and thus
935- // become an infinite recursion
937+ // become an infinite recursion.
936938 if (Header.e_shnum == 0 )
937939 RealShNum = 1 ;
938940 else
0 commit comments