Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x86_64-pc-windows-gnu can't link libtest with LTO=thin #104852

Closed
ehuss opened this issue Nov 25, 2022 · 11 comments · Fixed by #104855
Closed

x86_64-pc-windows-gnu can't link libtest with LTO=thin #104852

ehuss opened this issue Nov 25, 2022 · 11 comments · Fixed by #104855
Labels
C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.

Comments

@ehuss
Copy link
Contributor

ehuss commented Nov 25, 2022

x86_64-pc-windows-gnu is unable to build a test binary with LTO=thin.

Reproduction:

  1. touch foo.rs
  2. rustc +nightly-x86_64-pc-windows-gnu -Clto=thin --test foo.rs

Error:

error: linking with `x86_64-w64-mingw32-gcc` failed: exit code: 1
  |
  = note: "x86_64-w64-mingw32-gcc" "-fno-use-linker-plugin" "-Wl,--dynamicbase" "-Wl,--disable-auto-image-base" "-m64" "-Wl,--high-entropy-va" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained\\crt2.o" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsbegin.o" "C:\\Users\\User\\AppData\\Local\\Temp\\rustcSapTY6\\symbols.o" "foo.58ka7t7dknl8118b.rcgu.o" "foo.addr2line-14c0744e763fbcc5.addr2line.a755ea22-cgu.0.rcgu.o.rcgu.o" "foo.adler-67dc86611d2bf020.adler.5bb2de54-cgu.0.rcgu.o.rcgu.o" "foo.alloc-366e8128b0a2151f.alloc.0e2959ea-cgu.0.rcgu.o.rcgu.o" "foo.cfg_if-22b8dcc08139ebd4.cfg_if.4a5ba149-cgu.0.rcgu.o.rcgu.o" "foo.core-0e5c5feeef4bd6da.core.35109205-cgu.0.rcgu.o.rcgu.o" "foo.foo.f94f5f32-cgu.0.rcgu.o" "foo.foo.f94f5f32-cgu.1.rcgu.o" "foo.foo.f94f5f32-cgu.2.rcgu.o" "foo.foo.f94f5f32-cgu.3.rcgu.o" "foo.foo.f94f5f32-cgu.4.rcgu.o" "foo.getopts-7a2ff2879c4becfe.getopts.27ae8826-cgu.0.rcgu.o.rcgu.o" "foo.gimli-281c67c7290209a3.gimli.b3006d85-cgu.0.rcgu.o.rcgu.o" "foo.hashbrown-73dc3622ac20c1ad.hashbrown.4fd96bfe-cgu.0.rcgu.o.rcgu.o" "foo.libc-8e4d63370c2f2f4b.libc.43b5f33f-cgu.0.rcgu.o.rcgu.o" "foo.memchr-4164db77eca254c1.memchr.bf2fc5a0-cgu.0.rcgu.o.rcgu.o" "foo.miniz_oxide-753b0ac42b327310.miniz_oxide.f6d02d85-cgu.0.rcgu.o.rcgu.o" "foo.object-fd18c1e2427b7d66.object.c948ec4e-cgu.0.rcgu.o.rcgu.o" "foo.panic_unwind-cd212035a0d658c8.panic_unwind.b47531a0-cgu.0.rcgu.o.rcgu.o" "foo.rustc_demangle-378d74d608afb5ce.rustc_demangle.15cbba0f-cgu.0.rcgu.o.rcgu.o" "foo.rustc_std_workspace_alloc-0279e801977a0519.rustc_std_workspace_alloc.3d4a570e-cgu.0.rcgu.o.rcgu.o" "foo.rustc_std_workspace_core-043dcb5cef4e65e2.rustc_std_workspace_core.7d396b72-cgu.0.rcgu.o.rcgu.o" "foo.rustc_std_workspace_std-924aa08e8fb30081.rustc_std_workspace_std.cfad5f07-cgu.0.rcgu.o.rcgu.o" "foo.std-67692f6c6473d324.std.14d27bce-cgu.0.rcgu.o.rcgu.o" "foo.std_detect-ac29a076266feb38.std_detect.3a222bf1-cgu.0.rcgu.o.rcgu.o" "foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o" "foo.unicode_width-4cda36558020d9a8.unicode_width.7d30b2de-cgu.0.rcgu.o.rcgu.o" "foo.unwind-d6953a7e39d3701d.unwind.aac5024c-cgu.0.rcgu.o.rcgu.o" "-L" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-Wl,-Bstatic" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\libcompiler_builtins-1e8650baf8073116.rlib" "-Wl,-Bdynamic" "-lkernel32" "-lkernel32" "-ladvapi32" "-luserenv" "-lkernel32" "-lws2_32" "-lbcrypt" "-lgcc_eh" "-l:libpthread.a" "-lmsvcrt" "-lmingwex" "-lmingw32" "-lgcc" "-lmsvcrt" "-luser32" "-lkernel32" "-Wl,--nxcompat" "-nostartfiles" "-L" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib" "-L" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\self-contained" "-o" "foo.exe" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "C:\\Users\\User\\.rustup\\toolchains\\nightly-x86_64-pc-windows-gnu\\lib\\rustlib\\x86_64-pc-windows-gnu\\lib\\rsend.o"
  = note: foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `core::sync::atomic::atomic_load::h1ce09cb4e3d2295f':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\core\src\sync/atomic.rs:2988: undefined reference to `__imp__ZN3std11collections4hash3map11RandomState3new4KEYS7__getit5__KEY17h4d133909dffb598dE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::sys::windows::thread_local_key::StaticKey::key::h5a360baef9e4385d':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: undefined reference to `__imp__ZN3std11collections4hash3map11RandomState3new4KEYS7__getit5__KEY17h4d133909dffb598dE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `core::sync::atomic::atomic_load::h1ce09cb4e3d2295f':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\core\src\sync/atomic.rs:2988: undefined reference to `__imp__ZN3std11collections4hash3map11RandomState3new4KEYS7__getit5__KEY17h4d133909dffb598dE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::sys::windows::thread_local_key::StaticKey::key::h5a360baef9e4385d':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: undefined reference to `__imp__ZN3std11collections4hash3map11RandomState3new4KEYS7__getit5__KEY17h4d133909dffb598dE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::thread::local::os::Key$LT$T$GT$::try_initialize::ha296643cb4ec4997':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\thread/local.rs:1118: undefined reference to `__imp__ZN3std11collections4hash3map11RandomState3new4KEYS7__getit5__KEY17h4d133909dffb598dE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o:/rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: more undefined references to `__imp__ZN3std11collections4hash3map11RandomState3new4KEYS7__getit5__KEY17h4d133909dffb598dE' follow
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `core::sync::atomic::atomic_load::h1ce09cb4e3d2295f':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\core\src\sync/atomic.rs:2988: undefined reference to `__imp__ZN3std4sync4mpmc5waker17current_thread_id5DUMMY7__getit5__KEY17h1ce21b2969d5bc2bE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::sys::windows::thread_local_key::StaticKey::key::h5a360baef9e4385d':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: undefined reference to `__imp__ZN3std4sync4mpmc5waker17current_thread_id5DUMMY7__getit5__KEY17h1ce21b2969d5bc2bE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `core::sync::atomic::atomic_load::h1ce09cb4e3d2295f':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\core\src\sync/atomic.rs:2988: undefined reference to `__imp__ZN3std4sync4mpmc5waker17current_thread_id5DUMMY7__getit5__KEY17h1ce21b2969d5bc2bE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::sys::windows::thread_local_key::StaticKey::key::h5a360baef9e4385d':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: undefined reference to `__imp__ZN3std4sync4mpmc5waker17current_thread_id5DUMMY7__getit5__KEY17h1ce21b2969d5bc2bE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::thread::local::os::Key$LT$T$GT$::try_initialize::h2eff52084c9a2c81':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\thread/local.rs:1118: undefined reference to `__imp__ZN3std4sync4mpmc5waker17current_thread_id5DUMMY7__getit5__KEY17h1ce21b2969d5bc2bE'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o:/rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: more undefined references to `__imp__ZN3std4sync4mpmc5waker17current_thread_id5DUMMY7__getit5__KEY17h1ce21b2969d5bc2bE' follow
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `core::sync::atomic::atomic_load::h1ce09cb4e3d2295f':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\core\src\sync/atomic.rs:2988: undefined reference to `__imp__ZN3std4sync4mpmc7context7Context4with7CONTEXT7__getit5__KEY17h8bce9e2e01c30f57E'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::sys::windows::thread_local_key::StaticKey::key::h5a360baef9e4385d':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: undefined reference to `__imp__ZN3std4sync4mpmc7context7Context4with7CONTEXT7__getit5__KEY17h8bce9e2e01c30f57E'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `core::sync::atomic::atomic_load::h1ce09cb4e3d2295f':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\core\src\sync/atomic.rs:2988: undefined reference to `__imp__ZN3std4sync4mpmc7context7Context4with7CONTEXT7__getit5__KEY17h8bce9e2e01c30f57E'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::sys::windows::thread_local_key::StaticKey::key::h5a360baef9e4385d':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: undefined reference to `__imp__ZN3std4sync4mpmc7context7Context4with7CONTEXT7__getit5__KEY17h8bce9e2e01c30f57E'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o: In function `std::thread::local::os::Key$LT$T$GT$::try_initialize::hc190752f4af349b0':
          /rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\thread/local.rs:1118: undefined reference to `__imp__ZN3std4sync4mpmc7context7Context4with7CONTEXT7__getit5__KEY17h8bce9e2e01c30f57E'
          foo.test-05c5485ddbae42f3.test.776f7fa2-cgu.0.rcgu.o.rcgu.o:/rustc/70f8737b2f5d3bf7d6b784fad00b663b7ff9feda/library\std\src\sys\windows/thread_local_key.rs:79: more undefined references to `__imp__ZN3std4sync4mpmc7context7Context4with7CONTEXT7__getit5__KEY17h8bce9e2e01c30f57E' follow

  = 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)

