summaryrefslogtreecommitdiffstats
path: root/userspace/cuda/570.144/scripts/ghidra_decompile.sh
blob: bf95e8c5f83bd73bc1b5d541ae67bb3dd80617a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/bash
# SPDX-License-Identifier: BSD-3-Clause
#
# ghidra_decompile.sh - decompile the function containing a file vaddr.
#
# Runs Ghidra headless with -noanalysis: the program is imported, the crash
# address is disassembled and turned into a function on demand, then
# decompiled. That avoids a full auto-analysis pass over a ~70 MB library.
#
# Usage: scripts/ghidra_decompile.sh <binary> <hex-vaddr> [outfile]
#   env: GHIDRA_HOME (default: /snap/ghidra/current/ghidra)
#        PROJECT_DIR (default: ../run/ghidra-proj)
#
# Output: run/<basename>_<vaddr>.c unless outfile is given.
set -e

HERE="$(cd "$(dirname "$0")" && pwd)"
ROOT="$(cd "$HERE/.." && pwd)"
RUN="$ROOT/run"
BIN="${1:?usage: $0 <binary> <hex-vaddr> [outfile]}"
VADDR="${2:?usage: $0 <binary> <hex-vaddr> [outfile]}"
OUT="${3:-$RUN/$(basename "$BIN")_${VADDR}.c}"

GHIDRA_HOME="${GHIDRA_HOME:-/snap/ghidra/current/ghidra}"
ANALYZE="$GHIDRA_HOME/support/analyzeHeadless"
PROJECT_DIR="${PROJECT_DIR:-$RUN/ghidra-proj}"
PROJECT_NAME="${PROJECT_NAME:-cuda-analysis}"

[ -x "$ANALYZE" ] || { echo "analyzeHeadless not found at $ANALYZE"; exit 1; }
mkdir -p "$PROJECT_DIR" "$RUN"

echo "[*] decompiling $BIN at $VADDR -> $OUT"
"$ANALYZE" "$PROJECT_DIR" "$PROJECT_NAME" \
    -import "$BIN" \
    -overwrite \
    -noanalysis \
    -scriptPath "$ROOT/ghidra" \
    -postScript DecompileAt.java "$VADDR" "$OUT" \
    -deleteProject \
    2>&1 | tail -5

[ -f "$OUT" ] || { echo "[FAIL] no output produced"; exit 1; }
echo "[ok] $OUT"