[llvm] r363371 - [llvm-readobj] - Do not fail to dump the object which has wrong type of .shstrtab.
George Rimar via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 14 04:56:11 PDT 2019
Author: grimar
Date: Fri Jun 14 04:56:10 2019
New Revision: 363371
URL: http://llvm.org/viewvc/llvm-project?rev=363371&view=rev
Log:
[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
Added:
llvm/trunk/test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64 (with props)
llvm/trunk/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test
Modified:
llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
Added: llvm/trunk/test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64?rev=363371&view=auto
==============================================================================
Binary file - no diff available.
Propchange: llvm/trunk/test/tools/llvm-readobj/Inputs/wrong-shstrtab-type.elf-x86-64
------------------------------------------------------------------------------
svn:mime-type = application/octet-stream
Added: llvm/trunk/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test?rev=363371&view=auto
==============================================================================
--- llvm/trunk/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test (added)
+++ llvm/trunk/test/tools/llvm-readobj/elf-wrong-shstrtab-type.test Fri Jun 14 04:56:10 2019
@@ -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
Modified: llvm/trunk/tools/llvm-readobj/ELFDumper.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-readobj/ELFDumper.cpp?rev=363371&r1=363370&r2=363371&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-readobj/ELFDumper.cpp (original)
+++ llvm/trunk/tools/llvm-readobj/ELFDumper.cpp Fri Jun 14 04:56:10 2019
@@ -3004,6 +3004,22 @@ static std::string getSectionTypeString(
}
template <class ELFT>
+static StringRef getSectionName(const typename ELFT::Shdr &Sec,
+ const ELFFile<ELFT> &Obj,
+ ArrayRef<typename ELFT::Shdr> 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<uint8_t>(&Sections[Index])));
+ return unwrapOrError(Obj.getSectionName(&Sec, Data));
+}
+
+template <class ELFT>
void GNUStyle<ELFT>::printSectionHeaders(const ELFO *Obj) {
unsigned Bias = ELFT::Is64Bits ? 0 : 8;
ArrayRef<Elf_Shdr> Sections = unwrapOrError(Obj->sections());
@@ -3023,7 +3039,7 @@ void GNUStyle<ELFT>::printSectionHeaders
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<ELFT>::printSectionHeader
ListScope SectionsD(W, "Sections");
int SectionIndex = -1;
- for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
- ++SectionIndex;
-
- StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
-
+ ArrayRef<Elf_Shdr> 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",
More information about the llvm-commits
mailing list