]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
elf: Introduce <dl-call_tls_init_tp.h> and call_tls_init_tp (bug 29249)
authorFlorian Weimer <fweimer@redhat.com>
Thu, 3 Nov 2022 16:28:03 +0000 (17:28 +0100)
committerFlorian Weimer <fweimer@redhat.com>
Thu, 3 Nov 2022 16:28:03 +0000 (17:28 +0100)
This makes it more likely that the compiler can compute the strlen
argument in _startup_fatal at compile time, which is required to
avoid a dependency on strlen this early during process startup.

Reviewed-by: Szabolcs Nagy <szabolcs.nagy@arm.com>
25 files changed:
csu/libc-tls.c
elf/rtld.c
sysdeps/aarch64/nptl/tls.h
sysdeps/alpha/nptl/tls.h
sysdeps/arc/nptl/tls.h
sysdeps/csky/nptl/tls.h
sysdeps/generic/dl-call_tls_init_tp.h [new file with mode: 0644]
sysdeps/generic/startup.h
sysdeps/hppa/nptl/tls.h
sysdeps/i386/nptl/tls.h
sysdeps/ia64/nptl/tls.h
sysdeps/loongarch/nptl/tls.h
sysdeps/m68k/nptl/tls.h
sysdeps/mach/hurd/i386/tls.h
sysdeps/microblaze/nptl/tls.h
sysdeps/mips/nptl/tls.h
sysdeps/nios2/nptl/tls.h
sysdeps/or1k/nptl/tls.h
sysdeps/powerpc/nptl/tls.h
sysdeps/riscv/nptl/tls.h
sysdeps/s390/nptl/tls.h
sysdeps/sh/nptl/tls.h
sysdeps/sparc/nptl/tls.h
sysdeps/unix/sysv/linux/arm/tls.h
sysdeps/x86_64/nptl/tls.h

index 0a216c550289058ecd408ec71bbcee3322e4f12a..ca4def26133bb9a9ac870b60159a64693a09049d 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/param.h>
 #include <array_length.h>
 #include <pthreadP.h>
+#include <dl-call_tls_init_tp.h>
 
 #ifdef SHARED
  #error makefile bug, this file is for static only
@@ -147,14 +148,14 @@ __libc_setup_tls (void)
   tcb_offset = roundup (memsz + GLRO(dl_tls_static_surplus), 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");
+    _startup_fatal_tls_error ();
 #elif TLS_DTV_AT_TP
   tcb_offset = roundup (TLS_INIT_TCB_SIZE, align ?: 1);
   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");
+    _startup_fatal_tls_error ();
   tlsblock += TLS_PRE_TCB_SIZE;
 #else
   /* In case a model with a different layout for the TCB and DTV
@@ -191,16 +192,11 @@ __libc_setup_tls (void)
 #if TLS_TCB_AT_TP
   INSTALL_DTV ((char *) tlsblock + tcb_offset, _dl_static_dtv);
 
-  const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset);
+  call_tls_init_tp ((char *) tlsblock + tcb_offset);
 #elif TLS_DTV_AT_TP
   INSTALL_DTV (tlsblock, _dl_static_dtv);
-  const char *lossage = TLS_INIT_TP (tlsblock);
-#else
-# error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
+  call_tls_init_tp (tlsblock);
 #endif
-  if (__builtin_expect (lossage != NULL, 0))
-    _startup_fatal (lossage);
-  __tls_init_tp ();
 
   /* Update the executable's link map with enough information to make
      the TLS routines happy.  */
index 8671594f1f51563f74f8d2aa5f5ee6de14ca5278..70ad1acc43e931795ade0125dd684bf68337888e 100644 (file)
@@ -52,6 +52,7 @@
 #include <dl-execve.h>
 #include <dl-find_object.h>
 #include <dl-audit-check.h>
+#include <dl-call_tls_init_tp.h>
 
 #include <assert.h>
 
@@ -796,10 +797,7 @@ cannot allocate TLS data structures for initial thread\n");
   GL(dl_initial_dtv) = GET_DTV (tcbp);
 
   /* And finally install it for the main thread.  */
-  const char *lossage = TLS_INIT_TP (tcbp);
-  if (__glibc_unlikely (lossage != NULL))
-    _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage);
-  __tls_init_tp ();
+  call_tls_init_tp (tcbp);
   __rtld_tls_init_tp_called = true;
 
   return tcbp;
@@ -2348,13 +2346,7 @@ dl_main (const ElfW(Phdr) *phdr,
 
   /* And finally install it for the main thread.  */
   if (! __rtld_tls_init_tp_called)
-    {
-      const char *lossage = TLS_INIT_TP (tcbp);
-      if (__glibc_unlikely (lossage != NULL))
-       _dl_fatal_printf ("cannot set up thread-local storage: %s\n",
-                         lossage);
-      __tls_init_tp ();
-    }
+    call_tls_init_tp (tcbp);
 
   /* Make sure no new search directories have been added.  */
   assert (GLRO(dl_init_all_dirs) == GL(dl_all_dirs));
index 08aa2eff891b7be32243e9955d998892807c7b2e..6fe084c1ef9299d6c9857d87ad3073192c94db4a 100644 (file)
@@ -72,7 +72,7 @@ typedef struct
    special attention since 'errno' is not yet available and if the
    operation can cause a failure 'errno' must not be touched.  */
 # define TLS_INIT_TP(tcbp) \
-  ({ __asm __volatile ("msr tpidr_el0, %0" : : "r" (tcbp)); NULL; })
+  ({ __asm __volatile ("msr tpidr_el0, %0" : : "r" (tcbp)); true; })
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
index 8f5b69ad3b1b0c557fa1bae55278547572a374cc..48dc1369fc1e9abf5679cc58ba289cb0e73f703f 100644 (file)
@@ -69,7 +69,7 @@ typedef struct
    special attention since 'errno' is not yet available and if the
    operation can cause a failure 'errno' must not be touched.  */
 # define TLS_INIT_TP(tcbp) \
-  (__builtin_set_thread_pointer ((void *)(tcbp)), NULL)
+  (__builtin_set_thread_pointer ((void *)(tcbp)), true)
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
index 7fc6602b236fa2455f8de4a0540442ae85d27c98..948e618a3aa4873eab04d45fa3c88f843927a58d 100644 (file)
@@ -75,8 +75,7 @@ typedef struct
        long result_var;                                        \
        __builtin_set_thread_pointer (tcbp);                    \
        result_var = INTERNAL_SYSCALL_CALL (arc_settls, (tcbp));\
-       INTERNAL_SYSCALL_ERROR_P (result_var)                   \
-         ? "settls syscall error" : NULL;                      \
+       !INTERNAL_SYSCALL_ERROR_P (result_var);                 \
    })
 
 /* Value passed to 'clone' for initialization of the thread register.  */
index 58d6ab0fb2ae90de50cffd5b4a98426c6a793050..3f7afc93391f694a7139fda8680a11c343ac0855 100644 (file)
@@ -92,8 +92,7 @@ typedef struct
   ({ long int result_var;                                              \
      result_var = INTERNAL_SYSCALL_CALL (set_thread_area,              \
                     (char *) (tcbp) + TLS_TCB_OFFSET);                 \
-     INTERNAL_SYSCALL_ERROR_P (result_var)                             \
-       ? "unknown error" : NULL; })
+     !INTERNAL_SYSCALL_ERROR_P (result_var); })
 
 /* Return the address of the dtv for the current thread.  */
 # define THREAD_DTV() \
