From d0253732f60960ece865cc6c657af284c3f26f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Tue, 18 Aug 2026 00:25:25 +0200 Subject: [PATCH] Honor a module's declared segment alignment when allocating it ElfReader read the first loadable segment's p_align into firstSegAlign but only ever used it to round down textStart for symbol bookkeeping - the allocation itself used the block allocator's default grain. A module whose relocations are only valid at a more strictly aligned base therefore got loaded somewhere it couldn't work, and the symptom is addresses off by a multiple of 64KB rather than an outright failure. CrossCraft Classic (Zig) declares p_align 0x10000 and hits exactly that. It declares it deliberately: a stage of Zig's PSP pipeline emits mispaired HI16/LO16 relocations, and a 64KB-aligned base makes that harmless - with no low bits in the base no carry is ever needed, so which of a symbol's LO16 entries a HI16 was paired with stops affecting the result. PPSSPP put it at 0x08804000 instead, where the carry does matter: 46 of its addresses came out 64KB low, and it jumped through a bogus vtable a few seconds in. It now loads at 0x08810000 and all 8405 lui/addiu pairs resolve correctly. Worth being clear that the relocation code was never wrong here - it matches what the hardware does, pairing a run of HI16 with the next non-HI16 entry and applying the carry. Only the load address differed. The two AllocAt paths can't move the module, since the caller picked the address, so they just report a misaligned one instead. Co-Authored-By: Claude Opus 5 Claude-Session: https://claude.ai/code/session_01GZq8ZtJmFY7bkX5FVkr3P9 --- Core/ELF/ElfReader.cpp | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/Core/ELF/ElfReader.cpp b/Core/ELF/ElfReader.cpp index bf4eea4324..37e9830df1 100644 --- a/Core/ELF/ElfReader.cpp +++ b/Core/ELF/ElfReader.cpp @@ -488,13 +488,37 @@ int ElfReader::LoadInto(u32 loadAddress, bool fromTop) { } else if (loadAddress) { - // Binary needs to be relocated: add loadAddress to the binary start address + // Binary needs to be relocated: add loadAddress to the binary start address. + // The caller picked the address, so we can't move it - but say so if it doesn't meet + // what the module asked for, since that's how relocations end up off by 64KB. + if (firstSegAlign > 1 && ((loadAddress + totalStart) & (firstSegAlign - 1)) != 0) { + WARN_LOG_REPORT(Log::Loader, "Module %s loaded at %08x, which doesn't meet its segment alignment %08x", + modName.c_str(), loadAddress + totalStart, firstSegAlign); + } vaddr = memblock.AllocAt(loadAddress + totalStart, totalSize, modName.c_str()); } else { - // Just put it where there is room - vaddr = memblock.Alloc(totalSize, fromTop, modName.c_str()); + // Just put it where there is room, but honor the alignment the first loadable segment + // asks for. Most PRXs want no more than the allocator's default grain, so this usually + // changes nothing - but a module's relocations are only guaranteed to resolve correctly + // at a base meeting its declared alignment, and ignoring that produces addresses that + // are wrong by a multiple of 64KB rather than an outright failure. + // + // CrossCraft Classic (Zig) is the case in point: it declares p_align 0x10000 precisely + // because a stage of Zig's PSP pipeline emits mispaired HI16/LO16 relocations, and a + // 64KB-aligned base makes that harmless (no carry is ever needed, so which of a symbol's + // LO16 entries a HI16 got paired with stops mattering). Loaded at 0x08804000 instead, 46 + // of its addresses came out 64KB low and it jumped through a bogus vtable almost at once. + u32 align = firstSegAlign; + if (align > 1 && (align & (align - 1)) == 0) { + if (align > 0x1000) { + INFO_LOG(Log::Loader, "Module %s requests an unusually large segment alignment (%08x)", modName.c_str(), align); + } + vaddr = memblock.AllocAligned(totalSize, 1, align, fromTop, modName.c_str()); + } else { + vaddr = memblock.Alloc(totalSize, fromTop, modName.c_str()); + } } if (vaddr == (u32)-1) {