From d7b05bc6b43309ce5f6ad74ac1cdb41adf5a0107 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sun, 29 May 2016 18:35:16 +0200 Subject: [PATCH] =?UTF-8?q?Update=20=E2=80=9CSet=20Up=20Rust=E2=80=9D=20po?= =?UTF-8?q?st=20to=20use=20the=20cargo=20panic=20option?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blog/post/2015-09-02-set-up-rust.md | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/blog/post/2015-09-02-set-up-rust.md b/blog/post/2015-09-02-set-up-rust.md index e90cf4cc..d4de2127 100644 --- a/blog/post/2015-09-02-set-up-rust.md +++ b/blog/post/2015-09-02-set-up-rust.md @@ -219,17 +219,31 @@ target/debug/libblog_os.a(blog_os.0.o): /home/.../src/libcore/iter.rs:654: undefined reference to `_Unwind_Resume' ``` -So the linker can't find a function named `_Unwind_Resume` that is referenced in `iter.rs:654` in libcore. This reference is not really there at [line 654 of libcore's `iter.rs`][iter.rs:654]. Instead, it is a compiler inserted _landing pad_, which is used for exception handling. +So the linker can't find a function named `_Unwind_Resume` that is referenced in `iter.rs:654` in libcore. This reference is not really there at [line 654 of libcore's `iter.rs`][iter.rs:654]. Instead, it is a compiler inserted _landing pad_, which is used for panic handling by default. [iter.rs:654]: https://github.com/rust-lang/rust/blob/b0ca03923359afc8df92a802b7cc1476a72fb2d0/src/libcore/iter.rs#L654 -The easiest way of fixing this problem is to disable the landing pad creation since we don't supports panics anyway right now. We can do this by passing a `-Z no-landing-pads` flag to `rustc` (the actual Rust compiler below cargo). To do this we replace the `cargo build` command in our Makefile with the `cargo rustc` command, which does the same but allows passing flags to `rustc`: +By default, the destructors of all stack variables are run when a `panic` occurs. This is called _unwinding_ and allows parent threads to [recover from panics]. However, it requires a platform specific gcc library, which isn't available in our kernel. -```make -cargo: - @cargo rustc --target $(target) -- -Z no-landing-pads +[recover from panics]: https://doc.rust-lang.org/book/concurrency.html#panics + +Fortunately, Rust allows us to disable unwinding. We just need to add some entries in our `Cargo.toml`: + +```toml +# The development profile, used for `cargo build`. +[profile.dev] +panic = "abort" + +# The release profile, used for `cargo build --release`. +[profile.release] +panic = "abort" ``` -Now we fixed all linking issues and our kernel should _build_ again. But instead of displaying `Hello World`, it constantly reboots itself when we start it. + +These [profile sections] specify options for `cargo build` and `cargo release`. By setting the `panic` option to `abort`, we disable all unwinding in our kernel. + +[profile sections]: http://doc.crates.io/manifest.html#the-profile-sections + +Now we fixed all linking issues and our kernel builds again. But instead of displaying `Hello World`, it constantly reboots itself when we start it. ## Debugging the Boot Loop Such a boot loop is most likely caused by some [CPU exception][exception table]. When these exceptions aren't handled, a [Triple Fault] occurs and the processor resets itself. We can look at generated CPU interrupts/exceptions using QEMU: