[PATCH] D74887: [ELF] Shuffle .init_array/.fini_array with --shuffle-sections=

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 19 23:36:51 PST 2020


MaskRay created this revision.
MaskRay added reviewers: grimar, respindola, ruiu.
Herald added subscribers: llvm-commits, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

Useful for detecting static initialization order fiasco.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D74887

Files:
  lld/ELF/Writer.cpp
  lld/test/ELF/shuffle-sections-init-fini.s


Index: lld/test/ELF/shuffle-sections-init-fini.s
===================================================================
--- /dev/null
+++ lld/test/ELF/shuffle-sections-init-fini.s
@@ -0,0 +1,25 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64 %s -o %t.o
+
+# RUN: ld.lld --shuffle-sections=1 %t.o -o %t
+# RUN: llvm-readelf -x .init_array -x .fini_array %t | FileCheck %s
+# RUN: ld.lld --shuffle-sections=2 %t.o -o %t
+# RUN: llvm-readelf -x .init_array -x .fini_array %t | FileCheck %s
+# CHECK:      Hex dump of section '.init_array'
+# CHECK-NEXT: ff{{....}}
+# CHECK:      Hex dump of section '.fini_array'
+# CHECK-NEXT: {{....}}ff
+
+.section .init_array,"aw", at init_array,unique,0
+.byte 0
+.section .init_array,"aw", at init_array,unique,1
+.byte 1
+.section .init_array.1,"aw", at init_array,unique,2
+.byte 255
+
+.section .fini_array,"aw", at fini_array,unique,0
+.byte 0
+.section .fini_array,"aw", at fini_array,unique,1
+.byte 1
+.section .fini_array.1,"aw", at fini_array,unique,2
+.byte 255
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -1355,11 +1355,22 @@
                         const DenseMap<const InputSectionBase *, int> &order) {
   StringRef name = sec->name;
 
+  // Sort input sections by priority using the list provided
+  // by --symbol-ordering-file.
+  auto sortBySectionOrder = [&]() {
+    if (!order.empty())
+      for (BaseCommand *b : sec->sectionCommands)
+        if (auto *isd = dyn_cast<InputSectionDescription>(b))
+          sortISDBySectionOrder(isd, order);
+  };
+
   // Sort input sections by section name suffixes for
   // __attribute__((init_priority(N))).
   if (name == ".init_array" || name == ".fini_array") {
-    if (!script->hasSectionsCommand)
+    if (!script->hasSectionsCommand) {
+      sortBySectionOrder();
       sec->sortInitFini();
+    }
     return;
   }
 
@@ -1392,12 +1403,7 @@
     return;
   }
 
-  // Sort input sections by priority using the list provided
-  // by --symbol-ordering-file.
-  if (!order.empty())
-    for (BaseCommand *b : sec->sectionCommands)
-      if (auto *isd = dyn_cast<InputSectionDescription>(b))
-        sortISDBySectionOrder(isd, order);
+  sortBySectionOrder();
 }
 
 // If no layout was provided by linker script, we want to apply default


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D74887.245588.patch
Type: text/x-patch
Size: 2341 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200220/d35bdfda/attachment.bin>


More information about the llvm-commits mailing list