From 5e46749c64d51f50f8511ed99c1266d7c13e182b Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Thu, 24 Oct 2019 19:19:33 +0000 Subject: Use clock_gettime to implement gettimeofday. Consolidate generic gettimeofday implementation to use clock_gettime. Linux ports that still provide gettimeofday through vDSO are not changed. Remove sysdeps/unix/clock_gettime.c, which implemented clock_gettime using gettimeofday; new OS ports must provide a real implementation of clock_gettime. Rename sysdeps/mach/gettimeofday.c to sysdeps/mach/clock_gettime.c and convert into an implementation of clock_gettime. It only supports CLOCK_REALTIME; Mach does not appear to have any support for monotonic clocks. It uses __host_get_time, which provides at best microsecond resolution. Hurd is currently using sysdeps/posix/clock_getres.c for clock_getres; its output for CLOCK_REALTIME is based on sysconf (_SC_CLK_TCK), and I do not know whether that gives the correct result. Unlike settimeofday, there are no known uses of gettimeofday's vestigial "get time zone" feature that are not bugs. (The per-process timezone support in localtime and friends is unrelated, and the programs that set the kernel's offset between the hardware clock and UTC do not need to read it back.) Therefore, this feature is dummied out. Henceforth, if gettimeofday's "struct timezone" argument is not NULL, it will write zeroes to both fields. Any program that is actually looking at this data will thus think it is running in UTC, which is probably more correct than whatever it was doing before. [__]gettimeofday no longer has any internal callers, so we can now remove its internal prototype and PLT bypass aliases. The __gettimeofday@GLIBC_2.0 export remains, in case it is used by any third-party code. It also allows to simplify the arch-specific implementation on x86 and powerpc to remove the hack to disable the internal route to non iFUNC variant for internal symbol. This patch also fixes a missing optimization on aarch64, powerpc, and x86 where the code used on static build do not use the vDSO. Checked on x86_64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu, powerpc64-linux-gnu, powerpc-linux-gnu, and aarch64-linux-gnu. Co-authored-by: Zack Weinberg Reviewed-by: Lukasz Majewski --- sysdeps/unix/clock_gettime.c | 56 ------------------ sysdeps/unix/syscalls.list | 1 - sysdeps/unix/sysv/linux/aarch64/gettimeofday.c | 34 +++++------ sysdeps/unix/sysv/linux/alpha/gettimeofday.c | 22 ++++++++ sysdeps/unix/sysv/linux/alpha/osf_gettimeofday.c | 11 +++- sysdeps/unix/sysv/linux/alpha/tv32-compat.h | 6 ++ sysdeps/unix/sysv/linux/gettimeofday.c | 49 ---------------- sysdeps/unix/sysv/linux/i386/gettimeofday.c | 35 ------------ sysdeps/unix/sysv/linux/powerpc/gettimeofday.c | 69 +++++++---------------- sysdeps/unix/sysv/linux/x86/gettimeofday.c | 43 ++++++-------- sysdeps/unix/sysv/linux/x86_64/x32/gettimeofday.c | 1 + sysdeps/unix/sysv/linux/x86_64/x32/syscalls.list | 1 - 12 files changed, 90 insertions(+), 238 deletions(-) delete mode 100644 sysdeps/unix/clock_gettime.c create mode 100644 sysdeps/unix/sysv/linux/alpha/gettimeofday.c delete mode 100644 sysdeps/unix/sysv/linux/gettimeofday.c delete mode 100644 sysdeps/unix/sysv/linux/i386/gettimeofday.c create mode 100644 sysdeps/unix/sysv/linux/x86_64/x32/gettimeofday.c (limited to 'sysdeps/unix') diff --git a/sysdeps/unix/clock_gettime.c b/sysdeps/unix/clock_gettime.c deleted file mode 100644 index aa74e11703..0000000000 --- a/sysdeps/unix/clock_gettime.c +++ /dev/null @@ -1,56 +0,0 @@ -/* clock_gettime -- Get the current time from a POSIX clockid_t. Unix version. - Copyright (C) 1999-2019 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include - -/* Get current value of CLOCK and store it in TP. */ -int -__clock_gettime (clockid_t clock_id, struct timespec *tp) -{ - int retval = -1; - - switch (clock_id) - { - case CLOCK_REALTIME: - { - struct timeval tv; - retval = __gettimeofday (&tv, NULL); - if (retval == 0) - TIMEVAL_TO_TIMESPEC (&tv, tp); - } - break; - - default: - __set_errno (EINVAL); - break; - } - - return retval; -} -libc_hidden_def (__clock_gettime) - -versioned_symbol (libc, __clock_gettime, clock_gettime, GLIBC_2_17); -/* clock_gettime moved to libc in version 2.17; - old binaries may expect the symbol version it had in librt. */ -#if SHLIB_COMPAT (libc, GLIBC_2_2, GLIBC_2_17) -strong_alias (__clock_gettime, __clock_gettime_2); -compat_symbol (libc, __clock_gettime_2, clock_gettime, GLIBC_2_2); -#endif diff --git a/sysdeps/unix/syscalls.list b/sysdeps/unix/syscalls.list index 5fedd5733d..e28e801c7a 100644 --- a/sysdeps/unix/syscalls.list +++ b/sysdeps/unix/syscalls.list @@ -33,7 +33,6 @@ getrlimit - getrlimit i:ip __getrlimit getrlimit getrusage - getrusage i:ip __getrusage getrusage getsockname - getsockname i:ibN __getsockname getsockname getsockopt - getsockopt i:iiiBN getsockopt -gettimeofday - gettimeofday i:pP __gettimeofday gettimeofday getuid - getuid Ei: __getuid getuid ioctl - ioctl i:iiI __ioctl ioctl kill - kill i:ii __kill kill diff --git a/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c b/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c index 4ff74fa285..075af3d0d3 100644 --- a/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c +++ b/sysdeps/unix/sysv/linux/aarch64/gettimeofday.c @@ -20,39 +20,39 @@ putting it into *tv and *tz. If tz is null, *tz is not filled. Returns 0 on success, -1 on errors. */ -#include +#include +#include -#ifdef SHARED - -# include -# include +#ifdef HAVE_GETTIMEOFDAY_VSYSCALL +# define HAVE_VSYSCALL +#endif +#include /* Used as a fallback in the ifunc resolver if VDSO is not available and for libc.so internal __gettimeofday calls. */ - static int __gettimeofday_vsyscall (struct timeval *tv, struct timezone *tz) { + if (__glibc_unlikely (tz != 0)) + memset (tz, 0, sizeof *tz); + return INLINE_VSYSCALL (gettimeofday, 2, tv, tz); } +#ifdef SHARED +# include +# include + # define INIT_ARCH() -libc_ifunc_hidden (__gettimeofday, __gettimeofday, - (get_vdso_symbol (HAVE_GETTIMEOFDAY_VSYSCALL) - ?: __gettimeofday_vsyscall)) -libc_hidden_def (__gettimeofday) +libc_ifunc (__gettimeofday, + (get_vdso_symbol (HAVE_GETTIMEOFDAY_VSYSCALL) + ?: __gettimeofday_vsyscall)) #else - -# include int __gettimeofday (struct timeval *tv, struct timezone *tz) { - return INLINE_SYSCALL (gettimeofday, 2, tv, tz); + return __gettimeofday_vsyscall (tv, tz); } -libc_hidden_def (__gettimeofday) - #endif - weak_alias (__gettimeofday, gettimeofday) -libc_hidden_weak (gettimeofday) diff --git a/sysdeps/unix/sysv/linux/alpha/gettimeofday.c b/sysdeps/unix/sysv/linux/alpha/gettimeofday.c new file mode 100644 index 0000000000..262a3c2352 --- /dev/null +++ b/sysdeps/unix/sysv/linux/alpha/gettimeofday.c @@ -0,0 +1,22 @@ +/* gettimeofday -- Get the current time of day. Linux/Alpha/tv64 version. + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* We can use the generic implementation, but we have to override its + default symbol version. */ +#define VERSION_gettimeofday GLIBC_2.1 +#include