[lld] r288019 - [ELF] Print file:line for 'undefined section' errors

Eugene Leviant via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 28 01:58:04 PST 2016


Author: evgeny777
Date: Mon Nov 28 03:58:04 2016
New Revision: 288019

URL: http://llvm.org/viewvc/llvm-project?rev=288019&view=rev
Log:
[ELF] Print file:line for 'undefined section' errors

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

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/ELF/LinkerScript.h
    lld/trunk/ELF/ScriptParser.cpp
    lld/trunk/ELF/ScriptParser.h
    lld/trunk/test/ELF/linkerscript/alignof.s
    lld/trunk/test/ELF/linkerscript/loadaddr.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=288019&r1=288018&r2=288019&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Mon Nov 28 03:58:04 2016
@@ -884,13 +884,15 @@ template <class ELFT> bool LinkerScript<
 }
 
 template <class ELFT>
-const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(StringRef Name) {
+const OutputSectionBase *LinkerScript<ELFT>::getOutputSection(const Twine &Loc,
+                                                              StringRef Name) {
   static OutputSectionBase FakeSec("", 0, 0);
 
   for (OutputSectionBase *Sec : *OutputSections)
     if (Sec->getName() == Name)
       return Sec;
-  error("undefined section " + Name);
+
+  error(Loc + ": undefined section " + Name);
   return &FakeSec;
 }
 
@@ -1697,6 +1699,7 @@ Expr ScriptParser::readPrimary() {
     return readParenExpr();
 
   StringRef Tok = next();
+  std::string Location = currentLocation();
 
   if (Tok == "~") {
     Expr E = readPrimary();
@@ -1711,15 +1714,16 @@ Expr ScriptParser::readPrimary() {
   // https://sourceware.org/binutils/docs/ld/Builtin-Functions.html.
   if (Tok == "ADDR") {
     StringRef Name = readParenLiteral();
-    return {
-        [=](uint64_t Dot) { return ScriptBase->getOutputSection(Name)->Addr; },
-        [=] { return false; },
-        [=] { return ScriptBase->getOutputSection(Name); }};
+    return {[=](uint64_t Dot) {
+              return ScriptBase->getOutputSection(Location, Name)->Addr;
+            },
+            [=] { return false; },
+            [=] { return ScriptBase->getOutputSection(Location, Name); }};
   }
   if (Tok == "LOADADDR") {
     StringRef Name = readParenLiteral();
     return [=](uint64_t Dot) {
-      return ScriptBase->getOutputSection(Name)->getLMA();
+      return ScriptBase->getOutputSection(Location, Name)->getLMA();
     };
   }
   if (Tok == "ASSERT")
@@ -1776,7 +1780,7 @@ Expr ScriptParser::readPrimary() {
   if (Tok == "ALIGNOF") {
     StringRef Name = readParenLiteral();
     return [=](uint64_t Dot) {
-      return ScriptBase->getOutputSection(Name)->Addralign;
+      return ScriptBase->getOutputSection(Location, Name)->Addralign;
     };
   }
   if (Tok == "SIZEOF_HEADERS")

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=288019&r1=288018&r2=288019&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Mon Nov 28 03:58:04 2016
@@ -194,7 +194,8 @@ public:
   virtual bool isDefined(StringRef S) = 0;
   virtual bool isAbsolute(StringRef S) = 0;
   virtual const OutputSectionBase *getSymbolSection(StringRef S) = 0;
-  virtual const OutputSectionBase *getOutputSection(StringRef S) = 0;
+  virtual const OutputSectionBase *getOutputSection(const Twine &Loc,
+                                                    StringRef S) = 0;
   virtual uint64_t getOutputSectionSize(StringRef S) = 0;
 };
 
@@ -245,7 +246,8 @@ public:
   bool isDefined(StringRef S) override;
   bool isAbsolute(StringRef S) override;
   const OutputSectionBase *getSymbolSection(StringRef S) override;
-  const OutputSectionBase *getOutputSection(StringRef S) override;
+  const OutputSectionBase *getOutputSection(const Twine &Loc,
+                                            StringRef S) override;
   uint64_t getOutputSectionSize(StringRef S) override;
 
   std::vector<OutputSectionBase *> *OutputSections;

Modified: lld/trunk/ELF/ScriptParser.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.cpp?rev=288019&r1=288018&r2=288019&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.cpp (original)
+++ lld/trunk/ELF/ScriptParser.cpp Mon Nov 28 03:58:04 2016
@@ -172,6 +172,13 @@ void ScriptParserBase::expect(StringRef
     setError(Expect + " expected, but got " + Tok);
 }
 
+std::string ScriptParserBase::currentLocation() {
+  MemoryBufferRef MB = currentBuffer();
+  return (MB.getBufferIdentifier() + ":" +
+          Twine(getPos(MB.getBuffer(), Tokens[Pos - 1]).first))
+      .str();
+}
+
 // Returns true if string 'Bigger' contains string 'Shorter'.
 static bool containsString(StringRef Bigger, StringRef Shorter) {
   const char *BiggerEnd = Bigger.data() + Bigger.size();

Modified: lld/trunk/ELF/ScriptParser.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/ScriptParser.h?rev=288019&r1=288018&r2=288019&view=diff
==============================================================================
--- lld/trunk/ELF/ScriptParser.h (original)
+++ lld/trunk/ELF/ScriptParser.h Mon Nov 28 03:58:04 2016
@@ -32,6 +32,7 @@ public:
   void skip();
   bool consume(StringRef Tok);
   void expect(StringRef Expect);
+  std::string currentLocation();
 
   std::vector<MemoryBufferRef> MBs;
   std::vector<StringRef> Tokens;

Modified: lld/trunk/test/ELF/linkerscript/alignof.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/alignof.s?rev=288019&r1=288018&r2=288019&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/alignof.s (original)
+++ lld/trunk/test/ELF/linkerscript/alignof.s Mon Nov 28 03:58:04 2016
@@ -23,7 +23,7 @@
 # RUN: }" > %t.script
 # RUN: not ld.lld -o %t1 --script %t.script %t 2>&1 \
 # RUN:  | FileCheck -check-prefix=ERR %s
-# ERR: undefined section .foo
+# ERR: {{.*}}.script:1: undefined section .foo
 .global _start
 _start:
  nop

Modified: lld/trunk/test/ELF/linkerscript/loadaddr.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/loadaddr.s?rev=288019&r1=288018&r2=288019&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/loadaddr.s (original)
+++ lld/trunk/test/ELF/linkerscript/loadaddr.s Mon Nov 28 03:58:04 2016
@@ -23,7 +23,7 @@
 # CHECK-NEXT: 0000000000003000         *ABS*     00000000 ccc_lma
 # CHECK-NEXT: 0000000000004000         *ABS*     00000000 ddd_lma
 # CHECK-NEXT: 0000000000004008         *ABS*     00000000 txt_lma
-# ERROR: undefined section .zzz
+# ERROR: {{.*}}.script:1: undefined section .zzz
 
 .global _start
 _start:




More information about the llvm-commits mailing list