Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
60 changes: 60 additions & 0 deletions tests/elf/non-alloc-symbols.test
Original file line number Diff line number Diff line change
@@ -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
...
Loading