// 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 seen = new HashSet<>(); for (Iterator 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()); 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 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 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; } }