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"
35 #include "wine/exception.h"
37 #include "wine/unicode.h"
38 #include "wine/debug.h"
39 #include "wine/server.h"
40 #include "ntdll_misc.h"
42 WINE_DEFAULT_DEBUG_CHANNEL(module);
43 WINE_DECLARE_DEBUG_CHANNEL(relay);
44 WINE_DECLARE_DEBUG_CHANNEL(snoop);
45 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
47 typedef DWORD (CALLBACK *DLLENTRYPROC)(HMODULE,DWORD,LPVOID);
49 static int process_detaching = 0; /* set on process detach to avoid deadlocks with thread detach */
50 static int free_lib_count; /* recursion depth of LdrUnloadDll calls */
52 /* filter for page-fault exceptions */
53 static WINE_EXCEPTION_FILTER(page_fault)
55 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
56 return EXCEPTION_EXECUTE_HANDLER;
57 return EXCEPTION_CONTINUE_SEARCH;
60 static const char * const reason_names[] =
68 static const WCHAR dllW[] = {'.','d','l','l',0};
70 /* internal representation of 32bit modules. per process. */
71 typedef struct _wine_modref
75 struct _wine_modref **deps;
78 /* info about the current builtin dll load */
79 /* used to keep track of things across the register_dll constructor call */
80 struct builtin_load_info
82 const WCHAR *load_path;
87 static struct builtin_load_info default_load_info;
88 static struct builtin_load_info *builtin_load_info = &default_load_info;
90 static UINT tls_module_count; /* number of modules with TLS directory */
91 static UINT tls_total_size; /* total size of TLS storage */
92 static const IMAGE_TLS_DIRECTORY **tls_dirs; /* array of TLS directories */
94 UNICODE_STRING system_dir = { 0, 0, NULL }; /* system directory */
96 static CRITICAL_SECTION loader_section;
97 static CRITICAL_SECTION_DEBUG critsect_debug =
99 0, 0, &loader_section,
100 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
101 0, 0, { 0, (DWORD)(__FILE__ ": loader_section") }
103 static CRITICAL_SECTION loader_section = { &critsect_debug, -1, 0, 0, 0, 0 };
105 static WINE_MODREF *cached_modref;
106 static WINE_MODREF *current_modref;
107 static WINE_MODREF *last_failed_modref;
109 static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_MODREF** pwm );
110 static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
111 DWORD exp_size, const char *name, int hint );
113 /* convert PE image VirtualAddress to Real Address */
114 inline static void *get_rva( HMODULE module, DWORD va )
116 return (void *)((char *)module + va);
119 /* check whether the file name contains a path */
120 inline static int contains_path( LPCWSTR name )
122 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
125 /* convert from straight ASCII to Unicode without depending on the current codepage */
126 inline static void ascii_to_unicode( WCHAR *dst, const char *src, size_t len )
128 while (len--) *dst++ = (unsigned char)*src++;
132 /*************************************************************************
133 * call_dll_entry_point
135 * Some brain-damaged dlls (ir32_32.dll for instance) modify ebx in
136 * their entry point, so we need a small asm wrapper.
139 extern BOOL call_dll_entry_point( DLLENTRYPROC proc, void *module, UINT reason, void *reserved );
140 __ASM_GLOBAL_FUNC(call_dll_entry_point,
147 "movl 8(%ebp),%eax\n\t"
149 "leal -4(%ebp),%esp\n\t"
154 static inline BOOL call_dll_entry_point( DLLENTRYPROC proc, void *module,
155 UINT reason, void *reserved )
157 return proc( module, reason, reserved );
159 #endif /* __i386__ */
163 /*************************************************************************
166 * Entry point for stub functions.
168 static void stub_entry_point( const char *dll, const char *name, ... )
170 EXCEPTION_RECORD rec;
172 rec.ExceptionCode = EXCEPTION_WINE_STUB;
173 rec.ExceptionFlags = EH_NONCONTINUABLE;
174 rec.ExceptionRecord = NULL;
176 rec.ExceptionAddress = __builtin_return_address(0);
178 rec.ExceptionAddress = *((void **)&dll - 1);
180 rec.NumberParameters = 2;
181 rec.ExceptionInformation[0] = (ULONG_PTR)dll;
182 rec.ExceptionInformation[1] = (ULONG_PTR)name;
183 for (;;) RtlRaiseException( &rec );
187 #include <pshpack1.h>
190 BYTE popl_eax; /* popl %eax */
191 BYTE pushl1; /* pushl $name */
193 BYTE pushl2; /* pushl $dll */
195 BYTE pushl_eax; /* pushl %eax */
196 BYTE jmp; /* jmp stub_entry_point */
201 /*************************************************************************
204 * Allocate a stub entry point.
206 static void *allocate_stub( const char *dll, const char *name )
208 #define MAX_SIZE 65536
209 static struct stub *stubs;
210 static unsigned int nb_stubs;
213 if (nb_stubs >= MAX_SIZE / sizeof(*stub)) return (void *)0xdeadbeef;
217 ULONG size = MAX_SIZE;
218 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs, 0, &size,
219 MEM_COMMIT, PAGE_EXECUTE_WRITECOPY ) != STATUS_SUCCESS)
220 return (void *)0xdeadbeef;
222 stub = &stubs[nb_stubs++];
223 stub->popl_eax = 0x58; /* popl %eax */
224 stub->pushl1 = 0x68; /* pushl $name */
226 stub->pushl2 = 0x68; /* pushl $dll */
228 stub->pushl_eax = 0x50; /* pushl %eax */
229 stub->jmp = 0xe9; /* jmp stub_entry_point */
230 stub->entry = (BYTE *)stub_entry_point - (BYTE *)(&stub->entry + 1);
235 static inline void *allocate_stub( const char *dll, const char *name ) { return (void *)0xdeadbeef; }
236 #endif /* __i386__ */
239 /*************************************************************************
242 * Looks for the referenced HMODULE in the current process
243 * The loader_section must be locked while calling this function.
245 static WINE_MODREF *get_modref( HMODULE hmod )
247 PLIST_ENTRY mark, entry;
250 if (cached_modref && cached_modref->ldr.BaseAddress == hmod) return cached_modref;
252 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
253 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
255 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
256 if (mod->BaseAddress == hmod)
257 return cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
258 if (mod->BaseAddress > (void*)hmod) break;
264 /**********************************************************************
265 * find_basename_module
267 * Find a module from its base name.
268 * The loader_section must be locked while calling this function
270 static WINE_MODREF *find_basename_module( LPCWSTR name )
272 PLIST_ENTRY mark, entry;
274 if (cached_modref && !strcmpiW( name, cached_modref->ldr.BaseDllName.Buffer ))
275 return cached_modref;
277 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
278 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
280 LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
281 if (!strcmpiW( name, mod->BaseDllName.Buffer ))
283 cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
284 return cached_modref;
291 /**********************************************************************
292 * find_fullname_module
294 * Find a module from its full path name.
295 * The loader_section must be locked while calling this function
297 static WINE_MODREF *find_fullname_module( LPCWSTR name )
299 PLIST_ENTRY mark, entry;
301 if (cached_modref && !strcmpiW( name, cached_modref->ldr.FullDllName.Buffer ))
302 return cached_modref;
304 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
305 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
307 LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
308 if (!strcmpiW( name, mod->FullDllName.Buffer ))
310 cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
311 return cached_modref;
318 /*************************************************************************
319 * find_forwarded_export
321 * Find the final function pointer for a forwarded function.
322 * The loader_section must be locked while calling this function.
324 static FARPROC find_forwarded_export( HMODULE module, const char *forward )
326 const IMAGE_EXPORT_DIRECTORY *exports;
330 const char *end = strchr(forward, '.');
333 if (!end) return NULL;
334 if ((end - forward) * sizeof(WCHAR) >= sizeof(mod_name) - sizeof(dllW)) return NULL;
335 ascii_to_unicode( mod_name, forward, end - forward );
336 memcpy( mod_name + (end - forward), dllW, sizeof(dllW) );
338 if (!(wm = find_basename_module( mod_name )))
340 ERR("module not found for forward '%s' used by %s\n",
341 forward, debugstr_w(get_modref(module)->ldr.FullDllName.Buffer) );
344 if ((exports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
345 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
346 proc = find_named_export( wm->ldr.BaseAddress, exports, exp_size, end + 1, -1 );
350 ERR("function not found for forward '%s' used by %s."
351 " If you are using builtin %s, try using the native one instead.\n",
352 forward, debugstr_w(get_modref(module)->ldr.FullDllName.Buffer),
353 debugstr_w(get_modref(module)->ldr.BaseDllName.Buffer) );
359 /*************************************************************************
360 * find_ordinal_export
362 * Find an exported function by ordinal.
363 * The exports base must have been subtracted from the ordinal already.
364 * The loader_section must be locked while calling this function.
366 static FARPROC find_ordinal_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
367 DWORD exp_size, int ordinal )
370 const DWORD *functions = get_rva( module, exports->AddressOfFunctions );
372 if (ordinal >= exports->NumberOfFunctions)
374 TRACE(" ordinal %ld out of range!\n", ordinal + exports->Base );
377 if (!functions[ordinal]) return NULL;
379 proc = get_rva( module, functions[ordinal] );
381 /* if the address falls into the export dir, it's a forward */
382 if (((const char *)proc >= (const char *)exports) &&
383 ((const char *)proc < (const char *)exports + exp_size))
384 return find_forwarded_export( module, (const char *)proc );
388 const WCHAR *user = current_modref ? current_modref->ldr.BaseDllName.Buffer : NULL;
389 proc = SNOOP_GetProcAddress( module, exports, exp_size, proc, ordinal, user );
393 const WCHAR *user = current_modref ? current_modref->ldr.BaseDllName.Buffer : NULL;
394 proc = RELAY_GetProcAddress( module, exports, exp_size, proc, user );
400 /*************************************************************************
403 * Find an exported function by name.
404 * The loader_section must be locked while calling this function.
406 static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
407 DWORD exp_size, const char *name, int hint )
409 const WORD *ordinals = get_rva( module, exports->AddressOfNameOrdinals );
410 const DWORD *names = get_rva( module, exports->AddressOfNames );
411 int min = 0, max = exports->NumberOfNames - 1;
413 /* first check the hint */
414 if (hint >= 0 && hint <= max)
416 char *ename = get_rva( module, names[hint] );
417 if (!strcmp( ename, name ))
418 return find_ordinal_export( module, exports, exp_size, ordinals[hint] );
421 /* then do a binary search */
424 int res, pos = (min + max) / 2;
425 char *ename = get_rva( module, names[pos] );
426 if (!(res = strcmp( ename, name )))
427 return find_ordinal_export( module, exports, exp_size, ordinals[pos] );
428 if (res > 0) max = pos - 1;
436 /*************************************************************************
439 * Import the dll specified by the given import descriptor.
440 * The loader_section must be locked while calling this function.
442 static WINE_MODREF *import_dll( HMODULE module, const IMAGE_IMPORT_DESCRIPTOR *descr, LPCWSTR load_path )
447 const IMAGE_EXPORT_DIRECTORY *exports;
449 const IMAGE_THUNK_DATA *import_list;
450 IMAGE_THUNK_DATA *thunk_list;
452 const char *name = get_rva( module, descr->Name );
453 DWORD len = strlen(name) + 1;
455 DWORD protect_size = 0;
458 thunk_list = get_rva( module, (DWORD)descr->FirstThunk );
459 if (descr->u.OriginalFirstThunk)
460 import_list = get_rva( module, (DWORD)descr->u.OriginalFirstThunk );
462 import_list = thunk_list;
464 if (len * sizeof(WCHAR) <= sizeof(buffer))
466 ascii_to_unicode( buffer, name, len );
467 status = load_dll( load_path, buffer, 0, &wmImp );
469 else /* need to allocate a larger buffer */
471 WCHAR *ptr = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) );
472 if (!ptr) return NULL;
473 ascii_to_unicode( ptr, name, len );
474 status = load_dll( load_path, ptr, 0, &wmImp );
475 RtlFreeHeap( GetProcessHeap(), 0, ptr );
480 if (status == STATUS_DLL_NOT_FOUND)
481 ERR("Library %s (which is needed by %s) not found\n",
482 name, debugstr_w(current_modref->ldr.FullDllName.Buffer));
484 ERR("Loading library %s (which is needed by %s) failed (error %lx).\n",
485 name, debugstr_w(current_modref->ldr.FullDllName.Buffer), status);
489 /* unprotect the import address table since it can be located in
490 * readonly section */
491 while (import_list[protect_size].u1.Ordinal) protect_size++;
492 protect_base = thunk_list;
493 protect_size *= sizeof(*thunk_list);
494 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base,
495 &protect_size, PAGE_WRITECOPY, &protect_old );
497 imp_mod = wmImp->ldr.BaseAddress;
498 exports = RtlImageDirectoryEntryToData( imp_mod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size );
502 /* set all imported function to deadbeef */
503 while (import_list->u1.Ordinal)
505 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
507 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
508 WARN("No implementation for %s.%d", name, ordinal );
509 thunk_list->u1.Function = allocate_stub( name, (const char *)ordinal );
513 IMAGE_IMPORT_BY_NAME *pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
514 WARN("No implementation for %s.%s", name, pe_name->Name );
515 thunk_list->u1.Function = allocate_stub( name, pe_name->Name );
517 WARN(" imported from %s, allocating stub %p\n",
518 debugstr_w(current_modref->ldr.FullDllName.Buffer),
519 thunk_list->u1.Function );
526 while (import_list->u1.Ordinal)
528 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
530 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
532 thunk_list->u1.Function = (PDWORD)find_ordinal_export( imp_mod, exports, exp_size,
533 ordinal - exports->Base );
534 if (!thunk_list->u1.Function)
536 thunk_list->u1.Function = allocate_stub( name, (const char *)ordinal );
537 WARN("No implementation for %s.%d imported from %s, setting to %p\n",
538 name, ordinal, debugstr_w(current_modref->ldr.FullDllName.Buffer),
539 thunk_list->u1.Function );
541 TRACE("--- Ordinal %s.%d = %p\n", name, ordinal, thunk_list->u1.Function );
543 else /* import by name */
545 IMAGE_IMPORT_BY_NAME *pe_name;
546 pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
547 thunk_list->u1.Function = (PDWORD)find_named_export( imp_mod, exports, exp_size,
548 pe_name->Name, pe_name->Hint );
549 if (!thunk_list->u1.Function)
551 thunk_list->u1.Function = allocate_stub( name, pe_name->Name );
552 WARN("No implementation for %s.%s imported from %s, setting to %p\n",
553 name, pe_name->Name, debugstr_w(current_modref->ldr.FullDllName.Buffer),
554 thunk_list->u1.Function );
556 TRACE("--- %s %s.%d = %p\n", pe_name->Name, name, pe_name->Hint, thunk_list->u1.Function);
563 /* restore old protection of the import address table */
564 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base, &protect_size, protect_old, NULL );
569 /****************************************************************
572 * Fixup all imports of a given module.
573 * The loader_section must be locked while calling this function.
575 static NTSTATUS fixup_imports( WINE_MODREF *wm, LPCWSTR load_path )
578 const IMAGE_IMPORT_DESCRIPTOR *imports;
583 if (!(imports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
584 IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
585 return STATUS_SUCCESS;
587 nb_imports = size / sizeof(*imports);
588 for (i = 0; i < nb_imports; i++)
590 if (!imports[i].Name)
596 if (!nb_imports) return STATUS_SUCCESS; /* no imports */
598 /* Allocate module dependency list */
599 wm->nDeps = nb_imports;
600 wm->deps = RtlAllocateHeap( GetProcessHeap(), 0, nb_imports*sizeof(WINE_MODREF *) );
602 /* load the imported modules. They are automatically
603 * added to the modref list of the process.
605 prev = current_modref;
607 status = STATUS_SUCCESS;
608 for (i = 0; i < nb_imports; i++)
610 if (!(wm->deps[i] = import_dll( wm->ldr.BaseAddress, &imports[i], load_path )))
611 status = STATUS_DLL_NOT_FOUND;
613 current_modref = prev;
618 /*************************************************************************
621 * Allocate a WINE_MODREF structure and add it to the process list
622 * The loader_section must be locked while calling this function.
624 static WINE_MODREF *alloc_module( HMODULE hModule, LPCWSTR filename )
628 const IMAGE_NT_HEADERS *nt = RtlImageNtHeader(hModule);
629 PLIST_ENTRY entry, mark;
631 if (!(wm = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm) ))) return NULL;
636 wm->ldr.BaseAddress = hModule;
637 wm->ldr.EntryPoint = NULL;
638 wm->ldr.SizeOfImage = nt->OptionalHeader.SizeOfImage;
640 wm->ldr.LoadCount = 0;
641 wm->ldr.TlsIndex = -1;
642 wm->ldr.SectionHandle = NULL;
643 wm->ldr.CheckSum = 0;
644 wm->ldr.TimeDateStamp = 0;
646 RtlCreateUnicodeString( &wm->ldr.FullDllName, filename );
647 if ((p = strrchrW( wm->ldr.FullDllName.Buffer, '\\' ))) p++;
648 else p = wm->ldr.FullDllName.Buffer;
649 RtlInitUnicodeString( &wm->ldr.BaseDllName, p );
651 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
653 wm->ldr.Flags |= LDR_IMAGE_IS_DLL;
654 if (nt->OptionalHeader.AddressOfEntryPoint)
655 wm->ldr.EntryPoint = (char *)hModule + nt->OptionalHeader.AddressOfEntryPoint;
658 InsertTailList(&NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList,
659 &wm->ldr.InLoadOrderModuleList);
661 /* insert module in MemoryList, sorted in increasing base addresses */
662 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
663 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
665 if (CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList)->BaseAddress > wm->ldr.BaseAddress)
668 entry->Blink->Flink = &wm->ldr.InMemoryOrderModuleList;
669 wm->ldr.InMemoryOrderModuleList.Blink = entry->Blink;
670 wm->ldr.InMemoryOrderModuleList.Flink = entry;
671 entry->Blink = &wm->ldr.InMemoryOrderModuleList;
673 /* wait until init is called for inserting into this list */
674 wm->ldr.InInitializationOrderModuleList.Flink = NULL;
675 wm->ldr.InInitializationOrderModuleList.Blink = NULL;
680 /*************************************************************************
683 * Allocate the process-wide structure for module TLS storage.
685 static NTSTATUS alloc_process_tls(void)
687 PLIST_ENTRY mark, entry;
689 const IMAGE_TLS_DIRECTORY *dir;
692 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
693 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
695 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
696 if (!(dir = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE,
697 IMAGE_DIRECTORY_ENTRY_TLS, &size )))
699 size = (dir->EndAddressOfRawData - dir->StartAddressOfRawData) + dir->SizeOfZeroFill;
701 tls_total_size += size;
704 if (!tls_module_count) return STATUS_SUCCESS;
706 TRACE( "count %u size %u\n", tls_module_count, tls_total_size );
708 tls_dirs = RtlAllocateHeap( GetProcessHeap(), 0, tls_module_count * sizeof(*tls_dirs) );
709 if (!tls_dirs) return STATUS_NO_MEMORY;
711 for (i = 0, entry = mark->Flink; entry != mark; entry = entry->Flink)
713 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
714 if (!(dir = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE,
715 IMAGE_DIRECTORY_ENTRY_TLS, &size )))
718 *dir->AddressOfIndex = i;
720 mod->LoadCount = -1; /* can't unload it */
723 return STATUS_SUCCESS;
727 /*************************************************************************
730 * Allocate the per-thread structure for module TLS storage.
732 static NTSTATUS alloc_thread_tls(void)
738 if (!tls_module_count) return STATUS_SUCCESS;
740 if (!(pointers = RtlAllocateHeap( GetProcessHeap(), 0,
741 tls_module_count * sizeof(*pointers) )))
742 return STATUS_NO_MEMORY;
744 if (!(data = RtlAllocateHeap( GetProcessHeap(), 0, tls_total_size )))
746 RtlFreeHeap( GetProcessHeap(), 0, pointers );
747 return STATUS_NO_MEMORY;
750 for (i = 0; i < tls_module_count; i++)
752 const IMAGE_TLS_DIRECTORY *dir = tls_dirs[i];
753 ULONG size = dir->EndAddressOfRawData - dir->StartAddressOfRawData;
755 TRACE( "thread %04lx idx %d: %ld/%ld bytes from %p to %p\n",
756 GetCurrentThreadId(), i, size, dir->SizeOfZeroFill,
757 (void *)dir->StartAddressOfRawData, data );
760 memcpy( data, (void *)dir->StartAddressOfRawData, size );
762 memset( data, 0, dir->SizeOfZeroFill );
763 data += dir->SizeOfZeroFill;
765 NtCurrentTeb()->ThreadLocalStoragePointer = pointers;
766 return STATUS_SUCCESS;
770 /*************************************************************************
773 static void call_tls_callbacks( HMODULE module, UINT reason )
775 const IMAGE_TLS_DIRECTORY *dir;
776 const PIMAGE_TLS_CALLBACK *callback;
779 dir = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_TLS, &dirsize );
780 if (!dir || !dir->AddressOfCallBacks) return;
782 for (callback = dir->AddressOfCallBacks; *callback; callback++)
785 DPRINTF("%04lx:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
786 GetCurrentThreadId(), *callback, module, reason_names[reason] );
787 (*callback)( module, reason, NULL );
789 DPRINTF("%04lx:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
790 GetCurrentThreadId(), *callback, module, reason_names[reason] );
795 /*************************************************************************
798 static BOOL MODULE_InitDLL( WINE_MODREF *wm, UINT reason, LPVOID lpReserved )
802 DLLENTRYPROC entry = wm->ldr.EntryPoint;
803 void *module = wm->ldr.BaseAddress;
805 /* Skip calls for modules loaded with special load flags */
807 if (wm->ldr.Flags & LDR_DONT_RESOLVE_REFS) return TRUE;
808 if (wm->ldr.TlsIndex != -1) call_tls_callbacks( wm->ldr.BaseAddress, reason );
809 if (!entry) return TRUE;
813 size_t len = min( wm->ldr.BaseDllName.Length, sizeof(mod_name)-sizeof(WCHAR) );
814 memcpy( mod_name, wm->ldr.BaseDllName.Buffer, len );
815 mod_name[len / sizeof(WCHAR)] = 0;
816 DPRINTF("%04lx:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
817 GetCurrentThreadId(), entry, module, debugstr_w(mod_name),
818 reason_names[reason], lpReserved );
820 else TRACE("(%p %s,%s,%p) - CALL\n", module, debugstr_w(wm->ldr.BaseDllName.Buffer),
821 reason_names[reason], lpReserved );
823 retv = call_dll_entry_point( entry, module, reason, lpReserved );
825 /* The state of the module list may have changed due to the call
826 to the dll. We cannot assume that this module has not been
829 DPRINTF("%04lx:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
830 GetCurrentThreadId(), entry, module, debugstr_w(mod_name),
831 reason_names[reason], lpReserved, retv );
832 else TRACE("(%p,%s,%p) - RETURN %d\n", module, reason_names[reason], lpReserved, retv );
838 /*************************************************************************
841 * Send the process attach notification to all DLLs the given module
842 * depends on (recursively). This is somewhat complicated due to the fact that
844 * - we have to respect the module dependencies, i.e. modules implicitly
845 * referenced by another module have to be initialized before the module
846 * itself can be initialized
848 * - the initialization routine of a DLL can itself call LoadLibrary,
849 * thereby introducing a whole new set of dependencies (even involving
850 * the 'old' modules) at any time during the whole process
852 * (Note that this routine can be recursively entered not only directly
853 * from itself, but also via LoadLibrary from one of the called initialization
856 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
857 * the process *detach* notifications to be sent in the correct order.
858 * This must not only take into account module dependencies, but also
859 * 'hidden' dependencies created by modules calling LoadLibrary in their
860 * attach notification routine.
862 * The strategy is rather simple: we move a WINE_MODREF to the head of the
863 * list after the attach notification has returned. This implies that the
864 * detach notifications are called in the reverse of the sequence the attach
865 * notifications *returned*.
867 * The loader_section must be locked while calling this function.
869 static NTSTATUS process_attach( WINE_MODREF *wm, LPVOID lpReserved )
871 NTSTATUS status = STATUS_SUCCESS;
874 /* prevent infinite recursion in case of cyclical dependencies */
875 if ( ( wm->ldr.Flags & LDR_LOAD_IN_PROGRESS )
876 || ( wm->ldr.Flags & LDR_PROCESS_ATTACHED ) )
879 TRACE("(%s,%p) - START\n", debugstr_w(wm->ldr.BaseDllName.Buffer), lpReserved );
881 /* Tag current MODREF to prevent recursive loop */
882 wm->ldr.Flags |= LDR_LOAD_IN_PROGRESS;
884 /* Recursively attach all DLLs this one depends on */
885 for ( i = 0; i < wm->nDeps; i++ )
887 if (!wm->deps[i]) continue;
888 if ((status = process_attach( wm->deps[i], lpReserved )) != STATUS_SUCCESS) break;
891 /* Call DLL entry point */
892 if (status == STATUS_SUCCESS)
894 WINE_MODREF *prev = current_modref;
896 if (MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved ))
898 wm->ldr.Flags |= LDR_PROCESS_ATTACHED;
902 /* point to the name so LdrInitializeThunk can print it */
903 last_failed_modref = wm;
904 WARN("Initialization of %s failed\n", debugstr_w(wm->ldr.BaseDllName.Buffer));
905 status = STATUS_DLL_INIT_FAILED;
907 current_modref = prev;
910 InsertTailList(&NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList,
911 &wm->ldr.InInitializationOrderModuleList);
913 /* Remove recursion flag */
914 wm->ldr.Flags &= ~LDR_LOAD_IN_PROGRESS;
916 TRACE("(%s,%p) - END\n", debugstr_w(wm->ldr.BaseDllName.Buffer), lpReserved );
920 /*************************************************************************
923 * Send DLL process detach notifications. See the comment about calling
924 * sequence at process_attach. Unless the bForceDetach flag
925 * is set, only DLLs with zero refcount are notified.
927 static void process_detach( BOOL bForceDetach, LPVOID lpReserved )
929 PLIST_ENTRY mark, entry;
932 RtlEnterCriticalSection( &loader_section );
933 if (bForceDetach) process_detaching = 1;
934 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
937 for (entry = mark->Blink; entry != mark; entry = entry->Blink)
939 mod = CONTAINING_RECORD(entry, LDR_MODULE,
940 InInitializationOrderModuleList);
941 /* Check whether to detach this DLL */
942 if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
944 if ( mod->LoadCount && !bForceDetach )
947 /* Call detach notification */
948 mod->Flags &= ~LDR_PROCESS_ATTACHED;
949 MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
950 DLL_PROCESS_DETACH, lpReserved );
952 /* Restart at head of WINE_MODREF list, as entries might have
953 been added and/or removed while performing the call ... */
956 } while (entry != mark);
958 RtlLeaveCriticalSection( &loader_section );
961 /*************************************************************************
962 * MODULE_DllThreadAttach
964 * Send DLL thread attach notifications. These are sent in the
965 * reverse sequence of process detach notification.
968 NTSTATUS MODULE_DllThreadAttach( LPVOID lpReserved )
970 PLIST_ENTRY mark, entry;
974 /* don't do any attach calls if process is exiting */
975 if (process_detaching) return STATUS_SUCCESS;
976 /* FIXME: there is still a race here */
978 RtlEnterCriticalSection( &loader_section );
980 if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto done;
982 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
983 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
985 mod = CONTAINING_RECORD(entry, LDR_MODULE,
986 InInitializationOrderModuleList);
987 if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
989 if ( mod->Flags & LDR_NO_DLL_CALLS )
992 MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
993 DLL_THREAD_ATTACH, lpReserved );
997 RtlLeaveCriticalSection( &loader_section );
1001 /******************************************************************
1002 * LdrDisableThreadCalloutsForDll (NTDLL.@)
1005 NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HMODULE hModule)
1008 NTSTATUS ret = STATUS_SUCCESS;
1010 RtlEnterCriticalSection( &loader_section );
1012 wm = get_modref( hModule );
1013 if (!wm || wm->ldr.TlsIndex != -1)
1014 ret = STATUS_DLL_NOT_FOUND;
1016 wm->ldr.Flags |= LDR_NO_DLL_CALLS;
1018 RtlLeaveCriticalSection( &loader_section );
1023 /******************************************************************
1024 * LdrFindEntryForAddress (NTDLL.@)
1026 * The loader_section must be locked while calling this function
1028 NTSTATUS WINAPI LdrFindEntryForAddress(const void* addr, PLDR_MODULE* pmod)
1030 PLIST_ENTRY mark, entry;
1033 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
1034 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1036 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
1037 if ((const void *)mod->BaseAddress <= addr &&
1038 (const char *)addr < (char*)mod->BaseAddress + mod->SizeOfImage)
1041 return STATUS_SUCCESS;
1043 if ((const void *)mod->BaseAddress > addr) break;
1045 return STATUS_NO_MORE_ENTRIES;
1048 /******************************************************************
1049 * LdrLockLoaderLock (NTDLL.@)
1051 * Note: flags are not implemented.
1052 * Flag 0x01 is used to raise exceptions on errors.
1053 * Flag 0x02 is used to avoid waiting on the section (does RtlTryEnterCriticalSection instead).
1055 NTSTATUS WINAPI LdrLockLoaderLock( ULONG flags, ULONG *result, ULONG *magic )
1057 if (flags) FIXME( "flags %lx not supported\n", flags );
1059 if (result) *result = 1;
1060 if (!magic) return STATUS_INVALID_PARAMETER_3;
1061 RtlEnterCriticalSection( &loader_section );
1062 *magic = GetCurrentThreadId();
1063 return STATUS_SUCCESS;
1067 /******************************************************************
1068 * LdrUnlockLoaderUnlock (NTDLL.@)
1070 NTSTATUS WINAPI LdrUnlockLoaderLock( ULONG flags, ULONG magic )
1074 if (magic != GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2;
1075 RtlLeaveCriticalSection( &loader_section );
1077 return STATUS_SUCCESS;
1081 /******************************************************************
1082 * LdrGetDllHandle (NTDLL.@)
1084 NTSTATUS WINAPI LdrGetDllHandle(ULONG x, ULONG y, const UNICODE_STRING *name, HMODULE *base)
1086 NTSTATUS status = STATUS_DLL_NOT_FOUND;
1087 WCHAR dllname[MAX_PATH+4], *p;
1089 PLIST_ENTRY mark, entry;
1092 if (x != 0 || y != 0)
1093 FIXME("Unknown behavior, please report\n");
1095 /* Append .DLL to name if no extension present */
1096 if (!(p = strrchrW( name->Buffer, '.')) || strchrW( p, '/' ) || strchrW( p, '\\'))
1098 if (name->Length >= MAX_PATH) return STATUS_NAME_TOO_LONG;
1099 strcpyW( dllname, name->Buffer );
1100 strcatW( dllname, dllW );
1101 RtlInitUnicodeString( &str, dllname );
1105 RtlEnterCriticalSection( &loader_section );
1109 if (RtlEqualUnicodeString( name, &cached_modref->ldr.FullDllName, TRUE ) ||
1110 RtlEqualUnicodeString( name, &cached_modref->ldr.BaseDllName, TRUE ))
1112 *base = cached_modref->ldr.BaseAddress;
1113 status = STATUS_SUCCESS;
1118 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
1119 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1121 mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
1123 if (RtlEqualUnicodeString( name, &mod->FullDllName, TRUE ) ||
1124 RtlEqualUnicodeString( name, &mod->BaseDllName, TRUE ))
1126 cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
1127 *base = mod->BaseAddress;
1128 status = STATUS_SUCCESS;
1133 RtlLeaveCriticalSection( &loader_section );
1134 TRACE("%lx %lx %s -> %p\n", x, y, debugstr_us(name), status ? NULL : *base);
1139 /******************************************************************
1140 * LdrGetProcedureAddress (NTDLL.@)
1142 NTSTATUS WINAPI LdrGetProcedureAddress(HMODULE module, const ANSI_STRING *name,
1143 ULONG ord, PVOID *address)
1145 IMAGE_EXPORT_DIRECTORY *exports;
1147 NTSTATUS ret = STATUS_PROCEDURE_NOT_FOUND;
1149 RtlEnterCriticalSection( &loader_section );
1151 if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
1152 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
1154 void *proc = name ? find_named_export( module, exports, exp_size, name->Buffer, -1 )
1155 : find_ordinal_export( module, exports, exp_size, ord - exports->Base );
1159 ret = STATUS_SUCCESS;
1164 /* check if the module itself is invalid to return the proper error */
1165 if (!get_modref( module )) ret = STATUS_DLL_NOT_FOUND;
1168 RtlLeaveCriticalSection( &loader_section );
1173 /***********************************************************************
1174 * load_builtin_callback
1176 * Load a library in memory; callback function for wine_dll_register
1178 static void load_builtin_callback( void *module, const char *filename )
1180 static const WCHAR emptyW[1];
1182 IMAGE_NT_HEADERS *nt;
1184 WCHAR *fullname, *p;
1185 const WCHAR *load_path;
1189 ERR("could not map image for %s\n", filename ? filename : "main exe" );
1192 if (!(nt = RtlImageNtHeader( module )))
1194 ERR( "bad module for %s\n", filename ? filename : "main exe" );
1195 builtin_load_info->status = STATUS_INVALID_IMAGE_FORMAT;
1198 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
1200 /* if we already have an executable, ignore this one */
1201 if (!NtCurrentTeb()->Peb->ImageBaseAddress)
1203 NtCurrentTeb()->Peb->ImageBaseAddress = module;
1204 return; /* don't create the modref here, will be done later on */
1208 /* create the MODREF */
1210 if (!(fullname = RtlAllocateHeap( GetProcessHeap(), 0,
1211 system_dir.MaximumLength + (strlen(filename) + 1) * sizeof(WCHAR) )))
1213 ERR( "can't load %s\n", filename );
1214 builtin_load_info->status = STATUS_NO_MEMORY;
1217 memcpy( fullname, system_dir.Buffer, system_dir.Length );
1218 p = fullname + system_dir.Length / sizeof(WCHAR);
1219 if (p > fullname && p[-1] != '\\') *p++ = '\\';
1220 ascii_to_unicode( p, filename, strlen(filename) + 1 );
1222 wm = alloc_module( module, fullname );
1223 RtlFreeHeap( GetProcessHeap(), 0, fullname );
1226 ERR( "can't load %s\n", filename );
1227 builtin_load_info->status = STATUS_NO_MEMORY;
1230 wm->ldr.Flags |= LDR_WINE_INTERNAL;
1232 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &nt->OptionalHeader.SizeOfImage,
1233 MEM_SYSTEM | MEM_IMAGE, PAGE_EXECUTE_WRITECOPY );
1237 load_path = builtin_load_info->load_path;
1238 if (!load_path) load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
1239 if (!load_path) load_path = emptyW;
1240 if (fixup_imports( wm, load_path ) != STATUS_SUCCESS)
1242 /* the module has only be inserted in the load & memory order lists */
1243 RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
1244 RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
1245 /* FIXME: free the modref */
1246 builtin_load_info->status = STATUS_DLL_NOT_FOUND;
1249 builtin_load_info->wm = wm;
1250 TRACE( "loaded %s %p %p\n", filename, wm, module );
1252 /* send the DLL load event */
1254 SERVER_START_REQ( load_dll )
1258 req->size = nt->OptionalHeader.SizeOfImage;
1259 req->dbg_offset = nt->FileHeader.PointerToSymbolTable;
1260 req->dbg_size = nt->FileHeader.NumberOfSymbols;
1261 req->name = &wm->ldr.FullDllName.Buffer;
1262 wine_server_add_data( req, wm->ldr.FullDllName.Buffer, wm->ldr.FullDllName.Length );
1263 wine_server_call( req );
1267 /* setup relay debugging entry points */
1268 if (TRACE_ON(relay)) RELAY_SetupDLL( module );
1272 /******************************************************************************
1273 * load_native_dll (internal)
1275 static NTSTATUS load_native_dll( LPCWSTR load_path, LPCWSTR name, HANDLE file,
1276 DWORD flags, WINE_MODREF** pwm )
1280 OBJECT_ATTRIBUTES attr;
1282 IMAGE_NT_HEADERS *nt;
1287 TRACE( "loading %s\n", debugstr_w(name) );
1289 attr.Length = sizeof(attr);
1290 attr.RootDirectory = 0;
1291 attr.ObjectName = NULL;
1292 attr.Attributes = 0;
1293 attr.SecurityDescriptor = NULL;
1294 attr.SecurityQualityOfService = NULL;
1297 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1298 &attr, &size, 0, SEC_IMAGE, file );
1299 if (status != STATUS_SUCCESS) return status;
1302 status = NtMapViewOfSection( mapping, NtCurrentProcess(),
1303 &module, 0, 0, &size, &len, ViewShare, 0, PAGE_READONLY );
1305 if (status != STATUS_SUCCESS) return status;
1307 /* create the MODREF */
1309 if (!(wm = alloc_module( module, name ))) return STATUS_NO_MEMORY;
1313 if (!(flags & DONT_RESOLVE_DLL_REFERENCES))
1315 if ((status = fixup_imports( wm, load_path )) != STATUS_SUCCESS)
1317 /* the module has only be inserted in the load & memory order lists */
1318 RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
1319 RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
1321 /* FIXME: there are several more dangling references
1322 * left. Including dlls loaded by this dll before the
1323 * failed one. Unrolling is rather difficult with the
1324 * current structure and we can leave them lying
1325 * around with no problems, so we don't care.
1326 * As these might reference our wm, we don't free it.
1331 else wm->ldr.Flags |= LDR_DONT_RESOLVE_REFS;
1333 /* send DLL load event */
1335 nt = RtlImageNtHeader( module );
1337 /* don't keep the file open if the mapping is from removable media */
1338 if (!VIRTUAL_HasMapping( module )) file = 0;
1340 SERVER_START_REQ( load_dll )
1344 req->size = nt->OptionalHeader.SizeOfImage;
1345 req->dbg_offset = nt->FileHeader.PointerToSymbolTable;
1346 req->dbg_size = nt->FileHeader.NumberOfSymbols;
1347 req->name = &wm->ldr.FullDllName.Buffer;
1348 wine_server_add_data( req, wm->ldr.FullDllName.Buffer, wm->ldr.FullDllName.Length );
1349 wine_server_call( req );
1353 if (TRACE_ON(snoop)) SNOOP_SetupDLL( module );
1356 return STATUS_SUCCESS;
1360 /***********************************************************************
1363 static NTSTATUS load_builtin_dll( LPCWSTR load_path, LPCWSTR path, DWORD flags, WINE_MODREF** pwm )
1365 char error[256], dllname[MAX_PATH];
1367 const WCHAR *name, *p;
1370 struct builtin_load_info info, *prev_info;
1372 /* Fix the name in case we have a full path and extension */
1374 if ((p = strrchrW( name, '\\' ))) name = p + 1;
1375 if ((p = strrchrW( name, '/' ))) name = p + 1;
1377 /* we don't want to depend on the current codepage here */
1378 len = strlenW( name ) + 1;
1379 if (len >= sizeof(dllname)) return STATUS_NAME_TOO_LONG;
1380 for (i = 0; i < len; i++)
1382 if (name[i] > 127) return STATUS_DLL_NOT_FOUND;
1383 dllname[i] = (char)name[i];
1384 if (dllname[i] >= 'A' && dllname[i] <= 'Z') dllname[i] += 'a' - 'A';
1387 /* load_library will modify info.status. Note also that load_library can be
1388 * called several times, if the .so file we're loading has dependencies.
1389 * info.status will gather all the errors we may get while loading all these
1392 info.load_path = load_path;
1393 info.status = STATUS_SUCCESS;
1395 prev_info = builtin_load_info;
1396 builtin_load_info = &info;
1397 handle = wine_dll_load( dllname, error, sizeof(error), &file_exists );
1398 builtin_load_info = prev_info;
1404 /* The file does not exist -> WARN() */
1405 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name), error);
1406 return STATUS_DLL_NOT_FOUND;
1408 /* ERR() for all other errors (missing functions, ...) */
1409 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name), error );
1410 return STATUS_PROCEDURE_NOT_FOUND;
1412 if (info.status != STATUS_SUCCESS) return info.status;
1416 /* The constructor wasn't called, this means the .so is already
1417 * loaded under a different name. We can't support multiple names
1418 * for the same module, so return an error. */
1419 return STATUS_INVALID_IMAGE_FORMAT;
1422 info.wm->ldr.SectionHandle = handle;
1423 if (strcmpiW( info.wm->ldr.BaseDllName.Buffer, name ))
1425 ERR( "loaded .so for %s but got %s instead - probably 16-bit dll\n",
1426 debugstr_w(name), debugstr_w(info.wm->ldr.BaseDllName.Buffer) );
1427 /* wine_dll_unload( handle );*/
1428 return STATUS_INVALID_IMAGE_FORMAT;
1431 return STATUS_SUCCESS;
1435 /***********************************************************************
1438 * Find the file (or already loaded module) for a given dll name.
1440 static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname,
1441 WCHAR *filename, ULONG *size, WINE_MODREF **pwm, HANDLE *handle )
1443 OBJECT_ATTRIBUTES attr;
1445 UNICODE_STRING nt_name;
1446 WCHAR *file_part, *ext, *dllname;
1449 /* first append .dll if needed */
1452 if (!(ext = strrchrW( libname, '.')) || strchrW( ext, '/' ) || strchrW( ext, '\\'))
1454 if (!(dllname = RtlAllocateHeap( GetProcessHeap(), 0,
1455 (strlenW(libname) * sizeof(WCHAR)) + sizeof(dllW) )))
1456 return STATUS_NO_MEMORY;
1457 strcpyW( dllname, libname );
1458 strcatW( dllname, dllW );
1462 nt_name.Buffer = NULL;
1463 if (RtlDetermineDosPathNameType_U( libname ) == RELATIVE_PATH)
1465 /* we need to search for it */
1466 len = RtlDosSearchPath_U( load_path, libname, NULL, *size, filename, &file_part );
1469 if (len >= *size) goto overflow;
1470 if ((*pwm = find_fullname_module( filename )) != NULL) goto found;
1472 /* check for already loaded module in a different path */
1473 if (!contains_path( libname ))
1475 if ((*pwm = find_basename_module( file_part )) != NULL) goto found;
1477 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, NULL, NULL ))
1479 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1480 return STATUS_NO_MEMORY;
1482 attr.Length = sizeof(attr);
1483 attr.RootDirectory = 0;
1484 attr.Attributes = OBJ_CASE_INSENSITIVE;
1485 attr.ObjectName = &nt_name;
1486 attr.SecurityDescriptor = NULL;
1487 attr.SecurityQualityOfService = NULL;
1488 if (NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_DELETE, 0 )) *handle = 0;
1494 if (!contains_path( libname ))
1496 /* if libname doesn't contain a path at all, we simply return the name as is,
1497 * to be loaded as builtin */
1498 len = strlenW(libname) * sizeof(WCHAR);
1499 if (len >= *size) goto overflow;
1500 strcpyW( filename, libname );
1501 *pwm = find_basename_module( filename );
1506 /* absolute path name, or relative path name but not found above */
1508 if (!RtlDosPathNameToNtPathName_U( libname, &nt_name, &file_part, NULL ))
1510 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1511 return STATUS_NO_MEMORY;
1513 len = nt_name.Length - 4*sizeof(WCHAR); /* for \??\ prefix */
1514 if (len >= *size) goto overflow;
1515 memcpy( filename, nt_name.Buffer + 4, len + sizeof(WCHAR) );
1516 if (!(*pwm = find_fullname_module( filename )))
1518 attr.Length = sizeof(attr);
1519 attr.RootDirectory = 0;
1520 attr.Attributes = OBJ_CASE_INSENSITIVE;
1521 attr.ObjectName = &nt_name;
1522 attr.SecurityDescriptor = NULL;
1523 attr.SecurityQualityOfService = NULL;
1524 if (NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_DELETE, 0 )) *handle = 0;
1527 RtlFreeUnicodeString( &nt_name );
1528 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1529 return STATUS_SUCCESS;
1532 RtlFreeUnicodeString( &nt_name );
1533 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1534 *size = len + sizeof(WCHAR);
1535 return STATUS_BUFFER_TOO_SMALL;
1539 /***********************************************************************
1540 * load_dll (internal)
1542 * Load a PE style module according to the load order.
1543 * The loader_section must be locked while calling this function.
1545 static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_MODREF** pwm )
1548 enum loadorder_type loadorder[LOADORDER_NTYPES];
1552 const char *filetype = "";
1553 WINE_MODREF *main_exe;
1557 TRACE( "looking for %s in %s\n", debugstr_w(libname), debugstr_w(load_path) );
1560 size = sizeof(buffer);
1563 nts = find_dll_file( load_path, libname, filename, &size, pwm, &handle );
1564 if (nts == STATUS_SUCCESS) break;
1565 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1566 if (nts != STATUS_BUFFER_TOO_SMALL) return nts;
1567 /* grow the buffer and retry */
1568 if (!(filename = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return STATUS_NO_MEMORY;
1571 if (*pwm) /* found already loaded module */
1573 if ((*pwm)->ldr.LoadCount != -1) (*pwm)->ldr.LoadCount++;
1575 if (((*pwm)->ldr.Flags & LDR_DONT_RESOLVE_REFS) &&
1576 !(flags & DONT_RESOLVE_DLL_REFERENCES))
1578 (*pwm)->ldr.Flags &= ~LDR_DONT_RESOLVE_REFS;
1579 fixup_imports( *pwm, load_path );
1581 TRACE("Found loaded module %s for %s at %p, count=%d\n",
1582 debugstr_w((*pwm)->ldr.FullDllName.Buffer), debugstr_w(libname),
1583 (*pwm)->ldr.BaseAddress, (*pwm)->ldr.LoadCount);
1584 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1585 return STATUS_SUCCESS;
1588 main_exe = get_modref( NtCurrentTeb()->Peb->ImageBaseAddress );
1589 MODULE_GetLoadOrderW( loadorder, main_exe ? main_exe->ldr.BaseDllName.Buffer : NULL, filename );
1591 nts = STATUS_DLL_NOT_FOUND;
1592 for (i = 0; i < LOADORDER_NTYPES; i++)
1594 if (loadorder[i] == LOADORDER_INVALID) break;
1596 switch (loadorder[i])
1599 TRACE("Trying native dll %s\n", debugstr_w(filename));
1600 if (!handle) continue; /* it cannot possibly be loaded */
1601 nts = load_native_dll( load_path, filename, handle, flags, pwm );
1602 filetype = "native";
1605 TRACE("Trying built-in %s\n", debugstr_w(filename));
1606 nts = load_builtin_dll( load_path, filename, flags, pwm );
1607 filetype = "builtin";
1610 nts = STATUS_INTERNAL_ERROR;
1614 if (nts == STATUS_SUCCESS)
1616 /* Initialize DLL just loaded */
1617 TRACE("Loaded module %s (%s) at %p\n",
1618 debugstr_w(filename), filetype, (*pwm)->ldr.BaseAddress);
1619 if (!TRACE_ON(module))
1620 TRACE_(loaddll)("Loaded module %s : %s\n",
1621 debugstr_w((*pwm)->ldr.FullDllName.Buffer), filetype);
1622 /* Set the ldr.LoadCount here so that an attach failure will */
1623 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1624 (*pwm)->ldr.LoadCount = 1;
1625 if (handle) NtClose( handle );
1626 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1629 if (nts != STATUS_DLL_NOT_FOUND) break;
1632 WARN("Failed to load module %s; status=%lx\n", debugstr_w(libname), nts);
1633 if (handle) NtClose( handle );
1634 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1638 /******************************************************************
1639 * LdrLoadDll (NTDLL.@)
1641 NTSTATUS WINAPI LdrLoadDll(LPCWSTR path_name, DWORD flags,
1642 const UNICODE_STRING *libname, HMODULE* hModule)
1647 RtlEnterCriticalSection( &loader_section );
1649 if (!path_name) path_name = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
1650 nts = load_dll( path_name, libname->Buffer, flags, &wm );
1652 if (nts == STATUS_SUCCESS && !(wm->ldr.Flags & LDR_DONT_RESOLVE_REFS))
1654 nts = process_attach( wm, NULL );
1655 if (nts != STATUS_SUCCESS)
1657 WARN("Attach failed for module %s\n", debugstr_w(libname->Buffer));
1658 LdrUnloadDll(wm->ldr.BaseAddress);
1662 *hModule = (wm) ? wm->ldr.BaseAddress : NULL;
1664 RtlLeaveCriticalSection( &loader_section );
1668 /******************************************************************
1669 * LdrQueryProcessModuleInformation
1672 NTSTATUS WINAPI LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi,
1673 ULONG buf_size, ULONG* req_size)
1675 SYSTEM_MODULE* sm = &smi->Modules[0];
1676 ULONG size = sizeof(ULONG);
1677 NTSTATUS nts = STATUS_SUCCESS;
1680 PLIST_ENTRY mark, entry;
1683 smi->ModulesCount = 0;
1685 RtlEnterCriticalSection( &loader_section );
1686 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
1687 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1689 mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
1690 size += sizeof(*sm);
1691 if (size <= buf_size)
1693 sm->Reserved1 = 0; /* FIXME */
1694 sm->Reserved2 = 0; /* FIXME */
1695 sm->ImageBaseAddress = mod->BaseAddress;
1696 sm->ImageSize = mod->SizeOfImage;
1697 sm->Flags = mod->Flags;
1698 sm->Id = 0; /* FIXME */
1699 sm->Rank = 0; /* FIXME */
1700 sm->Unknown = 0; /* FIXME */
1702 str.MaximumLength = MAXIMUM_FILENAME_LENGTH;
1703 str.Buffer = sm->Name;
1704 RtlUnicodeStringToAnsiString(&str, &mod->FullDllName, FALSE);
1705 ptr = strrchr(sm->Name, '\\');
1706 sm->NameOffset = (ptr != NULL) ? (ptr - (char*)sm->Name + 1) : 0;
1708 smi->ModulesCount++;
1711 else nts = STATUS_INFO_LENGTH_MISMATCH;
1713 RtlLeaveCriticalSection( &loader_section );
1715 if (req_size) *req_size = size;
1720 /******************************************************************
1721 * LdrShutdownProcess (NTDLL.@)
1724 void WINAPI LdrShutdownProcess(void)
1727 process_detach( TRUE, (LPVOID)1 );
1730 /******************************************************************
1731 * LdrShutdownThread (NTDLL.@)
1734 void WINAPI LdrShutdownThread(void)
1736 PLIST_ENTRY mark, entry;
1741 /* don't do any detach calls if process is exiting */
1742 if (process_detaching) return;
1743 /* FIXME: there is still a race here */
1745 RtlEnterCriticalSection( &loader_section );
1747 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
1748 for (entry = mark->Blink; entry != mark; entry = entry->Blink)
1750 mod = CONTAINING_RECORD(entry, LDR_MODULE,
1751 InInitializationOrderModuleList);
1752 if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
1754 if ( mod->Flags & LDR_NO_DLL_CALLS )
1757 MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
1758 DLL_THREAD_DETACH, NULL );
1761 RtlLeaveCriticalSection( &loader_section );
1764 /***********************************************************************
1765 * MODULE_FlushModrefs
1767 * Remove all unused modrefs and call the internal unloading routines
1768 * for the library type.
1770 * The loader_section must be locked while calling this function.
1772 static void MODULE_FlushModrefs(void)
1774 PLIST_ENTRY mark, entry, prev;
1778 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
1779 for (entry = mark->Blink; entry != mark; entry = prev)
1781 mod = CONTAINING_RECORD(entry, LDR_MODULE,
1782 InInitializationOrderModuleList);
1783 wm = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
1785 prev = entry->Blink;
1786 if (mod->LoadCount) continue;
1788 RemoveEntryList(&mod->InLoadOrderModuleList);
1789 RemoveEntryList(&mod->InMemoryOrderModuleList);
1790 RemoveEntryList(&mod->InInitializationOrderModuleList);
1792 TRACE(" unloading %s\n", debugstr_w(mod->FullDllName.Buffer));
1793 if (!TRACE_ON(module))
1794 TRACE_(loaddll)("Unloaded module %s : %s\n",
1795 debugstr_w(mod->FullDllName.Buffer),
1796 (wm->ldr.Flags & LDR_WINE_INTERNAL) ? "builtin" : "native" );
1798 SERVER_START_REQ( unload_dll )
1800 req->base = mod->BaseAddress;
1801 wine_server_call( req );
1805 NtUnmapViewOfSection( NtCurrentProcess(), mod->BaseAddress );
1806 if (wm->ldr.Flags & LDR_WINE_INTERNAL) wine_dll_unload( wm->ldr.SectionHandle );
1807 if (cached_modref == wm) cached_modref = NULL;
1808 RtlFreeUnicodeString( &mod->FullDllName );
1809 RtlFreeHeap( GetProcessHeap(), 0, wm->deps );
1810 RtlFreeHeap( GetProcessHeap(), 0, wm );
1814 /***********************************************************************
1815 * MODULE_DecRefCount
1817 * The loader_section must be locked while calling this function.
1819 static void MODULE_DecRefCount( WINE_MODREF *wm )
1823 if ( wm->ldr.Flags & LDR_UNLOAD_IN_PROGRESS )
1826 if ( wm->ldr.LoadCount <= 0 )
1829 --wm->ldr.LoadCount;
1830 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm->ldr.BaseDllName.Buffer), wm->ldr.LoadCount );
1832 if ( wm->ldr.LoadCount == 0 )
1834 wm->ldr.Flags |= LDR_UNLOAD_IN_PROGRESS;
1836 for ( i = 0; i < wm->nDeps; i++ )
1838 MODULE_DecRefCount( wm->deps[i] );
1840 wm->ldr.Flags &= ~LDR_UNLOAD_IN_PROGRESS;
1844 /******************************************************************
1845 * LdrUnloadDll (NTDLL.@)
1849 NTSTATUS WINAPI LdrUnloadDll( HMODULE hModule )
1851 NTSTATUS retv = STATUS_SUCCESS;
1853 TRACE("(%p)\n", hModule);
1855 RtlEnterCriticalSection( &loader_section );
1857 /* if we're stopping the whole process (and forcing the removal of all
1858 * DLLs) the library will be freed anyway
1860 if (!process_detaching)
1865 if ((wm = get_modref( hModule )) != NULL)
1867 TRACE("(%s) - START\n", debugstr_w(wm->ldr.BaseDllName.Buffer));
1869 /* Recursively decrement reference counts */
1870 MODULE_DecRefCount( wm );
1872 /* Call process detach notifications */
1873 if ( free_lib_count <= 1 )
1875 process_detach( FALSE, NULL );
1876 MODULE_FlushModrefs();
1882 retv = STATUS_DLL_NOT_FOUND;
1887 RtlLeaveCriticalSection( &loader_section );
1892 /***********************************************************************
1893 * RtlImageNtHeader (NTDLL.@)
1895 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
1897 IMAGE_NT_HEADERS *ret;
1901 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
1904 if (dos->e_magic == IMAGE_DOS_SIGNATURE)
1906 ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
1907 if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
1910 __EXCEPT(page_fault)
1919 /******************************************************************
1920 * LdrInitializeThunk (NTDLL.@)
1922 * FIXME: the arguments are not correct, main_file is a Wine invention.
1924 void WINAPI LdrInitializeThunk( HANDLE main_file, ULONG unknown2, ULONG unknown3, ULONG unknown4 )
1929 PEB *peb = NtCurrentTeb()->Peb;
1930 UNICODE_STRING *main_exe_name = &peb->ProcessParameters->ImagePathName;
1931 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( peb->ImageBaseAddress );
1933 /* allocate the modref for the main exe */
1934 if (!(wm = alloc_module( peb->ImageBaseAddress, main_exe_name->Buffer )))
1936 status = STATUS_NO_MEMORY;
1939 wm->ldr.LoadCount = -1; /* can't unload main exe */
1941 /* the main exe needs to be the first in the load order list */
1942 RemoveEntryList( &wm->ldr.InLoadOrderModuleList );
1943 InsertHeadList( &peb->LdrData->InLoadOrderModuleList, &wm->ldr.InLoadOrderModuleList );
1945 /* Install signal handlers; this cannot be done before, since we cannot
1946 * send exceptions to the debugger before the create process event that
1947 * is sent by REQ_INIT_PROCESS_DONE.
1948 * We do need the handlers in place by the time the request is over, so
1949 * we set them up here. If we segfault between here and the server call
1950 * something is very wrong... */
1951 if (!SIGNAL_Init()) exit(1);
1953 /* Signal the parent process to continue */
1954 SERVER_START_REQ( init_process_done )
1956 req->module = peb->ImageBaseAddress;
1957 req->module_size = wm->ldr.SizeOfImage;
1958 req->entry = (char *)peb->ImageBaseAddress + nt->OptionalHeader.AddressOfEntryPoint;
1959 /* API requires a double indirection */
1960 req->name = &main_exe_name->Buffer;
1961 req->exe_file = main_file;
1962 req->gui = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI);
1963 wine_server_add_data( req, main_exe_name->Buffer, main_exe_name->Length );
1964 wine_server_call( req );
1968 if (main_file) NtClose( main_file ); /* we no longer need it */
1970 if (TRACE_ON(relay) || TRACE_ON(snoop))
1972 RELAY_InitDebugLists();
1974 if (TRACE_ON(relay)) /* setup relay for already loaded dlls */
1976 LIST_ENTRY *entry, *mark = &peb->LdrData->InLoadOrderModuleList;
1977 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1979 LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
1980 if (mod->Flags & LDR_WINE_INTERNAL) RELAY_SetupDLL( mod->BaseAddress );
1985 RtlEnterCriticalSection( &loader_section );
1987 load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
1988 if ((status = fixup_imports( wm, load_path )) != STATUS_SUCCESS) goto error;
1989 if ((status = alloc_process_tls()) != STATUS_SUCCESS) goto error;
1990 if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto error;
1991 if ((status = process_attach( wm, (LPVOID)1 )) != STATUS_SUCCESS)
1993 if (last_failed_modref)
1994 ERR( "%s failed to initialize, aborting\n", debugstr_w(last_failed_modref->ldr.BaseDllName.Buffer) + 1 );
1998 RtlLeaveCriticalSection( &loader_section );
2000 if (nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE) VIRTUAL_UseLargeAddressSpace();
2004 ERR( "Main exe initialization for %s failed, status %lx\n", debugstr_w(main_exe_name->Buffer), status );
2009 /***********************************************************************
2010 * RtlImageDirectoryEntryToData (NTDLL.@)
2012 PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
2014 const IMAGE_NT_HEADERS *nt;
2017 if ((ULONG_PTR)module & 1) /* mapped as data file */
2019 module = (HMODULE)((ULONG_PTR)module & ~1);
2022 if (!(nt = RtlImageNtHeader( module ))) return NULL;
2023 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
2024 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
2025 *size = nt->OptionalHeader.DataDirectory[dir].Size;
2026 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)module + addr;
2028 /* not mapped as image, need to find the section containing the virtual address */
2029 return RtlImageRvaToVa( nt, module, addr, NULL );
2033 /***********************************************************************
2034 * RtlImageRvaToSection (NTDLL.@)
2036 PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt,
2037 HMODULE module, DWORD rva )
2040 const IMAGE_SECTION_HEADER *sec;
2042 sec = (const IMAGE_SECTION_HEADER*)((const char*)&nt->OptionalHeader +
2043 nt->FileHeader.SizeOfOptionalHeader);
2044 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
2046 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
2047 return (PIMAGE_SECTION_HEADER)sec;
2053 /***********************************************************************
2054 * RtlImageRvaToVa (NTDLL.@)
2056 PVOID WINAPI RtlImageRvaToVa( const IMAGE_NT_HEADERS *nt, HMODULE module,
2057 DWORD rva, IMAGE_SECTION_HEADER **section )
2059 IMAGE_SECTION_HEADER *sec;
2061 if (section && *section) /* try this section first */
2064 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
2067 if (!(sec = RtlImageRvaToSection( nt, module, rva ))) return NULL;
2069 if (section) *section = sec;
2070 return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress);
2074 /***********************************************************************
2075 * NtLoadDriver (NTDLL.@)
2076 * ZwLoadDriver (NTDLL.@)
2078 NTSTATUS WINAPI NtLoadDriver( const UNICODE_STRING *DriverServiceName )
2080 FIXME("(%p), stub!\n",DriverServiceName);
2081 return STATUS_NOT_IMPLEMENTED;
2085 /***********************************************************************
2086 * NtUnloadDriver (NTDLL.@)
2087 * ZwUnloadDriver (NTDLL.@)
2089 NTSTATUS WINAPI NtUnloadDriver( const UNICODE_STRING *DriverServiceName )
2091 FIXME("(%p), stub!\n",DriverServiceName);
2092 return STATUS_NOT_IMPLEMENTED;
2096 /******************************************************************
2097 * __wine_init_windows_dir (NTDLL.@)
2099 * Windows and system dir initialization once kernel32 has been loaded.
2101 void __wine_init_windows_dir( const WCHAR *windir, const WCHAR *sysdir )
2103 PLIST_ENTRY mark, entry;
2106 RtlCreateUnicodeString( &system_dir, sysdir );
2108 /* prepend the system dir to the name of the already created modules */
2109 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
2110 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
2112 LDR_MODULE *mod = CONTAINING_RECORD( entry, LDR_MODULE, InLoadOrderModuleList );
2114 assert( mod->Flags & LDR_WINE_INTERNAL );
2116 buffer = RtlAllocateHeap( GetProcessHeap(), 0,
2117 system_dir.Length + mod->FullDllName.Length + 2*sizeof(WCHAR) );
2118 if (!buffer) continue;
2119 strcpyW( buffer, system_dir.Buffer );
2120 p = buffer + strlenW( buffer );
2121 if (p > buffer && p[-1] != '\\') *p++ = '\\';
2122 strcpyW( p, mod->FullDllName.Buffer );
2123 RtlInitUnicodeString( &mod->FullDllName, buffer );
2124 RtlInitUnicodeString( &mod->BaseDllName, p );
2129 /***********************************************************************
2130 * __wine_process_init
2132 void __wine_process_init( int argc, char *argv[] )
2134 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
2138 ANSI_STRING func_name;
2139 void (* DECLSPEC_NORETURN init_func)();
2140 extern mode_t FILE_umask;
2144 /* retrieve current umask */
2145 FILE_umask = umask(0777);
2146 umask( FILE_umask );
2148 /* setup the load callback and create ntdll modref */
2149 wine_dll_set_callback( load_builtin_callback );
2151 if ((status = load_builtin_dll( NULL, kernel32W, 0, &wm )) != STATUS_SUCCESS)
2153 MESSAGE( "wine: could not load kernel32.dll, status %lx\n", status );
2156 RtlInitAnsiString( &func_name, "__wine_kernel_init" );
2157 if ((status = LdrGetProcedureAddress( wm->ldr.BaseAddress, &func_name,
2158 0, (void **)&init_func )) != STATUS_SUCCESS)
2160 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %lx\n", status );