static_prune_aa64.sh (2580B)
1 #!/usr/bin/env bash 2 # Structural checks for AArch64 O1 static-function pruning. 3 set -euo pipefail 4 5 ROOT="$(cd "$(dirname "$0")/../.." && pwd)" 6 KIT="${KIT:-$ROOT/build/kit}" 7 WORK="$ROOT/build/test/opt/static_prune_aa64" 8 mkdir -p "$WORK" 9 10 compile_case() { 11 local name=$1 12 local src="$WORK/$name.c" 13 local obj="$WORK/$name.o" 14 cat > "$src" 15 "$KIT" cc -target aarch64-linux-gnu -O1 -std=c11 -c "$src" \ 16 -o "$obj" > "$WORK/$name.cc.out" 2>&1 17 "$KIT" nm "$obj" > "$WORK/$name.nm" 2>&1 18 } 19 20 has_sym() { 21 local name=$1 22 local sym=$2 23 grep -Eq "[[:space:]]$sym$" "$WORK/$name.nm" 24 } 25 26 want_sym() { 27 local name=$1 28 local sym=$2 29 if ! has_sym "$name" "$sym"; then 30 printf 'static-prune FAILED: %s missing expected symbol %s\n' \ 31 "$name" "$sym" >&2 32 sed 's/^/ | /' "$WORK/$name.nm" >&2 33 exit 1 34 fi 35 } 36 37 want_no_sym() { 38 local name=$1 39 local sym=$2 40 if has_sym "$name" "$sym"; then 41 printf 'static-prune FAILED: %s kept unexpected symbol %s\n' \ 42 "$name" "$sym" >&2 43 sed 's/^/ | /' "$WORK/$name.nm" >&2 44 exit 1 45 fi 46 } 47 48 compile_case unused_static <<'EOF' 49 static int dead_unused(void) { return 1; } 50 int exported_live(void) { return 2; } 51 EOF 52 want_no_sym unused_static dead_unused 53 want_sym unused_static exported_live 54 55 compile_case reached_static <<'EOF' 56 static int reached_helper(void) { return 3; } 57 int exported_calls_helper(void) { return reached_helper(); } 58 EOF 59 want_sym reached_static reached_helper 60 want_sym reached_static exported_calls_helper 61 62 compile_case call_chain <<'EOF' 63 static int chain_leaf(void) { return 4; } 64 static int chain_mid(void) { return chain_leaf(); } 65 int exported_chain(void) { return chain_mid(); } 66 EOF 67 want_sym call_chain chain_leaf 68 want_sym call_chain chain_mid 69 70 compile_case global_fnptr <<'EOF' 71 static int pointed_target(void) { return 5; } 72 int (*exported_fp)(void) = pointed_target; 73 EOF 74 want_sym global_fnptr pointed_target 75 want_sym global_fnptr exported_fp 76 77 compile_case used_attr <<'EOF' 78 static int used_target(void) __attribute__((used)); 79 static int used_target(void) { return 6; } 80 int exported_for_used_case(void) { return 0; } 81 EOF 82 want_sym used_attr used_target 83 84 compile_case alias_root <<'EOF' 85 static int aliased_target(void) { return 7; } 86 int exported_alias(void) __attribute__((alias("aliased_target"))); 87 EOF 88 want_sym alias_root aliased_target 89 want_sym alias_root exported_alias 90 91 compile_case file_scope_asm <<'EOF' 92 __asm__(".text\n"); 93 static int asm_conserved_local(void) { return 8; } 94 int exported_with_asm(void) { return 0; } 95 EOF 96 want_sym file_scope_asm asm_conserved_local 97 98 printf 'static-prune-aa64: ok\n'