3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: boot/freeldr/freeldr/windows/peloader.c
5 * PURPOSE: Provides routines for loading PE files.
6 * (Deprecated remark) To be merged with arch/i386/loader.c in future.
8 * PROGRAMMERS: Aleksey Bragin (aleksey@reactos.org)
10 * The source code in this file is based on the work of respective
11 * authors of PE loading code in ReactOS and Brian Palmer and
12 * Alex Ionescu's arch/i386/loader.c, and my research project
13 * (creating a native EFI loader for Windows).
15 * NOTE: This article was very handy during development:
16 * http://msdn.microsoft.com/msdnmag/issues/02/03/PE2/
19 /* INCLUDES ***************************************************************/
23 DBG_DEFAULT_CHANNEL(PELOADER
);
26 WinLdrpCompareDllName(IN PCH DllName
,
27 IN PUNICODE_STRING UnicodeName
);
30 WinLdrpBindImportName(IN OUT PLIST_ENTRY ModuleListHead
,
33 IN PIMAGE_THUNK_DATA ThunkData
,
34 IN PIMAGE_EXPORT_DIRECTORY ExportDirectory
,
36 IN BOOLEAN ProcessForwards
,
37 IN PCSTR DirectoryPath
);
40 WinLdrpLoadAndScanReferencedDll(PLIST_ENTRY ModuleListHead
,
43 PLDR_DATA_TABLE_ENTRY
*DataTableEntry
);
46 WinLdrpScanImportAddressTable(IN OUT PLIST_ENTRY ModuleListHead
,
49 IN PIMAGE_THUNK_DATA ThunkData
,
50 IN PCSTR DirectoryPath
);
54 /* FUNCTIONS **************************************************************/
56 /* Returns TRUE if DLL has already been loaded - looks in LoadOrderList in LPB */
58 WinLdrCheckForLoadedDll(IN OUT PLIST_ENTRY ModuleListHead
,
60 OUT PLDR_DATA_TABLE_ENTRY
*LoadedEntry
)
62 PLDR_DATA_TABLE_ENTRY DataTableEntry
;
63 LIST_ENTRY
*ModuleEntry
;
65 TRACE("WinLdrCheckForLoadedDll: DllName %s\n", DllName
);
67 /* Just go through each entry in the LoadOrderList and compare loaded module's
68 name with a given name */
69 ModuleEntry
= ModuleListHead
->Flink
;
70 while (ModuleEntry
!= ModuleListHead
)
72 /* Get pointer to the current DTE */
73 DataTableEntry
= CONTAINING_RECORD(ModuleEntry
,
77 TRACE("WinLdrCheckForLoadedDll: DTE %p, EP %p, base %p name '%.*ws'\n",
78 DataTableEntry
, DataTableEntry
->EntryPoint
, DataTableEntry
->DllBase
,
79 DataTableEntry
->BaseDllName
.Length
/ 2, VaToPa(DataTableEntry
->BaseDllName
.Buffer
));
82 if (WinLdrpCompareDllName(DllName
, &DataTableEntry
->BaseDllName
))
84 /* Yes, found it, report pointer to the loaded module's DTE
85 to the caller and increase load count for it */
86 *LoadedEntry
= DataTableEntry
;
87 DataTableEntry
->LoadCount
++;
88 TRACE("WinLdrCheckForLoadedDll: LoadedEntry %X\n", DataTableEntry
);
92 /* Go to the next entry */
93 ModuleEntry
= ModuleEntry
->Flink
;
101 WinLdrScanImportDescriptorTable(IN OUT PLIST_ENTRY ModuleListHead
,
102 IN PCCH DirectoryPath
,
103 IN PLDR_DATA_TABLE_ENTRY ScanDTE
)
105 PLDR_DATA_TABLE_ENTRY DataTableEntry
;
106 PIMAGE_IMPORT_DESCRIPTOR ImportTable
;
107 ULONG ImportTableSize
;
111 /* Get a pointer to the import table of this image */
112 ImportTable
= (PIMAGE_IMPORT_DESCRIPTOR
)RtlImageDirectoryEntryToData(VaToPa(ScanDTE
->DllBase
),
113 TRUE
, IMAGE_DIRECTORY_ENTRY_IMPORT
, &ImportTableSize
);
116 UNICODE_STRING BaseName
;
117 BaseName
.Buffer
= VaToPa(ScanDTE
->BaseDllName
.Buffer
);
118 BaseName
.MaximumLength
= ScanDTE
->BaseDllName
.MaximumLength
;
119 BaseName
.Length
= ScanDTE
->BaseDllName
.Length
;
120 TRACE("WinLdrScanImportDescriptorTable(): %wZ ImportTable = 0x%X\n",
121 &BaseName
, ImportTable
);
124 /* If image doesn't have any import directory - just return success */
125 if (ImportTable
== NULL
)
128 /* Loop through all entries */
129 for (;(ImportTable
->Name
!= 0) && (ImportTable
->FirstThunk
!= 0);ImportTable
++)
131 /* Get pointer to the name */
132 ImportName
= (PCH
)VaToPa(RVA(ScanDTE
->DllBase
, ImportTable
->Name
));
133 TRACE("WinLdrScanImportDescriptorTable(): Looking at %s\n", ImportName
);
135 /* In case we get a reference to ourselves - just skip it */
136 if (WinLdrpCompareDllName(ImportName
, &ScanDTE
->BaseDllName
))
139 /* Load the DLL if it is not already loaded */
140 if (!WinLdrCheckForLoadedDll(ModuleListHead
, ImportName
, &DataTableEntry
))
142 Success
= WinLdrpLoadAndScanReferencedDll(ModuleListHead
,
148 ERR("WinLdrpLoadAndScanReferencedDll() failed\n");
153 /* Scan its import address table */
154 Success
= WinLdrpScanImportAddressTable(ModuleListHead
,
155 DataTableEntry
->DllBase
,
157 (PIMAGE_THUNK_DATA
)RVA(ScanDTE
->DllBase
, ImportTable
->FirstThunk
),
162 ERR("WinLdrpScanImportAddressTable() failed: ImportName = '%s', DirectoryPath = '%s'\n",
163 ImportName
, DirectoryPath
);
172 WinLdrAllocateDataTableEntry(IN OUT PLIST_ENTRY ModuleListHead
,
176 OUT PLDR_DATA_TABLE_ENTRY
*NewEntry
)
178 PVOID BaseVA
= PaToVa(BasePA
);
180 PLDR_DATA_TABLE_ENTRY DataTableEntry
;
181 PIMAGE_NT_HEADERS NtHeaders
;
183 TRACE("WinLdrAllocateDataTableEntry(, '%s', '%s', %p)\n",
184 BaseDllName
, FullDllName
, BasePA
);
186 /* Allocate memory for a data table entry, zero-initialize it */
187 DataTableEntry
= (PLDR_DATA_TABLE_ENTRY
)FrLdrHeapAlloc(sizeof(LDR_DATA_TABLE_ENTRY
),
189 if (DataTableEntry
== NULL
)
191 RtlZeroMemory(DataTableEntry
, sizeof(LDR_DATA_TABLE_ENTRY
));
193 /* Get NT headers from the image */
194 NtHeaders
= RtlImageNtHeader(BasePA
);
196 /* Initialize corresponding fields of DTE based on NT headers value */
197 DataTableEntry
->DllBase
= BaseVA
;
198 DataTableEntry
->SizeOfImage
= NtHeaders
->OptionalHeader
.SizeOfImage
;
199 DataTableEntry
->EntryPoint
= RVA(BaseVA
, NtHeaders
->OptionalHeader
.AddressOfEntryPoint
);
200 DataTableEntry
->SectionPointer
= 0;
201 DataTableEntry
->CheckSum
= NtHeaders
->OptionalHeader
.CheckSum
;
203 /* Initialize BaseDllName field (UNICODE_STRING) from the Ansi BaseDllName
204 by simple conversion - copying each character */
205 Length
= (USHORT
)(strlen(BaseDllName
) * sizeof(WCHAR
));
206 Buffer
= (PWSTR
)FrLdrHeapAlloc(Length
, TAG_WLDR_NAME
);
209 FrLdrHeapFree(DataTableEntry
, TAG_WLDR_DTE
);
212 RtlZeroMemory(Buffer
, Length
);
214 DataTableEntry
->BaseDllName
.Length
= Length
;
215 DataTableEntry
->BaseDllName
.MaximumLength
= Length
;
216 DataTableEntry
->BaseDllName
.Buffer
= PaToVa(Buffer
);
217 while (*BaseDllName
!= 0)
219 *Buffer
++ = *BaseDllName
++;
222 /* Initialize FullDllName field (UNICODE_STRING) from the Ansi FullDllName
223 using the same method */
224 Length
= (USHORT
)(strlen(FullDllName
) * sizeof(WCHAR
));
225 Buffer
= (PWSTR
)FrLdrHeapAlloc(Length
, TAG_WLDR_NAME
);
228 FrLdrHeapFree(DataTableEntry
, TAG_WLDR_DTE
);
231 RtlZeroMemory(Buffer
, Length
);
233 DataTableEntry
->FullDllName
.Length
= Length
;
234 DataTableEntry
->FullDllName
.MaximumLength
= Length
;
235 DataTableEntry
->FullDllName
.Buffer
= PaToVa(Buffer
);
236 while (*FullDllName
!= 0)
238 *Buffer
++ = *FullDllName
++;
241 /* Initialize what's left - LoadCount which is 1, and set Flags so that
242 we know this entry is processed */
243 DataTableEntry
->Flags
= LDRP_ENTRY_PROCESSED
;
244 DataTableEntry
->LoadCount
= 1;
246 /* Insert this DTE to a list in the LPB */
247 InsertTailList(ModuleListHead
, &DataTableEntry
->InLoadOrderLinks
);
248 TRACE("Inserting DTE %p, name='%.*S' DllBase=%p \n", DataTableEntry
,
249 DataTableEntry
->BaseDllName
.Length
/ 2,
250 VaToPa(DataTableEntry
->BaseDllName
.Buffer
),
251 DataTableEntry
->DllBase
);
253 /* Save pointer to a newly allocated and initialized entry */
254 *NewEntry
= DataTableEntry
;
261 * WinLdrLoadImage loads the specified image from the file (it doesn't
262 * perform any additional operations on the filename, just directly
263 * calls the file I/O routines), and relocates it so that it's ready
264 * to be used when paging is enabled.
265 * Addressing mode: physical
268 WinLdrLoadImage(IN PCHAR FileName
,
269 TYPE_OF_MEMORY MemoryType
,
270 OUT PVOID
*ImageBasePA
)
274 PVOID VirtualBase
= NULL
;
275 UCHAR HeadersBuffer
[SECTOR_SIZE
* 2];
276 PIMAGE_NT_HEADERS NtHeaders
;
277 PIMAGE_SECTION_HEADER SectionHeader
;
278 ULONG VirtualSize
, SizeOfRawData
, NumberOfSections
;
280 LARGE_INTEGER Position
;
282 TRACE("WinLdrLoadImage(%s, %ld, *)\n", FileName
, MemoryType
);
284 /* Open the image file */
285 Status
= ArcOpen(FileName
, OpenReadOnly
, &FileId
);
286 if (Status
!= ESUCCESS
)
288 // UiMessageBox("Can not open the file.");
292 /* Load the first 2 sectors of the image so we can read the PE header */
293 Status
= ArcRead(FileId
, HeadersBuffer
, SECTOR_SIZE
* 2, &BytesRead
);
294 if (Status
!= ESUCCESS
)
296 UiMessageBox("Error reading from file.");
301 /* Now read the MZ header to get the offset to the PE Header */
302 NtHeaders
= RtlImageNtHeader(HeadersBuffer
);
305 // Print(L"Error - no NT header found in %s\n", FileName);
306 UiMessageBox("Error - no NT header found.");
311 /* Ensure this is executable image */
312 if (((NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_EXECUTABLE_IMAGE
) == 0))
314 // Print(L"Not an executable image %s\n", FileName);
315 UiMessageBox("Not an executable image.");
320 /* Store number of sections to read and a pointer to the first section */
321 NumberOfSections
= NtHeaders
->FileHeader
.NumberOfSections
;
322 SectionHeader
= IMAGE_FIRST_SECTION(NtHeaders
);
324 /* Try to allocate this memory, if fails - allocate somewhere else */
325 PhysicalBase
= MmAllocateMemoryAtAddress(NtHeaders
->OptionalHeader
.SizeOfImage
,
326 (PVOID
)((ULONG
)NtHeaders
->OptionalHeader
.ImageBase
& (KSEG0_BASE
- 1)),
329 if (PhysicalBase
== NULL
)
331 /* It's ok, we don't panic - let's allocate again at any other "low" place */
332 PhysicalBase
= MmAllocateMemoryWithType(NtHeaders
->OptionalHeader
.SizeOfImage
, MemoryType
);
334 if (PhysicalBase
== NULL
)
336 // Print(L"Failed to alloc pages for image %s\n", FileName);
337 UiMessageBox("Failed to alloc pages for image.");
343 /* This is the real image base - in form of a virtual address */
344 VirtualBase
= PaToVa(PhysicalBase
);
346 TRACE("Base PA: 0x%X, VA: 0x%X\n", PhysicalBase
, VirtualBase
);
348 /* Set to 0 position and fully load the file image */
349 Position
.HighPart
= Position
.LowPart
= 0;
350 Status
= ArcSeek(FileId
, &Position
, SeekAbsolute
);
351 if (Status
!= ESUCCESS
)
353 UiMessageBox("Error seeking to start of file.");
358 Status
= ArcRead(FileId
, PhysicalBase
, NtHeaders
->OptionalHeader
.SizeOfHeaders
, &BytesRead
);
359 if (Status
!= ESUCCESS
)
361 // Print(L"Error reading headers %s\n", FileName);
362 UiMessageBox("Error reading headers.");
367 /* Reload the NT Header */
368 NtHeaders
= RtlImageNtHeader(PhysicalBase
);
370 /* Load the first section */
371 SectionHeader
= IMAGE_FIRST_SECTION(NtHeaders
);
373 /* Fill output parameters */
374 *ImageBasePA
= PhysicalBase
;
376 /* Walk through each section and read it (check/fix any possible
377 bad situations, if they arise) */
378 for (i
= 0; i
< NumberOfSections
; i
++)
380 VirtualSize
= SectionHeader
->Misc
.VirtualSize
;
381 SizeOfRawData
= SectionHeader
->SizeOfRawData
;
383 /* Handle a case when VirtualSize equals 0 */
384 if (VirtualSize
== 0)
385 VirtualSize
= SizeOfRawData
;
387 /* If PointerToRawData is 0, then force its size to be also 0 */
388 if (SectionHeader
->PointerToRawData
== 0)
394 /* Cut the loaded size to the VirtualSize extents */
395 if (SizeOfRawData
> VirtualSize
)
396 SizeOfRawData
= VirtualSize
;
399 /* Actually read the section (if its size is not 0) */
400 if (SizeOfRawData
!= 0)
402 /* Seek to the correct position */
403 Position
.LowPart
= SectionHeader
->PointerToRawData
;
404 Status
= ArcSeek(FileId
, &Position
, SeekAbsolute
);
406 TRACE("SH->VA: 0x%X\n", SectionHeader
->VirtualAddress
);
408 /* Read this section from the file, size = SizeOfRawData */
409 Status
= ArcRead(FileId
, (PUCHAR
)PhysicalBase
+ SectionHeader
->VirtualAddress
, SizeOfRawData
, &BytesRead
);
410 if (Status
!= ESUCCESS
)
412 ERR("WinLdrLoadImage(): Error reading section from file!\n");
417 /* Size of data is less than the virtual size - fill up the remainder with zeroes */
418 if (SizeOfRawData
< VirtualSize
)
420 TRACE("WinLdrLoadImage(): SORD %d < VS %d\n", SizeOfRawData
, VirtualSize
);
421 RtlZeroMemory((PVOID
)(SectionHeader
->VirtualAddress
+ (ULONG_PTR
)PhysicalBase
+ SizeOfRawData
), VirtualSize
- SizeOfRawData
);
427 /* We are done with the file - close it */
430 /* If loading failed - return right now */
431 if (Status
!= ESUCCESS
)
434 /* Relocate the image, if it needs it */
435 if (NtHeaders
->OptionalHeader
.ImageBase
!= (ULONG_PTR
)VirtualBase
)
437 WARN("Relocating %p -> %p\n", NtHeaders
->OptionalHeader
.ImageBase
,
439 return (BOOLEAN
)LdrRelocateImageWithBias(PhysicalBase
,
440 (ULONG_PTR
)VirtualBase
- (ULONG_PTR
)PhysicalBase
,
443 TRUE
, /* in case of conflict still return success */
447 TRACE("WinLdrLoadImage() done, PA = %p\n", *ImageBasePA
);
451 /* PRIVATE FUNCTIONS *******************************************************/
453 /* DllName - physical, UnicodeString->Buffer - virtual */
455 WinLdrpCompareDllName(IN PCH DllName
,
456 IN PUNICODE_STRING UnicodeName
)
459 UNICODE_STRING UnicodeNamePA
;
462 /* First obvious check: for length of two names */
463 Length
= strlen(DllName
);
465 UnicodeNamePA
.Length
= UnicodeName
->Length
;
466 UnicodeNamePA
.MaximumLength
= UnicodeName
->MaximumLength
;
467 UnicodeNamePA
.Buffer
= VaToPa(UnicodeName
->Buffer
);
468 TRACE("WinLdrpCompareDllName: %s and %wZ, Length = %d "
469 "UN->Length %d\n", DllName
, &UnicodeNamePA
, Length
, UnicodeName
->Length
);
471 if ((Length
* sizeof(WCHAR
)) > UnicodeName
->Length
)
474 /* Store pointer to unicode string's buffer */
475 Buffer
= VaToPa(UnicodeName
->Buffer
);
477 /* Loop character by character */
478 for (i
= 0; i
< Length
; i
++)
480 /* Compare two characters, uppercasing them */
481 if (toupper(*DllName
) != toupper((CHAR
)*Buffer
))
484 /* Move to the next character */
489 /* Check, if strings either fully match, or match till the "." (w/o extension) */
490 if ((UnicodeName
->Length
== Length
* sizeof(WCHAR
)) || (*Buffer
== L
'.'))
496 /* Strings don't match, return FALSE */
501 WinLdrpBindImportName(IN OUT PLIST_ENTRY ModuleListHead
,
504 IN PIMAGE_THUNK_DATA ThunkData
,
505 IN PIMAGE_EXPORT_DIRECTORY ExportDirectory
,
507 IN BOOLEAN ProcessForwards
,
508 IN PCSTR DirectoryPath
)
511 PULONG NameTable
, FunctionTable
;
512 PUSHORT OrdinalTable
;
513 LONG High
, Low
, Middle
, Result
;
515 PIMAGE_IMPORT_BY_NAME ImportData
;
516 PCHAR ExportName
, ForwarderName
;
519 //TRACE("WinLdrpBindImportName(): DllBase 0x%X, ImageBase 0x%X, ThunkData 0x%X, ExportDirectory 0x%X, ExportSize %d, ProcessForwards 0x%X\n",
520 // DllBase, ImageBase, ThunkData, ExportDirectory, ExportSize, ProcessForwards);
522 /* Check passed DllBase param */
525 WARN("DllBase == NULL!\n");
529 /* Convert all non-critical pointers to PA from VA */
530 ThunkData
= VaToPa(ThunkData
);
532 /* Is the reference by ordinal? */
533 if (IMAGE_SNAP_BY_ORDINAL(ThunkData
->u1
.Ordinal
) && !ProcessForwards
)
535 /* Yes, calculate the ordinal */
536 Ordinal
= (ULONG
)(IMAGE_ORDINAL(ThunkData
->u1
.Ordinal
) - (UINT32
)ExportDirectory
->Base
);
537 //TRACE("WinLdrpBindImportName(): Ordinal %d\n", Ordinal);
541 /* It's reference by name, we have to look it up in the export directory */
542 if (!ProcessForwards
)
544 /* AddressOfData in thunk entry will become a virtual address (from relative) */
545 //TRACE("WinLdrpBindImportName(): ThunkData->u1.AOD was %p\n", ThunkData->u1.AddressOfData);
546 ThunkData
->u1
.AddressOfData
=
547 (ULONG_PTR
)RVA(ImageBase
, ThunkData
->u1
.AddressOfData
);
548 //TRACE("WinLdrpBindImportName(): ThunkData->u1.AOD became %p\n", ThunkData->u1.AddressOfData);
551 /* Get the import name */
552 ImportData
= VaToPa((PVOID
)ThunkData
->u1
.AddressOfData
);
554 /* Get pointers to Name and Ordinal tables (RVA -> VA) */
555 NameTable
= VaToPa(RVA(DllBase
, ExportDirectory
->AddressOfNames
));
556 OrdinalTable
= VaToPa(RVA(DllBase
, ExportDirectory
->AddressOfNameOrdinals
));
558 //TRACE("NameTable 0x%X, OrdinalTable 0x%X, ED->AddressOfNames 0x%X, ED->AOFO 0x%X\n",
559 // NameTable, OrdinalTable, ExportDirectory->AddressOfNames, ExportDirectory->AddressOfNameOrdinals);
561 /* Get the hint, convert it to a physical pointer */
562 Hint
= ((PIMAGE_IMPORT_BY_NAME
)VaToPa((PVOID
)ThunkData
->u1
.AddressOfData
))->Hint
;
563 //TRACE("HintIndex %d\n", Hint);
565 /* Get the export name from the hint */
566 ExportName
= VaToPa(RVA(DllBase
, NameTable
[Hint
]));
568 /* If Hint is less than total number of entries in the export directory,
569 and import name == export name, then we can just get it from the OrdinalTable */
570 if ((Hint
< ExportDirectory
->NumberOfNames
) &&
571 (strcmp(ExportName
, (PCHAR
)ImportData
->Name
) == 0))
573 Ordinal
= OrdinalTable
[Hint
];
574 //TRACE("WinLdrpBindImportName(): Ordinal %d\n", Ordinal);
578 /* It's not the easy way, we have to lookup import name in the name table.
579 Let's use a binary search for this task. */
581 //TRACE("WinLdrpBindImportName() looking up the import name using binary search...\n");
583 /* Low boundary is set to 0, and high boundary to the maximum index */
585 High
= ExportDirectory
->NumberOfNames
- 1;
587 /* Perform a binary-search loop */
590 /* Divide by 2 by shifting to the right once */
591 Middle
= (Low
+ High
) / 2;
593 /* Get the name from the name table */
594 ExportName
= VaToPa(RVA(DllBase
, NameTable
[Middle
]));
596 /* Compare the names */
597 Result
= strcmp(ExportName
, (PCHAR
)ImportData
->Name
);
599 /*TRACE("Binary search: comparing Import '__', Export '%s'\n",*/
600 /*VaToPa(&((PIMAGE_IMPORT_BY_NAME)VaToPa(ThunkData->u1.AddressOfData))->Name[0]),*/
601 /*(PCHAR)VaToPa(RVA(DllBase, NameTable[Middle])));*/
603 /*TRACE("TE->u1.AOD %p, fulladdr %p\n",
604 ThunkData->u1.AddressOfData,
605 ((PIMAGE_IMPORT_BY_NAME)VaToPa(ThunkData->u1.AddressOfData))->Name );*/
608 /* Depending on result of strcmp, perform different actions */
611 /* Adjust top boundary */
616 /* Adjust bottom boundary */
626 /* If high boundary is less than low boundary, then no result found */
629 //Print(L"Error in binary search\n");
630 ERR("Did not find export '%s'!\n", (PCHAR
)ImportData
->Name
);
634 /* Everything allright, get the ordinal */
635 Ordinal
= OrdinalTable
[Middle
];
637 //TRACE("WinLdrpBindImportName() found Ordinal %d\n", Ordinal);
641 /* Check ordinal number for validity! */
642 if (Ordinal
>= ExportDirectory
->NumberOfFunctions
)
644 ERR("Ordinal number is invalid!\n");
648 /* Get a pointer to the function table */
649 FunctionTable
= (PULONG
)VaToPa(RVA(DllBase
, ExportDirectory
->AddressOfFunctions
));
651 /* Save a pointer to the function */
652 ThunkData
->u1
.Function
= (ULONG_PTR
)RVA(DllBase
, FunctionTable
[Ordinal
]);
654 /* Is it a forwarder? (function pointer is within the export directory) */
655 ForwarderName
= (PCHAR
)VaToPa((PVOID
)ThunkData
->u1
.Function
);
656 if (((ULONG_PTR
)ForwarderName
> (ULONG_PTR
)ExportDirectory
) &&
657 ((ULONG_PTR
)ForwarderName
< ((ULONG_PTR
)ExportDirectory
+ ExportSize
)))
659 PLDR_DATA_TABLE_ENTRY DataTableEntry
;
660 CHAR ForwardDllName
[255];
661 PIMAGE_EXPORT_DIRECTORY RefExportDirectory
;
663 TRACE("WinLdrpBindImportName(): ForwarderName %s\n", ForwarderName
);
665 /* Save the name of the forward dll */
666 RtlCopyMemory(ForwardDllName
, ForwarderName
, sizeof(ForwardDllName
));
668 /* Strip out the symbol name */
669 *strrchr(ForwardDllName
,'.') = '\0';
671 /* Check if the target image is already loaded */
672 if (!WinLdrCheckForLoadedDll(ModuleListHead
, ForwardDllName
, &DataTableEntry
))
674 /* Check if the forward dll name has an extension */
675 if (strchr(ForwardDllName
, '.') == NULL
)
677 /* Name does not have an extension, append '.dll' */
678 strcat(ForwardDllName
, ".dll");
681 /* Now let's try to load it! */
682 Success
= WinLdrpLoadAndScanReferencedDll(ModuleListHead
,
688 ERR("WinLdrpLoadAndScanReferencedDll() failed to load forwarder dll.\n");
693 /* Get pointer to the export directory of loaded DLL */
694 RefExportDirectory
= (PIMAGE_EXPORT_DIRECTORY
)
695 RtlImageDirectoryEntryToData(VaToPa(DataTableEntry
->DllBase
),
697 IMAGE_DIRECTORY_ENTRY_EXPORT
,
700 /* Fail if it's NULL */
701 if (RefExportDirectory
)
704 IMAGE_THUNK_DATA RefThunkData
;
705 PIMAGE_IMPORT_BY_NAME ImportByName
;
708 /* Get pointer to the import name */
709 ImportName
= strrchr(ForwarderName
, '.') + 1;
711 /* Create a IMAGE_IMPORT_BY_NAME structure, pointing to the local Buffer */
712 ImportByName
= (PIMAGE_IMPORT_BY_NAME
)Buffer
;
714 /* Fill the name with the import name */
715 RtlCopyMemory(ImportByName
->Name
, ImportName
, strlen(ImportName
)+1);
718 ImportByName
->Hint
= 0;
720 /* And finally point ThunkData's AddressOfData to that structure */
721 RefThunkData
.u1
.AddressOfData
= (ULONG_PTR
)ImportByName
;
723 /* And recursively call ourselves */
724 Success
= WinLdrpBindImportName(ModuleListHead
,
725 DataTableEntry
->DllBase
,
733 /* Fill out the ThunkData with data from RefThunkData */
734 ThunkData
->u1
= RefThunkData
.u1
;
736 /* Return what we got from the recursive call */
741 /* Fail if ExportDirectory is NULL */
751 WinLdrpLoadAndScanReferencedDll(PLIST_ENTRY ModuleListHead
,
754 PLDR_DATA_TABLE_ENTRY
*DataTableEntry
)
756 CHAR FullDllName
[256];
760 /* Prepare the full path to the file to be loaded */
761 strcpy(FullDllName
, DirectoryPath
);
762 strcat(FullDllName
, ImportName
);
764 TRACE("Loading referenced DLL: %s\n", FullDllName
);
765 //Print(L"Loading referenced DLL: %s\n", FullDllName);
768 Success
= WinLdrLoadImage(FullDllName
, LoaderBootDriver
, &BasePA
);
771 ERR("WinLdrLoadImage() failed\n");
775 /* Allocate DTE for newly loaded DLL */
776 Success
= WinLdrAllocateDataTableEntry(ModuleListHead
,
783 ERR("WinLdrAllocateDataTableEntry() failed\n");
787 /* Scan its dependencies too */
788 TRACE("WinLdrScanImportDescriptorTable() calling ourselves for %S\n",
789 VaToPa((*DataTableEntry
)->BaseDllName
.Buffer
));
790 Success
= WinLdrScanImportDescriptorTable(ModuleListHead
, DirectoryPath
, *DataTableEntry
);
793 ERR("WinLdrScanImportDescriptorTable() failed\n");
801 WinLdrpScanImportAddressTable(IN OUT PLIST_ENTRY ModuleListHead
,
804 IN PIMAGE_THUNK_DATA ThunkData
,
805 IN PCSTR DirectoryPath
)
807 PIMAGE_EXPORT_DIRECTORY ExportDirectory
= NULL
;
811 TRACE("WinLdrpScanImportAddressTable(): DllBase 0x%X, "
812 "ImageBase 0x%X, ThunkData 0x%X\n", DllBase
, ImageBase
, ThunkData
);
814 /* Obtain the export table from the DLL's base */
817 ERR("Error, DllBase == NULL!\n");
823 (PIMAGE_EXPORT_DIRECTORY
)RtlImageDirectoryEntryToData(VaToPa(DllBase
),
825 IMAGE_DIRECTORY_ENTRY_EXPORT
,
829 TRACE("WinLdrpScanImportAddressTable(): ExportDirectory 0x%X\n", ExportDirectory
);
831 /* If pointer to Export Directory is */
832 if (ExportDirectory
== NULL
)
834 ERR("DllBase=%p(%p)\n", DllBase
, VaToPa(DllBase
));
838 /* Go through each entry in the thunk table and bind it */
839 while (((PIMAGE_THUNK_DATA
)VaToPa(ThunkData
))->u1
.AddressOfData
!= 0)
842 Success
= WinLdrpBindImportName(ModuleListHead
,
851 /* Move to the next entry */
854 /* Return error if binding was unsuccessful */