diff options
| author | Derek Hensley <hensley.derek58@gmail.com> | 2022-05-31 11:28:17 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-31 20:28:17 +0200 |
| commit | bd6b51a869b34c1695397dcfc9ef3248faa2e5b0 (patch) | |
| tree | 6f60700c8032a8e85beb3a5a4e4f6806f221c423 /src/code/relocation.c | |
| parent | 6eeb217225f9b865c0a3da0d31b512815c01b415 (diff) | |
Overlay Functions Cleanup (#1235)
* Cleanup load functions
* Some cleanup
* forgot .
* Split off Overlay_Load
* review
* OverlayRelocationType -> MIPSRelocationType
* Reloc type macro and mips relocations
Diffstat (limited to 'src/code/relocation.c')
| -rw-r--r-- | src/code/relocation.c | 101 |
1 files changed, 53 insertions, 48 deletions
diff --git a/src/code/relocation.c b/src/code/relocation.c index c56bc9115..60fbc7e1a 100644 --- a/src/code/relocation.c +++ b/src/code/relocation.c @@ -1,23 +1,23 @@ #include "global.h" -void Overlay_Relocate(void* allocatedVRamAddress, OverlayRelocationSection* overlayInfo, void* vRamAddress) { +void Overlay_Relocate(void* allocatedVRamAddress, OverlayRelocationSection* overlayInfo, void* vRamStart) { u32 sections[4]; u32 relocatedValue; u32 dbg; u32 relocOffset; u32 relocData; - u32 unrelocatedAddress; + uintptr_t unrelocatedAddress; u32 i; u32* relocDataP; u32* luiRefs[32]; u32 luiVals[32]; - u32 relocatedAddress; + uintptr_t relocatedAddress; u32 reloc; - u32 vaddr; u32* luiInstRef; - u32 allocu32 = (u32)allocatedVRamAddress; + uintptr_t allocu32 = (uintptr_t)allocatedVRamAddress; u32* regValP; u32 isLoNeg; + s32 pad; relocOffset = 0; relocatedValue = 0; @@ -25,7 +25,7 @@ void Overlay_Relocate(void* allocatedVRamAddress, OverlayRelocationSection* over relocatedAddress = 0; if (gOverlayLogSeverity >= 3) { - osSyncPrintf("DoRelocation(%08x, %08x, %08x)\n", allocatedVRamAddress, overlayInfo, vRamAddress); + osSyncPrintf("DoRelocation(%08x, %08x, %08x)\n", allocatedVRamAddress, overlayInfo, vRamStart); osSyncPrintf("text=%08x, data=%08x, rodata=%08x, bss=%08x\n", overlayInfo->textSize, overlayInfo->dataSize, overlayInfo->rodataSize, overlayInfo->bssSize); } @@ -37,55 +37,60 @@ void Overlay_Relocate(void* allocatedVRamAddress, OverlayRelocationSection* over for (i = 0; i < overlayInfo->nRelocations; i++) { reloc = overlayInfo->relocations[i]; - relocDataP = (u32*)(sections[reloc >> 0x1E] + (reloc & 0xFFFFFF)); + relocDataP = (u32*)(sections[RELOC_SECTION(reloc)] + RELOC_OFFSET(reloc)); relocData = *relocDataP; - switch (reloc & 0x3F000000) { - case 0x2000000: - /* R_MIPS_32 - * Handles 32-bit address relocation. Used in things such as - * jump tables. - */ - if ((*relocDataP & 0xF000000) == 0) { - luiInstRef = vRamAddress; - relocOffset = *relocDataP - (u32)luiInstRef; + + switch (RELOC_TYPE_MASK(reloc)) { + case R_MIPS_32 << RELOC_TYPE_SHIFT: + // Handles 32-bit address relocation, used for things such as jump tables and pointers in data. + // Just relocate the full address. + + // Check address is valid for relocation + if ((*relocDataP & 0x0F000000) == 0) { + relocOffset = *relocDataP - (uintptr_t)vRamStart; relocatedValue = relocOffset + allocu32; relocatedAddress = relocatedValue; unrelocatedAddress = relocData; *relocDataP = relocatedAddress; } break; - case 0x4000000: - /* R_MIPS_26 - * Handles 26-bit address relocation, used for jumps and jals - */ - unrelocatedAddress = ((*relocDataP & 0x3FFFFFF) << 2) | 0x80000000; - relocOffset = unrelocatedAddress - (u32)vRamAddress; - relocatedValue = (*relocDataP & 0xFC000000) | (((allocu32 + relocOffset) & 0xFFFFFFF) >> 2); - relocatedAddress = ((relocatedValue & 0x3FFFFFF) << 2) | 0x80000000; + + case R_MIPS_26 << RELOC_TYPE_SHIFT: + // Handles 26-bit address relocation, used for jumps and jals. + // Extract the address from the target field of the J-type MIPS instruction. + // Relocate the address and update the instruction. + + unrelocatedAddress = PHYS_TO_K0((*relocDataP & 0x03FFFFFF) << 2); + relocOffset = unrelocatedAddress - (uintptr_t)vRamStart; + relocatedValue = (*relocDataP & 0xFC000000) | (((allocu32 + relocOffset) & 0x0FFFFFFF) >> 2); + relocatedAddress = PHYS_TO_K0((relocatedValue & 0x03FFFFFF) << 2); *relocDataP = relocatedValue; break; - case 0x5000000: - /* R_MIPS_HI16 - * Handles relocation for a lui instruciton, store the reference to - * the instruction, and will update it in the R_MIPS_LO16 section. - */ + + case R_MIPS_HI16 << RELOC_TYPE_SHIFT: + // Handles relocation for a hi/lo pair, part 1. + // Store the reference to the LUI instruction (hi) using the `rt` register of the instruction. + // This will be updated later in the `R_MIPS_LO16` section. + luiRefs[(*relocDataP >> 0x10) & 0x1F] = relocDataP; luiVals[(*relocDataP >> 0x10) & 0x1F] = *relocDataP; break; - case 0x6000000: - /* R_MIPS_LO16 - * Updates the LUI instruction to reflect the relocated address. - * The full address is calculated from the LUI and lo parts, and then updated. - * if the lo part is negative, add 1 to the lui. - */ - regValP = &luiVals[((*relocDataP >> 0x15) & 0x1F)]; - vaddr = (*regValP << 0x10) + (s16)*relocDataP; - luiInstRef = luiRefs[((*relocDataP >> 0x15) & 0x1F)]; - if ((vaddr & 0xF000000) == 0) { - relocOffset = vaddr - (u32)vRamAddress; - vaddr = (s16)relocData; + + case R_MIPS_LO16 << RELOC_TYPE_SHIFT: + // Handles relocation for a hi/lo pair, part 2. + // Grab the stored LUI (hi) from the `R_MIPS_HI16` section using the `rs` register of the instruction. + // The full address is calculated, relocated, and then used to update both the LUI and lo instructions. + // If the lo part is negative, add 1 to the LUI value. + // Note: The lo instruction is assumed to have a signed immediate. + + luiInstRef = luiRefs[(*relocDataP >> 0x15) & 0x1F]; + regValP = &luiVals[(*relocDataP >> 0x15) & 0x1F]; + + // Check address is valid for relocation + if ((((*regValP << 0x10) + (s16)*relocDataP) & 0x0F000000) == 0) { + relocOffset = ((*regValP << 0x10) + (s16)*relocDataP) - (uintptr_t)vRamStart; isLoNeg = (((relocOffset + allocu32) & 0x8000) ? 1 : 0); - unrelocatedAddress = (*luiInstRef << 0x10) + vaddr; + unrelocatedAddress = (*luiInstRef << 0x10) + (s16)relocData; *luiInstRef = (*luiInstRef & 0xFFFF0000) | ((((relocOffset + allocu32) >> 0x10) & 0xFFFF) + isLoNeg); relocatedValue = (*relocDataP & 0xFFFF0000) | ((relocOffset + allocu32) & 0xFFFF); @@ -97,16 +102,16 @@ void Overlay_Relocate(void* allocatedVRamAddress, OverlayRelocationSection* over } dbg = 0x10; - switch (reloc & 0x3F000000) { - case 0x2000000: + switch (RELOC_TYPE_MASK(reloc)) { + case R_MIPS_32 << RELOC_TYPE_SHIFT: dbg = 0x16; - case 0x4000000: + case R_MIPS_26 << RELOC_TYPE_SHIFT: dbg += 0xA; - case 0x6000000: + case R_MIPS_LO16 << RELOC_TYPE_SHIFT: if (gOverlayLogSeverity >= 3) { osSyncPrintf("%02d %08x %08x %08x ", dbg, relocDataP, relocatedValue, relocatedAddress); - osSyncPrintf(" %08x %08x %08x %08x\n", ((u32)relocDataP + (u32)vRamAddress) - allocu32, relocData, - unrelocatedAddress, relocOffset); + osSyncPrintf(" %08x %08x %08x %08x\n", (uintptr_t)relocDataP + (uintptr_t)vRamStart - allocu32, + relocData, unrelocatedAddress, relocOffset); } } } |
