llvm.org GIT mirror llvm / f9750b5
[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 George Rimar 3 months ago
3 changed file(s) with 32 addition(s) and 7 deletion(s). Raw diff Collapse all Expand all
0 ## wrong-shstrtab-type.elf-x86-64 contains .shstrtab section which has SHT_PROGBITS type.
1 ## Check we do not fail to dump the section headers in this case.
2
3 # RUN: llvm-readobj -S %p/Inputs/wrong-shstrtab-type.elf-x86-64 | FileCheck %s --check-prefix LLVM
4 # RUN: llvm-readelf -S %p/Inputs/wrong-shstrtab-type.elf-x86-64 | FileCheck %s --check-prefix GNU
5
6 # LLVM: Name: .shstrtab
7 # LLVM-NEXT: Type: SHT_PROGBITS
8
9 # GNU: [Nr] Name Type
10 # GNU: [ 3] .shstrtab PROGBITS
30033003 }
30043004
30053005 template
3006 static StringRef getSectionName(const typename ELFT::Shdr &Sec,
3007 const ELFFile &Obj,
3008 ArrayRef Sections) {
3009 uint32_t Index = Obj.getHeader()->e_shstrndx;
3010 if (Index == ELF::SHN_XINDEX)
3011 Index = Sections[0].sh_link;
3012 if (!Index) // no section string table.
3013 return "";
3014 if (Index >= Sections.size())
3015 reportError("invalid section index");
3016 StringRef Data = toStringRef(unwrapOrError(
3017 Obj.template getSectionContentsAsArray(&Sections[Index])));
3018 return unwrapOrError(Obj.getSectionName(&Sec, Data));
3019 }
3020
3021 template
30063022 void GNUStyle::printSectionHeaders(const ELFO *Obj) {
30073023 unsigned Bias = ELFT::Is64Bits ? 0 : 8;
30083024 ArrayRef Sections = unwrapOrError(Obj->sections());
30223038 size_t SectionIndex = 0;
30233039 for (const Elf_Shdr &Sec : Sections) {
30243040 Fields[0].Str = to_string(SectionIndex);
3025 Fields[1].Str = unwrapOrError(Obj->getSectionName(&Sec));
3041 Fields[1].Str = getSectionName(Sec, *Obj, Sections);
30263042 Fields[2].Str =
30273043 getSectionTypeString(Obj->getHeader()->e_machine, Sec.sh_type);
30283044 Fields[3].Str =
45684584 ListScope SectionsD(W, "Sections");
45694585
45704586 int SectionIndex = -1;
4571 for (const Elf_Shdr &Sec : unwrapOrError(Obj->sections())) {
4572 ++SectionIndex;
4573
4574 StringRef Name = unwrapOrError(Obj->getSectionName(&Sec));
4575
4587 ArrayRef Sections = unwrapOrError(Obj->sections());
4588 for (const Elf_Shdr &Sec : Sections) {
4589 StringRef Name = getSectionName(Sec, *Obj, Sections);
45764590 DictScope SectionD(W, "Section");
4577 W.printNumber("Index", SectionIndex);
4591 W.printNumber("Index", ++SectionIndex);
45784592 W.printNumber("Name", Name, Sec.sh_name);
45794593 W.printHex(
45804594 "Type",