Bug 23704 - Too many PT_LOAD segments with -z separate-code
Summary: Too many PT_LOAD segments with -z separate-code
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.31
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 34003
Blocks:
  Show dependency tree / graph
 
Reported: 2018-09-24 13:29 UTC by Jakub Jelinek
Modified: 2026-03-30 19:04 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 Jakub Jelinek 2018-09-24 13:29:41 UTC
As I've already mentioned in https://bugzilla.redhat.com/show_bug.cgi?id=1623218#c13
I think it is complete waste to generate 4 PT_LOAD segments for -z separate-code,
IMNSHO 3 PT_LOADs should be enough, which should be achievable by moving for -z separate-code in the linker script all sections that need read-only non-executable before all read-only/executable sections.  That way we'd have one PF_R PT_LOAD, then one PF_R|PF_X and one PF_R|PF_W, the last one as before would start with relro sections if -z relro, rather than the current PF_R PT_LOAD, followed by PF_R|PF_X, followed by another PF_R one and finally PF_R|PF_W.
Comment 1 Alan Modra 2018-10-02 03:55:18 UTC
I agree that moving read-only (and relro) sections is a good idea, but it may not work for targets that have limited addressing and use offsets from a register to access .got, .got.plt, .plt and .sdata.  Any target that uses the same register to access sections on both sides of DATA_SEGMENT_RELRO_END will likely not be able to tolerate moving read-only sections.
Comment 2 Sourceware Commits 2026-03-30 19:04:01 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=99600a4787910d4b1994b243473a7386dbee641d

commit 99600a4787910d4b1994b243473a7386dbee641d
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Mar 18 12:13:26 2026 -0700

    elf/x86: Place code after read-only data for --rosegment
    
    commit e8e10743f7b207b21a1efb0cc9e42487080db013
    Author: Nick Clifton <nickc@redhat.com>
    Date:   Thu Jun 13 15:10:15 2024 +0100
    
        Add --rosegment option to BFD linker to stop the '-z separate-code' from gen erating two read-only segments.
    
    added --rosegment option to generate one read-only segment with
    -z separate-code.  But it puts the read-only data, which contains ELF
    headers and .note.gnu.build-id section, in the executable segment:
    
    Program Headers:
      Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
      LOAD           0x000000 0x00000000 0x00000000 0x00185 0x00185 R E 0x1000
      LOAD           0x001000 0x00001000 0x00001000 0x000d0 0x000d0 R   0x1000
      LOAD           0x001f78 0x00002f78 0x00002f78 0x0008c 0x0008c RW  0x1000
      DYNAMIC        0x001f78 0x00002f78 0x00002f78 0x00070 0x00070 RW  0x4
      NOTE           0x000154 0x00000154 0x00000154 0x00024 0x00024 R   0x4
      NOTE           0x00109c 0x0000109c 0x0000109c 0x00034 0x00034 R   0x4
      GNU_PROPERTY   0x00109c 0x0000109c 0x0000109c 0x00034 0x00034 R   0x4
      GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
      GNU_RELRO      0x001f78 0x00002f78 0x00002f78 0x00088 0x00088 R   0x1
    
     Section to Segment mapping:
      Segment Sections...
       00     .note.gnu.build-id .text
       01     .gnu.hash .dynsym .dynstr .rela.dyn .rodata .eh_frame .note.gnu.property
       02     .dynamic .got.plt .data
       03     .dynamic
       04     .note.gnu.build-id
       05     .note.gnu.property
       06     .note.gnu.property
       07
       08     .dynamic .got.plt
    
    which defeats the purpose of -z separate-code.  Update --rosegment to
    place code after read-only data:
    
    Program Headers:
      Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
      LOAD           0x000000 0x00000000 0x00000000 0x00248 0x00248 R   0x1000
      LOAD           0x001000 0x00001000 0x00001000 0x00005 0x00005 R E 0x1000
      LOAD           0x001f78 0x00002f78 0x00002f78 0x0008c 0x0008c RW  0x1000
      DYNAMIC        0x001f78 0x00002f78 0x00002f78 0x00070 0x00070 RW  0x4
      NOTE           0x000154 0x00000154 0x00000154 0x00024 0x00024 R   0x4
      NOTE           0x000214 0x00000214 0x00000214 0x00034 0x00034 R   0x4
      GNU_PROPERTY   0x000214 0x00000214 0x00000214 0x00034 0x00034 R   0x4
      GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0x10
      GNU_RELRO      0x001f78 0x00002f78 0x00002f78 0x00088 0x00088 R   0x1
    
     Section to Segment mapping:
      Segment Sections...
       00     .note.gnu.build-id .gnu.hash .dynsym .dynstr .rela.dyn .rodata .eh_frame .note.gnu.property
       01     .text
       02     .dynamic .got.plt .data
       03     .dynamic
       04     .note.gnu.build-id
       05     .note.gnu.property
       06     .note.gnu.property
       07
       08     .dynamic .got.plt
    
            PR ld/23704
            PR ld/30907
            PR ld/32191
            PR ld/34003
            * emulparams/elf32_x86_64.sh (ALL_TEXT_AFTER_RO): New.
            * emulparams/elf_i386.sh (ALL_TEXT_AFTER_RO): Likewise.
            * emulparams/elf_i386_be.sh (ALL_TEXT_AFTER_RO): Likewise.
            * emulparams/elf_i386_vxworks.sh (ALL_TEXT_AFTER_RO): Likewise.
            * emulparams/elf_iamcu.sh (ALL_TEXT_AFTER_RO): Likewise.
            * emulparams/elf_x86_64.sh (ALL_TEXT_AFTER_RO): Likewise.
            * scripttempl/elf.sc (ALL_TEXT_AFTER_RO): New.  If set, place
            code after read-only data for -z separate-code --rosegment.
            * testsuite/ld-elf/pr30907-2.d: Skip x86 targets.
            * testsuite/ld-elf/pr34003.d: New file.
            * testsuite/ld-i386/pr32191.d: Updated.
            * testsuite/ld-x86-64/pr32191-x32.d: Likewise.
            * testsuite/ld-x86-64/pr32191.d: Likewise.
    
    Signed-off-by: H.J. Lu <hjl.tools@gmail.com>