workaround for gcc's optimizer breaking dynamic symbol resolution
[musl] / src / time / clock_gettime.c
1 #include <time.h>
2 #include <errno.h>
3 #include <stdint.h>
4 #include "syscall.h"
5 #include "libc.h"
6
7 int __vdso_clock_gettime(clockid_t, struct timespec *) __attribute__((weak));
8 static int (*cgt)(clockid_t, struct timespec *) = __vdso_clock_gettime;
9
10 int __clock_gettime(clockid_t clk, struct timespec *ts)
11 {
12         int r;
13         if (cgt) return cgt(clk, ts);
14         r = __syscall(SYS_clock_gettime, clk, ts);
15         if (!r) return r;
16         if (r == -ENOSYS) {
17                 cgt = 0;
18                 if (clk == CLOCK_REALTIME) {
19                         __syscall(SYS_gettimeofday, clk, ts, 0);
20                         ts->tv_nsec = (int)ts->tv_nsec * 1000;
21                         return 0;
22                 }
23                 r = -EINVAL;
24         }
25         errno = -r;
26         return -1;
27 }
28
29 weak_alias(__clock_gettime, clock_gettime);