diff mbox series

[v2] RISC-V: Enable static-pie.

Message ID 20230815014434.1902446-1-yanzhang.wang@intel.com
State New
Headers show
Series [v2] RISC-V: Enable static-pie. | expand

Commit Message

develop--- via Libc-alpha Aug. 15, 2023, 1:44 a.m. UTC
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.
---
 sysdeps/riscv/configure    | 1 +
 sysdeps/riscv/configure.ac | 3 +++
 sysdeps/riscv/dl-machine.h | 2 +-
 3 files changed, 5 insertions(+), 1 deletion(-)

Comments

Wang, Yanzhang Dec. 17, 2023, 1:16 p.m. UTC | #1
Hi,

We have a lot talks about this patch sometimes ago at 
https://patchwork.sourceware.org/project/glibc/patch/20230810233348.1214955-1-yanzhang.wang@intel.com/.
It mainly focus on the regression test of this feature. I did some tests
as the mail shows. And based on the results, it seems no regression
introduced by this patch.

I think I got one approve from Adhemerval. Is there any further comments?
What do I need to do next step to get this patch be merged?

Thanks,
Yanzhang

> -----Original Message-----
> From: Wang, Yanzhang <yanzhang.wang@intel.com>
> Sent: Tuesday, August 15, 2023 9:45 AM
> To: libc-alpha@sourceware.org
> Cc: carlos@redhat.com; palmer@dabbelt.com; Wang, Yanzhang
> <yanzhang.wang@intel.com>
> Subject: [PATCH v2] RISC-V: Enable static-pie.
> 
> 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.
> ---
>  sysdeps/riscv/configure    | 1 +
>  sysdeps/riscv/configure.ac | 3 +++
>  sysdeps/riscv/dl-machine.h | 2 +-
>  3 files changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/sysdeps/riscv/configure b/sysdeps/riscv/configure index
> acd1f5e743..ecaf8e2a0b 100644
> --- a/sysdeps/riscv/configure
> +++ b/sysdeps/riscv/configure
> @@ -31,3 +31,4 @@ printf "%s\n" "$libc_cv_riscv_r_align" >&6; }
> config_vars="$config_vars  riscv-r-align = $libc_cv_riscv_r_align"
> 
> +$as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
> diff --git a/sysdeps/riscv/configure.ac b/sysdeps/riscv/configure.ac index
> dbcc216689..36da2b5396 100644
> --- a/sysdeps/riscv/configure.ac
> +++ b/sysdeps/riscv/configure.ac
> @@ -16,3 +16,6 @@ EOF
>    fi
>    rm -rf conftest.*])
>  LIBC_CONFIG_VAR([riscv-r-align], [$libc_cv_riscv_r_align])
> +
> +dnl Static PIE is supported.
> +AC_DEFINE(SUPPORT_STATIC_PIE)
> diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h index
> c0c9bd93ad..ad875c0828 100644
> --- a/sysdeps/riscv/dl-machine.h
> +++ b/sysdeps/riscv/dl-machine.h
> @@ -323,7 +323,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.41.0
Adhemerval Zanella Netto Dec. 19, 2023, 5:44 p.m. UTC | #2
On 14/08/23 22:44, yanzhang.wang--- via Libc-alpha wrote:
> 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.

To enable this support as default woudl equires GCC commit 
b20e59f49b51b7baf05e1b727be5da947e617496 that pass the expected ld flags. 
Otherwise, building static-pie will fail with missing _DYNAMIC:

riscv64-glibc-linux-gnu-gcc -o [...] support/test-run-command -nostdlib -nostartfiles -static -static-pie [...]
[...]libc.a(dl-reloc-static-pie.o): in function `_dl_relocate_static_pie':
[...]elf/dl-reloc-static-pie.c:48: undefined reference to `_DYNAMIC' 

I think you will need to add something similar to what loongarch did on 
sysdeps/loongarch/configure.ac and check whether compile do actually 
supports -static-pie.

> ---
>  sysdeps/riscv/configure    | 1 +
>  sysdeps/riscv/configure.ac | 3 +++
>  sysdeps/riscv/dl-machine.h | 2 +-
>  3 files changed, 5 insertions(+), 1 deletion(-)
> 
> diff --git a/sysdeps/riscv/configure b/sysdeps/riscv/configure
> index acd1f5e743..ecaf8e2a0b 100644
> --- a/sysdeps/riscv/configure
> +++ b/sysdeps/riscv/configure
> @@ -31,3 +31,4 @@ printf "%s\n" "$libc_cv_riscv_r_align" >&6; }
>  config_vars="$config_vars
>  riscv-r-align = $libc_cv_riscv_r_align"
>  
> +$as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
> diff --git a/sysdeps/riscv/configure.ac b/sysdeps/riscv/configure.ac
> index dbcc216689..36da2b5396 100644
> --- a/sysdeps/riscv/configure.ac
> +++ b/sysdeps/riscv/configure.ac
> @@ -16,3 +16,6 @@ EOF
>    fi
>    rm -rf conftest.*])
>  LIBC_CONFIG_VAR([riscv-r-align], [$libc_cv_riscv_r_align])
> +
> +dnl Static PIE is supported.
> +AC_DEFINE(SUPPORT_STATIC_PIE)
> diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
> index c0c9bd93ad..ad875c0828 100644
> --- a/sysdeps/riscv/dl-machine.h
> +++ b/sysdeps/riscv/dl-machine.h
> @@ -323,7 +323,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
Wang, Yanzhang Jan. 2, 2024, 11:02 a.m. UTC | #3
Thanks for pointing this out. Yes, I should do the verification. I have
pushed a new patch (patch v3) just now and verified with two version's 
gcc (support and not support). Please help to review it.

