Skip to content

Instantly share code, notes, and snippets.

@aldelaro5
Created April 18, 2019 03:31
Show Gist options
  • Save aldelaro5/8c6bea2493cf7b24a49c05c56d433aa9 to your computer and use it in GitHub Desktop.
Save aldelaro5/8c6bea2493cf7b24a49c05c56d433aa9 to your computer and use it in GitHub Desktop.
SHAR leaked ps2 symbols import script
//@author aldelaro5
//@category Import
//@keybinding
//@menupath
//@toolbar
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileReader;
import java.util.ArrayList;
import ghidra.app.script.GhidraScript;
import ghidra.app.util.demangler.DemangledObject;
import ghidra.app.util.demangler.DemanglerOptions;
import ghidra.app.util.demangler.DemanglerUtil;
import ghidra.program.database.function.OverlappingFunctionException;
import ghidra.program.model.address.AddressFactory;
import ghidra.program.model.address.AddressSet;
import ghidra.program.model.address.AddressSpace;
import ghidra.program.model.listing.Program;
import ghidra.program.model.symbol.Namespace;
import ghidra.program.model.symbol.SourceType;
import ghidra.program.model.symbol.SymbolTable;
public class ImportSharSymbolScript extends GhidraScript
{
@Override
protected void run() throws Exception
{
class SharSymbol
{
public long address = 0;
public long size = 0;
public String symbol = "";
public DemangledObject demangledSymbol = null;
public String objectFileName = "";
}
ArrayList<SharSymbol> symbolsList = new ArrayList<SharSymbol>();
String fileName = "/home/aldelaro5/Documents/SHAR PS2 beta/SRR2.MAP";
BufferedReader reader = new BufferedReader(new FileReader(fileName));
// Skip the 2 headers lines
reader.readLine();
reader.readLine();
String line = "";
String objectFileName = null;
while ((line = reader.readLine()) != null)
{
if (line.contains(" "))
{
String addressHex = line.substring(0, 8);
String sizeHex = line.substring(9, 17);
String mangledSymbol = line.substring(48);
SharSymbol sym = new SharSymbol();
sym.address = Integer.parseUnsignedInt(addressHex, 16);
sym.size = Integer.parseUnsignedInt(sizeHex, 16);
sym.demangledSymbol = DemanglerUtil.demangle(mangledSymbol);
sym.symbol = mangledSymbol;
sym.objectFileName = objectFileName;
symbolsList.add(sym);
}
else if (line.contains(" "))
{
if (line.contains("\\"))
objectFileName = line.substring(line.lastIndexOf('\\') + 1);
else
objectFileName = line.substring(40);
}
}
reader.close();
Program program = getCurrentProgram();
AddressFactory factory = program.getAddressFactory();
AddressSpace addressSpace = factory.getDefaultAddressSpace();
SymbolTable symbolTable = program.getSymbolTable();
Namespace globalNamespace = program.getGlobalNamespace();
var demanglerOptions = new DemanglerOptions();
demanglerOptions.setApplySignature(true);
for (var symbolInfo : symbolsList)
{
Namespace objectNamespace = null;
objectNamespace = symbolTable.getNamespace(symbolInfo.objectFileName, globalNamespace);
if (objectNamespace == null)
{
objectNamespace = symbolTable.createNameSpace(globalNamespace, symbolInfo.objectFileName,
SourceType.IMPORTED);
}
if (symbolInfo.demangledSymbol != null)
{
var demangledNamespace = symbolInfo.demangledSymbol.getNamespace();
if (demangledNamespace != null && demangledNamespace != globalNamespace)
{
var realNamespace = symbolTable.getNamespace(demangledNamespace.getName(), objectNamespace);
if (realNamespace == null)
{
realNamespace = symbolTable.createNameSpace(objectNamespace, demangledNamespace.getName(),
SourceType.IMPORTED);
}
objectNamespace = realNamespace;
}
}
var demangledName = symbolInfo.demangledSymbol == null ? symbolInfo.symbol
: symbolInfo.demangledSymbol.getName();
var symbolAddress = addressSpace.getAddress(symbolInfo.address);
symbolTable.createLabel(symbolAddress, demangledName,
objectNamespace == null ? globalNamespace : objectNamespace, SourceType.ANALYSIS);
// If it's a function, create it.
if (symbolInfo.size > 3 && program.getMemory().getBlock(symbolAddress).isExecute())
{
var addressSet = new AddressSet(symbolAddress,
addressSpace.getAddress(symbolInfo.address + symbolInfo.size - 1));
try
{
program.getFunctionManager().createFunction(demangledName,
objectNamespace == null ? globalNamespace : objectNamespace, symbolAddress, addressSet,
SourceType.ANALYSIS);
}
catch (OverlappingFunctionException | IllegalArgumentException e)
{
e.printStackTrace();
}
}
if (symbolInfo.demangledSymbol != null)
{
try
{
symbolInfo.demangledSymbol.applyTo(program, addressSpace.getAddress(symbolInfo.address),
demanglerOptions, monitor);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment