weak pthread symbols in libgcc/gthr-posix.h cause issues

Szabolcs Nagy nsz@port70.net
Sun Nov 16 21:08:00 GMT 2014

the weakref magic in libgcc/gthr-posix.h is not guaranteed to work
which can at least break libstdc++ with static linking and dlopen

there are several bugs here:

- fallback code (unknown posix systems) should assume multi-threaded
application instead of using a fragile threadedness test

- determining threadedness with weak symbols is broken for dynamic
loading and static linking as well (dlopened library can pull in pthread
dependency at runtime, and with static linking a symbol does not indicate
the availability of another)

- using symbols through weak references at runtime is wrong with static
linking (it just happens to work with hacks that put a single .o into

see this analysis for more details and crashing example code:


the static linking issue there was fixed by unconditionally disabling
the weak symbols in libgcc/gthr.h when building the toolchain:


i sent this report to the libstdc++ list first but got redirected here:


the static linking issue there was worked around by using linker flags
'-Wl,--whole-archive -lpthread -Wl,--no-whole-archive'

i think upstream should fix this properly

More information about the Gcc mailing list