So now we know what a stub is, and also how it looks like. Time to see how we can resolve the stubs for the ELF program we already loaded.
To do this, we can simply use the game's already resolved imports and subtitute the right stub with the right call from the game's stubs. To do this, I would first backup all game's stubs in a data structure, so we don't lost them if they're going to be overwritten by the homebrew ELF we loaded. So, as I said, first we need to backup the game's stubs before loading the ELF program section. This can easily be done using the information I gave on the previous chapter.
I personally prefer saving memory and not include the whole stubs, but only the effective call associated with a NID. For example this struct:
Code: Select all
// Struct holding all NIDs imported by the game and their respective jump/syscalls
typedef struct
{
u32 nid; // NID
u32 call; // Syscall/jump associated to the NID
} tNIDResolver;User mode system call:
Code: Select all
0x08C92974: 0x0A200020 ' . .' - j loc_08800080
0x08C92978: 0x00000000 '....' - nop Code: Select all
0x08C92984: 0x03E00008 '....' - jr $ra
0x08C92988: 0x00088A8C '....' - syscall 0x222ACode: Select all
#define SYSCALL_MASK_IMPORT 0x01000000
// Return real instruction that makes the system call (jump or syscall)
u32 get_good_call(u32* call_pointer)
{
// Dirty hack here but works
if(*call_pointer & SYSCALL_MASK_IMPORT)
call_pointer++;
return *call_pointer;
}To resolve the ELF stubs, we just do the inverse process: check the effective call we have on the tNIDResolver data structure, then recreate the stub's missing instruction. Here's an example:
Code: Select all
#define JR_RA_OPCODE 0x03E00008
#define NOP_OPCODE 0x00000000
/* Subsitutes the right instruction */
void resolve_call(u32 *call_to_resolve, u32 call_resolved)
{
/* SYSCALL */
if(!(call_resolved & SYSCALL_MASK_RESOLVE))
{
*call_to_resolve = JR_RA_OPCODE;
*(++call_to_resolve) = call_resolved;
}
/* JUMP */
else
{
*call_to_resolve = call_resolved;
*(++call_to_resolve) = NOP_OPCODE;
}
}
Advertising

