Index: lld/trunk/ELF/OutputSections.cpp =================================================================== --- lld/trunk/ELF/OutputSections.cpp +++ lld/trunk/ELF/OutputSections.cpp @@ -296,6 +296,7 @@ } void OutputSection::sort(std::function Order) { + assert(Live); assert(SectionCommands.size() == 1); sortByOrder(cast(SectionCommands[0])->Sections, Order); Index: lld/trunk/ELF/Writer.cpp =================================================================== --- lld/trunk/ELF/Writer.cpp +++ lld/trunk/ELF/Writer.cpp @@ -52,6 +52,7 @@ void createSections(); void forEachRelSec(std::function Fn); void sortSections(); + void sortInputSections(); void finalizeSections(); void addPredefinedSections(); void setReservedSymbolSections(); @@ -837,31 +838,6 @@ 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 SectionOrder = buildSectionOrder(); - for (BaseCommand *Base : Script->SectionCommands) - if (auto *Sec = dyn_cast(Base)) - Sec->sort([&](InputSectionBase *S) { return SectionOrder.lookup(S); }); -} - template void Writer::forEachRelSec(std::function Fn) { // Scan all relocations. Each relocation goes through a series @@ -888,11 +864,6 @@ 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 @@ 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 void Writer::sortInputSections() { + assert(!Script->HasSectionsCommand); + + // Sort input sections by priority using the list provided + // by --symbol-ordering-file. + DenseMap Order = buildSectionOrder(); + if (!Order.empty()) + for (BaseCommand *Base : Script->SectionCommands) + if (auto *Sec = dyn_cast(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 void Writer::sortSections() { Script->adjustSectionsBeforeSorting(); @@ -1063,8 +1062,9 @@ 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(Base); };