diff --git a/lld/ELF/LinkerScript.cpp b/lld/ELF/LinkerScript.cpp --- a/lld/ELF/LinkerScript.cpp +++ b/lld/ELF/LinkerScript.cpp @@ -408,15 +408,16 @@ // 3. If one SORT command is given, and if it is SORT_NONE, don't sort. // 4. If no SORT command is given, sort according to --sort-section. static void sortInputSections(MutableArrayRef vec, - const SectionPattern &pat) { - if (pat.sortOuter == SortSectionPolicy::None) + SortSectionPolicy outer, + SortSectionPolicy inner) { + if (outer == SortSectionPolicy::None) return; - if (pat.sortInner == SortSectionPolicy::Default) + if (inner == SortSectionPolicy::Default) sortSections(vec, config->sortSection); else - sortSections(vec, pat.sortInner); - sortSections(vec, pat.sortOuter); + sortSections(vec, inner); + sortSections(vec, outer); } // Compute and remember which sections the InputSectionDescription matches. @@ -424,14 +425,29 @@ LinkerScript::computeInputSections(const InputSectionDescription *cmd, ArrayRef sections) { std::vector ret; + DenseMap pos; + pos.reserve(sections.size()); + for (auto it : enumerate(sections)) + pos[it.value()] = it.index() * 2; + auto sortByPosition = [&](size_t sizeLastSort) { + llvm::sort(MutableArrayRef(ret).slice(sizeLastSort), + [&](InputSectionBase *l, InputSectionBase *r) { + return pos.find(l)->second < pos.find(r)->second; + }); + }; // Collects all sections that satisfy constraints of Cmd. + size_t sizeLastSort = 0; for (const SectionPattern &pat : cmd->sectionPatterns) { size_t sizeBefore = ret.size(); for (InputSectionBase *sec : sections) { if (!sec->isLive() || sec->parent) continue; + // If the section has been matched by a previous pattern, skip. + auto it = pos.find(sec); + if (it->second % 2) + continue; // For -emit-relocs we have to ignore entries like // .rela.dyn : { *(.rela.data) } @@ -452,11 +468,21 @@ continue; ret.push_back(sec); + it->second |= 1; } + if (pat.sortOuter == SortSectionPolicy::Default) + continue; + sortByPosition(sizeLastSort); sortInputSections( - MutableArrayRef(ret).slice(sizeBefore), pat); + MutableArrayRef(ret).slice(sizeBefore), + pat.sortOuter, pat.sortInner); + sizeLastSort = ret.size(); } + sortByPosition(sizeLastSort); + sortInputSections( + MutableArrayRef(ret).slice(sizeLastSort), + config->sortSection, SortSectionPolicy::None); return ret; } diff --git a/lld/test/ELF/linkerscript/exclude-multiple.s b/lld/test/ELF/linkerscript/exclude-multiple.s --- a/lld/test/ELF/linkerscript/exclude-multiple.s +++ b/lld/test/ELF/linkerscript/exclude-multiple.s @@ -8,10 +8,12 @@ # RUN: ld.lld -script %t1.script %tfile1.o %tfile2.o %tfile3.o -o %t1.o # RUN: llvm-objdump -s %t1.o | FileCheck %s +## Sections from %tfile1 precede sections from %tfile2 and %tfile3. +## In each file, the sections are added in the original order. # CHECK: Contents of section .foo: -# CHECK-NEXT: 01000000 00000000 04000000 00000000 -# CHECK-NEXT: 07000000 00000000 05000000 00000000 -# CHECK-NEXT: 08000000 00000000 03000000 00000000 +# CHECK-NEXT: 03000000 00000000 01000000 00000000 +# CHECK-NEXT: 04000000 00000000 05000000 00000000 +# CHECK-NEXT: 07000000 00000000 08000000 00000000 # CHECK-NEXT: 09000000 00000000 # CHECK-NEXT: Contents of section .foo.2: # CHECK-NEXT: 02000000 00000000 @@ -27,11 +29,12 @@ # RUN: not ld.lld -script %t3.script %tfile1.o %tfile2.o %tfile3.o -o /dev/null 2>&1 | \ # RUN: FileCheck %s --check-prefix=ERR -.section .foo.1,"a" - .quad 1 - .section .foo.2,"a" .quad 2 +## %tfile1.o(.foo.3) precedes %tfile.o(.foo.1) in the output section. .section .foo.3,"a" .quad 3 + +.section .foo.1,"a" + .quad 1 diff --git a/lld/test/ELF/linkerscript/sort2.s b/lld/test/ELF/linkerscript/sort2.s --- a/lld/test/ELF/linkerscript/sort2.s +++ b/lld/test/ELF/linkerscript/sort2.s @@ -5,9 +5,10 @@ # RUN: ld.lld -o %t1 --script %t1.script %tfile1.o # RUN: llvm-readelf -x .abc %t1 | FileCheck %s -## FIXME Some input sections are duplicated in .abc and their second occurrences are zeros. +## SORT and patterns between two SORT are sorted separately. +## Note, GNU ld has a strange behavior https://sourceware.org/pipermail/binutils/2020-November/114083.html # CHECK: Hex dump of section '.abc' -# CHECK-NEXT: 0x00000000 01020306 05040000 00070908 0b0c0a +# CHECK-NEXT: 0x00000000 01020306 05040708 090b0c0a # RUN: echo "SECTIONS { \ # RUN: .abc : { *(SORT(.foo.* EXCLUDE_FILE (*file1.o) .bar.*) .a* SORT(.bar.*) .b*) } \