Split removeUnusedSyntheticSections into two functions.

llvm-svn: 288707
This commit is contained in:
Rui Ueyama 2016-12-05 21:37:16 +00:00
parent d1fd383b28
commit 29e7a19e17
1 changed files with 27 additions and 16 deletions

View File

@ -60,6 +60,7 @@ private:
void sortSections();
void finalizeSections();
void addPredefinedSections();
void removeEmptyOutputSections();
std::vector<Phdr> createPhdrs();
void addPtArmExid(std::vector<Phdr> &Phdrs);
@ -910,29 +911,38 @@ finalizeSynthetic(const std::vector<SyntheticSection<ELFT> *> &Sections) {
}
// We need to add input synthetic sections early in createSyntheticSections()
// to make them visible from linkescript side. But not all sections are always
// required to be in output. For example we don't need dynamic section content
// sometimes. This function filters out such unused sections from output.
template <class ELFT>
static void removeUnusedSyntheticSections(std::vector<OutputSectionBase *> &V) {
// to make them visible from linker scripts before we can see whether they
// will be needed or not. As a result, some syntehtic input sections could be
// left empty. We remove such sections in this function.
template <class ELFT> static void removeEmptySyntheticSections() {
// Input synthetic sections are placed after all regular ones. We iterate over
// them all and exit at first non-synthetic.
for (InputSectionBase<ELFT> *S : llvm::reverse(Symtab<ELFT>::X->Sections)) {
SyntheticSection<ELFT> *SS = dyn_cast<SyntheticSection<ELFT>>(S);
if (!SS)
for (InputSectionBase<ELFT> *Sec : llvm::reverse(Symtab<ELFT>::X->Sections)) {
SyntheticSection<ELFT> *In = dyn_cast<SyntheticSection<ELFT>>(S);
if (!In)
return;
if (!SS->empty() || !SS->OutSec)
if (!In->empty() || !In->OutSec)
continue;
OutputSection<ELFT> *OutSec = cast<OutputSection<ELFT>>(SS->OutSec);
OutSec->Sections.erase(
std::find(OutSec->Sections.begin(), OutSec->Sections.end(), SS));
// If there is no other sections in output section, remove it from output.
if (OutSec->Sections.empty())
V.erase(std::find(V.begin(), V.end(), OutSec));
OutputSection<ELFT> *Out = cast<OutputSection<ELFT>>(In->OutSec);
Out->Sections.erase(
std::find(Out->Sections.begin(), Out->Sections.end(), In));
}
}
// This function removes empty output sections so that LLD doesn't create
// empty sections. Such output sections are usually results of linker scripts.
template <class ELFT> void Writer<ELFT>::removeEmptyOutputSections() {
OutputSections.erase(
std::remove_if(OutputSections.begin(), OutputSections.end(),
[](OutputSectionBase *Sec) {
if (auto *S = dyn_cast<OutputSection<ELFT>>(Sec))
return S->Sections.empty();
return false;
}),
OutputSections.end());
}
// Create output section objects and add them to OutputSections.
template <class ELFT> void Writer<ELFT>::finalizeSections() {
Out<ELFT>::DebugInfo = findSection(".debug_info");
@ -993,7 +1003,8 @@ template <class ELFT> void Writer<ELFT>::finalizeSections() {
// So far we have added sections from input object files.
// This function adds linker-created Out<ELFT>::* sections.
addPredefinedSections();
removeUnusedSyntheticSections<ELFT>(OutputSections);
removeEmptySyntheticSections<ELFT>();
removeEmptyOutputSections();
sortSections();