Skip to contentSkip to conversation

Cobalt build fixes for uClibc-ng

PATCH Superseded Yegor Yefremov 3 participants 8 messages Nov 24 – Nov 25

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Cobalt build fixes for uClibc-ng

Cobalt has not built with uClibc-ng since v3.1.1. These patches fix the build, plus the clock_gettime() vDSO selection, which picks the 32-bit call for uClibc-ng with 64-bit time_t. They complement Jan's mmap() off_t fix for uClibc-ng >= 1.0.58, now in next.

Patch 2 is only needed for uClibc-ng releases before 1.0.43, which lack getauxval(). The Buildroot autobuilders still use such a toolchain (br-arm-full-static, uClibc-ng 1.0.36).

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

Without the series, both uClibc-ng builds fail on __OPEN_NEEDS_MODE, and the older one also fails on <sys/auxv.h>. The ADJ_SETOFFSET error only showed up once these were fixed.

For patch 3, I compared the vDSO symbol the preprocessor picks before and after: __vdso_clock_gettime64 instead of __vdso_clock_gettime on uClibc-ng time64, and no change for armv7 glibc with or without _TIME_BITS=64. I have not run it on uClibc-ng hardware.

musl is not covered. It defines neither __USE_TIME_BITS64 nor __TIMESIZE, so libcobalt would use the 32-bit time syscalls there as well. That needs a separate look.

Yegor Yefremov (4):
lib/cobalt: Don't rely on glibc's __OPEN_NEEDS_MODE lib/cobalt: Provide getauxval() fallback for the vDSO parser lib/cobalt: Select the time64 vDSO call without __TIMESIZE testsuite/smokey/y2038: Define ADJ_SETOFFSET if missing

 configure.ac                                  |  3 +-
 .../arch/arm/include/asm/xenomai/time.h       |  8 ++++-
 .../arch/arm64/include/asm/xenomai/time.h     |  8 ++++-
 .../arch/x86/include/asm/xenomai/time.h       |  8 ++++-
 lib/cobalt/parse_vdso.c                       | 31 ++++++++++++++++++-
 lib/cobalt/rtdm.c                             | 14 ++++++---
 testsuite/smokey/y2038/syscall-tests.c        |  5 +++
 7 files changed, 68 insertions(+), 9 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/4 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:

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 2/4 lib/cobalt: Provide getauxval() fallback for the vDSO parser

In reply to Yegor Yefremov

uClibc-ng only provides getauxval() and <sys/auxv.h> since 1.0.43, so libcobalt fails to build with older releases:

parse_vdso.c:18:10: fatal error: sys/auxv.h: No such file or directory

Check for getauxval() in configure and otherwise look up the auxiliary vector entry in /proc/self/auxv.

Fixes f8cd2923b0b0 ("lib/cobalt: dovetail: use clock_gettime() vcall for reading timestamps")
Assisted-by Claude:claude-opus-5-5
---
 configure.ac            |  3 ++-
 lib/cobalt/parse_vdso.c | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)
configure.ac +2 -1
index 0d6dd87b6..6e8d8d549 100644
@@ -572,7 +572,8 @@ AC_CHECK_FUNCS([pthread_mutexattr_setprotocol	\
 		clock_nanosleep			\
 		shm_open			\
 		shm_unlink			\
-		backtrace])
+		backtrace			\
+		getauxval])
 LIBS="$save_LIBS"
 
 save_CPPFLAGS="$CPPFLAGS"
lib/cobalt/parse_vdso.c +30 -1
index 37b997b78..f65af1191 100644
@@ -15,7 +15,6 @@
  * architecture that has a vDSO.
  */
 
-#include <sys/auxv.h>
 #include <stdbool.h>
 #include <string.h>
 #include <limits.h>
@@ -25,6 +24,36 @@
 #include <elf.h>
 #include "internal.h"
 
