From 8a96cdce6a5e3eeb91d2a9225bb5e7f3b7cc87e8 Mon Sep 17 00:00:00 2001 From: Mike Hunhoff Date: Mon, 18 Mar 2024 16:32:44 -0600 Subject: [PATCH] Extend Ghidra exporter to include data references --- .../security/binexport/BinExport2Builder.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/java/src/main/java/com/google/security/binexport/BinExport2Builder.java b/java/src/main/java/com/google/security/binexport/BinExport2Builder.java index 2692507f..245ba94c 100644 --- a/java/src/main/java/com/google/security/binexport/BinExport2Builder.java +++ b/java/src/main/java/com/google/security/binexport/BinExport2Builder.java @@ -89,6 +89,10 @@ public BinExport2Builder setPrependNamespace(boolean isPrepended) { return this; } + private Address getAddressFromLong(long offset) { + return program.getAddressFactory().getDefaultAddressSpace().getAddress(offset); + } + private long getMappedAddress(Address address) { return address.getOffset() - addressOffset; } @@ -407,6 +411,23 @@ private void buildCallGraph() throws CancelledException { } } + private void buildDataReferences(Map instructionIndices) { + monitor.setMessage("Exporting data references"); + monitor.setIndeterminate(true); + + for (Map.Entry insnIndex : instructionIndices.entrySet()) { + Address addr = getAddressFromLong(insnIndex.getKey()); + for (Reference ref : program.getReferenceManager().getReferencesFrom(addr)) { + if (ref.isMemoryReference() && ref.getReferenceType().isData()) { + builder + .addDataReferenceBuilder() + .setInstructionIndex(insnIndex.getValue()) + .setAddress(ref.getToAddress().getOffset()); + } + } + } + } + private void buildSections() { monitor.setMessage("Exporting sections"); monitor.setIndeterminate(false); @@ -532,7 +553,7 @@ public BinExport2 build(TaskMonitor taskMonitor) throws CancelledException { // TODO(cblichmann): Implement these: // buildComments() // buildStrings(); - // buildDataReferences() + buildDataReferences(instructionIndices); monitor.setMessage("Exporting flow graphs"); buildFlowGraphs(basicBlockIndices); monitor.setMessage("Exporting call graph");