From 710fa81c27d325f3eadad506fb77f9c45a7b6735 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Sat, 24 Oct 2015 15:54:59 +0200 Subject: [PATCH] Add and link article about cross compiling binutils --- _posts/2015-08-18-multiboot-kernel.md | 3 ++ rust-os/cross-compile-binutils.md | 47 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 rust-os/cross-compile-binutils.md diff --git a/_posts/2015-08-18-multiboot-kernel.md b/_posts/2015-08-18-multiboot-kernel.md index 96c4e2e9..f17e3a82 100644 --- a/_posts/2015-08-18-multiboot-kernel.md +++ b/_posts/2015-08-18-multiboot-kernel.md @@ -172,6 +172,8 @@ Idx Name Size VMA LMA File off Algn 1 .text 0000000b 0000000000100020 0000000000100020 000000a0 2**4 CONTENTS, ALLOC, LOAD, READONLY, CODE ``` +_Note_: The `ld` and `objdump` commands are platform specific. If you're not working on x86_64 architecture, you will need to [cross compile binutils]. Then use `x86_64-elf-ld` and `x86_64-elf-objdump` instead of `ld` and `objdump`. +[cross compile binutils]: {{ site.url }}/rust-os/cross-compile-binutils.html ## Creating the ISO The last step is to create a bootable ISO image with GRUB. We need to create the following directory structure and copy the `kernel.bin` to the right place: @@ -283,6 +285,7 @@ Some comments (see the [Makefile tutorial] if you don't know `make`): - the `$(wildcard src/arch/$(arch)/*.asm)` chooses all assembly files in the src/arch/$(arch)` directory, so you don't have to update the Makefile when you add a file - the `patsubst` operation for `assembly_object_files` just translates `src/arch/$(arch)/XYZ.asm` to `build/arch/$(arch)/XYZ.o` - the `$<` and `$@` in the assembly target are [automatic variables] +- if you're using [cross-compiled binutils][cross compile binutils] just replace `ld` with `x86_64-elf-ld` [automatic variables]: https://www.gnu.org/software/make/manual/html_node/Automatic-Variables.html diff --git a/rust-os/cross-compile-binutils.md b/rust-os/cross-compile-binutils.md new file mode 100644 index 00000000..25820ac8 --- /dev/null +++ b/rust-os/cross-compile-binutils.md @@ -0,0 +1,47 @@ +--- +layout: page +title: Cross Compile Binutils +category: "rust-os" +--- +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.