Print more information about undefined symbols.

llvm-svn: 248382
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 765e539..57180df 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -288,6 +288,27 @@
   }
 }
 
+template <class ELFT>
+static void undefError(const SymbolTable &S, const SymbolBody &Sym) {
+  typedef typename ELFFile<ELFT>::Elf_Sym Elf_Sym;
+  typedef typename ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
+
+  const Elf_Sym &SymE = cast<ELFSymbolBody<ELFT>>(Sym).Sym;
+  ELFFileBase *SymFile = nullptr;
+
+  for (const std::unique_ptr<ObjectFileBase> &F : S.getObjectFiles()) {
+    const auto &File = cast<ObjectFile<ELFT>>(*F);
+    Elf_Sym_Range Syms = File.getObj()->symbols(File.getSymbolTable());
+    if (&SymE > Syms.begin() && &SymE < Syms.end())
+      SymFile = F.get();
+  }
+  if (SymFile)
+    error(Twine("undefined symbol: ") + Sym.getName() + " in " +
+          SymFile->getName());
+  else
+    error(Twine("undefined symbol: ") + Sym.getName());
+}
+
 // Create output section objects and add them to OutputSections.
 template <class ELFT> void Writer<ELFT>::createSections() {
   SmallDenseMap<SectionKey<ELFT::Is64Bits>, OutputSection<ELFT> *> Map;
@@ -331,7 +352,7 @@
     StringRef Name = P.first;
     SymbolBody *Body = P.second->Body;
     if (Body->isStrongUndefined())
-      error(Twine("undefined symbol: ") + Name);
+      undefError<ELFT>(Symtab, *Body);
 
     if (auto *C = dyn_cast<DefinedCommon<ELFT>>(Body))
       CommonSymbols.push_back(C);
diff --git a/lld/test/elf2/undef.s b/lld/test/elf2/undef.s
index 2196c1d..9e27bc0 100644
--- a/lld/test/elf2/undef.s
+++ b/lld/test/elf2/undef.s
@@ -1,6 +1,6 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
 # RUN: not lld -flavor gnu2 %t -o %t2 2>&1 | FileCheck %s
-# CHECK: undefined symbol: foo
+# CHECK: undefined symbol: foo in {{.*}}
 # REQUIRES: x86
 
   .globl _start;