]> git.imager.perl.org - imager.git/blob - dynaload.c
more test entries
[imager.git] / dynaload.c
1 #include "dynaload.h"
2 /* #include "XSUB.h"  so we can compile on threaded perls */
3 #include "imagei.h"
4
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_line,i_arc,i_copyto,i_copyto_trans,i_rubthru};
9
10
11 /*
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.
17 */
18
19 void
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);
23 }
24
25
26 #if defined( OS_hpux )
27
28 void*
29 DSO_open(char* file,char** evalstring) {
30   shl_t tt_handle;
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 */
36   int i;
37
38   *evalstring=NULL;
39
40   mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
41
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;
44
45   /*
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;
50   */
51
52   if ( (shl_findsym(&tt_handle, I_INSTALL_TABLES ,TYPE_UNDEFINED, &f ))) return NULL; 
53  
54   mm_log( (1,"Calling install_tables\n") );
55   f(&symbol_table,&i_UTIL_table);
56   mm_log( (1,"Call ok.\n") ); 
57  
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;
60
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);
65
66   mm_log((1,"DSO_open <- (0x%X)\n",dso_handle));
67   return (void*)dso_handle;
68 }
69
70 undef_int
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));
75 }
76
77 #elif defined(WIN32)
78
79 void *
80 DSO_open(char *file, char **evalstring) {
81   HMODULE d_handle;
82   func_ptr *function_list;
83   DSO_handle *dso_handle;
84   
85   void (*f)(void *s,void *u); /* these will just have to be void for now */
86
87   mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
88
89   *evalstring = NULL;
90   if ((d_handle = LoadLibrary(file)) == NULL) {
91     mm_log((1, "DSO_open: LoadLibrary(%s) failed: %lu\n", file, GetLastError()));
92     return NULL;
93   }
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);
97     return NULL;
98   }
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);
102     return NULL;
103   }
104   mm_log((1, "Calling install tables\n"));
105   f(&symbol_table, &i_UTIL_table);
106   mm_log((1, "Call ok\n"));
107   
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);
111     return NULL;
112   }
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);
116     return NULL;
117   }
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);
122
123   mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
124   return (void*)dso_handle;
125
126 }
127
128 undef_int
129 DSO_close(void *ptr) {
130   DSO_handle *handle = (DSO_handle *)ptr;
131   BOOL result = FreeLibrary(handle->handle);
132   free(handle->filename);
133   free(handle);
134
135   return result;
136 }
137
138 #else
139
140 /* OS/2 has no dlclose; Perl doesn't provide one. */
141 #ifdef __EMX__ /* OS/2 */
142 int
143 dlclose(minthandle_t h) {
144   return DosFreeModule(h) ? -1 : 0;
145 }
146 #endif /* __EMX__ */
147
148 #ifdef OS_darwin
149
150 #import <mach-o/dyld.h>
151
152 static char *dl_error = "unknown";
153
154 static char *dlopen(char *path, int mode /* mode is ignored */)
155 {
156   int dyld_result;
157   NSObjectFileImage ofile;
158   NSModule handle = NULL;
159
160
161
162   dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
163   if (dyld_result != NSObjectFileImageSuccess)
164     {
165      switch (dyld_result) {
166        case NSObjectFileImageFailure:
167            dl_error = "object file setup failure";
168            break;
169        case NSObjectFileImageInappropriateFile:
170            dl_error = "not a Mach-O MH_BUNDLE file type";
171            break;
172        case NSObjectFileImageArch:
173            dl_error = "no object for this architecture";
174            break;
175        case NSObjectFileImageFormat:
176            dl_error = "bad object file format";
177            break;
178        case NSObjectFileImageAccess:
179            dl_error = "can't read object file";
180            break;
181        default:
182            dl_error = "unknown error from NSCreateObjectFileImageFromFile()";
183            break;
184      }
185     }
186     else
187       {
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);
191       }
192
193   return handle;
194 }
195
196 static void *
197 dlsym(void *handle, char *symbol)
198 {
199   void *addr;
200
201   if (NSIsSymbolNameDefined(symbol))
202   {
203     addr = NSAddressOfSymbol(NSLookupAndBindSymbol(symbol));
204   }
205   else
206   {
207     dl_error = "cannot find symbol";
208     addr = NULL;
209   }
210
211   return addr;
212 }
213
214 static int dlclose(void *handle) /* stub only */
215 {
216   return 0;
217 }
218
219 static char *dlerror(void) /* stub only */
220 {
221   printf("Error occurred\n");
222   return dl_error; 
223 }
224
225 #define RTLD_LAZY 0
226
227 #endif 
228
229 void*
230 DSO_open(char* file,char** evalstring) {
231   void *d_handle;
232   func_ptr *function_list;
233   DSO_handle *dso_handle;
234
235   void (*f)(void *s,void *u); /* these will just have to be void for now */
236   
237   *evalstring=NULL;
238
239   mm_log( (1,"DSO_open(file '%s' (0x%08X), evalstring 0x%08X)\n",file,file,evalstring) );
240
241   if ( (d_handle = dlopen(file, RTLD_LAZY)) == NULL) {
242     mm_log( (1,"DSO_open: dlopen failed: %s.\n",dlerror()) );
243     return NULL;
244   }
245
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()) );
248     return NULL;
249   }
250
251   /*
252
253     I'll just leave this thing in here for now if I need it real soon
254
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()) );
258      return NULL;
259    }
260   
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()) );
264      return NULL;
265    }
266
267   */
268
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()) );
273     return NULL;
274   }
275
276   mm_log( (1,"Calling install_tables\n") );
277   f(&symbol_table,&i_UTIL_table);
278   mm_log( (1,"Call ok.\n") );
279
280   /* (*plugin_symtab)=&symbol_table;
281      (*plugin_utiltab)=&i_UTIL_table; */
282   
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()) );
286     return NULL;
287   }
288   
289   if ( (dso_handle=(DSO_handle*)malloc(sizeof(DSO_handle))) == NULL) return NULL;
290   
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);
295
296   mm_log( (1,"DSO_open <- 0x%X\n",dso_handle) );
297   return (void*)dso_handle;
298 }
299
300 undef_int
301 DSO_close(void *ptr) {
302   DSO_handle *handle;
303   mm_log((1,"DSO_close(ptr 0x%X)\n",ptr));
304   handle=(DSO_handle*) ptr;
305   return !dlclose(handle->handle);
306 }
307
308 #endif
309