xref: /openbmc/u-boot/include/pe.h (revision 61a5ced6)
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 
14268ec6e0SHeinrich Schuchardt #include <asm-generic/pe.h>
15268ec6e0SHeinrich 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 
41*61a5ced6SIvan Gorinov #define IMAGE_FILE_MACHINE_I386		0x014c
42cb149c66SAlexander Graf #define IMAGE_FILE_MACHINE_ARM		0x01c0
43cb149c66SAlexander Graf #define IMAGE_FILE_MACHINE_THUMB	0x01c2
44cb149c66SAlexander Graf #define IMAGE_FILE_MACHINE_ARMNT	0x01c4
45cb149c66SAlexander Graf #define IMAGE_FILE_MACHINE_AMD64	0x8664
46cb149c66SAlexander Graf #define IMAGE_FILE_MACHINE_ARM64	0xaa64
47*61a5ced6SIvan Gorinov #define IMAGE_FILE_MACHINE_RISCV32	0x5032
48*61a5ced6SIvan Gorinov #define IMAGE_FILE_MACHINE_RISCV64	0x5064
49*61a5ced6SIvan Gorinov 
50cb149c66SAlexander Graf #define IMAGE_NT_OPTIONAL_HDR32_MAGIC	0x10b
51cb149c66SAlexander Graf #define IMAGE_NT_OPTIONAL_HDR64_MAGIC	0x20b
52cb149c66SAlexander Graf #define IMAGE_SUBSYSTEM_EFI_APPLICATION	10
53cb149c66SAlexander Graf 
54cb149c66SAlexander Graf typedef struct _IMAGE_FILE_HEADER {
55cb149c66SAlexander Graf 	uint16_t Machine;
56cb149c66SAlexander Graf 	uint16_t NumberOfSections;
57cb149c66SAlexander Graf 	uint32_t TimeDateStamp;
58cb149c66SAlexander Graf 	uint32_t PointerToSymbolTable;
59cb149c66SAlexander Graf 	uint32_t NumberOfSymbols;
60cb149c66SAlexander Graf 	uint16_t SizeOfOptionalHeader;
61cb149c66SAlexander Graf 	uint16_t Characteristics;
62cb149c66SAlexander Graf } IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
63cb149c66SAlexander Graf 
64cb149c66SAlexander Graf typedef struct _IMAGE_DATA_DIRECTORY {
65cb149c66SAlexander Graf 	uint32_t VirtualAddress;
66cb149c66SAlexander Graf 	uint32_t Size;
67cb149c66SAlexander Graf } IMAGE_DATA_DIRECTORY, *PIMAGE_DATA_DIRECTORY;
68cb149c66SAlexander Graf 
69cb149c66SAlexander Graf #define IMAGE_NUMBEROF_DIRECTORY_ENTRIES 16
70cb149c66SAlexander Graf 
71cb149c66SAlexander Graf typedef struct _IMAGE_OPTIONAL_HEADER64 {
72cb149c66SAlexander Graf 	uint16_t Magic; /* 0x20b */
73cb149c66SAlexander Graf 	uint8_t  MajorLinkerVersion;
74cb149c66SAlexander Graf 	uint8_t  MinorLinkerVersion;
75cb149c66SAlexander Graf 	uint32_t SizeOfCode;
76cb149c66SAlexander Graf 	uint32_t SizeOfInitializedData;
77cb149c66SAlexander Graf 	uint32_t SizeOfUninitializedData;
78cb149c66SAlexander Graf 	uint32_t AddressOfEntryPoint;
79cb149c66SAlexander Graf 	uint32_t BaseOfCode;
80cb149c66SAlexander Graf 	uint64_t ImageBase;
81cb149c66SAlexander Graf 	uint32_t SectionAlignment;
82cb149c66SAlexander Graf 	uint32_t FileAlignment;
83cb149c66SAlexander Graf 	uint16_t MajorOperatingSystemVersion;
84cb149c66SAlexander Graf 	uint16_t MinorOperatingSystemVersion;
85cb149c66SAlexander Graf 	uint16_t MajorImageVersion;
86cb149c66SAlexander Graf 	uint16_t MinorImageVersion;
87cb149c66SAlexander Graf 	uint16_t MajorSubsystemVersion;
88cb149c66SAlexander Graf 	uint16_t MinorSubsystemVersion;
89cb149c66SAlexander Graf 	uint32_t Win32VersionValue;
90cb149c66SAlexander Graf 	uint32_t SizeOfImage;
91cb149c66SAlexander Graf 	uint32_t SizeOfHeaders;
92cb149c66SAlexander Graf 	uint32_t CheckSum;
93cb149c66SAlexander Graf 	uint16_t Subsystem;
94cb149c66SAlexander Graf 	uint16_t DllCharacteristics;
95cb149c66SAlexander Graf 	uint64_t SizeOfStackReserve;
96cb149c66SAlexander Graf 	uint64_t SizeOfStackCommit;
97cb149c66SAlexander Graf 	uint64_t SizeOfHeapReserve;
98cb149c66SAlexander Graf 	uint64_t SizeOfHeapCommit;
99cb149c66SAlexander Graf 	uint32_t LoaderFlags;
100cb149c66SAlexander Graf 	uint32_t NumberOfRvaAndSizes;
101cb149c66SAlexander Graf 	IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES];
102cb149c66SAlexander Graf } IMAGE_OPTIONAL_HEADER64, *PIMAGE_OPTIONAL_HEADER64;
103cb149c66SAlexander Graf 
104cb149c66SAlexander Graf typedef struct _IMAGE_NT_HEADERS64 {
105cb149c66SAlexander Graf 	uint32_t Signature;
106cb149c66SAlexander Graf 	IMAGE_FILE_HEADER FileHeader;
107cb149c66SAlexander Graf 	IMAGE_OPTIONAL_HEADER64 OptionalHeader;
108cb149c66SAlexander Graf } IMAGE_NT_HEADERS64, *PIMAGE_NT_HEADERS64;
109cb149c66SAlexander Graf 
110cb149c66SAlexander Graf typedef struct _IMAGE_OPTIONAL_HEADER {
111cb149c66SAlexander Graf 
112cb149c66SAlexander Graf 	/* Standard fields */
113cb149c66SAlexander Graf 
114cb149c66SAlexander Graf 	uint16_t Magic; /* 0x10b or 0x107 */     /* 0x00 */
115cb149c66SAlexander Graf 	uint8_t  MajorLinkerVersion;
116cb149c66SAlexander Graf 	uint8_t  MinorLinkerVersion;
117cb149c66SAlexander Graf 	uint32_t SizeOfCode;
118cb149c66SAlexander Graf 	uint32_t SizeOfInitializedData;
119cb149c66SAlexander Graf 	uint32_t SizeOfUninitializedData;
120cb149c66SAlexander Graf 	uint32_t AddressOfEntryPoint;            /* 0x10 */
121cb149c66SAlexander Graf 	uint32_t BaseOfCode;
122cb149c66SAlexander Graf 	uint32_t BaseOfData;
123cb149c66SAlexander Graf 
124cb149c66SAlexander Graf 	/* NT additional fields */
125cb149c66SAlexander Graf 
126cb149c66SAlexander Graf 	uint32_t ImageBase;
127cb149c66SAlexander Graf 	uint32_t SectionAlignment;               /* 0x20 */
128cb149c66SAlexander Graf 	uint32_t FileAlignment;
129cb149c66SAlexander Graf 	uint16_t MajorOperatingSystemVersion;
130cb149c66SAlexander Graf 	uint16_t MinorOperatingSystemVersion;
131cb149c66SAlexander Graf 	uint16_t MajorImageVersion;
132cb149c66SAlexander Graf 	uint16_t MinorImageVersion;
133cb149c66SAlexander Graf 	uint16_t MajorSubsystemVersion;          /* 0x30 */
134cb149c66SAlexander Graf 	uint16_t MinorSubsystemVersion;
135cb149c66SAlexander Graf 	uint32_t Win32VersionValue;
136cb149c66SAlexander Graf 	uint32_t SizeOfImage;
137cb149c66SAlexander Graf 	uint32_t SizeOfHeaders;
138cb149c66SAlexander Graf 	uint32_t CheckSum;                       /* 0x40 */
139cb149c66SAlexander Graf 	uint16_t Subsystem;
140cb149c66SAlexander Graf 	uint16_t DllCharacteristics;
141cb149c66SAlexander Graf 	uint32_t SizeOfStackReserve;
142cb149c66SAlexander Graf 	uint32_t SizeOfStackCommit;
143cb149c66SAlexander Graf 	uint32_t SizeOfHeapReserve;              /* 0x50 */
144cb149c66SAlexander Graf 	uint32_t SizeOfHeapCommit;
145cb149c66SAlexander Graf 	uint32_t LoaderFlags;
146cb149c66SAlexander Graf 	uint32_t NumberOfRvaAndSizes;
147cb149c66SAlexander Graf 	IMAGE_DATA_DIRECTORY DataDirectory[IMAGE_NUMBEROF_DIRECTORY_ENTRIES]; /* 0x60 */
148cb149c66SAlexander Graf 	/* 0xE0 */
149cb149c66SAlexander Graf } IMAGE_OPTIONAL_HEADER32, *PIMAGE_OPTIONAL_HEADER32;
150cb149c66SAlexander Graf 
151cb149c66SAlexander Graf typedef struct _IMAGE_NT_HEADERS {
152cb149c66SAlexander Graf 	uint32_t Signature; /* "PE"\0\0 */       /* 0x00 */
153cb149c66SAlexander Graf 	IMAGE_FILE_HEADER FileHeader;         /* 0x04 */
154cb149c66SAlexander Graf 	IMAGE_OPTIONAL_HEADER32 OptionalHeader;       /* 0x18 */
155cb149c66SAlexander Graf } IMAGE_NT_HEADERS32, *PIMAGE_NT_HEADERS32;
156cb149c66SAlexander Graf 
157cb149c66SAlexander Graf #define IMAGE_SIZEOF_SHORT_NAME 8
158cb149c66SAlexander Graf 
159cb149c66SAlexander Graf typedef struct _IMAGE_SECTION_HEADER {
160cb149c66SAlexander Graf 	uint8_t	Name[IMAGE_SIZEOF_SHORT_NAME];
161cb149c66SAlexander Graf 	union {
162cb149c66SAlexander Graf 		uint32_t PhysicalAddress;
163cb149c66SAlexander Graf 		uint32_t VirtualSize;
164cb149c66SAlexander Graf 	} Misc;
165cb149c66SAlexander Graf 	uint32_t VirtualAddress;
166cb149c66SAlexander Graf 	uint32_t SizeOfRawData;
167cb149c66SAlexander Graf 	uint32_t PointerToRawData;
168cb149c66SAlexander Graf 	uint32_t PointerToRelocations;
169cb149c66SAlexander Graf 	uint32_t PointerToLinenumbers;
170cb149c66SAlexander Graf 	uint16_t NumberOfRelocations;
171cb149c66SAlexander Graf 	uint16_t NumberOfLinenumbers;
172cb149c66SAlexander Graf 	uint32_t Characteristics;
173cb149c66SAlexander Graf } IMAGE_SECTION_HEADER, *PIMAGE_SECTION_HEADER;
174cb149c66SAlexander Graf 
175cb149c66SAlexander Graf #define IMAGE_DIRECTORY_ENTRY_BASERELOC         5
176cb149c66SAlexander Graf 
177cb149c66SAlexander Graf typedef struct _IMAGE_BASE_RELOCATION
178cb149c66SAlexander Graf {
179cb149c66SAlexander Graf         uint32_t VirtualAddress;
180cb149c66SAlexander Graf         uint32_t SizeOfBlock;
181cb149c66SAlexander Graf         /* WORD TypeOffset[1]; */
182cb149c66SAlexander Graf } IMAGE_BASE_RELOCATION,*PIMAGE_BASE_RELOCATION;
183cb149c66SAlexander Graf 
184cb149c66SAlexander Graf typedef struct _IMAGE_RELOCATION
185cb149c66SAlexander Graf {
186cb149c66SAlexander Graf 	union {
187cb149c66SAlexander Graf 		uint32_t VirtualAddress;
188cb149c66SAlexander Graf 		uint32_t RelocCount;
189cb149c66SAlexander Graf 	} DUMMYUNIONNAME;
190cb149c66SAlexander Graf 	uint32_t SymbolTableIndex;
191cb149c66SAlexander Graf 	uint16_t Type;
192cb149c66SAlexander Graf } IMAGE_RELOCATION, *PIMAGE_RELOCATION;
193cb149c66SAlexander Graf 
194cb149c66SAlexander Graf #define IMAGE_SIZEOF_RELOCATION 10
195cb149c66SAlexander Graf 
196cb149c66SAlexander Graf /* generic relocation types */
197cb149c66SAlexander Graf #define IMAGE_REL_BASED_ABSOLUTE                0
198cb149c66SAlexander Graf #define IMAGE_REL_BASED_HIGH                    1
199cb149c66SAlexander Graf #define IMAGE_REL_BASED_LOW                     2
200cb149c66SAlexander Graf #define IMAGE_REL_BASED_HIGHLOW                 3
201cb149c66SAlexander Graf #define IMAGE_REL_BASED_HIGHADJ                 4
202cb149c66SAlexander Graf #define IMAGE_REL_BASED_MIPS_JMPADDR            5
203cb149c66SAlexander Graf #define IMAGE_REL_BASED_ARM_MOV32A              5 /* yes, 5 too */
204cb149c66SAlexander Graf #define IMAGE_REL_BASED_ARM_MOV32               5 /* yes, 5 too */
205cb149c66SAlexander Graf #define IMAGE_REL_BASED_SECTION                 6
206cb149c66SAlexander Graf #define IMAGE_REL_BASED_REL                     7
207cb149c66SAlexander Graf #define IMAGE_REL_BASED_ARM_MOV32T              7 /* yes, 7 too */
208cb149c66SAlexander Graf #define IMAGE_REL_BASED_THUMB_MOV32             7 /* yes, 7 too */
209cb149c66SAlexander Graf #define IMAGE_REL_BASED_MIPS_JMPADDR16          9
210cb149c66SAlexander Graf #define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
211cb149c66SAlexander Graf #define IMAGE_REL_BASED_DIR64                   10
212cb149c66SAlexander Graf #define IMAGE_REL_BASED_HIGH3ADJ                11
213cb149c66SAlexander Graf 
214cb149c66SAlexander Graf /* ARM relocation types */
215cb149c66SAlexander Graf #define IMAGE_REL_ARM_ABSOLUTE          0x0000
216cb149c66SAlexander Graf #define IMAGE_REL_ARM_ADDR              0x0001
217cb149c66SAlexander Graf #define IMAGE_REL_ARM_ADDR32NB          0x0002
218cb149c66SAlexander Graf #define IMAGE_REL_ARM_BRANCH24          0x0003
219cb149c66SAlexander Graf #define IMAGE_REL_ARM_BRANCH11          0x0004
220cb149c66SAlexander Graf #define IMAGE_REL_ARM_TOKEN             0x0005
221cb149c66SAlexander Graf #define IMAGE_REL_ARM_GPREL12           0x0006
222cb149c66SAlexander Graf #define IMAGE_REL_ARM_GPREL7            0x0007
223cb149c66SAlexander Graf #define IMAGE_REL_ARM_BLX24             0x0008
224cb149c66SAlexander Graf #define IMAGE_REL_ARM_BLX11             0x0009
225cb149c66SAlexander Graf #define IMAGE_REL_ARM_SECTION           0x000E
226cb149c66SAlexander Graf #define IMAGE_REL_ARM_SECREL            0x000F
227cb149c66SAlexander Graf #define IMAGE_REL_ARM_MOV32A            0x0010
228cb149c66SAlexander Graf #define IMAGE_REL_ARM_MOV32T            0x0011
229cb149c66SAlexander Graf #define IMAGE_REL_ARM_BRANCH20T         0x0012
230cb149c66SAlexander Graf #define IMAGE_REL_ARM_BRANCH24T         0x0014
231cb149c66SAlexander Graf #define IMAGE_REL_ARM_BLX23T            0x0015
232cb149c66SAlexander Graf 
233cb149c66SAlexander Graf /* ARM64 relocation types */
234cb149c66SAlexander Graf #define IMAGE_REL_ARM64_ABSOLUTE        0x0000
235cb149c66SAlexander Graf #define IMAGE_REL_ARM64_ADDR32          0x0001
236cb149c66SAlexander Graf #define IMAGE_REL_ARM64_ADDR32NB        0x0002
237cb149c66SAlexander Graf #define IMAGE_REL_ARM64_BRANCH26        0x0003
238cb149c66SAlexander Graf #define IMAGE_REL_ARM64_PAGEBASE_REL21  0x0004
239cb149c66SAlexander Graf #define IMAGE_REL_ARM64_REL21           0x0005
240cb149c66SAlexander Graf #define IMAGE_REL_ARM64_PAGEOFFSET_12A  0x0006
241cb149c66SAlexander Graf #define IMAGE_REL_ARM64_PAGEOFFSET_12L  0x0007
242cb149c66SAlexander Graf #define IMAGE_REL_ARM64_SECREL          0x0008
243cb149c66SAlexander Graf #define IMAGE_REL_ARM64_SECREL_LOW12A   0x0009
244cb149c66SAlexander Graf #define IMAGE_REL_ARM64_SECREL_HIGH12A  0x000A
245cb149c66SAlexander Graf #define IMAGE_REL_ARM64_SECREL_LOW12L   0x000B
246cb149c66SAlexander Graf #define IMAGE_REL_ARM64_TOKEN           0x000C
247cb149c66SAlexander Graf #define IMAGE_REL_ARM64_SECTION         0x000D
248cb149c66SAlexander Graf #define IMAGE_REL_ARM64_ADDR64          0x000E
249cb149c66SAlexander Graf 
250cb149c66SAlexander Graf /* AMD64 relocation types */
251cb149c66SAlexander Graf #define IMAGE_REL_AMD64_ABSOLUTE        0x0000
252cb149c66SAlexander Graf #define IMAGE_REL_AMD64_ADDR64          0x0001
253cb149c66SAlexander Graf #define IMAGE_REL_AMD64_ADDR32          0x0002
254cb149c66SAlexander Graf #define IMAGE_REL_AMD64_ADDR32NB        0x0003
255cb149c66SAlexander Graf #define IMAGE_REL_AMD64_REL32           0x0004
256cb149c66SAlexander Graf #define IMAGE_REL_AMD64_REL32_1         0x0005
257cb149c66SAlexander Graf #define IMAGE_REL_AMD64_REL32_2         0x0006
258cb149c66SAlexander Graf #define IMAGE_REL_AMD64_REL32_3         0x0007
259cb149c66SAlexander Graf #define IMAGE_REL_AMD64_REL32_4         0x0008
260cb149c66SAlexander Graf #define IMAGE_REL_AMD64_REL32_5         0x0009
261cb149c66SAlexander Graf #define IMAGE_REL_AMD64_SECTION         0x000A
262cb149c66SAlexander Graf #define IMAGE_REL_AMD64_SECREL          0x000B
263cb149c66SAlexander Graf #define IMAGE_REL_AMD64_SECREL7         0x000C
264cb149c66SAlexander Graf #define IMAGE_REL_AMD64_TOKEN           0x000D
265cb149c66SAlexander Graf #define IMAGE_REL_AMD64_SREL32          0x000E
266cb149c66SAlexander Graf #define IMAGE_REL_AMD64_PAIR            0x000F
267cb149c66SAlexander Graf #define IMAGE_REL_AMD64_SSPAN32         0x0010
268cb149c66SAlexander Graf 
269cb149c66SAlexander Graf #endif /* _PE_H */
270