From 2b1fe6c4e12ede777e7243d922cc19f3bddb2b37 Mon Sep 17 00:00:00 2001 From: Teal Bauer Date: Mon, 14 Apr 2025 08:54:18 +0200 Subject: [PATCH] fix: Update XrefsEndpoints.java for Ghidra 11 API compatibility - Fix reference handling using ReferenceIterator - Use proper Ghidra 11 services for current address retrieval - Implement location tracking via LocationService and SelectionService --- .../ghidra/endpoints/XrefsEndpoints.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/starsong/ghidra/endpoints/XrefsEndpoints.java b/src/main/java/eu/starsong/ghidra/endpoints/XrefsEndpoints.java index a5e84ef..ec9c1e7 100644 --- a/src/main/java/eu/starsong/ghidra/endpoints/XrefsEndpoints.java +++ b/src/main/java/eu/starsong/ghidra/endpoints/XrefsEndpoints.java @@ -104,8 +104,9 @@ public class XrefsEndpoints extends AbstractEndpoint { // Get references to this address if (toAddr != null) { - Reference[] refsToArray = refManager.getReferencesTo(toAddr); - for (Reference ref : refsToArray) { + ReferenceIterator refsTo = refManager.getReferencesTo(toAddr); + while (refsTo.hasNext()) { + Reference ref = refsTo.next(); if (refTypeStr != null && !ref.getReferenceType().getName().equalsIgnoreCase(refTypeStr)) { continue; // Skip if type filter doesn't match } @@ -117,8 +118,9 @@ public class XrefsEndpoints extends AbstractEndpoint { // Get references from this address if (fromAddr != null) { - Reference[] refsFromArray = refManager.getReferencesFrom(fromAddr); - for (Reference ref : refsFromArray) { + ReferenceIterator refsFrom = refManager.getReferencesFrom(fromAddr); + while (refsFrom.hasNext()) { + Reference ref = refsFrom.next(); if (refTypeStr != null && !ref.getReferenceType().getName().equalsIgnoreCase(refTypeStr)) { continue; // Skip if type filter doesn't match } @@ -286,19 +288,28 @@ public class XrefsEndpoints extends AbstractEndpoint { } } - // Try to get the address from the current listing + // Try to get the address from the current listing using LocationService ghidra.app.services.ProgramManager programManager = tool.getService(ghidra.app.services.ProgramManager.class); if (programManager != null && programManager.getCurrentProgram() == program) { - ghidra.program.util.ProgramSelection selection = programManager.getCurrentSelection(); - if (selection != null && !selection.isEmpty()) { - return selection.getMinAddress(); + // In Ghidra 11+, use the current cursor location + ghidra.app.services.LocationService locationService = + tool.getService(ghidra.app.services.LocationService.class); + if (locationService != null) { + ghidra.program.util.ProgramLocation location = locationService.getLocation(); + if (location != null && location.getProgram() == program) { + return location.getAddress(); + } } - // Try the current highlight selection - selection = programManager.getCurrentHighlight(); - if (selection != null && !selection.isEmpty()) { - return selection.getMinAddress(); + // Try selection service as a last resort + ghidra.app.services.SelectionService selectionService = + tool.getService(ghidra.app.services.SelectionService.class); + if (selectionService != null) { + ghidra.program.util.ProgramSelection selection = selectionService.getCurrentSelection(); + if (selection != null && !selection.isEmpty()) { + return selection.getMinAddress(); + } } } } catch (Exception e) {