You are not logged in.

#1 2022-02-15 19:00:27

lopster
Member
Registered: 2021-10-11
Posts: 8

[SOLVED] Segfault in statically linked C++ program with new tool chain

The program below compiled and linked statically with

g++ -static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive cv.cpp

segfaults in the condition_variable destructor on my machine with latest updates to the arch toolchain. It works with the previous version. Can anyone else duplicate this or have insight into where the problem lies?

Thank you!

#include <condition_variable>
int main() {
    std::condition_variable v;
    return 0;
}

Last edited by lopster (2022-02-15 23:42:40)

Offline

#2 2022-02-15 19:11:29

lopster
Member
Registered: 2021-10-11
Posts: 8

Re: [SOLVED] Segfault in statically linked C++ program with new tool chain

More details: Compiling it with

g++ -static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive cv.cpp

is necessary to get it to work with the old toolchain. That doesn't work with the new toolchain, however. Perhaps the pthread library is not built right in the new stuff?

Offline

#3 2022-02-15 19:12:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,444
Website

Re: [SOLVED] Segfault in statically linked C++ program with new tool chain

lopster wrote:

Can anyone else duplicate this ...

Yes, I can confirm duplication of the problem as described.  But I have no insight to offer towards a solution.

EDIT: I see most of my pacman mirrors recently dropped off the supported list and I didn't have the newest gcc.  I've updated now and re-confirmed the symptoms.  However this suggests it is not just with the newest toolchain as I saw the same results with both gcc 11.1.0-3 and 11.2.0-3.

Last edited by Trilby (2022-02-15 19:17:44)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Online

#4 2022-02-15 19:34:33

lopster
Member
Registered: 2021-10-11
Posts: 8

Re: [SOLVED] Segfault in statically linked C++ program with new tool chain

Trilby wrote:

EDIT: I see most of my pacman mirrors recently dropped off the supported list and I didn't have the newest gcc.  I've updated now and re-confirmed the symptoms.  However this suggests it is not just with the newest toolchain as I saw the same results with both gcc 11.1.0-3 and 11.2.0-3.

Thanks for checking. I had the original build command wrong and edited it. The corrected command should work with the old toolchain.

It looks like the problem is that /usr/lib/libpthread.a in the new toolchain is 8 bytes long containing just

!<arch>

I'll file a bug about that.

Offline

#5 2022-02-15 20:01:59

Scimmia
Fellow
Registered: 2012-09-01
Posts: 11,463

Re: [SOLVED] Segfault in statically linked C++ program with new tool chain

Check the release notes for glibc 2.34 for more info. That is not your problem.

Offline

#6 2022-02-15 23:34:25

lopster
Member
Registered: 2021-10-11
Posts: 8

Re: [SOLVED] Segfault in statically linked C++ program with new tool chain

Scimmia wrote:

Check the release notes for glibc 2.34 for more info. That is not your problem.

Thank you for pointing me to the glibc release notes.

If anyone else comes across this, more details about this change are here: Why glibc 2.34 removed libpthread

This issue with condition_variable and static linking has been around for almost 10 years: C++11's condition variables fail with static linking

I attempted to --whole-archive glibc but that resulted in linker errors.

Putting unused pthread calls in the program as mentioned in the gcc bug report fixes this example and the larger program where I originally encountered this issue. The working code is:

#include <condition_variable>
#include <pthread.h>
void pthread_cond_bug() {
        pthread_cond_signal((pthread_cond_t *) nullptr);
        pthread_cond_init((pthread_cond_t *) nullptr,
                          (const pthread_condattr_t *) nullptr);
        pthread_cond_destroy((pthread_cond_t *) nullptr);
        pthread_cond_timedwait((pthread_cond_t *) nullptr, (pthread_mutex_t *)
                               nullptr, (const struct timespec *) nullptr);
        pthread_cond_wait((pthread_cond_t *) nullptr, (pthread_mutex_t *) nullptr);
}
int main() {
    std::condition_variable v;
    return 0;
}

Offline

Board footer

Powered by FluxBB