Skip to content

cmd/link,runtime: fix c-shared dlopen on non-glibc systems (#13492) #75048

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jgowdy
Copy link

@jgowdy jgowdy commented Aug 16, 2025

This PR fixes long-standing compatibility issues preventing Go c-shared
and c-archive libraries from working correctly on non-glibc systems,
particularly Alpine Linux (musl libc) and other alternative libc
implementations.

Problem

Go's c-shared/c-archive build modes make glibc-specific assumptions that
cause failures on other systems:

  1. SIGSEGV on dlopen(): Go assumes DT_INIT_ARRAY functions receive
    (argc, argv, envp) arguments, but this is glibc-specific behavior not
    guaranteed by the ELF specification. On musl and other systems, these
    pointers are null or garbage, causing immediate segfaults when Go
    tries to dereference argv in runtime.sysargs().

  2. TLS incompatibility: Go uses Initial Exec TLS model which requires
    static TLS allocation. This prevents dlopen() from working on musl and
    other dynamic loaders that don't reserve extra TLS space. The error
    "initial-exec TLS resolves to dynamic definition" makes Go shared
    libraries impossible to load dynamically.

Solution

This PR implements three key fixes:

  1. argc/argv null safety: Add null checks in runtime initialization
    to handle systems where DT_INIT_ARRAY functions don't receive arguments.
    The code now gracefully handles null argv instead of crashing.

  2. TLS General Dynamic support: Implement TLS GD model across all
    architectures (x86, ARM64, ARM, PowerPC64, s390x, RISC-V, LoongArch64,
    MIPS) with a new -tls flag for explicit control. The GD model uses
    __tls_get_addr() for dynamic TLS resolution, enabling dlopen()
    compatibility.

  3. Standards compliance: All changes follow established standards:

    • ELF gABI for DT_INIT_ARRAY behavior
    • ELF TLS specification for General Dynamic model

Implementation

The implementation maintains full backward compatibility with existing
glibc systems while enabling support for other libc implementations. The
-tls flag allows users to control TLS model selection (auto/LE/IE/GD)
with sensible defaults that automatically select GD for c-shared/c-archive
builds on Unix platforms.

Testing

  • Verified dlopen() works on Alpine Linux (musl) with the fixes
  • Confirmed no regressions on Ubuntu/Debian (glibc) systems
  • All existing tests pass
  • Cross-compilation tested for linux/amd64, linux/arm64, linux/386,
    freebsd/amd64, openbsd/amd64

Impact

This enables Go shared libraries to work correctly on Alpine Linux and
other non-glibc systems, addressing a major compatibility gap that has
affected container deployments and embedded systems for years.

Fixes #13492
Fixes #54805

Co-authored-by: Alexander Musman [email protected]

jgowdy and others added 3 commits August 15, 2025 17:27
This change adds comprehensive TLS General Dynamic model support across
all architectures to enable dlopen() of Go shared libraries on
non-glibc systems.

Implementation includes:
- R_*_TLS_GD relocation support for all architectures (amd64, 386,
  arm64, arm, ppc64, s390x, riscv64, loong64, mips/mips64)
- TLS_GD instruction sequences in assemblers for proper __tls_get_addr
  calling convention
- Linker support for TLS GD relocations with external linking
- Automatic GD model selection for c-shared/c-archive builds on Unix
- Preserved compatibility with existing glibc IE model behavior

Fixes dlopen() incompatibility caused by glibc-specific TLS behavior
that prevents Go shared libraries from loading on other dynamic
loaders and libc implementations.

Co-Authored-By: Alexander Musman <[email protected]>
This change extends null-safety checks to handle argc/argv absence
across all Unix platforms for shared library builds.

When running as c-archive or c-shared on non-glibc systems, argv may
be nil since DT_INIT_ARRAY constructors don't receive arguments per
ELF specification. Only glibc provides argc/argv as a non-standard
extension.

Added universal null checks in sysargs() for:
- Linux: Handles standards-compliant libc implementations
- Darwin/macOS: Prevents crashes when using c-shared builds
- FreeBSD/NetBSD/OpenBSD/DragonFly: BSD libc follows ELF spec strictly
- Solaris: Standards-compliant libc implementation

Prevents SIGSEGV crashes while maintaining full backward compatibility
with glibc systems.
This change adds comprehensive documentation and testing infrastructure
for the non-glibc compatibility fixes.

Documentation includes:
- Technical details of TLS General Dynamic implementation
- argc/argv SIGSEGV fix explanation with ELF specification references
- Proper attribution to prior work by Alexander Musman and related
  GitHub issues (golang#71953, golang#73667)
- Standards references for ELF gABI and TLS specifications

Testing infrastructure includes:
- Docker-based multi-architecture test framework
- Alpine Linux (musl) and Ubuntu (glibc) test containers
- Comprehensive c-shared library dlopen() validation
- Test cases for both TLS GD and argc/argv fixes

All documentation uses plain text format following Go project
requirements with 76-character line wrapping.
Add -tls flag to control Thread Local Storage model selection with
options: auto, LE (Local Exec), IE (Initial Exec), GD (General Dynamic).
Default behavior selects GD for c-shared/c-archive builds on Unix
platforms to enable dlopen() compatibility with non-glibc systems.

Validates TLS model selection to prevent invalid combinations such as
LE with shared libraries. Updates all architectures (x86, ARM64, ARM,
PowerPC64, s390x, RISC-V, LoongArch64, MIPS) to use centralized TLS
model selection logic through ShouldUseTLSGD() and ShouldUseTLSLE()
helper functions.

Also fixes missing variable declaration in runtime/os_linux.go that
was causing build failures.

Fixes golang#13492

Co-Authored-By: Alexander Musman <[email protected]>
@gopherbot
Copy link
Contributor

This PR (HEAD: 2fb37bd) has been imported to Gerrit for code review.

Please visit Gerrit at https://go-review.googlesource.com/c/go/+/696635.

Important tips:

  • Don't comment on this PR. All discussion takes place in Gerrit.
  • You need a Gmail or other Google account to log in to Gerrit.
  • To change your code in response to feedback:
    • Push a new commit to the branch used by your GitHub PR.
    • A new "patch set" will then appear in Gerrit.
    • Respond to each comment by marking as Done in Gerrit if implemented as suggested. You can alternatively write a reply.
    • Critical: you must click the blue Reply button near the top to publish your Gerrit responses.
    • Multiple commits in the PR will be squashed by GerritBot.
  • The title and description of the GitHub PR are used to construct the final commit message.
    • Edit these as needed via the GitHub web interface (not via Gerrit or git).
    • You should word wrap the PR description at ~76 characters unless you need longer lines (e.g., for tables or URLs).
  • See the Sending a change via GitHub and Reviews sections of the Contribution Guide as well as the FAQ for details.

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/696635.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Gopher Robot:

Patch Set 1:

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.


Please don’t reply on this GitHub thread. Visit golang.org/cl/696635.
After addressing review feedback, remember to publish your drafts!

@jgowdy-godaddy
Copy link

@richfelker 👍

@gopherbot
Copy link
Contributor

Message from dalias:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/696635.
After addressing review feedback, remember to publish your drafts!

@gopherbot
Copy link
Contributor

Message from Alexander Musman:

Patch Set 1:

(1 comment)


Please don’t reply on this GitHub thread. Visit golang.org/cl/696635.
After addressing review feedback, remember to publish your drafts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants