7729eb
commit f42373f911a28d34325a5bfc1ed5a962c89da7eb
7729eb
Author: H.J. Lu <hjl.tools@gmail.com>
7729eb
Date:   Thu Sep 16 08:15:29 2021 -0700
7729eb
7729eb
    ld.so: Replace DL_RO_DYN_SECTION with dl_relocate_ld [BZ #28340]
7729eb
    
7729eb
    We can't relocate entries in dynamic section if it is readonly:
7729eb
    
7729eb
    1. Add a l_ld_readonly field to struct link_map to indicate if dynamic
7729eb
    section is readonly and set it based on p_flags of PT_DYNAMIC segment.
7729eb
    2. Replace DL_RO_DYN_SECTION with dl_relocate_ld to decide if dynamic
7729eb
    section should be relocated.
7729eb
    3. Remove DL_RO_DYN_TEMP_CNT.
7729eb
    4. Don't use a static dynamic section to make readonly dynamic section
7729eb
    in vDSO writable.
7729eb
    5. Remove the temp argument from elf_get_dynamic_info.
7729eb
    
7729eb
    This fixes BZ #28340.
7729eb
    
7729eb
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
7729eb
    (cherry picked from commit b413280cfb16834450f66f554bc0d618bb513851)
7729eb
7729eb
diff --git a/elf/Makefile b/elf/Makefile
7729eb
index d05f410592723f09..de854b4d1a815bd3 100644
7729eb
--- a/elf/Makefile
7729eb
+++ b/elf/Makefile
7729eb
@@ -224,7 +224,7 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \
7729eb
 	 tst-tls-ie tst-tls-ie-dlmopen argv0test \
7729eb
 	 tst-glibc-hwcaps tst-glibc-hwcaps-prepend tst-glibc-hwcaps-mask \
7729eb
 	 tst-tls20 tst-tls21 tst-dlmopen-dlerror tst-dlmopen-gethostbyname \
7729eb
-	 tst-dl-is_dso
7729eb
+	 tst-dl-is_dso tst-ro-dynamic
7729eb
 #	 reldep9
7729eb
 tests-internal += loadtest unload unload2 circleload1 \
7729eb
 	 neededtest neededtest2 neededtest3 neededtest4 \
7729eb
@@ -357,7 +357,7 @@ modules-names = testobj1 testobj2 testobj3 testobj4 testobj5 testobj6 \
7729eb
 		libmarkermod4-1 libmarkermod4-2 libmarkermod4-3 libmarkermod4-4 \
7729eb
 		tst-tls20mod-bad tst-tls21mod tst-dlmopen-dlerror-mod \
7729eb
 		tst-auxvalmod \
7729eb
-		tst-dlmopen-gethostbyname-mod \
7729eb
+		tst-dlmopen-gethostbyname-mod tst-ro-dynamic-mod \
7729eb
 
7729eb
 # Most modules build with _ISOMAC defined, but those filtered out
7729eb
 # depend on internal headers.
7729eb
@@ -1906,3 +1906,10 @@ $(objpfx)tst-getauxval-static.out: $(objpfx)tst-auxvalmod.so
7729eb
 tst-getauxval-static-ENV = LD_LIBRARY_PATH=$(objpfx):$(common-objpfx)
7729eb
 
7729eb
 $(objpfx)tst-dlmopen-gethostbyname.out: $(objpfx)tst-dlmopen-gethostbyname-mod.so
7729eb
+
7729eb
+$(objpfx)tst-ro-dynamic: $(objpfx)tst-ro-dynamic-mod.so
7729eb
+$(objpfx)tst-ro-dynamic-mod.so: $(objpfx)tst-ro-dynamic-mod.os \
7729eb
+  tst-ro-dynamic-mod.map
7729eb
+	$(LINK.o) -nostdlib -nostartfiles -shared -o $@ \
7729eb
+		-Wl,--script=tst-ro-dynamic-mod.map \
7729eb
+		$(objpfx)tst-ro-dynamic-mod.os
7729eb
diff --git a/elf/dl-load.c b/elf/dl-load.c
7729eb
index 650e4edc35e5e582..4445c28ef3fb4a7e 100644
7729eb
--- a/elf/dl-load.c
7729eb
+++ b/elf/dl-load.c
7729eb
@@ -1149,6 +1149,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
7729eb
 		 such a segment to avoid a crash later.  */
7729eb
 	      l->l_ld = (void *) ph->p_vaddr;
7729eb
 	      l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
7729eb
+	      l->l_ld_readonly = (ph->p_flags & PF_W) == 0;
7729eb
 	    }
