2 /* #include "XSUB.h" so we can compile on threaded perls */
5 static symbol_table_t symbol_table={i_has_format,ICL_set_internal,ICL_info,
6 i_img_new,i_img_empty,i_img_empty_ch,i_img_exorcise,
7 i_img_info,i_img_setmask,i_img_getmask,
8 i_box,i_draw,i_arc,i_copyto,i_copyto_trans,i_rubthru};
12 Dynamic loading works like this:
13 dynaload opens the shared object and
14 loads all the functions into an array of functions
15 it returns a string from the dynamic function that
16 can be supplied to the parser for evaling.
20 DSO_call(DSO_handle *handle,int func_index,HV* hv) {
21 mm_log((1,"DSO_call(handle 0x%X, func_index %d, hv 0x%X)\n",handle,func_index,hv));
22 (handle->function_list[func_index].iptr)((void*)hv);
26 #if defined( OS_hpux )
29 DSO_open(char* file,char** evalstring) {
31 void *d_handle,**plugin_symtab,**plugin_utiltab;
32 int rc,*iptr, (*fptr)(int);
33 func_ptr *function_list;
34 DSO_handle *dso_handle;
35 void (*f)(void *s,void *u); /* these will just have to be void for now */
40 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
42 if ( (tt_handle = shl_load(file, BIND_DEFERRED,0L)) == NULL) return NULL;
43 if ( (shl_findsym(&tt_handle, I_EVALSTR,TYPE_UNDEFINED,(void*)evalstring))) return NULL;
46 if ( (shl_findsym(&tt_handle, "symbol_table",TYPE_UNDEFINED,(void*)&plugin_symtab))) return NULL;
47 if ( (shl_findsym(&tt_handle, "util_table",TYPE_UNDEFINED,&plugin_utiltab))) return NULL;
48 (*plugin_symtab)=&symbol_table;
49 (*plugin_utiltab)=&i_UTIL_table;
52 if ( (shl_findsym(&tt_handle, I_INSTALL_TABLES ,TYPE_UNDEFINED, &f ))) return NULL;
54 mm_log( (1,"Calling install_tables\n") );
55 f(&symbol_table,&i_UTIL_table);
56 mm_log( (1,"Call ok.\n") );
58 if ( (shl_findsym(&tt_handle, I_FUNCTION_LIST ,TYPE_UNDEFINED,(func_ptr*)&function_list))) return NULL;
59 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) return NULL;
61 dso_handle->handle=tt_handle; /* needed to close again */
62 dso_handle->function_list=function_list;
63 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); return NULL; }
64 strcpy(dso_handle->filename,file);
66 mm_log((1,"DSO_open <- (0x%X)\n",dso_handle));
67 return (void*)dso_handle;
71 DSO_close(void *ptr) {
72 DSO_handle *handle=(DSO_handle*) ptr;
73 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
74 return !shl_unload((handle->handle));
80 DSO_open(char *file, char **evalstring) {
82 func_ptr *function_list;
83 DSO_handle *dso_handle;
85 void (*f)(void *s,void *u); /* these will just have to be void for now */
87 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
90 if ((d_handle = LoadLibrary(file)) == NULL) {
91 mm_log((1, "DSO_open: LoadLibrary(%s) failed: %lu\n", file, GetLastError()));
94 if ( (*evalstring = (char *)GetProcAddress(d_handle, I_EVALSTR)) == NULL) {
95 mm_log((1,"DSO_open: GetProcAddress didn't fine '%s': %lu\n", I_EVALSTR, GetLastError()));
96 FreeLibrary(d_handle);
99 if ((f = (void (*)(void *, void*))GetProcAddress(d_handle, I_INSTALL_TABLES)) == NULL) {
100 mm_log((1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_INSTALL_TABLES, GetLastError()));
101 FreeLibrary(d_handle);
104 mm_log((1, "Calling install tables\n"));
105 f(&symbol_table, &i_UTIL_table);
106 mm_log((1, "Call ok\n"));
108 if ( (function_list = (func_ptr *)GetProcAddress(d_handle, I_FUNCTION_LIST)) == NULL) {
109 mm_log((1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_FUNCTION_LIST, GetLastError()));
110 FreeLibrary(d_handle);
113 if ( (dso_handle = (DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) {
114 mm_log( (1, "DSO_Open: out of memory\n") );
115 FreeLibrary(d_handle);
118 dso_handle->handle=d_handle; /* needed to close again */
119 dso_handle->function_list=function_list;
120 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); FreeLibrary(d_handle); return NULL; }
121 strcpy(dso_handle->filename,file);
123 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
124 return (void*)dso_handle;
129 DSO_close(void *ptr) {
130 DSO_handle *handle = (DSO_handle *)ptr;
131 FreeLibrary(handle->handle);
132 free(handle->filename);
138 /* OS/2 has no dlclose; Perl doesn't provide one. */
139 #ifdef __EMX__ /* OS/2 */
141 dlclose(minthandle_t h) {
142 return DosFreeModule(h) ? -1 : 0;
148 #import <mach-o/dyld.h>
150 static char *dl_error = "unknown";
152 static char *dlopen(char *path, int mode /* mode is ignored */)
155 NSObjectFileImage ofile;
156 NSModule handle = NULL;
160 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
161 if (dyld_result != NSObjectFileImageSuccess)
163 switch (dyld_result) {
164 case NSObjectFileImageFailure:
165 dl_error = "object file setup failure";
167 case NSObjectFileImageInappropriateFile:
168 dl_error = "not a Mach-O MH_BUNDLE file type";
170 case NSObjectFileImageArch:
171 dl_error = "no object for this architecture";
173 case NSObjectFileImageFormat:
174 dl_error = "bad object file format";
176 case NSObjectFileImageAccess:
177 dl_error = "can't read object file";
180 dl_error = "unknown error from NSCreateObjectFileImageFromFile()";
186 // NSLinkModule will cause the run to abort on any link error's
187 // not very friendly but the error recovery functionality is limited.
188 handle = NSLinkModule(ofile, path, TRUE);
195 dlsym(void *handle, char *symbol)
199 if (NSIsSymbolNameDefined(symbol))
201 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
205 dl_error = "cannot find symbol";
212 static int dlclose(void *handle) /* stub only */
217 static char *dlerror(void) /* stub only */
219 printf("Error occurred\n");
228 DSO_open(char* file,char** evalstring) {
230 func_ptr *function_list;
231 DSO_handle *dso_handle;
233 void (*f)(void *s,void *u); /* these will just have to be void for now */
237 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
239 if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) {
240 mm_log( (1,"DSO_open: dlopen failed: %s.\n",dlerror()) );
244 if ( (*evalstring = (char *)dlsym(d_handle, I_EVALSTR)) == NULL) {
245 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_EVALSTR,dlerror()) );
251 I'll just leave this thing in here for now if I need it real soon
253 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_SYMBOL_TABLE ));
254 if ( (plugin_symtab = dlsym(d_handle, I_SYMBOL_TABLE)) == NULL) {
255 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_SYMBOL_TABLE,dlerror()) );
259 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_UTIL_TABLE ));
260 if ( (plugin_utiltab = dlsym(d_handle, I_UTIL_TABLE)) == NULL) {
261 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_UTIL_TABLE,dlerror()) );
267 f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES);
268 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_INSTALL_TABLES ));
269 if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) {
270 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_INSTALL_TABLES,dlerror()) );
274 mm_log( (1,"Calling install_tables\n") );
275 f(&symbol_table,&i_UTIL_table);
276 mm_log( (1,"Call ok.\n") );
278 /* (*plugin_symtab)=&symbol_table;
279 (*plugin_utiltab)=&i_UTIL_table; */
281 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_FUNCTION_LIST ));
282 if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) {
283 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_FUNCTION_LIST,dlerror()) );
287 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) return NULL;
289 dso_handle->handle=d_handle; /* needed to close again */
290 dso_handle->function_list=function_list;
291 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); return NULL; }
292 strcpy(dso_handle->filename,file);
294 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
295 return (void*)dso_handle;
299 DSO_close(void *ptr) {
301 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
302 handle=(DSO_handle*) ptr;
303 return !dlclose(handle->handle);