Jie Han
Jie Han

Reputation: 25

Undefined reference to 'malloc' and more

When I try to complile the dhrystone benchmark, it shows the following errors:

xilinx@pynq:~/dhrystone$ riscv32-unknown-elf-gcc -Os -ffreestanding -nostdlib -o out.elf -Wl,-Bstatic,-T,picorv32.ld dhry_1.o dhry_2.o init.o -lgcc -march=rv32im

/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.o: in function `Proc_1':
dhry_1.c:(.text+0x84): undefined reference to `memcpy'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text+0x100): undefined reference to `memcpy'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.o: in function `main':
dhry_1.c:(.text.startup+0x3c): undefined reference to `malloc'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0x4c): undefined reference to `malloc'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0x84): undefined reference to `strcpy'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0x98): undefined reference to `strcpy'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0xb8): undefined reference to `time'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0xf0): undefined reference to `strcpy'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0x184): undefined reference to `time'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_1.c:(.text.startup+0x254): undefined reference to `strcpy'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: dhry_2.o: in function `.L15':
dhry_2.c:(.text+0x118): undefined reference to `strcmp'
collect2: error: ld returned 1 exit status

It seems that all the missing functions are from stdih.h. But I don't know how to fix it.

heres the commands I used to compile the benchmark.

riscv32-unknown-elf-gcc -c -Qn -DSTKPTR=32768 -march=rv32im -o init.o -Os --std=c99 init.S 
riscv32-unknown-elf-gcc -c -Qn -march=rv32im -o dhry_1.o -Os --std=c99 dhry_1.c
riscv32-unknown-elf-gcc -c -Qn -march=rv32im -o dhry_2.o -Os --std=c99 dhry_2.c

riscv32-unknown-elf-gcc -Os -ffreestanding -nostdlib -o out.elf -Wl,-Bstatic,-T,picorv32.ld init.o dhry_1.o dhry_2.o -lgcc -march=rv32im
riscv32-unknown-elf-objcopy -O binary out.elf out.bin

Can anyone help me pls. Thanks for any help.

With the help of @yflelion, i've changed the fifth line to the following,

riscv32-unknown-elf-gcc -Os -ffreestanding -nostdlib -o out.elf -Wl,-Bstatic,-T,picorv32.ld init.o dhry_1.o dhry_2.o -lgcc -lc -lgloss -march=rv32im

And it shows the following errors,

/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: /opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/lib/libgloss.a(sys_gettimeofday.o): in function `.L5':
sys_gettimeofday.c:(.text+0x48): undefined reference to `__errno'
/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: /opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/lib/libgloss.a(sys_sbrk.o): in function `.L7':
sys_sbrk.c:(.text+0x84): undefined reference to `__errno'
collect2: error: ld returned 1 exit status

with the following command,

riscv32-unknown-elf-gcc -Os -ffreestanding -nostdlib -o out.elf -Wl,-Bstatic,-T,picorv32.ld init.o dhry_1.o dhry_2.o -lgcc -lc -march=rv32im

it shows the following errors,

/opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/bin/ld: /opt/riscv32im/lib/gcc/riscv32-unknown-elf/10.1.0/../../../../riscv32-unknown-elf/lib/libc.a(lib_a-sbrkr.o): in function `_sbrk_r':
sbrkr.c:(.text+0x20): undefined reference to `_sbrk'

Upvotes: 2

Views: 6600

Answers (1)

yflelion
yflelion

Reputation: 1746

When you use nostdlib, the compiler not use the standard system startup files or libraries when linking.
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
The only library you are linking against by using your command line is libgcc, but you are using functions from libc. you need to add -lc and most likely -lgloss. be careful with circular dependencies ( --start-group --end-group).
You can add the -v option and compile a minimalist example with the default options to see how it is done by default.
If there is no reason why you cannot use libc and libgloss by default, it is better to remove nostdlib option.

To compile your example try:

riscv32-unknown-elf-gcc -Os -ffreestanding -nostdlib -o out.elf -Wl,-Bstatic,-T,picorv32.ld init.o dhry_1.o dhry_2.o -lgcc -lc -lgloss -lc -march=rv32im

But the best solution to avoid circular dependencies would be:

riscv32-unknown-elf-gcc -Os -ffreestanding -nostdlib -o out.elf -Wl,-Bstatic,-T,picorv32.ld init.o dhry_1.o dhry_2.o -lgcc --start-group -lc -lgloss --end-group -lgcc -march=rv32im

Upvotes: 2

Related Questions