From 885d7e93bf0cc1f329cb6efe31627c01f5efb7b6 Mon Sep 17 00:00:00 2001 From: Eric Rahm Date: Wed, 3 Dec 2025 18:16:03 +0000 Subject: [PATCH 1/2] Allow undefined symbol names When processing symbol table names an error would be thrown if the the name was undefined (0) resulting in failures to process files like: > bloaty: can't read index 0 from strtab, total size is 254049 This modifies the symbol table processing to return an empty string if the name is undefined. An empty string will trigger the existing logic to generate a synthetic "Anonymous" name instead of crashing. Fixes: #217 Fixes: #345 --- src/elf.cc | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/elf.cc b/src/elf.cc index 5acfc8c..c6ebab2 100644 --- a/src/elf.cc +++ b/src/elf.cc @@ -377,7 +377,7 @@ string_view ElfFile::Section::GetName() const { string_view ElfFile::Section::ReadString(Elf64_Word index) const { assert(header().sh_type == SHT_STRTAB); - if (index == SHN_UNDEF || index >= contents_.size()) { + if (index >= contents_.size()) { THROWF("can't read index $0 from strtab, total size is $1", index, contents_.size()); } @@ -972,8 +972,7 @@ static void ReadELFSymbolTableEntries(const ElfFile& elf, section.ReadSymbol(i, &sym, &sym_range); if (ELF64_ST_TYPE(sym.st_info) == STT_SECTION || - sym.st_shndx == STN_UNDEF || - sym.st_name == SHN_UNDEF) { + sym.st_shndx == STN_UNDEF) { continue; } From 1aa76be254dc1d7f401098f39ac9869e6b1d3f04 Mon Sep 17 00:00:00 2001 From: Eric Rahm Date: Wed, 3 Dec 2025 18:13:56 +0000 Subject: [PATCH 2/2] Add unamed symbol test Adds a lit test to make sure an "Anonymous" entry is emitted if a symbol table entry has an undefined name. --- tests/elf/unnamed_symbol.test | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 tests/elf/unnamed_symbol.test diff --git a/tests/elf/unnamed_symbol.test b/tests/elf/unnamed_symbol.test new file mode 100644 index 0000000..1f7fa35 --- /dev/null +++ b/tests/elf/unnamed_symbol.test @@ -0,0 +1,32 @@ +# Test that bloaty handles symbols with no name (st_name == 0) by creating a +# synthetic name. + +# RUN: %yaml2obj %s -o %t.obj +# RUN: %bloaty %t.obj -d symbols | %FileCheck %s + +# CHECK: named_symbol +# CHECK: [Anonymous symbol #1] + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x10 + Content: '9090909090909090909090909090909090909090909090909090909090909090' +Symbols: + - StName: 0 + Type: STT_FUNC + Section: .text + Value: 0x0 + Size: 0x10 + - Name: named_symbol + Type: STT_FUNC + Section: .text + Value: 0x10 + Size: 0x10