Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bloaty error if zero-sized segment is at offset larger than file size #379

Merged
merged 1 commit into from
Sep 22, 2024
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
4 changes: 3 additions & 1 deletion src/elf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,9 @@ void ElfFile::ReadSegment(Elf64_Word index, Segment* segment) const {
entire_file(),
CheckedAdd(header_.e_phoff, CheckedMul(header_.e_phentsize, index)),
PhdrMunger(), &segment->range_, header);
segment->contents_ = GetRegion(header->p_offset, header->p_filesz);
if (header->p_filesz > 0) {
segment->contents_ = GetRegion(header->p_offset, header->p_filesz);
}
}

void ElfFile::ReadSection(Elf64_Word index, Section* section) const {
Expand Down
71 changes: 71 additions & 0 deletions tests/elf/sections/segment-offset-larger-than-file-size.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# RUN: %yaml2obj %s -o %t.obj
# RUN: %bloaty --raw-map %t.obj | %FileCheck %s

--- !ELF
FileHeader:
Class: ELFCLASS64
Data: ELFDATA2LSB
Type: ET_EXEC
Machine: EM_X86_64
Sections:
- Name: .text
Type: SHT_PROGBITS
Flags: [ SHF_ALLOC, SHF_EXECINSTR ]
AddressAlign: 0x1
- Name: .data
Type: SHT_PROGBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x1
- Name: .bss
Type: SHT_NOBITS
Flags: [ SHF_WRITE, SHF_ALLOC ]
AddressAlign: 0x1
- Name: .comment
Type: SHT_PROGBITS
Flags: [ SHF_MERGE, SHF_STRINGS ]
AddressAlign: 0x1
EntSize: 0x1
Content: 004743433A202844656269616E2031302E322E312D362B6275696C6432292031302E322E3120323032313031313000
- Name: .note.GNU-stack
Type: SHT_PROGBITS
AddressAlign: 0x1
ProgramHeaders:
- Type: PT_LOAD
Flags: [ PF_W, PF_R ]
VAddr: 0x20000000
Align: 0x00010000
FileSize: 0x0
MemSize: 0x08010000
Offset: 0x00690000
Symbols:
- Name: 'null'
Type: STT_FILE
Index: SHN_ABS
- Name: .text
Type: STT_SECTION
Section: .text
- Name: .data
Type: STT_SECTION
Section: .data
- Name: .bss
Type: STT_SECTION
Section: .bss
- Name: .note.GNU-stack
Type: STT_SECTION
Section: .note.GNU-stack
- Name: .comment
Type: STT_SECTION
Section: .comment
...

# CHECK: FILE MAP:
# CHECK: 000-040 64 [ELF Header]
# CHECK: 040-078 56 [ELF Program Headers]
# CHECK: 078-0a8 48 .comment
# CHECK: 0a8-150 168 .symtab
# CHECK: 150-180 48 .strtab
# CHECK: 180-1c8 72 .shstrtab
# CHECK: 1c8-408 576 [ELF Section Headers]
# CHECK: VM MAP:
# CHECK: 00000000-20000000 536870912 [-- Nothing mapped --]
# CHECK: 20000000-28010000 134283264 [LOAD #0 [RW]]
Loading