Error in running baremetal code for riscv (clang-llvm)

Hi all,

I get the following error after running the baremetal code [link.ld,init.S,systemcalls.c]

root@user:/home/user# clang -mcmodel=medany -ffunction-sections -fdata-sections -ffreestanding /home/user/Downloads/riscv-master/demo/baremetal/init.s -fuse-ld=ld -mno-relax --gcc-toolchain=$(RISCV) -march=rv64gc -o kernel_baseline --rtlib=libgcc -static -nostdlib -nostartfiles -Wl,-L/usr/bin/ld -Wl,-T/home/user/Downloads/riscv-master/demo/baremetal/link.ld -lm -lgcc /home/user/Downloads/riscv-master/demo/baremetal/syscalls.c -o /home/user/Desktop/bare.elf
RISCV: command not found
/home/user/Documents/riscv/_install/bin/riscv64-unknown-elf-ld: cannot find -lgcc: No such file or directory
clang-14: error: ld command failed with exit code 1 (use -v to see invocation)
root@user-ML:/home/user# clang -mcmodel=medany -ffunction-sections -fdata-sections -ffreestanding /home/user/Downloads/riscv-master/demo/baremetal/init.s -fuse-ld=ld -mno-relax --gcc-toolchain=$(RISCV) -march=rv64gc -o kernel_baseline --rtlib=libgcc -static -nostdlib -nostartfiles -Wl,-L/usr/bin/ld -Wl,-T/home/user/Downloads/riscv-master/demo/baremetal/link.ld -lm -lgcc /home/user/Downloads/riscv-master/demo/baremetal/syscalls.c -o /home/user/Desktop/bare.elf -v
RISCV: command not found
clang version 14.0.6 (GitHub - llvm/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org. f28c006a5895fc0e329fe15fead81e37457cb1d1)
Target: riscv64-unknown-unknown-elf
Thread model: posix
InstalledDir: /usr/local/bin
“/usr/local/bin/clang-14” -cc1as -triple riscv64-unknown-unknown-elf -filetype obj -main-file-name init.s -target-feature +m -target-feature +a -target-feature +f -target-feature +d -target-feature +c -target-feature -relax -target-feature -save-restore -fdebug-compilation-dir=/home/user -dwarf-debug-producer “clang version 14.0.6 (GitHub - llvm/llvm-project: The LLVM Project is a collection of modular and reusable compiler and toolchain technologies. Note: the repository does not accept github pull requests at this moment. Please submit your patches at http://reviews.llvm.org. f28c006a5895fc0e329fe15fead81e37457cb1d1)” -dwarf-version=5 -mrelocation-model static -target-abi lp64d -mrelax-all --mrelax-relocations -o /tmp/init-4a0936.o /home/user/Downloads/riscv-master/demo/baremetal/init.s
“/usr/local/bin/clang-14” -cc1 -triple riscv64-unknown-unknown-elf -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -main-file-name syscalls.c -static-define -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -nostdsysteminc -mcmodel=medium -target-feature +m -target-feature +a -target-feature +f -target-feature +d -target-feature +c -target-feature -relax -target-feature -save-restore -target-abi lp64d -msmall-data-limit 8 -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -ffunction-sections -fdata-sections -fcoverage-compilation-dir=/home/user -resource-dir /usr/local/lib/clang/14.0.6 -isysroot /home/user/Documents/riscv/_install/riscv64-unknown-elf/ -internal-isystem /home/user/Documents/riscv/_install/riscv64-unknown-elf/include -fdebug-compilation-dir=/home/user -ferror-limit 19 -fno-signed-char -fgnuc-version=4.2.1 -fcolor-diagnostics -faddrsig -o /tmp/syscalls-399666.o -x c /home/user/Downloads/riscv-master/demo/baremetal/syscalls.c
clang -cc1 version 14.0.6 based upon LLVM 14.0.6 default target riscv64-unknown-elf
#include “…” search starts here:
#include <…> search starts here:
/home/user/Documents/riscv/_install/riscv64-unknown-elf/include
/usr/local/lib/clang/14.0.6/include
End of search list.
“/home/user/Documents/riscv/_install/bin/riscv64-unknown-elf-ld” --sysroot=/home/user/Documents/riscv/_install/riscv64-unknown-elf/ -m elf64lriscv /tmp/init-4a0936.o -L/usr/bin/ld -T/home/user/Downloads/riscv-master/demo/baremetal/link.ld -lm -lgcc /tmp/syscalls-399666.o -L/home/user/Documents/riscv/_install/riscv64-unknown-elf//lib -o /home/user/Desktop/bare.elf
/home/user/Documents/riscv/_install/bin/riscv64-unknown-elf-ld: cannot find -lgcc: No such file or directory

It would be very helpful if we could run this.

Maybe “$(RISCV)” is the problem.

command not found
/home/user/Documents/riscv/_install/bin/riscv64-unknown-elf-ld

You probably did not want to use Documents folder.

1 Like

I have a small doubt that is for baremetal program we need to use -lgcc libraries or not ?
The path is same for gcc-toolchain installed in same path(riscv64-unknown-elf-gcc) is working well.

root@user:/# riscv64-unknown-elf-ld -lgcc
riscv64-unknown-elf-ld: cannot find -lgcc: No such file or directory

See here for some general guidance on libgcc. -nostdlib means that libgcc won’t be linked by default - copying from the GCC documentation

In most cases, you need libgcc.a even when you want to avoid other standard libraries. In other words, when you specify -nostdlib or -nodefaultlibs you should usually specify -lgcc as well. This ensures that you have no unresolved references to internal GCC library subroutines. (An example of such an internal subroutine is “__main”, used to ensure C++ constructors are called.)

1 Like

@asb @tschuett Thank you both.
I omitted -lgcc and tried to run the code using the following command.Then I could generate elf.

root@user-ML:/# clang -mcmodel=medany -ffunction-sections -fdata-sections /home/user/Documents/baremetal/init.s -fuse-ld=ld -mno-relax --gcc-toolchain=$RISCV -march=rv64gc_zbb -o kernel_baseline --rtlib=libgcc -static -nostdlib -nostartfiles -Wl,-L/usr/bin/ld -Wl,-T/home/user/Documents/baremetal/link.ld -lm /home/user/Documents/baremetal/syscalls.c -o /home/user/Documents/bare.elf
/home/user/Documents/riscv/_install/lib/gcc/riscv64-unknown-elf/12.1.0/…/…/…/…/bin/riscv64-unknown-elf-ld: warning: /home/user/Documents/bare.elf has a LOAD segment with RWX permissions

Please correct me if I am wrong.

You’ll just have problems if the future code changes mean the compiler generates any calls to functions that are provided by libgcc, in which case you’ll see linker errors for undefined symbols and you’d need to add -lgcc and ensure the library search paths (-L) are specified to that libgcc can be found.

1 Like

Yes.I do understand the relevance of including this linker flag.

  1. Could you suggest an exact way to include libgcc?
    It should be specified in cmake or the path to be added.
  2. If a path to libgcc is to be specified from where can we find the installation path to libgcc?
    [ I have already added the llvm-project’s and riscv-gcc-toolchain’s install to PATH.]

Please correct me.