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/unicode.h"
41 #include "wine/debug.h"
42 #include "wine/server.h"
43 #include "ntdll_misc.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(module);
46 WINE_DECLARE_DEBUG_CHANNEL(relay);
47 WINE_DECLARE_DEBUG_CHANNEL(snoop);
48 WINE_DECLARE_DEBUG_CHANNEL(loaddll);
50 typedef DWORD (CALLBACK *DLLENTRYPROC)(HMODULE,DWORD,LPVOID);
52 static int process_detaching = 0; /* set on process detach to avoid deadlocks with thread detach */
53 static int free_lib_count; /* recursion depth of LdrUnloadDll calls */
55 /* filter for page-fault exceptions */
56 static WINE_EXCEPTION_FILTER(page_fault)
58 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
59 return EXCEPTION_EXECUTE_HANDLER;
60 return EXCEPTION_CONTINUE_SEARCH;
63 static const char * const reason_names[] =
71 static const WCHAR dllW[] = {'.','d','l','l',0};
73 /* internal representation of 32bit modules. per process. */
74 typedef struct _wine_modref
78 struct _wine_modref **deps;
81 /* info about the current builtin dll load */
82 /* used to keep track of things across the register_dll constructor call */
83 struct builtin_load_info
85 const WCHAR *load_path;
90 static struct builtin_load_info default_load_info;
91 static struct builtin_load_info *builtin_load_info = &default_load_info;
93 static UINT tls_module_count; /* number of modules with TLS directory */
94 static UINT tls_total_size; /* total size of TLS storage */
95 static const IMAGE_TLS_DIRECTORY **tls_dirs; /* array of TLS directories */
97 UNICODE_STRING system_dir = { 0, 0, NULL }; /* system directory */
99 static CRITICAL_SECTION loader_section;
100 static CRITICAL_SECTION_DEBUG critsect_debug =
102 0, 0, &loader_section,
103 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
104 0, 0, { 0, (DWORD)(__FILE__ ": loader_section") }
106 static CRITICAL_SECTION loader_section = { &critsect_debug, -1, 0, 0, 0, 0 };
108 static WINE_MODREF *cached_modref;
109 static WINE_MODREF *current_modref;
110 static WINE_MODREF *last_failed_modref;
112 static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_MODREF** pwm );
113 static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
114 DWORD exp_size, const char *name, int hint );
116 /* convert PE image VirtualAddress to Real Address */
117 inline static void *get_rva( HMODULE module, DWORD va )
119 return (void *)((char *)module + va);
122 /* check whether the file name contains a path */
123 inline static int contains_path( LPCWSTR name )
125 return ((*name && (name[1] == ':')) || strchrW(name, '/') || strchrW(name, '\\'));
128 /* convert from straight ASCII to Unicode without depending on the current codepage */
129 inline static void ascii_to_unicode( WCHAR *dst, const char *src, size_t len )
131 while (len--) *dst++ = (unsigned char)*src++;
135 /*************************************************************************
136 * call_dll_entry_point
138 * Some brain-damaged dlls (ir32_32.dll for instance) modify ebx in
139 * their entry point, so we need a small asm wrapper.
142 extern BOOL call_dll_entry_point( DLLENTRYPROC proc, void *module, UINT reason, void *reserved );
143 __ASM_GLOBAL_FUNC(call_dll_entry_point,
150 "movl 8(%ebp),%eax\n\t"
152 "leal -4(%ebp),%esp\n\t"
157 static inline BOOL call_dll_entry_point( DLLENTRYPROC proc, void *module,
158 UINT reason, void *reserved )
160 return proc( module, reason, reserved );
162 #endif /* __i386__ */
166 /*************************************************************************
169 * Entry point for stub functions.
171 static void stub_entry_point( const char *dll, const char *name, ... )
173 EXCEPTION_RECORD rec;
175 rec.ExceptionCode = EXCEPTION_WINE_STUB;
176 rec.ExceptionFlags = EH_NONCONTINUABLE;
177 rec.ExceptionRecord = NULL;
179 rec.ExceptionAddress = __builtin_return_address(0);
181 rec.ExceptionAddress = *((void **)&dll - 1);
183 rec.NumberParameters = 2;
184 rec.ExceptionInformation[0] = (ULONG_PTR)dll;
185 rec.ExceptionInformation[1] = (ULONG_PTR)name;
186 for (;;) RtlRaiseException( &rec );
190 #include "pshpack1.h"
193 BYTE popl_eax; /* popl %eax */
194 BYTE pushl1; /* pushl $name */
196 BYTE pushl2; /* pushl $dll */
198 BYTE pushl_eax; /* pushl %eax */
199 BYTE jmp; /* jmp stub_entry_point */
204 /*************************************************************************
207 * Allocate a stub entry point.
209 static void *allocate_stub( const char *dll, const char *name )
211 #define MAX_SIZE 65536
212 static struct stub *stubs;
213 static unsigned int nb_stubs;
216 if (nb_stubs >= MAX_SIZE / sizeof(*stub)) return (void *)0xdeadbeef;
220 ULONG size = MAX_SIZE;
221 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs, 0, &size,
222 MEM_COMMIT, PAGE_EXECUTE_WRITECOPY ) != STATUS_SUCCESS)
223 return (void *)0xdeadbeef;
225 stub = &stubs[nb_stubs++];
226 stub->popl_eax = 0x58; /* popl %eax */
227 stub->pushl1 = 0x68; /* pushl $name */
229 stub->pushl2 = 0x68; /* pushl $dll */
231 stub->pushl_eax = 0x50; /* pushl %eax */
232 stub->jmp = 0xe9; /* jmp stub_entry_point */
233 stub->entry = (BYTE *)stub_entry_point - (BYTE *)(&stub->entry + 1);
238 static inline void *allocate_stub( const char *dll, const char *name ) { return (void *)0xdeadbeef; }
239 #endif /* __i386__ */
242 /*************************************************************************
245 * Looks for the referenced HMODULE in the current process
246 * The loader_section must be locked while calling this function.
248 static WINE_MODREF *get_modref( HMODULE hmod )
250 PLIST_ENTRY mark, entry;
253 if (cached_modref && cached_modref->ldr.BaseAddress == hmod) return cached_modref;
255 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
256 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
258 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
259 if (mod->BaseAddress == hmod)
260 return cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
261 if (mod->BaseAddress > (void*)hmod) break;
267 /**********************************************************************
268 * find_basename_module
270 * Find a module from its base name.
271 * The loader_section must be locked while calling this function
273 static WINE_MODREF *find_basename_module( LPCWSTR name )
275 PLIST_ENTRY mark, entry;
277 if (cached_modref && !strcmpiW( name, cached_modref->ldr.BaseDllName.Buffer ))
278 return cached_modref;
280 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
281 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
283 LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
284 if (!strcmpiW( name, mod->BaseDllName.Buffer ))
286 cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
287 return cached_modref;
294 /**********************************************************************
295 * find_fullname_module
297 * Find a module from its full path name.
298 * The loader_section must be locked while calling this function
300 static WINE_MODREF *find_fullname_module( LPCWSTR name )
302 PLIST_ENTRY mark, entry;
304 if (cached_modref && !strcmpiW( name, cached_modref->ldr.FullDllName.Buffer ))
305 return cached_modref;
307 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
308 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
310 LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
311 if (!strcmpiW( name, mod->FullDllName.Buffer ))
313 cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
314 return cached_modref;
321 /*************************************************************************
322 * find_forwarded_export
324 * Find the final function pointer for a forwarded function.
325 * The loader_section must be locked while calling this function.
327 static FARPROC find_forwarded_export( HMODULE module, const char *forward )
329 const IMAGE_EXPORT_DIRECTORY *exports;
333 const char *end = strchr(forward, '.');
336 if (!end) return NULL;
337 if ((end - forward) * sizeof(WCHAR) >= sizeof(mod_name) - sizeof(dllW)) return NULL;
338 ascii_to_unicode( mod_name, forward, end - forward );
339 memcpy( mod_name + (end - forward), dllW, sizeof(dllW) );
341 if (!(wm = find_basename_module( mod_name )))
343 ERR("module not found for forward '%s' used by %s\n",
344 forward, debugstr_w(get_modref(module)->ldr.FullDllName.Buffer) );
347 if ((exports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
348 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
349 proc = find_named_export( wm->ldr.BaseAddress, exports, exp_size, end + 1, -1 );
353 ERR("function not found for forward '%s' used by %s."
354 " If you are using builtin %s, try using the native one instead.\n",
355 forward, debugstr_w(get_modref(module)->ldr.FullDllName.Buffer),
356 debugstr_w(get_modref(module)->ldr.BaseDllName.Buffer) );
362 /*************************************************************************
363 * find_ordinal_export
365 * Find an exported function by ordinal.
366 * The exports base must have been subtracted from the ordinal already.
367 * The loader_section must be locked while calling this function.
369 static FARPROC find_ordinal_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
370 DWORD exp_size, int ordinal )
373 const DWORD *functions = get_rva( module, exports->AddressOfFunctions );
375 if (ordinal >= exports->NumberOfFunctions)
377 TRACE(" ordinal %ld out of range!\n", ordinal + exports->Base );
380 if (!functions[ordinal]) return NULL;
382 proc = get_rva( module, functions[ordinal] );
384 /* if the address falls into the export dir, it's a forward */
385 if (((const char *)proc >= (const char *)exports) &&
386 ((const char *)proc < (const char *)exports + exp_size))
387 return find_forwarded_export( module, (const char *)proc );
391 const WCHAR *user = current_modref ? current_modref->ldr.BaseDllName.Buffer : NULL;
392 proc = SNOOP_GetProcAddress( module, exports, exp_size, proc, ordinal, user );
396 const WCHAR *user = current_modref ? current_modref->ldr.BaseDllName.Buffer : NULL;
397 proc = RELAY_GetProcAddress( module, exports, exp_size, proc, user );
403 /*************************************************************************
406 * Find an exported function by name.
407 * The loader_section must be locked while calling this function.
409 static FARPROC find_named_export( HMODULE module, const IMAGE_EXPORT_DIRECTORY *exports,
410 DWORD exp_size, const char *name, int hint )
412 const WORD *ordinals = get_rva( module, exports->AddressOfNameOrdinals );
413 const DWORD *names = get_rva( module, exports->AddressOfNames );
414 int min = 0, max = exports->NumberOfNames - 1;
416 /* first check the hint */
417 if (hint >= 0 && hint <= max)
419 char *ename = get_rva( module, names[hint] );
420 if (!strcmp( ename, name ))
421 return find_ordinal_export( module, exports, exp_size, ordinals[hint] );
424 /* then do a binary search */
427 int res, pos = (min + max) / 2;
428 char *ename = get_rva( module, names[pos] );
429 if (!(res = strcmp( ename, name )))
430 return find_ordinal_export( module, exports, exp_size, ordinals[pos] );
431 if (res > 0) max = pos - 1;
439 /*************************************************************************
442 * Import the dll specified by the given import descriptor.
443 * The loader_section must be locked while calling this function.
445 static WINE_MODREF *import_dll( HMODULE module, const IMAGE_IMPORT_DESCRIPTOR *descr, LPCWSTR load_path )
450 const IMAGE_EXPORT_DIRECTORY *exports;
452 const IMAGE_THUNK_DATA *import_list;
453 IMAGE_THUNK_DATA *thunk_list;
455 const char *name = get_rva( module, descr->Name );
456 DWORD len = strlen(name) + 1;
458 DWORD protect_size = 0;
461 thunk_list = get_rva( module, (DWORD)descr->FirstThunk );
462 if (descr->u.OriginalFirstThunk)
463 import_list = get_rva( module, (DWORD)descr->u.OriginalFirstThunk );
465 import_list = thunk_list;
467 if (len * sizeof(WCHAR) <= sizeof(buffer))
469 ascii_to_unicode( buffer, name, len );
470 status = load_dll( load_path, buffer, 0, &wmImp );
472 else /* need to allocate a larger buffer */
474 WCHAR *ptr = RtlAllocateHeap( GetProcessHeap(), 0, len * sizeof(WCHAR) );
475 if (!ptr) return NULL;
476 ascii_to_unicode( ptr, name, len );
477 status = load_dll( load_path, ptr, 0, &wmImp );
478 RtlFreeHeap( GetProcessHeap(), 0, ptr );
483 if (status == STATUS_DLL_NOT_FOUND)
484 ERR("Library %s (which is needed by %s) not found\n",
485 name, debugstr_w(current_modref->ldr.FullDllName.Buffer));
487 ERR("Loading library %s (which is needed by %s) failed (error %lx).\n",
488 name, debugstr_w(current_modref->ldr.FullDllName.Buffer), status);
492 /* unprotect the import address table since it can be located in
493 * readonly section */
494 while (import_list[protect_size].u1.Ordinal) protect_size++;
495 protect_base = thunk_list;
496 protect_size *= sizeof(*thunk_list);
497 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base,
498 &protect_size, PAGE_WRITECOPY, &protect_old );
500 imp_mod = wmImp->ldr.BaseAddress;
501 exports = RtlImageDirectoryEntryToData( imp_mod, TRUE, IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size );
505 /* set all imported function to deadbeef */
506 while (import_list->u1.Ordinal)
508 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
510 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
511 WARN("No implementation for %s.%d", name, ordinal );
512 thunk_list->u1.Function = allocate_stub( name, (const char *)ordinal );
516 IMAGE_IMPORT_BY_NAME *pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
517 WARN("No implementation for %s.%s", name, pe_name->Name );
518 thunk_list->u1.Function = allocate_stub( name, pe_name->Name );
520 WARN(" imported from %s, allocating stub %p\n",
521 debugstr_w(current_modref->ldr.FullDllName.Buffer),
522 thunk_list->u1.Function );
529 while (import_list->u1.Ordinal)
531 if (IMAGE_SNAP_BY_ORDINAL(import_list->u1.Ordinal))
533 int ordinal = IMAGE_ORDINAL(import_list->u1.Ordinal);
535 thunk_list->u1.Function = (PDWORD)find_ordinal_export( imp_mod, exports, exp_size,
536 ordinal - exports->Base );
537 if (!thunk_list->u1.Function)
539 thunk_list->u1.Function = allocate_stub( name, (const char *)ordinal );
540 WARN("No implementation for %s.%d imported from %s, setting to %p\n",
541 name, ordinal, debugstr_w(current_modref->ldr.FullDllName.Buffer),
542 thunk_list->u1.Function );
544 TRACE("--- Ordinal %s.%d = %p\n", name, ordinal, thunk_list->u1.Function );
546 else /* import by name */
548 IMAGE_IMPORT_BY_NAME *pe_name;
549 pe_name = get_rva( module, (DWORD)import_list->u1.AddressOfData );
550 thunk_list->u1.Function = (PDWORD)find_named_export( imp_mod, exports, exp_size,
551 pe_name->Name, pe_name->Hint );
552 if (!thunk_list->u1.Function)
554 thunk_list->u1.Function = allocate_stub( name, pe_name->Name );
555 WARN("No implementation for %s.%s imported from %s, setting to %p\n",
556 name, pe_name->Name, debugstr_w(current_modref->ldr.FullDllName.Buffer),
557 thunk_list->u1.Function );
559 TRACE("--- %s %s.%d = %p\n", pe_name->Name, name, pe_name->Hint, thunk_list->u1.Function);
566 /* restore old protection of the import address table */
567 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base, &protect_size, protect_old, NULL );
572 /****************************************************************
575 * Fixup all imports of a given module.
576 * The loader_section must be locked while calling this function.
578 static NTSTATUS fixup_imports( WINE_MODREF *wm, LPCWSTR load_path )
581 const IMAGE_IMPORT_DESCRIPTOR *imports;
586 if (!(imports = RtlImageDirectoryEntryToData( wm->ldr.BaseAddress, TRUE,
587 IMAGE_DIRECTORY_ENTRY_IMPORT, &size )))
588 return STATUS_SUCCESS;
590 nb_imports = size / sizeof(*imports);
591 for (i = 0; i < nb_imports; i++)
593 if (!imports[i].Name)
599 if (!nb_imports) return STATUS_SUCCESS; /* no imports */
601 /* Allocate module dependency list */
602 wm->nDeps = nb_imports;
603 wm->deps = RtlAllocateHeap( GetProcessHeap(), 0, nb_imports*sizeof(WINE_MODREF *) );
605 /* load the imported modules. They are automatically
606 * added to the modref list of the process.
608 prev = current_modref;
610 status = STATUS_SUCCESS;
611 for (i = 0; i < nb_imports; i++)
613 if (!(wm->deps[i] = import_dll( wm->ldr.BaseAddress, &imports[i], load_path )))
614 status = STATUS_DLL_NOT_FOUND;
616 current_modref = prev;
621 /*************************************************************************
624 * Allocate a WINE_MODREF structure and add it to the process list
625 * The loader_section must be locked while calling this function.
627 static WINE_MODREF *alloc_module( HMODULE hModule, LPCWSTR filename )
631 const IMAGE_NT_HEADERS *nt = RtlImageNtHeader(hModule);
632 PLIST_ENTRY entry, mark;
634 if (!(wm = RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm) ))) return NULL;
639 wm->ldr.BaseAddress = hModule;
640 wm->ldr.EntryPoint = NULL;
641 wm->ldr.SizeOfImage = nt->OptionalHeader.SizeOfImage;
643 wm->ldr.LoadCount = 0;
644 wm->ldr.TlsIndex = -1;
645 wm->ldr.SectionHandle = NULL;
646 wm->ldr.CheckSum = 0;
647 wm->ldr.TimeDateStamp = 0;
649 RtlCreateUnicodeString( &wm->ldr.FullDllName, filename );
650 if ((p = strrchrW( wm->ldr.FullDllName.Buffer, '\\' ))) p++;
651 else p = wm->ldr.FullDllName.Buffer;
652 RtlInitUnicodeString( &wm->ldr.BaseDllName, p );
654 if (nt->FileHeader.Characteristics & IMAGE_FILE_DLL)
656 wm->ldr.Flags |= LDR_IMAGE_IS_DLL;
657 if (nt->OptionalHeader.AddressOfEntryPoint)
658 wm->ldr.EntryPoint = (char *)hModule + nt->OptionalHeader.AddressOfEntryPoint;
661 InsertTailList(&NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList,
662 &wm->ldr.InLoadOrderModuleList);
664 /* insert module in MemoryList, sorted in increasing base addresses */
665 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
666 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
668 if (CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList)->BaseAddress > wm->ldr.BaseAddress)
671 entry->Blink->Flink = &wm->ldr.InMemoryOrderModuleList;
672 wm->ldr.InMemoryOrderModuleList.Blink = entry->Blink;
673 wm->ldr.InMemoryOrderModuleList.Flink = entry;
674 entry->Blink = &wm->ldr.InMemoryOrderModuleList;
676 /* wait until init is called for inserting into this list */
677 wm->ldr.InInitializationOrderModuleList.Flink = NULL;
678 wm->ldr.InInitializationOrderModuleList.Blink = NULL;
683 /*************************************************************************
686 * Allocate the process-wide structure for module TLS storage.
688 static NTSTATUS alloc_process_tls(void)
690 PLIST_ENTRY mark, entry;
692 const IMAGE_TLS_DIRECTORY *dir;
695 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
696 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
698 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
699 if (!(dir = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE,
700 IMAGE_DIRECTORY_ENTRY_TLS, &size )))
702 size = (dir->EndAddressOfRawData - dir->StartAddressOfRawData) + dir->SizeOfZeroFill;
704 tls_total_size += size;
707 if (!tls_module_count) return STATUS_SUCCESS;
709 TRACE( "count %u size %u\n", tls_module_count, tls_total_size );
711 tls_dirs = RtlAllocateHeap( GetProcessHeap(), 0, tls_module_count * sizeof(*tls_dirs) );
712 if (!tls_dirs) return STATUS_NO_MEMORY;
714 for (i = 0, entry = mark->Flink; entry != mark; entry = entry->Flink)
716 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
717 if (!(dir = RtlImageDirectoryEntryToData( mod->BaseAddress, TRUE,
718 IMAGE_DIRECTORY_ENTRY_TLS, &size )))
721 *dir->AddressOfIndex = i;
723 mod->LoadCount = -1; /* can't unload it */
726 return STATUS_SUCCESS;
730 /*************************************************************************
733 * Allocate the per-thread structure for module TLS storage.
735 static NTSTATUS alloc_thread_tls(void)
741 if (!tls_module_count) return STATUS_SUCCESS;
743 if (!(pointers = RtlAllocateHeap( GetProcessHeap(), 0,
744 tls_module_count * sizeof(*pointers) )))
745 return STATUS_NO_MEMORY;
747 if (!(data = RtlAllocateHeap( GetProcessHeap(), 0, tls_total_size )))
749 RtlFreeHeap( GetProcessHeap(), 0, pointers );
750 return STATUS_NO_MEMORY;
753 for (i = 0; i < tls_module_count; i++)
755 const IMAGE_TLS_DIRECTORY *dir = tls_dirs[i];
756 ULONG size = dir->EndAddressOfRawData - dir->StartAddressOfRawData;
758 TRACE( "thread %04lx idx %d: %ld/%ld bytes from %p to %p\n",
759 GetCurrentThreadId(), i, size, dir->SizeOfZeroFill,
760 (void *)dir->StartAddressOfRawData, data );
763 memcpy( data, (void *)dir->StartAddressOfRawData, size );
765 memset( data, 0, dir->SizeOfZeroFill );
766 data += dir->SizeOfZeroFill;
768 NtCurrentTeb()->ThreadLocalStoragePointer = pointers;
769 return STATUS_SUCCESS;
773 /*************************************************************************
776 static void call_tls_callbacks( HMODULE module, UINT reason )
778 const IMAGE_TLS_DIRECTORY *dir;
779 const PIMAGE_TLS_CALLBACK *callback;
782 dir = RtlImageDirectoryEntryToData( module, TRUE, IMAGE_DIRECTORY_ENTRY_TLS, &dirsize );
783 if (!dir || !dir->AddressOfCallBacks) return;
785 for (callback = dir->AddressOfCallBacks; *callback; callback++)
788 DPRINTF("%04lx:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
789 GetCurrentThreadId(), *callback, module, reason_names[reason] );
790 (*callback)( module, reason, NULL );
792 DPRINTF("%04lx:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
793 GetCurrentThreadId(), *callback, module, reason_names[reason] );
798 /*************************************************************************
801 static BOOL MODULE_InitDLL( WINE_MODREF *wm, UINT reason, LPVOID lpReserved )
805 DLLENTRYPROC entry = wm->ldr.EntryPoint;
806 void *module = wm->ldr.BaseAddress;
808 /* Skip calls for modules loaded with special load flags */
810 if (wm->ldr.Flags & LDR_DONT_RESOLVE_REFS) return TRUE;
811 if (wm->ldr.TlsIndex != -1) call_tls_callbacks( wm->ldr.BaseAddress, reason );
812 if (!entry) return TRUE;
816 size_t len = min( wm->ldr.BaseDllName.Length, sizeof(mod_name)-sizeof(WCHAR) );
817 memcpy( mod_name, wm->ldr.BaseDllName.Buffer, len );
818 mod_name[len / sizeof(WCHAR)] = 0;
819 DPRINTF("%04lx:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
820 GetCurrentThreadId(), entry, module, debugstr_w(mod_name),
821 reason_names[reason], lpReserved );
823 else TRACE("(%p %s,%s,%p) - CALL\n", module, debugstr_w(wm->ldr.BaseDllName.Buffer),
824 reason_names[reason], lpReserved );
826 retv = call_dll_entry_point( entry, module, reason, lpReserved );
828 /* The state of the module list may have changed due to the call
829 to the dll. We cannot assume that this module has not been
832 DPRINTF("%04lx:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
833 GetCurrentThreadId(), entry, module, debugstr_w(mod_name),
834 reason_names[reason], lpReserved, retv );
835 else TRACE("(%p,%s,%p) - RETURN %d\n", module, reason_names[reason], lpReserved, retv );
841 /*************************************************************************
844 * Send the process attach notification to all DLLs the given module
845 * depends on (recursively). This is somewhat complicated due to the fact that
847 * - we have to respect the module dependencies, i.e. modules implicitly
848 * referenced by another module have to be initialized before the module
849 * itself can be initialized
851 * - the initialization routine of a DLL can itself call LoadLibrary,
852 * thereby introducing a whole new set of dependencies (even involving
853 * the 'old' modules) at any time during the whole process
855 * (Note that this routine can be recursively entered not only directly
856 * from itself, but also via LoadLibrary from one of the called initialization
859 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
860 * the process *detach* notifications to be sent in the correct order.
861 * This must not only take into account module dependencies, but also
862 * 'hidden' dependencies created by modules calling LoadLibrary in their
863 * attach notification routine.
865 * The strategy is rather simple: we move a WINE_MODREF to the head of the
866 * list after the attach notification has returned. This implies that the
867 * detach notifications are called in the reverse of the sequence the attach
868 * notifications *returned*.
870 * The loader_section must be locked while calling this function.
872 static NTSTATUS process_attach( WINE_MODREF *wm, LPVOID lpReserved )
874 NTSTATUS status = STATUS_SUCCESS;
877 /* prevent infinite recursion in case of cyclical dependencies */
878 if ( ( wm->ldr.Flags & LDR_LOAD_IN_PROGRESS )
879 || ( wm->ldr.Flags & LDR_PROCESS_ATTACHED ) )
882 TRACE("(%s,%p) - START\n", debugstr_w(wm->ldr.BaseDllName.Buffer), lpReserved );
884 /* Tag current MODREF to prevent recursive loop */
885 wm->ldr.Flags |= LDR_LOAD_IN_PROGRESS;
887 /* Recursively attach all DLLs this one depends on */
888 for ( i = 0; i < wm->nDeps; i++ )
890 if (!wm->deps[i]) continue;
891 if ((status = process_attach( wm->deps[i], lpReserved )) != STATUS_SUCCESS) break;
894 /* Call DLL entry point */
895 if (status == STATUS_SUCCESS)
897 WINE_MODREF *prev = current_modref;
899 if (MODULE_InitDLL( wm, DLL_PROCESS_ATTACH, lpReserved ))
901 wm->ldr.Flags |= LDR_PROCESS_ATTACHED;
905 /* point to the name so LdrInitializeThunk can print it */
906 last_failed_modref = wm;
907 WARN("Initialization of %s failed\n", debugstr_w(wm->ldr.BaseDllName.Buffer));
908 status = STATUS_DLL_INIT_FAILED;
910 current_modref = prev;
913 InsertTailList(&NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList,
914 &wm->ldr.InInitializationOrderModuleList);
916 /* Remove recursion flag */
917 wm->ldr.Flags &= ~LDR_LOAD_IN_PROGRESS;
919 TRACE("(%s,%p) - END\n", debugstr_w(wm->ldr.BaseDllName.Buffer), lpReserved );
923 /*************************************************************************
926 * Send DLL process detach notifications. See the comment about calling
927 * sequence at process_attach. Unless the bForceDetach flag
928 * is set, only DLLs with zero refcount are notified.
930 static void process_detach( BOOL bForceDetach, LPVOID lpReserved )
932 PLIST_ENTRY mark, entry;
935 RtlEnterCriticalSection( &loader_section );
936 if (bForceDetach) process_detaching = 1;
937 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
940 for (entry = mark->Blink; entry != mark; entry = entry->Blink)
942 mod = CONTAINING_RECORD(entry, LDR_MODULE,
943 InInitializationOrderModuleList);
944 /* Check whether to detach this DLL */
945 if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
947 if ( mod->LoadCount && !bForceDetach )
950 /* Call detach notification */
951 mod->Flags &= ~LDR_PROCESS_ATTACHED;
952 MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
953 DLL_PROCESS_DETACH, lpReserved );
955 /* Restart at head of WINE_MODREF list, as entries might have
956 been added and/or removed while performing the call ... */
959 } while (entry != mark);
961 RtlLeaveCriticalSection( &loader_section );
964 /*************************************************************************
965 * MODULE_DllThreadAttach
967 * Send DLL thread attach notifications. These are sent in the
968 * reverse sequence of process detach notification.
971 NTSTATUS MODULE_DllThreadAttach( LPVOID lpReserved )
973 PLIST_ENTRY mark, entry;
977 /* don't do any attach calls if process is exiting */
978 if (process_detaching) return STATUS_SUCCESS;
979 /* FIXME: there is still a race here */
981 RtlEnterCriticalSection( &loader_section );
983 if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto done;
985 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
986 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
988 mod = CONTAINING_RECORD(entry, LDR_MODULE,
989 InInitializationOrderModuleList);
990 if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
992 if ( mod->Flags & LDR_NO_DLL_CALLS )
995 MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
996 DLL_THREAD_ATTACH, lpReserved );
1000 RtlLeaveCriticalSection( &loader_section );
1004 /******************************************************************
1005 * LdrDisableThreadCalloutsForDll (NTDLL.@)
1008 NTSTATUS WINAPI LdrDisableThreadCalloutsForDll(HMODULE hModule)
1011 NTSTATUS ret = STATUS_SUCCESS;
1013 RtlEnterCriticalSection( &loader_section );
1015 wm = get_modref( hModule );
1016 if (!wm || wm->ldr.TlsIndex != -1)
1017 ret = STATUS_DLL_NOT_FOUND;
1019 wm->ldr.Flags |= LDR_NO_DLL_CALLS;
1021 RtlLeaveCriticalSection( &loader_section );
1026 /******************************************************************
1027 * LdrFindEntryForAddress (NTDLL.@)
1029 * The loader_section must be locked while calling this function
1031 NTSTATUS WINAPI LdrFindEntryForAddress(const void* addr, PLDR_MODULE* pmod)
1033 PLIST_ENTRY mark, entry;
1036 mark = &NtCurrentTeb()->Peb->LdrData->InMemoryOrderModuleList;
1037 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1039 mod = CONTAINING_RECORD(entry, LDR_MODULE, InMemoryOrderModuleList);
1040 if ((const void *)mod->BaseAddress <= addr &&
1041 (const char *)addr < (char*)mod->BaseAddress + mod->SizeOfImage)
1044 return STATUS_SUCCESS;
1046 if ((const void *)mod->BaseAddress > addr) break;
1048 return STATUS_NO_MORE_ENTRIES;
1051 /******************************************************************
1052 * LdrLockLoaderLock (NTDLL.@)
1054 * Note: flags are not implemented.
1055 * Flag 0x01 is used to raise exceptions on errors.
1056 * Flag 0x02 is used to avoid waiting on the section (does RtlTryEnterCriticalSection instead).
1058 NTSTATUS WINAPI LdrLockLoaderLock( ULONG flags, ULONG *result, ULONG *magic )
1060 if (flags) FIXME( "flags %lx not supported\n", flags );
1062 if (result) *result = 1;
1063 if (!magic) return STATUS_INVALID_PARAMETER_3;
1064 RtlEnterCriticalSection( &loader_section );
1065 *magic = GetCurrentThreadId();
1066 return STATUS_SUCCESS;
1070 /******************************************************************
1071 * LdrUnlockLoaderUnlock (NTDLL.@)
1073 NTSTATUS WINAPI LdrUnlockLoaderLock( ULONG flags, ULONG magic )
1077 if (magic != GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2;
1078 RtlLeaveCriticalSection( &loader_section );
1080 return STATUS_SUCCESS;
1084 /******************************************************************
1085 * LdrGetDllHandle (NTDLL.@)
1087 NTSTATUS WINAPI LdrGetDllHandle(ULONG x, ULONG y, const UNICODE_STRING *name, HMODULE *base)
1089 NTSTATUS status = STATUS_DLL_NOT_FOUND;
1090 WCHAR dllname[MAX_PATH+4], *p;
1092 PLIST_ENTRY mark, entry;
1095 if (x != 0 || y != 0)
1096 FIXME("Unknown behavior, please report\n");
1098 /* Append .DLL to name if no extension present */
1099 if (!(p = strrchrW( name->Buffer, '.')) || strchrW( p, '/' ) || strchrW( p, '\\'))
1101 if (name->Length >= MAX_PATH) return STATUS_NAME_TOO_LONG;
1102 strcpyW( dllname, name->Buffer );
1103 strcatW( dllname, dllW );
1104 RtlInitUnicodeString( &str, dllname );
1108 RtlEnterCriticalSection( &loader_section );
1112 if (RtlEqualUnicodeString( name, &cached_modref->ldr.FullDllName, TRUE ) ||
1113 RtlEqualUnicodeString( name, &cached_modref->ldr.BaseDllName, TRUE ))
1115 *base = cached_modref->ldr.BaseAddress;
1116 status = STATUS_SUCCESS;
1121 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
1122 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1124 mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
1126 if (RtlEqualUnicodeString( name, &mod->FullDllName, TRUE ) ||
1127 RtlEqualUnicodeString( name, &mod->BaseDllName, TRUE ))
1129 cached_modref = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
1130 *base = mod->BaseAddress;
1131 status = STATUS_SUCCESS;
1136 RtlLeaveCriticalSection( &loader_section );
1137 TRACE("%lx %lx %s -> %p\n", x, y, debugstr_us(name), status ? NULL : *base);
1142 /******************************************************************
1143 * LdrGetProcedureAddress (NTDLL.@)
1145 NTSTATUS WINAPI LdrGetProcedureAddress(HMODULE module, const ANSI_STRING *name,
1146 ULONG ord, PVOID *address)
1148 IMAGE_EXPORT_DIRECTORY *exports;
1150 NTSTATUS ret = STATUS_PROCEDURE_NOT_FOUND;
1152 RtlEnterCriticalSection( &loader_section );
1154 if ((exports = RtlImageDirectoryEntryToData( module, TRUE,
1155 IMAGE_DIRECTORY_ENTRY_EXPORT, &exp_size )))
1157 void *proc = name ? find_named_export( module, exports, exp_size, name->Buffer, -1 )
1158 : find_ordinal_export( module, exports, exp_size, ord - exports->Base );
1162 ret = STATUS_SUCCESS;
1167 /* check if the module itself is invalid to return the proper error */
1168 if (!get_modref( module )) ret = STATUS_DLL_NOT_FOUND;
1171 RtlLeaveCriticalSection( &loader_section );
1176 /***********************************************************************
1177 * load_builtin_callback
1179 * Load a library in memory; callback function for wine_dll_register
1181 static void load_builtin_callback( void *module, const char *filename )
1183 static const WCHAR emptyW[1];
1185 IMAGE_NT_HEADERS *nt;
1187 WCHAR *fullname, *p;
1188 const WCHAR *load_path;
1192 ERR("could not map image for %s\n", filename ? filename : "main exe" );
1195 if (!(nt = RtlImageNtHeader( module )))
1197 ERR( "bad module for %s\n", filename ? filename : "main exe" );
1198 builtin_load_info->status = STATUS_INVALID_IMAGE_FORMAT;
1201 if (!(nt->FileHeader.Characteristics & IMAGE_FILE_DLL))
1203 /* if we already have an executable, ignore this one */
1204 if (!NtCurrentTeb()->Peb->ImageBaseAddress)
1206 NtCurrentTeb()->Peb->ImageBaseAddress = module;
1207 return; /* don't create the modref here, will be done later on */
1211 /* create the MODREF */
1213 if (!(fullname = RtlAllocateHeap( GetProcessHeap(), 0,
1214 system_dir.MaximumLength + (strlen(filename) + 1) * sizeof(WCHAR) )))
1216 ERR( "can't load %s\n", filename );
1217 builtin_load_info->status = STATUS_NO_MEMORY;
1220 memcpy( fullname, system_dir.Buffer, system_dir.Length );
1221 p = fullname + system_dir.Length / sizeof(WCHAR);
1222 if (p > fullname && p[-1] != '\\') *p++ = '\\';
1223 ascii_to_unicode( p, filename, strlen(filename) + 1 );
1225 wm = alloc_module( module, fullname );
1226 RtlFreeHeap( GetProcessHeap(), 0, fullname );
1229 ERR( "can't load %s\n", filename );
1230 builtin_load_info->status = STATUS_NO_MEMORY;
1233 wm->ldr.Flags |= LDR_WINE_INTERNAL;
1235 NtAllocateVirtualMemory( NtCurrentProcess(), &addr, 0, &nt->OptionalHeader.SizeOfImage,
1236 MEM_SYSTEM | MEM_IMAGE, PAGE_EXECUTE_WRITECOPY );
1240 load_path = builtin_load_info->load_path;
1241 if (!load_path) load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
1242 if (!load_path) load_path = emptyW;
1243 if (fixup_imports( wm, load_path ) != STATUS_SUCCESS)
1245 /* the module has only be inserted in the load & memory order lists */
1246 RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
1247 RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
1248 /* FIXME: free the modref */
1249 builtin_load_info->status = STATUS_DLL_NOT_FOUND;
1252 builtin_load_info->wm = wm;
1253 TRACE( "loaded %s %p %p\n", filename, wm, module );
1255 /* send the DLL load event */
1257 SERVER_START_REQ( load_dll )
1261 req->size = nt->OptionalHeader.SizeOfImage;
1262 req->dbg_offset = nt->FileHeader.PointerToSymbolTable;
1263 req->dbg_size = nt->FileHeader.NumberOfSymbols;
1264 req->name = &wm->ldr.FullDllName.Buffer;
1265 wine_server_add_data( req, wm->ldr.FullDllName.Buffer, wm->ldr.FullDllName.Length );
1266 wine_server_call( req );
1270 /* setup relay debugging entry points */
1271 if (TRACE_ON(relay)) RELAY_SetupDLL( module );
1275 /******************************************************************************
1276 * load_native_dll (internal)
1278 static NTSTATUS load_native_dll( LPCWSTR load_path, LPCWSTR name, HANDLE file,
1279 DWORD flags, WINE_MODREF** pwm )
1283 OBJECT_ATTRIBUTES attr;
1285 IMAGE_NT_HEADERS *nt;
1290 TRACE( "loading %s\n", debugstr_w(name) );
1292 attr.Length = sizeof(attr);
1293 attr.RootDirectory = 0;
1294 attr.ObjectName = NULL;
1295 attr.Attributes = 0;
1296 attr.SecurityDescriptor = NULL;
1297 attr.SecurityQualityOfService = NULL;
1300 status = NtCreateSection( &mapping, STANDARD_RIGHTS_REQUIRED | SECTION_QUERY | SECTION_MAP_READ,
1301 &attr, &size, 0, SEC_IMAGE, file );
1302 if (status != STATUS_SUCCESS) return status;
1305 status = NtMapViewOfSection( mapping, NtCurrentProcess(),
1306 &module, 0, 0, &size, &len, ViewShare, 0, PAGE_READONLY );
1308 if (status != STATUS_SUCCESS) return status;
1310 /* create the MODREF */
1312 if (!(wm = alloc_module( module, name ))) return STATUS_NO_MEMORY;
1316 if (!(flags & DONT_RESOLVE_DLL_REFERENCES))
1318 if ((status = fixup_imports( wm, load_path )) != STATUS_SUCCESS)
1320 /* the module has only be inserted in the load & memory order lists */
1321 RemoveEntryList(&wm->ldr.InLoadOrderModuleList);
1322 RemoveEntryList(&wm->ldr.InMemoryOrderModuleList);
1324 /* FIXME: there are several more dangling references
1325 * left. Including dlls loaded by this dll before the
1326 * failed one. Unrolling is rather difficult with the
1327 * current structure and we can leave them lying
1328 * around with no problems, so we don't care.
1329 * As these might reference our wm, we don't free it.
1334 else wm->ldr.Flags |= LDR_DONT_RESOLVE_REFS;
1336 /* send DLL load event */
1338 nt = RtlImageNtHeader( module );
1340 /* don't keep the file open if the mapping is from removable media */
1341 if (!VIRTUAL_HasMapping( module )) file = 0;
1343 SERVER_START_REQ( load_dll )
1347 req->size = nt->OptionalHeader.SizeOfImage;
1348 req->dbg_offset = nt->FileHeader.PointerToSymbolTable;
1349 req->dbg_size = nt->FileHeader.NumberOfSymbols;
1350 req->name = &wm->ldr.FullDllName.Buffer;
1351 wine_server_add_data( req, wm->ldr.FullDllName.Buffer, wm->ldr.FullDllName.Length );
1352 wine_server_call( req );
1356 if (TRACE_ON(snoop)) SNOOP_SetupDLL( module );
1359 return STATUS_SUCCESS;
1363 /***********************************************************************
1366 static NTSTATUS load_builtin_dll( LPCWSTR load_path, LPCWSTR path, DWORD flags, WINE_MODREF** pwm )
1368 char error[256], dllname[MAX_PATH];
1370 const WCHAR *name, *p;
1373 struct builtin_load_info info, *prev_info;
1375 /* Fix the name in case we have a full path and extension */
1377 if ((p = strrchrW( name, '\\' ))) name = p + 1;
1378 if ((p = strrchrW( name, '/' ))) name = p + 1;
1380 /* we don't want to depend on the current codepage here */
1381 len = strlenW( name ) + 1;
1382 if (len >= sizeof(dllname)) return STATUS_NAME_TOO_LONG;
1383 for (i = 0; i < len; i++)
1385 if (name[i] > 127) return STATUS_DLL_NOT_FOUND;
1386 dllname[i] = (char)name[i];
1387 if (dllname[i] >= 'A' && dllname[i] <= 'Z') dllname[i] += 'a' - 'A';
1390 /* load_library will modify info.status. Note also that load_library can be
1391 * called several times, if the .so file we're loading has dependencies.
1392 * info.status will gather all the errors we may get while loading all these
1395 info.load_path = load_path;
1396 info.status = STATUS_SUCCESS;
1398 prev_info = builtin_load_info;
1399 builtin_load_info = &info;
1400 handle = wine_dll_load( dllname, error, sizeof(error), &file_exists );
1401 builtin_load_info = prev_info;
1407 /* The file does not exist -> WARN() */
1408 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name), error);
1409 return STATUS_DLL_NOT_FOUND;
1411 /* ERR() for all other errors (missing functions, ...) */
1412 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name), error );
1413 return STATUS_PROCEDURE_NOT_FOUND;
1415 if (info.status != STATUS_SUCCESS) return info.status;
1419 /* The constructor wasn't called, this means the .so is already
1420 * loaded under a different name. We can't support multiple names
1421 * for the same module, so return an error. */
1422 return STATUS_INVALID_IMAGE_FORMAT;
1425 info.wm->ldr.SectionHandle = handle;
1426 if (strcmpiW( info.wm->ldr.BaseDllName.Buffer, name ))
1428 ERR( "loaded .so for %s but got %s instead - probably 16-bit dll\n",
1429 debugstr_w(name), debugstr_w(info.wm->ldr.BaseDllName.Buffer) );
1430 /* wine_dll_unload( handle );*/
1431 return STATUS_INVALID_IMAGE_FORMAT;
1434 return STATUS_SUCCESS;
1438 /***********************************************************************
1441 * Find the file (or already loaded module) for a given dll name.
1443 static NTSTATUS find_dll_file( const WCHAR *load_path, const WCHAR *libname,
1444 WCHAR *filename, ULONG *size, WINE_MODREF **pwm, HANDLE *handle )
1446 OBJECT_ATTRIBUTES attr;
1448 UNICODE_STRING nt_name;
1449 WCHAR *file_part, *ext, *dllname;
1452 /* first append .dll if needed */
1455 if (!(ext = strrchrW( libname, '.')) || strchrW( ext, '/' ) || strchrW( ext, '\\'))
1457 if (!(dllname = RtlAllocateHeap( GetProcessHeap(), 0,
1458 (strlenW(libname) * sizeof(WCHAR)) + sizeof(dllW) )))
1459 return STATUS_NO_MEMORY;
1460 strcpyW( dllname, libname );
1461 strcatW( dllname, dllW );
1465 nt_name.Buffer = NULL;
1466 if (RtlDetermineDosPathNameType_U( libname ) == RELATIVE_PATH)
1468 /* we need to search for it */
1469 len = RtlDosSearchPath_U( load_path, libname, NULL, *size, filename, &file_part );
1472 if (len >= *size) goto overflow;
1473 if ((*pwm = find_fullname_module( filename )) != NULL) goto found;
1475 /* check for already loaded module in a different path */
1476 if (!contains_path( libname ))
1478 if ((*pwm = find_basename_module( file_part )) != NULL) goto found;
1480 if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, NULL, NULL ))
1482 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1483 return STATUS_NO_MEMORY;
1485 attr.Length = sizeof(attr);
1486 attr.RootDirectory = 0;
1487 attr.Attributes = OBJ_CASE_INSENSITIVE;
1488 attr.ObjectName = &nt_name;
1489 attr.SecurityDescriptor = NULL;
1490 attr.SecurityQualityOfService = NULL;
1491 if (NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_DELETE, 0 )) *handle = 0;
1497 if (!contains_path( libname ))
1499 /* if libname doesn't contain a path at all, we simply return the name as is,
1500 * to be loaded as builtin */
1501 len = strlenW(libname) * sizeof(WCHAR);
1502 if (len >= *size) goto overflow;
1503 strcpyW( filename, libname );
1504 *pwm = find_basename_module( filename );
1509 /* absolute path name, or relative path name but not found above */
1511 if (!RtlDosPathNameToNtPathName_U( libname, &nt_name, &file_part, NULL ))
1513 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1514 return STATUS_NO_MEMORY;
1516 len = nt_name.Length - 4*sizeof(WCHAR); /* for \??\ prefix */
1517 if (len >= *size) goto overflow;
1518 memcpy( filename, nt_name.Buffer + 4, len + sizeof(WCHAR) );
1519 if (!(*pwm = find_fullname_module( filename )))
1521 attr.Length = sizeof(attr);
1522 attr.RootDirectory = 0;
1523 attr.Attributes = OBJ_CASE_INSENSITIVE;
1524 attr.ObjectName = &nt_name;
1525 attr.SecurityDescriptor = NULL;
1526 attr.SecurityQualityOfService = NULL;
1527 if (NtOpenFile( handle, GENERIC_READ, &attr, &io, FILE_SHARE_READ|FILE_SHARE_DELETE, 0 )) *handle = 0;
1530 RtlFreeUnicodeString( &nt_name );
1531 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1532 return STATUS_SUCCESS;
1535 RtlFreeUnicodeString( &nt_name );
1536 RtlFreeHeap( GetProcessHeap(), 0, dllname );
1537 *size = len + sizeof(WCHAR);
1538 return STATUS_BUFFER_TOO_SMALL;
1542 /***********************************************************************
1543 * load_dll (internal)
1545 * Load a PE style module according to the load order.
1546 * The loader_section must be locked while calling this function.
1548 static NTSTATUS load_dll( LPCWSTR load_path, LPCWSTR libname, DWORD flags, WINE_MODREF** pwm )
1551 enum loadorder_type loadorder[LOADORDER_NTYPES];
1555 const char *filetype = "";
1556 WINE_MODREF *main_exe;
1560 TRACE( "looking for %s in %s\n", debugstr_w(libname), debugstr_w(load_path) );
1563 size = sizeof(buffer);
1566 nts = find_dll_file( load_path, libname, filename, &size, pwm, &handle );
1567 if (nts == STATUS_SUCCESS) break;
1568 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1569 if (nts != STATUS_BUFFER_TOO_SMALL) return nts;
1570 /* grow the buffer and retry */
1571 if (!(filename = RtlAllocateHeap( GetProcessHeap(), 0, size ))) return STATUS_NO_MEMORY;
1574 if (*pwm) /* found already loaded module */
1576 if ((*pwm)->ldr.LoadCount != -1) (*pwm)->ldr.LoadCount++;
1578 if (((*pwm)->ldr.Flags & LDR_DONT_RESOLVE_REFS) &&
1579 !(flags & DONT_RESOLVE_DLL_REFERENCES))
1581 (*pwm)->ldr.Flags &= ~LDR_DONT_RESOLVE_REFS;
1582 fixup_imports( *pwm, load_path );
1584 TRACE("Found loaded module %s for %s at %p, count=%d\n",
1585 debugstr_w((*pwm)->ldr.FullDllName.Buffer), debugstr_w(libname),
1586 (*pwm)->ldr.BaseAddress, (*pwm)->ldr.LoadCount);
1587 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1588 return STATUS_SUCCESS;
1591 main_exe = get_modref( NtCurrentTeb()->Peb->ImageBaseAddress );
1592 MODULE_GetLoadOrderW( loadorder, main_exe ? main_exe->ldr.BaseDllName.Buffer : NULL, filename );
1594 nts = STATUS_DLL_NOT_FOUND;
1595 for (i = 0; i < LOADORDER_NTYPES; i++)
1597 if (loadorder[i] == LOADORDER_INVALID) break;
1599 switch (loadorder[i])
1602 TRACE("Trying native dll %s\n", debugstr_w(filename));
1603 if (!handle) continue; /* it cannot possibly be loaded */
1604 nts = load_native_dll( load_path, filename, handle, flags, pwm );
1605 filetype = "native";
1608 TRACE("Trying built-in %s\n", debugstr_w(filename));
1609 nts = load_builtin_dll( load_path, filename, flags, pwm );
1610 filetype = "builtin";
1613 nts = STATUS_INTERNAL_ERROR;
1617 if (nts == STATUS_SUCCESS)
1619 /* Initialize DLL just loaded */
1620 TRACE("Loaded module %s (%s) at %p\n",
1621 debugstr_w(filename), filetype, (*pwm)->ldr.BaseAddress);
1622 if (!TRACE_ON(module))
1623 TRACE_(loaddll)("Loaded module %s : %s\n",
1624 debugstr_w((*pwm)->ldr.FullDllName.Buffer), filetype);
1625 /* Set the ldr.LoadCount here so that an attach failure will */
1626 /* decrement the dependencies through the MODULE_FreeLibrary call. */
1627 (*pwm)->ldr.LoadCount = 1;
1628 if (handle) NtClose( handle );
1629 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1632 if (nts != STATUS_DLL_NOT_FOUND) break;
1635 WARN("Failed to load module %s; status=%lx\n", debugstr_w(libname), nts);
1636 if (handle) NtClose( handle );
1637 if (filename != buffer) RtlFreeHeap( GetProcessHeap(), 0, filename );
1641 /******************************************************************
1642 * LdrLoadDll (NTDLL.@)
1644 NTSTATUS WINAPI LdrLoadDll(LPCWSTR path_name, DWORD flags,
1645 const UNICODE_STRING *libname, HMODULE* hModule)
1650 RtlEnterCriticalSection( &loader_section );
1652 if (!path_name) path_name = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
1653 nts = load_dll( path_name, libname->Buffer, flags, &wm );
1655 if (nts == STATUS_SUCCESS && !(wm->ldr.Flags & LDR_DONT_RESOLVE_REFS))
1657 nts = process_attach( wm, NULL );
1658 if (nts != STATUS_SUCCESS)
1660 WARN("Attach failed for module %s\n", debugstr_w(libname->Buffer));
1661 LdrUnloadDll(wm->ldr.BaseAddress);
1665 *hModule = (wm) ? wm->ldr.BaseAddress : NULL;
1667 RtlLeaveCriticalSection( &loader_section );
1671 /******************************************************************
1672 * LdrQueryProcessModuleInformation
1675 NTSTATUS WINAPI LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi,
1676 ULONG buf_size, ULONG* req_size)
1678 SYSTEM_MODULE* sm = &smi->Modules[0];
1679 ULONG size = sizeof(ULONG);
1680 NTSTATUS nts = STATUS_SUCCESS;
1683 PLIST_ENTRY mark, entry;
1686 smi->ModulesCount = 0;
1688 RtlEnterCriticalSection( &loader_section );
1689 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
1690 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1692 mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
1693 size += sizeof(*sm);
1694 if (size <= buf_size)
1696 sm->Reserved1 = 0; /* FIXME */
1697 sm->Reserved2 = 0; /* FIXME */
1698 sm->ImageBaseAddress = mod->BaseAddress;
1699 sm->ImageSize = mod->SizeOfImage;
1700 sm->Flags = mod->Flags;
1701 sm->Id = 0; /* FIXME */
1702 sm->Rank = 0; /* FIXME */
1703 sm->Unknown = 0; /* FIXME */
1705 str.MaximumLength = MAXIMUM_FILENAME_LENGTH;
1706 str.Buffer = sm->Name;
1707 RtlUnicodeStringToAnsiString(&str, &mod->FullDllName, FALSE);
1708 ptr = strrchr(sm->Name, '\\');
1709 sm->NameOffset = (ptr != NULL) ? (ptr - (char*)sm->Name + 1) : 0;
1711 smi->ModulesCount++;
1714 else nts = STATUS_INFO_LENGTH_MISMATCH;
1716 RtlLeaveCriticalSection( &loader_section );
1718 if (req_size) *req_size = size;
1723 /******************************************************************
1724 * LdrShutdownProcess (NTDLL.@)
1727 void WINAPI LdrShutdownProcess(void)
1730 process_detach( TRUE, (LPVOID)1 );
1733 /******************************************************************
1734 * LdrShutdownThread (NTDLL.@)
1737 void WINAPI LdrShutdownThread(void)
1739 PLIST_ENTRY mark, entry;
1744 /* don't do any detach calls if process is exiting */
1745 if (process_detaching) return;
1746 /* FIXME: there is still a race here */
1748 RtlEnterCriticalSection( &loader_section );
1750 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
1751 for (entry = mark->Blink; entry != mark; entry = entry->Blink)
1753 mod = CONTAINING_RECORD(entry, LDR_MODULE,
1754 InInitializationOrderModuleList);
1755 if ( !(mod->Flags & LDR_PROCESS_ATTACHED) )
1757 if ( mod->Flags & LDR_NO_DLL_CALLS )
1760 MODULE_InitDLL( CONTAINING_RECORD(mod, WINE_MODREF, ldr),
1761 DLL_THREAD_DETACH, NULL );
1764 RtlLeaveCriticalSection( &loader_section );
1767 /***********************************************************************
1768 * MODULE_FlushModrefs
1770 * Remove all unused modrefs and call the internal unloading routines
1771 * for the library type.
1773 * The loader_section must be locked while calling this function.
1775 static void MODULE_FlushModrefs(void)
1777 PLIST_ENTRY mark, entry, prev;
1781 mark = &NtCurrentTeb()->Peb->LdrData->InInitializationOrderModuleList;
1782 for (entry = mark->Blink; entry != mark; entry = prev)
1784 mod = CONTAINING_RECORD(entry, LDR_MODULE,
1785 InInitializationOrderModuleList);
1786 wm = CONTAINING_RECORD(mod, WINE_MODREF, ldr);
1788 prev = entry->Blink;
1789 if (mod->LoadCount) continue;
1791 RemoveEntryList(&mod->InLoadOrderModuleList);
1792 RemoveEntryList(&mod->InMemoryOrderModuleList);
1793 RemoveEntryList(&mod->InInitializationOrderModuleList);
1795 TRACE(" unloading %s\n", debugstr_w(mod->FullDllName.Buffer));
1796 if (!TRACE_ON(module))
1797 TRACE_(loaddll)("Unloaded module %s : %s\n",
1798 debugstr_w(mod->FullDllName.Buffer),
1799 (wm->ldr.Flags & LDR_WINE_INTERNAL) ? "builtin" : "native" );
1801 SERVER_START_REQ( unload_dll )
1803 req->base = mod->BaseAddress;
1804 wine_server_call( req );
1808 NtUnmapViewOfSection( NtCurrentProcess(), mod->BaseAddress );
1809 if (wm->ldr.Flags & LDR_WINE_INTERNAL) wine_dll_unload( wm->ldr.SectionHandle );
1810 if (cached_modref == wm) cached_modref = NULL;
1811 RtlFreeUnicodeString( &mod->FullDllName );
1812 RtlFreeHeap( GetProcessHeap(), 0, wm->deps );
1813 RtlFreeHeap( GetProcessHeap(), 0, wm );
1817 /***********************************************************************
1818 * MODULE_DecRefCount
1820 * The loader_section must be locked while calling this function.
1822 static void MODULE_DecRefCount( WINE_MODREF *wm )
1826 if ( wm->ldr.Flags & LDR_UNLOAD_IN_PROGRESS )
1829 if ( wm->ldr.LoadCount <= 0 )
1832 --wm->ldr.LoadCount;
1833 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm->ldr.BaseDllName.Buffer), wm->ldr.LoadCount );
1835 if ( wm->ldr.LoadCount == 0 )
1837 wm->ldr.Flags |= LDR_UNLOAD_IN_PROGRESS;
1839 for ( i = 0; i < wm->nDeps; i++ )
1841 MODULE_DecRefCount( wm->deps[i] );
1843 wm->ldr.Flags &= ~LDR_UNLOAD_IN_PROGRESS;
1847 /******************************************************************
1848 * LdrUnloadDll (NTDLL.@)
1852 NTSTATUS WINAPI LdrUnloadDll( HMODULE hModule )
1854 NTSTATUS retv = STATUS_SUCCESS;
1856 TRACE("(%p)\n", hModule);
1858 RtlEnterCriticalSection( &loader_section );
1860 /* if we're stopping the whole process (and forcing the removal of all
1861 * DLLs) the library will be freed anyway
1863 if (!process_detaching)
1868 if ((wm = get_modref( hModule )) != NULL)
1870 TRACE("(%s) - START\n", debugstr_w(wm->ldr.BaseDllName.Buffer));
1872 /* Recursively decrement reference counts */
1873 MODULE_DecRefCount( wm );
1875 /* Call process detach notifications */
1876 if ( free_lib_count <= 1 )
1878 process_detach( FALSE, NULL );
1879 MODULE_FlushModrefs();
1885 retv = STATUS_DLL_NOT_FOUND;
1890 RtlLeaveCriticalSection( &loader_section );
1895 /***********************************************************************
1896 * RtlImageNtHeader (NTDLL.@)
1898 PIMAGE_NT_HEADERS WINAPI RtlImageNtHeader(HMODULE hModule)
1900 IMAGE_NT_HEADERS *ret;
1904 IMAGE_DOS_HEADER *dos = (IMAGE_DOS_HEADER *)hModule;
1907 if (dos->e_magic == IMAGE_DOS_SIGNATURE)
1909 ret = (IMAGE_NT_HEADERS *)((char *)dos + dos->e_lfanew);
1910 if (ret->Signature != IMAGE_NT_SIGNATURE) ret = NULL;
1913 __EXCEPT(page_fault)
1922 /******************************************************************
1923 * LdrInitializeThunk (NTDLL.@)
1925 * FIXME: the arguments are not correct, main_file is a Wine invention.
1927 void WINAPI LdrInitializeThunk( HANDLE main_file, ULONG unknown2, ULONG unknown3, ULONG unknown4 )
1932 PEB *peb = NtCurrentTeb()->Peb;
1933 UNICODE_STRING *main_exe_name = &peb->ProcessParameters->ImagePathName;
1934 IMAGE_NT_HEADERS *nt = RtlImageNtHeader( peb->ImageBaseAddress );
1936 /* allocate the modref for the main exe */
1937 if (!(wm = alloc_module( peb->ImageBaseAddress, main_exe_name->Buffer )))
1939 status = STATUS_NO_MEMORY;
1942 wm->ldr.LoadCount = -1; /* can't unload main exe */
1944 /* the main exe needs to be the first in the load order list */
1945 RemoveEntryList( &wm->ldr.InLoadOrderModuleList );
1946 InsertHeadList( &peb->LdrData->InLoadOrderModuleList, &wm->ldr.InLoadOrderModuleList );
1948 /* Install signal handlers; this cannot be done before, since we cannot
1949 * send exceptions to the debugger before the create process event that
1950 * is sent by REQ_INIT_PROCESS_DONE.
1951 * We do need the handlers in place by the time the request is over, so
1952 * we set them up here. If we segfault between here and the server call
1953 * something is very wrong... */
1954 if (!SIGNAL_Init()) exit(1);
1956 /* Signal the parent process to continue */
1957 SERVER_START_REQ( init_process_done )
1959 req->module = peb->ImageBaseAddress;
1960 req->module_size = wm->ldr.SizeOfImage;
1961 req->entry = (char *)peb->ImageBaseAddress + nt->OptionalHeader.AddressOfEntryPoint;
1962 /* API requires a double indirection */
1963 req->name = &main_exe_name->Buffer;
1964 req->exe_file = main_file;
1965 req->gui = (nt->OptionalHeader.Subsystem != IMAGE_SUBSYSTEM_WINDOWS_CUI);
1966 wine_server_add_data( req, main_exe_name->Buffer, main_exe_name->Length );
1967 wine_server_call( req );
1971 if (main_file) NtClose( main_file ); /* we no longer need it */
1973 if (TRACE_ON(relay) || TRACE_ON(snoop))
1975 RELAY_InitDebugLists();
1977 if (TRACE_ON(relay)) /* setup relay for already loaded dlls */
1979 LIST_ENTRY *entry, *mark = &peb->LdrData->InLoadOrderModuleList;
1980 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
1982 LDR_MODULE *mod = CONTAINING_RECORD(entry, LDR_MODULE, InLoadOrderModuleList);
1983 if (mod->Flags & LDR_WINE_INTERNAL) RELAY_SetupDLL( mod->BaseAddress );
1988 RtlEnterCriticalSection( &loader_section );
1990 load_path = NtCurrentTeb()->Peb->ProcessParameters->DllPath.Buffer;
1991 if ((status = fixup_imports( wm, load_path )) != STATUS_SUCCESS) goto error;
1992 if ((status = alloc_process_tls()) != STATUS_SUCCESS) goto error;
1993 if ((status = alloc_thread_tls()) != STATUS_SUCCESS) goto error;
1994 if ((status = process_attach( wm, (LPVOID)1 )) != STATUS_SUCCESS)
1996 if (last_failed_modref)
1997 ERR( "%s failed to initialize, aborting\n", debugstr_w(last_failed_modref->ldr.BaseDllName.Buffer) + 1 );
2001 RtlLeaveCriticalSection( &loader_section );
2003 if (nt->FileHeader.Characteristics & IMAGE_FILE_LARGE_ADDRESS_AWARE) VIRTUAL_UseLargeAddressSpace();
2007 ERR( "Main exe initialization for %s failed, status %lx\n", debugstr_w(main_exe_name->Buffer), status );
2012 /***********************************************************************
2013 * RtlImageDirectoryEntryToData (NTDLL.@)
2015 PVOID WINAPI RtlImageDirectoryEntryToData( HMODULE module, BOOL image, WORD dir, ULONG *size )
2017 const IMAGE_NT_HEADERS *nt;
2020 if ((ULONG_PTR)module & 1) /* mapped as data file */
2022 module = (HMODULE)((ULONG_PTR)module & ~1);
2025 if (!(nt = RtlImageNtHeader( module ))) return NULL;
2026 if (dir >= nt->OptionalHeader.NumberOfRvaAndSizes) return NULL;
2027 if (!(addr = nt->OptionalHeader.DataDirectory[dir].VirtualAddress)) return NULL;
2028 *size = nt->OptionalHeader.DataDirectory[dir].Size;
2029 if (image || addr < nt->OptionalHeader.SizeOfHeaders) return (char *)module + addr;
2031 /* not mapped as image, need to find the section containing the virtual address */
2032 return RtlImageRvaToVa( nt, module, addr, NULL );
2036 /***********************************************************************
2037 * RtlImageRvaToSection (NTDLL.@)
2039 PIMAGE_SECTION_HEADER WINAPI RtlImageRvaToSection( const IMAGE_NT_HEADERS *nt,
2040 HMODULE module, DWORD rva )
2043 const IMAGE_SECTION_HEADER *sec;
2045 sec = (const IMAGE_SECTION_HEADER*)((const char*)&nt->OptionalHeader +
2046 nt->FileHeader.SizeOfOptionalHeader);
2047 for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++)
2049 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
2050 return (PIMAGE_SECTION_HEADER)sec;
2056 /***********************************************************************
2057 * RtlImageRvaToVa (NTDLL.@)
2059 PVOID WINAPI RtlImageRvaToVa( const IMAGE_NT_HEADERS *nt, HMODULE module,
2060 DWORD rva, IMAGE_SECTION_HEADER **section )
2062 IMAGE_SECTION_HEADER *sec;
2064 if (section && *section) /* try this section first */
2067 if ((sec->VirtualAddress <= rva) && (sec->VirtualAddress + sec->SizeOfRawData > rva))
2070 if (!(sec = RtlImageRvaToSection( nt, module, rva ))) return NULL;
2072 if (section) *section = sec;
2073 return (char *)module + sec->PointerToRawData + (rva - sec->VirtualAddress);
2077 /***********************************************************************
2078 * NtLoadDriver (NTDLL.@)
2079 * ZwLoadDriver (NTDLL.@)
2081 NTSTATUS WINAPI NtLoadDriver( const UNICODE_STRING *DriverServiceName )
2083 FIXME("(%p), stub!\n",DriverServiceName);
2084 return STATUS_NOT_IMPLEMENTED;
2088 /***********************************************************************
2089 * NtUnloadDriver (NTDLL.@)
2090 * ZwUnloadDriver (NTDLL.@)
2092 NTSTATUS WINAPI NtUnloadDriver( const UNICODE_STRING *DriverServiceName )
2094 FIXME("(%p), stub!\n",DriverServiceName);
2095 return STATUS_NOT_IMPLEMENTED;
2099 /******************************************************************
2100 * __wine_init_windows_dir (NTDLL.@)
2102 * Windows and system dir initialization once kernel32 has been loaded.
2104 void __wine_init_windows_dir( const WCHAR *windir, const WCHAR *sysdir )
2106 PLIST_ENTRY mark, entry;
2109 RtlCreateUnicodeString( &system_dir, sysdir );
2111 /* prepend the system dir to the name of the already created modules */
2112 mark = &NtCurrentTeb()->Peb->LdrData->InLoadOrderModuleList;
2113 for (entry = mark->Flink; entry != mark; entry = entry->Flink)
2115 LDR_MODULE *mod = CONTAINING_RECORD( entry, LDR_MODULE, InLoadOrderModuleList );
2117 assert( mod->Flags & LDR_WINE_INTERNAL );
2119 buffer = RtlAllocateHeap( GetProcessHeap(), 0,
2120 system_dir.Length + mod->FullDllName.Length + 2*sizeof(WCHAR) );
2121 if (!buffer) continue;
2122 strcpyW( buffer, system_dir.Buffer );
2123 p = buffer + strlenW( buffer );
2124 if (p > buffer && p[-1] != '\\') *p++ = '\\';
2125 strcpyW( p, mod->FullDllName.Buffer );
2126 RtlInitUnicodeString( &mod->FullDllName, buffer );
2127 RtlInitUnicodeString( &mod->BaseDllName, p );
2132 /***********************************************************************
2133 * __wine_process_init
2135 void __wine_process_init( int argc, char *argv[] )
2137 static const WCHAR kernel32W[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
2141 ANSI_STRING func_name;
2142 void (* DECLSPEC_NORETURN init_func)();
2143 extern mode_t FILE_umask;
2147 /* retrieve current umask */
2148 FILE_umask = umask(0777);
2149 umask( FILE_umask );
2151 /* setup the load callback and create ntdll modref */
2152 wine_dll_set_callback( load_builtin_callback );
2154 if ((status = load_builtin_dll( NULL, kernel32W, 0, &wm )) != STATUS_SUCCESS)
2156 MESSAGE( "wine: could not load kernel32.dll, status %lx\n", status );
2159 RtlInitAnsiString( &func_name, "__wine_kernel_init" );
2160 if ((status = LdrGetProcedureAddress( wm->ldr.BaseAddress, &func_name,
2161 0, (void **)&init_func )) != STATUS_SUCCESS)
2163 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %lx\n", status );