Repository: rust
Issue: Panic when trying to link a static library
Labels: A-linkage,I-ICE,T-compiler,C-bug,S-needs-repro

<!--
Thank you for finding an Internal Compiler Error! 🧊  If possible, try to provide
a minimal verifiable example. You can read "Rust Bug Minimization Patterns" for
how to create smaller examples.
http://blog.pnkfx.org/blog/2019/11/18/rust-bug-minimization-patterns/
-->

### Code

```Rust
use std::{env, path::Path};

fn main() {
    let cargo_manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();

    println!(
        "cargo:rustc-link-search={}",
        Path::new(&cargo_manifest_dir)
            .join("lib")
            .display()
    );

    println!(
        "cargo:rustc-link-lib=static:+verbatim=mylib.a",
    );
}

```


### Meta
<!--
If you're using the stable version of the compiler, you should also check if the
bug also exists in the beta or nightly versions.
-->

`rustc --version --verbose`:
```
rustc 1.74.0-nightly (e4133ba9b 2023-09-22)
binary: rustc
commit-hash: e4133ba9b1a150ef624f5d189913a37405f4414c
commit-date: 2023-09-22
host: aarch64-apple-darwin
release: 1.74.0-nightly
LLVM version: 17.0.0
```

I'm pretty sure I could also reproduce this on the nightly build of 1.68 from 2022-12-15.

### Error output

```
thread 'rustc' panicked at compiler/rustc_codegen_llvm/src/back/archive.rs:397:67:
called `Option::unwrap()` on a `None` value
```

<!--
Include a backtrace in the code block by setting `RUST_BACKTRACE=1` in your
environment. E.g. `RUST_BACKTRACE=1 cargo build`.
-->
<details><summary><strong>Backtrace</strong></summary>
<p>

```
stack backtrace:
   0:        0x1052cc53c - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h888d5accfa36c168
   1:        0x10531ffb8 - core::fmt::write::h04d9f73f7e836658
   2:        0x1052c20ec - std::io::Write::write_fmt::h94e1673c729f9317
   3:        0x1052cc37c - std::sys_common::backtrace::print::h10a4fbdfaaae2dd2
   4:        0x1052cef78 - std::panicking::default_hook::{{closure}}::h7765bf8325104f29
   5:        0x1052cec90 - std::panicking::default_hook::hb5f126536978c525
   6:        0x10d8685e8 - <alloc[9034f14e527f4aec]::boxed::Box<rustc_driver_impl[36007714369cb99c]::install_ice_hook::{closure#0}> as core[f9fe6632a5d6f467]::ops::function::Fn<(&dyn for<'a, 'b> core[f9fe6632a5d6f467]::ops::function::Fn<(&'a core[f9fe6632a5d6f467]::panic::panic_info::PanicInfo<'b>,), Output = ()> + core[f9fe6632a5d6f467]::marker::Sync + core[f9fe6632a5d6f467]::marker::Send, &core[f9fe6632a5d6f467]::panic::panic_info::PanicInfo)>>::call
   7:        0x1052cf710 - std::panicking::rust_panic_with_hook::h324900f77a570bad
   8:        0x1052cf49c - std::panicking::begin_panic_handler::{{closure}}::he54b71682c96c343
   9:        0x1052cc9c8 - std::sys_common::backtrace::__rust_end_short_backtrace::h49b09aa4dc6f1101
  10:        0x1052cf248 - _rust_begin_unwind
  11:        0x10534b320 - core::panicking::panic_fmt::hfb1f3dbf27828692
  12:        0x10534b394 - core::panicking::panic::hae950524f84a2ba6
  13:        0x10da8154c - <rustc_codegen_llvm[e9fef58160de1096]::back::archive::LlvmArchiveBuilder as rustc_codegen_ssa[bcfeca12232ca141]::back::archive::ArchiveBuilder>::build
  14:        0x110d00840 - rustc_codegen_ssa[bcfeca12232ca141]::back::link::link_binary
  15:        0x10dad6dfc - <rustc_codegen_llvm[e9fef58160de1096]::LlvmCodegenBackend as rustc_codegen_ssa[bcfeca12232ca141]::traits::backend::CodegenBackend>::link
  16:        0x10d9bcf6c - <rustc_interface[609b5eee07ebfc53]::queries::Linker>::link
  17:        0x10d881ff4 - rustc_span[b8fa0d11b7a16384]::set_source_map::<core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>, rustc_interface[609b5eee07ebfc53]::interface::run_compiler<core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>, rustc_driver_impl[36007714369cb99c]::run_compiler::{closure#1}>::{closure#0}::{closure#0}>
  18:        0x10d8664b4 - std[7f4e316fde1c09b8]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[609b5eee07ebfc53]::util::run_in_thread_with_globals<rustc_interface[609b5eee07ebfc53]::interface::run_compiler<core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>, rustc_driver_impl[36007714369cb99c]::run_compiler::{closure#1}>::{closure#0}, core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>>
  19:        0x10d867624 - <<std[7f4e316fde1c09b8]::thread::Builder>::spawn_unchecked_<rustc_interface[609b5eee07ebfc53]::util::run_in_thread_with_globals<rustc_interface[609b5eee07ebfc53]::interface::run_compiler<core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>, rustc_driver_impl[36007714369cb99c]::run_compiler::{closure#1}>::{closure#0}, core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[f9fe6632a5d6f467]::result::Result<(), rustc_span[b8fa0d11b7a16384]::ErrorGuaranteed>>::{closure#1} as core[f9fe6632a5d6f467]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  20:        0x1052d85b8 - std::sys::unix::thread::Thread::new::thread_start::hb31de041cbbaf8e4
  21:        0x1acbd9240 - __pthread_deallocate

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: please attach the file at `/Users/py/projects/paulyoung/power-player/main/rustc-ice-2023-09-23T05:08:39.149995Z-34553.txt` to your bug report

note: compiler flags: --crate-type staticlib --crate-type cdylib -C opt-level=z -C panic=abort -C lto -Z unstable-options -C target-cpu=cortex-m7 -C link-args=--emit-relocs -C relocation-model=pic -C panic=abort

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
end of query stack
warning: `power_player` (lib) generated 2 warnings
error: could not compile `power_player` (lib); 2 warnings emitted
Error: cargo failed with error ExitStatus(unix_wait_status(25856))
```

</p>
</details>

### Relevant compiler source code

https://github.com/rust-lang/rust/blob/e4133ba9b1a150ef624f5d189913a37405f4414c/compiler/rustc_codegen_llvm/src/back/archive.rs#L390-L397