some __stdcall fixes in ntoskrnl and ntdll
[reactos.git] / reactos / lib / ntdll / ldr / startup.c
1 /* $Id: startup.c,v 1.9 1999/08/29 06:59:04 ea 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_PEHDR
15 #include <windows.h>
16 #include <ddk/ntddk.h>
17 #include <pe.h>
18 #include <string.h>
19 #include <wchar.h>
20 #include <ntdll/ldr.h>
21 #include <ntdll/rtl.h>
22
23 #ifndef DBG_NTDLL_LDR_STARTUP
24 #define NDEBUG
25 #endif
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 NTSTATUS
38 LdrMapNTDllForProcess (
39 HANDLE ProcessHandle,
40 PHANDLE PtrNTDllSectionHandle
41 )
42 {
43 ULONG InitialViewSize;
44 NTSTATUS Status;
45 HANDLE NTDllSectionHandle;
46 PVOID ImageBase;
47 PIMAGE_NT_HEADERS NTHeaders;
48 PIMAGE_DOS_HEADER PEDosHeader;
49
50 DPRINT("LdrMapNTDllForProcess(ProcessHandle %x)\n",ProcessHandle);
51
52 PEDosHeader = (PIMAGE_DOS_HEADER)LdrDllListHead.BaseAddress;
53 NTHeaders = (PIMAGE_NT_HEADERS)(LdrDllListHead.BaseAddress +
54 PEDosHeader->e_lfanew);
55
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,
62 ProcessHandle,
63 (PVOID *)&ImageBase,
64 0,
65 InitialViewSize,
66 NULL,
67 &InitialViewSize,
68 0,
69 MEM_COMMIT,
70 PAGE_READWRITE);
71 LdrMapSections(ProcessHandle,
72 ImageBase,
73 NTDllSectionHandle,
74 NTHeaders);
75 *PtrNTDllSectionHandle = NTDllSectionHandle;
76 return(STATUS_SUCCESS);
77 }
78
79 /* LdrStartup
80 * FUNCTION:
81 * Handles Process Startup Activities.
82 * ARGUMENTS:
83 * DWORD ImageBase The base address of the process image
84 */
85 VOID LdrStartup(HANDLE SectionHandle,
86 DWORD ImageBase,
87 HANDLE NTDllSectionHandle)
88 {
89 PEPFUNC EntryPoint;
90 PIMAGE_DOS_HEADER PEDosHeader;
91 NTSTATUS Status;
92 PIMAGE_NT_HEADERS NTHeaders;
93
94 DPRINT("LdrStartup(ImageBase %x, SectionHandle %x, "
95 "NTDllSectionHandle %x)\n",ImageBase,
96 SectionHandle, NTDllSectionHandle);
97
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);
105
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)
111 {
112 DPRINT("Image has bad header\n");
113 ZwTerminateProcess(NULL,STATUS_UNSUCCESSFUL);
114 }
115
116 NTHeaders = (PIMAGE_NT_HEADERS)(ImageBase + PEDosHeader->e_lfanew);
117 __ProcessHeap = RtlCreateHeap(0,
118 (PVOID)HEAP_BASE,
119 NTHeaders->OptionalHeader.SizeOfHeapCommit,
120 NTHeaders->OptionalHeader.SizeOfHeapReserve,
121 NULL,
122 NULL);
123 EntryPoint = LdrPEStartup((PVOID)ImageBase, SectionHandle);
124
125 if (EntryPoint == NULL)
126 {
127 dprintf("Failed to initialize image\n");
128 ZwTerminateProcess(NtCurrentProcess(),STATUS_UNSUCCESSFUL);
129 }
130
131 dprintf("Transferring control to image at %x\n",EntryPoint);
132 Status = EntryPoint();
133 ZwTerminateProcess(NtCurrentProcess(),Status);
134 }
135
136
137 /* EOF */