Bug 32191 - --rosegment places .note.gnu.property in executable PT_LOAD segment for -z separate-code
Summary: --rosegment places .note.gnu.property in executable PT_LOAD segment for -z se...
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.44 (HEAD)
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 32190
Blocks:
  Show dependency treegraph
 
Reported: 2024-09-20 02:17 UTC by H.J. Lu
Modified: 2024-09-27 22:50 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description H.J. Lu 2024-09-20 02:17:00 UTC
[hjl@gnu-tgl-3 tmp]$ cat x.c
int
main ()
{
}
[hjl@gnu-tgl-3 tmp]$ gcc x.c -fPIC -shared -Wl,-z,separate-code -Wl,--no-rosegment
[hjl@gnu-tgl-3 tmp]$ readelf -lW a.out 

Elf file type is DYN (Shared object file)
Entry point 0x0
There are 11 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x000518 0x000518 R   0x1000
  LOAD           0x001000 0x0000000000001000 0x0000000000001000 0x000111 0x000111 R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000 0x0000a4 0x0000a4 R   0x1000
  LOAD           0x002df0 0x0000000000003df0 0x0000000000003df0 0x000210 0x000218 RW  0x1000
  DYNAMIC        0x002e08 0x0000000000003e08 0x0000000000003e08 0x0001c0 0x0001c0 RW  0x8
  NOTE           0x0002a8 0x00000000000002a8 0x00000000000002a8 0x000030 0x000030 R   0x8
  NOTE           0x0002d8 0x00000000000002d8 0x00000000000002d8 0x000024 0x000024 R   0x4
  GNU_PROPERTY   0x0002a8 0x00000000000002a8 0x00000000000002a8 0x000030 0x000030 R   0x8
  GNU_EH_FRAME   0x002000 0x0000000000002000 0x0000000000002000 0x000024 0x000024 R   0x4
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10
  GNU_RELRO      0x002df0 0x0000000000003df0 0x0000000000003df0 0x000210 0x000210 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     .note.gnu.property .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn 
   01     .init .plt .plt.got .text .fini 
   02     .eh_frame_hdr .eh_frame 
   03     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt .bss 
   04     .dynamic 
   05     .note.gnu.property 
   06     .note.gnu.build-id 
   07     .note.gnu.property 
   08     .eh_frame_hdr 
   09     
   10     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt 
[hjl@gnu-tgl-3 tmp]$ gcc x.c -fPIC -shared -Wl,-z,separate-code -Wl,--rosegment [hjl@gnu-tgl-3 tmp]$ readelf -lW a.out 

Elf file type is DYN (Shared object file)
Entry point 0x0
There are 10 program headers, starting at offset 64

Program Headers:
  Type           Offset   VirtAddr           PhysAddr           FileSiz  MemSiz   Flg Align
  LOAD           0x000000 0x0000000000000000 0x0000000000000000 0x001148 0x001148 R E 0x1000
  LOAD           0x002000 0x0000000000002000 0x0000000000002000 0x0002e4 0x0002e4 R   0x1000
  LOAD           0x002df0 0x0000000000003df0 0x0000000000003df0 0x000210 0x000218 RW  0x1000
  DYNAMIC        0x002e08 0x0000000000003e08 0x0000000000003e08 0x0001c0 0x0001c0 RW  0x8
  NOTE           0x001118 0x0000000000001118 0x0000000000001118 0x000030 0x000030 R   0x8
  NOTE           0x002000 0x0000000000002000 0x0000000000002000 0x000024 0x000024 R   0x4
  GNU_PROPERTY   0x001118 0x0000000000001118 0x0000000000001118 0x000030 0x000030 R   0x8
  GNU_EH_FRAME   0x002240 0x0000000000002240 0x0000000000002240 0x000024 0x000024 R   0x4
  GNU_STACK      0x000000 0x0000000000000000 0x0000000000000000 0x000000 0x000000 RW  0x10
  GNU_RELRO      0x002df0 0x0000000000003df0 0x0000000000003df0 0x000210 0x000210 R   0x1

 Section to Segment mapping:
  Segment Sections...
   00     .init .plt .plt.got .text .fini .note.gnu.property 
   01     .note.gnu.build-id .gnu.hash .dynsym .dynstr .gnu.version .gnu.version_r .rela.dyn .eh_frame_hdr .eh_frame 
   02     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt .bss 
   03     .dynamic 
   04     .note.gnu.property 
   05     .note.gnu.build-id 
   06     .note.gnu.property 
   07     .eh_frame_hdr 
   08     
   09     .init_array .fini_array .data.rel.ro .dynamic .got .got.plt 
[hjl@gnu-tgl-3 tmp]$ 

.note.gnu.property shouldn't be in the same segment as .text section.
Comment 1 Sourceware Commits 2024-09-26 11:52:34 UTC
The master branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=052940eba6fcd2b9f359f03ee205e9cd4dfb1575

commit 052940eba6fcd2b9f359f03ee205e9cd4dfb1575
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Fri Sep 20 11:32:26 2024 +0800

    ld: Ignore .note.gnu.build-id when placing orphaned notes
    
    The commits:
    
    e8e10743f7b Add --rosegment option to BFD linker to stop the '-z separate-code' from generating two read-only segments.
    bf6d7087de0 ld: Move the .note.build-id section to near the start of the memory map
    
    place .note.gnu.build-id before text sections when --rosegment is used.
    Ignore .note.gnu.build-id when placing orphaned notes if --rosegment and
    -z separate-code are used together to avoid putting any note sections
    between .note.gnu.build-id and text sections in the same PT_LOAD segment.
    
            PR ld/32191
            * ldlang.c (lang_insert_orphan): Ignore .note.gnu.build-id when
            placing orphaned notes.
            * testsuite/ld-elf/pr23658-1a.d: Pass --no-rosegment to ld.
            * testsuite/ld-elf/pr23658-1c.d: Likewise.
            * testsuite/ld-elf/pr23658-1e.d: New file.
            * testsuite/ld-elf/pr23658-1f.d: Likewise.
            * testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
            * testsuite/ld-i386/pr32191.d: New file.
            * testsuite/ld-x86-64/lam-u48.rd: Updated.
            * testsuite/ld-x86-64/lam-u57.rd: Likewise.
            * testsuite/ld-x86-64/pr32191-x32.d: New file.
            * testsuite/ld-x86-64/pr32191.d: Likewise.
            * testsuite/ld-x86-64/pr32191.s: Likewise.
            * testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Comment 2 Sourceware Commits 2024-09-27 22:47:11 UTC
The binutils-2_43-branch branch has been updated by H.J. Lu <hjl@sourceware.org>:

https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=0a71d78f6a6810db1816139e7a9af7d82bb8fb7a

commit 0a71d78f6a6810db1816139e7a9af7d82bb8fb7a
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Sep 22 08:23:08 2024 +0800

    ld: Don't explicitly add .note.gnu.build-id in elf.sc
    
    The commit:
    
    e8e10743f7b Add --rosegment option to BFD linker to stop the '-z separate-code' from generating two read-only segments.
    
    places .note.gnu.property and text sections in the same PT_LOAD segment
    when --rosegment and -z separate-code are used together.  After
    
    commit f3e660db14a0a95f3953496d8beb7c58ef34c6d5
    Author: H.J. Lu <hjl.tools@gmail.com>
    Date:   Mon Oct 8 04:39:09 2018 -0700
    
        ELF: Properly group and place orphan note sections
    
        Properly group orphan note sections.  When placing orphan note section
        as the first note section, place it after the section before all note
        sections.
    
    ld properly groups and places orphan note sections.  If .note.gnu.build-id
    is added to elf.sc, the orphan .note.gnu.property section will be placed
    before the .note.gnu.build-id section due to the larger section alignment
    and may be placed in the same PT_LOAD segment with text sections.  Remove
    .note.gnu.build-id from elf.sc to let ld properly group and add all orphan
    note sections.
    
    Binutils 2.44 has been fixed by a different fix:
    
    commit 052940eba6fcd2b9f359f03ee205e9cd4dfb1575
    Author: H.J. Lu <hjl.tools@gmail.com>
    Date:   Fri Sep 20 11:32:26 2024 +0800
    
        ld: Ignore .note.gnu.build-id when placing orphaned notes
    
    since .note.gnu.build-id has been removed from emit_early_ro in elf.sc.
    
            PR ld/32191
            * scripttempl/elf.sc (emit_early_ro): Remove .note.gnu.build-id.
            * testsuite/ld-i386/i386.exp: Run PR ld/32191 test.
            * testsuite/ld-i386/pr32191.d: New file.
            * testsuite/ld-x86-64/pr32191-x32.d: New file.
            * testsuite/ld-x86-64/pr32191.d: Likewise.
            * testsuite/ld-x86-64/pr32191.s: Likewise.
            * testsuite/ld-x86-64/x86-64.exp: Run PR ld/32191 tests.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>
Comment 3 H.J. Lu 2024-09-27 22:50:10 UTC
Fixed for 2.44 and 2.43 branch.