reactos/boot
Hermès Bélusca-Maïto dfb43905bb
[FREELDR] peloader.c: Fix PE import binding (#7537)
Use the PE import table's OriginalFirstThunk array when scanning and
resolving imports during DLL binding.

It points to an array of pointer-sized IMAGE_THUNK_DATA structures
which describe the functions being imported. On the other hand, the
FirstThunk points to an array of pointers, whose initial values are
a copy of those pointed to by OriginalFirstThunk, but are replaced
by the actual function pointers determined at runtime, when a DLL
is loaded (see PeLdrpBindImportName() function).
If we were to use the FirstThunk array to find again later the imports
by name or ordinal, we would fail because these are replaced by the
addresses of the corresponding functions.

This fixes loading kdcom.dll from Windows XP x64 with FreeLDR when
testing on ReactOS x64.

```diff
 (freeldr\freeldr\lib\peloader.c:498) trace: PeLdrpScanImportAddressTable() ---- Calling PeLdrpBindImportName() in a loop
 (freeldr\freeldr\lib\peloader.c:501) trace: *** ThunkName->u1.AddressOfData = 00000000000070F0
 (freeldr\freeldr\lib\peloader.c:502) trace: *** ThunkData->u1.AddressOfData = 0000000000573780
 (freeldr\freeldr\lib\peloader.c:209) trace:   !!! ExportDirectory->NumberOfNames 1504
-(freeldr\freeldr\lib\peloader.c:210) trace:   !!! ImportHint 0 - ExportName 'CcCanIWrite' - ImportDataName ''
+(freeldr\freeldr\lib\peloader.c:210) trace:   !!! ImportHint 282 - ExportName 'HalPrivateDispatchTable' - ImportDataName 'HalPrivateDispatchTable'
 ....
-(freeldr\freeldr\lib\peloader.c:268) err: Did not find export ''!
-(freeldr\freeldr\lib\peloader.c:709) err: PeLdrpScanImportAddressTable() failed: ImportName = 'ntoskrnl.exe', DirectoryPath = 'multi(0)disk(0)rdisk(0)partition(2)\ReactOS\system32\'
```
('-': lines before the fix; '+': lines after the fix)

Code has been adapted based from the following functions:
ntdll/ldr/ldrpe.c!LdrpSnapThunk() and LdrpSnapIAT()
ntoskrnl/mm/ARM3/sysldr.c!MiSnapThunk() and MiResolveImageReferences()

References:
https://devblogs.microsoft.com/oldnewthing/20231129-00/?p=109077
https://devblogs.microsoft.com/oldnewthing/20231130-00/?p=109084
https://stackoverflow.com/questions/42413937/why-pe-need-original-first-thunkoft
2024-12-03 13:13:38 +01:00
..
armllb [REACTOS] Add missing line breaks at end of file 2024-10-06 10:47:11 +03:00
bcd Add .keep guard files in order to restore lost but empty directories we had with SVN. 2017-10-04 10:28:36 +02:00
bgfx Add .keep guard files in order to restore lost but empty directories we had with SVN. 2017-10-04 10:28:36 +02:00
bootdata [THEMEUI][INF] Update German (de-DE) translation (#7489) 2024-11-18 21:24:57 +01:00
environ [REACTOS] Add missing line breaks at end of file 2024-10-06 10:47:11 +03:00
freeldr [FREELDR] peloader.c: Fix PE import binding (#7537) 2024-12-03 13:13:38 +01:00
rtl [CMAKE] Elimitate the use of GCC and CLANG variables 2022-05-27 01:37:34 +03:00
boot_images.cmake Revert "[BOOT] Don't create CSIDL_ADMINTOOLS folder in initial (#6551)" (#7017) 2024-06-14 11:08:14 +09:00
CMakeLists.txt [BOOT][SDK:RTL] Compile a reduced RTL library for FreeLdr / NT bootloader... 2022-04-07 03:19:09 +02:00