+#ifdef HAVE_GETAUXVAL
+#include <sys/auxv.h>
+#else
+#include <fcntl.h>
+#include <unistd.h>
+
+/* For C libraries without getauxval(), e.g. uClibc-ng before 1.0.43. */
+static unsigned long getauxval(unsigned long type)
+{
+	unsigned long entry[2], val = 0;
+	int fd;
+
+	fd = __STD(open("/proc/self/auxv", O_RDONLY));
+	if (fd < 0)
+		return 0;
+
+	while (__STD(read(fd, entry, sizeof(entry))) == sizeof(entry) &&
+	       entry[0] != AT_NULL) {
+		if (entry[0] == type) {
+			val = entry[1];
+			break;
+		}
+	}
+
+	__STD(close(fd));
+
+	return val;
+}
+#endif
+
 #ifndef ELF_BITS
 # if ULONG_MAX > 0xffffffffUL
 #  define ELF_BITS 64
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH 2/4] lib/cobalt: Provide getauxval() fallback for the vDSO parser

Quoted 1 line · Yegor Yefremov

uClibc-ng only provides getauxval() and <sys/auxv.h> since 1.0.43, so

Ok, I'm not against backward compatibility per se, but the build was broken for ages, and 1.0.43 was released in April 2023 - do we really need that anymore? I think the AI went too far here.

Jan

Quoted 78 lines

libcobalt fails to build with older releases:

parse_vdso.c:18:10: fatal error: sys/auxv.h: No such file or directory

Check for getauxval() in configure and otherwise look up the auxiliary vector entry in /proc/self/auxv.

Fixes f8cd2923b0b0 ("lib/cobalt: dovetail: use clock_gettime() vcall for reading timestamps")
Assisted-by Claude:claude-opus-5-5
---
 configure.ac            |  3 ++-
 lib/cobalt/parse_vdso.c | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 32 insertions(+), 2 deletions(-)
configure.ac +2 -1
index 0d6dd87b6..6e8d8d549 100644
@@ -572,7 +572,8 @@ AC_CHECK_FUNCS([pthread_mutexattr_setprotocol	\
 		clock_nanosleep			\
 		shm_open			\
 		shm_unlink			\
-		backtrace])
+		backtrace			\
+		getauxval])
 LIBS="$save_LIBS"
 
 save_CPPFLAGS="$CPPFLAGS"
lib/cobalt/parse_vdso.c +30 -1
index 37b997b78..f65af1191 100644
@@ -15,7 +15,6 @@
  * architecture that has a vDSO.
  */
 
-#include <sys/auxv.h>
 #include <stdbool.h>
 #include <string.h>
 #include <limits.h>
@@ -25,6 +24,36 @@
 #include <elf.h>
 #include "internal.h"
 
+#ifdef HAVE_GETAUXVAL
+#include <sys/auxv.h>
+#else
+#include <fcntl.h>
+#include <unistd.h>
+
+/* For C libraries without getauxval(), e.g. uClibc-ng before 1.0.43. */
+static unsigned long getauxval(unsigned long type)
+{
+	unsigned long entry[2], val = 0;
+	int fd;
+
+	fd = __STD(open("/proc/self/auxv", O_RDONLY));
+	if (fd < 0)
+		return 0;
+
+	while (__STD(read(fd, entry, sizeof(entry))) == sizeof(entry) &&
+	       entry[0] != AT_NULL) {
+		if (entry[0] == type) {
+			val = entry[1];
+			break;
+		}
+	}
+
+	__STD(close(fd));
+
+	return val;
+}
+#endif
+
 #ifndef ELF_BITS
 # if ULONG_MAX > 0xffffffffUL
 #  define ELF_BITS 64
Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
#4 lore raw

PATCH 3/4 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) defines __USE_TIME_BITS64 and uses a 64-bit time_t on 32-bit targets, but has 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.

Fall back to the size of long if __TIMESIZE is not defined. 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
---
 lib/cobalt/arch/arm/include/asm/xenomai/time.h   | 8 +++++++-
 lib/cobalt/arch/arm64/include/asm/xenomai/time.h | 8 +++++++-
 lib/cobalt/arch/x86/include/asm/xenomai/time.h   | 8 +++++++-
 3 files changed, 21 insertions(+), 3 deletions(-)
