@@ -806,9 +806,12 @@ template <class ELFT>
806806Expected<StringRef>
807807ELFFile<ELFT>::getSectionStringTable(Elf_Shdr_Range Sections,
808808 WarningHandler WarnHandler) const {
809- if (getHeader ().e_shstrndx == ELF::SHN_XINDEX && !RealShStrNdx)
809+ Expected<uint32_t > ShStrNdxOrErr = getShStrNdx ();
810+ if (!ShStrNdxOrErr || (*ShStrNdxOrErr == ELF::SHN_XINDEX && RealShNum == 0 )) {
811+ consumeError (ShStrNdxOrErr.takeError ());
810812 return createError (
811813 " e_shstrndx == SHN_XINDEX, but the section header table is empty" );
814+ }
812815
813816 uint32_t Index;
814817 if (Expected<uint32_t > IndexOrErr = getShStrNdx ())
@@ -932,15 +935,29 @@ template <class ELFT> Error ELFFile<ELFT>::readShdrZero() {
932935 // Pretend we have section 0 or sections() would call getShNum and thus
933936 // become an infinite recursion
934937 RealShNum = 0 ;
935- auto SecOrErr = getSection ( 0 );
936- if (!SecOrErr ) {
938+ auto SecsOrErr = sections ( );
939+ if (!SecsOrErr ) {
937940 RealShNum = std::nullopt ;
938- return SecOrErr .takeError ();
941+ return SecsOrErr .takeError ();
939942 }
943+
944+ // We can really have 0 number of seciton
945+ if ((*SecsOrErr).size () == 0 ) {
946+ if (Header.e_phnum == ELF::PN_XNUM ||
947+ Header.e_shstrndx == ELF::SHN_XINDEX) {
948+ return createError (" Unable to find Section 0" );
949+ }
950+ RealShNum = 0 ;
951+ RealPhNum = Header.e_phnum ;
952+ RealShStrNdx = Header.e_shstrndx ;
953+ return Error::success ();
954+ }
955+
956+ auto &Section = (*SecsOrErr)[0 ];
940957 RealPhNum =
941- Header.e_phnum == ELF::PN_XNUM ? (*SecOrErr)-> sh_info : Header.e_phnum ;
942- RealShNum = Header.e_shnum == 0 ? (*SecOrErr)-> sh_size : Header.e_shnum ;
943- RealShStrNdx = Header.e_shstrndx == ELF::SHN_XINDEX ? (*SecOrErr)-> sh_link
958+ Header.e_phnum == ELF::PN_XNUM ? Section. sh_info : Header.e_phnum ;
959+ RealShNum = Header.e_shnum == 0 ? Section. sh_size : Header.e_shnum ;
960+ RealShStrNdx = Header.e_shstrndx == ELF::SHN_XINDEX ? Section. sh_link
944961 : Header.e_shstrndx ;
945962 } else {
946963 RealPhNum = Header.e_phnum ;
0 commit comments