Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mingw64 from msys2 creates corrupt executable #287

Closed
BitingPanda opened this issue Jul 30, 2023 · 10 comments
Closed

Mingw64 from msys2 creates corrupt executable #287

BitingPanda opened this issue Jul 30, 2023 · 10 comments

Comments

@BitingPanda
Copy link

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.

I also posted this on StackOverflow for help. Here is the link

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

@Biswa96
Copy link
Member

Biswa96 commented Jul 30, 2023

I can not reproduce the issue in my system. Please make sure all the packages are updated using pacman -Syyu command.

@BitingPanda
Copy link
Author

I can not reproduce the issue in my system. Please make sure all the packages are updated using pacman -Syyu command.

Done it. Even reinstalled the whole thing from scratch. Still doesn't work. @Biswa96

@Biswa96
Copy link
Member

Biswa96 commented Jul 30, 2023

  • Where are you running g++ command?
  • What is the output of which g++.exe command?
  • Did you check if antivirus is not messing up the msys2 installation?
  • Did you check if ASLR is not forcefully enabled for all executables?

@lazka
Copy link
Member

lazka commented Jul 30, 2023

Note that your example will depend on the following DLLs, so make sure they are findable:

$ ntldd -R main.exe  | grep mingw64
        libstdc++-6.dll => C:\msys64\mingw64\bin\libstdc++-6.dll (0x000002677ac20000)
         libgcc_s_seh-1.dll => C:\msys64\mingw64\bin\libgcc_s_seh-1.dll (0x000002677a9f0000)
         libwinpthread-1.dll => C:\msys64\mingw64\bin\libwinpthread-1.dll (0x000002677a780000)

@BitingPanda
Copy link
Author

BitingPanda commented Jul 30, 2023

@lazka if i run this $ ntldd -R main.exe | grep mingw64 it shows nothing.
For both the corrupt and the working executable(created with an older version of mingw(native) version-12.3.0

However, I manually found the files using Windows Explorer, and they are in the mentioned directory.

@lazka
Copy link
Member

lazka commented Jul 30, 2023

@lazka if i run this $ ntldd -R main.exe | grep mingw64 it shows nothing.

hmm, so what does ntldd main.exe show?

@BitingPanda
Copy link
Author

BitingPanda commented Jul 31, 2023

nurpr@Legion MSYS /c/Users/nurpr/Desktop
$ ntldd main.exe

nurpr@Legion MSYS /c/Users/nurpr/Desktop

It literally doesn't show anything. @lazka

@Biswa96
Copy link
Member

Biswa96 commented Aug 3, 2023

This issue can not be reproduced and only happens in your system. The same gcc compiler toolchain is being used to compile mingw packages.

@Biswa96 Biswa96 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
@BitingPanda
Copy link
Author

Apparently, as I installed Qt, it installed it's own version of mingw64 which was messing with my default g++ installation. Uninstalling Qt fixed it.

@lazka
Copy link
Member

lazka commented Aug 3, 2023

thanks for the update

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants