gmodule implementation for Darwin/Mac OS X
authorDan Winship <danw@src.gnome.org>
Fri, 20 Apr 2001 17:05:38 +0000 (17:05 +0000)
committerDan Winship <danw@src.gnome.org>
Fri, 20 Apr 2001 17:05:38 +0000 (17:05 +0000)
        * gmodule-dyld.c: gmodule implementation for Darwin/Mac OS X

        * gmodule.c:
        * gmoduleconf.h.in: Add gmodule-dyld support

        * testgmodule.c (main): Fix spelling

gmodule/ChangeLog
gmodule/gmodule-dyld.c [new file with mode: 0644]
gmodule/gmodule.c
gmodule/gmoduleconf.h.in
gmodule/testgmodule.c

index c7d2448..3a37a82 100644 (file)
@@ -1,3 +1,12 @@
+2001-04-20  Dan Winship  <danw@ximian.com>
+
+       * gmodule-dyld.c: gmodule implementation for Darwin/Mac OS X
+
+       * gmodule.c: 
+       * gmoduleconf.h.in: Add gmodule-dyld support
+
+       * testgmodule.c (main): Fix spelling
+
 2001-03-13  Tor Lillqvist  <tml@iki.fi>
 
        From Edward M. Lee  <tailbert@yahoo.com>:
