[PATCH] D98445: [ELF] Special case --shuffle-sections=-1 to reverse input sections

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 17 09:33:03 PDT 2021


This revision was automatically updated to reflect the committed changes.
Closed by commit rG423cb321dfae: [ELF] Special case --shuffle-sections=-1 to reverse input sections (authored by MaskRay).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98445/new/

https://reviews.llvm.org/D98445

Files:
  lld/ELF/Options.td
  lld/ELF/Writer.cpp
  lld/docs/ld.lld.1
  lld/test/ELF/shuffle-sections.s


Index: lld/test/ELF/shuffle-sections.s
===================================================================
--- lld/test/ELF/shuffle-sections.s
+++ lld/test/ELF/shuffle-sections.s
@@ -26,6 +26,12 @@
 # SHUFFLE3: Hex dump of section '.text':
 # SHUFFLE3-NEXT: 02cccccc 010403
 
+## As a special case, -1 reverses sections as a stable transform.
+# RUN: ld.lld --shuffle-sections=-1 %t.o -o %t-1.out
+# RUN: llvm-readelf -x .text %t-1.out | FileCheck %s --check-prefix=SHUFFLE-1
+# SHUFFLE-1: Hex dump of section '.text':
+# SHUFFLE-1-NEXT: 040302cc 01
+
 ## .text has an alignment of 4.
 .global _start
 _start:
Index: lld/docs/ld.lld.1
===================================================================
--- lld/docs/ld.lld.1
+++ lld/docs/ld.lld.1
@@ -487,7 +487,8 @@
 .It Fl -shared , Fl -Bsharable
 Build a shared object.
 .It Fl -shuffle-sections Ns = Ns Ar seed
-Shuffle input sections using the given seed. If 0, use a random seed.
+Shuffle input sections using the given seed.
+If -1, reverse the section order. If 0, use a random seed.
 .It Fl -soname Ns = Ns Ar value , Fl h Ar value
 Set
 .Dv DT_SONAME
Index: lld/ELF/Writer.cpp
===================================================================
--- lld/ELF/Writer.cpp
+++ lld/ELF/Writer.cpp
@@ -1301,8 +1301,15 @@
   for (int &prio : priorities)
     prio = curPrio++;
   uint32_t seed = *config->shuffleSectionSeed;
-  std::mt19937 g(seed ? seed : std::random_device()());
-  llvm::shuffle(priorities.begin(), priorities.end(), g);
+  if (seed == UINT32_MAX) {
+    // If --shuffle-sections=-1, reverse the section order. The section order is
+    // stable even if the number of sections changes. This is useful to catch
+    // issues like static initialization order fiasco reliably.
+    std::reverse(priorities.begin(), priorities.end());
+  } else {
+    std::mt19937 g(seed ? seed : std::random_device()());
+    llvm::shuffle(priorities.begin(), priorities.end(), g);
+  }
   int prioIndex = 0;
   for (InputSectionBase *sec : inputSections) {
     if (order.try_emplace(sec, priorities[prioIndex]).second)
Index: lld/ELF/Options.td
===================================================================
--- lld/ELF/Options.td
+++ lld/ELF/Options.td
@@ -587,7 +587,8 @@
     "Give unique names to every basic block section for LTO",
     "Do not give unique names to every basic block section for LTO (default)">;
 def shuffle_sections: JJ<"shuffle-sections=">, MetaVarName<"<seed>">,
-  HelpText<"Shuffle input sections using the given seed. If 0, use a random seed">;
+  HelpText<"Shuffle input sections using the given seed. "
+  "If -1, reverse the section order. If 0, use a random seed">;
 def thinlto_cache_dir: JJ<"thinlto-cache-dir=">,
   HelpText<"Path to ThinLTO cached object file directory">;
 defm thinlto_cache_policy: EEq<"thinlto-cache-policy", "Pruning policy for the ThinLTO cache">;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98445.331291.patch
Type: text/x-patch
Size: 2869 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210317/d93532d7/attachment.bin>


More information about the llvm-commits mailing list