From f9750b5becde9abb0a11731b5a9fad996ba9721c Mon Sep 17 00:00:00 2001 From: George Rimar Date: Fri, 14 Jun 2019 11:56:10 +0000 Subject: [PATCH] [llvm-readobj] - Do not fail to dump the object which has wrong type of .shstrtab. Imagine we have object that has .shstrtab with type != SHT_STRTAB. In this case, we fail to dump the object, though GNU readelf dumps it without any issues and warnings. This patch fixes that. It adds a code to ELFDumper.cpp which is based on the implementation of getSectionName from the ELF.h: https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Object/ELF.h#L608 https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Object/ELF.h#L431 https://github.com/llvm-mirror/llvm/blob/master/include/llvm/Object/ELF.h#L539 The difference is that all non critical errors are ommitted what allows us to improve the dumping on a tool side. Also, this opens a road for a follow-up that should allow us to dump the section headers, but drop the section names in case if .shstrtab is completely absent and/or broken. Differential revision: https://reviews.llvm.org/D63266 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363371 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Inputs/wrong-shstrtab-type.elf-x86-64 | Bin 0 -> 541 bytes .../llvm-readobj/elf-wrong-shstrtab-type.test | 11 +++++++ tools/llvm-readobj/ELFDumper.cpp | 28 +++++++++++++----- 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64 create mode 100644 test/tools/llvm-readobj/elf-wrong-shstrtab-type.test diff --git a/test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64 b/test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64 new file mode 100644 index 0000000000000000000000000000000000000000..801a21be823f14bc3744f251cc42cba58368e692 GIT binary patch literal 541 zcmb<-^>JfjWMqH=W(GS31doBi0V)BbL0kt03oyyXzzpJ21eAfgk?n&Sb^t0U0Tp0^ zNkD0sxHOQDP5%w3AT~Kc#OR3B&E-7LF)0Mdldc_$)Zb@PikOiYFb3uHZ0070s6(s-w literal 0 HcmV?d00001 diff --git a/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test b/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test new file mode 100644 index 00000000000..2d5fd68cd57 --- /dev/null +++ b/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test @@ -0,0 +1,11 @@ +## wrong-shstrtab-type.elf-x86-64 contains .shstrtab section which has SHT_PROGBITS type. +## Check we do not fail to dump the section headers in this case. + +# RUN: llvm-readobj -S %p/Inputs/wrong-shstrtab-type.elf-x86-64 | FileCheck %s --check-prefix LLVM +# RUN: llvm-readelf -S %p/Inputs/wrong-shstrtab-type.elf-x86-64 | FileCheck %s --check-prefix GNU + +# LLVM: Name: .shstrtab +# LLVM-NEXT: Type: SHT_PROGBITS + +# GNU: [Nr] Name Type +# GNU: [ 3] .shstrtab PROGBITS diff --git a/tools/llvm-readobj/ELFDumper.cpp b/tools/llvm-readobj/ELFDumper.cpp index 5efa3519719..e751e6db9eb 100644 --- a/tools/llvm-readobj/ELFDumper.cpp +++ b/tools/llvm-readobj/ELFDumper.cpp @@ -3003,6 +3003,22 @@ static std::string getSectionTypeString(unsigned Arch, unsigned Type) { return ""; } +template +static StringRef getSectionName(const typename ELFT::Shdr &Sec, + const ELFFile &Obj, + ArrayRef Sections) { + uint32_t Index = Obj.getHeader()->e_shstrndx; + if (Index == ELF::SHN_XINDEX) + Index = Sections[0].sh_link; + if (!Index) // no section string table. + return ""; + if (Index >= Sections.size()) + reportError("invalid section index"); + StringRef Data = toStringRef(unwrapOrError( + Obj.template getSectionContentsAsArray(&Sections[Index]))); + return unwrapOrError(Obj.getSectionName(&Sec, Data)); +} + template void GNUStyle::printSectionHeaders(const ELFO *Obj) { unsigned Bias = ELFT::Is64Bits ? 0 : 8; @@ -3023,7 +3039,7 @@ void GNUStyle::printSectionHeaders(const ELFO *Obj) { size_t SectionIndex = 0; for (const Elf_Shdr &Sec : Sections) { Fields[0].Str = to_string(SectionIndex); - Fields[1].Str = unwrapOrError(Obj->getSectionName(&Sec)); + Fields[1].Str = getSectionName(Sec, *Obj, Sections); Fields[2].Str = getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type); Fields[3].Str = @@ -4569,13 +4585,11 @@ void LLVMStyle::printSectionHeaders(const ELFO *Obj) { ListScope SectionsD(W, "Sections"); int SectionIndex = -1; - for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) { - ++SectionIndex; - - StringRef Name = unwrapOrError(Obj->getSectionName(&Sec)); - + ArrayRef Sections = unwrapOrError(Obj->sections()); + for (const Elf_Shdr &Sec : Sections) { + StringRef Name = getSectionName(Sec, *Obj, Sections); DictScope SectionD(W, "Section"); - W.printNumber("Index", SectionIndex); + W.printNumber("Index", ++SectionIndex); W.printNumber("Name", Name, Sec.sh_name); W.printHex( "Type", -- 2.40.0