Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions library/std/src/sys/personality/emcc.rs

This file was deleted.

3 changes: 0 additions & 3 deletions library/std/src/sys/personality/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ mod dwarf;

#[cfg(not(any(test, doctest)))]
cfg_select! {
target_os = "emscripten" => {
mod emcc;
}
any(target_env = "msvc", target_family = "wasm", target_os = "motor") => {
// This is required by the compiler to exist (e.g., it's a lang item),
// but it's never actually called by the compiler because
Expand Down
2 changes: 2 additions & 0 deletions library/unwind/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@ cfg_select! {
) => {
mod libunwind;
pub use libunwind::*;
mod types;
}
target_family = "wasm" => {
mod types;
mod wasm;
pub use wasm::*;
}
Expand Down
120 changes: 17 additions & 103 deletions library/unwind/src/libunwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,69 +8,12 @@ pub use unwinding::custom_eh_frame_finder::{
EhFrameFinder, FrameInfo, FrameInfoKind, set_custom_eh_frame_finder,
};

#[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 crate::types::*;

pub type _Unwind_Exception_Class = u64;
pub type _Unwind_Word = *const u8;
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;

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 {
pub exception_class: _Unwind_Exception_Class,
pub exception_cleanup: _Unwind_Exception_Cleanup_Fn,
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::<unwinding::abi::UnwindException>() == size_of::<_Unwind_Exception>());
};

pub enum _Unwind_Context {}

pub type _Unwind_Exception_Cleanup_Fn =
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;

// FIXME: The `#[link]` attributes on `extern "C"` block marks those symbols declared in
// the block are reexported in dylib build of std. This is needed when build rustc with
// feature `llvm-libunwind`, as no other cdylib will provided those _Unwind_* symbols.
Expand Down Expand Up @@ -110,10 +53,10 @@ cfg_select! {
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_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;
//pub const _UA_END_OF_STACK: c_int = 16;

#[cfg_attr(
all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
Expand All @@ -122,11 +65,9 @@ cfg_select! {
unsafe extern "C" {
pub fn _Unwind_GetGR(ctx: *mut _Unwind_Context, reg_index: c_int) -> _Unwind_Word;
pub fn _Unwind_SetGR(ctx: *mut _Unwind_Context, reg_index: c_int, value: _Unwind_Word);
pub fn _Unwind_GetIP(ctx: *mut _Unwind_Context) -> _Unwind_Word;
pub fn _Unwind_SetIP(ctx: *mut _Unwind_Context, value: _Unwind_Word);
pub fn _Unwind_GetIPInfo(ctx: *mut _Unwind_Context, ip_before_insn: *mut c_int)
-> _Unwind_Word;
pub fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void;
}

}
Expand Down Expand Up @@ -213,12 +154,6 @@ cfg_select! {
(&raw mut value) as *mut c_void); }
}

pub unsafe fn _Unwind_GetIP(ctx: *mut _Unwind_Context)
-> _Unwind_Word {
let val = unsafe { _Unwind_GetGR(ctx, UNWIND_IP_REG) };
val.map_addr(|v| v & !1)
}

pub unsafe fn _Unwind_SetIP(ctx: *mut _Unwind_Context,
value: _Unwind_Word) {
// Propagate thumb bit to instruction pointer
Expand All @@ -232,45 +167,24 @@ cfg_select! {
-> _Unwind_Word {
unsafe {
*ip_before_insn = 0;
_Unwind_GetIP(ctx)
let val = unsafe { _Unwind_GetGR(ctx, UNWIND_IP_REG) };

@tmiasko tmiasko Jul 25, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

   warning: unnecessary `unsafe` block
     --> /rustc/39f1d7da58b7277d3ac42f851507082654e56a2e/library/unwind/src/libunwind.rs:170:27
      |
  168 |             unsafe {
      |             ------ because it's nested under this `unsafe` block
  169 |                 *ip_before_insn = 0;
  170 |                 let val = unsafe { _Unwind_GetGR(ctx, UNWIND_IP_REG) };
      |                           ^^^^^^ unnecessary `unsafe` block

View changes since the review

val.map_addr(|v| v & !1)
}
}

// This function also doesn't exist on Android or ARM/Linux, so make it a no-op
pub unsafe fn _Unwind_FindEnclosingFunction(pc: *mut c_void) -> *mut c_void {
pc
}
}
}

cfg_select! {
all(target_vendor = "apple", not(target_os = "watchos"), target_arch = "arm") => {
// 32-bit ARM Apple (except for watchOS armv7k specifically) uses SjLj and
// does not provide _Unwind_Backtrace()
unsafe extern "C-unwind" {
pub fn _Unwind_SjLj_RaiseException(e: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
}

pub use _Unwind_SjLj_RaiseException as _Unwind_RaiseException;
}
_ => {
#[cfg_attr(
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")),
link(name = "unwind", kind = "static", modifiers = "-bundle")
)]
unsafe extern "C" {
pub fn _Unwind_Backtrace(trace: _Unwind_Trace_Fn,
trace_argument: *mut c_void)
-> _Unwind_Reason_Code;
}
}
#[cfg_attr(
all(feature = "llvm-libunwind", any(target_os = "fuchsia", target_os = "linux")),
link(name = "unwind", kind = "static", modifiers = "-bundle")
)]
unsafe extern "C-unwind" {
// 32-bit ARM Apple (except for watchOS armv7k specifically) uses SjLj
#[cfg_attr(
all(target_vendor = "apple", not(target_os = "watchos"), target_arch = "arm"),
link_name = "_Unwind_SjLj_RaiseException"
)]
pub fn _Unwind_RaiseException(exception: *mut _Unwind_Exception) -> _Unwind_Reason_Code;
}

cfg_select! {
Expand Down
59 changes: 59 additions & 0 deletions library/unwind/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#![allow(nonstandard_style)]

#[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 type _Unwind_Exception_Class = u64;
pub type _Unwind_Word = *const u8;

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,
target_family = "wasm" => 2,
target_arch = "hexagon" => 5,
any(target_arch = "loongarch32", target_arch = "loongarch64") => 2,
};

#[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],
}

// Check the size of _Unwind_Exception against the source of truth when using the unwinding crate.
#[cfg(target_os = "xous")]
const _: () = {
assert!(size_of::<unwinding::abi::UnwindException>() == size_of::<_Unwind_Exception>());
};

pub type _Unwind_Exception_Cleanup_Fn =
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
31 changes: 1 addition & 30 deletions library/unwind/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,7 @@ core::arch::global_asm!(
"__cpp_exception:",
);

#[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 type _Unwind_Exception_Class = u64;
pub type _Unwind_Word = *const u8;

pub const unwinder_private_data_size: usize = 2;

#[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 type _Unwind_Exception_Cleanup_Fn =
Option<extern "C" fn(unwind_code: _Unwind_Reason_Code, exception: *mut _Unwind_Exception)>;
pub use crate::types::*;

pub unsafe fn _Unwind_DeleteException(exception: *mut _Unwind_Exception) {
if let Some(exception_cleanup) = unsafe { (*exception).exception_cleanup } {
Expand Down
Loading