mtk-wifi-fw/tools/ghidra_scripts/FindGPInit.py

29 lines
1.1 KiB
Python
Raw Permalink Normal View History

# Headless post-script: find instructions that write the GP register and
# report candidate GP base values. Once GP is known, set it as register
# context and re-run analysis so GP-relative data references resolve.
# Run via pyghidra ghidra_launch ... -postScript FindGPInit.py
#@category Analysis
listing = currentProgram.getListing()
reg = currentProgram.getLanguage().getRegisters()
gp_regs = [r for r in reg if r.getName().lower() in ('gp', 'r26', 'r11')]
print('GP candidates: %s' % [r.getName() for r in gp_regs])
it = listing.getInstructions(True)
hits = 0
while it.hasNext() and hits < 80:
ins = it.next()
n = ins.getNumOperands()
for i in range(n):
try:
ops = ins.getOpObjects(i)
except Exception: # noqa: BLE001
continue
for o in ops:
if hasattr(o, 'getName') and o in gp_regs:
# operand 0 = destination on NDS32 ALU forms
if i == 0:
print('%s %s' % (ins.getAddress(), ins))
hits += 1
print('total gp-writes shown: %d' % hits)