WIP setup CRT

This commit is contained in:
2025-05-30 02:02:43 +08:00
parent acb9d47e0e
commit 2c104648cd
15 changed files with 334 additions and 34 deletions

View File

@@ -0,0 +1,28 @@
// Script to refresh all custom globals & types from Ghidra
// @category _Reman3
// @menupath Reman3.Redump Globals and Types
import ghidra.app.script.GhidraScript;
import re3lib.GlobalDumper;
import re3lib.RemanConfig;
import re3lib.TypeDumper;
import re3lib.FunctionDatabase;
public class DumpGlobals extends GhidraScript {
@Override
protected void run() throws Exception {
RemanConfig.INSTANCE = new RemanConfig(this);
RemanConfig.INSTANCE.createDirectories();
try (FunctionDatabase functionDatabase = new FunctionDatabase(this)) {
GlobalDumper globalDumper = new GlobalDumper(this, functionDatabase);
globalDumper.loadGlobalManifest();
globalDumper.dumpGlobals();
globalDumper.saveGlobalManifest();
TypeDumper dumper = new TypeDumper(this);
dumper.run();
}
}
}

View File

@@ -1,6 +1,6 @@
// Exports binary read only and data segments to a binary + header file
// @category _Reman3
// @menupath Tools.Reman3.Export Data
// @menupath Reman3.Export Data Segment
import java.io.File;
import java.io.FileOutputStream;

View File

@@ -625,7 +625,7 @@ public class FunctionDatabase implements AutoCloseable {
public void addGlobal(Address address, String name, String dataType) throws Exception {
ensureConnection();
String filepath = "globals.h"; // Default filepath for globals
String filepath = RemanConfig.GLOBAL_H_FILE; // Default filepath for globals
String addressStr = address.toString();
try {

View File

@@ -58,7 +58,7 @@ public class GlobalDumper {
public void removeGlobalManifest() {
// Remove globals from database instead of file
try {
functionDatabase.removeGlobalsByFilepath("globals.h");
functionDatabase.removeGlobalsByFilepath(RemanConfig.INSTANCE.GLOBAL_H_FILE);
globalAddrs.clear();
} catch (Exception e) {
script.println("Error removing global manifest: " + e.getMessage());
@@ -91,7 +91,13 @@ public class GlobalDumper {
if (type == null) {
script.println("WARNING: Could not reconstruct type for global: " + entry.name + " at " + entry.address);
continue;
type = dtp.parse("undefined4");
}
// Retrieve the name again
Symbol sym = script.getSymbolAt(entry.address);
if (sym != null) {
entry.name = sym.getName();
}
globalAddrs.put(entry.address, new GlobalRec(entry.address, entry.name, type));
@@ -130,14 +136,14 @@ public class GlobalDumper {
}
public void dumpGlobals() throws Exception {
File globalSymbolsListH = new File(RemanConfig.INSTANCE.outputDir, "gh_global.h");
File globalSymbolsListH = new File(RemanConfig.INSTANCE.outputDir, RemanConfig.GLOBAL_H_FILE);
PrintWriter hwriter = new PrintWriter(globalSymbolsListH, "UTF-8");
hwriter.println("// AUTO-GENERATED FILE ");
Utils.headerGuardPre(hwriter, "GLOBALS");
hwriter.println("#include <r3/binders/global.h>");
hwriter.println();
File globalSymbolsListC = new File(RemanConfig.INSTANCE.outputDir, "gh_global.cxx");
File globalSymbolsListC = new File(RemanConfig.INSTANCE.outputDir, RemanConfig.GLOBAL_CXX_FILE);
PrintWriter cwriter = new PrintWriter(globalSymbolsListC, "UTF-8");
cwriter.println("// AUTO-GENERATED FILE ");
cwriter.println("#include <r3/binders/global.h>");
@@ -216,7 +222,7 @@ public class GlobalDumper {
script.println("Saving globals to database");
// Clear existing globals for the default filepath
functionDatabase.removeGlobalsByFilepath("globals.h");
functionDatabase.removeGlobalsByFilepath(RemanConfig.GLOBAL_H_FILE);
// Add all current globals to database
for (GlobalRec global : globalAddrs.values()) {
@@ -226,6 +232,10 @@ public class GlobalDumper {
}
public void addGlobal(Address addr, HighSymbol sym) throws Exception {
if (sym.getName().equals("ExceptionList")) {
return;
}
if (addr.compareTo(RemanConfig.INSTANCE.staticMemoryBlockStart) < 0
|| addr.compareTo(RemanConfig.INSTANCE.staticMemoryBlockEnd) > 0) {
throw new Exception("Global address out of range: " + addr);

View File

@@ -55,8 +55,11 @@ public class RemanConfig {
public static RemanConfig INSTANCE;
public static final String GLOBAL_H_FILE = "gh_global.h"; // Default filepath for globals
public static final String GLOBAL_CXX_FILE = "gh_global.cxx"; // Default filepath for globals
public RemanConfig(GhidraScript script) {
staticMemoryBlockStart = script.getCurrentProgram().getAddressFactory().getAddress("00597000");
staticMemoryBlockStart = script.getCurrentProgram().getAddressFactory().getAddress("00400000");
staticMemoryBlockEnd = script.getCurrentProgram().getAddressFactory().getAddress("00843fff");
this.script = script;