Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/stdlib Rewrite atexit(3), and add support for __cxa...



details:   https://anonhg.NetBSD.org/src/rev/f3ad41ff080b
branches:  trunk
changeset: 543611:f3ad41ff080b
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sat Mar 01 04:19:37 2003 +0000

description:
Rewrite atexit(3), and add support for __cxa_atexit()/__cxa_finalize(),
as specified by the C++ ABI for Itanium.  Despite the ABI document's name,
many of the items in it are implemented for other architectures.

See the following URL for a reference:

        http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor

Fixes lib/18379 (from Joel Baker).

diffstat:

 lib/libc/stdlib/atexit.c |  221 +++++++++++++++++++++++++++++++++++-----------
 lib/libc/stdlib/atexit.h |   64 +++++--------
 lib/libc/stdlib/exit.c   |   14 +--
 3 files changed, 194 insertions(+), 105 deletions(-)

diffs (truncated from 368 to 300 lines):

diff -r 6c72a5bed0e7 -r f3ad41ff080b lib/libc/stdlib/atexit.c
--- a/lib/libc/stdlib/atexit.c  Sat Mar 01 01:46:02 2003 +0000
+++ b/lib/libc/stdlib/atexit.c  Sat Mar 01 04:19:37 2003 +0000
@@ -1,11 +1,11 @@
-/*     $NetBSD: atexit.c,v 1.13 2003/01/18 11:32:03 thorpej Exp $      */
+/*     $NetBSD: atexit.c,v 1.14 2003/03/01 04:19:37 thorpej Exp $      */
 
 /*-
- * Copyright (c) 1990, 1993
- *     The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 2003 The NetBSD Foundation, Inc.
+ * All rights reserved.
  *
- * This code is derived from software contributed to Berkeley by
- * Chris Torek.
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -17,71 +17,182 @@
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include <sys/cdefs.h>
-#if defined(LIBC_SCCS) && !defined(lint)
-#if 0
-static char sccsid[] = "@(#)atexit.c   8.2 (Berkeley) 7/3/94";
-#else
-__RCSID("$NetBSD: atexit.c,v 1.13 2003/01/18 11:32:03 thorpej Exp $");
-#endif
-#endif /* LIBC_SCCS and not lint */
+#include "reentrant.h"
 
 #include <assert.h>
-#include <errno.h>
 #include <stdlib.h>
-#include "reentrant.h"
+
 #include "atexit.h"
 
-struct atexit *__atexit;       /* points to head of LIFO stack */
+struct atexit_handler {
+       struct atexit_handler *ah_next;
+       union {
+               void (*fun_atexit)(void);
+               void (*fun_cxa_atexit)(void *);
+       } ah_fun;
+#define        ah_atexit       ah_fun.fun_atexit
+#define        ah_cxa_atexit   ah_fun.fun_cxa_atexit
+
+       void *ah_arg;   /* argument for cxa_atexit handlers */
+       void *ah_dso;   /* home DSO for cxa_atexit handlers */
+};
+
+/*
+ * There must be at least 32 to guarantee ANSI conformance, plus
+ * 3 additional ones for the benefit of the startup code, which
+ * may use them to register the dynamic loader's cleanup routine,
+ * the profiling cleanup routine, and the global destructor routine.
+ */
+#define        NSTATIC_HANDLERS        (32 + 3)
+static struct atexit_handler atexit_handler0[NSTATIC_HANDLERS];
+
+#define        STATIC_HANDLER_P(ah)                                            \
+       (ah >= &atexit_handler0[0] && ah < &atexit_handler0[NSTATIC_HANDLERS])
+
+/*
+ * Stack of atexit handlers.  Handlers must be called in the opposite
+ * order they were registered.
+ */
+static struct atexit_handler *atexit_handler_stack;
 
 #ifdef _REENTRANT
