1 /* 2 * Portable Executable binary format structures 3 * 4 * Copyright (c) 2016 Alexander Graf 5 * 6 * Based on wine code 7 * 8 * SPDX-License-Identifier: GPL-2.0+ 9 */ 10 11 #ifndef _PE_H 12 #define _PE_H 13 14 #include <asm-generic/pe.h> 15 16 typedef struct _IMAGE_DOS_HEADER { 17 uint16_t e_magic; /* 00: MZ Header signature */ 18 uint16_t e_cblp; /* 02: Bytes on last page of file */ 19 uint16_t e_cp; /* 04: Pages in file */ 20 uint16_t e_crlc; /* 06: Relocations */ 21 uint16_t e_cparhdr; /* 08: Size of header in paragraphs */ 22 uint16_t e_minalloc; /* 0a: Minimum extra paragraphs needed */ 23 uint16_t e_maxalloc; /* 0c: Maximum extra paragraphs needed */ 24 uint16_t e_ss; /* 0e: Initial (relative) SS value */ 25 uint16_t e_sp; /* 10: Initial SP value */ 26 uint16_t e_csum; /* 12: Checksum */ 27 uint16_t e_ip; /* 14: Initial IP value */ 28 uint16_t e_cs; /* 16: Initial (relative) CS value */ 29 uint16_t e_lfarlc; /* 18: File address of relocation table */ 30 uint16_t e_ovno; /* 1a: Overlay number */ 31 uint16_t e_res[4]; /* 1c: Reserved words */ 32 uint16_t e_oemid; /* 24: OEM identifier (for e_oeminfo) */ 33 uint16_t e_oeminfo; /* 26: OEM information; e_oemid specific */ 34 uint16_t e_res2[10]; /* 28: Reserved words */ 35 uint32_t e_lfanew; /* 3c: Offset to extended header */ 36 } IMAGE_DOS_HEADER, *PIMAGE_DOS_HEADER; 37 38 #define IMAGE_DOS_SIGNATURE 0x5A4D /* MZ */ 39 #define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */ 40 41 #define IMAGE_FILE_MACHINE_ARM 0x01c0 42 #define IMAGE_FILE_MACHINE_THUMB 0x01c2 43 #define IMAGE_FILE_MACHINE_ARMNT 0x01c4 44 #define IMAGE_FILE_MACHINE_AMD64 0x8664 45 #define IMAGE_FILE_MACHINE_ARM64 0xaa64 46 #define IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b 47 #define IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b 48 #define IMAGE_SUBSYSTEM_EFI_APPLICATION 10 49 50 typedef struct _IMAGE_FILE_HEADER { 51 uint16_t Machine; 52 uint16_t NumberOfSections; 53 uint32_t TimeDateStamp; 54 uint32_t PointerToSymbolTable; 55 uint32_t NumberOfSymbols; 56 uint16_t SizeOfOptionalHeader; 57 uint16_t Characteristics; 58 } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER; 59 60 typedef struct _IMAGE_DATA_DIRECTORY { 61 uint32_t VirtualAddress; 62 uint32_t Size; 63 } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY; 64 65 #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16 66 67 typedef struct _IMAGE_OPTIONAL_HEADER64 { 68 uint16_t Magic; /* 0x20b */ 69 uint8_t MajorLinkerVersion; 70 uint8_t MinorLinkerVersion; 71 uint32_t SizeOfCode; 72 uint32_t SizeOfInitializedData; 73 uint32_t SizeOfUninitializedData; 74 uint32_t AddressOfEntryPoint; 75 uint32_t BaseOfCode; 76 uint64_t ImageBase; 77 uint32_t SectionAlignment; 78 uint32_t FileAlignment; 79 uint16_t MajorOperatingSystemVersion; 80 uint16_t MinorOperatingSystemVersion; 81 uint16_t MajorImageVersion; 82 uint16_t MinorImageVersion; 83 uint16_t MajorSubsystemVersion; 84 uint16_t MinorSubsystemVersion; 85 uint32_t Win32VersionValue; 86 uint32_t SizeOfImage; 87 uint32_t SizeOfHeaders; 88 uint32_t CheckSum; 89 uint16_t Subsystem; 90 uint16_t DllCharacteristics; 91 uint64_t SizeOfStackReserve; 92 uint64_t SizeOfStackCommit; 93 uint64_t SizeOfHeapReserve; 94 uint64_t SizeOfHeapCommit; 95 uint32_t LoaderFlags; 96 uint32_t NumberOfRvaAndSizes; 97 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; 98 } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64; 99 100 typedef struct _IMAGE_NT_HEADERS64 { 101 uint32_t Signature; 102 IMAGE_FILE_HEADER FileHeader; 103 IMAGE_OPTIONAL_HEADER64 OptionalHeader; 104 } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64; 105 106 typedef struct _IMAGE_OPTIONAL_HEADER { 107 108 /* Standard fields */ 109 110 uint16_t Magic; /* 0x10b or 0x107 */ /* 0x00 */ 111 uint8_t MajorLinkerVersion; 112 uint8_t MinorLinkerVersion; 113 uint32_t SizeOfCode; 114 uint32_t SizeOfInitializedData; 115 uint32_t SizeOfUninitializedData; 116 uint32_t AddressOfEntryPoint; /* 0x10 */ 117 uint32_t BaseOfCode; 118 uint32_t BaseOfData; 119 120 /* NT additional fields */ 121 122 uint32_t ImageBase; 123 uint32_t SectionAlignment; /* 0x20 */ 124 uint32_t FileAlignment; 125 uint16_t MajorOperatingSystemVersion; 126 uint16_t MinorOperatingSystemVersion; 127 uint16_t MajorImageVersion; 128 uint16_t MinorImageVersion; 129 uint16_t MajorSubsystemVersion; /* 0x30 */ 130 uint16_t MinorSubsystemVersion; 131 uint32_t Win32VersionValue; 132 uint32_t SizeOfImage; 133 uint32_t SizeOfHeaders; 134 uint32_t CheckSum; /* 0x40 */ 135 uint16_t Subsystem; 136 uint16_t DllCharacteristics; 137 uint32_t SizeOfStackReserve; 138 uint32_t SizeOfStackCommit; 139 uint32_t SizeOfHeapReserve; /* 0x50 */ 140 uint32_t SizeOfHeapCommit; 141 uint32_t LoaderFlags; 142 uint32_t NumberOfRvaAndSizes; 143 IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; /* 0x60 */ 144 /* 0xE0 */ 145 } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32; 146 147 typedef struct _IMAGE_NT_HEADERS { 148 uint32_t Signature; /* "PE"\0\0 */ /* 0x00 */ 149 IMAGE_FILE_HEADER FileHeader; /* 0x04 */ 150 IMAGE_OPTIONAL_HEADER32 OptionalHeader; /* 0x18 */ 151 } IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32; 152 153 #define IMAGE_SIZEOF_SHORT_NAME 8 154 155 typedef struct _IMAGE_SECTION_HEADER { 156 uint8_t Name[IMAGE_SIZEOF_SHORT_NAME]; 157 union { 158 uint32_t PhysicalAddress; 159 uint32_t VirtualSize; 160 } Misc; 161 uint32_t VirtualAddress; 162 uint32_t SizeOfRawData; 163 uint32_t PointerToRawData; 164 uint32_t PointerToRelocations; 165 uint32_t PointerToLinenumbers; 166 uint16_t NumberOfRelocations; 167 uint16_t NumberOfLinenumbers; 168 uint32_t Characteristics; 169 } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER; 170 171 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 172 173 typedef struct _IMAGE_BASE_RELOCATION 174 { 175 uint32_t VirtualAddress; 176 uint32_t SizeOfBlock; 177 /* WORD TypeOffset[1]; */ 178 } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION; 179 180 typedef struct _IMAGE_RELOCATION 181 { 182 union { 183 uint32_t VirtualAddress; 184 uint32_t RelocCount; 185 } DUMMYUNIONNAME; 186 uint32_t SymbolTableIndex; 187 uint16_t Type; 188 } IMAGE_RELOCATION, *PIMAGE_RELOCATION; 189 190 #define IMAGE_SIZEOF_RELOCATION 10 191 192 /* generic relocation types */ 193 #define IMAGE_REL_BASED_ABSOLUTE 0 194 #define IMAGE_REL_BASED_HIGH 1 195 #define IMAGE_REL_BASED_LOW 2 196 #define IMAGE_REL_BASED_HIGHLOW 3 197 #define IMAGE_REL_BASED_HIGHADJ 4 198 #define IMAGE_REL_BASED_MIPS_JMPADDR 5 199 #define IMAGE_REL_BASED_ARM_MOV32A 5 /* yes, 5 too */ 200 #define IMAGE_REL_BASED_ARM_MOV32 5 /* yes, 5 too */ 201 #define IMAGE_REL_BASED_SECTION 6 202 #define IMAGE_REL_BASED_REL 7 203 #define IMAGE_REL_BASED_ARM_MOV32T 7 /* yes, 7 too */ 204 #define IMAGE_REL_BASED_THUMB_MOV32 7 /* yes, 7 too */ 205 #define IMAGE_REL_BASED_MIPS_JMPADDR16 9 206 #define IMAGE_REL_BASED_IA64_IMM64 9 /* yes, 9 too */ 207 #define IMAGE_REL_BASED_DIR64 10 208 #define IMAGE_REL_BASED_HIGH3ADJ 11 209 210 /* ARM relocation types */ 211 #define IMAGE_REL_ARM_ABSOLUTE 0x0000 212 #define IMAGE_REL_ARM_ADDR 0x0001 213 #define IMAGE_REL_ARM_ADDR32NB 0x0002 214 #define IMAGE_REL_ARM_BRANCH24 0x0003 215 #define IMAGE_REL_ARM_BRANCH11 0x0004 216 #define IMAGE_REL_ARM_TOKEN 0x0005 217 #define IMAGE_REL_ARM_GPREL12 0x0006 218 #define IMAGE_REL_ARM_GPREL7 0x0007 219 #define IMAGE_REL_ARM_BLX24 0x0008 220 #define IMAGE_REL_ARM_BLX11 0x0009 221 #define IMAGE_REL_ARM_SECTION 0x000E 222 #define IMAGE_REL_ARM_SECREL 0x000F 223 #define IMAGE_REL_ARM_MOV32A 0x0010 224 #define IMAGE_REL_ARM_MOV32T 0x0011 225 #define IMAGE_REL_ARM_BRANCH20T 0x0012 226 #define IMAGE_REL_ARM_BRANCH24T 0x0014 227 #define IMAGE_REL_ARM_BLX23T 0x0015 228 229 /* ARM64 relocation types */ 230 #define IMAGE_REL_ARM64_ABSOLUTE 0x0000 231 #define IMAGE_REL_ARM64_ADDR32 0x0001 232 #define IMAGE_REL_ARM64_ADDR32NB 0x0002 233 #define IMAGE_REL_ARM64_BRANCH26 0x0003 234 #define IMAGE_REL_ARM64_PAGEBASE_REL21 0x0004 235 #define IMAGE_REL_ARM64_REL21 0x0005 236 #define IMAGE_REL_ARM64_PAGEOFFSET_12A 0x0006 237 #define IMAGE_REL_ARM64_PAGEOFFSET_12L 0x0007 238 #define IMAGE_REL_ARM64_SECREL 0x0008 239 #define IMAGE_REL_ARM64_SECREL_LOW12A 0x0009 240 #define IMAGE_REL_ARM64_SECREL_HIGH12A 0x000A 241 #define IMAGE_REL_ARM64_SECREL_LOW12L 0x000B 242 #define IMAGE_REL_ARM64_TOKEN 0x000C 243 #define IMAGE_REL_ARM64_SECTION 0x000D 244 #define IMAGE_REL_ARM64_ADDR64 0x000E 245 246 /* AMD64 relocation types */ 247 #define IMAGE_REL_AMD64_ABSOLUTE 0x0000 248 #define IMAGE_REL_AMD64_ADDR64 0x0001 249 #define IMAGE_REL_AMD64_ADDR32 0x0002 250 #define IMAGE_REL_AMD64_ADDR32NB 0x0003 251 #define IMAGE_REL_AMD64_REL32 0x0004 252 #define IMAGE_REL_AMD64_REL32_1 0x0005 253 #define IMAGE_REL_AMD64_REL32_2 0x0006 254 #define IMAGE_REL_AMD64_REL32_3 0x0007 255 #define IMAGE_REL_AMD64_REL32_4 0x0008 256 #define IMAGE_REL_AMD64_REL32_5 0x0009 257 #define IMAGE_REL_AMD64_SECTION 0x000A 258 #define IMAGE_REL_AMD64_SECREL 0x000B 259 #define IMAGE_REL_AMD64_SECREL7 0x000C 260 #define IMAGE_REL_AMD64_TOKEN 0x000D 261 #define IMAGE_REL_AMD64_SREL32 0x000E 262 #define IMAGE_REL_AMD64_PAIR 0x000F 263 #define IMAGE_REL_AMD64_SSPAN32 0x0010 264 265 #endif /* _PE_H */ 266