<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">I feel like I should have explained this better.<div class=""><br class=""></div><div class="">Apple’s thread local implementation is in here, and uses their tlv names for these variables.</div><div class=""><br class=""></div><div class=""><a href="https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c" class="">https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c</a></div><div class=""><br class=""></div><div class="">gcc’s thread local implementation is here, and uses their names:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/gcc-mirror/gcc/blob/master/libgcc/emutls.c" class="">https://github.com/gcc-mirror/gcc/blob/master/libgcc/emutls.c</a></div><div class=""><br class=""></div><div class="">llvm / clang can support both system tlv and emulated TlS. On Apple systems >= 10.7, they use the dyld implementation. On Apple systems < 10.7, by default, they don’t support any. So all standard macOS software that uses thread_local uses the dyld implementation and the names they have there.</div><div class=""><br class=""></div><div class="">llvm/clang does have an emulated_tls implementation of their own here:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/emutls.c" class="">https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/emutls.c</a></div><div class=""><br class=""></div><div class="">They just don’t enable it by default on Apple. So I forced it to be available and turned that on by default on < 10.7.</div><div class=""><br class=""></div><div class="">For c++ threads, the exit function is named differently, so you have to call the right one, which is what this does:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23" class="">https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">If we wanted to bring the dyld thread_local symbols into < 10.7, we would have to implement the functions in the dyld threadLocalVariables.c file, either somehow making that compile and work, or calling into the emutls.c functions with the threadLocalVariables.c names, if that is possible.</div><div class=""><br class=""></div><div class="">The exit function "_tlv_atexit” would than presumably be made to call “__cxa_thread_atexit”.</div><div class=""><br class=""></div><div class="">I hope I did a better job this time.</div><div class=""><br class=""></div><div class="">Ken</div><div class=""><br class=""></div><div class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On May 1, 2021, at 10:18 PM, Ken Cunningham <<a href="mailto:ken.cunningham.webuse@gmail.com" class="">ken.cunningham.webuse@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">thread_local storage on MacOS is handled by dyld and uses and Apple-specific implementation that came into being in 10.7.<div class=""><br class=""></div><div class="">The thread_local infrastructure uses symbols that start with “_tlv_*”.</div><div class=""><br class=""></div><div class="">See here for details:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c" class="">https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">On < 10.6 I have implemented the “emulated_tls” infrastructure that systems without OS implemented TLS use. </div><div class=""><br class=""></div><div class="">This is what gcc uses on every macOS system from 10.4 to BigSur.</div><div class=""><br class=""></div><div class="">The variables have different names, cxa_thread_*.</div><div class=""><br class=""></div><div class="">They normally live in libc on Linux, but I added them the libcxxabi using their mechanism for doing that on systems other than Apple.</div><div class=""><br class=""></div><div class="">To make that change, I added a small test and switch the implementation here:</div><div class=""><br class=""></div><div class=""><a href="https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23" class="">https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23</a></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I have pondered making some “work-alike” functions with the Apple names “tlv_init” etc, so the links would work, and then making perhaps the tlv_* functions call the cxa_thread_* functions, but I just never got around to it.</div><div class=""><br class=""></div><div class="">It might be do-able. Or we might do better to figure out how to make rust use the cxa_thread_* functions instead; probably, it already can. rust is backended by llvm, and we’ve already sorted out how to make llvm do this.</div><div class=""><br class=""></div><div class="">Stay in touch!</div><div class=""><br class=""></div><div class="">Ken</div></div></div></blockquote></div><br class=""></div></body></html>