From 6173f848b965edac09579a6132888bc24e9041e8 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 23 Sep 2015 14:37:01 +0000 Subject: [PATCH] Print more information about undefined symbols. llvm-svn: 248382 --- lld/ELF/Writer.cpp | 23 ++++++++++++++++++++++- lld/test/elf2/undef.s | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp index 765e53916357..57180df89933 100644 --- a/lld/ELF/Writer.cpp +++ b/lld/ELF/Writer.cpp @@ -288,6 +288,27 @@ void Writer::scanRelocs(const InputSection &C) { } } +template +static void undefError(const SymbolTable &S, const SymbolBody &Sym) { + typedef typename ELFFile::Elf_Sym Elf_Sym; + typedef typename ELFFile::Elf_Sym_Range Elf_Sym_Range; + + const Elf_Sym &SymE = cast>(Sym).Sym; + ELFFileBase *SymFile = nullptr; + + for (const std::unique_ptr &F : S.getObjectFiles()) { + const auto &File = cast>(*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 void Writer::createSections() { SmallDenseMap, OutputSection *> Map; @@ -331,7 +352,7 @@ template void Writer::createSections() { StringRef Name = P.first; SymbolBody *Body = P.second->Body; if (Body->isStrongUndefined()) - error(Twine("undefined symbol: ") + Name); + undefError(Symtab, *Body); if (auto *C = dyn_cast>(Body)) CommonSymbols.push_back(C); diff --git a/lld/test/elf2/undef.s b/lld/test/elf2/undef.s index 2196c1d59674..9e27bc082e64 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;