3 /* #include "XSUB.h" so we can compile on threaded perls */
6 static symbol_table_t symbol_table={i_has_format,ICL_set_internal,ICL_info,
7 i_img_new,i_img_empty,i_img_empty_ch,i_img_exorcise,
8 i_img_info,i_img_setmask,i_img_getmask,
9 i_box,i_line,i_arc,i_copyto,i_copyto_trans,i_rubthru};
13 Dynamic loading works like this:
14 dynaload opens the shared object and
15 loads all the functions into an array of functions
16 it returns a string from the dynamic function that
17 can be supplied to the parser for evaling.
21 DSO_call(DSO_handle *handle,int func_index,HV* hv) {
22 mm_log((1,"DSO_call(handle 0x%X, func_index %d, hv 0x%X)\n",handle,func_index,hv));
23 (handle->function_list[func_index].iptr)((void*)hv);
27 #if defined( OS_hpux )
30 DSO_open(char* file,char** evalstring) {
32 void *d_handle,**plugin_symtab,**plugin_utiltab;
33 int rc,*iptr, (*fptr)(int);
34 func_ptr *function_list;
35 DSO_handle *dso_handle;
36 void (*f)(void *s,void *u); /* these will just have to be void for now */
41 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
43 if ( (tt_handle = shl_load(file, BIND_DEFERRED,0L)) == NULL) return NULL;
44 if ( (shl_findsym(&tt_handle, I_EVALSTR,TYPE_UNDEFINED,(void*)evalstring))) return NULL;
47 if ( (shl_findsym(&tt_handle, "symbol_table",TYPE_UNDEFINED,(void*)&plugin_symtab))) return NULL;
48 if ( (shl_findsym(&tt_handle, "util_table",TYPE_UNDEFINED,&plugin_utiltab))) return NULL;
49 (*plugin_symtab)=&symbol_table;
50 (*plugin_utiltab)=&i_UTIL_table;
53 if ( (shl_findsym(&tt_handle, I_INSTALL_TABLES ,TYPE_UNDEFINED, &f ))) return NULL;
55 mm_log( (1,"Calling install_tables\n") );
56 f(&symbol_table,&i_UTIL_table);
57 mm_log( (1,"Call ok.\n") );
59 if ( (shl_findsym(&tt_handle, I_FUNCTION_LIST ,TYPE_UNDEFINED,(func_ptr*)&function_list))) return NULL;
60 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) return NULL;
62 dso_handle->handle=tt_handle; /* needed to close again */
63 dso_handle->function_list=function_list;
64 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); return NULL; }
65 strcpy(dso_handle->filename,file);
67 mm_log((1,"DSO_open <- (0x%X)\n",dso_handle));
68 return (void*)dso_handle;
72 DSO_close(void *ptr) {
73 DSO_handle *handle=(DSO_handle*) ptr;
74 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
75 return !shl_unload((handle->handle));
81 DSO_open(char *file, char **evalstring) {
83 func_ptr *function_list;
84 DSO_handle *dso_handle;
86 void (*f)(void *s,void *u); /* these will just have to be void for now */
88 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
91 if ((d_handle = LoadLibrary(file)) == NULL) {
92 mm_log((1, "DSO_open: LoadLibrary(%s) failed: %lu\n", file, GetLastError()));
95 if ( (*evalstring = (char *)GetProcAddress(d_handle, I_EVALSTR)) == NULL) {
96 mm_log((1,"DSO_open: GetProcAddress didn't fine '%s': %lu\n", I_EVALSTR, GetLastError()));
97 FreeLibrary(d_handle);
100 if ((f = (void (*)(void *, void*))GetProcAddress(d_handle, I_INSTALL_TABLES)) == NULL) {
101 mm_log((1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_INSTALL_TABLES, GetLastError()));
102 FreeLibrary(d_handle);
105 mm_log((1, "Calling install tables\n"));
106 f(&symbol_table, &i_UTIL_table);
107 mm_log((1, "Call ok\n"));
109 if ( (function_list = (func_ptr *)GetProcAddress(d_handle, I_FUNCTION_LIST)) == NULL) {
110 mm_log((1, "DSO_open: GetProcAddress didn't find '%s': %lu\n", I_FUNCTION_LIST, GetLastError()));
111 FreeLibrary(d_handle);
114 if ( (dso_handle = (DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) {
115 mm_log( (1, "DSO_Open: out of memory\n") );
116 FreeLibrary(d_handle);
119 dso_handle->handle=d_handle; /* needed to close again */
120 dso_handle->function_list=function_list;
121 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); FreeLibrary(d_handle); return NULL; }
122 strcpy(dso_handle->filename,file);
124 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
125 return (void*)dso_handle;
130 DSO_close(void *ptr) {
131 DSO_handle *handle = (DSO_handle *)ptr;
132 BOOL result = FreeLibrary(handle->handle);
133 free(handle->filename);
141 /* OS/2 has no dlclose; Perl doesn't provide one. */
142 #ifdef __EMX__ /* OS/2 */
144 dlclose(minthandle_t h) {
145 return DosFreeModule(h) ? -1 : 0;
151 #import <mach-o/dyld.h>
153 static char *dl_error = "unknown";
155 static char *dlopen(char *path, int mode /* mode is ignored */)
158 NSObjectFileImage ofile;
159 NSModule handle = NULL;
163 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
164 if (dyld_result != NSObjectFileImageSuccess)
166 switch (dyld_result) {
167 case NSObjectFileImageFailure:
168 dl_error = "object file setup failure";
170 case NSObjectFileImageInappropriateFile:
171 dl_error = "not a Mach-O MH_BUNDLE file type";
173 case NSObjectFileImageArch:
174 dl_error = "no object for this architecture";
176 case NSObjectFileImageFormat:
177 dl_error = "bad object file format";
179 case NSObjectFileImageAccess:
180 dl_error = "can't read object file";
183 dl_error = "unknown error from NSCreateObjectFileImageFromFile()";
189 // NSLinkModule will cause the run to abort on any link error's
190 // not very friendly but the error recovery functionality is limited.
191 handle = NSLinkModule(ofile, path, TRUE);
198 dlsym(void *handle, char *symbol)
202 if (NSIsSymbolNameDefined(symbol))
204 addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
208 dl_error = "cannot find symbol";
215 static int dlclose(void *handle) /* stub only */
220 static char *dlerror(void) /* stub only */
222 printf("Error occurred\n");
231 DSO_open(char* file,char** evalstring) {
233 func_ptr *function_list;
234 DSO_handle *dso_handle;
236 void (*f)(void *s,void *u); /* these will just have to be void for now */
240 mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
242 if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) {
243 mm_log( (1,"DSO_open: dlopen failed: %s.\n",dlerror()) );
247 if ( (*evalstring = (char *)dlsym(d_handle, I_EVALSTR)) == NULL) {
248 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_EVALSTR,dlerror()) );
254 I'll just leave this thing in here for now if I need it real soon
256 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_SYMBOL_TABLE ));
257 if ( (plugin_symtab = dlsym(d_handle, I_SYMBOL_TABLE)) == NULL) {
258 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_SYMBOL_TABLE,dlerror()) );
262 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_UTIL_TABLE ));
263 if ( (plugin_utiltab = dlsym(d_handle, I_UTIL_TABLE)) == NULL) {
264 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_UTIL_TABLE,dlerror()) );
270 f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES);
271 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_INSTALL_TABLES ));
272 if ( (f = (void(*)(void *s,void *u))dlsym(d_handle, I_INSTALL_TABLES)) == NULL) {
273 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_INSTALL_TABLES,dlerror()) );
277 mm_log( (1,"Calling install_tables\n") );
278 f(&symbol_table,&i_UTIL_table);
279 mm_log( (1,"Call ok.\n") );
281 /* (*plugin_symtab)=&symbol_table;
282 (*plugin_utiltab)=&i_UTIL_table; */
284 mm_log( (1,"DSO_open: going to dlsym '%s'\n", I_FUNCTION_LIST ));
285 if ( (function_list=(func_ptr *)dlsym(d_handle, I_FUNCTION_LIST)) == NULL) {
286 mm_log( (1,"DSO_open: dlsym didn't find '%s': %s.\n",I_FUNCTION_LIST,dlerror()) );
290 if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) return NULL;
292 dso_handle->handle=d_handle; /* needed to close again */
293 dso_handle->function_list=function_list;
294 if ( (dso_handle->filename=(char*)malloc(strlen(file))) == NULL) { free(dso_handle); return NULL; }
295 strcpy(dso_handle->filename,file);
297 mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
298 return (void*)dso_handle;
302 DSO_close(void *ptr) {
304 mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
305 handle=(DSO_handle*) ptr;
306 return !dlclose(handle->handle);