Files
isaac-port/scripts/ApplyProtos.java
T

87 lines
5.3 KiB
Java

// Applies REPENTOGON's function declarations (zhl dir) as prototypes, but only where this exe confirms them: for
// __thiscall/__stdcall/__fastcall the callee pops its stack arguments (RET n), so the declared stack parameter sizes must
// add up to the purge Ghidra found; __cdecl must purge 0. Overloads pick the declaration that fits. Arg: zhl dir
import ghidra.app.script.GhidraScript;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.symbol.SourceType;
import java.nio.file.*;
import java.util.*;
import java.util.regex.*;
public class ApplyProtos extends GhidraScript {
DataTypeManager dtm;
DataType type(String t) { // null = unknown (by-value struct we do not have, or unparseable)
t = t.replace("const ", "").replace("struct ", "").replace("&", "*").trim();
int ptr = 0;
while (t.endsWith("*")) { ptr++; t = t.substring(0, t.length() - 1).trim(); }
DataType base;
switch (t) {
case "void": base = VoidDataType.dataType; break;
case "bool": case "char": case "int8_t": case "uint8_t": case "unsigned char": base = ByteDataType.dataType; break;
case "short": case "int16_t": case "uint16_t": case "unsigned short": base = ShortDataType.dataType; break;
case "int": case "int32_t": case "long": base = IntegerDataType.dataType; break;
case "unsigned int": case "uint32_t": case "unsigned long": base = UnsignedIntegerDataType.dataType; break;
case "float": base = FloatDataType.dataType; break;
case "double": base = DoubleDataType.dataType; break;
case "long long": case "int64_t": base = LongLongDataType.dataType; break;
case "unsigned long long": case "uint64_t": base = UnsignedLongLongDataType.dataType; break;
default:
base = t.matches("\\w+") ? dtm.getDataType(new CategoryPath("/"), t) : null;
if (base == null && ptr == 0) return null;
if (base == null) base = VoidDataType.dataType;
}
for (int i = 0; i < ptr; i++) base = new PointerDataType(base);
return ptr == 0 && base instanceof Structure && base.getLength() < 4 ? null : base; // empty placeholder struct
}
public void run() throws Exception {
dtm = currentProgram.getDataTypeManager();
Pattern decl = Pattern.compile("^\\s*(?:static\\s+)?(__thiscall|__stdcall|__cdecl|__fastcall)\\s+(.+?)\\s*([\\w:]+::~?\\w+)\\s*\\((.*?)\\)\\s*;");
Map<String, List<Matcher>> byName = new HashMap<>();
for (Path p : Files.newDirectoryStream(Paths.get(getScriptArgs()[0]), "*.zhl"))
for (String l : Files.readAllLines(p, java.nio.charset.StandardCharsets.ISO_8859_1)) {
Matcher m = decl.matcher(l);
if (m.find()) byName.computeIfAbsent(m.group(3).replace("_", ""), k -> new ArrayList<>()).add(m);
}
int applied = 0, rejected = 0, unknown = 0;
for (Function f : currentProgram.getListing().getFunctions(true)) {
List<Matcher> cands = byName.get(f.getName(true).replace("_", ""));
if (cands == null || f.getName(true).startsWith("NpcAI::")) continue;
int purge = f.getStackPurgeSize();
if (purge < 0 || purge > 0x400) { unknown++; continue; }
boolean done = false;
for (Matcher m : cands) {
String cc = m.group(1);
DataType ret = type(m.group(2));
List<ParameterImpl> ps = new ArrayList<>();
int stack = 0, regs = cc.equals("__fastcall") ? 2 : 0;
boolean ok = ret != null;
String args = m.group(4).trim();
if (ok && !args.isEmpty() && !args.equals("void"))
for (String a : args.split(",")) {
a = a.replaceAll("=.*", "").trim();
Matcher nm = Pattern.compile("^(.*?)([A-Za-z_]\\w*)$").matcher(a);
String t = nm.matches() && nm.group(1).trim().length() > 0 ? nm.group(1) : a, n = nm.matches() && nm.group(1).trim().length() > 0 ? nm.group(2) : "p" + ps.size();
DataType dt = type(t);
if (dt == null || dt instanceof VoidDataType) { ok = false; break; }
int slot = (dt.getLength() + 3) & ~3;
if (regs > 0 && slot == 4 && !(dt instanceof FloatDataType)) regs--; else stack += slot;
ps.add(new ParameterImpl(n, dt, currentProgram));
}
int expect = cc.equals("__cdecl") ? 0 : stack;
if (!ok || expect != purge) continue;
f.setCallingConvention(cc);
f.setReturnType(ret, SourceType.USER_DEFINED);
f.replaceParameters(Function.FunctionUpdateType.DYNAMIC_STORAGE_FORMAL_PARAMS, true, SourceType.USER_DEFINED, ps.toArray(new ParameterImpl[0]));
String cm = f.getComment() == null ? "" : f.getComment() + "\n";
if (!cm.contains("prototype:")) f.setComment(cm + "prototype: REPENTOGON, confirmed by RET " + purge);
applied++; done = true; break;
}
if (!done) rejected++;
}
println("ApplyProtos: " + applied + " applied, " + rejected + " rejected (stack purge disagrees or unknown types), " + unknown + " purge unknown");
}
}