Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-6465 Corrected CreateThunkFunctionCmd re-use of existing External symbol

ghidra14467561830f0
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java
index 52ea98aa69..28ca122bb1 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/cmd/function/CreateThunkFunctionCmd.java
@@ -669,10 +669,12 @@ public class CreateThunkFunctionCmd extends BackgroundCommand<Program> {
}
/**
- * Handle conversion of label within reserved EXTERNAL block to a real
- * external function which can be thunked. This may be necessary when a
- * loaded symbol failed to identify itself as a function. This will
- * only handle single symbols contained within the global namespace.
+ * Facilitates conversion of a global label within the reserved EXTERNAL block to an
+ * external function which can be thunked. This may be necessary when a loaded symbol
+ * failed to identify itself as a function.
+ * <p>
+ * NOTE: If a suitable External symbol is found or created the original symbol for the
+ * thunk function will be removed.
*
* @param program the program
* @param entry function being created
@@ -696,10 +698,29 @@ public class CreateThunkFunctionCmd extends BackgroundCommand<Program> {
}
try {
ExternalManager extMgr = program.getExternalManager();
- ExternalLocation extLoc =
- extMgr.addExtFunction(Library.UNKNOWN, s.getName(), null, s.getSource());
+
+ Symbol externalSymbol = null;
+ for (Symbol symbol : symbolTable.getSymbols(s.getName())) {
+ // Only consider external symbols directly in a Library root Namespace
+ if (!symbol.isExternal() || !symbol.getParentNamespace().isLibrary()) {
+ continue;
+ }
+ if (externalSymbol != null) {
+ // multiple ambiguous external symbols - can't decide
+ return null;
+ }
+ externalSymbol = symbol;
+ }
+
+ if (externalSymbol == null) {
+ ExternalLocation extLoc =
+ extMgr.addExtFunction(Library.UNKNOWN, s.getName(), null, s.getSource());
+ externalSymbol = extLoc.getSymbol();
+ }
+
s.delete(); // remove original symbol from EXTERNAL block
- return extLoc.getExternalSpaceAddress();
+
+ return externalSymbol.getAddress();
}
catch (DuplicateNameException | InvalidInputException e) {
// ignore - unexpected