-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
base: master
Are you sure you want to change the base?
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 <[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]>
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:
|
Message from Gopher Robot: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/696635. |
Message from Gopher Robot: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/696635. |
Message from dalias: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/696635. |
Message from Alexander Musman: Patch Set 1: (1 comment) Please don’t reply on this GitHub thread. Visit golang.org/cl/696635. |
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 [email protected]