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

Fix linkage and C++ support for musl targets. #1063

Merged
merged 1 commit into from
Oct 25, 2022

Conversation

Alexhuszagh
Copy link
Contributor

@Alexhuszagh Alexhuszagh commented Oct 7, 2022

Change behavior to use static-pie linkage by default (consistent with Alpine's behavior for pure Rust packages as well as those with external C dependencies), and fix musl targets with C++ dependencies by forcing static-pie linkage to standard libraries.

Closes #902.

@Alexhuszagh Alexhuszagh requested a review from a team as a code owner October 7, 2022 20:24
Copy link
Member

@Emilgardis Emilgardis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is great

docker/musl.sh Outdated Show resolved Hide resolved
docker/musl.sh Outdated Show resolved Hide resolved
docker/musl.sh Outdated Show resolved Hide resolved
@Emilgardis
Copy link
Member

bors try --target musl --cpp 1

bors bot added a commit that referenced this pull request Oct 7, 2022
@Alexhuszagh
Copy link
Contributor Author

Alexhuszagh commented Oct 7, 2022

This changes the default linkage, and will increase the executable size of the generated binaries (but only for C++ binaries, which currently don't really work anyway), but ensure maximum portability and allow libraries with C++ dependencies to build and run correctly.

The following sizes exist:

Package Type Original Original (Stripped) New New (Stripped)
Rust 3.9M 396K 3.9M 396K
C 3.9M 392K 3.9M 392K
C++ 4.2M 492K 14M 892K

docker/musl.sh Outdated Show resolved Hide resolved
@bors
Copy link
Contributor

bors bot commented Oct 7, 2022

try

Build failed:

@Alexhuszagh
Copy link
Contributor Author

Alexhuszagh commented Oct 7, 2022

The mips-unknown-linux-musl and mipsel-unknown-linux-musl failures in CI confuse me, since crt_static_respected: true for linux, base.crt_static_default = true; for the 32-bit mips targets and I don't believe they're supposed to link anywhere to libgcc_s. This seems to be a known issue rust-lang/rust#82521. That being said, we can probably get around this by just by symlinking our libgcc.a to libgcc_s.a.

error: linking with `mips-linux-muslsf-gcc` failed: exit status: 1
Error:   |
  = note: "mips-linux-muslsf-gcc" "/tmp/rustcUH2otM/symbols.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.0.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.1.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.2.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.3.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.4.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.5.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.6.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.hello.fccae6e0-cgu.7.rcgu.o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9.3lna4hjplgiexh2x.rcgu.o" "-Wl,--as-needed" "-L" "/target/mips-unknown-linux-musl/debug/deps" "-L" "/target/debug/deps" "-L" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libstd-d6d67ab280b6c06a.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libpanic_unwind-d27f360bda44297f.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libobject-4ca0eb31a10e71f4.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libmemchr-fcabdc871c5d6f00.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libaddr2line-ea9c884ef3d4e1c7.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libgimli-b9cd8f9115c89d02.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/librustc_demangle-9d508ef7308138c6.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libstd_detect-c2e22da431dbb0f4.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libhashbrown-277d58d205da90dd.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libminiz_oxide-db11edea7ebc97c1.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libadler-831b50189349c89c.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/librustc_std_workspace_alloc-b0d3bdee69724baa.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libunwind-8393a96e77e3a073.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libcfg_if-711b760cf48f8ee6.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/liblibc-7af5383540ab9b65.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/liballoc-8a6fceb722872fdf.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/librustc_std_workspace_core-54eead1a7c1070c7.rlib" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libcore-4ed9f6d0735ec78a.rlib" "-Wl,--end-group" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib/libcompiler_builtins-d8ca1f65dd356df9.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/runner/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/mips-unknown-linux-musl/lib" "-o" "/target/mips-unknown-linux-musl/debug/deps/hello-6820248237858af9" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/local/bin/../lib/gcc/mips-linux-muslsf/9.2.0/../../../../mips-linux-muslsf/bin/ld: cannot find -lgcc_s
          collect2: error: ld returned 1 exit status

EDIT: Seems like the known bug:

$ rustc --print native-static-libs src/lib.rs 
note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.

note: native-static-libs: -lgcc_s -lutil -lrt -lpthread -lm -ldl -lc

$ rustc --print native-static-libs src/lib.rs --target mips-unknown-linux-musl
note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.

note: native-static-libs: -lgcc_s -lc

$ rustc --print native-static-libs src/lib.rs --target mips64-unknown-linux-muslabi64
note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.

note: native-static-libs: -lunwind -lc

$ rustc --print native-static-libs src/lib.rs --target aarch64-unknown-linux-musl
note: Link against the following native artifacts when linking against this static library. The order and any duplication can be significant on some platforms.

note: native-static-libs: -lunwind -lc

@Alexhuszagh
Copy link
Contributor Author

bors try --target mips*musl --cpp 1

bors bot added a commit that referenced this pull request Oct 7, 2022
@Alexhuszagh Alexhuszagh marked this pull request as draft October 7, 2022 22:16
@Alexhuszagh
Copy link
Contributor Author

Alexhuszagh commented Oct 7, 2022

Changing this to a draft since it breaks if -C target-feature=-crt-static is enabled. There might be a better way to do this. Also, previously running x86_64-unknown-linux-musl with -C target-feature=-crt-static actually works, which means it's only the crt-static feature that's failing (the default). It's also not statically linking to the runtime in that case.

@bors
Copy link
Contributor

bors bot commented Oct 7, 2022

try

Build succeeded:

Change behavior to use static-pie linkage by default (consistent with
Alpine's behavior for pure Rust packages as well as those with external
C dependencies), and fix musl targets with C++ dependencies by forcing
statie-pie linkage to standard libraries.
@Alexhuszagh
Copy link
Contributor Author

Alexhuszagh commented Oct 8, 2022

bors try --target mips*musl --cpp 1

Whoops this was supposed to be all musl targets.

bors bot added a commit that referenced this pull request Oct 8, 2022
@@ -182,6 +182,21 @@ main() {

rm -rf "${td}"
fi

# special tests for a shared C runtime, since we disable the shared c++ runtime
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need a check for C++ support since it's for a shared C runtime. The C++ runtime will always be static, and we've tested that above.

@bors
Copy link
Contributor

bors bot commented Oct 8, 2022

try

Build succeeded:

@Alexhuszagh Alexhuszagh marked this pull request as ready for review October 8, 2022 16:46
@Emilgardis Emilgardis self-requested a review October 8, 2022 16:52
Copy link
Member

@Emilgardis Emilgardis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bors r+

@bors
Copy link
Contributor

bors bot commented Oct 25, 2022

Build succeeded:

@bors bors bot merged commit 2c9843f into cross-rs:main Oct 25, 2022
@Alexhuszagh Alexhuszagh deleted the musl_cpp branch October 25, 2022 18:20
@Emilgardis Emilgardis added this to the v0.3.0 milestone Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

x86_64-unknown-linux-musl links to Glibc With C++ Dependencies
2 participants