From 7c3ff2eb584ff62a8d02ecde9dfd5bcda48d6498 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 16 Sep 2016 21:05:36 +0000 Subject: [PATCH] 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 llvm-svn: 281775 --- lld/ELF/LinkerScript.cpp | 33 +++++++++++++----------- lld/test/ELF/linkerscript/sections-constraint3.s | 11 ++++++++ 2 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 lld/test/ELF/linkerscript/sections-constraint3.s diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp index ea38f0e..e6c6093 100644 --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -208,24 +208,14 @@ LinkerScript::createInputSectionList(OutputSectionCommand &OutCmd) { std::vector *> Ret; for (const std::unique_ptr &Base : OutCmd.Commands) { - if (auto *OutCmd = dyn_cast(Base.get())) { - if (shouldDefine(OutCmd)) - addSymbol(OutCmd); + auto *Cmd = dyn_cast(Base.get()); + if (!Cmd) continue; - } - - auto *Cmd = cast(Base.get()); 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; } @@ -275,7 +265,9 @@ void LinkerScript::addSection(OutputSectionFactory &Factory, template void LinkerScript::processCommands(OutputSectionFactory &Factory) { - for (const std::unique_ptr &Base1 : Opt.Commands) { + for (unsigned I = 0; I < Opt.Commands.size(); ++I) { + auto Iter = Opt.Commands.begin() + I; + const std::unique_ptr &Base1 = *Iter; if (auto *Cmd = dyn_cast(Base1.get())) { if (shouldDefine(Cmd)) addRegular(Cmd); @@ -298,6 +290,18 @@ void LinkerScript::processCommands(OutputSectionFactory &Factory) { continue; } + if (!matchConstraints(V, Cmd->Constraint)) { + for (InputSectionBase *S : V) + S->OutSec = nullptr; + Opt.Commands.erase(Iter); + continue; + } + + for (const std::unique_ptr &Base : Cmd->Commands) + if (auto *OutCmd = dyn_cast(Base.get())) + if (shouldDefine(OutCmd)) + addSymbol(OutCmd); + if (V.empty()) continue; @@ -410,8 +414,7 @@ findSections(OutputSectionCommand &Cmd, const std::vector *> &Sections) { std::vector *> Ret; for (OutputSectionBase *Sec : Sections) - if (Sec->getName() == Cmd.Name && - checkConstraint(Sec->getFlags(), Cmd.Constraint)) + if (Sec->getName() == Cmd.Name) Ret.push_back(Sec); return Ret; } diff --git a/lld/test/ELF/linkerscript/sections-constraint3.s b/lld/test/ELF/linkerscript/sections-constraint3.s new file mode 100644 index 0000000..259f11e --- /dev/null +++ b/lld/test/ELF/linkerscript/sections-constraint3.s @@ -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 -- 2.7.4