db/globals.tsv + ApplyGlobals.java: label and type global data; apply_db.py 1000x faster (token match + dict lookup)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1d43433e1b
commit
2e4322b8a5
5 files changed
+70
-4
No files matched your search
@@ -0,0 +1,51 @@
|
||||
// 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, or pointers to them ("Game*"). 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 ptr = 0;
|
||||
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 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");
|
||||
}
|
||||
}
|
||||
+14
-3
@@ -14,9 +14,12 @@ for l in open(db):
|
||||
# By address everywhere: "//@@ addr name" headers, functions.tsv, and addr:name user lists in strings.tsv/npc_ai.tsv.
|
||||
# Call sites only show the old name, so they are rewritten only where that old name belonged to one function.
|
||||
old2new = {fns[a]: n for a, n in ren.items() if fns[a] != n and count[fns[a]] == 1}
|
||||
calls = re.compile(r'(?<!\w)(?<!::)(' + '|'.join(sorted(map(re.escape, old2new), key=len, reverse=True)) + r')(?![\w:])')
|
||||
users = re.compile(r'(?:(?<=^//@@ )|\b)(' + '|'.join(ren) + r')([: ])[^\s]+', re.M)
|
||||
def fix(s): return users.sub(lambda m: m.group(1) + m.group(2) + ren[m.group(1)], calls.sub(lambda m: old2new[m.group(1)], s) if old2new else s)
|
||||
# (generic token patterns + dict lookups: an alternation of ~1,500 names is far too slow on a 35 MB dump)
|
||||
calls = re.compile(r'(?<![\w:])([A-Za-z_~][\w~]*(?:::[A-Za-z_~][\w~]*)+|FUN_[0-9a-f]{8})(?![\w:])')
|
||||
users = re.compile(r'(?:(?<=^//@@ )|\b)([0-9a-f]{8})([: ])([^\s]+)', re.M)
|
||||
def fix(s):
|
||||
s = calls.sub(lambda m: old2new.get(m.group(1), m.group(1)), s) if old2new else s
|
||||
return users.sub(lambda m: m.group(1) + m.group(2) + ren[m.group(1)] if m.group(1) in ren else m.group(0), s)
|
||||
# db/locals.tsv rows without a type: rename the variable inside its function's block (typed rows need build.sh --db)
|
||||
locals = {}
|
||||
for l in open(db.replace('names.tsv', 'locals.tsv')):
|
||||
@@ -32,6 +35,14 @@ def fix_locals(s):
|
||||
for r, new in subs: body = r.sub(new, body)
|
||||
s = s[:i] + body + s[j:]
|
||||
return s
|
||||
# db/globals.tsv: DAT_<address> -> the global's name
|
||||
gl = {}
|
||||
for l in open(db.replace('names.tsv', 'globals.tsv')):
|
||||
c = l.rstrip('\n').split('\t')
|
||||
if l.strip() and not l.startswith('#'): gl['DAT_%08x' % int(c[0], 16)] = c[1]
|
||||
dat = re.compile(r'\bDAT_[0-9a-f]{8}\b')
|
||||
_fix = fix
|
||||
def fix(s): return dat.sub(lambda m: gl.get(m.group(0), m.group(0)), _fix(s)) if gl else _fix(s)
|
||||
for f in ('all.c', 'strings.tsv', 'npc_ai.tsv'):
|
||||
p = f'{d}/{f}'; s = open(p).read(); open(p, 'w').write(fix_locals(fix(s)) if f == 'all.c' else fix(s))
|
||||
rows = [l.split('\t', 2) for l in open(f'{d}/functions.tsv')]
|
||||
|
||||
Reference in new issue
Block a user