aboutsummaryrefslogtreecommitdiff
path: root/sysdeps
diff options
context:
space:
mode:
authorCupertino Miranda <cupertino.miranda@oracle.com>2025-08-22 11:37:00 +0100
committerAdhemerval Zanella <adhemerval.zanella@linaro.org>2025-08-27 10:45:45 -0300
commit3b2b88cceeb79f73a72367800d91599e2af4bb39 (patch)
treea4f7c0efe8d6c2a49ac448c977a639e609c3f45c /sysdeps
parent921e251e8f364d00fc753274095007275381ae65 (diff)
downloadglibc-3b2b88cceeb79f73a72367800d91599e2af4bb39.zip
glibc-3b2b88cceeb79f73a72367800d91599e2af4bb39.tar.gz
glibc-3b2b88cceeb79f73a72367800d91599e2af4bb39.tar.bz2
elf: early conversion of elf p_flags to mprotect flags
This patch replaces _dl_stack_flags global variable by _dl_stack_prot_flags. The advantage is that any convertion from p_flags to final used mprotect flags occurs at loading of p_flags. It avoids repeated spurious convertions of _dl_stack_flags, for example in allocate_thread_stack. This modification was suggested in: https://sourceware.org/pipermail/libc-alpha/2025-March/165537.html Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'sysdeps')
-rw-r--r--sysdeps/alpha/stackinfo.h6
-rw-r--r--sysdeps/arm/stackinfo.h6
-rw-r--r--sysdeps/generic/ldsodefs.h15
-rw-r--r--sysdeps/generic/stackinfo.h2
-rw-r--r--sysdeps/hppa/stackinfo.h6
-rw-r--r--sysdeps/i386/stackinfo.h6
-rw-r--r--sysdeps/m68k/stackinfo.h4
-rw-r--r--sysdeps/mach/htl/pt-stack-alloc.c2
-rw-r--r--sysdeps/mach/hurd/dl-execstack.c2
-rw-r--r--sysdeps/mach/hurd/htl/pt-sysdep.c1
-rw-r--r--sysdeps/microblaze/stackinfo.h6
-rw-r--r--sysdeps/mips/stackinfo.h6
-rw-r--r--sysdeps/or1k/stackinfo.h6
-rw-r--r--sysdeps/powerpc/powerpc32/stackinfo.h5
-rw-r--r--sysdeps/s390/stackinfo.h6
-rw-r--r--sysdeps/sh/stackinfo.h6
-rw-r--r--sysdeps/sparc/stackinfo.h6
-rw-r--r--sysdeps/unix/sysv/linux/dl-execstack.c3
-rw-r--r--sysdeps/unix/sysv/linux/machine-sp.h2
-rw-r--r--sysdeps/unix/sysv/linux/spawni.c5
-rw-r--r--sysdeps/x86_64/stackinfo.h6
21 files changed, 48 insertions, 59 deletions
diff --git a/sysdeps/alpha/stackinfo.h b/sysdeps/alpha/stackinfo.h
index a469964..d696477 100644
--- a/sysdeps/alpha/stackinfo.h
+++ b/sysdeps/alpha/stackinfo.h
@@ -26,8 +26,8 @@
/* On Alpha the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/arm/stackinfo.h b/sysdeps/arm/stackinfo.h
index 3068352..30608f7 100644
--- a/sysdeps/arm/stackinfo.h
+++ b/sysdeps/arm/stackinfo.h
@@ -26,8 +26,8 @@
/* On Arm the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 74025f1..31e9a6b 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -171,19 +171,6 @@ dl_symbol_visibility_binds_local_p (const ElfW(Sym) *sym)
# define ELF_RTYPE_CLASS_COPY 0
#endif
-/* ELF uses the PF_x macros to specify the segment permissions, mmap
- uses PROT_xxx. In most cases the three macros have the values 1, 2,
- and 3 but not in a matching order. The following macros allows
- converting from the PF_x values to PROT_xxx values. */
-#define PF_TO_PROT \
- ((PROT_READ << (PF_R * 4)) \
- | (PROT_WRITE << (PF_W * 4)) \
- | (PROT_EXEC << (PF_X * 4)) \
- | ((PROT_READ | PROT_WRITE) << ((PF_R | PF_W) * 4)) \
- | ((PROT_READ | PROT_EXEC) << ((PF_R | PF_X) * 4)) \
- | ((PROT_WRITE | PROT_EXEC) << (PF_W | PF_X) * 4) \
- | ((PROT_READ | PROT_WRITE | PROT_EXEC) << ((PF_R | PF_W | PF_X) * 4)))
-
/* The filename itself, or the main program name, if available. */
#define DSO_FILENAME(name) ((name)[0] ? (name) \
: (rtld_progname ?: "<main program>"))
@@ -416,7 +403,7 @@ struct rtld_global
#include <dl-procruntime.c>
/* Prevailing state of the stack, PF_X indicating it's executable. */
- EXTERN ElfW(Word) _dl_stack_flags;
+ EXTERN int _dl_stack_prot_flags;
/* Flag signalling whether there are gaps in the module ID allocation. */
EXTERN bool _dl_tls_dtv_gaps;
diff --git a/sysdeps/generic/stackinfo.h b/sysdeps/generic/stackinfo.h
index 8abbb3d..ab3e72e 100644
--- a/sysdeps/generic/stackinfo.h
+++ b/sysdeps/generic/stackinfo.h
@@ -24,6 +24,6 @@
#include <elf.h>
#define _STACK_GROWS_DOWN 1
-#define DEFAULT_STACK_PERMS (PF_R|PF_W)
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE)
#endif
diff --git a/sysdeps/hppa/stackinfo.h b/sysdeps/hppa/stackinfo.h
index 53bb11f..22920d2 100644
--- a/sysdeps/hppa/stackinfo.h
+++ b/sysdeps/hppa/stackinfo.h
@@ -23,9 +23,9 @@
#include <elf.h>
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
/* On PA the stack grows up. */
#define _STACK_GROWS_UP 1
diff --git a/sysdeps/i386/stackinfo.h b/sysdeps/i386/stackinfo.h
index 74e8227..8d7a46c 100644
--- a/sysdeps/i386/stackinfo.h
+++ b/sysdeps/i386/stackinfo.h
@@ -26,9 +26,9 @@
/* On x86 the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
/* Access to the stack pointer. The macros are used in alloca_account
for which they need to act as barriers as well, hence the additional
diff --git a/sysdeps/m68k/stackinfo.h b/sysdeps/m68k/stackinfo.h
index 7a757df..74c0af5 100644
--- a/sysdeps/m68k/stackinfo.h
+++ b/sysdeps/m68k/stackinfo.h
@@ -26,9 +26,9 @@
/* On m68k the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
is present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
/* Access to the stack pointer. */
#define stackinfo_get_sp() \
diff --git a/sysdeps/mach/htl/pt-stack-alloc.c b/sysdeps/mach/htl/pt-stack-alloc.c
index 64cc186..d9f3e24 100644
--- a/sysdeps/mach/htl/pt-stack-alloc.c
+++ b/sysdeps/mach/htl/pt-stack-alloc.c
@@ -34,7 +34,7 @@ __pthread_stack_alloc (void **stackaddr, size_t stacksize)
error_t err;
vm_prot_t prot = VM_PROT_READ | VM_PROT_WRITE;
- if (GL(dl_stack_flags) & PF_X)
+ if (GL(dl_stack_prot_flags) & PROT_EXEC)
prot |= VM_PROT_EXECUTE;
err = __vm_map (__mach_task_self (), (vm_offset_t *) stackaddr,
diff --git a/sysdeps/mach/hurd/dl-execstack.c b/sysdeps/mach/hurd/dl-execstack.c
index dc4719b..9e69169 100644
--- a/sysdeps/mach/hurd/dl-execstack.c
+++ b/sysdeps/mach/hurd/dl-execstack.c
@@ -38,7 +38,7 @@ _dl_make_stack_executable (const void *stack_endp)
return errno;
/* Remember that we changed the permission. */
- GL(dl_stack_flags) |= PF_X;
+ GL(dl_stack_prot_flags) |= PROT_EXEC;
return 0;
#else
diff --git a/sysdeps/mach/hurd/htl/pt-sysdep.c b/sysdeps/mach/hurd/htl/pt-sysdep.c
index 3505d31..735dc5b 100644
--- a/sysdeps/mach/hurd/htl/pt-sysdep.c
+++ b/sysdeps/mach/hurd/htl/pt-sysdep.c
@@ -25,6 +25,7 @@
#include <pt-internal.h>
#include <pthreadP.h>
+#include <stackinfo.h>
static void
reset_pthread_total (void)
diff --git a/sysdeps/microblaze/stackinfo.h b/sysdeps/microblaze/stackinfo.h
index 8960bd3..51d40a7 100644
--- a/sysdeps/microblaze/stackinfo.h
+++ b/sysdeps/microblaze/stackinfo.h
@@ -27,8 +27,8 @@
/* On MicroBlaze the stack grows down. */
# define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-# define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h. */
diff --git a/sysdeps/mips/stackinfo.h b/sysdeps/mips/stackinfo.h
index 6cce938..107a8ea 100644
--- a/sysdeps/mips/stackinfo.h
+++ b/sysdeps/mips/stackinfo.h
@@ -26,8 +26,8 @@
/* On MIPS the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/or1k/stackinfo.h b/sysdeps/or1k/stackinfo.h
index 39cf5c2..10a56c5 100644
--- a/sysdeps/or1k/stackinfo.h
+++ b/sysdeps/or1k/stackinfo.h
@@ -27,8 +27,8 @@
/* On or1k the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R | PF_W | PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/powerpc/powerpc32/stackinfo.h b/sysdeps/powerpc/powerpc32/stackinfo.h
index 31cba55..327e614 100644
--- a/sysdeps/powerpc/powerpc32/stackinfo.h
+++ b/sysdeps/powerpc/powerpc32/stackinfo.h
@@ -26,7 +26,8 @@
/* On PPC the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* PF_X can be overridden if PT_GNU_STACK is present but is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* PROT_EXEC can be overridden if PT_GNU_STACK is present but is presumed
+ absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/s390/stackinfo.h b/sysdeps/s390/stackinfo.h
index 657ab34..9be7644 100644
--- a/sysdeps/s390/stackinfo.h
+++ b/sysdeps/s390/stackinfo.h
@@ -26,8 +26,8 @@
/* On s390 the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/sh/stackinfo.h b/sysdeps/sh/stackinfo.h
index 8f7bf16..e502993 100644
--- a/sysdeps/sh/stackinfo.h
+++ b/sysdeps/sh/stackinfo.h
@@ -26,8 +26,8 @@
/* On SH the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/sparc/stackinfo.h b/sysdeps/sparc/stackinfo.h
index 23a74ed..a4a0eb1 100644
--- a/sysdeps/sparc/stackinfo.h
+++ b/sysdeps/sparc/stackinfo.h
@@ -26,8 +26,8 @@
/* On sparc the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
#endif /* stackinfo.h */
diff --git a/sysdeps/unix/sysv/linux/dl-execstack.c b/sysdeps/unix/sysv/linux/dl-execstack.c
index 6db9601..a59bd56 100644
--- a/sysdeps/unix/sysv/linux/dl-execstack.c
+++ b/sysdeps/unix/sysv/linux/dl-execstack.c
@@ -17,6 +17,7 @@
<https://www.gnu.org/licenses/>. */
#include <ldsodefs.h>
+#include <stackinfo.h>
int
_dl_make_stack_executable (const void *stack_endp)
@@ -36,7 +37,7 @@ _dl_make_stack_executable (const void *stack_endp)
return errno;
/* Remember that we changed the permission. */
- GL(dl_stack_flags) |= PF_X;
+ GL(dl_stack_prot_flags) |= PROT_EXEC;
return 0;
}
diff --git a/sysdeps/unix/sysv/linux/machine-sp.h b/sysdeps/unix/sysv/linux/machine-sp.h
index 6ab3ca6..bda90fe 100644
--- a/sysdeps/unix/sysv/linux/machine-sp.h
+++ b/sysdeps/unix/sysv/linux/machine-sp.h
@@ -19,6 +19,8 @@
#ifndef _MACHINE_SP_H
#define _MACHINE_SP_H
+#include <stackinfo.h>
+
/* Return the current stack pointer. */
static inline uintptr_t
__thread_stack_pointer (void)
diff --git a/sysdeps/unix/sysv/linux/spawni.c b/sysdeps/unix/sysv/linux/spawni.c
index eca1a84..bc8476f 100644
--- a/sysdeps/unix/sysv/linux/spawni.c
+++ b/sysdeps/unix/sysv/linux/spawni.c
@@ -348,9 +348,6 @@ __spawnix (int *pid, const char *file,
return errno;
}
- int prot = (PROT_READ | PROT_WRITE
- | ((GL (dl_stack_flags) & PF_X) ? PROT_EXEC : 0));
-
/* Add a slack area for child's stack. */
size_t argv_size = (argc * sizeof (void *)) + 512;
/* We need at least a few pages in case the compiler's stack checking is
@@ -361,7 +358,7 @@ __spawnix (int *pid, const char *file,
where it might use about 1k extra stack space). */
argv_size += (32 * 1024);
size_t stack_size = ALIGN_UP (argv_size, GLRO(dl_pagesize));
- void *stack = __mmap (NULL, stack_size, prot,
+ void *stack = __mmap (NULL, stack_size, GL (dl_stack_prot_flags),
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (__glibc_unlikely (stack == MAP_FAILED))
return errno;
diff --git a/sysdeps/x86_64/stackinfo.h b/sysdeps/x86_64/stackinfo.h
index 416d687..0e88e6d 100644
--- a/sysdeps/x86_64/stackinfo.h
+++ b/sysdeps/x86_64/stackinfo.h
@@ -32,9 +32,9 @@
/* On x86_64 the stack grows down. */
#define _STACK_GROWS_DOWN 1
-/* Default to an executable stack. PF_X can be overridden if PT_GNU_STACK is
- * present, but it is presumed absent. */
-#define DEFAULT_STACK_PERMS (PF_R|PF_W|PF_X)
+/* Default to an executable stack. PROT_EXEC can be overridden if PT_GNU_STACK
+ * is present, but it is presumed absent. */
+#define DEFAULT_STACK_PROT_PERMS (PROT_READ|PROT_WRITE|PROT_EXEC)
/* Access to the stack pointer. The macros are used in alloca_account
for which they need to act as barriers as well, hence the additional