Menu

#557 Crash if compiled with -flto, -static and iostream is used

v1.0 (example)
open-accepted
niXman
None
5
2019-02-24
2016-08-24
No

Hi guys,

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.

1 Attachments

Discussion

  • Mateusz Mikuła

    Mateusz Mikuła - 2016-09-15

    Testing with MSYS2 mingw-w64 gcc:
    32 bit: no crash
    64 bit:

    (gdb) run
    Starting program: C:\Users\mati865\Desktop\a.exe
    [New Thread 5668.0x1898]
    [New Thread 5668.0x31c]
    
    Thread 1 received signal SIGSEGV, Segmentation fault.
    0x00000000004d00f0 in __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)
        at D:/test/msys64/mingw64/include/c++/6.1.0/iostream:74
    #6  0x0000000000402fac in _GLOBAL__sub_I__Z9get_regexB5cxx11v ()
        at main.cpp:26
    #7  0x0000000000428b59 in __do_global_ctors ()
        at C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:44
    #8  0x0000000000428baf in __main ()
        at C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/gccmain.c:60
    #9  0x00000000004013cb in __tmainCRTStartup ()
        at C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:324
    #10 0x000000000040151b in mainCRTStartup ()
        at C:/repo/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crtexe.c:212
    
     

    Last edit: Mateusz Mikuła 2016-09-15
  • niXman

    niXman - 2016-09-16
    • status: open --> open-accepted
    • assigned_to: niXman
     
  • niXman

    niXman - 2016-09-16

    Does anyone have any ideas?

     
  • niXman

    niXman - 2016-10-09
    • status: open-accepted --> closed-fixed
     
  • niXman

    niXman - 2016-10-09

    Fixed in 6.2.0

     
    • Alex Alabuzhev

      Alex Alabuzhev - 2016-10-09
       
      • niXman

        niXman - 2016-10-09

        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

        nixman@nixman-pc MSYS ~
        $ g++ -v
        Using built-in specs.
        COLLECT_GCC=C:\mingw32-6.2.0-r1\bin\g++.exe
        COLLECT_LTO_WRAPPER=C:/mingw32-6.2.0-r1/bin/../libexec/gcc/i686-w64-mingw32/6.2.0/lto-wrapper.exe
        Target: i686-w64-mingw32
        Configured with: ../../../src/gcc-6.2.0/configure --host=i686-w64-mingw32 --build=i686-w64-mingw32 --target=i686-w64-mingw32 --prefix=/mingw32 --with-sysroot=/c/mingw620/i686-620-posix-dwarf-rt_v5-rev1/mingw32 --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --enable-libstdcxx-filesystem-ts=yes --disable-sjlj-exceptions --with-dwarf2 --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=i686 --with-tune=generic --with-libiconv --with-system-zlib --with-gmp=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-mpfr=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-mpc=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-isl=/c/mingw620/prerequisites/i686-w64-mingw32-static --with-pkgversion='i686-posix-dwarf-rev1, Built by MinGW-W64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 CFLAGS='-O2 -pipe -I/c/mingw620/i686-620-posix-dwarf-rt_v5-rev1/mingw32/opt/include -I/c/mingw620/prerequisites/i686-zlib-static/include -I/c/mingw620/prerequisites/i686-w64-mingw32-static/include' CXXFLAGS='-O2 -pipe -I/c/mingw620/i686-620-posix-dwarf-rt_v5-rev1/mingw32/opt/include -I/c/mingw620/prerequisites/i686-zlib-static/include -I/c/mingw620/prerequisites/i686-w64-mingw32-static/include' CPPFLAGS= LDFLAGS='-pipe -L/c/mingw620/i686-620-posix-dwarf-rt_v5-rev1/mingw32/opt/lib -L/c/mingw620/prerequisites/i686-zlib-static/lib -L/c/mingw620/prerequisites/i686-w64-mingw32-static/lib -Wl,--large-address-aware'
        Thread model: posix
        gcc version 6.2.0 (i686-posix-dwarf-rev1, Built by MinGW-W64 project)

        nixman@nixman-pc MSYS ~
        $ cat 557.cpp

        #include <regex>
        // if the #include <iostream> is commented - crash doesn't occur
        #include <iostream>
        
        const std::regex& get_regex()
        {
                static std::regex re;
        
                static bool Inited = true; //false
        
                // this block shall never be entered, as Inited is set to true
                if (!Inited)
                {
                        // if the next line is commented - crash doesn't occur
                        re.assign("");
        
                        Inited = true;
                }
                return re;
        }
        
        int main()
        {
                auto z = get_regex();
                std::cout << "ping!" << std::endl;
         }
        

        nixman@nixman-pc MSYS ~
        $ g++ -std=c++11 557.cpp -o557 -static -flto && ./557
        ping!

         

        Last edit: niXman 2016-10-09
        • Alex Alabuzhev

          Alex Alabuzhev - 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).

           
          • niXman

            niXman - 2016-10-09

            Hmm... It seems this is x86_64 target bug. I`ll see.

             
  • niXman

    niXman - 2016-10-09
    • status: closed-fixed --> open-accepted
     
  • xnor

    xnor - 2019-02-22

    Years later, 64 bit binaries produced with -flto still crash. Is there ever going to be a fix for this?

     
  • Mateusz Mikuła

    Mateusz Mikuła - 2019-02-22

    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.

     
  • xnor

    xnor - 2019-02-23

    So does this bug need to be reported somewhere else?

     
    • Mateusz Mikuła

      Mateusz Mikuła - 2019-02-24

      Yes this report belongs to Binutils or GCC bugzilla, I'm not sure which one.

       

Log in to post a comment.