GP-4371 Added check to RecoverClassesFromRTTIScript to not run when there are unhandled relocations.

This commit is contained in:
ghidra007 2024-03-04 18:23:46 +00:00
parent 59a048fdf2
commit fd8fcebe09

View file

@ -54,18 +54,10 @@
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import classrecovery.DecompilerScriptUtils;
import classrecovery.RTTIClassRecoverer;
import classrecovery.RTTIGccClassRecoverer;
import classrecovery.RTTIWindowsClassRecoverer;
import classrecovery.RecoveredClass;
import classrecovery.RecoveredClassHelper;
import classrecovery.*;
import generic.theme.GThemeDefaults.Colors.Palette;
import ghidra.app.decompiler.DecompInterface;
import ghidra.app.plugin.core.analysis.AutoAnalysisManager;
@ -83,30 +75,15 @@ import ghidra.app.util.importer.MessageLog;
import ghidra.app.util.opinion.PeLoader;
import ghidra.framework.options.Options;
import ghidra.framework.plugintool.PluginTool;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSetView;
import ghidra.program.model.data.CategoryPath;
import ghidra.program.model.data.DataType;
import ghidra.program.model.data.DataTypeComponent;
import ghidra.program.model.data.DataTypeManager;
import ghidra.program.model.data.Structure;
import ghidra.program.model.listing.Function;
import ghidra.program.model.listing.Parameter;
import ghidra.program.model.listing.Program;
import ghidra.program.model.address.*;
import ghidra.program.model.data.*;
import ghidra.program.model.listing.*;
import ghidra.program.model.mem.MemoryBlock;
import ghidra.program.model.reloc.Relocation;
import ghidra.program.model.reloc.RelocationTable;
import ghidra.program.model.symbol.Symbol;
import ghidra.program.util.GhidraProgramUtilities;
import ghidra.service.graph.AttributedEdge;
import ghidra.service.graph.AttributedGraph;
import ghidra.service.graph.AttributedVertex;
import ghidra.service.graph.GraphDisplay;
import ghidra.service.graph.GraphDisplayOptions;
import ghidra.service.graph.GraphDisplayOptionsBuilder;
import ghidra.service.graph.GraphDisplayProvider;
import ghidra.service.graph.GraphType;
import ghidra.service.graph.GraphTypeBuilder;
import ghidra.service.graph.VertexShape;
import ghidra.service.graph.*;
import ghidra.util.exception.CancelledException;
import ghidra.util.exception.GraphException;
import ghidra.util.task.TaskMonitor;
@ -218,7 +195,7 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
this, BOOKMARK_FOUND_FUNCTIONS, USE_SHORT_TEMPLATE_NAMES_IN_STRUCTURE_FIELDS,
nameVfunctions, hasDebugSymbols, monitor);
}
else if (isPE() && isGcc()){
else if (isPE() && isGcc()) {
println("Program is a gcc compiled PE.");
@ -441,7 +418,7 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
}
// check that gcc loader or mingw analyzer has fixed the relocations correctly
if(isGcc()) {
if (isGcc()) {
// first check that there is even rtti by searching the special string in memory
if (!isStringInProgramMemory("class_type_info")) {
@ -451,14 +428,42 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
// then check to see if the special typeinfo namespace is in external space
// if so then relocations are present and have not been fixed up because when fixed up
// the namespace gets moved to inside program space
if(isExternalNamespace("__cxxabiv1::__class_type_info")) {
if (isExternalNamespace("__cxxabiv1::__class_type_info")) {
return ("This program's relocations were not correctly fixed so the script cannot " +
"continue. If this program is mingw this is a known issue and " +
"will be fixed in a later release. For all other gcc programs please " +
"contact the Ghidra team so this issue can be fixed.");
}
if (hasUnhandledRelocations()) {
return ("This program has unhandled elf relocations so cannot continue. Please " +
"contact the Ghidra team for assistance.");
}
}
return new String();
}
private boolean hasUnhandledRelocations() throws CancelledException {
RelocationTable relocationTable = currentProgram.getRelocationTable();
Iterator<Relocation> relocations = relocationTable.getRelocations();
while (relocations.hasNext()) {
monitor.checkCancelled();
Relocation r = relocations.next();
if (r.getSymbolName().contains("class_type_info") &&
(r.getStatus() != Relocation.Status.APPLIED &&
r.getStatus() != Relocation.Status.APPLIED_OTHER &&
r.getStatus() != Relocation.Status.SKIPPED)) {
return true;
}
}
return false;
}
private void analyzeProgramChanges(AddressSetView beforeChanges) throws Exception {
@ -642,7 +647,6 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
return false;
}
/**
* Method to check if executable format is PE
*/
@ -671,7 +675,7 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
}
String compiler = currentProgram.getCompiler();
if(compiler != null && compiler.contains("gcc")) {
if (compiler != null && compiler.contains("gcc")) {
return true;
}
@ -701,7 +705,6 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
return isGcc;
}
/**
* Method to set the global variable isWindows
*/
@ -1592,9 +1595,9 @@ public class RecoverClassesFromRTTIScript extends GhidraScript {
List<Symbol> symbols = NamespaceUtils.getSymbols(path, currentProgram, true);
for(Symbol symbol : symbols) {
for (Symbol symbol : symbols) {
monitor.checkCancelled();
if(symbol.isExternal() && symbol.getSymbolType().isNamespace()) {
if (symbol.isExternal() && symbol.getSymbolType().isNamespace()) {
return true;
}
}