Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -43,7 +43,7 @@ enum class UnresolvedPolicy { NoUndef, ReportError, Warn, Ignore }; // For --sort-section and linkerscript sorting rules. -enum class SortSectionPolicy { None, IgnoreConfig, Alignment, Name, Priority }; +enum class SortSectionPolicy { Default, None, Alignment, Name, Priority }; struct SymbolVersion { llvm::StringRef Name; Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -415,7 +415,7 @@ return SortSectionPolicy::Name; if (!S.empty()) error("unknown --sort-section rule: " + S); - return SortSectionPolicy::None; + return SortSectionPolicy::Default; } // Initializes Config members by the command line options. Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -104,8 +104,8 @@ FileRe(compileGlobPatterns({FilePattern})) {} static bool classof(const BaseCommand *C); llvm::Regex FileRe; - SortSectionPolicy SortOuter = SortSectionPolicy::None; - SortSectionPolicy SortInner = SortSectionPolicy::None; + SortSectionPolicy SortOuter = SortSectionPolicy::Default; + SortSectionPolicy SortInner = SortSectionPolicy::Default; // Pairs of section regex and files excluded. std::list> SectionsVec; std::vector Sections; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -162,6 +162,12 @@ }); } +void sortSections(std::vector &Sections, + SortSectionPolicy K) { + if (K != SortSectionPolicy::Default) + std::stable_sort(Sections.begin(), Sections.end(), getComparator(K)); +} + // Compute and remember which sections the InputSectionDescription matches. template void LinkerScript::computeInputSections(InputSectionDescription *I) { @@ -179,12 +185,15 @@ } } - if (I->SortInner != SortSectionPolicy::None) - std::stable_sort(I->Sections.begin(), I->Sections.end(), - getComparator(I->SortInner)); - if (I->SortOuter != SortSectionPolicy::None) - std::stable_sort(I->Sections.begin(), I->Sections.end(), - getComparator(I->SortOuter)); + // Sort sections by --sort-section and SORT_*() commands. + // We do not sort it at all if it was surrounded by SORT_NONE(). + if (I->SortOuter != SortSectionPolicy::None) { + if (I->SortInner == SortSectionPolicy::Default) + sortSections(I->Sections, Config->SortSection); + else + sortSections(I->Sections, I->SortInner); + sortSections(I->Sections, I->SortOuter); + } // We do not add duplicate input sections, so mark them with a dummy output // section for now. @@ -998,32 +1007,9 @@ return SortSectionPolicy::Alignment; if (skip("SORT_BY_INIT_PRIORITY")) return SortSectionPolicy::Priority; - // `SORT_NONE' disables section sorting by ignoring the command line - // section sorting option. if (skip("SORT_NONE")) - return SortSectionPolicy::IgnoreConfig; - return SortSectionPolicy::None; -} - -static void selectSortKind(InputSectionDescription *Cmd) { - if (Cmd->SortOuter == SortSectionPolicy::IgnoreConfig) { - Cmd->SortOuter = SortSectionPolicy::None; - return; - } - - if (Cmd->SortOuter != SortSectionPolicy::None) { - // If the section sorting command in linker script is nested, the command - // line option will be ignored. - if (Cmd->SortInner != SortSectionPolicy::None) - return; - // If the section sorting command in linker script isn't nested, the - // command line option will make the section sorting command to be treated - // as nested sorting command. - Cmd->SortInner = Config->SortSection; - return; - } - // If sorting rule not specified, use command line option. - Cmd->SortOuter = Config->SortSection; + return SortSectionPolicy::None; + return SortSectionPolicy::Default; } // Method reads a list of sequence of excluded files and section globs given in @@ -1063,11 +1049,11 @@ // Read SORT(). SortSectionPolicy K1 = readSortKind(); - if (K1 != SortSectionPolicy::None) { + if (K1 != SortSectionPolicy::Default) { Cmd->SortOuter = K1; expect("("); SortSectionPolicy K2 = readSortKind(); - if (K2 != SortSectionPolicy::None) { + if (K2 != SortSectionPolicy::Default) { Cmd->SortInner = K2; expect("("); Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()}); @@ -1076,11 +1062,9 @@ Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()}); } expect(")"); - selectSortKind(Cmd); return Cmd; } - selectSortKind(Cmd); readSectionExcludes(Cmd); return Cmd; }