7729eb
 	  break;
7729eb
 
7729eb
@@ -1292,7 +1293,7 @@ _dl_map_object_from_fd (const char *name, const char *origname, int fd,
7729eb
   else
7729eb
     l->l_ld = (ElfW(Dyn) *) ((ElfW(Addr)) l->l_ld + l->l_addr);
7729eb
 
7729eb
-  elf_get_dynamic_info (l, NULL);
7729eb
+  elf_get_dynamic_info (l);
7729eb
 
7729eb
   /* Make sure we are not dlopen'ing an object that has the
7729eb
      DF_1_NOOPEN flag set, or a PIE object.  */
7729eb
diff --git a/elf/dl-reloc-static-pie.c b/elf/dl-reloc-static-pie.c
7729eb
index d5bd2f31e9ca50ce..2fb02d727654c87d 100644
7729eb
--- a/elf/dl-reloc-static-pie.c
7729eb
+++ b/elf/dl-reloc-static-pie.c
7729eb
@@ -40,7 +40,17 @@ _dl_relocate_static_pie (void)
7729eb
 
7729eb
   /* Read our own dynamic section and fill in the info array.  */
7729eb
   main_map->l_ld = ((void *) main_map->l_addr + elf_machine_dynamic ());
7729eb
-  elf_get_dynamic_info (main_map, NULL);
7729eb
+
7729eb
+  const ElfW(Phdr) *ph, *phdr = GL(dl_phdr);
7729eb
+  size_t phnum = GL(dl_phnum);
7729eb
+  for (ph = phdr; ph < &phdr[phnum]; ++ph)
7729eb
+    if (ph->p_type == PT_DYNAMIC)
7729eb
+      {
7729eb
+	main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0;
7729eb
+	break;
7729eb
+      }
7729eb
+
7729eb
+  elf_get_dynamic_info (main_map);
7729eb
 
7729eb
 # ifdef ELF_MACHINE_BEFORE_RTLD_RELOC
7729eb
   ELF_MACHINE_BEFORE_RTLD_RELOC (main_map->l_info);
7729eb
diff --git a/elf/get-dynamic-info.h b/elf/get-dynamic-info.h
7729eb
index d8ec32377d37635c..4aa2058abf6443c9 100644
7729eb
--- a/elf/get-dynamic-info.h
7729eb
+++ b/elf/get-dynamic-info.h
7729eb
@@ -28,7 +28,7 @@ static
7729eb
 auto
7729eb
 #endif
7729eb
 inline void __attribute__ ((unused, always_inline))
7729eb
-elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
7729eb
+elf_get_dynamic_info (struct link_map *l)
7729eb
 {
7729eb
 #if __ELF_NATIVE_CLASS == 32
7729eb
   typedef Elf32_Word d_tag_utype;
7729eb
@@ -69,28 +69,15 @@ elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
7729eb
       info[i] = dyn;
7729eb
     }
7729eb
 
7729eb
-#define DL_RO_DYN_TEMP_CNT	8
7729eb
-
7729eb
-#ifndef DL_RO_DYN_SECTION
7729eb
   /* Don't adjust .dynamic unnecessarily.  */
7729eb
-  if (l->l_addr != 0)
7729eb
+  if (l->l_addr != 0 && dl_relocate_ld (l))
7729eb
     {
7729eb
       ElfW(Addr) l_addr = l->l_addr;
7729eb
-      int cnt = 0;
7729eb
 
7729eb
 # define ADJUST_DYN_INFO(tag) \
7729eb
       do								      \
7729eb
 	if (info[tag] != NULL)						      \
7729eb
-	  {								      \
7729eb
-	    if (temp)							      \
7729eb
-	      {								      \
7729eb
-		temp[cnt].d_tag = info[tag]->d_tag;			      \
7729eb
-		temp[cnt].d_un.d_ptr = info[tag]->d_un.d_ptr + l_addr;	      \
7729eb
-		info[tag] = temp + cnt++;				      \
7729eb
-	      }								      \
7729eb
-	    else							      \
7729eb
-	      info[tag]->d_un.d_ptr += l_addr;				      \
7729eb
-	  }								      \
7729eb
+         info[tag]->d_un.d_ptr += l_addr;				      \
7729eb
       while (0)
7729eb
 
7729eb
       ADJUST_DYN_INFO (DT_HASH);
7729eb
@@ -107,9 +94,7 @@ elf_get_dynamic_info (struct link_map *l, ElfW(Dyn) *temp)
7729eb
       ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM));
