Skip to content

build-std panic_immediate_abort undefined reference to panic_cannot_unwind #107016

Closed

Description

@ehuss

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

Activity

  1. saethlin commented on Jan 18, 2023

    @saethlin
    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.

  2. Super-Pizza commented on Jan 21, 2023

    @Super-Pizza

    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

  3. saethlin commented on Jan 21, 2023

    @saethlin
    Member

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

  4. bjorn3 commented on Jan 21, 2023

    @bjorn3
    Member

    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.

  5. bjorn3 commented on Jan 21, 2023

    @bjorn3
    Member

    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 {

  6. tmiasko commented on Jan 23, 2023

    @tmiasko
    Contributor

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

  7. saethlin commented on Jan 23, 2023

    @saethlin
    Member

    🤦 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.

  8. tmiasko commented on Jan 24, 2023

    @tmiasko
    Contributor

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

  9. self-assigned this
    on Mar 8, 2023
  10. tmiasko commented on Mar 9, 2023

    @tmiasko
    Contributor

    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.

  11. removed their assignment
    on Mar 24, 2023
  12. SUPERCILEX commented on Mar 31, 2023

    @SUPERCILEX
    Contributor

    Can this issue be closed since the PR was merged?

  13. ehuss commented on Mar 31, 2023

    @ehuss
    ContributorAuthor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    -Zfixed-x18Unstable option: -Zfixed-x18C-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions