Skip to content

Commit 0c0455b

Browse files
authored
Merge pull request #2745 from pan-/disable_global_objects_destruction
Disable global objects destruction
2 parents c424913 + f4f8b49 commit 0c0455b

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

hal/common/retarget.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,64 @@ extern "C" void exit(int return_code) {
629629
} //namespace std
630630
#endif
631631

632+
#if defined(TOOLCHAIN_ARM) || defined(TOOLCHAIN_GCC)
633+
634+
// This series of function disable the registration of global destructors
635+
// in a dynamic table which will be called when the application exit.
636+
// In mbed, program never exit properly, it dies.
637+
// More informations about this topic for ARMCC here:
638+
// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/6449.html
639+
extern "C" {
640+
int __aeabi_atexit(void *object, void (*dtor)(void* /*this*/), void *handle) {
641+
return 1;
642+
}
643+
644+
int __cxa_atexit(void (*dtor)(void* /*this*/), void *object, void *handle) {
645+
return 1;
646+
}
647+
648+
void __cxa_finalize(void *handle) {
649+
}
650+
651+
} // end of extern "C"
652+
653+
#endif
654+
655+
656+
#if defined(TOOLCHAIN_GCC)
657+
658+
/*
659+
* Depending on how newlib is configured, it is often not enough to define
660+
* __aeabi_atexit, __cxa_atexit and __cxa_finalize in order to override the
661+
* behavior regarding the registration of handlers with atexit.
662+
*
663+
* To overcome this limitation, exit and atexit are overriden here.
664+
*/
665+
extern "C"{
666+
667+
/**
668+
* @brief Retarget of exit for GCC.
669+
* @details Unlike the standard version, this function doesn't call any function
670+
* registered with atexit before calling _exit.
671+
*/
672+
void __wrap_exit(int return_code) {
673+
_exit(return_code);
674+
}
675+
676+
/**
677+
* @brief Retarget atexit from GCC.
678+
* @details This function will always fail and never register any handler to be
679+
* called at exit.
680+
*/
681+
int __wrap_atexit(void (*func)()) {
682+
return 1;
683+
}
684+
685+
}
686+
687+
#endif
688+
689+
632690

633691
namespace mbed {
634692

rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,7 +796,6 @@ void pre_main(void) {
796796
singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
797797
malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
798798
env_mutex_id = osMutexCreate(osMutex(env_mutex));
799-
atexit(__libc_fini_array);
800799
__libc_init_array();
801800
main(0, NULL);
802801
}

tools/toolchains/gcc.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ class GCC(mbedToolchain):
4242
'c': ["-std=gnu99"],
4343
'cxx': ["-std=gnu++98", "-fno-rtti", "-Wvla"],
4444
'ld': ["-Wl,--gc-sections", "-Wl,--wrap,main",
45-
"-Wl,--wrap,_malloc_r", "-Wl,--wrap,_free_r", "-Wl,--wrap,_realloc_r", "-Wl,--wrap,_calloc_r"],
45+
"-Wl,--wrap,_malloc_r", "-Wl,--wrap,_free_r", "-Wl,--wrap,_realloc_r", "-Wl,--wrap,_calloc_r",
46+
"-Wl,--wrap,exit", "-Wl,--wrap,atexit"],
4647
}
4748

4849
def __init__(self, target, options=None, notify=None, macros=None, silent=False, tool_path="", extra_verbose=False):

tools/toolchains/iar.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class IAR(mbedToolchain):
4343
"--diag_suppress=Pa050,Pa084,Pa093,Pa082"],
4444
'asm': [],
4545
'c': ["--vla"],
46-
'cxx': ["--guard_calls"],
46+
'cxx': ["--guard_calls", "--no_static_destruction"],
4747
'ld': ["--skip_dynamic_initialization", "--threaded_lib"],
4848
}
4949

0 commit comments

Comments
 (0)