[lld] r281772 - Fix constraint checking in ONLY_IF_RO.

Rafael Espindola via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 16 13:34:02 PDT 2016


Author: rafael
Date: Fri Sep 16 15:34:02 2016
New Revision: 281772

URL: http://llvm.org/viewvc/llvm-project?rev=281772&view=rev
Log:
Fix constraint checking in ONLY_IF_RO.

We have to look at all the relevant input sections at once.

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

Modified: lld/trunk/ELF/LinkerScript.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.cpp?rev=281772&r1=281771&r2=281772&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.cpp (original)
+++ lld/trunk/ELF/LinkerScript.cpp Fri Sep 16 15:34:02 2016
@@ -152,7 +152,7 @@ static bool checkConstraint(uint64_t Fla
 }
 
 template <class ELFT>
-static bool matchConstraints(ArrayRef<InputSectionData *> Sections,
+static bool matchConstraints(ArrayRef<InputSectionBase<ELFT> *> Sections,
                              ConstraintKind Kind) {
   if (Kind == ConstraintKind::NoConstraint)
     return true;
@@ -164,8 +164,7 @@ static bool matchConstraints(ArrayRef<In
 
 // Compute and remember which sections the InputSectionDescription matches.
 template <class ELFT>
-void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I,
-                                              ConstraintKind Constraint) {
+void LinkerScript<ELFT>::computeInputSections(InputSectionDescription *I) {
   for (const std::pair<llvm::Regex, llvm::Regex> &V : I->SectionsVec) {
     for (ObjectFile<ELFT> *F : Symtab<ELFT>::X->getObjectFiles()) {
       if (fileMatches(I->FileRe, V.first, sys::path::filename(F->getName()))) {
@@ -180,11 +179,6 @@ void LinkerScript<ELFT>::computeInputSec
     }
   }
 
-  if (!matchConstraints<ELFT>(I->Sections, Constraint)) {
-    I->Sections.clear();
-    return;
-  }
-
   if (I->SortInner != SortSectionPolicy::None)
     std::stable_sort(I->Sections.begin(), I->Sections.end(),
                      getComparator(I->SortInner));
@@ -221,10 +215,17 @@ LinkerScript<ELFT>::createInputSectionLi
     }
 
     auto *Cmd = cast<InputSectionDescription>(Base.get());
-    computeInputSections(Cmd, OutCmd.Constraint);
+    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;
 }
 

Modified: lld/trunk/ELF/LinkerScript.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/LinkerScript.h?rev=281772&r1=281771&r2=281772&view=diff
==============================================================================
--- lld/trunk/ELF/LinkerScript.h (original)
+++ lld/trunk/ELF/LinkerScript.h Fri Sep 16 15:34:02 2016
@@ -186,8 +186,7 @@ public:
   std::vector<OutputSectionBase<ELFT> *> *OutputSections;
 
 private:
-  void computeInputSections(InputSectionDescription *,
-                            ConstraintKind Constraint);
+  void computeInputSections(InputSectionDescription *);
 
   void addSection(OutputSectionFactory<ELFT> &Factory,
                   InputSectionBase<ELFT> *Sec, StringRef Name);

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




More information about the llvm-commits mailing list