Skip to content

Ghidra is a software reverse engineering (SRE) framework

Merge remote-tracking branch 'origin/GP-1315_add_option_to_remove_replaced_class_structures--SQUASHED' into Ghidra_10.1 (Closes #3443)

ghidra1f22e05af3f25
diff --cc Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RecoveredClassUtils.java
index da19b6b4b0,8672184d42..711bae1f52
--- a/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RecoveredClassUtils.java
+++ b/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RecoveredClassUtils.java
@@@ -3535,104 -3505,59 +3535,159 @@@ public class RecoveredClassUtils
}
}
+ /**
++<<<<<<< HEAD
+ * Method to replace the program's current class structure, only if an empty placeholder structure,
+ * with the one generated by this script
+ * @param function a class method with current class structure applied
+ * @param className the given class name
+ * @param newClassStructure the new structure to replace the old with
+ * @throws DataTypeDependencyException if there is a data dependency exception when replacing
+ * @throws CancelledException if cancelled
+ */
+ public void replaceClassStructure(Function function, String className,
+ Structure newClassStructure) throws DataTypeDependencyException, CancelledException {
+
+ Parameter thisParam = function.getParameter(0);
+ if (thisParam == null) {
+ return;
+ }
+
+ DataType dataType = thisParam.getDataType();
+ if (dataType instanceof Pointer) {
+ Pointer ptr = (Pointer) dataType;
+ DataType baseDataType = ptr.getDataType();
+ if (!baseDataType.equals(newClassStructure) &&
+ baseDataType.getName().equals(className)) {
+
+ // check if fid demangler or pdb - don't replace user ones
+ if (!isReplaceableType(function.getEntryPoint(), baseDataType)) {
+ return;
+ }
+ // create copy of existing one
+ DataType baseDataTypeCopy = baseDataType.copy(dataTypeManager);
+
+ renameDataType(baseDataTypeCopy, baseDataType.getName() + "_REPLACED");
+
+ // replace the other with the new one
+ dataTypeManager.replaceDataType(baseDataType, newClassStructure, false);
+
+// // remove original folder if it is empty after the replace
+ // in future if decide to just remove the other ones, then do the following
+// CategoryPath originalPath = baseDataType.getCategoryPath();
+// Category category = dataTypeManager.getCategory(originalPath);
+// Category parentCategory = category.getParent();
+// if (parentCategory != null) {
+// parentCategory.removeEmptyCategory(category.getName(), monitor);
+// }
+
+ }
+ }
+ }
+
+ private void renameDataType(DataType dataType, String name) throws CancelledException {
+
+ boolean renamed = false;
+ int oneup = 2;
+ while (!renamed) {
+ monitor.checkCanceled();
+ try {
+ dataType.setName(name);
+ dataTypeManager.resolve(dataType, DataTypeConflictHandler.DEFAULT_HANDLER);
+ renamed = true;
+ }
+ catch (InvalidNameException | DuplicateNameException e) {
+ name = name + oneup++;
+ renamed = false;
+ }
+ }
+ }
+
+ private boolean isReplaceableType(Address address, DataType dataType) {
+
+ // return false if it isn't even a structure
+ if (!(dataType instanceof Structure)) {
+ return false;
+ }
+ String categoryPath = dataType.getPathName();
+ if (categoryPath.startsWith("/Demangler")) {
+ return true;
+ }
+
+ if (categoryPath.contains(".pdb")) {
+ return true;
+ }
+
+ //TODO: decide whether to replace dwarf or not
+
+ // test to see if the data type is an empty structure with "PlaceHolder Class Structure" in
+ // the description
+ Structure structure = (Structure) dataType;
+ if (structure.isNotYetDefined() &&
+ structure.getDescription().equals("PlaceHolder Class Structure")) {
+ return true;
+ }
+
+ if (program.getBookmarkManager().getBookmark(address, BookmarkType.ANALYSIS,
+ "Function ID Analyzer") != null) {
+ return true;
+ }
+ return false;
++ }
++
+ /**
+ * Method to remove existing class structures from the data type manager that were replaced by
+ * newly created class structures and that have the "_REPLACED" suffix on them
+ * @param recoveredClasses list of given recovered classes
+ * @param removeNonEmpty if true, remove not only the empty replaced class structures but
+ * also the non-empty ones.
+ * @throws CancelledException if cancelled
+ */
+ public void removeReplacedClassStructures(List<RecoveredClass> recoveredClasses,
+ boolean removeNonEmpty) throws CancelledException {
+
+ if (recoveredClasses.isEmpty()) {
+ return;
+ }
+
+ for (RecoveredClass recoveredClass : recoveredClasses) {
+ monitor.checkCanceled();
+ // first get the new class structure and verify it exists - don't remove others if
+ // new one doesn't exist
+ DataType classStructureDataType = dataTypeManager.getDataType(
+ recoveredClass.getClassPath(), recoveredClass.getName());
+ if (classStructureDataType == null) {
+ continue;
+ }
+ // then find all class structures with name "<className>_REPLACED"
+ List<DataType> replacedClassDataTypes = new ArrayList<DataType>();
+ dataTypeManager.findDataTypes(recoveredClass.getName() + "_REPLACED",
+ replacedClassDataTypes);
+
+ if (replacedClassDataTypes.isEmpty()) {
+ continue;
+ }
+
+ for (DataType replacedClassDataType : replacedClassDataTypes) {
+ monitor.checkCanceled();
+
+ if (!(replacedClassDataType instanceof Structure)) {
+ continue;
+ }
+
+ if (removeNonEmpty) {
+ dataTypeManager.remove(replacedClassDataType, monitor);
+ }
+ else {
+ Structure replacedStructure = (Structure) replacedClassDataType;
+ if (replacedStructure.isNotYetDefined()) {
+ dataTypeManager.remove(replacedClassDataType, monitor);
+ }
+ }
+ }
+
+ }
}
/**