From patchwork Tue Sep 2 19:26:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119395 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2E9393858431 for ; Tue, 2 Sep 2025 19:30:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2E9393858431 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=yi4pZRvh X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 94EEF3858D39 for ; Tue, 2 Sep 2025 19:27:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 94EEF3858D39 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 94EEF3858D39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::434 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841270; cv=none; b=gj1Q5BZ/owWDjxIJv898gXlJnR1TPjgky9lvbLmrWvnJy9mxN/cvcze3FCmPHTzReX4WuakY2+wwypsDNjj4mulgikpH+iFjiX+COKuHJOTZUhB8hhlugqcmDvq9VLqQty/vpuy6NZ05qaL2DG3L0aZW8QZNlYrItSvBsFQziIo= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841270; c=relaxed/simple; bh=Gm5vuKyJW10iNBOHo0h9L88KqPwzr4cco2hIf7Ew5/k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QknERB085qSwY00YzngNiEmEWm1txePjl9qXuk4DO21+31lkym6rgNoZUMJwtayCFPitW7agD9P0I5am43OgrcAKl4L116oqzPBAjl4D4QbUx/mTwzOrh1wh5xC5nWZ//JyPXtiqLDTKTtlYzHtY8+RjrEocDPQbsZRSq9u32h4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 94EEF3858D39 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-7722c8d2694so3680787b3a.3 for ; Tue, 02 Sep 2025 12:27:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756841269; x=1757446069; darn=sourceware.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=ZdCvmMN9YvPapJBX35Lbog4twEIjbHR1ObyLX4X4Hew=; b=yi4pZRvhYqvP1/ZufMdBl1uvm4t3bqIGwiu10uwlSN7kExGa9zDlwAxCY9a4XciK4M 4W8k8fIbCuZZqWpnuoZk7GackypPqnDxG/uMs98VarhV++yfjsUv/a+L74kQj6aiBx0i WdlBGbaz3SRksu5C2jjk8yN7sgbUt6IO8C1VWCaeHHm8vbvT2JWLdgkZeyXJ2K+7+Xze uT2G0ZWJcLWW9rm5OHndnd7ycBo9cT0SCq0V/nNTPL8BVzpbWuJkCJcKF1KhUcIiq5oS aALbnxaNdKmWnKdk3XIfo56FfTubjpL+6AbTyM2K+6OTyAUBYyggxEb2iNPjhgF8cKPv g1/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756841269; x=1757446069; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=ZdCvmMN9YvPapJBX35Lbog4twEIjbHR1ObyLX4X4Hew=; b=B0P3zO/qr0Ft8sjiwVe7goG3R7LumnJrpWJMviGjVbGDcw6flwGEQm4aF/zKRwWFqY q3VXVVeoVHVfZ8GgeU63jJGLHmFAEajAMfU6wToya6ab8DXSLyWvjvPl2XvJezHpg3IN X3hJdfmxQIydkIciWHQ4lwdEhMqghtBhuottsVGNpDWmjhmvQFOnrC4wLZfXIPbkFY2S 8G/h54tJRE+xf+T6OmPdtH1JISq8aicE4+iFgJ2UVGgVdw15fO8LRbbZzkSdKHuoo4EI UZlh9XpefekMRNMXlZ31iX0XJbpIGFbWhpnGD7tAlV6omtaxieEMIGY95N2kYRzqZIeu iAsQ== X-Gm-Message-State: AOJu0YzwM0MbbG3zx0KqdEO06JhCYBXR+85f6sj2EWa3GFPYYVDwSS5l DlJmmLbS3XguwJBGja03ijSXxTlPJbccUtPI05lo7ULS6NIV/vtO/JDQRMIcF9slzibEPWtCDFe kHeaq X-Gm-Gg: ASbGncsm8vauiIpIAUy52rzlq3nzf96edCKAvaPrF2FBut9UchTsW4sUi27xgSw4/6v 82YFTw+No4JBWPE+mAw6TLBkdaW5InBaTXcARUA5jLALZgPAGyjoAtm0XGQKbkUg4nstht3fA7j I86J7Cmit61HvYFJGbKwJF3OQRhfey9p7U4hkgvozAu9nJBWquJty+MhXAtZTLgMe5hKyGd/V7D vrQ6wp8wLD2Iqm0OhRZlC2IMT0DcVooNyNhyOrmhpe3dMhdhO5HA7Zc8mom0tFvInIr7qvhm0th IBiRdb/qIORcb0XEKj0QLi3hBfUTPVfiK43DJ2oaArV9ktmFjBXUJJSxpW3grURa/T5LKMVffbC DdukSaMqMzUDsA1a0mpdKG1e7Nt9WUDV1Fg== X-Google-Smtp-Source: AGHT+IF6V+1OhYzyCT/JxrNJYehLwgxSH4J55sd+FYtY+470qmIk5+gZxeWLDdVxwqNPHE4J6cMTxA== X-Received: by 2002:a05:6a20:918f:b0:246:d43a:3856 with SMTP id adf61e73a8af0-246d449839amr318701637.22.1756841269098; Tue, 02 Sep 2025 12:27:49 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:b25:e661:f380:50c7:e6aa]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-772396d2dbcsm12079458b3a.50.2025.09.02.12.27.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 12:27:48 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Florian Weimer Subject: [PATCH v2 1/5] string: Add fallback implementation for ctz/clz Date: Tue, 2 Sep 2025 16:26:53 -0300 Message-ID: <20250902192743.2773679-2-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> References: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The libgcc implementation for __builtin_clzl/__builtin_ctzl may require access to additional data that is not marked as hidden, which could create additional GOT indirection that necessitates RELATIVE relocs. And the RELATIVE reloc is an issue if the code is used during static-pie startup before self-relocation (for instance, during an assert). For this case, the ABI can add a string-bitops.h header that defines HAVE_BITOPTS_WORKING to 0. A configure check for this issue is tricky because it requires linking against the standard libraries, which create a lot of RELATIVE relocations and complicate filtering the ones that might be created by the builtins. The fallback is disabled by default, so no target is affected. --- sysdeps/generic/string-bitops.h | 26 +++++++++++++++++ sysdeps/generic/string-fzi.h | 50 ++++++++++++++++++++++----------- 2 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 sysdeps/generic/string-bitops.h diff --git a/sysdeps/generic/string-bitops.h b/sysdeps/generic/string-bitops.h new file mode 100644 index 0000000000..8c5c168cdd --- /dev/null +++ b/sysdeps/generic/string-bitops.h @@ -0,0 +1,26 @@ +/* Zero byte detection, define whether to use stdbit.h + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* These macros define whether index_first/index_last macro can use the + stdbit.h routines, otherwise fallback that do not call libgcc is used + instead. + + An architecture can override it is it uses a generic string routines + on process startup and it can not use the libgcc routines for find + trailing/leading zeros. */ +#define HAVE_BITOPTS_WORKING 1 diff --git a/sysdeps/generic/string-fzi.h b/sysdeps/generic/string-fzi.h index 3c1028d1ec..8dad61c0d4 100644 --- a/sysdeps/generic/string-fzi.h +++ b/sysdeps/generic/string-fzi.h @@ -22,23 +22,39 @@ #include #include #include +#include +#include -static __always_inline int -clz (find_t c) -{ - if (sizeof (find_t) == sizeof (unsigned long)) - return __builtin_clzl (c); - else - return __builtin_clzll (c); -} - -static __always_inline int +static __always_inline unsigned int ctz (find_t c) { - if (sizeof (find_t) == sizeof (unsigned long)) - return __builtin_ctzl (c); +#if HAVE_BITOPTS_WORKING + return stdc_trailing_zeros (c) / CHAR_BIT; +#else + if (sizeof (find_t) <= 4) + return (((c & -c) >> 7) * 0x00010203) >> 24; else - return __builtin_ctzll (c); + return (((c & -c) >> 7) * 0x0001020304050607UL) >> 56; +#endif +} + +static __always_inline unsigned int +clz (find_t c) +{ +#if HAVE_BITOPTS_WORKING + return stdc_leading_zeros (c) / CHAR_BIT; +#else +# if ULONG_MAX == 0xFFFFFFFFUL + c |= c >> 8; + c |= c >> 16; + return ((c >> 7) * 0x30f0f0f0) >> 28; +# else + c |= c >> 8; + c |= c >> 16; + c |= c >> 32; + return ((c >> 7) * 0x70f0f0f0f0f0f0f0UL) >> 60; +# endif +#endif } /* A subroutine for the index_zero functions. Given a test word C, return @@ -47,12 +63,12 @@ ctz (find_t c) static __always_inline unsigned int index_first (find_t c) { - int r; + unsigned int r; if (__BYTE_ORDER == __LITTLE_ENDIAN) r = ctz (c); else r = clz (c); - return r / CHAR_BIT; + return r; } /* Similarly, but return the (memory order) index of the last byte that is @@ -60,12 +76,12 @@ index_first (find_t c) static __always_inline unsigned int index_last (find_t c) { - int r; + unsigned int r; if (__BYTE_ORDER == __LITTLE_ENDIAN) r = clz (c); else r = ctz (c); - return sizeof (find_t) - 1 - (r / CHAR_BIT); + return sizeof (find_t) - 1 - r; } #endif /* STRING_FZI_H */ From patchwork Tue Sep 2 19:26:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119396 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 064CF3858C78 for ; Tue, 2 Sep 2025 19:30:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 064CF3858C78 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=MSpklT8P X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x434.google.com (mail-pf1-x434.google.com [IPv6:2607:f8b0:4864:20::434]) by sourceware.org (Postfix) with ESMTPS id 4C1D73858D33 for ; Tue, 2 Sep 2025 19:27:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C1D73858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4C1D73858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::434 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841272; cv=none; b=XV/1OcJh1DeWSo5V4lxvJGSsLnxc/OFzXflw0m2aq1/dLzgBmebmmwH+CcRloCkqhi2oSEJu8ARD47djaDVM9gxZAWizcROlmgpj5eITpSzzXVPI2EVWNcwQVdqKgsXLzjHurur1THu45kMPHW9yO1XqHUhCopkD3LQv0JAc2zE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841272; c=relaxed/simple; bh=f5L+8LHUb2kzEyfpjy0N3qM5vQ3GBpjoR4pjTp8zCuo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QtNbZ1Zyy+cYqW18Mhwu72OGCY16rm+AciAbzMn5zJTjKb0N41DkHZ+l53lR6W2DiMjJQG5Lwg9mQ71T6A+AQWwnfzcGLkxjVzsXcfoJP/gKyMAb8NWNUGPuCJ5FEYlHYcOKKKqmbt1b5Tq1FRNDpErDu7qb86x0ubS8Fayt8vo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4C1D73858D33 Received: by mail-pf1-x434.google.com with SMTP id d2e1a72fcca58-7725147ec88so160891b3a.0 for ; Tue, 02 Sep 2025 12:27:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756841271; x=1757446071; darn=sourceware.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=eV+uL4MBAj6ms169IHK99eyzFRnaLgCwbQ2BIeJAMA0=; b=MSpklT8PPTrpxUv0xFmDxUJag+GSi5L2txwKeZlrcrof3hTXSaiFfveHsikDsxMWSC 5QFUpVtfZ3ffPq0qTAhq5i76lJ04GSjrWd2w7BCgFOynUwK3FsVBOv+psK1uy1YPwwB+ wJ26d71ZHLqXNrTc3MR+GX8lrrLE7qYWtwbd8SLSoA/BI7G/VLYNyVOpuxNotUihGXc+ e/EzjX30BlCERKicJmlTqBYuggmewHQ8UvFMyG3l+l8BHYPuFJ114NxINUYKFROuPFAD D8XwKXitpEIMEb8hiC8Ojs8R/imzBT48Mn5xHD/+jU7GxqC/sB72pdkojQ0Ss781pDZ/ K43w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756841271; x=1757446071; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eV+uL4MBAj6ms169IHK99eyzFRnaLgCwbQ2BIeJAMA0=; b=mDMF6qpp6QR0juXhbwrt90VU2bpUIJiMaPGUhuseY4Qq90Z/Bl9Kx+eKr9IGfmj7aY GCsf5DUjoqHv1wqdBjTJV3AUAyeaqIXhduzvgeg7kcuwWSFtaNAvME/u6gEeS0GCT+A8 1A+k9MbuU8m8QX27t2esdBaagvvj8lczKzXd00/rkvAjptweKYz2OtfLQ1s1ZJrCf7X9 C5i1HSil3igYJmW1m7kKzYbPSxTlFqPOz/gZ8oqLusFzm1wzc9/nuMzgyH7r1zXxWLhj MFCU6ou8Z3XYXCsHG/fwG5Vt+I0uPHkHkHhTLXwLiaqoyXya6pzjYUo+s8tIY5A01fZs Ycew== X-Gm-Message-State: AOJu0YwXk5uSPCSbWLga9iuiUdFsgJQGEZtBqwQZIK0Dkzcl8K92qYkp ksVGCwGkoUfRtI4uWAKn5lq+x6xqn30nkv4iyikJYkzgBbqp6BuAjgDm74eo1fUA5xxE/nJ3XNW he9vh X-Gm-Gg: ASbGncvJQBg6a75QlRv69mmVeaZdCaXhjp/mPmNWsOE2RI1MEky0yt679j5hmqd1cuu sIqHvHLzLw1EHGjJE60X0o4Dz6Z3a6kkHP6Id4/39DBYmcH3IwtI5QGLS9G0xkLvaHOxKzJdF6m 49tSTV7sezduPmcMuaTzXLrQsbHAukg3kFHewkLficgeQZjimEeZUpRxWcn+XxJFOh0h4/zax8j fmhv7r6BIGxd8zz6vgT2KusI+em7HXGFST8NUpf8rkSnc5Stiar2420jYSCY22doo7LRxqHgS1S m2WOIZm3LTpmH4DnErnzH7Wp/5b/zQbXPTV+Exe6+VDx077crA4SgDBrkGnWhpr/zsH8El5hI+6 xGvg4vvHF04pDjudnCY7K8um5brKP8AoNO0qm7KuUc4TT X-Google-Smtp-Source: AGHT+IEQyyFMWUDiRIpoWbs60u27u6BKtAzZt/Kh0aAI/wdLY9t9yHsW11PdaRfwFPQuOk2Aagr6UQ== X-Received: by 2002:a05:6a20:7a11:b0:243:eaa4:b21a with SMTP id adf61e73a8af0-243eaa4b25bmr8289071637.27.1756841270827; Tue, 02 Sep 2025 12:27:50 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:b25:e661:f380:50c7:e6aa]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-772396d2dbcsm12079458b3a.50.2025.09.02.12.27.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 12:27:50 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Florian Weimer Subject: [PATCH v2 2/5] nptl: Add __raise_nocancel Date: Tue, 2 Sep 2025 16:26:54 -0300 Message-ID: <20250902192743.2773679-3-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> References: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The function sends a signal to current thread using raw syscalls. --- include/signal.h | 2 ++ nptl/pthread_kill.c | 21 ++++++---------- signal/Makefile | 2 +- signal/raise_nostatus.c | 27 +++++++++++++++++++++ sysdeps/posix/raise.c | 2 +- sysdeps/unix/sysv/linux/raise_nostatus.c | 31 ++++++++++++++++++++++++ 6 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 signal/raise_nostatus.c create mode 100644 sysdeps/unix/sysv/linux/raise_nostatus.c diff --git a/include/signal.h b/include/signal.h index 73f18dddd7..9aa4c0e055 100644 --- a/include/signal.h +++ b/include/signal.h @@ -65,6 +65,8 @@ extern int __xpg_sigpause (int sig); /* Allocate real-time signal with highest/lowest available priority. */ extern int __libc_allocate_rtsig (int __high); +extern int __raise_nostatus (int signo) attribute_hidden; + # if IS_IN (rtld) extern __typeof (__sigaction) __sigaction attribute_hidden; extern __typeof (__libc_sigaction) __libc_sigaction attribute_hidden; diff --git a/nptl/pthread_kill.c b/nptl/pthread_kill.c index 3938e3f8cd..6529887ff5 100644 --- a/nptl/pthread_kill.c +++ b/nptl/pthread_kill.c @@ -29,20 +29,13 @@ __pthread_kill_implementation (pthread_t threadid, int signo, int no_tid) { struct pthread *pd = (struct pthread *) threadid; if (pd == THREAD_SELF) - { - /* Use the actual TID from the kernel, so that it refers to the - current thread even if called after vfork. There is no - signal blocking in this case, so that the signal is delivered - immediately, before __pthread_kill_internal returns: a signal - sent to the thread itself needs to be delivered - synchronously. (It is unclear if Linux guarantees the - delivery of all pending signals after unblocking in the code - below. POSIX only guarantees delivery of a single signal, - which may not be the right one.) */ - pid_t tid = INTERNAL_SYSCALL_CALL (gettid); - int ret = INTERNAL_SYSCALL_CALL (tgkill, __getpid (), tid, signo); - return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0; - } + /* There is no signal blocking in this case, so that the signal is + delivered immediately, before __pthread_kill_internal returns: a signal + sent to the thread itself needs to be delivered synchronously. (It is + unclear if Linux guarantees the delivery of all pending signals after + unblocking in the code below. POSIX only guarantees delivery of a + single signal, which may not be the right one.) */ + return __raise_nostatus (signo); /* Block all signals, as required by pd->exit_lock. */ internal_sigset_t old_mask; diff --git a/signal/Makefile b/signal/Makefile index 8c7961c5f0..468a8b93f0 100644 --- a/signal/Makefile +++ b/signal/Makefile @@ -34,7 +34,7 @@ headers := signal.h sys/signal.h \ bits/types/__sigval_t.h bits/signal_ext.h \ bits/sigstksz.h -routines := signal raise killpg \ +routines := signal raise raise_nostatus killpg \ sigaction libc_sigaction sigprocmask kill \ sigpending sigsuspend sigwait \ sigblock sigsetmask sigpause sigvec \ diff --git a/signal/raise_nostatus.c b/signal/raise_nostatus.c new file mode 100644 index 0000000000..ed572ddee2 --- /dev/null +++ b/signal/raise_nostatus.c @@ -0,0 +1,27 @@ +/* Internal function to send a signal to itself. Generic version. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +int +__raise_nostatus (int signo) +{ + return __kill (__getpid (), signo); +} diff --git a/sysdeps/posix/raise.c b/sysdeps/posix/raise.c index da3f11ccf2..3c5f032c5c 100644 --- a/sysdeps/posix/raise.c +++ b/sysdeps/posix/raise.c @@ -23,7 +23,7 @@ int raise (int sig) { - int ret = __pthread_kill (__pthread_self (), sig); + int ret = __raise_nostatus (sig); if (ret != 0) { __set_errno (ret); diff --git a/sysdeps/unix/sysv/linux/raise_nostatus.c b/sysdeps/unix/sysv/linux/raise_nostatus.c new file mode 100644 index 0000000000..6f9ebc1ac9 --- /dev/null +++ b/sysdeps/unix/sysv/linux/raise_nostatus.c @@ -0,0 +1,31 @@ +/* Internal function to send a signal to itself. Linux version. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include + +int +__raise_nostatus (int signo) +{ + /* Use direct syscall to avoid external symbols. */ + pid_t tid = INTERNAL_SYSCALL_CALL (gettid); + pid_t pid = INTERNAL_SYSCALL_CALL (getpid); + int ret = INTERNAL_SYSCALL_CALL (tgkill, pid, tid, signo); + return INTERNAL_SYSCALL_ERROR_P (ret) ? INTERNAL_SYSCALL_ERRNO (ret) : 0; +} From patchwork Tue Sep 2 19:26:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119398 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6E5483858C78 for ; Tue, 2 Sep 2025 19:32:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6E5483858C78 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=ro4ZKPKr X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id 29F5B3858C2D for ; Tue, 2 Sep 2025 19:27:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 29F5B3858C2D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 29F5B3858C2D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::435 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841274; cv=none; b=IboEMem/Mrp1f8QhbxjWfTOr79rVDuqXY7J26XeUYfknDg8jqDpeow2BHV79OC8RjOfYkKsItoBF9rXZzI8BU1yokzYb39DKdebP3bPos97LAEPj3weMScqZrquES8nDFDhhUUfbMKYQnQJHyJWW7CvI53kE9DPSNrQhyQSlQlI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841274; c=relaxed/simple; bh=KBOhLgjpq5e5Wf21w6bU6mQhVbxevvRfdzr8WMX7oTQ=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YfBGMFNmRWatwVc6g2tLIo0Ivltg0p+sURnFAoJLSpQ9psoqabCivnLLJQy8TFPWI6hQoXNzdKjKAAhHwyDvQ/vTfeV6aQTb8+SW301txZcmjWQvIWnUVrZJFnC3XXglqRZZwOMMdoNcKMZzSAPsYEcIgGq2DnooDNYaZLGbl90= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 29F5B3858C2D Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-77251d7cca6so2251919b3a.3 for ; Tue, 02 Sep 2025 12:27:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756841273; x=1757446073; darn=sourceware.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=QhopaJE6utWF6eZ2ikcTckbYqdgzP6Ai3ngd+r3OaiM=; b=ro4ZKPKrkAGjn4nruuRqHJN1GwoUStbc/x2FqwmbbClTdLbKgeIio9nUwQv+OSOggg E5Gh79EPyN7F6WCYk91qpq0JWSHjBr9wojm5RKSfUi3YC/xjqCzLcfwOFiGQKYbTmkBR v5xPV3Vd+Hqmua+eh71YydGrcZ44oyLHacLH6gcjk6qi2GXPtDIcZC8dwzBLOIz8U5vM yDRt+7fAAL8y8w3ME870QofiFxtRszpeq3l20o31PrYR5g2le7QsuNYEwU0Yr8oqqYEE 7lwPSUjfDQjTWSJPKkDSXHxthqTmFiKLok+zDwyXUZSmTO6PoOdpkH0raT9qyhTN4B7j kVqQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756841273; x=1757446073; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QhopaJE6utWF6eZ2ikcTckbYqdgzP6Ai3ngd+r3OaiM=; b=Kg5vBxEeM8fMw5aiKvcluQRA/qGBOQ2yuB2viL2pjLH067ep2MaYKFWnWzfaWf6cra z3B7mRffpnmYCcwwdHtTvZ2MA4Z/4ruAzf2Q9zHvdBOOStC5moZH8N+nQHtjgYYwsnLD mYmtu95lbgw4jldxjoqhAWXrR4o9RYq7BDZLJqf5qHbDEbKhovieGvGR+KyvJFcfA4Ka 7SFFQRwwL48OdN4f9o3wVBiaEIIK4dZhzN/775PnnStXIQ/oU0ZAq8Ntvm/Ve5JnTnfg d2PM+b4XcL3ogocWY53y8dzAc0BC2MdWO0wff5BiqBHK0KWnv0ISjHxIy4Eo1+JGRcw2 qyZQ== X-Gm-Message-State: AOJu0Yyy4qxe4VulpdBKfiS3jTfPQnhG+m5g5/MTfVdWYivzK9px9HBC VX2v7iDrBdclqV2bhwo1+lj5XoSu3YT0GR5eZ2EGCwX8/m63uC6qA715QeDHy31cvkFc3+nppXL OOg4u X-Gm-Gg: ASbGnctU6g1J4dVnK/u014LO0xX2zhsTLmIIl7lBvL13+3UrHFUP9kaCb2/gjMo4Z8u NttuYw66SteGa8M+jsGtgJ3u7e40DueGnBdoOJjyreF1nyPJDqROT1aDflgB2HGkAzOscD295Z8 B4wKcZ+JgTYshIvGbRrBabPZfZuIjv7ipee60TyNAGT1CE05Y+Hqm8FAasXVG+7jV3OTf6r4dUP MHmW1xB+gn+ShmRneYBr51AGcyX6bGXwTZZ1Les7uags4LpJ9eA/yY8NPvDmcq0hfdIXD2ent9/ siKRXhlNwOxdA94Kxn80jfRYH2rhOetyYFpWDnuBVw4jw54zoYbODpThQXzQaWNbPrhkEucwRjq JmTjVXdj3wNEHrr1UNuhz4Z5B4SvejlR4k/gFQFBiHgBJ X-Google-Smtp-Source: AGHT+IFECrvq/+alCbvlTSTM7ATNk9aB0rmvLW/DsW1jrW8qcmcsAf8UMbnLm9zrpYSYG5PLHwhvOQ== X-Received: by 2002:a05:6a00:84a:b0:772:5165:3f68 with SMTP id d2e1a72fcca58-772516540fdmr11073746b3a.27.1756841272609; Tue, 02 Sep 2025 12:27:52 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:b25:e661:f380:50c7:e6aa]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-772396d2dbcsm12079458b3a.50.2025.09.02.12.27.51 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 12:27:52 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Florian Weimer Subject: [PATCH v2 3/5] Use _dl_writev on __libc_message_impl Date: Tue, 2 Sep 2025 16:26:55 -0300 Message-ID: <20250902192743.2773679-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> References: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org And change _dl_writev to return an negative errno in case of failure. This keeps the required semantic on not setting errno in case of failure and allow remove the Linux libc_fatal.c implementation. It also makes is simple to use writev syscall during process startup, specially on i386 where it requires to disable the vDSO usage. --- {elf => sysdeps/generic}/dl-writev.h | 18 +++++++------- sysdeps/posix/libc_fatal.c | 15 ++++++------ sysdeps/unix/sysv/linux/dl-writev.h | 13 ++++------ sysdeps/unix/sysv/linux/libc_fatal.c | 36 ---------------------------- 4 files changed, 21 insertions(+), 61 deletions(-) rename {elf => sysdeps/generic}/dl-writev.h (80%) delete mode 100644 sysdeps/unix/sysv/linux/libc_fatal.c diff --git a/elf/dl-writev.h b/sysdeps/generic/dl-writev.h similarity index 80% rename from elf/dl-writev.h rename to sysdeps/generic/dl-writev.h index cf22367a82..0829da4ee9 100644 --- a/elf/dl-writev.h +++ b/sysdeps/generic/dl-writev.h @@ -20,37 +20,35 @@ #include #include -/* This is used from only one place: dl-misc.c:_dl_debug_vdprintf. - Hence it's in a header with the expectation it will be inlined. - - This is writev, but with a constraint added and others loosened: +/* This is writev, but with a constraint added and others loosened: 1. Under RTLD_PRIVATE_ERRNO, it must not clobber the private errno when another thread holds the dl_load_lock. - 2. It is not obliged to detect and report errors at all. - 3. It's not really obliged to deliver a single atomic write + 2. It's not really obliged to deliver a single atomic write (though it may be preferable). */ -static inline void +static inline ssize_t _dl_writev (int fd, const struct iovec *iov, size_t niov) { /* Note that if __writev is an implementation that calls malloc, this will cause linking problems building the dynamic linker. */ + ssize_t r; #if RTLD_PRIVATE_ERRNO /* We have to take this lock just to be sure we don't clobber the private errno when it's being used by another thread that cares about it. Yet we must be sure not to try calling the lock functions before the thread library is fully initialized. */ if (__glibc_unlikely (_dl_starting_up)) - __writev (fd, iov, niov); + _r = _writev (fd, iov, niov); else { __rtld_lock_lock_recursive (GL(dl_load_lock)); - __writev (fd, iov, niov); + r = __writev (fd, iov, niov); __rtld_lock_unlock_recursive (GL(dl_load_lock)); } #else - __writev (fd, iov, niov); + r = __writev (fd, iov, niov); #endif + return r == -1 ? -errno : r; } diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c index 6f75197e96..c36bd39c13 100644 --- a/sysdeps/posix/libc_fatal.c +++ b/sysdeps/posix/libc_fatal.c @@ -16,6 +16,7 @@ License along with the GNU C Library; if not, see . */ +#include #include #include #include @@ -28,14 +29,14 @@ #include FATAL_PREPARE_INCLUDE #endif -#ifndef WRITEV_FOR_FATAL -# define WRITEV_FOR_FATAL writev_for_fatal -static bool -writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total) +static void +writev_for_fatal (int fd, const struct iovec *iov, size_t niov) { - return TEMP_FAILURE_RETRY (__writev (fd, iov, niov)) == total; + ssize_t cnt; + do + cnt = _dl_writev (fd, iov, niov); + while (cnt == -EINTR); } -#endif /* At most a substring before each conversion specification and the trailing substring (the plus one). */ @@ -108,7 +109,7 @@ __libc_message_impl (const char *fmt, ...) if (iovcnt > 0) { - WRITEV_FOR_FATAL (fd, iov, iovcnt, total); + writev_for_fatal (fd, iov, iovcnt); total = ALIGN_UP (total + sizeof (struct abort_msg_s) + 1, GLRO(dl_pagesize)); diff --git a/sysdeps/unix/sysv/linux/dl-writev.h b/sysdeps/unix/sysv/linux/dl-writev.h index f3fa5c777b..0abe28202f 100644 --- a/sysdeps/unix/sysv/linux/dl-writev.h +++ b/sysdeps/unix/sysv/linux/dl-writev.h @@ -19,19 +19,16 @@ #include #include -/* This is used from only one place: dl-misc.c:_dl_debug_vdprintf. - Hence it's in a header with the expectation it will be inlined. - - This is writev, but with a constraint added and others loosened: +/* This is writev, but with a constraint added and others loosened: 1. Under RTLD_PRIVATE_ERRNO, it must not clobber the private errno when another thread holds the dl_load_lock. - 2. It is not obliged to detect and report errors at all. - 3. It's not really obliged to deliver a single atomic write + 2. It's not really obliged to deliver a single atomic write (though it may be preferable). */ -static inline void +static inline ssize_t _dl_writev (int fd, const struct iovec *iov, size_t niov) { - INTERNAL_SYSCALL_CALL (writev, fd, iov, niov); + int r = INTERNAL_SYSCALL_CALL (writev, fd, iov, niov); + return INTERNAL_SYSCALL_ERROR_P (r) ? -INTERNAL_SYSCALL_ERRNO (r) : r; } diff --git a/sysdeps/unix/sysv/linux/libc_fatal.c b/sysdeps/unix/sysv/linux/libc_fatal.c deleted file mode 100644 index 72d7328a20..0000000000 --- a/sysdeps/unix/sysv/linux/libc_fatal.c +++ /dev/null @@ -1,36 +0,0 @@ -/* Catastrophic failure reports. Linux version. - Copyright (C) 1993-2025 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, see - . */ - -#include -#include -#include -#include - -static bool -writev_for_fatal (int fd, const struct iovec *iov, size_t niov, size_t total) -{ - ssize_t cnt; - do - cnt = INTERNAL_SYSCALL_CALL (writev, fd, iov, niov); - while (INTERNAL_SYSCALL_ERROR_P (cnt) - && INTERNAL_SYSCALL_ERRNO (cnt) == EINTR); - return cnt == total; -} -#define WRITEV_FOR_FATAL writev_for_fatal - -#include From patchwork Tue Sep 2 19:26:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119399 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4DAFA3858C53 for ; Tue, 2 Sep 2025 19:34:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4DAFA3858C53 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=qYgTWUwG X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 443833858413 for ; Tue, 2 Sep 2025 19:27:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 443833858413 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 443833858413 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::430 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841276; cv=none; b=rtT63cXxfNSNZih4sAiVbdgvy9fdrhbrt8tmFsJXYr02gB1eqFHzY6ssd9xt3zTIuIk1+4m99kZ9fkgO8ClhUFkQOVCudf5JKbQYGJwkPuXyfmPAbmQ1adcEIorNEJC/rMuRZHwjjoPmP1NrmsGGM52Rw3BN5wReQ+r+LmLQ9vQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841276; c=relaxed/simple; bh=B4NGPhMuT8Y/OS1eyr/SzMVgrtlr/h95poOIzSqM54w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=qsbpBdpcZ16MFNDdEVOXahEl8H6olG5l1hKnLpmS4bsWrPL1qYCHymP56xwtOGszoe25zxyR/6Ec8MQPInP3v4gb0aEAGkg7zIO5bR+6fCNK2v+QXpN5XNZbCqHFExx6wpwpGj3va4rv3SObO5oYAuhv7CObQfd8ujVjvUFMHa4= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 443833858413 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-76e4fc419a9so5628916b3a.0 for ; Tue, 02 Sep 2025 12:27:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756841275; x=1757446075; darn=sourceware.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=8G7B1kveEzSqe65K3mCPuGVihdLD70feIAWVTNaQfUc=; b=qYgTWUwGJWcS/3z43iBP3iz2hyheicnJDprN0dL6OfxAM9gIiFEOUX3UeSsbEt5XPn AEX4KWhfCsPPCA4b1lSH02PiVnrm8zguxDejtg9VCmV1OYR7KlSEKMCkLUBL6rXLzjE2 99qunhLzDIIvuRmy6UQbv03T6omZv6pVInS5nzE9R0WdDQqMo5DdhevR7nJxPLrFXB3n IrewBtnGs9Xgs1d3gRkIgQe81QffyrYSW6PpE1ZR3cfa5pynOkaySWPhI56e6S9dT90H VdJX+yLZ0gRA+pFBq5RyHJtwSOWCMrUy5EwCugviYj1FQv9QVV67dpTKdAT1Qymxe0e5 yVGg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756841275; x=1757446075; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=8G7B1kveEzSqe65K3mCPuGVihdLD70feIAWVTNaQfUc=; b=NKnvm4Dbz4GamF5fHSc/8pRWkabNNqOhCh0dq3gJKfTGcnE8DFs+bTSYgmLKm7tVEZ xKojkseEqFgPoSGpt6F8EdMFplq5TvLFirEOOlatSaaAqOX8VYO+bx2dIlamkuOnQHKV 3BW5RbOLpke8mrO63YJTh0qa7JQMFnRruznXmzP0Xv3VxGQHvhAA2CG4vW7RivArNeiM PeBxn0jwO8If9PmaExU64L6sf4k2Fhi8M1grNd7bk8kIRCrErSonP7uqZsPSkDP4NuZz NGE5JaYUgyJ0ku1YRjJceMqqFDR/T2Ck2mbKylZuHp9SPwMaA6rkZhc9JD6OSiXbQ0Tg GABQ== X-Gm-Message-State: AOJu0Ywn++6iEjr7gGRjh9PoJUBUvVI76qotKvTqFHvPFXDiK0GyfKIW LL3EWym2+S7ZsqAklgAhMb2tcJrzxUzUnnPPm3nL3hh0FInY5kAPhWO0SnhvfXs/1vW9h000ZQr C5fZw X-Gm-Gg: ASbGncuBBYvbBZKR3s4EFjIP93rhEFbUtcsWaz8NcT3VSCEguir8dM5dfK04SA26St4 NCSQsysptSbv8RHvHHPMtK/zlfT7urcOh1WrUcx1L6C5N3HqSFWeKxY8O1Q7QjlDmAzbGWZfq2G VkinRX2jEJmMefOrA4kr03lYkjQMUQl0IJZInxzlt/Rce2y3828nRm2Pckz1t6nlktj7Eqml11G BUxO1fIhNIO88Igw7e0bWjIummjMPXRtoQqRewIePpe/ztcM5yw5xR/mRA96G1799DHJJ90pEaf ZBJeyo9FtmRKQviY/aMwPv7njbubXYqGKzqkIdXpxZzEHKB2O0+7E5uIj6HJJYMTLcu+QNOPzB2 cYxpCR8JwNNQgSjvfvNYY4AQbDanXT0UxTYIEu8vsqCBH X-Google-Smtp-Source: AGHT+IEHs5LOm0ie/nEAtuBZ/v4sMBt0s/9cqUcHviOxF4pj4Qy1Zux0NnlyCCfh/BzUaYiZufFeaQ== X-Received: by 2002:a05:6a20:7fa0:b0:243:9845:4137 with SMTP id adf61e73a8af0-243d6e5b2edmr18513347637.26.1756841274406; Tue, 02 Sep 2025 12:27:54 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:b25:e661:f380:50c7:e6aa]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-772396d2dbcsm12079458b3a.50.2025.09.02.12.27.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 12:27:54 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Florian Weimer Subject: [PATCH v2 4/5] Fix assert during static startup Date: Tue, 2 Sep 2025 16:26:56 -0300 Message-ID: <20250902192743.2773679-5-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> References: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The BZ#33326 testcase triggers an assertion during process startup, which results in a segmentation fault instead of an error message and process termination with a SIGABRT. The assert issues __libc_message_impl, which in turn might call string functions depending on the ABI (strchrnul, strlen, memcpy/mempcpy), system calls (writev and mmap), and finally the abort call. Since each function may be called during process startup, before self-relocation and/or the thread pointer being set up, the functions should be built without stack protection. The dl-symbol-redir-ifunc.h is also expanded to cover strlen/memcpy/mempcpy/strchrnul on multiple architectures that implement ifunc. On i386, syscalls should not use the vDSO ("call *%gs:SYSINFO_OFFSET"), so a raise_nostatus.c and mmap call is added that disables I386_USE_SYSENTER. The s390x requires to not call libgcc for the generic strchrnul-c (through ctz/clz macros) due a libgcc issue where the __clzdi2 builtin is not built against a hidden reference to __clz_tab, which creates a GOT reference for static-pie (and it can not be called before self-relocation). Creating a test case is challenging. For static-pie, the assert is only called for ill-formatted ELF files on elf_get_dynamic_info and by some targets on ELF_DYNAMIC_RELOCATE (although not all targets use assert in their dl-machine.h). Some targets also issue __libc_fatal on ARCH_SETUP_IREL, but also only for ill-formatted ELF files. The test employs a different strategy and overrides the __tunables_init symbol, which is invoked immediately before self-relocation and TLS setup. The test is built with -Wl,-z,muldefs to avoid linker issues. I checked on aarch64, x86_64, i686, s390x (qemu), sparc (qemu), mips64el (qemu), armhf, riscv, and powerpc. --- assert/Makefile | 5 +++ elf/Makefile | 16 ++++++++ elf/tst-assert-startup-static.c | 35 +++++++++++++++++ libio/Makefile | 5 +++ signal/Makefile | 10 +++++ stdlib/Makefile | 5 +++ string/Makefile | 2 + .../aarch64/multiarch/dl-symbol-redir-ifunc.h | 2 + sysdeps/aarch64/multiarch/memcpy_generic.S | 4 ++ sysdeps/aarch64/multiarch/strlen_generic.S | 4 ++ sysdeps/generic/dl-mmap.h | 34 +++++++++++++++++ .../lp64/multiarch/dl-symbol-redir-ifunc.h | 3 ++ sysdeps/posix/libc_fatal.c | 12 ++++-- .../powerpc32/power4/multiarch/Makefile | 5 +++ .../be/multiarch/dl-symbol-redir-ifunc.h | 27 +++++++++++++ .../le/multiarch/dl-symbol-redir-ifunc.h | 1 + sysdeps/powerpc/powerpc64/multiarch/Makefile | 1 + sysdeps/s390/Makefile | 5 +++ .../s390/multiarch/dl-symbol-redir-ifunc.h | 4 ++ sysdeps/s390/string-bitops.h | 27 +++++++++++++ .../sparcv9/multiarch/dl-symbol-redir-ifunc.h | 3 ++ .../sparc64/multiarch/dl-symbol-redir-ifunc.h | 3 ++ sysdeps/unix/sysv/linux/Makefile | 5 +++ sysdeps/unix/sysv/linux/dl-mmap.h | 34 +++++++++++++++++ sysdeps/unix/sysv/linux/i386/dl-mmap.h | 38 +++++++++++++++++++ sysdeps/unix/sysv/linux/i386/raise_nostatus.c | 26 +++++++++++++ .../unix/sysv/linux/riscv/multiarch/Makefile | 5 +++ .../riscv/multiarch/dl-symbol-redir-ifunc.h | 26 +++++++++++++ .../x86_64/multiarch/dl-symbol-redir-ifunc.h | 33 ++++++++++++++++ 29 files changed, 377 insertions(+), 3 deletions(-) create mode 100644 elf/tst-assert-startup-static.c create mode 100644 sysdeps/generic/dl-mmap.h create mode 100644 sysdeps/powerpc/powerpc64/be/multiarch/dl-symbol-redir-ifunc.h create mode 100644 sysdeps/s390/string-bitops.h create mode 100644 sysdeps/unix/sysv/linux/dl-mmap.h create mode 100644 sysdeps/unix/sysv/linux/i386/dl-mmap.h create mode 100644 sysdeps/unix/sysv/linux/i386/raise_nostatus.c create mode 100644 sysdeps/unix/sysv/linux/riscv/multiarch/dl-symbol-redir-ifunc.h diff --git a/assert/Makefile b/assert/Makefile index 8d106d8752..a77362e685 100644 --- a/assert/Makefile +++ b/assert/Makefile @@ -33,6 +33,11 @@ routines := \ assert-perr \ # routines +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-__libc_assert_fail.o = $(no-stack-protector) +CFLAGS-__libc_assert_fail.op = $(no-stack-protector) + tests := \ test-assert \ test-assert-2 \ diff --git a/elf/Makefile b/elf/Makefile index 5a676f858d..07504e1f74 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -282,6 +282,7 @@ tests-static-normal := \ # tests-static-normal tests-static-internal := \ + tst-assert-startup-static \ tst-dl-printf-static \ tst-dl_find_object-static \ tst-env-setuid-tunables \ @@ -293,6 +294,10 @@ tests-static-internal := \ tst-tunables-enable_secure \ # tests-static-internal +tests-special += \ + $(objpfx)tst-assert-startup-static.out \ + # tests-special + CRT-tst-tls1-static-non-pie := $(csu-objpfx)crt1.o tst-tls1-static-non-pie-no-pie = yes @@ -3519,3 +3524,14 @@ $(objpfx)tst-origin.out: tst-origin.sh $(objpfx)tst-origin $(evaluate-test) $(objpfx)tst-dlopen-sgid.out: $(objpfx)tst-dlopen-sgid-mod.so + +CFLAGS-tst-assert-startup-static.c += $(no-stack-protector) +LDFLAGS-tst-assert-startup-static = -Wl,-z,muldefs + +$(objpfx)tst-assert-startup-static.out: $(objpfx)tst-assert-startup-static + $(test-program-cmd-before-env) \ + $(run-program-env) \ + $< > $@ 2>&1; echo "status: $$?" >> $@; \ + grep -q 'Fatal glibc error: tst-assert-startup-static' $@ \ + && grep -q '^status: 134$$' $@; \ + $(evaluate-test) diff --git a/elf/tst-assert-startup-static.c b/elf/tst-assert-startup-static.c new file mode 100644 index 0000000000..66f6ccfd4c --- /dev/null +++ b/elf/tst-assert-startup-static.c @@ -0,0 +1,35 @@ +/* Check if assert work during program startup. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +/* The __tunables_init is called just before self-relocation and TLS setup, + and the __libc_assert_fail is used internally for assert() calls. */ +extern _Noreturn __typeof (__assert_fail) __libc_assert_fail; + +void __tunables_init (char **env) +{ + __libc_assert_fail ("error", __FILE__, __LINE__, __func__); +} + +int main (int argc, char *argv[]) +{ + /* Fail with a different error code than abort(). */ + exit (EXIT_FAILURE); +} diff --git a/libio/Makefile b/libio/Makefile index 6ce669e103..01b2958112 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -180,6 +180,11 @@ endif CPPFLAGS += $(libio-mtsafe) +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-libc_fatal.o = $(no-stack-protector) +CFLAGS-libc_fatal.op = $(no-stack-protector) + # Support for exception handling. CFLAGS-fileops.c += -fexceptions CFLAGS-fputc.c += -fexceptions diff --git a/signal/Makefile b/signal/Makefile index 468a8b93f0..af57346e98 100644 --- a/signal/Makefile +++ b/signal/Makefile @@ -65,6 +65,11 @@ tests := \ include ../Rules +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-raise.o = $(no-stack-protector) +CFLAGS-raise.op = $(no-stack-protector) + CFLAGS-raise.c += -fasynchronous-unwind-tables CFLAGS-sigpause.c += -fexceptions CFLAGS-sigsuspend.c += -fexceptions -fasynchronous-unwind-tables @@ -74,6 +79,11 @@ CFLAGS-sigwaitinfo.c += -fexceptions -fasynchronous-unwind-tables CFLAGS-sigreturn.c += $(no-stack-protector) +# It is also called during static library initialization (during abort()/assert()), +# so turn stack-protection off for non-shared builds. +CFLAGS-raise_nostatus.o = $(no-stack-protector) +CFLAGS-raise_nostatus.op = $(no-stack-protector) + # We don't want to test the lazy resolution stack usage, just the # execution of the handler and the functions. LDFLAGS-tst-minsigstksz-1 = -Wl,-z,now diff --git a/stdlib/Makefile b/stdlib/Makefile index 25f777e1a5..f867f0ce80 100644 --- a/stdlib/Makefile +++ b/stdlib/Makefile @@ -518,6 +518,11 @@ generated += \ tst-putenvmod.so \ # generated +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-abort.o = $(no-stack-protector) +CFLAGS-abort.op = $(no-stack-protector) + CFLAGS-bsearch.c += $(uses-callbacks) CFLAGS-qsort.c += $(uses-callbacks) CFLAGS-system.c += -fexceptions diff --git a/string/Makefile b/string/Makefile index c83b195e2e..411dd03a21 100644 --- a/string/Makefile +++ b/string/Makefile @@ -278,6 +278,8 @@ CFLAGS-wordcopy.c += $(no-stack-protector) # Called during static initialization CFLAGS-strncmp.c += $(no-stack-protector) CFLAGS-memset.c += $(no-stack-protector) +CFLAGS-strlen.c += $(no-stack-protector) +CFLAGS-strchrnul.c += $(no-stack-protector) CFLAGS-argz-next.c += $(config-cflags-wno-ignored-attributes) CFLAGS-basename.c += $(config-cflags-wno-ignored-attributes) diff --git a/sysdeps/aarch64/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/aarch64/multiarch/dl-symbol-redir-ifunc.h index 63ac28f50f..4646d6a137 100644 --- a/sysdeps/aarch64/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/aarch64/multiarch/dl-symbol-redir-ifunc.h @@ -19,6 +19,8 @@ #ifndef _DL_IFUNC_GENERIC_H #define _DL_IFUNC_GENERIC_H +asm ("memcpy = __memcpy_generic"); asm ("memset = __memset_generic"); +asm ("strlen = __strlen_generic"); #endif diff --git a/sysdeps/aarch64/multiarch/memcpy_generic.S b/sysdeps/aarch64/multiarch/memcpy_generic.S index f044ebcdec..1d783199bc 100644 --- a/sysdeps/aarch64/multiarch/memcpy_generic.S +++ b/sysdeps/aarch64/multiarch/memcpy_generic.S @@ -42,3 +42,7 @@ #endif #include "../memcpy.S" + +#if IS_IN (rtld) +strong_alias (memcpy, __memcpy_generic) +#endif diff --git a/sysdeps/aarch64/multiarch/strlen_generic.S b/sysdeps/aarch64/multiarch/strlen_generic.S index 270586c451..effb98085b 100644 --- a/sysdeps/aarch64/multiarch/strlen_generic.S +++ b/sysdeps/aarch64/multiarch/strlen_generic.S @@ -40,3 +40,7 @@ #endif #include "../strlen.S" + +#if IS_IN (rtld) +strong_alias (strlen, __strlen_generic) +#endif diff --git a/sysdeps/generic/dl-mmap.h b/sysdeps/generic/dl-mmap.h new file mode 100644 index 0000000000..bed3523422 --- /dev/null +++ b/sysdeps/generic/dl-mmap.h @@ -0,0 +1,34 @@ +/* mmap wrapper for dynamic loader. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_MMAP_H +#define _DL_MMAP_H + +#include + +/* This mmap call is used to allocate some memory to backup assert() messages + before TLS setup is done (which setup the thread pointer used by some ABIs + to issues syscalls). */ + +static inline void * +_dl_mmap (void *addr, size_t len, int prot, int flags) +{ + return __mmap (addr, len, prot, flags, -1, 0); +} + +#endif diff --git a/sysdeps/loongarch/lp64/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/loongarch/lp64/multiarch/dl-symbol-redir-ifunc.h index 9b43cdda69..bc00b411b1 100644 --- a/sysdeps/loongarch/lp64/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/loongarch/lp64/multiarch/dl-symbol-redir-ifunc.h @@ -20,8 +20,11 @@ #define _DL_IFUNC_GENERIC_H #ifndef SHARED +asm ("memcpy = __memcpy_aligned"); asm ("memset = __memset_aligned"); asm ("memcmp = __memcmp_aligned"); +asm ("__strchrnul = __strchrnul_aligned"); +asm ("strlen = __strlen_aligned"); #endif #endif diff --git a/sysdeps/posix/libc_fatal.c b/sysdeps/posix/libc_fatal.c index c36bd39c13..875bf9ecf9 100644 --- a/sysdeps/posix/libc_fatal.c +++ b/sysdeps/posix/libc_fatal.c @@ -16,7 +16,12 @@ License along with the GNU C Library; if not, see . */ +/* Mark symbols hidden in static PIE for early self relocation to work. */ +#if BUILD_PIE_DEFAULT +# pragma GCC visibility push(hidden) +#endif #include +#include #include #include #include @@ -24,6 +29,7 @@ #include #include #include +#include #ifdef FATAL_PREPARE_INCLUDE #include FATAL_PREPARE_INCLUDE @@ -113,9 +119,9 @@ __libc_message_impl (const char *fmt, ...) total = ALIGN_UP (total + sizeof (struct abort_msg_s) + 1, GLRO(dl_pagesize)); - struct abort_msg_s *buf = __mmap (NULL, total, - PROT_READ | PROT_WRITE, - MAP_ANON | MAP_PRIVATE, -1, 0); + struct abort_msg_s *buf = _dl_mmap (NULL, total, + PROT_READ | PROT_WRITE, + MAP_ANON | MAP_PRIVATE); if (__glibc_likely (buf != MAP_FAILED)) { buf->size = total; diff --git a/sysdeps/powerpc/powerpc32/power4/multiarch/Makefile b/sysdeps/powerpc/powerpc32/power4/multiarch/Makefile index 3a49b855ca..60ba2e50d2 100644 --- a/sysdeps/powerpc/powerpc32/power4/multiarch/Makefile +++ b/sysdeps/powerpc/powerpc32/power4/multiarch/Makefile @@ -11,4 +11,9 @@ sysdep_routines += memcpy-power7 memcpy-a2 memcpy-power6 memcpy-cell \ strchr-power7 strchr-ppc32 \ wordcopy-power7 wordcopy-ppc32 \ memmove-power7 memmove-ppc + +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-strchrnul-ppc32.o = $(no-stack-protector) +CFLAGS-strchrnul-ppc32.op = $(no-stack-protector) endif diff --git a/sysdeps/powerpc/powerpc64/be/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/powerpc/powerpc64/be/multiarch/dl-symbol-redir-ifunc.h new file mode 100644 index 0000000000..9a30f21ecd --- /dev/null +++ b/sysdeps/powerpc/powerpc64/be/multiarch/dl-symbol-redir-ifunc.h @@ -0,0 +1,27 @@ +/* Symbol rediretion for loader/static initialization code. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_IFUNC_GENERIC_H +#define _DL_IFUNC_GENERIC_H + +#ifndef SHARED +asm ("__mempcpy = __mempcpy_ppc"); +asm ("__strchrnul = __strchrnul_ppc"); +#endif + +#endif diff --git a/sysdeps/powerpc/powerpc64/le/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/powerpc/powerpc64/le/multiarch/dl-symbol-redir-ifunc.h index 93276e1578..b1d237eadc 100644 --- a/sysdeps/powerpc/powerpc64/le/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/powerpc/powerpc64/le/multiarch/dl-symbol-redir-ifunc.h @@ -21,5 +21,6 @@ asm ("memset = __memset_power8"); asm ("__mempcpy = __mempcpy_power7"); +asm ("__strchrnul = __strchrnul_power8"); #endif diff --git a/sysdeps/powerpc/powerpc64/multiarch/Makefile b/sysdeps/powerpc/powerpc64/multiarch/Makefile index e321ce54e0..6408176ebc 100644 --- a/sysdeps/powerpc/powerpc64/multiarch/Makefile +++ b/sysdeps/powerpc/powerpc64/multiarch/Makefile @@ -40,3 +40,4 @@ endif # Called during static initialization CFLAGS-strncmp-ppc64.c += $(no-stack-protector) +CFLAGS-strchrnul-ppc64.c += $(no-stack-protector) diff --git a/sysdeps/s390/Makefile b/sysdeps/s390/Makefile index 985b4f25ee..078c0d165d 100644 --- a/sysdeps/s390/Makefile +++ b/sysdeps/s390/Makefile @@ -104,6 +104,11 @@ routines_no_fortify += \ # routines_no_fortify endif +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-strchrnul-c.o = $(no-stack-protector) +CFLAGS-strchrnul-c.op = $(no-stack-protector) + ifeq ($(subdir),wcsmbs) sysdep_routines += wcslen wcslen-vx wcslen-c \ wcsnlen wcsnlen-vx wcsnlen-c \ diff --git a/sysdeps/s390/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/s390/multiarch/dl-symbol-redir-ifunc.h index c34907c20c..e921ca6e6b 100644 --- a/sysdeps/s390/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/s390/multiarch/dl-symbol-redir-ifunc.h @@ -21,11 +21,15 @@ #include #include +#include #define IFUNC_SYMBOL_STR1(s) #s #define IFUNC_SYMBOL_STR(s) IFUNC_SYMBOL_STR1(s) +#ifndef SHARED asm ("memset = " IFUNC_SYMBOL_STR(MEMSET_DEFAULT)); asm ("memcmp = " IFUNC_SYMBOL_STR(MEMCMP_DEFAULT)); +asm ("__strchrnul = " IFUNC_SYMBOL_STR(STRCHRNUL_DEFAULT)); +#endif #endif diff --git a/sysdeps/s390/string-bitops.h b/sysdeps/s390/string-bitops.h new file mode 100644 index 0000000000..408d18c7a7 --- /dev/null +++ b/sysdeps/s390/string-bitops.h @@ -0,0 +1,27 @@ +/* Zero byte detection, define whether to use stdbit.h + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* s390x support static-pie and the libgcc implementation for + __builtin_clzl/__builtin_ctzl might access extern data that is not marked + as hidden, which creates additiona GOT access that is used before + self-relocation. */ +#if __ARCH__ > 6 +# define HAVE_BITOPTS_WORKING 1 +#else +# define HAVE_BITOPTS_WORKING 0 +#endif diff --git a/sysdeps/sparc/sparc32/sparcv9/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/sparc/sparc32/sparcv9/multiarch/dl-symbol-redir-ifunc.h index 12ff2a03fa..9cd78de2bf 100644 --- a/sysdeps/sparc/sparc32/sparcv9/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/sparc/sparc32/sparcv9/multiarch/dl-symbol-redir-ifunc.h @@ -19,6 +19,9 @@ #ifndef _DL_IFUNC_GENERIC_H #define _DL_IFUNC_GENERIC_H +#ifndef SHARED asm ("memset = __memset_ultra1"); +asm ("memcpy = __memcpy_ultra1"); +#endif #endif diff --git a/sysdeps/sparc/sparc64/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/sparc/sparc64/multiarch/dl-symbol-redir-ifunc.h index 12ff2a03fa..9cd78de2bf 100644 --- a/sysdeps/sparc/sparc64/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/sparc/sparc64/multiarch/dl-symbol-redir-ifunc.h @@ -19,6 +19,9 @@ #ifndef _DL_IFUNC_GENERIC_H #define _DL_IFUNC_GENERIC_H +#ifndef SHARED asm ("memset = __memset_ultra1"); +asm ("memcpy = __memcpy_ultra1"); +#endif #endif diff --git a/sysdeps/unix/sysv/linux/Makefile b/sysdeps/unix/sysv/linux/Makefile index 2c5bf42236..bb74b4aca8 100644 --- a/sysdeps/unix/sysv/linux/Makefile +++ b/sysdeps/unix/sysv/linux/Makefile @@ -114,6 +114,11 @@ sysdep_routines += \ xstat \ xstat64 \ # sysdep_routines +# +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-setvmaname.o = $(no-stack-protector) +CFLAGS-setvmaname.op = $(no-stack-protector) CFLAGS-gethostid.c = -fexceptions CFLAGS-tee.c = -fexceptions -fasynchronous-unwind-tables diff --git a/sysdeps/unix/sysv/linux/dl-mmap.h b/sysdeps/unix/sysv/linux/dl-mmap.h new file mode 100644 index 0000000000..bed3523422 --- /dev/null +++ b/sysdeps/unix/sysv/linux/dl-mmap.h @@ -0,0 +1,34 @@ +/* mmap wrapper for dynamic loader. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_MMAP_H +#define _DL_MMAP_H + +#include + +/* This mmap call is used to allocate some memory to backup assert() messages + before TLS setup is done (which setup the thread pointer used by some ABIs + to issues syscalls). */ + +static inline void * +_dl_mmap (void *addr, size_t len, int prot, int flags) +{ + return __mmap (addr, len, prot, flags, -1, 0); +} + +#endif diff --git a/sysdeps/unix/sysv/linux/i386/dl-mmap.h b/sysdeps/unix/sysv/linux/i386/dl-mmap.h new file mode 100644 index 0000000000..b036f32864 --- /dev/null +++ b/sysdeps/unix/sysv/linux/i386/dl-mmap.h @@ -0,0 +1,38 @@ +/* mmap wrapper for dynamic loader. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_MMAP_H +#define _DL_MMAP_H + +#include +#include + +/* This mmap call is used to allocate some memory to backup assert() messages + before TLS setup is done, so it can not use "call *%gs:SYSINFO_OFFSET" + during startup in static PIE. */ +#if BUILD_PIE_DEFAULT +# define I386_USE_SYSENTER 0 +#endif + +static inline void * +_dl_mmap (void *addr, size_t len, int prot, int flags) +{ + return (void *) MMAP_CALL (mmap2, addr, len, prot, flags, -1, 0); +} + +#endif diff --git a/sysdeps/unix/sysv/linux/i386/raise_nostatus.c b/sysdeps/unix/sysv/linux/i386/raise_nostatus.c new file mode 100644 index 0000000000..d4099b273b --- /dev/null +++ b/sysdeps/unix/sysv/linux/i386/raise_nostatus.c @@ -0,0 +1,26 @@ +/* Send a signal to a specific pthread. Stub version. + Copyright (C) 2014-2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +/* This is called from abort() (issued by ssert()) before TLS setup is done, + so it can not use "call *%gs:SYSINFO_OFFSET" during startup in static + PIE. */ +#if BUILD_PIE_DEFAULT +# define I386_USE_SYSENTER 0 +#endif + +#include diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile index fcef5659d4..c7d19c18c7 100644 --- a/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/Makefile @@ -5,5 +5,10 @@ sysdep_routines += \ memcpy_noalignment \ # sysdep_routines +# Called during static library initialization, so turn stack-protection +# off for non-shared builds. +CFLAGS-memcpy-generic.o = $(no-stack-protector) +CFLAGS-memcpy-generic.op = $(no-stack-protector) + CFLAGS-memcpy_noalignment.c += -mno-strict-align endif diff --git a/sysdeps/unix/sysv/linux/riscv/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/unix/sysv/linux/riscv/multiarch/dl-symbol-redir-ifunc.h new file mode 100644 index 0000000000..ae38926a5c --- /dev/null +++ b/sysdeps/unix/sysv/linux/riscv/multiarch/dl-symbol-redir-ifunc.h @@ -0,0 +1,26 @@ +/* Symbol rediretion for loader/static initialization code. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#ifndef _DL_IFUNC_GENERIC_H +#define _DL_IFUNC_GENERIC_H + +#ifndef SHARED +asm ("memcpy = __memcpy_generic"); +#endif + +#endif diff --git a/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h b/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h index 163b25d096..a31f91c329 100644 --- a/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h +++ b/sysdeps/x86_64/multiarch/dl-symbol-redir-ifunc.h @@ -44,6 +44,39 @@ asm ("memset = " HAVE_MEMSET_IFUNC_GENERIC); asm ("memcmp = " HAVE_MEMCMP_IFUNC_GENERIC); + +#if MINIMUM_X86_ISA_LEVEL >= 4 +# define HAVE_MEMPCPY_IFUNC_GENERIC "__mempcpy_evex_unaligned" +#elif MINIMUM_X86_ISA_LEVEL == 3 +# define HAVE_MEMPCPY_IFUNC_GENERIC "__mempcpy_avx2_unaligned" +#else +# define HAVE_MEMPCPY_IFUNC_GENERIC "__mempcpy_sse2_unaligned" +#endif + +asm ("__mempcpy = " HAVE_MEMPCPY_IFUNC_GENERIC); + + +#if MINIMUM_X86_ISA_LEVEL >= 4 +# define HAVE_STRCHRNUL_IFUNC_GENERIC "__strchrnul_evex" +#elif MINIMUM_X86_ISA_LEVEL == 3 +# define HAVE_STRCHRNUL_IFUNC_GENERIC "__strchrnul_avx2" +#else +# define HAVE_STRCHRNUL_IFUNC_GENERIC "__strchrnul_sse2" +#endif + +asm ("__strchrnul = " HAVE_STRCHRNUL_IFUNC_GENERIC); + + +#if MINIMUM_X86_ISA_LEVEL >= 4 +# define HAVE_STRLEN_IFUNC_GENERIC "__strlen_evex" +#elif MINIMUM_X86_ISA_LEVEL == 3 +# define HAVE_STRLEN_IFUNC_GENERIC "__strlen_avx2" +#else +# define HAVE_STRLEN_IFUNC_GENERIC "__strlen_sse2" +#endif + +asm ("strlen = " HAVE_STRLEN_IFUNC_GENERIC); + #endif /* SHARED */ #endif From patchwork Tue Sep 2 19:26:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 119397 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2245F3858D39 for ; Tue, 2 Sep 2025 19:30:53 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 2245F3858D39 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=hdCPCfVW X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from mail-pf1-x430.google.com (mail-pf1-x430.google.com [IPv6:2607:f8b0:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id F14E73858C50 for ; Tue, 2 Sep 2025 19:27:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F14E73858C50 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F14E73858C50 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::430 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841278; cv=none; b=LriIAR02mkcGNyGe9fK8povMnKb190cmzmp2Vmg1Y4lWgP6KIQHOM0Mm/LDv61z1O7d0qBMK35VpqCyX/0R1pC2h9Hqh+uClndTUaV0HJN0QZdiaX518PGlPnGVfx7qMM7pjtJ8DVXOhe7+y4GUfghXecJHLpqvoMzPgvjiZ2yI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1756841278; c=relaxed/simple; bh=ribOrgQfkoJ0k0Nnx7b+tK4PLPNu5hps5voMr4xBGT8=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=WcF4HloXhw/Tx8MBucEWWSY7fVj3B57AdNq77KXRlzzrz0zDIfphPUbvSZPjVZMiD4SgoCoGAYHqo1djiPcAFWXYZs+DD1Lgemy8fDhZb01v4CK1kZERaV+CDCXCS54dUAiU2gCwq/YWjjjeluk04U2kxrVwUf6GfzdQfLd829Y= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F14E73858C50 Received: by mail-pf1-x430.google.com with SMTP id d2e1a72fcca58-7704f3c46ceso4859493b3a.2 for ; Tue, 02 Sep 2025 12:27:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1756841276; x=1757446076; darn=sourceware.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=eJkQuFLoMW+pMVUafy3YONUj2/YBXWLV1hFkzAxNX4g=; b=hdCPCfVWQyHDvg7miRopNrdYX0KoI7QqaD8/KqENFRBfZnE91gWA3rPDznRUbMGUad 8+ibzG9EYkVkXYtR6kVwFa+IQwbJUzpcEdbGxlwKdZf2CaHfKaMlJbEvROXJhmBj/3AV fkkf9lcqLU3HkCzmrgkdR5pvf0uiz3HvVdf1iV5znV3WuI/9vk3jdXAPoaO/PTpQj0L0 McLzrfuZ5dxwRA5qViuqHnorUd4yipvWzClFWpUev3ISc0mTq+vd6SPLT3dlMFLNKGOK CDniGTkpW213c7cND6JOjQPFZ7R2BenwwThgjHnUYvwAD9FQJRjmB2ok84xLD602G/zi tRkA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756841276; x=1757446076; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=eJkQuFLoMW+pMVUafy3YONUj2/YBXWLV1hFkzAxNX4g=; b=RlkwF0PMaKUzG4TSv8iQ+n+8DMypqRzSYuJYWlbREyDeoTrpf7P+iGJ7ZcfMZj9umM O5fTP2Kfxkmd1wED/6AJwbYFp2qIGya1MPM4ofMUsFJhhGMBd0s7lwi85SjBSCEXVIfJ d5DmUonrH8Rjzm0vvj3L/z0zhknYeYgY4uDY/JJTb1rZTgldToFQlPCezFqOgGkIVTrA yXJUn7CM6/LeD/YyYe56vvU84NdVGjjt89dr0b5KPEdSlASD8BRbA8cOPeDRggNE6cl2 wwQ51XV4rgQazyQZ/2EOZURRfo+ZukKoG7tM1sXOCmcu3d4CDC4bVhr4hb5Ah31l0e5H dg+w== X-Gm-Message-State: AOJu0YyvalTchinsfTEcg49atjRLLkzd9+8nyv2QPQQZ81OxcC6O6VAY xsaT1pgUE82aOZqnw+xWx2lpwU17Wq8iH2d80OGJvmYyMza7YEcfwsi+Y1OCSlJc60lUbS2p1fR f0oDF X-Gm-Gg: ASbGncuUun9FddUNKAnHFAXidNICrjFZorHpCQmwPtR3xJlOnMRJkfzIW/3pcU3uI2V JLo7w9ZQrZr5zYoWXp05voBMeA9O9f934B/IStYywT2UtnEwrZtBPfWWK6MPJsFTKvZcJYrv7uh OqU+Kmqutyjzla543RJ5wxr/qvCLHms9OsbDmiYAoYh/lve/pEjkwKyBzt+NVJNPmGuE1jYTB+1 20YXRWAmWKdrHlfeOkMA/v5LsVxuW6UrBxosp1/iQ2yyWlOyD40ZnRd0slklRcp0XMrxIQBQ9je epv1PD6l38hrjmpooO/LYylvs7vOBjWT/Iu3gxO4bGrUigOQ73VjoeeuILzgibtt724mlgwN8QX DRTbEqvrL10W5qf/WaEq6FLeaqn5aEJozozL7yp0zQ8I3 X-Google-Smtp-Source: AGHT+IFviECRwYC+MVKhdyApmmd6BBgq5KzlZRdR3zjaXMIzaFIwWN2+ay6M3NblAvVEtqmruZQQWA== X-Received: by 2002:a05:6a21:99aa:b0:246:f1:bec3 with SMTP id adf61e73a8af0-24600f1c221mr2996944637.42.1756841276514; Tue, 02 Sep 2025 12:27:56 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c1:b25:e661:f380:50c7:e6aa]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-772396d2dbcsm12079458b3a.50.2025.09.02.12.27.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Sep 2025 12:27:55 -0700 (PDT) From: Adhemerval Zanella To: libc-alpha@sourceware.org Cc: Wilco Dijkstra , Florian Weimer Subject: [PATCH v2 5/5] elf: Allow RPATH/RUNPATH for static-pie Date: Tue, 2 Sep 2025 16:26:57 -0300 Message-ID: <20250902192743.2773679-6-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> References: <20250902192743.2773679-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org The initial static-pie support (commit 9d7a3741c9e59eba87fb) reused ld.so ELF parsing logic, even though RPATH/RUNPATH should not appear in the static-pie bootstrap. With static-pie, RPATH/RUNPATH on the loader typically indicates toolchain misconfiguration. However, for static-pie, the presence of RPATH/RUNPATH has no impact because these inaries do not use dynamic linking at runtime. Static binaries do not support rpath because they lack dynamic sections, and adding static-pie support affects only the dlopen function. If static dlopen support is removed, this change has no effect. This change also simplifies elf_get_dynamic_info and removes a difference between dynamic and static binaries. Tested on aarch64-linux-gnu and x86_64-linux-gnu. --- elf/Makefile | 8 ++++++ elf/dl-load.c | 2 +- elf/dl-reloc-static-pie.c | 2 +- elf/get-dynamic-info.h | 5 ++-- elf/rtld.c | 4 +-- elf/setup-vdso.h | 2 +- elf/tst-pie-rpath-mod.c | 19 ++++++++++++++ elf/tst-pie-rpath-static.c | 53 ++++++++++++++++++++++++++++++++++++++ 8 files changed, 87 insertions(+), 8 deletions(-) create mode 100644 elf/tst-pie-rpath-mod.c create mode 100644 elf/tst-pie-rpath-static.c diff --git a/elf/Makefile b/elf/Makefile index 07504e1f74..eb7f3f016b 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -1201,7 +1201,11 @@ tests += \ # tests tests-static += \ tst-pie-address-static \ + tst-pie-rpath-static \ # tests-static +modules-names += \ + tst-pie-rpath-mod \ + # modules-names ifeq (yes,$(aligned-65536)) tests += tst-pie-bss-static tests-static += tst-pie-bss-static @@ -3535,3 +3539,7 @@ $(objpfx)tst-assert-startup-static.out: $(objpfx)tst-assert-startup-static grep -q 'Fatal glibc error: tst-assert-startup-static' $@ \ && grep -q '^status: 134$$' $@; \ $(evaluate-test) + +LDFLAGS-tst-pie-rpath-static += -Wl,-rpath,\$$ORIGIN/tst-pie-rpath-static-subdir +CFLAGS-tst-pie-rpath-static.c += -DPFX=\"$(objpfx)\" +$(objpfx)tst-pie-rpath-static.out: $(objpfx)tst-pie-rpath-mod.so diff --git a/elf/dl-load.c b/elf/dl-load.c index 891b44966c..abe05e48fb 100644 --- a/elf/dl-load.c +++ b/elf/dl-load.c @@ -1269,7 +1269,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd, if (l->l_ld != NULL) l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l, false, false); + elf_get_dynamic_info (l, false); /* Make sure we are not dlopen'ing an object that has the DF_1_NOOPEN flag set, or a PIE object. */ diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c index 758bf9893e..f19ab7e5cd 100644 --- a/elf/dl-reloc-static-pie.c +++ b/elf/dl-reloc-static-pie.c @@ -69,7 +69,7 @@ _dl_relocate_static_pie (void) /* Read our own dynamic section and fill in the info array. */ main_map->l_ld = ((void *) l_addr + elf_machine_dynamic ()); - elf_get_dynamic_info (main_map, false, true); + elf_get_dynamic_info (main_map, false); # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC ELF_MACHINE_BEFORE_RTLD_RELOC (main_map, main_map->l_info); diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h index d3d830e86c..2d9cd41641 100644 --- a/elf/get-dynamic-info.h +++ b/elf/get-dynamic-info.h @@ -26,8 +26,7 @@ #include static inline void __attribute__ ((unused, always_inline)) -elf_get_dynamic_info (struct link_map *l, bool bootstrap, - bool static_pie_bootstrap) +elf_get_dynamic_info (struct link_map *l, bool bootstrap) { #if __ELF_NATIVE_CLASS == 32 typedef Elf32_Word d_tag_utype; @@ -128,7 +127,7 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap, #endif if (info[DT_RELR] != NULL) assert (info[DT_RELRENT]->d_un.d_val == sizeof (ElfW(Relr))); - if (bootstrap || static_pie_bootstrap) + if (bootstrap) { assert (info[DT_RUNPATH] == NULL); assert (info[DT_RPATH] == NULL); diff --git a/elf/rtld.c b/elf/rtld.c index 753ce6690b..1a6b986c3b 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -548,7 +548,7 @@ _dl_start (void *arg) /* Read our own dynamic section and fill in the info array. */ bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic (); bootstrap_map.l_ld_readonly = DL_RO_DYN_SECTION; - elf_get_dynamic_info (&bootstrap_map, true, false); + elf_get_dynamic_info (&bootstrap_map, true); #if NO_TLS_OFFSET != 0 bootstrap_map.l_tls_offset = NO_TLS_OFFSET; @@ -1702,7 +1702,7 @@ dl_main (const ElfW(Phdr) *phdr, if (! rtld_is_main) { /* Extract the contents of the dynamic section for easy access. */ - elf_get_dynamic_info (main_map, false, false); + elf_get_dynamic_info (main_map, false); /* If the main map is libc.so, update the base namespace to refer to this map. If libc.so is loaded later, this happens diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h index 935d9e3baf..420ce70d15 100644 --- a/elf/setup-vdso.h +++ b/elf/setup-vdso.h @@ -60,7 +60,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)), l->l_addr = l->l_map_start - l->l_addr; l->l_map_end += l->l_addr; l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr); - elf_get_dynamic_info (l, false, false); + elf_get_dynamic_info (l, false); _dl_setup_hash (l); l->l_relocated = 1; diff --git a/elf/tst-pie-rpath-mod.c b/elf/tst-pie-rpath-mod.c new file mode 100644 index 0000000000..40e3b78d6a --- /dev/null +++ b/elf/tst-pie-rpath-mod.c @@ -0,0 +1,19 @@ +/* Check if RPATH/RUNPATH is allowed for static-pie. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +int foo (void) { return 42; } diff --git a/elf/tst-pie-rpath-static.c b/elf/tst-pie-rpath-static.c new file mode 100644 index 0000000000..e02ca47364 --- /dev/null +++ b/elf/tst-pie-rpath-static.c @@ -0,0 +1,53 @@ +/* Check if RPATH/RUNPATH is allowed for static-pie. + Copyright (C) 2025 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include +#include + +#define LIBNAME "tst-pie-rpath-mod.so" +#define TESTSUBDIR PFX "tst-pie-rpath-static-subdir" +#define LIBPATH TESTSUBDIR "/" LIBNAME + +static void +cleanup (void) +{ + xunlink (LIBPATH); + rmdir (TESTSUBDIR); +} + +static int +do_test (void) +{ + atexit (cleanup); + + xmkdir (TESTSUBDIR, 0777); + + support_copy_file (PFX "/" LIBNAME, LIBPATH); + + void *h = xdlopen (LIBNAME, RTLD_NOW); + int (*foo)(void) = xdlsym (h, "foo"); + TEST_COMPARE (foo (), 42); + xdlclose (h); + + return 0; +} + +#include