public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/52590] New: std::thread Segmentation fault static linking
@ 2012-03-14 20:42 jason at cornsyrup dot org
  2012-03-14 20:43 ` [Bug libstdc++/52590] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: jason at cornsyrup dot org @ 2012-03-14 20:42 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

             Bug #: 52590
           Summary: std::thread Segmentation fault static linking
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: jason@cornsyrup.org


$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.7/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.7.0~rc1-1ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-4.7/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr
--program-suffix=-4.7 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.7
--libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin
--enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.7.0 20120302 (prerelease) (Ubuntu/Linaro 4.7.0~rc1-1ubuntu1) 

$ g++ -static -pthread -std=c++0x -o thread thread.cc 

$ ./thread 
pthread_create works
std::thread works
Segmentation fault

$ cat thread.cc 
#include <thread>
#include <iostream>

void *foo1(void *) {
    std::cout << "pthread_create works\n";
    return 0;
}

void foo2() {
    std::cout << "std::thread works\n";
}

int main() {
    {
        pthread_t t;
        pthread_create(&t, 0, foo1, 0);
        void *r;
        pthread_join(t, &r);
    }

    {
        std::thread t(foo2);
        t.join();
    }
    return 0;
}

I was surprised to see that pthreads worked in this case while std::thread did
not. I've also heard that boost::thread works. I'm having a hard time finding
any documentation on whether static linking and std::thread should work
together. Also see
http://stackoverflow.com/questions/7090623/c0x-thread-static-linking-problem

If it is the case that pthreads and std::thread cannot work together with
static linking, then it'd be nice to fail at some point before runtime if that
is possible.


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

* [Bug libstdc++/52590] std::thread Segmentation fault static linking
  2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
@ 2012-03-14 20:43 ` pinskia at gcc dot gnu.org
  2012-03-14 22:20 ` jason at cornsyrup dot org
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-03-14 20:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID
           Severity|major                       |normal

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-03-14 20:42:22 UTC ---
> -static 

You need to make sure all of libpthread.a is included, right it does not get
linked that way.

This is not a GCC or glibc bug.


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

* [Bug libstdc++/52590] std::thread Segmentation fault static linking
  2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
  2012-03-14 20:43 ` [Bug libstdc++/52590] " pinskia at gcc dot gnu.org
@ 2012-03-14 22:20 ` jason at cornsyrup dot org
  2012-03-15  4:47 ` jason at cornsyrup dot org
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jason at cornsyrup dot org @ 2012-03-14 22:20 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

