Improve single file dump script
This commit is contained in:
239
scripts/re3lib/FunctionDumper.java
Normal file
239
scripts/re3lib/FunctionDumper.java
Normal file
@@ -0,0 +1,239 @@
|
||||
package re3lib;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import ghidra.app.decompiler.ClangLine;
|
||||
import ghidra.app.decompiler.ClangSyntaxToken;
|
||||
import ghidra.app.decompiler.ClangToken;
|
||||
import ghidra.app.decompiler.DecompileResults;
|
||||
import ghidra.app.decompiler.PrettyPrinter;
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.program.model.address.Address;
|
||||
import ghidra.program.model.listing.Function;
|
||||
import ghidra.program.model.pcode.HighFunction;
|
||||
import ghidra.program.model.pcode.HighSymbol;
|
||||
import ghidra.program.model.pcode.PcodeOp;
|
||||
import ghidra.program.model.pcode.Varnode;
|
||||
import re3lib.GlobalDumper.GlobalRec;
|
||||
|
||||
public class FunctionDumper {
|
||||
GhidraScript script;
|
||||
GlobalDumper globalDumper;
|
||||
HashSet<Address> functionAddrBlackList = new HashSet<>();
|
||||
|
||||
static final Pattern fieldAccessRegex = Pattern.compile("^_([0-9]+)_([0-9]+)_$");
|
||||
|
||||
public FunctionDumper(GhidraScript script, GlobalDumper globalDumper) {
|
||||
this.script = script;
|
||||
this.globalDumper = globalDumper;
|
||||
initFunctionBlacklist();
|
||||
}
|
||||
|
||||
boolean isValidFunction(Function function) {
|
||||
if (functionAddrBlackList.contains(function.getEntryPoint()))
|
||||
return false;
|
||||
if (function.getComment() != null) {
|
||||
if (function.getComment().startsWith("/i"))
|
||||
return false;
|
||||
if (function.getComment().startsWith("library function"))
|
||||
return false;
|
||||
}
|
||||
if (function.getName().startsWith("crt_"))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
void initFunctionBlacklist() {
|
||||
functionAddrBlackList = Utils.loadFunctionBlacklist(RecompileConfig.INSTANCE.functionBlacklistPath);
|
||||
|
||||
// Build blacklist if not loaded
|
||||
if (functionAddrBlackList == null) {
|
||||
boolean modified = false;
|
||||
Iterator<Function> functionsIt = script.getCurrentProgram().getFunctionManager().getFunctions(true).iterator();
|
||||
while (functionsIt.hasNext()) {
|
||||
Function function = functionsIt.next();
|
||||
if (functionAddrBlackList.contains(function.getEntryPoint())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
String comment = function.getComment();
|
||||
boolean isIgnoredFunction = false;
|
||||
if (comment != null && comment.contains("Library Function")) {
|
||||
script.println("Adding library function " + function.getName() + " to blacklist");
|
||||
script.println("ac:" + functionAddrBlackList.size() + " jj:"
|
||||
+ functionAddrBlackList.contains(function.getEntryPoint()) + " "
|
||||
+ function.getEntryPoint());
|
||||
isIgnoredFunction = true;
|
||||
}
|
||||
|
||||
if (function.getName().startsWith("crt_")) {
|
||||
script.println("Adding crt function " + function.getName() + " to blacklist");
|
||||
isIgnoredFunction = true;
|
||||
}
|
||||
|
||||
if (isIgnoredFunction) {
|
||||
// Decompile and trace
|
||||
PCallTracer tracer = new PCallTracer();
|
||||
tracer.setBlacklist(functionAddrBlackList);
|
||||
tracer.traceCalls(function);
|
||||
for (Function f : tracer.out) {
|
||||
script.println(" Adding " + f.getName() + " to blacklist");
|
||||
functionAddrBlackList.add(f.getEntryPoint());
|
||||
modified = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (modified) {
|
||||
Utils.saveFunctionBlacklist(functionAddrBlackList, RecompileConfig.INSTANCE.functionBlacklistPath);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void dump(Function function)
|
||||
throws Exception {
|
||||
String sanitizedFunctionName = Utils.sanitizeIdentifier(function.getName());
|
||||
String fileName = sanitizedFunctionName + ".cxx";
|
||||
|
||||
File f0 = new File(RecompileConfig.INSTANCE.dirDecompFix, fileName);
|
||||
if (f0.exists()) {
|
||||
script.println("Func " + function.getName() + " skipped (gh_fix)");
|
||||
f0 = new File(RecompileConfig.INSTANCE.dirDecompRef, fileName);
|
||||
} else {
|
||||
f0 = new File(RecompileConfig.INSTANCE.dirDecompAuto, fileName);
|
||||
if (f0.exists()) {
|
||||
f0.delete();
|
||||
}
|
||||
}
|
||||
|
||||
script.println("Processing " + function.getName() + " => " + f0.toString());
|
||||
|
||||
DecompileResults decompRes = RecompileConfig.INSTANCE.decompCache.getOrInsert(function);
|
||||
try (PrintWriter writer2 = new PrintWriter(f0, "UTF-8")) {
|
||||
writer2.println("// AUTO-GENERATED FILE, MOVE TO 'gh_fix' FOLDER PREVENT OVERWRITING!!!!! ");
|
||||
writer2.println();
|
||||
writer2.println("#include <gh_auto_binder.h>");
|
||||
writer2.println("#include \"../gh_global.h\"");
|
||||
writer2.println();
|
||||
|
||||
HighFunction highFunction = decompRes.getHighFunction();
|
||||
|
||||
HashSet<String> headers = new HashSet<>();
|
||||
StringWriter codeWriter = new StringWriter();
|
||||
|
||||
PrettyPrinter pp = new PrettyPrinter(decompRes.getFunction(), decompRes.getCCodeMarkup(), null);
|
||||
Iterator<ClangLine> lines = pp.getLines().iterator();
|
||||
while (lines.hasNext()) {
|
||||
ClangLine line = lines.next();
|
||||
for (int i = 0; i < line.getIndent(); i++) {
|
||||
codeWriter.write(' ');
|
||||
}
|
||||
|
||||
List<ClangToken> tokens = new ArrayList<>();
|
||||
// Parse preliminary line tokens
|
||||
for (int i = 0; i < line.getNumTokens(); i++) {
|
||||
ClangToken token = line.getToken(i);
|
||||
if (!token.getText().isEmpty())
|
||||
tokens.add(token);
|
||||
}
|
||||
|
||||
// Preprocess tokens
|
||||
boolean prevDot = false;
|
||||
for (int t = 0; t < tokens.size(); t++) {
|
||||
ClangToken token = tokens.get(t);
|
||||
|
||||
boolean thisDot = false;
|
||||
// println("Token: " + token.toString());
|
||||
if (token.toString().equals(".")) {
|
||||
// println("Found dot: " + token.toString() + " - " + token.getClass());
|
||||
thisDot = true;
|
||||
}
|
||||
|
||||
if (prevDot) {
|
||||
// println("Possible field access: " + token.getText());
|
||||
if (token instanceof ClangSyntaxToken) {
|
||||
// Parse _4_4_ sub-access using regex
|
||||
String text = token.getText();
|
||||
Matcher matcher = fieldAccessRegex.matcher(text);
|
||||
if (matcher.matches()) {
|
||||
int offset = Integer.parseInt(matcher.group(1));
|
||||
int size = Integer.parseInt(matcher.group(2));
|
||||
// println("MATCHED: " + token.getText() + " - " + token.getSyntaxType() + " - "
|
||||
// + token.getVarnode() + " - "
|
||||
// + token.getPcodeOp());
|
||||
|
||||
// Replace tokens with + Field<offset, size>
|
||||
ClangToken replacement = new ClangToken(token.Parent(), " + Field<" + offset + ", " + size + ">()");
|
||||
tokens.remove(t);
|
||||
tokens.remove(t - 1);
|
||||
tokens.add(t - 1, replacement);
|
||||
t--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Extract memory references
|
||||
HighSymbol gsym = token.getHighSymbol(highFunction);
|
||||
if (gsym != null) {
|
||||
var symStorage = gsym.getStorage();
|
||||
var sym = gsym.getSymbol();
|
||||
|
||||
Address address;
|
||||
if (symStorage.isUnassignedStorage()) {
|
||||
address = sym.getAddress();
|
||||
} else {
|
||||
address = gsym.getStorage().getMinAddress();
|
||||
}
|
||||
|
||||
if (address.isMemoryAddress()) {
|
||||
globalDumper.addGlobal(address, gsym);
|
||||
}
|
||||
}
|
||||
|
||||
// Extract external function calls
|
||||
PcodeOp op = token.getPcodeOp();
|
||||
if (op != null && op.getOpcode() == PcodeOp.CALL) {
|
||||
// println("PcodeOp: " + op.toString() + " - " + op.getInput(0).toString());
|
||||
Varnode target = op.getInput(0);
|
||||
if (target.isAddress()) {
|
||||
Address callAddr = target.getAddress();
|
||||
Function calledFunction = script.getFunctionAt(callAddr);
|
||||
if (calledFunction != null) {
|
||||
if (isValidFunction(calledFunction)) {
|
||||
headers.add("extern " + calledFunction.getSignature().getPrototypeString(true)
|
||||
+ "; // " + calledFunction.getEntryPoint() + " // "
|
||||
+ calledFunction.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
prevDot = thisDot;
|
||||
}
|
||||
|
||||
// Print tokens
|
||||
for (int t = 0; t < tokens.size(); t++) {
|
||||
ClangToken token = tokens.get(t);
|
||||
codeWriter.write(token.toString());
|
||||
}
|
||||
codeWriter.write('\n');
|
||||
}
|
||||
|
||||
for (String header : headers) {
|
||||
writer2.println(header);
|
||||
}
|
||||
writer2.println();
|
||||
writer2.println("// " + function.getEntryPoint());
|
||||
writer2.print(codeWriter.toString());
|
||||
writer2.println();
|
||||
}
|
||||
}
|
||||
}
|
181
scripts/re3lib/GlobalDumper.java
Normal file
181
scripts/re3lib/GlobalDumper.java
Normal file
@@ -0,0 +1,181 @@
|
||||
package re3lib;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.Dictionary;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.program.model.address.Address;
|
||||
import ghidra.program.model.data.AbstractStringDataType;
|
||||
import ghidra.program.model.data.Array;
|
||||
import ghidra.program.model.data.DataType;
|
||||
import ghidra.program.model.data.PointerDataType;
|
||||
import ghidra.program.model.pcode.HighSymbol;
|
||||
|
||||
public class GlobalDumper {
|
||||
|
||||
public class GlobalRec {
|
||||
public Address address;
|
||||
public String name;
|
||||
public DataType type;
|
||||
|
||||
public GlobalRec(Address address, String name, DataType type) {
|
||||
this.address = address;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
};
|
||||
|
||||
GhidraScript script;
|
||||
File manifestFile;
|
||||
HashMap<Address, GlobalRec> globalAddrs = new HashMap<>();
|
||||
|
||||
public GlobalDumper(GhidraScript script) {
|
||||
this.script = script;
|
||||
manifestFile = new File(RecompileConfig.INSTANCE.outputDir, "globals.txt");
|
||||
}
|
||||
|
||||
public void loadGlobalManifest() throws Exception {
|
||||
// Globals are stored in the format of
|
||||
// <address> || <name> || <type>
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(manifestFile))) {
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] parts = line.split(" || ");
|
||||
if (parts.length == 3) {
|
||||
String address = parts[0];
|
||||
String name = parts[1];
|
||||
String typeName = parts[2];
|
||||
DataType type = script.getCurrentProgram().getDataTypeManager().getDataType(typeName);
|
||||
globalAddrs.put(script.parseAddress(address), new GlobalRec(script.parseAddress(address), name, type));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void addGlobal(Address addr, HighSymbol sym) {
|
||||
globalAddrs.put(addr, new GlobalRec(addr, sym.getName(), sym.getDataType()));
|
||||
}
|
||||
|
||||
String escapeCString(String str) {
|
||||
str = str.replace("\\", "\\\\");
|
||||
str = str.replace("\"", "\\\"");
|
||||
return str;
|
||||
}
|
||||
|
||||
String readCString(Address addr, int maxLen) throws Exception {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
int ofs = 0;
|
||||
while (true) {
|
||||
Address read = addr.add(ofs++);
|
||||
// println("Reading: " + read);
|
||||
byte b = script.getCurrentProgram().getMemory().getByte(read);
|
||||
// println("Read: " + b);
|
||||
if (b == 0 || ofs >= maxLen) {
|
||||
break;
|
||||
}
|
||||
sb.append((char) b);
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
// println("STR \"" + sb.toString() + "\"");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public void dumpGlobals() throws Exception {
|
||||
File globalSymbolsListH = new File(RecompileConfig.INSTANCE.outputDir, "gh_global.h");
|
||||
PrintWriter hwriter = new PrintWriter(globalSymbolsListH, "UTF-8");
|
||||
hwriter.println("// AUTO-GENERATED FILE ");
|
||||
Utils.headerGuardPre(hwriter, "GLOBALS");
|
||||
hwriter.println("#include <gh_global_binder.h>");
|
||||
hwriter.println();
|
||||
|
||||
File globalSymbolsListC = new File(RecompileConfig.INSTANCE.outputDir, "gh_global.cxx");
|
||||
PrintWriter cwriter = new PrintWriter(globalSymbolsListC, "UTF-8");
|
||||
cwriter.println("// AUTO-GENERATED FILE ");
|
||||
cwriter.println("#include <gh_global_binder.h>");
|
||||
hwriter.println();
|
||||
|
||||
List<GlobalRec> globals = new ArrayList<>(globalAddrs.values());
|
||||
globals.sort((o1, o2) -> o1.address.compareTo(o2.address));
|
||||
|
||||
for (GlobalRec global : globals) {
|
||||
DataType dt = global.type;
|
||||
if (dt == null) {
|
||||
script.println("WARNING: Missing type for global: " + global.name + " at " + global.address);
|
||||
continue;
|
||||
}
|
||||
String dataType = dt.getDisplayName();
|
||||
String sanitizedName = Utils.sanitizeIdentifier(global.name);
|
||||
String name = global.name;
|
||||
Address addr = global.address;
|
||||
if (!sanitizedName.equals(name)) {
|
||||
script.println("WARNING: Invalid global symbol name: " + name);
|
||||
name = sanitizedName;
|
||||
}
|
||||
// println("Symbol: " + symbol + " Addr: " + addr + " Size:" + symSize + " " +
|
||||
// storage.getSerializationString());
|
||||
try {
|
||||
String initBlk = " = ";
|
||||
boolean fullyDefinedType = false;
|
||||
if (dt instanceof AbstractStringDataType) {
|
||||
AbstractStringDataType sdt = (AbstractStringDataType) dt;
|
||||
dataType = "const char*";
|
||||
// String type
|
||||
initBlk += "\"" + escapeCString(readCString(addr, 2048)) + "\"";
|
||||
fullyDefinedType = true;
|
||||
} else if (dt instanceof PointerDataType) {
|
||||
PointerDataType pdt = (PointerDataType) dt;
|
||||
DataType baseType = pdt.getDataType();
|
||||
dataType = baseType.getDisplayName() + "*";
|
||||
initBlk += "(" + dataType + ")&GH_MEM(0x" + addr + ")";
|
||||
fullyDefinedType = true;
|
||||
}
|
||||
if (fullyDefinedType) {
|
||||
hwriter.println("extern " + dataType + " " + name + "; // " + addr);
|
||||
cwriter.println(dataType + " " + name + initBlk + "; // " + addr);
|
||||
} else {
|
||||
if (dt instanceof Array) {
|
||||
// println("Array: " + dt.getDisplayName() + " - " + addr + " - " +
|
||||
// dt.getClass().getSimpleName());
|
||||
Array adt = (Array) dt;
|
||||
DataType baseType = adt.getDataType();
|
||||
hwriter.println(
|
||||
"extern " + baseType.getDisplayName() + "(&" + name + ")[" + adt.getNumElements() + "]; // " + addr);
|
||||
cwriter.println(
|
||||
baseType.getDisplayName() + "(&" + name + ")[" + adt.getNumElements() + "] = *reinterpret_cast<"
|
||||
+ baseType.getDisplayName() + "(*)[" + adt.getNumElements() + "]>(GH_MEM(0x" + addr + "));");
|
||||
} else {
|
||||
String refTypeStr = dt.getDisplayName() + "&";
|
||||
hwriter.println("extern " + refTypeStr + " " + name + "; // " + addr);
|
||||
cwriter.println(dataType + " " + name + "= (" + refTypeStr + ") GH_MEM(0x" + addr + ");");
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
script.println("Error processing global symbol: " + e);
|
||||
script.println("Symbol: " + name + " - " + addr);
|
||||
}
|
||||
}
|
||||
|
||||
Utils.headerGuardPost(hwriter, "GLOBALS");
|
||||
hwriter.close();
|
||||
cwriter.close();
|
||||
}
|
||||
|
||||
public void saveGlobalManifest() throws Exception {
|
||||
try (PrintWriter writer = new PrintWriter(manifestFile)) {
|
||||
script.println("Saving global manifest to " + manifestFile);
|
||||
for (Map.Entry<Address, GlobalRec> entry : globalAddrs.entrySet()) {
|
||||
writer.println(entry.getKey().toString() + " || " + entry.getValue().name + " || " + entry.getValue().type.getDisplayName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -21,6 +21,10 @@ public class Utils {
|
||||
writer.println("#endif // GH_GENERATED_" + tag + "_H");
|
||||
}
|
||||
|
||||
public static String sanitizeIdentifier(String name) {
|
||||
return name.replaceAll("[^a-zA-Z0-9_]", "_");
|
||||
}
|
||||
|
||||
public static HashSet<String> loadStructBlacklist(String path) {
|
||||
File file = new File(path);
|
||||
HashSet<String> structBlacklist = new HashSet<>();
|
||||
|
Reference in New Issue
Block a user