Help with legacy-support

Ken Cunningham ken.cunningham.webuse at gmail.com
Sun May 2 05:56:17 UTC 2021


I feel like I should have explained this better.

Apple’s thread local implementation is in here, and uses their tlv names for these variables.

https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c <https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c>

gcc’s thread local implementation is here, and uses their names:

https://github.com/gcc-mirror/gcc/blob/master/libgcc/emutls.c <https://github.com/gcc-mirror/gcc/blob/master/libgcc/emutls.c>

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.

llvm/clang does have an emulated_tls implementation of their own here:

https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/emutls.c <https://github.com/llvm-mirror/compiler-rt/blob/master/lib/builtins/emutls.c>

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.

For c++ threads, the exit function is named differently, so you have to call the right one, which is what this does:

https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23 <https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23>



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.

The exit function "_tlv_atexit” would than presumably be made to call “__cxa_thread_atexit”.

I hope I did a better job this time.

Ken



> On May 1, 2021, at 10:18 PM, Ken Cunningham <ken.cunningham.webuse at gmail.com> wrote:
> 
> thread_local storage on MacOS is handled by dyld and uses and Apple-specific implementation that came into being in 10.7.
> 
> The thread_local infrastructure uses symbols that start with “_tlv_*”.
> 
> See here for details:
> 
> https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c <https://github.com/apple-opensource/dyld/blob/b6b86eb2db14440d373f6f7fd21be4a2bc0da897/src/threadLocalVariables.c>
> 
> 
> On < 10.6 I have implemented the “emulated_tls” infrastructure that systems without OS implemented TLS use. 
> 
> This is what gcc uses on every macOS system from 10.4 to BigSur.
> 
> The variables have different names, cxa_thread_*.
> 
> They normally live in libc on Linux, but I added them the libcxxabi using their mechanism for doing that on systems other than Apple.
> 
> To make that change, I added a small test and switch the implementation here:
> 
> https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23 <https://github.com/macports/macports-ports/blob/19157729c2b2081d3434eca941440846ef8b796d/lang/llvm-9.0/files/9000-patch-clang-7.0-support-emulated-tls.diff#L23>
> 
> 
> 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.
> 
> 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.
> 
> Stay in touch!
> 
> Ken

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macports.org/pipermail/macports-dev/attachments/20210501/a1cc0310/attachment.htm>


More information about the macports-dev mailing list