Search code examples
c++assemblymingw-w64msys2

Mingw64 from msys2 creates corrupt executable


I am currently using MSYS_NT-10.0-22621 Legion 3.4.7.x86_64 2023-07-14 16:57 UTC x86_64 Msys and using mingw64

My mingw64 version is

g++.exe (Rev7, Built by MSYS2 project) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I wrote a simple main.cpp file which contained.

#include <iostream>


int main()
{
    std::cout<<"Hello world"<<std::endl;
}

However, while building it, even though it creates a .exe file, it doesn't output anything.

So, I install a previous version, using Mingw Natively. And my version was

g++.exe (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 12.3.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

For Comparison, I compiled the same file into assembly.

12.3.0

    .file   "main.cpp"
    .text
.lcomm _ZStL8__ioinit,1,1
    .def    __main; .scl    2;  .type   32; .endef
    .section .rdata,"dr"
.LC0:
    .ascii "Hello world\0"
    .text
    .globl  main
    .def    main;   .scl    2;  .type   32; .endef
    .seh_proc   main
main:
.LFB1991:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    call    __main
    leaq    .LC0(%rip), %rax
    movq    %rax, %rdx
    movq    .refptr._ZSt4cout(%rip), %rax
    movq    %rax, %rcx
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movq    %rax, %rcx
    movq    .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rax
    movq    %rax, %rdx
    call    _ZNSolsEPFRSoS_E
    movl    $0, %eax
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .def    __tcf_0;    .scl    3;  .type   32; .endef
    .seh_proc   __tcf_0
__tcf_0:
.LFB2524:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    leaq    _ZStL8__ioinit(%rip), %rax
    movq    %rax, %rcx
    call    _ZNSt8ios_base4InitD1Ev
    nop
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .def    _Z41__static_initialization_and_destruction_0ii;    .scl    3;  .type   32; .endef
    .seh_proc   _Z41__static_initialization_and_destruction_0ii
_Z41__static_initialization_and_destruction_0ii:
.LFB2523:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    movl    %ecx, 16(%rbp)
    movl    %edx, 24(%rbp)
    cmpl    $1, 16(%rbp)
    jne .L6
    cmpl    $65535, 24(%rbp)
    jne .L6
    leaq    _ZStL8__ioinit(%rip), %rax
    movq    %rax, %rcx
    call    _ZNSt8ios_base4InitC1Ev
    leaq    __tcf_0(%rip), %rax
    movq    %rax, %rcx
    call    atexit
.L6:
    nop
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .def    _GLOBAL__sub_I_main;    .scl    3;  .type   32; .endef
    .seh_proc   _GLOBAL__sub_I_main
_GLOBAL__sub_I_main:
.LFB2525:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    movl    $65535, %edx
    movl    $1, %ecx
    call    _Z41__static_initialization_and_destruction_0ii
    nop
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .section    .ctors,"w"
    .align 8
    .quad   _GLOBAL__sub_I_main
    .ident  "GCC: (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 12.3.0"
    .def    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;    .scl    2;  .type   32; .endef
    .def    _ZNSolsEPFRSoS_E;   .scl    2;  .type   32; .endef
    .def    _ZNSt8ios_base4InitD1Ev;    .scl    2;  .type   32; .endef
    .def    _ZNSt8ios_base4InitC1Ev;    .scl    2;  .type   32; .endef
    .def    atexit; .scl    2;  .type   32; .endef
    .section    .rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, "dr"
    .globl  .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .linkonce   discard
.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_:
    .quad   _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .section    .rdata$.refptr._ZSt4cout, "dr"
    .globl  .refptr._ZSt4cout
    .linkonce   discard
.refptr._ZSt4cout:
    .quad   _ZSt4cout

And the 13.1 outputs

    .file   "main.cpp"
    .text
    .def    __main; .scl    2;  .type   32; .endef
    .section .rdata,"dr"
.LC0:
    .ascii "Hello world\0"
    .text
    .globl  main
    .def    main;   .scl    2;  .type   32; .endef
    .seh_proc   main
main:
.LFB2073:
    pushq   %rbp
    .seh_pushreg    %rbp
    movq    %rsp, %rbp
    .seh_setframe   %rbp, 0
    subq    $32, %rsp
    .seh_stackalloc 32
    .seh_endprologue
    call    __main
    leaq    .LC0(%rip), %rax
    movq    %rax, %rdx
    movq    .refptr._ZSt4cout(%rip), %rax
    movq    %rax, %rcx
    call    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc
    movq    %rax, %rcx
    movq    .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_(%rip), %rax
    movq    %rax, %rdx
    call    _ZNSolsEPFRSoS_E
    movl    $0, %eax
    addq    $32, %rsp
    popq    %rbp
    ret
    .seh_endproc
    .section .rdata,"dr"
_ZNSt8__detail30__integer_to_chars_is_unsignedIjEE:
    .byte   1
_ZNSt8__detail30__integer_to_chars_is_unsignedImEE:
    .byte   1
_ZNSt8__detail30__integer_to_chars_is_unsignedIyEE:
    .byte   1
    .ident  "GCC: (Rev7, Built by MSYS2 project) 13.1.0"
    .def    _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc;    .scl    2;  .type   32; .endef
    .def    _ZNSolsEPFRSoS_E;   .scl    2;  .type   32; .endef
    .section    .rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_, "dr"
    .globl  .refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .linkonce   discard
.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_:
    .quad   _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_
    .section    .rdata$.refptr._ZSt4cout, "dr"
    .globl  .refptr._ZSt4cout
    .linkonce   discard
.refptr._ZSt4cout:
    .quad   _ZSt4cout

My command was same for the both version, (without any optimisation option)

g++ .\main.cpp -S

for executable

g++ .\main.cpp -o .\main.exe

Using pacman -Qq | grep gcc in msys2shell, mingw64shell and ucrt64shell, I get these packages

gcc-libs
mingw-w64-x86_64-gcc
mingw-w64-x86_64-gcc-ada
mingw-w64-x86_64-gcc-fortran
mingw-w64-x86_64-gcc-libgfortran
mingw-w64-x86_64-gcc-libs
mingw-w64-x86_64-gcc-objc
mingw-w64-x86_64-libgccjit

Same for all the shells

When I ran $ pacman -Qi gcc-libs in the shell it returned:

Name            : gcc-libs
Version         : 11.3.0-4
Description     : Runtime libraries shipped by GCC
Architecture    : x86_64
URL             : https://gcc.gnu.org/
Licenses        : GPL  LGPL  FDL  custom
Groups          : None
Provides        : None
Depends On      : None
Optional Deps   : None
Required By     : brotli  bsdtar  file  inetutils  libasprintf  libassuan  libbz2  libdb  libexpat  libgdbm
                  libgettextpo  libgnutls  libiconv  libintl  libksba  liblz4  libnghttp2  libnpth  libpcre  libpcre2_8
                  libutil-linux  libzstd  ncurses  wget  zlib  zstd
Optional For    : None
Conflicts With  : None
Replaces        : None
Installed Size  : 5.30 MiB
Packager        : CI (msys2/msys2-autobuild/79096b75/5113523851)
Build Date      : Mon, May 29, 2023 10:03:27 PM
Install Date    : Tue, Jul 18, 2023 9:15:05 AM
Install Reason  : Installed as a dependency for another package
Install Script  : No
Validated By    : Signature

Also, for more information, I'm adding this:

$ pacman -Qi | grep gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libbz2  libiconv  libexpat  liblzma  liblz4  libopenssl  libzstd  zlib
Depends On      : gcc-libs  zlib  libbz2  liblzma  libzstd
Name            : gcc-libs
Depends On      : gcc-libs  libintl  libreadline  ncurses
Depends On      : gcc-libs
Depends On      : gcc-libs  libgpg-error
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libreadline  libiconv  libintl  ncurses
Depends On      : gcc-libs
Depends On      : gcc-libs  libidn2  libiconv  libintl  gmp  libnettle  libp11-kit  libtasn1  zlib
Depends On      : gcc-libs  libintl
Depends On      : gcc-libs  libiconv
Depends On      : gcc-libs  libgpg-error
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libintl
Depends On      : gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-binutils  mingw-w64-x86_64-crt  mingw-w64-x86_64-headers  mingw-w64-x86_64-isl  mingw-w64-x86_64-libiconv  mingw-w64-x86_64-gmp  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-mpc  mingw-w64-x86_64-gcc-libs=13.1.0-7  mingw-w64-x86_64-windows-default-manifest  mingw-w64-x86_64-winpthreads  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-gcc-libs=13.1.0-7
Name            : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-mpc  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-python  mingw-w64-x86_64-readline  mingw-w64-x86_64-xxhash  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-expat  mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-libiconv
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gettext
Required By     : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-winpthreads-git
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gmp
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-expat  mingw-w64-x86_64-bzip2  mingw-w64-x86_64-libffi  mingw-w64-x86_64-mpdecimal  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-openssl  mingw-w64-x86_64-sqlite3  mingw-w64-x86_64-tcl  mingw-w64-x86_64-tk  mingw-w64-x86_64-zlib  mingw-w64-x86_64-xz  mingw-w64-x86_64-tzdata
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-termcap
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-readline  mingw-w64-x86_64-tcl  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : gcc-libs
Depends On      : gcc-libs  libiconv  libidn2  libintl  libgpgme  libpcre2_8  libpsl  libuuid  libgnutls  zlib
Depends On      : gcc-libs
Depends On      : gcc-libs  libzstd

nurpr@Legion MSYS ~
$ pacman -Qi | grep mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-binutils  mingw-w64-x86_64-crt  mingw-w64-x86_64-headers  mingw-w64-x86_64-isl  mingw-w64-x86_64-libiconv  mingw-w64-x86_64-gmp  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-mpc  mingw-w64-x86_64-gcc-libs=13.1.0-7  mingw-w64-x86_64-windows-default-manifest  mingw-w64-x86_64-winpthreads  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-gcc-libs=13.1.0-7
Name            : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-mpc  mingw-w64-x86_64-mpfr  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-python  mingw-w64-x86_64-readline  mingw-w64-x86_64-xxhash  mingw-w64-x86_64-zlib  mingw-w64-x86_64-zstd
Depends On      : mingw-w64-x86_64-expat  mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-libiconv
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gettext
Required By     : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-winpthreads-git
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-gmp
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-expat  mingw-w64-x86_64-bzip2  mingw-w64-x86_64-libffi  mingw-w64-x86_64-mpdecimal  mingw-w64-x86_64-ncurses  mingw-w64-x86_64-openssl  mingw-w64-x86_64-sqlite3  mingw-w64-x86_64-tcl  mingw-w64-x86_64-tk  mingw-w64-x86_64-zlib  mingw-w64-x86_64-xz  mingw-w64-x86_64-tzdata
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-termcap
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-readline  mingw-w64-x86_64-tcl  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs  mingw-w64-x86_64-zlib
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs
Depends On      : mingw-w64-x86_64-gcc-libs

Running executable generated from 13.1 outputs nothing where, 12.3 executable outputs correctly. It used to work before I updated it. I also used same version of Mingw natively, and it had the same problem.

Is it some sort of bug in g++ 13.1. Why isn't it working?


Solution

  • I had Qt install and apparently that was messing up with my mingw64. The windows system path in environmental variable also had qt directory before the msys2 itself.

    After Uninstalling qt and removing it from system path fixed this issue.