Remove global load/save functions
This commit is contained in:
parent
027829659c
commit
836c856258
|
@ -17,7 +17,6 @@ public class DumpCurrentFunction extends GhidraScript {
|
|||
|
||||
try (FunctionDatabase functionDatabase = new FunctionDatabase(this)) {
|
||||
GlobalDumper globalDumper = new GlobalDumper(this, functionDatabase);
|
||||
globalDumper.loadGlobalManifest();
|
||||
FunctionDumper functionDumper = new FunctionDumper(this, functionDatabase, globalDumper);
|
||||
|
||||
Function currentFunction = getFunctionContaining(currentAddress);
|
||||
|
@ -31,7 +30,6 @@ public class DumpCurrentFunction extends GhidraScript {
|
|||
RemanConfig.INSTANCE.touchCMakeTimestamp();
|
||||
|
||||
globalDumper.dumpGlobals();
|
||||
globalDumper.saveGlobalManifest();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,7 +17,6 @@ public class DumpCurrentFunctionFix extends GhidraScript {
|
|||
|
||||
try (FunctionDatabase functionDatabase = new FunctionDatabase(this)) {
|
||||
GlobalDumper globalDumper = new GlobalDumper(this, functionDatabase);
|
||||
globalDumper.loadGlobalManifest();
|
||||
FunctionDumper functionDumper = new FunctionDumper(this, functionDatabase, globalDumper);
|
||||
|
||||
// Force Fix type instead of Auto
|
||||
|
@ -34,7 +33,6 @@ public class DumpCurrentFunctionFix extends GhidraScript {
|
|||
RemanConfig.INSTANCE.touchCMakeTimestamp();
|
||||
|
||||
globalDumper.dumpGlobals();
|
||||
globalDumper.saveGlobalManifest();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,7 +22,6 @@ public class DumpCurrentFunctionRecursive extends GhidraScript {
|
|||
|
||||
try (FunctionDatabase functionDatabase = new FunctionDatabase(this)) {
|
||||
GlobalDumper globalDumper = new GlobalDumper(this, functionDatabase);
|
||||
globalDumper.loadGlobalManifest();
|
||||
|
||||
FunctionDumper functionDumper = new FunctionDumper(this, functionDatabase, globalDumper);
|
||||
|
||||
|
@ -70,7 +69,6 @@ public class DumpCurrentFunctionRecursive extends GhidraScript {
|
|||
RemanConfig.INSTANCE.touchCMakeTimestamp();
|
||||
|
||||
globalDumper.dumpGlobals();
|
||||
globalDumper.saveGlobalManifest();
|
||||
}
|
||||
|
||||
// Dump types
|
||||
|
|
|
@ -16,10 +16,8 @@ public class DumpGlobals extends GhidraScript {
|
|||
|
||||
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();
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
import ghidra.app.script.GhidraScript;
|
||||
import re3lib.GlobalDumper;
|
||||
import re3lib.RemanConfig;
|
||||
import re3lib.FunctionDatabase;
|
||||
|
||||
public class SanitizeGlobalSymbols extends GhidraScript {
|
||||
@Override
|
||||
|
@ -12,12 +13,12 @@ public class SanitizeGlobalSymbols extends GhidraScript {
|
|||
RemanConfig.INSTANCE = new RemanConfig(this);
|
||||
RemanConfig.INSTANCE.createDirectories();
|
||||
|
||||
GlobalDumper globalDumper = new GlobalDumper(this);
|
||||
globalDumper.loadGlobalManifest();
|
||||
try (FunctionDatabase functionDatabase = new FunctionDatabase(this)) {
|
||||
GlobalDumper globalDumper = new GlobalDumper(this, functionDatabase);
|
||||
|
||||
globalDumper.sanitizeGlobalSymbols();
|
||||
globalDumper.sanitizeGlobalSymbols();
|
||||
|
||||
globalDumper.dumpGlobals();
|
||||
globalDumper.saveGlobalManifest();
|
||||
globalDumper.dumpGlobals();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -673,7 +673,6 @@ public class FunctionDatabase implements AutoCloseable {
|
|||
// Update CMake timestamp
|
||||
RemanConfig.INSTANCE.touchCMakeTimestamp();
|
||||
globalDumper.dumpGlobals();
|
||||
globalDumper.saveGlobalManifest();
|
||||
|
||||
TypeDumper typeDumper = new TypeDumper(script);
|
||||
typeDumper.run();
|
||||
|
|
|
@ -48,7 +48,6 @@ public class GlobalDumper {
|
|||
|
||||
GhidraScript script;
|
||||
FunctionDatabase functionDatabase;
|
||||
HashMap<Address, GlobalRec> globalAddrs = new HashMap<>();
|
||||
|
||||
public GlobalDumper(GhidraScript script, FunctionDatabase functionDatabase) {
|
||||
this.script = script;
|
||||
|
@ -56,57 +55,14 @@ public class GlobalDumper {
|
|||
}
|
||||
|
||||
public void removeGlobalManifest() {
|
||||
// Remove globals from database instead of file
|
||||
// Remove globals from database
|
||||
try {
|
||||
functionDatabase.removeGlobalsByFilepath(RemanConfig.INSTANCE.GLOBAL_H_FILE);
|
||||
globalAddrs.clear();
|
||||
} catch (Exception e) {
|
||||
script.println("Error removing global manifest: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public boolean loadGlobalManifest() throws Exception {
|
||||
globalAddrs.clear();
|
||||
|
||||
// Load globals from database
|
||||
List<FunctionDatabase.GlobalEntry> dbGlobals = functionDatabase.loadAllGlobals();
|
||||
|
||||
// Get the dataTypeManagerService for parsing types
|
||||
DataTypeManagerService dataTypeManagerService = (DataTypeManagerService) script.getState().getTool()
|
||||
.getService(DataTypeManagerService.class);
|
||||
DataTypeManager dtm = script.getCurrentProgram().getDataTypeManager();
|
||||
DataTypeParser dtp = new DataTypeParser(dataTypeManagerService, AllowedDataTypes.ALL);
|
||||
|
||||
for (FunctionDatabase.GlobalEntry entry : dbGlobals) {
|
||||
// Note: The database stores type as string, need to reconstruct DataType
|
||||
// For now, we'll parse it back from the type string stored in database
|
||||
// This is a limitation of moving from the manifest format
|
||||
DataType type = null;
|
||||
|
||||
// Try to get from existing data at address
|
||||
Data data = script.getDataAt(entry.address);
|
||||
if (data != null) {
|
||||
type = data.getDataType();
|
||||
}
|
||||
|
||||
if (type == null) {
|
||||
script.println("WARNING: Could not reconstruct type for global: " + entry.name + " at " + entry.address);
|
||||
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));
|
||||
}
|
||||
|
||||
script.println("Loaded " + globalAddrs.size() + " globals from database");
|
||||
return !globalAddrs.isEmpty();
|
||||
}
|
||||
|
||||
String escapeCString(String str) {
|
||||
str = str.replace("\\", "\\\\");
|
||||
str = str.replace("\"", "\\\"");
|
||||
|
@ -149,7 +105,39 @@ public class GlobalDumper {
|
|||
cwriter.println("#include <r3/binders/global.h>");
|
||||
hwriter.println();
|
||||
|
||||
List<GlobalRec> globals = new ArrayList<>(globalAddrs.values());
|
||||
// Load globals directly from database
|
||||
List<FunctionDatabase.GlobalEntry> dbGlobals = functionDatabase.loadAllGlobals();
|
||||
List<GlobalRec> globals = new ArrayList<>();
|
||||
|
||||
// Convert database entries to GlobalRec objects
|
||||
for (FunctionDatabase.GlobalEntry entry : dbGlobals) {
|
||||
DataType type = null;
|
||||
|
||||
// Try to get from existing data at address
|
||||
Data data = script.getDataAt(entry.address);
|
||||
if (data != null) {
|
||||
type = data.getDataType();
|
||||
}
|
||||
|
||||
if (type == null) {
|
||||
script.println("WARNING: Could not determine type for global: " + entry.name + " at " + entry.address);
|
||||
// Get the dataTypeManagerService for parsing types
|
||||
DataTypeManagerService dataTypeManagerService = (DataTypeManagerService) script.getState().getTool()
|
||||
.getService(DataTypeManagerService.class);
|
||||
DataTypeParser dtp = new DataTypeParser(dataTypeManagerService, AllowedDataTypes.ALL);
|
||||
type = dtp.parse("undefined4");
|
||||
}
|
||||
|
||||
// Retrieve the current symbol name (may have changed since stored)
|
||||
Symbol sym = script.getSymbolAt(entry.address);
|
||||
String currentName = entry.name;
|
||||
if (sym != null) {
|
||||
currentName = sym.getName();
|
||||
}
|
||||
|
||||
globals.add(new GlobalRec(entry.address, currentName, type));
|
||||
}
|
||||
|
||||
globals.sort((o1, o2) -> o1.address.compareTo(o2.address));
|
||||
|
||||
for (GlobalRec global : globals) {
|
||||
|
@ -217,19 +205,6 @@ public class GlobalDumper {
|
|||
cwriter.close();
|
||||
}
|
||||
|
||||
public void saveGlobalManifest() throws Exception {
|
||||
// Save globals to database instead of file
|
||||
script.println("Saving globals to database");
|
||||
|
||||
// Clear existing globals for the default filepath
|
||||
functionDatabase.removeGlobalsByFilepath(RemanConfig.GLOBAL_H_FILE);
|
||||
|
||||
// Add all current globals to database
|
||||
for (GlobalRec global : globalAddrs.values()) {
|
||||
functionDatabase.addGlobal(global.address, global.name);
|
||||
}
|
||||
}
|
||||
|
||||
public void addGlobal(Address addr, HighSymbol sym) throws Exception {
|
||||
if (sym.getName().equals("ExceptionList")) {
|
||||
return;
|
||||
|
@ -257,23 +232,30 @@ public class GlobalDumper {
|
|||
}
|
||||
// script.println("Global: " + addr + " - " + sym.getName() + " - " +
|
||||
// dt.getDisplayName());
|
||||
globalAddrs.put(addr, new GlobalRec(addr, sym.getName(), dt));
|
||||
|
||||
// Add directly to database instead of storing in memory
|
||||
functionDatabase.addGlobal(addr, sym.getName());
|
||||
}
|
||||
|
||||
public void sanitizeGlobalSymbols() {
|
||||
for (GlobalRec global : globalAddrs.values()) {
|
||||
String sanitizedName = Utils.sanitizeIdentifier(global.name);
|
||||
if (!sanitizedName.equals(global.name)) {
|
||||
Symbol symbol = script.getSymbolAt(global.address);
|
||||
public void sanitizeGlobalSymbols() throws Exception {
|
||||
// Load globals from database, sanitize symbol names, and update both Ghidra symbols and database
|
||||
List<FunctionDatabase.GlobalEntry> dbGlobals = functionDatabase.loadAllGlobals();
|
||||
|
||||
for (FunctionDatabase.GlobalEntry entry : dbGlobals) {
|
||||
String sanitizedName = Utils.sanitizeIdentifier(entry.name);
|
||||
if (!sanitizedName.equals(entry.name)) {
|
||||
Symbol symbol = script.getSymbolAt(entry.address);
|
||||
if (symbol != null) {
|
||||
script.println("Renaming global symbol: " + global.name + " -> " + sanitizedName);
|
||||
AddLabelCmd cmd = new AddLabelCmd(global.address, sanitizedName,
|
||||
script.println("Renaming global symbol: " + entry.name + " -> " + sanitizedName);
|
||||
AddLabelCmd cmd = new AddLabelCmd(entry.address, sanitizedName,
|
||||
symbol.getParentNamespace(),
|
||||
SourceType.USER_DEFINED);
|
||||
if (!cmd.applyTo(script.getCurrentProgram())) {
|
||||
script.println("Error renaming symbol: " + cmd.getStatusMsg());
|
||||
} else {
|
||||
// Update the database with the new name
|
||||
functionDatabase.addGlobal(entry.address, sanitizedName);
|
||||
}
|
||||
global.name = sanitizedName;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue