Skip to content
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

-crt-static on musl links with libgcc_s #82521

Open
nitsky opened this issue Feb 25, 2021 · 6 comments
Open

-crt-static on musl links with libgcc_s #82521

nitsky opened this issue Feb 25, 2021 · 6 comments
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-musl Target: The musl libc T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nitsky
Copy link

nitsky commented Feb 25, 2021

Hi, I am building dynamic libraries that I call from scripting languages (elixir nif, nodejs native addon, etc) on Alpine Linux. I have been successful using the x86_64-unknown-linux-musl target with -C target-feature=-crt-static to enable building cdylibs. However, I find that any cdylib produced this way links dynamically not only to musl but also to libgcc_s:

$ ldd target/release/libhello.so
/lib/ld-musl-x86_64.so.1 (0x7ffa2d2fb000)
libgcc_s.so.1 => /usr/lib/libgcc_s.so.1 (0x7ffa2d1cd000)
libc.musl-x86_64.so.1 => /lib/ld-musl-x86_64.so.1 (0x7ffa2d2fb000)

Unfortunately, the Alpine Linux docker image does not include the libgcc package by default, and the packages for most scripting languages do not depend on it, so users of my library will be required to explicitly install it, which is an inconvenience.

Below is the line in the unwind crate that dynamically links to libgcc_s on musl targets when crt-static is disabled:

#[link(name = "gcc_s", cfg(not(target_feature = "crt-static")))]

Is there a path to compiling cdylibs for musl without dynamically linking to libgcc_s? Perhaps while unwinding across FFI boundaries remains undefined behavior, -crt-static on musl could statically link the unwind functionality like it does with +crt-static?

Related issues:
#29527
#55120

rustc --version --verbose:

rustc 1.50.0 (cb75ad5db 2021-02-10)
binary: rustc
commit-hash: cb75ad5db02783e8b0222fee363c5f63f7e2cf5b
commit-date: 2021-02-10
host: x86_64-unknown-linux-musl
release: 1.50.0
@nitsky nitsky added the C-bug Category: This is a bug. label Feb 25, 2021
@nitsky
Copy link
Author

nitsky commented Feb 25, 2021

Dynamically linking libgcc_s with -crt-static also causes an error cross-compiling a cdylib from gnu linux to musl. I tried the following both on arch linux and in an ubuntu docker container with musl-gcc installed:

RUSTFLAGS="-C linker=musl-gcc -C target-feature=-crt-static" cargo build --release --target x86_64-unknown-linux-musl

error: linking with `musl-gcc` failed: exit code: 1
  = note: /usr/bin/ld: cannot find libgcc_s.so.1
          collect2: error: ld returned 1 exit status

@rylev
Copy link
Member

rylev commented Mar 10, 2021

I've also run into this issue as well when compiling for an environment which uses musllibc as the libc implementation. Unfortunately, the environment is not under my direct control so installing libgcc is not possible meaning there's no workaround as far as I can tell.

@Logarithmus
Copy link
Contributor

Why can't use just do apk add libgcc or xbps-install libgcc?

@wader
Copy link

wader commented May 28, 2022

@Logarithmus In my case I want to produce a binary that will run in many different docker base images. Having a binary with no external dependencies makes that very convenient. Installing libgcc requires that the base image is a distro with a package manger and has a libgcc package that is compatible. One could of course distribute the alpine libgcc somehow but that is less convenient.

@dupu222
Copy link

dupu222 commented Sep 26, 2022

is there a way dont't to link libgcc_s when building dynamic lib on musl -crt-static?
is there a way to static link libunwind in this case?

@wader
Copy link

wader commented Sep 26, 2022

@dupu222 Maybe these alpine issues are interesting for you. The gcc_s issue should be fixed in alpine edge built rust toolchain (package version 1.62.1 or laster):
https://gitlab.alpinelinux.org/alpine/aports/-/issues/11806
https://gitlab.alpinelinux.org/alpine/aports/-/issues/12941

@jieyouxu jieyouxu added A-linkage Area: linking into static, shared libraries and binaries T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. O-musl Target: The musl libc and removed needs-triage-legacy labels Feb 18, 2024
kxxt added a commit to kxxt/archriscv-packages that referenced this issue Oct 20, 2024
Musl Patch
==========

riscv64gc-unknown-linux-musl gets promoted to tier 2 and `crt_static_default`
is updated to false in rust-lang/rust#122049,
which triggers rust-lang/rust#82521 (comment)
when building stage2 library artifacts
(riscv64gc-unknown-linux-gnu -> riscv64gc-unknown-linux-musl).

I patched it to re-enable `crt_static_default` for `riscv64gc-unknown-linux-musl`
to fix the build and align with the behavior on x86 Arch Linux, where `rust-musl`
defaults to statically link musl.

Wasm compiler_builtins bug
==========================

Wasm compiler_builtins rlib from built `rust-wasm` package includes objects for
host architecture(riscv64 in our case, and x86_64 for x86 Arch Linux).
This is not reproducible for toolchains installed via rustup so I have reported it
to Arch Linux:
https://gitlab.archlinux.org/archlinux/packaging/packages/rust/-/issues/3

Complications when building 1.82.0
==================================

