From 2e11d3400e20de59b2f6a7b921d9687796dc4f2c Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sat, 16 Mar 2019 17:25:36 -0700 Subject: [PATCH] Fix MSVC2015 build. --- extensions/mysql/msvc15hack.c | 83 ++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/extensions/mysql/msvc15hack.c b/extensions/mysql/msvc15hack.c index 06abdfea..ea155503 100644 --- a/extensions/mysql/msvc15hack.c +++ b/extensions/mysql/msvc15hack.c @@ -1,13 +1,86 @@ + +#define WIN32_LEAN_AND_MEAN +#include +#include + +// Fix from from https://stackoverflow.com/a/34655235. +// +// __iob_func required by the MySQL we use, +// but no longer exists in the VS 14.0+ crt. + +#pragma comment(lib, "DbgHelp.lib") +#pragma warning(disable:4091) // 'typedef ': ignored on left of '' when no variable is declared +#include +#include + +#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. -#define WIN32_LEAN_AND_MEAN -#include - -#include - static struct errentry { DWORD oscode; // OS return value