From nobody Mon Jun 22 13:25:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 0A2DEC433F5 for ; Wed, 23 Mar 2022 07:18:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242171AbiCWHUA (ORCPT ); Wed, 23 Mar 2022 03:20:00 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237005AbiCWHTv (ORCPT ); Wed, 23 Mar 2022 03:19:51 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F3F4672E15 for ; Wed, 23 Mar 2022 00:18:21 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 22N7IDKI014463; Wed, 23 Mar 2022 08:18:13 +0100 From: Willy Tarreau To: "Paul E . McKenney" Cc: linux-kernel@vger.kernel.org, Ammar Faizi Subject: [PATCH 1/2] tools/nolibc/string: do not use __builtin_strlen() at -O0 Date: Wed, 23 Mar 2022 08:18:06 +0100 Message-Id: <20220323071807.14413-2-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20220323071807.14413-1-w@1wt.eu> References: <20220323071807.14413-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" clang wants to use strlen() for __builtin_strlen() at -O0. We don't really care about -O0 but it at least ought to build, so let's make sure we don't choke on this, by dropping the optimizationn for constant strings in this case. Signed-off-by: Willy Tarreau --- tools/include/nolibc/string.h | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h index 0d5e870c7c0b..75a453870498 100644 --- a/tools/include/nolibc/string.h +++ b/tools/include/nolibc/string.h @@ -122,7 +122,9 @@ char *strcpy(char *dst, const char *src) return ret; } =20 -/* this function is only used with arguments that are not constants */ +/* this function is only used with arguments that are not constants or when + * it's not known because optimizations are disabled. + */ static __attribute__((unused)) size_t nolibc_strlen(const char *str) { @@ -132,11 +134,18 @@ size_t nolibc_strlen(const char *str) return len; } =20 +/* do not trust __builtin_constant_p() at -O0, as clang will emit a test a= nd + * the two branches, then will rely on an external definition of strlen(). + */ +#if defined(__OPTIMIZE__) #define strlen(str) ({ \ __builtin_constant_p((str)) ? \ __builtin_strlen((str)) : \ nolibc_strlen((str)); \ }) +#else +#define strlen(str) nolibc_strlen((str)) +#endif =20 static __attribute__((unused)) size_t strlcat(char *dst, const char *src, size_t size) --=20 2.35.1 From nobody Mon Jun 22 13:25:03 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9D83AC433F5 for ; Wed, 23 Mar 2022 07:18:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242183AbiCWHUH (ORCPT ); Wed, 23 Mar 2022 03:20:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241153AbiCWHTv (ORCPT ); Wed, 23 Mar 2022 03:19:51 -0400 Received: from 1wt.eu (wtarreau.pck.nerim.net [62.212.114.60]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id F420B72E33 for ; Wed, 23 Mar 2022 00:18:21 -0700 (PDT) Received: (from willy@localhost) by pcw.home.local (8.15.2/8.15.2/Submit) id 22N7IDK9014464; Wed, 23 Mar 2022 08:18:13 +0100 From: Willy Tarreau To: "Paul E . McKenney" Cc: linux-kernel@vger.kernel.org, Ammar Faizi Subject: [PATCH 2/2] tools/nolibc/stdlib: only reference the external environ when inlined Date: Wed, 23 Mar 2022 08:18:07 +0100 Message-Id: <20220323071807.14413-3-w@1wt.eu> X-Mailer: git-send-email 2.17.5 In-Reply-To: <20220323071807.14413-1-w@1wt.eu> References: <20220323071807.14413-1-w@1wt.eu> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" When building with gcc at -O0 we're seeing link errors due to the "environ" variable being referenced by getenv(). The problem is that at -O0 gcc will not inline getenv() and will not drop the external reference. One solution would be to locally declare the variable as weak, but then it would appear in all programs even those not using it, and would be confusing to users of getenv() who would forget to set environ to envp. An alternate approach used in this patch consists in always inlining the outer part of getenv() that references this extern so that it's always dropped when not used. The biggest part of the function was now moved to a new function called _getenv() that's still not inlined by default. Reported-by: Ammar Faizi Signed-off-by: Willy Tarreau Tested-by: Ammar Faizi --- tools/include/nolibc/stdlib.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tools/include/nolibc/stdlib.h b/tools/include/nolibc/stdlib.h index aca8616335e3..8a07e263f0d0 100644 --- a/tools/include/nolibc/stdlib.h +++ b/tools/include/nolibc/stdlib.h @@ -60,16 +60,17 @@ int atoi(const char *s) return atol(s); } =20 -/* Tries to find the environment variable named in the environment = array - * pointed to by global variable "environ" which must be declared as a cha= r **, - * and must be terminated by a NULL (it is recommended to set this variabl= e to - * the "envp" argument of main()). If the requested environment variable e= xists - * its value is returned otherwise NULL is returned. +/* getenv() tries to find the environment variable named in the + * environment array pointed to by global variable "environ" which must be + * declared as a char **, and must be terminated by a NULL (it is recommen= ded + * to set this variable to the "envp" argument of main()). If the requested + * environment variable exists its value is returned otherwise NULL is + * returned. getenv() is forcefully inlined so that the reference to "envi= ron" + * will be dropped if unused, even at -O0. */ static __attribute__((unused)) -char *getenv(const char *name) +char *_getenv(const char *name, char **environ) { - extern char **environ; int idx, i; =20 if (environ) { @@ -83,6 +84,13 @@ char *getenv(const char *name) return NULL; } =20 +static inline __attribute__((unused,always_inline)) +char *getenv(const char *name) +{ + extern char **environ; + return _getenv(name, environ); +} + /* Converts the unsigned long integer to its hex representation into * buffer , which must be long enough to store the number and the * trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The --=20 2.35.1