-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Conversation
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 <alexander.musman@gmail.com>
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.
9283b04
to
220f130
Compare
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 <alexander.musman@gmail.com>
541f29d
to
2fb37bd
Compare
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.
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!
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
commented
Aug 16, 2025
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!
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!
Uh oh!
There was an error while loading. Please reload this page.
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:
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().
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:
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.
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.
Standards compliance: All changes follow established standards:
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
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 alexander.musman@gmail.com