[ELF][PPC64] Don't apply LD->LE relaxation on R_PPC64_GOT_DTPREL16*

In ELF v2 ABI, R_PPC64_GOT_DTPREL16* are not relaxed.

This family of relocation types are used for variables outside of 2GiB
of the TLS block. 2 instructions cannot materialize a DTPREL offset that
is not 32-bit.

Reviewed By: ruiu

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

git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@362357 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/ELF/Arch/PPC64.cpp b/ELF/Arch/PPC64.cpp
index ae0d63a..a79e9c8 100644
--- a/ELF/Arch/PPC64.cpp
+++ b/ELF/Arch/PPC64.cpp
@@ -445,10 +445,6 @@
   case R_PPC64_DTPREL16_DS:
   case R_PPC64_DTPREL16_LO:
   case R_PPC64_DTPREL16_LO_DS:
-  case R_PPC64_GOT_DTPREL16_HA:
-  case R_PPC64_GOT_DTPREL16_LO_DS:
-  case R_PPC64_GOT_DTPREL16_DS:
-  case R_PPC64_GOT_DTPREL16_HI:
     relocateOne(Loc, Type, Val);
     break;
   default:
diff --git a/ELF/Relocations.cpp b/ELF/Relocations.cpp
index b02e1cc..fe10234 100644
--- a/ELF/Relocations.cpp
+++ b/ELF/Relocations.cpp
@@ -257,13 +257,8 @@
   }
 
   // Local-Dynamic sequence where offset of tls variable relative to dynamic
