Files

76 lines
3.0 KiB
Java

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.FunctionManager;
import ghidra.program.model.listing.Instruction;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSpace;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.util.*;
/**
* Decompile a specific list of addresses given in the FIRST line of a file
* (comma-separated hex). Includes tiny functions. Writes C into an output file.
*/
public class DumpList extends GhidraScript {
@Override
public void run() throws Exception {
long LO = 0x180000000L;
long HI = 0x182c00000L;
String listFile = System.getProperty("DumpList.list",
"/home/m/re-tools/focus_list.txt");
String outFile = System.getProperty("DumpList.out",
"/home/m/re-tools/focus_decomp.txt");
Set<Long> targets = new LinkedHashSet<>();
BufferedReader br = new BufferedReader(new FileReader(listFile));
String line;
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.isEmpty()) continue;
for (String tok : line.split(",")) {
tok = tok.trim();
if (tok.isEmpty()) continue;
try {
targets.add(Long.parseLong(tok.replaceFirst("^0x", ""), 16));
} catch (Exception e) { println("bad tok " + tok); }
}
}
br.close();
FunctionManager fm = currentProgram.getFunctionManager();
AddressSpace as = currentProgram.getAddressFactory().getDefaultAddressSpace();
DecompInterface di = new DecompInterface();
di.openProgram(currentProgram);
PrintWriter pw = new PrintWriter(new java.io.BufferedWriter(
new FileWriter(outFile, false)));
for (long a : targets) {
if (a < LO || a > HI) continue;
Function f = fm.getFunctionAt(as.getAddress(a));
if (f == null) f = fm.getFunctionContaining(as.getAddress(a));
if (f == null) {
pw.println("############ NO_FUNCTION 0x" + Long.toHexString(a) + " ############");
continue;
}
DecompileResults res = di.decompileFunction(f, 120, monitor);
pw.println("############ FUN_ " + Long.toHexString(a) + " size=" +
f.getBody().getNumAddresses() + " ############");
if (res != null && res.getDecompiledFunction() != null) {
pw.println(res.getDecompiledFunction().getC());
} else {
pw.println("// decompile failed");
}
pw.println();
}
pw.close();
di.dispose();
println("DUMPLIST_DONE n=" + targets.size());
}
}