You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
runtime: c-shared builds fail with musllibc #13492
Currently, some of the init_array functions provided by a c-shared build expect to be called with (argc, argv, envp) arguments as is done by glibc, but this isn't specified by the ELF gABI for DT_INIT_ARRAY (http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#init_fini), and isn't done with other libc implementations like musllibc.
musl does expose getauxval() for querying the auxiliary vector but does not provide any way to get the main program's argc/argv. The musl maintainers argue that this isn't something a shared library should have access to anyway.
The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.
So in the shared library cases where a C main function runs, the Go runtime should conservatively assume the C program may legitimately mutate the argv strings. In particular, it can't use gostringnocopy on them like it currently does in goargs.
So I think in the cases where we need to play nicely with arbitrary C code:
syscall's {Clear,Get,Put,Set}env and Environ functions should all treat C as the source of truth for the environment, rather than keeping a local copy of the environment to manipulate and just trying to copy mutations to C. (E.g., even today, if cgo code calls setenv, it won't be visible to os.Getenv; whereas os.Setenv is visible to getenv.)
package runtime should use getauxval for accessing the aux value array, to avoid needing to locate the array in memory.
That would just leave needing to figure out a solution for os.Args. It would kinda suck, but maybe we could just leave it nil in the case where Go isn't acting as the program's entry point?
If we can't get os.Args, then we have to leave it nil. But I think we should only do that for environments where we can't get it. When we can get it, as for glibc, we should.
I just looked at glibc, Bionic, uClibc, musl, and dietlibc, as well as the dynamic linkers for FreeBSD, NetBSD, and OpenBSD. It looks like only glibc passes (argc, argv, envp) to the DSO init functions.
If we want to detect glibc at build time, it seems like we can include <limits.h> and test for defined(__GLIBC__) && !defined(__UCLIBC__). (The User-Agent saga continues as uClibc claims to be glibc.)
To detect at runtime, we could use a weak reference to a glibc-only symbol like gnu_get_libc_version and see if it resolves. I'm worried about making sure we pick a symbol that won't later be implemented by other C libraries though. (E.g., I found a thread where someone suggested adding a gnu_get_libc_version function to musl to make it compatible with Nvidia's binary drivers.)
I just ran into this issue while trying to help my partner debug a crash when using a golang plugin they'd written for fluentbit (https://github.com/fluent/fluent-bit).
When running on Alpine Linux which uses musl, I get a segfault inside of runtime.sysargs due to a bad argv pointer. The cause of this is as mentioned above, when the plugin is dlopened, musl does not pass any arguments to the members of DT_INIT_ARRAY. The platform-specific init function (_rt0_amd64_linux_lib in my case) assumes it's being passed a valid argc / argv, and eventually segfaults as they are not.
After finding this issue, it seems that the way forward is to:
update each platform-specific init file to contain an empty argument vector, and update _rt0_*_lib_argv to point at this empty vector
update each platform-specific init function to only overwrite the default argv pointer with the incoming arguments if glibc is detected
Can anyone comment if testing for glibc would still be desired or not?
@mdempsky I started work on the x64 / x86 / arm / arm64 versions the other night, but it was becoming quite time consuming to test. I have docker images running qemu, which are bootstrapped with a cross-compiled toolchain from my host using bootstrap.sh, but then recompile the toolchain locally inside of qemu for CGO support. Is there a better / faster way to test across the various targets?
Also, do you have any recommendations for getting access to the above pre-processor defines from the platform-specific assembly files? Is it possible, or would they need to call into some C function to do the work, e.g.:
Confirming this is still broken across the current release line with a trivial repro — a hello-world Go c-shared (zero application code) segfaults when dlopen'd on Alpine, on go1.22.12 / 1.23.10 / 1.24.5 / 1.25.1 / 1.26.5 alike (built with GOTOOLCHAIN=local for each). All link modes tried (dynamic, -extldflags=-static, -ldflags=-linkmode=internal), and through gcompat with a glibc-linked build — same result. GOTRACEBACK=crash prints nothing (the crash is in c-shared init before the runtime's crash handler is installed), and gdb shows an infinite-recursion pattern to stack exhaustion.
Repro (alpine:3.21 container): go build -buildmode=c-shared -o hello.node on a package that just exports one function, then node -e "require('./hello.node')" (or any dlopen) → SIGSEGV.
Watching #75048 (CL 696635) — confirming the problem is real and current, and the fix is much needed.
Reacted by Domenico Andreoli, Jonas Thelemann, ZM, xhe, Anton Lazarev and Nicola Del Gobbo
Activity
jamesr commentedon Dec 5, 2015
musl does expose getauxval() for querying the auxiliary vector but does not provide any way to get the main program's argc/argv. The musl maintainers argue that this isn't something a shared library should have access to anyway.
mdempsky commentedon Dec 5, 2015
Somewhat relatedly, C99 says [section 5.1.2.2.1]:
So in the shared library cases where a C main function runs, the Go runtime should conservatively assume the C program may legitimately mutate the argv strings. In particular, it can't use gostringnocopy on them like it currently does in goargs.
mdempsky commentedon Dec 5, 2015
So I think in the cases where we need to play nicely with arbitrary C code:
That would just leave needing to figure out a solution for os.Args. It would kinda suck, but maybe we could just leave it nil in the case where Go isn't acting as the program's entry point?
ianlancetaylor commentedon Dec 5, 2015
If we can't get os.Args, then we have to leave it nil. But I think we should only do that for environments where we can't get it. When we can get it, as for glibc, we should.
mdempsky commentedon Dec 5, 2015
I just looked at glibc, Bionic, uClibc, musl, and dietlibc, as well as the dynamic linkers for FreeBSD, NetBSD, and OpenBSD. It looks like only glibc passes (argc, argv, envp) to the DSO init functions.
If we want to detect glibc at build time, it seems like we can include
<limits.h>and test fordefined(__GLIBC__) && !defined(__UCLIBC__). (The User-Agent saga continues as uClibc claims to be glibc.)To detect at runtime, we could use a weak reference to a glibc-only symbol like gnu_get_libc_version and see if it resolves. I'm worried about making sure we pick a symbol that won't later be implemented by other C libraries though. (E.g., I found a thread where someone suggested adding a gnu_get_libc_version function to musl to make it compatible with Nvidia's binary drivers.)
Any other suggestions/ideas for detecting glibc?
mdempsky commentedon Dec 7, 2015
For what it's worth, I tracked down that glibc started passing (argc, argv, envp) to the DSO init functions in 1996: https://sourceware.org/git/?p=glibc.git;a=blobdiff;f=elf/dl-open.c;h=76f6329762308de4ba1620c50ff32d2c02359766;hp=40b52247253cf045498761342afd09ba3c7e1187;hb=dcf0671d905200c449f92ead6cf43c184637a0d5;hpb=4884d0f03c5a3b3d2459655e76fa2d0684d389dc
So at least we don't need to worry about glibc version detection.
elf/app: Pass argc and argv to library initialization
inolen commentedon Dec 29, 2016
I just ran into this issue while trying to help my partner debug a crash when using a golang plugin they'd written for fluentbit (https://github.com/fluent/fluent-bit).
When running on Alpine Linux which uses musl, I get a segfault inside of
runtime.sysargsdue to a bad argv pointer. The cause of this is as mentioned above, when the plugin is dlopened, musl does not pass any arguments to the members ofDT_INIT_ARRAY. The platform-specific init function (_rt0_amd64_linux_lib in my case) assumes it's being passed a valid argc / argv, and eventually segfaults as they are not.After finding this issue, it seems that the way forward is to:
_rt0_*_lib_argvto point at this empty vectorCan anyone comment if testing for glibc would still be desired or not?
rrozestw commentedon Dec 30, 2016
The same problem appears when using c-archive build mode with musl.
ianlancetaylor commentedon Jan 2, 2017
If there is a reliable way to test for glibc, then I think it would be perfectly reasonable to do that. @mdempsky 's comment above suggests a way.
mdempsky commentedon Jan 9, 2017
Unassigning because I'm not planning to work on this, but still happy to review if anyone has suggestions.
inolen commentedon Jan 9, 2017
@mdempsky I started work on the x64 / x86 / arm / arm64 versions the other night, but it was becoming quite time consuming to test. I have docker images running qemu, which are bootstrapped with a cross-compiled toolchain from my host using bootstrap.sh, but then recompile the toolchain locally inside of qemu for CGO support. Is there a better / faster way to test across the various targets?
Also, do you have any recommendations for getting access to the above pre-processor defines from the platform-specific assembly files? Is it possible, or would they need to call into some C function to do the work, e.g.:
82 remaining items
Implement TLS General Dynamic model for ARM64
cmd/link,runtime: implement TLS General Dynamic for standards-complia…
cmd/link,cmd/compile: add -tls flag for TLS model control
cmd/link,cmd/compile: add -tls flag for TLS model control
cmd/link,cmd/compile: add -tls flag for TLS model control
cmd/link,cmd/compile: add -tls flag for TLS model control
gopherbot commentedon Aug 16, 2025
Change https://go.dev/cl/696635 mentions this issue:
cmd/link,runtime: fix c-shared dlopen on non-glibc systems (#13492)Add kernel WireGuard support with musl compatibility
cmd/link,runtime: fix c-shared dlopen on non-glibc systems
cmd/link,runtime: fix c-shared dlopen on non-glibc systems
cmd/link,runtime: fix c-shared dlopen on non-glibc systems
cmd/link,runtime: fix c-shared dlopen on non-glibc systems
johnsoncodehk commentedon Jul 24, 2026
Confirming this is still broken across the current release line with a trivial repro — a hello-world Go c-shared (zero application code) segfaults when dlopen'd on Alpine, on go1.22.12 / 1.23.10 / 1.24.5 / 1.25.1 / 1.26.5 alike (built with GOTOOLCHAIN=local for each). All link modes tried (dynamic,
-extldflags=-static,-ldflags=-linkmode=internal), and through gcompat with a glibc-linked build — same result.GOTRACEBACK=crashprints nothing (the crash is in c-shared init before the runtime's crash handler is installed), and gdb shows an infinite-recursion pattern to stack exhaustion.Repro (alpine:3.21 container):
go build -buildmode=c-shared -o hello.nodeon a package that just exports one function, thennode -e "require('./hello.node')"(or any dlopen) → SIGSEGV.Watching #75048 (CL 696635) — confirming the problem is real and current, and the fix is much needed.
docs(readme): musl workarounds + upstream issue links (#28)
feat(ipsec): strongSwan 6 builder, PKCS#11 on musl, kernel require_co…