Skip to content

Instantly share code, notes, and snippets.

@bog-dan-ro
Created October 17, 2023 10:49
Show Gist options
  • Save bog-dan-ro/b34793041ce9f49cb524cf8e9274b0f7 to your computer and use it in GitHub Desktop.
Save bog-dan-ro/b34793041ce9f49cb524cf8e9274b0f7 to your computer and use it in GitHub Desktop.
Ghidra script to fix broken refs
import java.util.ArrayList;
import java.util.List;
import ghidra.app.script.GhidraScript;
import ghidra.program.model.address.*;
import ghidra.program.model.symbol.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.lang.*;
import java.math.BigInteger;
public class NewScript extends GhidraScript {
@Override
public void run() throws Exception {
println("Hello there");
ReferenceManager refMgr = currentProgram.getReferenceManager();
ProgramContext pCtx = currentProgram.getProgramContext();
Listing list = currentProgram.getListing();
// // println("pCtx.getBaseContextRegister() " + pCtx.getBaseContextRegister().toString());
// // for (Register reg : pCtx.getContextRegisters())
// // println("Register " + reg.toString());
// // for (String str : pCtx.getRegisterNames())
// // println("Reg " + str);
// // for (Register reg : pCtx.getRegistersWithValues())
// // println("Register " + reg.toString());
Register ds = pCtx.getRegister("DS");
// Register es = pCtx.getRegister("ES");
// Register ss = pCtx.getRegister("SS");
// Address lastAddr = currentProgram.getMaxAddress();
// FunctionManager fm = currentProgram.getFunctionManager();
// FunctionIterator fit = fm.getFunctions(false);
// while(fit.hasNext()) {
// Function func = fit.next();
// RegisterValue rv = pCtx.getDefaultValue(ds, func.getEntryPoint());
// if (rv == null) {
// pCtx.setValue(ds, func.getEntryPoint(), lastAddr, new java.math.BigInteger("768"));
// }
// lastAddr = func.getEntryPoint();
// }
//
// AddressRangeIterator rit = pCtx.getDefaultRegisterValueAddressRanges​(ds);
// while(rit.hasNext()) {
// AddressRange ar = rit.next();
// println("AddressRange " + ar.toString());
// }
AddressIterator it = refMgr.getReferenceSourceIterator(currentProgram.getMinAddress(), true);
int totalRefs = 0;
List<Reference> badRefs = new ArrayList<Reference>();
class Ref {
Ref(Address from, Address to, int opIdx) {
this.from = from;
this.to = to;
this.opIdx = opIdx;
}
Address from;
Address to;
int opIdx;
}
List<Ref> addRefs = new ArrayList<Ref>();
while(it.hasNext()) {
Address address = it.next();
if (address.getUnsignedOffset() < 0x20000)
continue;
Reference[] refs = refMgr.getReferencesFrom(address);
if (refs.length != 1)
continue;
Reference ref = refs[0];
// println("ref=" + ref.toString());
Address addr = ref.getToAddress();
if (ref.getReferenceType() == RefType.DATA && ref.getSource() == SourceType.ANALYSIS && addr.isMemoryAddress() && addr.getUnsignedOffset() < (0x12060 - 0x3000)) {
addRefs.add(new Ref(ref.getFromAddress(), addr.add(0x3000), ref.getOperandIndex()));
badRefs.add(ref);
totalRefs++;
}
}
for (Reference reference : badRefs) {
refMgr.delete(reference);
}
for (Ref ref : addRefs) {
refMgr.addMemoryReference(ref.from, ref.to, RefType.DATA, SourceType.ANALYSIS, ref.opIdx);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment