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