Loader
The Cafe OS loader is responsible for loading RPL-formatted libraries and executables into memory. It is a standard ELF executable named loader.elf. It includes a statically linked copy of zlib, probably for decompressing sections of RPL files. Each userspace process has the loader mapped into its address space, and the loader is initially ran on process creation, loading coreinit.rpl, the application's dependencies, and the application RPX itself. After control passes to the application, the loader continues to be available through a special loader call interface, used by the OSDynLoad functions in coreinit.
Functions
__LoaderStart(bool user_req, LoaderRequest *request)
__LoaderStart() is the entry point of loader.elf, the first function called by both the kernel on process startup and loader calls from OSDynLoad. It takes a caller argument from the kernel in r3, which is 0 if called on process startup and 1 if called from OSDynLoad. There is also a loader call buffer passed in r4. If the function has been called from OSDynLoad, it goes to LOADER_Entry() and processes the loader call. Otherwise, it continues and sets up the app address space.
LOADER_Entry(LoaderRequest* request)
LOADER_Entry() receives a loader request buffer from the coreinit OSDynLoad functions. These are copied to the matching LoaderEntry global variables at the start of this method. The dispatchCode is looked up in a jumptable, and used to call the appropriate loader function. Once the function completes, it calls a kernel syscall to return to the caller's context.
Structures
LoaderRequest
This structure is a request buffer created by the OSDynLoad functions and sent to the loader. It contains the saved context of the caller, the identifying process information, and the function number and arguments. Dispatch Codes are from 0-11 (inclusive). After processing the request, the loader sends it back with a return value.
typedef struct
{
void *context;
int procId;
void *procConfig;
void *context2;
char unkc[0x14-0xc];
int returnVal;
int dispatchCode;
} LoaderRequest;
Dispatch Codes
Code | Description |
---|---|
0x00 | Invalid dispatchCode (Called if dispatchCode > 0x0B) |
0x01 | Loader_UpdateHeartBeat |
0x02 | Loader_Setup |
0x03 | Loader_Setup (Same as 02) |
0x04 | Loader_Link |
0x05 | Loader_Query |
0x06 | Loader_Tag |
0x07 | Loader_UserGainControl |
0x08 | Loader_Done |
0x09 | Loader_GetLoaderHeapStatistics |
0x0A | Unknown |
0x0B | Unknown |