public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
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	[thread overview]
Message-ID: <20240102105415.516313-1-yanzhang.wang@intel.com> (raw)
In-Reply-To: <20230810233348.1214955-1-yanzhang.wang@intel.com>

From: Yanzhang Wang <yanzhang.wang@intel.com>

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


  parent reply	other threads:[~2024-01-02 10:54 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-10 23:33 [PATCH] " yanzhang.wang
2023-08-11  1:57 ` Palmer Dabbelt
2023-08-13 12:20   ` Wang, Yanzhang
2023-08-15 11:46     ` Adhemerval Zanella Netto
2023-09-09  3:17       ` Wang, Yanzhang
2023-09-09  3:30         ` Palmer Dabbelt
2023-09-09  6:54           ` Wang, Yanzhang
2023-09-11 14:14             ` Palmer Dabbelt
2023-09-11 13:34           ` Darius Rad
2023-09-11 17:28             ` DJ Delorie
2023-09-11 16:17           ` Adhemerval Zanella Netto
2023-09-20 13:36             ` Palmer Dabbelt
2023-09-21 13:47               ` Wang, Yanzhang
2023-10-17  8:28                 ` Wang, Yanzhang
2023-10-17 13:42                 ` Adhemerval Zanella Netto
2023-10-24  5:59                   ` Wang, Yanzhang
2023-10-24 11:39                     ` Adhemerval Zanella Netto
2023-10-26  3:30                       ` Wang, Yanzhang
2023-08-14 13:12 ` Carlos O'Donell
2023-08-15  1:48   ` Wang, Yanzhang
2023-08-15  1:44 ` [PATCH v2] " yanzhang.wang
2023-12-17 13:16   ` Wang, Yanzhang
2023-12-19 17:44   ` Adhemerval Zanella Netto
2024-01-02 11:02     ` Wang, Yanzhang
2024-01-02 10:54 ` yanzhang.wang [this message]
2024-01-02 18:30   ` [PATCH v3] " Adhemerval Zanella Netto
2024-01-17 12:23   ` Andreas Schwab
2024-01-29 12:46   ` Andreas Schwab
2024-02-01 12:39     ` Wang, Yanzhang
2024-02-01 12:53       ` Andreas Schwab
2024-05-21 11:13   ` Maciej W. Rozycki

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240102105415.516313-1-yanzhang.wang@intel.com \
    --to=yanzhang.wang@intel.com \
    --cc=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).