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 *****************************************************************/
16 /* GLOBALS *******************************************************************/
18 PVOID LdrpManifestProberRoutine
;
21 /* FUNCTIONS *****************************************************************/
25 AVrfPageHeapDllNotification(IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
27 /* Check if page heap dll notification is turned on */
28 if (!(RtlpDphGlobalFlags
& DPH_FLAG_DLL_NOTIFY
))
31 /* We don't support this flag currently */
37 LdrpSnapIAT(IN PLDR_DATA_TABLE_ENTRY ExportLdrEntry
,
38 IN PLDR_DATA_TABLE_ENTRY ImportLdrEntry
,
39 IN PIMAGE_IMPORT_DESCRIPTOR IatEntry
,
40 IN BOOLEAN EntriesValid
)
44 PIMAGE_THUNK_DATA OriginalThunk
, FirstThunk
;
45 PIMAGE_NT_HEADERS NtHeader
;
46 PIMAGE_SECTION_HEADER SectionHeader
;
47 PIMAGE_EXPORT_DIRECTORY ExportDirectory
;
49 ULONG ForwarderChain
, i
, Rva
, OldProtect
, IatSize
, ExportSize
;
51 DPRINT("LdrpSnapIAT(%wZ %wZ %p %u)\n", &ExportLdrEntry
->BaseDllName
, &ImportLdrEntry
->BaseDllName
, IatEntry
, EntriesValid
);
53 /* Get export directory */
54 ExportDirectory
= RtlImageDirectoryEntryToData(ExportLdrEntry
->DllBase
,
56 IMAGE_DIRECTORY_ENTRY_EXPORT
,
59 /* Make sure it has one */
63 DbgPrint("LDR: %wZ doesn't contain an EXPORT table\n",
64 &ExportLdrEntry
->BaseDllName
);
65 return STATUS_INVALID_IMAGE_FORMAT
;
69 Iat
= RtlImageDirectoryEntryToData(ImportLdrEntry
->DllBase
,
71 IMAGE_DIRECTORY_ENTRY_IAT
,
75 /* Check if we don't have one */
78 /* Get the NT Header and the first section */
79 NtHeader
= RtlImageNtHeader(ImportLdrEntry
->DllBase
);
80 if (!NtHeader
) return STATUS_INVALID_IMAGE_FORMAT
;
81 SectionHeader
= IMAGE_FIRST_SECTION(NtHeader
);
83 /* Get the RVA of the import directory */
84 Rva
= NtHeader
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_IMPORT
].VirtualAddress
;
86 /* Make sure we got one */
89 /* Loop all the sections */
90 for (i
= 0; i
< NtHeader
->FileHeader
.NumberOfSections
; i
++)
92 /* Check if we are inside this section */
93 if ((Rva
>= SectionHeader
->VirtualAddress
) &&
94 (Rva
< (SectionHeader
->VirtualAddress
+
95 SectionHeader
->SizeOfRawData
)))
97 /* We are, so set the IAT here */
98 Iat
= (PVOID
)((ULONG_PTR
)(ImportLdrEntry
->DllBase
) +
99 SectionHeader
->VirtualAddress
);
102 IatSize
= SectionHeader
->Misc
.VirtualSize
;
104 /* Deal with Watcom and other retarded compilers */
105 if (!IatSize
) IatSize
= SectionHeader
->SizeOfRawData
;
107 /* Found it, get out */
111 /* No match, move to the next section */
116 /* If we still don't have an IAT, that's bad */
120 DbgPrint("LDR: Unable to unprotect IAT for %wZ (Image Base %p)\n",
121 &ImportLdrEntry
->BaseDllName
,
122 ImportLdrEntry
->DllBase
);
123 return STATUS_INVALID_IMAGE_FORMAT
;
126 /* Set the right size */
127 ImportSize
= IatSize
;
130 /* Unprotect the IAT */
131 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
136 if (!NT_SUCCESS(Status
))
139 DbgPrint("LDR: Unable to unprotect IAT for %wZ (Status %x)\n",
140 &ImportLdrEntry
->BaseDllName
,
145 /* Check if the Thunks are already valid */
148 /* We'll only do forwarders. Get the import name */
149 ImportName
= (LPSTR
)((ULONG_PTR
)ImportLdrEntry
->DllBase
+ IatEntry
->Name
);
151 /* Get the list of forwaders */
152 ForwarderChain
= IatEntry
->ForwarderChain
;
155 while (ForwarderChain
!= -1)
157 /* Get the cached thunk VA*/
158 OriginalThunk
= (PIMAGE_THUNK_DATA
)
159 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
160 IatEntry
->OriginalFirstThunk
+
161 (ForwarderChain
* sizeof(IMAGE_THUNK_DATA
)));
163 /* Get the first thunk */
164 FirstThunk
= (PIMAGE_THUNK_DATA
)
165 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
166 IatEntry
->FirstThunk
+
167 (ForwarderChain
* sizeof(IMAGE_THUNK_DATA
)));
169 /* Get the Forwarder from the thunk */
170 ForwarderChain
= (ULONG
)FirstThunk
->u1
.Ordinal
;
175 Status
= LdrpSnapThunk(ExportLdrEntry
->DllBase
,
176 ImportLdrEntry
->DllBase
,
184 /* Move to the next thunk */
186 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
188 /* Fail with the SEH error */
189 Status
= _SEH2_GetExceptionCode();
192 /* If we messed up, exit */
193 if (!NT_SUCCESS(Status
)) break;
196 else if (IatEntry
->FirstThunk
)
198 /* Full snapping. Get the First thunk */
199 FirstThunk
= (PIMAGE_THUNK_DATA
)
200 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
201 IatEntry
->FirstThunk
);
203 /* Get the NT Header */
204 NtHeader
= RtlImageNtHeader(ImportLdrEntry
->DllBase
);
206 /* Get the Original thunk VA, watch out for weird images */
207 if ((IatEntry
->Characteristics
< NtHeader
->OptionalHeader
.SizeOfHeaders
) ||
208 (IatEntry
->Characteristics
>= NtHeader
->OptionalHeader
.SizeOfImage
))
210 /* Refuse it, this is a strange linked file */
211 OriginalThunk
= FirstThunk
;
215 /* Get the address from the field and convert to VA */
216 OriginalThunk
= (PIMAGE_THUNK_DATA
)
217 ((ULONG_PTR
)ImportLdrEntry
->DllBase
+
218 IatEntry
->OriginalFirstThunk
);
221 /* Get the Import name VA */
222 ImportName
= (LPSTR
)((ULONG_PTR
)ImportLdrEntry
->DllBase
+
225 /* Loop while it's valid */
226 while (OriginalThunk
->u1
.AddressOfData
)
231 Status
= LdrpSnapThunk(ExportLdrEntry
->DllBase
,
232 ImportLdrEntry
->DllBase
,
243 } _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
245 /* Fail with the SEH error */
246 Status
= _SEH2_GetExceptionCode();
249 /* If we failed the snap, break out */
250 if (!NT_SUCCESS(Status
)) break;
254 /* Protect the IAT again */
255 NtProtectVirtualMemory(NtCurrentProcess(),
261 /* Also flush out the cache */
262 NtFlushInstructionCache(NtCurrentProcess(), Iat
, IatSize
);
264 /* Return to Caller */
270 LdrpHandleOneNewFormatImportDescriptor(IN LPWSTR DllPath OPTIONAL
,
271 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
272 IN PIMAGE_BOUND_IMPORT_DESCRIPTOR
*BoundEntryPtr
,
273 IN PIMAGE_BOUND_IMPORT_DESCRIPTOR FirstEntry
)
275 LPSTR ImportName
= NULL
, BoundImportName
, ForwarderName
;
277 BOOLEAN AlreadyLoaded
= FALSE
, Stale
;
278 PIMAGE_IMPORT_DESCRIPTOR ImportEntry
;
279 PLDR_DATA_TABLE_ENTRY DllLdrEntry
, ForwarderLdrEntry
;
280 PIMAGE_BOUND_FORWARDER_REF ForwarderEntry
;
281 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
;
282 PPEB Peb
= NtCurrentPeb();
285 /* Get the pointer to the bound entry */
286 BoundEntry
= *BoundEntryPtr
;
288 /* Get the name's VA */
289 BoundImportName
= (LPSTR
)FirstEntry
+ BoundEntry
->OffsetModuleName
;
291 /* Show debug mesage */
294 DPRINT1("LDR: %wZ bound to %s\n", &LdrEntry
->BaseDllName
, BoundImportName
);
297 /* Load the module for this entry */
298 Status
= LdrpLoadImportModule(DllPath
,
303 if (!NT_SUCCESS(Status
))
305 /* Show debug message */
308 DPRINT1("LDR: %wZ failed to load import module %s; status = %x\n",
309 &LdrEntry
->BaseDllName
,
316 /* Check if it wasn't already loaded */
319 /* Add it to our list */
320 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
321 &DllLdrEntry
->InInitializationOrderModuleList
);
324 /* Check if the Bound Entry is now invalid */
325 if ((BoundEntry
->TimeDateStamp
!= DllLdrEntry
->TimeDateStamp
) ||
326 (DllLdrEntry
->Flags
& LDRP_IMAGE_NOT_AT_BASE
))
328 /* Show debug message */
331 DPRINT1("LDR: %wZ has stale binding to %s\n",
332 &LdrEntry
->BaseDllName
,
336 /* Remember it's become stale */
341 /* Show debug message */
344 DPRINT1("LDR: %wZ has correct binding to %s\n",
345 &LdrEntry
->BaseDllName
,
349 /* Remember it's valid */
353 /* Get the forwarders */
354 ForwarderEntry
= (PIMAGE_BOUND_FORWARDER_REF
)(BoundEntry
+ 1);
357 for (i
= 0; i
< BoundEntry
->NumberOfModuleForwarderRefs
; i
++)
360 ForwarderName
= (LPSTR
)FirstEntry
+ ForwarderEntry
->OffsetModuleName
;
362 /* Show debug message */
365 DPRINT1("LDR: %wZ bound to %s via forwarder(s) from %wZ\n",
366 &LdrEntry
->BaseDllName
,
368 &DllLdrEntry
->BaseDllName
);
371 /* Load the module */
372 Status
= LdrpLoadImportModule(DllPath
,
377 if (NT_SUCCESS(Status
))
379 /* Loaded it, was it already loaded? */
382 /* Add it to our list */
383 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
384 &ForwarderLdrEntry
->InInitializationOrderModuleList
);
388 /* Check if the Bound Entry is now invalid */
389 if (!(NT_SUCCESS(Status
)) ||
390 (ForwarderEntry
->TimeDateStamp
!= ForwarderLdrEntry
->TimeDateStamp
) ||
391 (ForwarderLdrEntry
->Flags
& LDRP_IMAGE_NOT_AT_BASE
))
393 /* Show debug message */
396 DPRINT1("LDR: %wZ has stale binding to %s\n",
397 &LdrEntry
->BaseDllName
,
401 /* Remember it's become stale */
406 /* Show debug message */
409 DPRINT1("LDR: %wZ has correct binding to %s\n",
410 &LdrEntry
->BaseDllName
,
414 /* Remember it's valid */
418 /* Move to the next one */
422 /* Set the next bound entry to the forwarder */
423 FirstEntry
= (PIMAGE_BOUND_IMPORT_DESCRIPTOR
)ForwarderEntry
;
425 /* Check if the binding was stale */
428 /* It was, so find the IAT entry for it */
430 ImportEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
432 IMAGE_DIRECTORY_ENTRY_IMPORT
,
435 /* Make sure it has a name */
436 while (ImportEntry
->Name
)
439 ImportName
= (LPSTR
)((ULONG_PTR
)LdrEntry
->DllBase
+ ImportEntry
->Name
);
442 if (!_stricmp(ImportName
, BoundImportName
)) break;
444 /* Move to next entry */
448 /* If we didn't find a name, fail */
449 if (!ImportEntry
->Name
)
451 /* Show debug message */
454 DPRINT1("LDR: LdrpWalkImportTable - failing with"
455 "STATUS_OBJECT_NAME_INVALID due to no import descriptor name\n");
459 Status
= STATUS_OBJECT_NAME_INVALID
;
463 /* Show debug message */
466 DPRINT1("LDR: Stale Bind %s from %wZ\n",
468 &LdrEntry
->BaseDllName
);
471 /* Snap the IAT Entry*/
472 Status
= LdrpSnapIAT(DllLdrEntry
,
477 /* Make sure we didn't fail */
478 if (!NT_SUCCESS(Status
))
480 /* Show debug message */
483 DPRINT1("LDR: %wZ failed to load import module %s; status = %x\n",
484 &LdrEntry
->BaseDllName
,
495 Status
= STATUS_SUCCESS
;
498 /* Write where we are now and return */
499 *BoundEntryPtr
= FirstEntry
;
505 LdrpHandleNewFormatImportDescriptors(IN LPWSTR DllPath OPTIONAL
,
506 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
507 IN PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
)
509 PIMAGE_BOUND_IMPORT_DESCRIPTOR FirstEntry
= BoundEntry
;
512 /* Make sure we have a name */
513 while (BoundEntry
->OffsetModuleName
)
515 /* Parse this descriptor */
516 Status
= LdrpHandleOneNewFormatImportDescriptor(DllPath
,
520 if (!NT_SUCCESS(Status
)) return Status
;
524 return STATUS_SUCCESS
;
529 LdrpHandleOneOldFormatImportDescriptor(IN LPWSTR DllPath OPTIONAL
,
530 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
531 IN PIMAGE_IMPORT_DESCRIPTOR
*ImportEntry
)
535 BOOLEAN AlreadyLoaded
= FALSE
;
536 PLDR_DATA_TABLE_ENTRY DllLdrEntry
;
537 PIMAGE_THUNK_DATA FirstThunk
;
538 PPEB Peb
= NtCurrentPeb();
540 /* Get the import name's VA */
541 ImportName
= (LPSTR
)((ULONG_PTR
)LdrEntry
->DllBase
+ (*ImportEntry
)->Name
);
543 /* Get the first thunk */
544 FirstThunk
= (PIMAGE_THUNK_DATA
)((ULONG_PTR
)LdrEntry
->DllBase
+
545 (*ImportEntry
)->FirstThunk
);
547 /* Make sure it's valid */
548 if (!FirstThunk
->u1
.Function
) goto SkipEntry
;
550 /* Show debug message */
553 DPRINT1("LDR: %s used by %wZ\n",
555 &LdrEntry
->BaseDllName
);
558 /* Load the module associated to it */
559 Status
= LdrpLoadImportModule(DllPath
,
564 if (!NT_SUCCESS(Status
))
569 DbgPrint("LDR: LdrpWalkImportTable - LdrpLoadImportModule failed "
570 "on import %s with status %x\n",
579 /* Show debug message */
582 DPRINT1("LDR: Snapping imports for %wZ from %s\n",
583 &LdrEntry
->BaseDllName
,
587 /* Check if it wasn't already loaded */
591 /* Add the DLL to our list */
592 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
593 &DllLdrEntry
->InInitializationOrderModuleList
);
596 /* Now snap the IAT Entry */
597 Status
= LdrpSnapIAT(DllLdrEntry
, LdrEntry
, *ImportEntry
, FALSE
);
598 if (!NT_SUCCESS(Status
))
603 DbgPrint("LDR: LdrpWalkImportTable - LdrpSnapIAT #2 failed with "
615 return STATUS_SUCCESS
;
620 LdrpHandleOldFormatImportDescriptors(IN LPWSTR DllPath OPTIONAL
,
621 IN PLDR_DATA_TABLE_ENTRY LdrEntry
,
622 IN PIMAGE_IMPORT_DESCRIPTOR ImportEntry
)
626 /* Check for Name and Thunk */
627 while ((ImportEntry
->Name
) && (ImportEntry
->FirstThunk
))
629 /* Parse this descriptor */
630 Status
= LdrpHandleOneOldFormatImportDescriptor(DllPath
,
633 if (!NT_SUCCESS(Status
)) return Status
;
637 return STATUS_SUCCESS
;
642 LdrpNameToOrdinal(IN LPSTR ImportName
,
643 IN ULONG NumberOfNames
,
646 IN PUSHORT OrdinalTable
)
648 LONG Start
, End
, Next
, CmpResult
;
650 /* Use classical binary search to find the ordinal */
652 End
= NumberOfNames
- 1;
655 /* Next will be exactly between Start and End */
656 Next
= (Start
+ End
) >> 1;
658 /* Compare this name with the one we need to find */
659 CmpResult
= strcmp(ImportName
, (PCHAR
)((ULONG_PTR
)ExportBase
+ NameTable
[Next
]));
661 /* We found our entry if result is 0 */
662 if (!CmpResult
) break;
664 /* We didn't find, update our range then */
669 else if (CmpResult
> 0)
675 /* If end is before start, then the search failed */
676 if (End
< Start
) return -1;
678 /* Return found name */
679 return OrdinalTable
[Next
];
684 LdrpWalkImportDescriptor(IN LPWSTR DllPath OPTIONAL
,
685 IN PLDR_DATA_TABLE_ENTRY LdrEntry
)
687 RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_EXTENDED ActCtx
;
688 PPEB Peb
= NtCurrentPeb();
689 NTSTATUS Status
= STATUS_SUCCESS
;
690 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
= NULL
;
691 PIMAGE_IMPORT_DESCRIPTOR ImportEntry
;
692 ULONG BoundSize
, IatSize
;
693 DPRINT("LdrpWalkImportDescriptor('%S' %p)\n", DllPath
, LdrEntry
);
695 /* Set up the Act Ctx */
696 RtlZeroMemory(&ActCtx
, sizeof(ActCtx
));
697 ActCtx
.Size
= sizeof(ActCtx
);
698 ActCtx
.Format
= RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_FORMAT_WHISTLER
;
700 /* Check if we have a manifest prober routine */
701 if (LdrpManifestProberRoutine
)
703 DPRINT1("We don't support manifests yet, much less prober routines\n");
706 /* Check if we failed above */
707 if (!NT_SUCCESS(Status
)) return Status
;
709 /* Get the Active ActCtx */
710 Status
= RtlGetActiveActivationContext(&LdrEntry
->EntryPointActivationContext
);
711 if (!NT_SUCCESS(Status
))
714 DbgPrintEx(51, // DPFLTR_SXS_ID
715 DPFLTR_WARNING_LEVEL
,
716 "LDR: RtlGetActiveActivationContext() failed; ntstatus = "
722 /* Activate the ActCtx */
723 RtlActivateActivationContextUnsafeFast(&ActCtx
,
724 LdrEntry
->EntryPointActivationContext
);
726 /* Check if we were redirected */
727 if (!(LdrEntry
->Flags
& LDRP_REDIRECTED
))
729 /* Get the Bound IAT */
730 BoundEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
732 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
736 /* Get the regular IAT, for fallback */
737 ImportEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
739 IMAGE_DIRECTORY_ENTRY_IMPORT
,
742 /* Check if we got at least one */
743 if ((BoundEntry
) || (ImportEntry
))
745 /* Do we have a Bound IAT */
748 /* Handle the descriptor */
749 Status
= LdrpHandleNewFormatImportDescriptors(DllPath
,
755 /* Handle the descriptor */
756 Status
= LdrpHandleOldFormatImportDescriptors(DllPath
,
761 /* Check the status of the handlers */
762 if (NT_SUCCESS(Status
))
764 /* Check for Per-DLL Heap Tagging */
765 if (Peb
->NtGlobalFlag
& FLG_HEAP_ENABLE_TAG_BY_DLL
)
768 DPRINT1("We don't support Per-DLL Heap Tagging yet!\n");
771 /* Check if Page Heap was enabled */
772 if (Peb
->NtGlobalFlag
& FLG_HEAP_PAGE_ALLOCS
)
774 /* Initialize target DLL */
775 AVrfPageHeapDllNotification(LdrEntry
);
778 /* Check if Application Verifier was enabled */
779 if (Peb
->NtGlobalFlag
& FLG_HEAP_ENABLE_TAIL_CHECK
)
782 DPRINT1("We don't support Application Verifier yet!\n");
785 /* Just to be safe */
786 Status
= STATUS_SUCCESS
;
790 /* Release the activation context */
791 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
797 /* FIXME: This function is missing SxS support and has wrong prototype */
800 LdrpLoadImportModule(IN PWSTR DllPath OPTIONAL
,
803 OUT PLDR_DATA_TABLE_ENTRY
*DataTableEntry
,
804 OUT PBOOLEAN Existing
)
806 ANSI_STRING AnsiString
;
807 PUNICODE_STRING ImpDescName
;
809 PPEB Peb
= RtlGetCurrentPeb();
810 PTEB Teb
= NtCurrentTeb();
812 DPRINT("LdrpLoadImportModule('%S' '%s' %p %p %p)\n", DllPath
, ImportName
, DllBase
, DataTableEntry
, Existing
);
814 /* Convert import descriptor name to unicode string */
815 ImpDescName
= &Teb
->StaticUnicodeString
;
816 RtlInitAnsiString(&AnsiString
, ImportName
);
817 Status
= RtlAnsiStringToUnicodeString(ImpDescName
, &AnsiString
, FALSE
);
818 if (!NT_SUCCESS(Status
)) return Status
;
820 /* Check if it's loaded */
821 if (LdrpCheckForLoadedDll(DllPath
,
827 /* It's already existing in the list */
829 return STATUS_SUCCESS
;
832 /* We're loading it for the first time */
836 Status
= LdrpMapDll(DllPath
,
844 if (!NT_SUCCESS(Status
)) return Status
;
846 /* Walk its import descriptor table */
847 Status
= LdrpWalkImportDescriptor(DllPath
,
849 if (!NT_SUCCESS(Status
))
851 /* Add it to the in-init-order list in case of failure */
852 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
853 &(*DataTableEntry
)->InInitializationOrderModuleList
);
861 LdrpSnapThunk(IN PVOID ExportBase
,
863 IN PIMAGE_THUNK_DATA OriginalThunk
,
864 IN OUT PIMAGE_THUNK_DATA Thunk
,
865 IN PIMAGE_EXPORT_DIRECTORY ExportEntry
,
872 ULONG OriginalOrdinal
= 0;
873 PIMAGE_IMPORT_BY_NAME AddressOfData
;
875 PUSHORT OrdinalTable
;
876 LPSTR ImportName
= NULL
;
879 ULONG_PTR HardErrorParameters
[3];
880 UNICODE_STRING HardErrorDllName
, HardErrorEntryPointName
;
881 ANSI_STRING TempString
;
884 PULONG AddressOfFunctions
;
885 UNICODE_STRING TempUString
;
886 ANSI_STRING ForwarderName
;
887 PANSI_STRING ForwardName
;
888 PVOID ForwarderHandle
;
889 ULONG ForwardOrdinal
;
891 /* Check if the snap is by ordinal */
892 if ((IsOrdinal
= IMAGE_SNAP_BY_ORDINAL(OriginalThunk
->u1
.Ordinal
)))
894 /* Get the ordinal number, and its normalized version */
895 OriginalOrdinal
= IMAGE_ORDINAL(OriginalThunk
->u1
.Ordinal
);
896 Ordinal
= (USHORT
)(OriginalOrdinal
- ExportEntry
->Base
);
900 /* First get the data VA */
901 AddressOfData
= (PIMAGE_IMPORT_BY_NAME
)
902 ((ULONG_PTR
)ImportBase
+
903 ((ULONG_PTR
)OriginalThunk
->u1
.AddressOfData
& 0xffffffff));
906 ImportName
= (LPSTR
)AddressOfData
->Name
;
908 /* Now get the VA of the Name and Ordinal Tables */
909 NameTable
= (PULONG
)((ULONG_PTR
)ExportBase
+
910 (ULONG_PTR
)ExportEntry
->AddressOfNames
);
911 OrdinalTable
= (PUSHORT
)((ULONG_PTR
)ExportBase
+
912 (ULONG_PTR
)ExportEntry
->AddressOfNameOrdinals
);
915 Hint
= AddressOfData
->Hint
;
917 /* Try to get a match by using the hint */
918 if (((ULONG
)Hint
< ExportEntry
->NumberOfNames
) &&
919 (!strcmp(ImportName
, ((LPSTR
)((ULONG_PTR
)ExportBase
+ NameTable
[Hint
])))))
921 /* We got a match, get the Ordinal from the hint */
922 Ordinal
= OrdinalTable
[Hint
];
926 /* Well bummer, hint didn't work, do it the long way */
927 Ordinal
= LdrpNameToOrdinal(ImportName
,
928 ExportEntry
->NumberOfNames
,
935 /* Check if the ordinal is invalid */
936 if ((ULONG
)Ordinal
>= ExportEntry
->NumberOfFunctions
)
939 /* Is this a static snap? */
942 /* Inform the debug log */
944 DPRINT1("Failed to snap ordinal 0x%x\n", OriginalOrdinal
);
946 DPRINT1("Failed to snap %s\n", ImportName
);
948 /* These are critical errors. Setup a string for the DLL name */
949 RtlInitAnsiString(&TempString
, DllName
? DllName
: "Unknown");
950 RtlAnsiStringToUnicodeString(&HardErrorDllName
, &TempString
, TRUE
);
952 /* Set it as the parameter */
953 HardErrorParameters
[1] = (ULONG_PTR
)&HardErrorDllName
;
956 /* Check if we have an ordinal */
959 /* Then set the ordinal as the 1st parameter */
960 HardErrorParameters
[0] = OriginalOrdinal
;
964 /* We don't, use the entrypoint. Set up a string for it */
965 RtlInitAnsiString(&TempString
, ImportName
);
966 RtlAnsiStringToUnicodeString(&HardErrorEntryPointName
,
970 /* Set it as the parameter */
971 HardErrorParameters
[0] = (ULONG_PTR
)&HardErrorEntryPointName
;
975 /* Raise the error */
976 NtRaiseHardError(IsOrdinal
? STATUS_ORDINAL_NOT_FOUND
:
977 STATUS_ENTRYPOINT_NOT_FOUND
,
984 /* Increase the error count */
985 if (LdrpInLdrInit
) LdrpFatalHardErrorCount
++;
987 /* Free our string */
988 RtlFreeUnicodeString(&HardErrorDllName
);
991 /* Free our second string. Return entrypoint error */
992 RtlFreeUnicodeString(&HardErrorEntryPointName
);
993 RtlRaiseStatus(STATUS_ENTRYPOINT_NOT_FOUND
);
996 /* Return ordinal error */
997 RtlRaiseStatus(STATUS_ORDINAL_NOT_FOUND
);
1001 /* Inform the debug log */
1003 DPRINT("Non-fatal: Failed to snap ordinal 0x%x\n", OriginalOrdinal
);
1005 DPRINT("Non-fatal: Failed to snap %s\n", ImportName
);
1008 /* Set this as a bad DLL */
1009 Thunk
->u1
.Function
= (ULONG_PTR
)0xffbadd11;
1011 /* Return the right error code */
1012 Status
= IsOrdinal
? STATUS_ORDINAL_NOT_FOUND
:
1013 STATUS_ENTRYPOINT_NOT_FOUND
;
1017 /* The ordinal seems correct, get the AddressOfFunctions VA */
1018 AddressOfFunctions
= (PULONG
)
1019 ((ULONG_PTR
)ExportBase
+
1020 (ULONG_PTR
)ExportEntry
->AddressOfFunctions
);
1022 /* Write the function pointer*/
1023 Thunk
->u1
.Function
= (ULONG_PTR
)ExportBase
+ AddressOfFunctions
[Ordinal
];
1025 /* Make sure it's within the exports */
1026 if ((Thunk
->u1
.Function
> (ULONG_PTR
)ExportEntry
) &&
1027 (Thunk
->u1
.Function
< ((ULONG_PTR
)ExportEntry
+ ExportSize
)))
1029 /* Get the Import and Forwarder Names */
1030 ImportName
= (LPSTR
)Thunk
->u1
.Function
;
1031 ForwarderName
.Buffer
= ImportName
;
1032 ForwarderName
.Length
= (USHORT
)(strchr(ImportName
, '.') - ImportName
);
1033 ForwarderName
.MaximumLength
= ForwarderName
.Length
;
1034 Status
= RtlAnsiStringToUnicodeString(&TempUString
,
1038 /* Make sure the conversion was OK */
1039 if (NT_SUCCESS(Status
))
1041 /* Load the forwarder, free the temp string */
1042 Status
= LdrpLoadDll(FALSE
,
1048 RtlFreeUnicodeString(&TempUString
);
1051 /* If the load or conversion failed, use the failure path */
1052 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1054 /* Now set up a name for the actual forwarder dll */
1055 RtlInitAnsiString(&ForwarderName
,
1056 ImportName
+ ForwarderName
.Length
+ sizeof(CHAR
));
1058 /* Check if it's an ordinal forward */
1059 if ((ForwarderName
.Length
> 1) && (*ForwarderName
.Buffer
== '#'))
1061 /* We don't have an actual function name */
1064 /* Convert the string into an ordinal */
1065 Status
= RtlCharToInteger(ForwarderName
.Buffer
+ sizeof(CHAR
),
1069 /* If this fails, then error out */
1070 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1074 /* Import by name */
1075 ForwardName
= &ForwarderName
;
1078 /* Get the pointer */
1079 Status
= LdrpGetProcedureAddress(ForwarderHandle
,
1082 (PVOID
*)&Thunk
->u1
.Function
,
1084 /* If this fails, then error out */
1085 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1089 /* It's not within the exports, let's hope it's valid */
1090 if (!AddressOfFunctions
[Ordinal
]) goto FailurePath
;
1093 /* If we got here, then it's success */
1094 Status
= STATUS_SUCCESS
;