-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymbols_to_ghidra.pl
392 lines (318 loc) · 12.2 KB
/
symbols_to_ghidra.pl
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
#!/usr/bin/perl
# this script generates a Ghidra script to create globals and vmethods, apply their types, etc.
# see readme for invocation details
use strict;
use warnings;
my $symbolsname = $ARGV[0]; #'v0.23.130.23a';
my $input1 = $ARGV[1]; #'23a/codegen.out.xml';
my $input2 = $ARGV[2]; #'23a/symbols.xml';
use XML::LibXML;
my @lines_full;
our @lines;
my %seen_class;
my %fwd_decl_class;
my %global_types;
my %global_types_orig;
my %global_objects;
my @calls;
sub emit_label {
my ($name, $addr, $type) = @_;
# print "$name @ $addr -> $type\n";
print 'createGlobal(toAddr('.$addr.'L), "'.$name.'", "'.$type.'");', "\n";
}
sub emit_vmethod {
my ($addr, $pos, $class, $name, $type, $basevtaddr) = @_;
my $k = $basevtaddr && "toAddr(${basevtaddr}L)" || 'null';
# print "$pos: $name -> $type\n";
print "applyVmethodType(toAddr(${addr}L), $pos, \"$class\", \"$name\", \"$type\", $k);\n";
}
sub get_vt_name {
my ($typename) = @_;
my $type = $global_types_orig{$typename};
if (not $type) {
if ($typename =~ /^renderer_/) {
return 'renderer';
}
return;
}
return if not $type;
my $vt;
while (not $vt = $type->findnodes('child::virtual-methods')->[0]) {
my $parentname = $type->getAttribute('inherits-from');
if (not $parentname) {
last;
}
$type = $global_types{$parentname};
}
return $type->getAttribute('original-name') || $type->getAttribute('type-name');
}
sub get_full_vtable {
my ($name, $vtname) = @_;
my @vms_all;
my $type = $global_types_orig{$vtname};
if ($type) {
my $base = $type->getAttribute('inherits-from');
if ($base) {
my $basetype = $global_types{$base};
my $basevms = $basetype->findnodes('child::virtual-methods')->[0];
push @vms_all, $basevms->findnodes('child::vmethod');
}
my $vms = $type->findnodes('child::virtual-methods')->[0];
push @vms_all, $vms->findnodes('child::vmethod');
}
if ($name ne $vtname) {
my $self = $global_types_orig{$name};
if ($self) {
my $selfvms = $self->findnodes('child::virtual-methods')->[0];
if ($selfvms) {
push @vms_all, $selfvms->findnodes('child::vmethod');
}
}
}
return @vms_all
}
sub process_globals {
my ($symbols) = @_;
my @globals = $symbols->findnodes('global-address');
for my $n (@globals) {
my $name = $n->getAttribute('name');
my $addr = $n->getAttribute('value');
my $globaldef = $global_objects{$name};
my $typename = '';
if ($globaldef) {
$typename = $globaldef->getAttribute('type-name') || '';
if ($typename) {
my $type = $global_types{$typename};
if ($type and $type->getAttribute('original-name')) {
$typename = $type->getAttribute('original-name');
}
}
}
emit_label('_'.$name, $addr, $typename);
}
}
sub process_vtables {
my ($symbols) = @_;
my @vtables = $symbols->findnodes('vtable-address');
for my $n (@vtables) {
my $typename = $n->getAttribute('name');
my $name = '_vtable_'.$typename;
my $addr = $n->getAttribute('value');
next if not $addr;
my $vtname = get_vt_name($typename);
if ($vtname) {
emit_label($name, $addr, 'vtable_'.$vtname);
} else {
emit_label($name, $addr, '');
}
}
}
sub process_vmethods {
my ($symbols) = @_;
my @vtables = $symbols->findnodes('vtable-address');
for my $n (@vtables) {
my $typename = $n->getAttribute('name');
my $name = '_vtable_'.$typename;
my $addr = $n->getAttribute('value');
next if not $addr;
my $vtname = get_vt_name($typename);
if ($vtname) {
my $type = $global_types_orig{$typename};
my $basevtaddr;
# print $type;
if ($type) {
while (1) {
my $base = $type->getAttribute('inherits-from');
if ($base) {
my $basetype = $global_types{$base};
my $basename = $basetype->getAttribute('original-name') || $base;
my $basevt = $symbols->findnodes('vtable-address[@name="'.$basename.'"]')->[0];
if ($basevt) {
$basevtaddr = $basevt->getAttribute('value');
last;
} else {
$type = $basetype;
}
} else {
last;
}
}
}
my $k = $basevtaddr && "toAddr(${basevtaddr}L)" || 'null';
print "applyVmethodTypes(toAddr(${addr}L), \"$typename\", \"vtable_$vtname\", $k);\n";
# my @vms = get_full_vtable($typename, $vtname);
# my $pos = 0;
# for my $vm (@vms) {
# if ($vm->getAttribute('is-destructor')) {
# #todo: this
# } else {
# my $vmname = $vm->getAttribute('name') || $vm->getAttribute('ld:anon-name');
# emit_vmethod($addr, $pos, $typename, $typename.'_'.$vmname, 'vm_'.$vtname.'_'.$vmname, $basevtaddr);
# }
# $pos++;
# }
}
}
}
my $parser = XML::LibXML->new();
$parser->set_options({line_numbers=>1});
my $doc1 = $parser->parse_file($input1);
$global_types{$_->getAttribute('type-name')} = $_ foreach $doc1->findnodes('/ld:data-definition/ld:global-type');
$global_types_orig{$_->getAttribute('original-name') || $_->getAttribute('type-name')} = $_ foreach $doc1->findnodes('/ld:data-definition/ld:global-type');
$global_objects{$_->getAttribute('name')} = $_ foreach $doc1->findnodes('/ld:data-definition/ld:global-object');
my $doc2 = $parser->parse_file($input2);
my $symbols = ($doc2->findnodes('/data-definition/symbol-table[@name="'.$symbolsname.'"]'))[-1];
print <<END
import java.util.*;
import ghidra.app.script.GhidraScript;
import ghidra.app.services.DataTypeManagerService;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.address.*;
import ghidra.program.model.symbol.*;
import ghidra.program.model.listing.Function.FunctionUpdateType;
public class df_ghidra extends GhidraScript {
DataTypeManager manager;
Address vmeth_nullptr;
void applyFunctionType(Address addr, String fnName, FunctionDefinition fnDataType, DataType thisType, String className, GhidraClass cls) throws Exception
{
Function fn = getFunctionAt(addr);
if (fn == null)
fn = createFunction(addr, fnName);
else if (fn.getName().startsWith("__"))
return;
fn.setName(fnName, SourceType.USER_DEFINED);
fn.setCallingConvention("__thiscall");
fn.setReturnType(fnDataType.getReturnType(), SourceType.USER_DEFINED);
fn.setParentNamespace(cls);
if (cls != null)
currentProgram.getSymbolTable().createLabel(addr, fnName, cls, SourceType.USER_DEFINED).setPrimary();
List params = new ArrayList();
ParameterDefinition[] typeParams = fnDataType.getArguments();
for (int i = 0; i < typeParams.length; i++)
{
ParameterDefinition p = typeParams[i];
DataType t = "this".equals(p.getName()) ? thisType : p.getDataType();
params.add(new ParameterImpl(p.getName(), t, currentProgram));
}
fn.replaceParameters(params, FunctionUpdateType.DYNAMIC_STORAGE_ALL_PARAMS, true, SourceType.USER_DEFINED);
}
Address vmethodAddress(Address vtableAddr, int pos) throws Exception
{
if (currentProgram.getDefaultPointerSize() == 8)
return toAddr(getLong(vtableAddr.add(pos*8)));
else
return toAddr(getInt(vtableAddr.add(pos*4)));
}
void applyVmethodType(Address vtableAddr, int pos, String className, String fnName, FunctionDefinition fnDataType, Address parentVtableAddr, GhidraClass cls) throws Exception
{
Address addr = vmethodAddress(vtableAddr, pos);
if (addr.equals(vmeth_nullptr))
return;
if (parentVtableAddr != null)
{
Address parentAddr = vmethodAddress(parentVtableAddr, pos);
if (parentAddr.equals(addr))
{
println("skipping "+fnName);
return;
}
}
DataType dataType = manager.getDataType("/codegen.h/"+className);
DataType thisType = new PointerDataType(dataType);
applyFunctionType(addr, fnName, fnDataType, thisType, className, cls);
}
void flattenVtable(Structure vtType, ArrayList<DataTypeComponent> flat)
{
for (int i = 0; i < vtType.getNumComponents(); i++) {
DataTypeComponent dc = vtType.getComponent(i);
if ("super".equals(dc.getFieldName())) {
Structure parent = (Structure) dc.getDataType();
flattenVtable(parent, flat);
} else
flat.add(dc);
}
}
void applyVmethodTypes(Address vtableAddr, String className, String vtTypeName, Address parentVtableAddr) throws Exception
{
println("processing vtable for " + className+ " "+parentVtableAddr);
Data d = getDataAt(vtableAddr);
if (d == null) {
println("vtable problem for "+className);
return;
}
GhidraClass cls = (GhidraClass) currentProgram.getSymbolTable().getNamespace(className, currentProgram.getGlobalNamespace());
if (cls == null)
cls = currentProgram.getSymbolTable().createClass(currentProgram.getGlobalNamespace(), className, SourceType.USER_DEFINED);
Structure vtType = (Structure) manager.getDataType("/codegen.h/"+vtTypeName);
ArrayList<DataTypeComponent> flat = new ArrayList<DataTypeComponent>();
flattenVtable(vtType, flat);
for (int i = 0; i < flat.size(); i++) {
DataTypeComponent dc = flat.get(i);
String fnName = className + "_" + dc.getFieldName();
DataType fieldType = dc.getDataType();
if (fieldType instanceof TypeDef)
{
Pointer ptrDataType = (Pointer) ((TypeDef)fieldType).getBaseDataType();
FunctionDefinition fnDataType = (FunctionDefinition) ptrDataType.getDataType();
applyVmethodType(vtableAddr, i, className, fnName, fnDataType, parentVtableAddr, cls);
}
}
}
void createVtable(Address vtableAddr, String className) throws Exception
{
DataType dataType = manager.getDataType("/codegen.h/vtable_"+className);
clearListing(vtableAddr, vtableAddr.add(dataType.getLength()-1));
createData(vtableAddr, dataType);
createLabel(vtableAddr, "_vtable_"+className, true, SourceType.USER_DEFINED);
}
void createGlobal(Address addr, String name, String typeName) throws Exception
{
if (typeName != null && !typeName.equals(""))
{
DataType dataType = getDataTypes(typeName)[0];//manager.getDataType("/codegen.h/"+typeName);
clearListing(addr, addr.add(dataType.getLength()-1));
createData(addr, dataType);
}
createLabel(addr, name, true, SourceType.USER_DEFINED);
}
END
;
print <<END
void globals() throws Exception {
END
;
process_globals($symbols);
print "}\n";
push @calls, "globals";
print <<END
void vtables() throws Exception {
END
;
process_vtables($symbols);
print "}\n";
push @calls, "vtables";
print <<END
void vmethods() throws Exception {
END
;
process_vmethods($symbols);
print "}\n";
push @calls, "vmethods";
print <<END
protected void run() throws Exception {
manager = currentProgram.getDataTypeManager();
Function fn = getFunction("_guard_check_icall");
vmeth_nullptr = (fn != null) ? fn.getEntryPoint() : toAddr(0);
//406c10
END
;
for my $call (@calls) {
print "$call();\n";
}
print <<END
}
}
END
;