From 26a006cfac4e044fab5062b7baeed12ef982f68c Mon Sep 17 00:00:00 2001 From: Ash Date: Fri, 1 Dec 2017 18:18:02 +1100 Subject: [PATCH] [WiiU] Toolchain: Fix C++ constructor/destructor handling The old setup relied on there being at least one constructor *or* the value of *__CTOR_LIST__ being NULL. Neither of these are guaranteed; and having no C++ constructors actually resulted in a random value being read (which passed the NULL check!). This new setup uses the __CTOR_END__ symbol; which is a pointer to just after the end of the list. When there are no constructors, it has the same value as __CTOR_LIST__; so the while loop is never entered. This fix also allows us to re-enable destructors; in case they're ever needed. --- frontend/drivers/platform_wiiu.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/frontend/drivers/platform_wiiu.c b/frontend/drivers/platform_wiiu.c index c97ec97139..c975df3b16 100644 --- a/frontend/drivers/platform_wiiu.c +++ b/frontend/drivers/platform_wiiu.c @@ -523,22 +523,26 @@ void __eabi() __attribute__((weak)) void __init(void) { - extern void(*__CTOR_LIST__[])(void); - void(**ctor)(void) = __CTOR_LIST__; + extern void (**const __CTOR_LIST__)(void); + extern void (**const __CTOR_END__)(void); - while (*ctor) + void (**ctor)(void) = __CTOR_LIST__; + while (ctor < __CTOR_END__) { (*ctor++)(); + } } __attribute__((weak)) void __fini(void) { - extern void(*__DTOR_LIST__[])(void); - void(**ctor)(void) = __DTOR_LIST__; + extern void (**const __DTOR_LIST__)(void); + extern void (**const __DTOR_END__)(void); - while (*ctor) - (*ctor++)(); + void (**dtor)(void) = __DTOR_LIST__; + while (dtor < __DTOR_END__) { + (*dtor++)(); + } } /* libiosuhax related */ @@ -633,7 +637,7 @@ int __entry_menu(int argc, char **argv) int ret = main(argc, argv); fsdev_exit(); -// __fini(); + __fini(); memoryRelease(); return ret; }