On Sat, 12 Nov 2022, Pali Rohár wrote:

When application does not use WinMain() or wWinMain() entry point then
there is no need to compute arguments for these entry points.

So move WinMain/wWinMain specific code from crtexe.c file to crtexewin.c
file. This new crtexewin.c file compute arguments for WinMain/wWinMain
function and then directly call it. This new file replaces old crt0_c.c
and ucrtexewin.c replaces old crt0_w.c file.

Moving WinMain/wWinMain specific code outside of crtexe.c file reduces size
of all executables which do not use WinMain entry point, so all console
applications.

Old name crt0 is confusing as it is NOT linker entry point (like crt1.o
and crt2.o). So use new name crtexewin which should indicate that it is for
WinMain function. And to reduce code repetition for non-unicode and unicode
variant, define WinMain via tchar.t and unicode variant in ucrtexewin.c
just include crtexewin.c like other u<file>.c implementations.

This change decrease size of simple 'int main() { return 0; }' console
program from 9728 bytes to 9216 bytes when compiled with -Os -s flags.
Similar simple GUI program with WinMain() does not change its size.
---
mingw-w64-crt/Makefile.am      |  4 +-
mingw-w64-crt/crt/crt0_c.c     | 20 ----------
mingw-w64-crt/crt/crt0_w.c     | 25 ------------
mingw-w64-crt/crt/crtexe.c     | 50 +-----------------------
mingw-w64-crt/crt/crtexewin.c  | 71 ++++++++++++++++++++++++++++++++++
mingw-w64-crt/crt/ucrtexewin.c | 15 +++++++
6 files changed, 89 insertions(+), 96 deletions(-)
delete mode 100644 mingw-w64-crt/crt/crt0_c.c
delete mode 100644 mingw-w64-crt/crt/crt0_w.c
create mode 100644 mingw-w64-crt/crt/crtexewin.c
create mode 100644 mingw-w64-crt/crt/ucrtexewin.c

This looks mostly reasonable to me - let me include it in my next nightly builds to see if it turns up any surprises.

I noticed one factorization which does change behaviour in the code though:

@@ -283,40 +269,6 @@ __tmainCRTStartup (void)

    _fpreset ();

-    __mingw_winmain_hInstance = (HINSTANCE) &__ImageBase;
-
-#ifdef WPRFLAG
-    lpszCommandLine = (_TCHAR *) _wcmdln;
-#else
-    lpszCommandLine = (char *) _acmdln;
-#endif
-
-    if (lpszCommandLine)
-      {
-       while (*lpszCommandLine > SPACECHAR || (*lpszCommandLine && 
inDoubleQuote))
-         {
-           if (*lpszCommandLine == DQUOTECHAR)
-             inDoubleQuote = !inDoubleQuote;
-#ifdef _MBCS

The existing code checked the flag _MBCS here. I don't see anything in mingw-w64 setting this flag (nothing in headers, nothing in crt makefiles). I also grepped through both GCC and Clang source, and there's no mentions of this define anywhere there either, so it doesn't seem to be predefined by the compiler.

Thus, I would think that this codepath isn't actually in use. So therefore, changing it into "#ifndef _UNICODE" is a functional change here.

The change is probably benign/beneficial though, but it would be good to separate out such funtional changes and/or acknowledge them clearly.

// Martin

_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to