2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: ntoskrnl/ldr/sysdll.c
5 * PURPOSE: Loaders for PE executables
6 * PROGRAMMERS: Jean Michault
7 * Rex Jolliff (rex@lvcablemodem.com)
12 /* INCLUDES *****************************************************************/
14 #include <ddk/ntddk.h>
15 #include <internal/i386/segment.h>
16 #include <internal/linkage.h>
17 #include <internal/module.h>
18 #include <internal/ntoskrnl.h>
19 #include <internal/ob.h>
20 #include <internal/ps.h>
22 #include <internal/string.h>
23 #include <internal/teb.h>
26 #include <internal/debug.h>
28 /* GLOBALS *******************************************************************/
30 static PVOID SystemDllEntryPoint
= NULL
;
31 //static PVOID SystemDllApcDispatcher = NULL;
32 //static PVOID SystemDllCallbackDispatcher = NULL;
33 //static PVOID SystemDllExceptionDispatcher = NULL;
35 /* FUNCTIONS *****************************************************************/
37 PVOID
LdrpGetSystemDllEntryPoint(VOID
)
39 return(SystemDllEntryPoint
);
42 NTSTATUS
LdrpMapSystemDll(HANDLE ProcessHandle
,
43 PVOID
* LdrStartupAddr
)
45 * FUNCTION: LdrpMapSystemDll maps the system dll into the specified process
46 * address space and returns its startup address.
49 * Points to the process to map the system dll into
52 * Receives the startup address of the system dll on function
58 CHAR BlockBuffer
[1024];
62 OBJECT_ATTRIBUTES FileObjectAttributes
;
64 HANDLE NTDllSectionHandle
;
65 UNICODE_STRING DllPathname
;
66 PIMAGE_DOS_HEADER DosHeader
;
67 PIMAGE_NT_HEADERS NTHeaders
;
68 ULONG InitialViewSize
;
72 * Locate and open NTDLL to determine ImageBase
75 RtlInitUnicodeString(&DllPathname
,
76 L
"\\SystemRoot\\system32\\ntdll.dll");
77 InitializeObjectAttributes(&FileObjectAttributes
,
82 DPRINT("Opening NTDLL\n");
83 Status
= ZwOpenFile(&FileHandle
,
85 &FileObjectAttributes
,
89 if (!NT_SUCCESS(Status
))
91 DbgPrint("NTDLL open failed (Status %x)\n", Status
);
94 Status
= ZwReadFile(FileHandle
,
103 if (!NT_SUCCESS(Status
))
105 DbgPrint("NTDLL header read failed (Status %x)\n", Status
);
111 * FIXME: this will fail if the NT headers are
112 * more than 1024 bytes from start.
114 DosHeader
= (PIMAGE_DOS_HEADER
) BlockBuffer
;
115 NTHeaders
= (PIMAGE_NT_HEADERS
) (BlockBuffer
+ DosHeader
->e_lfanew
);
116 if ((DosHeader
->e_magic
!= IMAGE_DOS_MAGIC
)
117 || (DosHeader
->e_lfanew
== 0L)
118 || (*(PULONG
) NTHeaders
!= IMAGE_PE_MAGIC
))
120 DbgPrint("NTDLL format invalid\n");
122 return(STATUS_UNSUCCESSFUL
);
124 ImageBase
= NTHeaders
->OptionalHeader
.ImageBase
;
125 ImageSize
= NTHeaders
->OptionalHeader
.SizeOfImage
;
128 * FIXME: retrieve the offset of LdrStartup from NTDLL
130 DPRINT("ImageBase %x\n",ImageBase
);
132 (PVOID
)ImageBase
+ NTHeaders
->OptionalHeader
.AddressOfEntryPoint
;
133 DPRINT("LdrStartupAddr %x\n", LdrStartupAddr
);
134 SystemDllEntryPoint
= *LdrStartupAddr
;
137 * Create a section for NTDLL
139 DPRINT("Creating section\n");
140 Status
= ZwCreateSection(&NTDllSectionHandle
,
147 if (!NT_SUCCESS(Status
))
149 DbgPrint("NTDLL create section failed (Status %x)\n", Status
);
156 * Map the NTDLL into the process
158 InitialViewSize
= DosHeader
->e_lfanew
+
159 sizeof (IMAGE_NT_HEADERS
) +
160 (sizeof (IMAGE_SECTION_HEADER
) * NTHeaders
->FileHeader
.NumberOfSections
);
161 DPRINT("Mapping view of section\n");
162 Status
= ZwMapViewOfSection(NTDllSectionHandle
,
172 if (!NT_SUCCESS(Status
))
174 DbgPrint("NTDLL map view of secion failed (Status %x)", Status
);
175 ZwClose(NTDllSectionHandle
);
179 for (i
= 0; i
< NTHeaders
->FileHeader
.NumberOfSections
; i
++)
181 PIMAGE_SECTION_HEADER Sections
;
182 LARGE_INTEGER Offset
;
185 DPRINT("Mapping view of section %d\n", i
);
186 Sections
= (PIMAGE_SECTION_HEADER
) SECHDROFFSET(BlockBuffer
);
187 DPRINT("Sections %x\n", Sections
);
188 Base
= Sections
[i
].VirtualAddress
+ ImageBase
;
189 DPRINT("Base %x\n", Base
);
190 Offset
.u
.LowPart
= Sections
[i
].PointerToRawData
;
191 Offset
.u
.HighPart
= 0;
192 DPRINT("Mapping view of section\n");
193 Status
= ZwMapViewOfSection(NTDllSectionHandle
,
197 Sections
[i
].Misc
.VirtualSize
,
199 (PULONG
)&Sections
[i
].Misc
.VirtualSize
,
203 if (!NT_SUCCESS(Status
))
205 DbgPrint("NTDLL map view of secion failed (Status %x)\n", Status
);
206 ZwClose(NTDllSectionHandle
);
210 DPRINT("Finished mapping\n");
211 ZwClose(NTDllSectionHandle
);
213 return(STATUS_SUCCESS
);