3 typedef shl_t minthandle_t;
5 #define WIN32_LEAN_AND_MEAN
7 typedef HMODULE minthandle_t;
8 #undef WIN32_LEAN_AND_MEAN
9 #elif defined(OS_darwin)
10 #define DL_LOADONCEONLY
15 #import <mach-o/dyld.h>
16 typedef void *minthandle_t;
19 typedef void *minthandle_t;
24 struct DSO_handle_tag {
27 func_ptr *function_list;
32 /* #include "XSUB.h" so we can compile on threaded perls */
35 static symbol_table_t symbol_table=
57 Dynamic loading works like this:
58 dynaload opens the shared object and
59 loads all the functions into an array of functions
60 it returns a string from the dynamic function that
61 can be supplied to the parser for evaling.
65 DSO_call(DSO_handle *handle,int func_index,HV* hv) {
66 mm_log((1,"DSO_call(handle 0x%X, func_index %d, hv 0x%X)\n",handle,func_index,hv));
67 (handle->function_list[func_index].iptr)((void*)hv);
71 DSO_funclist(DSO_handle *handle) {
72 return handle->function_list;
76 #if defined( OS_hpux )
79 DSO_open(char* file,char** evalstring) {
81 void *d_handle,**plugin_symtab,**plugin_utiltab;
82 int rc,*iptr, (*fptr)(int);
83 func_ptr *function_list;
84 DSO_handle *dso_handle;
85 void (*f)(void *s,void *u); /* these will just have to be void for now */
90 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
92 if ( (tt_handle = shl_load(file, BIND_DEFERRED,0L)) == NULL) return NULL;
93 if ( (shl_findsym(&tt_handle, I_EVALSTR,TYPE_UNDEFINED,(void*)evalstring))) return NULL;
96 if ( (shl_findsym(&tt_handle, "symbol_table",TYPE_UNDEFINED,(void*)&plugin_symtab))) return NULL;
97 if ( (shl_findsym(&tt_handle, "util_table",TYPE_UNDEFINED,&plugin_utiltab))) return NULL;
98 (*plugin_symtab)=&symbol_table;
99 (*plugin_utiltab)=&i_UTIL_table;
102 if ( (shl_findsym(&tt_handle, I_INSTALL_TABLES ,TYPE_UNDEFINED, &f ))) return NULL;
104 mm_log( (1,"Calling install_tables\n") );
105 f(&symbol_table,&i_UTIL_table);
106 mm_log( (1,"Call ok.\n") );
108 if ( (shl_findsym(&tt_handle, I_FUNCTION_LIST ,TYPE_UNDEFINED,(func_ptr*)&function_list))) return NULL;
109 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) /* checked 17jul05 tonyc */
112 dso_handle->handle=tt_handle; /* needed to close again */
113 dso_handle->function_list=function_list;
114 if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
115 free(dso_handle); return NULL;
117 strcpy(dso_handle->filename,file);
119 mm_log((1,"DSO_open <- (0x%X)\n",dso_handle));
120 return (void*)dso_handle;
124 DSO_close(void *ptr) {
125 DSO_handle *handle=(DSO_handle*) ptr;
126 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
127 return !shl_unload((handle->handle));
133 DSO_open(char *file, char **evalstring) {
135 func_ptr *function_list;
136 DSO_handle *dso_handle;
138 void (*f)(void *s,void *u); /* these will just have to be void for now */
140 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
143 if ((d_handle = LoadLibrary(file)) == NULL) {
144 mm_log((1, "DSO_open: LoadLibrary(%s) failed: %lu\n", file, GetLastError()));
147 if ( (*evalstring = (char *)GetProcAddress(d_handle, I_EVALSTR)) == NULL) {
148 mm_log((1,"DSO_open: GetProcAddress didn't fine '%s': %lu\n", I_EVALSTR, GetLastError()));
149 FreeLibrary(d_handle);
152 if ((f = (void (*)(void *, void*))GetProcAddress(d_handle, I_INSTALL_TABLES)) == NULL) {
153 mm_log((1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_INSTALL_TABLES, GetLastError()));
154 FreeLibrary(d_handle);
157 mm_log((1, "Calling install tables\n"));
158 f(&symbol_table, &i_UTIL_table);
159 mm_log((1, "Call ok\n"));
161 if ( (function_list = (func_ptr *)GetProcAddress(d_handle, I_FUNCTION_LIST)) == NULL) {
162 mm_log((1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_FUNCTION_LIST, GetLastError()));
163 FreeLibrary(d_handle);
166 if ( (dso_handle = (DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) { /* checked 17jul05 tonyc */
167 mm_log( (1, "DSO_Open: out of memory\n") );
168 FreeLibrary(d_handle);
171 dso_handle->handle=d_handle; /* needed to close again */
172 dso_handle->function_list=function_list;
173 if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
175 FreeLibrary(d_handle);
178 strcpy(dso_handle->filename,file);
180 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
181 return (void*)dso_handle;
186 DSO_close(void *ptr) {
187 DSO_handle *handle = (DSO_handle *)ptr;
188 BOOL result = FreeLibrary(handle->handle);
189 free(handle->filename);
197 /* OS/2 has no dlclose; Perl doesn't provide one. */
198 #ifdef __EMX__ /* OS/2 */
200 dlclose(minthandle_t h) {
201 return DosFreeModule(h) ? -1 : 0;
207 #import <mach-o/dyld.h>
209 static char *dl_error = "unknown";
211 static char *dlopen(char *path, int mode /* mode is ignored */)
214 NSObjectFileImage ofile;
215 NSModule handle = NULL;
219 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
220 if (dyld_result != NSObjectFileImageSuccess)
222 switch (dyld_result) {
223 case NSObjectFileImageFailure:
224 dl_error = "object file setup failure";
226 case NSObjectFileImageInappropriateFile:
227 dl_error = "not a Mach-O MH_BUNDLE file type";
229 case NSObjectFileImageArch:
230 dl_error = "no object for this architecture";
232 case NSObjectFileImageFormat:
233 dl_error = "bad object file format";
235 case NSObjectFileImageAccess:
236 dl_error = "can't read object file";
239 dl_error = "unknown error from NSCreateObjectFileImageFromFile()";
245 // NSLinkModule will cause the run to abort on any link error's
246 // not very friendly but the error recovery functionality is limited.
247 handle = NSLinkModule(ofile, path, TRUE);
254 dlsym(void *handle, char *symbol)
258 if (NSIsSymbolNameDefined(symbol))
260 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
264 dl_error = "cannot find symbol";
271 static int dlclose(void *handle) /* stub only */
276 static char *dlerror(void) /* stub only */
278 printf("Error occurred\n");
287 DSO_open(char* file,char** evalstring) {
289 func_ptr *function_list;
290 DSO_handle *dso_handle;
292 void (*f)(void *s,void *u); /* these will just have to be void for now */
296 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
298 if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) {
299 mm_log( (1,"DSO_open: dlopen failed: %s.\n",dlerror()) );
303 if ( (*evalstring = (char *)dlsym(d_handle, I_EVALSTR)) == NULL) {
304 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_EVALSTR,dlerror()) );
310 I'll just leave this thing in here for now if I need it real soon
312 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_SYMBOL_TABLE ));
313 if ( (plugin_symtab = dlsym(d_handle, I_SYMBOL_TABLE)) == NULL) {
314 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_SYMBOL_TABLE,dlerror()) );
318 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_UTIL_TABLE ));
319 if ( (plugin_utiltab = dlsym(d_handle, I_UTIL_TABLE)) == NULL) {
320 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_UTIL_TABLE,dlerror()) );
326 f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES);
327 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_INSTALL_TABLES ));
328 if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) {
329 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_INSTALL_TABLES,dlerror()) );
333 mm_log( (1,"Calling install_tables\n") );
334 f(&symbol_table,&i_UTIL_table);
335 mm_log( (1,"Call ok.\n") );
337 /* (*plugin_symtab)=&symbol_table;
338 (*plugin_utiltab)=&i_UTIL_table; */
340 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_FUNCTION_LIST ));
341 if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) {
342 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_FUNCTION_LIST,dlerror()) );
346 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) /* checked 17jul05 tonyc */
349 dso_handle->handle=d_handle; /* needed to close again */
350 dso_handle->function_list=function_list;
351 if ( (dso_handle->filename=(char*)malloc(strlen(file)+1)) == NULL) { /* checked 17jul05 tonyc */
355 strcpy(dso_handle->filename,file);
357 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
358 return (void*)dso_handle;
362 DSO_close(void *ptr) {
364 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
365 handle=(DSO_handle*) ptr;
366 return !dlclose(handle->handle);