rustc -v should print out linker output

d08c9a4
Opened by Jeremy Fitzhardinge at 2025-02-20 14:23:32

If I compile something with rustc -v, it doesn't show the linker invocations or anything else:

$ echo "fn main() {}" > t.rs
$ rustc -v --crate-type bin t.rs
$ ls -l t
-rwxr-xr-x. 1 jsgf users 2052670 Dec  6 17:15 t
  1. If I compile something with rustc -v, it doesn't show the linker invocations or anything else:

    $ echo "fn main() {}" > t.rs
    $ rustc -v --crate-type bin t.rs
    $ ls -l t
    -rwxr-xr-x. 1 jsgf users 2052670 Dec  6 17:15 t
    
    <!-- TRIAGEBOT_START --> <!-- TRIAGEBOT_ASSIGN_START --> <!-- TRIAGEBOT_ASSIGN_DATA_START$${"user":"yanok"}$$TRIAGEBOT_ASSIGN_DATA_END --> <!-- TRIAGEBOT_ASSIGN_END --> <!-- TRIAGEBOT_END -->

    rustbot at 2021-11-02 09:11:26

  2. Currently we don't actually print anything on -v other than rustc -h -v has an extended help message. Seems reasonable to me though that we'd extend the output quite a bit (e.g. with linker invocations)

    Alex Crichton at 2016-12-07 08:02:05

  3. It used to show the linker command lines. Was dropping that a deliberate decision?

    Jeremy Fitzhardinge at 2016-12-07 19:12:16

  4. Oh dear I had no idea that it ever used to emit the linker command line, if that stopped working it's definitely a regression!

    Alex Crichton at 2016-12-08 01:10:14

  5. I'd really appreciate if we printed the output from the linker when using -v as sometimes linkers will print out exceptionally helpful warnings.

    Peter Atashian at 2017-02-09 11:50:15

  6. I found a way to get the linker output today if you need it now. You can set the RUST_LOG environnement variable to rustc_trans=info. With the nightly rust version I am using, you should see a line that says linker output.

    Jòan at 2018-03-26 11:13:03

  7. Copying @japaric's useful tip for those of us that need the info before the request is implemented:

    FWIW, you can use rustc -Z print-link-args foo.rs to see the linker invocation.

    Jake Goulding at 2018-04-16 15:54:54

  8. -Z print-link-args only prints the arguments to the linker, not the output from the linker.

    Peter Atashian at 2018-04-16 16:38:59

  9. also...

    error: the option `Z` is only accepted on the nightly compiler
    

    Sevki at 2019-10-12 09:19:12

  10. You can use

    export RUSTC_LOG=rustc_codegen_ssa::back::link=info
    

    to get linker command line and stderr+stdout.

    example:

    RUSTFLAGS="-C link-arg=-Wl,--verbose"
    

    Output:

    > export RUSTFLAGS="-C link-arg=-Wl,--verbose"
    > export RUSTC_LOG=rustc_codegen_ssa::back::link=info
    > cargo build -v
       Compiling hello v0.1.0 (/tmp/hello)
         Running `rustc --crate-name hello --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=45f8637d16581c8f -C extra-filename=-45f8637d16581c8f --out-dir /usr/obj/rust/debug/deps -C incremental=/usr/obj/rust/debug/incremental -L dependency=/usr/obj/rust/debug/deps -C link-arg=-Wl,--verbose`
    INFO rustc_codegen_ssa::back::link preparing Executable to "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f"
    INFO rustc_codegen_ssa::back::link "cc" "-m64" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-Wl,--as-needed" "-nostartfiles" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.12qz3h90flkmctyy.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.18bwyoe3zsankc6h.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1evuf5tptkdn3v4y.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1ks010txjl1pix0f.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.23lkiijklf2p4g21.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.38sw7slh52jfxdr3.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.52b1zbprkak691s9.rcgu.o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.5a02jqibtvw5j173.rcgu.o" "-o" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f" "/usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3xix89bijvttq8xf.rcgu.o" "-Wl,--gc-sections" "-static-pie" "-Wl,-zrelro" "-Wl,-znow" "-nodefaultlibs" "-L" "/usr/obj/rust/debug/deps" "-L" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-3b4724f13fd9b314.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-8370d68adc8c1b27.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-a8d5199c03b80566.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-64842aa7c220460e.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-193c992b516cb994.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-530bc5c0808f03e3.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-f1e3de972413762a.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-0708ef471f010f9e.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-e0ac20be4751c3d2.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-bfee6cb7e808ec7f.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-b22097ce75ccf616.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-1f58ee4ae7403119.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-4a9ec9bb065104e9.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-683c2ad1ec876f13.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-d5514ba8af77a08c.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-9af05494f0573e1b.rlib" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-60eab00334ec8288.rlib" "-Wl,--end-group" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-7a2a7a2602cba6f5.rlib" "-Wl,--verbose" "-Wl,-Bdynamic" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o" "/home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o"
    INFO rustc_codegen_ssa::back::link linker stderr:
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/rcrt1.o
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crti.o
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtbeginS.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.12qz3h90flkmctyy.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.18bwyoe3zsankc6h.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1evuf5tptkdn3v4y.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.1ks010txjl1pix0f.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.23lkiijklf2p4g21.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.38sw7slh52jfxdr3.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.52b1zbprkak691s9.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.5a02jqibtvw5j173.rcgu.o
    ld.lld: /usr/obj/rust/debug/deps/hello-45f8637d16581c8f.3xix89bijvttq8xf.rcgu.o
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd-3b4724f13fd9b314.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libpanic_unwind-8370d68adc8c1b27.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libminiz_oxide-a8d5199c03b80566.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libadler-64842aa7c220460e.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libobject-193c992b516cb994.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libaddr2line-530bc5c0808f03e3.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libgimli-f1e3de972413762a.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libstd_detect-0708ef471f010f9e.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_demangle-e0ac20be4751c3d2.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libhashbrown-bfee6cb7e808ec7f.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_alloc-b22097ce75ccf616.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libunwind-1f58ee4ae7403119.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcfg_if-4a9ec9bb065104e9.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liblibc-683c2ad1ec876f13.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/liballoc-d5514ba8af77a08c.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/librustc_std_workspace_core-9af05494f0573e1b.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcore-60eab00334ec8288.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/libcompiler_builtins-7a2a7a2602cba6f5.rlib
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtendS.o
    ld.lld: /home/han/.rustup/toolchains/nightly-x86_64-unknown-linux-musl/lib/rustlib/x86_64-unknown-linux-musl/lib/self-contained/crtn.o
    
    INFO rustc_codegen_ssa::back::link linker stdout:
    
        Finished dev [unoptimized + debuginfo] target(s) in 0.39s
    

    韩朴宇 at 2021-05-23 09:42:58

  11. @rustbot claim

    Ilya Yanok at 2021-11-02 09:11:24

  12. @yanok are you still working on this?

    Chris Denton at 2022-03-02 15:25:40

  13. Yes, I have a commit for this bug. I wasn't happy about it somehow and wanted to do some more but I guess I will make a PR and discuss it with reviewers.

    Ilya Yanok at 2022-03-02 16:50:34

  14. Copying @japaric's useful tip for those of us that need the info before the request is implemented:

    FWIW, you can use rustc -Z print-link-args foo.rs to see the linker invocation.

    Was this removed at some point? because

    error: unknown unstable option: `print-link-args`
    
    $ rustc -V
    rustc 1.64.0-nightly (0f4bcadb4 2022-07-30)
    

    Yassine Oudjana at 2022-07-31 20:40:16

  15. It was stabilized as --print link-args

    Chris Denton at 2022-07-31 20:43:10

  16. It looks like there's a difference in --print link-args vs export RUSTC_LOG=rustc_codegen_ssa::back::link=info where with the env var if you also pass -Clink-args=-v you will only see the linkers stdout/stderr with the env var, which makes sense based on the link-args name, but depending on your use case you might want that too

    Keith Smiley at 2023-11-30 19:30:23

  17. #136098

    nora at 2025-01-26 16:42:40

  18. subtrees.....

    nora at 2025-02-20 14:23:32