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

Version mismatch between Rust's bundled musl libc and system musl may cause linking errors with native libraries #73589

Open
wfchandler opened this issue Jun 21, 2020 · 3 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

Comments

@wfchandler
Copy link

When attempting to compile a project using jemallocator on the rust:1.44.1-alpine3.12 container, linking fails with jemalloc/src/jemalloc.c:700: undefined reference to 'secure_getenv'. This error appears to caused by Alpine 3.12's system libc using musl v1.1.24, while Rust is bundling musl v1.1.22 in liblibc.

secure_getenv was added to musl in v1.1.24. jemalloc's configure script detects that the function is available in /usr/lib/libc.a, but linking jemalloc-sys to liblibc-2c7c7c631d98bf98.rlib fails as that symbol is not present in the bundled musl v1.1.22.

This problem does not occur with rustc 1.46.0-nightly (f455e46ea 2020-06-20) as the bundled musl has since been upgraded to v1.1.24.

# rustc 1.44.1 (c7087fe00 2020-06-17)
$ nm liblibc-2c7c7c631d98bf98.rlib | grep secure_getenv

# rustc 1.46.0-nightly (f455e46ea 2020-06-20)
$ nm liblibc-484d9d5b8731d696.rlib | grep secure_getenv
secure_getenv.lo:
0000000000000000 T secure_getenv

I tried this code:

main.rs

#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn main() {
    println!("Hello, world!");
}

Cargo.toml

[package]
name = "jemalloc-test"
version = "0.1.0"
edition = "2018"
[dependencies]
jemallocator = "0.3.2"

I expected to see this happen: I am able to successfully build my Rust program against jemallocator when using the x86_64-unknown-linux-musl toolchain.

Instead, this happened: Linking fails with error:

$ cargo build
   Compiling jemalloc-sys v0.3.2
   Compiling jemallocator v0.3.2
   Compiling jemalloc-test v0.1.0 (/builds/jemalloc-test)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-m64" "-nostdlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crt1.o" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crti.o" "-L" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.1087d6qndg68890c.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.1kvg0wjvhta83ld3.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.1n71sjocmlbcmon6.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.1xg437nr24obd7aj.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.2dui9fjyjsndk8r7.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.2wy03b0phibcju1j.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.3voluc358azwt9b5.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.4fd7yruqsunwarts.rcgu.o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.xpgnzo2ky9bi8lg.rcgu.o" "-o" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95" "/builds/jemalloc-test/target/debug/deps/jemalloc_test-050cc2446a85ff95.43ziwmxfggfxt0ho.rcgu.o" "-Wl,--gc-sections" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/builds/jemalloc-test/target/debug/deps" "-L" "/builds/jemalloc-test/target/debug/build/jemalloc-sys-714dd49d1b0c21a8/out/build/lib" "-L" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,-Bstatic" "/builds/jemalloc-test/target/debug/deps/libjemallocator-5fbb8bbe4039c248.rlib" "/builds/jemalloc-test/target/debug/deps/libjemalloc_sys-5f5b441c50e8fbc2.rlib" "/builds/jemalloc-test/target/debug/deps/liblibc-2a90658ea36ddb59.rlib" "-Wl,--start-group" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-1e989660c7e0239b.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-d8f51de00a920e83.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-5da8b29606049f2e.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-45709aa0b2aa6591.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace-adb67d3710b6e7c3.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libbacktrace_sys-7ef0bf860ae4794b.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-24976b1c64c2eb52.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-8bfad4e4aaac5889.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-967450a8cca946f9.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-2c7c7c631d98bf98.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-2f3283fe87bacf1c.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-930355d52951d06e.rlib" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-8ff867d27d5a1b9e.rlib" "-Wl,--end-group" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-3270e1187f690bd6.rlib" "-Wl,-Bdynamic" "-lpthread" "-static" "/usr/local/rustup/toolchains/1.44.1-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/crtn.o"
  = note: /usr/lib/gcc/x86_64-alpine-linux-musl/9.3.0/../../../../x86_64-alpine-linux-musl/bin/ld: /builds/jemalloc-test/target/debug/deps/libjemalloc_sys-5f5b441c50e8fbc2.rlib(jemalloc.pic.o): in function `jemalloc_secure_getenv':
          /builds/jemalloc-test/target/debug/build/jemalloc-sys-714dd49d1b0c21a8/out/build/../jemalloc/src/jemalloc.c:700: undefined reference to `secure_getenv'
          collect2: error: ld returned 1 exit status


error: aborting due to previous error

error: could not compile `jemalloc-test`.

Meta

rustc --version --verbose:

rustc 1.44.1 (c7087fe00 2020-06-17)
binary: rustc
commit-hash: c7087fe00d2ba919df1d813c040a5d47e43b0fe7
commit-date: 2020-06-17
host: x86_64-unknown-linux-musl
release: 1.44.1
LLVM version: 9.0

This may have the same root cause as #61328, though only one symbol is missing in this case.

I don't know if there's a way to prevent this, but a warning would be useful it the version mismatch can be detected.

I also didn't come across any documentation on how Rust bundles its own libc other than notes in issues. Is this specific to the musl toolchain? It would be useful to have this noted somewhere, perhaps in the 2018 edition notes.

@wfchandler wfchandler added the C-bug Category: This is a bug. label Jun 21, 2020
@petrochenkov
Copy link
Contributor

This should be addressed by #72274, which is currently on its way to being fixed (#72738).

If linking musl dynamically (-Ctarget-feature=-crt-static) is acceptable for you, then it can be used as a workaround because it disables the use of bundled libc.

@jonas-schievink jonas-schievink added A-linkage Area: linking into static, shared libraries and binaries O-musl Target: The musl libc labels Jun 21, 2020
@wfchandler
Copy link
Author

Thanks, @petrochenkov, that's good to know. In my case I'm using musl specifically to get a static binary, so I switched to nightly as a workaround.

@mathstuf
Copy link
Contributor

mathstuf commented Jun 1, 2021

Note that even nightly isn't sufficient anymore due to missing aarch64 internal symbols (see sfackler/rust-openssl#1476).

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
Projects
None yet
Development

No branches or pull requests

4 participants