00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
#if defined(NTOS_KERNEL_RUNTIME)
00029
#include "ntos.h"
00030
#else
00031
#include <nt.h>
00032
#include <ntrtl.h>
00033
#include <nturtl.h>
00034
#endif
00035
00036
#if !defined(NTOS_KERNEL_RUNTIME)
00037 extern PVOID
NtDllBase;
00038
#endif
00039
00040
00041 PVOID
00042 RtlPcToFileHeader(
00043 IN PVOID PcValue,
00044 OUT PVOID *BaseOfImage
00045 )
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075 {
00076
00077
#if defined(NTOS_KERNEL_RUNTIME)
00078
00079
extern LIST_ENTRY
PsLoadedModuleList;
00080
extern KSPIN_LOCK
PsLoadedModuleSpinLock;
00081
00082 PVOID Base;
00083 ULONG_PTR Bounds;
00084 PLDR_DATA_TABLE_ENTRY Entry;
00085 PLIST_ENTRY Next;
00086 KIRQL OldIrql;
00087
00088
00089
00090
00091
00092
00093 ExAcquireSpinLock(&
PsLoadedModuleSpinLock, &OldIrql);
00094 Next =
PsLoadedModuleList.Flink;
00095
if (Next !=
NULL) {
00096
while (Next != &
PsLoadedModuleList) {
00097 Entry = CONTAINING_RECORD(Next,
00098 LDR_DATA_TABLE_ENTRY,
00099 InLoadOrderLinks);
00100
00101 Next = Next->Flink;
00102 Base = Entry->DllBase;
00103 Bounds = (ULONG_PTR)Base + Entry->SizeOfImage;
00104
if (((ULONG_PTR)PcValue >= (ULONG_PTR)Base) && ((ULONG_PTR)PcValue < Bounds)) {
00105 ExReleaseSpinLock(&
PsLoadedModuleSpinLock, OldIrql);
00106 *BaseOfImage = Base;
00107
return Base;
00108 }
00109 }
00110 }
00111
00112
00113
00114
00115
00116 ExReleaseSpinLock(&
PsLoadedModuleSpinLock, OldIrql);
00117 *BaseOfImage =
NULL;
00118
return NULL;
00119
00120
#else
00121
00122 PVOID Base;
00123 ULONG_PTR Bounds;
00124 PLDR_DATA_TABLE_ENTRY Entry;
00125 PLIST_ENTRY ModuleListHead;
00126 PLIST_ENTRY Next;
00127 PIMAGE_NT_HEADERS NtHeaders;
00128 PPEB Peb;
00129 PTEB Teb;
00130 MEMORY_BASIC_INFORMATION MemInfo;
00131
NTSTATUS st;
00132
00133
00134
00135
00136
00137
00138
00139
if ( !RtlTryEnterCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock) ) {
00140
00141
00142
00143
00144
00145
00146 st =
NtQueryVirtualMemory(
00147 NtCurrentProcess(),
00148 PcValue,
00149 MemoryBasicInformation,
00150 (PVOID)&MemInfo,
00151
sizeof(MemInfo),
00152
NULL
00153 );
00154
if ( !
NT_SUCCESS(st) ) {
00155 MemInfo.AllocationBase =
NULL;;
00156 }
00157
else {
00158
if ( MemInfo.Type == MEM_IMAGE ) {
00159
try {
00160 *BaseOfImage = MemInfo.AllocationBase;
00161 }
00162 except (
EXCEPTION_EXECUTE_HANDLER) {
00163 MemInfo.AllocationBase =
NULL;
00164 }
00165 }
00166
else {
00167 MemInfo.AllocationBase =
NULL;;
00168 }
00169 }
00170
return MemInfo.AllocationBase;
00171 }
00172
00173
try {
00174 Teb = NtCurrentTeb();
00175
if (Teb !=
NULL) {
00176 Peb = Teb->ProcessEnvironmentBlock;
00177
if (Peb->Ldr !=
NULL) {
00178 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
00179 Next = ModuleListHead->Flink;
00180
if (Next !=
NULL) {
00181
while (Next != ModuleListHead) {
00182 Entry = CONTAINING_RECORD(Next,
00183 LDR_DATA_TABLE_ENTRY,
00184 InLoadOrderLinks);
00185
00186 Next = Next->Flink;
00187 Base = Entry->DllBase;
00188 NtHeaders =
RtlImageNtHeader(Base);
00189 Bounds = (ULONG_PTR)Base + NtHeaders->OptionalHeader.SizeOfImage;
00190
if (((ULONG_PTR)PcValue >= (ULONG_PTR)Base) && ((ULONG_PTR)PcValue < Bounds)) {
00191 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
00192 *BaseOfImage = Base;
00193
return Base;
00194 }
00195 }
00196 }
00197
00198 }
else {
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
if (
NtDllBase !=
NULL ) {
00212 Base =
NtDllBase;
00213 NtHeaders =
RtlImageNtHeader( Base );
00214 Bounds = (ULONG_PTR)Base + NtHeaders->OptionalHeader.SizeOfImage;
00215
if (((ULONG_PTR)PcValue >= (ULONG_PTR)Base) && ((ULONG_PTR)PcValue < Bounds)) {
00216 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
00217 *BaseOfImage = Base;
00218
return Base;
00219 }
00220 }
00221 }
00222 }
00223
00224 } except(
EXCEPTION_EXECUTE_HANDLER) {
00225 NOTHING;
00226 }
00227
00228
00229
00230
00231
00232 RtlLeaveCriticalSection((PRTL_CRITICAL_SECTION)NtCurrentPeb()->
LoaderLock);
00233 *BaseOfImage =
NULL;
00234
return NULL;
00235
00236
#endif
00237
00238 }