From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1791) id 5F4C03857822; Tue, 10 May 2022 18:36:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5F4C03857822 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Adhemerval Zanella To: glibc-cvs@sourceware.org Subject: [glibc/azanella/clang] elf: Fix _startup_fatal on clang X-Act-Checkin: glibc X-Git-Author: Adhemerval Zanella X-Git-Refname: refs/heads/azanella/clang X-Git-Oldrev: 6e1d228224fc5543476b0e7f5045d9c2a0accea2 X-Git-Newrev: bd03225ffbbeab70a08d594cfb8d480d0771756f Message-Id: <20220510183622.5F4C03857822@sourceware.org> Date: Tue, 10 May 2022 18:36:22 +0000 (GMT) X-BeenThere: glibc-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Glibc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 May 2022 18:36:22 -0000 https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=bd03225ffbbeab70a08d594cfb8d480d0771756f commit bd03225ffbbeab70a08d594cfb8d480d0771756f Author: Adhemerval Zanella Date: Tue May 10 15:34:42 2022 -0300 elf: Fix _startup_fatal on clang clang can not see that the input string is a constant one, so __builtin_constant_p fails and a strlen might be issue. Instead change TLS_INIT_TP to return the string size. Fixed only for x86_64 and aarch64. Diff: --- csu/libc-tls.c | 8 +++++--- elf/rtld.c | 6 ++++-- sysdeps/aarch64/nptl/tls.h | 4 ++-- sysdeps/unix/sysv/linux/startup.h | 10 ++-------- sysdeps/x86_64/nptl/tls.h | 6 ++++-- 5 files changed, 17 insertions(+), 17 deletions(-) diff --git a/csu/libc-tls.c b/csu/libc-tls.c index bef92a7568..915b016bf5 100644 --- a/csu/libc-tls.c +++ b/csu/libc-tls.c @@ -183,18 +183,20 @@ __libc_setup_tls (void) /* Install the pointer to the dtv. */ /* Initialize the thread pointer. */ + size_t lossagelen; #if TLS_TCB_AT_TP INSTALL_DTV ((char *) tlsblock + tcb_offset, _dl_static_dtv); - const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset); + const char *lossage = TLS_INIT_TP ((char *) tlsblock + tcb_offset, + lossagelen); #elif TLS_DTV_AT_TP INSTALL_DTV (tlsblock, _dl_static_dtv); - const char *lossage = TLS_INIT_TP (tlsblock); + const char *lossage = TLS_INIT_TP (tlsblock, lossagelen); #else # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined" #endif if (__builtin_expect (lossage != NULL, 0)) - _startup_fatal (lossage); + _startup_fatal (lossage, lossagelen); __tls_init_tp (); /* Update the executable's link map with enough information to make diff --git a/elf/rtld.c b/elf/rtld.c index 3d53107504..a549afc233 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -809,7 +809,8 @@ 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); + size_t lossagelen __attribute_maybe_unused__; + const char *lossage = TLS_INIT_TP (tcbp, lossagelen); if (__glibc_unlikely (lossage != NULL)) _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage); __tls_init_tp (); @@ -2317,7 +2318,8 @@ dl_main (const ElfW(Phdr) *phdr, /* And finally install it for the main thread. */ if (! tls_init_tp_called) { - const char *lossage = TLS_INIT_TP (tcbp); + size_t lossagelen __attribute_maybe_unused__; + const char *lossage = TLS_INIT_TP (tcbp, lossagelen); if (__glibc_unlikely (lossage != NULL)) _dl_fatal_printf ("cannot set up thread-local storage: %s\n", lossage); diff --git a/sysdeps/aarch64/nptl/tls.h b/sysdeps/aarch64/nptl/tls.h index 8d62b31e23..2f8cf50b89 100644 --- a/sysdeps/aarch64/nptl/tls.h +++ b/sysdeps/aarch64/nptl/tls.h @@ -71,8 +71,8 @@ typedef struct /* Code to initially initialize the thread pointer. This might need 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; }) +# define TLS_INIT_TP(tcbp, l) \ + ({ __asm __volatile ("msr tpidr_el0, %0" : : "r" (tcbp)); l = 0, NULL; }) /* Value passed to 'clone' for initialization of the thread register. */ # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd) + 1 diff --git a/sysdeps/unix/sysv/linux/startup.h b/sysdeps/unix/sysv/linux/startup.h index 39859b404a..6f9931106a 100644 --- a/sysdeps/unix/sysv/linux/startup.h +++ b/sysdeps/unix/sysv/linux/startup.h @@ -22,17 +22,11 @@ # include /* Avoid a run-time invocation of strlen. */ -#define _startup_fatal(message) \ +#define _startup_fatal(message, message_length) \ 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); \ + (message_length)); \ INTERNAL_SYSCALL_CALL (exit_group, 127); \ } \ while (0) diff --git a/sysdeps/x86_64/nptl/tls.h b/sysdeps/x86_64/nptl/tls.h index 75f8020975..57f85e78ba 100644 --- a/sysdeps/x86_64/nptl/tls.h +++ b/sysdeps/x86_64/nptl/tls.h @@ -132,6 +132,8 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80, # define GET_DTV(descr) \ (((tcbhead_t *) (descr))->dtv) +# define TLS_INIT_TP_ERR_MSG \ + "cannot set %fs base address for thread-local storage" /* Code to initially initialize the thread pointer. This might need special attention since 'errno' is not yet available and if the @@ -139,7 +141,7 @@ _Static_assert (offsetof (tcbhead_t, __glibc_unused2) == 0x80, We have to make the syscall for both uses of the macro since the address might be (and probably is) different. */ -# define TLS_INIT_TP(thrdescr) \ +# define TLS_INIT_TP(thrdescr, l) \ ({ void *_thrdescr = (thrdescr); \ tcbhead_t *_head = _thrdescr; \ int _result; \ @@ -156,7 +158,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 ? l = sizeof (TLS_INIT_TP_ERR_MSG) - 1, TLS_INIT_TP_ERR_MSG : 0; \ }) # define TLS_DEFINE_INIT_TP(tp, pd) void *tp = (pd)