00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 #include <stdio.h> 
00028 #include <stdlib.h>
00029 #include <string.h>
00030 #include "vmddlopen.h"
00031 
00032 #if defined(__hpux)
00033 
00034 #include <dl.h>
00035 #include <errno.h>
00036 #include <string.h>
00037 
00038 void *vmddlopen( const char *path) {
00039     void *ret;
00040     ret = shl_load( path, BIND_IMMEDIATE | BIND_FIRST | BIND_VERBOSE, 0);
00041     return ret;
00042 }
00043 
00044 int vmddlclose( void *handle ) {
00045     return shl_unload( (shl_t) handle );
00046 }
00047 
00048 void *vmddlsym( void *handle, const char *sym ) {
00049     void *value=0;
00050 
00051     if ( shl_findsym( (shl_t*)&handle, sym, TYPE_UNDEFINED, &value ) != 0 ) 
00052         return 0;
00053     return value;
00054 }
00055 
00056 const char *vmddlerror( void  ) {
00057     return strerror( errno );
00058 }
00059 
00060 #elif 0 && defined(__APPLE__)
00061 
00062 
00063 
00064 #include <mach-o/dyld.h>
00065 
00066 void *vmddlopen( const char *path) {
00067   NSObjectFileImage image;
00068   NSObjectFileImageReturnCode retval;
00069   NSModule module;
00070 
00071   retval = NSCreateObjectFileImageFromFile(path, &image);
00072   if (retval != NSObjectFileImageSuccess)
00073     return NULL;
00074 
00075   module = NSLinkModule(image, path,
00076             NSLINKMODULE_OPTION_BINDNOW | NSLINKMODULE_OPTION_PRIVATE
00077             | NSLINKMODULE_OPTION_RETURN_ON_ERROR);
00078   return module;  
00079 }
00080 
00081 int vmddlclose( void *handle ) {
00082   NSModule module = (NSModule *)handle;
00083   NSUnLinkModule(module, NSUNLINKMODULE_OPTION_NONE);
00084   return 0;
00085 }
00086 
00087 void *vmddlsym( void *handle, const char *symname ) {
00088   char *realsymname;
00089   NSModule module;
00090   NSSymbol sym;
00091   
00092   realsymname = (char *)malloc(strlen(symname)+2);
00093   strcpy(realsymname, "_");
00094   strcat(realsymname, symname);
00095   module = (NSModule)handle;
00096   sym = NSLookupSymbolInModule(module, realsymname);
00097   free(realsymname);
00098   if (sym) 
00099     return (void *)(NSAddressOfSymbol(sym));
00100   return NULL;
00101 }
00102 
00103 const char *vmddlerror( void  ) {
00104   NSLinkEditErrors c;
00105   int errorNumber;
00106   const char *fileName;
00107   const char *errorString = NULL;
00108   NSLinkEditError(&c, &errorNumber, &fileName, &errorString);
00109   return errorString;
00110 }
00111 
00112 #elif defined(_MSC_VER)
00113 
00114 #include <windows.h>
00115 
00116 void *vmddlopen(const char *fname) {
00117   return (void *)LoadLibrary(fname);
00118 }
00119 
00120 const char *vmddlerror(void) {
00121   static CHAR szBuf[80]; 
00122   DWORD dw = GetLastError(); 
00123  
00124   sprintf(szBuf, "vmddlopen failed: GetLastError returned %u\n", dw); 
00125   return szBuf;
00126 }
00127 
00128 void *vmddlsym(void *h, const char *sym) {
00129   return (void *)GetProcAddress((HINSTANCE)h, sym);
00130 }
00131 
00132 int vmddlclose(void *h) {
00133   
00134   return !FreeLibrary((HINSTANCE)h);
00135 }
00136 
00137 #else
00138 
00139 
00140 #include <dlfcn.h>
00141 
00142 void *vmddlopen(const char *fname) {
00143   return dlopen(fname, RTLD_NOW);
00144 }
00145 const char *vmddlerror(void) {
00146   return dlerror();
00147 }
00148 void *vmddlsym(void *h, const char *sym) {
00149   return dlsym(h, sym);
00150 }
00151 int vmddlclose(void *h) {
00152   return dlclose(h);
00153 }
00154 
00155 #endif 
00156