public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails
@ 2022-05-02 19:15 Florian Weimer
  2022-05-02 19:15 ` [PATCH 1/5] Linux: Implement a useful version of _startup_fatal Florian Weimer
                   ` (4 more replies)
  0 siblings, 5 replies; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:15 UTC (permalink / raw)
  To: libc-alpha

This series implements a _dl_early_allocate function that does not need
a TCB.  It prefers sbrk over mmap to avoid creating too many PTE, but it
can use mmap as well.

Tested on aarch64-linux-gnu, i686-linux-gnu, powerpc64le-linux-gnu,
s390x-linux-gnu, x86_64-linux-gnu.  Built with build-many-glibcs.py.

I manually used strace to inject an brk failure, and it mmap fallback
kicks in.

Thanks,
Florian

Florian Weimer (5):
  Linux: Implement a useful version of _startup_fatal
  Linux: Define MMAP_CALL_INTERNAL
  i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S
  i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls
  csu: Implement and use _dl_early_allocate during static startup

 csu/libc-tls.c                                | 12 ++-
 elf/Makefile                                  | 19 +++++
 elf/dl-early_allocate.c                       | 30 +++++++
 elf/tst-tls-allocation-failure-static.c       | 31 +++++++
 scripts/tst-elf-edit.py                       | 34 ++++++--
 sysdeps/generic/ldsodefs.h                    |  5 ++
 sysdeps/unix/sysv/linux/dl-early_allocate.c   | 81 +++++++++++++++++++
 sysdeps/unix/sysv/linux/i386/Makefile         |  2 +-
 .../sysv/linux/i386/libc-do-syscall-int80.S   | 25 ++++++
 .../unix/sysv/linux/i386/libc-do-syscall.S    |  3 -
 sysdeps/unix/sysv/linux/i386/startup.h        | 17 +---
 sysdeps/unix/sysv/linux/i386/sysdep.h         | 13 ++-
 sysdeps/unix/sysv/linux/mmap_call.h           | 22 +++++
 sysdeps/unix/sysv/linux/mmap_internal.h       |  6 +-
 .../s390/{mmap_internal.h => mmap_call.h}     | 14 ++--
 sysdeps/unix/sysv/linux/startup.h             | 38 +++++++++
 16 files changed, 309 insertions(+), 43 deletions(-)
 create mode 100644 elf/dl-early_allocate.c
 create mode 100644 elf/tst-tls-allocation-failure-static.c
 create mode 100644 sysdeps/unix/sysv/linux/dl-early_allocate.c
 create mode 100644 sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S
 create mode 100644 sysdeps/unix/sysv/linux/mmap_call.h
 rename sysdeps/unix/sysv/linux/s390/{mmap_internal.h => mmap_call.h} (78%)
 create mode 100644 sysdeps/unix/sysv/linux/startup.h


base-commit: d056c212130280c0a54d9a4f72170ec621b70ce5
-- 
2.35.1


