Skip to contentSkip to conversation

Cobalt build and time64 fixes for uClibc-ng

PATCH New Yegor Yefremov 1 participant 6 messages Nov 30

lore
Share & tools
lore
#0 lore raw

v3 cover Cobalt build and time64 fixes for uClibc-ng

Cobalt has not built with uClibc-ng since v3.1.1. Patches 1 and 5 fix the build. Patches 2-4 fix the places that pick the time64 variant of a kernel interface by checking __TIMESIZE, which only glibc defines: the clock_gettime() vDSO call, the select() wrapper and the clocktest reference clock. On 32-bit uClibc-ng and musl with 64-bit time_t, these used the 32-bit interfaces with 64-bit structures.

The other __TIMESIZE users (the symbol redirections in include/cobalt/wrappers.h and include/cobalt/pthread.h, and the *_time64 variant of pthread_timedmutex_lock_interruptible_np() in lib/cobalt/wrappers_time64.c) are about glibc providing both 32-bit and *64 symbols, so they stay glibc-only.

Tested with Buildroot's test-pkg on next plus the series, building Cobalt with the testsuite, RTCAN, Analogy and the pSOS and VxWorks skins:
- armv5 uClibc-ng 1.0.59 (time64), GCC 15
- armv7 glibc 2.44, GCC 16
- aarch64 glibc 2.24, GCC 6

I also checked with a small test program that XN_TIME64_ON_32BIT is defined for armv5 uClibc-ng (time64), armv7 and i686 musl, and armv7 glibc with _TIME_BITS=64, and is not defined for armv7 glibc without it, aarch64 glibc and x86_64 musl.

musl still doesn't build, for the reasons listed in the v2 cover letter. Since e14e0e562 ("configure.ac: Fix typo") enables -O2 again, two more errors show up with musl: the __OPTIMIZE__ branch of include/cobalt/uapi/signal.h uses glibc's siginfo_t._sifields and sigevent_t, and GCC 15 reports '*val' may be used uninitialized in lib/analogy/math.c:166 (-Werror). Neither comes from this series.

Changes v2 -> v3:
- Added XN_TIME64_ON_32BIT and used it for the vDSO selection, the select() wrapper (new patch 3) and the clocktest (new patch 4) (Florian).
- No special case for x32, as Xenomai no longer supports it (Jan).
- Rebased on next.

Changes v1 -> v2:
- Dropped the getauxval() fallback for uClibc-ng < 1.0.43 (Jan).
- Checked the series with musl.

Yegor Yefremov (5):
lib/cobalt: Don't rely on glibc's __OPEN_NEEDS_MODE lib/cobalt: Select the time64 vDSO call without __TIMESIZE lib/cobalt: Use the time64 select() path without __TIMESIZE testsuite/clocktest: Read the reference clock with time64 without
__TIMESIZE
testsuite/smokey/y2038: Define ADJ_SETOFFSET if missing

 include/cobalt/wrappers.h                        | 14 ++++++++++++++
 lib/cobalt/arch/arm/include/asm/xenomai/time.h   |  4 +++-
 lib/cobalt/arch/arm64/include/asm/xenomai/time.h |  4 +++-
 lib/cobalt/arch/x86/include/asm/xenomai/time.h   |  4 +++-
 lib/cobalt/rtdm.c                                | 14 ++++++++++----
 lib/cobalt/wrappers_time64.c                     |  6 +++---
 testsuite/clocktest/clocktest.c                  |  2 +-
 testsuite/smokey/y2038/syscall-tests.c           |  5 +++++
 8 files changed, 42 insertions(+), 11 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v3 1/5 lib/cobalt: Don't rely on glibc's __OPEN_NEEDS_MODE

__OPEN_NEEDS_MODE() is an internal helper of glibc's <fcntl.h>, so libcobalt fails to build with other C libraries such as uClibc-ng and musl:

rtdm.c:94:13: error: implicit declaration of function '__OPEN_NEEDS_MODE'

