[lld] r334672 - [ELF][X86_64] Use R_GOTREL_FROM_END instead of R_GOTREL for R_X86_64_GOTOFF64

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 13 16:29:28 PDT 2018


Author: maskray
Date: Wed Jun 13 16:29:28 2018
New Revision: 334672

URL: http://llvm.org/viewvc/llvm-project?rev=334672&view=rev
Log:
[ELF][X86_64] Use R_GOTREL_FROM_END instead of R_GOTREL for R_X86_64_GOTOFF64

Summary:
R_X86_64_GOTOFF64: S + A - GOT
R_X86_64_GOTPC{32,64}: GOT + A - P (R_GOTONLY_PC_FROM_END)

R_X86_64_GOTOFF64 should use R_GOTREL_FROM_END so that in conjunction with
R_X86_64_GOTPC{32,64}, the `GOT` term is neutralized. This also matches
the handling of R_386_GOTOFF (S + A - GOT).

Reviewers: ruiu, espindola

Subscribers: emaste, arichardson, llvm-commits

Differential Revision: https://reviews.llvm.org/D48095

Added:
    lld/trunk/test/ELF/x86-64-reloc-gotpc64.s
      - copied, changed from r334647, lld/trunk/test/ELF/x86-64-reloc-got.s
Removed:
    lld/trunk/test/ELF/x86-64-reloc-got.s
Modified:
    lld/trunk/ELF/Arch/X86_64.cpp
    lld/trunk/test/ELF/x86-64-reloc-gotoff64.s

