From patchwork Thu Nov 28 15:43:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Adhemerval Zanella X-Patchwork-Id: 845947 Delivered-To: patch@linaro.org Received: by 2002:adf:f2c4:0:b0:382:43a8:7b94 with SMTP id d4csp314887wrp; Thu, 28 Nov 2024 07:48:33 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCV6jpy9DraHk784WcOIpB5CYbjRpEB0D6SS4IUwn2+DNxpl8AISMl+x2n+Qzn/8AounNhqfBA==@linaro.org X-Google-Smtp-Source: AGHT+IH+pnSlgiioNgIZUlY2PX3uqwMeU1pzCiJLfi2kuBYKyZGiFXIXfibfbwC9CI/JLX8wOpSL X-Received: by 2002:a05:620a:688c:b0:7b6:668a:5a5 with SMTP id af79cd13be357-7b67c460eb1mr903982285a.50.1732808913612; Thu, 28 Nov 2024 07:48:33 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1732808913; cv=pass; d=google.com; s=arc-20240605; b=N3EuRhTYDSwUUFZBZhFOXw/rT3xk3nA0Iji7YUUrR6kilgsw/XsPpx578tW9fdG969 AgYRfGNFCK8aM6nMOMeb2nwcySqOPNLmSLRRh0Ut1yPcSY7Ih/0OOASk1ReTNUo13YvB AFiwO0nfVD/2zFACwnMH6LaZAvMW40gc4r2FSfa7+zuMU/wXjnjrfi3yE18p4xqepDFM xKILz3D/AxxIxNl9IYIYzjFwReOwjwnloF7rBVe5Pd+2B+sEg7d2+0jGgCFdI1uvc5DY bjG7kMzQNjEa3sIKkrZOVhdIfBHeyRfNS1bbqmnvKrmubB3+I0VjvVpGOpkjZp9OAyH8 HvEQ== 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:dkim-filter:arc-filter:dmarc-filter :delivered-to:dkim-filter; bh=eFbHC6KJzF5KTMSUPsJZMaiMR5Lw8zdSDkrto+kLnuo=; fh=JruiOR+n5wiv4jZbtXJYp9lJ8UkedxUHyGOCC37Fjus=; b=JASSNUt27fVD4xJfxcNdaTsKySxQpyGniGkh6nGU6aihHjFebQpDYosue1XKYuk8O5 93sqftVq1OM8elEocHnwA4mN3zAgbrwmteF0h+dnQE0I0WFfAgYNhccVk1UXcWgi+FYm 6YtLndkldq7Sz5/Tk9LDHoUoNHVPZkHgBZAhZBC4fshGq5c1AsH3mC6UcIBcsn2R9CRp SIWnfQU23/PLqVJwhZRtc3Ga7/vQ0RDuXMXiBs2EbyTehY1JJIxUyKSEjh5sNh/EKOmZ 0ZaQW+SzrdDY7pifMKpkiAkSfoslVgpKwBtbeJVQR8IHCqBbxHotghvQTePYfhqwpYuF GW1g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=uLd3USVo; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id d75a77b69052e-466c446272fsi19948291cf.717.2024.11.28.07.48.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2024 07:48:33 -0800 (PST) Received-SPF: pass (google.com: domain of binutils-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=uLd3USVo; arc=pass (i=1); spf=pass (google.com: domain of binutils-bounces~patch=linaro.org@sourceware.org designates 2620:52:3:1:0:246e:9693:128c 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 97F963858C66 for ; Thu, 28 Nov 2024 15:48:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 97F963858C66 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=uLd3USVo X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pl1-x644.google.com (mail-pl1-x644.google.com [IPv6:2607:f8b0:4864:20::644]) by sourceware.org (Postfix) with ESMTPS id D76ED3858C98 for ; Thu, 28 Nov 2024 15:45:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D76ED3858C98 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 D76ED3858C98 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::644 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732808723; cv=none; b=JwVvygTGvh5lWvT0Q9Ajid7Ftcn5DXjBahmI4sWasLEusn9U3nX5oy2FKD3sIriwxr3YJ0+qt68Zpweg316r+bJRrZ9DJOYy1R4obQBG869Yp2BuIvKDaNhkOJJahoEelA1FtxVDvw0Tp2KSXWMFah9LlBNvWLWd0l4Ic2tkr9c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1732808723; c=relaxed/simple; bh=PSZbumG6qnDoGE2f2fLd/IsH/K9xklLca4EnmzelKN4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=CfiuheYH8AvVJiFkgzSXYGOQqm767Elft5qeyR6mlBd6nSs/8F7AQ7I5WxaSumZ0MULejAoRtMOIcWNc/m5RKCqYDbnILfegjz0+92FyhbmoKtKD3pMLLZ1R4yu6D7lXWhzGi9iQBbu+M6e5dWIVGYI2+l8M/LI3IpLxF5j7RKI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D76ED3858C98 Received: by mail-pl1-x644.google.com with SMTP id d9443c01a7336-21269c8df64so8680005ad.2 for ; Thu, 28 Nov 2024 07:45:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1732808721; x=1733413521; 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=eFbHC6KJzF5KTMSUPsJZMaiMR5Lw8zdSDkrto+kLnuo=; b=uLd3USVofYjh2iSOBETt5f5o1ZKjwd4IBwWM267085mSeQB6NPHKn6/UDl1bAeXX/9 OjgxPfLjqbzzFKxaN4uxQeGstDV9mDFIKwy7PzT/pFzz9ubZEPkAXIzkTD7hfU1urRuz r9znO9mLIWqgiI7e5tynH0qoZRzFWNdtc8XqzyJzPlQsZFu2aDbATeBIb9neXo8iKnLI RnYHAEmjmovfg9f67MQQ6xs3UTEOIhQBTxV+UYOUgWvtnsmnkLTRlqiDY0yTG1HbxI+4 u1RwY35Tegx+P6Wactvyv3ZPKyfqksOgg3KgqDAnS7ZxVwmVykO3QPW4rB/Bo0URviMA 5mYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1732808721; x=1733413521; 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=eFbHC6KJzF5KTMSUPsJZMaiMR5Lw8zdSDkrto+kLnuo=; b=Sz7Yx95zxBqofvpFNTO58AYUd5JmNI0i8llWjMEQ2NkxgFauZonqO16AIQL4r61HKG qHwle/k3YNNxQfJ66nv7/q7JkOPXOlGFcd7yfnPm4ponrEktgy5lLdXFxPY3Ra3P8k13 3IWYtmiNQsGiXS9AoihCGWABpcxnxONyuL3oI/vHNyQW/V4Jm5mDivbulHjihFfJc+ib ZTcB4Va3A6Kg2i2DNeurSF52c6bsxGaTpz8H4P7shkqilf/e/QuT0J2OqbPqvp4bmwsf IggizyPadGjWyeqFMU9/Hwi20IoWB8nz92yfBkqfpq4pnrIQLgwq84pIQ3r0dRvhkjGV ZK7A== X-Gm-Message-State: AOJu0Yw52ZxWs/AvNoNR11ON4x4Zh6NAz8KpdP3tEZRpGQ3gsiwzGRbs INUPsHlIUO6nhqan5DZ+LQ1JOVs6lUxyAGgrJr7EjuuTM4+7VxGR/tyjccZXzXXv0akCmVftx8k DYJMnF1QS X-Gm-Gg: ASbGncvy99JzHKwUqm1f3obEGuFoosmZKjiKJ+SH+qHv+ku6NmNYxYpYS1mwyTK/6rV hW++o7RMFe79HXL9ntrvAQX2wXqIXiOpBcouN8FkylF77JRz+ddGqvd/lvzXGPjTBjLAHGo4PvX EqPEMaAWcCo0/pl/tfzPTlLLDV9bwGDvzHjD5XjAs59oz34nXDNt+61j1cMA7ECoU0KWyBkdCmR symXOQ33aiQ1cARfDSQ4iUMTV0LLPNPWP05ZjwKIvBpjXnovdvAthAGMpvYaLM= X-Received: by 2002:a17:902:c946:b0:211:eb15:9b75 with SMTP id d9443c01a7336-2150147a828mr64484955ad.27.1732808721444; Thu, 28 Nov 2024 07:45:21 -0800 (PST) Received: from mandiga.. ([2804:1b3:a7c1:68c8:2c85:3a76:728e:ead2]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21521985773sm14877405ad.199.2024.11.28.07.45.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Nov 2024 07:45:21 -0800 (PST) From: Adhemerval Zanella To: binutils@sourceware.org Cc: Jeff Xu , "H . J . Lu" Subject: [PATCH v4 3/3] ld: Add --enable-memory-seal configure option Date: Thu, 28 Nov 2024 12:43:21 -0300 Message-ID: <20241128154511.564500-4-adhemerval.zanella@linaro.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20241128154511.564500-1-adhemerval.zanella@linaro.org> References: <20241128154511.564500-1-adhemerval.zanella@linaro.org> MIME-Version: 1.0 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 babcf8753e8..a129ad634e6 100644 --- a/ld/NEWS +++ b/ld/NEWS @@ -34,7 +34,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 633105a43ad..ed838463856 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 0b4197d1c4f..f34141bb238 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. @@ -18965,6 +18978,8 @@ main () if (*(data + i) != *(data3 + i)) return 14; close (fd); + free (data); + free (data3); return 0; } _ACEOF @@ -19444,6 +19459,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 3ac2b46ee03..2166f75b487 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 b2d281f96c7..7d7af8c8068 100644 --- a/ld/emultempl/elf.em +++ b/ld/emultempl/elf.em @@ -99,6 +99,7 @@ fragment <