Mercurial > mplayer.hg
annotate loader/module.c @ 1808:e37f7103df91
endless loop (bad/missing file) fixed
author | arpi |
---|---|
date | Fri, 31 Aug 2001 20:59:41 +0000 |
parents | d8c1b0b38edc |
children | ce45cce7f7a5 |
rev | line source |
---|---|
1 | 1 /* |
2 * Modules | |
3 * | |
4 * Copyright 1995 Alexandre Julliard | |
5 */ | |
6 #include <config.h> | |
7 | |
8 #include <assert.h> | |
9 #include <errno.h> | |
10 #include <fcntl.h> | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <string.h> | |
14 #include <unistd.h> | |
15 | |
16 #include <sys/mman.h> | |
17 #include <sys/types.h> | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
18 #ifdef HAVE_LIBDL |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
19 #include <wine/elfdll.h> |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
20 #include <dlfcn.h> |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
21 #endif |
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
22 |
1 | 23 /* |
24 #ifdef __linux__ | |
25 #include <asm/unistd.h> | |
26 #include <asm/ldt.h> | |
27 #else | |
28 #define LDT_ENTRIES 8192 | |
29 #define LDT_ENTRY_SIZE 8 | |
30 | |
31 struct modify_ldt_ldt_s { | |
32 unsigned int entry_number; | |
33 unsigned long base_addr; | |
34 unsigned int limit; | |
35 unsigned int seg_32bit:1; | |
36 unsigned int contents:2; | |
37 unsigned int read_exec_only:1; | |
38 unsigned int limit_in_pages:1; | |
39 unsigned int seg_not_present:1; | |
40 unsigned int useable:1; | |
41 }; | |
42 | |
43 #define MODIFY_LDT_CONTENTS_DATA 0 | |
44 #define MODIFY_LDT_CONTENTS_STACK 1 | |
45 #define MODIFY_LDT_CONTENTS_CODE 2 | |
46 #define __NR_modify_ldt 123 | |
47 #endif | |
48 | |
49 */ | |
50 #include <wine/windef.h> | |
51 #include <wine/winerror.h> | |
52 #include <wine/heap.h> | |
53 #include <wine/module.h> | |
54 #include <wine/pe_image.h> | |
55 #include <wine/debugtools.h> | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
56 #include "win32.h" |
1 | 57 |
58 struct modref_list_t; | |
59 | |
60 typedef struct modref_list_t | |
61 { | |
62 WINE_MODREF* wm; | |
63 struct modref_list_t *next; | |
64 struct modref_list_t *prev; | |
65 } | |
66 modref_list; | |
67 | |
68 //WINE_MODREF *local_wm=NULL; | |
69 modref_list* local_wm=NULL; | |
70 | |
71 WINE_MODREF *MODULE_FindModule(LPCSTR m) | |
72 { | |
73 modref_list* list=local_wm; | |
74 TRACE("Module %s request\n", m); | |
75 if(list==NULL) | |
76 return NULL; | |
77 while(strcmp(m, list->wm->filename)) | |
78 { | |
128 | 79 TRACE("%s: %x\n", list->wm->filename, list->wm->module); |
1 | 80 list=list->prev; |
81 if(list==NULL) | |
82 return NULL; | |
83 } | |
84 TRACE("Resolved to %s\n", list->wm->filename); | |
85 return list->wm; | |
86 } | |
87 | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
88 static void MODULE_RemoveFromList(WINE_MODREF *mod) |
1 | 89 { |
90 modref_list* list=local_wm; | |
91 if(list==0) | |
92 return; | |
93 if(mod==0) | |
94 return; | |
95 if((list->prev==NULL)&&(list->next==NULL)) | |
96 { | |
97 free(list); | |
98 local_wm=NULL; | |
99 // uninstall_fs(); | |
100 return; | |
101 } | |
102 for(;list;list=list->prev) | |
103 { | |
104 if(list->wm==mod) | |
105 { | |
106 if(list->prev) | |
107 list->prev->next=list->next; | |
108 if(list->next) | |
109 list->next->prev=list->prev; | |
110 if(list==local_wm) | |
111 local_wm=list->prev; | |
112 free(list); | |
113 return; | |
114 } | |
115 } | |
116 } | |
117 | |
118 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m) | |
119 { | |
120 modref_list* list=local_wm; | |
121 TRACE("Module %X request\n", m); | |
122 if(list==NULL) | |
123 return NULL; | |
124 while(m!=list->wm->module) | |
125 { | |
126 // printf("Checking list %X wm %X module %X\n", | |
127 // list, list->wm, list->wm->module); | |
128 list=list->prev; | |
129 if(list==NULL) | |
130 return NULL; | |
131 } | |
132 TRACE("LookupHMODULE hit %X\n", list->wm); | |
133 return list->wm; | |
134 } | |
135 | |
136 /************************************************************************* | |
137 * MODULE_InitDll | |
138 */ | |
139 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved ) | |
140 { | |
141 WIN_BOOL retv = TRUE; | |
142 | |
143 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH", | |
144 "THREAD_ATTACH", "THREAD_DETACH" }; | |
145 assert( wm ); | |
146 | |
147 | |
148 /* Skip calls for modules loaded with special load flags */ | |
149 | |
150 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS ) | |
151 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) ) | |
152 return TRUE; | |
153 | |
154 | |
155 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved ); | |
156 | |
157 /* Call the initialization routine */ | |
158 switch ( wm->type ) | |
159 { | |
160 case MODULE32_PE: | |
161 retv = PE_InitDLL( wm, type, lpReserved ); | |
162 break; | |
163 | |
164 case MODULE32_ELF: | |
165 /* no need to do that, dlopen() already does */ | |
166 break; | |
167 | |
168 default: | |
169 ERR("wine_modref type %d not handled.\n", wm->type ); | |
170 retv = FALSE; | |
171 break; | |
172 } | |
173 | |
174 /* The state of the module list may have changed due to the call | |
175 to PE_InitDLL. We cannot assume that this module has not been | |
176 deleted. */ | |
177 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv ); | |
178 | |
179 return retv; | |
180 } | |
181 | |
182 /************************************************************************* | |
183 * MODULE_DllProcessAttach | |
184 * | |
185 * Send the process attach notification to all DLLs the given module | |
186 * depends on (recursively). This is somewhat complicated due to the fact that | |
187 * | |
188 * - we have to respect the module dependencies, i.e. modules implicitly | |
189 * referenced by another module have to be initialized before the module | |
190 * itself can be initialized | |
191 * | |
192 * - the initialization routine of a DLL can itself call LoadLibrary, | |
193 * thereby introducing a whole new set of dependencies (even involving | |
194 * the 'old' modules) at any time during the whole process | |
195 * | |
196 * (Note that this routine can be recursively entered not only directly | |
197 * from itself, but also via LoadLibrary from one of the called initialization | |
198 * routines.) | |
199 * | |
200 * Furthermore, we need to rearrange the main WINE_MODREF list to allow | |
201 * the process *detach* notifications to be sent in the correct order. | |
202 * This must not only take into account module dependencies, but also | |
203 * 'hidden' dependencies created by modules calling LoadLibrary in their | |
204 * attach notification routine. | |
205 * | |
206 * The strategy is rather simple: we move a WINE_MODREF to the head of the | |
207 * list after the attach notification has returned. This implies that the | |
208 * detach notifications are called in the reverse of the sequence the attach | |
209 * notifications *returned*. | |
210 * | |
211 * NOTE: Assumes that the process critical section is held! | |
212 * | |
213 */ | |
214 WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved ) | |
215 { | |
216 WIN_BOOL retv = TRUE; | |
217 int i; | |
218 assert( wm ); | |
219 | |
220 /* prevent infinite recursion in case of cyclical dependencies */ | |
221 if ( ( wm->flags & WINE_MODREF_MARKER ) | |
222 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) ) | |
223 return retv; | |
224 | |
225 TRACE("(%s,%p) - START\n", wm->modname, lpReserved ); | |
226 | |
227 /* Tag current MODREF to prevent recursive loop */ | |
228 wm->flags |= WINE_MODREF_MARKER; | |
229 | |
230 /* Recursively attach all DLLs this one depends on */ | |
231 /* for ( i = 0; retv && i < wm->nDeps; i++ ) | |
232 if ( wm->deps[i] ) | |
233 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved ); | |
234 */ | |
235 /* Call DLL entry point */ | |
236 | |
237 //local_wm=wm; | |
238 if(local_wm) | |
239 { | |
240 local_wm->next=malloc(sizeof(modref_list)); | |
241 local_wm->next->prev=local_wm; | |
242 local_wm->next->next=NULL; | |
243 local_wm->next->wm=wm; | |
244 local_wm=local_wm->next; | |
245 } | |
246 else | |
247 { | |
248 local_wm=malloc(sizeof(modref_list)); | |
249 local_wm->next=local_wm->prev=NULL; | |
250 local_wm->wm=wm; | |
251 } | |
252 /* Remove recursion flag */ | |
253 wm->flags &= ~WINE_MODREF_MARKER; | |
254 | |
255 if ( retv ) | |
256 { | |
257 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved ); | |
258 if ( retv ) | |
259 wm->flags |= WINE_MODREF_PROCESS_ATTACHED; | |
260 } | |
261 | |
262 | |
263 TRACE("(%s,%p) - END\n", wm->modname, lpReserved ); | |
264 | |
265 return retv; | |
266 } | |
267 | |
268 /************************************************************************* | |
269 * MODULE_DllProcessDetach | |
270 * | |
271 * Send DLL process detach notifications. See the comment about calling | |
272 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag | |
273 * is set, only DLLs with zero refcount are notified. | |
274 */ | |
275 void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved ) | |
276 { | |
277 // WINE_MODREF *wm=local_wm; | |
278 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED; | |
279 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved ); | |
280 } | |
281 | |
282 | |
283 /*********************************************************************** | |
284 * LoadLibraryExA (KERNEL32) | |
285 */ | |
286 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags) | |
287 { | |
288 WINE_MODREF *wm; | |
289 | |
290 if(!libname) | |
291 { | |
292 SetLastError(ERROR_INVALID_PARAMETER); | |
293 return 0; | |
294 } | |
295 // if(fs_installed==0) | |
296 // install_fs(); | |
297 | |
298 | |
299 wm = MODULE_LoadLibraryExA( libname, hfile, flags ); | |
300 if ( wm ) | |
301 { | |
302 if ( !MODULE_DllProcessAttach( wm, NULL ) ) | |
303 { | |
304 WARN_(module)("Attach failed for module '%s', \n", libname); | |
305 MODULE_FreeLibrary(wm); | |
306 SetLastError(ERROR_DLL_INIT_FAILED); | |
307 MODULE_RemoveFromList(wm); | |
308 wm = NULL; | |
309 } | |
310 } | |
311 | |
312 return wm ? wm->module : 0; | |
313 } | |
314 | |
315 | |
316 /*********************************************************************** | |
317 * MODULE_LoadLibraryExA (internal) | |
318 * | |
319 * Load a PE style module according to the load order. | |
320 * | |
321 * The HFILE parameter is not used and marked reserved in the SDK. I can | |
322 * only guess that it should force a file to be mapped, but I rather | |
323 * ignore the parameter because it would be extremely difficult to | |
324 * integrate this with different types of module represenations. | |
325 * | |
326 */ | |
327 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags ) | |
328 { | |
329 DWORD err = GetLastError(); | |
330 WINE_MODREF *pwm; | |
331 int i; | |
332 // module_loadorder_t *plo; | |
333 | |
334 | |
335 SetLastError( ERROR_FILE_NOT_FOUND ); | |
336 TRACE("Trying native dll '%s'\n", libname); | |
337 pwm = PE_LoadLibraryExA(libname, flags); | |
338 #ifdef HAVE_LIBDL | |
339 if(!pwm) | |
340 { | |
341 TRACE("Trying ELF dll '%s'\n", libname); | |
342 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags); | |
343 } | |
344 #endif | |
345 // printf("0x%08x\n", pwm); | |
346 // break; | |
347 if(pwm) | |
348 { | |
349 /* Initialize DLL just loaded */ | |
350 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module); | |
351 /* Set the refCount here so that an attach failure will */ | |
352 /* decrement the dependencies through the MODULE_FreeLibrary call. */ | |
353 pwm->refCount++; | |
354 | |
355 SetLastError( err ); /* restore last error */ | |
356 return pwm; | |
357 } | |
358 | |
359 | |
360 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError()); | |
361 return NULL; | |
362 } | |
363 | |
364 /*********************************************************************** | |
365 * LoadLibraryA (KERNEL32) | |
366 */ | |
367 HMODULE WINAPI LoadLibraryA(LPCSTR libname) { | |
368 return LoadLibraryExA(libname,0,0); | |
369 } | |
370 | |
371 | |
372 /*********************************************************************** | |
373 * FreeLibrary | |
374 */ | |
375 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule) | |
376 { | |
377 WIN_BOOL retv = FALSE; | |
378 WINE_MODREF *wm; | |
379 | |
380 wm=MODULE32_LookupHMODULE(hLibModule); | |
381 // wm=local_wm; | |
382 | |
383 if ( !wm || !hLibModule ) | |
384 { | |
385 SetLastError( ERROR_INVALID_HANDLE ); | |
386 return 0; | |
387 } | |
388 else | |
389 retv = MODULE_FreeLibrary( wm ); | |
390 | |
391 MODULE_RemoveFromList(wm); | |
392 | |
128 | 393 /* garbage... */ |
394 if (local_wm == NULL) my_garbagecollection(); | |
395 | |
1 | 396 return retv; |
397 } | |
398 | |
399 /*********************************************************************** | |
400 * MODULE_DecRefCount | |
401 * | |
402 * NOTE: Assumes that the process critical section is held! | |
403 */ | |
404 static void MODULE_DecRefCount( WINE_MODREF *wm ) | |
405 { | |
406 int i; | |
407 | |
408 if ( wm->flags & WINE_MODREF_MARKER ) | |
409 return; | |
410 | |
411 if ( wm->refCount <= 0 ) | |
412 return; | |
413 | |
414 --wm->refCount; | |
415 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount ); | |
416 | |
417 if ( wm->refCount == 0 ) | |
418 { | |
419 wm->flags |= WINE_MODREF_MARKER; | |
420 | |
421 for ( i = 0; i < wm->nDeps; i++ ) | |
422 if ( wm->deps[i] ) | |
423 MODULE_DecRefCount( wm->deps[i] ); | |
424 | |
425 wm->flags &= ~WINE_MODREF_MARKER; | |
426 } | |
427 } | |
428 | |
429 /*********************************************************************** | |
430 * MODULE_FreeLibrary | |
431 * | |
432 * NOTE: Assumes that the process critical section is held! | |
433 */ | |
434 WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm ) | |
435 { | |
436 TRACE("(%s) - START\n", wm->modname ); | |
437 | |
438 /* Recursively decrement reference counts */ | |
439 //MODULE_DecRefCount( wm ); | |
440 | |
441 /* Call process detach notifications */ | |
442 MODULE_DllProcessDetach( wm, FALSE, NULL ); | |
443 | |
444 PE_UnloadLibrary(wm); | |
445 | |
446 TRACE("END\n"); | |
447 | |
448 return TRUE; | |
449 } | |
450 | |
451 /*********************************************************************** | |
452 * GetProcAddress (KERNEL32.257) | |
453 */ | |
454 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function ) | |
455 { | |
456 return MODULE_GetProcAddress( hModule, function, TRUE ); | |
457 } | |
458 | |
459 /*********************************************************************** | |
460 * MODULE_GetProcAddress (internal) | |
461 */ | |
462 FARPROC MODULE_GetProcAddress( | |
463 HMODULE hModule, /* [in] current module handle */ | |
464 LPCSTR function, /* [in] function to be looked up */ | |
465 WIN_BOOL snoop ) | |
466 { | |
467 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule ); | |
468 // WINE_MODREF *wm=local_wm; | |
469 FARPROC retproc; | |
470 | |
471 if (HIWORD(function)) | |
472 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function); | |
473 else | |
474 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function); | |
475 if (!wm) { | |
476 SetLastError(ERROR_INVALID_HANDLE); | |
477 return (FARPROC)0; | |
478 } | |
479 switch (wm->type) | |
480 { | |
481 case MODULE32_PE: | |
482 retproc = PE_FindExportedFunction( wm, function, snoop ); | |
483 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND); | |
484 return retproc; | |
485 #ifdef HAVE_LIBDL | |
486 case MODULE32_ELF: | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
487 retproc = (FARPROC) dlsym( (void*)wm->module, function); |
1 | 488 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND); |
489 return retproc; | |
490 #endif | |
491 default: | |
492 ERR("wine_modref type %d not handled.\n",wm->type); | |
493 SetLastError(ERROR_INVALID_HANDLE); | |
494 return (FARPROC)0; | |
495 } | |
496 } | |
497 |