Bug 17689 - PIE copy relocations handling broken with ld.bfd
Summary: PIE copy relocations handling broken with ld.bfd
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.25
Assignee: H.J. Lu
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-12-09 15:22 UTC by Markus Trippelsdorf
Modified: 2025-05-23 08:53 UTC (History)
0 users

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 Markus Trippelsdorf 2014-12-09 15:22:28 UTC
See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=64237

 % cat nscd.i
extern int program_invocation_name;
extern void fn2(int, int);
void
fn1() { fn2(0, program_invocation_name); }
 
 % gcc -fuse-ld=bfd -O2 -pie -fPIE nscd.i
/usr/lib/x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/home/trippels/bin/ld.bfd: /home/trippels/tmp/ccAUYBy5.o: relocation R_X86_64_PC32 against undefined symbol `program_invocation_name@@GLIBC_2.2.5' can not be used when making a shared object; recompile with -fPIC

Gold is fine.
Comment 1 Sourceware Commits 2014-12-14 07:53:18 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  bc696fd5af08fa199750abfca092894ab54206c7 (commit)
      from  94ec908766bbe8c146798754261bcce83572b00f (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=bc696fd5af08fa199750abfca092894ab54206c7

commit bc696fd5af08fa199750abfca092894ab54206c7
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Dec 13 23:37:22 2014 -0800

    Handle weak alias for PIE with copy reloc
    
    When there is a weak symbol with a real definition, the processor
    independent code will have arranged for us to see the real definition
    first.  We need to copy the needs_copy bit from the real definition and
    check it when allowing copy reloc in PIE.
    
    bfd/
    
    	PR ld/17689
    	* elf64-x86-64.c (elf_x86_64_link_hash_entry): Add needs_copy.
    	Change has_bnd_reloc to bit field.
    	(elf_x86_64_link_hash_newfunc): Initialize needs_copy and
    	has_bnd_reloc to 0.
    	(elf_x86_64_check_relocs): Set has_bnd_reloc to 1 instead
    	of TRUE.
    	(elf_x86_64_adjust_dynamic_symbol): Copy needs_copy from the
    	real definition to a weak symbol.
    	(elf_x86_64_allocate_dynrelocs): Also check needs_copy of a
    	weak symbol for PIE when discarding space for relocs against
    	symbols which turn out to need copy relocs.
    	(elf_x86_64_relocate_section): Also check needs_copy of a
    	weak symbol for PIE with copy reloc.
    
    ld/testsuite/
    
    	PR ld/17689
    	* ld-x86-64/pr17689.out: New file.
    	* ld-x86-64/pr17689.rd: Likewise.
    	* ld-x86-64/pr17689a.c: Likewise.
    	* ld-x86-64/pr17689b.S: Likewise.
    
    	* ld-x86-64/x86-64.exp: Run PR ld/17689 tests.

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

Summary of changes:
 bfd/ChangeLog                                      |   17 ++++++++++++
 bfd/elf64-x86-64.c                                 |   27 ++++++++++++++-----
 ld/testsuite/ChangeLog                             |   10 +++++++
 .../{ld-elf/dl5.out => ld-x86-64/pr17689.out}      |    0
 ld/testsuite/ld-x86-64/pr17689.rd                  |    3 ++
 ld/testsuite/ld-x86-64/pr17689a.c                  |   10 +++++++
 ld/testsuite/ld-x86-64/pr17689b.S                  |   12 +++++++++
 ld/testsuite/ld-x86-64/x86-64.exp                  |   24 +++++++++++++++++
 8 files changed, 96 insertions(+), 7 deletions(-)
 copy ld/testsuite/{ld-elf/dl5.out => ld-x86-64/pr17689.out} (100%)
 create mode 100644 ld/testsuite/ld-x86-64/pr17689.rd
 create mode 100644 ld/testsuite/ld-x86-64/pr17689a.c
 create mode 100644 ld/testsuite/ld-x86-64/pr17689b.S
Comment 2 Sourceware Commits 2014-12-14 07:53:35 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, binutils-2_25-branch has been updated
       via  3e5d8b05745a9fa1d9ad120459143e6e36cfbde0 (commit)
      from  27cef631a78b56f8ad030ba6231432e04cc1838e (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=3e5d8b05745a9fa1d9ad120459143e6e36cfbde0

commit 3e5d8b05745a9fa1d9ad120459143e6e36cfbde0
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat Dec 13 23:37:22 2014 -0800

    Handle weak alias for PIE with copy reloc
    
    When there is a weak symbol with a real definition, the processor
    independent code will have arranged for us to see the real definition
    first.  We need to copy the needs_copy bit from the real definition and
    check it when allowing copy reloc in PIE.
    
    bfd/
    
    	PR ld/17689
    	* elf64-x86-64.c (elf_x86_64_link_hash_entry): Add needs_copy.
    	Change has_bnd_reloc to bit field.
    	(elf_x86_64_link_hash_newfunc): Initialize needs_copy and
    	has_bnd_reloc to 0.
    	(elf_x86_64_check_relocs): Set has_bnd_reloc to 1 instead
    	of TRUE.
    	(elf_x86_64_adjust_dynamic_symbol): Copy needs_copy from the
    	real definition to a weak symbol.
    	(elf_x86_64_allocate_dynrelocs): Also check needs_copy of a
    	weak symbol for PIE when discarding space for relocs against
    	symbols which turn out to need copy relocs.
    	(elf_x86_64_relocate_section): Also check needs_copy of a
    	weak symbol for PIE with copy reloc.
    
    ld/testsuite/
    
    	PR ld/17689
    	* ld-x86-64/pr17689.out: New file.
    	* ld-x86-64/pr17689.rd: Likewise.
    	* ld-x86-64/pr17689a.c: Likewise.
    	* ld-x86-64/pr17689b.S: Likewise.
    
    	* ld-x86-64/x86-64.exp: Run PR ld/17689 tests.

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

Summary of changes:
 bfd/ChangeLog                                      |   22 ++++++++++++++++
 bfd/elf64-x86-64.c                                 |   27 ++++++++++++++-----
 ld/testsuite/ChangeLog                             |   10 +++++++
 .../{ld-elf/dl5.out => ld-x86-64/pr17689.out}      |    0
 ld/testsuite/ld-x86-64/pr17689.rd                  |    3 ++
 ld/testsuite/ld-x86-64/pr17689a.c                  |   10 +++++++
 ld/testsuite/ld-x86-64/pr17689b.S                  |   12 +++++++++
 ld/testsuite/ld-x86-64/x86-64.exp                  |   24 +++++++++++++++++
 8 files changed, 101 insertions(+), 7 deletions(-)
 copy ld/testsuite/{ld-elf/dl5.out => ld-x86-64/pr17689.out} (100%)
 create mode 100644 ld/testsuite/ld-x86-64/pr17689.rd
 create mode 100644 ld/testsuite/ld-x86-64/pr17689a.c
 create mode 100644 ld/testsuite/ld-x86-64/pr17689b.S
Comment 3 H.J. Lu 2014-12-14 08:12:25 UTC
Fixed.
Comment 4 Sourceware Commits 2015-05-16 14:01:52 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=25070364b0ce33eed46aa5d78ebebbec6accec7e

commit 25070364b0ce33eed46aa5d78ebebbec6accec7e
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sat May 16 07:00:21 2015 -0700

    Don't generate PLT relocations for now binding
    
    There is no need for PLT relocations with -z now. We can use GOT
    relocations, which take less space, instead and replace 16-byte .plt
    entres with 8-byte .plt.got entries.
    
    bfd/
    
    	* elf32-i386.c (elf_i386_check_relocs): Create .plt.got section
    	for now binding.
    	(elf_i386_allocate_dynrelocs): Use .plt.got section for now
    	binding.
    	* elf64-x86-64.c (elf_x86_64_check_relocs): Create .plt.got
    	section for now binding.
    	(elf_x86_64_allocate_dynrelocs): Use .plt.got section for now
    	binding.
    
    ld/testsuite/
    
    	* ld-i386/i386.exp: Run PR ld/17689 tests with -z now.
    	* ld-x86-64/x86-64.exp: Likewise
    	* ld-i386/pr17689now.rd: New file.
    	* ld-x86-64/pr17689now.rd: Likewise