From patchwork Tue Aug 13 10:04:31 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Yann E. MORIN" X-Patchwork-Id: 1971872 Return-Path: X-Original-To: incoming-buildroot@patchwork.ozlabs.org Delivered-To: patchwork-incoming-buildroot@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=buildroot.org (client-ip=2605:bc80:3010::138; helo=smtp1.osuosl.org; envelope-from=buildroot-bounces@buildroot.org; receiver=patchwork.ozlabs.org) Received: from smtp1.osuosl.org (smtp1.osuosl.org [IPv6:2605:bc80:3010::138]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Wjn776vlBz1yYl for ; Tue, 13 Aug 2024 20:04:51 +1000 (AEST) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id 2707280C62; Tue, 13 Aug 2024 10:04:50 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id Z3e7ASVhgrMp; Tue, 13 Aug 2024 10:04:48 +0000 (UTC) X-Comment: SPF check N/A for local connections - client-ip=140.211.166.34; helo=ash.osuosl.org; envelope-from=buildroot-bounces@buildroot.org; receiver= DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 323B680E3D Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by smtp1.osuosl.org (Postfix) with ESMTP id 323B680E3D; Tue, 13 Aug 2024 10:04:48 +0000 (UTC) X-Original-To: buildroot@lists.busybox.net Delivered-To: buildroot@osuosl.org Received: from smtp1.osuosl.org (smtp1.osuosl.org [140.211.166.138]) by ash.osuosl.org (Postfix) with ESMTP id C976C1BF35F for ; Tue, 13 Aug 2024 10:04:40 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by smtp1.osuosl.org (Postfix) with ESMTP id B268380C6C for ; Tue, 13 Aug 2024 10:04:40 +0000 (UTC) X-Virus-Scanned: amavis at osuosl.org Received: from smtp1.osuosl.org ([127.0.0.1]) by localhost (smtp1.osuosl.org [127.0.0.1]) (amavis, port 10024) with ESMTP id w69tNFuoVLD0 for ; Tue, 13 Aug 2024 10:04:39 +0000 (UTC) Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=80.12.210.123; helo=smtp-out.orange.com; envelope-from=yann.morin@orange.com; receiver= DMARC-Filter: OpenDMARC Filter v1.4.2 smtp1.osuosl.org 110CD80ADC DKIM-Filter: OpenDKIM Filter v2.11.0 smtp1.osuosl.org 110CD80ADC Received: from smtp-out.orange.com (smtp-out.orange.com [80.12.210.123]) by smtp1.osuosl.org (Postfix) with ESMTPS id 110CD80ADC for ; Tue, 13 Aug 2024 10:04:36 +0000 (UTC) Received: from unknown (HELO opfedv1rlp0h.nor.fr.ftgroup) ([x.x.x.x]) by smtp-out.orange.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 13 Aug 2024 12:04:36 +0200 Received: from unknown (HELO OPE16NORMBX305.corporate.adroot.infra.ftgroup) ([x.x.x.x]) by opfedv1rlp0h.nor.fr.ftgroup with ESMTP/TLS/ECDHE-RSA-AES128-GCM-SHA256; 13 Aug 2024 12:04:36 +0200 Received: from tl-lnx-nyma7486-2 [x.x.x.x] by OPE16NORMBX305.corporate.adroot.infra.ftgroup [x.x.x.x] with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.39; Tue, 13 Aug 2024 12:04:35 +0200 Received: by tl-lnx-nyma7486-2 (sSMTP sendmail emulation); Tue, 13 Aug 2024 12:04:34 +0200 X-IronPort-AV: E=Sophos;i="6.09,285,1716242400"; d="scan'208";a="178909847" From: To: Date: Tue, 13 Aug 2024 12:04:31 +0200 Message-ID: <8402ba3c5551505159a155c5cf0fc5b58c6a3614.1723543467.git.yann.morin@orange.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 X-Originating-IP: [10.115.27.53] X-ClientProxiedBy: OPE16NORMBX304.corporate.adroot.infra.ftgroup (10.115.26.13) To OPE16NORMBX305.corporate.adroot.infra.ftgroup (10.115.27.10) X-Mailman-Original-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=orange.com; i=@orange.com; q=dns/txt; s=orange002; t=1723543477; x=1755079477; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=l4uh5wF4iKBQtWel4atvYvt7fCxgrOAANsi9NDvrcpI=; b=nFj/U6djW/95KvtUaWdhC+MRO6BUmdOJ0Mt6yan93fDm/0GHGM7hhCaN Cgl58uzcLdZs24ogvrk32fh/qNnhUvjIKm2i3msNQPqSZx4eV4lxOzNts rQUKPaQTol5lgvdh23S1uJuDBRt3KJlscdOThCwS65y3QDq+60UHlpjVC AUsU4ISjjki3nvBYHhhF+DXDSqRbKs8KUeusNxbc15U550b1oP2ShiAiV dTxckK4gdqu0HGR1CgaFt2LsUtcgUJ/+GEzAMo2XCcmvoFeQ7fRPmxtal 5/1oCpRzPHQDauZFmxz0AqSEVxLXmh/uPE8GIxiDI/yPkk6HPmTvJzyCd w==; X-Mailman-Original-Authentication-Results: smtp1.osuosl.org; dmarc=pass (p=none dis=none) header.from=orange.com X-Mailman-Original-Authentication-Results: smtp1.osuosl.org; dkim=pass (2048-bit key) header.d=orange.com header.i=@orange.com header.a=rsa-sha256 header.s=orange002 header.b=nFj/U6dj Subject: [Buildroot] [PATCH 3/3] toolchain/wrapper: move -ztext from LDFLAGS to toolchain wrapper X-BeenThere: buildroot@buildroot.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion and development of buildroot List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Giulio Benetti , Romain Naour , yann.morin@orange.com, =?utf-8?b?SiAuIE5ldXNjaMOkZmVy?= , Thomas Petazzoni Errors-To: buildroot-bounces@buildroot.org Sender: "buildroot" From: "Yann E. MORIN" Passing linker flags via LDFLAGS is notoriously fragile: packages either use LDFLAGS with the gcc frontend, or directly with ld. This means that care must be taken to only pass flags that are recognised by both gcc and ld, which usually proves a bit challenging, as bd035872be99 (package/Makefile.in: Use "-z text" instead of "-Wl, -z, text") demonstrates (later ammended by 3a39e706a91a). It turns out that we already have a better, robust way of passing LDFLAGS via the gcc frotned: the toolchain wrapper, which we already use to pass relro for example. We can revert back to using -Wl,-z,text, as we're sure whis is going to be a call to the gcc frontend (the wrapper is not used for ld), which makes it consistent with the other similar args for -z relro and -z now. Signed-off-by: Yann E. MORIN Cc: J. Neuschäfer Cc: Romain Naour Cc: Giulio Benetti Cc: Thomas Petazzoni Tested-by: Markus Mayer --- package/Makefile.in | 19 ------------------- toolchain/toolchain-wrapper.c | 6 +++++- toolchain/toolchain-wrapper.mk | 9 +++++++++ 3 files changed, 14 insertions(+), 20 deletions(-) diff --git a/package/Makefile.in b/package/Makefile.in index 808b71a93e..47a89f1ae1 100644 --- a/package/Makefile.in +++ b/package/Makefile.in @@ -149,25 +149,6 @@ endif TARGET_LDFLAGS = $(call qstrip,$(BR2_TARGET_LDFLAGS)) -# musl's dynamic loader doesn't support DT_TEXTREL, which results in a runtime -# crash if it gets used. The "-z text" linker option issues a build-time error -# when DT_TEXREL is used, so we capture the problem earlier. -# -# See also: https://www.openwall.com/lists/musl/2020/09/25/4 -# -# NOTE: We're using "-ztext" instead of "-Wl,-z,text" here, because some -# packages pass TARGET_LDFLAGS directly to ld rather than gcc, and ld doesn't -# support -Wl,[...]. -z is supported by both gcc and clang, so it probably -# won't cause us problems. -# -# We're using "-ztext" instead of "-z text" here, because some buildsystems -# (like scons, for gpsd) will reorder and/or drop LDFLAGS, causing a lone -# "-z" to be passed and the "text" keyword to be dropped otherwise. Both -# gcc and ld supports that, so it probably won't cause us problems. -ifeq ($(BR2_TOOLCHAIN_USES_MUSL):$(BR2_STATIC_LIBS),y:) -TARGET_LDFLAGS += -ztext -endif - # By design, _FORTIFY_SOURCE requires gcc optimization to be enabled. # Therefore, we need to pass _FORTIFY_SOURCE and the optimization level # through the same mechanism, i.e currently through CFLAGS. Passing diff --git a/toolchain/toolchain-wrapper.c b/toolchain/toolchain-wrapper.c index 7647a1a12d..de54f838ec 100644 --- a/toolchain/toolchain-wrapper.c +++ b/toolchain/toolchain-wrapper.c @@ -51,10 +51,11 @@ static char _date_[sizeof("-D__DATE__=\"MMM DD YYYY\"")]; * -Wno-builtin-macro-redefined * -Wl,-z,now * -Wl,-z,relro + * -Wl,-z,text * -fPIE * -pie */ -#define EXCLUSIVE_ARGS 10 +#define EXCLUSIVE_ARGS 11 static char *predef_args[] = { #ifdef BR_CCACHE @@ -486,6 +487,9 @@ int main(int argc, char **argv) break; } if (i == argc) { +#ifdef BR2_ZTEXT + *cur++ = "-Wl,-z,text"; +#endif /* https://wiki.gentoo.org/wiki/Hardened/Toolchain#Mark_Read-Only_Appropriate_Sections */ #ifdef BR2_RELRO_PARTIAL *cur++ = "-Wl,-z,relro"; diff --git a/toolchain/toolchain-wrapper.mk b/toolchain/toolchain-wrapper.mk index cbf46f15fa..d605e41f2c 100644 --- a/toolchain/toolchain-wrapper.mk +++ b/toolchain/toolchain-wrapper.mk @@ -83,6 +83,15 @@ else ifeq ($(BR2_RELRO_FULL),y) TOOLCHAIN_WRAPPER_ARGS += -DBR2_RELRO_FULL endif +# musl's dynamic loader doesn't support DT_TEXTREL, which results in a runtime +# crash if it gets used. The "-z text" linker option issues a build-time error +# when DT_TEXREL is used, so we capture the problem earlier. +# +# See also: https://www.openwall.com/lists/musl/2020/09/25/4 +ifeq ($(BR2_TOOLCHAIN_USES_MUSL):$(BR2_STATIC_LIBS),y:) +TOOLCHAIN_WRAPPER_ARGS += -DBR2_ZTEXT +endif + define TOOLCHAIN_WRAPPER_BUILD $(HOSTCC) $(HOST_CFLAGS) $(TOOLCHAIN_WRAPPER_ARGS) \ -s -Wl,--hash-style=$(TOOLCHAIN_WRAPPER_HASH_STYLE) \