Bug 26939 - gold: Don't relax R_X86_64_GOTPCRELX when addend != -4
Summary: gold: Don't relax R_X86_64_GOTPCRELX when addend != -4
Status: RESOLVED FIXED
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.36
: P2 normal
Target Milestone: 2.36
Assignee: Cary Coutant
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-11-24 18:09 UTC by Fangrui Song
Modified: 2020-11-29 14:15 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed: 2020-11-24 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Fangrui Song 2020-11-24 18:09:56 UTC
Clang emits movl x@GOTPCREL+4(%rip), %eax for the following code:

   void x();
   int foo() { return (long)x >> 32; }

HJ Lu said this is valid: https://sourceware.org/pipermail/binutils/2020-November/114264.html

---

Let's compare the -no-pie linked output of `movl foo@GOTPCREL(%rip), %eax; movl foo@GOTPCREL+4(%rip), %eax`:

GNU ld (the second instruction loads the high 32-bit address):

0000000000401000 <foo-0xd>:
   401000:       48 c7 c0 0d 10 40 00    mov    rax,0x40100d
   401007:       8b 05 ef 1f 00 00       mov    eax,DWORD PTR [rip+0x1fef]        # 402ffc <.got+0x4>

gold (the second instruction loads the address of foo plus 4; it should be changed to not relax the instruction):

00000000004000e8 <foo-0xd>:
   4000e8:       48 8d 05 06 00 00 00    lea    rax,[rip+0x6]        # 4000f5 <foo>
   4000ef:       8d 05 04 00 00 00       lea    eax,[rip+0x4]        # 4000f9 <foo+0x4>
Comment 1 Sourceware Commits 2020-11-24 19:33:59 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=d0089f12f6f40e7b69fd432ef4915908407379f2

commit d0089f12f6f40e7b69fd432ef4915908407379f2
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Nov 24 11:32:14 2020 -0800

    ld/x86-64: Add PR gold/26939 tests
    
    GOTPCRELX relocations can be transformed only when addend == -4.  Add
    tests for GOTPCRELX relocations with addend != -4.
    
            PR gold/26939
            * testsuite/ld-x86-64/pr26939-x32.d: New file.
            * testsuite/ld-x86-64/pr26939.d: Likewise.
            * testsuite/ld-x86-64/pr26939.s: Likewise.
            * testsuite/ld-x86-64/x86-64.exp: Run PR gold/26939 tests.
Comment 2 H.J. Lu 2020-11-24 22:39:16 UTC
A patch is posted at

https://sourceware.org/pipermail/binutils/2020-November/114272.html
Comment 3 Sourceware Commits 2020-11-29 14:08:51 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=568cbddc710f6cb6be752f390a1521cd5556ad9b

commit 568cbddc710f6cb6be752f390a1521cd5556ad9b
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Sun Nov 29 06:00:37 2020 -0800

    gold: Convert x86-64 GOTPCRELX only if addend == -4
    
    Convert x86-64 GOTPCRELX relocations only if addend == -4.
    
            PR gold/26939
            * x86_64.cc (Target_x86_64<size>::Scan::local): Check
            get_r_addend() == -4 for GOTPCRELX conversion.
            (Target_x86_64<size>::Scan::global): Likewise.
            (Target_x86_64<size>::Relocate::relocate): Likewise.
            * testsuite/Makefile.am (check_DATA): Add
            x86_64_mov_to_lea15.stdout and x86_64_mov_to_lea16.stdout.
            (MOSTLYCLEANFILES): Add x86_64_mov_to_lea15 and
            x86_64_mov_to_lea16.
            (x86_64_mov_to_lea9.o): New target.
            (x86_64_mov_to_lea10.o): Likewise.
            (x86_64_mov_to_lea15): Likewise.
            (x86_64_mov_to_lea16): Likewise.
            (x86_64_mov_to_lea15.stdout): Likewise.
            (x86_64_mov_to_lea16.stdout): Likewise.
            * testsuite/Makefile.in: Regenerated.
            * testsuite/x86_64_mov_to_lea.sh: Updated.
            * testsuite/x86_64_mov_to_lea5.s: New file.
Comment 4 H.J. Lu 2020-11-29 14:15:37 UTC
Fixed for 2.36.