Repository: rust
Issue: Specific code layout can cause link failure with lto=true
Labels: A-linkage,T-compiler,C-bug

This is the same code layout as #59137, with slightly more of my actual code - turns out that's not the only issue preventing me from using LTO.

I have a three-crate workspace; only two of these crates are relevant for this issue (if the code *ran*, the third would be relevant).

- `parent`, a binary, depending on `shared` and `libloading`, using the latter to load `child`
- `shared`, a library, with `crate-type = ["dylib"]`
- `child`, a library, also with `crate-type = ["dylib"]`. This crate can be deleted without changing the compile behaviour; it is only relevant at runtime.

https://github.com/GinjaNinja32/rust-issues/tree/62479

I expected to see this happen: Successful compile, i.e.
```
$ cargo build --release
   Compiling cc v1.0.37
   Compiling shared v0.1.0 (/home/nyx/git/rust-issues/shared)
   Compiling child v0.1.0 (/home/nyx/git/rust-issues/child)
   Compiling libloading v0.5.2
   Compiling parent v0.1.0 (/home/nyx/git/rust-issues/parent)
    Finished release [optimized] target(s) in 5.62s
```

Instead, this happened: Linking failed:
```
$ cargo build --release
   Compiling cc v1.0.37
   Compiling shared v0.1.0 (/home/nyx/git/rust-issues/shared)
   Compiling child v0.1.0 (/home/nyx/git/rust-issues/child)
   Compiling libloading v0.5.2
   Compiling parent v0.1.0 (/home/nyx/git/rust-issues/parent)
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/home/nyx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/home/nyx/git/rust-issues/target/release/deps/parent-e24cfa8e761d50d8.parent.6gomyrbo-cgu.6.rcgu.o" "-o" "/home/nyx/git/rust-issues/target/release/deps/parent-e24cfa8e761d50d8" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro" "-Wl,-znow" "-Wl,-O1" "-nodefaultlibs" "-L" "/home/nyx/git/rust-issues/target/release/deps" "-L" "/home/nyx/git/rust-issues/target/release/build/libloading-fb9b968fc3777b66/out" "-L" "/home/nyx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-L" "/home/nyx/git/rust-issues/target/release/deps" "-lshared" "-Wl,-Bstatic" "/tmp/rustcht55QS/liblibloading-6ea24a0114b349f0.rlib" "-Wl,--start-group" "-L" "/home/nyx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bdynamic" "-lstd-9895e8982b0a79e7" "-Wl,--end-group" "-Wl,-Bstatic" "/home/nyx/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-38e90baf978bc428.rlib" "-Wl,-Bdynamic" "-ldl" "-ldl" "-lrt" "-lpthread" "-lgcc_s" "-lc" "-lm" "-lrt" "-lpthread" "-lutil" "-lutil"
  = note: /usr/bin/ld: /home/nyx/git/rust-issues/target/release/deps/parent-e24cfa8e761d50d8.parent.6gomyrbo-cgu.6.rcgu.o: in function `core::ptr::real_drop_in_place':
          parent.6gomyrbo-cgu.6:(.text._ZN4core3ptr18real_drop_in_place17ha2fb5a25020df4d8E+0xc): undefined reference to `<libloading::os::unix::Library as core::ops::drop::Drop>::drop'
          /usr/bin/ld: /home/nyx/git/rust-issues/target/release/deps/parent-e24cfa8e761d50d8.parent.6gomyrbo-cgu.6.rcgu.o: in function `core::ptr::real_drop_in_place':
          parent.6gomyrbo-cgu.6:(.text._ZN4core3ptr18real_drop_in_place17hf2d0808cb9ce830eE+0x2): undefined reference to `<libloading::os::unix::DlerrorMutexGuard as core::ops::drop::Drop>::drop'
          /usr/bin/ld: /home/nyx/git/rust-issues/target/release/deps/parent-e24cfa8e761d50d8.parent.6gomyrbo-cgu.6.rcgu.o: in function `parent::main':
          parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0x2a): undefined reference to `libloading::util::cstr_cow_from_bytes'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0x68): undefined reference to `libloading::os::unix::DlerrorMutexGuard::new'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0xdc): undefined reference to `<libloading::os::unix::DlerrorMutexGuard as core::ops::drop::Drop>::drop'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0xf3): undefined reference to `<libloading::Library as core::convert::From<libloading::os::unix::Library>>::from'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0x117): undefined reference to `<libloading::Library as core::fmt::Debug>::fmt'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0x163): undefined reference to `<libloading::os::unix::Library as core::ops::drop::Drop>::drop'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0x2a8): undefined reference to `<libloading::os::unix::Library as core::ops::drop::Drop>::drop'
          /usr/bin/ld: parent.6gomyrbo-cgu.6:(.text._ZN6parent4main17h1271b7f81bb8859cE+0x2b8): undefined reference to `libloading::util::<impl core::convert::From<libloading::util::NullError> for std::io::error::Error>::from'
          /usr/bin/ld: /home/nyx/git/rust-issues/target/release/deps/parent-e24cfa8e761d50d8.parent.6gomyrbo-cgu.6.rcgu.o: in function `core::ptr::real_drop_in_place':
          parent.6gomyrbo-cgu.6:(.text._ZN4core3ptr18real_drop_in_place17h414db0f3839003feE+0x2): undefined reference to `<libloading::os::unix::Library as core::ops::drop::Drop>::drop'
          collect2: error: ld returned 1 exit status
          

error: aborting due to previous error

error: Could not compile `parent`.

To learn more, run the command again with --verbose.
```

## Meta

`rustc --version --verbose`:
```
rustc 1.36.0 (a53f9df32 2019-07-03)
binary: rustc
commit-hash: a53f9df32fbb0b5f4382caaad8f1a46f36ea887c
commit-date: 2019-07-03
host: x86_64-unknown-linux-gnu
release: 1.36.0
LLVM version: 8.0
```
(as used above)

also occurs on:
```
rustc 1.38.0-nightly (dfd52ba6a 2019-07-06)
binary: rustc
commit-hash: dfd52ba6ac2262c6b61c59ec86bfd23e4e53d3de
commit-date: 2019-07-06
host: x86_64-unknown-linux-gnu
release: 1.38.0-nightly
LLVM version: 8.0
```