diff mbox series

[02/14] use initial mmap also for ET_EXEC

Message ID 20230518082854.3903342-3-stsp2@yandex.ru
State New
Headers show
Series implement RTLD_NORELOCATE api [BZ #30007] | expand

Commit Message

stsp May 18, 2023, 8:28 a.m. UTC
This allows to replace the further anonymous mmaps with mprotect.
Which, in turn, will allow to split the protection stage in the
subsequent patches.

The test-suite was run on x86_64/64 and showed no regressions.

Signed-off-by: Stas Sergeev <stsp2@yandex.ru>
---
 elf/dl-map-segments.h | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/elf/dl-map-segments.h b/elf/dl-map-segments.h
index ed7675cabf..6a6127f773 100644
--- a/elf/dl-map-segments.h
+++ b/elf/dl-map-segments.h
@@ -129,7 +129,12 @@  _dl_map_segments (struct link_map *l, int fd,
   else
     {
       /* Remember which part of the address space this object uses.  */
-      l->l_map_start = c->mapstart + l->l_addr;
+      l->l_map_start = (ElfW(Addr)) __mmap ((caddr_t) l->l_addr + c->mapstart,
+                                            maplength, PROT_NONE,
+                                            MAP_ANON|MAP_PRIVATE|MAP_FIXED,
+                                            -1, 0);
+      if (__glibc_unlikely ((void *) l->l_map_start == MAP_FAILED))
+        return DL_MAP_SEGMENTS_ERROR_MAP_SEGMENT;
       l->l_map_end = l->l_map_start + maplength;
       l->l_contiguous = !has_holes;
     }
@@ -182,13 +187,11 @@  _dl_map_segments (struct link_map *l, int fd,
 
           if (zeroend > zeropage)
             {
-              /* Map the remaining zero pages in from the zero fill FD.  */
-              caddr_t mapat;
-              mapat = __mmap ((caddr_t) zeropage, zeroend - zeropage,
-                              c->prot, MAP_ANON|MAP_PRIVATE|MAP_FIXED,
-                              -1, 0);
-              if (__glibc_unlikely (mapat == MAP_FAILED))
-                return DL_MAP_SEGMENTS_ERROR_MAP_ZERO_FILL;
+              /* Protect the remaining zero pages.  */
+              if (__glibc_unlikely (__mprotect ((caddr_t) zeropage,
+                                                zeroend - zeropage,
+                                                c->prot) < 0))
+                return DL_MAP_SEGMENTS_ERROR_MPROTECT;
             }
         }