* Sync to recent trunk (r52563).
[reactos.git] / dll / ntdll / ldr / ldrinit.c
1 /*
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)
8 */
9
10 /* INCLUDES *****************************************************************/
11
12 #include <ntdll.h>
13 #include <win32k/callback.h>
14
15 #define NDEBUG
16 #include <debug.h>
17
18
19 /* GLOBALS *******************************************************************/
20
21 HKEY ImageExecOptionsKey;
22 HKEY Wow64ExecOptionsKey;
23 UNICODE_STRING ImageExecOptionsString = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options");
24 UNICODE_STRING Wow64OptionsString = RTL_CONSTANT_STRING(L"");
25 UNICODE_STRING NtDllString = RTL_CONSTANT_STRING(L"ntdll.dll");
26
27 BOOLEAN LdrpInLdrInit;
28 LONG LdrpProcessInitialized;
29 BOOLEAN LdrpLoaderLockInit;
30 BOOLEAN LdrpLdrDatabaseIsSetup;
31 BOOLEAN LdrpShutdownInProgress;
32 HANDLE LdrpShutdownThreadId;
33
34 BOOLEAN LdrpDllValidation;
35
36 PLDR_DATA_TABLE_ENTRY LdrpImageEntry;
37 PUNICODE_STRING LdrpTopLevelDllBeingLoaded;
38 WCHAR StringBuffer[156];
39 extern PTEB LdrpTopLevelDllBeingLoadedTeb; // defined in rtlsupp.c!
40 PLDR_DATA_TABLE_ENTRY LdrpCurrentDllInitializer;
41 PLDR_DATA_TABLE_ENTRY LdrpNtDllDataTableEntry;
42
43 RTL_BITMAP TlsBitMap;
44 RTL_BITMAP TlsExpansionBitMap;
45 RTL_BITMAP FlsBitMap;
46 BOOLEAN LdrpImageHasTls;
47 LIST_ENTRY LdrpTlsList;
48 ULONG LdrpNumberOfTlsEntries;
49 ULONG LdrpNumberOfProcessors;
50 PVOID NtDllBase;
51 LARGE_INTEGER RtlpTimeout;
52 BOOLEAN RtlpTimeoutDisable;
53 LIST_ENTRY LdrpHashTable[LDR_HASH_TABLE_ENTRIES];
54 LIST_ENTRY LdrpDllNotificationList;
55 HANDLE LdrpKnownDllObjectDirectory;
56 UNICODE_STRING LdrpKnownDllPath;
57 WCHAR LdrpKnownDllPathBuffer[128];
58 UNICODE_STRING LdrpDefaultPath;
59
60 PEB_LDR_DATA PebLdr;
61
62 RTL_CRITICAL_SECTION_DEBUG LdrpLoaderLockDebug;
63 RTL_CRITICAL_SECTION LdrpLoaderLock =
64 {
65 &LdrpLoaderLockDebug,
66 -1,
67 0,
68 0,
69 0,
70 0
71 };
72 RTL_CRITICAL_SECTION FastPebLock;
73
74 BOOLEAN ShowSnaps;
75
76 ULONG LdrpFatalHardErrorCount;
77 ULONG LdrpActiveUnloadCount;
78
79 //extern LIST_ENTRY RtlCriticalSectionList;
80
81 VOID RtlpInitializeVectoredExceptionHandling(VOID);
82 VOID NTAPI RtlpInitDeferedCriticalSection(VOID);
83 VOID RtlInitializeHeapManager(VOID);
84 extern BOOLEAN RtlpPageHeapEnabled;
85 extern ULONG RtlpDphGlobalFlags;
86
87 NTSTATUS LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders, PVOID ImageBase);
88
89 #ifdef _WIN64
90 #define DEFAULT_SECURITY_COOKIE 0x00002B992DDFA232ll
91 #else
92 #define DEFAULT_SECURITY_COOKIE 0xBB40E64E
93 #endif
94
95 /* FUNCTIONS *****************************************************************/
96
97 /*
98 * @implemented
99 */
100 NTSTATUS
101 NTAPI
102 LdrOpenImageFileOptionsKey(IN PUNICODE_STRING SubKey,
103 IN BOOLEAN Wow64,
104 OUT PHKEY NewKeyHandle)
105 {
106 PHKEY RootKeyLocation;
107 HANDLE RootKey;
108 UNICODE_STRING SubKeyString;
109 OBJECT_ATTRIBUTES ObjectAttributes;
110 NTSTATUS Status;
111 PWCHAR p1;
112
113 /* Check which root key to open */
114 if (Wow64)
115 RootKeyLocation = &Wow64ExecOptionsKey;
116 else
117 RootKeyLocation = &ImageExecOptionsKey;
118
119 /* Get the current key */
120 RootKey = *RootKeyLocation;
121
122 /* Setup the object attributes */
123 InitializeObjectAttributes(&ObjectAttributes,
124 Wow64 ?
125 &Wow64OptionsString : &ImageExecOptionsString,
126 OBJ_CASE_INSENSITIVE,
127 NULL,
128 NULL);
129
130 /* Open the root key */
131 Status = ZwOpenKey(&RootKey, KEY_ENUMERATE_SUB_KEYS, &ObjectAttributes);
132 if (NT_SUCCESS(Status))
133 {
134 /* Write the key handle */
135 if (_InterlockedCompareExchange((LONG*)RootKeyLocation, (LONG)RootKey, 0) != 0)
136 {
137 /* Someone already opened it, use it instead */
138 NtClose(RootKey);
139 RootKey = *RootKeyLocation;
140 }
141
142 /* Extract the name */
143 SubKeyString = *SubKey;
144 p1 = (PWCHAR)((ULONG_PTR)SubKeyString.Buffer + SubKeyString.Length);
145 while (SubKey->Length)
146 {
147 if (p1[-1] == L'\\') break;
148 p1--;
149 SubKeyString.Length -= sizeof(*p1);
150 }
151 SubKeyString.Buffer = p1;
152 SubKeyString.Length = SubKeyString.MaximumLength - SubKeyString.Length - sizeof(WCHAR);
153
154 /* Setup the object attributes */
155 InitializeObjectAttributes(&ObjectAttributes,
156 &SubKeyString,
157 OBJ_CASE_INSENSITIVE,
158 RootKey,
159 NULL);
160
161 /* Open the setting key */
162 Status = ZwOpenKey((PHANDLE)NewKeyHandle, GENERIC_READ, &ObjectAttributes);
163 }
164
165 /* Return to caller */
166 return Status;
167 }
168
169 /*
170 * @implemented
171 */
172 NTSTATUS
173 NTAPI
174 LdrQueryImageFileKeyOption(IN HKEY KeyHandle,
175 IN PCWSTR ValueName,
176 IN ULONG Type,
177 OUT PVOID Buffer,
178 IN ULONG BufferSize,
179 OUT PULONG ReturnedLength OPTIONAL)
180 {
181 ULONG KeyInfo[256];
182 UNICODE_STRING ValueNameString, IntegerString;
183 ULONG KeyInfoSize, ResultSize;
184 PKEY_VALUE_PARTIAL_INFORMATION KeyValueInformation = (PKEY_VALUE_PARTIAL_INFORMATION)&KeyInfo;
185 BOOLEAN FreeHeap = FALSE;
186 NTSTATUS Status;
187
188 /* Build a string for the value name */
189 Status = RtlInitUnicodeStringEx(&ValueNameString, ValueName);
190 if (!NT_SUCCESS(Status)) return Status;
191
192 /* Query the value */
193 Status = NtQueryValueKey(KeyHandle,
194 &ValueNameString,
195 KeyValuePartialInformation,
196 KeyValueInformation,
197 sizeof(KeyInfo),
198 &ResultSize);
199 if (Status == STATUS_BUFFER_OVERFLOW)
200 {
201 /* Our local buffer wasn't enough, allocate one */
202 KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) +
203 KeyValueInformation->DataLength;
204 KeyValueInformation = RtlAllocateHeap(RtlGetProcessHeap(),
205 0,
206 KeyInfoSize);
207 if (KeyInfo == NULL)
208 {
209 /* Give up this time */
210 Status = STATUS_NO_MEMORY;
211 }
212
213 /* Try again */
214 Status = NtQueryValueKey(KeyHandle,
215 &ValueNameString,
216 KeyValuePartialInformation,
217 KeyValueInformation,
218 KeyInfoSize,
219 &ResultSize);
220 FreeHeap = TRUE;
221 }
222
223 /* Check for success */
224 if (NT_SUCCESS(Status))
225 {
226 /* Handle binary data */
227 if (KeyValueInformation->Type == REG_BINARY)
228 {
229 /* Check validity */
230 if ((Buffer) && (KeyValueInformation->DataLength <= BufferSize))
231 {
232 /* Copy into buffer */
233 RtlMoveMemory(Buffer,
234 &KeyValueInformation->Data,
235 KeyValueInformation->DataLength);
236 }
237 else
238 {
239 Status = STATUS_BUFFER_OVERFLOW;
240 }
241
242 /* Copy the result length */
243 if (ReturnedLength) *ReturnedLength = KeyValueInformation->DataLength;
244 }
245 else if (KeyValueInformation->Type == REG_DWORD)
246 {
247 /* Check for valid type */
248 if (KeyValueInformation->Type != Type)
249 {
250 /* Error */
251 Status = STATUS_OBJECT_TYPE_MISMATCH;
252 }
253 else
254 {
255 /* Check validity */
256 if ((Buffer) &&
257 (BufferSize == sizeof(ULONG)) &&
258 (KeyValueInformation->DataLength <= BufferSize))
259 {
260 /* Copy into buffer */
261 RtlMoveMemory(Buffer,
262 &KeyValueInformation->Data,
263 KeyValueInformation->DataLength);
264 }
265 else
266 {
267 Status = STATUS_BUFFER_OVERFLOW;
268 }
269
270 /* Copy the result length */
271 if (ReturnedLength) *ReturnedLength = KeyValueInformation->DataLength;
272 }
273 }
274 else if (KeyValueInformation->Type != REG_SZ)
275 {
276 /* We got something weird */
277 Status = STATUS_OBJECT_TYPE_MISMATCH;
278 }
279 else
280 {
281 /* String, check what you requested */
282 if (Type == REG_DWORD)
283 {
284 /* Validate */
285 if (BufferSize != sizeof(ULONG))
286 {
287 /* Invalid size */
288 BufferSize = 0;
289 Status = STATUS_INFO_LENGTH_MISMATCH;
290 }
291 else
292 {
293 /* OK, we know what you want... */
294 IntegerString.Buffer = (PWSTR)KeyValueInformation->Data;
295 IntegerString.Length = KeyValueInformation->DataLength -
296 sizeof(WCHAR);
297 IntegerString.MaximumLength = KeyValueInformation->DataLength;
298 Status = RtlUnicodeStringToInteger(&IntegerString, 0, (PULONG)Buffer);
299 }
300 }
301 else
302 {
303 /* Validate */
304 if (KeyValueInformation->DataLength > BufferSize)
305 {
306 /* Invalid */
307 Status = STATUS_BUFFER_OVERFLOW;
308 }
309 else
310 {
311 /* Set the size */
312 BufferSize = KeyValueInformation->DataLength;
313 }
314
315 /* Copy the string */
316 RtlMoveMemory(Buffer, &KeyValueInformation->Data, BufferSize);
317 }
318
319 /* Copy the result length */
320 if (ReturnedLength) *ReturnedLength = KeyValueInformation->DataLength;
321 }
322 }
323
324 /* Check if buffer was in heap */
325 if (FreeHeap) RtlFreeHeap(RtlGetProcessHeap(), 0, KeyValueInformation);
326
327 /* Close key and return */
328 NtClose(KeyHandle);
329 return Status;
330 }
331
332 /*
333 * @implemented
334 */
335 NTSTATUS
336 NTAPI
337 LdrQueryImageFileExecutionOptionsEx(IN PUNICODE_STRING SubKey,
338 IN PCWSTR ValueName,
339 IN ULONG Type,
340 OUT PVOID Buffer,
341 IN ULONG BufferSize,
342 OUT PULONG ReturnedLength OPTIONAL,
343 IN BOOLEAN Wow64)
344 {
345 NTSTATUS Status;
346 HKEY KeyHandle;
347
348 /* Open a handle to the key */
349 Status = LdrOpenImageFileOptionsKey(SubKey, Wow64, &KeyHandle);
350
351 /* Check for success */
352 if (NT_SUCCESS(Status))
353 {
354 /* Query the data */
355 Status = LdrQueryImageFileKeyOption(KeyHandle,
356 ValueName,
357 Type,
358 Buffer,
359 BufferSize,
360 ReturnedLength);
361
362 /* Close the key */
363 NtClose(KeyHandle);
364 }
365
366 /* Return to caller */
367 return Status;
368 }
369
370 /*
371 * @implemented
372 */
373 NTSTATUS
374 NTAPI
375 LdrQueryImageFileExecutionOptions(IN PUNICODE_STRING SubKey,
376 IN PCWSTR ValueName,
377 IN ULONG Type,
378 OUT PVOID Buffer,
379 IN ULONG BufferSize,
380 OUT PULONG ReturnedLength OPTIONAL)
381 {
382 /* Call the newer function */
383 return LdrQueryImageFileExecutionOptionsEx(SubKey,
384 ValueName,
385 Type,
386 Buffer,
387 BufferSize,
388 ReturnedLength,
389 FALSE);
390 }
391
392 VOID
393 NTAPI
394 LdrpEnsureLoaderLockIsHeld()
395 {
396 // Ignored atm
397 }
398
399 PVOID
400 NTAPI
401 LdrpFetchAddressOfSecurityCookie(PVOID BaseAddress, ULONG SizeOfImage)
402 {
403 PIMAGE_LOAD_CONFIG_DIRECTORY ConfigDir;
404 ULONG DirSize;
405 PVOID Cookie = NULL;
406
407 /* Check NT header first */
408 if (!RtlImageNtHeader(BaseAddress)) return NULL;
409
410 /* Get the pointer to the config directory */
411 ConfigDir = RtlImageDirectoryEntryToData(BaseAddress,
412 TRUE,
413 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG,
414 &DirSize);
415
416 /* Check for sanity */
417 if (!ConfigDir ||
418 (DirSize != 64 && ConfigDir->Size != DirSize) ||
419 (ConfigDir->Size < 0x48))
420 return NULL;
421
422 /* Now get the cookie */
423 Cookie = (PVOID)ConfigDir->SecurityCookie;
424
425 /* Check this cookie */
426 if ((PCHAR)Cookie <= (PCHAR)BaseAddress ||
427 (PCHAR)Cookie >= (PCHAR)BaseAddress + SizeOfImage)
428 {
429 Cookie = NULL;
430 }
431
432 /* Return validated security cookie */
433 return Cookie;
434 }
435
436 PVOID
437 NTAPI
438 LdrpInitSecurityCookie(PLDR_DATA_TABLE_ENTRY LdrEntry)
439 {
440 PULONG_PTR Cookie;
441 LARGE_INTEGER Counter;
442 //ULONG NewCookie;
443
444 /* Fetch address of the cookie */
445 Cookie = LdrpFetchAddressOfSecurityCookie(LdrEntry->DllBase, LdrEntry->SizeOfImage);
446
447 if (Cookie)
448 {
449 /* Check if it's a default one */
450 if (*Cookie == DEFAULT_SECURITY_COOKIE ||
451 *Cookie == 0xBB40)
452 {
453 /* We need to initialize it */
454
455 NtQueryPerformanceCounter(&Counter, NULL);
456 #if 0
457 GetSystemTimeAsFileTime (&systime.ft_struct);
458 #ifdef _WIN64
459 cookie = systime.ft_scalar;
460 #else
461 cookie = systime.ft_struct.dwLowDateTime;
462 cookie ^= systime.ft_struct.dwHighDateTime;
463 #endif
464
465 cookie ^= GetCurrentProcessId ();
466 cookie ^= GetCurrentThreadId ();
467 cookie ^= GetTickCount ();
468
469 QueryPerformanceCounter (&perfctr);
470 #ifdef _WIN64
471 cookie ^= perfctr.QuadPart;
472 #else
473 cookie ^= perfctr.LowPart;
474 cookie ^= perfctr.HighPart;
475 #endif
476
477 #ifdef _WIN64
478 cookie &= 0x0000ffffffffffffll;
479 #endif
480 #endif
481 *Cookie = Counter.LowPart;
482
483 //Cookie = NULL;
484 }
485 }
486
487 return Cookie;
488 }
489
490 VOID
491 NTAPI
492 LdrpInitializeThread(IN PCONTEXT Context)
493 {
494 PPEB Peb = NtCurrentPeb();
495 PLDR_DATA_TABLE_ENTRY LdrEntry;
496 PLIST_ENTRY NextEntry, ListHead;
497 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx;
498 NTSTATUS Status;
499 PVOID EntryPoint;
500
501 DPRINT("LdrpInitializeThread() called for %wZ\n",
502 &LdrpImageEntry->BaseDllName);
503
504 /* Allocate an Activation Context Stack */
505 /* FIXME: This is a hack for Wine's actctx stuff */
506 DPRINT("ActivationContextStack %p\n", NtCurrentTeb()->ActivationContextStackPointer);
507 if (!(NtCurrentTeb()->ActivationContextStackPointer))
508 {
509 Status = RtlAllocateActivationContextStack((PVOID*)&NtCurrentTeb()->ActivationContextStackPointer);
510 if (NT_SUCCESS(Status))
511 {
512 DPRINT("ActivationContextStack %p\n", NtCurrentTeb()->ActivationContextStackPointer);
513 DPRINT("ActiveFrame %p\n", ((PACTIVATION_CONTEXT_STACK)NtCurrentTeb()->ActivationContextStackPointer)->ActiveFrame);
514 NtCurrentTeb()->ActivationContextStackPointer->ActiveFrame = NULL;
515 }
516 else
517 DPRINT1("Warning: Unable to allocate ActivationContextStack\n");
518 }
519
520 /* Make sure we are not shutting down */
521 if (LdrpShutdownInProgress) return;
522
523 /* Allocate TLS */
524 LdrpAllocateTls();
525
526 /* Start at the beginning */
527 ListHead = &Peb->Ldr->InMemoryOrderModuleList;
528 NextEntry = ListHead->Flink;
529 while (NextEntry != ListHead)
530 {
531 /* Get the current entry */
532 LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InMemoryOrderModuleList);
533
534 /* Make sure it's not ourselves */
535 if (Peb->ImageBaseAddress != LdrEntry->DllBase)
536 {
537 /* Check if we should call */
538 if (!(LdrEntry->Flags & LDRP_DONT_CALL_FOR_THREADS))
539 {
540 /* Get the entrypoint */
541 EntryPoint = LdrEntry->EntryPoint;
542
543 /* Check if we are ready to call it */
544 if ((EntryPoint) &&
545 (LdrEntry->Flags & LDRP_PROCESS_ATTACH_CALLED) &&
546 (LdrEntry->Flags & LDRP_IMAGE_DLL))
547 {
548 /* Set up the Act Ctx */
549 ActCtx.Size = sizeof(ActCtx);
550 ActCtx.Format = 1;
551 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
552
553 /* Activate the ActCtx */
554 RtlActivateActivationContextUnsafeFast(&ActCtx,
555 LdrEntry->EntryPointActivationContext);
556
557 /* Check if it has TLS */
558 if (LdrEntry->TlsIndex)
559 {
560 /* Make sure we're not shutting down */
561 if (!LdrpShutdownInProgress)
562 {
563 /* Call TLS */
564 LdrpTlsCallback(LdrEntry->DllBase, DLL_THREAD_ATTACH);
565 }
566 }
567
568 /* Make sure we're not shutting down */
569 if (!LdrpShutdownInProgress)
570 {
571 /* Call the Entrypoint */
572 DPRINT("%wZ - Calling entry point at %x for thread attaching\n",
573 &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
574 LdrpCallDllEntry(LdrEntry->EntryPoint,
575 LdrEntry->DllBase,
576 DLL_THREAD_ATTACH,
577 NULL);
578 }
579
580 /* Deactivate the ActCtx */
581 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
582 }
583 }
584 }
585
586 /* Next entry */
587 NextEntry = NextEntry->Flink;
588 }
589
590 /* Check for TLS */
591 if (LdrpImageHasTls && !LdrpShutdownInProgress)
592 {
593 /* Set up the Act Ctx */
594 ActCtx.Size = sizeof(ActCtx);
595 ActCtx.Format = 1;
596 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
597
598 /* Activate the ActCtx */
599 RtlActivateActivationContextUnsafeFast(&ActCtx,
600 LdrpImageEntry->EntryPointActivationContext);
601
602 /* Do TLS callbacks */
603 LdrpTlsCallback(Peb->ImageBaseAddress, DLL_THREAD_ATTACH);
604
605 /* Deactivate the ActCtx */
606 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
607 }
608
609 DPRINT("LdrpInitializeThread() done\n");
610 }
611
612 NTSTATUS
613 NTAPI
614 LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
615 {
616 PLDR_DATA_TABLE_ENTRY LocalArray[16];
617 PLIST_ENTRY ListHead;
618 PLIST_ENTRY NextEntry;
619 PLDR_DATA_TABLE_ENTRY LdrEntry, *LdrRootEntry, OldInitializer;
620 PVOID EntryPoint;
621 ULONG Count, i;
622 //ULONG BreakOnInit;
623 NTSTATUS Status = STATUS_SUCCESS;
624 PPEB Peb = NtCurrentPeb();
625 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx;
626 ULONG BreakOnDllLoad;
627 PTEB OldTldTeb;
628 BOOLEAN DllStatus;
629
630 DPRINT("LdrpRunInitializeRoutines() called for %wZ\n", &LdrpImageEntry->BaseDllName);
631
632 /* Check the Loader Lock */
633 LdrpEnsureLoaderLockIsHeld();
634
635 /* Get the number of entries to call */
636 if ((Count = LdrpClearLoadInProgress()))
637 {
638 /* Check if we can use our local buffer */
639 if (Count > 16)
640 {
641 /* Allocate space for all the entries */
642 LdrRootEntry = RtlAllocateHeap(RtlGetProcessHeap(),
643 0,
644 Count * sizeof(LdrRootEntry));
645 if (!LdrRootEntry) return STATUS_NO_MEMORY;
646 }
647 else
648 {
649 /* Use our local array */
650 LdrRootEntry = LocalArray;
651 }
652 }
653 else
654 {
655 /* Don't need one */
656 LdrRootEntry = NULL;
657 }
658
659 /* Show debug message */
660 if (ShowSnaps)
661 {
662 DPRINT1("[%x,%x] LDR: Real INIT LIST for Process %wZ\n",
663 NtCurrentTeb()->RealClientId.UniqueThread,
664 NtCurrentTeb()->RealClientId.UniqueProcess,
665 &Peb->ProcessParameters->ImagePathName);
666 }
667
668 /* Loop in order */
669 ListHead = &Peb->Ldr->InInitializationOrderModuleList;
670 NextEntry = ListHead->Flink;
671 i = 0;
672 while (NextEntry != ListHead)
673 {
674 /* Get the Data Entry */
675 LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InInitializationOrderModuleList);
676
677 /* Check if we have a Root Entry */
678 if (LdrRootEntry)
679 {
680 /* Check flags */
681 if (!(LdrEntry->Flags & LDRP_ENTRY_PROCESSED))
682 {
683 /* Setup the Cookie for the DLL */
684 LdrpInitSecurityCookie(LdrEntry);
685
686 /* Check for valid entrypoint */
687 if (LdrEntry->EntryPoint)
688 {
689 /* Write in array */
690 LdrRootEntry[i] = LdrEntry;
691
692 /* Display debug message */
693 if (ShowSnaps)
694 {
695 DPRINT1("[%x,%x] LDR: %wZ init routine %p\n",
696 NtCurrentTeb()->RealClientId.UniqueThread,
697 NtCurrentTeb()->RealClientId.UniqueProcess,
698 &LdrEntry->FullDllName,
699 LdrEntry->EntryPoint);
700 }
701 i++;
702 }
703 }
704 }
705
706 /* Set the flag */
707 LdrEntry->Flags |= LDRP_ENTRY_PROCESSED;
708 NextEntry = NextEntry->Flink;
709 }
710
711 /* If we got a context, then we have to call Kernel32 for TS support */
712 if (Context)
713 {
714 /* Check if we have one */
715 //if (Kernel32ProcessInitPostImportfunction)
716 //{
717 /* Call it */
718 //Kernel32ProcessInitPostImportfunction();
719 //}
720
721 /* Clear it */
722 //Kernel32ProcessInitPostImportfunction = NULL;
723 UNIMPLEMENTED;
724 }
725
726 /* No root entry? return */
727 if (!LdrRootEntry) return STATUS_SUCCESS;
728
729 /* Set the TLD TEB */
730 OldTldTeb = LdrpTopLevelDllBeingLoadedTeb;
731 LdrpTopLevelDllBeingLoadedTeb = NtCurrentTeb();
732
733 /* Loop */
734 i = 0;
735 while (i < Count)
736 {
737 /* Get an entry */
738 LdrEntry = LdrRootEntry[i];
739
740 /* FIXME: Verify NX Compat */
741
742 /* Move to next entry */
743 i++;
744
745 /* Get its entrypoint */
746 EntryPoint = LdrEntry->EntryPoint;
747
748 /* Are we being debugged? */
749 BreakOnDllLoad = 0;
750 if (Peb->BeingDebugged || Peb->ReadImageFileExecOptions)
751 {
752 /* Check if we should break on load */
753 Status = LdrQueryImageFileExecutionOptions(&LdrEntry->BaseDllName,
754 L"BreakOnDllLoad",
755 REG_DWORD,
756 &BreakOnDllLoad,
757 sizeof(ULONG),
758 NULL);
759 if (!NT_SUCCESS(Status)) BreakOnDllLoad = 0;
760
761 /* Reset status back to STATUS_SUCCESS */
762 Status = STATUS_SUCCESS;
763 }
764
765 /* Break if aksed */
766 if (BreakOnDllLoad)
767 {
768 /* Check if we should show a message */
769 if (ShowSnaps)
770 {
771 DPRINT1("LDR: %wZ loaded.", &LdrEntry->BaseDllName);
772 DPRINT1(" - About to call init routine at %p\n", EntryPoint);
773 }
774
775 /* Break in debugger */
776 DbgBreakPoint();
777 }
778
779 /* Make sure we have an entrypoint */
780 if (EntryPoint)
781 {
782 /* Save the old Dll Initializer and write the current one */
783 OldInitializer = LdrpCurrentDllInitializer;
784 LdrpCurrentDllInitializer = LdrEntry;
785
786 /* Set up the Act Ctx */
787 ActCtx.Size = sizeof(ActCtx);
788 ActCtx.Format = 1;
789 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
790
791 /* Activate the ActCtx */
792 RtlActivateActivationContextUnsafeFast(&ActCtx,
793 LdrEntry->EntryPointActivationContext);
794
795 /* Check if it has TLS */
796 if (LdrEntry->TlsIndex && Context)
797 {
798 /* Call TLS */
799 LdrpTlsCallback(LdrEntry->DllBase, DLL_PROCESS_ATTACH);
800 }
801
802 /* Call the Entrypoint */
803 if (ShowSnaps)
804 {
805 DPRINT1("%wZ - Calling entry point at %p for DLL_PROCESS_ATTACH\n",
806 &LdrEntry->BaseDllName, EntryPoint);
807 }
808 DllStatus = LdrpCallDllEntry(EntryPoint,
809 LdrEntry->DllBase,
810 DLL_PROCESS_ATTACH,
811 Context);
812
813 /* Deactivate the ActCtx */
814 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
815
816 /* Save the Current DLL Initializer */
817 LdrpCurrentDllInitializer = OldInitializer;
818
819 /* Mark the entry as processed */
820 LdrEntry->Flags |= LDRP_PROCESS_ATTACH_CALLED;
821
822 /* Fail if DLL init failed */
823 if (!DllStatus)
824 {
825 DPRINT1("LDR: DLL_PROCESS_ATTACH for dll \"%wZ\" (InitRoutine: %p) failed\n",
826 &LdrEntry->BaseDllName, EntryPoint);
827
828 Status = STATUS_DLL_INIT_FAILED;
829 goto Quickie;
830 }
831 }
832 }
833
834 /* Loop in order */
835 ListHead = &Peb->Ldr->InInitializationOrderModuleList;
836 NextEntry = NextEntry->Flink;
837 while (NextEntry != ListHead)
838 {
839 /* Get the Data Entrry */
840 LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InInitializationOrderModuleList);
841
842 /* FIXME: Verify NX Compat */
843 // LdrpCheckNXCompatibility()
844
845 /* Next entry */
846 NextEntry = NextEntry->Flink;
847 }
848
849 /* Check for TLS */
850 if (LdrpImageHasTls && Context)
851 {
852 /* Set up the Act Ctx */
853 ActCtx.Size = sizeof(ActCtx);
854 ActCtx.Format = 1;
855 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
856
857 /* Activate the ActCtx */
858 RtlActivateActivationContextUnsafeFast(&ActCtx,
859 LdrpImageEntry->EntryPointActivationContext);
860
861 /* Do TLS callbacks */
862 LdrpTlsCallback(Peb->ImageBaseAddress, DLL_PROCESS_ATTACH);
863
864 /* Deactivate the ActCtx */
865 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
866 }
867
868 Quickie:
869 /* Restore old TEB */
870 LdrpTopLevelDllBeingLoadedTeb = OldTldTeb;
871
872 /* Check if the array is in the heap */
873 if (LdrRootEntry != LocalArray)
874 {
875 /* Free the array */
876 RtlFreeHeap(RtlGetProcessHeap(), 0, LdrRootEntry);
877 }
878
879 /* Return to caller */
880 DPRINT("LdrpRunInitializeRoutines() done\n");
881 return Status;
882 }
883
884 /*
885 * @implemented
886 */
887 NTSTATUS
888 NTAPI
889 LdrShutdownProcess(VOID)
890 {
891 PPEB Peb = NtCurrentPeb();
892 PLDR_DATA_TABLE_ENTRY LdrEntry;
893 PLIST_ENTRY NextEntry, ListHead;
894 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx;
895 PVOID EntryPoint;
896
897 DPRINT("LdrShutdownProcess() called for %wZ\n", &LdrpImageEntry->BaseDllName);
898 if (LdrpShutdownInProgress) return STATUS_SUCCESS;
899
900 /* Tell the Shim Engine */
901 //if (ShimsEnabled)
902 //{
903 /* FIXME */
904 //}
905
906 /* Tell the world */
907 if (ShowSnaps)
908 {
909 DPRINT1("\n");
910 }
911
912 /* Set the shutdown variables */
913 LdrpShutdownThreadId = NtCurrentTeb()->RealClientId.UniqueThread;
914 LdrpShutdownInProgress = TRUE;
915
916 /* Enter the Loader Lock */
917 RtlEnterCriticalSection(&LdrpLoaderLock);
918
919 /* Cleanup trace logging data (Etw) */
920 if (SharedUserData->TraceLogging)
921 {
922 /* FIXME */
923 DPRINT1("We don't support Etw yet.\n");
924 }
925
926 /* Start at the end */
927 ListHead = &Peb->Ldr->InInitializationOrderModuleList;
928 NextEntry = ListHead->Blink;
929 while (NextEntry != ListHead)
930 {
931 /* Get the current entry */
932 LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InInitializationOrderModuleList);
933 NextEntry = NextEntry->Blink;
934
935 /* Make sure it's not ourselves */
936 if (Peb->ImageBaseAddress != LdrEntry->DllBase)
937 {
938 /* Get the entrypoint */
939 EntryPoint = LdrEntry->EntryPoint;
940
941 /* Check if we are ready to call it */
942 if (EntryPoint &&
943 (LdrEntry->Flags & LDRP_PROCESS_ATTACH_CALLED) &&
944 LdrEntry->Flags)
945 {
946 /* Set up the Act Ctx */
947 ActCtx.Size = sizeof(ActCtx);
948 ActCtx.Format = 1;
949 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
950
951 /* Activate the ActCtx */
952 RtlActivateActivationContextUnsafeFast(&ActCtx,
953 LdrEntry->EntryPointActivationContext);
954
955 /* Check if it has TLS */
956 if (LdrEntry->TlsIndex)
957 {
958 /* Call TLS */
959 LdrpTlsCallback(LdrEntry->DllBase, DLL_PROCESS_DETACH);
960 }
961
962 /* Call the Entrypoint */
963 DPRINT("%wZ - Calling entry point at %x for thread detaching\n",
964 &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
965 LdrpCallDllEntry(EntryPoint,
966 LdrEntry->DllBase,
967 DLL_PROCESS_DETACH,
968 (PVOID)1);
969
970 /* Deactivate the ActCtx */
971 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
972 }
973 }
974 }
975
976 /* Check for TLS */
977 if (LdrpImageHasTls)
978 {
979 /* Set up the Act Ctx */
980 ActCtx.Size = sizeof(ActCtx);
981 ActCtx.Format = 1;
982 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
983
984 /* Activate the ActCtx */
985 RtlActivateActivationContextUnsafeFast(&ActCtx,
986 LdrpImageEntry->EntryPointActivationContext);
987
988 /* Do TLS callbacks */
989 LdrpTlsCallback(Peb->ImageBaseAddress, DLL_PROCESS_DETACH);
990
991 /* Deactivate the ActCtx */
992 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
993 }
994
995 /* FIXME: Do Heap detection and Etw final shutdown */
996
997 /* Release the lock */
998 RtlLeaveCriticalSection(&LdrpLoaderLock);
999 DPRINT("LdrpShutdownProcess() done\n");
1000
1001 return STATUS_SUCCESS;
1002 }
1003
1004 /*
1005 * @implemented
1006 */
1007 NTSTATUS
1008 NTAPI
1009 LdrShutdownThread(VOID)
1010 {
1011 PPEB Peb = NtCurrentPeb();
1012 PTEB Teb = NtCurrentTeb();
1013 PLDR_DATA_TABLE_ENTRY LdrEntry;
1014 PLIST_ENTRY NextEntry, ListHead;
1015 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx;
1016 PVOID EntryPoint;
1017
1018 DPRINT("LdrShutdownThread() called for %wZ\n",
1019 &LdrpImageEntry->BaseDllName);
1020
1021 /* Cleanup trace logging data (Etw) */
1022 if (SharedUserData->TraceLogging)
1023 {
1024 /* FIXME */
1025 DPRINT1("We don't support Etw yet.\n");
1026 }
1027
1028 /* Get the Ldr Lock */
1029 RtlEnterCriticalSection(&LdrpLoaderLock);
1030
1031 /* Start at the end */
1032 ListHead = &Peb->Ldr->InInitializationOrderModuleList;
1033 NextEntry = ListHead->Blink;
1034 while (NextEntry != ListHead)
1035 {
1036 /* Get the current entry */
1037 LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InInitializationOrderModuleList);
1038 NextEntry = NextEntry->Blink;
1039
1040 /* Make sure it's not ourselves */
1041 if (Peb->ImageBaseAddress != LdrEntry->DllBase)
1042 {
1043 /* Check if we should call */
1044 if (!(LdrEntry->Flags & LDRP_DONT_CALL_FOR_THREADS) &&
1045 (LdrEntry->Flags & LDRP_PROCESS_ATTACH_CALLED) &&
1046 (LdrEntry->Flags & LDRP_IMAGE_DLL))
1047 {
1048 /* Get the entrypoint */
1049 EntryPoint = LdrEntry->EntryPoint;
1050
1051 /* Check if we are ready to call it */
1052 if (EntryPoint)
1053 {
1054 /* Set up the Act Ctx */
1055 ActCtx.Size = sizeof(ActCtx);
1056 ActCtx.Format = 1;
1057 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
1058
1059 /* Activate the ActCtx */
1060 RtlActivateActivationContextUnsafeFast(&ActCtx,
1061 LdrEntry->EntryPointActivationContext);
1062
1063 /* Check if it has TLS */
1064 if (LdrEntry->TlsIndex)
1065 {
1066 /* Make sure we're not shutting down */
1067 if (!LdrpShutdownInProgress)
1068 {
1069 /* Call TLS */
1070 LdrpTlsCallback(LdrEntry->DllBase, DLL_THREAD_DETACH);
1071 }
1072 }
1073
1074 /* Make sure we're not shutting down */
1075 if (!LdrpShutdownInProgress)
1076 {
1077 /* Call the Entrypoint */
1078 DPRINT("%wZ - Calling entry point at %x for thread detaching\n",
1079 &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
1080 LdrpCallDllEntry(EntryPoint,
1081 LdrEntry->DllBase,
1082 DLL_THREAD_DETACH,
1083 NULL);
1084 }
1085
1086 /* Deactivate the ActCtx */
1087 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
1088 }
1089 }
1090 }
1091 }
1092
1093 /* Check for TLS */
1094 if (LdrpImageHasTls)
1095 {
1096 /* Set up the Act Ctx */
1097 ActCtx.Size = sizeof(ActCtx);
1098 ActCtx.Format = 1;
1099 RtlZeroMemory(&ActCtx.Frame, sizeof(RTL_ACTIVATION_CONTEXT_STACK_FRAME));
1100
1101 /* Activate the ActCtx */
1102 RtlActivateActivationContextUnsafeFast(&ActCtx,
1103 LdrpImageEntry->EntryPointActivationContext);
1104
1105 /* Do TLS callbacks */
1106 LdrpTlsCallback(Peb->ImageBaseAddress, DLL_THREAD_DETACH);
1107
1108 /* Deactivate the ActCtx */
1109 RtlDeactivateActivationContextUnsafeFast(&ActCtx);
1110 }
1111
1112 /* Free TLS */
1113 LdrpFreeTls();
1114 RtlLeaveCriticalSection(&LdrpLoaderLock);
1115
1116 /* Check for expansion slots */
1117 if (Teb->TlsExpansionSlots)
1118 {
1119 /* Free expansion slots */
1120 RtlFreeHeap(RtlGetProcessHeap(), 0, Teb->TlsExpansionSlots);
1121 }
1122
1123 /* Check for FLS Data */
1124 if (Teb->FlsData)
1125 {
1126 /* FIXME */
1127 DPRINT1("We don't support FLS Data yet\n");
1128 }
1129
1130 /* Check for Fiber data */
1131 if (Teb->HasFiberData)
1132 {
1133 /* Free Fiber data*/
1134 RtlFreeHeap(RtlGetProcessHeap(), 0, Teb->NtTib.FiberData);
1135 Teb->NtTib.FiberData = NULL;
1136 }
1137
1138 /* Free the activation context stack */
1139 RtlFreeThreadActivationContextStack();
1140 DPRINT("LdrShutdownThread() done\n");
1141
1142 return STATUS_SUCCESS;
1143 }
1144
1145 NTSTATUS
1146 NTAPI
1147 LdrpInitializeTls(VOID)
1148 {
1149 PLIST_ENTRY NextEntry, ListHead;
1150 PLDR_DATA_TABLE_ENTRY LdrEntry;
1151 PIMAGE_TLS_DIRECTORY TlsDirectory;
1152 PLDRP_TLS_DATA TlsData;
1153 ULONG Size;
1154
1155 /* Initialize the TLS List */
1156 InitializeListHead(&LdrpTlsList);
1157
1158 /* Loop all the modules */
1159 ListHead = &NtCurrentPeb()->Ldr->InLoadOrderModuleList;
1160 NextEntry = ListHead->Flink;
1161 while (ListHead != NextEntry)
1162 {
1163 /* Get the entry */
1164 LdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
1165 NextEntry = NextEntry->Flink;
1166
1167 /* Get the TLS directory */
1168 TlsDirectory = RtlImageDirectoryEntryToData(LdrEntry->DllBase,
1169 TRUE,
1170 IMAGE_DIRECTORY_ENTRY_TLS,
1171 &Size);
1172
1173 /* Check if we have a directory */
1174 if (!TlsDirectory) continue;
1175
1176 /* Check if the image has TLS */
1177 if (!LdrpImageHasTls) LdrpImageHasTls = TRUE;
1178
1179 /* Show debug message */
1180 if (ShowSnaps)
1181 {
1182 DPRINT1("LDR: Tls Found in %wZ at %p\n",
1183 &LdrEntry->BaseDllName,
1184 TlsDirectory);
1185 }
1186
1187 /* Allocate an entry */
1188 TlsData = RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof(LDRP_TLS_DATA));
1189 if (!TlsData) return STATUS_NO_MEMORY;
1190
1191 /* Lock the DLL and mark it for TLS Usage */
1192 LdrEntry->LoadCount = -1;
1193 LdrEntry->TlsIndex = -1;
1194
1195 /* Save the cached TLS data */
1196 TlsData->TlsDirectory = *TlsDirectory;
1197 InsertTailList(&LdrpTlsList, &TlsData->TlsLinks);
1198
1199 /* Update the index */
1200 *(PLONG)TlsData->TlsDirectory.AddressOfIndex = LdrpNumberOfTlsEntries;
1201 TlsData->TlsDirectory.Characteristics = LdrpNumberOfTlsEntries++;
1202 }
1203
1204 /* Done setting up TLS, allocate entries */
1205 return LdrpAllocateTls();
1206 }
1207
1208 NTSTATUS
1209 NTAPI
1210 LdrpAllocateTls(VOID)
1211 {
1212 PTEB Teb = NtCurrentTeb();
1213 PLIST_ENTRY NextEntry, ListHead;
1214 PLDRP_TLS_DATA TlsData;
1215 ULONG TlsDataSize;
1216 PVOID *TlsVector;
1217
1218 /* Check if we have any entries */
1219 if (!LdrpNumberOfTlsEntries)
1220 return STATUS_SUCCESS;
1221
1222 /* Allocate the vector array */
1223 TlsVector = RtlAllocateHeap(RtlGetProcessHeap(),
1224 0,
1225 LdrpNumberOfTlsEntries * sizeof(PVOID));
1226 if (!TlsVector) return STATUS_NO_MEMORY;
1227 Teb->ThreadLocalStoragePointer = TlsVector;
1228
1229 /* Loop the TLS Array */
1230 ListHead = &LdrpTlsList;
1231 NextEntry = ListHead->Flink;
1232 while (NextEntry != ListHead)
1233 {
1234 /* Get the entry */
1235 TlsData = CONTAINING_RECORD(NextEntry, LDRP_TLS_DATA, TlsLinks);
1236 NextEntry = NextEntry->Flink;
1237
1238 /* Allocate this vector */
1239 TlsDataSize = TlsData->TlsDirectory.EndAddressOfRawData -
1240 TlsData->TlsDirectory.StartAddressOfRawData;
1241 TlsVector[TlsData->TlsDirectory.Characteristics] = RtlAllocateHeap(RtlGetProcessHeap(),
1242 0,
1243 TlsDataSize);
1244 if (!TlsVector[TlsData->TlsDirectory.Characteristics])
1245 {
1246 /* Out of memory */
1247 return STATUS_NO_MEMORY;
1248 }
1249
1250 /* Show debug message */
1251 if (ShowSnaps)
1252 {
1253 DPRINT1("LDR: TlsVector %x Index %d = %x copied from %x to %x\n",
1254 TlsVector,
1255 TlsData->TlsDirectory.Characteristics,
1256 &TlsVector[TlsData->TlsDirectory.Characteristics],
1257 TlsData->TlsDirectory.StartAddressOfRawData,
1258 TlsVector[TlsData->TlsDirectory.Characteristics]);
1259 }
1260
1261 /* Copy the data */
1262 RtlCopyMemory(TlsVector[TlsData->TlsDirectory.Characteristics],
1263 (PVOID)TlsData->TlsDirectory.StartAddressOfRawData,
1264 TlsDataSize);
1265 }
1266
1267 /* Done */
1268 return STATUS_SUCCESS;
1269 }
1270
1271 VOID
1272 NTAPI
1273 LdrpFreeTls(VOID)
1274 {
1275 PLIST_ENTRY ListHead, NextEntry;
1276 PLDRP_TLS_DATA TlsData;
1277 PVOID *TlsVector;
1278 PTEB Teb = NtCurrentTeb();
1279
1280 /* Get a pointer to the vector array */
1281 TlsVector = Teb->ThreadLocalStoragePointer;
1282 if (!TlsVector) return;
1283
1284 /* Loop through it */
1285 ListHead = &LdrpTlsList;
1286 NextEntry = ListHead->Flink;
1287 while (NextEntry != ListHead)
1288 {
1289 TlsData = CONTAINING_RECORD(NextEntry, LDRP_TLS_DATA, TlsLinks);
1290 NextEntry = NextEntry->Flink;
1291
1292 /* Free each entry */
1293 if (TlsVector[TlsData->TlsDirectory.Characteristics])
1294 {
1295 RtlFreeHeap(RtlGetProcessHeap(),
1296 0,
1297 TlsVector[TlsData->TlsDirectory.Characteristics]);
1298 }
1299 }
1300
1301 /* Free the array itself */
1302 RtlFreeHeap(RtlGetProcessHeap(),
1303 0,
1304 TlsVector);
1305 }
1306
1307 NTSTATUS
1308 NTAPI
1309 LdrpInitializeExecutionOptions(PUNICODE_STRING ImagePathName, PPEB Peb, PHKEY OptionsKey)
1310 {
1311 UNIMPLEMENTED;
1312 *OptionsKey = NULL;
1313 return STATUS_SUCCESS;
1314 }
1315
1316 VOID
1317 NTAPI
1318 LdrpValidateImageForMp(IN PLDR_DATA_TABLE_ENTRY LdrDataTableEntry)
1319 {
1320 UNIMPLEMENTED;
1321 }
1322
1323 NTSTATUS
1324 NTAPI
1325 LdrpInitializeProcess(IN PCONTEXT Context,
1326 IN PVOID SystemArgument1)
1327 {
1328 RTL_HEAP_PARAMETERS HeapParameters;
1329 ULONG ComSectionSize;
1330 //ANSI_STRING FunctionName = RTL_CONSTANT_STRING("BaseQueryModuleData");
1331 PVOID OldShimData;
1332 OBJECT_ATTRIBUTES ObjectAttributes;
1333 //UNICODE_STRING LocalFileName, FullImageName;
1334 HANDLE SymLinkHandle;
1335 //ULONG DebugHeapOnly;
1336 UNICODE_STRING CommandLine, NtSystemRoot, ImagePathName, FullPath, ImageFileName, KnownDllString;
1337 PPEB Peb = NtCurrentPeb();
1338 BOOLEAN IsDotNetImage = FALSE;
1339 BOOLEAN FreeCurDir = FALSE;
1340 //HKEY CompatKey;
1341 PRTL_USER_PROCESS_PARAMETERS ProcessParameters;
1342 //LPWSTR ImagePathBuffer;
1343 ULONG ConfigSize;
1344 UNICODE_STRING CurrentDirectory;
1345 HKEY OptionsKey;
1346 ULONG HeapFlags;
1347 PIMAGE_NT_HEADERS NtHeader;
1348 LPWSTR NtDllName = NULL;
1349 NTSTATUS Status;
1350 NLSTABLEINFO NlsTable;
1351 PIMAGE_LOAD_CONFIG_DIRECTORY LoadConfig;
1352 PTEB Teb = NtCurrentTeb();
1353 PLIST_ENTRY ListHead;
1354 PLIST_ENTRY NextEntry;
1355 ULONG i;
1356 PWSTR ImagePath;
1357 ULONG DebugProcessHeapOnly = 0;
1358 PLDR_DATA_TABLE_ENTRY NtLdrEntry;
1359 PWCHAR Current;
1360 ULONG ExecuteOptions = 0;
1361
1362 /* Set a NULL SEH Filter */
1363 RtlSetUnhandledExceptionFilter(NULL);
1364
1365 /* Get the image path */
1366 ImagePath = Peb->ProcessParameters->ImagePathName.Buffer;
1367
1368 /* Check if it's normalized */
1369 if (Peb->ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_NORMALIZED)
1370 {
1371 /* Normalize it*/
1372 ImagePath = (PWSTR)((ULONG_PTR)ImagePath + (ULONG_PTR)Peb->ProcessParameters);
1373 }
1374
1375 /* Create a unicode string for the Image Path */
1376 ImagePathName.Length = Peb->ProcessParameters->ImagePathName.Length;
1377 ImagePathName.MaximumLength = ImagePathName.Length + sizeof(WCHAR);
1378 ImagePathName.Buffer = ImagePath;
1379
1380 /* Get the NT Headers */
1381 NtHeader = RtlImageNtHeader(Peb->ImageBaseAddress);
1382
1383 /* Get the execution options */
1384 Status = LdrpInitializeExecutionOptions(&ImagePathName, Peb, &OptionsKey);
1385
1386 /* Check if this is a .NET executable */
1387 if (RtlImageDirectoryEntryToData(Peb->ImageBaseAddress,
1388 TRUE,
1389 IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR,
1390 &ComSectionSize))
1391 {
1392 /* Remeber this for later */
1393 IsDotNetImage = TRUE;
1394 }
1395
1396 /* Save the NTDLL Base address */
1397 NtDllBase = SystemArgument1;
1398
1399 /* If this is a Native Image */
1400 if (NtHeader->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_NATIVE)
1401 {
1402 /* Then do DLL Validation */
1403 LdrpDllValidation = TRUE;
1404 }
1405
1406 /* Save the old Shim Data */
1407 OldShimData = Peb->pShimData;
1408
1409 /* Clear it */
1410 Peb->pShimData = NULL;
1411
1412 /* Save the number of processors and CS Timeout */
1413 LdrpNumberOfProcessors = Peb->NumberOfProcessors;
1414 RtlpTimeout = Peb->CriticalSectionTimeout;
1415
1416 /* Normalize the parameters */
1417 ProcessParameters = RtlNormalizeProcessParams(Peb->ProcessParameters);
1418 ProcessParameters = Peb->ProcessParameters;
1419 if (ProcessParameters)
1420 {
1421 /* Save the Image and Command Line Names */
1422 ImageFileName = ProcessParameters->ImagePathName;
1423 CommandLine = ProcessParameters->CommandLine;
1424 }
1425 else
1426 {
1427 /* It failed, initialize empty strings */
1428 RtlInitUnicodeString(&ImageFileName, NULL);
1429 RtlInitUnicodeString(&CommandLine, NULL);
1430 }
1431
1432 /* Initialize NLS data */
1433 RtlInitNlsTables(Peb->AnsiCodePageData,
1434 Peb->OemCodePageData,
1435 Peb->UnicodeCaseTableData,
1436 &NlsTable);
1437
1438 /* Reset NLS Translations */
1439 RtlResetRtlTranslations(&NlsTable);
1440
1441 /* Get the Image Config Directory */
1442 LoadConfig = RtlImageDirectoryEntryToData(Peb->ImageBaseAddress,
1443 TRUE,
1444 IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG,
1445 &ConfigSize);
1446
1447 /* Setup the Heap Parameters */
1448 RtlZeroMemory(&HeapParameters, sizeof(RTL_HEAP_PARAMETERS));
1449 HeapFlags = HEAP_GROWABLE;
1450 HeapParameters.Length = sizeof(RTL_HEAP_PARAMETERS);
1451
1452 /* Check if we have Configuration Data */
1453 if ((LoadConfig) && (ConfigSize == sizeof(IMAGE_LOAD_CONFIG_DIRECTORY)))
1454 {
1455 /* FIXME: Custom heap settings and misc. */
1456 DPRINT1("We don't support LOAD_CONFIG data yet\n");
1457 }
1458
1459 /* Check for custom affinity mask */
1460 if (Peb->ImageProcessAffinityMask)
1461 {
1462 /* Set it */
1463 Status = NtSetInformationProcess(NtCurrentProcess(),
1464 ProcessAffinityMask,
1465 &Peb->ImageProcessAffinityMask,
1466 sizeof(Peb->ImageProcessAffinityMask));
1467 }
1468
1469 /* Check if verbose debugging (ShowSnaps) was requested */
1470 ShowSnaps = Peb->NtGlobalFlag & FLG_SHOW_LDR_SNAPS;
1471
1472 /* Start verbose debugging messages right now if they were requested */
1473 if (ShowSnaps)
1474 {
1475 DPRINT1("LDR: PID: 0x%x started - '%wZ'\n",
1476 Teb->ClientId.UniqueProcess,
1477 &CommandLine);
1478 }
1479
1480 /* If the timeout is too long */
1481 if (RtlpTimeout.QuadPart < Int32x32To64(3600, -10000000))
1482 {
1483 /* Then disable CS Timeout */
1484 RtlpTimeoutDisable = TRUE;
1485 }
1486
1487 /* Initialize Critical Section Data */
1488 RtlpInitDeferedCriticalSection();
1489
1490 /* Initialize VEH Call lists */
1491 RtlpInitializeVectoredExceptionHandling();
1492
1493 /* Set TLS/FLS Bitmap data */
1494 Peb->FlsBitmap = &FlsBitMap;
1495 Peb->TlsBitmap = &TlsBitMap;
1496 Peb->TlsExpansionBitmap = &TlsExpansionBitMap;
1497
1498 /* Initialize FLS Bitmap */
1499 RtlInitializeBitMap(&FlsBitMap,
1500 Peb->FlsBitmapBits,
1501 FLS_MAXIMUM_AVAILABLE);
1502 RtlSetBit(&FlsBitMap, 0);
1503
1504 /* Initialize TLS Bitmap */
1505 RtlInitializeBitMap(&TlsBitMap,
1506 Peb->TlsBitmapBits,
1507 TLS_MINIMUM_AVAILABLE);
1508 RtlSetBit(&TlsBitMap, 0);
1509 RtlInitializeBitMap(&TlsExpansionBitMap,
1510 Peb->TlsExpansionBitmapBits,
1511 TLS_EXPANSION_SLOTS);
1512 RtlSetBit(&TlsExpansionBitMap, 0);
1513
1514 /* Initialize the Hash Table */
1515 for (i = 0; i < LDR_HASH_TABLE_ENTRIES; i++)
1516 {
1517 InitializeListHead(&LdrpHashTable[i]);
1518 }
1519
1520 /* Initialize the Loader Lock */
1521 // FIXME: What's the point of initing it manually, if two lines lower
1522 // a call to RtlInitializeCriticalSection() is being made anyway?
1523 //InsertTailList(&RtlCriticalSectionList, &LdrpLoaderLock.DebugInfo->ProcessLocksList);
1524 //LdrpLoaderLock.DebugInfo->CriticalSection = &LdrpLoaderLock;
1525 RtlInitializeCriticalSection(&LdrpLoaderLock);
1526 LdrpLoaderLockInit = TRUE;
1527
1528 /* Check if User Stack Trace Database support was requested */
1529 if (Peb->NtGlobalFlag & FLG_USER_STACK_TRACE_DB)
1530 {
1531 DPRINT1("We don't support user stack trace databases yet\n");
1532 }
1533
1534 /* Setup Fast PEB Lock */
1535 RtlInitializeCriticalSection(&FastPebLock);
1536 Peb->FastPebLock = &FastPebLock;
1537 //Peb->FastPebLockRoutine = (PPEBLOCKROUTINE)RtlEnterCriticalSection;
1538 //Peb->FastPebUnlockRoutine = (PPEBLOCKROUTINE)RtlLeaveCriticalSection;
1539
1540 /* Setup Callout Lock and Notification list */
1541 //RtlInitializeCriticalSection(&RtlpCalloutEntryLock);
1542 InitializeListHead(&LdrpDllNotificationList);
1543
1544 /* For old executables, use 16-byte aligned heap */
1545 if ((NtHeader->OptionalHeader.MajorSubsystemVersion <= 3) &&
1546 (NtHeader->OptionalHeader.MinorSubsystemVersion < 51))
1547 {
1548 HeapFlags |= HEAP_CREATE_ALIGN_16;
1549 }
1550
1551 /* Setup the Heap */
1552 RtlInitializeHeapManager();
1553 Peb->ProcessHeap = RtlCreateHeap(HeapFlags,
1554 NULL,
1555 NtHeader->OptionalHeader.SizeOfHeapReserve,
1556 NtHeader->OptionalHeader.SizeOfHeapCommit,
1557 NULL,
1558 &HeapParameters);
1559
1560 if (!Peb->ProcessHeap)
1561 {
1562 DPRINT1("Failed to create process heap\n");
1563 return STATUS_NO_MEMORY;
1564 }
1565
1566 // FIXME: Is it located properly?
1567 /* Initialize table of callbacks for the kernel. */
1568 Peb->KernelCallbackTable = RtlAllocateHeap(RtlGetProcessHeap(),
1569 0,
1570 sizeof(PVOID) *
1571 (USER32_CALLBACK_MAXIMUM + 1));
1572 if (!Peb->KernelCallbackTable)
1573 {
1574 DPRINT1("Failed to create callback table\n");
1575 ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
1576 }
1577
1578 /* Allocate an Activation Context Stack */
1579 Status = RtlAllocateActivationContextStack((PVOID *)&Teb->ActivationContextStackPointer);
1580 if (!NT_SUCCESS(Status)) return Status;
1581
1582 // FIXME: Loader private heap is missing
1583 //DPRINT1("Loader private heap is missing\n");
1584
1585 /* Check for Debug Heap */
1586 if (OptionsKey)
1587 {
1588 /* Query the setting */
1589 Status = LdrQueryImageFileKeyOption(OptionsKey,
1590 L"DebugProcessHeapOnly",
1591 REG_DWORD,
1592 &DebugProcessHeapOnly,
1593 sizeof(ULONG),
1594 NULL);
1595
1596 if (NT_SUCCESS(Status))
1597 {
1598 /* Reset DPH if requested */
1599 if (RtlpPageHeapEnabled && DebugProcessHeapOnly)
1600 {
1601 RtlpDphGlobalFlags &= ~0x40;
1602 RtlpPageHeapEnabled = FALSE;
1603 }
1604 }
1605 }
1606
1607 /* Build the NTDLL Path */
1608 FullPath.Buffer = StringBuffer;
1609 FullPath.Length = 0;
1610 FullPath.MaximumLength = sizeof(StringBuffer);
1611 RtlInitUnicodeString(&NtSystemRoot, SharedUserData->NtSystemRoot);
1612 RtlAppendUnicodeStringToString(&FullPath, &NtSystemRoot);
1613 RtlAppendUnicodeToString(&FullPath, L"\\System32\\");
1614
1615 /* Open the Known DLLs directory */
1616 RtlInitUnicodeString(&KnownDllString, L"\\KnownDlls");
1617 InitializeObjectAttributes(&ObjectAttributes,
1618 &KnownDllString,
1619 OBJ_CASE_INSENSITIVE,
1620 NULL,
1621 NULL);
1622 Status = ZwOpenDirectoryObject(&LdrpKnownDllObjectDirectory,
1623 DIRECTORY_QUERY | DIRECTORY_TRAVERSE,
1624 &ObjectAttributes);
1625
1626 /* Check if it exists */
1627 if (!NT_SUCCESS(Status))
1628 {
1629 /* It doesn't, so assume System32 */
1630 LdrpKnownDllObjectDirectory = NULL;
1631 RtlInitUnicodeString(&LdrpKnownDllPath, StringBuffer);
1632 LdrpKnownDllPath.Length -= sizeof(WCHAR);
1633 }
1634 else
1635 {
1636 /* Open the Known DLLs Path */
1637 InitializeObjectAttributes(&ObjectAttributes,
1638 &KnownDllString,
1639 OBJ_CASE_INSENSITIVE,
1640 LdrpKnownDllObjectDirectory,
1641 NULL);
1642 Status = NtOpenSymbolicLinkObject(&SymLinkHandle,
1643 SYMBOLIC_LINK_QUERY,
1644 &ObjectAttributes);
1645 if (NT_SUCCESS(Status))
1646 {
1647 /* Query the path */
1648 LdrpKnownDllPath.Length = 0;
1649 LdrpKnownDllPath.MaximumLength = sizeof(LdrpKnownDllPathBuffer);
1650 LdrpKnownDllPath.Buffer = LdrpKnownDllPathBuffer;
1651 Status = ZwQuerySymbolicLinkObject(SymLinkHandle, &LdrpKnownDllPath, NULL);
1652 NtClose(SymLinkHandle);
1653 if (!NT_SUCCESS(Status))
1654 {
1655 DPRINT1("LDR: %s - failed call to ZwQuerySymbolicLinkObject with status %x\n", "", Status);
1656 return Status;
1657 }
1658 }
1659 }
1660
1661 /* If we have process parameters, get the default path and current path */
1662 if (ProcessParameters)
1663 {
1664 /* Check if we have a Dll Path */
1665 if (ProcessParameters->DllPath.Length)
1666 {
1667 /* Get the path */
1668 LdrpDefaultPath = *(PUNICODE_STRING)&ProcessParameters->DllPath;
1669 }
1670 else
1671 {
1672 /* We need a valid path */
1673 DPRINT1("No valid DllPath was given!\n");
1674 LdrpInitFailure(STATUS_INVALID_PARAMETER);
1675 }
1676
1677 /* Set the current directory */
1678 CurrentDirectory = ProcessParameters->CurrentDirectory.DosPath;
1679
1680 /* Check if it's empty or invalid */
1681 if ((!CurrentDirectory.Buffer) ||
1682 (CurrentDirectory.Buffer[0] == UNICODE_NULL) ||
1683 (!CurrentDirectory.Length))
1684 {
1685 /* Allocate space for the buffer */
1686 CurrentDirectory.Buffer = RtlAllocateHeap(Peb->ProcessHeap,
1687 0,
1688 3 * sizeof(WCHAR) +
1689 sizeof(UNICODE_NULL));
1690 if (!CurrentDirectory.Buffer)
1691 {
1692 DPRINT1("LDR: LdrpInitializeProcess - unable to allocate current working directory buffer\n");
1693 // FIXME: And what?
1694 }
1695
1696 /* Copy the drive of the system root */
1697 RtlMoveMemory(CurrentDirectory.Buffer,
1698 SharedUserData->NtSystemRoot,
1699 3 * sizeof(WCHAR));
1700 CurrentDirectory.Buffer[3] = UNICODE_NULL;
1701 CurrentDirectory.Length = 3 * sizeof(WCHAR);
1702 CurrentDirectory.MaximumLength = CurrentDirectory.Length + sizeof(WCHAR);
1703
1704 FreeCurDir = TRUE;
1705 DPRINT("Using dynamically allocd curdir\n");
1706 }
1707 else
1708 {
1709 /* Use the local buffer */
1710 DPRINT("Using local system root\n");
1711 }
1712 }
1713
1714 /* Setup Loader Data */
1715 Peb->Ldr = &PebLdr;
1716 InitializeListHead(&PebLdr.InLoadOrderModuleList);
1717 InitializeListHead(&PebLdr.InMemoryOrderModuleList);
1718 InitializeListHead(&PebLdr.InInitializationOrderModuleList);
1719 PebLdr.Length = sizeof(PEB_LDR_DATA);
1720 PebLdr.Initialized = TRUE;
1721
1722 /* Allocate a data entry for the Image */
1723 LdrpImageEntry = NtLdrEntry = LdrpAllocateDataTableEntry(Peb->ImageBaseAddress);
1724
1725 /* Set it up */
1726 NtLdrEntry->EntryPoint = LdrpFetchAddressOfEntryPoint(NtLdrEntry->DllBase);
1727 NtLdrEntry->LoadCount = -1;
1728 NtLdrEntry->EntryPointActivationContext = 0;
1729 NtLdrEntry->FullDllName = ImageFileName;
1730
1731 if (IsDotNetImage)
1732 NtLdrEntry->Flags = LDRP_COR_IMAGE;
1733 else
1734 NtLdrEntry->Flags = 0;
1735
1736 /* Check if the name is empty */
1737 if (!ImageFileName.Buffer[0])
1738 {
1739 /* Use the same Base name */
1740 NtLdrEntry->BaseDllName = NtLdrEntry->FullDllName;
1741 }
1742 else
1743 {
1744 /* Find the last slash */
1745 Current = ImageFileName.Buffer;
1746 while (*Current)
1747 {
1748 if (*Current++ == '\\')
1749 {
1750 /* Set this path */
1751 NtDllName = Current;
1752 }
1753 }
1754
1755 /* Did we find anything? */
1756 if (!NtDllName)
1757 {
1758 /* Use the same Base name */
1759 NtLdrEntry->BaseDllName = NtLdrEntry->FullDllName;
1760 }
1761 else
1762 {
1763 /* Setup the name */
1764 NtLdrEntry->BaseDllName.Length = (USHORT)((ULONG_PTR)ImageFileName.Buffer + ImageFileName.Length - (ULONG_PTR)NtDllName);
1765 NtLdrEntry->BaseDllName.MaximumLength = NtLdrEntry->BaseDllName.Length + sizeof(WCHAR);
1766 NtLdrEntry->BaseDllName.Buffer = (PWSTR)((ULONG_PTR)ImageFileName.Buffer +
1767 (ImageFileName.Length - NtLdrEntry->BaseDllName.Length));
1768 }
1769 }
1770
1771 /* Processing done, insert it */
1772 LdrpInsertMemoryTableEntry(NtLdrEntry);
1773 NtLdrEntry->Flags |= LDRP_ENTRY_PROCESSED;
1774
1775 /* Now add an entry for NTDLL */
1776 NtLdrEntry = LdrpAllocateDataTableEntry(SystemArgument1);
1777 NtLdrEntry->Flags = LDRP_IMAGE_DLL;
1778 NtLdrEntry->EntryPoint = LdrpFetchAddressOfEntryPoint(NtLdrEntry->DllBase);
1779 NtLdrEntry->LoadCount = -1;
1780 NtLdrEntry->EntryPointActivationContext = 0;
1781
1782 NtLdrEntry->FullDllName.Length = FullPath.Length;
1783 NtLdrEntry->FullDllName.MaximumLength = FullPath.MaximumLength;
1784 NtLdrEntry->FullDllName.Buffer = StringBuffer;
1785 RtlAppendUnicodeStringToString(&NtLdrEntry->FullDllName, &NtDllString);
1786
1787 NtLdrEntry->BaseDllName.Length = NtDllString.Length;
1788 NtLdrEntry->BaseDllName.MaximumLength = NtDllString.MaximumLength;
1789 NtLdrEntry->BaseDllName.Buffer = NtDllString.Buffer;
1790
1791 /* Processing done, insert it */
1792 LdrpNtDllDataTableEntry = NtLdrEntry;
1793 LdrpInsertMemoryTableEntry(NtLdrEntry);
1794
1795 /* Let the world know */
1796 if (ShowSnaps)
1797 {
1798 DPRINT1("LDR: NEW PROCESS\n");
1799 DPRINT1(" Image Path: %wZ (%wZ)\n", &LdrpImageEntry->FullDllName, &LdrpImageEntry->BaseDllName);
1800 DPRINT1(" Current Directory: %wZ\n", &CurrentDirectory);
1801 DPRINT1(" Search Path: %wZ\n", &LdrpDefaultPath);
1802 }
1803
1804 /* Link the Init Order List */
1805 InsertHeadList(&Peb->Ldr->InInitializationOrderModuleList,
1806 &LdrpNtDllDataTableEntry->InInitializationOrderModuleList);
1807
1808 /* Set the current directory */
1809 Status = RtlSetCurrentDirectory_U(&CurrentDirectory);
1810 if (!NT_SUCCESS(Status))
1811 {
1812 /* We failed, check if we should free it */
1813 if (FreeCurDir) RtlFreeUnicodeString(&CurrentDirectory);
1814
1815 /* Set it to the NT Root */
1816 CurrentDirectory = NtSystemRoot;
1817 RtlSetCurrentDirectory_U(&CurrentDirectory);
1818 }
1819 else
1820 {
1821 /* We're done with it, free it */
1822 if (FreeCurDir) RtlFreeUnicodeString(&CurrentDirectory);
1823 }
1824
1825 /* Check if we should look for a .local file */
1826 if (ProcessParameters->Flags & RTL_USER_PROCESS_PARAMETERS_LOCAL_DLL_PATH)
1827 {
1828 /* FIXME */
1829 DPRINT1("We don't support .local overrides yet\n");
1830 }
1831
1832 /* Check if the Application Verifier was enabled */
1833 if (Peb->NtGlobalFlag & FLG_POOL_ENABLE_TAIL_CHECK)
1834 {
1835 /* FIXME */
1836 DPRINT1("We don't support Application Verifier yet\n");
1837 }
1838
1839 if (IsDotNetImage)
1840 {
1841 /* FIXME */
1842 DPRINT1("We don't support .NET applications yet\n");
1843 }
1844
1845 /* FIXME: Load support for Terminal Services */
1846 if (NtHeader->OptionalHeader.Subsystem == IMAGE_SUBSYSTEM_WINDOWS_GUI)
1847 {
1848 /* Load kernel32 and call BasePostImportInit... */
1849 DPRINT1("Unimplemented codepath!\n");
1850 }
1851
1852 /* Walk the IAT and load all the DLLs */
1853 LdrpWalkImportDescriptor(LdrpDefaultPath.Buffer, LdrpImageEntry);
1854
1855 /* Check if relocation is needed */
1856 if (Peb->ImageBaseAddress != (PVOID)NtHeader->OptionalHeader.ImageBase)
1857 {
1858 DPRINT("LDR: Performing relocations\n");
1859 Status = LdrPerformRelocations(NtHeader, Peb->ImageBaseAddress);
1860 if (!NT_SUCCESS(Status))
1861 {
1862 DPRINT1("LdrPerformRelocations() failed\n");
1863 return STATUS_INVALID_IMAGE_FORMAT;
1864 }
1865 }
1866
1867 /* Lock the DLLs */
1868 ListHead = &Peb->Ldr->InLoadOrderModuleList;
1869 NextEntry = ListHead->Flink;
1870 while (ListHead != NextEntry)
1871 {
1872 NtLdrEntry = CONTAINING_RECORD(NextEntry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
1873 NtLdrEntry->LoadCount = -1;
1874 NextEntry = NextEntry->Flink;
1875 }
1876
1877 /* Phase 0 is done */
1878 LdrpLdrDatabaseIsSetup = TRUE;
1879
1880 /* Initialize TLS */
1881 Status = LdrpInitializeTls();
1882 if (!NT_SUCCESS(Status))
1883 {
1884 DPRINT1("LDR: LdrpProcessInitialization failed to initialize TLS slots; status %x\n",
1885 Status);
1886 return Status;
1887 }
1888
1889 /* FIXME Mark the DLL Ranges for Stack Traces later */
1890
1891 /* Notify the debugger now */
1892 if (Peb->BeingDebugged)
1893 {
1894 /* Break */
1895 DbgBreakPoint();
1896
1897 /* Update show snaps again */
1898 ShowSnaps = Peb->NtGlobalFlag & FLG_SHOW_LDR_SNAPS;
1899 }
1900
1901 /* Validate the Image for MP Usage */
1902 if (LdrpNumberOfProcessors > 1) LdrpValidateImageForMp(LdrpImageEntry);
1903
1904 /* Check NX Options */
1905 if (SharedUserData->NXSupportPolicy == 1)
1906 {
1907 ExecuteOptions = 0xD;
1908 }
1909 else if (!SharedUserData->NXSupportPolicy)
1910 {
1911 ExecuteOptions = 0xA;
1912 }
1913
1914 /* Let Mm know */
1915 ZwSetInformationProcess(NtCurrentProcess(),
1916 ProcessExecuteFlags,
1917 &ExecuteOptions,
1918 sizeof(ULONG));
1919
1920 /* Check if we had Shim Data */
1921 if (OldShimData)
1922 {
1923 /* Load the Shim Engine */
1924 Peb->AppCompatInfo = NULL;
1925 //LdrpLoadShimEngine(OldShimData, ImagePathName, OldShimData);
1926 DPRINT1("We do not support shims yet\n");
1927 }
1928 else
1929 {
1930 /* Check for Application Compatibility Goo */
1931 //LdrQueryApplicationCompatibilityGoo(hKey);
1932 DPRINT1("Querying app compat hacks is missing!\n");
1933 }
1934
1935 /*
1936 * FIXME: Check for special images, SecuROM, SafeDisc and other NX-
1937 * incompatible images.
1938 */
1939
1940 /* Now call the Init Routines */
1941 Status = LdrpRunInitializeRoutines(Context);
1942 if (!NT_SUCCESS(Status))
1943 {
1944 DPRINT1("LDR: LdrpProcessInitialization failed running initialization routines; status %x\n",
1945 Status);
1946 return Status;
1947 }
1948
1949 /* FIXME: Unload the Shim Engine if it was loaded */
1950
1951 /* Check if we have a user-defined Post Process Routine */
1952 if (NT_SUCCESS(Status) && Peb->PostProcessInitRoutine)
1953 {
1954 DPRINT1("CP\n");
1955 /* Call it */
1956 Peb->PostProcessInitRoutine();
1957 }
1958
1959 ///* Close the key if we have one opened */
1960 //if (hKey) NtClose(hKey);
1961
1962 /* Return status */
1963 return Status;
1964 }
1965
1966 VOID
1967 NTAPI
1968 LdrpInitFailure(NTSTATUS Status)
1969 {
1970 ULONG Response;
1971
1972 /* Print a debug message */
1973 DPRINT1("LDR: Process initialization failure; NTSTATUS = %08lx\n", Status);
1974
1975 /* Raise a hard error */
1976 if (!LdrpFatalHardErrorCount)
1977 {
1978 ZwRaiseHardError(STATUS_APP_INIT_FAILURE, 1, 0, (PULONG_PTR)&Status, OptionOk, &Response);
1979 }
1980 }
1981
1982 VOID
1983 NTAPI
1984 LdrpInit(PCONTEXT Context,
1985 PVOID SystemArgument1,
1986 PVOID SystemArgument2)
1987 {
1988 LARGE_INTEGER Timeout;
1989 PTEB Teb = NtCurrentTeb();
1990 NTSTATUS Status, LoaderStatus = STATUS_SUCCESS;
1991 MEMORY_BASIC_INFORMATION MemoryBasicInfo;
1992 PPEB Peb = NtCurrentPeb();
1993
1994 DPRINT("LdrpInit()\n");
1995
1996 /* Check if we have a deallocation stack */
1997 if (!Teb->DeallocationStack)
1998 {
1999 /* We don't, set one */
2000 Status = NtQueryVirtualMemory(NtCurrentProcess(),
2001 Teb->NtTib.StackLimit,
2002 MemoryBasicInformation,
2003 &MemoryBasicInfo,
2004 sizeof(MEMORY_BASIC_INFORMATION),
2005 NULL);
2006 if (!NT_SUCCESS(Status))
2007 {
2008 /* Fail */
2009 LdrpInitFailure(Status);
2010 RtlRaiseStatus(Status);
2011 return;
2012 }
2013
2014 /* Set the stack */
2015 Teb->DeallocationStack = MemoryBasicInfo.AllocationBase;
2016 }
2017
2018 /* Now check if the process is already being initialized */
2019 while (_InterlockedCompareExchange(&LdrpProcessInitialized,
2020 1,
2021 0) == 1)
2022 {
2023 /* Set the timeout to 30 seconds */
2024 Timeout.QuadPart = Int32x32To64(30, -10000);
2025
2026 /* Make sure the status hasn't changed */
2027 while (!LdrpProcessInitialized)
2028 {
2029 /* Do the wait */
2030 ZwDelayExecution(FALSE, &Timeout);
2031 }
2032 }
2033
2034 /* Check if we have already setup LDR data */
2035 if (!Peb->Ldr)
2036 {
2037 /* Setup the Loader Lock */
2038 Peb->LoaderLock = &LdrpLoaderLock;
2039
2040 /* Let other code know we're initializing */
2041 LdrpInLdrInit = TRUE;
2042
2043 /* Protect with SEH */
2044 _SEH2_TRY
2045 {
2046 /* Initialize the Process */
2047 LoaderStatus = LdrpInitializeProcess(Context,
2048 SystemArgument1);
2049
2050 /* Check for success and if MinimumStackCommit was requested */
2051 if (NT_SUCCESS(LoaderStatus) && Peb->MinimumStackCommit)
2052 {
2053 /* Enforce the limit */
2054 //LdrpTouchThreadStack(Peb->MinimumStackCommit);
2055 UNIMPLEMENTED;
2056 }
2057 }
2058 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2059 {
2060 /* Fail with the SEH error */
2061 LoaderStatus = _SEH2_GetExceptionCode();
2062 }
2063 _SEH2_END;
2064
2065 /* We're not initializing anymore */
2066 LdrpInLdrInit = FALSE;
2067
2068 /* Check if init worked */
2069 if (NT_SUCCESS(LoaderStatus))
2070 {
2071 /* Set the process as Initialized */
2072 _InterlockedIncrement(&LdrpProcessInitialized);
2073 }
2074 }
2075 else
2076 {
2077 /* Loader data is there... is this a fork() ? */
2078 if(Peb->InheritedAddressSpace)
2079 {
2080 /* Handle the fork() */
2081 //LoaderStatus = LdrpForkProcess();
2082 LoaderStatus = STATUS_NOT_IMPLEMENTED;
2083 UNIMPLEMENTED;
2084 }
2085 else
2086 {
2087 /* This is a new thread initializing */
2088 LdrpInitializeThread(Context);
2089 }
2090 }
2091
2092 /* All done, test alert the thread */
2093 NtTestAlert();
2094
2095 /* Return */
2096 if (!NT_SUCCESS(LoaderStatus))
2097 {
2098 /* Fail */
2099 LdrpInitFailure(LoaderStatus);
2100 RtlRaiseStatus(LoaderStatus);
2101 }
2102 }
2103
2104 /* EOF */