Don't crash if reloc targets discarded section

Differential revision: https://reviews.llvm.org/D25433

llvm-svn: 283984
diff --git a/lld/ELF/Symbols.cpp b/lld/ELF/Symbols.cpp
index 7457224..1dcb8cb 100644
--- a/lld/ELF/Symbols.cpp
+++ b/lld/ELF/Symbols.cpp
@@ -15,6 +15,7 @@
 #include "Target.h"
 
 #include "llvm/ADT/STLExtras.h"
+#include "llvm/Support/Path.h"
 
 using namespace llvm;
 using namespace llvm::object;
@@ -24,6 +25,12 @@
 using namespace lld::elf;
 
 template <class ELFT>
+static std::string getSectionName(InputSectionBase<ELFT> *S) {
+  StringRef Filename = S->getFile()->getName();
+  return (sys::path::filename(Filename) + "(" + S->Name + ")").str();
+}
+
+template <class ELFT>
 static typename ELFT::uint getSymVA(const SymbolBody &Body,
                                     typename ELFT::uint &Addend) {
   typedef typename ELFT::uint uintX_t;
@@ -53,6 +60,11 @@
     if (!SC)
       return D.Value;
 
+    if (!SC->Live) {
+      warn("relocation refers to discarded section '" + getSectionName(SC) + "'");
+      return 0;
+    }
+
     uintX_t Offset = D.Value;
     if (D.isSection()) {
       Offset += Addend;