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 BOOL result = FreeLibrary(handle->handle);
132 free(handle->filename);
140 /* OS/2 has no dlclose; Perl doesn't provide one. */
141 #ifdef __EMX__ /* OS/2 */
143 dlclose(minthandle_t h) {
144 return DosFreeModule(h) ? -1 : 0;
150 #import <mach-o/dyld.h>
152 static char *dl_error = "unknown";
154 static char *dlopen(char *path, int mode /* mode is ignored */)
157 NSObjectFileImage ofile;
158 NSModule handle = NULL;
162 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
163 if (dyld_result != NSObjectFileImageSuccess)
165 switch (dyld_result) {
166 case NSObjectFileImageFailure:
167 dl_error = "object file setup failure";
169 case NSObjectFileImageInappropriateFile:
170 dl_error = "not a Mach-O MH_BUNDLE file type";
172 case NSObjectFileImageArch:
173 dl_error = "no object for this architecture";
175 case NSObjectFileImageFormat:
176 dl_error = "bad object file format";
178 case NSObjectFileImageAccess:
179 dl_error = "can't read object file";
182 dl_error = "unknown error from NSCreateObjectFileImageFromFile()";
188 // NSLinkModule will cause the run to abort on any link error's
189 // not very friendly but the error recovery functionality is limited.
190 handle = NSLinkModule(ofile, path, TRUE);
197 dlsym(void *handle, char *symbol)
201 if (NSIsSymbolNameDefined(symbol))
203 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
207 dl_error = "cannot find symbol";
214 static int dlclose(void *handle) /* stub only */
219 static char *dlerror(void) /* stub only */
221 printf("Error occurred\n");
230 DSO_open(char* file,char** evalstring) {
232 func_ptr *function_list;
233 DSO_handle *dso_handle;
235 void (*f)(void *s,void *u); /* these will just have to be void for now */
239 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
241 if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) {
242 mm_log( (1,"DSO_open: dlopen failed: %s.\n",dlerror()) );
246 if ( (*evalstring = (char *)dlsym(d_handle, I_EVALSTR)) == NULL) {
247 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_EVALSTR,dlerror()) );
253 I'll just leave this thing in here for now if I need it real soon
255 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_SYMBOL_TABLE ));
256 if ( (plugin_symtab = dlsym(d_handle, I_SYMBOL_TABLE)) == NULL) {
257 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_SYMBOL_TABLE,dlerror()) );
261 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_UTIL_TABLE ));
262 if ( (plugin_utiltab = dlsym(d_handle, I_UTIL_TABLE)) == NULL) {
263 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_UTIL_TABLE,dlerror()) );
269 f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES);
270 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_INSTALL_TABLES ));
271 if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) {
272 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_INSTALL_TABLES,dlerror()) );
276 mm_log( (1,"Calling install_tables\n") );
277 f(&symbol_table,&i_UTIL_table);
278 mm_log( (1,"Call ok.\n") );
280 /* (*plugin_symtab)=&symbol_table;
281 (*plugin_utiltab)=&i_UTIL_table; */
283 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_FUNCTION_LIST ));
284 if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) {
285 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_FUNCTION_LIST,dlerror()) );
289 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) return NULL;
291 dso_handle->handle=d_handle; /* needed to close again */
292 dso_handle->function_list=function_list;
293 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); return NULL; }
294 strcpy(dso_handle->filename,file);
296 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
297 return (void*)dso_handle;
301 DSO_close(void *ptr) {
303 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
304 handle=(DSO_handle*) ptr;
305 return !dlclose(handle->handle);