* [PATCH 0/4] tools/nolibc: add err.h
@ 2026-03-11 22:00 Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 1/4] selftests/nolibc: add a variable for nolibc-test source files Thomas Weißschuh
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-03-11 22:00 UTC (permalink / raw)
To: Willy Tarreau, Shuah Khan
Cc: linux-kselftest, linux-kernel, Thomas Weißschuh
Add a few convenient helpers to print error and warning messages.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
Thomas Weißschuh (4):
selftests/nolibc: add a variable for nolibc-test source files
selftests/nolibc: validate NOLIBC_IGNORE_ERRNO compilation
tools/nolibc: add support for program_invocation_{,short_}name
tools/nolibc: add err.h
tools/include/nolibc/Makefile | 1 +
tools/include/nolibc/crt.h | 26 +++++++
tools/include/nolibc/err.h | 91 ++++++++++++++++++++++
tools/include/nolibc/errno.h | 2 +
tools/include/nolibc/nolibc.h | 1 +
tools/testing/selftests/nolibc/Makefile | 4 +-
tools/testing/selftests/nolibc/Makefile.include | 2 +
tools/testing/selftests/nolibc/Makefile.nolibc | 8 +-
.../selftests/nolibc/nolibc-test-ignore-errno.c | 6 ++
tools/testing/selftests/nolibc/nolibc-test.c | 2 +
10 files changed, 137 insertions(+), 6 deletions(-)
---
base-commit: db4f34cc063d68f329fc543a5808fe53e61fc796
change-id: 20260311-nolibc-err-h-df36a75dddf8
Best regards,
--
Thomas Weißschuh <linux@weissschuh.net>
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH 1/4] selftests/nolibc: add a variable for nolibc-test source files
2026-03-11 22:00 [PATCH 0/4] tools/nolibc: add err.h Thomas Weißschuh
@ 2026-03-11 22:00 ` Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 2/4] selftests/nolibc: validate NOLIBC_IGNORE_ERRNO compilation Thomas Weißschuh
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-03-11 22:00 UTC (permalink / raw)
To: Willy Tarreau, Shuah Khan
Cc: linux-kselftest, linux-kernel, Thomas Weißschuh
The list of the nolibc-test source files is repeated many times.
Another source file is about to be added, adding to the mess.
Introduce a common variable instead.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/testing/selftests/nolibc/Makefile | 4 ++--
tools/testing/selftests/nolibc/Makefile.include | 2 ++
tools/testing/selftests/nolibc/Makefile.nolibc | 8 ++++----
3 files changed, 8 insertions(+), 6 deletions(-)
diff --git a/tools/testing/selftests/nolibc/Makefile b/tools/testing/selftests/nolibc/Makefile
index 0370489d938b..6bace04227a7 100644
--- a/tools/testing/selftests/nolibc/Makefile
+++ b/tools/testing/selftests/nolibc/Makefile
@@ -13,9 +13,9 @@ $(OUTPUT)/nolibc-test: CFLAGS = -nostdlib -nostdinc -static \
-isystem $(top_srcdir)/tools/include/nolibc -isystem $(top_srcdir)/usr/include \
$(CFLAGS_NOLIBC_TEST)
$(OUTPUT)/nolibc-test: LDLIBS = $(if $(LLVM),,-lgcc)
-$(OUTPUT)/nolibc-test: nolibc-test.c nolibc-test-linkage.c | headers
+$(OUTPUT)/nolibc-test: $(NOLIBC_TEST_SOURCES) | headers
-$(OUTPUT)/libc-test: nolibc-test.c nolibc-test-linkage.c
+$(OUTPUT)/libc-test: $(NOLIBC_TEST_SOURCES)
$(call msg,CC,,$@)
$(Q)$(LINK.c) $^ -o $@
diff --git a/tools/testing/selftests/nolibc/Makefile.include b/tools/testing/selftests/nolibc/Makefile.include
index 66287fafbbe0..6455373d56be 100644
--- a/tools/testing/selftests/nolibc/Makefile.include
+++ b/tools/testing/selftests/nolibc/Makefile.include
@@ -8,3 +8,5 @@ _CFLAGS_SANITIZER ?= $(call cc-option,-fsanitize=undefined -fsanitize-trap=all)
CFLAGS_NOLIBC_TEST ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -W -Wall -Wextra \
$(call cc-option,-fno-stack-protector) $(call cc-option,-Wmissing-prototypes) \
$(_CFLAGS_STACKPROTECTOR) $(_CFLAGS_SANITIZER)
+
+NOLIBC_TEST_SOURCES := nolibc-test.c nolibc-test-linkage.c
diff --git a/tools/testing/selftests/nolibc/Makefile.nolibc b/tools/testing/selftests/nolibc/Makefile.nolibc
index f5704193038f..2d32cec1105f 100644
--- a/tools/testing/selftests/nolibc/Makefile.nolibc
+++ b/tools/testing/selftests/nolibc/Makefile.nolibc
@@ -302,12 +302,12 @@ sysroot/$(ARCH)/include:
$(Q)$(MAKE) -C $(srctree)/tools/include/nolibc ARCH=$(ARCH) OUTPUT=$(CURDIR)/sysroot/ headers_standalone headers_check
$(Q)mv sysroot/sysroot sysroot/$(ARCH)
-nolibc-test: nolibc-test.c nolibc-test-linkage.c sysroot/$(ARCH)/include
+nolibc-test: $(NOLIBC_TEST_SOURCES) sysroot/$(ARCH)/include
$(QUIET_CC)$(CC) $(CFLAGS) $(LDFLAGS) -o $@ \
- -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include nolibc-test.c nolibc-test-linkage.c $(LIBGCC)
+ -nostdlib -nostdinc -static -Isysroot/$(ARCH)/include $(NOLIBC_TEST_SOURCES) $(LIBGCC)
-libc-test: nolibc-test.c nolibc-test-linkage.c
- $(QUIET_CC)$(HOSTCC) -o $@ nolibc-test.c nolibc-test-linkage.c
+libc-test: $(NOLIBC_TEST_SOURCES)
+ $(QUIET_CC)$(HOSTCC) -o $@ $(NOLIBC_TEST_SOURCES)
# local libc-test
run-libc-test: libc-test
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 2/4] selftests/nolibc: validate NOLIBC_IGNORE_ERRNO compilation
2026-03-11 22:00 [PATCH 0/4] tools/nolibc: add err.h Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 1/4] selftests/nolibc: add a variable for nolibc-test source files Thomas Weißschuh
@ 2026-03-11 22:00 ` Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 3/4] tools/nolibc: add support for program_invocation_{,short_}name Thomas Weißschuh
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-03-11 22:00 UTC (permalink / raw)
To: Willy Tarreau, Shuah Khan
Cc: linux-kselftest, linux-kernel, Thomas Weißschuh
When NOLIBC_IGNORE_ERRNO is set, various bits of nolibc are disabled.
Make sure that all the ifdeffery does not result in any compilation
errors by compiling a dummy source file.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/testing/selftests/nolibc/Makefile.include | 2 +-
tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c | 6 ++++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/tools/testing/selftests/nolibc/Makefile.include b/tools/testing/selftests/nolibc/Makefile.include
index 6455373d56be..41a0039f774c 100644
--- a/tools/testing/selftests/nolibc/Makefile.include
+++ b/tools/testing/selftests/nolibc/Makefile.include
@@ -9,4 +9,4 @@ CFLAGS_NOLIBC_TEST ?= -Os -fno-ident -fno-asynchronous-unwind-tables -std=c89 -
$(call cc-option,-fno-stack-protector) $(call cc-option,-Wmissing-prototypes) \
$(_CFLAGS_STACKPROTECTOR) $(_CFLAGS_SANITIZER)
-NOLIBC_TEST_SOURCES := nolibc-test.c nolibc-test-linkage.c
+NOLIBC_TEST_SOURCES := nolibc-test.c nolibc-test-linkage.c nolibc-test-ignore-errno.c
diff --git a/tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c b/tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c
new file mode 100644
index 000000000000..aea816da4fca
--- /dev/null
+++ b/tools/testing/selftests/nolibc/nolibc-test-ignore-errno.c
@@ -0,0 +1,6 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+
+#define NOLIBC_IGNORE_ERRNO
+
+/* Include all of nolibc and make sure everything compiles */
+#include <stdlib.h>
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 3/4] tools/nolibc: add support for program_invocation_{,short_}name
2026-03-11 22:00 [PATCH 0/4] tools/nolibc: add err.h Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 1/4] selftests/nolibc: add a variable for nolibc-test source files Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 2/4] selftests/nolibc: validate NOLIBC_IGNORE_ERRNO compilation Thomas Weißschuh
@ 2026-03-11 22:00 ` Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 4/4] tools/nolibc: add err.h Thomas Weißschuh
2026-03-12 3:15 ` [PATCH 0/4] " Willy Tarreau
4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-03-11 22:00 UTC (permalink / raw)
To: Willy Tarreau, Shuah Khan
Cc: linux-kselftest, linux-kernel, Thomas Weißschuh
Add support for the GNU extensions 'program_invocation_name' and
'program_invocation_short_name'. These are useful to print error
messages, which by convention include the program name.
As these are global variables which take up memory even if not used,
similar to 'errno', gate them behind NOLIBC_IGNORE_ERRNO.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/include/nolibc/crt.h | 26 ++++++++++++++++++++++++++
tools/include/nolibc/errno.h | 2 ++
tools/testing/selftests/nolibc/nolibc-test.c | 2 ++
3 files changed, 30 insertions(+)
diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h
index d9262998dae9..842f86e41f2f 100644
--- a/tools/include/nolibc/crt.h
+++ b/tools/include/nolibc/crt.h
@@ -17,6 +17,7 @@ const unsigned long *_auxv __attribute__((weak));
void _start(void);
static void __stack_chk_init(void);
static void exit(int);
+static char *strrchr(const char *s, int c);
extern void (*const __preinit_array_start[])(int, char **, char**) __attribute__((weak));
extern void (*const __preinit_array_end[])(int, char **, char**) __attribute__((weak));
@@ -27,6 +28,24 @@ extern void (*const __init_array_end[])(int, char **, char**) __attribute__((wea
extern void (*const __fini_array_start[])(void) __attribute__((weak));
extern void (*const __fini_array_end[])(void) __attribute__((weak));
+extern char *program_invocation_name __attribute__((weak));
+extern char *program_invocation_short_name __attribute__((weak));
+
+static __inline__
+char *__nolibc_program_invocation_short_name(char *long_name)
+{
+ char *short_name;
+
+ if (!long_name)
+ return NULL;
+
+ short_name = strrchr(long_name, '/');
+ if (!short_name || !short_name[0])
+ return NULL;
+
+ return short_name + 1;
+}
+
void _start_c(long *sp);
__attribute__((weak,used))
#if __nolibc_has_feature(undefined_behavior_sanitizer)
@@ -76,6 +95,13 @@ void _start_c(long *sp)
;
_auxv = auxv;
+#ifndef NOLIBC_IGNORE_ERRNO
+ if (argc > 0) {
+ program_invocation_name = argv[0];
+ program_invocation_short_name = __nolibc_program_invocation_short_name(argv[0]);
+ }
+#endif /* NOLIBC_IGNORE_ERRNO */
+
for (ctor_func = __preinit_array_start; ctor_func < __preinit_array_end; ctor_func++)
(*ctor_func)(argc, argv, envp);
for (ctor_func = __init_array_start; ctor_func < __init_array_end; ctor_func++)
diff --git a/tools/include/nolibc/errno.h b/tools/include/nolibc/errno.h
index 08a33c40ec0c..64d9f9bc6539 100644
--- a/tools/include/nolibc/errno.h
+++ b/tools/include/nolibc/errno.h
@@ -15,6 +15,8 @@
#ifndef NOLIBC_IGNORE_ERRNO
#define SET_ERRNO(v) do { errno = (v); } while (0)
int errno __attribute__((weak));
+char *program_invocation_name __attribute__((weak));
+char *program_invocation_short_name __attribute__((weak));
#else
#define SET_ERRNO(v) do { } while (0)
#endif
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index a140c54e4d72..5565ada679cc 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -754,6 +754,8 @@ int run_startup(int min, int max)
CASE_TEST(constructor); EXPECT_EQ(is_nolibc, constructor_test_value, 0x3); break;
CASE_TEST(linkage_errno); EXPECT_PTREQ(1, linkage_test_errno_addr(), &errno); break;
CASE_TEST(linkage_constr); EXPECT_EQ(1, linkage_test_constructor_test_value, 0x3); break;
+ CASE_TEST(prog_name_nolibc); EXPECT_STREQ(is_nolibc, program_invocation_short_name, "nolibc-test"); break;
+ CASE_TEST(prog_name_libc); EXPECT_STREQ(!is_nolibc, program_invocation_short_name, "libc-test"); break;
case __LINE__:
return ret; /* must be last */
/* note: do not set any defaults so as to permit holes above */
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH 4/4] tools/nolibc: add err.h
2026-03-11 22:00 [PATCH 0/4] tools/nolibc: add err.h Thomas Weißschuh
` (2 preceding siblings ...)
2026-03-11 22:00 ` [PATCH 3/4] tools/nolibc: add support for program_invocation_{,short_}name Thomas Weißschuh
@ 2026-03-11 22:00 ` Thomas Weißschuh
2026-03-12 3:15 ` [PATCH 0/4] " Willy Tarreau
4 siblings, 0 replies; 6+ messages in thread
From: Thomas Weißschuh @ 2026-03-11 22:00 UTC (permalink / raw)
To: Willy Tarreau, Shuah Khan
Cc: linux-kselftest, linux-kernel, Thomas Weißschuh
Add a few convenient helpers to print error and warning messages.
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
---
tools/include/nolibc/Makefile | 1 +
tools/include/nolibc/err.h | 91 +++++++++++++++++++++++++++++++++++++++++++
tools/include/nolibc/nolibc.h | 1 +
3 files changed, 93 insertions(+)
diff --git a/tools/include/nolibc/Makefile b/tools/include/nolibc/Makefile
index 1958dda98895..5d242312943f 100644
--- a/tools/include/nolibc/Makefile
+++ b/tools/include/nolibc/Makefile
@@ -30,6 +30,7 @@ all_files := \
ctype.h \
dirent.h \
elf.h \
+ err.h \
errno.h \
fcntl.h \
getopt.h \
diff --git a/tools/include/nolibc/err.h b/tools/include/nolibc/err.h
new file mode 100644
index 000000000000..2373c4999b71
--- /dev/null
+++ b/tools/include/nolibc/err.h
@@ -0,0 +1,91 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * formatted error message for NOLIBC
+ * Copyright (C) 2026 Thomas Weißschuh <linux@weissschuh.net>
+ */
+
+/* make sure to include all global symbols */
+#include "nolibc.h"
+
+#ifndef _NOLIBC_ERR_H
+#define _NOLIBC_ERR_H
+
+#include "errno.h"
+#include "stdarg.h"
+#include "sys.h"
+
+static __attribute__((unused))
+void vwarn(const char *fmt, va_list args)
+{
+#ifndef NOLIBC_IGNORE_ERRNO
+ fprintf(stderr, "%s: ", program_invocation_short_name);
+#endif
+ vfprintf(stderr, fmt, args);
+ fprintf(stderr, ": %m\n");
+}
+
+static __attribute__((unused))
+void vwarnx(const char *fmt, va_list args)
+{
+#ifndef NOLIBC_IGNORE_ERRNO
+ fprintf(stderr, "%s: ", program_invocation_short_name);
+#endif
+ vfprintf(stderr, fmt, args);
+ fprintf(stderr, "\n");
+}
+
+static __attribute__((unused))
+void warn(const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ vwarn(fmt, args);
+ va_end(args);
+}
+
+static __attribute__((unused))
+void warnx(const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ vwarnx(fmt, args);
+ va_end(args);
+}
+
+static __attribute__((noreturn, unused))
+void verr(int eval, const char *fmt, va_list args)
+{
+ vwarn(fmt, args);
+ exit(eval);
+}
+
+static __attribute__((noreturn, unused))
+void verrx(int eval, const char *fmt, va_list args)
+{
+ warnx(fmt, args);
+ exit(eval);
+}
+
+static __attribute__((noreturn, unused))
+void err(int eval, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ verr(eval, fmt, args);
+ va_end(args);
+}
+
+static __attribute__((noreturn, unused))
+void errx(int eval, const char *fmt, ...)
+{
+ va_list args;
+
+ va_start(args, fmt);
+ verrx(eval, fmt, args);
+ va_end(args);
+}
+
+#endif /* _NOLIBC_ERR_H */
diff --git a/tools/include/nolibc/nolibc.h b/tools/include/nolibc/nolibc.h
index d1d08b7f8599..fe8195483b6d 100644
--- a/tools/include/nolibc/nolibc.h
+++ b/tools/include/nolibc/nolibc.h
@@ -130,6 +130,7 @@
#include "getopt.h"
#include "poll.h"
#include "math.h"
+#include "err.h"
/* Used by programs to avoid std includes */
#define NOLIBC
--
2.53.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH 0/4] tools/nolibc: add err.h
2026-03-11 22:00 [PATCH 0/4] tools/nolibc: add err.h Thomas Weißschuh
` (3 preceding siblings ...)
2026-03-11 22:00 ` [PATCH 4/4] tools/nolibc: add err.h Thomas Weißschuh
@ 2026-03-12 3:15 ` Willy Tarreau
4 siblings, 0 replies; 6+ messages in thread
From: Willy Tarreau @ 2026-03-12 3:15 UTC (permalink / raw)
To: Thomas Weißschuh; +Cc: Shuah Khan, linux-kselftest, linux-kernel
Hi Thomas!
On Wed, Mar 11, 2026 at 11:00:48PM +0100, Thomas Weißschuh wrote:
> Add a few convenient helpers to print error and warning messages.
Nice! I had never heard about these variables but I agree these are
convenient and do simplify error messages production.
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
For the whole series:
Acked-by: Willy Tarreau <w@1wt.eu>
thanks,
Willy
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-03-12 3:15 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-11 22:00 [PATCH 0/4] tools/nolibc: add err.h Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 1/4] selftests/nolibc: add a variable for nolibc-test source files Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 2/4] selftests/nolibc: validate NOLIBC_IGNORE_ERRNO compilation Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 3/4] tools/nolibc: add support for program_invocation_{,short_}name Thomas Weißschuh
2026-03-11 22:00 ` [PATCH 4/4] tools/nolibc: add err.h Thomas Weißschuh
2026-03-12 3:15 ` [PATCH 0/4] " Willy Tarreau
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox