Skip to content

Instantly share code, notes, and snippets.

@VelocityRa
Last active March 14, 2021 08:27
Show Gist options
  • Save VelocityRa/122ba92556a5bc8ab7ccd819851daf41 to your computer and use it in GitHub Desktop.
Save VelocityRa/122ba92556a5bc8ab7ccd819851daf41 to your computer and use it in GitHub Desktop.
Ghidra script that finds jumptable targets for PS3 jumptables (generated for C 'switch'es) and adds refs to their indirect jump instruction
# Finds jumptable targets for PS3 jumptables (generated for C 'switch'es) and adds
# refs to the indirect jump instruction so that decompilation for the cases is present
# (instead of Ghidra complaining with an error in the switch as it often does).
# Select the block with the address offsets before running (first mark them all as
# addresses with `P` and `[`).
# After you run this you'll probably want to select the indirect jump instruction
# (usually `bctr`) and run the SwitchOverride.java script.
#@author VelocityRa
#@category Repair
#@keybinding
#@menupath
#@toolbar
from ghidra.program.model.symbol import RefType
start = currentSelection.minAddress
instr = getInstructionBefore(start)
print(instr)
for i in range(currentSelection.getNumAddresses() / 4):
data = getDataAt(currentSelection.minAddress).getComponentAt(i*4)
addr = start.addWrapSpace(data.value.offset)
print(addr, data)
data.removeValueReference(data.value)
data.addValueReference(addr, RefType.DATA)
createMemoryReference(instr, 0, addr, RefType.COMPUTED_JUMP)
# createBookmark(addr, "FindJumptableTargetsMine.py", "target for " + str(start))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment