Files
isaac-port/decompile.sh
T

39 lines
2.4 KiB
Bash
Executable File

#!/bin/bash
# Usage: decompile.sh [--live|--asm] QUERY [MAX] | decompile.sh --case CASEADDR...
# QUERY = entry/inner address (0x6c0f30 / 006c0f30) or a regex on the qualified name (grep -E).
# Default reads the pre-dumped corpus dump/all.c (instant, concurrency-safe).
# --asm ADDR [N] prints N (default 20) instructions from ADDR (for functions the decompiler times out on).
# --case decompiles single switch cases of a function the decompiler times out on (Entity_Effect::Update): each
# CASEADDR is a jump-table target; the switch is patched to jump straight there in a throwaway copy (DecompileCase.java).
# --live runs the Ghidra decompiler on a copy of the saved project (~25s; env DECOMP_TIMEOUT=secs, default 180).
D="$(cd "$(dirname "$0")" && pwd)"
LIVE=0; SCRIPT=Decompile.java
[ "$1" = --live ] && { LIVE=1; shift; }
[ "$1" = --asm ] && { LIVE=1; SCRIPT=Disasm.java; shift; }
if [ "$1" = --case ]; then
shift; TMP=$(mktemp -d); export JAVA_HOME="$D/jdk"
cp -r "$D/project" "$TMP/project"; rm -f "$TMP"/project/*.lock*
"$D/ghidra_12.2_PUBLIC/support/analyzeHeadless" "$TMP/project" isaac -process isaac-ng.exe -noanalysis -readOnly \
-scriptPath "$D/scripts" -postScript DecompileCase.java "$TMP/out.c" "$@" >/dev/null 2>&1
cat "$TMP/out.c"; rm -rf "$TMP"; exit
fi
Q="$1"; MAX="${2:-20}"
[ -z "$Q" ] && { sed -n '2,7p' "$0"; exit 1; }
if [[ "$Q" =~ ^(0x)?[0-9a-fA-F]{6,8}$ ]]; then
A=$(printf '%08x' $((16#${Q#0x})))
# resolve inner address to containing function (largest entry <= A)
Q=$(awk -F'\t' -v a="$A" '($1 "") <= (a "") {best=$1} END{print best}' <(sort "$D/dump/functions.tsv"))
MODE=addr; ASMADDR=$A
fi
if [ $LIVE = 1 ]; then
# Ghidra locks a project even read-only, so each live run works on a private copy (~160MB, tmpfs-fast)
TMP=$(mktemp -d); OUT="$TMP/out.c"; export JAVA_HOME="$D/jdk"
cp -r "$D/project" "$TMP/project"; rm -f "$TMP"/project/*.lock*
"$D/ghidra_12.2_PUBLIC/support/analyzeHeadless" "$TMP/project" isaac -process isaac-ng.exe -noanalysis -readOnly \
-scriptPath "$D/scripts" -postScript $SCRIPT "${ASMADDR:-$Q}" "$MAX" "$OUT" >/dev/null 2>&1
cat "$OUT"; rm -rf "$TMP"; exit
fi
awk -v q="$Q" -v mode="$MODE" -v max="$MAX" '
/^\/\/@@ / { on = mode == "addr" ? ($2 == q "") : ($3 ~ q); if (on && ++n > max) { print "// ... more matches (raise MAX)"; exit } }
on' "$D/dump/all.c"