From e5e6b6c49f1fbca0b3fd9b58e98fa25116d9e438 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Wed, 5 Nov 2014 16:26:18 +0000 Subject: * lib/atexit.c (atexit): Check for being linked into the executable. If so, call __cxa_atexit with NULL DSO handle. Explain why. * lib/dso_handle.c: New file providing fallback __dso_handle. --- winsup/cygwin/lib/atexit.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'winsup/cygwin/lib/atexit.c') diff --git a/winsup/cygwin/lib/atexit.c b/winsup/cygwin/lib/atexit.c index 0b724771a..af82d1de7 100644 --- a/winsup/cygwin/lib/atexit.c +++ b/winsup/cygwin/lib/atexit.c @@ -9,6 +9,7 @@ Cygwin license. Please consult the file "CYGWIN_LICENSE" for details. */ #include +#include /* Statically linked replacement for the former cygwin_atexit. We need the function here to be able to access the correct __dso_handle of the @@ -18,6 +19,16 @@ atexit (void (*fn) (void)) { extern int __cxa_atexit(void (*)(void*), void*, void*); extern void *__dso_handle; + extern void *__ImageBase; - return __cxa_atexit ((void (*)(void*))fn, NULL, &__dso_handle); + /* Check for being called from inside the executable. If so, use NULL + as __dso_handle. This allows to link executables with GCC versions + not providing __dso_handle in crtbegin{S}.o. In this case our own + __dso_handle defined in lib/dso_handle.c is used. However, our + __dso_handle always points to &__ImageBase, while the __dso_handle + for executables provided by crtbegin.o usually points to NULL. + That's what we remodel here. */ + return __cxa_atexit ((void (*)(void*))fn, NULL, + &__ImageBase == (void **) GetModuleHandleW (NULL) + ? NULL : &__dso_handle); } -- cgit v1.2.3