在 2024-10-17 17:13, ericxu...@protonmail.com 写道:
Using your method, I replaced "int main()" with "int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, 
LPSTR lpCmdLine, int nShowCmd)", and the result was more assembly code added than the previous version. After reverse 
engineering, I found that the result compiled with Visual Studio was very similar, with added "_tmainCRTStartup", 
"_InterlockedCompareExchange64", "_main()" and other codes. I guess these codes are used to process the 
parameter information of the running program.

The code is always there [1] even you don't use those parameters. You may pass `-nostartfiles` to GCC, but then be careful:

1. There shall be an entry-point function, which can be named arbitrarily,
   but it must return `int` and take no argument.
2. The symbolic name to this function shall be passed to GCC with
   `-Wl,--entry,ENTRYPOINT` when linking.
3. For x86-32, ESP is not aligned properly, and the function should be
   declared `__attribute__((__force_align_arg_pointer__))`.
4. For x86-32 and x86-64, the x87 FPU is initialized to 53-bit precision.
   If `long double` is used, `__asm__ ("fninit");` is required.
5. Auto-import [2] may be unusable, and there may be undefined references
   to `_pei386_runtime_relocator`. It is sometimes necessary to add
   `__declspec(dllimport)` accordingly.


[1] https://github.com/mingw-w64/mingw-w64/blob/cdf6b16b805ce7d02f6b1b742911ba0770b49bbb/mingw-w64-crt/crt/crtexe.c#L203-L267
[2] https://sourceware.org/binutils/docs/ld/WIN32.html

--
Best regards,
LIU Hao

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature

Reply via email to