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 ****************************************************************/
23 PIMAGE_NT_HEADERS STDCALL
24 RtlImageNtHeader (IN PVOID BaseAddress)
26 PIMAGE_NT_HEADERS NtHeader;
27 PIMAGE_DOS_HEADER DosHeader = (PIMAGE_DOS_HEADER)BaseAddress;
29 if (DosHeader && DosHeader->e_magic != IMAGE_DOS_SIGNATURE)
31 DPRINT1("DosHeader->e_magic %x\n", DosHeader->e_magic);
32 DPRINT1("NtHeader %x\n", (BaseAddress + DosHeader->e_lfanew));
35 if (DosHeader && DosHeader->e_magic == IMAGE_DOS_SIGNATURE)
37 NtHeader = (PIMAGE_NT_HEADERS)(BaseAddress + DosHeader->e_lfanew);
38 if (NtHeader->Signature == IMAGE_NT_SIGNATURE)
51 RtlImageDirectoryEntryToData (
58 PIMAGE_NT_HEADERS NtHeader;
59 PIMAGE_SECTION_HEADER SectionHeader;
63 NtHeader = RtlImageNtHeader (BaseAddress);
67 if (Directory >= NtHeader->OptionalHeader.NumberOfRvaAndSizes)
70 Va = NtHeader->OptionalHeader.DataDirectory[Directory].VirtualAddress;
75 *Size = NtHeader->OptionalHeader.DataDirectory[Directory].Size;
78 return (PVOID)(BaseAddress + Va);
80 /* image mapped as ordinary file, we must find raw pointer */
81 SectionHeader = (PIMAGE_SECTION_HEADER)(NtHeader + 1);
82 Count = NtHeader->FileHeader.NumberOfSections;
85 if (SectionHeader->VirtualAddress == Va)
86 return (PVOID)(BaseAddress + SectionHeader->PointerToRawData);
99 RtlImageRvaToSection (
100 PIMAGE_NT_HEADERS NtHeader,
105 PIMAGE_SECTION_HEADER Section;
109 Count = NtHeader->FileHeader.NumberOfSections;
110 Section = (PIMAGE_SECTION_HEADER)((ULONG)&NtHeader->OptionalHeader +
111 NtHeader->FileHeader.SizeOfOptionalHeader);
114 Va = Section->VirtualAddress;
116 (Rva < Va + Section->SizeOfRawData))
130 PIMAGE_NT_HEADERS NtHeader,
133 PIMAGE_SECTION_HEADER *SectionHeader
136 PIMAGE_SECTION_HEADER Section = NULL;
139 Section = *SectionHeader;
141 if (Section == NULL ||
142 Rva < Section->VirtualAddress ||
143 Rva >= Section->VirtualAddress + Section->SizeOfRawData)
145 Section = RtlImageRvaToSection (NtHeader, BaseAddress, Rva);
150 *SectionHeader = Section;
153 return (ULONG)(BaseAddress +
155 Section->PointerToRawData -
156 Section->VirtualAddress);