comparison loader/module.c @ 1:3b5f5d1c5041

Initial revision
author arpi_esp
date Sat, 24 Feb 2001 20:28:24 +0000
parents
children 28091b3caff9
comparison
equal deleted inserted replaced
0:c1bb2c071d63 1:3b5f5d1c5041
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>
18 /*
19 #ifdef __linux__
20 #include <asm/unistd.h>
21 #include <asm/ldt.h>
22 #else
23 #define LDT_ENTRIES 8192
24 #define LDT_ENTRY_SIZE 8
25
26 struct modify_ldt_ldt_s {
27 unsigned int entry_number;
28 unsigned long base_addr;
29 unsigned int limit;
30 unsigned int seg_32bit:1;
31 unsigned int contents:2;
32 unsigned int read_exec_only:1;
33 unsigned int limit_in_pages:1;
34 unsigned int seg_not_present:1;
35 unsigned int useable:1;
36 };
37
38 #define MODIFY_LDT_CONTENTS_DATA 0
39 #define MODIFY_LDT_CONTENTS_STACK 1
40 #define MODIFY_LDT_CONTENTS_CODE 2
41 #define __NR_modify_ldt 123
42 #endif
43
44 */
45 #include <wine/windef.h>
46 #include <wine/winerror.h>
47 #include <wine/heap.h>
48 #include <wine/module.h>
49 #include <wine/pe_image.h>
50 #include <wine/debugtools.h>
51
52 struct modref_list_t;
53
54 typedef struct modref_list_t
55 {
56 WINE_MODREF* wm;
57 struct modref_list_t *next;
58 struct modref_list_t *prev;
59 }
60 modref_list;
61
62
63 /***********************************************************************
64 * LDT_EntryToBytes
65 *
66 * Convert an ldt_entry structure to the raw bytes of the descriptor.
67 */
68 /*static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content )
69 {
70 *buffer++ = ((content->base_addr & 0x0000ffff) << 16) |
71 (content->limit & 0x0ffff);
72 *buffer = (content->base_addr & 0xff000000) |
73 ((content->base_addr & 0x00ff0000)>>16) |
74 (content->limit & 0xf0000) |
75 (content->contents << 10) |
76 ((content->read_exec_only == 0) << 9) |
77 ((content->seg_32bit != 0) << 22) |
78 ((content->limit_in_pages != 0) << 23) |
79 0xf000;
80 }
81 */
82
83 //
84 // funcs:
85 //
86 // 0 read LDT
87 // 1 write old mode
88 // 0x11 write
89 //
90 /*
91 static int modify_ldt( int func, struct modify_ldt_ldt_s *ptr,
92 unsigned long count )
93 {
94 int res;
95 #ifdef __PIC__
96 __asm__ __volatile__( "pushl %%ebx\n\t"
97 "movl %2,%%ebx\n\t"
98 "int $0x80\n\t"
99 "popl %%ebx"
100 : "=a" (res)
101 : "0" (__NR_modify_ldt),
102 "r" (func),
103 "c" (ptr),
104 "d" (sizeof(struct modify_ldt_ldt_s)*count) );
105 #else
106 __asm__ __volatile__("int $0x80"
107 : "=a" (res)
108 : "0" (__NR_modify_ldt),
109 "b" (func),
110 "c" (ptr),
111 "d" (sizeof(struct modify_ldt_ldt_s)*count) );
112 #endif
113 if (res >= 0) return res;
114 errno = -res;
115 return -1;
116 }
117 static int fs_installed=0;
118 static char* fs_seg=0;
119 static int install_fs()
120 {
121 struct modify_ldt_ldt_s array;
122 int fd;
123 int ret;
124 void* prev_struct;
125
126 if(fs_installed)
127 return 0;
128
129 fd=open("/dev/zero", O_RDWR);
130 fs_seg=mmap((void*)0xbf000000, 0x30000, PROT_READ | PROT_WRITE, MAP_PRIVATE,
131 fd, 0);
132 if(fs_seg==0)
133 {
134 printf("ERROR: Couldn't allocate memory for fs segment\n");
135 return -1;
136 }
137 array.base_addr=((int)fs_seg+0xffff) & 0xffff0000;
138 array.entry_number=0x1;
139 array.limit=array.base_addr+getpagesize()-1;
140 array.seg_32bit=1;
141 array.read_exec_only=0;
142 array.seg_not_present=0;
143 array.contents=MODIFY_LDT_CONTENTS_DATA;
144 array.limit_in_pages=0;
145 #ifdef linux
146 ret=modify_ldt(0x1, &array, 1);
147 if(ret<0)
148 {
149 perror("install_fs");
150 MESSAGE("Couldn't install fs segment, expect segfault\n");
151 }
152 #endif
153
154 #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__)
155 {
156 long d[2];
157
158 LDT_EntryToBytes( d, &array );
159 ret = i386_set_ldt(0x1, (union descriptor *)d, 1);
160 if (ret < 0)
161 {
162 perror("install_fs");
163 MESSAGE("Did you reconfigure the kernel with \"options USER_LDT\"?\n");
164 }
165 }
166 #endif
167 __asm__
168 (
169 "movl $0xf,%eax\n\t"
170 // "pushw %ax\n\t"
171 "movw %ax, %fs\n\t"
172 );
173 prev_struct=malloc(8);
174 *(void**)array.base_addr=prev_struct;
175 printf("prev_struct: 0x%X\n", prev_struct);
176 close(fd);
177
178 fs_installed=1;
179 return 0;
180 };
181 static int uninstall_fs()
182 {
183 printf("Uninstalling FS segment\n");
184 if(fs_seg==0)
185 return -1;
186 munmap(fs_seg, 0x30000);
187 fs_installed=0;
188 return 0;
189 }
190
191 */
192 //WINE_MODREF *local_wm=NULL;
193 modref_list* local_wm=NULL;
194
195 WINE_MODREF *MODULE_FindModule(LPCSTR m)
196 {
197 modref_list* list=local_wm;
198 TRACE("Module %s request\n", m);
199 if(list==NULL)
200 return NULL;
201 while(strcmp(m, list->wm->filename))
202 {
203 // printf("%s: %x\n", list->wm->filename, list->wm->module);
204 list=list->prev;
205 if(list==NULL)
206 return NULL;
207 }
208 TRACE("Resolved to %s\n", list->wm->filename);
209 return list->wm;
210 }
211
212 void MODULE_RemoveFromList(WINE_MODREF *mod)
213 {
214 modref_list* list=local_wm;
215 if(list==0)
216 return;
217 if(mod==0)
218 return;
219 if((list->prev==NULL)&&(list->next==NULL))
220 {
221 free(list);
222 local_wm=NULL;
223 // uninstall_fs();
224 return;
225 }
226 for(;list;list=list->prev)
227 {
228 if(list->wm==mod)
229 {
230 if(list->prev)
231 list->prev->next=list->next;
232 if(list->next)
233 list->next->prev=list->prev;
234 if(list==local_wm)
235 local_wm=list->prev;
236 free(list);
237 return;
238 }
239 }
240 }
241
242 WINE_MODREF *MODULE32_LookupHMODULE(HMODULE m)
243 {
244 modref_list* list=local_wm;
245 TRACE("Module %X request\n", m);
246 if(list==NULL)
247 return NULL;
248 while(m!=list->wm->module)
249 {
250 // printf("Checking list %X wm %X module %X\n",
251 // list, list->wm, list->wm->module);
252 list=list->prev;
253 if(list==NULL)
254 return NULL;
255 }
256 TRACE("LookupHMODULE hit %X\n", list->wm);
257 return list->wm;
258 }
259
260 /*************************************************************************
261 * MODULE_InitDll
262 */
263 static WIN_BOOL MODULE_InitDll( WINE_MODREF *wm, DWORD type, LPVOID lpReserved )
264 {
265 WIN_BOOL retv = TRUE;
266
267 static LPCSTR typeName[] = { "PROCESS_DETACH", "PROCESS_ATTACH",
268 "THREAD_ATTACH", "THREAD_DETACH" };
269 assert( wm );
270
271
272 /* Skip calls for modules loaded with special load flags */
273
274 if ( ( wm->flags & WINE_MODREF_DONT_RESOLVE_REFS )
275 || ( wm->flags & WINE_MODREF_LOAD_AS_DATAFILE ) )
276 return TRUE;
277
278
279 TRACE("(%s,%s,%p) - CALL\n", wm->modname, typeName[type], lpReserved );
280
281 /* Call the initialization routine */
282 switch ( wm->type )
283 {
284 case MODULE32_PE:
285 retv = PE_InitDLL( wm, type, lpReserved );
286 break;
287
288 case MODULE32_ELF:
289 /* no need to do that, dlopen() already does */
290 break;
291
292 default:
293 ERR("wine_modref type %d not handled.\n", wm->type );
294 retv = FALSE;
295 break;
296 }
297
298 /* The state of the module list may have changed due to the call
299 to PE_InitDLL. We cannot assume that this module has not been
300 deleted. */
301 TRACE("(%p,%s,%p) - RETURN %d\n", wm, typeName[type], lpReserved, retv );
302
303 return retv;
304 }
305
306 /*************************************************************************
307 * MODULE_DllProcessAttach
308 *
309 * Send the process attach notification to all DLLs the given module
310 * depends on (recursively). This is somewhat complicated due to the fact that
311 *
312 * - we have to respect the module dependencies, i.e. modules implicitly
313 * referenced by another module have to be initialized before the module
314 * itself can be initialized
315 *
316 * - the initialization routine of a DLL can itself call LoadLibrary,
317 * thereby introducing a whole new set of dependencies (even involving
318 * the 'old' modules) at any time during the whole process
319 *
320 * (Note that this routine can be recursively entered not only directly
321 * from itself, but also via LoadLibrary from one of the called initialization
322 * routines.)
323 *
324 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
325 * the process *detach* notifications to be sent in the correct order.
326 * This must not only take into account module dependencies, but also
327 * 'hidden' dependencies created by modules calling LoadLibrary in their
328 * attach notification routine.
329 *
330 * The strategy is rather simple: we move a WINE_MODREF to the head of the
331 * list after the attach notification has returned. This implies that the
332 * detach notifications are called in the reverse of the sequence the attach
333 * notifications *returned*.
334 *
335 * NOTE: Assumes that the process critical section is held!
336 *
337 */
338 WIN_BOOL MODULE_DllProcessAttach( WINE_MODREF *wm, LPVOID lpReserved )
339 {
340 WIN_BOOL retv = TRUE;
341 int i;
342 assert( wm );
343
344 /* prevent infinite recursion in case of cyclical dependencies */
345 if ( ( wm->flags & WINE_MODREF_MARKER )
346 || ( wm->flags & WINE_MODREF_PROCESS_ATTACHED ) )
347 return retv;
348
349 TRACE("(%s,%p) - START\n", wm->modname, lpReserved );
350
351 /* Tag current MODREF to prevent recursive loop */
352 wm->flags |= WINE_MODREF_MARKER;
353
354 /* Recursively attach all DLLs this one depends on */
355 /* for ( i = 0; retv && i < wm->nDeps; i++ )
356 if ( wm->deps[i] )
357 retv = MODULE_DllProcessAttach( wm->deps[i], lpReserved );
358 */
359 /* Call DLL entry point */
360
361 //local_wm=wm;
362 if(local_wm)
363 {
364 local_wm->next=malloc(sizeof(modref_list));
365 local_wm->next->prev=local_wm;
366 local_wm->next->next=NULL;
367 local_wm->next->wm=wm;
368 local_wm=local_wm->next;
369 }
370 else
371 {
372 local_wm=malloc(sizeof(modref_list));
373 local_wm->next=local_wm->prev=NULL;
374 local_wm->wm=wm;
375 }
376 /* Remove recursion flag */
377 wm->flags &= ~WINE_MODREF_MARKER;
378
379 if ( retv )
380 {
381 retv = MODULE_InitDll( wm, DLL_PROCESS_ATTACH, lpReserved );
382 if ( retv )
383 wm->flags |= WINE_MODREF_PROCESS_ATTACHED;
384 }
385
386
387 TRACE("(%s,%p) - END\n", wm->modname, lpReserved );
388
389 return retv;
390 }
391
392 /*************************************************************************
393 * MODULE_DllProcessDetach
394 *
395 * Send DLL process detach notifications. See the comment about calling
396 * sequence at MODULE_DllProcessAttach. Unless the bForceDetach flag
397 * is set, only DLLs with zero refcount are notified.
398 */
399 void MODULE_DllProcessDetach( WINE_MODREF* wm, WIN_BOOL bForceDetach, LPVOID lpReserved )
400 {
401 // WINE_MODREF *wm=local_wm;
402 wm->flags &= ~WINE_MODREF_PROCESS_ATTACHED;
403 MODULE_InitDll( wm, DLL_PROCESS_DETACH, lpReserved );
404 }
405
406
407 /***********************************************************************
408 * LoadLibraryExA (KERNEL32)
409 */
410 HMODULE WINAPI LoadLibraryExA(LPCSTR libname, HANDLE hfile, DWORD flags)
411 {
412 WINE_MODREF *wm;
413
414 if(!libname)
415 {
416 SetLastError(ERROR_INVALID_PARAMETER);
417 return 0;
418 }
419 // if(fs_installed==0)
420 // install_fs();
421
422
423 wm = MODULE_LoadLibraryExA( libname, hfile, flags );
424 if ( wm )
425 {
426 if ( !MODULE_DllProcessAttach( wm, NULL ) )
427 {
428 WARN_(module)("Attach failed for module '%s', \n", libname);
429 MODULE_FreeLibrary(wm);
430 SetLastError(ERROR_DLL_INIT_FAILED);
431 MODULE_RemoveFromList(wm);
432 wm = NULL;
433 }
434 }
435
436 return wm ? wm->module : 0;
437 }
438
439
440 /***********************************************************************
441 * MODULE_LoadLibraryExA (internal)
442 *
443 * Load a PE style module according to the load order.
444 *
445 * The HFILE parameter is not used and marked reserved in the SDK. I can
446 * only guess that it should force a file to be mapped, but I rather
447 * ignore the parameter because it would be extremely difficult to
448 * integrate this with different types of module represenations.
449 *
450 */
451 WINE_MODREF *MODULE_LoadLibraryExA( LPCSTR libname, HFILE hfile, DWORD flags )
452 {
453 DWORD err = GetLastError();
454 WINE_MODREF *pwm;
455 int i;
456 // module_loadorder_t *plo;
457
458
459 SetLastError( ERROR_FILE_NOT_FOUND );
460 TRACE("Trying native dll '%s'\n", libname);
461 pwm = PE_LoadLibraryExA(libname, flags);
462 #ifdef HAVE_LIBDL
463 if(!pwm)
464 {
465 TRACE("Trying ELF dll '%s'\n", libname);
466 pwm=(WINE_MODREF*)ELFDLL_LoadLibraryExA(libname, flags);
467 }
468 #endif
469 // printf("0x%08x\n", pwm);
470 // break;
471 if(pwm)
472 {
473 /* Initialize DLL just loaded */
474 TRACE("Loaded module '%s' at 0x%08x, \n", libname, pwm->module);
475 /* Set the refCount here so that an attach failure will */
476 /* decrement the dependencies through the MODULE_FreeLibrary call. */
477 pwm->refCount++;
478
479 SetLastError( err ); /* restore last error */
480 return pwm;
481 }
482
483
484 WARN("Failed to load module '%s'; error=0x%08lx, \n", libname, GetLastError());
485 return NULL;
486 }
487
488 /***********************************************************************
489 * LoadLibraryA (KERNEL32)
490 */
491 HMODULE WINAPI LoadLibraryA(LPCSTR libname) {
492 return LoadLibraryExA(libname,0,0);
493 }
494
495
496 /***********************************************************************
497 * FreeLibrary
498 */
499 WIN_BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
500 {
501 WIN_BOOL retv = FALSE;
502 WINE_MODREF *wm;
503
504 wm=MODULE32_LookupHMODULE(hLibModule);
505 // wm=local_wm;
506
507 if ( !wm || !hLibModule )
508 {
509 SetLastError( ERROR_INVALID_HANDLE );
510 return 0;
511 }
512 else
513 retv = MODULE_FreeLibrary( wm );
514
515 MODULE_RemoveFromList(wm);
516
517 return retv;
518 }
519
520 /***********************************************************************
521 * MODULE_DecRefCount
522 *
523 * NOTE: Assumes that the process critical section is held!
524 */
525 static void MODULE_DecRefCount( WINE_MODREF *wm )
526 {
527 int i;
528
529 if ( wm->flags & WINE_MODREF_MARKER )
530 return;
531
532 if ( wm->refCount <= 0 )
533 return;
534
535 --wm->refCount;
536 TRACE("(%s) refCount: %d\n", wm->modname, wm->refCount );
537
538 if ( wm->refCount == 0 )
539 {
540 wm->flags |= WINE_MODREF_MARKER;
541
542 for ( i = 0; i < wm->nDeps; i++ )
543 if ( wm->deps[i] )
544 MODULE_DecRefCount( wm->deps[i] );
545
546 wm->flags &= ~WINE_MODREF_MARKER;
547 }
548 }
549
550 /***********************************************************************
551 * MODULE_FreeLibrary
552 *
553 * NOTE: Assumes that the process critical section is held!
554 */
555 WIN_BOOL MODULE_FreeLibrary( WINE_MODREF *wm )
556 {
557 TRACE("(%s) - START\n", wm->modname );
558
559 /* Recursively decrement reference counts */
560 //MODULE_DecRefCount( wm );
561
562 /* Call process detach notifications */
563 MODULE_DllProcessDetach( wm, FALSE, NULL );
564
565 PE_UnloadLibrary(wm);
566
567 TRACE("END\n");
568
569 return TRUE;
570 }
571
572 /***********************************************************************
573 * GetProcAddress (KERNEL32.257)
574 */
575 FARPROC WINAPI GetProcAddress( HMODULE hModule, LPCSTR function )
576 {
577 return MODULE_GetProcAddress( hModule, function, TRUE );
578 }
579
580 /***********************************************************************
581 * MODULE_GetProcAddress (internal)
582 */
583 FARPROC MODULE_GetProcAddress(
584 HMODULE hModule, /* [in] current module handle */
585 LPCSTR function, /* [in] function to be looked up */
586 WIN_BOOL snoop )
587 {
588 WINE_MODREF *wm = MODULE32_LookupHMODULE( hModule );
589 // WINE_MODREF *wm=local_wm;
590 FARPROC retproc;
591
592 if (HIWORD(function))
593 TRACE_(win32)("(%08lx,%s)\n",(DWORD)hModule,function);
594 else
595 TRACE_(win32)("(%08lx,%p)\n",(DWORD)hModule,function);
596 if (!wm) {
597 SetLastError(ERROR_INVALID_HANDLE);
598 return (FARPROC)0;
599 }
600 switch (wm->type)
601 {
602 case MODULE32_PE:
603 retproc = PE_FindExportedFunction( wm, function, snoop );
604 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
605 return retproc;
606 #ifdef HAVE_LIBDL
607 case MODULE32_ELF:
608 retproc = (FARPROC) dlsym( wm->module, function);
609 if (!retproc) SetLastError(ERROR_PROC_NOT_FOUND);
610 return retproc;
611 #endif
612 default:
613 ERR("wine_modref type %d not handled.\n",wm->type);
614 SetLastError(ERROR_INVALID_HANDLE);
615 return (FARPROC)0;
616 }
617 }
618