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