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 48C3D379EDF for ; Mon, 2 Mar 2026 10:18:51 +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=1772446732; cv=none; b=L3blmTuCfoN29tINT11PbxwJkoNCqfD0hUpqdT6gNfwPCk+7c007n5ktiMsA3RKlvkbRbg+w7c4BWDH986RNdzOsGMuGReyqTpyc7hDDu00uDfGbKf34xzgxRd4wDpTQHXXQDxNi2LxzR5vqwW7jOL25P4caf/6knVDYmgOZpuY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772446732; c=relaxed/simple; bh=21Ni4sgy3oP/YfIQHf1CNpcmZ82ZbMS2bDhRc58HV80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=uiy2+yEWbgKxsP/0jkFS19a5h0Oq+Hf26m1pG6E/RI2xCSieCb9xpulk/EFnGjkE1hUbagEMozwmVkIbL7hZ67KUpUGYmETZ0E2HOFyPm7oLZBukPeov6kaaEzrrWsdVvr24B20W2XFhy11+Mn5l8tCHkbobrfeBLfN/53db6Iw= 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=gKxiCkbQ; 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="gKxiCkbQ" 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 1vx0MT-003BZS-Kl; Mon, 02 Mar 2026 11:18:49 +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=C2n+wgBdtz9zcscPp4wqXNFM9CAwkM9llyY5FovAdd4=; b=gKxiCkbQLK7ha/6krn7j4PDoDU Ab1J+UESmZyrgfclRK2nvXGkH7rQYeSEUGo4gGP6FiQk3VVUrRLOyDUnuYxL3m0RWh0k+UO59JZaz fGscuNhBgpacf7fJMn+AK/6u19FE1OGSE3GqoX/6ChE5+I6mGaKq0ad67fxeC4Epm4g8yX3sdDLPn GZ8nAz01sc+nW8oAyyRB3lcewn0bEp8yA7QvvR5JMDQylNHoc5cvQmGXi0c9A+uhzZK54BnmMBE/i uz8xxoKs0BuIayjF8nim6auZRaV06g3qc1wbg3b8B0LBeYdU43+R2EJPh73wqKXTB6XNk5CBLNFr/ nrgF0V+g==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit03.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1vx0MT-0002yL-9i; Mon, 02 Mar 2026 11:18:49 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (1493616)] (TLS1.2:ECDHE_SECP256R1__RSA_PSS_RSAE_SHA256__AES_256_GCM:256) (Exim 4.93) id 1vx0MA-006y7o-Nd; Mon, 02 Mar 2026 11:18:30 +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 v4 next 09/23] tools/nolibc: Implement strerror() in terms of strerror_r() Date: Mon, 2 Mar 2026 10:18:01 +0000 Message-Id: <20260302101815.3043-10-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260302101815.3043-1-david.laight.linux@gmail.com> References: <20260302101815.3043-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 buflen) 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. Ignoring buflen is unlikely to be a problem given that the output is always short. 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 --- v4: - Leave the NOLIBC_IGNORE_ERRNO check in __nolibc_printf(). - Don't rename the errno parameter to strerror() in this patch. tools/include/nolibc/stdio.h | 18 +++++++++++++++--- tools/testing/selftests/nolibc/nolibc-test.c | 20 ++------------------ 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index a4df72d9a2d3..03fcd0229f90 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -722,14 +722,26 @@ int setvbuf(FILE *stream __attribute__((unused)), return 0; } +static __attribute__((unused,)) +int strerror_r(int errnum, char *buf, size_t buflen __attribute__((unused))) +{ + __builtin_memcpy(buf, "errno=", 6); + return 6 + i64toa_r(errnum, buf + 6); +} + static __attribute__((unused)) const char *strerror(int errno) { - 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(errno, 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 9ebebe4ff253..638f18fc5123 100644 --- a/tools/testing/selftests/nolibc/nolibc-test.c +++ b/tools/testing/selftests/nolibc/nolibc-test.c @@ -1786,23 +1786,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; @@ -1852,8 +1835,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", "%25d", 1); break; + CASE_TEST(errno); errno = 22; EXPECT_VFPRINTF(is_nolibc, "errno=22", "%m"); break; + CASE_TEST(errno-neg); errno = -22; EXPECT_VFPRINTF(is_nolibc, " errno=-22", "%12m"); 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