Thread Local Storage (TLS) implementation requires collaboration from compiler, linker, dynamic linker, kernel, and language runtime. Ulrich Drepper’s work on ELF Handling For Thread-Local Storage in Linux is explored, with a focus on x86-64 architecture. The mysterious behavior of implicit TLS in C code is dissected using assembly, revealing the unusual use of segment register FS. The role of FS/GS registers in accessing thread-specific context, such as TLS, is highlighted. The initialization of Thread Control Block (TCB) for managing thread-specific data is discussed, along with the setup of TLS and Dynamic Thread Vector (DTV) for accessing TLS variables. The complex interplay between compiler and dynamic linker in determining TLS offsets is unveiled.
https://chao-tic.github.io/blog/2018/12/25/tls