i386: Allow copy relocs for building PIE
authorH.J. Lu <hjl.tools@gmail.com>
Wed, 22 Apr 2015 12:24:54 +0000 (05:24 -0700)
committerH.J. Lu <hjl.tools@gmail.com>
Wed, 22 Apr 2015 12:30:01 +0000 (05:30 -0700)
commitd5597ebccca6761fb641b7fc99b6e8b56fbac6e2
treea70a40f8a62dc7d494881f71041791e753e8d942
parent712e55b92481d89d01877a0668d8140029feca88
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.
17 files changed:
bfd/ChangeLog
bfd/elf32-i386.c
ld/testsuite/ChangeLog
ld/testsuite/ld-i386/copyreloc-lib.c [new file with mode: 0644]
ld/testsuite/ld-i386/copyreloc-main.S [new file with mode: 0644]
ld/testsuite/ld-i386/copyreloc-main.out [new file with mode: 0644]
ld/testsuite/ld-i386/copyreloc-main1.rd [new file with mode: 0644]
ld/testsuite/ld-i386/copyreloc-main2.rd [new file with mode: 0644]
ld/testsuite/ld-i386/dummy.c [new file with mode: 0644]
ld/testsuite/ld-i386/i386.exp
ld/testsuite/ld-i386/pr17689.out [new file with mode: 0644]
ld/testsuite/ld-i386/pr17689.rd [new file with mode: 0644]
ld/testsuite/ld-i386/pr17689a.c [new file with mode: 0644]
ld/testsuite/ld-i386/pr17689a.t [new file with mode: 0644]
ld/testsuite/ld-i386/pr17689b.S [new file with mode: 0644]
ld/testsuite/ld-i386/pr17689ver.rd [new file with mode: 0644]
ld/testsuite/ld-i386/pr17827.rd [new file with mode: 0644]