Ghidra is a software reverse engineering (SRE) framework
Populate GlobalSymbolMap with symbol references
caheckmand552aa4b8217
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.javaindex 8982e208d0..6e00b90655 100644--- 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.java@@ -25,7 +25,6 @@ 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.address.AddressSpace;import ghidra.program.model.data.*;import ghidra.program.model.listing.Function;import ghidra.program.model.listing.Program;@@ -59,25 +58,8 @@ public class RenameVariableAction extends AbstractDecompilerAction {// op could be a PTRSUB, need to dig it out...else if (tokenAtCursor instanceof ClangVariableToken) {PcodeOp op = ((ClangVariableToken) tokenAtCursor).getPcodeOp();- if (op == null) {- return null;- }- if (op.getOpcode() == PcodeOp.PTRSUB) {- vnode = op.getInput(0);- if (vnode.isRegister()) {- AddressSpace stackspace =- controller.getProgram().getAddressFactory().getStackSpace();- if (stackspace != null) {- Address caddr = op.getInput(1).getAddress();- storageAddress = stackspace.getAddress(caddr.getOffset());- }- }- else {- Address caddr = op.getInput(1).getAddress();- storageAddress =- controller.getLocation().getAddress().getNewAddress(caddr.getOffset());- }- }+ storageAddress =+ HighFunctionDBUtil.getSpacebaseReferenceAddress(controller.getProgram(), op);}return storageAddress;}@@ -94,9 +76,13 @@ public class RenameVariableAction extends AbstractDecompilerAction {}else {GlobalSymbolMap gsym = hfunc.getGlobalSymbolMap();- HighSymbol hsym = gsym.getSymbol(addr);+ HighCodeSymbol hsym = gsym.getSymbol(addr);if (hsym != null) {res = hsym.getHighVariable();+ if (res == null) {+ Varnode vnrep = new Varnode(addr, hsym.getSize());+ res = new HighGlobal(hsym, vnrep, null);+ }}}return res;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/GlobalSymbolMap.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/GlobalSymbolMap.javaindex b07a0c336e..8347761358 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/GlobalSymbolMap.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/GlobalSymbolMap.java@@ -21,6 +21,7 @@ import java.util.Iterator;import ghidra.program.database.symbol.CodeSymbol;import ghidra.program.model.address.Address;import ghidra.program.model.data.DataType;+import ghidra.program.model.listing.Data;import ghidra.program.model.listing.Program;import ghidra.program.model.symbol.Symbol;import ghidra.program.model.symbol.SymbolTable;@@ -71,6 +72,17 @@ public class GlobalSymbolMap {if (symbol == null || !(symbol instanceof CodeSymbol)) {return null;}+ if (dataType == null) {+ Object dataObj = symbol.getObject();+ if (dataObj instanceof Data) {+ dataType = ((Data) dataObj).getDataType();+ sz = dataType.getLength();+ }+ else {+ dataType = DataType.DEFAULT;+ sz = 1;+ }+ }HighCodeSymbol highSym = new HighCodeSymbol((CodeSymbol) symbol, dataType, sz, func);insertSymbol(highSym, symbol.getAddress());return highSym;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunction.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunction.javaindex a567d939f5..3d85795afc 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunction.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunction.java@@ -357,20 +357,30 @@ public class HighFunction extends PcodeSyntaxTree {HighSymbol sym = null;if (symref != 0) {sym = localSymbols.getSymbol(symref);- if (sym != null) {- var = sym.getHighVariable();+ if (sym == null) {+ sym = globalSymbols.getSymbol(symref);}- }- if (var == null) {if (sym instanceof DynamicSymbol) {+ var = sym.getHighVariable();var = new HighConstant(sym.getName(), tp, rep, getPCAddress(rep),(DynamicSymbol) sym);sym.setHighVariable(var);}- else {- var = new HighConstant(null, tp, rep, getPCAddress(rep), this);+ else if (sym == null) {+ sym = globalSymbols.populateSymbol(symref, null, -1);+ if (sym == null) {+ PcodeOp op = ((VarnodeAST) rep).getLoneDescend();+ Address addr =+ HighFunctionDBUtil.getSpacebaseReferenceAddress(func.getProgram(), op);+ if (addr != null) {+ sym = globalSymbols.newSymbol(symref, addr, DataType.DEFAULT, 1);+ }+ }}}+ if (var == null) {+ var = new HighConstant(null, tp, rep, getPCAddress(rep), this);+ }}else if (classstring.equals("global")) {HighCodeSymbol sym = null;diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.javaindex 377d2a4645..2b079358d1 100644--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionDBUtil.java@@ -17,8 +17,7 @@ package ghidra.program.model.pcode;import java.util.*;-import ghidra.program.model.address.Address;-import ghidra.program.model.address.AddressOverflowException;+import ghidra.program.model.address.*;import ghidra.program.model.data.*;import ghidra.program.model.lang.Register;import ghidra.program.model.listing.*;@@ -640,4 +639,34 @@ public class HighFunctionDBUtil {return datsym;}+ /**+ * Get the Address referred to by a spacebase reference. Address-of references are encoded in+ * the p-code syntax tree as: vn = PTRSUB(<spacebase>, #const). This decodes the reference and+ * returns the Address+ * @param program is the program containing the Address+ * @param op is the PTRSUB op encoding the reference+ * @return the recovered Address (or null if not correct form)+ */+ public static Address getSpacebaseReferenceAddress(Program program, PcodeOp op) {+ Address storageAddress = null;+ if (op == null) {+ return storageAddress;+ }+ if (op.getOpcode() == PcodeOp.PTRSUB) {+ Varnode vnode = op.getInput(0);+ if (vnode.isRegister()) {+ AddressSpace stackspace = program.getAddressFactory().getStackSpace();+ if (stackspace != null) {+ Address caddr = op.getInput(1).getAddress();+ storageAddress = stackspace.getAddress(caddr.getOffset());+ }+ }+ else {+ Address caddr = op.getInput(1).getAddress();+ storageAddress = program.getAddressFactory().getDefaultAddressSpace().getAddress(+ caddr.getOffset());+ }+ }+ return storageAddress;+ }}