-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixupStructPtrs.py
68 lines (57 loc) · 1.7 KB
/
FixupStructPtrs.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#Given pointer to struct, change destination type to that struct.
#@author Rena
#@category Struct
#@keybinding
#@menupath
#@toolbar
AF = currentProgram.getAddressFactory()
listing = currentProgram.getListing()
def addrToInt(addr):
return int(str(addr), 16)
def intToAddr(addr):
return AF.getAddress("0x%08X" % addr)
if currentSelection is None:
startAddr = currentAddress
endAddr = currentAddress
else:
startAddr = currentSelection.getMinAddress()
endAddr = currentSelection.getMaxAddress()
data = listing.getDataContaining(startAddr)
if data.isArray():
struc = data.getComponent(0)
else:
struc = data
if not struc.isPointer():
raise "Select a POINTER to a struct!"
struc = struc.dataType.dataType
#struc = currentProgram.getDataTypeManager().getDataType("/auto_structs/FileStruct")
sLen = struc.getLength()
#print("Struct is", struc)
numFixed, numFailed, numSkipped = 0, 0, 0
def fix(addr):
global numFixed
global numFailed
global numSkipped
if addrToInt(addr) == 0:
numSkipped += 1
return
#print("Fixing", addr)
try:
listing.clearCodeUnits(addr, addr.add(sLen), False)
listing.createData(addr, struc)
numFixed += 1
except ghidra.program.model.util.CodeUnitInsertionException:
#print("Failed", addr)
numFailed += 1
#print("Data is:", data)
if data.isArray():
count = data.getLength() / data.getComponent(0).getLength()
#printf("array, length %d\n", count)
for i in range(count):
fix(data.getComponent(i).value)
else:
AF = currentProgram.getAddressFactory()
incr = data.getLength()
for addr in range(addrToInt(startAddr), addrToInt(endAddr), incr):
fix(listing.getDataAt(AF.getAddress(hex(addr)).value))
printf("Fixed %d, failed to fix %d, skipped %d\n", numFixed, numFailed, numSkipped)