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