2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS NT User-Mode Library
4 * FILE: dll/ntdll/ldr/ldrinit.c
5 * PURPOSE: User-Mode Process/Thread Startup
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
7 * Aleksey Bragin (aleksey@reactos.org)
10 /* INCLUDES *****************************************************************/
18 /* GLOBALS *******************************************************************/
20 HANDLE ImageExecOptionsKey
;
21 HANDLE Wow64ExecOptionsKey
;
22 UNICODE_STRING ImageExecOptionsString
= RTL_CONSTANT_STRING(L
"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options");
23 UNICODE_STRING Wow64OptionsString
= RTL_CONSTANT_STRING(L
"");
24 UNICODE_STRING NtDllString
= RTL_CONSTANT_STRING(L
"ntdll.dll");
26 BOOLEAN LdrpInLdrInit
;
27 LONG LdrpProcessInitialized
;
28 BOOLEAN LdrpLoaderLockInit
;
29 BOOLEAN LdrpLdrDatabaseIsSetup
;
30 BOOLEAN LdrpShutdownInProgress
;
31 HANDLE LdrpShutdownThreadId
;
33 BOOLEAN LdrpDllValidation
;
35 PLDR_DATA_TABLE_ENTRY LdrpImageEntry
;
36 PUNICODE_STRING LdrpTopLevelDllBeingLoaded
;
37 WCHAR StringBuffer
[156];
38 extern PTEB LdrpTopLevelDllBeingLoadedTeb
; // defined in rtlsupp.c!
39 PLDR_DATA_TABLE_ENTRY LdrpCurrentDllInitializer
;
40 PLDR_DATA_TABLE_ENTRY LdrpNtDllDataTableEntry
;
43 RTL_BITMAP TlsExpansionBitMap
;
45 BOOLEAN LdrpImageHasTls
;
46 LIST_ENTRY LdrpTlsList
;
47 ULONG LdrpNumberOfTlsEntries
;
48 ULONG LdrpNumberOfProcessors
;
50 extern LARGE_INTEGER RtlpTimeout
;
51 BOOLEAN RtlpTimeoutDisable
;
52 LIST_ENTRY LdrpHashTable
[LDR_HASH_TABLE_ENTRIES
];
53 LIST_ENTRY LdrpDllNotificationList
;
54 HANDLE LdrpKnownDllObjectDirectory
;
55 UNICODE_STRING LdrpKnownDllPath
;
56 WCHAR LdrpKnownDllPathBuffer
[128];
57 UNICODE_STRING LdrpDefaultPath
;
61 RTL_CRITICAL_SECTION_DEBUG LdrpLoaderLockDebug
;
62 RTL_CRITICAL_SECTION LdrpLoaderLock
=
71 RTL_CRITICAL_SECTION FastPebLock
;
75 ULONG LdrpFatalHardErrorCount
;
76 ULONG LdrpActiveUnloadCount
;
78 //extern LIST_ENTRY RtlCriticalSectionList;
80 VOID NTAPI
RtlpInitializeVectoredExceptionHandling(VOID
);
81 VOID NTAPI
RtlpInitDeferedCriticalSection(VOID
);
82 VOID NTAPI
RtlInitializeHeapManager(VOID
);
83 extern BOOLEAN RtlpPageHeapEnabled
;
85 ULONG RtlpDisableHeapLookaside
; // TODO: Move to heap.c
86 ULONG RtlpShutdownProcessFlags
; // TODO: Use it
88 NTSTATUS
LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders
, PVOID ImageBase
);
89 void actctx_init(void);
90 extern BOOLEAN RtlpUse16ByteSLists
;
93 #define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
95 #define DEFAULT_SECURITY_COOKIE 0xBB40E64E
98 /* FUNCTIONS *****************************************************************/
105 LdrOpenImageFileOptionsKey(IN PUNICODE_STRING SubKey
,
107 OUT PHANDLE NewKeyHandle
)
109 PHANDLE RootKeyLocation
;
111 UNICODE_STRING SubKeyString
;
112 OBJECT_ATTRIBUTES ObjectAttributes
;
116 /* Check which root key to open */
118 RootKeyLocation
= &Wow64ExecOptionsKey
;
120 RootKeyLocation
= &ImageExecOptionsKey
;
122 /* Get the current key */
123 RootKey
= *RootKeyLocation
;
125 /* Setup the object attributes */
126 InitializeObjectAttributes(&ObjectAttributes
,
128 &Wow64OptionsString
: &ImageExecOptionsString
,
129 OBJ_CASE_INSENSITIVE
,
133 /* Open the root key */
134 Status
= ZwOpenKey(&RootKey
, KEY_ENUMERATE_SUB_KEYS
, &ObjectAttributes
);
135 if (NT_SUCCESS(Status
))
137 /* Write the key handle */
138 if (_InterlockedCompareExchange((LONG
*)RootKeyLocation
, (LONG
)RootKey
, 0) != 0)
140 /* Someone already opened it, use it instead */
142 RootKey
= *RootKeyLocation
;
145 /* Extract the name */
146 SubKeyString
= *SubKey
;
147 p1
= (PWCHAR
)((ULONG_PTR
)SubKeyString
.Buffer
+ SubKeyString
.Length
);
148 while (SubKeyString
.Length
)
150 if (p1
[-1] == L
'\\') break;
152 SubKeyString
.Length
-= sizeof(*p1
);
154 SubKeyString
.Buffer
= p1
;
155 SubKeyString
.Length
= SubKey
->Length
- SubKeyString
.Length
;
157 /* Setup the object attributes */
158 InitializeObjectAttributes(&ObjectAttributes
,
160 OBJ_CASE_INSENSITIVE
,
164 /* Open the setting key */
165 Status
= ZwOpenKey((PHANDLE
)NewKeyHandle
, GENERIC_READ
, &ObjectAttributes
);
168 /* Return to caller */
177 LdrQueryImageFileKeyOption(IN HANDLE KeyHandle
,
182 OUT PULONG ReturnedLength OPTIONAL
)
185 UNICODE_STRING ValueNameString
, IntegerString
;
186 ULONG KeyInfoSize
, ResultSize
;
187 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInformation
= (PKEY_VALUE_PARTIAL_INFORMATION
)&KeyInfo
;
188 BOOLEAN FreeHeap
= FALSE
;
191 /* Build a string for the value name */
192 Status
= RtlInitUnicodeStringEx(&ValueNameString
, ValueName
);
193 if (!NT_SUCCESS(Status
)) return Status
;
195 /* Query the value */
196 Status
= ZwQueryValueKey(KeyHandle
,
198 KeyValuePartialInformation
,
202 if (Status
== STATUS_BUFFER_OVERFLOW
)
204 /* Our local buffer wasn't enough, allocate one */
205 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) +
206 KeyValueInformation
->DataLength
;
207 KeyValueInformation
= RtlAllocateHeap(RtlGetProcessHeap(),
210 if (KeyValueInformation
!= NULL
)
213 Status
= ZwQueryValueKey(KeyHandle
,
215 KeyValuePartialInformation
,
223 /* Give up this time */
224 Status
= STATUS_NO_MEMORY
;
228 /* Check for success */
229 if (NT_SUCCESS(Status
))
231 /* Handle binary data */
232 if (KeyValueInformation
->Type
== REG_BINARY
)
235 if ((Buffer
) && (KeyValueInformation
->DataLength
<= BufferSize
))
237 /* Copy into buffer */
238 RtlMoveMemory(Buffer
,
239 &KeyValueInformation
->Data
,
240 KeyValueInformation
->DataLength
);
244 Status
= STATUS_BUFFER_OVERFLOW
;
247 /* Copy the result length */
248 if (ReturnedLength
) *ReturnedLength
= KeyValueInformation
->DataLength
;
250 else if (KeyValueInformation
->Type
== REG_DWORD
)
252 /* Check for valid type */
253 if (KeyValueInformation
->Type
!= Type
)
256 Status
= STATUS_OBJECT_TYPE_MISMATCH
;
262 (BufferSize
== sizeof(ULONG
)) &&
263 (KeyValueInformation
->DataLength
<= BufferSize
))
265 /* Copy into buffer */
266 RtlMoveMemory(Buffer
,
267 &KeyValueInformation
->Data
,
268 KeyValueInformation
->DataLength
);
272 Status
= STATUS_BUFFER_OVERFLOW
;
275 /* Copy the result length */
276 if (ReturnedLength
) *ReturnedLength
= KeyValueInformation
->DataLength
;
279 else if (KeyValueInformation
->Type
!= REG_SZ
)
281 /* We got something weird */
282 Status
= STATUS_OBJECT_TYPE_MISMATCH
;
286 /* String, check what you requested */
287 if (Type
== REG_DWORD
)
290 if (BufferSize
!= sizeof(ULONG
))
294 Status
= STATUS_INFO_LENGTH_MISMATCH
;
298 /* OK, we know what you want... */
299 IntegerString
.Buffer
= (PWSTR
)KeyValueInformation
->Data
;
300 IntegerString
.Length
= (USHORT
)KeyValueInformation
->DataLength
-
302 IntegerString
.MaximumLength
= (USHORT
)KeyValueInformation
->DataLength
;
303 Status
= RtlUnicodeStringToInteger(&IntegerString
, 0, (PULONG
)Buffer
);
309 if (KeyValueInformation
->DataLength
> BufferSize
)
312 Status
= STATUS_BUFFER_OVERFLOW
;
317 BufferSize
= KeyValueInformation
->DataLength
;
320 /* Copy the string */
321 RtlMoveMemory(Buffer
, &KeyValueInformation
->Data
, BufferSize
);
324 /* Copy the result length */
325 if (ReturnedLength
) *ReturnedLength
= KeyValueInformation
->DataLength
;
329 /* Check if buffer was in heap */
330 if (FreeHeap
) RtlFreeHeap(RtlGetProcessHeap(), 0, KeyValueInformation
);
341 LdrQueryImageFileExecutionOptionsEx(IN PUNICODE_STRING SubKey
,
346 OUT PULONG ReturnedLength OPTIONAL
,
352 /* Open a handle to the key */
353 Status
= LdrOpenImageFileOptionsKey(SubKey
, Wow64
, &KeyHandle
);
355 /* Check for success */
356 if (NT_SUCCESS(Status
))
359 Status
= LdrQueryImageFileKeyOption(KeyHandle
,
370 /* Return to caller */
379 LdrQueryImageFileExecutionOptions(IN PUNICODE_STRING SubKey
,
384 OUT PULONG ReturnedLength OPTIONAL
)
386 /* Call the newer function */
387 return LdrQueryImageFileExecutionOptionsEx(SubKey
,
398 LdrpEnsureLoaderLockIsHeld(VOID
)
405 LdrpFetchAddressOfSecurityCookie(PVOID BaseAddress
, ULONG SizeOfImage
)
407 PIMAGE_LOAD_CONFIG_DIRECTORY ConfigDir
;
411 /* Check NT header first */
412 if (!RtlImageNtHeader(BaseAddress
)) return NULL
;
414 /* Get the pointer to the config directory */
415 ConfigDir
= RtlImageDirectoryEntryToData(BaseAddress
,
417 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
420 /* Check for sanity */
422 (DirSize
!= 64 && ConfigDir
->Size
!= DirSize
) ||
423 (ConfigDir
->Size
< 0x48))
426 /* Now get the cookie */
427 Cookie
= (PVOID
)ConfigDir
->SecurityCookie
;
429 /* Check this cookie */
430 if ((PCHAR
)Cookie
<= (PCHAR
)BaseAddress
||
431 (PCHAR
)Cookie
>= (PCHAR
)BaseAddress
+ SizeOfImage
)
436 /* Return validated security cookie */
442 LdrpInitSecurityCookie(PLDR_DATA_TABLE_ENTRY LdrEntry
)
445 LARGE_INTEGER Counter
;
448 /* Fetch address of the cookie */
449 Cookie
= LdrpFetchAddressOfSecurityCookie(LdrEntry
->DllBase
, LdrEntry
->SizeOfImage
);
453 /* Check if it's a default one */
454 if ((*Cookie
== DEFAULT_SECURITY_COOKIE
) ||
457 /* Make up a cookie from a bunch of values which may uniquely represent
458 current moment of time, environment, etc */
459 NtQueryPerformanceCounter(&Counter
, NULL
);
461 NewCookie
= Counter
.LowPart
^ Counter
.HighPart
;
462 NewCookie
^= (ULONG
)NtCurrentTeb()->ClientId
.UniqueProcess
;
463 NewCookie
^= (ULONG
)NtCurrentTeb()->ClientId
.UniqueThread
;
465 /* Loop like it's done in KeQueryTickCount(). We don't want to call it directly. */
466 while (SharedUserData
->SystemTime
.High1Time
!= SharedUserData
->SystemTime
.High2Time
)
471 /* Calculate the milliseconds value and xor it to the cookie */
472 NewCookie
^= Int64ShrlMod32(UInt32x32To64(SharedUserData
->TickCountMultiplier
, SharedUserData
->TickCount
.LowPart
), 24) +
473 (SharedUserData
->TickCountMultiplier
* (SharedUserData
->TickCount
.High1Time
<< 8));
475 /* Make the cookie 16bit if necessary */
476 if (*Cookie
== 0xBB40) NewCookie
&= 0xFFFF;
478 /* If the result is 0 or the same as we got, just subtract one from the existing value
480 if ((NewCookie
== 0) || (NewCookie
== *Cookie
))
482 NewCookie
= *Cookie
- 1;
485 /* Set the new cookie value */
495 LdrpInitializeThread(IN PCONTEXT Context
)
497 PPEB Peb
= NtCurrentPeb();
498 PLDR_DATA_TABLE_ENTRY LdrEntry
;
499 PLIST_ENTRY NextEntry
, ListHead
;
500 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx
;
504 DPRINT("LdrpInitializeThread() called for %wZ (%p/%p)\n",
505 &LdrpImageEntry
->BaseDllName
,
506 NtCurrentTeb()->RealClientId
.UniqueProcess
,
507 NtCurrentTeb()->RealClientId
.UniqueThread
);
509 /* Allocate an Activation Context Stack */
510 DPRINT("ActivationContextStack %p\n", NtCurrentTeb()->ActivationContextStackPointer
);
511 Status
= RtlAllocateActivationContextStack(&NtCurrentTeb()->ActivationContextStackPointer
);
512 if (!NT_SUCCESS(Status
))
514 DPRINT1("Warning: Unable to allocate ActivationContextStack\n");
517 /* Make sure we are not shutting down */
518 if (LdrpShutdownInProgress
) return;
523 /* Start at the beginning */
524 ListHead
= &Peb
->Ldr
->InMemoryOrderModuleList
;
525 NextEntry
= ListHead
->Flink
;
526 while (NextEntry
!= ListHead
)
528 /* Get the current entry */
529 LdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InMemoryOrderLinks
);
531 /* Make sure it's not ourselves */
532 if (Peb
->ImageBaseAddress
!= LdrEntry
->DllBase
)
534 /* Check if we should call */
535 if (!(LdrEntry
->Flags
& LDRP_DONT_CALL_FOR_THREADS
))
537 /* Get the entrypoint */
538 EntryPoint
= LdrEntry
->EntryPoint
;
540 /* Check if we are ready to call it */
542 (LdrEntry
->Flags
& LDRP_PROCESS_ATTACH_CALLED
) &&
543 (LdrEntry
->Flags
& LDRP_IMAGE_DLL
))
545 /* Set up the Act Ctx */
546 ActCtx
.Size
= sizeof(ActCtx
);
548 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
550 /* Activate the ActCtx */
551 RtlActivateActivationContextUnsafeFast(&ActCtx
,
552 LdrEntry
->EntryPointActivationContext
);
554 /* Check if it has TLS */
555 if (LdrEntry
->TlsIndex
)
557 /* Make sure we're not shutting down */
558 if (!LdrpShutdownInProgress
)
561 LdrpCallTlsInitializers(LdrEntry
->DllBase
, DLL_THREAD_ATTACH
);
565 /* Make sure we're not shutting down */
566 if (!LdrpShutdownInProgress
)
568 /* Call the Entrypoint */
569 DPRINT("%wZ - Calling entry point at %p for thread attaching, %p/%p\n",
570 &LdrEntry
->BaseDllName
, LdrEntry
->EntryPoint
,
571 NtCurrentTeb()->RealClientId
.UniqueProcess
,
572 NtCurrentTeb()->RealClientId
.UniqueThread
);
573 LdrpCallInitRoutine(LdrEntry
->EntryPoint
,
579 /* Deactivate the ActCtx */
580 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
586 NextEntry
= NextEntry
->Flink
;
590 if (LdrpImageHasTls
&& !LdrpShutdownInProgress
)
592 /* Set up the Act Ctx */
593 ActCtx
.Size
= sizeof(ActCtx
);
595 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
597 /* Activate the ActCtx */
598 RtlActivateActivationContextUnsafeFast(&ActCtx
,
599 LdrpImageEntry
->EntryPointActivationContext
);
601 /* Do TLS callbacks */
602 LdrpCallTlsInitializers(Peb
->ImageBaseAddress
, DLL_THREAD_ATTACH
);
604 /* Deactivate the ActCtx */
605 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
608 DPRINT("LdrpInitializeThread() done\n");
613 LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL
)
615 PLDR_DATA_TABLE_ENTRY LocalArray
[16];
616 PLIST_ENTRY ListHead
;
617 PLIST_ENTRY NextEntry
;
618 PLDR_DATA_TABLE_ENTRY LdrEntry
, *LdrRootEntry
, OldInitializer
;
622 NTSTATUS Status
= STATUS_SUCCESS
;
623 PPEB Peb
= NtCurrentPeb();
624 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx
;
625 ULONG BreakOnDllLoad
;
629 DPRINT("LdrpRunInitializeRoutines() called for %wZ (%p/%p)\n",
630 &LdrpImageEntry
->BaseDllName
,
631 NtCurrentTeb()->RealClientId
.UniqueProcess
,
632 NtCurrentTeb()->RealClientId
.UniqueThread
);
634 /* Check the Loader Lock */
635 LdrpEnsureLoaderLockIsHeld();
637 /* Get the number of entries to call */
638 if ((Count
= LdrpClearLoadInProgress()))
640 /* Check if we can use our local buffer */
643 /* Allocate space for all the entries */
644 LdrRootEntry
= RtlAllocateHeap(RtlGetProcessHeap(),
646 Count
* sizeof(*LdrRootEntry
));
647 if (!LdrRootEntry
) return STATUS_NO_MEMORY
;
651 /* Use our local array */
652 LdrRootEntry
= LocalArray
;
661 /* Show debug message */
664 DPRINT1("[%p,%p] LDR: Real INIT LIST for Process %wZ\n",
665 NtCurrentTeb()->RealClientId
.UniqueThread
,
666 NtCurrentTeb()->RealClientId
.UniqueProcess
,
667 &Peb
->ProcessParameters
->ImagePathName
);
671 ListHead
= &Peb
->Ldr
->InInitializationOrderModuleList
;
672 NextEntry
= ListHead
->Flink
;
674 while (NextEntry
!= ListHead
)
676 /* Get the Data Entry */
677 LdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderLinks
);
679 /* Check if we have a Root Entry */
683 if (!(LdrEntry
->Flags
& LDRP_ENTRY_PROCESSED
))
685 /* Setup the Cookie for the DLL */
686 LdrpInitSecurityCookie(LdrEntry
);
688 /* Check for valid entrypoint */
689 if (LdrEntry
->EntryPoint
)
693 LdrRootEntry
[i
] = LdrEntry
;
695 /* Display debug message */
698 DPRINT1("[%p,%p] LDR: %wZ init routine %p\n",
699 NtCurrentTeb()->RealClientId
.UniqueThread
,
700 NtCurrentTeb()->RealClientId
.UniqueProcess
,
701 &LdrEntry
->FullDllName
,
702 LdrEntry
->EntryPoint
);
710 LdrEntry
->Flags
|= LDRP_ENTRY_PROCESSED
;
711 NextEntry
= NextEntry
->Flink
;
714 /* If we got a context, then we have to call Kernel32 for TS support */
717 /* Check if we have one */
718 //if (Kernel32ProcessInitPostImportfunction)
721 //Kernel32ProcessInitPostImportfunction();
725 //Kernel32ProcessInitPostImportfunction = NULL;
729 /* No root entry? return */
730 if (!LdrRootEntry
) return STATUS_SUCCESS
;
732 /* Set the TLD TEB */
733 OldTldTeb
= LdrpTopLevelDllBeingLoadedTeb
;
734 LdrpTopLevelDllBeingLoadedTeb
= NtCurrentTeb();
741 LdrEntry
= LdrRootEntry
[i
];
743 /* FIXME: Verify NX Compat */
745 /* Move to next entry */
748 /* Get its entrypoint */
749 EntryPoint
= LdrEntry
->EntryPoint
;
751 /* Are we being debugged? */
753 if (Peb
->BeingDebugged
|| Peb
->ReadImageFileExecOptions
)
755 /* Check if we should break on load */
756 Status
= LdrQueryImageFileExecutionOptions(&LdrEntry
->BaseDllName
,
762 if (!NT_SUCCESS(Status
)) BreakOnDllLoad
= 0;
764 /* Reset status back to STATUS_SUCCESS */
765 Status
= STATUS_SUCCESS
;
771 /* Check if we should show a message */
774 DPRINT1("LDR: %wZ loaded.", &LdrEntry
->BaseDllName
);
775 DPRINT1(" - About to call init routine at %p\n", EntryPoint
);
778 /* Break in debugger */
782 /* Make sure we have an entrypoint */
785 /* Save the old Dll Initializer and write the current one */
786 OldInitializer
= LdrpCurrentDllInitializer
;
787 LdrpCurrentDllInitializer
= LdrEntry
;
789 /* Set up the Act Ctx */
790 ActCtx
.Size
= sizeof(ActCtx
);
792 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
794 /* Activate the ActCtx */
795 RtlActivateActivationContextUnsafeFast(&ActCtx
,
796 LdrEntry
->EntryPointActivationContext
);
798 /* Check if it has TLS */
799 if (LdrEntry
->TlsIndex
&& Context
)
802 LdrpCallTlsInitializers(LdrEntry
->DllBase
, DLL_PROCESS_ATTACH
);
805 /* Call the Entrypoint */
808 DPRINT1("%wZ - Calling entry point at %p for DLL_PROCESS_ATTACH\n",
809 &LdrEntry
->BaseDllName
, EntryPoint
);
811 DllStatus
= LdrpCallInitRoutine(EntryPoint
,
816 /* Deactivate the ActCtx */
817 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
819 /* Save the Current DLL Initializer */
820 LdrpCurrentDllInitializer
= OldInitializer
;
822 /* Mark the entry as processed */
823 LdrEntry
->Flags
|= LDRP_PROCESS_ATTACH_CALLED
;
825 /* Fail if DLL init failed */
828 DPRINT1("LDR: DLL_PROCESS_ATTACH for dll \"%wZ\" (InitRoutine: %p) failed\n",
829 &LdrEntry
->BaseDllName
, EntryPoint
);
831 Status
= STATUS_DLL_INIT_FAILED
;
838 ListHead
= &Peb
->Ldr
->InInitializationOrderModuleList
;
839 NextEntry
= NextEntry
->Flink
;
840 while (NextEntry
!= ListHead
)
842 /* Get the Data Entrry */
843 LdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderLinks
);
845 /* FIXME: Verify NX Compat */
846 // LdrpCheckNXCompatibility()
849 NextEntry
= NextEntry
->Flink
;
853 if (LdrpImageHasTls
&& Context
)
855 /* Set up the Act Ctx */
856 ActCtx
.Size
= sizeof(ActCtx
);
858 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
860 /* Activate the ActCtx */
861 RtlActivateActivationContextUnsafeFast(&ActCtx
,
862 LdrpImageEntry
->EntryPointActivationContext
);
864 /* Do TLS callbacks */
865 LdrpCallTlsInitializers(Peb
->ImageBaseAddress
, DLL_PROCESS_ATTACH
);
867 /* Deactivate the ActCtx */
868 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
872 /* Restore old TEB */
873 LdrpTopLevelDllBeingLoadedTeb
= OldTldTeb
;
875 /* Check if the array is in the heap */
876 if (LdrRootEntry
!= LocalArray
)
879 RtlFreeHeap(RtlGetProcessHeap(), 0, LdrRootEntry
);
882 /* Return to caller */
883 DPRINT("LdrpRunInitializeRoutines() done\n");
892 LdrShutdownProcess(VOID
)
894 PPEB Peb
= NtCurrentPeb();
895 PLDR_DATA_TABLE_ENTRY LdrEntry
;
896 PLIST_ENTRY NextEntry
, ListHead
;
897 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx
;
900 DPRINT("LdrShutdownProcess() called for %wZ\n", &LdrpImageEntry
->BaseDllName
);
901 if (LdrpShutdownInProgress
) return STATUS_SUCCESS
;
903 /* Tell the Shim Engine */
915 /* Set the shutdown variables */
916 LdrpShutdownThreadId
= NtCurrentTeb()->RealClientId
.UniqueThread
;
917 LdrpShutdownInProgress
= TRUE
;
919 /* Enter the Loader Lock */
920 RtlEnterCriticalSection(&LdrpLoaderLock
);
922 /* Cleanup trace logging data (Etw) */
923 if (SharedUserData
->TraceLogging
)
926 DPRINT1("We don't support Etw yet.\n");
929 /* Start at the end */
930 ListHead
= &Peb
->Ldr
->InInitializationOrderModuleList
;
931 NextEntry
= ListHead
->Blink
;
932 while (NextEntry
!= ListHead
)
934 /* Get the current entry */
935 LdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderLinks
);
936 NextEntry
= NextEntry
->Blink
;
938 /* Make sure it's not ourselves */
939 if (Peb
->ImageBaseAddress
!= LdrEntry
->DllBase
)
941 /* Get the entrypoint */
942 EntryPoint
= LdrEntry
->EntryPoint
;
944 /* Check if we are ready to call it */
946 (LdrEntry
->Flags
& LDRP_PROCESS_ATTACH_CALLED
) &&
949 /* Set up the Act Ctx */
950 ActCtx
.Size
= sizeof(ActCtx
);
952 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
954 /* Activate the ActCtx */
955 RtlActivateActivationContextUnsafeFast(&ActCtx
,
956 LdrEntry
->EntryPointActivationContext
);
958 /* Check if it has TLS */
959 if (LdrEntry
->TlsIndex
)
962 LdrpCallTlsInitializers(LdrEntry
->DllBase
, DLL_PROCESS_DETACH
);
965 /* Call the Entrypoint */
966 DPRINT("%wZ - Calling entry point at %p for thread detaching\n",
967 &LdrEntry
->BaseDllName
, LdrEntry
->EntryPoint
);
968 LdrpCallInitRoutine(EntryPoint
,
973 /* Deactivate the ActCtx */
974 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
982 /* Set up the Act Ctx */
983 ActCtx
.Size
= sizeof(ActCtx
);
985 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
987 /* Activate the ActCtx */
988 RtlActivateActivationContextUnsafeFast(&ActCtx
,
989 LdrpImageEntry
->EntryPointActivationContext
);
991 /* Do TLS callbacks */
992 LdrpCallTlsInitializers(Peb
->ImageBaseAddress
, DLL_PROCESS_DETACH
);
994 /* Deactivate the ActCtx */
995 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
998 /* FIXME: Do Heap detection and Etw final shutdown */
1000 /* Release the lock */
1001 RtlLeaveCriticalSection(&LdrpLoaderLock
);
1002 DPRINT("LdrpShutdownProcess() done\n");
1004 return STATUS_SUCCESS
;
1012 LdrShutdownThread(VOID
)
1014 PPEB Peb
= NtCurrentPeb();
1015 PTEB Teb
= NtCurrentTeb();
1016 PLDR_DATA_TABLE_ENTRY LdrEntry
;
1017 PLIST_ENTRY NextEntry
, ListHead
;
1018 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx
;
1021 DPRINT("LdrShutdownThread() called for %wZ\n",
1022 &LdrpImageEntry
->BaseDllName
);
1024 /* Cleanup trace logging data (Etw) */
1025 if (SharedUserData
->TraceLogging
)
1028 DPRINT1("We don't support Etw yet.\n");
1031 /* Get the Ldr Lock */
1032 RtlEnterCriticalSection(&LdrpLoaderLock
);
1034 /* Start at the end */
1035 ListHead
= &Peb
->Ldr
->InInitializationOrderModuleList
;
1036 NextEntry
= ListHead
->Blink
;
1037 while (NextEntry
!= ListHead
)
1039 /* Get the current entry */
1040 LdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderLinks
);
1041 NextEntry
= NextEntry
->Blink
;
1043 /* Make sure it's not ourselves */
1044 if (Peb
->ImageBaseAddress
!= LdrEntry
->DllBase
)
1046 /* Check if we should call */
1047 if (!(LdrEntry
->Flags
& LDRP_DONT_CALL_FOR_THREADS
) &&
1048 (LdrEntry
->Flags
& LDRP_PROCESS_ATTACH_CALLED
) &&
1049 (LdrEntry
->Flags
& LDRP_IMAGE_DLL
))
1051 /* Get the entrypoint */
1052 EntryPoint
= LdrEntry
->EntryPoint
;
1054 /* Check if we are ready to call it */
1057 /* Set up the Act Ctx */
1058 ActCtx
.Size
= sizeof(ActCtx
);
1060 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
1062 /* Activate the ActCtx */
1063 RtlActivateActivationContextUnsafeFast(&ActCtx
,
1064 LdrEntry
->EntryPointActivationContext
);
1066 /* Check if it has TLS */
1067 if (LdrEntry
->TlsIndex
)
1069 /* Make sure we're not shutting down */
1070 if (!LdrpShutdownInProgress
)
1073 LdrpCallTlsInitializers(LdrEntry
->DllBase
, DLL_THREAD_DETACH
);
1077 /* Make sure we're not shutting down */
1078 if (!LdrpShutdownInProgress
)
1080 /* Call the Entrypoint */
1081 DPRINT("%wZ - Calling entry point at %p for thread detaching\n",
1082 &LdrEntry
->BaseDllName
, LdrEntry
->EntryPoint
);
1083 LdrpCallInitRoutine(EntryPoint
,
1089 /* Deactivate the ActCtx */
1090 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
1097 if (LdrpImageHasTls
)
1099 /* Set up the Act Ctx */
1100 ActCtx
.Size
= sizeof(ActCtx
);
1102 RtlZeroMemory(&ActCtx
.Frame
, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME
));
1104 /* Activate the ActCtx */
1105 RtlActivateActivationContextUnsafeFast(&ActCtx
,
1106 LdrpImageEntry
->EntryPointActivationContext
);
1108 /* Do TLS callbacks */
1109 LdrpCallTlsInitializers(Peb
->ImageBaseAddress
, DLL_THREAD_DETACH
);
1111 /* Deactivate the ActCtx */
1112 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
1117 RtlLeaveCriticalSection(&LdrpLoaderLock
);
1119 /* Check for expansion slots */
1120 if (Teb
->TlsExpansionSlots
)
1122 /* Free expansion slots */
1123 RtlFreeHeap(RtlGetProcessHeap(), 0, Teb
->TlsExpansionSlots
);
1126 /* Check for FLS Data */
1130 DPRINT1("We don't support FLS Data yet\n");
1133 /* Check for Fiber data */
1134 if (Teb
->HasFiberData
)
1136 /* Free Fiber data*/
1137 RtlFreeHeap(RtlGetProcessHeap(), 0, Teb
->NtTib
.FiberData
);
1138 Teb
->NtTib
.FiberData
= NULL
;
1141 /* Free the activation context stack */
1142 RtlFreeThreadActivationContextStack();
1143 DPRINT("LdrShutdownThread() done\n");
1145 return STATUS_SUCCESS
;
1150 LdrpInitializeTls(VOID
)
1152 PLIST_ENTRY NextEntry
, ListHead
;
1153 PLDR_DATA_TABLE_ENTRY LdrEntry
;
1154 PIMAGE_TLS_DIRECTORY TlsDirectory
;
1155 PLDRP_TLS_DATA TlsData
;
1158 /* Initialize the TLS List */
1159 InitializeListHead(&LdrpTlsList
);
1161 /* Loop all the modules */
1162 ListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
1163 NextEntry
= ListHead
->Flink
;
1164 while (ListHead
!= NextEntry
)
1167 LdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
1168 NextEntry
= NextEntry
->Flink
;
1170 /* Get the TLS directory */
1171 TlsDirectory
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
1173 IMAGE_DIRECTORY_ENTRY_TLS
,
1176 /* Check if we have a directory */
1177 if (!TlsDirectory
) continue;
1179 /* Check if the image has TLS */
1180 if (!LdrpImageHasTls
) LdrpImageHasTls
= TRUE
;
1182 /* Show debug message */
1185 DPRINT1("LDR: Tls Found in %wZ at %p\n",
1186 &LdrEntry
->BaseDllName
,
1190 /* Allocate an entry */
1191 TlsData
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(LDRP_TLS_DATA
));
1192 if (!TlsData
) return STATUS_NO_MEMORY
;
1194 /* Lock the DLL and mark it for TLS Usage */
1195 LdrEntry
->LoadCount
= -1;
1196 LdrEntry
->TlsIndex
= -1;
1198 /* Save the cached TLS data */
1199 TlsData
->TlsDirectory
= *TlsDirectory
;
1200 InsertTailList(&LdrpTlsList
, &TlsData
->TlsLinks
);
1202 /* Update the index */
1203 *(PLONG
)TlsData
->TlsDirectory
.AddressOfIndex
= LdrpNumberOfTlsEntries
;
1204 TlsData
->TlsDirectory
.Characteristics
= LdrpNumberOfTlsEntries
++;
1207 /* Done setting up TLS, allocate entries */
1208 return LdrpAllocateTls();
1213 LdrpAllocateTls(VOID
)
1215 PTEB Teb
= NtCurrentTeb();
1216 PLIST_ENTRY NextEntry
, ListHead
;
1217 PLDRP_TLS_DATA TlsData
;
1221 /* Check if we have any entries */
1222 if (!LdrpNumberOfTlsEntries
)
1223 return STATUS_SUCCESS
;
1225 /* Allocate the vector array */
1226 TlsVector
= RtlAllocateHeap(RtlGetProcessHeap(),
1228 LdrpNumberOfTlsEntries
* sizeof(PVOID
));
1229 if (!TlsVector
) return STATUS_NO_MEMORY
;
1230 Teb
->ThreadLocalStoragePointer
= TlsVector
;
1232 /* Loop the TLS Array */
1233 ListHead
= &LdrpTlsList
;
1234 NextEntry
= ListHead
->Flink
;
1235 while (NextEntry
!= ListHead
)
1238 TlsData
= CONTAINING_RECORD(NextEntry
, LDRP_TLS_DATA
, TlsLinks
);
1239 NextEntry
= NextEntry
->Flink
;
1241 /* Allocate this vector */
1242 TlsDataSize
= TlsData
->TlsDirectory
.EndAddressOfRawData
-
1243 TlsData
->TlsDirectory
.StartAddressOfRawData
;
1244 TlsVector
[TlsData
->TlsDirectory
.Characteristics
] = RtlAllocateHeap(RtlGetProcessHeap(),
1247 if (!TlsVector
[TlsData
->TlsDirectory
.Characteristics
])
1250 return STATUS_NO_MEMORY
;
1253 /* Show debug message */
1256 DPRINT1("LDR: TlsVector %p Index %lu = %p copied from %x to %p\n",
1258 TlsData
->TlsDirectory
.Characteristics
,
1259 &TlsVector
[TlsData
->TlsDirectory
.Characteristics
],
1260 TlsData
->TlsDirectory
.StartAddressOfRawData
,
1261 TlsVector
[TlsData
->TlsDirectory
.Characteristics
]);
1265 RtlCopyMemory(TlsVector
[TlsData
->TlsDirectory
.Characteristics
],
1266 (PVOID
)TlsData
->TlsDirectory
.StartAddressOfRawData
,
1271 return STATUS_SUCCESS
;
1278 PLIST_ENTRY ListHead
, NextEntry
;
1279 PLDRP_TLS_DATA TlsData
;
1281 PTEB Teb
= NtCurrentTeb();
1283 /* Get a pointer to the vector array */
1284 TlsVector
= Teb
->ThreadLocalStoragePointer
;
1285 if (!TlsVector
) return;
1287 /* Loop through it */
1288 ListHead
= &LdrpTlsList
;
1289 NextEntry
= ListHead
->Flink
;
1290 while (NextEntry
!= ListHead
)
1292 TlsData
= CONTAINING_RECORD(NextEntry
, LDRP_TLS_DATA
, TlsLinks
);
1293 NextEntry
= NextEntry
->Flink
;
1295 /* Free each entry */
1296 if (TlsVector
[TlsData
->TlsDirectory
.Characteristics
])
1298 RtlFreeHeap(RtlGetProcessHeap(),
1300 TlsVector
[TlsData
->TlsDirectory
.Characteristics
]);
1304 /* Free the array itself */
1305 RtlFreeHeap(RtlGetProcessHeap(),
1312 LdrpInitializeApplicationVerifierPackage(PUNICODE_STRING ImagePathName
, PPEB Peb
, BOOLEAN SystemWide
, BOOLEAN ReadAdvancedOptions
)
1314 /* If global flags request DPH, perform some additional actions */
1315 if (Peb
->NtGlobalFlag
& FLG_HEAP_PAGE_ALLOCS
)
1317 // TODO: Read advanced DPH flags from the registry if requested
1318 if (ReadAdvancedOptions
)
1323 /* Enable page heap */
1324 RtlpPageHeapEnabled
= TRUE
;
1327 return STATUS_SUCCESS
;
1332 LdrpInitializeExecutionOptions(PUNICODE_STRING ImagePathName
, PPEB Peb
, PHANDLE OptionsKey
)
1336 ULONG ExecuteOptions
, MinimumStackCommit
= 0, GlobalFlag
;
1338 /* Return error if we were not provided a pointer where to save the options key handle */
1339 if (!OptionsKey
) return STATUS_INVALID_HANDLE
;
1341 /* Zero initialize the optinos key pointer */
1344 /* Open the options key */
1345 Status
= LdrOpenImageFileOptionsKey(ImagePathName
, 0, &KeyHandle
);
1347 /* Save it if it was opened successfully */
1348 if (NT_SUCCESS(Status
))
1349 *OptionsKey
= KeyHandle
;
1353 /* There are image specific options, read them starting with NXCOMPAT */
1354 Status
= LdrQueryImageFileKeyOption(KeyHandle
,
1358 sizeof(ExecuteOptions
),
1361 if (NT_SUCCESS(Status
))
1363 /* TODO: Set execution options for the process */
1365 if (ExecuteOptions == 0)
1369 ZwSetInformationProcess(NtCurrentProcess(),
1370 ProcessExecuteFlags,
1376 /* Check if this image uses large pages */
1377 if (Peb
->ImageUsesLargePages
)
1379 /* TODO: If it does, open large page key */
1383 /* Get various option values */
1384 LdrQueryImageFileKeyOption(KeyHandle
,
1385 L
"DisableHeapLookaside",
1387 &RtlpDisableHeapLookaside
,
1388 sizeof(RtlpDisableHeapLookaside
),
1391 LdrQueryImageFileKeyOption(KeyHandle
,
1394 &RtlpShutdownProcessFlags
,
1395 sizeof(RtlpShutdownProcessFlags
),
1398 LdrQueryImageFileKeyOption(KeyHandle
,
1399 L
"MinimumStackCommitInBytes",
1401 &MinimumStackCommit
,
1402 sizeof(MinimumStackCommit
),
1405 /* Update PEB's minimum stack commit if it's lower */
1406 if (Peb
->MinimumStackCommit
< MinimumStackCommit
)
1407 Peb
->MinimumStackCommit
= MinimumStackCommit
;
1409 /* Set the global flag */
1410 Status
= LdrQueryImageFileKeyOption(KeyHandle
,
1417 if (NT_SUCCESS(Status
))
1418 Peb
->NtGlobalFlag
= GlobalFlag
;
1422 /* Call AVRF if necessary */
1423 if (Peb
->NtGlobalFlag
& (FLG_POOL_ENABLE_TAIL_CHECK
| FLG_HEAP_PAGE_ALLOCS
))
1425 Status
= LdrpInitializeApplicationVerifierPackage(ImagePathName
, Peb
, TRUE
, FALSE
);
1426 if (!NT_SUCCESS(Status
))
1428 DPRINT1("AVRF: LdrpInitializeApplicationVerifierPackage failed with %08X\n", Status
);
1434 /* There are no image-specific options, so perform global initialization */
1435 if (Peb
->NtGlobalFlag
& (FLG_POOL_ENABLE_TAIL_CHECK
| FLG_HEAP_PAGE_ALLOCS
))
1437 /* Initialize app verifier package */
1438 Status
= LdrpInitializeApplicationVerifierPackage(ImagePathName
, Peb
, TRUE
, FALSE
);
1439 if (!NT_SUCCESS(Status
))
1441 DPRINT1("AVRF: LdrpInitializeApplicationVerifierPackage failed with %08X\n", Status
);
1446 return STATUS_SUCCESS
;
1451 LdrpValidateImageForMp(IN PLDR_DATA_TABLE_ENTRY LdrDataTableEntry
)
1458 LdrpInitializeProcess(IN PCONTEXT Context
,
1459 IN PVOID SystemArgument1
)
1461 RTL_HEAP_PARAMETERS HeapParameters
;
1462 ULONG ComSectionSize
;
1463 //ANSI_STRING FunctionName = RTL_CONSTANT_STRING("BaseQueryModuleData");
1465 OBJECT_ATTRIBUTES ObjectAttributes
;
1466 //UNICODE_STRING LocalFileName, FullImageName;
1467 HANDLE SymLinkHandle
;
1468 //ULONG DebugHeapOnly;
1469 UNICODE_STRING CommandLine
, NtSystemRoot
, ImagePathName
, FullPath
, ImageFileName
, KnownDllString
;
1470 PPEB Peb
= NtCurrentPeb();
1471 BOOLEAN IsDotNetImage
= FALSE
;
1472 BOOLEAN FreeCurDir
= FALSE
;
1474 PRTL_USER_PROCESS_PARAMETERS ProcessParameters
;
1475 //LPWSTR ImagePathBuffer;
1477 UNICODE_STRING CurrentDirectory
;
1480 PIMAGE_NT_HEADERS NtHeader
;
1481 LPWSTR NtDllName
= NULL
;
1482 NTSTATUS Status
, ImportStatus
;
1483 NLSTABLEINFO NlsTable
;
1484 PIMAGE_LOAD_CONFIG_DIRECTORY LoadConfig
;
1485 PTEB Teb
= NtCurrentTeb();
1486 PLIST_ENTRY ListHead
;
1487 PLIST_ENTRY NextEntry
;
1490 ULONG DebugProcessHeapOnly
= 0;
1491 PLDR_DATA_TABLE_ENTRY NtLdrEntry
;
1493 ULONG ExecuteOptions
= 0;
1496 /* Set a NULL SEH Filter */
1497 RtlSetUnhandledExceptionFilter(NULL
);
1499 /* Get the image path */
1500 ImagePath
= Peb
->ProcessParameters
->ImagePathName
.Buffer
;
1502 /* Check if it's not normalized */
1503 if (!(Peb
->ProcessParameters
->Flags
& RTL_USER_PROCESS_PARAMETERS_NORMALIZED
))
1506 ImagePath
= (PWSTR
)((ULONG_PTR
)ImagePath
+ (ULONG_PTR
)Peb
->ProcessParameters
);
1509 /* Create a unicode string for the Image Path */
1510 ImagePathName
.Length
= Peb
->ProcessParameters
->ImagePathName
.Length
;
1511 ImagePathName
.MaximumLength
= ImagePathName
.Length
+ sizeof(WCHAR
);
1512 ImagePathName
.Buffer
= ImagePath
;
1514 /* Get the NT Headers */
1515 NtHeader
= RtlImageNtHeader(Peb
->ImageBaseAddress
);
1517 /* Get the execution options */
1518 Status
= LdrpInitializeExecutionOptions(&ImagePathName
, Peb
, &OptionsKey
);
1520 /* Check if this is a .NET executable */
1521 if (RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
1523 IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR
,
1526 /* Remeber this for later */
1527 IsDotNetImage
= TRUE
;
1530 /* Save the NTDLL Base address */
1531 NtDllBase
= SystemArgument1
;
1533 /* If this is a Native Image */
1534 if (NtHeader
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_NATIVE
)
1536 /* Then do DLL Validation */
1537 LdrpDllValidation
= TRUE
;
1540 /* Save the old Shim Data */
1541 OldShimData
= Peb
->pShimData
;
1544 Peb
->pShimData
= NULL
;
1546 /* Save the number of processors and CS Timeout */
1547 LdrpNumberOfProcessors
= Peb
->NumberOfProcessors
;
1548 RtlpTimeout
= Peb
->CriticalSectionTimeout
;
1550 /* Normalize the parameters */
1551 ProcessParameters
= RtlNormalizeProcessParams(Peb
->ProcessParameters
);
1552 if (ProcessParameters
)
1554 /* Save the Image and Command Line Names */
1555 ImageFileName
= ProcessParameters
->ImagePathName
;
1556 CommandLine
= ProcessParameters
->CommandLine
;
1560 /* It failed, initialize empty strings */
1561 RtlInitUnicodeString(&ImageFileName
, NULL
);
1562 RtlInitUnicodeString(&CommandLine
, NULL
);
1565 /* Initialize NLS data */
1566 RtlInitNlsTables(Peb
->AnsiCodePageData
,
1567 Peb
->OemCodePageData
,
1568 Peb
->UnicodeCaseTableData
,
1571 /* Reset NLS Translations */
1572 RtlResetRtlTranslations(&NlsTable
);
1574 /* Get the Image Config Directory */
1575 LoadConfig
= RtlImageDirectoryEntryToData(Peb
->ImageBaseAddress
,
1577 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG
,
1580 /* Setup the Heap Parameters */
1581 RtlZeroMemory(&HeapParameters
, sizeof(RTL_HEAP_PARAMETERS
));
1582 HeapFlags
= HEAP_GROWABLE
;
1583 HeapParameters
.Length
= sizeof(RTL_HEAP_PARAMETERS
);
1585 /* Check if we have Configuration Data */
1586 if ((LoadConfig
) && (ConfigSize
== sizeof(IMAGE_LOAD_CONFIG_DIRECTORY
)))
1588 /* FIXME: Custom heap settings and misc. */
1589 DPRINT1("We don't support LOAD_CONFIG data yet\n");
1592 /* Check for custom affinity mask */
1593 if (Peb
->ImageProcessAffinityMask
)
1596 Status
= NtSetInformationProcess(NtCurrentProcess(),
1597 ProcessAffinityMask
,
1598 &Peb
->ImageProcessAffinityMask
,
1599 sizeof(Peb
->ImageProcessAffinityMask
));
1602 /* Check if verbose debugging (ShowSnaps) was requested */
1603 ShowSnaps
= Peb
->NtGlobalFlag
& FLG_SHOW_LDR_SNAPS
;
1605 /* Start verbose debugging messages right now if they were requested */
1608 DPRINT1("LDR: PID: 0x%p started - '%wZ'\n",
1609 Teb
->ClientId
.UniqueProcess
,
1613 /* If the timeout is too long */
1614 if (RtlpTimeout
.QuadPart
< Int32x32To64(3600, -10000000))
1616 /* Then disable CS Timeout */
1617 RtlpTimeoutDisable
= TRUE
;
1620 /* Initialize Critical Section Data */
1621 RtlpInitDeferedCriticalSection();
1623 /* Initialize VEH Call lists */
1624 RtlpInitializeVectoredExceptionHandling();
1626 /* Set TLS/FLS Bitmap data */
1627 Peb
->FlsBitmap
= &FlsBitMap
;
1628 Peb
->TlsBitmap
= &TlsBitMap
;
1629 Peb
->TlsExpansionBitmap
= &TlsExpansionBitMap
;
1631 /* Initialize FLS Bitmap */
1632 RtlInitializeBitMap(&FlsBitMap
,
1634 FLS_MAXIMUM_AVAILABLE
);
1635 RtlSetBit(&FlsBitMap
, 0);
1637 /* Initialize TLS Bitmap */
1638 RtlInitializeBitMap(&TlsBitMap
,
1640 TLS_MINIMUM_AVAILABLE
);
1641 RtlSetBit(&TlsBitMap
, 0);
1642 RtlInitializeBitMap(&TlsExpansionBitMap
,
1643 Peb
->TlsExpansionBitmapBits
,
1644 TLS_EXPANSION_SLOTS
);
1645 RtlSetBit(&TlsExpansionBitMap
, 0);
1647 /* Initialize the Hash Table */
1648 for (i
= 0; i
< LDR_HASH_TABLE_ENTRIES
; i
++)
1650 InitializeListHead(&LdrpHashTable
[i
]);
1653 /* Initialize the Loader Lock */
1654 // FIXME: What's the point of initing it manually, if two lines lower
1655 // a call to RtlInitializeCriticalSection() is being made anyway?
1656 //InsertTailList(&RtlCriticalSectionList, &LdrpLoaderLock.DebugInfo->ProcessLocksList);
1657 //LdrpLoaderLock.DebugInfo->CriticalSection = &LdrpLoaderLock;
1658 RtlInitializeCriticalSection(&LdrpLoaderLock
);
1659 LdrpLoaderLockInit
= TRUE
;
1661 /* Check if User Stack Trace Database support was requested */
1662 if (Peb
->NtGlobalFlag
& FLG_USER_STACK_TRACE_DB
)
1664 DPRINT1("We don't support user stack trace databases yet\n");
1667 /* Setup Fast PEB Lock */
1668 RtlInitializeCriticalSection(&FastPebLock
);
1669 Peb
->FastPebLock
= &FastPebLock
;
1670 //Peb->FastPebLockRoutine = (PPEBLOCKROUTINE)RtlEnterCriticalSection;
1671 //Peb->FastPebUnlockRoutine = (PPEBLOCKROUTINE)RtlLeaveCriticalSection;
1673 /* Setup Callout Lock and Notification list */
1674 //RtlInitializeCriticalSection(&RtlpCalloutEntryLock);
1675 InitializeListHead(&LdrpDllNotificationList
);
1677 /* For old executables, use 16-byte aligned heap */
1678 if ((NtHeader
->OptionalHeader
.MajorSubsystemVersion
<= 3) &&
1679 (NtHeader
->OptionalHeader
.MinorSubsystemVersion
< 51))
1681 HeapFlags
|= HEAP_CREATE_ALIGN_16
;
1684 /* Setup the Heap */
1685 RtlInitializeHeapManager();
1686 Peb
->ProcessHeap
= RtlCreateHeap(HeapFlags
,
1688 NtHeader
->OptionalHeader
.SizeOfHeapReserve
,
1689 NtHeader
->OptionalHeader
.SizeOfHeapCommit
,
1693 if (!Peb
->ProcessHeap
)
1695 DPRINT1("Failed to create process heap\n");
1696 return STATUS_NO_MEMORY
;
1699 /* Allocate an Activation Context Stack */
1700 Status
= RtlAllocateActivationContextStack(&Teb
->ActivationContextStackPointer
);
1701 if (!NT_SUCCESS(Status
)) return Status
;
1703 // FIXME: Loader private heap is missing
1704 //DPRINT1("Loader private heap is missing\n");
1706 /* Check for Debug Heap */
1709 /* Query the setting */
1710 Status
= LdrQueryImageFileKeyOption(OptionsKey
,
1711 L
"DebugProcessHeapOnly",
1713 &DebugProcessHeapOnly
,
1717 if (NT_SUCCESS(Status
))
1719 /* Reset DPH if requested */
1720 if (RtlpPageHeapEnabled
&& DebugProcessHeapOnly
)
1722 RtlpDphGlobalFlags
&= ~DPH_FLAG_DLL_NOTIFY
;
1723 RtlpPageHeapEnabled
= FALSE
;
1728 /* Build the NTDLL Path */
1729 FullPath
.Buffer
= StringBuffer
;
1730 FullPath
.Length
= 0;
1731 FullPath
.MaximumLength
= sizeof(StringBuffer
);
1732 RtlInitUnicodeString(&NtSystemRoot
, SharedUserData
->NtSystemRoot
);
1733 RtlAppendUnicodeStringToString(&FullPath
, &NtSystemRoot
);
1734 RtlAppendUnicodeToString(&FullPath
, L
"\\System32\\");
1736 /* Open the Known DLLs directory */
1737 RtlInitUnicodeString(&KnownDllString
, L
"\\KnownDlls");
1738 InitializeObjectAttributes(&ObjectAttributes
,
1740 OBJ_CASE_INSENSITIVE
,
1743 Status
= ZwOpenDirectoryObject(&LdrpKnownDllObjectDirectory
,
1744 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
1747 /* Check if it exists */
1748 if (NT_SUCCESS(Status
))
1750 /* Open the Known DLLs Path */
1751 RtlInitUnicodeString(&KnownDllString
, L
"KnownDllPath");
1752 InitializeObjectAttributes(&ObjectAttributes
,
1754 OBJ_CASE_INSENSITIVE
,
1755 LdrpKnownDllObjectDirectory
,
1757 Status
= NtOpenSymbolicLinkObject(&SymLinkHandle
,
1758 SYMBOLIC_LINK_QUERY
,
1760 if (NT_SUCCESS(Status
))
1762 /* Query the path */
1763 LdrpKnownDllPath
.Length
= 0;
1764 LdrpKnownDllPath
.MaximumLength
= sizeof(LdrpKnownDllPathBuffer
);
1765 LdrpKnownDllPath
.Buffer
= LdrpKnownDllPathBuffer
;
1766 Status
= ZwQuerySymbolicLinkObject(SymLinkHandle
, &LdrpKnownDllPath
, NULL
);
1767 NtClose(SymLinkHandle
);
1768 if (!NT_SUCCESS(Status
))
1770 DPRINT1("LDR: %s - failed call to ZwQuerySymbolicLinkObject with status %x\n", "", Status
);
1776 /* Check if we failed */
1777 if (!NT_SUCCESS(Status
))
1779 /* Assume System32 */
1780 LdrpKnownDllObjectDirectory
= NULL
;
1781 RtlInitUnicodeString(&LdrpKnownDllPath
, StringBuffer
);
1782 LdrpKnownDllPath
.Length
-= sizeof(WCHAR
);
1785 /* If we have process parameters, get the default path and current path */
1786 if (ProcessParameters
)
1788 /* Check if we have a Dll Path */
1789 if (ProcessParameters
->DllPath
.Length
)
1792 LdrpDefaultPath
= *(PUNICODE_STRING
)&ProcessParameters
->DllPath
;
1796 /* We need a valid path */
1797 DPRINT1("No valid DllPath was given!\n");
1798 LdrpInitFailure(STATUS_INVALID_PARAMETER
);
1801 /* Set the current directory */
1802 CurrentDirectory
= ProcessParameters
->CurrentDirectory
.DosPath
;
1804 /* Check if it's empty or invalid */
1805 if ((!CurrentDirectory
.Buffer
) ||
1806 (CurrentDirectory
.Buffer
[0] == UNICODE_NULL
) ||
1807 (!CurrentDirectory
.Length
))
1809 /* Allocate space for the buffer */
1810 CurrentDirectory
.Buffer
= RtlAllocateHeap(Peb
->ProcessHeap
,
1813 sizeof(UNICODE_NULL
));
1814 if (!CurrentDirectory
.Buffer
)
1816 DPRINT1("LDR: LdrpInitializeProcess - unable to allocate current working directory buffer\n");
1820 /* Copy the drive of the system root */
1821 RtlMoveMemory(CurrentDirectory
.Buffer
,
1822 SharedUserData
->NtSystemRoot
,
1824 CurrentDirectory
.Buffer
[3] = UNICODE_NULL
;
1825 CurrentDirectory
.Length
= 3 * sizeof(WCHAR
);
1826 CurrentDirectory
.MaximumLength
= CurrentDirectory
.Length
+ sizeof(WCHAR
);
1829 DPRINT("Using dynamically allocd curdir\n");
1833 /* Use the local buffer */
1834 DPRINT("Using local system root\n");
1838 /* Setup Loader Data */
1840 InitializeListHead(&PebLdr
.InLoadOrderModuleList
);
1841 InitializeListHead(&PebLdr
.InMemoryOrderModuleList
);
1842 InitializeListHead(&PebLdr
.InInitializationOrderModuleList
);
1843 PebLdr
.Length
= sizeof(PEB_LDR_DATA
);
1844 PebLdr
.Initialized
= TRUE
;
1846 /* Allocate a data entry for the Image */
1847 LdrpImageEntry
= LdrpAllocateDataTableEntry(Peb
->ImageBaseAddress
);
1850 LdrpImageEntry
->EntryPoint
= LdrpFetchAddressOfEntryPoint(LdrpImageEntry
->DllBase
);
1851 LdrpImageEntry
->LoadCount
= -1;
1852 LdrpImageEntry
->EntryPointActivationContext
= 0;
1853 LdrpImageEntry
->FullDllName
= ImageFileName
;
1856 LdrpImageEntry
->Flags
= LDRP_COR_IMAGE
;
1858 LdrpImageEntry
->Flags
= 0;
1860 /* Check if the name is empty */
1861 if (!ImageFileName
.Buffer
[0])
1863 /* Use the same Base name */
1864 LdrpImageEntry
->BaseDllName
= LdrpImageEntry
->FullDllName
;
1868 /* Find the last slash */
1869 Current
= ImageFileName
.Buffer
;
1872 if (*Current
++ == '\\')
1875 NtDllName
= Current
;
1879 /* Did we find anything? */
1882 /* Use the same Base name */
1883 LdrpImageEntry
->BaseDllName
= LdrpImageEntry
->FullDllName
;
1887 /* Setup the name */
1888 LdrpImageEntry
->BaseDllName
.Length
= (USHORT
)((ULONG_PTR
)ImageFileName
.Buffer
+ ImageFileName
.Length
- (ULONG_PTR
)NtDllName
);
1889 LdrpImageEntry
->BaseDllName
.MaximumLength
= LdrpImageEntry
->BaseDllName
.Length
+ sizeof(WCHAR
);
1890 LdrpImageEntry
->BaseDllName
.Buffer
= (PWSTR
)((ULONG_PTR
)ImageFileName
.Buffer
+
1891 (ImageFileName
.Length
- LdrpImageEntry
->BaseDllName
.Length
));
1895 /* Processing done, insert it */
1896 LdrpInsertMemoryTableEntry(LdrpImageEntry
);
1897 LdrpImageEntry
->Flags
|= LDRP_ENTRY_PROCESSED
;
1899 /* Now add an entry for NTDLL */
1900 NtLdrEntry
= LdrpAllocateDataTableEntry(SystemArgument1
);
1901 NtLdrEntry
->Flags
= LDRP_IMAGE_DLL
;
1902 NtLdrEntry
->EntryPoint
= LdrpFetchAddressOfEntryPoint(NtLdrEntry
->DllBase
);
1903 NtLdrEntry
->LoadCount
= -1;
1904 NtLdrEntry
->EntryPointActivationContext
= 0;
1906 NtLdrEntry
->FullDllName
.Length
= FullPath
.Length
;
1907 NtLdrEntry
->FullDllName
.MaximumLength
= FullPath
.MaximumLength
;
1908 NtLdrEntry
->FullDllName
.Buffer
= StringBuffer
;
1909 RtlAppendUnicodeStringToString(&NtLdrEntry
->FullDllName
, &NtDllString
);
1911 NtLdrEntry
->BaseDllName
.Length
= NtDllString
.Length
;
1912 NtLdrEntry
->BaseDllName
.MaximumLength
= NtDllString
.MaximumLength
;
1913 NtLdrEntry
->BaseDllName
.Buffer
= NtDllString
.Buffer
;
1915 /* Processing done, insert it */
1916 LdrpNtDllDataTableEntry
= NtLdrEntry
;
1917 LdrpInsertMemoryTableEntry(NtLdrEntry
);
1919 /* Let the world know */
1922 DPRINT1("LDR: NEW PROCESS\n");
1923 DPRINT1(" Image Path: %wZ (%wZ)\n", &LdrpImageEntry
->FullDllName
, &LdrpImageEntry
->BaseDllName
);
1924 DPRINT1(" Current Directory: %wZ\n", &CurrentDirectory
);
1925 DPRINT1(" Search Path: %wZ\n", &LdrpDefaultPath
);
1928 /* Link the Init Order List */
1929 InsertHeadList(&Peb
->Ldr
->InInitializationOrderModuleList
,
1930 &LdrpNtDllDataTableEntry
->InInitializationOrderLinks
);
1932 /* Initialize Wine's active context implementation for the current process */
1935 /* Set the current directory */
1936 Status
= RtlSetCurrentDirectory_U(&CurrentDirectory
);
1937 if (!NT_SUCCESS(Status
))
1939 /* We failed, check if we should free it */
1940 if (FreeCurDir
) RtlFreeUnicodeString(&CurrentDirectory
);
1942 /* Set it to the NT Root */
1943 CurrentDirectory
= NtSystemRoot
;
1944 RtlSetCurrentDirectory_U(&CurrentDirectory
);
1948 /* We're done with it, free it */
1949 if (FreeCurDir
) RtlFreeUnicodeString(&CurrentDirectory
);
1952 /* Check if we should look for a .local file */
1953 if (ProcessParameters
->Flags
& RTL_USER_PROCESS_PARAMETERS_LOCAL_DLL_PATH
)
1956 DPRINT1("We don't support .local overrides yet\n");
1959 /* Check if the Application Verifier was enabled */
1960 if (Peb
->NtGlobalFlag
& FLG_POOL_ENABLE_TAIL_CHECK
)
1963 DPRINT1("We don't support Application Verifier yet\n");
1969 DPRINT1("We don't support .NET applications yet\n");
1972 /* FIXME: Load support for Terminal Services */
1973 if (NtHeader
->OptionalHeader
.Subsystem
== IMAGE_SUBSYSTEM_WINDOWS_GUI
)
1975 /* Load kernel32 and call BasePostImportInit... */
1976 DPRINT("Unimplemented codepath!\n");
1979 /* Walk the IAT and load all the DLLs */
1980 ImportStatus
= LdrpWalkImportDescriptor(LdrpDefaultPath
.Buffer
, LdrpImageEntry
);
1982 /* Check if relocation is needed */
1983 if (Peb
->ImageBaseAddress
!= (PVOID
)NtHeader
->OptionalHeader
.ImageBase
)
1985 DPRINT1("LDR: Performing EXE relocation\n");
1987 /* Change the protection to prepare for relocation */
1988 ViewBase
= Peb
->ImageBaseAddress
;
1989 Status
= LdrpSetProtection(ViewBase
, FALSE
);
1990 if (!NT_SUCCESS(Status
)) return Status
;
1992 /* Do the relocation */
1993 Status
= LdrRelocateImageWithBias(ViewBase
,
1997 STATUS_CONFLICTING_ADDRESSES
,
1998 STATUS_INVALID_IMAGE_FORMAT
);
1999 if (!NT_SUCCESS(Status
))
2001 DPRINT1("LdrRelocateImageWithBias() failed\n");
2005 /* Check if a start context was provided */
2008 DPRINT1("WARNING: Relocated EXE Context");
2009 UNIMPLEMENTED
; // We should support this
2010 return STATUS_INVALID_IMAGE_FORMAT
;
2013 /* Restore the protection */
2014 Status
= LdrpSetProtection(ViewBase
, TRUE
);
2015 if (!NT_SUCCESS(Status
)) return Status
;
2019 ListHead
= &Peb
->Ldr
->InLoadOrderModuleList
;
2020 NextEntry
= ListHead
->Flink
;
2021 while (ListHead
!= NextEntry
)
2023 NtLdrEntry
= CONTAINING_RECORD(NextEntry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2024 NtLdrEntry
->LoadCount
= -1;
2025 NextEntry
= NextEntry
->Flink
;
2028 /* Phase 0 is done */
2029 LdrpLdrDatabaseIsSetup
= TRUE
;
2031 /* Check whether all static imports were properly loaded and return here */
2032 if (!NT_SUCCESS(ImportStatus
)) return ImportStatus
;
2034 /* Initialize TLS */
2035 Status
= LdrpInitializeTls();
2036 if (!NT_SUCCESS(Status
))
2038 DPRINT1("LDR: LdrpProcessInitialization failed to initialize TLS slots; status %x\n",
2043 /* FIXME Mark the DLL Ranges for Stack Traces later */
2045 /* Notify the debugger now */
2046 if (Peb
->BeingDebugged
)
2051 /* Update show snaps again */
2052 ShowSnaps
= Peb
->NtGlobalFlag
& FLG_SHOW_LDR_SNAPS
;
2055 /* Validate the Image for MP Usage */
2056 if (LdrpNumberOfProcessors
> 1) LdrpValidateImageForMp(LdrpImageEntry
);
2058 /* Check NX Options */
2059 if (SharedUserData
->NXSupportPolicy
== 1)
2061 ExecuteOptions
= 0xD;
2063 else if (!SharedUserData
->NXSupportPolicy
)
2065 ExecuteOptions
= 0xA;
2069 ZwSetInformationProcess(NtCurrentProcess(),
2070 ProcessExecuteFlags
,
2074 // FIXME: Should be done by Application Compatibility features,
2075 // by reading the registry, etc...
2076 // For now, this is the old code from ntdll!RtlGetVersion().
2077 RtlInitEmptyUnicodeString(&Peb
->CSDVersion
, NULL
, 0);
2078 if (((Peb
->OSCSDVersion
>> 8) & 0xFF) != 0)
2080 WCHAR szCSDVersion
[128];
2082 ULONG Length
= ARRAYSIZE(szCSDVersion
) - 1;
2083 i
= _snwprintf(szCSDVersion
, Length
,
2085 ((Peb
->OSCSDVersion
>> 8) & 0xFF));
2088 /* Null-terminate if it was overflowed */
2089 szCSDVersion
[Length
] = UNICODE_NULL
;
2092 Length
*= sizeof(WCHAR
);
2093 Peb
->CSDVersion
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
2095 Length
+ sizeof(UNICODE_NULL
));
2096 if (Peb
->CSDVersion
.Buffer
)
2098 Peb
->CSDVersion
.Length
= Length
;
2099 Peb
->CSDVersion
.MaximumLength
= Length
+ sizeof(UNICODE_NULL
);
2101 RtlCopyMemory(Peb
->CSDVersion
.Buffer
,
2103 Peb
->CSDVersion
.MaximumLength
);
2104 Peb
->CSDVersion
.Buffer
[Peb
->CSDVersion
.Length
/ sizeof(WCHAR
)] = UNICODE_NULL
;
2108 /* Check if we had Shim Data */
2111 /* Load the Shim Engine */
2112 Peb
->AppCompatInfo
= NULL
;
2113 //LdrpLoadShimEngine(OldShimData, ImagePathName, OldShimData);
2114 DPRINT1("We do not support shims yet\n");
2118 /* Check for Application Compatibility Goo */
2119 //LdrQueryApplicationCompatibilityGoo(hKey);
2120 DPRINT("Querying app compat hacks is missing!\n");
2124 * FIXME: Check for special images, SecuROM, SafeDisc and other NX-
2125 * incompatible images.
2128 /* Now call the Init Routines */
2129 Status
= LdrpRunInitializeRoutines(Context
);
2130 if (!NT_SUCCESS(Status
))
2132 DPRINT1("LDR: LdrpProcessInitialization failed running initialization routines; status %x\n",
2137 /* FIXME: Unload the Shim Engine if it was loaded */
2139 /* Check if we have a user-defined Post Process Routine */
2140 if (NT_SUCCESS(Status
) && Peb
->PostProcessInitRoutine
)
2143 Peb
->PostProcessInitRoutine();
2146 /* Close the key if we have one opened */
2147 if (OptionsKey
) NtClose(OptionsKey
);
2155 LdrpInitFailure(NTSTATUS Status
)
2158 PPEB Peb
= NtCurrentPeb();
2160 /* Print a debug message */
2161 DPRINT1("LDR: Process initialization failure for %wZ; NTSTATUS = %08lx\n",
2162 &Peb
->ProcessParameters
->ImagePathName
, Status
);
2164 /* Raise a hard error */
2165 if (!LdrpFatalHardErrorCount
)
2167 ZwRaiseHardError(STATUS_APP_INIT_FAILURE
, 1, 0, (PULONG_PTR
)&Status
, OptionOk
, &Response
);
2173 LdrpInit(PCONTEXT Context
,
2174 PVOID SystemArgument1
,
2175 PVOID SystemArgument2
)
2177 LARGE_INTEGER Timeout
;
2178 PTEB Teb
= NtCurrentTeb();
2179 NTSTATUS Status
, LoaderStatus
= STATUS_SUCCESS
;
2180 MEMORY_BASIC_INFORMATION MemoryBasicInfo
;
2181 PPEB Peb
= NtCurrentPeb();
2183 DPRINT("LdrpInit() %p/%p\n",
2184 NtCurrentTeb()->RealClientId
.UniqueProcess
,
2185 NtCurrentTeb()->RealClientId
.UniqueThread
);
2188 /* Set the SList header usage */
2189 RtlpUse16ByteSLists
= SharedUserData
->ProcessorFeatures
[PF_COMPARE_EXCHANGE128
];
2192 /* Check if we have a deallocation stack */
2193 if (!Teb
->DeallocationStack
)
2195 /* We don't, set one */
2196 Status
= NtQueryVirtualMemory(NtCurrentProcess(),
2197 Teb
->NtTib
.StackLimit
,
2198 MemoryBasicInformation
,
2200 sizeof(MEMORY_BASIC_INFORMATION
),
2202 if (!NT_SUCCESS(Status
))
2205 LdrpInitFailure(Status
);
2206 RtlRaiseStatus(Status
);
2211 Teb
->DeallocationStack
= MemoryBasicInfo
.AllocationBase
;
2214 /* Now check if the process is already being initialized */
2215 while (_InterlockedCompareExchange(&LdrpProcessInitialized
,
2219 /* Set the timeout to 30 seconds */
2220 Timeout
.QuadPart
= Int32x32To64(30, -10000);
2222 /* Make sure the status hasn't changed */
2223 while (!LdrpProcessInitialized
)
2226 ZwDelayExecution(FALSE
, &Timeout
);
2230 /* Check if we have already setup LDR data */
2233 /* Setup the Loader Lock */
2234 Peb
->LoaderLock
= &LdrpLoaderLock
;
2236 /* Let other code know we're initializing */
2237 LdrpInLdrInit
= TRUE
;
2239 /* Protect with SEH */
2242 /* Initialize the Process */
2243 LoaderStatus
= LdrpInitializeProcess(Context
,
2246 /* Check for success and if MinimumStackCommit was requested */
2247 if (NT_SUCCESS(LoaderStatus
) && Peb
->MinimumStackCommit
)
2249 /* Enforce the limit */
2250 //LdrpTouchThreadStack(Peb->MinimumStackCommit);
2254 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
2256 /* Fail with the SEH error */
2257 LoaderStatus
= _SEH2_GetExceptionCode();
2261 /* We're not initializing anymore */
2262 LdrpInLdrInit
= FALSE
;
2264 /* Check if init worked */
2265 if (NT_SUCCESS(LoaderStatus
))
2267 /* Set the process as Initialized */
2268 _InterlockedIncrement(&LdrpProcessInitialized
);
2273 /* Loader data is there... is this a fork() ? */
2274 if(Peb
->InheritedAddressSpace
)
2276 /* Handle the fork() */
2277 //LoaderStatus = LdrpForkProcess();
2278 LoaderStatus
= STATUS_NOT_IMPLEMENTED
;
2283 /* This is a new thread initializing */
2284 LdrpInitializeThread(Context
);
2288 /* All done, test alert the thread */
2292 if (!NT_SUCCESS(LoaderStatus
))
2295 LdrpInitFailure(LoaderStatus
);
2296 RtlRaiseStatus(LoaderStatus
);