this_types: also place unnamed FUN_ functions in a class when their ECX offsets match one class's field starts unambiguously
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0b7ee2703d
commit
1d43433e1b
2 files changed
+24
-1
No files matched your search
@@ -111,8 +111,14 @@ public class ApplyTypes extends GhidraScript {
|
|||||||
// methods whose ECX offsets fit their class (scripts/this_types.py) become __thiscall, typing `this`
|
// methods whose ECX offsets fit their class (scripts/this_types.py) become __thiscall, typing `this`
|
||||||
int fitted = 0;
|
int fitted = 0;
|
||||||
if (getScriptArgs().length > 1) for (String l : Files.readAllLines(Paths.get(getScriptArgs()[1]))) {
|
if (getScriptArgs().length > 1) for (String l : Files.readAllLines(Paths.get(getScriptArgs()[1]))) {
|
||||||
Function f = getFunctionAt(toAddr(l.split("\t")[0]));
|
String[] c = l.split("\t");
|
||||||
|
Function f = getFunctionAt(toAddr(c[0]));
|
||||||
if (f == null || "__thiscall".equals(f.getCallingConventionName())) continue;
|
if (f == null || "__thiscall".equals(f.getCallingConventionName())) continue;
|
||||||
|
if (f.getName().startsWith("FUN_")) { // unnamed, placed by its ECX offsets: move it into the class
|
||||||
|
Namespace cn = st.getNamespace(c[1], global);
|
||||||
|
if (cn == null) continue;
|
||||||
|
try { f.setParentNamespace(cn); } catch (Exception e) { continue; }
|
||||||
|
}
|
||||||
Namespace ns = f.getParentNamespace();
|
Namespace ns = f.getParentNamespace();
|
||||||
if (!(ns instanceof GhidraClass)) try { st.convertNamespaceToClass(ns); } catch (Exception e) { continue; }
|
if (!(ns instanceof GhidraClass)) try { st.convertNamespaceToClass(ns); } catch (Exception e) { continue; }
|
||||||
f.setCallingConvention("__thiscall");
|
f.setCallingConvention("__thiscall");
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
# Which named methods (Class::method, from Lua, db or ZHL without a declaration) really get a Class object in ECX:
|
# Which named methods (Class::method, from Lua, db or ZHL without a declaration) really get a Class object in ECX:
|
||||||
# at least 60% of the distinct offsets they read from in_ECX must fall inside Class's confirmed fields (5+ known).
|
# at least 60% of the distinct offsets they read from in_ECX must fall inside Class's confirmed fields (5+ known).
|
||||||
# ApplyTypes.java then makes them __thiscall, so `this` is typed. Lua aliases: Sprite = ANM2, Color = ColorMod.
|
# ApplyTypes.java then makes them __thiscall, so `this` is typed. Lua aliases: Sprite = ANM2, Color = ColorMod.
|
||||||
|
# Unnamed FUN_ functions are placed in a class too when 75%+ of their ECX offsets are exact field starts of one class
|
||||||
|
# (the most general one, if a base and subclass tie) and it leads every unrelated class by 2+ offsets.
|
||||||
# Usage: this_types.py all.c merged_fields.tsv out.tsv (out: address \t class \t hits/offsets \t name)
|
# Usage: this_types.py all.c merged_fields.tsv out.tsv (out: address \t class \t hits/offsets \t name)
|
||||||
import collections, re, sys
|
import collections, re, sys
|
||||||
src, fields_tsv, out = sys.argv[1:4]
|
src, fields_tsv, out = sys.argv[1:4]
|
||||||
@@ -20,8 +22,23 @@ def known(cls):
|
|||||||
r = []
|
r = []
|
||||||
while cls: r += spans.get(cls, []); cls = base.get(cls)
|
while cls: r += spans.get(cls, []); cls = base.get(cls)
|
||||||
return r
|
return r
|
||||||
|
def chain(c):
|
||||||
|
r = []
|
||||||
|
while c: r.append(c); c = base.get(c)
|
||||||
|
return r
|
||||||
|
starts = {c: {a for a, _ in known(c)} for c in set(spans) | set(base)}
|
||||||
|
starts = {c: s for c, s in starts.items() if len(s) >= 8}
|
||||||
|
def place(offs): # class for an unnamed function, or None
|
||||||
|
sc = {c: len(offs & s) for c, s in starts.items()}
|
||||||
|
best = max(sc.values())
|
||||||
|
top = [c for c, v in sc.items() if v == best]
|
||||||
|
roots = [c for c in top if all(c in chain(g) for g in top)]
|
||||||
|
rest = [v for c, v in sc.items() if not any(c in chain(t) or t in chain(c) for t in top)]
|
||||||
|
return roots[0] if len(offs) >= 4 and best / len(offs) >= 0.75 and roots and best - max(rest or [0]) >= 2 else None
|
||||||
rows, addr, name, offs = [], None, None, set()
|
rows, addr, name, offs = [], None, None, set()
|
||||||
def flush():
|
def flush():
|
||||||
|
if name and name.startswith('FUN_') and offs and (cls := place(offs)):
|
||||||
|
rows.append(f'{addr}\t{cls}\t{len(offs & starts[cls])}/{len(offs)}\t{name}\n'); return
|
||||||
if not (name and '::' in name and offs) or re.match(r'(FUN_|NpcAI|NpcHelper|std::|Catch|Unwind)', name): return
|
if not (name and '::' in name and offs) or re.match(r'(FUN_|NpcAI|NpcHelper|std::|Catch|Unwind)', name): return
|
||||||
ns = name.rsplit('::', 1)[0]; cls = ALIAS.get(ns, ns); k = known(cls)
|
ns = name.rsplit('::', 1)[0]; cls = ALIAS.get(ns, ns); k = known(cls)
|
||||||
hit = sum(1 for o in offs if any(a <= o < a + s for a, s in k))
|
hit = sum(1 for o in offs if any(a <= o < a + s for a, s in k))
|
||||||
|
|||||||
Reference in new issue
Block a user