Index: ELF/LinkerScript.h =================================================================== --- ELF/LinkerScript.h +++ ELF/LinkerScript.h @@ -87,13 +87,14 @@ ConstraintKind Constraint = ConstraintKind::NoConstraint; }; -enum class SortKind { None, Name, Align, NameAlign, AlignName }; +enum SortKind { SortNone, SortByName, SortByAlignment }; struct InputSectionDescription : BaseCommand { InputSectionDescription() : BaseCommand(InputSectionKind) {} static bool classof(const BaseCommand *C); StringRef FilePattern; - SortKind Sort = SortKind::None; + SortKind SortOuter = SortNone; + SortKind SortInner = SortNone; std::vector ExcludedFiles; std::vector SectionPatterns; }; Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -131,27 +131,24 @@ Sec->addSection(C); } -template struct SectionsSorter { - SectionsSorter(SortKind Kind) : Kind(Kind) {} - bool operator()(InputSectionBase *A, InputSectionBase *B) { - int AlignmentCmp = A->Alignment - B->Alignment; - if (Kind == SortKind::Align || (Kind == SortKind::AlignName && AlignmentCmp != 0)) - return AlignmentCmp < 0; - - int NameCmp = A->getSectionName().compare(B->getSectionName()); - if (Kind == SortKind::Name || (Kind == SortKind::NameAlign && NameCmp != 0)) - return NameCmp < 0; - - if (Kind == SortKind::NameAlign) - return AlignmentCmp < 0; - if (Kind == SortKind::AlignName) - return NameCmp < 0; - - llvm_unreachable("unknown section sort kind in predicate"); - return false; - } - SortKind Kind; -}; +template +static bool compareName(InputSectionBase *A, InputSectionBase *B) { + return A->getSectionName() < B->getSectionName(); +} + +template +static bool compareAlignment(InputSectionBase *A, + InputSectionBase *B) { + return A->Alignment < B->Alignment; +} + +template +static std::function *, InputSectionBase *)> +getComparator(SortKind K) { + if (K == SortByName) + return compareName; + return compareAlignment; +} template void LinkerScript::createSections( @@ -172,9 +169,12 @@ continue; } - if (Cmd->Sort != SortKind::None) + if (Cmd->SortInner) + std::stable_sort(Sections.begin(), Sections.end(), + getComparator(Cmd->SortInner)); + if (Cmd->SortOuter) std::stable_sort(Sections.begin(), Sections.end(), - SectionsSorter(Cmd->Sort)); + getComparator(Cmd->SortOuter)); for (InputSectionBase *S : Sections) addSection(Factory, *Out, S, OutputName); @@ -484,6 +484,7 @@ std::vector readInputFilePatterns(); InputSectionDescription *readInputSectionRules(); unsigned readPhdrType(); + SortKind readSortKind(); SymbolAssignment *readProvide(bool Hidden); Expr readAlign(); void readSort(); @@ -723,41 +724,36 @@ return V; } +SortKind ScriptParser::readSortKind() { + if (skip("SORT") || skip("SORT_BY_NAME")) + return SortByName; + if (skip("SORT_BY_ALIGNMENT")) + return SortByAlignment; + return SortNone; +} + InputSectionDescription *ScriptParser::readInputSectionRules() { auto *Cmd = new InputSectionDescription; Cmd->FilePattern = next(); expect("("); + // Read EXCLUDE_FILE(). if (skip("EXCLUDE_FILE")) { expect("("); while (!Error && !skip(")")) Cmd->ExcludedFiles.push_back(next()); } - if (skip("SORT") || skip("SORT_BY_NAME")) { - expect("("); - if (skip("SORT_BY_ALIGNMENT")) { - Cmd->Sort = SortKind::NameAlign; - expect("("); - Cmd->SectionPatterns = readInputFilePatterns(); - expect(")"); - } else { - Cmd->Sort = SortKind::Name; - Cmd->SectionPatterns = readInputFilePatterns(); - } - expect(")"); - return Cmd; - } - - if (skip("SORT_BY_ALIGNMENT")) { + // Read SORT(). + if (SortKind K1 = readSortKind()) { + Cmd->SortOuter = K1; expect("("); - if (skip("SORT") || skip("SORT_BY_NAME")) { - Cmd->Sort = SortKind::AlignName; + if (SortKind K2 = readSortKind()) { + Cmd->SortInner = K2; expect("("); Cmd->SectionPatterns = readInputFilePatterns(); expect(")"); } else { - Cmd->Sort = SortKind::Align; Cmd->SectionPatterns = readInputFilePatterns(); } expect(")");