Enable plugin support for windows
cover Enable plugin support for windows
This patch enables plugin support on windows.
qemu plugins on windows now correctly load/link the qemu_plugin_* symbols when they are loaded[1]. This works by creating a delay-loaded .lib file with dlltool[2], then linking the plugins against that to get them compiled. No messing around with function pointers required.
The .lib file is created with "qemu.exe" as the name of the DLL. The only input to the creation of the .lib file is a list of qemu_plugin_* api symbol names.
When the DLL is being loaded, it will fail to find qemu.exe[3], but the windows linker has a 'dll loading failed' hook you can put in the plugin.
I've implemented this hook to check if the file that's failed to load is 'qemu.exe', and if so, substitute in a handle to the top-level executable. Symbol linking then continues in the normal way.
As such, windows plugins work for me, and with minimal change to actual qemu code.
[1]: Except lockstep, which uses unix sockets. It could be changed to
use another communication mechanism, but that felt outside what I'm trying to achieve here.
[2]: This would be the first use of dlltool in qemu. Is that OK? do we
need anything in meson to check it exists?
[3]: If qemu moved to a single executable model, and we got the name
right when creating the .lib, then the dll hook would not be needed at all.
Limitations/things I'm worried about
* There is some amount of meson/Makefile tweaking going on here, and while what I've got works for me, I'm not very familiar with meson, and might not be doing things The Right Way.
* plugin/FFI bindings authors would have to copy this hook themselves, or implement something equivalent. Also they need the qemu_plugin_api.lib file, which I think I've added to the set of installed files.
* License for win32_linker.c - I'm happy to put this under whatever license seems most convenient. Given qemu-plugin.h is already GPL, all plugins will be GPL anyway.
Greg Manning (1):
plugins: enable plugins for windows
configure | 9 ++---- contrib/plugins/Makefile | 27 ++++++++++++++++-- contrib/plugins/win32_linker.c | 34 +++++++++++++++++++++++ include/qemu/qemu-plugin.h | 50 ++++++++++++++++++++++++++++++++-- meson.build | 5 ++++ plugins/meson.build | 17 ++++++++++++ tests/plugin/meson.build | 14 ++++++++-- 7 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 contrib/plugins/win32_linker.c
--
2.42.0
--
Follow Rapita Systems on LinkedIn<https://www.linkedin.com/company/rapita-systems?utm_source=rs_email_sig>
PATCH 1/1 plugins: enable plugins for windows
Enable plugins for windows, including updating the example plugins to compile and work on windows (except lockstep.c, which uses unix sockets)
In qemu-plugin.h, mark all API functions as __declspec(dllexport) when compiling the executables, and as __declspec(dllimport) when being used to compile plugins against.
Generate a qemu_plugin_api.lib delay import lib on windows, for windows qemu plugins to link against.
Implement an example dll load fail hook to link up the API functions correctly when a plugin is loaded on windows.
Build system changes to get the plugins in contrib/plugins and test/plugin to compile correctly on windows
--- configure | 9 ++---- contrib/plugins/Makefile | 27 ++++++++++++++++-- contrib/plugins/win32_linker.c | 34 +++++++++++++++++++++++ include/qemu/qemu-plugin.h | 50 ++++++++++++++++++++++++++++++++-- meson.build | 5 ++++ plugins/meson.build | 17 ++++++++++++ tests/plugin/meson.build | 14 ++++++++-- 7 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 contrib/plugins/win32_linker.c
configure +3 -6
@@ -1010,12 +1010,6 @@ if test "$targetos" = "bogus"; then fi # test for any invalid configuration combinations -if test "$targetos" = "windows"; then - if test "$plugins" = "yes"; then - error_exit "TCG plugins not currently supported on Windows platforms" - fi - plugins="no" -fi if test "$tcg" = "disabled" ; then if test "$plugins" = "yes"; then error_exit "Can't enable plugins on non-TCG builds"
@@ -1662,6 +1656,9 @@ echo "CFLAGS=${CFLAGS-$default_cflags} $EXTRA_CFLAGS" >> contrib/plugins/$config if test "$targetos" = darwin; then echo "CONFIG_DARWIN=y" >> contrib/plugins/$config_host_mak fi +if test "$targetos" = windows; then + echo "CONFIG_WIN32=y" >> contrib/plugins/$config_host_mak +fi # tests/tcg configuration (config_host_mak=tests/tcg/config-host.mak
contrib/plugins/Makefile +24 -3
@@ -17,12 +17,25 @@ NAMES += execlog NAMES += hotblocks NAMES += hotpages NAMES += howvec + +# The lockstep example communicates using unix sockets, +# and can't be easily made to work on windows. +ifneq ($(CONFIG_WIN32),y) NAMES += lockstep +endif + NAMES += hwprofile NAMES += cache NAMES += drcov -SONAMES := $(addsuffix .so,$(addprefix lib,$(NAMES))) +ifeq ($(CONFIG_WIN32),y) +SO_SUFFIX := .dll +LDLIBS += $(shell $(PKG_CONFIG) --libs glib-2.0) +else +SO_SUFFIX := .so +endif + +SONAMES := $(addsuffix $(SO_SUFFIX),$(addprefix lib,$(NAMES))) # The main QEMU uses Glib extensively so it's perfectly fine to use it # in plugins (which many example do).
@@ -35,15 +48,23 @@ all: $(SONAMES) %.o: %.c $(CC) $(CFLAGS) $(PLUGIN_CFLAGS) -c -o $@ $< -lib%.so: %.o +ifeq ($(CONFIG_WIN32),y) +lib%$(SO_SUFFIX): %.o win32_linker.o ../../plugins/qemu_plugin_api.lib + $(CC) -shared -o $@ $^ $(LDLIBS) +else + +lib%$(SO_SUFFIX): %.o ifeq ($(CONFIG_DARWIN),y) $(CC) -bundle -Wl,-undefined,dynamic_lookup -o $@ $^ $(LDLIBS) else $(CC) -shared -o $@ $^ $(LDLIBS) endif +endif + + clean: - rm -f *.o *.so *.d + rm -f *.o *$(SO_SUFFIX) *.d rm -Rf .libs .PHONY: all clean
contrib/plugins/win32_linker.c new file +34 -0
@@ -0,0 +1,34 @@ +/* +* Copyright (C) 2023, Greg Manning <[email protected]> +* +* This hook, __pfnDliFailureHook2, is documented in the microsoft documentation here: +* https://learn.microsoft.com/en-us/cpp/build/reference/error-handling-and-notification +* It gets called when a delay-loaded DLL encounters various errors. +* We handle the specific case of a DLL looking for a "qemu.exe", +* and give it the running executable (regardless of what it is named). +* +* This work is licensed under the terms of the GNU LGPL, version 2 or later. +* See the COPYING.LIB file in the top-level directory. +*/ + +#include <Windows.h> +#include <delayimp.h> + +FARPROC WINAPI dll_failure_hook(unsigned dliNotify, PDelayLoadInfo pdli); + + +PfnDliHook __pfnDliFailureHook2 = dll_failure_hook; + +FARPROC WINAPI dll_failure_hook(unsigned dliNotify, PDelayLoadInfo pdli) { +if (dliNotify == dliFailLoadLib) { +/* If the failing request was for qemu.exe, ... */ +if (strcmp (pdli->szDll,"qemu.exe" ) ==0 ) { +/* Then pass back a pointer to the top level module. */ +HMODULE top =GetModuleHandle (NULL ); +return (FARPROC ) top; + } + } +/* Otherwise we can't do anything special. */ +return 0 ; +} +
include/qemu/qemu-plugin.h +47 -3
305 lines not on this page · raw message
meson.build +5 -0
@@ -3904,6 +3904,11 @@ endforeach if get_option('plugins') install_headers('include/qemu/qemu-plugin.h') + if targetos == 'windows' + # On windows, we want to deliver the qemu_plugin_api.lib file in the qemu installer, + # so that plugin authors can compile against it. + install_data(win32_qemu_plugin_api_lib, install_dir: 'lib') + endif endif subdir('qga')
plugins/meson.build +17 -0
@@ -14,6 +14,23 @@ if not enable_modules endif if get_option('plugins') + if targetos == 'windows' + # Generate a .lib file for plugins to link against. + # First, create a .def file listing all the symbols a plugin should expect to have + # available in qemu + win32_plugin_def = configure_file( + input: files('qemu-plugins.symbols'), + output: 'qemu_plugin_api.def', + capture: true, + command: ['sed', '-e', '0,/^/s//EXPORTS/; s/[{};]//g', '@INPUT@']) + # then use dlltool to assemble a delaylib. + win32_qemu_plugin_api_lib = configure_file( + input: win32_plugin_def, + output: 'qemu_plugin_api.lib', + command: ['dlltool', '--input-def', '@INPUT@', + '--output-delaylib', '@OUTPUT@', '--dllname', 'qemu.exe'] + ) + endif specific_ss.add(files( 'loader.c', 'core.c',
tests/plugin/meson.build +11 -3
@@ -1,9 +1,17 @@ t = [] if get_option('plugins') foreach i : ['bb', 'empty', 'insn', 'mem', 'syscall'] - t += shared_module(i, files(i + '.c'), - include_directories: '../../include/qemu', - dependencies: glib) + if targetos == 'windows' + t += shared_module(i, files(i + '.c') + '../../contrib/plugins/win32_linker.c', + include_directories: '../../include/qemu', + objects: [win32_qemu_plugin_api_lib], + dependencies: glib) + + else + t += shared_module(i, files(i + '.c'), + include_directories: '../../include/qemu', + dependencies: glib) + endif endforeach endif if t.length() > 0
--
2.42.0
--
Follow Rapita Systems on LinkedIn<https://www.linkedin.com/company/rapita-systems?utm_source=rs_email_sig>
(Add Paolo to cc for meson questions)
Quoted 15 lines · Greg Manning
This patch enables plugin support on windows.
qemu plugins on windows now correctly load/link the qemu_plugin_* symbols when they are loaded[1]. This works by creating a delay-loaded .lib file with dlltool[2], then linking the plugins against that to get them compiled. No messing around with function pointers required.
The .lib file is created with "qemu.exe" as the name of the DLL. The only input to the creation of the .lib file is a list of qemu_plugin_* api symbol names.
When the DLL is being loaded, it will fail to find qemu.exe[3], but the windows linker has a 'dll loading failed' hook you can put in the plugin.
Quoted 6 lines
I've implemented this hook to check if the file that's failed to load is 'qemu.exe', and if so, substitute in a handle to the top-level executable. Symbol linking then continues in the normal way.
As such, windows plugins work for me, and with minimal change to actual qemu code.
Excellent work - I'm glad you can make it work without having to hack the rest of the plugins too much. I'm afraid I can't do much to test this but overall I'm much happier to merge this.
However can we split the patches into slightly smaller chunks. Maybe something like:
- add the QEMU_PLUGIN_API annotations
- introduce the windows linker bits
- filter the plugins that can't be built for windows
- flip the switch in configure to allow plugins to be built on Windows
Also what are we going to do for testing? Most qemu developers are using POSIX systems so I'm worried about support bit rotting. We do have some windows coverage in CI but currently only running qtest. There are some "make check-tcg" tests which are softmmu but I suspect getting the cross-compilers on windows would be a blocker. Do we know the state of the avocado tests? We have some minimal testing there which could be extended as all you need is avocado working and some binary images to run. I've just posted a patch to update them:
Message-Id: <[email protected]>
Date: Wed, 1 Nov 2023 13:50:03 +0000
Subject: [RFC PATCH] tests/avocado: update the tcg_plugins test
From: =?UTF-8?q?Alex=20Benn=C3=A9e?= <[email protected]>
Quoted 6 lines
[1]: Except lockstep, which uses unix sockets. It could be changed to
use another communication mechanism, but that felt outside what I'm trying to achieve here.[2]: This would be the first use of dlltool in qemu. Is that OK? do we
need anything in meson to check it exists?
Paolo any comments on the meson bits?
Quoted 11 lines
[3]: If qemu moved to a single executable model, and we got the name
right when creating the .lib, then the dll hook would not be needed at all.Limitations/things I'm worried about
* There is some amount of meson/Makefile tweaking going on here, and while what I've got works for me, I'm not very familiar with meson, and might not be doing things The Right Way.
Quoted 8 lines
* plugin/FFI bindings authors would have to copy this hook themselves, or implement something equivalent. Also they need the qemu_plugin_api.lib file, which I think I've added to the set of installed files.
* License for
win32_linker.c- I'm happy to put this under whatever license seems most convenient. Givenqemu-plugin.his already GPL, all plugins will be GPL anyway.
While there may be private downstream plugins being used behind closed doors we very much don't want to encourage binary only plugins so GPL is fine.
Quoted 15 lines
Greg Manning (1):
plugins: enable plugins for windowsconfigure | 9 ++---- contrib/plugins/Makefile | 27 ++++++++++++++++-- contrib/plugins/win32_linker.c | 34 +++++++++++++++++++++++ include/qemu/qemu-plugin.h | 50 ++++++++++++++++++++++++++++++++-- meson.build | 5 ++++ plugins/meson.build | 17 ++++++++++++ tests/plugin/meson.build | 14 ++++++++-- 7 files changed, 141 insertions(+), 15 deletions(-) create mode 100644 contrib/plugins/win32_linker.c--
2.42.0
Signature · 2 lines
Alex Bennée Virtualisation Tech Lead @ Linaro