[v6,2/5] elf: Properly handle zero DT_RELA/DT_REL values

Message ID 20220310200329.1935466-3-hjl.tools@gmail.com
State Superseded
Headers
Series Support DT_RELR relative relocation format |

Checks

Context Check Description
dj/TryBot-apply_patch success Patch applied to master at the time it was sent

Commit Message

H.J. Lu March 10, 2022, 8:03 p.m. UTC
  With DT_RELR, there may be no relocations in DT_RELA/DT_REL and their
entry values are zero.  Don't relocate DT_RELA/DT_REL and update the
combined relocation start address if their entry values are zero.
---
 elf/dynamic-link.h     |  6 +++++-
 elf/get-dynamic-info.h | 18 ++++++++++++++----
 2 files changed, 19 insertions(+), 5 deletions(-)
  

Comments

Adhemerval Zanella Netto March 29, 2022, 4:38 p.m. UTC | #1
On 10/03/2022 17:03, H.J. Lu via Libc-alpha wrote:
> With DT_RELR, there may be no relocations in DT_RELA/DT_REL and their
> entry values are zero.  Don't relocate DT_RELA/DT_REL and update the
> combined relocation start address if their entry values are zero.

Patch looks good with the two small fixes below.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
>  elf/dynamic-link.h     |  6 +++++-
>  elf/get-dynamic-info.h | 18 ++++++++++++++----
>  2 files changed, 19 insertions(+), 5 deletions(-)
> 
> diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
> index d04c457e55..252f407a12 100644
> --- a/elf/dynamic-link.h
> +++ b/elf/dynamic-link.h
> @@ -84,7 +84,9 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
>  	     __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; }  \
>        ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };			      \
>  									      \
> -    if ((map)->l_info[DT_##RELOC])					      \
> +    /* With DT_RELR, DT_RELA/DT_REL can have zero value.  */		      \
> +    if ((map)->l_info[DT_##RELOC]					      \

Compare to NULL here.

> +	&& (map)->l_info[DT_##RELOC]->d_un.d_ptr != 0)			      \
>        {									      \
>  	ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]);		      \
>  	ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;	      \
> @@ -98,6 +100,8 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
>  	ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]);		      \
>  	ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;	      \
>  									      \
> +	if (ranges[0].start == 0)					      \
> +	  ranges[0].start = start;					      \
>  	if (ranges[0].start + ranges[0].size == (start + size))		      \
>  	  ranges[0].size -= size;					      \
>  	if (!(do_lazy)							      \

Ok.

> diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
> index 6c2a3a12b1..f4b957684b 100644
> --- a/elf/get-dynamic-info.h
> +++ b/elf/get-dynamic-info.h
> @@ -83,16 +83,26 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap,
>        ADJUST_DYN_INFO (DT_PLTGOT);
>        ADJUST_DYN_INFO (DT_STRTAB);
>        ADJUST_DYN_INFO (DT_SYMTAB);
> +      ADJUST_DYN_INFO (DT_RELR);
> +      ADJUST_DYN_INFO (DT_JMPREL);
> +      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
> +      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
> +# undef ADJUST_DYN_INFO
> +
> +      /* DT_RELA/DT_REL are mandatory.  But they may have zero value if
> +	 there is DT_RELR.  Don't relocate them if they are zero.  */
> +# define ADJUST_DYN_INFO(tag) \
> +      do								      \
> +	if (info[tag] != NULL && info[tag]->d_un.d_ptr != 0)		      \
> +         info[tag]->d_un.d_ptr += l_addr;				      \
> +      while (0)
> +

Maybe use '{' and '}' on the do ... while.

>  # if ! ELF_MACHINE_NO_RELA
>        ADJUST_DYN_INFO (DT_RELA);
>  # endif
>  # if ! ELF_MACHINE_NO_REL
>        ADJUST_DYN_INFO (DT_REL);
>  # endif
> -      ADJUST_DYN_INFO (DT_RELR);
> -      ADJUST_DYN_INFO (DT_JMPREL);
> -      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
> -      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
>  # undef ADJUST_DYN_INFO
>      }
>    if (info[DT_PLTREL] != NULL)

Ok.
  
H.J. Lu March 29, 2022, 10:30 p.m. UTC | #2
On Tue, Mar 29, 2022 at 9:38 AM Adhemerval Zanella
<adhemerval.zanella@linaro.org> wrote:
>
>
>
> On 10/03/2022 17:03, H.J. Lu via Libc-alpha wrote:
> > With DT_RELR, there may be no relocations in DT_RELA/DT_REL and their
> > entry values are zero.  Don't relocate DT_RELA/DT_REL and update the
> > combined relocation start address if their entry values are zero.
>
> Patch looks good with the two small fixes below.
>
> Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
>
> > ---
> >  elf/dynamic-link.h     |  6 +++++-
> >  elf/get-dynamic-info.h | 18 ++++++++++++++----
> >  2 files changed, 19 insertions(+), 5 deletions(-)
> >
> > diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
> > index d04c457e55..252f407a12 100644
> > --- a/elf/dynamic-link.h
> > +++ b/elf/dynamic-link.h
> > @@ -84,7 +84,9 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
> >            __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; }  \
> >        ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };                              \
> >                                                                             \
> > -    if ((map)->l_info[DT_##RELOC])                                         \
> > +    /* With DT_RELR, DT_RELA/DT_REL can have zero value.  */               \
> > +    if ((map)->l_info[DT_##RELOC]                                          \
>
> Compare to NULL here.

Fixed in v7.

> > +     && (map)->l_info[DT_##RELOC]->d_un.d_ptr != 0)                        \
> >        {                                                                            \
> >       ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]);                  \
> >       ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;           \
> > @@ -98,6 +100,8 @@ elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
> >       ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]);                  \
> >       ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;             \
> >                                                                             \
> > +     if (ranges[0].start == 0)                                             \
> > +       ranges[0].start = start;                                            \
> >       if (ranges[0].start + ranges[0].size == (start + size))               \
> >         ranges[0].size -= size;                                             \
> >       if (!(do_lazy)                                                        \
>
> Ok.
>
> > diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
> > index 6c2a3a12b1..f4b957684b 100644
> > --- a/elf/get-dynamic-info.h
> > +++ b/elf/get-dynamic-info.h
> > @@ -83,16 +83,26 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap,
> >        ADJUST_DYN_INFO (DT_PLTGOT);
> >        ADJUST_DYN_INFO (DT_STRTAB);
> >        ADJUST_DYN_INFO (DT_SYMTAB);
> > +      ADJUST_DYN_INFO (DT_RELR);
> > +      ADJUST_DYN_INFO (DT_JMPREL);
> > +      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
> > +      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
> > +# undef ADJUST_DYN_INFO
> > +
> > +      /* DT_RELA/DT_REL are mandatory.  But they may have zero value if
> > +      there is DT_RELR.  Don't relocate them if they are zero.  */
> > +# define ADJUST_DYN_INFO(tag) \
> > +      do                                                                   \
> > +     if (info[tag] != NULL && info[tag]->d_un.d_ptr != 0)                  \
> > +         info[tag]->d_un.d_ptr += l_addr;                                  \
> > +      while (0)
> > +
>
> Maybe use '{' and '}' on the do ... while.

Fixed in v7.

> >  # if ! ELF_MACHINE_NO_RELA
> >        ADJUST_DYN_INFO (DT_RELA);
> >  # endif
> >  # if ! ELF_MACHINE_NO_REL
> >        ADJUST_DYN_INFO (DT_REL);
> >  # endif
> > -      ADJUST_DYN_INFO (DT_RELR);
> > -      ADJUST_DYN_INFO (DT_JMPREL);
> > -      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
> > -      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
> >  # undef ADJUST_DYN_INFO
> >      }
> >    if (info[DT_PLTREL] != NULL)
>
> Ok.

Thanks.
  

Patch

diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
index d04c457e55..252f407a12 100644
--- a/elf/dynamic-link.h
+++ b/elf/dynamic-link.h
@@ -84,7 +84,9 @@  elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
 	     __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; }  \
       ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } };			      \
 									      \
