Skip to content

Disable global objects destruction #2745

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions hal/common/retarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,64 @@ extern "C" void exit(int return_code) {
} //namespace std
#endif

#if defined(TOOLCHAIN_ARM) || defined(TOOLCHAIN_GCC)

// This series of function disable the registration of global destructors
// in a dynamic table which will be called when the application exit.
// In mbed, program never exit properly, it dies.
// More informations about this topic for ARMCC here:
// http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/6449.html
extern "C" {
int __aeabi_atexit(void *object, void (*dtor)(void* /*this*/), void *handle) {
return 1;
}

int __cxa_atexit(void (*dtor)(void* /*this*/), void *object, void *handle) {
return 1;
}

void __cxa_finalize(void *handle) {
}

} // end of extern "C"

#endif


#if defined(TOOLCHAIN_GCC)

/*
* Depending on how newlib is configured, it is often not enough to define
* __aeabi_atexit, __cxa_atexit and __cxa_finalize in order to override the
* behavior regarding the registration of handlers with atexit.
*
* To overcome this limitation, exit and atexit are overriden here.
*/
extern "C"{

/**
* @brief Retarget of exit for GCC.
* @details Unlike the standard version, this function doesn't call any function
* registered with atexit before calling _exit.
*/
void __wrap_exit(int return_code) {
_exit(return_code);
}

/**
* @brief Retarget atexit from GCC.
* @details This function will always fail and never register any handler to be
* called at exit.
*/
int __wrap_atexit(void (*func)()) {
return 1;
}

}

#endif



namespace mbed {

Expand Down
1 change: 0 additions & 1 deletion rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,6 @@ void pre_main(void) {
singleton_mutex_id = osMutexCreate(osMutex(singleton_mutex));
malloc_mutex_id = osMutexCreate(osMutex(malloc_mutex));
env_mutex_id = osMutexCreate(osMutex(env_mutex));
atexit(__libc_fini_array);
__libc_init_array();
main(0, NULL);
}
Expand Down
3 changes: 2 additions & 1 deletion tools/toolchains/gcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class GCC(mbedToolchain):
'c': ["-std=gnu99"],
'cxx': ["-std=gnu++98", "-fno-rtti", "-Wvla"],
'ld': ["-Wl,--gc-sections", "-Wl,--wrap,main",
"-Wl,--wrap,_malloc_r", "-Wl,--wrap,_free_r", "-Wl,--wrap,_realloc_r", "-Wl,--wrap,_calloc_r"],
"-Wl,--wrap,_malloc_r", "-Wl,--wrap,_free_r", "-Wl,--wrap,_realloc_r", "-Wl,--wrap,_calloc_r",
"-Wl,--wrap,exit", "-Wl,--wrap,atexit"],
}

def __init__(self, target, options=None, notify=None, macros=None, silent=False, tool_path="", extra_verbose=False):
Expand Down
2 changes: 1 addition & 1 deletion tools/toolchains/iar.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class IAR(mbedToolchain):
"--diag_suppress=Pa050,Pa084,Pa093,Pa082"],
'asm': [],
'c': ["--vla"],
'cxx': ["--guard_calls"],
'cxx': ["--guard_calls", "--no_static_destruction"],
'ld': ["--skip_dynamic_initialization", "--threaded_lib"],
}

Expand Down