--- Comment #2 from Jason Toffaletti <jason at cornsyrup dot org> 2012-03-14 21:29:18 UTC ---
(In reply to comment #1)
> > -static 
> 
> You need to make sure all of libpthread.a is included, right it does not get
> linked that way.
> 
> This is not a GCC or glibc bug.

Your comment seems to suggest there is a way, but I can't find it.

g++ -Wl,--whole-archive -static -pthread -std=c++0x -o thread thread.cc

Gives pages of linker errors.
http://sourceware.org/bugzilla/show_bug.cgi?id=10652 suggests glibc and
pthreads just don't work with static linking.


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

* [Bug libstdc++/52590] std::thread Segmentation fault static linking
  2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
  2012-03-14 20:43 ` [Bug libstdc++/52590] " pinskia at gcc dot gnu.org
  2012-03-14 22:20 ` jason at cornsyrup dot org
@ 2012-03-15  4:47 ` jason at cornsyrup dot org
  2012-03-15  7:52 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: jason at cornsyrup dot org @ 2012-03-15  4:47 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

--- Comment #3 from Jason Toffaletti <jason at cornsyrup dot org> 2012-03-15 04:14:40 UTC ---
$ g++-4.7 -Wl,-M -v -static -pthread -std=c++0x -o thread thread.cc shows all
of libpthread being linked.

With a bit more digging I traced the problem to
/usr/include/c++/4.7.0/x86_64-linux-gnu/bits/gthr-posix.h declaring all of its
symbols with __attribute__((weakref)) even when linking statically. Which
caused the __gthread_* symbols to resolve to zero. I found this for some
background info http://gcc.gnu.org/ml/gcc/2005-10/msg00235.html

This change made my example work:

$ g++ -D_GLIBCXX_GTHREAD_USE_WEAK=0 -static -pthread -std=c++0x -o thread
thread.cc 
$ ./thread 
pthread_create works
std::thread works

However, if I change the code to call std::thread::detach instead of join, then
it crashes again with std::thread::detach being resolved to zero.

Program received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb) bt
#0  0x0000000000000000 in ?? ()
#1  0x0000000000439785 in std::thread::detach() ()
#2  0x0000000000401f52 in main ()


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

* [Bug libstdc++/52590] std::thread Segmentation fault static linking
  2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
                   ` (2 preceding siblings ...)
  2012-03-15  4:47 ` jason at cornsyrup dot org
@ 2012-03-15  7:52 ` jakub at gcc dot gnu.org
  2022-03-09 17:40 ` redi at gcc dot gnu.org
  2023-03-03 19:10 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-03-15  7:52 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-03-15 07:46:27 UTC ---
The weakref attributes are of course correct, the bug is that you aren't
linking the whole libpthread.a into the app.  On some Linux distributions
libpthread.a
has all the objects linked together with ld -r and therefore this will just
work there, on others you need -Wl,--whole-archive -lpthread
-Wl,--no-whole-archive.
Better yet don't link statically, there are many reasons why it is a bad idea.
http://www.akkadia.org/drepper/no_static_linking.html


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

* [Bug libstdc++/52590] std::thread Segmentation fault static linking
  2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
                   ` (3 preceding siblings ...)
  2012-03-15  7:52 ` jakub at gcc dot gnu.org
@ 2022-03-09 17:40 ` redi at gcc dot gnu.org
  2023-03-03 19:10 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2022-03-09 17:40 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |lightningdzeyenr at gmail dot com

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 104852 has been marked as a duplicate of this bug. ***

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

* [Bug libstdc++/52590] std::thread Segmentation fault static linking
  2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
                   ` (4 preceding siblings ...)
  2022-03-09 17:40 ` redi at gcc dot gnu.org
@ 2023-03-03 19:10 ` cvs-commit at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-03-03 19:10 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Alexandre Oliva <aoliva@gcc.gnu.org>:

https://gcc.gnu.org/g:21edd841611a97442a6b95e8ec7e91ff8fd3a451

commit r13-6461-g21edd841611a97442a6b95e8ec7e91ff8fd3a451
Author: Alexandre Oliva <oliva@adacore.com>
Date:   Fri Mar 3 15:59:36 2023 -0300

    link pthread_join from std::thread ctor

    Like pthread_create, pthread_join may fail to be statically linked in
    absent strong uses, so add to user code strong references to both when
    std::thread objects are created.


    for  libstdc++-v3/ChangeLog

            PR libstdc++/104852
            PR libstdc++/95989
            PR libstdc++/52590
            * include/bits/std_thread.h (thread::_M_thread_deps): New
            static implicitly-inline member function.
            (std::thread template ctor): Pass it to _M_start_thread.
            * src/c++11/thread.cc (thread::_M_start_thread): Name depend
            parameter, force it live on entry.

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

end of thread, other threads:[~2023-03-03 19:10 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-14 20:42 [Bug libstdc++/52590] New: std::thread Segmentation fault static linking jason at cornsyrup dot org
2012-03-14 20:43 ` [Bug libstdc++/52590] " pinskia at gcc dot gnu.org
2012-03-14 22:20 ` jason at cornsyrup dot org
2012-03-15  4:47 ` jason at cornsyrup dot org
2012-03-15  7:52 ` jakub at gcc dot gnu.org
2022-03-09 17:40 ` redi at gcc dot gnu.org
2023-03-03 19:10 ` cvs-commit at gcc dot gnu.org

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).