[lld] r281775 - Only process commands in a ONLY_IF_RO if it matches.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 16 14:05:36 PDT 2016


Author: rafael
Date: Fri Sep 16 16:05:36 2016
New Revision: 281775

URL: http://llvm.org/viewvc/llvm-project?rev=281775&view=rev
Log:
Only process commands in a ONLY_IF_RO if it matches.

This matches bfd behavior. It also makes future changes simpler as we
don't have to worry about ignoring these commands in multiple places

Added:
    lld/trunk/test/ELF/linkerscript/sections-constraint3.s
Modified:
    lld/trunk/ELF/LinkerScript.cpp

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281775&r1=281774&r2=281775&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Sep 16 16:05:36 2016
@@ -208,24 +208,14 @@ LinkerScript<ELFT>::createInputSectionLi
   std::vector<InputSectionBase<ELFT> *> Ret;
 
   for (const std::unique_ptr<BaseCommand> &Base : OutCmd.Commands) {
-    if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get())) {
-      if (shouldDefine<ELFT>(OutCmd))
-        addSymbol<ELFT>(OutCmd);
+    auto *Cmd = dyn_cast<InputSectionDescription>(Base.get());
+    if (!Cmd)
       continue;
-    }
-
-    auto *Cmd = cast<InputSectionDescription>(Base.get());
     computeInputSections(Cmd);
     for (InputSectionData *S : Cmd->Sections)
       Ret.push_back(static_cast<InputSectionBase<ELFT> *>(S));
   }
 
-  if (!matchConstraints<ELFT>(Ret, OutCmd.Constraint)) {
-    for (InputSectionBase<ELFT> *S : Ret)
-      S->OutSec = nullptr;
-    Ret.clear();
-  }
-
   return Ret;
 }
 
@@ -275,7 +265,9 @@ void LinkerScript<ELFT>::addSection(Outp
 template <class ELFT>
 void LinkerScript<ELFT>::processCommands(OutputSectionFactory<ELFT> &Factory) {
 
-  for (const std::unique_ptr<BaseCommand> &Base1 : Opt.Commands) {
+  for (unsigned I = 0; I < Opt.Commands.size(); ++I) {
+    auto Iter = Opt.Commands.begin() + I;
+    const std::unique_ptr<BaseCommand> &Base1 = *Iter;
     if (auto *Cmd = dyn_cast<SymbolAssignment>(Base1.get())) {
       if (shouldDefine<ELFT>(Cmd))
         addRegular<ELFT>(Cmd);
@@ -298,6 +290,18 @@ void LinkerScript<ELFT>::processCommands
         continue;
       }
 
+      if (!matchConstraints<ELFT>(V, Cmd->Constraint)) {
+        for (InputSectionBase<ELFT> *S : V)
+          S->OutSec = nullptr;
+        Opt.Commands.erase(Iter);
+        continue;
+      }
+
+      for (const std::unique_ptr<BaseCommand> &Base : Cmd->Commands)
+        if (auto *OutCmd = dyn_cast<SymbolAssignment>(Base.get()))
+          if (shouldDefine<ELFT>(OutCmd))
+            addSymbol<ELFT>(OutCmd);
+
       if (V.empty())
         continue;
 
@@ -410,8 +414,7 @@ findSections(OutputSectionCommand &Cmd,
              const std::vector<OutputSectionBase<ELFT> *> &Sections) {
   std::vector<OutputSectionBase<ELFT> *> Ret;
   for (OutputSectionBase<ELFT> *Sec : Sections)
-    if (Sec->getName() == Cmd.Name &&
-        checkConstraint(Sec->getFlags(), Cmd.Constraint))
+    if (Sec->getName() == Cmd.Name)
       Ret.push_back(Sec);
   return Ret;
 }

Added: lld/trunk/test/ELF/linkerscript/sections-constraint3.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/linkerscript/sections-constraint3.s?rev=281775&view=auto
==============================================================================
--- lld/trunk/test/ELF/linkerscript/sections-constraint3.s (added)
+++ lld/trunk/test/ELF/linkerscript/sections-constraint3.s Fri Sep 16 16:05:36 2016
@@ -0,0 +1,11 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: echo "SECTIONS { zed : ONLY_IF_RO { abc = 1; *(foo) } }" > %t.script
+# RUN: ld.lld -T %t.script %t.o -o %t.so -shared
+# RUN: llvm-readobj -t %t.so | FileCheck %s
+
+# CHECK: Symbols [
+# CHECK-NOT: abc
+
+.section foo,"aw"
+.quad 1




More information about the llvm-commits mailing list