# Toolchain and ABI ## Compiler flags ``` -m32 -mpowerpc64 -mhard-float -maltivec -fno-pic -G0 -msdata=none ``` - **`-m32 -mpowerpc64`:** titles run with 32-bit pointers in 64-bit registers. The code is 32-bit SysV PowerPC, which may use 64-bit instructions. - **`-fno-pic -G0 -msdata=none`:** no small-data sections and no PIC. The XEX is loaded at a fixed address (0x82000000, code from 0x82010000). - Use `-ffunction-sections -fdata-sections` together with `--gc-sections`. The link script keeps `.ctors`, `.eh_frame` and the import sections. ## Calling the kernel: the console ABI The kernel and XAM use the console's own calling convention, which differs from SysV in ways that matter: | | SysV (our code) | Console (kernel, XAM) | |---|---|---| | r14-r31 | callee saves the low 32 bits | callee must keep all 64 bits | | Arguments 9+ | 4-byte slots at 8(r1) | 8-byte slots from 0x50 of the caller's frame | | Home area | none | 0x50 bytes the callee may store r3-r10 in | | 64-bit argument | two registers | one register | xenon-forge's import thunks (`sdk/thunks`) translate calls into the kernel. Functions with more than 8 arguments need a hand-written stub: see `VdSwap` (10 arguments) in `sdk/thunks/ppc32.S` and `VdInitializeScalerCommandBuffer` (12) in `runtime/extra_imports.S`: ```asm xbox360_frame 0x70 li 12, 0; stw 12, 0x50(1) ; argument 9, high word 0 lwz 11, 0x78(1); stw 11, 0x54(1) ; ... low word from the SysV slot ... ``` A 64-bit argument goes in a single register. `XNotifyQueueUI`'s `Areas` argument is one, so the text pointer moves to r6. ### The kernel calling back into the title The **title's entry point**, the **audio render-driver callback** and the **vectored exception handler** are all called by the kernel with the console ABI. GCC's prologues save only the low words of r14-r31, so the kernel would get corrupted registers back. Wrap these functions in `xbox360_entry` (`sdk/crt/entry.inc`), which saves r14-r31 with `std`/`ld` and reserves the home area. See `runtime/kernel_entries.S`. ## Imports An import is a 16-byte thunk whose first word is `type << 24 | library << 16 | ordinal`, with library 0 = xboxkrnl and 1 = xam. Add a missing one with `xbox360_stub Name, 0x0100xxxx` in `runtime/extra_imports.S`. Xenia's `xboxkrnl_table.inc` and `xam_table.inc` list the ordinals. Ordinals we needed beyond xenon-forge's headers: | Function | Record | |---|---| | `XNotifyQueueUI` (5 arguments, not `XNotifyQueueUICustom`) | 0x01010290 | | `VdQueryVideoMode` | 0x010001ca | | `VdInitializeScalerCommandBuffer` | 0x010001c5 | | `VdCallGraphicsNotificationRoutines` | 0x010001b1 | | `RtlSetVectoredExceptionHandler` | 0x0100036d | ## Link script and C++ start-up `runtime/link.ld` places text, rodata+`.eh_frame`, data (with `__CTOR_LIST__`/`__CTOR_END__`) and bss in separate 64 KB pages. `crt.c`, in order: 1. installs the fault handler (see [Crashes and debugging](Crashes-and-Debugging.md)); 2. registers `.eh_frame` with `__register_frame`, so C++ exceptions work; 3. mounts the devices; 4. runs `.ctors` **from the end towards the start**, as GCC expects; 5. calls `main(1, {"game:\\default.xex"})`, then `exit()`, which ends in `XamLoaderTerminateTitle`, back to the dashboard. The `.eh_frame` section has to be zero-terminated, and code that C++ exceptions pass through has to be compiled as C++. Lua's C sources, for example, are built with `-x c++` when Lua errors are C++ exceptions. ## Stack xex-link writes the main thread's stack size into the PE header, and the kernel uses it. Upstream writes 256 KB. Lua and emulator cores recurse deeper than that, and a **stack overflow hangs the console** without any report, so `tools/build-xex-link.sh` patches it to 1 MB. `tests/hello` recurses 896 KB deep to check it. Keep big arrays off the stack anyway: make them `static`, or put them on the heap. ## Heap `malloc` comes from newlib's `_sbrk_r` over a 96 MB `.bss` arena (`XEX_HEAP_SIZE` in `runtime/sys.c`). Physical memory for the GPU and audio comes from `MmAllocatePhysicalMemoryEx`.