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 PLDR_MANIFEST_PROBER_ROUTINE 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
->InInitializationOrderLinks
);
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
->InInitializationOrderLinks
);
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
->InInitializationOrderLinks
);
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
, Status2
;
690 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundEntry
= NULL
;
691 PIMAGE_IMPORT_DESCRIPTOR ImportEntry
;
692 ULONG BoundSize
, IatSize
;
694 DPRINT("LdrpWalkImportDescriptor - BEGIN (%wZ %p '%S')\n", &LdrEntry
->BaseDllName
, LdrEntry
, DllPath
);
696 /* Set up the Act Ctx */
697 RtlZeroMemory(&ActCtx
, sizeof(ActCtx
));
698 ActCtx
.Size
= sizeof(ActCtx
);
699 ActCtx
.Format
= RTL_CALLER_ALLOCATED_ACTIVATION_CONTEXT_STACK_FRAME_FORMAT_WHISTLER
;
701 /* Check if we have a manifest prober routine */
702 if (LdrpManifestProberRoutine
)
704 /* Probe the DLL for its manifest. Some details are omitted */
705 Status2
= LdrpManifestProberRoutine(LdrEntry
->DllBase
, LdrEntry
->FullDllName
.Buffer
, &LdrEntry
->EntryPointActivationContext
);
707 if (!NT_SUCCESS(Status2
) &&
708 Status2
!= STATUS_NO_SUCH_FILE
&&
709 Status2
!= STATUS_RESOURCE_DATA_NOT_FOUND
&&
710 Status2
!= STATUS_RESOURCE_TYPE_NOT_FOUND
&&
711 Status2
!= STATUS_RESOURCE_NAME_NOT_FOUND
&&
712 Status2
!= STATUS_RESOURCE_LANG_NOT_FOUND
)
714 /* Some serious issue */
715 //Status = Status2; // FIXME: Ignore that error for now
716 DbgPrintEx(DPFLTR_SXS_ID
,
717 DPFLTR_WARNING_LEVEL
,
718 "LDR: LdrpWalkImportDescriptor() failed to probe %wZ for its "
719 "manifest, ntstatus = 0x%08lx\n",
720 &LdrEntry
->FullDllName
, Status
);
724 /* Check if we failed above */
725 if (!NT_SUCCESS(Status
)) return Status
;
727 /* Get the Active ActCtx */
728 if (!LdrEntry
->EntryPointActivationContext
)
730 Status
= RtlGetActiveActivationContext(&LdrEntry
->EntryPointActivationContext
);
732 if (!NT_SUCCESS(Status
))
735 DbgPrintEx(DPFLTR_SXS_ID
,
736 DPFLTR_WARNING_LEVEL
,
737 "LDR: RtlGetActiveActivationContext() failed; ntstatus = "
744 /* Activate the ActCtx */
745 RtlActivateActivationContextUnsafeFast(&ActCtx
,
746 LdrEntry
->EntryPointActivationContext
);
748 /* Check if we were redirected */
749 if (!(LdrEntry
->Flags
& LDRP_REDIRECTED
))
751 /* Get the Bound IAT */
752 BoundEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
754 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
758 /* Get the regular IAT, for fallback */
759 ImportEntry
= RtlImageDirectoryEntryToData(LdrEntry
->DllBase
,
761 IMAGE_DIRECTORY_ENTRY_IMPORT
,
764 /* Check if we got at least one */
765 if ((BoundEntry
) || (ImportEntry
))
767 /* Do we have a Bound IAT */
770 /* Handle the descriptor */
771 Status
= LdrpHandleNewFormatImportDescriptors(DllPath
,
777 /* Handle the descriptor */
778 Status
= LdrpHandleOldFormatImportDescriptors(DllPath
,
783 /* Check the status of the handlers */
784 if (NT_SUCCESS(Status
))
786 /* Check for Per-DLL Heap Tagging */
787 if (Peb
->NtGlobalFlag
& FLG_HEAP_ENABLE_TAG_BY_DLL
)
790 DPRINT1("We don't support Per-DLL Heap Tagging yet!\n");
793 /* Check if Page Heap was enabled */
794 if (Peb
->NtGlobalFlag
& FLG_HEAP_PAGE_ALLOCS
)
796 /* Initialize target DLL */
797 AVrfPageHeapDllNotification(LdrEntry
);
800 /* Check if Application Verifier was enabled */
801 if (Peb
->NtGlobalFlag
& FLG_HEAP_ENABLE_TAIL_CHECK
)
804 DPRINT1("We don't support Application Verifier yet!\n");
807 /* Just to be safe */
808 Status
= STATUS_SUCCESS
;
812 /* Release the activation context */
813 RtlDeactivateActivationContextUnsafeFast(&ActCtx
);
815 DPRINT("LdrpWalkImportDescriptor - END (%wZ %p)\n", &LdrEntry
->BaseDllName
, LdrEntry
);
821 /* FIXME: This function is missing SxS support and has wrong prototype */
824 LdrpLoadImportModule(IN PWSTR DllPath OPTIONAL
,
827 OUT PLDR_DATA_TABLE_ENTRY
*DataTableEntry
,
828 OUT PBOOLEAN Existing
)
830 ANSI_STRING AnsiString
;
831 PUNICODE_STRING ImpDescName
;
833 PPEB Peb
= RtlGetCurrentPeb();
834 PTEB Teb
= NtCurrentTeb();
836 DPRINT("LdrpLoadImportModule('%s' %p %p %p '%S')\n", ImportName
, DllBase
, DataTableEntry
, Existing
, DllPath
);
838 /* Convert import descriptor name to unicode string */
839 ImpDescName
= &Teb
->StaticUnicodeString
;
840 RtlInitAnsiString(&AnsiString
, ImportName
);
841 Status
= RtlAnsiStringToUnicodeString(ImpDescName
, &AnsiString
, FALSE
);
842 if (!NT_SUCCESS(Status
)) return Status
;
844 /* Check if it's loaded */
845 if (LdrpCheckForLoadedDll(DllPath
,
851 /* It's already existing in the list */
853 return STATUS_SUCCESS
;
856 /* We're loading it for the first time */
862 ACTCTX_SECTION_KEYED_DATA data
;
865 //DPRINT1("find_actctx_dll for %S\n", fullname);
866 //RtlInitUnicodeString(&nameW, libname);
867 data
.cbSize
= sizeof(data
);
868 status
= RtlFindActivationContextSectionString(
869 FIND_ACTCTX_SECTION_KEY_RETURN_HACTCTX
, NULL
,
870 ACTIVATION_CONTEXT_SECTION_DLL_REDIRECTION
,
873 //if (status != STATUS_SUCCESS) return status;
874 DPRINT1("Status: 0x%08X\n", status
);
876 if (NT_SUCCESS(status
))
878 ACTIVATION_CONTEXT_ASSEMBLY_DETAILED_INFORMATION
*info
;
879 SIZE_T needed
, size
= 1024;
883 if (!(info
= RtlAllocateHeap(RtlGetProcessHeap(), 0, size
)))
885 status
= STATUS_NO_MEMORY
;
888 status
= RtlQueryInformationActivationContext(0, data
.hActCtx
, &data
.ulAssemblyRosterIndex
,
889 AssemblyDetailedInformationInActivationContext
,
890 info
, size
, &needed
);
891 if (status
== STATUS_SUCCESS
) break;
892 if (status
!= STATUS_BUFFER_TOO_SMALL
) goto done
;
893 RtlFreeHeap(RtlGetProcessHeap(), 0, info
);
897 DPRINT("manifestpath === %S\n", info
->lpAssemblyManifestPath
);
898 DPRINT("DirectoryName === %S\n", info
->lpAssemblyDirectoryName
);
905 Status
= LdrpMapDll(DllPath
,
913 if (!NT_SUCCESS(Status
)) return Status
;
915 /* Walk its import descriptor table */
916 Status
= LdrpWalkImportDescriptor(DllPath
,
918 if (!NT_SUCCESS(Status
))
920 /* Add it to the in-init-order list in case of failure */
921 InsertTailList(&Peb
->Ldr
->InInitializationOrderModuleList
,
922 &(*DataTableEntry
)->InInitializationOrderLinks
);
930 LdrpSnapThunk(IN PVOID ExportBase
,
932 IN PIMAGE_THUNK_DATA OriginalThunk
,
933 IN OUT PIMAGE_THUNK_DATA Thunk
,
934 IN PIMAGE_EXPORT_DIRECTORY ExportEntry
,
941 ULONG OriginalOrdinal
= 0;
942 PIMAGE_IMPORT_BY_NAME AddressOfData
;
944 PUSHORT OrdinalTable
;
945 LPSTR ImportName
= NULL
;
948 ULONG_PTR HardErrorParameters
[3];
949 UNICODE_STRING HardErrorDllName
, HardErrorEntryPointName
;
950 ANSI_STRING TempString
;
953 PULONG AddressOfFunctions
;
954 UNICODE_STRING TempUString
;
955 ANSI_STRING ForwarderName
;
956 PANSI_STRING ForwardName
;
957 PVOID ForwarderHandle
;
958 ULONG ForwardOrdinal
;
960 /* Check if the snap is by ordinal */
961 if ((IsOrdinal
= IMAGE_SNAP_BY_ORDINAL(OriginalThunk
->u1
.Ordinal
)))
963 /* Get the ordinal number, and its normalized version */
964 OriginalOrdinal
= IMAGE_ORDINAL(OriginalThunk
->u1
.Ordinal
);
965 Ordinal
= (USHORT
)(OriginalOrdinal
- ExportEntry
->Base
);
969 /* First get the data VA */
970 AddressOfData
= (PIMAGE_IMPORT_BY_NAME
)
971 ((ULONG_PTR
)ImportBase
+
972 ((ULONG_PTR
)OriginalThunk
->u1
.AddressOfData
& 0xffffffff));
975 ImportName
= (LPSTR
)AddressOfData
->Name
;
977 /* Now get the VA of the Name and Ordinal Tables */
978 NameTable
= (PULONG
)((ULONG_PTR
)ExportBase
+
979 (ULONG_PTR
)ExportEntry
->AddressOfNames
);
980 OrdinalTable
= (PUSHORT
)((ULONG_PTR
)ExportBase
+
981 (ULONG_PTR
)ExportEntry
->AddressOfNameOrdinals
);
984 Hint
= AddressOfData
->Hint
;
986 /* Try to get a match by using the hint */
987 if (((ULONG
)Hint
< ExportEntry
->NumberOfNames
) &&
988 (!strcmp(ImportName
, ((LPSTR
)((ULONG_PTR
)ExportBase
+ NameTable
[Hint
])))))
990 /* We got a match, get the Ordinal from the hint */
991 Ordinal
= OrdinalTable
[Hint
];
995 /* Well bummer, hint didn't work, do it the long way */
996 Ordinal
= LdrpNameToOrdinal(ImportName
,
997 ExportEntry
->NumberOfNames
,
1004 /* Check if the ordinal is invalid */
1005 if ((ULONG
)Ordinal
>= ExportEntry
->NumberOfFunctions
)
1008 /* Is this a static snap? */
1011 /* Inform the debug log */
1013 DPRINT1("Failed to snap ordinal 0x%x\n", OriginalOrdinal
);
1015 DPRINT1("Failed to snap %s\n", ImportName
);
1017 /* These are critical errors. Setup a string for the DLL name */
1018 RtlInitAnsiString(&TempString
, DllName
? DllName
: "Unknown");
1019 RtlAnsiStringToUnicodeString(&HardErrorDllName
, &TempString
, TRUE
);
1021 /* Set it as the parameter */
1022 HardErrorParameters
[1] = (ULONG_PTR
)&HardErrorDllName
;
1025 /* Check if we have an ordinal */
1028 /* Then set the ordinal as the 1st parameter */
1029 HardErrorParameters
[0] = OriginalOrdinal
;
1033 /* We don't, use the entrypoint. Set up a string for it */
1034 RtlInitAnsiString(&TempString
, ImportName
);
1035 RtlAnsiStringToUnicodeString(&HardErrorEntryPointName
,
1039 /* Set it as the parameter */
1040 HardErrorParameters
[0] = (ULONG_PTR
)&HardErrorEntryPointName
;
1044 /* Raise the error */
1045 NtRaiseHardError(IsOrdinal
? STATUS_ORDINAL_NOT_FOUND
:
1046 STATUS_ENTRYPOINT_NOT_FOUND
,
1049 HardErrorParameters
,
1053 /* Increase the error count */
1054 if (LdrpInLdrInit
) LdrpFatalHardErrorCount
++;
1056 /* Free our string */
1057 RtlFreeUnicodeString(&HardErrorDllName
);
1060 /* Free our second string. Return entrypoint error */
1061 RtlFreeUnicodeString(&HardErrorEntryPointName
);
1062 RtlRaiseStatus(STATUS_ENTRYPOINT_NOT_FOUND
);
1065 /* Return ordinal error */
1066 RtlRaiseStatus(STATUS_ORDINAL_NOT_FOUND
);
1070 /* Inform the debug log */
1072 DPRINT("Non-fatal: Failed to snap ordinal 0x%x\n", OriginalOrdinal
);
1074 DPRINT("Non-fatal: Failed to snap %s\n", ImportName
);
1077 /* Set this as a bad DLL */
1078 Thunk
->u1
.Function
= (ULONG_PTR
)0xffbadd11;
1080 /* Return the right error code */
1081 Status
= IsOrdinal
? STATUS_ORDINAL_NOT_FOUND
:
1082 STATUS_ENTRYPOINT_NOT_FOUND
;
1086 /* The ordinal seems correct, get the AddressOfFunctions VA */
1087 AddressOfFunctions
= (PULONG
)
1088 ((ULONG_PTR
)ExportBase
+
1089 (ULONG_PTR
)ExportEntry
->AddressOfFunctions
);
1091 /* Write the function pointer*/
1092 Thunk
->u1
.Function
= (ULONG_PTR
)ExportBase
+ AddressOfFunctions
[Ordinal
];
1094 /* Make sure it's within the exports */
1095 if ((Thunk
->u1
.Function
> (ULONG_PTR
)ExportEntry
) &&
1096 (Thunk
->u1
.Function
< ((ULONG_PTR
)ExportEntry
+ ExportSize
)))
1098 /* Get the Import and Forwarder Names */
1099 ImportName
= (LPSTR
)Thunk
->u1
.Function
;
1100 ForwarderName
.Buffer
= ImportName
;
1101 ForwarderName
.Length
= (USHORT
)(strchr(ImportName
, '.') - ImportName
);
1102 ForwarderName
.MaximumLength
= ForwarderName
.Length
;
1103 Status
= RtlAnsiStringToUnicodeString(&TempUString
,
1107 /* Make sure the conversion was OK */
1108 if (NT_SUCCESS(Status
))
1110 /* Load the forwarder, free the temp string */
1111 Status
= LdrpLoadDll(FALSE
,
1117 RtlFreeUnicodeString(&TempUString
);
1120 /* If the load or conversion failed, use the failure path */
1121 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1123 /* Now set up a name for the actual forwarder dll */
1124 RtlInitAnsiString(&ForwarderName
,
1125 ImportName
+ ForwarderName
.Length
+ sizeof(CHAR
));
1127 /* Check if it's an ordinal forward */
1128 if ((ForwarderName
.Length
> 1) && (*ForwarderName
.Buffer
== '#'))
1130 /* We don't have an actual function name */
1133 /* Convert the string into an ordinal */
1134 Status
= RtlCharToInteger(ForwarderName
.Buffer
+ sizeof(CHAR
),
1138 /* If this fails, then error out */
1139 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1143 /* Import by name */
1144 ForwardName
= &ForwarderName
;
1147 /* Get the pointer */
1148 Status
= LdrpGetProcedureAddress(ForwarderHandle
,
1151 (PVOID
*)&Thunk
->u1
.Function
,
1153 /* If this fails, then error out */
1154 if (!NT_SUCCESS(Status
)) goto FailurePath
;
1158 /* It's not within the exports, let's hope it's valid */
1159 if (!AddressOfFunctions
[Ordinal
]) goto FailurePath
;
1162 /* If we got here, then it's success */
1163 Status
= STATUS_SUCCESS
;