[AVIFIL32]
[reactos.git] / reactos / dll / ntdll / ldr / ldrinit.c
index 240eea0..8aa9e74 100644 (file)
@@ -10,7 +10,7 @@
 /* INCLUDES *****************************************************************/
 
 #include <ntdll.h>
-#include <win32k/callback.h>
+#include <callback.h>
 
 #define NDEBUG
 #include <debug.h>
@@ -48,7 +48,7 @@ LIST_ENTRY LdrpTlsList;
 ULONG LdrpNumberOfTlsEntries;
 ULONG LdrpNumberOfProcessors;
 PVOID NtDllBase;
-LARGE_INTEGER RtlpTimeout;
+extern LARGE_INTEGER RtlpTimeout;
 BOOLEAN RtlpTimeoutDisable;
 LIST_ENTRY LdrpHashTable[LDR_HASH_TABLE_ENTRIES];
 LIST_ENTRY LdrpDllNotificationList;
@@ -80,7 +80,7 @@ ULONG LdrpActiveUnloadCount;
 
 VOID RtlpInitializeVectoredExceptionHandling(VOID);
 VOID NTAPI RtlpInitDeferedCriticalSection(VOID);
-VOID RtlInitializeHeapManager(VOID);
+VOID NTAPI RtlInitializeHeapManager(VOID);
 extern BOOLEAN RtlpPageHeapEnabled;
 
 ULONG RtlpDisableHeapLookaside; // TODO: Move to heap.c
@@ -207,20 +207,22 @@ LdrQueryImageFileKeyOption(IN HANDLE KeyHandle,
         KeyValueInformation = RtlAllocateHeap(RtlGetProcessHeap(),
                                               0,
                                               KeyInfoSize);
-        if (KeyValueInformation == NULL)
+        if (KeyValueInformation != NULL)
+        {
+            /* Try again */
+            Status = ZwQueryValueKey(KeyHandle,
+                                     &ValueNameString,
+                                     KeyValuePartialInformation,
+                                     KeyValueInformation,
+                                     KeyInfoSize,
+                                     &ResultSize);
+            FreeHeap = TRUE;
+        }
+        else
         {
             /* Give up this time */
             Status = STATUS_NO_MEMORY;
         }
-
-        /* Try again */
-        Status = ZwQueryValueKey(KeyHandle,
-                                 &ValueNameString,
-                                 KeyValuePartialInformation,
-                                 KeyValueInformation,
-                                 KeyInfoSize,
-                                 &ResultSize);
-        FreeHeap = TRUE;
     }
 
     /* Check for success */
@@ -499,14 +501,14 @@ LdrpInitializeThread(IN PCONTEXT Context)
     NTSTATUS Status;
     PVOID EntryPoint;
 