^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-02 19:15 [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails Florian Weimer
@ 2022-05-02 19:15 ` Florian Weimer
  2022-05-02 19:45   ` H.J. Lu
  2022-05-02 19:15 ` [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL Florian Weimer
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:15 UTC (permalink / raw)
  To: libc-alpha

---
 sysdeps/unix/sysv/linux/i386/startup.h | 17 ++----------
 sysdeps/unix/sysv/linux/startup.h      | 38 ++++++++++++++++++++++++++
 2 files changed, 40 insertions(+), 15 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/startup.h

diff --git a/sysdeps/unix/sysv/linux/i386/startup.h b/sysdeps/unix/sysv/linux/i386/startup.h
index 67c9310f3a..694d6cf4e7 100644
--- a/sysdeps/unix/sysv/linux/i386/startup.h
+++ b/sysdeps/unix/sysv/linux/i386/startup.h
@@ -19,19 +19,6 @@
 #if BUILD_PIE_DEFAULT
 /* Can't use "call *%gs:SYSINFO_OFFSET" during statup in static PIE.  */
 # define I386_USE_SYSENTER 0
-
-# include <sysdep.h>
-# include <abort-instr.h>
-
-__attribute__ ((__noreturn__))
-static inline void
-_startup_fatal (const char *message __attribute__ ((unused)))
-{
-  /* This is only called very early during startup in static PIE.
-     FIXME: How can it be improved?  */
-  ABORT_INSTRUCTION;
-  __builtin_unreachable ();
-}
-#else
-# include_next <startup.h>
 #endif
+
+#include_next <startup.h>
diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
new file mode 100644
index 0000000000..a5de941759
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/startup.h
@@ -0,0 +1,38 @@
+/* Linux definitions of functions used by static libc main startup.
+   Copyright (C) 2017-2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifdef SHARED
+# include_next <startup.h>
+#else
+# include <sysdep.h>
+
+/* Avoid a run-time invocation of strlen.  */
+#define _startup_fatal(message)                                         \
+  do                                                                    \
+    {                                                                   \
+      size_t message_length = __builtin_strlen (message);               \
+      if (! __builtin_constant_p (message_length))                      \
+        {                                                               \
+          extern void _startup_fatal_not_constant (void);               \
+          _startup_fatal_not_constant ();                               \
+        }                                                               \
+      INTERNAL_SYSCALL_CALL (write, STDERR_FILENO, message, message_length); \
+      INTERNAL_SYSCALL_CALL (exit_group, 127);                          \
+    }                                                                   \
+  while (0)
+#endif  /* !SHARED */
-- 
2.35.1



^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL
  2022-05-02 19:15 [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails Florian Weimer
  2022-05-02 19:15 ` [PATCH 1/5] Linux: Implement a useful version of _startup_fatal Florian Weimer
@ 2022-05-02 19:15 ` Florian Weimer
  2022-05-03 18:30   ` Florian Weimer
  2022-05-02 19:15 ` [PATCH 3/5] i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S Florian Weimer
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:15 UTC (permalink / raw)
  To: libc-alpha

Unlike MMAP_CALL, this avoids a TCB dependency for an errno update
on failure.

<mmap_internal.h> cannot be included as is on several architectures
due to the definition of page_unit, so introduce a separate header
file for the definition of MMAP_CALL and MMAP_CALL_INTERNAL,
 <mmap_call.h>.
---
 sysdeps/unix/sysv/linux/mmap_call.h           | 22 +++++++++++++++++++
 sysdeps/unix/sysv/linux/mmap_internal.h       |  6 +----
 .../s390/{mmap_internal.h => mmap_call.h}     | 14 ++++++------
 3 files changed, 30 insertions(+), 12 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/mmap_call.h
 rename sysdeps/unix/sysv/linux/s390/{mmap_internal.h => mmap_call.h} (78%)

diff --git a/sysdeps/unix/sysv/linux/mmap_call.h b/sysdeps/unix/sysv/linux/mmap_call.h
new file mode 100644
index 0000000000..3547c99e14
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/mmap_call.h
@@ -0,0 +1,22 @@
+/* Generic definition of MMAP_CALL and MMAP_CALL_INTERNAL.
+   Copyright (C) 2017-2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
+  INLINE_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
+#define MMAP_CALL_INTERNAL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
+  INTERNAL_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
diff --git a/sysdeps/unix/sysv/linux/mmap_internal.h b/sysdeps/unix/sysv/linux/mmap_internal.h
index 841b731391..aebf97d064 100644
--- a/sysdeps/unix/sysv/linux/mmap_internal.h
+++ b/sysdeps/unix/sysv/linux/mmap_internal.h
@@ -42,10 +42,6 @@ static uint64_t page_unit;
 /* Do not accept offset not multiple of page size.  */
 #define MMAP_OFF_LOW_MASK  (MMAP2_PAGE_UNIT - 1)
 
-/* An architecture may override this.  */
-#ifndef MMAP_CALL
-# define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
-  INLINE_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
-#endif
+#include <mmap_call.h>
 
 #endif /* MMAP_INTERNAL_LINUX_H  */
diff --git a/sysdeps/unix/sysv/linux/s390/mmap_internal.h b/sysdeps/unix/sysv/linux/s390/mmap_call.h
similarity index 78%
rename from sysdeps/unix/sysv/linux/s390/mmap_internal.h
rename to sysdeps/unix/sysv/linux/s390/mmap_call.h
index cc76ac9735..f169b8bab9 100644
--- a/sysdeps/unix/sysv/linux/s390/mmap_internal.h
+++ b/sysdeps/unix/sysv/linux/s390/mmap_call.h
@@ -16,9 +16,6 @@
    License along with the GNU C Library; if not, see
    <https://www.gnu.org/licenses/>.  */
 
-#ifndef MMAP_S390_INTERNAL_H
-# define MMAP_S390_INTERNAL_H
-
 #define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset)	\
   ({									\
     long int __args[6] = { (long int) (__addr), (long int) (__len),	\
@@ -26,7 +23,10 @@
 			   (long int) (__fd), (long int) (__offset) };	\
     INLINE_SYSCALL_CALL (__nr, __args);					\
   })
-
-#include_next <mmap_internal.h>
-
-#endif
+#define MMAP_CALL_INTERNAL(__nr, __addr, __len, __prot, __flags, __fd, __offset)	\
+  ({									\
+    long int __args[6] = { (long int) (__addr), (long int) (__len),	\
+			   (long int) (__prot), (long int) (__flags),	\
+			   (long int) (__fd), (long int) (__offset) };	\
+    INTERNAL_SYSCALL_CALL (__nr, __args);				\
+  })
-- 
2.35.1



^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 3/5] i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S
  2022-05-02 19:15 [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails Florian Weimer
  2022-05-02 19:15 ` [PATCH 1/5] Linux: Implement a useful version of _startup_fatal Florian Weimer
  2022-05-02 19:15 ` [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL Florian Weimer
@ 2022-05-02 19:15 ` Florian Weimer
  2022-05-02 19:20   ` H.J. Lu
  2022-05-02 19:15 ` [PATCH 4/5] i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls Florian Weimer
  2022-05-02 19:15 ` [PATCH 5/5] csu: Implement and use _dl_early_allocate during static startup Florian Weimer
  4 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:15 UTC (permalink / raw)
  To: libc-alpha

After commit a78e6a10d0b50d0ca80309775980fc99944b1727
("i386: Remove broken CAN_USE_REGISTER_ASM_EBP (bug 28771)"),
it is never defined.
---
 sysdeps/unix/sysv/linux/i386/libc-do-syscall.S | 3 ---
 1 file changed, 3 deletions(-)

diff --git a/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S b/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S
index 04154f43e0..3eea5f3a5d 100644
--- a/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S
+++ b/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S
@@ -18,8 +18,6 @@
 
 #include <sysdep.h>
 
-#ifndef OPTIMIZE_FOR_GCC_5
-
 /* %eax, %ecx, %edx and %esi contain the values expected by the kernel.
    %edi points to a structure with the values of %ebx, %edi and %ebp.  */
 
@@ -50,4 +48,3 @@ ENTRY (__libc_do_syscall)
 	cfi_restore (ebx)
 	ret
 END (__libc_do_syscall)
-#endif
-- 
2.35.1



^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 4/5] i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls
  2022-05-02 19:15 [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails Florian Weimer
                   ` (2 preceding siblings ...)
  2022-05-02 19:15 ` [PATCH 3/5] i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S Florian Weimer
@ 2022-05-02 19:15 ` Florian Weimer
  2022-05-02 19:24   ` H.J. Lu
  2022-05-02 19:15 ` [PATCH 5/5] csu: Implement and use _dl_early_allocate during static startup Florian Weimer
  4 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:15 UTC (permalink / raw)
  To: libc-alpha

Introduce an int-80h-based version of __libc_do_syscall and use
it if I386_USE_SYSENTER is defined as 0.
---
 sysdeps/unix/sysv/linux/i386/Makefile         |  2 +-
 .../sysv/linux/i386/libc-do-syscall-int80.S   | 25 +++++++++++++++++++
 sysdeps/unix/sysv/linux/i386/sysdep.h         | 13 ++++++++--
 3 files changed, 37 insertions(+), 3 deletions(-)
 create mode 100644 sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S

diff --git a/sysdeps/unix/sysv/linux/i386/Makefile b/sysdeps/unix/sysv/linux/i386/Makefile
index fcab129fc1..598e27dcda 100644
--- a/sysdeps/unix/sysv/linux/i386/Makefile
+++ b/sysdeps/unix/sysv/linux/i386/Makefile
@@ -13,7 +13,7 @@ install-bin += lddlibc4
 endif
 
 ifeq ($(subdir),io)
-sysdep_routines += libc-do-syscall
+sysdep_routines += libc-do-syscall libc-do-syscall-int80
 endif
 
 ifeq ($(subdir),stdlib)
diff --git a/sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S b/sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S
new file mode 100644
index 0000000000..2c472f2557
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S
@@ -0,0 +1,25 @@
+/* Out-of-line syscall stub for six-argument syscalls from C.  For static PIE.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef SHARED
+# define I386_USE_SYSENTER 0
+# include <sysdep.h>
+
+# define __libc_do_syscall __libc_do_syscall_int80
+# include "libc-do-syscall.S"
+#endif
diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
index 4558ab66cb..7085f7e19a 100644
--- a/sysdeps/unix/sysv/linux/i386/sysdep.h
+++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
@@ -42,6 +42,15 @@
 # endif
 #endif
 
+#if !I386_USE_SYSENTER && IS_IN (libc) && !defined SHARED
+/* Inside static libc, we have two versions.  For compilation units
+   with !I386_USE_SYSENTER, the vDSO entry mechanism cannot be
+   used. */
+# define I386_DO_SYSCALL_STRING "__libc_do_syscall_int80"
+#else
+# define I386_DO_SYSCALL_STRING "__libc_do_syscall"
+#endif
+
 #ifdef __ASSEMBLER__
 
 /* Linux uses a negative return value to indicate syscall errors,
@@ -301,7 +310,7 @@ struct libc_do_syscall_args
     };									\
     asm volatile (							\
     "movl %1, %%eax\n\t"						\
-    "call __libc_do_syscall"						\
+    "call " I386_DO_SYSCALL_STRING					\
     : "=a" (resultvar)							\
     : "i" (__NR_##name), "c" (arg2), "d" (arg3), "S" (arg4), "D" (&_xv) \
     : "memory", "cc")
@@ -315,7 +324,7 @@ struct libc_do_syscall_args
     };									\
     asm volatile (							\
     "movl %1, %%eax\n\t"						\
-    "call __libc_do_syscall"						\
+    "call " I386_DO_SYSCALL_STRING					\
     : "=a" (resultvar)							\
     : "a" (name), "c" (arg2), "d" (arg3), "S" (arg4), "D" (&_xv)	\
     : "memory", "cc")
-- 
2.35.1



^ permalink raw reply	[flat|nested] 23+ messages in thread

* [PATCH 5/5] csu: Implement and use _dl_early_allocate during static startup
  2022-05-02 19:15 [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails Florian Weimer
                   ` (3 preceding siblings ...)
  2022-05-02 19:15 ` [PATCH 4/5] i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls Florian Weimer
@ 2022-05-02 19:15 ` Florian Weimer
  4 siblings, 0 replies; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:15 UTC (permalink / raw)
  To: libc-alpha

This implements mmap fallback for a brk failure during TLS
allocation.

scripts/tls-elf-edit.py is updated to support the new patching method.
The script no longer requires that in the input object is of ET_DYN
type.
---
 csu/libc-tls.c                              | 12 ++-
 elf/Makefile                                | 19 +++++
 elf/dl-early_allocate.c                     | 30 ++++++++
 elf/tst-tls-allocation-failure-static.c     | 31 ++++++++
 scripts/tst-elf-edit.py                     | 34 +++++++--
 sysdeps/generic/ldsodefs.h                  |  5 ++
 sysdeps/unix/sysv/linux/dl-early_allocate.c | 81 +++++++++++++++++++++
 7 files changed, 202 insertions(+), 10 deletions(-)
 create mode 100644 elf/dl-early_allocate.c
 create mode 100644 elf/tst-tls-allocation-failure-static.c
 create mode 100644 sysdeps/unix/sysv/linux/dl-early_allocate.c

diff --git a/csu/libc-tls.c b/csu/libc-tls.c
index bef92a7568..7d311056bc 100644
--- a/csu/libc-tls.c
+++ b/csu/libc-tls.c
@@ -145,11 +145,16 @@ __libc_setup_tls (void)
      _dl_allocate_tls_storage (in elf/dl-tls.c) does using __libc_memalign
      and dl_tls_static_align.  */
   tcb_offset = roundup (memsz + GLRO(dl_tls_static_surplus), max_align);
-  tlsblock = __sbrk (tcb_offset + TLS_INIT_TCB_SIZE + max_align);
+  tlsblock = _dl_early_allocate (tcb_offset + TLS_INIT_TCB_SIZE + max_align);
+  if (tlsblock == NULL)
+    _startup_fatal ("Fatal glibc error: Cannot allocate TLS block\n");
 #elif TLS_DTV_AT_TP
   tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1);
-  tlsblock = __sbrk (tcb_offset + memsz + max_align
-		     + TLS_PRE_TCB_SIZE + GLRO(dl_tls_static_surplus));
+  tlsblock = _dl_early_allocate (tcb_offset + memsz + max_align
+				 + TLS_PRE_TCB_SIZE
+				 + GLRO(dl_tls_static_surplus));
+  if (tlsblock == NULL)
+    _startup_fatal ("Fatal glibc error: Cannot allocate TLS block\n");
   tlsblock += TLS_PRE_TCB_SIZE;
 #else
   /* In case a model with a different layout for the TCB and DTV
@@ -157,6 +162,7 @@ __libc_setup_tls (void)
 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
 #endif
 
+
   /* Align the TLS block.  */
   tlsblock = (void *) (((uintptr_t) tlsblock + max_align - 1)
 		       & ~(max_align - 1));
diff --git a/elf/Makefile b/elf/Makefile
index fc9860edee..ce3345ed92 100644
--- a/elf/Makefile
+++ b/elf/Makefile
@@ -33,6 +33,7 @@ routines = \
   $(all-dl-routines) \
   dl-addr \
   dl-addr-obj \
+  dl-early_allocate \
   dl-error \
   dl-iteratephdr \
   dl-libc \
@@ -108,6 +109,7 @@ all-dl-routines = $(dl-routines) $(sysdep-dl-routines)
 # But they are absent from the shared libc, because that code is in ld.so.
 elide-routines.os = \
   $(all-dl-routines) \
+  dl-early_allocate \
   dl-exception \
   dl-origin \
   dl-reloc-static-pie \
@@ -276,6 +278,7 @@ tests-static-normal := \
   tst-linkall-static \
   tst-single_threaded-pthread-static \
   tst-single_threaded-static \
+  tst-tls-allocation-failure-static \
   tst-tlsalign-extern-static \
   tst-tlsalign-static \
   # tests-static-normal
@@ -1213,6 +1216,10 @@ $(objpfx)tst-glibcelf.out: tst-glibcelf.py elf.h $(..)/scripts/glibcelf.py \
           --cc="$(CC) $(patsubst -DMODULE_NAME=%,-DMODULE_NAME=testsuite,$(CPPFLAGS))" \
 	  < /dev/null > $@ 2>&1; $(evaluate-test)
 
+ifeq ($(run-built-tests),yes)
+tests-special += $(objpfx)tst-tls-allocation-failure-static-patched.out
+endif
+
 # The test requires shared _and_ PIE because the executable
 # unit test driver must be able to link with the shared object
 # that is going to eventually go into an installed DSO.
