public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc] RISC-V: Enable static-pie.
@ 2024-01-12 19:35 Adhemerval Zanella
  0 siblings, 0 replies; only message in thread
From: Adhemerval Zanella @ 2024-01-12 19:35 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=e0590f41fe1e7a54169e8f8828efe62b5064139e

commit e0590f41fe1e7a54169e8f8828efe62b5064139e
Author: Yanzhang Wang <yanzhang.wang@intel.com>
Date:   Tue Jan 2 18:54:15 2024 +0800

    RISC-V: Enable static-pie.
    
    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.
    Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

Diff:
---
 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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-01-12 19:35 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-12 19:35 [glibc] RISC-V: Enable static-pie Adhemerval Zanella

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).