Bug 21191 - objcopy --only-keep-debug creates non-monotonically increasing section offsets
Summary: objcopy --only-keep-debug creates non-monotonically increasing section offsets
Status: RESOLVED WONTFIX
Alias: None
Product: binutils
Classification: Unclassified
Component: binutils (show other bugs)
Version: 2.29
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-02-21 12:55 UTC by Richard Biener
Modified: 2019-02-20 14:47 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
Project(s) to access:
ssh public key:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Richard Biener 2017-02-21 12:55:12 UTC
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.
Comment 1 Alan Modra 2017-02-21 23:57:40 UTC
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.
Comment 2 Richard Biener 2017-02-22 08:22:10 UTC
(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.
Comment 3 Tom de Vries 2019-02-20 14:47:18 UTC
(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"