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