[llvm-readobj] - Stop using `unwrapOrError` in `DumpStyle<ELFT>::getGroups()`

With this we are able to diagnose possible issues much better and
don't exit on an error.

Differential revision: https://reviews.llvm.org/D91867
This commit is contained in:
Georgii Rymar 2020-11-20 12:03:18 +03:00
parent fee78fb004
commit 4dcdf0df31
2 changed files with 270 additions and 22 deletions

View File

@ -48,28 +48,32 @@ FileHeader:
Sections:
- Name: .group
Type: SHT_GROUP
Link: .symtab
Link: [[SYMTAB1=.symtab]]
Info: foo
Members:
- SectionOrType: GRP_COMDAT
- SectionOrType: .text.foo
- SectionOrType: .rela.text.foo
- SectionOrType: [[MEMBER1=.rela.text.foo]]
ShSize: [[SECSIZE1=<none>]]
ShName: [[GROUP1SHNAME=<none>]]
- Name: .group1
Type: SHT_GROUP
Link: .symtab
Link: [[SYMTAB2=.symtab]]
Info: bar
Members:
- SectionOrType: GRP_COMDAT
- SectionOrType: [[TEXTBARNAME=.text.bar]]
- SectionOrType: [[MEMBER2=.text.bar]]
- SectionOrType: .rela.text.bar
ShSize: [[SECSIZE2=<none>]]
- Name: .text.foo
Type: SHT_PROGBITS
- Name: .rela.text.foo
Type: SHT_RELA
Link: .symtab
Info: .text.foo
- Name: .text.bar
Type: SHT_PROGBITS
- Name: .text.bar
Type: SHT_PROGBITS
ShName: [[TEXTBARSHNAME=<none>]]
- Name: .rela.text.bar
Type: SHT_RELA
Link: .symtab
@ -86,7 +90,7 @@ Symbols:
## Check that we report a warning and continue dumping when a section is included
## in two group sections at the same time.
# RUN: yaml2obj %s -DTEXTBARNAME=.text.foo -o %t.dup.o
# RUN: yaml2obj %s -DMEMBER2=.text.foo -o %t.dup.o
# RUN: llvm-readobj --elf-section-groups %t.dup.o 2>&1 | FileCheck %s -DFILE=%t.dup.o --check-prefix=DUP-LLVM
# RUN: llvm-readelf --elf-section-groups %t.dup.o 2>&1 | FileCheck %s -DFILE=%t.dup.o --check-prefix=DUP-GNU
@ -127,6 +131,11 @@ Symbols:
# DUP-GNU-NEXT: [ 3] .text.foo
# DUP-GNU-NEXT: [ 6] .rela.text.bar
## Check what we do when we are unable to dump the signature symbol name.
## In this case the index of the string table section, linked to the symbol table used by a group section,
## is broken (section does not exist).
## Check we report a warning in this case. Check we don't print the same warning message twice.
# RUN: yaml2obj %s -DSYMTABLINK=0xFF -o %t.symtab.o
# RUN: llvm-readobj --elf-section-groups %t.symtab.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.symtab.o %s --check-prefix=SYMTAB-LLVM --implicit-check-not=warning:
@ -172,3 +181,201 @@ Symbols:
# SYMTAB-GNU-NEXT: [Index] Name
# SYMTAB-GNU-NEXT: [ 5] .text.bar
# SYMTAB-GNU-NEXT: [ 6] .rela.text.bar
## This tests the behavior for two more cases when we are unable to dump the signature symbol name.
## In the first case we link the group section to the section with index 255, which does not exist.
## We check that a warning is reported when we are unable to locate the symbol table.
## In the second case we link the SHT_GROUP section to itself. This documents that we don't check the
## type of the linked section (we assume it is the symbol table) and checks that we report a warning
## when we are unable to read a signature symbol.
# RUN: yaml2obj %s -DSYMTAB1=0xFF -DSYMTAB2=0x1 -o %t.symtab2.o
# RUN: llvm-readobj --elf-section-groups %t.symtab2.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.symtab2.o %s --check-prefix=SIGNATURE-LLVM --implicit-check-not=warning:
# RUN: llvm-readelf --elf-section-groups %t.symtab2.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.symtab2.o %s --check-prefix=SIGNATURE-GNU --implicit-check-not=warning:
# SIGNATURE: Groups {
# SIGNATURE-LLVM: warning: '[[FILE]]': unable to get the symbol table for SHT_GROUP section with index 1: invalid section index: 255
# SIGNATURE-LLVM: warning: '[[FILE]]': unable to get the signature symbol for SHT_GROUP section with index 2: section [index 1] has invalid sh_entsize: expected 24, but got 4
# SIGNATURE-LLVM: Group {
# SIGNATURE-LLVM: Name: .group (16)
# SIGNATURE-LLVM: Index: 1
# SIGNATURE-LLVM: Link: 255
# SIGNATURE-LLVM: Info: 1
# SIGNATURE-LLVM: Type: COMDAT (0x1)
# SIGNATURE-LLVM: Signature: <?>
# SIGNATURE-LLVM: Section(s) in group [
# SIGNATURE-LLVM: .text.foo (3)
# SIGNATURE-LLVM: .rela.text.foo (4)
# SIGNATURE-LLVM: ]
# SIGNATURE-LLVM: }
# SIGNATURE-LLVM: Group {
# SIGNATURE-LLVM: Name: .group1 (64)
# SIGNATURE-LLVM: Index: 2
# SIGNATURE-LLVM: Link: 1
# SIGNATURE-LLVM: Info: 2
# SIGNATURE-LLVM: Type: COMDAT (0x1)
# SIGNATURE-LLVM: Signature: <?>
# SIGNATURE-LLVM: Section(s) in group [
# SIGNATURE-LLVM: .text.bar (5)
# SIGNATURE-LLVM: .rela.text.bar (6)
# SIGNATURE-LLVM: ]
# SIGNATURE-LLVM: }
# SIGNATURE-LLVM: }
# SIGNATURE-GNU: warning: '[[FILE]]': unable to get the symbol table for SHT_GROUP section with index 1: invalid section index: 255
# SIGNATURE-GNU-NEXT: warning: '[[FILE]]': unable to get the signature symbol for SHT_GROUP section with index 2: section [index 1] has invalid sh_entsize: expected 24, but got 4
# SIGNATURE-GNU-EMPTY:
# SIGNATURE-GNU-NEXT: COMDAT group section [ 1] `.group' [<?>] contains 2 sections:
# SIGNATURE-GNU-NEXT: [Index] Name
# SIGNATURE-GNU-NEXT: [ 3] .text.foo
# SIGNATURE-GNU-NEXT: [ 4] .rela.text.foo
# SIGNATURE-GNU-EMPTY:
# SIGNATURE-GNU-NEXT: COMDAT group section [ 2] `.group1' [<?>] contains 2 sections:
# SIGNATURE-GNU-NEXT: [Index] Name
# SIGNATURE-GNU-NEXT: [ 5] .text.bar
# SIGNATURE-GNU-NEXT: [ 6] .rela.text.bar
## Check we report a warning when the content of the group section is empty or can't be read.
## In both cases we are unable to read the section group flag and dump it as 0.
# RUN: yaml2obj %s -DSECSIZE1=0x0 -DSECSIZE2=0x1 -o %t.secsize.o
# RUN: llvm-readobj --elf-section-groups %t.secsize.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.secsize.o %s --check-prefix=CONTENT-LLVM --implicit-check-not=warning:
# RUN: llvm-readelf --elf-section-groups %t.secsize.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.secsize.o %s --check-prefix=CONTENT-GNU --implicit-check-not=warning:
# CONTENT-LLVM: Groups {
# CONTENT-LLVM-NEXT: warning: '[[FILE]]': unable to read the section group flag from the SHT_GROUP section with index 1: the section is empty
# CONTENT-LLVM-NEXT: warning: '[[FILE]]': unable to get the content of the SHT_GROUP section with index 2: section [index 2] has an invalid sh_size (1) which is not a multiple of its sh_entsize (4)
# CONTENT-LLVM-NEXT: Group {
# CONTENT-LLVM-NEXT: Name: .group (16)
# CONTENT-LLVM-NEXT: Index: 1
# CONTENT-LLVM-NEXT: Link: 7
# CONTENT-LLVM-NEXT: Info: 1
# CONTENT-LLVM-NEXT: Type: (unknown) (0x0)
# CONTENT-LLVM-NEXT: Signature: foo
# CONTENT-LLVM-NEXT: Section(s) in group [
# CONTENT-LLVM-NEXT: ]
# CONTENT-LLVM-NEXT: }
# CONTENT-LLVM-NEXT: Group {
# CONTENT-LLVM-NEXT: Name: .group1 (64)
# CONTENT-LLVM-NEXT: Index: 2
# CONTENT-LLVM-NEXT: Link: 7
# CONTENT-LLVM-NEXT: Info: 2
# CONTENT-LLVM-NEXT: Type: (unknown) (0x0)
# CONTENT-LLVM-NEXT: Signature: bar
# CONTENT-LLVM-NEXT: Section(s) in group [
# CONTENT-LLVM-NEXT: ]
# CONTENT-LLVM-NEXT: }
# CONTENT-LLVM-NEXT: }
# CONTENT-GNU: warning: '[[FILE]]': unable to read the section group flag from the SHT_GROUP section with index 1: the section is empty
# CONTENT-GNU: warning: '[[FILE]]': unable to get the content of the SHT_GROUP section with index 2: section [index 2] has an invalid sh_size (1) which is not a multiple of its sh_entsize (4)
# CONTENT-GNU-EMPTY:
# CONTENT-GNU-NEXT: (unknown) group section [ 1] `.group' [foo] contains 0 sections:
# CONTENT-GNU-NEXT: [Index] Name
# CONTENT-GNU-EMPTY:
# CONTENT-GNU-NEXT: (unknown) group section [ 2] `.group1' [bar] contains 0 sections:
# CONTENT-GNU-NEXT: [Index] Name
## Check that we emit a warning when we are unable to read the group section name or the name of a member.
# RUN: yaml2obj %s -DGROUP1SHNAME=0xAAAA -DTEXTBARSHNAME=0xBBBB -o %t.name.o
# RUN: llvm-readobj --elf-section-groups %t.name.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.name.o %s --check-prefix=NAME-LLVM --implicit-check-not=warning:
# RUN: llvm-readelf --elf-section-groups %t.name.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.name.o %s --check-prefix=NAME-GNU --implicit-check-not=warning:
# NAME-LLVM: Groups {
# NAME-LLVM-NEXT: warning: '[[FILE]]': unable to get the name of the SHT_GROUP section with index 1: a section [index 1] has an invalid sh_name (0xaaaa) offset which goes past the end of the section name string table
# NAME-LLVM-NEXT: warning: '[[FILE]]': unable to get the name of the SHT_PROGBITS section with index 5: a section [index 5] has an invalid sh_name (0xbbbb) offset which goes past the end of the section name string table
# NAME-LLVM-NEXT: Group {
# NAME-LLVM-NEXT: Name: <?> (43690)
# NAME-LLVM-NEXT: Index: 1
# NAME-LLVM-NEXT: Link: 7
# NAME-LLVM-NEXT: Info: 1
# NAME-LLVM-NEXT: Type: COMDAT (0x1)
# NAME-LLVM-NEXT: Signature: foo
# NAME-LLVM-NEXT: Section(s) in group [
# NAME-LLVM-NEXT: .text.foo (3)
# NAME-LLVM-NEXT: .rela.text.foo (4)
# NAME-LLVM-NEXT: ]
# NAME-LLVM-NEXT: }
# NAME-LLVM-NEXT: Group {
# NAME-LLVM-NEXT: Name: .group1 (64)
# NAME-LLVM-NEXT: Index: 2
# NAME-LLVM-NEXT: Link: 7
# NAME-LLVM-NEXT: Info: 2
# NAME-LLVM-NEXT: Type: COMDAT (0x1)
# NAME-LLVM-NEXT: Signature: bar
# NAME-LLVM-NEXT: Section(s) in group [
# NAME-LLVM-NEXT: <?> (5)
# NAME-LLVM-NEXT: .rela.text.bar (6)
# NAME-LLVM-NEXT: ]
# NAME-LLVM-NEXT: }
# NAME-LLVM-NEXT: }
# NAME-GNU: warning: '[[FILE]]': unable to get the name of the SHT_GROUP section with index 1: a section [index 1] has an invalid sh_name (0xaaaa) offset which goes past the end of the section name string table
# NAME-GNU-NEXT: warning: '[[FILE]]': unable to get the name of the SHT_PROGBITS section with index 5: a section [index 5] has an invalid sh_name (0xbbbb) offset which goes past the end of the section name string table
# NAME-GNU-EMPTY:
# NAME-GNU-NEXT: COMDAT group section [ 1] `<?>' [foo] contains 2 sections:
# NAME-GNU-NEXT: [Index] Name
# NAME-GNU-NEXT: [ 3] .text.foo
# NAME-GNU-NEXT: [ 4] .rela.text.foo
# NAME-GNU-EMPTY:
# NAME-GNU-NEXT: COMDAT group section [ 2] `.group1' [bar] contains 2 sections:
# NAME-GNU-NEXT: [Index] Name
# NAME-GNU-NEXT: [ 5] <?>
# NAME-GNU-NEXT: [ 6] .rela.text.bar
## Check we report a warning then the section index of a section group member is invalid.
# RUN: yaml2obj %s -DMEMBER1=0xEE -DMEMBER2=0xFF -o %t.member.index.o
# RUN: llvm-readobj --elf-section-groups %t.member.index.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.member.index.o %s --check-prefix=MEMBER-LLVM --implicit-check-not=warning:
# RUN: llvm-readelf --elf-section-groups %t.member.index.o 2>&1 | \
# RUN: FileCheck -DFILE=%t.member.index.o %s --check-prefix=MEMBER-GNU --implicit-check-not=warning:
# MEMBER-LLVM: Groups {
# MEMBER-LLVM-NEXT: warning: '[[FILE]]': unable to get the section with index 238 when dumping the SHT_GROUP section with index 1: invalid section index: 238
# MEMBER-LLVM-NEXT: warning: '[[FILE]]': unable to get the section with index 255 when dumping the SHT_GROUP section with index 2: invalid section index: 255
# MEMBER-LLVM-NEXT: Group {
# MEMBER-LLVM-NEXT: Name: .group (16)
# MEMBER-LLVM-NEXT: Index: 1
# MEMBER-LLVM-NEXT: Link: 7
# MEMBER-LLVM-NEXT: Info: 1
# MEMBER-LLVM-NEXT: Type: COMDAT (0x1)
# MEMBER-LLVM-NEXT: Signature: foo
# MEMBER-LLVM-NEXT: Section(s) in group [
# MEMBER-LLVM-NEXT: .text.foo (3)
# MEMBER-LLVM-NEXT: <?> (238)
# MEMBER-LLVM-NEXT: ]
# MEMBER-LLVM-NEXT: }
# MEMBER-LLVM-NEXT: Group {
# MEMBER-LLVM-NEXT: Name: .group1 (64)
# MEMBER-LLVM-NEXT: Index: 2
# MEMBER-LLVM-NEXT: Link: 7
# MEMBER-LLVM-NEXT: Info: 2
# MEMBER-LLVM-NEXT: Type: COMDAT (0x1)
# MEMBER-LLVM-NEXT: Signature: bar
# MEMBER-LLVM-NEXT: Section(s) in group [
# MEMBER-LLVM-NEXT: <?> (255)
# MEMBER-LLVM-NEXT: .rela.text.bar (6)
# MEMBER-LLVM-NEXT: ]
# MEMBER-LLVM-NEXT: }
# MEMBER-LLVM-NEXT: }
# MEMBER-GNU: warning: '[[FILE]]': unable to get the section with index 238 when dumping the SHT_GROUP section with index 1: invalid section index: 238
# MEMBER-GNU-NEXT: warning: '[[FILE]]': unable to get the section with index 255 when dumping the SHT_GROUP section with index 2: invalid section index: 255
# MEMBER-GNU-EMPTY:
# MEMBER-GNU-NEXT: COMDAT group section [ 1] `.group' [foo] contains 2 sections:
# MEMBER-GNU-NEXT: [Index] Name
# MEMBER-GNU-NEXT: [ 3] .text.foo
# MEMBER-GNU-NEXT: [ 238] <?>
# MEMBER-GNU-EMPTY:
# MEMBER-GNU-NEXT: COMDAT group section [ 2] `.group1' [bar] contains 2 sections:
# MEMBER-GNU-NEXT: [Index] Name
# MEMBER-GNU-NEXT: [ 255] <?>
# MEMBER-GNU-NEXT: [ 6] .rela.text.bar

