forked from trevor403/xd-asm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
232 lines (184 loc) · 8.13 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
def prompt(local, scope):
import code
import readline
import rlcompleter
vars = globals()
vars.update(local)
vars.update(scope)
readline.set_completer(rlcompleter.Completer(vars).complete)
readline.parse_and_bind("tab: complete")
code.InteractiveConsole(vars).interact()
scope = dict()
def main(override):
import json
import sys
out = []
if override:
dprint = out.append
else:
dprint = eval("print")
# def printer(*args):
# # xprint = eval("print")
# xprint(" ".join(map(str,args)))
# # print("/home/trevor/wk/ghidra-export/code")
# dprint = printer
funcMgr = currentProgram.getFunctionManager()
funcAddr = currentProgram.getAddressFactory().getAddress("80003100")
# funcAddr = getState().getCurrentAddress().getAddress("80158da4")
# funcAddr = getState().getCurrentAddress().getAddress("8027dbb4")
funcs = funcMgr.getFunctions(funcAddr, True)
# funcs = funcMgr.getFunctions(True)
# # debug
# prompt(locals(), {})
# return
opts = ghidra.program.model.listing.CodeUnitFormatOptions
opt = ghidra.program.model.listing.CodeUnitFormatOptions(opts.ShowBlockName.NEVER,opts.ShowNamespace.ALWAYS,"",True,True,True,True,True,True,True)
codeUnitFormat = ghidra.program.model.listing.CodeUnitFormat(opt)
optASM = ghidra.program.model.listing.CodeUnitFormatOptions(opts.ShowBlockName.NEVER,opts.ShowNamespace.ALWAYS,"",False,False,False,False,True,True,True)
codeUnitFormatASM = ghidra.program.model.listing.CodeUnitFormat(optASM)
for func_index, func in enumerate(funcs):
# if func_index >= 1: break
if override:
dprint = out.append
else:
dprint = eval("print")
sig = str(func.getSignature())
name = func.getName()
body = func.getBody()
insts = currentProgram.getListing().getInstructions(body, True)
# # debug
# if name not in [
# "OSGetGbsMode",
# "OSSetGbsMode",
# ]: continue
sys.stdout.write(name+"\n")
bodies = list(body)
main_body = bodies[0]
start_address_raw = int(main_body.minAddress.offset)
end_address_raw = int(main_body.maxAddress.offset)
start_address = hex(start_address_raw).replace("L", "")
end_address = hex(end_address_raw).replace("L", "")
range_repr = start_address + " - " + end_address
## TODO exclude MetroTRK area
if start_address_raw >= 0x800d39d0 and end_address_raw < 0x800d9c2c:
dprint("# SKIPPING MetroTRK {} at {}".format(name, hex(body.minAddress.offset)))
continue
ranges = [[a.minAddress.offset, a.maxAddress.offset] for a in body]
if len(ranges) != 1:
dprint("# SKIPPING RAW {} at {}".format(name, hex(body.minAddress.offset)))
dprint("# "+repr([[hex(addr).replace("L", "") for addr in r] for r in ranges]))
continue # skip this function
# start_address[2:]
filename = "func_{}.s".format(name)
output = "/home/trevor/wk/ghidra-export/code/" + filename
f = open(output, 'w')
if override:
xprint = lambda s: f.write(s)
else:
xprint = lambda s: f.write(s) + sys.stdout.write(s)
dprint = lambda *args: xprint(" ".join(map(str,args))+"\n")
# def printer(*args):
# line = " ".join(map(str,args))
# sys.stdout.write(line+"\n")
# print(f.write(line+"\n"))
# dprint = printer
metadata = {
"name": name,
"inst": int(main_body.length / 4),
"size": main_body.length,
"startAddress": start_address,
"endAddress": end_address,
}
dprint("# metadata: " + json.dumps(metadata))
dprint('\n#include "def.h"')
dprint("\n### Function: %s\n.global %s" % (sig, name))
for i, inst in enumerate(insts):
# if i < 14: continue
rep = codeUnitFormatASM.getRepresentationString(inst)
# print(rep)
lab = inst.getLabel()
if lab:
namespace = str(inst.getSymbols()[0].getParentNamespace())
if namespace != "Global":
lab = namespace+"::"+lab
line = lab+":"
if i == 0: line += "\t# "+range_repr
dprint(line.replace("::", "_X_"))
comments = []
ops = []
for p in range(inst.getNumOperands()):
op = codeUnitFormat.getOperandRepresentationList(inst, p)
opASM = codeUnitFormatASM.getOperandRepresentationList(inst, p)
ops.append(opASM)
if 'OperandRepresentationList' in repr(type(op)):
for k in range(len(op)):
current = op[k]
if 'OperandRepresentationList' in repr(type(current)):
if 'offset' in str(current[0]):
comments.append(str(current))
rep = str(inst)
if 'local' in repr(current[-1]):
comments.append("stack")
else:
comments.append("op %d: %s" % (p, current[-1]))
elif 'VariableOffset' in repr(type(current)):
if 'local' in repr(current):
comments.append("stack")
else:
comments.append("op %d: %s" % (p, current))
elif 'LabelString' in repr(type(current)):
lab = current.toString()
if lab.startswith("DAT_0"):
fixed = hex(int(lab.replace("DAT_", ''),16))
rep = rep.replace(lab, fixed)
# if inst.getNumOperands() > 0:
# mnemonic = codeUnitFormatASM.getMnemonicRepresentation(inst)
# rep = mnemonic+"\t\t"+rep[len(mnemonic)+1:]
mnemonic = codeUnitFormatASM.getMnemonicRepresentation(inst)
if mnemonic == "or" and len(set([str(o) for o in ops[1:]])) == 1:
rep = "mr {},{}".format(ops[0], ops[1])
elif mnemonic == "psq_l" or mnemonic == "psq_st" or mnemonic == 'psq_stu':
rep += "_INDEX"
displayableEol = ghidra.app.util.DisplayableEol(inst, False, False, False, True, 6, True, True)
displays = [o for o in displayableEol.getComments()]
if len(displays) > 0 and not (mnemonic in ['bl', 'blrl'] and 'undefined' in displays[0]):
index = 0
for display in displays:
if display == '= ??': continue
comments.insert(index, display)
index += 1
# comment.append(display)
asm = " "+rep
if len(comments) > 0:
asm += "\t# "
asm += ", ".join(comments)
dprint(asm.replace("::", "_X_"))
# function exports DONE
# labels DONE
# xrefs
# operand markups DONE
# value estimate DONE
# if i >= 8: break
f.close()
# break ## debug only
if not override:
# global scope
scope = locals()
return out
import ghidra_bridge
b = ghidra_bridge.GhidraBridge(namespace=globals(), response_timeout=600)
# print(getState().getCurrentAddress().getOffset())
# instrAddr = getState().getCurrentAddress().getAddress("800033cc")
# inst = currentProgram.getListing().getInstructionAt(instrAddr)
remote = False # default
remote = True # enable remote exec
if remote:
print("REMOTE EXEC")
remote_main = b.remoteify(main)
out = remote_main(True)
for o in out: print(o)
b.remote_shutdown()
else:
print("LOCAL EXEC")
main(False)
# prompt(locals(), scope)