[lld] r287996 - Also skip regular symbol assignment at the start of a script.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 01:44:46 PST 2016


Author: rafael
Date: Sun Nov 27 03:44:45 2016
New Revision: 287996

URL: http://llvm.org/viewvc/llvm-project?rev=287996&view=rev
Log:
Also skip regular symbol assignment at the start of a script.

Unfortunatelly some scripts look like

kernphys = ...
. = ....

and the expectation in that every orphan section is after the
assignment.

Modified:
    lld/trunk/ELF/LinkerScript.cpp
    lld/trunk/test/ELF/linkerscript/orphan-first-cmd.s

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=287996&r1=287995&r2=287996&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Sun Nov 27 03:44:45 2016
@@ -632,21 +632,13 @@ template <class ELFT> void LinkerScript<
 //  /* The RW PT_LOAD starts here*/
 //  rw_sec : { *(rw_sec) }
 // would mean that the RW PT_LOAD would become unaligned.
-static bool shouldSkip(int CmdIndex) {
-  auto CmdIter = ScriptConfig->Commands.begin() + CmdIndex;
-  const BaseCommand &Cmd = **CmdIter;
+static bool shouldSkip(const BaseCommand &Cmd) {
   if (isa<OutputSectionCommand>(Cmd))
     return false;
   const auto *Assign = dyn_cast<SymbolAssignment>(&Cmd);
   if (!Assign)
     return true;
-  if (Assign->Name != ".")
-    return true;
-  // As a horrible special case, skip a . assignment if it is the first thing in
-  // the script. We do this because it is common to set a load address by
-  // starting the script with ". = 0xabcd" and the expectation is that every
-  // section is after that.
-  return CmdIndex == 0;
+  return Assign->Name != ".";
 }
 
 // Orphan sections are sections present in the input files which are not
@@ -658,6 +650,27 @@ void LinkerScript<ELFT>::placeOrphanSect
   // This loops creates or moves commands as needed so that they are in the
   // correct order.
   int CmdIndex = 0;
+
+  // As a horrible special case, skip the first . assignment if it is before any
+  // section. We do this because it is common to set a load address by starting
+  // the script with ". = 0xabcd" and the expectation is that every section is
+  // after that.
+  auto FirstSectionOrDotAssignment =
+      std::find_if(Opt.Commands.begin(), Opt.Commands.end(),
+                   [](const std::unique_ptr<BaseCommand> &Cmd) {
+                     if (isa<OutputSectionCommand>(*Cmd))
+                       return true;
+                     const auto *Assign = dyn_cast<SymbolAssignment>(Cmd.get());
+                     if (!Assign)
+                       return false;
+                     return Assign->Name == ".";
+                   });
+  if (FirstSectionOrDotAssignment != Opt.Commands.end()) {
+    CmdIndex = FirstSectionOrDotAssignment - Opt.Commands.begin();
+    if (isa<SymbolAssignment>(**FirstSectionOrDotAssignment))
+      ++CmdIndex;
+  }
+
   for (OutputSectionBase *Sec : *OutputSections) {
     StringRef Name = Sec->getName();
 
@@ -665,7 +678,7 @@ void LinkerScript<ELFT>::placeOrphanSect
     // correct result.
     auto CmdIter = Opt.Commands.begin() + CmdIndex;
     auto E = Opt.Commands.end();
-    while (CmdIter != E && shouldSkip(CmdIndex)) {
+    while (CmdIter != E && shouldSkip(**CmdIter)) {
       ++CmdIter;
       ++CmdIndex;
     }

Modified: lld/trunk/test/ELF/linkerscript/orphan-first-cmd.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/orphan-first-cmd.s?rev=287996&r1=287995&r2=287996&view=diff
==============================================================================
--- lld/trunk/test/ELF/linkerscript/orphan-first-cmd.s (original)
+++ lld/trunk/test/ELF/linkerscript/orphan-first-cmd.s Sun Nov 27 03:44:45 2016
@@ -1,6 +1,7 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
 # RUN: echo "SECTIONS { \
+# RUN:         foo = 123; \
 # RUN:         . = 0x1000; \
 # RUN:         . = 0x2000; \
 # RUN:         .bar : { . = . + 1; } \




More information about the llvm-commits mailing list