1

I'm trying to link two risc-v elf files together with ld.lld, but ld.lld is giving me the following error:

ld.lld: error: undefined symbol: __divdi3

I suppose that I need to link my files with some helper functions, but after looking for it in my clang lib folder (/usr/local/Cellar/llvm/15.0.6/lib/clang/15.0.6/lib), but in the directory, there is only a folder for darwin, as you can see here:

.
└── darwin
    ├── libclang_rt.asan_osx_dynamic.dylib
    ├── libclang_rt.cc_kext.a
    ├── libclang_rt.fuzzer_interceptors_osx.a
[more]
    ├── libclang_rt.xray-profiling_osx.a
    └── libclang_rt.xray_osx.a

So does anyone know how should I fix this link error/get the rv64i helper functions?

Thanks!

Edit: Here are the commands that I used:

/usr/local/Cellar/llvm/15.0.6/bin/clang --target=riscv64 -march=rv64i  -fno-builtin -nostdlib -ffreestanding -Wall -Wextra -Wwrite-strings -Wstrict-prototypes -pedantic -c -o build/main.o bios/main.c
riscv64-unknown-elf-as -march=rv64i -o build/start.o src/start.S
/usr/local/Cellar/llvm/15.0.6/bin/ld.lld -T link.ld -O2 -nostdlib --oformat=binary -o output.bin build/main.o build/start.o 
Cyao
  • 727
  • 4
  • 18
  • 1) Could you share the command used? 2) Is this reproducable with using gcc instead of clang? If not, it might be related to a current clang issue. – Krokomot Jan 08 '23 at 09:53
  • @Krokomot Just edited my post to include the commands I used. Also idk if this is reproducable with using gcc, didn't try, but I think it is only a clang issue, since each compiler has it's own helper functions – Cyao Jan 08 '23 at 09:59
  • the .o is with the `-o` file, which specifies the name of the output file, `build/main.o` is not a input – Cyao Jan 08 '23 at 10:05
  • Have a nice break! – Cyao Jan 08 '23 at 10:17
  • You probably need to link the library output by `clang -print-libgcc-file-name`. Or just use `clang` as the linker command. – user3840170 Jan 08 '23 at 10:57
  • @user3840170 The file that clang gove me by using `clang -print-libgcc-file-name` doesn't exist on my computer, which is a bit weird, do you know how to fix this? – Cyao Jan 08 '23 at 13:34

2 Answers2

1

__divdi3 is as a routine from the compiler supporting library. In case of gcc it is called libgcc, in case of clang it's compiler-rt. You need to have compiler-rt built from the architecture in question (RISC-V) and make it available to compiler via e.g. sysroot or something similar (or link explicitly).

Alternatively, just do not use 64-bit division in your program, so the call will not be generated :)

Anton Korobeynikov
  • 9,074
  • 25
  • 28
1

You can get a copy of the source of the functions in libgcc here: https://github.com/gcc-mirror/gcc/tree/releases/gcc-12/libgcc/config/riscv Just copy them and place them in your source and they will compile for whatever arch your compiling for

The function __divdi3 is in the div.c file

Cyao
  • 26
  • 3