diff --git a/llvm/test/Object/invalid.test b/llvm/test/Object/invalid.test --- a/llvm/test/Object/invalid.test +++ b/llvm/test/Object/invalid.test @@ -343,7 +343,7 @@ # RUN: yaml2obj %s --docnum=16 -o %t16 # RUN: llvm-readobj --sections %t16 2>&1 | FileCheck -DFILE=%t16 --check-prefix=BROKEN-SECNAME %s -## BROKEN-SECNAME: warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0xb) offset which goes past the end of the section name string table +## BROKEN-SECNAME: warning: '[[FILE]]': unable to get the name of SHT_STRTAB section with index 2: a section [index 2] has an invalid sh_name (0xb) offset which goes past the end of the section name string table --- !ELF FileHeader: diff --git a/llvm/test/tools/llvm-readobj/ELF/dependent-libraries.test b/llvm/test/tools/llvm-readobj/ELF/dependent-libraries.test --- a/llvm/test/tools/llvm-readobj/ELF/dependent-libraries.test +++ b/llvm/test/tools/llvm-readobj/ELF/dependent-libraries.test @@ -72,7 +72,7 @@ # MIX-GNU-NEXT: [ 0] bar # MIX-GNU-NEXT: [ 4] xxx # MIX-GNU-EMPTY: -# MIX-GNU-NEXT: warning: '[[FILE]]': cannot get section name of SHT_LLVM_DEPENDENT_LIBRARIES section: a section [index 6] has an invalid sh_name (0x10000) offset which goes past the end of the section name string table +# MIX-GNU-NEXT: warning: '[[FILE]]': unable to get the name of SHT_LLVM_DEPENDENT_LIBRARIES section with index 6: a section [index 6] has an invalid sh_name (0x10000) offset which goes past the end of the section name string table # MIX-GNU-NEXT: Dependent libraries section at offset 0x53 contains 1 entries: # MIX-GNU-NEXT: [ 0] baz diff --git a/llvm/test/tools/llvm-readobj/ELF/invalid-shstrndx.test b/llvm/test/tools/llvm-readobj/ELF/invalid-shstrndx.test --- a/llvm/test/tools/llvm-readobj/ELF/invalid-shstrndx.test +++ b/llvm/test/tools/llvm-readobj/ELF/invalid-shstrndx.test @@ -20,9 +20,9 @@ # LLVM: StringTableSectionIndex: 255 # LLVM-NEXT: } # LLVM-NEXT: Sections [ -# LLVM-NEXT: warning: '[[FILE]]': section header string table index 255 does not exist # LLVM-NEXT: Section { # LLVM-NEXT: Index: 0 +# LLVM-NEXT: warning: '[[FILE]]': unable to get the name of SHT_NULL section with index 0: section header string table index 255 does not exist # LLVM-NEXT: Name: (0) # LLVM-NEXT: Type: SHT_NULL (0x0) # LLVM-NEXT: Flags [ (0x0) @@ -36,8 +36,10 @@ # LLVM-NEXT: EntrySize: 0 # LLVM-NEXT: } # LLVM: Index: 1 +# LLVM-NEXT: warning: '[[FILE]]': unable to get the name of SHT_STRTAB section with index 1: section header string table index 255 does not exist # LLVM-NEXT: Name: (11) # LLVM: Index: 2 +# LLVM-NEXT: warning: '[[FILE]]': unable to get the name of SHT_STRTAB section with index 2: section header string table index 255 does not exist # LLVM-NEXT: Name: (1) --- !ELF diff --git a/llvm/test/tools/llvm-readobj/ELF/relocations.test b/llvm/test/tools/llvm-readobj/ELF/relocations.test --- a/llvm/test/tools/llvm-readobj/ELF/relocations.test +++ b/llvm/test/tools/llvm-readobj/ELF/relocations.test @@ -125,9 +125,10 @@ - Name: .text Type: SHT_PROGBITS Content: '00FFFFFFFF020000001234567890ABCDEF' - - Name: .rel.text - Type: SHT_REL - Info: .text + - Name: .rel.text + Type: SHT_REL + Info: .text + ShName: [[SHNAME=]] Relocations: - Symbol: rel_0 Type: R_X86_64_NONE @@ -143,6 +144,7 @@ - Name: .rela.text Type: SHT_RELA Info: .text + ShName: [[SHNAME=]] Relocations: - Symbol: rela_0 Type: R_X86_64_NONE @@ -369,3 +371,42 @@ - Name: rela_maxpos Section: .text Value: 0xFFFFFFFF + +## Check we report a warning when the name of a relocation section can't be read. Check we continue dumping. +# RUN: yaml2obj %s --docnum=1 -DSHNAME=0xffffffff -o %tshname +# RUN: llvm-readobj --relocs %tshname 2>&1 | FileCheck %s -DFILE=%tshname --check-prefix=LLVM-SHNAME +# RUN: llvm-readelf --relocs %tshname 2>&1 | FileCheck %s -DFILE=%tshname --check-prefix=GNU-SHNAME + +# LLVM-SHNAME: Relocations [ +# LLVM-SHNAME-NEXT: warning: '[[FILE]]': unable to get the name of SHT_REL section with index 2: a section [index 2] has an invalid sh_name (0xffffffff) offset which goes past the end of the section name string table +# LLVM-SHNAME-NEXT: Section (2) { +# LLVM-SHNAME-NEXT: 0x0 R_X86_64_NONE rel_0 0x0 +# LLVM-SHNAME-NEXT: 0x1 R_X86_64_PC32 rel_neg 0x0 +# LLVM-SHNAME-NEXT: 0x5 R_X86_64_PLT32 rel_pos 0x0 +# LLVM-SHNAME-NEXT: 0x9 R_X86_64_64 rel_64 0x0 +# LLVM-SHNAME-NEXT: } +# LLVM-SHNAME-NEXT: warning: '[[FILE]]': unable to get the name of SHT_RELA section with index 3: a section [index 3] has an invalid sh_name (0xffffffff) offset which goes past the end of the section name string table +# LLVM-SHNAME-NEXT: Section (3) { +# LLVM-SHNAME-NEXT: 0x0 R_X86_64_NONE rela_0 0x0 +# LLVM-SHNAME-NEXT: 0x1 R_X86_64_PC32 rela_neg 0xFFFFFFFFFFFFFFFF +# LLVM-SHNAME-NEXT: 0x5 R_X86_64_PLT32 rela_pos 0x2 +# LLVM-SHNAME-NEXT: 0xFFFFFFFFFFFFFFFF R_X86_64_64 rela_minneg 0x8000000000000000 +# LLVM-SHNAME-NEXT: 0x9 R_X86_64_32S rela_maxpos 0x7FFFFFFFFFFFFFFF +# LLVM-SHNAME-NEXT: } +# LLVM-SHNAME-NEXT: ] + +# GNU-SHNAME: warning: '[[FILE]]': unable to get the name of SHT_REL section with index 2: a section [index 2] has an invalid sh_name (0xffffffff) offset which goes past the end of the section name string table +# GNU-SHNAME: Relocation section '' at offset 0x51 contains 4 entries: +# GNU-SHNAME-NEXT: Offset Info Type Symbol's Value Symbol's Name +# GNU-SHNAME-NEXT: 0000000000000000 0000000100000000 R_X86_64_NONE 0000000000000000 rel_0 +# GNU-SHNAME-NEXT: 0000000000000001 0000000200000002 R_X86_64_PC32 0000000000000001 rel_neg +# GNU-SHNAME-NEXT: 0000000000000005 0000000300000004 R_X86_64_PLT32 0000000000000002 rel_pos +# GNU-SHNAME-NEXT: 0000000000000009 0000000400000001 R_X86_64_64 ffffffffffffffff rel_64 +# GNU-SHNAME: warning: '[[FILE]]': unable to get the name of SHT_RELA section with index 3: a section [index 3] has an invalid sh_name (0xffffffff) offset which goes past the end of the section name string table +# GNU-SHNAME: Relocation section '' at offset 0x91 contains 5 entries: +# GNU-SHNAME-NEXT: Offset Info Type Symbol's Value Symbol's Name + Addend +# GNU-SHNAME-NEXT: 0000000000000000 0000000500000000 R_X86_64_NONE 0000000000000000 rela_0 + 0 +# GNU-SHNAME-NEXT: 0000000000000001 0000000600000002 R_X86_64_PC32 0000000000000001 rela_neg - 1 +# GNU-SHNAME-NEXT: 0000000000000005 0000000700000004 R_X86_64_PLT32 0000000000000002 rela_pos + 2 +# GNU-SHNAME-NEXT: ffffffffffffffff 0000000800000001 R_X86_64_64 0000000000000003 rela_minneg - 8000000000000000 +# GNU-SHNAME-NEXT: 0000000000000009 000000090000000b R_X86_64_32S ffffffffffffffff rela_maxpos + 7fffffffffffffff diff --git a/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test b/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test --- a/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test +++ b/llvm/test/tools/llvm-readobj/ELF/sections-no-section-header-string-table.test @@ -43,9 +43,9 @@ # LLVM-NEXT: AddressAlignment: 0 # LLVM-NEXT: EntrySize: 0 # LLVM-NEXT: } -# LLVM-NEXT: warning: '[[FILE]]': a section [index 2] has an invalid sh_name (0x1) offset which goes past the end of the section name string table # LLVM-NEXT: Section { # LLVM-NEXT: Index: 2 +# LLVM-NEXT: warning: '[[FILE]]': unable to get the name of SHT_PROGBITS section with index 2: a section [index 2] has an invalid sh_name (0x1) offset which goes past the end of the section name string table # LLVM-NEXT: Name: (1) # LLVM-NEXT: Type: SHT_PROGBITS (0x1) # LLVM-NEXT: Flags [ (0x0) @@ -58,9 +58,9 @@ # LLVM-NEXT: AddressAlignment: 0 # LLVM-NEXT: EntrySize: 0 # LLVM-NEXT: } -# LLVM-NEXT: warning: '[[FILE]]': a section [index 3] has an invalid sh_name (0x15) offset which goes past the end of the section name string table # LLVM-NEXT: Section { # LLVM-NEXT: Index: 3 +# LLVM-NEXT: warning: '[[FILE]]': unable to get the name of SHT_STRTAB section with index 3: a section [index 3] has an invalid sh_name (0x15) offset which goes past the end of the section name string table # LLVM-NEXT: Name: (21) # LLVM-NEXT: Type: SHT_STRTAB (0x3) # LLVM-NEXT: Flags [ (0x0) @@ -73,9 +73,9 @@ # LLVM-NEXT: AddressAlignment: 1 # LLVM-NEXT: EntrySize: 0 # LLVM-NEXT: } -# LLVM-NEXT: warning: '[[FILE]]': a section [index 4] has an invalid sh_name (0xb) offset which goes past the end of the section name string table # LLVM-NEXT: Section { # LLVM-NEXT: Index: 4 +# LLVM-NEXT: warning: '[[FILE]]': unable to get the name of SHT_STRTAB section with index 4: a section [index 4] has an invalid sh_name (0xb) offset which goes past the end of the section name string table # LLVM-NEXT: Name: (11) # LLVM-NEXT: Type: SHT_STRTAB (0x3) # LLVM-NEXT: Flags [ (0x0) diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp --- a/llvm/tools/llvm-readobj/ELFDumper.cpp +++ b/llvm/tools/llvm-readobj/ELFDumper.cpp @@ -778,6 +778,9 @@ virtual void printRelrReloc(const Elf_Relr &R) = 0; void printRelocationsHelper(const ELFFile *Obj, const Elf_Shdr &Sec); + StringRef getPrintableSectionName(const ELFFile *Obj, + const Elf_Shdr &Sec) const; + void reportUniqueWarning(Error Err) const; StringRef FileName; @@ -3732,7 +3735,6 @@ continue; HasRelocSections = true; - StringRef Name = unwrapOrError(this->FileName, Obj->getSectionName(&Sec)); unsigned Entries; // Android's packed relocation section needs to be unpacked first // to get the actual number of entries. @@ -3748,6 +3750,7 @@ } uintX_t Offset = Sec.sh_offset; + StringRef Name = this->getPrintableSectionName(Obj, Sec); OS << "\nRelocation section '" << Name << "' at offset 0x" << to_hexString(Offset, false) << " contains " << Entries << " entries:\n"; @@ -3877,17 +3880,8 @@ size_t Entries, bool NonVisibilityBitsUsed) { StringRef Name; - if (Symtab) { - if (Expected NameOrErr = Obj->getSectionName(Symtab)) { - Name = *NameOrErr; - } else { - this->reportUniqueWarning(createError("unable to get the name of " + - describe(Obj, *Symtab) + ": " + - toString(NameOrErr.takeError()))); - Name = ""; - } - } - + if (Symtab) + Name = this->getPrintableSectionName(Obj, *Symtab); if (!Name.empty()) OS << "\nSymbol table '" << Name << "'"; else @@ -5519,6 +5513,20 @@ } } +template +StringRef DumpStyle::getPrintableSectionName(const ELFFile *Obj, + const Elf_Shdr &Sec) const { + StringRef Name = ""; + if (Expected SecNameOrErr = + Obj->getSectionName(&Sec, this->dumper()->WarningHandler)) + Name = *SecNameOrErr; + else + this->reportUniqueWarning(createError("unable to get the name of " + + describe(Obj, Sec) + ": " + + toString(SecNameOrErr.takeError()))); + return Name; +} + template void GNUStyle::printDependentLibs(const ELFFile *Obj) { bool SectionStarted = false; @@ -5544,16 +5552,7 @@ PrintSection(); SectionStarted = true; Current.Offset = Shdr.sh_offset; - Expected Name = Obj->getSectionName(&Shdr); - if (!Name) { - Current.Name = ""; - this->reportUniqueWarning( - createError("cannot get section name of " - "SHT_LLVM_DEPENDENT_LIBRARIES section: " + - toString(Name.takeError()))); - } else { - Current.Name = *Name; - } + Current.Name = this->getPrintableSectionName(Obj, Shdr); }; auto OnLibEntry = [&](StringRef Lib, uint64_t Offset) { SecEntries.push_back(NameOffset{Lib, Offset}); @@ -6135,7 +6134,7 @@ if (!isRelocationSec(Sec)) continue; - StringRef Name = unwrapOrError(this->FileName, Obj->getSectionName(&Sec)); + StringRef Name = this->getPrintableSectionName(Obj, Sec); unsigned SecNdx = &Sec - &cantFail(Obj->sections()).front(); W.startLine() << "Section (" << SecNdx << ") " << Name << " {\n"; W.indent(); @@ -6205,16 +6204,9 @@ std::vector> FlagsList = getSectionFlagsForTarget(Obj->getHeader()->e_machine); for (const Elf_Shdr &Sec : cantFail(Obj->sections())) { - StringRef Name = ""; - if (Expected SecNameOrErr = - Obj->getSectionName(&Sec, this->dumper()->WarningHandler)) - Name = *SecNameOrErr; - else - this->reportUniqueWarning(SecNameOrErr.takeError()); - DictScope SectionD(W, "Section"); W.printNumber("Index", ++SectionIndex); - W.printNumber("Name", Name, Sec.sh_name); + W.printNumber("Name", this->getPrintableSectionName(Obj, Sec), Sec.sh_name); W.printHex( "Type", object::getELFSectionTypeName(Obj->getHeader()->e_machine, Sec.sh_type),