1
1
mirror of https://github.com/rui314/mold.git synced 2024-10-04 16:48:04 +03:00
mold/test/elf/gc-sections.sh
2022-05-09 13:56:48 +08:00

64 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
export LC_ALL=C
set -e
CC="${CC:-cc}"
CXX="${CXX:-c++}"
GCC="${GCC:-gcc}"
GXX="${GXX:-g++}"
OBJDUMP="${OBJDUMP:-objdump}"
MACHINE="${MACHINE:-$(uname -m)}"
testname=$(basename "$0" .sh)
echo -n "Testing $testname ... "
cd "$(dirname "$0")"/../..
t=out/test/elf/$testname
mkdir -p $t
cat <<EOF > $t/a.cc
#include <stdio.h>
int two() { return 2; }
int live_var1 = 1;
int live_var2 = two();
int dead_var1 = 3;
int dead_var2 = 4;
void live_fn1() {}
void live_fn2() { live_fn1(); }
void dead_fn1() {}
void dead_fn2() { dead_fn1(); }
int main() {
printf("%d %d\n", live_var1, live_var2);
live_fn2();
}
EOF
$CXX -B. -o $t/exe1 $t/a.cc -ffunction-sections -fdata-sections
readelf --symbols $t/exe1 > $t/log.1
grep -qv live_fn1 $t/log.1
grep -qv live_fn2 $t/log.1
grep -qv dead_fn1 $t/log.1
grep -qv dead_fn2 $t/log.1
grep -qv live_var1 $t/log.1
grep -qv live_var2 $t/log.1
grep -qv dead_var1 $t/log.1
grep -qv dead_var2 $t/log.1
$QEMU $t/exe1 | grep -q '1 2'
$CXX -B. -o $t/exe2 $t/a.cc -ffunction-sections -fdata-sections -Wl,-gc-sections
readelf --symbols $t/exe2 > $t/log.2
grep -q live_fn1 $t/log.2
grep -q live_fn2 $t/log.2
grep -qv dead_fn1 $t/log.2
grep -qv dead_fn2 $t/log.2
grep -q live_var1 $t/log.2
grep -q live_var2 $t/log.2
grep -qv dead_var1 $t/log.2
grep -qv dead_var2 $t/log.2
$QEMU $t/exe2 | grep -q '1 2'
echo OK