@@ -3572,45 +3572,62 @@ 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+
3579+ if (Obj.getHeader ().e_phnum != ELF::PN_XNUM)
3580+ return to_string (Obj.getHeader ().e_phnum );
3581+
3582+ Expected<uint32_t > PhNumOrErr = Obj.getPhNum ();
3583+ if (!PhNumOrErr) {
3584+ // In this case we can ignore an error, because we have already reported a
3585+ // warning about the broken section header table earlier.
3586+ consumeError (PhNumOrErr.takeError ());
3587+ return " <?>" ;
3588+ }
3589+
3590+ if (*PhNumOrErr == ELF::PN_XNUM)
3591+ return " 65535 (corrupt)" ;
3592+ return " 65535 (" + to_string (*PhNumOrErr) + " )" ;
3593+ }
3594+
35753595template <class ELFT >
35763596static std::string getSectionHeadersNumString (const ELFFile<ELFT> &Obj,
35773597 StringRef FileName) {
3578- const typename ELFT::Ehdr &ElfHeader = Obj.getHeader ();
3579- if (ElfHeader.e_shnum != 0 )
3580- return to_string (ElfHeader.e_shnum );
3598+ if (Obj.getHeader ().e_shnum != 0 )
3599+ return to_string (Obj.getHeader ().e_shnum );
35813600
3582- Expected<ArrayRef< typename ELFT::Shdr>> ArrOrErr = Obj.sections ();
3583- if (!ArrOrErr ) {
3601+ Expected<uint64_t > ShNumOrErr = Obj.getShNum ();
3602+ if (!ShNumOrErr ) {
35843603 // In this case we can ignore an error, because we have already reported a
35853604 // warning about the broken section header table earlier.
3586- consumeError (ArrOrErr .takeError ());
3605+ consumeError (ShNumOrErr .takeError ());
35873606 return " <?>" ;
35883607 }
35893608
3590- if (ArrOrErr-> empty () )
3609+ if (*ShNumOrErr == 0 )
35913610 return " 0" ;
3592- return " 0 (" + to_string ((*ArrOrErr)[ 0 ]. sh_size ) + " )" ;
3611+ return " 0 (" + to_string (*ShNumOrErr ) + " )" ;
35933612}
35943613
35953614template <class ELFT >
35963615static std::string getSectionHeaderTableIndexString (const ELFFile<ELFT> &Obj,
35973616 StringRef FileName) {
3598- const typename ELFT::Ehdr &ElfHeader = Obj.getHeader ();
3599- if (ElfHeader.e_shstrndx != SHN_XINDEX)
3600- return to_string (ElfHeader.e_shstrndx );
3617+ if (Obj.getHeader ().e_shstrndx != ELF::SHN_XINDEX)
3618+ return to_string (Obj.getHeader ().e_shstrndx );
36013619
3602- Expected<ArrayRef< typename ELFT::Shdr>> ArrOrErr = Obj.sections ();
3603- if (!ArrOrErr ) {
3620+ Expected<uint32_t > ShStrNdxOrErr = Obj.getShStrNdx ();
3621+ if (!ShStrNdxOrErr ) {
36043622 // In this case we can ignore an error, because we have already reported a
36053623 // warning about the broken section header table earlier.
3606- consumeError (ArrOrErr .takeError ());
3624+ consumeError (ShStrNdxOrErr .takeError ());
36073625 return " <?>" ;
36083626 }
36093627
3610- if (ArrOrErr-> empty () )
3628+ if (*ShStrNdxOrErr == ELF::SHN_XINDEX )
36113629 return " 65535 (corrupt: out of range)" ;
3612- return to_string (ElfHeader.e_shstrndx ) + " (" +
3613- to_string ((*ArrOrErr)[0 ].sh_link ) + " )" ;
3630+ return " 65535 (" + to_string (*ShStrNdxOrErr) + " )" ;
36143631}
36153632
36163633static const EnumEntry<unsigned > *getObjectFileEnumEntry (unsigned Type) {
@@ -3765,7 +3782,7 @@ template <class ELFT> void GNUELFDumper<ELFT>::printFileHeaders() {
37653782 printFields (OS, " Size of this header:" , Str);
37663783 Str = to_string (e.e_phentsize ) + " (bytes)" ;
37673784 printFields (OS, " Size of program headers:" , Str);
3768- Str = to_string (e. e_phnum );
3785+ Str = getProgramHeadersNumString ( this -> Obj , this -> FileName );
37693786 printFields (OS, " Number of program headers:" , Str);
37703787 Str = to_string (e.e_shentsize ) + " (bytes)" ;
37713788 printFields (OS, " Size of section headers:" , Str);
@@ -4778,8 +4795,10 @@ void GNUELFDumper<ELFT>::printProgramHeaders(
47784795 return ;
47794796
47804797 if (PrintProgramHeaders) {
4781- const Elf_Ehdr &Header = this ->Obj .getHeader ();
4782- if (Header.e_phnum == 0 ) {
4798+ Expected<uint32_t > PhNumOrErr = this ->Obj .getPhNum ();
4799+ if (!PhNumOrErr) {
4800+ OS << ' \n ' << errorToErrorCode (PhNumOrErr.takeError ()).message () << ' \n ' ;
4801+ } else if (*PhNumOrErr == 0 ) {
47834802 OS << " \n There are no program headers in this file.\n " ;
47844803 } else {
47854804 printProgramHeaders ();
@@ -4795,10 +4814,18 @@ template <class ELFT> void GNUELFDumper<ELFT>::printProgramHeaders() {
47954814 const Elf_Ehdr &Header = this ->Obj .getHeader ();
47964815 Field Fields[8 ] = {2 , 17 , 26 , 37 + Bias,
47974816 48 + Bias, 56 + Bias, 64 + Bias, 68 + Bias};
4817+ uint32_t PhNum;
4818+ if (Expected<uint32_t > PhNumOrErr = this ->Obj .getPhNum ())
4819+ PhNum = *PhNumOrErr;
4820+ else {
4821+ OS << ' \n ' << errorToErrorCode (PhNumOrErr.takeError ()).message () << ' \n ' ;
4822+ return ;
4823+ }
4824+
47984825 OS << " \n Elf file type is "
47994826 << enumToString (Header.e_type , ArrayRef (ElfObjectFileType)) << " \n "
48004827 << " Entry point " << format_hex (Header.e_entry , 3 ) << " \n "
4801- << " There are " << Header. e_phnum << " program headers,"
4828+ << " There are " << PhNum << " program headers,"
48024829 << " starting at offset " << Header.e_phoff << " \n\n "
48034830 << " Program Headers:\n " ;
48044831 if (ELFT::Is64Bits)
@@ -7470,7 +7497,8 @@ template <class ELFT> void LLVMELFDumper<ELFT>::printFileHeaders() {
74707497 W.printFlags (" Flags" , E.e_flags );
74717498 W.printNumber (" HeaderSize" , E.e_ehsize );
74727499 W.printNumber (" ProgramHeaderEntrySize" , E.e_phentsize );
7473- W.printNumber (" ProgramHeaderCount" , E.e_phnum );
7500+ W.printString (" ProgramHeaderCount" ,
7501+ getProgramHeadersNumString (this ->Obj , this ->FileName ));
74747502 W.printNumber (" SectionHeaderEntrySize" , E.e_shentsize );
74757503 W.printString (" SectionHeaderCount" ,
74767504 getSectionHeadersNumString (this ->Obj , this ->FileName ));
0 commit comments