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

build-std panic_immediate_abort undefined reference to panic_cannot_unwind #107016

Closed
ehuss opened this issue Jan 18, 2023 · 11 comments
Closed

build-std panic_immediate_abort undefined reference to panic_cannot_unwind #107016

ehuss opened this issue Jan 18, 2023 · 11 comments
Labels
-Zbuild-std Unstable Cargo option: Compile the standard library yourself. C-bug Category: This is a bug.

Comments

@ehuss
Copy link
Contributor

ehuss commented Jan 18, 2023

A Cargo project with:

fn main() {
    panic!("foo");
}

built with:

cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu

results in a linking failure:

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH="/home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/bin:/home/eric/.cargo/bin:/home/eric/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin" VSLANG="1033" "cc" "-m64" "/tmp/rustcFgLEbF/symbols.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.1zyytfss6ge6z1c2.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.2cgsr7cusby7dzxg.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.2rjthrgaumg840ve.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.32emvdk76bp91vcu.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.3cj49qdg70f0gyk6.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.3p0mbady7r38kwwe.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.3poyfj1g4stxz92z.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.4yr89ousk3c7d2jy.rcgu.o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349.50cbq5av1898h8h6.rcgu.o" "-Wl,--as-needed" "-L" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps" "-L" "/home/eric/Temp/z5/target/debug/deps" "-L" "/home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libstd-71ebc58fe381d1e2.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libpanic_unwind-3df6cc821977c293.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/librustc_demangle-666a7821c202eadd.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libstd_detect-da0d60497b1c7e5c.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libhashbrown-59ad5acb3417a67f.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/librustc_std_workspace_alloc-afb6c9f84f868152.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libunwind-e44bde4753f5d656.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libcfg_if-947c10ee011f75a2.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liblibc-c433df73698cac4e.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/librustc_std_workspace_core-b0197b2ea51ce186.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libcore-d4d5a26fa7790993.rlib" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/libcompiler_builtins-aa52e866272be7f0.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-znoexecstack" "-L" "/home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "/home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349" "-Wl,--gc-sections" "-pie" "-Wl,-zrelro,-znow" "-nodefaultlibs"
  = note: /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib(alloc-86b4281adfdfc05f.alloc.d0a1cb85-cgu.0.rcgu.o): in function `alloc::raw_vec::RawVec<T,A>::into_box':
          /home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/raw_vec.rs:154: undefined reference to `core::panicking::panic_cannot_unwind'
          /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib(alloc-86b4281adfdfc05f.alloc.d0a1cb85-cgu.1.rcgu.o): in function `core::ptr::drop_in_place<alloc::vec::Vec<u8>>':
          /home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ptr/mod.rs:490: undefined reference to `core::panicking::panic_cannot_unwind'
          /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib(alloc-86b4281adfdfc05f.alloc.d0a1cb85-cgu.11.rcgu.o): in function `<T as alloc::slice::hack::ConvertVec>::to_vec':
          /home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/slice.rs:159: undefined reference to `core::panicking::panic_cannot_unwind'
          /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib(alloc-86b4281adfdfc05f.alloc.d0a1cb85-cgu.2.rcgu.o): in function `alloc::string::String::from_utf8_lossy':
          /home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/string.rs:631: undefined reference to `core::panicking::panic_cannot_unwind'
          /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib(alloc-86b4281adfdfc05f.alloc.d0a1cb85-cgu.4.rcgu.o): in function `alloc::vec::Vec<T,A>::into_boxed_slice':
          /home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/vec/mod.rs:1103: undefined reference to `core::panicking::panic_cannot_unwind'
          /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/liballoc-86b4281adfdfc05f.rlib(alloc-86b4281adfdfc05f.alloc.d0a1cb85-cgu.7.rcgu.o):/home/eric/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/boxed.rs:1124: more undefined references to `core::panicking::panic_cannot_unwind' follow
          /usr/bin/ld: /home/eric/Temp/z5/target/x86_64-unknown-linux-gnu/debug/deps/z5-76afd39352411349: hidden symbol `_ZN4core9panicking19panic_cannot_unwind17h206df51ed63dec18E' isn't defined
          /usr/bin/ld: final link failed: bad value
          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)

This recently started with #104999. cc @saethlin do you have any insight into this?

Meta

rustc --version --verbose:

rustc 1.68.0-nightly (3984bc583 2023-01-17)
binary: rustc
commit-hash: 3984bc5833db8bfb0acc522c9775383e4171f3de
commit-date: 2023-01-17
host: x86_64-unknown-linux-gnu
release: 1.68.0-nightly
LLVM version: 15.0.6
@ehuss ehuss added the C-bug Category: This is a bug. label Jan 18, 2023
@saethlin
Copy link
Member

