// Applies names from zhl_matches.tsv (va, Qualified::Name, quality, decl). Arg: tsv path import ghidra.app.script.GhidraScript; import ghidra.program.model.address.*; import ghidra.program.model.listing.*; import ghidra.program.model.symbol.*; import java.nio.file.*; public class ApplyNames extends GhidraScript { public void run() throws Exception { SymbolTable st = currentProgram.getSymbolTable(); int n = 0; for (Function f : currentProgram.getListing().getFunctions(true)) { // idempotent: undo previous runs String cm = f.getComment(); if (cm == null || !cm.startsWith("ZHL [")) continue; f.setComment(null); f.setParentNamespace(currentProgram.getGlobalNamespace()); f.setName(null, SourceType.DEFAULT); } for (String line : Files.readAllLines(Paths.get(getScriptArgs()[0]))) { String[] c = line.split("\t", 4); Address a = toAddr(c[0]); Function fn = getFunctionAt(a); if (fn == null) { disassemble(a); fn = createFunction(a, null); } String[] parts = c[1].split("::"); Namespace ns = currentProgram.getGlobalNamespace(); for (int k = 0; k < parts.length - 1; k++) { Namespace n2 = st.getNamespace(parts[k], ns); ns = n2 != null ? n2 : st.createNameSpace(ns, parts[k], SourceType.USER_DEFINED); } String leaf = parts[parts.length - 1]; if (fn == null) { st.createLabel(a, leaf, ns, SourceType.USER_DEFINED); continue; } try { fn.setParentNamespace(ns); fn.setName(leaf, SourceType.USER_DEFINED); } catch (Exception e) { fn.setName(leaf + "_" + c[0], SourceType.USER_DEFINED); } fn.setComment("ZHL [" + c[2] + "] " + c[3]); n++; } // vtable slots: name still-unnamed virtuals via RTTI "::::vftable" labels int v = 0; java.util.Map vft = new java.util.HashMap<>(); for (Symbol s : st.getAllSymbols(false)) { if (!s.getName().equals("vftable") || s.getParentNamespace() == null) continue; vft.putIfAbsent(s.getParentNamespace().getName(), s.getAddress()); } for (String line : Files.readAllLines(Paths.get(getScriptArgs()[1]))) { String[] c = line.split(" "); Address t = vft.get(c[0]); if (t == null) continue; Address a = toAddr(getInt(t.add(4L * Integer.parseInt(c[1]))) & 0xffffffffL); Function fn = getFunctionAt(a); if (fn == null) { disassemble(a); fn = createFunction(a, null); } if (fn == null || fn.getSymbol().getSource() != SourceType.DEFAULT) continue; // already named (inherited or ZHL) Namespace ns = st.getNamespace(c[0], currentProgram.getGlobalNamespace()); if (ns == null) ns = st.createNameSpace(currentProgram.getGlobalNamespace(), c[0], SourceType.USER_DEFINED); fn.setParentNamespace(ns); fn.setName(c[2], SourceType.USER_DEFINED); fn.setComment("ZHL [vtable slot " + c[1] + "]"); v++; } // Lua binding targets (optional 3rd tsv, same format): only name functions nothing else named int l = 0; if (getScriptArgs().length > 2) for (String line : Files.readAllLines(Paths.get(getScriptArgs()[2]))) { String[] c = line.split(" ", 4); Address a = toAddr(c[0]); Function fn = getFunctionAt(a); if (fn == null) { disassemble(a); fn = createFunction(a, null); } if (fn == null || fn.getSymbol().getSource() != SourceType.DEFAULT) continue; String[] parts = c[1].split("::"); Namespace ns = st.getNamespace(parts[0], currentProgram.getGlobalNamespace()); if (ns == null) ns = st.createNameSpace(currentProgram.getGlobalNamespace(), parts[0], SourceType.USER_DEFINED); fn.setParentNamespace(ns); try { fn.setName(parts[1], SourceType.USER_DEFINED); } catch (Exception e) { fn.setName(parts[1] + "_" + c[0], SourceType.USER_DEFINED); } fn.setComment("ZHL [lua] " + c[3]); l++; } println("ApplyNames: " + n + " sig, " + v + " vtable, " + l + " lua"); } }