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
7 * PROGRAMMERS: Jean Michault
8 * Rex Jolliff (rex@lvcablemodem.com)
9 * Jason Filby (jasonfilby@yahoo.com)
10 * Casper S. Hornstrup (chorns@users.sourceforge.net)
13 * RJJ 10/12/98 Completed image loader function and added hooks for MZ/PE
14 * RJJ 10/12/98 Built driver loader function and added hooks for PE/COFF
15 * RJJ 10/12/98 Rolled in David's code to load COFF drivers
16 * JM 14/12/98 Built initial PE user module loader
17 * RJJ 06/03/99 Moved user PE loader into NTDLL
18 * JF 26/01/2000 Recoded some parts to retrieve export details correctly
19 * DW 27/06/2000 Removed redundant header files
20 * CSH 11/04/2001 Added automatic loading of module symbols if they exist
21 * KJK 02/04/2003 Nebbet-ized a couple of type names
25 /* INCLUDES *****************************************************************/
30 #include <internal/ntosdbg.h>
40 #include <internal/debug.h>
42 /* GLOBALS *******************************************************************/
44 LIST_ENTRY ModuleListHead
;
45 KSPIN_LOCK ModuleListLock
;
46 MODULE_OBJECT NtoskrnlModuleObject
;
47 MODULE_OBJECT HalModuleObject
;
49 LIST_ENTRY ModuleTextListHead
;
50 STATIC MODULE_TEXT_SECTION NtoskrnlTextSection
;
51 STATIC MODULE_TEXT_SECTION LdrHalTextSection
;
54 #define TAG_DRIVER_MEM TAG('D', 'R', 'V', 'M')
57 #define HIWORD(X) ((WORD) (((DWORD) (X) >> 16) & 0xFFFF))
60 #define LOWORD(X) ((WORD) (X))
63 /* FORWARD DECLARATIONS ******************************************************/
66 LdrProcessModule(PVOID ModuleLoadBase
,
67 PUNICODE_STRING ModuleName
,
68 PMODULE_OBJECT
*ModuleObject
);
71 LdrpBuildModuleBaseName(PUNICODE_STRING BaseName
,
72 PUNICODE_STRING FullName
);
75 LdrpCompareModuleNames(IN PUNICODE_STRING String1
,
76 IN PUNICODE_STRING String2
);
79 /* PE Driver load support */
80 static NTSTATUS
LdrPEProcessModule(PVOID ModuleLoadBase
,
81 PUNICODE_STRING FileName
,
82 PMODULE_OBJECT
*ModuleObject
);
84 LdrPEGetExportByName(PVOID BaseAddress
,
89 LdrPEFixupForward(PCHAR ForwardName
);
92 LdrPEPerformRelocations(PVOID DriverBase
,
96 LdrPEFixupImports(PMODULE_OBJECT Module
);
98 /* FUNCTIONS *****************************************************************/
101 LdrInitDebug(PLOADER_MODULE Module
, PWCH Name
)
103 PLIST_ENTRY current_entry
;
104 MODULE_TEXT_SECTION
* current
;
106 current_entry
= ModuleTextListHead
.Flink
;
107 while (current_entry
!= &ModuleTextListHead
)
110 CONTAINING_RECORD(current_entry
, MODULE_TEXT_SECTION
, ListEntry
);
111 if (wcscmp(current
->Name
, Name
) == 0)
115 current_entry
= current_entry
->Flink
;
118 if (current_entry
== &ModuleTextListHead
)
127 PIMAGE_NT_HEADERS NtHeader
;
128 PIMAGE_SECTION_HEADER SectionList
;
130 InitializeListHead(&ModuleTextListHead
);
132 /* Setup ntoskrnl.exe text section */
134 * This isn't the base of the text segment, but the start of the
135 * full image (in memory)
136 * Also, the Length field isn't set to the length of the segment,
137 * but is more like the offset, from the image base, to the end
140 NtHeader
= RtlImageNtHeader((PVOID
)KERNEL_BASE
);
141 SectionList
= IMAGE_FIRST_SECTION(NtHeader
);
142 NtoskrnlTextSection
.Base
= KERNEL_BASE
;
143 NtoskrnlTextSection
.Length
= SectionList
[0].Misc
.VirtualSize
+
144 SectionList
[0].VirtualAddress
;
145 NtoskrnlTextSection
.Name
= KERNEL_MODULE_NAME
;
146 NtoskrnlTextSection
.OptionalHeader
= OPTHDROFFSET(KERNEL_BASE
);
147 InsertTailList(&ModuleTextListHead
, &NtoskrnlTextSection
.ListEntry
);
149 /* Setup hal.dll text section */
150 /* Same comment as above applies */
151 NtHeader
= RtlImageNtHeader((PVOID
)LdrHalBase
);
152 SectionList
= IMAGE_FIRST_SECTION(NtHeader
);
153 LdrHalTextSection
.Base
= LdrHalBase
;
154 LdrHalTextSection
.Length
= SectionList
[0].Misc
.VirtualSize
+
155 SectionList
[0].VirtualAddress
;
156 LdrHalTextSection
.Name
= HAL_MODULE_NAME
;
157 LdrHalTextSection
.OptionalHeader
= OPTHDROFFSET(LdrHalBase
);
158 InsertTailList(&ModuleTextListHead
, &LdrHalTextSection
.ListEntry
);
160 /* Hook for KDB on initialization of the loader. */
161 KDB_LOADERINIT_HOOK(&NtoskrnlTextSection
, &LdrHalTextSection
);
165 LdrInitModuleManagement(VOID
)
167 PIMAGE_NT_HEADERS NtHeader
;
169 /* Initialize the module list and spinlock */
170 InitializeListHead(&ModuleListHead
);
171 KeInitializeSpinLock(&ModuleListLock
);
173 /* Initialize ModuleObject for NTOSKRNL */
174 RtlZeroMemory(&NtoskrnlModuleObject
, sizeof(MODULE_OBJECT
));
175 NtoskrnlModuleObject
.Base
= (PVOID
) KERNEL_BASE
;
176 NtoskrnlModuleObject
.Flags
= MODULE_FLAG_PE
;
177 RtlInitUnicodeString(&NtoskrnlModuleObject
.FullName
, KERNEL_MODULE_NAME
);
178 LdrpBuildModuleBaseName(&NtoskrnlModuleObject
.BaseName
, &NtoskrnlModuleObject
.FullName
);
180 NtHeader
= RtlImageNtHeader((PVOID
)KERNEL_BASE
);
181 NtoskrnlModuleObject
.Image
.PE
.FileHeader
= &NtHeader
->FileHeader
;
182 NtoskrnlModuleObject
.Image
.PE
.OptionalHeader
= &NtHeader
->OptionalHeader
;
183 NtoskrnlModuleObject
.Image
.PE
.SectionList
= IMAGE_FIRST_SECTION(NtHeader
);
184 NtoskrnlModuleObject
.EntryPoint
= (PVOID
) ((ULONG_PTR
) NtoskrnlModuleObject
.Base
+ NtHeader
->OptionalHeader
.AddressOfEntryPoint
);
185 DPRINT("ModuleObject:%08x entrypoint at %x\n", &NtoskrnlModuleObject
, NtoskrnlModuleObject
.EntryPoint
);
186 NtoskrnlModuleObject
.Length
= NtoskrnlModuleObject
.Image
.PE
.OptionalHeader
->SizeOfImage
;
187 NtoskrnlModuleObject
.TextSection
= &NtoskrnlTextSection
;
189 InsertTailList(&ModuleListHead
,
190 &NtoskrnlModuleObject
.ListEntry
);
192 /* Initialize ModuleObject for HAL */
193 RtlZeroMemory(&HalModuleObject
, sizeof(MODULE_OBJECT
));
194 HalModuleObject
.Base
= (PVOID
) LdrHalBase
;
195 HalModuleObject
.Flags
= MODULE_FLAG_PE
;
197 RtlInitUnicodeString(&HalModuleObject
.FullName
, HAL_MODULE_NAME
);
198 LdrpBuildModuleBaseName(&HalModuleObject
.BaseName
, &HalModuleObject
.FullName
);
200 NtHeader
= RtlImageNtHeader((PVOID
)LdrHalBase
);
201 HalModuleObject
.Image
.PE
.FileHeader
= &NtHeader
->FileHeader
;
202 HalModuleObject
.Image
.PE
.OptionalHeader
= &NtHeader
->OptionalHeader
;
203 HalModuleObject
.Image
.PE
.SectionList
= IMAGE_FIRST_SECTION(NtHeader
);
204 HalModuleObject
.EntryPoint
= (PVOID
) ((ULONG_PTR
) HalModuleObject
.Base
+ NtHeader
->OptionalHeader
.AddressOfEntryPoint
);
205 DPRINT("ModuleObject:%08x entrypoint at %x\n", &HalModuleObject
, HalModuleObject
.EntryPoint
);
206 HalModuleObject
.Length
= HalModuleObject
.Image
.PE
.OptionalHeader
->SizeOfImage
;
207 HalModuleObject
.TextSection
= &LdrHalTextSection
;
209 InsertTailList(&ModuleListHead
,
210 &HalModuleObject
.ListEntry
);
214 LdrpLoadImage(PUNICODE_STRING DriverName
,
216 PVOID
*SectionPointer
,
218 PVOID
*ExportSectionPointer
)
220 PMODULE_OBJECT ModuleObject
;
223 ModuleObject
= LdrGetModuleObject(DriverName
);
224 if (ModuleObject
== NULL
)
226 Status
= LdrLoadModule(DriverName
, &ModuleObject
);
227 if (!NT_SUCCESS(Status
))
234 *ModuleBase
= ModuleObject
->Base
;
236 // if (SectionPointer)
237 // *SectionPointer = ModuleObject->
240 *EntryPoint
= ModuleObject
->EntryPoint
;
242 // if (ExportSectionPointer)
243 // *ExportSectionPointer = ModuleObject->
245 return(STATUS_SUCCESS
);
250 LdrpUnloadImage(PVOID ModuleBase
)
252 return(STATUS_NOT_IMPLEMENTED
);
257 LdrpLoadAndCallImage(PUNICODE_STRING ModuleName
)
259 PDRIVER_INITIALIZE DriverEntry
;
260 PMODULE_OBJECT ModuleObject
;
263 ModuleObject
= LdrGetModuleObject(ModuleName
);
264 if (ModuleObject
!= NULL
)
266 return(STATUS_IMAGE_ALREADY_LOADED
);
269 Status
= LdrLoadModule(ModuleName
, &ModuleObject
);
270 if (!NT_SUCCESS(Status
))
275 DriverEntry
= (PDRIVER_INITIALIZE
)ModuleObject
->EntryPoint
;
277 Status
= DriverEntry(NULL
, NULL
);
278 if (!NT_SUCCESS(Status
))
280 LdrUnloadModule(ModuleObject
);
288 LdrLoadModule(PUNICODE_STRING Filename
,
289 PMODULE_OBJECT
*ModuleObject
)
291 PVOID ModuleLoadBase
;
294 OBJECT_ATTRIBUTES ObjectAttributes
;
295 PMODULE_OBJECT Module
;
296 FILE_STANDARD_INFORMATION FileStdInfo
;
297 IO_STATUS_BLOCK IoStatusBlock
;
299 *ModuleObject
= NULL
;
301 DPRINT("Loading Module %wZ...\n", Filename
);
303 /* Open the Module */
304 InitializeObjectAttributes(&ObjectAttributes
,
306 OBJ_CASE_INSENSITIVE
,
310 Status
= ZwOpenFile(&FileHandle
,
315 FILE_SYNCHRONOUS_IO_NONALERT
);
317 if (!NT_SUCCESS(Status
))
319 CPRINT("Could not open module file: %wZ\n", Filename
);
324 /* Get the size of the file */
325 Status
= ZwQueryInformationFile(FileHandle
,
329 FileStandardInformation
);
330 if (!NT_SUCCESS(Status
))
332 CPRINT("Could not get file size\n");
338 /* Allocate nonpageable memory for driver */
339 ModuleLoadBase
= ExAllocatePoolWithTag(NonPagedPool
,
340 FileStdInfo
.EndOfFile
.u
.LowPart
,
342 if (ModuleLoadBase
== NULL
)
344 CPRINT("Could not allocate memory for module");
346 return(STATUS_INSUFFICIENT_RESOURCES
);
350 /* Load driver into memory chunk */
351 Status
= ZwReadFile(FileHandle
,
355 FileStdInfo
.EndOfFile
.u
.LowPart
,
357 if (!NT_SUCCESS(Status
))
359 CPRINT("Could not read module file into memory");
360 ExFreePool(ModuleLoadBase
);
368 Status
= LdrProcessModule(ModuleLoadBase
,
371 if (!NT_SUCCESS(Status
))
373 CPRINT("Could not process module\n");
374 ExFreePool(ModuleLoadBase
);
379 ExFreePool(ModuleLoadBase
);
381 *ModuleObject
= Module
;
383 /* Hook for KDB on loading a driver. */
384 KDB_LOADDRIVER_HOOK(Filename
, Module
);
386 return(STATUS_SUCCESS
);
391 LdrUnloadModule(PMODULE_OBJECT ModuleObject
)
395 /* Remove the module from the module list */
396 KeAcquireSpinLock(&ModuleListLock
,&Irql
);
397 RemoveEntryList(&ModuleObject
->ListEntry
);
398 KeReleaseSpinLock(&ModuleListLock
, Irql
);
400 /* Hook for KDB on unloading a driver. */
401 KDB_UNLOADDRIVER_HOOK(ModuleObject
);
403 /* Free text section */
404 if (ModuleObject
->TextSection
!= NULL
)
406 ExFreePool(ModuleObject
->TextSection
->Name
);
407 RemoveEntryList(&ModuleObject
->TextSection
->ListEntry
);
408 ExFreePool(ModuleObject
->TextSection
);
409 ModuleObject
->TextSection
= NULL
;
412 /* Free module section */
413 // MmFreeSection(ModuleObject->Base);
415 ExFreePool(ModuleObject
->FullName
.Buffer
);
416 ExFreePool(ModuleObject
);
418 return(STATUS_SUCCESS
);
423 LdrProcessModule(PVOID ModuleLoadBase
,
424 PUNICODE_STRING ModuleName
,
425 PMODULE_OBJECT
*ModuleObject
)
427 PIMAGE_DOS_HEADER PEDosHeader
;
429 /* If MZ header exists */
430 PEDosHeader
= (PIMAGE_DOS_HEADER
) ModuleLoadBase
;
431 if (PEDosHeader
->e_magic
== IMAGE_DOS_SIGNATURE
&& PEDosHeader
->e_lfanew
!= 0L)
433 return LdrPEProcessModule(ModuleLoadBase
,
438 CPRINT("Module wasn't PE\n");
439 return STATUS_UNSUCCESSFUL
;
443 LdrpQueryModuleInformation(PVOID Buffer
,
447 PLIST_ENTRY current_entry
;
448 PMODULE_OBJECT current
;
449 ULONG ModuleCount
= 0;
450 PSYSTEM_MODULE_INFORMATION Smi
;
451 ANSI_STRING AnsiName
;
455 KeAcquireSpinLock(&ModuleListLock
,&Irql
);
457 /* calculate required size */
458 current_entry
= ModuleListHead
.Flink
;
459 while (current_entry
!= (&ModuleListHead
))
462 current_entry
= current_entry
->Flink
;
465 *ReqSize
= sizeof(SYSTEM_MODULE_INFORMATION
)+
466 (ModuleCount
- 1) * sizeof(SYSTEM_MODULE_INFORMATION_ENTRY
);
470 KeReleaseSpinLock(&ModuleListLock
, Irql
);
471 return(STATUS_INFO_LENGTH_MISMATCH
);
474 /* fill the buffer */
475 memset(Buffer
, '=', Size
);
477 Smi
= (PSYSTEM_MODULE_INFORMATION
)Buffer
;
478 Smi
->Count
= ModuleCount
;
481 current_entry
= ModuleListHead
.Flink
;
482 while (current_entry
!= (&ModuleListHead
))
484 current
= CONTAINING_RECORD(current_entry
,MODULE_OBJECT
,ListEntry
);
486 Smi
->Module
[ModuleCount
].Unknown1
= 0; /* Always 0 */
487 Smi
->Module
[ModuleCount
].Unknown2
= 0; /* Always 0 */
488 Smi
->Module
[ModuleCount
].Base
= current
->Base
;
489 Smi
->Module
[ModuleCount
].Size
= current
->Length
;
490 Smi
->Module
[ModuleCount
].Flags
= 0; /* Flags ??? (GN) */
491 Smi
->Module
[ModuleCount
].Index
= (USHORT
)ModuleCount
;
492 Smi
->Module
[ModuleCount
].NameLength
= 0;
493 Smi
->Module
[ModuleCount
].LoadCount
= 0; /* FIXME */
496 AnsiName
.MaximumLength
= 256;
497 AnsiName
.Buffer
= Smi
->Module
[ModuleCount
].ImageName
;
498 RtlUnicodeStringToAnsiString(&AnsiName
,
502 p
= strrchr(AnsiName
.Buffer
, '\\');
505 Smi
->Module
[ModuleCount
].PathLength
= 0;
510 Smi
->Module
[ModuleCount
].PathLength
= p
- AnsiName
.Buffer
;
514 current_entry
= current_entry
->Flink
;
517 KeReleaseSpinLock(&ModuleListLock
, Irql
);
519 return(STATUS_SUCCESS
);
524 LdrpBuildModuleBaseName(PUNICODE_STRING BaseName
,
525 PUNICODE_STRING FullName
)
529 DPRINT("LdrpBuildModuleBaseName()\n");
530 DPRINT("FullName %wZ\n", FullName
);
532 p
= wcsrchr(FullName
->Buffer
, L
'\\');
535 p
= FullName
->Buffer
;
544 RtlInitUnicodeString(BaseName
, p
);
549 LdrpCompareModuleNames(IN PUNICODE_STRING String1
,
550 IN PUNICODE_STRING String2
)
556 if (String1
&& String2
)
558 /* Search String1 for last path component */
559 len1
= String1
->Length
/ sizeof(WCHAR
);
560 s1
= String1
->Buffer
;
561 for (i
= 0, p
= String1
->Buffer
; i
< String1
->Length
; i
= i
+ sizeof(WCHAR
), p
++)
565 if (i
== String1
->Length
- sizeof(WCHAR
))
573 len1
= (String1
->Length
- i
) / sizeof(WCHAR
);
578 /* Search String2 for last path component */
579 len2
= String2
->Length
/ sizeof(WCHAR
);
580 s2
= String2
->Buffer
;
581 for (i
= 0, p
= String2
->Buffer
; i
< String2
->Length
; i
= i
+ sizeof(WCHAR
), p
++)
585 if (i
== String2
->Length
- sizeof(WCHAR
))
593 len2
= (String2
->Length
- i
) / sizeof(WCHAR
);
598 /* Compare last path components */
603 c1
= len1
-- ? RtlUpcaseUnicodeChar (*s1
++) : 0;
604 c2
= len2
-- ? RtlUpcaseUnicodeChar (*s2
++) : 0;
605 if ((c1
== 0 && c2
== L
'.') || (c1
== L
'.' && c2
== 0))
607 if (!c1
|| !c2
|| c1
!= c2
)
617 LdrGetModuleObject(PUNICODE_STRING ModuleName
)
619 PMODULE_OBJECT Module
;
623 DPRINT("LdrGetModuleObject(%wZ) called\n", ModuleName
);
625 KeAcquireSpinLock(&ModuleListLock
,&Irql
);
627 Entry
= ModuleListHead
.Flink
;
628 while (Entry
!= &ModuleListHead
)
630 Module
= CONTAINING_RECORD(Entry
, MODULE_OBJECT
, ListEntry
);
632 DPRINT("Comparing %wZ and %wZ\n",
636 if (!LdrpCompareModuleNames(&Module
->BaseName
, ModuleName
))
638 DPRINT("Module %wZ\n", &Module
->BaseName
);
639 KeReleaseSpinLock(&ModuleListLock
, Irql
);
643 Entry
= Entry
->Flink
;
646 KeReleaseSpinLock(&ModuleListLock
, Irql
);
648 DPRINT("Could not find module '%wZ'\n", ModuleName
);
654 /* ---------------------------------------------- PE Module support */
657 LdrLookupPageProtection(PVOID PageStart
,
659 PIMAGE_FILE_HEADER PEFileHeader
,
660 PIMAGE_SECTION_HEADER PESectionHeaders
)
662 BOOLEAN Write
= FALSE
;
663 BOOLEAN Execute
= FALSE
;
664 ULONG Characteristics
;
669 for (Idx
= 0; Idx
< PEFileHeader
->NumberOfSections
&& (!Write
|| !Execute
); Idx
++)
671 Characteristics
= PESectionHeaders
[Idx
].Characteristics
;
672 if (!(Characteristics
& IMAGE_SCN_TYPE_NOLOAD
))
674 Length
= max(PESectionHeaders
[Idx
].Misc
.VirtualSize
, PESectionHeaders
[Idx
].SizeOfRawData
);
675 BaseAddress
= PESectionHeaders
[Idx
].VirtualAddress
+ (char*)DriverBase
;
676 if (BaseAddress
< (PVOID
)((ULONG_PTR
)PageStart
+ PAGE_SIZE
) &&
677 PageStart
< (PVOID
)((ULONG_PTR
)BaseAddress
+ Length
))
679 if (Characteristics
& IMAGE_SCN_CNT_CODE
)
683 if (Characteristics
& (IMAGE_SCN_MEM_WRITE
|IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
690 if (Write
&& Execute
)
692 return PAGE_EXECUTE_READWRITE
;
696 return PAGE_EXECUTE_READ
;
700 return PAGE_READWRITE
;
704 return PAGE_READONLY
;
709 LdrPEProcessModule(PVOID ModuleLoadBase
,
710 PUNICODE_STRING FileName
,
711 PMODULE_OBJECT
*ModuleObject
)
713 unsigned int DriverSize
, Idx
;
716 PIMAGE_DOS_HEADER PEDosHeader
;
717 PIMAGE_NT_HEADERS PENtHeaders
;
718 PIMAGE_SECTION_HEADER PESectionHeaders
;
719 PMODULE_OBJECT CreatedModuleObject
;
720 MODULE_TEXT_SECTION
* ModuleTextSection
;
724 DPRINT("Processing PE Module at module base:%08lx\n", ModuleLoadBase
);
726 /* Get header pointers */
727 PEDosHeader
= (PIMAGE_DOS_HEADER
) ModuleLoadBase
;
728 PENtHeaders
= RtlImageNtHeader(ModuleLoadBase
);
729 PESectionHeaders
= IMAGE_FIRST_SECTION(PENtHeaders
);
732 /* Check file magic numbers */
733 if (PEDosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
735 CPRINT("Incorrect MZ magic: %04x\n", PEDosHeader
->e_magic
);
736 return STATUS_UNSUCCESSFUL
;
738 if (PEDosHeader
->e_lfanew
== 0)
740 CPRINT("Invalid lfanew offset: %08x\n", PEDosHeader
->e_lfanew
);
741 return STATUS_UNSUCCESSFUL
;
743 if (PENtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
745 CPRINT("Incorrect PE magic: %08x\n", PENtHeaders
->Signature
);
746 return STATUS_UNSUCCESSFUL
;
748 if (PENtHeaders
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
750 CPRINT("Incorrect Architechture: %04x\n", PENtHeaders
->FileHeader
.Machine
);
751 return STATUS_UNSUCCESSFUL
;
755 /* FIXME: if image is fixed-address load, then fail */
757 /* FIXME: check/verify OS version number */
759 DPRINT("OptionalHdrMagic:%04x LinkVersion:%d.%d\n",
760 PENtHeaders
->OptionalHeader
.Magic
,
761 PENtHeaders
->OptionalHeader
.MajorLinkerVersion
,
762 PENtHeaders
->OptionalHeader
.MinorLinkerVersion
);
763 DPRINT("Entry Point:%08lx\n", PENtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
765 /* Determine the size of the module */
767 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
769 if (!(PESectionHeaders
[Idx
].Characteristics
& IMAGE_SCN_TYPE_NOLOAD
))
771 CurrentSize
= PESectionHeaders
[Idx
].VirtualAddress
+ PESectionHeaders
[Idx
].Misc
.VirtualSize
;
772 DriverSize
= max(DriverSize
, CurrentSize
);
775 DriverSize
= ROUND_UP(DriverSize
, PENtHeaders
->OptionalHeader
.SectionAlignment
);
776 DPRINT("DriverSize %x, SizeOfImage %x\n",DriverSize
, PENtHeaders
->OptionalHeader
.SizeOfImage
);
778 /* Allocate a virtual section for the module */
780 DriverBase
= MmAllocateSection(DriverSize
, DriverBase
);
783 CPRINT("Failed to allocate a virtual section for driver\n");
784 return STATUS_UNSUCCESSFUL
;
786 DbgPrint("DriverBase for %wZ: %x\n", FileName
, DriverBase
);
788 /* Copy headers over */
789 memcpy(DriverBase
, ModuleLoadBase
, PENtHeaders
->OptionalHeader
.SizeOfHeaders
);
791 /* Copy image sections into virtual section */
792 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
794 CurrentSize
= PESectionHeaders
[Idx
].VirtualAddress
+ PESectionHeaders
[Idx
].Misc
.VirtualSize
;
795 /* Copy current section into current offset of virtual section */
796 if (CurrentSize
<= DriverSize
&&
797 PESectionHeaders
[Idx
].SizeOfRawData
)
799 DPRINT("PESectionHeaders[Idx].VirtualAddress + DriverBase %x\n",
800 PESectionHeaders
[Idx
].VirtualAddress
+ (ULONG_PTR
)DriverBase
);
801 memcpy((PVOID
)((ULONG_PTR
)DriverBase
+ PESectionHeaders
[Idx
].VirtualAddress
),
802 (PVOID
)((ULONG_PTR
)ModuleLoadBase
+ PESectionHeaders
[Idx
].PointerToRawData
),
803 PESectionHeaders
[Idx
].Misc
.VirtualSize
> PESectionHeaders
[Idx
].SizeOfRawData
804 ? PESectionHeaders
[Idx
].SizeOfRawData
: PESectionHeaders
[Idx
].Misc
.VirtualSize
);
808 /* Perform relocation fixups */
809 Status
= LdrPEPerformRelocations(DriverBase
, DriverSize
);
810 if (!NT_SUCCESS(Status
))
812 // MmFreeSection(DriverBase);
816 /* Create the module */
817 CreatedModuleObject
= ExAllocatePool(NonPagedPool
, sizeof(MODULE_OBJECT
));
818 if (CreatedModuleObject
== NULL
)
820 // MmFreeSection(DriverBase);
821 return STATUS_INSUFFICIENT_RESOURCES
;
824 RtlZeroMemory(CreatedModuleObject
, sizeof(MODULE_OBJECT
));
826 /* Initialize ModuleObject data */
827 CreatedModuleObject
->Base
= DriverBase
;
828 CreatedModuleObject
->Flags
= MODULE_FLAG_PE
;
830 CreatedModuleObject
->FullName
.Length
= 0;
831 CreatedModuleObject
->FullName
.MaximumLength
= FileName
->Length
+ sizeof(UNICODE_NULL
);
832 CreatedModuleObject
->FullName
.Buffer
= ExAllocatePool(PagedPool
, CreatedModuleObject
->FullName
.MaximumLength
);
833 if (CreatedModuleObject
->FullName
.Buffer
== NULL
)
835 ExFreePool(CreatedModuleObject
);
836 // MmFreeSection(DriverBase);
837 return STATUS_INSUFFICIENT_RESOURCES
;
840 RtlCopyUnicodeString(&CreatedModuleObject
->FullName
, FileName
);
841 LdrpBuildModuleBaseName(&CreatedModuleObject
->BaseName
,
842 &CreatedModuleObject
->FullName
);
844 CreatedModuleObject
->EntryPoint
=
845 (PVOID
)((ULONG_PTR
)DriverBase
+
846 PENtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
847 CreatedModuleObject
->Length
= DriverSize
;
848 DPRINT("EntryPoint at %x\n", CreatedModuleObject
->EntryPoint
);
850 CreatedModuleObject
->Image
.PE
.FileHeader
=
851 (PIMAGE_FILE_HEADER
) ((unsigned int) DriverBase
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
));
853 DPRINT("FileHeader at %x\n", CreatedModuleObject
->Image
.PE
.FileHeader
);
854 CreatedModuleObject
->Image
.PE
.OptionalHeader
=
855 (PIMAGE_OPTIONAL_HEADER
) ((unsigned int) DriverBase
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
) +
856 sizeof(IMAGE_FILE_HEADER
));
857 DPRINT("OptionalHeader at %x\n", CreatedModuleObject
->Image
.PE
.OptionalHeader
);
858 CreatedModuleObject
->Image
.PE
.SectionList
=
859 (PIMAGE_SECTION_HEADER
) ((unsigned int) DriverBase
+ PEDosHeader
->e_lfanew
+ sizeof(ULONG
) +
860 sizeof(IMAGE_FILE_HEADER
) + CreatedModuleObject
->Image
.PE
.FileHeader
->SizeOfOptionalHeader
);
861 DPRINT("SectionList at %x\n", CreatedModuleObject
->Image
.PE
.SectionList
);
863 /* Perform import fixups */
864 Status
= LdrPEFixupImports(CreatedModuleObject
);
865 if (!NT_SUCCESS(Status
))
867 // MmFreeSection(DriverBase);
868 ExFreePool(CreatedModuleObject
->FullName
.Buffer
);
869 ExFreePool(CreatedModuleObject
);
873 MmSetPageProtect(NULL
, DriverBase
, PAGE_READONLY
);
874 /* Set the protections for the various parts of the driver */
875 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
877 ULONG Characteristics
= PESectionHeaders
[Idx
].Characteristics
;
882 Length
= PESectionHeaders
[Idx
].Misc
.VirtualSize
;
883 BaseAddress
= PESectionHeaders
[Idx
].VirtualAddress
+ (char*)DriverBase
;
884 PageAddress
= (PVOID
)PAGE_ROUND_DOWN(BaseAddress
);
886 Protect
= LdrLookupPageProtection(PageAddress
, DriverBase
, &PENtHeaders
->FileHeader
, PESectionHeaders
);
890 * This driver modifies a string in the first page of the text section while initialising.
892 if (0 == _wcsicmp(L
"fireport.sys", FileName
->Buffer
))
894 Protect
= PAGE_EXECUTE_READWRITE
;
897 if (PageAddress
< DriverBase
+ DriverSize
)
899 MmSetPageProtect(NULL
, PageAddress
, Protect
);
902 if (Characteristics
& IMAGE_SCN_CNT_CODE
)
904 if (Characteristics
& IMAGE_SCN_MEM_WRITE
)
906 Protect
= PAGE_EXECUTE_READWRITE
;
910 Protect
= PAGE_EXECUTE_READ
;
913 else if (Characteristics
& (IMAGE_SCN_MEM_WRITE
|IMAGE_SCN_CNT_UNINITIALIZED_DATA
))
915 Protect
= PAGE_READWRITE
;
919 Protect
= PAGE_READONLY
;
921 PageAddress
= (PVOID
)((ULONG_PTR
)PageAddress
+ PAGE_SIZE
);
922 while ((ULONG_PTR
)PageAddress
+ PAGE_SIZE
< (ULONG_PTR
)BaseAddress
+ Length
)
924 if (PageAddress
< DriverBase
+ DriverSize
)
926 MmSetPageProtect(NULL
, PageAddress
, Protect
);
928 PageAddress
= (PVOID
)((ULONG_PTR
)PageAddress
+ PAGE_SIZE
);
930 if (PageAddress
< (PVOID
)((ULONG_PTR
)BaseAddress
+ Length
) &&
931 PageAddress
< DriverBase
+ DriverSize
)
933 Protect
= LdrLookupPageProtection(PageAddress
, DriverBase
, &PENtHeaders
->FileHeader
, PESectionHeaders
);
934 MmSetPageProtect(NULL
, PageAddress
, Protect
);
939 KeAcquireSpinLock(&ModuleListLock
, &Irql
);
940 InsertTailList(&ModuleListHead
,
941 &CreatedModuleObject
->ListEntry
);
942 KeReleaseSpinLock(&ModuleListLock
, Irql
);
945 ModuleTextSection
= ExAllocatePool(NonPagedPool
,
946 sizeof(MODULE_TEXT_SECTION
));
947 ASSERT(ModuleTextSection
);
948 RtlZeroMemory(ModuleTextSection
, sizeof(MODULE_TEXT_SECTION
));
949 ModuleTextSection
->Base
= (ULONG
)DriverBase
;
950 ModuleTextSection
->Length
= DriverSize
;
951 ModuleTextSection
->Name
= ExAllocatePool(NonPagedPool
,
952 (CreatedModuleObject
->BaseName
.Length
+ 1) * sizeof(WCHAR
));
953 RtlCopyMemory(ModuleTextSection
->Name
,
954 CreatedModuleObject
->BaseName
.Buffer
,
955 CreatedModuleObject
->BaseName
.Length
);
956 ModuleTextSection
->Name
[CreatedModuleObject
->BaseName
.Length
/ sizeof(WCHAR
)] = 0;
957 ModuleTextSection
->OptionalHeader
=
958 CreatedModuleObject
->Image
.PE
.OptionalHeader
;
959 InsertTailList(&ModuleTextListHead
, &ModuleTextSection
->ListEntry
);
961 CreatedModuleObject
->TextSection
= ModuleTextSection
;
963 *ModuleObject
= CreatedModuleObject
;
965 DPRINT("Loading Module %wZ...\n", FileName
);
967 if (KdDebuggerEnabled
&& (KdDebugState
& KD_DEBUG_GDB
))
969 DPRINT("Module %wZ loaded at 0x%.08x.\n",
970 FileName
, CreatedModuleObject
->Base
);
973 return STATUS_SUCCESS
;
978 LdrSafePEProcessModule(PVOID ModuleLoadBase
,
980 PVOID ImportModuleBase
,
985 PIMAGE_DOS_HEADER PEDosHeader
;
986 PIMAGE_NT_HEADERS PENtHeaders
;
987 PIMAGE_SECTION_HEADER PESectionHeaders
;
990 ps("Processing PE Module at module base:%08lx\n", ModuleLoadBase
);
992 /* Get header pointers */
993 PEDosHeader
= (PIMAGE_DOS_HEADER
) ModuleLoadBase
;
994 PENtHeaders
= RtlImageNtHeader(ModuleLoadBase
);
995 PESectionHeaders
= IMAGE_FIRST_SECTION(PENtHeaders
);
998 /* Check file magic numbers */
999 if (PEDosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
1003 if (PEDosHeader
->e_lfanew
== 0)
1007 if (PENtHeaders
->Signature
!= IMAGE_NT_SIGNATURE
)
1011 if (PENtHeaders
->FileHeader
.Machine
!= IMAGE_FILE_MACHINE_I386
)
1016 ps("OptionalHdrMagic:%04x LinkVersion:%d.%d\n",
1017 PENtHeaders
->OptionalHeader
.Magic
,
1018 PENtHeaders
->OptionalHeader
.MajorLinkerVersion
,
1019 PENtHeaders
->OptionalHeader
.MinorLinkerVersion
);
1020 ps("Entry Point:%08lx\n", PENtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
1022 /* Determine the size of the module */
1023 *DriverSize
= PENtHeaders
->OptionalHeader
.SizeOfImage
;
1024 ps("DriverSize %x\n",*DriverSize
);
1026 /* Copy headers over */
1027 if (DriverBase
!= ModuleLoadBase
)
1029 memcpy(DriverBase
, ModuleLoadBase
, PENtHeaders
->OptionalHeader
.SizeOfHeaders
);
1032 ps("Hdr: 0x%X\n", PENtHeaders
);
1033 ps("Hdr->SizeOfHeaders: 0x%X\n", PENtHeaders
->OptionalHeader
.SizeOfHeaders
);
1034 ps("FileHdr->NumberOfSections: 0x%X\n", PENtHeaders
->FileHeader
.NumberOfSections
);
1036 /* Ntoskrnl.exe need no relocation fixups since it is linked to run at the same
1037 address as it is mapped */
1038 if (DriverBase
!= ModuleLoadBase
)
1042 /* Copy image sections into virtual section */
1043 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
1045 PIMAGE_SECTION_HEADER Section
= &PESectionHeaders
[Idx
];
1046 // Copy current section into current offset of virtual section
1047 if (Section
->SizeOfRawData
)
1049 // ps("PESectionHeaders[Idx].VirtualAddress (%X) + DriverBase %x\n",
1050 // PESectionHeaders[Idx].VirtualAddress, PESectionHeaders[Idx].VirtualAddress + DriverBase);
1051 memcpy(Section
->VirtualAddress
+ (char*)DriverBase
,
1052 Section
->PointerToRawData
+ (char*)ModuleLoadBase
,
1053 Section
->Misc
.VirtualSize
> Section
->SizeOfRawData
? Section
->SizeOfRawData
: Section
->Misc
.VirtualSize
);
1055 if (Section
->SizeOfRawData
< Section
->Misc
.VirtualSize
)
1057 memset(Section
->VirtualAddress
+ Section
->SizeOfRawData
+ (char*)DriverBase
,
1059 Section
->Misc
.VirtualSize
- Section
->SizeOfRawData
);
1061 CurrentSize
+= ROUND_UP(Section
->Misc
.VirtualSize
,
1062 PENtHeaders
->OptionalHeader
.SectionAlignment
);
1065 /* Perform relocation fixups */
1066 Status
= LdrPEPerformRelocations(DriverBase
, *DriverSize
);
1067 if (!NT_SUCCESS(Status
))
1073 /* Perform import fixups */
1074 Status
= LdrPEFixupImports(DriverBase
== ModuleLoadBase
? &NtoskrnlModuleObject
: &HalModuleObject
);
1075 if (!NT_SUCCESS(Status
))
1080 /* Set the page protection for the virtual sections */
1081 MmSetPageProtect(NULL
, DriverBase
, PAGE_READONLY
);
1082 for (Idx
= 0; Idx
< PENtHeaders
->FileHeader
.NumberOfSections
; Idx
++)
1084 ULONG Characteristics
= PESectionHeaders
[Idx
].Characteristics
;
1089 Length
= PESectionHeaders
[Idx
].Misc
.VirtualSize
;
1090 BaseAddress
= PESectionHeaders
[Idx
].VirtualAddress
+ (char*)DriverBase
;
1091 PageAddress
= (PVOID
)PAGE_ROUND_DOWN(BaseAddress
);
1093 if (Characteristics
& IMAGE_SCN_MEM_EXECUTE
)
1095 if (Characteristics
& IMAGE_SCN_MEM_WRITE
)
1097 Protect
= PAGE_EXECUTE_READWRITE
;
1101 Protect
= PAGE_EXECUTE_READ
;
1104 else if (Characteristics
& IMAGE_SCN_MEM_WRITE
)
1106 Protect
= PAGE_READWRITE
;
1110 Protect
= PAGE_READONLY
;
1112 while ((ULONG_PTR
)PageAddress
< (ULONG_PTR
)BaseAddress
+ Length
)
1114 MmSetPageProtect(NULL
, PageAddress
, Protect
);
1115 PageAddress
= (PVOID
)((ULONG_PTR
)PageAddress
+ PAGE_SIZE
);
1117 if (DriverBase
== ModuleLoadBase
&&
1118 Characteristics
& IMAGE_SCN_CNT_UNINITIALIZED_DATA
)
1120 /* For ntoskrnl, we must stop after the bss section */
1130 LdrPEFixupForward(PCHAR ForwardName
)
1132 CHAR NameBuffer
[128];
1133 UNICODE_STRING ModuleName
;
1135 PMODULE_OBJECT ModuleObject
;
1137 DPRINT("LdrPEFixupForward (%s)\n", ForwardName
);
1139 strcpy(NameBuffer
, ForwardName
);
1140 p
= strchr(NameBuffer
, '.');
1148 DPRINT("Driver: %s Function: %s\n", NameBuffer
, p
+1);
1150 RtlCreateUnicodeStringFromAsciiz(&ModuleName
,
1152 ModuleObject
= LdrGetModuleObject(&ModuleName
);
1153 RtlFreeUnicodeString(&ModuleName
);
1155 DPRINT("ModuleObject: %p\n", ModuleObject
);
1157 if (ModuleObject
== NULL
)
1159 CPRINT("LdrPEFixupForward: failed to find module %s\n", NameBuffer
);
1162 return LdrPEGetExportByName(ModuleObject
->Base
, (PUCHAR
)(p
+1), 0xffff);
1166 LdrPEPerformRelocations(PVOID DriverBase
,
1169 PIMAGE_NT_HEADERS NtHeaders
;
1170 PIMAGE_DATA_DIRECTORY RelocationDDir
;
1171 PIMAGE_BASE_RELOCATION RelocationDir
, RelocationEnd
;
1173 PVOID Address
, MaxAddress
;
1181 NtHeaders
= RtlImageNtHeader(DriverBase
);
1183 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1185 return STATUS_UNSUCCESSFUL
;
1188 RelocationDDir
= &NtHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1190 if (RelocationDDir
->VirtualAddress
== 0 || RelocationDDir
->Size
== 0)
1192 return STATUS_SUCCESS
;
1195 Delta
= (ULONG_PTR
)DriverBase
- NtHeaders
->OptionalHeader
.ImageBase
;
1196 RelocationDir
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)DriverBase
+ RelocationDDir
->VirtualAddress
);
1197 RelocationEnd
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)RelocationDir
+ RelocationDDir
->Size
);
1198 MaxAddress
= DriverBase
+ DriverSize
;
1200 while (RelocationDir
< RelocationEnd
&&
1201 RelocationDir
->SizeOfBlock
> 0)
1203 Count
= (RelocationDir
->SizeOfBlock
- sizeof(IMAGE_BASE_RELOCATION
)) / sizeof(USHORT
);
1204 Address
= DriverBase
+ RelocationDir
->VirtualAddress
;
1205 TypeOffset
= (PUSHORT
)(RelocationDir
+ 1);
1207 for (i
= 0; i
< Count
; i
++)
1209 Offset
= *TypeOffset
& 0xFFF;
1210 Type
= *TypeOffset
>> 12;
1211 ShortPtr
= (PUSHORT
)(Address
+ Offset
);
1213 /* Don't relocate after the end of the loaded driver */
1214 if ((PVOID
)ShortPtr
>= MaxAddress
)
1220 * Don't relocate within the relocation section itself.
1221 * GCC/LD generates sometimes relocation records for the relecotion section.
1222 * This is a bug in GCC/LD.
1224 if ((ULONG_PTR
)ShortPtr
< (ULONG_PTR
)RelocationDir
||
1225 (ULONG_PTR
)ShortPtr
>= (ULONG_PTR
)RelocationEnd
)
1229 case IMAGE_REL_BASED_ABSOLUTE
:
1232 case IMAGE_REL_BASED_HIGH
:
1233 *ShortPtr
+= HIWORD(Delta
);
1236 case IMAGE_REL_BASED_LOW
:
1237 *ShortPtr
+= LOWORD(Delta
);
1240 case IMAGE_REL_BASED_HIGHLOW
:
1241 LongPtr
= (PULONG
)ShortPtr
;
1245 case IMAGE_REL_BASED_HIGHADJ
:
1246 case IMAGE_REL_BASED_MIPS_JMPADDR
:
1248 DPRINT1("Unknown/unsupported fixup type %hu.\n", Type
);
1249 DPRINT1("Address %x, Current %d, Count %d, *TypeOffset %x\n", Address
, i
, Count
, *TypeOffset
);
1250 return STATUS_UNSUCCESSFUL
;
1255 RelocationDir
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)RelocationDir
+ RelocationDir
->SizeOfBlock
);
1258 return STATUS_SUCCESS
;
1262 LdrPEGetOrLoadModule(PMODULE_OBJECT Module
,
1264 PMODULE_OBJECT
* ImportedModule
)
1266 UNICODE_STRING DriverName
;
1267 UNICODE_STRING NameString
;
1268 WCHAR NameBuffer
[PATH_MAX
];
1269 NTSTATUS Status
= STATUS_SUCCESS
;
1271 if (0 == _stricmp(ImportedName
, "ntoskrnl") ||
1272 0 == _stricmp(ImportedName
, "ntoskrnl.exe"))
1274 *ImportedModule
= &NtoskrnlModuleObject
;
1275 return STATUS_SUCCESS
;
1278 if (0 == _stricmp(ImportedName
, "hal") ||
1279 0 == _stricmp(ImportedName
, "hal.dll"))
1281 *ImportedModule
= &HalModuleObject
;
1282 return STATUS_SUCCESS
;
1285 RtlCreateUnicodeStringFromAsciiz (&DriverName
, ImportedName
);
1286 DPRINT("Import module: %wZ\n", &DriverName
);
1288 *ImportedModule
= LdrGetModuleObject(&DriverName
);
1289 if (*ImportedModule
== NULL
)
1294 PathEnd
= wcsrchr(Module
->FullName
.Buffer
, L
'\\');
1295 if (NULL
!= PathEnd
)
1297 PathLength
= (PathEnd
- Module
->FullName
.Buffer
+ 1) * sizeof(WCHAR
);
1298 RtlCopyMemory(NameBuffer
, Module
->FullName
.Buffer
, PathLength
);
1299 RtlCopyMemory(NameBuffer
+ (PathLength
/ sizeof(WCHAR
)), DriverName
.Buffer
, DriverName
.Length
);
1300 NameString
.Buffer
= NameBuffer
;
1301 NameString
.MaximumLength
= NameString
.Length
= PathLength
+ DriverName
.Length
;
1303 /* NULL-terminate */
1304 NameString
.MaximumLength
++;
1305 NameBuffer
[NameString
.Length
/ sizeof(WCHAR
)] = 0;
1307 Status
= LdrLoadModule(&NameString
, ImportedModule
);
1311 DPRINT("Module '%wZ' not loaded yet\n", &DriverName
);
1312 wcscpy(NameBuffer
, L
"\\SystemRoot\\system32\\drivers\\");
1313 wcsncat(NameBuffer
, DriverName
.Buffer
, DriverName
.Length
/ sizeof(WCHAR
));
1314 RtlInitUnicodeString(&NameString
, NameBuffer
);
1315 Status
= LdrLoadModule(&NameString
, ImportedModule
);
1317 if (!NT_SUCCESS(Status
))
1319 wcscpy(NameBuffer
, L
"\\SystemRoot\\system32\\");
1320 wcsncat(NameBuffer
, DriverName
.Buffer
, DriverName
.Length
/ sizeof(WCHAR
));
1321 RtlInitUnicodeString(&NameString
, NameBuffer
);
1322 Status
= LdrLoadModule(&NameString
, ImportedModule
);
1323 if (!NT_SUCCESS(Status
))
1325 DPRINT1("Unknown import module: %wZ (Status %lx)\n", &DriverName
, Status
);
1329 RtlFreeUnicodeString(&DriverName
);
1334 LdrPEGetExportByName(PVOID BaseAddress
,
1338 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1339 PDWORD
* ExFunctions
;
1341 USHORT
* ExOrdinals
;
1347 ULONG ExportDirSize
;
1349 DPRINT("LdrPEGetExportByName %x %s %hu\n", BaseAddress
, SymbolName
, Hint
);
1351 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)RtlImageDirectoryEntryToData(BaseAddress
,
1353 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1355 if (ExportDir
== NULL
)
1357 DPRINT1("LdrPEGetExportByName(): no export directory!\n");
1362 /* The symbol names may be missing entirely */
1363 if (ExportDir
->AddressOfNames
== 0)
1365 DPRINT("LdrPEGetExportByName(): symbol names missing entirely\n");
1370 * Get header pointers
1372 ExNames
= (PDWORD
*)RVA(BaseAddress
, ExportDir
->AddressOfNames
);
1373 ExOrdinals
= (USHORT
*)RVA(BaseAddress
, ExportDir
->AddressOfNameOrdinals
);
1374 ExFunctions
= (PDWORD
*)RVA(BaseAddress
, ExportDir
->AddressOfFunctions
);
1377 * Check the hint first
1379 if (Hint
< ExportDir
->NumberOfNames
)
1381 ExName
= RVA(BaseAddress
, ExNames
[Hint
]);
1382 if (strcmp(ExName
, (PCHAR
)SymbolName
) == 0)
1384 Ordinal
= ExOrdinals
[Hint
];
1385 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1386 if ((ULONG_PTR
)Function
>= (ULONG_PTR
)ExportDir
&&
1387 (ULONG_PTR
)Function
< (ULONG_PTR
)ExportDir
+ ExportDirSize
)
1389 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1390 Function
= LdrPEFixupForward((PCHAR
)Function
);
1391 if (Function
== NULL
)
1393 DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName
);
1397 if (Function
!= NULL
)
1405 * Try a binary search first
1408 maxn
= ExportDir
->NumberOfNames
- 1;
1409 while (minn
<= maxn
)
1414 mid
= (minn
+ maxn
) / 2;
1416 ExName
= RVA(BaseAddress
, ExNames
[mid
]);
1417 res
= strcmp(ExName
, (PCHAR
)SymbolName
);
1420 Ordinal
= ExOrdinals
[mid
];
1421 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1422 if ((ULONG_PTR
)Function
>= (ULONG_PTR
)ExportDir
&&
1423 (ULONG_PTR
)Function
< (ULONG_PTR
)ExportDir
+ ExportDirSize
)
1425 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1426 Function
= LdrPEFixupForward((PCHAR
)Function
);
1427 if (Function
== NULL
)
1429 DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName
);
1433 if (Function
!= NULL
)
1438 else if (minn
== maxn
)
1440 DPRINT("LdrPEGetExportByName(): binary search failed\n");
1454 * Fall back on a linear search
1456 DPRINT("LdrPEGetExportByName(): Falling back on a linear search of export table\n");
1457 for (i
= 0; i
< ExportDir
->NumberOfNames
; i
++)
1459 ExName
= RVA(BaseAddress
, ExNames
[i
]);
1460 if (strcmp(ExName
, (PCHAR
)SymbolName
) == 0)
1462 Ordinal
= ExOrdinals
[i
];
1463 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1464 DPRINT("%x %x %x\n", Function
, ExportDir
, ExportDir
+ ExportDirSize
);
1465 if ((ULONG_PTR
)Function
>= (ULONG_PTR
)ExportDir
&&
1466 (ULONG_PTR
)Function
< (ULONG_PTR
)ExportDir
+ ExportDirSize
)
1468 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1469 Function
= LdrPEFixupForward((PCHAR
)Function
);
1471 if (Function
== NULL
)
1478 DPRINT1("LdrPEGetExportByName(): failed to find %s\n",SymbolName
);
1483 LdrPEGetExportByOrdinal (PVOID BaseAddress
,
1486 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1487 ULONG ExportDirSize
;
1488 PDWORD
* ExFunctions
;
1491 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)RtlImageDirectoryEntryToData (BaseAddress
,
1493 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1496 ExFunctions
= (PDWORD
*)RVA(BaseAddress
,
1497 ExportDir
->AddressOfFunctions
);
1498 DPRINT("LdrPEGetExportByOrdinal(Ordinal %d) = %x\n",
1500 RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
]));
1502 Function
= 0 != ExFunctions
[Ordinal
- ExportDir
->Base
]
1503 ? RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
] )
1506 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1507 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1509 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1510 Function
= LdrPEFixupForward((PCHAR
)Function
);
1517 LdrPEProcessImportDirectoryEntry(PVOID DriverBase
,
1518 PMODULE_OBJECT ImportedModule
,
1519 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
)
1521 PVOID
* ImportAddressList
;
1522 PULONG FunctionNameList
;
1525 if (ImportModuleDirectory
== NULL
|| ImportModuleDirectory
->Name
== 0)
1527 return STATUS_UNSUCCESSFUL
;
1530 /* Get the import address list. */
1531 ImportAddressList
= (PVOID
*)(DriverBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1533 /* Get the list of functions to import. */
1534 if (ImportModuleDirectory
->OriginalFirstThunk
!= 0)
1536 FunctionNameList
= (PULONG
) (DriverBase
+ (ULONG_PTR
)ImportModuleDirectory
->OriginalFirstThunk
);
1540 FunctionNameList
= (PULONG
)(DriverBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1543 /* Walk through function list and fixup addresses. */
1544 while (*FunctionNameList
!= 0L)
1546 if ((*FunctionNameList
) & 0x80000000)
1548 Ordinal
= (*FunctionNameList
) & 0x7fffffff;
1549 *ImportAddressList
= LdrPEGetExportByOrdinal(ImportedModule
->Base
, Ordinal
);
1550 if ((*ImportAddressList
) == NULL
)
1552 DPRINT1("Failed to import #%ld from %wZ\n", Ordinal
, &ImportedModule
->FullName
);
1553 return STATUS_UNSUCCESSFUL
;
1558 IMAGE_IMPORT_BY_NAME
*pe_name
;
1559 pe_name
= RVA(DriverBase
, *FunctionNameList
);
1560 *ImportAddressList
= LdrPEGetExportByName(ImportedModule
->Base
, pe_name
->Name
, pe_name
->Hint
);
1561 if ((*ImportAddressList
) == NULL
)
1563 DPRINT1("Failed to import %s from %wZ\n", pe_name
->Name
, &ImportedModule
->FullName
);
1564 return STATUS_UNSUCCESSFUL
;
1567 ImportAddressList
++;
1570 return STATUS_SUCCESS
;
1574 LdrPEFixupImports(PMODULE_OBJECT Module
)
1576 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1578 PMODULE_OBJECT ImportedModule
;
1581 /* Process each import module */
1582 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1583 RtlImageDirectoryEntryToData(Module
->Base
,
1585 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1587 DPRINT("Processeing import directory at %p\n", ImportModuleDirectory
);
1588 while (ImportModuleDirectory
->Name
)
1590 /* Check to make sure that import lib is kernel */
1591 ImportedName
= (PCHAR
) Module
->Base
+ ImportModuleDirectory
->Name
;
1593 Status
= LdrPEGetOrLoadModule(Module
, ImportedName
, &ImportedModule
);
1594 if (!NT_SUCCESS(Status
))
1599 Status
= LdrPEProcessImportDirectoryEntry(Module
->Base
, ImportedModule
, ImportModuleDirectory
);
1600 if (!NT_SUCCESS(Status
))
1605 ImportModuleDirectory
++;
1607 return STATUS_SUCCESS
;