From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailtransmit05.runbox.com (mailtransmit05.runbox.com [185.226.149.38]) (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 B1C4E2BEC43 for ; Mon, 23 Feb 2026 10:48:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=185.226.149.38 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771843697; cv=none; b=K/RGx1FuPei5m+6gJ6DfXzPfTWzR7QTqaMbo6oj5lcFkaJK8GPeMY0bsOppnx8ecNc+9Xbq/bDMMuGE24oz9oyWZo+lSDqwZZcDuaIWHU0v8nXya4Pe43vVG87WHDx1NUjeK6hIrw3r+T0lB2MC9vDYyH2BwF5Lupcu9MLRmTss= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771843697; c=relaxed/simple; bh=P6Ml2UaBtGH4hK3sQz7Sa5hALD1X44N559yIFJOsIxU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ce0ZrQliLWLEu5lpPKRAz5fzVg15WiNp95vDoVF+0zkWnt/XQqopqE1l2/AbMF5xnsZIxI0DVf2r+MlHQ4bnUZknm2bhdmRSvQoMAyRrpzUkNLVuLSKmUwpBsWaElNvMZK+hkK210yq1VWlyIgVkJDWSWzdHzeqwAiT8o13BsJY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=runbox.com; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b=WBeFtfaV; arc=none smtp.client-ip=185.226.149.38 Authentication-Results: smtp.subspace.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=runbox.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=runbox.com header.i=@runbox.com header.b="WBeFtfaV" Received: from mailtransmit03.runbox ([10.9.9.163] helo=aibo.runbox.com) by mailtransmit05.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1vuT1D-00GoDa-NW; Mon, 23 Feb 2026 11:18:23 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=runbox.com; s=selector2; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To :Message-Id:Date:Subject:Cc:To:From; bh=dbanrDQfNAigkyuhDRj9Ra0Waug2sr0tPZBE+TFICq4=; b=WBeFtfaVq3P+i6KFoj+DhoCV7w E8JjDR+ZPSsh8omLKKInufJQUYxjkgMSo1oPUnbLWJBDpWbpWVJx3EWLZxEI75gm4mcGz5kn3u5P4 w4/kt+4+sEw0ICtm6LFjrCJhD13Fe3fQyOcLbTPr/HCYjcHsnutr1mUeP3olRoN+gjR2bH8kLMcqN UyB3qRFA4hOLouvnkf+/sFr77WpGuvr01ehuFQnpDaYETtNweCxATvt/c52efSJBVFU4LS4Aae82J sRC4P5xN6RTAc4fr6n+B05z2QikkcjzZoRaQ8GclCGpUobnjMjOh17l32bCfXjbvRSD25bMHcp9pI DlJFbudA==; Received: from [10.9.9.73] (helo=submission02.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vuT1D-0001T0-EE; Mon, 23 Feb 2026 11:18:23 +0100 Received: by submission02.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vuT12-006AjD-6q; Mon, 23 Feb 2026 11:18:12 +0100 From: david.laight.linux@gmail.com To: Willy Tarreau , =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= , linux-kernel@vger.kernel.org, Cheng Li Cc: David Laight Subject: [PATCH v3 next 05/17] tools/nolibc: Implement strerror() in terms of strerror_r() Date: Mon, 23 Feb 2026 10:17:23 +0000 Message-Id: <20260223101735.2922-6-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260223101735.2922-1-david.laight.linux@gmail.com> References: <20260223101735.2922-1-david.laight.linux@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: David Laight strerror() can be the only part of a program that has a .data section. This requres 4k in the program file. Add a simple implementation of strerror_r() (ignores the length) and use that in strerror() so that the "errno=" string is copied at run-time. Use __builtin_memcpy() because that optimises away the input string and just writes the required constants to the target buffer. Put the check for NOLIBC_IGNORE_ERRNO into strerror_r(). Always call strerror() from __nolibc_printf(). Code size change largely depends on whether the inlining decision for strerror() changes. Change the tests to use the normal EXPECT_VFPRINTF() when testing %m. Skip the tests when !is_nolibc. Signed-off-by: David Laight --- New patch for v3. tools/include/nolibc/stdio.h | 29 ++++++++++++++------ tools/testing/selftests/nolibc/nolibc-test.c | 20 ++------------ 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index 233318b0d0f0..2267f50d03b4 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -373,11 +373,7 @@ int __nolibc_printf(__nolibc_printf_cb cb, intptr_t state, size_t n, const char outstr="(null)"; } else if (c == 'm') { -#ifdef NOLIBC_IGNORE_ERRNO - outstr = "unknown error"; -#else outstr = strerror(errno); -#endif /* NOLIBC_IGNORE_ERRNO */ } else if (c == '%') { /* queue it verbatim */ @@ -682,14 +678,31 @@ int setvbuf(FILE *stream __attribute__((unused)), return 0; } +static __attribute__((unused,)) +int strerror_r(int errnum, char *buf, size_t buflen __attribute__((unused))) +{ +#ifdef NOLIBC_IGNORE_ERRNO + __builtin_memcpy(buf, "unknown error", 14); + return 13; +#else + __builtin_memcpy(buf, "errno=", 6); + return 6 + i64toa_r(errnum, buf + 6); +#endif +} + static __attribute__((unused)) -const char *strerror(int errno) +const char *strerror(int errnum) { - static char buf[18] = "errno="; + static char buf[18]; + char *b = buf; + + /* Force gcc to use 'register offset' to access buf[]. */ + _NOLIBC_OPTIMIZER_HIDE_VAR(b); - i64toa_r(errno, &buf[6]); + /* Use strerror_r() to avoid having the only .data in small programs. */ + strerror_r(errnum, b, sizeof(buf)); - return buf; + return b; } #endif /* _NOLIBC_STDIO_H */ diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c index 029ed63e1ae4..61968fdfeec0 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1793,23 +1793,6 @@ static int test_scanf(void) return 0; } -int test_strerror(void) -{ - char buf[100]; - ssize_t ret; - - memset(buf, 'A', sizeof(buf)); - - errno = EINVAL; - ret = snprintf(buf, sizeof(buf), "%m"); - if (is_nolibc) { - if (ret < 6 || memcmp(buf, "errno=", 6)) - return 1; - } - - return 0; -} - static int test_printf_error(void) { int fd, ret, saved_errno; @@ -1859,8 +1842,9 @@ static int run_printf(int min, int max) CASE_TEST(string_width); EXPECT_VFPRINTF(1, " 1", "%10s", "1"); break; CASE_TEST(number_width); EXPECT_VFPRINTF(1, " 1", "%10d", 1); break; CASE_TEST(width_trunc); EXPECT_VFPRINTF(1, " 1", "%30d", 1); break; + CASE_TEST(errno); EXPECT_VFPRINTF(is_nolibc, "22:errno=22", "%d:%m", errno=22); break; + CASE_TEST(errno-neg); EXPECT_VFPRINTF(is_nolibc, "-22: errno=-22", "%d:%12m", errno=-22); break; CASE_TEST(scanf); EXPECT_ZR(1, test_scanf()); break; - CASE_TEST(strerror); EXPECT_ZR(1, test_strerror()); break; CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break; case __LINE__: return ret; /* must be last */ -- 2.39.5