Factor out the check open() and open64() already do into a local helper and use it in __open_2() and __open64_2() as well.

Fixes 89f00a2e5458 ("lib/cobalt: Wrap __open_2/__open64_2 to support _FORTIFY_SOURCE")
Assisted-by Claude:claude-opus-5-5
---
 lib/cobalt/rtdm.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
lib/cobalt/rtdm.c +10 -4
index df7aa7eb6..58d943804 100644
@@ -36,6 +36,12 @@
 #define O_TMPFILE (020000000 | 0200000)
 #endif
 
+/* like __OPEN_NEEDS_MODE(), which only glibc provides */
+static inline int open_needs_mode(int oflag)
+{
+	return (oflag & O_CREAT) || (oflag & O_TMPFILE) == O_TMPFILE;
+}
+
 static int do_open(const char *path, int oflag, mode_t mode)
 {
 	int fd, oldtype;
@@ -62,7 +68,7 @@ COBALT_IMPL(int, open, (const char *path, int oflag, ...))
 	mode_t mode = 0;
 	va_list ap;
 
-	if ((oflag & O_CREAT)  || (oflag & O_TMPFILE) == O_TMPFILE) {
+	if (open_needs_mode(oflag)) {
 		va_start(ap, oflag);
 		mode = va_arg(ap, int);
 		va_end(ap);
@@ -76,7 +82,7 @@ COBALT_IMPL(int, open64, (const char *path, int oflag, ...))
 	mode_t mode = 0;
 	va_list ap;
 
-	if ((oflag & O_CREAT)  || (oflag & O_TMPFILE) == O_TMPFILE) {
+	if (open_needs_mode(oflag)) {
 		va_start(ap, oflag);
 		mode = va_arg(ap, int);
 		va_end(ap);
@@ -91,7 +97,7 @@ COBALT_IMPL(int, __open_2, (const char *path, int oflag))
 	 * parameter to the functionality of open(). It may be used when the macro
 	 * _FORTIFY_SOURCE is defined when compiling the application code.
 	 */
-	if (__OPEN_NEEDS_MODE(oflag)) {
+	if (open_needs_mode(oflag)) {
 		const char *msg =
 			"invalid open call: O_CREAT or O_TMPFILE without mode\n";
 		ssize_t n = __STD(write(STDERR_FILENO, msg, strlen(msg)));
@@ -105,7 +111,7 @@ COBALT_IMPL(int, __open_2, (const char *path, int oflag))
 COBALT_IMPL(int, __open64_2, (const char *path, int oflag))
 {
 	/* just like __open_2() and open64() */
-	if (__OPEN_NEEDS_MODE(oflag)) {
+	if (open_needs_mode(oflag)) {
 		const char *msg =
 			"invalid open64 call: O_CREAT or O_TMPFILE without mode\n";
 		ssize_t n = __STD(write(STDERR_FILENO, msg, strlen(msg)));
Signature · 1 line
2.34.1
#2 lore raw

PATCH v3 2/5 lib/cobalt: Select the time64 vDSO call without __TIMESIZE

In reply to Yegor Yefremov

The clock_gettime() vcall is chosen by checking for __TIMESIZE == 32, which only glibc defines. uClibc-ng built with time64 support (__UCLIBC_USE_TIME64__, as e.g. Buildroot builds it) and musl >= 1.2 define __USE_TIME_BITS64 and use a 64-bit time_t on 32-bit targets, but have no __TIMESIZE. libcobalt then picks __vdso_clock_gettime, which fills in a 32-bit struct timespec: the seconds and nanoseconds both end up in the 64-bit tv_sec of the caller, and tv_nsec is left untouched.

Add XN_TIME64_ON_32BIT for the places that have to pick the time64 variant of a kernel interface, falling back to the size of long if __TIMESIZE is not defined, and use it for the vDSO selection. Nothing changes for glibc, and on 64-bit targets.

Fixes fb112c2fdc69 ("y2038: lib/cobalt: Switch vdso to gettime64")
Assisted-by Claude:claude-opus-5-5
---
 include/cobalt/wrappers.h                        | 14 ++++++++++++++
 lib/cobalt/arch/arm/include/asm/xenomai/time.h   |  4 +++-
 lib/cobalt/arch/arm64/include/asm/xenomai/time.h |  4 +++-
 lib/cobalt/arch/x86/include/asm/xenomai/time.h   |  4 +++-
 4 files changed, 23 insertions(+), 3 deletions(-)
include/cobalt/wrappers.h +14 -0
index 9dd022a75..4a7e075e9 100644
@@ -29,6 +29,20 @@
 	__typeof__(T) __STD(FN) I;                                             \
 	__typeof__(T) __WRAP(FN) I
 
+/*
+ * XN_TIME64_ON_32BIT is defined if time_t is 64-bit on a 32-bit target,
+ * i.e. the time64 variants of the kernel interfaces (syscalls, vDSO) have
+ * to be used. Only glibc defines __TIMESIZE, other C libraries (e.g.
+ * uClibc-ng and musl) fall back to the size of long.
+ */
+#ifdef __TIMESIZE
+#if __USE_TIME_BITS64 && __TIMESIZE == 32
+#define XN_TIME64_ON_32BIT
+#endif
+#elif __USE_TIME_BITS64 && __SIZEOF_LONG__ == 4
+#define XN_TIME64_ON_32BIT
+#endif
+
 #if __USE_TIME_BITS64 && __TIMESIZE == 32
 /*
  * Make __RT() and __STD() usable in combination with time64_t related services.
lib/cobalt/arch/arm/include/asm/xenomai/time.h +3 -1
index c260acf46..886afb957 100644
@@ -10,9 +10,11 @@
 #ifndef _LIB_COBALT_ARM_TIME_H
 #define _LIB_COBALT_ARM_TIME_H
 
+#include <time.h>
+
 #define COBALT_VDSO_VERSION	"LINUX_2.6"
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+#ifdef XN_TIME64_ON_32BIT
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime64"
 #else
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime"
lib/cobalt/arch/arm64/include/asm/xenomai/time.h +3 -1
index 06992e709..3ea0568eb 100644
@@ -10,9 +10,11 @@
 #ifndef _LIB_COBALT_ARM64_TIME_H
 #define _LIB_COBALT_ARM64_TIME_H
 
+#include <time.h>
+
 #define COBALT_VDSO_VERSION	"LINUX_2.6.39"
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+#ifdef XN_TIME64_ON_32BIT
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime64"
 #else
 #define COBALT_VDSO_GETTIME	"__kernel_clock_gettime"
lib/cobalt/arch/x86/include/asm/xenomai/time.h +3 -1
index 36e027329..538992d79 100644
@@ -10,9 +10,11 @@
 #ifndef _LIB_COBALT_X86_TIME_H
 #define _LIB_COBALT_X86_TIME_H
 
+#include <time.h>
+
 #define COBALT_VDSO_VERSION	"LINUX_2.6"
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+#ifdef XN_TIME64_ON_32BIT
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime64"
 #else
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime"
Signature · 1 line
2.34.1
#3 lore raw

PATCH v3 3/5 lib/cobalt: Use the time64 select() path without __TIMESIZE

In reply to Yegor Yefremov

On 32-bit uClibc-ng and musl with 64-bit time_t, the __TIMESIZE check fails, so select() passes the caller's 64-bit struct timeval to sc_cobalt_select, which expects the old 32-bit layout.

Use XN_TIME64_ON_32BIT instead, so that sc_cobalt_pselect64 is used. For the fallback, call __STD(select()): with glibc it is redirected to __real___select64 as before, while the other C libraries only have select(), which is 64-bit there.

Fixes c2b883446b80 ("y2038: lib/cobalt: Implement necessary wrappers for y2038 support")
Assisted-by Claude:claude-opus-5-5
---
 lib/cobalt/wrappers_time64.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
lib/cobalt/wrappers_time64.c +3 -3
index 024d22704..76a30f2ed 100644
@@ -768,7 +768,7 @@ COBALT_IMPL_TIME64(int, setsockopt, __setsockopt64,
 	return __STD(setsockopt(fd, level, optname, optval, optlen));
 }
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+#ifdef XN_TIME64_ON_32BIT
 
 #define USEC_PER_SEC	1000000L
 #define NSEC_PER_USEC	1000L
@@ -805,8 +805,8 @@ static inline int do_select(int __nfds, fd_set *__restrict __readfds,
 	pthread_setcanceltype(oldtype, NULL);
 
 	if (err == -EADV || err == -EPERM || err == -ENOSYS) {
-		err = __STD(__select64(__nfds, __readfds, __writefds,
-				       __exceptfds, __timeout));
+		err = __STD(select(__nfds, __readfds, __writefds,
+				   __exceptfds, __timeout));
 	} else if (__timeout) {
 		__timeout->tv_sec = to.tv_sec;
 		__timeout->tv_usec = to.tv_nsec / 1000;
Signature · 1 line
2.34.1
#4 lore raw

PATCH v3 4/5 testsuite/clocktest: Read the reference clock with time64 without __TIMESIZE

In reply to Yegor Yefremov

On 32-bit uClibc-ng and musl with 64-bit time_t, the __TIMESIZE check fails, so the reference clock is read with SYS_clock_gettime into a 64-bit struct timespec, which the kernel fills in with the old 32-bit layout.

Use XN_TIME64_ON_32BIT to pick SYS_clock_gettime64 instead.

Fixes 1e63f3ea4f0a ("y2038: testsuite/clocktest: Fix reading of reference clock")
Assisted-by Claude:claude-opus-5-5
---
 testsuite/clocktest/clocktest.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
testsuite/clocktest/clocktest.c +1 -1
index 283d33ccd..92adf4292 100644
@@ -86,7 +86,7 @@ static inline uint64_t read_reference_clock(void)
 	 * Make sure we do not pick the vsyscall variant. It won't
 	 * switch us into secondary mode and can easily deadlock.
 	 */
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+#ifdef XN_TIME64_ON_32BIT
 	syscall(SYS_clock_gettime64, CLOCK_REALTIME, &ts);
 #else
 	syscall(SYS_clock_gettime, CLOCK_REALTIME, &ts);
Signature · 1 line
2.34.1
#5 lore raw

PATCH v3 5/5 testsuite/smokey/y2038: Define ADJ_SETOFFSET if missing

In reply to Yegor Yefremov

The <sys/timex.h> of uClibc-ng and musl lacks ADJ_SETOFFSET, so the y2038 test fails to build:

syscall-tests.c:412:22: error: 'ADJ_SETOFFSET' undeclared (first use in this function)

Fall back to the value from the kernel UAPI, as the test already does for the time64 data types.

Fixes fa89ca44e33d ("y2038: testsuite/smokey/y2038: testcase for adjtime64")
Assisted-by Claude:claude-opus-5-5
---
 testsuite/smokey/y2038/syscall-tests.c | 5 +++++
 1 file changed, 5 insertions(+)
testsuite/smokey/y2038/syscall-tests.c +5 -0
index 4318712cc..27deb80e5 100644
@@ -17,6 +17,11 @@
 #include <sched.h>
 #include <rtdm/ipc.h>
 
+/* not provided by all C libraries, e.g. uClibc-ng and musl */
+#ifndef ADJ_SETOFFSET
+#define ADJ_SETOFFSET 0x0100
+#endif
+
 smokey_test_plugin(y2038, SMOKEY_NOARGS, "Validate correct y2038 support");
 
 /*
Signature · 1 line
2.34.1