forked from grayhatacademy/ghidra_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenameVariables.py
48 lines (35 loc) · 1.46 KB
/
RenameVariables.py
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
44
45
46
47
48
# Rename saved stack variables in MIPS programs.
#@author fuzzywalls
#@category TNS
#@keybinding
#@menupath TNS.Rename Variables
from utils import utils
from ghidra.program.model.symbol import SourceType
utils.allowed_processors(currentProgram, 'MIPS')
func_man = currentProgram.getFunctionManager()
code_man = currentProgram.getCodeManager()
func_list = func_man.getFunctions(True)
for func in func_list:
# Order of this list is important.
savable_registers = ['gp', 's0', 's1', 's2',
's3', 's4', 's5', 's6', 's7', 's8', 'ra']
variables = func.getAllVariables()
for var in variables:
if len(savable_registers) == 0:
break
symbol = var.getSymbol()
if not symbol:
continue
references = symbol.getReferences()
for ref in references:
ins = code_man.getInstructionAt(ref.getFromAddress())
if 'sw ' in str(ins) or 'sd ' in str(ins):
saved_register = ins.getRegister(0)
if saved_register:
saved_register = saved_register.toString()
if saved_register in savable_registers:
var.setName('saved_%s' % saved_register,
SourceType.USER_DEFINED)
# Remove the saved register to avoid renaming registers
# saved later.
savable_registers.remove(saved_register)