msvcp_main.c 8.29 KB
Newer Older
Piotr Caban's avatar
Piotr Caban committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright 2010 Piotr Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 */

#include "config.h"

#include <stdarg.h>

23 24
#include "msvcp90.h"

Piotr Caban's avatar
Piotr Caban committed
25 26 27 28
#include "windef.h"
#include "winbase.h"
#include "wine/debug.h"

29
WINE_DEFAULT_DEBUG_CHANNEL(msvcp);
Piotr Caban's avatar
Piotr Caban committed
30

31 32 33 34 35 36 37 38 39 40 41
#ifdef __i386__

#define DEFINE_VTBL_WRAPPER(off)            \
    __ASM_GLOBAL_FUNC(vtbl_wrapper_ ## off, \
        "popl %eax\n\t"                     \
        "popl %ecx\n\t"                     \
        "pushl %eax\n\t"                    \
        "movl 0(%ecx), %eax\n\t"            \
        "jmp *" #off "(%eax)\n\t")

DEFINE_VTBL_WRAPPER(0);
42
DEFINE_VTBL_WRAPPER(4);
43 44 45 46 47 48 49 50 51
DEFINE_VTBL_WRAPPER(8);
DEFINE_VTBL_WRAPPER(12);
DEFINE_VTBL_WRAPPER(16);
DEFINE_VTBL_WRAPPER(20);
DEFINE_VTBL_WRAPPER(24);
DEFINE_VTBL_WRAPPER(28);
DEFINE_VTBL_WRAPPER(32);
DEFINE_VTBL_WRAPPER(36);
DEFINE_VTBL_WRAPPER(40);
52 53 54 55
DEFINE_VTBL_WRAPPER(44);
DEFINE_VTBL_WRAPPER(48);
DEFINE_VTBL_WRAPPER(52);
DEFINE_VTBL_WRAPPER(56);
56 57 58

#endif

59
void* (__cdecl *MSVCRT_operator_new)(MSVCP_size_t);
60
void (__cdecl *MSVCRT_operator_delete)(void*);
61 62
void* (__cdecl *MSVCRT_set_new_handler)(void*);

63 64 65 66 67 68 69 70
#if _MSVCP_VER >= 110
critical_section* (__thiscall *critical_section_ctor)(critical_section*);
void (__thiscall *critical_section_dtor)(critical_section*);
void (__thiscall *critical_section_lock)(critical_section*);
void (__thiscall *critical_section_unlock)(critical_section*);
MSVCP_bool (__thiscall *critical_section_trylock)(critical_section*);
#endif

71 72 73 74
#if _MSVCP_VER >= 100
MSVCP_bool (__cdecl *Context_IsCurrentTaskCollectionCanceling)(void);
#endif

75
#define VERSION_STRING(ver) #ver
76 77
#if _MSVCP_VER >= 140
#define MSVCRT_NAME(ver) "ucrtbase.dll"
78
#define CONCRT_NAME(ver) "concrt" VERSION_STRING(ver) ".dll"
79
#else
80
#define MSVCRT_NAME(ver) "msvcr" VERSION_STRING(ver) ".dll"
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#endif

#if _MSVCP_VER >= 140
static void* __cdecl operator_new(MSVCP_size_t size)
{
    void *retval;
    int freed;

    do
    {
        retval = malloc(size);
        if (retval)
        {
            TRACE("(%ld) returning %p\n", size, retval);
            return retval;
        }
        freed = _callnewh(size);
    } while (freed);

    TRACE("(%ld) out of memory\n", size);
    throw_exception(EXCEPTION_BAD_ALLOC, "bad allocation");
    return NULL;
}

static void __cdecl operator_delete(void *mem)
{
    TRACE("(%p)\n", mem);
    free(mem);
}

void __cdecl _invalid_parameter(const wchar_t *expr, const wchar_t *func, const wchar_t *file, unsigned int line, uintptr_t arg)
{
   _invalid_parameter_noinfo();
}

int __cdecl _scprintf(const char* fmt, ...)
{
    int ret;
    __ms_va_list valist;
    __ms_va_start(valist, fmt);
    ret = __stdio_common_vsprintf(UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR, NULL, 0, fmt, NULL, valist);
    __ms_va_end(valist);
    return ret;
}
Daniel Lehman's avatar
Daniel Lehman committed
125 126 127 128 129 130 131 132 133 134

int __cdecl sprintf(char *buf, const char *fmt, ...)
{
    int ret;
    __ms_va_list valist;
    __ms_va_start(valist, fmt);
    ret = __stdio_common_vsprintf(UCRTBASE_PRINTF_STANDARD_SNPRINTF_BEHAVIOUR, buf, -1, fmt, NULL, valist);
    __ms_va_end(valist);
    return ret;
}
135
#endif
136

137
static void init_cxx_funcs(void)
138
{
139
    HMODULE hmod = GetModuleHandleA( MSVCRT_NAME(_MSVCP_VER) );
140
#if _MSVCP_VER >= 100
141 142
    HMODULE hcon = hmod;
#endif
143 144

    if (!hmod) FIXME( "%s not loaded\n", MSVCRT_NAME(_MSVCP_VER) );
145

146 147 148 149
#if _MSVCP_VER >= 140
    MSVCRT_operator_new = operator_new;
    MSVCRT_operator_delete = operator_delete;
    MSVCRT_set_new_handler = (void*)GetProcAddress(hmod, "_set_new_handler");
150 151 152

    hcon = LoadLibraryA( CONCRT_NAME(_MSVCP_VER) );
    if (!hcon) FIXME( "%s not loaded\n", CONCRT_NAME(_MSVCP_VER) );
153
#else
154 155 156 157
    if (sizeof(void *) > sizeof(int))  /* 64-bit has different names */
    {
        MSVCRT_operator_new = (void*)GetProcAddress(hmod, "??2@YAPEAX_K@Z");
        MSVCRT_operator_delete = (void*)GetProcAddress(hmod, "??3@YAXPEAX@Z");
158
        MSVCRT_set_new_handler = (void*)GetProcAddress(hmod, "?_set_new_handler@@YAP6AH_K@ZP6AH0@Z@Z");
159 160 161 162 163
    }
    else
    {
        MSVCRT_operator_new = (void*)GetProcAddress(hmod, "??2@YAPAXI@Z");
        MSVCRT_operator_delete = (void*)GetProcAddress(hmod, "??3@YAXPAX@Z");
164
        MSVCRT_set_new_handler = (void*)GetProcAddress(hmod, "?_set_new_handler@@YAP6AHI@ZP6AHI@Z@Z");
165 166
    }
#endif
167 168

#if _MSVCP_VER >= 110
169 170 171 172 173 174 175 176 177 178
    if (sizeof(void *) > sizeof(int))  /* 64-bit has different names */
    {
        critical_section_ctor = (void*)GetProcAddress(hcon, "??0critical_section@Concurrency@@QEAA@XZ");
        critical_section_dtor = (void*)GetProcAddress(hcon, "??1critical_section@Concurrency@@QEAA@XZ");
        critical_section_lock = (void*)GetProcAddress(hcon, "?lock@critical_section@Concurrency@@QEAAXXZ");
        critical_section_unlock = (void*)GetProcAddress(hcon, "?unlock@critical_section@Concurrency@@QEAAXXZ");
        critical_section_trylock = (void*)GetProcAddress(hcon, "?try_lock@critical_section@Concurrency@@QEAA_NXZ");
    }
    else
    {
179
#ifdef __arm__
180 181 182 183 184
        critical_section_ctor = (void*)GetProcAddress(hcon, "??0critical_section@Concurrency@@QAA@XZ");
        critical_section_dtor = (void*)GetProcAddress(hcon, "??1critical_section@Concurrency@@QAA@XZ");
        critical_section_lock = (void*)GetProcAddress(hcon, "?lock@critical_section@Concurrency@@QAAXXZ");
        critical_section_unlock = (void*)GetProcAddress(hcon, "?unlock@critical_section@Concurrency@@QAAXXZ");
        critical_section_trylock = (void*)GetProcAddress(hcon, "?try_lock@critical_section@Concurrency@@QAA_NXZ");
185
#else
186 187 188 189 190
        critical_section_ctor = (void*)GetProcAddress(hcon, "??0critical_section@Concurrency@@QAE@XZ");
        critical_section_dtor = (void*)GetProcAddress(hcon, "??1critical_section@Concurrency@@QAE@XZ");
        critical_section_lock = (void*)GetProcAddress(hcon, "?lock@critical_section@Concurrency@@QAEXXZ");
        critical_section_unlock = (void*)GetProcAddress(hcon, "?unlock@critical_section@Concurrency@@QAEXXZ");
        critical_section_trylock = (void*)GetProcAddress(hcon, "?try_lock@critical_section@Concurrency@@QAE_NXZ");
191
#endif
192
    }
193
#endif /* _MSVCP_VER >= 110 */
194 195 196 197

#if _MSVCP_VER >= 100
    Context_IsCurrentTaskCollectionCanceling = (void*)GetProcAddress(hcon, "?IsCurrentTaskCollectionCanceling@Context@Concurrency@@SA_NXZ");
#endif
198 199
}

Piotr Caban's avatar
Piotr Caban committed
200 201 202 203 204 205 206
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
    TRACE("(0x%p, %d, %p)\n", hinstDLL, fdwReason, lpvReserved);

    switch (fdwReason)
    {
        case DLL_PROCESS_ATTACH:
207
            init_cxx_funcs();
208
            init_lockit();
209 210 211
            init_exception(hinstDLL);
            init_locale(hinstDLL);
            init_io(hinstDLL);
212
#if _MSVCP_VER >= 100
213
            init_misc(hinstDLL);
214
#endif
Piotr Caban's avatar
Piotr Caban committed
215 216
            break;
        case DLL_PROCESS_DETACH:
217
            if (lpvReserved) break;
218
            free_io();
219
            free_locale();
220
            free_lockit();
221
#if _MSVCP_VER >= 100
222
            free_misc();
223
#endif
Piotr Caban's avatar
Piotr Caban committed
224 225 226 227 228
            break;
    }

    return TRUE;
}
229 230 231

