# ARM AArch64

## Linux VM Setup

This guide assumes that you have already setup your Linux environment and are already in it. If you haven't set it up already, we recommend using UTM to do so.

{% embed url="<https://mac.getutm.app/>" %}
Get UTM
{% endembed %}

## 1. Use Precompiled Toolchain (Recommended)

Compiling the entire toolchain yourself might take a long time, as well as a large amount of space, so we have prepared a precompiled version of it here.

### Step 1: Download the zipped toolchain

{% embed url="<https://cdn.cloud.vjssn.dev/riscv64-unknown-elf-aarch64.zip>" %}
Download Compiled RISC-V GNU-Toolchain for Linux-AArch64
{% endembed %}

{% hint style="info" %}
You can either download this on your host OS (macOS) and SCP it to your guest OS (Linux), or directly download it from inside the guest OS which is more convenient.
{% endhint %}

```bash
wget https://cdn.cloud.vjssn.dev/riscv64-unknown-elf-aarch64.zip
```

Running this command in your terminal will download the file to the directory that you are currently in. By default, the terminal will open in your home directory, which is where the rest of this guide assumes you will place the downloaded file.

Invoking the `cd` command without any arguments will bring you to your user's home directory if you are not in it.

### Step 2: Unzip toolchain

```bash
unzip riscv64-unknown-elf-aarch64.zip
```

### Step 3: Add binaries to PATH

```bash
export PATH=$PWD/riscv64-unknown-elf/bin
```

After exporting the PATH to the binaries, you will be able to use all the tools inside the `riscv64-unknown-elf/bin` folder. To use the cross-compiler, simply call `riscv64-unknown-elf-gcc`.&#x20;

{% hint style="info" %}
Note that without completing step 4, you will need to export the PATH (step 3) every time you start a new terminal session.
{% endhint %}

### Step 4: Place PATH in \~/.bashrc (Optional)

1. Adding the PATH to your `~/.<shell>rc` file automatically invokes the command for you on every startup.

{% hint style="info" %}
As there are many shells in use, you may need to change the `<shell>` to the one you are using. In this guide, we assume that you are using bash as it is the default for most Linux Distributions.
{% endhint %}

2. To edit the `~/.bashrc` you can use any editor you want, but terminal editors like `nano` or `vi` are the most convenient.

```bash
nano ~/.bashrc
```

3. After invoking the command above, simply add the export command shown in step 3 to a new line in the `.bashrc` file.

## 2. Compiling Your Own Toolchain (Advanced)

### Step 1: Clone the RISC-V GNU Toolchain

```bash
git clone https://github.com/riscv/riscv-gnu-toolchain
```

### Step 2: Install prerequisites

You will need to install some prerequisites for the build process. Different Linux distros have different package managers, so we will provide separate commands. **You only need to run the one for your distro.**

#### Debian/Ubuntu

```bash
sudo apt update && sudo apt install autoconf automake autotools-dev curl python3 python3-pip libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev ninja-build git cmake libglib2.0-dev
```

#### Fedora/CentOS/RHEL

```bash
sudo yum install autoconf automake python3 libmpc-devel mpfr-devel gmp-devel gawk  bison flex texinfo patchutils gcc gcc-c++ zlib-devel expat-devel
```

**Arch**

```bash
sudo pacman -Syyu autoconf automake curl python3 libmpc mpfr gmp gawk base-devel bison flex texinfo gperf libtool patchutils bc zlib expat
```

### Step 3: Build Toolchain

```bash
cd riscv-gnu-toolchain
./configure --prefix=$PWD/install --enable-multilib
make
```

### Step 4: Add binaries to PATH

```bash
export PATH=$PATH:$PWD/install/bin
```

### Step 5: Place PATH in \~/.\<shell>rc (Optional)

You can follow the same steps as in the [Precompiled Toolchain Step 4](#step-4-place-path-in-.bashrc-optional)

## 3. Using your cross-compiler

```bash
riscv64-unknown-elf-gcc
```

You can invoke this command to compile for RISC-V


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://csc3050.vjssn.dev/environment-setup/risc-v-gnu-toolchain/arm-aarch64.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
