Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-789 fix double symbol and plate comments when RTTI analyzer runs before demangler. Issue caused by moving RTTI analyzer earlier in analysis order.

emteere7ce63e3e30df
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti1BackgroundCmd.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti1BackgroundCmd.java
index 5c490aceaa..c1fe0996ec 100644
--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti1BackgroundCmd.java
+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti1BackgroundCmd.java
@@ -100,36 +100,38 @@ public class CreateRtti1BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
Program program = model.getProgram();
TypeDescriptorModel rtti0Model = model.getRtti0Model();
- monitor.checkCanceled();
+ if (rtti0Model == null) {
+ return true;
+ }
- if (rtti0Model != null) {
+ monitor.checkCanceled();
+
+ String suffix = "";
+ try {
+ suffix = " at " + getPMDAttrList(program);
+ }
+ catch (InvalidDataTypeException e) {
+ // Couldn't get pmd and attributes so leave it off and simply log the error.
+ String message =
+ "Unable to get PMD and attributes for RTTI1 at " + getDataAddress() + ".";
+ handleError(message);
+ }
- String suffix = "";
- try {
- suffix = " at " + getPMDAttrList(program);
- }
- catch (InvalidDataTypeException e) {
- // Couldn't get pmd and attributes so leave it off and simply log the error.
- String message =
- "Unable to get PMD and attributes for RTTI1 at " + getDataAddress() + ".";
- handleError(message);
- }
+ // Label
+ boolean shouldCreateComment = true;
+ if (applyOptions.shouldCreateLabel()) {
+ String rtti1Suffix = RTTI_1_NAME + suffix;
+ rtti1Suffix = SymbolUtilities.replaceInvalidChars(rtti1Suffix, true);
+ shouldCreateComment = RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model,
+ rtti1Suffix);
+ }
- // Plate Comment
+ // Plate Comment
+ if (shouldCreateComment) {
+ // comment created if a label was created, or createLabel option off
EHDataTypeUtilities.createPlateCommentIfNeeded(program,
- RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER, RTTI_1_NAME,
- suffix, getDataAddress(), applyOptions);
-
- monitor.checkCanceled();
-
- // Label
- if (applyOptions.shouldCreateLabel()) {
- String rtti1Suffix = RTTI_1_NAME + suffix;
- rtti1Suffix = SymbolUtilities.replaceInvalidChars(rtti1Suffix, true);
- RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model,
- rtti1Suffix);
- }
-
+ RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER, RTTI_1_NAME,
+ suffix, getDataAddress(), applyOptions);
}
return true;
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti2BackgroundCmd.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti2BackgroundCmd.java
index 60080ff2a7..9658fb4e6b 100644
--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti2BackgroundCmd.java
+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti2BackgroundCmd.java
@@ -112,20 +112,27 @@ public class CreateRtti2BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
Program program = model.getProgram();
TypeDescriptorModel rtti0Model = model.getRtti0Model();
-
- monitor.checkCanceled();
-
- // Plate Comment
- EHDataTypeUtilities.createPlateCommentIfNeeded(program,
- RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
- RTTI_2_NAME, null, getDataAddress(), applyOptions);
-
+
+ if (rtti0Model == null) {
+ return true;
+ }
+
monitor.checkCanceled();
// Label
+ boolean shouldCreateComment = true;
if (applyOptions.shouldCreateLabel()) {
- RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model, RTTI_2_NAME);
+ shouldCreateComment = RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model, RTTI_2_NAME);
+ }
+
+ // Plate Comment
+ if (shouldCreateComment) {
+ // comment created if a label was created, or createLabel option off
+ EHDataTypeUtilities.createPlateCommentIfNeeded(program,
+ RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
+ RTTI_2_NAME, null, getDataAddress(), applyOptions);
}
+
return true;
}
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti3BackgroundCmd.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti3BackgroundCmd.java
index e96739305f..0836d8bb2a 100644
--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti3BackgroundCmd.java
+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti3BackgroundCmd.java
@@ -98,23 +98,27 @@ public class CreateRtti3BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
Program program = model.getProgram();
TypeDescriptorModel rtti0Model = model.getRtti0Model();
- monitor.checkCanceled();
+ if (rtti0Model == null) {
+ return true;
+ }
- if (rtti0Model != null) {
+ monitor.checkCanceled();
+
+ // Label
+ boolean shouldCreateComment = true;
+ if (applyOptions.shouldCreateLabel()) {
+ shouldCreateComment = RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model, RTTI_3_NAME);
+ }
- // Plate Comment
+ // Plate Comment
+ if (shouldCreateComment) {
+ // comment created if a label was created, or createLabel option off
EHDataTypeUtilities.createPlateCommentIfNeeded(program,
- RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
- RTTI_3_NAME, null, getDataAddress(), applyOptions);
-
- monitor.checkCanceled();
-
- // Label
- if (applyOptions.shouldCreateLabel()) {
- RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model, RTTI_3_NAME);
- }
+ RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
+ RTTI_3_NAME, null, getDataAddress(), applyOptions);
}
+
return true;
}
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti4BackgroundCmd.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti4BackgroundCmd.java
index 163ac26d5e..7b0fbfef83 100644
--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti4BackgroundCmd.java
+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateRtti4BackgroundCmd.java
@@ -244,22 +244,25 @@ public class CreateRtti4BackgroundCmd extends AbstractCreateDataBackgroundCmd<Rt
Program program = model.getProgram();
TypeDescriptorModel rtti0Model = model.getRtti0Model();
+ if (rtti0Model == null) {
+ return true;
+ }
+
monitor.checkCanceled();
+
+ // Label
+ boolean shouldCreateComment = true;
+ if (applyOptions.shouldCreateLabel()) {
+ shouldCreateComment = RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model,
+ RTTI_4_NAME);
+ }
- if (rtti0Model != null) {
-
- // Plate Comment
- // Plate Comment
+ // Plate Comment
+ if (shouldCreateComment) {
+ // comment created if a label was created, or createLabel option off
EHDataTypeUtilities.createPlateCommentIfNeeded(program, RttiUtil.CONST_PREFIX +
- RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER, RTTI_4_NAME,
- null, getDataAddress(), applyOptions);
- monitor.checkCanceled();
-
- // Label
- if (applyOptions.shouldCreateLabel()) {
- RttiUtil.createSymbolFromDemangledType(program, getDataAddress(), rtti0Model,
- RTTI_4_NAME);
- }
+ RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER, RTTI_4_NAME,
+ null, getDataAddress(), applyOptions);
}
return true;
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateVfTableBackgroundCmd.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateVfTableBackgroundCmd.java
index 742989fbe6..c02658f763 100644
--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateVfTableBackgroundCmd.java
+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/CreateVfTableBackgroundCmd.java
@@ -169,20 +169,23 @@ public class CreateVfTableBackgroundCmd extends AbstractCreateDataBackgroundCmd<
TypeDescriptorModel rtti0Model = model.getRtti0Model();
- if (rtti0Model != null) {
+ if (rtti0Model == null) {
+ return true;
+ }
+
+ // Label
+ boolean shouldCreateComment = true;
+ if (applyOptions.shouldCreateLabel()) {
+ shouldCreateComment = RttiUtil.createSymbolFromDemangledType(program, vfTableAddress, rtti0Model,
+ VF_TABLE_LABEL);
+ }
- // Plate Comment
+ // Plate Comment
+ if (shouldCreateComment) {
+ // comment created if a label was created, or createLabel option off
EHDataTypeUtilities.createPlateCommentIfNeeded(program, RttiUtil.CONST_PREFIX +
- RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
- VF_TABLE_LABEL, null, vfTableAddress, applyOptions);
-
- monitor.checkCanceled();
-
- // Label
- if (applyOptions.shouldCreateLabel()) {
- RttiUtil.createSymbolFromDemangledType(program, vfTableAddress, rtti0Model,
- VF_TABLE_LABEL);
- }
+ RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
+ VF_TABLE_LABEL, null, vfTableAddress, applyOptions);
}
// Create functions that are referred to by the vf table.
@@ -209,21 +212,26 @@ public class CreateVfTableBackgroundCmd extends AbstractCreateDataBackgroundCmd<
TypeDescriptorModel rtti0Model = model.getRtti0Model();
- if (rtti0Model != null) {
-
- // Plate Comment
- EHDataTypeUtilities.createPlateCommentIfNeeded(
- program, META_LABEL + " pointer for " +
- RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
- VF_TABLE_LABEL, null, metaAddress, applyOptions);
-
- monitor.checkCanceled();
+ if (rtti0Model == null) {
+ return true;
+ }
+
+ monitor.checkCanceled();
- // Label
- if (applyOptions.shouldCreateLabel()) {
- RttiUtil.createSymbolFromDemangledType(program, metaAddress, rtti0Model,
+ // Label
+ boolean shouldCreateComment = true;
+ if (applyOptions.shouldCreateLabel()) {
+ shouldCreateComment = RttiUtil.createSymbolFromDemangledType(program, metaAddress, rtti0Model,
VF_TABLE_LABEL + NAME_SEPARATOR + META_LABEL + "_ptr");
- }
+ }
+
+ // Plate Comment
+ if (shouldCreateComment) {
+ // comment created if a label was created, or createLabel option off
+ EHDataTypeUtilities.createPlateCommentIfNeeded(
+ program, META_LABEL + " pointer for " +
+ RttiUtil.getDescriptorTypeNamespace(rtti0Model) + Namespace.DELIMITER,
+ VF_TABLE_LABEL, null, metaAddress, applyOptions);
}
return true;
diff --git a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/RttiUtil.java b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/RttiUtil.java
index 921960802a..587772cc65 100644
--- a/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/RttiUtil.java
+++ b/Ghidra/Features/MicrosoftCodeAnalyzer/src/main/java/ghidra/app/cmd/data/rtti/RttiUtil.java
@@ -61,11 +61,11 @@ public class RttiUtil {
* @param rttiAddress Address of the RTTI datatype
* @param typeDescriptorModel the model for the type descriptor structure
* @param rttiSuffix suffix name indicating which type of RTTI structure
- * @return the symbol or null.
+ * @return true if a symbol was created, false otherwise
*/
- static Symbol createSymbolFromDemangledType(Program program, Address rttiAddress,
+ static boolean createSymbolFromDemangledType(Program program, Address rttiAddress,
TypeDescriptorModel typeDescriptorModel, String rttiSuffix) {
-
+
rttiSuffix = SymbolUtilities.replaceInvalidChars(rttiSuffix, true);
// Get or create the namespace for this RTTI's type descriptor.
@@ -94,25 +94,32 @@ public class RttiUtil {
// See if the symbol already exists for the RTTI data.
Symbol matchingSymbol = symbolTable.getSymbol(rttiSuffix, rttiAddress, classNamespace);
if (matchingSymbol != null) {
- return matchingSymbol;
+ return false;
}
// Don't create it if a similar symbol already exists at the address of the data.
Symbol[] symbols = symbolTable.getSymbols(rttiAddress);
for (Symbol symbol : symbols) {
String name = symbol.getName();
if (name.contains(rttiSuffix)) {
- return symbol; // Similar symbol already exists.
+ return false; // Similar symbol already exists.
+ }
+ // assume any imported symbol is better than what we would put down
+ // if mangled, it will get demangled later
+ SourceType source = symbol.getSource();
+ if (source == SourceType.IMPORTED) {
+ return false;
}
}
try {
// Didn't find the symbol, so create it.
- return symbolTable.createLabel(rttiAddress, rttiSuffix, classNamespace,
+ symbolTable.createLabel(rttiAddress, rttiSuffix, classNamespace,
SourceType.IMPORTED);
+ return true;
}
catch (InvalidInputException e) {
Msg.error(RttiUtil.class,
"Unable to create label for " + rttiSuffix + " at " + rttiAddress + ".", e);
- return null;
+ return false;
}
}