Merge remote-tracking branch

'origin/GP-2520_ghidra1_DecompilerActionsAffectingThunks' (Closes #4566)
This commit is contained in:
Ryan Kurtz 2022-09-14 11:27:55 -04:00
commit 145abee008
4 changed files with 36 additions and 60 deletions

View file

@ -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)) {
return null;
}
ClangToken token = ((DecompilerLocation) location).getToken();
ClangToken token = context.getTokenAtCursor();
if (!(token instanceof ClangFuncNameToken)) {
return null;
}
Program program = location.getProgram();
return DecompilerUtils.getFunction(program, (ClangFuncNameToken) token);
Program program = context.getProgram();
Function f = DecompilerUtils.getFunction(program, (ClangFuncNameToken) token);
if (f == null) {
return null;
}
// Ignore default thunks
while (f.isThunk() && f.getSymbol().getSource() == SourceType.DEFAULT) {
f = f.getThunkedFunction(false);
}
return f;
}
/**

View file

@ -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;
}
@Override
protected boolean isEnabledForDecompilerContext(DecompilerActionContext context) {
Function func = getFunction(context);

View file

@ -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 {
@Override
protected 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());
}
}
}

View file

@ -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;
}
@Override
protected 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);