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