Ghidra is a software reverse engineering (SRE) framework
Reorganize decompile GUI actions
caheckman885402bbc361
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.javaindex c1896b914e..57a21c1bb3 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/component/DecompilerUtils.java@@ -680,7 +680,7 @@ public class DecompilerUtils {// prefer the selection over the current locationClangToken token = decompilerPanel.getSelectedToken();if (token == null) {- token = decompilerPanel.getTokenAtCursor();+ token = context.getTokenAtCursor();}Varnode varnode = DecompilerUtils.getVarnodeRef(token);diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.javaindex 5be4eca9c6..76163df5e3 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/DecompilerActionContext.java@@ -21,9 +21,13 @@ import docking.ActionContext;import docking.action.DockingActionIf;import ghidra.app.context.NavigatableActionContext;import ghidra.app.context.RestrictedAddressSetContext;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.decompiler.ClangTokenGroup;import ghidra.app.decompiler.component.DecompilerPanel;+import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Function;+import ghidra.program.model.pcode.HighFunction;import ghidra.util.Msg;import ghidra.util.UndefinedFunction;import utility.function.Callback;@@ -33,6 +37,9 @@ public class DecompilerActionContext extends NavigatableActionContextprivate final Address functionEntryPoint;private final boolean isDecompiling;+ private ClangToken tokenAtCursor = null;+ private boolean tokenIsInitialized = false;+public DecompilerActionContext(DecompilerProvider provider, Address functionEntryPoint,boolean isDecompiling) {super(provider, provider);@@ -53,6 +60,18 @@ public class DecompilerActionContext extends NavigatableActionContextreturn (DecompilerProvider) super.getComponentProvider();}+ public PluginTool getTool() {+ return getComponentProvider().getTool();+ }++ public ClangToken getTokenAtCursor() {+ if (!tokenIsInitialized) {+ tokenAtCursor = getDecompilerPanel().getTokenAtCursor();+ tokenIsInitialized = true;+ }+ return tokenAtCursor;+ }+public DecompilerPanel getDecompilerPanel() {return getComponentProvider().getDecompilerPanel();}@@ -61,11 +80,23 @@ public class DecompilerActionContext extends NavigatableActionContextreturn getComponentProvider().getController().getFunction();}+ public HighFunction getHighFunction() {+ return getComponentProvider().getController().getHighFunction();+ }++ public ClangTokenGroup getCCodeModel() {+ return getComponentProvider().getController().getCCodeModel();+ }+public boolean hasRealFunction() {Function f = getFunction();return f != null && !(f instanceof UndefinedFunction);}+ public void setStatusMessage(String msg) {+ getComponentProvider().getController().setStatusMessage(msg);+ }+/*** The companion method of {@link #checkActionEnablement(Supplier)}. Decompiler actions* must call this method from their {@link DockingActionIf#actionPerformed(ActionContext)}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.javaindex b55588dfa4..c30bfd0a75 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@@ -68,8 +68,13 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterprivate DockingAction backwardSliceToOpsAction;private DockingAction lockProtoAction;private DockingAction lockLocalAction;- private DockingAction renameVarAction;- private DockingAction retypeVarAction;+ private DockingAction renameLocalAction;+ private DockingAction renameGlobalAction;+ private DockingAction renameFieldAction;+ private DockingAction retypeLocalAction;+ private DockingAction retypeGlobalAction;+ private DockingAction retypeReturnAction;+ private DockingAction retypeFieldAction;private DockingAction isolateVarAction;private DockingAction specifyCProtoAction;private DockingAction overrideSigAction;@@ -755,16 +760,16 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterString functionGroup = "1 - Function Group";int subGroupPosition = 0;- specifyCProtoAction = new SpecifyCPrototypeAction(tool, controller);+ specifyCProtoAction = new SpecifyCPrototypeAction();setGroupInfo(specifyCProtoAction, functionGroup, subGroupPosition++);- overrideSigAction = new OverridePrototypeAction(tool, controller);+ overrideSigAction = new OverridePrototypeAction();setGroupInfo(overrideSigAction, functionGroup, subGroupPosition++);- deleteSigAction = new DeletePrototypeOverrideAction(tool, controller);+ deleteSigAction = new DeletePrototypeOverrideAction();setGroupInfo(deleteSigAction, functionGroup, subGroupPosition++);- renameFunctionAction = new RenameFunctionAction(tool, controller);+ renameFunctionAction = new RenameFunctionAction();setGroupInfo(renameFunctionAction, functionGroup, subGroupPosition++);//@@ -773,20 +778,35 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterString variableGroup = "2 - Variable Group";subGroupPosition = 0; // reset for the next group- renameVarAction = new RenameVariableAction(tool, controller);- setGroupInfo(renameVarAction, variableGroup, subGroupPosition++);+ renameLocalAction = new RenameLocalAction();+ setGroupInfo(renameLocalAction, variableGroup, subGroupPosition++);- retypeVarAction = new RetypeVariableAction(tool, controller);- setGroupInfo(retypeVarAction, variableGroup, subGroupPosition++);+ renameGlobalAction = new RenameGlobalAction();+ setGroupInfo(renameGlobalAction, variableGroup, subGroupPosition++);- isolateVarAction = new IsolateVariableAction(tool, controller);+ renameFieldAction = new RenameFieldAction();+ setGroupInfo(renameFieldAction, variableGroup, subGroupPosition++);++ retypeLocalAction = new RetypeLocalAction();+ setGroupInfo(retypeLocalAction, variableGroup, subGroupPosition++);++ retypeGlobalAction = new RetypeGlobalAction();+ setGroupInfo(retypeGlobalAction, variableGroup, subGroupPosition++);++ retypeReturnAction = new RetypeReturnAction();+ setGroupInfo(retypeReturnAction, variableGroup, subGroupPosition++);++ retypeFieldAction = new RetypeFieldAction();+ setGroupInfo(retypeFieldAction, variableGroup, subGroupPosition++);++ isolateVarAction = new IsolateVariableAction();setGroupInfo(isolateVarAction, variableGroup, subGroupPosition++);decompilerCreateStructureAction =new DecompilerStructureVariableAction(owner, tool, controller);setGroupInfo(decompilerCreateStructureAction, variableGroup, subGroupPosition++);- editDataTypeAction = new EditDataTypeAction(tool, controller);+ editDataTypeAction = new EditDataTypeAction();setGroupInfo(editDataTypeAction, variableGroup, subGroupPosition++);//@@ -800,10 +820,10 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterString commitGroup = "3 - Commit Group";subGroupPosition = 0; // reset for the next group- lockProtoAction = new CommitParamsAction(tool, controller);+ lockProtoAction = new CommitParamsAction();setGroupInfo(lockProtoAction, commitGroup, subGroupPosition++);- lockLocalAction = new CommitLocalsAction(tool, controller);+ lockLocalAction = new CommitLocalsAction();setGroupInfo(lockLocalAction, commitGroup, subGroupPosition++);subGroupPosition = 0; // reset for the next group@@ -813,19 +833,19 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter//String highlightGroup = "4a - Highlight Group";tool.setMenuGroup(new String[] { "Highlight" }, highlightGroup);- defUseHighlightAction = new HighlightDefinedUseAction(controller);+ defUseHighlightAction = new HighlightDefinedUseAction();setGroupInfo(defUseHighlightAction, highlightGroup, subGroupPosition++);- forwardSliceAction = new ForwardSliceAction(controller);+ forwardSliceAction = new ForwardSliceAction();setGroupInfo(forwardSliceAction, highlightGroup, subGroupPosition++);backwardSliceAction = new BackwardsSliceAction();setGroupInfo(backwardSliceAction, highlightGroup, subGroupPosition++);- forwardSliceToOpsAction = new ForwardSliceToPCodeOpsAction(controller);+ forwardSliceToOpsAction = new ForwardSliceToPCodeOpsAction();setGroupInfo(forwardSliceToOpsAction, highlightGroup, subGroupPosition++);- backwardSliceToOpsAction = new BackwardsSliceToPCodeOpsAction(controller);+ backwardSliceToOpsAction = new BackwardsSliceToPCodeOpsAction();setGroupInfo(backwardSliceToOpsAction, highlightGroup, subGroupPosition++);tool.setMenuGroup(new String[] { "Secondary Highlight" }, highlightGroup);@@ -854,7 +874,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterString searchGroup = "comment2 - Search Group";subGroupPosition = 0; // reset for the next group- findAction = new FindAction(tool, controller);+ findAction = new FindAction();setGroupInfo(findAction, searchGroup, subGroupPosition++);//@@ -869,7 +889,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterfindReferencesAction.getPopupMenuData().setParentMenuGroup(referencesParentGroup);FindReferencesToSymbolAction findReferencesToSymbolAction =- new FindReferencesToSymbolAction(tool);+ new FindReferencesToSymbolAction();setGroupInfo(findReferencesToSymbolAction, searchGroup, subGroupPosition++);findReferencesToSymbolAction.getPopupMenuData().setParentMenuGroup(referencesParentGroup);addLocalAction(findReferencesToSymbolAction);@@ -893,8 +913,8 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapter// These actions are not in the popup menu//debugFunctionAction = new DebugDecompilerAction(controller);- convertAction = new ExportToCAction(controller);- cloneDecompilerAction = new CloneDecompilerAction(this, controller);+ convertAction = new ExportToCAction();+ cloneDecompilerAction = new CloneDecompilerAction();addLocalAction(refreshAction);addLocalAction(selectAllAction);@@ -905,12 +925,17 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapteraddLocalAction(backwardSliceToOpsAction);addLocalAction(lockProtoAction);addLocalAction(lockLocalAction);- addLocalAction(renameVarAction);+ addLocalAction(renameLocalAction);+ addLocalAction(renameGlobalAction);+ addLocalAction(renameFieldAction);addLocalAction(setSecondaryHighlightAction);addLocalAction(setSecondaryHighlightColorChooserAction);addLocalAction(removeSecondaryHighlightAction);addLocalAction(removeAllSecondadryHighlightsAction);- addLocalAction(retypeVarAction);+ addLocalAction(retypeLocalAction);+ addLocalAction(retypeGlobalAction);+ addLocalAction(retypeReturnAction);+ addLocalAction(retypeFieldAction);addLocalAction(isolateVarAction);addLocalAction(decompilerCreateStructureAction);tool.addAction(listingCreateStructureAction);@@ -948,7 +973,7 @@ public class DecompilerProvider extends NavigatableComponentProviderAdapterprivate void graphServiceAdded() {if (graphASTControlFlowAction == null && tool.getService(GraphService.class) != null) {- graphASTControlFlowAction = new GraphASTControlFlowAction(plugin, controller);+ graphASTControlFlowAction = new GraphASTControlFlowAction();addLocalAction(graphASTControlFlowAction);}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractDecompilerAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractDecompilerAction.javaindex 81f550e14d..54c589589b 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractDecompilerAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractDecompilerAction.java@@ -17,8 +17,16 @@ package ghidra.app.plugin.core.decompile.actions;import docking.ActionContext;import docking.action.DockingAction;+import ghidra.app.decompiler.*;import ghidra.app.plugin.core.decompile.DecompilePlugin;import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.app.util.datatype.DataTypeSelectionDialog;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.address.Address;+import ghidra.program.model.data.*;+import ghidra.program.model.listing.*;+import ghidra.program.model.pcode.*;+import ghidra.util.data.DataTypeParser.AllowedDataTypes;/*** A base class for {@link DecompilePlugin} actions that handles checking whether the@@ -59,6 +67,136 @@ public abstract class AbstractDecompilerAction extends DockingAction {});}+ /**+ * Find the HighSymbol the decompiler associates with a specific address.+ * @param addr is the specific address+ * @param highFunction is the decompiler results in which to search for the symbol+ * @return the matching symbol or null if no symbol exists+ */+ private static HighSymbol findHighSymbol(Address addr, HighFunction highFunction) {+ HighSymbol highSymbol = null;+ if (addr.isStackAddress()) {+ LocalSymbolMap lsym = highFunction.getLocalSymbolMap();+ highSymbol = lsym.findLocal(addr, null);+ }+ else {+ GlobalSymbolMap gsym = highFunction.getGlobalSymbolMap();+ highSymbol = gsym.getSymbol(addr);+ }+ return highSymbol;+ }++ /**+ * Track down the HighSymbol associated with a particular token. The token may be directly attached to+ * the symbol, or it may be a reference that needs to be looked up.+ * @param token is the given token+ * @param highFunction is the decompiler model of the function+ * @return the associated HighSymbol or null if one can't be found+ */+ public static HighSymbol findHighSymbolFromToken(ClangToken token, HighFunction highFunction) {+ HighVariable variable = token.getHighVariable();+ HighSymbol highSymbol = null;+ if (variable == null) {+ // Token may be from a variable reference, in which case we have to dig to find the actual symbol+ Address storageAddress =+ getStorageAddress(token, highFunction.getFunction().getProgram());+ if (storageAddress == null) {+ return null;+ }+ highSymbol = findHighSymbol(storageAddress, highFunction);+ }+ else {+ highSymbol = variable.getSymbol();+ }+ return highSymbol;+ }++ /**+ * Get the storage address of the variable attached to the given token, if any.+ * The variable may be directly referenced by the token, or indirectly referenced as a point.+ * @param tokenAtCursor is the given token+ * @param program is the Program+ * @return the storage Address or null if there is no variable attached+ */+ private static Address getStorageAddress(ClangToken tokenAtCursor, Program program) {+ Varnode vnode = tokenAtCursor.getVarnode();+ Address storageAddress = null;+ if (vnode != null) {+ storageAddress = vnode.getAddress();+ }+ // op could be a PTRSUB, need to dig it out...+ else if (tokenAtCursor instanceof ClangVariableToken) {+ PcodeOp op = ((ClangVariableToken) tokenAtCursor).getPcodeOp();+ storageAddress = HighFunctionDBUtil.getSpacebaseReferenceAddress(program, op);+ }+ return storageAddress;+ }++ /**+ * Get the structure associated with a field token+ * @param tok is the token representing a field+ * @return the structure which contains this field+ */+ public static Structure getStructDataType(ClangToken tok) {+ // We already know tok is a ClangFieldToken+ ClangFieldToken fieldtok = (ClangFieldToken) tok;+ DataType dt = fieldtok.getDataType();+ if (dt == null) {+ return null;+ }+ if (dt instanceof TypeDef) {+ dt = ((TypeDef) dt).getBaseDataType();+ }+ if (dt instanceof Structure) {+ return (Structure) dt;+ }+ return null;+ }++ /**+ * Compare the given HighFunction's idea of the prototype with the Function's idea.+ * Return true if there is a difference. If a specific symbol is being changed,+ * it can be passed in to check whether or not the prototype is being affected.+ * @param highSymbol (if not null) is the symbol being modified+ * @param hfunction is the given HighFunction+ * @return true if there is a difference (and a full commit is required)+ */+ protected static boolean checkFullCommit(HighSymbol highSymbol, HighFunction hfunction) {+ if (highSymbol != null && !highSymbol.isParameter()) {+ return false;+ }+ Function function = hfunction.getFunction();+ Parameter[] parameters = function.getParameters();+ LocalSymbolMap localSymbolMap = hfunction.getLocalSymbolMap();+ int numParams = localSymbolMap.getNumParams();+ if (numParams != parameters.length) {+ return true;+ }++ for (int i = 0; i < numParams; i++) {+ HighSymbol param = localSymbolMap.getParamSymbol(i);+ if (param.getCategoryIndex() != i) {+ return true;+ }+ VariableStorage storage = param.getStorage();+ // Don't compare using the equals method so that DynamicVariableStorage can match+ if (0 != storage.compareTo(parameters[i].getVariableStorage())) {+ return true;+ }+ }++ return false;+ }++ public DataType chooseDataType(PluginTool tool, Program program, DataType currentDataType) {+ DataTypeManager dataTypeManager = program.getDataTypeManager();+ DataTypeSelectionDialog chooserDialog = new DataTypeSelectionDialog(tool, dataTypeManager,+ Integer.MAX_VALUE, AllowedDataTypes.FIXED_LENGTH);+ chooserDialog.setInitialDataType(currentDataType);+ tool.showDialog(chooserDialog);+ return chooserDialog.getUserChosenDataType();+ }+/*** Subclasses return true if they are enabled for the given context*diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractSetSecondaryHighlightAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractSetSecondaryHighlightAction.javaindex d4483a3c9a..9b66c5748f 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractSetSecondaryHighlightAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/AbstractSetSecondaryHighlightAction.java@@ -16,7 +16,6 @@package ghidra.app.plugin.core.decompile.actions;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.decompiler.component.TokenHighlights;import ghidra.app.plugin.core.decompile.DecompilerActionContext;@@ -32,13 +31,13 @@ public abstract class AbstractSetSecondaryHighlightAction extends AbstractDecompreturn false;}- DecompilerPanel panel = context.getDecompilerPanel();- ClangToken tokenAtCursor = panel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();if (tokenAtCursor == null) {return false;}- TokenHighlights highlightedTokens = panel.getSecondaryHighlightedTokens();+ TokenHighlights highlightedTokens =+ context.getDecompilerPanel().getSecondaryHighlightedTokens();if (highlightedTokens.contains(tokenAtCursor)) {return false; // already highlighted}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.javaindex 3b04af6d22..ae3f0ebbc4 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceAction.java@@ -36,8 +36,7 @@ public class BackwardsSliceAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = context.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);return varnode != null;}@@ -45,13 +44,13 @@ public class BackwardsSliceAction extends AbstractDecompilerAction {@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = context.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);if (varnode == null) {return;}+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();decompilerPanel.clearPrimaryHighlights();PcodeOp op = tokenAtCursor.getPcodeOp();diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.javaindex 027a7381ee..fdf6321cd6 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/BackwardsSliceToPCodeOpsAction.java@@ -19,38 +19,36 @@ import java.util.Set;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.DecompilerPanel;+import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.program.model.pcode.PcodeOp;import ghidra.program.model.pcode.Varnode;public class BackwardsSliceToPCodeOpsAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public BackwardsSliceToPCodeOpsAction(DecompilerController controller) {+ public BackwardsSliceToPCodeOpsAction() {super("Highlight Backward Inst Slice");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Highlight", "Backward Inst Slice" }, "Decompile"));}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);return varnode != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);if (varnode != null) {PcodeOp op = tokenAtCursor.getPcodeOp();Set<PcodeOp> backwardSlice = DecompilerUtils.getBackwardSliceToPCodeOps(varnode);backwardSlice.add(op);+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();decompilerPanel.clearPrimaryHighlights();decompilerPanel.addPcodeOpHighlights(backwardSlice,decompilerPanel.getCurrentVariableHighlightColor());diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CloneDecompilerAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CloneDecompilerAction.javaindex 6c12f97f38..df338e1692 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CloneDecompilerAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CloneDecompilerAction.java@@ -22,21 +22,14 @@ import javax.swing.ImageIcon;import docking.action.KeyBindingData;import docking.action.ToolBarData;-import ghidra.app.decompiler.component.DecompilerController;import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.app.plugin.core.decompile.DecompilerProvider;import ghidra.util.HelpLocation;import resources.ResourceManager;public class CloneDecompilerAction extends AbstractDecompilerAction {- private final DecompilerProvider provider;- private DecompilerController controller;-- public CloneDecompilerAction(DecompilerProvider provider, DecompilerController controller) {+ public CloneDecompilerAction() {super("Decompile Clone");- this.provider = provider;- this.controller = controller;ImageIcon image = ResourceManager.loadImage("images/camera-photo.png");setToolBarData(new ToolBarData(image, "ZZZ"));setDescription("Create a snapshot (disconnected) copy of this Decompiler window ");@@ -47,11 +40,11 @@ public class CloneDecompilerAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- return controller.getFunction() != null;+ return context.getFunction() != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- provider.cloneWindow();+ context.getComponentProvider().cloneWindow();}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.javaindex f4580eebc5..b707c65c89 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitLocalsAction.java@@ -16,55 +16,35 @@package ghidra.app.plugin.core.decompile.actions;import docking.action.MenuData;-import ghidra.app.decompiler.ClangFunction;-import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.listing.Program;import ghidra.program.model.pcode.HighFunction;import ghidra.program.model.pcode.HighFunctionDBUtil;import ghidra.program.model.symbol.SourceType;public class CommitLocalsAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public CommitLocalsAction(PluginTool tool, DecompilerController controller) {+ public CommitLocalsAction() {super("Commit Locals");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Commit Locals" }, "Commit"));setDescription("Save Local variable definitions to Program, locking them into their current type definitions");}- private HighFunction getHighFunction() {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- if (tokenAtCursor == null) {- return null;- }- ClangFunction clfunc = tokenAtCursor.getClangFunction();- if (clfunc == null) {- return null;- }- return clfunc.getHighFunction();- }-@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {if (!context.hasRealFunction()) {return false;}- return getHighFunction() != null;+ return context.getHighFunction() != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Program program = controller.getProgram();+ Program program = context.getProgram();int transaction = program.startTransaction("Commit Params/Return");try {- HighFunction hfunc = getHighFunction();+ HighFunction hfunc = context.getHighFunction();HighFunctionDBUtil.commitLocalsToDatabase(hfunc, SourceType.USER_DEFINED);}finally {diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.javaindex 6f3dd6bed6..e956eb2c20 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/CommitParamsAction.java@@ -19,12 +19,7 @@ import java.awt.event.KeyEvent;import docking.action.KeyBindingData;import docking.action.MenuData;-import ghidra.app.decompiler.ClangFunction;-import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.listing.Program;import ghidra.program.model.pcode.HighFunction;import ghidra.program.model.pcode.HighFunctionDBUtil;@@ -33,48 +28,29 @@ import ghidra.util.Msg;import ghidra.util.exception.*;public class CommitParamsAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public CommitParamsAction(PluginTool tool, DecompilerController controller) {+ public CommitParamsAction() {super("Commit Params/Return");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Commit Params/Return" }, "Commit"));setKeyBindingData(new KeyBindingData(KeyEvent.VK_P, 0));setDescription("Save Parameters/Return definitions to Program, locking them into their current type definitions");}- private HighFunction getHighFunction() {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();-- // getTokenAtCursor can explicitly return null, so we must check here- // before dereferencing it.- if (tokenAtCursor == null) {- return null;- }-- ClangFunction clfunc = tokenAtCursor.getClangFunction();- if (clfunc == null) {- return null;- }- return clfunc.getHighFunction();- }-@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {if (!context.hasRealFunction()) {return false;}- return getHighFunction() != null;+ return context.getHighFunction() != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Program program = controller.getProgram();+ Program program = context.getProgram();int transaction = program.startTransaction("Commit Params/Return");try {- HighFunction hfunc = getHighFunction();+ HighFunction hfunc = context.getHighFunction();SourceType source = SourceType.ANALYSIS;if (hfunc.getFunction().getSignatureSource() == SourceType.USER_DEFINED) {source = SourceType.USER_DEFINED;diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DebugDecompilerAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DebugDecompilerAction.javaindex 0ad5057dd4..8ff1404df6 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DebugDecompilerAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DebugDecompilerAction.java@@ -27,7 +27,8 @@ import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.util.filechooser.ExtensionFileFilter;public class DebugDecompilerAction extends AbstractDecompilerAction {- private final DecompilerController controller;++ private DecompilerController controller;public DebugDecompilerAction(DecompilerController controller) {super("Debug Function Decompilation");@@ -37,12 +38,12 @@ public class DebugDecompilerAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- return controller.getFunction() != null;+ return context.getFunction() != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- JComponent parentComponent = controller.getDecompilerPanel();+ JComponent parentComponent = context.getDecompilerPanel();GhidraFileChooser fileChooser = new GhidraFileChooser(parentComponent);fileChooser.setTitle("Please Choose Output File");fileChooser.setFileFilter(new ExtensionFileFilter(new String[] { "xml" }, "XML Files"));diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DeletePrototypeOverrideAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DeletePrototypeOverrideAction.javaindex f21dbecc45..b9b71b052a 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DeletePrototypeOverrideAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/DeletePrototypeOverrideAction.java@@ -17,10 +17,7 @@ package ghidra.app.plugin.core.decompile.actions;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.framework.plugintool.PluginTool;import ghidra.program.database.symbol.CodeSymbol;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Function;@@ -31,17 +28,13 @@ import ghidra.util.Msg;import ghidra.util.UndefinedFunction;public class DeletePrototypeOverrideAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public DeletePrototypeOverrideAction(PluginTool tool, DecompilerController controller) {+ public DeletePrototypeOverrideAction() {super("Remove Signature Override");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Remove Signature Override" }, "Decompile"));}- public static CodeSymbol getSymbol(DecompilerController controller) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ public static CodeSymbol getSymbol(Function func, ClangToken tokenAtCursor) {if (tokenAtCursor == null) {return null;}@@ -49,7 +42,6 @@ public class DeletePrototypeOverrideAction extends AbstractDecompilerAction {if (addr == null) {return null;}- Function func = controller.getFunction();Namespace overspace = HighFunction.findOverrideSpace(func);if (overspace == null) {return null;@@ -76,25 +68,25 @@ public class DeletePrototypeOverrideAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();+ Function function = context.getFunction();if (function == null || function instanceof UndefinedFunction) {return false;}- return getSymbol(controller) != null;+ return getSymbol(function, context.getTokenAtCursor()) != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- CodeSymbol sym = getSymbol(controller);- Function func = controller.getFunction();+ Function func = context.getFunction();+ CodeSymbol sym = getSymbol(func, context.getTokenAtCursor());Program program = func.getProgram();SymbolTable symtab = program.getSymbolTable();int transaction = program.startTransaction("Remove Override Signature");boolean commit = true;if (!symtab.removeSymbolSpecial(sym)) {commit = false;- Msg.showError(getClass(), controller.getDecompilerPanel(),+ Msg.showError(getClass(), context.getDecompilerPanel(),"Removing Override Signature Failed", "Error removing override signature");}program.endTransaction(transaction, commit);diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.javaindex 7bca2dbba8..d380bb749d 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/EditDataTypeAction.java@@ -17,7 +17,6 @@ package ghidra.app.plugin.core.decompile.actions;import docking.ActionContext;import docking.action.MenuData;-import ghidra.app.decompiler.component.DecompilerController;import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.datamgr.util.DataTypeUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;@@ -29,13 +28,9 @@ import ghidra.program.model.listing.Function;import ghidra.util.UndefinedFunction;public class EditDataTypeAction extends AbstractDecompilerAction {- private final DecompilerController controller;- private final PluginTool tool;- public EditDataTypeAction(PluginTool tool, DecompilerController controller) {+ public EditDataTypeAction() {super("Edit Data Type");- this.tool = tool;- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Edit Data Type" }, "Decompile"));}@@ -44,7 +39,7 @@ public class EditDataTypeAction extends AbstractDecompilerAction {return (context instanceof DecompilerActionContext);}- private boolean hasCustomEditorForBaseDataType(DataType dataType) {+ private boolean hasCustomEditorForBaseDataType(PluginTool tool, DataType dataType) {DataType baseDataType = DataTypeUtils.getBaseDataType(dataType);final DataTypeManagerService service = tool.getService(DataTypeManagerService.class);return baseDataType != null && service.isEditable(baseDataType);@@ -53,7 +48,7 @@ public class EditDataTypeAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();+ Function function = context.getFunction();if (function instanceof UndefinedFunction) {return false;}@@ -63,7 +58,7 @@ public class EditDataTypeAction extends AbstractDecompilerAction {return false;}- return hasCustomEditorForBaseDataType(dataType);+ return hasCustomEditorForBaseDataType(context.getTool(), dataType);}@Override@@ -76,7 +71,8 @@ public class EditDataTypeAction extends AbstractDecompilerAction {if (baseDtDTM != dataTypeManager) {baseDataType = baseDataType.clone(dataTypeManager);}- final DataTypeManagerService service = tool.getService(DataTypeManagerService.class);+ final DataTypeManagerService service =+ context.getTool().getService(DataTypeManagerService.class);service.edit(baseDataType);}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ExportToCAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ExportToCAction.javaindex 6873bbc857..6c947f3ee3 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ExportToCAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ExportToCAction.java@@ -23,7 +23,7 @@ import docking.action.ToolBarData;import docking.widgets.OptionDialog;import docking.widgets.filechooser.GhidraFileChooser;import ghidra.app.decompiler.*;-import ghidra.app.decompiler.component.DecompilerController;+import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.framework.preferences.Preferences;import ghidra.util.Msg;@@ -33,11 +33,9 @@ import resources.ResourceManager;public class ExportToCAction extends AbstractDecompilerAction {private static final ImageIcon EXPORT_ICON = ResourceManager.loadImage("images/page_edit.png");private static final String LAST_USED_C_FILE = "last.used.decompiler.c.export.file";- private final DecompilerController controller;- public ExportToCAction(DecompilerController controller) {+ public ExportToCAction() {super("Export to C");- this.controller = controller;setToolBarData(new ToolBarData(EXPORT_ICON, "Local"));setDescription("Export the current function to C");}@@ -55,11 +53,11 @@ public class ExportToCAction extends AbstractDecompilerAction {Preferences.store();}- private File getFile() {+ private File getFile(DecompilerPanel decompilerPanel) {File lastUsedFile = readLastUsedFile();String[] extensions = new String[] { "h", "c", "cpp" };- GhidraFileChooser fileChooser = new GhidraFileChooser(controller.getDecompilerPanel());+ GhidraFileChooser fileChooser = new GhidraFileChooser(decompilerPanel);fileChooser.setFileFilter(new ExtensionFileFilter(extensions, "C/C++ Files"));if (lastUsedFile != null) {fileChooser.setSelectedFile(lastUsedFile);@@ -87,19 +85,19 @@ public class ExportToCAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- return controller.getFunction() != null && controller.getCCodeModel() != null;+ return context.getFunction() != null && context.getCCodeModel() != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- File file = getFile();+ File file = getFile(context.getDecompilerPanel());if (file == null) {return;}if (file.exists()) {- if (OptionDialog.showYesNoDialog(controller.getDecompilerPanel(),+ if (OptionDialog.showYesNoDialog(context.getDecompilerPanel(),"Overwrite Existing File?","Do you want to overwrite the existing file?") == OptionDialog.OPTION_TWO) {return;@@ -108,16 +106,16 @@ public class ExportToCAction extends AbstractDecompilerAction {try {PrintWriter writer = new PrintWriter(new FileOutputStream(file));- ClangTokenGroup grp = controller.getCCodeModel();- PrettyPrinter printer = new PrettyPrinter(controller.getFunction(), grp);+ ClangTokenGroup grp = context.getCCodeModel();+ PrettyPrinter printer = new PrettyPrinter(context.getFunction(), grp);DecompiledFunction decompFunc = printer.print(true);writer.write(decompFunc.getC());writer.close();- controller.setStatusMessage(+ context.setStatusMessage("Successfully exported function(s) to " + file.getAbsolutePath());}catch (IOException e) {- Msg.showError(getClass(), controller.getDecompilerPanel(), "Export to C Failed",+ Msg.showError(getClass(), context.getDecompilerPanel(), "Export to C Failed","Error exporting to C: " + e);}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.javaindex 4a8bc9e5de..df5503e470 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/FindAction.java@@ -24,28 +24,25 @@ import docking.action.MenuData;import docking.widgets.*;import docking.widgets.fieldpanel.field.Field;import docking.widgets.fieldpanel.support.FieldLocation;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.ClangTextField;+import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.framework.plugintool.PluginTool;import ghidra.util.HelpLocation;public class FindAction extends AbstractDecompilerAction {private FindDialog findDialog;- private final DecompilerPanel decompilerPanel;- private final PluginTool tool;- public FindAction(PluginTool tool, DecompilerController controller) {+ public FindAction() {super("Find");- this.tool = tool;- this.decompilerPanel = controller.getDecompilerPanel();setPopupMenuData(new MenuData(new String[] { "Find..." }, "Decompile"));setKeyBindingData(new KeyBindingData(KeyEvent.VK_F, InputEvent.CTRL_DOWN_MASK));setEnabled(true);}- protected FindDialog getFindDialog() {+ protected FindDialog getFindDialog(DecompilerPanel decompilerPanel) {if (findDialog == null) {- findDialog = new FindDialog("Decompiler Find Text", new DecompilerSearcher()) {+ findDialog =+ new FindDialog("Decompiler Find Text", new DecompilerSearcher(decompilerPanel)) {@Overrideprotected void dialogClosed() {// clear the search results when the dialog is closed@@ -57,7 +54,13 @@ public class FindAction extends AbstractDecompilerAction {return findDialog;}- private class DecompilerSearcher implements FindDialogSearcher {+ private static class DecompilerSearcher implements FindDialogSearcher {++ private DecompilerPanel decompilerPanel;++ public DecompilerSearcher(DecompilerPanel dPanel) {+ decompilerPanel = dPanel;+ }@Overridepublic CursorPosition getCursorPosition() {@@ -119,13 +122,14 @@ public class FindAction extends AbstractDecompilerAction {@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- FindDialog dialog = getFindDialog();+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();+ FindDialog dialog = getFindDialog(decompilerPanel);String text = decompilerPanel.getHighlightedText();if (text != null) {dialog.setSearchText(text);}// show over the root frame, so the user can still see the Decompiler window- tool.showDialog(dialog);+ context.getTool().showDialog(dialog);}}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/FindReferencesToSymbolAction.javaindex 6922e699d3..ffa99b2d85 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/FindReferencesToSymbolAction.java@@ -22,7 +22,6 @@ 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.framework.plugintool.PluginTool;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Function;import ghidra.program.model.listing.Program;@@ -40,11 +39,9 @@ public class FindReferencesToSymbolAction extends AbstractDecompilerAction {private static final String MENU_ITEM_TEXT = "Find References to";public static final String NAME = "Find References to Symbol";- private PluginTool tool;- public FindReferencesToSymbolAction(PluginTool tool) {+ public FindReferencesToSymbolAction() {super(NAME);- this.tool = tool;setPopupMenuData(new MenuData(new String[] { LocationReferencesService.MENU_GROUP, MENU_ITEM_TEXT }));@@ -116,7 +113,8 @@ public class FindReferencesToSymbolAction extends AbstractDecompilerAction {@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- LocationReferencesService service = tool.getService(LocationReferencesService.class);+ LocationReferencesService service =+ context.getTool().getService(LocationReferencesService.class);if (service == null) {Msg.showError(this, null, "Missing Plugin","The " + LocationReferencesService.class.getSimpleName() + " is not installed.\n" +diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.javaindex 432a50e05b..b41d033c3e 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceAction.java@@ -19,32 +19,29 @@ import java.util.Set;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.DecompilerPanel;+import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.program.model.pcode.PcodeOp;import ghidra.program.model.pcode.Varnode;public class ForwardSliceAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public ForwardSliceAction(DecompilerController controller) {+ public ForwardSliceAction() {super("Highlight Forward Slice");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Highlight", "Forward Slice" }, "Decompile"));}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);return varnode != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);if (varnode == null) {return;@@ -52,6 +49,7 @@ public class ForwardSliceAction extends AbstractDecompilerAction {PcodeOp op = tokenAtCursor.getPcodeOp();Set<Varnode> forwardSlice = DecompilerUtils.getForwardSlice(varnode);+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();decompilerPanel.clearPrimaryHighlights();SliceHighlightColorProvider colorProvider =diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.javaindex 154aa17c3d..1203b345a6 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/ForwardSliceToPCodeOpsAction.java@@ -19,38 +19,36 @@ import java.util.Set;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.DecompilerPanel;+import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.program.model.pcode.PcodeOp;import ghidra.program.model.pcode.Varnode;public class ForwardSliceToPCodeOpsAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public ForwardSliceToPCodeOpsAction(DecompilerController controller) {+ public ForwardSliceToPCodeOpsAction() {super("Highlight Forward Inst Slice");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Highlight", "Forward Inst Slice" }, "Decompile"));}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);return varnode != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);if (varnode != null) {PcodeOp op = tokenAtCursor.getPcodeOp();Set<PcodeOp> forwardSlice = DecompilerUtils.getForwardSliceToPCodeOps(varnode);forwardSlice.add(op);+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();decompilerPanel.clearPrimaryHighlights();decompilerPanel.addPcodeOpHighlights(forwardSlice,decompilerPanel.getCurrentVariableHighlightColor());diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/GraphASTControlFlowAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/GraphASTControlFlowAction.javaindex 6e23e61353..990b00b2e9 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/GraphASTControlFlowAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/GraphASTControlFlowAction.java@@ -16,11 +16,9 @@package ghidra.app.plugin.core.decompile.actions;import docking.action.MenuData;-import ghidra.app.decompiler.component.DecompilerController;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.services.GraphService;import ghidra.framework.options.Options;-import ghidra.framework.plugintool.Plugin;import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.address.Address;import ghidra.program.model.pcode.HighFunction;@@ -28,23 +26,20 @@ import ghidra.util.Msg;import ghidra.util.task.TaskLauncher;public class GraphASTControlFlowAction extends AbstractDecompilerAction {- private final DecompilerController controller;- private final PluginTool tool;- public GraphASTControlFlowAction(Plugin plugin, DecompilerController controller) {+ public GraphASTControlFlowAction() {super("Graph AST Control Flow");- this.tool = plugin.getTool();- this.controller = controller;setMenuBarData(new MenuData(new String[] { "Graph AST Control Flow" }, "graph"));}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- return controller.getFunction() != null;+ return context.getFunction() != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {+ PluginTool tool = context.getTool();GraphService graphService = tool.getService(GraphService.class);if (graphService == null) {Msg.showError(this, tool.getToolFrame(), "AST Graph Failed",@@ -56,8 +51,8 @@ public class GraphASTControlFlowAction extends AbstractDecompilerAction {Options options = tool.getOptions("Graph");boolean reuseGraph = options.getBoolean("Reuse Graph", false);int codeLimitPerBlock = options.getInt("Max Code Lines Displayed", 10);- HighFunction highFunction = controller.getHighFunction();- Address locationAddr = controller.getLocation().getAddress();+ HighFunction highFunction = context.getHighFunction();+ Address locationAddr = context.getLocation().getAddress();ASTGraphTask task = new ASTGraphTask(graphService, !reuseGraph, codeLimitPerBlock,locationAddr, highFunction, ASTGraphTask.CONTROL_FLOW_GRAPH);new TaskLauncher(task, tool.getToolFrame());diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.javaindex 7e619f2442..e8c2f7ac11 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/HighlightDefinedUseAction.java@@ -19,37 +19,35 @@ import java.util.Set;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.DecompilerPanel;+import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.program.model.pcode.PcodeOp;import ghidra.program.model.pcode.Varnode;public class HighlightDefinedUseAction extends AbstractDecompilerAction {- private final DecompilerController controller;- public HighlightDefinedUseAction(DecompilerController controller) {+ public HighlightDefinedUseAction() {super("Highlight Defined Use");- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Highlight", "Def-use" }, "Decompile"));}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);return varnode != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();Varnode varnode = DecompilerUtils.getVarnodeRef(tokenAtCursor);if (varnode == null) {return;}+ DecompilerPanel decompilerPanel = context.getDecompilerPanel();decompilerPanel.clearPrimaryHighlights();Set<Varnode> varnodes = Set.of(varnode);diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableAction.javaindex 3a4964cee2..fe4404e6ae 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableAction.java@@ -17,31 +17,28 @@ package ghidra.app.plugin.core.decompile.actions;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.listing.Function;import ghidra.program.model.pcode.*;+import ghidra.program.model.symbol.SourceType;import ghidra.util.UndefinedFunction;-public class IsolateVariableAction extends RetypeVariableAction {+public class IsolateVariableAction extends AbstractDecompilerAction {- public IsolateVariableAction(PluginTool tool, DecompilerController controller) {- super("New Isolated Variable", tool, controller);- setPopupMenuData(new MenuData(new String[] { "New Isolated Variable" }, "Decompile"));+ public IsolateVariableAction() {+ super("Split out as New Variable");+ setPopupMenuData(new MenuData(new String[] { "Split out as New Variable" }, "Decompile"));// setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, 0));}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();- if (function instanceof UndefinedFunction) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {return false;}- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ ClangToken tokenAtCursor = context.getTokenAtCursor();if (tokenAtCursor == null) {return false;}@@ -77,12 +74,13 @@ public class IsolateVariableAction extends RetypeVariableAction {@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- final ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- HighVariable variable = tokenAtCursor.getHighVariable();- HighSymbol highSymbol = variable.getSymbol();- highSymbol.setTypeLock(true);- retypeSymbol(highSymbol, tokenAtCursor.getVarnode(), highSymbol.getDataType());+ final ClangToken tokenAtCursor = context.getTokenAtCursor();+ HighSymbol highSymbol = tokenAtCursor.getHighVariable().getSymbol();+ IsolateVariableTask newVariableTask =+ new IsolateVariableTask(context.getTool(), context.getProgram(),+ context.getDecompilerPanel(), tokenAtCursor, highSymbol, SourceType.USER_DEFINED);++ newVariableTask.runTask();}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableTask.javanew file mode 100644index 0000000000..9c45c39a98--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/IsolateVariableTask.java@@ -0,0 +1,88 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import ghidra.app.decompiler.ClangToken;+import ghidra.app.decompiler.component.DecompilerPanel;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.data.*;+import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;+import ghidra.program.model.pcode.*;+import ghidra.program.model.symbol.SourceType;+import ghidra.util.Msg;+import ghidra.util.exception.DuplicateNameException;+import ghidra.util.exception.InvalidInputException;++public class IsolateVariableTask extends RenameTask {++ private HighSymbol highSymbol;+ private HighFunction highFunction;+ private Function function;+ private SourceType srcType;++ public IsolateVariableTask(PluginTool tool, Program program, DecompilerPanel panel,+ ClangToken token, HighSymbol sym, SourceType st) {+ super(tool, program, panel, token, "");+ highSymbol = sym;+ highFunction = highSymbol.getHighFunction();+ function = highFunction.getFunction();+ srcType = st;+ }++ @Override+ public String getTransactionName() {+ return "Name New Variable";+ }++ @Override+ public boolean isValid(String newNm) {+ newName = newNm;+ LocalSymbolMap localSymbolMap = highFunction.getLocalSymbolMap();+ if (localSymbolMap.containsVariableWithName(newName) ||+ isSymbolInFunction(function, newName)) {+ errorMsg = "Duplicate name";+ return false;+ }+ return true;+ }++ @Override+ public void commit() throws DuplicateNameException, InvalidInputException {+ // Split out the specific instance into its own symbol+ Varnode vn = tokenAtCursor.getVarnode();+ try {+ HighVariable highVariable = highFunction.splitOutMergeGroup(vn.getHigh(), vn);+ highSymbol = highVariable.getSymbol();+ }+ catch (PcodeException e) {+ Msg.showError(this, tool.getToolFrame(), "New Variable Failed", e.getMessage());+ return;+ }++ DataType dataType = highSymbol.getDataType();+ if (Undefined.isUndefined(dataType)) {+ // An undefined datatype will not be considered typelocked. Since the new variable+ // needs to be typelocked we use an unsigned integer of equivalent size+ dataType = AbstractIntegerDataType.getUnsignedDataType(dataType.getLength(),+ program.getDataTypeManager());+ }++ // Create the new variable, typelocking in a new data-type+ HighFunctionDBUtil.updateDBVariable(highSymbol, newName, dataType, srcType);+ }++}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/OverridePrototypeAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/OverridePrototypeAction.javaindex a4c906745e..e62af14dfd 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/OverridePrototypeAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/OverridePrototypeAction.java@@ -20,8 +20,6 @@ import java.util.Iterator;import docking.action.MenuData;import docking.widgets.OptionDialog;import ghidra.app.decompiler.*;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.plugin.core.function.EditFunctionSignatureDialog;import ghidra.framework.plugintool.PluginTool;@@ -36,9 +34,6 @@ import ghidra.util.exception.CancelledException;public class OverridePrototypeAction extends AbstractDecompilerAction {- private final DecompilerController controller;- private final PluginTool tool;-public class ProtoOverrideDialog extends EditFunctionSignatureDialog {private FunctionDefinition functionDefinition;@@ -86,21 +81,18 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {}}- public OverridePrototypeAction(PluginTool tool, DecompilerController controller) {+ public OverridePrototypeAction() {super("Override Signature");- this.tool = tool;- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Override Signature" }, "Decompile"));}/*** Try to find the PcodeOp representing the call the user has selected- * @param controller the decompiler controller+ * @param program is the Program+ * @param tokenAtCursor is the point in the window the user has selected* @return the PcodeOp or null*/- public static PcodeOp getCallOp(DecompilerController controller) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();+ public static PcodeOp getCallOp(Program program, ClangToken tokenAtCursor) {if (tokenAtCursor == null) {return null;}@@ -112,7 +104,7 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {if (addr == null) {return null;}- Instruction instr = controller.getProgram().getListing().getInstructionAt(addr);+ Instruction instr = program.getListing().getInstructionAt(addr);if (instr == null) {return null;}@@ -135,12 +127,11 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {return null;}- private Function getCalledFunction(PcodeOp op) {+ private Function getCalledFunction(Program program, PcodeOp op) {if (op.getOpcode() != PcodeOp.CALL) {return null;}Address addr = op.getInput(0).getAddress();- Program program = controller.getProgram();FunctionManager functionManager = program.getFunctionManager();Function function = functionManager.getFunctionAt(addr);if (function != null) {@@ -196,26 +187,26 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();+ Function function = context.getFunction();if (function == null || function instanceof UndefinedFunction) {return false;}- return getCallOp(controller) != null;+ return getCallOp(context.getProgram(), context.getTokenAtCursor()) != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Function func = controller.getFunction();+ Function func = context.getFunction();Program program = func.getProgram();- PcodeOp op = getCallOp(controller);- Function calledfunc = getCalledFunction(op);+ PcodeOp op = getCallOp(program, context.getTokenAtCursor());+ Function calledfunc = getCalledFunction(program, op);boolean varargs = false;if (calledfunc != null) {varargs = calledfunc.hasVarArgs();}if ((op.getOpcode() == PcodeOp.CALL) && !varargs) {- if (OptionDialog.showOptionDialog(controller.getDecompilerPanel(),+ if (OptionDialog.showOptionDialog(context.getDecompilerPanel(),"Warning : Localized Override","Incorrect information entered here may hide other good information.\n" +"For direct calls, it is usually better to alter the prototype on the function\n" +@@ -233,6 +224,7 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {}String signature = generateSignature(op, name);+ PluginTool tool = context.getTool();ProtoOverrideDialog dialog = new ProtoOverrideDialog(tool, func, signature, conv);// dialog.setHelpLocation( new HelpLocation( getOwner(), "Edit_Function_Signature" ) );tool.showDialog(dialog);@@ -247,7 +239,7 @@ public class OverridePrototypeAction extends AbstractDecompilerAction {commit = true;}catch (Exception e) {- Msg.showError(getClass(), controller.getDecompilerPanel(), "Override Signature Failed",+ Msg.showError(getClass(), context.getDecompilerPanel(), "Override Signature Failed","Error overriding signature: " + e);}finally {diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightAction.javaindex 22ad295c9a..565b2a58b1 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RemoveSecondaryHighlightAction.java@@ -44,20 +44,20 @@ public class RemoveSecondaryHighlightAction extends AbstractDecompilerAction {return false;}- DecompilerPanel panel = context.getDecompilerPanel();- ClangToken token = panel.getTokenAtCursor();+ ClangToken token = context.getTokenAtCursor();if (token == null) {return false;}+ DecompilerPanel panel = context.getDecompilerPanel();TokenHighlights highlightedTokens = panel.getSecondaryHighlightedTokens();return highlightedTokens.contains(token);}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {+ ClangToken token = context.getTokenAtCursor();DecompilerPanel panel = context.getDecompilerPanel();- ClangToken token = panel.getTokenAtCursor();panel.removeSecondaryHighlight(token);}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameConstantTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameConstantTask.javadeleted file mode 100644index e7cdc8a3bb..0000000000--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameConstantTask.java+++ /dev/null@@ -1,70 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.app.plugin.core.decompile.actions;--import ghidra.app.util.bean.SetEquateDialog;-import ghidra.framework.plugintool.PluginTool;-import ghidra.program.model.listing.Program;-import ghidra.program.model.pcode.HighConstant;-import ghidra.util.HelpLocation;-import ghidra.util.exception.DuplicateNameException;-import ghidra.util.exception.InvalidInputException;--public class RenameConstantTask extends RenameTask {-- private HighConstant high;- private Program program;-- public RenameConstantTask(PluginTool tool, String old, HighConstant h, Program program) {- super(tool, old);- high = h;- this.program = program;- }-- @Override- public void commit() throws DuplicateNameException, InvalidInputException {- // TODO: Constant equates do not work properly with decompiler- //high.rename(newName,SourceType.USER_DEFINED);- }-- @Override- public String getTransactionName() {- return "Set Equate";- }-- @Override- public boolean isValid(String newNm) {- newName = newNm;- return true;- }-- /* (non-Javadoc)- * @see ghidra.app.plugin.core.decompile.actions.RenameTask#runDialog()- */- @Override- public boolean runDialog() {- // NOTE: acstion must ensure that HighConstant datatype produces Scalar value and is integer type- // BooleanDataType and CharDataType do not produce scalar values in assembly listing.- SetEquateDialog setEquateDialog = new SetEquateDialog(tool, program, high.getScalar());- setEquateDialog.setHelpLocation(new HelpLocation("EquatesPlugin", "Set_Equate"));-- if (setEquateDialog.showSetDialog() == SetEquateDialog.CANCELED) {- return false;- }- isValid(setEquateDialog.getEquateName());- return true;- }-}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFieldAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFieldAction.javanew file mode 100644index 0000000000..64b3176f96--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFieldAction.java@@ -0,0 +1,79 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import ghidra.app.decompiler.ClangFieldToken;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.data.Structure;+import ghidra.program.model.listing.Function;+import ghidra.util.Msg;+import ghidra.util.UndefinedFunction;++/**+ * Action triggered from a specific token in the decompiler window to rename a field within+ * a structure data-type. If the field already exists within the specific structure, it is+ * simply renamed. Otherwise, if the decompiler has discovered an undefined structure offset, a new+ * field is added to the structure with this offset and the user selected name. In either case,+ * the altered structure is committed permanently to the program's database.+ */+public class RenameFieldAction extends AbstractDecompilerAction {++ public RenameFieldAction() {+ super("Rename Field");+ setPopupMenuData(new MenuData(new String[] { "Rename Field" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, 0));+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ return (tokenAtCursor instanceof ClangFieldToken);+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ PluginTool tool = context.getTool();+ final ClangToken tokenAtCursor = context.getTokenAtCursor();++ Structure dt = getStructDataType(tokenAtCursor);+ if (dt == null) {+ Msg.showError(this, tool.getToolFrame(), "Rename Failed",+ "Could not find structure datatype");+ return;+ }+ int offset = ((ClangFieldToken) tokenAtCursor).getOffset();+ if (offset < 0 || offset >= dt.getLength()) {+ Msg.showError(this, tool.getToolFrame(), "Rename Failed",+ "Could not resolve field within structure");+ return;+ }+ RenameStructureFieldTask nameTask =+ new RenameStructureFieldTask(tool, context.getProgram(), context.getDecompilerPanel(),+ tokenAtCursor, dt, offset);+ nameTask.runTask();+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFunctionAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFunctionAction.javaindex acbd928770..b243876484 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFunctionAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameFunctionAction.java@@ -21,49 +21,43 @@ import docking.action.KeyBindingData;import docking.action.MenuData;import ghidra.app.decompiler.ClangFuncNameToken;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.util.AddEditDialog;-import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;import ghidra.util.UndefinedFunction;public class RenameFunctionAction extends AbstractDecompilerAction {- private final DecompilerController controller;- private final PluginTool tool;-- public RenameFunctionAction(PluginTool tool, DecompilerController controller) {+ public RenameFunctionAction() {super("Rename Function");- this.tool = tool;- this.controller = controller;setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, 0));setPopupMenuData(new MenuData(new String[] { "Rename Function" }, "Decompile"));}- private Function getFunction() {+ private Function getFunction(Program program, ClangToken tokenAtCursor) {// try to look up the function that is at the current cursor location// If there isn't one, just use the function we are in.- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();if (tokenAtCursor instanceof ClangFuncNameToken) {- return DecompilerUtils.getFunction(controller.getProgram(),- (ClangFuncNameToken) tokenAtCursor);+ return DecompilerUtils.getFunction(program, (ClangFuncNameToken) tokenAtCursor);}return null;}@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function func = getFunction();+ Function func =+ getFunction(context.getProgram(), context.getTokenAtCursor());return func != null && !(func instanceof UndefinedFunction);}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Function function = getFunction();- AddEditDialog dialog = new AddEditDialog("Edit Function Name", tool);- dialog.editLabel(function.getSymbol(), controller.getProgram());+ Program program = context.getProgram();+ Function function = getFunction(program, context.getTokenAtCursor());+ AddEditDialog dialog = new AddEditDialog("Edit Function Name", context.getTool());+ dialog.editLabel(function.getSymbol(), program);}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalAction.javanew file mode 100644index 0000000000..0d3612cc60--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalAction.java@@ -0,0 +1,86 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import ghidra.app.decompiler.ClangFieldToken;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.address.Address;+import ghidra.program.model.listing.Function;+import ghidra.program.model.pcode.HighCodeSymbol;+import ghidra.program.model.pcode.HighSymbol;+import ghidra.util.Msg;+import ghidra.util.UndefinedFunction;++/**+ * Action triggered from a specific token in the decompiler window to rename a global variable.+ * The variable is associated with an address. There may already be a symbol in the database+ * there, in which case the symbol is simply renamed. Otherwise a new symbol is added.+ */+public class RenameGlobalAction extends AbstractDecompilerAction {++ public RenameGlobalAction() {+ super("Rename Global");+ setPopupMenuData(new MenuData(new String[] { "Rename Global" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, 0));+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ if (tokenAtCursor == null) {+ return false;+ }+ if (tokenAtCursor instanceof ClangFieldToken) {+ return false;+ }+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ if (highSymbol == null) {+ return false;+ }+ return highSymbol.isGlobal();+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ PluginTool tool = context.getTool();+ final ClangToken tokenAtCursor = context.getTokenAtCursor();+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ Address addr = null;+ if (highSymbol instanceof HighCodeSymbol) {+ addr = ((HighCodeSymbol) highSymbol).getStorage().getMinAddress();+ }+ if (addr == null || !addr.isMemoryAddress()) {+ Msg.showError(this, tool.getToolFrame(), "Rename Failed",+ "Memory storage not found for global variable");+ return;+ }+ RenameGlobalVariableTask nameTask =+ new RenameGlobalVariableTask(tool, context.getProgram(), context.getDecompilerPanel(),+ tokenAtCursor, addr);+ nameTask.runTask();+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalVariableTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalVariableTask.javaindex 9b2435c699..47f7d0a2d9 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalVariableTask.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalVariableTask.java@@ -15,6 +15,8 @@*/package ghidra.app.plugin.core.decompile.actions;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.address.Address;import ghidra.program.model.listing.Program;@@ -24,14 +26,13 @@ import ghidra.util.exception.InvalidInputException;public class RenameGlobalVariableTask extends RenameTask {private Address address; // Address of global variable- private Program program;private SymbolTable symboltable;private Symbol symbol;- public RenameGlobalVariableTask(PluginTool tool, String old, Address addr, Program prog) {- super(tool, old);+ public RenameGlobalVariableTask(PluginTool tool, Program program, DecompilerPanel panel,+ ClangToken token, Address addr) {+ super(tool, program, panel, token, token.getText());address = addr;- program = prog;symboltable = program.getSymbolTable();symbol = null;}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLocalAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLocalAction.javanew file mode 100644index 0000000000..38e5aa9762--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLocalAction.java@@ -0,0 +1,88 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import ghidra.app.decompiler.ClangFieldToken;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.listing.Function;+import ghidra.program.model.pcode.HighSymbol;+import ghidra.program.model.symbol.SourceType;+import ghidra.util.UndefinedFunction;++/**+ * Action triggered from a specific token in the decompiler window to rename a local variable.+ * If a matching variable in the database already exists, it is simply renamed. Otherwise+ * a new variable is added to the database. In this case the new variable is assigned+ * an "undefined" datatype, which leaves it un-typelocked, and the decompiler will take+ * the name but lets the data-type continue to "float" and can speculatively merge the+ * variable with others.+ *+ * If the selected variable is an input parameter, other input parameters within the decompiler+ * model will need to be committed, if they do not already exist in the database, as any parameters+ * committed to the database are forcing on the decompiler. Any new parameters committed this way+ * inherit their name from the decompiler model, but the parameters will not be type-locked, allowing+ * their data-type to "float".++ */+public class RenameLocalAction extends AbstractDecompilerAction {++ public RenameLocalAction() {+ super("Rename Variable");+ setPopupMenuData(new MenuData(new String[] { "Rename Variable" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, 0));+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ if (tokenAtCursor == null) {+ return false;+ }+ if (tokenAtCursor instanceof ClangFieldToken) {+ return false;+ }+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ if (highSymbol == null) {+ return false;+ }+ return !highSymbol.isGlobal();+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ PluginTool tool = context.getTool();+ final ClangToken tokenAtCursor = context.getTokenAtCursor();+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());++ RenameVariableTask nameTask = new RenameVariableTask(tool, context.getProgram(),+ context.getDecompilerPanel(),+ tokenAtCursor, highSymbol, SourceType.USER_DEFINED);++ nameTask.runTask();+ }++}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameStructureFieldTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameStructureFieldTask.javaindex 6931f7b70a..a70e582c84 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameStructureFieldTask.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameStructureFieldTask.java@@ -15,11 +15,11 @@*/package ghidra.app.plugin.core.decompile.actions;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.framework.plugintool.PluginTool;-import ghidra.program.model.data.DataType;-import ghidra.program.model.data.DataTypeComponent;-import ghidra.program.model.data.Structure;-import ghidra.program.model.data.Undefined1DataType;+import ghidra.program.model.data.*;+import ghidra.program.model.listing.Program;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -28,8 +28,9 @@ public class RenameStructureFieldTask extends RenameTask {private Structure structure;public int offset;- public RenameStructureFieldTask(PluginTool tool,String old,Structure structure,int offset) {- super(tool,old);+ public RenameStructureFieldTask(PluginTool tool, Program program, DecompilerPanel panel,+ ClangToken token, Structure structure, int offset) {+ super(tool, program, panel, token, token.getText());this.structure = structure;this.offset = offset;}@@ -45,8 +46,9 @@ public class RenameStructureFieldTask extends RenameTask {DataType newtype = new Undefined1DataType();structure.replaceAtOffset(offset, newtype,1, newName, "Created by retype action");}- else+ else {comp.setFieldName(newName);+ }}@Override@@ -58,10 +60,12 @@ public class RenameStructureFieldTask extends RenameTask {public boolean isValid(String newNm) {newName = newNm;DataTypeComponent[] comp = structure.getComponents();- for(int i=0;i<comp.length;++i) {+ for (DataTypeComponent element : comp) {// if (comp[i].getDataType() == DataType.DEFAULT) continue; // Placeholder, don't compare name- String fieldname = comp[i].getFieldName();- if (fieldname == null) continue;+ String fieldname = element.getFieldName();+ if (fieldname == null) {+ continue;+ }if (fieldname.equals(newName)) {errorMsg = "Duplicate Field Name";return false;diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.javaindex b1f979d99e..3c3398aabc 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameTask.java@@ -1,6 +1,5 @@/* ###* IP: GHIDRA- * REVIEWED: YES** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.@@ -18,7 +17,13 @@ package ghidra.app.plugin.core.decompile.actions;import docking.widgets.dialogs.InputDialog;import docking.widgets.dialogs.InputDialogListener;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;+import ghidra.program.model.symbol.SymbolTable;+import ghidra.util.Msg;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -31,9 +36,16 @@ public abstract class RenameTask {protected String oldName;protected String errorMsg = null; // Error to return if isValid returns falseprotected PluginTool tool;+ protected Program program;+ protected DecompilerPanel decompilerPanel;+ protected ClangToken tokenAtCursor;- public RenameTask(PluginTool tool,String old) {+ public RenameTask(PluginTool tool, Program program, DecompilerPanel panel, ClangToken token,+ String old) {this.tool = tool;+ this.program = program;+ this.decompilerPanel = panel;+ this.tokenAtCursor = token;oldName = old;}@@ -49,8 +61,9 @@ public abstract class RenameTask {* Bring up a dialog that is initialized with the old name, and allows the user to select a new name* @return true unless the user canceled*/- public boolean runDialog() {+ private boolean runDialog() {InputDialogListener listener = new InputDialogListener() {+ @Overridepublic boolean inputIsValid(InputDialog dialog) {String name = dialog.getValue();if ((name==null)||(name.length()==0)) {@@ -62,8 +75,9 @@ public abstract class RenameTask {return true; // but valid (ends up being equivalent to cancel}boolean res = isValid(name);- if (!res)+ if (!res) {dialog.setStatusText(errorMsg);+ }return res;}};@@ -77,10 +91,38 @@ public abstract class RenameTask {if (renameVarDialog.isCanceled()) {return false;}- if (newName.equals(oldName)) // No change to name- return false;+ if (newName.equals(oldName)) {+ return false;+ }return true;}-++ public void runTask() {+ boolean dialogres = runDialog();+ if (dialogres) {+ int transaction = program.startTransaction(getTransactionName());+ boolean commit = false;+ try {+ commit();+ commit = true;+ }+ catch (DuplicateNameException e) {+ Msg.showError(this, tool.getToolFrame(), "Rename Failed", e.getMessage());+ }+ catch (InvalidInputException e) {+ Msg.showError(this, tool.getToolFrame(), "Rename Failed", e.getMessage());+ }+ finally {+ program.endTransaction(transaction, commit);+ decompilerPanel.tokenRenamed(tokenAtCursor, getNewName());+ }+ }++ }++ public static boolean isSymbolInFunction(Function function, String name) {+ SymbolTable symbolTable = function.getProgram().getSymbolTable();+ return !symbolTable.getSymbols(name, function).isEmpty();+ }}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableAction.javadeleted file mode 100644index 19adc2b094..0000000000--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableAction.java+++ /dev/null@@ -1,240 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.app.plugin.core.decompile.actions;--import java.awt.event.KeyEvent;--import docking.action.KeyBindingData;-import docking.action.MenuData;-import ghidra.app.decompiler.*;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;-import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.framework.plugintool.PluginTool;-import ghidra.program.model.address.Address;-import ghidra.program.model.data.*;-import ghidra.program.model.listing.Function;-import ghidra.program.model.listing.Program;-import ghidra.program.model.pcode.*;-import ghidra.program.model.symbol.SourceType;-import ghidra.util.Msg;-import ghidra.util.UndefinedFunction;-import ghidra.util.exception.DuplicateNameException;-import ghidra.util.exception.InvalidInputException;--public class RenameVariableAction extends AbstractDecompilerAction {- private final DecompilerController controller;- private final PluginTool tool;- private RenameTask nameTask = null;-- public RenameVariableAction(PluginTool tool, DecompilerController controller) {- super("Rename Variable");- this.tool = tool;- this.controller = controller;- setPopupMenuData(new MenuData(new String[] { "Rename Variable" }, "Decompile"));- setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, 0));- }-- public static Address getStorageAddress(ClangToken tokenAtCursor,- DecompilerController controller) {- Varnode vnode = tokenAtCursor.getVarnode();- Address storageAddress = null;- if (vnode != null) {- storageAddress = vnode.getAddress();- }- // op could be a PTRSUB, need to dig it out...- else if (tokenAtCursor instanceof ClangVariableToken) {- PcodeOp op = ((ClangVariableToken) tokenAtCursor).getPcodeOp();- storageAddress =- HighFunctionDBUtil.getSpacebaseReferenceAddress(controller.getProgram(), op);- }- return storageAddress;- }-- /**- * Find the HighSymbol the decompiler associates with a specific address.- * @param addr is the specific address- * @param controller is the decompiler being queried- * @return the matching symbol or null if no symbol exists- */- public static HighSymbol findHighSymbol(Address addr, DecompilerController controller) {- HighSymbol highSymbol = null;- HighFunction hfunc = controller.getDecompileData().getHighFunction();- if (addr.isStackAddress()) {- LocalSymbolMap lsym = hfunc.getLocalSymbolMap();- highSymbol = lsym.findLocal(addr, null);- }- else {- GlobalSymbolMap gsym = hfunc.getGlobalSymbolMap();- highSymbol = gsym.getSymbol(addr);- }- return highSymbol;- }-- /**- * Get the structure associated with a field token- * @param tok is the token representing a field- * @return the structure which contains this field- */- public static Structure getStructDataType(ClangToken tok) {- // We already know tok is a ClangFieldToken- ClangFieldToken fieldtok = (ClangFieldToken) tok;- DataType dt = fieldtok.getDataType();- if (dt == null) {- return null;- }- if (dt instanceof TypeDef) {- dt = ((TypeDef) dt).getBaseDataType();- }- if (dt instanceof Structure) {- return (Structure) dt;- }- return null;- }-- /**- * Get the offset of the field within its structure- * @param tok is the display token associated with the field- * @return the offset, in bytes, of that field within its structure- */- public static int getDataTypeOffset(ClangToken tok) {- // Assume tok is already vetted as a structure carrying ClangFieldToken- return ((ClangFieldToken) tok).getOffset();- }-- @Override- protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();- if (function instanceof UndefinedFunction) {- return false;- }-- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- if (tokenAtCursor == null) {- return false;- }- if (tokenAtCursor instanceof ClangFieldToken) {- DataType dt = getStructDataType(tokenAtCursor);- if (dt == null) {- return false;- }- getPopupMenuData().setMenuItemName("Rename Field");- return true;- }- HighVariable variable = tokenAtCursor.getHighVariable();- HighSymbol highSymbol = null;- if (variable == null) {- // Token may be from a variable reference, in which case we have to dig to find the actual symbol- Address storageAddress = getStorageAddress(tokenAtCursor, controller);- if (storageAddress == null) {- return false;- }- highSymbol = findHighSymbol(storageAddress, controller);- }- else {- highSymbol = variable.getSymbol();- }- if (highSymbol == null) {- return false;- }- if (highSymbol.isGlobal()) {- getPopupMenuData().setMenuItemName("Rename Global");- }- else {- getPopupMenuData().setMenuItemName("Rename Variable");- }- return true;- }-- @Override- protected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- final ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- HighVariable variable = tokenAtCursor.getHighVariable();- HighSymbol highSymbol = null;-- if (variable == null) {- if (tokenAtCursor instanceof ClangVariableToken) {- Address addr = getStorageAddress(tokenAtCursor, controller);- highSymbol = findHighSymbol(addr, controller);- }- }- else {- highSymbol = variable.getSymbol();- }- if (highSymbol != null) {- if (highSymbol.isGlobal()) {- Address addr = null;- if (highSymbol instanceof HighCodeSymbol) {- addr = ((HighCodeSymbol) highSymbol).getStorage().getMinAddress();- }- if (addr == null || !addr.isMemoryAddress()) {- Msg.showError(this, tool.getToolFrame(), "Rename Failed",- "Memory storage not found for global variable");- return;- }- nameTask = new RenameGlobalVariableTask(tool, tokenAtCursor.getText(), addr,- controller.getProgram());- }- else {- nameTask = new RenameVariableTask(tool, highSymbol, tokenAtCursor.getVarnode(),- SourceType.USER_DEFINED);- }- }- else if (tokenAtCursor instanceof ClangFieldToken) {- Structure dt = getStructDataType(tokenAtCursor);- if (dt == null) {- Msg.showError(this, tool.getToolFrame(), "Rename Failed",- "Could not find structure datatype");- return;- }- int offset = getDataTypeOffset(tokenAtCursor);- if (offset < 0 || offset >= dt.getLength()) {- Msg.showError(this, tool.getToolFrame(), "Rename Failed",- "Could not resolve field within structure");- return;- }- nameTask = new RenameStructureFieldTask(tool, tokenAtCursor.getText(), dt, offset);- }- else {- Msg.showError(this, tool.getToolFrame(), "Rename Failed",- "Selected variable does not support renaming");- return;- }-- boolean dialogres = nameTask.runDialog();- if (dialogres) {- Program program = controller.getProgram();- int transaction = program.startTransaction(nameTask.getTransactionName());- boolean commit = false;- try {- nameTask.commit();- commit = true;- }- catch (DuplicateNameException e) {- Msg.showError(this, tool.getToolFrame(), "Rename Failed", e.getMessage());- }- catch (InvalidInputException e) {- Msg.showError(this, tool.getToolFrame(), "Rename Failed", e.getMessage());- }- finally {- program.endTransaction(transaction, commit);- controller.getDecompilerPanel().tokenRenamed(tokenAtCursor, nameTask.getNewName());- }- }- }-}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.javaindex 5a4861c998..da946cf53e 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameVariableTask.java@@ -15,12 +15,13 @@*/package ghidra.app.plugin.core.decompile.actions;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.framework.plugintool.PluginTool;import ghidra.program.model.listing.Function;import ghidra.program.model.listing.Program;import ghidra.program.model.pcode.*;import ghidra.program.model.symbol.SourceType;-import ghidra.program.model.symbol.SymbolTable;import ghidra.util.exception.DuplicateNameException;import ghidra.util.exception.InvalidInputException;@@ -29,19 +30,18 @@ public class RenameVariableTask extends RenameTask {private HighSymbol highSymbol;private Varnode exactSpot;private HighFunction hfunction;- private Program program;private Function function;private boolean commitRequired; // Set to true if all parameters are committed before renamingprivate SourceType srctype; // Desired source type for the variable being renamedprivate SourceType signatureSrcType; // Signature source type of the function (which will be preserved)- public RenameVariableTask(PluginTool tool, HighSymbol sym, Varnode ex, SourceType st) {- super(tool, sym.getName());+ public RenameVariableTask(PluginTool tool, Program program, DecompilerPanel panel,+ ClangToken token, HighSymbol sym, SourceType st) {+ super(tool, program, panel, token, sym.getName());highSymbol = sym;- exactSpot = ex;+ exactSpot = token.getVarnode();hfunction = sym.getHighFunction();function = hfunction.getFunction();- program = function.getProgram();srctype = st;signatureSrcType = function.getSignatureSource();}@@ -61,11 +61,12 @@ public class RenameVariableTask extends RenameTask {public boolean isValid(String newNm) {newName = newNm;LocalSymbolMap localSymbolMap = hfunction.getLocalSymbolMap();- if (localSymbolMap.containsVariableWithName(newName) || isSymbolInFunction(newName)) {+ if (localSymbolMap.containsVariableWithName(newName) ||+ isSymbolInFunction(function, newName)) {errorMsg = "Duplicate name";return false;}- commitRequired = RetypeVariableAction.checkFullCommit(highSymbol, hfunction);+ commitRequired = AbstractDecompilerAction.checkFullCommit(highSymbol, hfunction);if (commitRequired) {exactSpot = null; // Don't try to split out if we need to commit}@@ -87,11 +88,6 @@ public class RenameVariableTask extends RenameTask {return true;}- private boolean isSymbolInFunction(String name) {- SymbolTable symbolTable = program.getSymbolTable();- return !symbolTable.getSymbols(name, function).isEmpty();- }-@Overridepublic String getTransactionName() {return "Rename Local Variable";diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeFieldAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeFieldAction.javanew file mode 100644index 0000000000..c54b9fb86e--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeFieldAction.java@@ -0,0 +1,181 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.InputEvent;+import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import ghidra.app.decompiler.ClangFieldToken;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.data.*;+import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;+import ghidra.util.Msg;+import ghidra.util.UndefinedFunction;++/**+ * Action triggered from a specific token in the decompiler window to change the data-type of+ * a field within a structure data-type. The field must already exist, except in the case of a+ * completely undefined structure. The data-type of the field is changed according to the user+ * selection. If the size of the seleted data-type is bigger, this can trigger other fields in+ * the structure to be removed and may change the size of the structure. The modified data-type+ * is permanently committed to the program's database.+ */+public class RetypeFieldAction extends AbstractDecompilerAction {++ public RetypeFieldAction() {+ super("Retype Field");+ setPopupMenuData(new MenuData(new String[] { "Retype Field" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK));+ }++ /**+ * Return the index of the last component that would be overwritten by a new datatype, if we started overwriting+ * with at a specific component. All components, except the first, must be undefined datatypes or we return -1+ * @param struct is the structure we are testing+ * @param comp is the starting component to overwrite+ * @param newtype is the datatype to overwrite with+ * @return the index of the last component overwritten or -1+ */+ private static int getEndComponentIndex(Structure struct, DataTypeComponent comp,+ DataType newtype) {+ int newlen = newtype.getLength();+ if (newlen <= 0) {+ return -1; // Don't support variable length types+ }+ DataType curtype = comp.getDataType();+ newlen -= curtype.getLength();+ int index = comp.getOrdinal();+ while (newlen > 0) {+ index += 1;+ if (index >= struct.getNumComponents()) {+ return -1; // Not enough space in the structure+ }+ comp = struct.getComponent(index);+// String nm = comp.getFieldName();+// if ((nm !=null)&&(nm.length()!=0))+// return -1;+ curtype = comp.getDataType();+// if (!Undefined.isUndefined(curtype))+// return -1; // Overlaps non-undefined datatype+ if (curtype != DataType.DEFAULT) {+ return -1; // Only allow overwrite of placeholder components+ }+ newlen -= curtype.getLength();+ }+ return index;+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ if (tokenAtCursor == null) {+ return false;+ }+ if (tokenAtCursor instanceof ClangFieldToken) {+ DataType dt = getStructDataType(tokenAtCursor);+ return (dt != null);+ }+ return false;+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ Program program = context.getProgram();+ PluginTool tool = context.getTool();+ ClangToken tokenAtCursor = context.getTokenAtCursor();+ DataTypeManager dataTypeManager = program.getDataTypeManager();++ DataType dataType = null;+ Structure struct = getStructDataType(tokenAtCursor);+ int offset = ((ClangFieldToken) tokenAtCursor).getOffset();+ if (struct == null) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Failed to re-type structure");+ return;+ }+ if (offset < 0 || offset >= struct.getLength()) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Failed to re-type structure: " + struct.getName());+ return;+ }+ DataTypeComponent comp = struct.getComponentAt(offset);+ if (comp == null) {+ dataType = chooseDataType(tool, program, DataType.DEFAULT);+ }+ else {+ dataType = chooseDataType(tool, program, comp.getDataType());+ }++ if (dataType == null) {+ return;+ }+ boolean successfulMod = false;+ if (comp == null) {+ if (!struct.isNotYetDefined()) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Could not find component of '" + struct.getName() + "' to retype");+ return;+ }+ // note if we reach here the offset must be zero, so assume we are inserting newtype+ int transaction = program.startTransaction("Retype Structure Field");+ try {+ // Make sure datatype is using the program's data organization before testing fit+ if (dataType.getDataTypeManager() != dataTypeManager) {+ dataType = dataTypeManager.resolve(dataType, null);+ }+ struct.insert(0, dataType);+ successfulMod = true;+ }+ finally {+ program.endTransaction(transaction, successfulMod);+ }+ return;+ }+ int transaction = program.startTransaction("Retype Structure Field");+ try {+ // Make sure datatype is using the program's data organization before testing fit+ if (dataType.getDataTypeManager() != dataTypeManager) {+ dataType = dataTypeManager.resolve(dataType, null);+ }+ int startind = comp.getOrdinal();+ int endind = getEndComponentIndex(struct, comp, dataType);+ if (endind < 0) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Failed to re-type structure '" + struct.getName() + "': Datatype did not fit");+ return;+ }+ for (int i = endind; i > startind; --i) { // Clear all but first field+ struct.clearComponent(i);+ }+ struct.replaceAtOffset(comp.getOffset(), dataType, dataType.getLength(),+ comp.getFieldName(), comp.getComment());+ successfulMod = true;+ }+ finally {+ program.endTransaction(transaction, successfulMod);+ }+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeGlobalAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeGlobalAction.javanew file mode 100644index 0000000000..14cc4511cb--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeGlobalAction.java@@ -0,0 +1,119 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.InputEvent;+import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import ghidra.app.decompiler.*;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.data.DataType;+import ghidra.program.model.data.DataTypeManager;+import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;+import ghidra.program.model.pcode.HighFunctionDBUtil;+import ghidra.program.model.pcode.HighSymbol;+import ghidra.program.model.symbol.SourceType;+import ghidra.util.Msg;+import ghidra.util.UndefinedFunction;+import ghidra.util.exception.*;++/**+ * Action triggered from a specific token in the decompiler window to change the data-type+ * associated with a global variable. If the variable does not already exist in the program database,+ * it will be created using storage address the decompiler has assigned to the variable within its model.+ * In either case, there is a preexisting notion of variable storage. This action may allow the newly+ * selected data-type to be of a different size relative to this preexisting storage, constrained by+ * other global variables that might already consume storage.+ */+public class RetypeGlobalAction extends AbstractDecompilerAction {++ public RetypeGlobalAction() {+ super("Retype Global");+ setPopupMenuData(new MenuData(new String[] { "Retype Global" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK));+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ if (tokenAtCursor == null) {+ return false;+ }+ if (tokenAtCursor instanceof ClangFieldToken) {+ return false;+ }+ if (tokenAtCursor.Parent() instanceof ClangReturnType) {+ return false;+ }+ if (!tokenAtCursor.isVariableRef()) {+ return false;+ }+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ if (highSymbol == null) {+ return false;+ }+ return highSymbol.isGlobal();+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ Program program = context.getProgram();+ PluginTool tool = context.getTool();+ ClangToken tokenAtCursor = context.getTokenAtCursor();++ DataType dataType = null;+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ if (highSymbol == null) {+ return;+ }+ dataType = chooseDataType(tool, program, highSymbol.getDataType());++ if (dataType == null) {+ return;+ }++ DataTypeManager dataTypeManager = program.getDataTypeManager();+ boolean successfulMod = false;+ int transaction = program.startTransaction("Retype Global");+ try {+ if (dataType.getDataTypeManager() != dataTypeManager) {+ dataType = dataTypeManager.resolve(dataType, null);+ }+ HighFunctionDBUtil.updateDBVariable(highSymbol, null, dataType,+ SourceType.USER_DEFINED);+ successfulMod = true;+ }+ catch (DuplicateNameException e) {+ throw new AssertException("Unexpected exception", e);+ }+ catch (InvalidInputException e) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Failed to re-type variable '" + highSymbol.getName() + "': " + e.getMessage());+ }+ finally {+ program.endTransaction(transaction, successfulMod);+ }+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeLocalAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeLocalAction.javanew file mode 100644index 0000000000..3d5681f353--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeLocalAction.java@@ -0,0 +1,167 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.InputEvent;+import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import ghidra.app.decompiler.*;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.data.DataType;+import ghidra.program.model.data.DataTypeManager;+import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;+import ghidra.program.model.pcode.*;+import ghidra.program.model.symbol.SourceType;+import ghidra.util.Msg;+import ghidra.util.UndefinedFunction;+import ghidra.util.exception.*;++/**+ * Action triggered from a specific token in the decompiler window to change the data-type+ * associated with a variable in the local scope of the function. This can be an input parameter,+ * a stack variable, a variable associated with a register, or a "dynamic" variable. If the+ * variable does not already exist in the program database, it will be created using storage the+ * decompiler has assigned to the variable within its model. In either case, there is a preexisting+ * notion of variable storage. This action may allow the newly selected data-type to be of a+ * different size relative to this preexisting storage, constrained by other variables that might+ * already consume storage.+ *+ * If the selected variable is an input parameter, other input parameters within the decompiler+ * model will need to be committed, if they do not already exist in the database, as any parameters+ * committed to the database are forcing on the decompiler. Any new parameters committed this way+ * inherit their name from the decompiler model, but the parameters will not be type-locked, allowing+ * their data-type to "float".+ *+ */+public class RetypeLocalAction extends AbstractDecompilerAction {++ public RetypeLocalAction() {+ super("Retype Variable");+ setPopupMenuData(new MenuData(new String[] { "Retype Variable" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK));+ }++ private void retypeSymbol(Program program, HighSymbol highSymbol, Varnode exactSpot,+ DataType dt, PluginTool tool) {+ HighFunction hfunction = highSymbol.getHighFunction();++ boolean commitRequired = checkFullCommit(highSymbol, hfunction);+ if (commitRequired) {+ exactSpot = null; // Don't try to split out if commit is required+ }++ if (exactSpot != null) { // The user pointed at a particular usage, not just the vardecl+ try {+ HighVariable var = hfunction.splitOutMergeGroup(exactSpot.getHigh(), exactSpot);+ highSymbol = var.getSymbol();+ }+ catch (PcodeException e) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed", e.getMessage());+ return;+ }+ }+ DataTypeManager dataTypeManager = program.getDataTypeManager();+ boolean successfulMod = false;+ int transaction = program.startTransaction("Retype Variable");+ try {+ if (dt.getDataTypeManager() != dataTypeManager) {+ dt = dataTypeManager.resolve(dt, null);+ }+ if (commitRequired) {+ // Don't use datatypes of other parameters if the datatypes were floating.+ // Datatypes were floating if signature source was DEFAULT+ boolean useDataTypes =+ hfunction.getFunction().getSignatureSource() != SourceType.DEFAULT;+ try {+ HighFunctionDBUtil.commitParamsToDatabase(hfunction, useDataTypes,+ SourceType.USER_DEFINED);+ if (useDataTypes) {+ HighFunctionDBUtil.commitReturnToDatabase(hfunction,+ SourceType.USER_DEFINED);+ }+ }+ catch (DuplicateNameException e) {+ throw new AssertException("Unexpected exception", e);+ }+ catch (InvalidInputException e) {+ Msg.showError(this, null, "Parameter Commit Failed", e.getMessage());+ }+ }+ HighFunctionDBUtil.updateDBVariable(highSymbol, null, dt, SourceType.USER_DEFINED);+ successfulMod = true;+ }+ catch (DuplicateNameException e) {+ throw new AssertException("Unexpected exception", e);+ }+ catch (InvalidInputException e) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Failed to re-type variable '" + highSymbol.getName() + "': " + e.getMessage());+ }+ finally {+ program.endTransaction(transaction, successfulMod);+ }+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ if (tokenAtCursor == null) {+ return false;+ }+ if (tokenAtCursor instanceof ClangFieldToken) {+ return false;+ }+ if (tokenAtCursor.Parent() instanceof ClangReturnType) {+ return false;+ }+ if (!tokenAtCursor.isVariableRef()) {+ return false;+ }+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ if (highSymbol == null) {+ return false;+ }+ return !highSymbol.isGlobal();+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ Program program = context.getProgram();+ PluginTool tool = context.getTool();+ ClangToken tokenAtCursor = context.getTokenAtCursor();++ DataType dataType = null;+ HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());+ if (highSymbol == null) {+ return;+ }+ dataType = chooseDataType(tool, program, highSymbol.getDataType());++ if (dataType == null) {+ return;+ }+ retypeSymbol(program, highSymbol, tokenAtCursor.getVarnode(), dataType, tool);+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeReturnAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeReturnAction.javanew file mode 100644index 0000000000..22a89a868c--- /dev/null+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeReturnAction.java@@ -0,0 +1,117 @@+/* ###+ * IP: GHIDRA+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */+package ghidra.app.plugin.core.decompile.actions;++import java.awt.event.InputEvent;+import java.awt.event.KeyEvent;++import docking.action.KeyBindingData;+import docking.action.MenuData;+import docking.widgets.OptionDialog;+import ghidra.app.decompiler.ClangReturnType;+import ghidra.app.decompiler.ClangToken;+import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.framework.plugintool.PluginTool;+import ghidra.program.model.data.DataType;+import ghidra.program.model.data.DataTypeManager;+import ghidra.program.model.listing.Function;+import ghidra.program.model.listing.Program;+import ghidra.program.model.pcode.HighFunction;+import ghidra.program.model.pcode.HighFunctionDBUtil;+import ghidra.program.model.symbol.SourceType;+import ghidra.util.Msg;+import ghidra.util.UndefinedFunction;+import ghidra.util.exception.*;++/**+ * Action triggered from a specific token in the decompiler window to change the return type of+ * the function. The user selected data-type is permanently set as the return type. As the+ * return type is part of the function prototype and is forcing on the decompiler,+ * this action may trigger input parameters to be committed to the database as well. This situation+ * currently triggers a confirmation dialog. If new input parameters need to be committed, their+ * name and data-type are taken from the decompiler model.+ */+public class RetypeReturnAction extends AbstractDecompilerAction {++ public RetypeReturnAction() {+ super("Retype Return");+ setPopupMenuData(new MenuData(new String[] { "Retype Return" }, "Decompile"));+ setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK));+ }++ @Override+ protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {+ Function function = context.getFunction();+ if (function == null || function instanceof UndefinedFunction) {+ return false;+ }++ ClangToken tokenAtCursor = context.getTokenAtCursor();+ return (tokenAtCursor.Parent() instanceof ClangReturnType);+ }++ @Override+ protected void decompilerActionPerformed(DecompilerActionContext context) {+ Program program = context.getProgram();+ PluginTool tool = context.getTool();+ ClangToken tokenAtCursor = context.getTokenAtCursor();+ HighFunction highFunction = context.getHighFunction();+ Function function = highFunction.getFunction();+ DataTypeManager dataTypeManager = program.getDataTypeManager();++ ClangReturnType parent = (ClangReturnType) tokenAtCursor.Parent();+ DataType dataType = chooseDataType(tool, program, parent.getDataType());+ if (dataType == null) {+ return;+ }+ boolean commitRequired = checkFullCommit(null, highFunction);+ if (commitRequired) {+ int resp = OptionDialog.showOptionDialog(tool.getToolFrame(),+ "Parameter Commit Required",+ "Retyping the return value requires all other parameters to be committed!\nContinue with retype?",+ "Continue");+ if (resp != OptionDialog.OPTION_ONE) {+ return;+ }+ }+ boolean successfulMod = false;+ int transactionID = program.startTransaction("Retype return type");+ try {+ if (dataType.getDataTypeManager() != dataTypeManager) {+ dataType = dataTypeManager.resolve(dataType, null);+ }+ if (commitRequired) {+ try {+ HighFunctionDBUtil.commitParamsToDatabase(highFunction, true,+ SourceType.USER_DEFINED);+ }+ catch (DuplicateNameException e) {+ throw new AssertException("Unexpected exception", e);+ }+ catch (InvalidInputException e) {+ Msg.showError(this, null, "Parameter Commit Failed", e.getMessage());+ }+ }+ function.setReturnType(dataType, SourceType.USER_DEFINED);+ successfulMod = true;+ }+ catch (InvalidInputException e) {+ Msg.showError(this, tool.getToolFrame(), "Retype Failed",+ "Failed to re-type return type '" + getName() + "': " + e.getMessage());+ }+ program.endTransaction(transactionID, successfulMod);+ }+}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.javadeleted file mode 100644index d999e52281..0000000000--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RetypeVariableAction.java+++ /dev/null@@ -1,433 +0,0 @@-/* ###- * IP: GHIDRA- *- * Licensed under the Apache License, Version 2.0 (the "License");- * you may not use this file except in compliance with the License.- * You may obtain a copy of the License at- *- * http://www.apache.org/licenses/LICENSE-2.0- *- * Unless required by applicable law or agreed to in writing, software- * distributed under the License is distributed on an "AS IS" BASIS,- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.- * See the License for the specific language governing permissions and- * limitations under the License.- */-package ghidra.app.plugin.core.decompile.actions;--import java.awt.event.InputEvent;-import java.awt.event.KeyEvent;--import docking.action.KeyBindingData;-import docking.action.MenuData;-import docking.widgets.OptionDialog;-import ghidra.app.decompiler.*;-import ghidra.app.decompiler.component.DecompilerController;-import ghidra.app.decompiler.component.DecompilerPanel;-import ghidra.app.plugin.core.decompile.DecompilerActionContext;-import ghidra.app.util.datatype.DataTypeSelectionDialog;-import ghidra.framework.plugintool.PluginTool;-import ghidra.program.model.address.Address;-import ghidra.program.model.data.*;-import ghidra.program.model.listing.*;-import ghidra.program.model.pcode.*;-import ghidra.program.model.symbol.SourceType;-import ghidra.util.Msg;-import ghidra.util.UndefinedFunction;-import ghidra.util.data.DataTypeParser.AllowedDataTypes;-import ghidra.util.exception.*;--public class RetypeVariableAction extends AbstractDecompilerAction {- protected final DecompilerController controller;- private final PluginTool tool;-- protected RetypeVariableAction(String name, PluginTool tool, DecompilerController controller) {- super(name);- this.tool = tool;- this.controller = controller;- }-- public RetypeVariableAction(PluginTool tool, DecompilerController controller) {- super("Retype Variable");- this.tool = tool;- this.controller = controller;- setPopupMenuData(new MenuData(new String[] { "Retype Variable" }, "Decompile"));- setKeyBindingData(new KeyBindingData(KeyEvent.VK_L, InputEvent.CTRL_DOWN_MASK));-- }-- private void retypeReturnType(DataType dataType, ClangReturnType parent) {- Program program = controller.getProgram();- DataTypeManager dataTypeManager = program.getDataTypeManager();- HighFunction hfunction = getHighFunctionFromReturnTypeToken(parent);- if (hfunction == null) {- return;- }- boolean commitRequired = checkFullCommit(null, hfunction);- if (commitRequired) {- int resp = OptionDialog.showOptionDialog(tool.getToolFrame(),- "Parameter Commit Required",- "Retyping the return value requires all other parameters to be committed!\nContinue with retype?",- "Continue");- if (resp != OptionDialog.OPTION_ONE) {- return;- }- }- Function function = hfunction.getFunction();- boolean successfulMod = false;- int transactionID = program.startTransaction("Retype return type");- try {- if (dataType.getDataTypeManager() != dataTypeManager) {- dataType = dataTypeManager.resolve(dataType, null);- }- if (commitRequired) {- try {- HighFunctionDBUtil.commitParamsToDatabase(hfunction, true,- SourceType.USER_DEFINED);- }- catch (DuplicateNameException e) {- throw new AssertException("Unexpected exception", e);- }- catch (InvalidInputException e) {- Msg.showError(this, null, "Parameter Commit Failed", e.getMessage());- }- }- function.setReturnType(dataType, SourceType.USER_DEFINED);- successfulMod = true;- }- catch (InvalidInputException e) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed",- "Failed to re-type return type '" + getName() + "': " + e.getMessage());- }- program.endTransaction(transactionID, successfulMod);- }-- private HighFunction getHighFunctionFromReturnTypeToken(ClangReturnType returnType) {- Varnode varnode = returnType.getVarnode();- // varnode is null for void return type- if (varnode == null) {- ClangNode proto = returnType.Parent();- if (proto instanceof ClangFuncProto) {- ClangFunction func = ((ClangFuncProto) proto).getClangFunction();- if (func == null) {- return null;- }- return func.getHighFunction();- }- return null;- }- HighVariable high = varnode.getHigh();- return high.getHighFunction();- }-- private DataType chooseDataType(DataType currentDataType) {- Program program = controller.getProgram();- DataTypeManager dataTypeManager = program.getDataTypeManager();- DataTypeSelectionDialog chooserDialog = new DataTypeSelectionDialog(tool, dataTypeManager,- Integer.MAX_VALUE, AllowedDataTypes.FIXED_LENGTH);- chooserDialog.setInitialDataType(currentDataType);- tool.showDialog(chooserDialog);- return chooserDialog.getUserChosenDataType();- }-- protected void retypeSymbol(HighSymbol highSymbol, Varnode exactSpot, DataType dt) {- HighFunction hfunction = highSymbol.getHighFunction();-- boolean commitRequired = checkFullCommit(highSymbol, hfunction);- if (commitRequired) {- exactSpot = null; // Don't try to split out if commit is required- }-- if (exactSpot != null) { // The user pointed at a particular usage, not just the vardecl- try {- HighVariable var = hfunction.splitOutMergeGroup(exactSpot.getHigh(), exactSpot);- highSymbol = var.getSymbol();- }- catch (PcodeException e) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed", e.getMessage());- return;- }- }- Program program = controller.getProgram();- DataTypeManager dataTypeManager = program.getDataTypeManager();- boolean successfulMod = false;- int transaction = program.startTransaction("Retype Variable");- try {- if (dt.getDataTypeManager() != dataTypeManager) {- dt = dataTypeManager.resolve(dt, null);- }- if (commitRequired) {- // Don't use datatypes of other parameters if the datatypes were floating.- // Datatypes were floating if signature source was DEFAULT- boolean useDataTypes =- hfunction.getFunction().getSignatureSource() != SourceType.DEFAULT;- try {- HighFunctionDBUtil.commitParamsToDatabase(hfunction, useDataTypes,- SourceType.USER_DEFINED);- if (useDataTypes) {- HighFunctionDBUtil.commitReturnToDatabase(hfunction,- SourceType.USER_DEFINED);- }- }- catch (DuplicateNameException e) {- throw new AssertException("Unexpected exception", e);- }- catch (InvalidInputException e) {- Msg.showError(this, null, "Parameter Commit Failed", e.getMessage());- }- }- HighFunctionDBUtil.updateDBVariable(highSymbol, null, dt, SourceType.USER_DEFINED);- successfulMod = true;- }- catch (DuplicateNameException e) {- throw new AssertException("Unexpected exception", e);- }- catch (InvalidInputException e) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed",- "Failed to re-type variable '" + highSymbol.getName() + "': " + e.getMessage());- }- finally {- program.endTransaction(transaction, successfulMod);- }- }-- /**- * Return the index of the last component that would be overwritten by a new datatype, if we started overwriting- * with at a specific component. All components, except the first, must be undefined datatypes or we return -1- * @param struct is the structure we are testing- * @param comp is the starting component to overwrite- * @param newtype is the datatype to overwrite with- * @return the index of the last component overwritten or -1- */- private int getEndComponentIndex(Structure struct, DataTypeComponent comp, DataType newtype) {- int newlen = newtype.getLength();- if (newlen <= 0) {- return -1; // Don't support variable length types- }- DataType curtype = comp.getDataType();- newlen -= curtype.getLength();- int index = comp.getOrdinal();- while (newlen > 0) {- index += 1;- if (index >= struct.getNumComponents()) {- return -1; // Not enough space in the structure- }- comp = struct.getComponent(index);-// String nm = comp.getFieldName();-// if ((nm !=null)&&(nm.length()!=0))-// return -1;- curtype = comp.getDataType();-// if (!Undefined.isUndefined(curtype))-// return -1; // Overlaps non-undefined datatype- if (curtype != DataType.DEFAULT) {- return -1; // Only allow overwrite of placeholder components- }- newlen -= curtype.getLength();- }- return index;- }-- private void retypeStructVariable(Structure dt, DataTypeComponent comp, DataType newtype) {- Program program = controller.getProgram();- DataTypeManager dataTypeManager = program.getDataTypeManager();- boolean successfulMod = false;- if (comp == null) {- if (!dt.isNotYetDefined()) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed",- "Could not find component of '" + dt.getName() + "' to retype");- return;- }- // note if we reach here the offset must be zero, so assume we are inserting newtype- int transaction = program.startTransaction("Retype Structure Field");- try {- // Make sure datatype is using the program's data organization before testing fit- if (newtype.getDataTypeManager() != dataTypeManager) {- newtype = dataTypeManager.resolve(newtype, null);- }- dt.insert(0, newtype);- successfulMod = true;- }- finally {- program.endTransaction(transaction, successfulMod);- }- return;- }- int transaction = program.startTransaction("Retype Structure Field");- try {- // Make sure datatype is using the program's data organization before testing fit- if (newtype.getDataTypeManager() != dataTypeManager) {- newtype = dataTypeManager.resolve(newtype, null);- }- int startind = comp.getOrdinal();- int endind = getEndComponentIndex(dt, comp, newtype);- if (endind < 0) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed",- "Failed to re-type structure '" + dt.getName() + "': Datatype did not fit");- return;- }- for (int i = endind; i > startind; --i) { // Clear all but first field- dt.clearComponent(i);- }- dt.replaceAtOffset(comp.getOffset(), newtype, newtype.getLength(), comp.getFieldName(),- comp.getComment());- successfulMod = true;- }- finally {- program.endTransaction(transaction, successfulMod);- }- }-- /**- * Compare the given HighFunction's idea of the prototype with the Function's idea.- * Return true if there is a difference. If a specific symbol is being changed,- * it can be passed in to check whether or not the prototype is being affected.- * @param highSymbol (if not null) is the symbol being modified- * @param hfunction is the given HighFunction- * @return true if there is a difference (and a full commit is required)- */- public static boolean checkFullCommit(HighSymbol highSymbol, HighFunction hfunction) {- if (highSymbol != null && !highSymbol.isParameter()) {- return false;- }- Function function = hfunction.getFunction();- Parameter[] parameters = function.getParameters();- LocalSymbolMap localSymbolMap = hfunction.getLocalSymbolMap();- int numParams = localSymbolMap.getNumParams();- if (numParams != parameters.length) {- return true;- }-- for (int i = 0; i < numParams; i++) {- HighSymbol param = localSymbolMap.getParamSymbol(i);- if (param.getCategoryIndex() != i) {- return true;- }- VariableStorage storage = param.getStorage();- // Don't compare using the equals method so that DynamicVariableStorage can match- if (0 != storage.compareTo(parameters[i].getVariableStorage())) {- return true;- }- }-- return false;- }-- @Override- protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();- if (function instanceof UndefinedFunction) {- return false;- }-- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- if (tokenAtCursor == null) {- return false;- }- if (tokenAtCursor instanceof ClangFieldToken) {- DataType dt = RenameVariableAction.getStructDataType(tokenAtCursor);- if (dt == null) {- return false;- }- getPopupMenuData().setMenuItemName("Retype Field");- return true;- }- if (tokenAtCursor.Parent() instanceof ClangReturnType) {- getPopupMenuData().setMenuItemName("Retype Return");- return true;- }- if (!tokenAtCursor.isVariableRef()) {- return false;- }- HighVariable variable = tokenAtCursor.getHighVariable();- HighSymbol highSymbol = null;- if (variable == null) {- Address addr = RenameVariableAction.getStorageAddress(tokenAtCursor, controller);- if (addr == null) {- return false;- }- highSymbol = RenameVariableAction.findHighSymbol(addr, controller);- }- else {- highSymbol = variable.getSymbol();- }- if (highSymbol == null) {- return false;- }- if (highSymbol.isGlobal()) {- getPopupMenuData().setMenuItemName("Retype Global");- }- else {- getPopupMenuData().setMenuItemName("Retype Variable");- }- return true;- }-- @Override- protected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- HighSymbol highSymbol = null;- Structure struct = null;- DataTypeComponent comp = null;-- DataType dataType = null;- if (tokenAtCursor instanceof ClangFieldToken) {- struct = RenameVariableAction.getStructDataType(tokenAtCursor);- int offset = RenameVariableAction.getDataTypeOffset(tokenAtCursor);- if (struct == null) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed",- "Failed to re-type structure");- return;- }- if (offset < 0 || offset >= struct.getLength()) {- Msg.showError(this, tool.getToolFrame(), "Retype Failed",- "Failed to re-type structure: " + struct.getName());- return;- }- comp = struct.getComponentAt(offset);- if (comp == null) {- dataType = chooseDataType(DataType.DEFAULT);- }- else {- dataType = chooseDataType(comp.getDataType());- }- }- else if (tokenAtCursor.Parent() instanceof ClangReturnType) {- ClangReturnType parent = (ClangReturnType) tokenAtCursor.Parent();- dataType = chooseDataType(parent.getDataType());- if (dataType == null) {- return;- }- retypeReturnType(dataType, parent);- return;- }- else {- HighVariable variable = tokenAtCursor.getHighVariable();- if (variable == null) {- Address addr = RenameVariableAction.getStorageAddress(tokenAtCursor, controller);- if (addr == null) {- return;- }- highSymbol = RenameVariableAction.findHighSymbol(addr, controller);- }- else {- highSymbol = variable.getSymbol();- }- if (highSymbol == null) {- return;- }- dataType = chooseDataType(highSymbol.getDataType());- }-- if (dataType == null) {- return;- }- if (struct != null) {- retypeStructVariable(struct, comp, dataType);- }- else {- retypeSymbol(highSymbol, tokenAtCursor.getVarnode(), dataType);- }- }-}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightAction.javaindex e8cb698568..06012ff083 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightAction.java@@ -18,7 +18,6 @@ package ghidra.app.plugin.core.decompile.actions;import docking.action.MenuData;import ghidra.app.decompiler.ClangToken;import ghidra.app.decompiler.component.ClangHighlightController;-import ghidra.app.decompiler.component.DecompilerPanel;import ghidra.app.plugin.core.decompile.DecompilerActionContext;/**@@ -40,8 +39,7 @@ public class SetSecondaryHighlightAction extends AbstractSetSecondaryHighlightAc@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- DecompilerPanel panel = context.getDecompilerPanel();- ClangToken token = panel.getTokenAtCursor();- panel.addSecondaryHighlight(token);+ ClangToken token = context.getTokenAtCursor();+ context.getDecompilerPanel().addSecondaryHighlight(token);}}diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightColorChooserAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightColorChooserAction.javaindex c5ceb11de1..e147244ec4 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightColorChooserAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SetSecondaryHighlightColorChooserAction.java@@ -39,8 +39,8 @@ public class SetSecondaryHighlightColorChooserAction extends AbstractSetSecondar@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {+ ClangToken token = context.getTokenAtCursor();DecompilerPanel panel = context.getDecompilerPanel();- ClangToken token = panel.getTokenAtCursor();TokenHighlightColors colors = panel.getSecondaryHighlightColors();List<Color> recentColors = colors.getRecentColors();diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SpecifyCPrototypeAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SpecifyCPrototypeAction.javaindex 5967d60b5d..e447c8af8c 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SpecifyCPrototypeAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/SpecifyCPrototypeAction.java@@ -20,7 +20,7 @@ import java.util.List;import docking.action.MenuData;import ghidra.app.decompiler.ClangFuncNameToken;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.*;+import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.plugin.core.function.editor.*;import ghidra.app.services.DataTypeManagerService;@@ -33,13 +33,9 @@ import ghidra.program.model.symbol.SourceType;import ghidra.util.UndefinedFunction;public class SpecifyCPrototypeAction extends AbstractDecompilerAction {- private final DecompilerController controller;- private final PluginTool tool;- public SpecifyCPrototypeAction(PluginTool tool, DecompilerController controller) {+ public SpecifyCPrototypeAction() {super("Edit Function Signature");- this.tool = tool;- this.controller = controller;setPopupMenuData(new MenuData(new String[] { "Edit Function Signature" }, "Decompile"));}@@ -132,17 +128,16 @@ public class SpecifyCPrototypeAction extends AbstractDecompilerAction {}/**+ * @param function is the current function+ * @param tokenAtCursor is the user selected token* @return the currently highlighted function or the currently decompiled* function if there isn't one.*/- synchronized Function getFunction() {+ synchronized Function getFunction(Function function, ClangToken tokenAtCursor) {// try to look up the function that is at the current cursor location// If there isn't one, just use the function we are in.- DecompilerPanel decompilerPanel = controller.getDecompilerPanel();- ClangToken tokenAtCursor = decompilerPanel.getTokenAtCursor();- Function function = controller.getFunction();if (tokenAtCursor instanceof ClangFuncNameToken) {- Function tokenFunction = DecompilerUtils.getFunction(controller.getProgram(),+ Function tokenFunction = DecompilerUtils.getFunction(function.getProgram(),(ClangFuncNameToken) tokenAtCursor);if (tokenFunction != null) {function = tokenFunction;@@ -153,22 +148,24 @@ public class SpecifyCPrototypeAction extends AbstractDecompilerAction {@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {- Function function = controller.getFunction();+ Function function = context.getFunction();if (function instanceof UndefinedFunction) {return false;}- return getFunction() != null;+ return getFunction(function, context.getTokenAtCursor()) != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Function function = getFunction();+ Function function =+ getFunction(context.getFunction(), context.getTokenAtCursor());+ PluginTool tool = context.getTool();DataTypeManagerService service = tool.getService(DataTypeManagerService.class);FunctionEditorModel model = new FunctionEditorModel(service, function);- HighFunction hf = controller.getHighFunction();+ HighFunction hf = context.getHighFunction();FunctionPrototype functionPrototype = hf.getFunctionPrototype();// If editing the decompiled function (i.e., not a subfunction) and functiondiff --git a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/HighSymbolTest.java b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/HighSymbolTest.javaindex 30007d0cf4..cf7e588498 100644--- a/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/HighSymbolTest.java+++ b/Ghidra/Features/Decompiler/src/test.slow/java/ghidra/app/plugin/core/decompile/HighSymbolTest.java@@ -72,10 +72,11 @@ public class HighSymbolTest extends AbstractDecompilerTest {return provider.getController().getHighFunction();}- private void renameGlobalVariable(HighSymbol highSymbol, Varnode exact, String newName) {+ private void renameGlobalVariable(HighSymbol highSymbol, ClangToken tokenAtCursor,+ String newName) {Address addr = highSymbol.getStorage().getMinAddress();RenameGlobalVariableTask rename = new RenameGlobalVariableTask(provider.getTool(),- highSymbol.getName(), addr, highSymbol.getProgram());+ highSymbol.getProgram(), provider.getDecompilerPanel(), tokenAtCursor, addr);assertTrue(rename.isValid(newName));modifyProgram(p -> {@@ -84,9 +85,10 @@ public class HighSymbolTest extends AbstractDecompilerTest {waitForDecompiler();}- private void renameVariable(HighSymbol highSymbol, Varnode exact, String newName) {- RenameVariableTask rename = new RenameVariableTask(provider.getTool(), highSymbol, exact,- SourceType.USER_DEFINED);+ private void renameVariable(HighSymbol highSymbol, ClangToken tokenAtCursor, String newName) {+ RenameVariableTask rename =+ new RenameVariableTask(provider.getTool(), highSymbol.getProgram(),+ provider.getDecompilerPanel(), tokenAtCursor, highSymbol, SourceType.USER_DEFINED);assertTrue(rename.isValid(newName));modifyProgram(p -> {rename.commit();@@ -94,14 +96,14 @@ public class HighSymbolTest extends AbstractDecompilerTest {waitForDecompiler();}- private void renameExisting(HighSymbol highSymbol, Varnode exact, String newName) {+ private void renameExisting(HighSymbol highSymbol, ClangToken tokenAtCursor, String newName) {SymbolEntry oldEntry = highSymbol.getFirstWholeMap();long oldId = highSymbol.getId();if (highSymbol.isGlobal()) {- renameGlobalVariable(highSymbol, exact, newName);+ renameGlobalVariable(highSymbol, tokenAtCursor, newName);}else {- renameVariable(highSymbol, exact, newName);+ renameVariable(highSymbol, tokenAtCursor, newName);}Symbol symbol = program.getSymbolTable().getSymbol(oldId);assertEquals(symbol.getName(), newName);@@ -136,7 +138,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {assertEquals(data.getAddress().getOffset(), 0x10056a0L);assertEquals(data.getBaseDataType().getLength(), 2);- renameGlobalVariable(highSymbol, null, "newGlobal");+ renameGlobalVariable(highSymbol, token, "newGlobal");waitForDecompiler();line = getLineStarting("newGlobal");loc = loc(line.getLineNumber(), 5);@@ -153,7 +155,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {codeSymbol = highCode.getCodeSymbol();assertNotNull(codeSymbol);assertEquals(codeSymbol.getID(), highCode.getId());- renameExisting(highSymbol, null, "nameAgain");+ renameExisting(highSymbol, token, "nameAgain");}@Test@@ -181,7 +183,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {}assertTrue(stackCount > 0); // Verify speculative mergeassertTrue(regCount > 0);- renameVariable(highSymbol, token.getVarnode(), "newLocal");+ renameVariable(highSymbol, token, "newLocal");line = getLineStarting("newLocal");loc = loc(line.getLineNumber(), 5);token = line.getToken(loc);@@ -196,7 +198,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {assertFalse(highSymbol.isTypeLocked());assertEquals(numInst, variable.getInstances().length);assertEquals(variable.getRepresentative().getAddress().getOffset(), 0xfffffffffffffff0L);- renameExisting(highSymbol, null, "nameAgain");+ renameExisting(highSymbol, token, "nameAgain");}@Test@@ -213,7 +215,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {LocalSymbolMap lsym = highFunction.getLocalSymbolMap();HighSymbol highSymbol = lsym.findLocal(addr, null);assertEquals(highSymbol.getName(), "local_44");- renameVariable(highSymbol, token.getVarnode(), "newArray");+ renameVariable(highSymbol, token, "newArray");line = getLineStarting("wsprintfW");token = line.getToken(loc);assertTrue(token instanceof ClangVariableToken);@@ -228,7 +230,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {assertTrue(entry instanceof MappedEntry);assertEquals(entry.getStorage().getMinAddress(), addr);assertEquals(entry.getSize(), 64);- renameExisting(highSymbol, null, "nameAgain");+ renameExisting(highSymbol, token, "nameAgain");}@Test@@ -245,16 +247,18 @@ public class HighSymbolTest extends AbstractDecompilerTest {Address addr = entry.getStorage().getMinAddress();assertTrue(entry instanceof MappedEntry); // Comes back initially as untied stack locationassertEquals(addr.getAddressSpace().getName(), "register");- renameVariable(highSymbol, token.getVarnode(), "newReg");+ renameVariable(highSymbol, token, "newReg");line = getLineContaining("newReg < 0x40");assertNotNull(line);+ loc = loc(line.getLineNumber(), 11);+ token = line.getToken(loc);HighFunction highFunction = getHighFunction();highSymbol = highFunction.getLocalSymbolMap().findLocal(addr, entry.getPCAdress());assertNotNull(highSymbol);assertEquals(highSymbol.getName(), "newReg");assertTrue(highSymbol.isNameLocked());assertFalse(highSymbol.isTypeLocked());- renameExisting(highSymbol, null, "nameAgain");+ renameExisting(highSymbol, token, "nameAgain");}@Test@@ -273,7 +277,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {SymbolEntry entry = highSymbol.getFirstWholeMap();Address addr = entry.getStorage().getMinAddress();assertEquals(addr.getOffset(), 8L);- renameExisting(highSymbol, null, "paramAgain");+ renameExisting(highSymbol, token, "paramAgain");}@Test@@ -289,7 +293,7 @@ public class HighSymbolTest extends AbstractDecompilerTest {SymbolEntry entry = highSymbol.getFirstWholeMap();assertTrue(entry instanceof MappedEntry);Address usepoint = token.getVarnode().getPCAddress();- renameVariable(highSymbol, token.getVarnode(), "newLocal");+ renameVariable(highSymbol, token, "newLocal");line = getLineContaining("0x4e");token = line.getToken(loc);assertTrue(token instanceof ClangVariableToken);