[PATCH] D24832: [ELF] Resolve weak undefined TLS symbols when no phdr is available

Davide Italiano via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 23 11:47:27 PDT 2016


davide updated this revision to Diff 72315.
davide added a comment.

Update after Rafael's feedback.


https://reviews.llvm.org/D24832

Files:
  ELF/InputSection.cpp
  test/ELF/tls-weak-undef.s

Index: test/ELF/tls-weak-undef.s
===================================================================
--- /dev/null
+++ test/ELF/tls-weak-undef.s
@@ -0,0 +1,16 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+// RUN: ld.lld %t.o -o %t --gc-sections
+
+// RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux \
+// RUN:   %p/Inputs/tls-in-archive.s -o %t1.o
+// RUN: llvm-ar cru %t.a %t1.o
+// RUN: ld.lld %t.o %t.a -o %t
+
+// Check that lld doesn't crash because we don't reference
+// the TLS phdr when it's not created.
+        .globl  _start
+_start:
+        movq    foo at gottpoff(%rip), %rax
+        .section        .tbss,"awT", at nobits
+        .weak   foo
Index: ELF/InputSection.cpp
===================================================================
--- ELF/InputSection.cpp
+++ ELF/InputSection.cpp
@@ -241,6 +241,14 @@
   case R_RELAX_TLS_IE_TO_LE:
   case R_RELAX_TLS_GD_TO_LE:
   case R_TLS:
+    // A weak undefined TLS symbol resolves to the base of the TLS
+    // block, i.e. gets a value of zero. If we pass --gc-sections to
+    // lld and .tbss is not referenced, it gets reclaimed and we don't
+    // create a TLS program header. Therefore, we resolve this
+    // statically to zero.
+    if (Body.isTls() && (Body.isLazy() || Body.isUndefined()) &&
+        Body.symbol()->isWeak())
+      return 0;
     if (Target->TcbSize)
       return Body.getVA<ELFT>(A) +
              alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D24832.72315.patch
Type: text/x-patch
Size: 1492 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160923/4af7b706/attachment.bin>


More information about the llvm-commits mailing list