Skip to content

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

Open
@amusman

Description

@amusman

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    LanguageProposalIssues describing a requested change to the Go language specification.Proposalcompiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    Status

    No status

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions