Updated CREDITS
[reactos.git] / reactos / lib / ntdll / ldr / startup.c
1 /* $Id: startup.c,v 1.29 2000/08/12 19:33:18 dwelch Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/ldr/startup.c
6 * PURPOSE: Process startup for PE executables
7 * PROGRAMMERS: Jean Michault
8 * Rex Jolliff (rex@lvcablemodem.com)
9 */
10
11 /* INCLUDES *****************************************************************/
12
13 #include <reactos/config.h>
14 #include <ddk/ntddk.h>
15 #include <windows.h>
16 #include <ntdll/ldr.h>
17 #include <ntdll/rtl.h>
18 #include <csrss/csrss.h>
19 #include <ntdll/csr.h>
20
21 #define NDEBUG
22 #include <ntdll/ntdll.h>
23
24
25 VOID RtlInitializeHeapManager (VOID);
26
27 /* GLOBALS *******************************************************************/
28
29 DLL LdrDllListHead;
30 extern unsigned int _image_base__;
31
32 static CRITICAL_SECTION PebLock;
33
34 ULONG NtGlobalFlag = 0;
35
36
37 /* FUNCTIONS *****************************************************************/
38
39 VOID STDCALL
40 LdrInitializeThunk (ULONG Unknown1,
41 ULONG Unknown2,
42 ULONG Unknown3,
43 ULONG Unknown4)
44 {
45 PEPFUNC EntryPoint;
46 PIMAGE_DOS_HEADER PEDosHeader;
47 NTSTATUS Status;
48 PIMAGE_NT_HEADERS NTHeaders;
49 PVOID ImageBase;
50 PPEB Peb;
51
52 DPRINT("LdrInitializeThunk()\n");
53
54 LdrDllListHead.BaseAddress = (PVOID)&_image_base__;
55 LdrDllListHead.Prev = &LdrDllListHead;
56 LdrDllListHead.Next = &LdrDllListHead;
57 LdrDllListHead.SectionHandle = NULL;
58 PEDosHeader = (PIMAGE_DOS_HEADER)LdrDllListHead.BaseAddress;
59 LdrDllListHead.Headers = (PIMAGE_NT_HEADERS)(LdrDllListHead.BaseAddress +
60 PEDosHeader->e_lfanew);
61
62 Peb = (PPEB)(PEB_BASE);
63 DPRINT("Peb %x\n", Peb);
64 ImageBase = Peb->ImageBaseAddress;
65 DPRINT("ImageBase %x\n", ImageBase);
66 if (ImageBase <= (PVOID)0x1000)
67 {
68 DPRINT("ImageBase is null\n");
69 ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL);
70 }
71
72 NtGlobalFlag = Peb->NtGlobalFlag;
73
74 /* If MZ header exists */
75 PEDosHeader = (PIMAGE_DOS_HEADER) ImageBase;
76 DPRINT("PEDosHeader %x\n", PEDosHeader);
77 if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC ||
78 PEDosHeader->e_lfanew == 0L ||
79 *(PULONG)((PUCHAR)ImageBase + PEDosHeader->e_lfanew) != IMAGE_PE_MAGIC)
80 {
81 DbgPrint("Image has bad header\n");
82 ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL);
83 }
84
85 /* normalize process parameters */
86 RtlNormalizeProcessParams (Peb->ProcessParameters);
87
88 #if 0
89 /* initialize NLS data */
90 RtlInitNlsTables (Peb->AnsiCodePageData,
91 Peb->OemCodePageData,
92 Peb->UnicodeCaseTableData,
93 &TranslationTable);
94 RtlResetRtlTranslations (&TranslationTable);
95 #endif
96
97 NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + PEDosHeader->e_lfanew);
98
99 /* create process heap */
100 RtlInitializeHeapManager();
101 Peb->ProcessHeap = RtlCreateHeap(0,
102 (PVOID)HEAP_BASE,
103 NTHeaders->OptionalHeader.SizeOfHeapCommit,
104 NTHeaders->OptionalHeader.SizeOfHeapReserve,
105 NULL,
106 NULL);
107 if (Peb->ProcessHeap == 0)
108 {
109 DbgPrint("Failed to create process heap\n");
110 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
111 }
112
113 /* initalize peb lock support */
114 RtlInitializeCriticalSection (&PebLock);
115 Peb->FastPebLock = &PebLock;
116 Peb->FastPebLockRoutine = (PPEBLOCKROUTINE)RtlEnterCriticalSection;
117 Peb->FastPebUnlockRoutine = (PPEBLOCKROUTINE)RtlLeaveCriticalSection;
118
119 EntryPoint = LdrPEStartup((PVOID)ImageBase, NULL);
120 if (EntryPoint == NULL)
121 {
122 DbgPrint("Failed to initialize image\n");
123 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
124 }
125
126 DbgPrint("Transferring control to image at %x\n",EntryPoint);
127 Status = EntryPoint(Peb);
128 ZwTerminateProcess(NtCurrentProcess(),Status);
129 }
130
131 /* EOF */