Ghidra is a software reverse engineering (SRE) framework
GP-2520 correct decompiler actions affecting thunk functions
ghidra1fcf20ed9a5ac
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 a2ef36cd78..970047aa8b 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@@ -28,9 +28,7 @@ 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.Symbol;-import ghidra.program.model.symbol.SymbolTable;-import ghidra.program.util.ProgramLocation;+import ghidra.program.model.symbol.*;import ghidra.util.UndefinedFunction;import ghidra.util.data.DataTypeParser.AllowedDataTypes;@@ -231,19 +229,30 @@ public abstract class AbstractDecompilerAction extends DockingAction {return symbolTable.getPrimarySymbol(address);}+ /**+ * Get the function corresponding to the specified decompiler context.+ *+ * @param context decompiler action context+ * @return the function associated with the current context token or null if none identified.+ */protected Function getFunction(DecompilerActionContext context) {- ProgramLocation location = context.getLocation();- if (!(location instanceof DecompilerLocation)) {++ ClangToken token = context.getTokenAtCursor();+ if (!(token instanceof ClangFuncNameToken)) {return null;}- ClangToken token = ((DecompilerLocation) location).getToken();- if (!(token instanceof ClangFuncNameToken)) {+ Program program = context.getProgram();+ Function f = DecompilerUtils.getFunction(program, (ClangFuncNameToken) token);+ if (f == null) {return null;}- Program program = location.getProgram();- return DecompilerUtils.getFunction(program, (ClangFuncNameToken) token);+ // Ignore default thunks+ while (f.isThunk() && f.getSymbol().getSource() == SourceType.DEFAULT) {+ f = f.getThunkedFunction(false);+ }+ return f;}/**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 b342f072df..684b46e934 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@@ -20,17 +20,13 @@ import java.util.Objects;import docking.action.KeyBindingData;import docking.action.MenuData;-import ghidra.app.decompiler.ClangFuncNameToken;import ghidra.app.decompiler.ClangToken;-import ghidra.app.decompiler.component.DecompilerUtils;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.plugin.core.decompile.DecompilerProvider;import ghidra.app.util.AddEditDialog;import ghidra.app.util.HelpTopics;import ghidra.program.model.listing.Function;import ghidra.program.model.listing.Program;-import ghidra.program.model.pcode.HighFunctionShellSymbol;-import ghidra.program.model.pcode.HighSymbol;import ghidra.program.model.symbol.Symbol;import ghidra.util.HelpLocation;import ghidra.util.UndefinedFunction;@@ -44,23 +40,6 @@ public class RenameFunctionAction extends AbstractDecompilerAction {setPopupMenuData(new MenuData(new String[] { "Rename Function" }, "Decompile"));}- @Override- protected Function getFunction(DecompilerActionContext context) {- Program program = context.getProgram();- ClangToken tokenAtCursor = context.getTokenAtCursor();-- // 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.- if (tokenAtCursor instanceof ClangFuncNameToken) {- return DecompilerUtils.getFunction(program, (ClangFuncNameToken) tokenAtCursor);- }- HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());- if (highSymbol instanceof HighFunctionShellSymbol) {- return (Function) highSymbol.getSymbol().getObject();- }- return null;- }-@Overrideprotected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {Function func = getFunction(context);diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLabelAction.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLabelAction.javaindex 02badc18ff..5b4f4093c8 100644--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLabelAction.java+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameLabelAction.java@@ -24,10 +24,7 @@ import ghidra.app.decompiler.ClangToken;import ghidra.app.plugin.core.decompile.DecompilerActionContext;import ghidra.app.util.AddEditDialog;import ghidra.app.util.HelpTopics;-import ghidra.program.model.address.Address;-import ghidra.program.model.listing.Program;import ghidra.program.model.symbol.Symbol;-import ghidra.program.model.symbol.SymbolTable;import ghidra.util.HelpLocation;public class RenameLabelAction extends AbstractDecompilerAction {@@ -50,12 +47,11 @@ public class RenameLabelAction extends AbstractDecompilerAction {@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Program program = context.getProgram();- SymbolTable symbolTable = program.getSymbolTable();- Address address = context.getAddress();- Symbol symbol = symbolTable.getPrimarySymbol(address);- AddEditDialog dialog = new AddEditDialog("", context.getTool());- dialog.editLabel(symbol, context.getProgram());+ Symbol symbol = getSymbol(context);+ if (symbol != null) {+ AddEditDialog dialog = new AddEditDialog("", context.getTool());+ dialog.editLabel(symbol, context.getProgram());+ }}}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 d9991bf397..d825ac8516 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@@ -18,9 +18,6 @@ package ghidra.app.plugin.core.decompile.actions;import java.util.List;import docking.action.MenuData;-import ghidra.app.decompiler.ClangFuncNameToken;-import ghidra.app.decompiler.ClangToken;-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;@@ -132,22 +129,19 @@ 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.+ * Get function affected by specified action context+ *+ * @param function is the current decompiled function which will be the default if no other+ * function identified by context token.+ * @param context decompiler action context+ * @return the function associated with the current context token. If no function corresponds+ * to context token the decompiled function will be returned.*/- synchronized Function getFunction(Function function, ClangToken tokenAtCursor) {+ private Function getFunction(Function function, DecompilerActionContext context) {// 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.- if (tokenAtCursor instanceof ClangFuncNameToken) {- Function tokenFunction = DecompilerUtils.getFunction(function.getProgram(),- (ClangFuncNameToken) tokenAtCursor);- if (tokenFunction != null) {- function = tokenFunction;- }- }- return function;+ Function tokenFunction = getFunction(context);+ return tokenFunction != null ? tokenFunction : function;}@Override@@ -156,14 +150,12 @@ public class SpecifyCPrototypeAction extends AbstractDecompilerAction {if (function instanceof UndefinedFunction) {return false;}-- return getFunction(function, context.getTokenAtCursor()) != null;+ return getFunction(function, context) != null;}@Overrideprotected void decompilerActionPerformed(DecompilerActionContext context) {- Function function =- getFunction(context.getFunction(), context.getTokenAtCursor());+ Function function = getFunction(context.getFunction(), context);PluginTool tool = context.getTool();DataTypeManagerService service = tool.getService(DataTypeManagerService.class);