I am building a RISC-V cpu core, and i am trying to build some C code for it. I have some basic code compiling and linking the way i want and working just fine, for example this:
asm("li sp, 0x390");
asm("li s0, 0x390");
asm("li ra, 0");
int main(void){
char *memptr = 0 ;
int sum = 0;
for(int i = 0; i < 8; i++)
*(memptr + i) = i;
for(int i = 0; i < 8; i++)
sum += *(memptr + i);
*((int*)0xC) = sum;
asm("lw x31, 0xc(x0)");
}
and compile/link with these commands:
riscv64-unknown-elf-gcc -nostartfiles -nostdlib -mno-relax -march=rv32i -mabi=ilp32 -c main.c
riscv64-unknown-elf-ld -nostartfiles -nostdlib -march=rv32i -melf32lriscv -o main -T link.ld main.o
my linker script is the following:
OUTPUT_ARCH( "riscv" )
ENTRY(main)
MEMORY
{
progmem (rx) : ORIGIN = 0x10000000, LENGTH = 8K
ram (!rx) : ORIGIN = 0x20000000, LENGTH = 64K
}
SECTIONS
{
.text : {
main.o(.text)
} > progmem
.data : {
main.o(.rodata)
main.o(.data)
main.o(.sdata)
main.o(.bss)
} > ram
}
With this setup, i get the results i want no problem. I get my text section (which i put in the program memory), and i get the data section with all of the subsections put in there and load it straight into ram (it works for what im doing right now).
The problems begin when i try to use floating point addition:
asm("li sp, 0x390");
asm("li s0, 0x390");
asm("li ra, 0");
float a = 1.7;
float b = 0.7;
int main(void){
int *memptr = (int*)0x8;
float c = a - b;
}
trying to link after compilation results in this error:
riscv64-unknown-elf-ld -nostartfiles -march=rv32i -melf32lriscv -o main -T link.ld main.o
riscv64-unknown-elf-ld: main.o: in function `main':
main.c:(.text+0x3c): undefined reference to `__subsf3'
I can see in the assembly file (using gcc -S option) that there is an instruction call __subsf3, but there is no such label anywhere in the .text section. I believe i have to somehow link the math library for this to work, so i have tried adding #include "math.h", and then adding various flags to the ld command like "-lc", "-lgcc", "-lm", but it results in these errors:
riscv64-unknown-elf-ld: cannot find -lc
riscv64-unknown-elf-ld: cannot find -lgcc
riscv64-unknown-elf-ld: cannot find -lm
By trial and error i have managed to come up with this
riscv64-unknown-elf-gcc -nostartfiles -mno-relax -march=rv32i -mabi=ilp32 -Wl,-emain -lm main.c -o main
which almost does what i want, but does not use my link script. if i add -Wl,link.ld then it again doesnt work, giving out errors:
/usr/lib/gcc/riscv64-unknown-elf/9.1.0/../../../../riscv64-unknown-elf/bin/ld: main.o:(.sdata+0x0): multiple definition of `a'; /tmp/cc0LmWB4.o:(.sdata+0x0): first defined here
/usr/lib/gcc/riscv64-unknown-elf/9.1.0/../../../../riscv64-unknown-elf/bin/ld: main.o:(.sdata+0x4): multiple definition of `b'; /tmp/cc0LmWB4.o:(.sdata+0x4): first defined here
/usr/lib/gcc/riscv64-unknown-elf/9.1.0/../../../../riscv64-unknown-elf/bin/ld: main.o: in function `main':
main.c:(.text+0xc): multiple definition of `main'; /tmp/cc0LmWB4.o:main.c:(.text+0xc): first defined here
/usr/lib/gcc/riscv64-unknown-elf/9.1.0/../../../../riscv64-unknown-elf/bin/ld: error: no memory region specified for loadable section `.sdata'
collect2: error: ld returned 1 exit status
I would really appreciate if someone could explain me what im doing wrong, and how i should actually go about building the code using the floating point math above for bare metal RISC-V, and get ld to add the math functions in my .text section.
-nostartfiles -nostdlib
aren'tld
options. It will parse them as single-letter options like-n -o startfiles
-n -o stdlib
. A later-o
option overrides the two earlier output-file name options.libm.a
. So use-lm
when statically linking.