Disassemble getReturnAddress

This commit is contained in:
Ryan Dwyer 2021-10-07 21:23:59 +10:00
parent eea8eaa77d
commit f54a8fc3f0
4 changed files with 47 additions and 28 deletions

View File

@ -34,6 +34,7 @@
build/ROMID/lib/lib_09a80.o (section); \
build/ROMID/lib/lib_0bfb0.o (section); \
build/ROMID/lib/lib_0c000.o (section); \
build/ROMID/lib/getra.o (section); \
build/ROMID/lib/dma.o (section); \
build/ROMID/lib/main.o (section); \
build/ROMID/lib/snd.o (section); \

45
src/lib/getra.s Normal file
View File

@ -0,0 +1,45 @@
#include "macros.h"
.set noat
.set noreorder
/**
* Get the return address ($ra) that the caller would return to.
*
* For example, a C function could use:
*
* osSyncPrintf("Called from %u\n", getReturnAddress());
*
* It follows the current $ra to get to the function that called
* getReturnAddress, then steps backwards through that function's bytecode until
* it finds either `addiu $sp` or `sw $ra, 0xnn($sp)`.
*
* If it finds `addiu $sp` first then it's reached the top of the function and
* $ra wasn't saved to the stack. In this case something is massively wrong and
* the function returns -1.
*
* If it finds `sw $ra, 0xnn($sp)` first then it reads the stack offset out of
* the instruction, then reads the value out of the stack using that offset.
*/
glabel getReturnAddress
or $a0, $ra, $zero
addiu $v0, $zero, -1
.loop:
lw $t0, 0($a0)
addiu $t2, $zero, 0x27bd
srl $t1, $t0, 16
beq $t1, $t2, .foundtop
nop
dli $t2, 0xafbf
beql $t1, $t2, .foundstore
sll $t2, $t0, 16
j .loop
addiu $a0, $a0, -4
sll $t2, $t0, 16
.foundstore:
sra $t2, $t2, 16
add $t2, $t2, $sp
lw $v0, 0($t2)
.foundtop:
jr $ra
nop

View File

@ -1546,31 +1546,3 @@ void func0000cf54(u16 *fb)
}
}
}
#if VERSION < VERSION_NTSC_1_0
GLOBAL_ASM(
glabel func0000d678nb
/* d678: 00000000 */ sll $zero,$zero,0x0
/* d67c: 00000000 */ sll $zero,$zero,0x0
/* d680: 03e02025 */ or $a0,$ra,$zero
/* d684: 2402ffff */ addiu $v0,$zero,-1
/* d688: 8c880000 */ lw $t0,0x0($a0)
/* d68c: 240a27bd */ addiu $t2,$zero,0x27bd
/* d690: 00084c02 */ srl $t1,$t0,0x10
/* d694: 112a000a */ beq $t1,$t2,.NB0000d6c0
/* d698: 00000000 */ sll $zero,$zero,0x0
/* d69c: 340aafbf */ dli $t2,0xafbf
/* d6a0: 512a0004 */ beql $t1,$t2,.NB0000d6b4
/* d6a4: 00085400 */ sll $t2,$t0,0x10
/* d6a8: 080035a2 */ j 0xd688
/* d6ac: 2484fffc */ addiu $a0,$a0,-4
/* d6b0: 00085400 */ sll $t2,$t0,0x10
.NB0000d6b4:
/* d6b4: 000a5403 */ sra $t2,$t2,0x10
/* d6b8: 015d5020 */ add $t2,$t2,$sp
/* d6bc: 8d420000 */ lw $v0,0x0($t2)
.NB0000d6c0:
/* d6c0: 03e00008 */ jr $ra
/* d6c4: 00000000 */ sll $zero,$zero,0x0
);
#endif

View File

@ -1,3 +1,4 @@
#include "versions.h"
#include "macros.inc"
.set noat
.set noreorder