summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2007-09-03 14:40:17 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2007-09-03 14:40:17 +0000
commitea72182faa98806557ba6170604b3ce6ca5c9eda (patch)
tree52f017367cab0025480befd06a8474a198fe0871 /lib
parentd5cabb42a1a17d02ebac157f110b3835dbc64959 (diff)
Add __cxa_atexit() support for gcc3. This provides support for shared object destructors called at dlclose() time. Inspired by similar changes in FreeBSD and NetBSD.
Diffstat (limited to 'lib')
-rw-r--r--lib/csu/common_elf/crtbegin.c15
-rw-r--r--lib/csu/common_elf/crtbeginS.c23
-rw-r--r--lib/libc/stdlib/abort.c9
-rw-r--r--lib/libc/stdlib/atexit.c93
-rw-r--r--lib/libc/stdlib/atexit.h14
-rw-r--r--lib/libc/stdlib/exit.c22
6 files changed, 144 insertions, 32 deletions
diff --git a/lib/csu/common_elf/crtbegin.c b/lib/csu/common_elf/crtbegin.c
index 66099e4f0b7..4c6eedcd94b 100644
--- a/lib/csu/common_elf/crtbegin.c
+++ b/lib/csu/common_elf/crtbegin.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: crtbegin.c,v 1.11 2004/10/26 20:18:24 kettenis Exp $ */
+/* $OpenBSD: crtbegin.c,v 1.12 2007/09/03 14:40:16 millert Exp $ */
/* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */
/*
@@ -59,6 +59,19 @@ void __register_frame_info(const void *begin, struct dwarf2_eh_object *ob)
static const char __EH_FRAME_BEGIN__[]
__attribute__((section(".eh_frame"), aligned(4))) = { };
+/*
+ * Include support for the __cxa_atexit/__cxa_finalize C++ abi for
+ * gcc > 2.x. __dso_handle is NULL in the main program and a unique
+ * value for each C++ shared library. For more info on this API, see:
+ *
+ * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
+ */
+
+#if (__GNUC__ > 2)
+void *__dso_handle = NULL;
+__asm(".hidden __dso_handle");
+#endif
+
static const init_f __CTOR_LIST__[1]
__attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */
static const init_f __DTOR_LIST__[1]
diff --git a/lib/csu/common_elf/crtbeginS.c b/lib/csu/common_elf/crtbeginS.c
index f8151b21e10..0874e31d2ef 100644
--- a/lib/csu/common_elf/crtbeginS.c
+++ b/lib/csu/common_elf/crtbeginS.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: crtbeginS.c,v 1.7 2004/01/26 20:04:11 espie Exp $ */
+/* $OpenBSD: crtbeginS.c,v 1.8 2007/09/03 14:40:16 millert Exp $ */
/* $NetBSD: crtbegin.c,v 1.1 1996/09/12 16:59:03 cgd Exp $ */
/*
@@ -44,6 +44,21 @@
#include "md_init.h"
#include "extern.h"
+/*
+ * Include support for the __cxa_atexit/__cxa_finalize C++ abi for
+ * gcc > 2.x. __dso_handle is NULL in the main program and a unique
+ * value for each C++ shared library. For more info on this API, see:
+ *
+ * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
+ */
+
+#if (__GNUC__ > 2)
+void *__dso_handle = &__dso_handle;
+__asm(".hidden __dso_handle");
+
+extern void __cxa_finalize(void *) __attribute__((weak));
+#endif
+
static init_f __CTOR_LIST__[1]
__attribute__((section(".ctors"))) = { (void *)-1 }; /* XXX */
static init_f __DTOR_LIST__[1]
@@ -111,6 +126,12 @@ _do_fini(void)
static int finalized = 0;
if (!finalized) {
finalized = 1;
+
+#if (__GNUC__ > 2)
+ if (__cxa_finalize != NULL)
+ __cxa_finalize(__dso_handle);
+#endif
+
/*
* since the _init() function sets up the destructors to
* be called by atexit, do not call the destructors here.
diff --git a/lib/libc/stdlib/abort.c b/lib/libc/stdlib/abort.c
index 072a9fa8c11..244e3b28aa2 100644
--- a/lib/libc/stdlib/abort.c
+++ b/lib/libc/stdlib/abort.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: abort.c,v 1.14 2005/08/08 08:05:36 espie Exp $ */
+/* $OpenBSD: abort.c,v 1.15 2007/09/03 14:40:16 millert Exp $ */
/*
* Copyright (c) 1985 Regents of the University of California.
* All rights reserved.
@@ -54,11 +54,14 @@ abort(void)
* POSIX requires we flush stdio buffers on abort
*/
if (cleanup_called == 0) {
+ /* the cleanup routine lives in fns[0] on the last page */
while (p != NULL && p->next != NULL)
p = p->next;
- if (p != NULL && p->fns[0] != NULL) {
+ /* the check for fn_dso == NULL is mostly paranoia */
+ if (p != NULL && p->fns[0].fn_dso == NULL &&
+ p->fns[0].fn_ptr.std_func != NULL) {
cleanup_called = 1;
- (*p->fns[0])();
+ (*p->fns[0].fn_ptr.std_func)();
}
}
diff --git a/lib/libc/stdlib/atexit.c b/lib/libc/stdlib/atexit.c
index 50f8ec93728..ebf5f8775c7 100644
--- a/lib/libc/stdlib/atexit.c
+++ b/lib/libc/stdlib/atexit.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: atexit.c,v 1.12 2006/02/22 07:16:32 otto Exp $ */
+/* $OpenBSD: atexit.c,v 1.13 2007/09/03 14:40:16 millert Exp $ */
/*
* Copyright (c) 2002 Daniel Hartmeier
* All rights reserved.
@@ -45,17 +45,22 @@ struct atexit *__atexit;
* function pointer in the first allocated page (the last one in
* the linked list) is reserved for the cleanup function.
*
- * Outside the following two functions, all pages are mprotect()'ed
+ * Outside the following functions, all pages are mprotect()'ed
* to prevent unintentional/malicious corruption.
*/
/*
- * Register a function to be performed at exit.
+ * Register a function to be performed at exit or when a shared object
+ * with the given dso handle is unloaded dynamically. Also used as
+ * the backend for atexit(). For more info on this API, see:
+ *
+ * http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
*/
int
-atexit(void (*fn)(void))
+__cxa_atexit(void (*func)(void *), void *arg, void *dso)
{
- struct atexit *p;
+ struct atexit *p = __atexit;
+ struct atexit_fn *fnp;
int pgsize = getpagesize();
int ret = -1;
@@ -75,7 +80,7 @@ atexit(void (*fn)(void))
if (p == MAP_FAILED)
goto unlock;
if (__atexit == NULL) {
- p->fns[0] = NULL;
+ memset(&p->fns[0], 0, sizeof(p->fns[0]));
p->ind = 1;
} else
p->ind = 0;
@@ -86,7 +91,10 @@ atexit(void (*fn)(void))
if (__atexit_invalid)
__atexit_invalid = 0;
}
- p->fns[p->ind++] = fn;
+ fnp = &p->fns[p->ind++];
+ fnp->fn_ptr.cxa_func = func;
+ fnp->fn_arg = arg;
+ fnp->fn_dso = dso;
if (mprotect(p, pgsize, PROT_READ))
goto unlock;
ret = 0;
@@ -96,10 +104,75 @@ unlock:
}
/*
+ * Register a function to be performed at exit.
+ */
+int
+atexit(void (*func)(void))
+{
+ return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
+}
+
+/*
+ * Call all handlers registered with __cxa_atexit() for the shared
+ * object owning 'dso'.
+ * Note: if 'dso' is NULL, then all remaining handlers are called.
+ */
+void
+__cxa_finalize(void *dso)
+{
+ struct atexit *p, *q;
+ struct atexit_fn fn;
+ int n, pgsize = getpagesize();
+ static int call_depth;
+
+ if (__atexit_invalid)
+ return;
+
+ call_depth++;
+
+ for (p = __atexit; p != NULL; p = p->next) {
+ for (n = p->ind; --n >= 0;) {
+ if (p->fns[n].fn_ptr.cxa_func == NULL)
+ continue; /* already called */
+ if (dso != NULL && dso != p->fns[n].fn_dso)
+ continue; /* wrong DSO */
+
+ /*
+ * Mark handler as having been already called to avoid
+ * dupes and loops, then call the appropriate function.
+ */
+ fn = p->fns[n];
+ if (mprotect(p, pgsize, PROT_READ | PROT_WRITE) == 0) {
+ p->fns[n].fn_ptr.cxa_func = NULL;
+ mprotect(p, pgsize, PROT_READ);
+ }
+ if (dso != NULL)
+ (*fn.fn_ptr.cxa_func)(fn.fn_arg);
+ else
+ (*fn.fn_ptr.std_func)();
+ }
+ }
+
+ /*
+ * If called via exit(), unmap the pages since we have now run
+ * all the handlers. We defer this until calldepth == 0 so that
+ * we don't unmap things prematurely if called recursively.
+ */
+ if (dso == NULL && --call_depth == 0) {
+ for (p = __atexit; p != NULL; ) {
+ q = p;
+ p = p->next;
+ munmap(q, pgsize);
+ }
+ __atexit = NULL;
+ }
+}
+
+/*
* Register the cleanup function
*/
void
-__atexit_register_cleanup(void (*fn)(void))
+__atexit_register_cleanup(void (*func)(void))
{
struct atexit *p;
int pgsize = getpagesize();
@@ -126,7 +199,9 @@ __atexit_register_cleanup(void (*fn)(void))
if (mprotect(p, pgsize, PROT_READ | PROT_WRITE))
goto unlock;
}
- p->fns[0] = fn;
+ p->fns[0].fn_ptr.std_func = func;
+ p->fns[0].fn_arg = NULL;
+ p->fns[0].fn_dso = NULL;
mprotect(p, pgsize, PROT_READ);
unlock:
_ATEXIT_UNLOCK();
diff --git a/lib/libc/stdlib/atexit.h b/lib/libc/stdlib/atexit.h
index 21b0c2e5327..1b23565dd06 100644
--- a/lib/libc/stdlib/atexit.h
+++ b/lib/libc/stdlib/atexit.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: atexit.h,v 1.6 2003/07/31 07:08:42 deraadt Exp $ */
+/* $OpenBSD: atexit.h,v 1.7 2007/09/03 14:40:16 millert Exp $ */
/*
* Copyright (c) 2002 Daniel Hartmeier
@@ -34,8 +34,18 @@ struct atexit {
struct atexit *next; /* next in list */
int ind; /* next index in this table */
int max; /* max entries >= ATEXIT_SIZE */
- void (*fns[1])(void); /* the table itself */
+ struct atexit_fn {
+ union {
+ void (*std_func)(void);
+ void (*cxa_func)(void *);
+ } fn_ptr;
+ void *fn_arg; /* argument for CXA callback */
+ void *fn_dso; /* shared module handle */
+ } fns[1]; /* the table itself */
};
extern int __atexit_invalid;
extern struct atexit *__atexit; /* points to head of LIFO stack */
+
+int __cxa_atexit(void (*)(void *), void *, void *);
+void __cxa_finalize(void *);
diff --git a/lib/libc/stdlib/exit.c b/lib/libc/stdlib/exit.c
index 90b7d5adc2d..83fe3d2de56 100644
--- a/lib/libc/stdlib/exit.c
+++ b/lib/libc/stdlib/exit.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: exit.c,v 1.11 2005/08/08 08:05:36 espie Exp $ */
+/* $OpenBSD: exit.c,v 1.12 2007/09/03 14:40:16 millert Exp $ */
/*-
* Copyright (c) 1990 The Regents of the University of California.
* All rights reserved.
@@ -50,20 +50,10 @@ int __isthreaded = 0;
void
exit(int status)
{
- struct atexit *p, *q;
- int n, pgsize = getpagesize();
-
- if (!__atexit_invalid) {
- p = __atexit;
- while (p != NULL) {
- for (n = p->ind; --n >= 0;)
- if (p->fns[n] != NULL)
- (*p->fns[n])();
- q = p;
- p = p->next;
- munmap(q, pgsize);
- }
- }
- /* cleanup, if registered, was called through fns[0] in the last page */
+ /*
+ * Call functions registered by atexit() or _cxa_atexit()
+ * (including the stdio cleanup routine) and then _exit().
+ */
+ __cxa_finalize(NULL);
_exit(status);
}