Repository: rust
Issue: Passing `-C panic=abort` still attempts to link in `libunwind` when targeting `i686-pc-windows-gnu` on `v1.44+`
Labels: A-runtime,A-linkage,O-windows,A-cross,P-high,T-compiler,regression-from-stable-to-stable,C-bug,O-x86_32

### Summary

When cross-compiling from a Linux distribution that provides SLJL mingw32, linker errors about `libunwind` symbols are a known issue (https://github.com/rust-lang/rust/issues/12859).  The generally-accepted workaround is to disable exception handling (via `-C panic=abort`) which should disable the need to collect backtraces and eliminate the linker errors.  This works on a 1.43.0 toolchain, but is broken on 1.44.0-1.48.0

### Example of error
Here's an example of the error, running within the [`BinaryBuilder.jl` cross-compilation environment](https://github.com/JuliaPackaging/BinaryBuilder.jl).

```
# cat hello_world.rs 
fn main() {
    println!("Hello, World!");
}
# /opt/x86_64-linux-musl/bin/rustc --target=i686-pc-windows-gnu -C panic=abort -o /tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.exe hello_world.rs
error: linking with `i686-w64-mingw32-gcc` failed: exit code: 1
  |
  = note: "i686-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--nxcompat" "-Wl,--large-address-aware" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/rsbegin.o" "-L" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.0.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.1.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.2.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.3.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.4.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.5.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.6.rcgu.o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.hello_world.7rcbfp3g-cgu.7.rcgu.o" "-o" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.exe" "/tmp/testsuite/i686-w64-mingw32/rust/hello_world/hello_world.1oeskw8cnf06rbmk.rcgu.o" "-Wl,--gc-sections" "-nodefaultlibs" "-L" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib" "-Wl,--start-group" "-Wl,-Bstatic" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libpanic_abort-905e0827b1faa99b.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libobject-b5919c53897ea4e7.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libaddr2line-09e1099705854178.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libgimli-a13132083f96cf01.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/librustc_demangle-1a2a881500c3aa11.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libhashbrown-12335b7735858229.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/librustc_std_workspace_alloc-03e236d940d65c13.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libunwind-67ee36f8c83e0d23.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libcfg_if-551dfddd5bf52674.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/liblibc-119051673c0a64ec.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/liballoc-e4d213396c740246.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/librustc_std_workspace_core-09a82c5ce50e9376.rlib" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libcore-e59a5606ba4e2b3b.rlib" "-Wl,--end-group" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libcompiler_builtins-c634db1b8ae16db1.rlib" "-Wl,-Bdynamic" "-ladvapi32" "-lws2_32" "-luserenv" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "/opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/rsend.o"
  = note: /opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib(std-fe449066d03836b9.std.3uongtsb-cgu.0.rcgu.o): In function `ZN64_$LT$std..backtrace..BytesOrWide$u20$as$u20$core..fmt..Debug$GT$3fmt17h42ce8df8b153bc33E':
          /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src/backtrace.rs:231: undefined reference to `_Unwind_Resume'
          /opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib(std-fe449066d03836b9.std.3uongtsb-cgu.0.rcgu.o): In function `ZN4core3ops8function6FnOnce9call_once17h47abe59cb2be30c1E':
          /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops/function.rs:227: undefined reference to `_Unwind_Resume'
          /opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib(std-fe449066d03836b9.std.3uongtsb-cgu.0.rcgu.o): In function `ZN4core3ops8function6FnOnce9call_once17h48798ced7406d4f4E':
          /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\io/stdio.rs:563: undefined reference to `_Unwind_Resume'
          /opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib(std-fe449066d03836b9.std.3uongtsb-cgu.0.rcgu.o): In function `ZN4core3ops8function6FnOnce9call_once17h0a2adbee20aeb7b2E':
          /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops/function.rs:227: undefined reference to `_Unwind_Resume'
          /opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib(std-fe449066d03836b9.std.3uongtsb-cgu.0.rcgu.o): In function `ZN71_$LT$alloc..vec..IntoIter$LT$T$GT$$u20$as$u20$core..ops..drop..Drop$GT$4drop17h9354c90820e79e8dE':
          /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\alloc\src/vec.rs:3069: undefined reference to `_Unwind_Resume'
          /opt/x86_64-linux-musl/toolchains/1.48.0-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/libstd-fe449066d03836b9.rlib(std-fe449066d03836b9.std.3uongtsb-cgu.0.rcgu.o):/rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ptr/mod.rs:175: more undefined references to `_Unwind_Resume' follow
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error
```

### Testing with multiple versions

I use the following script to install new versions of `rustc`:

```bash
#!/bin/bash
# Usage: change_win32_toolchain.sh [version]

TOOLCHAIN_VER=${1:-1.48.0}
rustup toolchain add ${TOOLCHAIN_VER}
rustup target add --toolchain ${TOOLCHAIN_VER} i686-pc-windows-gnu

# Copy crt2.o in to rust, see https://github.com/rust-lang/rust/issues/32859#issuecomment-573423629
cp /opt/i686-w64-mingw32/i686-w64-mingw32/sys-root/lib/crt2.o /opt/x86_64-linux-musl/toolchains/${TOOLCHAIN_VER}-x86_64-unknown-linux-musl/lib/rustlib/i686-pc-windows-gnu/lib/crt2.o

# Set this new toolchain as the default for all invocations of `rustc`
export RUSTUP_TOOLCHAIN=${TOOLCHAIN_VER}-x86_64-unknown-linux-musl
```

### Version it worked on

This works on Rust 1.43.0.

### Version with regression

This does not work on Rust 1.44.0-1.48.0