diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc index e754e091..55b80185 100644 --- a/src/lib/libc/stdlib/Makefile.inc +++ b/src/lib/libc/stdlib/Makefile.inc @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile.inc,v 1.63 2017/03/24 16:15:31 otto Exp $ +# $OpenBSD: Makefile.inc,v 1.64 2017/12/16 20:06:55 guenther Exp $ # stdlib sources .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib @@ -10,7 +10,9 @@ SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ merge.c posix_pty.c qsort.c radixsort.c rand.c random.c \ realpath.c remque.c setenv.c strtoimax.c \ strtol.c strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c \ - system.c tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c \ + system.c \ + tfind.c thread_atexit.c tsearch.c \ + _rand48.c drand48.c erand48.c jrand48.c \ lcong48.c lrand48.c mrand48.c nrand48.c seed48.c srand48.c \ _Exit.c icdb.c diff --git a/src/lib/libc/stdlib/atexit.c b/src/lib/libc/stdlib/atexit.c index fe4d5bc7..ea9dd129 100644 --- a/src/lib/libc/stdlib/atexit.c +++ b/src/lib/libc/stdlib/atexit.c @@ -1,4 +1,4 @@ -/* $OpenBSD: atexit.c,v 1.26 2017/12/05 21:11:10 kettenis Exp $ */ +/* $OpenBSD: atexit.c,v 1.27 2017/12/16 20:06:56 guenther Exp $ */ /* * Copyright (c) 2002 Daniel Hartmeier * All rights reserved. @@ -31,24 +31,14 @@ #include #include -#include -#include -#pragma weak _DYNAMIC #include #include +#include #include + #include "atexit.h" #include "atfork.h" #include "thread_private.h" -#include "tib.h" - -typeof(dlctl) dlctl asm("_dlctl") __attribute__((weak)); - -struct thread_atexit_fn { - void (*func)(void *); - void *arg; - struct thread_atexit_fn *next; -}; struct atexit *__atexit; static int restartloop; @@ -133,29 +123,6 @@ atexit(void (*fn)(void)) } DEF_STRONG(atexit); -__weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl); - -int -__cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso) -{ - struct thread_atexit_fn *fnp; - struct tib *tib = TIB_GET(); - - fnp = calloc(1, sizeof(struct thread_atexit_fn)); - if (fnp == NULL) - return -1; - - if (_DYNAMIC) - dlctl(NULL, DL_REFERENCE, dso); - - fnp->func = func; - fnp->arg = arg; - fnp->next = tib->tib_atexit; - tib->tib_atexit = fnp; - - return 0; -} - void _thread_finalize(void) { diff --git a/src/lib/libc/stdlib/atexit.h b/src/lib/libc/stdlib/atexit.h index d9bfed85..f2fa7bd8 100644 --- a/src/lib/libc/stdlib/atexit.h +++ b/src/lib/libc/stdlib/atexit.h @@ -1,4 +1,4 @@ -/* $OpenBSD: atexit.h,v 1.11 2017/12/05 13:45:31 kettenis Exp $ */ +/* $OpenBSD: atexit.h,v 1.12 2017/12/16 20:06:56 guenther Exp $ */ /* * Copyright (c) 2002 Daniel Hartmeier @@ -41,6 +41,13 @@ struct atexit { } fns[1]; /* the table itself */ }; +/* a chain of these are hung off each thread's TIB's tib_atexit member */ +struct thread_atexit_fn { + void (*func)(void *); + void *arg; + struct thread_atexit_fn *next; +}; + __BEGIN_HIDDEN_DECLS extern struct atexit *__atexit; /* points to head of LIFO stack */ __END_HIDDEN_DECLS diff --git a/src/lib/libc/stdlib/thread_atexit.c b/src/lib/libc/stdlib/thread_atexit.c new file mode 100644 index 00000000..2e00428e --- /dev/null +++ b/src/lib/libc/stdlib/thread_atexit.c @@ -0,0 +1,49 @@ +/* $OpenBSD: thread_atexit.c,v 1.1 2017/12/16 20:06:56 guenther Exp $ */ +/* + * Copyright (c) 2017 Mark Kettenis + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include +#include +#pragma weak _DYNAMIC +#include +#include + +#include "atexit.h" + +typeof(dlctl) dlctl asm("_dlctl") __attribute__((weak)); + +__weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl); + +int +__cxa_thread_atexit_impl(void (*func)(void *), void *arg, void *dso) +{ + struct thread_atexit_fn *fnp; + struct tib *tib = TIB_GET(); + + fnp = calloc(1, sizeof(struct thread_atexit_fn)); + if (fnp == NULL) + return -1; + + if (_DYNAMIC) + dlctl(NULL, DL_REFERENCE, dso); + + fnp->func = func; + fnp->arg = arg; + fnp->next = tib->tib_atexit; + tib->tib_atexit = fnp; + + return 0; +}