From 4b627c18440f51077f8fd4c18adaa3919c3a373e Mon Sep 17 00:00:00 2001 From: "H.J. Lu" Date: Wed, 30 Sep 2015 08:32:45 -0700 Subject: [PATCH] Create a PLT entry for R_386_PC32 in non-code sections Since something like ".long foo - ." may be used as pointer, we make sure that PLT is used if foo is a function defined in a shared library. bfd/ PR ld/19031 * elf32-i386.c (elf_i386_check_relocs): Set pointer_equality_needed for R_386_PC32 reloc in non-code sections. ld/testsuite/ PR ld/19031 * ld-i386/i386.exp: Run PR ld/19031 test. * ld/testsuite/ld-i386/pr19031.out: New file. * ld/testsuite/ld-i386/pr19031a.c: Likewise. * ld/testsuite/ld-i386/pr19031b.S: Likewise. * ld/testsuite/ld-i386/pr19031c.c: Likewise. --- bfd/ChangeLog | 7 +++++++ bfd/elf32-i386.c | 10 +++++++++- ld/testsuite/ChangeLog | 9 +++++++++ ld/testsuite/ld-i386/i386.exp | 16 ++++++++++++++++ ld/testsuite/ld-i386/pr19031.out | 1 + ld/testsuite/ld-i386/pr19031a.c | 4 ++++ ld/testsuite/ld-i386/pr19031b.S | 23 +++++++++++++++++++++++ ld/testsuite/ld-i386/pr19031c.c | 14 ++++++++++++++ 8 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 ld/testsuite/ld-i386/pr19031.out create mode 100644 ld/testsuite/ld-i386/pr19031a.c create mode 100644 ld/testsuite/ld-i386/pr19031b.S create mode 100644 ld/testsuite/ld-i386/pr19031c.c diff --git a/bfd/ChangeLog b/bfd/ChangeLog index a218d44483d..36109481d9b 100644 --- a/bfd/ChangeLog +++ b/bfd/ChangeLog @@ -1,3 +1,10 @@ +2015-09-30 H.J. Lu + + PR ld/19031 + * elf32-i386.c (elf_i386_check_relocs): Set + pointer_equality_needed for R_386_PC32 reloc in non-code + sections. + 2015-09-30 H.J. Lu PR ld/19013 diff --git a/bfd/elf32-i386.c b/bfd/elf32-i386.c index 93904503e4f..0a06191f98e 100644 --- a/bfd/elf32-i386.c +++ b/bfd/elf32-i386.c @@ -1772,7 +1772,15 @@ elf_i386_check_relocs (bfd *abfd, /* We may need a .plt entry if the function this reloc refers to is in a shared lib. */ h->plt.refcount += 1; - if (r_type != R_386_PC32) + if (r_type == R_386_PC32) + { + /* Since something like ".long foo - ." may be used + as pointer, make sure that PLT is used if foo is + a function defined in a shared library. */ + if ((sec->flags & SEC_CODE) == 0) + h->pointer_equality_needed = 1; + } + else { h->pointer_equality_needed = 1; /* R_386_32 can be resolved at run-time. */ diff --git a/ld/testsuite/ChangeLog b/ld/testsuite/ChangeLog index 7f1b7acab9f..9c77301f74a 100644 --- a/ld/testsuite/ChangeLog +++ b/ld/testsuite/ChangeLog @@ -1,3 +1,12 @@ +2015-09-30 H.J. Lu + + PR ld/19031 + * ld-i386/i386.exp: Run PR ld/19031 test. + * ld/testsuite/ld-i386/pr19031.out: New file. + * ld/testsuite/ld-i386/pr19031a.c: Likewise. + * ld/testsuite/ld-i386/pr19031b.S: Likewise. + * ld/testsuite/ld-i386/pr19031c.c: Likewise. + 2015-09-30 H.J. Lu PR ld/19013 diff --git a/ld/testsuite/ld-i386/i386.exp b/ld/testsuite/ld-i386/i386.exp index 6213dbebcca..8cddfce9e48 100644 --- a/ld/testsuite/ld-i386/i386.exp +++ b/ld/testsuite/ld-i386/i386.exp @@ -490,6 +490,14 @@ if { [isnative] {{readelf {-Wrd} pr18900b.rd}} \ "pr18900b" \ ] \ + [list \ + "Build pr19031.so" \ + "-shared" \ + "-fPIC" \ + { pr19031a.c } \ + "" \ + "pr19031.so" \ + ] \ ] run_ld_link_exec_tests [] [list \ @@ -552,6 +560,14 @@ if { [isnative] "pr18900" \ "pr18900.out" \ ] \ + [list \ + "Run pr19031" \ + "tmpdir/pr19031.so" \ + "" \ + { pr19031b.S pr19031c.c } \ + "pr19031" \ + "pr19031.out" \ + ] \ ] } diff --git a/ld/testsuite/ld-i386/pr19031.out b/ld/testsuite/ld-i386/pr19031.out new file mode 100644 index 00000000000..d86bac9de59 --- /dev/null +++ b/ld/testsuite/ld-i386/pr19031.out @@ -0,0 +1 @@ +OK diff --git a/ld/testsuite/ld-i386/pr19031a.c b/ld/testsuite/ld-i386/pr19031a.c new file mode 100644 index 00000000000..fa34d37e588 --- /dev/null +++ b/ld/testsuite/ld-i386/pr19031a.c @@ -0,0 +1,4 @@ +void +f (void) +{ +} diff --git a/ld/testsuite/ld-i386/pr19031b.S b/ld/testsuite/ld-i386/pr19031b.S new file mode 100644 index 00000000000..3249652f4f7 --- /dev/null +++ b/ld/testsuite/ld-i386/pr19031b.S @@ -0,0 +1,23 @@ + .text + .globl g + .type g, @function +g: + calll .Lfoo +.Lfoo: + popl %eax +.Lbar: + addl $_GLOBAL_OFFSET_TABLE_+(.Lbar-.Lfoo), %eax + movl f@GOT(%eax), %eax + ret + + .globl h + .type h, @function +h: + leal zed, %eax + movl zed, %ebx + addl %ebx, %eax + ret + + .data +zed: + .long f - . diff --git a/ld/testsuite/ld-i386/pr19031c.c b/ld/testsuite/ld-i386/pr19031c.c new file mode 100644 index 00000000000..60eccaf3624 --- /dev/null +++ b/ld/testsuite/ld-i386/pr19031c.c @@ -0,0 +1,14 @@ +#include + +extern void *h (void); +extern void *g (void); + +int +main (void) +{ + + if (h () == g ()) + printf ("OK\n"); + + return 0; +} -- 2.30.2