Ghidra is a software reverse engineering (SRE) framework
Use AddEditDialog for decompiler "Rename Global" action
caheckman4f63568bdb4d
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.javaindex 2c71102eb6..c8c384f994 100644--- 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.java@@ -22,11 +22,14 @@ import docking.action.MenuData;import ghidra.app.decompiler.ClangFieldToken;import ghidra.app.decompiler.ClangToken;import ghidra.app.plugin.core.decompile.DecompilerActionContext;+import ghidra.app.util.AddEditDialog;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.program.model.symbol.Symbol;+import ghidra.program.model.symbol.SymbolTable;import ghidra.util.Msg;import ghidra.util.UndefinedFunction;@@ -69,18 +72,22 @@ public class RenameGlobalAction extends AbstractDecompilerAction {PluginTool tool = context.getTool();final ClangToken tokenAtCursor = context.getTokenAtCursor();HighSymbol highSymbol = findHighSymbolFromToken(tokenAtCursor, context.getHighFunction());- Address addr = null;+ Symbol symbol = null;if (highSymbol instanceof HighCodeSymbol) {- addr = ((HighCodeSymbol) highSymbol).getStorage().getMinAddress();+ symbol = ((HighCodeSymbol) highSymbol).getCodeSymbol();+ if (symbol == null) {+ // Try to get the dynamic symbol+ Address addr = ((HighCodeSymbol) highSymbol).getStorage().getMinAddress();+ SymbolTable symbolTable = context.getProgram().getSymbolTable();+ symbol = symbolTable.getPrimarySymbol(addr);+ }}- if (addr == null || !addr.isMemoryAddress()) {+ if (symbol == null) {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(true);+ AddEditDialog dialog = new AddEditDialog("Rename Global", context.getTool());+ dialog.editLabel(symbol, context.getProgram());}}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.javadeleted file mode 100644index 47f7d0a2d9..0000000000--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/plugin/core/decompile/actions/RenameGlobalVariableTask.java+++ /dev/null@@ -1,68 +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.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;-import ghidra.program.model.symbol.*;-import ghidra.util.exception.DuplicateNameException;-import ghidra.util.exception.InvalidInputException;--public class RenameGlobalVariableTask extends RenameTask {- private Address address; // Address of global variable- private SymbolTable symboltable;- private Symbol symbol;-- public RenameGlobalVariableTask(PluginTool tool, Program program, DecompilerPanel panel,- ClangToken token, Address addr) {- super(tool, program, panel, token, token.getText());- address = addr;- symboltable = program.getSymbolTable();- symbol = null;- }-- @Override- public void commit() throws DuplicateNameException, InvalidInputException {- if (symbol != null) {- symbol.setName(newName, SourceType.USER_DEFINED);- }- else {- symboltable.createLabel(address, newName, SourceType.USER_DEFINED);- }- }-- @Override- public String getTransactionName() {- return "Rename Global Variable";- }-- @Override- public boolean isValid(String newNm) {- newName = newNm;- Symbol dupsym = symboltable.getGlobalSymbol(newName, address);- if (dupsym != null) {- errorMsg = "Duplicate symbol name";- return false;- }- symbol = symboltable.getPrimarySymbol(address);-- return true;- }--}diff --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 06244e3876..659c518a1e 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@@ -25,11 +25,13 @@ import docking.widgets.fieldpanel.field.Field;import docking.widgets.fieldpanel.support.FieldLocation;import ghidra.app.cmd.function.CreateFunctionCmd;import ghidra.app.cmd.function.DeleteFunctionCmd;+import ghidra.app.cmd.label.RenameLabelCmd;import ghidra.app.decompiler.ClangToken;import ghidra.app.decompiler.ClangVariableToken;import ghidra.app.decompiler.component.ClangTextField;import ghidra.app.decompiler.component.DecompilerPanel;-import ghidra.app.plugin.core.decompile.actions.*;+import ghidra.app.plugin.core.decompile.actions.IsolateVariableTask;+import ghidra.app.plugin.core.decompile.actions.RenameVariableTask;import ghidra.framework.options.Options;import ghidra.program.database.symbol.CodeSymbol;import ghidra.program.model.address.Address;@@ -79,12 +81,11 @@ public class HighSymbolTest extends AbstractDecompilerTest {private void renameGlobalVariable(HighSymbol highSymbol, ClangToken tokenAtCursor,String newName) {Address addr = highSymbol.getStorage().getMinAddress();- RenameGlobalVariableTask rename = new RenameGlobalVariableTask(provider.getTool(),- highSymbol.getProgram(), provider.getDecompilerPanel(), tokenAtCursor, addr);+ RenameLabelCmd cmd =+ new RenameLabelCmd(addr, highSymbol.getName(), newName, SourceType.USER_DEFINED);- assertTrue(rename.isValid(newName));modifyProgram(p -> {- rename.commit();+ cmd.applyTo(highSymbol.getProgram());});waitForDecompiler();}