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