unofficial mirror of libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Samuel Thibault <samuel.thibault@ens-lyon.org>
To: libc-alpha@sourceware.org
Subject: as-needed and libstdc++ and pulling libpthread
Date: Sat, 4 Jan 2020 19:07:54 +0100	[thread overview]
Message-ID: <20200104180754.towsjpyfwv7b5rqi@function> (raw)

Hello,

We are having an issue with pulling libpthread with the as-needed
linking feature, which seems to be half-working only by luck on
GNU/Linux, and not working on GNU/Hurd. Basically, if I try to build:

    #include <mutex>
    
    void f(void) { }
    
    std::once_flag flag;
    int main(void) {
    	std::call_once(flag, f);
    }

with

    g++ test.cpp -o test -pthread
or
    g++ test.cpp -o test -lpthread

the link does not actually add libpthread (even if libpthread.so does
get read by the linker, as seen in -Wl,-verbose), and I am getting a
runtime error:

    terminate called after throwing an instance of 'std::system_error'
      what():  Error in unknown error system: FFFFFFFF

because libstdc++'s call_once checks that the libpthread got linked in.

The same issue can be seen on GNU/Linux when using 

    g++ test.cpp -o test -lpthread

but not with

    g++ test.cpp -o test -pthread

Because in the former case, libpthread is before libstdc++ on the link
command.


The "as-needed" feature is here on purpose avoiding to link against
libpthread, because it thinks it's not needed, because it doesn't know
that calling std::call_once will later on need libpthread. And libstdc++
itself doesn't link against libpthread on purpose, making all its
references to pthread function weak.

AFAICT, on GNU/Linux things work (by luck) with -pthread because
libstdc++ not only calls pthread functions, but also __errno_location.
printf("%p\n", errno); is indeed enough to emit a reference to
__errno_location, and then linking with -pthread will indeed bring
in libpthread. But that seems pure luck to me: should we remove
__errno_location from libpthread, we'd have the same situation. And it
doesn't work with -lpthread, programmers will wonder why.

I plan to "fix" the issue on GNU/Hurd by adding errno_location in
libpthread, so it gets the same behavior as on GNU/Linux to avoid being
exposed to such situations while GNU/Linux is not, but that's really
worrying for GNU/Linux too.

Samuel

             reply	other threads:[~2020-01-04 18:08 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-04 18:07 Samuel Thibault [this message]
2020-01-07 10:58 ` as-needed and libstdc++ and pulling libpthread Andreas Schwab

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/libc/involved.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200104180754.towsjpyfwv7b5rqi@function \
    --to=samuel.thibault@ens-lyon.org \
    --cc=libc-alpha@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).