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
diff --git a/mingw-w64-crt/Makefile.am b/mingw-w64-crt/Makefile.am
index d3896946f6f3..2056b344dd3e 100644
--- a/mingw-w64-crt/Makefile.am
+++ b/mingw-w64-crt/Makefile.am
@@ -119,8 +119,8 @@ src_libntoskrnl=libsrc/memcmp.c
src_libdloadhelper=libsrc/dloadhelper.c misc/delay-f.c
src_libmingw32=include/oscalls.h include/internal.h include/sect_attribs.h \
- crt/crt0_c.c crt/dll_argv.c crt/gccmain.c crt/natstart.c
crt/pseudo-reloc-list.c crt/wildcard.c \
- crt/charmax.c crt/crt0_w.c crt/dllargv.c crt/_newmode.c
crt/tlssup.c crt/xncommod.c \
+ crt/crtexewin.c crt/dll_argv.c crt/gccmain.c crt/natstart.c
crt/pseudo-reloc-list.c crt/wildcard.c \
+ crt/charmax.c crt/ucrtexewin.c crt/dllargv.c crt/_newmode.c
crt/tlssup.c crt/xncommod.c \
crt/cinitexe.c crt/merr.c crt/pesect.c crt/udllargc.c
crt/xthdloc.c crt/CRT_fp10.c \
crt/mingw_custom.c crt/mingw_helpers.c \
crt/pseudo-reloc.c crt/udll_argv.c \
diff --git a/mingw-w64-crt/crt/crt0_c.c b/mingw-w64-crt/crt/crt0_c.c
deleted file mode 100644
index e42dd320f4d7..000000000000
--- a/mingw-w64-crt/crt/crt0_c.c
+++ /dev/null
@@ -1,20 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-
-#include <windows.h>
-
-extern HINSTANCE __mingw_winmain_hInstance;
-extern LPSTR __mingw_winmain_lpCmdLine;
-extern DWORD __mingw_winmain_nShowCmd;
-
-/*ARGSUSED*/
-int main (int __UNUSED_PARAM(flags),
- char ** __UNUSED_PARAM(cmdline),
- char ** __UNUSED_PARAM(inst))
-{
- return (int) WinMain (__mingw_winmain_hInstance, NULL,
- __mingw_winmain_lpCmdLine, __mingw_winmain_nShowCmd);
-}
diff --git a/mingw-w64-crt/crt/crt0_w.c b/mingw-w64-crt/crt/crt0_w.c
deleted file mode 100644
index ab099c96329d..000000000000
--- a/mingw-w64-crt/crt/crt0_w.c
+++ /dev/null
@@ -1,25 +0,0 @@
-/**
- * This file has no copyright assigned and is placed in the Public Domain.
- * This file is part of the mingw-w64 runtime package.
- * No warranty is given; refer to the file DISCLAIMER.PD within this package.
- */
-#include <windows.h>
-
-/* Do the UNICODE prototyping of WinMain. Be aware that in winbase.h WinMain
is a macro
- defined to wWinMain. */
-int WINAPI wWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPWSTR
lpCmdLine,int nShowCmd);
-
-extern HINSTANCE __mingw_winmain_hInstance;
-extern LPWSTR __mingw_winmain_lpCmdLine;
-extern DWORD __mingw_winmain_nShowCmd;
-
-int wmain (int, wchar_t **, wchar_t **);
-
-/*ARGSUSED*/
-int wmain (int __UNUSED_PARAM(flags),
- wchar_t ** __UNUSED_PARAM(cmdline),
- wchar_t ** __UNUSED_PARAM(inst))
-{
- return (int) wWinMain (__mingw_winmain_hInstance, NULL,
- __mingw_winmain_lpCmdLine, __mingw_winmain_nShowCmd);
-}
diff --git a/mingw-w64-crt/crt/crtexe.c b/mingw-w64-crt/crt/crtexe.c
index 9093a2de29e7..ef7f3fd1f990 100644
--- a/mingw-w64-crt/crt/crtexe.c
+++ b/mingw-w64-crt/crt/crtexe.c
@@ -43,8 +43,6 @@ extern char *** __MINGW_IMP_SYMBOL(__initenv);
extern IMAGE_DOS_HEADER __ImageBase;
extern void _fpreset (void);
-#define SPACECHAR _T(' ')
-#define DQUOTECHAR _T('\"')
int *__cdecl __p__commode(void);
@@ -68,10 +66,6 @@ extern const PIMAGE_TLS_CALLBACK __dyn_tls_init_callback;
extern int __mingw_app_type;
-HINSTANCE __mingw_winmain_hInstance;
-_TCHAR *__mingw_winmain_lpCmdLine;
-DWORD __mingw_winmain_nShowCmd = SW_SHOWDEFAULT;
-
static int argc;
extern void __main(void);
#ifdef WPRFLAG
@@ -229,14 +223,6 @@ __attribute__((force_align_arg_pointer))
__declspec(noinline) int
__tmainCRTStartup (void)
{
- _TCHAR *lpszCommandLine = NULL;
- STARTUPINFO StartupInfo;
- WINBOOL inDoubleQuote = FALSE;
- memset (&StartupInfo, 0, sizeof (STARTUPINFO));
-
- if (__mingw_app_type)
- GetStartupInfo (&StartupInfo);
- {
void *lock_free = NULL;
void *fiberid = ((PNT_TIB)NtCurrentTeb())->StackBase;
int nested = FALSE;
@@ -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
- if (_ismbblead (*lpszCommandLine))
- {
- if (lpszCommandLine[1])
- ++lpszCommandLine;
- }
-#endif
- ++lpszCommandLine;
- }
- while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR))
- lpszCommandLine++;
-
- __mingw_winmain_lpCmdLine = lpszCommandLine;
- }
-
- if (__mingw_app_type)
- {
- __mingw_winmain_nShowCmd = StartupInfo.dwFlags & STARTF_USESHOWWINDOW ?
- StartupInfo.wShowWindow : SW_SHOWDEFAULT;
- }
duplicate_ppstrings (argc, &argv);
__main (); /* C++ initialization. */
#ifdef WPRFLAG
@@ -331,7 +283,7 @@ __tmainCRTStartup (void)
if (has_cctor == 0)
_cexit ();
- }
+
return mainret;
}
diff --git a/mingw-w64-crt/crt/crtexewin.c b/mingw-w64-crt/crt/crtexewin.c
new file mode 100644
index 000000000000..33ac1979e331
--- /dev/null
+++ b/mingw-w64-crt/crt/crtexewin.c
@@ -0,0 +1,71 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+#include <windows.h>
+#include <tchar.h>
+#include <corecrt_startup.h>
+
+#ifndef _UNICODE
+#include <mbctype.h>
+#endif
+
+#define SPACECHAR _T(' ')
+#define DQUOTECHAR _T('\"')
+
+#if defined(__GNUC__)
+#define __ImageBase __MINGW_LSYMBOL(_image_base__)
+#endif
+extern IMAGE_DOS_HEADER __ImageBase;
+
+int _tmain (int, _TCHAR **, _TCHAR **);
+int _tmain (int __UNUSED_PARAM(argc),
+ _TCHAR ** __UNUSED_PARAM(argv),
+ _TCHAR ** __UNUSED_PARAM(envp))
+{
+ HINSTANCE hInstance;
+ _TCHAR *lpCmdLine;
+ DWORD nShowCmd;
+
+ hInstance = (HINSTANCE) &__ImageBase;
+
+#ifdef _UNICODE
+ lpCmdLine = _wcmdln;
+#else
+ lpCmdLine = _acmdln;
+#endif
+ if (lpCmdLine)
+ {
+ BOOL inDoubleQuote = FALSE;
+ while (*lpCmdLine > SPACECHAR || (*lpCmdLine && inDoubleQuote))
+ {
+ if (*lpCmdLine == DQUOTECHAR)
+ inDoubleQuote = !inDoubleQuote;
+#ifndef _UNICODE
+ if (_ismbblead (*lpCmdLine))
+ {
+ if (lpCmdLine[1])
+ ++lpCmdLine;
+ }
+#endif
+ ++lpCmdLine;
+ }
+ while (*lpCmdLine && (*lpCmdLine <= SPACECHAR))
+ lpCmdLine++;
+ }
+ else
+ lpCmdLine = _TEXT("");
+
+ {
+ STARTUPINFO StartupInfo;
+ memset (&StartupInfo, 0, sizeof (STARTUPINFO));
+ GetStartupInfo (&StartupInfo);
+ if (StartupInfo.dwFlags & STARTF_USESHOWWINDOW)
+ nShowCmd = StartupInfo.wShowWindow;
+ else
+ nShowCmd = SW_SHOWDEFAULT;
+ }
+
+ return _tWinMain (hInstance, NULL, lpCmdLine, nShowCmd);
+}
diff --git a/mingw-w64-crt/crt/ucrtexewin.c b/mingw-w64-crt/crt/ucrtexewin.c
new file mode 100644
index 000000000000..51897d91ca5c
--- /dev/null
+++ b/mingw-w64-crt/crt/ucrtexewin.c
@@ -0,0 +1,15 @@
+/**
+ * This file has no copyright assigned and is placed in the Public Domain.
+ * This file is part of the mingw-w64 runtime package.
+ * No warranty is given; refer to the file DISCLAIMER.PD within this package.
+ */
+
+#ifndef UNICODE
+#define UNICODE
+#endif
+#ifndef _UNICODE
+#define _UNICODE
+#endif
+#define WPRFLAG 1
+
+#include "crtexewin.c"
--
2.20.1
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public