From d703b6bb2b3e97616968b93fd4bbc8b974664cb4 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 9 Jul 2026 14:13:49 +0200 Subject: [PATCH 1/4] Use cfg_select! for unwinder_private_data_size --- library/panic_unwind/src/gcc.rs | 2 +- library/unwind/src/libunwind.rs | 81 +++++-------------- .../miri/tests/pass/panic/unwind_dwarf.rs | 2 +- 3 files changed, 24 insertions(+), 61 deletions(-) diff --git a/library/panic_unwind/src/gcc.rs b/library/panic_unwind/src/gcc.rs index 5f95870069dc5..adcbe5170623c 100644 --- a/library/panic_unwind/src/gcc.rs +++ b/library/panic_unwind/src/gcc.rs @@ -63,7 +63,7 @@ pub(crate) unsafe fn panic(data: Box) -> u32 { _uwe: uw::_Unwind_Exception { exception_class: RUST_EXCEPTION_CLASS, exception_cleanup: Some(exception_cleanup), - private: [core::ptr::null(); uw::unwinder_private_data_size], + private: [core::ptr::null(); _], }, canary: &CANARY, cause: data, diff --git a/library/unwind/src/libunwind.rs b/library/unwind/src/libunwind.rs index 6967c7f7f2665..263fa51c78b0c 100644 --- a/library/unwind/src/libunwind.rs +++ b/library/unwind/src/libunwind.rs @@ -24,65 +24,28 @@ pub type _Unwind_Ptr = *const u8; pub type _Unwind_Trace_Fn = extern "C" fn(ctx: *mut _Unwind_Context, arg: *mut c_void) -> _Unwind_Reason_Code; -#[cfg(target_arch = "x86")] -pub const unwinder_private_data_size: usize = 5; - -#[cfg(all(target_arch = "x86_64", not(any(target_os = "windows", target_os = "cygwin"))))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(all(target_arch = "x86_64", any(target_os = "windows", target_os = "cygwin")))] -pub const unwinder_private_data_size: usize = 6; - -#[cfg(all(target_arch = "arm", not(target_vendor = "apple")))] -pub const unwinder_private_data_size: usize = 20; - -#[cfg(all(target_arch = "arm", target_vendor = "apple"))] -pub const unwinder_private_data_size: usize = 5; - -#[cfg(all(target_arch = "aarch64", target_pointer_width = "64", not(target_os = "windows")))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(all(target_arch = "aarch64", target_pointer_width = "64", target_os = "windows"))] -pub const unwinder_private_data_size: usize = 6; - -#[cfg(all(target_arch = "aarch64", target_pointer_width = "32"))] -pub const unwinder_private_data_size: usize = 5; - -#[cfg(target_arch = "m68k")] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(any(target_arch = "mips", target_arch = "mips32r6"))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(target_arch = "csky")] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(any(target_arch = "mips64", target_arch = "mips64r6"))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(any(target_arch = "powerpc", target_arch = "powerpc64"))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(target_arch = "s390x")] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(any(target_arch = "sparc", target_arch = "sparc64"))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(any(target_arch = "riscv64", target_arch = "riscv32"))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(all(target_family = "wasm", target_os = "emscripten"))] -pub const unwinder_private_data_size: usize = 20; - -#[cfg(all(target_arch = "wasm32", any(target_os = "linux", target_os = "wasi")))] -pub const unwinder_private_data_size: usize = 2; - -#[cfg(target_arch = "hexagon")] -pub const unwinder_private_data_size: usize = 5; - -#[cfg(any(target_arch = "loongarch32", target_arch = "loongarch64"))] -pub const unwinder_private_data_size: usize = 2; +pub const unwinder_private_data_size: usize = cfg_select! { + target_arch = "x86" => 5, + all(target_arch = "x86_64", not(any(target_os = "windows", target_os = "cygwin"))) => 2, + all(target_arch = "x86_64", any(target_os = "windows", target_os = "cygwin")) => 6, + all(target_arch = "arm", not(target_vendor = "apple")) => 20, + all(target_arch = "arm", target_vendor = "apple") => 5, + all(target_arch = "aarch64", target_pointer_width = "64", not(target_os = "windows")) => 2, + all(target_arch = "aarch64", target_pointer_width = "64", target_os = "windows") => 6, + all(target_arch = "aarch64", target_pointer_width = "32") => 5, + target_arch = "m68k" => 2, + any(target_arch = "mips", target_arch = "mips32r6") => 2, + target_arch = "csky" => 2, + any(target_arch = "mips64", target_arch = "mips64r6") => 2, + any(target_arch = "powerpc", target_arch = "powerpc64") => 2, + target_arch = "s390x" => 2, + any(target_arch = "sparc", target_arch = "sparc64") => 2, + any(target_arch = "riscv64", target_arch = "riscv32") => 2, + all(target_family = "wasm", target_os = "emscripten") => 20, + all(target_arch = "wasm32", any(target_os = "linux", target_os = "wasi")) => 2, + target_arch = "hexagon" => 5, + any(target_arch = "loongarch32", target_arch = "loongarch64") => 2, +}; #[repr(C)] pub struct _Unwind_Exception { diff --git a/src/tools/miri/tests/pass/panic/unwind_dwarf.rs b/src/tools/miri/tests/pass/panic/unwind_dwarf.rs index 3631c3f43aea9..aaecacd95ec9a 100644 --- a/src/tools/miri/tests/pass/panic/unwind_dwarf.rs +++ b/src/tools/miri/tests/pass/panic/unwind_dwarf.rs @@ -27,7 +27,7 @@ fn panic(data: Box) -> u32 { _uwe: uw::_Unwind_Exception { exception_class: miri_exception_class(), exception_cleanup: Some(exception_cleanup), - private: [core::ptr::null(); uw::unwinder_private_data_size], + private: [core::ptr::null(); _], }, cause: data, }); From 96058868cea768c7484f4b0864de13f15785bfab Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Thu, 9 Jul 2026 14:24:44 +0200 Subject: [PATCH 2/4] Unify the type definitions for libunwind and the unwinding crate --- library/unwind/src/lib.rs | 5 +- library/unwind/src/libunwind.rs | 23 +++++--- library/unwind/src/unwinding.rs | 100 -------------------------------- 3 files changed, 16 insertions(+), 112 deletions(-) delete mode 100644 library/unwind/src/unwinding.rs diff --git a/library/unwind/src/lib.rs b/library/unwind/src/lib.rs index 02201f43c08ce..dce3ea8c7e007 100644 --- a/library/unwind/src/lib.rs +++ b/library/unwind/src/lib.rs @@ -33,14 +33,11 @@ cfg_select! { target_os = "solid_asp3", all(target_vendor = "fortanix", target_env = "sgx"), all(target_os = "wasi", panic = "unwind"), + target_os = "xous", ) => { mod libunwind; pub use libunwind::*; } - target_os = "xous" => { - mod unwinding; - pub use unwinding::*; - } target_family = "wasm" => { mod wasm; pub use wasm::*; diff --git a/library/unwind/src/libunwind.rs b/library/unwind/src/libunwind.rs index 263fa51c78b0c..24fe12a3c834f 100644 --- a/library/unwind/src/libunwind.rs +++ b/library/unwind/src/libunwind.rs @@ -2,6 +2,10 @@ use core::ffi::{c_int, c_void}; +// Use the unwinding crate as unwinder on Xous +#[cfg(target_os = "xous")] +use unwinding as _; + #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq)] pub enum _Unwind_Reason_Code { @@ -54,6 +58,12 @@ pub struct _Unwind_Exception { pub private: [_Unwind_Word; unwinder_private_data_size], } +// Check the size of _Unwind_Exception against the source of thruth when using the unwinding crate. +#[cfg(target_os = "xous")] +const _: () = { + assert!(size_of::() == size_of::<_Unwind_Exception>()); +}; + pub enum _Unwind_Context {} pub type _Unwind_Exception_Cleanup_Fn = @@ -68,10 +78,7 @@ pub type _Unwind_Exception_Cleanup_Fn = // rustc_codegen_ssa::src::back::symbol_export, rustc_middle::middle::exported_symbols // and RFC 2841 #[cfg_attr( - all( - feature = "llvm-libunwind", - any(target_os = "fuchsia", target_os = "linux", target_os = "xous") - ), + all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static", modifiers = "-bundle") )] // Explicitly link the `unwind` library on WASI targets. @@ -107,7 +114,7 @@ cfg_select! { pub const _UA_END_OF_STACK: c_int = 16; #[cfg_attr( - all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux", target_os = "xous")), + all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static", modifiers = "-bundle") )] unsafe extern "C" { @@ -166,7 +173,7 @@ cfg_select! { pub const UNWIND_IP_REG: c_int = 15; #[cfg_attr( - all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux", target_os = "xous")), + all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static", modifiers = "-bundle") )] unsafe extern "C" { @@ -246,14 +253,14 @@ cfg_select! { } _ => { #[cfg_attr( - all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux", target_os = "xous")), + all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static", modifiers = "-bundle") )] unsafe extern "C-unwind" { pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code; } #[cfg_attr( - all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux", target_os = "xous")), + all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")), link(name = "unwind", kind = "static", modifiers = "-bundle") )] unsafe extern "C" { diff --git a/library/unwind/src/unwinding.rs b/library/unwind/src/unwinding.rs deleted file mode 100644 index 36120bc868ee9..0000000000000 --- a/library/unwind/src/unwinding.rs +++ /dev/null @@ -1,100 +0,0 @@ -#![allow(nonstandard_style)] - -use core::ffi::{c_int, c_void}; - -pub type _Unwind_Action = c_int; - -pub const _UA_SEARCH_PHASE: c_int = 1; -pub const _UA_CLEANUP_PHASE: c_int = 2; -pub const _UA_HANDLER_FRAME: c_int = 4; -pub const _UA_FORCE_UNWIND: c_int = 8; -pub const _UA_END_OF_STACK: c_int = 16; - -#[repr(C)] -#[derive(Debug, Copy, Clone, PartialEq)] -pub enum _Unwind_Reason_Code { - _URC_NO_REASON = 0, - _URC_FOREIGN_EXCEPTION_CAUGHT = 1, - _URC_FATAL_PHASE2_ERROR = 2, - _URC_FATAL_PHASE1_ERROR = 3, - _URC_NORMAL_STOP = 4, - _URC_END_OF_STACK = 5, - _URC_HANDLER_FOUND = 6, - _URC_INSTALL_CONTEXT = 7, - _URC_CONTINUE_UNWIND = 8, - _URC_FAILURE = 9, // used only by ARM EHABI -} -pub use _Unwind_Reason_Code::*; -pub use unwinding::abi::{UnwindContext, UnwindException}; -pub enum _Unwind_Context {} - -pub use unwinding::custom_eh_frame_finder::{ - EhFrameFinder, FrameInfo, FrameInfoKind, set_custom_eh_frame_finder, -}; - -pub type _Unwind_Exception_Class = u64; -pub type _Unwind_Word = *const u8; -pub type _Unwind_Ptr = *const u8; - -pub const unwinder_private_data_size: usize = size_of::() - - size_of::<_Unwind_Exception_Class>() - - size_of::<_Unwind_Exception_Cleanup_Fn>(); - -pub type _Unwind_Exception_Cleanup_Fn = - Option; - -#[repr(C)] -pub struct _Unwind_Exception { - pub exception_class: _Unwind_Exception_Class, - pub exception_cleanup: _Unwind_Exception_Cleanup_Fn, - pub private: [_Unwind_Word; unwinder_private_data_size], -} - -pub unsafe fn _Unwind_GetDataRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - unwinding::abi::_Unwind_GetDataRelBase(ctx) as _Unwind_Ptr -} - -pub unsafe fn _Unwind_GetTextRelBase(ctx: *mut _Unwind_Context) -> _Unwind_Ptr { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - unwinding::abi::_Unwind_GetTextRelBase(ctx) as _Unwind_Ptr -} - -pub unsafe fn _Unwind_GetRegionStart(ctx: *mut _Unwind_Context) -> _Unwind_Ptr { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - unwinding::abi::_Unwind_GetRegionStart(ctx) as _Unwind_Ptr -} - -pub unsafe fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word) { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - unwinding::abi::_Unwind_SetGR(ctx, reg_index, value as usize) -} - -pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word) { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - unwinding::abi::_Unwind_SetIP(ctx, value as usize) -} - -pub unsafe fn _Unwind_GetIPInfo( - ctx: *mut _Unwind_Context, - ip_before_insn: *mut c_int, -) -> _Unwind_Word { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - let ip_before_insn = unsafe { &mut *(ip_before_insn as *mut c_int) }; - unsafe { &*(unwinding::abi::_Unwind_GetIPInfo(ctx, ip_before_insn) as _Unwind_Word) } -} - -pub unsafe fn _Unwind_GetLanguageSpecificData(ctx: *mut _Unwind_Context) -> *mut c_void { - let ctx = unsafe { &mut *(ctx as *mut UnwindContext<'_>) }; - unwinding::abi::_Unwind_GetLanguageSpecificData(ctx) -} - -pub unsafe fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code { - let exception = unsafe { &mut *(exception as *mut UnwindException) }; - unsafe { core::mem::transmute(unwinding::abi::_Unwind_RaiseException(exception)) } -} - -pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) { - let exception = unsafe { &mut *(exception as *mut UnwindException) }; - unsafe { unwinding::abi::_Unwind_DeleteException(exception) } -} From 3715de0eccab692e0babb9b096332dce508eac49 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Fri, 10 Jul 2026 15:39:00 +0200 Subject: [PATCH 3/4] Remove unused include in unwind/Cargo.toml --- library/unwind/Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/library/unwind/Cargo.toml b/library/unwind/Cargo.toml index 1293c469d11c1..628b9d5e8c3ec 100644 --- a/library/unwind/Cargo.toml +++ b/library/unwind/Cargo.toml @@ -4,9 +4,6 @@ version = "0.0.0" license = "MIT OR Apache-2.0" repository = "https://github.com/rust-lang/rust.git" edition = "2024" -include = [ - '/libunwind/*', -] [lib] test = false From aa318514c7e6c7baca393a7f8d8126c73832d431 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Sun, 12 Jul 2026 11:35:46 +0200 Subject: [PATCH 4/4] Fix building for xous --- library/unwind/src/libunwind.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/library/unwind/src/libunwind.rs b/library/unwind/src/libunwind.rs index 24fe12a3c834f..aee03879dd0a6 100644 --- a/library/unwind/src/libunwind.rs +++ b/library/unwind/src/libunwind.rs @@ -4,7 +4,9 @@ use core::ffi::{c_int, c_void}; // Use the unwinding crate as unwinder on Xous #[cfg(target_os = "xous")] -use unwinding as _; +pub use unwinding::custom_eh_frame_finder::{ + EhFrameFinder, FrameInfo, FrameInfoKind, set_custom_eh_frame_finder, +}; #[repr(C)] #[derive(Debug, Copy, Clone, PartialEq)]