title: Undefined reference to 'malloc' and more
body: <p>When I try to complile the dhrystone benchmark, it shows the following errors:</p>
  <pre><code>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
  </code></pre>
  <p>It seems that all the missing functions are from stdih.h. But I don't know how to fix it.</p>
  <p>heres the commands I used to compile the benchmark.</p>
  <pre><code>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
  </code></pre>
  <p>Can anyone help me pls. Thanks for any help.</p>
  <p>With the help of @yflelion, i've changed the fifth line to the following,</p>
  <pre><code>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
  </code></pre>
  <p>And it shows the following errors,</p>
  <pre><code>/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
  </code></pre>
  <p>with the following command,</p>
  <pre><code>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
  </code></pre>
  <p>it shows the following errors,</p>
  <pre><code>/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'
  </code></pre>