Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ 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: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -52,6 +52,7 @@ void createSections(); void forEachRelSec(std::function Fn); void sortSections(); + void sortInputSections(); void finalizeSections(); void addPredefinedSections(); void setReservedSymbolSections(); @@ -826,31 +827,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 @@ -877,11 +853,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 @@ -1042,6 +1013,35 @@ 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 *Cmd = findSection(".init_array")) + Cmd->sortInitFini(); + if (OutputSection *Cmd = findSection(".fini_array")) + Cmd->sortInitFini(); + + // Sort input sections by the special rule for .ctors and .dtors. + if (OutputSection *Cmd = findSection(".ctors")) + Cmd->sortCtorsDtors(); + if (OutputSection *Cmd = findSection(".dtors")) + Cmd->sortCtorsDtors(); +} + template void Writer::sortSections() { Script->adjustSectionsBeforeSorting(); @@ -1055,8 +1055,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); };