Populate GlobalSymbolMap with symbol references

This commit is contained in:
caheckman 2019-12-05 17:15:05 -05:00
parent 917d2492e4
commit d552aa4b82
4 changed files with 66 additions and 29 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;
}
}