Repository navigation
Expand file tree
/
Copy pathAnnotate.java
More file actions
382 lines (361 loc) · 16.4 KB
/
Copy pathAnnotate.java
File metadata and controls
382 lines (361 loc) · 16.4 KB
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
// Annotates the payload: defines the data types of a schema, then applies names, comments, data
// types and function signatures from tables of them.
//
// Annotate.java <types.tsv> <names.tsv>...
//
// The schema is what `ghidragen types` writes from the Zig definitions; src/tools/ghidragen/types.zig
// describes its rows. Every type lands in the /StarLancer category and replaces an earlier
// definition of the same name, so running this again brings the project up to date, and data and
// functions typed with one keep it.
//
// A names table has rows of address, kind, name, type and comment, tab-separated; type and comment
// may be empty. A `function` row names the function at its address, creating it if need be. Its
// type is either the name of a function type from the schema or a signature in C without the
// function's name, such as `void __fastcall (VmThread *thread)`, with `...` last for a variadic
// one; Ghidra adds the `this` of a `__thiscall` itself, so such a signature leaves it out. A
// `data` row labels its address, and its type, such as `VmThread *` or `ConditionDescriptor[35]`,
// replaces whatever data was defined there. The comment becomes a function's plate comment or the
// data's pre comment. Lines starting with '#', blank lines and addresses the program does not
// contain are skipped, so one table can cover a group holding several programs.
//
// A type string is a type's name, then `*` and `[n]` decorations read as C reads them. Names are
// looked up in /StarLancer first, then among Ghidra's built-in types. Names are applied as
// user-defined. Re-running is harmless.
//
//@category StarLancer
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import ghidra.app.cmd.function.ApplyFunctionSignatureCmd;
import ghidra.app.cmd.function.CreateFunctionCmd;
import ghidra.app.cmd.function.FunctionRenameOption;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.ArrayDataType;
import ghidra.program.model.data.BuiltInDataTypeManager;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeConflictHandler;
import ghidra.program.model.data.DataTypeManager;
import ghidra.program.model.data.DataUtilities;
import ghidra.program.model.data.EnumDataType;
import ghidra.program.model.data.FunctionDefinition;
import ghidra.program.model.data.FunctionDefinitionDataType;
import ghidra.program.model.data.ParameterDefinition;
import ghidra.program.model.data.ParameterDefinitionImpl;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.data.StructureDataType;
import ghidra.program.model.data.UnionDataType;
import ghidra.program.model.listing.CommentType;
import ghidra.program.model.listing.Function;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
public class Annotate extends GhidraScript {
private static final CategoryPath CATEGORY = new CategoryPath("/StarLancer");
private static final String[] CONVENTIONS = { "__cdecl", "__stdcall", "__fastcall", "__thiscall" };
private DataTypeManager dtm;
private int types;
private int functions;
private int labels;
private int typed;
private int skipped;
private int failed;
@Override
protected void run() throws Exception {
String[] args = getScriptArgs();
if (args.length < 1) {
println("usage: Annotate.java <types.tsv> <names.tsv>...");
return;
}
dtm = currentProgram.getDataTypeManager();
defineTypes(Path.of(args[0]));
for (int i = 1; i < args.length; i++) {
applyNames(Path.of(args[i]));
}
println(String.format(
"%s: %d types defined; %d functions and %d labels named, %d of them typed; " +
"%d rows not in this program, %d failed",
currentProgram.getName(), types, functions, labels, typed, skipped, failed));
}
// --- the schema -----------------------------------------------------------------------------
/** One definition of the schema: its header row's fields, then its member rows' fields. */
private record Definition(String[] header, List<String[]> rows) {
String kind() { return header[0]; }
String name() { return header[1]; }
}
private void defineTypes(Path schema) throws Exception {
Map<String, Definition> definitions = new LinkedHashMap<>();
for (String line : Files.readAllLines(schema)) {
if (line.isBlank() || line.startsWith("#")) {
continue;
}
String[] fields = line.split("\t", -1);
switch (fields[0]) {
case "struct", "union", "enum", "function" ->
definitions.put(fields[1], new Definition(fields, new ArrayList<>()));
case "field", "bits", "member", "value" -> {
Definition owner = definitions.get(fields[1]);
if (owner == null) {
throw new IllegalArgumentException("row before its type: " + line);
}
owner.rows().add(fields);
}
default -> throw new IllegalArgumentException("unknown row: " + line);
}
}
// Structures and unions first as empty shells of the right size, so that anything may
// refer to them; then what refers only by pointer; structures last, since they may hold
// the others by value.
for (Definition d : definitions.values()) {
switch (d.kind()) {
case "struct" -> add(new StructureDataType(CATEGORY, d.name(), Integer.parseInt(d.header()[2]), dtm));
case "union" -> add(new UnionDataType(CATEGORY, d.name(), dtm));
default -> { }
}
}
for (Definition d : definitions.values()) {
if (d.kind().equals("enum")) {
EnumDataType e = new EnumDataType(CATEGORY, d.name(), Integer.parseInt(d.header()[2]), dtm);
for (String[] row : d.rows()) {
e.add(row[2], Long.parseLong(row[3]));
}
add(e);
}
}
for (Definition d : definitions.values()) {
if (d.kind().equals("function")) {
add(parseSignature(d.name(), d.header()[2]));
}
}
for (Definition d : definitions.values()) {
if (d.kind().equals("union")) {
UnionDataType u = new UnionDataType(CATEGORY, d.name(), dtm);
for (String[] row : d.rows()) {
DataType member = parseType(row[3]);
u.add(member, member.getLength(), row[2], null);
}
expectLength(u, d);
add(u);
}
}
for (Definition d : definitions.values()) {
if (d.kind().equals("struct")) {
StructureDataType s = new StructureDataType(CATEGORY, d.name(), Integer.parseInt(d.header()[2]), dtm);
for (String[] row : d.rows()) {
if (row[0].equals("field")) {
DataType field = parseType(row[4]);
s.replaceAtOffset(Integer.parseInt(row[2]), field, field.getLength(), row[3], null);
} else {
s.insertBitFieldAt(Integer.parseInt(row[2]), Integer.parseInt(row[3]),
Integer.parseInt(row[4]), parseType(row[7]), Integer.parseInt(row[5]), row[6], null);
}
}
expectLength(s, d);
add(s);
}
}
types = definitions.size();
}
private DataType add(DataType dt) {
return dtm.addDataType(dt, DataTypeConflictHandler.REPLACE_HANDLER);
}
private static void expectLength(DataType dt, Definition d) {
int expected = Integer.parseInt(d.header()[2]);
if (dt.getLength() != expected) {
throw new IllegalStateException(String.format("%s: %d bytes in Ghidra, %d in the schema",
d.name(), dt.getLength(), expected));
}
}
// --- type strings and signatures ------------------------------------------------------------
private DataType lookup(String name) {
DataType dt = dtm.getDataType(CATEGORY, name);
if (dt != null) {
return dt;
}
dt = BuiltInDataTypeManager.getDataTypeManager().getDataType(CategoryPath.ROOT, name);
if (dt != null) {
return dt.clone(dtm);
}
throw new IllegalArgumentException("unknown type " + name);
}
/** A type string: a name, then `*` and `[n]`, with a run of dimensions outermost first. */
private DataType parseType(String text) {
text = text.trim();
int end = 0;
while (end < text.length() && Character.isJavaIdentifierPart(text.charAt(end))) {
end++;
}
DataType dt = lookup(text.substring(0, end));
List<Integer> dimensions = new ArrayList<>();
for (int i = end; i < text.length(); i++) {
char c = text.charAt(i);
if (c == ' ') {
continue;
}
if (c == '*') {
dt = new PointerDataType(array(dt, dimensions), dtm);
dimensions.clear();
} else if (c == '[') {
int close = text.indexOf(']', i);
dimensions.add(Integer.parseInt(text.substring(i + 1, close).trim()));
i = close;
} else {
throw new IllegalArgumentException("bad type string: " + text);
}
}
return array(dt, dimensions);
}
private DataType array(DataType element, List<Integer> dimensions) {
DataType dt = element;
for (int k = dimensions.size() - 1; k >= 0; k--) {
dt = new ArrayDataType(dt, dimensions.get(k), dt.getLength(), dtm);
}
return dt;
}
/**
* `<return type> [<convention>] (<type> <name>, ...)`, which gets `name`. A last parameter of
* `...` makes the function variadic.
*/
private FunctionDefinitionDataType parseSignature(String name, String text) throws Exception {
int open = text.indexOf('(');
int close = text.lastIndexOf(')');
if (open < 0 || close < open) {
throw new IllegalArgumentException("bad signature: " + text);
}
String head = text.substring(0, open).trim();
String convention = null;
for (String c : CONVENTIONS) {
if (head.endsWith(c)) {
convention = c;
head = head.substring(0, head.length() - c.length()).trim();
}
}
List<ParameterDefinition> parameters = new ArrayList<>();
String list = text.substring(open + 1, close).trim();
boolean variadic = list.endsWith("...");
if (variadic) {
list = list.substring(0, list.length() - 3).trim();
if (list.endsWith(",")) {
list = list.substring(0, list.length() - 1).trim();
}
}
if (!list.isEmpty() && !list.equals("void")) {
for (String parameter : list.split(",")) {
parameter = parameter.trim();
int split = parameter.length();
while (split > 0 && Character.isJavaIdentifierPart(parameter.charAt(split - 1))) {
split--;
}
parameters.add(new ParameterDefinitionImpl(parameter.substring(split),
parseType(parameter.substring(0, split)), null));
}
}
FunctionDefinitionDataType definition = new FunctionDefinitionDataType(CATEGORY, name, dtm);
definition.setReturnType(parseType(head));
definition.setArguments(parameters.toArray(new ParameterDefinition[0]));
definition.setVarArgs(variadic);
if (convention != null) {
definition.setCallingConvention(convention);
}
return definition;
}
// --- names tables ---------------------------------------------------------------------------
private void applyNames(Path table) throws Exception {
for (String line : Files.readAllLines(table)) {
monitor.checkCancelled();
// The hand tables start with a header row, which GitHub shows as the table's header.
if (line.isBlank() || line.startsWith("#") || line.startsWith("address\t")) {
continue;
}
String[] fields = line.split("\t", -1);
if (fields.length < 3) {
println("malformed row in " + table + ": " + line);
failed++;
continue;
}
Address address = toAddr(Long.parseLong(fields[0].trim(), 16));
if (!currentProgram.getMemory().contains(address)) {
skipped++;
continue;
}
String kind = fields[1].trim();
String name = fields[2].trim();
String type = fields.length > 3 ? fields[3].trim() : "";
String comment = fields.length > 4 ? fields[4].trim() : "";
try {
switch (kind) {
case "function" -> nameFunction(address, name, type, comment);
case "data" -> labelData(address, name, type, comment);
default -> {
println("unknown kind '" + kind + "' in " + table + ": " + line);
failed++;
}
}
} catch (Exception e) {
println("could not apply " + name + " at " + address + ": " + e.getMessage());
failed++;
}
}
}
private void nameFunction(Address address, String name, String type, String comment) throws Exception {
// Code only the tables reach was never disassembled, and a function made over undefined
// bytes gets no body.
if (getInstructionAt(address) == null && !disassemble(address)) {
throw new Exception("no code at the address");
}
Function function = getFunctionAt(address);
if (function == null) {
function = createFunction(address, name);
if (function == null) {
throw new Exception("no function could be created");
}
} else if (function.getBody().getNumAddresses() <= 1) {
CreateFunctionCmd.fixupFunctionBody(currentProgram, function, monitor);
}
if (!function.getName().equals(name)) {
function.setName(name, SourceType.USER_DEFINED);
}
if (!type.isEmpty()) {
FunctionDefinition signature = type.contains("(")
? parseSignature(name, type)
: (FunctionDefinition) lookup(type);
ApplyFunctionSignatureCmd command = new ApplyFunctionSignatureCmd(address, signature,
SourceType.USER_DEFINED, false, FunctionRenameOption.NO_CHANGE);
if (!command.applyTo(currentProgram, monitor)) {
throw new Exception("signature not applied: " + command.getStatusMsg());
}
typed++;
}
if (!comment.isEmpty()) {
function.setComment(comment);
}
functions++;
}
private void labelData(Address address, String name, String type, String comment) throws Exception {
if (!type.isEmpty()) {
DataUtilities.createData(currentProgram, address, parseType(type), -1,
DataUtilities.ClearDataMode.CLEAR_ALL_CONFLICT_DATA);
typed++;
}
SymbolTable symbols = currentProgram.getSymbolTable();
Symbol existing = symbols.getGlobalSymbol(name, address);
Symbol symbol = existing != null ? existing : symbols.createLabel(address, name, SourceType.USER_DEFINED);
if (!symbol.isPrimary()) {
symbol.setPrimary();
}
// The table is the authority on the addresses it lists: a name it no longer gives goes.
for (Symbol other : symbols.getSymbols(address)) {
if (!other.equals(symbol) && other.getSource() == SourceType.USER_DEFINED) {
other.delete();
}
}
if (!comment.isEmpty()) {
currentProgram.getListing().setComment(address, CommentType.PRE, comment);
}
labels++;
}
}