Bug 18289 - Support copy relocation in PIE
Summary: Support copy relocation in PIE
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: ld (show other bugs)
Version: 2.26
: P2 normal
Target Milestone: 2.26
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-04-21 13:39 UTC by H.J. Lu
Modified: 2019-06-17 12:20 UTC (History)
4 users (show)

See Also:
Host:
Target: i386-elf
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 2015-04-21 13:39:38 UTC
This

main:
	call	__x86.get_pc_thunk.ax
	addl	$_GLOBAL_OFFSET_TABLE_, %eax
	movl	$4, optopt@GOTOFF(%eax)
	xorl	%eax, %eax
	ret

should work with copy relocation in PIE.  But we get

[hjl@gnu-tools-1 copyreloc]$ gcc -pie -m32 x.s
/usr/local/bin/ld: /tmp/ccCk6nT6.o: relocation R_386_GOTOFF against undefined symbol `optopt@@GLIBC_2.0' can not be used when making a shared object
/usr/local/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status
[hjl@gnu-tools-1 copyreloc]$ 

We need to implement x86-64 commit 631d040f80d99b7b993abd77c9d064fa8bccd711
on i386 and handle cases similar to PRs 17689/17827/17847 if needed.
Comment 1 Sourceware Commits 2015-04-22 12:34:13 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=d5597ebccca6761fb641b7fc99b6e8b56fbac6e2

commit d5597ebccca6761fb641b7fc99b6e8b56fbac6e2
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Wed Apr 22 05:24:54 2015 -0700

    i386: Allow copy relocs for building PIE
    
    This patch allows copy relocs for R_386_GOTOFF relocations in PIE.  For
    
    extern int glob_a;
    int foo ()
    {
      return glob_a;
    }
    
    compiler now can optimize it from
    
    	call	__x86.get_pc_thunk.ax
    	addl	$_GLOBAL_OFFSET_TABLE_, %eax
    	movl	glob_a@GOT(%eax), %eax
    	movl	(%eax), %eax
    	ret
    
    to
    
    	call	__x86.get_pc_thunk.ax
    	addl	$_GLOBAL_OFFSET_TABLE_, %eax
    	movl	glob_a@GOTOFF(%eax), %eax
    	ret
    
    bfd/
    
    	PR ld/18289
    	* elf32-i386.c (elf_i386_link_hash_entry): Add gotoff_ref.
    	(elf_i386_link_hash_newfunc): Initialize gotoff_ref to 0.
    	(elf_i386_create_dynamic_sections): Always allow copy relocs for
    	building executables.
    	(elf_i386_copy_indirect_symbol): Also copy gotoff_ref.
    	(elf_i386_check_relocs): Set gotoff_ref for R_386_GOTOFF.
    	(elf_i386_adjust_dynamic_symbol): Also allocate copy relocs for
    	PIE and R_386_GOTOFF.
    	(elf_i386_relocate_section): Allow R_386_GOTOFF in executable.
    
    ld/testsuite/
    
    	PR ld/18289
    	* ld-i386/copyreloc-lib.c: New file.
    	* ld-i386/copyreloc-main.S: Likewise.
    	* ld-i386/copyreloc-main.out: Likewise.
    	* ld-i386/copyreloc-main1.rd: Likewise.
    	* ld-i386/copyreloc-main2.rd: Likewise.
    	* ld-i386/dummy.c: Likewise.
    	* ld-i386/pr17689.out: Likewise.
    	* ld-i386/pr17689.rd: Likewise.
    	* ld-i386/pr17689a.c: Likewise.
    	* ld-i386/pr17689b.S: Likewise.
    	* ld-i386/pr17827.rd: Likewise.
    	* ld-i386/pr17827ver.rd: Likewise.
    	* ld-i386/i386.exp: Run copyreloc tests.
Comment 2 H.J. Lu 2015-04-22 16:55:15 UTC
Fixed for 2.26.
Comment 3 dura 2016-03-06 10:51:56 UTC
This test failed with:
tmpdir/copyreloc-main.o: In function `main':
(.text.startup+0xc): undefined reference to `a_glob'
collect2: error: ld returned 1 exit status

if --as-needed is given to the linker. Is this normal?
Comment 4 H.J. Lu 2016-03-06 16:29:34 UTC
(In reply to dura from comment #3)
> This test failed with:
> tmpdir/copyreloc-main.o: In function `main':
> (.text.startup+0xc): undefined reference to `a_glob'
> collect2: error: ld returned 1 exit status
> 
> if --as-needed is given to the linker. Is this normal?

It should be fixed now.
Comment 5 dura 2016-03-06 17:38:00 UTC
Same problem for these testcase:
FAIL: Build pr18900a
FAIL: Build pr18900a
FAIL: Build pr18900b
FAIL: Build pr18900b
FAIL: Build gotpc1
FAIL: Build gotpc1
Comment 6 H.J. Lu 2016-03-06 17:46:58 UTC
(In reply to dura from comment #5)
> Same problem for these testcase:
> FAIL: Build pr18900a
> FAIL: Build pr18900a
> FAIL: Build pr18900b
> FAIL: Build pr18900b
> FAIL: Build gotpc1
> FAIL: Build gotpc1

Please open a new bug report and assign it to me.
Comment 7 Fangrui Song 2019-06-17 08:38:37 UTC
This bug can be closed now.
Comment 8 Nick Clifton 2019-06-17 12:20:14 UTC
OK, closing.