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

Bad return code before reaching main when fprintf is linked with aarch64-w64-mingw32-g++ executable #198

Open
Blackhex opened this issue Sep 20, 2024 · 1 comment
Labels
C++ C++ Support

Comments

@Blackhex
Copy link
Member

Blackhex commented Sep 20, 2024

Issue

When the following C/C++ code:

#include <stdio.h>

int main () {
    FILE *f = fopen ("test.txt", "w");
    fprintf (f, "%d\n", 10);
    return 0;
}

is compiled with aarch64-w64-mingw32-g++ the control flow does not reach main and return code 127 or 123 is returned.

  • When the same code is compiled with aarch64-w64-mingw32-gcc, it works fine.
  • When fprintf line is removed, it works fine.
  • Other functions like printf work fine.
  • When object files are produced from this source code first, both with aarch64-w64-mingw32-gcc and aarch64-w64-mingw32-g++ they are identical and when then they are linked with aarch64-w64-mingw32-g++ it fails while with aarch64-w64-mingw32-gcc it works.
  • The issue happens only when shared runtime libraries are linked, with -static the binary works fine.
  • It seems that the C++ linked binaries are not loading libgcc_s_seh-1.dll correctly while C linked ones does.
  • Disassembled EXE produced by aarch64-w64-mingw32-g++ bad-exe.txt and by aarch64-w64-mingw32-gcc good-exe.txt linkers are showing some differences.
@Blackhex Blackhex changed the title Bad return code before reaching main when fprintf is linked with aarch64-w64-mingw32-g++ executable Bad return code before reaching main when fprintf is linked with aarch64-w64-mingw32-g++ executable Sep 20, 2024
@Blackhex Blackhex added the C++ C++ Support label Sep 20, 2024
@ZacWalk
Copy link
Member

ZacWalk commented Sep 23, 2024

Interesting: fprintf itself is part of the C standard library, using it in a C++ program might indirectly trigger the initialization of parts of the C++ standard library. Constructors etc.

@Blackhex it might be worth taking a look at .init_array or the ctors list in the c++ output. Looking at the disam, ctors looks the same for both but a little weird for c++

0000000140008d78 <__CTOR_LIST__>:
   140008d78:	ffffffff 	.inst	0xffffffff ; undefined
   140008d7c:	ffffffff 	.inst	0xffffffff ; undefined`

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

No branches or pull requests

2 participants