Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -1048,40 +1048,45 @@ return SectionOrder; } -static bool isKnownNonreorderableSection(const OutputSection *OS) { - return llvm::StringSwitch(OS->Name) - .Cases(".init", ".fini", ".init_array", ".fini_array", ".ctors", - ".dtors", true) - .Default(false); -} - -// If no layout was provided by linker script, we want to apply default -// sorting for special input sections. This also handles --symbol-ordering-file. -template void Writer::sortInputSections() { - // 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 && !isKnownNonreorderableSection(Sec)) - Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); }); - - if (Script->HasSectionsCommand) +static void sortSection(OutputSection *Sec, + const DenseMap &Order) { + if (!Sec->Live) return; + StringRef Name = Sec->Name; // 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(); + if (Name == ".init_array" || Name == ".fini_array") { + if (!Script->HasSectionsCommand) + Sec->sortInitFini(); + return; + } // 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(); + if (Name == ".ctors" || Name == ".dtors") { + if (!Script->HasSectionsCommand) + Sec->sortCtorsDtors(); + return; + } + + // Never sort these. + if (Name == ".init" || Name == ".fini") + return; + + // Sort input sections by priority using the list provided + // by --symbol-ordering-file. + if (!Order.empty()) + Sec->sort([&](InputSectionBase *S) { return Order.lookup(S); }); +} + +// If no layout was provided by linker script, we want to apply default +// sorting for special input sections. This also handles --symbol-ordering-file. +template void Writer::sortInputSections() { + // Build the order once since it is expensive. + DenseMap Order = buildSectionOrder(); + for (BaseCommand *Base : Script->SectionCommands) + if (auto *Sec = dyn_cast(Base)) + sortSection(Sec, Order); } template void Writer::sortSections() {