[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.
This commit is contained in:
Ash 2017-12-01 18:18:02 +11:00
parent f48460b114
commit 26a006cfac
No known key found for this signature in database
GPG Key ID: F8C85A8C836C0A7E

View File

@ -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;
}