[ELF] - Do not crash if symbol type set to TLS when there is no tls sections.

id_000021,sig_11,src_000002,op_flip1,pos_92 from PR30540

does not have TLS sections, but type
of one of the symbol is broken and set to STT_TLS,
what resulted in a crash. Patch fixes crash.

DIfferential revision: https://reviews.llvm.org/D25083

llvm-svn: 283198
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 4819bb17..7457224 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -59,8 +59,12 @@
       Addend = 0;
     }
     uintX_t VA = (SC->OutSec ? SC->OutSec->getVA() : 0) + SC->getOffset(Offset);
-    if (D.isTls() && !Config->Relocatable)
+    if (D.isTls() && !Config->Relocatable) {
+      if (!Out<ELFT>::TlsPhdr)
+        fatal(getFilename(D.File) +
+              " has a STT_TLS symbol but doesn't have a PT_TLS section");
       return VA - Out<ELFT>::TlsPhdr->p_vaddr;
+    }
     return VA;
   }
   case SymbolBody::DefinedCommonKind:
diff --git a/lld/test/ELF/invalid/Inputs/tls-symbol.elf b/lld/test/ELF/invalid/Inputs/tls-symbol.elf
new file mode 100644
index 0000000..78c7598
--- /dev/null
+++ b/lld/test/ELF/invalid/Inputs/tls-symbol.elf
Binary files differ
diff --git a/lld/test/ELF/invalid/tls-symbol.s b/lld/test/ELF/invalid/tls-symbol.s
new file mode 100644
index 0000000..7c65c6c
--- /dev/null
+++ b/lld/test/ELF/invalid/tls-symbol.s
@@ -0,0 +1,5 @@
+# REQUIRES: x86
+
+## The test file contains a STT_TLS symbol but has no TLS section.
+# RUN: not ld.lld %S/Inputs/tls-symbol.elf -o %t 2>&1 | FileCheck %s
+# CHECK: has a STT_TLS symbol but doesn't have a PT_TLS section