예제 #1
0
// static
void Disassembler::StaticInitialize()
{
    LIMITED_METHOD_CONTRACT;

#if USE_COREDISTOOLS_DISASSEMBLER
    _ASSERTE(!IsAvailable());

    // TODO: The 'coredistools' library will eventually be part of a NuGet package, need to be able to load
    // that using appropriate search paths
    LPCWSTR libraryName = MAKEDLLNAME(W("coredistools"));
    HMODULE libraryHandle = CLRLoadLibrary(libraryName);
    do
    {
        if (libraryHandle == nullptr)
        {
        #ifdef _DEBUG
            wprintf(W("LoadLibrary failed for '%s': error %u\n"), libraryName, GetLastError());
        #endif // _DEBUG
            break;
        }

        External_InitDisasm =
            reinterpret_cast<decltype(External_InitDisasm)>(GetProcAddress(libraryHandle, "InitDisasm"));
        if (External_InitDisasm == nullptr)
        {
        #ifdef _DEBUG
            wprintf(
                W("GetProcAddress failed for library '%s', function 'InitDisasm': error %u\n"),
                libraryName,
                GetLastError());
        #endif // _DEBUG
            break;
        }

        External_DisasmInstruction =
            reinterpret_cast<decltype(External_DisasmInstruction)>(GetProcAddress(libraryHandle, "DisasmInstruction"));
        if (External_DisasmInstruction == nullptr)
        {
        #ifdef _DEBUG
            wprintf(
                W("GetProcAddress failed for library '%s', function 'DisasmInstruction': error %u\n"),
                libraryName,
                GetLastError());
        #endif // _DEBUG
            break;
        }

        External_FinishDisasm =
            reinterpret_cast<decltype(External_FinishDisasm)>(GetProcAddress(libraryHandle, "FinishDisasm"));
        if (External_FinishDisasm == nullptr)
        {
        #ifdef _DEBUG
            wprintf(
                W("GetProcAddress failed for library '%s', function 'FinishDisasm': error %u\n"),
                libraryName,
                GetLastError());
        #endif // _DEBUG
            break;
        }

        // Set this last to indicate successful load of the library and all exports
        s_libraryHandle = libraryHandle;
        _ASSERTE(IsAvailable());
        return;
    } while (false);

    CLRFreeLibrary(libraryHandle);
    _ASSERTE(!IsAvailable());
#endif // USE_COREDISTOOLS_DISASSEMBLER
}
예제 #2
0
파일: helpers.cpp 프로젝트: ArildF/masters
HRESULT InitializeEEShim()
{
    HRESULT                              hr = S_OK;
    HMODULE                              hMod;
    HMODULE                              hSnMod;
    CCriticalSection                     cs(&g_csInitClb);

    if (!g_hMSCorEE) {
        hr = cs.Lock();
        if (FAILED(hr)) {
            goto Exit;
        }

        if (!g_hMSCorEE) {
            hMod = LoadLibrary(MSCOREE_SHIM_W);
            if (!hMod) {
                hr = HRESULT_FROM_WIN32(GetLastError());
                cs.Unlock();
                goto Exit;
            }

#if PLATFORM_UNIX
            hSnMod = LoadLibrary(MAKEDLLNAME(L"mscorsn"));
            if (!hSnMod) {
                hr = HRESULT_FROM_WIN32(GetLastError());
                cs.Unlock();
                goto Exit;
            }
#else   // PLATFORM_UNIX
            hSnMod = hMod;
#endif  // PLATFORM_UNIX

            g_pfnGetCorSystemDirectory = (PFNGETCORSYSTEMDIRECTORY)GetProcAddress(hMod, "GetCORSystemDirectory");
            g_pfnGetCORVersion = (pfnGetCORVersion)GetProcAddress(hMod, "GetCORVersion");
            g_pfnStrongNameTokenFromPublicKey = (PFNSTRONGNAMETOKENFROMPUBLICKEY)GetProcAddress(hSnMod, "StrongNameTokenFromPublicKey");
            g_pfnStrongNameErrorInfo = (PFNSTRONGNAMEERRORINFO)GetProcAddress(hSnMod, "StrongNameErrorInfo");
            g_pfnStrongNameFreeBuffer = (PFNSTRONGNAMEFREEBUFFER)GetProcAddress(hSnMod, "StrongNameFreeBuffer");
            g_pfnStrongNameSignatureVerification = (PFNSTRONGNAMESIGNATUREVERIFICATION)GetProcAddress(hSnMod, "StrongNameSignatureVerification");
            g_pfnGetAssemblyMDImport = (pfnGetAssemblyMDImport)GetProcAddress(hMod, "GetAssemblyMDImport");
            g_pfnCoInitializeCor = (COINITIALIZECOR) GetProcAddress(hMod, "CoInitializeCor");
            g_pfnGetXMLObject = (pfnGetXMLObject)GetProcAddress(hMod, "GetXMLObject");

            if (!g_pfnGetCorSystemDirectory || !g_pfnGetCORVersion || !g_pfnStrongNameTokenFromPublicKey ||
                !g_pfnStrongNameErrorInfo || !g_pfnStrongNameFreeBuffer || !g_pfnStrongNameSignatureVerification ||
                !g_pfnGetAssemblyMDImport || !g_pfnCoInitializeCor || !g_pfnGetXMLObject) {

                hr = HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND);
                cs.Unlock();
                goto Exit;
            }

            hr = (*g_pfnCoInitializeCor)(COINITCOR_DEFAULT);
            if (FAILED(hr)) {
                cs.Unlock();
                goto Exit;
            }

            // Interlocked exchange guarantees memory barrier
            
            InterlockedExchangePointer((void **)&g_hMSCorEE, hMod);
        }

        cs.Unlock();
    }

Exit:
    return hr;
}
예제 #3
0
void Disassembler::StaticInitialize()
{
    LIMITED_METHOD_CONTRACT;

#if USE_COREDISTOOLS_DISASSEMBLER
    _ASSERTE(!IsAvailable());

    HMODULE libraryHandle = nullptr;
    PathString libPath;
    DWORD result = WszGetModuleFileName(nullptr, libPath);
    if (result == 0) {
#ifdef _DEBUG
        wprintf(
            W("GetModuleFileName failed, function 'DisasmInstruction': error %u\n"),
            GetLastError());
#endif // _DEBUG
        return;
    }

#if defined(FEATURE_PAL)
    WCHAR delim = W('/');
#else
    WCHAR delim = W('\\');
#endif
    LPCWSTR libFileName = MAKEDLLNAME(W("coredistools"));
    PathString::Iterator iter = libPath.End();
    if (libPath.FindBack(iter, delim)) {
        libPath.Truncate(++iter);
        libPath.Append(libFileName);
    }
    else {
        _ASSERTE(!"unreachable");
    }

    LPCWSTR libraryName = libPath.GetUnicode();
    libraryHandle = CLRLoadLibrary(libraryName);
    do
    {
        if (libraryHandle == nullptr)
        {
        #ifdef _DEBUG
            wprintf(W("LoadLibrary failed for '%s': error %u\n"), libraryName, GetLastError());
        #endif // _DEBUG
            break;
        }

        External_InitDisasm =
            reinterpret_cast<decltype(External_InitDisasm)>(GetProcAddress(libraryHandle, "InitDisasm"));
        if (External_InitDisasm == nullptr)
        {
        #ifdef _DEBUG
            wprintf(
                W("GetProcAddress failed for library '%s', function 'InitDisasm': error %u\n"),
                libraryName,
                GetLastError());
        #endif // _DEBUG
            break;
        }

        External_DisasmInstruction =
            reinterpret_cast<decltype(External_DisasmInstruction)>(GetProcAddress(libraryHandle, "DisasmInstruction"));
        if (External_DisasmInstruction == nullptr)
        {
        #ifdef _DEBUG
            wprintf(
                W("GetProcAddress failed for library '%s', function 'DisasmInstruction': error %u\n"),
                libraryName,
                GetLastError());
        #endif // _DEBUG
            break;
        }

        External_FinishDisasm =
            reinterpret_cast<decltype(External_FinishDisasm)>(GetProcAddress(libraryHandle, "FinishDisasm"));
        if (External_FinishDisasm == nullptr)
        {
        #ifdef _DEBUG
            wprintf(
                W("GetProcAddress failed for library '%s', function 'FinishDisasm': error %u\n"),
                libraryName,
                GetLastError());
        #endif // _DEBUG
            break;
        }

        // Set this last to indicate successful load of the library and all exports
        s_libraryHandle = libraryHandle;
        _ASSERTE(IsAvailable());
        return;
    } while (false);

    _ASSERTE(!IsAvailable());
    
#endif // USE_COREDISTOOLS_DISASSEMBLER
}