| 1 |
/* |
| 2 |
* Copyright (c) 2022 |
| 3 |
* |
| 4 |
* Permission to use, copy, modify, and distribute this software for any |
| 5 |
* purpose with or without fee is hereby granted, provided that the above |
| 6 |
* copyright notice and this permission notice appear in all copies. |
| 7 |
* |
| 8 |
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
| 9 |
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
| 10 |
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
| 11 |
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 12 |
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 13 |
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
| 14 |
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 15 |
*/ |
| 16 |
|
| 17 |
/* MP support header */ |
| 18 |
#include "MacportsLegacySupport.h" |
| 19 |
|
| 20 |
/* _tlv_atexit wrap */ |
| 21 |
#if __MP_LEGACY_SUPPORT_ATEXIT_WRAP__ |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <dlfcn.h> |
| 25 |
|
| 26 |
/* signature from https://opensource.apple.com/source/dyld/dyld-852.2/src/threadLocalVariables.c.auto.html */ |
| 27 |
void _tlv_atexit(void (*func)(void*), void* objAddr) { |
| 28 |
void (*real__tlv_atexit)(void (*func)(void*), void* objAddr); |
| 29 |
int (*real___cxa_thread_atexit)(void (*dtor)(void*), void* obj, void* dso_symbol); |
| 30 |
|
| 31 |
real__tlv_atexit = dlsym(RTLD_NEXT, "_tlv_atexit"); |
| 32 |
if (real__tlv_atexit != NULL) { |
| 33 |
/* _tlv_atexit exists in libSystem.B.dylib on 10.7 and later */ |
| 34 |
real__tlv_atexit(func, objAddr); |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
real___cxa_thread_atexit = dlsym(RTLD_DEFAULT, "__cxa_thread_atexit"); |
| 39 |
if (real___cxa_thread_atexit != NULL) { |
| 40 |
/* __cxa_thread_atexit exists in |
| 41 |
MacPorts provided libc++ on 10.6 and earlier |
| 42 |
libSystem.B.dylib on 10.8 and later |
| 43 |
MacPorts provided libstdc++ |
| 44 |
|
| 45 |
in all cases, third parameter (dso_handle) seems to be ignored |
| 46 |
*/ |
| 47 |
real___cxa_thread_atexit(func, objAddr, NULL); |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
exit(EXIT_FAILURE); |
| 52 |
} |
| 53 |
|
| 54 |
/* signature from https://github.com/llvm-mirror/libcxxabi/blob/master/src/cxa_thread_atexit.cpp */ |
| 55 |
int __cxa_thread_atexit(void (*dtor)(void*), void* obj, void* dso_symbol) { |
| 56 |
void (*real__tlv_atexit)(void (*func)(void*), void* objAddr); |
| 57 |
int (*real___cxa_thread_atexit)(void (*dtor)(void*), void* obj, void* dso_symbol); |
| 58 |
|
| 59 |
real___cxa_thread_atexit = dlsym(RTLD_NEXT, "__cxa_thread_atexit"); |
| 60 |
if (real___cxa_thread_atexit != NULL) { |
| 61 |
/* __cxa_thread_atexit exists in |
| 62 |
MacPorts provided libc++ on 10.6 and earlier |
| 63 |
libSystem.B.dylib on 10.8 and later |
| 64 |
MacPorts provided libstdc++ |
| 65 |
*/ |
| 66 |
return real___cxa_thread_atexit(dtor, obj, dso_symbol); |
| 67 |
} |
| 68 |
|
| 69 |
real__tlv_atexit = dlsym(RTLD_DEFAULT, "_tlv_atexit"); |
| 70 |
if (real__tlv_atexit != NULL) { |
| 71 |
/* _tlv_atexit exists in libSystem.B.dylib on 10.7 and later |
| 72 |
|
| 73 |
it seems to be common practice to ignore dso_symbol |
| 74 |
*/ |
| 75 |
real__tlv_atexit(dtor, obj); |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
exit(EXIT_FAILURE); |
| 80 |
} |
| 81 |
|
| 82 |
#endif /*__MP_LEGACY_SUPPORT_ATEXIT_WRAP__*/ |