Moved user-process loading into ZwCreateProcess
[reactos.git] / reactos / lib / ntdll / ldr / startup.c
1 /* $Id: startup.c,v 1.18 2000/02/13 16:05:14 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 #define WIN32_NO_STATUS
15 #define WIN32_NO_PEHDR
16 #include <windows.h>
17 #include <ddk/ntddk.h>
18 #include <pe.h>
19 #include <string.h>
20 #include <wchar.h>
21 #include <ntdll/ldr.h>
22 #include <ntdll/rtl.h>
23 #include <csrss/csrss.h>
24
25 #define NDEBUG
26 #include <ntdll/ntdll.h>
27
28 /* GLOBALS *******************************************************************/
29
30 DLL LdrDllListHead;
31 extern unsigned int _image_base__;
32 extern HANDLE __ProcessHeap;
33
34
35 /* FUNCTIONS *****************************************************************/
36
37 VOID LdrStartup(VOID)
38 {
39 PEPFUNC EntryPoint;
40 PIMAGE_DOS_HEADER PEDosHeader;
41 NTSTATUS Status;
42 PIMAGE_NT_HEADERS NTHeaders;
43 PVOID ImageBase;
44 PPEB Peb;
45
46 DPRINT("LdrStartup()\n");
47
48 LdrDllListHead.BaseAddress = (PVOID)&_image_base__;
49 LdrDllListHead.Prev = &LdrDllListHead;
50 LdrDllListHead.Next = &LdrDllListHead;
51 LdrDllListHead.SectionHandle = NULL;
52 PEDosHeader = (PIMAGE_DOS_HEADER)LdrDllListHead.BaseAddress;
53 LdrDllListHead.Headers = (PIMAGE_NT_HEADERS)(LdrDllListHead.BaseAddress +
54 PEDosHeader->e_lfanew);
55
56
57 Peb = (PPEB)(PEB_BASE);
58 DPRINT("Peb %x\n", Peb);
59 ImageBase = Peb->ImageBaseAddress;
60 DPRINT("ImageBase %x\n", ImageBase);
61 if (ImageBase <= (PVOID)0x1000)
62 {
63 DPRINT("ImageBase is null\n");
64 for(;;);
65 }
66
67 /* If MZ header exists */
68 PEDosHeader = (PIMAGE_DOS_HEADER) ImageBase;
69 DPRINT("PEDosHeader %x\n", PEDosHeader);
70 if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC ||
71 PEDosHeader->e_lfanew == 0L ||
72 *(PULONG)((PUCHAR)ImageBase + PEDosHeader->e_lfanew) != IMAGE_PE_MAGIC)
73 {
74 DbgPrint("Image has bad header\n");
75 ZwTerminateProcess(NULL, STATUS_UNSUCCESSFUL);
76 }
77
78 /* normalize process parameters */
79 RtlNormalizeProcessParams (Peb->ProcessParameters);
80
81 NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + PEDosHeader->e_lfanew);
82 __ProcessHeap = RtlCreateHeap(0,
83 (PVOID)HEAP_BASE,
84 NTHeaders->OptionalHeader.SizeOfHeapCommit,
85 NTHeaders->OptionalHeader.SizeOfHeapReserve,
86 NULL,
87 NULL);
88 EntryPoint = LdrPEStartup((PVOID)ImageBase, NULL);
89
90 if (EntryPoint == NULL)
91 {
92 DbgPrint("Failed to initialize image\n");
93 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
94 }
95
96 /*
97 *
98 */
99 Status = CsrConnectToServer();
100 if (!NT_SUCCESS(Status))
101 {
102 DbgPrint("Failed to connect to csrss.exe: expect trouble\n");
103 }
104
105 DbgPrint("Transferring control to image at %x\n",EntryPoint);
106 Status = EntryPoint(NULL);
107 ZwTerminateProcess(NtCurrentProcess(),Status);
108 }
109
110 /* EOF */