Bug 25437 - Invalid combination --as-needed and -pthread options
Summary: Invalid combination --as-needed and -pthread options
Status: RESOLVED MOVED
Alias: None
Product: binutils
Classification: Unclassified
Component: gold (show other bugs)
Version: 2.33
: P2 normal
Target Milestone: ---
Assignee: Cary Coutant
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-22 09:27 UTC by laurent.stacul
Modified: 2020-01-22 10:16 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description laurent.stacul 2020-01-22 09:27:27 UTC
I have a program that compiles fine. As soon as I try to use the linker option `--as-needed`, the program builds but crases at runtime. I investigated this issue and managed to build a reproducer.

The reproducer:

#include <mutex>
#include <iostream>

std::once_flag flag;

int main(int argc, const char *argv[])
{
    std::call_once(flag, [](){ std::cout << "Simple example: called once\n"; });
    return 0;
}

There is a difference if I use bfd and gold/lld when the -pthread and -Wl,--as-needed options are used as you can see in the following results:

* With BFD
  --------

g++ -g -c test_call_once.cpp -o test_call_once.o
g++ -g -fuse-ld=bfd -Wl,--as-needed -pthread  test_call_once.o -lm
readelf -d a.out | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libpthread.so.0]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

* With Gold
  ---------

g++ -g -c test_call_once.cpp -o test_call_once.o
g++ -g -fuse-ld=gold -Wl,--as-needed -pthread  test_call_once.o -lm
readelf -d a.out | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

* With lld (to be exhaustive)
  --------------------------

g++ -g -c test_call_once.cpp -o test_call_once.o
g++ -g -fuse-ld=lld -Wl,--as-needed -pthread  test_call_once.o -lm
readelf -d a.out | grep NEEDED
 0x0000000000000001 (NEEDED)             Shared library: [libstdc++.so.6]
 0x0000000000000001 (NEEDED)             Shared library: [libc.so.6]

In the two last cases, when I run the program, it crash with SIGABRT and following backtrace:

#0  __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:50
#1  0x00007f2cbfb05899 in __GI_abort () at abort.c:79
#2  0x00007f2cbfd6f5f6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007f2cbfd7b9ec in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007f2cbfd7ba47 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007f2cbfd7bca9 in __cxa_throw () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007f2cbfd722cc in std::__throw_system_error(int) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x000055bf57ea93a5 in std::call_once<main(int, char const**)::<lambda()> >(std::once_flag &, <lambda()> &&) (__once=..., __f=...) at /usr/include/c++/9/mutex:697
#8  0x000055bf57ea929b in main (argc=1, argv=0x7ffffc66be48) at test_call_once.cpp:8

Here are the versions of the used tools but I can reproduce with older ones too.

OS: Ubuntu 19.10
GCC: gcc (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
binutils: GNU Binutils for Ubuntu 2.33
lld: LLD 9.0.0 (compatible with GNU linkers)

My first thought was that it was because of the libc++. But as I have different behaviours with different linker, I was wondering what was the official expected behaviour.

Thanks in advance for you help,
Comment 1 Andreas Schwab 2020-01-22 09:40:55 UTC
This is a bug in libstdc++, see <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55394>.
Comment 2 laurent.stacul 2020-01-22 10:13:20 UTC
Thanks for this precious information.
Regards,
Laurent