mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 22:37:49 +00:00
Move blog posts/pages into code repository
For history see the `rust_os_posts` tag in the phil-opp/phil-opp.github.io repository.
This commit is contained in:
46
pages/cross-compile-binutils.md
Normal file
46
pages/cross-compile-binutils.md
Normal file
@@ -0,0 +1,46 @@
|
||||
---
|
||||
layout: page
|
||||
title: Cross Compile Binutils
|
||||
---
|
||||
The [GNU Binutils] are a collection of various binary tools such as `ld`, `as`, `objdump`, or `readelf`. These tools are platform-specific, so you need to compile them again if your host system and target system are different. In our case, we need `ld` and `objdump` for the x86_64 architecture.
|
||||
[GNU Binutils]: https://www.gnu.org/software/binutils/
|
||||
|
||||
## Building Setup
|
||||
First, you need to download a current binutils version from [here][download] \(the latest one is near the bottom). After extracting, you should have a folder named `binutils-2.X` where `X` is for example `25.1`. Now can create and switch to a new folder for building (recommended):
|
||||
[download]: ftp://sourceware.org/pub/binutils/snapshots
|
||||
|
||||
```bash
|
||||
mkdir build-binutils
|
||||
cd build-binutils
|
||||
```
|
||||
|
||||
## Configuration
|
||||
We execute binutils's `configure` and pass a lot of arguments to it (replace the `X` with the version number):
|
||||
|
||||
```bash
|
||||
../binutils-2.X/configure --target=x86_64-elf --prefix="$HOME/opt/cross" \
|
||||
--disable-nls --disable-werror \
|
||||
--disable-gdb --disable-libdecnumber --disable-readline --disable-sim
|
||||
```
|
||||
- The `target` argument specifies the the x86_64 target architecture.
|
||||
- The `prefix` argument selects the installation directory, you can change it if you like. But be careful that you do not overwrite your system's binutils.
|
||||
- The `disable-nls` flag disables native language support (so you'll get the same english error messages). It also reduces build dependencies.
|
||||
- The `disable-werror` turns all warnings into errors.
|
||||
- The last line disables features we don't need to reduce compile time.
|
||||
|
||||
## Building it
|
||||
Now we can build and install it to the location supplied as `prefix` (it will take a while):
|
||||
|
||||
```bash
|
||||
make
|
||||
make install
|
||||
```
|
||||
Now you should have multiple `x86_64-elf-XXX` files in `$HOME/opt/cross/bin`.
|
||||
|
||||
## Adding it to the PATH
|
||||
To use the tools from the command line easily, you should add the `bin` folder to your PATH:
|
||||
|
||||
```bash
|
||||
export PATH="$HOME/opt/cross/bin:$PATH"
|
||||
```
|
||||
If you add this line to your e.g. `.bashrc`, the `x86_64-elf-XXX` commands are always available.
|
||||
47
pages/cross-compile-libcore.md
Normal file
47
pages/cross-compile-libcore.md
Normal file
@@ -0,0 +1,47 @@
|
||||
---
|
||||
layout: page
|
||||
title: "Cross Compiling: libcore"
|
||||
---
|
||||
So you're getting an ``error: can't find crate for `core` [E0463]`` when using `--target x86_64-unknown-linux-gnu`. That means that you're not running Linux or not using using a x86_64 processor.
|
||||
|
||||
**If you have an x86_64 processor and want a quick fix**, try it with `x86_64-pc-windows-gnu` or `x86_64-apple-darwin` (or simply omit the explicit `--target`).
|
||||
|
||||
The idiomatic alternative and the only option for non x86_64 CPUs is described below. Note that you need to [cross compile binutils], too.
|
||||
[cross compile binutils]: {{ site.url }}/cross-compile-binutils.html
|
||||
|
||||
## Libcore
|
||||
The core library is a dependency-free library that is added implicitly when using `#![no_std]`. It provides basic standard library features like Option or Iterator. The core library is installed together with the rust compiler (just like the std library). But the installed libcore is specific to your architecture. If you aren't working on x86_64 Linux and pass `‑‑target x86_64‑unknown‑linux‑gnu` to cargo, it can't find a x86_64 libcore. To fix this, you can either download it or build it using cargo.
|
||||
|
||||
## Download it
|
||||
You need to download the 64-bit Linux Rust build corresponding to your installed nightly. You can either just update to the current nightly and download the current nightly source [here][Rust downloads]. Or you retrieve your installed version through `rustc --version` and search the corresponding subfolder [here](http://static.rust-lang.org/dist/).
|
||||
[Rust downloads]: https://www.rust-lang.org/downloads.html
|
||||
|
||||
After extracting it and you need to copy the `x86_64-unknown-linux-gnu` folder in `rust-std-x86_64-unknown-linux-gnu/lib/rustlib` to your local Rust installation. For multirust, the right target folder is `~/.multirust/toolchains/nightly/lib/rustlib`. That's it!
|
||||
|
||||
## Build it using cargo
|
||||
The alternative is to use cargo to build libcore. But this variant has one big disadvantage: You have to modify each crate you depend on because it needs to use the same libcore. So you can't just add a crates.io dependency anymore, you need to fork and modify it first.
|
||||
|
||||
If you want to build libcore anyway, you need its source code. You can either clone the [rust repository] \(makes updates easy) or manually [download the Rust source][Rust downloads] \(faster and less memory).
|
||||
[rust repository]: https://github.com/rust-lang/rust
|
||||
|
||||
Now we create a new cargo project named `core`, but delete its `src` folder:
|
||||
|
||||
```bash
|
||||
cargo new core
|
||||
rm -r core/src
|
||||
```
|
||||
|
||||
Then we create a symbolic link named `src` to the `rust/src/libcore` of the Rust source code:
|
||||
|
||||
```bash
|
||||
ln -s ../rust/src/libcore core/src
|
||||
```
|
||||
|
||||
To use our new libcore crate (instead of the one installed together with rust) in our OS, we need to add it as a local dependency in the `Cargo.toml`:
|
||||
|
||||
```toml
|
||||
...
|
||||
[dependencies.core]
|
||||
path = "core"
|
||||
```
|
||||
Now cargo compiles libcore for all Rust targets automatically.
|
||||
Reference in New Issue
Block a user