Program compiled with gcc 6.1 crashes if compiled with -flto and -static and iostream header is included.
Please see the minimal example in attachment.
It works as expected on native linux version so I assume it's mingw-specific bug.
Testing with MSYS2 mingw-w64 gcc:
32 bit: no crash
64 bit:
(gdb)runStartingprogram:C:\Users\mati865\Desktop\a.exe[NewThread5668.0x1898][NewThread5668.0x31c]Thread1receivedsignalSIGSEGV,Segmentationfault.0x00000000004d00f0in__dynamic_cast()(gdb)bt#0 0x00000000004d00f0 in __dynamic_cast ()#1 0x00000000004cb565 in bool std::has_facet<std::ctype<char> >(std::locale const&) ()#2 0x00000000004c5e94 in std::basic_ios<char, std::char_traits<char> >::_M_cache_locale(std::locale const&) ()#3 0x00000000004c6000 in std::basic_ios<char, std::char_traits<char> >::init(std::basic_streambuf<char, std::char_traits<char> >*) ()#4 0x00000000004c456d in std::ios_base::Init::Init() ()#5 0x0000000000402d7f in __static_initialization_and_destruction_0 (__initialize_p=1,__priority=65535)atD:/test/msys64/mingw64/include/c++/6.1.0/iostream:74#6 0x0000000000402fac in _GLOBAL__sub_I__Z9get_regexB5cxx11v ()atmain.cpp:26#7 0x0000000000428b59 in __do_global_ctors ()atC:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:44#8 0x0000000000428baf in __main ()atC:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:60#9 0x00000000004013cb in __tmainCRTStartup ()atC:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:324#10 0x000000000040151b in mainCRTStartup ()atC:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:212
Last edit: Mateusz Mikuła 2016-09-15
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
#include<regex>// if the #include <iostream> is commented - crash doesn't occur#include<iostream>conststd::regex&get_regex(){staticstd::regexre;staticboolInited=true;//false// this block shall never be entered, as Inited is set to trueif(!Inited){// if the next line is commented - crash doesn't occurre.assign("");Inited=true;}returnre;}intmain(){autoz=get_regex();std::cout<<"ping!"<<std::endl;}
Testing with MSYS2 mingw-w64 gcc:
32 bit: no crash
64 bit:
Last edit: Mateusz Mikuła 2016-09-15
Does anyone have any ideas?
Fixed in 6.2.0
Gould you provide an url please?
Example above compiled with this one 6.2.0 - https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/Personal%20Builds/mingw-builds/6.2.0/threads-win32/seh/x86_64-6.2.0-release-win32-seh-rt_v5-rev1.7z/download - crashes just as before.
Tested using this build: https://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/Personal%20Builds/mingw-builds/6.2.0/threads-posix/dwarf/i686-6.2.0-release-posix-dwarf-rt_v5-rev1.7z/download
Last edit: niXman 2016-10-09
You're testing a Win32 build, while problem is with Win64 (see the bat file in my example.zip and Mateusz's comment above).
Hmm... It seems this is x86_64 target bug. I`ll see.
Years later, 64 bit binaries produced with -flto still crash. Is there ever going to be a fix for this?
It's binutils/GCC bug and it should be fixed there.
When using 64 bit mingw-w64 Clang with libc++ and lld it works fine, GCC 8.2 still crashes.
So does this bug need to be reported somewhere else?
Yes this report belongs to Binutils or GCC bugzilla, I'm not sure which one.