Bug 19031 - R_386_PC32 does not imply a call
Summary: R_386_PC32 does not imply a call
Status: NEW
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: unspecified
: P2 normal
Target Milestone: ---
Assignee: Cary Coutant
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-09-30 13:34 UTC by Rafael Ávila de Espíndola
Modified: 2015-10-01 17:51 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments
testcase (599 bytes, application/x-tar)
2015-09-30 13:34 UTC, Rafael Ávila de Espíndola
Details
patch (488 bytes, patch)
2015-09-30 13:35 UTC, Rafael Ávila de Espíndola
Details | Diff
x86_64 testcase (577 bytes, application/x-tar)
2015-10-01 17:08 UTC, Rafael Ávila de Espíndola
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rafael Ávila de Espíndola 2015-09-30 13:34:28 UTC
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.
Comment 1 Rafael Ávila de Espíndola 2015-09-30 13:35:17 UTC
Created attachment 8653 [details]
patch
Comment 2 Sourceware Commits 2015-09-30 15:46: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=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.
Comment 3 H.J. Lu 2015-09-30 15:48:25 UTC
(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
Comment 4 Rafael Ávila de Espíndola 2015-10-01 17:08:58 UTC
Created attachment 8665 [details]
x86_64 testcase
Comment 5 Sourceware Commits 2015-10-01 17:51:27 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=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.