2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * FILE: lib/ntdll/ldr/utils.c
5 * PURPOSE: Process startup for PE executables
6 * PROGRAMMERS: Jean Michault
7 * Rex Jolliff (rex@lvcablemodem.com)
12 * - Handle loading flags correctly
13 * - Handle errors correctly (unload dll's)
14 * - Implement a faster way to find modules (hash table)
18 /* INCLUDES *****************************************************************/
24 #define LDRP_PROCESS_CREATION_TIME 0x8000000
26 /* GLOBALS *******************************************************************/
30 #define TRACE_LDR(args...) if (RtlGetNtGlobalFlags() & FLG_SHOW_LDR_SNAPS) { DbgPrint("(LDR:%s:%d) ",__FILE__,__LINE__); DbgPrint(args); }
31 #elif defined(_MSC_VER)
32 #define TRACE_LDR(args, ...) if (RtlGetNtGlobalFlags() & FLG_SHOW_LDR_SNAPS) { DbgPrint("(LDR:%s:%d) ",__FILE__,__LINE__); DbgPrint(__VA_ARGS__); }
35 #define TRACE_LDR(args...) do { DbgPrint("(LDR:%s:%d) ",__FILE__,__LINE__); DbgPrint(args); } while(0)
38 typedef struct _TLS_DATA
40 PVOID StartAddressOfRawData
;
43 PIMAGE_TLS_CALLBACK
*TlsAddressOfCallBacks
;
44 PLDR_DATA_TABLE_ENTRY Module
;
45 } TLS_DATA
, *PTLS_DATA
;
47 static BOOLEAN LdrpDllShutdownInProgress
= FALSE
;
48 static PTLS_DATA LdrpTlsArray
= NULL
;
49 static ULONG LdrpTlsCount
= 0;
50 static ULONG LdrpTlsSize
= 0;
51 static HANDLE LdrpKnownDllsDirHandle
= NULL
;
52 static UNICODE_STRING LdrpKnownDllPath
= {0, 0, NULL
};
53 static PLDR_DATA_TABLE_ENTRY LdrpLastModule
= NULL
;
54 extern PLDR_DATA_TABLE_ENTRY ExeModule
;
56 /* PROTOTYPES ****************************************************************/
58 static NTSTATUS
LdrFindEntryForName(PUNICODE_STRING Name
, PLDR_DATA_TABLE_ENTRY
*Module
, BOOLEAN Ref
);
59 static PVOID
LdrFixupForward(PCHAR ForwardName
);
60 static PVOID
LdrGetExportByName(PVOID BaseAddress
, PUCHAR SymbolName
, USHORT Hint
);
61 static NTSTATUS
LdrpLoadModule(IN PWSTR SearchPath OPTIONAL
,
63 IN PUNICODE_STRING Name
,
64 OUT PLDR_DATA_TABLE_ENTRY
*Module
,
65 OUT PVOID
*BaseAddress OPTIONAL
);
66 static NTSTATUS
LdrpAttachProcess(VOID
);
67 static VOID
LdrpDetachProcess(BOOLEAN UnloadAll
);
69 /* FUNCTIONS *****************************************************************/
71 #if defined(DBG) || defined(KDBG)
74 LdrpLoadUserModuleSymbols(PLDR_DATA_TABLE_ENTRY LdrModule
)
85 #endif /* DBG || KDBG */
88 LdrMappedAsDataFile(PVOID
*BaseAddress
)
90 if (0 != ((DWORD_PTR
) *BaseAddress
& (PAGE_SIZE
- 1)))
92 *BaseAddress
= (PVOID
) ((DWORD_PTR
) *BaseAddress
& ~ ((DWORD_PTR
) PAGE_SIZE
- 1));
99 static __inline LONG
LdrpDecrementLoadCount(PLDR_DATA_TABLE_ENTRY Module
, BOOLEAN Locked
)
104 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
106 LoadCount
= Module
->LoadCount
;
107 if (Module
->LoadCount
> 0 && Module
->LoadCount
!= 0xFFFF)
113 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
118 static __inline LONG
LdrpIncrementLoadCount(PLDR_DATA_TABLE_ENTRY Module
, BOOLEAN Locked
)
123 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
125 LoadCount
= Module
->LoadCount
;
126 if (Module
->LoadCount
!= 0xFFFF)
132 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
137 static __inline VOID
LdrpAcquireTlsSlot(PLDR_DATA_TABLE_ENTRY Module
, ULONG Size
, BOOLEAN Locked
)
141 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
143 Module
->TlsIndex
= (SHORT
)LdrpTlsCount
;
148 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
152 static __inline VOID
LdrpTlsCallback(PLDR_DATA_TABLE_ENTRY Module
, ULONG dwReason
)
154 PIMAGE_TLS_CALLBACK
*TlsCallback
;
155 if (Module
->TlsIndex
!= 0xFFFF && Module
->LoadCount
== 0xFFFF)
157 TlsCallback
= LdrpTlsArray
[Module
->TlsIndex
].TlsAddressOfCallBacks
;
162 TRACE_LDR("%wZ - Calling tls callback at %x\n",
163 &Module
->BaseDllName
, *TlsCallback
);
164 (*TlsCallback
)(Module
->DllBase
, dwReason
, NULL
);
171 static BOOLEAN
LdrpCallDllEntry(PLDR_DATA_TABLE_ENTRY Module
, DWORD dwReason
, PVOID lpReserved
)
173 if (!(Module
->Flags
& LDRP_IMAGE_DLL
) ||
174 Module
->EntryPoint
== 0)
178 LdrpTlsCallback(Module
, dwReason
);
179 return ((PDLLMAIN_FUNC
)Module
->EntryPoint
)(Module
->DllBase
, dwReason
, lpReserved
);
183 LdrpInitializeTlsForThread(VOID
)
189 PTEB Teb
= NtCurrentTeb();
191 DPRINT("LdrpInitializeTlsForThread() called for %wZ\n", &ExeModule
->BaseDllName
);
193 Teb
->StaticUnicodeString
.Length
= 0;
194 Teb
->StaticUnicodeString
.MaximumLength
= sizeof(Teb
->StaticUnicodeBuffer
);
195 Teb
->StaticUnicodeString
.Buffer
= Teb
->StaticUnicodeBuffer
;
197 if (LdrpTlsCount
> 0)
199 TlsPointers
= RtlAllocateHeap(RtlGetProcessHeap(),
201 LdrpTlsCount
* sizeof(PVOID
) + LdrpTlsSize
);
202 if (TlsPointers
== NULL
)
204 DPRINT1("failed to allocate thread tls data\n");
205 return STATUS_NO_MEMORY
;
208 TlsData
= (PVOID
)((ULONG_PTR
)TlsPointers
+ LdrpTlsCount
* sizeof(PVOID
));
209 Teb
->ThreadLocalStoragePointer
= TlsPointers
;
211 TlsInfo
= LdrpTlsArray
;
212 for (i
= 0; i
< LdrpTlsCount
; i
++, TlsInfo
++)
214 TRACE_LDR("Initialize tls data for %wZ\n", &TlsInfo
->Module
->BaseDllName
);
215 TlsPointers
[i
] = TlsData
;
216 if (TlsInfo
->TlsDataSize
)
218 memcpy(TlsData
, TlsInfo
->StartAddressOfRawData
, TlsInfo
->TlsDataSize
);
219 TlsData
= (PVOID
)((ULONG_PTR
)TlsData
+ TlsInfo
->TlsDataSize
);
221 if (TlsInfo
->TlsZeroSize
)
223 memset(TlsData
, 0, TlsInfo
->TlsZeroSize
);
224 TlsData
= (PVOID
)((ULONG_PTR
)TlsData
+ TlsInfo
->TlsZeroSize
);
228 DPRINT("LdrpInitializeTlsForThread() done\n");
229 return STATUS_SUCCESS
;
233 LdrpInitializeTlsForProccess(VOID
)
235 PLIST_ENTRY ModuleListHead
;
237 PLDR_DATA_TABLE_ENTRY Module
;
238 PIMAGE_TLS_DIRECTORY TlsDirectory
;
242 DPRINT("LdrpInitializeTlsForProccess() called for %wZ\n", &ExeModule
->BaseDllName
);
244 if (LdrpTlsCount
> 0)
246 LdrpTlsArray
= RtlAllocateHeap(RtlGetProcessHeap(),
248 LdrpTlsCount
* sizeof(TLS_DATA
));
249 if (LdrpTlsArray
== NULL
)
251 DPRINT1("Failed to allocate global tls data\n");
252 return STATUS_NO_MEMORY
;
255 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
256 Entry
= ModuleListHead
->Flink
;
257 while (Entry
!= ModuleListHead
)
259 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
260 if (Module
->LoadCount
== 0xFFFF &&
261 Module
->TlsIndex
!= 0xFFFF)
263 TlsDirectory
= (PIMAGE_TLS_DIRECTORY
)
264 RtlImageDirectoryEntryToData(Module
->DllBase
,
266 IMAGE_DIRECTORY_ENTRY_TLS
,
268 ASSERT(Module
->TlsIndex
< LdrpTlsCount
);
269 TlsData
= &LdrpTlsArray
[Module
->TlsIndex
];
270 TlsData
->StartAddressOfRawData
= (PVOID
)TlsDirectory
->StartAddressOfRawData
;
271 TlsData
->TlsDataSize
= TlsDirectory
->EndAddressOfRawData
- TlsDirectory
->StartAddressOfRawData
;
272 TlsData
->TlsZeroSize
= TlsDirectory
->SizeOfZeroFill
;
273 if (TlsDirectory
->AddressOfCallBacks
)
274 TlsData
->TlsAddressOfCallBacks
= (PIMAGE_TLS_CALLBACK
*)TlsDirectory
->AddressOfCallBacks
;
276 TlsData
->TlsAddressOfCallBacks
= NULL
;
277 TlsData
->Module
= Module
;
279 DbgPrint("TLS directory for %wZ\n", &Module
->BaseDllName
);
280 DbgPrint("StartAddressOfRawData: %x\n", TlsDirectory
->StartAddressOfRawData
);
281 DbgPrint("EndAddressOfRawData: %x\n", TlsDirectory
->EndAddressOfRawData
);
282 DbgPrint("SizeOfRawData: %d\n", TlsDirectory
->EndAddressOfRawData
- TlsDirectory
->StartAddressOfRawData
);
283 DbgPrint("AddressOfIndex: %x\n", TlsDirectory
->AddressOfIndex
);
284 DbgPrint("AddressOfCallBacks: %x (%x)\n", TlsDirectory
->AddressOfCallBacks
, *TlsDirectory
->AddressOfCallBacks
);
285 DbgPrint("SizeOfZeroFill: %d\n", TlsDirectory
->SizeOfZeroFill
);
286 DbgPrint("Characteristics: %x\n", TlsDirectory
->Characteristics
);
290 * Is this region allways writable ?
292 *(PULONG
)TlsDirectory
->AddressOfIndex
= Module
->TlsIndex
;
294 Entry
= Entry
->Flink
;
297 DPRINT("LdrpInitializeTlsForProccess() done\n");
298 return STATUS_SUCCESS
;
304 OBJECT_ATTRIBUTES ObjectAttributes
;
305 UNICODE_STRING LinkTarget
;
311 DPRINT("LdrpInitLoader() called for %wZ\n", &ExeModule
->BaseDllName
);
313 /* Get handle to the 'KnownDlls' directory */
314 RtlInitUnicodeString(&Name
,
316 InitializeObjectAttributes(&ObjectAttributes
,
318 OBJ_CASE_INSENSITIVE
,
321 Status
= NtOpenDirectoryObject(&LdrpKnownDllsDirHandle
,
322 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
324 if (!NT_SUCCESS(Status
))
326 DPRINT("NtOpenDirectoryObject() failed (Status %lx)\n", Status
);
327 LdrpKnownDllsDirHandle
= NULL
;
331 /* Allocate target name string */
332 LinkTarget
.Length
= 0;
333 LinkTarget
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
334 LinkTarget
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
336 MAX_PATH
* sizeof(WCHAR
));
337 if (LinkTarget
.Buffer
== NULL
)
339 NtClose(LdrpKnownDllsDirHandle
);
340 LdrpKnownDllsDirHandle
= NULL
;
344 RtlInitUnicodeString(&Name
,
346 InitializeObjectAttributes(&ObjectAttributes
,
348 OBJ_CASE_INSENSITIVE
,
349 LdrpKnownDllsDirHandle
,
351 Status
= NtOpenSymbolicLinkObject(&LinkHandle
,
352 SYMBOLIC_LINK_ALL_ACCESS
,
354 if (!NT_SUCCESS(Status
))
356 RtlFreeUnicodeString(&LinkTarget
);
357 NtClose(LdrpKnownDllsDirHandle
);
358 LdrpKnownDllsDirHandle
= NULL
;
362 Status
= NtQuerySymbolicLinkObject(LinkHandle
,
366 if (!NT_SUCCESS(Status
))
368 RtlFreeUnicodeString(&LinkTarget
);
369 NtClose(LdrpKnownDllsDirHandle
);
370 LdrpKnownDllsDirHandle
= NULL
;
373 RtlCreateUnicodeString(&LdrpKnownDllPath
,
376 RtlFreeUnicodeString(&LinkTarget
);
378 DPRINT("LdrpInitLoader() done\n");
382 /***************************************************************************
387 * Adjusts the name of a dll to a fully qualified name.
390 * FullDllName: Pointer to caller supplied storage for the fully
391 * qualified dll name.
392 * DllName: Pointer to the dll name.
393 * BaseName: TRUE: Only the file name is passed to FullDllName
394 * FALSE: The full path is preserved in FullDllName
402 * A given path is not affected by the adjustment, but the file
404 * ntdll --> ntdll.dll
406 * ntdll.xyz --> ntdll.xyz
409 LdrAdjustDllName (PUNICODE_STRING FullDllName
,
410 PUNICODE_STRING DllName
,
413 WCHAR Buffer
[MAX_PATH
];
418 Length
= DllName
->Length
/ sizeof(WCHAR
);
422 /* get the base dll name */
423 Pointer
= DllName
->Buffer
+ Length
;
430 while (Pointer
>= DllName
->Buffer
&& *Pointer
!= L
'\\' && *Pointer
!= L
'/');
433 Length
= Extension
- Pointer
;
434 memmove (Buffer
, Pointer
, Length
* sizeof(WCHAR
));
435 Buffer
[Length
] = L
'\0';
439 /* get the full dll name */
440 memmove (Buffer
, DllName
->Buffer
, DllName
->Length
);
441 Buffer
[DllName
->Length
/ sizeof(WCHAR
)] = L
'\0';
444 /* Build the DLL's absolute name */
445 Extension
= wcsrchr (Buffer
, L
'.');
446 if ((Extension
!= NULL
) && (*Extension
== L
'.'))
448 /* with extension - remove dot if it's the last character */
449 if (Buffer
[Length
- 1] == L
'.')
455 /* name without extension - assume that it is .dll */
456 memmove (Buffer
+ Length
, L
".dll", 10);
459 RtlCreateUnicodeString(FullDllName
, Buffer
);
462 PLDR_DATA_TABLE_ENTRY
463 LdrAddModuleEntry(PVOID ImageBase
,
464 PIMAGE_NT_HEADERS NTHeaders
,
467 PLDR_DATA_TABLE_ENTRY Module
;
469 Module
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof (LDR_DATA_TABLE_ENTRY
));
471 memset(Module
, 0, sizeof(LDR_DATA_TABLE_ENTRY
));
472 Module
->DllBase
= (PVOID
)ImageBase
;
473 Module
->EntryPoint
= (PVOID
)NTHeaders
->OptionalHeader
.AddressOfEntryPoint
;
474 if (Module
->EntryPoint
!= 0)
475 Module
->EntryPoint
= (PVOID
)((ULONG_PTR
)Module
->EntryPoint
+ (ULONG_PTR
)Module
->DllBase
);
476 Module
->SizeOfImage
= LdrpGetResidentSize(NTHeaders
);
477 if (NtCurrentPeb()->Ldr
->Initialized
== TRUE
)
479 /* loading while app is running */
480 Module
->LoadCount
= 1;
483 * loading while app is initializing
484 * dll must not be unloaded
486 Module
->LoadCount
= 0xFFFF;
490 Module
->TlsIndex
= -1;
491 Module
->CheckSum
= NTHeaders
->OptionalHeader
.CheckSum
;
492 Module
->TimeDateStamp
= NTHeaders
->FileHeader
.TimeDateStamp
;
494 RtlCreateUnicodeString (&Module
->FullDllName
,
496 RtlCreateUnicodeString (&Module
->BaseDllName
,
497 wcsrchr(FullDosName
, L
'\\') + 1);
498 DPRINT ("BaseDllName %wZ\n", &Module
->BaseDllName
);
500 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
501 InsertTailList(&NtCurrentPeb()->Ldr
->InLoadOrderModuleList
,
502 &Module
->InLoadOrderLinks
);
503 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
510 LdrpMapKnownDll(IN PUNICODE_STRING DllName
,
511 OUT PUNICODE_STRING FullDosName
,
512 OUT PHANDLE SectionHandle
)
514 OBJECT_ATTRIBUTES ObjectAttributes
;
517 DPRINT("LdrpMapKnownDll() called\n");
519 if (LdrpKnownDllsDirHandle
== NULL
)
521 DPRINT("Invalid 'KnownDlls' directory\n");
522 return STATUS_UNSUCCESSFUL
;
525 DPRINT("LdrpKnownDllPath '%wZ'\n", &LdrpKnownDllPath
);
527 InitializeObjectAttributes(&ObjectAttributes
,
529 OBJ_CASE_INSENSITIVE
,
530 LdrpKnownDllsDirHandle
,
532 Status
= NtOpenSection(SectionHandle
,
533 SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
,
535 if (!NT_SUCCESS(Status
))
537 DPRINT("NtOpenSection() failed for '%wZ' (Status 0x%08lx)\n", DllName
, Status
);
541 FullDosName
->Length
= LdrpKnownDllPath
.Length
+ DllName
->Length
+ sizeof(WCHAR
);
542 FullDosName
->MaximumLength
= FullDosName
->Length
+ sizeof(WCHAR
);
543 FullDosName
->Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
545 FullDosName
->MaximumLength
);
546 if (FullDosName
->Buffer
== NULL
)
548 FullDosName
->Length
= 0;
549 FullDosName
->MaximumLength
= 0;
550 return STATUS_SUCCESS
;
553 wcscpy(FullDosName
->Buffer
, LdrpKnownDllPath
.Buffer
);
554 wcscat(FullDosName
->Buffer
, L
"\\");
555 wcscat(FullDosName
->Buffer
, DllName
->Buffer
);
557 DPRINT("FullDosName '%wZ'\n", FullDosName
);
559 DPRINT("LdrpMapKnownDll() done\n");
561 return STATUS_SUCCESS
;
566 LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL
,
567 IN PUNICODE_STRING DllName
,
568 OUT PUNICODE_STRING FullDosName
,
569 IN BOOLEAN MapAsDataFile
,
570 OUT PHANDLE SectionHandle
)
572 WCHAR SearchPathBuffer
[MAX_PATH
];
573 WCHAR DosName
[MAX_PATH
];
574 UNICODE_STRING FullNtFileName
;
575 OBJECT_ATTRIBUTES FileObjectAttributes
;
577 char BlockBuffer
[1024];
578 PIMAGE_DOS_HEADER DosHeader
;
579 PIMAGE_NT_HEADERS NTHeaders
;
580 IO_STATUS_BLOCK IoStatusBlock
;
584 DPRINT("LdrpMapDllImageFile() called\n");
586 if (SearchPath
== NULL
)
588 /* get application running path */
590 wcscpy (SearchPathBuffer
, NtCurrentPeb()->ProcessParameters
->ImagePathName
.Buffer
);
592 len
= wcslen (SearchPathBuffer
);
594 while (len
&& SearchPathBuffer
[len
- 1] != L
'\\')
597 if (len
) SearchPathBuffer
[len
-1] = L
'\0';
599 wcscat (SearchPathBuffer
, L
";");
601 wcscat (SearchPathBuffer
, SharedUserData
->NtSystemRoot
);
602 wcscat (SearchPathBuffer
, L
"\\system32;");
603 wcscat (SearchPathBuffer
, SharedUserData
->NtSystemRoot
);
604 wcscat (SearchPathBuffer
, L
";.");
606 SearchPath
= SearchPathBuffer
;
609 if (RtlDosSearchPath_U (SearchPath
,
615 return STATUS_DLL_NOT_FOUND
;
618 if (!RtlDosPathNameToNtPathName_U (DosName
,
622 return STATUS_DLL_NOT_FOUND
;
624 DPRINT("FullNtFileName %wZ\n", &FullNtFileName
);
626 InitializeObjectAttributes(&FileObjectAttributes
,
632 DPRINT("Opening dll \"%wZ\"\n", &FullNtFileName
);
634 Status
= NtOpenFile(&FileHandle
,
635 GENERIC_READ
|SYNCHRONIZE
,
636 &FileObjectAttributes
,
639 FILE_SYNCHRONOUS_IO_NONALERT
);
640 if (!NT_SUCCESS(Status
))
642 DPRINT1("Dll open of %wZ failed: Status = 0x%08lx\n",
643 &FullNtFileName
, Status
);
644 RtlFreeHeap (RtlGetProcessHeap (),
646 FullNtFileName
.Buffer
);
649 RtlFreeHeap (RtlGetProcessHeap (),
651 FullNtFileName
.Buffer
);
656 Status
= NtReadFile(FileHandle
,
665 if (!NT_SUCCESS(Status
))
667 DPRINT("Dll header read failed: Status = 0x%08lx\n", Status
);
673 * Overlay DOS and NT headers structures to the
674 * buffer with DLL's header raw data.
676 DosHeader
= (PIMAGE_DOS_HEADER
) BlockBuffer
;
677 NTHeaders
= (PIMAGE_NT_HEADERS
) (BlockBuffer
+ DosHeader
->e_lfanew
);
679 * Check it is a PE image file.
681 if ((DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
682 || (DosHeader
->e_lfanew
== 0L)
683 || (*(PULONG
)(NTHeaders
) != IMAGE_NT_SIGNATURE
))
685 DPRINT("NTDLL format invalid\n");
688 return STATUS_UNSUCCESSFUL
;
693 * Create a section for dll.
695 Status
= NtCreateSection(SectionHandle
,
700 MapAsDataFile
? SEC_COMMIT
: SEC_IMAGE
,
704 if (!NT_SUCCESS(Status
))
706 DPRINT("NTDLL create section failed: Status = 0x%08lx\n", Status
);
710 RtlCreateUnicodeString(FullDosName
,
718 /***************************************************************************
735 LdrLoadDll (IN PWSTR SearchPath OPTIONAL
,
736 IN PULONG LoadFlags OPTIONAL
,
737 IN PUNICODE_STRING Name
,
738 OUT PVOID
*BaseAddress
/* also known as HMODULE*, and PHANDLE 'DllHandle' */)
741 PLDR_DATA_TABLE_ENTRY Module
;
743 PPEB Peb
= NtCurrentPeb();
745 TRACE_LDR("LdrLoadDll, loading %wZ%s%S\n",
747 SearchPath
? L
" from " : L
"",
748 SearchPath
? SearchPath
: L
"");
750 Status
= LdrpLoadModule(SearchPath
, LoadFlags
? *LoadFlags
: 0, Name
, &Module
, BaseAddress
);
752 if (NT_SUCCESS(Status
) &&
753 (!LoadFlags
|| 0 == (*LoadFlags
& LOAD_LIBRARY_AS_DATAFILE
)))
755 if (!(Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
))
757 RtlEnterCriticalSection(Peb
->LoaderLock
);
758 Status
= LdrpAttachProcess();
759 RtlLeaveCriticalSection(Peb
->LoaderLock
);
763 if ((!Module
) && (NT_SUCCESS(Status
)))
766 *BaseAddress
= NT_SUCCESS(Status
) ? Module
->DllBase
: NULL
;
772 /***************************************************************************
774 * LdrFindEntryForAddress
789 LdrFindEntryForAddress(PVOID Address
,
790 PLDR_DATA_TABLE_ENTRY
*Module
)
792 PLIST_ENTRY ModuleListHead
;
794 PLDR_DATA_TABLE_ENTRY ModulePtr
;
796 DPRINT("LdrFindEntryForAddress(Address %p)\n", Address
);
798 if (NtCurrentPeb()->Ldr
== NULL
)
799 return(STATUS_NO_MORE_ENTRIES
);
801 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
802 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
803 Entry
= ModuleListHead
->Flink
;
804 if (Entry
== ModuleListHead
)
806 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
807 return(STATUS_NO_MORE_ENTRIES
);
810 while (Entry
!= ModuleListHead
)
812 ModulePtr
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
814 DPRINT("Scanning %wZ at %p\n", &ModulePtr
->BaseDllName
, ModulePtr
->DllBase
);
816 if ((Address
>= ModulePtr
->DllBase
) &&
817 ((ULONG_PTR
)Address
<= ((ULONG_PTR
)ModulePtr
->DllBase
+ ModulePtr
->SizeOfImage
)))
820 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
821 return(STATUS_SUCCESS
);
824 Entry
= Entry
->Flink
;
827 DPRINT("Failed to find module entry.\n");
829 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
830 return(STATUS_NO_MORE_ENTRIES
);
834 /***************************************************************************
836 * LdrFindEntryForName
850 LdrFindEntryForName(PUNICODE_STRING Name
,
851 PLDR_DATA_TABLE_ENTRY
*Module
,
854 PLIST_ENTRY ModuleListHead
;
856 PLDR_DATA_TABLE_ENTRY ModulePtr
;
857 BOOLEAN ContainsPath
;
858 UNICODE_STRING AdjustedName
;
861 DPRINT("LdrFindEntryForName(Name %wZ)\n", Name
);
863 if (NtCurrentPeb()->Ldr
== NULL
)
864 return(STATUS_NO_MORE_ENTRIES
);
866 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
867 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
868 Entry
= ModuleListHead
->Flink
;
869 if (Entry
== ModuleListHead
)
871 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
872 return(STATUS_NO_MORE_ENTRIES
);
875 // NULL is the current process
879 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
880 return(STATUS_SUCCESS
);
883 LdrAdjustDllName (&AdjustedName
, Name
, FALSE
);
885 ContainsPath
= (AdjustedName
.Length
>= 2 * sizeof(WCHAR
) && L
':' == AdjustedName
.Buffer
[1]);
886 for (i
= 0; ! ContainsPath
&& i
< AdjustedName
.Length
/ sizeof(WCHAR
); i
++)
888 ContainsPath
= L
'\\' == AdjustedName
.Buffer
[i
] ||
889 L
'/' == AdjustedName
.Buffer
[i
];
894 if ((! ContainsPath
&&
895 0 == RtlCompareUnicodeString(&LdrpLastModule
->BaseDllName
, &AdjustedName
, TRUE
)) ||
897 0 == RtlCompareUnicodeString(&LdrpLastModule
->FullDllName
, &AdjustedName
, TRUE
)))
899 *Module
= LdrpLastModule
;
900 if (Ref
&& (*Module
)->LoadCount
!= 0xFFFF)
902 (*Module
)->LoadCount
++;
904 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
905 RtlFreeUnicodeString(&AdjustedName
);
906 return(STATUS_SUCCESS
);
909 while (Entry
!= ModuleListHead
)
911 ModulePtr
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
913 DPRINT("Scanning %wZ %wZ\n", &ModulePtr
->BaseDllName
, &AdjustedName
);
915 if ((! ContainsPath
&&
916 0 == RtlCompareUnicodeString(&ModulePtr
->BaseDllName
, &AdjustedName
, TRUE
)) ||
918 0 == RtlCompareUnicodeString(&ModulePtr
->FullDllName
, &AdjustedName
, TRUE
)))
920 *Module
= LdrpLastModule
= ModulePtr
;
921 if (Ref
&& ModulePtr
->LoadCount
!= 0xFFFF)
923 ModulePtr
->LoadCount
++;
925 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
926 RtlFreeUnicodeString(&AdjustedName
);
927 return(STATUS_SUCCESS
);
930 Entry
= Entry
->Flink
;
933 DPRINT("Failed to find dll %wZ\n", Name
);
934 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
935 RtlFreeUnicodeString(&AdjustedName
);
936 return(STATUS_NO_MORE_ENTRIES
);
939 /**********************************************************************
955 LdrFixupForward(PCHAR ForwardName
)
957 CHAR NameBuffer
[128];
958 UNICODE_STRING DllName
;
961 PLDR_DATA_TABLE_ENTRY Module
;
964 strcpy(NameBuffer
, ForwardName
);
965 p
= strchr(NameBuffer
, '.');
970 DPRINT("Dll: %s Function: %s\n", NameBuffer
, p
+1);
971 RtlCreateUnicodeStringFromAsciiz (&DllName
,
974 Status
= LdrFindEntryForName (&DllName
, &Module
, FALSE
);
976 * The caller (or the image) is responsible for loading of the dll, where the function is forwarded.
978 if (!NT_SUCCESS(Status
))
980 ULONG Flags
= LDRP_PROCESS_CREATION_TIME
;
981 Status
= LdrLoadDll(NULL
,
985 if (NT_SUCCESS(Status
))
987 Status
= LdrFindEntryForName (&DllName
, &Module
, FALSE
);
990 RtlFreeUnicodeString (&DllName
);
991 if (!NT_SUCCESS(Status
))
993 DPRINT1("LdrFixupForward: failed to load %s\n", NameBuffer
);
997 DPRINT("BaseAddress: %p\n", Module
->DllBase
);
999 return LdrGetExportByName(Module
->DllBase
, (PUCHAR
)(p
+1), -1);
1006 /**********************************************************************
1008 * LdrGetExportByOrdinal
1022 LdrGetExportByOrdinal (
1027 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1028 ULONG ExportDirSize
;
1029 PDWORD
* ExFunctions
;
1032 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)
1033 RtlImageDirectoryEntryToData (BaseAddress
,
1035 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1039 ExFunctions
= (PDWORD
*)
1042 ExportDir
->AddressOfFunctions
1045 "LdrGetExportByOrdinal(Ordinal %lu) = %p\n",
1047 RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
] )
1050 Function
= (0 != ExFunctions
[Ordinal
- ExportDir
->Base
]
1051 ? RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
] )
1054 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1055 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1057 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1058 Function
= LdrFixupForward((PCHAR
)Function
);
1065 /**********************************************************************
1067 * LdrGetExportByName
1078 * AddressOfNames and AddressOfNameOrdinals are paralell tables,
1079 * both with NumberOfNames entries.
1083 LdrGetExportByName(PVOID BaseAddress
,
1087 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1088 PDWORD
* ExFunctions
;
1090 USHORT
* ExOrdinals
;
1095 ULONG ExportDirSize
;
1097 DPRINT("LdrGetExportByName %p %s %hu\n", BaseAddress
, SymbolName
, Hint
);
1099 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)
1100 RtlImageDirectoryEntryToData(BaseAddress
,
1102 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1104 if (ExportDir
== NULL
)
1106 DPRINT1("LdrGetExportByName(): no export directory, "
1107 "can't lookup %s/%hu!\n", SymbolName
, Hint
);
1112 //The symbol names may be missing entirely
1113 if (ExportDir
->AddressOfNames
== 0)
1115 DPRINT("LdrGetExportByName(): symbol names missing entirely\n");
1120 * Get header pointers
1122 ExNames
= (PDWORD
*)RVA(BaseAddress
,
1123 ExportDir
->AddressOfNames
);
1124 ExOrdinals
= (USHORT
*)RVA(BaseAddress
,
1125 ExportDir
->AddressOfNameOrdinals
);
1126 ExFunctions
= (PDWORD
*)RVA(BaseAddress
,
1127 ExportDir
->AddressOfFunctions
);
1130 * Check the hint first
1132 if (Hint
< ExportDir
->NumberOfNames
)
1134 ExName
= RVA(BaseAddress
, ExNames
[Hint
]);
1135 if (strcmp(ExName
, (PCHAR
)SymbolName
) == 0)
1137 Ordinal
= ExOrdinals
[Hint
];
1138 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1139 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1140 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1142 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1143 Function
= LdrFixupForward((PCHAR
)Function
);
1144 if (Function
== NULL
)
1146 DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName
);
1150 if (Function
!= NULL
)
1159 maxn
= ExportDir
->NumberOfNames
- 1;
1160 while (minn
<= maxn
)
1165 mid
= (minn
+ maxn
) / 2;
1167 ExName
= RVA(BaseAddress
, ExNames
[mid
]);
1168 res
= strcmp(ExName
, (PCHAR
)SymbolName
);
1171 Ordinal
= ExOrdinals
[mid
];
1172 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1173 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1174 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1176 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1177 Function
= LdrFixupForward((PCHAR
)Function
);
1178 if (Function
== NULL
)
1180 DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName
);
1184 if (Function
!= NULL
)
1187 else if (minn
== maxn
)
1189 DPRINT("LdrGetExportByName(): binary search failed\n");
1202 DPRINT("LdrGetExportByName(): failed to find %s\n",SymbolName
);
1207 /**********************************************************************
1209 * LdrPerformRelocations
1212 * Relocate a DLL's memory image.
1224 LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders
,
1227 PIMAGE_DATA_DIRECTORY RelocationDDir
;
1228 PIMAGE_BASE_RELOCATION RelocationDir
, RelocationEnd
;
1229 ULONG Count
, ProtectSize
, OldProtect
, OldProtect2
;
1230 PVOID Page
, ProtectPage
, ProtectPage2
;
1235 if (NTHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1237 return STATUS_SUCCESS
;
1241 &NTHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1243 if (RelocationDDir
->VirtualAddress
== 0 || RelocationDDir
->Size
== 0)
1245 return STATUS_SUCCESS
;
1248 ProtectSize
= PAGE_SIZE
;
1249 Delta
= (ULONG_PTR
)ImageBase
- NTHeaders
->OptionalHeader
.ImageBase
;
1250 RelocationDir
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)ImageBase
+
1251 RelocationDDir
->VirtualAddress
);
1252 RelocationEnd
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)ImageBase
+
1253 RelocationDDir
->VirtualAddress
+ RelocationDDir
->Size
);
1255 while (RelocationDir
< RelocationEnd
&&
1256 RelocationDir
->SizeOfBlock
> 0)
1258 Count
= (RelocationDir
->SizeOfBlock
- sizeof(IMAGE_BASE_RELOCATION
)) /
1260 Page
= (PVOID
)((ULONG_PTR
)ImageBase
+ (ULONG_PTR
)RelocationDir
->VirtualAddress
);
1261 TypeOffset
= (PUSHORT
)(RelocationDir
+ 1);
1263 /* Unprotect the page(s) we're about to relocate. */
1265 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1270 if (!NT_SUCCESS(Status
))
1272 DPRINT1("Failed to unprotect relocation target.\n");
1276 if (RelocationDir
->VirtualAddress
+ PAGE_SIZE
<
1277 NTHeaders
->OptionalHeader
.SizeOfImage
)
1279 ProtectPage2
= (PVOID
)((ULONG_PTR
)ProtectPage
+ PAGE_SIZE
);
1280 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1285 if (!NT_SUCCESS(Status
))
1287 DPRINT1("Failed to unprotect relocation target (2).\n");
1288 NtProtectVirtualMemory(NtCurrentProcess(),
1298 ProtectPage2
= NULL
;
1301 RelocationDir
= LdrProcessRelocationBlock((ULONG_PTR
)Page
,
1305 if (RelocationDir
== NULL
)
1306 return STATUS_UNSUCCESSFUL
;
1308 /* Restore old page protection. */
1309 NtProtectVirtualMemory(NtCurrentProcess(),
1315 if (ProtectPage2
!= NULL
)
1317 NtProtectVirtualMemory(NtCurrentProcess(),
1325 return STATUS_SUCCESS
;
1329 LdrpGetOrLoadModule(PWCHAR SearchPath
,
1331 PLDR_DATA_TABLE_ENTRY
* Module
,
1334 ANSI_STRING AnsiDllName
;
1335 UNICODE_STRING DllName
;
1338 DPRINT("LdrpGetOrLoadModule() called for %s\n", Name
);
1340 RtlInitAnsiString(&AnsiDllName
, Name
);
1341 Status
= RtlAnsiStringToUnicodeString(&DllName
, &AnsiDllName
, TRUE
);
1342 if (!NT_SUCCESS(Status
))
1347 Status
= LdrFindEntryForName (&DllName
, Module
, Load
);
1348 if (Load
&& !NT_SUCCESS(Status
))
1350 Status
= LdrpLoadModule(SearchPath
,
1351 NtCurrentPeb()->Ldr
->Initialized
? 0 : LDRP_PROCESS_CREATION_TIME
,
1355 if (NT_SUCCESS(Status
))
1357 Status
= LdrFindEntryForName (&DllName
, Module
, FALSE
);
1359 if (!NT_SUCCESS(Status
))
1361 ULONG ErrorResponse
;
1362 ULONG_PTR ErrorParameter
= (ULONG_PTR
)&DllName
;
1364 DPRINT1("failed to load %wZ\n", &DllName
);
1365 NtRaiseHardError(STATUS_DLL_NOT_FOUND
,
1373 RtlFreeUnicodeString (&DllName
);
1378 RtlpRaiseImportNotFound(CHAR
*FuncName
, ULONG Ordinal
, PUNICODE_STRING DllName
)
1380 ULONG ErrorResponse
;
1381 ULONG_PTR ErrorParameters
[2];
1382 ANSI_STRING ProcNameAnsi
;
1383 UNICODE_STRING ProcName
;
1388 _snprintf(Buffer
, 8, "# %ld", Ordinal
);
1392 RtlInitAnsiString(&ProcNameAnsi
, FuncName
);
1393 RtlAnsiStringToUnicodeString(&ProcName
, &ProcNameAnsi
, TRUE
);
1394 ErrorParameters
[0] = (ULONG_PTR
)&ProcName
;
1395 ErrorParameters
[1] = (ULONG_PTR
)DllName
;
1396 NtRaiseHardError(STATUS_ENTRYPOINT_NOT_FOUND
,
1402 RtlFreeUnicodeString(&ProcName
);
1406 LdrpProcessImportDirectoryEntry(PLDR_DATA_TABLE_ENTRY Module
,
1407 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1408 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
)
1411 PVOID
* ImportAddressList
;
1412 PULONG FunctionNameList
;
1418 if (ImportModuleDirectory
== NULL
|| ImportModuleDirectory
->Name
== 0)
1420 return STATUS_UNSUCCESSFUL
;
1423 /* Get the import address list. */
1424 ImportAddressList
= (PVOID
*)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1426 /* Get the list of functions to import. */
1427 if (ImportModuleDirectory
->OriginalFirstThunk
!= 0)
1429 FunctionNameList
= (PULONG
) ((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->OriginalFirstThunk
);
1433 FunctionNameList
= (PULONG
)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1436 /* Get the size of IAT. */
1438 while (FunctionNameList
[IATSize
] != 0L)
1443 /* Unprotect the region we are about to write into. */
1444 IATBase
= (PVOID
)ImportAddressList
;
1445 IATSize
*= sizeof(PVOID
*);
1446 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1451 if (!NT_SUCCESS(Status
))
1453 DPRINT1("Failed to unprotect IAT.\n");
1457 /* Walk through function list and fixup addresses. */
1458 while (*FunctionNameList
!= 0L)
1460 if ((*FunctionNameList
) & 0x80000000)
1462 Ordinal
= (*FunctionNameList
) & 0x7fffffff;
1463 *ImportAddressList
= LdrGetExportByOrdinal(ImportedModule
->DllBase
, Ordinal
);
1464 if ((*ImportAddressList
) == NULL
)
1466 DPRINT1("Failed to import #%ld from %wZ\n", Ordinal
, &ImportedModule
->FullDllName
);
1467 RtlpRaiseImportNotFound(NULL
, Ordinal
, &ImportedModule
->FullDllName
);
1468 return STATUS_ENTRYPOINT_NOT_FOUND
;
1473 IMAGE_IMPORT_BY_NAME
*pe_name
;
1474 pe_name
= RVA(Module
->DllBase
, *FunctionNameList
);
1475 *ImportAddressList
= LdrGetExportByName(ImportedModule
->DllBase
, pe_name
->Name
, pe_name
->Hint
);
1476 if ((*ImportAddressList
) == NULL
)
1478 DPRINT1("Failed to import %s from %wZ\n", pe_name
->Name
, &ImportedModule
->FullDllName
);
1479 RtlpRaiseImportNotFound((CHAR
*)pe_name
->Name
, 0, &ImportedModule
->FullDllName
);
1480 return STATUS_ENTRYPOINT_NOT_FOUND
;
1483 ImportAddressList
++;
1487 /* Protect the region we are about to write into. */
1488 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1493 if (!NT_SUCCESS(Status
))
1495 DPRINT1("Failed to protect IAT.\n");
1499 return STATUS_SUCCESS
;
1503 LdrpProcessImportDirectory(
1504 PLDR_DATA_TABLE_ENTRY Module
,
1505 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1509 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1513 DPRINT("LdrpProcessImportDirectory(%p '%wZ', '%s')\n",
1514 Module
, &Module
->BaseDllName
, ImportedName
);
1517 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1518 RtlImageDirectoryEntryToData(Module
->DllBase
,
1520 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1522 if (ImportModuleDirectory
== NULL
)
1524 return STATUS_UNSUCCESSFUL
;
1527 while (ImportModuleDirectory
->Name
)
1529 Name
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectory
->Name
;
1530 if (0 == _stricmp(Name
, ImportedName
))
1532 Status
= LdrpProcessImportDirectoryEntry(Module
,
1534 ImportModuleDirectory
);
1535 if (!NT_SUCCESS(Status
))
1540 ImportModuleDirectory
++;
1544 return STATUS_SUCCESS
;
1549 LdrpAdjustImportDirectory(PLDR_DATA_TABLE_ENTRY Module
,
1550 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1553 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1555 PVOID
* ImportAddressList
;
1558 PULONG FunctionNameList
;
1563 PIMAGE_NT_HEADERS NTHeaders
;
1567 DPRINT("LdrpAdjustImportDirectory(Module %p '%wZ', %p '%wZ', '%s')\n",
1568 Module
, &Module
->BaseDllName
, ImportedModule
, &ImportedModule
->BaseDllName
, ImportedName
);
1570 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1571 RtlImageDirectoryEntryToData(Module
->DllBase
,
1573 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1575 if (ImportModuleDirectory
== NULL
)
1577 return STATUS_UNSUCCESSFUL
;
1580 while (ImportModuleDirectory
->Name
)
1582 Name
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectory
->Name
;
1583 if (0 == _stricmp(Name
, (PCHAR
)ImportedName
))
1586 /* Get the import address list. */
1587 ImportAddressList
= (PVOID
*)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1589 /* Get the list of functions to import. */
1590 if (ImportModuleDirectory
->OriginalFirstThunk
!= 0)
1592 FunctionNameList
= (PULONG
) ((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->OriginalFirstThunk
);
1596 FunctionNameList
= (PULONG
)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1599 /* Get the size of IAT. */
1601 while (FunctionNameList
[IATSize
] != 0L)
1606 /* Unprotect the region we are about to write into. */
1607 IATBase
= (PVOID
)ImportAddressList
;
1608 IATSize
*= sizeof(PVOID
*);
1609 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1614 if (!NT_SUCCESS(Status
))
1616 DPRINT1("Failed to unprotect IAT.\n");
1620 NTHeaders
= RtlImageNtHeader (ImportedModule
->DllBase
);
1621 Start
= (PVOID
)NTHeaders
->OptionalHeader
.ImageBase
;
1622 End
= (PVOID
)((ULONG_PTR
)Start
+ ImportedModule
->SizeOfImage
);
1623 Offset
= (ULONG
)((ULONG_PTR
)ImportedModule
->DllBase
- (ULONG_PTR
)Start
);
1625 /* Walk through function list and fixup addresses. */
1626 while (*FunctionNameList
!= 0L)
1628 if (*ImportAddressList
>= Start
&& *ImportAddressList
< End
)
1630 (*ImportAddressList
) = (PVOID
)((ULONG_PTR
)(*ImportAddressList
) + Offset
);
1632 ImportAddressList
++;
1636 /* Protect the region we are about to write into. */
1637 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1642 if (!NT_SUCCESS(Status
))
1644 DPRINT1("Failed to protect IAT.\n");
1648 ImportModuleDirectory
++;
1650 return STATUS_SUCCESS
;
1654 /**********************************************************************
1659 * Compute the entry point for every symbol the DLL imports
1660 * from other modules.
1672 LdrFixupImports(IN PWSTR SearchPath OPTIONAL
,
1673 IN PLDR_DATA_TABLE_ENTRY Module
)
1675 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1676 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectoryCurrent
;
1677 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptor
;
1678 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptorCurrent
;
1679 PIMAGE_TLS_DIRECTORY TlsDirectory
;
1682 PLDR_DATA_TABLE_ENTRY ImportedModule
;
1686 DPRINT("LdrFixupImports(SearchPath %S, Module %p)\n", SearchPath
, Module
);
1688 /* Check for tls data */
1689 TlsDirectory
= (PIMAGE_TLS_DIRECTORY
)
1690 RtlImageDirectoryEntryToData(Module
->DllBase
,
1692 IMAGE_DIRECTORY_ENTRY_TLS
,
1696 TlsSize
= TlsDirectory
->EndAddressOfRawData
1697 - TlsDirectory
->StartAddressOfRawData
1698 + TlsDirectory
->SizeOfZeroFill
;
1700 NtCurrentPeb()->Ldr
->Initialized
)
1702 TRACE_LDR("Trying to load dynamicly %wZ which contains a tls directory\n",
1703 &Module
->BaseDllName
);
1704 return STATUS_UNSUCCESSFUL
;
1708 * Process each import module.
1710 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1711 RtlImageDirectoryEntryToData(Module
->DllBase
,
1713 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1716 BoundImportDescriptor
= (PIMAGE_BOUND_IMPORT_DESCRIPTOR
)
1717 RtlImageDirectoryEntryToData(Module
->DllBase
,
1719 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
1722 if (BoundImportDescriptor
!= NULL
&& ImportModuleDirectory
== NULL
)
1724 DPRINT1("%wZ has only a bound import directory\n", &Module
->BaseDllName
);
1725 return STATUS_UNSUCCESSFUL
;
1727 if (BoundImportDescriptor
)
1729 DPRINT("BoundImportDescriptor %p\n", BoundImportDescriptor
);
1731 BoundImportDescriptorCurrent
= BoundImportDescriptor
;
1732 while (BoundImportDescriptorCurrent
->OffsetModuleName
)
1734 ImportedName
= (PCHAR
)BoundImportDescriptor
+ BoundImportDescriptorCurrent
->OffsetModuleName
;
1735 TRACE_LDR("%wZ bound to %s\n", &Module
->BaseDllName
, ImportedName
);
1736 Status
= LdrpGetOrLoadModule(SearchPath
, ImportedName
, &ImportedModule
, TRUE
);
1737 if (!NT_SUCCESS(Status
))
1739 DPRINT1("failed to load %s\n", ImportedName
);
1742 if (Module
== ImportedModule
)
1744 LdrpDecrementLoadCount(Module
, FALSE
);
1746 if (ImportedModule
->TimeDateStamp
!= BoundImportDescriptorCurrent
->TimeDateStamp
)
1748 TRACE_LDR("%wZ has stale binding to %wZ\n",
1749 &Module
->BaseDllName
, &ImportedModule
->BaseDllName
);
1750 Status
= LdrpProcessImportDirectory(Module
, ImportedModule
, ImportedName
);
1751 if (!NT_SUCCESS(Status
))
1753 DPRINT1("failed to import %s\n", ImportedName
);
1759 BOOLEAN WrongForwarder
;
1760 WrongForwarder
= FALSE
;
1761 if (ImportedModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1763 TRACE_LDR("%wZ has stale binding to %s\n",
1764 &Module
->BaseDllName
, ImportedName
);
1768 TRACE_LDR("%wZ has correct binding to %wZ\n",
1769 &Module
->BaseDllName
, &ImportedModule
->BaseDllName
);
1771 if (BoundImportDescriptorCurrent
->NumberOfModuleForwarderRefs
)
1773 PIMAGE_BOUND_FORWARDER_REF BoundForwarderRef
;
1775 PLDR_DATA_TABLE_ENTRY ForwarderModule
;
1776 PCHAR ForwarderName
;
1778 BoundForwarderRef
= (PIMAGE_BOUND_FORWARDER_REF
)(BoundImportDescriptorCurrent
+ 1);
1779 for (i
= 0; i
< BoundImportDescriptorCurrent
->NumberOfModuleForwarderRefs
; i
++, BoundForwarderRef
++)
1781 ForwarderName
= (PCHAR
)BoundImportDescriptor
+ BoundForwarderRef
->OffsetModuleName
;
1782 TRACE_LDR("%wZ bound to %s via forwardes from %s\n",
1783 &Module
->BaseDllName
, ForwarderName
, ImportedName
);
1784 Status
= LdrpGetOrLoadModule(SearchPath
, ForwarderName
, &ForwarderModule
, TRUE
);
1785 if (!NT_SUCCESS(Status
))
1787 DPRINT1("failed to load %s\n", ForwarderName
);
1790 if (Module
== ImportedModule
)
1792 LdrpDecrementLoadCount(Module
, FALSE
);
1794 if (ForwarderModule
->TimeDateStamp
!= BoundForwarderRef
->TimeDateStamp
||
1795 ForwarderModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1797 TRACE_LDR("%wZ has stale binding to %s\n",
1798 &Module
->BaseDllName
, ForwarderName
);
1799 WrongForwarder
= TRUE
;
1803 TRACE_LDR("%wZ has correct binding to %s\n",
1804 &Module
->BaseDllName
, ForwarderName
);
1808 if (WrongForwarder
||
1809 ImportedModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1811 Status
= LdrpProcessImportDirectory(Module
, ImportedModule
, ImportedName
);
1812 if (!NT_SUCCESS(Status
))
1814 DPRINT1("failed to import %s\n", ImportedName
);
1818 else if (ImportedModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1820 TRACE_LDR("Adjust imports for %s from %wZ\n",
1821 ImportedName
, &Module
->BaseDllName
);
1822 Status
= LdrpAdjustImportDirectory(Module
, ImportedModule
, ImportedName
);
1823 if (!NT_SUCCESS(Status
))
1825 DPRINT1("failed to adjust import entries for %s\n", ImportedName
);
1829 else if (WrongForwarder
)
1833 * Update only forwarders
1835 TRACE_LDR("Stale BIND %s from %wZ\n",
1836 ImportedName
, &Module
->BaseDllName
);
1837 Status
= LdrpProcessImportDirectory(Module
, ImportedModule
, ImportedName
);
1838 if (!NT_SUCCESS(Status
))
1840 DPRINT1("faild to import %s\n", ImportedName
);
1849 BoundImportDescriptorCurrent
+= BoundImportDescriptorCurrent
->NumberOfModuleForwarderRefs
+ 1;
1852 else if (ImportModuleDirectory
)
1854 DPRINT("ImportModuleDirectory %p\n", ImportModuleDirectory
);
1856 ImportModuleDirectoryCurrent
= ImportModuleDirectory
;
1857 while (ImportModuleDirectoryCurrent
->Name
)
1859 ImportedName
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectoryCurrent
->Name
;
1860 TRACE_LDR("%wZ imports functions from %s\n", &Module
->BaseDllName
, ImportedName
);
1862 Status
= LdrpGetOrLoadModule(SearchPath
, ImportedName
, &ImportedModule
, TRUE
);
1863 if (!NT_SUCCESS(Status
))
1865 DPRINT1("failed to load %s\n", ImportedName
);
1868 if (Module
== ImportedModule
)
1870 LdrpDecrementLoadCount(Module
, FALSE
);
1873 TRACE_LDR("Initializing imports for %wZ from %s\n",
1874 &Module
->BaseDllName
, ImportedName
);
1875 Status
= LdrpProcessImportDirectoryEntry(Module
, ImportedModule
, ImportModuleDirectoryCurrent
);
1876 if (!NT_SUCCESS(Status
))
1878 DPRINT1("failed to import %s\n", ImportedName
);
1881 ImportModuleDirectoryCurrent
++;
1885 if (TlsDirectory
&& TlsSize
> 0)
1887 LdrpAcquireTlsSlot(Module
, TlsSize
, FALSE
);
1890 return STATUS_SUCCESS
;
1894 /**********************************************************************
1899 * 1. Relocate, if needed the EXE.
1900 * 2. Fixup any imported symbol.
1901 * 3. Compute the EXE's entry point.
1905 * Address at which the EXE's image
1909 * Handle of the section that contains
1913 * NULL on error; otherwise the entry point
1914 * to call for initializing the DLL.
1919 * 04.01.2004 hb Previous this function was used for all images (dll + exe).
1920 * Currently the function is only used for the exe.
1922 PEPFUNC
LdrPEStartup (PVOID ImageBase
,
1923 HANDLE SectionHandle
,
1924 PLDR_DATA_TABLE_ENTRY
* Module
,
1928 PEPFUNC EntryPoint
= NULL
;
1929 PIMAGE_DOS_HEADER DosHeader
;
1930 PIMAGE_NT_HEADERS NTHeaders
;
1931 PLDR_DATA_TABLE_ENTRY tmpModule
;
1933 DPRINT("LdrPEStartup(ImageBase %p SectionHandle %p)\n",
1934 ImageBase
, SectionHandle
);
1937 * Overlay DOS and WNT headers structures
1938 * to the DLL's image.
1940 DosHeader
= (PIMAGE_DOS_HEADER
) ImageBase
;
1941 NTHeaders
= (PIMAGE_NT_HEADERS
) ((ULONG_PTR
)ImageBase
+ DosHeader
->e_lfanew
);
1944 * If the base address is different from the
1945 * one the DLL is actually loaded, perform any
1948 if (ImageBase
!= (PVOID
) NTHeaders
->OptionalHeader
.ImageBase
)
1950 DPRINT("LDR: Performing relocations\n");
1951 Status
= LdrPerformRelocations(NTHeaders
, ImageBase
);
1952 if (!NT_SUCCESS(Status
))
1954 DPRINT1("LdrPerformRelocations() failed\n");
1961 *Module
= LdrAddModuleEntry(ImageBase
, NTHeaders
, FullDosName
);
1962 (*Module
)->SectionPointer
= SectionHandle
;
1966 Module
= &tmpModule
;
1967 Status
= LdrFindEntryForAddress(ImageBase
, Module
);
1968 if (!NT_SUCCESS(Status
))
1974 if (ImageBase
!= (PVOID
) NTHeaders
->OptionalHeader
.ImageBase
)
1976 (*Module
)->Flags
|= LDRP_IMAGE_NOT_AT_BASE
;
1980 * If the DLL's imports symbols from other
1981 * modules, fixup the imported calls entry points.
1983 DPRINT("About to fixup imports\n");
1984 Status
= LdrFixupImports(NULL
, *Module
);
1985 if (!NT_SUCCESS(Status
))
1987 DPRINT1("LdrFixupImports() failed for %wZ\n", &(*Module
)->BaseDllName
);
1990 DPRINT("Fixup done\n");
1991 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
1992 Status
= LdrpInitializeTlsForProccess();
1993 if (NT_SUCCESS(Status
))
1995 Status
= LdrpAttachProcess();
1997 if (NT_SUCCESS(Status
))
1999 LdrpTlsCallback(*Module
, DLL_PROCESS_ATTACH
);
2003 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
2004 if (!NT_SUCCESS(Status
))
2010 * Compute the DLL's entry point's address.
2012 DPRINT("ImageBase = %p\n", ImageBase
);
2013 DPRINT("AddressOfEntryPoint = 0x%lx\n",(ULONG
)NTHeaders
->OptionalHeader
.AddressOfEntryPoint
);
2014 if (NTHeaders
->OptionalHeader
.AddressOfEntryPoint
!= 0)
2016 EntryPoint
= (PEPFUNC
) ((ULONG_PTR
)ImageBase
2017 + NTHeaders
->OptionalHeader
.AddressOfEntryPoint
);
2019 DPRINT("LdrPEStartup() = %p\n",EntryPoint
);
2024 LdrpLoadModule(IN PWSTR SearchPath OPTIONAL
,
2026 IN PUNICODE_STRING Name
,
2027 PLDR_DATA_TABLE_ENTRY
*Module
,
2028 PVOID
*BaseAddress OPTIONAL
)
2030 UNICODE_STRING AdjustedName
;
2031 UNICODE_STRING FullDosName
;
2033 PLDR_DATA_TABLE_ENTRY tmpModule
;
2034 HANDLE SectionHandle
;
2037 PIMAGE_NT_HEADERS NtHeaders
;
2038 BOOLEAN MappedAsDataFile
;
2039 PVOID ArbitraryUserPointer
;
2043 Module
= &tmpModule
;
2045 /* adjust the full dll name */
2046 LdrAdjustDllName(&AdjustedName
, Name
, FALSE
);
2048 DPRINT("%wZ\n", &AdjustedName
);
2050 MappedAsDataFile
= FALSE
;
2051 /* Test if dll is already loaded */
2052 Status
= LdrFindEntryForName(&AdjustedName
, Module
, TRUE
);
2053 if (NT_SUCCESS(Status
))
2055 RtlFreeUnicodeString(&AdjustedName
);
2056 if (NULL
!= BaseAddress
)
2058 *BaseAddress
= (*Module
)->DllBase
;
2063 /* Open or create dll image section */
2064 Status
= LdrpMapKnownDll(&AdjustedName
, &FullDosName
, &SectionHandle
);
2065 if (!NT_SUCCESS(Status
))
2067 MappedAsDataFile
= (0 != (LoadFlags
& LOAD_LIBRARY_AS_DATAFILE
));
2068 Status
= LdrpMapDllImageFile(SearchPath
, &AdjustedName
, &FullDosName
,
2069 MappedAsDataFile
, &SectionHandle
);
2071 if (!NT_SUCCESS(Status
))
2073 DPRINT1("Failed to create or open dll section of '%wZ' (Status %lx)\n", &AdjustedName
, Status
);
2074 RtlFreeUnicodeString(&AdjustedName
);
2077 RtlFreeUnicodeString(&AdjustedName
);
2078 /* Map the dll into the process */
2081 ArbitraryUserPointer
= NtCurrentTeb()->Tib
.ArbitraryUserPointer
;
2082 NtCurrentTeb()->Tib
.ArbitraryUserPointer
= FullDosName
.Buffer
;
2083 Status
= NtMapViewOfSection(SectionHandle
,
2093 NtCurrentTeb()->Tib
.ArbitraryUserPointer
= ArbitraryUserPointer
;
2094 if (!NT_SUCCESS(Status
))
2096 DPRINT1("map view of section failed (Status 0x%08lx)\n", Status
);
2097 RtlFreeUnicodeString(&FullDosName
);
2098 NtClose(SectionHandle
);
2101 if (NULL
!= BaseAddress
)
2103 *BaseAddress
= ImageBase
;
2105 if (!MappedAsDataFile
)
2107 /* Get and check the NT headers */
2108 NtHeaders
= RtlImageNtHeader(ImageBase
);
2109 if (NtHeaders
== NULL
)
2111 DPRINT1("RtlImageNtHeaders() failed\n");
2112 NtUnmapViewOfSection (NtCurrentProcess (), ImageBase
);
2113 NtClose (SectionHandle
);
2114 RtlFreeUnicodeString(&FullDosName
);
2115 return STATUS_UNSUCCESSFUL
;
2118 DPRINT("Mapped %wZ at %x\n", &FullDosName
, ImageBase
);
2119 if (MappedAsDataFile
)
2121 ASSERT(NULL
!= BaseAddress
);
2122 if (NULL
!= BaseAddress
)
2124 *BaseAddress
= (PVOID
) ((char *) *BaseAddress
+ 1);
2127 RtlFreeUnicodeString(&FullDosName
);
2128 NtClose(SectionHandle
);
2129 return STATUS_SUCCESS
;
2131 /* If the base address is different from the
2132 * one the DLL is actually loaded, perform any
2134 if (ImageBase
!= (PVOID
) NtHeaders
->OptionalHeader
.ImageBase
)
2136 DPRINT1("Relocating (%lx -> %p) %wZ\n",
2137 NtHeaders
->OptionalHeader
.ImageBase
, ImageBase
, &FullDosName
);
2138 Status
= LdrPerformRelocations(NtHeaders
, ImageBase
);
2139 if (!NT_SUCCESS(Status
))
2141 DPRINT1("LdrPerformRelocations() failed\n");
2142 NtUnmapViewOfSection (NtCurrentProcess (), ImageBase
);
2143 NtClose (SectionHandle
);
2144 RtlFreeUnicodeString(&FullDosName
);
2145 return STATUS_UNSUCCESSFUL
;
2148 *Module
= LdrAddModuleEntry(ImageBase
, NtHeaders
, FullDosName
.Buffer
);
2149 (*Module
)->SectionPointer
= SectionHandle
;
2150 if (ImageBase
!= (PVOID
) NtHeaders
->OptionalHeader
.ImageBase
)
2152 (*Module
)->Flags
|= LDRP_IMAGE_NOT_AT_BASE
;
2154 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
2156 (*Module
)->Flags
|= LDRP_IMAGE_DLL
;
2158 /* fixup the imported calls entry points */
2159 Status
= LdrFixupImports(SearchPath
, *Module
);
2160 if (!NT_SUCCESS(Status
))
2162 DPRINT1("LdrFixupImports failed for %wZ, status=%x\n", &(*Module
)->BaseDllName
, Status
);
2165 #if defined(DBG) || defined(KDBG)
2166 LdrpLoadUserModuleSymbols(*Module
);
2167 #endif /* DBG || KDBG */
2168 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
2169 InsertTailList(&NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
,
2170 &(*Module
)->InInitializationOrderModuleList
);
2171 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2173 return STATUS_SUCCESS
;
2177 LdrpUnloadModule(PLDR_DATA_TABLE_ENTRY Module
,
2180 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
2181 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptor
;
2182 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptorCurrent
;
2184 PLDR_DATA_TABLE_ENTRY ImportedModule
;
2191 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
2194 LoadCount
= LdrpDecrementLoadCount(Module
, Unload
);
2196 TRACE_LDR("Unload %wZ, LoadCount %d\n", &Module
->BaseDllName
, LoadCount
);
2200 /* ?????????????????? */
2202 else if (!(Module
->Flags
& LDRP_STATIC_LINK
) && LoadCount
== 1)
2204 BoundImportDescriptor
= (PIMAGE_BOUND_IMPORT_DESCRIPTOR
)
2205 RtlImageDirectoryEntryToData(Module
->DllBase
,
2207 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
2209 if (BoundImportDescriptor
)
2211 /* dereferencing all imported modules, use the bound import descriptor */
2212 BoundImportDescriptorCurrent
= BoundImportDescriptor
;
2213 while (BoundImportDescriptorCurrent
->OffsetModuleName
)
2215 ImportedName
= (PCHAR
)BoundImportDescriptor
+ BoundImportDescriptorCurrent
->OffsetModuleName
;
2216 TRACE_LDR("%wZ trys to unload %s\n", &Module
->BaseDllName
, ImportedName
);
2217 Status
= LdrpGetOrLoadModule(NULL
, ImportedName
, &ImportedModule
, FALSE
);
2218 if (!NT_SUCCESS(Status
))
2220 DPRINT1("unable to found imported modul %s\n", ImportedName
);
2224 if (Module
!= ImportedModule
)
2226 Status
= LdrpUnloadModule(ImportedModule
, FALSE
);
2227 if (!NT_SUCCESS(Status
))
2229 DPRINT1("unable to unload %s\n", ImportedName
);
2233 BoundImportDescriptorCurrent
++;
2238 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
2239 RtlImageDirectoryEntryToData(Module
->DllBase
,
2241 IMAGE_DIRECTORY_ENTRY_IMPORT
,
2243 if (ImportModuleDirectory
)
2245 /* dereferencing all imported modules, use the import descriptor */
2246 while (ImportModuleDirectory
->Name
)
2248 ImportedName
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectory
->Name
;
2249 TRACE_LDR("%wZ trys to unload %s\n", &Module
->BaseDllName
, ImportedName
);
2250 Status
= LdrpGetOrLoadModule(NULL
, ImportedName
, &ImportedModule
, FALSE
);
2251 if (!NT_SUCCESS(Status
))
2253 DPRINT1("unable to found imported modul %s\n", ImportedName
);
2257 if (Module
!= ImportedModule
)
2259 Status
= LdrpUnloadModule(ImportedModule
, FALSE
);
2260 if (!NT_SUCCESS(Status
))
2262 DPRINT1("unable to unload %s\n", ImportedName
);
2266 ImportModuleDirectory
++;
2274 if (!(Module
->Flags
& LDRP_STATIC_LINK
))
2276 LdrpDetachProcess(FALSE
);
2279 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2281 return STATUS_SUCCESS
;
2289 LdrUnloadDll (IN PVOID BaseAddress
)
2291 PLDR_DATA_TABLE_ENTRY Module
;
2294 if (BaseAddress
== NULL
)
2295 return STATUS_SUCCESS
;
2297 if (LdrMappedAsDataFile(&BaseAddress
))
2299 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
2303 Status
= LdrFindEntryForAddress(BaseAddress
, &Module
);
2304 if (NT_SUCCESS(Status
))
2306 TRACE_LDR("LdrUnloadDll, , unloading %wZ\n", &Module
->BaseDllName
);
2307 Status
= LdrpUnloadModule(Module
, TRUE
);
2318 LdrDisableThreadCalloutsForDll(IN PVOID BaseAddress
)
2320 PLIST_ENTRY ModuleListHead
;
2322 PLDR_DATA_TABLE_ENTRY Module
;
2325 DPRINT("LdrDisableThreadCalloutsForDll (BaseAddress %p)\n", BaseAddress
);
2327 Status
= STATUS_DLL_NOT_FOUND
;
2328 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2329 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2330 Entry
= ModuleListHead
->Flink
;
2331 while (Entry
!= ModuleListHead
)
2333 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2335 DPRINT("BaseDllName %wZ BaseAddress %p\n", &Module
->BaseDllName
, Module
->DllBase
);
2337 if (Module
->DllBase
== BaseAddress
)
2339 if (Module
->TlsIndex
== 0xFFFF)
2341 Module
->Flags
|= LDRP_DONT_CALL_FOR_THREADS
;
2342 Status
= STATUS_SUCCESS
;
2346 Entry
= Entry
->Flink
;
2348 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2356 LdrGetDllHandle(IN PWSTR DllPath OPTIONAL
,
2357 IN PULONG DllCharacteristics
,
2358 IN PUNICODE_STRING DllName
,
2359 OUT PVOID
*DllHandle
)
2361 PLDR_DATA_TABLE_ENTRY Module
;
2364 TRACE_LDR("LdrGetDllHandle, searching for %wZ from %S\n",
2365 DllName
, DllPath
? DllPath
: L
"");
2367 /* NULL is the current executable */
2368 if (DllName
== NULL
)
2370 *DllHandle
= ExeModule
->DllBase
;
2371 DPRINT("BaseAddress 0x%lx\n", *DllHandle
);
2372 return STATUS_SUCCESS
;
2375 Status
= LdrFindEntryForName(DllName
, &Module
, FALSE
);
2376 if (NT_SUCCESS(Status
))
2378 *DllHandle
= Module
->DllBase
;
2379 return STATUS_SUCCESS
;
2382 DPRINT("Failed to find dll %wZ\n", DllName
);
2384 return STATUS_DLL_NOT_FOUND
;
2391 LdrAddRefDll(IN ULONG Flags
,
2392 IN PVOID BaseAddress
)
2394 PLIST_ENTRY ModuleListHead
;
2396 PLDR_DATA_TABLE_ENTRY Module
;
2399 if (Flags
& ~(LDR_PIN_MODULE
))
2401 return STATUS_INVALID_PARAMETER
;
2404 Status
= STATUS_DLL_NOT_FOUND
;
2405 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2406 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2407 Entry
= ModuleListHead
->Flink
;
2408 while (Entry
!= ModuleListHead
)
2410 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2412 if (Module
->DllBase
== BaseAddress
)
2414 if (Flags
& LDR_PIN_MODULE
)
2416 Module
->Flags
|= LDRP_STATIC_LINK
;
2420 LdrpIncrementLoadCount(Module
,
2423 Status
= STATUS_SUCCESS
;
2426 Entry
= Entry
->Flink
;
2428 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2436 RtlPcToFileHeader(IN PVOID PcValue
,
2439 PLIST_ENTRY ModuleListHead
;
2441 PLDR_DATA_TABLE_ENTRY Module
;
2442 PVOID ImageBase
= NULL
;
2444 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2445 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2446 Entry
= ModuleListHead
->Flink
;
2447 while (Entry
!= ModuleListHead
)
2449 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2451 if ((ULONG_PTR
)PcValue
>= (ULONG_PTR
)Module
->DllBase
&&
2452 (ULONG_PTR
)PcValue
< (ULONG_PTR
)Module
->DllBase
+ Module
->SizeOfImage
)
2454 ImageBase
= Module
->DllBase
;
2457 Entry
= Entry
->Flink
;
2459 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2461 *BaseOfImage
= ImageBase
;
2469 LdrGetProcedureAddress (IN PVOID BaseAddress
,
2470 IN PANSI_STRING Name
,
2472 OUT PVOID
*ProcedureAddress
)
2474 if (Name
&& Name
->Length
)
2476 TRACE_LDR("LdrGetProcedureAddress by NAME - %Z\n", Name
);
2480 TRACE_LDR("LdrGetProcedureAddress by ORDINAL - %d\n", Ordinal
);
2483 DPRINT("LdrGetProcedureAddress (BaseAddress %p Name %Z Ordinal %lu ProcedureAddress %p)\n",
2484 BaseAddress
, Name
, Ordinal
, ProcedureAddress
);
2486 if (Name
&& Name
->Length
)
2489 *ProcedureAddress
= LdrGetExportByName(BaseAddress
, (PUCHAR
)Name
->Buffer
, 0xffff);
2490 if (*ProcedureAddress
!= NULL
)
2492 return STATUS_SUCCESS
;
2494 DPRINT("LdrGetProcedureAddress: Can't resolve symbol '%Z'\n", Name
);
2499 Ordinal
&= 0x0000FFFF;
2500 *ProcedureAddress
= LdrGetExportByOrdinal(BaseAddress
, (WORD
)Ordinal
);
2501 if (*ProcedureAddress
)
2503 return STATUS_SUCCESS
;
2505 DPRINT("LdrGetProcedureAddress: Can't resolve symbol @%lu\n", Ordinal
);
2507 return STATUS_PROCEDURE_NOT_FOUND
;
2510 /**********************************************************************
2515 * Unload dll's which are no longer referenced from others dll's
2526 * The loader lock must be held on enty.
2529 LdrpDetachProcess(BOOLEAN UnloadAll
)
2531 PLIST_ENTRY ModuleListHead
;
2533 PLDR_DATA_TABLE_ENTRY Module
;
2534 static ULONG CallingCount
= 0;
2536 DPRINT("LdrpDetachProcess() called for %wZ\n",
2537 &ExeModule
->BaseDllName
);
2540 LdrpDllShutdownInProgress
= TRUE
;
2544 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2545 Entry
= ModuleListHead
->Blink
;
2546 while (Entry
!= ModuleListHead
)
2548 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2549 if (((UnloadAll
&& Module
->LoadCount
== 0xFFFF) || Module
->LoadCount
== 0) &&
2550 Module
->Flags
& LDRP_ENTRY_PROCESSED
&&
2551 !(Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
))
2553 Module
->Flags
|= LDRP_UNLOAD_IN_PROGRESS
;
2554 if (Module
== LdrpLastModule
)
2556 LdrpLastModule
= NULL
;
2558 if (Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
)
2560 TRACE_LDR("Unload %wZ - Calling entry point at %x\n",
2561 &Module
->BaseDllName
, Module
->EntryPoint
);
2562 LdrpCallDllEntry(Module
, DLL_PROCESS_DETACH
, (PVOID
)(Module
->LoadCount
== 0xFFFF ? 1 : 0));
2566 TRACE_LDR("Unload %wZ\n", &Module
->BaseDllName
);
2568 Entry
= ModuleListHead
->Blink
;
2572 Entry
= Entry
->Blink
;
2576 if (CallingCount
== 1)
2578 Entry
= ModuleListHead
->Blink
;
2579 while (Entry
!= ModuleListHead
)
2581 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2582 Entry
= Entry
->Blink
;
2583 if (Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
&&
2584 ((UnloadAll
&& Module
->LoadCount
!= 0xFFFF) || Module
->LoadCount
== 0))
2586 /* remove the module entry from the list */
2587 RemoveEntryList (&Module
->InLoadOrderLinks
);
2588 RemoveEntryList (&Module
->InInitializationOrderModuleList
);
2590 NtUnmapViewOfSection (NtCurrentProcess (), Module
->DllBase
);
2591 NtClose (Module
->SectionPointer
);
2593 TRACE_LDR("%wZ unloaded\n", &Module
->BaseDllName
);
2595 RtlFreeUnicodeString (&Module
->FullDllName
);
2596 RtlFreeUnicodeString (&Module
->BaseDllName
);
2598 RtlFreeHeap (RtlGetProcessHeap (), 0, Module
);
2603 DPRINT("LdrpDetachProcess() done\n");
2606 /**********************************************************************
2611 * Initialize all dll's which are prepered for loading
2622 * The loader lock must be held on entry.
2626 LdrpAttachProcess(VOID
)
2628 PLIST_ENTRY ModuleListHead
;
2630 PLDR_DATA_TABLE_ENTRY Module
;
2632 NTSTATUS Status
= STATUS_SUCCESS
;
2634 DPRINT("LdrpAttachProcess() called for %wZ\n",
2635 &ExeModule
->BaseDllName
);
2637 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2638 Entry
= ModuleListHead
->Flink
;
2639 while (Entry
!= ModuleListHead
)
2641 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2642 if (!(Module
->Flags
& (LDRP_LOAD_IN_PROGRESS
|LDRP_UNLOAD_IN_PROGRESS
|LDRP_ENTRY_PROCESSED
)))
2644 Module
->Flags
|= LDRP_LOAD_IN_PROGRESS
;
2645 TRACE_LDR("%wZ loaded - Calling init routine at %x for process attaching\n",
2646 &Module
->BaseDllName
, Module
->EntryPoint
);
2647 Result
= LdrpCallDllEntry(Module
, DLL_PROCESS_ATTACH
, (PVOID
)(Module
->LoadCount
== 0xFFFF ? 1 : 0));
2650 Status
= STATUS_DLL_INIT_FAILED
;
2653 if (Module
->Flags
& LDRP_IMAGE_DLL
&& Module
->EntryPoint
!= 0)
2655 Module
->Flags
|= LDRP_PROCESS_ATTACH_CALLED
|LDRP_ENTRY_PROCESSED
;
2659 Module
->Flags
|= LDRP_ENTRY_PROCESSED
;
2661 Module
->Flags
&= ~LDRP_LOAD_IN_PROGRESS
;
2663 Entry
= Entry
->Flink
;
2666 DPRINT("LdrpAttachProcess() done\n");
2675 RtlDllShutdownInProgress (VOID
)
2677 return LdrpDllShutdownInProgress
;
2684 LdrShutdownProcess (VOID
)
2686 LdrpDetachProcess(TRUE
);
2687 return STATUS_SUCCESS
;
2695 LdrpAttachThread (VOID
)
2697 PLIST_ENTRY ModuleListHead
;
2699 PLDR_DATA_TABLE_ENTRY Module
;
2702 DPRINT("LdrpAttachThread() called for %wZ\n",
2703 &ExeModule
->BaseDllName
);
2705 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2707 Status
= LdrpInitializeTlsForThread();
2709 if (NT_SUCCESS(Status
))
2711 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2712 Entry
= ModuleListHead
->Flink
;
2714 while (Entry
!= ModuleListHead
)
2716 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2717 if (Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
&&
2718 !(Module
->Flags
& LDRP_DONT_CALL_FOR_THREADS
) &&
2719 !(Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
))
2721 TRACE_LDR("%wZ - Calling entry point at %x for thread attaching\n",
2722 &Module
->BaseDllName
, Module
->EntryPoint
);
2723 LdrpCallDllEntry(Module
, DLL_THREAD_ATTACH
, NULL
);
2725 Entry
= Entry
->Flink
;
2728 Entry
= NtCurrentPeb()->Ldr
->InLoadOrderModuleList
.Flink
;
2729 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2730 LdrpTlsCallback(Module
, DLL_THREAD_ATTACH
);
2733 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2735 DPRINT("LdrpAttachThread() done\n");
2745 LdrShutdownThread (VOID
)
2747 PLIST_ENTRY ModuleListHead
;
2749 PLDR_DATA_TABLE_ENTRY Module
;
2751 DPRINT("LdrShutdownThread() called for %wZ\n",
2752 &ExeModule
->BaseDllName
);
2754 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2756 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2757 Entry
= ModuleListHead
->Blink
;
2758 while (Entry
!= ModuleListHead
)
2760 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2762 if (Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
&&
2763 !(Module
->Flags
& LDRP_DONT_CALL_FOR_THREADS
) &&
2764 !(Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
))
2766 TRACE_LDR("%wZ - Calling entry point at %x for thread detaching\n",
2767 &Module
->BaseDllName
, Module
->EntryPoint
);
2768 LdrpCallDllEntry(Module
, DLL_THREAD_DETACH
, NULL
);
2770 Entry
= Entry
->Blink
;
2773 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2777 RtlFreeHeap (RtlGetProcessHeap(), 0, NtCurrentTeb()->ThreadLocalStoragePointer
);
2780 DPRINT("LdrShutdownThread() done\n");
2782 return STATUS_SUCCESS
;
2786 /***************************************************************************
2788 * LdrQueryProcessModuleInformation
2803 LdrQueryProcessModuleInformation(IN PRTL_PROCESS_MODULES ModuleInformation OPTIONAL
,
2804 IN ULONG Size OPTIONAL
,
2805 OUT PULONG ReturnedSize
)
2807 PLIST_ENTRY ModuleListHead
;
2809 PLDR_DATA_TABLE_ENTRY Module
;
2810 PRTL_PROCESS_MODULE_INFORMATION ModulePtr
= NULL
;
2811 NTSTATUS Status
= STATUS_SUCCESS
;
2812 ULONG UsedSize
= sizeof(ULONG
);
2813 ANSI_STRING AnsiString
;
2816 DPRINT("LdrQueryProcessModuleInformation() called\n");
2817 // FIXME: This code is ultra-duplicated. see lib\rtl\dbgbuffer.c
2818 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2820 if (ModuleInformation
== NULL
|| Size
== 0)
2822 Status
= STATUS_INFO_LENGTH_MISMATCH
;
2826 ModuleInformation
->NumberOfModules
= 0;
2827 ModulePtr
= &ModuleInformation
->Modules
[0];
2828 Status
= STATUS_SUCCESS
;
2831 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2832 Entry
= ModuleListHead
->Flink
;
2834 while (Entry
!= ModuleListHead
)
2836 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2838 DPRINT(" Module %wZ\n",
2839 &Module
->FullDllName
);
2841 if (UsedSize
> Size
)
2843 Status
= STATUS_INFO_LENGTH_MISMATCH
;
2845 else if (ModuleInformation
!= NULL
)
2847 ModulePtr
->Section
= 0;
2848 ModulePtr
->MappedBase
= NULL
; // FIXME: ??
2849 ModulePtr
->ImageBase
= Module
->DllBase
;
2850 ModulePtr
->ImageSize
= Module
->SizeOfImage
;
2851 ModulePtr
->Flags
= Module
->Flags
;
2852 ModulePtr
->LoadOrderIndex
= 0; // FIXME: ??
2853 ModulePtr
->InitOrderIndex
= 0; // FIXME: ??
2854 ModulePtr
->LoadCount
= Module
->LoadCount
;
2856 AnsiString
.Length
= 0;
2857 AnsiString
.MaximumLength
= 256;
2858 AnsiString
.Buffer
= ModulePtr
->FullPathName
;
2859 RtlUnicodeStringToAnsiString(&AnsiString
,
2860 &Module
->FullDllName
,
2863 p
= strrchr(ModulePtr
->FullPathName
, '\\');
2865 ModulePtr
->OffsetToFileName
= p
- ModulePtr
->FullPathName
+ 1;
2867 ModulePtr
->OffsetToFileName
= 0;
2870 ModuleInformation
->NumberOfModules
++;
2872 UsedSize
+= sizeof(RTL_PROCESS_MODULE_INFORMATION
);
2874 Entry
= Entry
->Flink
;
2877 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2879 if (ReturnedSize
!= 0)
2880 *ReturnedSize
= UsedSize
;
2882 DPRINT("LdrQueryProcessModuleInformation() done\n");
2889 LdrpCheckImageChecksum (IN PVOID BaseAddress
,
2892 PIMAGE_NT_HEADERS Header
;
2899 Header
= RtlImageNtHeader (BaseAddress
);
2903 HeaderSum
= Header
->OptionalHeader
.CheckSum
;
2908 Ptr
= (PUSHORT
) BaseAddress
;
2909 for (i
= 0; i
< ImageSize
/ sizeof (USHORT
); i
++)
2912 if (HIWORD(Sum
) != 0)
2914 Sum
= LOWORD(Sum
) + HIWORD(Sum
);
2921 Sum
+= (ULONG
)*((PUCHAR
)Ptr
);
2922 if (HIWORD(Sum
) != 0)
2924 Sum
= LOWORD(Sum
) + HIWORD(Sum
);
2928 CalcSum
= (USHORT
)(LOWORD(Sum
) + HIWORD(Sum
));
2930 /* Subtract image checksum from calculated checksum. */
2931 /* fix low word of checksum */
2932 if (LOWORD(CalcSum
) >= LOWORD(HeaderSum
))
2934 CalcSum
-= LOWORD(HeaderSum
);
2938 CalcSum
= ((LOWORD(CalcSum
) - LOWORD(HeaderSum
)) & 0xFFFF) - 1;
2941 /* fix high word of checksum */
2942 if (LOWORD(CalcSum
) >= HIWORD(HeaderSum
))
2944 CalcSum
-= HIWORD(HeaderSum
);
2948 CalcSum
= ((LOWORD(CalcSum
) - HIWORD(HeaderSum
)) & 0xFFFF) - 1;
2951 /* add file length */
2952 CalcSum
+= ImageSize
;
2954 return (BOOLEAN
)(CalcSum
== HeaderSum
);
2958 * Compute size of an image as it is actually present in virt memory
2959 * (i.e. excluding NEVER_LOAD sections)
2962 LdrpGetResidentSize(PIMAGE_NT_HEADERS NTHeaders
)
2964 PIMAGE_SECTION_HEADER SectionHeader
;
2965 unsigned SectionIndex
;
2968 SectionHeader
= (PIMAGE_SECTION_HEADER
)((char *) &NTHeaders
->OptionalHeader
2969 + NTHeaders
->FileHeader
.SizeOfOptionalHeader
);
2971 for (SectionIndex
= 0; SectionIndex
< NTHeaders
->FileHeader
.NumberOfSections
; SectionIndex
++)
2973 if (0 == (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
)
2974 && ResidentSize
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
)
2976 ResidentSize
= SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
;
2981 return ResidentSize
;
2985 /***************************************************************************
2987 * LdrVerifyImageMatchesChecksum
3002 LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle
,
3007 FILE_STANDARD_INFORMATION FileInfo
;
3008 IO_STATUS_BLOCK IoStatusBlock
;
3009 HANDLE SectionHandle
;
3015 DPRINT ("LdrVerifyImageMatchesChecksum() called\n");
3017 Status
= NtCreateSection (&SectionHandle
,
3024 if (!NT_SUCCESS(Status
))
3026 DPRINT1 ("NtCreateSection() failed (Status %lx)\n", Status
);
3032 Status
= NtMapViewOfSection (SectionHandle
,
3033 NtCurrentProcess (),
3042 if (!NT_SUCCESS(Status
))
3044 DPRINT1 ("NtMapViewOfSection() failed (Status %lx)\n", Status
);
3045 NtClose (SectionHandle
);
3049 Status
= NtQueryInformationFile (FileHandle
,
3052 sizeof (FILE_STANDARD_INFORMATION
),
3053 FileStandardInformation
);
3054 if (!NT_SUCCESS(Status
))
3056 DPRINT1 ("NtMapViewOfSection() failed (Status %lx)\n", Status
);
3057 NtUnmapViewOfSection (NtCurrentProcess (),
3059 NtClose (SectionHandle
);
3063 Result
= LdrpCheckImageChecksum (BaseAddress
,
3064 FileInfo
.EndOfFile
.u
.LowPart
);
3065 if (Result
== FALSE
)
3067 Status
= STATUS_IMAGE_CHECKSUM_MISMATCH
;
3070 NtUnmapViewOfSection (NtCurrentProcess (),
3073 NtClose (SectionHandle
);
3079 /***************************************************************************
3081 * LdrQueryImageFileExecutionOptions
3096 LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey
,
3097 IN PCWSTR ValueName
,
3100 IN ULONG BufferSize
,
3101 OUT PULONG ReturnedLength OPTIONAL
)
3103 PKEY_VALUE_PARTIAL_INFORMATION KeyInfo
;
3104 OBJECT_ATTRIBUTES ObjectAttributes
;
3105 UNICODE_STRING ValueNameString
;
3106 UNICODE_STRING KeyName
;
3107 WCHAR NameBuffer
[256];
3115 L
"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\");
3116 Ptr
= wcsrchr (SubKey
->Buffer
, L
'\\');
3119 Ptr
= SubKey
->Buffer
;
3125 wcscat (NameBuffer
, Ptr
);
3126 RtlInitUnicodeString (&KeyName
,
3129 InitializeObjectAttributes (&ObjectAttributes
,
3131 OBJ_CASE_INSENSITIVE
,
3135 Status
= NtOpenKey (&KeyHandle
,
3138 if (!NT_SUCCESS(Status
))
3140 DPRINT ("NtOpenKey() failed (Status %lx)\n", Status
);
3144 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 32;
3145 KeyInfo
= RtlAllocateHeap (RtlGetProcessHeap(),
3148 if (KeyInfo
== NULL
)
3150 NtClose (KeyHandle
);
3151 return STATUS_INSUFFICIENT_RESOURCES
;
3154 RtlInitUnicodeString (&ValueNameString
,
3156 Status
= NtQueryValueKey (KeyHandle
,
3158 KeyValuePartialInformation
,
3162 if (Status
== STATUS_BUFFER_OVERFLOW
)
3164 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + KeyInfo
->DataLength
;
3165 RtlFreeHeap (RtlGetProcessHeap(),
3168 KeyInfo
= RtlAllocateHeap (RtlGetProcessHeap(),
3171 if (KeyInfo
== NULL
)
3173 NtClose (KeyHandle
);
3174 return STATUS_INSUFFICIENT_RESOURCES
;
3177 Status
= NtQueryValueKey (KeyHandle
,
3179 KeyValuePartialInformation
,
3184 NtClose (KeyHandle
);
3186 if (!NT_SUCCESS(Status
))
3188 if (KeyInfo
!= NULL
)
3190 RtlFreeHeap (RtlGetProcessHeap(),
3197 if (KeyInfo
->Type
!= Type
)
3199 RtlFreeHeap (RtlGetProcessHeap(),
3202 return STATUS_OBJECT_TYPE_MISMATCH
;
3205 ResultSize
= BufferSize
;
3206 if (ResultSize
< KeyInfo
->DataLength
)
3208 Status
= STATUS_BUFFER_OVERFLOW
;
3212 ResultSize
= KeyInfo
->DataLength
;
3214 RtlCopyMemory (Buffer
,
3218 RtlFreeHeap (RtlGetProcessHeap(),
3222 if (ReturnedLength
!= NULL
)
3224 *ReturnedLength
= ResultSize
;
3231 PIMAGE_BASE_RELOCATION NTAPI
3232 LdrProcessRelocationBlock(IN ULONG_PTR Address
,
3234 IN PUSHORT TypeOffset
,
3243 for (i
= 0; i
< Count
; i
++)
3245 Offset
= *TypeOffset
& 0xFFF;
3246 Type
= *TypeOffset
>> 12;
3250 case IMAGE_REL_BASED_ABSOLUTE
:
3253 case IMAGE_REL_BASED_HIGH
:
3254 ShortPtr
= (PUSHORT
)((ULONG_PTR
)Address
+ Offset
);
3255 *ShortPtr
+= HIWORD(Delta
);
3258 case IMAGE_REL_BASED_LOW
:
3259 ShortPtr
= (PUSHORT
)((ULONG_PTR
)Address
+ Offset
);
3260 *ShortPtr
+= LOWORD(Delta
);
3263 case IMAGE_REL_BASED_HIGHLOW
:
3264 LongPtr
= (PULONG
)((ULONG_PTR
)Address
+ Offset
);
3268 case IMAGE_REL_BASED_HIGHADJ
:
3269 case IMAGE_REL_BASED_MIPS_JMPADDR
:
3271 DPRINT1("Unknown/unsupported fixup type %hu.\n", Type
);
3278 return (PIMAGE_BASE_RELOCATION
)TypeOffset
;
3283 LdrLockLoaderLock(IN ULONG Flags
,
3284 OUT PULONG Disposition OPTIONAL
,
3285 OUT PULONG Cookie OPTIONAL
)
3288 return STATUS_NOT_IMPLEMENTED
;
3293 LdrUnlockLoaderLock(IN ULONG Flags
,
3294 IN ULONG Cookie OPTIONAL
)
3297 return STATUS_NOT_IMPLEMENTED
;
3302 LdrUnloadAlternateResourceModule(IN PVOID BaseAddress
)