[PATCH] dyld support

Wilfredo Sánchez wsanchez at wsanchez.net
Thu Dec 5 02:10:04 CET 2002


   This patch adds support for the Darwin (Mac OS X) dyld linker.  It 
removes the need
or a dlopen() emulation library.  I added a test for dyld in 
configure.ac, and
supporting code in cipher/idea-stub.c.  This patch favors dyld over 
dlopen() if both
are present, to avoid use of dlcompat if it is available for other 
reasons.

   This patch is based in gnupg-1.2.1.

	-wsv


Index: configure.ac
===================================================================
--- configure.ac	(revision 7)
+++ configure.ac	(working copy)
@@ -495,17 +495,24 @@


  if test "$try_dynload" = yes ; then
-  AC_CHECK_LIB(dl,dlopen)
-  if test "$ac_cv_lib_dl_dlopen" = "yes"; then
-    AC_DEFINE(USE_DYNAMIC_LINKING,1,
-              [define to enable the use of extensions])
-    AC_DEFINE(HAVE_DL_DLOPEN,1,
-              [Defined when the dlopen function family is available])
+  AC_CHECK_HEADERS(mach-o/dyld.h)
+  AC_CHECK_FUNCS(NSLinkModule)
+  if test "$ac_cv_func_NSLinkModule" = "yes"; then
+    AC_DEFINE(USE_DYNAMIC_LINKING)
+    AC_DEFINE(HAVE_DL_DYLD,1,[Defined when the dyld API is available])
    else
-    AC_CHECK_FUNCS(dlopen)
-    if test "$ac_cv_func_dlopen" = "yes"; then
-      AC_DEFINE(USE_DYNAMIC_LINKING)
-      AC_DEFINE(HAVE_DL_DLOPEN)
+    AC_CHECK_LIB(dl,dlopen)
+    if test "$ac_cv_lib_dl_dlopen" = "yes"; then
+      AC_DEFINE(USE_DYNAMIC_LINKING,1,
+                [define to enable the use of extensions])
+      AC_DEFINE(HAVE_DL_DLOPEN,1,
+                [Defined when the dlopen function family is available])
+    else
+      AC_CHECK_FUNCS(dlopen)
+      if test "$ac_cv_func_dlopen" = "yes"; then
+        AC_DEFINE(USE_DYNAMIC_LINKING)
+        AC_DEFINE(HAVE_DL_DLOPEN)
+      fi
      fi
    fi
  else
Index: cipher/idea-stub.c
===================================================================
--- cipher/idea-stub.c	(revision 7)
+++ cipher/idea-stub.c	(working copy)
@@ -38,7 +38,9 @@
  #include <stdlib.h>
  #include <string.h>
  #include <unistd.h>
-#ifdef HAVE_DL_DLOPEN
+#if defined (HAVE_DL_DYLD)
+  #include <mach-o/dyld.h>
+#else if defined (HAVE_DL_DLOPEN)
    #include <dlfcn.h>
  #endif
  #ifdef __MINGW32__
@@ -104,7 +106,7 @@
  #endif /*__MINGW32__*/

  /* We do only support dlopen and the Windows emulation of it. */
-#ifndef HAVE_DL_DLOPEN
+#if ! defined (HAVE_DL_DLOPEN) && ! defined (HAVE_DL_DYLD)
  #undef USE_DYNAMIC_LINKING
  #endif

@@ -113,6 +115,54 @@
  load_module (const char *name)
  {
  #ifdef USE_DYNAMIC_LINKING
+#if defined (HAVE_DL_DYLD)
+  NSObjectFileImage image;
+  NSObjectFileImageReturnCode result;
+  NSModule handle = NULL;
+  NSSymbol symbol;
+  void *sym;
+  const char* err = NULL;
+
+  result = NSCreateObjectFileImageFromFile(name, &image);
+
+  if (result == NSObjectFileImageSuccess) {
+    handle = NSLinkModule(image, name, 
NSLINKMODULE_OPTION_RETURN_ON_ERROR);
+    /* If something went wrong, get the errors... */
+    if (!handle) {
+      NSLinkEditErrors errors;
+      int errornumber;
+      const char *filename;
+      NSLinkEditError(&errors, &errornumber, &filename, &err);
+    }
+    NSDestroyObjectFileImage(image);
+  }
+  else if ((result == NSObjectFileImageFormat ||
+            result == NSObjectFileImageInappropriateFile)) {
+    err = "invalid MH_BUNDLE file";
+  }
+  else {
+    err = "unable to create object file image";
+  }
+
+  /* Can't continue without a handle */
+  if (!handle) goto failure;
+
+  if (!(symbol = NSLookupSymbolInModule(handle, "_idea_get_info"))) {
+    err = "idea_get_info undefined";
+    goto failure;
+  }
+  if (!(sym = NSAddressOfSymbol(symbol))) {
+    err = "can't resolve idea_get_info";
+    goto failure;
+  }
+
+  return sym;
+
+ failure:
+  log_error ("error loading module `%s': %s\n", name, (err) ? err : 
"unknown error");
+  if (handle) NSUnLinkModule(handle, FALSE);
+
+#else if defined (HAVE_DL_DLOPEN)
    const char *err;
    void *handle;
    void *sym;
@@ -144,6 +194,9 @@
   failure:
    if (handle)
        dlclose (handle);
+#else
+  #error USE_DYNAMIC_LINKING with no implementation
+#endif /*HAVE_DL_DLOPEN*/
  #endif /*USE_DYNAMIC_LINKING*/
    return NULL;
  }





More information about the Gnupg-devel mailing list