From fbcc8ee98770850c21730e38bd629d9b700367f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Mon, 7 Sep 2026 20:25:39 -0600 Subject: [PATCH] Fix type-B relocations losing the shared lo16 between two HI16s LoadRelocations2 declared last_type, initialised it to -1, read it once - and never assigned it. So the (flag & 0x38) == 0x08 case, which means "reuse the lo16 the previous relocation carried", always saw last_type != 4 and reset lo16 to 0 instead. That matters because R_MIPS_HI16 computes ((op << 16) + lo16) + relocate_to and then adds 0x10000 if bit 15 of the result is set, to pre-compensate the sign extension the paired addiu will do. With lo16 wrongly 0 the carry decision is made on the load address alone, so for any base whose low half has bit 15 set the high half comes out one too high and the pointer lands 0x10000 past what it should be. A compiler emits exactly this pattern around a branch-likely: one lui in the delay slot, another on the fall-through path, both for the same symbol, sharing a single addiu after the paths converge. Only the second lui is adjacent to a HI16, so the first one silently got the wrong high half. last_type is assigned where JPCSP assigns its R_TYPE_OLD: at the end of the branch that actually relocates something, so the commands that only move the base around don't count as "the previous relocation" and a HI16/HI16/LO16 group still pairs up across them. R_MIPS_NONE stops continuing the loop for the same reason - it has to clear last_type, or a HI16 after it would reuse a lo16 that isn't its own. Co-Authored-By: Claude Opus 5 --- Core/ELF/ElfReader.cpp | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/Core/ELF/ElfReader.cpp b/Core/ELF/ElfReader.cpp index ed2b4c61d7..8ed7d8a082 100644 --- a/Core/ELF/ElfReader.cpp +++ b/Core/ELF/ElfReader.cpp @@ -269,6 +269,8 @@ void ElfReader::LoadRelocations2(int rel_seg) int flag_bits, seg_bits, type_bits; int cmd, flag, seg, type; int off_seg = 0, addr_seg, rel_base, rel_offset; + // last_type is the type of the previous relocation, which one of the lo16 encodings below + // depends on. int relocate_to, last_type, lo16 = 0; u32 op, addr; int rcount = 0; @@ -329,6 +331,15 @@ void ElfReader::LoadRelocations2(int rel_seg) return; buf += type_table_size; + // Note: flash0:/kd/loadcore.prx and flash0:/kd/sysmem.prx are relocated by the PSP's own + // reboot code, which reads this table through a different type mapping - + // {0, 3, 6, 7, 1, 2, 4, 5} indexed by the entry, see + // https://github.com/uofw/uofw/blob/master/src/reboot/elf.c and the same special case in + // JPCSP's Loader.relocateFromBufferA1. We don't apply it, because we don't emulate that + // reboot path - those two only get here through a --vsh boot, and would need the module + // filename plumbed down to tell them apart. Worth doing if VSH/LLE boot is ever taken + // further; harmless for every game module, which is everything else that lands here. + rel_base = 0; last_type = -1; while(buf