Skip to content

Commit

Permalink
Automatically set the entry point for kip1s
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Mar 30, 2019
1 parent 0de2e8f commit eb80587
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public abstract class SwitchProgramBuilder
protected Program program;
protected MemoryBlockUtil mbu;

long baseAddress;
protected long baseAddress;
protected AddressSpace aSpace;
protected MemoryBlockHelper memBlockHelper;

Expand Down Expand Up @@ -515,9 +515,42 @@ private String[] getDynamicLibraryNames()
return dynamicLibraryNames;
}

protected Address createEntryFunction(String name, long entryAddr, TaskMonitor monitor)
{
Address entryAddress = this.aSpace.getAddress(entryAddr);

// TODO: Entry may refer to a pointer - make sure we have execute permission
MemoryBlock block = this.program.getMemory().getBlock(entryAddress);

if (block == null || !block.isExecute())
{
return entryAddress;
}

Function function = program.getFunctionManager().getFunctionAt(entryAddress);

if (function != null)
{
program.getSymbolTable().addExternalEntryPoint(entryAddress);
return entryAddress; // symbol-based function already created
}

try
{
createOneByteFunction(name, entryAddress, true);
}
catch (Exception e)
{
Msg.error(this, "Could not create symbol at entry point: " + e);
}

return entryAddress;
}

private Set<Long> processRelocations(Program program, BinaryReader provider, List<Relocation> relocs, ElfSymbolTable symtab, long rel, long relsz) throws IOException
{
Set<Long> locations = new HashSet<Long>();

for (long i = 0; i < relsz / 0x18; i++)
{
long offset = provider.readLong(rel + i * 0x18);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/adubbz/switchloader/kip1/KIP1ProgramBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import ghidra.app.util.bin.ByteArrayProvider;
import ghidra.app.util.bin.ByteProvider;
import ghidra.app.util.importer.MemoryConflictHandler;
import ghidra.program.model.address.Address;
import ghidra.program.model.address.AddressOutOfBoundsException;
import ghidra.program.model.address.AddressOverflowException;
import ghidra.program.model.address.AddressSpace;
Expand All @@ -38,6 +39,15 @@ public static void loadKIP1(KIP1Header header, ByteProvider provider, Program pr
builder.load(monitor);
}

@Override
protected void load(TaskMonitor monitor)
{
super.load(monitor);

// KIP1s always start with a branch instruction at the start of their text
this.createEntryFunction("entry", this.baseAddress, monitor);
}

@Override
protected void loadDefaultSegments(TaskMonitor monitor) throws IOException, AddressOverflowException, AddressOutOfBoundsException
{
Expand Down

0 comments on commit eb80587

Please sign in to comment.