From ba03bb558149a592831215f1402d7f4e8fdb977e Mon Sep 17 00:00:00 2001 From: Francisco Javier Trujillo Mata Date: Thu, 18 Jul 2024 09:26:25 +0200 Subject: [PATCH] Improve exit flow --- src/libcglue/Makefile.am | 2 -- src/libcglue/init.c | 31 +------------------------------ src/startup/crt0.c | 31 +++++++++++++++++++++++++------ src/startup/crt0_prx.c | 31 +++++++++++++++++++++++++------ 4 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/libcglue/Makefile.am b/src/libcglue/Makefile.am index 4f3c59a9..4aeab90e 100755 --- a/src/libcglue/Makefile.am +++ b/src/libcglue/Makefile.am @@ -37,8 +37,6 @@ INIT_OBJS = \ __libcglue_init.o \ __gprof_cleanup.o \ __libcglue_deinit.o \ - _exit.o \ - abort.o \ exit.o LOCK_OBJS = \ diff --git a/src/libcglue/init.c b/src/libcglue/init.c index 6dd43fbd..a97bc833 100644 --- a/src/libcglue/init.c +++ b/src/libcglue/init.c @@ -29,8 +29,6 @@ void __deinit_mutex(); void __locks_init(); void __locks_deinit(); -extern int sce_newlib_nocreate_thread_in_start __attribute__((weak)); - #ifdef F___gprof_cleanup /* Note: This function is being called from _exit and it is overrided when compiling with -pg */ __attribute__((weak)) @@ -91,6 +89,7 @@ void __libcglue_init(int argc, char *argv[]) __attribute__((weak)) void __libcglue_deinit() { + __gprof_cleanup(); __psp_free_heap(); __deinit_mutex(); __locks_deinit(); @@ -99,34 +98,6 @@ void __libcglue_deinit() void __libcglue_deinit(); #endif -#ifdef F__exit -__attribute__((__noreturn__)) -void _exit (int __status) -{ - __gprof_cleanup(); - __libcglue_deinit(); - - if (&sce_newlib_nocreate_thread_in_start != NULL) { - sceKernelSelfStopUnloadModule(1, 0, NULL); - } else { - sceKernelExitGame(); - } - - while (1); // Avoid warning -} -#else -__attribute__((__noreturn__)) -void _exit (int __status); -#endif - -#ifdef F_abort -__attribute__((weak)) -void abort() -{ - _exit(1); -} -#endif - #ifdef F_exit __attribute__((weak)) void exit(int retval) diff --git a/src/startup/crt0.c b/src/startup/crt0.c index 38a6f6f5..53eb00c3 100644 --- a/src/startup/crt0.c +++ b/src/startup/crt0.c @@ -9,11 +9,14 @@ * */ +#include +#include + #include #include #include -#include -#include +#include +#include /* The maximum number of arguments that can be passed to main(). */ #define ARG_MAX 19 @@ -39,6 +42,7 @@ extern SceModuleInfo module_info __attribute__((weak)); /* Allow to provide a libc init hook to be called before main */ extern void __libcglue_init(int argc, char *argv[]); +extern void __libcglue_deinit(); extern void _init(void); extern void _fini(void); extern int main(int argc, char *argv[]); @@ -78,16 +82,31 @@ void _main(SceSize args, void *argp) /* Init can contain C++ constructors that require working threading */ _init(); - /* Make sure _fini() is called when the program ends. */ - atexit((void *) _fini); - /* Call main(). */ int res = main(argc, argv); - /* Return control to the operating system. */ + /* Return control to the operating system. This will end calling `_exit`*/ exit(res); } +__attribute__((__noreturn__)) +void _exit(int status) +{ + /* call global destructors*/ + _fini(); + + // uninitialize libcglue + __libcglue_deinit(); + + if (&sce_newlib_nocreate_thread_in_start != NULL) { + sceKernelSelfStopUnloadModule(1, 0, NULL); + } else { + sceKernelExitGame(); + } + + while (1); // Avoid warning +} + /** * Startup thread * diff --git a/src/startup/crt0_prx.c b/src/startup/crt0_prx.c index f4d1faf5..4e7437e8 100644 --- a/src/startup/crt0_prx.c +++ b/src/startup/crt0_prx.c @@ -9,11 +9,14 @@ * */ +#include +#include + #include #include #include -#include -#include +#include +#include /* The maximum number of arguments that can be passed to main(). */ #define ARG_MAX 19 @@ -39,6 +42,7 @@ extern SceModuleInfo module_info __attribute__((weak)); /* Allow to provide a libc init hook to be called before main */ extern void __libcglue_init(int argc, char *argv[]); +extern void __libcglue_deinit(); extern void _init(void); extern void _fini(void); extern int main(int argc, char *argv[]); @@ -78,16 +82,31 @@ void _main(SceSize args, void *argp) /* Init can contain C++ constructors that require working threading */ _init(); - /* Make sure _fini() is called when the program ends. */ - atexit((void *) _fini); - /* Call main(). */ int res = main(argc, argv); - /* Return control to the operating system. */ + /* Return control to the operating system. This will end calling `_exit`*/ exit(res); } +__attribute__((__noreturn__)) +void _exit(int status) +{ + /* call global destructors*/ + _fini(); + + // uninitialize libcglue + __libcglue_deinit(); + + if (&sce_newlib_nocreate_thread_in_start != NULL) { + sceKernelSelfStopUnloadModule(1, 0, NULL); + } else { + sceKernelExitGame(); + } + + while (1); // Avoid warning +} + int module_start(SceSize args, void *argp) __attribute__((alias("_start"))); /**