mirror of
https://codeberg.org/KeybadeBlox/JSRF-Decompilation.git
synced 2026-02-20 02:07:02 +03:00
Mangle referenced symbols in mangling script
This produces correct symbol names in disassembly in objdiff.
This commit is contained in:
parent
0e84f9ab1f
commit
05b4da2f78
1 changed files with 54 additions and 25 deletions
|
|
@ -19,6 +19,7 @@
|
|||
// @category Symbol
|
||||
|
||||
import ghidra.app.script.GhidraScript;
|
||||
import ghidra.program.flatapi.FlatProgramAPI;
|
||||
import ghidra.program.model.address.Address;
|
||||
import ghidra.program.model.address.AddressSet;
|
||||
import ghidra.program.model.data.Array;
|
||||
|
|
@ -53,6 +54,7 @@ import ghidra.program.model.data.WideCharDataType;
|
|||
import ghidra.program.model.listing.Data;
|
||||
import ghidra.program.model.listing.Function;
|
||||
import ghidra.program.model.listing.FunctionSignature;
|
||||
import ghidra.program.model.listing.Instruction;
|
||||
import ghidra.program.model.symbol.Namespace;
|
||||
import ghidra.program.model.symbol.Reference;
|
||||
import ghidra.program.model.symbol.SourceType;
|
||||
|
|
@ -74,16 +76,12 @@ public class MSVC7Mangle extends GhidraScript{
|
|||
public void run() throws Exception {
|
||||
// Get selected ranges from arguments if invoked headless
|
||||
if (isRunningHeadless()) {
|
||||
final String[] args = getScriptArgs();
|
||||
final AddressSet addr = new AddressSet();
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
final String[] range = args[i].split("-");
|
||||
addr.add(
|
||||
currentAddress.getAddress(range[0]),
|
||||
currentAddress.getAddress(range[1])
|
||||
);
|
||||
}
|
||||
Arrays.stream(getScriptArgs()).forEach(arg -> {
|
||||
final String[] range = arg.split("-");
|
||||
addr.add(toAddr(range[0]), toAddr(range[1]));
|
||||
});
|
||||
|
||||
setCurrentSelection(addr);
|
||||
}
|
||||
|
|
@ -94,6 +92,30 @@ public class MSVC7Mangle extends GhidraScript{
|
|||
while (iter.hasNext() && !monitor.isCancelled()) {
|
||||
final Symbol s = iter.next();
|
||||
|
||||
mangle(s);
|
||||
|
||||
if (
|
||||
isRunningHeadless() &&
|
||||
s.getObject() instanceof Function f
|
||||
) {
|
||||
// Also mangle everything referenced inside f
|
||||
for (
|
||||
Instruction ins = getFirstInstruction(f);
|
||||
ins != null && f.getBody().contains(ins.getAddress());
|
||||
ins = ins.getNext()
|
||||
) {
|
||||
final Reference[] refs = ins.getReferencesFrom();
|
||||
for (int i = 0; i < refs.length; i++) {
|
||||
final Symbol symbol = getSymbolAt(refs[i].getToAddress());
|
||||
if (symbol != null) mangle(symbol);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void mangle(final Symbol s) throws Exception {
|
||||
/* Set the given symbol's name to its mangled version */
|
||||
// Skip if already mangled
|
||||
if (s.getName().charAt(0) == '?') return;
|
||||
|
||||
|
|
@ -108,17 +130,24 @@ public class MSVC7Mangle extends GhidraScript{
|
|||
if (mangled != null) {
|
||||
s.setName(mangled, SourceType.USER_DEFINED);
|
||||
makeGlobal(s);
|
||||
}
|
||||
|
||||
// TODO: in headless mode, also mangle everything
|
||||
// referenced by functions
|
||||
if (s.getObject() instanceof Function f) {
|
||||
// Also apply to target function if f is thunk
|
||||
final Function thunked = f.getThunkedFunction(true);
|
||||
if (thunked != null) {
|
||||
final Symbol ts = thunked.getSymbol();
|
||||
ts.setName(mangled, SourceType.USER_DEFINED);
|
||||
makeGlobal(ts);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String mangleFn(final Function f) throws Exception {
|
||||
/* Generate a mangled name for a function */
|
||||
// Special case for main()
|
||||
// Special cases for main()
|
||||
if (f.getName(true).equals("main" )) return "_main";
|
||||
if (f.getName(true).equals("___CxxFrameHandler")) return "___CxxFrameHandler";
|
||||
|
||||
final ArrayList<String> dict = new ArrayList<>();
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue