diff mbox series

[2/3] elf: Introduce _dl_relocate_object_no_relro

Message ID 200b2c6531a6a325c0b8824fe411ee70e622ed2f.1722934012.git.fweimer@redhat.com
State New
Headers show
Series Make ld.so self-relocation/main malloc switch more robust | expand

Commit Message

Florian Weimer Aug. 6, 2024, 8:48 a.m. UTC
And make _dl_protect_relro apply RELRO conditionally.
---
 elf/dl-reloc.c             | 24 ++++++++++++++----------
 sysdeps/generic/ldsodefs.h |  7 +++++++
 2 files changed, 21 insertions(+), 10 deletions(-)
diff mbox series

Patch

diff --git a/elf/dl-reloc.c b/elf/dl-reloc.c
index 4bf7aec88b..bdb271fdfc 100644
--- a/elf/dl-reloc.c
+++ b/elf/dl-reloc.c
@@ -202,12 +202,9 @@  resolve_map (lookup_t l, struct r_scope_elem *scope[], const ElfW(Sym) **ref,
 #include "dynamic-link.h"
 
 void
-_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
-		     int reloc_mode, int consider_profiling)
+_dl_relocate_object_no_relro (struct link_map *l, struct r_scope_elem *scope[],
+			      int reloc_mode, int consider_profiling)
 {
-  if (l->l_relocated)
-    return;
-
   struct textrels
   {
     caddr_t start;
@@ -342,17 +339,24 @@  _dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
 
       textrels = textrels->next;
     }
-
-  /* In case we can protect the data now that the relocations are
-     done, do it.  */
-  if (l->l_relro_size != 0)
-    _dl_protect_relro (l);
 }
 
+void
+_dl_relocate_object (struct link_map *l, struct r_scope_elem *scope[],
+		     int reloc_mode, int consider_profiling)
+{
+  if (l->l_relocated)
+    return;
+  _dl_relocate_object_no_relro (l, scope, reloc_mode, consider_profiling);
+  _dl_protect_relro (l);
+}
 
 void
 _dl_protect_relro (struct link_map *l)
 {
+  if (l->l_relro_size == 0)
+    return;
+
   ElfW(Addr) start = ALIGN_DOWN((l->l_addr
 				 + l->l_relro_addr),
 				GLRO(dl_pagesize));
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 259ce2e7d6..91447a5e77 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1014,6 +1014,13 @@  extern void _dl_relocate_object (struct link_map *map,
 				 int reloc_mode, int consider_profiling)
      attribute_hidden;
 
+/* Perform relocation, but do not apply RELRO.  Does not check
+   L->relocated.  Otherwise the same as _dl_relocate_object.  */
+void _dl_relocate_object_no_relro (struct link_map *map,
+				   struct r_scope_elem *scope[],
+				   int reloc_mode, int consider_profiling)
+     attribute_hidden;
+
 /* Protect PT_GNU_RELRO area.  */
 extern void _dl_protect_relro (struct link_map *map) attribute_hidden;