<NME> x86-64-tls-pie.s
<BEF> ADDFILE
<MSG> Make TLS work for PIE executables on x86-64.

While trying to get PIE work on CloudABI for x86-64, I noticed that even
though GNU ld would generate functional binaries, LLD would not. It
turns out that we generate relocations for referencing TLS objects
inside of the text segment, which shouldn't happen.

This change extends the isRelRelative() function to list some additional
relocation types that should be treated as relative. This makes my C
library unit testing binary work on x86-64.

Approved by:	ruiu
Differential Revision:	http://reviews.llvm.org/D18688
Fixes bug:	https://llvm.org/bugs/show_bug.cgi?id=27174


git-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@265462 91177308-0d34-0410-b5e6-96231b3b80d8

<DFF> @@ -0,0 +1,26 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-cloudabi %s -o %t1.o
+# RUN: ld.lld -pie %t1.o -o %t
+# RUN: llvm-readobj -r %t | FileCheck %s
+
+# Bug 27174: R_X86_64_TPOFF32 and R_X86_64_GOTTPOFF relocations should
+# be eliminated when building a PIE executable, as the static TLS layout
+# is fixed.
+#
+# CHECK:      Relocations [
+# CHECK-NEXT: ]
+
+	.globl	_start
+_start:
+	movq	%fs:0, %rax
+	movl	$3, i@TPOFF(%rax)
+
+	movq	%fs:0, %rdx
+	movq	i@GOTTPOFF(%rip), %rcx
+	movl	$3, (%rdx,%rcx)
+
+	.section	.tbss.i,"awT",@nobits
+	.globl	i
+i:
+	.long	0
+	.size	i, 4

{'commit': 'diff --git a/ELF/Target.cpp b/ELF/Target.cpp\nindex fe4d89a35..a5893d231 100644\n--- a/ELF/Target.cpp\n+++ b/ELF/Target.cpp\n@@ -818,6 +818,8 @@ bool X86_64TargetInfo::isRelRelative(uint32_t Type) const {\n   switch (Type) {\n   default:\n     return false;\n+  case R_X86_64_GOTTPOFF:\n+  case R_X86_64_TPOFF32:\n   case R_X86_64_DTPOFF32:\n   case R_X86_64_DTPOFF64:\n   case R_X86_64_PC8:\ndiff --git a/test/ELF/x86-64-tls-pie.s b/test/ELF/x86-64-tls-pie.s\nnew file mode 100644\nindex 000000000..5ef0f54f4\n--- /dev/null\n+++ b/test/ELF/x86-64-tls-pie.s\n@@ -0,0 +1,26 @@\n+# REQUIRES: x86\n+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-cloudabi %s -o %t1.o\n+# RUN: ld.lld -pie %t1.o -o %t\n+# RUN: llvm-readobj -r %t | FileCheck %s\n+\n+# Bug 27174: R_X86_64_TPOFF32 and R_X86_64_GOTTPOFF relocations should\n+# be eliminated when building a PIE executable, as the static TLS layout\n+# is fixed.\n+#\n+# CHECK:      Relocations [\n+# CHECK-NEXT: ]\n+\n+\t.globl\t_start\n+_start:\n+\tmovq\t%fs:0, %rax\n+\tmovl\t$3, i@TPOFF(%rax)\n+\n+\tmovq\t%fs:0, %rdx\n+\tmovq\ti@GOTTPOFF(%rip), %rcx\n+\tmovl\t$3, (%rdx,%rcx)\n+\n+\t.section\t.tbss.i,"awT",@nobits\n+\t.globl\ti\n+i:\n+\t.long\t0\n+\t.size\ti, 4\n', 'commit_subject': 'Make TLS work for PIE executables on x86-64.', 'repo_name': 'llvm-mirror/lld', 'committer': {'name': 'Ed Schouten', 'email': 'a162caffcb6897220f4d1cf28164d73cb91993a8@nuxi.nl', 'time_sec': '1459889510', 'tz_offset': '0', 'date': {'seconds': '1459889510'}}, 'commit_tree': '518c98c5070ada96b6ad6d7aae3731205744989a', 'commit_message': "Make TLS work for PIE executables on x86-64.\n\nWhile trying to get PIE work on CloudABI for x86-64, I noticed that even\nthough GNU ld would generate functional binaries, LLD would not. It\nturns out that we generate relocations for referencing TLS objects\ninside of the text segment, which shouldn't happen.\n\nThis change extends the isRelRelative() function to list some additional\nrelocation types that should be treated as relative. This makes my C\nlibrary unit testing binary work on x86-64.\n\nApproved by:\truiu\nDifferential Revision:\thttp://reviews.llvm.org/D18688\nFixes bug:\thttps://llvm.org/bugs/show_bug.cgi?id=27174\n\n\ngit-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@265462 91177308-0d34-0410-b5e6-96231b3b80d8\n", 'language_name': 'C++', 'license': 'apache-2.0', 'hunks': '@@ -0,0 +1,26 @@\n+# REQUIRES: x86\n+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-cloudabi %s -o %t1.o\n+# RUN: ld.lld -pie %t1.o -o %t\n+# RUN: llvm-readobj -r %t | FileCheck %s\n+\n+# Bug 27174: R_X86_64_TPOFF32 and R_X86_64_GOTTPOFF relocations should\n+# be eliminated when building a PIE executable, as the static TLS layout\n+# is fixed.\n+#\n+# CHECK:      Relocations [\n+# CHECK-NEXT: ]\n+\n+\t.globl\t_start\n+_start:\n+\tmovq\t%fs:0, %rax\n+\tmovl\t$3, i@TPOFF(%rax)\n+\n+\tmovq\t%fs:0, %rdx\n+\tmovq\ti@GOTTPOFF(%rip), %rcx\n+\tmovl\t$3, (%rdx,%rcx)\n+\n+\t.section\t.tbss.i,"awT",@nobits\n+\t.globl\ti\n+i:\n+\t.long\t0\n+\t.size\ti, 4\n', 'addition_count': 26, 'deletion_count': 0, 'src_file': '/dev/null', 'tgt_file': 'b/test/ELF/x86-64-tls-pie.s', 'file_extension': '.s', 'before_file': 'ADDFILE', 'message': "Make TLS work for PIE executables on x86-64.\n\nWhile trying to get PIE work on CloudABI for x86-64, I noticed that even\nthough GNU ld would generate functional binaries, LLD would not. It\nturns out that we generate relocations for referencing TLS objects\ninside of the text segment, which shouldn't happen.\n\nThis change extends the isRelRelative() function to list some additional\nrelocation types that should be treated as relative. This makes my C\nlibrary unit testing binary work on x86-64.\n\nApproved by:\truiu\nDifferential Revision:\thttp://reviews.llvm.org/D18688\nFixes bug:\thttps://llvm.org/bugs/show_bug.cgi?id=27174\n\n\ngit-svn-id: f6089bf0e6284f307027cef4f64114ee9ebb0424@265462 91177308-0d34-0410-b5e6-96231b3b80d8\n", 'author_name': 'Ed Schouten', 'lang': 's'}