From patchwork Wed Oct 16 17:01:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella Netto X-Patchwork-Id: 835815 Delivered-To: patch@linaro.org Received: by 2002:a5d:6804:0:b0:37d:45d0:187 with SMTP id w4csp542985wru; Wed, 16 Oct 2024 10:05:10 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUdSvWqBvDlGYD5WKRiUdReGHyYaKa1/S9o88FOssxahOFHj6384tR40KWPlXt1HesiRg0wVQ==@linaro.org X-Google-Smtp-Source: AGHT+IEgAn/wNtOzcPk+rvF7Bh+53giYar8LHYhD/MHpg6R+/Sdt4oh0N3VWnQq+zjuoEKHZwJ2k X-Received: by 2002:a05:6102:440e:b0:492:9e3a:9f48 with SMTP id ada2fe7eead31-4a475f0be5dmr9869890137.2.1729098310046; Wed, 16 Oct 2024 10:05:10 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1729098310; cv=pass; d=google.com; s=arc-20240605; b=JO2jGU/k8NALS6gnsgzleHCydhDBE/kg0D4jin27W4pT0HiRKCG69NAYT+3KXFbjdZ H/Gx95KPm9QJ272BrNdK905vLZNOnoR/e6yH4kBdxJVykyOkkXRYqnhqTdnfYsQAhiMk CSzDOHXepwxOESqu1j7Q6Rl45iEq4frkLhD++a2A2BRvSgKEr9KhrDSk3OQDHDJwgScM 1cs1p6acuh7i9vZm6Xwl+MtRheSzcYjpUwg9jSnRdAPbsOro9Zcw17nvr73jzrY5CIAF tLd55S+vUZ7oCMN/pFvoZXP/xacy3gzgIuEHEHuzKaBXk1gUarA7M+Kg7iI+RVdXugS6 91oA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=5ani4XH/FZVXZvkL6qOY+5+auSX9JmlLAxsLgTCQADA=; fh=rjCpEwa0NAgCAfKxc1n8YCvYLFmdHeGXz6gk9UDhPEY=; b=jBoz2w3sQ8A3ITwKJ9jgVSiwU+QWfHwj8QmhU2ZcRRniQF9a6Q5Epz1zMMa8U34U5H kUc+YohrhFSLquubkApXP0D+bIcqEm0AKZZg4EgNdev49f47ELw+GhvtzGRS3t12a29c Z2bbcLNeI3hd0FLxArPeoq2mXoSd5v+91RTZMY9swubO1+l2GaL7IH310ykPvSTTVezo iQ4xg8Y7Mcy4UomgjggsgiWFrdeXOk2fkFPZbO1FDqmfB574OtbWMU+toahxJgoQYIf8 0RyHk52da+WkgKvaZ4CtDJCywJY0/jBANLg6FhR35NZtZ9SRORRg2vksP8l80FLd3CgY tHTg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dCRj8nyA; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from server2.sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ada2fe7eead31-4a5ace5cf64si1016385137.728.2024.10.16.10.05.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Oct 2024 10:05:10 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=dCRj8nyA; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces~patch=linaro.org@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces~patch=linaro.org@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 96A2A385840A for ; Wed, 16 Oct 2024 17:05:09 +0000 (GMT) X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x62c.google.com (mail-pl1-x62c.google.com [IPv6:2607:f8b0:4864:20::62c]) by sourceware.org (Postfix) with ESMTPS id 98F84385841D for ; Wed, 16 Oct 2024 17:04:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 98F84385841D 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 98F84385841D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::62c ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729098290; cv=none; b=WIU8OJfNvFuykBLJ9FwdQ+OUAFb3vP2JZGKKoLkuSgRatsJm7rPl5aJpMmP0WbLmLUaBcztlTolHsF8YLI+wsez+mHNOblCe2M+Ml8buXFfaBaatvuQeP9uT8eS/8bzAo7udoW6n6FIbUgMWxyxl3djI4ckU5B5lYk3Dx2UthRg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1729098290; c=relaxed/simple; bh=hmH+hdW8Mtj7KIYjDtqCvKJLklip2rUez1btHZ/WlTs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=i+sfUB3nimUj+6x9CUtjJ1TLY/F0y+LVbyGIa2uFPAkTi3RnDS5ekhP2uOdKIt8SRmdvfTwVS7Cx+YwO2C5thgwAXTQ5La2JOt9PsdvXSLCV0Nkl2UujQFgLA6ndnPqNE/+dWdfx5C9mPRpwwhV0vVCtnWVU5pSVADHtpuJqSlQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-20ce5e3b116so334945ad.1 for ; Wed, 16 Oct 2024 10:04:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1729098286; x=1729703086; 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=5ani4XH/FZVXZvkL6qOY+5+auSX9JmlLAxsLgTCQADA=; b=dCRj8nyA5ffJYd/MTeBwqG7H3x6HP5gYXBkIk1amKz3O9tl1QpYw2kiyFpapefMnK3 nhQZt7wZF19Jt59NLKxffkaQPkz0PQF+ORbT6h2blvIX3VddyY6kA/+4pODWftrgTj/x gP+UVi1Ew3IUHII2+JWf56qDNHJaWJVgBvJXoo9T5R9qsOsj21kL4hePnCPZ1qiGy9oR Au3Piru3p4hJzJ7u7I9tJKfUpBU8yeIzOD6Ir4CpgzYTlrXudD8IUjnRl7dLZuY7ULcW 2Hm3NGNYrZ6PsoxdMZYF1tE73JCRyDGO1lDuGFo+eeYb85hWS/TLYvoVaeQXLAnqIUWw F9Vw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729098286; x=1729703086; 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=5ani4XH/FZVXZvkL6qOY+5+auSX9JmlLAxsLgTCQADA=; b=r0fY/XpHCpSsTPlm4BkNJFlcEuMhnMfZ4KN4CWvEr7KvwvPMpq264p0LSKoOCwqxtz AYK5AS7rGCTD0jLZzltnCpsqMMd0rswd5yLyt2lhlpr+qJ0vMUUaXeEpnoP6aVw0mOcR BKsy8Dw2U91MjvlOpwkAisi8sdf9q0toKkRlx6yui8lBEpqZSmQcSd7D+OHTNDtXtRYO j4zyeEuV4kPC8tnTbA7pRirt6uRuQJX2EVYPvi+FFw784LPWTbQlEUsNqSjIQxhOIPoN cb0YOVzwfzlqY8Lm5FjWPT3/uCmJt/kdIpAAV4Zr7Qs6KXEb35lAobZVKO7BYAsiIOvC G+5g== X-Gm-Message-State: AOJu0YwUyaBzXAEzNCn5/MsyKYkm3fiuCwlgtCTYGvEzoaTEnen93JHk H+MBH5W5SdXiAC04UjNF5Kflt1ElfX71fgMaAUBUhTItFGZq4uoctpx0muJQ+9qo48pVTbXYKdR f X-Received: by 2002:a17:902:f78e:b0:20c:c15c:96ab with SMTP id d9443c01a7336-20cc15c9986mr251629885ad.48.1729098286026; Wed, 16 Oct 2024 10:04:46 -0700 (PDT) Received: from mandiga.. ([2804:1b3:a7c3:1434:ab87:e5f9:1b86:daf6]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-20d1805cdaesm30912895ad.281.2024.10.16.10.04.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Oct 2024 10:04:45 -0700 (PDT) From: Adhemerval Zanella To: binutils@sourceware.org Cc: Stephen Roettger , Jeff Xu , "H . J . Lu" Subject: [PATCH v3 3/3] ld: Add --enable-memory-seal configure option Date: Wed, 16 Oct 2024 14:01:13 -0300 Message-ID: <20241016170435.1404114-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241016170435.1404114-1-adhemerval.zanella@linaro.org> References: <20241016170435.1404114-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, 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: binutils@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: binutils-bounces~patch=linaro.org@sourceware.org Add --enable-memory-seal linker configure option to enable memory sealing (GNU_PROPERTY_MEMORY_SEAL) by default. Change-Id: I4ce4ff33657f0f09b1ceb06210b6fcaa501f1799 --- binutils/testsuite/lib/binutils-common.exp | 22 +++++++++++++ ld/NEWS | 3 +- ld/config.in | 3 ++ ld/configure | 38 ++++++++++++++++++---- ld/configure.ac | 17 ++++++++++ ld/emultempl/elf.em | 1 + ld/lexsup.c | 7 ++++ ld/testsuite/config/default.exp | 8 +++++ ld/testsuite/ld-srec/srec.exp | 4 +++ ld/testsuite/lib/ld-lib.exp | 6 ++++ 10 files changed, 101 insertions(+), 8 deletions(-) diff --git a/binutils/testsuite/lib/binutils-common.exp b/binutils/testsuite/lib/binutils-common.exp index 03e8dbb855b..063ba4f20c2 100644 --- a/binutils/testsuite/lib/binutils-common.exp +++ b/binutils/testsuite/lib/binutils-common.exp @@ -408,6 +408,25 @@ proc check_relro_support { } { return $relro_available_saved } +proc check_memory_seal_support { } { + global memory_seal_available_saved + global ld + + if {![info exists memory_seal_available_saved]} { + remote_file host delete nomemory_seal + set ld_output [remote_exec host $ld "-z nomemory-seal"] + if { [string first "not supported" $ld_output] >= 0 + || [string first "unrecognized option" $ld_output] >= 0 + || [string first "-z nomemory-seal ignored" $ld_output] >= 0 + || [string first "cannot find nomemory-seal" $ld_output] >= 0 } { + set memory_seal_available_saved 0 + } else { + set memory_seal_available_saved 1 + } + } + return $memory_seal_available_saved +} + # Check for support of the .noinit section, used for data that is not # initialized at load, or during the application's initialization sequence. proc supports_noinit_section {} { @@ -1401,6 +1420,9 @@ proc run_dump_test { name {extra_options {}} } { if [check_relro_support] { set ld_extra_opt "-z norelro" } + if [check_memory_seal_support] { + append ld_extra_opt " -z nomemory-seal" + } # Add -L$srcdir/$subdir so that the linker command can use # linker scripts in the source directory. diff --git a/ld/NEWS b/ld/NEWS index 4a28592fa32..ba64ef221fb 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -24,7 +24,8 @@ Changes in 2.43: * Add -plugin-save-temps to store plugin intermediate files permanently. * Add -z memory-seal/-z nomemory-seal options to ELF linker to mark the - object to memory sealed. + object to memory sealed. Also added --enable-memory-seal configure option + to enable the memory sealing by default. Changes in 2.42: diff --git a/ld/config.in b/ld/config.in index f2aaf0a6879..74c58ebb319 100644 --- a/ld/config.in +++ b/ld/config.in @@ -60,6 +60,9 @@ default. */ #undef DEFAULT_LD_Z_SEPARATE_CODE +/* Define to 1 if you want to enable -z memory-seal in ELF linker by default. */ +#undef DEFAULT_LD_Z_MEMORY_SEAL + /* Define to 1 if you want to set DT_RUNPATH instead of DT_RPATH by default. */ #undef DEFAULT_NEW_DTAGS diff --git a/ld/configure b/ld/configure index d905f1c6001..361b9fed85c 100755 --- a/ld/configure +++ b/ld/configure @@ -854,6 +854,7 @@ enable_textrel_check enable_separate_code enable_rosegment enable_mark_plt +enable_memory_seal enable_warn_execstack enable_error_execstack enable_warn_rwx_segments @@ -1551,6 +1552,7 @@ Optional Features: --enable-separate-code enable -z separate-code in ELF linker by default --enable-rosegment enable --rosegment in the ELF linker by default --enable-mark-plt enable -z mark-plt in ELF x86-64 linker by default + --enable-memory-seal enable -z memory-seal in ELF linker by default --enable-warn-execstack enable warnings when creating an executable stack --enable-error-execstack turn executable stack warnings into errors @@ -11686,7 +11688,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 11689 "configure" +#line 11691 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -11792,7 +11794,7 @@ else lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2 lt_status=$lt_dlunknown cat > conftest.$ac_ext <<_LT_EOF -#line 11795 "configure" +#line 11797 "configure" #include "confdefs.h" #if HAVE_DLFCN_H @@ -15251,7 +15253,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15297,7 +15299,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15321,7 +15323,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15366,7 +15368,7 @@ else We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15390,7 +15392,7 @@ rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext We can't simply define LARGE_OFF_T to be 9223372036854775807, since some C++ compilers masquerading as C compilers incorrectly reject 9223372036854775807. */ -#define LARGE_OFF_T (((off_t) 1 << 62) - 1 + ((off_t) 1 << 62)) +#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + (((off_t) 1 << 31) << 31)) int off_t_is_large[(LARGE_OFF_T % 2147483629 == 721 && LARGE_OFF_T % 2147483647 == 1) ? 1 : -1]; @@ -15709,6 +15711,17 @@ esac fi +# Decide if -z memory-seal should be enabled in ELF linker by default. +ac_default_ld_z_memory_seal=unset +# Check whether --enable-memory-seal was given. +if test "${enable_memory_seal+set}" = set; then : + enableval=$enable_memory_seal; case "${enableval}" in + yes) ac_default_ld_z_memory_seal=1 ;; + no) ac_default_ld_z_memory_seal=0 ;; +esac +fi + + # By default warn when an executable stack is created due to object files # requesting such, not when the user specifies -z execstack. @@ -18975,6 +18988,8 @@ main () if (*(data + i) != *(data3 + i)) return 14; close (fd); + free (data); + free (data3); return 0; } _ACEOF @@ -19454,6 +19469,15 @@ cat >>confdefs.h <<_ACEOF _ACEOF +if test "${ac_default_ld_z_memory_seal}" = unset; then + ac_default_ld_z_memory_seal=0 +fi + +cat >>confdefs.h <<_ACEOF +#define DEFAULT_LD_Z_MEMORY_SEAL $ac_default_ld_z_memory_seal +_ACEOF + + cat >>confdefs.h <<_ACEOF diff --git a/ld/configure.ac b/ld/configure.ac index 5d10b38a528..7c90b9ad62b 100644 --- a/ld/configure.ac +++ b/ld/configure.ac @@ -232,6 +232,16 @@ AC_ARG_ENABLE(mark-plt, no) ac_default_ld_z_mark_plt=0 ;; esac]) +# Decide if -z memory-seal should be enabled in ELF linker by default. +ac_default_ld_z_memory_seal=unset +AC_ARG_ENABLE(memory-seal, + AS_HELP_STRING([--enable-memory-seal], + [enable -z memory-seal in ELF linker by default]), +[case "${enableval}" in + yes) ac_default_ld_z_memory_seal=1 ;; + no) ac_default_ld_z_memory_seal=0 ;; +esac]) + # By default warn when an executable stack is created due to object files # requesting such, not when the user specifies -z execstack. @@ -617,6 +627,13 @@ AC_DEFINE_UNQUOTED(DEFAULT_LD_Z_MARK_PLT, $ac_default_ld_z_mark_plt, [Define to 1 if you want to enable -z mark-plt in ELF x86-64 linker by default.]) +if test "${ac_default_ld_z_memory_seal}" = unset; then + ac_default_ld_z_memory_seal=0 +fi +AC_DEFINE_UNQUOTED(DEFAULT_LD_Z_MEMORY_SEAL, + $ac_default_ld_z_memory_seal, + [Define to 1 if you want to enable -z memory_seal in ELF linker by default.]) + AC_DEFINE_UNQUOTED(DEFAULT_LD_WARN_EXECSTACK, $ac_default_ld_warn_execstack, diff --git a/ld/emultempl/elf.em b/ld/emultempl/elf.em index ccd43531237..58bd79b09d2 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -99,6 +99,7 @@ fragment <