Skip to content

Ghidra is a software reverse engineering (SRE) framework

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

caheckman0d9e25548aaa
diff --git a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java
index da9365cad8..76ba1d60f0 100644
--- a/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java
+++ b/Ghidra/Features/Decompiler/src/main/java/ghidra/app/decompiler/DecompileCallback.java
@@ -704,7 +704,7 @@ public class DecompileCallback {
hfunc.grabFromFunction(extrapop, false, (extrapop != default_extrapop));
HighSymbol funcSymbol = new HighFunctionSymbol(addr, 2, hfunc);
- Namespace namespc = func.getParentNamespace();
+ Namespace namespc = funcSymbol.getNamespace();
if (debug != null) {
debug.getFNTypes(hfunc);
}
@@ -945,7 +945,7 @@ public class DecompileCallback {
(extrapop != default_extrapop));
HighSymbol functionSymbol = new HighFunctionSymbol(entry, (int) (diff + 1), hfunc);
- Namespace namespc = func.getParentNamespace();
+ Namespace namespc = functionSymbol.getNamespace();
if (debug != null) {
debug.getFNTypes(hfunc);
}
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.java
index 45dcbbc356..edf03cd90b 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
@@ -467,8 +467,8 @@ public class HighFunction extends PcodeSyntaxTree {
proto.buildPrototypeXML(resBuf, getDataTypeManager());
if ((jumpTables != null) && (jumpTables.size() > 0)) {
resBuf.append("<jumptablelist>\n");
- for (int i = 0; i < jumpTables.size(); ++i) {
- jumpTables.get(i).buildXml(resBuf);
+ for (JumpTable jumpTable : jumpTables) {
+ jumpTable.buildXml(resBuf);
}
resBuf.append("</jumptablelist>\n");
}
@@ -478,8 +478,7 @@ public class HighFunction extends PcodeSyntaxTree {
}
if ((protoOverrides != null) && (protoOverrides.size() > 0)) {
PcodeDataTypeManager dtmanage = getDataTypeManager();
- for (int i = 0; i < protoOverrides.size(); ++i) {
- DataTypeSymbol sym = protoOverrides.get(i);
+ for (DataTypeSymbol sym : protoOverrides) {
Address addr = sym.getAddress();
FunctionPrototype fproto = new FunctionPrototype(
(FunctionSignature) sym.getDataType(), compilerSpec, false);
@@ -626,6 +625,9 @@ public class HighFunction extends PcodeSyntaxTree {
Namespace curspc = namespc;
while (curspc != null) {
arr.add(0, curspc.getName());
+ if (curspc instanceof Library) {
+ break; // Treat library namespace as root
+ }
curspc = curspc.getParentNamespace();
}
buf.append("<val/>\n"); // Force global scope to have empty name
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionSymbol.java
index a5eb542e3e..fbd1d181f4 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionSymbol.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighFunctionSymbol.java
@@ -17,7 +17,9 @@ package ghidra.program.model.pcode;
import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
+import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.VariableStorage;
+import ghidra.program.model.symbol.Namespace;
import ghidra.util.exception.InvalidInputException;
/**
@@ -54,6 +56,21 @@ public class HighFunctionSymbol extends HighSymbol {
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
public void saveXML(StringBuilder buf) {
MappedEntry entry = (MappedEntry) getFirstWholeMap();
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java
index 1a106184c1..2a3c2b3fd3 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/pcode/HighSymbol.java
@@ -19,6 +19,7 @@ import ghidra.program.model.address.Address;
import ghidra.program.model.data.DataType;
import ghidra.program.model.listing.Program;
import ghidra.program.model.listing.VariableStorage;
+import ghidra.program.model.symbol.Namespace;
import ghidra.program.model.symbol.Symbol;
import ghidra.util.xml.SpecXmlUtils;
import ghidra.xml.XmlElement;
@@ -130,6 +131,18 @@ public class HighSymbol {
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
* into the decompiler's description of how a function manipulates a particular symbol.