3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: lib/ntdll/rtl/image.c
6 * PURPOSE: Image handling functions
7 * PROGRAMMER: Eric Kohl
12 #include <ddk/ntddk.h>
13 #include <ntdll/rtl.h>
16 #include <ntdll/ntdll.h>
18 /* FUNCTIONS ****************************************************************/
20 PIMAGE_NT_HEADERS STDCALL
21 RtlImageNtHeader (IN PVOID BaseAddress)
23 PIMAGE_NT_HEADERS NtHeader;
24 PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)BaseAddress;
26 if (DosHeader && DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
28 DPRINT1("DosHeader->e_magic %x\n", DosHeader->e_magic);
29 DPRINT1("NtHeader %x\n", (BaseAddress + DosHeader->e_lfanew));
32 // if (DosHeader && DosHeader->e_magic == IMAGE_DOS_SIGNATURE)
34 NtHeader = (PIMAGE_NT_HEADERS)(BaseAddress + DosHeader->e_lfanew);
35 if (NtHeader->Signature == IMAGE_NT_SIGNATURE)
44 RtlImageDirectoryEntryToData (
51 PIMAGE_NT_HEADERS NtHeader;
52 PIMAGE_SECTION_HEADER SectionHeader;
56 NtHeader = RtlImageNtHeader (BaseAddress);
60 if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes)
63 Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress;
68 *Size = NtHeader->OptionalHeader.DataDirectory[Directory].Size;
71 return (PVOID)(BaseAddress + Va);
73 /* image mapped as ordinary file, we must find raw pointer */
74 SectionHeader = (PIMAGE_SECTION_HEADER)(NtHeader + 1);
75 Count = NtHeader->FileHeader.NumberOfSections;
78 if (SectionHeader->VirtualAddress == Va)
79 return (PVOID)(BaseAddress + SectionHeader->PointerToRawData);
89 RtlImageRvaToSection (
90 PIMAGE_NT_HEADERS NtHeader,
95 PIMAGE_SECTION_HEADER Section;
99 Count = NtHeader->FileHeader.NumberOfSections;
100 Section = (PIMAGE_SECTION_HEADER)((ULONG)&NtHeader->OptionalHeader +
101 NtHeader->FileHeader.SizeOfOptionalHeader);
104 Va = Section->VirtualAddress;
106 (Rva < Va + Section->SizeOfRawData))
117 PIMAGE_NT_HEADERS NtHeader,
120 PIMAGE_SECTION_HEADER *SectionHeader
123 PIMAGE_SECTION_HEADER Section = NULL;
126 Section = *SectionHeader;
128 if (Section == NULL ||
129 Rva < Section->VirtualAddress ||
130 Rva >= Section->VirtualAddress + Section->SizeOfRawData)
132 Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva);
137 *SectionHeader = Section;
140 return (ULONG)(BaseAddress +
142 Section->PointerToRawData -
143 Section->VirtualAddress);