lld: [ELF] Remove unused synthetic sections from script commands

Script commands are processed before unused synthetic sections are
removed. Therefore, if a linker script matches one of these sections
it'll get emitted as an empty output section because the logic for
removing unused synthetic sections ignores script commands which
could have already matched and captured one of these sections. This
patch fixes that by also removing the unused synthetic sections from
the script commands.

Discussed with:	dim
Obtained from:	LLVM r307037
Sponsored by:	The FreeBSD Foundation
This commit is contained in:
Ed Maste 2017-07-04 17:32:51 +00:00
parent bb07ac4d64
commit 0a7e7475c3
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/projects/clang500-import/; revision=320649

View File

@ -1149,8 +1149,17 @@ static void removeUnusedSyntheticSections(std::vector<OutputSection *> &V) {
SS->Live = false;
// If there are no other sections in the output section, remove it from the
// output.
if (OS->Sections.empty())
if (OS->Sections.empty()) {
V.erase(std::find(V.begin(), V.end(), OS));
// Also remove script commands matching the output section.
auto &Cmds = Script->Opt.Commands;
auto I = std::remove_if(Cmds.begin(), Cmds.end(), [&](BaseCommand *Cmd) {
if (auto *OSCmd = dyn_cast<OutputSectionCommand>(Cmd))
return OSCmd->Sec == OS;
return false;
});
Cmds.erase(I, Cmds.end());
}
}
}