From nobody Mon Apr 6 17:29:05 2026 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 470C421ADC7; Wed, 18 Mar 2026 15:50:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773849032; cv=none; b=UNZjjM01HvaefOLU7zRX8QHnD7aPgyBhDuSXPaTBKvrewZPTCHHwdHZj8bgkzAkEYMO5/ypdR7UxbipCtYrSlPKLcKqMvyd1Eyq+VZoXdvu6f73ZDxVkhJPsnbuxVGLoJRt845qvRA4xelfFbyynt2jQKfAgi+XTQDOCAHprwnI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773849032; c=relaxed/simple; bh=CrltttxUhqp5Vd18DjWh/G2vFxyiJizaO2/2C6z0s1I=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=Kf/hRKwWkNb1Pp6F4KX666y72ffIPXNS8D5E/a5peK4nXOFYElWdwJuxCvkLfzFKxlAr9snN5vouqIXEYCumL19gLei0P86100SiqAWKzfRnb4anR1yUo/yAxi7ZcYF8LbXeu/2vwEcsjr0khClPUlg0IkaygxgEm7i3+njbRD4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=ZdUtQ81R; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="ZdUtQ81R" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1773849020; bh=CrltttxUhqp5Vd18DjWh/G2vFxyiJizaO2/2C6z0s1I=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=ZdUtQ81RWqZBUEvU0CdmQRaWpt1q7ieeRyR05NlaaPCKUPMrDDZMTlTXoQdyux+5Y 7hSp8QQURjEJ5PkeGaZU8mtFcysLO23wpGm+OAIG7p18Eo4xPujFrpjOlgpe740Xl0 +YrAeYBYn5GIfnhT6gxbJ3Gvy715FussGiB1eX+0= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Wed, 18 Mar 2026 16:50:16 +0100 Subject: [PATCH v4 1/2] tools/nolibc: add support for program_invocation_{,short_}name Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260318-nolibc-err-h-v4-1-08247a694bd9@weissschuh.net> References: <20260318-nolibc-err-h-v4-0-08247a694bd9@weissschuh.net> In-Reply-To: <20260318-nolibc-err-h-v4-0-08247a694bd9@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1773849019; l=5074; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=CrltttxUhqp5Vd18DjWh/G2vFxyiJizaO2/2C6z0s1I=; b=sxeXtpDSphiP/ja4oc9JlwX4imD1l+tYlXxoLUoTmhDbsqQLohWxByGjlxAHLjatkTr73Bp9S 0yG2BeZnxzYCe51jf7uboBC27KjH9hemnzlfrHqiwE2tm6R/AJ5bhhP X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= 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=C3=9Fschuh Acked-by: Willy Tarreau --- tools/include/nolibc/crt.h | 26 +++++++++++++++++++++ tools/include/nolibc/errno.h | 4 ++++ tools/testing/selftests/nolibc/nolibc-test.c | 34 ++++++++++++++++++++++++= ++++ 3 files changed, 64 insertions(+) diff --git a/tools/include/nolibc/crt.h b/tools/include/nolibc/crt.h index d9262998dae9..5bb492555f13 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); =20 extern void (*const __preinit_array_start[])(int, char **, char**) __attri= bute__((weak)); extern void (*const __preinit_array_end[])(int, char **, char**) __attribu= te__((weak)); @@ -27,6 +28,24 @@ extern void (*const __init_array_end[])(int, char **, ch= ar**) __attribute__((wea extern void (*const __fini_array_start[])(void) __attribute__((weak)); extern void (*const __fini_array_end[])(void) __attribute__((weak)); =20 +#ifndef NOLIBC_IGNORE_ERRNO +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; + + short_name =3D strrchr(long_name, '/'); + if (!short_name || !short_name[0]) + return long_name; + + return short_name + 1; +} +#endif /* NOLIBC_IGNORE_ERRNO */ + 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 =3D auxv; =20 +#ifndef NOLIBC_IGNORE_ERRNO + if (argc > 0 && argv[0]) { + program_invocation_name =3D argv[0]; + program_invocation_short_name =3D __nolibc_program_invocation_short_name= (argv[0]); + } +#endif /* NOLIBC_IGNORE_ERRNO */ + for (ctor_func =3D __preinit_array_start; ctor_func < __preinit_array_end= ; ctor_func++) (*ctor_func)(argc, argv, envp); for (ctor_func =3D __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..bab83692ea1c 100644 --- a/tools/include/nolibc/errno.h +++ b/tools/include/nolibc/errno.h @@ -15,8 +15,12 @@ #ifndef NOLIBC_IGNORE_ERRNO #define SET_ERRNO(v) do { errno =3D (v); } while (0) int errno __attribute__((weak)); +char *program_invocation_name __attribute__((weak)) =3D ""; +char *program_invocation_short_name __attribute__((weak)) =3D ""; #else #define SET_ERRNO(v) do { } while (0) +#define program_invocation_name "" +#define program_invocation_short_name "" #endif =20 =20 diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/s= elftests/nolibc/nolibc-test.c index 180611aabbfb..51da2f9e281b 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -42,6 +42,7 @@ #include #include #include +#include =20 #pragma GCC diagnostic ignored "-Wmissing-prototypes" =20 @@ -702,6 +703,37 @@ static void constructor2(int argc, char **argv, char *= *envp) constructor_test_value |=3D 1 << 1; } =20 +int test_program_invocation_name(void) +{ + char buf[100]; + char *dirsep; + ssize_t r; + int fd; + + fd =3D open("/proc/self/cmdline", O_RDONLY); + if (fd =3D=3D -1) + return 1; + + r =3D read(fd, buf, sizeof(buf)); + close(fd); + if (r < 1 || r =3D=3D sizeof(buf)) + return 1; + + buf[r - 1] =3D '\0'; + + if (strcmp(program_invocation_name, buf) !=3D 0) + return 1; + + dirsep =3D strrchr(buf, '/'); + if (!dirsep || dirsep[1] =3D=3D '\0') + return 1; + + if (strcmp(program_invocation_short_name, dirsep + 1) !=3D 0) + return 1; + + return 0; +} + int run_startup(int min, int max) { int test; @@ -716,6 +748,7 @@ int run_startup(int min, int max) #ifdef NOLIBC test_auxv =3D _auxv; #endif + bool proc =3D access("/proc", R_OK) =3D=3D 0; =20 for (test =3D min; test >=3D 0 && test <=3D max; test++) { int llen =3D 0; /* line length */ @@ -741,6 +774,7 @@ 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); EXPECT_ZR(proc, test_program_invocation_nam= e()); break; case __LINE__: return ret; /* must be last */ /* note: do not set any defaults so as to permit holes above */ --=20 2.53.0 From nobody Mon Apr 6 17:29:05 2026 Received: from todd.t-8ch.de (todd.t-8ch.de [159.69.126.157]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2E5923C3BEE; Wed, 18 Mar 2026 15:50:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=159.69.126.157 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773849032; cv=none; b=qE+25+kCPzzYFvCfHA5p543I0pYE8B5LMyu9SjC035Mll9m73mkFEQrf0gdQrsreV/gJbxpq4hgC7IuBF+E8eGDxQk/DdOCKUN5RWpVDurhFBLwOPsTSoswPrJkrUP5DD5JYmHMEoHIpGIFYZw1HOIiKjqA+WPeJ24zyTjuh6AI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773849032; c=relaxed/simple; bh=ppeFS/esmOXFVznMHGq33Ol4W76goRqIc1twAuAacu4=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=jX3B5X1sTtdpBeSnfkq62z689kJtrSR2Esr72dfa0Lt1Pnk1cCWdPnN3ec288ZIfGcoABzYJC7FnBwzbgGL7HM5UneVde7IvwfpxcAW5M++O4K/0FxrH+SGI7wnYg9yus4rieBOoaFaNFoOk40Po4jnc9Xg5zW82FpEPKO6aU3M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net; spf=pass smtp.mailfrom=weissschuh.net; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b=N1GbyH6f; arc=none smtp.client-ip=159.69.126.157 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=weissschuh.net Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=weissschuh.net header.i=@weissschuh.net header.b="N1GbyH6f" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=weissschuh.net; s=mail; t=1773849020; bh=ppeFS/esmOXFVznMHGq33Ol4W76goRqIc1twAuAacu4=; h=From:Date:Subject:References:In-Reply-To:To:Cc:From; b=N1GbyH6fU2l3ExgyNVmsaawi4XH6Byr/2/As6z6P1npZ5a8al71UOrI3wFo/g97gO 64QHDz+6gM1OGQXin+0A2eSYy5voGoycIGbyFGlLuVjGCt/8Po8miQp0ysrgdjbo7r F7KTjnCtd2ey128HRa+85oMna4NhLrKvtRAT4XLw= From: =?utf-8?q?Thomas_Wei=C3=9Fschuh?= Date: Wed, 18 Mar 2026 16:50:17 +0100 Subject: [PATCH v4 2/2] tools/nolibc: add err.h Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20260318-nolibc-err-h-v4-2-08247a694bd9@weissschuh.net> References: <20260318-nolibc-err-h-v4-0-08247a694bd9@weissschuh.net> In-Reply-To: <20260318-nolibc-err-h-v4-0-08247a694bd9@weissschuh.net> To: Willy Tarreau , Shuah Khan Cc: linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org, =?utf-8?q?Thomas_Wei=C3=9Fschuh?= X-Mailer: b4 0.14.3 X-Developer-Signature: v=1; a=ed25519-sha256; t=1773849019; l=2997; i=linux@weissschuh.net; s=20221212; h=from:subject:message-id; bh=ppeFS/esmOXFVznMHGq33Ol4W76goRqIc1twAuAacu4=; b=ODSuRwrWWSg9WC0ZWpzkfmFKGrEPjnKCWj6qkEk4yc0mNiIlt0pv5u3pMYXScxI3ROYgvSFXU 2m5JiHuX4F0A3TaYt45X1irfLnvsPOdYa5K2VqNrnFItkGuJrYbVexU X-Developer-Key: i=linux@weissschuh.net; a=ed25519; pk=KcycQgFPX2wGR5azS7RhpBqedglOZVgRPfdFSPB1LNw= Add a few convenient helpers to print error and warning messages. Signed-off-by: Thomas Wei=C3=9Fschuh Acked-by: Willy Tarreau --- tools/include/nolibc/Makefile | 1 + tools/include/nolibc/err.h | 87 +++++++++++++++++++++++++++++++++++++++= ++++ tools/include/nolibc/nolibc.h | 1 + 3 files changed, 89 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 :=3D \ 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..e22ae87a7289 --- /dev/null +++ b/tools/include/nolibc/err.h @@ -0,0 +1,87 @@ +/* SPDX-License-Identifier: LGPL-2.1 OR MIT */ +/* + * formatted error message for NOLIBC + * Copyright (C) 2026 Thomas Wei=C3=9Fschuh + */ + +/* 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) +{ + fprintf(stderr, "%s: ", program_invocation_short_name); + vfprintf(stderr, fmt, args); + fprintf(stderr, ": %m\n"); +} + +static __attribute__((unused)) +void vwarnx(const char *fmt, va_list args) +{ + fprintf(stderr, "%s: ", program_invocation_short_name); + 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" =20 /* Used by programs to avoid std includes */ #define NOLIBC --=20 2.53.0