pe.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*++
  2. Copyright (c) 2012 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. pe.h
  5. Abstract:
  6. This header contains definitions for PE images.
  7. Author:
  8. Evan Green 13-Oct-2012
  9. --*/
  10. //
  11. // ---------------------------------------------------------------- Definitions
  12. //
  13. //
  14. // This macro returns the image alignment, in bytes, given the image section
  15. // characteristics.
  16. //
  17. #define PE_SECTION_ALIGNMENT(_SectionCharacteristics) \
  18. (1 << ((((_SectionCharacteristics) & PE_SECTION_ALIGNMENT_MASK) >> \
  19. PE_SECTION_ALIGNMENT_SHIFT) - 1))
  20. //
  21. // PE Image definitions.
  22. //
  23. #define PE_MAX_LIBRARY_NAME 100
  24. #define PE_MAX_FUNCTION_NAME 256
  25. #define IMAGE_SIZEOF_SHORT_NAME 8
  26. #define IMAGE_DOS_SIGNATURE 0x5A4D
  27. #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
  28. #define IMAGE_FILE_EXECUTABLE_IMAGE 2
  29. #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
  30. #define IMAGE_SCN_MEM_DISCARDABLE 0x2000000
  31. #define IMAGE_SCN_CNT_CODE 0x00000020
  32. #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
  33. #define IMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
  34. #define PE_IMPORT_BY_ORDINAL 0x80000000
  35. #define COFF_SYMBOL_NAME_LENGTH 8
  36. //
  37. // Machine type definitions.
  38. //
  39. #define IMAGE_FILE_MACHINE_I386 0x14C
  40. #define IMAGE_FILE_MACHINE_AMD64 0x8664
  41. #define IMAGE_FILE_MACHINE_ARM 0x1C0
  42. #define IMAGE_FILE_MACHINE_ARMT 0x1C2
  43. //
  44. // Data directory definitions.
  45. //
  46. #define PE_EXPORT_DIRECTORY 0
  47. #define PE_IMPORT_DIRECTORY 1
  48. #define PE_RESOURCE_DIRECTORY 2
  49. #define PE_EXCEPTION_DIRECTORY 3
  50. #define PE_SECURITY_DIRECTORY 4
  51. #define PE_RELOCATION_DIRECTORY 5
  52. #define PE_DEBUG_DIRECTORY 6
  53. #define PE_DESCRIPTION_DIRECTORY 7
  54. #define PE_SPECIAL_DIRECTORY 8
  55. #define PE_THREAD_LOCAL_STORAGE_DIRECTORY 9
  56. #define PE_LOAD_CONFIGURATION_DIRECTORY 10
  57. #define PE_BOUND_IMPORT_DIRECTORY 11
  58. #define PE_IMPORT_ADDRESS_TABLE_DIRECTORY 12
  59. #define PE_DELAY_IMPORT_TABLE 13
  60. #define PE_CLR_RUNTIME_DIRECTORY 14
  61. #define PE_RESERVED_DIRECTORY 15
  62. //
  63. // Relocation definitions.
  64. //
  65. #define PE_RELOCATION_OFFSET_MASK 0x0FFF
  66. #define PE_RELOCATION_TYPE_SHIFT 12
  67. //
  68. // Section Definitions
  69. //
  70. #define PE_SECTION_ALIGNMENT_MASK 0x00F00000
  71. #define PE_SECTION_ALIGNMENT_SHIFT 20
  72. //
  73. // ------------------------------------------------------ Data Type Definitions
  74. //
  75. //
  76. // PE Image header definitions.
  77. //
  78. typedef struct _IMAGE_SECTION_HEADER {
  79. BYTE Name[IMAGE_SIZEOF_SHORT_NAME];
  80. union {
  81. DWORD PhysicalAddress;
  82. DWORD VirtualSize;
  83. } Misc;
  84. DWORD VirtualAddress;
  85. DWORD SizeOfRawData;
  86. DWORD PointerToRawData;
  87. DWORD PointerToRelocations;
  88. DWORD PointerToLinenumbers;
  89. WORD NumberOfRelocations;
  90. WORD NumberOfLinenumbers;
  91. DWORD Characteristics;
  92. } PACKED IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
  93. typedef struct _IMAGE_FILE_HEADER {
  94. WORD Machine;
  95. WORD NumberOfSections;
  96. DWORD TimeDateStamp;
  97. DWORD PointerToSymbolTable;
  98. DWORD NumberOfSymbols;
  99. WORD SizeOfOptionalHeader;
  100. WORD Characteristics;
  101. } PACKED IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
  102. typedef struct _IMAGE_DATA_DIRECTORY {
  103. DWORD VirtualAddress;
  104. DWORD Size;
  105. } PACKED IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
  106. typedef struct _IMAGE_OPTIONAL_HEADER {
  107. WORD Magic;
  108. BYTE MajorLinkerVersion;
  109. BYTE MinorLinkerVersion;
  110. DWORD SizeOfCode;
  111. DWORD SizeOfInitializedData;
  112. DWORD SizeOfUninitializedData;
  113. DWORD AddressOfEntryPoint;
  114. DWORD BaseOfCode;
  115. DWORD BaseOfData;
  116. DWORD ImageBase;
  117. DWORD SectionAlignment;
  118. DWORD FileAlignment;
  119. WORD MajorOperatingSystemVersion;
  120. WORD MinorOperatingSystemVersion;
  121. WORD MajorImageVersion;
  122. WORD MinorImageVersion;
  123. WORD MajorSubsystemVersion;
  124. WORD MinorSubsystemVersion;
  125. DWORD Win32VersionValue;
  126. DWORD SizeOfImage;
  127. DWORD SizeOfHeaders;
  128. DWORD CheckSum;
  129. WORD Subsystem;
  130. WORD DllCharacteristics;
  131. DWORD SizeOfStackReserve;
  132. DWORD SizeOfStackCommit;
  133. DWORD SizeOfHeapReserve;
  134. DWORD SizeOfHeapCommit;
  135. DWORD LoaderFlags;
  136. DWORD NumberOfRvaAndSizes;
  137. IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
  138. } PACKED IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
  139. typedef IMAGE_OPTIONAL_HEADER32 IMAGE_OPTIONAL_HEADER;
  140. typedef PIMAGE_OPTIONAL_HEADER32 PIMAGE_OPTIONAL_HEADER;
  141. typedef struct _IMAGE_NT_HEADERS32 {
  142. DWORD Signature;
  143. IMAGE_FILE_HEADER FileHeader;
  144. IMAGE_OPTIONAL_HEADER OptionalHeader;
  145. } PACKED IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
  146. typedef IMAGE_NT_HEADERS32 IMAGE_NT_HEADERS;
  147. typedef PIMAGE_NT_HEADERS32 PIMAGE_NT_HEADERS;
  148. typedef struct _IMAGE_DOS_HEADER {
  149. WORD e_magic;
  150. WORD e_cblp;
  151. WORD e_cp;
  152. WORD e_crlc;
  153. WORD e_cparhdr;
  154. WORD e_minalloc;
  155. WORD e_maxalloc;
  156. WORD e_ss;
  157. WORD e_sp;
  158. WORD e_csum;
  159. WORD e_ip;
  160. WORD e_cs;
  161. WORD e_lfarlc;
  162. WORD e_ovno;
  163. WORD e_res[4];
  164. WORD e_oemid;
  165. WORD e_oeminfo;
  166. WORD e_res2[10];
  167. LONG e_lfanew;
  168. } PACKED IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER;
  169. typedef struct _PE_RELOCATION_BLOCK {
  170. PVOID PageRva;
  171. ULONG BlockSizeInBytes;
  172. } PACKED PE_RELOCATION_BLOCK, *PPE_RELOCATION_BLOCK;
  173. typedef USHORT PE_RELOCATION, *PPE_RELOCATION;
  174. typedef enum _PE_RELOCATION_TYPE {
  175. PeRelocationAbsolute = 0,
  176. PeRelocationHigh = 1,
  177. PeRelocationLow = 2,
  178. PeRelocationHighLow = 3,
  179. PeRelocationHighAdjust = 4,
  180. PeRelocationMipsJumpAddress = 5,
  181. PeRelocationMipsJumpAddress16 = 9,
  182. PeRelocation64 = 10
  183. } PE_RELOCATION_TYPE, *PPE_RELOCATION_TYPE;
  184. typedef struct _PE_EXPORT_DIRECTORY_TABLE {
  185. ULONG ExportFlags;
  186. ULONG Timestamp;
  187. USHORT MajorVersion;
  188. USHORT MinorVersion;
  189. ULONG NameRva;
  190. ULONG OrdinalBase;
  191. ULONG AddressTableEntryCount;
  192. ULONG NamePointerCount;
  193. ULONG ExportAddressTableRva;
  194. ULONG NamePointerRva;
  195. ULONG OrdinalTableRva;
  196. } PACKED PE_EXPORT_DIRECTORY_TABLE, *PPE_EXPORT_DIRECTORY_TABLE;
  197. typedef struct _PE_IMPORT_DIRECTORY_TABLE {
  198. ULONG ImportLookupTableRva;
  199. ULONG Timestamp;
  200. ULONG ForwarderChain;
  201. ULONG NameRva;
  202. ULONG ImportAddressTableRva;
  203. } PACKED PE_IMPORT_DIRECTORY_TABLE, *PPE_IMPORT_DIRECTORY_TABLE;
  204. typedef ULONG PE_IMPORT_LOOKUP_TABLE, *PPE_IMPORT_LOOKUP_TABLE;
  205. typedef struct _PE_IMPORT_NAME_ENTRY {
  206. USHORT Hint;
  207. CHAR Name[ANYSIZE_ARRAY];
  208. } PACKED PE_IMPORT_NAME_ENTRY, *PPE_IMPORT_NAME_ENTRY;
  209. typedef struct _COFF_SYMBOL {
  210. union {
  211. CHAR Name[COFF_SYMBOL_NAME_LENGTH];
  212. struct {
  213. ULONG Zeroes;
  214. ULONG Offset;
  215. };
  216. };
  217. ULONG Value;
  218. USHORT Section;
  219. USHORT Type;
  220. UCHAR Class;
  221. UCHAR AuxCount;
  222. } PACKED COFF_SYMBOL, *PCOFF_SYMBOL;
  223. //
  224. // -------------------------------------------------------- Function Prototypes
  225. //
  226. BOOL
  227. ImpPeGetHeaders (
  228. PIMAGE_BUFFER Buffer,
  229. PIMAGE_NT_HEADERS *PeHeaders
  230. );
  231. /*++
  232. Routine Description:
  233. This routine returns a pointer to the PE image headers given a buffer
  234. containing the executable image mapped in memory.
  235. Arguments:
  236. Buffer - Supplies a pointer to the buffer to get the headers from.
  237. PeHeaders - Supplies a pointer where the location of the PE headers will
  238. be returned.
  239. Return Value:
  240. TRUE on success.
  241. FALSE otherwise.
  242. --*/
  243. BOOL
  244. ImpPeGetSection (
  245. PIMAGE_BUFFER Buffer,
  246. PSTR SectionName,
  247. PVOID *Section,
  248. PULONGLONG VirtualAddress,
  249. PULONG SectionSizeInFile,
  250. PULONG SectionSizeInMemory
  251. );
  252. /*++
  253. Routine Description:
  254. This routine gets a pointer to the given section in a PE image given a
  255. memory mapped file.
  256. Arguments:
  257. Buffer - Supplies a pointer to the file buffer.
  258. SectionName - Supplies the name of the desired section.
  259. Section - Supplies a pointer where the pointer to the section will be
  260. returned.
  261. VirtualAddress - Supplies a pointer where the virtual address of the section
  262. will be returned, if applicable.
  263. SectionSizeInFile - Supplies a pointer where the size of the section as it
  264. appears in the file will be returned.
  265. SectionSizeInMemory - Supplies a pointer where the size of the section as it
  266. appears after being loaded in memory will be returned.
  267. Return Value:
  268. TRUE on success.
  269. FALSE otherwise.
  270. --*/