Issue
My platform is x86_64 + Windows 10 + Cygwin. My compiler is x86_64-w64-mingw32-gcc
.
For some reason, I had to compile my program with -mabi=sysv
option, and I would like to avoid the default -mabi=ms
option if it is possible at all.
The program compiled successfully. But when it calls library functions like printf
, it segfaults. The reason is that the library functions reside in msvcrt.dll
, which was probably prebuilt with a calling convention other than -mabi=sysv
.
So, is there a way to install libraries compiled with -mabi=sysv
in Cygwin?
Solution
Based on your comment about handwritten assembly, it seems you're experiencing firsthand how not so portable assembly actually is and why historically there was a need for a more portable higher level language like C.
The reason why you have a crash is likely due to the difference in register usage between the two ABIs. When calling a function, an ABI defines what registers are associated with each of the arguments. So when you call a library function using one ABI where the library was previously compiled for a different ABI, that library code will look at its specified registers and potentially access memory leading to undefined behavior.
I think that there is a misunderstanding though that obtaining libraries will solve your problem. Think for a moment that you get what you want, the windows C runtime library compiled against the System V ABI. Now when your program calls printf(...)
you will at least have your arguments in the right registers. But the text has to be printed to the console, so the kernel has to be asked to step in and make it happen. In order to make that system call, the windows API library will be used and yet another ABI mismatch will occur. So you can see how this dilemma goes up to the top. You would basically need a complete windows OS compiled against the System V ABI, or some other form of compatibility layer like how WINE works.
You may have two options here. Rewrite the assembly to match the native windows ABI, or look into using WSL which provides a full (but virtualized) Linux environment.
Answered By - diametralpitch Answer Checked By - Cary Denson (WPSolving Admin)