Regression started with #101368.

Meta

rustc --version --verbose:

rustc 1.67.0-nightly (70f8737b2 2022-11-23)
binary: rustc
commit-hash: 70f8737b2f5d3bf7d6b784fad00b663b7ff9feda
commit-date: 2022-11-23
host: x86_64-pc-windows-gnu
release: 1.67.0-nightly
LLVM version: 15.0.4
@ehuss ehuss added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. C-bug Category: This is a bug. labels Nov 25, 2022
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 25, 2022
weihanglo added a commit to weihanglo/cargo that referenced this issue Nov 25, 2022
@thomcc
Copy link
Member

thomcc commented Nov 25, 2022

This pretty much sucks since it seems like a rustc bug preventing a (admittedly imperfect) soundness fix, but @nikic (#101368 (comment)) and @eddyb (#101368 (comment)) seemed to have a other ideas for how to prevent that issue from happening, so maybe we take the revert here and wait til something can try one of those.

CC @ChrisDenton

@ChrisDenton
Copy link
Member

As I said on the revert, I think we do need a proper fix to the underlying issue rather than attempting libs workarounds.

@bors bors closed this as completed in af63e3b Nov 25, 2022
@thomcc
Copy link
Member

thomcc commented Nov 25, 2022

Hmm, I wonder if we could just do it for target_env = "msvc", since that would at least fix the user's problem in #101368 (comment).

@ChrisDenton
Copy link
Member

Maybe. My concern now is that we maybe end up essentially pushing the miscompilation around. So we workaround one problem but maybe we cause another, harder to diagnose, problem somewhere else.

That said, I'm not totally against the idea. Perhaps its a trade off we should make. It doesn't sit particularly well with me though I guess there aren't a lot of good options here.

@ChrisDenton
Copy link
Member

Hm, well, thinking about it after a cup of coffee... maybe it's the best trade off. But rather than target_env just don't set it for any windows target that's not also target_thread_local. Something like:

#[cfg_attr(not(windows), inline)]
#[cfg_attr(all(windows, target_thread_local), inline(never))]

Though I'm still not entirely comfortable with all this.

@thomcc
Copy link
Member

thomcc commented Nov 25, 2022

Yeah, good idea.

@thomcc
Copy link
Member

thomcc commented Nov 25, 2022

I'll wait on PRing that until I can test the patch in a few different build configurations (to avoid a repeat). That will probably be later this weekend.

@mati865
Copy link
Contributor

mati865 commented Nov 25, 2022

Why would it be compiler bug?
If we look at expanded code, the mentioned PR has changed it on *-windows-gnu from:

            unsafe fn __getit(
                init: $crate::option::Option<&mut $crate::option::Option<$t>>,
            ) -> $crate::option::Option<&'static $t> {

to:

            #[inline]
            unsafe fn __getit(
                init: $crate::option::Option<&mut $crate::option::Option<$t>>,
            ) -> $crate::option::Option<&'static $t> {

By the way looks like this could use some unit test to avoid regressions in the future.

@ChrisDenton
Copy link
Member

ChrisDenton commented Nov 25, 2022

Why would it be compiler bug?

Using the inline attribute on __getit should not be causing linker errors. That is not desirable and so I would personally consider it a bug.

@mati865
Copy link
Contributor

mati865 commented Nov 25, 2022

Without target_thread_local this is has a static symbol (so it has to use dllimport/dllexport) and when you are telling compiler to inline it it can easily confuse the linker like we do see with ld.bfd.
Note the same error doesn't appear when linking with LLD.

@ChrisDenton
Copy link
Member

But that's a compiler issue. If the gnu linker cannot handle the inlining, then surely gnu toolchains should not be inlining it? The inline attribute does not mean "you must inline this, no matter if it's nonsensical", no?

#[inline] is an optimization hint; from a libs perspective I wouldn't expect it to have any effect on whether or not a program compiles.

@apiraino apiraino removed the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Nov 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. regression-from-stable-to-nightly Performance or correctness regression from stable to nightly.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants