Bug 16323

Summary: strip changes PT_LOAD segment generated by gold
Product: binutils Reporter: H.J. Lu <hjl.tools>
Component: binutilsAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 2.25   
Target Milestone: ---   
Host: Target:
Build: Last reconfirmed:

Description H.J. Lu 2013-12-12 13:44:37 UTC
[hjl@gnu-6 pr16322]$ cat x.c
extern void f1 (void);
void * const p1[1] __attribute__ ((aligned(8))) = { &f1 };
[hjl@gnu-6 pr16322]$ make
gcc -m32 -O2 -fPIC   -c -o x.o x.c
ld.gold -melf_i386 -shared --gc-sections -z relro -o libx.so x.o
readelf -lS libx.so
There are 12 section headers, starting at offset 0x1168:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .dynsym           DYNSYM          000000f4 0000f4 000060 10   A  2   1  4
  [ 2] .dynstr           STRTAB          00000154 000154 00001f 00   A  0   0  1
  [ 3] .hash             HASH            00000174 000174 00002c 04   A  1   0  4
  [ 4] .rel.dyn          REL             000001a0 0001a0 000008 08   A  1   0  4
  [ 5] .data.rel.ro      PROGBITS        00001f88 000f88 000004 00  WA  0   0  8
  [ 6] .dynamic          DYNAMIC         00001f8c 000f8c 000070 08  WA  2   0  4
  [ 7] .comment          PROGBITS        00000000 001000 00002d 01  MS  0   0  1
  [ 8] .note.gnu.gold-ve NOTE            00000000 001030 00001c 00      0   0  4
  [ 9] .symtab           SYMTAB          00000000 00104c 000080 10     10   3  4
  [10] .strtab           STRTAB          00000000 0010cc 00002c 00      0   0  1
  [11] .shstrtab         STRTAB          00000000 0010f8 000070 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

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

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x000c0 0x000c0 R   0x4
  LOAD           0x000000 0x00000000 0x00000000 0x001a8 0x001a8 R   0x1000
  LOAD           0x000f88 0x00001f88 0x00001f88 0x00078 0x00078 RW  0x1000
  DYNAMIC        0x000f8c 0x00001f8c 0x00001f8c 0x00070 0x00070 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0
  GNU_RELRO      0x000f88 0x00001f88 0x00001f88 0x00078 0x00078 RW  0x8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .dynsym .dynstr .hash .rel.dyn 
   02     .data.rel.ro .dynamic 
   03     .dynamic 
   04     
   05     .data.rel.ro .dynamic 
[hjl@gnu-6 pr16322]$ strip libx.so
[hjl@gnu-6 pr16322]$ readelf -SlW libx.so
There are 10 section headers, starting at offset 0x10a8:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] .dynsym           DYNSYM          000000f4 0000f4 000060 10   A  2   1  4
  [ 2] .dynstr           STRTAB          00000154 000154 00001f 00   A  0   0  1
  [ 3] .hash             HASH            00000174 000174 00002c 04   A  1   0  4
  [ 4] .rel.dyn          REL             000001a0 0001a0 000008 08   A  1   0  4
  [ 5] .data.rel.ro      PROGBITS        00001f88 000f88 000004 00  WA  0   0  8
  [ 6] .dynamic          DYNAMIC         00001f8c 000f8c 000070 08  WA  2   0  4
  [ 7] .comment          PROGBITS        00000000 000ffc 00002d 01  MS  0   0  1
  [ 8] .note.gnu.gold-version NOTE            00000000 00102c 00001c 00      0   0  4
  [ 9] .shstrtab         STRTAB          00000000 001048 000060 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

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

Program Headers:
  Type           Offset   VirtAddr   PhysAddr   FileSiz MemSiz  Flg Align
  PHDR           0x000034 0x00000034 0x00000034 0x000c0 0x000c0 R   0x4
  LOAD           0x000000 0x00000000 0x00000000 0x001a8 0x001a8 R   0x1000
  LOAD           0x000f88 0x00001f88 0x00001f88 0x00074 0x00074 RW  0x1000
  DYNAMIC        0x000f8c 0x00001f8c 0x00001f8c 0x00070 0x00070 RW  0x4
  GNU_STACK      0x000000 0x00000000 0x00000000 0x00000 0x00000 RW  0
  GNU_RELRO      0x000f88 0x00001f88 0x00001f88 0x00078 0x00078 RW  0x8

 Section to Segment mapping:
  Segment Sections...
   00     
   01     .dynsym .dynstr .hash .rel.dyn 
   02     .data.rel.ro .dynamic 
   03     .dynamic 
   04     
   05     .data.rel.ro .dynamic 
[hjl@gnu-6 pr16322]$ 

strip changes

  LOAD           0x000f88 0x00001f88 0x00001f88 0x00078 0x00078 RW  0x1000

to

  LOAD           0x000f88 0x00001f88 0x00001f88 0x00074 0x00074 RW  0x1000

It makes

  GNU_RELRO      0x000f88 0x00001f88 0x00001f88 0x00078 0x00078 RW  0x8

