diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -447,19 +447,19 @@ return std::regex_match(s.begin(), s.end(), re); } -// .ctors and .dtors are sorted by this priority from highest to lowest. -// -// 1. The section was contained in crtbegin (crtbegin contains -// some sentinel value in its .ctors and .dtors so that the runtime -// can find the beginning of the sections.) -// -// 2. The section has an optional priority value in the form of ".ctors.N" -// or ".dtors.N" where N is a number. Unlike .{init,fini}_array, -// they are compared as string rather than number. -// -// 3. The section is just ".ctors" or ".dtors". -// -// 4. The section was contained in crtend, which contains an end marker. +// .ctors and .dtors are sorted by this order: +// +// 1. .ctors/.dtors in crtbegin (which contains a sentinel value -1). +// 2. The section is named ".ctors" or ".dtors" (priority: 65536). +// 3. The section has an optional priority value in the form of ".ctors.N" or +// ".dtors.N" where N is a number in the form of %05u (priority: 65535-N). +// 4. .ctors/.dtors in crtend (which contains a sentinel value 0). +// +// For 2 and 3, the sections are sorted by priority from high to low, e.g. +// .ctors (65536), .ctors.00100 (65436), .ctors.00200 (65336). In GNU ld's +// internal linker scripts, the sorting is by string comparison which can +// achieve the same goal given the optional priority values are of the same +// length. // // In an ideal world, we don't need this function because .init_array and // .ctors are duplicate features (and .init_array is newer.) However, there @@ -474,13 +474,7 @@ bool endB = isCrtend(b->file->getName()); if (endA != endB) return endB; - StringRef x = a->name; - StringRef y = b->name; - assert(x.startswith(".ctors") || x.startswith(".dtors")); - assert(y.startswith(".ctors") || y.startswith(".dtors")); - x = x.substr(6); - y = y.substr(6); - return x < y; + return getPriority(a->name) > getPriority(b->name); } // Sorts input sections by the special rules for .ctors and .dtors. @@ -492,16 +486,18 @@ llvm::stable_sort(isd->sections, compCtors); } -// If an input string is in the form of "foo.N" where N is a number, -// return N. Otherwise, returns 65536, which is one greater than the -// lowest priority. +// If an input string is in the form of "foo.N" where N is a number, return N +// (65535-N if .ctors.N or .dtors.N). Otherwise, returns 65536, which is one +// greater than the lowest priority. int elf::getPriority(StringRef s) { size_t pos = s.rfind('.'); if (pos == StringRef::npos) return 65536; int v; if (!to_integer(s.substr(pos + 1), v, 10)) - return 65536; + v = 65536; + else if (pos == 6 && (s.startswith(".ctors") || s.startswith(".dtors"))) + v = 65535 - v; return v; } diff --git a/lld/test/ELF/linkerscript/sort-init.s b/lld/test/ELF/linkerscript/sort-init.s --- a/lld/test/ELF/linkerscript/sort-init.s +++ b/lld/test/ELF/linkerscript/sort-init.s @@ -1,26 +1,43 @@ # REQUIRES: x86 -# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o -# RUN: echo "SECTIONS { .init_array : { *(SORT_BY_INIT_PRIORITY(.init_array.* foo*)) } }" > %t1.script -# RUN: ld.lld --script %t1.script %t1.o -o %t2 -# RUN: llvm-objdump -s %t2 | FileCheck %s +## Test SORT_BY_INIT_PRIORITY can be used to convert .ctors into .init_array -# CHECK: Contents of section .init_array: -# CHECK-NEXT: 03020000 00060000 010405 +# RUN: split-file %s %t +# RUN: llvm-mc -filetype=obj -triple=x86_64 %t/asm -o %t1.o +# RUN: ld.lld -T %t/lds %t1.o -o %t2 +# RUN: llvm-readelf -x .init_array %t2 | FileCheck %s +# CHECK: Hex dump of section '.init_array': +# CHECK-NEXT: 0x00000001 00010203 04050607 + +#--- asm .globl _start _start: nop .section foo, "aw", @init_array - .byte 6 -.section .init_array, "aw", @init_array - .align 8 - .byte 1 -.section .init_array.100, "aw", @init_array - .long 2 -.section .init_array.5, "aw", @init_array + .byte 5 + +.section .ctors.65435, "a" .byte 3 -.section .init_array, "aw", @init_array +.section .init_array.100, "aw", @init_array .byte 4 + +.section .init_array.7, "aw", @init_array + .byte 2 +.section .ctors.65529,"a" + .byte 1 +.section .init_array.5, "aw", @init_array + .byte 0 + .section .init_array, "aw", @init_array - .byte 5 + .byte 6 +.section .ctors, "a" + .byte 7 + +#--- lds +SECTIONS { + .init_array : { + *(SORT_BY_INIT_PRIORITY(.init_array.* .ctors.*) SORT_BY_INIT_PRIORITY(foo*)) + *(.init_array .ctors) + } +}