Bug 103133 - Binary built with -static using std::thread crashes
Summary: Binary built with -static using std::thread crashes
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 11.2.1
: P3 normal
Target Milestone: 10.5
Assignee: Jonathan Wakely
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-11-08 12:02 UTC by Sergio Losilla
Modified: 2024-02-12 17:19 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-11-08 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sergio Losilla 2021-11-08 12:02:48 UTC
The following program crashes when built with -static on Fedora 35:

    #include <thread>
    
    int main(int argc, char *argv[])
    {
        std::thread foo{[](){}};
        foo.join();

        return 0;
    }

Dynamic linking works fine though:

    $ g++ f35_thread_static_broken.cpp
    $ ./a.out 
    $ g++ -static f35_thread_static_broken.cpp
    $ ./a.out
    zsh: segmentation fault (core dumped)  ./a.out
Comment 1 Jonathan Wakely 2021-11-08 12:17:58 UTC
The binary doesn't have a definition of pthread_join.
Comment 2 Florian Weimer 2021-11-08 12:31:46 UTC
libstdc++.a still uses weak symbols. I assumed we had already removed all that weak symbol stuff from libstdc++ for __GLIBC_PREREQ (2, 34).
Comment 3 Richard Biener 2021-11-08 12:47:17 UTC
Still a user error - you should link with -pthread
Comment 4 Jonathan Wakely 2021-11-08 12:50:20 UTC
(In reply to Richard Biener from comment #3)
> Still a user error - you should link with -pthread

That doesn't help here though.
Comment 5 Jonathan Wakely 2021-11-08 12:51:03 UTC
(In reply to Florian Weimer from comment #2)
> libstdc++.a still uses weak symbols. I assumed we had already removed all
> that weak symbol stuff from libstdc++ for __GLIBC_PREREQ (2, 34).

No, none of that work has even started yet.
Comment 6 GCC Commits 2021-11-10 12:04:05 UTC
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:80fe172ba9820199c2bbce5d0611ffca27823049

commit r12-5108-g80fe172ba9820199c2bbce5d0611ffca27823049
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 9 23:45:36 2021 +0000

    libstdc++: Disable gthreads weak symbols for glibc 2.34 [PR103133]
    
    Since Glibc 2.34 all pthreads symbols are defined directly in libc not
    libpthread, and since Glibc 2.32 we have used __libc_single_threaded to
    avoid unnecessary locking in single-threaded programs. This means there
    is no reason to avoid linking to libpthread now, and so no reason to use
    weak symbols defined in gthr-posix.h for all the pthread_xxx functions.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100748
            PR libstdc++/103133
            * config/os/gnu-linux/os_defines.h (_GLIBCXX_GTHREAD_USE_WEAK):
            Define for glibc 2.34 and later.
Comment 7 Jonathan Wakely 2021-11-10 12:09:02 UTC
Fixed on trunk, backports needed too.
Comment 8 Jonathan Wakely 2021-11-12 10:28:08 UTC
For the record, musl does the same, since g:85da5c3024f731e719c4093314da8edcd1056527
Comment 9 GCC Commits 2021-11-24 11:52:20 UTC
The releases/gcc-11 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:293075002a7eaf1e9d457a1839bcaf83f8626824

commit r11-9301-g293075002a7eaf1e9d457a1839bcaf83f8626824
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 9 23:45:36 2021 +0000

    libstdc++: Disable gthreads weak symbols for glibc 2.34 [PR103133]
    
    Since Glibc 2.34 all pthreads symbols are defined directly in libc not
    libpthread, and since Glibc 2.32 we have used __libc_single_threaded to
    avoid unnecessary locking in single-threaded programs. This means there
    is no reason to avoid linking to libpthread now, and so no reason to use
    weak symbols defined in gthr-posix.h for all the pthread_xxx functions.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100748
            PR libstdc++/103133
            * config/os/gnu-linux/os_defines.h (_GLIBCXX_GTHREAD_USE_WEAK):
            Define for glibc 2.34 and later.
    
    (cherry picked from commit 80fe172ba9820199c2bbce5d0611ffca27823049)
Comment 10 Jonathan Wakely 2021-11-24 11:55:12 UTC
Fixed for GCC 11.3
Comment 11 GCC Commits 2022-08-03 12:30:38 UTC
The releases/gcc-10 branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:18eecb8c4a97716d4bc4890b05c91f172fadc7b3

commit r10-10928-g18eecb8c4a97716d4bc4890b05c91f172fadc7b3
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Tue Nov 9 23:45:36 2021 +0000

    libstdc++: Disable gthreads weak symbols for glibc 2.34 [PR103133]
    
    Since Glibc 2.34 all pthreads symbols are defined directly in libc not
    libpthread, and since Glibc 2.32 we have used __libc_single_threaded to
    avoid unnecessary locking in single-threaded programs. This means there
    is no reason to avoid linking to libpthread now, and so no reason to use
    weak symbols defined in gthr-posix.h for all the pthread_xxx functions.
    
    libstdc++-v3/ChangeLog:
    
            PR libstdc++/100748
            PR libstdc++/103133
            * config/os/gnu-linux/os_defines.h (_GLIBCXX_GTHREAD_USE_WEAK):
            Define for glibc 2.34 and later.
    
    (cherry picked from commit 80fe172ba9820199c2bbce5d0611ffca27823049)
Comment 12 Jonathan Wakely 2022-08-03 13:33:38 UTC
Also fixed for 10.5