Index: ELF/LinkerScript.cpp =================================================================== --- ELF/LinkerScript.cpp +++ ELF/LinkerScript.cpp @@ -250,6 +250,15 @@ }; case SortSectionPolicy::Priority: return [](InputSectionBase *A, InputSectionBase *B) { + // Scripts may place both .init_array.* and .ctors.* to the same section. + // That what default ld.bfd script do, it contains next line: + // KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) + // SORT_BY_INIT_PRIORITY(.ctors.*))). But .init_array.* + // sections should go in a reversed order in compare with .ctors.*. And so + // behavior of SORT_BY_INIT_PRIORITY is different depending on section + // names. + if (A->Name.startswith(".ctors.") || A->Name.startswith(".dtors")) + return getPriority(A->Name) > getPriority(B->Name); return getPriority(A->Name) < getPriority(B->Name); }; default: Index: test/ELF/linkerscript/sort-init2.s =================================================================== --- test/ELF/linkerscript/sort-init2.s +++ test/ELF/linkerscript/sort-init2.s @@ -0,0 +1,31 @@ +# REQUIRES: x86 +# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o +# RUN: echo "SECTIONS { .init_array : { \ +# RUN: *(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)) } \ +# RUN: .fini_array : { \ +# RUN: *(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)) } }" > %t1.script +# RUN: ld.lld --script %t1.script %t1.o -o %t2 +# RUN: llvm-objdump -s %t2 | FileCheck %s + +# CHECK: Contents of section .init_array: +# CHECK-NEXT: 01020403 +# CHECK: Contents of section .fini_array: +# CHECK-NEXT: 11121413 + +.section .init_array.5, "aw", @init_array + .byte 0x1 +.section .init_array.10, "aw", @init_array + .byte 0x2 +.section .ctors.005, "aw", @progbits + .byte 0x3 +.section .ctors.100, "aw", @progbits + .byte 0x4 + +.section .fini_array.5, "aw", @fini_array + .byte 0x11 +.section .fini_array.10, "aw", @fini_array + .byte 0x12 +.section .dtors.005, "aw", @progbits + .byte 0x13 +.section .dtors.100, "aw", @progbits + .byte 0x14