[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
Thu Mar 11 12:05:20 PST 2021
MaskRay created this revision.
MaskRay added reviewers: grimar, jhenderson, peter.smith.
Herald added subscribers: dang, arichardson, emaste.
MaskRay requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
If the number of sections changes, which is common for relinks after
incremental updates, the section order may change drastically.
Special case -1 to reverse input sections. This is a stable transform
and can catch issues like Static Initialization Order Fiasco more
reliably. Usually such a problem is due to the relative order between
two problematic input files A and B. With a fixed/random seed, the order
of A and B may stay unchanged until the user adds new sections or
removes some sections.
Repository:
rG LLVM Github Monorepo
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 --symbol-ordering-file %t_order.txt --shuffle-sections=-1 %t.o -o %t-1.out
+# RUN: llvm-readelf -x .text %t-1.out | FileCheck %s --check-prefix=SHUFFLE3
+# SHUFFLE-1: Hex dump of section '.text':
+# SHUFFLE-1-NEXT: 02cccccc 040301
+
## .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,7 @@
.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 input sections. 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 sections. 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,7 @@
"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 input sections. 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.330045.patch
Type: text/x-patch
Size: 2886 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210311/58a44231/attachment.bin>
More information about the llvm-commits
mailing list