-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
It's impossible to write a panic crate for no_std #48661
Comments
FWIW, LTO works around the issue. |
Another work around: using the dylib crate-type instead of cdylib, because of: rust/src/librustc_trans/back/link.rs Lines 1393 to 1395 in 5db73fc
But this doesn't help for the bin crate-type, which actually fails to link because of the missing All in all, I just wish there was a way for me to make that library appear after libcore on the linker command. |
Another workaround: building libpanic as both a rlib and a staticlib, and add: #[link(name = "panic", kind = "static")]
extern "C" {} in the same file that contains |
This use std::env;
use std::path::Path;
fn main() {
let out_dir = env::var_os("OUT_DIR").unwrap();
let out_dir = Path::new(&out_dir).parent().unwrap().parent().unwrap().parent().unwrap();
println!("cargo:rustc-link-search=native={}", out_dir.to_str().unwrap());
println!("cargo:rustc-link-lib=static=panic");
} The triple |
I don't remember the differences between cdylbs and plain executables but this sounds like #47074 / #18807, which has a proposed rustc fix in #47074 (comment). @glandium could you try wrapping the linker arguments in --start-group / --end-group? |
It works, but only if libcore and libpanic are between the same --start-group/--end-group. That doesn't help with the second half of #44489 (comment) though. |
Yes, that's what #47074 (comment) proposes to do. FWIW, you may be able to avoid the linking errors if you disable both incr comp and parallel codegen in your dev profile (IDK if you also get the error using the release profile). We have run into this problem in Cortex-M land because the cortex-m-rt provides a panic implementation (the panic_fmt lang item) and both disabling incr comp and parallel codegen fixes the linking issue in must, but not all, cases. |
@alexcrichton Sadly, #49316 didn't fix this :( |
It turns out that the support in rust-lang#49316 wasn't enough to handle all cases notably the example in rust-lang#48661. The underlying bug was connected to panic=abort where lang items were listed in the `missing_lang_items` sets but didn't actually exist anywhere. This caused the linker backend to deduce that start-group/end-group wasn't needed because not all items were defined. Instead the missing lang items that don't actually need to have a definition are filtered out and not considered for the start-group/end-group arguments Closes rust-lang#48661
I believe this should be fixed by #49672 |
…haelwoerister Fix another circular deps link args issue It turns out that the support in #49316 wasn't enough to handle all cases notably the example in #48661. The underlying bug was connected to panic=abort where lang items were listed in the `missing_lang_items` sets but didn't actually exist anywhere. This caused the linker backend to deduce that start-group/end-group wasn't needed because not all items were defined. Instead the missing lang items that don't actually need to have a definition are filtered out and not considered for the start-group/end-group arguments Closes #48661
I can confirm this is fixed. |
(Originally filed as rust-lang/cargo#5106, but I think it's actually a rustc issue)
I've created a workspace and have multiple crates, all of which are
no_std
and need apanic_fmt
implementation, so I created a separate crate that can be shared for that. Then I built a cdylib which built and linked fine, but failed at runtime with "undefined symbol: rust_begin_unwind".Here's how to reproduce (on Linux, but I expect this should fail the same on all platforms):
Note how this all compiled and linked fine. But:
rust_begin_unwind
is undefined.The reason this is happening is because nothing is using the symbol in any library that appears before libpanic on the linker command line, and the only thing that does use the symbol is libcore, which comes after. So the linker happily removes it.
If I force libpanic to be added last on the linker command line, it works:
Relatedly, this makes me think cargo should probably add
-Wl,-z,defs
:The text was updated successfully, but these errors were encountered: