2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/ntdll/ldr/ldrpe.c
5 * PURPOSE: Loader Functions dealing low-level PE Format structures
6 * PROGRAMMERS: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES *****************************************************************/
15 /* GLOBALS *******************************************************************/
17 PVOID LdrpManifestProberRoutine
;
20 /* FUNCTIONS *****************************************************************/
24 LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry
,
25 IN PLDR_DATA_TABLE_ENTRY ImportLdrEntry
,
26 IN PIMAGE_IMPORT_DESCRIPTOR IatEntry
,
27 IN BOOLEAN EntriesValid
)
31 PIMAGE_THUNK_DATA OriginalThunk
, FirstThunk
;
32 PIMAGE_NT_HEADERS NtHeader
;
33 PIMAGE_SECTION_HEADER SectionHeader
;
34 PIMAGE_EXPORT_DIRECTORY ExportDirectory
;
36 ULONG ForwarderChain
, i
, Rva
, OldProtect
, IatSize
, ExportSize
;
38 DPRINT("LdrpSnapIAT(%wZ %wZ %p %d)\n", &ExportLdrEntry
->BaseDllName
, &ImportLdrEntry
->BaseDllName
, IatEntry
, EntriesValid
);
40 /* Get export directory */
41 ExportDirectory
= RtlImageDirectoryEntryToData(ExportLdrEntry
->DllBase
,
43 IMAGE_DIRECTORY_ENTRY_EXPORT
,
46 /* Make sure it has one */
50 DbgPrint("LDR: %wZ doesn't contain an EXPORT table\n",
51 &ExportLdrEntry
->BaseDllName
);
52 return STATUS_INVALID_IMAGE_FORMAT
;
56 Iat
= RtlImageDirectoryEntryToData(ImportLdrEntry
->DllBase
,
58 IMAGE_DIRECTORY_ENTRY_IAT
,
62 /* Check if we don't have one */
65 /* Get the NT Header and the first section */
66 NtHeader
= RtlImageNtHeader(ImportLdrEntry
->DllBase
);
67 if (!NtHeader
) return STATUS_INVALID_IMAGE_FORMAT
;
68 SectionHeader
= IMAGE_FIRST_SECTION(NtHeader
);
70 /* Get the RVA of the import directory */
71 Rva
= NtHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
;
73 /* Make sure we got one */
76 /* Loop all the sections */
77 for (i
= 0; i
< NtHeader
->FileHeader
.NumberOfSections
; i
++)
79 /* Check if we are inside this section */
80 if ((Rva
>= SectionHeader
->VirtualAddress
) &&
81 (Rva
< (SectionHeader
->VirtualAddress
+
82 SectionHeader
->SizeOfRawData
)))
84 /* We are, so set the IAT here */
85 Iat
= (PVOID
)((ULONG_PTR
)(ImportLdrEntry
->DllBase
) +
86 SectionHeader
->VirtualAddress
);
89 IatSize
= SectionHeader
->Misc
.VirtualSize
;
91 /* Deal with Watcom and other retarded compilers */
92 if (!IatSize
) IatSize
= SectionHeader
->SizeOfRawData
;
94 /* Found it, get out */
98 /* No match, move to the next section */
103 /* If we still don't have an IAT, that's bad */
107 DbgPrint("LDR: Unable to unprotect IAT for %wZ (Image Base %p)\n",
108 &ImportLdrEntry
->BaseDllName
,
109 ImportLdrEntry
->DllBase
);
110 return STATUS_INVALID_IMAGE_FORMAT
;
113 /* Set the right size */
114 ImportSize
= IatSize
;
117 /* Unprotect the IAT */
118 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
123 if (!NT_SUCCESS(Status
))
126 DbgPrint("LDR: Unable to unprotect IAT for %wZ (Status %x)\n",
127 &ImportLdrEntry
->BaseDllName
,
132 /* Check if the Thunks are already valid */
135 /* We'll only do forwarders. Get the import name */
136 ImportName
= (LPSTR
)((ULONG_PTR
)ImportLdrEntry
->DllBase
+ IatEntry
->Name
);
138 /* Get the list of forwaders */
139 ForwarderChain
= IatEntry
->ForwarderChain
;
142 while (ForwarderChain
!= -1)
144 /* Get the cached thunk VA*/
145 OriginalThunk
= (PIMAGE_THUNK_DATA
)
146 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
147 IatEntry
->OriginalFirstThunk
+
148 (ForwarderChain
* sizeof(IMAGE_THUNK_DATA
)));
150 /* Get the first thunk */
151 FirstThunk
= (PIMAGE_THUNK_DATA
)
152 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
153 IatEntry
->FirstThunk
+
154 (ForwarderChain
* sizeof(IMAGE_THUNK_DATA
)));
156 /* Get the Forwarder from the thunk */
157 ForwarderChain
= (ULONG
)FirstThunk
->u1
.Ordinal
;
162 Status
= LdrpSnapThunk(ExportLdrEntry
->DllBase
,
163 ImportLdrEntry
->DllBase
,
171 /* Move to the next thunk */
173 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
175 /* Fail with the SEH error */
176 Status
= _SEH2_GetExceptionCode();
179 /* If we messed up, exit */
180 if (!NT_SUCCESS(Status
)) break;
183 else if (IatEntry
->FirstThunk
)
185 /* Full snapping. Get the First thunk */
186 FirstThunk
= (PIMAGE_THUNK_DATA
)
187 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
188 IatEntry
->FirstThunk
);
190 /* Get the NT Header */
191 NtHeader
= RtlImageNtHeader(ImportLdrEntry
->DllBase
);
193 /* Get the Original thunk VA, watch out for weird images */
194 if ((IatEntry
->Characteristics
< NtHeader
->OptionalHeader
.SizeOfHeaders
) ||
195 (IatEntry
->Characteristics
>= NtHeader
->OptionalHeader
.SizeOfImage
))
197 /* Refuse it, this is a strange linked file */
198 OriginalThunk
= FirstThunk
;
202 /* Get the address from the field and convert to VA */
203 OriginalThunk
= (PIMAGE_THUNK_DATA
)
204 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
205 IatEntry
->OriginalFirstThunk
);
208 /* Get the Import name VA */
209 ImportName
= (LPSTR
)((ULONG_PTR
)ImportLdrEntry
->DllBase
+
212 /* Loop while it's valid */
213 while (OriginalThunk
->u1
.AddressOfData
)
218 Status
= LdrpSnapThunk(ExportLdrEntry
->DllBase
,
219 ImportLdrEntry
->DllBase
,
230 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
232 /* Fail with the SEH error */
233 Status
= _SEH2_GetExceptionCode();
236 /* If we failed the snap, break out */
237 if (!NT_SUCCESS(Status
)) break;
241 /* Protect the IAT again */
242 NtProtectVirtualMemory(NtCurrentProcess(),
248 /* Also flush out the cache */
249 NtFlushInstructionCache(NtCurrentProcess(), Iat
, IatSize
);
251 /* Return to Caller */
257 LdrpHandleOneNewFormatImportDescriptor(IN LPWSTR DllPath OPTIONAL
,
258 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
259 IN PIMAGE_BOUND_IMPORT_DESCRIPTOR
*BoundEntryPtr
,
260 IN PIMAGE_BOUND_IMPORT_DESCRIPTOR FirstEntry
)
262 LPSTR ImportName
= NULL
, BoundImportName
, ForwarderName
;
264 BOOLEAN AlreadyLoaded
= FALSE
, Stale
;
265 PIMAGE_IMPORT_DESCRIPTOR ImportEntry
;
266 PLDR_DATA_TABLE_ENTRY DllLdrEntry
, ForwarderLdrEntry
;
267 PIMAGE_BOUND_FORWARDER_REF ForwarderEntry
;
268 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
;
269 PPEB Peb
= NtCurrentPeb();
272 /* Get the pointer to the bound entry */
273 BoundEntry
= *BoundEntryPtr
;
275 /* Get the name's VA */
276 BoundImportName
= (LPSTR
)FirstEntry
+ BoundEntry
->OffsetModuleName
;
278 /* Show debug mesage */
281 DPRINT1("LDR: %wZ bound to %s\n", &LdrEntry
->BaseDllName
, BoundImportName
);
284 /* Load the module for this entry */
285 Status
= LdrpLoadImportModule(DllPath
,
290 if (!NT_SUCCESS(Status
))
292 /* Show debug message */
295 DPRINT1("LDR: %wZ failed to load import module %s; status = %x\n",
296 &LdrEntry
->BaseDllName
,
303 /* Check if it wasn't already loaded */
306 /* Add it to our list */
307 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
308 &DllLdrEntry
->InInitializationOrderModuleList
);
311 /* Check if the Bound Entry is now invalid */
312 if ((BoundEntry
->TimeDateStamp
!= DllLdrEntry
->TimeDateStamp
) ||
313 (DllLdrEntry
->Flags
& LDRP_IMAGE_NOT_AT_BASE
))
315 /* Show debug message */
318 DPRINT1("LDR: %wZ has stale binding to %s\n",
319 &LdrEntry
->BaseDllName
,
323 /* Remember it's become stale */
328 /* Show debug message */
331 DPRINT1("LDR: %wZ has correct binding to %s\n",
332 &LdrEntry
->BaseDllName
,
336 /* Remember it's valid */
340 /* Get the forwarders */
341 ForwarderEntry
= (PIMAGE_BOUND_FORWARDER_REF
)(BoundEntry
+ 1);
344 for (i
= 0; i
< BoundEntry
->NumberOfModuleForwarderRefs
; i
++)
347 ForwarderName
= (LPSTR
)FirstEntry
+ ForwarderEntry
->OffsetModuleName
;
349 /* Show debug message */
352 DPRINT1("LDR: %wZ bound to %s via forwarder(s) from %wZ\n",
353 &LdrEntry
->BaseDllName
,
355 &DllLdrEntry
->BaseDllName
);
358 /* Load the module */
359 Status
= LdrpLoadImportModule(DllPath
,
364 if (NT_SUCCESS(Status
))
366 /* Loaded it, was it already loaded? */
369 /* Add it to our list */
370 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
371 &ForwarderLdrEntry
->InInitializationOrderModuleList
);
375 /* Check if the Bound Entry is now invalid */
376 if (!(NT_SUCCESS(Status
)) ||
377 (ForwarderEntry
->TimeDateStamp
!= ForwarderLdrEntry
->TimeDateStamp
) ||
378 (ForwarderLdrEntry
->Flags
& LDRP_IMAGE_NOT_AT_BASE
))
380 /* Show debug message */
383 DPRINT1("LDR: %wZ has stale binding to %s\n",
384 &LdrEntry
->BaseDllName
,
388 /* Remember it's become stale */
393 /* Show debug message */
396 DPRINT1("LDR: %wZ has correct binding to %s\n",
397 &LdrEntry
->BaseDllName
,
401 /* Remember it's valid */
405 /* Move to the next one */
409 /* Set the next bound entry to the forwarder */
410 FirstEntry
= (PIMAGE_BOUND_IMPORT_DESCRIPTOR
)ForwarderEntry
;
412 /* Check if the binding was stale */
415 /* It was, so find the IAT entry for it */
417 ImportEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
419 IMAGE_DIRECTORY_ENTRY_IMPORT
,
422 /* Make sure it has a name */
423 while (ImportEntry
->Name
)
426 ImportName
= (LPSTR
)((ULONG_PTR
)LdrEntry
->DllBase
+ ImportEntry
->Name
);
429 if (!_stricmp(ImportName
, BoundImportName
)) break;
431 /* Move to next entry */
435 /* If we didn't find a name, fail */
436 if (!ImportEntry
->Name
)
438 /* Show debug message */
441 DPRINT1("LDR: LdrpWalkImportTable - failing with"
442 "STATUS_OBJECT_NAME_INVALID due to no import descriptor name\n");
446 Status
= STATUS_OBJECT_NAME_INVALID
;
450 /* Show debug message */
453 DPRINT1("LDR: Stale Bind %s from %wZ\n",
455 &LdrEntry
->BaseDllName
);
458 /* Snap the IAT Entry*/
459 Status
= LdrpSnapIAT(DllLdrEntry
,
464 /* Make sure we didn't fail */
465 if (!NT_SUCCESS(Status
))
467 /* Show debug message */
470 DPRINT1("LDR: %wZ failed to load import module %s; status = %x\n",
471 &LdrEntry
->BaseDllName
,
482 Status
= STATUS_SUCCESS
;
485 /* Write where we are now and return */
486 *BoundEntryPtr
= FirstEntry
;
492 LdrpHandleNewFormatImportDescriptors(IN LPWSTR DllPath OPTIONAL
,
493 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
494 IN PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
)
496 PIMAGE_BOUND_IMPORT_DESCRIPTOR FirstEntry
= BoundEntry
;
499 /* Make sure we have a name */
500 while (BoundEntry
->OffsetModuleName
)
502 /* Parse this descriptor */
503 Status
= LdrpHandleOneNewFormatImportDescriptor(DllPath
,
507 if (!NT_SUCCESS(Status
)) return Status
;
511 return STATUS_SUCCESS
;
516 LdrpHandleOneOldFormatImportDescriptor(IN LPWSTR DllPath OPTIONAL
,
517 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
518 IN PIMAGE_IMPORT_DESCRIPTOR
*ImportEntry
)
522 BOOLEAN AlreadyLoaded
= FALSE
;
523 PLDR_DATA_TABLE_ENTRY DllLdrEntry
;
524 PIMAGE_THUNK_DATA FirstThunk
;
525 PPEB Peb
= NtCurrentPeb();
527 /* Get the import name's VA */
528 ImportName
= (LPSTR
)((ULONG_PTR
)LdrEntry
->DllBase
+ (*ImportEntry
)->Name
);
530 /* Get the first thunk */
531 FirstThunk
= (PIMAGE_THUNK_DATA
)((ULONG_PTR
)LdrEntry
->DllBase
+
532 (*ImportEntry
)->FirstThunk
);
534 /* Make sure it's valid */
535 if (!FirstThunk
->u1
.Function
) goto SkipEntry
;
537 /* Show debug message */
540 DPRINT1("LDR: %s used by %wZ\n",
542 &LdrEntry
->BaseDllName
);
545 /* Load the module associated to it */
546 Status
= LdrpLoadImportModule(DllPath
,
551 if (!NT_SUCCESS(Status
))
556 DbgPrint("LDR: LdrpWalkImportTable - LdrpLoadImportModule failed "
557 "on import %s with status %x\n",
566 /* Show debug message */
569 DPRINT1("LDR: Snapping imports for %wZ from %s\n",
570 &LdrEntry
->BaseDllName
,
574 /* Check if it wasn't already loaded */
578 /* Add the DLL to our list */
579 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
580 &DllLdrEntry
->InInitializationOrderModuleList
);
583 /* Now snap the IAT Entry */
584 Status
= LdrpSnapIAT(DllLdrEntry
, LdrEntry
, *ImportEntry
, FALSE
);
585 if (!NT_SUCCESS(Status
))
590 DbgPrint("LDR: LdrpWalkImportTable - LdrpSnapIAT #2 failed with "
602 return STATUS_SUCCESS
;
607 LdrpHandleOldFormatImportDescriptors(IN LPWSTR DllPath OPTIONAL
,
608 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
609 IN PIMAGE_IMPORT_DESCRIPTOR ImportEntry
)
613 /* Check for Name and Thunk */
614 while ((ImportEntry
->Name
) && (ImportEntry
->FirstThunk
))
616 /* Parse this descriptor */
617 Status
= LdrpHandleOneOldFormatImportDescriptor(DllPath
,
620 if (!NT_SUCCESS(Status
)) return Status
;
624 return STATUS_SUCCESS
;
629 LdrpNameToOrdinal(IN LPSTR ImportName
,
630 IN ULONG NumberOfNames
,
633 IN PUSHORT OrdinalTable
)
635 LONG Start
, End
, Next
, CmpResult
;
637 /* Use classical binary search to find the ordinal */
639 End
= NumberOfNames
- 1;
642 /* Next will be exactly between Start and End */
643 Next
= (Start
+ End
) >> 1;
645 /* Compare this name with the one we need to find */
646 CmpResult
= strcmp(ImportName
, (PCHAR
)((ULONG_PTR
)ExportBase
+ NameTable
[Next
]));
648 /* We found our entry if result is 0 */
649 if (!CmpResult
) break;
651 /* We didn't find, update our range then */
656 else if (CmpResult
> 0)
662 /* If end is before start, then the search failed */
663 if (End
< Start
) return -1;
665 /* Return found name */
666 return OrdinalTable
[Next
];
671 LdrpWalkImportDescriptor(IN LPWSTR DllPath OPTIONAL
,
672 IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
674 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx
;
675 PPEB Peb
= NtCurrentPeb();
676 NTSTATUS Status
= STATUS_SUCCESS
;
677 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
= NULL
;
678 PIMAGE_IMPORT_DESCRIPTOR ImportEntry
;
679 ULONG BoundSize
, IatSize
;
680 DPRINT("LdrpWalkImportDescriptor('%S' %x)\n", DllPath
, LdrEntry
);
682 /* Set up the Act Ctx */
683 RtlZeroMemory(&ActCtx
, sizeof(ActCtx
));
684 ActCtx
.Size
= sizeof(ActCtx
);
685 ActCtx
.Format
= RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_FORMAT_WHISTLER
;
687 /* Check if we have a manifest prober routine */
688 if (LdrpManifestProberRoutine
)
690 DPRINT1("We don't support manifests yet, much less prober routines\n");
693 /* Check if we failed above */
694 if (!NT_SUCCESS(Status
)) return Status
;
696 /* Get the Active ActCtx */
697 Status
= RtlGetActiveActivationContext(&LdrEntry
->EntryPointActivationContext
);
698 if (!NT_SUCCESS(Status
))
701 DbgPrintEx(51, // DPFLTR_SXS_ID
702 DPFLTR_WARNING_LEVEL
,
703 "LDR: RtlGetActiveActivationContext() failed; ntstatus = "
709 /* Activate the ActCtx */
710 RtlActivateActivationContextUnsafeFast(&ActCtx
,
711 LdrEntry
->EntryPointActivationContext
);
713 /* Check if we were redirected */
714 if (!(LdrEntry
->Flags
& LDRP_REDIRECTED
))
716 /* Get the Bound IAT */
717 BoundEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
719 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
723 /* Get the regular IAT, for fallback */
724 ImportEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
726 IMAGE_DIRECTORY_ENTRY_IMPORT
,
729 /* Check if we got at least one */
730 if ((BoundEntry
) || (ImportEntry
))
732 /* Do we have a Bound IAT */
735 /* Handle the descriptor */
736 Status
= LdrpHandleNewFormatImportDescriptors(DllPath
,
742 /* Handle the descriptor */
743 Status
= LdrpHandleOldFormatImportDescriptors(DllPath
,
748 /* Check the status of the handlers */
749 if (NT_SUCCESS(Status
))
751 /* Check for Per-DLL Heap Tagging */
752 if (Peb
->NtGlobalFlag
& FLG_HEAP_ENABLE_TAG_BY_DLL
)
755 DPRINT1("We don't support Per-DLL Heap Tagging yet!\n");
758 /* Check if Page Heap was enabled */
759 if (Peb
->NtGlobalFlag
& FLG_HEAP_PAGE_ALLOCS
)
762 DPRINT1("We don't support Page Heaps yet!\n");
765 /* Check if Application Verifier was enabled */
766 if (Peb
->NtGlobalFlag
& FLG_HEAP_ENABLE_TAIL_CHECK
)
769 DPRINT1("We don't support Application Verifier yet!\n");
772 /* Just to be safe */
773 Status
= STATUS_SUCCESS
;
777 /* Release the activation context */
778 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
784 /* FIXME: This function is missing SxS support and has wrong prototype */
787 LdrpLoadImportModule(IN PWSTR DllPath OPTIONAL
,
790 OUT PLDR_DATA_TABLE_ENTRY
*DataTableEntry
,
791 OUT PBOOLEAN Existing
)
793 ANSI_STRING AnsiString
;
794 PUNICODE_STRING ImpDescName
;
796 PPEB Peb
= RtlGetCurrentPeb();
797 PTEB Teb
= NtCurrentTeb();
799 DPRINT("LdrpLoadImportModule('%S' '%s' %p %p %p)\n", DllPath
, ImportName
, DllBase
, DataTableEntry
, Existing
);
801 /* Convert import descriptor name to unicode string */
802 ImpDescName
= &Teb
->StaticUnicodeString
;
803 RtlInitAnsiString(&AnsiString
, ImportName
);
804 Status
= RtlAnsiStringToUnicodeString(ImpDescName
, &AnsiString
, FALSE
);
805 if (!NT_SUCCESS(Status
)) return Status
;
807 /* Check if it's loaded */
808 if (LdrpCheckForLoadedDll(DllPath
,
814 /* It's already existing in the list */
816 return STATUS_SUCCESS
;
819 /* We're loading it for the first time */
823 Status
= LdrpMapDll(DllPath
,
831 if (!NT_SUCCESS(Status
)) return Status
;
833 /* Walk its import descriptor table */
834 Status
= LdrpWalkImportDescriptor(DllPath
,
836 if (!NT_SUCCESS(Status
))
838 /* Add it to the in-init-order list in case of failure */
839 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
840 &(*DataTableEntry
)->InInitializationOrderModuleList
);
848 LdrpSnapThunk(IN PVOID ExportBase
,
850 IN PIMAGE_THUNK_DATA OriginalThunk
,
851 IN OUT PIMAGE_THUNK_DATA Thunk
,
852 IN PIMAGE_EXPORT_DIRECTORY ExportEntry
,
859 ULONG OriginalOrdinal
= 0;
860 PIMAGE_IMPORT_BY_NAME AddressOfData
;
862 PUSHORT OrdinalTable
;
863 LPSTR ImportName
= NULL
;
866 ULONG_PTR HardErrorParameters
[3];
867 UNICODE_STRING HardErrorDllName
, HardErrorEntryPointName
;
868 ANSI_STRING TempString
;
871 PULONG AddressOfFunctions
;
872 UNICODE_STRING TempUString
;
873 ANSI_STRING ForwarderName
;
874 PANSI_STRING ForwardName
;
875 PVOID ForwarderHandle
;
876 ULONG ForwardOrdinal
;
878 /* Check if the snap is by ordinal */
879 if ((IsOrdinal
= IMAGE_SNAP_BY_ORDINAL(OriginalThunk
->u1
.Ordinal
)))
881 /* Get the ordinal number, and its normalized version */
882 OriginalOrdinal
= IMAGE_ORDINAL(OriginalThunk
->u1
.Ordinal
);
883 Ordinal
= (USHORT
)(OriginalOrdinal
- ExportEntry
->Base
);
887 /* First get the data VA */
888 AddressOfData
= (PIMAGE_IMPORT_BY_NAME
)
889 ((ULONG_PTR
)ImportBase
+
890 ((ULONG_PTR
)OriginalThunk
->u1
.AddressOfData
& 0xffffffff));
893 ImportName
= (LPSTR
)AddressOfData
->Name
;
895 /* Now get the VA of the Name and Ordinal Tables */
896 NameTable
= (PULONG
)((ULONG_PTR
)ExportBase
+
897 (ULONG_PTR
)ExportEntry
->AddressOfNames
);
898 OrdinalTable
= (PUSHORT
)((ULONG_PTR
)ExportBase
+
899 (ULONG_PTR
)ExportEntry
->AddressOfNameOrdinals
);
902 Hint
= AddressOfData
->Hint
;
904 /* Try to get a match by using the hint */
905 if (((ULONG
)Hint
< ExportEntry
->NumberOfNames
) &&
906 (!strcmp(ImportName
, ((LPSTR
)((ULONG_PTR
)ExportBase
+ NameTable
[Hint
])))))
908 /* We got a match, get the Ordinal from the hint */
909 Ordinal
= OrdinalTable
[Hint
];
913 /* Well bummer, hint didn't work, do it the long way */
914 Ordinal
= LdrpNameToOrdinal(ImportName
,
915 ExportEntry
->NumberOfNames
,
922 /* Check if the ordinal is invalid */
923 if ((ULONG
)Ordinal
>= ExportEntry
->NumberOfFunctions
)
926 /* Is this a static snap? */
929 /* Inform the debug log */
931 DPRINT1("Failed to snap ordinal 0x%x\n", OriginalOrdinal
);
933 DPRINT1("Failed to snap %s\n", ImportName
);
935 /* These are critical errors. Setup a string for the DLL name */
936 RtlInitAnsiString(&TempString
, DllName
? DllName
: "Unknown");
937 RtlAnsiStringToUnicodeString(&HardErrorDllName
, &TempString
, TRUE
);
939 /* Set it as the parameter */
940 HardErrorParameters
[1] = (ULONG_PTR
)&HardErrorDllName
;
943 /* Check if we have an ordinal */
946 /* Then set the ordinal as the 1st parameter */
947 HardErrorParameters
[0] = OriginalOrdinal
;
951 /* We don't, use the entrypoint. Set up a string for it */
952 RtlInitAnsiString(&TempString
, ImportName
);
953 RtlAnsiStringToUnicodeString(&HardErrorEntryPointName
,
957 /* Set it as the parameter */
958 HardErrorParameters
[0] = (ULONG_PTR
)&HardErrorEntryPointName
;
962 /* Raise the error */
963 NtRaiseHardError(IsOrdinal
? STATUS_ORDINAL_NOT_FOUND
:
964 STATUS_ENTRYPOINT_NOT_FOUND
,
971 /* Increase the error count */
972 if (LdrpInLdrInit
) LdrpFatalHardErrorCount
++;
974 /* Free our string */
975 RtlFreeUnicodeString(&HardErrorDllName
);
978 /* Free our second string. Return entrypoint error */
979 RtlFreeUnicodeString(&HardErrorEntryPointName
);
980 RtlRaiseStatus(STATUS_ENTRYPOINT_NOT_FOUND
);
983 /* Return ordinal error */
984 RtlRaiseStatus(STATUS_ORDINAL_NOT_FOUND
);
988 /* Inform the debug log */
990 DPRINT("Non-fatal: Failed to snap ordinal 0x%x\n", OriginalOrdinal
);
992 DPRINT("Non-fatal: Failed to snap %s\n", ImportName
);
995 /* Set this as a bad DLL */
996 Thunk
->u1
.Function
= (ULONG_PTR
)0xffbadd11;
998 /* Return the right error code */
999 Status
= IsOrdinal
? STATUS_ORDINAL_NOT_FOUND
:
1000 STATUS_ENTRYPOINT_NOT_FOUND
;
1004 /* The ordinal seems correct, get the AddressOfFunctions VA */
1005 AddressOfFunctions
= (PULONG
)
1006 ((ULONG_PTR
)ExportBase
+
1007 (ULONG_PTR
)ExportEntry
->AddressOfFunctions
);
1009 /* Write the function pointer*/
1010 Thunk
->u1
.Function
= (ULONG_PTR
)ExportBase
+ AddressOfFunctions
[Ordinal
];
1012 /* Make sure it's within the exports */
1013 if ((Thunk
->u1
.Function
> (ULONG_PTR
)ExportEntry
) &&
1014 (Thunk
->u1
.Function
< ((ULONG_PTR
)ExportEntry
+ ExportSize
)))
1016 /* Get the Import and Forwarder Names */
1017 ImportName
= (LPSTR
)Thunk
->u1
.Function
;
1018 ForwarderName
.Buffer
= ImportName
;
1019 ForwarderName
.Length
= (USHORT
)(strchr(ImportName
, '.') - ImportName
);
1020 ForwarderName
.MaximumLength
= ForwarderName
.Length
;
1021 Status
= RtlAnsiStringToUnicodeString(&TempUString
,
1025 /* Make sure the conversion was OK */
1026 if (NT_SUCCESS(Status
))
1028 /* Load the forwarder, free the temp string */
1029 Status
= LdrpLoadDll(FALSE
,
1035 RtlFreeUnicodeString(&TempUString
);
1038 /* If the load or conversion failed, use the failure path */
1039 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1041 /* Now set up a name for the actual forwarder dll */
1042 RtlInitAnsiString(&ForwarderName
,
1043 ImportName
+ ForwarderName
.Length
+ sizeof(CHAR
));
1045 /* Check if it's an ordinal forward */
1046 if ((ForwarderName
.Length
> 1) && (*ForwarderName
.Buffer
== '#'))
1048 /* We don't have an actual function name */
1051 /* Convert the string into an ordinal */
1052 Status
= RtlCharToInteger(ForwarderName
.Buffer
+ sizeof(CHAR
),
1056 /* If this fails, then error out */
1057 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1061 /* Import by name */
1062 ForwardName
= &ForwarderName
;
1065 /* Get the pointer */
1066 Status
= LdrpGetProcedureAddress(ForwarderHandle
,
1069 (PVOID
*)&Thunk
->u1
.Function
,
1071 /* If this fails, then error out */
1072 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1076 /* It's not within the exports, let's hope it's valid */
1077 if (!AddressOfFunctions
[Ordinal
]) goto FailurePath
;
1080 /* If we got here, then it's success */
1081 Status
= STATUS_SUCCESS
;