118 lines
4.9 KiB
Java
118 lines
4.9 KiB
Java
// Parse the shipped Iggy ABI headers and apply matching function signatures.
|
|
// @category Iggy
|
|
|
|
import ghidra.app.cmd.function.ApplyFunctionSignatureCmd;
|
|
import ghidra.app.script.GhidraScript;
|
|
import ghidra.app.util.cparser.C.CParserUtils;
|
|
import ghidra.program.model.data.DataType;
|
|
import ghidra.program.model.data.FileDataTypeManager;
|
|
import ghidra.program.model.data.FunctionDefinition;
|
|
import ghidra.program.model.listing.Function;
|
|
import ghidra.program.model.listing.FunctionIterator;
|
|
import ghidra.program.model.symbol.SourceType;
|
|
|
|
import java.io.File;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class ApplyIggyTypes extends GhidraScript {
|
|
@Override
|
|
protected void run() throws Exception {
|
|
String[] scriptArgs = getScriptArgs();
|
|
if (scriptArgs.length != 4) {
|
|
throw new IllegalArgumentException(
|
|
"expected include directory, stub directory, GDT path, and platform");
|
|
}
|
|
|
|
File includeDirectory = new File(scriptArgs[0]).getCanonicalFile();
|
|
File stubDirectory = new File(scriptArgs[1]).getCanonicalFile();
|
|
File archiveFile = new File(scriptArgs[2]).getCanonicalFile();
|
|
String platform = scriptArgs[3];
|
|
if (!archiveFile.isFile()) {
|
|
String[] headers = {
|
|
new File(includeDirectory, "iggy.h").getPath(),
|
|
new File(includeDirectory, "gdraw.h").getPath(),
|
|
new File(includeDirectory, "iggyperfmon.h").getPath(),
|
|
new File(includeDirectory, "iggyexpruntime.h").getPath(),
|
|
new File(stubDirectory, "iggy_internal_overlay.h").getPath()
|
|
};
|
|
String[] includePaths = {stubDirectory.getPath(), includeDirectory.getPath()};
|
|
String[] parseArgs = getParseArguments(platform);
|
|
FileDataTypeManager parsed = CParserUtils.parseHeaderFiles(
|
|
null, headers, includePaths, parseArgs, archiveFile.getPath(),
|
|
currentProgram.getLanguageID().toString(),
|
|
currentProgram.getCompilerSpec().getCompilerSpecID().toString(), monitor);
|
|
parsed.close();
|
|
}
|
|
|
|
int applied = 0;
|
|
try (FileDataTypeManager archive = FileDataTypeManager.openFileArchive(archiveFile, false)) {
|
|
FunctionIterator functions = currentProgram.getFunctionManager().getFunctions(true);
|
|
for (Function function : functions) {
|
|
if (function.isExternal()) {
|
|
continue;
|
|
}
|
|
String lookupName = function.getName();
|
|
while (lookupName.startsWith(".") || lookupName.startsWith("_")) {
|
|
lookupName = lookupName.substring(1);
|
|
}
|
|
if (!lookupName.startsWith("Iggy")) {
|
|
continue;
|
|
}
|
|
List<DataType> matches = new ArrayList<>();
|
|
archive.findDataTypes(lookupName, matches);
|
|
for (DataType match : matches) {
|
|
if (!(match instanceof FunctionDefinition)) {
|
|
continue;
|
|
}
|
|
ApplyFunctionSignatureCmd command = new ApplyFunctionSignatureCmd(
|
|
function.getEntryPoint(), (FunctionDefinition)match, SourceType.IMPORTED);
|
|
if (command.applyTo(currentProgram, monitor)) {
|
|
applied++;
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
println("Applied " + applied + " Iggy ABI signatures to " + currentProgram.getName());
|
|
}
|
|
|
|
private String[] getParseArguments(String platform) {
|
|
List<String> arguments = new ArrayList<>();
|
|
arguments.add("-D__RADINSTATICLIB__=1");
|
|
arguments.add("-D__RADNOEXPORTS__=1");
|
|
arguments.add("-D__RADFINAL__=1");
|
|
switch (platform) {
|
|
case "durango":
|
|
arguments.add("-D_DURANGO=1");
|
|
arguments.add("-D_WIN64=1");
|
|
arguments.add("-D_WIN32=1");
|
|
arguments.add("-D_M_X64=100");
|
|
break;
|
|
case "windows":
|
|
arguments.add("-D_WIN64=1");
|
|
arguments.add("-D_WIN32=1");
|
|
arguments.add("-D_M_X64=100");
|
|
break;
|
|
case "orbis":
|
|
arguments.add("-D__ORBIS__=1");
|
|
arguments.add("-D__GNUC__=1");
|
|
arguments.add("-D__x86_64__=1");
|
|
break;
|
|
case "ps3":
|
|
arguments.add("-D__CELLOS_LV2__=1");
|
|
arguments.add("-D__GNUC__=1");
|
|
arguments.add("-D__LP32__=1");
|
|
break;
|
|
case "vita":
|
|
arguments.add("-D__psp2__=1");
|
|
arguments.add("-D__GNUC__=1");
|
|
arguments.add("-D__arm__=1");
|
|
break;
|
|
default:
|
|
throw new IllegalArgumentException("unsupported Iggy platform: " + platform);
|
|
}
|
|
return arguments.toArray(new String[0]);
|
|
}
|
|
}
|