g_game/g_manager typed: 1483 g_manager-> and 834 g_game-> member accesses now visible (field_0x 10636 -> 14385 counts them); named members 81k -> 91k, raw in_ECX + 0x 18425 -> 16225. ApplyGlobals takes array types (char[5]). apply_db.py used to truncate all.c before computing the new text, so a killed build left it empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
56 lines
2.8 KiB
Java
56 lines
2.8 KiB
Java
// Applies db/globals.tsv (address, name, C type, evidence): labels the global and lays down its type, so a Game* global
|
|
// makes every access read g_game->_field. Types: primitives, a struct name, pointers to them ("Game*"), or an array ("char[5]"). Arg: globals.tsv
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.program.model.address.Address;
|
|
import ghidra.program.model.data.*;
|
|
import ghidra.program.model.symbol.SourceType;
|
|
import java.nio.file.*;
|
|
|
|
public class ApplyGlobals extends GhidraScript {
|
|
DataType type(DataTypeManager dtm, String t) {
|
|
t = t.replace("const ", "").trim();
|
|
int count = 1, ptr = 0;
|
|
if (t.endsWith("]")) {
|
|
count = Integer.decode(t.substring(t.indexOf('[') + 1, t.length() - 1));
|
|
t = t.substring(0, t.indexOf('[')).trim();
|
|
}
|
|
while (t.endsWith("*")) { ptr++; t = t.substring(0, t.length() - 1).trim(); }
|
|
DataType b;
|
|
switch (t) {
|
|
case "void": b = VoidDataType.dataType; break;
|
|
case "bool": case "char": case "uint8_t": case "int8_t": b = ByteDataType.dataType; break;
|
|
case "short": case "uint16_t": case "int16_t": b = ShortDataType.dataType; break;
|
|
case "int": case "int32_t": b = IntegerDataType.dataType; break;
|
|
case "uint32_t": case "unsigned int": b = UnsignedIntegerDataType.dataType; break;
|
|
case "float": b = FloatDataType.dataType; break;
|
|
case "double": b = DoubleDataType.dataType; break;
|
|
case "uint64_t": case "int64_t": b = QWordDataType.dataType; break;
|
|
default: b = dtm.getDataType(new CategoryPath("/"), t);
|
|
}
|
|
if (b == null) b = ptr > 0 ? VoidDataType.dataType : null;
|
|
if (b == null) return null;
|
|
for (int i = 0; i < ptr; i++) b = new PointerDataType(b);
|
|
return count > 1 ? new ArrayDataType(b, count, b.getLength()) : b;
|
|
}
|
|
|
|
public void run() throws Exception {
|
|
DataTypeManager dtm = currentProgram.getDataTypeManager();
|
|
int n = 0, typed = 0;
|
|
for (String line : Files.readAllLines(Paths.get(getScriptArgs()[0]))) {
|
|
if (line.isBlank() || line.startsWith("#")) continue;
|
|
String[] c = line.split("\t");
|
|
Address a = toAddr(c[0].replace("0x", ""));
|
|
createLabel(a, c[1], true, SourceType.USER_DEFINED);
|
|
n++;
|
|
DataType t = c.length > 2 ? type(dtm, c[2]) : null;
|
|
if (t == null || t instanceof VoidDataType) continue;
|
|
try {
|
|
clearListing(a, a.add(t.getLength() - 1));
|
|
createData(a, t);
|
|
typed++;
|
|
} catch (Exception e) { println("ApplyGlobals: cannot type " + c[0] + " as " + c[2] + ": " + e.getMessage()); }
|
|
}
|
|
println("ApplyGlobals: " + n + " globals, " + typed + " typed");
|
|
}
|
|
}
|