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
;
61 NtHeader
= RtlImageNtHeader (BaseAddress
);
65 if (Directory
>= NtHeader
->OptionalHeader
.NumberOfRvaAndSizes
)
68 Va
= NtHeader
->OptionalHeader
.DataDirectory
[Directory
].VirtualAddress
;
73 *Size
= NtHeader
->OptionalHeader
.DataDirectory
[Directory
].Size
;
76 return (PVOID
)(BaseAddress
+ Va
);
78 /* image mapped as ordinary file, we must find raw pointer */
79 return (PVOID
)RtlImageRvaToVa (NtHeader
, BaseAddress
, Va
, NULL
);
88 RtlImageRvaToSection (
89 PIMAGE_NT_HEADERS NtHeader
,
94 PIMAGE_SECTION_HEADER Section
;
98 Count
= NtHeader
->FileHeader
.NumberOfSections
;
99 Section
= (PIMAGE_SECTION_HEADER
)((ULONG
)&NtHeader
->OptionalHeader
+
100 NtHeader
->FileHeader
.SizeOfOptionalHeader
);
103 Va
= Section
->VirtualAddress
;
105 (Rva
< Va
+ Section
->SizeOfRawData
))
119 PIMAGE_NT_HEADERS NtHeader
,
122 PIMAGE_SECTION_HEADER
*SectionHeader
125 PIMAGE_SECTION_HEADER Section
= NULL
;
128 Section
= *SectionHeader
;
130 if (Section
== NULL
||
131 Rva
< Section
->VirtualAddress
||
132 Rva
>= Section
->VirtualAddress
+ Section
->SizeOfRawData
)
134 Section
= RtlImageRvaToSection (NtHeader
, BaseAddress
, Rva
);
139 *SectionHeader
= Section
;
142 return (ULONG
)(BaseAddress
+
144 Section
->PointerToRawData
-
145 Section
->VirtualAddress
);