Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

403 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1999-2001 Microsoft Corporation
  3. Module Name:
  4. imagedir.c
  5. Abstract:
  6. For backwards compatability on Win9x platforms, the imagehlp ImageNtHeader
  7. etc. functions have been physically compiled into minidump.dll.
  8. Author:
  9. Matthew D Hendel (math) 28-April-1999
  10. Revision History:
  11. --*/
  12. #include "pch.h"
  13. PIMAGE_NT_HEADERS
  14. xxxImageNtHeader (
  15. IN PVOID Base
  16. )
  17. /*++
  18. Routine Description:
  19. This function returns the address of the NT Header.
  20. Arguments:
  21. Base - Supplies the base of the image.
  22. Return Value:
  23. Returns the address of the NT Header.
  24. --*/
  25. {
  26. PIMAGE_NT_HEADERS NtHeaders = NULL;
  27. if (Base != NULL && Base != (PVOID)-1) {
  28. if (((PIMAGE_DOS_HEADER)Base)->e_magic == IMAGE_DOS_SIGNATURE) {
  29. NtHeaders = (PIMAGE_NT_HEADERS)((PCHAR)Base + ((PIMAGE_DOS_HEADER)Base)->e_lfanew);
  30. if (NtHeaders->Signature != IMAGE_NT_SIGNATURE) {
  31. NtHeaders = NULL;
  32. }
  33. }
  34. }
  35. return NtHeaders;
  36. }
  37. PIMAGE_SECTION_HEADER
  38. xxxSectionTableFromVirtualAddress (
  39. IN PIMAGE_NT_HEADERS NtHeaders,
  40. IN PVOID Base,
  41. IN ULONG Address
  42. )
  43. /*++
  44. Routine Description:
  45. This function locates a VirtualAddress within the image header
  46. of a file that is mapped as a file and returns a pointer to the
  47. section table entry for that virtual address
  48. Arguments:
  49. NtHeaders - Supplies the pointer to the image or data file.
  50. Base - Supplies the base of the image or data file.
  51. Address - Supplies the virtual address to locate.
  52. Return Value:
  53. NULL - The file does not contain data for the specified directory entry.
  54. NON-NULL - Returns the pointer of the section entry containing the data.
  55. --*/
  56. {
  57. ULONG i;
  58. PIMAGE_SECTION_HEADER NtSection;
  59. NtSection = IMAGE_FIRST_SECTION( NtHeaders );
  60. for (i=0; i<NtHeaders->FileHeader.NumberOfSections; i++) {
  61. if ((ULONG)Address >= NtSection->VirtualAddress &&
  62. (ULONG)Address < NtSection->VirtualAddress + NtSection->SizeOfRawData
  63. ) {
  64. return NtSection;
  65. }
  66. ++NtSection;
  67. }
  68. return NULL;
  69. }
  70. PVOID
  71. xxxAddressInSectionTable (
  72. IN PIMAGE_NT_HEADERS NtHeaders,
  73. IN PVOID Base,
  74. IN ULONG Address
  75. )
  76. /*++
  77. Routine Description:
  78. This function locates a VirtualAddress within the image header
  79. of a file that is mapped as a file and returns the seek address
  80. of the data the Directory describes.
  81. Arguments:
  82. NtHeaders - Supplies the pointer to the image or data file.
  83. Base - Supplies the base of the image or data file.
  84. Address - Supplies the virtual address to locate.
  85. Return Value:
  86. NULL - The file does not contain data for the specified directory entry.
  87. NON-NULL - Returns the address of the raw data the directory describes.
  88. --*/
  89. {
  90. PIMAGE_SECTION_HEADER NtSection;
  91. NtSection = xxxSectionTableFromVirtualAddress( NtHeaders,
  92. Base,
  93. Address
  94. );
  95. if (NtSection != NULL) {
  96. return( ((PCHAR)Base + ((ULONG_PTR)Address - NtSection->VirtualAddress) + NtSection->PointerToRawData) );
  97. }
  98. else {
  99. return( NULL );
  100. }
  101. }
  102. PVOID
  103. xxxpImageDirectoryEntryToData32 (
  104. IN PVOID Base,
  105. IN BOOLEAN MappedAsImage,
  106. IN USHORT DirectoryEntry,
  107. OUT PULONG Size,
  108. PIMAGE_NT_HEADERS32 NtHeaders
  109. )
  110. {
  111. ULONG DirectoryAddress;
  112. if (DirectoryEntry >= NtHeaders->OptionalHeader.NumberOfRvaAndSizes) {
  113. return( NULL );
  114. }
  115. if (!(DirectoryAddress = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].VirtualAddress)) {
  116. return( NULL );
  117. }
  118. *Size = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].Size;
  119. if (MappedAsImage || DirectoryAddress < NtHeaders->OptionalHeader.SizeOfHeaders) {
  120. return( (PVOID)((PCHAR)Base + DirectoryAddress) );
  121. }
  122. return( xxxAddressInSectionTable((PIMAGE_NT_HEADERS)NtHeaders, Base, DirectoryAddress ));
  123. }
  124. PVOID
  125. xxxpImageDirectoryEntryToData64 (
  126. IN PVOID Base,
  127. IN BOOLEAN MappedAsImage,
  128. IN USHORT DirectoryEntry,
  129. OUT PULONG Size,
  130. PIMAGE_NT_HEADERS64 NtHeaders
  131. )
  132. {
  133. ULONG DirectoryAddress;
  134. if (DirectoryEntry >= NtHeaders->OptionalHeader.NumberOfRvaAndSizes) {
  135. return( NULL );
  136. }
  137. if (!(DirectoryAddress = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].VirtualAddress)) {
  138. return( NULL );
  139. }
  140. *Size = NtHeaders->OptionalHeader.DataDirectory[ DirectoryEntry ].Size;
  141. if (MappedAsImage || DirectoryAddress < NtHeaders->OptionalHeader.SizeOfHeaders) {
  142. return( (PVOID)((PCHAR)Base + DirectoryAddress) );
  143. }
  144. return( xxxAddressInSectionTable((PIMAGE_NT_HEADERS)NtHeaders, Base, DirectoryAddress ));
  145. }
  146. PVOID
  147. xxxImageDirectoryEntryToData (
  148. IN PVOID Base,
  149. IN BOOLEAN MappedAsImage,
  150. IN USHORT DirectoryEntry,
  151. OUT PULONG Size
  152. )
  153. /*++
  154. Routine Description:
  155. This function locates a Directory Entry within the image header
  156. and returns either the virtual address or seek address of the
  157. data the Directory describes.
  158. Arguments:
  159. Base - Supplies the base of the image or data file.
  160. MappedAsImage - FALSE if the file is mapped as a data file.
  161. - TRUE if the file is mapped as an image.
  162. DirectoryEntry - Supplies the directory entry to locate.
  163. Size - Return the size of the directory.
  164. Return Value:
  165. NULL - The file does not contain data for the specified directory entry.
  166. NON-NULL - Returns the address of the raw data the directory describes.
  167. --*/
  168. {
  169. PIMAGE_NT_HEADERS NtHeaders;
  170. if ((ULONG_PTR)Base & 0x00000001) {
  171. Base = (PVOID)((ULONG_PTR)Base & ~0x00000001);
  172. MappedAsImage = FALSE;
  173. }
  174. NtHeaders = xxxImageNtHeader(Base);
  175. if (!NtHeaders)
  176. return NULL;
  177. if (NtHeaders->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR32_MAGIC) {
  178. return (xxxpImageDirectoryEntryToData32(Base,
  179. MappedAsImage,
  180. DirectoryEntry,
  181. Size,
  182. (PIMAGE_NT_HEADERS32)NtHeaders));
  183. } else if (NtHeaders->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
  184. return (xxxpImageDirectoryEntryToData64(Base,
  185. MappedAsImage,
  186. DirectoryEntry,
  187. Size,
  188. (PIMAGE_NT_HEADERS64)NtHeaders));
  189. } else {
  190. return (NULL);
  191. }
  192. }
  193. #if 0
  194. #if !defined(NTOS_KERNEL_RUNTIME) && !defined(BLDR_KERNEL_RUNTIME)
  195. PIMAGE_SECTION_HEADER
  196. RtlImageRvaToSection(
  197. IN PIMAGE_NT_HEADERS NtHeaders,
  198. IN PVOID Base,
  199. IN ULONG Rva
  200. )
  201. /*++
  202. Routine Description:
  203. This function locates an RVA within the image header of a file
  204. that is mapped as a file and returns a pointer to the section
  205. table entry for that virtual address
  206. Arguments:
  207. NtHeaders - Supplies the pointer to the image or data file.
  208. Base - Supplies the base of the image or data file. The image
  209. was mapped as a data file.
  210. Rva - Supplies the relative virtual address (RVA) to locate.
  211. Return Value:
  212. NULL - The RVA was not found within any of the sections of the image.
  213. NON-NULL - Returns the pointer to the image section that contains
  214. the RVA
  215. --*/
  216. {
  217. ULONG i;
  218. PIMAGE_SECTION_HEADER NtSection;
  219. NtSection = IMAGE_FIRST_SECTION( NtHeaders );
  220. for (i=0; i<NtHeaders->FileHeader.NumberOfSections; i++) {
  221. if (Rva >= NtSection->VirtualAddress &&
  222. Rva < NtSection->VirtualAddress + NtSection->SizeOfRawData
  223. ) {
  224. return NtSection;
  225. }
  226. ++NtSection;
  227. }
  228. return NULL;
  229. }
  230. PVOID
  231. RtlImageRvaToVa(
  232. IN PIMAGE_NT_HEADERS NtHeaders,
  233. IN PVOID Base,
  234. IN ULONG Rva,
  235. IN OUT PIMAGE_SECTION_HEADER *LastRvaSection OPTIONAL
  236. )
  237. /*++
  238. Routine Description:
  239. This function locates an RVA within the image header of a file that
  240. is mapped as a file and returns the virtual addrees of the
  241. corresponding byte in the file.
  242. Arguments:
  243. NtHeaders - Supplies the pointer to the image or data file.
  244. Base - Supplies the base of the image or data file. The image
  245. was mapped as a data file.
  246. Rva - Supplies the relative virtual address (RVA) to locate.
  247. LastRvaSection - Optional parameter that if specified, points
  248. to a variable that contains the last section value used for
  249. the specified image to translate and RVA to a VA.
  250. Return Value:
  251. NULL - The file does not contain the specified RVA
  252. NON-NULL - Returns the virtual addrees in the mapped file.
  253. --*/
  254. {
  255. PIMAGE_SECTION_HEADER NtSection;
  256. if (!ARGUMENT_PRESENT( LastRvaSection ) ||
  257. (NtSection = *LastRvaSection) == NULL ||
  258. Rva < NtSection->VirtualAddress ||
  259. Rva >= NtSection->VirtualAddress + NtSection->SizeOfRawData
  260. ) {
  261. NtSection = RtlImageRvaToSection( NtHeaders,
  262. Base,
  263. Rva
  264. );
  265. }
  266. if (NtSection != NULL) {
  267. if (LastRvaSection != NULL) {
  268. *LastRvaSection = NtSection;
  269. }
  270. return (PVOID)((PCHAR)Base +
  271. (Rva - NtSection->VirtualAddress) +
  272. NtSection->PointerToRawData
  273. );
  274. }
  275. else {
  276. return NULL;
  277. }
  278. }
  279. #endif
  280. #endif