Modified: lld/trunk/ELF/Arch/X86_64.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Arch/X86_64.cpp?rev=334672&r1=334671&r2=334672&view=diff
==============================================================================
--- lld/trunk/ELF/Arch/X86_64.cpp (original)
+++ lld/trunk/ELF/Arch/X86_64.cpp Wed Jun 13 16:29:28 2018
@@ -106,7 +106,7 @@ RelExpr X86_64<ELFT>::getRelExpr(RelType
   case R_X86_64_GOTTPOFF:
     return R_GOT_PC;
   case R_X86_64_GOTOFF64:
-    return R_GOTREL;
+    return R_GOTREL_FROM_END;
   case R_X86_64_GOTPC32:
   case R_X86_64_GOTPC64:
     return R_GOTONLY_PC_FROM_END;

Removed: lld/trunk/test/ELF/x86-64-reloc-got.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/x86-64-reloc-got.s?rev=334671&view=auto
==============================================================================
--- lld/trunk/test/ELF/x86-64-reloc-got.s (original)
+++ lld/trunk/test/ELF/x86-64-reloc-got.s (removed)
@@ -1,20 +0,0 @@
-// REQUIRES: x86
-// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld %t.o -shared -o %t.so
-// RUN: llvm-readelf -s %t.so | FileCheck %s -check-prefix=SECTION
-// RUN: llvm-objdump -d %t.so | FileCheck %s
-
-// SECTION: .got PROGBITS 0000000000003070 003070 000000
-
-// 0x3070 (.got end) - 0x1007 = 8297
-// CHECK: gotpc32:
-// CHECK-NEXT: 1000: {{.*}} leaq 8297(%rip), %r15
-.global gotpc32
-gotpc32:
-  leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
-
-// CHECK: gotpc64:
-// CHECK-NEXT: 1007: {{.*}} movabsq $8297, %r11
-.global gotpc64
-gotpc64:
-  movabsq $_GLOBAL_OFFSET_TABLE_-., %r11

Modified: lld/trunk/test/ELF/x86-64-reloc-gotoff64.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/x86-64-reloc-gotoff64.s?rev=334672&r1=334671&r2=334672&view=diff
==============================================================================
--- lld/trunk/test/ELF/x86-64-reloc-gotoff64.s (original)
+++ lld/trunk/test/ELF/x86-64-reloc-gotoff64.s Wed Jun 13 16:29:28 2018
@@ -1,17 +1,32 @@
 // REQUIRES: x86
-
 // RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
-// RUN: ld.lld -shared -o %t.so %t.o
-// RUN: llvm-readelf -sections %t.so | FileCheck %s
-// RUN: llvm-objdump -d %t.so | FileCheck -check-prefix=DISASM %s
+// RUN: ld.lld %t.o -shared -o %t.so
+// RUN: llvm-readelf -s %t.so | FileCheck %s -check-prefix=SECTION
+// RUN: llvm-objdump -d %t.so | FileCheck %s
+
+// SECTION: .dynamic DYNAMIC 0000000000003000
+// SECTION: .got PROGBITS 0000000000003070 003070 000000
 
-// CHECK: .dynamic DYNAMIC  0000000000002000 002000
-// CHECK: .got     PROGBITS 0000000000002070 002070
+// All the _GLOBAL_OFFSET_TABLE_ occurrences below refer to the address
+// of GOT base, not the address of the symbol _GLOBAL_OFFSET_TABLE_. These
+// instructions are special and produce GOT base relative relocations. We
+// currently use .got end as the GOT base, which is not equal to
+// the address of the special symbol _GLOBAL_OFFSET_TABLE_.
 
-// DISASM: 1000: 48 ba 90 ff ff ff ff ff ff ff   movabsq $-112, %rdx
+// The assembly is generated by
+// gcc -O2 -S -mcmodel=medium -fPIC a.c
+// This computes the pc-relative address (runtime address) of _DYNAMIC.
+//
+// extern long _DYNAMIC[] __attribute__((visibility("hidden")));
+// long* dynamic() { return _DYNAMIC; }
 
-.global _start
-.weak _DYNAMIC
-.hidden _DYNAMIC
-_start:
-  movabsq $_DYNAMIC at GOTOFF, %rdx
+// 0x3070 (.got end) - 0x1007 = 8297
+// 0x3000 (_DYNAMIC) - 0x3070 (.got end) = -112
+// CHECK:      1000: {{.*}} leaq 8297(%rip), %rdx
+// CHECK-NEXT: 1007: {{.*}} movabsq $-112, %rax
+.global dynamic
+dynamic:
+  leaq _GLOBAL_OFFSET_TABLE_(%rip), %rdx
+  movabsq $_DYNAMIC at GOTOFF, %rax
+  addq %rdx, %rax
+  ret

Copied: lld/trunk/test/ELF/x86-64-reloc-gotpc64.s (from r334647, lld/trunk/test/ELF/x86-64-reloc-got.s)
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/x86-64-reloc-gotpc64.s?p2=lld/trunk/test/ELF/x86-64-reloc-gotpc64.s&p1=lld/trunk/test/ELF/x86-64-reloc-got.s&r1=334647&r2=334672&rev=334672&view=diff
==============================================================================
--- lld/trunk/test/ELF/x86-64-reloc-got.s (original)
+++ lld/trunk/test/ELF/x86-64-reloc-gotpc64.s Wed Jun 13 16:29:28 2018
@@ -6,15 +6,9 @@
 
 // SECTION: .got PROGBITS 0000000000003070 003070 000000
 
-// 0x3070 (.got end) - 0x1007 = 8297
-// CHECK: gotpc32:
-// CHECK-NEXT: 1000: {{.*}} leaq 8297(%rip), %r15
-.global gotpc32
-gotpc32:
-  leaq _GLOBAL_OFFSET_TABLE_(%rip), %r15
-
+// 0x3070 (.got end) - 0x1000 = 8304
 // CHECK: gotpc64:
-// CHECK-NEXT: 1007: {{.*}} movabsq $8297, %r11
+// CHECK-NEXT: 1000: {{.*}} movabsq $8304, %r11
 .global gotpc64
 gotpc64:
   movabsq $_GLOBAL_OFFSET_TABLE_-., %r11




More information about the llvm-commits mailing list