@@ -2937,3 +2944,15 @@ $(eval $(call tst-trace-skeleton,4,\
 	$(objpfx)libtracemod2:$(objpfx)libtracemod3:$(objpfx)libtracemod4))
 $(eval $(call tst-trace-skeleton,5,\
 	$(objpfx)libtracemod2:$(objpfx)libtracemod3:$(objpfx)libtracemod4:$(objpfx)libtracemod5))
+
+$(objpfx)tst-tls-allocation-failure-static-patched: \
+  $(objpfx)tst-tls-allocation-failure-static $(..)scripts/tst-elf-edit.py
+	cp $< $@
+	$(PYTHON) $(..)scripts/tst-elf-edit.py --maximize-tls-size $@
+
+$(objpfx)tst-tls-allocation-failure-static-patched.out: \
+  $(objpfx)tst-tls-allocation-failure-static-patched
+	$< > $@ 2>&1; echo "status: $$?" >> $@
+	grep -q '^Fatal glibc error: Cannot allocate TLS block$$' $@ \
+	  && grep -q '^status: 127$$' $@; \
+	  $(evaluate-test)
diff --git a/elf/dl-early_allocate.c b/elf/dl-early_allocate.c
new file mode 100644
index 0000000000..d7f59d41e5
--- /dev/null
+++ b/elf/dl-early_allocate.c
@@ -0,0 +1,30 @@
+/* Early memory allocation for the dynamic loader.  Generic version.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+#include <ldsodefs.h>
+#include <stddef.h>
+#include <unistd.h>
+
+void *
+_dl_early_allocate (size_t size)
+{
+  void *result = __sbrk (size);
+  if (result == (void *) -1)
+    _dl_fatal_printf ("Fatal glibc error: Early memory allocation failed\n");
+  return result;
+}
diff --git a/elf/tst-tls-allocation-failure-static.c b/elf/tst-tls-allocation-failure-static.c
new file mode 100644
index 0000000000..8de831b246
--- /dev/null
+++ b/elf/tst-tls-allocation-failure-static.c
@@ -0,0 +1,31 @@
+/* Base for test program with impossiblyh large PT_TLS segment.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* The test actual binary is patched using scripts/tst-elf-edit.py
+   --maximize-tls-size, and this introduces the expected test
+   allocation failure due to an excessive PT_LS p_memsz value.
+
+   Patching the binary is required because on some 64-bit targets, TLS
+   relocations can only cover a 32-bit range, and glibc-internal TLS
+   variables such as errno end up outside that range.  */
+
+int
+main (void)
+{
+  return 0;
+}
diff --git a/scripts/tst-elf-edit.py b/scripts/tst-elf-edit.py
index a514179bbf..0e19ce1e73 100644
--- a/scripts/tst-elf-edit.py
+++ b/scripts/tst-elf-edit.py
@@ -43,9 +43,11 @@ EI_DATA=5
 ELFDATA2LSB=b'\x01'
 ELFDATA2MSB=b'\x02'
 
+ET_EXEC=2
 ET_DYN=3
 
 PT_LOAD=1
+PT_TLS=7
 
 def elf_types_fmts(e_ident):
     endian = '<' if e_ident[EI_DATA] == ELFDATA2LSB else '>'
@@ -146,8 +148,15 @@ def elf_edit_align(phdr, align):
     else:
         phdr.p_align = int(align)
 
+def elf_edit_maximize_tls_size(phdr, elfclass):
+    if elfclass == ELFCLASS32:
+        # It is possible that the kernel can allocate half of the
+        # address space, so use something larger.
+        phdr.p_memsz = 0xfff00000
+    else:
+        phdr.p_memsz = 1 << 63
 
-def elf_edit(f, align):
+def elf_edit(f, opts):
     ei_nident_fmt = 'c' * EI_NIDENT
     ei_nident_len = struct.calcsize(ei_nident_fmt)
 
@@ -172,24 +181,35 @@ def elf_edit(f, align):
 
     ehdr = Elf_Ehdr(e_ident)
     ehdr.read(f)
-    if ehdr.e_type != ET_DYN:
-       error('{}: not a shared library'.format(f.name))
+    if ehdr.e_type not in (ET_EXEC, ET_DYN):
+       error('{}: not an executable or shared library'.format(f.name))
 
     phdr = Elf_Phdr(e_ident)
+    maximize_tls_size_done = False
     for i in range(0, ehdr.e_phnum):
         f.seek(ehdr.e_phoff + i * phdr.len)
         phdr.read(f)
-        if phdr.p_type == PT_LOAD:
-            elf_edit_align(phdr, align)
+        if phdr.p_type == PT_LOAD and opts.align is not None:
+            elf_edit_align(phdr, opts.align)
+            f.seek(ehdr.e_phoff + i * phdr.len)
+            phdr.write(f)
+            break
+        if phdr.p_type == PT_TLS and opts.maximize_tls_size:
+            elf_edit_maximize_tls_size(phdr, e_ident[EI_CLASS])
             f.seek(ehdr.e_phoff + i * phdr.len)
             phdr.write(f)
+            maximize_tls_size_done = True
             break
 
+    if opts.maximize_tls_size and not maximize_tls_size_done:
+        error('{}: TLS maximum size was not updated'.format(f.name))
 
 def get_parser():
     parser = argparse.ArgumentParser(description=__doc__)
-    parser.add_argument('-a', dest='align', required=True,
+    parser.add_argument('-a', dest='align',
                         help='How to set the LOAD alignment')
+    parser.add_argument('--maximize-tls-size', action='store_true',
+                        help='Set maximum PT_TLS size')
     parser.add_argument('output',
                         help='ELF file to edit')
     return parser
@@ -199,7 +219,7 @@ def main(argv):
     parser = get_parser()
     opts = parser.parse_args(argv)
     with open(opts.output, 'r+b') as fout:
-       elf_edit(fout, opts.align)
+       elf_edit(fout, opts)
 
 
 if __name__ == '__main__':
diff --git a/sysdeps/generic/ldsodefs.h b/sysdeps/generic/ldsodefs.h
index 4a5e698db2..5d0369358d 100644
--- a/sysdeps/generic/ldsodefs.h
+++ b/sysdeps/generic/ldsodefs.h
@@ -1211,6 +1211,11 @@ extern struct link_map * _dl_get_dl_main_map (void)
 # endif
 #endif
 
+/* Perform early memory allocation, avoding a TCB dependency.
+   Terminate the process if allocation fails.  May attempt to use
+   brk.  */
+void *_dl_early_allocate (size_t size) attribute_hidden;
+
 /* Initialize the DSO sort algorithm to use.  */
 #if !HAVE_TUNABLES
 static inline void
diff --git a/sysdeps/unix/sysv/linux/dl-early_allocate.c b/sysdeps/unix/sysv/linux/dl-early_allocate.c
new file mode 100644
index 0000000000..2c77b6e3ff
--- /dev/null
+++ b/sysdeps/unix/sysv/linux/dl-early_allocate.c
@@ -0,0 +1,81 @@
+/* Early memory allocation for the dynamic loader.  Generic version.
+   Copyright (C) 2022 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Mark symbols hidden in static PIE for early self relocation to work.  */
+#if BUILD_PIE_DEFAULT
+# pragma GCC visibility push(hidden)
+#endif
+#include <startup.h>
+
+#include <ldsodefs.h>
+#include <stddef.h>
+#include <string.h>
+#include <sysdep.h>
+#include <unistd.h>
+
+#include <mmap_call.h>
+
+/* Defined in brk.c.  */
+extern void *__curbrk;
+
+void *
+_dl_early_allocate (size_t size)
+{
+  void *result;
+
+  if (__curbrk != NULL)
+    /* If the break has been initialized, brk must have run before,
+       so just call it once more.  */
+    {
+      result = __sbrk (size);
+      if (result == (void *) -1)
+        result = NULL;
+    }
+  else
+    {
+      /* If brk has not been invoked, there is no need to update
+         __curbrk.  The first call to brk will take care of that.  */
+      void *previous = (void *) INTERNAL_SYSCALL_CALL (brk, 0);
+      result = (void *) INTERNAL_SYSCALL_CALL (brk, previous + size);
+      if (result == previous)
+        result = NULL;
+      else
+        result = previous;
+    }
+
+  /* If brk fails, fall back to mmap.  This can happen due to
+     unfortunate ASLR layout decisions and kernel bugs, particularly
+     for static PIE.  */
+  if (result == NULL)
+    {
+      long int ret;
+      int prot = PROT_READ | PROT_WRITE;
+      int flags = MAP_PRIVATE | MAP_ANONYMOUS;
+#ifdef __NR_mmap2
+      ret = MMAP_CALL_INTERNAL (mmap2, 0, size, prot, flags, -1, 0);
+#else
+      ret = MMAP_CALL_INTERNAL (mmap, 0, size, prot, flags, -1, 0);
+#endif
+      if (INTERNAL_SYSCALL_ERROR_P (ret))
+        result = NULL;
+      else
+        result = (void *) ret;
+    }
+
+  return result;
+}
-- 
2.35.1


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 3/5] i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S
  2022-05-02 19:15 ` [PATCH 3/5] i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S Florian Weimer
@ 2022-05-02 19:20   ` H.J. Lu
  0 siblings, 0 replies; 23+ messages in thread
From: H.J. Lu @ 2022-05-02 19:20 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Mon, May 2, 2022 at 12:18 PM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> After commit a78e6a10d0b50d0ca80309775980fc99944b1727
> ("i386: Remove broken CAN_USE_REGISTER_ASM_EBP (bug 28771)"),
> it is never defined.
> ---
>  sysdeps/unix/sysv/linux/i386/libc-do-syscall.S | 3 ---
>  1 file changed, 3 deletions(-)
>
> diff --git a/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S b/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S
> index 04154f43e0..3eea5f3a5d 100644
> --- a/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S
> +++ b/sysdeps/unix/sysv/linux/i386/libc-do-syscall.S
> @@ -18,8 +18,6 @@
>
>  #include <sysdep.h>
>
> -#ifndef OPTIMIZE_FOR_GCC_5
> -
>  /* %eax, %ecx, %edx and %esi contain the values expected by the kernel.
>     %edi points to a structure with the values of %ebx, %edi and %ebp.  */
>
> @@ -50,4 +48,3 @@ ENTRY (__libc_do_syscall)
>         cfi_restore (ebx)
>         ret
>  END (__libc_do_syscall)
> -#endif
> --
> 2.35.1
>
>

LGTM.

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>

Thanks.

-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 4/5] i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls
  2022-05-02 19:15 ` [PATCH 4/5] i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls Florian Weimer
@ 2022-05-02 19:24   ` H.J. Lu
  0 siblings, 0 replies; 23+ messages in thread
From: H.J. Lu @ 2022-05-02 19:24 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Mon, May 2, 2022 at 12:18 PM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> Introduce an int-80h-based version of __libc_do_syscall and use
> it if I386_USE_SYSENTER is defined as 0.
> ---
>  sysdeps/unix/sysv/linux/i386/Makefile         |  2 +-
>  .../sysv/linux/i386/libc-do-syscall-int80.S   | 25 +++++++++++++++++++
>  sysdeps/unix/sysv/linux/i386/sysdep.h         | 13 ++++++++--
>  3 files changed, 37 insertions(+), 3 deletions(-)
>  create mode 100644 sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S
>
> diff --git a/sysdeps/unix/sysv/linux/i386/Makefile b/sysdeps/unix/sysv/linux/i386/Makefile
> index fcab129fc1..598e27dcda 100644
> --- a/sysdeps/unix/sysv/linux/i386/Makefile
> +++ b/sysdeps/unix/sysv/linux/i386/Makefile
> @@ -13,7 +13,7 @@ install-bin += lddlibc4
>  endif
>
>  ifeq ($(subdir),io)
> -sysdep_routines += libc-do-syscall
> +sysdep_routines += libc-do-syscall libc-do-syscall-int80
>  endif
>
>  ifeq ($(subdir),stdlib)
> diff --git a/sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S b/sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S
> new file mode 100644
> index 0000000000..2c472f2557
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/i386/libc-do-syscall-int80.S
> @@ -0,0 +1,25 @@
> +/* Out-of-line syscall stub for six-argument syscalls from C.  For static PIE.
> +   Copyright (C) 2022 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SHARED
> +# define I386_USE_SYSENTER 0
> +# include <sysdep.h>
> +
> +# define __libc_do_syscall __libc_do_syscall_int80
> +# include "libc-do-syscall.S"
> +#endif
> diff --git a/sysdeps/unix/sysv/linux/i386/sysdep.h b/sysdeps/unix/sysv/linux/i386/sysdep.h
> index 4558ab66cb..7085f7e19a 100644
> --- a/sysdeps/unix/sysv/linux/i386/sysdep.h
> +++ b/sysdeps/unix/sysv/linux/i386/sysdep.h
> @@ -42,6 +42,15 @@
>  # endif
>  #endif
>
> +#if !I386_USE_SYSENTER && IS_IN (libc) && !defined SHARED
> +/* Inside static libc, we have two versions.  For compilation units
> +   with !I386_USE_SYSENTER, the vDSO entry mechanism cannot be
> +   used. */
> +# define I386_DO_SYSCALL_STRING "__libc_do_syscall_int80"
> +#else
> +# define I386_DO_SYSCALL_STRING "__libc_do_syscall"
> +#endif
> +
>  #ifdef __ASSEMBLER__
>
>  /* Linux uses a negative return value to indicate syscall errors,
> @@ -301,7 +310,7 @@ struct libc_do_syscall_args
>      };                                                                 \
>      asm volatile (                                                     \
>      "movl %1, %%eax\n\t"                                               \
> -    "call __libc_do_syscall"                                           \
> +    "call " I386_DO_SYSCALL_STRING                                     \
>      : "=a" (resultvar)                                                 \
>      : "i" (__NR_##name), "c" (arg2), "d" (arg3), "S" (arg4), "D" (&_xv) \
>      : "memory", "cc")
> @@ -315,7 +324,7 @@ struct libc_do_syscall_args
>      };                                                                 \
>      asm volatile (                                                     \
>      "movl %1, %%eax\n\t"                                               \
> -    "call __libc_do_syscall"                                           \
> +    "call " I386_DO_SYSCALL_STRING                                     \
>      : "=a" (resultvar)                                                 \
>      : "a" (name), "c" (arg2), "d" (arg3), "S" (arg4), "D" (&_xv)       \
>      : "memory", "cc")
> --
> 2.35.1
>
>

LGTM.

Reviewed-by: H.J. Lu <hjl.tools@gmail.com>

Thanks.


-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-02 19:15 ` [PATCH 1/5] Linux: Implement a useful version of _startup_fatal Florian Weimer
@ 2022-05-02 19:45   ` H.J. Lu
  2022-05-02 19:52     ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: H.J. Lu @ 2022-05-02 19:45 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Mon, May 2, 2022 at 12:16 PM Florian Weimer via Libc-alpha
<libc-alpha@sourceware.org> wrote:
>
> ---
>  sysdeps/unix/sysv/linux/i386/startup.h | 17 ++----------
>  sysdeps/unix/sysv/linux/startup.h      | 38 ++++++++++++++++++++++++++
>  2 files changed, 40 insertions(+), 15 deletions(-)
>  create mode 100644 sysdeps/unix/sysv/linux/startup.h
>
> diff --git a/sysdeps/unix/sysv/linux/i386/startup.h b/sysdeps/unix/sysv/linux/i386/startup.h
> index 67c9310f3a..694d6cf4e7 100644
> --- a/sysdeps/unix/sysv/linux/i386/startup.h
> +++ b/sysdeps/unix/sysv/linux/i386/startup.h
> @@ -19,19 +19,6 @@
>  #if BUILD_PIE_DEFAULT
>  /* Can't use "call *%gs:SYSINFO_OFFSET" during statup in static PIE.  */
>  # define I386_USE_SYSENTER 0
> -
> -# include <sysdep.h>
> -# include <abort-instr.h>
> -
> -__attribute__ ((__noreturn__))
> -static inline void
> -_startup_fatal (const char *message __attribute__ ((unused)))
> -{
> -  /* This is only called very early during startup in static PIE.
> -     FIXME: How can it be improved?  */
> -  ABORT_INSTRUCTION;
> -  __builtin_unreachable ();
> -}
> -#else
> -# include_next <startup.h>
>  #endif
> +
> +#include_next <startup.h>
> diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
> new file mode 100644
> index 0000000000..a5de941759
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/startup.h
> @@ -0,0 +1,38 @@
> +/* Linux definitions of functions used by static libc main startup.
> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
                             Shouldn't it just be 2022?
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifdef SHARED
> +# include_next <startup.h>
> +#else
> +# include <sysdep.h>
> +
> +/* Avoid a run-time invocation of strlen.  */
> +#define _startup_fatal(message)                                         \

Is this needed only for static PIE?

> +  do                                                                    \
> +    {                                                                   \
> +      size_t message_length = __builtin_strlen (message);               \
> +      if (! __builtin_constant_p (message_length))                      \
> +        {                                                               \
> +          extern void _startup_fatal_not_constant (void);               \
> +          _startup_fatal_not_constant ();                               \
> +        }                                                               \
> +      INTERNAL_SYSCALL_CALL (write, STDERR_FILENO, message, message_length); \
> +      INTERNAL_SYSCALL_CALL (exit_group, 127);                          \
> +    }                                                                   \
> +  while (0)
> +#endif  /* !SHARED */
> --
> 2.35.1
>
>


-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-02 19:45   ` H.J. Lu
@ 2022-05-02 19:52     ` Florian Weimer
  2022-05-02 20:06       ` H.J. Lu
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 19:52 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

* H. J. Lu:

>> +#include_next <startup.h>
>> diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
>> new file mode 100644
>> index 0000000000..a5de941759
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/startup.h
>> @@ -0,0 +1,38 @@
>> +/* Linux definitions of functions used by static libc main startup.
>> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
>                              Shouldn't it just be 2022?
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#ifdef SHARED
>> +# include_next <startup.h>
>> +#else
>> +# include <sysdep.h>
>> +
>> +/* Avoid a run-time invocation of strlen.  */
>> +#define _startup_fatal(message)                                         \
>
> Is this needed only for static PIE?

I don't know.  It's used from csu/libc-tls.c, some targets may need it
for ET_EXEC static linking, too.  I haven't checked.  The additional
code size is minimal.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-02 19:52     ` Florian Weimer
@ 2022-05-02 20:06       ` H.J. Lu
  2022-05-02 20:37         ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: H.J. Lu @ 2022-05-02 20:06 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Mon, May 2, 2022 at 12:53 PM Florian Weimer <fweimer@redhat.com> wrote:
>
> * H. J. Lu:
>
> >> +#include_next <startup.h>
> >> diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
> >> new file mode 100644
> >> index 0000000000..a5de941759
> >> --- /dev/null
> >> +++ b/sysdeps/unix/sysv/linux/startup.h
> >> @@ -0,0 +1,38 @@
> >> +/* Linux definitions of functions used by static libc main startup.
> >> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
> >                              Shouldn't it just be 2022?
> >> +   This file is part of the GNU C Library.
> >> +
> >> +   The GNU C Library is free software; you can redistribute it and/or
> >> +   modify it under the terms of the GNU Lesser General Public
> >> +   License as published by the Free Software Foundation; either
> >> +   version 2.1 of the License, or (at your option) any later version.
> >> +
> >> +   The GNU C Library is distributed in the hope that it will be useful,
> >> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> >> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> >> +   Lesser General Public License for more details.
> >> +
> >> +   You should have received a copy of the GNU Lesser General Public
> >> +   License along with the GNU C Library; if not, see
> >> +   <https://www.gnu.org/licenses/>.  */
> >> +
> >> +#ifdef SHARED
> >> +# include_next <startup.h>
> >> +#else
> >> +# include <sysdep.h>
> >> +
> >> +/* Avoid a run-time invocation of strlen.  */
> >> +#define _startup_fatal(message)                                         \
> >
> > Is this needed only for static PIE?
>
> I don't know.  It's used from csu/libc-tls.c, some targets may need it
> for ET_EXEC static linking, too.  I haven't checked.  The additional
> code size is minimal.

_startup_fatal was added for i386 static PIE.  I don't think it is
needed for normal static executables.

-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-02 20:06       ` H.J. Lu
@ 2022-05-02 20:37         ` Florian Weimer
  2022-05-03  5:18           ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-02 20:37 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

* H. J. Lu:

> On Mon, May 2, 2022 at 12:53 PM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * H. J. Lu:
>>
>> >> +#include_next <startup.h>
>> >> diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
>> >> new file mode 100644
>> >> index 0000000000..a5de941759
>> >> --- /dev/null
>> >> +++ b/sysdeps/unix/sysv/linux/startup.h
>> >> @@ -0,0 +1,38 @@
>> >> +/* Linux definitions of functions used by static libc main startup.
>> >> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
>> >                              Shouldn't it just be 2022?
>> >> +   This file is part of the GNU C Library.
>> >> +
>> >> +   The GNU C Library is free software; you can redistribute it and/or
>> >> +   modify it under the terms of the GNU Lesser General Public
>> >> +   License as published by the Free Software Foundation; either
>> >> +   version 2.1 of the License, or (at your option) any later version.
>> >> +
>> >> +   The GNU C Library is distributed in the hope that it will be useful,
>> >> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> >> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> >> +   Lesser General Public License for more details.
>> >> +
>> >> +   You should have received a copy of the GNU Lesser General Public
>> >> +   License along with the GNU C Library; if not, see
>> >> +   <https://www.gnu.org/licenses/>.  */
>> >> +
>> >> +#ifdef SHARED
>> >> +# include_next <startup.h>
>> >> +#else
>> >> +# include <sysdep.h>
>> >> +
>> >> +/* Avoid a run-time invocation of strlen.  */
>> >> +#define _startup_fatal(message)                                         \
>> >
>> > Is this needed only for static PIE?
>>
>> I don't know.  It's used from csu/libc-tls.c, some targets may need it
>> for ET_EXEC static linking, too.  I haven't checked.  The additional
>> code size is minimal.
>
> _startup_fatal was added for i386 static PIE.  I don't think it is
> needed for normal static executables.

Based on the current initialization ordering, it seems to me that we
need it for non-PIE static programs, too.  Maybe I should check this
tomorrow.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-02 20:37         ` Florian Weimer
@ 2022-05-03  5:18           ` Florian Weimer
  2022-05-03 15:54             ` H.J. Lu
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-03  5:18 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

* Florian Weimer:

> * H. J. Lu:
>
>> On Mon, May 2, 2022 at 12:53 PM Florian Weimer <fweimer@redhat.com> wrote:
>>>
>>> * H. J. Lu:
>>>
>>> >> +#include_next <startup.h>
>>> >> diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
>>> >> new file mode 100644
>>> >> index 0000000000..a5de941759
>>> >> --- /dev/null
>>> >> +++ b/sysdeps/unix/sysv/linux/startup.h
>>> >> @@ -0,0 +1,38 @@
>>> >> +/* Linux definitions of functions used by static libc main startup.
>>> >> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
>>> >                              Shouldn't it just be 2022?
>>> >> +   This file is part of the GNU C Library.
>>> >> +
>>> >> +   The GNU C Library is free software; you can redistribute it and/or
>>> >> +   modify it under the terms of the GNU Lesser General Public
>>> >> +   License as published by the Free Software Foundation; either
>>> >> +   version 2.1 of the License, or (at your option) any later version.
>>> >> +
>>> >> +   The GNU C Library is distributed in the hope that it will be useful,
>>> >> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>>> >> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>>> >> +   Lesser General Public License for more details.
>>> >> +
>>> >> +   You should have received a copy of the GNU Lesser General Public
>>> >> +   License along with the GNU C Library; if not, see
>>> >> +   <https://www.gnu.org/licenses/>.  */
>>> >> +
>>> >> +#ifdef SHARED
>>> >> +# include_next <startup.h>
>>> >> +#else
>>> >> +# include <sysdep.h>
>>> >> +
>>> >> +/* Avoid a run-time invocation of strlen.  */
>>> >> +#define _startup_fatal(message)                                         \
>>> >
>>> > Is this needed only for static PIE?
>>>
>>> I don't know.  It's used from csu/libc-tls.c, some targets may need it
>>> for ET_EXEC static linking, too.  I haven't checked.  The additional
>>> code size is minimal.
>>
>> _startup_fatal was added for i386 static PIE.  I don't think it is
>> needed for normal static executables.
>
> Based on the current initialization ordering, it seems to me that we
> need it for non-PIE static programs, too.  Maybe I should check this
> tomorrow.

It does not work on x86-64 because cancellation handling needs the TCB:

Breakpoint 1, _dl_early_allocate (size=size@entry=3520) at ../sysdeps/unix/sysv/linux/dl-early_allocate.c:41
41	  if (__curbrk != NULL)
(gdb) finish
Run till exit from #0  _dl_early_allocate (size=size@entry=3520)
    at ../sysdeps/unix/sysv/linux/dl-early_allocate.c:41
__libc_setup_tls () at libc-tls.c:149
149	  if (tlsblock == NULL)
Value returned is $1 = (void *) 0x4eb000
(gdb) set $rax = 0
(gdb) c
Continuing.

Program received signal SIGSEGV, Segmentation fault.
__writev (fd=fd@entry=2, iov=iov@entry=0x7fffffffddb0, iovcnt=iovcnt@entry=1) at ../sysdeps/unix/sysv/linux/writev.c:26
26	  return SYSCALL_CANCEL (writev, fd, iov, iovcnt);
(gdb) disas
Dump of assembler code for function __writev:
=> 0x000000000044e3c0 <+0>:	mov    %fs:0x18,%eax
   0x000000000044e3c8 <+8>:	test   %eax,%eax
   0x000000000044e3ca <+10>:	jne    0x44e3e0 <__writev+32>
   0x000000000044e3cc <+12>:	mov    $0x14,%eax
   0x000000000044e3d1 <+17>:	syscall 
   0x000000000044e3d3 <+19>:	cmp    $0xfffffffffffff000,%rax
   0x000000000044e3d9 <+25>:	ja     0x44e430 <__writev+112>
   0x000000000044e3db <+27>:	ret    
   0x000000000044e3dc <+28>:	nopl   0x0(%rax)
   0x000000000044e3e0 <+32>:	sub    $0x28,%rsp
   0x000000000044e3e4 <+36>:	mov    %edx,0x1c(%rsp)
   0x000000000044e3e8 <+40>:	mov    %rsi,0x10(%rsp)
   0x000000000044e3ed <+45>:	mov    %edi,0x8(%rsp)
   0x000000000044e3f1 <+49>:	call   0x46d2b0 <__pthread_enable_asynccancel>
   0x000000000044e3f6 <+54>:	mov    0x1c(%rsp),%edx
   0x000000000044e3fa <+58>:	mov    0x10(%rsp),%rsi
   0x000000000044e3ff <+63>:	mov    %eax,%r8d
   0x000000000044e402 <+66>:	mov    0x8(%rsp),%edi
   0x000000000044e406 <+70>:	mov    $0x14,%eax
   0x000000000044e40b <+75>:	syscall 
   0x000000000044e40d <+77>:	cmp    $0xfffffffffffff000,%rax
   0x000000000044e413 <+83>:	ja     0x44e448 <__writev+136>
   0x000000000044e415 <+85>:	mov    %r8d,%edi
   0x000000000044e418 <+88>:	mov    %rax,0x8(%rsp)
   0x000000000044e41d <+93>:	call   0x46d330 <__pthread_disable_asynccancel>
   0x000000000044e422 <+98>:	mov    0x8(%rsp),%rax
   0x000000000044e427 <+103>:	add    $0x28,%rsp
   0x000000000044e42b <+107>:	ret    
   0x000000000044e42c <+108>:	nopl   0x0(%rax)
   0x000000000044e430 <+112>:	mov    $0xffffffffffffffa8,%rdx
   0x000000000044e437 <+119>:	neg    %eax
   0x000000000044e439 <+121>:	mov    %eax,%fs:(%rdx)
   0x000000000044e43c <+124>:	mov    $0xffffffffffffffff,%rax
   0x000000000044e443 <+131>:	ret    
   0x000000000044e444 <+132>:	nopl   0x0(%rax)
   0x000000000044e448 <+136>:	mov    $0xffffffffffffffa8,%rdx
   0x000000000044e44f <+143>:	neg    %eax
   0x000000000044e451 <+145>:	mov    %eax,%fs:(%rdx)
   0x000000000044e454 <+148>:	mov    $0xffffffffffffffff,%rax
   0x000000000044e45b <+155>:	jmp    0x44e415 <__writev+85>
End of assembler dump.
(gdb) bt
#0  __writev (fd=fd@entry=2, iov=iov@entry=0x7fffffffddb0, 
    iovcnt=iovcnt@entry=1) at ../sysdeps/unix/sysv/linux/writev.c:26
#1  0x00000000004077aa in writev_for_fatal (fd=<optimized out>, 
    total=<optimized out>, niov=<optimized out>, iov=<optimized out>)
    at ../sysdeps/posix/libc_fatal.c:44
