From e5f3e9642d82601eab22ad56b29a755bdede9bfb Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 24 Jun 2024 12:47:33 +0200 Subject: [PATCH] Use origin's errno_location in the __errno_location implementation This will help with integrating the musl dynamic linker until a rust replacement is written. cc sunfishcode/origin#110 --- c-scape/Cargo.toml | 2 +- c-scape/src/errno_.rs | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/c-scape/Cargo.toml b/c-scape/Cargo.toml index c7d29ae..cf525e8 100644 --- a/c-scape/Cargo.toml +++ b/c-scape/Cargo.toml @@ -64,7 +64,7 @@ static_assertions = "1.1.0" [features] default = ["thread", "std", "coexist-with-libc", "threadsafe-setenv", "use-compiler-builtins"] -thread = [] +thread = ["origin/unstable-errno"] std = ["rustix/std", "printf-compat/std"] # In "take-charge" mode, this enables code in c-scape to define the diff --git a/c-scape/src/errno_.rs b/c-scape/src/errno_.rs index f5fb970..d2d6ed7 100644 --- a/c-scape/src/errno_.rs +++ b/c-scape/src/errno_.rs @@ -1,7 +1,7 @@ use alloc::borrow::ToOwned; use alloc::format; use core::cell::SyncUnsafeCell; -use core::ptr::{addr_of_mut, copy_nonoverlapping, null_mut}; +use core::ptr::{copy_nonoverlapping, null_mut}; use libc::{c_char, c_int}; /// Return the address of the thread-local `errno` state. @@ -13,9 +13,14 @@ use libc::{c_char, c_int}; unsafe extern "C" fn __errno_location() -> *mut c_int { libc!(libc::__errno_location()); - #[cfg_attr(feature = "thread", thread_local)] - static mut ERRNO: i32 = 0; - addr_of_mut!(ERRNO) + #[cfg(feature = "thread")] + return origin::thread::errno_location(); + + #[cfg(not(feature = "thread"))] + { + static mut ERRNO: i32 = 0; + return core::ptr::addr_of_mut!(ERRNO); + } } #[no_mangle]