Skip to content

Tracking issue for musl host toolchain #59302

Open

Description

@mati865

View all comments

TODO:

For people looking how to use as it dynamic target:

RUSTFLAGS="-C target-feature=-crt-static" cargo build

Activity

  1. added
    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFC
    O-muslTarget: The musl libc
    on Mar 19, 2019
  2. wangbj commented on Mar 20, 2019

    @wangbj

    It seems x86_64-unknown-linux-musl target doesn't honor --enable-default-pie, a different behavior compare to x86_64-unknown-linux-gnu. Is this a known issue?

  3. mati865 commented on Mar 20, 2019

    @mati865
    MemberAuthor

    @wangbj I don't think Rust supports --enable-default-pie for any target. Could you provide more details?

  4. wangbj commented on Mar 20, 2019

    @wangbj

    hmm, the problem seems caused by musl-gcc wrapper instead of rustc:

    details: https://gist.github.com/wangbj/ead5fa8c96418de7c9050bc354fbf353

    --enable-default-pie is a gcc configure flag. sorry for the noise. (shortened the text to a gist).

  5. Cogitri commented on May 3, 2019

    @Cogitri
  6. mati865 commented on May 5, 2019

    @mati865
    MemberAuthor

    I knew about Alpine and Void. IMO Alpine approach is better because <arch>-unknown-linux-musl remain static by default (it behaves like upstream) and new target <arch>-alpine-linux-musl links dynamically by default.

    I had looked at that PR and libc changes will be incompatible with upstream rust behaviour. Official Rust packages when statically linking to musl create executables that all fully static (run basically everywhere), that patch will create static executables that depend on musl runtime (use have to install musl to be able to run them).

  7. Cogitri commented on May 5, 2019

    @Cogitri

    Yup, but at least on Void we've come to terms that the Rust in the repos is only really meant for packages in the repos, while users interested in rust development should use upstream Rust via rustip.

  8. smaeul commented on May 5, 2019

    @smaeul
    Contributor

    I had looked at that PR and libc changes will be incompatible with upstream rust behaviour. Official Rust packages when statically linking to musl create executables that all fully static (run basically everywhere), that patch will create static executables that depend on musl runtime (use have to install musl to be able to run them).

    That doesn't make sense. Static executables by definition don't depend on having musl installed. Are you referring to rust-lang/libc#1327? That change would require having a musl-targeting toolchain installed for rustc to link (static) musl-targeting binaries. But any static binaries compiled by rustc would still be totally independent and require no musl installation.

  9. mati865 commented on May 5, 2019

    @mati865
    MemberAuthor
  10. smaeul commented on May 5, 2019

    @smaeul
    Contributor

    @mati865 Okay, so that patch is misnamed. It doesn't have anything to do with static versus dynamic linking. It makes the same change as rust-lang/libc#1327 -- using the C toolchain's copy of libc.a instead of bundling it inside liblibc.rlib. And my comment above applies to it as well.

    Alpine also ships custom targets that use the C toolchain's crt*.o instead of rustc shipping a second copy. Since on Alpine targets rustc's copy of crt*.o are not used, this patch stops rustc from making the copy. (This change is unrelated to the liblibc change.)

  11. mati865 commented on May 6, 2019

    @mati865
    MemberAuthor

    Thanks for the explanation.

  12. thedrow commented on Jun 17, 2019

    @thedrow

    #53968 also seems to be related but it's not included in the OP's checklist.
    Am I correct?

  13. mati865 commented on Jun 17, 2019

    @mati865
    MemberAuthor

    Updated

  14. thedrow commented on Jun 27, 2019

    @thedrow

    #61328 also seems important.

  15. 15 remaining items

  16. yshui commented on Sep 25, 2024

    @yshui
    Contributor

    is it possible to have dylib/cdylib with +crt-static?

  17. bjorn3 commented on Sep 26, 2024

    @bjorn3
    Member

    Musl doesn't support that as the dynamic linker is part of libc and it doesn't support multiple copies of libc in a single process like on basically every Unix. The only mainstream system where that is possible is Windows as there the dynamic linker is part of the kernel and libc is not the system abi and multiple libc copies can co-exist in a single process.

  18. yshui commented on Sep 27, 2024

    @yshui
    Contributor

    i believe it would be possible to create "statically linked" shared library. i.e. all libc symbols are resolved at link time and the shared library is completely self-contained that it neither exposes nor references any libc symbols. we would also need the equivalent of -fno-semantic-interposition etc., and other things i might have missed, but i think it is doable at least in theory.

    it might cause some confusion since two symbols ostensibly have the same name can actually be completely unrelated to each other, (which, btw, already happens in rust today if my project happens to use different versions of the same crate), but the shared library should otherwise work.

  19. bjorn3 commented on Sep 27, 2024

    @bjorn3
    Member

    There are various pieces of global state in libc as well as some process state like the tls register for which each libc instance assumes exclusive access.

  20. yshui commented on Sep 27, 2024

    @yshui
    Contributor

    Ah right, yeah I forgot about that. That definitely makes this unsupportable from Rust's perspective. There goes my dreams :'(

    OTOH it's probably not that bad? I'd imagine process state and tls register setup generally happen before main and probably isn't going to be touched again when a shared library is loaded. I can probably still make it work as a hack.

  21. bjorn3 commented on Sep 28, 2024

    @bjorn3
    Member

    TLS registering also happens on every dlopen and pthread_create, so don't use either from within your statically linked dylib. And you better make sure that the host program uses musl as libc as errno is stored at a fixed offset from the TLS register, which other libc's may use for other purposes.

  22. WhyNotHugo commented on Apr 18, 2026

    @WhyNotHugo

    Would a patch implementing the suggestion above be acceptable?

    override the crt_static_default = true in musl targets when the host environment is also musl, or alternatively change it to crt_static_default = false and override it when cross compiling.

    I believe this yields a sane/expected behaviour when compiling x86_64-unknown-linux-musl on x86_64-unknown-linux-musl distributions, but also won't break the workflow of users who cross-compile without a musl sysroot.

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

    C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCO-muslTarget: The musl libcS-tracking-impl-incompleteStatus: The implementation is incomplete.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions