crt: crtdll.dll and msvcrt10.dll: Fix linking of the WinMain() function

WinMain() entry point provided by the application is called by mingw-w64
startup code, more precisely by function main() from crtexewin.c file.

mingw-w64 startup code in file crtexewin.c calls _ismbblead() function.
Function _ismbblead() is not available in some versions of crtdll.dll
and is completely missing in msvcrt10.dll library.

Version of crtdll.dll library stored in its PE resource directory is not
meaningful as two different libraries, one with _ismbblead symbol and one
without _ismbblead symbol has same version.

Seems that there are MBCS aware crtdll.dll versions with _ismbblead symbol
and versions which are not MBCS aware without _ismbblead symbol.

All checked msvcrt10.dll files do not have _ismbblead symbols, so
msvcrt10.dll is not MBCS awre.

For msvcrt10.dll import library provides simple dummy _ismbblead() function
which always returns false to satisfy mingw-w64 startup file crtexewin.c.

For crtdll.dll import library provides _ismbblead() wrapper function and
disable the real function in the def file to prevent symbol conflicts. The
wrapper function via GetProcAddress() checks if the real crtdll.dll
provides _ismbblead() function. If real function exists then it is called.
If not then wrapper function returns false like the msvcrt10.dll one.

This fixes linking WinMain() entry point with msvcrt10.dll.

  ld: /usr/local/lib/libmingw32.a(lib32_libmingw32_a-crtexewin.o): in function `main':
  mingw-w64/mingw-w64-crt/crt/crtexewin.c:43: undefined reference to `_ismbblead'
  collect2: error: ld returned 1 exit status

And fixes runtime error about missing _ismbblead symbol with some versions of crtdll.dll.

  Entry Point Not Found
  The procedure entry point _ismbblead could not be located in the
  dynamic link library CRTDLL.dll.

Signed-off-by: Martin Storsjö <martin@martin.st>
This commit is contained in:
Pali Rohár 2024-06-02 14:59:03 +02:00 committed by Martin Storsjö
parent 6bddd5f9f1
commit fd242b3ae4
4 changed files with 62 additions and 1 deletions

View File

@ -575,6 +575,7 @@ src_msvcrtarm64=\
src_crtdll=\
crt/crtdll_getmainargs.c \
crt/crtdll_ismbblead.c \
math/x86/_copysignf.c \
misc/___mb_cur_max_func.c \
misc/__initenv.c \
@ -599,6 +600,7 @@ src_crtdll=\
src_msvcrt10=\
crt/crtdll_getmainargs.c \
crt/msvcrt10_ismbblead.c \
misc/___mb_cur_max_func.c \
misc/__initenv.c \
misc/__p___argv.c \

View File

@ -0,0 +1,45 @@
/**
* 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 <internal.h>
#include <windows.h>
/**
* Implementation of _ismbblead() function called by crtexewin.c startup code
* for crtdll.dll library which in some versions does not contain this function.
*/
static int __cdecl emu_ismbblead(unsigned int __UNUSED_PARAM(_C))
{
/* Fallback implementation for crtdll.dll version which is not MBCS aware. */
return 0;
}
static int __cdecl init_ismbblead(unsigned int _C);
int (__cdecl *__MINGW_IMP_SYMBOL(_ismbblead))(unsigned int _C) = init_ismbblead;
static int __cdecl init_ismbblead(unsigned int _C)
{
HMODULE crtdll;
int (__cdecl *func)(unsigned int _C) = NULL;
crtdll = GetModuleHandleA("crtdll.dll");
if (crtdll)
func = (int (__cdecl *)(unsigned int _C))GetProcAddress(crtdll, "_ismbblead");
if (!func)
func = emu_ismbblead;
return (__MINGW_IMP_SYMBOL(_ismbblead) = func)(_C);
}
int __cdecl _ismbblead(unsigned int _C);
int __cdecl _ismbblead(unsigned int _C)
{
return __MINGW_IMP_SYMBOL(_ismbblead)(_C);
}

View File

@ -0,0 +1,14 @@
/**
* 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 <internal.h>
/**
* Implementation of _ismbblead() function called by crtexewin.c startup code
* for msvcrt10.dll library which does not contain this function and is not MBCS aware.
*/
int __cdecl _ismbblead(unsigned int _C);
int __cdecl _ismbblead(unsigned int __UNUSED_PARAM(_C)) { return 0; }

View File

@ -227,7 +227,7 @@ _ismbbgraph
_ismbbkalnum
_ismbbkana
_ismbbkpunct
_ismbblead
; _ismbblead is replaced by emu
_ismbbprint
_ismbbpunct
_ismbbtrail