bigger than its PT_LOAD segment.
Comment 1 Sourceware Commits 2014-01-08 14:01:08 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  43a8475ca01b676fb764aaed0c4ed1cc16fc3c87 (commit)
      from  221fd5d598e7dcf7b953150986a501b462b99891 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=43a8475ca01b676fb764aaed0c4ed1cc16fc3c87

commit 43a8475ca01b676fb764aaed0c4ed1cc16fc3c87
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Thu Dec 12 10:35:47 2013 -0800

    Adjust LOAD segment to generate GNU_RELRO segment
    
    This patch fixes 2 GNU_RELRO segment bugs:
    
    1. lang_size_sections didn't properly align base to the maximum
    alignment power of sections between DATA_SEGMENT_ALIGN and
    DATA_SEGMENT_RELRO_END.
    2. ld failed to adjust LOAD segment to generate GNU_RELRO segment
    when LOAD segment doesn't fit GNU_RELRO segment.  This is
    
    https://sourceware.org/bugzilla/show_bug.cgi?id=14207
    
    We "fixed" ld by not generating GNU_RELRO segment.  This patch
    adjusts LOAD segment to generate GNU_RELRO segment.  It fixes
    PR ld/16322 and at the same time it also fixes PR binutils/16323
    since now we can adjust LOAD segment if it is too small.
    
    bfd/
    
    	PR ld/14207
    	PR ld/16322
    	PR binutils/16323
    	* elf.c (_bfd_elf_map_sections_to_segments): Don't check section
    	size for PT_GNU_RELRO segment.
    	(assign_file_positions_for_load_sections): If PT_LOAD segment
    	doesn't fit PT_GNU_RELRO segment, adjust its p_filesz and p_memsz.
    
    ld/
    
    	PR ld/14207
    	PR ld/16322
    	PR binutils/16323
    	* ldlang.c (lang_size_sections): Properly align RELRO base.
    
    ld/testsuite/
    
    	PR ld/14207
    	PR ld/16322
    	PR binutils/16323
    	* ld-elf/pr16322.d: New file.
    	* ld-elf/pr16322.s: Likewise.
    
    	* ld-x86-64/pr14207.d: Expect PT_GNU_RELRO segment.

-----------------------------------------------------------------------

Summary of changes:
 bfd/ChangeLog                    |   10 +++++++++
 bfd/elf.c                        |   41 +++++++++++++++++++++++++++++++++----
 ld/ChangeLog                     |    7 ++++++
 ld/ldlang.c                      |    3 +-
 ld/testsuite/ChangeLog           |   10 +++++++++
 ld/testsuite/ld-elf/pr16322.d    |    7 ++++++
 ld/testsuite/ld-elf/pr16322.s    |    6 +++++
 ld/testsuite/ld-x86-64/pr14207.d |   23 +++++++++++++++++---
 8 files changed, 97 insertions(+), 10 deletions(-)
 create mode 100644 ld/testsuite/ld-elf/pr16322.d
 create mode 100644 ld/testsuite/ld-elf/pr16322.s
Comment 2 H.J. Lu 2014-01-08 14:06:09 UTC
Fixed.
Comment 3 Sourceware Commits 2014-01-10 11:21:04 UTC
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "gdb and binutils".

The branch, master has been updated
       via  eec2f3ed9f053653ed5d629eb50e08e3ee61e9bd (commit)
      from  a2cd8cfed14491303eb8338f90e206034c5a3fe2 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

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

commit eec2f3ed9f053653ed5d629eb50e08e3ee61e9bd
Author: Alan Modra <amodra@gmail.com>
Date:   Fri Jan 10 21:11:46 2014 +1030

    Don't adjust LOAD segment to match GNU_RELRO segment
    
    Instead, fix Jakub's original code setting up the PR_GNU_RELRO header
    from the PT_LOAD header.
    
    	PR ld/14207
    	PR ld/16322
    	PR binutils/16323
    bfd/
    	* elf.c (assign_file_positions_for_load_sections): Revert last change.
    	(assign_file_positions_for_non_load_sections): When setting up
    	PT_GNU_RELRO header, don't require a corresponding PT_LOAD
    	header that completely covers the relro region.
    ld/
    	* ldlang.c (lang_size_sections): Remove unneeded RELRO base
    	adjust.  Tidy comments.
    	* ld.texinfo (DATA_SEGMENT_RELRO_END): Correct description.
    ld/testsuite/
    	* ld-x86-64/pr14207.d: Adjust

-----------------------------------------------------------------------

Summary of changes:
 bfd/ChangeLog                    |   10 ++++++++++
 bfd/elf.c                        |   37 -------------------------------------
 ld/ChangeLog                     |    9 +++++++++
 ld/ld.texinfo                    |    6 ++++--
 ld/ldlang.c                      |   21 +++++++++------------
 ld/testsuite/ChangeLog           |    4 ++++
 ld/testsuite/ld-x86-64/pr14207.d |    2 +-
 7 files changed, 37 insertions(+), 52 deletions(-)