-    if ((map)->l_info[DT_##RELOC])					      \
+    /* With DT_RELR, DT_RELA/DT_REL can have zero value.  */		      \
+    if ((map)->l_info[DT_##RELOC]					      \
+	&& (map)->l_info[DT_##RELOC]->d_un.d_ptr != 0)			      \
       {									      \
 	ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]);		      \
 	ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val;	      \
@@ -98,6 +100,8 @@  elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[],
 	ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]);		      \
 	ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val;	      \
 									      \
+	if (ranges[0].start == 0)					      \
+	  ranges[0].start = start;					      \
 	if (ranges[0].start + ranges[0].size == (start + size))		      \
 	  ranges[0].size -= size;					      \
 	if (!(do_lazy)							      \
diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
index 6c2a3a12b1..f4b957684b 100644
--- a/elf/get-dynamic-info.h
+++ b/elf/get-dynamic-info.h
@@ -83,16 +83,26 @@  elf_get_dynamic_info (struct link_map *l, bool bootstrap,
       ADJUST_DYN_INFO (DT_PLTGOT);
       ADJUST_DYN_INFO (DT_STRTAB);
       ADJUST_DYN_INFO (DT_SYMTAB);
+      ADJUST_DYN_INFO (DT_RELR);
+      ADJUST_DYN_INFO (DT_JMPREL);
+      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
+      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
+# undef ADJUST_DYN_INFO
+
+      /* DT_RELA/DT_REL are mandatory.  But they may have zero value if
+	 there is DT_RELR.  Don't relocate them if they are zero.  */
+# define ADJUST_DYN_INFO(tag) \
+      do								      \
+	if (info[tag] != NULL && info[tag]->d_un.d_ptr != 0)		      \
+         info[tag]->d_un.d_ptr += l_addr;				      \
+      while (0)
+
 # if ! ELF_MACHINE_NO_RELA
       ADJUST_DYN_INFO (DT_RELA);
 # endif
 # if ! ELF_MACHINE_NO_REL
       ADJUST_DYN_INFO (DT_REL);
 # endif
-      ADJUST_DYN_INFO (DT_RELR);
-      ADJUST_DYN_INFO (DT_JMPREL);
-      ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
-      ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
 # undef ADJUST_DYN_INFO
     }
   if (info[DT_PLTREL] != NULL)