-
-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ELF] Add the --shuffle-sections option
If the --shuffle-sections is given, mold now randomize the output by shuffling input sections randomly. This feature is compatible with lld's --shuffle-sections=SEED option introduced in https://reviews.llvm.org/D74791. This feature is useful when you want to equalize the conditions of benchmarks. That is, some particular memory layout can produce a very good benchmark number due to hardware-level cache hit rate or something like that. Therefore, even if you get a good benchmark number after changing code, there's a chance that that's caused by the layout change and not by the new code itself. With --shuffle-sections, you can isolate that. The other use case I can think of is to enhance security. If you build your program as a position-independent executable, the kernel automatically enables ASLR (Address Space Layout Randomization), but ASLR only shift the entire program image in memory by some random offset; Relative offsets between sections remain the same. If you compile programs from source, by using --shuffle-sections, you can make the offsets unpredictable to attackers.
- Loading branch information
Showing
5 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#!/bin/bash | ||
export LANG= | ||
set -e | ||
CC="${CC:-cc}" | ||
CXX="${CXX:-c++}" | ||
testname=$(basename "$0" .sh) | ||
echo -n "Testing $testname ... " | ||
cd "$(dirname "$0")"/../.. | ||
mold="$(pwd)/mold" | ||
t=out/test/elf/$testname | ||
mkdir -p $t | ||
|
||
cat <<EOF | $CC -o $t/a.o -c -xc - | ||
#include <stdio.h> | ||
int main() { | ||
printf("Hello world\n"); | ||
} | ||
EOF | ||
|
||
$CC -B. -o $t/exe1 $t/a.o | ||
$t/exe1 | grep -q 'Hello world' | ||
|
||
$CC -B. -o $t/exe2 $t/a.o -Wl,-shuffle-sections | ||
$t/exe2 | grep -q 'Hello world' | ||
|
||
$CC -B. -o $t/exe3 $t/a.o -Wl,-shuffle-sections=100 | ||
$t/exe3 | grep -q 'Hello world' | ||
|
||
$CC -B. -o $t/exe4 $t/a.o -Wl,-shuffle-sections=100 | ||
$t/exe4 | grep -q 'Hello world' | ||
|
||
diff $t/exe3 $t/exe4 | ||
|
||
$CC -B. -o $t/exe5 $t/a.o -Wl,-shuffle-sections=101 | ||
$t/exe5 | grep -q 'Hello world' | ||
|
||
! diff $t/exe3 $t/exe4 >& /dev/null || false | ||
|
||
echo OK |