[PATCH] D55360: [LLD][ELF] - Report a location for symbols from the linker script when reporting an error.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 19 02:22:59 PST 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL349612: [LLD][ELF] - Report a location for symbols from the linker script when… (authored by grimar, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D55360?vs=176944&id=178851#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D55360/new/

https://reviews.llvm.org/D55360

Files:
  lld/trunk/ELF/Relocations.cpp
  lld/trunk/test/ELF/linkerscript/symbol-location.s


Index: lld/trunk/test/ELF/linkerscript/symbol-location.s
===================================================================
--- lld/trunk/test/ELF/linkerscript/symbol-location.s
+++ lld/trunk/test/ELF/linkerscript/symbol-location.s
@@ -0,0 +1,15 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
+# RUN: echo "foo = 1;" > %t.script
+# RUN: not ld.lld -pie -o %t --script %t.script %t.o 2>&1 | FileCheck %s
+
+## Here we check that symbol 'foo' location is reported properly.
+
+# CHECK: error: relocation R_X86_64_PLT32 cannot refer to absolute symbol: foo
+# CHECK: >>> defined in {{.*}}.script:1
+# CHECK: >>> referenced by {{.*}}.o:(.text+0x1)
+
+.text
+.globl _start
+_start:
+ call foo at PLT
Index: lld/trunk/ELF/Relocations.cpp
===================================================================
--- lld/trunk/ELF/Relocations.cpp
+++ lld/trunk/ELF/Relocations.cpp
@@ -66,6 +66,14 @@
 using namespace lld;
 using namespace lld::elf;
 
+static Optional<std::string> getLinkerScriptLocation(const Symbol &Sym) {
+  for (BaseCommand *Base : Script->SectionCommands)
+    if (auto *Cmd = dyn_cast<SymbolAssignment>(Base))
+      if (Cmd->Sym == &Sym)
+        return Cmd->Location;
+  return None;
+}
+
 // Construct a message in the following format.
 //
 // >>> defined in /home/alice/src/foo.o
@@ -73,8 +81,13 @@
 // >>>               /home/alice/src/bar.o:(.text+0x1)
 static std::string getLocation(InputSectionBase &S, const Symbol &Sym,
                                uint64_t Off) {
-  std::string Msg =
-      "\n>>> defined in " + toString(Sym.File) + "\n>>> referenced by ";
+  std::string Msg = "\n>>> defined in ";
+  if (Sym.File)
+    Msg += toString(Sym.File);
+  else if (Optional<std::string> Loc = getLinkerScriptLocation(Sym))
+    Msg += *Loc;
+
+  Msg += "\n>>> referenced by ";
   std::string Src = S.getSrcMsg(Sym, Off);
   if (!Src.empty())
     Msg += Src + "\n>>>               ";


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D55360.178851.patch
Type: text/x-patch
Size: 1949 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181219/fa6fbeb7/attachment-0001.bin>


More information about the llvm-commits mailing list