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

Some functions result in dynamically linked binaries with +crt-static on non-x86 linux gnu #2033

Closed
Jakob-Naucke opened this issue Jan 13, 2021 · 2 comments · Fixed by #2046
Labels
C-bug Category: bug

Comments

@Jakob-Naucke
Copy link
Contributor

Jakob-Naucke commented Jan 13, 2021

Building e.g. this

use libc;
use std::{mem,ptr};
fn main() {
    let mut primary = mem::MaybeUninit::<libc::c_int>::uninit();
    let mut secondary = mem::MaybeUninit::<libc::c_int>::uninit();
    unsafe {
        libc::openpty(
            primary.as_mut_ptr(),
            secondary.as_mut_ptr(),
            ptr::null_mut(),
            ptr::null_mut(),
            ptr::null_mut()
        );
    }
}

(adapted from nix) with -C target-feature=+crt-static on s390x-unknown-linux-gnu yields a binary that still has some .so dependencies:

$ ldd target/debug/test
        linux-vdso64.so.1 (0x000003ff9f7fe000)
        libutil.so.1 => /lib/s390x-linux-gnu/libutil.so.1 (0x000003ff9f680000)
        libc.so.6 => /lib/s390x-linux-gnu/libc.so.6 (0x000003ff9f480000)
        /lib/ld64.so.1 (0x000003ff9f780000)

This was also reproducible (all linux-gnu)

  • on ppc64le
  • on aarch64
  • with forkpty instead

but not reproducible

  • on x86_64
  • with some other unsafe functions such as time
Very verbose output with `-Z print-link-args`

   Compiling libc v0.2.82
     Running `CARGO=/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=build_script_build CARGO_MANIFEST_DIR=/home/ubuntu/.cargo/registry/src/git.luolix.top-eae4ba8cbf2ce1c7/libc-0.2.82 CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='Raw FFI bindings to platform libraries like libc.
' CARGO_PKG_HOMEPAGE='https://github.com/rust-lang/libc' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libc CARGO_PKG_REPOSITORY='https://github.com/rust-lang/libc' CARGO_PKG_VERSION=0.2.82 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=82 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/ubuntu/test-static/target/debug/deps:/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib:/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib:/usr/lib64' rustc --crate-name build_script_build /home/ubuntu/.cargo/registry/src/git.luolix.top-eae4ba8cbf2ce1c7/libc-0.2.82/build.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=09cbcd591eae9ba1 -C extra-filename=-09cbcd591eae9ba1 --out-dir /home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1 -L dependency=/home/ubuntu/test-static/target/debug/deps --cap-lints warn -C target-feature=+crt-static -Z print-link-args`
"cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-L" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.0.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.1.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.10.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.11.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.12.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.13.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.14.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.15.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.2.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.3.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.4.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.5.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.6.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.7.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.8.rcgu.o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.build_script_build.5fa6zfcy-cgu.9.rcgu.o" "-o" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1" "/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build_script_build-09cbcd591eae9ba1.2c0x1qbnibhk5cn.rcgu.o" "-Wl,--gc-sections" "-static" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/ubuntu/test-static/target/debug/deps" "-L" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libstd-50f7c55511b11716.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libpanic_unwind-6b06374750fe5024.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libminiz_oxide-fabc4d4dd11ea3a8.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libadler-1397a5d023aaea88.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libobject-ab1bf02644328503.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libaddr2line-a0f14a96e3061bf5.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libgimli-7ef43e4b423d0eb6.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/librustc_demangle-2187c3d66bd703f9.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libhashbrown-3f4201b41a6c7558.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8bd33c3b7aae2168.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libunwind-d1b4b1683acb236b.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libcfg_if-c545463366acaedf.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/liblibc-caec2c67e026c6e3.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/liballoc-28a6b62567f5a962.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/librustc_std_workspace_core-d04d6d043c6dc122.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libcore-2e6f58b9f207495c.rlib" "-Wl,--end-group" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libcompiler_builtins-9f6cdbe065d300eb.rlib" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-lgcc_eh" "-lgcc" "-Wl,-Bdynamic" "-lutil"
     Running `/home/ubuntu/test-static/target/debug/build/libc-09cbcd591eae9ba1/build-script-build`
