Ghidra is a software reverse engineering (SRE) framework
GP-1891 - fixed incorrect xrefs being shown when double-clicking the 'XREF' field in the listing for a function variable
dragonmacherfb7db7e72d71
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.javaindex bbfb99207c..9fc62c35bd 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/XReferenceUtils.java@@ -25,12 +25,14 @@ import ghidra.program.model.address.*;import ghidra.program.model.data.DataUtilities;import ghidra.program.model.listing.*;import ghidra.program.model.symbol.*;-import ghidra.program.util.ProgramLocation;+import ghidra.program.util.*;import ghidra.util.table.ReferencesFromTableModel;import ghidra.util.table.field.ReferenceEndpoint;public class XReferenceUtils {+ private static final String X_REFS_TO = "XRefs to ";+// Methods in this class treat -1 as a key to return all references and// not cap the result set.private final static int ALL_REFS = -1;@@ -38,10 +40,10 @@ public class XReferenceUtils {/*** Returns an array containing the first <b><code>max</code></b>* direct xref references to the specified code unit.- *+ ** @param cu the code unit to generate the xrefs* @param max max number of xrefs to get, or -1 to get all references- *+ ** @return array first <b><code>max</code></b> xrefs to the code unit*/public final static List<Reference> getXReferences(CodeUnit cu, int max) {@@ -78,7 +80,7 @@ public class XReferenceUtils {/*** Returns an array containing all offcut xref references to the specified code unit- *+ ** @param cu the code unit to generate the offcut xrefs* @param max max number of offcut xrefs to get, or -1 to get all offcut references* @return array of all offcut xrefs to the code unit@@ -115,7 +117,7 @@ public class XReferenceUtils {/*** Populates the provided lists with the direct and offcut xrefs to the specified variable- *+ ** @param var variable to get references* @param xrefs list to put direct references in* @param offcuts list to put offcut references in@@ -127,14 +129,14 @@ public class XReferenceUtils {/*** Populates the provided lists with the direct and offcut xrefs to the specified variable- *+ ** @param var variable to get references* @param xrefs list to put direct references in* @param offcuts list to put offcut references in* @param max max number of xrefs to get, or -1 to get all references*/- public static void getVariableRefs(Variable var, List<Reference> xrefs,- List<Reference> offcuts, int max) {+ public static void getVariableRefs(Variable var, List<Reference> xrefs, List<Reference> offcuts,+ int max) {Address addr = var.getMinAddress();if (addr == null) {@@ -163,7 +165,7 @@ public class XReferenceUtils {* Returns all xrefs to the given location. If in data, then xrefs to the specific data* component will be returned. Otherwise, the code unit containing the address of the* given location will be used as the source of the xrefs.- *+ ** @param location the location for which to get xrefs* @return the xrefs*/@@ -192,7 +194,7 @@ public class XReferenceUtils {/*** Shows all xrefs to the given location in a new table.- *+ ** @param navigatable the navigatable used for navigation from the table* @param serviceProvider the service provider needed to wire navigation* @param service the service needed to show the table@@ -202,11 +204,41 @@ public class XReferenceUtils {public static void showXrefs(Navigatable navigatable, ServiceProvider serviceProvider,TableService service, ProgramLocation location, Collection<Reference> xrefs) {- ReferencesFromTableModel model =- new ReferencesFromTableModel(new ArrayList<>(xrefs), serviceProvider,- location.getProgram());- TableComponentProvider<ReferenceEndpoint> provider = service.showTable(- "XRefs to " + location.getAddress().toString(), "XRefs", model, "XRefs", navigatable);+ ReferencesFromTableModel model = new ReferencesFromTableModel(new ArrayList<>(xrefs),+ serviceProvider, location.getProgram());++ String title = generateXRefTitle(location);+ TableComponentProvider<ReferenceEndpoint> provider =+ service.showTable(title, "XRefs", model, "XRefs", navigatable);provider.installRemoveItemsAction();}++ private static String generateXRefTitle(ProgramLocation location) {++ // note: we likely need to improve this title generation as we find more specific needs+ Program program = location.getProgram();+ FunctionManager functionManager = program.getFunctionManager();+ Address address = location.getAddress();+ if (location instanceof VariableLocation) {+ VariableLocation vl = (VariableLocation) location;+ String name = vl.getVariable().getName();+ Function f = functionManager.getFunctionContaining(vl.getFunctionAddress());+ return X_REFS_TO + name + (f == null ? "" : " in " + f.getName());+ }+ else if (location instanceof FunctionLocation) {+ FunctionLocation fl = (FunctionLocation) location;+ Function f = functionManager.getFunctionContaining(fl.getFunctionAddress());+ if (f != null) {+ return X_REFS_TO + f.getName();+ }+ }+ else {+ Function f = functionManager.getFunctionAt(address);+ if (f != null) {+ return X_REFS_TO + f.getName();+ }+ }++ return X_REFS_TO + location.getAddress();+ }}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldMouseHandler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldMouseHandler.javaindex 52d121c1a5..ee7698a202 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldMouseHandler.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/VariableXRefFieldMouseHandler.java@@ -34,33 +34,14 @@ import ghidra.program.util.*;*/public class VariableXRefFieldMouseHandler extends XRefFieldMouseHandler {- private final static Class<?>[] SUPPORTED_CLASSES = new Class<?>[] {- VariableXRefFieldLocation.class, VariableXRefHeaderFieldLocation.class };-- @Override- protected Address getToReferenceAddress(ProgramLocation programLocation, Program program) {- Variable variable = ((VariableLocation) programLocation).getVariable();- return variable.getMinAddress();- }+ private final static Class<?>[] SUPPORTED_CLASSES =+ new Class<?>[] { VariableXRefFieldLocation.class, VariableXRefHeaderFieldLocation.class };@Overrideprotected Address getFromReferenceAddress(ProgramLocation programLocation) {return ((VariableXRefFieldLocation) programLocation).getRefAddress();}- @Override- protected ProgramLocation getReferredToLocation(Navigatable navigatable,- ProgramLocation location) {- VariableLocation variableLocation = (VariableLocation) location;- Variable variable = variableLocation.getVariable();- return new VariableNameFieldLocation(variable.getProgram(), variable, 0);- }-- @Override- protected int getIndex(ProgramLocation programLocation) {- return ((VariableXRefFieldLocation) programLocation).getIndex();- }-@Overridepublic Class<?>[] getSupportedProgramLocations() {return SUPPORTED_CLASSES;@@ -71,6 +52,7 @@ public class VariableXRefFieldMouseHandler extends XRefFieldMouseHandler {return location instanceof VariableXRefHeaderFieldLocation;}+ @Overrideprotected void showXRefDialog(Navigatable navigatable, ProgramLocation location,ServiceProvider serviceProvider) {TableService service = serviceProvider.getService(TableService.class);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldMouseHandler.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldMouseHandler.javaindex 5f4b52f883..ff81d2e2ec 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldMouseHandler.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/XRefFieldMouseHandler.java@@ -26,7 +26,6 @@ import ghidra.app.util.XReferenceUtils;import ghidra.app.util.query.TableService;import ghidra.framework.plugintool.ServiceProvider;import ghidra.program.model.address.Address;-import ghidra.program.model.listing.Program;import ghidra.program.model.symbol.Reference;import ghidra.program.util.*;@@ -85,20 +84,11 @@ public class XRefFieldMouseHandler implements FieldMouseHandlerExtension {return clickedObject.toString();}- // the unused parameter is needed for overridden method in subclass- protected Address getToReferenceAddress(ProgramLocation programLocation, Program program) {- return programLocation.getAddress();- }-protected Address getFromReferenceAddress(ProgramLocation programLocation) {return ((XRefFieldLocation) programLocation).getRefAddress();}- protected int getIndex(ProgramLocation programLocation) {- return ((XRefFieldLocation) programLocation).getIndex();- }-- private void showXRefDialog(Navigatable navigatable, ProgramLocation location,+ protected void showXRefDialog(Navigatable navigatable, ProgramLocation location,ServiceProvider serviceProvider) {TableService service = serviceProvider.getService(TableService.class);if (service == null) {@@ -109,12 +99,6 @@ public class XRefFieldMouseHandler implements FieldMouseHandlerExtension {XReferenceUtils.showXrefs(navigatable, serviceProvider, service, location, refs);}- protected ProgramLocation getReferredToLocation(Navigatable sourceNavigatable,- ProgramLocation location) {- Program program = sourceNavigatable.getProgram();- return new CodeUnitLocation(program, getToReferenceAddress(location, program), 0, 0, 0);- }-private boolean goTo(Navigatable navigatable, Address referencedAddress,GoToService goToService) {if (referencedAddress != null) {