[lld] 7b44f04 - Add a llvm::shuffle and use it in lld
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 22 10:06:35 PST 2020
Author: Rafael Ávila de Espíndola
Date: 2020-02-22T10:05:29-08:00
New Revision: 7b44f0428af4000372af5f016995c032a959d17e
URL: https://github.com/llvm/llvm-project/commit/7b44f0428af4000372af5f016995c032a959d17e
DIFF: https://github.com/llvm/llvm-project/commit/7b44f0428af4000372af5f016995c032a959d17e.diff
LOG: Add a llvm::shuffle and use it in lld
With this --shuffle-sections=seed produces the same result in every
host.
Reviewed By: grimar, MaskRay
Differential Revision: https://reviews.llvm.org/D74971
Added:
Modified:
lld/ELF/Writer.cpp
lld/test/ELF/shuffle-sections-init-fini.s
lld/test/ELF/shuffle-sections.s
llvm/include/llvm/ADT/STLExtras.h
Removed:
################################################################################
diff --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index be9273fc2095..8fd69af68fe7 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -1218,7 +1218,7 @@ static void maybeShuffle(DenseMap<const InputSectionBase *, int> &order) {
prio = curPrio++;
uint32_t seed = *config->shuffleSectionSeed;
std::mt19937 g(seed ? seed : std::random_device()());
- std::shuffle(priorities.begin(), priorities.end(), g);
+ llvm::shuffle(priorities.begin(), priorities.end(), g);
int prioIndex = 0;
for (InputSectionBase *sec : inputSections) {
if (order.try_emplace(sec, priorities[prioIndex]).second)
diff --git a/lld/test/ELF/shuffle-sections-init-fini.s b/lld/test/ELF/shuffle-sections-init-fini.s
index d8e4832fb7fe..31d87bb32444 100644
--- a/lld/test/ELF/shuffle-sections-init-fini.s
+++ b/lld/test/ELF/shuffle-sections-init-fini.s
@@ -21,12 +21,12 @@
# CHECK: Hex dump of section '.init_array'
# CHECK-NEXT: 0x{{[0-9a-f]+}} ff
# ORDERED-SAME: 000102 03040506 0708090a 0b
-# SHUFFLED-NOT: 000102 03040506 0708090a 0b
+# SHUFFLED-SAME: 04000b 06010a08 09070203 05
# CHECK: Hex dump of section '.fini_array'
# CHECK-NEXT: 0x{{[0-9a-f]+}} ff
-# ORDERED-SAME: 000102 03040506 0708090a 0b
-# SHUFFLED-NOT: 000102 03040506 0708090a 0b
+# ORDERED-SAME: 000102 03040506 0708090a 0b
+# SHUFFLED-SAME: 090401 070b0003 080a0605 02
## With a SECTIONS command, SHT_INIT_ARRAY prirotities are ignored.
## All .init_array* are shuffled together.
@@ -40,11 +40,8 @@
# CHECK2: Hex dump of section '.init_array'
# ORDERED2-NEXT: 0x{{[0-9a-f]+}} 00010203 04050607 08090a0b ff
-# SHUFFLED2-NOT: 0x{{[0-9a-f]+}} 00010203 04050607 08090a0b ff
+# SHUFFLED2-NEXT: 0x{{[0-9a-f]+}} 04000b06 010a0809 07ff0203 05
-## std::shuffle have
diff erent implementations.
-## When the number of input sections are large, it is almost guaranteed
-## to have an unordered result with --shuffle-sections=.
.irp i,0,1,2,3,4,5,6,7,8,9,10,11
.section .init,"ax", at progbits,unique,\i
.byte \i
diff --git a/lld/test/ELF/shuffle-sections.s b/lld/test/ELF/shuffle-sections.s
index 259ee5fed462..bc0f57b98d7a 100644
--- a/lld/test/ELF/shuffle-sections.s
+++ b/lld/test/ELF/shuffle-sections.s
@@ -6,6 +6,12 @@
# CHECK: Hex dump of section '.text':
# CHECK-NEXT: 01020304
+## --shuffle-sections= shuffles input sections.
+# RUN: ld.lld --shuffle-sections=1 %t.o -o %t1.out
+# RUN: llvm-readelf -x .text %t1.out | FileCheck %s --check-prefix=SHUFFLE1
+# SHUFFLE1: Hex dump of section '.text':
+# SHUFFLE1-NEXT: 0204cccc 0103
+
## Test that --shuffle-sections= can be used with --symbol-ordering-file
# RUN: echo "foo" > %t_order.txt
# RUN: echo "_start " >> %t_order.txt
@@ -13,12 +19,12 @@
# RUN: ld.lld --symbol-ordering-file %t_order.txt --shuffle-sections=2 %t.o -o %t2.out
# RUN: llvm-readelf -x .text %t2.out | FileCheck %s --check-prefix=SHUFFLE2
# SHUFFLE2: Hex dump of section '.text':
-# SHUFFLE2-NEXT: 02cccccc 01{{....}}
+# SHUFFLE2-NEXT: 02cccccc 010304
# RUN: ld.lld --symbol-ordering-file %t_order.txt --shuffle-sections=3 %t.o -o %t3.out
# RUN: llvm-readelf -x .text %t3.out | FileCheck %s --check-prefix=SHUFFLE3
# SHUFFLE3: Hex dump of section '.text':
-# SHUFFLE3-NEXT: 02cccccc 01{{....}}
+# SHUFFLE3-NEXT: 02cccccc 010403
## .text has an alignment of 4.
.global _start
diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h
index 8888e8d48978..cdde0cb67a43 100644
--- a/llvm/include/llvm/ADT/STLExtras.h
+++ b/llvm/include/llvm/ADT/STLExtras.h
@@ -1007,6 +1007,16 @@ struct are_base_of<T, U, Ts...> {
// Extra additions for arrays
//===----------------------------------------------------------------------===//
+// We have a copy here so that LLVM behaves the same when using
diff erent
+// standard libraries.
+template <class Iterator, class RNG>
+void shuffle(Iterator first, Iterator last, RNG &&g) {
+ // It would be better to use a std::uniform_int_distribution,
+ // but that would be stdlib dependent.
+ for (auto size = last - first; size > 1; ++first, (void)--size)
+ std::iter_swap(first, first + g() % size);
+}
+
/// Find the length of an array.
template <class T, std::size_t N>
constexpr inline size_t array_lengthof(T (&)[N]) {
More information about the llvm-commits
mailing list