Skip to content
Permalink
Browse files

Fix MSVC2015 build.

  • Loading branch information...
dvander committed Mar 17, 2019
1 parent 364cd09 commit 2e11d3400e20de59b2f6a7b921d9687796dc4f2c
Showing with 78 additions and 5 deletions.
  1. +78 −5 extensions/mysql/msvc15hack.c
@@ -1,12 +1,85 @@
// Adapted from dosmap.c in Visual Studio 12.0 CRT sources.

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <stdlib.h>

// Fix from from https://stackoverflow.com/a/34655235.
//
// The _dosmaperr function is required by the MySQL lib we use,
// __iob_func required by the MySQL we use,
// but no longer exists in the VS 14.0+ crt.

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment(lib, "DbgHelp.lib")
#pragma warning(disable:4091) // 'typedef ': ignored on left of '' when no variable is declared
#include <DbgHelp.h>
#include <corecrt_wstdio.h>

#include <stdlib.h>
#define GET_CURRENT_CONTEXT(c, contextFlags) \
do { \
c.ContextFlags = contextFlags; \
__asm call x \
__asm x: pop eax \
__asm mov c.Eip, eax \
__asm mov c.Ebp, ebp \
__asm mov c.Esp, esp \
} while(0);


FILE * __cdecl __iob_func(void)
{
CONTEXT c = { 0 };
STACKFRAME64 s = { 0 };
DWORD imageType;
HANDLE hThread = GetCurrentThread();
HANDLE hProcess = GetCurrentProcess();

GET_CURRENT_CONTEXT(c, CONTEXT_FULL);

imageType = IMAGE_FILE_MACHINE_I386;
s.AddrPC.Offset = c.Eip;
s.AddrPC.Mode = AddrModeFlat;
s.AddrFrame.Offset = c.Ebp;
s.AddrFrame.Mode = AddrModeFlat;
s.AddrStack.Offset = c.Esp;
s.AddrStack.Mode = AddrModeFlat;

if (!StackWalk64(imageType, hProcess, hThread, &s, &c, NULL, SymFunctionTableAccess64, SymGetModuleBase64, NULL))
{
return NULL;
}

if (s.AddrReturn.Offset == 0)
{
return NULL;
}

{
unsigned char const * assembly = (unsigned char const *)(s.AddrReturn.Offset);

if (*assembly == 0x83 && *(assembly + 1) == 0xC0 && (*(assembly + 2) == 0x20 || *(assembly + 2) == 0x40))
{
if (*(assembly + 2) == 32)
{
return (FILE*)((unsigned char *)stdout - 32);
}
if (*(assembly + 2) == 64)
{
return (FILE*)((unsigned char *)stderr - 64);
}

}
else
{
return stdin;
}
}

return NULL;
}

// Adapted from dosmap.c in Visual Studio 12.0 CRT sources.
//
// The _dosmaperr function is required by the MySQL lib we use,
// but no longer exists in the VS 14.0+ crt.

static struct errentry
{

0 comments on commit 2e11d34

Please sign in to comment.
You can’t perform that action at this time.