[Libguestfs] [PATCH nbdkit 1/3] include: Function indirection for PE DLL

Richard W.M. Jones rjones at redhat.com
Mon Mar 23 12:28:10 UTC 2020


From: Yifan Gu <gyf304 at gmail.com>

This patch adds in indirection for API functions, as PE DLL loader
does not resolve undefined symbols.

This patch only includes header changes.
---
 include/nbdkit-common.h | 133 +++++++++++++++++++++++++++++++++++++++-
 include/nbdkit-compat.h |  97 +++++++++++++++++++++++++++++
 include/nbdkit-filter.h |  39 ++++++++++++
 include/nbdkit-plugin.h |  27 ++++++++
 include/Makefile.am     |   1 +
 5 files changed, 295 insertions(+), 2 deletions(-)

diff --git a/include/nbdkit-common.h b/include/nbdkit-common.h
index 9d1d89d0..57abc423 100644
--- a/include/nbdkit-common.h
+++ b/include/nbdkit-common.h
@@ -40,10 +40,14 @@
 #include <stdarg.h>
 #include <stdint.h>
 #include <errno.h>
-#include <sys/socket.h>
 
+#include <nbdkit-compat.h>
 #include <nbdkit-version.h>
 
+#if defined(NBDKIT_INTERNAL) || !defined(WINDOWS_COMPAT)
+#include <sys/socket.h>
+#endif
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -76,6 +80,7 @@ extern "C" {
 #define NBDKIT_EXTENT_HOLE    (1<<0) /* Same as NBD_STATE_HOLE */
 #define NBDKIT_EXTENT_ZERO    (1<<1) /* Same as NBD_STATE_ZERO */
 
+#if defined(NBDKIT_INTERNAL) || !defined(WINDOWS_COMPAT)
 extern void nbdkit_error (const char *msg, ...) ATTRIBUTE_FORMAT_PRINTF (1, 2);
 extern void nbdkit_verror (const char *msg, va_list args)
   ATTRIBUTE_FORMAT_PRINTF (1, 0);
@@ -113,9 +118,133 @@ extern const char *nbdkit_export_name (void);
 extern int nbdkit_peer_name (struct sockaddr *addr, socklen_t *addrlen);
 extern void nbdkit_shutdown (void);
 
-struct nbdkit_extents;
 extern int nbdkit_add_extent (struct nbdkit_extents *,
                               uint64_t offset, uint64_t length, uint32_t type);
+#else
+static void nbdkit_error (const char *msg, ...)
+  ATTRIBUTE_FORMAT_PRINTF (1, 2);
+static void nbdkit_error (const char *msg, ...)
+{
+  va_list args;
+  va_start(args, msg);
+  _nbdkit_functions.verror(msg, args);
+  va_end(args);
+}
+static void nbdkit_verror (const char *msg, va_list args)
+  ATTRIBUTE_FORMAT_PRINTF (1, 0);
+static void nbdkit_verror (const char *msg, va_list args)
+{
+  _nbdkit_functions.verror (msg, args);
+}
+static void nbdkit_debug (const char *msg, ...)
+  ATTRIBUTE_FORMAT_PRINTF (1, 2);
+static void nbdkit_debug (const char *msg, ...)
+{
+  va_list args;
+  va_start (args, msg);
+  _nbdkit_functions.vdebug (msg, args);
+  va_end (args);
+}
+static void nbdkit_vdebug (const char *msg, va_list args)
+  ATTRIBUTE_FORMAT_PRINTF (1, 0);
+static void nbdkit_vdebug (const char *msg, va_list args)
+{
+  _nbdkit_functions.vdebug (msg, args);
+}
+
+static char *nbdkit_absolute_path (const char *path)
+{
+  return _nbdkit_functions.absolute_path (path);
+}
+static int64_t nbdkit_parse_size (const char *str)
+{
+  return _nbdkit_functions.parse_size (str);
+}
+static int nbdkit_parse_bool (const char *str)
+{
+  return _nbdkit_functions.parse_bool (str);
+}
+static int nbdkit_parse_int (const char *what, const char *str,
+                            int *r)
+{
+  return _nbdkit_functions.parse_int (what, str, r);
+}
+static int nbdkit_parse_unsigned (const char *what, const char *str,
+                                  unsigned *r)
+{
+  return _nbdkit_functions.parse_unsigned (what, str, r);
+}
+static int nbdkit_parse_int8_t (const char *what, const char *str,
+                                int8_t *r)
+{
+  return _nbdkit_functions.parse_int8_t (what, str, r);
+}
+static int nbdkit_parse_uint8_t (const char *what, const char *str,
+                                uint8_t *r)
+{
+  return _nbdkit_functions.parse_uint8_t (what, str, r);
+}
+static int nbdkit_parse_int16_t (const char *what, const char *str,
+                                int16_t *r)
+{
+  return _nbdkit_functions.parse_int16_t (what, str, r);
+}
+static int nbdkit_parse_uint16_t (const char *what, const char *str,
+                                  uint16_t *r)
+{
+  return _nbdkit_functions.parse_uint16_t (what, str, r);
+}
+static int nbdkit_parse_int32_t (const char *what, const char *str,
+                                int32_t *r)
+{
+  return _nbdkit_functions.parse_int32_t (what, str, r);
+}
+static int nbdkit_parse_uint32_t (const char *what, const char *str,
+                                  uint32_t *r)
+{
+  return _nbdkit_functions.parse_uint32_t (what, str, r);
+}
+static int nbdkit_parse_int64_t (const char *what, const char *str,
+                                int64_t *r)
+{
+  return _nbdkit_functions.parse_int64_t (what, str, r);
+}
+static int nbdkit_parse_uint64_t (const char *what, const char *str,
+                                  uint64_t *r)
+{
+  return _nbdkit_functions.parse_uint64_t(what, str, r);
+}
+static int nbdkit_read_password (const char *value, char **password)
+{
+  return _nbdkit_functions.read_password(value, password);
+}
+static char *nbdkit_realpath (const char *path)
+{
+  return _nbdkit_functions.realpath(path);
+}
+static int nbdkit_nanosleep (unsigned sec, unsigned nsec)
+{
+  return _nbdkit_functions.nanosleep(sec, nsec);
+}
+static const char *nbdkit_export_name (void)
+{
+  return _nbdkit_functions.export_name();
+}
+static int nbdkit_peer_name (void *addr, void *addrlen)
+{
+  return _nbdkit_functions.peer_name(addr, addrlen);
+}
+static void nbdkit_shutdown (void)
+{
+  _nbdkit_functions.shutdown();
+}
+
+static int nbdkit_add_extent (struct nbdkit_extents *extents,
+                              uint64_t offset, uint64_t length, uint32_t type)
+{
+  return _nbdkit_functions.add_extent(extents, offset, length, type);
+}
+#endif
 
 /* A static non-NULL pointer which can be used when you don't need a
  * per-connection handle.
diff --git a/include/nbdkit-compat.h b/include/nbdkit-compat.h
new file mode 100644
index 00000000..a0a4cb69
--- /dev/null
+++ b/include/nbdkit-compat.h
@@ -0,0 +1,97 @@
+/* nbdkit
+ * Copyright (C) 2013-2020 Red Hat Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ *
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * * Neither the name of Red Hat nor the names of its contributors may be
+ * used to endorse or promote products derived from this software without
+ * specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY RED HAT AND CONTRIBUTORS ''AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
+ * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RED HAT OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
+ * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#ifndef NBDKIT_COMPAT_H
+#define NBDKIT_COMPAT_H
+
+#if !defined (NBDKIT_PLUGIN_H) && !defined (NBDKIT_FILTER_H)
+#error this header file should not be directly included
+#endif
+
+#if defined(_WIN32) || defined(__MINGW32__) || defined(__CYGWIN__) || \
+    defined(_MSC_VER)
+#define WINDOWS_COMPAT
+#endif
+
+struct nbdkit_extents;
+
+#if defined(WINDOWS_COMPAT)
+#include <string.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef WINDOWS_COMPAT
+struct nbdkit_functions {
+  void (*verror) (const char *msg, va_list args);
+  void (*vdebug) (const char *msg, va_list args);
+  char * (*absolute_path) (const char *path);
+  int64_t (*parse_size) (const char *str);
+  int (*parse_bool) (const char *str);
+  int (*parse_int) (const char *what, const char *str, int *r);
+  int (*parse_unsigned) (const char *what, const char *str, unsigned *r);
+  int (*parse_int8_t) (const char *what, const char *str, int8_t *r);
+  int (*parse_uint8_t) (const char *what, const char *str, uint8_t *r);
+  int (*parse_int16_t) (const char *what, const char *str, int16_t *r);
+  int (*parse_uint16_t) (const char *what, const char *str, uint16_t *r);
+  int (*parse_int32_t) (const char *what, const char *str, int32_t *r);
+  int (*parse_uint32_t) (const char *what, const char *str, uint32_t *r);
+  int (*parse_int64_t) (const char *what, const char *str, int64_t *r);
+  int (*parse_uint64_t) (const char *what, const char *str, uint64_t *r);
+  int (*read_password) (const char *value, char **password);
+  char * (*realpath) (const char *path);
+  int (*nanosleep) (unsigned sec, unsigned nsec);
+  const char * (*export_name) (void);
+  int (*peer_name) (void *addr, void *addrlen);
+  void (*shutdown) (void);
+  
+  struct nbdkit_extents *(*extents_new) (uint64_t start, uint64_t end);
+  void (*extents_free) (struct nbdkit_extents *);
+  size_t (*extents_count) (const struct nbdkit_extents *);
+  struct nbdkit_extent (*get_extent) (const struct nbdkit_extents *, size_t);
+  int (*add_extent) (struct nbdkit_extents *, uint64_t offset, uint64_t length,
+                    uint32_t type);
+
+  void (*set_error) (int err);
+};
+
+struct nbdkit_functions _nbdkit_functions;
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif
+#endif
diff --git a/include/nbdkit-filter.h b/include/nbdkit-filter.h
index ca58e496..40ab4345 100644
--- a/include/nbdkit-filter.h
+++ b/include/nbdkit-filter.h
@@ -110,11 +110,31 @@ struct nbdkit_extent {
   uint32_t type;
 };
 
+#if defined(NBDKIT_INTERNAL) || !defined(WINDOWS_COMPAT)
 extern struct nbdkit_extents *nbdkit_extents_new (uint64_t start, uint64_t end);
 extern void nbdkit_extents_free (struct nbdkit_extents *);
 extern size_t nbdkit_extents_count (const struct nbdkit_extents *);
 extern struct nbdkit_extent nbdkit_get_extent (const struct nbdkit_extents *,
                                                size_t);
+#else
+static struct nbdkit_extents *nbdkit_extents_new (uint64_t start, uint64_t end)
+{
+  return _nbdkit_functions.extents_new(start, end);
+}
+static void nbdkit_extents_free (struct nbdkit_extents *extents)
+{
+  _nbdkit_functions.extents_free(extents);
+}
+static size_t nbdkit_extents_count (const struct nbdkit_extents *extents)
+{
+  return _nbdkit_functions.extents_count(extents);
+}
+static struct nbdkit_extent nbdkit_get_extent (const struct nbdkit_extents *extents,
+                                               size_t size)
+{
+  return _nbdkit_functions.get_extent(extents, size);
+}
+#endif
 
 /* Filter struct. */
 struct nbdkit_filter {
@@ -204,6 +224,24 @@ struct nbdkit_filter {
                 int *err);
 };
 
+#if defined(WINDOWS_COMPAT)
+#define NBDKIT_REGISTER_FILTER(filter)                                  \
+  NBDKIT_CXX_LANG_C __declspec(dllexport)                               \
+  struct nbdkit_filter *                                                \
+  filter_init (void)                                                    \
+  {                                                                     \
+    (filter)._api_version = NBDKIT_FILTER_API_VERSION;                  \
+    (filter)._version = NBDKIT_VERSION_STRING;                          \
+    return &(filter);                                                   \
+  }                                                                     \
+  NBDKIT_CXX_LANG_C __declspec(dllexport)                               \
+  void                                                                  \
+  functions_init (struct nbdkit_functions *functions)                   \
+  {                                                                     \
+    memcpy(&_nbdkit_functions, functions,                               \
+           sizeof(struct nbdkit_functions));                            \
+  }
+#else
 #define NBDKIT_REGISTER_FILTER(filter)                                  \
   NBDKIT_CXX_LANG_C                                                     \
   struct nbdkit_filter *                                                \
@@ -213,6 +251,7 @@ struct nbdkit_filter {
     (filter)._version = NBDKIT_VERSION_STRING;                          \
     return &(filter);                                                   \
   }
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/include/nbdkit-plugin.h b/include/nbdkit-plugin.h
index 7ff7bcee..6b6d99d3 100644
--- a/include/nbdkit-plugin.h
+++ b/include/nbdkit-plugin.h
@@ -140,8 +140,34 @@ struct nbdkit_plugin {
   int (*get_ready) (void);
 };
 
+#if defined(NBDKIT_INTERNAL) || !defined(WINDOWS_COMPAT)
 extern void nbdkit_set_error (int err);
+#else
+static void nbdkit_set_error (int err)
+{
+  _nbdkit_functions.set_error (err);
+}
+#endif
 
+#if defined(WINDOWS_COMPAT)
+#define NBDKIT_REGISTER_PLUGIN(plugin)                                  \
+  NBDKIT_CXX_LANG_C __declspec(dllexport)                               \
+  struct nbdkit_plugin *                                                \
+  plugin_init (void)                                                    \
+  {                                                                     \
+    (plugin)._struct_size = sizeof (plugin);                            \
+    (plugin)._api_version = NBDKIT_API_VERSION;                         \
+    (plugin)._thread_model = THREAD_MODEL;                              \
+    return &(plugin);                                                   \
+  }                                                                     \
+  NBDKIT_CXX_LANG_C __declspec(dllexport)                               \
+  void                                                                  \
+  functions_init (struct nbdkit_functions *functions)                   \
+  {                                                                     \
+    memcpy(&_nbdkit_functions, functions,                               \
+           sizeof(struct nbdkit_functions));                            \
+  }
+#else
 #define NBDKIT_REGISTER_PLUGIN(plugin)                                  \
   NBDKIT_CXX_LANG_C                                                     \
   struct nbdkit_plugin *                                                \
@@ -152,6 +178,7 @@ extern void nbdkit_set_error (int err);
     (plugin)._thread_model = THREAD_MODEL;                              \
     return &(plugin);                                                   \
   }
+#endif
 
 #ifdef __cplusplus
 }
diff --git a/include/Makefile.am b/include/Makefile.am
index cf46abc6..8b413570 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -32,6 +32,7 @@
 include $(top_srcdir)/common-rules.mk
 
 include_HEADERS = \
+	nbdkit-compat.h \
 	nbdkit-common.h \
 	nbdkit-plugin.h \
 	nbdkit-filter.h \
-- 
2.25.0




More information about the Libguestfs mailing list