rust-lang/rust#125016 landed in 1.82.0, which breaks
building rust 1.82.0 using our packaged rust 1.81.0.
Compiling the new compiler_builtins component requires a rustc compiler that
includes
rust-lang/rust@99e6a28
but unfortunately 1.81.0 does not, leading to the following ICE:
https://archriscv.felixc.at/.status/log.htm?url=logs/rust/rust-1:1.82.0-1.log

    internal compiler error: compiler/rustc_codegen_llvm/src/abi.rs:126:22: unsupported float: Reg { kind: Float, size: Size(2 bytes) }

This is mitigated upstream by bumping stage0 to 1.82:
rust-lang/rust#129268 (comment)

So we need to first build 1.82.0 rustc once without the f16/f128 handling part
in compiler_builtins, to get a compiler that is capable of handling f16/f128.
And then we can use this compiler to compile compiler_builtins with f16/f128 handling.
It's not easy to do so in one patch. The most easy way is to build and package
rust 1.82.0 twice. This PR covers the first part and disable-f16-f128.diff will
be removed in the second part.
kxxt added a commit to kxxt/archriscv-packages that referenced this issue Oct 20, 2024
Musl Patch
==========

riscv64gc-unknown-linux-musl gets promoted to tier 2 and `crt_static_default`
is updated to false in rust-lang/rust#122049,
which triggers rust-lang/rust#82521 (comment)
when building stage2 library artifacts
(riscv64gc-unknown-linux-gnu -> riscv64gc-unknown-linux-musl).

I patched it to re-enable `crt_static_default` for `riscv64gc-unknown-linux-musl`
to fix the build and align with the behavior on x86 Arch Linux, where `rust-musl`
defaults to statically link musl.

Wasm compiler_builtins bug
==========================

Wasm compiler_builtins rlib from built `rust-wasm` package includes objects for
host architecture(riscv64 in our case, and x86_64 for x86 Arch Linux).
This is not reproducible for toolchains installed via rustup so I have reported it
to Arch Linux:
https://gitlab.archlinux.org/archlinux/packaging/packages/rust/-/issues/3

Complications when building 1.82.0
==================================

rust-lang/rust#125016 landed in 1.82.0, which breaks
building rust 1.82.0 using our packaged rust 1.81.0.
Compiling the new compiler_builtins component requires a rustc compiler that
includes
rust-lang/rust@99e6a28
but unfortunately 1.81.0 does not, leading to the following ICE:
https://archriscv.felixc.at/.status/log.htm?url=logs/rust/rust-1:1.82.0-1.log

    internal compiler error: compiler/rustc_codegen_llvm/src/abi.rs:126:22: unsupported float: Reg { kind: Float, size: Size(2 bytes) }

This is mitigated upstream by bumping stage0 to 1.82:
rust-lang/rust#129268 (comment)

So we need to first build 1.82.0 rustc once without the f16/f128 handling part
in compiler_builtins, to get a compiler that is capable of handling f16/f128.
And then we can use this compiler to compile compiler_builtins with f16/f128 handling.
It's not easy to do so in one patch. The most easy way is to build and package
rust 1.82.0 twice. This PR covers the first part and disable-f16-f128.diff will
be removed in the second part.
felixonmars pushed a commit to felixonmars/archriscv-packages that referenced this issue Oct 20, 2024
Musl Patch
==========

riscv64gc-unknown-linux-musl gets promoted to tier 2 and `crt_static_default`
is updated to false in rust-lang/rust#122049,
which triggers rust-lang/rust#82521 (comment)
when building stage2 library artifacts
(riscv64gc-unknown-linux-gnu -> riscv64gc-unknown-linux-musl).

I patched it to re-enable `crt_static_default` for `riscv64gc-unknown-linux-musl`
to fix the build and align with the behavior on x86 Arch Linux, where `rust-musl`
defaults to statically link musl.

Wasm compiler_builtins bug
==========================

Wasm compiler_builtins rlib from built `rust-wasm` package includes objects for
host architecture(riscv64 in our case, and x86_64 for x86 Arch Linux).
This is not reproducible for toolchains installed via rustup so I have reported it
to Arch Linux:
https://gitlab.archlinux.org/archlinux/packaging/packages/rust/-/issues/3

Complications when building 1.82.0
==================================

rust-lang/rust#125016 landed in 1.82.0, which breaks
building rust 1.82.0 using our packaged rust 1.81.0.
Compiling the new compiler_builtins component requires a rustc compiler that
includes
rust-lang/rust@99e6a28
but unfortunately 1.81.0 does not, leading to the following ICE:
https://archriscv.felixc.at/.status/log.htm?url=logs/rust/rust-1:1.82.0-1.log

    internal compiler error: compiler/rustc_codegen_llvm/src/abi.rs:126:22: unsupported float: Reg { kind: Float, size: Size(2 bytes) }

This is mitigated upstream by bumping stage0 to 1.82:
rust-lang/rust#129268 (comment)

So we need to first build 1.82.0 rustc once without the f16/f128 handling part
in compiler_builtins, to get a compiler that is capable of handling f16/f128.
And then we can use this compiler to compile compiler_builtins with f16/f128 handling.
It's not easy to do so in one patch. The most easy way is to build and package
rust 1.82.0 twice. This PR covers the first part and disable-f16-f128.diff will
be removed in the second part.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-linkage Area: linking into static, shared libraries and binaries C-bug Category: This is a bug. O-musl Target: The musl libc T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

7 participants