#2  __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x4c70ad "%s")
    at ../sysdeps/posix/libc_fatal.c:124
#3  0x000000000040789c in __libc_fatal (
    message=message@entry=0x4adb60 "Fatal glibc error: Cannot allocate TLS block\n") at ../sysdeps/posix/libc_fatal.c:164
#4  0x000000000040a3c0 in __libc_setup_tls () at libc-tls.c:150
#5  0x0000000000409709 in __libc_start_main_impl (main=0x401740 <main>, 
    argc=1, argv=0x7fffffffe008, init=<optimized out>, fini=<optimized out>, 
    rtld_fini=0x0, stack_end=0x7fffffffdff8) at ../csu/libc-start.c:301
#6  0x0000000000401da1 in _start () at ../sysdeps/x86_64/start.S:115
(gdb) 

That might be a different bug.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03  5:18           ` Florian Weimer
@ 2022-05-03 15:54             ` H.J. Lu
  2022-05-03 16:00               ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: H.J. Lu @ 2022-05-03 15:54 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Mon, May 2, 2022 at 10:18 PM Florian Weimer <fweimer@redhat.com> wrote:
>
> * Florian Weimer:
>
> > * H. J. Lu:
> >
> >> On Mon, May 2, 2022 at 12:53 PM Florian Weimer <fweimer@redhat.com> wrote:
> >>>
> >>> * H. J. Lu:
> >>>
> >>> >> +#include_next <startup.h>
> >>> >> diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h
> >>> >> new file mode 100644
> >>> >> index 0000000000..a5de941759
> >>> >> --- /dev/null
> >>> >> +++ b/sysdeps/unix/sysv/linux/startup.h
> >>> >> @@ -0,0 +1,38 @@
> >>> >> +/* Linux definitions of functions used by static libc main startup.
> >>> >> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
> >>> >                              Shouldn't it just be 2022?
> >>> >> +   This file is part of the GNU C Library.
> >>> >> +
> >>> >> +   The GNU C Library is free software; you can redistribute it and/or
> >>> >> +   modify it under the terms of the GNU Lesser General Public
> >>> >> +   License as published by the Free Software Foundation; either
> >>> >> +   version 2.1 of the License, or (at your option) any later version.
> >>> >> +
> >>> >> +   The GNU C Library is distributed in the hope that it will be useful,
> >>> >> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> >>> >> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> >>> >> +   Lesser General Public License for more details.
> >>> >> +
> >>> >> +   You should have received a copy of the GNU Lesser General Public
> >>> >> +   License along with the GNU C Library; if not, see
> >>> >> +   <https://www.gnu.org/licenses/>.  */
> >>> >> +
> >>> >> +#ifdef SHARED
> >>> >> +# include_next <startup.h>
> >>> >> +#else
> >>> >> +# include <sysdep.h>
> >>> >> +
> >>> >> +/* Avoid a run-time invocation of strlen.  */
> >>> >> +#define _startup_fatal(message)                                         \
> >>> >
> >>> > Is this needed only for static PIE?
> >>>
> >>> I don't know.  It's used from csu/libc-tls.c, some targets may need it
> >>> for ET_EXEC static linking, too.  I haven't checked.  The additional
> >>> code size is minimal.
> >>
> >> _startup_fatal was added for i386 static PIE.  I don't think it is
> >> needed for normal static executables.
> >
> > Based on the current initialization ordering, it seems to me that we
> > need it for non-PIE static programs, too.  Maybe I should check this
> > tomorrow.
>
> It does not work on x86-64 because cancellation handling needs the TCB:
>
> Breakpoint 1, _dl_early_allocate (size=size@entry=3520) at ../sysdeps/unix/sysv/linux/dl-early_allocate.c:41
> 41        if (__curbrk != NULL)
> (gdb) finish
> Run till exit from #0  _dl_early_allocate (size=size@entry=3520)
>     at ../sysdeps/unix/sysv/linux/dl-early_allocate.c:41
> __libc_setup_tls () at libc-tls.c:149
> 149       if (tlsblock == NULL)
> Value returned is $1 = (void *) 0x4eb000
> (gdb) set $rax = 0
> (gdb) c
> Continuing.
>
> Program received signal SIGSEGV, Segmentation fault.
> __writev (fd=fd@entry=2, iov=iov@entry=0x7fffffffddb0, iovcnt=iovcnt@entry=1) at ../sysdeps/unix/sysv/linux/writev.c:26
> 26        return SYSCALL_CANCEL (writev, fd, iov, iovcnt);
> (gdb) disas
> Dump of assembler code for function __writev:
> => 0x000000000044e3c0 <+0>:     mov    %fs:0x18,%eax
>    0x000000000044e3c8 <+8>:     test   %eax,%eax
>    0x000000000044e3ca <+10>:    jne    0x44e3e0 <__writev+32>
>    0x000000000044e3cc <+12>:    mov    $0x14,%eax
>    0x000000000044e3d1 <+17>:    syscall
>    0x000000000044e3d3 <+19>:    cmp    $0xfffffffffffff000,%rax
>    0x000000000044e3d9 <+25>:    ja     0x44e430 <__writev+112>
>    0x000000000044e3db <+27>:    ret
>    0x000000000044e3dc <+28>:    nopl   0x0(%rax)
>    0x000000000044e3e0 <+32>:    sub    $0x28,%rsp
>    0x000000000044e3e4 <+36>:    mov    %edx,0x1c(%rsp)
>    0x000000000044e3e8 <+40>:    mov    %rsi,0x10(%rsp)
>    0x000000000044e3ed <+45>:    mov    %edi,0x8(%rsp)
>    0x000000000044e3f1 <+49>:    call   0x46d2b0 <__pthread_enable_asynccancel>
>    0x000000000044e3f6 <+54>:    mov    0x1c(%rsp),%edx
>    0x000000000044e3fa <+58>:    mov    0x10(%rsp),%rsi
>    0x000000000044e3ff <+63>:    mov    %eax,%r8d
>    0x000000000044e402 <+66>:    mov    0x8(%rsp),%edi
>    0x000000000044e406 <+70>:    mov    $0x14,%eax
>    0x000000000044e40b <+75>:    syscall
>    0x000000000044e40d <+77>:    cmp    $0xfffffffffffff000,%rax
>    0x000000000044e413 <+83>:    ja     0x44e448 <__writev+136>
>    0x000000000044e415 <+85>:    mov    %r8d,%edi
>    0x000000000044e418 <+88>:    mov    %rax,0x8(%rsp)
>    0x000000000044e41d <+93>:    call   0x46d330 <__pthread_disable_asynccancel>
>    0x000000000044e422 <+98>:    mov    0x8(%rsp),%rax
>    0x000000000044e427 <+103>:   add    $0x28,%rsp
>    0x000000000044e42b <+107>:   ret
>    0x000000000044e42c <+108>:   nopl   0x0(%rax)
>    0x000000000044e430 <+112>:   mov    $0xffffffffffffffa8,%rdx
>    0x000000000044e437 <+119>:   neg    %eax
>    0x000000000044e439 <+121>:   mov    %eax,%fs:(%rdx)
>    0x000000000044e43c <+124>:   mov    $0xffffffffffffffff,%rax
>    0x000000000044e443 <+131>:   ret
>    0x000000000044e444 <+132>:   nopl   0x0(%rax)
>    0x000000000044e448 <+136>:   mov    $0xffffffffffffffa8,%rdx
>    0x000000000044e44f <+143>:   neg    %eax
>    0x000000000044e451 <+145>:   mov    %eax,%fs:(%rdx)
>    0x000000000044e454 <+148>:   mov    $0xffffffffffffffff,%rax
>    0x000000000044e45b <+155>:   jmp    0x44e415 <__writev+85>
> End of assembler dump.
> (gdb) bt
> #0  __writev (fd=fd@entry=2, iov=iov@entry=0x7fffffffddb0,
>     iovcnt=iovcnt@entry=1) at ../sysdeps/unix/sysv/linux/writev.c:26
> #1  0x00000000004077aa in writev_for_fatal (fd=<optimized out>,
>     total=<optimized out>, niov=<optimized out>, iov=<optimized out>)
>     at ../sysdeps/posix/libc_fatal.c:44
> #2  __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x4c70ad "%s")
>     at ../sysdeps/posix/libc_fatal.c:124
> #3  0x000000000040789c in __libc_fatal (
>     message=message@entry=0x4adb60 "Fatal glibc error: Cannot allocate TLS block\n") at ../sysdeps/posix/libc_fatal.c:164
> #4  0x000000000040a3c0 in __libc_setup_tls () at libc-tls.c:150
> #5  0x0000000000409709 in __libc_start_main_impl (main=0x401740 <main>,
>     argc=1, argv=0x7fffffffe008, init=<optimized out>, fini=<optimized out>,
>     rtld_fini=0x0, stack_end=0x7fffffffdff8) at ../csu/libc-start.c:301
> #6  0x0000000000401da1 in _start () at ../sysdeps/x86_64/start.S:115
> (gdb)
>
> That might be a different bug.

The startup codes can't use cancellation before it is ready.  Do we need to
check SHARED in this case?

-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 15:54             ` H.J. Lu
@ 2022-05-03 16:00               ` Florian Weimer
  2022-05-03 16:36                 ` H.J. Lu
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-03 16:00 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

* H. J. Lu:

> The startup codes can't use cancellation before it is ready.  Do we need to
> check SHARED in this case?

I think __libc_fatal shouldn't be a cancellation point.  That's not
really related to SHARED.

We can reimplement __libc_fatal in terms of direct system calls (with
some caveats), so that it is guaranteed to be usable at this point.  But
I think that would be a different patch series.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 16:00               ` Florian Weimer
@ 2022-05-03 16:36                 ` H.J. Lu
  2022-05-03 16:43                   ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: H.J. Lu @ 2022-05-03 16:36 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Tue, May 3, 2022 at 9:01 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * H. J. Lu:
>
> > The startup codes can't use cancellation before it is ready.  Do we need to
> > check SHARED in this case?
>
> I think __libc_fatal shouldn't be a cancellation point.  That's not
> really related to SHARED.

Should we define _startup_fatal unconditionally and always define
I386_USE_SYSENTER as 0 for i386 so that _startup_fatal can be
used safely during startup?

> We can reimplement __libc_fatal in terms of direct system calls (with
> some caveats), so that it is guaranteed to be usable at this point.  But
> I think that would be a different patch series.

__libc_fatal shouldn't be used in csu/libc-tls.c.

-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 16:36                 ` H.J. Lu
@ 2022-05-03 16:43                   ` Florian Weimer
  2022-05-03 16:46                     ` H.J. Lu
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-03 16:43 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

* H. J. Lu:

> On Tue, May 3, 2022 at 9:01 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * H. J. Lu:
>>
>> > The startup codes can't use cancellation before it is ready.  Do we need to
>> > check SHARED in this case?
>>
>> I think __libc_fatal shouldn't be a cancellation point.  That's not
>> really related to SHARED.
>
> Should we define _startup_fatal unconditionally and always define
> I386_USE_SYSENTER as 0 for i386 so that _startup_fatal can be
> used safely during startup?

Hmm.  Isn't that what my patch does?

Thanks,
Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 16:43                   ` Florian Weimer
@ 2022-05-03 16:46                     ` H.J. Lu
  2022-05-03 16:57                       ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: H.J. Lu @ 2022-05-03 16:46 UTC (permalink / raw)
  To: Florian Weimer; +Cc: GNU C Library

On Tue, May 3, 2022 at 9:44 AM Florian Weimer <fweimer@redhat.com> wrote:
>
> * H. J. Lu:
>
> > On Tue, May 3, 2022 at 9:01 AM Florian Weimer <fweimer@redhat.com> wrote:
> >>
> >> * H. J. Lu:
> >>
> >> > The startup codes can't use cancellation before it is ready.  Do we need to
> >> > check SHARED in this case?
> >>
> >> I think __libc_fatal shouldn't be a cancellation point.  That's not
> >> really related to SHARED.
> >
> > Should we define _startup_fatal unconditionally and always define
> > I386_USE_SYSENTER as 0 for i386 so that _startup_fatal can be
> > used safely during startup?
>
> Hmm.  Isn't that what my patch does?
>

Your patch still checks BUILD_PIE_DEFAULT and SHARED.

-- 
H.J.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 16:46                     ` H.J. Lu
@ 2022-05-03 16:57                       ` Florian Weimer
  2022-05-03 17:01                         ` Adhemerval Zanella
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-03 16:57 UTC (permalink / raw)
  To: H.J. Lu; +Cc: GNU C Library

* H. J. Lu:

> On Tue, May 3, 2022 at 9:44 AM Florian Weimer <fweimer@redhat.com> wrote:
>>
>> * H. J. Lu:
>>
>> > On Tue, May 3, 2022 at 9:01 AM Florian Weimer <fweimer@redhat.com> wrote:
>> >>
>> >> * H. J. Lu:
>> >>
>> >> > The startup codes can't use cancellation before it is ready.  Do we need to
>> >> > check SHARED in this case?
>> >>
>> >> I think __libc_fatal shouldn't be a cancellation point.  That's not
>> >> really related to SHARED.
>> >
>> > Should we define _startup_fatal unconditionally and always define
>> > I386_USE_SYSENTER as 0 for i386 so that _startup_fatal can be
>> > used safely during startup?
>>
>> Hmm.  Isn't that what my patch does?
>>
>
> Your patch still checks BUILD_PIE_DEFAULT and SHARED.

Ah, I see, thanks.  You are right, the i386 version should define
I386_USE_SYSENTER unconditionally.  Will fix.

Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 16:57                       ` Florian Weimer
@ 2022-05-03 17:01                         ` Adhemerval Zanella
  2022-05-03 20:16                           ` Florian Weimer
  0 siblings, 1 reply; 23+ messages in thread
From: Adhemerval Zanella @ 2022-05-03 17:01 UTC (permalink / raw)
  To: libc-alpha



On 03/05/2022 13:57, Florian Weimer via Libc-alpha wrote:
> * H. J. Lu:
> 
>> On Tue, May 3, 2022 at 9:44 AM Florian Weimer <fweimer@redhat.com> wrote:
>>>
>>> * H. J. Lu:
>>>
>>>> On Tue, May 3, 2022 at 9:01 AM Florian Weimer <fweimer@redhat.com> wrote:
>>>>>
>>>>> * H. J. Lu:
>>>>>
>>>>>> The startup codes can't use cancellation before it is ready.  Do we need to
>>>>>> check SHARED in this case?
>>>>>
>>>>> I think __libc_fatal shouldn't be a cancellation point.  That's not
>>>>> really related to SHARED.
>>>>
>>>> Should we define _startup_fatal unconditionally and always define
>>>> I386_USE_SYSENTER as 0 for i386 so that _startup_fatal can be
>>>> used safely during startup?
>>>
>>> Hmm.  Isn't that what my patch does?
>>>
>>
>> Your patch still checks BUILD_PIE_DEFAULT and SHARED.
> 
> Ah, I see, thanks.  You are right, the i386 version should define
> I386_USE_SYSENTER unconditionally.  Will fix.

I think you might want to check ia64 as well, since it uses a TCB variable
to call the syscall in some cases.

^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL
  2022-05-02 19:15 ` [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL Florian Weimer
@ 2022-05-03 18:30   ` Florian Weimer
  2022-05-04 13:10     ` Stefan Liebler
  0 siblings, 1 reply; 23+ messages in thread
From: Florian Weimer @ 2022-05-03 18:30 UTC (permalink / raw)
  To: Stefan Liebler; +Cc: libc-alpha

* Florian Weimer via Libc-alpha:

> Unlike MMAP_CALL, this avoids a TCB dependency for an errno update
> on failure.
>
> <mmap_internal.h> cannot be included as is on several architectures
> due to the definition of page_unit, so introduce a separate header
> file for the definition of MMAP_CALL and MMAP_CALL_INTERNAL,
>  <mmap_call.h>.
> ---
>  sysdeps/unix/sysv/linux/mmap_call.h           | 22 +++++++++++++++++++
>  sysdeps/unix/sysv/linux/mmap_internal.h       |  6 +----
>  .../s390/{mmap_internal.h => mmap_call.h}     | 14 ++++++------
>  3 files changed, 30 insertions(+), 12 deletions(-)
>  create mode 100644 sysdeps/unix/sysv/linux/mmap_call.h
>  rename sysdeps/unix/sysv/linux/s390/{mmap_internal.h => mmap_call.h} (78%)
>
> diff --git a/sysdeps/unix/sysv/linux/mmap_call.h b/sysdeps/unix/sysv/linux/mmap_call.h
> new file mode 100644
> index 0000000000..3547c99e14
> --- /dev/null
> +++ b/sysdeps/unix/sysv/linux/mmap_call.h
> @@ -0,0 +1,22 @@
> +/* Generic definition of MMAP_CALL and MMAP_CALL_INTERNAL.
> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
> +   This file is part of the GNU C Library.
> +
> +   The GNU C Library is free software; you can redistribute it and/or
> +   modify it under the terms of the GNU Lesser General Public
> +   License as published by the Free Software Foundation; either
> +   version 2.1 of the License, or (at your option) any later version.
> +
> +   The GNU C Library is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> +   Lesser General Public License for more details.
> +
> +   You should have received a copy of the GNU Lesser General Public
> +   License along with the GNU C Library; if not, see
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
> +  INLINE_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
> +#define MMAP_CALL_INTERNAL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
> +  INTERNAL_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
> diff --git a/sysdeps/unix/sysv/linux/mmap_internal.h b/sysdeps/unix/sysv/linux/mmap_internal.h
> index 841b731391..aebf97d064 100644
> --- a/sysdeps/unix/sysv/linux/mmap_internal.h
> +++ b/sysdeps/unix/sysv/linux/mmap_internal.h
> @@ -42,10 +42,6 @@ static uint64_t page_unit;
>  /* Do not accept offset not multiple of page size.  */
>  #define MMAP_OFF_LOW_MASK  (MMAP2_PAGE_UNIT - 1)
>  
> -/* An architecture may override this.  */
> -#ifndef MMAP_CALL
> -# define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
> -  INLINE_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
> -#endif
> +#include <mmap_call.h>
>  
>  #endif /* MMAP_INTERNAL_LINUX_H  */
> diff --git a/sysdeps/unix/sysv/linux/s390/mmap_internal.h b/sysdeps/unix/sysv/linux/s390/mmap_call.h
> similarity index 78%
> rename from sysdeps/unix/sysv/linux/s390/mmap_internal.h
> rename to sysdeps/unix/sysv/linux/s390/mmap_call.h
> index cc76ac9735..f169b8bab9 100644
> --- a/sysdeps/unix/sysv/linux/s390/mmap_internal.h
> +++ b/sysdeps/unix/sysv/linux/s390/mmap_call.h
> @@ -16,9 +16,6 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>  
> -#ifndef MMAP_S390_INTERNAL_H
> -# define MMAP_S390_INTERNAL_H
> -
>  #define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset)	\
>    ({									\
>      long int __args[6] = { (long int) (__addr), (long int) (__len),	\
> @@ -26,7 +23,10 @@
>  			   (long int) (__fd), (long int) (__offset) };	\
>      INLINE_SYSCALL_CALL (__nr, __args);					\
>    })
> -
> -#include_next <mmap_internal.h>
> -
> -#endif
> +#define MMAP_CALL_INTERNAL(__nr, __addr, __len, __prot, __flags, __fd, __offset)	\
> +  ({									\
> +    long int __args[6] = { (long int) (__addr), (long int) (__len),	\
> +			   (long int) (__prot), (long int) (__flags),	\
> +			   (long int) (__fd), (long int) (__offset) };	\
> +    INTERNAL_SYSCALL_CALL (__nr, __args);				\
> +  })

Stefan, would you be able to review this patch?  It's mostly an S390
change, after all.

Thanks,
Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 1/5] Linux: Implement a useful version of _startup_fatal
  2022-05-03 17:01                         ` Adhemerval Zanella
@ 2022-05-03 20:16                           ` Florian Weimer
  0 siblings, 0 replies; 23+ messages in thread
From: Florian Weimer @ 2022-05-03 20:16 UTC (permalink / raw)
  To: Adhemerval Zanella via Libc-alpha

* Adhemerval Zanella via Libc-alpha:

> On 03/05/2022 13:57, Florian Weimer via Libc-alpha wrote:
>> * H. J. Lu:
>> 
>>> On Tue, May 3, 2022 at 9:44 AM Florian Weimer <fweimer@redhat.com> wrote:
>>>>
>>>> * H. J. Lu:
>>>>
>>>>> On Tue, May 3, 2022 at 9:01 AM Florian Weimer <fweimer@redhat.com> wrote:
>>>>>>
>>>>>> * H. J. Lu:
>>>>>>
>>>>>>> The startup codes can't use cancellation before it is ready.  Do we need to
>>>>>>> check SHARED in this case?
>>>>>>
>>>>>> I think __libc_fatal shouldn't be a cancellation point.  That's not
>>>>>> really related to SHARED.
>>>>>
>>>>> Should we define _startup_fatal unconditionally and always define
>>>>> I386_USE_SYSENTER as 0 for i386 so that _startup_fatal can be
>>>>> used safely during startup?
>>>>
>>>> Hmm.  Isn't that what my patch does?
>>>>
>>>
>>> Your patch still checks BUILD_PIE_DEFAULT and SHARED.
>> 
>> Ah, I see, thanks.  You are right, the i386 version should define
>> I386_USE_SYSENTER unconditionally.  Will fix.
>
> I think you might want to check ia64 as well, since it uses a TCB variable
> to call the syscall in some cases.

“might want to check“ isn't a phrase I would use in the context with
ia64, but yes, I can add a startup.h file for it, too.  Thanks for the
suggestion.

Florian


^ permalink raw reply	[flat|nested] 23+ messages in thread

* Re: [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL
  2022-05-03 18:30   ` Florian Weimer
@ 2022-05-04 13:10     ` Stefan Liebler
  0 siblings, 0 replies; 23+ messages in thread
From: Stefan Liebler @ 2022-05-04 13:10 UTC (permalink / raw)
  To: Florian Weimer; +Cc: libc-alpha

On 03/05/2022 20:30, Florian Weimer wrote:
> * Florian Weimer via Libc-alpha:
> 
>> Unlike MMAP_CALL, this avoids a TCB dependency for an errno update
>> on failure.
>>
>> <mmap_internal.h> cannot be included as is on several architectures
>> due to the definition of page_unit, so introduce a separate header
>> file for the definition of MMAP_CALL and MMAP_CALL_INTERNAL,
>>  <mmap_call.h>.
>> ---
>>  sysdeps/unix/sysv/linux/mmap_call.h           | 22 +++++++++++++++++++
>>  sysdeps/unix/sysv/linux/mmap_internal.h       |  6 +----
>>  .../s390/{mmap_internal.h => mmap_call.h}     | 14 ++++++------
>>  3 files changed, 30 insertions(+), 12 deletions(-)
>>  create mode 100644 sysdeps/unix/sysv/linux/mmap_call.h
>>  rename sysdeps/unix/sysv/linux/s390/{mmap_internal.h => mmap_call.h} (78%)
>>
>> diff --git a/sysdeps/unix/sysv/linux/mmap_call.h b/sysdeps/unix/sysv/linux/mmap_call.h
>> new file mode 100644
>> index 0000000000..3547c99e14
>> --- /dev/null
>> +++ b/sysdeps/unix/sysv/linux/mmap_call.h
>> @@ -0,0 +1,22 @@
>> +/* Generic definition of MMAP_CALL and MMAP_CALL_INTERNAL.
>> +   Copyright (C) 2017-2022 Free Software Foundation, Inc.
>> +   This file is part of the GNU C Library.
>> +
>> +   The GNU C Library is free software; you can redistribute it and/or
>> +   modify it under the terms of the GNU Lesser General Public
>> +   License as published by the Free Software Foundation; either
>> +   version 2.1 of the License, or (at your option) any later version.
>> +
>> +   The GNU C Library is distributed in the hope that it will be useful,
>> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
>> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
>> +   Lesser General Public License for more details.
>> +
>> +   You should have received a copy of the GNU Lesser General Public
>> +   License along with the GNU C Library; if not, see
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
>> +  INLINE_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
>> +#define MMAP_CALL_INTERNAL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
>> +  INTERNAL_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
>> diff --git a/sysdeps/unix/sysv/linux/mmap_internal.h b/sysdeps/unix/sysv/linux/mmap_internal.h
>> index 841b731391..aebf97d064 100644
>> --- a/sysdeps/unix/sysv/linux/mmap_internal.h
>> +++ b/sysdeps/unix/sysv/linux/mmap_internal.h
>> @@ -42,10 +42,6 @@ static uint64_t page_unit;
>>  /* Do not accept offset not multiple of page size.  */
>>  #define MMAP_OFF_LOW_MASK  (MMAP2_PAGE_UNIT - 1)
>>  
>> -/* An architecture may override this.  */
>> -#ifndef MMAP_CALL
>> -# define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset) \
>> -  INLINE_SYSCALL_CALL (__nr, __addr, __len, __prot, __flags, __fd, __offset)
>> -#endif
>> +#include <mmap_call.h>
>>  
>>  #endif /* MMAP_INTERNAL_LINUX_H  */
>> diff --git a/sysdeps/unix/sysv/linux/s390/mmap_internal.h b/sysdeps/unix/sysv/linux/s390/mmap_call.h
>> similarity index 78%
>> rename from sysdeps/unix/sysv/linux/s390/mmap_internal.h
>> rename to sysdeps/unix/sysv/linux/s390/mmap_call.h
>> index cc76ac9735..f169b8bab9 100644
>> --- a/sysdeps/unix/sysv/linux/s390/mmap_internal.h
>> +++ b/sysdeps/unix/sysv/linux/s390/mmap_call.h
>> @@ -16,9 +16,6 @@
>>     License along with the GNU C Library; if not, see
>>     <https://www.gnu.org/licenses/>.  */
>>  
>> -#ifndef MMAP_S390_INTERNAL_H
>> -# define MMAP_S390_INTERNAL_H
>> -
>>  #define MMAP_CALL(__nr, __addr, __len, __prot, __flags, __fd, __offset)	\
>>    ({									\
>>      long int __args[6] = { (long int) (__addr), (long int) (__len),	\
>> @@ -26,7 +23,10 @@
>>  			   (long int) (__fd), (long int) (__offset) };	\
>>      INLINE_SYSCALL_CALL (__nr, __args);					\
>>    })
>> -
>> -#include_next <mmap_internal.h>
>> -
>> -#endif
>> +#define MMAP_CALL_INTERNAL(__nr, __addr, __len, __prot, __flags, __fd, __offset)	\
>> +  ({									\
>> +    long int __args[6] = { (long int) (__addr), (long int) (__len),	\
>> +			   (long int) (__prot), (long int) (__flags),	\
>> +			   (long int) (__fd), (long int) (__offset) };	\
>> +    INTERNAL_SYSCALL_CALL (__nr, __args);				\
>> +  })
> 
> Stefan, would you be able to review this patch?  It's mostly an S390
> change, after all.
> 
> Thanks,
> Florian
> 

Sure. The patch looks good to me.
Moving the definition of MMAP_CALL is just a mechanical change. The
definition of MMAP_CALL_INTERNAL is similar and no other architecture
than s390x has its own definition of MMAP_CALL.
Thanks.

Reviewed-by: Stefan Liebler <stli@linux.ibm.com>

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2022-05-04 13:10 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-02 19:15 [PATCH 0/5] Linux: Fall back to mmap if early sbrk fails Florian Weimer
2022-05-02 19:15 ` [PATCH 1/5] Linux: Implement a useful version of _startup_fatal Florian Weimer
2022-05-02 19:45   ` H.J. Lu
2022-05-02 19:52     ` Florian Weimer
2022-05-02 20:06       ` H.J. Lu
2022-05-02 20:37         ` Florian Weimer
2022-05-03  5:18           ` Florian Weimer
2022-05-03 15:54             ` H.J. Lu
2022-05-03 16:00               ` Florian Weimer
2022-05-03 16:36                 ` H.J. Lu
2022-05-03 16:43                   ` Florian Weimer
2022-05-03 16:46                     ` H.J. Lu
2022-05-03 16:57                       ` Florian Weimer
2022-05-03 17:01                         ` Adhemerval Zanella
2022-05-03 20:16                           ` Florian Weimer
2022-05-02 19:15 ` [PATCH 2/5] Linux: Define MMAP_CALL_INTERNAL Florian Weimer
2022-05-03 18:30   ` Florian Weimer
2022-05-04 13:10     ` Stefan Liebler
2022-05-02 19:15 ` [PATCH 3/5] i386: Remove OPTIMIZE_FOR_GCC_5 from Linux libc-do-syscall.S Florian Weimer
2022-05-02 19:20   ` H.J. Lu
2022-05-02 19:15 ` [PATCH 4/5] i386: Honor I386_USE_SYSENTER for 6-argument Linux system calls Florian Weimer
2022-05-02 19:24   ` H.J. Lu
2022-05-02 19:15 ` [PATCH 5/5] csu: Implement and use _dl_early_allocate during static startup Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).