do you have any insight

Not particularly. All I can say is this diff makes the example program link fine:

diff --git a/library/core/src/panicking.rs b/library/core/src/panicking.rs
index 48e90e6d794..3b7e6882107 100644
--- a/library/core/src/panicking.rs
+++ b/library/core/src/panicking.rs
@@ -164,7 +164,6 @@ fn panic_bounds_check(index: usize, len: usize) -> ! {
 /// This function is called directly by the codegen backend, and must not have
 /// any extra arguments (including those synthesized by track_caller).
 #[cfg_attr(not(feature = "panic_immediate_abort"), inline(never), cold)]
-#[cfg_attr(feature = "panic_immediate_abort", inline)]
 #[cfg_attr(bootstrap, lang = "panic_no_unwind")] // needed by codegen for panic in nounwind function
 #[cfg_attr(not(bootstrap), lang = "panic_cannot_unwind")] // needed by codegen for panic in nounwind function
 #[rustc_nounwind]

But that doesn't really make any sense to me. There are a lot of other panic lang items which are fine with #[inline], and I don't understand what is special about this one.

@Super-Pizza
Copy link

Super-Pizza commented Jan 21, 2023

Looking inside crate/target/target/debug/deps/libpanic_abort-XXXXXXXXXXXXXXXX.rlib (it's an archive, use ar xo <file> for that), there are a few object files.
I looked inside with objdump -x <object> to find the symbols
And in some of them there's a symbol _ZN4core9panicking15panic_no_unwind17h129a8875f76752ecE
that demangles to core::panicking::panic_no_unwind::h129a8875f76752ec here, which is weird

@saethlin
Copy link
Member

But the symbol we want is core::panicking::panic_cannot_unwind, right?

@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2023

There is both panic_no_unwind and panic_cannot_unwind. Both are lang items used by codegen backends and the later calls the former with a fixed argument.

@bjorn3
Copy link
Member

bjorn3 commented Jan 21, 2023

I think the monomorphization collector forgot to consider panic_cannot_unwind used in some cases. It is handled for TerminatorKind::Abort:

mir::TerminatorKind::Abort { .. } => {
let instance = Instance::mono(
tcx,
tcx.require_lang_item(LangItem::PanicCannotUnwind, Some(source)),
);
if should_codegen_locally(tcx, &instance) {
self.output.push(create_fn_mono_item(tcx, instance, source));
}
}
But it isn't handled for the double_unwind_guard emitted for calls in cleanup blocks:
} else if fx.mir[self.bb].is_cleanup
&& fn_abi.can_unwind
&& !base::wants_msvc_seh(fx.cx.tcx().sess)
{
// Exception must not propagate out of the execution of a cleanup (doing so
// can cause undefined behaviour). We insert a double unwind guard for
// functions that can potentially unwind to protect against this.
//
// This is not necessary for SEH which does not use successive unwinding
// like Itanium EH. EH frames in SEH are different from normal function
// frames and SEH will abort automatically if an exception tries to
// propagate out from cleanup.
Some(fx.double_unwind_guard())
} else {

@tmiasko
Copy link
Contributor

tmiasko commented Jan 23, 2023

Using -Cpanic=abort works around the issue, and probably something one would like to do this scenario regardless?

@saethlin
Copy link
Member

saethlin commented Jan 23, 2023

🤦 Yes, definitely. This is not the first time I have found odd behavior from mixing unwind with panic_immediate_abort. I think it would make sense for something somewhere to reject that configuration.

@tmiasko
Copy link
Contributor

tmiasko commented Jan 24, 2023

Might be worth retesting when #102906 lands. I think it should address this issue as well.

@tmiasko
Copy link
Contributor

tmiasko commented Mar 9, 2023

I think it would make sense for something somewhere to reject that configuration.

#108924 proposes a static assertion that ensures that panic_immediate_abort is used with -Cpanic=abort.

@workingjubilee workingjubilee added the -Zbuild-std Unstable Cargo option: Compile the standard library yourself. label Mar 13, 2023
@tmiasko tmiasko removed their assignment Mar 24, 2023
@SUPERCILEX
Copy link
Contributor

Can this issue be closed since the PR was merged?

@ehuss
Copy link
Contributor Author

ehuss commented Mar 31, 2023

Yea, I think this is effectively solved by #108924. Thanks!
(Although anyone should feel free to reopen if you feel there is more to do here.)

@ehuss ehuss closed this as completed Mar 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
-Zbuild-std Unstable Cargo option: Compile the standard library yourself. C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

7 participants