Skip to content

runtime: c-shared builds fail with musllibc #13492

Description

@mdempsky

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.

CC @ianlancetaylor @mwhudson

Activity

  1. jamesr commented on Dec 5, 2015

    @jamesr

    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.

  2. mdempsky commented on Dec 5, 2015

    @mdempsky
    ContributorAuthor

    Somewhat relatedly, C99 says [section 5.1.2.2.1]:

    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.

  3. mdempsky commented on Dec 5, 2015

    @mdempsky
    ContributorAuthor

    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?

  4. ianlancetaylor commented on Dec 5, 2015

    @ianlancetaylor
    Contributor

    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.

  5. added this to the Unplanned milestone on Dec 5, 2015
  6. mdempsky commented on Dec 5, 2015

    @mdempsky
    ContributorAuthor

    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.)

    Any other suggestions/ideas for detecting glibc?

  7. self-assigned this
    on Dec 7, 2015
  8. mdempsky commented on Dec 7, 2015

    @mdempsky
    ContributorAuthor

    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.

  9. inolen commented on Dec 29, 2016

    @inolen

    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?

  10. rrozestw commented on Dec 30, 2016

    @rrozestw

    The same problem appears when using c-archive build mode with musl.

  11. ianlancetaylor commented on Jan 2, 2017

    @ianlancetaylor
    Contributor

    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.

  12. removed their assignment
    on Jan 9, 2017
  13. mdempsky commented on Jan 9, 2017

    @mdempsky
    ContributorAuthor

    Unassigning because I'm not planning to work on this, but still happy to review if anyone has suggestions.

  14. inolen commented on Jan 9, 2017

    @inolen

    @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.:

    void override_args(int argc, char *argv, int *argc_out, char *argv_out) {
    #if GLIBC
      *argc_out = argc;
      *argv_out = argv;
    #endif
    }
  15. 82 remaining items

  16. added 6 commits that reference this issue on Aug 15, 2025
    7acbefb
    221403c
    a77a083
    9283b04
    220f130
    2fb37bd
  17. gopherbot commented on Aug 16, 2025

    @gopherbot
    Contributor

    Change https://go.dev/cl/696635 mentions this issue: cmd/link,runtime: fix c-shared dlopen on non-glibc systems (#13492)

  18. added a commit that references this issue on Feb 28, 2026
    e2cf84a
  19. added 4 commits that reference this issue on Apr 8, 2026
    3d32f23
    d550c0d
    6ea22ce
    1a087d0
  20. johnsoncodehk commented on Jul 24, 2026

    @johnsoncodehk

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    compiler/runtimeIssues related to the Go compiler and/or runtime.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions