ReactOS  0.4.12-dev-75-g00dd17e
image.c File Reference
#include <rtl.h>
#include <debug.h>
Include dependency graph for image.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define RVA(m, b)   ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))
 

Functions

FORCEINLINE USHORT ChkSum (ULONG Sum, PUSHORT Src, ULONG Len)
 
BOOLEAN NTAPI LdrVerifyMappedImageMatchesChecksum (IN PVOID BaseAddress, IN SIZE_T ImageSize, IN ULONG FileLength)
 
NTSTATUS NTAPI RtlImageNtHeaderEx (_In_ ULONG Flags, _In_ PVOID Base, _In_ ULONG64 Size, _Out_ PIMAGE_NT_HEADERS *OutHeaders)
 
PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader (IN PVOID Base)
 
PVOID NTAPI RtlImageDirectoryEntryToData (PVOID BaseAddress, BOOLEAN MappedAsImage, USHORT Directory, PULONG Size)
 
PIMAGE_SECTION_HEADER NTAPI RtlImageRvaToSection (PIMAGE_NT_HEADERS NtHeader, PVOID BaseAddress, ULONG Rva)
 
PVOID NTAPI RtlImageRvaToVa (PIMAGE_NT_HEADERS NtHeader, PVOID BaseAddress, ULONG Rva, PIMAGE_SECTION_HEADER *SectionHeader)
 
PIMAGE_BASE_RELOCATION NTAPI LdrProcessRelocationBlockLongLong (IN ULONG_PTR Address, IN ULONG Count, IN PUSHORT TypeOffset, IN LONGLONG Delta)
 
ULONG NTAPI LdrRelocateImage (IN PVOID BaseAddress, IN PCCH LoaderName, IN ULONG Success, IN ULONG Conflict, IN ULONG Invalid)
 
ULONG NTAPI LdrRelocateImageWithBias (IN PVOID BaseAddress, IN LONGLONG AdditionalBias, IN PCCH LoaderName, IN ULONG Success, IN ULONG Conflict, IN ULONG Invalid)
 

Macro Definition Documentation

◆ NDEBUG

#define NDEBUG

Definition at line 17 of file image.c.

◆ RVA

#define RVA (   m,
  b 
)    ((PVOID)((ULONG_PTR)(b) + (ULONG_PTR)(m)))

Definition at line 20 of file image.c.

Referenced by LdrProcessRelocationBlockLongLong(), and LdrRelocateImageWithBias().

Function Documentation

◆ ChkSum()

FORCEINLINE USHORT ChkSum ( ULONG  Sum,
PUSHORT  Src,
ULONG  Len 
)

Definition at line 26 of file image.c.

27 {
28  ULONG i;
29 
30  for (i=0; i<Len; i++)
31  {
32  /* Sum up the current word */
33  Sum += Src[i];
34 
35  /* Sum up everything above the low word as a carry */
36  Sum = (Sum & 0xFFFF) + (Sum >> 16);
37  }
38 
39  /* Apply carry one more time and clamp to the USHORT */
40  return (Sum + (Sum >> 16)) & 0xFFFF;
41 }
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define Len
Definition: deflate.h:82
unsigned int ULONG
Definition: retypes.h:1

◆ LdrProcessRelocationBlockLongLong()

PIMAGE_BASE_RELOCATION NTAPI LdrProcessRelocationBlockLongLong ( IN ULONG_PTR  Address,
IN ULONG  Count,
IN PUSHORT  TypeOffset,
IN LONGLONG  Delta 
)

Definition at line 372 of file image.c.

Referenced by LdrRelocateImageWithBias().

377 {
378  SHORT Offset;
379  USHORT Type;
380  ULONG i;
381  PUSHORT ShortPtr;
382  PULONG LongPtr;
383  PULONGLONG LongLongPtr;
384 
385  for (i = 0; i < Count; i++)
386  {
387  Offset = SWAPW(*TypeOffset) & 0xFFF;
388  Type = SWAPW(*TypeOffset) >> 12;
389  ShortPtr = (PUSHORT)(RVA(Address, Offset));
390  /*
391  * Don't relocate within the relocation section itself.
392  * GCC/LD generates sometimes relocation records for the relocation section.
393  * This is a bug in GCC/LD.
394  * Fix for it disabled, since it was only in ntoskrnl and not in ntdll
395  */
396  /*
397  if ((ULONG_PTR)ShortPtr < (ULONG_PTR)RelocationDir ||
398  (ULONG_PTR)ShortPtr >= (ULONG_PTR)RelocationEnd)
399  {*/
400  switch (Type)
401  {
402  /* case IMAGE_REL_BASED_SECTION : */
403  /* case IMAGE_REL_BASED_REL32 : */
405  break;
406 
408  *ShortPtr = HIWORD(MAKELONG(0, *ShortPtr) + (Delta & 0xFFFFFFFF));
409  break;
410 
411  case IMAGE_REL_BASED_LOW:
412  *ShortPtr = SWAPW(*ShortPtr) + LOWORD(Delta & 0xFFFF);
413  break;
414 
416  LongPtr = (PULONG)RVA(Address, Offset);
417  *LongPtr = SWAPD(*LongPtr) + (Delta & 0xFFFFFFFF);
418  break;
419 
421  LongLongPtr = (PUINT64)RVA(Address, Offset);
422  *LongLongPtr = SWAPQ(*LongLongPtr) + Delta;
423  break;
424 
427  default:
428  DPRINT1("Unknown/unsupported fixup type %hu.\n", Type);
429  DPRINT1("Address %p, Current %u, Count %u, *TypeOffset %x\n",
430  (PVOID)Address, i, Count, SWAPW(*TypeOffset));
432  }
433 
434  TypeOffset++;
435  }
436 
437  return (PIMAGE_BASE_RELOCATION)TypeOffset;
438 }
#define IMAGE_REL_BASED_DIR64
Definition: winnt_old.h:890
Type
Definition: Type.h:6
#define SWAPW(x)
Definition: bytesex.h:8
#define SWAPD(x)
Definition: bytesex.h:7
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define IMAGE_REL_BASED_HIGHLOW
Definition: winnt_old.h:885
#define IMAGE_REL_BASED_ABSOLUTE
Definition: winnt_old.h:882
GLenum GLclampf GLint i
Definition: glfuncs.h:14
_In_ ULONG _In_ ULONG Offset
Definition: ntddpcm.h:101
short SHORT
Definition: pedump.c:59
#define IMAGE_REL_BASED_MIPS_JMPADDR
Definition: winnt_old.h:887
#define IMAGE_REL_BASED_LOW
Definition: winnt_old.h:884
#define MAKELONG(a, b)
Definition: typedefs.h:248
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
#define IMAGE_REL_BASED_HIGHADJ
Definition: winnt_old.h:886
#define SWAPQ(x)
Definition: rtlp.h:26
#define IMAGE_REL_BASED_HIGH
Definition: winnt_old.h:883
unsigned short USHORT
Definition: pedump.c:61
static ULONG Delta
Definition: xboxvideo.c:28
unsigned int * PULONG
Definition: retypes.h:1
#define DPRINT1
Definition: precomp.h:8
__GNU_EXTENSION typedef unsigned __int64 * PULONGLONG
Definition: ntbasedef.h:390
#define HIWORD(l)
Definition: typedefs.h:246
unsigned int ULONG
Definition: retypes.h:1
#define RVA(m, b)
Definition: image.c:20
#define LOWORD(l)
Definition: pedump.c:82
unsigned short * PUSHORT
Definition: retypes.h:2
unsigned __int64 * PUINT64
Definition: basetsd.h:189

◆ LdrRelocateImage()

ULONG NTAPI LdrRelocateImage ( IN PVOID  BaseAddress,
IN PCCH  LoaderName,
IN ULONG  Success,
IN ULONG  Conflict,
IN ULONG  Invalid 
)

Definition at line 442 of file image.c.

Referenced by ImgpLoadPEImage().

448 {
449  return LdrRelocateImageWithBias(BaseAddress, 0, LoaderName, Success, Conflict, Invalid);
450 }
ULONG NTAPI LdrRelocateImageWithBias(IN PVOID BaseAddress, IN LONGLONG AdditionalBias, IN PCCH LoaderName, IN ULONG Success, IN ULONG Conflict, IN ULONG Invalid)
Definition: image.c:454
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404

◆ LdrRelocateImageWithBias()

ULONG NTAPI LdrRelocateImageWithBias ( IN PVOID  BaseAddress,
IN LONGLONG  AdditionalBias,
IN PCCH  LoaderName,
IN ULONG  Success,
IN ULONG  Conflict,
IN ULONG  Invalid 
)

Definition at line 454 of file image.c.

Referenced by LdrRelocateImage().

461 {
462  PIMAGE_NT_HEADERS NtHeaders;
463  PIMAGE_DATA_DIRECTORY RelocationDDir;
464  PIMAGE_BASE_RELOCATION RelocationDir, RelocationEnd;
465  ULONG Count;
467  PUSHORT TypeOffset;
468  LONGLONG Delta;
469 
470  NtHeaders = RtlImageNtHeader(BaseAddress);
471 
472  if (NtHeaders == NULL)
473  return Invalid;
474 
476  {
477  return Conflict;
478  }
479 
480  RelocationDDir = &NtHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC];
481 
482  if (SWAPD(RelocationDDir->VirtualAddress) == 0 || SWAPD(RelocationDDir->Size) == 0)
483  {
484  return Success;
485  }
486 
487  Delta = (ULONG_PTR)BaseAddress - SWAPD(NtHeaders->OptionalHeader.ImageBase) + AdditionalBias;
488  RelocationDir = (PIMAGE_BASE_RELOCATION)((ULONG_PTR)BaseAddress + SWAPD(RelocationDDir->VirtualAddress));
489  RelocationEnd = (PIMAGE_BASE_RELOCATION)((ULONG_PTR)RelocationDir + SWAPD(RelocationDDir->Size));
490 
491  while (RelocationDir < RelocationEnd &&
492  SWAPW(RelocationDir->SizeOfBlock) > 0)
493  {
494  Count = (SWAPW(RelocationDir->SizeOfBlock) - sizeof(IMAGE_BASE_RELOCATION)) / sizeof(USHORT);
495  Address = (ULONG_PTR)RVA(BaseAddress, SWAPD(RelocationDir->VirtualAddress));
496  TypeOffset = (PUSHORT)(RelocationDir + 1);
497 
498  RelocationDir = LdrProcessRelocationBlockLongLong(Address,
499  Count,
500  TypeOffset,
501  Delta);
502 
503  if (RelocationDir == NULL)
504  {
505  DPRINT1("Error during call to LdrProcessRelocationBlockLongLong()!\n");
506  return Invalid;
507  }
508  }
509 
510  return Success;
511 }
PIMAGE_BASE_RELOCATION NTAPI LdrProcessRelocationBlockLongLong(IN ULONG_PTR Address, IN ULONG Count, IN PUSHORT TypeOffset, IN LONGLONG Delta)
Definition: image.c:372
struct _IMAGE_BASE_RELOCATION * PIMAGE_BASE_RELOCATION
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
#define SWAPW(x)
Definition: bytesex.h:8
#define SWAPD(x)
Definition: bytesex.h:7
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
struct _IMAGE_BASE_RELOCATION IMAGE_BASE_RELOCATION
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
uint32_t ULONG_PTR
Definition: typedefs.h:63
smooth NULL
Definition: ftsmooth.c:416
static WCHAR Address[46]
Definition: ping.c:68
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
int64_t LONGLONG
Definition: typedefs.h:66
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader(IN PVOID Base)
Definition: image.c:256
unsigned short USHORT
Definition: pedump.c:61
static ULONG Delta
Definition: xboxvideo.c:28
#define IMAGE_DIRECTORY_ENTRY_BASERELOC
Definition: pedump.c:264
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
#define IMAGE_FILE_RELOCS_STRIPPED
Definition: pedump.c:159
#define ULONG_PTR
Definition: config.h:101
#define RVA(m, b)
Definition: image.c:20
unsigned short * PUSHORT
Definition: retypes.h:2

◆ LdrVerifyMappedImageMatchesChecksum()

BOOLEAN NTAPI LdrVerifyMappedImageMatchesChecksum ( IN PVOID  BaseAddress,
IN SIZE_T  ImageSize,
IN ULONG  FileLength 
)

Definition at line 45 of file image.c.

49 {
50 #if 0
52  PUSHORT Ptr;
53  ULONG Sum;
54  ULONG CalcSum;
55  ULONG HeaderSum;
56  ULONG i;
57 
58  // HACK: Ignore calls with ImageSize=0. Should be fixed by new MM.
59  if (ImageSize == 0) return TRUE;
60 
61  /* Get NT header to check if it's an image at all */
62  Header = RtlImageNtHeader(BaseAddress);
63  if (!Header) return FALSE;
64 
65  /* Get checksum to match */
66  HeaderSum = Header->OptionalHeader.CheckSum;
67 
68  /* Zero checksum seems to be accepted */
69  if (HeaderSum == 0) return TRUE;
70 
71  /* Calculate the checksum */
72  Sum = 0;
73  Ptr = (PUSHORT) BaseAddress;
74  for (i = 0; i < ImageSize / sizeof (USHORT); i++)
75  {
76  Sum += (ULONG)*Ptr;
77  if (HIWORD(Sum) != 0)
78  {
79  Sum = LOWORD(Sum) + HIWORD(Sum);
80  }
81  Ptr++;
82  }
83 
84  if (ImageSize & 1)
85  {
86  Sum += (ULONG)*((PUCHAR)Ptr);
87  if (HIWORD(Sum) != 0)
88  {
89  Sum = LOWORD(Sum) + HIWORD(Sum);
90  }
91  }
92 
93  CalcSum = (USHORT)(LOWORD(Sum) + HIWORD(Sum));
94 
95  /* Subtract image checksum from calculated checksum. */
96  /* fix low word of checksum */
97  if (LOWORD(CalcSum) >= LOWORD(HeaderSum))
98  {
99  CalcSum -= LOWORD(HeaderSum);
100  }
101  else
102  {
103  CalcSum = ((LOWORD(CalcSum) - LOWORD(HeaderSum)) & 0xFFFF) - 1;
104  }
105 
106  /* Fix high word of checksum */
107  if (LOWORD(CalcSum) >= HIWORD(HeaderSum))
108  {
109  CalcSum -= HIWORD(HeaderSum);
110  }
111  else
112  {
113  CalcSum = ((LOWORD(CalcSum) - HIWORD(HeaderSum)) & 0xFFFF) - 1;
114  }
115 
116  /* Add file length */
117  CalcSum += ImageSize;
118 
119  if (CalcSum != HeaderSum)
120  DPRINT1("Image %p checksum mismatches! 0x%x != 0x%x, ImageSize %x, FileLen %x\n", BaseAddress, CalcSum, HeaderSum, ImageSize, FileLength);
121 
122  return (BOOLEAN)(CalcSum == HeaderSum);
123 #else
124  /*
125  * FIXME: Warning, this violates the PE standard and makes ReactOS drivers
126  * and other system code when normally on Windows they would not, since
127  * we do not write the checksum in them.
128  * Our compilers should be made to write out the checksum and this function
129  * should be enabled as to reject badly checksummed code.
130  */
131  return TRUE;
132 #endif
133 }
_Out_ PNDIS_HANDLE _Out_ PUINT FileLength
Definition: ndis.h:3209
#define TRUE
Definition: types.h:120
unsigned char * PUCHAR
Definition: retypes.h:3
_Must_inspect_result_ _In_ PFSRTL_PER_STREAM_CONTEXT Ptr
Definition: fsrtlfuncs.h:898
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
GLenum GLclampf GLint i
Definition: glfuncs.h:14
Definition: Header.h:8
unsigned char BOOLEAN
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader(IN PVOID Base)
Definition: image.c:256
unsigned short USHORT
Definition: pedump.c:61
#define DPRINT1
Definition: precomp.h:8
#define HIWORD(l)
Definition: typedefs.h:246
unsigned int ULONG
Definition: retypes.h:1
#define LOWORD(l)
Definition: pedump.c:82
unsigned short * PUSHORT
Definition: retypes.h:2

◆ RtlImageDirectoryEntryToData()

PVOID NTAPI RtlImageDirectoryEntryToData ( PVOID  BaseAddress,
BOOLEAN  MappedAsImage,
USHORT  Directory,
PULONG  Size 
)

Definition at line 273 of file image.c.

278 {
279  PIMAGE_NT_HEADERS NtHeader;
280  ULONG Va;
281 
282  /* Magic flag for non-mapped images. */
283  if ((ULONG_PTR)BaseAddress & 1)
284  {
285  BaseAddress = (PVOID)((ULONG_PTR)BaseAddress & ~1);
287  }
288 
289  NtHeader = RtlImageNtHeader(BaseAddress);
290  if (NtHeader == NULL)
291  return NULL;
292 
294  return NULL;
295 
297  if (Va == 0)
298  return NULL;
299 
301 
302  if (MappedAsImage || Va < SWAPD(NtHeader->OptionalHeader.SizeOfHeaders))
303  return (PVOID)((ULONG_PTR)BaseAddress + Va);
304 
305  /* Image mapped as ordinary file, we must find raw pointer */
306  return RtlImageRvaToVa(NtHeader, BaseAddress, Va, NULL);
307 }
IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]
Definition: ntddk_ex.h:178
#define SWAPD(x)
Definition: bytesex.h:7
PVOID NTAPI RtlImageRvaToVa(PIMAGE_NT_HEADERS NtHeader, PVOID BaseAddress, ULONG Rva, PIMAGE_SECTION_HEADER *SectionHeader)
Definition: image.c:342
IMAGE_OPTIONAL_HEADER32 OptionalHeader
Definition: ntddk_ex.h:184
uint32_t ULONG_PTR
Definition: typedefs.h:63
_In_ BOOLEAN MappedAsImage
Definition: rtlfuncs.h:3718
smooth NULL
Definition: ftsmooth.c:416
void * PVOID
Definition: retypes.h:9
PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader(IN PVOID Base)
Definition: image.c:256
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
unsigned int ULONG
Definition: retypes.h:1
base for all directory entries
Definition: entries.h:138
#define ULONG_PTR
Definition: config.h:101

◆ RtlImageNtHeader()

PIMAGE_NT_HEADERS NTAPI RtlImageNtHeader ( IN PVOID  Base)

Definition at line 256 of file image.c.

Referenced by GetTimestampForLoadedLibrary(), LdrRelocateImageWithBias(), LdrVerifyMappedImageMatchesChecksum(), and RtlImageDirectoryEntryToData().

257 {
258  PIMAGE_NT_HEADERS NtHeader;
259 
260  /* Call the new API */
261  RtlImageNtHeaderEx(RTL_IMAGE_NT_HEADER_EX_FLAG_NO_RANGE_CHECK,
262  Base,
263  0,
264  &NtHeader);
265  return NtHeader;
266 }
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2327
NTSTATUS NTAPI RtlImageNtHeaderEx(_In_ ULONG Flags, _In_ PVOID Base, _In_ ULONG64 Size, _Out_ PIMAGE_NT_HEADERS *OutHeaders)
Definition: image.c:141

◆ RtlImageNtHeaderEx()

NTSTATUS NTAPI RtlImageNtHeaderEx ( _In_ ULONG  Flags,
_In_ PVOID  Base,
_In_ ULONG64  Size,
_Out_ PIMAGE_NT_HEADERS OutHeaders 
)

