diff --git a/llvm/include/llvm/Object/ELFObjectFile.h b/llvm/include/llvm/Object/ELFObjectFile.h --- a/llvm/include/llvm/Object/ELFObjectFile.h +++ b/llvm/include/llvm/Object/ELFObjectFile.h @@ -625,12 +625,18 @@ if (ESym->getType() == ELF::STT_FILE || ESym->getType() == ELF::STT_SECTION) Result |= SymbolRef::SF_FormatSpecific; - auto DotSymtabSecSyms = EF.symbols(DotSymtabSec); - if (DotSymtabSecSyms && ESym == (*DotSymtabSecSyms).begin()) - Result |= SymbolRef::SF_FormatSpecific; - auto DotDynSymSecSyms = EF.symbols(DotDynSymSec); - if (DotDynSymSecSyms && ESym == (*DotDynSymSecSyms).begin()) - Result |= SymbolRef::SF_FormatSpecific; + if (auto DotSymtabSecSymsOrErr = EF.symbols(DotSymtabSec)) { + if (ESym == (*DotSymtabSecSymsOrErr).begin()) + Result |= SymbolRef::SF_FormatSpecific; + } else + // TODO: Actually report errors helpfully. + consumeError(DotSymtabSecSymsOrErr.takeError()); + if (auto DotDynSymSecSymsOrErr = EF.symbols(DotDynSymSec)) { + if (ESym == (*DotDynSymSecSymsOrErr).begin()) + Result |= SymbolRef::SF_FormatSpecific; + } else + // TODO: Actually report errors helpfully. + consumeError(DotDynSymSecSymsOrErr.takeError()); if (EF.getHeader()->e_machine == ELF::EM_ARM) { if (Expected NameOrErr = getSymbolName(Sym)) { diff --git a/llvm/test/tools/llvm-nm/X86/invalid-section-size.test b/llvm/test/tools/llvm-nm/X86/invalid-section-size.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-nm/X86/invalid-section-size.test @@ -0,0 +1,22 @@ +## This test ensures llvm-nm will not crash when dumping symbol table +## whose sh_size isn't a multiple of the symbol size (sh_size % sizeof(Elf_Sym) != 0). +## It will be helpful if we could warn about this in future. + +# RUN: yaml2obj -DBITS=32 -DSIZE=33 %s -o %t.32-bit.o +# RUN: llvm-nm %t.32-bit.o | FileCheck %s +# RUN: yaml2obj -DBITS=64 -DSIZE=49 %s -o %t.64-bit.o +# RUN: llvm-nm %t.64-bit.o | FileCheck %s + +# CHECK: U +# CHECK-EMPTY: + +--- !ELF +FileHeader: + Class: ELFCLASS[[BITS]] + Data: ELFDATA2LSB + Type: ET_DYN + Machine: EM_386 +Sections: + - Name: .symtab + Type: SHT_SYMTAB + Size: [[SIZE]]