Mercurial > mplayer.hg
annotate loader/module.c @ 2623:0d8f8d313f9c
fixed fault with --help
author | alex |
---|---|
date | Fri, 02 Nov 2001 02:21:57 +0000 |
parents | ce45cce7f7a5 |
children | 958d10763c34 |
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 #include <sys/mman.h> | |
16 #include <sys/types.h> | |
17 /* | |
18 #ifdef __linux__ | |
19 #include <asm/unistd.h> | |
20 #include <asm/ldt.h> | |
21 #else | |
22 #define LDT_ENTRIES 8192 | |
23 #define LDT_ENTRY_SIZE 8 | |
24 | |
25 struct modify_ldt_ldt_s { | |
26 unsigned int entry_number; | |
27 unsigned long base_addr; | |
28 unsigned int limit; | |
29 unsigned int seg_32bit:1; | |
30 unsigned int contents:2; | |
31 unsigned int read_exec_only:1; | |
32 unsigned int limit_in_pages:1; | |
33 unsigned int seg_not_present:1; | |
34 unsigned int useable:1; | |
35 }; | |
36 | |
37 #define MODIFY_LDT_CONTENTS_DATA 0 | |
38 #define MODIFY_LDT_CONTENTS_STACK 1 | |
39 #define MODIFY_LDT_CONTENTS_CODE 2 | |
40 #define __NR_modify_ldt 123 | |
41 #endif | |
42 | |
43 */ | |
44 #include <wine/windef.h> | |
45 #include <wine/winerror.h> | |
46 #include <wine/heap.h> | |
47 #include <wine/module.h> | |
48 #include <wine/pe_image.h> | |
49 #include <wine/debugtools.h> | |
2069 | 50 #ifdef HAVE_LIBDL |
51 #include <dlfcn.h> | |
52 #include <wine/elfdll.h> | |
53 #endif | |
1307
d8c1b0b38edc
Add prototypes to wine/loader stuff, so that we can catch __stdcall function
jkeil
parents:
128
diff
changeset
|
54 #include "win32.h" |
1 | 55 |
56 struct modref_list_t; | |
57 | |
58 typedef struct modref_list_t | |
59 { | |
60 WINE_MODREF* wm; | |
61 struct modref_list_t *next; | |
2069 | 62 struct modref_list_t *prev; |
1 | 63 } |
64 modref_list; | |
65 | |
66 //WINE_MODREF *local_wm=NULL; | |
67 modref_list* local_wm=NULL; | |
68 | |
2069 | 69 //HANDLE SegptrHeap; // unused? |
70 | |
1 | 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; | |
2069 | 83 } |
1 | 84 TRACE("Resolved to %s\n", list->wm->filename); |
85 return list->wm; | |
2069 | 86 } |
1 | 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 } | |
2069 | 116 } |
117 | |
1 | 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; | |
2069 | 131 } |
132 TRACE("LookupHMODULE hit %p\n", list->wm); | |
1 | 133 return list->wm; |
2069 | 134 } |
1 | 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 | |
2069 | 143 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH", |
1 | 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 | |
2069 | 184 * |
1 | 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 | |
2069 | 191 * |
1 | 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. | |
2069 | 202 * This must not only take into account module dependencies, but also |
1 | 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; | |
2069 | 251 } |
1 | 252 /* Remove recursion flag */ |
253 wm->flags &= ~WINE_MODREF_MARKER; | |
2069 | 254 |
1 | 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 | |
2069 | 270 * |
271 * Send DLL process detach notifications. See the comment about calling | |
1 | 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 { | |
2069 | 288 WINE_MODREF *wm = 0; |
289 char* listpath[] = { "", "", "/usr/lib/win32", "/usr/local/lib/win32", 0 }; | |
290 extern char* def_path; | |
291 char path[512]; | |
292 char checked[2000]; | |
293 int i = -1; | |
1 | 294 |
2069 | 295 checked[0] = 0; |
1 | 296 if(!libname) |
297 { | |
298 SetLastError(ERROR_INVALID_PARAMETER); | |
299 return 0; | |
300 } | |
2069 | 301 |
302 printf("Loading DLL: '%s'\n", libname); | |
303 | |
1 | 304 // if(fs_installed==0) |
305 // install_fs(); | |
2069 | 306 |
307 while (wm == 0 && listpath[++i]) | |
308 { | |
309 if (i < 2) | |
310 { | |
311 if (i == 0) | |
312 /* check just original file name */ | |
313 strncpy(path, libname, 511); | |
314 else | |
315 /* check default user path */ | |
316 strncpy(path, def_path, 300); | |
317 } | |
318 else if (strcmp(def_path, listpath[i])) | |
319 /* path from the list */ | |
320 strncpy(path, listpath[i], 300); | |
321 else | |
322 continue; | |
1 | 323 |
2069 | 324 if (i > 0) |
325 { | |
326 strcat(path, "/"); | |
327 strncat(path, libname, 100); | |
328 } | |
329 path[511] = 0; | |
330 wm = MODULE_LoadLibraryExA( path, hfile, flags ); | |
331 | |
332 if (!wm) | |
333 { | |
334 if (checked[0]) | |
335 strcat(checked, ", "); | |
336 strcat(checked, path); | |
337 checked[1500] = 0; | |
338 | |
339 } | |
340 } | |
1 | 341 if ( wm ) |
342 { | |
343 if ( !MODULE_DllProcessAttach( wm, NULL ) ) | |
344 { | |
345 WARN_(module)("Attach failed for module '%s', \n", libname); | |
346 MODULE_FreeLibrary(wm); | |
347 SetLastError(ERROR_DLL_INIT_FAILED); | |
348 MODULE_RemoveFromList(wm); | |
349 wm = NULL; | |
350 } | |
351 } | |
352 | |
2069 | 353 if (!wm) |
354 printf("Win32 LoadLibrary failed to load: %s\n", checked); | |
355 | |
356 | |
1 | 357 return wm ? wm->module : 0; |
358 } | |
359 | |
360 | |
361 /*********************************************************************** | |
362 * MODULE_LoadLibraryExA (internal) | |
363 * | |
364 * Load a PE style module according to the load order. | |
365 * | |
366 * The HFILE parameter is not used and marked reserved in the SDK. I can | |
367 * only guess that it should force a file to be mapped, but I rather | |
368 * ignore the parameter because it would be extremely difficult to | |
369 * integrate this with different types of module represenations. | |
370 * | |
371 */ | |
372 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags ) | |
373 { | |
374 DWORD err = GetLastError(); | |
375 WINE_MODREF *pwm; | |
376 int i; | |
377 // module_loadorder_t *plo; | |
378 | |
379 SetLastError( ERROR_FILE_NOT_FOUND ); | |
380 TRACE("Trying native dll '%s'\n", libname); | |
381 pwm = PE_LoadLibraryExA(libname, flags); | |
382 #ifdef HAVE_LIBDL | |
383 if(!pwm) | |
384 { | |
385 TRACE("Trying ELF dll '%s'\n", libname); | |
386 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags); | |
2069 | 387 } |
388 #endif | |
1 | 389 // printf("0x%08x\n", pwm); |
390 // break; | |
391 if(pwm) | |
392 { | |
393 /* Initialize DLL just loaded */ | |
394 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module); | |
395 /* Set the refCount here so that an attach failure will */ | |
396 /* decrement the dependencies through the MODULE_FreeLibrary call. */ | |
397 pwm->refCount++; | |
398 | |
399 SetLastError( err ); /* restore last error */ | |
400 return pwm; | |
401 } | |
402 | |
2069 | 403 |
1 | 404 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError()); |
405 return NULL; | |
406 } | |
407 | |
408 /*********************************************************************** | |
409 * LoadLibraryA (KERNEL32) | |
410 */ | |
411 HMODULE WINAPI LoadLibraryA(LPCSTR libname) { | |
412 return LoadLibraryExA(libname,0,0); | |
413 } | |
414 | |
415 | |
416 /*********************************************************************** | |
417 * FreeLibrary | |
418 */ | |
419 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule) | |
420 { | |
421 WIN_BOOL retv = FALSE; | |
422 WINE_MODREF *wm; | |
423 | |
424 wm=MODULE32_LookupHMODULE(hLibModule); | |
425 // wm=local_wm; | |
426 | |
427 if ( !wm || !hLibModule ) | |
428 { | |
429 SetLastError( ERROR_INVALID_HANDLE ); | |
430 return 0; | |
2069 | 431 } |
1 | 432 else |
433 retv = MODULE_FreeLibrary( wm ); | |
2069 | 434 |
1 | 435 MODULE_RemoveFromList(wm); |
436 | |
128 | 437 /* garbage... */ |
438 if (local_wm == NULL) my_garbagecollection(); | |
439 | |
1 | 440 return retv; |
441 } | |
442 | |
443 /*********************************************************************** | |
444 * MODULE_DecRefCount | |
445 * | |
446 * NOTE: Assumes that the process critical section is held! | |
447 */ | |
448 static void MODULE_DecRefCount( WINE_MODREF *wm ) | |
449 { | |
450 int i; | |
451 | |
452 if ( wm->flags & WINE_MODREF_MARKER ) | |
453 return; | |
454 | |
455 if ( wm->refCount <= 0 ) | |
456 return; | |
457 | |
458 --wm->refCount; | |
459 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount ); | |
460 | |
461 if ( wm->refCount == 0 ) | |
462 { | |
463 wm->flags |= WINE_MODREF_MARKER; | |
464 | |
465 for ( i = 0; i < wm->nDeps; i++ ) | |
466 if ( wm->deps[i] ) | |
467 MODULE_DecRefCount( wm->deps[i] ); | |
468 | |
469 wm->flags &= ~WINE_MODREF_MARKER; | |
470 } | |
471 } | |
472 | |
473 /*********************************************************************** | |
474 * MODULE_FreeLibrary | |
475 * | |
476 * NOTE: Assumes that the process critical section is held! | |
477 */ | |
478 WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm ) | |
479 { | |
480 TRACE("(%s) - START\n", wm->modname ); | |
481 | |
482 /* Recursively decrement reference counts */ | |
483 //MODULE_DecRefCount( wm ); | |
484 | |
485 /* Call process detach notifications */ | |
486 MODULE_DllProcessDetach( wm, FALSE, NULL ); | |
487 | |
488 PE_UnloadLibrary(wm); | |
489 | |
490 TRACE("END\n"); | |
491 | |
492 return TRUE; | |
493 } | |
494 | |
495 /*********************************************************************** | |
496 * GetProcAddress (KERNEL32.257) | |
497 */ | |
498 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function ) | |
499 { | |
500 return MODULE_GetProcAddress( hModule, function, TRUE ); | |
501 } | |
502 | |
503 /*********************************************************************** | |
504 * MODULE_GetProcAddress (internal) | |
505 */ | |
2069 | 506 FARPROC MODULE_GetProcAddress( |
1 | 507 HMODULE hModule, /* [in] current module handle */ |
508 LPCSTR function, /* [in] function to be looked up */ | |
509 WIN_BOOL snoop ) | |
510 { | |
511 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule ); | |
2069 | 512 // WINE_MODREF *wm=local_wm; |
1 | 513 FARPROC retproc; |
514 | |
515 if (HIWORD(function)) | |
516 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function); | |
517 else | |
518 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function); | |
519 if (!wm) { | |
520 SetLastError(ERROR_INVALID_HANDLE); | |
521 return (FARPROC)0; | |
522 } | |
523 switch (wm->type) | |
524 { | |
525 case MODULE32_PE: | |
526 retproc = PE_FindExportedFunction( wm, function, snoop ); | |
527 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND); | |
528 return retproc; | |
2069 | 529 #ifdef HAVE_LIBDL |
1 | 530 case MODULE32_ELF: |
2069 | 531 retproc = (FARPROC) dlsym( (void*) wm->module, function); |
1 | 532 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND); |
533 return retproc; | |
534 #endif | |
535 default: | |
536 ERR("wine_modref type %d not handled.\n",wm->type); | |
537 SetLastError(ERROR_INVALID_HANDLE); | |
538 return (FARPROC)0; | |
539 } | |
540 } | |
541 |