diff --git a/sysdeps/generic/dl-call_tls_init_tp.h b/sysdeps/generic/dl-call_tls_init_tp.h
new file mode 100644 (file)
index 0000000..411feda
--- /dev/null
@@ -0,0 +1,34 @@
+/* Invoke TLS_INIT_TP and __tls_init_tp with error handling.
+   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 <startup.h>
+#include <tls.h>
+
+static inline void
+_startup_fatal_tls_error (void)
+{
+  _startup_fatal ("Fatal glibc error: Cannot allocate TLS block\n");
+}
+
+static inline void
+call_tls_init_tp (void *addr)
+{
+  if (!TLS_INIT_TP (addr))
+    _startup_fatal_tls_error ();
+  __tls_init_tp ();
+}
index 45979ab2319b2257dda70ecd17d1e839ce1ec5cb..4b444511b4f29e9284b3276e7d73a21e160addac 100644 (file)
@@ -19,7 +19,7 @@
 /* Targets should override this file if the default definitions below
    will not work correctly very early before TLS is initialized.  */
 
-#include <unistd.h>
+#include <stdio.h>
 
 /* Use macro instead of inline function to avoid including <stdio.h>.  */
 #define _startup_fatal(message) __libc_fatal ((message))
index e6b0bd5c7182b497aaf0d2bb08f62551a223c403..6af22cdc00442ab272fcf14b7730de132d66a372 100644 (file)
@@ -74,7 +74,7 @@ typedef struct
    special attention since 'errno' is not yet available and if the
    operation can cause a failure 'errno' must not be touched.  */
 # define TLS_INIT_TP(tcbp) \
-  ({ __set_cr27(tcbp); NULL; })
+  ({ __set_cr27(tcbp); true; })
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
index 91090bf28777fefb44f05404d44bfe0abd42e66a..553b55b3f2d86a80b4e152c986bdb43db5b050b2 100644 (file)
@@ -203,8 +203,7 @@ tls_fill_user_desc (union user_desc_init *desc,
          which is necessary since we have changed it.   */                   \
        TLS_SET_GS (_segdescr.desc.entry_number * 8 + 3);                     \
                                                                              \
-     _result == 0 ? NULL                                                     \
-     : "set_thread_area failed when setting up thread-local storage\n"; })
+     _result == 0; })
 
 # define TLS_DEFINE_INIT_TP(tp, pd)                                          \
   union user_desc_init _segdescr;                                            \
index d2411b3c1ac29733c0bb3683d83388e2e0e8e277..4085c568326a0de328df304930023da8f50ab11a 100644 (file)
@@ -105,7 +105,7 @@ register struct pthread *__thread_self __asm__("r13");
    special attention since 'errno' is not yet available and if the
    operation can cause a failure 'errno' must not be touched.  */
 # define TLS_INIT_TP(thrdescr) \
-  (__thread_self = (thrdescr), INIT_SYSINFO, NULL)
+  (__thread_self = (thrdescr), INIT_SYSINFO, true)
 
 /* Value passed to 'clone2' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) \
index 24bffe3a0adb91cda193463e2d0aa7d5a4d61b6f..e8b616854f7751cbc0a838525ce509858c5ea8bd 100644 (file)
@@ -83,7 +83,7 @@ typedef struct
 #define TLS_INIT_TP(tcbp) \
   ({ \
     __thread_self = (char *) tcbp + TLS_TCB_OFFSET; \
-    NULL; \
+    true; \
   })
 
 /* Return the address of the dtv for the current thread.  */
index 742e1b6767d99fa6011ac1d207264c7b82e53787..ebcc118bbe2d5f282936aca6cbb8dcfce174af55 100644 (file)
@@ -90,7 +90,7 @@ typedef struct
                                                                        \
     _sys_result = INTERNAL_SYSCALL_CALL (set_thread_area,              \
                                    ((void *) (tcbp)) + TLS_TCB_OFFSET); \
-    INTERNAL_SYSCALL_ERROR_P (_sys_result) ? "unknown error" : NULL; })
+    !INTERNAL_SYSCALL_ERROR_P (_sys_result); })
 
 # define TLS_DEFINE_INIT_TP(tp, pd) \
   void *tp = (void *) (pd) + TLS_TCB_OFFSET + TLS_PRE_TCB_SIZE
index 602bacc0de0e25580c3bccb03fe2f29a9607444a..2847e3ee61cd4f8890d6f31753618ebd2cec4b02 100644 (file)
@@ -117,12 +117,12 @@ extern unsigned short __init1_desc;
 
 # define HURD_SEL_LDT(sel) (__builtin_expect ((sel) & 4, 0))
 
-static inline const char * __attribute__ ((unused))
+static inline bool __attribute__ ((unused))
 _hurd_tls_init (tcbhead_t *tcb)
 {
   HURD_TLS_DESC_DECL (desc, tcb);
   thread_t self = __mach_thread_self ();
-  const char *msg = NULL;
+  bool success = true;
 
   /* This field is used by TLS accesses to get our "thread pointer"
      from the TLS point of view.  */
@@ -141,14 +141,14 @@ _hurd_tls_init (tcbhead_t *tcb)
       assert_perror (err);
       if (err)
       {
-       msg = "i386_set_ldt failed";
+       success = false;
        goto out;
       }
     }
   else if (err)
     {
       assert_perror (err); /* Separate from above with different line #. */
-      msg = "i386_set_gdt failed";
+      success = false;
       goto out;
     }
 
@@ -157,7 +157,7 @@ _hurd_tls_init (tcbhead_t *tcb)
 
 out:
   __mach_port_deallocate (__mach_task_self (), self);
-  return msg;
+  return success;
 }
 
 /* Code to initially initialize the thread pointer.  This might need
index 588fd1c5d63ee4e6a1b284cc19e216b6730a2091..d8be1932b9b0d2750c811b63ceb31b90d78bbb6f 100644 (file)
@@ -75,7 +75,7 @@ typedef struct
 /* Code to initially initialize the thread pointer.
    r21 is reserved for thread pointer.  */
 # define TLS_INIT_TP(tcbp) \
-  ({ __asm __volatile ("or r21,r0,%0" : : "r" ((void *)tcbp)); NULL; })
+  ({ __asm __volatile ("or r21,r0,%0" : : "r" ((void *)tcbp)); true; })
 
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
 
index 2aa7cb4bb8d0b5a31889aa33d5751104ff1e4f45..bac6e8771cd17087e14730248e36a238116b2f97 100644 (file)
@@ -116,8 +116,7 @@ typedef struct
   ({ long int result_var;                                              \
      result_var = INTERNAL_SYSCALL_CALL (set_thread_area,              \
                                    (char *) (tcbp) + TLS_TCB_OFFSET);  \
-     INTERNAL_SYSCALL_ERROR_P (result_var)                             \
-       ? "unknown error" : NULL; })
+     !INTERNAL_SYSCALL_ERROR_P (result_var); })
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) \
index cb231e2a4bbfa52495c4c017a7d3e1c6dd7937ca..f5a9713f93fc95f47fa9bb336f3f10253b5d5ed8 100644 (file)
@@ -88,7 +88,7 @@ register struct pthread *__thread_self __asm__("r23");
 
 /* Code to initially initialize the thread pointer.  */
 # define TLS_INIT_TP(tcbp) \
-  (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), NULL)
+  (__thread_self = (struct pthread *) ((char *) tcbp + TLS_TCB_OFFSET), true)
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) \
index e82f444738de222c0e4866d5a2ab8191ce99ddc9..eda0e893aeac0c89e97d7a77bc7242c77ea42a60 100644 (file)
@@ -112,7 +112,7 @@ register tcbhead_t *__thread_self __asm__("r10");
    It's hard to fail this, so return NULL always.  */
 
 # define TLS_INIT_TP(tcbp) \
-  ({__thread_self = ((tcbhead_t *)tcbp + 1); NULL;})
+  ({__thread_self = ((tcbhead_t *)tcbp + 1); true;})
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) \
index e62a96238aa95c79ac1f749b4dbf03985b6e15d4..68a1d8b546b4cf8447e8d68fcbf59d31090c1f9d 100644 (file)
@@ -139,7 +139,7 @@ typedef struct
     __thread_register = (void *) (tcbp) + TLS_TCB_OFFSET;                    \
     THREAD_SET_HWCAP (__tcb.hwcap);                                          \
     THREAD_SET_AT_PLATFORM (__tcb.at_platform);                                      \
-    NULL;                                                                    \
+    true;                                                                    \
   })
 
 /* Value passed to 'clone' for initialization of the thread register.  */
index 700c2f51899b0385d7ebaa4810c84de4fa6f2b45..b221980399b7b33cc710ed6394e3a424a3aa9dbe 100644 (file)
@@ -79,7 +79,7 @@ typedef struct
 
 /* Code to initially initialize the thread pointer.  */
 # define TLS_INIT_TP(tcbp) \
-  ({ __thread_self = (char*)tcbp + TLS_TCB_OFFSET; NULL; })
+  ({ __thread_self = (char*)tcbp + TLS_TCB_OFFSET; true; })
 
 /* Return the address of the dtv for the current thread.  */
 # define THREAD_DTV() \
index 98d7870148ce6bc1d6397b1465dfabe96f7280b2..937c49dda6876bd22b5c0d0867597bad44883ed0 100644 (file)
@@ -112,7 +112,7 @@ typedef struct
      INIT_SYSINFO;                                                           \
                                                                              \
     __builtin_set_thread_pointer (_thrdescr);                                \
-    NULL;                                                                    \
+    true;                                                                    \
   })
 
 /* Value passed to 'clone' for initialization of the thread register.  */
index 1530489a6ce4286bc5146e6cd83e3b463b965467..3882114f5c8137c471179e1271c7f5263cf137ae 100644 (file)
@@ -83,7 +83,7 @@ typedef struct
    special attention since 'errno' is not yet available and if the
    operation can cause a failure 'errno' must not be touched.  */
 # define TLS_INIT_TP(tcbp) \
-  ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); NULL; })
+  ({ __asm __volatile ("ldc %0,gbr" : : "r" (tcbp)); true; })
 
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1
 
index 95a69cb8249dc79c3a063637a21d976d2660c48f..e8fbe40e7bb5e04c655b26e7fafe9ebaef3637b6 100644 (file)
@@ -89,7 +89,7 @@ register struct pthread *__thread_self __asm__("%g7");
 
 /* Code to initially initialize the thread pointer.  */
 # define TLS_INIT_TP(descr) \
-  (__thread_self = (__typeof (__thread_self)) (descr), NULL)
+  (__thread_self = (__typeof (__thread_self)) (descr), true)
 
 /* Value passed to 'clone' for initialization of the thread register.  */
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd)
index 9d1601af44cbd7a224cebcd9edc5d1955a8aa27e..045fad22751074b9a8e1ebcd21363500ea80286a 100644 (file)
@@ -33,8 +33,7 @@
 # define TLS_INIT_TP(tcbp) \
   ({ long int result_var;                                              \
      result_var = INTERNAL_SYSCALL_CALL (set_tls, (tcbp));             \
-     INTERNAL_SYSCALL_ERROR_P (result_var)                             \
-       ? "unknown error" : NULL; })
+     !INTERNAL_SYSCALL_ERROR_P (result_var); })
 
 #endif /* __ASSEMBLER__ */
 
index 75f80209758aa19cc59c413e4bbd1a565e5b9696..63744f63845eb437a1e14c8abc948982e17c38ce 100644 (file)
@@ -156,7 +156,7 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80,
                     "S" (_thrdescr)                                          \
                   : "memory", "cc", "r11", "cx");                            \
                                                                              \
-    _result ? "cannot set %fs base address for thread-local storage" : 0;     \
+    _result == 0;                                                            \
   })
 
 # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd)