This commit is contained in:
2024-09-18 01:56:43 +08:00
parent 33f393e123
commit 0a3025302f
6 changed files with 365 additions and 1926 deletions

View File

@@ -7,6 +7,7 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashSet;
@@ -15,24 +16,34 @@ import java.util.Iterator;
import java.util.List;
import java.util.Scanner;
import java.util.Arrays;
import java.util.Dictionary;
import ghidra.app.cmd.label.AddLabelCmd;
import ghidra.app.decompiler.ClangLine;
import ghidra.app.decompiler.ClangMarkup;
import ghidra.app.decompiler.ClangNode;
import ghidra.app.decompiler.ClangToken;
import ghidra.app.decompiler.ClangTokenGroup;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.decompiler.DecompileResults;
import ghidra.app.decompiler.DecompiledFunction;
import ghidra.app.decompiler.PrettyPrinter;
import ghidra.app.script.GhidraScript;
import ghidra.docking.settings.Settings;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressFactory;
import ghidra.program.model.data.AbstractStringDataType;
import ghidra.program.model.data.BuiltInDataType;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.data.DataOrganization;
import ghidra.program.model.data.DataOrganizationImpl;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeComponent;
import ghidra.program.model.data.PointerDataType;
import ghidra.program.model.data.ProgramBasedDataTypeManager;
import ghidra.program.model.data.SourceArchive;
import ghidra.program.model.data.StringDataInstance;
import ghidra.program.model.data.Structure;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Variable;
import ghidra.program.model.listing.VariableStorage;
@@ -44,12 +55,37 @@ import ghidra.program.model.pcode.HighSymbol;
import ghidra.program.model.pcode.PcodeOp;
import ghidra.program.model.pcode.PcodeOpAST;
import ghidra.program.model.pcode.Varnode;
import ghidra.program.model.symbol.NameTransformer;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.model.symbol.SymbolTable;
import ghidra.program.model.symbol.SymbolType;
public class DecompileC extends GhidraScript {
public class DecompileCache {
private static final int TIMEOUT = 10000;
Hashtable<Function, DecompileResults> cache = new Hashtable<>();
DecompInterface decomp;
public DecompileCache(DecompInterface decomp) {
this.decomp = decomp;
}
public DecompileResults get(Function function) {
return cache.get(function);
}
public DecompileResults getOrInsert(Function function) {
DecompileResults res = cache.get(function);
if (res == null) {
res = decomp.decompileFunction(function, TIMEOUT, monitor);
cache.put(function, res);
}
return res;
}
}
public class PCallTracer {
public class QueueItem {
Function function;
@@ -65,9 +101,9 @@ public class DecompileC extends GhidraScript {
public boolean trace = false;
List<QueueItem> queue = new ArrayList<>();
HashSet<Address> visited = new HashSet<>();
DecompInterface decomp;
DecompileCache decomp;
PCallTracer(DecompInterface decomp) {
PCallTracer(DecompileCache decomp) {
this.decomp = decomp;
}
@@ -104,7 +140,7 @@ public class DecompileC extends GhidraScript {
if (trace) {
println("PCallTracer, visiting " + function.getName() + " (depth:" + depth + ")");
}
DecompileResults decompRes = decomp.decompileFunction(function, TIMEOUT, monitor);
DecompileResults decompRes = decomp.getOrInsert(function);
visit(decompRes.getHighFunction(), depth);
out.add(function);
}
@@ -120,12 +156,13 @@ public class DecompileC extends GhidraScript {
}
private static final String OUTPUT_DIR = "game_re";
private static final int TIMEOUT = 10000;
// The static memory block
private Address staticMemoryBlockStart;
private Address staticMemoryBlockEnd;
private DecompileCache decompCache;
// Auto rename invalid symbols
private static final boolean AUTO_RENAME_SYMBOLS = true;
@@ -187,7 +224,7 @@ public class DecompileC extends GhidraScript {
static final boolean BUILD_BLACKLIST = true;
void buildFunctionBlacklist(DecompInterface decomp) {
void buildFunctionBlacklist() {
loadFunctionBlacklist();
if (BUILD_BLACKLIST) {
@@ -216,7 +253,7 @@ public class DecompileC extends GhidraScript {
if (isIgnoredFunction) {
// Decompile and trace
PCallTracer tracer = new PCallTracer(decomp);
PCallTracer tracer = new PCallTracer(decompCache);
tracer.setBlacklist(functionAddrBlackList);
tracer.traceCalls(function);
for (Function f : tracer.out) {
@@ -233,13 +270,13 @@ public class DecompileC extends GhidraScript {
}
}
void sanitizeGlobalSymbolsPass(DecompInterface decomp, List<Function> functions) {
void sanitizeGlobalSymbolsPass(List<Function> functions) {
Hashtable<String, HighSymbol> globalSymbols = new Hashtable<>();
for (Function function : functions) {
println("Processing global symbols for " + function.getName());
DecompileResults decompRes = decomp.decompileFunction(function, TIMEOUT, monitor);
DecompileResults decompRes = decompCache.getOrInsert(function);
Iterator<HighSymbol> smyIt = decompRes.getHighFunction().getGlobalSymbolMap().getSymbols();
HighSymbol gsym = smyIt.next();
@@ -303,7 +340,7 @@ public class DecompileC extends GhidraScript {
return sb.toString();
}
void decompileFunction(Hashtable<String, HighSymbol> outGlobalSymbols, DecompInterface decomp, Function function)
void decompileFunction(Hashtable<String, HighSymbol> outGlobalSymbols, Function function)
throws Exception {
String fileName = sanitizeFunctionName(function.getName()) + ".cxx";
@@ -320,51 +357,192 @@ public class DecompileC extends GhidraScript {
println("Processing " + function.getName() + " => " + f0.toString());
DecompileResults decompRes = decomp.decompileFunction(function, TIMEOUT, monitor);
PrintWriter writer2 = new PrintWriter(f0, "UTF-8");
writer2.println("// AUTO-GENERATED FILE, MOVE TO 'gh_fix' FOLDER PREVENT OVERWRITING!!!!! ");
writer2.println("// " + function.getEntryPoint());
writer2.println();
writer2.println("#include <gh_auto_shared.h>");
writer2.println("#include \"../gh_global.h\"");
writer2.println();
DecompileResults decompRes = 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_shared.h>");
writer2.println("#include \"../gh_global.h\"");
writer2.println();
HighFunction highFunction = decompRes.getHighFunction();
// ClangTokenGroup
// ClangNode.
// ClangTokenGroup ctg = decompRes.getCCodeMarkup();
// for (ClangTokenGroup it = ctg.groupIterator(); it.hasNext();) {
// }
writer2.println(cm.getCode());
// decompRes.get
HighFunction highFunction = decompRes.getHighFunction();
writer2.close();
// Remap for dynamic symbols
// Dictionary<String, String> symbolRemap = new Hashtable<>();
// Collect referenced global symbols
Iterator<HighSymbol> smyIt = decompRes.getHighFunction().getGlobalSymbolMap().getSymbols();
while (smyIt.hasNext()) {
HighSymbol gsym = smyIt.next();
if (outGlobalSymbols.containsKey(gsym.getName()))
continue;
outGlobalSymbols.put(gsym.getName(), gsym);
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(' ');
}
for (int t = 0; t < line.getNumTokens(); t++) {
ClangToken token = line.getToken(t);
HighSymbol gsym = token.getHighSymbol(highFunction);
if (gsym != null) {
var symStorage = gsym.getStorage();
if (symStorage.isMemoryStorage() || symStorage.isConstantStorage()) {
// println("Token: " + token.toString() + " - " + gsym.getName());
outGlobalSymbols.put(gsym.getName(), gsym);
}
}
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 = getFunctionAt(callAddr);
if (calledFunction != null) {
if (functionAddrBlackList.contains(calledFunction.getEntryPoint())) {
println("Adding header: " + calledFunction + " / "
+ calledFunction.getSignature().getPrototypeString(true));
headers.add("extern " + calledFunction.getSignature().getPrototypeString(true));
}
}
}
}
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();
// Iterator<ClangToken> it = decompRes.getCCodeMarkup().tokenIterator(true);
// int ln = 0;
// while(it.hasNext()) {
// ClangToken token = it.next();
// ClangLine line = token.getLineParent();
// while (line != null && ln < line.getLineNumber()) {
// writer2.println();
// ln++;
// }
// writer2.print(token.toString());
// }
// Collect referenced global symbols
// Iterator<HighSymbol> smyIt = highFunction.getGlobalSymbolMap().getSymbols();
// while (smyIt.hasNext()) {
// HighSymbol gsym = smyIt.next();
// Address addr = gsym.getSymbol().getAddress();
// println("FunctionSym " + addr + " " + gsym.getName() + " " +
// gsym.getStorage().getMinAddress());
// println(" IsMem: " + gsym.getStorage().isMemoryStorage() + " " +
// gsym.getStorage().getSerializationString());
// if (outGlobalSymbols.containsKey(gsym.getName()))
// continue;
// outGlobalSymbols.put(gsym.getName(), gsym);
// }
}
}
void decompileAll(DecompInterface decomp, List<Function> functions) throws Exception {
Hashtable<String, HighSymbol> globalSymbols = new Hashtable<>();
for (Function function : functions) {
decompileFunction(globalSymbols, decomp, function);
HashSet<String> loadStructBlacklist() {
File file = new File(outputDir, "struct_blacklist.txt");
HashSet<String> structBlacklist = new HashSet<>();
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
structBlacklist.add(line.trim());
}
} catch (FileNotFoundException e) {
return null;
}
return structBlacklist;
}
void saveStructBlacklist(HashSet<String> structBlacklist) throws Exception {
String[] arr = structBlacklist.toArray(new String[0]);
Arrays.sort(arr);
File file = new File(outputDir, "struct_blacklist.txt");
try (PrintWriter writer = new PrintWriter(file)) {
for (String structName : arr) {
writer.println(structName);
}
}
}
void headerGuardPre(PrintWriter writer, String tag) {
writer.println("#ifndef GH_GENERATED_" + tag + "_H");
writer.println("#define GH_GENERATED_" + tag + "_H");
writer.println();
}
void headerGuardPost(PrintWriter writer, String tag) {
writer.println("#endif // GH_GENERATED_" + tag + "_H");
}
void dumpStructureTypes() throws Exception {
try (PrintWriter writer = new PrintWriter(new File(outputDir, "gh_structs.h"), "UTF-8")) {
headerGuardPre(writer, "STRUCTS");
writer.println("// AUTO-GENERATED FILE ");
writer.println("#include <gh_auto_shared.h>");
ProgramBasedDataTypeManager dtm = currentProgram.getDataTypeManager();
HashSet<String> structBlacklist = loadStructBlacklist();
if (structBlacklist == null) {
println("Building struct blacklist from existing data types");
structBlacklist = new HashSet<>();
Iterator<DataType> it = dtm.getAllDataTypes();
while (it.hasNext()) {
DataType dt = it.next();
if (dt instanceof Structure) {
structBlacklist.add(dt.getName());
}
}
saveStructBlacklist(structBlacklist);
}
Iterator<DataType> it = dtm.getAllDataTypes();
while (it.hasNext()) {
DataType dt = it.next();
if (dt instanceof Structure) {
Structure struct = (Structure) dt;
if (structBlacklist.contains(struct.getName()))
continue;
writer.println("struct " + struct.getName() + " {");
for (DataTypeComponent component : struct.getComponents()) {
writer.println(
" " + component.getDataType().getDisplayName() + " " + component.getDefaultFieldName() + ";");
}
writer.println("};");
writer.println();
}
}
headerGuardPost(writer, "STRUCTS");
}
}
void dumpGlobals(Hashtable<String, HighSymbol> globalSymbols) throws Exception {
File globalSymbolsListH = new File(outputDir, "gh_global.h");
PrintWriter hwriter = new PrintWriter(globalSymbolsListH, "UTF-8");
hwriter.println("// AUTO-GENERATED FILE ");
headerGuardPre(hwriter, "GLOBALS");
hwriter.println("#include <gh_auto_shared.h>");
hwriter.println("#include \"gh_structs.h\"");
hwriter.println();
File globalSymbolsListC = new File(outputDir, "gh_global.cxx");
PrintWriter cwriter = new PrintWriter(globalSymbolsListC, "UTF-8");
cwriter.println("// AUTO-GENERATED FILE ");
cwriter.println("#include <gh_auto_shared.h>");
cwriter.println("#include \"gh_structs.h\"");
hwriter.println();
for (HighSymbol highSym : globalSymbols.values()) {
DataType dt = highSym.getDataType();
String dataType = dt.getDisplayName();
@@ -400,7 +578,7 @@ public class DecompileC extends GhidraScript {
dataType = baseType.getDisplayName() + "*";
initBlk += "gh_ptr(0x" + addr + ")";
} else {
initBlk = " = 0";
initBlk = " {}";
}
cwriter.println(dataType + " " + name + initBlk + "; // " + addr);
} catch (Exception e) {
@@ -412,16 +590,32 @@ public class DecompileC extends GhidraScript {
hwriter.println("extern " + dataType + " " + name + "; // " + addr);
}
}
headerGuardPost(hwriter, "GLOBALS");
hwriter.close();
cwriter.close();
}
void decompileAll(List<Function> functions) throws Exception {
Hashtable<String, HighSymbol> globalSymbols = new Hashtable<>();
for (Function function : functions) {
decompileFunction(globalSymbols, function);
}
dumpStructureTypes();
dumpGlobals(globalSymbols);
}
@Override
public void run() throws Exception {
if (currentProgram == null) {
return;
}
DecompInterface decomp = new DecompInterface();
decomp.openProgram(currentProgram);
decompCache = new DecompileCache(decomp);
staticMemoryBlockStart = currentProgram.getAddressFactory().getAddress("005b6400");
staticMemoryBlockEnd = currentProgram.getAddressFactory().getAddress("00843fff");
@@ -438,10 +632,7 @@ public class DecompileC extends GhidraScript {
println("Output path: " + outputDir.getCanonicalPath());
DecompInterface decomp = new DecompInterface();
decomp.openProgram(currentProgram);
buildFunctionBlacklist(decomp);
buildFunctionBlacklist();
List<Function> functions = new ArrayList<>();
@@ -457,9 +648,9 @@ public class DecompileC extends GhidraScript {
int mode = 1;
if (mode == 0) { // Sanitize symbols
sanitizeGlobalSymbolsPass(decomp, functions);
sanitizeGlobalSymbolsPass(functions);
} else if (mode == 1) { // Decompile all functions
decompileAll(decomp, functions);
decompileAll(functions);
}
}