Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

/usr/bin/ld: /tmp/lto-llvm-e9c16c.o: relocation R_X86_64_32S against `.text._ZL4foo2v' can not be used when making a shared object; recompile with -fPIC #38498

Open Quuxplusone opened 6 years ago

Quuxplusone commented 6 years ago
Bugzilla Link PR39525
Status NEW
Importance P normal
Reported by Yeoul Na (yeouln@uci.edu)
Reported on 2018-11-01 16:05:47 -0700
Last modified on 2018-12-27 11:40:25 -0800
Version trunk
Hardware PC Linux
CC craig.topper@gmail.com, llvm-bugs@lists.llvm.org, llvm-dev@redking.me.uk, rnk@google.com, spatel+llvm@rotateright.com, yeouln@uci.edu
Fixed by commit(s)
Attachments test_static.c (235 bytes, text/x-csrc)
pic.diff (702 bytes, text/plain)
Blocks
Blocked by
See also
Created attachment 21064
The C source code with inline assembly that is mis-compiled when the PIC is
enabled.

When I try to create a shared library containing inline assembly, I get this
link error.

relocation R_X86_64_32S against `.text._ZL4foo2v' can not be used when making a
shared object; recompile with -fPIC

To reproduce the error:

clang -O2 -fPIC -c test_static.c -o test_static.o
clang -fPIC -shared test_static.o -o test_static.so

objdump -Dtx test_static.o:

0000000000000000 <_Z3foov>:
   0:›  48 8b 05 00 00 00 00 ›  mov    0x0(%rip),%rax        # 7 <_Z3foov+0x7>
›   ›   ›   3: R_X86_64_GOTPCREL›   offset-0x4
   7:›  8b 00                ›  mov    (%rax),%eax
   9:›  65 67 48 c7 00 00 00 ›  movq   $0x0,%gs:(%eax)
  10:›  00 00•
›   ›   ›   e: R_X86_64_32S›.text+0x20
  12:›  48 8d 3d 00 00 00 00 ›  lea    0x0(%rip),%rdi        # 19 <_Z3foov+0x19>
›   ›   ›   15: R_X86_64_PC32›  .Lstr-0x4
  19:›  e9 00 00 00 00       ›  jmpq   1e <_Z3foov+0x1e>
›   ›   ›   1a: R_X86_64_PLT32› puts-0x4
  1e:›  66 90                ›  xchg   %ax,%ax

0000000000000020 <_ZL4foo2v>:
  20:›  48 8d 3d 00 00 00 00 ›  lea    0x0(%rip),%rdi        # 27 <_ZL4foo2v+0x7>
›   ›   ›   23: R_X86_64_PC32›  .Lstr.2-0x4
  27:›  e9 00 00 00 00       ›  jmpq   2c <_ZL4foo2v+0xc>
›   ›   ›   28: R_X86_64_PLT32› puts-0x4

The compiler generates the position dependent code (movq at address 9 with
R_X86_64_32S), even though the code is compiled with -fPIC. The mis-compilation
appears only when the function foo2() is a static function. I believe the
correct compilation of address 9 should be like the following <FIX> block.

<FIX>
   9: 48 8d 0d 20 00 00 00  lea    0x20(%rip),%rcx        # 30 <_ZL4foo2v>
  10: 65 67 48 89 08        mov    %rcx,%gs:(%eax)
Quuxplusone commented 6 years ago

Attached test_static.c (235 bytes, text/x-csrc): The C source code with inline assembly that is mis-compiled when the PIC is enabled.

Quuxplusone commented 6 years ago
The following patch should work. I will send it to llvm-commits after checking
the formatting.

Index: lib/Target/X86/X86ISelLowering.cpp
===================================================================
--- lib/Target/X86/X86ISelLowering.cpp  (revision 345806)
+++ lib/Target/X86/X86ISelLowering.cpp  (working copy)
@@ -41119,7 +41119,9 @@
     // In any sort of PIC mode addresses need to be computed at runtime by
     // adding in a register or some sort of table lookup.  These can't
     // be used as immediates.
-    if (Subtarget.isPICStyleGOT() || Subtarget.isPICStyleStubPIC())
+    if (Subtarget.isPICStyleGOT() ||
+        Subtarget.isPICStyleStubPIC() ||
+        Subtarget.isPICStyleRIPRel())
       return;

     // If we are in non-pic codegen mode, we allow the address of a global (with
Quuxplusone commented 6 years ago

Attached pic.diff (702 bytes, text/plain): A proposed patch

Quuxplusone commented 5 years ago

See also https://bugs.llvm.org/show_bug.cgi?id=4752 for a related bug.