Hi,

Mike Love wrote:
> On musl, gl_multithreaded() returns false in a statically linked
> program that creates threads, with both -static and -static-pie. Locks
> guarded by thread-optim, such as libunistring's struniq(), are then
> skipped while threads are running.
> 
> gnulib's own test shows it. With gnulib master 0727e5ff1e on Alpine
> Linux 3.24 (musl, gcc 15, x86_64):
> 
>   gnulib-tool --create-testdir --dir=td --single-configure thread-optim
>   mkdir b && cd b
>   ../td/configure CFLAGS="-O2 -static-pie" LDFLAGS="-static-pie"
>   make && make -C gltests check TESTS=test-thread-optim2
> 
> test-thread-optim2 fails at test-thread-optim2.c:43 with -static-pie
> and with "-static -no-pie", and passes when dynamically linked.
> libunistring 1.5 fails the same way. glibc is unaffected.

Thanks for the report. For this use-case, the programmer can use the
function gl_set_multithreaded, in order to overwrite the wrong guess.

> - With -static, musl's dl_iterate_phdr() names the program
> "/proc/self/exe", so the name is not empty.

With -static-pie as well. Thanks for the hint.

I'm applying the patch below. It has the effect that test-thread-optim2
passes and, instead, test-thread-optim1 fails. Which means, it replaces
an invalid optimization with a missed optimization opportunity.


2026-11-15  Bruno Haible  <[email protected]>

        thread-optim: Improvement for statically linked programs on musl libc.
        Reported by Mike Love <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2026-11/msg00070.html>.
        * lib/thread-optim.c (inspect_one_GOT): In statically linked programs on
        musl libc, return -1.
        * modules/thread-optim (Files): Add m4/musl.m4.
        (Depends-on): Add streq.
        (configure.ac): Invoke gl_MUSL_LIBC.

diff --git a/lib/thread-optim.c b/lib/thread-optim.c
index ba43f16d89..d24b4886f1 100644
--- a/lib/thread-optim.c
+++ b/lib/thread-optim.c
@@ -151,16 +151,25 @@ inspect_one_GOT (struct dl_phdr_info *info, size_t size, 
void *data)
   /* IMPORTANT: Before changing this code, make the corresponding changes
      to thread-optim-proto.c and test them.  */
 
-  /* Skip the processing of libc.so.
-     * On specific platforms, this is necessary:
-         - FreeBSD 12/arm64 has lio_listio in the GOT of libc.so.
-         - NetBSD 10 has timer_create in the GOT of libc.so.
-         - Android has pthread_create in the GOT of libc.so.
-       This does not make the process multithreaded.
-     * On the other platforms, it is just an optimization, to not scan
-       the (possibly many) symbols of libc.so.  */
   {
     const char *filename = info->dlpi_name;
+#if MUSL_LIBC
+    if (streq (filename, "/proc/self/exe"))
+      {
+        /* The executable is statically linked.
+           (See musl/src/ldso/dl_iterate_phdr.c.)  */
+        dbg_printf ("gl_multithreaded() = true, because the executable is 
statically linked.\n");
+        return -1;
+      }
+#endif
+    /* Skip the processing of libc.so.
+       * On specific platforms, this is necessary:
+           - FreeBSD 12/arm64 has lio_listio in the GOT of libc.so.
+           - NetBSD 10 has timer_create in the GOT of libc.so.
+           - Android has pthread_create in the GOT of libc.so.
+         This does not make the process multithreaded.
+       * On the other platforms, it is just an optimization, to not scan
+         the (possibly many) symbols of libc.so.  */
     const char *last_slash = strrchr (filename, '/');
     if (last_slash != NULL
         && strncmp (last_slash + 1, "libc.so", 7) == 0
diff --git a/modules/thread-optim b/modules/thread-optim
index 5acbbe5855..44aca6c029 100644
--- a/modules/thread-optim
+++ b/modules/thread-optim
@@ -6,11 +6,13 @@ lib/thread-optim.h
 lib/thread-optim.c
 lib/thread-creators.gperf
 lib/thread-optim-proto.c
+m4/musl.m4
 
 Depends-on:
 gperf
 bool
 stdint-h
+streq
 
 configure.ac:
 AC_CHECK_HEADERS([sys/single_threaded.h])
@@ -19,6 +21,7 @@ dnl Solaris 10 has dl_iterate_phdr in a separate library 
libdl.
 dnl Haiku has dl_iterate_phdr in a separate library libbsd.
 dnl We don't want a link dependency here.
 AC_CHECK_FUNCS([dl_iterate_phdr])
+gl_MUSL_LIBC
 
 Makefile.am:
 lib_SOURCES += thread-optim.c




Reply via email to