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
24 /* INCLUDES *****************************************************************/
27 #include <ddk/ntddk.h>
29 #include <internal/module.h>
30 #include <internal/ntoskrnl.h>
31 #include <internal/kd.h>
32 #include <internal/io.h>
33 #include <internal/mm.h>
34 #include <internal/ps.h>
35 #include <internal/ldr.h>
36 #include <internal/pool.h>
37 #include <internal/kd.h>
38 #include <ntos/minmax.h>
41 #include <internal/ntosdbg.h>
47 #include <internal/debug.h>
49 /* GLOBALS *******************************************************************/
51 LIST_ENTRY ModuleListHead;
52 KSPIN_LOCK ModuleListLock;
54 LIST_ENTRY ModuleTextListHead;
55 STATIC MODULE_TEXT_SECTION NtoskrnlTextSection;
56 STATIC MODULE_TEXT_SECTION LdrHalTextSection;
59 #define TAG_DRIVER_MEM TAG('D', 'R', 'V', 'M')
61 /* FORWARD DECLARATIONS ******************************************************/
64 LdrProcessModule(PVOID ModuleLoadBase,
65 PUNICODE_STRING ModuleName,
66 PMODULE_OBJECT *ModuleObject);
69 LdrGetExportAddress(PMODULE_OBJECT ModuleObject,
74 LdrpBuildModuleBaseName(PUNICODE_STRING BaseName,
75 PUNICODE_STRING FullName);
78 LdrpCompareModuleNames(IN PUNICODE_STRING String1,
79 IN PUNICODE_STRING String2);
82 /* PE Driver load support */
83 static NTSTATUS LdrPEProcessModule(PVOID ModuleLoadBase,
84 PUNICODE_STRING FileName,
85 PMODULE_OBJECT *ModuleObject);
87 LdrPEGetExportAddress(PMODULE_OBJECT ModuleObject,
92 LdrSafePEGetExportAddress(PVOID ImportModuleBase,
97 LdrPEFixupForward(PCHAR ForwardName);
100 /* FUNCTIONS *****************************************************************/
103 LdrInitDebug(PLOADER_MODULE Module, PWCH Name)
105 PLIST_ENTRY current_entry;
106 MODULE_TEXT_SECTION* current;
108 current_entry = ModuleTextListHead.Flink;
109 while (current_entry != &ModuleTextListHead)
112 CONTAINING_RECORD(current_entry, MODULE_TEXT_SECTION, ListEntry);
113 if (wcscmp(current->Name, Name) == 0)
117 current_entry = current_entry->Flink;
120 if (current_entry == &ModuleTextListHead)
129 PIMAGE_DOS_HEADER DosHeader;
130 PIMAGE_FILE_HEADER FileHeader;
131 PIMAGE_OPTIONAL_HEADER OptionalHeader;
132 PIMAGE_SECTION_HEADER SectionList;
134 InitializeListHead(&ModuleTextListHead);
136 /* Setup ntoskrnl.exe text section */
137 DosHeader = (PIMAGE_DOS_HEADER) KERNEL_BASE;
139 (PIMAGE_FILE_HEADER) ((DWORD)KERNEL_BASE +
140 DosHeader->e_lfanew + sizeof(ULONG));
141 OptionalHeader = (PIMAGE_OPTIONAL_HEADER)
142 ((DWORD)FileHeader + sizeof(IMAGE_FILE_HEADER));
143 SectionList = (PIMAGE_SECTION_HEADER)
144 ((DWORD)OptionalHeader + sizeof(IMAGE_OPTIONAL_HEADER));
145 NtoskrnlTextSection.Base = KERNEL_BASE;
146 NtoskrnlTextSection.Length = SectionList[0].Misc.VirtualSize +
147 SectionList[0].VirtualAddress;
148 NtoskrnlTextSection.Name = KERNEL_MODULE_NAME;
149 NtoskrnlTextSection.OptionalHeader = OptionalHeader;
150 InsertTailList(&ModuleTextListHead, &NtoskrnlTextSection.ListEntry);
152 /* Setup hal.dll text section */
153 DosHeader = (PIMAGE_DOS_HEADER)LdrHalBase;
155 (PIMAGE_FILE_HEADER) ((DWORD)LdrHalBase +
156 DosHeader->e_lfanew + sizeof(ULONG));
157 OptionalHeader = (PIMAGE_OPTIONAL_HEADER)
158 ((DWORD)FileHeader + sizeof(IMAGE_FILE_HEADER));
159 SectionList = (PIMAGE_SECTION_HEADER)
160 ((DWORD)OptionalHeader + sizeof(IMAGE_OPTIONAL_HEADER));
161 LdrHalTextSection.Base = LdrHalBase;
162 LdrHalTextSection.Length = SectionList[0].Misc.VirtualSize +
163 SectionList[0].VirtualAddress;
164 LdrHalTextSection.Name = HAL_MODULE_NAME;
165 LdrHalTextSection.OptionalHeader = OptionalHeader;
166 InsertTailList(&ModuleTextListHead, &LdrHalTextSection.ListEntry);
168 /* Hook for KDB on initialization of the loader. */
169 KDB_LOADERINIT_HOOK(&NtoskrnlTextSection, &LdrHalTextSection);
174 LdrInitModuleManagement(VOID)
176 PIMAGE_DOS_HEADER DosHeader;
177 PMODULE_OBJECT ModuleObject;
179 /* Initialize the module list and spinlock */
180 InitializeListHead(&ModuleListHead);
181 KeInitializeSpinLock(&ModuleListLock);
183 /* Create module object for NTOSKRNL */
184 ModuleObject = ExAllocatePool(NonPagedPool, sizeof(MODULE_OBJECT));
185 assert(ModuleObject != NULL);
186 RtlZeroMemory(ModuleObject, sizeof(MODULE_OBJECT));
188 /* Initialize ModuleObject data */
189 ModuleObject->Base = (PVOID) KERNEL_BASE;
190 ModuleObject->Flags = MODULE_FLAG_PE;
191 RtlCreateUnicodeString(&ModuleObject->FullName,
193 LdrpBuildModuleBaseName(&ModuleObject->BaseName,
194 &ModuleObject->FullName);
196 DosHeader = (PIMAGE_DOS_HEADER) KERNEL_BASE;
197 ModuleObject->Image.PE.FileHeader =
198 (PIMAGE_FILE_HEADER) ((DWORD) ModuleObject->Base +
199 DosHeader->e_lfanew + sizeof(ULONG));
200 ModuleObject->Image.PE.OptionalHeader = (PIMAGE_OPTIONAL_HEADER)
201 ((DWORD)ModuleObject->Image.PE.FileHeader + sizeof(IMAGE_FILE_HEADER));
202 ModuleObject->Image.PE.SectionList = (PIMAGE_SECTION_HEADER)
203 ((DWORD)ModuleObject->Image.PE.OptionalHeader + sizeof(IMAGE_OPTIONAL_HEADER));
204 ModuleObject->EntryPoint = (PVOID) ((DWORD) ModuleObject->Base +
205 ModuleObject->Image.PE.OptionalHeader->AddressOfEntryPoint);
206 DPRINT("ModuleObject:%08x entrypoint at %x\n", ModuleObject, ModuleObject->EntryPoint);
207 ModuleObject->Length = ModuleObject->Image.PE.OptionalHeader->SizeOfImage;
208 ModuleObject->TextSection = &NtoskrnlTextSection;
210 InsertTailList(&ModuleListHead,
211 &ModuleObject->ListEntry);
213 /* Create module object for HAL */
214 ModuleObject = ExAllocatePool(NonPagedPool, sizeof(MODULE_OBJECT));
215 assert(ModuleObject != NULL);
216 RtlZeroMemory(ModuleObject, sizeof(MODULE_OBJECT));
218 /* Initialize ModuleObject data */
219 ModuleObject->Base = (PVOID) LdrHalBase;
220 ModuleObject->Flags = MODULE_FLAG_PE;
222 RtlCreateUnicodeString(&ModuleObject->FullName,
224 LdrpBuildModuleBaseName(&ModuleObject->BaseName,
225 &ModuleObject->FullName);
227 DosHeader = (PIMAGE_DOS_HEADER) LdrHalBase;
228 ModuleObject->Image.PE.FileHeader =
229 (PIMAGE_FILE_HEADER) ((DWORD) ModuleObject->Base +
230 DosHeader->e_lfanew + sizeof(ULONG));
231 ModuleObject->Image.PE.OptionalHeader = (PIMAGE_OPTIONAL_HEADER)
232 ((DWORD)ModuleObject->Image.PE.FileHeader + sizeof(IMAGE_FILE_HEADER));
233 ModuleObject->Image.PE.SectionList = (PIMAGE_SECTION_HEADER)
234 ((DWORD)ModuleObject->Image.PE.OptionalHeader + sizeof(IMAGE_OPTIONAL_HEADER));
235 ModuleObject->EntryPoint = (PVOID) ((DWORD) ModuleObject->Base +
236 ModuleObject->Image.PE.OptionalHeader->AddressOfEntryPoint);
237 DPRINT("ModuleObject:%08x entrypoint at %x\n", ModuleObject, ModuleObject->EntryPoint);
238 ModuleObject->Length = ModuleObject->Image.PE.OptionalHeader->SizeOfImage;
239 ModuleObject->TextSection = &LdrHalTextSection;
241 InsertTailList(&ModuleListHead,
242 &ModuleObject->ListEntry);
246 LdrpLoadImage(PUNICODE_STRING DriverName,
248 PVOID *SectionPointer,
250 PVOID *ExportSectionPointer)
252 PMODULE_OBJECT ModuleObject;
255 ModuleObject = LdrGetModuleObject(DriverName);
256 if (ModuleObject == NULL)
258 Status = LdrLoadModule(DriverName, &ModuleObject);
259 if (!NT_SUCCESS(Status))
266 *ModuleBase = ModuleObject->Base;
268 // if (SectionPointer)
269 // *SectionPointer = ModuleObject->
272 *EntryPoint = ModuleObject->EntryPoint;
274 // if (ExportSectionPointer)
275 // *ExportSectionPointer = ModuleObject->
277 return(STATUS_SUCCESS);
282 LdrpUnloadImage(PVOID ModuleBase)
284 return(STATUS_NOT_IMPLEMENTED);
289 LdrpLoadAndCallImage(PUNICODE_STRING ModuleName)
291 PDRIVER_INITIALIZE DriverEntry;
292 PMODULE_OBJECT ModuleObject;
295 ModuleObject = LdrGetModuleObject(ModuleName);
296 if (ModuleObject != NULL)
298 return(STATUS_IMAGE_ALREADY_LOADED);
301 Status = LdrLoadModule(ModuleName, &ModuleObject);
302 if (!NT_SUCCESS(Status))
307 DriverEntry = (PDRIVER_INITIALIZE)ModuleObject->EntryPoint;
309 Status = DriverEntry(NULL, NULL);
310 if (!NT_SUCCESS(Status))
312 LdrUnloadModule(ModuleObject);
320 LdrLoadModule(PUNICODE_STRING Filename,
321 PMODULE_OBJECT *ModuleObject)
323 PVOID ModuleLoadBase;
326 OBJECT_ATTRIBUTES ObjectAttributes;
327 PMODULE_OBJECT Module;
328 FILE_STANDARD_INFORMATION FileStdInfo;
329 IO_STATUS_BLOCK IoStatusBlock;
331 *ModuleObject = NULL;
333 DPRINT("Loading Module %wZ...\n", Filename);
335 /* Open the Module */
336 InitializeObjectAttributes(&ObjectAttributes,
342 Status = NtOpenFile(&FileHandle,
347 FILE_SYNCHRONOUS_IO_NONALERT);
349 if (!NT_SUCCESS(Status))
351 CPRINT("Could not open module file: %wZ\n", Filename);
356 /* Get the size of the file */
357 Status = NtQueryInformationFile(FileHandle,
361 FileStandardInformation);
362 if (!NT_SUCCESS(Status))
364 CPRINT("Could not get file size\n");
370 /* Allocate nonpageable memory for driver */
371 ModuleLoadBase = ExAllocatePoolWithTag(NonPagedPool,
372 FileStdInfo.EndOfFile.u.LowPart,
374 if (ModuleLoadBase == NULL)
376 CPRINT("Could not allocate memory for module");
378 return(STATUS_INSUFFICIENT_RESOURCES);
382 /* Load driver into memory chunk */
383 Status = NtReadFile(FileHandle,
387 FileStdInfo.EndOfFile.u.LowPart,
389 if (!NT_SUCCESS(Status))
391 CPRINT("Could not read module file into memory");
392 ExFreePool(ModuleLoadBase);
400 Status = LdrProcessModule(ModuleLoadBase,
403 if (!NT_SUCCESS(Status))
405 CPRINT("Could not process module");
406 ExFreePool(ModuleLoadBase);
411 ExFreePool(ModuleLoadBase);
413 *ModuleObject = Module;
415 /* Hook for KDB on loading a driver. */
416 KDB_LOADDRIVER_HOOK(Filename, Module);
418 return(STATUS_SUCCESS);
423 LdrUnloadModule(PMODULE_OBJECT ModuleObject)
427 /* Remove the module from the module list */
428 KeAcquireSpinLock(&ModuleListLock,&Irql);
429 RemoveEntryList(&ModuleObject->ListEntry);
430 KeReleaseSpinLock(&ModuleListLock, Irql);
432 /* Hook for KDB on unloading a driver. */
433 KDB_UNLOADDRIVER_HOOK(ModuleObject);
435 /* Free text section */
436 if (ModuleObject->TextSection != NULL)
438 ExFreePool(ModuleObject->TextSection->Name);
439 RemoveEntryList(&ModuleObject->TextSection->ListEntry);
440 ExFreePool(ModuleObject->TextSection);
441 ModuleObject->TextSection = NULL;
444 /* Free module section */
445 // MmFreeSection(ModuleObject->Base);
447 ExFreePool(ModuleObject);
449 return(STATUS_SUCCESS);
454 LdrInitializeBootStartDriver(PVOID ModuleLoadBase,
458 PMODULE_OBJECT ModuleObject;
459 UNICODE_STRING ModuleName;
460 PDEVICE_NODE DeviceNode;
463 WCHAR Buffer[MAX_PATH];
468 CHAR TextBuffer [256];
471 HalQueryDisplayParameters(&x, &y, &cx, &cy);
472 RtlFillMemory(TextBuffer, x, ' ');
473 TextBuffer[x] = '\0';
474 HalSetDisplayParameters(0, y-1);
475 HalDisplayString(TextBuffer);
477 sprintf(TextBuffer, "Initializing %s...\n", FileName);
478 HalSetDisplayParameters(0, y-1);
479 HalDisplayString(TextBuffer);
480 HalSetDisplayParameters(cx, cy);
482 /* Split the filename into base name and extension */
483 FileExt = strrchr(FileName, '.');
485 Length = FileExt - FileName;
487 Length = strlen(FileName);
489 if ((FileExt != NULL) && (strcmp(FileExt, ".sym") == 0))
491 KDB_SYMBOLFILE_HOOK(ModuleLoadBase, FileName, Length);
492 return(STATUS_SUCCESS);
494 else if ((FileExt != NULL) && !(strcmp(FileExt, ".sys") == 0))
496 CPRINT("Ignoring non-driver file %s\n", FileName);
497 return STATUS_SUCCESS;
500 /* Use IopRootDeviceNode for now */
501 Status = IopCreateDeviceNode(IopRootDeviceNode, NULL, &DeviceNode);
502 if (!NT_SUCCESS(Status))
504 CPRINT("Driver load failed, status (%x)\n", Status);
508 RtlCreateUnicodeStringFromAsciiz(&ModuleName,
510 Status = LdrProcessModule(ModuleLoadBase,
513 RtlFreeUnicodeString(&ModuleName);
514 if (ModuleObject == NULL)
516 IopFreeDeviceNode(DeviceNode);
517 CPRINT("Driver load failed, status (%x)\n", Status);
518 return(STATUS_UNSUCCESSFUL);
522 /* Get the service name from the module name */
523 Start = wcsrchr(ModuleObject->BaseName.Buffer, L'\\');
525 Start = ModuleObject->BaseName.Buffer;
529 Ext = wcsrchr(ModuleObject->BaseName.Buffer, L'.');
531 Length = Ext - Start;
533 Length = wcslen(Start);
535 wcsncpy(Buffer, Start, Length);
536 RtlCreateUnicodeString(&DeviceNode->ServiceName, Buffer);
538 Status = IopInitializeDriver(ModuleObject->EntryPoint,
540 if (!NT_SUCCESS(Status))
542 IopFreeDeviceNode(DeviceNode);
543 CPRINT("Driver load failed, status (%x)\n", Status);
551 LdrProcessModule(PVOID ModuleLoadBase,
552 PUNICODE_STRING ModuleName,
553 PMODULE_OBJECT *ModuleObject)
555 PIMAGE_DOS_HEADER PEDosHeader;
557 /* If MZ header exists */
558 PEDosHeader = (PIMAGE_DOS_HEADER) ModuleLoadBase;
559 if (PEDosHeader->e_magic == IMAGE_DOS_MAGIC && PEDosHeader->e_lfanew != 0L)
561 return LdrPEProcessModule(ModuleLoadBase,
566 CPRINT("Module wasn't PE\n");
567 return STATUS_UNSUCCESSFUL;
572 LdrGetExportAddress(PMODULE_OBJECT ModuleObject,
576 if (ModuleObject->Flags & MODULE_FLAG_PE)
578 return LdrPEGetExportAddress(ModuleObject, Name, Hint);
588 LdrpQueryModuleInformation(PVOID Buffer,
592 PLIST_ENTRY current_entry;
593 PMODULE_OBJECT current;
594 ULONG ModuleCount = 0;
595 PSYSTEM_MODULE_INFORMATION Smi;
596 ANSI_STRING AnsiName;
600 KeAcquireSpinLock(&ModuleListLock,&Irql);
602 /* calculate required size */
603 current_entry = ModuleListHead.Flink;
604 while (current_entry != (&ModuleListHead))
607 current_entry = current_entry->Flink;
610 *ReqSize = sizeof(SYSTEM_MODULE_INFORMATION)+
611 (ModuleCount - 1) * sizeof(SYSTEM_MODULE_ENTRY);
615 KeReleaseSpinLock(&ModuleListLock, Irql);
616 return(STATUS_INFO_LENGTH_MISMATCH);
619 /* fill the buffer */
620 memset(Buffer, '=', Size);
622 Smi = (PSYSTEM_MODULE_INFORMATION)Buffer;
623 Smi->Count = ModuleCount;
626 current_entry = ModuleListHead.Flink;
627 while (current_entry != (&ModuleListHead))
629 current = CONTAINING_RECORD(current_entry,MODULE_OBJECT,ListEntry);
631 Smi->Module[ModuleCount].Unknown2 = 0; /* Always 0 */
632 Smi->Module[ModuleCount].BaseAddress = current->Base;
633 Smi->Module[ModuleCount].Size = current->Length;
634 Smi->Module[ModuleCount].Flags = 0; /* Flags ??? (GN) */
635 Smi->Module[ModuleCount].EntryIndex = ModuleCount;
638 AnsiName.MaximumLength = 256;
639 AnsiName.Buffer = Smi->Module[ModuleCount].Name;
640 RtlUnicodeStringToAnsiString(&AnsiName,
644 p = strrchr(AnsiName.Buffer, '\\');
647 Smi->Module[ModuleCount].PathLength = 0;
648 Smi->Module[ModuleCount].NameLength = strlen(AnsiName.Buffer);
653 Smi->Module[ModuleCount].PathLength = p - AnsiName.Buffer;
654 Smi->Module[ModuleCount].NameLength = strlen(p);
658 current_entry = current_entry->Flink;
661 KeReleaseSpinLock(&ModuleListLock, Irql);
663 return(STATUS_SUCCESS);
668 LdrpBuildModuleBaseName(PUNICODE_STRING BaseName,
669 PUNICODE_STRING FullName)
675 DPRINT("LdrpBuildModuleBaseName()\n");
676 DPRINT("FullName %wZ\n", FullName);
678 p = wcsrchr(FullName->Buffer, L'\\');
681 p = FullName->Buffer;
690 RtlCreateUnicodeString(&Name, p);
692 q = wcschr(Name.Buffer, L'.');
700 RtlCreateUnicodeString(BaseName, Name.Buffer);
701 RtlFreeUnicodeString(&Name);
706 LdrpCompareModuleNames(IN PUNICODE_STRING String1,
707 IN PUNICODE_STRING String2)
713 if (String1 && String2)
715 /* Search String1 for last path component */
716 len1 = String1->Length / sizeof(WCHAR);
717 s1 = String1->Buffer;
718 for (i = 0, p = String1->Buffer; i < String1->Length; i = i + sizeof(WCHAR), p++)
722 if (i == String1->Length - sizeof(WCHAR))
730 len1 = (String1->Length - i) / sizeof(WCHAR);
735 /* Search String2 for last path component */
736 len2 = String2->Length / sizeof(WCHAR);
737 s2 = String2->Buffer;
738 for (i = 0, p = String2->Buffer; i < String2->Length; i = i + sizeof(WCHAR), p++)
742 if (i == String2->Length - sizeof(WCHAR))
750 len2 = (String2->Length - i) / sizeof(WCHAR);
755 /* Compare last path components */
760 c1 = len1-- ? RtlUpcaseUnicodeChar (*s1++) : 0;
761 c2 = len2-- ? RtlUpcaseUnicodeChar (*s2++) : 0;
762 if ((c1 == 0 && c2 == L'.') || (c1 == L'.' && c2 == 0))
764 if (!c1 || !c2 || c1 != c2)
775 LdrGetModuleObject(PUNICODE_STRING ModuleName)
777 PMODULE_OBJECT Module;
781 DPRINT("LdrpGetModuleObject(%wZ) called\n", ModuleName);
783 KeAcquireSpinLock(&ModuleListLock,&Irql);
785 Entry = ModuleListHead.Flink;
786 while (Entry != &ModuleListHead)
788 Module = CONTAINING_RECORD(Entry, MODULE_OBJECT, ListEntry);
790 DPRINT("Comparing %wZ and %wZ\n",
794 if (!LdrpCompareModuleNames(&Module->BaseName, ModuleName))
796 DPRINT("Module %wZ\n", &Module->BaseName);
797 KeReleaseSpinLock(&ModuleListLock, Irql);
801 Entry = Entry->Flink;
804 KeReleaseSpinLock(&ModuleListLock, Irql);
806 DPRINT("Could not find module '%wZ'\n", ModuleName);
812 /* ---------------------------------------------- PE Module support */
815 LdrPEProcessModule(PVOID ModuleLoadBase,
816 PUNICODE_STRING FileName,
817 PMODULE_OBJECT *ModuleObject)
819 unsigned int DriverSize, Idx;
820 ULONG RelocDelta, NumRelocs;
821 DWORD CurrentSize, TotalRelocs;
824 PIMAGE_DOS_HEADER PEDosHeader;
825 PIMAGE_FILE_HEADER PEFileHeader;
826 PIMAGE_OPTIONAL_HEADER PEOptionalHeader;
827 PIMAGE_SECTION_HEADER PESectionHeaders;
828 PRELOCATION_DIRECTORY RelocDir;
829 PRELOCATION_ENTRY RelocEntry;
830 PMODULE_OBJECT LibraryModuleObject;
831 PMODULE_OBJECT CreatedModuleObject;
832 PVOID *ImportAddressList;
833 PULONG FunctionNameList;
836 UNICODE_STRING ModuleName;
837 UNICODE_STRING NameString;
838 WCHAR NameBuffer[60];
839 MODULE_TEXT_SECTION* ModuleTextSection;
843 DPRINT("Processing PE Module at module base:%08lx\n", ModuleLoadBase);
845 /* Get header pointers */
846 PEDosHeader = (PIMAGE_DOS_HEADER) ModuleLoadBase;
847 PEMagic = (PULONG) ((unsigned int) ModuleLoadBase +
848 PEDosHeader->e_lfanew);
849 PEFileHeader = (PIMAGE_FILE_HEADER) ((unsigned int) ModuleLoadBase +
850 PEDosHeader->e_lfanew + sizeof(ULONG));
851 PEOptionalHeader = (PIMAGE_OPTIONAL_HEADER) ((unsigned int) ModuleLoadBase +
852 PEDosHeader->e_lfanew + sizeof(ULONG) + sizeof(IMAGE_FILE_HEADER));
853 PESectionHeaders = (PIMAGE_SECTION_HEADER) ((unsigned int) ModuleLoadBase +
854 PEDosHeader->e_lfanew + sizeof(ULONG) + sizeof(IMAGE_FILE_HEADER) +
855 sizeof(IMAGE_OPTIONAL_HEADER));
858 /* Check file magic numbers */
859 if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC)
861 CPRINT("Incorrect MZ magic: %04x\n", PEDosHeader->e_magic);
862 return STATUS_UNSUCCESSFUL;
864 if (PEDosHeader->e_lfanew == 0)
866 CPRINT("Invalid lfanew offset: %08x\n", PEDosHeader->e_lfanew);
867 return STATUS_UNSUCCESSFUL;
869 if (*PEMagic != IMAGE_PE_MAGIC)
871 CPRINT("Incorrect PE magic: %08x\n", *PEMagic);
872 return STATUS_UNSUCCESSFUL;
874 if (PEFileHeader->Machine != IMAGE_FILE_MACHINE_I386)
876 CPRINT("Incorrect Architechture: %04x\n", PEFileHeader->Machine);
877 return STATUS_UNSUCCESSFUL;
881 /* FIXME: if image is fixed-address load, then fail */
883 /* FIXME: check/verify OS version number */
885 DPRINT("OptionalHdrMagic:%04x LinkVersion:%d.%d\n",
886 PEOptionalHeader->Magic,
887 PEOptionalHeader->MajorLinkerVersion,
888 PEOptionalHeader->MinorLinkerVersion);
889 DPRINT("Entry Point:%08lx\n", PEOptionalHeader->AddressOfEntryPoint);
892 /* Determine the size of the module */
893 DriverSize = PEOptionalHeader->SizeOfImage;
894 DPRINT("DriverSize %x\n",DriverSize);
896 /* Allocate a virtual section for the module */
897 DriverBase = MmAllocateSection(DriverSize);
900 CPRINT("Failed to allocate a virtual section for driver\n");
901 return STATUS_UNSUCCESSFUL;
903 DbgPrint("DriverBase for %wZ: %x\n", FileName, DriverBase);
905 /* Copy headers over */
906 memcpy(DriverBase, ModuleLoadBase, PEOptionalHeader->SizeOfHeaders);
908 /* Copy image sections into virtual section */
909 for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
911 // Copy current section into current offset of virtual section
912 if (PESectionHeaders[Idx].Characteristics &
913 (IMAGE_SECTION_CHAR_CODE | IMAGE_SECTION_CHAR_DATA))
915 DPRINT("PESectionHeaders[Idx].VirtualAddress + DriverBase %x\n",
916 PESectionHeaders[Idx].VirtualAddress + DriverBase);
917 memcpy(PESectionHeaders[Idx].VirtualAddress + DriverBase,
918 (PVOID)(ModuleLoadBase + PESectionHeaders[Idx].PointerToRawData),
919 PESectionHeaders[Idx].Misc.VirtualSize > PESectionHeaders[Idx].SizeOfRawData
920 ? PESectionHeaders[Idx].SizeOfRawData : PESectionHeaders[Idx].Misc.VirtualSize );
924 DPRINT("PESectionHeaders[Idx].VirtualAddress + DriverBase %x\n",
925 PESectionHeaders[Idx].VirtualAddress + DriverBase);
926 memset(PESectionHeaders[Idx].VirtualAddress + DriverBase,
927 '\0', PESectionHeaders[Idx].Misc.VirtualSize);
930 CurrentSize += ROUND_UP(PESectionHeaders[Idx].Misc.VirtualSize,
931 PEOptionalHeader->SectionAlignment);
934 // CurrentBase = (PVOID)((DWORD)CurrentBase +
935 // ROUND_UP(PESectionHeaders[Idx].SizeOfRawData.Misc.VirtualSize,
936 // PEOptionalHeader->SectionAlignment));
939 /* Perform relocation fixups */
940 RelocDelta = (DWORD) DriverBase - PEOptionalHeader->ImageBase;
941 RelocDir = (PRELOCATION_DIRECTORY)(PEOptionalHeader->DataDirectory[
942 IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
943 DPRINT("DrvrBase:%08lx ImgBase:%08lx RelocDelta:%08lx\n",
945 PEOptionalHeader->ImageBase,
947 DPRINT("RelocDir %x\n",RelocDir);
949 for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
951 if (PESectionHeaders[Idx].VirtualAddress == (DWORD)RelocDir)
953 DPRINT("Name %.8s PESectionHeader[Idx].PointerToRawData %x\n",
954 PESectionHeaders[Idx].Name,
955 PESectionHeaders[Idx].PointerToRawData);
956 RelocDir = PESectionHeaders[Idx].PointerToRawData +
958 CurrentSize = PESectionHeaders[Idx].Misc.VirtualSize;
963 RelocDir = RelocDir + (ULONG)DriverBase;
964 CurrentSize = PEOptionalHeader->DataDirectory
965 [IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;
967 DPRINT("RelocDir %08lx CurrentSize %08lx\n", RelocDir, CurrentSize);
969 while (TotalRelocs < CurrentSize && RelocDir->SizeOfBlock != 0)
971 NumRelocs = (RelocDir->SizeOfBlock - sizeof(RELOCATION_DIRECTORY)) /
973 /* DPRINT("RelocDir at %08lx for VA %08lx with %08lx relocs\n",
975 RelocDir->VirtualAddress,
977 RelocEntry = (PRELOCATION_ENTRY) ((DWORD)RelocDir +
978 sizeof(RELOCATION_DIRECTORY));
979 for (Idx = 0; Idx < NumRelocs; Idx++)
985 Offset = RelocEntry[Idx].TypeOffset & 0xfff;
986 Type = (RelocEntry[Idx].TypeOffset >> 12) & 0xf;
987 RelocItem = (PDWORD)(DriverBase + RelocDir->VirtualAddress +
989 /* DPRINT(" reloc at %08lx %x %s old:%08lx new:%08lx\n",
992 Type ? "HIGHLOW" : "ABS",
994 (*RelocItem) + RelocDelta); */
997 (*RelocItem) += RelocDelta;
1001 CPRINT("Unknown relocation type %x at %x\n",Type, &Type);
1002 return STATUS_UNSUCCESSFUL;
1005 TotalRelocs += RelocDir->SizeOfBlock;
1006 RelocDir = (PRELOCATION_DIRECTORY)((DWORD)RelocDir +
1007 RelocDir->SizeOfBlock);
1008 // DPRINT("TotalRelocs: %08lx CurrentSize: %08lx\n", TotalRelocs, CurrentSize);
1011 DPRINT("PEOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] %x\n",
1012 PEOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
1014 /* Perform import fixups */
1015 if (PEOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
1017 PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
1019 /* Process each import module */
1020 ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY)
1021 ((DWORD)DriverBase + PEOptionalHeader->
1022 DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
1023 DPRINT("Processeing import directory at %p\n", ImportModuleDirectory);
1024 while (ImportModuleDirectory->dwRVAModuleName)
1026 /* Check to make sure that import lib is kernel */
1027 pName = (PCHAR) DriverBase +
1028 ImportModuleDirectory->dwRVAModuleName;
1030 RtlCreateUnicodeStringFromAsciiz(&ModuleName, pName);
1031 DPRINT("Import module: %wZ\n", &ModuleName);
1033 LibraryModuleObject = LdrGetModuleObject(&ModuleName);
1034 if (LibraryModuleObject == NULL)
1036 CPRINT("Module '%wZ' not loaded yet\n", &ModuleName);
1037 wcscpy(NameBuffer, L"\\SystemRoot\\system32\\drivers\\");
1038 wcscat(NameBuffer, ModuleName.Buffer);
1039 RtlInitUnicodeString(&NameString, NameBuffer);
1040 Status = LdrLoadModule(&NameString, &LibraryModuleObject);
1041 if (!NT_SUCCESS(Status))
1043 CPRINT("Unknown import module: %wZ (Status %lx)\n", &ModuleName, Status);
1047 /* Get the import address list */
1048 ImportAddressList = (PVOID *) ((DWORD)DriverBase +
1049 ImportModuleDirectory->dwRVAFunctionAddressList);
1051 /* Get the list of functions to import */
1052 if (ImportModuleDirectory->dwRVAFunctionNameList != 0)
1054 FunctionNameList = (PULONG) ((DWORD)DriverBase +
1055 ImportModuleDirectory->dwRVAFunctionNameList);
1059 FunctionNameList = (PULONG) ((DWORD)DriverBase +
1060 ImportModuleDirectory->dwRVAFunctionAddressList);
1062 /* Walk through function list and fixup addresses */
1063 while (*FunctionNameList != 0L)
1065 if ((*FunctionNameList) & 0x80000000) // hint
1070 Hint = (*FunctionNameList) & 0xffff;
1074 pName = (PCHAR)((DWORD)DriverBase +
1075 *FunctionNameList + 2);
1076 Hint = *(PWORD)((DWORD)DriverBase + *FunctionNameList);
1078 DPRINT(" Hint:%04x Name:%s\n", Hint, pName);
1080 /* Fixup the current import symbol */
1081 if (LibraryModuleObject != NULL)
1083 *ImportAddressList = LdrGetExportAddress(LibraryModuleObject,
1089 CPRINT("Unresolved kernel symbol: %s\n", pName);
1090 return STATUS_UNSUCCESSFUL;
1092 ImportAddressList++;
1096 RtlFreeUnicodeString(&ModuleName);
1098 ImportModuleDirectory++;
1102 /* Set the protections for the various parts of the driver */
1103 for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
1105 ULONG Characteristics = PESectionHeaders[Idx].Characteristics;
1110 max(PESectionHeaders[Idx].Misc.VirtualSize,
1111 PESectionHeaders[Idx].SizeOfRawData);
1112 BaseAddress = PESectionHeaders[Idx].VirtualAddress + DriverBase;
1113 if (Characteristics & IMAGE_SECTION_CHAR_CODE &&
1114 !(Characteristics & IMAGE_SECTION_CHAR_WRITABLE ||
1115 Characteristics & IMAGE_SECTION_CHAR_DATA ||
1116 Characteristics & IMAGE_SECTION_CHAR_BSS))
1118 for (i = 0; i < PAGE_ROUND_DOWN(Length) / PAGE_SIZE; i++)
1120 MmSetPageProtect(NULL, BaseAddress + (i * PAGE_SIZE),
1126 /* Create the module */
1127 CreatedModuleObject = ExAllocatePool(NonPagedPool, sizeof(MODULE_OBJECT));
1128 if (CreatedModuleObject == NULL)
1130 return(STATUS_INSUFFICIENT_RESOURCES);
1133 RtlZeroMemory(CreatedModuleObject, sizeof(MODULE_OBJECT));
1135 /* Initialize ModuleObject data */
1136 CreatedModuleObject->Base = DriverBase;
1137 CreatedModuleObject->Flags = MODULE_FLAG_PE;
1139 RtlCreateUnicodeString(&CreatedModuleObject->FullName,
1141 LdrpBuildModuleBaseName(&CreatedModuleObject->BaseName,
1142 &CreatedModuleObject->FullName);
1144 CreatedModuleObject->EntryPoint =
1145 (PVOID)((DWORD)DriverBase +
1146 PEOptionalHeader->AddressOfEntryPoint);
1147 CreatedModuleObject->Length = DriverSize;
1148 DPRINT("EntryPoint at %x\n", CreatedModuleObject->EntryPoint);
1150 CreatedModuleObject->Image.PE.FileHeader =
1151 (PIMAGE_FILE_HEADER) ((unsigned int) DriverBase + PEDosHeader->e_lfanew + sizeof(ULONG));
1153 DPRINT("FileHeader at %x\n", CreatedModuleObject->Image.PE.FileHeader);
1154 CreatedModuleObject->Image.PE.OptionalHeader =
1155 (PIMAGE_OPTIONAL_HEADER) ((unsigned int) DriverBase + PEDosHeader->e_lfanew + sizeof(ULONG) +
1156 sizeof(IMAGE_FILE_HEADER));
1157 DPRINT("OptionalHeader at %x\n", CreatedModuleObject->Image.PE.OptionalHeader);
1158 CreatedModuleObject->Image.PE.SectionList =
1159 (PIMAGE_SECTION_HEADER) ((unsigned int) DriverBase + PEDosHeader->e_lfanew + sizeof(ULONG) +
1160 sizeof(IMAGE_FILE_HEADER) + sizeof(IMAGE_OPTIONAL_HEADER));
1161 DPRINT("SectionList at %x\n", CreatedModuleObject->Image.PE.SectionList);
1164 KeAcquireSpinLock(&ModuleListLock, &Irql);
1165 InsertTailList(&ModuleListHead,
1166 &CreatedModuleObject->ListEntry);
1167 KeReleaseSpinLock(&ModuleListLock, Irql);
1170 ModuleTextSection = ExAllocatePool(NonPagedPool,
1171 sizeof(MODULE_TEXT_SECTION));
1172 assert(ModuleTextSection);
1173 RtlZeroMemory(ModuleTextSection, sizeof(MODULE_TEXT_SECTION));
1174 ModuleTextSection->Base = (ULONG)DriverBase;
1175 ModuleTextSection->Length = DriverSize;
1176 ModuleTextSection->Name = ExAllocatePool(NonPagedPool,
1177 (wcslen(CreatedModuleObject->BaseName.Buffer) + 1) * sizeof(WCHAR));
1178 wcscpy(ModuleTextSection->Name, CreatedModuleObject->BaseName.Buffer);
1179 ModuleTextSection->OptionalHeader =
1180 CreatedModuleObject->Image.PE.OptionalHeader;
1181 InsertTailList(&ModuleTextListHead, &ModuleTextSection->ListEntry);
1183 CreatedModuleObject->TextSection = ModuleTextSection;
1185 *ModuleObject = CreatedModuleObject;
1187 DPRINT("Loading Module %wZ...\n", FileName);
1189 if ((KdDebuggerEnabled == TRUE) && (KdDebugState & KD_DEBUG_GDB))
1191 DPRINT("Module %wZ loaded at 0x%.08x.\n",
1192 FileName, CreatedModuleObject->Base);
1195 return STATUS_SUCCESS;
1200 LdrSafePEProcessModule(PVOID ModuleLoadBase,
1202 PVOID ImportModuleBase,
1206 ULONG RelocDelta, NumRelocs;
1207 ULONG CurrentSize, TotalRelocs;
1209 PIMAGE_DOS_HEADER PEDosHeader;
1210 PIMAGE_FILE_HEADER PEFileHeader;
1211 PIMAGE_OPTIONAL_HEADER PEOptionalHeader;
1212 PIMAGE_SECTION_HEADER PESectionHeaders;
1213 PRELOCATION_DIRECTORY RelocDir;
1214 PRELOCATION_ENTRY RelocEntry;
1215 PVOID *ImportAddressList;
1216 PULONG FunctionNameList;
1220 ps("Processing PE Module at module base:%08lx\n", ModuleLoadBase);
1222 /* Get header pointers */
1223 PEDosHeader = (PIMAGE_DOS_HEADER) ModuleLoadBase;
1224 PEMagic = (PULONG) ((unsigned int) ModuleLoadBase +
1225 PEDosHeader->e_lfanew);
1226 PEFileHeader = (PIMAGE_FILE_HEADER) ((unsigned int) ModuleLoadBase +
1227 PEDosHeader->e_lfanew + sizeof(ULONG));
1228 PEOptionalHeader = (PIMAGE_OPTIONAL_HEADER) ((unsigned int) ModuleLoadBase +
1229 PEDosHeader->e_lfanew + sizeof(ULONG) + sizeof(IMAGE_FILE_HEADER));
1230 PESectionHeaders = (PIMAGE_SECTION_HEADER) ((unsigned int) ModuleLoadBase +
1231 PEDosHeader->e_lfanew + sizeof(ULONG) + sizeof(IMAGE_FILE_HEADER) +
1232 sizeof(IMAGE_OPTIONAL_HEADER));
1235 /* Check file magic numbers */
1236 if (PEDosHeader->e_magic != IMAGE_DOS_MAGIC)
1240 if (PEDosHeader->e_lfanew == 0)
1244 if (*PEMagic != IMAGE_PE_MAGIC)
1248 if (PEFileHeader->Machine != IMAGE_FILE_MACHINE_I386)
1253 ps("OptionalHdrMagic:%04x LinkVersion:%d.%d\n",
1254 PEOptionalHeader->Magic,
1255 PEOptionalHeader->MajorLinkerVersion,
1256 PEOptionalHeader->MinorLinkerVersion);
1257 ps("Entry Point:%08lx\n", PEOptionalHeader->AddressOfEntryPoint);
1259 /* Determine the size of the module */
1260 *DriverSize = PEOptionalHeader->SizeOfImage;
1261 ps("DriverSize %x\n",*DriverSize);
1263 /* Copy headers over */
1264 if (DriverBase != ModuleLoadBase)
1266 memcpy(DriverBase, ModuleLoadBase, PEOptionalHeader->SizeOfHeaders);
1269 ps("Hdr: 0x%X\n", (ULONG)PEOptionalHeader);
1270 ps("Hdr->SizeOfHeaders: 0x%X\n", (ULONG)PEOptionalHeader->SizeOfHeaders);
1271 ps("FileHdr->NumberOfSections: 0x%X\n", (ULONG)PEFileHeader->NumberOfSections);
1273 /* Ntoskrnl.exe need no relocation fixups since it is linked to run at the same
1274 address as it is mapped */
1275 if (DriverBase != ModuleLoadBase)
1279 /* Copy image sections into virtual section */
1280 for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
1282 // Copy current section into current offset of virtual section
1283 if (PESectionHeaders[Idx].Characteristics &
1284 (IMAGE_SECTION_CHAR_CODE | IMAGE_SECTION_CHAR_DATA))
1286 //ps("PESectionHeaders[Idx].VirtualAddress (%X) + DriverBase %x\n",
1287 //PESectionHeaders[Idx].VirtualAddress, PESectionHeaders[Idx].VirtualAddress + DriverBase);
1288 memcpy(PESectionHeaders[Idx].VirtualAddress + DriverBase,
1289 (PVOID)(ModuleLoadBase + PESectionHeaders[Idx].PointerToRawData),
1290 PESectionHeaders[Idx].Misc.VirtualSize > PESectionHeaders[Idx].SizeOfRawData ?
1291 PESectionHeaders[Idx].SizeOfRawData : PESectionHeaders[Idx].Misc.VirtualSize );
1295 ps("PESectionHeaders[Idx].VirtualAddress (%X) + DriverBase %x\n",
1296 PESectionHeaders[Idx].VirtualAddress, PESectionHeaders[Idx].VirtualAddress + DriverBase);
1297 memset(PESectionHeaders[Idx].VirtualAddress + DriverBase,
1299 PESectionHeaders[Idx].Misc.VirtualSize);
1301 CurrentSize += ROUND_UP(PESectionHeaders[Idx].Misc.VirtualSize,
1302 PEOptionalHeader->SectionAlignment);
1305 /* Perform relocation fixups */
1306 RelocDelta = (ULONG) DriverBase - PEOptionalHeader->ImageBase;
1307 RelocDir = (PRELOCATION_DIRECTORY)(PEOptionalHeader->DataDirectory[
1308 IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress);
1309 ps("DrvrBase:%08lx ImgBase:%08lx RelocDelta:%08lx\n",
1311 PEOptionalHeader->ImageBase,
1313 ps("RelocDir %x\n",RelocDir);
1315 for (Idx = 0; Idx < PEFileHeader->NumberOfSections; Idx++)
1317 if (PESectionHeaders[Idx].VirtualAddress == (ULONG)RelocDir)
1319 DPRINT("Name %.8s PESectionHeader[Idx].PointerToRawData %x\n",
1320 PESectionHeaders[Idx].Name,
1321 PESectionHeaders[Idx].PointerToRawData);
1322 RelocDir = PESectionHeaders[Idx].PointerToRawData + ModuleLoadBase;
1323 CurrentSize = PESectionHeaders[Idx].Misc.VirtualSize;
1328 ps("RelocDir %08lx CurrentSize %08lx\n", RelocDir, CurrentSize);
1331 while (TotalRelocs < CurrentSize && RelocDir->SizeOfBlock != 0)
1333 NumRelocs = (RelocDir->SizeOfBlock - sizeof(RELOCATION_DIRECTORY)) /
1335 RelocEntry = (PRELOCATION_ENTRY)((ULONG)RelocDir +
1336 sizeof(RELOCATION_DIRECTORY));
1337 for (Idx = 0; Idx < NumRelocs; Idx++)
1343 Offset = RelocEntry[Idx].TypeOffset & 0xfff;
1344 Type = (RelocEntry[Idx].TypeOffset >> 12) & 0xf;
1345 RelocItem = (PULONG)(DriverBase + RelocDir->VirtualAddress + Offset);
1348 (*RelocItem) += RelocDelta;
1352 CPRINT("Unknown relocation type %x at %x\n",Type, &Type);
1356 TotalRelocs += RelocDir->SizeOfBlock;
1357 RelocDir = (PRELOCATION_DIRECTORY)((ULONG)RelocDir +
1358 RelocDir->SizeOfBlock);
1361 ps("PEOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT] %x\n",
1362 PEOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
1366 /* Perform import fixups */
1367 if (PEOptionalHeader->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress)
1369 PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory;
1371 /* Process each import module */
1372 ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY)
1373 ((ULONG)DriverBase + PEOptionalHeader->
1374 DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress);
1376 ps("Processeing import directory at %p\n", ImportModuleDirectory);
1378 /* Check to make sure that import lib is kernel */
1379 pName = (PCHAR)DriverBase + ImportModuleDirectory->dwRVAModuleName;
1381 ps("Import module: %s\n", pName);
1383 /* Get the import address list */
1384 ImportAddressList = (PVOID *)((ULONG)DriverBase +
1385 ImportModuleDirectory->dwRVAFunctionAddressList);
1387 ps(" ImportModuleDirectory->dwRVAFunctionAddressList: 0x%X\n",
1388 ImportModuleDirectory->dwRVAFunctionAddressList);
1389 ps(" ImportAddressList: 0x%X\n", ImportAddressList);
1391 /* Get the list of functions to import */
1392 if (ImportModuleDirectory->dwRVAFunctionNameList != 0)
1394 ps("Using function name list.\n");
1396 FunctionNameList = (PULONG)((ULONG)DriverBase +
1397 ImportModuleDirectory->dwRVAFunctionNameList);
1401 ps("Using function address list.\n");
1403 FunctionNameList = (PULONG)((ULONG)DriverBase +
1404 ImportModuleDirectory->dwRVAFunctionAddressList);
1407 /* Walk through function list and fixup addresses */
1408 while (*FunctionNameList != 0L)
1410 if ((*FunctionNameList) & 0x80000000)
1414 Hint = (*FunctionNameList) & 0xffff;
1419 pName = (PCHAR)((ULONG)DriverBase + *FunctionNameList + 2);
1420 Hint = *(PWORD)((ULONG)DriverBase + *FunctionNameList);
1422 //ps(" Hint:%04x Name:%s(0x%X)(%x)\n", Hint, pName, pName, ImportAddressList);
1424 *ImportAddressList = LdrSafePEGetExportAddress(ImportModuleBase,
1428 ImportAddressList++;
1433 ps("Finished importing.\n");
1440 LdrPEGetExportAddress(PMODULE_OBJECT ModuleObject,
1444 PIMAGE_EXPORT_DIRECTORY ExportDir;
1445 ULONG ExportDirSize;
1447 PVOID ExportAddress;
1449 PDWORD FunctionList, NameList;
1451 ExportDir = (PIMAGE_EXPORT_DIRECTORY)
1452 RtlImageDirectoryEntryToData(ModuleObject->Base,
1454 IMAGE_DIRECTORY_ENTRY_EXPORT,
1456 DPRINT("ExportDir %p ExportDirSize %lx\n", ExportDir, ExportDirSize);
1457 if (ExportDir == NULL)
1462 FunctionList = (PDWORD)((DWORD)ExportDir->AddressOfFunctions + ModuleObject->Base);
1463 NameList = (PDWORD)((DWORD)ExportDir->AddressOfNames + ModuleObject->Base);
1464 OrdinalList = (PWORD)((DWORD)ExportDir->AddressOfNameOrdinals + ModuleObject->Base);
1470 for (Idx = 0; Idx < ExportDir->NumberOfNames; Idx++)
1473 DPRINT(" Name:%s NameList[%d]:%s\n",
1476 (DWORD) ModuleObject->Base + NameList[Idx]);
1479 if (!strcmp(Name, (PCHAR) ((DWORD)ModuleObject->Base + NameList[Idx])))
1481 ExportAddress = (PVOID) ((DWORD)ModuleObject->Base +
1482 FunctionList[OrdinalList[Idx]]);
1483 if (((ULONG)ExportAddress >= (ULONG)ExportDir) &&
1484 ((ULONG)ExportAddress < (ULONG)ExportDir + ExportDirSize))
1486 DPRINT("Forward: %s\n", (PCHAR)ExportAddress);
1487 ExportAddress = LdrPEFixupForward((PCHAR)ExportAddress);
1488 DPRINT("ExportAddress: %p\n", ExportAddress);
1497 ExportAddress = (PVOID) ((DWORD)ModuleObject->Base +
1498 FunctionList[Hint - ExportDir->Base]);
1501 if (ExportAddress == NULL)
1503 CPRINT("Export not found for %d:%s\n",
1505 Name != NULL ? Name : "(Ordinal)");
1509 return(ExportAddress);
1514 LdrSafePEGetExportAddress(PVOID ImportModuleBase,
1519 PVOID ExportAddress;
1521 PDWORD FunctionList, NameList;
1522 PIMAGE_EXPORT_DIRECTORY ExportDir;
1523 ULONG ExportDirSize;
1525 static BOOLEAN EP = FALSE;
1527 ExportDir = (PIMAGE_EXPORT_DIRECTORY)
1528 RtlImageDirectoryEntryToData(ImportModuleBase,
1530 IMAGE_DIRECTORY_ENTRY_EXPORT,
1535 ps("ExportDir %x\n", ExportDir);
1538 FunctionList = (PDWORD)((DWORD)ExportDir->AddressOfFunctions + ImportModuleBase);
1539 NameList = (PDWORD)((DWORD)ExportDir->AddressOfNames + ImportModuleBase);
1540 OrdinalList = (PWORD)((DWORD)ExportDir->AddressOfNameOrdinals + ImportModuleBase);
1546 for (Idx = 0; Idx < ExportDir->NumberOfNames; Idx++)
1548 if (!strcmp(Name, (PCHAR) ((DWORD)ImportModuleBase + NameList[Idx])))
1550 ExportAddress = (PVOID) ((DWORD)ImportModuleBase +
1551 FunctionList[OrdinalList[Idx]]);
1558 ExportAddress = (PVOID) ((DWORD)ImportModuleBase +
1560 FunctionList[Hint - ExportDir->Base]);
1563 if (ExportAddress == 0)
1565 ps("Export not found for %d:%s\n",
1567 Name != NULL ? Name : "(Ordinal)");
1570 return ExportAddress;
1575 LdrPEFixupForward(PCHAR ForwardName)
1577 CHAR NameBuffer[128];
1578 UNICODE_STRING ModuleName;
1580 PMODULE_OBJECT ModuleObject;
1582 DPRINT("LdrPEFixupForward (%s)\n", ForwardName);
1584 strcpy(NameBuffer, ForwardName);
1585 p = strchr(NameBuffer, '.');
1593 DPRINT("Driver: %s Function: %s\n", NameBuffer, p+1);
1595 RtlCreateUnicodeStringFromAsciiz(&ModuleName,
1597 ModuleObject = LdrGetModuleObject(&ModuleName);
1598 RtlFreeUnicodeString(&ModuleName);
1600 DPRINT("ModuleObject: %p\n", ModuleObject);
1602 if (ModuleObject == NULL)
1604 CPRINT("LdrPEFixupForward: failed to find module %s\n", NameBuffer);
1608 return(LdrPEGetExportAddress(ModuleObject, p+1, 0));