1*e5678be8Sjoerg /* $NetBSD: cxa_thread_atexit.c,v 1.1 2017/07/11 15:21:35 joerg Exp $ */
2*e5678be8Sjoerg
3*e5678be8Sjoerg /*-
4*e5678be8Sjoerg * Copyright (c) 2017 Joerg Sonnenberger <joerg@NetBSD.org>
5*e5678be8Sjoerg * All rights reserved.
6*e5678be8Sjoerg *
7*e5678be8Sjoerg * Redistribution and use in source and binary forms, with or without
8*e5678be8Sjoerg * modification, are permitted provided that the following conditions
9*e5678be8Sjoerg * are met:
10*e5678be8Sjoerg * 1. Redistributions of source code must retain the above copyright
11*e5678be8Sjoerg * notice, this list of conditions and the following disclaimer.
12*e5678be8Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
13*e5678be8Sjoerg * notice, this list of conditions and the following disclaimer in the
14*e5678be8Sjoerg * documentation and/or other materials provided with the distribution.
15*e5678be8Sjoerg *
16*e5678be8Sjoerg * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*e5678be8Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*e5678be8Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*e5678be8Sjoerg * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*e5678be8Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*e5678be8Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*e5678be8Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*e5678be8Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*e5678be8Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*e5678be8Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*e5678be8Sjoerg * SUCH DAMAGE.
27*e5678be8Sjoerg */
28*e5678be8Sjoerg
29*e5678be8Sjoerg #include <sys/cdefs.h>
30*e5678be8Sjoerg __RCSID("$NetBSD: cxa_thread_atexit.c,v 1.1 2017/07/11 15:21:35 joerg Exp $");
31*e5678be8Sjoerg
32*e5678be8Sjoerg #include <sys/queue.h>
33*e5678be8Sjoerg #include <dlfcn.h>
34*e5678be8Sjoerg #include <stdlib.h>
35*e5678be8Sjoerg
36*e5678be8Sjoerg #include "atexit.h"
37*e5678be8Sjoerg
38*e5678be8Sjoerg __dso_hidden bool __cxa_thread_atexit_used;
39*e5678be8Sjoerg
40*e5678be8Sjoerg struct cxa_dtor {
41*e5678be8Sjoerg SLIST_ENTRY(cxa_dtor) link;
42*e5678be8Sjoerg void *dso_symbol;
43*e5678be8Sjoerg void *obj;
44*e5678be8Sjoerg void (*dtor)(void *);
45*e5678be8Sjoerg };
46*e5678be8Sjoerg
47*e5678be8Sjoerg /* Assumes NULL initialization. */
48*e5678be8Sjoerg static __thread SLIST_HEAD(, cxa_dtor) cxa_dtors = SLIST_HEAD_INITIALIZER(cxa_dstors);
49*e5678be8Sjoerg
50*e5678be8Sjoerg void
__cxa_thread_run_atexit(void)51*e5678be8Sjoerg __cxa_thread_run_atexit(void)
52*e5678be8Sjoerg {
53*e5678be8Sjoerg struct cxa_dtor *entry;
54*e5678be8Sjoerg
55*e5678be8Sjoerg while ((entry = SLIST_FIRST(&cxa_dtors)) != NULL) {
56*e5678be8Sjoerg SLIST_REMOVE_HEAD(&cxa_dtors, link);
57*e5678be8Sjoerg (*entry->dtor)(entry->obj);
58*e5678be8Sjoerg if (entry->dso_symbol)
59*e5678be8Sjoerg __dl_cxa_refcount(entry->dso_symbol, -1);
60*e5678be8Sjoerg free(entry);
61*e5678be8Sjoerg }
62*e5678be8Sjoerg }
63*e5678be8Sjoerg
64*e5678be8Sjoerg /*
65*e5678be8Sjoerg * This dance is necessary since libstdc++ includes
66*e5678be8Sjoerg * __cxa_thread_atexit unconditionally.
67*e5678be8Sjoerg */
68*e5678be8Sjoerg __weak_alias(__cxa_thread_atexit, __cxa_thread_atexit_impl)
69*e5678be8Sjoerg int __cxa_thread_atexit_impl(void (*)(void *), void *, void *);
70*e5678be8Sjoerg
71*e5678be8Sjoerg int
__cxa_thread_atexit_impl(void (* dtor)(void *),void * obj,void * dso_symbol)72*e5678be8Sjoerg __cxa_thread_atexit_impl(void (*dtor)(void *), void *obj, void *dso_symbol)
73*e5678be8Sjoerg {
74*e5678be8Sjoerg struct cxa_dtor *entry;
75*e5678be8Sjoerg
76*e5678be8Sjoerg __cxa_thread_atexit_used = true;
77*e5678be8Sjoerg
78*e5678be8Sjoerg entry = malloc(sizeof(*entry));
79*e5678be8Sjoerg if (entry == NULL)
80*e5678be8Sjoerg return -1;
81*e5678be8Sjoerg entry->dso_symbol = dso_symbol;
82*e5678be8Sjoerg if (dso_symbol)
83*e5678be8Sjoerg __dl_cxa_refcount(entry->dso_symbol, 1);
84*e5678be8Sjoerg entry->obj = obj;
85*e5678be8Sjoerg entry->dtor = dtor;
86*e5678be8Sjoerg SLIST_INSERT_HEAD(&cxa_dtors, entry, link);
87*e5678be8Sjoerg return 0;
88*e5678be8Sjoerg }
89