diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index 8881154ec8d8..ea38f0efef55 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -152,7 +152,7 @@ static bool checkConstraint(uint64_t Flags, ConstraintKind Kind) { } template -static bool matchConstraints(ArrayRef Sections, +static bool matchConstraints(ArrayRef *> Sections, ConstraintKind Kind) { if (Kind == ConstraintKind::NoConstraint) return true; @@ -164,8 +164,7 @@ static bool matchConstraints(ArrayRef Sections, // Compute and remember which sections the InputSectionDescription matches. template -void LinkerScript::computeInputSections(InputSectionDescription *I, - ConstraintKind Constraint) { +void LinkerScript::computeInputSections(InputSectionDescription *I) { for (const std::pair &V : I->SectionsVec) { for (ObjectFile *F : Symtab::X->getObjectFiles()) { if (fileMatches(I->FileRe, V.first, sys::path::filename(F->getName()))) { @@ -180,11 +179,6 @@ void LinkerScript::computeInputSections(InputSectionDescription *I, } } - if (!matchConstraints(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::createInputSectionList(OutputSectionCommand &OutCmd) { } auto *Cmd = cast(Base.get()); - computeInputSections(Cmd, OutCmd.Constraint); + computeInputSections(Cmd); for (InputSectionData *S : Cmd->Sections) Ret.push_back(static_cast *>(S)); } + + if (!matchConstraints(Ret, OutCmd.Constraint)) { + for (InputSectionBase *S : Ret) + S->OutSec = nullptr; + Ret.clear(); + } + return Ret; } diff --git a/lld/ELF/LinkerScript.h b/lld/ELF/LinkerScript.h index 07abe76211cf..cc8ffdad1843 100644 --- a/lld/ELF/LinkerScript.h +++ b/lld/ELF/LinkerScript.h @@ -186,8 +186,7 @@ public: std::vector *> *OutputSections; private: - void computeInputSections(InputSectionDescription *, - ConstraintKind Constraint); + void computeInputSections(InputSectionDescription *); void addSection(OutputSectionFactory &Factory, InputSectionBase *Sec, StringRef Name); diff --git a/lld/test/ELF/linkerscript/sections-constraint2.s b/lld/test/ELF/linkerscript/sections-constraint2.s new file mode 100644 index 000000000000..e726365582d8 --- /dev/null +++ b/lld/test/ELF/linkerscript/sections-constraint2.s @@ -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