Search code examples
cgcclinker-errorsriscvgnu-toolchain

RISC V linker cannot find -lgcc


I'm trying to compile C code for GCC, but the linker is unable to find libgcc. I want to compile some simple tests for an RV32I core.

When I try to use the modulo operator, GCC generates a call to the soft implementation of the mod instruction.

call    __modsi3

But linking fails because I think im missing libgcc

test.c:(.text+0x34): undefined reference to `__modsi3'

This is my compilation command:

riscv64-unknown-elf-gcc -lm -lgcc -static -march=rv32i -mabi=ilp32 -nostartfiles -ffreestanding  crt0.s -O0

These are my linker options:

-lgcc -b elf32-littleriscv -m elf32lriscv

If I try to use the -lgcc option on the linker, it will throw the following error:

riscv64-unknown-elf-ld: cannot find -lgcc

I also tried to provide directly the path of libgcc.a. But it didn't help. I got the path of libgcc.a this way:

riscv64-unknown-elf-gcc -march=rv32i -print-libgcc-file-name

I added the path directly to my compilation command like so:

-L="/mnt/c/Users/camin/Documents/opt/riscv/lib/gcc/riscv64-unknown-elf/10.2.0/libgcc.a"

Also, the way I compiled the toolchain might be the issue, I'm using the RISCV toolchain with multi-lib support

I followed the instructions from https://github.com/riscv/riscv-gnu-toolchain with some slight modification (-j12)

./configure --prefix=/opt/riscv --enable-multilib
make -j12

This also installed the toolchain so I didn't had to make install

Thanks.


Solution

  • The problem comes from -L="/mnt/c/Users/camin/Documents/opt/riscv/lib/gcc/riscv64-unknown-elf/10.2.0/libgcc.a" you must only give the path to the L option. Your option should be :-L="/mnt/c/Users/camin/Documents/opt/riscv/lib/gcc/riscv64-unknown-elf/10.2.0"

    the enable-multilib is working well, it only provide libraries for set of archs. However be careful to give the path of the libgcc corresponding to your arch. When you enable multilib the default one will be a 64 bit versions. Normally for rv32i, the option you need is :-L="/mnt/c/Users/camin/Documents/opt/riscv/lib/gcc/riscv64-unknown-elf/10.2.0/rv32i/ilp32".

    If you don't give the good library you will probably get ABI is incompatible with that of the selected emulation error.

    It would be easier to use the riscv64-unknown-elf-gcc driver to make the link because he knows all the options better. Normally it will find the necessary libraries (libgcc, libgloss ....) automatically for your arch.