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 (mod
->BaseAddress
<= addr
&&
1229 (const char *)addr
< (char*)mod
->BaseAddress
+ mod
->SizeOfImage
)
1232 return STATUS_SUCCESS
;
1234 if (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
)
1471 IMAGE_NT_HEADERS
*nt
;
1476 TRACE("Trying native dll %s\n", debugstr_w(name
));
1479 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1480 NULL
, &size
, PAGE_READONLY
, SEC_IMAGE
, file
);
1481 if (status
!= STATUS_SUCCESS
) return status
;
1484 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(),
1485 &module
, 0, 0, &size
, &len
, ViewShare
, 0, PAGE_READONLY
);
1487 if (status
!= STATUS_SUCCESS
) return status
;
1489 /* create the MODREF */
1491 if (!(wm
= alloc_module( module
, name
))) return STATUS_NO_MEMORY
;
1495 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1497 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
)
1499 /* the module has only be inserted in the load & memory order lists */
1500 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1501 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1503 /* FIXME: there are several more dangling references
1504 * left. Including dlls loaded by this dll before the
1505 * failed one. Unrolling is rather difficult with the
1506 * current structure and we can leave them lying
1507 * around with no problems, so we don't care.
1508 * As these might reference our wm, we don't free it.
1514 /* send DLL load event */
1516 nt
= RtlImageNtHeader( module
);
1518 SERVER_START_REQ( load_dll
)
1520 req
->handle
= wine_server_obj_handle( file
);
1521 req
->base
= wine_server_client_ptr( module
);
1522 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1523 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1524 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1525 req
->name
= wine_server_client_ptr( &wm
->ldr
.FullDllName
.Buffer
);
1526 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1527 wine_server_call( req
);
1531 if ((wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
) && TRACE_ON(snoop
)) SNOOP_SetupDLL( module
);
1533 TRACE_(loaddll
)( "Loaded %s at %p: native\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
), module
);
1535 wm
->ldr
.LoadCount
= 1;
1537 return STATUS_SUCCESS
;
1541 /***********************************************************************
1544 static NTSTATUS
load_builtin_dll( LPCWSTR load_path
, LPCWSTR path
, HANDLE file
,
1545 DWORD flags
, WINE_MODREF
** pwm
)
1547 char error
[256], dllname
[MAX_PATH
];
1548 const WCHAR
*name
, *p
;
1550 void *handle
= NULL
;
1551 struct builtin_load_info info
, *prev_info
;
1553 /* Fix the name in case we have a full path and extension */
1555 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
1556 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
1558 /* load_library will modify info.status. Note also that load_library can be
1559 * called several times, if the .so file we're loading has dependencies.
1560 * info.status will gather all the errors we may get while loading all these
1563 info
.load_path
= load_path
;
1564 info
.filename
= NULL
;
1565 info
.status
= STATUS_SUCCESS
;
1568 if (file
) /* we have a real file, try to load it */
1570 UNICODE_STRING nt_name
;
1571 ANSI_STRING unix_name
;
1573 TRACE("Trying built-in %s\n", debugstr_w(path
));
1575 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1576 return STATUS_DLL_NOT_FOUND
;
1578 if (wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
))
1580 RtlFreeUnicodeString( &nt_name
);
1581 return STATUS_DLL_NOT_FOUND
;
1583 prev_info
= builtin_load_info
;
1584 info
.filename
= nt_name
.Buffer
+ 4; /* skip \??\ */
1585 builtin_load_info
= &info
;
1586 handle
= wine_dlopen( unix_name
.Buffer
, RTLD_NOW
, error
, sizeof(error
) );
1587 builtin_load_info
= prev_info
;
1588 RtlFreeUnicodeString( &nt_name
);
1589 RtlFreeHeap( GetProcessHeap(), 0, unix_name
.Buffer
);
1592 WARN( "failed to load .so lib for builtin %s: %s\n", debugstr_w(path
), error
);
1593 return STATUS_INVALID_IMAGE_FORMAT
;
1600 TRACE("Trying built-in %s\n", debugstr_w(name
));
1602 /* we don't want to depend on the current codepage here */
1603 len
= strlenW( name
) + 1;
1604 if (len
>= sizeof(dllname
)) return STATUS_NAME_TOO_LONG
;
1605 for (i
= 0; i
< len
; i
++)
1607 if (name
[i
] > 127) return STATUS_DLL_NOT_FOUND
;
1608 dllname
[i
] = (char)name
[i
];
1609 if (dllname
[i
] >= 'A' && dllname
[i
] <= 'Z') dllname
[i
] += 'a' - 'A';
1612 prev_info
= builtin_load_info
;
1613 builtin_load_info
= &info
;
1614 handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
);
1615 builtin_load_info
= prev_info
;
1620 /* The file does not exist -> WARN() */
1621 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1622 return STATUS_DLL_NOT_FOUND
;
1624 /* ERR() for all other errors (missing functions, ...) */
1625 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1626 return STATUS_PROCEDURE_NOT_FOUND
;
1630 if (info
.status
!= STATUS_SUCCESS
)
1632 wine_dll_unload( handle
);
1638 PLIST_ENTRY mark
, entry
;
1640 /* The constructor wasn't called, this means the .so is already
1641 * loaded under a different name. Try to find the wm for it. */
1643 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1644 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1646 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1647 if (mod
->Flags
& LDR_WINE_INTERNAL
&& mod
->SectionHandle
== handle
)
1649 info
.wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
1650 TRACE( "Found %s at %p for builtin %s\n",
1651 debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
, debugstr_w(path
) );
1655 wine_dll_unload( handle
); /* release the libdl refcount */
1656 if (!info
.wm
) return STATUS_INVALID_IMAGE_FORMAT
;
1657 if (info
.wm
->ldr
.LoadCount
!= -1) info
.wm
->ldr
.LoadCount
++;
1661 TRACE_(loaddll
)( "Loaded %s at %p: builtin\n", debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
);
1662 info
.wm
->ldr
.LoadCount
= 1;
1663 info
.wm
->ldr
.SectionHandle
= handle
;
1667 return STATUS_SUCCESS
;
1671 /***********************************************************************
1674 * Find the full path (if any) of the dll from the activation context.
1676 static NTSTATUS
find_actctx_dll( LPCWSTR libname
, LPWSTR
*fullname
)
1678 static const WCHAR winsxsW
[] = {'\\','w','i','n','s','x','s','\\'};
1679 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
1681 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
1682 ACTCTX_SECTION_KEYED_DATA data
;
1683 UNICODE_STRING nameW
;
1685 SIZE_T needed
, size
= 1024;
1688 RtlInitUnicodeString( &nameW
, libname
);
1689 data
.cbSize
= sizeof(data
);
1690 status
= RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
1691 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
1693 if (status
!= STATUS_SUCCESS
) return status
;
1697 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1699 status
= STATUS_NO_MEMORY
;
1702 status
= RtlQueryInformationActivationContext( 0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
1703 AssemblyDetailedInformationInActivationContext
,
1704 info
, size
, &needed
);
1705 if (status
== STATUS_SUCCESS
) break;
1706 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
1707 RtlFreeHeap( GetProcessHeap(), 0, info
);
1709 /* restart with larger buffer */
1712 if ((p
= strrchrW( info
->lpAssemblyManifestPath
, '\\' )))
1714 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1717 if (strncmpiW( p
, info
->lpAssemblyDirectoryName
, dirlen
) || strcmpiW( p
+ dirlen
, dotManifestW
))
1719 /* manifest name does not match directory name, so it's not a global
1720 * windows/winsxs manifest; use the manifest directory name instead */
1721 dirlen
= p
- info
->lpAssemblyManifestPath
;
1722 needed
= (dirlen
+ 1) * sizeof(WCHAR
) + nameW
.Length
;
1723 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1725 status
= STATUS_NO_MEMORY
;
1728 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
1730 strcpyW( p
, libname
);
1735 needed
= (windows_dir
.Length
+ sizeof(winsxsW
) + info
->ulAssemblyDirectoryNameLength
+
1736 nameW
.Length
+ 2*sizeof(WCHAR
));
1738 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1740 status
= STATUS_NO_MEMORY
;
1743 memcpy( p
, windows_dir
.Buffer
, windows_dir
.Length
);
1744 p
+= windows_dir
.Length
/ sizeof(WCHAR
);
1745 memcpy( p
, winsxsW
, sizeof(winsxsW
) );
1746 p
+= sizeof(winsxsW
) / sizeof(WCHAR
);
1747 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
1748 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1750 strcpyW( p
, libname
);
1752 RtlFreeHeap( GetProcessHeap(), 0, info
);
1753 RtlReleaseActivationContext( data
.hActCtx
);
1758 /***********************************************************************
1761 * Find the file (or already loaded module) for a given dll name.
1763 static NTSTATUS
find_dll_file( const WCHAR
*load_path
, const WCHAR
*libname
,
1764 WCHAR
*filename
, ULONG
*size
, WINE_MODREF
**pwm
, HANDLE
*handle
)
1766 OBJECT_ATTRIBUTES attr
;
1768 UNICODE_STRING nt_name
;
1769 WCHAR
*file_part
, *ext
, *dllname
;
1772 /* first append .dll if needed */
1775 if (!(ext
= strrchrW( libname
, '.')) || strchrW( ext
, '/' ) || strchrW( ext
, '\\'))
1777 if (!(dllname
= RtlAllocateHeap( GetProcessHeap(), 0,
1778 (strlenW(libname
) * sizeof(WCHAR
)) + sizeof(dllW
) )))
1779 return STATUS_NO_MEMORY
;
1780 strcpyW( dllname
, libname
);
1781 strcatW( dllname
, dllW
);
1785 nt_name
.Buffer
= NULL
;
1787 if (!contains_path( libname
))
1790 WCHAR
*fullname
= NULL
;
1792 if ((*pwm
= find_basename_module( libname
)) != NULL
) goto found
;
1794 status
= find_actctx_dll( libname
, &fullname
);
1795 if (status
== STATUS_SUCCESS
)
1797 TRACE ("found %s for %s\n", debugstr_w(fullname
), debugstr_w(libname
) );
1798 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1799 libname
= dllname
= fullname
;
1801 else if (status
!= STATUS_SXS_KEY_NOT_FOUND
)
1803 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1808 if (RtlDetermineDosPathNameType_U( libname
) == RELATIVE_PATH
)
1810 /* we need to search for it */
1811 len
= RtlDosSearchPath_U( load_path
, libname
, NULL
, *size
, filename
, &file_part
);
1814 if (len
>= *size
) goto overflow
;
1815 if ((*pwm
= find_fullname_module( filename
)) || !handle
) goto found
;
1817 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, NULL
, NULL
))
1819 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1820 return STATUS_NO_MEMORY
;
1822 attr
.Length
= sizeof(attr
);
1823 attr
.RootDirectory
= 0;
1824 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1825 attr
.ObjectName
= &nt_name
;
1826 attr
.SecurityDescriptor
= NULL
;
1827 attr
.SecurityQualityOfService
= NULL
;
1828 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1834 if (!contains_path( libname
))
1836 /* if libname doesn't contain a path at all, we simply return the name as is,
1837 * to be loaded as builtin */
1838 len
= strlenW(libname
) * sizeof(WCHAR
);
1839 if (len
>= *size
) goto overflow
;
1840 strcpyW( filename
, libname
);
1845 /* absolute path name, or relative path name but not found above */
1847 if (!RtlDosPathNameToNtPathName_U( libname
, &nt_name
, &file_part
, NULL
))
1849 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1850 return STATUS_NO_MEMORY
;
1852 len
= nt_name
.Length
- 4*sizeof(WCHAR
); /* for \??\ prefix */
1853 if (len
>= *size
) goto overflow
;
1854 memcpy( filename
, nt_name
.Buffer
+ 4, len
+ sizeof(WCHAR
) );
1855 if (!(*pwm
= find_fullname_module( filename
)) && handle
)
1857 attr
.Length
= sizeof(attr
);
1858 attr
.RootDirectory
= 0;
1859 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1860 attr
.ObjectName
= &nt_name
;
1861 attr
.SecurityDescriptor
= NULL
;
1862 attr
.SecurityQualityOfService
= NULL
;
1863 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1866 RtlFreeUnicodeString( &nt_name
);
1867 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1868 return STATUS_SUCCESS
;
1871 RtlFreeUnicodeString( &nt_name
);
1872 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1873 *size
= len
+ sizeof(WCHAR
);
1874 return STATUS_BUFFER_TOO_SMALL
;
1878 /***********************************************************************
1879 * load_dll (internal)
1881 * Load a PE style module according to the load order.
1882 * The loader_section must be locked while calling this function.
1884 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
)
1886 enum loadorder loadorder
;
1890 WINE_MODREF
*main_exe
;
1894 TRACE( "looking for %s in %s\n", debugstr_w(libname
), debugstr_w(load_path
) );
1898 size
= sizeof(buffer
);
1901 nts
= find_dll_file( load_path
, libname
, filename
, &size
, pwm
, &handle
);
1902 if (nts
== STATUS_SUCCESS
) break;
1903 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1904 if (nts
!= STATUS_BUFFER_TOO_SMALL
) return nts
;
1905 /* grow the buffer and retry */
1906 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
))) return STATUS_NO_MEMORY
;
1909 if (*pwm
) /* found already loaded module */
1911 if ((*pwm
)->ldr
.LoadCount
!= -1) (*pwm
)->ldr
.LoadCount
++;
1913 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
)) fixup_imports( *pwm
, load_path
);
1915 TRACE("Found %s for %s at %p, count=%d\n",
1916 debugstr_w((*pwm
)->ldr
.FullDllName
.Buffer
), debugstr_w(libname
),
1917 (*pwm
)->ldr
.BaseAddress
, (*pwm
)->ldr
.LoadCount
);
1918 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1919 return STATUS_SUCCESS
;
1922 main_exe
= get_modref( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1923 loadorder
= get_load_order( main_exe
? main_exe
->ldr
.BaseDllName
.Buffer
: NULL
, filename
);
1925 if (handle
&& is_fake_dll( handle
))
1927 TRACE( "%s is a fake Wine dll\n", debugstr_w(filename
) );
1935 nts
= STATUS_NO_MEMORY
;
1938 nts
= STATUS_DLL_NOT_FOUND
;
1941 case LO_NATIVE_BUILTIN
:
1942 if (!handle
) nts
= STATUS_DLL_NOT_FOUND
;
1945 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
1946 if (nts
== STATUS_INVALID_FILE_FOR_SECTION
)
1947 /* not in PE format, maybe it's a builtin */
1948 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
1950 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
== LO_NATIVE_BUILTIN
)
1951 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
1954 case LO_BUILTIN_NATIVE
:
1955 case LO_DEFAULT
: /* default is builtin,native */
1956 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
1957 if (!handle
) break; /* nothing else we can try */
1958 /* file is not a builtin library, try without using the specified file */
1959 if (nts
!= STATUS_SUCCESS
)
1960 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
1961 if (nts
== STATUS_SUCCESS
&& loadorder
== LO_DEFAULT
&&
1962 (MODULE_InitDLL( *pwm
, DLL_WINE_PREATTACH
, NULL
) != STATUS_SUCCESS
))
1964 /* stub-only dll, try native */
1965 TRACE( "%s pre-attach returned FALSE, preferring native\n", debugstr_w(filename
) );
1966 LdrUnloadDll( (*pwm
)->ldr
.BaseAddress
);
1967 nts
= STATUS_DLL_NOT_FOUND
;
1969 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
!= LO_BUILTIN
)
1970 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
1974 if (nts
== STATUS_SUCCESS
)
1976 /* Initialize DLL just loaded */
1977 TRACE("Loaded module %s (%s) at %p\n", debugstr_w(filename
),
1978 ((*pwm
)->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native",
1979 (*pwm
)->ldr
.BaseAddress
);
1980 if (handle
) NtClose( handle
);
1981 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1985 WARN("Failed to load module %s; status=%x\n", debugstr_w(libname
), nts
);
1986 if (handle
) NtClose( handle
);
1987 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1991 /******************************************************************
1992 * LdrLoadDll (NTDLL.@)
1994 NTSTATUS WINAPI
LdrLoadDll(LPCWSTR path_name
, DWORD flags
,
1995 const UNICODE_STRING
*libname
, HMODULE
* hModule
)
2000 RtlEnterCriticalSection( &loader_section
);
2002 if (!path_name
) path_name
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2003 nts
= load_dll( path_name
, libname
->Buffer
, flags
, &wm
);
2005 if (nts
== STATUS_SUCCESS
&& !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
2007 nts
= process_attach( wm
, NULL
);
2008 if (nts
!= STATUS_SUCCESS
)
2010 LdrUnloadDll(wm
->ldr
.BaseAddress
);
2014 *hModule
= (wm
) ? wm
->ldr
.BaseAddress
: NULL
;
2016 RtlLeaveCriticalSection( &loader_section
);
2021 /******************************************************************
2022 * LdrGetDllHandle (NTDLL.@)
2024 NTSTATUS WINAPI
LdrGetDllHandle( LPCWSTR load_path
, ULONG flags
, const UNICODE_STRING
*name
, HMODULE
*base
)
2032 RtlEnterCriticalSection( &loader_section
);
2034 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2037 size
= sizeof(buffer
);
2040 status
= find_dll_file( load_path
, name
->Buffer
, filename
, &size
, &wm
, NULL
);
2041 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2042 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
2043 /* grow the buffer and retry */
2044 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
2046 status
= STATUS_NO_MEMORY
;
2051 if (status
== STATUS_SUCCESS
)
2053 if (wm
) *base
= wm
->ldr
.BaseAddress
;
2054 else status
= STATUS_DLL_NOT_FOUND
;
2057 RtlLeaveCriticalSection( &loader_section
);
2058 TRACE( "%s -> %p (load path %s)\n", debugstr_us(name
), status
? NULL
: *base
, debugstr_w(load_path
) );
2063 /******************************************************************
2064 * LdrAddRefDll (NTDLL.@)
2066 NTSTATUS WINAPI
LdrAddRefDll( ULONG flags
, HMODULE module
)
2068 NTSTATUS ret
= STATUS_SUCCESS
;
2071 if (flags
) FIXME( "%p flags %x not implemented\n", module
, flags
);
2073 RtlEnterCriticalSection( &loader_section
);
2075 if ((wm
= get_modref( module
)))
2077 if (wm
->ldr
.LoadCount
!= -1) wm
->ldr
.LoadCount
++;
2078 TRACE( "(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2080 else ret
= STATUS_INVALID_PARAMETER
;
2082 RtlLeaveCriticalSection( &loader_section
);
2087 /***********************************************************************
2088 * LdrProcessRelocationBlock (NTDLL.@)
2090 * Apply relocations to a given page of a mapped PE image.
2092 IMAGE_BASE_RELOCATION
* WINAPI
LdrProcessRelocationBlock( void *page
, UINT count
,
2093 USHORT
*relocs
, INT_PTR delta
)
2097 USHORT offset
= *relocs
& 0xfff;
2098 int type
= *relocs
>> 12;
2101 case IMAGE_REL_BASED_ABSOLUTE
:
2104 case IMAGE_REL_BASED_HIGH
:
2105 *(short *)((char *)page
+ offset
) += HIWORD(delta
);
2107 case IMAGE_REL_BASED_LOW
:
2108 *(short *)((char *)page
+ offset
) += LOWORD(delta
);
2110 case IMAGE_REL_BASED_HIGHLOW
:
2111 *(int *)((char *)page
+ offset
) += delta
;
2113 #elif defined(__x86_64__)
2114 case IMAGE_REL_BASED_DIR64
:
2115 *(INT_PTR
*)((char *)page
+ offset
) += delta
;
2119 FIXME("Unknown/unsupported fixup type %x.\n", type
);
2124 return (IMAGE_BASE_RELOCATION
*)relocs
; /* return address of next block */
2128 /******************************************************************
2129 * LdrQueryProcessModuleInformation
2132 NTSTATUS WINAPI
LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi
,
2133 ULONG buf_size
, ULONG
* req_size
)
2135 SYSTEM_MODULE
* sm
= &smi
->Modules
[0];
2136 ULONG size
= sizeof(ULONG
);
2137 NTSTATUS nts
= STATUS_SUCCESS
;
2140 PLIST_ENTRY mark
, entry
;
2144 smi
->ModulesCount
= 0;
2146 RtlEnterCriticalSection( &loader_section
);
2147 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2148 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2150 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2151 size
+= sizeof(*sm
);
2152 if (size
<= buf_size
)
2154 sm
->Reserved1
= 0; /* FIXME */
2155 sm
->Reserved2
= 0; /* FIXME */
2156 sm
->ImageBaseAddress
= mod
->BaseAddress
;
2157 sm
->ImageSize
= mod
->SizeOfImage
;
2158 sm
->Flags
= mod
->Flags
;
2160 sm
->Rank
= 0; /* FIXME */
2161 sm
->Unknown
= 0; /* FIXME */
2163 str
.MaximumLength
= MAXIMUM_FILENAME_LENGTH
;
2164 str
.Buffer
= (char*)sm
->Name
;
2165 RtlUnicodeStringToAnsiString(&str
, &mod
->FullDllName
, FALSE
);
2166 ptr
= strrchr(str
.Buffer
, '\\');
2167 sm
->NameOffset
= (ptr
!= NULL
) ? (ptr
- str
.Buffer
+ 1) : 0;
2169 smi
->ModulesCount
++;
2172 else nts
= STATUS_INFO_LENGTH_MISMATCH
;
2174 RtlLeaveCriticalSection( &loader_section
);
2176 if (req_size
) *req_size
= size
;
2182 /******************************************************************
2183 * RtlDllShutdownInProgress (NTDLL.@)
2185 BOOLEAN WINAPI
RtlDllShutdownInProgress(void)
2187 return process_detaching
;
2191 /******************************************************************
2192 * LdrShutdownProcess (NTDLL.@)
2195 void WINAPI
LdrShutdownProcess(void)
2198 process_detach( TRUE
, (LPVOID
)1 );
2201 /******************************************************************
2202 * LdrShutdownThread (NTDLL.@)
2205 void WINAPI
LdrShutdownThread(void)
2207 PLIST_ENTRY mark
, entry
;
2212 /* don't do any detach calls if process is exiting */
2213 if (process_detaching
) return;
2214 /* FIXME: there is still a race here */
2216 RtlEnterCriticalSection( &loader_section
);
2218 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2219 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
2221 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
2222 InInitializationOrderModuleList
);
2223 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
2225 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
2228 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
2229 DLL_THREAD_DETACH
, NULL
);
2232 RtlLeaveCriticalSection( &loader_section
);
2233 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->ThreadLocalStoragePointer
);
2237 /***********************************************************************
2241 static void free_modref( WINE_MODREF
*wm
)
2243 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
2244 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
2245 if (wm
->ldr
.InInitializationOrderModuleList
.Flink
)
2246 RemoveEntryList(&wm
->ldr
.InInitializationOrderModuleList
);
2248 TRACE(" unloading %s\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
));
2249 if (!TRACE_ON(module
))
2250 TRACE_(loaddll
)("Unloaded module %s : %s\n",
2251 debugstr_w(wm
->ldr
.FullDllName
.Buffer
),
2252 (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native" );
2254 SERVER_START_REQ( unload_dll
)
2256 req
->base
= wine_server_client_ptr( wm
->ldr
.BaseAddress
);
2257 wine_server_call( req
);
2261 RtlReleaseActivationContext( wm
->ldr
.ActivationContext
);
2262 NtUnmapViewOfSection( NtCurrentProcess(), wm
->ldr
.BaseAddress
);
2263 if (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) wine_dll_unload( wm
->ldr
.SectionHandle
);
2264 if (cached_modref
== wm
) cached_modref
= NULL
;
2265 RtlFreeUnicodeString( &wm
->ldr
.FullDllName
);
2266 RtlFreeHeap( GetProcessHeap(), 0, wm
->deps
);
2267 RtlFreeHeap( GetProcessHeap(), 0, wm
);
2270 /***********************************************************************
2271 * MODULE_FlushModrefs
2273 * Remove all unused modrefs and call the internal unloading routines
2274 * for the library type.
2276 * The loader_section must be locked while calling this function.
2278 static void MODULE_FlushModrefs(void)
2280 PLIST_ENTRY mark
, entry
, prev
;
2284 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2285 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2287 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InInitializationOrderModuleList
);
2288 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2289 prev
= entry
->Blink
;
2290 if (!mod
->LoadCount
) free_modref( wm
);
2293 /* check load order list too for modules that haven't been initialized yet */
2294 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2295 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2297 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2298 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2299 prev
= entry
->Blink
;
2300 if (!mod
->LoadCount
) free_modref( wm
);
2304 /***********************************************************************
2305 * MODULE_DecRefCount
2307 * The loader_section must be locked while calling this function.
2309 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
2313 if ( wm
->ldr
.Flags
& LDR_UNLOAD_IN_PROGRESS
)
2316 if ( wm
->ldr
.LoadCount
<= 0 )
2319 --wm
->ldr
.LoadCount
;
2320 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2322 if ( wm
->ldr
.LoadCount
== 0 )
2324 wm
->ldr
.Flags
|= LDR_UNLOAD_IN_PROGRESS
;
2326 for ( i
= 0; i
< wm
->nDeps
; i
++ )
2328 MODULE_DecRefCount( wm
->deps
[i
] );
2330 wm
->ldr
.Flags
&= ~LDR_UNLOAD_IN_PROGRESS
;
2334 /******************************************************************
2335 * LdrUnloadDll (NTDLL.@)
2339 NTSTATUS WINAPI
LdrUnloadDll( HMODULE hModule
)
2341 NTSTATUS retv
= STATUS_SUCCESS
;
2343 TRACE("(%p)\n", hModule
);
2345 RtlEnterCriticalSection( &loader_section
);
2347 /* if we're stopping the whole process (and forcing the removal of all
2348 * DLLs) the library will be freed anyway
2350 if (!process_detaching
)
2355 if ((wm
= get_modref( hModule
)) != NULL
)
2357 TRACE("(%s) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
2359 /* Recursively decrement reference counts */
2360 MODULE_DecRefCount( wm
);
2362 /* Call process detach notifications */
2363 if ( free_lib_count
<= 1 )
2365 process_detach( FALSE
, NULL
);
2366 MODULE_FlushModrefs();
2372 retv
= STATUS_DLL_NOT_FOUND
;
2377 RtlLeaveCriticalSection( &loader_section
);
2382 /***********************************************************************
2383 * RtlImageNtHeader (NTDLL.@)
2385 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
2387 IMAGE_NT_HEADERS
*ret
;
2391 IMAGE_DOS_HEADER
*dos
= (IMAGE_DOS_HEADER
*)hModule
;
2394 if (dos
->e_magic
== IMAGE_DOS_SIGNATURE
)
2396 ret
= (IMAGE_NT_HEADERS
*)((char *)dos
+ dos
->e_lfanew
);
2397 if (ret
->Signature
!= IMAGE_NT_SIGNATURE
) ret
= NULL
;
2409 /***********************************************************************
2410 * attach_process_dlls
2412 * Initial attach to all the dlls loaded by the process.
2414 static NTSTATUS
attach_process_dlls( void *wm
)
2418 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
2420 RtlEnterCriticalSection( &loader_section
);
2421 if ((status
= process_attach( wm
, (LPVOID
)1 )) != STATUS_SUCCESS
)
2423 if (last_failed_modref
)
2424 ERR( "%s failed to initialize, aborting\n",
2425 debugstr_w(last_failed_modref
->ldr
.BaseDllName
.Buffer
) + 1 );
2428 attach_implicitly_loaded_dlls( (LPVOID
)1 );
2429 RtlLeaveCriticalSection( &loader_section
);
2434 /******************************************************************
2435 * LdrInitializeThunk (NTDLL.@)
2438 void WINAPI
LdrInitializeThunk( ULONG unknown1
, ULONG unknown2
, ULONG unknown3
, ULONG unknown4
)
2443 PEB
*peb
= NtCurrentTeb()->Peb
;
2444 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
2446 if (main_exe_file
) NtClose( main_exe_file
); /* at this point the main module is created */
2448 /* allocate the modref for the main exe (if not already done) */
2449 wm
= get_modref( peb
->ImageBaseAddress
);
2451 if (wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
)
2453 ERR("%s is a dll, not an executable\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
) );
2457 peb
->LoaderLock
= &loader_section
;
2458 peb
->ProcessParameters
->ImagePathName
= wm
->ldr
.FullDllName
;
2459 version_init( wm
->ldr
.FullDllName
.Buffer
);
2461 /* the main exe needs to be the first in the load order list */
2462 RemoveEntryList( &wm
->ldr
.InLoadOrderModuleList
);
2463 InsertHeadList( &peb
->LdrData
->InLoadOrderModuleList
, &wm
->ldr
.InLoadOrderModuleList
);
2465 if ((status
= virtual_alloc_thread_stack( NtCurrentTeb(), 0, 0 )) != STATUS_SUCCESS
) goto error
;
2466 if ((status
= server_init_process_done()) != STATUS_SUCCESS
) goto error
;
2469 load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2470 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
) goto error
;
2471 if ((status
= alloc_process_tls()) != STATUS_SUCCESS
) goto error
;
2472 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto error
;
2473 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
) VIRTUAL_UseLargeAddressSpace();
2475 status
= wine_call_on_stack( attach_process_dlls
, wm
, NtCurrentTeb()->Tib
.StackBase
);
2476 if (status
!= STATUS_SUCCESS
) goto error
;
2478 virtual_clear_thread_stack();
2482 ERR( "Main exe initialization for %s failed, status %x\n",
2483 debugstr_w(peb
->ProcessParameters
->ImagePathName
.Buffer
), status
);
2484 NtTerminateProcess( GetCurrentProcess(), status
);
2488 /***********************************************************************
2489 * RtlImageDirectoryEntryToData (NTDLL.@)
2491 PVOID WINAPI
RtlImageDirectoryEntryToData( HMODULE module
, BOOL image
, WORD dir
, ULONG
*size
)
2493 const IMAGE_NT_HEADERS
*nt
;
2496 if ((ULONG_PTR
)module
& 1) /* mapped as data file */
2498 module
= (HMODULE
)((ULONG_PTR
)module
& ~1);
2501 if (!(nt
= RtlImageNtHeader( module
))) return NULL
;
2502 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
2503 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
2504 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
2505 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)module
+ addr
;
2507 /* not mapped as image, need to find the section containing the virtual address */
2508 return RtlImageRvaToVa( nt
, module
, addr
, NULL
);
2512 /***********************************************************************
2513 * RtlImageRvaToSection (NTDLL.@)
2515 PIMAGE_SECTION_HEADER WINAPI
RtlImageRvaToSection( const IMAGE_NT_HEADERS
*nt
,
2516 HMODULE module
, DWORD rva
)
2519 const IMAGE_SECTION_HEADER
*sec
;
2521 sec
= (const IMAGE_SECTION_HEADER
*)((const char*)&nt
->OptionalHeader
+
2522 nt
->FileHeader
.SizeOfOptionalHeader
);
2523 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
2525 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2526 return (PIMAGE_SECTION_HEADER
)sec
;
2532 /***********************************************************************
2533 * RtlImageRvaToVa (NTDLL.@)
2535 PVOID WINAPI
RtlImageRvaToVa( const IMAGE_NT_HEADERS
*nt
, HMODULE module
,
2536 DWORD rva
, IMAGE_SECTION_HEADER
**section
)
2538 IMAGE_SECTION_HEADER
*sec
;
2540 if (section
&& *section
) /* try this section first */
2543 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2546 if (!(sec
= RtlImageRvaToSection( nt
, module
, rva
))) return NULL
;
2548 if (section
) *section
= sec
;
2549 return (char *)module
+ sec
->PointerToRawData
+ (rva
- sec
->VirtualAddress
);
2553 /***********************************************************************
2554 * RtlPcToFileHeader (NTDLL.@)
2556 PVOID WINAPI
RtlPcToFileHeader( PVOID pc
, PVOID
*address
)
2561 RtlEnterCriticalSection( &loader_section
);
2562 if (!LdrFindEntryForAddress( pc
, &module
)) ret
= module
->BaseAddress
;
2563 RtlLeaveCriticalSection( &loader_section
);
2569 /***********************************************************************
2570 * NtLoadDriver (NTDLL.@)
2571 * ZwLoadDriver (NTDLL.@)
2573 NTSTATUS WINAPI
NtLoadDriver( const UNICODE_STRING
*DriverServiceName
)
2575 FIXME("(%p), stub!\n",DriverServiceName
);
2576 return STATUS_NOT_IMPLEMENTED
;
2580 /***********************************************************************
2581 * NtUnloadDriver (NTDLL.@)
2582 * ZwUnloadDriver (NTDLL.@)
2584 NTSTATUS WINAPI
NtUnloadDriver( const UNICODE_STRING
*DriverServiceName
)
2586 FIXME("(%p), stub!\n",DriverServiceName
);
2587 return STATUS_NOT_IMPLEMENTED
;
2591 /******************************************************************
2594 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
2596 if (reason
== DLL_PROCESS_ATTACH
) LdrDisableThreadCalloutsForDll( inst
);
2601 /******************************************************************
2602 * __wine_init_windows_dir (NTDLL.@)
2604 * Windows and system dir initialization once kernel32 has been loaded.
2606 void CDECL
__wine_init_windows_dir( const WCHAR
*windir
, const WCHAR
*sysdir
)
2608 PLIST_ENTRY mark
, entry
;
2611 RtlCreateUnicodeString( &windows_dir
, windir
);
2612 RtlCreateUnicodeString( &system_dir
, sysdir
);
2613 strcpyW( user_shared_data
->NtSystemRoot
, windir
);
2615 /* prepend the system dir to the name of the already created modules */
2616 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2617 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2619 LDR_MODULE
*mod
= CONTAINING_RECORD( entry
, LDR_MODULE
, InLoadOrderModuleList
);
2621 assert( mod
->Flags
& LDR_WINE_INTERNAL
);
2623 buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
2624 system_dir
.Length
+ mod
->FullDllName
.Length
+ 2*sizeof(WCHAR
) );
2625 if (!buffer
) continue;
2626 strcpyW( buffer
, system_dir
.Buffer
);
2627 p
= buffer
+ strlenW( buffer
);
2628 if (p
> buffer
&& p
[-1] != '\\') *p
++ = '\\';
2629 strcpyW( p
, mod
->FullDllName
.Buffer
);
2630 RtlInitUnicodeString( &mod
->FullDllName
, buffer
);
2631 RtlInitUnicodeString( &mod
->BaseDllName
, p
);
2636 /***********************************************************************
2637 * __wine_process_init
2639 void __wine_process_init(void)
2641 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
2645 ANSI_STRING func_name
;
2646 void (* DECLSPEC_NORETURN CDECL init_func
)(void);
2647 extern mode_t FILE_umask
;
2649 main_exe_file
= thread_init();
2651 /* retrieve current umask */
2652 FILE_umask
= umask(0777);
2653 umask( FILE_umask
);
2655 /* setup the load callback and create ntdll modref */
2656 wine_dll_set_callback( load_builtin_callback
);
2658 if ((status
= load_builtin_dll( NULL
, kernel32W
, 0, 0, &wm
)) != STATUS_SUCCESS
)
2660 MESSAGE( "wine: could not load kernel32.dll, status %x\n", status
);
2663 RtlInitAnsiString( &func_name
, "UnhandledExceptionFilter" );
2664 LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
, 0, (void **)&unhandled_exception_filter
);
2666 RtlInitAnsiString( &func_name
, "__wine_kernel_init" );
2667 if ((status
= LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
,
2668 0, (void **)&init_func
)) != STATUS_SUCCESS
)
2670 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %x\n", status
);