-  // thread pointer is stored in the got.
+  // thread pointer is stored in the got. This cannot be relaxed to Local-Exec.
   if (Expr == R_TLSLD_GOT_OFF) {
-    // Local-Dynamic relocs can be relaxed to local-exec
-    if (!Config->Shared) {
-      C.Relocations.push_back({R_RELAX_TLS_LD_TO_LE, Type, Offset, Addend, &Sym});
-      return 1;
-    }
     if (!Sym.isInGot()) {
       In.Got->addEntry(Sym);
       uint64_t Off = Sym.getGotOffset();
diff --git a/test/ELF/ppc64-dtprel.s b/test/ELF/ppc64-dtprel.s
index a7ece1d..4bb767a 100644
--- a/test/ELF/ppc64-dtprel.s
+++ b/test/ELF/ppc64-dtprel.s
@@ -97,15 +97,6 @@
         mtlr 0
         blr
 
-        .globl test_got_dtprel
-        .p2align 4
-        .type test_got_dtprel,@function
-test_got_dtprel:
-         addis 3, 2, i@got@dtprel@ha
-         ld 3, i@got@dtprel@l(3)
-         addis 3, 2, i@got@dtprel@h
-         addi 3, 2, i@got@dtprel
-
         .section        .debug_addr,"",@progbits
         .quad   i@dtprel+32768
 
@@ -137,10 +128,6 @@
 // InputRelocs: R_PPC64_DTPREL16_HIGHER   {{[0-9a-f]+}} k + 0
 // InputRelocs: R_PPC64_DTPREL16_HI       {{[0-9a-f]+}} k + 0
 // InputRelocs: R_PPC64_DTPREL16_LO       {{[0-9a-f]+}} k + 0
-// InputRelocs: R_PPC64_GOT_DTPREL16_HA    {{[0-9a-f]+}} i + 0
-// InputRelocs: R_PPC64_GOT_DTPREL16_LO_DS {{[0-9a-f]+}} i + 0
-// InputRelocs: R_PPC64_GOT_DTPREL16_HI    {{[0-9a-f]+}} i + 0
-// InputRelocs: R_PPC64_GOT_DTPREL16_DS    {{[0-9a-f]+}} i + 0
 // InputRelocs: Relocation section '.rela.debug_addr'
 // InputRelocs: R_PPC64_DTPREL64          {{[0-9a-f]+}} i + 8000
 
@@ -154,11 +141,11 @@
 // i@dtprel = 1024 - 0x8000 = -31744 = 0xffffffffffff8400
 // HEX-LE:      section '.got':
 // HEX-LE-NEXT: 4204f8 f8844200 00000000 00000000 00000000
-// HEX-LE-NEXT: 420508 00000000 00000000 0084ffff ffffffff
+// HEX-LE-NEXT: 420508 00000000 00000000
 
 // HEX-BE:      section '.got':
 // HEX-BE-NEXT: 4204f8 00000000 004284f8 00000000 00000000
-// HEX-BE-NEXT: 420508 00000000 00000000 ffffffff ffff8400
+// HEX-BE-NEXT: 420508 00000000 00000000
 
 // Dis:     test:
 // Dis:      addi 4, 3, -31744
@@ -186,12 +173,3 @@
 // Dis:    oris 4, 4, 63
 // Dis:    ori 4, 4, 33796
 
-// #ha(i@got@dtprel) = ((0x420510 - (.got+0x8000) + 0x8000) >> 16) & 0xffff = 0
-// #lo(i@got@dtprel) = (0x420510 - (.got+0x8000)) & 0xffff = -32744
-// #hi(i@got@dtprel) = ((0x420510 - (.got+0x8000)) >> 16) & 0xffff = -1
-// i@got@dtprel --> 0x420510 - (.got+0x8000) = -32744
-// Dis: test_got_dtprel:
-// Dis:    addis 3, 2, 0
-// Dis:    ld 3, -32744(3)
-// Dis:    addis 3, 2, -1
-// Dis:    addi 3, 2, -32744
diff --git a/test/ELF/ppc64-ld-got-dtprel.s b/test/ELF/ppc64-ld-got-dtprel.s
new file mode 100644
index 0000000..319c14e
--- /dev/null
+++ b/test/ELF/ppc64-ld-got-dtprel.s
@@ -0,0 +1,44 @@
+# REQUIRES: ppc
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc64le %s -o %t.o
+# RUN: llvm-readobj -r %t.o | FileCheck --check-prefix=RELOCS %s
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-objdump -d --no-show-raw-insn %t.so | FileCheck %s
+## Check LD->LE relaxation does not affect R_PPC64_GOT_DTPREL16*.
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc64 %s -o %t.o
+# RUN: llvm-readobj -r %t.o | FileCheck --check-prefix=RELOCS %s
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-objdump -d --no-show-raw-insn %t.so | FileCheck %s
+# RUN: ld.lld %t.o -o %t
+# RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
+
+# RELOCS:      .rela.text {
+# RELOCS-NEXT:   R_PPC64_GOT_DTPREL16_HA i 0x0
+# RELOCS-NEXT:   R_PPC64_GOT_DTPREL16_LO_DS i 0x0
+# RELOCS-NEXT:   R_PPC64_GOT_DTPREL16_HI j 0x0
+# RELOCS-NEXT:   R_PPC64_GOT_DTPREL16_DS j 0x0
+# RELOCS-NEXT: }
+
+## ha(i@got@dtprel) = (&.got[0] - (.got+0x8000) + 0x8000 >> 16) & 0xffff = 0
+## lo(i@got@dtprel) = &.got[0] - (.got+0x8000) & 0xffff = -32768
+## hi(j@got@dtprel) = (&.got[1] - (.got+0x8000) >> 16) & 0xffff = -1
+## j@got@dtprel = &.got[1] - (.got+0x8000) = -32760
+# CHECK:      addis 3, 2, 0
+# CHECK-NEXT: ld 3, -32768(3)
+# CHECK-NEXT: addis 3, 2, -1
+# CHECK-NEXT: addi 3, 2, -32760
+
+  addis 3, 2, i@got@dtprel@ha
+  ld 3, i@got@dtprel@l(3)
+  addis 3, 2, j@got@dtprel@h
+  addi 3, 2, j@got@dtprel
+
+.section .tbss,"awT",@progbits
+.p2align 2
+i:
+  .long 0
+j:
+  .long 0