From 367be58666a13a3a2e69df4e403a4285e5ce7ba9 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Sun, 11 Sep 2022 11:11:49 -0600
Subject: [PATCH] Set linker_ptr when using static TLS

---
 src/ld_so/linker.rs |  4 +++-
 src/start.rs        | 18 ++++++++++++++++--
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs
index a1a517f78..210f8cdbb 100644
--- a/src/ld_so/linker.rs
+++ b/src/ld_so/linker.rs
@@ -83,7 +83,9 @@ impl Linker {
                     obj.use_count += 1;
                     return Ok(*id);
                 } else {
-                    let parent_runpath = &self.objects.get(&root_id).unwrap().runpath.clone();
+                    let parent_runpath = &self.objects.get(&root_id).and_then(|parent| {
+                        parent.runpath.clone()
+                    });
                     let lib_id = self.next_object_id;
                     self.load_object(name, parent_runpath, None, true)?;
 
diff --git a/src/start.rs b/src/start.rs
index 3c297a831..c03af9c56 100644
--- a/src/start.rs
+++ b/src/start.rs
@@ -1,10 +1,11 @@
-use alloc::vec::Vec;
+use alloc::{boxed::Box, vec::Vec};
 use core::{intrinsics, ptr};
 
 use crate::{
     header::{libgen, stdio, stdlib},
-    ld_so,
+    ld_so::{self, linker::Linker},
     platform::{self, get_auxvs, new_mspace, types::*, Pal, Sys},
+    sync::mutex::Mutex,
     ALLOCATOR,
 };
 
@@ -162,6 +163,19 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
     // if any memory rust based memory allocation happen before this step .. we are doomed.
     alloc_init();
 
+    if let Some(tcb) = ld_so::tcb::Tcb::current() {
+        // Update TCB mspace
+        tcb.mspace = ALLOCATOR.get_book_keeper();
+
+        // Set linker pointer if necessary
+        if tcb.linker_ptr.is_null() {
+            //TODO: get ld path
+            let linker = Linker::new(None);
+            //TODO: load root object
+            tcb.linker_ptr = Box::into_raw(Box::new(Mutex::new(linker)));
+        }
+    }
+
     // Set up argc and argv
     let argc = sp.argc;
     let argv = sp.argv();
-- 
GitLab