xref: /openbmc/qemu/contrib/plugins/win32_linker.c (revision 330fe3b0)
1 /*
2  * Copyright (C) 2023, Greg Manning <gmanning@rapitasystems.com>
3  *
4  * This hook, __pfnDliFailureHook2, is documented in the microsoft documentation here:
5  * https://learn.microsoft.com/en-us/cpp/build/reference/error-handling-and-notification
6  * It gets called when a delay-loaded DLL encounters various errors.
7  * We handle the specific case of a DLL looking for a "qemu.exe",
8  * and give it the running executable (regardless of what it is named).
9  *
10  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
11  * See the COPYING.LIB file in the top-level directory.
12  */
13 
14 #include <windows.h>
15 #include <delayimp.h>
16 
17 FARPROC WINAPI dll_failure_hook(unsigned dliNotify, PDelayLoadInfo pdli);
18 
19 
20 PfnDliHook __pfnDliFailureHook2 = dll_failure_hook;
21 
dll_failure_hook(unsigned dliNotify,PDelayLoadInfo pdli)22 FARPROC WINAPI dll_failure_hook(unsigned dliNotify, PDelayLoadInfo pdli) {
23     if (dliNotify == dliFailLoadLib) {
24         /* If the failing request was for qemu.exe, ... */
25         if (strcmp(pdli->szDll, "qemu.exe") == 0) {
26             /* Then pass back a pointer to the top level module. */
27             HMODULE top = GetModuleHandle(NULL);
28             return (FARPROC) top;
29         }
30     }
31     /* Otherwise we can't do anything special. */
32     return 0;
33 }
34 
35