ELF: Use stable sort to sort .{init,fini}_array sections.
authorRui Ueyama <ruiu@google.com>
Wed, 10 Feb 2016 23:26:27 +0000 (23:26 +0000)
committerRui Ueyama <ruiu@google.com>
Wed, 10 Feb 2016 23:26:27 +0000 (23:26 +0000)
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
lld/test/ELF/init_fini_priority.s

index c1a05ac..b6b1787 100644 (file)
@@ -772,8 +772,9 @@ template <class ELFT> void OutputSection<ELFT>::sortByPriority() {
   std::vector<Pair> V;
   for (InputSection<ELFT> *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);
index d46ae6f..84e5dc3 100644 (file)
@@ -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