[ELF] - Stop sorting input sections in createSections().

It does not seem that createSections() is a good place for
applying sorting. Patch changes code to do that inside
sortSections(), which looks more appropriate place.

Differential revision: https://reviews.llvm.org/D39371

llvm-svn: 316893
This commit is contained in:
George Rimar
2017-10-30 10:12:49 +00:00
parent 9a8c8bf60d
commit a6b1fbece1
2 changed files with 33 additions and 32 deletions

View File

@@ -296,6 +296,7 @@ bool OutputSection::classof(const BaseCommand *C) {
}
void OutputSection::sort(std::function<int(InputSectionBase *S)> Order) {
assert(Live);
assert(SectionCommands.size() == 1);
sortByOrder(cast<InputSectionDescription>(SectionCommands[0])->Sections,
Order);

View File

@@ -52,6 +52,7 @@ private:
void createSections();
void forEachRelSec(std::function<void(InputSectionBase &)> Fn);
void sortSections();
void sortInputSections();
void finalizeSections();
void addPredefinedSections();
void setReservedSymbolSections();
@@ -837,31 +838,6 @@ template <class ELFT> void Writer<ELFT>::addReservedSymbols() {
ElfSym::Edata2 = Add("_edata", -1);
}
// Sort input sections by section name suffixes for
// __attribute__((init_priority(N))).
static void sortInitFini(OutputSection *Cmd) {
if (Cmd)
Cmd->sortInitFini();
}
// Sort input sections by the special rule for .ctors and .dtors.
static void sortCtorsDtors(OutputSection *Cmd) {
if (Cmd)
Cmd->sortCtorsDtors();
}
// Sort input sections using the list provided by --symbol-ordering-file.
static void sortBySymbolsOrder() {
if (Config->SymbolOrderingFile.empty())
return;
// Sort sections by priority.
DenseMap<SectionBase *, int> SectionOrder = buildSectionOrder();
for (BaseCommand *Base : Script->SectionCommands)
if (auto *Sec = dyn_cast<OutputSection>(Base))
Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); });
}
template <class ELFT>
void Writer<ELFT>::forEachRelSec(std::function<void(InputSectionBase &)> Fn) {
// Scan all relocations. Each relocation goes through a series
@@ -888,11 +864,6 @@ template <class ELFT> void Writer<ELFT>::createSections() {
Vec.end());
Script->fabricateDefaultCommands();
sortBySymbolsOrder();
sortInitFini(findSection(".init_array"));
sortInitFini(findSection(".fini_array"));
sortCtorsDtors(findSection(".ctors"));
sortCtorsDtors(findSection(".dtors"));
}
// This function generates assignments for predefined symbols (e.g. _end or
@@ -1050,6 +1021,34 @@ findOrphanPos(std::vector<BaseCommand *>::iterator B,
return I;
}
// If no layout was provided by linker script, we want to apply default
// sorting for special input sections and handle --symbol-ordering-file.
template <class ELFT> void Writer<ELFT>::sortInputSections() {
assert(!Script->HasSectionsCommand);
// Sort input sections by priority using the list provided
// by --symbol-ordering-file.
DenseMap<SectionBase *, int> Order = buildSectionOrder();
if (!Order.empty())
for (BaseCommand *Base : Script->SectionCommands)
if (auto *Sec = dyn_cast<OutputSection>(Base))
if (Sec->Live)
Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); });
// Sort input sections by section name suffixes for
// __attribute__((init_priority(N))).
if (OutputSection *Sec = findSection(".init_array"))
Sec->sortInitFini();
if (OutputSection *Sec = findSection(".fini_array"))
Sec->sortInitFini();
// Sort input sections by the special rule for .ctors and .dtors.
if (OutputSection *Sec = findSection(".ctors"))
Sec->sortCtorsDtors();
if (OutputSection *Sec = findSection(".dtors"))
Sec->sortCtorsDtors();
}
template <class ELFT> void Writer<ELFT>::sortSections() {
Script->adjustSectionsBeforeSorting();
@@ -1063,8 +1062,9 @@ template <class ELFT> void Writer<ELFT>::sortSections() {
Sec->SortRank = getSectionRank(Sec);
if (!Script->HasSectionsCommand) {
// We know that all the OutputSections are contiguous in
// this case.
sortInputSections();
// We know that all the OutputSections are contiguous in this case.
auto E = Script->SectionCommands.end();
auto I = Script->SectionCommands.begin();
auto IsSection = [](BaseCommand *Base) { return isa<OutputSection>(Base); };