From 69360f11c1c8fb723d0da143d577fed9ea1a4043 Mon Sep 17 00:00:00 2001 From: Kyle Teske Date: Tue, 17 Sep 2024 10:38:59 -0500 Subject: [PATCH] Fix bloaty error if zero-sized segment is at offset larger than file size Add a test: copy from empty-bin-64.test, than add a program header with an offset greater than the file size. https://github.com/google/bloaty/issues/378 --- src/elf.cc | 4 +- .../segment-offset-larger-than-file-size.test | 71 +++++++++++++++++++ 2 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 tests/elf/sections/segment-offset-larger-than-file-size.test diff --git a/src/elf.cc b/src/elf.cc index 7d1a3641..c1f701fa 100644 --- a/src/elf.cc +++ b/src/elf.cc @@ -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 { diff --git a/tests/elf/sections/segment-offset-larger-than-file-size.test b/tests/elf/sections/segment-offset-larger-than-file-size.test new file mode 100644 index 00000000..95ec6350 --- /dev/null +++ b/tests/elf/sections/segment-offset-larger-than-file-size.test @@ -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]]