4 * Copyright 1995, 2003 Alexandre Julliard
5 * Copyright 2002 Dmitry Timoshkov for CodeWeavers
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include "wine/port.h"
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
38 #include "wine/exception.h"
40 #include "wine/library.h"
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43 #include "wine/server.h"
44 #include "ntdll_misc.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(module
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
48 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
49 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
50 WINE_DECLARE_DEBUG_CHANNEL(imports
);
52 typedef DWORD (CALLBACK
*DLLENTRYPROC
)(HMODULE
,DWORD
,LPVOID
);
54 static int process_detaching
= 0; /* set on process detach to avoid deadlocks with thread detach */
55 static int free_lib_count
; /* recursion depth of LdrUnloadDll calls */
57 /* filter for page-fault exceptions */
58 static WINE_EXCEPTION_FILTER(page_fault
)
60 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION
)
61 return EXCEPTION_EXECUTE_HANDLER
;
62 return EXCEPTION_CONTINUE_SEARCH
;
65 static const char * const reason_names
[] =
73 static const WCHAR dllW
[] = {'.','d','l','l',0};
75 /* internal representation of 32bit modules. per process. */
76 typedef struct _wine_modref
80 struct _wine_modref
**deps
;
83 /* info about the current builtin dll load */
84 /* used to keep track of things across the register_dll constructor call */
85 struct builtin_load_info
87 const WCHAR
*load_path
;
92 static struct builtin_load_info default_load_info
;
93 static struct builtin_load_info
*builtin_load_info
= &default_load_info
;
95 static UINT tls_module_count
; /* number of modules with TLS directory */
96 static UINT tls_total_size
; /* total size of TLS storage */
97 static const IMAGE_TLS_DIRECTORY
**tls_dirs
; /* array of TLS directories */
99 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
101 static CRITICAL_SECTION loader_section
;
102 static CRITICAL_SECTION_DEBUG critsect_debug
=
104 0, 0, &loader_section
,
105 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
106 0, 0, { 0, (DWORD
)(__FILE__
": loader_section") }
108 static CRITICAL_SECTION loader_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
110 static WINE_MODREF
*cached_modref
;
111 static WINE_MODREF
*current_modref
;
112 static WINE_MODREF
*last_failed_modref
;
114 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
);
115 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
116 DWORD exp_size
, const char *name
, int hint
);
118 /* convert PE image VirtualAddress to Real Address */
119 inline static void *get_rva( HMODULE module
, DWORD va
)
121 return (void *)((char *)module
+ va
);
124 /* check whether the file name contains a path */
125 inline static int contains_path( LPCWSTR name
)
127 return ((*name
&& (name
[1] == ':')) || strchrW(name
, '/') || strchrW(name
, '\\'));
130 /* convert from straight ASCII to Unicode without depending on the current codepage */
131 inline static void ascii_to_unicode( WCHAR
*dst
, const char *src
, size_t len
)
133 while (len
--) *dst
++ = (unsigned char)*src
++;
137 /*************************************************************************
138 * call_dll_entry_point
140 * Some brain-damaged dlls (ir32_32.dll for instance) modify ebx in
141 * their entry point, so we need a small asm wrapper.
144 extern BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
, UINT reason
, void *reserved
);
145 __ASM_GLOBAL_FUNC(call_dll_entry_point
,
152 "movl 8(%ebp),%eax\n\t"
154 "leal -4(%ebp),%esp\n\t"
159 static inline BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
,
160 UINT reason
, void *reserved
)
162 return proc( module
, reason
, reserved
);
164 #endif /* __i386__ */
168 /*************************************************************************
171 * Entry point for stub functions.
173 static void stub_entry_point( const char *dll
, const char *name
, ... )
175 EXCEPTION_RECORD rec
;
177 rec
.ExceptionCode
= EXCEPTION_WINE_STUB
;
178 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
179 rec
.ExceptionRecord
= NULL
;
181 rec
.ExceptionAddress
= __builtin_return_address(0);
183 rec
.ExceptionAddress
= *((void **)&dll
- 1);
185 rec
.NumberParameters
= 2;
186 rec
.ExceptionInformation
[0] = (ULONG_PTR
)dll
;
187 rec
.ExceptionInformation
[1] = (ULONG_PTR
)name
;
188 for (;;) RtlRaiseException( &rec
);
192 #include "pshpack1.h"
195 BYTE popl_eax
; /* popl %eax */
196 BYTE pushl1
; /* pushl $name */
198 BYTE pushl2
; /* pushl $dll */
200 BYTE pushl_eax
; /* pushl %eax */
201 BYTE jmp
; /* jmp stub_entry_point */
206 /*************************************************************************
209 * Allocate a stub entry point.
211 static void *allocate_stub( const char *dll
, const char *name
)
213 #define MAX_SIZE 65536
214 static struct stub
*stubs
;
215 static unsigned int nb_stubs
;
218 if (nb_stubs
>= MAX_SIZE
/ sizeof(*stub
)) return (void *)0xdeadbeef;
222 ULONG size
= MAX_SIZE
;
223 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs
, 0, &size
,
224 MEM_COMMIT
, PAGE_EXECUTE_WRITECOPY
) != STATUS_SUCCESS
)
225 return (void *)0xdeadbeef;
227 stub
= &stubs
[nb_stubs
++];
228 stub
->popl_eax
= 0x58; /* popl %eax */
229 stub
->pushl1
= 0x68; /* pushl $name */
231 stub
->pushl2
= 0x68; /* pushl $dll */
233 stub
->pushl_eax
= 0x50; /* pushl %eax */
234 stub
->jmp
= 0xe9; /* jmp stub_entry_point */
235 stub
->entry
= (BYTE
*)stub_entry_point
- (BYTE
*)(&stub
->entry
+ 1);
240 static inline void *allocate_stub( const char *dll
, const char *name
) { return (void *)0xdeadbeef; }
241 #endif /* __i386__ */
244 /*************************************************************************
247 * Looks for the referenced HMODULE in the current process
248 * The loader_section must be locked while calling this function.
250 static WINE_MODREF
*get_modref( HMODULE hmod
)
252 PLIST_ENTRY mark
, entry
;
255 if (cached_modref
&& cached_modref
->ldr
.BaseAddress
== hmod
) return cached_modref
;
257 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
258 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
260 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
261 if (mod
->BaseAddress
== hmod
)
262 return cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
263 if (mod
->BaseAddress
> (void*)hmod
) break;
269 /**********************************************************************
270 * find_basename_module
272 * Find a module from its base name.
273 * The loader_section must be locked while calling this function
275 static WINE_MODREF
*find_basename_module( LPCWSTR name
)
277 PLIST_ENTRY mark
, entry
;
279 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.BaseDllName
.Buffer
))
280 return cached_modref
;
282 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
283 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
285 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
286 if (!strcmpiW( name
, mod
->BaseDllName
.Buffer
))
288 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
289 return cached_modref
;
296 /**********************************************************************
297 * find_fullname_module
299 * Find a module from its full path name.
300 * The loader_section must be locked while calling this function
302 static WINE_MODREF
*find_fullname_module( LPCWSTR name
)
304 PLIST_ENTRY mark
, entry
;
306 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.FullDllName
.Buffer
))
307 return cached_modref
;
309 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
310 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
312 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
313 if (!strcmpiW( name
, mod
->FullDllName
.Buffer
))
315 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
316 return cached_modref
;
323 /*************************************************************************
324 * find_forwarded_export
326 * Find the final function pointer for a forwarded function.
327 * The loader_section must be locked while calling this function.
329 static FARPROC
find_forwarded_export( HMODULE module
, const char *forward
)
331 const IMAGE_EXPORT_DIRECTORY
*exports
;
335 const char *end
= strchr(forward
, '.');
338 if (!end
) return NULL
;
339 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
) - sizeof(dllW
)) return NULL
;
340 ascii_to_unicode( mod_name
, forward
, end
- forward
);
341 memcpy( mod_name
+ (end
- forward
), dllW
, sizeof(dllW
) );
343 if (!(wm
= find_basename_module( mod_name
)))
345 ERR("module not found for forward '%s' used by %s\n",
346 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
) );
349 if ((exports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
350 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
351 proc
= find_named_export( wm
->ldr
.BaseAddress
, exports
, exp_size
, end
+ 1, -1 );
355 ERR("function not found for forward '%s' used by %s."
356 " If you are using builtin %s, try using the native one instead.\n",
357 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
),
358 debugstr_w(get_modref(module
)->ldr
.BaseDllName
.Buffer
) );
364 /*************************************************************************
365 * find_ordinal_export
367 * Find an exported function by ordinal.
368 * The exports base must have been subtracted from the ordinal already.
369 * The loader_section must be locked while calling this function.
371 static FARPROC
find_ordinal_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
372 DWORD exp_size
, int ordinal
)
375 const DWORD
*functions
= get_rva( module
, exports
->AddressOfFunctions
);
377 if (ordinal
>= exports
->NumberOfFunctions
)
379 TRACE(" ordinal %ld out of range!\n", ordinal
+ exports
->Base
);
382 if (!functions
[ordinal
]) return NULL
;
384 proc
= get_rva( module
, functions
[ordinal
] );
386 /* if the address falls into the export dir, it's a forward */
387 if (((const char *)proc
>= (const char *)exports
) &&
388 ((const char *)proc
< (const char *)exports
+ exp_size
))
389 return find_forwarded_export( module
, (const char *)proc
);
393 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
394 proc
= SNOOP_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
398 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
399 proc
= RELAY_GetProcAddress( module
, exports
, exp_size
, proc
, user
);
405 /*************************************************************************
408 * Find an exported function by name.
409 * The loader_section must be locked while calling this function.
411 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
412 DWORD exp_size
, const char *name
, int hint
)
414 const WORD
*ordinals
= get_rva( module
, exports
->AddressOfNameOrdinals
);
415 const DWORD
*names
= get_rva( module
, exports
->AddressOfNames
);
416 int min
= 0, max
= exports
->NumberOfNames
- 1;
418 /* first check the hint */
419 if (hint
>= 0 && hint
<= max
)
421 char *ename
= get_rva( module
, names
[hint
] );
422 if (!strcmp( ename
, name
))
423 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[hint
] );
426 /* then do a binary search */
429 int res
, pos
= (min
+ max
) / 2;
430 char *ename
= get_rva( module
, names
[pos
] );
431 if (!(res
= strcmp( ename
, name
)))
432 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[pos
] );
433 if (res
> 0) max
= pos
- 1;
441 /*************************************************************************
444 * Import the dll specified by the given import descriptor.
445 * The loader_section must be locked while calling this function.
447 static WINE_MODREF
*import_dll( HMODULE module
, const IMAGE_IMPORT_DESCRIPTOR
*descr
, LPCWSTR load_path
)
452 const IMAGE_EXPORT_DIRECTORY
*exports
;
454 const IMAGE_THUNK_DATA
*import_list
;
455 IMAGE_THUNK_DATA
*thunk_list
;
457 const char *name
= get_rva( module
, descr
->Name
);
458 DWORD len
= strlen(name
) + 1;
460 DWORD protect_size
= 0;
463 thunk_list
= get_rva( module
, (DWORD
)descr
->FirstThunk
);
464 if (descr
->u
.OriginalFirstThunk
)
465 import_list
= get_rva( module
, (DWORD
)descr
->u
.OriginalFirstThunk
);
467 import_list
= thunk_list
;
469 if (len
* sizeof(WCHAR
) <= sizeof(buffer
))
471 ascii_to_unicode( buffer
, name
, len
);
472 status
= load_dll( load_path
, buffer
, 0, &wmImp
);
474 else /* need to allocate a larger buffer */
476 WCHAR
*ptr
= RtlAllocateHeap( GetProcessHeap(), 0, len
* sizeof(WCHAR
) );
477 if (!ptr
) return NULL
;
478 ascii_to_unicode( ptr
, name
, len
);
479 status
= load_dll( load_path
, ptr
, 0, &wmImp
);
480 RtlFreeHeap( GetProcessHeap(), 0, ptr
);
485 if (status
== STATUS_DLL_NOT_FOUND
)
486 ERR("Library %s (which is needed by %s) not found\n",
487 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
));
489 ERR("Loading library %s (which is needed by %s) failed (error %lx).\n",
490 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
), status
);
494 /* unprotect the import address table since it can be located in
495 * readonly section */
496 while (import_list
[protect_size
].u1
.Ordinal
) protect_size
++;
497 protect_base
= thunk_list
;
498 protect_size
*= sizeof(*thunk_list
);
499 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
,
500 &protect_size
, PAGE_WRITECOPY
, &protect_old
);
502 imp_mod
= wmImp
->ldr
.BaseAddress
;
503 exports
= RtlImageDirectoryEntryToData( imp_mod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
);
507 /* set all imported function to deadbeef */
508 while (import_list
->u1
.Ordinal
)
510 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
512 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
513 WARN("No implementation for %s.%d", name
, ordinal
);
514 thunk_list
->u1
.Function
= allocate_stub( name
, (const char *)ordinal
);
518 IMAGE_IMPORT_BY_NAME
*pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
519 WARN("No implementation for %s.%s", name
, pe_name
->Name
);
520 thunk_list
->u1
.Function
= allocate_stub( name
, pe_name
->Name
);
522 WARN(" imported from %s, allocating stub %p\n",
523 debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
524 thunk_list
->u1
.Function
);
531 while (import_list
->u1
.Ordinal
)
533 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
535 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
537 thunk_list
->u1
.Function
= (PDWORD
)find_ordinal_export( imp_mod
, exports
, exp_size
,
538 ordinal
- exports
->Base
);
539 if (!thunk_list
->u1
.Function
)
541 thunk_list
->u1
.Function
= allocate_stub( name
, (const char *)ordinal
);
542 WARN("No implementation for %s.%d imported from %s, setting to %p\n",
543 name
, ordinal
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
544 thunk_list
->u1
.Function
);
546 TRACE_(imports
)("--- Ordinal %s.%d = %p\n", name
, ordinal
, thunk_list
->u1
.Function
);
548 else /* import by name */
550 IMAGE_IMPORT_BY_NAME
*pe_name
;
551 pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
552 thunk_list
->u1
.Function
= (PDWORD
)find_named_export( imp_mod
, exports
, exp_size
,
553 pe_name
->Name
, pe_name
->Hint
);
554 if (!thunk_list
->u1
.Function
)
556 thunk_list
->u1
.Function
= allocate_stub( name
, pe_name
->Name
);
557 WARN("No implementation for %s.%s imported from %s, setting to %p\n",
558 name
, pe_name
->Name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
559 thunk_list
->u1
.Function
);
561 TRACE_(imports
)("--- %s %s.%d = %p\n", pe_name
->Name
, name
, pe_name
->Hint
, thunk_list
->u1
.Function
);
568 /* restore old protection of the import address table */
569 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
, &protect_size
, protect_old
, NULL
);
574 /****************************************************************
577 * Fixup all imports of a given module.
578 * The loader_section must be locked while calling this function.
580 static NTSTATUS
fixup_imports( WINE_MODREF
*wm
, LPCWSTR load_path
)
583 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
588 if (!(imports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
589 IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
590 return STATUS_SUCCESS
;
592 nb_imports
= size
/ sizeof(*imports
);
593 for (i
= 0; i
< nb_imports
; i
++)
595 if (!imports
[i
].Name
)
601 if (!nb_imports
) return STATUS_SUCCESS
; /* no imports */
603 /* Allocate module dependency list */
604 wm
->nDeps
= nb_imports
;
605 wm
->deps
= RtlAllocateHeap( GetProcessHeap(), 0, nb_imports
*sizeof(WINE_MODREF
*) );
607 /* load the imported modules. They are automatically
608 * added to the modref list of the process.
610 prev
= current_modref
;
612 status
= STATUS_SUCCESS
;
613 for (i
= 0; i
< nb_imports
; i
++)
615 if (!(wm
->deps
[i
] = import_dll( wm
->ldr
.BaseAddress
, &imports
[i
], load_path
)))
616 status
= STATUS_DLL_NOT_FOUND
;
618 current_modref
= prev
;
623 /*************************************************************************
626 * Allocate a WINE_MODREF structure and add it to the process list
627 * The loader_section must be locked while calling this function.
629 static WINE_MODREF
*alloc_module( HMODULE hModule
, LPCWSTR filename
)
633 const IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader(hModule
);
634 PLIST_ENTRY entry
, mark
;
636 if (!(wm
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm
) ))) return NULL
;
641 wm
->ldr
.BaseAddress
= hModule
;
642 wm
->ldr
.EntryPoint
= NULL
;
643 wm
->ldr
.SizeOfImage
= nt
->OptionalHeader
.SizeOfImage
;
645 wm
->ldr
.LoadCount
= 0;
646 wm
->ldr
.TlsIndex
= -1;
647 wm
->ldr
.SectionHandle
= NULL
;
648 wm
->ldr
.CheckSum
= 0;
649 wm
->ldr
.TimeDateStamp
= 0;
651 RtlCreateUnicodeString( &wm
->ldr
.FullDllName
, filename
);
652 if ((p
= strrchrW( wm
->ldr
.FullDllName
.Buffer
, '\\' ))) p
++;
653 else p
= wm
->ldr
.FullDllName
.Buffer
;
654 RtlInitUnicodeString( &wm
->ldr
.BaseDllName
, p
);
656 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
658 wm
->ldr
.Flags
|= LDR_IMAGE_IS_DLL
;
659 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
660 wm
->ldr
.EntryPoint
= (char *)hModule
+ nt
->OptionalHeader
.AddressOfEntryPoint
;
663 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
,
664 &wm
->ldr
.InLoadOrderModuleList
);
666 /* insert module in MemoryList, sorted in increasing base addresses */
667 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
668 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
670 if (CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
)->BaseAddress
> wm
->ldr
.BaseAddress
)
673 entry
->Blink
->Flink
= &wm
->ldr
.InMemoryOrderModuleList
;
674 wm
->ldr
.InMemoryOrderModuleList
.Blink
= entry
->Blink
;
675 wm
->ldr
.InMemoryOrderModuleList
.Flink
= entry
;
676 entry
->Blink
= &wm
->ldr
.InMemoryOrderModuleList
;
678 /* wait until init is called for inserting into this list */
679 wm
->ldr
.InInitializationOrderModuleList
.Flink
= NULL
;
680 wm
->ldr
.InInitializationOrderModuleList
.Blink
= NULL
;
685 /*************************************************************************
688 * Allocate the process-wide structure for module TLS storage.
690 static NTSTATUS
alloc_process_tls(void)
692 PLIST_ENTRY mark
, entry
;
694 const IMAGE_TLS_DIRECTORY
*dir
;
697 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
698 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
700 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
701 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
,
702 IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
704 size
= (dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
) + dir
->SizeOfZeroFill
;
706 tls_total_size
+= size
;
709 if (!tls_module_count
) return STATUS_SUCCESS
;
711 TRACE( "count %u size %u\n", tls_module_count
, tls_total_size
);
713 tls_dirs
= RtlAllocateHeap( GetProcessHeap(), 0, tls_module_count
* sizeof(*tls_dirs
) );
714 if (!tls_dirs
) return STATUS_NO_MEMORY
;
716 for (i
= 0, entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
718 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
719 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
,
720 IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
723 *dir
->AddressOfIndex
= i
;
725 mod
->LoadCount
= -1; /* can't unload it */
728 return STATUS_SUCCESS
;
732 /*************************************************************************
735 * Allocate the per-thread structure for module TLS storage.
737 static NTSTATUS
alloc_thread_tls(void)
743 if (!tls_module_count
) return STATUS_SUCCESS
;
745 if (!(pointers
= RtlAllocateHeap( GetProcessHeap(), 0,
746 tls_module_count
* sizeof(*pointers
) )))
747 return STATUS_NO_MEMORY
;
749 if (!(data
= RtlAllocateHeap( GetProcessHeap(), 0, tls_total_size
)))
751 RtlFreeHeap( GetProcessHeap(), 0, pointers
);
752 return STATUS_NO_MEMORY
;
755 for (i
= 0; i
< tls_module_count
; i
++)
757 const IMAGE_TLS_DIRECTORY
*dir
= tls_dirs
[i
];
758 ULONG size
= dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
;
760 TRACE( "thread %04lx idx %d: %ld/%ld bytes from %p to %p\n",
761 GetCurrentThreadId(), i
, size
, dir
->SizeOfZeroFill
,
762 (void *)dir
->StartAddressOfRawData
, data
);
765 memcpy( data
, (void *)dir
->StartAddressOfRawData
, size
);
767 memset( data
, 0, dir
->SizeOfZeroFill
);
768 data
+= dir
->SizeOfZeroFill
;
770 NtCurrentTeb()->ThreadLocalStoragePointer
= pointers
;
771 return STATUS_SUCCESS
;
775 /*************************************************************************
778 static void call_tls_callbacks( HMODULE module
, UINT reason
)
780 const IMAGE_TLS_DIRECTORY
*dir
;
781 const PIMAGE_TLS_CALLBACK
*callback
;
784 dir
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_TLS
, &dirsize
);
785 if (!dir
|| !dir
->AddressOfCallBacks
) return;
787 for (callback
= dir
->AddressOfCallBacks
; *callback
; callback
++)
790 DPRINTF("%04lx:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
791 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
792 (*callback
)( module
, reason
, NULL
);
794 DPRINTF("%04lx:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
795 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
800 /*************************************************************************
803 static BOOL
MODULE_InitDLL( WINE_MODREF
*wm
, UINT reason
, LPVOID lpReserved
)
807 DLLENTRYPROC entry
= wm
->ldr
.EntryPoint
;
808 void *module
= wm
->ldr
.BaseAddress
;
810 /* Skip calls for modules loaded with special load flags */
812 if (wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
) return TRUE
;
813 if (wm
->ldr
.TlsIndex
!= -1) call_tls_callbacks( wm
->ldr
.BaseAddress
, reason
);
814 if (!entry
) return TRUE
;
818 size_t len
= min( wm
->ldr
.BaseDllName
.Length
, sizeof(mod_name
)-sizeof(WCHAR
) );
819 memcpy( mod_name
, wm
->ldr
.BaseDllName
.Buffer
, len
);
820 mod_name
[len
/ sizeof(WCHAR
)] = 0;
821 DPRINTF("%04lx:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
822 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
823 reason_names
[reason
], lpReserved
);
825 else TRACE("(%p %s,%s,%p) - CALL\n", module
, debugstr_w(wm
->ldr
.BaseDllName
.Buffer
),
826 reason_names
[reason
], lpReserved
);
828 retv
= call_dll_entry_point( entry
, module
, reason
, lpReserved
);
830 /* The state of the module list may have changed due to the call
831 to the dll. We cannot assume that this module has not been
834 DPRINTF("%04lx:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
835 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
836 reason_names
[reason
], lpReserved
, retv
);
837 else TRACE("(%p,%s,%p) - RETURN %d\n", module
, reason_names
[reason
], lpReserved
, retv
);
843 /*************************************************************************
846 * Send the process attach notification to all DLLs the given module
847 * depends on (recursively). This is somewhat complicated due to the fact that
849 * - we have to respect the module dependencies, i.e. modules implicitly
850 * referenced by another module have to be initialized before the module
851 * itself can be initialized
853 * - the initialization routine of a DLL can itself call LoadLibrary,
854 * thereby introducing a whole new set of dependencies (even involving
855 * the 'old' modules) at any time during the whole process
857 * (Note that this routine can be recursively entered not only directly
858 * from itself, but also via LoadLibrary from one of the called initialization
861 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
862 * the process *detach* notifications to be sent in the correct order.
863 * This must not only take into account module dependencies, but also
864 * 'hidden' dependencies created by modules calling LoadLibrary in their
865 * attach notification routine.
867 * The strategy is rather simple: we move a WINE_MODREF to the head of the
868 * list after the attach notification has returned. This implies that the
869 * detach notifications are called in the reverse of the sequence the attach
870 * notifications *returned*.
872 * The loader_section must be locked while calling this function.
874 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
)
876 NTSTATUS status
= STATUS_SUCCESS
;
879 /* prevent infinite recursion in case of cyclical dependencies */
880 if ( ( wm
->ldr
.Flags
& LDR_LOAD_IN_PROGRESS
)
881 || ( wm
->ldr
.Flags
& LDR_PROCESS_ATTACHED
) )
884 TRACE("(%s,%p) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
886 /* Tag current MODREF to prevent recursive loop */
887 wm
->ldr
.Flags
|= LDR_LOAD_IN_PROGRESS
;
889 /* Recursively attach all DLLs this one depends on */
890 for ( i
= 0; i
< wm
->nDeps
; i
++ )
892 if (!wm
->deps
[i
]) continue;
893 if ((status
= process_attach( wm
->deps
[i
], lpReserved
)) != STATUS_SUCCESS
) break;
896 /* Call DLL entry point */
897 if (status
== STATUS_SUCCESS
)
899 WINE_MODREF
*prev
= current_modref
;
901 if (MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
))
903 wm
->ldr
.Flags
|= LDR_PROCESS_ATTACHED
;
907 /* point to the name so LdrInitializeThunk can print it */
908 last_failed_modref
= wm
;
909 WARN("Initialization of %s failed\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
910 status
= STATUS_DLL_INIT_FAILED
;
912 current_modref
= prev
;
915 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
,
916 &wm
->ldr
.InInitializationOrderModuleList
);
918 /* Remove recursion flag */
919 wm
->ldr
.Flags
&= ~LDR_LOAD_IN_PROGRESS
;
921 TRACE("(%s,%p) - END\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
925 /*************************************************************************
928 * Send DLL process detach notifications. See the comment about calling
929 * sequence at process_attach. Unless the bForceDetach flag
930 * is set, only DLLs with zero refcount are notified.
932 static void process_detach( BOOL bForceDetach
, LPVOID lpReserved
)
934 PLIST_ENTRY mark
, entry
;
937 RtlEnterCriticalSection( &loader_section
);
938 if (bForceDetach
) process_detaching
= 1;
939 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
942 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
944 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
945 InInitializationOrderModuleList
);
946 /* Check whether to detach this DLL */
947 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
949 if ( mod
->LoadCount
&& !bForceDetach
)
952 /* Call detach notification */
953 mod
->Flags
&= ~LDR_PROCESS_ATTACHED
;
954 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
955 DLL_PROCESS_DETACH
, lpReserved
);
957 /* Restart at head of WINE_MODREF list, as entries might have
958 been added and/or removed while performing the call ... */
961 } while (entry
!= mark
);
963 RtlLeaveCriticalSection( &loader_section
);
966 /*************************************************************************
967 * MODULE_DllThreadAttach
969 * Send DLL thread attach notifications. These are sent in the
970 * reverse sequence of process detach notification.
973 NTSTATUS
MODULE_DllThreadAttach( LPVOID lpReserved
)
975 PLIST_ENTRY mark
, entry
;
979 /* don't do any attach calls if process is exiting */
980 if (process_detaching
) return STATUS_SUCCESS
;
981 /* FIXME: there is still a race here */
983 RtlEnterCriticalSection( &loader_section
);
985 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto done
;
987 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
988 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
990 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
991 InInitializationOrderModuleList
);
992 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
994 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
997 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
998 DLL_THREAD_ATTACH
, lpReserved
);
1002 RtlLeaveCriticalSection( &loader_section
);
1006 /******************************************************************
1007 * LdrDisableThreadCalloutsForDll (NTDLL.@)
1010 NTSTATUS WINAPI
LdrDisableThreadCalloutsForDll(HMODULE hModule
)
1013 NTSTATUS ret
= STATUS_SUCCESS
;
1015 RtlEnterCriticalSection( &loader_section
);
1017 wm
= get_modref( hModule
);
1018 if (!wm
|| wm
->ldr
.TlsIndex
!= -1)
1019 ret
= STATUS_DLL_NOT_FOUND
;
1021 wm
->ldr
.Flags
|= LDR_NO_DLL_CALLS
;
1023 RtlLeaveCriticalSection( &loader_section
);
1028 /******************************************************************
1029 * LdrFindEntryForAddress (NTDLL.@)
1031 * The loader_section must be locked while calling this function
1033 NTSTATUS WINAPI
LdrFindEntryForAddress(const void* addr
, PLDR_MODULE
* pmod
)
1035 PLIST_ENTRY mark
, entry
;
1038 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
1039 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1041 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
1042 if ((const void *)mod
->BaseAddress
<= addr
&&
1043 (const char *)addr
< (char*)mod
->BaseAddress
+ mod
->SizeOfImage
)
1046 return STATUS_SUCCESS
;
1048 if ((const void *)mod
->BaseAddress
> addr
) break;
1050 return STATUS_NO_MORE_ENTRIES
;
1053 /******************************************************************
1054 * LdrLockLoaderLock (NTDLL.@)
1056 * Note: flags are not implemented.
1057 * Flag 0x01 is used to raise exceptions on errors.
1058 * Flag 0x02 is used to avoid waiting on the section (does RtlTryEnterCriticalSection instead).
1060 NTSTATUS WINAPI
LdrLockLoaderLock( ULONG flags
, ULONG
*result
, ULONG
*magic
)
1062 if (flags
) FIXME( "flags %lx not supported\n", flags
);
1064 if (result
) *result
= 1;
1065 if (!magic
) return STATUS_INVALID_PARAMETER_3
;
1066 RtlEnterCriticalSection( &loader_section
);
1067 *magic
= GetCurrentThreadId();
1068 return STATUS_SUCCESS
;
1072 /******************************************************************
1073 * LdrUnlockLoaderUnlock (NTDLL.@)
1075 NTSTATUS WINAPI
LdrUnlockLoaderLock( ULONG flags
, ULONG magic
)
1079 if (magic
!= GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2
;
1080 RtlLeaveCriticalSection( &loader_section
);
1082 return STATUS_SUCCESS
;
1086 /******************************************************************
1087 * LdrGetDllHandle (NTDLL.@)
1089 NTSTATUS WINAPI
LdrGetDllHandle(ULONG x
, ULONG y
, const UNICODE_STRING
*name
, HMODULE
*base
)
1091 NTSTATUS status
= STATUS_DLL_NOT_FOUND
;
1092 WCHAR dllname
[MAX_PATH
+4], *p
;
1094 PLIST_ENTRY mark
, entry
;
1097 if (x
!= 0 || y
!= 0)
1098 FIXME("Unknown behavior, please report\n");
1100 /* Append .DLL to name if no extension present */
1101 if (!(p
= strrchrW( name
->Buffer
, '.')) || strchrW( p
, '/' ) || strchrW( p
, '\\'))
1103 if (name
->Length
>= MAX_PATH
) return STATUS_NAME_TOO_LONG
;
1104 strcpyW( dllname
, name
->Buffer
);
1105 strcatW( dllname
, dllW
);
1106 RtlInitUnicodeString( &str
, dllname
);
1110 RtlEnterCriticalSection( &loader_section
);
1114 if (RtlEqualUnicodeString( name
, &cached_modref
->ldr
.FullDllName
, TRUE
) ||
1115 RtlEqualUnicodeString( name
, &cached_modref
->ldr
.BaseDllName
, TRUE
))
1117 *base
= cached_modref
->ldr
.BaseAddress
;
1118 status
= STATUS_SUCCESS
;
1123 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1124 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1126 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1128 if (RtlEqualUnicodeString( name
, &mod
->FullDllName
, TRUE
) ||
1129 RtlEqualUnicodeString( name
, &mod
->BaseDllName
, TRUE
))
1131 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
1132 *base
= mod
->BaseAddress
;
1133 status
= STATUS_SUCCESS
;
1138 RtlLeaveCriticalSection( &loader_section
);
1139 TRACE("%lx %lx %s -> %p\n", x
, y
, debugstr_us(name
), status
? NULL
: *base
);
1144 /******************************************************************
1145 * LdrGetProcedureAddress (NTDLL.@)
1147 NTSTATUS WINAPI
LdrGetProcedureAddress(HMODULE module
, const ANSI_STRING
*name
,
1148 ULONG ord
, PVOID
*address
)
1150 IMAGE_EXPORT_DIRECTORY
*exports
;
1152 NTSTATUS ret
= STATUS_PROCEDURE_NOT_FOUND
;
1154 RtlEnterCriticalSection( &loader_section
);
1156 if ((exports
= RtlImageDirectoryEntryToData( module
, TRUE
,
1157 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
1159 void *proc
= name
? find_named_export( module
, exports
, exp_size
, name
->Buffer
, -1 )
1160 : find_ordinal_export( module
, exports
, exp_size
, ord
- exports
->Base
);
1164 ret
= STATUS_SUCCESS
;
1169 /* check if the module itself is invalid to return the proper error */
1170 if (!get_modref( module
)) ret
= STATUS_DLL_NOT_FOUND
;
1173 RtlLeaveCriticalSection( &loader_section
);
1178 /***********************************************************************
1179 * load_builtin_callback
1181 * Load a library in memory; callback function for wine_dll_register
1183 static void load_builtin_callback( void *module
, const char *filename
)
1185 static const WCHAR emptyW
[1];
1187 IMAGE_NT_HEADERS
*nt
;
1189 WCHAR
*fullname
, *p
;
1190 const WCHAR
*load_path
;
1194 ERR("could not map image for %s\n", filename
? filename
: "main exe" );
1197 if (!(nt
= RtlImageNtHeader( module
)))
1199 ERR( "bad module for %s\n", filename
? filename
: "main exe" );
1200 builtin_load_info
->status
= STATUS_INVALID_IMAGE_FORMAT
;
1203 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
))
1205 /* if we already have an executable, ignore this one */
1206 if (!NtCurrentTeb()->Peb
->ImageBaseAddress
)
1208 NtCurrentTeb()->Peb
->ImageBaseAddress
= module
;
1209 return; /* don't create the modref here, will be done later on */
1213 /* create the MODREF */
1215 if (!(fullname
= RtlAllocateHeap( GetProcessHeap(), 0,
1216 system_dir
.MaximumLength
+ (strlen(filename
) + 1) * sizeof(WCHAR
) )))
1218 ERR( "can't load %s\n", filename
);
1219 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1222 memcpy( fullname
, system_dir
.Buffer
, system_dir
.Length
);
1223 p
= fullname
+ system_dir
.Length
/ sizeof(WCHAR
);
1224 if (p
> fullname
&& p
[-1] != '\\') *p
++ = '\\';
1225 ascii_to_unicode( p
, filename
, strlen(filename
) + 1 );
1227 wm
= alloc_module( module
, fullname
);
1228 RtlFreeHeap( GetProcessHeap(), 0, fullname
);
1231 ERR( "can't load %s\n", filename
);
1232 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1235 wm
->ldr
.Flags
|= LDR_WINE_INTERNAL
;
1237 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &nt
->OptionalHeader
.SizeOfImage
,
1238 MEM_SYSTEM
| MEM_IMAGE
, PAGE_EXECUTE_WRITECOPY
);
1242 load_path
= builtin_load_info
->load_path
;
1243 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1244 if (!load_path
) load_path
= emptyW
;
1245 if (fixup_imports( wm
, load_path
) != STATUS_SUCCESS
)
1247 /* the module has only be inserted in the load & memory order lists */
1248 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1249 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1250 /* FIXME: free the modref */
1251 builtin_load_info
->status
= STATUS_DLL_NOT_FOUND
;
1254 builtin_load_info
->wm
= wm
;
1255 TRACE( "loaded %s %p %p\n", filename
, wm
, module
);
1257 /* send the DLL load event */
1259 SERVER_START_REQ( load_dll
)
1263 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1264 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1265 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1266 req
->name
= &wm
->ldr
.FullDllName
.Buffer
;
1267 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1268 wine_server_call( req
);
1272 /* setup relay debugging entry points */
1273 if (TRACE_ON(relay
)) RELAY_SetupDLL( module
);
1277 /******************************************************************************
1278 * load_native_dll (internal)
1280 static NTSTATUS
load_native_dll( LPCWSTR load_path
, LPCWSTR name
, HANDLE file
,
1281 DWORD flags
, WINE_MODREF
** pwm
)
1285 OBJECT_ATTRIBUTES attr
;
1287 IMAGE_NT_HEADERS
*nt
;
1292 TRACE( "loading %s\n", debugstr_w(name
) );
1294 attr
.Length
= sizeof(attr
);
1295 attr
.RootDirectory
= 0;
1296 attr
.ObjectName
= NULL
;
1297 attr
.Attributes
= 0;
1298 attr
.SecurityDescriptor
= NULL
;
1299 attr
.SecurityQualityOfService
= NULL
;
1302 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1303 &attr
, &size
, 0, SEC_IMAGE
, file
);
1304 if (status
!= STATUS_SUCCESS
) return status
;
1307 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(),
1308 &module
, 0, 0, &size
, &len
, ViewShare
, 0, PAGE_READONLY
);
1310 if (status
!= STATUS_SUCCESS
) return status
;
1312 /* create the MODREF */
1314 if (!(wm
= alloc_module( module
, name
))) return STATUS_NO_MEMORY
;
1318 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1320 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
)
1322 /* the module has only be inserted in the load & memory order lists */
1323 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1324 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1326 /* FIXME: there are several more dangling references
1327 * left. Including dlls loaded by this dll before the
1328 * failed one. Unrolling is rather difficult with the
1329 * current structure and we can leave them lying
1330 * around with no problems, so we don't care.
1331 * As these might reference our wm, we don't free it.
1336 else wm
->ldr
.Flags
|= LDR_DONT_RESOLVE_REFS
;
1338 /* send DLL load event */
1340 nt
= RtlImageNtHeader( module
);
1342 /* don't keep the file open if the mapping is from removable media */
1343 if (!VIRTUAL_HasMapping( module
)) file
= 0;
1345 SERVER_START_REQ( load_dll
)
1349 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1350 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1351 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1352 req
->name
= &wm
->ldr
.FullDllName
.Buffer
;
1353 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1354 wine_server_call( req
);
1358 if (TRACE_ON(snoop
)) SNOOP_SetupDLL( module
);
1360 TRACE_(loaddll
)( " Loaded module %s : native\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
) );
1363 return STATUS_SUCCESS
;
1367 /***********************************************************************
1370 static NTSTATUS
load_builtin_dll( LPCWSTR load_path
, LPCWSTR path
, DWORD flags
, WINE_MODREF
** pwm
)
1372 char error
[256], dllname
[MAX_PATH
];
1374 const WCHAR
*name
, *p
;
1377 struct builtin_load_info info
, *prev_info
;
1379 /* Fix the name in case we have a full path and extension */
1381 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
1382 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
1384 /* we don't want to depend on the current codepage here */
1385 len
= strlenW( name
) + 1;
1386 if (len
>= sizeof(dllname
)) return STATUS_NAME_TOO_LONG
;
1387 for (i
= 0; i
< len
; i
++)
1389 if (name
[i
] > 127) return STATUS_DLL_NOT_FOUND
;
1390 dllname
[i
] = (char)name
[i
];
1391 if (dllname
[i
] >= 'A' && dllname
[i
] <= 'Z') dllname
[i
] += 'a' - 'A';
1394 /* load_library will modify info.status. Note also that load_library can be
1395 * called several times, if the .so file we're loading has dependencies.
1396 * info.status will gather all the errors we may get while loading all these
1399 info
.load_path
= load_path
;
1400 info
.status
= STATUS_SUCCESS
;
1402 prev_info
= builtin_load_info
;
1403 builtin_load_info
= &info
;
1404 handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
);
1405 builtin_load_info
= prev_info
;
1411 /* The file does not exist -> WARN() */
1412 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1413 return STATUS_DLL_NOT_FOUND
;
1415 /* ERR() for all other errors (missing functions, ...) */
1416 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1417 return STATUS_PROCEDURE_NOT_FOUND
;
1419 if (info
.status
!= STATUS_SUCCESS
) return info
.status
;
1423 /* The constructor wasn't called, this means the .so is already
1424 * loaded under a different name. We can't support multiple names
1425 * for the same module, so return an error. */
1426 return STATUS_INVALID_IMAGE_FORMAT
;
1429 TRACE_(loaddll
)( "Loaded module %s : builtin\n", debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
) );
1431 info
.wm
->ldr
.SectionHandle
= handle
;
1432 if (strcmpiW( info
.wm
->ldr
.BaseDllName
.Buffer
, name
))
1434 ERR( "loaded .so for %s but got %s instead - probably 16-bit dll\n",
1435 debugstr_w(name
), debugstr_w(info
.wm
->ldr
.BaseDllName
.Buffer
) );
1436 /* wine_dll_unload( handle );*/
1437 return STATUS_INVALID_IMAGE_FORMAT
;
1440 return STATUS_SUCCESS
;
1444 /***********************************************************************
1447 * Find the file (or already loaded module) for a given dll name.
1449 static NTSTATUS
find_dll_file( const WCHAR
*load_path
, const WCHAR
*libname
,
1450 WCHAR
*filename
, ULONG
*size
, WINE_MODREF
**pwm
, HANDLE
*handle
)
1452 OBJECT_ATTRIBUTES attr
;
1454 UNICODE_STRING nt_name
;
1455 WCHAR
*file_part
, *ext
, *dllname
;
1458 /* first append .dll if needed */
1461 if (!(ext
= strrchrW( libname
, '.')) || strchrW( ext
, '/' ) || strchrW( ext
, '\\'))
1463 if (!(dllname
= RtlAllocateHeap( GetProcessHeap(), 0,
1464 (strlenW(libname
) * sizeof(WCHAR
)) + sizeof(dllW
) )))
1465 return STATUS_NO_MEMORY
;
1466 strcpyW( dllname
, libname
);
1467 strcatW( dllname
, dllW
);
1471 nt_name
.Buffer
= NULL
;
1472 if (RtlDetermineDosPathNameType_U( libname
) == RELATIVE_PATH
)
1474 /* we need to search for it */
1475 len
= RtlDosSearchPath_U( load_path
, libname
, NULL
, *size
, filename
, &file_part
);
1478 if (len
>= *size
) goto overflow
;
1479 if ((*pwm
= find_fullname_module( filename
)) != NULL
) goto found
;
1481 /* check for already loaded module in a different path */
1482 if (!contains_path( libname
))
1484 if ((*pwm
= find_basename_module( file_part
)) != NULL
) goto found
;
1486 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, NULL
, NULL
))
1488 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1489 return STATUS_NO_MEMORY
;
1491 attr
.Length
= sizeof(attr
);
1492 attr
.RootDirectory
= 0;
1493 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1494 attr
.ObjectName
= &nt_name
;
1495 attr
.SecurityDescriptor
= NULL
;
1496 attr
.SecurityQualityOfService
= NULL
;
1497 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1503 if (!contains_path( libname
))
1505 /* if libname doesn't contain a path at all, we simply return the name as is,
1506 * to be loaded as builtin */
1507 len
= strlenW(libname
) * sizeof(WCHAR
);
1508 if (len
>= *size
) goto overflow
;
1509 strcpyW( filename
, libname
);
1510 *pwm
= find_basename_module( filename
);
1515 /* absolute path name, or relative path name but not found above */
1517 if (!RtlDosPathNameToNtPathName_U( libname
, &nt_name
, &file_part
, NULL
))
1519 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1520 return STATUS_NO_MEMORY
;
1522 len
= nt_name
.Length
- 4*sizeof(WCHAR
); /* for \??\ prefix */
1523 if (len
>= *size
) goto overflow
;
1524 memcpy( filename
, nt_name
.Buffer
+ 4, len
+ sizeof(WCHAR
) );
1525 if (!(*pwm
= find_fullname_module( filename
)))
1527 attr
.Length
= sizeof(attr
);
1528 attr
.RootDirectory
= 0;
1529 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1530 attr
.ObjectName
= &nt_name
;
1531 attr
.SecurityDescriptor
= NULL
;
1532 attr
.SecurityQualityOfService
= NULL
;
1533 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1536 RtlFreeUnicodeString( &nt_name
);
1537 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1538 return STATUS_SUCCESS
;
1541 RtlFreeUnicodeString( &nt_name
);
1542 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1543 *size
= len
+ sizeof(WCHAR
);
1544 return STATUS_BUFFER_TOO_SMALL
;
1548 /***********************************************************************
1549 * load_dll (internal)
1551 * Load a PE style module according to the load order.
1552 * The loader_section must be locked while calling this function.
1554 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
)
1557 enum loadorder_type loadorder
[LOADORDER_NTYPES
];
1561 const char *filetype
= "";
1562 WINE_MODREF
*main_exe
;
1566 TRACE( "looking for %s in %s\n", debugstr_w(libname
), debugstr_w(load_path
) );
1569 size
= sizeof(buffer
);
1572 nts
= find_dll_file( load_path
, libname
, filename
, &size
, pwm
, &handle
);
1573 if (nts
== STATUS_SUCCESS
) break;
1574 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1575 if (nts
!= STATUS_BUFFER_TOO_SMALL
) return nts
;
1576 /* grow the buffer and retry */
1577 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
))) return STATUS_NO_MEMORY
;
1580 if (*pwm
) /* found already loaded module */
1582 if ((*pwm
)->ldr
.LoadCount
!= -1) (*pwm
)->ldr
.LoadCount
++;
1584 if (((*pwm
)->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
) &&
1585 !(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1587 (*pwm
)->ldr
.Flags
&= ~LDR_DONT_RESOLVE_REFS
;
1588 fixup_imports( *pwm
, load_path
);
1590 TRACE("Found loaded module %s for %s at %p, count=%d\n",
1591 debugstr_w((*pwm
)->ldr
.FullDllName
.Buffer
), debugstr_w(libname
),
1592 (*pwm
)->ldr
.BaseAddress
, (*pwm
)->ldr
.LoadCount
);
1593 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1594 return STATUS_SUCCESS
;
1597 main_exe
= get_modref( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1598 MODULE_GetLoadOrderW( loadorder
, main_exe
? main_exe
->ldr
.BaseDllName
.Buffer
: NULL
, filename
);
1600 nts
= STATUS_DLL_NOT_FOUND
;
1601 for (i
= 0; i
< LOADORDER_NTYPES
; i
++)
1603 if (loadorder
[i
] == LOADORDER_INVALID
) break;
1605 switch (loadorder
[i
])
1608 TRACE("Trying native dll %s\n", debugstr_w(filename
));
1609 if (!handle
) continue; /* it cannot possibly be loaded */
1610 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
1611 filetype
= "native";
1614 TRACE("Trying built-in %s\n", debugstr_w(filename
));
1615 nts
= load_builtin_dll( load_path
, filename
, flags
, pwm
);
1616 filetype
= "builtin";
1619 nts
= STATUS_INTERNAL_ERROR
;
1623 if (nts
== STATUS_SUCCESS
)
1625 /* Initialize DLL just loaded */
1626 TRACE("Loaded module %s (%s) at %p\n",
1627 debugstr_w(filename
), filetype
, (*pwm
)->ldr
.BaseAddress
);
1628 /* Set the ldr.LoadCount here so that an attach failure will */
1629 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1630 (*pwm
)->ldr
.LoadCount
= 1;
1631 if (handle
) NtClose( handle
);
1632 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1635 if (nts
!= STATUS_DLL_NOT_FOUND
) break;
1638 WARN("Failed to load module %s; status=%lx\n", debugstr_w(libname
), nts
);
1639 if (handle
) NtClose( handle
);
1640 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1644 /******************************************************************
1645 * LdrLoadDll (NTDLL.@)
1647 NTSTATUS WINAPI
LdrLoadDll(LPCWSTR path_name
, DWORD flags
,
1648 const UNICODE_STRING
*libname
, HMODULE
* hModule
)
1653 RtlEnterCriticalSection( &loader_section
);
1655 if (!path_name
) path_name
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1656 nts
= load_dll( path_name
, libname
->Buffer
, flags
, &wm
);
1658 if (nts
== STATUS_SUCCESS
&& !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
1660 nts
= process_attach( wm
, NULL
);
1661 if (nts
!= STATUS_SUCCESS
)
1663 WARN("Attach failed for module %s\n", debugstr_w(libname
->Buffer
));
1664 LdrUnloadDll(wm
->ldr
.BaseAddress
);
1668 *hModule
= (wm
) ? wm
->ldr
.BaseAddress
: NULL
;
1670 RtlLeaveCriticalSection( &loader_section
);
1674 /******************************************************************
1675 * LdrQueryProcessModuleInformation
1678 NTSTATUS WINAPI
LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi
,
1679 ULONG buf_size
, ULONG
* req_size
)
1681 SYSTEM_MODULE
* sm
= &smi
->Modules
[0];
1682 ULONG size
= sizeof(ULONG
);
1683 NTSTATUS nts
= STATUS_SUCCESS
;
1686 PLIST_ENTRY mark
, entry
;
1689 smi
->ModulesCount
= 0;
1691 RtlEnterCriticalSection( &loader_section
);
1692 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1693 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1695 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1696 size
+= sizeof(*sm
);
1697 if (size
<= buf_size
)
1699 sm
->Reserved1
= 0; /* FIXME */
1700 sm
->Reserved2
= 0; /* FIXME */
1701 sm
->ImageBaseAddress
= mod
->BaseAddress
;
1702 sm
->ImageSize
= mod
->SizeOfImage
;
1703 sm
->Flags
= mod
->Flags
;
1704 sm
->Id
= 0; /* FIXME */
1705 sm
->Rank
= 0; /* FIXME */
1706 sm
->Unknown
= 0; /* FIXME */
1708 str
.MaximumLength
= MAXIMUM_FILENAME_LENGTH
;
1709 str
.Buffer
= sm
->Name
;
1710 RtlUnicodeStringToAnsiString(&str
, &mod
->FullDllName
, FALSE
);
1711 ptr
= strrchr(sm
->Name
, '\\');
1712 sm
->NameOffset
= (ptr
!= NULL
) ? (ptr
- (char*)sm
->Name
+ 1) : 0;
1714 smi
->ModulesCount
++;
1717 else nts
= STATUS_INFO_LENGTH_MISMATCH
;
1719 RtlLeaveCriticalSection( &loader_section
);
1721 if (req_size
) *req_size
= size
;
1726 /******************************************************************
1727 * LdrShutdownProcess (NTDLL.@)
1730 void WINAPI
LdrShutdownProcess(void)
1733 process_detach( TRUE
, (LPVOID
)1 );
1736 /******************************************************************
1737 * LdrShutdownThread (NTDLL.@)
1740 void WINAPI
LdrShutdownThread(void)
1742 PLIST_ENTRY mark
, entry
;
1747 /* don't do any detach calls if process is exiting */
1748 if (process_detaching
) return;
1749 /* FIXME: there is still a race here */
1751 RtlEnterCriticalSection( &loader_section
);
1753 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1754 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
1756 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1757 InInitializationOrderModuleList
);
1758 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1760 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
1763 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1764 DLL_THREAD_DETACH
, NULL
);
1767 RtlLeaveCriticalSection( &loader_section
);
1770 /***********************************************************************
1771 * MODULE_FlushModrefs
1773 * Remove all unused modrefs and call the internal unloading routines
1774 * for the library type.
1776 * The loader_section must be locked while calling this function.
1778 static void MODULE_FlushModrefs(void)
1780 PLIST_ENTRY mark
, entry
, prev
;
1784 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1785 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
1787 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1788 InInitializationOrderModuleList
);
1789 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
1791 prev
= entry
->Blink
;
1792 if (mod
->LoadCount
) continue;
1794 RemoveEntryList(&mod
->InLoadOrderModuleList
);
1795 RemoveEntryList(&mod
->InMemoryOrderModuleList
);
1796 RemoveEntryList(&mod
->InInitializationOrderModuleList
);
1798 TRACE(" unloading %s\n", debugstr_w(mod
->FullDllName
.Buffer
));
1799 if (!TRACE_ON(module
))
1800 TRACE_(loaddll
)("Unloaded module %s : %s\n",
1801 debugstr_w(mod
->FullDllName
.Buffer
),
1802 (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native" );
1804 SERVER_START_REQ( unload_dll
)
1806 req
->base
= mod
->BaseAddress
;
1807 wine_server_call( req
);
1811 NtUnmapViewOfSection( NtCurrentProcess(), mod
->BaseAddress
);
1812 if (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) wine_dll_unload( wm
->ldr
.SectionHandle
);
1813 if (cached_modref
== wm
) cached_modref
= NULL
;
1814 RtlFreeUnicodeString( &mod
->FullDllName
);
1815 RtlFreeHeap( GetProcessHeap(), 0, wm
->deps
);
1816 RtlFreeHeap( GetProcessHeap(), 0, wm
);
1820 /***********************************************************************
1821 * MODULE_DecRefCount
1823 * The loader_section must be locked while calling this function.
1825 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
1829 if ( wm
->ldr
.Flags
& LDR_UNLOAD_IN_PROGRESS
)
1832 if ( wm
->ldr
.LoadCount
<= 0 )
1835 --wm
->ldr
.LoadCount
;
1836 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
1838 if ( wm
->ldr
.LoadCount
== 0 )
1840 wm
->ldr
.Flags
|= LDR_UNLOAD_IN_PROGRESS
;
1842 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1844 MODULE_DecRefCount( wm
->deps
[i
] );
1846 wm
->ldr
.Flags
&= ~LDR_UNLOAD_IN_PROGRESS
;
1850 /******************************************************************
1851 * LdrUnloadDll (NTDLL.@)
1855 NTSTATUS WINAPI
LdrUnloadDll( HMODULE hModule
)
1857 NTSTATUS retv
= STATUS_SUCCESS
;
1859 TRACE("(%p)\n", hModule
);
1861 RtlEnterCriticalSection( &loader_section
);
1863 /* if we're stopping the whole process (and forcing the removal of all
1864 * DLLs) the library will be freed anyway
1866 if (!process_detaching
)
1871 if ((wm
= get_modref( hModule
)) != NULL
)
1873 TRACE("(%s) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
1875 /* Recursively decrement reference counts */
1876 MODULE_DecRefCount( wm
);
1878 /* Call process detach notifications */
1879 if ( free_lib_count
<= 1 )
1881 process_detach( FALSE
, NULL
);
1882 MODULE_FlushModrefs();
1888 retv
= STATUS_DLL_NOT_FOUND
;
1893 RtlLeaveCriticalSection( &loader_section
);
1898 /***********************************************************************
1899 * RtlImageNtHeader (NTDLL.@)
1901 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
1903 IMAGE_NT_HEADERS
*ret
;
1907 IMAGE_DOS_HEADER
*dos
= (IMAGE_DOS_HEADER
*)hModule
;
1910 if (dos
->e_magic
== IMAGE_DOS_SIGNATURE
)
1912 ret
= (IMAGE_NT_HEADERS
*)((char *)dos
+ dos
->e_lfanew
);
1913 if (ret
->Signature
!= IMAGE_NT_SIGNATURE
) ret
= NULL
;
1916 __EXCEPT(page_fault
)
1925 /******************************************************************
1926 * LdrInitializeThunk (NTDLL.@)
1928 * FIXME: the arguments are not correct, main_file is a Wine invention.
1930 void WINAPI
LdrInitializeThunk( HANDLE main_file
, ULONG unknown2
, ULONG unknown3
, ULONG unknown4
)
1935 PEB
*peb
= NtCurrentTeb()->Peb
;
1936 UNICODE_STRING
*main_exe_name
= &peb
->ProcessParameters
->ImagePathName
;
1937 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
1939 /* allocate the modref for the main exe */
1940 if (!(wm
= alloc_module( peb
->ImageBaseAddress
, main_exe_name
->Buffer
)))
1942 status
= STATUS_NO_MEMORY
;
1945 wm
->ldr
.LoadCount
= -1; /* can't unload main exe */
1947 /* the main exe needs to be the first in the load order list */
1948 RemoveEntryList( &wm
->ldr
.InLoadOrderModuleList
);
1949 InsertHeadList( &peb
->LdrData
->InLoadOrderModuleList
, &wm
->ldr
.InLoadOrderModuleList
);
1951 /* Install signal handlers; this cannot be done before, since we cannot
1952 * send exceptions to the debugger before the create process event that
1953 * is sent by REQ_INIT_PROCESS_DONE.
1954 * We do need the handlers in place by the time the request is over, so
1955 * we set them up here. If we segfault between here and the server call
1956 * something is very wrong... */
1957 if (!SIGNAL_Init()) exit(1);
1959 /* Signal the parent process to continue */
1960 SERVER_START_REQ( init_process_done
)
1962 req
->module
= peb
->ImageBaseAddress
;
1963 req
->module_size
= wm
->ldr
.SizeOfImage
;
1964 req
->entry
= (char *)peb
->ImageBaseAddress
+ nt
->OptionalHeader
.AddressOfEntryPoint
;
1965 /* API requires a double indirection */
1966 req
->name
= &main_exe_name
->Buffer
;
1967 req
->exe_file
= main_file
;
1968 req
->gui
= (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_WINDOWS_CUI
);
1969 wine_server_add_data( req
, main_exe_name
->Buffer
, main_exe_name
->Length
);
1970 wine_server_call( req
);
1974 if (main_file
) NtClose( main_file
); /* we no longer need it */
1976 if (TRACE_ON(relay
) || TRACE_ON(snoop
))
1978 RELAY_InitDebugLists();
1980 if (TRACE_ON(relay
)) /* setup relay for already loaded dlls */
1982 LIST_ENTRY
*entry
, *mark
= &peb
->LdrData
->InLoadOrderModuleList
;
1983 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1985 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1986 if (mod
->Flags
& LDR_WINE_INTERNAL
) RELAY_SetupDLL( mod
->BaseAddress
);
1991 RtlEnterCriticalSection( &loader_section
);
1993 load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1994 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
) goto error
;
1995 if ((status
= alloc_process_tls()) != STATUS_SUCCESS
) goto error
;
1996 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto error
;
1997 if ((status
= process_attach( wm
, (LPVOID
)1 )) != STATUS_SUCCESS
)
1999 if (last_failed_modref
)
2000 ERR( "%s failed to initialize, aborting\n", debugstr_w(last_failed_modref
->ldr
.BaseDllName
.Buffer
) + 1 );
2004 RtlLeaveCriticalSection( &loader_section
);
2006 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
) VIRTUAL_UseLargeAddressSpace();
2010 ERR( "Main exe initialization for %s failed, status %lx\n", debugstr_w(main_exe_name
->Buffer
), status
);
2015 /***********************************************************************
2016 * RtlImageDirectoryEntryToData (NTDLL.@)
2018 PVOID WINAPI
RtlImageDirectoryEntryToData( HMODULE module
, BOOL image
, WORD dir
, ULONG
*size
)
2020 const IMAGE_NT_HEADERS
*nt
;
2023 if ((ULONG_PTR
)module
& 1) /* mapped as data file */
2025 module
= (HMODULE
)((ULONG_PTR
)module
& ~1);
2028 if (!(nt
= RtlImageNtHeader( module
))) return NULL
;
2029 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
2030 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
2031 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
2032 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)module
+ addr
;
2034 /* not mapped as image, need to find the section containing the virtual address */
2035 return RtlImageRvaToVa( nt
, module
, addr
, NULL
);
2039 /***********************************************************************
2040 * RtlImageRvaToSection (NTDLL.@)
2042 PIMAGE_SECTION_HEADER WINAPI
RtlImageRvaToSection( const IMAGE_NT_HEADERS
*nt
,
2043 HMODULE module
, DWORD rva
)
2046 const IMAGE_SECTION_HEADER
*sec
;
2048 sec
= (const IMAGE_SECTION_HEADER
*)((const char*)&nt
->OptionalHeader
+
2049 nt
->FileHeader
.SizeOfOptionalHeader
);
2050 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
2052 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2053 return (PIMAGE_SECTION_HEADER
)sec
;
2059 /***********************************************************************
2060 * RtlImageRvaToVa (NTDLL.@)
2062 PVOID WINAPI
RtlImageRvaToVa( const IMAGE_NT_HEADERS
*nt
, HMODULE module
,
2063 DWORD rva
, IMAGE_SECTION_HEADER
**section
)
2065 IMAGE_SECTION_HEADER
*sec
;
2067 if (section
&& *section
) /* try this section first */
2070 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2073 if (!(sec
= RtlImageRvaToSection( nt
, module
, rva
))) return NULL
;
2075 if (section
) *section
= sec
;
2076 return (char *)module
+ sec
->PointerToRawData
+ (rva
- sec
->VirtualAddress
);
2080 /***********************************************************************
2081 * NtLoadDriver (NTDLL.@)
2082 * ZwLoadDriver (NTDLL.@)
2084 NTSTATUS WINAPI
NtLoadDriver( const UNICODE_STRING
*DriverServiceName
)
2086 FIXME("(%p), stub!\n",DriverServiceName
);
2087 return STATUS_NOT_IMPLEMENTED
;
2091 /***********************************************************************
2092 * NtUnloadDriver (NTDLL.@)
2093 * ZwUnloadDriver (NTDLL.@)
2095 NTSTATUS WINAPI
NtUnloadDriver( const UNICODE_STRING
*DriverServiceName
)
2097 FIXME("(%p), stub!\n",DriverServiceName
);
2098 return STATUS_NOT_IMPLEMENTED
;
2102 /******************************************************************
2103 * __wine_init_windows_dir (NTDLL.@)
2105 * Windows and system dir initialization once kernel32 has been loaded.
2107 void __wine_init_windows_dir( const WCHAR
*windir
, const WCHAR
*sysdir
)
2109 PLIST_ENTRY mark
, entry
;
2112 RtlCreateUnicodeString( &system_dir
, sysdir
);
2114 /* prepend the system dir to the name of the already created modules */
2115 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2116 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2118 LDR_MODULE
*mod
= CONTAINING_RECORD( entry
, LDR_MODULE
, InLoadOrderModuleList
);
2120 assert( mod
->Flags
& LDR_WINE_INTERNAL
);
2122 buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
2123 system_dir
.Length
+ mod
->FullDllName
.Length
+ 2*sizeof(WCHAR
) );
2124 if (!buffer
) continue;
2125 strcpyW( buffer
, system_dir
.Buffer
);
2126 p
= buffer
+ strlenW( buffer
);
2127 if (p
> buffer
&& p
[-1] != '\\') *p
++ = '\\';
2128 strcpyW( p
, mod
->FullDllName
.Buffer
);
2129 RtlInitUnicodeString( &mod
->FullDllName
, buffer
);
2130 RtlInitUnicodeString( &mod
->BaseDllName
, p
);
2135 /***********************************************************************
2136 * __wine_process_init
2138 void __wine_process_init( int argc
, char *argv
[] )
2140 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
2144 ANSI_STRING func_name
;
2145 void (* DECLSPEC_NORETURN init_func
)();
2146 extern mode_t FILE_umask
;
2150 /* retrieve current umask */
2151 FILE_umask
= umask(0777);
2152 umask( FILE_umask
);
2154 /* setup the load callback and create ntdll modref */
2155 wine_dll_set_callback( load_builtin_callback
);
2157 if ((status
= load_builtin_dll( NULL
, kernel32W
, 0, &wm
)) != STATUS_SUCCESS
)
2159 MESSAGE( "wine: could not load kernel32.dll, status %lx\n", status
);
2162 RtlInitAnsiString( &func_name
, "__wine_kernel_init" );
2163 if ((status
= LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
,
2164 0, (void **)&init_func
)) != STATUS_SUCCESS
)
2166 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %lx\n", status
);