diff --git a/src/elf.cc b/src/elf.cc index c6ebab2..ac0f610 100644 --- a/src/elf.cc +++ b/src/elf.cc @@ -915,6 +915,18 @@ static void ReadELFSymbols(const InputFile& file, RangeSink* sink, name = name_storage; } + if (sym.st_shndx < SHN_LORESERVE) { + ElfFile::Section symbol_section; + elf.ReadSection(sym.st_shndx, &symbol_section); + if (!(symbol_section.header().sh_flags & SHF_ALLOC)) { + uint64_t offset = + symbol_section.header().sh_offset + + (sym.st_value - symbol_section.header().sh_addr); + sink->AddFileRange("elf_symbols", name, offset, sym.st_size); + continue; + } + } + uint64_t full_addr = ToVMAddr(sym.st_value, index_base + sym.st_shndx, is_object); if (sink && !(capstone_available && disassemble)) { diff --git a/tests/elf/non-alloc-symbols.test b/tests/elf/non-alloc-symbols.test new file mode 100644 index 0000000..463bf3d --- /dev/null +++ b/tests/elf/non-alloc-symbols.test @@ -0,0 +1,60 @@ +# Test that symbols in non-allocatable sections (missing SHF_ALLOC) are ignored +# by the "symbols" data source. +# +# This reproduces an issue where non-allocatable sections (like debug info or +# documentation) could be assigned address 0 by the linker, causing them to +# potentially overlap with valid allocatable sections at address 0 (or simply +# appear in the VM map when they shouldn't). + +# RUN: %yaml2obj %s -o %t.o +# RUN: %bloaty %t.o -d symbols --raw-map | %FileCheck %s + +# CHECK: FILE MAP: +# CHECK: KeepMe +# CHECK: DropMe + +# CHECK: VM MAP: +# The "KeepMe" symbol in the allocatable .text section should appear. +# CHECK: KeepMe + +# The "DropMe" symbol in the non-allocatable .info section should NOT appear. +# CHECK-NOT: DropMe + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + Address: 0x0 + Size: 0x1 + - Name: .info + Type: SHT_PROGBITS + # Missing SHF_ALLOC flag. + # Address 0x0 overlaps with .text, but since it's not allocatable, + # its symbols should be ignored in the VM view. + Address: 0x0 + Size: 0x1 +Symbols: + - Name: DropMe + Type: STT_OBJECT + Section: .info + Value: 0x0 + Size: 0x1 + - Name: KeepMe + Type: STT_FUNC + Section: .text + Value: 0x0 + Size: 0x1 +ProgramHeaders: + - Type: PT_LOAD + Flags: [ PF_R, PF_X ] + VAddr: 0x0 + PAddr: 0x0 + FirstSec: .text + LastSec: .text +...