You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Create personality stub function for no_std panic=abort crates
**This is a very WIP state. It has no tests, no platform gates, and
probably many bugs.**
Every crate that may unwind (being compiled with panic=unwind) needs a reference to a personality function in its object code.
The personality function is defined in `std`. For crates downstream of `std` (with `std` in the cstore), they will resolve to the personality lang item of std and reference that function from there (using its symbol_name `rust_eh_personality`).
For `#![no_std]` crates, they will also reference the `rust_eh_personality` symbol (directly, not via weak lang item machinery).
But when `std` is never linked into a crate graph containing panic=unwind crates (which happens on all panic=unwind default targets because of core), the symbol isn't present and we get a linker error.
This PR solves this problem by injecting a stub for
`rust_eh_personality` into the final link of binaries where the previous
conditions were fulfilled. This is implemented in a way that's very
similar to the allocator shim.
Because we don't want to insta-stabilize this functionality, the stub
doesn't just abort, it will first do a volatile read of the
`__rust_personality_stub_is_unstable` symbol (once again similar to the
allocator) to ensure that this functionality cannot be relied on without
providing this symbol.
Example code that now works on x86_64-unknown-linux-gnu:
```rust
fn panic_handler(_: &core::panic::PanicInfo<'_>) -> ! {
loop {}
}
unsafe extern "C" {
fn write(fd: i32, buf: *const u8, count: usize);
}
static __rust_personality_stub_is_unstable: u8 = 0;
fn main() -> i32 {
// bring in some code that requires the personality function
[1].sort_unstable();
let msg = b"meow\n";
unsafe {
write(1, msg.as_ptr(), msg.len());
}
0
}
```
When compiled with `-Cpanic=abort`, this would previously result in a
linker error because the `rust_eh_personality` symbol was not found.
Now, it compiles and runs successfully.
0 commit comments