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"
28 #define NONAMELESSUNION
29 #define NONAMELESSSTRUCT
32 #define WIN32_NO_STATUS
37 #include "wine/exception.h"
38 #include "wine/library.h"
39 #include "wine/pthread.h"
40 #include "wine/unicode.h"
41 #include "wine/debug.h"
42 #include "wine/server.h"
43 #include "ntdll_misc.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(module
);
47 WINE_DECLARE_DEBUG_CHANNEL(relay
);
48 WINE_DECLARE_DEBUG_CHANNEL(snoop
);
49 WINE_DECLARE_DEBUG_CHANNEL(loaddll
);
50 WINE_DECLARE_DEBUG_CHANNEL(imports
);
52 /* we don't want to include winuser.h */
53 #define RT_MANIFEST ((ULONG_PTR)24)
54 #define ISOLATIONAWARE_MANIFEST_RESOURCE_ID ((ULONG_PTR)2)
56 extern struct wine_pthread_functions pthread_functions
;
58 typedef DWORD (CALLBACK
*DLLENTRYPROC
)(HMODULE
,DWORD
,LPVOID
);
60 static int process_detaching
= 0; /* set on process detach to avoid deadlocks with thread detach */
61 static int free_lib_count
; /* recursion depth of LdrUnloadDll calls */
63 static const char * const reason_names
[] =
69 NULL
, NULL
, NULL
, NULL
,
73 static const WCHAR dllW
[] = {'.','d','l','l',0};
75 /* internal representation of 32bit modules. per process. */
76 typedef struct _wine_modref
80 struct _wine_modref
**deps
;
83 /* info about the current builtin dll load */
84 /* used to keep track of things across the register_dll constructor call */
85 struct builtin_load_info
87 const WCHAR
*load_path
;
88 const WCHAR
*filename
;
93 static struct builtin_load_info default_load_info
;
94 static struct builtin_load_info
*builtin_load_info
= &default_load_info
;
96 static HANDLE main_exe_file
;
97 static UINT tls_module_count
; /* number of modules with TLS directory */
98 static UINT tls_total_size
; /* total size of TLS storage */
99 static const IMAGE_TLS_DIRECTORY
**tls_dirs
; /* array of TLS directories */
101 UNICODE_STRING windows_dir
= { 0, 0, NULL
}; /* windows directory */
102 UNICODE_STRING system_dir
= { 0, 0, NULL
}; /* system directory */
104 static RTL_CRITICAL_SECTION loader_section
;
105 static RTL_CRITICAL_SECTION_DEBUG critsect_debug
=
107 0, 0, &loader_section
,
108 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
109 0, 0, { (DWORD_PTR
)(__FILE__
": loader_section") }
111 static RTL_CRITICAL_SECTION loader_section
= { &critsect_debug
, -1, 0, 0, 0, 0 };
113 static WINE_MODREF
*cached_modref
;
114 static WINE_MODREF
*current_modref
;
115 static WINE_MODREF
*last_failed_modref
;
117 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
);
118 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
);
119 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
120 DWORD exp_size
, const char *name
, int hint
, LPCWSTR load_path
);
122 /* convert PE image VirtualAddress to Real Address */
123 static inline void *get_rva( HMODULE module
, DWORD va
)
125 return (void *)((char *)module
+ va
);
128 /* check whether the file name contains a path */
129 static inline int contains_path( LPCWSTR name
)
131 return ((*name
&& (name
[1] == ':')) || strchrW(name
, '/') || strchrW(name
, '\\'));
134 /* convert from straight ASCII to Unicode without depending on the current codepage */
135 static inline void ascii_to_unicode( WCHAR
*dst
, const char *src
, size_t len
)
137 while (len
--) *dst
++ = (unsigned char)*src
++;
141 /*************************************************************************
142 * call_dll_entry_point
144 * Some brain-damaged dlls (ir32_32.dll for instance) modify ebx in
145 * their entry point, so we need a small asm wrapper.
148 extern BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
, UINT reason
, void *reserved
);
149 __ASM_GLOBAL_FUNC(call_dll_entry_point
,
157 "movl 8(%ebp),%eax\n\t"
159 "leal -4(%ebp),%esp\n\t"
164 static inline BOOL
call_dll_entry_point( DLLENTRYPROC proc
, void *module
,
165 UINT reason
, void *reserved
)
167 return proc( module
, reason
, reserved
);
169 #endif /* __i386__ */
173 /*************************************************************************
176 * Entry point for stub functions.
178 static void stub_entry_point( const char *dll
, const char *name
, ... )
180 EXCEPTION_RECORD rec
;
182 rec
.ExceptionCode
= EXCEPTION_WINE_STUB
;
183 rec
.ExceptionFlags
= EH_NONCONTINUABLE
;
184 rec
.ExceptionRecord
= NULL
;
186 rec
.ExceptionAddress
= __builtin_return_address(0);
188 rec
.ExceptionAddress
= *((void **)&dll
- 1);
190 rec
.NumberParameters
= 2;
191 rec
.ExceptionInformation
[0] = (ULONG_PTR
)dll
;
192 rec
.ExceptionInformation
[1] = (ULONG_PTR
)name
;
193 for (;;) RtlRaiseException( &rec
);
197 #include "pshpack1.h"
200 BYTE popl_eax
; /* popl %eax */
201 BYTE pushl1
; /* pushl $name */
203 BYTE pushl2
; /* pushl $dll */
205 BYTE pushl_eax
; /* pushl %eax */
206 BYTE jmp
; /* jmp stub_entry_point */
211 /*************************************************************************
214 * Allocate a stub entry point.
216 static ULONG_PTR
allocate_stub( const char *dll
, const char *name
)
218 #define MAX_SIZE 65536
219 static struct stub
*stubs
;
220 static unsigned int nb_stubs
;
223 if (nb_stubs
>= MAX_SIZE
/ sizeof(*stub
)) return 0xdeadbeef;
227 SIZE_T size
= MAX_SIZE
;
228 if (NtAllocateVirtualMemory( NtCurrentProcess(), (void **)&stubs
, 0, &size
,
229 MEM_COMMIT
, PAGE_EXECUTE_WRITECOPY
) != STATUS_SUCCESS
)
232 stub
= &stubs
[nb_stubs
++];
233 stub
->popl_eax
= 0x58; /* popl %eax */
234 stub
->pushl1
= 0x68; /* pushl $name */
236 stub
->pushl2
= 0x68; /* pushl $dll */
238 stub
->pushl_eax
= 0x50; /* pushl %eax */
239 stub
->jmp
= 0xe9; /* jmp stub_entry_point */
240 stub
->entry
= (BYTE
*)stub_entry_point
- (BYTE
*)(&stub
->entry
+ 1);
241 return (ULONG_PTR
)stub
;
245 static inline ULONG_PTR
allocate_stub( const char *dll
, const char *name
) { return 0xdeadbeef; }
246 #endif /* __i386__ */
249 /*************************************************************************
252 * Looks for the referenced HMODULE in the current process
253 * The loader_section must be locked while calling this function.
255 static WINE_MODREF
*get_modref( HMODULE hmod
)
257 PLIST_ENTRY mark
, entry
;
260 if (cached_modref
&& cached_modref
->ldr
.BaseAddress
== hmod
) return cached_modref
;
262 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
263 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
265 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
266 if (mod
->BaseAddress
== hmod
)
267 return cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
268 if (mod
->BaseAddress
> (void*)hmod
) break;
274 /**********************************************************************
275 * find_basename_module
277 * Find a module from its base name.
278 * The loader_section must be locked while calling this function
280 static WINE_MODREF
*find_basename_module( LPCWSTR name
)
282 PLIST_ENTRY mark
, entry
;
284 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.BaseDllName
.Buffer
))
285 return cached_modref
;
287 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
288 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
290 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
291 if (!strcmpiW( name
, mod
->BaseDllName
.Buffer
))
293 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
294 return cached_modref
;
301 /**********************************************************************
302 * find_fullname_module
304 * Find a module from its full path name.
305 * The loader_section must be locked while calling this function
307 static WINE_MODREF
*find_fullname_module( LPCWSTR name
)
309 PLIST_ENTRY mark
, entry
;
311 if (cached_modref
&& !strcmpiW( name
, cached_modref
->ldr
.FullDllName
.Buffer
))
312 return cached_modref
;
314 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
315 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
317 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
318 if (!strcmpiW( name
, mod
->FullDllName
.Buffer
))
320 cached_modref
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
321 return cached_modref
;
328 /*************************************************************************
329 * find_forwarded_export
331 * Find the final function pointer for a forwarded function.
332 * The loader_section must be locked while calling this function.
334 static FARPROC
find_forwarded_export( HMODULE module
, const char *forward
, LPCWSTR load_path
)
336 const IMAGE_EXPORT_DIRECTORY
*exports
;
340 const char *end
= strrchr(forward
, '.');
343 if (!end
) return NULL
;
344 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
)) return NULL
;
345 ascii_to_unicode( mod_name
, forward
, end
- forward
);
346 mod_name
[end
- forward
] = 0;
347 if (!strchrW( mod_name
, '.' ))
349 if ((end
- forward
) * sizeof(WCHAR
) >= sizeof(mod_name
) - sizeof(dllW
)) return NULL
;
350 memcpy( mod_name
+ (end
- forward
), dllW
, sizeof(dllW
) );
353 if (!(wm
= find_basename_module( mod_name
)))
355 TRACE( "delay loading %s for '%s'\n", debugstr_w(mod_name
), forward
);
356 if (load_dll( load_path
, mod_name
, 0, &wm
) == STATUS_SUCCESS
&&
357 !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
359 if (process_attach( wm
, NULL
) != STATUS_SUCCESS
)
361 LdrUnloadDll( wm
->ldr
.BaseAddress
);
368 ERR( "module not found for forward '%s' used by %s\n",
369 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
) );
373 if ((exports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
374 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
375 proc
= find_named_export( wm
->ldr
.BaseAddress
, exports
, exp_size
, end
+ 1, -1, load_path
);
379 ERR("function not found for forward '%s' used by %s."
380 " If you are using builtin %s, try using the native one instead.\n",
381 forward
, debugstr_w(get_modref(module
)->ldr
.FullDllName
.Buffer
),
382 debugstr_w(get_modref(module
)->ldr
.BaseDllName
.Buffer
) );
388 /*************************************************************************
389 * find_ordinal_export
391 * Find an exported function by ordinal.
392 * The exports base must have been subtracted from the ordinal already.
393 * The loader_section must be locked while calling this function.
395 static FARPROC
find_ordinal_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
396 DWORD exp_size
, DWORD ordinal
, LPCWSTR load_path
)
399 const DWORD
*functions
= get_rva( module
, exports
->AddressOfFunctions
);
401 if (ordinal
>= exports
->NumberOfFunctions
)
403 TRACE(" ordinal %d out of range!\n", ordinal
+ exports
->Base
);
406 if (!functions
[ordinal
]) return NULL
;
408 proc
= get_rva( module
, functions
[ordinal
] );
410 /* if the address falls into the export dir, it's a forward */
411 if (((const char *)proc
>= (const char *)exports
) &&
412 ((const char *)proc
< (const char *)exports
+ exp_size
))
413 return find_forwarded_export( module
, (const char *)proc
, load_path
);
417 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
418 proc
= SNOOP_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
422 const WCHAR
*user
= current_modref
? current_modref
->ldr
.BaseDllName
.Buffer
: NULL
;
423 proc
= RELAY_GetProcAddress( module
, exports
, exp_size
, proc
, ordinal
, user
);
429 /*************************************************************************
432 * Find an exported function by name.
433 * The loader_section must be locked while calling this function.
435 static FARPROC
find_named_export( HMODULE module
, const IMAGE_EXPORT_DIRECTORY
*exports
,
436 DWORD exp_size
, const char *name
, int hint
, LPCWSTR load_path
)
438 const WORD
*ordinals
= get_rva( module
, exports
->AddressOfNameOrdinals
);
439 const DWORD
*names
= get_rva( module
, exports
->AddressOfNames
);
440 int min
= 0, max
= exports
->NumberOfNames
- 1;
442 /* first check the hint */
443 if (hint
>= 0 && hint
<= max
)
445 char *ename
= get_rva( module
, names
[hint
] );
446 if (!strcmp( ename
, name
))
447 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[hint
], load_path
);
450 /* then do a binary search */
453 int res
, pos
= (min
+ max
) / 2;
454 char *ename
= get_rva( module
, names
[pos
] );
455 if (!(res
= strcmp( ename
, name
)))
456 return find_ordinal_export( module
, exports
, exp_size
, ordinals
[pos
], load_path
);
457 if (res
> 0) max
= pos
- 1;
465 /*************************************************************************
468 * Import the dll specified by the given import descriptor.
469 * The loader_section must be locked while calling this function.
471 static WINE_MODREF
*import_dll( HMODULE module
, const IMAGE_IMPORT_DESCRIPTOR
*descr
, LPCWSTR load_path
)
476 const IMAGE_EXPORT_DIRECTORY
*exports
;
478 const IMAGE_THUNK_DATA
*import_list
;
479 IMAGE_THUNK_DATA
*thunk_list
;
481 const char *name
= get_rva( module
, descr
->Name
);
482 DWORD len
= strlen(name
);
484 SIZE_T protect_size
= 0;
487 thunk_list
= get_rva( module
, (DWORD
)descr
->FirstThunk
);
488 if (descr
->u
.OriginalFirstThunk
)
489 import_list
= get_rva( module
, (DWORD
)descr
->u
.OriginalFirstThunk
);
491 import_list
= thunk_list
;
493 while (len
&& name
[len
-1] == ' ') len
--; /* remove trailing spaces */
495 if (len
* sizeof(WCHAR
) < sizeof(buffer
))
497 ascii_to_unicode( buffer
, name
, len
);
499 status
= load_dll( load_path
, buffer
, 0, &wmImp
);
501 else /* need to allocate a larger buffer */
503 WCHAR
*ptr
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) );
504 if (!ptr
) return NULL
;
505 ascii_to_unicode( ptr
, name
, len
);
507 status
= load_dll( load_path
, ptr
, 0, &wmImp
);
508 RtlFreeHeap( GetProcessHeap(), 0, ptr
);
513 if (status
== STATUS_DLL_NOT_FOUND
)
514 ERR("Library %s (which is needed by %s) not found\n",
515 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
));
517 ERR("Loading library %s (which is needed by %s) failed (error %x).\n",
518 name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
), status
);
522 /* unprotect the import address table since it can be located in
523 * readonly section */
524 while (import_list
[protect_size
].u1
.Ordinal
) protect_size
++;
525 protect_base
= thunk_list
;
526 protect_size
*= sizeof(*thunk_list
);
527 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
,
528 &protect_size
, PAGE_WRITECOPY
, &protect_old
);
530 imp_mod
= wmImp
->ldr
.BaseAddress
;
531 exports
= RtlImageDirectoryEntryToData( imp_mod
, TRUE
, IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
);
535 /* set all imported function to deadbeef */
536 while (import_list
->u1
.Ordinal
)
538 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
540 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
541 WARN("No implementation for %s.%d", name
, ordinal
);
542 thunk_list
->u1
.Function
= allocate_stub( name
, IntToPtr(ordinal
) );
546 IMAGE_IMPORT_BY_NAME
*pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
547 WARN("No implementation for %s.%s", name
, pe_name
->Name
);
548 thunk_list
->u1
.Function
= allocate_stub( name
, (const char*)pe_name
->Name
);
550 WARN(" imported from %s, allocating stub %p\n",
551 debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
552 (void *)thunk_list
->u1
.Function
);
559 while (import_list
->u1
.Ordinal
)
561 if (IMAGE_SNAP_BY_ORDINAL(import_list
->u1
.Ordinal
))
563 int ordinal
= IMAGE_ORDINAL(import_list
->u1
.Ordinal
);
565 thunk_list
->u1
.Function
= (ULONG_PTR
)find_ordinal_export( imp_mod
, exports
, exp_size
,
566 ordinal
- exports
->Base
, load_path
);
567 if (!thunk_list
->u1
.Function
)
569 thunk_list
->u1
.Function
= allocate_stub( name
, IntToPtr(ordinal
) );
570 WARN("No implementation for %s.%d imported from %s, setting to %p\n",
571 name
, ordinal
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
572 (void *)thunk_list
->u1
.Function
);
574 TRACE_(imports
)("--- Ordinal %s.%d = %p\n", name
, ordinal
, (void *)thunk_list
->u1
.Function
);
576 else /* import by name */
578 IMAGE_IMPORT_BY_NAME
*pe_name
;
579 pe_name
= get_rva( module
, (DWORD
)import_list
->u1
.AddressOfData
);
580 thunk_list
->u1
.Function
= (ULONG_PTR
)find_named_export( imp_mod
, exports
, exp_size
,
581 (const char*)pe_name
->Name
,
582 pe_name
->Hint
, load_path
);
583 if (!thunk_list
->u1
.Function
)
585 thunk_list
->u1
.Function
= allocate_stub( name
, (const char*)pe_name
->Name
);
586 WARN("No implementation for %s.%s imported from %s, setting to %p\n",
587 name
, pe_name
->Name
, debugstr_w(current_modref
->ldr
.FullDllName
.Buffer
),
588 (void *)thunk_list
->u1
.Function
);
590 TRACE_(imports
)("--- %s %s.%d = %p\n",
591 pe_name
->Name
, name
, pe_name
->Hint
, (void *)thunk_list
->u1
.Function
);
598 /* restore old protection of the import address table */
599 NtProtectVirtualMemory( NtCurrentProcess(), &protect_base
, &protect_size
, protect_old
, NULL
);
604 /***********************************************************************
605 * create_module_activation_context
607 static NTSTATUS
create_module_activation_context( LDR_MODULE
*module
)
610 LDR_RESOURCE_INFO info
;
611 const IMAGE_RESOURCE_DATA_ENTRY
*entry
;
613 info
.Type
= RT_MANIFEST
;
614 info
.Name
= ISOLATIONAWARE_MANIFEST_RESOURCE_ID
;
616 if (!(status
= LdrFindResource_U( module
->BaseAddress
, &info
, 3, &entry
)))
619 ctx
.cbSize
= sizeof(ctx
);
621 ctx
.dwFlags
= ACTCTX_FLAG_RESOURCE_NAME_VALID
| ACTCTX_FLAG_HMODULE_VALID
;
622 ctx
.hModule
= module
->BaseAddress
;
623 ctx
.lpResourceName
= (LPCWSTR
)ISOLATIONAWARE_MANIFEST_RESOURCE_ID
;
624 status
= RtlCreateActivationContext( &module
->ActivationContext
, &ctx
);
630 /****************************************************************
633 * Fixup all imports of a given module.
634 * The loader_section must be locked while calling this function.
636 static NTSTATUS
fixup_imports( WINE_MODREF
*wm
, LPCWSTR load_path
)
639 const IMAGE_IMPORT_DESCRIPTOR
*imports
;
645 if (!(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
)) return STATUS_SUCCESS
; /* already done */
646 wm
->ldr
.Flags
&= ~LDR_DONT_RESOLVE_REFS
;
648 if (!(imports
= RtlImageDirectoryEntryToData( wm
->ldr
.BaseAddress
, TRUE
,
649 IMAGE_DIRECTORY_ENTRY_IMPORT
, &size
)))
650 return STATUS_SUCCESS
;
653 while (imports
[nb_imports
].Name
&& imports
[nb_imports
].FirstThunk
) nb_imports
++;
655 if (!nb_imports
) return STATUS_SUCCESS
; /* no imports */
657 if (!create_module_activation_context( &wm
->ldr
))
658 RtlActivateActivationContext( 0, wm
->ldr
.ActivationContext
, &cookie
);
660 /* Allocate module dependency list */
661 wm
->nDeps
= nb_imports
;
662 wm
->deps
= RtlAllocateHeap( GetProcessHeap(), 0, nb_imports
*sizeof(WINE_MODREF
*) );
664 /* load the imported modules. They are automatically
665 * added to the modref list of the process.
667 prev
= current_modref
;
669 status
= STATUS_SUCCESS
;
670 for (i
= 0; i
< nb_imports
; i
++)
672 if (!(wm
->deps
[i
] = import_dll( wm
->ldr
.BaseAddress
, &imports
[i
], load_path
)))
673 status
= STATUS_DLL_NOT_FOUND
;
675 current_modref
= prev
;
676 if (wm
->ldr
.ActivationContext
) RtlDeactivateActivationContext( 0, cookie
);
681 /*************************************************************************
684 * Allocate a WINE_MODREF structure and add it to the process list
685 * The loader_section must be locked while calling this function.
687 static WINE_MODREF
*alloc_module( HMODULE hModule
, LPCWSTR filename
)
691 const IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader(hModule
);
692 PLIST_ENTRY entry
, mark
;
694 if (!(wm
= RtlAllocateHeap( GetProcessHeap(), 0, sizeof(*wm
) ))) return NULL
;
699 wm
->ldr
.BaseAddress
= hModule
;
700 wm
->ldr
.EntryPoint
= NULL
;
701 wm
->ldr
.SizeOfImage
= nt
->OptionalHeader
.SizeOfImage
;
702 wm
->ldr
.Flags
= LDR_DONT_RESOLVE_REFS
;
703 wm
->ldr
.LoadCount
= 1;
704 wm
->ldr
.TlsIndex
= -1;
705 wm
->ldr
.SectionHandle
= NULL
;
706 wm
->ldr
.CheckSum
= 0;
707 wm
->ldr
.TimeDateStamp
= 0;
708 wm
->ldr
.ActivationContext
= 0;
710 RtlCreateUnicodeString( &wm
->ldr
.FullDllName
, filename
);
711 if ((p
= strrchrW( wm
->ldr
.FullDllName
.Buffer
, '\\' ))) p
++;
712 else p
= wm
->ldr
.FullDllName
.Buffer
;
713 RtlInitUnicodeString( &wm
->ldr
.BaseDllName
, p
);
715 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
717 wm
->ldr
.Flags
|= LDR_IMAGE_IS_DLL
;
718 if (nt
->OptionalHeader
.AddressOfEntryPoint
)
719 wm
->ldr
.EntryPoint
= (char *)hModule
+ nt
->OptionalHeader
.AddressOfEntryPoint
;
722 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
,
723 &wm
->ldr
.InLoadOrderModuleList
);
725 /* insert module in MemoryList, sorted in increasing base addresses */
726 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
727 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
729 if (CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
)->BaseAddress
> wm
->ldr
.BaseAddress
)
732 entry
->Blink
->Flink
= &wm
->ldr
.InMemoryOrderModuleList
;
733 wm
->ldr
.InMemoryOrderModuleList
.Blink
= entry
->Blink
;
734 wm
->ldr
.InMemoryOrderModuleList
.Flink
= entry
;
735 entry
->Blink
= &wm
->ldr
.InMemoryOrderModuleList
;
737 /* wait until init is called for inserting into this list */
738 wm
->ldr
.InInitializationOrderModuleList
.Flink
= NULL
;
739 wm
->ldr
.InInitializationOrderModuleList
.Blink
= NULL
;
741 if (!(nt
->OptionalHeader
.DllCharacteristics
& IMAGE_DLLCHARACTERISTICS_NX_COMPAT
))
743 WARN( "disabling no-exec because of %s\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
) );
744 VIRTUAL_SetForceExec( TRUE
);
750 /*************************************************************************
753 * Allocate the process-wide structure for module TLS storage.
755 static NTSTATUS
alloc_process_tls(void)
757 PLIST_ENTRY mark
, entry
;
759 const IMAGE_TLS_DIRECTORY
*dir
;
762 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
763 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
765 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
766 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
,
767 IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
769 size
= (dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
) + dir
->SizeOfZeroFill
;
771 tls_total_size
+= size
;
774 if (!tls_module_count
) return STATUS_SUCCESS
;
776 TRACE( "count %u size %u\n", tls_module_count
, tls_total_size
);
778 tls_dirs
= RtlAllocateHeap( GetProcessHeap(), 0, tls_module_count
* sizeof(*tls_dirs
) );
779 if (!tls_dirs
) return STATUS_NO_MEMORY
;
781 for (i
= 0, entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
783 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
784 if (!(dir
= RtlImageDirectoryEntryToData( mod
->BaseAddress
, TRUE
,
785 IMAGE_DIRECTORY_ENTRY_TLS
, &size
)))
788 *(DWORD
*)dir
->AddressOfIndex
= i
;
790 mod
->LoadCount
= -1; /* can't unload it */
793 return STATUS_SUCCESS
;
797 /*************************************************************************
800 * Allocate the per-thread structure for module TLS storage.
802 static NTSTATUS
alloc_thread_tls(void)
808 if (!tls_module_count
) return STATUS_SUCCESS
;
810 if (!(pointers
= RtlAllocateHeap( GetProcessHeap(), 0,
811 tls_module_count
* sizeof(*pointers
) )))
812 return STATUS_NO_MEMORY
;
814 if (!(data
= RtlAllocateHeap( GetProcessHeap(), 0, tls_total_size
)))
816 RtlFreeHeap( GetProcessHeap(), 0, pointers
);
817 return STATUS_NO_MEMORY
;
820 for (i
= 0; i
< tls_module_count
; i
++)
822 const IMAGE_TLS_DIRECTORY
*dir
= tls_dirs
[i
];
823 ULONG size
= dir
->EndAddressOfRawData
- dir
->StartAddressOfRawData
;
825 TRACE( "thread %04x idx %d: %d/%d bytes from %p to %p\n",
826 GetCurrentThreadId(), i
, size
, dir
->SizeOfZeroFill
,
827 (void *)dir
->StartAddressOfRawData
, data
);
830 memcpy( data
, (void *)dir
->StartAddressOfRawData
, size
);
832 memset( data
, 0, dir
->SizeOfZeroFill
);
833 data
+= dir
->SizeOfZeroFill
;
835 NtCurrentTeb()->ThreadLocalStoragePointer
= pointers
;
836 return STATUS_SUCCESS
;
840 /*************************************************************************
843 static void call_tls_callbacks( HMODULE module
, UINT reason
)
845 const IMAGE_TLS_DIRECTORY
*dir
;
846 const PIMAGE_TLS_CALLBACK
*callback
;
849 dir
= RtlImageDirectoryEntryToData( module
, TRUE
, IMAGE_DIRECTORY_ENTRY_TLS
, &dirsize
);
850 if (!dir
|| !dir
->AddressOfCallBacks
) return;
852 for (callback
= (const PIMAGE_TLS_CALLBACK
*)dir
->AddressOfCallBacks
; *callback
; callback
++)
855 DPRINTF("%04x:Call TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
856 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
857 if (NtCurrentTeb()->Peb
->BeingDebugged
) DbgBreakPoint();
860 (*callback
)( module
, reason
, NULL
);
865 DPRINTF("%04x:exception in TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
866 GetCurrentThreadId(), callback
, module
, reason_names
[reason
] );
871 DPRINTF("%04x:Ret TLS callback (proc=%p,module=%p,reason=%s,reserved=0)\n",
872 GetCurrentThreadId(), *callback
, module
, reason_names
[reason
] );
877 /*************************************************************************
880 static BOOL
MODULE_InitDLL( WINE_MODREF
*wm
, UINT reason
, LPVOID lpReserved
)
884 DLLENTRYPROC entry
= wm
->ldr
.EntryPoint
;
885 void *module
= wm
->ldr
.BaseAddress
;
887 /* Skip calls for modules loaded with special load flags */
889 if (wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
) return TRUE
;
890 if (wm
->ldr
.TlsIndex
!= -1) call_tls_callbacks( wm
->ldr
.BaseAddress
, reason
);
891 if (!entry
) return TRUE
;
895 size_t len
= min( wm
->ldr
.BaseDllName
.Length
, sizeof(mod_name
)-sizeof(WCHAR
) );
896 memcpy( mod_name
, wm
->ldr
.BaseDllName
.Buffer
, len
);
897 mod_name
[len
/ sizeof(WCHAR
)] = 0;
898 DPRINTF("%04x:Call PE DLL (proc=%p,module=%p %s,reason=%s,res=%p)\n",
899 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
900 reason_names
[reason
], lpReserved
);
902 else TRACE("(%p %s,%s,%p) - CALL\n", module
, debugstr_w(wm
->ldr
.BaseDllName
.Buffer
),
903 reason_names
[reason
], lpReserved
);
905 retv
= call_dll_entry_point( entry
, module
, reason
, lpReserved
);
907 /* The state of the module list may have changed due to the call
908 to the dll. We cannot assume that this module has not been
911 DPRINTF("%04x:Ret PE DLL (proc=%p,module=%p %s,reason=%s,res=%p) retval=%x\n",
912 GetCurrentThreadId(), entry
, module
, debugstr_w(mod_name
),
913 reason_names
[reason
], lpReserved
, retv
);
914 else TRACE("(%p,%s,%p) - RETURN %d\n", module
, reason_names
[reason
], lpReserved
, retv
);
920 /*************************************************************************
923 * Send the process attach notification to all DLLs the given module
924 * depends on (recursively). This is somewhat complicated due to the fact that
926 * - we have to respect the module dependencies, i.e. modules implicitly
927 * referenced by another module have to be initialized before the module
928 * itself can be initialized
930 * - the initialization routine of a DLL can itself call LoadLibrary,
931 * thereby introducing a whole new set of dependencies (even involving
932 * the 'old' modules) at any time during the whole process
934 * (Note that this routine can be recursively entered not only directly
935 * from itself, but also via LoadLibrary from one of the called initialization
938 * Furthermore, we need to rearrange the main WINE_MODREF list to allow
939 * the process *detach* notifications to be sent in the correct order.
940 * This must not only take into account module dependencies, but also
941 * 'hidden' dependencies created by modules calling LoadLibrary in their
942 * attach notification routine.
944 * The strategy is rather simple: we move a WINE_MODREF to the head of the
945 * list after the attach notification has returned. This implies that the
946 * detach notifications are called in the reverse of the sequence the attach
947 * notifications *returned*.
949 * The loader_section must be locked while calling this function.
951 static NTSTATUS
process_attach( WINE_MODREF
*wm
, LPVOID lpReserved
)
953 NTSTATUS status
= STATUS_SUCCESS
;
957 if (process_detaching
) return status
;
959 /* prevent infinite recursion in case of cyclical dependencies */
960 if ( ( wm
->ldr
.Flags
& LDR_LOAD_IN_PROGRESS
)
961 || ( wm
->ldr
.Flags
& LDR_PROCESS_ATTACHED
) )
964 TRACE("(%s,%p) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
966 /* Tag current MODREF to prevent recursive loop */
967 wm
->ldr
.Flags
|= LDR_LOAD_IN_PROGRESS
;
968 if (lpReserved
) wm
->ldr
.LoadCount
= -1; /* pin it if imported by the main exe */
969 if (wm
->ldr
.ActivationContext
) RtlActivateActivationContext( 0, wm
->ldr
.ActivationContext
, &cookie
);
971 /* Recursively attach all DLLs this one depends on */
972 for ( i
= 0; i
< wm
->nDeps
; i
++ )
974 if (!wm
->deps
[i
]) continue;
975 if ((status
= process_attach( wm
->deps
[i
], lpReserved
)) != STATUS_SUCCESS
) break;
978 /* Call DLL entry point */
979 if (status
== STATUS_SUCCESS
)
981 WINE_MODREF
*prev
= current_modref
;
983 if (MODULE_InitDLL( wm
, DLL_PROCESS_ATTACH
, lpReserved
))
985 wm
->ldr
.Flags
|= LDR_PROCESS_ATTACHED
;
989 /* point to the name so LdrInitializeThunk can print it */
990 last_failed_modref
= wm
;
991 WARN("Initialization of %s failed\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
992 status
= STATUS_DLL_INIT_FAILED
;
994 current_modref
= prev
;
997 if (!wm
->ldr
.InInitializationOrderModuleList
.Flink
)
998 InsertTailList(&NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
,
999 &wm
->ldr
.InInitializationOrderModuleList
);
1001 if (wm
->ldr
.ActivationContext
) RtlDeactivateActivationContext( 0, cookie
);
1002 /* Remove recursion flag */
1003 wm
->ldr
.Flags
&= ~LDR_LOAD_IN_PROGRESS
;
1005 TRACE("(%s,%p) - END\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), lpReserved
);
1010 /**********************************************************************
1011 * attach_implicitly_loaded_dlls
1013 * Attach to the (builtin) dlls that have been implicitly loaded because
1014 * of a dependency at the Unix level, but not imported at the Win32 level.
1016 static void attach_implicitly_loaded_dlls( LPVOID reserved
)
1020 PLIST_ENTRY mark
, entry
;
1022 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1023 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1025 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1027 if (mod
->Flags
& (LDR_LOAD_IN_PROGRESS
| LDR_PROCESS_ATTACHED
)) continue;
1028 TRACE( "found implicitly loaded %s, attaching to it\n",
1029 debugstr_w(mod
->BaseDllName
.Buffer
));
1030 process_attach( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
), reserved
);
1031 break; /* restart the search from the start */
1033 if (entry
== mark
) break; /* nothing found */
1038 /*************************************************************************
1041 * Send DLL process detach notifications. See the comment about calling
1042 * sequence at process_attach. Unless the bForceDetach flag
1043 * is set, only DLLs with zero refcount are notified.
1045 static void process_detach( BOOL bForceDetach
, LPVOID lpReserved
)
1047 PLIST_ENTRY mark
, entry
;
1050 RtlEnterCriticalSection( &loader_section
);
1051 if (bForceDetach
) process_detaching
= 1;
1052 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1055 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
1057 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1058 InInitializationOrderModuleList
);
1059 /* Check whether to detach this DLL */
1060 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1062 if ( mod
->LoadCount
&& !bForceDetach
)
1065 /* Call detach notification */
1066 mod
->Flags
&= ~LDR_PROCESS_ATTACHED
;
1067 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1068 DLL_PROCESS_DETACH
, lpReserved
);
1070 /* Restart at head of WINE_MODREF list, as entries might have
1071 been added and/or removed while performing the call ... */
1074 } while (entry
!= mark
);
1076 RtlLeaveCriticalSection( &loader_section
);
1079 /*************************************************************************
1080 * MODULE_DllThreadAttach
1082 * Send DLL thread attach notifications. These are sent in the
1083 * reverse sequence of process detach notification.
1086 NTSTATUS
MODULE_DllThreadAttach( LPVOID lpReserved
)
1088 PLIST_ENTRY mark
, entry
;
1092 /* don't do any attach calls if process is exiting */
1093 if (process_detaching
) return STATUS_SUCCESS
;
1094 /* FIXME: there is still a race here */
1096 RtlEnterCriticalSection( &loader_section
);
1098 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto done
;
1100 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
1101 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1103 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
1104 InInitializationOrderModuleList
);
1105 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
1107 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
1110 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
1111 DLL_THREAD_ATTACH
, lpReserved
);
1115 RtlLeaveCriticalSection( &loader_section
);
1119 /******************************************************************
1120 * LdrDisableThreadCalloutsForDll (NTDLL.@)
1123 NTSTATUS WINAPI
LdrDisableThreadCalloutsForDll(HMODULE hModule
)
1126 NTSTATUS ret
= STATUS_SUCCESS
;
1128 RtlEnterCriticalSection( &loader_section
);
1130 wm
= get_modref( hModule
);
1131 if (!wm
|| wm
->ldr
.TlsIndex
!= -1)
1132 ret
= STATUS_DLL_NOT_FOUND
;
1134 wm
->ldr
.Flags
|= LDR_NO_DLL_CALLS
;
1136 RtlLeaveCriticalSection( &loader_section
);
1141 /******************************************************************
1142 * LdrFindEntryForAddress (NTDLL.@)
1144 * The loader_section must be locked while calling this function
1146 NTSTATUS WINAPI
LdrFindEntryForAddress(const void* addr
, PLDR_MODULE
* pmod
)
1148 PLIST_ENTRY mark
, entry
;
1151 mark
= &NtCurrentTeb()->Peb
->LdrData
->InMemoryOrderModuleList
;
1152 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1154 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InMemoryOrderModuleList
);
1155 if ((const void *)mod
->BaseAddress
<= addr
&&
1156 (const char *)addr
< (char*)mod
->BaseAddress
+ mod
->SizeOfImage
)
1159 return STATUS_SUCCESS
;
1161 if ((const void *)mod
->BaseAddress
> addr
) break;
1163 return STATUS_NO_MORE_ENTRIES
;
1166 /******************************************************************
1167 * LdrLockLoaderLock (NTDLL.@)
1169 * Note: flags are not implemented.
1170 * Flag 0x01 is used to raise exceptions on errors.
1171 * Flag 0x02 is used to avoid waiting on the section (does RtlTryEnterCriticalSection instead).
1173 NTSTATUS WINAPI
LdrLockLoaderLock( ULONG flags
, ULONG
*result
, ULONG
*magic
)
1175 if (flags
) FIXME( "flags %x not supported\n", flags
);
1177 if (result
) *result
= 1;
1178 if (!magic
) return STATUS_INVALID_PARAMETER_3
;
1179 RtlEnterCriticalSection( &loader_section
);
1180 *magic
= GetCurrentThreadId();
1181 return STATUS_SUCCESS
;
1185 /******************************************************************
1186 * LdrUnlockLoaderUnlock (NTDLL.@)
1188 NTSTATUS WINAPI
LdrUnlockLoaderLock( ULONG flags
, ULONG magic
)
1192 if (magic
!= GetCurrentThreadId()) return STATUS_INVALID_PARAMETER_2
;
1193 RtlLeaveCriticalSection( &loader_section
);
1195 return STATUS_SUCCESS
;
1199 /******************************************************************
1200 * LdrGetProcedureAddress (NTDLL.@)
1202 NTSTATUS WINAPI
LdrGetProcedureAddress(HMODULE module
, const ANSI_STRING
*name
,
1203 ULONG ord
, PVOID
*address
)
1205 IMAGE_EXPORT_DIRECTORY
*exports
;
1207 NTSTATUS ret
= STATUS_PROCEDURE_NOT_FOUND
;
1209 RtlEnterCriticalSection( &loader_section
);
1211 /* check if the module itself is invalid to return the proper error */
1212 if (!get_modref( module
)) ret
= STATUS_DLL_NOT_FOUND
;
1213 else if ((exports
= RtlImageDirectoryEntryToData( module
, TRUE
,
1214 IMAGE_DIRECTORY_ENTRY_EXPORT
, &exp_size
)))
1216 LPCWSTR load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1217 void *proc
= name
? find_named_export( module
, exports
, exp_size
, name
->Buffer
, -1, load_path
)
1218 : find_ordinal_export( module
, exports
, exp_size
, ord
- exports
->Base
, load_path
);
1222 ret
= STATUS_SUCCESS
;
1226 RtlLeaveCriticalSection( &loader_section
);
1231 /***********************************************************************
1234 * Check if a loaded native dll is a Wine fake dll.
1236 static BOOL
is_fake_dll( const void *base
)
1238 static const char fakedll_signature
[] = "Wine placeholder DLL";
1239 const IMAGE_DOS_HEADER
*dos
= base
;
1241 if (dos
->e_lfanew
>= sizeof(*dos
) + sizeof(fakedll_signature
) &&
1242 !memcmp( dos
+ 1, fakedll_signature
, sizeof(fakedll_signature
) )) return TRUE
;
1247 /***********************************************************************
1248 * get_builtin_fullname
1250 * Build the full pathname for a builtin dll.
1252 static WCHAR
*get_builtin_fullname( const WCHAR
*path
, const char *filename
)
1254 static const WCHAR soW
[] = {'.','s','o',0};
1255 WCHAR
*p
, *fullname
;
1256 size_t i
, len
= strlen(filename
);
1258 /* check if path can correspond to the dll we have */
1259 if (path
&& (p
= strrchrW( path
, '\\' )))
1262 for (i
= 0; i
< len
; i
++)
1263 if (tolowerW(p
[i
]) != tolowerW( (WCHAR
)filename
[i
]) ) break;
1264 if (i
== len
&& (!p
[len
] || !strcmpiW( p
+ len
, soW
)))
1266 /* the filename matches, use path as the full path */
1268 if ((fullname
= RtlAllocateHeap( GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
) )))
1270 memcpy( fullname
, path
, len
* sizeof(WCHAR
) );
1277 if ((fullname
= RtlAllocateHeap( GetProcessHeap(), 0,
1278 system_dir
.MaximumLength
+ (len
+ 1) * sizeof(WCHAR
) )))
1280 memcpy( fullname
, system_dir
.Buffer
, system_dir
.Length
);
1281 p
= fullname
+ system_dir
.Length
/ sizeof(WCHAR
);
1282 if (p
> fullname
&& p
[-1] != '\\') *p
++ = '\\';
1283 ascii_to_unicode( p
, filename
, len
+ 1 );
1289 /***********************************************************************
1290 * load_builtin_callback
1292 * Load a library in memory; callback function for wine_dll_register
1294 static void load_builtin_callback( void *module
, const char *filename
)
1296 static const WCHAR emptyW
[1];
1298 IMAGE_NT_HEADERS
*nt
;
1301 const WCHAR
*load_path
;
1306 ERR("could not map image for %s\n", filename
? filename
: "main exe" );
1309 if (!(nt
= RtlImageNtHeader( module
)))
1311 ERR( "bad module for %s\n", filename
? filename
: "main exe" );
1312 builtin_load_info
->status
= STATUS_INVALID_IMAGE_FORMAT
;
1316 size
= nt
->OptionalHeader
.SizeOfImage
;
1317 NtAllocateVirtualMemory( NtCurrentProcess(), &addr
, 0, &size
,
1318 MEM_SYSTEM
| MEM_IMAGE
, PAGE_EXECUTE_WRITECOPY
);
1319 /* create the MODREF */
1321 if (!(fullname
= get_builtin_fullname( builtin_load_info
->filename
, filename
)))
1323 ERR( "can't load %s\n", filename
);
1324 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1328 wm
= alloc_module( module
, fullname
);
1329 RtlFreeHeap( GetProcessHeap(), 0, fullname
);
1332 ERR( "can't load %s\n", filename
);
1333 builtin_load_info
->status
= STATUS_NO_MEMORY
;
1336 wm
->ldr
.Flags
|= LDR_WINE_INTERNAL
;
1338 if (!(nt
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
) &&
1339 !NtCurrentTeb()->Peb
->ImageBaseAddress
) /* if we already have an executable, ignore this one */
1341 NtCurrentTeb()->Peb
->ImageBaseAddress
= module
;
1347 load_path
= builtin_load_info
->load_path
;
1348 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
1349 if (!load_path
) load_path
= emptyW
;
1350 if (fixup_imports( wm
, load_path
) != STATUS_SUCCESS
)
1352 /* the module has only be inserted in the load & memory order lists */
1353 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1354 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1355 /* FIXME: free the modref */
1356 builtin_load_info
->status
= STATUS_DLL_NOT_FOUND
;
1361 builtin_load_info
->wm
= wm
;
1362 TRACE( "loaded %s %p %p\n", filename
, wm
, module
);
1364 /* send the DLL load event */
1366 SERVER_START_REQ( load_dll
)
1370 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1371 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1372 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1373 req
->name
= &wm
->ldr
.FullDllName
.Buffer
;
1374 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1375 wine_server_call( req
);
1379 /* setup relay debugging entry points */
1380 if (TRACE_ON(relay
)) RELAY_SetupDLL( module
);
1384 /***********************************************************************
1387 * Apply the relocations to a mapped PE image
1389 static int do_relocations( char *base
, const IMAGE_DATA_DIRECTORY
*dir
,
1390 int delta
, SIZE_T total_size
)
1392 IMAGE_BASE_RELOCATION
*rel
;
1394 TRACE_(module
)( "relocating from %p-%p to %p-%p\n",
1395 base
- delta
, base
- delta
+ total_size
, base
, base
+ total_size
);
1397 for (rel
= (IMAGE_BASE_RELOCATION
*)(base
+ dir
->VirtualAddress
);
1398 ((char *)rel
< base
+ dir
->VirtualAddress
+ dir
->Size
) && rel
->SizeOfBlock
;
1399 rel
= (IMAGE_BASE_RELOCATION
*)((char*)rel
+ rel
->SizeOfBlock
) )
1401 char *page
= base
+ rel
->VirtualAddress
;
1402 WORD
*TypeOffset
= (WORD
*)(rel
+ 1);
1403 int i
, count
= (rel
->SizeOfBlock
- sizeof(*rel
)) / sizeof(*TypeOffset
);
1405 if (!count
) continue;
1408 if ((char *)rel
+ rel
->SizeOfBlock
> base
+ dir
->VirtualAddress
+ dir
->Size
)
1410 ERR_(module
)("invalid relocation %p,%x,%d at %p,%x,%x\n",
1411 rel
, rel
->VirtualAddress
, rel
->SizeOfBlock
,
1412 base
, dir
->VirtualAddress
, dir
->Size
);
1416 if (page
> base
+ total_size
)
1418 WARN_(module
)("skipping %d relocations for page %p beyond module %p-%p\n",
1419 count
, page
, base
, base
+ total_size
);
1423 TRACE_(module
)("%d relocations for page %x\n", count
, rel
->VirtualAddress
);
1425 /* patching in reverse order */
1426 for (i
= 0 ; i
< count
; i
++)
1428 int offset
= TypeOffset
[i
] & 0xFFF;
1429 int type
= TypeOffset
[i
] >> 12;
1432 case IMAGE_REL_BASED_ABSOLUTE
:
1434 case IMAGE_REL_BASED_HIGH
:
1435 *(short*)(page
+offset
) += HIWORD(delta
);
1437 case IMAGE_REL_BASED_LOW
:
1438 *(short*)(page
+offset
) += LOWORD(delta
);
1440 case IMAGE_REL_BASED_HIGHLOW
:
1441 *(int*)(page
+offset
) += delta
;
1442 /* FIXME: if this is an exported address, fire up enhanced logic */
1445 FIXME_(module
)("Unknown/unsupported fixup type %d.\n", type
);
1453 /******************************************************************************
1454 * load_native_dll (internal)
1456 static NTSTATUS
load_native_dll( LPCWSTR load_path
, LPCWSTR name
, HANDLE file
,
1457 DWORD flags
, WINE_MODREF
** pwm
)
1462 IMAGE_NT_HEADERS
*nt
;
1468 TRACE("Trying native dll %s\n", debugstr_w(name
));
1472 status
= NtCreateSection( &mapping
, STANDARD_RIGHTS_REQUIRED
| SECTION_QUERY
| SECTION_MAP_READ
,
1473 NULL
, &size
, 0, SEC_IMAGE
, file
);
1474 if (status
!= STATUS_SUCCESS
) return status
;
1477 status
= NtMapViewOfSection( mapping
, NtCurrentProcess(),
1478 &module
, 0, 0, &size
, &len
, ViewShare
, 0, PAGE_READONLY
);
1481 if (status
!= STATUS_SUCCESS
&& status
!= STATUS_IMAGE_NOT_AT_BASE
)
1484 filename
= strrchrW(name
, '\\');
1488 wm
= find_basename_module(filename
);
1491 status
= NtAreMappedFilesTheSame(wm
->ldr
.BaseAddress
, module
);
1492 if(status
== STATUS_SUCCESS
)
1495 return STATUS_SUCCESS
;
1496 //FIXME: need to unmap?
1498 // FIXME what happens for other errors
1499 // maybe assert STATUS_NOT_SAME_DEVICE
1503 nt
= RtlImageNtHeader( module
);
1505 if(status
== STATUS_IMAGE_NOT_AT_BASE
)
1507 /* image was not loaded at preferred address -> need to relocate it */
1508 char *ptr
= (char*) module
;
1509 const IMAGE_DATA_DIRECTORY
*relocs
;
1511 IMAGE_SECTION_HEADER
*sec
;
1512 ULONG old_protections
[255];//FIXME
1518 nt
= RtlImageNtHeader( module
);
1519 base
= (char*)nt
->OptionalHeader
.ImageBase
;
1520 relocs
= &nt
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1522 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1523 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1526 address
= ptr
+ sec
->VirtualAddress
;//FIXME: virtual.c does some rounding/sanity checks on the parameters
1527 size
= sec
->Misc
.VirtualSize
;
1528 // FIXME("unprotecting %x, %p, %x\n", sec->Misc.VirtualSize, address, size);
1529 status
= NtProtectVirtualMemory(NtCurrentProcess(), &address
, &size
, PAGE_READWRITE
, &old_prot
);
1530 old_protections
[i
] = old_prot
;
1532 FIXME("NtProtectVirtualMemory failed with %x\n", status
);
1535 // FIXME("relocating DLL %s %p, %p %x\n", debugstr_w(name), ptr, base, ptr-base);
1536 // FIXME("len: %lx, size:%x%08x\n", len, size.u.HighPart, size.u.LowPart);
1537 do_relocations(ptr
, relocs
, ptr
- base
, len
);
1539 sec
= (IMAGE_SECTION_HEADER
*)((char*)&nt
->OptionalHeader
+nt
->FileHeader
.SizeOfOptionalHeader
);
1540 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
1543 address
= ptr
+ sec
->VirtualAddress
;//FIXME: virtual.c does some rounding/sanity checks on the parameters
1544 size
= sec
->Misc
.VirtualSize
;
1545 status
= NtProtectVirtualMemory(NtCurrentProcess(), &address
, &size
, old_protections
[i
], &old_prot
);
1547 FIXME("NtProtectVirtualMemory failed with %x\n", status
);
1551 if (is_fake_dll( module
))
1553 TRACE( "%s is a fake dll, not loading it\n", debugstr_w(name
) );
1554 NtUnmapViewOfSection( NtCurrentProcess(), module
);
1555 return STATUS_DLL_NOT_FOUND
;
1558 /* create the MODREF */
1560 if (!(wm
= alloc_module( module
, name
))) return STATUS_NO_MEMORY
;
1564 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
))
1566 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
)
1568 /* the module has only be inserted in the load & memory order lists */
1569 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
1570 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
1572 /* FIXME: there are several more dangling references
1573 * left. Including dlls loaded by this dll before the
1574 * failed one. Unrolling is rather difficult with the
1575 * current structure and we can leave them lying
1576 * around with no problems, so we don't care.
1577 * As these might reference our wm, we don't free it.
1583 /* send DLL load event */
1585 SERVER_START_REQ( load_dll
)
1589 req
->size
= nt
->OptionalHeader
.SizeOfImage
;
1590 req
->dbg_offset
= nt
->FileHeader
.PointerToSymbolTable
;
1591 req
->dbg_size
= nt
->FileHeader
.NumberOfSymbols
;
1592 req
->name
= &wm
->ldr
.FullDllName
.Buffer
;
1593 wine_server_add_data( req
, wm
->ldr
.FullDllName
.Buffer
, wm
->ldr
.FullDllName
.Length
);
1594 wine_server_call( req
);
1598 if ((wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
) && TRACE_ON(snoop
)) SNOOP_SetupDLL( module
);
1600 TRACE_(loaddll
)( "Loaded %s at %p: native\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
), module
);
1602 wm
->ldr
.LoadCount
= 1;
1604 return STATUS_SUCCESS
;
1608 /***********************************************************************
1611 static NTSTATUS
load_builtin_dll( LPCWSTR load_path
, LPCWSTR path
, HANDLE file
,
1612 DWORD flags
, WINE_MODREF
** pwm
)
1614 char error
[256], dllname
[MAX_PATH
];
1615 const WCHAR
*name
, *p
;
1617 void *handle
= NULL
;
1618 struct builtin_load_info info
, *prev_info
;
1620 /* Fix the name in case we have a full path and extension */
1622 if ((p
= strrchrW( name
, '\\' ))) name
= p
+ 1;
1623 if ((p
= strrchrW( name
, '/' ))) name
= p
+ 1;
1625 /* load_library will modify info.status. Note also that load_library can be
1626 * called several times, if the .so file we're loading has dependencies.
1627 * info.status will gather all the errors we may get while loading all these
1630 info
.load_path
= load_path
;
1631 info
.filename
= NULL
;
1632 info
.status
= STATUS_SUCCESS
;
1635 if (file
) /* we have a real file, try to load it */
1637 UNICODE_STRING nt_name
;
1638 ANSI_STRING unix_name
;
1640 TRACE("Trying built-in %s\n", debugstr_w(path
));
1642 if (!RtlDosPathNameToNtPathName_U( path
, &nt_name
, NULL
, NULL
))
1643 return STATUS_DLL_NOT_FOUND
;
1645 if (wine_nt_to_unix_file_name( &nt_name
, &unix_name
, FILE_OPEN
, FALSE
))
1647 RtlFreeUnicodeString( &nt_name
);
1648 return STATUS_DLL_NOT_FOUND
;
1650 prev_info
= builtin_load_info
;
1651 info
.filename
= nt_name
.Buffer
+ 4; /* skip \??\ */
1652 builtin_load_info
= &info
;
1653 handle
= wine_dlopen( unix_name
.Buffer
, RTLD_NOW
, error
, sizeof(error
) );
1654 builtin_load_info
= prev_info
;
1655 RtlFreeUnicodeString( &nt_name
);
1656 RtlFreeHeap( GetProcessHeap(), 0, unix_name
.Buffer
);
1659 WARN( "failed to load .so lib for builtin %s: %s\n", debugstr_w(path
), error
);
1660 return STATUS_INVALID_IMAGE_FORMAT
;
1667 TRACE("Trying built-in %s\n", debugstr_w(name
));
1669 /* we don't want to depend on the current codepage here */
1670 len
= strlenW( name
) + 1;
1671 if (len
>= sizeof(dllname
)) return STATUS_NAME_TOO_LONG
;
1672 for (i
= 0; i
< len
; i
++)
1674 if (name
[i
] > 127) return STATUS_DLL_NOT_FOUND
;
1675 dllname
[i
] = (char)name
[i
];
1676 if (dllname
[i
] >= 'A' && dllname
[i
] <= 'Z') dllname
[i
] += 'a' - 'A';
1679 prev_info
= builtin_load_info
;
1680 builtin_load_info
= &info
;
1681 handle
= wine_dll_load( dllname
, error
, sizeof(error
), &file_exists
);
1682 builtin_load_info
= prev_info
;
1687 /* The file does not exist -> WARN() */
1688 WARN("cannot open .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1689 return STATUS_DLL_NOT_FOUND
;
1691 /* ERR() for all other errors (missing functions, ...) */
1692 ERR("failed to load .so lib for builtin %s: %s\n", debugstr_w(name
), error
);
1693 return STATUS_PROCEDURE_NOT_FOUND
;
1697 if (info
.status
!= STATUS_SUCCESS
)
1699 wine_dll_unload( handle
);
1705 PLIST_ENTRY mark
, entry
;
1707 /* The constructor wasn't called, this means the .so is already
1708 * loaded under a different name. Try to find the wm for it. */
1710 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
1711 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
1713 LDR_MODULE
*mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
1714 if (mod
->Flags
& LDR_WINE_INTERNAL
&& mod
->SectionHandle
== handle
)
1716 info
.wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
1717 TRACE( "Found %s at %p for builtin %s\n",
1718 debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
, debugstr_w(path
) );
1722 wine_dll_unload( handle
); /* release the libdl refcount */
1723 if (!info
.wm
) return STATUS_INVALID_IMAGE_FORMAT
;
1724 if (info
.wm
->ldr
.LoadCount
!= -1) info
.wm
->ldr
.LoadCount
++;
1728 TRACE_(loaddll
)( "Loaded %s at %p: builtin\n", debugstr_w(info
.wm
->ldr
.FullDllName
.Buffer
), info
.wm
->ldr
.BaseAddress
);
1729 info
.wm
->ldr
.LoadCount
= 1;
1730 info
.wm
->ldr
.SectionHandle
= handle
;
1734 return STATUS_SUCCESS
;
1738 /***********************************************************************
1741 * Find the full path (if any) of the dll from the activation context.
1743 static NTSTATUS
find_actctx_dll( LPCWSTR libname
, LPWSTR
*fullname
)
1745 static const WCHAR winsxsW
[] = {'\\','w','i','n','s','x','s','\\'};
1746 static const WCHAR dotManifestW
[] = {'.','m','a','n','i','f','e','s','t',0};
1748 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
1749 ACTCTX_SECTION_KEYED_DATA data
;
1750 UNICODE_STRING nameW
;
1752 SIZE_T needed
, size
= 1024;
1755 RtlInitUnicodeString( &nameW
, libname
);
1756 data
.cbSize
= sizeof(data
);
1757 status
= RtlFindActivationContextSectionString( FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
1758 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
1760 if (status
!= STATUS_SUCCESS
) return status
;
1764 if (!(info
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
1766 status
= STATUS_NO_MEMORY
;
1769 status
= RtlQueryInformationActivationContext( 0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
1770 AssemblyDetailedInformationInActivationContext
,
1771 info
, size
, &needed
);
1772 if (status
== STATUS_SUCCESS
) break;
1773 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
1774 RtlFreeHeap( GetProcessHeap(), 0, info
);
1776 /* restart with larger buffer */
1779 if ((p
= strrchrW( info
->lpAssemblyManifestPath
, '\\' )))
1781 DWORD dirlen
= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1784 if (strncmpiW( p
, info
->lpAssemblyDirectoryName
, dirlen
) || strcmpiW( p
+ dirlen
, dotManifestW
))
1786 /* manifest name does not match directory name, so it's not a global
1787 * windows/winsxs manifest; use the manifest directory name instead */
1788 dirlen
= p
- info
->lpAssemblyManifestPath
;
1789 needed
= (dirlen
+ 1) * sizeof(WCHAR
) + nameW
.Length
;
1790 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1792 status
= STATUS_NO_MEMORY
;
1795 memcpy( p
, info
->lpAssemblyManifestPath
, dirlen
* sizeof(WCHAR
) );
1797 strcpyW( p
, libname
);
1802 needed
= (windows_dir
.Length
+ sizeof(winsxsW
) + info
->ulAssemblyDirectoryNameLength
+
1803 nameW
.Length
+ 2*sizeof(WCHAR
));
1805 if (!(*fullname
= p
= RtlAllocateHeap( GetProcessHeap(), 0, needed
)))
1807 status
= STATUS_NO_MEMORY
;
1810 memcpy( p
, windows_dir
.Buffer
, windows_dir
.Length
);
1811 p
+= windows_dir
.Length
/ sizeof(WCHAR
);
1812 memcpy( p
, winsxsW
, sizeof(winsxsW
) );
1813 p
+= sizeof(winsxsW
) / sizeof(WCHAR
);
1814 memcpy( p
, info
->lpAssemblyDirectoryName
, info
->ulAssemblyDirectoryNameLength
);
1815 p
+= info
->ulAssemblyDirectoryNameLength
/ sizeof(WCHAR
);
1817 strcpyW( p
, libname
);
1819 RtlFreeHeap( GetProcessHeap(), 0, info
);
1820 RtlReleaseActivationContext( data
.hActCtx
);
1825 /***********************************************************************
1828 * Find the file (or already loaded module) for a given dll name.
1830 static NTSTATUS
find_dll_file( const WCHAR
*load_path
, const WCHAR
*libname
,
1831 WCHAR
*filename
, ULONG
*size
, WINE_MODREF
**pwm
, HANDLE
*handle
)
1833 OBJECT_ATTRIBUTES attr
;
1835 UNICODE_STRING nt_name
;
1836 WCHAR
*file_part
, *ext
, *dllname
;
1839 /* first append .dll if needed */
1842 if (!(ext
= strrchrW( libname
, '.')) || strchrW( ext
, '/' ) || strchrW( ext
, '\\'))
1844 if (!(dllname
= RtlAllocateHeap( GetProcessHeap(), 0,
1845 (strlenW(libname
) * sizeof(WCHAR
)) + sizeof(dllW
) )))
1846 return STATUS_NO_MEMORY
;
1847 strcpyW( dllname
, libname
);
1848 strcatW( dllname
, dllW
);
1852 nt_name
.Buffer
= NULL
;
1854 if (!contains_path( libname
))
1857 WCHAR
*fullname
= NULL
;
1859 if ((*pwm
= find_basename_module( libname
)) != NULL
) goto found
;
1861 status
= find_actctx_dll( libname
, &fullname
);
1862 if (status
== STATUS_SUCCESS
)
1864 TRACE ("found %s for %s\n", debugstr_w(fullname
), debugstr_w(libname
) );
1865 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1866 libname
= dllname
= fullname
;
1868 else if (status
!= STATUS_SXS_KEY_NOT_FOUND
)
1870 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1875 if (RtlDetermineDosPathNameType_U( libname
) == RELATIVE_PATH
)
1877 /* we need to search for it */
1878 len
= RtlDosSearchPath_U( load_path
, libname
, NULL
, *size
, filename
, &file_part
);
1881 if (len
>= *size
) goto overflow
;
1882 if ((*pwm
= find_fullname_module( filename
)) || !handle
) goto found
;
1884 if (!RtlDosPathNameToNtPathName_U( filename
, &nt_name
, NULL
, NULL
))
1886 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1887 return STATUS_NO_MEMORY
;
1889 attr
.Length
= sizeof(attr
);
1890 attr
.RootDirectory
= 0;
1891 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1892 attr
.ObjectName
= &nt_name
;
1893 attr
.SecurityDescriptor
= NULL
;
1894 attr
.SecurityQualityOfService
= NULL
;
1895 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1901 if (!contains_path( libname
))
1903 /* if libname doesn't contain a path at all, we simply return the name as is,
1904 * to be loaded as builtin */
1905 len
= strlenW(libname
) * sizeof(WCHAR
);
1906 if (len
>= *size
) goto overflow
;
1907 strcpyW( filename
, libname
);
1912 /* absolute path name, or relative path name but not found above */
1914 if (!RtlDosPathNameToNtPathName_U( libname
, &nt_name
, &file_part
, NULL
))
1916 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1917 return STATUS_NO_MEMORY
;
1919 len
= nt_name
.Length
- 4*sizeof(WCHAR
); /* for \??\ prefix */
1920 if (len
>= *size
) goto overflow
;
1921 memcpy( filename
, nt_name
.Buffer
+ 4, len
+ sizeof(WCHAR
) );
1922 if (!(*pwm
= find_fullname_module( filename
)) && handle
)
1924 attr
.Length
= sizeof(attr
);
1925 attr
.RootDirectory
= 0;
1926 attr
.Attributes
= OBJ_CASE_INSENSITIVE
;
1927 attr
.ObjectName
= &nt_name
;
1928 attr
.SecurityDescriptor
= NULL
;
1929 attr
.SecurityQualityOfService
= NULL
;
1930 if (NtOpenFile( handle
, GENERIC_READ
, &attr
, &io
, FILE_SHARE_READ
|FILE_SHARE_DELETE
, 0 )) *handle
= 0;
1933 RtlFreeUnicodeString( &nt_name
);
1934 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1935 return STATUS_SUCCESS
;
1938 RtlFreeUnicodeString( &nt_name
);
1939 RtlFreeHeap( GetProcessHeap(), 0, dllname
);
1940 *size
= len
+ sizeof(WCHAR
);
1941 return STATUS_BUFFER_TOO_SMALL
;
1945 /***********************************************************************
1946 * load_dll (internal)
1948 * Load a PE style module according to the load order.
1949 * The loader_section must be locked while calling this function.
1951 static NTSTATUS
load_dll( LPCWSTR load_path
, LPCWSTR libname
, DWORD flags
, WINE_MODREF
** pwm
)
1953 enum loadorder loadorder
;
1957 WINE_MODREF
*main_exe
;
1961 TRACE( "looking for %s in %s\n", debugstr_w(libname
), debugstr_w(load_path
) );
1965 size
= sizeof(buffer
);
1968 nts
= find_dll_file( load_path
, libname
, filename
, &size
, pwm
, &handle
);
1969 if (nts
== STATUS_SUCCESS
) break;
1970 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1971 if (nts
!= STATUS_BUFFER_TOO_SMALL
) return nts
;
1972 /* grow the buffer and retry */
1973 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
))) return STATUS_NO_MEMORY
;
1976 if (*pwm
) /* found already loaded module */
1978 if ((*pwm
)->ldr
.LoadCount
!= -1) (*pwm
)->ldr
.LoadCount
++;
1980 if (!(flags
& DONT_RESOLVE_DLL_REFERENCES
)) fixup_imports( *pwm
, load_path
);
1982 TRACE("Found %s for %s at %p, count=%d\n",
1983 debugstr_w((*pwm
)->ldr
.FullDllName
.Buffer
), debugstr_w(libname
),
1984 (*pwm
)->ldr
.BaseAddress
, (*pwm
)->ldr
.LoadCount
);
1985 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
1986 return STATUS_SUCCESS
;
1989 main_exe
= get_modref( NtCurrentTeb()->Peb
->ImageBaseAddress
);
1990 loadorder
= get_load_order( main_exe
? main_exe
->ldr
.BaseDllName
.Buffer
: NULL
, filename
);
1995 nts
= STATUS_NO_MEMORY
;
1998 nts
= STATUS_DLL_NOT_FOUND
;
2001 case LO_NATIVE_BUILTIN
:
2002 if (!handle
) nts
= STATUS_DLL_NOT_FOUND
;
2005 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
2006 if (nts
== STATUS_INVALID_FILE_FOR_SECTION
)
2007 /* not in PE format, maybe it's a builtin */
2008 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
2010 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
== LO_NATIVE_BUILTIN
)
2011 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
2014 case LO_BUILTIN_NATIVE
:
2015 case LO_DEFAULT
: /* default is builtin,native */
2016 nts
= load_builtin_dll( load_path
, filename
, handle
, flags
, pwm
);
2017 if (!handle
) break; /* nothing else we can try */
2018 /* file is not a builtin library, try without using the specified file */
2019 if (nts
!= STATUS_SUCCESS
)
2020 nts
= load_builtin_dll( load_path
, filename
, 0, flags
, pwm
);
2021 if (nts
== STATUS_SUCCESS
&& loadorder
== LO_DEFAULT
&&
2022 !MODULE_InitDLL( *pwm
, DLL_WINE_PREATTACH
, NULL
))
2024 /* stub-only dll, try native */
2025 TRACE( "%s pre-attach returned FALSE, preferring native\n", debugstr_w(filename
) );
2026 LdrUnloadDll( (*pwm
)->ldr
.BaseAddress
);
2027 nts
= STATUS_DLL_NOT_FOUND
;
2029 if (nts
== STATUS_DLL_NOT_FOUND
&& loadorder
!= LO_BUILTIN
)
2030 nts
= load_native_dll( load_path
, filename
, handle
, flags
, pwm
);
2034 if (nts
== STATUS_SUCCESS
)
2036 /* Initialize DLL just loaded */
2037 TRACE("Loaded module %s (%s) at %p\n", debugstr_w(filename
),
2038 ((*pwm
)->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native",
2039 (*pwm
)->ldr
.BaseAddress
);
2040 if (handle
) NtClose( handle
);
2041 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2045 WARN("Failed to load module %s; status=%x\n", debugstr_w(libname
), nts
);
2046 if (handle
) NtClose( handle
);
2047 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2051 /******************************************************************
2052 * LdrLoadDll (NTDLL.@)
2054 NTSTATUS WINAPI
LdrLoadDll(LPCWSTR path_name
, DWORD flags
,
2055 const UNICODE_STRING
*libname
, HMODULE
* hModule
)
2060 RtlEnterCriticalSection( &loader_section
);
2062 if (!path_name
) path_name
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2063 nts
= load_dll( path_name
, libname
->Buffer
, flags
, &wm
);
2065 if (nts
== STATUS_SUCCESS
&& !(wm
->ldr
.Flags
& LDR_DONT_RESOLVE_REFS
))
2067 nts
= process_attach( wm
, NULL
);
2068 if (nts
!= STATUS_SUCCESS
)
2070 LdrUnloadDll(wm
->ldr
.BaseAddress
);
2074 *hModule
= (wm
) ? wm
->ldr
.BaseAddress
: NULL
;
2076 RtlLeaveCriticalSection( &loader_section
);
2081 /******************************************************************
2082 * LdrGetDllHandle (NTDLL.@)
2084 NTSTATUS WINAPI
LdrGetDllHandle( LPCWSTR load_path
, ULONG flags
, const UNICODE_STRING
*name
, HMODULE
*base
)
2092 RtlEnterCriticalSection( &loader_section
);
2094 if (!load_path
) load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2097 size
= sizeof(buffer
);
2100 status
= find_dll_file( load_path
, name
->Buffer
, filename
, &size
, &wm
, NULL
);
2101 if (filename
!= buffer
) RtlFreeHeap( GetProcessHeap(), 0, filename
);
2102 if (status
!= STATUS_BUFFER_TOO_SMALL
) break;
2103 /* grow the buffer and retry */
2104 if (!(filename
= RtlAllocateHeap( GetProcessHeap(), 0, size
)))
2106 status
= STATUS_NO_MEMORY
;
2111 if (status
== STATUS_SUCCESS
)
2113 if (wm
) *base
= wm
->ldr
.BaseAddress
;
2114 else status
= STATUS_DLL_NOT_FOUND
;
2117 RtlLeaveCriticalSection( &loader_section
);
2118 TRACE( "%s -> %p (load path %s)\n", debugstr_us(name
), status
? NULL
: *base
, debugstr_w(load_path
) );
2123 /******************************************************************
2124 * LdrAddRefDll (NTDLL.@)
2126 NTSTATUS WINAPI
LdrAddRefDll( ULONG flags
, HMODULE module
)
2128 NTSTATUS ret
= STATUS_SUCCESS
;
2131 if (flags
) FIXME( "%p flags %x not implemented\n", module
, flags
);
2133 RtlEnterCriticalSection( &loader_section
);
2135 if ((wm
= get_modref( module
)))
2137 if (wm
->ldr
.LoadCount
!= -1) wm
->ldr
.LoadCount
++;
2138 TRACE( "(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2140 else ret
= STATUS_INVALID_PARAMETER
;
2142 RtlLeaveCriticalSection( &loader_section
);
2147 /******************************************************************
2148 * LdrQueryProcessModuleInformation
2151 NTSTATUS WINAPI
LdrQueryProcessModuleInformation(PSYSTEM_MODULE_INFORMATION smi
,
2152 ULONG buf_size
, ULONG
* req_size
)
2154 SYSTEM_MODULE
* sm
= &smi
->Modules
[0];
2155 ULONG size
= sizeof(ULONG
);
2156 NTSTATUS nts
= STATUS_SUCCESS
;
2159 PLIST_ENTRY mark
, entry
;
2163 smi
->ModulesCount
= 0;
2165 RtlEnterCriticalSection( &loader_section
);
2166 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2167 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2169 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2170 size
+= sizeof(*sm
);
2171 if (size
<= buf_size
)
2173 sm
->Reserved1
= 0; /* FIXME */
2174 sm
->Reserved2
= 0; /* FIXME */
2175 sm
->ImageBaseAddress
= mod
->BaseAddress
;
2176 sm
->ImageSize
= mod
->SizeOfImage
;
2177 sm
->Flags
= mod
->Flags
;
2179 sm
->Rank
= 0; /* FIXME */
2180 sm
->Unknown
= 0; /* FIXME */
2182 str
.MaximumLength
= MAXIMUM_FILENAME_LENGTH
;
2183 str
.Buffer
= (char*)sm
->Name
;
2184 RtlUnicodeStringToAnsiString(&str
, &mod
->FullDllName
, FALSE
);
2185 ptr
= strrchr(str
.Buffer
, '\\');
2186 sm
->NameOffset
= (ptr
!= NULL
) ? (ptr
- str
.Buffer
+ 1) : 0;
2188 smi
->ModulesCount
++;
2191 else nts
= STATUS_INFO_LENGTH_MISMATCH
;
2193 RtlLeaveCriticalSection( &loader_section
);
2195 if (req_size
) *req_size
= size
;
2201 /******************************************************************
2202 * RtlDllShutdownInProgress (NTDLL.@)
2204 BOOLEAN WINAPI
RtlDllShutdownInProgress(void)
2206 return process_detaching
;
2210 /******************************************************************
2211 * LdrShutdownProcess (NTDLL.@)
2214 void WINAPI
LdrShutdownProcess(void)
2217 process_detach( TRUE
, (LPVOID
)1 );
2220 /******************************************************************
2221 * LdrShutdownThread (NTDLL.@)
2224 void WINAPI
LdrShutdownThread(void)
2226 PLIST_ENTRY mark
, entry
;
2231 /* don't do any detach calls if process is exiting */
2232 if (process_detaching
) return;
2233 /* FIXME: there is still a race here */
2235 RtlEnterCriticalSection( &loader_section
);
2237 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2238 for (entry
= mark
->Blink
; entry
!= mark
; entry
= entry
->Blink
)
2240 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
,
2241 InInitializationOrderModuleList
);
2242 if ( !(mod
->Flags
& LDR_PROCESS_ATTACHED
) )
2244 if ( mod
->Flags
& LDR_NO_DLL_CALLS
)
2247 MODULE_InitDLL( CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
),
2248 DLL_THREAD_DETACH
, NULL
);
2251 RtlLeaveCriticalSection( &loader_section
);
2252 RtlFreeHeap( GetProcessHeap(), 0, NtCurrentTeb()->ThreadLocalStoragePointer
);
2256 /***********************************************************************
2260 static void free_modref( WINE_MODREF
*wm
)
2262 RemoveEntryList(&wm
->ldr
.InLoadOrderModuleList
);
2263 RemoveEntryList(&wm
->ldr
.InMemoryOrderModuleList
);
2264 if (wm
->ldr
.InInitializationOrderModuleList
.Flink
)
2265 RemoveEntryList(&wm
->ldr
.InInitializationOrderModuleList
);
2267 TRACE(" unloading %s\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
));
2268 if (!TRACE_ON(module
))
2269 TRACE_(loaddll
)("Unloaded module %s : %s\n",
2270 debugstr_w(wm
->ldr
.FullDllName
.Buffer
),
2271 (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) ? "builtin" : "native" );
2273 SERVER_START_REQ( unload_dll
)
2275 req
->base
= wm
->ldr
.BaseAddress
;
2276 wine_server_call( req
);
2280 RtlReleaseActivationContext( wm
->ldr
.ActivationContext
);
2281 NtUnmapViewOfSection( NtCurrentProcess(), wm
->ldr
.BaseAddress
);
2282 if (wm
->ldr
.Flags
& LDR_WINE_INTERNAL
) wine_dll_unload( wm
->ldr
.SectionHandle
);
2283 if (cached_modref
== wm
) cached_modref
= NULL
;
2284 RtlFreeUnicodeString( &wm
->ldr
.FullDllName
);
2285 RtlFreeHeap( GetProcessHeap(), 0, wm
->deps
);
2286 RtlFreeHeap( GetProcessHeap(), 0, wm
);
2289 /***********************************************************************
2290 * MODULE_FlushModrefs
2292 * Remove all unused modrefs and call the internal unloading routines
2293 * for the library type.
2295 * The loader_section must be locked while calling this function.
2297 static void MODULE_FlushModrefs(void)
2299 PLIST_ENTRY mark
, entry
, prev
;
2303 mark
= &NtCurrentTeb()->Peb
->LdrData
->InInitializationOrderModuleList
;
2304 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2306 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InInitializationOrderModuleList
);
2307 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2308 prev
= entry
->Blink
;
2309 if (!mod
->LoadCount
) free_modref( wm
);
2312 /* check load order list too for modules that haven't been initialized yet */
2313 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2314 for (entry
= mark
->Blink
; entry
!= mark
; entry
= prev
)
2316 mod
= CONTAINING_RECORD(entry
, LDR_MODULE
, InLoadOrderModuleList
);
2317 wm
= CONTAINING_RECORD(mod
, WINE_MODREF
, ldr
);
2318 prev
= entry
->Blink
;
2319 if (!mod
->LoadCount
) free_modref( wm
);
2323 /***********************************************************************
2324 * MODULE_DecRefCount
2326 * The loader_section must be locked while calling this function.
2328 static void MODULE_DecRefCount( WINE_MODREF
*wm
)
2332 if ( wm
->ldr
.Flags
& LDR_UNLOAD_IN_PROGRESS
)
2335 if ( wm
->ldr
.LoadCount
<= 0 )
2338 --wm
->ldr
.LoadCount
;
2339 TRACE("(%s) ldr.LoadCount: %d\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
), wm
->ldr
.LoadCount
);
2341 if ( wm
->ldr
.LoadCount
== 0 )
2343 wm
->ldr
.Flags
|= LDR_UNLOAD_IN_PROGRESS
;
2345 for ( i
= 0; i
< wm
->nDeps
; i
++ )
2347 MODULE_DecRefCount( wm
->deps
[i
] );
2349 wm
->ldr
.Flags
&= ~LDR_UNLOAD_IN_PROGRESS
;
2353 /******************************************************************
2354 * LdrUnloadDll (NTDLL.@)
2358 NTSTATUS WINAPI
LdrUnloadDll( HMODULE hModule
)
2360 NTSTATUS retv
= STATUS_SUCCESS
;
2362 TRACE("(%p)\n", hModule
);
2364 RtlEnterCriticalSection( &loader_section
);
2366 /* if we're stopping the whole process (and forcing the removal of all
2367 * DLLs) the library will be freed anyway
2369 if (!process_detaching
)
2374 if ((wm
= get_modref( hModule
)) != NULL
)
2376 TRACE("(%s) - START\n", debugstr_w(wm
->ldr
.BaseDllName
.Buffer
));
2378 /* Recursively decrement reference counts */
2379 MODULE_DecRefCount( wm
);
2381 /* Call process detach notifications */
2382 if ( free_lib_count
<= 1 )
2384 process_detach( FALSE
, NULL
);
2385 MODULE_FlushModrefs();
2391 retv
= STATUS_DLL_NOT_FOUND
;
2396 RtlLeaveCriticalSection( &loader_section
);
2401 /***********************************************************************
2402 * RtlImageNtHeader (NTDLL.@)
2404 PIMAGE_NT_HEADERS WINAPI
RtlImageNtHeader(HMODULE hModule
)
2406 IMAGE_NT_HEADERS
*ret
;
2410 IMAGE_DOS_HEADER
*dos
= (IMAGE_DOS_HEADER
*)hModule
;
2413 if (dos
->e_magic
== IMAGE_DOS_SIGNATURE
)
2415 ret
= (IMAGE_NT_HEADERS
*)((char *)dos
+ dos
->e_lfanew
);
2416 if (ret
->Signature
!= IMAGE_NT_SIGNATURE
) ret
= NULL
;
2428 /******************************************************************
2429 * LdrInitializeThunk (NTDLL.@)
2432 void WINAPI
LdrInitializeThunk( ULONG unknown1
, ULONG unknown2
, ULONG unknown3
, ULONG unknown4
)
2437 PEB
*peb
= NtCurrentTeb()->Peb
;
2438 IMAGE_NT_HEADERS
*nt
= RtlImageNtHeader( peb
->ImageBaseAddress
);
2440 if (main_exe_file
) NtClose( main_exe_file
); /* at this point the main module is created */
2442 /* allocate the modref for the main exe (if not already done) */
2443 wm
= get_modref( peb
->ImageBaseAddress
);
2445 if (wm
->ldr
.Flags
& LDR_IMAGE_IS_DLL
)
2447 ERR("%s is a dll, not an executable\n", debugstr_w(wm
->ldr
.FullDllName
.Buffer
) );
2451 peb
->LoaderLock
= &loader_section
;
2452 peb
->ProcessParameters
->ImagePathName
= wm
->ldr
.FullDllName
;
2453 version_init( wm
->ldr
.FullDllName
.Buffer
);
2455 /* the main exe needs to be the first in the load order list */
2456 RemoveEntryList( &wm
->ldr
.InLoadOrderModuleList
);
2457 InsertHeadList( &peb
->LdrData
->InLoadOrderModuleList
, &wm
->ldr
.InLoadOrderModuleList
);
2459 status
= server_init_process_done();
2460 if (status
!= STATUS_SUCCESS
) goto error
;
2462 RtlEnterCriticalSection( &loader_section
);
2465 load_path
= NtCurrentTeb()->Peb
->ProcessParameters
->DllPath
.Buffer
;
2466 if ((status
= fixup_imports( wm
, load_path
)) != STATUS_SUCCESS
) goto error
;
2467 if ((status
= alloc_process_tls()) != STATUS_SUCCESS
) goto error
;
2468 if ((status
= alloc_thread_tls()) != STATUS_SUCCESS
) goto error
;
2470 pthread_functions
.sigprocmask( SIG_UNBLOCK
, &server_block_set
, NULL
);
2472 if ((status
= process_attach( wm
, (LPVOID
)1 )) != STATUS_SUCCESS
)
2474 if (last_failed_modref
)
2475 ERR( "%s failed to initialize, aborting\n", debugstr_w(last_failed_modref
->ldr
.BaseDllName
.Buffer
) + 1 );
2478 attach_implicitly_loaded_dlls( (LPVOID
)1 );
2480 RtlLeaveCriticalSection( &loader_section
);
2482 if (nt
->FileHeader
.Characteristics
& IMAGE_FILE_LARGE_ADDRESS_AWARE
) VIRTUAL_UseLargeAddressSpace();
2486 ERR( "Main exe initialization for %s failed, status %x\n",
2487 debugstr_w(peb
->ProcessParameters
->ImagePathName
.Buffer
), status
);
2488 NtTerminateProcess( GetCurrentProcess(), status
);
2492 /***********************************************************************
2493 * RtlImageDirectoryEntryToData (NTDLL.@)
2495 PVOID WINAPI
RtlImageDirectoryEntryToData( HMODULE module
, BOOL image
, WORD dir
, ULONG
*size
)
2497 const IMAGE_NT_HEADERS
*nt
;
2500 if ((ULONG_PTR
)module
& 1) /* mapped as data file */
2502 module
= (HMODULE
)((ULONG_PTR
)module
& ~1);
2505 if (!(nt
= RtlImageNtHeader( module
))) return NULL
;
2506 if (dir
>= nt
->OptionalHeader
.NumberOfRvaAndSizes
) return NULL
;
2507 if (!(addr
= nt
->OptionalHeader
.DataDirectory
[dir
].VirtualAddress
)) return NULL
;
2508 *size
= nt
->OptionalHeader
.DataDirectory
[dir
].Size
;
2509 if (image
|| addr
< nt
->OptionalHeader
.SizeOfHeaders
) return (char *)module
+ addr
;
2511 /* not mapped as image, need to find the section containing the virtual address */
2512 return RtlImageRvaToVa( nt
, module
, addr
, NULL
);
2516 /***********************************************************************
2517 * RtlImageRvaToSection (NTDLL.@)
2519 PIMAGE_SECTION_HEADER WINAPI
RtlImageRvaToSection( const IMAGE_NT_HEADERS
*nt
,
2520 HMODULE module
, DWORD rva
)
2523 const IMAGE_SECTION_HEADER
*sec
;
2525 sec
= (const IMAGE_SECTION_HEADER
*)((const char*)&nt
->OptionalHeader
+
2526 nt
->FileHeader
.SizeOfOptionalHeader
);
2527 for (i
= 0; i
< nt
->FileHeader
.NumberOfSections
; i
++, sec
++)
2529 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2530 return (PIMAGE_SECTION_HEADER
)sec
;
2536 /***********************************************************************
2537 * RtlImageRvaToVa (NTDLL.@)
2539 PVOID WINAPI
RtlImageRvaToVa( const IMAGE_NT_HEADERS
*nt
, HMODULE module
,
2540 DWORD rva
, IMAGE_SECTION_HEADER
**section
)
2542 IMAGE_SECTION_HEADER
*sec
;
2544 if (section
&& *section
) /* try this section first */
2547 if ((sec
->VirtualAddress
<= rva
) && (sec
->VirtualAddress
+ sec
->SizeOfRawData
> rva
))
2550 if (!(sec
= RtlImageRvaToSection( nt
, module
, rva
))) return NULL
;
2552 if (section
) *section
= sec
;
2553 return (char *)module
+ sec
->PointerToRawData
+ (rva
- sec
->VirtualAddress
);
2557 /***********************************************************************
2558 * RtlPcToFileHeader (NTDLL.@)
2560 PVOID WINAPI
RtlPcToFileHeader( PVOID pc
, PVOID
*address
)
2565 RtlEnterCriticalSection( &loader_section
);
2566 if (!LdrFindEntryForAddress( pc
, &module
)) ret
= module
->BaseAddress
;
2567 RtlLeaveCriticalSection( &loader_section
);
2573 /***********************************************************************
2574 * NtLoadDriver (NTDLL.@)
2575 * ZwLoadDriver (NTDLL.@)
2577 NTSTATUS WINAPI
NtLoadDriver( const UNICODE_STRING
*DriverServiceName
)
2579 FIXME("(%p), stub!\n",DriverServiceName
);
2580 return STATUS_NOT_IMPLEMENTED
;
2584 /***********************************************************************
2585 * NtUnloadDriver (NTDLL.@)
2586 * ZwUnloadDriver (NTDLL.@)
2588 NTSTATUS WINAPI
NtUnloadDriver( const UNICODE_STRING
*DriverServiceName
)
2590 FIXME("(%p), stub!\n",DriverServiceName
);
2591 return STATUS_NOT_IMPLEMENTED
;
2595 /******************************************************************
2598 BOOL WINAPI
DllMain( HINSTANCE inst
, DWORD reason
, LPVOID reserved
)
2600 if (reason
== DLL_PROCESS_ATTACH
) LdrDisableThreadCalloutsForDll( inst
);
2605 /******************************************************************
2606 * __wine_init_windows_dir (NTDLL.@)
2608 * Windows and system dir initialization once kernel32 has been loaded.
2610 void __wine_init_windows_dir( const WCHAR
*windir
, const WCHAR
*sysdir
)
2612 PLIST_ENTRY mark
, entry
;
2615 RtlCreateUnicodeString( &windows_dir
, windir
);
2616 RtlCreateUnicodeString( &system_dir
, sysdir
);
2617 strcpyW( user_shared_data
->NtSystemRoot
, windir
);
2619 /* prepend the system dir to the name of the already created modules */
2620 mark
= &NtCurrentTeb()->Peb
->LdrData
->InLoadOrderModuleList
;
2621 for (entry
= mark
->Flink
; entry
!= mark
; entry
= entry
->Flink
)
2623 LDR_MODULE
*mod
= CONTAINING_RECORD( entry
, LDR_MODULE
, InLoadOrderModuleList
);
2625 assert( mod
->Flags
& LDR_WINE_INTERNAL
);
2627 buffer
= RtlAllocateHeap( GetProcessHeap(), 0,
2628 system_dir
.Length
+ mod
->FullDllName
.Length
+ 2*sizeof(WCHAR
) );
2629 if (!buffer
) continue;
2630 strcpyW( buffer
, system_dir
.Buffer
);
2631 p
= buffer
+ strlenW( buffer
);
2632 if (p
> buffer
&& p
[-1] != '\\') *p
++ = '\\';
2633 strcpyW( p
, mod
->FullDllName
.Buffer
);
2634 RtlInitUnicodeString( &mod
->FullDllName
, buffer
);
2635 RtlInitUnicodeString( &mod
->BaseDllName
, p
);
2640 /***********************************************************************
2641 * __wine_process_init
2643 void __wine_process_init(void)
2645 static const WCHAR kernel32W
[] = {'k','e','r','n','e','l','3','2','.','d','l','l',0};
2649 ANSI_STRING func_name
;
2650 void (* DECLSPEC_NORETURN init_func
)(void);
2651 extern mode_t FILE_umask
;
2653 main_exe_file
= thread_init();
2655 criticalsection_init( );
2657 /* retrieve current umask */
2658 FILE_umask
= umask(0777);
2659 umask( FILE_umask
);
2661 /* setup the load callback and create ntdll modref */
2662 wine_dll_set_callback( load_builtin_callback
);
2664 if ((status
= load_builtin_dll( NULL
, kernel32W
, 0, 0, &wm
)) != STATUS_SUCCESS
)
2666 MESSAGE( "wine: could not load kernel32.dll, status %x\n", status
);
2669 RtlInitAnsiString( &func_name
, "__wine_kernel_init" );
2670 if ((status
= LdrGetProcedureAddress( wm
->ldr
.BaseAddress
, &func_name
,
2671 0, (void **)&init_func
)) != STATUS_SUCCESS
)
2673 MESSAGE( "wine: could not find __wine_kernel_init in kernel32.dll, status %x\n", status
);