Skip to content

Ghidra is a software reverse engineering (SRE) framework

Merge remote-tracking branch 'origin/GP-1519_ghirdra007_RecoverClass_replaceGetComponentA--SQUASHED' into Ghidra_10.1

ghidra1659892a81039
diff --cc Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/ExtendedFlatProgramAPI.java
index 9f2e10ff17,61896eacaf..c2791db137
--- a/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/ExtendedFlatProgramAPI.java
+++ b/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/ExtendedFlatProgramAPI.java
@@@ -143,10 -137,10 +137,12 @@@ public class ExtendedFlatProgramAPI ext
clearListing(address);
return false;
}
++ catch (CancelledException e) {
++ throw e;
++ }
catch (Exception e) {
return false;
}
--
}
return false;
@@@ -511,6 -505,6 +507,7 @@@
return false;
}
catch (CancelledException e) {
++ // FIXME: this should not be caught by this method and should propogate
return false;
}
@@@ -526,6 -520,6 +523,8 @@@
public AddressSet getSubroutineAddresses(Program program, Address address)
throws CancelledException {
++ // FIXME: Should not be passing program arg
++
// Create a new address set to hold the entire selection.
AddressSet subroutineAddresses = new AddressSet();
@@@ -556,6 -550,6 +555,7 @@@
return;
}
++ // FIXME: if you pass a Function arg you should use its program not currentProgram
ReturnParameterImpl returnType =
new ReturnParameterImpl(function.getSignature().getReturnType(), currentProgram);
@@@ -654,7 -648,7 +654,8 @@@
List<Symbol> symbolList = new ArrayList<Symbol>();
- SymbolIterator symbols = program.getSymbolTable().getSymbols(namespace);
++ // FIXME: if you are going to pass namespace arg you should use its program not currentProgram
+ SymbolIterator symbols = currentProgram.getSymbolTable().getSymbols(namespace);
while (symbols.hasNext()) {
monitor.checkCanceled();
@@@ -764,8 -758,8 +765,9 @@@
List<Address> referenceAddresses = new ArrayList<Address>();
++ // FIXME: if you pass a function arg you should use its program, not currentProgram
ReferenceIterator referencesToFunctionBIterator =
- program.getReferenceManager().getReferencesTo(bFunction.getEntryPoint());
+ currentProgram.getReferenceManager().getReferencesTo(bFunction.getEntryPoint());
while (referencesToFunctionBIterator.hasNext()) {
@@@ -1031,7 -1025,7 +1033,8 @@@
*/
public boolean hasSymbolsInNamespace(Namespace namespace) {
- SymbolIterator namespaceSymbols = program.getSymbolTable().getSymbols(namespace);
++ // FIXME: if you are going to use a Namespace arg you should its program not currentProgram
+ SymbolIterator namespaceSymbols = currentProgram.getSymbolTable().getSymbols(namespace);
if (namespaceSymbols.hasNext()) {
return true;
@@@ -1045,10 -1039,8 +1048,9 @@@
* @param parent parent CategoryPath
* @param categoryName name of the new category in the parent path
* @return CategoryPath for new categoryName
+ * @throws CancelledException if cancelled
*/
- public CategoryPath createDataTypeCategoryPath(CategoryPath parent, String categoryName)
- throws CancelledException {
- public CategoryPath createDataTypeCategoryPath(CategoryPath parent, String categoryName) {
++ public CategoryPath createDataTypeCategoryPath(CategoryPath parent, String categoryName) throws CancelledException {
CategoryPath dataTypePath;
@@@ -1072,8 -1064,12 +1074,7 @@@
int index = 0;
String newCategoryName = new String();
while (index < categoryName.length()) {
-
- try {
- monitor.checkCanceled();
- }
- catch (CancelledException e) {
- return null;
- }
+ monitor.checkCanceled();
if (categoryName.substring(index).startsWith("::") && !insideBrackets) {
newCategoryName = newCategoryName.concat("/");
diff --cc Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RTTIGccClassRecoverer.java
index 445a67e0a6,cd624a8213..b9405909ab
--- a/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RTTIGccClassRecoverer.java
+++ b/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RTTIGccClassRecoverer.java
@@@ -1314,9 -1407,9 +1314,9 @@@ public class RTTIGccClassRecoverer exte
// create a pointer and check to see if it is a reference to a valid memory location
try {
api.createData(address, pointer);
- Address referencedAddress = extraUtils.getSingleReferencedAddress(address);
+ Address referencedAddress = extendedFlatAPI.getSingleReferencedAddress(address);
- // if it isn't valid, clear what we just created and increment to offset so
+ // if it isn't a valid pointer, clear what we just created and increment to offset so
// the next can be checked
if (referencedAddress == null || !programAddressSet.contains(referencedAddress)) {
api.clearListing(address);
diff --cc Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RTTIWindowsClassRecoverer.java
index 86e25347e8,669e207c28..7644501cff
--- a/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RTTIWindowsClassRecoverer.java
+++ b/Ghidra/Features/Decompiler/ghidra_scripts/classrecovery/RTTIWindowsClassRecoverer.java
@@@ -115,81 -129,90 +115,81 @@@ public class RTTIWindowsClassRecoverer
}
@Override
- public List<RecoveredClass> createRecoveredClasses() {
+ public List<RecoveredClass> createRecoveredClasses() throws Exception {
List<Symbol> vftableSymbols;
- try {
- vftableSymbols = getListOfVftableSymbols();
- List<RecoveredClass> recoveredClasses =
- recoverClassesFromClassHierarchyDescriptors(vftableSymbols);
+ vftableSymbols = getListOfVftableSymbols();
- determineVftableOffsetsfromRTTI(recoveredClasses);
+ List<RecoveredClass> recoveredClasses =
+ recoverClassesFromClassHierarchyDescriptors(vftableSymbols);
- // If no new classes have been recovered, no need to continue. Return out of script.
- if (recoveredClasses.isEmpty()) {
+ determineVftableOffsetsfromRTTI(recoveredClasses);
- return recoveredClasses;
- }
+ // If no new classes have been recovered, no need to continue. Return out of script.
+ if (recoveredClasses.isEmpty()) {
- createCalledFunctionMap(recoveredClasses);
+ return recoveredClasses;
+ }
- // figure out class hierarchies using either RTTI or vftable refs
+ createCalledFunctionMap(recoveredClasses);
- monitor.setMessage("Assigning class inheritance and hierarchies");
- assignClassInheritanceAndHierarchies(recoveredClasses);
+ // figure out class hierarchies using either RTTI or vftable refs
- // Since PDB has applied so much information, use it to figure out the class member data 4
- // items (if it has them) and the constructors and destructors.
- if (isPDBLoaded) {
- monitor.setMessage(
- "Attempting to use pdb to assign class hierarchies and extend known pdb data " +
- "type information ...");
+ monitor.setMessage("Assigning class inheritance and hierarchies");
+ assignClassInheritanceAndHierarchies(recoveredClasses);
- retrieveExistingClassStructures(recoveredClasses);
+ // Since PDB has applied so much information, use it to figure out the class member data 4
+ // items (if it has them) and the constructors and destructors.
+ if (isPDBLoaded) {
+ monitor.setMessage(
+ "Attempting to use pdb to assign class hierarchies and extend known pdb data " +
+ "type information ...");
- // assign constructors and destructors based on name
- assignConstructorsAndDestructorsUsingExistingName(recoveredClasses);
- }
- // otherwise figure everything out from scratch
- else {
- monitor.setMessage("Figuring out class method types");
- // println(
+ retrieveExistingClassStructures(recoveredClasses);
+
+ // assign constructors and destructors based on name
+ assignConstructorsAndDestructorsUsingExistingName(recoveredClasses);
+ }
+ // otherwise figure everything out from scratch
+ else {
+ monitor.setMessage("Figuring out class method types");
+ // println(
// "Figuring out class method types (constructor, destructor, inline constructor, " +
// "inline destructor, deleting destructor, clone) ...");
- processConstructorAndDestructors(recoveredClasses);
-
- }
+ processConstructorAndDestructors(recoveredClasses);
- // create order of vftable in constructor map for each class that has a constructor so far
- createVftableOrderMap(recoveredClasses);
+ }
- determineParentClassInfoFromBaseClassArray(recoveredClasses);
+ // create order of vftable in constructor map for each class that has a constructor so far
+ createVftableOrderMap(recoveredClasses);
- assignParentClassToVftables(recoveredClasses);
+ determineParentClassInfoFromBaseClassArray(recoveredClasses);
- // using all the information found above, create the class structures, add the constructor,
- // destructor, vfunctions to class which finds the appropriate class structure and assigns
- // to "this" param
- //println("Creating class data types and applying class structures...");
- monitor.setMessage("Creating class data types and applying class structures");
- figureOutClassDataMembers(recoveredClasses);
+ assignParentClassToVftables(recoveredClasses);
- if (USE_SHORT_TEMPLATE_NAMES_IN_STRUCTURE_FIELDS) {
- extendedFlatAPI.createShortenedTemplateNamesForClasses(recoveredClasses);
- }
+ // using all the information found above, create the class structures, add the constructor,
+ // destructor, vfunctions to class which finds the appropriate class structure and assigns
+ // to "this" param
+ //println("Creating class data types and applying class structures...");
+ monitor.setMessage("Creating class data types and applying class structures");
+ figureOutClassDataMembers(recoveredClasses);
- createAndApplyClassStructures(recoveredClasses);
+ if (USE_SHORT_TEMPLATE_NAMES_IN_STRUCTURE_FIELDS) {
- extraUtils.createShortenedTemplateNamesForClasses(recoveredClasses);
++ extendedFlatAPI.createShortenedTemplateNamesForClasses(recoveredClasses);
+ }
- if (!isPDBLoaded) {
- // create better vftable labels for multi vftable classes
- updateMultiVftableLabels(recoveredClasses);
- //println("Removing erroneous FID namespaces and corresponding class data types");
- removeEmptyClassesAndStructures();
- }
+ createAndApplyClassStructures(recoveredClasses);
- return recoveredClasses;
- }
- catch (CancelledException e) {
- e.printStackTrace();
- return null;
- }
- catch (Exception e) {
- e.printStackTrace();
- return null;
+ if (!isPDBLoaded) {
+ // create better vftable labels for multi vftable classes
+ updateMultiVftableLabels(recoveredClasses);
+ //println("Removing erroneous FID namespaces and corresponding class data types");
+ removeEmptyClassesAndStructures();
}
+ return recoveredClasses;
+
}
private boolean isVisualStudioOrClangPe() {
@@@ -352,7 -375,7 +352,8 @@@
int sizeOfDt = completeObjLocatorDataType.getLength();
api.clearListing(address, address.add(sizeOfDt));
- Data completeObjectLocator = extraUtils.createData(address, completeObjLocatorDataType);
- Data completeObjectLocator = extendedFlatAPI.createData(address, completeObjLocatorDataType);
++ Data completeObjectLocator =
++ extendedFlatAPI.createData(address, completeObjLocatorDataType);
if (completeObjectLocator == null) {
return null;
}
@@@ -441,10 -464,10 +442,10 @@@
for (int i = 0; i < numBaseClasses; i++) {
monitor.checkCanceled();
-- //TODO: extraUtils.getReferencedAddress(address, getIboIf64bit);
++ //TODO: extendedFlatAPI.getReferencedAddress(address, getIboIf64bit);
Address baseClassDescriptorAddress = getReferencedAddress(address.add(i * 4));
- Data baseClassDescriptor = extraUtils.getDataAt(baseClassDescriptorAddress);
+ Data baseClassDescriptor = extendedFlatAPI.getDataAt(baseClassDescriptorAddress);
if (baseClassDescriptor == null || !baseClassDescriptor.getDataType()
.getName()
.equals(
@@@ -530,10 -553,10 +531,10 @@@
private Address createClassHierarchyDescriptor(Address address, Namespace classNamespace)
throws CancelledException, MemoryAccessException, InvalidInputException, Exception {
-- //TODO: extraUtils.getReferencedAddress(address, getIboIf64bit);
++ //TODO: extendedFlatAPI.getReferencedAddress(address, getIboIf64bit);
Address classHierarchyDescriptorAddress = getReferencedAddress(address);
- Data classHierarchyStructure = extraUtils.getDataAt(classHierarchyDescriptorAddress);
+ Data classHierarchyStructure = extendedFlatAPI.getDataAt(classHierarchyDescriptorAddress);
if (classHierarchyStructure != null &&
classHierarchyStructure.getDataType()
@@@ -611,9 -634,9 +612,9 @@@
symbolTable.getPrimarySymbol(classHierarchyDescriptorAddress);
Namespace classNamespace = classHierarchyDescriptorSymbol.getParentNamespace();
- int numBaseClasses = extraUtils.getInt(classHierarchyDescriptorAddress.add(8));
+ int numBaseClasses = extendedFlatAPI.getInt(classHierarchyDescriptorAddress.add(8));
-- //TODO: extraUtils.getReferencedAddress(address, getIboIf64bit);
++ //TODO: extendedFlatAPI.getReferencedAddress(address, getIboIf64bit);
Address baseClassArrayAddress =
getReferencedAddress(classHierarchyDescriptorAddress.add(12));
@@@ -718,9 -740,9 +719,10 @@@
continue;
}
- Reference[] referencesTo = extraUtils.getReferencesTo(completeObjectLocatorAddress);
- Reference[] referencesTo = extendedFlatAPI.getReferencesTo(completeObjectLocatorAddress);
++ Reference[] referencesTo =
++ extendedFlatAPI.getReferencesTo(completeObjectLocatorAddress);
if (referencesTo.length == 0) {
- //println("no refs to " + completeObjectLocatorAddress.toString());
+ Msg.debug(this, "No refs to " + completeObjectLocatorAddress.toString());
continue;
}
@@@ -989,25 -1011,31 +991,25 @@@
Address vftableAddress = vftableIterator.next();
Address ptrToColAddress = vftableAddress.subtract(defaultPointerSize);
- Data pointerToCompleteObjLocator = extraUtils.getDataAt(vftableAddress);
+ Data pointerToCompleteObjLocator = extendedFlatAPI.getDataAt(vftableAddress);
if (pointerToCompleteObjLocator == null) {
pointerToCompleteObjLocator =
- extraUtils.createData(ptrToColAddress, pointerDataType);
+ extendedFlatAPI.createData(ptrToColAddress, pointerDataType);
}
- Address colAddress = extraUtils.getReferencedAddress(ptrToColAddress, false);
+ Address colAddress = extendedFlatAPI.getReferencedAddress(ptrToColAddress, false);
if (colAddress == null) {
-// println(recoveredClass.getName() + " couldn't get referenced col from " +
-// ptrToColAddress.toString());
+ Msg.debug(this, recoveredClass.getName() +
+ " couldn't get referenced col from " + ptrToColAddress.toString());
continue;
}
- try {
- Address addressOfOffset = colAddress.add(4);
+ Address addressOfOffset = colAddress.add(4);
- int offset = extraUtils.getInt(addressOfOffset);
- int offset = extendedFlatAPI.getInt(addressOfOffset);
++ int offset = extendedFlatAPI.getInt(addressOfOffset);
- recoveredClass.addClassOffsetToVftableMapping(offset, vftableAddress);
- }
- catch (AddressOutOfBoundsException e) {
-// println(recoveredClass.getName() + "error getting offset at address " +
-// colAddress.toString() + " + 4");
- }
+ recoveredClass.addClassOffsetToVftableMapping(offset, vftableAddress);
}
}
@@@ -1222,9 -1250,21 +1224,9 @@@
RTTI_CLASS_HIERARCHY_DESCRIPTOR_LABEL, classNamespace, false);
if (symbols.size() >= 1) {
- return (extraUtils.getInt(symbols.get(0).getAddress().add(4)));
-
- try {
- return (extendedFlatAPI.getInt(symbols.get(0).getAddress().add(4)));
- }
- catch (MemoryAccessException e) {
-// println("Could not get class inheritance flag at address " +
-// symbols.get(0).getAddress().toString());
- return NONE;
- }
- catch (AddressOutOfBoundsException e) {
-// println("Could not get class inheritance flag at address " +
-// symbols.get(0).getAddress().toString());
- return NONE;
- }
++ return (extendedFlatAPI.getInt(symbols.get(0).getAddress().add(4)));
}
+
return NONE;
}