Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: runtime: support general dynamic thread local storage model #71953

Open
amusman opened this issue Feb 25, 2025 · 5 comments
Open

proposal: runtime: support general dynamic thread local storage model #71953

amusman opened this issue Feb 25, 2025 · 5 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. LanguageProposal Issues describing a requested change to the Go language specification. Proposal
Milestone

Comments

@amusman
Copy link
Contributor

amusman commented Feb 25, 2025

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:

  1. Hinders interoperability with C libraries that have strict TLS allocation schemes (e.g., Musl)
  2. 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.

@gopherbot gopherbot added this to the Proposal milestone Feb 25, 2025
@ianlancetaylor ianlancetaylor moved this to Incoming in Proposals Feb 25, 2025
@ianlancetaylor
Copy link
Member

CC @cherrymui @golang/compiler

@ianlancetaylor ianlancetaylor added the compiler/runtime Issues related to the Go compiler and/or runtime. label Feb 25, 2025
@gabyhelp
Copy link

Related Issues

Related Code Changes

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@gabyhelp gabyhelp added the LanguageProposal Issues describing a requested change to the Go language specification. label Feb 25, 2025
amusman added a commit to amusman/proposal that referenced this issue Feb 26, 2025
For golang/go#71953
For golang/go#54805

Change-Id: Ie6f3641dbea4a4e0993289f8db25bbe6c228724c
GitHub-Last-Rev: fd5b052
GitHub-Pull-Request: golang#56
amusman added a commit to amusman/proposal that referenced this issue Feb 27, 2025
For golang/go#71953
For golang/go#54805

Change-Id: Ie6f3641dbea4a4e0993289f8db25bbe6c228724c
GitHub-Last-Rev: fd5b052
GitHub-Pull-Request: golang#56
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/644615 mentions this issue: design: add 71953-go-dynamic-tls.md

gopherbot pushed a commit to golang/proposal that referenced this issue Feb 27, 2025
For golang/go#71953

Change-Id: Ie6f3641dbea4a4e0993289f8db25bbe6c228724c
GitHub-Last-Rev: ec997ee
GitHub-Pull-Request: #56
Reviewed-on: https://go-review.googlesource.com/c/proposal/+/644615
Commit-Queue: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
@seankhliao seankhliao changed the title proposal: runtime,cmd/asm,cmd/link,cmd/go: Support General Dynamic TLS Model proposal: runtime: support general dynamic thread local storage model Feb 28, 2025
@tudor-timcu
Copy link

Hello! Is there a plan to support this for x86_64? I hit this problem recently on Alpine Linux and was wondering if there's a fix in sight in the near future? Thank you!

@amusman
Copy link
Contributor Author

amusman commented Mar 10, 2025

Hello,
While there isn't a concrete timeline for x86_64 support yet, the proposed design will accommodate it. Specifically, we can pass the same -D=TLS_GD flag and -tls=GD option on x86_64, and then implement the architecture-specific parts under these conditional flags. The initial implementation focuses on Arm64 and allows to extend support to new architectures incrementally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. LanguageProposal Issues describing a requested change to the Go language specification. Proposal
Projects
Status: No status
Status: Incoming
Development

No branches or pull requests

5 participants