Definition at line 141 of file image.c.

Referenced by BlImgFindSection(), ImgArchEfiStartBootApplication(), ImgpInitializeBootApplicationParameters(), ImgpLoadPEImage(), and RtlImageNtHeader().

146 {
147  PIMAGE_NT_HEADERS NtHeaders;
148  PIMAGE_DOS_HEADER DosHeader;
149  BOOLEAN WantsRangeCheck;
150  ULONG NtHeaderOffset;
151 
152  /* You must want NT Headers, no? */
153  if (OutHeaders == NULL)
154  {
155  DPRINT1("OutHeaders is NULL\n");
157  }
158 
159  /* Assume failure */
160  *OutHeaders = NULL;
161 
162  /* Validate Flags */
163  if (Flags & ~RTL_IMAGE_NT_HEADER_EX_FLAG_NO_RANGE_CHECK)
164  {
165  DPRINT1("Invalid flags: 0x%lx\n", Flags);
167  }
168 
169  /* Validate base */
170  if ((Base == NULL) || (Base == (PVOID)-1))
171  {
172  DPRINT1("Invalid base address: %p\n", Base);
174  }
175 
176  /* Check if the caller wants range checks */
177  WantsRangeCheck = !(Flags & RTL_IMAGE_NT_HEADER_EX_FLAG_NO_RANGE_CHECK);
178  if (WantsRangeCheck)
179  {
180  /* Make sure the image size is at least big enough for the DOS header */
181  if (Size < sizeof(IMAGE_DOS_HEADER))
182  {
183  DPRINT1("Size too small\n");
185  }
186  }
187 
188  /* Check if the DOS Signature matches */
189  DosHeader = Base;
190  if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
191  {
192  /* Not a valid COFF */
193  DPRINT1("Invalid image DOS signature!\n");
195  }
196 
197  /* Get the offset to the NT headers (and copy from LONG to ULONG) */
198  NtHeaderOffset = DosHeader->e_lfanew;
199 
200  /* The offset must not be larger than 256MB, as a hard-coded check.
201  In Windows this check is only done in user mode, not in kernel mode,
202  but it shouldn't harm to have it anyway. Note that without this check,
203  other overflow checks would become necessary! */
204  if (NtHeaderOffset >= (256 * 1024 * 1024))
205  {
206  /* Fail */
207  DPRINT1("NT headers offset is larger than 256MB!\n");
209  }
210 
211  /* Check if the caller wants validation */
212  if (WantsRangeCheck)
213  {
214  /* Make sure the file header fits into the size */
215  if ((NtHeaderOffset +
217  {
218  /* Fail */
219  DPRINT1("NT headers beyond image size!\n");
221  }
222  }
223 
224  /* Now get a pointer to the NT Headers */
225  NtHeaders = (PIMAGE_NT_HEADERS)((ULONG_PTR)Base + NtHeaderOffset);
226 
227  /* Check if the mapping is in user space */
228  if (Base <= MmHighestUserAddress)
229  {
230  /* Make sure we don't overflow into kernel space */
231  if ((PVOID)(NtHeaders + 1) > MmHighestUserAddress)
232  {
233  DPRINT1("Image overflows from user space into kernel space!\n");
235  }
236  }
237 
238  /* Verify the PE Signature */
239  if (NtHeaders->Signature != IMAGE_NT_SIGNATURE)
240  {
241  /* Fail */
242  DPRINT1("Invalid image NT signature!\n");
244  }
245 
246  /* Now return success and the NT header */
247  *OutHeaders = NtHeaders;
248  return STATUS_SUCCESS;
249 }
#define STATUS_INVALID_PARAMETER
Definition: udferr_usr.h:135
_In_opt_ ULONG Base
Definition: rtlfuncs.h:2327
PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS
Definition: ntddk_ex.h:187
uint32_t ULONG_PTR
Definition: typedefs.h:63
#define IMAGE_DOS_SIGNATURE
Definition: pedump.c:89
_Must_inspect_result_ _In_ ULONG Flags
Definition: wsk.h:170
unsigned char BOOLEAN
smooth NULL
Definition: ftsmooth.c:416
#define IMAGE_NT_SIGNATURE
Definition: pedump.c:93
#define STATUS_INVALID_IMAGE_FORMAT
Definition: ntstatus.h:345
#define RTL_SIZEOF_THROUGH_FIELD(type, field)
Definition: ntbasedef.h:679
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
PVOID MmHighestUserAddress
Definition: rtlcompat.c:29
#define DPRINT1
Definition: precomp.h:8
unsigned int ULONG
Definition: retypes.h:1
return STATUS_SUCCESS
Definition: btrfs.c:2710

◆ RtlImageRvaToSection()

PIMAGE_SECTION_HEADER NTAPI RtlImageRvaToSection ( PIMAGE_NT_HEADERS  NtHeader,
PVOID  BaseAddress,
ULONG  Rva 
)

Definition at line 314 of file image.c.

Referenced by RtlImageRvaToVa().

318 {
319  PIMAGE_SECTION_HEADER Section;
320  ULONG Va;
321  ULONG Count;
322 
323  Count = SWAPW(NtHeader->FileHeader.NumberOfSections);
324  Section = IMAGE_FIRST_SECTION(NtHeader);
325 
326  while (Count--)
327  {
328  Va = SWAPD(Section->VirtualAddress);
329  if ((Va <= Rva) && (Rva < Va + SWAPD(Section->SizeOfRawData)))
330  return Section;
331  Section++;
332  }
333 
334  return NULL;
335 }
#define SWAPW(x)
Definition: bytesex.h:8
#define SWAPD(x)
Definition: bytesex.h:7
_Inout_ __drv_aliasesMem PSLIST_ENTRY _Inout_ PSLIST_ENTRY _In_ ULONG Count
Definition: exfuncs.h:1015
#define IMAGE_FIRST_SECTION(NtHeader)
Definition: ntimage.h:427
smooth NULL
Definition: ftsmooth.c:416
IMAGE_FILE_HEADER FileHeader
Definition: ntddk_ex.h:183
unsigned int ULONG
Definition: retypes.h:1

◆ RtlImageRvaToVa()

PVOID NTAPI RtlImageRvaToVa ( PIMAGE_NT_HEADERS  NtHeader,
PVOID  BaseAddress,
ULONG  Rva,
PIMAGE_SECTION_HEADER SectionHeader 
)

Definition at line 342 of file image.c.

Referenced by RtlImageDirectoryEntryToData().

347 {
348  PIMAGE_SECTION_HEADER Section = NULL;
349 
350  if (SectionHeader)
351  Section = *SectionHeader;
352 
353  if ((Section == NULL) ||
354  (Rva < SWAPD(Section->VirtualAddress)) ||
355  (Rva >= SWAPD(Section->VirtualAddress) + SWAPD(Section->SizeOfRawData)))
356  {
357  Section = RtlImageRvaToSection(NtHeader, BaseAddress, Rva);
358  if (Section == NULL)
359  return NULL;
360 
361  if (SectionHeader)
362  *SectionHeader = Section;
363  }
364 
365  return (PVOID)((ULONG_PTR)BaseAddress + Rva +
366  (ULONG_PTR)SWAPD(Section->PointerToRawData) -
367  (ULONG_PTR)SWAPD(Section->VirtualAddress));
368 }
#define SWAPD(x)
Definition: bytesex.h:7
DWORD PointerToRawData
Definition: pedump.c:290
uint32_t ULONG_PTR
Definition: typedefs.h:63
smooth NULL
Definition: ftsmooth.c:416
PIMAGE_SECTION_HEADER NTAPI RtlImageRvaToSection(PIMAGE_NT_HEADERS NtHeader, PVOID BaseAddress, ULONG Rva)
Definition: image.c:314
#define ULONG_PTR
Definition: config.h:101