-    DPRINT("LdrpInitializeThread() called for %wZ (%lx/%lx)\n",
+    DPRINT("LdrpInitializeThread() called for %wZ (%p/%p)\n",
             &LdrpImageEntry->BaseDllName,
             NtCurrentTeb()->RealClientId.UniqueProcess,
             NtCurrentTeb()->RealClientId.UniqueThread);
 
     /* Allocate an Activation Context Stack */
     DPRINT("ActivationContextStack %p\n", NtCurrentTeb()->ActivationContextStackPointer);
-    Status = RtlAllocateActivationContextStack((PVOID*)&NtCurrentTeb()->ActivationContextStackPointer);
+    Status = RtlAllocateActivationContextStack(&NtCurrentTeb()->ActivationContextStackPointer);
     if (!NT_SUCCESS(Status))
     {
         DPRINT1("Warning: Unable to allocate ActivationContextStack\n");
@@ -564,7 +566,7 @@ LdrpInitializeThread(IN PCONTEXT Context)
                     if (!LdrpShutdownInProgress)
                     {
                         /* Call the Entrypoint */
-                        DPRINT("%wZ - Calling entry point at %p for thread attaching, %lx/%lx\n",
+                        DPRINT("%wZ - Calling entry point at %p for thread attaching, %p/%p\n",
                                 &LdrEntry->BaseDllName, LdrEntry->EntryPoint,
                                 NtCurrentTeb()->RealClientId.UniqueProcess,
                                 NtCurrentTeb()->RealClientId.UniqueThread);
@@ -624,7 +626,7 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
     PTEB OldTldTeb;
     BOOLEAN DllStatus;
 
-    DPRINT("LdrpRunInitializeRoutines() called for %wZ (%lx/%lx)\n",
+    DPRINT("LdrpRunInitializeRoutines() called for %wZ (%p/%p)\n",
         &LdrpImageEntry->BaseDllName,
         NtCurrentTeb()->RealClientId.UniqueProcess,
         NtCurrentTeb()->RealClientId.UniqueThread);
@@ -641,7 +643,7 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
             /* Allocate space for all the entries */
             LdrRootEntry = RtlAllocateHeap(RtlGetProcessHeap(),
                                            0,
-                                           Count * sizeof(LdrRootEntry));
+                                           Count * sizeof(*LdrRootEntry));
             if (!LdrRootEntry) return STATUS_NO_MEMORY;
         }
         else
@@ -659,7 +661,7 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
     /* Show debug message */
     if (ShowSnaps)
     {
-        DPRINT1("[%x,%x] LDR: Real INIT LIST for Process %wZ\n",
+        DPRINT1("[%p,%p] LDR: Real INIT LIST for Process %wZ\n",
                 NtCurrentTeb()->RealClientId.UniqueThread,
                 NtCurrentTeb()->RealClientId.UniqueProcess,
                 &Peb->ProcessParameters->ImagePathName);
@@ -687,12 +689,13 @@ LdrpRunInitializeRoutines(IN PCONTEXT Context OPTIONAL)
                 if (LdrEntry->EntryPoint)
                 {
                     /* Write in array */
+                    ASSERT(i < Count);
                     LdrRootEntry[i] = LdrEntry;
 
                     /* Display debug message */
                     if (ShowSnaps)
                     {
-                        DPRINT1("[%x,%x] LDR: %wZ init routine %p\n",
+                        DPRINT1("[%p,%p] LDR: %wZ init routine %p\n",
                                 NtCurrentTeb()->RealClientId.UniqueThread,
                                 NtCurrentTeb()->RealClientId.UniqueProcess,
                                 &LdrEntry->FullDllName,
@@ -960,7 +963,7 @@ LdrShutdownProcess(VOID)
                 }
 
                 /* Call the Entrypoint */
-                DPRINT("%wZ - Calling entry point at %x for thread detaching\n",
+                DPRINT("%wZ - Calling entry point at %p for thread detaching\n",
                         &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
                 LdrpCallInitRoutine(EntryPoint,
                                  LdrEntry->DllBase,
@@ -1075,7 +1078,7 @@ LdrShutdownThread(VOID)
                     if (!LdrpShutdownInProgress)
                     {
                         /* Call the Entrypoint */
-                        DPRINT("%wZ - Calling entry point at %x for thread detaching\n",
+                        DPRINT("%wZ - Calling entry point at %p for thread detaching\n",
                                 &LdrEntry->BaseDllName, LdrEntry->EntryPoint);
                         LdrpCallInitRoutine(EntryPoint,
                                          LdrEntry->DllBase,
@@ -1250,7 +1253,7 @@ LdrpAllocateTls(VOID)
         /* Show debug message */
         if (ShowSnaps)
         {
-            DPRINT1("LDR: TlsVector %x Index %d = %x copied from %x to %x\n",
+            DPRINT1("LDR: TlsVector %p Index %lu = %p copied from %x to %p\n",
                     TlsVector,
                     TlsData->TlsDirectory.Characteristics,
                     &TlsVector[TlsData->TlsDirectory.Characteristics],
@@ -1476,7 +1479,7 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     ULONG HeapFlags;
     PIMAGE_NT_HEADERS NtHeader;
     LPWSTR NtDllName = NULL;
-    NTSTATUS Status;
+    NTSTATUS Status, ImportStatus;
     NLSTABLEINFO NlsTable;
     PIMAGE_LOAD_CONFIG_DIRECTORY LoadConfig;
     PTEB Teb = NtCurrentTeb();
@@ -1546,7 +1549,6 @@ LdrpInitializeProcess(IN PCONTEXT Context,
 
     /* Normalize the parameters */
     ProcessParameters = RtlNormalizeProcessParams(Peb->ProcessParameters);
-    ProcessParameters = Peb->ProcessParameters;
     if (ProcessParameters)
     {
         /* Save the Image and Command Line Names */
@@ -1603,7 +1605,7 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     /* Start verbose debugging messages right now if they were requested */
     if (ShowSnaps)
     {
-        DPRINT1("LDR: PID: 0x%x started - '%wZ'\n",
+        DPRINT1("LDR: PID: 0x%p started - '%wZ'\n",
                 Teb->ClientId.UniqueProcess,
                 &CommandLine);
     }
@@ -1694,20 +1696,8 @@ LdrpInitializeProcess(IN PCONTEXT Context,
         return STATUS_NO_MEMORY;
     }
 
-    // FIXME: Is it located properly?
-    /* Initialize table of callbacks for the kernel. */
-    Peb->KernelCallbackTable = RtlAllocateHeap(RtlGetProcessHeap(),
-                                               0,
-                                               sizeof(PVOID) *
-                                                (USER32_CALLBACK_MAXIMUM + 1));
-    if (!Peb->KernelCallbackTable)
-    {
-        DPRINT1("Failed to create callback table\n");
-        ZwTerminateProcess(NtCurrentProcess(), STATUS_INSUFFICIENT_RESOURCES);
-    }
-
     /* Allocate an Activation Context Stack */
-    Status = RtlAllocateActivationContextStack((PVOID *)&Teb->ActivationContextStackPointer);
+    Status = RtlAllocateActivationContextStack(&Teb->ActivationContextStackPointer);
     if (!NT_SUCCESS(Status)) return Status;
 
     // FIXME: Loader private heap is missing
@@ -1987,13 +1977,13 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     }
 
     /* Walk the IAT and load all the DLLs */
-    LdrpWalkImportDescriptor(LdrpDefaultPath.Buffer, LdrpImageEntry);
+    ImportStatus = LdrpWalkImportDescriptor(LdrpDefaultPath.Buffer, LdrpImageEntry);
 
     /* Check if relocation is needed */
     if (Peb->ImageBaseAddress != (PVOID)NtHeader->OptionalHeader.ImageBase)
     {
         DPRINT1("LDR: Performing EXE relocation\n");
-        
+
         /* Change the protection to prepare for relocation */
         ViewBase = Peb->ImageBaseAddress;
         Status = LdrpSetProtection(ViewBase, FALSE);
@@ -2011,7 +2001,7 @@ LdrpInitializeProcess(IN PCONTEXT Context,
             DPRINT1("LdrRelocateImageWithBias() failed\n");
             return Status;
         }
-        
+
         /* Check if a start context was provided */
         if (Context)
         {
@@ -2019,7 +2009,7 @@ LdrpInitializeProcess(IN PCONTEXT Context,
             UNIMPLEMENTED; // We should support this
             return STATUS_INVALID_IMAGE_FORMAT;
         }
-        
+
         /* Restore the protection */
         Status = LdrpSetProtection(ViewBase, TRUE);
         if (!NT_SUCCESS(Status)) return Status;
@@ -2038,6 +2028,9 @@ LdrpInitializeProcess(IN PCONTEXT Context,
     /* Phase 0 is done */
     LdrpLdrDatabaseIsSetup = TRUE;
 
+    /* Check whether all static imports were properly loaded and return here */
+    if (!NT_SUCCESS(ImportStatus)) return ImportStatus;
+
     /* Initialize TLS */
     Status = LdrpInitializeTls();
     if (!NT_SUCCESS(Status))
@@ -2153,7 +2146,7 @@ LdrpInit(PCONTEXT Context,
     MEMORY_BASIC_INFORMATION MemoryBasicInfo;
     PPEB Peb = NtCurrentPeb();
 
-    DPRINT("LdrpInit() %lx/%lx\n",
+    DPRINT("LdrpInit() %p/%p\n",
         NtCurrentTeb()->RealClientId.UniqueProcess,
         NtCurrentTeb()->RealClientId.UniqueThread);