diff --git a/scripts/ApplyTypes.java b/scripts/ApplyTypes.java index a0d68bc..3fba1e9 100644 --- a/scripts/ApplyTypes.java +++ b/scripts/ApplyTypes.java @@ -111,8 +111,14 @@ public class ApplyTypes extends GhidraScript { // methods whose ECX offsets fit their class (scripts/this_types.py) become __thiscall, typing `this` int fitted = 0; 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.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(); if (!(ns instanceof GhidraClass)) try { st.convertNamespaceToClass(ns); } catch (Exception e) { continue; } f.setCallingConvention("__thiscall"); diff --git a/scripts/this_types.py b/scripts/this_types.py index 6ed63e2..2f08952 100644 --- a/scripts/this_types.py +++ b/scripts/this_types.py @@ -1,6 +1,8 @@ # 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). # 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) import collections, re, sys src, fields_tsv, out = sys.argv[1:4] @@ -20,8 +22,23 @@ def known(cls): r = [] while cls: r += spans.get(cls, []); cls = base.get(cls) 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() 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 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))