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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "wine/port.h"
27 #ifdef HAVE_SYS_MMAN_H
28 # include <sys/mman.h>
31 #define NONAMELESSUNION
32 #define NONAMELESSSTRUCT
35 #define WIN32_NO_STATUS
40 #include "wine/exception.h"
41 #include "wine/library.h"
42 #include "wine/unicode.h"
43 #include "wine/debug.h"
44 #include "wine/server.h"
45 #include "ntdll_misc.h"
48 WINE_DEFAULT_DEBUG_CHANNEL(module
);
49 WINE_DECLARE_DEBUG_CHANNEL(relay
);
50 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
51 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
52 WINE_DECLARE_DEBUG_CHANNEL(imports
);
54 /* we don't want to include winuser.h */
55 #define RT_MANIFEST ((ULONG_PTR)24)
56 #define ISOLATIONAWARE_MANIFEST_RESOURCE_ID ((ULONG_PTR)2)
58 typedef DWORD (CALLBACK
*DLLENTRYPROC
)(HMODULE
,DWORD
,LPVOID
);
60 static int process_detaching
= 0; /* set on process detach to avoid deadlocks with thread detach */
61 static int free_lib_count
; /* recursion depth of LdrUnloadDll calls */
63 static const char * const reason_names
[] =
69 NULL
, NULL
, NULL
, NULL
,
73 static const WCHAR dllW
[] = {'.','d','l','l',0};
75 /* internal representation of 32bit modules. per process. */
76 typedef struct _wine_modref
80 struct _wine_modref
**deps
;
83 /* info about the current builtin dll load */
84 /* used to keep track of things across the register_dll constructor call */
85 struct builtin_load_info
87 const WCHAR
*load_path
;
88 const WCHAR
*filename
;
93 static struct builtin_load_info default_load_info
;
94 static struct builtin_load_info
*builtin_load_info
= &default_load_info
;
96 static HANDLE main_exe_file
;
97 static UINT tls_module_count
; /* number of modules with TLS directory */
98 static UINT tls_total_size
; /* total size of TLS storage */
99 static const IMAGE_TLS_DIRECTORY
**tls_dirs
; /* array of TLS directories */
101 UNICODE_STRING windows_dir
= { 0, 0, NULL
}; /* windows directory */
102 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
104 static RTL_CRITICAL_SECTION loader_section
;
105 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
107 0, 0, &loader_section
,
108 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
109 0, 0, { (DWORD_PTR
)(__FILE__
": loader_section") }
111 static RTL_CRITICAL_SECTION loader_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
113 static WINE_MODREF
*cached_modref
;
114 static WINE_MODREF
*current_modref
;
115 static WINE_MODREF
*last_failed_modref
;
117 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
);
118 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
);
119 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
120 DWORD exp_size
, const char *name
, int hint
, LPCWSTR load_path
);
122 /* convert PE image VirtualAddress to Real Address */
123 static inline void *get_rva( HMODULE module
, DWORD va
)
125 return (void *)((char *)module
+ va
);
128 /* check whether the file name contains a path */
129 static inline int contains_path( LPCWSTR name
)
131 return ((*name
&& (name
[1] == ':')) || strchrW(name
, '/') || strchrW(name
, '\\'));
134 /* convert from straight ASCII to Unicode without depending on the current codepage */
135 static inline void ascii_to_unicode( WCHAR
*dst
, const char *src
, size_t len
)
137 while (len
--) *dst
++ = (unsigned char)*src
++;
141 /*************************************************************************
142 * call_dll_entry_point
144 * Some brain-damaged dlls (ir32_32.dll for instance) modify ebx in
145 * their entry point, so we need a small asm wrapper.
148 extern BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
, UINT reason
, void *reserved
);
149 __ASM_GLOBAL_FUNC(call_dll_entry_point
,
157 "movl 8(%ebp),%eax\n\t"
159 "leal -4(%ebp),%esp\n\t"
164 static inline BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
,
165 UINT reason
, void *reserved
)
167 return proc( module
, reason
, reserved
);
169 #endif /* __i386__ */
172 #if defined(__i386__) || defined(__x86_64__)
173 /*************************************************************************
176 * Entry point for stub functions.
178 static void stub_entry_point( const char *dll
, const char *name
, void *ret_addr
)
180 EXCEPTION_RECORD rec
;
182 rec
.ExceptionCode
= EXCEPTION_WINE_STUB
;
183 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
184 rec
.ExceptionRecord
= NULL
;
185 rec
.ExceptionAddress
= ret_addr
;
186 rec
.NumberParameters
= 2;
187 rec
.ExceptionInformation
[0] = (ULONG_PTR
)dll
;
188 rec
.ExceptionInformation
[1] = (ULONG_PTR
)name
;
189 for (;;) RtlRaiseException( &rec
);
193 #include "pshpack1.h"
197 BYTE pushl1
; /* pushl $name */
199 BYTE pushl2
; /* pushl $dll */
201 BYTE call
; /* call stub_entry_point */
207 BYTE movq_rdi
[2]; /* movq $dll,%rdi */
209 BYTE movq_rsi
[2]; /* movq $name,%rsi */
211 BYTE movq_rsp_rdx
[4]; /* movq (%rsp),%rdx */
212 BYTE movq_rax
[2]; /* movq $entry, %rax */
214 BYTE jmpq_rax
[2]; /* jmp %rax */
219 /*************************************************************************
222 * Allocate a stub entry point.
224 static ULONG_PTR
allocate_stub( const char *dll
, const char *name
)
226 #define MAX_SIZE 65536
227 static struct stub
*stubs
;
228 static unsigned int nb_stubs
;
231 if (nb_stubs
>= MAX_SIZE
/ sizeof(*stub
)) return 0xdeadbeef;
235 SIZE_T size
= MAX_SIZE
;
236 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs
, 0, &size
,
237 MEM_COMMIT
, PAGE_EXECUTE_WRITECOPY
) != STATUS_SUCCESS
)
240 stub
= &stubs
[nb_stubs
++];
242 stub
->pushl1
= 0x68; /* pushl $name */
244 stub
->pushl2
= 0x68; /* pushl $dll */
246 stub
->call
= 0xe8; /* call stub_entry_point */
247 stub
->entry
= (BYTE
*)stub_entry_point
- (BYTE
*)(&stub
->entry
+ 1);
249 stub
->movq_rdi
[0] = 0x48; /* movq $dll,%rdi */
250 stub
->movq_rdi
[1] = 0xbf;
252 stub
->movq_rsi
[0] = 0x48; /* movq $name,%rsi */
253 stub
->movq_rsi
[1] = 0xbe;
255 stub
->movq_rsp_rdx
[0] = 0x48; /* movq (%rsp),%rdx */
256 stub
->movq_rsp_rdx
[1] = 0x8b;
257 stub
->movq_rsp_rdx
[2] = 0x14;
258 stub
->movq_rsp_rdx
[3] = 0x24;
259 stub
->movq_rax
[0] = 0x48; /* movq $entry, %rax */
260 stub
->movq_rax
[1] = 0xb8;
261 stub
->entry
= stub_entry_point
;
262 stub
->jmpq_rax
[0] = 0xff; /* jmp %rax */
263 stub
->jmpq_rax
[1] = 0xe0;
265 return (ULONG_PTR
)stub
;
269 static inline ULONG_PTR
allocate_stub( const char *dll
, const char *name
) { return 0xdeadbeef; }
270 #endif /* __i386__ */
273 /*************************************************************************
276 * Looks for the referenced HMODULE in the current process
277 * The loader_section must be locked while calling this function.
279 static WINE_MODREF
*get_modref( HMODULE hmod
)
281 PLIST_ENTRY mark
, entry
;
284 if (cached_modref
&& cached_modref
->ldr
.BaseAddress
== hmod
) return cached_modref
;
286 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
287 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
289 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
290 if (mod
->BaseAddress
== hmod
)
291 return cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
292 if (mod
->BaseAddress
> (void*)hmod
) break;
298 /**********************************************************************
299 * find_basename_module
301 * Find a module from its base name.
302 * The loader_section must be locked while calling this function
304 static WINE_MODREF
*find_basename_module( LPCWSTR name
)
306 PLIST_ENTRY mark
, entry
;
308 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.BaseDllName
.Buffer
))
309 return cached_modref
;
311 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
312 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
314 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
315 if (!strcmpiW( name
, mod
->BaseDllName
.Buffer
))
317 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
318 return cached_modref
;
325 /**********************************************************************
326 * find_fullname_module
328 * Find a module from its full path name.
329 * The loader_section must be locked while calling this function
331 static WINE_MODREF
*find_fullname_module( LPCWSTR name
)
333 PLIST_ENTRY mark
, entry
;
335 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.FullDllName
.Buffer
))
336 return cached_modref
;
338 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
339 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
341 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
342 if (!strcmpiW( name
, mod
->FullDllName
.Buffer
))
344 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
345 return cached_modref
;
352 /*************************************************************************
353 * find_forwarded_export
355 * Find the final function pointer for a forwarded function.
356 * The loader_section must be locked while calling this function.
358 static FARPROC
find_forwarded_export( HMODULE module
, const char *forward
, LPCWSTR load_path
)
360 const IMAGE_EXPORT_DIRECTORY
*exports
;
364 const char *end
= strrchr(forward
, '.');
367 if (!end
) return NULL
;
368 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
)) return NULL
;
369 ascii_to_unicode( mod_name
, forward
, end
- forward
);
370 mod_name
[end
- forward
] = 0;
371 if (!strchrW( mod_name
, '.' ))
373 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
) - sizeof(dllW
)) return NULL
;
374 memcpy( mod_name
+ (end
- forward
), dllW
, sizeof(dllW
) );
377 if (!(wm
= find_basename_module( mod_name
)))
379 TRACE( "delay loading %s for '%s'\n", debugstr_w(mod_name
), forward
);
380 if (load_dll( load_path
, mod_name
, 0, &wm
) == STATUS_SUCCESS
&&
381 !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
383 if (process_attach( wm
, NULL
) != STATUS_SUCCESS
)
385 LdrUnloadDll( wm
->ldr
.BaseAddress
);
392 ERR( "module not found for forward '%s' used by %s\n",
393 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
) );
397 if ((exports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
398 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
399 proc
= find_named_export( wm
->ldr
.BaseAddress
, exports
, exp_size
, end
+ 1, -1, load_path
);
403 ERR("function not found for forward '%s' used by %s."
404 " If you are using builtin %s, try using the native one instead.\n",
405 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
),
406 debugstr_w(get_modref(module
)->ldr
.BaseDllName
.Buffer
) );
412 /*************************************************************************
413 * find_ordinal_export
415 * Find an exported function by ordinal.
416 * The exports base must have been subtracted from the ordinal already.
417 * The loader_section must be locked while calling this function.
419 static FARPROC
find_ordinal_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
420 DWORD exp_size
, DWORD ordinal
, LPCWSTR load_path
)
423 const DWORD
*functions
= get_rva( module
, exports
->AddressOfFunctions
);
425 if (ordinal
>= exports
->NumberOfFunctions
)
427 TRACE(" ordinal %d out of range!\n", ordinal
+ exports
->Base
);
430 if (!functions
[ordinal
]) return NULL
;
432 proc
= get_rva( module
, functions
[ordinal
] );
434 /* if the address falls into the export dir, it's a forward */
435 if (((const char *)proc
>= (const char *)exports
) &&
436 ((const char *)proc
< (const char *)exports
+ exp_size
))
437 return find_forwarded_export( module
, (const char *)proc
, load_path
);
441 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
442 proc
= SNOOP_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
446 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
447 proc
= RELAY_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
453 /*************************************************************************
456 * Find an exported function by name.
457 * The loader_section must be locked while calling this function.
459 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
460 DWORD exp_size
, const char *name
, int hint
, LPCWSTR load_path
)
462 const WORD
*ordinals
= get_rva( module
, exports
->AddressOfNameOrdinals
);
463 const DWORD
*names
= get_rva( module
, exports
->AddressOfNames
);
464 int min
= 0, max
= exports
->NumberOfNames
- 1;
466 /* first check the hint */
467 if (hint
>= 0 && hint
<= max
)
469 char *ename
= get_rva( module
, names
[hint
] );
470 if (!strcmp( ename
, name
))
471 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[hint
], load_path
);
474 /* then do a binary search */
477 int res
, pos
= (min
+ max
) / 2;
478 char *ename
= get_rva( module
, names
[pos
] );
479 if (!(res
= strcmp( ename
, name
)))
480 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[pos
], load_path
);
481 if (res
> 0) max
= pos
- 1;
489 /*************************************************************************
492 * Import the dll specified by the given import descriptor.
493 * The loader_section must be locked while calling this function.
495 static WINE_MODREF
*import_dll( HMODULE module
, const IMAGE_IMPORT_DESCRIPTOR
*descr
, LPCWSTR load_path
)
500 const IMAGE_EXPORT_DIRECTORY
*exports
;
502 const IMAGE_THUNK_DATA
*import_list
;
503 IMAGE_THUNK_DATA
*thunk_list
;
505 const char *name
= get_rva( module
, descr
->Name
);
506 DWORD len
= strlen(name
);
508 SIZE_T protect_size
= 0;
511 thunk_list
= get_rva( module
, (DWORD
)descr
->FirstThunk
);
512 if (descr
->u
.OriginalFirstThunk
)
513 import_list
= get_rva( module
, (DWORD
)descr
->u
.OriginalFirstThunk
);
515 import_list
= thunk_list
;
517 while (len
&& name
[len
-1] == ' ') len
--; /* remove trailing spaces */
519 if (len
* sizeof(WCHAR
) < sizeof(buffer
))
521 ascii_to_unicode( buffer
, name
, len
);
523 status
= load_dll( load_path
, buffer
, 0, &wmImp
);
525 else /* need to allocate a larger buffer */
527 WCHAR
*ptr
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
528 if (!ptr
) return NULL
;
529 ascii_to_unicode( ptr
, name
, len
);
531 status
= load_dll( load_path
, ptr
, 0, &wmImp
);
532 RtlFreeHeap( GetProcessHeap(), 0, ptr
);
537 if (status
== STATUS_DLL_NOT_FOUND
)
538 ERR("Library %s (which is needed by %s) not found\n",
539 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
));
541 ERR("Loading library %s (which is needed by %s) failed (error %x).\n",
542 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
), status
);
546 /* unprotect the import address table since it can be located in
547 * readonly section */
548 while (import_list
[protect_size
].u1
.Ordinal
) protect_size
++;
549 protect_base
= thunk_list
;
550 protect_size
*= sizeof(*thunk_list
);
551 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
,
552 &protect_size
, PAGE_WRITECOPY
, &protect_old
);
554 imp_mod
= wmImp
->ldr
.BaseAddress
;
555 exports
= RtlImageDirectoryEntryToData( imp_mod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
);
559 /* set all imported function to deadbeef */
560 while (import_list
->u1
.Ordinal
)
562 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
564 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
565 WARN("No implementation for %s.%d", name
, ordinal
);
566 thunk_list
->u1
.Function
= allocate_stub( name
, IntToPtr(ordinal
) );
570 IMAGE_IMPORT_BY_NAME
*pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
571 WARN("No implementation for %s.%s", name
, pe_name
->Name
);
572 thunk_list
->u1
.Function
= allocate_stub( name
, (const char*)pe_name
->Name
);
574 WARN(" imported from %s, allocating stub %p\n",
575 debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
576 (void *)thunk_list
->u1
.Function
);
583 while (import_list
->u1
.Ordinal
)
585 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
587 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
589 thunk_list
->u1
.Function
= (ULONG_PTR
)find_ordinal_export( imp_mod
, exports
, exp_size
,
590 ordinal
- exports
->Base
, load_path
);
591 if (!thunk_list
->u1
.Function
)
593 thunk_list
->u1
.Function
= allocate_stub( name
, IntToPtr(ordinal
) );
594 WARN("No implementation for %s.%d imported from %s, setting to %p\n",
595 name
, ordinal
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
596 (void *)thunk_list
->u1
.Function
);
598 TRACE_(imports
)("--- Ordinal %s.%d = %p\n", name
, ordinal
, (void *)thunk_list
->u1
.Function
);
600 else /* import by name */
602 IMAGE_IMPORT_BY_NAME
*pe_name
;
603 pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
604 thunk_list
->u1
.Function
= (ULONG_PTR
)find_named_export( imp_mod
, exports
, exp_size
,
605 (const char*)pe_name
->Name
,
606 pe_name
->Hint
, load_path
);
607 if (!thunk_list
->u1
.Function
)
609 thunk_list
->u1
.Function
= allocate_stub( name
, (const char*)pe_name
->Name
);
610 WARN("No implementation for %s.%s imported from %s, setting to %p\n",
611 name
, pe_name
->Name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
612 (void *)thunk_list
->u1
.Function
);
614 TRACE_(imports
)("--- %s %s.%d = %p\n",
615 pe_name
->Name
, name
, pe_name
->Hint
, (void *)thunk_list
->u1
.Function
);
622 /* restore old protection of the import address table */
623 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
, &protect_size
, protect_old
, NULL
);
628 /***********************************************************************
629 * create_module_activation_context
631 static NTSTATUS
create_module_activation_context( LDR_MODULE
*module
)
634 LDR_RESOURCE_INFO info
;
635 const IMAGE_RESOURCE_DATA_ENTRY
*entry
;
637 info
.Type
= RT_MANIFEST
;
638 info
.Name
= ISOLATIONAWARE_MANIFEST_RESOURCE_ID
;
640 if (!(status
= LdrFindResource_U( module
->BaseAddress
, &info
, 3, &entry
)))
643 ctx
.cbSize
= sizeof(ctx
);
645 ctx
.dwFlags
= ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_HMODULE_VALID
;
646 ctx
.hModule
= module
->BaseAddress
;
647 ctx
.lpResourceName
= (LPCWSTR
)ISOLATIONAWARE_MANIFEST_RESOURCE_ID
;
648 status
= RtlCreateActivationContext( &module
->ActivationContext
, &ctx
);
654 /****************************************************************
657 * Fixup all imports of a given module.
658 * The loader_section must be locked while calling this function.
660 static NTSTATUS
fixup_imports( WINE_MODREF
*wm
, LPCWSTR load_path
)
663 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
669 if (!(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
)) return STATUS_SUCCESS
; /* already done */
670 wm
->ldr
.Flags
&= ~LDR_DONT_RESOLVE_REFS
;
672 if (!(imports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
673 IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
674 return STATUS_SUCCESS
;
677 while (imports
[nb_imports
].Name
&& imports
[nb_imports
].FirstThunk
) nb_imports
++;
679 if (!nb_imports
) return STATUS_SUCCESS
; /* no imports */
681 if (!create_module_activation_context( &wm
->ldr
))
682 RtlActivateActivationContext( 0, wm
->ldr
.ActivationContext
, &cookie
);
684 /* Allocate module dependency list */
685 wm
->nDeps
= nb_imports
;
686 wm
->deps
= RtlAllocateHeap( GetProcessHeap(), 0, nb_imports
*sizeof(WINE_MODREF
*) );
688 /* load the imported modules. They are automatically
689 * added to the modref list of the process.
691 prev
= current_modref
;
693 status
= STATUS_SUCCESS
;
694 for (i
= 0; i
< nb_imports
; i
++)
696 if (!(wm
->deps
[i
] = import_dll( wm
->ldr
.BaseAddress
, &imports
[i
], load_path
)))
697 status
= STATUS_DLL_NOT_FOUND
;
699 current_modref
= prev
;
700 if (wm
->ldr
.ActivationContext
) RtlDeactivateActivationContext( 0, cookie
);
705 /*************************************************************************
706 * is_dll_native_subsystem
708 * Check if dll is a proper native driver.
709 * Some dlls (corpol.dll from IE6 for instance) are incorrectly marked as native
710 * while being perfectly normal DLLs. This heuristic should catch such breakages.
712 static BOOL
is_dll_native_subsystem( HMODULE module
, const IMAGE_NT_HEADERS
*nt
, LPCWSTR filename
)
714 static const WCHAR ntdllW
[] = {'n','t','d','l','l','.','d','l','l',0};
715 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
716 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
720 if (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_NATIVE
) return FALSE
;
721 if (nt
->OptionalHeader
.SectionAlignment
< getpagesize()) return TRUE
;
723 if ((imports
= RtlImageDirectoryEntryToData( module
, TRUE
,
724 IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
726 for (i
= 0; imports
[i
].Name
; i
++)
728 const char *name
= get_rva( module
, imports
[i
].Name
);
729 DWORD len
= strlen(name
);
730 if (len
* sizeof(WCHAR
) >= sizeof(buffer
)) continue;
731 ascii_to_unicode( buffer
, name
, len
+ 1 );
732 if (!strcmpiW( buffer
, ntdllW
) || !strcmpiW( buffer
, kernel32W
))
734 TRACE( "%s imports %s, assuming not native\n", debugstr_w(filename
), debugstr_w(buffer
) );
743 /*************************************************************************
746 * Allocate a WINE_MODREF structure and add it to the process list
747 * The loader_section must be locked while calling this function.
749 static WINE_MODREF
*alloc_module( HMODULE hModule
, LPCWSTR filename
)
753 const IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader(hModule
);
754 PLIST_ENTRY entry
, mark
;
756 if (!(wm
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm
) ))) return NULL
;
761 wm
->ldr
.BaseAddress
= hModule
;
762 wm
->ldr
.EntryPoint
= NULL
;
763 wm
->ldr
.SizeOfImage
= nt
->OptionalHeader
.SizeOfImage
;
764 wm
->ldr
.Flags
= LDR_DONT_RESOLVE_REFS
;
765 wm
->ldr
.LoadCount
= 1;
766 wm
->ldr
.TlsIndex
= -1;
767 wm
->ldr
.SectionHandle
= NULL
;
768 wm
->ldr
.CheckSum
= 0;
769 wm
->ldr
.TimeDateStamp
= 0;
770 wm
->ldr
.ActivationContext
= 0;
772 RtlCreateUnicodeString( &wm
->ldr
.FullDllName
, filename
);
773 if ((p
= strrchrW( wm
->ldr
.FullDllName
.Buffer
, '\\' ))) p
++;
774 else p
= wm
->ldr
.FullDllName
.Buffer
;
775 RtlInitUnicodeString( &wm
->ldr
.BaseDllName
, p
);
777 if ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) && !is_dll_native_subsystem( hModule
, nt
, p
))
779 wm
->ldr
.Flags
|= LDR_IMAGE_IS_DLL
;
780 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
781 wm
->ldr
.EntryPoint
= (char *)hModule
+ nt
->OptionalHeader
.AddressOfEntryPoint
;
784 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
,
785 &wm
->ldr
.InLoadOrderModuleList
);
787 /* insert module in MemoryList, sorted in increasing base addresses */
788 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
789 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
791 if (CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
)->BaseAddress
> wm
->ldr
.BaseAddress
)
794 entry
->Blink
->Flink
= &wm
->ldr
.InMemoryOrderModuleList
;
795 wm
->ldr
.InMemoryOrderModuleList
.Blink
= entry
->Blink
;
796 wm
->ldr
.InMemoryOrderModuleList
.Flink
= entry
;
797 entry
->Blink
= &wm
->ldr
.InMemoryOrderModuleList
;
799 /* wait until init is called for inserting into this list */
800 wm
->ldr
.InInitializationOrderModuleList
.Flink
= NULL
;
801 wm
->ldr
.InInitializationOrderModuleList
.Blink
= NULL
;
803 if (!(nt
->OptionalHeader
.DllCharacteristics
& IMAGE_DLLCHARACTERISTICS_NX_COMPAT
))
805 WARN( "disabling no-exec because of %s\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
) );
806 VIRTUAL_SetForceExec( TRUE
);
812 /*************************************************************************
815 * Allocate the process-wide structure for module TLS storage.
817 static NTSTATUS
alloc_process_tls(void)
819 PLIST_ENTRY mark
, entry
;
821 const IMAGE_TLS_DIRECTORY
*dir
;
824 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
825 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
827 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
828 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
,
829 IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
831 size
= (dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
) + dir
->SizeOfZeroFill
;
833 tls_total_size
+= size
;
836 if (!tls_module_count
) return STATUS_SUCCESS
;
838 TRACE( "count %u size %u\n", tls_module_count
, tls_total_size
);
840 tls_dirs
= RtlAllocateHeap( GetProcessHeap(), 0, tls_module_count
* sizeof(*tls_dirs
) );
841 if (!tls_dirs
) return STATUS_NO_MEMORY
;
843 for (i
= 0, entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
845 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
846 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
,
847 IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
850 *(DWORD
*)dir
->AddressOfIndex
= i
;
852 mod
->LoadCount
= -1; /* can't unload it */
855 return STATUS_SUCCESS
;
859 /*************************************************************************
862 * Allocate the per-thread structure for module TLS storage.
864 static NTSTATUS
alloc_thread_tls(void)
870 if (!tls_module_count
) return STATUS_SUCCESS
;
872 if (!(pointers
= RtlAllocateHeap( GetProcessHeap(), 0,
873 tls_module_count
* sizeof(*pointers
) )))
874 return STATUS_NO_MEMORY
;
876 if (!(data
= RtlAllocateHeap( GetProcessHeap(), 0, tls_total_size
)))
878 RtlFreeHeap( GetProcessHeap(), 0, pointers
);
879 return STATUS_NO_MEMORY
;
882 for (i
= 0; i
< tls_module_count
; i
++)
884 const IMAGE_TLS_DIRECTORY
*dir
= tls_dirs
[i
];
885 ULONG size
= dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
;
887 TRACE( "thread %04x idx %d: %d/%d bytes from %p to %p\n",
888 GetCurrentThreadId(), i
, size
, dir
->SizeOfZeroFill
,
889 (void *)dir
->StartAddressOfRawData
, data
);
892 memcpy( data
, (void *)dir
->StartAddressOfRawData
, size
);
894 memset( data
, 0, dir
->SizeOfZeroFill
);
895 data
+= dir
->SizeOfZeroFill
;
897 NtCurrentTeb()->ThreadLocalStoragePointer
= pointers
;
898 return STATUS_SUCCESS
;
902 /*************************************************************************
905 static void call_tls_callbacks( HMODULE module
, UINT reason
)
907 const IMAGE_TLS_DIRECTORY
*dir
;
908 const PIMAGE_TLS_CALLBACK
*callback
;
911 dir
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_TLS
, &dirsize
);
912 if (!dir
|| !dir
->AddressOfCallBacks
) return;
914 for (callback
= (const PIMAGE_TLS_CALLBACK
*)dir
->AddressOfCallBacks
; *callback
; callback
++)
917 DPRINTF("%04x:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
918 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
921 (*callback
)( module
, reason
, NULL
);
926 DPRINTF("%04x:exception in TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
927 GetCurrentThreadId(), callback
, module
, reason_names
[reason
] );
932 DPRINTF("%04x:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
933 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
938 /*************************************************************************
941 static NTSTATUS
MODULE_InitDLL( WINE_MODREF
*wm
, UINT reason
, LPVOID lpReserved
)
944 NTSTATUS status
= STATUS_SUCCESS
;
945 DLLENTRYPROC entry
= wm
->ldr
.EntryPoint
;
946 void *module
= wm
->ldr
.BaseAddress
;
949 /* Skip calls for modules loaded with special load flags */
951 if (wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
) return STATUS_SUCCESS
;
952 if (wm
->ldr
.TlsIndex
!= -1) call_tls_callbacks( wm
->ldr
.BaseAddress
, reason
);
953 if (!entry
) return STATUS_SUCCESS
;
957 size_t len
= min( wm
->ldr
.BaseDllName
.Length
, sizeof(mod_name
)-sizeof(WCHAR
) );
958 memcpy( mod_name
, wm
->ldr
.BaseDllName
.Buffer
, len
);
959 mod_name
[len
/ sizeof(WCHAR
)] = 0;
960 DPRINTF("%04x:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
961 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
962 reason_names
[reason
], lpReserved
);
964 else TRACE("(%p %s,%s,%p) - CALL\n", module
, debugstr_w(wm
->ldr
.BaseDllName
.Buffer
),
965 reason_names
[reason
], lpReserved
);
969 retv
= call_dll_entry_point( entry
, module
, reason
, lpReserved
);
971 status
= STATUS_DLL_INIT_FAILED
;
976 DPRINTF("%04x:exception in PE entry point (proc=%p,module=%p,reason=%s,res=%p)\n",
977 GetCurrentThreadId(), entry
, module
, reason_names
[reason
], lpReserved
);
978 status
= GetExceptionCode();
982 /* The state of the module list may have changed due to the call
983 to the dll. We cannot assume that this module has not been
986 DPRINTF("%04x:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
987 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
988 reason_names
[reason
], lpReserved
, retv
);
989 else TRACE("(%p,%s,%p) - RETURN %d\n", module
, reason_names
[reason
], lpReserved
, retv
);
995 /*************************************************************************
998 * Send the process attach notification to all DLLs the given module
999 * depends on (recursively). This is somewhat complicated due to the fact that
1001 * - we have to respect the module dependencies, i.e. modules implicitly
1002 * referenced by another module have to be initialized before the module
1003 * itself can be initialized
1005 * - the initialization routine of a DLL can itself call LoadLibrary,
1006 * thereby introducing a whole new set of dependencies (even involving
1007 * the 'old' modules) at any time during the whole process
1009 * (Note that this routine can be recursively entered not only directly
1010 * from itself, but also via LoadLibrary from one of the called initialization
1013 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
1014 * the process *detach* notifications to be sent in the correct order.
1015 * This must not only take into account module dependencies, but also
1016 * 'hidden' dependencies created by modules calling LoadLibrary in their
1017 * attach notification routine.
1019 * The strategy is rather simple: we move a WINE_MODREF to the head of the
1020 * list after the attach notification has returned. This implies that the
1021 * detach notifications are called in the reverse of the sequence the attach
1022 * notifications *returned*.
1024 * The loader_section must be locked while calling this function.
1026 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
)
1028 NTSTATUS status
= STATUS_SUCCESS
;
1032 if (process_detaching
) return status
;
1034 /* prevent infinite recursion in case of cyclical dependencies */
1035 if ( ( wm
->ldr
.Flags
& LDR_LOAD_IN_PROGRESS
)
1036 || ( wm
->ldr
.Flags
& LDR_PROCESS_ATTACHED
) )
1039 TRACE("(%s,%p) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
1041 /* Tag current MODREF to prevent recursive loop */
1042 wm
->ldr
.Flags
|= LDR_LOAD_IN_PROGRESS
;
1043 if (lpReserved
) wm
->ldr
.LoadCount
= -1; /* pin it if imported by the main exe */
1044 if (wm
->ldr
.ActivationContext
) RtlActivateActivationContext( 0, wm
->ldr
.ActivationContext
, &cookie
);
1046 /* Recursively attach all DLLs this one depends on */
1047 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1049 if (!wm
->deps
[i
]) continue;
1050 if ((status
= process_attach( wm
->deps
[i
], lpReserved
)) != STATUS_SUCCESS
) break;
1053 /* Call DLL entry point */
1054 if (status
== STATUS_SUCCESS
)
1056 WINE_MODREF
*prev
= current_modref
;
1057 current_modref
= wm
;
1058 status
= MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
1059 if (status
== STATUS_SUCCESS
)
1060 wm
->ldr
.Flags
|= LDR_PROCESS_ATTACHED
;
1063 /* point to the name so LdrInitializeThunk can print it */
1064 last_failed_modref
= wm
;
1065 WARN("Initialization of %s failed\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
1067 current_modref
= prev
;
1070 if (!wm
->ldr
.InInitializationOrderModuleList
.Flink
)
1071 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
,
1072 &wm
->ldr
.InInitializationOrderModuleList
);
1074 if (wm
->ldr
.ActivationContext
) RtlDeactivateActivationContext( 0, cookie
);
1075 /* Remove recursion flag */
1076 wm
->ldr
.Flags
&= ~LDR_LOAD_IN_PROGRESS
;
1078 TRACE("(%s,%p) - END\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
1083 /**********************************************************************
1084 * attach_implicitly_loaded_dlls
1086 * Attach to the (builtin) dlls that have been implicitly loaded because
1087 * of a dependency at the Unix level, but not imported at the Win32 level.
1089 static void attach_implicitly_loaded_dlls( LPVOID reserved
)
1093 PLIST_ENTRY mark
, entry
;
1095 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1096 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1098 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1100 if (mod
->Flags
& (LDR_LOAD_IN_PROGRESS
| LDR_PROCESS_ATTACHED
)) continue;
1101 TRACE( "found implicitly loaded %s, attaching to it\n",
1102 debugstr_w(mod
->BaseDllName
.Buffer
));
1103 process_attach( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
), reserved
);
1104 break; /* restart the search from the start */
1106 if (entry
== mark
) break; /* nothing found */
1111 /*************************************************************************
1114 * Send DLL process detach notifications. See the comment about calling
1115 * sequence at process_attach. Unless the bForceDetach flag
1116 * is set, only DLLs with zero refcount are notified.
1118 static void process_detach( BOOL bForceDetach
, LPVOID lpReserved
)
1120 PLIST_ENTRY mark
, entry
;
1123 RtlEnterCriticalSection( &loader_section
);
1124 if (bForceDetach
) process_detaching
= 1;
1125 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1128 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
1130 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1131 InInitializationOrderModuleList
);
1132 /* Check whether to detach this DLL */
1133 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1135 if ( mod
->LoadCount
&& !bForceDetach
)
1138 /* Call detach notification */
1139 mod
->Flags
&= ~LDR_PROCESS_ATTACHED
;
1140 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1141 DLL_PROCESS_DETACH
, lpReserved
);
1143 /* Restart at head of WINE_MODREF list, as entries might have
1144 been added and/or removed while performing the call ... */
1147 } while (entry
!= mark
);
1149 RtlLeaveCriticalSection( &loader_section
);
1152 /*************************************************************************
1153 * MODULE_DllThreadAttach
1155 * Send DLL thread attach notifications. These are sent in the
1156 * reverse sequence of process detach notification.
1159 NTSTATUS
MODULE_DllThreadAttach( LPVOID lpReserved
)
1161 PLIST_ENTRY mark
, entry
;
1165 /* don't do any attach calls if process is exiting */
1166 if (process_detaching
) return STATUS_SUCCESS
;
1167 /* FIXME: there is still a race here */
1169 RtlEnterCriticalSection( &loader_section
);
1171 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto done
;
1173 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1174 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1176 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1177 InInitializationOrderModuleList
);
1178 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1180 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
1183 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1184 DLL_THREAD_ATTACH
, lpReserved
);
1188 RtlLeaveCriticalSection( &loader_section
);
1192 /******************************************************************
1193 * LdrDisableThreadCalloutsForDll (NTDLL.@)
1196 NTSTATUS WINAPI
LdrDisableThreadCalloutsForDll(HMODULE hModule
)
1199 NTSTATUS ret
= STATUS_SUCCESS
;
1201 RtlEnterCriticalSection( &loader_section
);
1203 wm
= get_modref( hModule
);
1204 if (!wm
|| wm
->ldr
.TlsIndex
!= -1)
1205 ret
= STATUS_DLL_NOT_FOUND
;
1207 wm
->ldr
.Flags
|= LDR_NO_DLL_CALLS
;
1209 RtlLeaveCriticalSection( &loader_section
);
1214 /******************************************************************
1215 * LdrFindEntryForAddress (NTDLL.@)
1217 * The loader_section must be locked while calling this function
1219 NTSTATUS WINAPI
LdrFindEntryForAddress(const void* addr
, PLDR_MODULE
* pmod
)
1221 PLIST_ENTRY mark
, entry
;
1224 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
1225 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1227 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
1228 if ((const void *)mod
->BaseAddress
<= addr
&&
1229 (const char *)addr
< (char*)mod
->BaseAddress
+ mod
->SizeOfImage
)
1232 return STATUS_SUCCESS
;
1234 if ((const void *)mod
->BaseAddress
> addr
) break;
1236 return STATUS_NO_MORE_ENTRIES
;
1239 /******************************************************************
1240 * LdrLockLoaderLock (NTDLL.@)
1242 * Note: flags are not implemented.
1243 * Flag 0x01 is used to raise exceptions on errors.
1244 * Flag 0x02 is used to avoid waiting on the section (does RtlTryEnterCriticalSection instead).
1246 NTSTATUS WINAPI
LdrLockLoaderLock( ULONG flags
, ULONG
*result
, ULONG
*magic
)
1248 if (flags
) FIXME( "flags %x not supported\n", flags
);
1250 if (result
) *result
= 1;
1251 if (!magic
) return STATUS_INVALID_PARAMETER_3
;
1252 RtlEnterCriticalSection( &loader_section
);
1253 *magic
= GetCurrentThreadId();
1254 return STATUS_SUCCESS
;
1258 /******************************************************************
1259 * LdrUnlockLoaderUnlock (NTDLL.@)
1261 NTSTATUS WINAPI
LdrUnlockLoaderLock( ULONG flags
, ULONG magic
)
1265 if (magic
!= GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2
;
1266 RtlLeaveCriticalSection( &loader_section
);
1268 return STATUS_SUCCESS
;
1272 /******************************************************************
1273 * LdrGetProcedureAddress (NTDLL.@)
1275 NTSTATUS WINAPI
LdrGetProcedureAddress(HMODULE module
, const ANSI_STRING
*name
,
1276 ULONG ord
, PVOID
*address
)
1278 IMAGE_EXPORT_DIRECTORY
*exports
;
1280 NTSTATUS ret
= STATUS_PROCEDURE_NOT_FOUND
;
1282 RtlEnterCriticalSection( &loader_section
);
1284 /* check if the module itself is invalid to return the proper error */
1285 if (!get_modref( module
)) ret
= STATUS_DLL_NOT_FOUND
;
1286 else if ((exports
= RtlImageDirectoryEntryToData( module
, TRUE
,
1287 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
1289 LPCWSTR load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1290 void *proc
= name
? find_named_export( module
, exports
, exp_size
, name
->Buffer
, -1, load_path
)
1291 : find_ordinal_export( module
, exports
, exp_size
, ord
- exports
->Base
, load_path
);
1295 ret
= STATUS_SUCCESS
;
1299 RtlLeaveCriticalSection( &loader_section
);
1304 /***********************************************************************
1307 * Check if a loaded native dll is a Wine fake dll.
1309 static BOOL
is_fake_dll( HANDLE handle
)
1311 static const char fakedll_signature
[] = "Wine placeholder DLL";
1312 char buffer
[sizeof(IMAGE_DOS_HEADER
) + sizeof(fakedll_signature
)];
1313 const IMAGE_DOS_HEADER
*dos
= (const IMAGE_DOS_HEADER
*)buffer
;
1315 LARGE_INTEGER offset
;
1317 offset
.QuadPart
= 0;
1318 if (NtReadFile( handle
, 0, NULL
, 0, &io
, buffer
, sizeof(buffer
), &offset
, NULL
)) return FALSE
;
1319 if (io
.Information
< sizeof(buffer
)) return FALSE
;
1320 if (dos
->e_magic
!= IMAGE_DOS_SIGNATURE
) return FALSE
;
1321 if (dos
->e_lfanew
>= sizeof(*dos
) + sizeof(fakedll_signature
) &&
1322 !memcmp( dos
+ 1, fakedll_signature
, sizeof(fakedll_signature
) )) return TRUE
;
1327 /***********************************************************************
1328 * get_builtin_fullname
1330 * Build the full pathname for a builtin dll.
1332 static WCHAR
*get_builtin_fullname( const WCHAR
*path
, const char *filename
)
1334 static const WCHAR soW
[] = {'.','s','o',0};
1335 WCHAR
*p
, *fullname
;
1336 size_t i
, len
= strlen(filename
);
1338 /* check if path can correspond to the dll we have */
1339 if (path
&& (p
= strrchrW( path
, '\\' )))
1342 for (i
= 0; i
< len
; i
++)
1343 if (tolowerW(p
[i
]) != tolowerW( (WCHAR
)filename
[i
]) ) break;
1344 if (i
== len
&& (!p
[len
] || !strcmpiW( p
+ len
, soW
)))
1346 /* the filename matches, use path as the full path */
1348 if ((fullname
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1350 memcpy( fullname
, path
, len
* sizeof(WCHAR
) );
1357 if ((fullname
= RtlAllocateHeap( GetProcessHeap(), 0,
1358 system_dir
.MaximumLength
+ (len
+ 1) * sizeof(WCHAR
) )))
1360 memcpy( fullname
, system_dir
.Buffer
, system_dir
.Length
);
1361 p
= fullname
+ system_dir
.Length
/ sizeof(WCHAR
);
1362 if (p
> fullname
&& p
[-1] != '\\') *p
++ = '\\';
1363 ascii_to_unicode( p
, filename
, len
+ 1 );
1369 /***********************************************************************
1370 * load_builtin_callback
1372 * Load a library in memory; callback function for wine_dll_register
1374 static void load_builtin_callback( void *module
, const char *filename
)
1376 static const WCHAR emptyW
[1];
1377 IMAGE_NT_HEADERS
*nt
;
1380 const WCHAR
*load_path
;
1384 ERR("could not map image for %s\n", filename
? filename
: "main exe" );
1387 if (!(nt
= RtlImageNtHeader( module
)))
1389 ERR( "bad module for %s\n", filename
? filename
: "main exe" );
1390 builtin_load_info
->status
= STATUS_INVALID_IMAGE_FORMAT
;
1393 virtual_create_system_view( module
, nt
->OptionalHeader
.SizeOfImage
,
1394 VPROT_SYSTEM
| VPROT_IMAGE
| VPROT_COMMITTED
|
1395 VPROT_READ
| VPROT_WRITECOPY
| VPROT_EXEC
);
1397 /* create the MODREF */
1399 if (!(fullname
= get_builtin_fullname( builtin_load_info
->filename
, filename
)))
1401 ERR( "can't load %s\n", filename
);
1402 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1406 wm
= alloc_module( module
, fullname
);
1407 RtlFreeHeap( GetProcessHeap(), 0, fullname
);
1410 ERR( "can't load %s\n", filename
);
1411 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1414 wm
->ldr
.Flags
|= LDR_WINE_INTERNAL
;
1416 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
1417 !NtCurrentTeb()->Peb
->ImageBaseAddress
) /* if we already have an executable, ignore this one */
1419 NtCurrentTeb()->Peb
->ImageBaseAddress
= module
;
1425 load_path
= builtin_load_info
->load_path
;
1426 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1427 if (!load_path
) load_path
= emptyW
;
1428 if (fixup_imports( wm
, load_path
) != STATUS_SUCCESS
)
1430 /* the module has only be inserted in the load & memory order lists */
1431 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1432 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1433 /* FIXME: free the modref */
1434 builtin_load_info
->status
= STATUS_DLL_NOT_FOUND
;
1439 builtin_load_info
->wm
= wm
;
1440 TRACE( "loaded %s %p %p\n", filename
, wm
, module
);
1442 /* send the DLL load event */
1444 SERVER_START_REQ( load_dll
)
1447 req
->base
= wine_server_client_ptr( module
);
1448 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1449 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1450 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1451 req
->name
= wine_server_client_ptr( &wm
->ldr
.FullDllName
.Buffer
);
1452 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1453 wine_server_call( req
);
1457 /* setup relay debugging entry points */
1458 if (TRACE_ON(relay
)) RELAY_SetupDLL( module
);
1462 /******************************************************************************
1463 * load_native_dll (internal)
1465 static NTSTATUS
load_native_dll( LPCWSTR load_path
, LPCWSTR name
, HANDLE file
,
1466 DWORD flags
, WINE_MODREF
** pwm
)
1470 OBJECT_ATTRIBUTES attr
;
1472 IMAGE_NT_HEADERS
*nt
;
1477 TRACE("Trying native dll %s\n", debugstr_w(name
));
1479 attr
.Length
= sizeof(attr
);
1480 attr
.RootDirectory
= 0;
1481 attr
.ObjectName
= NULL
;
1482 attr
.Attributes
= 0;
1483 attr
.SecurityDescriptor
= NULL
;
1484 attr
.SecurityQualityOfService
= NULL
;
1487 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1488 &attr
, &size
, PAGE_READONLY
, SEC_IMAGE
, file
);
1489 if (status
!= STATUS_SUCCESS
) return status
;
1492 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(),
1493 &module
, 0, 0, &size
, &len
, ViewShare
, 0, PAGE_READONLY
);
1495 if (status
!= STATUS_SUCCESS
) return status
;
1497 /* create the MODREF */
1499 if (!(wm
= alloc_module( module
, name
))) return STATUS_NO_MEMORY
;
1503 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1505 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
)
1507 /* the module has only be inserted in the load & memory order lists */
1508 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1509 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1511 /* FIXME: there are several more dangling references
1512 * left. Including dlls loaded by this dll before the
1513 * failed one. Unrolling is rather difficult with the
1514 * current structure and we can leave them lying
1515 * around with no problems, so we don't care.
1516 * As these might reference our wm, we don't free it.
1522 /* send DLL load event */
1524 nt
= RtlImageNtHeader( module
);
1526 SERVER_START_REQ( load_dll
)
1528 req
->handle
= wine_server_obj_handle( file
);
1529 req
->base
= wine_server_client_ptr( module
);
1530 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1531 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1532 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1533 req
->name
= wine_server_client_ptr( &wm
->ldr
.FullDllName
.Buffer
);
1534 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1535 wine_server_call( req
);
1539 if ((wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
) && TRACE_ON(snoop
)) SNOOP_SetupDLL( module
);
1541 TRACE_(loaddll
)( "Loaded %s at %p: native\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
), module
);
1543 wm
->ldr
.LoadCount
= 1;
1545 return STATUS_SUCCESS
;
1549 /***********************************************************************
1552 static NTSTATUS
load_builtin_dll( LPCWSTR load_path
, LPCWSTR path
, HANDLE file
,
1553 DWORD flags
, WINE_MODREF
** pwm
)
1555 char error
[256], dllname
[MAX_PATH
];
1556 const WCHAR
*name
, *p
;
1558 void *handle
= NULL
;
1559 struct builtin_load_info info
, *prev_info
;
1561 /* Fix the name in case we have a full path and extension */
1563 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
1564 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
1566 /* load_library will modify info.status. Note also that load_library can be
1567 * called several times, if the .so file we're loading has dependencies.
1568 * info.status will gather all the errors we may get while loading all these
1571 info
.load_path
= load_path
;
1572 info
.filename
= NULL
;
1573 info
.status
= STATUS_SUCCESS
;
1576 if (file
) /* we have a real file, try to load it */
1578 UNICODE_STRING nt_name
;
1579 ANSI_STRING unix_name
;
1581 TRACE("Trying built-in %s\n", debugstr_w(path
));
1583 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1584 return STATUS_DLL_NOT_FOUND
;
1586 if (wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
))
1588 RtlFreeUnicodeString( &nt_name
);
1589 return STATUS_DLL_NOT_FOUND
;
1591 prev_info
= builtin_load_info
;
1592 info
.filename
= nt_name
.Buffer
+ 4; /* skip \??\ */
1593 builtin_load_info
= &info
;
1594 handle
= wine_dlopen( unix_name
.Buffer
, RTLD_NOW
, error
, sizeof(error
) );
1595 builtin_load_info
= prev_info
;
1596 RtlFreeUnicodeString( &nt_name
);
1597 RtlFreeHeap( GetProcessHeap(), 0, unix_name
.Buffer
);
1600 WARN( "failed to load .so lib for builtin %s: %s\n", debugstr_w(path
), error
);
1601 return STATUS_INVALID_IMAGE_FORMAT
;
1608 TRACE("Trying built-in %s\n", debugstr_w(name
));
1610 /* we don't want to depend on the current codepage here */
1611 len
= strlenW( name
) + 1;
1612 if (len
>= sizeof(dllname
)) return STATUS_NAME_TOO_LONG
;
1613 for (i
= 0; i
< len
; i
++)
1615 if (name
[i
] > 127) return STATUS_DLL_NOT_FOUND
;
1616 dllname
[i
] = (char)name
[i
];
1617 if (dllname
[i
] >= 'A' && dllname
[i
] <= 'Z') dllname
[i
] += 'a' - 'A';
1620 prev_info
= builtin_load_info
;
1621 builtin_load_info
= &info
;
1622 handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
);
1623 builtin_load_info
= prev_info
;
1628 /* The file does not exist -> WARN() */
1629 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1630 return STATUS_DLL_NOT_FOUND
;
1632 /* ERR() for all other errors (missing functions, ...) */
1633 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1634 return STATUS_PROCEDURE_NOT_FOUND
;
1638 if (info
.status
!= STATUS_SUCCESS
)
1640 wine_dll_unload( handle
);
1646 PLIST_ENTRY mark
, entry
;
1648 /* The constructor wasn't called, this means the .so is already
1649 * loaded under a different name. Try to find the wm for it. */
1651 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1652 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1654 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1655 if (mod
->Flags
& LDR_WINE_INTERNAL
&& mod
->SectionHandle
== handle
)
1657 info
.wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
1658 TRACE( "Found %s at %p for builtin %s\n",
1659 debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
, debugstr_w(path
) );
1663 wine_dll_unload( handle
); /* release the libdl refcount */
1664 if (!info
.wm
) return STATUS_INVALID_IMAGE_FORMAT
;
1665 if (info
.wm
->ldr
.LoadCount
!= -1) info
.wm
->ldr
.LoadCount
++;
1669 TRACE_(loaddll
)( "Loaded %s at %p: builtin\n", debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
);
1670 info
.wm
->ldr
.LoadCount
= 1;
1671 info
.wm
->ldr
.SectionHandle
= handle
;
1675 return STATUS_SUCCESS
;
1679 /***********************************************************************
1682 * Find the full path (if any) of the dll from the activation context.
1684 static NTSTATUS
find_actctx_dll( LPCWSTR libname
, LPWSTR
*fullname
)
1686 static const WCHAR winsxsW
[] = {'\\','w','i','n','s','x','s','\\'};
1687 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
1689 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
1690 ACTCTX_SECTION_KEYED_DATA data
;
1691 UNICODE_STRING nameW
;
1693 SIZE_T needed
, size
= 1024;
1696 RtlInitUnicodeString( &nameW
, libname
);
1697 data
.cbSize
= sizeof(data
);
1698 status
= RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
1699 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
1701 if (status
!= STATUS_SUCCESS
) return status
;
1705 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1707 status
= STATUS_NO_MEMORY
;
1710 status
= RtlQueryInformationActivationContext( 0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
1711 AssemblyDetailedInformationInActivationContext
,
1712 info
, size
, &needed
);
1713 if (status
== STATUS_SUCCESS
) break;
1714 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
1715 RtlFreeHeap( GetProcessHeap(), 0, info
);
1717 /* restart with larger buffer */
1720 if ((p
= strrchrW( info
->lpAssemblyManifestPath
, '\\' )))
1722 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1725 if (strncmpiW( p
, info
->lpAssemblyDirectoryName
, dirlen
) || strcmpiW( p
+ dirlen
, dotManifestW
))
1727 /* manifest name does not match directory name, so it's not a global
1728 * windows/winsxs manifest; use the manifest directory name instead */
1729 dirlen
= p
- info
->lpAssemblyManifestPath
;
1730 needed
= (dirlen
+ 1) * sizeof(WCHAR
) + nameW
.Length
;
1731 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1733 status
= STATUS_NO_MEMORY
;
1736 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
1738 strcpyW( p
, libname
);
1743 needed
= (windows_dir
.Length
+ sizeof(winsxsW
) + info
->ulAssemblyDirectoryNameLength
+
1744 nameW
.Length
+ 2*sizeof(WCHAR
));
1746 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1748 status
= STATUS_NO_MEMORY
;
1751 memcpy( p
, windows_dir
.Buffer
, windows_dir
.Length
);
1752 p
+= windows_dir
.Length
/ sizeof(WCHAR
);
1753 memcpy( p
, winsxsW
, sizeof(winsxsW
) );
1754 p
+= sizeof(winsxsW
) / sizeof(WCHAR
);
1755 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
1756 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1758 strcpyW( p
, libname
);
1760 RtlFreeHeap( GetProcessHeap(), 0, info
);
1761 RtlReleaseActivationContext( data
.hActCtx
);
1766 /***********************************************************************
1769 * Find the file (or already loaded module) for a given dll name.
1771 static NTSTATUS
find_dll_file( const WCHAR
*load_path
, const WCHAR
*libname
,
1772 WCHAR
*filename
, ULONG
*size
, WINE_MODREF
**pwm
, HANDLE
*handle
)
1774 OBJECT_ATTRIBUTES attr
;
1776 UNICODE_STRING nt_name
;
1777 WCHAR
*file_part
, *ext
, *dllname
;
1780 /* first append .dll if needed */
1783 if (!(ext
= strrchrW( libname
, '.')) || strchrW( ext
, '/' ) || strchrW( ext
, '\\'))
1785 if (!(dllname
= RtlAllocateHeap( GetProcessHeap(), 0,
1786 (strlenW(libname
) * sizeof(WCHAR
)) + sizeof(dllW
) )))
1787 return STATUS_NO_MEMORY
;
1788 strcpyW( dllname
, libname
);
1789 strcatW( dllname
, dllW
);
1793 nt_name
.Buffer
= NULL
;
1795 if (!contains_path( libname
))
1798 WCHAR
*fullname
= NULL
;
1800 if ((*pwm
= find_basename_module( libname
)) != NULL
) goto found
;
1802 status
= find_actctx_dll( libname
, &fullname
);
1803 if (status
== STATUS_SUCCESS
)
1805 TRACE ("found %s for %s\n", debugstr_w(fullname
), debugstr_w(libname
) );
1806 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1807 libname
= dllname
= fullname
;
1809 else if (status
!= STATUS_SXS_KEY_NOT_FOUND
)
1811 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1816 if (RtlDetermineDosPathNameType_U( libname
) == RELATIVE_PATH
)
1818 /* we need to search for it */
1819 len
= RtlDosSearchPath_U( load_path
, libname
, NULL
, *size
, filename
, &file_part
);
1822 if (len
>= *size
) goto overflow
;
1823 if ((*pwm
= find_fullname_module( filename
)) || !handle
) goto found
;
1825 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, NULL
, NULL
))
1827 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1828 return STATUS_NO_MEMORY
;
1830 attr
.Length
= sizeof(attr
);
1831 attr
.RootDirectory
= 0;
1832 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1833 attr
.ObjectName
= &nt_name
;
1834 attr
.SecurityDescriptor
= NULL
;
1835 attr
.SecurityQualityOfService
= NULL
;
1836 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1842 if (!contains_path( libname
))
1844 /* if libname doesn't contain a path at all, we simply return the name as is,
1845 * to be loaded as builtin */
1846 len
= strlenW(libname
) * sizeof(WCHAR
);
1847 if (len
>= *size
) goto overflow
;
1848 strcpyW( filename
, libname
);
1853 /* absolute path name, or relative path name but not found above */
1855 if (!RtlDosPathNameToNtPathName_U( libname
, &nt_name
, &file_part
, NULL
))
1857 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1858 return STATUS_NO_MEMORY
;
1860 len
= nt_name
.Length
- 4*sizeof(WCHAR
); /* for \??\ prefix */
1861 if (len
>= *size
) goto overflow
;
1862 memcpy( filename
, nt_name
.Buffer
+ 4, len
+ sizeof(WCHAR
) );
1863 if (!(*pwm
= find_fullname_module( filename
)) && handle
)
1865 attr
.Length
= sizeof(attr
);
1866 attr
.RootDirectory
= 0;
1867 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1868 attr
.ObjectName
= &nt_name
;
1869 attr
.SecurityDescriptor
= NULL
;
1870 attr
.SecurityQualityOfService
= NULL
;
1871 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1874 RtlFreeUnicodeString( &nt_name
);
1875 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1876 return STATUS_SUCCESS
;
1879 RtlFreeUnicodeString( &nt_name
);
1880 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1881 *size
= len
+ sizeof(WCHAR
);
1882 return STATUS_BUFFER_TOO_SMALL
;
1886 /***********************************************************************
1887 * load_dll (internal)
1889 * Load a PE style module according to the load order.
1890 * The loader_section must be locked while calling this function.
1892 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
)
1894 enum loadorder loadorder
;
1898 WINE_MODREF
*main_exe
;
1902 TRACE( "looking for %s in %s\n", debugstr_w(libname
), debugstr_w(load_path
) );
1906 size
= sizeof(buffer
);
1909 nts
= find_dll_file( load_path
, libname
, filename
, &size
, pwm
, &handle
);
1910 if (nts
== STATUS_SUCCESS
) break;
1911 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1912 if (nts
!= STATUS_BUFFER_TOO_SMALL
) return nts
;
1913 /* grow the buffer and retry */
1914 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
))) return STATUS_NO_MEMORY
;
1917 if (*pwm
) /* found already loaded module */
1919 if ((*pwm
)->ldr
.LoadCount
!= -1) (*pwm
)->ldr
.LoadCount
++;
1921 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
)) fixup_imports( *pwm
, load_path
);
1923 TRACE("Found %s for %s at %p, count=%d\n",
1924 debugstr_w((*pwm
)->ldr
.FullDllName
.Buffer
), debugstr_w(libname
),
1925 (*pwm
)->ldr
.BaseAddress
, (*pwm
)->ldr
.LoadCount
);
1926 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1927 return STATUS_SUCCESS
;
1930 main_exe
= get_modref( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1931 loadorder
= get_load_order( main_exe
? main_exe
->ldr
.BaseDllName
.Buffer
: NULL
, filename
);
1933 if (handle
&& is_fake_dll( handle
))
1935 TRACE( "%s is a fake Wine dll\n", debugstr_w(filename
) );
1943 nts
= STATUS_NO_MEMORY
;
1946 nts
= STATUS_DLL_NOT_FOUND
;
1949 case LO_NATIVE_BUILTIN
:
1950 if (!handle
) nts
= STATUS_DLL_NOT_FOUND
;
1953 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
1954 if (nts
== STATUS_INVALID_FILE_FOR_SECTION
)
1955 /* not in PE format, maybe it's a builtin */
1956 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
1958 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
== LO_NATIVE_BUILTIN
)
1959 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
1962 case LO_BUILTIN_NATIVE
:
1963 case LO_DEFAULT
: /* default is builtin,native */
1964 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
1965 if (!handle
) break; /* nothing else we can try */
1966 /* file is not a builtin library, try without using the specified file */
1967 if (nts
!= STATUS_SUCCESS
)
1968 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
1969 if (nts
== STATUS_SUCCESS
&& loadorder
== LO_DEFAULT
&&
1970 (MODULE_InitDLL( *pwm
, DLL_WINE_PREATTACH
, NULL
) != STATUS_SUCCESS
))
1972 /* stub-only dll, try native */
1973 TRACE( "%s pre-attach returned FALSE, preferring native\n", debugstr_w(filename
) );
1974 LdrUnloadDll( (*pwm
)->ldr
.BaseAddress
);
1975 nts
= STATUS_DLL_NOT_FOUND
;
1977 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
!= LO_BUILTIN
)
1978 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
1982 if (nts
== STATUS_SUCCESS
)
1984 /* Initialize DLL just loaded */
1985 TRACE("Loaded module %s (%s) at %p\n", debugstr_w(filename
),
1986 ((*pwm
)->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native",
1987 (*pwm
)->ldr
.BaseAddress
);
1988 if (handle
) NtClose( handle
);
1989 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1993 WARN("Failed to load module %s; status=%x\n", debugstr_w(libname
), nts
);
1994 if (handle
) NtClose( handle
);
1995 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1999 /******************************************************************
2000 * LdrLoadDll (NTDLL.@)
2002 NTSTATUS WINAPI
LdrLoadDll(LPCWSTR path_name
, DWORD flags
,
2003 const UNICODE_STRING
*libname
, HMODULE
* hModule
)
2008 RtlEnterCriticalSection( &loader_section
);
2010 if (!path_name
) path_name
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2011 nts
= load_dll( path_name
, libname
->Buffer
, flags
, &wm
);
2013 if (nts
== STATUS_SUCCESS
&& !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
2015 nts
= process_attach( wm
, NULL
);
2016 if (nts
!= STATUS_SUCCESS
)
2018 LdrUnloadDll(wm
->ldr
.BaseAddress
);
2022 *hModule
= (wm
) ? wm
->ldr
.BaseAddress
: NULL
;
2024 RtlLeaveCriticalSection( &loader_section
);
2029 /******************************************************************
2030 * LdrGetDllHandle (NTDLL.@)
2032 NTSTATUS WINAPI
LdrGetDllHandle( LPCWSTR load_path
, ULONG flags
, const UNICODE_STRING
*name
, HMODULE
*base
)
2040 RtlEnterCriticalSection( &loader_section
);
2042 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2045 size
= sizeof(buffer
);
2048 status
= find_dll_file( load_path
, name
->Buffer
, filename
, &size
, &wm
, NULL
);
2049 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2050 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
2051 /* grow the buffer and retry */
2052 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
2054 status
= STATUS_NO_MEMORY
;
2059 if (status
== STATUS_SUCCESS
)
2061 if (wm
) *base
= wm
->ldr
.BaseAddress
;
2062 else status
= STATUS_DLL_NOT_FOUND
;
2065 RtlLeaveCriticalSection( &loader_section
);
2066 TRACE( "%s -> %p (load path %s)\n", debugstr_us(name
), status
? NULL
: *base
, debugstr_w(load_path
) );
2071 /******************************************************************
2072 * LdrAddRefDll (NTDLL.@)
2074 NTSTATUS WINAPI
LdrAddRefDll( ULONG flags
, HMODULE module
)
2076 NTSTATUS ret
= STATUS_SUCCESS
;
2079 if (flags
) FIXME( "%p flags %x not implemented\n", module
, flags
);
2081 RtlEnterCriticalSection( &loader_section
);
2083 if ((wm
= get_modref( module
)))
2085 if (wm
->ldr
.LoadCount
!= -1) wm
->ldr
.LoadCount
++;
2086 TRACE( "(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2088 else ret
= STATUS_INVALID_PARAMETER
;
2090 RtlLeaveCriticalSection( &loader_section
);
2095 /***********************************************************************
2096 * LdrProcessRelocationBlock (NTDLL.@)
2098 * Apply relocations to a given page of a mapped PE image.
2100 IMAGE_BASE_RELOCATION
* WINAPI
LdrProcessRelocationBlock( void *page
, UINT count
,
2101 USHORT
*relocs
, INT_PTR delta
)
2105 USHORT offset
= *relocs
& 0xfff;
2106 int type
= *relocs
>> 12;
2109 case IMAGE_REL_BASED_ABSOLUTE
:
2112 case IMAGE_REL_BASED_HIGH
:
2113 *(short *)((char *)page
+ offset
) += HIWORD(delta
);
2115 case IMAGE_REL_BASED_LOW
:
2116 *(short *)((char *)page
+ offset
) += LOWORD(delta
);
2118 case IMAGE_REL_BASED_HIGHLOW
:
2119 *(int *)((char *)page
+ offset
) += delta
;
2121 #elif defined(__x86_64__)
2122 case IMAGE_REL_BASED_DIR64
:
2123 *(INT_PTR
*)((char *)page
+ offset
) += delta
;
2127 FIXME("Unknown/unsupported fixup type %x.\n", type
);
2132 return (IMAGE_BASE_RELOCATION
*)relocs
; /* return address of next block */
2136 /******************************************************************
2137 * LdrQueryProcessModuleInformation
2140 NTSTATUS WINAPI
LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi
,
2141 ULONG buf_size
, ULONG
* req_size
)
2143 SYSTEM_MODULE
* sm
= &smi
->Modules
[0];
2144 ULONG size
= sizeof(ULONG
);
2145 NTSTATUS nts
= STATUS_SUCCESS
;
2148 PLIST_ENTRY mark
, entry
;
2152 smi
->ModulesCount
= 0;
2154 RtlEnterCriticalSection( &loader_section
);
2155 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2156 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2158 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2159 size
+= sizeof(*sm
);
2160 if (size
<= buf_size
)
2162 sm
->Reserved1
= 0; /* FIXME */
2163 sm
->Reserved2
= 0; /* FIXME */
2164 sm
->ImageBaseAddress
= mod
->BaseAddress
;
2165 sm
->ImageSize
= mod
->SizeOfImage
;
2166 sm
->Flags
= mod
->Flags
;
2168 sm
->Rank
= 0; /* FIXME */
2169 sm
->Unknown
= 0; /* FIXME */
2171 str
.MaximumLength
= MAXIMUM_FILENAME_LENGTH
;
2172 str
.Buffer
= (char*)sm
->Name
;
2173 RtlUnicodeStringToAnsiString(&str
, &mod
->FullDllName
, FALSE
);
2174 ptr
= strrchr(str
.Buffer
, '\\');
2175 sm
->NameOffset
= (ptr
!= NULL
) ? (ptr
- str
.Buffer
+ 1) : 0;
2177 smi
->ModulesCount
++;
2180 else nts
= STATUS_INFO_LENGTH_MISMATCH
;
2182 RtlLeaveCriticalSection( &loader_section
);
2184 if (req_size
) *req_size
= size
;
2190 /******************************************************************
2191 * RtlDllShutdownInProgress (NTDLL.@)
2193 BOOLEAN WINAPI
RtlDllShutdownInProgress(void)
2195 return process_detaching
;
2199 /******************************************************************
2200 * LdrShutdownProcess (NTDLL.@)
2203 void WINAPI
LdrShutdownProcess(void)
2206 process_detach( TRUE
, (LPVOID
)1 );
2209 /******************************************************************
2210 * LdrShutdownThread (NTDLL.@)
2213 void WINAPI
LdrShutdownThread(void)
2215 PLIST_ENTRY mark
, entry
;
2220 /* don't do any detach calls if process is exiting */
2221 if (process_detaching
) return;
2222 /* FIXME: there is still a race here */
2224 RtlEnterCriticalSection( &loader_section
);
2226 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2227 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
2229 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
2230 InInitializationOrderModuleList
);
2231 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
2233 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
2236 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
2237 DLL_THREAD_DETACH
, NULL
);
2240 RtlLeaveCriticalSection( &loader_section
);
2241 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->ThreadLocalStoragePointer
);
2245 /***********************************************************************
2249 static void free_modref( WINE_MODREF
*wm
)
2251 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
2252 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
2253 if (wm
->ldr
.InInitializationOrderModuleList
.Flink
)
2254 RemoveEntryList(&wm
->ldr
.InInitializationOrderModuleList
);
2256 TRACE(" unloading %s\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
));
2257 if (!TRACE_ON(module
))
2258 TRACE_(loaddll
)("Unloaded module %s : %s\n",
2259 debugstr_w(wm
->ldr
.FullDllName
.Buffer
),
2260 (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native" );
2262 SERVER_START_REQ( unload_dll
)
2264 req
->base
= wine_server_client_ptr( wm
->ldr
.BaseAddress
);
2265 wine_server_call( req
);
2269 RtlReleaseActivationContext( wm
->ldr
.ActivationContext
);
2270 NtUnmapViewOfSection( NtCurrentProcess(), wm
->ldr
.BaseAddress
);
2271 if (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) wine_dll_unload( wm
->ldr
.SectionHandle
);
2272 if (cached_modref
== wm
) cached_modref
= NULL
;
2273 RtlFreeUnicodeString( &wm
->ldr
.FullDllName
);
2274 RtlFreeHeap( GetProcessHeap(), 0, wm
->deps
);
2275 RtlFreeHeap( GetProcessHeap(), 0, wm
);
2278 /***********************************************************************
2279 * MODULE_FlushModrefs
2281 * Remove all unused modrefs and call the internal unloading routines
2282 * for the library type.
2284 * The loader_section must be locked while calling this function.
2286 static void MODULE_FlushModrefs(void)
2288 PLIST_ENTRY mark
, entry
, prev
;
2292 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2293 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2295 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InInitializationOrderModuleList
);
2296 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2297 prev
= entry
->Blink
;
2298 if (!mod
->LoadCount
) free_modref( wm
);
2301 /* check load order list too for modules that haven't been initialized yet */
2302 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2303 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2305 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2306 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2307 prev
= entry
->Blink
;
2308 if (!mod
->LoadCount
) free_modref( wm
);
2312 /***********************************************************************
2313 * MODULE_DecRefCount
2315 * The loader_section must be locked while calling this function.
2317 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
2321 if ( wm
->ldr
.Flags
& LDR_UNLOAD_IN_PROGRESS
)
2324 if ( wm
->ldr
.LoadCount
<= 0 )
2327 --wm
->ldr
.LoadCount
;
2328 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2330 if ( wm
->ldr
.LoadCount
== 0 )
2332 wm
->ldr
.Flags
|= LDR_UNLOAD_IN_PROGRESS
;
2334 for ( i
= 0; i
< wm
->nDeps
; i
++ )
2336 MODULE_DecRefCount( wm
->deps
[i
] );
2338 wm
->ldr
.Flags
&= ~LDR_UNLOAD_IN_PROGRESS
;
2342 /******************************************************************
2343 * LdrUnloadDll (NTDLL.@)
2347 NTSTATUS WINAPI
LdrUnloadDll( HMODULE hModule
)
2349 NTSTATUS retv
= STATUS_SUCCESS
;
2351 TRACE("(%p)\n", hModule
);
2353 RtlEnterCriticalSection( &loader_section
);
2355 /* if we're stopping the whole process (and forcing the removal of all
2356 * DLLs) the library will be freed anyway
2358 if (!process_detaching
)
2363 if ((wm
= get_modref( hModule
)) != NULL
)
2365 TRACE("(%s) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
2367 /* Recursively decrement reference counts */
2368 MODULE_DecRefCount( wm
);
2370 /* Call process detach notifications */
2371 if ( free_lib_count
<= 1 )
2373 process_detach( FALSE
, NULL
);
2374 MODULE_FlushModrefs();
2380 retv
= STATUS_DLL_NOT_FOUND
;
2385 RtlLeaveCriticalSection( &loader_section
);
2390 /***********************************************************************
2391 * RtlImageNtHeader (NTDLL.@)
2393 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
2395 IMAGE_NT_HEADERS
*ret
;
2399 IMAGE_DOS_HEADER
*dos
= (IMAGE_DOS_HEADER
*)hModule
;
2402 if (dos
->e_magic
== IMAGE_DOS_SIGNATURE
)
2404 ret
= (IMAGE_NT_HEADERS
*)((char *)dos
+ dos
->e_lfanew
);
2405 if (ret
->Signature
!= IMAGE_NT_SIGNATURE
) ret
= NULL
;
2417 /***********************************************************************
2418 * attach_process_dlls
2420 * Initial attach to all the dlls loaded by the process.
2422 static NTSTATUS
attach_process_dlls( void *wm
)
2426 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
2428 RtlEnterCriticalSection( &loader_section
);
2429 if ((status
= process_attach( wm
, (LPVOID
)1 )) != STATUS_SUCCESS
)
2431 if (last_failed_modref
)
2432 ERR( "%s failed to initialize, aborting\n",
2433 debugstr_w(last_failed_modref
->ldr
.BaseDllName
.Buffer
) + 1 );
2436 attach_implicitly_loaded_dlls( (LPVOID
)1 );
2437 RtlLeaveCriticalSection( &loader_section
);
2442 /******************************************************************
2443 * LdrInitializeThunk (NTDLL.@)
2446 void WINAPI
LdrInitializeThunk( ULONG unknown1
, ULONG unknown2
, ULONG unknown3
, ULONG unknown4
)
2452 PEB
*peb
= NtCurrentTeb()->Peb
;
2453 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
2455 if (main_exe_file
) NtClose( main_exe_file
); /* at this point the main module is created */
2457 /* allocate the modref for the main exe (if not already done) */
2458 wm
= get_modref( peb
->ImageBaseAddress
);
2460 if (wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
)
2462 ERR("%s is a dll, not an executable\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
) );
2466 peb
->LoaderLock
= &loader_section
;
2467 peb
->ProcessParameters
->ImagePathName
= wm
->ldr
.FullDllName
;
2468 version_init( wm
->ldr
.FullDllName
.Buffer
);
2470 /* the main exe needs to be the first in the load order list */
2471 RemoveEntryList( &wm
->ldr
.InLoadOrderModuleList
);
2472 InsertHeadList( &peb
->LdrData
->InLoadOrderModuleList
, &wm
->ldr
.InLoadOrderModuleList
);
2474 stack_size
= max( nt
->OptionalHeader
.SizeOfStackReserve
, nt
->OptionalHeader
.SizeOfStackCommit
);
2475 if (stack_size
< 1024 * 1024) stack_size
= 1024 * 1024; /* Xlib needs a large stack */
2477 if ((status
= virtual_alloc_thread_stack( NULL
, stack_size
)) != STATUS_SUCCESS
) goto error
;
2478 if ((status
= server_init_process_done()) != STATUS_SUCCESS
) goto error
;
2481 load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2482 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
) goto error
;
2483 if ((status
= alloc_process_tls()) != STATUS_SUCCESS
) goto error
;
2484 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto error
;
2485 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
) VIRTUAL_UseLargeAddressSpace();
2487 status
= wine_call_on_stack( attach_process_dlls
, wm
, NtCurrentTeb()->Tib
.StackBase
);
2488 if (status
!= STATUS_SUCCESS
) goto error
;
2490 virtual_clear_thread_stack();
2494 ERR( "Main exe initialization for %s failed, status %x\n",
2495 debugstr_w(peb
->ProcessParameters
->ImagePathName
.Buffer
), status
);
2496 NtTerminateProcess( GetCurrentProcess(), status
);
2500 /***********************************************************************
2501 * RtlImageDirectoryEntryToData (NTDLL.@)
2503 PVOID WINAPI
RtlImageDirectoryEntryToData( HMODULE module
, BOOL image
, WORD dir
, ULONG
*size
)
2505 const IMAGE_NT_HEADERS
*nt
;
2508 if ((ULONG_PTR
)module
& 1) /* mapped as data file */
2510 module
= (HMODULE
)((ULONG_PTR
)module
& ~1);
2513 if (!(nt
= RtlImageNtHeader( module
))) return NULL
;
2514 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
2515 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
2516 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
2517 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)module
+ addr
;
2519 /* not mapped as image, need to find the section containing the virtual address */
2520 return RtlImageRvaToVa( nt
, module
, addr
, NULL
);
2524 /***********************************************************************
2525 * RtlImageRvaToSection (NTDLL.@)
2527 PIMAGE_SECTION_HEADER WINAPI
RtlImageRvaToSection( const IMAGE_NT_HEADERS
*nt
,
2528 HMODULE module
, DWORD rva
)
2531 const IMAGE_SECTION_HEADER
*sec
;
2533 sec
= (const IMAGE_SECTION_HEADER
*)((const char*)&nt
->OptionalHeader
+
2534 nt
->FileHeader
.SizeOfOptionalHeader
);
2535 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
2537 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2538 return (PIMAGE_SECTION_HEADER
)sec
;
2544 /***********************************************************************
2545 * RtlImageRvaToVa (NTDLL.@)
2547 PVOID WINAPI
RtlImageRvaToVa( const IMAGE_NT_HEADERS
*nt
, HMODULE module
,
2548 DWORD rva
, IMAGE_SECTION_HEADER
**section
)
2550 IMAGE_SECTION_HEADER
*sec
;
2552 if (section
&& *section
) /* try this section first */
2555 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2558 if (!(sec
= RtlImageRvaToSection( nt
, module
, rva
))) return NULL
;
2560 if (section
) *section
= sec
;
2561 return (char *)module
+ sec
->PointerToRawData
+ (rva
- sec
->VirtualAddress
);
2565 /***********************************************************************
2566 * RtlPcToFileHeader (NTDLL.@)
2568 PVOID WINAPI
RtlPcToFileHeader( PVOID pc
, PVOID
*address
)
2573 RtlEnterCriticalSection( &loader_section
);
2574 if (!LdrFindEntryForAddress( pc
, &module
)) ret
= module
->BaseAddress
;
2575 RtlLeaveCriticalSection( &loader_section
);
2581 /***********************************************************************
2582 * NtLoadDriver (NTDLL.@)
2583 * ZwLoadDriver (NTDLL.@)
2585 NTSTATUS WINAPI
NtLoadDriver( const UNICODE_STRING
*DriverServiceName
)
2587 FIXME("(%p), stub!\n",DriverServiceName
);
2588 return STATUS_NOT_IMPLEMENTED
;
2592 /***********************************************************************
2593 * NtUnloadDriver (NTDLL.@)
2594 * ZwUnloadDriver (NTDLL.@)
2596 NTSTATUS WINAPI
NtUnloadDriver( const UNICODE_STRING
*DriverServiceName
)
2598 FIXME("(%p), stub!\n",DriverServiceName
);
2599 return STATUS_NOT_IMPLEMENTED
;
2603 /******************************************************************
2606 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
2608 if (reason
== DLL_PROCESS_ATTACH
) LdrDisableThreadCalloutsForDll( inst
);
2613 /******************************************************************
2614 * __wine_init_windows_dir (NTDLL.@)
2616 * Windows and system dir initialization once kernel32 has been loaded.
2618 void CDECL
__wine_init_windows_dir( const WCHAR
*windir
, const WCHAR
*sysdir
)
2620 PLIST_ENTRY mark
, entry
;
2623 RtlCreateUnicodeString( &windows_dir
, windir
);
2624 RtlCreateUnicodeString( &system_dir
, sysdir
);
2625 strcpyW( user_shared_data
->NtSystemRoot
, windir
);
2627 /* prepend the system dir to the name of the already created modules */
2628 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2629 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2631 LDR_MODULE
*mod
= CONTAINING_RECORD( entry
, LDR_MODULE
, InLoadOrderModuleList
);
2633 assert( mod
->Flags
& LDR_WINE_INTERNAL
);
2635 buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
2636 system_dir
.Length
+ mod
->FullDllName
.Length
+ 2*sizeof(WCHAR
) );
2637 if (!buffer
) continue;
2638 strcpyW( buffer
, system_dir
.Buffer
);
2639 p
= buffer
+ strlenW( buffer
);
2640 if (p
> buffer
&& p
[-1] != '\\') *p
++ = '\\';
2641 strcpyW( p
, mod
->FullDllName
.Buffer
);
2642 RtlInitUnicodeString( &mod
->FullDllName
, buffer
);
2643 RtlInitUnicodeString( &mod
->BaseDllName
, p
);
2648 /***********************************************************************
2649 * __wine_process_init
2651 void __wine_process_init(void)
2653 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
2657 ANSI_STRING func_name
;
2658 void (* DECLSPEC_NORETURN CDECL init_func
)(void);
2659 extern mode_t FILE_umask
;
2661 main_exe_file
= thread_init();
2663 /* retrieve current umask */
2664 FILE_umask
= umask(0777);
2665 umask( FILE_umask
);
2667 /* setup the load callback and create ntdll modref */
2668 wine_dll_set_callback( load_builtin_callback
);
2670 if ((status
= load_builtin_dll( NULL
, kernel32W
, 0, 0, &wm
)) != STATUS_SUCCESS
)
2672 MESSAGE( "wine: could not load kernel32.dll, status %x\n", status
);
2675 RtlInitAnsiString( &func_name
, "UnhandledExceptionFilter" );
2676 LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
, 0, (void **)&unhandled_exception_filter
);
2678 RtlInitAnsiString( &func_name
, "__wine_kernel_init" );
2679 if ((status
= LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
,
2680 0, (void **)&init_func
)) != STATUS_SUCCESS
)
2682 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %x\n", status
);