// Recovers Lua registrations from disassembly (catches the ones whose name the decompiler drops, e.g. Sprite:Play). // Args: registrationFuncAddr classes.txt out.tsv (classes.txt = known Lua class names, one per line) // Writes va \t Class::Member \t lua \t decl (ApplyNames format) import ghidra.app.script.GhidraScript; import ghidra.program.model.address.*; import ghidra.program.model.listing.*; import ghidra.program.model.symbol.*; import java.io.*; import java.nio.file.*; import java.util.*; public class LuaAsm extends GhidraScript { public void run() throws Exception { String[] a = getScriptArgs(); Set classes = new HashSet<>(Files.readAllLines(Paths.get(a[1]))); Function reg = getFunctionAt(toAddr(a[0])); String cls = null, str = null; Address fn = null; try (PrintWriter o = new PrintWriter(a[2])) { for (Instruction in : currentProgram.getListing().getInstructions(reg.getBody(), true)) { if (in.getFlowType().isCall()) { if (str != null && fn != null && cls != null) o.println(fn + "\t" + cls + "::" + str + "\tlua\tLua binding " + cls + ":" + str); else if (str != null && fn == null && classes.contains(str)) cls = str; str = null; fn = null; continue; } for (Reference r : in.getReferencesFrom()) { Data d = getDataAt(r.getToAddress()); if (d != null && d.hasStringValue()) { str = String.valueOf(d.getValue()); continue; } if (d == null && r.getReferenceType().isData() && getInstructionAt(r.getToAddress()) != null) fn = r.getToAddress(); else if (getFunctionAt(r.getToAddress()) != null && !r.getReferenceType().isCall()) fn = r.getToAddress(); } // short names Ghidra did not define as strings: MOV reg, imm pointing at ASCII if (str == null) for (Object op : in.getOpObjects(in.getNumOperands() - 1 < 0 ? 0 : in.getNumOperands() - 1)) { if (!(op instanceof ghidra.program.model.scalar.Scalar)) continue; long v = ((ghidra.program.model.scalar.Scalar) op).getUnsignedValue(); if (v < 0x400000 || v > 0x2000000) continue; try { byte[] b = getBytes(toAddr(v), 32); int n = 0; while (n < 32 && b[n] != 0 && (Character.isLetterOrDigit(b[n]) || b[n] == '_')) n++; if (n > 1 && n < 32 && b[n] == 0) str = new String(b, 0, n); } catch (Exception e) { } } } } } }