diff --git a/reactos/include/ntdll/ldr.h b/reactos/include/ntdll/ldr.h index 675e1a9303c..05d2de392c2 100644 --- a/reactos/include/ntdll/ldr.h +++ b/reactos/include/ntdll/ldr.h @@ -59,7 +59,7 @@ typedef struct _LDR_MODULE LIST_ENTRY InInitializationOrderModuleList; /* not used */ PVOID BaseAddress; ULONG EntryPoint; - ULONG SizeOfImage; + ULONG ResidentSize; UNICODE_STRING FullDllName; UNICODE_STRING BaseDllName; ULONG Flags; @@ -92,6 +92,9 @@ LdrpLoadUserModuleSymbols(PLDR_MODULE LdrModule); #endif +ULONG +LdrpGetResidentSize(PIMAGE_NT_HEADERS NTHeaders); + PEPFUNC LdrPEStartup (PVOID ImageBase, HANDLE SectionHandle, PLDR_MODULE* Module, diff --git a/reactos/lib/ntdll/ldr/startup.c b/reactos/lib/ntdll/ldr/startup.c index 72c85a3d986..f16e6a7d786 100644 --- a/reactos/lib/ntdll/ldr/startup.c +++ b/reactos/lib/ntdll/ldr/startup.c @@ -222,7 +222,6 @@ finish: return FALSE; } - /* FUNCTIONS *****************************************************************/ VOID STDCALL @@ -382,7 +381,7 @@ __true_LdrInitializeThunk (ULONG Unknown1, NtModule->CheckSum = 0; NTHeaders = RtlImageNtHeader (NtModule->BaseAddress); - NtModule->SizeOfImage = NTHeaders->OptionalHeader.SizeOfImage; + NtModule->ResidentSize = LdrpGetResidentSize(NTHeaders); NtModule->TimeDateStamp = NTHeaders->FileHeader.TimeDateStamp; InsertTailList(&Peb->Ldr->InLoadOrderModuleList, @@ -430,7 +429,7 @@ __true_LdrInitializeThunk (ULONG Unknown1, ExeModule->CheckSum = 0; NTHeaders = RtlImageNtHeader (ExeModule->BaseAddress); - ExeModule->SizeOfImage = NTHeaders->OptionalHeader.SizeOfImage; + ExeModule->ResidentSize = LdrpGetResidentSize(NTHeaders); ExeModule->TimeDateStamp = NTHeaders->FileHeader.TimeDateStamp; InsertHeadList(&Peb->Ldr->InLoadOrderModuleList, diff --git a/reactos/lib/ntdll/ldr/utils.c b/reactos/lib/ntdll/ldr/utils.c index d8245fb0a12..0ef7154e0e4 100644 --- a/reactos/lib/ntdll/ldr/utils.c +++ b/reactos/lib/ntdll/ldr/utils.c @@ -468,7 +468,7 @@ LdrAddModuleEntry(PVOID ImageBase, Module->EntryPoint = NTHeaders->OptionalHeader.AddressOfEntryPoint; if (Module->EntryPoint != 0) Module->EntryPoint += (ULONG)Module->BaseAddress; - Module->SizeOfImage = NTHeaders->OptionalHeader.SizeOfImage; + Module->ResidentSize = LdrpGetResidentSize(NTHeaders); if (NtCurrentPeb()->Ldr->Initialized == TRUE) { /* loading while app is running */ @@ -799,7 +799,7 @@ LdrFindEntryForAddress(PVOID Address, DPRINT("Scanning %wZ at %p\n", &ModulePtr->BaseDllName, ModulePtr->BaseAddress); if ((Address >= ModulePtr->BaseAddress) && - (Address <= (ModulePtr->BaseAddress + ModulePtr->SizeOfImage))) + (Address <= (ModulePtr->BaseAddress + ModulePtr->ResidentSize))) { *Module = ModulePtr; RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock); @@ -1581,7 +1581,7 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, NTHeaders = RtlImageNtHeader (ImportedModule->BaseAddress); Start = (PVOID)NTHeaders->OptionalHeader.ImageBase; - End = Start + ImportedModule->SizeOfImage; + End = Start + ImportedModule->ResidentSize; Offset = ImportedModule->BaseAddress - Start; /* Walk through function list and fixup addresses. */ @@ -2708,7 +2708,7 @@ LdrQueryProcessModuleInformation(IN PMODULE_INFORMATION ModuleInformation OPTION { ModulePtr->Reserved[0] = ModulePtr->Reserved[1] = 0; // FIXME: ?? ModulePtr->Base = Module->BaseAddress; - ModulePtr->Size = Module->SizeOfImage; + ModulePtr->Size = Module->ResidentSize; ModulePtr->Flags = Module->Flags; ModulePtr->Index = 0; // FIXME: index ?? ModulePtr->Unknown = 0; // FIXME: ?? @@ -2814,6 +2814,33 @@ LdrpCheckImageChecksum (IN PVOID BaseAddress, return (BOOLEAN)(CalcSum == HeaderSum); } +/* + * Compute size of an image as it is actually present in virt memory + * (i.e. excluding NEVER_LOAD sections) + */ +ULONG +LdrpGetResidentSize(PIMAGE_NT_HEADERS NTHeaders) +{ + PIMAGE_SECTION_HEADER SectionHeader; + unsigned SectionIndex; + ULONG ResidentSize; + + SectionHeader = (PIMAGE_SECTION_HEADER)((char *) &NTHeaders->OptionalHeader + + NTHeaders->FileHeader.SizeOfOptionalHeader); + ResidentSize = 0; + for (SectionIndex = 0; SectionIndex < NTHeaders->FileHeader.NumberOfSections; SectionIndex++) + { + if (0 == (SectionHeader->Characteristics & IMAGE_SCN_LNK_REMOVE) + && ResidentSize < SectionHeader->VirtualAddress + SectionHeader->Misc.VirtualSize) + { + ResidentSize = SectionHeader->VirtualAddress + SectionHeader->Misc.VirtualSize; + } + SectionHeader++; + } + + return ResidentSize; +} + /*************************************************************************** * NAME EXPORTED diff --git a/reactos/lib/ntdll/rtl/dbgbuffer.c b/reactos/lib/ntdll/rtl/dbgbuffer.c index f490f75ec18..707635d40c8 100644 --- a/reactos/lib/ntdll/rtl/dbgbuffer.c +++ b/reactos/lib/ntdll/rtl/dbgbuffer.c @@ -219,7 +219,7 @@ RtlpQueryRemoteProcessModules(HANDLE ProcessHandle, { ModulePtr->Reserved[0] = ModulePtr->Reserved[1] = 0; // FIXME: ?? ModulePtr->Base = lmModule.BaseAddress; - ModulePtr->Size = lmModule.SizeOfImage; + ModulePtr->Size = lmModule.ResidentSize; ModulePtr->Flags = lmModule.Flags; ModulePtr->Index = 0; // FIXME: ?? ModulePtr->Unknown = 0; // FIXME: ?? diff --git a/reactos/lib/psapi/psapi.c b/reactos/lib/psapi/psapi.c index 29c4029cd98..34b79d10fb6 100644 --- a/reactos/lib/psapi/psapi.c +++ b/reactos/lib/psapi/psapi.c @@ -521,10 +521,10 @@ exitWithStatus: } /* image size */ - if(nSize >= sizeof(CurrentModule->SizeOfImage)) + if(nSize >= sizeof(CurrentModule->ResidentSize)) { - Context->lpmodinfo->SizeOfImage = CurrentModule->SizeOfImage; - nSize -= sizeof(CurrentModule->SizeOfImage); + Context->lpmodinfo->SizeOfImage = CurrentModule->ResidentSize; + nSize -= sizeof(CurrentModule->ResidentSize); } /* entry point */ diff --git a/reactos/ntoskrnl/dbg/kdb_symbols.c b/reactos/ntoskrnl/dbg/kdb_symbols.c index 3780be0d6df..618711e6be0 100644 --- a/reactos/ntoskrnl/dbg/kdb_symbols.c +++ b/reactos/ntoskrnl/dbg/kdb_symbols.c @@ -92,7 +92,7 @@ KdbpSymFindUserModule(IN PVOID Address OPTIONAL, current = CONTAINING_RECORD(current_entry, LDR_MODULE, InLoadOrderModuleList); if ((Address != NULL && (Address >= (PVOID)current->BaseAddress && - Address < (PVOID)((char *)current->BaseAddress + current->SizeOfImage))) || + Address < (PVOID)((char *)current->BaseAddress + current->ResidentSize))) || (Name != NULL && _wcsicmp(current->BaseDllName.Buffer, Name) == 0) || (Index >= 0 && Count++ == Index)) { @@ -102,7 +102,7 @@ KdbpSymFindUserModule(IN PVOID Address OPTIONAL, wcsncpy(pInfo->Name, current->BaseDllName.Buffer, Length); pInfo->Name[Length] = L'\0'; pInfo->Base = (ULONG_PTR)current->BaseAddress; - pInfo->Size = current->SizeOfImage; + pInfo->Size = current->ResidentSize; pInfo->RosSymInfo = current->RosSymInfo; return TRUE; } diff --git a/reactos/ntoskrnl/ke/i386/usertrap.c b/reactos/ntoskrnl/ke/i386/usertrap.c index 6bbf0efef04..2307fe9099c 100644 --- a/reactos/ntoskrnl/ke/i386/usertrap.c +++ b/reactos/ntoskrnl/ke/i386/usertrap.c @@ -1,4 +1,4 @@ -/* $Id:$ +/* $Id$ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -54,7 +54,7 @@ print_user_address(PVOID address) CONTAINING_RECORD(current_entry, LDR_MODULE, InLoadOrderModuleList); if (address >= (PVOID)current->BaseAddress && - address < (PVOID)((char*)current->BaseAddress + current->SizeOfImage)) + address < (PVOID)((char*)current->BaseAddress + current->ResidentSize)) { RelativeAddress = (ULONG_PTR) address - (ULONG_PTR)current->BaseAddress;