foss-for-synopsys-dwc-arc-processors / toolchain

Repository containing releases of prebuilt GNU toolchains for DesignWare ARC Processors from Synopsys (available from "releases" link below).
http://www.synopsys.com/IP/ProcessorIP/ARCProcessors/Pages/default.aspx
GNU General Public License v3.0
92 stars 48 forks source link

clock() is not provided by Newlib's libsemihost for RISC-V #606

Closed abrodkin closed 5 months ago

abrodkin commented 6 months ago

Consider the following example:

#include "stdio.h"
#include "time.h"

int main(){
    long Begin_Time = clock();
    printf("Hello World\n");
}

Try to compile it for RISC-V with libsemihost with help of ARC GNU tools for ARC-V processors 2023.12 (https://github.com/foss-for-synopsys-dwc-arc-processors/toolchain/releases/tag/arc-2023.12-release):

riscv64-elf-gcc --specs=semihost.specs --specs=arcv.specs -mabi=ilp32 -mtune=rmx100 -march=rv32i -T arcv.ld clock.c
.../riscv64-unknown-elf/bin/../lib/gcc/riscv64-elf/13.2.0/../../../../riscv64-elf/bin/ld: .../riscv64-unknown-elf/bin/../lib/gcc/riscv64-elf/13.2.0/../../../../riscv64-elf/lib/rv32i/ilp32/libc.a(libc_a-timesr.o): in function `_times_r':
.../riscv64-elf/src/newlib/newlib/libc/reent/timesr.c:52: undefined reference to `_times'
collect2: error: ld returned 1 exit status

That happens because Newlib implements clock() (see https://github.com/bminor/newlib/blob/master/newlib/libc/time/clock.c#L53) with help of _times_r() (see https://github.com/bminor/newlib/blob/master/newlib/libc/reent/timesr.c#L47), which in its turn uses _times(), which is only implemented in libgloss (see https://github.com/bminor/newlib/blob/master/libgloss/riscv/sys_times.c#L22), but not libsemihost.

So, if we want to use clock() (which is BTW is a valid C99 function) then we need to enhance Newlib to be capable of doing it with libsemihost for RISC-V.

shahab-vahedi commented 6 months ago

@kolerov has kindly offered to look into this.

kolerov commented 6 months ago

@abrodkin I see that _times relies on gettimeofday system call which is available in libgloss environment. However, that system call is not even defined in semihosting specification for RISC-V (it uses ARM's set of semihosting system calls). In short, the implementation for libgloss does not fit to semihosting interface. In short - there was no a motivation doing it. I can try to implement it without using gettimeofday

kolerov commented 6 months ago

Here is the pull request which may fix this issue for semihosting: https://github.com/foss-for-synopsys-dwc-arc-processors/newlib/pull/59

abrodkin commented 5 months ago

Fixed with the most recent GNU toolchain build 6698.

$ riscv64-elf-gcc --version
riscv64-elf-gcc (RISC-V elf toolchain - build 6698) 14.0.1 20240226 (experimental)

$ riscv64-elf-gcc --specs=semihost.specs --specs=arcv.specs -mabi=ilp32 -mtune=rmx100 -march=rv32i -T arcv.ld clock.c

$ nm a.out | grep clock
000001f8 T clock