diff --git a/gmodule/gmodule-dyld.c b/gmodule/gmodule-dyld.c
new file mode 100644 (file)
index 0000000..fed8811
--- /dev/null
@@ -0,0 +1,150 @@
+/* GMODULE - GLIB wrapper code for dynamic module loading
+ * Copyright (C) 1998, 2000 Tim Janik
+ *
+ * dyld (Darwin) GMODULE implementation
+ * Copyright (C) 2001 Dan Winship
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include <mach-o/dyld.h>
+
+static gpointer self_module = GINT_TO_POINTER (1);
+
+static gpointer
+_g_module_open (const gchar *file_name,
+               gboolean     bind_lazy)
+{
+  NSObjectFileImage image;
+  NSObjectFileImageReturnCode ret;
+  NSModule module;
+  unsigned long options;
+  char *msg;
+
+  ret = NSCreateObjectFileImageFromFile (file_name, &image);
+  if (ret != NSObjectFileImageSuccess)
+    {
+      switch (ret)
+       {
+       case NSObjectFileImageInappropriateFile:
+       case NSObjectFileImageFormat:
+         msg = g_strdup_printf ("%s is not a loadable module", file_name);
+         break;
+
+       case NSObjectFileImageArch:
+         msg = g_strdup_printf ("%s is not built for this architecture",
+                                file_name);
+         break;
+
+       case NSObjectFileImageAccess:
+         if (access (file_name, F_OK) == 0)
+           msg = g_strdup_printf ("%s: permission denied", file_name);
+         else
+           msg = g_strdup_printf ("%s: no such file or directory", file_name);
+         break;
+
+       default:
+         msg = g_strdup_printf ("unknown error for %s", file_name);
+         break;
+       }
+
+      g_module_set_error (msg);
+      g_free (msg);
+      return NULL;
+    }
+
+  options = NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
+  if (!bind_lazy)
+    options |= NSLINKMODULE_OPTION_BINDNOW;
+  module = NSLinkModule (image, file_name, options);
+  NSDestroyObjectFileImage (image);
+  if (!module)
+    {
+      NSLinkEditErrors c;
+      int error_number;
+      const char *file, *error;
+
+      NSLinkEditError (&c, &error_number, &file, &error);
+      msg = g_strdup_printf ("could not link %s: %s", file_name, error);
+      g_module_set_error (msg);
+      g_free (msg);
+      return NULL;
+    }
+
+  return module;
+}
+
+static gpointer
+_g_module_self (void)
+{
+  return &self_module;
+}
+
+static void
+_g_module_close (gpointer handle,
+                gboolean is_unref)
+{
+  if (handle == &self_module)
+    return;
+
+  if (!NSUnLinkModule (handle, 0))
+    g_module_set_error ("could not unlink module");
+}
+
+static gpointer
+_g_module_symbol (gpointer     handle,
+                 const gchar *symbol_name)
+{
+  NSSymbol sym;
+  char *msg;
+
+  if (handle == &self_module)
+    {
+      if (NSIsSymbolNameDefined (symbol_name))
+       sym = NSLookupAndBindSymbol (symbol_name);
+      else
+       sym = NULL;
+    }
+  else
+    sym = NSLookupSymbolInModule (handle, symbol_name);
+
+  if (!sym)
+    {
+      msg = g_strdup_printf ("no such symbol %s", symbol_name);
+      g_module_set_error (msg);
+      g_free (msg);
+      return NULL;
+    }
+
+  return NSAddressOfSymbol (sym);
+}
+
+static gchar*
+_g_module_build_path (const gchar *directory,
+                     const gchar *module_name)
+{
+  if (directory && *directory)
+    {
+      if (strncmp (module_name, "lib", 3) == 0)
+       return g_strconcat (directory, "/", module_name, NULL);
+      else
+       return g_strconcat (directory, "/lib", module_name, ".so", NULL);
+    }
+  else if (strncmp (module_name, "lib", 3) == 0)
+    return g_strdup (module_name);
+  else
+    return g_strconcat ("lib", module_name, ".so", NULL);
+}
index b735fea..e006b8b 100644 (file)
@@ -139,6 +139,8 @@ g_module_set_error (const gchar *error)
 #include "gmodule-dld.c"
 #elif  (G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
 #include "gmodule-win32.c"
+#elif  (G_MODULE_IMPL == G_MODULE_IMPL_DYLD)
+#include "gmodule-dyld.c"
 #else
 #undef SUPPORT_OR_RETURN
 #define        SUPPORT_OR_RETURN(rv)   { g_module_set_error ("dynamic modules are " \
index 9aa6500..a8921d8 100644 (file)
@@ -31,6 +31,7 @@ extern "C" {
 #define        G_MODULE_IMPL_WIN32     3
 #define        G_MODULE_IMPL_OS2       4
 #define        G_MODULE_IMPL_BEOS      5
+#define        G_MODULE_IMPL_DYLD      6
 
 #define        G_MODULE_IMPL           @G_MODULE_IMPL@
 #undef G_MODULE_HAVE_DLERROR
index 17257e3..184e272 100644 (file)
@@ -71,14 +71,14 @@ main (int   arg,
   g_print ("check that not yet bound symbols in shared libraries of main module are retrievable:\n");
   string = "g_module_close";
   basename = g_path_get_basename (g_module_name (module_self));
-  g_print ("retrive symbol `%s' from \"%s\":\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\":\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
     {
       g_print ("error: %s\n", g_module_error ());
       return 1;
     }
-  g_print ("retrived symbol `%s' as %p\n", string, f_self);
+  g_print ("retrieved symbol `%s' as %p\n", string, f_self);
   g_print ("load plugin from \"%s\"\n", plugin_a);
   module_a = g_module_open (plugin_a, G_MODULE_BIND_LAZY);
   if (!module_a)
@@ -98,7 +98,7 @@ main (int   arg,
    */
   string = "gplugin_a_func";
   basename = g_path_get_basename (g_module_name (module_a));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
     {
@@ -107,7 +107,7 @@ main (int   arg,
     }
   string = "gplugin_b_func";
   basename = g_path_get_basename (g_module_name (module_b));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
     {
@@ -123,7 +123,7 @@ main (int   arg,
    */
   string = "g_clash_func";
   basename = g_path_get_basename (g_module_name (module_self));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
     {
@@ -131,7 +131,7 @@ main (int   arg,
       return 1;
     }
   basename = g_path_get_basename (g_module_name (module_a));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
     {
@@ -139,7 +139,7 @@ main (int   arg,
       return 1;
     }
   basename = g_path_get_basename (g_module_name (module_b));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
     {
@@ -157,13 +157,13 @@ main (int   arg,
    */
   string = "gplugin_clash_func";
   basename = g_path_get_basename (g_module_name (module_self));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_self, string, (gpointer) &f_self))
     f_self = NULL;
-  g_print ("retrived function `%s' from self: %p\n", string, f_self);
+  g_print ("retrieved function `%s' from self: %p\n", string, f_self);
   basename = g_path_get_basename (g_module_name (module_a));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_a, string, (gpointer) &f_a))
     {
@@ -171,7 +171,7 @@ main (int   arg,
       return 1;
     }
   basename = g_path_get_basename (g_module_name (module_b));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_b, string, (gpointer) &f_b))
     {
@@ -189,7 +189,7 @@ main (int   arg,
    */
   string = "gplugin_a_module_func";
   basename = g_path_get_basename (g_module_name (module_a));
-  g_print ("retrive symbol `%s' from \"%s\"\n", string, basename);
+  g_print ("retrieve symbol `%s' from \"%s\"\n", string, basename);
   g_free (basename);
   if (!g_module_symbol (module_a, string, (gpointer) &gmod_f))
     {