https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107701
Patrick Palka <ppalka at gcc dot gnu.org> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|UNCONFIRMED |NEW Ever confirmed|0 |1 Last reconfirmed| |2022-11-15 --- Comment #1 from Patrick Palka <ppalka at gcc dot gnu.org> --- D'oh, not sure how I missed this. For some reason, the ios_base::Init __ioinit initializer disappears when statically linking. $ nm libstdc++.a | grep _GLOBAL__sub 0000000000000000 t _GLOBAL__sub_I_eh_alloc.cc 0000000000000000 t _GLOBAL__sub_I_bitmap_allocator.cc 0000000000000000 t _GLOBAL__sub_I.00090_ios_init.cc 0000000000000000 t _GLOBAL__sub_I_parallel_settings.cc 0000000000000000 t _GLOBAL__sub_I_cxx11_locale_inst.cc 0000000000000000 t _GLOBAL__sub_I_cxx11_wlocale_inst.cc 0000000000000000 t _GLOBAL__sub_I_future.cc 0000000000000000 t _GLOBAL__sub_I_ios_errcat.cc 0000000000000000 t _GLOBAL__sub_I_locale_inst.cc 0000000000000000 t _GLOBAL__sub_I_system_error.cc 0000000000000000 t _GLOBAL__sub_I_wlocale_inst.cc 0000000000000000 t _GLOBAL__sub_I_memory_resource.cc $ ./a.out # g++ -static gcc/testsuite/g++.old-deja/g++.benjamin/15071.C Segmentation fault $ nm a.out | grep _GLOBAL__sub 0000000000403c00 t _GLOBAL__sub_I_cxx11_locale_inst.cc 0000000000403c80 t _GLOBAL__sub_I_cxx11_wlocale_inst.cc 0000000000403a10 t _GLOBAL__sub_I_eh_alloc.cc 0000000000403d00 t _GLOBAL__sub_I_ios_errcat.cc 0000000000403890 t _GLOBAL__sub_I_locale_inst.cc 0000000000403d20 t _GLOBAL__sub_I_system_error.cc 0000000000403950 t _GLOBAL__sub_I_wlocale_inst.cc I'm not sure why some global initializers (notably _GLOBAL__sub_I.00090_ios_init.cc) are missing in the statically linked executable vs libstdc++.a? If we define the __ioinit object in e.g. c++11/ios_errcat.cc instead of c++98/ios_init.cc then the ios_base::Init initializer runs and static linking works, not sure why that is. diff --git a/libstdc++-v3/src/c++11/ios_errcat.cc b/libstdc++-v3/src/c++11/ios_errcat.cc index 12192447b8b..aa778810009 100644 --- a/libstdc++-v3/src/c++11/ios_errcat.cc +++ b/libstdc++-v3/src/c++11/ios_errcat.cc @@ -76,6 +76,8 @@ namespace std _GLIBCXX_VISIBILITY(default) { _GLIBCXX_BEGIN_NAMESPACE_VERSION +#include "../c++98/ios_base_init.h" + const error_category& iostream_category() noexcept { return io_category_instance.cat; } diff --git a/libstdc++-v3/src/c++98/ios_init.cc b/libstdc++-v3/src/c++98/ios_init.cc index 4016fcab785..1b5132f1c2d 100644 --- a/libstdc++-v3/src/c++98/ios_init.cc +++ b/libstdc++-v3/src/c++98/ios_init.cc @@ -75,8 +75,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION extern wostream wclog; #endif -#include "ios_base_init.h" - ios_base::Init::Init() { if (__gnu_cxx::__exchange_and_add_dispatch(&_S_refcount, 1) == 0)