Ghidra is a software reverse engineering (SRE) framework
GP-17 additional thunk merge corrections
ghidra1f87fa761fd48
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/SymbolMerger.java b/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/SymbolMerger.javaindex d947cc42cc..0199747732 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/SymbolMerger.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/SymbolMerger.java@@ -934,14 +934,20 @@ class SymbolMerger extends AbstractListingMerger {monitor.setProgress(len);}+ private static boolean isDefaultThunk(Symbol s) {+ if (s.getSource() != SourceType.DEFAULT || s.getSymbolType() != SymbolType.FUNCTION) {+ return false;+ }+ Function f = (Function) s.getObject();+ return f.isThunk();+ }+private void processModifiedFunctionNamespace(long id, Symbol mySym, Symbol resultSym) {- SourceType mySource = mySym.getSource();Namespace myNs = // default thunks may lie about their namespace- mySource == SourceType.DEFAULT ? mySym.getProgram().getGlobalNamespace()+ isDefaultThunk(mySym) ? mySym.getProgram().getGlobalNamespace(): mySym.getParentNamespace();- SourceType resultSource = resultSym.getSource();Namespace resultNs = // default thunks may lie about their namespace- resultSource == SourceType.DEFAULT ? resultSym.getProgram().getGlobalNamespace()+ isDefaultThunk(resultSym) ? resultSym.getProgram().getGlobalNamespace(): resultSym.getParentNamespace();try {Namespace desiredNs = resolveNamespace(myPgm, myNs);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/util/FunctionMerge.java b/Ghidra/Features/Base/src/main/java/ghidra/program/util/FunctionMerge.javaindex 7dd6b5e0b6..ca8b10e38c 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/program/util/FunctionMerge.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/util/FunctionMerge.java@@ -40,6 +40,10 @@ public class FunctionMerge {this.toFunctionManager = toProgram.getFunctionManager();}+ static boolean isDefaultThunk(Function func) {+ return func.getSymbol().getSource() == SourceType.DEFAULT && func.isThunk();+ }+Symbol replaceFunctionSymbol(Address originEntryPoint, LongLongHashtable conflictSymbolIDMap,TaskMonitor monitor)throws DuplicateNameException, InvalidInputException, CircularDependencyException {@@ -52,13 +56,22 @@ public class FunctionMerge {String fromName = fromFunction.getName();Symbol fromSymbol = fromFunction.getSymbol();SourceType fromSource = fromSymbol.getSource();- // can't handle reversion to DEFAULT - use existing function if fromSymbol is DEFAULT- if (fromSource == SourceType.DEFAULT) {- return toFunction.getSymbol();+ boolean isFromDefaultThunk = isDefaultThunk(fromFunction);++ String toName = toFunction.getName();+ Symbol toSymbol = toFunction.getSymbol();+ SourceType toSource = toSymbol.getSource();+ boolean isToDefaultThunk = isDefaultThunk(fromFunction);++ if (isFromDefaultThunk && isToDefaultThunk) {+ return toSymbol; // matching default thunk}- Namespace fromNamespace = fromSymbol.getParentNamespace();++ Namespace fromNamespace = // default thunks will lie about their namespace+ isFromDefaultThunk ? fromProgram.getGlobalNamespace()+ : fromSymbol.getParentNamespace();Namespace expectedToNamespace = DiffUtility.getNamespace(fromNamespace, toProgram);- if (expectedToNamespace != null) {+ if (!isFromDefaultThunk && expectedToNamespace != null) {Symbol existingSymbol = toProgram.getSymbolTable().getSymbol(fromName,originEntryPoint, expectedToNamespace);if (existingSymbol != null) {@@ -72,18 +85,14 @@ public class FunctionMerge {return existingSymbol;}}- String toName = toFunction.getName();- Symbol toSymbol = toFunction.getSymbol();- SourceType toSource = toSymbol.getSource();- boolean toDefault = toSource == SourceType.DEFAULT;+Namespace currentToNamespace = // default thunks will lie about their namespace- toDefault ? toFunction.getProgram().getGlobalNamespace()- : toSymbol.getParentNamespace();+ isToDefaultThunk ? toProgram.getGlobalNamespace() : toSymbol.getParentNamespace();Symbol expectedNamespaceSymbol =SimpleDiffUtility.getSymbol(fromNamespace.getSymbol(), toProgram);boolean sameNamespace = currentToNamespace.getSymbol() == expectedNamespaceSymbol;- if (!toDefault && fromName.equals(toName) && sameNamespace) {- return toSymbol; // function symbol name and namespace match.+ if (fromSource == toSource && fromName.equals(toName) && sameNamespace) {+ return toSymbol; // function symbol source, name and namespace match}Namespace desiredToNamespace = currentToNamespace;if (!sameNamespace) {@@ -115,13 +124,22 @@ public class FunctionMerge {String fromName = fromFunction.getName();Symbol fromSymbol = fromFunction.getSymbol();SourceType fromSource = fromSymbol.getSource();- // can't handle reversion to DEFAULT - use existing function if fromSymbol is DEFAULT- if (fromSource == SourceType.DEFAULT) {- return toFunction.getSymbol();+ boolean isFromDefaultThunk = isDefaultThunk(fromFunction);++ String toName = toFunction.getName();+ Symbol toSymbol = toFunction.getSymbol();+ SourceType toSource = toSymbol.getSource();+ boolean isToDefaultThunk = isDefaultThunk(fromFunction);++ if (isFromDefaultThunk && isToDefaultThunk) {+ return toSymbol; // matching default thunk}- Namespace fromNamespace = fromSymbol.getParentNamespace();++ Namespace fromNamespace = // default thunks will lie about their namespace+ isFromDefaultThunk ? fromProgram.getGlobalNamespace()+ : fromSymbol.getParentNamespace();Namespace expectedToNamespace = DiffUtility.getNamespace(fromNamespace, toProgram);- if (expectedToNamespace != null) {+ if (!isFromDefaultThunk && expectedToNamespace != null) {Symbol existingSymbol =toProgram.getSymbolTable().getSymbol(fromName, entryPoint, expectedToNamespace);if (existingSymbol != null) {@@ -135,18 +153,14 @@ public class FunctionMerge {return existingSymbol;}}- String toName = toFunction.getName();- Symbol toSymbol = toFunction.getSymbol();- SourceType toSource = toSymbol.getSource();- boolean toDefault = toSource == SourceType.DEFAULT;+Namespace currentToNamespace = // default thunks will lie about their namespace- toDefault ? toFunction.getProgram().getGlobalNamespace()- : toSymbol.getParentNamespace();+ isToDefaultThunk ? toProgram.getGlobalNamespace() : toSymbol.getParentNamespace();Symbol expectedNamespaceSymbol =SimpleDiffUtility.getSymbol(fromNamespace.getSymbol(), toProgram);boolean sameNamespace = currentToNamespace.getSymbol() == expectedNamespaceSymbol;- if (!toDefault && fromName.equals(toName) && sameNamespace) {- return toSymbol; // function symbol name and namespace match.+ if (fromSource == toSource && fromName.equals(toName) && sameNamespace) {+ return toSymbol; // function symbol source, name and namespace match.}Namespace desiredToNamespace = currentToNamespace;if (!sameNamespace) {diff --git a/Ghidra/Features/Base/src/main/java/ghidra/program/util/SymbolMerge.java b/Ghidra/Features/Base/src/main/java/ghidra/program/util/SymbolMerge.javaindex acfac96636..8f5e44ab1b 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/program/util/SymbolMerge.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/program/util/SymbolMerge.java@@ -459,10 +459,10 @@ class SymbolMerge {Symbol fromSymbol = fromFunc.getSymbol();SourceType fromSource = fromSymbol.getSource();+ boolean isFromDefaultThunk = FunctionMerge.isDefaultThunk(fromFunc);String fromName = fromSymbol.getName();- Namespace fromNamespace =- fromSource == SourceType.DEFAULT ? fromFunc.getProgram().getGlobalNamespace()- : fromSymbol.getParentNamespace();+ Namespace fromNamespace = // default thunks will lie about their namespace+ isFromDefaultThunk ? fromProgram.getGlobalNamespace() : fromSymbol.getParentNamespace();Symbol toSymbol;if (toFunc == null) {@@ -549,29 +549,31 @@ class SymbolMerge {Symbol fromSymbol = fromFunc.getSymbol();String fromName = fromSymbol.getName();boolean fromDefault = fromSymbol.getSource() == SourceType.DEFAULT;- Namespace fromNamespace = // default thunk may lie about its namespace- fromDefault ? fromFunc.getProgram().getGlobalNamespace()- : fromSymbol.getParentNamespace();+ boolean isFromDefaultThunk = FunctionMerge.isDefaultThunk(fromFunc);+ Namespace fromNamespace = // default thunks will lie about their namespace+ isFromDefaultThunk ? fromProgram.getGlobalNamespace() : fromSymbol.getParentNamespace();+Namespace resolveNamespace = resolveNamespace(fromNamespace, conflictSymbolIDMap);if ((toFunc != null) && replacePrimary && !fromDefault) {// Save "to" function name and namespace.String toName = toFunc.getName();SourceType toSource = toFunc.getSymbol().getSource();boolean toDefault = toSource == SourceType.DEFAULT;- Namespace toNamespace = // default thunk may lie about its namespace- toDefault ? toFunc.getProgram().getGlobalNamespace()- : toFunc.getParentNamespace();+ Namespace toNamespace = toFunc.getParentNamespace();// Merging function name into function as primary.replaceFunctionSymbol(fromEntryPoint, toEntryPoint, conflictSymbolIDMap, monitor);if (!toDefault && !toName.equals(fromName)) {// Merge "to" function name and namespace as label.addFunctionAsLabel(toEntryPoint, conflictSymbolIDMap, toSymTab, toSource,- toName,- toNamespace, -1L);+ toName, toNamespace, -1L);}}else if (toFunc != null) {+ if (isFromDefaultThunk && FunctionMerge.isDefaultThunk(toFunc)) {+ return;+ }+if (toFunc.getSymbol().getSource() == SourceType.DEFAULT) {// Default "to" function so replacereplaceFunctionSymbol(fromEntryPoint, toEntryPoint, conflictSymbolIDMap,@@ -583,7 +585,7 @@ class SymbolMerge {fromSymbol.getSource(), fromName, resolveNamespace, fromSymbol.getID());}}- else {+ else if (!isFromDefaultThunk) {// No "to" function or not merging primary.addFunctionAsLabel(toEntryPoint, conflictSymbolIDMap, toSymTab,fromSymbol.getSource(), fromName, resolveNamespace, fromSymbol.getID());