1 /* $Id: startup.c,v 1.38 2002/07/13 12:44:06 chorns Exp $
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)
11 /* INCLUDES *****************************************************************/
13 #include <reactos/config.h>
14 #include <ddk/ntddk.h>
16 #include <ntdll/ldr.h>
17 #include <ntdll/rtl.h>
18 #include <csrss/csrss.h>
19 #include <ntdll/csr.h>
20 #include <user32/callback.h>
23 #include <ntdll/ntdll.h>
26 VOID
RtlInitializeHeapManager (VOID
);
28 /* GLOBALS *******************************************************************/
31 extern unsigned int _image_base__
;
33 static CRITICAL_SECTION PebLock
;
34 static CRITICAL_SECTION LoaderLock
;
35 static RTL_BITMAP TlsBitMap
;
37 ULONG NtGlobalFlag
= 0;
40 /* FUNCTIONS *****************************************************************/
43 LdrInitializeThunk (ULONG Unknown1
,
48 PIMAGE_NT_HEADERS NTHeaders
;
50 PIMAGE_DOS_HEADER PEDosHeader
;
54 PLDR_MODULE NtModule
; // ntdll
55 PLDR_MODULE ExeModule
; // executable
56 WCHAR FullNtDllPath
[MAX_PATH
];
58 DPRINT("LdrInitializeThunk()\n");
60 Peb
= (PPEB
)(PEB_BASE
);
61 DPRINT("Peb %x\n", Peb
);
62 ImageBase
= Peb
->ImageBaseAddress
;
63 DPRINT("ImageBase %x\n", ImageBase
);
64 if (ImageBase
<= (PVOID
)0x1000)
66 DPRINT("ImageBase is null\n");
67 ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL
);
70 NtGlobalFlag
= Peb
->NtGlobalFlag
;
72 /* If MZ header exists */
73 PEDosHeader
= (PIMAGE_DOS_HEADER
) ImageBase
;
74 DPRINT("PEDosHeader %x\n", PEDosHeader
);
75 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
||
76 PEDosHeader
->e_lfanew
== 0L ||
77 *(PULONG
)((PUCHAR
)ImageBase
+ PEDosHeader
->e_lfanew
) != IMAGE_PE_MAGIC
)
79 DbgPrint("Image has bad header\n");
80 ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL
);
83 /* normalize process parameters */
84 RtlNormalizeProcessParams (Peb
->ProcessParameters
);
87 /* initialize NLS data */
88 RtlInitNlsTables (Peb
->AnsiCodePageData
,
90 Peb
->UnicodeCaseTableData
,
92 RtlResetRtlTranslations (&TranslationTable
);
95 NTHeaders
= (PIMAGE_NT_HEADERS
)(ImageBase
+ PEDosHeader
->e_lfanew
);
97 /* create process heap */
98 RtlInitializeHeapManager();
99 Peb
->ProcessHeap
= RtlCreateHeap(0,
101 NTHeaders
->OptionalHeader
.SizeOfHeapCommit
,
102 NTHeaders
->OptionalHeader
.SizeOfHeapReserve
,
105 if (Peb
->ProcessHeap
== 0)
107 DbgPrint("Failed to create process heap\n");
108 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
111 /* initalize peb lock support */
112 RtlInitializeCriticalSection (&PebLock
);
113 Peb
->FastPebLock
= &PebLock
;
114 Peb
->FastPebLockRoutine
= (PPEBLOCKROUTINE
)RtlEnterCriticalSection
;
115 Peb
->FastPebUnlockRoutine
= (PPEBLOCKROUTINE
)RtlLeaveCriticalSection
;
117 /* initialize tls bitmap */
118 RtlInitializeBitMap (&TlsBitMap
,
120 TLS_MINIMUM_AVAILABLE
);
121 Peb
->TlsBitmap
= &TlsBitMap
;
122 Peb
->TlsExpansionCounter
= TLS_MINIMUM_AVAILABLE
;
124 /* Initialize table of callbacks for the kernel. */
125 Peb
->KernelCallbackTable
=
126 RtlAllocateHeap(RtlGetProcessHeap(),
128 sizeof(PVOID
) * USER32_CALLBACK_MAXIMUM
);
130 /* initalize loader lock */
131 RtlInitializeCriticalSection (&LoaderLock
);
132 Peb
->LoaderLock
= &LoaderLock
;
134 /* create loader information */
135 Peb
->Ldr
= (PPEB_LDR_DATA
)RtlAllocateHeap (Peb
->ProcessHeap
,
137 sizeof(PEB_LDR_DATA
));
138 if (Peb
->Ldr
== NULL
)
140 DbgPrint("Failed to create loader data\n");
141 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
143 Peb
->Ldr
->Length
= sizeof(PEB_LDR_DATA
);
144 Peb
->Ldr
->Initialized
= FALSE
;
145 Peb
->Ldr
->SsHandle
= NULL
;
146 InitializeListHead(&Peb
->Ldr
->InLoadOrderModuleList
);
147 InitializeListHead(&Peb
->Ldr
->InMemoryOrderModuleList
);
148 InitializeListHead(&Peb
->Ldr
->InInitializationOrderModuleList
);
150 /* build full ntdll path */
151 wcscpy (FullNtDllPath
, SharedUserData
->NtSystemRoot
);
152 wcscat (FullNtDllPath
, L
"\\system32\\ntdll.dll");
154 /* add entry for ntdll */
155 NtModule
= (PLDR_MODULE
)RtlAllocateHeap (Peb
->ProcessHeap
,
158 if (NtModule
== NULL
)
160 DbgPrint("Failed to create loader module entry (NTDLL)\n");
161 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
163 memset(NtModule
, 0, sizeof(LDR_MODULE
));
165 NtModule
->BaseAddress
= (PVOID
)&_image_base__
;
166 NtModule
->EntryPoint
= 0; /* no entry point */
167 RtlCreateUnicodeString (&NtModule
->FullDllName
,
169 RtlCreateUnicodeString (&NtModule
->BaseDllName
,
172 NtModule
->LoadCount
= -1; /* don't unload */
173 NtModule
->TlsIndex
= 0;
174 NtModule
->SectionHandle
= NULL
;
175 NtModule
->CheckSum
= 0;
177 NTHeaders
= RtlImageNtHeader (NtModule
->BaseAddress
);
178 NtModule
->SizeOfImage
= NTHeaders
->OptionalHeader
.SizeOfImage
;
179 NtModule
->TimeDateStamp
= NTHeaders
->FileHeader
.TimeDateStamp
;
181 InsertTailList(&Peb
->Ldr
->InLoadOrderModuleList
,
182 &NtModule
->InLoadOrderModuleList
);
183 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
184 &NtModule
->InInitializationOrderModuleList
);
188 LdrpLoadUserModuleSymbols(NtModule
);
192 /* add entry for executable (becomes first list entry) */
193 ExeModule
= (PLDR_MODULE
)RtlAllocateHeap (Peb
->ProcessHeap
,
196 if (ExeModule
== NULL
)
198 DbgPrint("Failed to create loader module infomation\n");
199 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
201 ExeModule
->BaseAddress
= Peb
->ImageBaseAddress
;
203 if ((Peb
->ProcessParameters
!= NULL
) &&
204 (Peb
->ProcessParameters
->ImagePathName
.Length
!= 0))
206 RtlCreateUnicodeString (&ExeModule
->FullDllName
,
207 Peb
->ProcessParameters
->ImagePathName
.Buffer
);
208 RtlCreateUnicodeString (&ExeModule
->BaseDllName
,
209 wcsrchr(ExeModule
->FullDllName
.Buffer
, L
'\\') + 1);
213 /* FIXME(???): smss.exe doesn't have a process parameter block */
214 RtlCreateUnicodeString (&ExeModule
->BaseDllName
,
216 RtlCreateUnicodeString (&ExeModule
->FullDllName
,
217 L
"C:\\reactos\\system32\\smss.exe");
220 ExeModule
->Flags
= 0;
221 ExeModule
->LoadCount
= -1; /* don't unload */
222 ExeModule
->TlsIndex
= 0;
223 ExeModule
->SectionHandle
= NULL
;
224 ExeModule
->CheckSum
= 0;
226 NTHeaders
= RtlImageNtHeader (ExeModule
->BaseAddress
);
227 ExeModule
->SizeOfImage
= NTHeaders
->OptionalHeader
.SizeOfImage
;
228 ExeModule
->TimeDateStamp
= NTHeaders
->FileHeader
.TimeDateStamp
;
230 InsertHeadList(&Peb
->Ldr
->InLoadOrderModuleList
,
231 &ExeModule
->InLoadOrderModuleList
);
235 LdrpLoadUserModuleSymbols(ExeModule
);
239 EntryPoint
= LdrPEStartup((PVOID
)ImageBase
, NULL
);
240 ExeModule
->EntryPoint
= (ULONG
)EntryPoint
;
242 /* all required dlls are loaded now */
243 Peb
->Ldr
->Initialized
= TRUE
;
245 if (EntryPoint
== NULL
)
247 DbgPrint("Failed to initialize image\n");
248 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
251 Status
= EntryPoint(Peb
);
252 ZwTerminateProcess(NtCurrentProcess(),Status
);