-mutex_t __atexit_mutex = MUTEX_INITIALIZER;
-#endif
+/* ..and a mutex to protect it all. */
+static mutex_t atexit_mutex = MUTEX_INITIALIZER;
+#endif /* _REENTRANT */
+
+/*
+ * Allocate an atexit handler descriptor.  If "dso" is NULL, it indicates
+ * a normal atexit handler, which must be allocated from the static pool,
+ * if possible. cxa_atexit handlers are never allocated from the static
+ * pool.
+ *
+ * atexit_mutex must be held.
+ */
+static struct atexit_handler *
+atexit_handler_alloc(void *dso)
+{
+       struct atexit_handler *ah;
+       int i;
+
+       if (dso == NULL) {
+               for (i = 0; i < NSTATIC_HANDLERS; i++) {
+                       ah = &atexit_handler0[i];
+                       if (ah->ah_atexit == NULL) {
+                               /* Slot is free. */
+                               return (ah);
+                       }
+               }
+       }
+
+       /*
+        * Either no static slot was free, or this is a cxa_atexit
+        * handler.  Allocate a new one.  We keep the atexit_mutex
+        * held to prevent handlers from being run while we (potentially)
+        * block in malloc().
+        */
+       ah = malloc(sizeof(*ah));
+       return (ah);
+}
+
+/*
+ * Register an atexit routine.  This is suitable either for a cxa_atexit
+ * or normal atexit type handler.  The __cxa_atexit() name and arguments
+ * are specified by the C++ ABI.  See:
+ *
+ *     http://www.codesourcery.com/cxx-abi/abi.html#dso-dtor
+ */
+int
+__cxa_atexit(void (*func)(void *), void *arg, void *dso)
+{
+       struct atexit_handler *ah;
+
+       _DIAGASSERT(func != NULL);
+
+       mutex_lock(&atexit_mutex);
+
+       ah = atexit_handler_alloc(dso);
+       if (ah == NULL) {
+               mutex_unlock(&atexit_mutex);
+               return (-1);
+       }
+
+       ah->ah_cxa_atexit = func;
+       ah->ah_arg = arg;
+       ah->ah_dso = dso;
+
+       ah->ah_next = atexit_handler_stack;
+       atexit_handler_stack = ah;
+
+       mutex_unlock(&atexit_mutex);
+       return (0);
+}
+
+/*
+ * Run the list of atexit handlers.  If dso is NULL, run all of them,
+ * otherwise run only those matching the specified dso.
+ */
+void
+__cxa_finalize(void *dso)
+{
+       struct atexit_handler *ah, *dead_handlers = NULL, **prevp;
+
+       mutex_lock(&atexit_mutex);
+
+       for (prevp = &atexit_handler_stack; (ah = (*prevp)) != NULL;) {
+               if (dso == NULL || dso == ah->ah_dso) {
+                       if (ah->ah_dso != NULL)
+                               (*ah->ah_cxa_atexit)(ah->ah_arg);
+                       else
+                               (*ah->ah_atexit)();
+
+                       *prevp = ah->ah_next;
+                       if (STATIC_HANDLER_P(ah))
+                               ah->ah_atexit = NULL;   /* mark it free */
+                       else {
+                               ah->ah_next = dead_handlers;
+                               dead_handlers = ah;
+                       }
+               } else
+                       prevp = &ah->ah_next;
+       }
+
+       mutex_unlock(&atexit_mutex);
+
+       /*
+        * Now free any dead handlers.  Do this even if we're about to
+        * exit, in case a leak-detecting malloc is being used.
+        */
+       while ((ah = dead_handlers) != NULL) {
+               dead_handlers = ah->ah_next;
+               free(ah);
+       }
+}
 
 /*
  * Register a function to be performed at exit.
  */
 int
-atexit(fn)
-       void (*fn) __P((void));
+atexit(void (*func)(void))
 {
-       static struct atexit __atexit0; /* one guaranteed table */
-       struct atexit *p;
-
-       _DIAGASSERT(fn != NULL);
 
-       mutex_lock(&__atexit_mutex);
-       if ((p = __atexit) == NULL)
-               __atexit = p = &__atexit0;
-       else if (p->ind >= ATEXIT_SIZE) {
-               if ((p = malloc(sizeof(*p))) == NULL) {
-                       mutex_unlock(&__atexit_mutex);
-                       return (-1);
-               }
-               p->ind = 0;
-               p->next = __atexit;
-               __atexit = p;
-       }
-       p->fns[p->ind++] = fn;
-       mutex_unlock(&__atexit_mutex);
-       return (0);
+       return (__cxa_atexit((void (*)(void *))func, NULL, NULL));
 }
diff -r 6c72a5bed0e7 -r f3ad41ff080b lib/libc/stdlib/atexit.h
--- a/lib/libc/stdlib/atexit.h  Sat Mar 01 01:46:02 2003 +0000
+++ b/lib/libc/stdlib/atexit.h  Sat Mar 01 04:19:37 2003 +0000
@@ -1,8 +1,11 @@
-/*     $NetBSD: atexit.h,v 1.9 2003/01/18 11:32:03 thorpej Exp $       */
+/*     $NetBSD: atexit.h,v 1.10 2003/03/01 04:19:38 thorpej Exp $      */
 
 /*-
- * Copyright (c) 1990, 1993
- *     The Regents of the University of California.  All rights reserved.
+ * Copyright (c) 2003 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * This code is derived from software contributed to The NetBSD Foundation
+ * by Jason R. Thorpe.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -14,43 +17,24 @@
  *    documentation and/or other materials provided with the distribution.
  * 3. All advertising materials mentioning features or use of this software
  *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
- * 4. Neither the name of the University nor the names of its contributors
- *    may be used to endorse or promote products derived from this software
- *    without specific prior written permission.
+ *        This product includes software developed by the NetBSD
+ *        Foundation, Inc. and its contributors.
+ * 4. Neither the name of The NetBSD Foundation nor the names of its
+ *    contributors may be used to endorse or promote products derived
+ *    from this software without specific prior written permission.
  *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- *     from: @(#)atexit.h      8.2 (Berkeley) 7/3/94
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS



Home | Main Index | Thread Index | Old Index