lib/cobalt/arch/arm/include/asm/xenomai/time.h +7 -1
index c260acf46..3bdd6fcd5 100644
@@ -12,7 +12,13 @@
 
 #define COBALT_VDSO_VERSION	"LINUX_2.6"
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+/*
+ * Use the time64 vcall whenever time_t is 64-bit on a 32-bit target. Not
+ * all C libraries define __TIMESIZE (e.g. uClibc-ng), so fall back to the
+ * word size.
+ */
+#if __USE_TIME_BITS64 &&						\
+	(__TIMESIZE == 32 || (!defined(__TIMESIZE) && __SIZEOF_LONG__ == 4))
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime64"
 #else
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime"
lib/cobalt/arch/arm64/include/asm/xenomai/time.h +7 -1
index 06992e709..7eeae991c 100644
@@ -12,7 +12,13 @@
 
 #define COBALT_VDSO_VERSION	"LINUX_2.6.39"
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+/*
+ * Use the time64 vcall whenever time_t is 64-bit on a 32-bit target. Not
+ * all C libraries define __TIMESIZE (e.g. uClibc-ng), so fall back to the
+ * word size.
+ */
+#if __USE_TIME_BITS64 &&						\
+	(__TIMESIZE == 32 || (!defined(__TIMESIZE) && __SIZEOF_LONG__ == 4))
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime64"
 #else
 #define COBALT_VDSO_GETTIME	"__kernel_clock_gettime"
lib/cobalt/arch/x86/include/asm/xenomai/time.h +7 -1
index 36e027329..4febcf45e 100644
@@ -12,7 +12,13 @@
 
 #define COBALT_VDSO_VERSION	"LINUX_2.6"
 
-#if __USE_TIME_BITS64 && __TIMESIZE == 32
+/*
+ * Use the time64 vcall whenever time_t is 64-bit on a 32-bit target. Not
+ * all C libraries define __TIMESIZE (e.g. uClibc-ng), so fall back to the
+ * word size.
+ */
+#if __USE_TIME_BITS64 &&						\
+	(__TIMESIZE == 32 || (!defined(__TIMESIZE) && __SIZEOF_LONG__ == 4))
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime64"
 #else
 #define COBALT_VDSO_GETTIME	"__vdso_clock_gettime"
Signature · 1 line
2.34.1
#5 lore raw

PATCH 4/4 testsuite/smokey/y2038: Define ADJ_SETOFFSET if missing

In reply to Yegor Yefremov

uClibc-ng's <sys/timex.h> 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..7c7035f15 100644
@@ -17,6 +17,11 @@
 #include <sched.h>
 #include <rtdm/ipc.h>
 
