Skip to content

Install Toolchain

thanos edited this page Mar 21, 2021 · 3 revisions

Install Toolchain

In order to compile the kernel for the Raspberry Pi a Cross Compiler Toolchain is required, because we are compiling on a different architecture (arm) from our host machine (x86).

Cross Compiler

The ARM developer's platform provides the GNU Toolchain already precompiled for various x86 Operating Systems hosts, so there is no need to build our own cross-compiler.

32-bit Cross Compiler

The cross compiler for the AArch32 bare-metal target is called arm-none-eabi-gcc, and we can download it from the ARM developer website. You can also download it from the terminal by grabbing the latest link from the website for the correct host (Linux, Windows, MacOS) and passing it to wget.

From the home directory run the command in order to download for Linux x86_64 host:

wget https://developer.arm.com/-/media/Files/downloads/gnu-rm/10-2020q4/gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2?revision=ca0cbf9c-9de2-491c-ac48-898b5bbc0443&la=en&hash=68760A8AE66026BCF99F05AC017A6A50C6FD832A

64-bit Cross Compiler

The cross compiler for the AArch64 ELF bare-metal target is called aarch64-none-elf-gcc, and we can download it from the ARM developer website. You can also download it from the terminal by grabbing the latest link from the website for the correct host (Linux, Windows) and passing it to wget.

From the home directory run the command in order to download for Linux x86_64 host:

wget https://developer.arm.com/-/media/Files/downloads/gnu-a/10.2-2020.11/binrel/gcc-arm-10.2-2020.11-x86_64-aarch64-none-elf.tar.xz?revision=79f65c42-1a1b-43f2-acb7-a795c8427085&la=en&hash=3BEE623628664E6E1736ABBE7CE5AD76B65B51EA

Extract Toolchain

32-bit Cross Compiler

Run the following command in order to extract the toolchain:

tar -xvf gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2

Now the extracted files will be in the gcc-arm-none-eabi-10-2020-q4-major directory.

You can also remove the .tar:

rm gcc-arm-none-eabi-10-2020-q4-major-x86_64-linux.tar.bz2

64-bit Cross Compiler

Run the following command in order to extract the toolchain:

tar -xvf gcc-arm-10.2-2020.11-x86_64-aarch64-none-elf.tar.xz

Now the extracted files will be in the gcc-arm-10.2-2020.11-x86_64-aarch64-none-elf directory.

You can also remove the .tar:

rm gcc-arm-10.2-2020.11-x86_64-aarch64-none-elf.tar.xz

Add toolchain to PATH

In order for the toolchain to be accessible from every directory we need to add it to our $PATH variable permantly.

We need to define the export to the $PATH variable in the shell configuration files:

  • .bashrc if you are using bash.
  • .zshrc if you are using zsh.

32-bit Cross Compiler

Run the following command, that adds the export command at then end of your shell configuration file (here .zshrc for example, change to .bashrc accordingly):

echo 'export PATH="$HOME/gcc-arm-none-eabi-10-2020-q4-major/bin:$PATH"' >> $HOME/.zshrc

Load the new $PATH into the current shell session using the source command:

source ~/.zshrc

or for bash:

source ~/.bashrc

Now you can type arm-none-eabi-gcc from everywhere:

$ arm-none-eabi-gcc -v
...
gcc version 10.2.1 20201103 (release) (GNU Arm Embedded Toolchain 10-2020-q4-major)

64-bit Cross Compiler

Run the following command, that adds the export command at then end of your shell configuration file (here .zshrc for example, change to .bashrc accordingly):

echo 'export PATH="$HOME/gcc-arm-10.2-2020.11-x86_64-aarch64-none-elf/bin:$PATH"' >> $HOME/.zshrc

Load the new $PATH into the current shell session using the source command:

source ~/.zshrc

or for bash:

source ~/.bashrc

Now you can type arm-none-eabi-gcc from everywhere:

$ aarch64-none-elf-gcc -v
...
gcc version 10.2.1 20201103 (GNU Toolchain for the A-profile Architecture 10.2-2020.11 (arm-10.16))

Using Toolchain

In order to compile a hello_world.c program for an AArch32 bare-metal target you run:

arm-none-eabi-gcc --specs=nosys.specs hello_world.c -o hello_world

Where --specs=nosys.specs is allowing semi-hosting.

Bare Metal Target Compiler Flags

Because we are not writing code to be run in an operating system we have to set some flags to the compiler in order for our bare-metal code to work properly.

  • -ffreestanding: A freestanding environment is an environment in which the standard library may not exist, and program startup may not necessarily be at main. The option -ffreestanding directs the compiler to not assume that standard functions have their usual definition.

  • -fPIC: Position Independent Code means that the generated machine code is not dependent on being located at a specific address in order to work.

  • -nostdlib: Don't use the C standard library. Most of the calls in the C standard library eventually interact with the operating system. We are writing a bare-metal program, and we don't have any underlying OS, so the C standard library is not going to work for us anyway.

Other Options

There are many other selections for a toolchain either precompiled or custom. Some notable are the following:

Previous Page

Home

Next Page

Serial Connection

Clone this wiki locally