pike.git / src / dynamic_load.c

version» Context lines:

pike.git/src/dynamic_load.c:1:   /*   || This file is part of Pike. For copyright information see COPYRIGHT.   || Pike is distributed under GPL, LGPL and MPL. See the file COPYING   || for more information. - || $Id: dynamic_load.c,v 1.76 2004/03/21 17:19:40 grubba Exp $ + || $Id: dynamic_load.c,v 1.77 2004/03/21 17:59:24 grubba Exp $   */      #ifdef TESTING   #define NO_PIKE_INCLUDES   #define CREATE_MAIN   #define NO_PIKE_GUTS   #endif      #ifndef NO_PIKE_INCLUDES   # include "global.h"
pike.git/src/dynamic_load.c:17:   # include "constants.h"   # include "pike_error.h"   # include "module.h"   # include "stralloc.h"   # include "pike_macros.h"   # include "main.h"   # include "constants.h"   # include "language.h"   # include "lex.h"    - RCSID("$Id: dynamic_load.c,v 1.76 2004/03/21 17:19:40 grubba Exp $"); + RCSID("$Id: dynamic_load.c,v 1.77 2004/03/21 17:59:24 grubba Exp $");      #else /* TESTING */      #include <stdio.h>      #endif /* !TESTING */      #ifdef HAVE_ERRNO_H   #include <errno.h>   #endif /* HAVE_ERRNO_H */
pike.git/src/dynamic_load.c:236:   #endif /* USE_HPUX_DL */      #ifdef USE_DYLD      #include <mach-o/dyld.h>      #define RTLD_NOW NSLINKMODULE_OPTION_BINDNOW      #define dlinit() _dyld_present()    + struct pike_dl_handle + { +  NSObjectFileImage image; +  NSModule module; + }; +  + static void *dlclose(void *handle_) + { +  struct pike_dl_handle *handle = handle_; +  if (handle) { +  if (handle->module) +  NSUnLinkModule(handle->module, NSUNLINKMODULE_OPTION_NONE); +  handle->module = NULL; +  if (handle->image) +  NSDestroyObjectFileImage(handle->image); +  handle->image = NULL; +  free(handle); +  } +  return NULL; + } +  + static char *pike_dl_error = NULL; +    static void *dlopen(const char *module_name, int how)   { -  +  struct pike_dl_handle *handle = malloc(sizeof(struct pike_dl_handle));    NSObjectFileImageReturnCode code = 0; -  NSObjectFileImage image = NULL; +     -  +  pike_dl_error = NULL; +  if (!handle) { +  pike_dl_error = "Out of memory."; +  return NULL; +  } +  +  handle->image = NULL; +  handle->module = NULL; +     /* FIXME: Should be fixed to detect if the module already is loaded. */ -  if ((code = NSCreateObjectFileImageFromFile(module_name, &image)) != +  if ((code = NSCreateObjectFileImageFromFile(module_name, +  &handle->image)) !=    NSObjectFileImageSuccess) { -  + #ifdef PIKE_DEBUG    fprintf(stderr, "NSCreateObjectFileImageFromFile(\"%s\") failed with %d\n",    module_name, code); -  + #endif /* PIKE_DEBUG */ +  pike_dl_error = "NSCreateObjectFileImageFromFile() failed."; +  dlclose(handle);    return NULL;    } -  /* FIXME: image should be freed somewhere! */ +     -  fprintf(stderr, "dlopen(\"%s\") ==> image:%p\n", -  module_name, image); -  -  return NSLinkModule(image, module_name, +  handle->module = NSLinkModule(handle->image, module_name,    how | NSLINKMODULE_OPTION_RETURN_ON_ERROR |    NSLINKMODULE_OPTION_PRIVATE); -  +  if (!handle->module) { +  dlclose(handle); +  return NULL;    } -  - static char *dlerror(void) - { -  NSLinkEditErrors class = 0; -  int error_number = 0; -  char *file_name = NULL; -  char *error_string = NULL; -  NSLinkEditError(&class, &error_number, &file_name, &error_string); -  return error_string; +  return handle;   }    - static void *dlsym(void *module, char *function) + static void *dlsym(void *handle, char *function)   { -  NSSymbol symbol = NSLookupSymbolInModule(module, function); +  NSSymbol symbol = +  NSLookupSymbolInModule(((struct pike_dl_handle *)handle)->module, +  function);    return symbol?NSAddressOfSymbol(symbol):NULL;   }    - static void *dlclose(void *module) + static const char *dlerror(void)   { -  NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE); -  return NULL; +  NSLinkEditErrors class = 0; +  int error_number = 0; +  const char *file_name = NULL; +  const char *error_string = NULL; +  +  if (pike_dl_error) return pike_dl_error; +  +  NSLinkEditError(&class, &error_number, &file_name, &error_string); +  return error_string;   }      #endif /* USE_DYLD */         #ifndef EMULATE_DLOPEN      #ifdef HAVE_DLFCN_H   #include <dlfcn.h>   #endif