3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/ldr/loader.c
6 * PURPOSE: Loaders for PE executables
8 * PROGRAMMERS: Jean Michault
9 * Rex Jolliff (rex@lvcablemodem.com)
10 * Jason Filby (jasonfilby@yahoo.com)
11 * Casper S. Hornstrup (chorns@users.sourceforge.net)
15 /* INCLUDES *****************************************************************/
20 #include <internal/ntosdbg.h>
31 #define ps(args...) DPRINT1(args)
35 #include <internal/debug.h>
37 #if defined (ALLOC_PRAGMA)
38 #pragma alloc_text(INIT, LdrInit1)
39 #pragma alloc_text(INIT, LdrInitModuleManagement)
40 #pragma alloc_text(INIT, LdrSafePEProcessModule)
43 /* GLOBALS *******************************************************************/
45 LIST_ENTRY ModuleListHead
;
46 KSPIN_LOCK ModuleListLock
;
47 LDR_DATA_TABLE_ENTRY NtoskrnlModuleObject
;
48 LDR_DATA_TABLE_ENTRY HalModuleObject
;
52 /* FORWARD DECLARATIONS ******************************************************/
57 PUNICODE_STRING ModuleName
,
58 PLDR_DATA_TABLE_ENTRY
*ModuleObject
);
61 LdrpBuildModuleBaseName (
62 PUNICODE_STRING BaseName
,
63 PUNICODE_STRING FullName
);
66 LdrpCompareModuleNames (
67 IN PUNICODE_STRING String1
,
68 IN PUNICODE_STRING String2
);
71 /* PE Driver load support */
75 PUNICODE_STRING FileName
,
76 PLDR_DATA_TABLE_ENTRY
*ModuleObject
);
79 LdrPEGetExportByName (
85 LdrPEFixupForward ( PCHAR ForwardName
);
88 LdrPEPerformRelocations (
93 LdrPEFixupImports ( PLDR_DATA_TABLE_ENTRY Module
);
95 /* FUNCTIONS *****************************************************************/
99 LdrInitDebug ( PLOADER_MODULE Module
, PWCH Name
)
108 /* Hook for KDB on initialization of the loader. */
109 KDB_LOADERINIT_HOOK(&NtoskrnlModuleObject
, &HalModuleObject
);
115 LdrInitModuleManagement ( VOID
)
117 PIMAGE_NT_HEADERS NtHeader
;
119 /* Initialize the module list and spinlock */
120 InitializeListHead(&ModuleListHead
);
121 KeInitializeSpinLock(&ModuleListLock
);
123 /* Initialize ModuleObject for NTOSKRNL */
124 RtlZeroMemory(&NtoskrnlModuleObject
, sizeof(LDR_DATA_TABLE_ENTRY
));
125 NtoskrnlModuleObject
.DllBase
= (PVOID
) KERNEL_BASE
;
126 RtlInitUnicodeString(&NtoskrnlModuleObject
.FullDllName
, KERNEL_MODULE_NAME
);
127 LdrpBuildModuleBaseName(&NtoskrnlModuleObject
.BaseDllName
, &NtoskrnlModuleObject
.FullDllName
);
129 NtHeader
= RtlImageNtHeader((PVOID
)KERNEL_BASE
);
130 NtoskrnlModuleObject
.EntryPoint
= (PVOID
) ((ULONG_PTR
) NtoskrnlModuleObject
.DllBase
+ NtHeader
->OptionalHeader
.AddressOfEntryPoint
);
131 DPRINT("ModuleObject:%08x entrypoint at %x\n", &NtoskrnlModuleObject
, NtoskrnlModuleObject
.EntryPoint
);
132 NtoskrnlModuleObject
.SizeOfImage
= NtHeader
->OptionalHeader
.SizeOfImage
;
134 InsertTailList(&ModuleListHead
, &NtoskrnlModuleObject
.InLoadOrderModuleList
);
136 /* Initialize ModuleObject for HAL */
137 RtlZeroMemory(&HalModuleObject
, sizeof(LDR_DATA_TABLE_ENTRY
));
138 HalModuleObject
.DllBase
= (PVOID
) LdrHalBase
;
140 RtlInitUnicodeString(&HalModuleObject
.FullDllName
, HAL_MODULE_NAME
);
141 LdrpBuildModuleBaseName(&HalModuleObject
.BaseDllName
, &HalModuleObject
.FullDllName
);
143 NtHeader
= RtlImageNtHeader((PVOID
)LdrHalBase
);
144 HalModuleObject
.EntryPoint
= (PVOID
) ((ULONG_PTR
) HalModuleObject
.DllBase
+ NtHeader
->OptionalHeader
.AddressOfEntryPoint
);
145 DPRINT("ModuleObject:%08x entrypoint at %x\n", &HalModuleObject
, HalModuleObject
.EntryPoint
);
146 HalModuleObject
.SizeOfImage
= NtHeader
->OptionalHeader
.SizeOfImage
;
148 InsertTailList(&ModuleListHead
, &HalModuleObject
.InLoadOrderModuleList
);
154 PUNICODE_STRING DriverName
,
156 PVOID
*SectionPointer
,
158 PVOID
*ExportSectionPointer
)
160 PLDR_DATA_TABLE_ENTRY ModuleObject
;
163 ModuleObject
= LdrGetModuleObject(DriverName
);
164 if (ModuleObject
== NULL
)
166 Status
= LdrLoadModule(DriverName
, &ModuleObject
);
167 if (!NT_SUCCESS(Status
))
174 *ModuleBase
= ModuleObject
->DllBase
;
177 *SectionPointer
= ModuleObject
;
180 *EntryPoint
= ModuleObject
->EntryPoint
;
182 //if (ExportSectionPointer)
183 // *ExportSectionPointer = ModuleObject->
185 return(STATUS_SUCCESS
);
191 LdrpUnloadImage ( PVOID ModuleBase
)
193 return(STATUS_NOT_IMPLEMENTED
);
199 LdrpLoadAndCallImage ( PUNICODE_STRING ModuleName
)
201 PDRIVER_INITIALIZE DriverEntry
;
202 PLDR_DATA_TABLE_ENTRY ModuleObject
;
203 DRIVER_OBJECT DriverObject
;
206 ModuleObject
= LdrGetModuleObject(ModuleName
);
207 if (ModuleObject
!= NULL
)
209 return(STATUS_IMAGE_ALREADY_LOADED
);
212 Status
= LdrLoadModule(ModuleName
, &ModuleObject
);
213 if (!NT_SUCCESS(Status
))
218 DriverEntry
= (PDRIVER_INITIALIZE
)ModuleObject
->EntryPoint
;
220 RtlZeroMemory(&DriverObject
, sizeof(DriverObject
));
221 // DriverObject.DriverStart = ModuleObject->DllBase;
223 Status
= DriverEntry(&DriverObject
, NULL
);
224 if (!NT_SUCCESS(Status
))
226 LdrUnloadModule(ModuleObject
);
236 PUNICODE_STRING Filename
,
237 PLDR_DATA_TABLE_ENTRY
*ModuleObject
)
239 PVOID ModuleLoadBase
;
242 OBJECT_ATTRIBUTES ObjectAttributes
;
243 PLDR_DATA_TABLE_ENTRY Module
;
244 FILE_STANDARD_INFORMATION FileStdInfo
;
245 IO_STATUS_BLOCK IoStatusBlock
;
247 *ModuleObject
= NULL
;
249 DPRINT("Loading Module %wZ...\n", Filename
);
251 /* Open the Module */
252 InitializeObjectAttributes(&ObjectAttributes
,
254 OBJ_CASE_INSENSITIVE
,
258 Status
= ZwOpenFile(&FileHandle
,
263 FILE_SYNCHRONOUS_IO_NONALERT
);
265 if (!NT_SUCCESS(Status
))
267 CPRINT("Could not open module file: %wZ (Status 0x%08lx)\n", Filename
, Status
);
272 /* Get the size of the file */
273 Status
= ZwQueryInformationFile(FileHandle
,
277 FileStandardInformation
);
278 if (!NT_SUCCESS(Status
))
280 CPRINT("Could not get file size\n");
286 /* Allocate nonpageable memory for driver */
287 ModuleLoadBase
= ExAllocatePoolWithTag(NonPagedPool
,
288 FileStdInfo
.EndOfFile
.u
.LowPart
,
290 if (ModuleLoadBase
== NULL
)
292 CPRINT("Could not allocate memory for module");
294 return(STATUS_INSUFFICIENT_RESOURCES
);
298 /* Load driver into memory chunk */
299 Status
= ZwReadFile(FileHandle
,
303 FileStdInfo
.EndOfFile
.u
.LowPart
,
305 if (!NT_SUCCESS(Status
))
307 CPRINT("Could not read module file into memory");
308 ExFreePool(ModuleLoadBase
);
316 Status
= LdrProcessModule(ModuleLoadBase
,
319 if (!NT_SUCCESS(Status
))
321 CPRINT("Could not process module\n");
322 ExFreePool(ModuleLoadBase
);
327 ExFreePool(ModuleLoadBase
);
329 *ModuleObject
= Module
;
331 /* Hook for KDB on loading a driver. */
332 KDB_LOADDRIVER_HOOK(Filename
, Module
);
334 return(STATUS_SUCCESS
);
340 LdrUnloadModule ( PLDR_DATA_TABLE_ENTRY ModuleObject
)
344 /* Remove the module from the module list */
345 KeAcquireSpinLock(&ModuleListLock
,&Irql
);
346 RemoveEntryList(&ModuleObject
->InLoadOrderModuleList
);
347 KeReleaseSpinLock(&ModuleListLock
, Irql
);
349 /* Hook for KDB on unloading a driver. */
350 KDB_UNLOADDRIVER_HOOK(ModuleObject
);
352 /* Free module section */
353 // MmFreeSection(ModuleObject->DllBase);
355 ExFreePool(ModuleObject
->FullDllName
.Buffer
);
356 ExFreePool(ModuleObject
);
358 return(STATUS_SUCCESS
);
364 PVOID ModuleLoadBase
,
365 PUNICODE_STRING ModuleName
,
366 PLDR_DATA_TABLE_ENTRY
*ModuleObject
)
368 PIMAGE_DOS_HEADER PEDosHeader
;
370 /* If MZ header exists */
371 PEDosHeader
= (PIMAGE_DOS_HEADER
) ModuleLoadBase
;
372 if (PEDosHeader
->e_magic
== IMAGE_DOS_SIGNATURE
&& PEDosHeader
->e_lfanew
!= 0L)
374 return LdrPEProcessModule(ModuleLoadBase
,
379 CPRINT("Module wasn't PE\n");
380 return STATUS_UNSUCCESSFUL
;
385 LdrpQueryModuleInformation (
390 PLIST_ENTRY current_entry
;
391 PLDR_DATA_TABLE_ENTRY current
;
392 ULONG ModuleCount
= 0;
393 PSYSTEM_MODULE_INFORMATION Smi
;
394 ANSI_STRING AnsiName
;
398 KeAcquireSpinLock(&ModuleListLock
,&Irql
);
400 /* calculate required size */
401 current_entry
= ModuleListHead
.Flink
;
402 while (current_entry
!= (&ModuleListHead
))
405 current_entry
= current_entry
->Flink
;
408 *ReqSize
= sizeof(SYSTEM_MODULE_INFORMATION
)+
409 (ModuleCount
- 1) * sizeof(SYSTEM_MODULE_INFORMATION_ENTRY
);
413 KeReleaseSpinLock(&ModuleListLock
, Irql
);
414 return(STATUS_INFO_LENGTH_MISMATCH
);
417 /* fill the buffer */
418 memset(Buffer
, '=', Size
);
420 Smi
= (PSYSTEM_MODULE_INFORMATION
)Buffer
;
421 Smi
->Count
= ModuleCount
;
424 current_entry
= ModuleListHead
.Flink
;
425 while (current_entry
!= (&ModuleListHead
))
427 current
= CONTAINING_RECORD(current_entry
,LDR_DATA_TABLE_ENTRY
,InLoadOrderModuleList
);
429 Smi
->Module
[ModuleCount
].Unknown1
= 0; /* Always 0 */
430 Smi
->Module
[ModuleCount
].Unknown2
= 0; /* Always 0 */
431 Smi
->Module
[ModuleCount
].Base
= current
->DllBase
;
432 Smi
->Module
[ModuleCount
].Size
= current
->SizeOfImage
;
433 Smi
->Module
[ModuleCount
].Flags
= 0; /* Flags ??? (GN) */
434 Smi
->Module
[ModuleCount
].Index
= (USHORT
)ModuleCount
;
435 Smi
->Module
[ModuleCount
].NameLength
= 0;
436 Smi
->Module
[ModuleCount
].LoadCount
= 0; /* FIXME */
439 AnsiName
.MaximumLength
= 256;
440 AnsiName
.Buffer
= Smi
->Module
[ModuleCount
].ImageName
;
441 RtlUnicodeStringToAnsiString(&AnsiName
,
442 ¤t
->FullDllName
,
445 p
= strrchr(AnsiName
.Buffer
, '\\');
448 Smi
->Module
[ModuleCount
].PathLength
= 0;
453 Smi
->Module
[ModuleCount
].PathLength
= p
- AnsiName
.Buffer
;
457 current_entry
= current_entry
->Flink
;
460 KeReleaseSpinLock(&ModuleListLock
, Irql
);
462 return(STATUS_SUCCESS
);
467 LdrpBuildModuleBaseName (
468 PUNICODE_STRING BaseName
,
469 PUNICODE_STRING FullName
)
473 DPRINT("LdrpBuildModuleBaseName()\n");
474 DPRINT("FullName %wZ\n", FullName
);
476 p
= wcsrchr(FullName
->Buffer
, L
'\\');
479 p
= FullName
->Buffer
;
488 RtlInitUnicodeString(BaseName
, p
);
493 LdrpCompareModuleNames (
494 IN PUNICODE_STRING String1
,
495 IN PUNICODE_STRING String2
)
501 if (String1
&& String2
)
503 /* Search String1 for last path component */
504 len1
= String1
->Length
/ sizeof(WCHAR
);
505 s1
= String1
->Buffer
;
506 for (i
= 0, p
= String1
->Buffer
; i
< String1
->Length
; i
= i
+ sizeof(WCHAR
), p
++)
510 if (i
== String1
->Length
- sizeof(WCHAR
))
518 len1
= (String1
->Length
- i
) / sizeof(WCHAR
);
523 /* Search String2 for last path component */
524 len2
= String2
->Length
/ sizeof(WCHAR
);
525 s2
= String2
->Buffer
;
526 for (i
= 0, p
= String2
->Buffer
; i
< String2
->Length
; i
= i
+ sizeof(WCHAR
), p
++)
530 if (i
== String2
->Length
- sizeof(WCHAR
))
538 len2
= (String2
->Length
- i
) / sizeof(WCHAR
);
543 /* Compare last path components */
548 c1
= len1
-- ? RtlUpcaseUnicodeChar (*s1
++) : 0;
549 c2
= len2
-- ? RtlUpcaseUnicodeChar (*s2
++) : 0;
550 if ((c1
== 0 && c2
== L
'.') || (c1
== L
'.' && c2
== 0))
552 if (!c1
|| !c2
|| c1
!= c2
)
561 PLDR_DATA_TABLE_ENTRY
563 LdrGetModuleObject ( PUNICODE_STRING ModuleName
)
565 PLDR_DATA_TABLE_ENTRY Module
;
569 DPRINT("LdrGetModuleObject(%wZ) called\n", ModuleName
);
571 KeAcquireSpinLock(&ModuleListLock
,&Irql
);
573 Entry
= ModuleListHead
.Flink
;
574 while (Entry
!= &ModuleListHead
)
576 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderModuleList
);
578 DPRINT("Comparing %wZ and %wZ\n",
579 &Module
->BaseDllName
,
582 if (!LdrpCompareModuleNames(&Module
->BaseDllName
, ModuleName
))
584 DPRINT("Module %wZ\n", &Module
->BaseDllName
);
585 KeReleaseSpinLock(&ModuleListLock
, Irql
);
589 Entry
= Entry
->Flink
;
592 KeReleaseSpinLock(&ModuleListLock
, Irql
);
594 DPRINT("Could not find module '%wZ'\n", ModuleName
);
600 /* ---------------------------------------------- PE Module support */
603 LdrLookupPageProtection (
606 PIMAGE_FILE_HEADER PEFileHeader
,
607 PIMAGE_SECTION_HEADER PESectionHeaders
)
609 BOOLEAN Write
= FALSE
;
610 BOOLEAN Execute
= FALSE
;
611 ULONG Characteristics
;
616 for (Idx
= 0; Idx
< PEFileHeader
->NumberOfSections
&& (!Write
|| !Execute
); Idx
++)
618 Characteristics
= PESectionHeaders
[Idx
].Characteristics
;
619 if (!(Characteristics
& IMAGE_SCN_TYPE_NOLOAD
))
621 Length
= max(PESectionHeaders
[Idx
].Misc
.VirtualSize
, PESectionHeaders
[Idx
].SizeOfRawData
);
622 BaseAddress
= PESectionHeaders
[Idx
].VirtualAddress
+ (char*)DriverBase
;
623 if (BaseAddress
< (PVOID
)((ULONG_PTR
)PageStart
+ PAGE_SIZE
) &&
624 PageStart
< (PVOID
)((ULONG_PTR
)BaseAddress
+ Length
))
626 if (Characteristics
& IMAGE_SCN_CNT_CODE
)
630 if (Characteristics
& (IMAGE_SCN_MEM_WRITE
|IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
637 if (Write
&& Execute
)
639 return PAGE_EXECUTE_READWRITE
;
643 return PAGE_EXECUTE_READ
;
647 return PAGE_READWRITE
;
651 return PAGE_READONLY
;
657 PVOID ModuleLoadBase
,
658 PUNICODE_STRING FileName
,
659 PLDR_DATA_TABLE_ENTRY
*ModuleObject
)
661 unsigned int DriverSize
, Idx
;
664 PIMAGE_DOS_HEADER PEDosHeader
;
665 PIMAGE_NT_HEADERS PENtHeaders
;
666 PIMAGE_SECTION_HEADER PESectionHeaders
;
667 PLDR_DATA_TABLE_ENTRY CreatedModuleObject
;
671 DPRINT("Processing PE Module at module base:%08lx\n", ModuleLoadBase
);
673 /* Get header pointers */
674 PEDosHeader
= (PIMAGE_DOS_HEADER
) ModuleLoadBase
;
675 PENtHeaders
= RtlImageNtHeader(ModuleLoadBase
);
676 PESectionHeaders
= IMAGE_FIRST_SECTION(PENtHeaders
);
679 /* Check file magic numbers */
680 if (PEDosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
682 CPRINT("Incorrect MZ magic: %04x\n", PEDosHeader
->e_magic
);
683 return STATUS_UNSUCCESSFUL
;
685 if (PEDosHeader
->e_lfanew
== 0)
687 CPRINT("Invalid lfanew offset: %08x\n", PEDosHeader
->e_lfanew
);
688 return STATUS_UNSUCCESSFUL
;
690 if (PENtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
692 CPRINT("Incorrect PE magic: %08x\n", PENtHeaders
->Signature
);
693 return STATUS_UNSUCCESSFUL
;
695 if (PENtHeaders
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
697 CPRINT("Incorrect Architechture: %04x\n", PENtHeaders
->FileHeader
.Machine
);
698 return STATUS_UNSUCCESSFUL
;
702 /* FIXME: if image is fixed-address load, then fail */
704 /* FIXME: check/verify OS version number */
706 DPRINT("OptionalHdrMagic:%04x LinkVersion:%d.%d\n",
707 PENtHeaders
->OptionalHeader
.Magic
,
708 PENtHeaders
->OptionalHeader
.MajorLinkerVersion
,
709 PENtHeaders
->OptionalHeader
.MinorLinkerVersion
);
710 DPRINT("Entry Point:%08lx\n", PENtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
712 /* Determine the size of the module */
714 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
716 if (!(PESectionHeaders
[Idx
].Characteristics
& IMAGE_SCN_TYPE_NOLOAD
))
718 CurrentSize
= PESectionHeaders
[Idx
].VirtualAddress
+ PESectionHeaders
[Idx
].Misc
.VirtualSize
;
719 DriverSize
= max(DriverSize
, CurrentSize
);
722 DriverSize
= ROUND_UP(DriverSize
, PENtHeaders
->OptionalHeader
.SectionAlignment
);
723 DPRINT("DriverSize %x, SizeOfImage %x\n",DriverSize
, PENtHeaders
->OptionalHeader
.SizeOfImage
);
725 /* Allocate a virtual section for the module */
727 DriverBase
= MmAllocateSection(DriverSize
, DriverBase
);
730 CPRINT("Failed to allocate a virtual section for driver\n");
731 return STATUS_UNSUCCESSFUL
;
733 DPRINT("DriverBase for %wZ: %x\n", FileName
, DriverBase
);
735 /* Copy headers over */
736 memcpy(DriverBase
, ModuleLoadBase
, PENtHeaders
->OptionalHeader
.SizeOfHeaders
);
738 /* Copy image sections into virtual section */
739 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
741 CurrentSize
= PESectionHeaders
[Idx
].VirtualAddress
+ PESectionHeaders
[Idx
].Misc
.VirtualSize
;
742 /* Copy current section into current offset of virtual section */
743 if (CurrentSize
<= DriverSize
&&
744 PESectionHeaders
[Idx
].SizeOfRawData
)
746 DPRINT("PESectionHeaders[Idx].VirtualAddress + DriverBase %x\n",
747 PESectionHeaders
[Idx
].VirtualAddress
+ (ULONG_PTR
)DriverBase
);
748 memcpy((PVOID
)((ULONG_PTR
)DriverBase
+ PESectionHeaders
[Idx
].VirtualAddress
),
749 (PVOID
)((ULONG_PTR
)ModuleLoadBase
+ PESectionHeaders
[Idx
].PointerToRawData
),
750 PESectionHeaders
[Idx
].Misc
.VirtualSize
> PESectionHeaders
[Idx
].SizeOfRawData
751 ? PESectionHeaders
[Idx
].SizeOfRawData
: PESectionHeaders
[Idx
].Misc
.VirtualSize
);
755 /* Perform relocation fixups */
756 Status
= LdrPEPerformRelocations(DriverBase
, DriverSize
);
757 if (!NT_SUCCESS(Status
))
759 // MmFreeSection(DriverBase);
763 /* Create the module */
764 CreatedModuleObject
= ExAllocatePoolWithTag (
765 NonPagedPool
, sizeof(LDR_DATA_TABLE_ENTRY
), TAG_MODULE_OBJECT
);
766 if (CreatedModuleObject
== NULL
)
768 // MmFreeSection(DriverBase);
769 return STATUS_INSUFFICIENT_RESOURCES
;
772 RtlZeroMemory(CreatedModuleObject
, sizeof(LDR_DATA_TABLE_ENTRY
));
774 /* Initialize ModuleObject data */
775 CreatedModuleObject
->DllBase
= DriverBase
;
777 CreatedModuleObject
->FullDllName
.Length
= 0;
778 CreatedModuleObject
->FullDllName
.MaximumLength
= FileName
->Length
+ sizeof(UNICODE_NULL
);
779 CreatedModuleObject
->FullDllName
.Buffer
=
780 ExAllocatePoolWithTag(PagedPool
, CreatedModuleObject
->FullDllName
.MaximumLength
, TAG_LDR_WSTR
);
781 if (CreatedModuleObject
->FullDllName
.Buffer
== NULL
)
783 ExFreePool(CreatedModuleObject
);
784 // MmFreeSection(DriverBase);
785 return STATUS_INSUFFICIENT_RESOURCES
;
788 RtlCopyUnicodeString(&CreatedModuleObject
->FullDllName
, FileName
);
789 CreatedModuleObject
->FullDllName
.Buffer
[FileName
->Length
/ sizeof(WCHAR
)] = 0;
790 LdrpBuildModuleBaseName(&CreatedModuleObject
->BaseDllName
,
791 &CreatedModuleObject
->FullDllName
);
793 CreatedModuleObject
->EntryPoint
=
794 (PVOID
)((ULONG_PTR
)DriverBase
+
795 PENtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
796 CreatedModuleObject
->SizeOfImage
= DriverSize
;
797 DPRINT("EntryPoint at %x\n", CreatedModuleObject
->EntryPoint
);
799 /* Perform import fixups */
800 Status
= LdrPEFixupImports(CreatedModuleObject
);
801 if (!NT_SUCCESS(Status
))
803 // MmFreeSection(DriverBase);
804 ExFreePool(CreatedModuleObject
->FullDllName
.Buffer
);
805 ExFreePool(CreatedModuleObject
);
809 MmSetPageProtect(NULL
, DriverBase
, PAGE_READONLY
);
810 /* Set the protections for the various parts of the driver */
811 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
813 ULONG Characteristics
= PESectionHeaders
[Idx
].Characteristics
;
818 Length
= PESectionHeaders
[Idx
].Misc
.VirtualSize
;
819 BaseAddress
= PESectionHeaders
[Idx
].VirtualAddress
+ (char*)DriverBase
;
820 PageAddress
= (PVOID
)PAGE_ROUND_DOWN(BaseAddress
);
822 Protect
= LdrLookupPageProtection(PageAddress
, DriverBase
, &PENtHeaders
->FileHeader
, PESectionHeaders
);
826 * This driver modifies a string in the first page of the text section while initialising.
828 if (0 == _wcsicmp(L
"fireport.sys", FileName
->Buffer
))
830 Protect
= PAGE_EXECUTE_READWRITE
;
833 if (PageAddress
< RVA(DriverBase
, DriverSize
))
835 MmSetPageProtect(NULL
, PageAddress
, Protect
);
838 if (Characteristics
& IMAGE_SCN_CNT_CODE
)
840 if (Characteristics
& IMAGE_SCN_MEM_WRITE
)
842 Protect
= PAGE_EXECUTE_READWRITE
;
846 Protect
= PAGE_EXECUTE_READ
;
849 else if (Characteristics
& (IMAGE_SCN_MEM_WRITE
|IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
851 Protect
= PAGE_READWRITE
;
855 Protect
= PAGE_READONLY
;
857 PageAddress
= (PVOID
)((ULONG_PTR
)PageAddress
+ PAGE_SIZE
);
858 while ((ULONG_PTR
)PageAddress
+ PAGE_SIZE
< (ULONG_PTR
)BaseAddress
+ Length
)
860 if (PageAddress
< RVA(DriverBase
, DriverSize
))
862 MmSetPageProtect(NULL
, PageAddress
, Protect
);
864 PageAddress
= (PVOID
)((ULONG_PTR
)PageAddress
+ PAGE_SIZE
);
866 if (PageAddress
< (PVOID
)((ULONG_PTR
)BaseAddress
+ Length
) &&
867 PageAddress
< RVA(DriverBase
, DriverSize
))
869 Protect
= LdrLookupPageProtection(PageAddress
, DriverBase
, &PENtHeaders
->FileHeader
, PESectionHeaders
);
870 MmSetPageProtect(NULL
, PageAddress
, Protect
);
875 KeAcquireSpinLock(&ModuleListLock
, &Irql
);
876 InsertTailList(&ModuleListHead
,
877 &CreatedModuleObject
->InLoadOrderModuleList
);
878 KeReleaseSpinLock(&ModuleListLock
, Irql
);
880 *ModuleObject
= CreatedModuleObject
;
882 DPRINT("Loading Module %wZ...\n", FileName
);
884 DPRINT("Module %wZ loaded at 0x%.08x.\n",
885 FileName
, CreatedModuleObject
->DllBase
);
887 return STATUS_SUCCESS
;
894 LdrSafePEProcessModule (
895 PVOID ModuleLoadBase
,
897 PVOID ImportModuleBase
,
902 PIMAGE_DOS_HEADER PEDosHeader
;
903 PIMAGE_NT_HEADERS PENtHeaders
;
904 PIMAGE_SECTION_HEADER PESectionHeaders
;
907 ps("Processing PE Module at module base:%08lx\n", ModuleLoadBase
);
909 /* Get header pointers */
910 PEDosHeader
= (PIMAGE_DOS_HEADER
) ModuleLoadBase
;
911 PENtHeaders
= RtlImageNtHeader(ModuleLoadBase
);
912 PESectionHeaders
= IMAGE_FIRST_SECTION(PENtHeaders
);
915 /* Check file magic numbers */
916 if (PEDosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
920 if (PEDosHeader
->e_lfanew
== 0)
924 if (PENtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
928 if (PENtHeaders
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
933 ps("OptionalHdrMagic:%04x LinkVersion:%d.%d\n",
934 PENtHeaders
->OptionalHeader
.Magic
,
935 PENtHeaders
->OptionalHeader
.MajorLinkerVersion
,
936 PENtHeaders
->OptionalHeader
.MinorLinkerVersion
);
937 ps("Entry Point:%08lx\n", PENtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
939 /* Determine the size of the module */
940 *DriverSize
= PENtHeaders
->OptionalHeader
.SizeOfImage
;
941 ps("DriverSize %x\n",*DriverSize
);
943 /* Copy headers over */
944 if (DriverBase
!= ModuleLoadBase
)
946 memcpy(DriverBase
, ModuleLoadBase
, PENtHeaders
->OptionalHeader
.SizeOfHeaders
);
949 ps("Hdr: 0x%X\n", PENtHeaders
);
950 ps("Hdr->SizeOfHeaders: 0x%X\n", PENtHeaders
->OptionalHeader
.SizeOfHeaders
);
951 ps("FileHdr->NumberOfSections: 0x%X\n", PENtHeaders
->FileHeader
.NumberOfSections
);
953 /* Ntoskrnl.exe need no relocation fixups since it is linked to run at the same
954 address as it is mapped */
955 if (DriverBase
!= ModuleLoadBase
)
959 /* Copy image sections into virtual section */
960 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
962 PIMAGE_SECTION_HEADER Section
= &PESectionHeaders
[Idx
];
963 // Copy current section into current offset of virtual section
964 if (Section
->SizeOfRawData
)
966 // ps("PESectionHeaders[Idx].VirtualAddress (%X) + DriverBase %x\n",
967 // PESectionHeaders[Idx].VirtualAddress, PESectionHeaders[Idx].VirtualAddress + DriverBase);
968 memcpy(Section
->VirtualAddress
+ (char*)DriverBase
,
969 Section
->PointerToRawData
+ (char*)ModuleLoadBase
,
970 Section
->Misc
.VirtualSize
> Section
->SizeOfRawData
? Section
->SizeOfRawData
: Section
->Misc
.VirtualSize
);
972 if (Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
)
974 memset(Section
->VirtualAddress
+ Section
->SizeOfRawData
+ (char*)DriverBase
,
976 Section
->Misc
.VirtualSize
- Section
->SizeOfRawData
);
978 CurrentSize
+= ROUND_UP(Section
->Misc
.VirtualSize
,
979 PENtHeaders
->OptionalHeader
.SectionAlignment
);
982 /* Perform relocation fixups */
983 Status
= LdrPEPerformRelocations(DriverBase
, *DriverSize
);
984 if (!NT_SUCCESS(Status
))
990 /* Perform import fixups */
991 Status
= LdrPEFixupImports(DriverBase
== ModuleLoadBase
? &NtoskrnlModuleObject
: &HalModuleObject
);
992 if (!NT_SUCCESS(Status
))
997 /* Set the page protection for the virtual sections */
998 MmSetPageProtect(NULL
, DriverBase
, PAGE_READONLY
);
999 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
1001 ULONG Characteristics
= PESectionHeaders
[Idx
].Characteristics
;
1006 Length
= PESectionHeaders
[Idx
].Misc
.VirtualSize
;
1007 BaseAddress
= PESectionHeaders
[Idx
].VirtualAddress
+ (char*)DriverBase
;
1008 PageAddress
= (PVOID
)PAGE_ROUND_DOWN(BaseAddress
);
1010 if (Characteristics
& IMAGE_SCN_MEM_EXECUTE
)
1012 if (Characteristics
& IMAGE_SCN_MEM_WRITE
)
1014 Protect
= PAGE_EXECUTE_READWRITE
;
1018 Protect
= PAGE_EXECUTE_READ
;
1021 else if (Characteristics
& IMAGE_SCN_MEM_WRITE
)
1023 Protect
= PAGE_READWRITE
;
1027 Protect
= PAGE_READONLY
;
1029 while ((ULONG_PTR
)PageAddress
< (ULONG_PTR
)BaseAddress
+ Length
)
1031 MmSetPageProtect(NULL
, PageAddress
, Protect
);
1032 PageAddress
= (PVOID
)((ULONG_PTR
)PageAddress
+ PAGE_SIZE
);
1034 if (DriverBase
== ModuleLoadBase
&&
1035 Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
1037 /* For ntoskrnl, we must stop after the bss section */
1047 LdrPEFixupForward ( PCHAR ForwardName
)
1049 CHAR NameBuffer
[128];
1050 UNICODE_STRING ModuleName
;
1052 PLDR_DATA_TABLE_ENTRY ModuleObject
;
1054 DPRINT("LdrPEFixupForward (%s)\n", ForwardName
);
1056 strcpy(NameBuffer
, ForwardName
);
1057 p
= strchr(NameBuffer
, '.');
1065 DPRINT("Driver: %s Function: %s\n", NameBuffer
, p
+1);
1067 RtlCreateUnicodeStringFromAsciiz(&ModuleName
,
1069 ModuleObject
= LdrGetModuleObject(&ModuleName
);
1070 RtlFreeUnicodeString(&ModuleName
);
1072 DPRINT("ModuleObject: %p\n", ModuleObject
);
1074 if (ModuleObject
== NULL
)
1076 CPRINT("LdrPEFixupForward: failed to find module %s\n", NameBuffer
);
1079 return LdrPEGetExportByName(ModuleObject
->DllBase
, (PUCHAR
)(p
+1), 0xffff);
1083 LdrPEPerformRelocations (
1087 PIMAGE_NT_HEADERS NtHeaders
;
1088 PIMAGE_DATA_DIRECTORY RelocationDDir
;
1089 PIMAGE_BASE_RELOCATION RelocationDir
, RelocationEnd
;
1091 PVOID Address
, MaxAddress
;
1099 NtHeaders
= RtlImageNtHeader(DriverBase
);
1101 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1103 return STATUS_UNSUCCESSFUL
;
1106 RelocationDDir
= &NtHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1108 if (RelocationDDir
->VirtualAddress
== 0 || RelocationDDir
->Size
== 0)
1110 return STATUS_SUCCESS
;
1113 Delta
= (ULONG_PTR
)DriverBase
- NtHeaders
->OptionalHeader
.ImageBase
;
1114 RelocationDir
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)DriverBase
+ RelocationDDir
->VirtualAddress
);
1115 RelocationEnd
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)RelocationDir
+ RelocationDDir
->Size
);
1116 MaxAddress
= RVA(DriverBase
, DriverSize
);
1118 while (RelocationDir
< RelocationEnd
&&
1119 RelocationDir
->SizeOfBlock
> 0)
1121 Count
= (RelocationDir
->SizeOfBlock
- sizeof(IMAGE_BASE_RELOCATION
)) / sizeof(USHORT
);
1122 Address
= RVA(DriverBase
, RelocationDir
->VirtualAddress
);
1123 TypeOffset
= (PUSHORT
)(RelocationDir
+ 1);
1125 for (i
= 0; i
< Count
; i
++)
1127 Offset
= *TypeOffset
& 0xFFF;
1128 Type
= *TypeOffset
>> 12;
1129 ShortPtr
= (PUSHORT
)(RVA(Address
, Offset
));
1131 /* Don't relocate after the end of the loaded driver */
1132 if ((PVOID
)ShortPtr
>= MaxAddress
)
1138 * Don't relocate within the relocation section itself.
1139 * GCC/LD generates sometimes relocation records for the relocation section.
1140 * This is a bug in GCC/LD.
1142 if ((ULONG_PTR
)ShortPtr
< (ULONG_PTR
)RelocationDir
||
1143 (ULONG_PTR
)ShortPtr
>= (ULONG_PTR
)RelocationEnd
)
1147 case IMAGE_REL_BASED_ABSOLUTE
:
1150 case IMAGE_REL_BASED_HIGH
:
1151 *ShortPtr
+= HIWORD(Delta
);
1154 case IMAGE_REL_BASED_LOW
:
1155 *ShortPtr
+= LOWORD(Delta
);
1158 case IMAGE_REL_BASED_HIGHLOW
:
1159 LongPtr
= (PULONG
)ShortPtr
;
1163 case IMAGE_REL_BASED_HIGHADJ
:
1164 case IMAGE_REL_BASED_MIPS_JMPADDR
:
1166 DPRINT1("Unknown/unsupported fixup type %hu.\n", Type
);
1167 DPRINT1("Address %x, Current %d, Count %d, *TypeOffset %x\n", Address
, i
, Count
, *TypeOffset
);
1168 return STATUS_UNSUCCESSFUL
;
1173 RelocationDir
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)RelocationDir
+ RelocationDir
->SizeOfBlock
);
1176 return STATUS_SUCCESS
;
1179 #define PATH_MAX 260
1183 LdrPEGetOrLoadModule (
1184 PLDR_DATA_TABLE_ENTRY Module
,
1186 PLDR_DATA_TABLE_ENTRY
* ImportedModule
)
1188 UNICODE_STRING DriverName
;
1189 UNICODE_STRING NameString
;
1190 WCHAR NameBuffer
[PATH_MAX
];
1191 NTSTATUS Status
= STATUS_SUCCESS
;
1193 if (0 == _stricmp(ImportedName
, "ntoskrnl") ||
1194 0 == _stricmp(ImportedName
, "ntoskrnl.exe"))
1196 *ImportedModule
= &NtoskrnlModuleObject
;
1197 return STATUS_SUCCESS
;
1200 if (0 == _stricmp(ImportedName
, "hal") ||
1201 0 == _stricmp(ImportedName
, "hal.dll"))
1203 *ImportedModule
= &HalModuleObject
;
1204 return STATUS_SUCCESS
;
1207 RtlCreateUnicodeStringFromAsciiz (&DriverName
, ImportedName
);
1208 DPRINT("Import module: %wZ\n", &DriverName
);
1210 *ImportedModule
= LdrGetModuleObject(&DriverName
);
1211 if (*ImportedModule
== NULL
)
1216 PathEnd
= wcsrchr(Module
->FullDllName
.Buffer
, L
'\\');
1217 if (NULL
!= PathEnd
)
1219 PathLength
= (PathEnd
- Module
->FullDllName
.Buffer
+ 1) * sizeof(WCHAR
);
1220 RtlCopyMemory(NameBuffer
, Module
->FullDllName
.Buffer
, PathLength
);
1221 RtlCopyMemory(NameBuffer
+ (PathLength
/ sizeof(WCHAR
)), DriverName
.Buffer
, DriverName
.Length
);
1222 NameString
.Buffer
= NameBuffer
;
1223 NameString
.MaximumLength
= NameString
.Length
= PathLength
+ DriverName
.Length
;
1225 /* NULL-terminate */
1226 NameString
.MaximumLength
++;
1227 NameBuffer
[NameString
.Length
/ sizeof(WCHAR
)] = 0;
1229 Status
= LdrLoadModule(&NameString
, ImportedModule
);
1233 DPRINT("Module '%wZ' not loaded yet\n", &DriverName
);
1234 wcscpy(NameBuffer
, L
"\\SystemRoot\\system32\\drivers\\");
1235 wcsncat(NameBuffer
, DriverName
.Buffer
, DriverName
.Length
/ sizeof(WCHAR
));
1236 RtlInitUnicodeString(&NameString
, NameBuffer
);
1237 Status
= LdrLoadModule(&NameString
, ImportedModule
);
1239 if (!NT_SUCCESS(Status
))
1241 wcscpy(NameBuffer
, L
"\\SystemRoot\\system32\\");
1242 wcsncat(NameBuffer
, DriverName
.Buffer
, DriverName
.Length
/ sizeof(WCHAR
));
1243 RtlInitUnicodeString(&NameString
, NameBuffer
);
1244 Status
= LdrLoadModule(&NameString
, ImportedModule
);
1245 if (!NT_SUCCESS(Status
))
1247 DPRINT1("Unknown import module: %wZ (Status %lx)\n", &DriverName
, Status
);
1251 RtlFreeUnicodeString(&DriverName
);
1256 LdrPEGetExportByName (
1261 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1262 PDWORD
* ExFunctions
;
1264 USHORT
* ExOrdinals
;
1268 LONG minn
, maxn
, mid
, res
;
1269 ULONG ExportDirSize
;
1271 DPRINT("LdrPEGetExportByName %x %s %hu\n", BaseAddress
, SymbolName
, Hint
);
1273 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)RtlImageDirectoryEntryToData(BaseAddress
,
1275 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1277 if (ExportDir
== NULL
)
1279 DPRINT1("LdrPEGetExportByName(): no export directory!\n");
1284 /* The symbol names may be missing entirely */
1285 if (ExportDir
->AddressOfNames
== 0)
1287 DPRINT("LdrPEGetExportByName(): symbol names missing entirely\n");
1292 * Get header pointers
1294 ExNames
= (PDWORD
*)RVA(BaseAddress
, ExportDir
->AddressOfNames
);
1295 ExOrdinals
= (USHORT
*)RVA(BaseAddress
, ExportDir
->AddressOfNameOrdinals
);
1296 ExFunctions
= (PDWORD
*)RVA(BaseAddress
, ExportDir
->AddressOfFunctions
);
1299 * Check the hint first
1301 if (Hint
< ExportDir
->NumberOfNames
)
1303 ExName
= RVA(BaseAddress
, ExNames
[Hint
]);
1304 if (strcmp(ExName
, (PCHAR
)SymbolName
) == 0)
1306 Ordinal
= ExOrdinals
[Hint
];
1307 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1308 if ((ULONG_PTR
)Function
>= (ULONG_PTR
)ExportDir
&&
1309 (ULONG_PTR
)Function
< (ULONG_PTR
)ExportDir
+ ExportDirSize
)
1311 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1312 Function
= LdrPEFixupForward((PCHAR
)Function
);
1313 if (Function
== NULL
)
1315 DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName
);
1319 if (Function
!= NULL
)
1330 maxn
= ExportDir
->NumberOfNames
- 1;
1331 while (minn
<= maxn
)
1333 mid
= (minn
+ maxn
) / 2;
1335 ExName
= RVA(BaseAddress
, ExNames
[mid
]);
1336 res
= strcmp(ExName
, (PCHAR
)SymbolName
);
1339 Ordinal
= ExOrdinals
[mid
];
1340 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1341 if ((ULONG_PTR
)Function
>= (ULONG_PTR
)ExportDir
&&
1342 (ULONG_PTR
)Function
< (ULONG_PTR
)ExportDir
+ ExportDirSize
)
1344 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1345 Function
= LdrPEFixupForward((PCHAR
)Function
);
1346 if (Function
== NULL
)
1348 DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName
);
1352 if (Function
!= NULL
)
1367 ExName
= RVA(BaseAddress
, ExNames
[mid
]);
1368 DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName
);
1373 LdrPEGetExportByOrdinal (
1377 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1378 ULONG ExportDirSize
;
1379 PDWORD
* ExFunctions
;
1382 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)RtlImageDirectoryEntryToData (
1385 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1388 ExFunctions
= (PDWORD
*)RVA(BaseAddress
,
1389 ExportDir
->AddressOfFunctions
);
1390 DPRINT("LdrPEGetExportByOrdinal(Ordinal %d) = %x\n",
1392 RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
]));
1394 Function
= 0 != ExFunctions
[Ordinal
- ExportDir
->Base
]
1395 ? RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
] )
1398 if (((ULONG_PTR
)Function
>= (ULONG_PTR
)ExportDir
) &&
1399 ((ULONG_PTR
)Function
< (ULONG_PTR
)ExportDir
+ ExportDirSize
))
1401 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1402 Function
= LdrPEFixupForward((PCHAR
)Function
);
1409 LdrPEProcessImportDirectoryEntry(
1411 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1412 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
)
1414 PVOID
* ImportAddressList
;
1415 PULONG FunctionNameList
;
1418 if (ImportModuleDirectory
== NULL
|| ImportModuleDirectory
->Name
== 0)
1420 return STATUS_UNSUCCESSFUL
;
1423 /* Get the import address list. */
1424 ImportAddressList
= (PVOID
*)RVA(DriverBase
, ImportModuleDirectory
->FirstThunk
);
1426 /* Get the list of functions to import. */
1427 if (ImportModuleDirectory
->OriginalFirstThunk
!= 0)
1429 FunctionNameList
= (PULONG
)RVA(DriverBase
, ImportModuleDirectory
->OriginalFirstThunk
);
1433 FunctionNameList
= (PULONG
)RVA(DriverBase
, ImportModuleDirectory
->FirstThunk
);
1436 /* Walk through function list and fixup addresses. */
1437 while (*FunctionNameList
!= 0L)
1439 if ((*FunctionNameList
) & 0x80000000)
1441 Ordinal
= (*FunctionNameList
) & 0x7fffffff;
1442 *ImportAddressList
= LdrPEGetExportByOrdinal(ImportedModule
->DllBase
, Ordinal
);
1443 if ((*ImportAddressList
) == NULL
)
1445 DPRINT1("Failed to import #%ld from %wZ\n", Ordinal
, &ImportedModule
->FullDllName
);
1446 return STATUS_UNSUCCESSFUL
;
1451 IMAGE_IMPORT_BY_NAME
*pe_name
;
1452 pe_name
= RVA(DriverBase
, *FunctionNameList
);
1453 *ImportAddressList
= LdrPEGetExportByName(ImportedModule
->DllBase
, pe_name
->Name
, pe_name
->Hint
);
1454 if ((*ImportAddressList
) == NULL
)
1456 DPRINT1("Failed to import %s from %wZ\n", pe_name
->Name
, &ImportedModule
->FullDllName
);
1457 return STATUS_UNSUCCESSFUL
;
1460 ImportAddressList
++;
1463 return STATUS_SUCCESS
;
1467 LdrPEFixupImports ( PLDR_DATA_TABLE_ENTRY Module
)
1469 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1471 PLDR_DATA_TABLE_ENTRY ImportedModule
;
1475 /* Process each import module */
1476 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1477 RtlImageDirectoryEntryToData(Module
->DllBase
,
1479 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1481 DPRINT("Processeing import directory at %p\n", ImportModuleDirectory
);
1482 while (ImportModuleDirectory
->Name
)
1484 if (Module
->SizeOfImage
<= ImportModuleDirectory
->Name
)
1486 DPRINT1("Invalid import directory in %wZ\n", &Module
->FullDllName
);
1487 return STATUS_SECTION_NOT_IMAGE
;
1490 /* Check to make sure that import lib is kernel */
1491 ImportedName
= (PCHAR
) Module
->DllBase
+ ImportModuleDirectory
->Name
;
1493 Status
= LdrPEGetOrLoadModule(Module
, ImportedName
, &ImportedModule
);
1494 if (!NT_SUCCESS(Status
))
1499 Status
= LdrPEProcessImportDirectoryEntry(Module
->DllBase
, ImportedModule
, ImportModuleDirectory
);
1500 if (!NT_SUCCESS(Status
))
1505 ImportModuleDirectory
++;
1507 return STATUS_SUCCESS
;