From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DFF532F659F for ; Sun, 8 Mar 2026 11:37:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.44 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772969875; cv=none; b=hP2ZoUc+VKGTQauGyzzHaD8JX9FDxVYO2mxEnak6tfPoTNiD0UoGRtl/LvrXtNXIyb17Shq1oJQg9uWqTcbKB26nanw1TSItFN0MozIjV9s8vzT+v5LPwMyphZvVtZGPiu4LnppknLhWFjA4qElrndUR7ZKwmZBUXiOZj+mgz3c= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772969875; c=relaxed/simple; bh=PXQaEe0fmSCOAJq6izJx3WPw7rKKNks8Z3ateh/yx80=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O7S6ENnUlgf+6V8599b5sUapB+PMfl5lz2uLgAUnQ0yggieV+PwziPEyTE7jcd/XIjeCLqeDXYTbMq7z2h1vPoxNNa3VIn/kI1TuSf0IErsKx7+k5+1WORIO74UCtWH/ob06uaGf337occ/cpUCjD95plO94wjtEq3M/IzGTF7E= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=JW0UYn8S; arc=none smtp.client-ip=209.85.221.44 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="JW0UYn8S" Received: by mail-wr1-f44.google.com with SMTP id ffacd0b85a97d-439c9eb5d36so3921331f8f.2 for ; Sun, 08 Mar 2026 04:37:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772969872; x=1773574672; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=SQ++kaR9otGJoykKyOnnTuQdcMyQbk/tZXUrcc3Q2eE=; b=JW0UYn8SIq5R80q/XNisXJBuZGEvucKHP/sZx7x3oy/wnJRqQOgCxncHRJlkzGLqcQ bR9F4NeSej9M+6xyd4fX11CVTKfiwJMgm08X9TjbeqLd1TzRzOOm6Ve2TwDA0JPfhtp4 ORzlyd31ClDDGvSQY1b3Q6jCaWimzXRGRJZ5JqsRcThB+sVILvAFqZPpwKlCU4KU/52v jFibXP+lcso1VsO9081ssEKLogMVTDpk8/k7G7PKkDX7mMeVQsNHiFil9AOMWbJImUjK TyKEd9e3Ny9ITcUAYIE10a1rAQ8pSYT44bEHI/2KpB7ANzaDyLw9JLBm3MOzFRD/yO7v My2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772969872; x=1773574672; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=SQ++kaR9otGJoykKyOnnTuQdcMyQbk/tZXUrcc3Q2eE=; b=tPFQ4mRbmvBnM/ZfsuIBbGbfr6FXQ9+ixy95lRzxZYcV9WK3/wdENuMovwS5YBhyq6 1GkR9jTEzA0iG93YI9QlHWAwMDrzedDkbJuydRpLaYZ55o8AfxA77PEq90/SmkCDPsOk MAFR/ERDlV6IFNikE8TkkS993Fsd7vu+kzMeL6Vi7KwAnaiq1qTsYDn8bI2TdhEB+xV0 aM3Ish0CYFId9IjdV4Hp8g+ObZ9i4EICVSzvVEBDZ74SfiL64ZSYU8IYELsBKU3UGFbR p6FGZQPWnD0YOGZ3JoKwR/1knH6dwzdKCiOfv+2l0hu2CX9ZCiaT67yynTzJAxT6oQvO gkJQ== X-Forwarded-Encrypted: i=1; AJvYcCVnB/l18WKudEz7zWbeXeWsGiXThL4w6s5mApqIY3NduzqXXdIrFrUp8gpZ5p2WhwY4PxSCUzbv14+GDQY=@vger.kernel.org X-Gm-Message-State: AOJu0YzfZkh1O1nVWh8FXeHa3px1ZxiVNV6n+zVW+AfbJsVyxt4bnKJR yB/Hhc2RUsdTIlAsElq7N4gCdtCu/VNzQYLhKfmc72MqzA4WG4Kxd95S X-Gm-Gg: ATEYQzzTXywvs2wXouuhwxzNWjpNm8LxnmSa6c/ekaL14wbn5ak5gi9w53BFmD4ZhiZ 4HGOjr6FmScBTsIk62KNvA6dIfOTeyt03R+cC5B/nUy2FwOFge1ATo5VQxSFXJD2IysV09k9AAR LMbFbjgGHnuCbOfpXCVh6BKCiZ5fPsEWKmgiGcwU+VKfJCEsyFrINQqTGcIX37dzKoSLqEDHxkM BnYjR25g9Wa4avF52mVydZOSqjXNevWR9NeqXbEk/nuMCzhG7/yP38T3M9mvo+eVA1JNKxRS+AI xOhwmfzAvW2ZMoqPZDGRW7RJTBNO5KHt+uBTiLY8dNy+5s5NGf9TdvVEOoLsbmVsCYDG5776kTv FK+UG0O/4y+JoILko1T5JcIaYTytRNZx2c1h5oMjRvwxIOGz5Vq1YbnEUDtnrvF8geb3xChguQJ DCQaHuagubf8c1dS/ECbEwK/050R7IxJrHA/1g+6IQxsDbgHCATlCKTGyCSmBiKUORBQn2F0I7b DM72HrFFYgK X-Received: by 2002:a05:600c:a413:b0:485:2fe9:336f with SMTP id 5b1f17b1804b1-4852fe93699mr50702485e9.30.1772969872211; Sun, 08 Mar 2026 04:37:52 -0700 (PDT) Received: from snowdrop.snailnet.com (82-69-66-36.dsl.in-addr.zen.co.uk. [82.69.66.36]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-485237dd017sm63621855e9.2.2026.03.08.04.37.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Mar 2026 04:37:52 -0700 (PDT) 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 v5 next 03/17] tools/nolibc: Implement strerror() in terms of strerror_r() Date: Sun, 8 Mar 2026 11:37:28 +0000 Message-Id: <20260308113742.12649-4-david.laight.linux@gmail.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20260308113742.12649-1-david.laight.linux@gmail.com> References: <20260308113742.12649-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() 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. 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. Acked-by: Willy Tarreau Signed-off-by: David Laight --- v5: - strerror_r() needs to return 0 on success. - Return ERANGE if the buffer is too short to contain the longest possible output. 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 | 22 +++++++++++++++++--- tools/testing/selftests/nolibc/nolibc-test.c | 20 ++---------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h index a4df72d9a2d3..af2d5be47aa3 100644 --- a/tools/include/nolibc/stdio.h +++ b/tools/include/nolibc/stdio.h @@ -722,14 +722,30 @@ int setvbuf(FILE *stream __attribute__((unused)), return 0; } +static __attribute__((unused,)) +int strerror_r(int errnum, char *buf, size_t buflen) +{ + if (buflen < 18) + return ERANGE; + + __builtin_memcpy(buf, "errno=", 6); + i64toa_r(errnum, buf + 6); + return 0; +} + 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