[libc 0.2.82] cargo:rustc-cfg=freebsd11
[libc 0.2.82] cargo:rustc-cfg=libc_priv_mod_use
[libc 0.2.82] cargo:rustc-cfg=libc_union
[libc 0.2.82] cargo:rustc-cfg=libc_const_size_of
[libc 0.2.82] cargo:rustc-cfg=libc_align
[libc 0.2.82] cargo:rustc-cfg=libc_core_cvoid
[libc 0.2.82] cargo:rustc-cfg=libc_packedN
[libc 0.2.82] cargo:rustc-cfg=libc_cfg_target_vendor
     Running `CARGO=/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/bin/cargo CARGO_CRATE_NAME=libc CARGO_MANIFEST_DIR=/home/ubuntu/.cargo/registry/src/git.luolix.top-eae4ba8cbf2ce1c7/libc-0.2.82 CARGO_PKG_AUTHORS='The Rust Project Developers' CARGO_PKG_DESCRIPTION='Raw FFI bindings to platform libraries like libc.
' CARGO_PKG_HOMEPAGE='https://github.com/rust-lang/libc' CARGO_PKG_LICENSE='MIT OR Apache-2.0' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=libc CARGO_PKG_REPOSITORY='https://github.com/rust-lang/libc' CARGO_PKG_VERSION=0.2.82 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=2 CARGO_PKG_VERSION_PATCH=82 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/ubuntu/test-static/target/debug/deps:/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib:/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib:/usr/lib64' OUT_DIR=/home/ubuntu/test-static/target/debug/build/libc-a6afc93cde3408db/out rustc --crate-name libc /home/ubuntu/.cargo/registry/src/git.luolix.top-eae4ba8cbf2ce1c7/libc-0.2.82/src/lib.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type lib --emit=dep-info,metadata,link -C embed-bitcode=no -C debuginfo=2 --cfg 'feature="default"' --cfg 'feature="std"' -C metadata=ff0e3c5579e7cbbc -C extra-filename=-ff0e3c5579e7cbbc --out-dir /home/ubuntu/test-static/target/debug/deps -L dependency=/home/ubuntu/test-static/target/debug/deps --cap-lints warn -C target-feature=+crt-static -Z print-link-args --cfg freebsd11 --cfg libc_priv_mod_use --cfg libc_union --cfg libc_const_size_of --cfg libc_align --cfg libc_core_cvoid --cfg libc_packedN --cfg libc_cfg_target_vendor`
   Compiling test v0.0.1 (/home/ubuntu/test-static)
     Running `CARGO=/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=test CARGO_CRATE_NAME=test CARGO_MANIFEST_DIR=/home/ubuntu/test-static CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=test CARGO_PKG_REPOSITORY='' CARGO_PKG_VERSION=0.0.1 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=0 CARGO_PKG_VERSION_PATCH=1 CARGO_PKG_VERSION_PRE='' LD_LIBRARY_PATH='/home/ubuntu/test-static/target/debug/deps:/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib:/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib:/usr/lib64' rustc --crate-name test --edition=2018 src/main.rs --error-format=json --json=diagnostic-rendered-ansi --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=8a1ea7ba351cd7e9 -C extra-filename=-8a1ea7ba351cd7e9 --out-dir /home/ubuntu/test-static/target/debug/deps -C incremental=/home/ubuntu/test-static/target/debug/incremental -L dependency=/home/ubuntu/test-static/target/debug/deps --extern libc=/home/ubuntu/test-static/target/debug/deps/liblibc-ff0e3c5579e7cbbc.rlib -C target-feature=+crt-static -Z print-link-args`
"cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-Wl,--eh-frame-hdr" "-L" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.1vri1zmzot0pzy5g.rcgu.o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.1w8grodbox6fegxa.rcgu.o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.3n1ha6ym3wlbmht5.rcgu.o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.46czccayvrskbsry.rcgu.o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.4zb5nwtixcrrg1ug.rcgu.o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.8bjz1dv9t5o8s4.rcgu.o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.dnmz56754fvdl4r.rcgu.o" "-o" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9" "/home/ubuntu/test-static/target/debug/deps/test-8a1ea7ba351cd7e9.foqfbn68vp8xjlg.rcgu.o" "-Wl,--gc-sections" "-static" "-no-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/home/ubuntu/test-static/target/debug/deps" "-L" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/ubuntu/test-static/target/debug/deps/liblibc-ff0e3c5579e7cbbc.rlib" "-Wl,--start-group" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libstd-50f7c55511b11716.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libpanic_unwind-6b06374750fe5024.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libminiz_oxide-fabc4d4dd11ea3a8.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libadler-1397a5d023aaea88.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libobject-ab1bf02644328503.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libaddr2line-a0f14a96e3061bf5.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libgimli-7ef43e4b423d0eb6.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/librustc_demangle-2187c3d66bd703f9.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libhashbrown-3f4201b41a6c7558.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8bd33c3b7aae2168.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libunwind-d1b4b1683acb236b.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libcfg_if-c545463366acaedf.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/liblibc-caec2c67e026c6e3.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/liballoc-28a6b62567f5a962.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/librustc_std_workspace_core-d04d6d043c6dc122.rlib" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libcore-2e6f58b9f207495c.rlib" "-Wl,--end-group" "/home/ubuntu/.rustup/toolchains/nightly-2020-10-08-s390x-unknown-linux-gnu/lib/rustlib/s390x-unknown-linux-gnu/lib/libcompiler_builtins-9f6cdbe065d300eb.rlib" "-Wl,-Bdynamic" "-lutil" "-Wl,-Bstatic" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-lgcc_eh" "-lgcc" "-Wl,-Bdynamic" "-lutil"
    Finished dev [unoptimized + debuginfo] target(s) in 1.55s

A fully statically linked binary can also be produced e.g. on s390x by removing this line from libc, reproducible on ppc64le (with ppc64le's respective file).

#[link(name = "util")]

Are these link directives necessary?

/cc @fidencio kata-containers/kata-containers#1239

@JohnTitor
Copy link
Member

cc @joshtriplett

@joshtriplett
Copy link
Member

Good catch. No, those link directives should not be there. #2046 should fix static linking for all linux-gnu targets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants