1 /* $Id: startup.c,v 1.18 2000/02/13 16:05:14 dwelch 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 #define WIN32_NO_STATUS
15 #define WIN32_NO_PEHDR
17 #include <ddk/ntddk.h>
21 #include <ntdll/ldr.h>
22 #include <ntdll/rtl.h>
23 #include <csrss/csrss.h>
26 #include <ntdll/ntdll.h>
28 /* GLOBALS *******************************************************************/
31 extern unsigned int _image_base__
;
32 extern HANDLE __ProcessHeap
;
35 /* FUNCTIONS *****************************************************************/
40 PIMAGE_DOS_HEADER PEDosHeader
;
42 PIMAGE_NT_HEADERS NTHeaders
;
46 DPRINT("LdrStartup()\n");
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
);
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)
63 DPRINT("ImageBase is null\n");
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
)
74 DbgPrint("Image has bad header\n");
75 ZwTerminateProcess(NULL
, STATUS_UNSUCCESSFUL
);
78 /* normalize process parameters */
79 RtlNormalizeProcessParams (Peb
->ProcessParameters
);
81 NTHeaders
= (PIMAGE_NT_HEADERS
)(ImageBase
+ PEDosHeader
->e_lfanew
);
82 __ProcessHeap
= RtlCreateHeap(0,
84 NTHeaders
->OptionalHeader
.SizeOfHeapCommit
,
85 NTHeaders
->OptionalHeader
.SizeOfHeapReserve
,
88 EntryPoint
= LdrPEStartup((PVOID
)ImageBase
, NULL
);
90 if (EntryPoint
== NULL
)
92 DbgPrint("Failed to initialize image\n");
93 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
99 Status
= CsrConnectToServer();
100 if (!NT_SUCCESS(Status
))
102 DbgPrint("Failed to connect to csrss.exe: expect trouble\n");
105 DbgPrint("Transferring control to image at %x\n",EntryPoint
);
106 Status
= EntryPoint(NULL
);
107 ZwTerminateProcess(NtCurrentProcess(),Status
);