From 0c5abf036152027c5736d0820a214b6f3c2e416c Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Mon, 7 Jan 2019 19:11:30 -0700
Subject: [PATCH] Combine all libraries into libc.a, call pthread_init and
 pthread_terminate in libc

---
 Cargo.toml               |  2 +-
 Makefile                 | 21 +++++++++++++++++++--
 pthreads-emb             |  2 +-
 src/header/stdlib/mod.rs |  3 +++
 src/start.rs             |  3 +++
 5 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index eb0906a9f..d42592f7f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -4,7 +4,7 @@ version = "0.1.0"
 authors = ["Jeremy Soller <jackpot51@gmail.com>"]
 
 [lib]
-name = "c"
+name = "relibc"
 crate-type = ["staticlib"]
 
 [workspace]
diff --git a/Makefile b/Makefile
index a378b9c05..4d120db12 100644
--- a/Makefile
+++ b/Makefile
@@ -76,7 +76,16 @@ sysroot: all
 test: sysroot
 	make -C tests run
 
-$(BUILD)/debug/libc.a: $(SRC)
+$(BUILD)/release/libc.a: $(BUILD)/release/librelibc.a $(BUILD)/pthreads-emb/libpthread.a $(BUILD)/openlibm/libopenlibm.a
+	echo "create $@" > "$@.mri"
+	for lib in $^; do\
+		echo "addlib $$lib" >> "$@.mri"; \
+	done
+	echo "save" >> "$@.mri"
+	echo "end" >> "$@.mri"
+	ar -M < "$@.mri"
+
+$(BUILD)/debug/librelibc.a: $(SRC)
 	$(CARGO) rustc $(CARGOFLAGS) -- $(RUSTCFLAGS)
 	touch $@
 
@@ -84,7 +93,15 @@ $(BUILD)/debug/crt0.o: $(SRC)
 	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
 	touch $@
 
-$(BUILD)/release/libc.a: $(SRC)
+$(BUILD)/debug/crti.o: $(SRC)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	touch $@
+
+$(BUILD)/debug/crtn.o: $(SRC)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	touch $@
+
+$(BUILD)/release/librelibc.a: $(SRC)
 	$(CARGO) rustc --release $(CARGOFLAGS) -- $(RUSTCFLAGS)
 	touch $@
 
diff --git a/pthreads-emb b/pthreads-emb
index a12978ccc..91f0dd6e3 160000
--- a/pthreads-emb
+++ b/pthreads-emb
@@ -1 +1 @@
-Subproject commit a12978ccca4c576f02971f5cfc2cbefe23e4daba
+Subproject commit 91f0dd6e3f9c8ee81508f3a32e1d6f3e97e35726
diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs
index bf00d54ea..08d02e116 100644
--- a/src/header/stdlib/mod.rs
+++ b/src/header/stdlib/mod.rs
@@ -233,6 +233,7 @@ pub unsafe extern "C" fn exit(status: c_int) {
         static __fini_array_start: extern "C" fn();
         static __fini_array_end: extern "C" fn();
 
+        fn pthread_terminate();
         fn _fini();
     }
 
@@ -251,6 +252,8 @@ pub unsafe extern "C" fn exit(status: c_int) {
 
     _fini();
 
+    pthread_terminate();
+
     Sys::exit(status);
 }
 
diff --git a/src/start.rs b/src/start.rs
index a21378c34..7f88e4e72 100644
--- a/src/start.rs
+++ b/src/start.rs
@@ -53,6 +53,7 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
         static __init_array_start: extern "C" fn();
         static __init_array_end: extern "C" fn();
 
+        fn pthread_init();
         fn _init();
         fn main(argc: isize, argv: *mut *mut c_char, envp: *mut *mut c_char) -> c_int;
     }
@@ -77,6 +78,8 @@ pub unsafe extern "C" fn relibc_start(sp: &'static Stack) -> ! {
     stdio::stdout = stdio::default_stdout.get();
     stdio::stderr = stdio::default_stderr.get();
 
+    pthread_init();
+
     // Run preinit array
     {
         let mut f = &__preinit_array_start as *const _;
-- 
GitLab