diff --git a/src/errno.rs b/src/errno.rs index 62cda2d12e..6ac6d455dc 100644 --- a/src/errno.rs +++ b/src/errno.rs @@ -396,11 +396,22 @@ pub const ERFKILL: i32 = 132; /// Robust mutexes: Memory page has hardware error pub const EHWPOISON: i32 = 133; +/// Returns the pointer to `errno`. #[cfg(not(any(feature = "common-os", feature = "nostd")))] -#[thread_local] -pub(crate) static ERRNO: core::cell::UnsafeCell = core::cell::UnsafeCell::new(0); +#[unsafe(no_mangle)] +#[linkage = "weak"] +pub extern "C" fn sys_errno_location() -> *mut i32 { + use core::cell::UnsafeCell; + + #[thread_local] + static ERRNO: UnsafeCell = UnsafeCell::new(0); + + ERRNO.get() +} /// Get the error number from the thread local storage +/// +/// Soft-deprecated in favor of using [`sys_errno_location`]. #[cfg(not(feature = "nostd"))] #[unsafe(no_mangle)] pub extern "C" fn sys_get_errno() -> i32 { @@ -408,6 +419,8 @@ pub extern "C" fn sys_get_errno() -> i32 { } /// Get the error number from the thread local storage +/// +/// Soft-deprecated in favor of using [`sys_errno_location`]. #[cfg(not(feature = "nostd"))] #[unsafe(no_mangle)] pub extern "C" fn sys_errno() -> i32 { @@ -415,7 +428,7 @@ pub extern "C" fn sys_errno() -> i32 { if #[cfg(feature = "common-os")] { 0 } else { - unsafe { ERRNO.get().read() } + unsafe { *sys_errno_location() } } } } @@ -435,7 +448,7 @@ pub(crate) trait ToErrno { let _ = errno; } else { unsafe { - ERRNO.get().write(errno); + *sys_errno_location() = errno; } } } diff --git a/src/lib.rs b/src/lib.rs index 768f388bdf..f14f0350b6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,7 @@ )] #![cfg_attr(target_arch = "x86_64", feature(abi_x86_interrupt))] #![feature(allocator_api)] +#![feature(linkage)] #![feature(linked_list_cursors)] #![feature(map_try_insert)] #![feature(maybe_uninit_as_bytes)]