View File

@ -802,6 +802,7 @@ public:
virtual void printMipsPLT(const MipsGOTParser<ELFT> &Parser) = 0;
virtual void printMipsABIFlags() = 0;
const ELFDumper<ELFT> &dumper() const { return Dumper; }
void reportUniqueWarning(Error Err) const;
protected:
std::vector<GroupSection> getGroups();
@ -827,8 +828,6 @@ protected:
StringRef getPrintableSectionName(const Elf_Shdr &Sec) const;
void reportUniqueWarning(Error Err) const;
StringRef FileName;
const ELFFile<ELFT> &Obj;
const ELFObjectFile<ELFT> &ElfObj;
@ -3582,6 +3581,19 @@ template <class ELFT> void GNUStyle<ELFT>::printFileHeaders() {
printFields(OS, "Section header string table index:", Str);
}
template <class ELFT>
static StringRef tryGetSectionName(const ELFFile<ELFT> &Obj,
const typename ELFT::Shdr &Sec,
DumpStyle<ELFT> &Dump) {
if (Expected<StringRef> SecNameOrErr = Obj.getSectionName(Sec))
return *SecNameOrErr;
else
Dump.reportUniqueWarning(createError("unable to get the name of the " +
describe(Obj, Sec) + ": " +
toString(SecNameOrErr.takeError())));
return "<?>";
}
template <class ELFT> std::vector<GroupSection> DumpStyle<ELFT>::getGroups() {
auto GetSignature = [&](const Elf_Sym &Sym,
const Elf_Shdr &Symtab) -> StringRef {
@ -3605,30 +3617,59 @@ template <class ELFT> std::vector<GroupSection> DumpStyle<ELFT>::getGroups() {
if (Sec.sh_type != ELF::SHT_GROUP)
continue;
const Elf_Shdr *Symtab =
unwrapOrError(FileName, Obj.getSection(Sec.sh_link));
StringRef Signature = "<?>";
if (Expected<const Elf_Shdr *> SymtabOrErr = Obj.getSection(Sec.sh_link)) {
if (Expected<const Elf_Sym *> SymOrErr =
Obj.template getEntry<Elf_Sym>(**SymtabOrErr, Sec.sh_info))
Signature = GetSignature(**SymOrErr, **SymtabOrErr);
else
reportUniqueWarning(createError(
"unable to get the signature symbol for " + describe(Obj, Sec) +
": " + toString(SymOrErr.takeError())));
} else {
reportUniqueWarning(createError("unable to get the symbol table for " +
describe(Obj, Sec) + ": " +
toString(SymtabOrErr.takeError())));
}
const Elf_Sym *Sym = unwrapOrError(
FileName, Obj.template getEntry<Elf_Sym>(*Symtab, Sec.sh_info));
auto Data = unwrapOrError(
FileName, Obj.template getSectionContentsAsArray<Elf_Word>(Sec));
ArrayRef<Elf_Word> Data;
if (Expected<ArrayRef<Elf_Word>> ContentsOrErr =
Obj.template getSectionContentsAsArray<Elf_Word>(Sec)) {
if (ContentsOrErr->empty())
reportUniqueWarning(
createError("unable to read the section group flag from the " +
describe(Obj, Sec) + ": the section is empty"));
else
Data = *ContentsOrErr;
} else {
reportUniqueWarning(createError("unable to get the content of the " +
describe(Obj, Sec) + ": " +
toString(ContentsOrErr.takeError())));
}
StringRef Name = unwrapOrError(FileName, Obj.getSectionName(Sec));
StringRef Signature = GetSignature(*Sym, *Symtab);
Ret.push_back({Name,
Ret.push_back({tryGetSectionName(Obj, Sec, *this),
maybeDemangle(Signature),
Sec.sh_name,
I - 1,
Sec.sh_link,
Sec.sh_info,
Data[0],
Data.empty() ? Elf_Word(0) : Data[0],
{}});
if (Data.empty())
continue;
std::vector<GroupMember> &GM = Ret.back().Members;
for (uint32_t Ndx : Data.slice(1)) {
const Elf_Shdr &Sec = *unwrapOrError(FileName, Obj.getSection(Ndx));
const StringRef Name = unwrapOrError(FileName, Obj.getSectionName(Sec));
GM.push_back({Name, Ndx});
if (Expected<const Elf_Shdr *> SecOrErr = Obj.getSection(Ndx)) {
GM.push_back({tryGetSectionName(Obj, **SecOrErr, *this), Ndx});
} else {
reportUniqueWarning(
createError("unable to get the section with index " + Twine(Ndx) +
" when dumping the " + describe(Obj, Sec) + ": " +
toString(SecOrErr.takeError())));
GM.push_back({"<?>", Ndx});
}
}
}
return Ret;