Skip to content

RISC V binutils

Kirill Korolev edited this page Dec 2, 2019 · 8 revisions

How to get binutils source files

RISC-V binutils must be build manually, so first you'll need to get sources.

The source files are stored in GitHub repository in the riscv project. Use git to get a local clone of the repository:

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

How to build binutils

This cheat-sheet is based on GitHub README. See it if you have any problem or need more information.

# Install prerequisites 
sudo apt-get install autoconf automake autotools-dev curl libmpc-dev libmpfr-dev libgmp-dev gawk build-essential bison flex texinfo gperf libtool patchutils bc zlib1g-dev libexpat-dev

# Create a folder where the binutils will be installed (can be any)
mkdir /opt/cross

# Create a folder for a build process (can be any):
cd /tmp
mkdir build-binutils && cd build-binutils

# Configure build (configures for RV64GC by default):
/path/to/binutils/configure --prefix=/opt/cross

# Make sources (can dump a lot of info):  
make
  
# Install (can dump a lot of info, usually requires sudo):  
sudo make install

# Update PATH variable to have binaries available:
export PATH=$PATH;/opt/cross/bin

Create a RISC-V binary with binutils

  • Create a file with assembler code and save it as <test name>.s
Note: Specify __start: before the first instruction of your program. This is required by mipt-mips to find a starting point of execution.
  • Generate an object file:
riscv64-unknown-elf-as <test name>.s -o <test name>.o
  • Convert the object file into the binary file:
riscv64-unknown-elf-ld <test name>.o -o <test name>.out
  • (optional) Look the content of .out using (pay attention only to .text section):
riscv64-unknown-elf-objdump -D <test name>.out

Note that in addition to assembler this RISC-V GNU toolchain also includes C and C++ cross-compiler, so you can also build C/C++ code into a RISC-V binary on regular x86 machines.

Clone this wiki locally