Skip to content

Custom cc toolchain with libunwind creates link problems #2458

Closed
@criemen

Description

@criemen

Hi,

I'm using a custom (bundled) cpp toolchain on Linux, which ships (besides other things) LLVM's libunwind.
I was excited to see that my bundled toolchain is also used for rust compilations - both as part of dependant C/C++ code, and for linking rust executables. This is key for me, as we're shipping an old glibc we want to link against, so our executables are compatible a wide range of distros.

However, I'm running into a problem already when building the process wrapper included in rules_rust:

The rustc invocation coming out of rules_rust looks like this:

  bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.sh -- bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/bin/rustc external/rules_rust/util/process_wrapper/main.rs '--crate-name=process_wrapper' '--crate-type=bin' '--error-format=human' '--out-dir=bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper' '--codegen=opt-level=3' '--codegen=debuginfo=0' '--remap-path-prefix=${pwd}=' '--emit=dep-info,link' '--color=always' '--target=x86_64-unknown-linux-gnu' -L bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/lib '-Cstrip=debuginfo' '--edition=2018' '--codegen=linker=/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/cc_linux_toolchain/bin/clang' --codegen 'link-args=-Wl,--hash-style=both -m64 -static-libgcc --rtlib=compiler-rt --unwindlib=libunwind -fuse-ld=lld --ld-path=/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/cc_linux_toolchain/bin/ld.lld -ldl -lpthread -lm -pthread -ldl --sysroot=/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/cc_linux_toolchain -l:libc++.a' '--extern=tinyjson=bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust_tinyjson/libtinyjson-4031717389.rlib' '-Ldependency=bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust_tinyjson' '--sysroot=bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain')

Note that it passes all the linker options from my custom toolchain, included --unwindlib=libunwind and -static-libgcc to rustc's --codegen 'link-args... option.

rustc invokes clang as

  = note: LC_ALL="C" PATH="bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/bin:/bin:/usr/bin:/usr/local/bin" VSLANG="1033" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/cc_linux_toolchain/bin/clang" "-m64" "/tmp/rustcwA6AdR/symbols.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.0.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.1.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.10.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.11.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.12.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.13.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.14.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.15.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.2.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.3.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.4.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.5.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.6.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.7.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.8.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.process_wrapper.1b271f66-cgu.9.rcgu.o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper.2omey7wc3bx5ld07.rcgu.o" "-Wl,--as-needed" "-L" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust_tinyjson" "-L" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/execroot/semmle_code/bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust_tinyjson/libtinyjson-4031717389.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-bc6b80525d6b1f3b.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-dbb416fff97e9855.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-af60be54961a030f.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-1303bc5098cb2f44.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-96ca4807f9d03fdf.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-03b108942351d49a.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-b348df34b7d8ac11.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-26a06d9c5ec29d3a.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-b5295fdab67e4cf6.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-b257ed099e7f67d0.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-c27b5dca54e295d8.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-f6c8245d52afa66d.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-642c68f15c02cc52.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-eecd84150c4ad967.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-123ffa13a38501db.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-2177aff67f4e9999.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-2298a66e03bd0fd2.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-f3c3b25345711552.rlib" "/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-10f041ff25bad5f3.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rust_linux_x86_64__x86_64-unknown-linux-gnu__stable_tools/rust_toolchain/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "bazel-out/k8-opt-exec-A914AEDA/bin/external/rules_rust/util/process_wrapper/process_wrapper" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-Wl,-O1" "-Wl,--strip-debug" "-nodefaultlibs" "-Wl,--hash-style=both" "-m64" "-static-libgcc" "--rtlib=compiler-rt" "--unwindlib=libunwind" "-fuse-ld=lld" "--ld-path=/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/cc_linux_toolchain/bin/ld.lld" "-ldl" "-lpthread" "-lm" "-pthread" "-ldl" "--sysroot=/home/codespace/.cache/bazel/_bazel_codespace/3c41b00dfcd8c4fb714360e042371c98/external/cc_linux_toolchain" "-l:libc++.a"

which fails with

  = note: clang: warning: argument unused during compilation: '-static-libgcc' [-Wunused-command-line-argument]
          clang: warning: argument unused during compilation: '--unwindlib=libunwind' [-Wunused-command-line-argument]
          clang: warning: argument unused during compilation: '-pthread' [-Wunused-command-line-argument]
          ld.lld: error: unable to find library -lgcc_s
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

This is because before passing in our (custom) link flags, we're getting a bunch of linker flags supplied from somewhere else (I've not been able to find out where, despite searching in rust-lang/rust), including -lgcc_s, and the linker also (now correctly) warns that some of our options aren't used.
As our toolchain uses libunwind for unwinding, libgcc_s is not present in the toolchain, and the linker complains correctly about its absence.

Is there any way to tell rustc not to insert these linker flags?

The only really hacky workaround I see here is a linker-wrapper script that detects being invoked from rustc, and then filters out the harmful CLI arguments, but if there's any other chance of solving this, I really don't want to go there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions