-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetObjNames.py
82 lines (68 loc) · 1.97 KB
/
SetObjNames.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#Set ObjectFileStruct's label to the object's name.
#@author Rena
#@category StarFox
#@keybinding
#@menupath
#@toolbar
import jarray
AF = currentProgram.getAddressFactory()
listing = currentProgram.getListing()
mem = currentProgram.getMemory()
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 = 0, 0
def fix(addr):
global numFixed
global numFailed
if addrToInt(addr) == 0: return
#print("Fixing", addr)
try:
listing.clearCodeUnits(addr, addr.add(sLen), False)
listing.createData(addr, struc)
# get name
data = jarray.zeros(13, "b")
mem.getBytes(addr.add(0x91), data)
name = ""
for i, b in enumerate(data):
if b == 0: break
if b < 0 or b > 255:
print("WTF this name has", b, "in it at", i)
#break
if b >= 0x20 and b <= 0x7E:
name += chr(b)
if name != "":
createLabel(addr, "ObjFileStruct_%s" % name, True)
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()
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))
print("Fixed %d, failed to fix %d" % (numFixed, numFailed))