Resolve thunk namespaces, don't send Library namespaces to decompiler

This commit is contained in:
caheckman 2020-06-18 12:02:56 -04:00
parent f7a8e264aa
commit 0d9e25548a
4 changed files with 38 additions and 6 deletions

View file

@ -704,7 +704,7 @@ public class DecompileCallback {
hfunc.grabFromFunction(extrapop, false, (extrapop != default_extrapop)); hfunc.grabFromFunction(extrapop, false, (extrapop != default_extrapop));
HighSymbol funcSymbol = new HighFunctionSymbol(addr, 2, hfunc); HighSymbol funcSymbol = new HighFunctionSymbol(addr, 2, hfunc);
Namespace namespc = func.getParentNamespace(); Namespace namespc = funcSymbol.getNamespace();
if (debug != null) { if (debug != null) {
debug.getFNTypes(hfunc); debug.getFNTypes(hfunc);
} }
@ -945,7 +945,7 @@ public class DecompileCallback {
(extrapop != default_extrapop)); (extrapop != default_extrapop));
HighSymbol functionSymbol = new HighFunctionSymbol(entry, (int) (diff + 1), hfunc); HighSymbol functionSymbol = new HighFunctionSymbol(entry, (int) (diff + 1), hfunc);
Namespace namespc = func.getParentNamespace(); Namespace namespc = functionSymbol.getNamespace();
if (debug != null) { if (debug != null) {
debug.getFNTypes(hfunc); debug.getFNTypes(hfunc);
} }

View file

@ -467,8 +467,8 @@ public class HighFunction extends PcodeSyntaxTree {
proto.buildPrototypeXML(resBuf, getDataTypeManager()); proto.buildPrototypeXML(resBuf, getDataTypeManager());
if ((jumpTables != null) && (jumpTables.size() > 0)) { if ((jumpTables != null) && (jumpTables.size() > 0)) {
resBuf.append("<jumptablelist>\n"); resBuf.append("<jumptablelist>\n");
for (int i = 0; i < jumpTables.size(); ++i) { for (JumpTable jumpTable : jumpTables) {
jumpTables.get(i).buildXml(resBuf); jumpTable.buildXml(resBuf);
} }
resBuf.append("</jumptablelist>\n"); resBuf.append("</jumptablelist>\n");
} }
@ -478,8 +478,7 @@ public class HighFunction extends PcodeSyntaxTree {
} }
if ((protoOverrides != null) && (protoOverrides.size() > 0)) { if ((protoOverrides != null) && (protoOverrides.size() > 0)) {
PcodeDataTypeManager dtmanage = getDataTypeManager(); PcodeDataTypeManager dtmanage = getDataTypeManager();
for (int i = 0; i < protoOverrides.size(); ++i) { for (DataTypeSymbol sym : protoOverrides) {
DataTypeSymbol sym = protoOverrides.get(i);
Address addr = sym.getAddress(); Address addr = sym.getAddress();
FunctionPrototype fproto = new FunctionPrototype( FunctionPrototype fproto = new FunctionPrototype(
(FunctionSignature) sym.getDataType(), compilerSpec, false); (FunctionSignature) sym.getDataType(), compilerSpec, false);
@ -626,6 +625,9 @@ public class HighFunction extends PcodeSyntaxTree {
Namespace curspc = namespc; Namespace curspc = namespc;
while (curspc != null) { while (curspc != null) {
arr.add(0, curspc.getName()); arr.add(0, curspc.getName());
if (curspc instanceof Library) {
break; // Treat library namespace as root
}
curspc = curspc.getParentNamespace(); curspc = curspc.getParentNamespace();
} }
buf.append("<val/>\n"); // Force global scope to have empty name buf.append("<val/>\n"); // Force global scope to have empty name

View file

@ -17,7 +17,9 @@ package ghidra.program.model.pcode;
import ghidra.program.model.address.Address; import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType; import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.VariableStorage; import ghidra.program.model.listing.VariableStorage;
import ghidra.program.model.symbol.Namespace;
import ghidra.util.exception.InvalidInputException; import ghidra.util.exception.InvalidInputException;
/** /**
@ -54,6 +56,21 @@ public class HighFunctionSymbol extends HighSymbol {
return true; return true;
} }
@Override
public Namespace getNamespace() {
Function func = function.getFunction();
Namespace namespc = func.getParentNamespace();
if (func.isThunk()) {
// Thunks can be in a different namespace than the thunked function.
// We choose the thunk's namespace unless it is the global namespace
if (namespc.getID() == Namespace.GLOBAL_NAMESPACE_ID) {
Function baseFunc = func.getThunkedFunction(true);
namespc = baseFunc.getParentNamespace();
}
}
return namespc;
}
@Override @Override
public void saveXML(StringBuilder buf) { public void saveXML(StringBuilder buf) {
MappedEntry entry = (MappedEntry) getFirstWholeMap(); MappedEntry entry = (MappedEntry) getFirstWholeMap();

View file

@ -19,6 +19,7 @@ import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType; import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.Program; import ghidra.program.model.listing.Program;
import ghidra.program.model.listing.VariableStorage; import ghidra.program.model.listing.VariableStorage;
import ghidra.program.model.symbol.Namespace;
import ghidra.program.model.symbol.Symbol; import ghidra.program.model.symbol.Symbol;
import ghidra.util.xml.SpecXmlUtils; import ghidra.util.xml.SpecXmlUtils;
import ghidra.xml.XmlElement; import ghidra.xml.XmlElement;
@ -130,6 +131,18 @@ public class HighSymbol {
return null; return null;
} }
/**
* Fetch the namespace owning this symbol, if it exists.
* @return the Namespace object or null
*/
public Namespace getNamespace() {
Symbol sym = getSymbol();
if (sym != null) {
return sym.getParentNamespace();
}
return null;
}
/** /**
* Associate a particular HighVariable with this symbol. This is used to link the symbol * Associate a particular HighVariable with this symbol. This is used to link the symbol
* into the decompiler's description of how a function manipulates a particular symbol. * into the decompiler's description of how a function manipulates a particular symbol.