From 12ee6e502e357529ccbd4f99e9627ca1b501e5ef Mon Sep 17 00:00:00 2001 From: yellows8 Date: Wed, 14 Nov 2018 20:55:17 -0500 Subject: [PATCH] Fixed register corruption in crt0 introduced by the exception commit. Only handle exceptions when __libnx_exception_handler is defined by the user, and updated comments. --- nx/source/runtime/exception.s | 8 ++++++++ nx/source/runtime/init.c | 9 ++++----- nx/source/runtime/switch_crt0.s | 10 +++++++--- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/nx/source/runtime/exception.s b/nx/source/runtime/exception.s index e7f6a522..54cc4d0e 100644 --- a/nx/source/runtime/exception.s +++ b/nx/source/runtime/exception.s @@ -13,11 +13,19 @@ // Called by crt0 when the args at the time of entry indicate an exception occured. +.weak __libnx_exception_handler + .weak __libnx_exception_entry CODE_BEGIN __libnx_exception_entry cmp x1, #0 beq __libnx_exception_entry_abort + // Abort exception handling when __libnx_exception_handler is not defined. + adrp x5, :got:__libnx_exception_handler + ldr x5, [x5, #:got_lo12:__libnx_exception_handler] + cmp x5, #0 + beq __libnx_exception_entry_abort + // Load IsCurrentProcessBeingDebugged. stp x9, x10, [sp, #-16]! diff --git a/nx/source/runtime/init.c b/nx/source/runtime/init.c index 0a4ea64f..d214c177 100644 --- a/nx/source/runtime/init.c +++ b/nx/source/runtime/init.c @@ -21,12 +21,15 @@ extern u32 __nx_applet_type; // Must be a multiple of 0x200000. __attribute__((weak)) size_t __nx_heap_size = 0; -/// Override these with your own if you're using __libnx_exception_handler. __nx_exception_stack is the stack-bottom. +/// Override these with your own if you're using \ref__libnx_exception_handler. __nx_exception_stack is the stack-bottom. Update \ref __nx_exception_stack_size if you change this. __attribute__((weak)) alignas(16) u8 __nx_exception_stack[0x400]; __attribute__((weak)) u64 __nx_exception_stack_size = sizeof(__nx_exception_stack); /// By default exception handling will be aborted when the current process is being debugged. Set this to non-zero to disable that. __attribute__((weak)) u32 __nx_exception_ignoredebug = 0; +/// Override this with your own func to handle exceptions. See \ref __nx_exception_stack. See here: https://switchbrew.org/wiki/SVC#Exception_handling +void __attribute__((weak)) __libnx_exception_handler(ThreadExceptionDump *ctx); + ThreadExceptionDump __nx_exceptiondump; /* @@ -175,7 +178,3 @@ void __attribute__((weak)) NORETURN __libnx_exit(int rc) __nx_exit(0, envGetExitFuncPtr()); } -/// You can override this with your own func to handle exceptions. See here: https://switchbrew.org/wiki/SVC#Exception_handling -void __attribute__((weak)) __libnx_exception_handler(ThreadExceptionDump *ctx) -{ -} diff --git a/nx/source/runtime/switch_crt0.s b/nx/source/runtime/switch_crt0.s index c06df8fb..4f6f1fb2 100644 --- a/nx/source/runtime/switch_crt0.s +++ b/nx/source/runtime/switch_crt0.s @@ -27,6 +27,10 @@ startup: b __libnx_exception_entry bssclr_start: + mov x27, x7 + mov x25, x5 + mov x26, x4 + // clear .bss adrp x0, __bss_start__ adrp x1, __bss_end__ @@ -53,9 +57,9 @@ bss_loop: bl __nx_dynamic // initialize system - mov x0, x5 - mov x1, x4 - mov x2, x7 + mov x0, x25 + mov x1, x26 + mov x2, x27 bl __libnx_init // call entrypoint