Repository: rust
Issue: Version mismatch between Rust's bundled musl libc and system musl may cause linking errors with native libraries
Labels: A-linkage,O-musl,C-bug

When attempting to compile a project using `jemallocator` on the [rust:1.44.1-alpine3.12](https://hub.docker.com/layers/rust/library/rust/1.44.1-alpine3.12/images/sha256-84783d5b9c9de0f38943e74d83eac5006728f5f3238ae73710ee519841629353?context=explore) 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`](https://github.com/rust-lang/rust/blob/1.44.1/src/ci/docker/scripts/musl.sh#L27).

`secure_getenv` was [added to `musl` in v1.1.24](https://git.musl-libc.org/cgit/musl/tree/WHATSNEW#n2127).  `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](https://github.com/rust-lang/rust/blob/349f6bfb11d73ebb6a272f9a3d00883484f8218c/src/ci/docker/scripts/musl.sh#L27).

```sh
# 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**
```rust
#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;

fn main() {
    println!("Hello, world!");
}
```

**Cargo.toml**
```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](https://doc.rust-lang.org/edition-guide/rust-2018/platform-and-target-support/musl-support-for-fully-static-binaries.html).