From 2cbc78f238b3eda426171def100f44707cfe8ae3 Mon Sep 17 00:00:00 2001 From: Jeremy Soller <jeremy@system76.com> Date: Wed, 18 Dec 2019 21:15:00 -0700 Subject: [PATCH] Add linker pointer --- src/ld_so/start.rs | 9 ++++++++- src/ld_so/tcb.rs | 9 ++++++++- src/platform/pte.rs | 12 +++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/ld_so/start.rs b/src/ld_so/start.rs index 4a5da627e..56501aad6 100644 --- a/src/ld_so/start.rs +++ b/src/ld_so/start.rs @@ -1,8 +1,11 @@ // Start code adapted from https://gitlab.redox-os.org/redox-os/relibc/blob/master/src/start.rs -use crate::{c_str::CStr, header::unistd, platform::types::c_char, start::Stack}; +use alloc::boxed::Box; + +use crate::{c_str::CStr, header::unistd, platform::types::c_char, start::Stack, sync::mutex::Mutex}; use super::linker::Linker; +use super::tcb::Tcb; #[no_mangle] pub extern "C" fn relibc_ld_so_start(sp: &'static mut Stack) -> usize { @@ -122,6 +125,10 @@ pub extern "C" fn relibc_ld_so_start(sp: &'static mut Stack) -> usize { } }; + if let Some(tcb) = unsafe { Tcb::current() } { + tcb.linker_ptr = Box::into_raw(Box::new(Mutex::new(linker))); + } + eprintln!("ld.so: entry '{}': {:#x}", path, entry); entry } diff --git a/src/ld_so/tcb.rs b/src/ld_so/tcb.rs index b595d1f12..3b71ebfaa 100644 --- a/src/ld_so/tcb.rs +++ b/src/ld_so/tcb.rs @@ -2,7 +2,11 @@ use alloc::boxed::Box; use core::{mem, ops::Range, ptr, slice}; use goblin::error::{Error, Result}; -use crate::header::sys_mman; +use crate::{ + header::sys_mman, + ld_so::linker::Linker, + sync::mutex::Mutex, +}; use super::PAGE_SIZE; @@ -43,6 +47,8 @@ pub struct Tcb { pub masters_ptr: *mut Master, /// Size of the masters list in bytes (multiple of mem::size_of::<Master>()) pub masters_len: usize, + /// Pointer to dynamic linker + pub linker_ptr: *const Mutex<Linker>, } impl Tcb { @@ -61,6 +67,7 @@ impl Tcb { tcb_len: tcb_page.len(), masters_ptr: ptr::null_mut(), masters_len: 0, + linker_ptr: ptr::null(), }, ); diff --git a/src/platform/pte.rs b/src/platform/pte.rs index 32f07f72c..c6ca0fba4 100644 --- a/src/platform/pte.rs +++ b/src/platform/pte.rs @@ -8,7 +8,10 @@ use core::{ use crate::{ header::{sys_mman, time::timespec}, - ld_so::tcb::{Master, Tcb}, + ld_so::{ + linker::Linker, + tcb::{Master, Tcb}, + }, platform::{ types::{c_int, c_uint, c_void, pid_t, size_t}, Pal, Sys, @@ -72,6 +75,7 @@ unsafe extern "C" fn pte_osThreadShim( tls_size: usize, tls_masters_ptr: *mut Master, tls_masters_len: usize, + tls_linker_ptr: *const Mutex<Linker>, ) { // The kernel allocated TLS does not have masters set, so do not attempt to copy it. // It will be copied by the kernel. @@ -79,6 +83,7 @@ unsafe extern "C" fn pte_osThreadShim( let tcb = Tcb::new(tls_size).unwrap(); tcb.masters_ptr = tls_masters_ptr; tcb.masters_len = tls_masters_len; + tcb.linker_ptr = tls_linker_ptr; tcb.copy_masters().unwrap(); tcb.activate(); } @@ -125,7 +130,11 @@ pub unsafe extern "C" fn pte_osThreadCreate( *stack = value; }; + // Stack must be 128-bit aligned for SSE + push(0); + if let Some(tcb) = Tcb::current() { + push(tcb.linker_ptr as usize); push(tcb.masters_len); push(tcb.masters_ptr as usize); push(tcb.tls_len); @@ -133,6 +142,7 @@ pub unsafe extern "C" fn pte_osThreadCreate( push(0); push(0); push(0); + push(0); } push(mutex as usize); -- GitLab