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
25 #define RVA(m, b) ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
27 /* GLOBALS *******************************************************************/
31 #define TRACE_LDR(args...) if (RtlGetNtGlobalFlags() & FLG_SHOW_LDR_SNAPS) { DbgPrint("(LDR:%s:%d) ",__FILE__,__LINE__); DbgPrint(args); }
32 #elif defined(_MSC_VER)
33 #define TRACE_LDR(args, ...) if (RtlGetNtGlobalFlags() & FLG_SHOW_LDR_SNAPS) { DbgPrint("(LDR:%s:%d) ",__FILE__,__LINE__); DbgPrint(__VA_ARGS__); }
36 #define TRACE_LDR(args...) do { DbgPrint("(LDR:%s:%d) ",__FILE__,__LINE__); DbgPrint(args); } while(0)
39 typedef struct _TLS_DATA
41 PVOID StartAddressOfRawData
;
44 PIMAGE_TLS_CALLBACK
*TlsAddressOfCallBacks
;
45 PLDR_DATA_TABLE_ENTRY Module
;
46 } TLS_DATA
, *PTLS_DATA
;
48 static BOOLEAN LdrpDllShutdownInProgress
= FALSE
;
49 static PTLS_DATA LdrpTlsArray
= NULL
;
50 static ULONG LdrpTlsCount
= 0;
51 static ULONG LdrpTlsSize
= 0;
52 static HANDLE LdrpKnownDllsDirHandle
= NULL
;
53 static UNICODE_STRING LdrpKnownDllPath
= {0, 0, NULL
};
54 static PLDR_DATA_TABLE_ENTRY LdrpLastModule
= NULL
;
55 extern PLDR_DATA_TABLE_ENTRY ExeModule
;
57 /* PROTOTYPES ****************************************************************/
59 static NTSTATUS
LdrFindEntryForName(PUNICODE_STRING Name
, PLDR_DATA_TABLE_ENTRY
*Module
, BOOLEAN Ref
);
60 static PVOID
LdrFixupForward(PCHAR ForwardName
);
61 static PVOID
LdrGetExportByName(PVOID BaseAddress
, PUCHAR SymbolName
, USHORT Hint
);
62 static NTSTATUS
LdrpLoadModule(IN PWSTR SearchPath OPTIONAL
,
64 IN PUNICODE_STRING Name
,
65 OUT PLDR_DATA_TABLE_ENTRY
*Module
,
66 OUT PVOID
*BaseAddress OPTIONAL
);
67 static NTSTATUS
LdrpAttachProcess(VOID
);
68 static VOID
LdrpDetachProcess(BOOLEAN UnloadAll
);
70 /* FUNCTIONS *****************************************************************/
72 #if defined(DBG) || defined(KDBG)
75 LdrpLoadUserModuleSymbols(PLDR_DATA_TABLE_ENTRY LdrModule
)
86 #endif /* DBG || KDBG */
89 LdrMappedAsDataFile(PVOID
*BaseAddress
)
91 if (0 != ((DWORD_PTR
) *BaseAddress
& (PAGE_SIZE
- 1)))
93 *BaseAddress
= (PVOID
) ((DWORD_PTR
) *BaseAddress
& ~ ((DWORD_PTR
) PAGE_SIZE
- 1));
100 static __inline LONG
LdrpDecrementLoadCount(PLDR_DATA_TABLE_ENTRY Module
, BOOLEAN Locked
)
105 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
107 LoadCount
= Module
->LoadCount
;
108 if (Module
->LoadCount
> 0 && Module
->LoadCount
!= 0xFFFF)
114 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
119 static __inline LONG
LdrpIncrementLoadCount(PLDR_DATA_TABLE_ENTRY Module
, BOOLEAN Locked
)
124 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
126 LoadCount
= Module
->LoadCount
;
127 if (Module
->LoadCount
!= 0xFFFF)
133 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
138 static __inline VOID
LdrpAcquireTlsSlot(PLDR_DATA_TABLE_ENTRY Module
, ULONG Size
, BOOLEAN Locked
)
142 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
144 Module
->TlsIndex
= (SHORT
)LdrpTlsCount
;
149 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
153 static __inline VOID
LdrpTlsCallback(PLDR_DATA_TABLE_ENTRY Module
, ULONG dwReason
)
155 PIMAGE_TLS_CALLBACK
*TlsCallback
;
156 if (Module
->TlsIndex
!= 0xFFFF && Module
->LoadCount
== 0xFFFF)
158 TlsCallback
= LdrpTlsArray
[Module
->TlsIndex
].TlsAddressOfCallBacks
;
163 TRACE_LDR("%wZ - Calling tls callback at %x\n",
164 &Module
->BaseDllName
, *TlsCallback
);
165 (*TlsCallback
)(Module
->DllBase
, dwReason
, NULL
);
172 static BOOLEAN
LdrpCallDllEntry(PLDR_DATA_TABLE_ENTRY Module
, DWORD dwReason
, PVOID lpReserved
)
174 if (!(Module
->Flags
& LDRP_IMAGE_DLL
) ||
175 Module
->EntryPoint
== 0)
179 LdrpTlsCallback(Module
, dwReason
);
180 return ((PDLLMAIN_FUNC
)Module
->EntryPoint
)(Module
->DllBase
, dwReason
, lpReserved
);
184 LdrpInitializeTlsForThread(VOID
)
190 PTEB Teb
= NtCurrentTeb();
192 DPRINT("LdrpInitializeTlsForThread() called for %wZ\n", &ExeModule
->BaseDllName
);
194 Teb
->StaticUnicodeString
.Length
= 0;
195 Teb
->StaticUnicodeString
.MaximumLength
= sizeof(Teb
->StaticUnicodeBuffer
);
196 Teb
->StaticUnicodeString
.Buffer
= Teb
->StaticUnicodeBuffer
;
198 if (LdrpTlsCount
> 0)
200 TlsPointers
= RtlAllocateHeap(RtlGetProcessHeap(),
202 LdrpTlsCount
* sizeof(PVOID
) + LdrpTlsSize
);
203 if (TlsPointers
== NULL
)
205 DPRINT1("failed to allocate thread tls data\n");
206 return STATUS_NO_MEMORY
;
209 TlsData
= (PVOID
)((ULONG_PTR
)TlsPointers
+ LdrpTlsCount
* sizeof(PVOID
));
210 Teb
->ThreadLocalStoragePointer
= TlsPointers
;
212 TlsInfo
= LdrpTlsArray
;
213 for (i
= 0; i
< LdrpTlsCount
; i
++, TlsInfo
++)
215 TRACE_LDR("Initialize tls data for %wZ\n", &TlsInfo
->Module
->BaseDllName
);
216 TlsPointers
[i
] = TlsData
;
217 if (TlsInfo
->TlsDataSize
)
219 memcpy(TlsData
, TlsInfo
->StartAddressOfRawData
, TlsInfo
->TlsDataSize
);
220 TlsData
= (PVOID
)((ULONG_PTR
)TlsData
+ TlsInfo
->TlsDataSize
);
222 if (TlsInfo
->TlsZeroSize
)
224 memset(TlsData
, 0, TlsInfo
->TlsZeroSize
);
225 TlsData
= (PVOID
)((ULONG_PTR
)TlsData
+ TlsInfo
->TlsZeroSize
);
230 DPRINT("LdrpInitializeTlsForThread() done\n");
231 return STATUS_SUCCESS
;
235 LdrpInitializeTlsForProccess(VOID
)
237 PLIST_ENTRY ModuleListHead
;
239 PLDR_DATA_TABLE_ENTRY Module
;
240 PIMAGE_TLS_DIRECTORY TlsDirectory
;
244 DPRINT("LdrpInitializeTlsForProccess() called for %wZ\n", &ExeModule
->BaseDllName
);
246 if (LdrpTlsCount
> 0)
248 LdrpTlsArray
= RtlAllocateHeap(RtlGetProcessHeap(),
250 LdrpTlsCount
* sizeof(TLS_DATA
));
251 if (LdrpTlsArray
== NULL
)
253 DPRINT1("Failed to allocate global tls data\n");
254 return STATUS_NO_MEMORY
;
257 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
258 Entry
= ModuleListHead
->Flink
;
259 while (Entry
!= ModuleListHead
)
261 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
262 if (Module
->LoadCount
== 0xFFFF &&
263 Module
->TlsIndex
!= 0xFFFF)
265 TlsDirectory
= (PIMAGE_TLS_DIRECTORY
)
266 RtlImageDirectoryEntryToData(Module
->DllBase
,
268 IMAGE_DIRECTORY_ENTRY_TLS
,
270 ASSERT(Module
->TlsIndex
< LdrpTlsCount
);
271 TlsData
= &LdrpTlsArray
[Module
->TlsIndex
];
272 TlsData
->StartAddressOfRawData
= (PVOID
)TlsDirectory
->StartAddressOfRawData
;
273 TlsData
->TlsDataSize
= TlsDirectory
->EndAddressOfRawData
- TlsDirectory
->StartAddressOfRawData
;
274 TlsData
->TlsZeroSize
= TlsDirectory
->SizeOfZeroFill
;
275 if (TlsDirectory
->AddressOfCallBacks
)
276 TlsData
->TlsAddressOfCallBacks
= (PIMAGE_TLS_CALLBACK
*)TlsDirectory
->AddressOfCallBacks
;
278 TlsData
->TlsAddressOfCallBacks
= NULL
;
279 TlsData
->Module
= Module
;
281 DbgPrint("TLS directory for %wZ\n", &Module
->BaseDllName
);
282 DbgPrint("StartAddressOfRawData: %x\n", TlsDirectory
->StartAddressOfRawData
);
283 DbgPrint("EndAddressOfRawData: %x\n", TlsDirectory
->EndAddressOfRawData
);
284 DbgPrint("SizeOfRawData: %d\n", TlsDirectory
->EndAddressOfRawData
- TlsDirectory
->StartAddressOfRawData
);
285 DbgPrint("AddressOfIndex: %x\n", TlsDirectory
->AddressOfIndex
);
286 DbgPrint("AddressOfCallBacks: %x\n", TlsDirectory
->AddressOfCallBacks
);
287 DbgPrint("SizeOfZeroFill: %d\n", TlsDirectory
->SizeOfZeroFill
);
288 DbgPrint("Characteristics: %x\n", TlsDirectory
->Characteristics
);
292 * Is this region allways writable ?
294 *(PULONG
)TlsDirectory
->AddressOfIndex
= Module
->TlsIndex
;
296 Entry
= Entry
->Flink
;
300 DPRINT("LdrpInitializeTlsForProccess() done\n");
301 return STATUS_SUCCESS
;
307 OBJECT_ATTRIBUTES ObjectAttributes
;
308 UNICODE_STRING LinkTarget
;
314 DPRINT("LdrpInitLoader() called for %wZ\n", &ExeModule
->BaseDllName
);
316 /* Get handle to the 'KnownDlls' directory */
317 RtlInitUnicodeString(&Name
,
319 InitializeObjectAttributes(&ObjectAttributes
,
321 OBJ_CASE_INSENSITIVE
,
324 Status
= NtOpenDirectoryObject(&LdrpKnownDllsDirHandle
,
325 DIRECTORY_QUERY
| DIRECTORY_TRAVERSE
,
327 if (!NT_SUCCESS(Status
))
329 DPRINT("NtOpenDirectoryObject() failed (Status %lx)\n", Status
);
330 LdrpKnownDllsDirHandle
= NULL
;
334 /* Allocate target name string */
335 LinkTarget
.Length
= 0;
336 LinkTarget
.MaximumLength
= MAX_PATH
* sizeof(WCHAR
);
337 LinkTarget
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
339 MAX_PATH
* sizeof(WCHAR
));
340 if (LinkTarget
.Buffer
== NULL
)
342 NtClose(LdrpKnownDllsDirHandle
);
343 LdrpKnownDllsDirHandle
= NULL
;
347 RtlInitUnicodeString(&Name
,
349 InitializeObjectAttributes(&ObjectAttributes
,
351 OBJ_CASE_INSENSITIVE
,
352 LdrpKnownDllsDirHandle
,
354 Status
= NtOpenSymbolicLinkObject(&LinkHandle
,
355 SYMBOLIC_LINK_ALL_ACCESS
,
357 if (!NT_SUCCESS(Status
))
359 RtlFreeUnicodeString(&LinkTarget
);
360 NtClose(LdrpKnownDllsDirHandle
);
361 LdrpKnownDllsDirHandle
= NULL
;
365 Status
= NtQuerySymbolicLinkObject(LinkHandle
,
369 if (!NT_SUCCESS(Status
))
371 RtlFreeUnicodeString(&LinkTarget
);
372 NtClose(LdrpKnownDllsDirHandle
);
373 LdrpKnownDllsDirHandle
= NULL
;
376 RtlCreateUnicodeString(&LdrpKnownDllPath
,
379 RtlFreeUnicodeString(&LinkTarget
);
381 DPRINT("LdrpInitLoader() done\n");
385 /***************************************************************************
390 * Adjusts the name of a dll to a fully qualified name.
393 * FullDllName: Pointer to caller supplied storage for the fully
394 * qualified dll name.
395 * DllName: Pointer to the dll name.
396 * BaseName: TRUE: Only the file name is passed to FullDllName
397 * FALSE: The full path is preserved in FullDllName
405 * A given path is not affected by the adjustment, but the file
407 * ntdll --> ntdll.dll
409 * ntdll.xyz --> ntdll.xyz
412 LdrAdjustDllName (PUNICODE_STRING FullDllName
,
413 PUNICODE_STRING DllName
,
416 WCHAR Buffer
[MAX_PATH
];
421 Length
= DllName
->Length
/ sizeof(WCHAR
);
425 /* get the base dll name */
426 Pointer
= DllName
->Buffer
+ Length
;
433 while (Pointer
>= DllName
->Buffer
&& *Pointer
!= L
'\\' && *Pointer
!= L
'/');
436 Length
= Extension
- Pointer
;
437 memmove (Buffer
, Pointer
, Length
* sizeof(WCHAR
));
438 Buffer
[Length
] = L
'\0';
442 /* get the full dll name */
443 memmove (Buffer
, DllName
->Buffer
, DllName
->Length
);
444 Buffer
[DllName
->Length
/ sizeof(WCHAR
)] = L
'\0';
447 /* Build the DLL's absolute name */
448 Extension
= wcsrchr (Buffer
, L
'.');
449 if ((Extension
!= NULL
) && (*Extension
== L
'.'))
451 /* with extension - remove dot if it's the last character */
452 if (Buffer
[Length
- 1] == L
'.')
458 /* name without extension - assume that it is .dll */
459 memmove (Buffer
+ Length
, L
".dll", 10);
462 RtlCreateUnicodeString(FullDllName
, Buffer
);
465 PLDR_DATA_TABLE_ENTRY
466 LdrAddModuleEntry(PVOID ImageBase
,
467 PIMAGE_NT_HEADERS NTHeaders
,
470 PLDR_DATA_TABLE_ENTRY Module
;
472 Module
= RtlAllocateHeap(RtlGetProcessHeap(), 0, sizeof (LDR_DATA_TABLE_ENTRY
));
474 memset(Module
, 0, sizeof(LDR_DATA_TABLE_ENTRY
));
475 Module
->DllBase
= (PVOID
)ImageBase
;
476 Module
->EntryPoint
= (PVOID
)NTHeaders
->OptionalHeader
.AddressOfEntryPoint
;
477 if (Module
->EntryPoint
!= 0)
478 Module
->EntryPoint
= (PVOID
)((ULONG_PTR
)Module
->EntryPoint
+ (ULONG_PTR
)Module
->DllBase
);
479 Module
->SizeOfImage
= LdrpGetResidentSize(NTHeaders
);
480 if (NtCurrentPeb()->Ldr
->Initialized
== TRUE
)
482 /* loading while app is running */
483 Module
->LoadCount
= 1;
486 * loading while app is initializing
487 * dll must not be unloaded
489 Module
->LoadCount
= 0xFFFF;
493 Module
->TlsIndex
= -1;
494 Module
->CheckSum
= NTHeaders
->OptionalHeader
.CheckSum
;
495 Module
->TimeDateStamp
= NTHeaders
->FileHeader
.TimeDateStamp
;
497 RtlCreateUnicodeString (&Module
->FullDllName
,
499 RtlCreateUnicodeString (&Module
->BaseDllName
,
500 wcsrchr(FullDosName
, L
'\\') + 1);
501 DPRINT ("BaseDllName %wZ\n", &Module
->BaseDllName
);
503 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
504 InsertTailList(&NtCurrentPeb()->Ldr
->InLoadOrderModuleList
,
505 &Module
->InLoadOrderLinks
);
506 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
513 LdrpMapKnownDll(IN PUNICODE_STRING DllName
,
514 OUT PUNICODE_STRING FullDosName
,
515 OUT PHANDLE SectionHandle
)
517 OBJECT_ATTRIBUTES ObjectAttributes
;
520 DPRINT("LdrpMapKnownDll() called\n");
522 if (LdrpKnownDllsDirHandle
== NULL
)
524 DPRINT("Invalid 'KnownDlls' directory\n");
525 return STATUS_UNSUCCESSFUL
;
528 DPRINT("LdrpKnownDllPath '%wZ'\n", &LdrpKnownDllPath
);
530 InitializeObjectAttributes(&ObjectAttributes
,
532 OBJ_CASE_INSENSITIVE
,
533 LdrpKnownDllsDirHandle
,
535 Status
= NtOpenSection(SectionHandle
,
536 SECTION_MAP_READ
| SECTION_MAP_WRITE
| SECTION_MAP_EXECUTE
,
538 if (!NT_SUCCESS(Status
))
540 DPRINT("NtOpenSection() failed for '%wZ' (Status 0x%08lx)\n", DllName
, Status
);
544 FullDosName
->Length
= LdrpKnownDllPath
.Length
+ DllName
->Length
+ sizeof(WCHAR
);
545 FullDosName
->MaximumLength
= FullDosName
->Length
+ sizeof(WCHAR
);
546 FullDosName
->Buffer
= RtlAllocateHeap(RtlGetProcessHeap(),
548 FullDosName
->MaximumLength
);
549 if (FullDosName
->Buffer
== NULL
)
551 FullDosName
->Length
= 0;
552 FullDosName
->MaximumLength
= 0;
553 return STATUS_SUCCESS
;
556 wcscpy(FullDosName
->Buffer
, LdrpKnownDllPath
.Buffer
);
557 wcscat(FullDosName
->Buffer
, L
"\\");
558 wcscat(FullDosName
->Buffer
, DllName
->Buffer
);
560 DPRINT("FullDosName '%wZ'\n", FullDosName
);
562 DPRINT("LdrpMapKnownDll() done\n");
564 return STATUS_SUCCESS
;
569 LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL
,
570 IN PUNICODE_STRING DllName
,
571 OUT PUNICODE_STRING FullDosName
,
572 IN BOOLEAN MapAsDataFile
,
573 OUT PHANDLE SectionHandle
)
575 WCHAR SearchPathBuffer
[MAX_PATH
];
576 WCHAR DosName
[MAX_PATH
];
577 UNICODE_STRING FullNtFileName
;
578 OBJECT_ATTRIBUTES FileObjectAttributes
;
580 char BlockBuffer
[1024];
581 PIMAGE_DOS_HEADER DosHeader
;
582 PIMAGE_NT_HEADERS NTHeaders
;
583 IO_STATUS_BLOCK IoStatusBlock
;
587 DPRINT("LdrpMapDllImageFile() called\n");
589 if (SearchPath
== NULL
)
591 /* get application running path */
593 wcscpy (SearchPathBuffer
, NtCurrentPeb()->ProcessParameters
->ImagePathName
.Buffer
);
595 len
= wcslen (SearchPathBuffer
);
597 while (len
&& SearchPathBuffer
[len
- 1] != L
'\\')
600 if (len
) SearchPathBuffer
[len
-1] = L
'\0';
602 wcscat (SearchPathBuffer
, L
";");
604 wcscat (SearchPathBuffer
, SharedUserData
->NtSystemRoot
);
605 wcscat (SearchPathBuffer
, L
"\\system32;");
606 wcscat (SearchPathBuffer
, SharedUserData
->NtSystemRoot
);
607 wcscat (SearchPathBuffer
, L
";.");
609 SearchPath
= SearchPathBuffer
;
612 if (RtlDosSearchPath_U (SearchPath
,
618 return STATUS_DLL_NOT_FOUND
;
620 if (!RtlDosPathNameToNtPathName_U (DosName
,
624 return STATUS_DLL_NOT_FOUND
;
626 DPRINT("FullNtFileName %wZ\n", &FullNtFileName
);
628 InitializeObjectAttributes(&FileObjectAttributes
,
634 DPRINT("Opening dll \"%wZ\"\n", &FullNtFileName
);
636 Status
= NtOpenFile(&FileHandle
,
637 GENERIC_READ
|SYNCHRONIZE
,
638 &FileObjectAttributes
,
641 FILE_SYNCHRONOUS_IO_NONALERT
);
642 if (!NT_SUCCESS(Status
))
644 DPRINT1("Dll open of %wZ failed: Status = 0x%08lx\n",
645 &FullNtFileName
, Status
);
646 RtlFreeHeap (RtlGetProcessHeap (),
648 FullNtFileName
.Buffer
);
651 RtlFreeHeap (RtlGetProcessHeap (),
653 FullNtFileName
.Buffer
);
658 Status
= NtReadFile(FileHandle
,
667 if (!NT_SUCCESS(Status
))
669 DPRINT("Dll header read failed: Status = 0x%08lx\n", Status
);
675 * Overlay DOS and NT headers structures to the
676 * buffer with DLL's header raw data.
678 DosHeader
= (PIMAGE_DOS_HEADER
) BlockBuffer
;
679 NTHeaders
= (PIMAGE_NT_HEADERS
) (BlockBuffer
+ DosHeader
->e_lfanew
);
681 * Check it is a PE image file.
683 if ((DosHeader
->e_magic
!= IMAGE_DOS_SIGNATURE
)
684 || (DosHeader
->e_lfanew
== 0L)
685 || (*(PULONG
)(NTHeaders
) != IMAGE_NT_SIGNATURE
))
687 DPRINT("NTDLL format invalid\n");
690 return STATUS_UNSUCCESSFUL
;
695 * Create a section for dll.
697 Status
= NtCreateSection(SectionHandle
,
702 MapAsDataFile
? SEC_COMMIT
: SEC_IMAGE
,
706 if (!NT_SUCCESS(Status
))
708 DPRINT("NTDLL create section failed: Status = 0x%08lx\n", Status
);
712 RtlCreateUnicodeString(FullDosName
,
720 /***************************************************************************
737 LdrLoadDll (IN PWSTR SearchPath OPTIONAL
,
738 IN PULONG LoadFlags OPTIONAL
,
739 IN PUNICODE_STRING Name
,
740 OUT PVOID
*BaseAddress
/* also known as HMODULE*, and PHANDLE 'DllHandle' */)
743 PLDR_DATA_TABLE_ENTRY Module
;
745 PPEB Peb
= NtCurrentPeb();
747 TRACE_LDR("LdrLoadDll, loading %wZ%s%S\n",
749 SearchPath
? L
" from " : L
"",
750 SearchPath
? SearchPath
: L
"");
752 Status
= LdrpLoadModule(SearchPath
, LoadFlags
? *LoadFlags
: 0, Name
, &Module
, BaseAddress
);
754 if (NT_SUCCESS(Status
) &&
755 (!LoadFlags
|| 0 == (*LoadFlags
& LOAD_LIBRARY_AS_DATAFILE
)))
757 if (!(Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
))
759 RtlEnterCriticalSection(Peb
->LoaderLock
);
760 Status
= LdrpAttachProcess();
761 RtlLeaveCriticalSection(Peb
->LoaderLock
);
765 if ((!Module
) && (NT_SUCCESS(Status
)))
768 *BaseAddress
= NT_SUCCESS(Status
) ? Module
->DllBase
: NULL
;
774 /***************************************************************************
776 * LdrFindEntryForAddress
791 LdrFindEntryForAddress(PVOID Address
,
792 PLDR_DATA_TABLE_ENTRY
*Module
)
794 PLIST_ENTRY ModuleListHead
;
796 PLDR_DATA_TABLE_ENTRY ModulePtr
;
798 DPRINT("LdrFindEntryForAddress(Address %p)\n", Address
);
800 if (NtCurrentPeb()->Ldr
== NULL
)
801 return(STATUS_NO_MORE_ENTRIES
);
803 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
804 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
805 Entry
= ModuleListHead
->Flink
;
806 if (Entry
== ModuleListHead
)
808 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
809 return(STATUS_NO_MORE_ENTRIES
);
812 while (Entry
!= ModuleListHead
)
814 ModulePtr
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
816 DPRINT("Scanning %wZ at %p\n", &ModulePtr
->BaseDllName
, ModulePtr
->DllBase
);
818 if ((Address
>= ModulePtr
->DllBase
) &&
819 ((ULONG_PTR
)Address
<= ((ULONG_PTR
)ModulePtr
->DllBase
+ ModulePtr
->SizeOfImage
)))
822 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
823 return(STATUS_SUCCESS
);
826 Entry
= Entry
->Flink
;
829 DPRINT("Failed to find module entry.\n");
831 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
832 return(STATUS_NO_MORE_ENTRIES
);
836 /***************************************************************************
838 * LdrFindEntryForName
852 LdrFindEntryForName(PUNICODE_STRING Name
,
853 PLDR_DATA_TABLE_ENTRY
*Module
,
856 PLIST_ENTRY ModuleListHead
;
858 PLDR_DATA_TABLE_ENTRY ModulePtr
;
859 BOOLEAN ContainsPath
;
860 UNICODE_STRING AdjustedName
;
863 DPRINT("LdrFindEntryForName(Name %wZ)\n", Name
);
865 if (NtCurrentPeb()->Ldr
== NULL
)
866 return(STATUS_NO_MORE_ENTRIES
);
868 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
869 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
870 Entry
= ModuleListHead
->Flink
;
871 if (Entry
== ModuleListHead
)
873 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
874 return(STATUS_NO_MORE_ENTRIES
);
877 // NULL is the current process
881 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
882 return(STATUS_SUCCESS
);
885 LdrAdjustDllName (&AdjustedName
, Name
, FALSE
);
887 ContainsPath
= (AdjustedName
.Length
>= 2 * sizeof(WCHAR
) && L
':' == AdjustedName
.Buffer
[1]);
888 for (i
= 0; ! ContainsPath
&& i
< AdjustedName
.Length
/ sizeof(WCHAR
); i
++)
890 ContainsPath
= L
'\\' == AdjustedName
.Buffer
[i
] ||
891 L
'/' == AdjustedName
.Buffer
[i
];
896 if ((! ContainsPath
&&
897 0 == RtlCompareUnicodeString(&LdrpLastModule
->BaseDllName
, &AdjustedName
, TRUE
)) ||
899 0 == RtlCompareUnicodeString(&LdrpLastModule
->FullDllName
, &AdjustedName
, TRUE
)))
901 *Module
= LdrpLastModule
;
902 if (Ref
&& (*Module
)->LoadCount
!= 0xFFFF)
904 (*Module
)->LoadCount
++;
906 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
907 RtlFreeUnicodeString(&AdjustedName
);
908 return(STATUS_SUCCESS
);
911 while (Entry
!= ModuleListHead
)
913 ModulePtr
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
915 DPRINT("Scanning %wZ %wZ\n", &ModulePtr
->BaseDllName
, &AdjustedName
);
917 if ((! ContainsPath
&&
918 0 == RtlCompareUnicodeString(&ModulePtr
->BaseDllName
, &AdjustedName
, TRUE
)) ||
920 0 == RtlCompareUnicodeString(&ModulePtr
->FullDllName
, &AdjustedName
, TRUE
)))
922 *Module
= LdrpLastModule
= ModulePtr
;
923 if (Ref
&& ModulePtr
->LoadCount
!= 0xFFFF)
925 ModulePtr
->LoadCount
++;
927 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
928 RtlFreeUnicodeString(&AdjustedName
);
929 return(STATUS_SUCCESS
);
932 Entry
= Entry
->Flink
;
935 DPRINT("Failed to find dll %wZ\n", Name
);
936 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
937 RtlFreeUnicodeString(&AdjustedName
);
938 return(STATUS_NO_MORE_ENTRIES
);
941 /**********************************************************************
957 LdrFixupForward(PCHAR ForwardName
)
959 CHAR NameBuffer
[128];
960 UNICODE_STRING DllName
;
963 PLDR_DATA_TABLE_ENTRY Module
;
966 strcpy(NameBuffer
, ForwardName
);
967 p
= strchr(NameBuffer
, '.');
972 DPRINT("Dll: %s Function: %s\n", NameBuffer
, p
+1);
973 RtlCreateUnicodeStringFromAsciiz (&DllName
,
976 Status
= LdrFindEntryForName (&DllName
, &Module
, FALSE
);
978 * The caller (or the image) is responsible for loading of the dll, where the function is forwarded.
980 if (!NT_SUCCESS(Status
))
982 ULONG Flags
= LDRP_PROCESS_CREATION_TIME
;
983 Status
= LdrLoadDll(NULL
,
987 if (NT_SUCCESS(Status
))
989 Status
= LdrFindEntryForName (&DllName
, &Module
, FALSE
);
992 RtlFreeUnicodeString (&DllName
);
993 if (!NT_SUCCESS(Status
))
995 DPRINT1("LdrFixupForward: failed to load %s\n", NameBuffer
);
999 DPRINT("BaseAddress: %p\n", Module
->DllBase
);
1001 return LdrGetExportByName(Module
->DllBase
, (PUCHAR
)(p
+1), -1);
1008 /**********************************************************************
1010 * LdrGetExportByOrdinal
1024 LdrGetExportByOrdinal (
1029 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1030 ULONG ExportDirSize
;
1031 PDWORD
* ExFunctions
;
1034 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)
1035 RtlImageDirectoryEntryToData (BaseAddress
,
1037 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1041 ExFunctions
= (PDWORD
*)
1044 ExportDir
->AddressOfFunctions
1047 "LdrGetExportByOrdinal(Ordinal %lu) = %p\n",
1049 RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
] )
1052 Function
= (0 != ExFunctions
[Ordinal
- ExportDir
->Base
]
1053 ? RVA(BaseAddress
, ExFunctions
[Ordinal
- ExportDir
->Base
] )
1056 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1057 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1059 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1060 Function
= LdrFixupForward((PCHAR
)Function
);
1067 /**********************************************************************
1069 * LdrGetExportByName
1080 * AddressOfNames and AddressOfNameOrdinals are paralell tables,
1081 * both with NumberOfNames entries.
1085 LdrGetExportByName(PVOID BaseAddress
,
1089 PIMAGE_EXPORT_DIRECTORY ExportDir
;
1090 PDWORD
* ExFunctions
;
1092 USHORT
* ExOrdinals
;
1097 ULONG ExportDirSize
;
1099 DPRINT("LdrGetExportByName %p %s %hu\n", BaseAddress
, SymbolName
, Hint
);
1101 ExportDir
= (PIMAGE_EXPORT_DIRECTORY
)
1102 RtlImageDirectoryEntryToData(BaseAddress
,
1104 IMAGE_DIRECTORY_ENTRY_EXPORT
,
1106 if (ExportDir
== NULL
)
1108 DPRINT1("LdrGetExportByName(): no export directory, "
1109 "can't lookup %s/%hu!\n", SymbolName
, Hint
);
1114 //The symbol names may be missing entirely
1115 if (ExportDir
->AddressOfNames
== 0)
1117 DPRINT("LdrGetExportByName(): symbol names missing entirely\n");
1122 * Get header pointers
1124 ExNames
= (PDWORD
*)RVA(BaseAddress
,
1125 ExportDir
->AddressOfNames
);
1126 ExOrdinals
= (USHORT
*)RVA(BaseAddress
,
1127 ExportDir
->AddressOfNameOrdinals
);
1128 ExFunctions
= (PDWORD
*)RVA(BaseAddress
,
1129 ExportDir
->AddressOfFunctions
);
1132 * Check the hint first
1134 if (Hint
< ExportDir
->NumberOfNames
)
1136 ExName
= RVA(BaseAddress
, ExNames
[Hint
]);
1137 if (strcmp(ExName
, (PCHAR
)SymbolName
) == 0)
1139 Ordinal
= ExOrdinals
[Hint
];
1140 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1141 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1142 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1144 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1145 Function
= LdrFixupForward((PCHAR
)Function
);
1146 if (Function
== NULL
)
1148 DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName
);
1152 if (Function
!= NULL
)
1161 maxn
= ExportDir
->NumberOfNames
- 1;
1162 while (minn
<= maxn
)
1167 mid
= (minn
+ maxn
) / 2;
1169 ExName
= RVA(BaseAddress
, ExNames
[mid
]);
1170 res
= strcmp(ExName
, (PCHAR
)SymbolName
);
1173 Ordinal
= ExOrdinals
[mid
];
1174 Function
= RVA(BaseAddress
, ExFunctions
[Ordinal
]);
1175 if (((ULONG
)Function
>= (ULONG
)ExportDir
) &&
1176 ((ULONG
)Function
< (ULONG
)ExportDir
+ (ULONG
)ExportDirSize
))
1178 DPRINT("Forward: %s\n", (PCHAR
)Function
);
1179 Function
= LdrFixupForward((PCHAR
)Function
);
1180 if (Function
== NULL
)
1182 DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName
);
1186 if (Function
!= NULL
)
1189 else if (minn
== maxn
)
1191 DPRINT("LdrGetExportByName(): binary search failed\n");
1204 DPRINT("LdrGetExportByName(): failed to find %s\n",SymbolName
);
1209 /**********************************************************************
1211 * LdrPerformRelocations
1214 * Relocate a DLL's memory image.
1226 LdrPerformRelocations(PIMAGE_NT_HEADERS NTHeaders
,
1229 PIMAGE_DATA_DIRECTORY RelocationDDir
;
1230 PIMAGE_BASE_RELOCATION RelocationDir
, RelocationEnd
;
1231 ULONG Count
, ProtectSize
, OldProtect
, OldProtect2
;
1232 PVOID Page
, ProtectPage
, ProtectPage2
;
1237 if (NTHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_RELOCS_STRIPPED
)
1239 return STATUS_SUCCESS
;
1243 &NTHeaders
->OptionalHeader
.DataDirectory
[IMAGE_DIRECTORY_ENTRY_BASERELOC
];
1245 if (RelocationDDir
->VirtualAddress
== 0 || RelocationDDir
->Size
== 0)
1247 return STATUS_SUCCESS
;
1250 ProtectSize
= PAGE_SIZE
;
1251 Delta
= (ULONG_PTR
)ImageBase
- NTHeaders
->OptionalHeader
.ImageBase
;
1252 RelocationDir
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)ImageBase
+
1253 RelocationDDir
->VirtualAddress
);
1254 RelocationEnd
= (PIMAGE_BASE_RELOCATION
)((ULONG_PTR
)ImageBase
+
1255 RelocationDDir
->VirtualAddress
+ RelocationDDir
->Size
);
1257 while (RelocationDir
< RelocationEnd
&&
1258 RelocationDir
->SizeOfBlock
> 0)
1260 Count
= (RelocationDir
->SizeOfBlock
- sizeof(IMAGE_BASE_RELOCATION
)) /
1262 Page
= (PVOID
)((ULONG_PTR
)ImageBase
+ (ULONG_PTR
)RelocationDir
->VirtualAddress
);
1263 TypeOffset
= (PUSHORT
)(RelocationDir
+ 1);
1265 /* Unprotect the page(s) we're about to relocate. */
1267 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1272 if (!NT_SUCCESS(Status
))
1274 DPRINT1("Failed to unprotect relocation target.\n");
1278 if (RelocationDir
->VirtualAddress
+ PAGE_SIZE
<
1279 NTHeaders
->OptionalHeader
.SizeOfImage
)
1281 ProtectPage2
= (PVOID
)((ULONG_PTR
)ProtectPage
+ PAGE_SIZE
);
1282 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1287 if (!NT_SUCCESS(Status
))
1289 DPRINT1("Failed to unprotect relocation target (2).\n");
1290 NtProtectVirtualMemory(NtCurrentProcess(),
1300 ProtectPage2
= NULL
;
1303 RelocationDir
= LdrProcessRelocationBlock((ULONG_PTR
)Page
,
1307 if (RelocationDir
== NULL
)
1308 return STATUS_UNSUCCESSFUL
;
1310 /* Restore old page protection. */
1311 NtProtectVirtualMemory(NtCurrentProcess(),
1317 if (ProtectPage2
!= NULL
)
1319 NtProtectVirtualMemory(NtCurrentProcess(),
1327 return STATUS_SUCCESS
;
1331 LdrpGetOrLoadModule(PWCHAR SearchPath
,
1333 PLDR_DATA_TABLE_ENTRY
* Module
,
1336 ANSI_STRING AnsiDllName
;
1337 UNICODE_STRING DllName
;
1340 DPRINT("LdrpGetOrLoadModule() called for %s\n", Name
);
1342 RtlInitAnsiString(&AnsiDllName
, Name
);
1343 Status
= RtlAnsiStringToUnicodeString(&DllName
, &AnsiDllName
, TRUE
);
1344 if (!NT_SUCCESS(Status
))
1349 Status
= LdrFindEntryForName (&DllName
, Module
, Load
);
1350 if (Load
&& !NT_SUCCESS(Status
))
1352 Status
= LdrpLoadModule(SearchPath
,
1353 NtCurrentPeb()->Ldr
->Initialized
? 0 : LDRP_PROCESS_CREATION_TIME
,
1357 if (NT_SUCCESS(Status
))
1359 Status
= LdrFindEntryForName (&DllName
, Module
, FALSE
);
1361 if (!NT_SUCCESS(Status
))
1363 ULONG ErrorResponse
;
1364 ULONG_PTR ErrorParameter
= (ULONG_PTR
)&DllName
;
1366 DPRINT1("failed to load %wZ\n", &DllName
);
1367 NtRaiseHardError(STATUS_DLL_NOT_FOUND
,
1375 RtlFreeUnicodeString (&DllName
);
1380 RtlpRaiseImportNotFound(CHAR
*FuncName
, ULONG Ordinal
, PUNICODE_STRING DllName
)
1382 ULONG ErrorResponse
;
1383 ULONG_PTR ErrorParameters
[2];
1384 ANSI_STRING ProcNameAnsi
;
1385 UNICODE_STRING ProcName
;
1390 _snprintf(Buffer
, 8, "# %ld", Ordinal
);
1394 RtlInitAnsiString(&ProcNameAnsi
, FuncName
);
1395 RtlAnsiStringToUnicodeString(&ProcName
, &ProcNameAnsi
, TRUE
);
1396 ErrorParameters
[0] = (ULONG_PTR
)&ProcName
;
1397 ErrorParameters
[1] = (ULONG_PTR
)DllName
;
1398 NtRaiseHardError(STATUS_ENTRYPOINT_NOT_FOUND
,
1404 RtlFreeUnicodeString(&ProcName
);
1408 LdrpProcessImportDirectoryEntry(PLDR_DATA_TABLE_ENTRY Module
,
1409 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1410 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
)
1413 PVOID
* ImportAddressList
;
1414 PULONG FunctionNameList
;
1420 if (ImportModuleDirectory
== NULL
|| ImportModuleDirectory
->Name
== 0)
1422 return STATUS_UNSUCCESSFUL
;
1425 /* Get the import address list. */
1426 ImportAddressList
= (PVOID
*)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1428 /* Get the list of functions to import. */
1429 if (ImportModuleDirectory
->OriginalFirstThunk
!= 0)
1431 FunctionNameList
= (PULONG
) ((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->OriginalFirstThunk
);
1435 FunctionNameList
= (PULONG
)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1438 /* Get the size of IAT. */
1440 while (FunctionNameList
[IATSize
] != 0L)
1445 /* Unprotect the region we are about to write into. */
1446 IATBase
= (PVOID
)ImportAddressList
;
1447 IATSize
*= sizeof(PVOID
*);
1448 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1453 if (!NT_SUCCESS(Status
))
1455 DPRINT1("Failed to unprotect IAT.\n");
1459 /* Walk through function list and fixup addresses. */
1460 while (*FunctionNameList
!= 0L)
1462 if ((*FunctionNameList
) & 0x80000000)
1464 Ordinal
= (*FunctionNameList
) & 0x7fffffff;
1465 *ImportAddressList
= LdrGetExportByOrdinal(ImportedModule
->DllBase
, Ordinal
);
1466 if ((*ImportAddressList
) == NULL
)
1468 DPRINT1("Failed to import #%ld from %wZ\n", Ordinal
, &ImportedModule
->FullDllName
);
1469 RtlpRaiseImportNotFound(NULL
, Ordinal
, &ImportedModule
->FullDllName
);
1470 return STATUS_ENTRYPOINT_NOT_FOUND
;
1475 IMAGE_IMPORT_BY_NAME
*pe_name
;
1476 pe_name
= RVA(Module
->DllBase
, *FunctionNameList
);
1477 *ImportAddressList
= LdrGetExportByName(ImportedModule
->DllBase
, pe_name
->Name
, pe_name
->Hint
);
1478 if ((*ImportAddressList
) == NULL
)
1480 DPRINT1("Failed to import %s from %wZ\n", pe_name
->Name
, &ImportedModule
->FullDllName
);
1481 RtlpRaiseImportNotFound((CHAR
*)pe_name
->Name
, 0, &ImportedModule
->FullDllName
);
1482 return STATUS_ENTRYPOINT_NOT_FOUND
;
1485 ImportAddressList
++;
1489 /* Protect the region we are about to write into. */
1490 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1495 if (!NT_SUCCESS(Status
))
1497 DPRINT1("Failed to protect IAT.\n");
1501 return STATUS_SUCCESS
;
1505 LdrpProcessImportDirectory(
1506 PLDR_DATA_TABLE_ENTRY Module
,
1507 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1511 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1515 DPRINT("LdrpProcessImportDirectory(%p '%wZ', '%s')\n",
1516 Module
, &Module
->BaseDllName
, ImportedName
);
1519 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1520 RtlImageDirectoryEntryToData(Module
->DllBase
,
1522 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1524 if (ImportModuleDirectory
== NULL
)
1526 return STATUS_UNSUCCESSFUL
;
1529 while (ImportModuleDirectory
->Name
)
1531 Name
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectory
->Name
;
1532 if (0 == _stricmp(Name
, ImportedName
))
1534 Status
= LdrpProcessImportDirectoryEntry(Module
,
1536 ImportModuleDirectory
);
1537 if (!NT_SUCCESS(Status
))
1542 ImportModuleDirectory
++;
1546 return STATUS_SUCCESS
;
1551 LdrpAdjustImportDirectory(PLDR_DATA_TABLE_ENTRY Module
,
1552 PLDR_DATA_TABLE_ENTRY ImportedModule
,
1555 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1557 PVOID
* ImportAddressList
;
1560 PULONG FunctionNameList
;
1565 PIMAGE_NT_HEADERS NTHeaders
;
1569 DPRINT("LdrpAdjustImportDirectory(Module %p '%wZ', %p '%wZ', '%s')\n",
1570 Module
, &Module
->BaseDllName
, ImportedModule
, &ImportedModule
->BaseDllName
, ImportedName
);
1572 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1573 RtlImageDirectoryEntryToData(Module
->DllBase
,
1575 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1577 if (ImportModuleDirectory
== NULL
)
1579 return STATUS_UNSUCCESSFUL
;
1582 while (ImportModuleDirectory
->Name
)
1584 Name
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectory
->Name
;
1585 if (0 == _stricmp(Name
, (PCHAR
)ImportedName
))
1588 /* Get the import address list. */
1589 ImportAddressList
= (PVOID
*)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1591 /* Get the list of functions to import. */
1592 if (ImportModuleDirectory
->OriginalFirstThunk
!= 0)
1594 FunctionNameList
= (PULONG
) ((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->OriginalFirstThunk
);
1598 FunctionNameList
= (PULONG
)((ULONG_PTR
)Module
->DllBase
+ (ULONG_PTR
)ImportModuleDirectory
->FirstThunk
);
1601 /* Get the size of IAT. */
1603 while (FunctionNameList
[IATSize
] != 0L)
1608 /* Unprotect the region we are about to write into. */
1609 IATBase
= (PVOID
)ImportAddressList
;
1610 IATSize
*= sizeof(PVOID
*);
1611 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1616 if (!NT_SUCCESS(Status
))
1618 DPRINT1("Failed to unprotect IAT.\n");
1622 NTHeaders
= RtlImageNtHeader (ImportedModule
->DllBase
);
1623 Start
= (PVOID
)NTHeaders
->OptionalHeader
.ImageBase
;
1624 End
= (PVOID
)((ULONG_PTR
)Start
+ ImportedModule
->SizeOfImage
);
1625 Offset
= (ULONG
)((ULONG_PTR
)ImportedModule
->DllBase
- (ULONG_PTR
)Start
);
1627 /* Walk through function list and fixup addresses. */
1628 while (*FunctionNameList
!= 0L)
1630 if (*ImportAddressList
>= Start
&& *ImportAddressList
< End
)
1632 (*ImportAddressList
) = (PVOID
)((ULONG_PTR
)(*ImportAddressList
) + Offset
);
1634 ImportAddressList
++;
1638 /* Protect the region we are about to write into. */
1639 Status
= NtProtectVirtualMemory(NtCurrentProcess(),
1644 if (!NT_SUCCESS(Status
))
1646 DPRINT1("Failed to protect IAT.\n");
1650 ImportModuleDirectory
++;
1652 return STATUS_SUCCESS
;
1656 /**********************************************************************
1661 * Compute the entry point for every symbol the DLL imports
1662 * from other modules.
1674 LdrFixupImports(IN PWSTR SearchPath OPTIONAL
,
1675 IN PLDR_DATA_TABLE_ENTRY Module
)
1677 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
1678 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectoryCurrent
;
1679 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptor
;
1680 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptorCurrent
;
1681 PIMAGE_TLS_DIRECTORY TlsDirectory
;
1684 PLDR_DATA_TABLE_ENTRY ImportedModule
;
1688 DPRINT("LdrFixupImports(SearchPath %S, Module %p)\n", SearchPath
, Module
);
1690 /* Check for tls data */
1691 TlsDirectory
= (PIMAGE_TLS_DIRECTORY
)
1692 RtlImageDirectoryEntryToData(Module
->DllBase
,
1694 IMAGE_DIRECTORY_ENTRY_TLS
,
1698 TlsSize
= TlsDirectory
->EndAddressOfRawData
1699 - TlsDirectory
->StartAddressOfRawData
1700 + TlsDirectory
->SizeOfZeroFill
;
1702 if (TlsSize
> 0 && NtCurrentPeb()->Ldr
->Initialized
)
1704 TRACE_LDR("Trying to dynamically load %wZ which contains a TLS directory\n",
1705 &Module
->BaseDllName
);
1706 TlsDirectory
= NULL
;
1711 * Process each import module.
1713 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
1714 RtlImageDirectoryEntryToData(Module
->DllBase
,
1716 IMAGE_DIRECTORY_ENTRY_IMPORT
,
1719 BoundImportDescriptor
= (PIMAGE_BOUND_IMPORT_DESCRIPTOR
)
1720 RtlImageDirectoryEntryToData(Module
->DllBase
,
1722 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
1725 if (BoundImportDescriptor
!= NULL
&& ImportModuleDirectory
== NULL
)
1727 DPRINT1("%wZ has only a bound import directory\n", &Module
->BaseDllName
);
1728 return STATUS_UNSUCCESSFUL
;
1730 if (BoundImportDescriptor
)
1732 DPRINT("BoundImportDescriptor %p\n", BoundImportDescriptor
);
1734 BoundImportDescriptorCurrent
= BoundImportDescriptor
;
1735 while (BoundImportDescriptorCurrent
->OffsetModuleName
)
1737 ImportedName
= (PCHAR
)BoundImportDescriptor
+ BoundImportDescriptorCurrent
->OffsetModuleName
;
1738 TRACE_LDR("%wZ bound to %s\n", &Module
->BaseDllName
, ImportedName
);
1739 Status
= LdrpGetOrLoadModule(SearchPath
, ImportedName
, &ImportedModule
, TRUE
);
1740 if (!NT_SUCCESS(Status
))
1742 DPRINT1("failed to load %s\n", ImportedName
);
1745 if (Module
== ImportedModule
)
1747 LdrpDecrementLoadCount(Module
, FALSE
);
1749 if (ImportedModule
->TimeDateStamp
!= BoundImportDescriptorCurrent
->TimeDateStamp
)
1751 TRACE_LDR("%wZ has stale binding to %wZ\n",
1752 &Module
->BaseDllName
, &ImportedModule
->BaseDllName
);
1753 Status
= LdrpProcessImportDirectory(Module
, ImportedModule
, ImportedName
);
1754 if (!NT_SUCCESS(Status
))
1756 DPRINT1("failed to import %s\n", ImportedName
);
1762 BOOLEAN WrongForwarder
;
1763 WrongForwarder
= FALSE
;
1764 if (ImportedModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1766 TRACE_LDR("%wZ has stale binding to %s\n",
1767 &Module
->BaseDllName
, ImportedName
);
1771 TRACE_LDR("%wZ has correct binding to %wZ\n",
1772 &Module
->BaseDllName
, &ImportedModule
->BaseDllName
);
1774 if (BoundImportDescriptorCurrent
->NumberOfModuleForwarderRefs
)
1776 PIMAGE_BOUND_FORWARDER_REF BoundForwarderRef
;
1778 PLDR_DATA_TABLE_ENTRY ForwarderModule
;
1779 PCHAR ForwarderName
;
1781 BoundForwarderRef
= (PIMAGE_BOUND_FORWARDER_REF
)(BoundImportDescriptorCurrent
+ 1);
1782 for (i
= 0; i
< BoundImportDescriptorCurrent
->NumberOfModuleForwarderRefs
; i
++, BoundForwarderRef
++)
1784 ForwarderName
= (PCHAR
)BoundImportDescriptor
+ BoundForwarderRef
->OffsetModuleName
;
1785 TRACE_LDR("%wZ bound to %s via forwardes from %s\n",
1786 &Module
->BaseDllName
, ForwarderName
, ImportedName
);
1787 Status
= LdrpGetOrLoadModule(SearchPath
, ForwarderName
, &ForwarderModule
, TRUE
);
1788 if (!NT_SUCCESS(Status
))
1790 DPRINT1("failed to load %s\n", ForwarderName
);
1793 if (Module
== ImportedModule
)
1795 LdrpDecrementLoadCount(Module
, FALSE
);
1797 if (ForwarderModule
->TimeDateStamp
!= BoundForwarderRef
->TimeDateStamp
||
1798 ForwarderModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1800 TRACE_LDR("%wZ has stale binding to %s\n",
1801 &Module
->BaseDllName
, ForwarderName
);
1802 WrongForwarder
= TRUE
;
1806 TRACE_LDR("%wZ has correct binding to %s\n",
1807 &Module
->BaseDllName
, ForwarderName
);
1811 if (WrongForwarder
||
1812 ImportedModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1814 Status
= LdrpProcessImportDirectory(Module
, ImportedModule
, ImportedName
);
1815 if (!NT_SUCCESS(Status
))
1817 DPRINT1("failed to import %s\n", ImportedName
);
1821 else if (ImportedModule
->Flags
& LDRP_IMAGE_NOT_AT_BASE
)
1823 TRACE_LDR("Adjust imports for %s from %wZ\n",
1824 ImportedName
, &Module
->BaseDllName
);
1825 Status
= LdrpAdjustImportDirectory(Module
, ImportedModule
, ImportedName
);
1826 if (!NT_SUCCESS(Status
))
1828 DPRINT1("failed to adjust import entries for %s\n", ImportedName
);
1832 else if (WrongForwarder
)
1836 * Update only forwarders
1838 TRACE_LDR("Stale BIND %s from %wZ\n",
1839 ImportedName
, &Module
->BaseDllName
);
1840 Status
= LdrpProcessImportDirectory(Module
, ImportedModule
, ImportedName
);
1841 if (!NT_SUCCESS(Status
))
1843 DPRINT1("faild to import %s\n", ImportedName
);
1852 BoundImportDescriptorCurrent
+= BoundImportDescriptorCurrent
->NumberOfModuleForwarderRefs
+ 1;
1855 else if (ImportModuleDirectory
)
1857 DPRINT("ImportModuleDirectory %p\n", ImportModuleDirectory
);
1859 ImportModuleDirectoryCurrent
= ImportModuleDirectory
;
1860 while (ImportModuleDirectoryCurrent
->Name
)
1862 ImportedName
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectoryCurrent
->Name
;
1863 TRACE_LDR("%wZ imports functions from %s\n", &Module
->BaseDllName
, ImportedName
);
1865 Status
= LdrpGetOrLoadModule(SearchPath
, ImportedName
, &ImportedModule
, TRUE
);
1866 if (!NT_SUCCESS(Status
))
1868 DPRINT1("failed to load %s\n", ImportedName
);
1871 if (Module
== ImportedModule
)
1873 LdrpDecrementLoadCount(Module
, FALSE
);
1876 TRACE_LDR("Initializing imports for %wZ from %s\n",
1877 &Module
->BaseDllName
, ImportedName
);
1878 Status
= LdrpProcessImportDirectoryEntry(Module
, ImportedModule
, ImportModuleDirectoryCurrent
);
1879 if (!NT_SUCCESS(Status
))
1881 DPRINT1("failed to import %s\n", ImportedName
);
1884 ImportModuleDirectoryCurrent
++;
1888 if (TlsDirectory
&& TlsSize
> 0)
1890 LdrpAcquireTlsSlot(Module
, TlsSize
, FALSE
);
1893 return STATUS_SUCCESS
;
1897 /**********************************************************************
1902 * 1. Relocate, if needed the EXE.
1903 * 2. Fixup any imported symbol.
1904 * 3. Compute the EXE's entry point.
1908 * Address at which the EXE's image
1912 * Handle of the section that contains
1916 * NULL on error; otherwise the entry point
1917 * to call for initializing the DLL.
1922 * 04.01.2004 hb Previous this function was used for all images (dll + exe).
1923 * Currently the function is only used for the exe.
1925 PEPFUNC
LdrPEStartup (PVOID ImageBase
,
1926 HANDLE SectionHandle
,
1927 PLDR_DATA_TABLE_ENTRY
* Module
,
1931 PEPFUNC EntryPoint
= NULL
;
1932 PIMAGE_DOS_HEADER DosHeader
;
1933 PIMAGE_NT_HEADERS NTHeaders
;
1934 PLDR_DATA_TABLE_ENTRY tmpModule
;
1936 DPRINT("LdrPEStartup(ImageBase %p SectionHandle %p)\n",
1937 ImageBase
, SectionHandle
);
1940 * Overlay DOS and WNT headers structures
1941 * to the DLL's image.
1943 DosHeader
= (PIMAGE_DOS_HEADER
) ImageBase
;
1944 NTHeaders
= (PIMAGE_NT_HEADERS
) ((ULONG_PTR
)ImageBase
+ DosHeader
->e_lfanew
);
1947 * If the base address is different from the
1948 * one the DLL is actually loaded, perform any
1951 if (ImageBase
!= (PVOID
) NTHeaders
->OptionalHeader
.ImageBase
)
1953 DPRINT("LDR: Performing relocations\n");
1954 Status
= LdrPerformRelocations(NTHeaders
, ImageBase
);
1955 if (!NT_SUCCESS(Status
))
1957 DPRINT1("LdrPerformRelocations() failed\n");
1964 *Module
= LdrAddModuleEntry(ImageBase
, NTHeaders
, FullDosName
);
1965 (*Module
)->SectionPointer
= SectionHandle
;
1969 Module
= &tmpModule
;
1970 Status
= LdrFindEntryForAddress(ImageBase
, Module
);
1971 if (!NT_SUCCESS(Status
))
1977 if (ImageBase
!= (PVOID
) NTHeaders
->OptionalHeader
.ImageBase
)
1979 (*Module
)->Flags
|= LDRP_IMAGE_NOT_AT_BASE
;
1983 * If the DLL's imports symbols from other
1984 * modules, fixup the imported calls entry points.
1986 DPRINT("About to fixup imports\n");
1987 Status
= LdrFixupImports(NULL
, *Module
);
1988 if (!NT_SUCCESS(Status
))
1990 DPRINT1("LdrFixupImports() failed for %wZ\n", &(*Module
)->BaseDllName
);
1993 DPRINT("Fixup done\n");
1994 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
1995 Status
= LdrpInitializeTlsForProccess();
1996 if (NT_SUCCESS(Status
))
1998 Status
= LdrpAttachProcess();
2000 if (NT_SUCCESS(Status
))
2002 LdrpTlsCallback(*Module
, DLL_PROCESS_ATTACH
);
2006 RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock
);
2007 if (!NT_SUCCESS(Status
))
2013 * Compute the DLL's entry point's address.
2015 DPRINT("ImageBase = %p\n", ImageBase
);
2016 DPRINT("AddressOfEntryPoint = 0x%lx\n",(ULONG
)NTHeaders
->OptionalHeader
.AddressOfEntryPoint
);
2017 if (NTHeaders
->OptionalHeader
.AddressOfEntryPoint
!= 0)
2019 EntryPoint
= (PEPFUNC
) ((ULONG_PTR
)ImageBase
2020 + NTHeaders
->OptionalHeader
.AddressOfEntryPoint
);
2022 DPRINT("LdrPEStartup() = %p\n",EntryPoint
);
2027 LdrpLoadModule(IN PWSTR SearchPath OPTIONAL
,
2029 IN PUNICODE_STRING Name
,
2030 PLDR_DATA_TABLE_ENTRY
*Module
,
2031 PVOID
*BaseAddress OPTIONAL
)
2033 UNICODE_STRING AdjustedName
;
2034 UNICODE_STRING FullDosName
;
2036 PLDR_DATA_TABLE_ENTRY tmpModule
;
2037 HANDLE SectionHandle
;
2040 PIMAGE_NT_HEADERS NtHeaders
;
2041 BOOLEAN MappedAsDataFile
;
2042 PVOID ArbitraryUserPointer
;
2046 Module
= &tmpModule
;
2048 /* adjust the full dll name */
2049 LdrAdjustDllName(&AdjustedName
, Name
, FALSE
);
2051 DPRINT("%wZ\n", &AdjustedName
);
2053 MappedAsDataFile
= FALSE
;
2054 /* Test if dll is already loaded */
2055 Status
= LdrFindEntryForName(&AdjustedName
, Module
, TRUE
);
2056 if (NT_SUCCESS(Status
))
2058 RtlFreeUnicodeString(&AdjustedName
);
2059 if (NULL
!= BaseAddress
)
2061 *BaseAddress
= (*Module
)->DllBase
;
2066 /* Open or create dll image section */
2067 Status
= LdrpMapKnownDll(&AdjustedName
, &FullDosName
, &SectionHandle
);
2068 if (!NT_SUCCESS(Status
))
2070 MappedAsDataFile
= (0 != (LoadFlags
& LOAD_LIBRARY_AS_DATAFILE
));
2071 Status
= LdrpMapDllImageFile(SearchPath
, &AdjustedName
, &FullDosName
,
2072 MappedAsDataFile
, &SectionHandle
);
2074 if (!NT_SUCCESS(Status
))
2076 DPRINT1("Failed to create or open dll section of '%wZ' (Status %lx)\n", &AdjustedName
, Status
);
2077 RtlFreeUnicodeString(&AdjustedName
);
2080 RtlFreeUnicodeString(&AdjustedName
);
2081 /* Map the dll into the process */
2084 ArbitraryUserPointer
= NtCurrentTeb()->Tib
.ArbitraryUserPointer
;
2085 NtCurrentTeb()->Tib
.ArbitraryUserPointer
= FullDosName
.Buffer
;
2086 Status
= NtMapViewOfSection(SectionHandle
,
2096 NtCurrentTeb()->Tib
.ArbitraryUserPointer
= ArbitraryUserPointer
;
2097 if (!NT_SUCCESS(Status
))
2099 DPRINT1("map view of section failed (Status 0x%08lx)\n", Status
);
2100 RtlFreeUnicodeString(&FullDosName
);
2101 NtClose(SectionHandle
);
2104 if (NULL
!= BaseAddress
)
2106 *BaseAddress
= ImageBase
;
2108 if (!MappedAsDataFile
)
2110 /* Get and check the NT headers */
2111 NtHeaders
= RtlImageNtHeader(ImageBase
);
2112 if (NtHeaders
== NULL
)
2114 DPRINT1("RtlImageNtHeaders() failed\n");
2115 NtUnmapViewOfSection (NtCurrentProcess (), ImageBase
);
2116 NtClose (SectionHandle
);
2117 RtlFreeUnicodeString(&FullDosName
);
2118 return STATUS_UNSUCCESSFUL
;
2121 DPRINT("Mapped %wZ at %x\n", &FullDosName
, ImageBase
);
2122 if (MappedAsDataFile
)
2124 ASSERT(NULL
!= BaseAddress
);
2125 if (NULL
!= BaseAddress
)
2127 *BaseAddress
= (PVOID
) ((char *) *BaseAddress
+ 1);
2130 RtlFreeUnicodeString(&FullDosName
);
2131 NtClose(SectionHandle
);
2132 return STATUS_SUCCESS
;
2134 /* If the base address is different from the
2135 * one the DLL is actually loaded, perform any
2137 if (ImageBase
!= (PVOID
) NtHeaders
->OptionalHeader
.ImageBase
)
2139 DPRINT1("Relocating (%lx -> %p) %wZ\n",
2140 NtHeaders
->OptionalHeader
.ImageBase
, ImageBase
, &FullDosName
);
2141 Status
= LdrPerformRelocations(NtHeaders
, ImageBase
);
2142 if (!NT_SUCCESS(Status
))
2144 DPRINT1("LdrPerformRelocations() failed\n");
2145 NtUnmapViewOfSection (NtCurrentProcess (), ImageBase
);
2146 NtClose (SectionHandle
);
2147 RtlFreeUnicodeString(&FullDosName
);
2148 return STATUS_UNSUCCESSFUL
;
2151 *Module
= LdrAddModuleEntry(ImageBase
, NtHeaders
, FullDosName
.Buffer
);
2152 (*Module
)->SectionPointer
= SectionHandle
;
2153 if (ImageBase
!= (PVOID
) NtHeaders
->OptionalHeader
.ImageBase
)
2155 (*Module
)->Flags
|= LDRP_IMAGE_NOT_AT_BASE
;
2157 if (NtHeaders
->FileHeader
.Characteristics
& IMAGE_FILE_DLL
)
2159 (*Module
)->Flags
|= LDRP_IMAGE_DLL
;
2161 /* fixup the imported calls entry points */
2162 Status
= LdrFixupImports(SearchPath
, *Module
);
2163 if (!NT_SUCCESS(Status
))
2165 DPRINT1("LdrFixupImports failed for %wZ, status=%x\n", &(*Module
)->BaseDllName
, Status
);
2168 #if defined(DBG) || defined(KDBG)
2169 LdrpLoadUserModuleSymbols(*Module
);
2170 #endif /* DBG || KDBG */
2171 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
2172 InsertTailList(&NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
,
2173 &(*Module
)->InInitializationOrderModuleList
);
2174 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2176 return STATUS_SUCCESS
;
2180 LdrpUnloadModule(PLDR_DATA_TABLE_ENTRY Module
,
2183 PIMAGE_IMPORT_DESCRIPTOR ImportModuleDirectory
;
2184 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptor
;
2185 PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptorCurrent
;
2187 PLDR_DATA_TABLE_ENTRY ImportedModule
;
2194 RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock
);
2197 LoadCount
= LdrpDecrementLoadCount(Module
, Unload
);
2199 TRACE_LDR("Unload %wZ, LoadCount %d\n", &Module
->BaseDllName
, LoadCount
);
2203 /* ?????????????????? */
2205 else if (!(Module
->Flags
& LDRP_STATIC_LINK
) && LoadCount
== 1)
2207 BoundImportDescriptor
= (PIMAGE_BOUND_IMPORT_DESCRIPTOR
)
2208 RtlImageDirectoryEntryToData(Module
->DllBase
,
2210 IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT
,
2212 if (BoundImportDescriptor
)
2214 /* dereferencing all imported modules, use the bound import descriptor */
2215 BoundImportDescriptorCurrent
= BoundImportDescriptor
;
2216 while (BoundImportDescriptorCurrent
->OffsetModuleName
)
2218 ImportedName
= (PCHAR
)BoundImportDescriptor
+ BoundImportDescriptorCurrent
->OffsetModuleName
;
2219 TRACE_LDR("%wZ trys to unload %s\n", &Module
->BaseDllName
, ImportedName
);
2220 Status
= LdrpGetOrLoadModule(NULL
, ImportedName
, &ImportedModule
, FALSE
);
2221 if (!NT_SUCCESS(Status
))
2223 DPRINT1("unable to found imported modul %s\n", ImportedName
);
2227 if (Module
!= ImportedModule
)
2229 Status
= LdrpUnloadModule(ImportedModule
, FALSE
);
2230 if (!NT_SUCCESS(Status
))
2232 DPRINT1("unable to unload %s\n", ImportedName
);
2236 BoundImportDescriptorCurrent
++;
2241 ImportModuleDirectory
= (PIMAGE_IMPORT_DESCRIPTOR
)
2242 RtlImageDirectoryEntryToData(Module
->DllBase
,
2244 IMAGE_DIRECTORY_ENTRY_IMPORT
,
2246 if (ImportModuleDirectory
)
2248 /* dereferencing all imported modules, use the import descriptor */
2249 while (ImportModuleDirectory
->Name
)
2251 ImportedName
= (PCHAR
)Module
->DllBase
+ ImportModuleDirectory
->Name
;
2252 TRACE_LDR("%wZ trys to unload %s\n", &Module
->BaseDllName
, ImportedName
);
2253 Status
= LdrpGetOrLoadModule(NULL
, ImportedName
, &ImportedModule
, FALSE
);
2254 if (!NT_SUCCESS(Status
))
2256 DPRINT1("unable to found imported modul %s\n", ImportedName
);
2260 if (Module
!= ImportedModule
)
2262 Status
= LdrpUnloadModule(ImportedModule
, FALSE
);
2263 if (!NT_SUCCESS(Status
))
2265 DPRINT1("unable to unload %s\n", ImportedName
);
2269 ImportModuleDirectory
++;
2277 if (!(Module
->Flags
& LDRP_STATIC_LINK
))
2279 LdrpDetachProcess(FALSE
);
2282 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2284 return STATUS_SUCCESS
;
2292 LdrUnloadDll (IN PVOID BaseAddress
)
2294 PLDR_DATA_TABLE_ENTRY Module
;
2297 if (BaseAddress
== NULL
)
2298 return STATUS_SUCCESS
;
2300 if (LdrMappedAsDataFile(&BaseAddress
))
2302 Status
= NtUnmapViewOfSection(NtCurrentProcess(), BaseAddress
);
2306 Status
= LdrFindEntryForAddress(BaseAddress
, &Module
);
2307 if (NT_SUCCESS(Status
))
2309 TRACE_LDR("LdrUnloadDll, , unloading %wZ\n", &Module
->BaseDllName
);
2310 Status
= LdrpUnloadModule(Module
, TRUE
);
2321 LdrDisableThreadCalloutsForDll(IN PVOID BaseAddress
)
2323 PLIST_ENTRY ModuleListHead
;
2325 PLDR_DATA_TABLE_ENTRY Module
;
2328 DPRINT("LdrDisableThreadCalloutsForDll (BaseAddress %p)\n", BaseAddress
);
2330 Status
= STATUS_DLL_NOT_FOUND
;
2331 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2332 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2333 Entry
= ModuleListHead
->Flink
;
2334 while (Entry
!= ModuleListHead
)
2336 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2338 DPRINT("BaseDllName %wZ BaseAddress %p\n", &Module
->BaseDllName
, Module
->DllBase
);
2340 if (Module
->DllBase
== BaseAddress
)
2342 if (Module
->TlsIndex
== 0xFFFF)
2344 Module
->Flags
|= LDRP_DONT_CALL_FOR_THREADS
;
2345 Status
= STATUS_SUCCESS
;
2349 Entry
= Entry
->Flink
;
2351 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2359 LdrGetDllHandle(IN PWSTR DllPath OPTIONAL
,
2360 IN PULONG DllCharacteristics
,
2361 IN PUNICODE_STRING DllName
,
2362 OUT PVOID
*DllHandle
)
2364 PLDR_DATA_TABLE_ENTRY Module
;
2367 TRACE_LDR("LdrGetDllHandle, searching for %wZ from %S\n",
2368 DllName
, DllPath
? DllPath
: L
"");
2370 /* NULL is the current executable */
2371 if (DllName
== NULL
)
2373 *DllHandle
= ExeModule
->DllBase
;
2374 DPRINT("BaseAddress 0x%lx\n", *DllHandle
);
2375 return STATUS_SUCCESS
;
2378 Status
= LdrFindEntryForName(DllName
, &Module
, FALSE
);
2379 if (NT_SUCCESS(Status
))
2381 *DllHandle
= Module
->DllBase
;
2382 return STATUS_SUCCESS
;
2385 DPRINT("Failed to find dll %wZ\n", DllName
);
2387 return STATUS_DLL_NOT_FOUND
;
2394 LdrAddRefDll(IN ULONG Flags
,
2395 IN PVOID BaseAddress
)
2397 PLIST_ENTRY ModuleListHead
;
2399 PLDR_DATA_TABLE_ENTRY Module
;
2402 if (Flags
& ~(LDR_PIN_MODULE
))
2404 return STATUS_INVALID_PARAMETER
;
2407 Status
= STATUS_DLL_NOT_FOUND
;
2408 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2409 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2410 Entry
= ModuleListHead
->Flink
;
2411 while (Entry
!= ModuleListHead
)
2413 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2415 if (Module
->DllBase
== BaseAddress
)
2417 if (Flags
& LDR_PIN_MODULE
)
2419 Module
->Flags
|= LDRP_STATIC_LINK
;
2423 LdrpIncrementLoadCount(Module
,
2426 Status
= STATUS_SUCCESS
;
2429 Entry
= Entry
->Flink
;
2431 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2439 RtlPcToFileHeader(IN PVOID PcValue
,
2442 PLIST_ENTRY ModuleListHead
;
2444 PLDR_DATA_TABLE_ENTRY Module
;
2445 PVOID ImageBase
= NULL
;
2447 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2448 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2449 Entry
= ModuleListHead
->Flink
;
2450 while (Entry
!= ModuleListHead
)
2452 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2454 if ((ULONG_PTR
)PcValue
>= (ULONG_PTR
)Module
->DllBase
&&
2455 (ULONG_PTR
)PcValue
< (ULONG_PTR
)Module
->DllBase
+ Module
->SizeOfImage
)
2457 ImageBase
= Module
->DllBase
;
2460 Entry
= Entry
->Flink
;
2462 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2464 *BaseOfImage
= ImageBase
;
2472 LdrGetProcedureAddress (IN PVOID BaseAddress
,
2473 IN PANSI_STRING Name
,
2475 OUT PVOID
*ProcedureAddress
)
2477 if (Name
&& Name
->Length
)
2479 TRACE_LDR("LdrGetProcedureAddress by NAME - %Z\n", Name
);
2483 TRACE_LDR("LdrGetProcedureAddress by ORDINAL - %d\n", Ordinal
);
2486 DPRINT("LdrGetProcedureAddress (BaseAddress %p Name %Z Ordinal %lu ProcedureAddress %p)\n",
2487 BaseAddress
, Name
, Ordinal
, ProcedureAddress
);
2491 if (Name
&& Name
->Length
)
2494 *ProcedureAddress
= LdrGetExportByName(BaseAddress
, (PUCHAR
)Name
->Buffer
, 0xffff);
2495 if (*ProcedureAddress
!= NULL
)
2497 return STATUS_SUCCESS
;
2499 DPRINT("LdrGetProcedureAddress: Can't resolve symbol '%Z'\n", Name
);
2504 Ordinal
&= 0x0000FFFF;
2505 *ProcedureAddress
= LdrGetExportByOrdinal(BaseAddress
, (WORD
)Ordinal
);
2506 if (*ProcedureAddress
)
2508 return STATUS_SUCCESS
;
2510 DPRINT("LdrGetProcedureAddress: Can't resolve symbol @%lu\n", Ordinal
);
2513 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
2515 /* Ignore exception and return */
2518 return STATUS_PROCEDURE_NOT_FOUND
;
2521 /**********************************************************************
2526 * Unload dll's which are no longer referenced from others dll's
2537 * The loader lock must be held on enty.
2540 LdrpDetachProcess(BOOLEAN UnloadAll
)
2542 PLIST_ENTRY ModuleListHead
;
2544 PLDR_DATA_TABLE_ENTRY Module
;
2545 static ULONG CallingCount
= 0;
2547 DPRINT("LdrpDetachProcess() called for %wZ\n",
2548 &ExeModule
->BaseDllName
);
2551 LdrpDllShutdownInProgress
= TRUE
;
2555 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2556 Entry
= ModuleListHead
->Blink
;
2557 while (Entry
!= ModuleListHead
)
2559 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2560 if (((UnloadAll
&& Module
->LoadCount
== 0xFFFF) || Module
->LoadCount
== 0) &&
2561 Module
->Flags
& LDRP_ENTRY_PROCESSED
&&
2562 !(Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
))
2564 Module
->Flags
|= LDRP_UNLOAD_IN_PROGRESS
;
2565 if (Module
== LdrpLastModule
)
2567 LdrpLastModule
= NULL
;
2569 if (Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
)
2571 TRACE_LDR("Unload %wZ - Calling entry point at %x\n",
2572 &Module
->BaseDllName
, Module
->EntryPoint
);
2573 LdrpCallDllEntry(Module
, DLL_PROCESS_DETACH
, (PVOID
)(Module
->LoadCount
== 0xFFFF ? 1 : 0));
2577 TRACE_LDR("Unload %wZ\n", &Module
->BaseDllName
);
2579 Entry
= ModuleListHead
->Blink
;
2583 Entry
= Entry
->Blink
;
2587 if (CallingCount
== 1)
2589 Entry
= ModuleListHead
->Blink
;
2590 while (Entry
!= ModuleListHead
)
2592 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2593 Entry
= Entry
->Blink
;
2594 if (Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
&&
2595 ((UnloadAll
&& Module
->LoadCount
!= 0xFFFF) || Module
->LoadCount
== 0))
2597 /* remove the module entry from the list */
2598 RemoveEntryList (&Module
->InLoadOrderLinks
);
2599 RemoveEntryList (&Module
->InInitializationOrderModuleList
);
2601 NtUnmapViewOfSection (NtCurrentProcess (), Module
->DllBase
);
2602 NtClose (Module
->SectionPointer
);
2604 TRACE_LDR("%wZ unloaded\n", &Module
->BaseDllName
);
2606 RtlFreeUnicodeString (&Module
->FullDllName
);
2607 RtlFreeUnicodeString (&Module
->BaseDllName
);
2609 RtlFreeHeap (RtlGetProcessHeap (), 0, Module
);
2614 DPRINT("LdrpDetachProcess() done\n");
2617 /**********************************************************************
2622 * Initialize all dll's which are prepered for loading
2633 * The loader lock must be held on entry.
2637 LdrpAttachProcess(VOID
)
2639 PLIST_ENTRY ModuleListHead
;
2641 PLDR_DATA_TABLE_ENTRY Module
;
2643 NTSTATUS Status
= STATUS_SUCCESS
;
2645 DPRINT("LdrpAttachProcess() called for %wZ\n",
2646 &ExeModule
->BaseDllName
);
2648 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2649 Entry
= ModuleListHead
->Flink
;
2650 while (Entry
!= ModuleListHead
)
2652 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2653 if (!(Module
->Flags
& (LDRP_LOAD_IN_PROGRESS
|LDRP_UNLOAD_IN_PROGRESS
|LDRP_ENTRY_PROCESSED
)))
2655 Module
->Flags
|= LDRP_LOAD_IN_PROGRESS
;
2656 TRACE_LDR("%wZ loaded - Calling init routine at %x for process attaching\n",
2657 &Module
->BaseDllName
, Module
->EntryPoint
);
2658 Result
= LdrpCallDllEntry(Module
, DLL_PROCESS_ATTACH
, (PVOID
)(Module
->LoadCount
== 0xFFFF ? 1 : 0));
2661 Status
= STATUS_DLL_INIT_FAILED
;
2664 if (Module
->Flags
& LDRP_IMAGE_DLL
&& Module
->EntryPoint
!= 0)
2666 Module
->Flags
|= LDRP_PROCESS_ATTACH_CALLED
|LDRP_ENTRY_PROCESSED
;
2670 Module
->Flags
|= LDRP_ENTRY_PROCESSED
;
2672 Module
->Flags
&= ~LDRP_LOAD_IN_PROGRESS
;
2674 Entry
= Entry
->Flink
;
2677 DPRINT("LdrpAttachProcess() done\n");
2686 RtlDllShutdownInProgress (VOID
)
2688 return LdrpDllShutdownInProgress
;
2695 LdrShutdownProcess (VOID
)
2697 LdrpDetachProcess(TRUE
);
2698 return STATUS_SUCCESS
;
2706 LdrpAttachThread (VOID
)
2708 PLIST_ENTRY ModuleListHead
;
2710 PLDR_DATA_TABLE_ENTRY Module
;
2713 DPRINT("LdrpAttachThread() called for %wZ\n",
2714 &ExeModule
->BaseDllName
);
2716 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2718 Status
= LdrpInitializeTlsForThread();
2720 if (NT_SUCCESS(Status
))
2722 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2723 Entry
= ModuleListHead
->Flink
;
2725 while (Entry
!= ModuleListHead
)
2727 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2728 if (Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
&&
2729 !(Module
->Flags
& LDRP_DONT_CALL_FOR_THREADS
) &&
2730 !(Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
))
2732 TRACE_LDR("%wZ - Calling entry point at %x for thread attaching\n",
2733 &Module
->BaseDllName
, Module
->EntryPoint
);
2734 LdrpCallDllEntry(Module
, DLL_THREAD_ATTACH
, NULL
);
2736 Entry
= Entry
->Flink
;
2739 Entry
= NtCurrentPeb()->Ldr
->InLoadOrderModuleList
.Flink
;
2740 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2741 LdrpTlsCallback(Module
, DLL_THREAD_ATTACH
);
2744 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2746 DPRINT("LdrpAttachThread() done\n");
2756 LdrShutdownThread (VOID
)
2758 PLIST_ENTRY ModuleListHead
;
2760 PLDR_DATA_TABLE_ENTRY Module
;
2762 DPRINT("LdrShutdownThread() called for %wZ\n",
2763 &ExeModule
->BaseDllName
);
2765 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2767 ModuleListHead
= &NtCurrentPeb()->Ldr
->InInitializationOrderModuleList
;
2768 Entry
= ModuleListHead
->Blink
;
2769 while (Entry
!= ModuleListHead
)
2771 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InInitializationOrderModuleList
);
2773 if (Module
->Flags
& LDRP_PROCESS_ATTACH_CALLED
&&
2774 !(Module
->Flags
& LDRP_DONT_CALL_FOR_THREADS
) &&
2775 !(Module
->Flags
& LDRP_UNLOAD_IN_PROGRESS
))
2777 TRACE_LDR("%wZ - Calling entry point at %x for thread detaching\n",
2778 &Module
->BaseDllName
, Module
->EntryPoint
);
2779 LdrpCallDllEntry(Module
, DLL_THREAD_DETACH
, NULL
);
2781 Entry
= Entry
->Blink
;
2784 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2788 RtlFreeHeap (RtlGetProcessHeap(), 0, NtCurrentTeb()->ThreadLocalStoragePointer
);
2791 DPRINT("LdrShutdownThread() done\n");
2793 return STATUS_SUCCESS
;
2797 /***************************************************************************
2799 * LdrQueryProcessModuleInformation
2814 LdrQueryProcessModuleInformation(IN PRTL_PROCESS_MODULES ModuleInformation OPTIONAL
,
2815 IN ULONG Size OPTIONAL
,
2816 OUT PULONG ReturnedSize
)
2818 PLIST_ENTRY ModuleListHead
;
2820 PLDR_DATA_TABLE_ENTRY Module
;
2821 PRTL_PROCESS_MODULE_INFORMATION ModulePtr
= NULL
;
2822 NTSTATUS Status
= STATUS_SUCCESS
;
2823 ULONG UsedSize
= sizeof(ULONG
);
2824 ANSI_STRING AnsiString
;
2827 DPRINT("LdrQueryProcessModuleInformation() called\n");
2828 // FIXME: This code is ultra-duplicated. see lib\rtl\dbgbuffer.c
2829 RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock
);
2831 if (ModuleInformation
== NULL
|| Size
== 0)
2833 Status
= STATUS_INFO_LENGTH_MISMATCH
;
2837 ModuleInformation
->NumberOfModules
= 0;
2838 ModulePtr
= &ModuleInformation
->Modules
[0];
2839 Status
= STATUS_SUCCESS
;
2842 ModuleListHead
= &NtCurrentPeb()->Ldr
->InLoadOrderModuleList
;
2843 Entry
= ModuleListHead
->Flink
;
2845 while (Entry
!= ModuleListHead
)
2847 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
2849 DPRINT(" Module %wZ\n",
2850 &Module
->FullDllName
);
2852 if (UsedSize
> Size
)
2854 Status
= STATUS_INFO_LENGTH_MISMATCH
;
2856 else if (ModuleInformation
!= NULL
)
2858 ModulePtr
->Section
= 0;
2859 ModulePtr
->MappedBase
= NULL
; // FIXME: ??
2860 ModulePtr
->ImageBase
= Module
->DllBase
;
2861 ModulePtr
->ImageSize
= Module
->SizeOfImage
;
2862 ModulePtr
->Flags
= Module
->Flags
;
2863 ModulePtr
->LoadOrderIndex
= 0; // FIXME: ??
2864 ModulePtr
->InitOrderIndex
= 0; // FIXME: ??
2865 ModulePtr
->LoadCount
= Module
->LoadCount
;
2867 AnsiString
.Length
= 0;
2868 AnsiString
.MaximumLength
= 256;
2869 AnsiString
.Buffer
= ModulePtr
->FullPathName
;
2870 RtlUnicodeStringToAnsiString(&AnsiString
,
2871 &Module
->FullDllName
,
2874 p
= strrchr(ModulePtr
->FullPathName
, '\\');
2876 ModulePtr
->OffsetToFileName
= p
- ModulePtr
->FullPathName
+ 1;
2878 ModulePtr
->OffsetToFileName
= 0;
2881 ModuleInformation
->NumberOfModules
++;
2883 UsedSize
+= sizeof(RTL_PROCESS_MODULE_INFORMATION
);
2885 Entry
= Entry
->Flink
;
2888 RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock
);
2890 if (ReturnedSize
!= 0)
2891 *ReturnedSize
= UsedSize
;
2893 DPRINT("LdrQueryProcessModuleInformation() done\n");
2900 LdrpCheckImageChecksum (IN PVOID BaseAddress
,
2903 PIMAGE_NT_HEADERS Header
;
2910 Header
= RtlImageNtHeader (BaseAddress
);
2914 HeaderSum
= Header
->OptionalHeader
.CheckSum
;
2919 Ptr
= (PUSHORT
) BaseAddress
;
2920 for (i
= 0; i
< ImageSize
/ sizeof (USHORT
); i
++)
2923 if (HIWORD(Sum
) != 0)
2925 Sum
= LOWORD(Sum
) + HIWORD(Sum
);
2932 Sum
+= (ULONG
)*((PUCHAR
)Ptr
);
2933 if (HIWORD(Sum
) != 0)
2935 Sum
= LOWORD(Sum
) + HIWORD(Sum
);
2939 CalcSum
= (USHORT
)(LOWORD(Sum
) + HIWORD(Sum
));
2941 /* Subtract image checksum from calculated checksum. */
2942 /* fix low word of checksum */
2943 if (LOWORD(CalcSum
) >= LOWORD(HeaderSum
))
2945 CalcSum
-= LOWORD(HeaderSum
);
2949 CalcSum
= ((LOWORD(CalcSum
) - LOWORD(HeaderSum
)) & 0xFFFF) - 1;
2952 /* fix high word of checksum */
2953 if (LOWORD(CalcSum
) >= HIWORD(HeaderSum
))
2955 CalcSum
-= HIWORD(HeaderSum
);
2959 CalcSum
= ((LOWORD(CalcSum
) - HIWORD(HeaderSum
)) & 0xFFFF) - 1;
2962 /* add file length */
2963 CalcSum
+= ImageSize
;
2965 return (BOOLEAN
)(CalcSum
== HeaderSum
);
2969 * Compute size of an image as it is actually present in virt memory
2970 * (i.e. excluding NEVER_LOAD sections)
2973 LdrpGetResidentSize(PIMAGE_NT_HEADERS NTHeaders
)
2975 PIMAGE_SECTION_HEADER SectionHeader
;
2976 unsigned SectionIndex
;
2979 SectionHeader
= (PIMAGE_SECTION_HEADER
)((char *) &NTHeaders
->OptionalHeader
2980 + NTHeaders
->FileHeader
.SizeOfOptionalHeader
);
2982 for (SectionIndex
= 0; SectionIndex
< NTHeaders
->FileHeader
.NumberOfSections
; SectionIndex
++)
2984 if (0 == (SectionHeader
->Characteristics
& IMAGE_SCN_LNK_REMOVE
)
2985 && ResidentSize
< SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
)
2987 ResidentSize
= SectionHeader
->VirtualAddress
+ SectionHeader
->Misc
.VirtualSize
;
2992 return ResidentSize
;
2996 /***************************************************************************
2998 * LdrVerifyImageMatchesChecksum
3013 LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle
,
3018 FILE_STANDARD_INFORMATION FileInfo
;
3019 IO_STATUS_BLOCK IoStatusBlock
;
3020 HANDLE SectionHandle
;
3026 DPRINT ("LdrVerifyImageMatchesChecksum() called\n");
3028 Status
= NtCreateSection (&SectionHandle
,
3035 if (!NT_SUCCESS(Status
))
3037 DPRINT1 ("NtCreateSection() failed (Status %lx)\n", Status
);
3043 Status
= NtMapViewOfSection (SectionHandle
,
3044 NtCurrentProcess (),
3053 if (!NT_SUCCESS(Status
))
3055 DPRINT1 ("NtMapViewOfSection() failed (Status %lx)\n", Status
);
3056 NtClose (SectionHandle
);
3060 Status
= NtQueryInformationFile (FileHandle
,
3063 sizeof (FILE_STANDARD_INFORMATION
),
3064 FileStandardInformation
);
3065 if (!NT_SUCCESS(Status
))
3067 DPRINT1 ("NtMapViewOfSection() failed (Status %lx)\n", Status
);
3068 NtUnmapViewOfSection (NtCurrentProcess (),
3070 NtClose (SectionHandle
);
3074 Result
= LdrpCheckImageChecksum (BaseAddress
,
3075 FileInfo
.EndOfFile
.u
.LowPart
);
3076 if (Result
== FALSE
)
3078 Status
= STATUS_IMAGE_CHECKSUM_MISMATCH
;
3081 NtUnmapViewOfSection (NtCurrentProcess (),
3084 NtClose (SectionHandle
);
3090 /***************************************************************************
3092 * LdrQueryImageFileExecutionOptions
3107 LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey
,
3108 IN PCWSTR ValueName
,
3111 IN ULONG BufferSize
,
3112 OUT PULONG ReturnedLength OPTIONAL
)
3114 PKEY_VALUE_PARTIAL_INFORMATION KeyInfo
;
3115 OBJECT_ATTRIBUTES ObjectAttributes
;
3116 UNICODE_STRING ValueNameString
;
3117 UNICODE_STRING KeyName
;
3118 WCHAR NameBuffer
[256];
3126 L
"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\");
3127 Ptr
= wcsrchr (SubKey
->Buffer
, L
'\\');
3130 Ptr
= SubKey
->Buffer
;
3136 wcscat (NameBuffer
, Ptr
);
3137 RtlInitUnicodeString (&KeyName
,
3140 InitializeObjectAttributes (&ObjectAttributes
,
3142 OBJ_CASE_INSENSITIVE
,
3146 Status
= NtOpenKey (&KeyHandle
,
3149 if (!NT_SUCCESS(Status
))
3151 DPRINT ("NtOpenKey() failed (Status %lx)\n", Status
);
3155 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + 32;
3156 KeyInfo
= RtlAllocateHeap (RtlGetProcessHeap(),
3159 if (KeyInfo
== NULL
)
3161 NtClose (KeyHandle
);
3162 return STATUS_INSUFFICIENT_RESOURCES
;
3165 RtlInitUnicodeString (&ValueNameString
,
3167 Status
= NtQueryValueKey (KeyHandle
,
3169 KeyValuePartialInformation
,
3173 if (Status
== STATUS_BUFFER_OVERFLOW
)
3175 KeyInfoSize
= sizeof(KEY_VALUE_PARTIAL_INFORMATION
) + KeyInfo
->DataLength
;
3176 RtlFreeHeap (RtlGetProcessHeap(),
3179 KeyInfo
= RtlAllocateHeap (RtlGetProcessHeap(),
3182 if (KeyInfo
== NULL
)
3184 NtClose (KeyHandle
);
3185 return STATUS_INSUFFICIENT_RESOURCES
;
3188 Status
= NtQueryValueKey (KeyHandle
,
3190 KeyValuePartialInformation
,
3195 NtClose (KeyHandle
);
3197 if (!NT_SUCCESS(Status
))
3199 if (KeyInfo
!= NULL
)
3201 RtlFreeHeap (RtlGetProcessHeap(),
3208 if (KeyInfo
->Type
!= Type
)
3210 RtlFreeHeap (RtlGetProcessHeap(),
3213 return STATUS_OBJECT_TYPE_MISMATCH
;
3216 ResultSize
= BufferSize
;
3217 if (ResultSize
< KeyInfo
->DataLength
)
3219 Status
= STATUS_BUFFER_OVERFLOW
;
3223 ResultSize
= KeyInfo
->DataLength
;
3225 RtlCopyMemory (Buffer
,
3229 RtlFreeHeap (RtlGetProcessHeap(),
3233 if (ReturnedLength
!= NULL
)
3235 *ReturnedLength
= ResultSize
;
3242 PIMAGE_BASE_RELOCATION NTAPI
3243 LdrProcessRelocationBlock(IN ULONG_PTR Address
,
3245 IN PUSHORT TypeOffset
,
3254 for (i
= 0; i
< Count
; i
++)
3256 Offset
= *TypeOffset
& 0xFFF;
3257 Type
= *TypeOffset
>> 12;
3261 case IMAGE_REL_BASED_ABSOLUTE
:
3264 case IMAGE_REL_BASED_HIGH
:
3265 ShortPtr
= (PUSHORT
)((ULONG_PTR
)Address
+ Offset
);
3266 *ShortPtr
+= HIWORD(Delta
);
3269 case IMAGE_REL_BASED_LOW
:
3270 ShortPtr
= (PUSHORT
)((ULONG_PTR
)Address
+ Offset
);
3271 *ShortPtr
+= LOWORD(Delta
);
3274 case IMAGE_REL_BASED_HIGHLOW
:
3275 LongPtr
= (PULONG
)((ULONG_PTR
)Address
+ Offset
);
3279 case IMAGE_REL_BASED_HIGHADJ
:
3280 case IMAGE_REL_BASED_MIPS_JMPADDR
:
3282 DPRINT1("Unknown/unsupported fixup type %hu.\n", Type
);
3289 return (PIMAGE_BASE_RELOCATION
)TypeOffset
;
3294 LdrLockLoaderLock(IN ULONG Flags
,
3295 OUT PULONG Disposition OPTIONAL
,
3296 OUT PULONG Cookie OPTIONAL
)
3299 return STATUS_NOT_IMPLEMENTED
;
3304 LdrUnlockLoaderLock(IN ULONG Flags
,
3305 IN ULONG Cookie OPTIONAL
)
3308 return STATUS_NOT_IMPLEMENTED
;
3313 LdrUnloadAlternateResourceModule(IN PVOID BaseAddress
)