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,13 +425,24 @@ 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) + if (!sec->isLive() || sec->parent || pos.find(sec)->second % 2) continue; // For -emit-relocs we have to ignore entries like @@ -452,11 +464,22 @@ continue; ret.push_back(sec); + // Prevent duplicate elements in ret. + pos[sec] |= 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/ELF/ScriptParser.cpp b/lld/ELF/ScriptParser.cpp --- a/lld/ELF/ScriptParser.cpp +++ b/lld/ELF/ScriptParser.cpp @@ -646,19 +646,24 @@ std::vector ret; while (!errorCount() && peek() != ")") { StringMatcher excludeFilePat; + bool excludeFile = false; if (consume("EXCLUDE_FILE")) { expect("("); excludeFilePat = readFilePatterns(); + excludeFile = true; } StringMatcher SectionMatcher; - while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE") + while (!errorCount() && peek() != ")" && peek() != "EXCLUDE_FILE" && + peek2() != "(") SectionMatcher.addPattern(unquote(next())); if (!SectionMatcher.empty()) ret.push_back({std::move(excludeFilePat), std::move(SectionMatcher)}); - else + else if (excludeFile) setError("section pattern is expected"); + else + break; } 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 @@ -1,39 +1,39 @@ # REQUIRES: x86 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %tfile1.o -# RUN: echo "SECTIONS { .abc : { *(SORT(.foo.*) .bar.*) } }" > %t1.script +# RUN: echo "SECTIONS { .abc : { *(SORT(.foo.*) .a* .a* SORT(.bar.*) .b*) } }" > %t1.script # RUN: ld.lld -o %t1 --script %t1.script %tfile1.o -# RUN: llvm-objdump -s %t1 | FileCheck %s +# RUN: llvm-readelf -x .abc %t1 | FileCheck %s -# CHECK: Contents of section .abc: -# CHECK: 01000000 00000000 02000000 00000000 -# CHECK: 03000000 00000000 04000000 00000000 -# CHECK: 06000000 00000000 05000000 00000000 +## With the current implementation, the sorting method of the first pattern +## is used and it applies on all patterns, i.e. SORT applies on both .foo.* and .bar.*. +## SORT has strange behaviors in GNU ld as well +## https://sourceware.org/pipermail/binutils/2020-November/114083.html +# CHECK: Hex dump of section '.abc' +# CHECK-NEXT: 0x00000000 01020306 05040708 090b0c0a # RUN: echo "SECTIONS { \ -# RUN: .abc : { *(SORT(.foo.* EXCLUDE_FILE (*file1.o) .bar.*) .bar.*) } \ +# RUN: .abc : { *(SORT(.foo.* EXCLUDE_FILE (*file1.o) .bar.*) .a* SORT(.bar.*) .b*) } \ # RUN: }" > %t2.script # RUN: ld.lld -o %t2 --script %t2.script %tfile1.o -# RUN: llvm-objdump -s %t2 | FileCheck %s +# RUN: llvm-readelf -x .abc %t2 | FileCheck %s .text .globl _start _start: -.section .foo.2,"a" - .quad 2 +.section .foo.2,"a"; .byte 2 +.section .foo.3,"a"; .byte 3 +.section .foo.1,"a"; .byte 1 -.section .foo.3,"a" - .quad 3 +.section .a6,"a"; .byte 6 +.section .a5,"a"; .byte 5 +.section .a4,"a"; .byte 4 -.section .foo.1,"a" - .quad 1 +.section .bar.7,"a"; .byte 7 +.section .bar.9,"a"; .byte 9 +.section .bar.8,"a"; .byte 8 -.section .bar.4,"a" - .quad 4 - -.section .bar.6,"a" - .quad 6 - -.section .bar.5,"a" - .quad 5 +.section .b11,"a"; .byte 11 +.section .b12,"a"; .byte 12 +.section .b10,"a"; .byte 10