Skip to content

Commit

Permalink
wrap _tlv_atexit and __cxa_thread_atexit
Browse files Browse the repository at this point in the history
`__cxa_thread_atexit` exists in
    MacPorts provided libc++ on 10.6 and earlier
    libSystem.B.dylib on 10.8 and later
    MacPorts provided libstdc++

`_tlv_atexit` exists in libSystem.B.dylib on 10.7 and later

Upstream Clang uses `_tlv_atexit` on all Darwin systems.
MacPorts Clang uses `_tlv_atexit` on 10.6 and earlier.

When MACOSX_DEPLOYMENT_TARGET is set, allow legacy-support library
to find the appropriate atexit function at runtime so the binary
can run on systems other than then one it was built on.

See macports/macports-ports#14514
  • Loading branch information
MarcusCalhoun-Lopez authored and cjones051073 committed Apr 11, 2022
1 parent 834c791 commit 345091d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/MacportsLegacySupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,7 @@
/* copyfile and its associated functions have gained functionality over the years */
#define __MP_LEGACY_SUPPORT_COPYFILE_WRAP__ (__APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1060)

/* _tlv_atexit and __cxa_thread_atexit */
#define __MP_LEGACY_SUPPORT_ATEXIT_WRAP__ (__APPLE__ && __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070)

#endif /* _MACPORTS_LEGACYSUPPORTDEFS_H_ */
10 changes: 10 additions & 0 deletions src/add_symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,13 @@ extern const char pthread_setname_np_tmp5 __asm("$ld$add$os10.5$_pthread_setname
/* extern const char copyfile_state_get_tmp4 __asm("$ld$add$os10.4$_copyfile_state_get"); __attribute__((visibility("default"))) const char copyfile_state_get_tmp4 = 0; */
extern const char copyfile_state_get_tmp5 __asm("$ld$add$os10.5$_copyfile_state_get"); __attribute__((visibility("default"))) const char copyfile_state_get_tmp5 = 0;
#endif

#if !(__MP_LEGACY_SUPPORT_ATEXIT_WRAP__)
extern const char _tlv_atexit_tmp4 __asm("$ld$add$os10.4$__tlv_atexit"); __attribute__((visibility("default"))) const char _tlv_atexit_tmp4 = 0;
extern const char _tlv_atexit_tmp5 __asm("$ld$add$os10.5$__tlv_atexit"); __attribute__((visibility("default"))) const char _tlv_atexit_tmp5 = 0;
extern const char _tlv_atexit_tmp6 __asm("$ld$add$os10.6$__tlv_atexit"); __attribute__((visibility("default"))) const char _tlv_atexit_tmp6 = 0;

extern const char __cxa_thread_atexit_tmp4 __asm("$ld$add$os10.4$___cxa_thread_atexit"); __attribute__((visibility("default"))) const char __cxa_thread_atexit_tmp4 = 0;
extern const char __cxa_thread_atexit_tmp5 __asm("$ld$add$os10.5$___cxa_thread_atexit"); __attribute__((visibility("default"))) const char __cxa_thread_atexit_tmp5 = 0;
extern const char __cxa_thread_atexit_tmp6 __asm("$ld$add$os10.6$___cxa_thread_atexit"); __attribute__((visibility("default"))) const char __cxa_thread_atexit_tmp6 = 0;
#endif
82 changes: 82 additions & 0 deletions src/macports_legacy_atexit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright (c) 2022
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

/* MP support header */
#include "MacportsLegacySupport.h"

/* _tlv_atexit wrap */
#if __MP_LEGACY_SUPPORT_ATEXIT_WRAP__

#include <stdlib.h>
#include <dlfcn.h>

/* signature from https://opensource.apple.com/source/dyld/dyld-852.2/src/threadLocalVariables.c.auto.html */
void _tlv_atexit(void (*func)(void*), void* objAddr) {
void (*real__tlv_atexit)(void (*func)(void*), void* objAddr);
int (*real___cxa_thread_atexit)(void (*dtor)(void*), void* obj, void* dso_symbol);

real__tlv_atexit = dlsym(RTLD_NEXT, "_tlv_atexit");
if (real__tlv_atexit != NULL) {
/* _tlv_atexit exists in libSystem.B.dylib on 10.7 and later */
real__tlv_atexit(func, objAddr);
return;
}

real___cxa_thread_atexit = dlsym(RTLD_DEFAULT, "__cxa_thread_atexit");
if (real___cxa_thread_atexit != NULL) {
/* __cxa_thread_atexit exists in
MacPorts provided libc++ on 10.6 and earlier
libSystem.B.dylib on 10.8 and later
MacPorts provided libstdc++
in all cases, third parameter (dso_handle) seems to be ignored
*/
real___cxa_thread_atexit(func, objAddr, NULL);
return;
}

exit(EXIT_FAILURE);
}

/* signature from https://github.com/llvm-mirror/libcxxabi/blob/master/src/cxa_thread_atexit.cpp */
int __cxa_thread_atexit(void (*dtor)(void*), void* obj, void* dso_symbol) {
void (*real__tlv_atexit)(void (*func)(void*), void* objAddr);
int (*real___cxa_thread_atexit)(void (*dtor)(void*), void* obj, void* dso_symbol);

real___cxa_thread_atexit = dlsym(RTLD_NEXT, "__cxa_thread_atexit");
if (real___cxa_thread_atexit != NULL) {
/* __cxa_thread_atexit exists in
MacPorts provided libc++ on 10.6 and earlier
libSystem.B.dylib on 10.8 and later
MacPorts provided libstdc++
*/
return real___cxa_thread_atexit(dtor, obj, dso_symbol);
}

real__tlv_atexit = dlsym(RTLD_DEFAULT, "_tlv_atexit");
if (real__tlv_atexit != NULL) {
/* _tlv_atexit exists in libSystem.B.dylib on 10.7 and later
it seems to be common practice to ignore dso_symbol
*/
real__tlv_atexit(dtor, obj);
return 0;
}

exit(EXIT_FAILURE);
}

#endif /*__MP_LEGACY_SUPPORT_ATEXIT_WRAP__*/

1 comment on commit 345091d

@cooljeanius
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cross-referencing this against iains/gcc-darwin-arm64#1

Please sign in to comment.