Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1880_James improve decompiler reference actions

Jamesef08cae2e784
diff --git a/Ghidra/Features/Decompiler/src/main/doc/decompileplugin.xml b/Ghidra/Features/Decompiler/src/main/doc/decompileplugin.xml
index f2529f9e2e..3cd54ce35e 100644
--- a/Ghidra/Features/Decompiler/src/main/doc/decompileplugin.xml
+++ b/Ghidra/Features/Decompiler/src/main/doc/decompileplugin.xml
@@ -4510,20 +4510,21 @@
</para>
</sect3>
<sect3 id="ActionFindReferences">
- <title>Find References to &lt;function&gt;</title>
+ <title>Find References to &lt;function/global variable&gt;</title>
<para>
- Display locations in the Program that reference the <emphasis role="bold">function</emphasis>
- associated with the token under the cursor. Results are collected in a
+ Display locations in the Program that reference the <emphasis role="bold">function</emphasis> or
+ <emphasis role="bold">global variable</emphasis> associated with the token under the cursor.
+ Results are collected in a
<link xlink:href="help/topics/LocationReferencesPlugin/Location_References.html">Location References Dialog</link>,
which displays a row for each reference with its address and other context.
</para>
<para>
- References include both instructions and data referring to the <emphasis>entry point</emphasis>
+ In the case of a function, references include both instructions and data referring to the <emphasis>entry point</emphasis>
address of the function.
</para>
</sect3>
<sect3 id="ActionShowReferences">
- <title>Show References to Address</title>
+ <title>Show References to &lt;address&gt;</title>
<para>
Display locations in the Program that reference the <emphasis role="bold">address</emphasis>
associated with the token under the cursor. Results are collected in a
diff --git a/Ghidra/Features/Decompiler/src/main/help/help/topics/DecompilePlugin/DecompilerWindow.html b/Ghidra/Features/Decompiler/src/main/help/help/topics/DecompilePlugin/DecompilerWindow.html
index c45a94b81c..4dcf3f8ebd 100644
--- a/Ghidra/Features/Decompiler/src/main/help/help/topics/DecompilePlugin/DecompilerWindow.html
+++ b/Ghidra/Features/Decompiler/src/main/help/help/topics/DecompilePlugin/DecompilerWindow.html
@@ -985,22 +985,23 @@
</div>
<div class="sect3">
<div class="titlepage"><div><div><h4 class="title">
-<a name="ActionFindReferences"></a>Find References to &lt;function&gt;</h4></div></div></div>
+<a name="ActionFindReferences"></a>Find References to &lt;function/global variable&gt;</h4></div></div></div>
<p>
- Display locations in the Program that reference the <span class="bold"><strong>function</strong></span>
+ Display locations in the Program that reference the <span class="bold"><strong>function</strong></span> or
+ <span class="bold"><strong>global variable</strong></span>
associated with the token under the cursor. Results are collected in a
<a class="ulink" href="help/topics/LocationReferencesPlugin/Location_References.html" target="_top">Location References Dialog</a>,
which displays a row for each reference with its address and other context.
</p>
<p>
- References include both instructions and data referring to the <span class="emphasis"><em>entry point</em></span>
+ In the case of a function, references include both instructions and data referring to the <span class="emphasis"><em>entry point</em></span>
address of the function.
</p>
</div>
<div class="sect3">
<div class="titlepage"><div><div><h4 class="title">
-<a name="ActionShowReferences"></a>Show References to Address</h4></div></div></div>
+<a name="ActionShowReferences"></a>Show References to &lt;address&gt</h4></div></div></div>
<p>
Display locations in the Program that reference the <span class="bold"><strong>address</strong></span>
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java
index fb7866edf5..31ddd2df18 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerProvider.java
@@ -939,17 +939,17 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter
setGroupInfo(findReferencesAction, searchGroup, subGroupPosition++);
findReferencesAction.getPopupMenuData().setParentMenuGroup(referencesParentGroup);
- FindReferencesToSymbolAction findReferencesToSymbolAction =
- new FindReferencesToSymbolAction();
+ FindReferencesToHighSymbolAction findReferencesToSymbolAction =
+ new FindReferencesToHighSymbolAction();
setGroupInfo(findReferencesToSymbolAction, searchGroup, subGroupPosition++);
findReferencesToSymbolAction.getPopupMenuData().setParentMenuGroup(referencesParentGroup);
addLocalAction(findReferencesToSymbolAction);
- FindReferencesToAddressAction findReferencesToAdressAction =
+ FindReferencesToAddressAction findReferencesToAddressAction =
new FindReferencesToAddressAction(tool, owner);
- setGroupInfo(findReferencesToAdressAction, searchGroup, subGroupPosition++);
- findReferencesToAdressAction.getPopupMenuData().setParentMenuGroup(referencesParentGroup);
- addLocalAction(findReferencesToAdressAction);
+ setGroupInfo(findReferencesToAddressAction, searchGroup, subGroupPosition++);
+ findReferencesToAddressAction.getPopupMenuData().setParentMenuGroup(referencesParentGroup);
+ addLocalAction(findReferencesToAddressAction);
//
// Options
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToAddressAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToAddressAction.java
index d0d7517cdf..7973ef74b0 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToAddressAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToAddressAction.java
@@ -23,6 +23,7 @@ import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.app.plugin.core.navigation.locationreferences.LocationReferencesService;
import ghidra.app.util.HelpTopics;
import ghidra.framework.plugintool.PluginTool;
+import ghidra.program.model.address.Address;
import ghidra.program.util.ProgramLocation;
import ghidra.util.HelpLocation;
@@ -54,6 +55,7 @@ public class FindReferencesToAddressAction extends AbstractFindReferencesToAddre
DecompilerActionContext decompilerContext = (DecompilerActionContext) context;
return decompilerContext.checkActionEnablement(() -> {
+ updateMenuName(decompilerContext.getAddress());
return super.isEnabledForContext(context);
});
}
@@ -65,4 +67,11 @@ public class FindReferencesToAddressAction extends AbstractFindReferencesToAddre
super.actionPerformed(context);
});
}
+
+ private void updateMenuName(Address addr) {
+ String menuName = "Find References to " + addr.toString();
+ MenuData data = getPopupMenuData().cloneData();
+ data.setMenuPath(new String[] { LocationReferencesService.MENU_GROUP, menuName });
+ setPopupMenuData(data);
+ }
}
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToSymbolAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToHighSymbolAction.java
similarity index 54%
rename from Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToSymbolAction.java
rename to Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToHighSymbolAction.java
index 6507a47a32..7717d7fa64 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToSymbolAction.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindReferencesToHighSymbolAction.java
@@ -16,39 +16,39 @@
package ghidra.app.plugin.core.decompile.actions;
import docking.action.MenuData;
+import ghidra.app.decompiler.ClangToken;
import ghidra.app.plugin.core.decompile.DecompilerActionContext;
import ghidra.app.plugin.core.decompile.DecompilerProvider;
import ghidra.app.plugin.core.navigation.locationreferences.LocationReferencesService;
import ghidra.app.util.HelpTopics;
+import ghidra.program.model.listing.Function;
+import ghidra.program.model.pcode.HighSymbol;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.util.LabelFieldLocation;
-import ghidra.util.HelpLocation;
-import ghidra.util.Msg;
+import ghidra.util.*;
/**
- * An action to show all references to the symbol under the cursor in the Decompiler
+ * An action to show all references to the {@link HighSymbol} under the cursor in the Decompiler.
+ * A HighSymbol is a symbol recovered by the decompiler during decompilation and is generally
+ * distinct from a {@link Symbol} stored in the Ghidra database (for more details see the
+ * "HighSymbol" entry in the "Decompiler Concepts" section of the Ghidra help). For this action
+ * to be enabled, the HighSymbol must represent a function or global variable (not a local variable
+ * or a parameter)
*/
-public class FindReferencesToSymbolAction extends AbstractDecompilerAction {
+public class FindReferencesToHighSymbolAction extends AbstractDecompilerAction {
private static final String MENU_ITEM_TEXT = "Find References to";
public static final String NAME = "Find References to Symbol";
- public FindReferencesToSymbolAction() {
+ public FindReferencesToHighSymbolAction() {
super(NAME);
setHelpLocation(new HelpLocation(HelpTopics.DECOMPILER, "ActionFindReferences"));
setPopupMenuData(
new MenuData(new String[] { LocationReferencesService.MENU_GROUP, MENU_ITEM_TEXT }));
}
- private void updateMenuName(Symbol symbol) {
-
- if (symbol == null) {
- return; // not sure if this can happen
- }
-
- String symbolName = symbol.getName(false);
- String menuName = MENU_ITEM_TEXT + ' ' + symbolName;
-
+ private void updateMenuName(String newName) {
+ String menuName = MENU_ITEM_TEXT + ' ' + newName;
MenuData data = getPopupMenuData().cloneData();
data.setMenuPath(new String[] { LocationReferencesService.MENU_GROUP, menuName });
setPopupMenuData(data);
@@ -56,13 +56,22 @@ public class FindReferencesToSymbolAction extends AbstractDecompilerAction {
@Override
protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
- Symbol symbol = getSymbol(context);
- if (symbol == null) {
+ Function function = getFunction(context);
+ if (function != null && !(function instanceof UndefinedFunction)) {
+ updateMenuName(function.getName());
+ return true;
+ }
+ ClangToken token = context.getTokenAtCursor();
+ if (token == null) {
return false;
}
+ HighSymbol highSymbol = findHighSymbolFromToken(token, context.getHighFunction());
- updateMenuName(symbol);
-
+ if (highSymbol == null || highSymbol.getStorage().isBadStorage() ||
+ !highSymbol.isGlobal()) {
+ return false;
+ }
+ updateMenuName(highSymbol.getName());
return true;
}
@@ -76,9 +85,17 @@ public class FindReferencesToSymbolAction extends AbstractDecompilerAction {
"Please add the plugin implementing this service.");
return;
}
-
- Symbol symbol = getSymbol(context);
- LabelFieldLocation location = new LabelFieldLocation(symbol);
+ LabelFieldLocation location = null;
+ Function function = getFunction(context);
+ if (function != null && !(function instanceof UndefinedFunction)) {
+ location = new LabelFieldLocation(function.getSymbol());
+ }
+ else {
+ HighSymbol highSymbol =
+ findHighSymbolFromToken(context.getTokenAtCursor(), context.getHighFunction());
+ location = new LabelFieldLocation(context.getProgram(),
+ highSymbol.getStorage().getMinAddress(), highSymbol.getName());
+ }
DecompilerProvider provider = context.getComponentProvider();
service.showReferencesToLocation(location, provider);
}
diff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerFindReferencesActionTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerFindReferencesActionTest.java
index c403d5f9d7..0f03b1e62e 100644
--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerFindReferencesActionTest.java
+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/AbstractDecompilerFindReferencesActionTest.java
@@ -28,7 +28,7 @@ import docking.widgets.table.threaded.ThreadedTableModel;
import ghidra.app.actions.AbstractFindReferencesDataTypeAction;
import ghidra.app.actions.AbstractFindReferencesToAddressAction;
import ghidra.app.nav.Navigatable;
-import ghidra.app.plugin.core.decompile.actions.FindReferencesToSymbolAction;
+import ghidra.app.plugin.core.decompile.actions.FindReferencesToHighSymbolAction;
import ghidra.app.plugin.core.navigation.locationreferences.LocationReferencesProvider;
import ghidra.app.plugin.core.navigation.locationreferences.LocationReferencesService;
import ghidra.app.services.*;
@@ -54,7 +54,7 @@ public abstract class AbstractDecompilerFindReferencesActionTest extends Abstrac
super.setUp();
findReferencesAction = getAction(decompiler, AbstractFindReferencesDataTypeAction.NAME);
- findReferencesToSymbolAction = getAction(decompiler, FindReferencesToSymbolAction.NAME);
+ findReferencesToSymbolAction = getAction(decompiler, FindReferencesToHighSymbolAction.NAME);
findReferencesToAddressAction =
getAction(decompiler, AbstractFindReferencesToAddressAction.NAME);