JSRF-Decompilation/ghidra/delink.sh
KeybadeBlox 9b9a736d4c Support DOS line endings in delink.sh
DOS line endings in objects.csv, as one might encounter when cloning on
Windows, would break the delinking script by adding an extra \r onto
every line.  The extra carriage return character is now removed.
2026-05-13 20:17:32 -04:00

95 lines
2.9 KiB
Bash
Executable file

#!/bin/sh -eu
# Script to produce delinked object files in decompile/target/ directory from
# objects.csv and a Ghidra project via boricj's delinker extension
main() {
if \
[ $# -eq 0 ] &&\
[ -n "${GHIDRA_HOME-}" ] &&\
[ -n "${JSRFDECOMP_PROJECTPATH-}" ] &&\
[ -n "${JSRFDECOMP_PROJECTNAME-}" ]
then
ghidra_path=$GHIDRA_HOME
project_path=$JSRFDECOMP_PROJECTPATH
project_name=$JSRFDECOMP_PROJECTNAME
elif [ $# -eq 3 ]; then
ghidra_path=$1
project_path=$2
project_name=$3
else usage
fi
printf '=== Delinking object files into ../decompile/target/ ===\n'
# Read objects.csv line by line (with EOL handling for Windows people)
CR=$(printf '\r')
while IFS=$CR read -r line; do
# Split columns (col 1 in $1, col 2 in $2, etc.)
set -f; IFS_PREV=$IFS; IFS=,
set -- $line
set +f; IFS=$IFS_PREV
if [ "$2" = true ]; then # If object is marked for extraction
object_name=$1
printf '\n--- %s ---\n' "$object_name"
# Delete object name and delink toggle positional
# arguments, leaving only address ranges
shift 2
# Call Ghidra delinker script
delink "$ghidra_path" "$project_path" "$project_name" "$*" "$object_name"
fi
done < objects.csv
printf '\nDelinking complete!\n'
}
usage() {
printf '%s\n'\
'Usage: delink.sh GHIDRA_PATH PROJECT_PATH PROJECT_NAME'\
' GHIDRA_PATH is the path to your Ghidra installation'\
' PROJECT_PATH is the path to your JSRF Ghidra project'\
' PROJECT_NAME is the name of your JSRF Ghidra project'\
'Alternatively, the environment variables $GHIDRA_HOME, $JSRFDECOMP_PROJECTPATH,'\
'and $JSRFDECOMP_PROJECTNAME can be set, and the script can be called with no'\
'arguments.'\
''\
'Populates the target/ directory with delinked object files using the address'\
'ranges given in objects.csv.' >& 2
exit 2
}
delink() {
# Invoke headless Ghidra with the delinker script to produce an object file
# $1: Ghidra installation path
# $2: Ghidra project path
# $3: Ghidra project name
# $4: Whitespace-separated address ranges to include in object
# $5: Output path (inside decompile/target/)
# Call the right script depending on whether we're on Windows
# (this seems like the most reliable method I can find for POSIX sh)
if [ -n "${WINDIR-}" ]; then suffix=.bat
else suffix=
fi
# POSIX compatibility layers for Windows (like in git bash) will
# interpret arguments like /exporter as filepaths and attempt to
# convert them to Windows paths, breaking them; this variable disables
# that
export MSYS_NO_PATHCONV=1
"$1/support/analyzeHeadless$suffix" "$2" "$3"\
-readOnly\
-process default.xbe\
-noanalysis\
-scriptPath ghidra_scripts\
-preScript MSVC7Mangle.java $4\
-postScript DelinkProgram.java\
/exporter 'COFF relocatable object'\
$(printf "/include-range %s " $4)\
/export "../decompile/target/$5"
}
main "$@"