1 /* $Id: startup.c,v 1.28 2000/08/11 12:34:58 ekohl 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>
22 #include <ntdll/ntdll.h>
25 VOID
RtlpInitProcessHeaps (PPEB Peb
);
27 /* GLOBALS *******************************************************************/
30 extern unsigned int _image_base__
;
32 CRITICAL_SECTION PebLock
;
34 ULONG NtGlobalFlag
= 0;
37 /* FUNCTIONS *****************************************************************/
40 LdrInitializeThunk (ULONG Unknown1
,
46 PIMAGE_DOS_HEADER PEDosHeader
;
48 PIMAGE_NT_HEADERS NTHeaders
;
52 DPRINT("LdrInitializeThunk()\n");
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
);
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)
68 DPRINT("ImageBase is null\n");
69 ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL
);
72 NtGlobalFlag
= Peb
->NtGlobalFlag
;
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
)
81 DbgPrint("Image has bad header\n");
82 ZwTerminateProcess(NtCurrentProcess(), STATUS_UNSUCCESSFUL
);
85 /* normalize process parameters */
86 RtlNormalizeProcessParams (Peb
->ProcessParameters
);
89 /* initialize NLS data */
90 RtlInitNlsTables (Peb
->AnsiCodePageData
,
92 Peb
->UnicodeCaseTableData
,
94 RtlResetRtlTranslations (&TranslationTable
);
97 NTHeaders
= (PIMAGE_NT_HEADERS
)(ImageBase
+ PEDosHeader
->e_lfanew
);
99 /* create process heap */
100 Peb
->ProcessHeap
= RtlCreateHeap(0,
102 NTHeaders
->OptionalHeader
.SizeOfHeapCommit
,
103 NTHeaders
->OptionalHeader
.SizeOfHeapReserve
,
106 if (Peb
->ProcessHeap
== 0)
108 DbgPrint("Failed to create process heap\n");
109 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
112 /* initialize process heaps support */
113 RtlpInitProcessHeaps (Peb
);
115 /* initalize peb lock support */
116 RtlInitializeCriticalSection (&PebLock
);
117 Peb
->FastPebLock
= &PebLock
;
118 Peb
->FastPebLockRoutine
= (PPEBLOCKROUTINE
)RtlEnterCriticalSection
;
119 Peb
->FastPebUnlockRoutine
= (PPEBLOCKROUTINE
)RtlLeaveCriticalSection
;
121 EntryPoint
= LdrPEStartup((PVOID
)ImageBase
, NULL
);
122 if (EntryPoint
== NULL
)
124 DbgPrint("Failed to initialize image\n");
125 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
128 DbgPrint("Transferring control to image at %x\n",EntryPoint
);
129 Status
= EntryPoint(Peb
);
130 ZwTerminateProcess(NtCurrentProcess(),Status
);