7729eb
       ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH));
7729eb
 # undef ADJUST_DYN_INFO
7729eb
-      assert (cnt <= DL_RO_DYN_TEMP_CNT);
7729eb
     }
7729eb
-#endif
7729eb
   if (info[DT_PLTREL] != NULL)
7729eb
     {
7729eb
 #if ELF_MACHINE_NO_RELA
7729eb
diff --git a/elf/rtld.c b/elf/rtld.c
7729eb
index 08cf50145a1c01ce..405166d62b34847c 100644
7729eb
--- a/elf/rtld.c
7729eb
+++ b/elf/rtld.c
7729eb
@@ -464,6 +464,7 @@ _dl_start_final (void *arg, struct dl_start_final_info *info)
7729eb
 #ifndef DONT_USE_BOOTSTRAP_MAP
7729eb
   GL(dl_rtld_map).l_addr = info->l.l_addr;
7729eb
   GL(dl_rtld_map).l_ld = info->l.l_ld;
7729eb
+  GL(dl_rtld_map).l_ld_readonly = info->l.l_ld_readonly;
7729eb
   memcpy (GL(dl_rtld_map).l_info, info->l.l_info,
7729eb
 	  sizeof GL(dl_rtld_map).l_info);
7729eb
   GL(dl_rtld_map).l_mach = info->l.l_mach;
7729eb
@@ -547,7 +548,7 @@ _dl_start (void *arg)
7729eb
 
7729eb
   /* Read our own dynamic section and fill in the info array.  */
7729eb
   bootstrap_map.l_ld = (void *) bootstrap_map.l_addr + elf_machine_dynamic ();
7729eb
-  elf_get_dynamic_info (&bootstrap_map, NULL);
7729eb
+  elf_get_dynamic_info (&bootstrap_map);
7729eb
 
7729eb
 #if NO_TLS_OFFSET != 0
7729eb
   bootstrap_map.l_tls_offset = NO_TLS_OFFSET;
7729eb
@@ -1469,6 +1470,7 @@ dl_main (const ElfW(Phdr) *phdr,
7729eb
 	/* This tells us where to find the dynamic section,
7729eb
 	   which tells us everything we need to do.  */
7729eb
 	main_map->l_ld = (void *) main_map->l_addr + ph->p_vaddr;
7729eb
+	main_map->l_ld_readonly = (ph->p_flags & PF_W) == 0;
7729eb
 	break;
7729eb
       case PT_INTERP:
7729eb
 	/* This "interpreter segment" was used by the program loader to
7729eb
@@ -1614,7 +1616,7 @@ dl_main (const ElfW(Phdr) *phdr,
7729eb
   if (! rtld_is_main)
7729eb
     {
7729eb
       /* Extract the contents of the dynamic section for easy access.  */
7729eb
-      elf_get_dynamic_info (main_map, NULL);
7729eb
+      elf_get_dynamic_info (main_map);
7729eb
 
7729eb
       /* If the main map is libc.so, update the base namespace to
7729eb
 	 refer to this map.  If libc.so is loaded later, this happens
7729eb
diff --git a/elf/setup-vdso.h b/elf/setup-vdso.h
7729eb
index 86c491e49c98345d..f44748bc9858e5fd 100644
7729eb
--- a/elf/setup-vdso.h
7729eb
+++ b/elf/setup-vdso.h
7729eb
@@ -33,8 +33,6 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
7729eb
 				       0, LM_ID_BASE);
7729eb
   if (__glibc_likely (l != NULL))
7729eb
     {
7729eb
-      static ElfW(Dyn) dyn_temp[DL_RO_DYN_TEMP_CNT] attribute_relro;
7729eb
-
7729eb
       l->l_phdr = ((const void *) GLRO(dl_sysinfo_dso)
7729eb
 		   + GLRO(dl_sysinfo_dso)->e_phoff);
7729eb
       l->l_phnum = GLRO(dl_sysinfo_dso)->e_phnum;
7729eb
@@ -45,6 +43,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
7729eb
 	    {
7729eb
 	      l->l_ld = (void *) ph->p_vaddr;
7729eb
 	      l->l_ldnum = ph->p_memsz / sizeof (ElfW(Dyn));
7729eb
+	      l->l_ld_readonly = (ph->p_flags & PF_W) == 0;
7729eb
 	    }
7729eb
 	  else if (ph->p_type == PT_LOAD)
7729eb
 	    {
7729eb
@@ -65,7 +64,7 @@ setup_vdso (struct link_map *main_map __attribute__ ((unused)),
7729eb
       l->l_map_end += l->l_addr;
7729eb
       l->l_text_end += l->l_addr;
7729eb
       l->l_ld = (void *) ((ElfW(Addr)) l->l_ld + l->l_addr);
7729eb
-      elf_get_dynamic_info (l, dyn_temp);
7729eb
+      elf_get_dynamic_info (l);
7729eb
       _dl_setup_hash (l);
7729eb
       l->l_relocated = 1;
7729eb
 
7729eb
diff --git a/elf/tst-ro-dynamic-mod.c b/elf/tst-ro-dynamic-mod.c
7729eb
new file mode 100644
7729eb
index 0000000000000000..6d99925964c50caf
7729eb
--- /dev/null
7729eb
+++ b/elf/tst-ro-dynamic-mod.c
7729eb
@@ -0,0 +1,19 @@
7729eb
+/* Test case for DSO with readonly dynamic section.
7729eb
+   Copyright (C) 2021 Free Software Foundation, Inc.
7729eb
+   This file is part of the GNU C Library.
7729eb
+
7729eb
+   The GNU C Library is free software; you can redistribute it and/or
7729eb
+   modify it under the terms of the GNU Lesser General Public
7729eb
+   License as published by the Free Software Foundation; either
7729eb
+   version 2.1 of the License, or (at your option) any later version.
7729eb
+
7729eb
+   The GNU C Library is distributed in the hope that it will be useful,
7729eb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
7729eb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7729eb
+   Lesser General Public License for more details.
7729eb
+
7729eb
+   You should have received a copy of the GNU Lesser General Public
7729eb
+   License along with the GNU C Library; if not, see
7729eb
+   <https://www.gnu.org/licenses/>.  */
7729eb
+
7729eb
+int foo = -1;
7729eb
diff --git a/elf/tst-ro-dynamic-mod.map b/elf/tst-ro-dynamic-mod.map
7729eb
new file mode 100644
7729eb
index 0000000000000000..2fe4a2998cddd587
7729eb
--- /dev/null
7729eb
+++ b/elf/tst-ro-dynamic-mod.map
7729eb
@@ -0,0 +1,16 @@
7729eb
+SECTIONS
7729eb
+{
7729eb
+ . = SIZEOF_HEADERS;
7729eb
+ .dynamic : { *(.dynamic) } :text :dynamic
7729eb
+ .rodata : { *(.data*) *(.bss*) } :text
7729eb
+ /DISCARD/ : {
7729eb
+  *(.note.gnu.property)
7729eb
+ }
7729eb
+ .note : { *(.note.*) } :text :note
7729eb
+}
7729eb
+PHDRS
7729eb
+{
7729eb
+ text PT_LOAD FLAGS(5) FILEHDR PHDRS;
7729eb
+ dynamic PT_DYNAMIC FLAGS(4);
7729eb
+ note PT_NOTE FLAGS(4);
7729eb
+}
7729eb
diff --git a/elf/tst-ro-dynamic.c b/elf/tst-ro-dynamic.c
7729eb
new file mode 100644
7729eb
index 0000000000000000..3a18f8789ad386bb
7729eb
--- /dev/null
7729eb
+++ b/elf/tst-ro-dynamic.c
7729eb
@@ -0,0 +1,31 @@
7729eb
+/* Test case for DSO with readonly dynamic section.
7729eb
+   Copyright (C) 2021 Free Software Foundation, Inc.
7729eb
+   This file is part of the GNU C Library.
7729eb
+
7729eb
+   The GNU C Library is free software; you can redistribute it and/or
7729eb
+   modify it under the terms of the GNU Lesser General Public
7729eb
+   License as published by the Free Software Foundation; either
7729eb
+   version 2.1 of the License, or (at your option) any later version.
7729eb
+
7729eb
+   The GNU C Library is distributed in the hope that it will be useful,
7729eb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
7729eb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7729eb
+   Lesser General Public License for more details.
7729eb
+
7729eb
+   You should have received a copy of the GNU Lesser General Public
7729eb
+   License along with the GNU C Library; if not, see
7729eb
+   <https://www.gnu.org/licenses/>.  */
7729eb
+
7729eb
+#include <support/check.h>
7729eb
+#include <support/test-driver.h>
7729eb
+
7729eb
+extern int foo;
7729eb
+
7729eb
+static int
7729eb
+do_test (void)
7729eb
+{
7729eb
+  TEST_COMPARE (foo, -1);
7729eb
+  return 0;
7729eb
+}
7729eb
+
7729eb
+#include <support/test-driver.c>
7729eb
diff --git a/include/link.h b/include/link.h
7729eb
index 4af16cb59639a077..c46aced9f7b43ba0 100644
7729eb
--- a/include/link.h
7729eb
+++ b/include/link.h
7729eb
@@ -205,6 +205,7 @@ struct link_map
7729eb
     unsigned int l_free_initfini:1; /* Nonzero if l_initfini can be
7729eb
 				       freed, ie. not allocated with
7729eb
 				       the dummy malloc in ld.so.  */
7729eb
+    unsigned int l_ld_readonly:1; /* Nonzero if dynamic section is readonly.  */
7729eb
 
7729eb
     /* NODELETE status of the map.  Only valid for maps of type
7729eb
        lt_loaded.  Lazy binding sets l_nodelete_active directly,
7729eb
@@ -342,6 +343,8 @@ struct link_map
7729eb
     unsigned long long int l_serial;
7729eb
   };
7729eb
 
7729eb
+#include <dl-relocate-ld.h>
7729eb
+
7729eb
 /* Information used by audit modules.  For most link maps, this data
7729eb
    immediate follows the link map in memory.  For the dynamic linker,
7729eb
    it is allocated separately.  See link_map_audit_state in
7729eb
diff --git a/sysdeps/generic/dl-relocate-ld.h b/sysdeps/generic/dl-relocate-ld.h
7729eb
new file mode 100644
7729eb
index 0000000000000000..5fae206db9941e97
7729eb
--- /dev/null
7729eb
+++ b/sysdeps/generic/dl-relocate-ld.h
7729eb
@@ -0,0 +1,32 @@
7729eb
+/* Check if dynamic section should be relocated.  Generic version.
7729eb
+   Copyright (C) 2021 Free Software Foundation, Inc.
7729eb
+   This file is part of the GNU C Library.
7729eb
+
7729eb
+   The GNU C Library is free software; you can redistribute it and/or
7729eb
+   modify it under the terms of the GNU Lesser General Public
7729eb
+   License as published by the Free Software Foundation; either
7729eb
+   version 2.1 of the License, or (at your option) any later version.
7729eb
+
7729eb
+   The GNU C Library is distributed in the hope that it will be useful,
7729eb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
7729eb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7729eb
+   Lesser General Public License for more details.
7729eb
+
7729eb
+   You should have received a copy of the GNU Lesser General Public
7729eb
+   License along with the GNU C Library; if not, see
7729eb
+   <https://www.gnu.org/licenses/>.  */
7729eb
+
7729eb
+#ifndef _DL_RELOCATE_LD_H
7729eb
+#define _DL_RELOCATE_LD_H
7729eb
+
7729eb
+/* Return true if dynamic section in the shared library L should be
7729eb
+   relocated.  */
7729eb
+
7729eb
+static inline bool
7729eb
+dl_relocate_ld (const struct link_map *l)
7729eb
+{
7729eb
+  /* Don't relocate dynamic section if it is readonly  */
7729eb
+  return !l->l_ld_readonly;
7729eb
+}
7729eb
+
7729eb
+#endif /* _DL_RELOCATE_LD_H */
7729eb
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
7729eb
index 1ceb9c3212c148ba..ed10953f34b96c49 100644
7729eb
--- a/sysdeps/generic/ldsodefs.h
7729eb
+++ b/sysdeps/generic/ldsodefs.h
7729eb
@@ -75,11 +75,8 @@ __BEGIN_DECLS
7729eb
   have to be accessed via the D_PTR macro.  The macro is needed since for
7729eb
   most architectures the entry is already relocated - but for some not
7729eb
   and we need to relocate at access time.  */
7729eb
-#ifdef DL_RO_DYN_SECTION
7729eb
-# define D_PTR(map, i) ((map)->i->d_un.d_ptr + (map)->l_addr)
7729eb
-#else
7729eb
-# define D_PTR(map, i) (map)->i->d_un.d_ptr
7729eb
-#endif
7729eb
+#define D_PTR(map, i) \
7729eb
+  ((map)->i->d_un.d_ptr + (dl_relocate_ld (map) ? 0 : (map)->l_addr))
7729eb
 
7729eb
 /* Result of the lookup functions and how to retrieve the base address.  */
7729eb
 typedef struct link_map *lookup_t;
7729eb
diff --git a/sysdeps/mips/dl-relocate-ld.h b/sysdeps/mips/dl-relocate-ld.h
7729eb
new file mode 100644
7729eb
index 0000000000000000..0c18d9a567cad54f
7729eb
--- /dev/null
7729eb
+++ b/sysdeps/mips/dl-relocate-ld.h
7729eb
@@ -0,0 +1,32 @@
7729eb
+/* Check if dynamic section should be relocated.  MIPS version.
7729eb
+   Copyright (C) 2021 Free Software Foundation, Inc.
7729eb
+   This file is part of the GNU C Library.
7729eb
+
7729eb
+   The GNU C Library is free software; you can redistribute it and/or
7729eb
+   modify it under the terms of the GNU Lesser General Public
7729eb
+   License as published by the Free Software Foundation; either
7729eb
+   version 2.1 of the License, or (at your option) any later version.
7729eb
+
7729eb
+   The GNU C Library is distributed in the hope that it will be useful,
7729eb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
7729eb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7729eb
+   Lesser General Public License for more details.
7729eb
+
7729eb
+   You should have received a copy of the GNU Lesser General Public
7729eb
+   License along with the GNU C Library; if not, see
7729eb
+   <https://www.gnu.org/licenses/>.  */
7729eb
+
7729eb
+#ifndef _DL_RELOCATE_LD_H
7729eb
+#define _DL_RELOCATE_LD_H
7729eb
+
7729eb
+/* Return true if dynamic section in the shared library L should be
7729eb
+   relocated.  */
7729eb
+
7729eb
+static inline bool
7729eb
+dl_relocate_ld (const struct link_map *l)
7729eb
+{
7729eb
+  /* Never relocate dynamic section.  */
7729eb
+  return false;
7729eb
+}
7729eb
+
7729eb
+#endif /* _DL_RELOCATE_LD_H */
7729eb
diff --git a/sysdeps/mips/ldsodefs.h b/sysdeps/mips/ldsodefs.h
7729eb
index 4db7c60e38e42ef2..36fd09a8bdb4cacf 100644
7729eb
--- a/sysdeps/mips/ldsodefs.h
7729eb
+++ b/sysdeps/mips/ldsodefs.h
7729eb
@@ -75,10 +75,6 @@ struct La_mips_64_retval;
7729eb
 					  struct La_mips_64_retval *,	    \
7729eb
 					  const char *);
7729eb
 
7729eb
-/* The MIPS ABI specifies that the dynamic section has to be read-only.  */
7729eb
-
7729eb
-#define DL_RO_DYN_SECTION 1
7729eb
-
7729eb
 #include_next <ldsodefs.h>
7729eb
 
7729eb
 /* The 64-bit MIPS ELF ABI uses an unusual reloc format.  Each
7729eb
diff --git a/sysdeps/riscv/dl-relocate-ld.h b/sysdeps/riscv/dl-relocate-ld.h
7729eb
new file mode 100644
7729eb
index 0000000000000000..10327454b17a7097
7729eb
--- /dev/null
7729eb
+++ b/sysdeps/riscv/dl-relocate-ld.h
7729eb
@@ -0,0 +1,32 @@
7729eb
+/* Check if dynamic section should be relocated.  RISC-V version.
7729eb
+   Copyright (C) 2021 Free Software Foundation, Inc.
7729eb
+   This file is part of the GNU C Library.
7729eb
+
7729eb
+   The GNU C Library is free software; you can redistribute it and/or
7729eb
+   modify it under the terms of the GNU Lesser General Public
7729eb
+   License as published by the Free Software Foundation; either
7729eb
+   version 2.1 of the License, or (at your option) any later version.
7729eb
+
7729eb
+   The GNU C Library is distributed in the hope that it will be useful,
7729eb
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
7729eb
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
7729eb
+   Lesser General Public License for more details.
7729eb
+
7729eb
+   You should have received a copy of the GNU Lesser General Public
7729eb
+   License along with the GNU C Library; if not, see
7729eb
+   <https://www.gnu.org/licenses/>.  */
7729eb
+
7729eb
+#ifndef _DL_RELOCATE_LD_H
7729eb
+#define _DL_RELOCATE_LD_H
7729eb
+
7729eb
+/* Return true if dynamic section in the shared library L should be
7729eb
+   relocated.  */
7729eb
+
7729eb
+static inline bool
7729eb
+dl_relocate_ld (const struct link_map *l)
7729eb
+{
7729eb
+  /* Never relocate dynamic section for ABI compatibility.  */
7729eb
+  return false;
7729eb
+}
7729eb
+
7729eb
+#endif /* _DL_RELOCATE_LD_H */
7729eb
diff --git a/sysdeps/riscv/ldsodefs.h b/sysdeps/riscv/ldsodefs.h
7729eb
index 0c696714a7a93cff..8947ffe4b5026a2e 100644
7729eb
--- a/sysdeps/riscv/ldsodefs.h
7729eb
+++ b/sysdeps/riscv/ldsodefs.h
7729eb
@@ -38,11 +38,6 @@ struct La_riscv_retval;
7729eb
 				       struct La_riscv_retval *,	\
7729eb
 				       const char *);
7729eb
 
7729eb
-/* Although the RISC-V ABI does not specify that the dynamic section has
7729eb
-   to be read-only, it needs to be kept for ABI compatibility.  */
7729eb
-
7729eb
-#define DL_RO_DYN_SECTION 1
7729eb
-
7729eb
 #include_next <ldsodefs.h>
7729eb
 
7729eb
 #endif