Files
OpenRoyale/tools/scripts/gen_fmod_import.sh
T
2026-07-09 07:40:42 +02:00

40 lines
2.2 KiB
Bash

#!/usr/bin/env bash
# gen_fmod_import.sh - build a LINK-CLEAN FMOD import stub for a given ABI.
#
# The FMOD 1.05.11 prebuilt libfmod.so has a legacy .dynsym (linker markers _bss_start/_edata/_end/...
# placed in the global section out of order) that modern lld refuses to link against directly. Rather
# than patch it, we synthesize a fresh stub .so that EXPORTS the same symbols with a clean table and
# the same SONAME (libfmod.so). The reconstructed libg links against this stub; at RUNTIME the real
# libfmod.so (shipped in the APK) resolves the calls. The stub is never shipped.
#
# Usage: gen_fmod_import.sh <arm64-v8a|armeabi-v7a>
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJ="$(cd "$HERE/../.." && pwd)"
ABI="${1:?usage: gen_fmod_import.sh <arm64-v8a|armeabi-v7a>}"
NDK="${ANDROID_NDK_HOME:-$PROJ/tools/android-sdk/ndk/26.3.11579264}"
TC="$NDK/toolchains/llvm/prebuilt/linux-x86_64"
NM="$TC/bin/llvm-nm"; CC="$TC/bin/clang"
case "$ABI" in arm64-v8a) TRIPLE=aarch64-linux-android21;; armeabi-v7a) TRIPLE=armv7a-linux-androideabi21;; *) echo "bad ABI"; exit 1;; esac
SRC="$PROJ/native/third_party/fmod/$ABI/libfmod.so"
OUT="$PROJ/native/third_party/fmod/import/$ABI"; mkdir -p "$OUT"
[ -f "$SRC" ] || { echo "FMOD lib missing: $SRC"; exit 2; }
ASM="$(mktemp --suffix=.s)"; trap 'rm -f "$ASM"' EXIT
{
echo "// auto-generated FMOD import stub for $ABI - link-only, never shipped"
echo ".text"
# exported code symbols (T) -> global labels in .text
"$NM" -D --defined-only "$SRC" | awk '$2=="T"{print $3}' | while read -r s; do
printf '.globl %s\n.type %s,%%function\n%s:\n' "$s" "$s" "$s"
done
echo ".bss"
# exported data symbols (B/D/R) -> global labels in .bss (skip the legacy linker markers)
"$NM" -D --defined-only "$SRC" | awk '($2=="B"||$2=="D"||$2=="R"){print $3}' \
| grep -vE '^(_bss_end__|__bss_start|__bss_start__|__bss_end__|_edata|_end|__end__|_start|__start)$' \
| while read -r s; do printf '.globl %s\n%s: .zero 8\n' "$s" "$s"; done
} > "$ASM"
"$CC" --target="$TRIPLE" -nostdlib -shared -Wl,-soname,libfmod.so -o "$OUT/libfmod.so" "$ASM"
echo "[+] import stub: $OUT/libfmod.so ($("$NM" -D --defined-only "$OUT/libfmod.so" | grep -c FMOD) FMOD symbols; SONAME libfmod.so)"