1 /* $Id: startup.c,v 1.9 1999/08/29 06:59:04 ea 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_PEHDR
16 #include <ddk/ntddk.h>
20 #include <ntdll/ldr.h>
21 #include <ntdll/rtl.h>
23 #ifndef DBG_NTDLL_LDR_STARTUP
26 #include <ntdll/ntdll.h>
28 /* GLOBALS *******************************************************************/
31 extern unsigned int _image_base__
;
32 extern HANDLE __ProcessHeap
;
35 /* FUNCTIONS *****************************************************************/
38 LdrMapNTDllForProcess (
40 PHANDLE PtrNTDllSectionHandle
43 ULONG InitialViewSize
;
45 HANDLE NTDllSectionHandle
;
47 PIMAGE_NT_HEADERS NTHeaders
;
48 PIMAGE_DOS_HEADER PEDosHeader
;
50 DPRINT("LdrMapNTDllForProcess(ProcessHandle %x)\n",ProcessHandle
);
52 PEDosHeader
= (PIMAGE_DOS_HEADER
)LdrDllListHead
.BaseAddress
;
53 NTHeaders
= (PIMAGE_NT_HEADERS
)(LdrDllListHead
.BaseAddress
+
54 PEDosHeader
->e_lfanew
);
56 NTDllSectionHandle
= LdrDllListHead
.SectionHandle
;
57 InitialViewSize
= PEDosHeader
->e_lfanew
+ sizeof(IMAGE_NT_HEADERS
)
58 + sizeof(IMAGE_SECTION_HEADER
) * NTHeaders
->FileHeader
.NumberOfSections
;
59 ImageBase
= LdrDllListHead
.BaseAddress
;
60 DPRINT("Mapping at %x\n",ImageBase
);
61 Status
= ZwMapViewOfSection(NTDllSectionHandle
,
71 LdrMapSections(ProcessHandle
,
75 *PtrNTDllSectionHandle
= NTDllSectionHandle
;
76 return(STATUS_SUCCESS
);
81 * Handles Process Startup Activities.
83 * DWORD ImageBase The base address of the process image
85 VOID
LdrStartup(HANDLE SectionHandle
,
87 HANDLE NTDllSectionHandle
)
90 PIMAGE_DOS_HEADER PEDosHeader
;
92 PIMAGE_NT_HEADERS NTHeaders
;
94 DPRINT("LdrStartup(ImageBase %x, SectionHandle %x, "
95 "NTDllSectionHandle %x)\n",ImageBase
,
96 SectionHandle
, NTDllSectionHandle
);
98 LdrDllListHead
.BaseAddress
= (PVOID
)&_image_base__
;
99 LdrDllListHead
.Prev
= &LdrDllListHead
;
100 LdrDllListHead
.Next
= &LdrDllListHead
;
101 LdrDllListHead
.SectionHandle
= NTDllSectionHandle
;
102 PEDosHeader
= (PIMAGE_DOS_HEADER
)LdrDllListHead
.BaseAddress
;
103 LdrDllListHead
.Headers
= (PIMAGE_NT_HEADERS
)(LdrDllListHead
.BaseAddress
+
104 PEDosHeader
->e_lfanew
);
106 /* If MZ header exists */
107 PEDosHeader
= (PIMAGE_DOS_HEADER
) ImageBase
;
108 if (PEDosHeader
->e_magic
!= IMAGE_DOS_MAGIC
||
109 PEDosHeader
->e_lfanew
== 0L ||
110 *(PULONG
)((PUCHAR
)ImageBase
+ PEDosHeader
->e_lfanew
) != IMAGE_PE_MAGIC
)
112 DPRINT("Image has bad header\n");
113 ZwTerminateProcess(NULL
,STATUS_UNSUCCESSFUL
);
116 NTHeaders
= (PIMAGE_NT_HEADERS
)(ImageBase
+ PEDosHeader
->e_lfanew
);
117 __ProcessHeap
= RtlCreateHeap(0,
119 NTHeaders
->OptionalHeader
.SizeOfHeapCommit
,
120 NTHeaders
->OptionalHeader
.SizeOfHeapReserve
,
123 EntryPoint
= LdrPEStartup((PVOID
)ImageBase
, SectionHandle
);
125 if (EntryPoint
== NULL
)
127 dprintf("Failed to initialize image\n");
128 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL
);
131 dprintf("Transferring control to image at %x\n",EntryPoint
);
132 Status
= EntryPoint();
133 ZwTerminateProcess(NtCurrentProcess(),Status
);