00001 /*************************************************************************** 00002 *cr 00003 *cr (C) Copyright 1995-2019 The Board of Trustees of the 00004 *cr University of Illinois 00005 *cr All Rights Reserved 00006 *cr 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * RCS INFORMATION: 00011 * 00012 * $RCSfile: vmddlopen.c,v $ 00013 * $Author: johns $ $Locker: $ $State: Exp $ 00014 * $Revision: 1.21 $ $Date: 2019年01月17日 21:21:03 $ 00015 * 00016 *************************************************************************** 00017 * DESCRIPTION: 00018 * Routines for loading dynamic link libraries and shared object files 00019 * on various platforms, abstracting from machine dependent APIs. 00020 * 00021 * LICENSE: 00022 * UIUC Open Source License 00023 * http://www.ks.uiuc.edu/Research/vmd/plugins/pluginlicense.html 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 * This is only needed for MacOS X version 10.3 or older 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; /* module will be NULL on error */ 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 /* Hack around the leading underscore in the symbol name */ 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 /* FreeLibrary returns nonzero on success */ 00134 return !FreeLibrary((HINSTANCE)h); 00135 } 00136 00137 #else 00138 00139 /* All remaining platforms (not Windows, HP-UX, or MacOS X <= 10.3) */ 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