Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5480 Additional corrections and test fixes to handling of external locations during merge

ghidra16ab0beeeb783
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/ExternalFunctionMerger.java b/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/ExternalFunctionMerger.java
index c23a670c3b..ad4976124f 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/ExternalFunctionMerger.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/merge/listing/ExternalFunctionMerger.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -455,19 +455,19 @@ public class ExternalFunctionMerger extends AbstractFunctionMerger implements Li
throws ProgramConflictException, MemoryAccessException, CancelledException {
if (mergeManager != null) {
- latestResolvedDts = (Map<Long, DataType>) mergeManager.getResolveInformation(
- MergeConstants.RESOLVED_LATEST_DTS);
- myResolvedDts = (Map<Long, DataType>) mergeManager.getResolveInformation(
- MergeConstants.RESOLVED_MY_DTS);
- origResolvedDts = (Map<Long, DataType>) mergeManager.getResolveInformation(
- MergeConstants.RESOLVED_ORIGINAL_DTS);
+ latestResolvedDts = (Map<Long, DataType>) mergeManager
+ .getResolveInformation(MergeConstants.RESOLVED_LATEST_DTS);
+ myResolvedDts = (Map<Long, DataType>) mergeManager
+ .getResolveInformation(MergeConstants.RESOLVED_MY_DTS);
+ origResolvedDts = (Map<Long, DataType>) mergeManager
+ .getResolveInformation(MergeConstants.RESOLVED_ORIGINAL_DTS);
- latestResolvedSymbols = (LongLongHashtable) mergeManager.getResolveInformation(
- MergeConstants.RESOLVED_LATEST_SYMBOLS);
- myResolvedSymbols = (LongLongHashtable) mergeManager.getResolveInformation(
- MergeConstants.RESOLVED_MY_SYMBOLS);
- originalResolvedSymbols = (LongLongHashtable) mergeManager.getResolveInformation(
- MergeConstants.RESOLVED_ORIGINAL_SYMBOLS);
+ latestResolvedSymbols = (LongLongHashtable) mergeManager
+ .getResolveInformation(MergeConstants.RESOLVED_LATEST_SYMBOLS);
+ myResolvedSymbols = (LongLongHashtable) mergeManager
+ .getResolveInformation(MergeConstants.RESOLVED_MY_SYMBOLS);
+ originalResolvedSymbols = (LongLongHashtable) mergeManager
+ .getResolveInformation(MergeConstants.RESOLVED_ORIGINAL_SYMBOLS);
}
initializeAutoMerge("Auto-merging External Labels and Functions and determining conflicts.",
@@ -546,6 +546,9 @@ public class ExternalFunctionMerger extends AbstractFunctionMerger implements Li
}
private void fixupLatestChangeIDsMarkedAsRemovesAndAdds(TaskMonitor monitor) {
+ // NOTE: Things get rather complicated when an external location transitions to
+ // a function since it is conveyed as a symbol removal and an add even though it
+ // is really the same external location.
@SuppressWarnings("unchecked")
HashSet<Long> latestRemovedIDs = (HashSet<Long>) latestRemovedOriginalIDs.clone();
for (Long id : latestRemovedIDs) {
@@ -874,6 +877,8 @@ public class ExternalFunctionMerger extends AbstractFunctionMerger implements Li
SymbolType symbolType2 = symbol2.getSymbolType();
Function function2 = externalLocation2.getFunction();
+ // TODO: Does not consider original imported name
+
if (!equivalentNamespaces(namespace1, namespace2)) {
conflicts |= EXTERNAL_NAMESPACE;
}
@@ -1818,8 +1823,8 @@ public class ExternalFunctionMerger extends AbstractFunctionMerger implements Li
if (originalImportedName != null) {
try {
resultExternalLocation.getSymbol()
- .setNameAndNamespace(externalLocation.getLabel(),
- resolvedNamespace, externalLocation.getSource());
+ .setNameAndNamespace(externalLocation.getLabel(), resolvedNamespace,
+ externalLocation.getSource());
}
catch (CircularDependencyException e) {
throw new AssertException(e);
@@ -2210,10 +2215,8 @@ public class ExternalFunctionMerger extends AbstractFunctionMerger implements Li
mergeParamInfo(functions, paramInfoConflicts, parameterInfoChoice, monitor);
}
else if (askUser && mergeManager != null) {
- Iterator<ParamInfoConflict> iter = paramInfoConflicts.iterator();
- while (iter.hasNext()) {
+ for (ParamInfoConflict pc : paramInfoConflicts) {
monitor.checkCancelled();
- ParamInfoConflict pc = iter.next();
boolean useForAll = (parameterInfoChoice != ASK_USER);
if (useForAll) {
mergeParamInfo(functions, pc, parameterInfoChoice, monitor);
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/LabelFieldFactory.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/LabelFieldFactory.java
index 8b54022f1d..59e99b70a6 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/LabelFieldFactory.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/field/LabelFieldFactory.java
@@ -26,6 +26,7 @@ import docking.widgets.fieldpanel.support.FieldLocation;
import generic.theme.GIcon;
import ghidra.app.util.*;
import ghidra.app.util.viewer.format.FieldFormatModel;
+import ghidra.app.util.viewer.options.OptionsGui;
import ghidra.app.util.viewer.proxy.ProxyObj;
import ghidra.framework.options.*;
import ghidra.program.model.address.Address;
@@ -194,6 +195,32 @@ public class LabelFieldFactory extends FieldFactory {
}
}
+ if (currAddr.isExternalAddress() && length == 1) {
+ // Show extenal address and original imported name (not supported by field location)
+ ExternalLocation extLoc = prog.getExternalManager().getExternalLocation(symbols[0]);
+ if (extLoc != null) {
+ StringBuilder externalLocationDetails = new StringBuilder();
+ Address addr = extLoc.getAddress();
+ if (addr != null) {
+ externalLocationDetails.append(addr.toString());
+ }
+ String origImportedName = extLoc.getOriginalImportedName();
+ if (origImportedName != null) {
+ if (!externalLocationDetails.isEmpty()) {
+ externalLocationDetails.append(": ");
+ }
+ externalLocationDetails.append(origImportedName);
+ }
+ if (!externalLocationDetails.isEmpty()) {
+ AttributedString as =
+ new AttributedString(EMPTY_ICON, externalLocationDetails.toString(),
+ OptionsGui.LABELS_NON_PRIMARY.getColor(),
+ getMetrics(OptionsGui.LABELS_NON_PRIMARY.getStyle()), false, null);
+ elements.add(new TextFieldElement(as, elements.size(), 0));
+ }
+ }
+ }
+
for (Symbol symbol : symbols) {
if (func != null && symbol.isPrimary() && !displayFunctionLabel) {
continue;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/multilisting/MultiListingLayoutModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/multilisting/MultiListingLayoutModel.java
index 6b80930c78..feb23ad798 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/multilisting/MultiListingLayoutModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/viewer/multilisting/MultiListingLayoutModel.java
@@ -139,7 +139,7 @@ public class MultiListingLayoutModel implements ListingModelListener, FormatMode
}
class AlignedModel implements ListingModel {
- private int modelID;
+ private final int modelID;
AlignedModel(int modelID) {
this.modelID = modelID;
@@ -175,14 +175,18 @@ public class MultiListingLayoutModel implements ListingModelListener, FormatMode
* <code>address</code> is null
*/
private Address getNextAddress(Address address, boolean after) {
+
+ // Assume only a single external location is ever displayed within a panel
+ if (address.isExternalAddress()) {
+ return null;
+ }
+
Address nextAddress = null; // Next address for this model
Program program = getProgram();
Program primaryProgram = models[0].getProgram();
Address primaryModelAddress = (program == primaryProgram) ? address
: SimpleDiffUtility.getCompatibleAddress(program, address, primaryProgram);
- // If address is an external from the other model, then we may not be able to get
- // an equivalent address in the primary model (i.e. primaryModelAddress may be null)
if (primaryModelAddress == null) {
return null;
}
@@ -217,11 +221,24 @@ public class MultiListingLayoutModel implements ListingModelListener, FormatMode
return nextAddress;
}
+ private Address lastAddress;
+ private Address lastCachedPrimaryModelAddress;
+
@Override
public Layout getLayout(Address thisModelAddress, boolean isGapAddress) {
- Address primaryModelAddress = (modelID == 0) ? thisModelAddress
- : SimpleDiffUtility.getCompatibleAddress(getProgram(), thisModelAddress,
- models[0].getProgram());
+
+ Address primaryModelAddress;
+ if (thisModelAddress.equals(lastAddress)) {
+ primaryModelAddress = lastCachedPrimaryModelAddress;
+ }
+ else {
+ primaryModelAddress = (modelID == 0) ? thisModelAddress
+ : SimpleDiffUtility.getCompatibleAddress(getProgram(), thisModelAddress,
+ models[0].getProgram());
+ lastAddress = thisModelAddress;
+ lastCachedPrimaryModelAddress = primaryModelAddress;
+ }
+
MultiLayout ml = getMultiLayout(primaryModelAddress, isGapAddress);
if (ml != null) {
return ml.getLayout(modelID);
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMergeManagerTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMergeManagerTest.java
index de060b0f8b..2f5b9d80cf 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMergeManagerTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMergeManagerTest.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -1641,11 +1641,11 @@ public class ExternalFunctionMergeManagerTest extends AbstractExternalMergerTest
@Override
public void modifyLatest(ProgramDB program) {
try {
- Function bettyFunction = createExternalFunction(program,
- new String[] { "user32.dll", "BETTY" }, addr(program, "1002239"));
+ createExternalFunction(program, new String[] { "user32.dll", "BETTY" },
+ addr(program, "1002239"));
- Function barneyFunction = createExternalFunction(program,
- new String[] { "user32.dll", "BARNEY" }, addr(program, "77db1020"));
+ createExternalFunction(program, new String[] { "user32.dll", "BARNEY" },
+ addr(program, "77db1020"));
}
catch (Exception e) {
Assert.fail(e.getMessage());
@@ -1655,11 +1655,11 @@ public class ExternalFunctionMergeManagerTest extends AbstractExternalMergerTest
@Override
public void modifyPrivate(ProgramDB program) {
try {
- Function bettyFunction = createExternalFunction(program,
- new String[] { "user32.dll", "BETTY" }, addr(program, "77db1020"));
+ createExternalFunction(program, new String[] { "user32.dll", "BETTY" },
+ addr(program, "77db1020"));
- Function barneyFunction = createExternalFunction(program,
- new String[] { "user32.dll", "BARNEY" }, addr(program, "1002239"));
+ createExternalFunction(program, new String[] { "user32.dll", "BARNEY" },
+ addr(program, "1002239"));
}
catch (Exception e) {
Assert.fail(e.getMessage());
@@ -1668,8 +1668,8 @@ public class ExternalFunctionMergeManagerTest extends AbstractExternalMergerTest
});
executeMerge(ASK_USER);
- chooseButtonAndApply("Resolve External Add Conflict", LATEST_BUTTON);
- chooseButtonAndApply("Resolve External Add Conflict", MY_BUTTON);
+ chooseButtonAndApply("Resolve External Add Conflict", LATEST_BUTTON); // BETTY has address conflict
+ chooseButtonAndApply("Resolve External Add Conflict", KEEP_BOTH_BUTTON); // BARNEY has address conflict
waitForMergeCompletion();
@@ -1679,6 +1679,10 @@ public class ExternalFunctionMergeManagerTest extends AbstractExternalMergerTest
assertEquals(2, externalLocations.size());
assertHasDifferentAddresses(externalLocations, "1002239", "77db1020");
+ externalLocations = externalManager.getExternalLocations("user32.dll", "BETTY");
+ assertEquals(1, externalLocations.size());
+ assertEquals("01002239", externalLocations.get(0).getAddress().toString());
+
}
@Test
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMerger1Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMerger1Test.java
index 662e4d4cdd..61b521ca1e 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMerger1Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/ExternalFunctionMerger1Test.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -222,10 +222,8 @@ public class ExternalFunctionMerger1Test extends AbstractExternalMergerTest {
});
executeMerge(ASK_USER);
-// chooseButtonAndApply("Resolve External Add Conflict",
-// ExternalFunctionMerger.MERGE_BOTH_BUTTON_NAME);
-// chooseVariousOptionsForConflictType("Resolve External Detail Conflict",
-// new int[] { INFO_ROW, KEEP_MY });// Name Conflict
+ chooseButtonAndApply("Resolve External Add Conflict",
+ ExternalFunctionMerger.KEEP_BOTH_BUTTON_NAME);
waitForMergeCompletion();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/FunctionMergerThunkTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/FunctionMergerThunkTest.java
index dea9344e3e..f35d98eecc 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/FunctionMergerThunkTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/merge/listing/FunctionMergerThunkTest.java
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
+ *
* http://www.apache.org/licenses/LICENSE-2.0
- *
+ *
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -113,8 +113,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
AddressSetView differences = programDiff.getDifferences(TaskMonitor.DUMMY);
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
}
@@ -170,8 +169,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
AddressSetView differences = programDiff.getDifferences(TaskMonitor.DUMMY);
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
}
@@ -288,8 +286,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -356,8 +353,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -430,8 +426,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -507,8 +502,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -585,8 +579,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -663,8 +656,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -1027,7 +1019,8 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
});
executeMerge(ASK_USER);
- chooseButtonAndApply("Resolve External Add Conflict", ListingMergeConstants.LATEST_BUTTON_NAME);
+ chooseButtonAndApply("Resolve External Add Conflict",
+ ListingMergeConstants.LATEST_BUTTON_NAME);
waitForMergeCompletion();
Function thunkFunction = getFunction(resultProgram, THUNK_A_ENTRY);
@@ -1095,7 +1088,8 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
});
executeMerge(ASK_USER);
- chooseButtonAndApply("Resolve External Add Conflict", ListingMergeConstants.CHECKED_OUT_BUTTON_NAME);
+ chooseButtonAndApply("Resolve External Add Conflict",
+ ListingMergeConstants.CHECKED_OUT_BUTTON_NAME);
waitForMergeCompletion();
Function thunkFunction = getFunction(resultProgram, THUNK_A_ENTRY);
@@ -1162,7 +1156,6 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
});
executeMerge(ASK_USER);
- chooseRadioButton("Resolve External Add Conflict", KEEP_BOTH_BUTTON); // keep both externals causes thunk conflict
chooseRadioButton("Resolve Thunk Function Conflict", LATEST_BUTTON);
waitForMergeCompletion();
@@ -1230,7 +1223,6 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
});
executeMerge(ASK_USER);
- chooseRadioButton("Resolve External Add Conflict", KEEP_BOTH_BUTTON); // keep both externals causes thunk conflict
chooseRadioButton("Resolve Thunk Function Conflict", MY_BUTTON);
waitForMergeCompletion();
@@ -1303,8 +1295,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -1380,8 +1371,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -1462,8 +1452,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
@@ -1547,8 +1536,7 @@ public class FunctionMergerThunkTest extends AbstractExternalMergerTest {
assertEquals(expectedDifferences, differences);
ProgramDiffFilter filter = new ProgramDiffFilter(ProgramDiffFilter.FUNCTION_DIFFS);
- AddressSetView functionDifferences =
- programDiff.getDifferences(filter, TaskMonitor.DUMMY);
+ AddressSetView functionDifferences = programDiff.getDifferences(filter, TaskMonitor.DUMMY);
assertEquals(expectedDifferences, functionDifferences);
executeMerge(ASK_USER);
diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/SimpleDiffUtility.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/SimpleDiffUtility.java
index 94487815bb..319584a6cc 100644
--- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/SimpleDiffUtility.java
+++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/util/SimpleDiffUtility.java
@@ -432,12 +432,19 @@ public class SimpleDiffUtility {
private static class ExternalReferenceCount implements Comparable<ExternalReferenceCount> {
+ static final int ADDRESS_RANK = 3;
+ static final int NAME_RANK = 2;
+ static final int NAMESPACE_RANK = 1;
+ static final int MANGLED_NAME_RANK = NAME_RANK + NAMESPACE_RANK;
+
final ExternalLocation extLoc;
+ final ExternalMatchType matchType;
int refCount = 1;
int rank;
- ExternalReferenceCount(ExternalLocation extLoc) {
+ ExternalReferenceCount(ExternalLocation extLoc, ExternalMatchType matchType) {
this.extLoc = extLoc;
+ this.matchType = matchType;
}
ExternalLocation getExternalLocation() {
@@ -478,26 +485,111 @@ public class SimpleDiffUtility {
return getSymbol().getName(true).compareTo(other.getSymbol().getName(true));
}
- public void setRelativeRank(Address targetAddr, String targetNamespace, String targetName) {
+ /**
+ * Generate relative rank when a non-default name match occurs.
+ * Method should not be invoked when either location symbol has a default source type.
+ * @param targetAddr
+ * @param targetNamespace
+ * @param targetName
+ * @param targetOrigImportedName
+ */
+ void setRelativeRank(Address targetAddr, String targetNamespace, String targetName,
+ String targetOrigImportedName) {
rank = 0;
+
+ if (matchType == ExternalMatchType.ADDRESS) {
+ rank = ADDRESS_RANK;
+ return;
+ }
+
if (targetAddr != null) {
Address myAddr = extLoc.getAddress();
- if (myAddr != null && targetAddr.equals(extLoc.getAddress())) {
- rank += 3; // address match
+ if (myAddr != null && targetAddr.equals(myAddr)) {
+ rank += ADDRESS_RANK; // address match
}
else if (myAddr != null) {
// If memory addresses both specified and differ - reduce rank
- rank -= 3;
+ rank -= ADDRESS_RANK;
}
}
- if (targetName != null && targetName.equals(getSymbolName())) {
- rank += 2; // non-default name match
- if (targetNamespace != null && targetNamespace.equals(getFullNamespaceName())) {
- rank += 1; // non-default namespace match improves name match
- }
+
+ if (matchType == ExternalMatchType.MANGLED_NAME) {
+ rank += MANGLED_NAME_RANK;
+ return;
+ }
+
+ if (matchType != ExternalMatchType.NAME) {
+ return;
+ }
+
+ // Impose mangled name mismatch penalty
+ String myOrigImportedName = extLoc.getOriginalImportedName();
+ if (targetOrigImportedName != null && myOrigImportedName != null) {
+ rank -= MANGLED_NAME_RANK;
+ return;
}
+
+ // assume NAME match
+ rank += NAME_RANK;
+
+ if (targetNamespace != null && targetNamespace.equals(getFullNamespaceName())) {
+ rank += NAMESPACE_RANK; // non-default namespace match improves name match
+ }
+ }
+
+ }
+
+ private enum ExternalMatchType {
+ NONE, NAME, MANGLED_NAME, ADDRESS;
+ }
+
+ private static ExternalMatchType testExternalMatch(ExternalLocation extLoc1,
+ ExternalLocation extLoc2) {
+ ExternalMatchType matchType = testExternalNameMatch(extLoc1, extLoc2);
+ if (matchType == ExternalMatchType.NONE) {
+ return hasExternalAddressMatch(extLoc1, extLoc2) ? ExternalMatchType.ADDRESS
+ : ExternalMatchType.NONE;
}
+ return matchType;
+ }
+ private static boolean hasExternalAddressMatch(ExternalLocation extLoc1,
+ ExternalLocation extLoc2) {
+ Address addr1 = extLoc1.getAddress();
+ return addr1 != null && addr1.equals(extLoc2.getAddress());
+ }
+
+ private static ExternalMatchType testExternalNameMatch(ExternalLocation extLoc1,
+ ExternalLocation extLoc2) {
+ boolean isDefaul1 = extLoc1.getSymbol().getSource() == SourceType.DEFAULT;
+ boolean isDefaul2 = extLoc2.getSymbol().getSource() == SourceType.DEFAULT;
+ if (isDefaul1 || isDefaul2) {
+ return ExternalMatchType.NONE;
+ }
+ if (!extLoc1.getLibraryName().equals(extLoc2.getLibraryName())) {
+ return ExternalMatchType.NONE; // assume this prevails over Namespace
+ }
+
+ String name1 = extLoc1.getLabel();
+ String name2 = extLoc2.getLabel();
+ String origName1 = extLoc1.getOriginalImportedName();
+ String origName2 = extLoc2.getOriginalImportedName();
+ if (origName1 != null) {
+ if (origName2 != null) {
+ // assume mangled names if both known must match
+ return origName1.equals(origName2) ? ExternalMatchType.MANGLED_NAME
+ : ExternalMatchType.NONE;
+ }
+ // mangled name must be in root namespace of library
+ if (extLoc2.getSymbol().getParentNamespace().isLibrary() && origName1.equals(name2)) {
+ return ExternalMatchType.MANGLED_NAME;
+ }
+ }
+ else if (origName2 != null && extLoc1.getSymbol().getParentNamespace().isLibrary() &&
+ origName2.equals(name1)) {
+ return ExternalMatchType.MANGLED_NAME;
+ }
+ return name1.equals(name2) ? ExternalMatchType.NAME : ExternalMatchType.NONE;
}
/**
@@ -527,6 +619,7 @@ public class SimpleDiffUtility {
ExternalLocation extLoc = extMgr.getExternalLocation(symbol);
String targetName = symbol.getSource() != SourceType.DEFAULT ? symbol.getName() : null;
+ String targetOrigImportedName = extLoc.getOriginalImportedName();
String targetNamespace = symbol.getParentNamespace().getName(true);
if (targetNamespace.startsWith(Library.UNKNOWN)) {
targetNamespace = null;
@@ -557,8 +650,12 @@ public class SimpleDiffUtility {
continue;
}
ExternalLocation otherExtLoc = otherExtMgr.getExternalLocation(otherSym);
- refMatch = new ExternalReferenceCount(otherExtLoc);
- refMatch.setRelativeRank(targetAddr, targetNamespace, targetName);
+ ExternalMatchType matchType = testExternalMatch(otherExtLoc, extLoc);
+ refMatch = new ExternalReferenceCount(otherExtLoc, matchType);
+ if (matchType != ExternalMatchType.NONE) {
+ refMatch.setRelativeRank(targetAddr, targetNamespace, targetName,
+ targetOrigImportedName);
+ }
matchesMap.put(otherExtAddr, refMatch);
}
else {
@@ -593,8 +690,12 @@ public class SimpleDiffUtility {
}
ExternalLocation otherExtLoc =
otherExtMgr.getExternalLocation(otherThunkedFunc.getSymbol());
- refMatch = new ExternalReferenceCount(otherExtLoc);
- refMatch.setRelativeRank(targetAddr, targetNamespace, targetName);
+ ExternalMatchType matchType = testExternalMatch(otherExtLoc, extLoc);
+ refMatch = new ExternalReferenceCount(otherExtLoc, matchType);
+ if (matchType != ExternalMatchType.NONE) {
+ refMatch.setRelativeRank(targetAddr, targetNamespace, targetName,
+ targetOrigImportedName);
+ }
matchesMap.put(otherThunkedFunc.getEntryPoint(), refMatch);
}
else {
@@ -612,22 +713,16 @@ public class SimpleDiffUtility {
!otherRestrictedSymbolIds.contains(otherSym.getID())) {
continue;
}
- boolean addIt = false;
- if (targetAddr != null) {
- ExternalLocation otherExtLoc = otherExtMgr.getExternalLocation(otherSym);
- Address otherAddr = otherExtLoc.getAddress();
- if (otherAddr != null && targetAddr.equals(otherAddr) &&
- originalNamesDontConflict(extLoc, otherExtLoc)) {
- addIt = true;
+ ExternalLocation otherExtLoc = otherExtMgr.getExternalLocation(otherSym);
+ if (otherExtLoc != null) {
+ ExternalMatchType matchType = testExternalMatch(otherExtLoc, extLoc);
+ if (matchType == ExternalMatchType.NONE) {
+ continue;
}
- }
- if (!addIt && targetName != null && targetName.equals(otherSym.getName())) {
- addIt = true;
- }
- if (addIt) {
ExternalReferenceCount refMatch =
- new ExternalReferenceCount(otherExtMgr.getExternalLocation(otherSym));
- refMatch.setRelativeRank(targetAddr, targetNamespace, targetName);
+ new ExternalReferenceCount(otherExtLoc, matchType);
+ refMatch.setRelativeRank(targetAddr, targetNamespace, targetName,
+ targetOrigImportedName);
matchesMap.put(otherSym.getAddress(), refMatch);
}
}
@@ -648,17 +743,6 @@ public class SimpleDiffUtility {
return matches[0].rank > 0 ? matches[0].getSymbol() : null;
}
- private static boolean originalNamesDontConflict(ExternalLocation extLoc,
- ExternalLocation otherExtLoc) {
- if (extLoc.getOriginalImportedName() == null) {
- return true;
- }
- if (otherExtLoc.getOriginalImportedName() == null) {
- return true;
- }
- return extLoc.getOriginalImportedName().equals(otherExtLoc.getOriginalImportedName());
- }
-
/**
* Given an external location for a specified program, get the corresponding external location,
* which has the same name and path, from the specified otherProgram.<br>