From 59090a83f4f5d1112361a8cfc054a5d21612e8f7 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Mon, 31 Mar 2025 15:17:11 +0200 Subject: [PATCH] Fix thread+proc fd init in ld.so --- src/ld_so/linker.rs | 9 ++++++--- src/ld_so/mod.rs | 4 ++++ src/ld_so/start.rs | 27 +++++++++++++++++++++------ 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index a1a429090d..ca55590d4f 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -612,10 +612,13 @@ impl Linker { unsafe { if !dlopened { #[cfg(target_os = "redox")] - let (tcb, old_tcb) = { + let (tcb, old_tcb, thr_fd) = { use redox_rt::signal::tmp_disable_signals; let old_tcb = Tcb::current().expect("failed to get bootstrap TCB"); + let thr_fd = (&mut *old_tcb.os_specific.thr_fd.get()) + .take() + .expect("no thread FD present"); let new_tcb = Tcb::new(self.tls_size)?; // This actually allocates TCB, TLS and ABI page. // Stash @@ -653,7 +656,7 @@ impl Linker { new_tcb.generic.tcb_len = new_tcb_len; drop(_guard); - (new_tcb, old_tcb as *mut Tcb as *mut c_void) + (new_tcb, old_tcb as *mut Tcb as *mut c_void, thr_fd) }; #[cfg(not(target_os = "redox"))] @@ -694,7 +697,7 @@ impl Linker { tcb.copy_masters().map_err(|_| DlError::Malformed)?; tcb.activate( #[cfg(target_os = "redox")] - todo!(), + thr_fd, ); #[cfg(target_os = "redox")] diff --git a/src/ld_so/mod.rs b/src/ld_so/mod.rs index d3d726df5d..425d04d859 100644 --- a/src/ld_so/mod.rs +++ b/src/ld_so/mod.rs @@ -196,6 +196,10 @@ pub unsafe fn init( #[cfg(target_os = "redox")] thr_fd, ); + } else { + // The thread fd must already be present in the already existing TCB. Don't close it. + #[cfg(target_os = "redox")] + core::mem::forget(thr_fd); } } diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs index 4c09f52f34..390a77c09a 100644 --- a/src/ld_so/start.rs +++ b/src/ld_so/start.rs @@ -145,16 +145,31 @@ fn resolve_path_name( None } -// TODO: Make unsafe #[no_mangle] -pub extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: usize) -> usize { +pub unsafe extern "C" fn relibc_ld_so_start(sp: &'static mut Stack, ld_entry: usize) -> usize { // Setup TCB for ourselves. unsafe { - let tcb = Tcb::new(0).expect_notls("ld.so: failed to allocate bootstrap TCB"); - tcb.activate(todo!()); - #[cfg(target_os = "redox")] - redox_rt::signal::setup_sighandler(&tcb.os_specific); + let thr_fd = + crate::platform::get_auxv_raw(sp.auxv().cast(), redox_rt::auxv_defs::AT_REDOX_THR_FD) + .expect_notls("no thread fd present"); + + let tcb = Tcb::new(0).expect_notls("ld.so: failed to allocate bootstrap TCB"); + tcb.activate( + #[cfg(target_os = "redox")] + redox_rt::proc::FdGuard::new(thr_fd), + ); + #[cfg(target_os = "redox")] + { + let proc_fd = crate::platform::get_auxv_raw( + sp.auxv().cast(), + redox_rt::auxv_defs::AT_REDOX_PROC_FD, + ) + .expect_notls("no proc fd present"); + + redox_rt::initialize(redox_rt::proc::FdGuard::new(proc_fd)); + redox_rt::signal::setup_sighandler(&tcb.os_specific); + } } // We get the arguments, the environment, and the auxilary vector