Created attachment 8652 [details] testcase In order for function pointer comparisons to work when a non -fPIC non -fPIE program uses a shared library symbol the static linker has to create an undefined symbol with a value that points to the plt entry. Currently both gold and bfd ld assume that R_386_PC32 is a call and therefore doesn't capture the address, but there is no such guarantee in the psabi. The attached patch has two functions, g and h. g will get the function address of f using the GOT. h will get the function address of f using R_386_PC32. If f is linked in from a .o, both return the same value. If f is linked in from a .so one gets the body of f and another gets the plt entry.
Created attachment 8653 [details] patch
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=4b627c18440f51077f8fd4c18adaa3919c3a373e commit 4b627c18440f51077f8fd4c18adaa3919c3a373e Author: H.J. Lu <hjl.tools@gmail.com> Date: Wed Sep 30 08:32:45 2015 -0700 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.
(In reply to Rafael Ávila de Espíndola from comment #1) > Created attachment 8653 [details] > patch I don't think it is correct. See my ld fix: https://sourceware.org/ml/binutils/2015-09/msg00328.html
Created attachment 8665 [details] x86_64 testcase
The master branch has been updated by H.J. Lu <hjl@sourceware.org>: https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5db4f0d383e3de40e594cf05dd8f6d743233a487 commit 5db4f0d383e3de40e594cf05dd8f6d743233a487 Author: H.J. Lu <hjl.tools@gmail.com> Date: Thu Oct 1 10:49:33 2015 -0700 Create a PLT entry for R_X86_64_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 * elf64-x86-64.c (elf_x86_64_check_relocs): Set pointer_equality_needed for R_X86_64_PC32 reloc in non-code sections. ld/testsuite/ PR ld/19031 * ld-x86-64/x86-64.exp: Run PR ld/19031 test. * ld-x86-64/pr19031.out: New file. * ld-x86-64/pr19031a.c: Likewise. * ld-x86-64/pr19031b.S: Likewise. * ld-x86-64/pr19031c.c: Likewise.