From 2625882ebbb3752fb215ec94dc128019c3b381c5 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 10 Feb 2016 23:26:27 +0000 Subject: [PATCH] ELF: Use stable sort to sort .{init,fini}_array sections. Global constructors and destructors are guaranteed to be called in the order as they appear in a translation unit. So we don't want to mess up the order if they have the same priority. llvm-svn: 260463 --- lld/ELF/OutputSections.cpp | 5 +++-- lld/test/ELF/init_fini_priority.s | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lld/ELF/OutputSections.cpp b/lld/ELF/OutputSections.cpp index c1a05ac..b6b1787 100644 --- a/lld/ELF/OutputSections.cpp +++ b/lld/ELF/OutputSections.cpp @@ -772,8 +772,9 @@ template void OutputSection::sortByPriority() { std::vector V; for (InputSection *S : Sections) V.push_back({getPriority(S->getSectionName()), S}); - std::sort(V.begin(), V.end(), - [](const Pair &A, const Pair &B) { return A.first < B.first; }); + std::stable_sort(V.begin(), V.end(), [](const Pair &A, const Pair &B) { + return A.first < B.first; + }); Sections.clear(); for (Pair &P : V) Sections.push_back(P.second); diff --git a/lld/test/ELF/init_fini_priority.s b/lld/test/ELF/init_fini_priority.s index d46ae6f..84e5dc3 100644 --- a/lld/test/ELF/init_fini_priority.s +++ b/lld/test/ELF/init_fini_priority.s @@ -14,16 +14,24 @@ _start: .long 2 .section .init_array.5, "aw", @init_array .byte 3 +.section .init_array, "aw", @init_array + .byte 4 +.section .init_array, "aw", @init_array + .byte 5 .section .fini_array, "aw", @fini_array .align 8 - .byte 4 + .byte 0x11 .section .fini_array.100, "aw", @fini_array - .long 5 + .long 0x12 .section .fini_array.5, "aw", @fini_array - .byte 6 + .byte 0x13 +.section .fini_array, "aw", @fini_array + .byte 0x14 +.section .fini_array, "aw", @fini_array + .byte 0x15 // CHECK: Contents of section .init_array: -// CHECK-NEXT: 03020000 00000000 01 +// CHECK-NEXT: 03020000 00000000 010405 // CHECK: Contents of section .fini_array: -// CHECK-NEXT: 06050000 00000000 04 +// CHECK-NEXT: 13120000 00000000 111415 -- 2.7.4