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
39 #include "delayloadhandler.h"
41 #include "wine/exception.h"
42 #include "wine/library.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45 #include "wine/server.h"
46 #include "ntdll_misc.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(module
);
50 WINE_DECLARE_DEBUG_CHANNEL(relay
);
51 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
52 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
53 WINE_DECLARE_DEBUG_CHANNEL(imports
);
55 /* we don't want to include winuser.h */
56 #define RT_MANIFEST ((ULONG_PTR)24)
57 #define ISOLATIONAWARE_MANIFEST_RESOURCE_ID ((ULONG_PTR)2)
59 typedef DWORD (CALLBACK
*DLLENTRYPROC
)(HMODULE
,DWORD
,LPVOID
);
61 static BOOL process_detaching
= FALSE
; /* set on process detach to avoid deadlocks with thread detach */
62 static int free_lib_count
; /* recursion depth of LdrUnloadDll calls */
64 static const char * const reason_names
[] =
70 NULL
, NULL
, NULL
, NULL
,
74 static const WCHAR dllW
[] = {'.','d','l','l',0};
76 /* internal representation of 32bit modules. per process. */
77 typedef struct _wine_modref
81 struct _wine_modref
**deps
;
84 /* info about the current builtin dll load */
85 /* used to keep track of things across the register_dll constructor call */
86 struct builtin_load_info
88 const WCHAR
*load_path
;
89 const WCHAR
*filename
;
94 static struct builtin_load_info default_load_info
;
95 static struct builtin_load_info
*builtin_load_info
= &default_load_info
;
97 static HANDLE main_exe_file
;
98 static UINT tls_module_count
; /* number of modules with TLS directory */
99 static const IMAGE_TLS_DIRECTORY
**tls_dirs
; /* array of TLS directories */
100 LIST_ENTRY tls_links
= { &tls_links
, &tls_links
};
102 static RTL_CRITICAL_SECTION loader_section
;
103 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
105 0, 0, &loader_section
,
106 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
107 0, 0, { (DWORD_PTR
)(__FILE__
": loader_section") }
109 static RTL_CRITICAL_SECTION loader_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
111 static WINE_MODREF
*cached_modref
;
112 static WINE_MODREF
*current_modref
;
113 static WINE_MODREF
*last_failed_modref
;
115 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
);
116 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
);
117 static FARPROC
find_ordinal_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
118 DWORD exp_size
, DWORD ordinal
, LPCWSTR load_path
);
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 BOOL
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. Testing indicates
146 * that only modifying esi leads to a crash, so use this one to backup
147 * ebp while running the dll entry proc.
150 extern BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
, UINT reason
, void *reserved
);
151 __ASM_GLOBAL_FUNC(call_dll_entry_point
,
153 __ASM_CFI(".cfi_adjust_cfa_offset 4\n\t")
154 __ASM_CFI(".cfi_rel_offset %ebp,0\n\t")
156 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
158 __ASM_CFI(".cfi_rel_offset %ebx,-4\n\t")
160 __ASM_CFI(".cfi_rel_offset %esi,-8\n\t")
162 __ASM_CFI(".cfi_rel_offset %edi,-12\n\t")
164 __ASM_CFI(".cfi_def_cfa_register %esi\n\t")
168 "movl 8(%ebp),%eax\n\t"
171 __ASM_CFI(".cfi_def_cfa_register %ebp\n\t")
172 "leal -12(%ebp),%esp\n\t"
174 __ASM_CFI(".cfi_same_value %edi\n\t")
176 __ASM_CFI(".cfi_same_value %esi\n\t")
178 __ASM_CFI(".cfi_same_value %ebx\n\t")
180 __ASM_CFI(".cfi_def_cfa %esp,4\n\t")
181 __ASM_CFI(".cfi_same_value %ebp\n\t")
184 static inline BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
,
185 UINT reason
, void *reserved
)
187 return proc( module
, reason
, reserved
);
189 #endif /* __i386__ */
192 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__)
193 /*************************************************************************
196 * Entry point for stub functions.
198 static void stub_entry_point( const char *dll
, const char *name
, void *ret_addr
)
200 EXCEPTION_RECORD rec
;
202 rec
.ExceptionCode
= EXCEPTION_WINE_STUB
;
203 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
204 rec
.ExceptionRecord
= NULL
;
205 rec
.ExceptionAddress
= ret_addr
;
206 rec
.NumberParameters
= 2;
207 rec
.ExceptionInformation
[0] = (ULONG_PTR
)dll
;
208 rec
.ExceptionInformation
[1] = (ULONG_PTR
)name
;
209 for (;;) RtlRaiseException( &rec
);
213 #include "pshpack1.h"
217 BYTE pushl1
; /* pushl $name */
219 BYTE pushl2
; /* pushl $dll */
221 BYTE call
; /* call stub_entry_point */
224 #elif defined(__arm__)
227 BYTE ldr_r0
[4]; /* ldr r0, $dll */
228 BYTE mov_pc_pc1
[4]; /* mov pc,pc */
230 BYTE ldr_r1
[4]; /* ldr r1, $name */
231 BYTE mov_pc_pc2
[4]; /* mov pc,pc */
233 BYTE mov_r2_lr
[4]; /* mov r2, lr */
234 BYTE ldr_pc_pc
[4]; /* ldr pc, [pc, #-4] */
240 BYTE movq_rdi
[2]; /* movq $dll,%rdi */
242 BYTE movq_rsi
[2]; /* movq $name,%rsi */
244 BYTE movq_rsp_rdx
[4]; /* movq (%rsp),%rdx */
245 BYTE movq_rax
[2]; /* movq $entry, %rax */
247 BYTE jmpq_rax
[2]; /* jmp %rax */
252 /*************************************************************************
255 * Allocate a stub entry point.
257 static ULONG_PTR
allocate_stub( const char *dll
, const char *name
)
259 #define MAX_SIZE 65536
260 static struct stub
*stubs
;
261 static unsigned int nb_stubs
;
264 if (nb_stubs
>= MAX_SIZE
/ sizeof(*stub
)) return 0xdeadbeef;
268 SIZE_T size
= MAX_SIZE
;
269 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs
, 0, &size
,
270 MEM_COMMIT
, PAGE_EXECUTE_READWRITE
) != STATUS_SUCCESS
)
273 stub
= &stubs
[nb_stubs
++];
275 stub
->pushl1
= 0x68; /* pushl $name */
277 stub
->pushl2
= 0x68; /* pushl $dll */
279 stub
->call
= 0xe8; /* call stub_entry_point */
280 stub
->entry
= (BYTE
*)stub_entry_point
- (BYTE
*)(&stub
->entry
+ 1);
281 #elif defined(__arm__)
282 stub
->ldr_r0
[0] = 0x00; /* ldr r0, $dll */
283 stub
->ldr_r0
[1] = 0x00;
284 stub
->ldr_r0
[2] = 0x9f;
285 stub
->ldr_r0
[3] = 0xe5;
286 stub
->mov_pc_pc1
[0] = 0x0f; /* mov pc,pc */
287 stub
->mov_pc_pc1
[1] = 0xf0;
288 stub
->mov_pc_pc1
[2] = 0xa0;
289 stub
->mov_pc_pc1
[3] = 0xe1;
291 stub
->ldr_r1
[0] = 0x00; /* ldr r1, $name */
292 stub
->ldr_r1
[1] = 0x10;
293 stub
->ldr_r1
[2] = 0x9f;
294 stub
->ldr_r1
[3] = 0xe5;
295 stub
->mov_pc_pc2
[0] = 0x0f; /* mov pc,pc */
296 stub
->mov_pc_pc2
[1] = 0xf0;
297 stub
->mov_pc_pc2
[2] = 0xa0;
298 stub
->mov_pc_pc2
[3] = 0xe1;
300 stub
->mov_r2_lr
[0] = 0x0e; /* mov r2, lr */
301 stub
->mov_r2_lr
[1] = 0x20;
302 stub
->mov_r2_lr
[2] = 0xa0;
303 stub
->mov_r2_lr
[3] = 0xe1;
304 stub
->ldr_pc_pc
[0] = 0x04; /* ldr pc, [pc, #-4] */
305 stub
->ldr_pc_pc
[1] = 0xf0;
306 stub
->ldr_pc_pc
[2] = 0x1f;
307 stub
->ldr_pc_pc
[3] = 0xe5;
308 stub
->entry
= stub_entry_point
;
310 stub
->movq_rdi
[0] = 0x48; /* movq $dll,%rdi */
311 stub
->movq_rdi
[1] = 0xbf;
313 stub
->movq_rsi
[0] = 0x48; /* movq $name,%rsi */
314 stub
->movq_rsi
[1] = 0xbe;
316 stub
->movq_rsp_rdx
[0] = 0x48; /* movq (%rsp),%rdx */
317 stub
->movq_rsp_rdx
[1] = 0x8b;
318 stub
->movq_rsp_rdx
[2] = 0x14;
319 stub
->movq_rsp_rdx
[3] = 0x24;
320 stub
->movq_rax
[0] = 0x48; /* movq $entry, %rax */
321 stub
->movq_rax
[1] = 0xb8;
322 stub
->entry
= stub_entry_point
;
323 stub
->jmpq_rax
[0] = 0xff; /* jmp %rax */
324 stub
->jmpq_rax
[1] = 0xe0;
326 return (ULONG_PTR
)stub
;
330 static inline ULONG_PTR
allocate_stub( const char *dll
, const char *name
) { return 0xdeadbeef; }
331 #endif /* __i386__ */
334 /*************************************************************************
337 * Looks for the referenced HMODULE in the current process
338 * The loader_section must be locked while calling this function.
340 static WINE_MODREF
*get_modref( HMODULE hmod
)
342 PLIST_ENTRY mark
, entry
;
345 if (cached_modref
&& cached_modref
->ldr
.BaseAddress
== hmod
) return cached_modref
;
347 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
348 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
350 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
351 if (mod
->BaseAddress
== hmod
)
352 return cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
353 if (mod
->BaseAddress
> (void*)hmod
) break;
359 /**********************************************************************
360 * find_basename_module
362 * Find a module from its base name.
363 * The loader_section must be locked while calling this function
365 static WINE_MODREF
*find_basename_module( LPCWSTR name
)
367 PLIST_ENTRY mark
, entry
;
369 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.BaseDllName
.Buffer
))
370 return cached_modref
;
372 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
373 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
375 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
376 if (!strcmpiW( name
, mod
->BaseDllName
.Buffer
))
378 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
379 return cached_modref
;
386 /**********************************************************************
387 * find_fullname_module
389 * Find a module from its full path name.
390 * The loader_section must be locked while calling this function
392 static WINE_MODREF
*find_fullname_module( LPCWSTR name
)
394 PLIST_ENTRY mark
, entry
;
396 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.FullDllName
.Buffer
))
397 return cached_modref
;
399 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
400 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
402 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
403 if (!strcmpiW( name
, mod
->FullDllName
.Buffer
))
405 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
406 return cached_modref
;
413 /*************************************************************************
414 * find_forwarded_export
416 * Find the final function pointer for a forwarded function.
417 * The loader_section must be locked while calling this function.
419 static FARPROC
find_forwarded_export( HMODULE module
, const char *forward
, LPCWSTR load_path
)
421 const IMAGE_EXPORT_DIRECTORY
*exports
;
425 const char *end
= strrchr(forward
, '.');
428 if (!end
) return NULL
;
429 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
)) return NULL
;
430 ascii_to_unicode( mod_name
, forward
, end
- forward
);
431 mod_name
[end
- forward
] = 0;
432 if (!strchrW( mod_name
, '.' ))
434 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
) - sizeof(dllW
)) return NULL
;
435 memcpy( mod_name
+ (end
- forward
), dllW
, sizeof(dllW
) );
438 if (!(wm
= find_basename_module( mod_name
)))
440 TRACE( "delay loading %s for '%s'\n", debugstr_w(mod_name
), forward
);
441 if (load_dll( load_path
, mod_name
, 0, &wm
) == STATUS_SUCCESS
&&
442 !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
444 if (process_attach( wm
, NULL
) != STATUS_SUCCESS
)
446 LdrUnloadDll( wm
->ldr
.BaseAddress
);
453 ERR( "module not found for forward '%s' used by %s\n",
454 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
) );
458 if ((exports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
459 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
461 const char *name
= end
+ 1;
462 if (*name
== '#') /* ordinal */
463 proc
= find_ordinal_export( wm
->ldr
.BaseAddress
, exports
, exp_size
, atoi(name
+1), load_path
);
465 proc
= find_named_export( wm
->ldr
.BaseAddress
, exports
, exp_size
, name
, -1, load_path
);
470 ERR("function not found for forward '%s' used by %s."
471 " If you are using builtin %s, try using the native one instead.\n",
472 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
),
473 debugstr_w(get_modref(module
)->ldr
.BaseDllName
.Buffer
) );
479 /*************************************************************************
480 * find_ordinal_export
482 * Find an exported function by ordinal.
483 * The exports base must have been subtracted from the ordinal already.
484 * The loader_section must be locked while calling this function.
486 static FARPROC
find_ordinal_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
487 DWORD exp_size
, DWORD ordinal
, LPCWSTR load_path
)
490 const DWORD
*functions
= get_rva( module
, exports
->AddressOfFunctions
);
492 if (ordinal
>= exports
->NumberOfFunctions
)
494 TRACE(" ordinal %d out of range!\n", ordinal
+ exports
->Base
);
497 if (!functions
[ordinal
]) return NULL
;
499 proc
= get_rva( module
, functions
[ordinal
] );
501 /* if the address falls into the export dir, it's a forward */
502 if (((const char *)proc
>= (const char *)exports
) &&
503 ((const char *)proc
< (const char *)exports
+ exp_size
))
504 return find_forwarded_export( module
, (const char *)proc
, load_path
);
508 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
509 proc
= SNOOP_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
513 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
514 proc
= RELAY_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
520 /*************************************************************************
523 * Find an exported function by name.
524 * The loader_section must be locked while calling this function.
526 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
527 DWORD exp_size
, const char *name
, int hint
, LPCWSTR load_path
)
529 const WORD
*ordinals
= get_rva( module
, exports
->AddressOfNameOrdinals
);
530 const DWORD
*names
= get_rva( module
, exports
->AddressOfNames
);
531 int min
= 0, max
= exports
->NumberOfNames
- 1;
533 /* first check the hint */
534 if (hint
>= 0 && hint
<= max
)
536 char *ename
= get_rva( module
, names
[hint
] );
537 if (!strcmp( ename
, name
))
538 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[hint
], load_path
);
541 /* then do a binary search */
544 int res
, pos
= (min
+ max
) / 2;
545 char *ename
= get_rva( module
, names
[pos
] );
546 if (!(res
= strcmp( ename
, name
)))
547 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[pos
], load_path
);
548 if (res
> 0) max
= pos
- 1;
556 /*************************************************************************
559 * Import the dll specified by the given import descriptor.
560 * The loader_section must be locked while calling this function.
562 static WINE_MODREF
*import_dll( HMODULE module
, const IMAGE_IMPORT_DESCRIPTOR
*descr
, LPCWSTR load_path
)
567 const IMAGE_EXPORT_DIRECTORY
*exports
;
569 const IMAGE_THUNK_DATA
*import_list
;
570 IMAGE_THUNK_DATA
*thunk_list
;
572 const char *name
= get_rva( module
, descr
->Name
);
573 DWORD len
= strlen(name
);
575 SIZE_T protect_size
= 0;
578 thunk_list
= get_rva( module
, (DWORD
)descr
->FirstThunk
);
579 if (descr
->u
.OriginalFirstThunk
)
580 import_list
= get_rva( module
, (DWORD
)descr
->u
.OriginalFirstThunk
);
582 import_list
= thunk_list
;
584 while (len
&& name
[len
-1] == ' ') len
--; /* remove trailing spaces */
586 if (len
* sizeof(WCHAR
) < sizeof(buffer
))
588 ascii_to_unicode( buffer
, name
, len
);
590 status
= load_dll( load_path
, buffer
, 0, &wmImp
);
592 else /* need to allocate a larger buffer */
594 WCHAR
*ptr
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
595 if (!ptr
) return NULL
;
596 ascii_to_unicode( ptr
, name
, len
);
598 status
= load_dll( load_path
, ptr
, 0, &wmImp
);
599 RtlFreeHeap( GetProcessHeap(), 0, ptr
);
604 if (status
== STATUS_DLL_NOT_FOUND
)
605 ERR("Library %s (which is needed by %s) not found\n",
606 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
));
608 ERR("Loading library %s (which is needed by %s) failed (error %x).\n",
609 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
), status
);
613 /* unprotect the import address table since it can be located in
614 * readonly section */
615 while (import_list
[protect_size
].u1
.Ordinal
) protect_size
++;
616 protect_base
= thunk_list
;
617 protect_size
*= sizeof(*thunk_list
);
618 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
,
619 &protect_size
, PAGE_READWRITE
, &protect_old
);
621 imp_mod
= wmImp
->ldr
.BaseAddress
;
622 exports
= RtlImageDirectoryEntryToData( imp_mod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
);
626 /* set all imported function to deadbeef */
627 while (import_list
->u1
.Ordinal
)
629 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
631 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
632 WARN("No implementation for %s.%d", name
, ordinal
);
633 thunk_list
->u1
.Function
= allocate_stub( name
, IntToPtr(ordinal
) );
637 IMAGE_IMPORT_BY_NAME
*pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
638 WARN("No implementation for %s.%s", name
, pe_name
->Name
);
639 thunk_list
->u1
.Function
= allocate_stub( name
, (const char*)pe_name
->Name
);
641 WARN(" imported from %s, allocating stub %p\n",
642 debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
643 (void *)thunk_list
->u1
.Function
);
650 while (import_list
->u1
.Ordinal
)
652 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
654 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
656 thunk_list
->u1
.Function
= (ULONG_PTR
)find_ordinal_export( imp_mod
, exports
, exp_size
,
657 ordinal
- exports
->Base
, load_path
);
658 if (!thunk_list
->u1
.Function
)
660 thunk_list
->u1
.Function
= allocate_stub( name
, IntToPtr(ordinal
) );
661 WARN("No implementation for %s.%d imported from %s, setting to %p\n",
662 name
, ordinal
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
663 (void *)thunk_list
->u1
.Function
);
665 TRACE_(imports
)("--- Ordinal %s.%d = %p\n", name
, ordinal
, (void *)thunk_list
->u1
.Function
);
667 else /* import by name */
669 IMAGE_IMPORT_BY_NAME
*pe_name
;
670 pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
671 thunk_list
->u1
.Function
= (ULONG_PTR
)find_named_export( imp_mod
, exports
, exp_size
,
672 (const char*)pe_name
->Name
,
673 pe_name
->Hint
, load_path
);
674 if (!thunk_list
->u1
.Function
)
676 thunk_list
->u1
.Function
= allocate_stub( name
, (const char*)pe_name
->Name
);
677 WARN("No implementation for %s.%s imported from %s, setting to %p\n",
678 name
, pe_name
->Name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
679 (void *)thunk_list
->u1
.Function
);
681 TRACE_(imports
)("--- %s %s.%d = %p\n",
682 pe_name
->Name
, name
, pe_name
->Hint
, (void *)thunk_list
->u1
.Function
);
689 /* restore old protection of the import address table */
690 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
, &protect_size
, protect_old
, NULL
);
695 /***********************************************************************
696 * create_module_activation_context
698 static NTSTATUS
create_module_activation_context( LDR_MODULE
*module
)
701 LDR_RESOURCE_INFO info
;
702 const IMAGE_RESOURCE_DATA_ENTRY
*entry
;
704 info
.Type
= RT_MANIFEST
;
705 info
.Name
= ISOLATIONAWARE_MANIFEST_RESOURCE_ID
;
707 if (!(status
= LdrFindResource_U( module
->BaseAddress
, &info
, 3, &entry
)))
710 ctx
.cbSize
= sizeof(ctx
);
712 ctx
.dwFlags
= ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_HMODULE_VALID
;
713 ctx
.hModule
= module
->BaseAddress
;
714 ctx
.lpResourceName
= (LPCWSTR
)ISOLATIONAWARE_MANIFEST_RESOURCE_ID
;
715 status
= RtlCreateActivationContext( &module
->ActivationContext
, &ctx
);
721 /*************************************************************************
722 * is_dll_native_subsystem
724 * Check if dll is a proper native driver.
725 * Some dlls (corpol.dll from IE6 for instance) are incorrectly marked as native
726 * while being perfectly normal DLLs. This heuristic should catch such breakages.
728 static BOOL
is_dll_native_subsystem( HMODULE module
, const IMAGE_NT_HEADERS
*nt
, LPCWSTR filename
)
730 static const WCHAR ntdllW
[] = {'n','t','d','l','l','.','d','l','l',0};
731 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
732 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
736 if (nt
->OptionalHeader
.Subsystem
!= IMAGE_SUBSYSTEM_NATIVE
) return FALSE
;
737 if (nt
->OptionalHeader
.SectionAlignment
< page_size
) return TRUE
;
739 if ((imports
= RtlImageDirectoryEntryToData( module
, TRUE
,
740 IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
742 for (i
= 0; imports
[i
].Name
; i
++)
744 const char *name
= get_rva( module
, imports
[i
].Name
);
745 DWORD len
= strlen(name
);
746 if (len
* sizeof(WCHAR
) >= sizeof(buffer
)) continue;
747 ascii_to_unicode( buffer
, name
, len
+ 1 );
748 if (!strcmpiW( buffer
, ntdllW
) || !strcmpiW( buffer
, kernel32W
))
750 TRACE( "%s imports %s, assuming not native\n", debugstr_w(filename
), debugstr_w(buffer
) );
758 /*************************************************************************
761 * Allocate a TLS slot for a newly-loaded module.
762 * The loader_section must be locked while calling this function.
764 static SHORT
alloc_tls_slot( LDR_MODULE
*mod
)
766 const IMAGE_TLS_DIRECTORY
*dir
;
771 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
, IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
774 size
= dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
;
775 if (!size
&& !dir
->SizeOfZeroFill
&& !dir
->AddressOfCallBacks
) return -1;
777 for (i
= 0; i
< tls_module_count
; i
++) if (!tls_dirs
[i
]) break;
779 TRACE( "module %p data %p-%p zerofill %u index %p callback %p flags %x -> slot %u\n", mod
->BaseAddress
,
780 (void *)dir
->StartAddressOfRawData
, (void *)dir
->EndAddressOfRawData
, dir
->SizeOfZeroFill
,
781 (void *)dir
->AddressOfIndex
, (void *)dir
->AddressOfCallBacks
, dir
->Characteristics
, i
);
783 if (i
== tls_module_count
)
785 UINT new_count
= max( 32, tls_module_count
* 2 );
788 new_ptr
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*tls_dirs
) );
790 new_ptr
= RtlReAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, tls_dirs
,
791 new_count
* sizeof(*tls_dirs
) );
792 if (!new_ptr
) return -1;
794 /* resize the pointer block in all running threads */
795 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
797 TEB
*teb
= CONTAINING_RECORD( entry
, TEB
, TlsLinks
);
798 void **old
= teb
->ThreadLocalStoragePointer
;
799 void **new = RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
, new_count
* sizeof(*new));
802 if (old
) memcpy( new, old
, tls_module_count
* sizeof(*new) );
803 teb
->ThreadLocalStoragePointer
= new;
804 TRACE( "thread %04lx tls block %p -> %p\n", (ULONG_PTR
)teb
->ClientId
.UniqueThread
, old
, new );
805 /* FIXME: can't free old block here, should be freed at thread exit */
809 tls_module_count
= new_count
;
812 /* allocate the data block in all running threads */
813 for (entry
= tls_links
.Flink
; entry
!= &tls_links
; entry
= entry
->Flink
)
815 TEB
*teb
= CONTAINING_RECORD( entry
, TEB
, TlsLinks
);
817 if (!(new_ptr
= RtlAllocateHeap( GetProcessHeap(), 0, size
+ dir
->SizeOfZeroFill
))) return -1;
818 memcpy( new_ptr
, (void *)dir
->StartAddressOfRawData
, size
);
819 memset( (char *)new_ptr
+ size
, 0, dir
->SizeOfZeroFill
);
821 TRACE( "thread %04lx slot %u: %u/%u bytes at %p\n",
822 (ULONG_PTR
)teb
->ClientId
.UniqueThread
, i
, size
, dir
->SizeOfZeroFill
, new_ptr
);
824 RtlFreeHeap( GetProcessHeap(), 0,
825 interlocked_xchg_ptr( (void **)teb
->ThreadLocalStoragePointer
+ i
, new_ptr
));
828 *(DWORD
*)dir
->AddressOfIndex
= i
;
834 /*************************************************************************
837 * Free the module TLS slot on unload.
838 * The loader_section must be locked while calling this function.
840 static void free_tls_slot( LDR_MODULE
*mod
)
842 ULONG i
= (USHORT
)mod
->TlsIndex
;
844 if (mod
->TlsIndex
== -1) return;
845 assert( i
< tls_module_count
);
850 /****************************************************************
853 * Fixup all imports of a given module.
854 * The loader_section must be locked while calling this function.
856 static NTSTATUS
fixup_imports( WINE_MODREF
*wm
, LPCWSTR load_path
)
859 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
865 if (!(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
)) return STATUS_SUCCESS
; /* already done */
866 wm
->ldr
.Flags
&= ~LDR_DONT_RESOLVE_REFS
;
868 wm
->ldr
.TlsIndex
= alloc_tls_slot( &wm
->ldr
);
870 if (!(imports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
871 IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
872 return STATUS_SUCCESS
;
875 while (imports
[nb_imports
].Name
&& imports
[nb_imports
].FirstThunk
) nb_imports
++;
877 if (!nb_imports
) return STATUS_SUCCESS
; /* no imports */
879 if (!create_module_activation_context( &wm
->ldr
))
880 RtlActivateActivationContext( 0, wm
->ldr
.ActivationContext
, &cookie
);
882 /* Allocate module dependency list */
883 wm
->nDeps
= nb_imports
;
884 wm
->deps
= RtlAllocateHeap( GetProcessHeap(), 0, nb_imports
*sizeof(WINE_MODREF
*) );
886 /* load the imported modules. They are automatically
887 * added to the modref list of the process.
889 prev
= current_modref
;
891 status
= STATUS_SUCCESS
;
892 for (i
= 0; i
< nb_imports
; i
++)
894 if (!(wm
->deps
[i
] = import_dll( wm
->ldr
.BaseAddress
, &imports
[i
], load_path
)))
895 status
= STATUS_DLL_NOT_FOUND
;
897 current_modref
= prev
;
898 if (wm
->ldr
.ActivationContext
) RtlDeactivateActivationContext( 0, cookie
);
903 /*************************************************************************
906 * Allocate a WINE_MODREF structure and add it to the process list
907 * The loader_section must be locked while calling this function.
909 static WINE_MODREF
*alloc_module( HMODULE hModule
, LPCWSTR filename
)
913 const IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader(hModule
);
914 PLIST_ENTRY entry
, mark
;
916 if (!(wm
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm
) ))) return NULL
;
921 wm
->ldr
.BaseAddress
= hModule
;
922 wm
->ldr
.EntryPoint
= NULL
;
923 wm
->ldr
.SizeOfImage
= nt
->OptionalHeader
.SizeOfImage
;
924 wm
->ldr
.Flags
= LDR_DONT_RESOLVE_REFS
;
925 wm
->ldr
.TlsIndex
= -1;
926 wm
->ldr
.LoadCount
= 1;
927 wm
->ldr
.SectionHandle
= NULL
;
928 wm
->ldr
.CheckSum
= 0;
929 wm
->ldr
.TimeDateStamp
= 0;
930 wm
->ldr
.ActivationContext
= 0;
932 RtlCreateUnicodeString( &wm
->ldr
.FullDllName
, filename
);
933 if ((p
= strrchrW( wm
->ldr
.FullDllName
.Buffer
, '\\' ))) p
++;
934 else p
= wm
->ldr
.FullDllName
.Buffer
;
935 RtlInitUnicodeString( &wm
->ldr
.BaseDllName
, p
);
937 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) || !is_dll_native_subsystem( hModule
, nt
, p
))
939 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
940 wm
->ldr
.Flags
|= LDR_IMAGE_IS_DLL
;
941 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
942 wm
->ldr
.EntryPoint
= (char *)hModule
+ nt
->OptionalHeader
.AddressOfEntryPoint
;
945 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
,
946 &wm
->ldr
.InLoadOrderModuleList
);
948 /* insert module in MemoryList, sorted in increasing base addresses */
949 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
950 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
952 if (CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
)->BaseAddress
> wm
->ldr
.BaseAddress
)
955 entry
->Blink
->Flink
= &wm
->ldr
.InMemoryOrderModuleList
;
956 wm
->ldr
.InMemoryOrderModuleList
.Blink
= entry
->Blink
;
957 wm
->ldr
.InMemoryOrderModuleList
.Flink
= entry
;
958 entry
->Blink
= &wm
->ldr
.InMemoryOrderModuleList
;
960 /* wait until init is called for inserting into this list */
961 wm
->ldr
.InInitializationOrderModuleList
.Flink
= NULL
;
962 wm
->ldr
.InInitializationOrderModuleList
.Blink
= NULL
;
964 if (!(nt
->OptionalHeader
.DllCharacteristics
& IMAGE_DLLCHARACTERISTICS_NX_COMPAT
))
966 ULONG flags
= MEM_EXECUTE_OPTION_ENABLE
;
967 WARN( "disabling no-exec because of %s\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
) );
968 NtSetInformationProcess( GetCurrentProcess(), ProcessExecuteFlags
, &flags
, sizeof(flags
) );
974 /*************************************************************************
977 * Allocate the per-thread structure for module TLS storage.
979 static NTSTATUS
alloc_thread_tls(void)
984 if (!tls_module_count
) return STATUS_SUCCESS
;
986 if (!(pointers
= RtlAllocateHeap( GetProcessHeap(), HEAP_ZERO_MEMORY
,
987 tls_module_count
* sizeof(*pointers
) )))
988 return STATUS_NO_MEMORY
;
990 for (i
= 0; i
< tls_module_count
; i
++)
992 const IMAGE_TLS_DIRECTORY
*dir
= tls_dirs
[i
];
995 size
= dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
;
996 if (!size
&& !dir
->SizeOfZeroFill
) continue;
998 if (!(pointers
[i
] = RtlAllocateHeap( GetProcessHeap(), 0, size
+ dir
->SizeOfZeroFill
)))
1000 while (i
) RtlFreeHeap( GetProcessHeap(), 0, pointers
[--i
] );
1001 RtlFreeHeap( GetProcessHeap(), 0, pointers
);
1002 return STATUS_NO_MEMORY
;
1004 memcpy( pointers
[i
], (void *)dir
->StartAddressOfRawData
, size
);
1005 memset( (char *)pointers
[i
] + size
, 0, dir
->SizeOfZeroFill
);
1007 TRACE( "thread %04x slot %u: %u/%u bytes at %p\n",
1008 GetCurrentThreadId(), i
, size
, dir
->SizeOfZeroFill
, pointers
[i
] );
1010 NtCurrentTeb()->ThreadLocalStoragePointer
= pointers
;
1011 return STATUS_SUCCESS
;
1015 /*************************************************************************
1016 * call_tls_callbacks
1018 static void call_tls_callbacks( HMODULE module
, UINT reason
)
1020 const IMAGE_TLS_DIRECTORY
*dir
;
1021 const PIMAGE_TLS_CALLBACK
*callback
;
1024 dir
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_TLS
, &dirsize
);
1025 if (!dir
|| !dir
->AddressOfCallBacks
) return;
1027 for (callback
= (const PIMAGE_TLS_CALLBACK
*)dir
->AddressOfCallBacks
; *callback
; callback
++)
1029 if (TRACE_ON(relay
))
1030 DPRINTF("%04x:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
1031 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
1034 call_dll_entry_point( (DLLENTRYPROC
)*callback
, module
, reason
, NULL
);
1038 if (TRACE_ON(relay
))
1039 DPRINTF("%04x:exception in TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
1040 GetCurrentThreadId(), callback
, module
, reason_names
[reason
] );
1044 if (TRACE_ON(relay
))
1045 DPRINTF("%04x:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
1046 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
1051 /*************************************************************************
1054 static NTSTATUS
MODULE_InitDLL( WINE_MODREF
*wm
, UINT reason
, LPVOID lpReserved
)
1057 NTSTATUS status
= STATUS_SUCCESS
;
1058 DLLENTRYPROC entry
= wm
->ldr
.EntryPoint
;
1059 void *module
= wm
->ldr
.BaseAddress
;
1062 /* Skip calls for modules loaded with special load flags */
1064 if (wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
) return STATUS_SUCCESS
;
1065 if (wm
->ldr
.TlsIndex
!= -1) call_tls_callbacks( wm
->ldr
.BaseAddress
, reason
);
1066 if (!entry
|| !(wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
)) return STATUS_SUCCESS
;
1068 if (TRACE_ON(relay
))
1070 size_t len
= min( wm
->ldr
.BaseDllName
.Length
, sizeof(mod_name
)-sizeof(WCHAR
) );
1071 memcpy( mod_name
, wm
->ldr
.BaseDllName
.Buffer
, len
);
1072 mod_name
[len
/ sizeof(WCHAR
)] = 0;
1073 DPRINTF("%04x:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
1074 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
1075 reason_names
[reason
], lpReserved
);
1077 else TRACE("(%p %s,%s,%p) - CALL\n", module
, debugstr_w(wm
->ldr
.BaseDllName
.Buffer
),
1078 reason_names
[reason
], lpReserved
);
1082 retv
= call_dll_entry_point( entry
, module
, reason
, lpReserved
);
1084 status
= STATUS_DLL_INIT_FAILED
;
1088 if (TRACE_ON(relay
))
1089 DPRINTF("%04x:exception in PE entry point (proc=%p,module=%p,reason=%s,res=%p)\n",
1090 GetCurrentThreadId(), entry
, module
, reason_names
[reason
], lpReserved
);
1091 status
= GetExceptionCode();
1095 /* The state of the module list may have changed due to the call
1096 to the dll. We cannot assume that this module has not been
1098 if (TRACE_ON(relay
))
1099 DPRINTF("%04x:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
1100 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
1101 reason_names
[reason
], lpReserved
, retv
);
1102 else TRACE("(%p,%s,%p) - RETURN %d\n", module
, reason_names
[reason
], lpReserved
, retv
);
1108 /*************************************************************************
1111 * Send the process attach notification to all DLLs the given module
1112 * depends on (recursively). This is somewhat complicated due to the fact that
1114 * - we have to respect the module dependencies, i.e. modules implicitly
1115 * referenced by another module have to be initialized before the module
1116 * itself can be initialized
1118 * - the initialization routine of a DLL can itself call LoadLibrary,
1119 * thereby introducing a whole new set of dependencies (even involving
1120 * the 'old' modules) at any time during the whole process
1122 * (Note that this routine can be recursively entered not only directly
1123 * from itself, but also via LoadLibrary from one of the called initialization
1126 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
1127 * the process *detach* notifications to be sent in the correct order.
1128 * This must not only take into account module dependencies, but also
1129 * 'hidden' dependencies created by modules calling LoadLibrary in their
1130 * attach notification routine.
1132 * The strategy is rather simple: we move a WINE_MODREF to the head of the
1133 * list after the attach notification has returned. This implies that the
1134 * detach notifications are called in the reverse of the sequence the attach
1135 * notifications *returned*.
1137 * The loader_section must be locked while calling this function.
1139 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
)
1141 NTSTATUS status
= STATUS_SUCCESS
;
1145 if (process_detaching
) return status
;
1147 /* prevent infinite recursion in case of cyclical dependencies */
1148 if ( ( wm
->ldr
.Flags
& LDR_LOAD_IN_PROGRESS
)
1149 || ( wm
->ldr
.Flags
& LDR_PROCESS_ATTACHED
) )
1152 TRACE("(%s,%p) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
1154 /* Tag current MODREF to prevent recursive loop */
1155 wm
->ldr
.Flags
|= LDR_LOAD_IN_PROGRESS
;
1156 if (lpReserved
) wm
->ldr
.LoadCount
= -1; /* pin it if imported by the main exe */
1157 if (wm
->ldr
.ActivationContext
) RtlActivateActivationContext( 0, wm
->ldr
.ActivationContext
, &cookie
);
1159 /* Recursively attach all DLLs this one depends on */
1160 for ( i
= 0; i
< wm
->nDeps
; i
++ )
1162 if (!wm
->deps
[i
]) continue;
1163 if ((status
= process_attach( wm
->deps
[i
], lpReserved
)) != STATUS_SUCCESS
) break;
1166 /* Call DLL entry point */
1167 if (status
== STATUS_SUCCESS
)
1169 WINE_MODREF
*prev
= current_modref
;
1170 current_modref
= wm
;
1171 status
= MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
);
1172 if (status
== STATUS_SUCCESS
)
1173 wm
->ldr
.Flags
|= LDR_PROCESS_ATTACHED
;
1176 MODULE_InitDLL( wm
, DLL_PROCESS_DETACH
, lpReserved
);
1177 /* point to the name so LdrInitializeThunk can print it */
1178 last_failed_modref
= wm
;
1179 WARN("Initialization of %s failed\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
1181 current_modref
= prev
;
1184 if (!wm
->ldr
.InInitializationOrderModuleList
.Flink
)
1185 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
,
1186 &wm
->ldr
.InInitializationOrderModuleList
);
1188 if (wm
->ldr
.ActivationContext
) RtlDeactivateActivationContext( 0, cookie
);
1189 /* Remove recursion flag */
1190 wm
->ldr
.Flags
&= ~LDR_LOAD_IN_PROGRESS
;
1192 TRACE("(%s,%p) - END\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
1197 /**********************************************************************
1198 * attach_implicitly_loaded_dlls
1200 * Attach to the (builtin) dlls that have been implicitly loaded because
1201 * of a dependency at the Unix level, but not imported at the Win32 level.
1203 static void attach_implicitly_loaded_dlls( LPVOID reserved
)
1207 PLIST_ENTRY mark
, entry
;
1209 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1210 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1212 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1214 if (mod
->Flags
& (LDR_LOAD_IN_PROGRESS
| LDR_PROCESS_ATTACHED
)) continue;
1215 TRACE( "found implicitly loaded %s, attaching to it\n",
1216 debugstr_w(mod
->BaseDllName
.Buffer
));
1217 process_attach( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
), reserved
);
1218 break; /* restart the search from the start */
1220 if (entry
== mark
) break; /* nothing found */
1225 /*************************************************************************
1228 * Send DLL process detach notifications. See the comment about calling
1229 * sequence at process_attach.
1231 static void process_detach(void)
1233 PLIST_ENTRY mark
, entry
;
1236 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1239 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
1241 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1242 InInitializationOrderModuleList
);
1243 /* Check whether to detach this DLL */
1244 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1246 if ( mod
->LoadCount
&& !process_detaching
)
1249 /* Call detach notification */
1250 mod
->Flags
&= ~LDR_PROCESS_ATTACHED
;
1251 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1252 DLL_PROCESS_DETACH
, ULongToPtr(process_detaching
) );
1254 /* Restart at head of WINE_MODREF list, as entries might have
1255 been added and/or removed while performing the call ... */
1258 } while (entry
!= mark
);
1261 /*************************************************************************
1262 * MODULE_DllThreadAttach
1264 * Send DLL thread attach notifications. These are sent in the
1265 * reverse sequence of process detach notification.
1268 NTSTATUS
MODULE_DllThreadAttach( LPVOID lpReserved
)
1270 PLIST_ENTRY mark
, entry
;
1274 /* don't do any attach calls if process is exiting */
1275 if (process_detaching
) return STATUS_SUCCESS
;
1277 RtlEnterCriticalSection( &loader_section
);
1279 RtlAcquirePebLock();
1280 InsertHeadList( &tls_links
, &NtCurrentTeb()->TlsLinks
);
1281 RtlReleasePebLock();
1283 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto done
;
1285 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1286 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1288 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1289 InInitializationOrderModuleList
);
1290 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1292 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
1295 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1296 DLL_THREAD_ATTACH
, lpReserved
);
1300 RtlLeaveCriticalSection( &loader_section
);
1304 /******************************************************************
1305 * LdrDisableThreadCalloutsForDll (NTDLL.@)
1308 NTSTATUS WINAPI
LdrDisableThreadCalloutsForDll(HMODULE hModule
)
1311 NTSTATUS ret
= STATUS_SUCCESS
;
1313 RtlEnterCriticalSection( &loader_section
);
1315 wm
= get_modref( hModule
);
1316 if (!wm
|| wm
->ldr
.TlsIndex
!= -1)
1317 ret
= STATUS_DLL_NOT_FOUND
;
1319 wm
->ldr
.Flags
|= LDR_NO_DLL_CALLS
;
1321 RtlLeaveCriticalSection( &loader_section
);
1326 /******************************************************************
1327 * LdrFindEntryForAddress (NTDLL.@)
1329 * The loader_section must be locked while calling this function
1331 NTSTATUS WINAPI
LdrFindEntryForAddress(const void* addr
, PLDR_MODULE
* pmod
)
1333 PLIST_ENTRY mark
, entry
;
1336 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
1337 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1339 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
1340 if (mod
->BaseAddress
<= addr
&&
1341 (const char *)addr
< (char*)mod
->BaseAddress
+ mod
->SizeOfImage
)
1344 return STATUS_SUCCESS
;
1346 if (mod
->BaseAddress
> addr
) break;
1348 return STATUS_NO_MORE_ENTRIES
;
1351 /******************************************************************
1352 * LdrLockLoaderLock (NTDLL.@)
1354 * Note: some flags are not implemented.
1355 * Flag 0x01 is used to raise exceptions on errors.
1357 NTSTATUS WINAPI
LdrLockLoaderLock( ULONG flags
, ULONG
*result
, ULONG_PTR
*magic
)
1359 if (flags
& ~0x2) FIXME( "flags %x not supported\n", flags
);
1361 if (result
) *result
= 0;
1362 if (magic
) *magic
= 0;
1363 if (flags
& ~0x3) return STATUS_INVALID_PARAMETER_1
;
1364 if (!result
&& (flags
& 0x2)) return STATUS_INVALID_PARAMETER_2
;
1365 if (!magic
) return STATUS_INVALID_PARAMETER_3
;
1369 if (!RtlTryEnterCriticalSection( &loader_section
))
1372 return STATUS_SUCCESS
;
1378 RtlEnterCriticalSection( &loader_section
);
1379 if (result
) *result
= 1;
1381 *magic
= GetCurrentThreadId();
1382 return STATUS_SUCCESS
;
1386 /******************************************************************
1387 * LdrUnlockLoaderUnlock (NTDLL.@)
1389 NTSTATUS WINAPI
LdrUnlockLoaderLock( ULONG flags
, ULONG_PTR magic
)
1393 if (magic
!= GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2
;
1394 RtlLeaveCriticalSection( &loader_section
);
1396 return STATUS_SUCCESS
;
1400 /******************************************************************
1401 * LdrGetProcedureAddress (NTDLL.@)
1403 NTSTATUS WINAPI
LdrGetProcedureAddress(HMODULE module
, const ANSI_STRING
*name
,
1404 ULONG ord
, PVOID
*address
)
1406 IMAGE_EXPORT_DIRECTORY
*exports
;
1408 NTSTATUS ret
= STATUS_PROCEDURE_NOT_FOUND
;
1410 RtlEnterCriticalSection( &loader_section
);
1412 /* check if the module itself is invalid to return the proper error */
1413 if (!get_modref( module
)) ret
= STATUS_DLL_NOT_FOUND
;
1414 else if ((exports
= RtlImageDirectoryEntryToData( module
, TRUE
,
1415 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
1417 LPCWSTR load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1418 void *proc
= name
? find_named_export( module
, exports
, exp_size
, name
->Buffer
, -1, load_path
)
1419 : find_ordinal_export( module
, exports
, exp_size
, ord
- exports
->Base
, load_path
);
1423 ret
= STATUS_SUCCESS
;
1427 RtlLeaveCriticalSection( &loader_section
);
1432 /***********************************************************************
1435 * Check if a loaded native dll is a Wine fake dll.
1437 static BOOL
is_fake_dll( HANDLE handle
)
1439 static const char fakedll_signature
[] = "Wine placeholder DLL";
1440 char buffer
[sizeof(IMAGE_DOS_HEADER
) + sizeof(fakedll_signature
)];
1441 const IMAGE_DOS_HEADER
*dos
= (const IMAGE_DOS_HEADER
*)buffer
;
1443 LARGE_INTEGER offset
;
1445 offset
.QuadPart
= 0;
1446 if (NtReadFile( handle
, 0, NULL
, 0, &io
, buffer
, sizeof(buffer
), &offset
, NULL
)) return FALSE
;
1447 if (io
.Information
< sizeof(buffer
)) return FALSE
;
1448 if (dos
->e_magic
!= IMAGE_DOS_SIGNATURE
) return FALSE
;
1449 if (dos
->e_lfanew
>= sizeof(*dos
) + sizeof(fakedll_signature
) &&
1450 !memcmp( dos
+ 1, fakedll_signature
, sizeof(fakedll_signature
) )) return TRUE
;
1455 /***********************************************************************
1456 * get_builtin_fullname
1458 * Build the full pathname for a builtin dll.
1460 static WCHAR
*get_builtin_fullname( const WCHAR
*path
, const char *filename
)
1462 static const WCHAR soW
[] = {'.','s','o',0};
1463 WCHAR
*p
, *fullname
;
1464 size_t i
, len
= strlen(filename
);
1466 /* check if path can correspond to the dll we have */
1467 if (path
&& (p
= strrchrW( path
, '\\' )))
1470 for (i
= 0; i
< len
; i
++)
1471 if (tolowerW(p
[i
]) != tolowerW( (WCHAR
)filename
[i
]) ) break;
1472 if (i
== len
&& (!p
[len
] || !strcmpiW( p
+ len
, soW
)))
1474 /* the filename matches, use path as the full path */
1476 if ((fullname
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1478 memcpy( fullname
, path
, len
* sizeof(WCHAR
) );
1485 if ((fullname
= RtlAllocateHeap( GetProcessHeap(), 0,
1486 system_dir
.MaximumLength
+ (len
+ 1) * sizeof(WCHAR
) )))
1488 memcpy( fullname
, system_dir
.Buffer
, system_dir
.Length
);
1489 p
= fullname
+ system_dir
.Length
/ sizeof(WCHAR
);
1490 if (p
> fullname
&& p
[-1] != '\\') *p
++ = '\\';
1491 ascii_to_unicode( p
, filename
, len
+ 1 );
1497 /*************************************************************************
1500 static BOOL
is_16bit_builtin( HMODULE module
)
1502 const IMAGE_EXPORT_DIRECTORY
*exports
;
1505 if (!(exports
= RtlImageDirectoryEntryToData( module
, TRUE
,
1506 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
1509 return find_named_export( module
, exports
, exp_size
, "__wine_spec_dos_header", -1, NULL
) != NULL
;
1513 /***********************************************************************
1514 * load_builtin_callback
1516 * Load a library in memory; callback function for wine_dll_register
1518 static void load_builtin_callback( void *module
, const char *filename
)
1520 static const WCHAR emptyW
[1];
1521 IMAGE_NT_HEADERS
*nt
;
1524 const WCHAR
*load_path
;
1528 ERR("could not map image for %s\n", filename
? filename
: "main exe" );
1531 if (!(nt
= RtlImageNtHeader( module
)))
1533 ERR( "bad module for %s\n", filename
? filename
: "main exe" );
1534 builtin_load_info
->status
= STATUS_INVALID_IMAGE_FORMAT
;
1538 virtual_create_builtin_view( module
);
1540 /* create the MODREF */
1542 if (!(fullname
= get_builtin_fullname( builtin_load_info
->filename
, filename
)))
1544 ERR( "can't load %s\n", filename
);
1545 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1549 wm
= alloc_module( module
, fullname
);
1550 RtlFreeHeap( GetProcessHeap(), 0, fullname
);
1553 ERR( "can't load %s\n", filename
);
1554 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1557 wm
->ldr
.Flags
|= LDR_WINE_INTERNAL
;
1559 if ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1560 nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_NATIVE
||
1561 is_16bit_builtin( module
))
1565 load_path
= builtin_load_info
->load_path
;
1566 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1567 if (!load_path
) load_path
= emptyW
;
1568 if (fixup_imports( wm
, load_path
) != STATUS_SUCCESS
)
1570 /* the module has only be inserted in the load & memory order lists */
1571 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1572 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1573 /* FIXME: free the modref */
1574 builtin_load_info
->status
= STATUS_DLL_NOT_FOUND
;
1579 builtin_load_info
->wm
= wm
;
1580 TRACE( "loaded %s %p %p\n", filename
, wm
, module
);
1582 /* send the DLL load event */
1584 SERVER_START_REQ( load_dll
)
1587 req
->base
= wine_server_client_ptr( module
);
1588 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1589 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1590 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1591 req
->name
= wine_server_client_ptr( &wm
->ldr
.FullDllName
.Buffer
);
1592 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1593 wine_server_call( req
);
1597 /* setup relay debugging entry points */
1598 if (TRACE_ON(relay
)) RELAY_SetupDLL( module
);
1602 /******************************************************************************
1603 * load_native_dll (internal)
1605 static NTSTATUS
load_native_dll( LPCWSTR load_path
, LPCWSTR name
, HANDLE file
,
1606 DWORD flags
, WINE_MODREF
** pwm
)
1611 IMAGE_NT_HEADERS
*nt
;
1616 TRACE("Trying native dll %s\n", debugstr_w(name
));
1619 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1620 NULL
, &size
, PAGE_EXECUTE_READ
, SEC_IMAGE
, file
);
1621 if (status
!= STATUS_SUCCESS
) return status
;
1624 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(),
1625 &module
, 0, 0, &size
, &len
, ViewShare
, 0, PAGE_EXECUTE_READ
);
1626 if (status
< 0) goto done
;
1628 /* create the MODREF */
1630 if (!(wm
= alloc_module( module
, name
)))
1632 status
= STATUS_NO_MEMORY
;
1638 nt
= RtlImageNtHeader( module
);
1640 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
) &&
1641 ((nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) ||
1642 nt
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_NATIVE
))
1644 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
)
1646 /* the module has only be inserted in the load & memory order lists */
1647 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1648 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1650 /* FIXME: there are several more dangling references
1651 * left. Including dlls loaded by this dll before the
1652 * failed one. Unrolling is rather difficult with the
1653 * current structure and we can leave them lying
1654 * around with no problems, so we don't care.
1655 * As these might reference our wm, we don't free it.
1661 /* send DLL load event */
1663 SERVER_START_REQ( load_dll
)
1665 req
->mapping
= wine_server_obj_handle( mapping
);
1666 req
->base
= wine_server_client_ptr( module
);
1667 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1668 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1669 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1670 req
->name
= wine_server_client_ptr( &wm
->ldr
.FullDllName
.Buffer
);
1671 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1672 wine_server_call( req
);
1676 if ((wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
) && TRACE_ON(snoop
)) SNOOP_SetupDLL( module
);
1678 TRACE_(loaddll
)( "Loaded %s at %p: native\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
), module
);
1680 wm
->ldr
.LoadCount
= 1;
1682 status
= STATUS_SUCCESS
;
1689 /***********************************************************************
1692 static NTSTATUS
load_builtin_dll( LPCWSTR load_path
, LPCWSTR path
, HANDLE file
,
1693 DWORD flags
, WINE_MODREF
** pwm
)
1695 char error
[256], dllname
[MAX_PATH
];
1696 const WCHAR
*name
, *p
;
1698 void *handle
= NULL
;
1699 struct builtin_load_info info
, *prev_info
;
1701 /* Fix the name in case we have a full path and extension */
1703 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
1704 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
1706 /* load_library will modify info.status. Note also that load_library can be
1707 * called several times, if the .so file we're loading has dependencies.
1708 * info.status will gather all the errors we may get while loading all these
1711 info
.load_path
= load_path
;
1712 info
.filename
= NULL
;
1713 info
.status
= STATUS_SUCCESS
;
1716 if (file
) /* we have a real file, try to load it */
1718 UNICODE_STRING nt_name
;
1719 ANSI_STRING unix_name
;
1721 TRACE("Trying built-in %s\n", debugstr_w(path
));
1723 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1724 return STATUS_DLL_NOT_FOUND
;
1726 if (wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
))
1728 RtlFreeUnicodeString( &nt_name
);
1729 return STATUS_DLL_NOT_FOUND
;
1731 prev_info
= builtin_load_info
;
1732 info
.filename
= nt_name
.Buffer
+ 4; /* skip \??\ */
1733 builtin_load_info
= &info
;
1734 handle
= wine_dlopen( unix_name
.Buffer
, RTLD_NOW
, error
, sizeof(error
) );
1735 builtin_load_info
= prev_info
;
1736 RtlFreeUnicodeString( &nt_name
);
1737 RtlFreeHeap( GetProcessHeap(), 0, unix_name
.Buffer
);
1740 WARN( "failed to load .so lib for builtin %s: %s\n", debugstr_w(path
), error
);
1741 return STATUS_INVALID_IMAGE_FORMAT
;
1748 TRACE("Trying built-in %s\n", debugstr_w(name
));
1750 /* we don't want to depend on the current codepage here */
1751 len
= strlenW( name
) + 1;
1752 if (len
>= sizeof(dllname
)) return STATUS_NAME_TOO_LONG
;
1753 for (i
= 0; i
< len
; i
++)
1755 if (name
[i
] > 127) return STATUS_DLL_NOT_FOUND
;
1756 dllname
[i
] = (char)name
[i
];
1757 if (dllname
[i
] >= 'A' && dllname
[i
] <= 'Z') dllname
[i
] += 'a' - 'A';
1760 prev_info
= builtin_load_info
;
1761 builtin_load_info
= &info
;
1762 handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
);
1763 builtin_load_info
= prev_info
;
1768 /* The file does not exist -> WARN() */
1769 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1770 return STATUS_DLL_NOT_FOUND
;
1772 /* ERR() for all other errors (missing functions, ...) */
1773 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1774 return STATUS_PROCEDURE_NOT_FOUND
;
1778 if (info
.status
!= STATUS_SUCCESS
)
1780 wine_dll_unload( handle
);
1786 PLIST_ENTRY mark
, entry
;
1788 /* The constructor wasn't called, this means the .so is already
1789 * loaded under a different name. Try to find the wm for it. */
1791 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1792 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1794 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1795 if (mod
->Flags
& LDR_WINE_INTERNAL
&& mod
->SectionHandle
== handle
)
1797 info
.wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
1798 TRACE( "Found %s at %p for builtin %s\n",
1799 debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
, debugstr_w(path
) );
1803 wine_dll_unload( handle
); /* release the libdl refcount */
1804 if (!info
.wm
) return STATUS_INVALID_IMAGE_FORMAT
;
1805 if (info
.wm
->ldr
.LoadCount
!= -1) info
.wm
->ldr
.LoadCount
++;
1809 TRACE_(loaddll
)( "Loaded %s at %p: builtin\n", debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
);
1810 info
.wm
->ldr
.LoadCount
= 1;
1811 info
.wm
->ldr
.SectionHandle
= handle
;
1815 return STATUS_SUCCESS
;
1819 /***********************************************************************
1822 * Find the full path (if any) of the dll from the activation context.
1824 static NTSTATUS
find_actctx_dll( LPCWSTR libname
, LPWSTR
*fullname
)
1826 static const WCHAR winsxsW
[] = {'\\','w','i','n','s','x','s','\\'};
1827 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
1829 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
1830 ACTCTX_SECTION_KEYED_DATA data
;
1831 UNICODE_STRING nameW
;
1833 SIZE_T needed
, size
= 1024;
1836 RtlInitUnicodeString( &nameW
, libname
);
1837 data
.cbSize
= sizeof(data
);
1838 status
= RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
1839 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
1841 if (status
!= STATUS_SUCCESS
) return status
;
1845 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1847 status
= STATUS_NO_MEMORY
;
1850 status
= RtlQueryInformationActivationContext( 0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
1851 AssemblyDetailedInformationInActivationContext
,
1852 info
, size
, &needed
);
1853 if (status
== STATUS_SUCCESS
) break;
1854 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
1855 RtlFreeHeap( GetProcessHeap(), 0, info
);
1857 /* restart with larger buffer */
1860 if (!info
->lpAssemblyManifestPath
|| !info
->lpAssemblyDirectoryName
)
1862 status
= STATUS_SXS_KEY_NOT_FOUND
;
1866 if ((p
= strrchrW( info
->lpAssemblyManifestPath
, '\\' )))
1868 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1871 if (strncmpiW( p
, info
->lpAssemblyDirectoryName
, dirlen
) || strcmpiW( p
+ dirlen
, dotManifestW
))
1873 /* manifest name does not match directory name, so it's not a global
1874 * windows/winsxs manifest; use the manifest directory name instead */
1875 dirlen
= p
- info
->lpAssemblyManifestPath
;
1876 needed
= (dirlen
+ 1) * sizeof(WCHAR
) + nameW
.Length
;
1877 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1879 status
= STATUS_NO_MEMORY
;
1882 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
1884 strcpyW( p
, libname
);
1889 needed
= (strlenW(user_shared_data
->NtSystemRoot
) * sizeof(WCHAR
) +
1890 sizeof(winsxsW
) + info
->ulAssemblyDirectoryNameLength
+ nameW
.Length
+ 2*sizeof(WCHAR
));
1892 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1894 status
= STATUS_NO_MEMORY
;
1897 strcpyW( p
, user_shared_data
->NtSystemRoot
);
1899 memcpy( p
, winsxsW
, sizeof(winsxsW
) );
1900 p
+= sizeof(winsxsW
) / sizeof(WCHAR
);
1901 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
1902 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1904 strcpyW( p
, libname
);
1906 RtlFreeHeap( GetProcessHeap(), 0, info
);
1907 RtlReleaseActivationContext( data
.hActCtx
);
1912 /***********************************************************************
1915 * Find the file (or already loaded module) for a given dll name.
1917 static NTSTATUS
find_dll_file( const WCHAR
*load_path
, const WCHAR
*libname
,
1918 WCHAR
*filename
, ULONG
*size
, WINE_MODREF
**pwm
, HANDLE
*handle
)
1920 OBJECT_ATTRIBUTES attr
;
1922 UNICODE_STRING nt_name
;
1923 WCHAR
*file_part
, *ext
, *dllname
;
1926 /* first append .dll if needed */
1929 if (!(ext
= strrchrW( libname
, '.')) || strchrW( ext
, '/' ) || strchrW( ext
, '\\'))
1931 if (!(dllname
= RtlAllocateHeap( GetProcessHeap(), 0,
1932 (strlenW(libname
) * sizeof(WCHAR
)) + sizeof(dllW
) )))
1933 return STATUS_NO_MEMORY
;
1934 strcpyW( dllname
, libname
);
1935 strcatW( dllname
, dllW
);
1939 nt_name
.Buffer
= NULL
;
1941 if (!contains_path( libname
))
1944 WCHAR
*fullname
= NULL
;
1946 if ((*pwm
= find_basename_module( libname
)) != NULL
) goto found
;
1948 status
= find_actctx_dll( libname
, &fullname
);
1949 if (status
== STATUS_SUCCESS
)
1951 TRACE ("found %s for %s\n", debugstr_w(fullname
), debugstr_w(libname
) );
1952 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1953 libname
= dllname
= fullname
;
1955 else if (status
!= STATUS_SXS_KEY_NOT_FOUND
)
1957 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1962 if (RtlDetermineDosPathNameType_U( libname
) == RELATIVE_PATH
)
1964 /* we need to search for it */
1965 len
= RtlDosSearchPath_U( load_path
, libname
, NULL
, *size
, filename
, &file_part
);
1968 if (len
>= *size
) goto overflow
;
1969 if ((*pwm
= find_fullname_module( filename
)) || !handle
) goto found
;
1971 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, NULL
, NULL
))
1973 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1974 return STATUS_NO_MEMORY
;
1976 attr
.Length
= sizeof(attr
);
1977 attr
.RootDirectory
= 0;
1978 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1979 attr
.ObjectName
= &nt_name
;
1980 attr
.SecurityDescriptor
= NULL
;
1981 attr
.SecurityQualityOfService
= NULL
;
1982 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
)) *handle
= 0;
1988 if (!contains_path( libname
))
1990 /* if libname doesn't contain a path at all, we simply return the name as is,
1991 * to be loaded as builtin */
1992 len
= strlenW(libname
) * sizeof(WCHAR
);
1993 if (len
>= *size
) goto overflow
;
1994 strcpyW( filename
, libname
);
1999 /* absolute path name, or relative path name but not found above */
2001 if (!RtlDosPathNameToNtPathName_U( libname
, &nt_name
, &file_part
, NULL
))
2003 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
2004 return STATUS_NO_MEMORY
;
2006 len
= nt_name
.Length
- 4*sizeof(WCHAR
); /* for \??\ prefix */
2007 if (len
>= *size
) goto overflow
;
2008 memcpy( filename
, nt_name
.Buffer
+ 4, len
+ sizeof(WCHAR
) );
2009 if (!(*pwm
= find_fullname_module( filename
)) && handle
)
2011 attr
.Length
= sizeof(attr
);
2012 attr
.RootDirectory
= 0;
2013 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2014 attr
.ObjectName
= &nt_name
;
2015 attr
.SecurityDescriptor
= NULL
;
2016 attr
.SecurityQualityOfService
= NULL
;
2017 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, FILE_SYNCHRONOUS_IO_NONALERT
|FILE_NON_DIRECTORY_FILE
)) *handle
= 0;
2020 RtlFreeUnicodeString( &nt_name
);
2021 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
2022 return STATUS_SUCCESS
;
2025 RtlFreeUnicodeString( &nt_name
);
2026 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
2027 *size
= len
+ sizeof(WCHAR
);
2028 return STATUS_BUFFER_TOO_SMALL
;
2032 /***********************************************************************
2033 * load_dll (internal)
2035 * Load a PE style module according to the load order.
2036 * The loader_section must be locked while calling this function.
2038 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
)
2040 enum loadorder loadorder
;
2044 WINE_MODREF
*main_exe
;
2048 TRACE( "looking for %s in %s\n", debugstr_w(libname
), debugstr_w(load_path
) );
2052 size
= sizeof(buffer
);
2055 nts
= find_dll_file( load_path
, libname
, filename
, &size
, pwm
, &handle
);
2056 if (nts
== STATUS_SUCCESS
) break;
2057 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2058 if (nts
!= STATUS_BUFFER_TOO_SMALL
) return nts
;
2059 /* grow the buffer and retry */
2060 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
))) return STATUS_NO_MEMORY
;
2063 if (*pwm
) /* found already loaded module */
2065 if ((*pwm
)->ldr
.LoadCount
!= -1) (*pwm
)->ldr
.LoadCount
++;
2067 TRACE("Found %s for %s at %p, count=%d\n",
2068 debugstr_w((*pwm
)->ldr
.FullDllName
.Buffer
), debugstr_w(libname
),
2069 (*pwm
)->ldr
.BaseAddress
, (*pwm
)->ldr
.LoadCount
);
2070 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2071 return STATUS_SUCCESS
;
2074 main_exe
= get_modref( NtCurrentTeb()->Peb
->ImageBaseAddress
);
2075 loadorder
= get_load_order( main_exe
? main_exe
->ldr
.BaseDllName
.Buffer
: NULL
, filename
);
2077 if (handle
&& is_fake_dll( handle
))
2079 TRACE( "%s is a fake Wine dll\n", debugstr_w(filename
) );
2087 nts
= STATUS_NO_MEMORY
;
2090 nts
= STATUS_DLL_NOT_FOUND
;
2093 case LO_NATIVE_BUILTIN
:
2094 if (!handle
) nts
= STATUS_DLL_NOT_FOUND
;
2097 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
2098 if (nts
== STATUS_INVALID_IMAGE_NOT_MZ
)
2099 /* not in PE format, maybe it's a builtin */
2100 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
2102 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
== LO_NATIVE_BUILTIN
)
2103 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
2106 case LO_BUILTIN_NATIVE
:
2107 case LO_DEFAULT
: /* default is builtin,native */
2108 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
2109 if (!handle
) break; /* nothing else we can try */
2110 /* file is not a builtin library, try without using the specified file */
2111 if (nts
!= STATUS_SUCCESS
)
2112 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
2113 if (nts
== STATUS_SUCCESS
&& loadorder
== LO_DEFAULT
&&
2114 (MODULE_InitDLL( *pwm
, DLL_WINE_PREATTACH
, NULL
) != STATUS_SUCCESS
))
2116 /* stub-only dll, try native */
2117 TRACE( "%s pre-attach returned FALSE, preferring native\n", debugstr_w(filename
) );
2118 LdrUnloadDll( (*pwm
)->ldr
.BaseAddress
);
2119 nts
= STATUS_DLL_NOT_FOUND
;
2121 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
!= LO_BUILTIN
)
2122 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
2126 if (nts
== STATUS_SUCCESS
)
2128 /* Initialize DLL just loaded */
2129 TRACE("Loaded module %s (%s) at %p\n", debugstr_w(filename
),
2130 ((*pwm
)->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native",
2131 (*pwm
)->ldr
.BaseAddress
);
2132 if (handle
) NtClose( handle
);
2133 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2137 WARN("Failed to load module %s; status=%x\n", debugstr_w(libname
), nts
);
2138 if (handle
) NtClose( handle
);
2139 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2143 /******************************************************************
2144 * LdrLoadDll (NTDLL.@)
2146 NTSTATUS WINAPI DECLSPEC_HOTPATCH
LdrLoadDll(LPCWSTR path_name
, DWORD flags
,
2147 const UNICODE_STRING
*libname
, HMODULE
* hModule
)
2152 RtlEnterCriticalSection( &loader_section
);
2154 if (!path_name
) path_name
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2155 nts
= load_dll( path_name
, libname
->Buffer
, flags
, &wm
);
2157 if (nts
== STATUS_SUCCESS
&& !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
2159 nts
= process_attach( wm
, NULL
);
2160 if (nts
!= STATUS_SUCCESS
)
2162 LdrUnloadDll(wm
->ldr
.BaseAddress
);
2166 *hModule
= (wm
) ? wm
->ldr
.BaseAddress
: NULL
;
2168 RtlLeaveCriticalSection( &loader_section
);
2173 /******************************************************************
2174 * LdrGetDllHandle (NTDLL.@)
2176 NTSTATUS WINAPI
LdrGetDllHandle( LPCWSTR load_path
, ULONG flags
, const UNICODE_STRING
*name
, HMODULE
*base
)
2184 RtlEnterCriticalSection( &loader_section
);
2186 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2189 size
= sizeof(buffer
);
2192 status
= find_dll_file( load_path
, name
->Buffer
, filename
, &size
, &wm
, NULL
);
2193 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2194 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
2195 /* grow the buffer and retry */
2196 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
2198 status
= STATUS_NO_MEMORY
;
2203 if (status
== STATUS_SUCCESS
)
2205 if (wm
) *base
= wm
->ldr
.BaseAddress
;
2206 else status
= STATUS_DLL_NOT_FOUND
;
2209 RtlLeaveCriticalSection( &loader_section
);
2210 TRACE( "%s -> %p (load path %s)\n", debugstr_us(name
), status
? NULL
: *base
, debugstr_w(load_path
) );
2215 /******************************************************************
2216 * LdrAddRefDll (NTDLL.@)
2218 NTSTATUS WINAPI
LdrAddRefDll( ULONG flags
, HMODULE module
)
2220 NTSTATUS ret
= STATUS_SUCCESS
;
2223 if (flags
& ~LDR_ADDREF_DLL_PIN
) FIXME( "%p flags %x not implemented\n", module
, flags
);
2225 RtlEnterCriticalSection( &loader_section
);
2227 if ((wm
= get_modref( module
)))
2229 if (flags
& LDR_ADDREF_DLL_PIN
)
2230 wm
->ldr
.LoadCount
= -1;
2232 if (wm
->ldr
.LoadCount
!= -1) wm
->ldr
.LoadCount
++;
2233 TRACE( "(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2235 else ret
= STATUS_INVALID_PARAMETER
;
2237 RtlLeaveCriticalSection( &loader_section
);
2242 /***********************************************************************
2243 * LdrProcessRelocationBlock (NTDLL.@)
2245 * Apply relocations to a given page of a mapped PE image.
2247 IMAGE_BASE_RELOCATION
* WINAPI
LdrProcessRelocationBlock( void *page
, UINT count
,
2248 USHORT
*relocs
, INT_PTR delta
)
2252 USHORT offset
= *relocs
& 0xfff;
2253 int type
= *relocs
>> 12;
2256 case IMAGE_REL_BASED_ABSOLUTE
:
2258 case IMAGE_REL_BASED_HIGH
:
2259 *(short *)((char *)page
+ offset
) += HIWORD(delta
);
2261 case IMAGE_REL_BASED_LOW
:
2262 *(short *)((char *)page
+ offset
) += LOWORD(delta
);
2264 case IMAGE_REL_BASED_HIGHLOW
:
2265 *(int *)((char *)page
+ offset
) += delta
;
2268 case IMAGE_REL_BASED_DIR64
:
2269 *(INT_PTR
*)((char *)page
+ offset
) += delta
;
2271 #elif defined(__arm__)
2272 case IMAGE_REL_BASED_THUMB_MOV32
:
2274 DWORD inst
= *(INT_PTR
*)((char *)page
+ offset
);
2275 DWORD imm16
= ((inst
<< 1) & 0x0800) + ((inst
<< 12) & 0xf000) +
2276 ((inst
>> 20) & 0x0700) + ((inst
>> 16) & 0x00ff);
2279 if ((inst
& 0x8000fbf0) != 0x0000f240)
2280 ERR("wrong Thumb2 instruction %08x, expected MOVW\n", inst
);
2282 imm16
+= LOWORD(delta
);
2283 hi_delta
= HIWORD(delta
) + HIWORD(imm16
);
2284 *(INT_PTR
*)((char *)page
+ offset
) = (inst
& 0x8f00fbf0) + ((imm16
>> 1) & 0x0400) +
2285 ((imm16
>> 12) & 0x000f) +
2286 ((imm16
<< 20) & 0x70000000) +
2287 ((imm16
<< 16) & 0xff0000);
2291 inst
= *(INT_PTR
*)((char *)page
+ offset
+ 4);
2292 imm16
= ((inst
<< 1) & 0x0800) + ((inst
<< 12) & 0xf000) +
2293 ((inst
>> 20) & 0x0700) + ((inst
>> 16) & 0x00ff);
2295 if ((inst
& 0x8000fbf0) != 0x0000f2c0)
2296 ERR("wrong Thumb2 instruction %08x, expected MOVT\n", inst
);
2300 ERR("resulting immediate value won't fit: %08x\n", imm16
);
2301 *(INT_PTR
*)((char *)page
+ offset
+ 4) = (inst
& 0x8f00fbf0) +
2302 ((imm16
>> 1) & 0x0400) +
2303 ((imm16
>> 12) & 0x000f) +
2304 ((imm16
<< 20) & 0x70000000) +
2305 ((imm16
<< 16) & 0xff0000);
2311 FIXME("Unknown/unsupported fixup type %x.\n", type
);
2316 return (IMAGE_BASE_RELOCATION
*)relocs
; /* return address of next block */
2320 /******************************************************************
2321 * LdrQueryProcessModuleInformation
2324 NTSTATUS WINAPI
LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi
,
2325 ULONG buf_size
, ULONG
* req_size
)
2327 SYSTEM_MODULE
* sm
= &smi
->Modules
[0];
2328 ULONG size
= sizeof(ULONG
);
2329 NTSTATUS nts
= STATUS_SUCCESS
;
2332 PLIST_ENTRY mark
, entry
;
2336 smi
->ModulesCount
= 0;
2338 RtlEnterCriticalSection( &loader_section
);
2339 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2340 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2342 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2343 size
+= sizeof(*sm
);
2344 if (size
<= buf_size
)
2346 sm
->Reserved1
= 0; /* FIXME */
2347 sm
->Reserved2
= 0; /* FIXME */
2348 sm
->ImageBaseAddress
= mod
->BaseAddress
;
2349 sm
->ImageSize
= mod
->SizeOfImage
;
2350 sm
->Flags
= mod
->Flags
;
2352 sm
->Rank
= 0; /* FIXME */
2353 sm
->Unknown
= 0; /* FIXME */
2355 str
.MaximumLength
= MAXIMUM_FILENAME_LENGTH
;
2356 str
.Buffer
= (char*)sm
->Name
;
2357 RtlUnicodeStringToAnsiString(&str
, &mod
->FullDllName
, FALSE
);
2358 ptr
= strrchr(str
.Buffer
, '\\');
2359 sm
->NameOffset
= (ptr
!= NULL
) ? (ptr
- str
.Buffer
+ 1) : 0;
2361 smi
->ModulesCount
++;
2364 else nts
= STATUS_INFO_LENGTH_MISMATCH
;
2366 RtlLeaveCriticalSection( &loader_section
);
2368 if (req_size
) *req_size
= size
;
2374 static NTSTATUS
query_dword_option( HANDLE hkey
, LPCWSTR name
, ULONG
*value
)
2380 KEY_VALUE_PARTIAL_INFORMATION
*info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
2382 RtlInitUnicodeString( &str
, name
);
2384 size
= sizeof(buffer
) - sizeof(WCHAR
);
2385 if ((status
= NtQueryValueKey( hkey
, &str
, KeyValuePartialInformation
, buffer
, size
, &size
)))
2388 if (info
->Type
!= REG_DWORD
)
2390 buffer
[size
/ sizeof(WCHAR
)] = 0;
2391 *value
= strtoulW( (WCHAR
*)info
->Data
, 0, 16 );
2393 else memcpy( value
, info
->Data
, sizeof(*value
) );
2397 static NTSTATUS
query_string_option( HANDLE hkey
, LPCWSTR name
, ULONG type
,
2398 void *data
, ULONG in_size
, ULONG
*out_size
)
2404 KEY_VALUE_PARTIAL_INFORMATION
*info
;
2405 static const int info_size
= FIELD_OFFSET( KEY_VALUE_PARTIAL_INFORMATION
, Data
);
2407 RtlInitUnicodeString( &str
, name
);
2409 size
= info_size
+ in_size
;
2410 if (!(buffer
= RtlAllocateHeap( GetProcessHeap(), 0, size
))) return STATUS_NO_MEMORY
;
2411 info
= (KEY_VALUE_PARTIAL_INFORMATION
*)buffer
;
2412 status
= NtQueryValueKey( hkey
, &str
, KeyValuePartialInformation
, buffer
, size
, &size
);
2413 if (!status
|| status
== STATUS_BUFFER_OVERFLOW
)
2415 if (out_size
) *out_size
= info
->DataLength
;
2416 if (data
&& !status
) memcpy( data
, info
->Data
, info
->DataLength
);
2418 RtlFreeHeap( GetProcessHeap(), 0, buffer
);
2423 /******************************************************************
2424 * LdrQueryImageFileExecutionOptions (NTDLL.@)
2426 NTSTATUS WINAPI
LdrQueryImageFileExecutionOptions( const UNICODE_STRING
*key
, LPCWSTR value
, ULONG type
,
2427 void *data
, ULONG in_size
, ULONG
*out_size
)
2429 static const WCHAR optionsW
[] = {'M','a','c','h','i','n','e','\\',
2430 'S','o','f','t','w','a','r','e','\\',
2431 'M','i','c','r','o','s','o','f','t','\\',
2432 'W','i','n','d','o','w','s',' ','N','T','\\',
2433 'C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2434 'I','m','a','g','e',' ','F','i','l','e',' ',
2435 'E','x','e','c','u','t','i','o','n',' ','O','p','t','i','o','n','s','\\'};
2436 WCHAR path
[MAX_PATH
+ sizeof(optionsW
)/sizeof(WCHAR
)];
2437 OBJECT_ATTRIBUTES attr
;
2438 UNICODE_STRING name_str
;
2444 attr
.Length
= sizeof(attr
);
2445 attr
.RootDirectory
= 0;
2446 attr
.ObjectName
= &name_str
;
2447 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2448 attr
.SecurityDescriptor
= NULL
;
2449 attr
.SecurityQualityOfService
= NULL
;
2451 if ((p
= memrchrW( key
->Buffer
, '\\', key
->Length
/ sizeof(WCHAR
) ))) p
++;
2452 else p
= key
->Buffer
;
2453 len
= key
->Length
- (p
- key
->Buffer
) * sizeof(WCHAR
);
2454 name_str
.Buffer
= path
;
2455 name_str
.Length
= sizeof(optionsW
) + len
;
2456 name_str
.MaximumLength
= name_str
.Length
;
2457 memcpy( path
, optionsW
, sizeof(optionsW
) );
2458 memcpy( path
+ sizeof(optionsW
)/sizeof(WCHAR
), p
, len
);
2459 if ((status
= NtOpenKey( &hkey
, KEY_QUERY_VALUE
, &attr
))) return status
;
2461 if (type
== REG_DWORD
)
2463 if (out_size
) *out_size
= sizeof(ULONG
);
2464 if (in_size
>= sizeof(ULONG
)) status
= query_dword_option( hkey
, value
, data
);
2465 else status
= STATUS_BUFFER_OVERFLOW
;
2467 else status
= query_string_option( hkey
, value
, type
, data
, in_size
, out_size
);
2474 /******************************************************************
2475 * RtlDllShutdownInProgress (NTDLL.@)
2477 BOOLEAN WINAPI
RtlDllShutdownInProgress(void)
2479 return process_detaching
;
2482 /****************************************************************************
2483 * LdrResolveDelayLoadedAPI (NTDLL.@)
2485 void* WINAPI
LdrResolveDelayLoadedAPI( void* base
, const IMAGE_DELAYLOAD_DESCRIPTOR
* desc
,
2486 PDELAYLOAD_FAILURE_DLL_CALLBACK dllhook
, void* syshook
,
2487 IMAGE_THUNK_DATA
* addr
, ULONG flags
)
2489 IMAGE_THUNK_DATA
*pIAT
, *pINT
;
2490 DELAYLOAD_INFO delayinfo
;
2498 FIXME("(%p, %p, %p, %p, %p, 0x%08x), partial stub\n", base
, desc
, dllhook
, syshook
, addr
, flags
);
2500 phmod
= get_rva(base
, desc
->ModuleHandleRVA
);
2501 pIAT
= get_rva(base
, desc
->ImportAddressTableRVA
);
2502 pINT
= get_rva(base
, desc
->ImportNameTableRVA
);
2503 name
= get_rva(base
, desc
->DllNameRVA
);
2508 if (!RtlCreateUnicodeStringFromAsciiz(&mod
, name
))
2510 nts
= STATUS_NO_MEMORY
;
2513 nts
= LdrLoadDll(NULL
, 0, &mod
, phmod
);
2514 RtlFreeUnicodeString(&mod
);
2518 if (IMAGE_SNAP_BY_ORDINAL(pINT
[id
].u1
.Ordinal
))
2519 nts
= LdrGetProcedureAddress(*phmod
, NULL
, LOWORD(pINT
[id
].u1
.Ordinal
), (void**)&fp
);
2522 const IMAGE_IMPORT_BY_NAME
* iibn
= get_rva(base
, pINT
[id
].u1
.AddressOfData
);
2525 RtlInitAnsiString(&fnc
, (char*)iibn
->Name
);
2526 nts
= LdrGetProcedureAddress(*phmod
, &fnc
, 0, (void**)&fp
);
2530 pIAT
[id
].u1
.Function
= (ULONG_PTR
)fp
;
2535 delayinfo
.Size
= sizeof(delayinfo
);
2536 delayinfo
.DelayloadDescriptor
= desc
;
2537 delayinfo
.ThunkAddress
= addr
;
2538 delayinfo
.TargetDllName
= name
;
2539 delayinfo
.TargetApiDescriptor
.ImportDescribedByName
= !IMAGE_SNAP_BY_ORDINAL(pINT
[id
].u1
.Ordinal
);
2540 delayinfo
.TargetApiDescriptor
.Description
.Ordinal
= LOWORD(pINT
[id
].u1
.Ordinal
);
2541 delayinfo
.TargetModuleBase
= *phmod
;
2542 delayinfo
.Unused
= NULL
;
2543 delayinfo
.LastError
= nts
;
2544 return dllhook(4, &delayinfo
);
2547 /******************************************************************
2548 * LdrShutdownProcess (NTDLL.@)
2551 void WINAPI
LdrShutdownProcess(void)
2554 process_detaching
= TRUE
;
2559 /******************************************************************
2560 * RtlExitUserProcess (NTDLL.@)
2562 void WINAPI
RtlExitUserProcess( DWORD status
)
2564 RtlEnterCriticalSection( &loader_section
);
2565 RtlAcquirePebLock();
2566 NtTerminateProcess( 0, status
);
2567 LdrShutdownProcess();
2568 NtTerminateProcess( GetCurrentProcess(), status
);
2572 /******************************************************************
2573 * LdrShutdownThread (NTDLL.@)
2576 void WINAPI
LdrShutdownThread(void)
2578 PLIST_ENTRY mark
, entry
;
2585 /* don't do any detach calls if process is exiting */
2586 if (process_detaching
) return;
2588 RtlEnterCriticalSection( &loader_section
);
2590 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2591 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
2593 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
2594 InInitializationOrderModuleList
);
2595 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
2597 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
2600 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
2601 DLL_THREAD_DETACH
, NULL
);
2604 RtlAcquirePebLock();
2605 RemoveEntryList( &NtCurrentTeb()->TlsLinks
);
2606 RtlReleasePebLock();
2608 if ((pointers
= NtCurrentTeb()->ThreadLocalStoragePointer
))
2610 for (i
= 0; i
< tls_module_count
; i
++) RtlFreeHeap( GetProcessHeap(), 0, pointers
[i
] );
2611 RtlFreeHeap( GetProcessHeap(), 0, pointers
);
2613 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->FlsSlots
);
2614 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->TlsExpansionSlots
);
2615 RtlLeaveCriticalSection( &loader_section
);
2619 /***********************************************************************
2623 static void free_modref( WINE_MODREF
*wm
)
2625 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
2626 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
2627 if (wm
->ldr
.InInitializationOrderModuleList
.Flink
)
2628 RemoveEntryList(&wm
->ldr
.InInitializationOrderModuleList
);
2630 TRACE(" unloading %s\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
));
2631 if (!TRACE_ON(module
))
2632 TRACE_(loaddll
)("Unloaded module %s : %s\n",
2633 debugstr_w(wm
->ldr
.FullDllName
.Buffer
),
2634 (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native" );
2636 SERVER_START_REQ( unload_dll
)
2638 req
->base
= wine_server_client_ptr( wm
->ldr
.BaseAddress
);
2639 wine_server_call( req
);
2643 free_tls_slot( &wm
->ldr
);
2644 RtlReleaseActivationContext( wm
->ldr
.ActivationContext
);
2645 if (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) wine_dll_unload( wm
->ldr
.SectionHandle
);
2646 NtUnmapViewOfSection( NtCurrentProcess(), wm
->ldr
.BaseAddress
);
2647 if (cached_modref
== wm
) cached_modref
= NULL
;
2648 RtlFreeUnicodeString( &wm
->ldr
.FullDllName
);
2649 RtlFreeHeap( GetProcessHeap(), 0, wm
->deps
);
2650 RtlFreeHeap( GetProcessHeap(), 0, wm
);
2653 /***********************************************************************
2654 * MODULE_FlushModrefs
2656 * Remove all unused modrefs and call the internal unloading routines
2657 * for the library type.
2659 * The loader_section must be locked while calling this function.
2661 static void MODULE_FlushModrefs(void)
2663 PLIST_ENTRY mark
, entry
, prev
;
2667 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2668 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2670 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InInitializationOrderModuleList
);
2671 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2672 prev
= entry
->Blink
;
2673 if (!mod
->LoadCount
) free_modref( wm
);
2676 /* check load order list too for modules that haven't been initialized yet */
2677 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2678 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2680 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2681 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2682 prev
= entry
->Blink
;
2683 if (!mod
->LoadCount
) free_modref( wm
);
2687 /***********************************************************************
2688 * MODULE_DecRefCount
2690 * The loader_section must be locked while calling this function.
2692 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
2696 if ( wm
->ldr
.Flags
& LDR_UNLOAD_IN_PROGRESS
)
2699 if ( wm
->ldr
.LoadCount
<= 0 )
2702 --wm
->ldr
.LoadCount
;
2703 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2705 if ( wm
->ldr
.LoadCount
== 0 )
2707 wm
->ldr
.Flags
|= LDR_UNLOAD_IN_PROGRESS
;
2709 for ( i
= 0; i
< wm
->nDeps
; i
++ )
2711 MODULE_DecRefCount( wm
->deps
[i
] );
2713 wm
->ldr
.Flags
&= ~LDR_UNLOAD_IN_PROGRESS
;
2717 /******************************************************************
2718 * LdrUnloadDll (NTDLL.@)
2722 NTSTATUS WINAPI
LdrUnloadDll( HMODULE hModule
)
2725 NTSTATUS retv
= STATUS_SUCCESS
;
2727 if (process_detaching
) return retv
;
2729 TRACE("(%p)\n", hModule
);
2731 RtlEnterCriticalSection( &loader_section
);
2734 if ((wm
= get_modref( hModule
)) != NULL
)
2736 TRACE("(%s) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
2738 /* Recursively decrement reference counts */
2739 MODULE_DecRefCount( wm
);
2741 /* Call process detach notifications */
2742 if ( free_lib_count
<= 1 )
2745 MODULE_FlushModrefs();
2751 retv
= STATUS_DLL_NOT_FOUND
;
2755 RtlLeaveCriticalSection( &loader_section
);
2760 /***********************************************************************
2761 * RtlImageNtHeader (NTDLL.@)
2763 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
2765 IMAGE_NT_HEADERS
*ret
;
2769 IMAGE_DOS_HEADER
*dos
= (IMAGE_DOS_HEADER
*)hModule
;
2772 if (dos
->e_magic
== IMAGE_DOS_SIGNATURE
)
2774 ret
= (IMAGE_NT_HEADERS
*)((char *)dos
+ dos
->e_lfanew
);
2775 if (ret
->Signature
!= IMAGE_NT_SIGNATURE
) ret
= NULL
;
2787 /***********************************************************************
2788 * attach_process_dlls
2790 * Initial attach to all the dlls loaded by the process.
2792 static NTSTATUS
attach_process_dlls( void *wm
)
2796 pthread_sigmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
2798 RtlEnterCriticalSection( &loader_section
);
2799 if ((status
= process_attach( wm
, (LPVOID
)1 )) != STATUS_SUCCESS
)
2801 if (last_failed_modref
)
2802 ERR( "%s failed to initialize, aborting\n",
2803 debugstr_w(last_failed_modref
->ldr
.BaseDllName
.Buffer
) + 1 );
2806 attach_implicitly_loaded_dlls( (LPVOID
)1 );
2807 RtlLeaveCriticalSection( &loader_section
);
2812 /***********************************************************************
2813 * load_global_options
2815 static void load_global_options(void)
2817 static const WCHAR sessionW
[] = {'M','a','c','h','i','n','e','\\',
2818 'S','y','s','t','e','m','\\',
2819 'C','u','r','r','e','n','t','C','o','n','t','r','o','l','S','e','t','\\',
2820 'C','o','n','t','r','o','l','\\',
2821 'S','e','s','s','i','o','n',' ','M','a','n','a','g','e','r',0};
2822 static const WCHAR globalflagW
[] = {'G','l','o','b','a','l','F','l','a','g',0};
2823 static const WCHAR critsectW
[] = {'C','r','i','t','i','c','a','l','S','e','c','t','i','o','n','T','i','m','e','o','u','t',0};
2824 static const WCHAR heapresW
[] = {'H','e','a','p','S','e','g','m','e','n','t','R','e','s','e','r','v','e',0};
2825 static const WCHAR heapcommitW
[] = {'H','e','a','p','S','e','g','m','e','n','t','C','o','m','m','i','t',0};
2826 static const WCHAR decommittotalW
[] = {'H','e','a','p','D','e','C','o','m','m','i','t','T','o','t','a','l','F','r','e','e','T','h','r','e','s','h','o','l','d',0};
2827 static const WCHAR decommitfreeW
[] = {'H','e','a','p','D','e','C','o','m','m','i','t','F','r','e','e','B','l','o','c','k','T','h','r','e','s','h','o','l','d',0};
2829 OBJECT_ATTRIBUTES attr
;
2830 UNICODE_STRING name_str
;
2834 attr
.Length
= sizeof(attr
);
2835 attr
.RootDirectory
= 0;
2836 attr
.ObjectName
= &name_str
;
2837 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
2838 attr
.SecurityDescriptor
= NULL
;
2839 attr
.SecurityQualityOfService
= NULL
;
2840 RtlInitUnicodeString( &name_str
, sessionW
);
2842 if (NtOpenKey( &hkey
, KEY_QUERY_VALUE
, &attr
)) return;
2844 query_dword_option( hkey
, globalflagW
, &NtCurrentTeb()->Peb
->NtGlobalFlag
);
2846 query_dword_option( hkey
, critsectW
, &value
);
2847 NtCurrentTeb()->Peb
->CriticalSectionTimeout
.QuadPart
= (ULONGLONG
)value
* -10000000;
2849 query_dword_option( hkey
, heapresW
, &value
);
2850 NtCurrentTeb()->Peb
->HeapSegmentReserve
= value
;
2852 query_dword_option( hkey
, heapcommitW
, &value
);
2853 NtCurrentTeb()->Peb
->HeapSegmentCommit
= value
;
2855 query_dword_option( hkey
, decommittotalW
, &value
);
2856 NtCurrentTeb()->Peb
->HeapDeCommitTotalFreeThreshold
= value
;
2858 query_dword_option( hkey
, decommitfreeW
, &value
);
2859 NtCurrentTeb()->Peb
->HeapDeCommitFreeBlockThreshold
= value
;
2865 /***********************************************************************
2868 static void start_process( void *kernel_start
)
2870 call_thread_entry_point( kernel_start
, NtCurrentTeb()->Peb
);
2873 /******************************************************************
2874 * LdrInitializeThunk (NTDLL.@)
2877 void WINAPI
LdrInitializeThunk( void *kernel_start
, ULONG_PTR unknown2
,
2878 ULONG_PTR unknown3
, ULONG_PTR unknown4
)
2880 static const WCHAR globalflagW
[] = {'G','l','o','b','a','l','F','l','a','g',0};
2884 PEB
*peb
= NtCurrentTeb()->Peb
;
2886 if (main_exe_file
) NtClose( main_exe_file
); /* at this point the main module is created */
2888 /* allocate the modref for the main exe (if not already done) */
2889 wm
= get_modref( peb
->ImageBaseAddress
);
2891 if (wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
)
2893 ERR("%s is a dll, not an executable\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
) );
2897 peb
->LoaderLock
= &loader_section
;
2898 peb
->ProcessParameters
->ImagePathName
= wm
->ldr
.FullDllName
;
2899 if (!peb
->ProcessParameters
->WindowTitle
.Buffer
)
2900 peb
->ProcessParameters
->WindowTitle
= wm
->ldr
.FullDllName
;
2901 version_init( wm
->ldr
.FullDllName
.Buffer
);
2902 virtual_set_large_address_space();
2904 LdrQueryImageFileExecutionOptions( &peb
->ProcessParameters
->ImagePathName
, globalflagW
,
2905 REG_DWORD
, &peb
->NtGlobalFlag
, sizeof(peb
->NtGlobalFlag
), NULL
);
2907 /* the main exe needs to be the first in the load order list */
2908 RemoveEntryList( &wm
->ldr
.InLoadOrderModuleList
);
2909 InsertHeadList( &peb
->LdrData
->InLoadOrderModuleList
, &wm
->ldr
.InLoadOrderModuleList
);
2911 if ((status
= virtual_alloc_thread_stack( NtCurrentTeb(), 0, 0 )) != STATUS_SUCCESS
) goto error
;
2912 if ((status
= server_init_process_done()) != STATUS_SUCCESS
) goto error
;
2915 load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2916 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
) goto error
;
2917 heap_set_debug_flags( GetProcessHeap() );
2919 status
= wine_call_on_stack( attach_process_dlls
, wm
, NtCurrentTeb()->Tib
.StackBase
);
2920 if (status
!= STATUS_SUCCESS
) goto error
;
2922 virtual_release_address_space();
2923 virtual_clear_thread_stack();
2924 wine_switch_to_stack( start_process
, kernel_start
, NtCurrentTeb()->Tib
.StackBase
);
2927 ERR( "Main exe initialization for %s failed, status %x\n",
2928 debugstr_w(peb
->ProcessParameters
->ImagePathName
.Buffer
), status
);
2929 NtTerminateProcess( GetCurrentProcess(), status
);
2933 /***********************************************************************
2934 * RtlImageDirectoryEntryToData (NTDLL.@)
2936 PVOID WINAPI
RtlImageDirectoryEntryToData( HMODULE module
, BOOL image
, WORD dir
, ULONG
*size
)
2938 const IMAGE_NT_HEADERS
*nt
;
2941 if ((ULONG_PTR
)module
& 1) /* mapped as data file */
2943 module
= (HMODULE
)((ULONG_PTR
)module
& ~1);
2946 if (!(nt
= RtlImageNtHeader( module
))) return NULL
;
2947 if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR64_MAGIC
)
2949 const IMAGE_NT_HEADERS64
*nt64
= (const IMAGE_NT_HEADERS64
*)nt
;
2951 if (dir
>= nt64
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
2952 if (!(addr
= nt64
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
2953 *size
= nt64
->OptionalHeader
.DataDirectory
[dir
].Size
;
2954 if (image
|| addr
< nt64
->OptionalHeader
.SizeOfHeaders
) return (char *)module
+ addr
;
2956 else if (nt
->OptionalHeader
.Magic
== IMAGE_NT_OPTIONAL_HDR32_MAGIC
)
2958 const IMAGE_NT_HEADERS32
*nt32
= (const IMAGE_NT_HEADERS32
*)nt
;
2960 if (dir
>= nt32
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
2961 if (!(addr
= nt32
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
2962 *size
= nt32
->OptionalHeader
.DataDirectory
[dir
].Size
;
2963 if (image
|| addr
< nt32
->OptionalHeader
.SizeOfHeaders
) return (char *)module
+ addr
;
2967 /* not mapped as image, need to find the section containing the virtual address */
2968 return RtlImageRvaToVa( nt
, module
, addr
, NULL
);
2972 /***********************************************************************
2973 * RtlImageRvaToSection (NTDLL.@)
2975 PIMAGE_SECTION_HEADER WINAPI
RtlImageRvaToSection( const IMAGE_NT_HEADERS
*nt
,
2976 HMODULE module
, DWORD rva
)
2979 const IMAGE_SECTION_HEADER
*sec
;
2981 sec
= (const IMAGE_SECTION_HEADER
*)((const char*)&nt
->OptionalHeader
+
2982 nt
->FileHeader
.SizeOfOptionalHeader
);
2983 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
2985 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2986 return (PIMAGE_SECTION_HEADER
)sec
;
2992 /***********************************************************************
2993 * RtlImageRvaToVa (NTDLL.@)
2995 PVOID WINAPI
RtlImageRvaToVa( const IMAGE_NT_HEADERS
*nt
, HMODULE module
,
2996 DWORD rva
, IMAGE_SECTION_HEADER
**section
)
2998 IMAGE_SECTION_HEADER
*sec
;
3000 if (section
&& *section
) /* try this section first */
3003 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
3006 if (!(sec
= RtlImageRvaToSection( nt
, module
, rva
))) return NULL
;
3008 if (section
) *section
= sec
;
3009 return (char *)module
+ sec
->PointerToRawData
+ (rva
- sec
->VirtualAddress
);
3013 /***********************************************************************
3014 * RtlPcToFileHeader (NTDLL.@)
3016 PVOID WINAPI
RtlPcToFileHeader( PVOID pc
, PVOID
*address
)
3021 RtlEnterCriticalSection( &loader_section
);
3022 if (!LdrFindEntryForAddress( pc
, &module
)) ret
= module
->BaseAddress
;
3023 RtlLeaveCriticalSection( &loader_section
);
3029 /***********************************************************************
3030 * NtLoadDriver (NTDLL.@)
3031 * ZwLoadDriver (NTDLL.@)
3033 NTSTATUS WINAPI
NtLoadDriver( const UNICODE_STRING
*DriverServiceName
)
3035 FIXME("(%p), stub!\n",DriverServiceName
);
3036 return STATUS_NOT_IMPLEMENTED
;
3040 /***********************************************************************
3041 * NtUnloadDriver (NTDLL.@)
3042 * ZwUnloadDriver (NTDLL.@)
3044 NTSTATUS WINAPI
NtUnloadDriver( const UNICODE_STRING
*DriverServiceName
)
3046 FIXME("(%p), stub!\n",DriverServiceName
);
3047 return STATUS_NOT_IMPLEMENTED
;
3051 /******************************************************************
3054 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
3056 if (reason
== DLL_PROCESS_ATTACH
) LdrDisableThreadCalloutsForDll( inst
);
3061 /******************************************************************
3062 * __wine_init_windows_dir (NTDLL.@)
3064 * Windows and system dir initialization once kernel32 has been loaded.
3066 void CDECL
__wine_init_windows_dir( const WCHAR
*windir
, const WCHAR
*sysdir
)
3068 PLIST_ENTRY mark
, entry
;
3071 strcpyW( user_shared_data
->NtSystemRoot
, windir
);
3072 DIR_init_windows_dir( windir
, sysdir
);
3074 /* prepend the system dir to the name of the already created modules */
3075 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
3076 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
3078 LDR_MODULE
*mod
= CONTAINING_RECORD( entry
, LDR_MODULE
, InLoadOrderModuleList
);
3080 assert( mod
->Flags
& LDR_WINE_INTERNAL
);
3082 buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
3083 system_dir
.Length
+ mod
->FullDllName
.Length
+ 2*sizeof(WCHAR
) );
3084 if (!buffer
) continue;
3085 strcpyW( buffer
, system_dir
.Buffer
);
3086 p
= buffer
+ strlenW( buffer
);
3087 if (p
> buffer
&& p
[-1] != '\\') *p
++ = '\\';
3088 strcpyW( p
, mod
->FullDllName
.Buffer
);
3089 RtlInitUnicodeString( &mod
->FullDllName
, buffer
);
3090 RtlInitUnicodeString( &mod
->BaseDllName
, p
);
3095 /***********************************************************************
3096 * __wine_process_init
3098 void __wine_process_init(void)
3100 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
3104 ANSI_STRING func_name
;
3105 void (* DECLSPEC_NORETURN CDECL init_func
)(void);
3107 main_exe_file
= thread_init();
3109 /* retrieve current umask */
3110 FILE_umask
= umask(0777);
3111 umask( FILE_umask
);
3113 load_global_options();
3115 /* setup the load callback and create ntdll modref */
3116 wine_dll_set_callback( load_builtin_callback
);
3118 if ((status
= load_builtin_dll( NULL
, kernel32W
, 0, 0, &wm
)) != STATUS_SUCCESS
)
3120 MESSAGE( "wine: could not load kernel32.dll, status %x\n", status
);
3123 RtlInitAnsiString( &func_name
, "UnhandledExceptionFilter" );
3124 LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
, 0, (void **)&unhandled_exception_filter
);
3126 RtlInitAnsiString( &func_name
, "__wine_kernel_init" );
3127 if ((status
= LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
,
3128 0, (void **)&init_func
)) != STATUS_SUCCESS
)
3130 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %x\n", status
);