mingw-woarm64-build icon indicating copy to clipboard operation
mingw-woarm64-build copied to clipboard

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

Open Blackhex opened this issue 5 months ago • 1 comments

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 avatar Sep 20 '24 10:09 Blackhex