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::Default) - std::stable_sort(I->Sections.begin(), I->Sections.end(), - getComparator(I->SortInner)); - if (I->SortOuter != SortSectionPolicy::Default) - 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. @@ -1006,27 +1015,6 @@ return SortSectionPolicy::Default; } -static void selectSortKind(InputSectionDescription *Cmd) { - if (Cmd->SortOuter == SortSectionPolicy::None) { - Cmd->SortOuter = SortSectionPolicy::Default; - return; - } - - if (Cmd->SortOuter != SortSectionPolicy::Default) { - // If the section sorting command in linker script is nested, the command - // line option will be ignored. - if (Cmd->SortInner != SortSectionPolicy::Default) - 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; -} - // Method reads a list of sequence of excluded files and section globs given in // a following form: ((EXCLUDE_FILE(file_pattern+))? section_pattern+)+ // Example: *(.foo.1 EXCLUDE_FILE (*a.o) .foo.2 EXCLUDE_FILE (*b.o) .foo.3) @@ -1077,11 +1065,9 @@ Cmd->SectionsVec.push_back({llvm::Regex(), readFilePatterns()}); } expect(")"); - selectSortKind(Cmd); return Cmd; } - selectSortKind(Cmd); readSectionExcludes(Cmd); return Cmd; }