[Libguestfs] [PATCH nbdkit 1/9] server: Add libnbdkit.so.

Richard W.M. Jones rjones at redhat.com
Wed Aug 19 10:42:35 UTC 2020


I believe after a lot of reading around this topic that it should be
possible to call from the plugin DLLs into functions in the EXE.

One technique is outlined here:

  https://stackoverflow.com/a/18147774

The idea above is to use DLLTOOL to generate an "import library" (a
static linked library containing stubs which are used to resolve at
runtime a list of symbols).  Import libraries are a normal part of
building DLLs on Windows where you always end up building the .DLL and
another .LIB containing these stubs.  The unusual thing here is only
that we generate the import library against an executable rather than
a DLL.

This would be the simplest technique to use, but I could not make it
work in the end.  Generating the import library is fine.  The problem
is actually that libtool doesn't believe it should be possible to add
a static .a file when building a DLL and I couldn't work around that.

In any case if we did the above we'd still have to hack every plugin
and filter so that it links to the import library.  But there's a
lower-level technique we could use instead:

https://nachtimwald.com/2012/07/15/calling-functions-in-exe-from-plugins-in-windows/
https://stackoverflow.com/questions/770344/visual-c-linking-plugin-dll-against-exe#comment1772764_770374

This is to use GetProcAddress from the plugin (DLL) to get the address
of symbols in nbdkit.exe.  It works a bit like dlsym.  I hacked
together a test plugin to try this:

static void
load (void)
{
  int (*_nbdkit_parse_int) (const char *what,
                           const char *str,
                           int *r) =
    GetProcAddress (GetModuleHandle (NULL), "nbdkit_parse_int");
  fprintf (stderr, "nbdkit_parse_int addr = %p\n", _nbdkit_parse_int);

  void (*_nbdkit_debug) (const char *msg, ...) =
    GetProcAddress (GetModuleHandle (NULL), "nbdkit_debug");
  _nbdkit_debug ("calling nbdkit_debug now ...");
}

and on loading this plugin into server/nbdkit.exe:

nbdkit_parse_int addr = 000000000040BD40
nbdkit: debug: calling nbdkit_debug now ...

So it really does work.

I'm not quite sure exactly how to integrate this.  Perhaps adding some
Windows-only code to plugin_init which does the appropriate
GetProcAddress calls, initializing some function pointers in the
plugin?

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
virt-builder quickly builds VMs from scratch
http://libguestfs.org/virt-builder.1.html




More information about the Libguestfs mailing list