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 @@ -238,6 +238,7 @@ using Elf_Rel = typename ELFT::Rel; using Elf_Rela = typename ELFT::Rela; using Elf_Dyn = typename ELFT::Dyn; + using Elf_Sym_Range = typename ELFT::SymRange; SectionRef toSectionRef(const Elf_Shdr *Sec) const { return SectionRef(toDRI(Sec), this); @@ -625,11 +626,17 @@ 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()) + auto CheckSymbol = [&](const Elf_Shdr *Sec) { + if (Expected SymbolsOrErr = EF.symbols(Sec)) { + if (ESym == SymbolsOrErr->begin()) + return true; + } else + // TODO: Actually report errors helpfully. + consumeError(SymbolsOrErr.takeError()); + return false; + }; + + if (CheckSymbol(DotSymtabSec) || CheckSymbol(DotDynSymSec)) Result |= SymbolRef::SF_FormatSpecific; if (EF.getHeader()->e_machine == ELF::EM_ARM) { diff --git a/llvm/test/Object/invalid-symtab-size.test b/llvm/test/Object/invalid-symtab-size.test new file mode 100644 --- /dev/null +++ b/llvm/test/Object/invalid-symtab-size.test @@ -0,0 +1,22 @@ +## This test llvm will not crash when dumping a symbol table +## whose sh_size isn't a multiple of the symbol size (sh_size % sizeof(Elf_Sym) != 0). +## TODO: It would 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 2>&1 | FileCheck --implicit-check-not=warning %s +# RUN: yaml2obj -DBITS=64 -DSIZE=49 %s -o %t.64-bit.o +# RUN: llvm-nm %t.64-bit.o 2>&1 | FileCheck --implicit-check-not=warning %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]]