Repository: rust
Issue: Linking error when mixing extern crate and dylib with lto enabled
Labels: A-linkage,T-compiler,C-bug,E-needs-mcve

<!--
Thank you for filing a bug report! 🐛 Please provide a short summary of the bug,
along with any information you feel relevant to replicating the bug.
-->
Hi, I created a minimal example at:
[https://github.com/Guilucand/rust-linking-bug](url)

The repository consists in a workspace with a binary (bin1) and three libraries:
- lib1 is a dependency of both bin1 and lib2, and exports a single function called only by bin1
- lib2 is a dependency of bin1, and imports lib1 and lib3_dylib (lib1 with `extern crate lib1;`) and exports a function called by bin1
- lib3_dylib is an empty dylib type crate 

The bug happens only when the code is compiled in release mode with thin or fat lto.
The bug does not happen if the lib3_dylib crate-type is not a dylib or if either line 4 or 7 of libs/lib2/src/lib.rs is commented.

To me, the problem seems a strange interaction between the dylib and the extern crate statement. 

I expected to see this happen: code compiles successfully

Instead, this happened:
<details><summary>Compiler output</summary>
<p>

```
cargo clean && cargo +nightly run --release --bin bin1
   Compiling lib1 v0.1.0 (/rust-linking-bug/libs/lib1)
   Compiling lib3_dylib v0.1.0 (/rust-linking-bug/libs/lib3_dylib)
   Compiling lib2 v0.1.0 (/rust-linking-bug/libs/lib2)
warning: unused import: `lib3_dylib::*`
 --> libs/lib2/src/lib.rs:7:5
  |
7 | use lib3_dylib::*;
  |     ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: `lib2` (lib) generated 1 warning
   Compiling bin1 v0.1.0 (/rust-linking-bug/bin1)
error: linking with `cc` failed: exit status: 1
  |
  = note: "cc" "-m64" "/tmp/rustcj63qcl/symbols.o" "/rust-linking-bug/target/release/deps/bin1-21b67fa9867c6b5f.bin1.885c23bf-cgu.0.rcgu.o" "-Wl,--as-needed" "-L" "/rust-linking-bug/target/release/deps" "-L" "/<home>/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "/rust-linking-bug/target/release/deps" "-llib3_dylib" "-L" "/<home>/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-lstd-de75e80c43801b1c" "-Wl,-Bstatic" "/<home>/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-b7c79d85cf21a511.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/<home>/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/rust-linking-bug/target/release/deps/bin1-21b67fa9867c6b5f" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-Wl,-O1" "-nodefaultlibs"
  = note: /usr/bin/ld: /rust-linking-bug/target/release/deps/bin1-21b67fa9867c6b5f.bin1.885c23bf-cgu.0.rcgu.o: in function `bin1::main':
          bin1.885c23bf-cgu.0:(.text._ZN4bin14main17h9514d892359a2ce5E+0x4): undefined reference to `lib1::lib1_function'
          collect2: error: ld returned 1 exit status
          
  = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  = note: use the `-l` flag to specify native libraries to link
  = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scripts.html#cargorustc-link-libkindname)

error: could not compile `bin1` due to previous error
```

</p>
</details>



### 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.66.0-nightly (0da281b60 2022-10-27)
binary: rustc
commit-hash: 0da281b6068a7d889ae89a9bd8991284cc9b7535
commit-date: 2022-10-27
host: x86_64-unknown-linux-gnu
release: 1.66.0-nightly
LLVM version: 15.0.2
```
and
```
rustc 1.64.0 (a55dd71d5 2022-09-19)
binary: rustc
commit-hash: a55dd71d5fb0ec5a6a3a9e8c27b2127ba491ce52
commit-date: 2022-09-19
host: x86_64-unknown-linux-gnu
release: 1.64.0
LLVM version: 14.0.6
```