@@ -3572,12 +3572,30 @@ static inline void printFields(formatted_raw_ostream &OS, StringRef Str1,
35723572 OS.flush ();
35733573}
35743574
3575+ template <class ELFT >
3576+ static std::string getProgramHeadersNumString (const ELFFile<ELFT> &Obj,
3577+ StringRef FileName) {
3578+ if (Obj.getHeader ().e_phnum != ELF::PN_XNUM)
3579+ return to_string (Obj.getHeader ().e_phnum );
3580+
3581+ Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections ();
3582+ if (!ArrOrErr) {
3583+ // In this case we can ignore an error, because we have already reported a
3584+ // warning about the broken section header table earlier.
3585+ consumeError (ArrOrErr.takeError ());
3586+ return " <?>" ;
3587+ }
3588+
3589+ if (Obj.getHeader ().e_phnum == Obj.getPhNum ())
3590+ return " 65535" ;
3591+ return " 65535 (" + to_string (Obj.getPhNum ()) + " )" ;
3592+ }
3593+
35753594template <class ELFT >
35763595static std::string getSectionHeadersNumString (const ELFFile<ELFT> &Obj,
35773596 StringRef FileName) {
3578- const typename ELFT::Ehdr &ElfHeader = Obj.getHeader ();
3579- if (ElfHeader.e_shnum != 0 )
3580- return to_string (ElfHeader.e_shnum );
3597+ if (Obj.getHeader ().e_shnum != 0 )
3598+ return to_string (Obj.getHeader ().e_shnum );
35813599
35823600 Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections ();
35833601 if (!ArrOrErr) {
@@ -3587,17 +3605,16 @@ static std::string getSectionHeadersNumString(const ELFFile<ELFT> &Obj,
35873605 return " <?>" ;
35883606 }
35893607
3590- if (ArrOrErr-> empty ())
3608+ if (Obj. getHeader (). e_shnum == Obj. getShNum ())
35913609 return " 0" ;
3592- return " 0 (" + to_string ((*ArrOrErr)[ 0 ]. sh_size ) + " )" ;
3610+ return " 0 (" + to_string (Obj. getShNum () ) + " )" ;
35933611}
35943612
35953613template <class ELFT >
35963614static std::string getSectionHeaderTableIndexString (const ELFFile<ELFT> &Obj,
35973615 StringRef FileName) {
3598- const typename ELFT::Ehdr &ElfHeader = Obj.getHeader ();
3599- if (ElfHeader.e_shstrndx != SHN_XINDEX)
3600- return to_string (ElfHeader.e_shstrndx );
3616+ if (Obj.getHeader ().e_shstrndx != SHN_XINDEX)
3617+ return to_string (Obj.getHeader ().e_shstrndx );
36013618
36023619 Expected<ArrayRef<typename ELFT::Shdr>> ArrOrErr = Obj.sections ();
36033620 if (!ArrOrErr) {
@@ -3607,10 +3624,9 @@ static std::string getSectionHeaderTableIndexString(const ELFFile<ELFT> &Obj,
36073624 return " <?>" ;
36083625 }
36093626
3610- if (ArrOrErr-> empty ())
3627+ if (Obj. getHeader (). e_shstrndx == Obj. getShStrNdx ())
36113628 return " 65535 (corrupt: out of range)" ;
3612- return to_string (ElfHeader.e_shstrndx ) + " (" +
3613- to_string ((*ArrOrErr)[0 ].sh_link ) + " )" ;
3629+ return " 65535 (" + to_string (Obj.getShStrNdx ()) + " )" ;
36143630}
36153631
36163632static const EnumEntry<unsigned > *getObjectFileEnumEntry (unsigned Type) {
@@ -3765,7 +3781,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
37653781 printFields (OS, " Size of this header:" , Str);
37663782 Str = to_string (e.e_phentsize ) + " (bytes)" ;
37673783 printFields (OS, " Size of program headers:" , Str);
3768- Str = to_string (e. e_phnum );
3784+ Str = getProgramHeadersNumString ( this -> Obj , this -> FileName );
37693785 printFields (OS, " Number of program headers:" , Str);
37703786 Str = to_string (e.e_shentsize ) + " (bytes)" ;
37713787 printFields (OS, " Size of section headers:" , Str);
@@ -4778,8 +4794,7 @@ void GNUELFDumper<ELFT>::printProgramHeaders(
47784794 return ;
47794795
47804796 if (PrintProgramHeaders) {
4781- const Elf_Ehdr &Header = this ->Obj .getHeader ();
4782- if (Header.e_phnum == 0 ) {
4797+ if (this ->Obj .getPhNum () == 0 ) {
47834798 OS << " \n There are no program headers in this file.\n " ;
47844799 } else {
47854800 printProgramHeaders ();
@@ -4798,7 +4813,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printProgramHeaders() {
47984813 OS << " \n Elf file type is "
47994814 << enumToString (Header.e_type , ArrayRef (ElfObjectFileType)) << " \n "
48004815 << " Entry point " << format_hex (Header.e_entry , 3 ) << " \n "
4801- << " There are " << Header. e_phnum << " program headers,"
4816+ << " There are " << this -> Obj . getPhNum () << " program headers,"
48024817 << " starting at offset " << Header.e_phoff << " \n\n "
48034818 << " Program Headers:\n " ;
48044819 if (ELFT::Is64Bits)
@@ -7470,7 +7485,8 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printFileHeaders() {
74707485 W.printFlags (" Flags" , E.e_flags );
74717486 W.printNumber (" HeaderSize" , E.e_ehsize );
74727487 W.printNumber (" ProgramHeaderEntrySize" , E.e_phentsize );
7473- W.printNumber (" ProgramHeaderCount" , E.e_phnum );
7488+ W.printString (" ProgramHeaderCount" ,
7489+ getProgramHeadersNumString (this ->Obj , this ->FileName ));
74747490 W.printNumber (" SectionHeaderEntrySize" , E.e_shentsize );
74757491 W.printString (" SectionHeaderCount" ,
74767492 getSectionHeadersNumString (this ->Obj , this ->FileName ));
0 commit comments