int main() { } > gcc t.c -g > objcopy --only-keep-debug a.out a.out.debug > readelf -S a.out There are 39 section headers, starting at offset 0x2788: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .interp PROGBITS 0000000000400238 00000238 000000000000001c 0000000000000000 A 0 0 1 [ 2] .note.ABI-tag NOTE 0000000000400254 00000254 0000000000000020 0000000000000000 A 0 0 4 [ 3] .note.gnu.build-i NOTE 0000000000400274 00000274 0000000000000024 0000000000000000 A 0 0 4 [ 4] .hash HASH 0000000000400298 00000298 0000000000000018 0000000000000004 A 6 0 8 [ 5] .gnu.hash GNU_HASH 00000000004002b0 000002b0 000000000000001c 0000000000000000 A 6 0 8 [ 6] .dynsym DYNSYM 00000000004002d0 000002d0 ... [25] .got.plt PROGBITS 0000000000601000 00001000 0000000000000020 0000000000000008 WA 0 0 8 [26] .data PROGBITS 0000000000601020 00001020 0000000000000010 0000000000000000 WA 0 0 8 [27] .bss NOBITS 0000000000601030 00001030 0000000000000008 0000000000000000 WA 0 0 1 [28] .comment PROGBITS 0000000000000000 00001030 0000000000000018 0000000000000001 MS 0 0 1 [29] .debug_aranges PROGBITS 0000000000000000 00001050 0000000000000130 0000000000000000 0 0 16 [30] .debug_info PROGBITS 0000000000000000 00001180 000000000000036d 0000000000000000 0 0 1 > readelf -S a.out.debug There are 39 section headers, starting at offset 0x19e8: Section Headers: [Nr] Name Type Address Offset Size EntSize Flags Link Info Align [ 0] NULL 0000000000000000 00000000 0000000000000000 0000000000000000 0 0 0 [ 1] .interp NOBITS 0000000000400238 00000238 000000000000001c 0000000000000000 A 0 0 1 [ 2] .note.ABI-tag NOTE 0000000000400254 00000254 0000000000000020 0000000000000000 A 0 0 4 [ 3] .note.gnu.build-i NOTE 0000000000400274 00000274 0000000000000024 0000000000000000 A 0 0 4 [ 4] .hash NOBITS 0000000000400298 00000298 0000000000000018 0000000000000004 A 6 0 8 [ 5] .gnu.hash NOBITS 00000000004002b0 00000298 000000000000001c 0000000000000000 A 6 0 8 [ 6] .dynsym NOBITS 00000000004002d0 00000298 ... [25] .got.plt NOBITS 0000000000601000 00000e00 0000000000000020 0000000000000008 WA 0 0 8 [26] .data NOBITS 0000000000601020 00000e00 0000000000000010 0000000000000000 WA 0 0 8 [27] .bss NOBITS 0000000000601030 00000e00 0000000000000008 0000000000000000 WA 0 0 1 [28] .comment PROGBITS 0000000000000000 00000298 0000000000000018 0000000000000001 MS 0 0 1 [29] .debug_aranges PROGBITS 0000000000000000 000002b0 0000000000000130 0000000000000000 0 0 16 [30] .debug_info PROGBITS 0000000000000000 000003e0 000000000000036d 0000000000000000 0 0 1 notice how section 28 and 29 start before section 27. eu-strip gets this correct. checked 2.27 and git HEAD.
File offset for a SHT_NOBITS section is irrelevant. Note that this "bug" can also occur with ld. You don't want to waste space in the output file with padding for NOBITS sections, but glibc ld.so checks that p_vaddr and p_offset agree modulo pagesize, even for segments with p_filesz zero. Thus p_offset needs to be adjusted to pacify glibc. p_offset is derived from sh_offset so we adjust sh_offset too. We could zero all the NOBITS sh_offset values for this specific case of --only-keep-debug, but I don't see the point given that ld will create out of order sh_offset. What tool was complaining about sh_offset? Fix it please.
(In reply to Alan Modra from comment #1) > File offset for a SHT_NOBITS section is irrelevant. > > Note that this "bug" can also occur with ld. You don't want to waste space > in the output file with padding for NOBITS sections, but glibc ld.so checks > that p_vaddr and p_offset agree modulo pagesize, even for segments with > p_filesz zero. Thus p_offset needs to be adjusted to pacify glibc. > p_offset is derived from sh_offset so we adjust sh_offset too. > > We could zero all the NOBITS sh_offset values for this specific case of > --only-keep-debug, but I don't see the point given that ld will create out > of order sh_offset. > > What tool was complaining about sh_offset? Fix it please. It is DWZ complaining, I've sent a fix to Jakub zeroing sh_offset (and ignoring NOBITS sections for its sanity checks). He didn't like it too much but we'll see.
(In reply to Richard Biener from comment #2) > (In reply to Alan Modra from comment #1) > > What tool was complaining about sh_offset? Fix it please. > > It is DWZ complaining, Filed as dwz PR24251 - "Allocatable section after non-allocatable ones"