2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ldr/loader.c
5 * PURPOSE: Loaders for PE executables
6 * PROGRAMMER: David Welch (welch@mcmail.com)
11 /* INCLUDES *****************************************************************/
13 #include <internal/kernel.h>
14 #include <internal/linkage.h>
15 #include <ddk/ntddk.h>
17 #include <internal/debug.h>
20 /* FUNCTIONS *****************************************************************/
22 NTSTATUS
LdrProcessImage(HANDLE SectionHandle
, PVOID BaseAddress
)
24 PIMAGE_DOS_HEADER dos_hdr
= (PIMAGE_DOS_HEADER
)BaseAddress
;
25 PIMAGE_NT_HEADERS hdr
= (PIMAGE_NT_HEADERS
)(BaseAddress
27 PIMAGE_SECTION_HEADER sections
= (PIMAGE_SECTION_HEADER
)(BaseAddress
28 + dos_hdr
->e_lfanew
+ sizeof(IMAGE_NT_HEADERS
));
30 // FIXME: Check image signature
31 // FIXME: Check architechture
32 // FIXME: Build/Load image sections
33 // FIXME: resolve imports
38 NTSTATUS
LdrLoadDriver(PUNICODE_STRING FileName
)
40 * FUNCTION: Loads a PE executable into the kernel
42 * FileName = Driver to load
49 ANSI_STRING AnsiFileName
;
50 UNICODE_STRING UnicodeFileName
;
51 OBJECT_ATTRIBUTES FileAttributes
;
54 // Open the image file or die
55 RtlInitAnsiString(&AnsiFileName
, FileName
);
56 RtlAnsiStringToUnicodeString(&UnicodeFileName
, &AnsiFileName
, TRUE
);
57 InitializeObjectAttributes(&FileAttributes
,
62 FileHandle
= ZwFileOpen(&FileHandle
, 0, &FileAttributes
, NULL
, 0, 0);
63 if (!NT_SUCCESS(Status
))
67 RtlFreeUnicodeString(&UnicodeFileName
);
69 // Map the image into a section or die
70 Status
= ZwCreateSection(&SectionHandle
,
77 if (!NT_SUCCESS(Status
))
82 // FIXME: get the base address of the section
84 ZwCloseFile(FileHandle
);
86 return LdrProcessImage(SectionHandle
, BaseAddress
);
90 * FUNCTION: Loads a PE executable into the specified process
92 * Filename = File to load
93 * ProcessHandle = handle
98 LdrLoadImage(PUNICODE_STRING Filename
, HANDLE ProcessHandle
)
100 char BlockBuffer
[512];
103 OBJECT_ATTRIBUTES FileObjectAttributes
;
104 PIMAGE_DOS_HEADER PEDosHeader
;
105 PIMAGE_NT_HEADERS PEHeader
;
107 HANDLE SectionHandle
;
110 /* Open the image file */
111 InitializeObjectAttributes(&FileObjectAttributes
,
116 Status
= ZwFileOpen(&FileHandle
, 0, &FileObjectAttributes
, NULL
, 0, 0);
117 if (!NT_SUCCESS(Status
))
122 /* Read first block of image to determine type */
123 Status
= ZwReadFile(FileHandle
, 0, 0, 0, 0, BlockBuffer
, 512, 0, 0);
124 if (!NT_SUCCESS(Status
))
130 /* If MZ header exists */
131 PEDosHeader
= (PIMAGE_DOS_HEADER
) BlockBuffer
;
132 if (PEDosHeader
->e_magic
== 0x54AD)
134 /* FIXME: if PE header exists */
135 /* FIXME: load PE image */
137 /* FIXME: load MZ image */
139 else /* Assume bin format and load */
140 /* FIXME: could check for a.out, ELF, COFF, etc. images here... */
142 Status
= ZwCreateSection(&SectionHandle
,
150 if (!NT_SUCCESS(Status
))
155 BaseAddress
= (PVOID
)0x10000;
156 SectionOffset
.HighPart
= 0;
157 SectionOffset
.LowPart
= 0;
159 /* FIXME: get the size of the file */
162 ZwMapViewOfSection(SectionHandle
,
173 memset(&Context
,0,sizeof(CONTEXT
));
175 Context
.SegSs
= USER_DS
;
176 Context
.Esp
= 0x2000;
177 Context
.EFlags
= 0x202;
178 Context
.SegCs
= USER_CS
;
179 Context
.Eip
= 0x10000;
180 Context
.SegDs
= USER_DS
;
181 Context
.SegEs
= USER_DS
;
182 Context
.SegFs
= USER_DS
;
183 Context
.SegGs
= USER_DS
;
185 BaseAddress
= 0x1000;
187 ZwAllocateVirtualMemory(ProcessHandle
,
193 ZwCreateThread(&ThreadHandle
,
203 /* FIXME: should DLLs be named sections? */
204 /* FIXME: get current process and associate with section */
206 // Map the image into a section or die
207 Status
= ZwCreateSection(&SectionHandle
,
214 if (!NT_SUCCESS(Status
))
219 // FIXME: get the base address of the section
221 ZwCloseFile(FileHandle
);
223 // FIXME: initialize process context for image
225 return LdrProcessImage(SectionHandle
, BaseAddress
);