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