mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Pub and no_mangle are no longer required for panic_handler (#468)
This commit is contained in:
committed by
GitHub
parent
2dd925d34d
commit
6f48a17ba4
@@ -206,8 +206,7 @@ use core::panic::PanicInfo;
|
||||
|
||||
/// This function is called on panic.
|
||||
#[panic_handler]
|
||||
#[no_mangle]
|
||||
pub fn panic(_info: &PanicInfo) -> ! {
|
||||
fn panic(_info: &PanicInfo) -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
@@ -238,7 +237,7 @@ It fails! The error tells us that the Rust compiler no longer finds the `core` o
|
||||
The problem is that the core library is distributed together with the Rust compiler as a _precompiled_ library. So it is only valid for supported host triples (e.g., `x86_64-unknown-linux-gnu`) but not for our custom target. If we want to compile code for other targets, we need to recompile `core` for these targets first.
|
||||
|
||||
#### Cargo xbuild
|
||||
That's where [`cargo xbuild`] comes in. It is a wrapper for `cargo build` that automatically cross-compiles the built-in libraries. We can install it by executing:
|
||||
That's where [`cargo xbuild`] comes in. It is a wrapper for `cargo build` that automatically cross-compiles `core` and other built-in libraries. We can install it by executing:
|
||||
|
||||
[`cargo xbuild`]: https://github.com/rust-osdev/cargo-xbuild
|
||||
|
||||
@@ -248,7 +247,27 @@ cargo install cargo-xbuild
|
||||
|
||||
The command depends on the rust source code, which we can install with `rustup component add rust-src`.
|
||||
|
||||
We now can rerun the above command with `xbuild` instead of `build`:
|
||||
Now we can rerun the above command with `xbuild` instead of `build`:
|
||||
|
||||
```
|
||||
> cargo xbuild --target x86_64-blog_os.json
|
||||
```
|
||||
|
||||
Depending on your version of the Rust compiler you might get the following error:
|
||||
|
||||
```
|
||||
error: The sysroot can't be built for the Stable channel. Switch to nightly.
|
||||
```
|
||||
|
||||
To understand this error, you need to know that the Rust compiler has three release channels: _stable_, _beta_, and _nightly_. The Rust Book explains the difference between these channels really well, so take a minute and [check it out](https://doc.rust-lang.org/book/second-edition/appendix-07-nightly-rust.html#choo-choo-release-channels-and-riding-the-trains).
|
||||
|
||||
Some experimental features are only available on the nightly channel. Since Rust uses many of these features for the internal implementation of `core` and other built-in libraries, we need to use a nightly compiler when invoking `cargo xbuild` (since it rebuilds these libraries).
|
||||
|
||||
To manage Rust installations I highly recommend [rustup]. It allows you to install nightly, beta, and stable compilers side-by-side and makes it easy to update them. With rustup you can use a nightly compiler for the current directory by running `rustup override add nightly`. Alternatively, you can add a file called `rust-toolchain` with the content `nightly` to the project's root directory.
|
||||
|
||||
[rustup]: https://www.rustup.rs/
|
||||
|
||||
With a nightly compiler the build finally succeeds:
|
||||
|
||||
```
|
||||
> cargo xbuild --target x86_64-blog_os.json
|
||||
@@ -260,7 +279,7 @@ We now can rerun the above command with `xbuild` instead of `build`:
|
||||
Finished dev [unoptimized + debuginfo] target(s) in 0.29 secs
|
||||
```
|
||||
|
||||
It worked! We see that `cargo xbuild` cross-compiled the `core`, `compiler_builtin`, and `alloc` libraries for our new custom target and then continued to compile our `blog_os` crate.
|
||||
We see that `cargo xbuild` cross-compiled the `core`, `compiler_builtin`, and `alloc` libraries for our new custom target and then continued to compile our `blog_os` crate.
|
||||
|
||||
Now we are able to build our kernel for a bare metal target. However, our `_start` entry point, which will be called by the boot loader, is still empty. So let's output something to screen from it.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user