1 /* COPYRIGHT: See COPYING in the top level directory
2 * PROJECT: ReactOS system libraries
3 * FILE: lib/rtl/image.c
4 * PURPOSE: Image handling functions
5 * PROGRAMMER: Eric Kohl
8 /* INCLUDES *****************************************************************/
15 /* FUNCTIONS *****************************************************************/
20 PIMAGE_NT_HEADERS NTAPI
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 0x%lx\n", ((ULONG_PTR
)BaseAddress
+ DosHeader
->e_lfanew
));
32 if (DosHeader
&& DosHeader
->e_magic
== IMAGE_DOS_SIGNATURE
)
34 NtHeader
= (PIMAGE_NT_HEADERS
)((ULONG_PTR
)BaseAddress
+ DosHeader
->e_lfanew
);
35 if (NtHeader
->Signature
== IMAGE_NT_SIGNATURE
)
48 RtlImageDirectoryEntryToData (
50 BOOLEAN bMappedAsImage
,
55 PIMAGE_NT_HEADERS NtHeader
;
58 /* Magic flag for non-mapped images. */
59 if ((ULONG_PTR
)BaseAddress
& 1)
61 BaseAddress
= (PVOID
)((ULONG_PTR
)BaseAddress
& ~1);
62 bMappedAsImage
= FALSE
;
66 NtHeader
= RtlImageNtHeader (BaseAddress
);
70 if (Directory
>= NtHeader
->OptionalHeader
.NumberOfRvaAndSizes
)
73 Va
= NtHeader
->OptionalHeader
.DataDirectory
[Directory
].VirtualAddress
;
77 *Size
= NtHeader
->OptionalHeader
.DataDirectory
[Directory
].Size
;
79 if (bMappedAsImage
|| Va
< NtHeader
->OptionalHeader
.SizeOfHeaders
)
80 return (PVOID
)((ULONG_PTR
)BaseAddress
+ Va
);
82 /* image mapped as ordinary file, we must find raw pointer */
83 return (PVOID
)RtlImageRvaToVa (NtHeader
, BaseAddress
, Va
, NULL
);
92 RtlImageRvaToSection (
93 PIMAGE_NT_HEADERS NtHeader
,
98 PIMAGE_SECTION_HEADER Section
;
102 Count
= NtHeader
->FileHeader
.NumberOfSections
;
103 Section
= (PIMAGE_SECTION_HEADER
)((ULONG
)&NtHeader
->OptionalHeader
+
104 NtHeader
->FileHeader
.SizeOfOptionalHeader
);
107 Va
= Section
->VirtualAddress
;
109 (Rva
< Va
+ Section
->SizeOfRawData
))
123 PIMAGE_NT_HEADERS NtHeader
,
126 PIMAGE_SECTION_HEADER
*SectionHeader
129 PIMAGE_SECTION_HEADER Section
= NULL
;
132 Section
= *SectionHeader
;
134 if (Section
== NULL
||
135 Rva
< Section
->VirtualAddress
||
136 Rva
>= Section
->VirtualAddress
+ Section
->SizeOfRawData
)
138 Section
= RtlImageRvaToSection (NtHeader
, BaseAddress
, Rva
);
143 *SectionHeader
= Section
;
146 return (ULONG
)((ULONG_PTR
)BaseAddress
+
148 Section
->PointerToRawData
-
149 (ULONG_PTR
)Section
->VirtualAddress
);