Thanks,
Yanzhang

> -----Original Message-----
> From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
> Sent: Wednesday, December 20, 2023 1:44 AM
> To: Wang, Yanzhang <yanzhang.wang@intel.com>; libc-alpha@sourceware.org
> Cc: carlos@redhat.com; palmer@dabbelt.com
> Subject: Re: [PATCH v2] RISC-V: Enable static-pie.
> 
> 
> 
> On 14/08/23 22:44, yanzhang.wang--- via Libc-alpha wrote:
> > 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.
> 
> To enable this support as default woudl equires GCC commit
> b20e59f49b51b7baf05e1b727be5da947e617496 that pass the expected ld flags.
> Otherwise, building static-pie will fail with missing _DYNAMIC:
> 
> riscv64-glibc-linux-gnu-gcc -o [...] support/test-run-command -nostdlib -
> nostartfiles -static -static-pie [...]
> [...]libc.a(dl-reloc-static-pie.o): in function `_dl_relocate_static_pie':
> [...]elf/dl-reloc-static-pie.c:48: undefined reference to `_DYNAMIC'
> 
> I think you will need to add something similar to what loongarch did on
> sysdeps/loongarch/configure.ac and check whether compile do actually
> supports -static-pie.
> 
> > ---
> >  sysdeps/riscv/configure    | 1 +
> >  sysdeps/riscv/configure.ac | 3 +++
> >  sysdeps/riscv/dl-machine.h | 2 +-
> >  3 files changed, 5 insertions(+), 1 deletion(-)
> >
> > diff --git a/sysdeps/riscv/configure b/sysdeps/riscv/configure index
> > acd1f5e743..ecaf8e2a0b 100644
> > --- a/sysdeps/riscv/configure
> > +++ b/sysdeps/riscv/configure
> > @@ -31,3 +31,4 @@ printf "%s\n" "$libc_cv_riscv_r_align" >&6; }
> > config_vars="$config_vars  riscv-r-align = $libc_cv_riscv_r_align"
> >
> > +$as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
> > diff --git a/sysdeps/riscv/configure.ac b/sysdeps/riscv/configure.ac
> > index dbcc216689..36da2b5396 100644
> > --- a/sysdeps/riscv/configure.ac
> > +++ b/sysdeps/riscv/configure.ac
> > @@ -16,3 +16,6 @@ EOF
> >    fi
> >    rm -rf conftest.*])
> >  LIBC_CONFIG_VAR([riscv-r-align], [$libc_cv_riscv_r_align])
> > +
> > +dnl Static PIE is supported.
> > +AC_DEFINE(SUPPORT_STATIC_PIE)
> > diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
> > index c0c9bd93ad..ad875c0828 100644
> > --- a/sysdeps/riscv/dl-machine.h
> > +++ b/sysdeps/riscv/dl-machine.h
> > @@ -323,7 +323,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
diff mbox series

Patch

diff --git a/sysdeps/riscv/configure b/sysdeps/riscv/configure
index acd1f5e743..ecaf8e2a0b 100644
--- a/sysdeps/riscv/configure
+++ b/sysdeps/riscv/configure
@@ -31,3 +31,4 @@  printf "%s\n" "$libc_cv_riscv_r_align" >&6; }
 config_vars="$config_vars
 riscv-r-align = $libc_cv_riscv_r_align"
 
+$as_echo "#define SUPPORT_STATIC_PIE 1" >>confdefs.h
diff --git a/sysdeps/riscv/configure.ac b/sysdeps/riscv/configure.ac
index dbcc216689..36da2b5396 100644
--- a/sysdeps/riscv/configure.ac
+++ b/sysdeps/riscv/configure.ac
@@ -16,3 +16,6 @@  EOF
   fi
   rm -rf conftest.*])
 LIBC_CONFIG_VAR([riscv-r-align], [$libc_cv_riscv_r_align])
+
+dnl Static PIE is supported.
+AC_DEFINE(SUPPORT_STATIC_PIE)
diff --git a/sysdeps/riscv/dl-machine.h b/sysdeps/riscv/dl-machine.h
index c0c9bd93ad..ad875c0828 100644
--- a/sysdeps/riscv/dl-machine.h
+++ b/sysdeps/riscv/dl-machine.h
@@ -323,7 +323,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