Files
AVA2/tools/reverse_speedtree/ExportSpeedTree.java
T

74 lines
3.9 KiB
Java

// Export analysis pseudocode and a function/call index from SpeedTreeRT.
// Run as a Ghidra headless postScript, passing an output directory.
// @category AVA2
import ghidra.app.script.GhidraScript;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.FunctionIterator;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.charset.StandardCharsets;
import java.io.BufferedWriter;
import java.util.ArrayList;
import java.util.Comparator;
public class ExportSpeedTree extends GhidraScript {
private static String cell(String s) {
return s.replace('\t', ' ').replace('\n', ' ').replace('\r', ' ');
}
@Override
public void run() throws Exception {
String[] args = getScriptArgs();
if (args.length != 1) throw new IllegalArgumentException("Expected output directory");
Path out = Path.of(args[0]);
Files.createDirectories(out.resolve("functions"));
DecompInterface dec = new DecompInterface();
dec.toggleCCode(true);
dec.toggleSyntaxTree(true);
if (!dec.openProgram(currentProgram)) throw new IllegalStateException(dec.getLastMessage());
int count = 0, success = 0, failed = 0;
try (BufferedWriter all = Files.newBufferedWriter(out.resolve("decompiled.c"), StandardCharsets.UTF_8);
BufferedWriter index = Files.newBufferedWriter(out.resolve("functions.tsv"), StandardCharsets.UTF_8);
BufferedWriter calls = Files.newBufferedWriter(out.resolve("calls.tsv"), StandardCharsets.UTF_8)) {
all.write("/* Ghidra analysis pseudocode, not original source or a compilable port. */\n");
index.write("address\tname\tprototype\tstatus\tfile\n");
calls.write("caller_address\tcaller\tcallee_address\tcallee\n");
FunctionIterator it = currentProgram.getFunctionManager().getFunctions(true);
while (it.hasNext()) {
monitor.checkCancelled();
Function f = it.next();
if (f.isExternal()) continue;
count++;
String addr = f.getEntryPoint().toString();
String name = f.getName(true);
DecompileResults result = dec.decompileFunction(f, 90, monitor);
boolean ok = result.decompileCompleted() && result.getDecompiledFunction() != null;
String status = ok ? "ok" : "failed: " + cell(result.getErrorMessage());
String code = "/* " + addr + " " + name + " */\n";
code += ok ? result.getDecompiledFunction().getC() : "/* " + status + " */\n";
if (ok) success++; else failed++;
String file = "functions/" + addr + ".c";
Files.writeString(out.resolve(file), code, StandardCharsets.UTF_8);
all.write(code + "\n");
index.write(addr + "\t" + cell(name) + "\t" + cell(f.getPrototypeString(false, false))
+ "\t" + status + "\t" + file + "\n");
ArrayList<Function> targets = new ArrayList<>(f.getCalledFunctions(monitor));
targets.sort(Comparator.comparing(t -> t.getEntryPoint().toString()));
for (Function t : targets) calls.write(addr + "\t" + cell(name) + "\t"
+ t.getEntryPoint() + "\t" + cell(t.getName(true)) + "\n");
if (count % 100 == 0) println("Exported " + count + " functions");
}
} finally {
dec.dispose();
}
String summary = "Program: " + currentProgram.getName() + "\nLanguage: "
+ currentProgram.getLanguageID() + "\nCompiler: " + currentProgram.getCompilerSpec().getCompilerSpecID()
+ "\nFunctions: " + count + "\nDecompiled: " + success + "\nFailed: " + failed + "\n";
Files.writeString(out.resolve("summary.txt"), summary, StandardCharsets.UTF_8);
println(summary);
}
}