aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxetc/_dl_resolve_wrapper.S
diff options
context:
space:
mode:
Diffstat (limited to 'libpsn00b/psxetc/_dl_resolve_wrapper.S')
-rw-r--r--libpsn00b/psxetc/_dl_resolve_wrapper.S48
1 files changed, 48 insertions, 0 deletions
diff --git a/libpsn00b/psxetc/_dl_resolve_wrapper.S b/libpsn00b/psxetc/_dl_resolve_wrapper.S
new file mode 100644
index 0000000..b715720
--- /dev/null
+++ b/libpsn00b/psxetc/_dl_resolve_wrapper.S
@@ -0,0 +1,48 @@
+# PSn00bSDK dynamic linker
+# (C) 2021-2022 spicyjpeg - MPL licensed
+#
+# This function is called by the lazy loader stubs generated by GCC in the
+# .plt/.MIPS.stubs section when attempting to call a GOT entry whose address
+# hasn't yet been resolved. The generated stubs conform to the MIPS ABI and
+# uses the following registers:
+# - $t7 = address the resolved function should return to (i.e. $ra of the
+# caller that triggered the stub)
+# - $t8 = index of the function in the .dynsym symbol table
+# - $t9 = _dl_resolve_wrapper itself's address
+
+.set noreorder
+
+.section .text._dl_resolve_wrapper, "ax", @progbits
+.global _dl_resolve_wrapper
+.type _dl_resolve_wrapper, @function
+
+_dl_resolve_wrapper:
+ # Save the arguments being passed to the function to be resolved.
+ addiu $sp, -20
+ sw $a0, 0($sp)
+ sw $a1, 4($sp)
+ sw $a2, 8($sp)
+ sw $a3, 12($sp)
+ sw $t7, 16($sp) # (will be restored directly to $ra)
+
+ # Figure out where the DLL's struct is. dlinit() places a pointer to the
+ # struct in the second GOT entry, so it's just a matter of indexing the GOT
+ # using $gp. Then call _dl_resolve_helper with the struct and $t8 as
+ # arguments, and store the return value into $t0.
+ lw $a0, -0x7fec($gp) # dll = &((uint32_t *) (gp - 0x7ff0))[1]
+ move $a1, $t8
+
+ jal _dl_resolve_helper
+ addiu $sp, -8
+ addiu $sp, 8
+
+ # Restore the arguments from the stack and tail-call the function at the
+ # address returned by the resolver.
+ lw $a0, 0($sp)
+ lw $a1, 4($sp)
+ lw $a2, 8($sp)
+ lw $a3, 12($sp)
+ lw $ra, 16($sp)
+
+ jr $v0
+ addiu $sp, 20