From bee72373be36008218e5df9f2b68a33d3afb2f5a Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Wed, 27 Mar 2019 21:28:39 -0600
Subject: [PATCH] Fix panic not producing output

---
 Cargo.lock          |  3 ---
 Makefile            | 12 ++++++------
 src/crt0/Cargo.toml |  3 ---
 src/crt0/src/lib.rs | 10 +++++++---
 src/crti/src/lib.rs | 10 +++++++---
 src/crtn/src/lib.rs | 10 +++++++---
 src/lib.rs          | 13 +++++++++----
 7 files changed, 36 insertions(+), 25 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index 148ae426..072da425 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -82,9 +82,6 @@ dependencies = [
 [[package]]
 name = "crt0"
 version = "0.1.0"
-dependencies = [
- "relibc 0.1.0",
-]
 
 [[package]]
 name = "crti"
diff --git a/Makefile b/Makefile
index da3f4d01..e00cde71 100644
--- a/Makefile
+++ b/Makefile
@@ -90,15 +90,15 @@ $(BUILD)/debug/librelibc.a: $(SRC)
 	touch $@
 
 $(BUILD)/debug/crt0.o: $(SRC)
-	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
 	touch $@
 
 $(BUILD)/debug/crti.o: $(SRC)
-	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
 	touch $@
 
 $(BUILD)/debug/crtn.o: $(SRC)
-	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
 	touch $@
 
 $(BUILD)/release/librelibc.a: $(SRC)
@@ -106,15 +106,15 @@ $(BUILD)/release/librelibc.a: $(SRC)
 	touch $@
 
 $(BUILD)/release/crt0.o: $(SRC)
-	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crt0/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
 	touch $@
 
 $(BUILD)/release/crti.o: $(SRC)
-	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crti/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
 	touch $@
 
 $(BUILD)/release/crtn.o: $(SRC)
-	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ $(RUSTCFLAGS)
+	CARGO_INCREMENTAL=0 $(CARGO) rustc --release --manifest-path src/crtn/Cargo.toml $(CARGOFLAGS) -- --emit obj=$@ -C panic=abort $(RUSTCFLAGS)
 	touch $@
 
 $(BUILD)/include: $(SRC)
diff --git a/src/crt0/Cargo.toml b/src/crt0/Cargo.toml
index 9b6c91a2..8c4bfad2 100644
--- a/src/crt0/Cargo.toml
+++ b/src/crt0/Cargo.toml
@@ -6,6 +6,3 @@ authors = ["Jeremy Soller <jackpot51@gmail.com>"]
 [lib]
 name = "crt0"
 crate-type = ["staticlib"]
-
-[dependencies]
-relibc = { path = "../.." }
diff --git a/src/crt0/src/lib.rs b/src/crt0/src/lib.rs
index 6b8c2552..85f1c5a1 100644
--- a/src/crt0/src/lib.rs
+++ b/src/crt0/src/lib.rs
@@ -2,7 +2,6 @@
 
 #![no_std]
 #![feature(asm)]
-#![feature(core_intrinsics)]
 #![feature(linkage)]
 #![feature(naked_functions)]
 
@@ -29,6 +28,11 @@ pub unsafe extern "C" fn _start() {
 }
 
 #[panic_handler]
-unsafe fn panic(_pi: &::core::panic::PanicInfo) -> ! {
-    ::core::intrinsics::abort();
+#[linkage = "weak"]
+#[no_mangle]
+pub unsafe extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
+    extern "C" {
+        fn relibc_panic(pi: &::core::panic::PanicInfo) -> !;
+    }
+    relibc_panic(pi)
 }
diff --git a/src/crti/src/lib.rs b/src/crti/src/lib.rs
index 4f1d7a6e..4a01af50 100644
--- a/src/crti/src/lib.rs
+++ b/src/crti/src/lib.rs
@@ -1,7 +1,6 @@
 //! crti
 
 #![no_std]
-#![feature(core_intrinsics)]
 #![feature(global_asm)]
 #![feature(linkage)]
 
@@ -47,6 +46,11 @@ global_asm!(r#"
 "#);
 
 #[panic_handler]
-unsafe fn panic(_pi: &::core::panic::PanicInfo) -> ! {
-    ::core::intrinsics::abort();
+#[linkage = "weak"]
+#[no_mangle]
+pub unsafe extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
+    extern "C" {
+        fn relibc_panic(pi: &::core::panic::PanicInfo) -> !;
+    }
+    relibc_panic(pi)
 }
diff --git a/src/crtn/src/lib.rs b/src/crtn/src/lib.rs
index e62acc65..4462e5dd 100644
--- a/src/crtn/src/lib.rs
+++ b/src/crtn/src/lib.rs
@@ -1,7 +1,6 @@
 //! crti
 
 #![no_std]
-#![feature(core_intrinsics)]
 #![feature(global_asm)]
 #![feature(linkage)]
 
@@ -37,6 +36,11 @@ global_asm!(r#"
 "#);
 
 #[panic_handler]
-unsafe fn panic(_pi: &::core::panic::PanicInfo) -> ! {
-    ::core::intrinsics::abort();
+#[linkage = "weak"]
+#[no_mangle]
+pub unsafe extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
+    extern "C" {
+        fn relibc_panic(pi: &::core::panic::PanicInfo) -> !;
+    }
+    relibc_panic(pi)
 }
diff --git a/src/lib.rs b/src/lib.rs
index 7cc40f81..b3757e19 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -58,11 +58,8 @@ use platform::{Allocator, Pal, Sys};
 #[global_allocator]
 static ALLOCATOR: Allocator = Allocator;
 
-#[cfg(not(test))]
-#[panic_handler]
-#[linkage = "weak"]
 #[no_mangle]
-pub extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
+pub extern "C" fn relibc_panic(pi: &::core::panic::PanicInfo) -> ! {
     use core::fmt::Write;
 
     let mut w = platform::FileWriter(2);
@@ -71,6 +68,14 @@ pub extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
     Sys::exit(1);
 }
 
+#[cfg(not(test))]
+#[panic_handler]
+#[linkage = "weak"]
+#[no_mangle]
+pub extern "C" fn rust_begin_unwind(pi: &::core::panic::PanicInfo) -> ! {
+    relibc_panic(pi)
+}
+
 #[cfg(not(test))]
 #[lang = "eh_personality"]
 #[no_mangle]
-- 
GitLab