Compilation

Toolchain 32 bits install

$ 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
$ git clone --recursive https://github.com/riscv/riscv-gnu-toolchain
$ cd riscv-gnu-toolchain
$ ./configure --prefix=[path where to put toolchain, ex : /opt/riscv/toolchain]  --with-arch=rv32gc --with-abi=ilp32
$ make

You have time for :coffee:

Note : $RISCV pointes to prefix defined previously ([path where to put toolchain, ex : /opt/riscv/toolchain])

export PATH=$RISCV/bin:$PATHto have acces to you new gcc, or call full path to your gcc binary

Compile binary for RISC-V

Explanation of some GCC arguments : All Aboard, Part 1: The -march, -mabi, and -mtune arguments to RISC-V Compilers

Compile assembly file to binary

riscv32-unknown-elf-as strlen.s -o strlen

Compile .c to binary

riscv32-unknown-elf-gcc -march=rv32i -mabi=ilp32 -Wall -O0 -o strlen strlen.c

Universal Makefile

GCC_RISCV = riscv32-unknown-elf-gcc
OBJDUMP_RISCV = riscv32-unknown-elf-objdump
AS_RISCV = riscv32-unknown-elf-as
LD_RISCV = riscv32-unknown-elf-ld
CFLAGS = -march=rv32i -mabi=ilp32 -Wall -O0 #RISC-V 32 without floating point inst and without Compressed Instructions
LD_FLAGS =

junk:
	echo "You should call this makefile with args, like 'run-x' to build and run x.{c,s}, or 'x.txt' to got desassembly from x.{c,s}"

%.bin:%.c
	${GCC_RISCV} ${CFLAGS} ${LD_FLAGS} -o $@ $^

%.o : %.s
	${AS_RISCV} -c $< -o $@

%.o : %.c
	${GCC_RISCV} ${CFLAGS} -c $< -o $@

%.txt: %.bin
	${OBJDUMP_RISCV} -D $< > $@

run-%: %.bin
	qemu-riscv32 $<

clean :
	rm -f *.bin *.o


.PHONY: clean run-% junk
.PRECIOUS: %.bin

Run on QEMU RISC-V

Compile QEMU :), with make build-qemu.

Usage : qemu-riscv32 strlen

Getting Started - QEMU for RISC-V

Note on QEMU user mode : Transparently running binaries from any architecture in Linux with QEMU and binfmt_misc