DumpAll writes dump/types.tsv (Ghidra structs/unions with components, typedefs, enums with values), data.tsv (global data labels with types) and externals.tsv (imported functions). source_tree.py lays the Ghidra types out with the classes (591 types, 347 sized), declares every global, import and code label, drops Windows calling conventions, leaves third-party classes (std, rapidxml, theoraplayer, luabridge, stb_vorbis) declared but not reconstructed, turns raw vtable calls on typed objects into named virtual calls and (int)p + n byte arithmetic into (char *)p + n. The tree now targets x86_64 Linux; the original 32-bit layout stays as LAYOUT32 asserts, checked with -m32 (they hold). --check: 775/3262 method bodies compile for x86_64 (23.8%), 23010 raw offset accesses left (64-bit hazards). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
111 lines
6.8 KiB
Java
111 lines
6.8 KiB
Java
// Dumps decompiled C of every function + index, strings-with-xrefs, call edges. Arg: output dir
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.app.decompiler.*;
|
|
import ghidra.program.model.address.*;
|
|
import ghidra.program.model.listing.*;
|
|
import ghidra.program.model.symbol.*;
|
|
import ghidra.program.model.data.StringDataInstance;
|
|
import java.io.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.*;
|
|
import java.util.concurrent.atomic.*;
|
|
|
|
public class DumpAll extends GhidraScript {
|
|
public void run() throws Exception {
|
|
File out = new File(getScriptArgs()[0]);
|
|
out.mkdirs();
|
|
Listing listing = currentProgram.getListing();
|
|
ReferenceManager rm = currentProgram.getReferenceManager();
|
|
|
|
try (PrintWriter idx = new PrintWriter(new File(out, "functions.tsv"));
|
|
PrintWriter calls = new PrintWriter(new File(out, "calls.tsv"))) {
|
|
for (Function f : listing.getFunctions(true)) {
|
|
idx.println(f.getEntryPoint() + "\t" + f.getName(true) + "\t" + f.getBody().getNumAddresses());
|
|
for (Function c : f.getCalledFunctions(monitor)) calls.println(f.getEntryPoint() + "\t" + c.getEntryPoint());
|
|
}
|
|
}
|
|
|
|
try (PrintWriter st = new PrintWriter(new File(out, "strings.tsv"))) {
|
|
for (Data d : listing.getDefinedData(true)) {
|
|
if (!d.hasStringValue()) continue;
|
|
String s = String.valueOf(d.getValue()).replace("\t", "\\t").replace("\n", "\\n");
|
|
StringBuilder who = new StringBuilder();
|
|
for (Reference r : rm.getReferencesTo(d.getAddress())) {
|
|
Function f = getFunctionContaining(r.getFromAddress());
|
|
who.append(f == null ? r.getFromAddress().toString() : f.getEntryPoint() + ":" + f.getName(true)).append(' ');
|
|
}
|
|
st.println(d.getAddress() + "\t" + s + "\t" + who.toString().trim());
|
|
}
|
|
}
|
|
|
|
// what a compilable body needs besides functions: every datatype as C (casts and locals name them) and every
|
|
// global data label with its type (DAT_, s_, g_ ...); undefined data gets "undefined" and its referenced size
|
|
try (PrintWriter ty = new PrintWriter(new File(out, "types.tsv"))) { // S/U struct|union name size, F component, T typedef, E enum, P function type
|
|
ty.println("# S|U\tname\tsize / F\tstruct\toffset\ttype\tname\tlength / T\tname\ttype / E\tname\tsize / P\tname\tprototype");
|
|
Set<String> seen = new HashSet<>();
|
|
for (Iterator<ghidra.program.model.data.DataType> it = currentProgram.getDataTypeManager().getAllDataTypes(); it.hasNext(); ) {
|
|
ghidra.program.model.data.DataType dt = it.next();
|
|
if (!seen.add(dt.getName())) continue; // the same name in two categories: keep the first
|
|
if (dt instanceof ghidra.program.model.data.Composite c) {
|
|
ty.println((c instanceof ghidra.program.model.data.Union ? "U" : "S") + "\t" + c.getName() + "\t" + c.getLength());
|
|
for (ghidra.program.model.data.DataTypeComponent k : c.getDefinedComponents())
|
|
ty.println("F\t" + c.getName() + "\t" + k.getOffset() + "\t" + k.getDataType().getDisplayName() + "\t" + (k.getFieldName() == null ? "" : k.getFieldName()) + "\t" + k.getLength());
|
|
}
|
|
else if (dt instanceof ghidra.program.model.data.TypeDef t) ty.println("T\t" + t.getName() + "\t" + t.getDataType().getDisplayName());
|
|
else if (dt instanceof ghidra.program.model.data.Enum e) {
|
|
ty.println("E\t" + e.getName() + "\t" + e.getLength());
|
|
for (String n : e.getNames()) ty.println("V\t" + e.getName() + "\t" + n + "\t" + e.getValue(n));
|
|
}
|
|
else if (dt instanceof ghidra.program.model.data.FunctionDefinition f) ty.println("P\t" + f.getName() + "\t" + f.getPrototypeString());
|
|
}
|
|
}
|
|
try (PrintWriter gd = new PrintWriter(new File(out, "data.tsv"))) {
|
|
gd.println("# address\tname\ttype\tlength");
|
|
for (Symbol s : currentProgram.getSymbolTable().getAllSymbols(false)) {
|
|
if (s.getSymbolType() != SymbolType.LABEL || !s.isPrimary() || !s.isGlobal()) continue;
|
|
var block = currentProgram.getMemory().getBlock(s.getAddress());
|
|
if (block == null || block.isExecute()) continue;
|
|
Data d = listing.getDataAt(s.getAddress());
|
|
boolean isDefined = d != null && d.isDefined();
|
|
gd.println(s.getAddress() + "\t" + s.getName() + "\t" + (isDefined ? d.getDataType().getPathName() : "undefined") + "\t" + (isDefined ? d.getLength() : 0));
|
|
}
|
|
}
|
|
try (PrintWriter ex = new PrintWriter(new File(out, "externals.tsv"))) { // imported functions (CRT, OpenGL, Win32 ...)
|
|
ex.println("# library\tname\tprototype");
|
|
for (Function f : currentProgram.getFunctionManager().getExternalFunctions())
|
|
ex.println(f.getExternalLocation().getLibraryName() + "\t" + f.getName() + "\t" + f.getPrototypeString(false, false));
|
|
}
|
|
|
|
List<Function> fns = new ArrayList<>();
|
|
for (Function f : listing.getFunctions(true)) if (!f.isThunk()) fns.add(f);
|
|
int nThreads = Math.max(1, Runtime.getRuntime().availableProcessors() - 2);
|
|
String[] results = new String[fns.size()];
|
|
AtomicInteger next = new AtomicInteger();
|
|
List<Thread> threads = new ArrayList<>();
|
|
for (int t = 0; t < nThreads; t++) {
|
|
Thread th = new Thread(() -> {
|
|
DecompInterface di = new DecompInterface();
|
|
DecompileOptions opts = new DecompileOptions();
|
|
opts.setMaxPayloadMBytes(1024); // UseActiveItem-sized switches overflow the 50MB default
|
|
di.setOptions(opts);
|
|
di.openProgram(currentProgram);
|
|
int i;
|
|
while ((i = next.getAndIncrement()) < fns.size()) {
|
|
Function f = fns.get(i);
|
|
DecompileResults r = di.decompileFunction(f, 900, TaskMonitor_dummy());
|
|
String c = r != null && r.decompileCompleted() ? r.getDecompiledFunction().getC() : "/* decompile failed: " + (r == null ? "" : r.getErrorMessage()) + " */\n";
|
|
results[i] = "//@@ " + f.getEntryPoint() + " " + f.getName(true) + "\n" + c + "\n";
|
|
if (i % 2000 == 0) System.out.println("decompiled " + i + "/" + fns.size());
|
|
}
|
|
di.dispose();
|
|
});
|
|
th.start();
|
|
threads.add(th);
|
|
}
|
|
for (Thread th : threads) th.join();
|
|
try (PrintWriter all = new PrintWriter(new File(out, "all.c"))) { for (String s : results) all.print(s); }
|
|
println("dumped " + fns.size());
|
|
}
|
|
static ghidra.util.task.TaskMonitor TaskMonitor_dummy() { return ghidra.util.task.TaskMonitor.DUMMY; }
|
|
}
|