Proposal Details
Proposal: Support General Dynamic TLS Model
Summary
This proposal aims to add support for the general dynamic (GD) Thread Local Storage (TLS) model to the Go runtime, to resolve issue 54805. The current implementation only supports the initial exec (IE) and local exec (LE) TLS models, which restricts interoperability with certain C libraries (particularly Musl) and prevents loading Go shared libraries without LD_PRELOAD.
A more detailed design document is available at: Proposal: Go general dynamic TLS
There is a prototype implementation for Arm64 Linux in review 644975.
Problem
The Go runtime uses Thread Local Storage to preserve goroutine state (g) when interacting with C code, but lacks support for the general dynamic TLS model. This limitation:
- Hinders interoperability with C libraries that have strict TLS allocation schemes (e.g., Musl)
- Restricts loading of Go shared libraries without using
LD_PRELOAD
Proposed Changes
Assembler and Linker Changes
- Add a new flag
-tls=[IE,LE,GD] to the assembler to explicitly select the TLS model
- Default to
-tls=IE for shared mode, while cmd/go specifies -tls=GD explicitly when needed
- Add target-specific relocations in the linker depending on target architecture
Runtime Changes
- Update references to thread-local variables in runtime assembly code
Command Changes
- Modify
cmd/go to automatically enable the general dynamic TLS model when required, based on the combination of GOOS/GOARCH and buildmode
- Pass
-D=TLS_GD and -tls=GD option to assembler to enable the GD model
Initial Implementation Focus
- Arm64 architecture on Linux systems
buildmode=c-shared and buildmode=c-archive
Please refer to the full design document for more detailed information on the implementation approach and technical considerations.
Proposal Details
Proposal: Support General Dynamic TLS Model
Summary
This proposal aims to add support for the general dynamic (GD) Thread Local Storage (TLS) model to the Go runtime, to resolve issue 54805. The current implementation only supports the initial exec (IE) and local exec (LE) TLS models, which restricts interoperability with certain C libraries (particularly Musl) and prevents loading Go shared libraries without
LD_PRELOAD.A more detailed design document is available at: Proposal: Go general dynamic TLS
There is a prototype implementation for Arm64 Linux in review 644975.
Problem
The Go runtime uses Thread Local Storage to preserve goroutine state (
g) when interacting with C code, but lacks support for the general dynamic TLS model. This limitation:LD_PRELOADProposed Changes
Assembler and Linker Changes
-tls=[IE,LE,GD]to the assembler to explicitly select the TLS model-tls=IEforsharedmode, whilecmd/gospecifies-tls=GDexplicitly when neededRuntime Changes
Command Changes
cmd/goto automatically enable the general dynamic TLS model when required, based on the combination ofGOOS/GOARCHandbuildmode-D=TLS_GDand-tls=GDoption to assembler to enable the GD modelInitial Implementation Focus
buildmode=c-sharedandbuildmode=c-archivePlease refer to the full design document for more detailed information on the implementation approach and technical considerations.