+/* not provided by all C libraries, e.g. uClibc-ng */
+#ifndef ADJ_SETOFFSET
+#define ADJ_SETOFFSET 0x0100
+#endif
+
 smokey_test_plugin(y2038, SMOKEY_NOARGS, "Validate correct y2038 support");
 
 /*
Signature · 1 line
2.34.1
#6 lore raw
In reply to Yegor Yefremov
Quoted 8 lines · Yegor Yefremov

Cobalt has not built with uClibc-ng since v3.1.1. These patches fix the build, plus the clock_gettime() vDSO selection, which picks the 32-bit call for uClibc-ng with 64-bit time_t. They complement Jan's mmap() off_t fix for uClibc-ng >= 1.0.58, now in next.

Patch 2 is only needed for uClibc-ng releases before 1.0.43, which lack getauxval(). The Buildroot autobuilders still use such a toolchain (br-arm-full-static, uClibc-ng 1.0.36).

See my comment on patch 2: This is too old to be still supported. If there are active ucLibc users (independent of Xenomai), there should be also recent versions supported.

Quoted 28 lines

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

Without the series, both uClibc-ng builds fail on __OPEN_NEEDS_MODE, and the older one also fails on <sys/auxv.h>. The ADJ_SETOFFSET error only showed up once these were fixed.

For patch 3, I compared the vDSO symbol the preprocessor picks before and after: __vdso_clock_gettime64 instead of __vdso_clock_gettime on uClibc-ng time64, and no change for armv7 glibc with or without _TIME_BITS=64. I have not run it on uClibc-ng hardware.

musl is not covered. It defines neither __USE_TIME_BITS64 nor __TIMESIZE, so libcobalt would use the 32-bit time syscalls there as well. That needs a separate look.

Yegor Yefremov (4):
lib/cobalt: Don't rely on glibc's __OPEN_NEEDS_MODE lib/cobalt: Provide getauxval() fallback for the vDSO parser lib/cobalt: Select the time64 vDSO call without __TIMESIZE testsuite/smokey/y2038: Define ADJ_SETOFFSET if missing

Quoted 9 lines
 configure.ac                                  |  3 +-
 .../arch/arm/include/asm/xenomai/time.h       |  8 ++++-
 .../arch/arm64/include/asm/xenomai/time.h     |  8 ++++-
 .../arch/x86/include/asm/xenomai/time.h       |  8 ++++-
 lib/cobalt/parse_vdso.c                       | 31 ++++++++++++++++++-
 lib/cobalt/rtdm.c                             | 14 ++++++---
 testsuite/smokey/y2038/syscall-tests.c        |  5 +++
 7 files changed, 68 insertions(+), 9 deletions(-)

While asking the AI for ucLibc fixes, could you please also let it cross-check if the fixes will work with musl as well? It should be able to do that without BR as environment (it did micro-builds for my other path on its own when given a cross-compiler).

Thanks,
Jan

Signature · 2 lines
Siemens AG, Foundational Technologies
Linux Expert Center
#7 lore raw
Quoted 52 lines · Jan Kiszka
Quoted 8 lines · Yegor Yefremov

Cobalt has not built with uClibc-ng since v3.1.1. These patches fix the build, plus the clock_gettime() vDSO selection, which picks the 32-bit call for uClibc-ng with 64-bit time_t. They complement Jan's mmap() off_t fix for uClibc-ng >= 1.0.58, now in next.

Patch 2 is only needed for uClibc-ng releases before 1.0.43, which lack getauxval(). The Buildroot autobuilders still use such a toolchain (br-arm-full-static, uClibc-ng 1.0.36).

See my comment on patch 2: This is too old to be still supported. If there are active ucLibc users (independent of Xenomai), there should be also recent versions supported.

Quoted 28 lines

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

Without the series, both uClibc-ng builds fail on __OPEN_NEEDS_MODE, and the older one also fails on <sys/auxv.h>. The ADJ_SETOFFSET error only showed up once these were fixed.

For patch 3, I compared the vDSO symbol the preprocessor picks before and after: __vdso_clock_gettime64 instead of __vdso_clock_gettime on uClibc-ng time64, and no change for armv7 glibc with or without _TIME_BITS=64. I have not run it on uClibc-ng hardware.

musl is not covered. It defines neither __USE_TIME_BITS64 nor __TIMESIZE, so libcobalt would use the 32-bit time syscalls there as well. That needs a separate look.

Yegor Yefremov (4):
lib/cobalt: Don't rely on glibc's __OPEN_NEEDS_MODE lib/cobalt: Provide getauxval() fallback for the vDSO parser lib/cobalt: Select the time64 vDSO call without __TIMESIZE testsuite/smokey/y2038: Define ADJ_SETOFFSET if missing

Quoted 9 lines
 configure.ac                                  |  3 +-
 .../arch/arm/include/asm/xenomai/time.h       |  8 ++++-
 .../arch/arm64/include/asm/xenomai/time.h     |  8 ++++-
 .../arch/x86/include/asm/xenomai/time.h       |  8 ++++-
 lib/cobalt/parse_vdso.c                       | 31 ++++++++++++++++++-
 lib/cobalt/rtdm.c                             | 14 ++++++---
 testsuite/smokey/y2038/syscall-tests.c        |  5 +++
 7 files changed, 68 insertions(+), 9 deletions(-)
Quoted 5 lines

While asking the AI for ucLibc fixes, could you please also let it cross-check if the fixes will work with musl as well? It should be able to do that without BR as environment (it did micro-builds for my other path on its own when given a cross-compiler).

I did some "tests" with musl at the time working on time64_t support. If I remember correctly - happy for a cross check again - the build was broken for that libc since a long time.