public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc] elf: Properly handle zero DT_RELA/DT_REL values
@ 2022-04-26 17:18 H.J. Lu
  0 siblings, 0 replies; only message in thread
From: H.J. Lu @ 2022-04-26 17:18 UTC (permalink / raw)
  To: glibc-cvs

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

commit 60196d2ef2850f222c7c93818ccb4d9cdbd77276
Author: H.J. Lu <hjl.tools@gmail.com>
Date:   Tue Jan 4 05:47:21 2022 -0800

    elf: Properly handle zero DT_RELA/DT_REL values
    
    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.

Diff:
---
 elf/dynamic-link.h     |  6 +++++-
 elf/get-dynamic-info.h | 24 ++++++++++++++++++------
 2 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/elf/dynamic-link.h b/elf/dynamic-link.h
index 019088f248..debbb8c9f7 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] != NULL				      \
+	&& (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..0314baf020 100644
--- a/elf/get-dynamic-info.h
+++ b/elf/get-dynamic-info.h
@@ -75,24 +75,36 @@ elf_get_dynamic_info (struct link_map *l, bool bootstrap,
 
 # define ADJUST_DYN_INFO(tag) \
       do								      \
-	if (info[tag] != NULL)						      \
-         info[tag]->d_un.d_ptr += l_addr;				      \
+	{								      \
+	  if (info[tag] != NULL)					      \
+	  info[tag]->d_un.d_ptr += l_addr;				      \
+	}								      \
       while (0)
 
       ADJUST_DYN_INFO (DT_HASH);
       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)


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

only message in thread, other threads:[~2022-04-26 17:18 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-26 17:18 [glibc] elf: Properly handle zero DT_RELA/DT_REL values H.J. Lu

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