2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include <windows.h>
|
2012-05-22 14:00:48 -07:00
|
|
|
#include <delayimp.h>
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsToolkit.h"
|
2012-07-23 14:11:45 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#if defined(__GNUC__)
|
|
|
|
// If DllMain gets name mangled, it won't be seen.
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2008-12-03 05:41:12 -08:00
|
|
|
BOOL APIENTRY DllMain(
|
|
|
|
HINSTANCE hModule,
|
|
|
|
DWORD reason,
|
|
|
|
LPVOID lpReserved )
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
switch( reason ) {
|
|
|
|
case DLL_PROCESS_ATTACH:
|
2008-12-03 05:41:12 -08:00
|
|
|
nsToolkit::Startup((HINSTANCE)hModule);
|
2007-03-22 10:30:00 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_THREAD_ATTACH:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_THREAD_DETACH:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DLL_PROCESS_DETACH:
|
|
|
|
nsToolkit::Shutdown();
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-05-22 14:00:48 -07:00
|
|
|
#if defined(MOZ_METRO)
|
|
|
|
/*
|
|
|
|
* DelayDllLoadHook - the crt calls here anytime a delay load dll is about to
|
|
|
|
* load. There are a number of events, we listen for dliNotePreLoadLibrary.
|
|
|
|
*
|
|
|
|
* On Win8, we enable Windows Runtime Component Extension support. When enabled
|
|
|
|
* the compiler bakes auto-generated code into our binary, including a c init-
|
|
|
|
* ializer which inits the winrt library through a call into the winrt standard
|
|
|
|
* lib 'vccorlib'. Vccorlib in turn has system dll dependencies which are only
|
|
|
|
* available on Win8 (currently API-MS-WIN-CORE-WINRT and
|
|
|
|
* API-MS-WIN-CORE-WINRT-STRING), which prevent xul.dll from loading on os <=
|
|
|
|
* Win7. To get around this we generate a dummy vccore lib with the three entry
|
|
|
|
* points the initializer needs and load it in place of the real vccorlib. We
|
|
|
|
* also have to add vccorlib and the system dlls to the delay load list.
|
2012-07-23 14:11:45 -07:00
|
|
|
*
|
|
|
|
* In addition to setting up dummyvccorlib, we also have additional checks for
|
|
|
|
* for unexpected behavior, specifically:
|
|
|
|
* - trap attempts to load entry points in dummyvccorlib. This is not expected
|
|
|
|
* to happen in release code. It can happen during development if winrt apis
|
|
|
|
* are accessed when running on the desktop, or when winrt objects are
|
|
|
|
* accidentially placed in the global scope.
|
|
|
|
* - trap and handle calls to vccorlib's __abi_FailFast in a breakpad compatible
|
|
|
|
* way. __abi_FailFast is called by exception handling code generated by the
|
|
|
|
* compiler for delegate events.
|
2012-05-22 14:00:48 -07:00
|
|
|
*/
|
|
|
|
static bool IsWin8OrHigher()
|
|
|
|
{
|
|
|
|
static PRInt32 version = 0;
|
|
|
|
|
|
|
|
if (version) {
|
|
|
|
return (version >= 0x602);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match Win8 or Win8 Server or higher
|
|
|
|
OSVERSIONINFOEX osInfo;
|
|
|
|
osInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
|
|
|
::GetVersionEx((OSVERSIONINFO*)&osInfo);
|
|
|
|
version =
|
|
|
|
(osInfo.dwMajorVersion & 0xff) << 8 | (osInfo.dwMinorVersion & 0xff);
|
|
|
|
return (version >= 0x602);
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* kvccorlib = "vccorlib";
|
|
|
|
const char* kwinrtprelim = "api-ms-win-core-winrt";
|
2012-07-23 14:11:45 -07:00
|
|
|
const char* kfailfast = "?__abi_FailFast";
|
2012-05-22 14:00:48 -07:00
|
|
|
|
2012-07-23 14:11:45 -07:00
|
|
|
static bool IsWinRTDLLNotPresent(PDelayLoadInfo pdli, const char* aLibToken)
|
2012-05-22 14:00:48 -07:00
|
|
|
{
|
|
|
|
return (!IsWin8OrHigher() && pdli->szDll &&
|
|
|
|
!strnicmp(pdli->szDll, aLibToken, strlen(aLibToken)));
|
|
|
|
}
|
|
|
|
|
2012-07-23 14:11:45 -07:00
|
|
|
static bool IsWinRTDLLPresent(PDelayLoadInfo pdli, const char* aLibToken)
|
|
|
|
{
|
|
|
|
return (IsWin8OrHigher() && pdli->szDll &&
|
|
|
|
!strnicmp(pdli->szDll, aLibToken, strlen(aLibToken)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void __stdcall __abi_MozFailFast()
|
|
|
|
{
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
|
|
|
|
2012-05-22 14:00:48 -07:00
|
|
|
FARPROC WINAPI DelayDllLoadHook(unsigned dliNotify, PDelayLoadInfo pdli)
|
|
|
|
{
|
|
|
|
if (dliNotify == dliNotePreLoadLibrary) {
|
2012-07-23 14:11:45 -07:00
|
|
|
if (IsWinRTDLLNotPresent(pdli, kvccorlib)) {
|
2012-05-22 14:00:48 -07:00
|
|
|
return (FARPROC)LoadLibraryA("dummyvccorlib.dll");
|
|
|
|
}
|
2012-07-23 14:11:45 -07:00
|
|
|
NS_ASSERTION(!IsWinRTDLLNotPresent(pdli, kwinrtprelim),
|
2012-05-22 14:00:48 -07:00
|
|
|
"Attempting to load winrt libs in non-metro environment. "
|
|
|
|
"(Winrt variable type placed in global scope?)");
|
|
|
|
}
|
2012-07-23 14:11:45 -07:00
|
|
|
if (dliNotify == dliFailGetProc && IsWinRTDLLNotPresent(pdli, kvccorlib)) {
|
2012-07-13 09:10:49 -07:00
|
|
|
NS_WARNING("Attempting to access winrt vccorlib entry point in non-metro environment.");
|
|
|
|
NS_WARNING(pdli->szDll);
|
|
|
|
NS_WARNING(pdli->dlp.szProcName);
|
|
|
|
NS_ABORT();
|
|
|
|
}
|
2012-07-23 14:11:45 -07:00
|
|
|
if (dliNotify == dliNotePreGetProcAddress &&
|
|
|
|
IsWinRTDLLPresent(pdli, kvccorlib) &&
|
|
|
|
pdli->dlp.szProcName &&
|
|
|
|
!strnicmp(pdli->dlp.szProcName, kfailfast, strlen(kfailfast))) {
|
|
|
|
return (FARPROC)__abi_MozFailFast;
|
|
|
|
}
|
2012-05-22 14:00:48 -07:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ExternC PfnDliHook __pfnDliNotifyHook2 = DelayDllLoadHook;
|
2012-07-13 09:10:49 -07:00
|
|
|
ExternC PfnDliHook __pfnDliFailureHook2 = DelayDllLoadHook;
|
|
|
|
|
2012-05-22 14:00:48 -07:00
|
|
|
#endif // MOZ_METRO
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|