/* ?_BADOFF@std@@3JB -> long const std::_BADOFF */
/* ?_BADOFF@std@@3_JB -> __int64 const std::_BADOFF */
232
const streamoff std_BADOFF = -1;
233 234 235

/* ?_BADOFF_func@std@@YAABJXZ -> long const & __cdecl std::_BADOFF_func(void) */
/* ?_BADOFF_func@std@@YAAEB_JXZ -> __int64 const & __ptr64 __cdecl std::_BADOFF_func(void) */
236
const streamoff * __cdecl std_BADOFF_func(void)
237 238 239 240 241 242 243 244 245 246 247 248 249
{
    return &std_BADOFF;
}

/* ?_Fpz@std@@3_JA  __int64 std::_Fpz */
__int64 std_Fpz = 0;

/* ?_Fpz_func@std@@YAAA_JXZ -> __int64 & __cdecl std::_Fpz_func(void) */
/* ?_Fpz_func@std@@YAAEA_JXZ -> __int64 & __ptr64 __cdecl std::_Fpz_func(void) */
__int64 * __cdecl std_Fpz_func(void)
{
    return &std_Fpz;
}
250 251 252 253 254

#if defined(__MINGW32__) && _MSVCP_VER >= 80 && _MSVCP_VER <= 90
/* Hack: prevent Mingw from importing mingw_helpers.o which conflicts with encode/decode_pointer */
int mingw_app_type = 0;
#endif