From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by sourceware.org (Postfix) with ESMTPS id 702303858C41 for ; Tue, 2 Jan 2024 10:54:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 702303858C41 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 702303858C41 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.55.52.115 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704192900; cv=none; b=BvZxn+ExzBARhU6JMvPUc1FmF2tBBvTWV1bD+1OozgwCb+yuak4ctNzHvBHNgdaRAKorRnp1F19O5vUr7Ur+t3s1s8PgblgmxPTjsjAYKxjJKCdscIRxImNUiAiTFwDJVNmdZP3V8RQXRMg19gOqghZjJ2xmEU6ZpgI9JH1TPHw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1704192900; c=relaxed/simple; bh=UypKmpv+dOM742JIli3QnCrsd0wkbDcCMhVwGT+/xrM=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=oe3xjxjwyz1c7Q5udM+yExJp6G0HBGfB1briB9yzCn3F1awAGBMFHiQap/1nX8C+Dkpn7NX+wGHb02Oi11YEy4U6Ln3BzJF1lUdw9HF3t2ua1FnIgoe94A4dHn1HL+ZoRFTk3wTmXCQpBT0XZJc0rygfzY5p03Qzsvv9O83BE0c= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1704192896; x=1735728896; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=UypKmpv+dOM742JIli3QnCrsd0wkbDcCMhVwGT+/xrM=; b=W8iOKY0i4/GTJ6fPN+uyOOKgSm+9ZwXHmEk+GqZpbfGjKj6/che4WdQ8 zu3CbUvx1PvEqRdHT9yFuAj/5lO3tYU5QtTwrJ6Dq9MPXLnr70JJQudtj BF9zuq26/si0EbLOis7S3yCN4+IdPXm0dt4MCltZc4T57HmCxzZAH3ENL Ju6Sf31jSoqAiMDv8uGQdllK/BQ/gnkhTZOVMY39L3M0NI/Jsp2C9o4wC iNXS2NREZEhzWawNKcGEUrT7Q/92F4Qh3CuDdBzE1dJkNPykR9SgU1gG+ 5748wgD/pmbIZUUtMfwoaETbxTCk0/aPc5M3fufYhajKjDqcFrYDYS9Yw Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10940"; a="396596695" X-IronPort-AV: E=Sophos;i="6.04,324,1695711600"; d="scan'208";a="396596695" Received: from orsmga001.jf.intel.com ([10.7.209.18]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2024 02:54:55 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10940"; a="813898913" X-IronPort-AV: E=Sophos;i="6.04,324,1695711600"; d="scan'208";a="813898913" Received: from shvmail03.sh.intel.com ([10.239.245.20]) by orsmga001.jf.intel.com with ESMTP; 02 Jan 2024 02:54:52 -0800 Received: from yanzhang-dev.sh.intel.com (yanzhang-dev.sh.intel.com [10.239.159.141]) by shvmail03.sh.intel.com (Postfix) with ESMTP id 1033F1005699; Tue, 2 Jan 2024 18:54:52 +0800 (CST) From: yanzhang.wang@intel.com To: libc-alpha@sourceware.org Cc: adhemerval.zanella@linaro.org, yanzhang.wang@intel.com Subject: [PATCH v3] RISC-V: Enable static-pie. Date: Tue, 2 Jan 2024 18:54:15 +0800 Message-ID: <20240102105415.516313-1-yanzhang.wang@intel.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20230810233348.1214955-1-yanzhang.wang@intel.com> References: <20230810233348.1214955-1-yanzhang.wang@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Yanzhang Wang This patch referents the commit 374cef3 to add static-pie support. And because the dummy link map is used when relocating ourselves, so need not to set __global_pointer$ at this time. It will also check whether toolchain supports to build static-pie. --- sysdeps/riscv/configure | 49 ++++++++++++++++++++++++++++++++++++++ sysdeps/riscv/configure.ac | 27 +++++++++++++++++++++ sysdeps/riscv/dl-machine.h | 2 +- 3 files changed, 77 insertions(+), 1 deletion(-) diff --git a/sysdeps/riscv/configure b/sysdeps/riscv/configure index acd1f5e743..c8f01709f8 100644 --- a/sysdeps/riscv/configure +++ b/sysdeps/riscv/configure @@ -31,3 +31,52 @@ printf "%s\n" "$libc_cv_riscv_r_align" >&6; } config_vars="$config_vars riscv-r-align = $libc_cv_riscv_r_align" +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: checking if the toolchain is sufficient to build static PIE on RISC-V" >&5 +printf %s "checking if the toolchain is sufficient to build static PIE on RISC-V... " >&6; } +if test ${libc_cv_static_pie_on_riscv+y} +then : + printf %s "(cached) " >&6 +else $as_nop + + cat > conftest1.S <<\EOF + .globl _start + .type _start, @function +_start: + nop + + .data + /* This should produce an R_RISCV_RELATIVE in the static PIE. */ + .dword _start +EOF + + libc_cv_static_pie_on_riscv=no + if { ac_try='${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest1 conftest1.S' + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 + (eval $ac_try) 2>&5 + ac_status=$? + printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; } \ + && { ac_try='LC_ALL=C $READELF -Wr conftest1 | grep -q R_RISCV_RELATIVE' + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 + (eval $ac_try) 2>&5 + ac_status=$? + printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; } \ + && ! { ac_try='LC_ALL=C $READELF -Wl conftest1 | grep -q INTERP' + { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5 + (eval $ac_try) 2>&5 + ac_status=$? + printf "%s\n" "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5 + test $ac_status = 0; }; } + then + libc_cv_static_pie_on_riscv=yes + fi + rm -rf conftest* +fi +{ printf "%s\n" "$as_me:${as_lineno-$LINENO}: result: $libc_cv_static_pie_on_riscv" >&5 +printf "%s\n" "$libc_cv_static_pie_on_riscv" >&6; } + +if test "$libc_cv_static_pie_on_riscv" = yes; then + printf "%s\n" "#define SUPPORT_STATIC_PIE 1" >>confdefs.h + +fi diff --git a/sysdeps/riscv/configure.ac b/sysdeps/riscv/configure.ac index dbcc216689..ee3d1ed014 100644 --- a/sysdeps/riscv/configure.ac +++ b/sysdeps/riscv/configure.ac @@ -16,3 +16,30 @@ EOF fi rm -rf conftest.*]) LIBC_CONFIG_VAR([riscv-r-align], [$libc_cv_riscv_r_align]) + +dnl Test if the toolchain is new enough for static PIE. +AC_CACHE_CHECK([if the toolchain is sufficient to build static PIE on RISC-V], +libc_cv_static_pie_on_riscv, [ + cat > conftest1.S <<\EOF + .globl _start + .type _start, @function +_start: + nop + + .data + /* This should produce an R_RISCV_RELATIVE in the static PIE. */ + .dword _start +EOF + + libc_cv_static_pie_on_riscv=no + if AC_TRY_COMMAND([${CC-cc} $CFLAGS $CPPFLAGS $LDFLAGS -static-pie -nostdlib -fPIE -o conftest1 conftest1.S]) \ + && AC_TRY_COMMAND([LC_ALL=C $READELF -Wr conftest1 | grep -q R_RISCV_RELATIVE]) \ + && ! AC_TRY_COMMAND([LC_ALL=C $READELF -Wl conftest1 | grep -q INTERP]) + then + libc_cv_static_pie_on_riscv=yes + fi + rm -rf conftest* ]) + +if test "$libc_cv_static_pie_on_riscv" = yes; then + AC_DEFINE(SUPPORT_STATIC_PIE) +fi diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index ffb8c4aaa0..0cbb476c05 100644 --- a/sysdeps/riscv/dl-machine.h +++ b/sysdeps/riscv/dl-machine.h @@ -348,7 +348,7 @@ elf_machine_runtime_setup (struct link_map *l, struct r_scope_elem *scope[], gotplt[1] = (ElfW(Addr)) l; } - if (l->l_type == lt_executable) + if (l->l_type == lt_executable && l->l_scope != NULL) { /* The __global_pointer$ may not be defined by the linker if the $gp register does not be used to access the global variable -- 2.42.1