Jelajahi Sumber

Add linker pointer

Jeremy Soller 5 tahun lalu
induk
melakukan
2cbc78f238
3 mengubah file dengan 27 tambahan dan 3 penghapusan
  1. 8 1
      src/ld_so/start.rs
  2. 8 1
      src/ld_so/tcb.rs
  3. 11 1
      src/platform/pte.rs

+ 8 - 1
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
 }

+ 8 - 1
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(),
             },
         );
 

+ 11 - 1
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);