xref: /illumos-gate/usr/src/boot/efi/include/IndustryStandard/PeImage.h (revision 627ade2ad3d27999387b31183c830a2d42c63ef5)
1 /** @file
2   EFI image format for PE32, PE32+ and TE. Please note some data structures are
3   different for PE32 and PE32+. EFI_IMAGE_NT_HEADERS32 is for PE32 and
4   EFI_IMAGE_NT_HEADERS64 is for PE32+.
5 
6   This file is coded to the Visual Studio, Microsoft Portable Executable and
7   Common Object File Format Specification, Revision 8.3 - February 6, 2013.
8   This file also includes some definitions in PI Specification, Revision 1.0.
9 
10 Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
11 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
12 Portions Copyright (c) 2016 - 2020, Hewlett Packard Enterprise Development LP. All rights reserved.<BR>
13 
14 SPDX-License-Identifier: BSD-2-Clause-Patent
15 
16 **/
17 
18 #ifndef __PE_IMAGE_H__
19 #define __PE_IMAGE_H__
20 
21 //
22 // PE32+ Subsystem type for EFI images
23 //
24 #define EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION          10
25 #define EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER  11
26 #define EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER       12
27 #define EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER       13///< defined PI Specification, 1.0
28 
29 //
30 // PE32+ Machine type for EFI images
31 //
32 #define IMAGE_FILE_MACHINE_I386            0x014c
33 #define IMAGE_FILE_MACHINE_IA64            0x0200
34 #define IMAGE_FILE_MACHINE_EBC             0x0EBC
35 #define IMAGE_FILE_MACHINE_X64             0x8664
36 #define IMAGE_FILE_MACHINE_ARMTHUMB_MIXED  0x01c2
37 #define IMAGE_FILE_MACHINE_ARM64           0xAA64
38 #define IMAGE_FILE_MACHINE_RISCV32         0x5032
39 #define IMAGE_FILE_MACHINE_RISCV64         0x5064
40 #define IMAGE_FILE_MACHINE_RISCV128        0x5128
41 
42 //
43 // EXE file formats
44 //
45 #define EFI_IMAGE_DOS_SIGNATURE     SIGNATURE_16('M', 'Z')
46 #define EFI_IMAGE_OS2_SIGNATURE     SIGNATURE_16('N', 'E')
47 #define EFI_IMAGE_OS2_SIGNATURE_LE  SIGNATURE_16('L', 'E')
48 #define EFI_IMAGE_NT_SIGNATURE      SIGNATURE_32('P', 'E', '\0', '\0')
49 
50 ///
51 /// PE images can start with an optional DOS header, so if an image is run
52 /// under DOS it can print an error message.
53 ///
54 typedef struct {
55   UINT16    e_magic;    ///< Magic number.
56   UINT16    e_cblp;     ///< Bytes on last page of file.
57   UINT16    e_cp;       ///< Pages in file.
58   UINT16    e_crlc;     ///< Relocations.
59   UINT16    e_cparhdr;  ///< Size of header in paragraphs.
60   UINT16    e_minalloc; ///< Minimum extra paragraphs needed.
61   UINT16    e_maxalloc; ///< Maximum extra paragraphs needed.
62   UINT16    e_ss;       ///< Initial (relative) SS value.
63   UINT16    e_sp;       ///< Initial SP value.
64   UINT16    e_csum;     ///< Checksum.
65   UINT16    e_ip;       ///< Initial IP value.
66   UINT16    e_cs;       ///< Initial (relative) CS value.
67   UINT16    e_lfarlc;   ///< File address of relocation table.
68   UINT16    e_ovno;     ///< Overlay number.
69   UINT16    e_res[4];   ///< Reserved words.
70   UINT16    e_oemid;    ///< OEM identifier (for e_oeminfo).
71   UINT16    e_oeminfo;  ///< OEM information; e_oemid specific.
72   UINT16    e_res2[10]; ///< Reserved words.
73   UINT32    e_lfanew;   ///< File address of new exe header.
74 } EFI_IMAGE_DOS_HEADER;
75 
76 ///
77 /// COFF File Header (Object and Image).
78 ///
79 typedef struct {
80   UINT16    Machine;
81   UINT16    NumberOfSections;
82   UINT32    TimeDateStamp;
83   UINT32    PointerToSymbolTable;
84   UINT32    NumberOfSymbols;
85   UINT16    SizeOfOptionalHeader;
86   UINT16    Characteristics;
87 } EFI_IMAGE_FILE_HEADER;
88 
89 ///
90 /// Size of EFI_IMAGE_FILE_HEADER.
91 ///
92 #define EFI_IMAGE_SIZEOF_FILE_HEADER  20
93 
94 //
95 // Characteristics
96 //
97 #define EFI_IMAGE_FILE_RELOCS_STRIPPED      BIT0     ///< 0x0001  Relocation info stripped from file.
98 #define EFI_IMAGE_FILE_EXECUTABLE_IMAGE     BIT1     ///< 0x0002  File is executable  (i.e. no unresolved externel references).
99 #define EFI_IMAGE_FILE_LINE_NUMS_STRIPPED   BIT2     ///< 0x0004  Line numbers stripped from file.
100 #define EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED  BIT3     ///< 0x0008  Local symbols stripped from file.
101 #define EFI_IMAGE_FILE_BYTES_REVERSED_LO    BIT7     ///< 0x0080  Bytes of machine word are reversed.
102 #define EFI_IMAGE_FILE_32BIT_MACHINE        BIT8     ///< 0x0100  32 bit word machine.
103 #define EFI_IMAGE_FILE_DEBUG_STRIPPED       BIT9     ///< 0x0200  Debugging info stripped from file in .DBG file.
104 #define EFI_IMAGE_FILE_SYSTEM               BIT12    ///< 0x1000  System File.
105 #define EFI_IMAGE_FILE_DLL                  BIT13    ///< 0x2000  File is a DLL.
106 #define EFI_IMAGE_FILE_BYTES_REVERSED_HI    BIT15    ///< 0x8000  Bytes of machine word are reversed.
107 
108 ///
109 /// Header Data Directories.
110 ///
111 typedef struct {
112   UINT32    VirtualAddress;
113   UINT32    Size;
114 } EFI_IMAGE_DATA_DIRECTORY;
115 
116 //
117 // Directory Entries
118 //
119 #define EFI_IMAGE_DIRECTORY_ENTRY_EXPORT       0
120 #define EFI_IMAGE_DIRECTORY_ENTRY_IMPORT       1
121 #define EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE     2
122 #define EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION    3
123 #define EFI_IMAGE_DIRECTORY_ENTRY_SECURITY     4
124 #define EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC    5
125 #define EFI_IMAGE_DIRECTORY_ENTRY_DEBUG        6
126 #define EFI_IMAGE_DIRECTORY_ENTRY_COPYRIGHT    7
127 #define EFI_IMAGE_DIRECTORY_ENTRY_GLOBALPTR    8
128 #define EFI_IMAGE_DIRECTORY_ENTRY_TLS          9
129 #define EFI_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG  10
130 
131 #define EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES  16
132 
133 ///
134 /// @attention
135 /// EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC means PE32 and
136 /// EFI_IMAGE_OPTIONAL_HEADER32 must be used. The data structures only vary
137 /// after NT additional fields.
138 ///
139 #define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC  0x10b
140 
141 ///
142 /// Optional Header Standard Fields for PE32.
143 ///
144 typedef struct {
145   ///
146   /// Standard fields.
147   ///
148   UINT16                      Magic;
149   UINT8                       MajorLinkerVersion;
150   UINT8                       MinorLinkerVersion;
151   UINT32                      SizeOfCode;
152   UINT32                      SizeOfInitializedData;
153   UINT32                      SizeOfUninitializedData;
154   UINT32                      AddressOfEntryPoint;
155   UINT32                      BaseOfCode;
156   UINT32                      BaseOfData; ///< PE32 contains this additional field, which is absent in PE32+.
157   ///
158   /// Optional Header Windows-Specific Fields.
159   ///
160   UINT32                      ImageBase;
161   UINT32                      SectionAlignment;
162   UINT32                      FileAlignment;
163   UINT16                      MajorOperatingSystemVersion;
164   UINT16                      MinorOperatingSystemVersion;
165   UINT16                      MajorImageVersion;
166   UINT16                      MinorImageVersion;
167   UINT16                      MajorSubsystemVersion;
168   UINT16                      MinorSubsystemVersion;
169   UINT32                      Win32VersionValue;
170   UINT32                      SizeOfImage;
171   UINT32                      SizeOfHeaders;
172   UINT32                      CheckSum;
173   UINT16                      Subsystem;
174   UINT16                      DllCharacteristics;
175   UINT32                      SizeOfStackReserve;
176   UINT32                      SizeOfStackCommit;
177   UINT32                      SizeOfHeapReserve;
178   UINT32                      SizeOfHeapCommit;
179   UINT32                      LoaderFlags;
180   UINT32                      NumberOfRvaAndSizes;
181   EFI_IMAGE_DATA_DIRECTORY    DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES];
182 } EFI_IMAGE_OPTIONAL_HEADER32;
183 
184 ///
185 /// @attention
186 /// EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC means PE32+ and
187 /// EFI_IMAGE_OPTIONAL_HEADER64 must be used. The data structures only vary
188 /// after NT additional fields.
189 ///
190 #define EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC  0x20b
191 
192 ///
193 /// Optional Header Standard Fields for PE32+.
194 ///
195 typedef struct {
196   ///
197   /// Standard fields.
198   ///
199   UINT16                      Magic;
200   UINT8                       MajorLinkerVersion;
201   UINT8                       MinorLinkerVersion;
202   UINT32                      SizeOfCode;
203   UINT32                      SizeOfInitializedData;
204   UINT32                      SizeOfUninitializedData;
205   UINT32                      AddressOfEntryPoint;
206   UINT32                      BaseOfCode;
207   ///
208   /// Optional Header Windows-Specific Fields.
209   ///
210   UINT64                      ImageBase;
211   UINT32                      SectionAlignment;
212   UINT32                      FileAlignment;
213   UINT16                      MajorOperatingSystemVersion;
214   UINT16                      MinorOperatingSystemVersion;
215   UINT16                      MajorImageVersion;
216   UINT16                      MinorImageVersion;
217   UINT16                      MajorSubsystemVersion;
218   UINT16                      MinorSubsystemVersion;
219   UINT32                      Win32VersionValue;
220   UINT32                      SizeOfImage;
221   UINT32                      SizeOfHeaders;
222   UINT32                      CheckSum;
223   UINT16                      Subsystem;
224   UINT16                      DllCharacteristics;
225   UINT64                      SizeOfStackReserve;
226   UINT64                      SizeOfStackCommit;
227   UINT64                      SizeOfHeapReserve;
228   UINT64                      SizeOfHeapCommit;
229   UINT32                      LoaderFlags;
230   UINT32                      NumberOfRvaAndSizes;
231   EFI_IMAGE_DATA_DIRECTORY    DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES];
232 } EFI_IMAGE_OPTIONAL_HEADER64;
233 
234 ///
235 /// @attention
236 /// EFI_IMAGE_NT_HEADERS32 is for use ONLY by tools.
237 ///
238 typedef struct {
239   UINT32                         Signature;
240   EFI_IMAGE_FILE_HEADER          FileHeader;
241   EFI_IMAGE_OPTIONAL_HEADER32    OptionalHeader;
242 } EFI_IMAGE_NT_HEADERS32;
243 
244 #define EFI_IMAGE_SIZEOF_NT_OPTIONAL32_HEADER  sizeof (EFI_IMAGE_NT_HEADERS32)
245 
246 ///
247 /// @attention
248 /// EFI_IMAGE_HEADERS64 is for use ONLY by tools.
249 ///
250 typedef struct {
251   UINT32                         Signature;
252   EFI_IMAGE_FILE_HEADER          FileHeader;
253   EFI_IMAGE_OPTIONAL_HEADER64    OptionalHeader;
254 } EFI_IMAGE_NT_HEADERS64;
255 
256 #define EFI_IMAGE_SIZEOF_NT_OPTIONAL64_HEADER  sizeof (EFI_IMAGE_NT_HEADERS64)
257 
258 //
259 // Other Windows Subsystem Values
260 //
261 #define EFI_IMAGE_SUBSYSTEM_UNKNOWN      0
262 #define EFI_IMAGE_SUBSYSTEM_NATIVE       1
263 #define EFI_IMAGE_SUBSYSTEM_WINDOWS_GUI  2
264 #define EFI_IMAGE_SUBSYSTEM_WINDOWS_CUI  3
265 #define EFI_IMAGE_SUBSYSTEM_OS2_CUI      5
266 #define EFI_IMAGE_SUBSYSTEM_POSIX_CUI    7
267 
268 ///
269 /// Length of ShortName.
270 ///
271 #define EFI_IMAGE_SIZEOF_SHORT_NAME  8
272 
273 ///
274 /// Section Table. This table immediately follows the optional header.
275 ///
276 typedef struct {
277   UINT8     Name[EFI_IMAGE_SIZEOF_SHORT_NAME];
278   union {
279     UINT32    PhysicalAddress;
280     UINT32    VirtualSize;
281   } Misc;
282   UINT32    VirtualAddress;
283   UINT32    SizeOfRawData;
284   UINT32    PointerToRawData;
285   UINT32    PointerToRelocations;
286   UINT32    PointerToLinenumbers;
287   UINT16    NumberOfRelocations;
288   UINT16    NumberOfLinenumbers;
289   UINT32    Characteristics;
290 } EFI_IMAGE_SECTION_HEADER;
291 
292 ///
293 /// Size of EFI_IMAGE_SECTION_HEADER.
294 ///
295 #define EFI_IMAGE_SIZEOF_SECTION_HEADER  40
296 
297 //
298 // Section Flags Values
299 //
300 #define EFI_IMAGE_SCN_TYPE_NO_PAD             BIT3        ///< 0x00000008  ///< Reserved.
301 #define EFI_IMAGE_SCN_CNT_CODE                BIT5        ///< 0x00000020
302 #define EFI_IMAGE_SCN_CNT_INITIALIZED_DATA    BIT6        ///< 0x00000040
303 #define EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA  BIT7        ///< 0x00000080
304 
305 #define EFI_IMAGE_SCN_LNK_OTHER   BIT8                    ///< 0x00000100  ///< Reserved.
306 #define EFI_IMAGE_SCN_LNK_INFO    BIT9                    ///< 0x00000200  ///< Section contains comments or some other type of information.
307 #define EFI_IMAGE_SCN_LNK_REMOVE  BIT11                   ///< 0x00000800  ///< Section contents will not become part of image.
308 #define EFI_IMAGE_SCN_LNK_COMDAT  BIT12                   ///< 0x00001000
309 
310 #define EFI_IMAGE_SCN_ALIGN_1BYTES   BIT20                ///< 0x00100000
311 #define EFI_IMAGE_SCN_ALIGN_2BYTES   BIT21                ///< 0x00200000
312 #define EFI_IMAGE_SCN_ALIGN_4BYTES   (BIT20|BIT21)        ///< 0x00300000
313 #define EFI_IMAGE_SCN_ALIGN_8BYTES   BIT22                ///< 0x00400000
314 #define EFI_IMAGE_SCN_ALIGN_16BYTES  (BIT20|BIT22)        ///< 0x00500000
315 #define EFI_IMAGE_SCN_ALIGN_32BYTES  (BIT21|BIT22)        ///< 0x00600000
316 #define EFI_IMAGE_SCN_ALIGN_64BYTES  (BIT20|BIT21|BIT22)  ///< 0x00700000
317 
318 #define EFI_IMAGE_SCN_MEM_DISCARDABLE  BIT25              ///< 0x02000000
319 #define EFI_IMAGE_SCN_MEM_NOT_CACHED   BIT26              ///< 0x04000000
320 #define EFI_IMAGE_SCN_MEM_NOT_PAGED    BIT27              ///< 0x08000000
321 #define EFI_IMAGE_SCN_MEM_SHARED       BIT28              ///< 0x10000000
322 #define EFI_IMAGE_SCN_MEM_EXECUTE      BIT29              ///< 0x20000000
323 #define EFI_IMAGE_SCN_MEM_READ         BIT30              ///< 0x40000000
324 #define EFI_IMAGE_SCN_MEM_WRITE        BIT31              ///< 0x80000000
325 
326 ///
327 /// Size of a Symbol Table Record.
328 ///
329 #define EFI_IMAGE_SIZEOF_SYMBOL  18
330 
331 //
332 // Symbols have a section number of the section in which they are
333 // defined. Otherwise, section numbers have the following meanings:
334 //
335 #define EFI_IMAGE_SYM_UNDEFINED  (UINT16) 0  ///< Symbol is undefined or is common.
336 #define EFI_IMAGE_SYM_ABSOLUTE   (UINT16) -1 ///< Symbol is an absolute value.
337 #define EFI_IMAGE_SYM_DEBUG      (UINT16) -2 ///< Symbol is a special debug item.
338 
339 //
340 // Symbol Type (fundamental) values.
341 //
342 #define EFI_IMAGE_SYM_TYPE_NULL    0  ///< no type.
343 #define EFI_IMAGE_SYM_TYPE_VOID    1  ///< no valid type.
344 #define EFI_IMAGE_SYM_TYPE_CHAR    2  ///< type character.
345 #define EFI_IMAGE_SYM_TYPE_SHORT   3  ///< type short integer.
346 #define EFI_IMAGE_SYM_TYPE_INT     4
347 #define EFI_IMAGE_SYM_TYPE_LONG    5
348 #define EFI_IMAGE_SYM_TYPE_FLOAT   6
349 #define EFI_IMAGE_SYM_TYPE_DOUBLE  7
350 #define EFI_IMAGE_SYM_TYPE_STRUCT  8
351 #define EFI_IMAGE_SYM_TYPE_UNION   9
352 #define EFI_IMAGE_SYM_TYPE_ENUM    10 ///< enumeration.
353 #define EFI_IMAGE_SYM_TYPE_MOE     11 ///< member of enumeration.
354 #define EFI_IMAGE_SYM_TYPE_BYTE    12
355 #define EFI_IMAGE_SYM_TYPE_WORD    13
356 #define EFI_IMAGE_SYM_TYPE_UINT    14
357 #define EFI_IMAGE_SYM_TYPE_DWORD   15
358 
359 //
360 // Symbol Type (derived) values.
361 //
362 #define EFI_IMAGE_SYM_DTYPE_NULL      0 ///< no derived type.
363 #define EFI_IMAGE_SYM_DTYPE_POINTER   1
364 #define EFI_IMAGE_SYM_DTYPE_FUNCTION  2
365 #define EFI_IMAGE_SYM_DTYPE_ARRAY     3
366 
367 //
368 // Storage classes.
369 //
370 #define EFI_IMAGE_SYM_CLASS_END_OF_FUNCTION   ((UINT8) -1)
371 #define EFI_IMAGE_SYM_CLASS_NULL              0
372 #define EFI_IMAGE_SYM_CLASS_AUTOMATIC         1
373 #define EFI_IMAGE_SYM_CLASS_EXTERNAL          2
374 #define EFI_IMAGE_SYM_CLASS_STATIC            3
375 #define EFI_IMAGE_SYM_CLASS_REGISTER          4
376 #define EFI_IMAGE_SYM_CLASS_EXTERNAL_DEF      5
377 #define EFI_IMAGE_SYM_CLASS_LABEL             6
378 #define EFI_IMAGE_SYM_CLASS_UNDEFINED_LABEL   7
379 #define EFI_IMAGE_SYM_CLASS_MEMBER_OF_STRUCT  8
380 #define EFI_IMAGE_SYM_CLASS_ARGUMENT          9
381 #define EFI_IMAGE_SYM_CLASS_STRUCT_TAG        10
382 #define EFI_IMAGE_SYM_CLASS_MEMBER_OF_UNION   11
383 #define EFI_IMAGE_SYM_CLASS_UNION_TAG         12
384 #define EFI_IMAGE_SYM_CLASS_TYPE_DEFINITION   13
385 #define EFI_IMAGE_SYM_CLASS_UNDEFINED_STATIC  14
386 #define EFI_IMAGE_SYM_CLASS_ENUM_TAG          15
387 #define EFI_IMAGE_SYM_CLASS_MEMBER_OF_ENUM    16
388 #define EFI_IMAGE_SYM_CLASS_REGISTER_PARAM    17
389 #define EFI_IMAGE_SYM_CLASS_BIT_FIELD         18
390 #define EFI_IMAGE_SYM_CLASS_BLOCK             100
391 #define EFI_IMAGE_SYM_CLASS_FUNCTION          101
392 #define EFI_IMAGE_SYM_CLASS_END_OF_STRUCT     102
393 #define EFI_IMAGE_SYM_CLASS_FILE              103
394 #define EFI_IMAGE_SYM_CLASS_SECTION           104
395 #define EFI_IMAGE_SYM_CLASS_WEAK_EXTERNAL     105
396 
397 //
398 // type packing constants
399 //
400 #define EFI_IMAGE_N_BTMASK  017
401 #define EFI_IMAGE_N_TMASK   060
402 #define EFI_IMAGE_N_TMASK1  0300
403 #define EFI_IMAGE_N_TMASK2  0360
404 #define EFI_IMAGE_N_BTSHFT  4
405 #define EFI_IMAGE_N_TSHIFT  2
406 
407 //
408 // Communal selection types.
409 //
410 #define EFI_IMAGE_COMDAT_SELECT_NODUPLICATES  1
411 #define EFI_IMAGE_COMDAT_SELECT_ANY           2
412 #define EFI_IMAGE_COMDAT_SELECT_SAME_SIZE     3
413 #define EFI_IMAGE_COMDAT_SELECT_EXACT_MATCH   4
414 #define EFI_IMAGE_COMDAT_SELECT_ASSOCIATIVE   5
415 
416 //
417 // the following values only be referred in PeCoff, not defined in PECOFF.
418 //
419 #define EFI_IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY  1
420 #define EFI_IMAGE_WEAK_EXTERN_SEARCH_LIBRARY    2
421 #define EFI_IMAGE_WEAK_EXTERN_SEARCH_ALIAS      3
422 
423 ///
424 /// Relocation format.
425 ///
426 typedef struct {
427   UINT32    VirtualAddress;
428   UINT32    SymbolTableIndex;
429   UINT16    Type;
430 } EFI_IMAGE_RELOCATION;
431 
432 ///
433 /// Size of EFI_IMAGE_RELOCATION
434 ///
435 #define EFI_IMAGE_SIZEOF_RELOCATION  10
436 
437 //
438 // I386 relocation types.
439 //
440 #define EFI_IMAGE_REL_I386_ABSOLUTE  0x0000 ///< Reference is absolute, no relocation is necessary.
441 #define EFI_IMAGE_REL_I386_DIR16     0x0001 ///< Direct 16-bit reference to the symbols virtual address.
442 #define EFI_IMAGE_REL_I386_REL16     0x0002 ///< PC-relative 16-bit reference to the symbols virtual address.
443 #define EFI_IMAGE_REL_I386_DIR32     0x0006 ///< Direct 32-bit reference to the symbols virtual address.
444 #define EFI_IMAGE_REL_I386_DIR32NB   0x0007 ///< Direct 32-bit reference to the symbols virtual address, base not included.
445 #define EFI_IMAGE_REL_I386_SEG12     0x0009 ///< Direct 16-bit reference to the segment-selector bits of a 32-bit virtual address.
446 #define EFI_IMAGE_REL_I386_SECTION   0x000A
447 #define EFI_IMAGE_REL_I386_SECREL    0x000B
448 #define EFI_IMAGE_REL_I386_REL32     0x0014 ///< PC-relative 32-bit reference to the symbols virtual address.
449 
450 //
451 // x64 processor relocation types.
452 //
453 #define IMAGE_REL_AMD64_ABSOLUTE  0x0000
454 #define IMAGE_REL_AMD64_ADDR64    0x0001
455 #define IMAGE_REL_AMD64_ADDR32    0x0002
456 #define IMAGE_REL_AMD64_ADDR32NB  0x0003
457 #define IMAGE_REL_AMD64_REL32     0x0004
458 #define IMAGE_REL_AMD64_REL32_1   0x0005
459 #define IMAGE_REL_AMD64_REL32_2   0x0006
460 #define IMAGE_REL_AMD64_REL32_3   0x0007
461 #define IMAGE_REL_AMD64_REL32_4   0x0008
462 #define IMAGE_REL_AMD64_REL32_5   0x0009
463 #define IMAGE_REL_AMD64_SECTION   0x000A
464 #define IMAGE_REL_AMD64_SECREL    0x000B
465 #define IMAGE_REL_AMD64_SECREL7   0x000C
466 #define IMAGE_REL_AMD64_TOKEN     0x000D
467 #define IMAGE_REL_AMD64_SREL32    0x000E
468 #define IMAGE_REL_AMD64_PAIR      0x000F
469 #define IMAGE_REL_AMD64_SSPAN32   0x0010
470 
471 ///
472 /// Based relocation format.
473 ///
474 typedef struct {
475   UINT32    VirtualAddress;
476   UINT32    SizeOfBlock;
477 } EFI_IMAGE_BASE_RELOCATION;
478 
479 ///
480 /// Size of EFI_IMAGE_BASE_RELOCATION.
481 ///
482 #define EFI_IMAGE_SIZEOF_BASE_RELOCATION  8
483 
484 //
485 // Based relocation types.
486 //
487 #define EFI_IMAGE_REL_BASED_ABSOLUTE        0
488 #define EFI_IMAGE_REL_BASED_HIGH            1
489 #define EFI_IMAGE_REL_BASED_LOW             2
490 #define EFI_IMAGE_REL_BASED_HIGHLOW         3
491 #define EFI_IMAGE_REL_BASED_HIGHADJ         4
492 #define EFI_IMAGE_REL_BASED_MIPS_JMPADDR    5
493 #define EFI_IMAGE_REL_BASED_ARM_MOV32A      5
494 #define EFI_IMAGE_REL_BASED_ARM_MOV32T      7
495 #define EFI_IMAGE_REL_BASED_IA64_IMM64      9
496 #define EFI_IMAGE_REL_BASED_MIPS_JMPADDR16  9
497 #define EFI_IMAGE_REL_BASED_DIR64           10
498 
499 ///
500 /// Relocation types of RISC-V processor.
501 ///
502 #define EFI_IMAGE_REL_BASED_RISCV_HI20    5
503 #define EFI_IMAGE_REL_BASED_RISCV_LOW12I  7
504 #define EFI_IMAGE_REL_BASED_RISCV_LOW12S  8
505 
506 ///
507 /// Line number format.
508 ///
509 typedef struct {
510   union {
511     UINT32    SymbolTableIndex; ///< Symbol table index of function name if Linenumber is 0.
512     UINT32    VirtualAddress;   ///< Virtual address of line number.
513   } Type;
514   UINT16    Linenumber;       ///< Line number.
515 } EFI_IMAGE_LINENUMBER;
516 
517 ///
518 /// Size of EFI_IMAGE_LINENUMBER.
519 ///
520 #define EFI_IMAGE_SIZEOF_LINENUMBER  6
521 
522 //
523 // Archive format.
524 //
525 #define EFI_IMAGE_ARCHIVE_START_SIZE        8
526 #define EFI_IMAGE_ARCHIVE_START             "!<arch>\n"
527 #define EFI_IMAGE_ARCHIVE_END               "`\n"
528 #define EFI_IMAGE_ARCHIVE_PAD               "\n"
529 #define EFI_IMAGE_ARCHIVE_LINKER_MEMBER     "/               "
530 #define EFI_IMAGE_ARCHIVE_LONGNAMES_MEMBER  "//              "
531 
532 ///
533 /// Archive Member Headers
534 ///
535 typedef struct {
536   UINT8    Name[16];     ///< File member name - `/' terminated.
537   UINT8    Date[12];     ///< File member date - decimal.
538   UINT8    UserID[6];    ///< File member user id - decimal.
539   UINT8    GroupID[6];   ///< File member group id - decimal.
540   UINT8    Mode[8];      ///< File member mode - octal.
541   UINT8    Size[10];     ///< File member size - decimal.
542   UINT8    EndHeader[2]; ///< String to end header. (0x60 0x0A).
543 } EFI_IMAGE_ARCHIVE_MEMBER_HEADER;
544 
545 ///
546 /// Size of EFI_IMAGE_ARCHIVE_MEMBER_HEADER.
547 ///
548 #define EFI_IMAGE_SIZEOF_ARCHIVE_MEMBER_HDR  60
549 
550 //
551 // DLL Support
552 //
553 
554 ///
555 /// Export Directory Table.
556 ///
557 typedef struct {
558   UINT32    Characteristics;
559   UINT32    TimeDateStamp;
560   UINT16    MajorVersion;
561   UINT16    MinorVersion;
562   UINT32    Name;
563   UINT32    Base;
564   UINT32    NumberOfFunctions;
565   UINT32    NumberOfNames;
566   UINT32    AddressOfFunctions;
567   UINT32    AddressOfNames;
568   UINT32    AddressOfNameOrdinals;
569 } EFI_IMAGE_EXPORT_DIRECTORY;
570 
571 ///
572 /// Hint/Name Table.
573 ///
574 typedef struct {
575   UINT16    Hint;
576   UINT8     Name[1];
577 } EFI_IMAGE_IMPORT_BY_NAME;
578 
579 ///
580 /// Import Address Table RVA (Thunk Table).
581 ///
582 typedef struct {
583   union {
584     UINT32                      Function;
585     UINT32                      Ordinal;
586     EFI_IMAGE_IMPORT_BY_NAME    *AddressOfData;
587   } u1;
588 } EFI_IMAGE_THUNK_DATA;
589 
590 #define EFI_IMAGE_ORDINAL_FLAG  BIT31                ///< Flag for PE32.
591 #define EFI_IMAGE_SNAP_BY_ORDINAL(Ordinal)  ((Ordinal & EFI_IMAGE_ORDINAL_FLAG) != 0)
592 #define EFI_IMAGE_ORDINAL(Ordinal)          (Ordinal & 0xffff)
593 
594 ///
595 /// Import Directory Table
596 ///
597 typedef struct {
598   UINT32                  Characteristics;
599   UINT32                  TimeDateStamp;
600   UINT32                  ForwarderChain;
601   UINT32                  Name;
602   EFI_IMAGE_THUNK_DATA    *FirstThunk;
603 } EFI_IMAGE_IMPORT_DESCRIPTOR;
604 
605 ///
606 /// Debug Directory Format.
607 ///
608 typedef struct {
609   UINT32    Characteristics;
610   UINT32    TimeDateStamp;
611   UINT16    MajorVersion;
612   UINT16    MinorVersion;
613   UINT32    Type;
614   UINT32    SizeOfData;
615   UINT32    RVA;         ///< The address of the debug data when loaded, relative to the image base.
616   UINT32    FileOffset;  ///< The file pointer to the debug data.
617 } EFI_IMAGE_DEBUG_DIRECTORY_ENTRY;
618 
619 #define EFI_IMAGE_DEBUG_TYPE_CODEVIEW  2    ///< The Visual C++ debug information.
620 
621 ///
622 /// Debug Data Structure defined in Microsoft C++.
623 ///
624 #define CODEVIEW_SIGNATURE_NB10  SIGNATURE_32('N', 'B', '1', '0')
625 typedef struct {
626   UINT32    Signature;                      ///< "NB10"
627   UINT32    Unknown;
628   UINT32    Unknown2;
629   UINT32    Unknown3;
630   //
631   // Filename of .PDB goes here
632   //
633 } EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY;
634 
635 ///
636 /// Debug Data Structure defined in Microsoft C++.
637 ///
638 #define CODEVIEW_SIGNATURE_RSDS  SIGNATURE_32('R', 'S', 'D', 'S')
639 typedef struct {
640   UINT32    Signature;                      ///< "RSDS".
641   UINT32    Unknown;
642   UINT32    Unknown2;
643   UINT32    Unknown3;
644   UINT32    Unknown4;
645   UINT32    Unknown5;
646   //
647   // Filename of .PDB goes here
648   //
649 } EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY;
650 
651 ///
652 /// Debug Data Structure defined by Apple Mach-O to Coff utility.
653 ///
654 #define CODEVIEW_SIGNATURE_MTOC  SIGNATURE_32('M', 'T', 'O', 'C')
655 typedef struct {
656   UINT32    Signature;                       ///< "MTOC".
657   GUID      MachOUuid;
658   //
659   //  Filename of .DLL (Mach-O with debug info) goes here
660   //
661 } EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY;
662 
663 ///
664 /// Resource format.
665 ///
666 typedef struct {
667   UINT32    Characteristics;
668   UINT32    TimeDateStamp;
669   UINT16    MajorVersion;
670   UINT16    MinorVersion;
671   UINT16    NumberOfNamedEntries;
672   UINT16    NumberOfIdEntries;
673   //
674   // Array of EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY entries goes here.
675   //
676 } EFI_IMAGE_RESOURCE_DIRECTORY;
677 
678 ///
679 /// Resource directory entry format.
680 ///
681 typedef struct {
682   union {
683     struct {
684       UINT32    NameOffset   : 31;
685       UINT32    NameIsString : 1;
686     } s;
687     UINT32    Id;
688   } u1;
689   union {
690     UINT32    OffsetToData;
691     struct {
692       UINT32    OffsetToDirectory : 31;
693       UINT32    DataIsDirectory   : 1;
694     } s;
695   } u2;
696 } EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY;
697 
698 ///
699 /// Resource directory entry for string.
700 ///
701 typedef struct {
702   UINT16    Length;
703   CHAR16    String[1];
704 } EFI_IMAGE_RESOURCE_DIRECTORY_STRING;
705 
706 ///
707 /// Resource directory entry for data array.
708 ///
709 typedef struct {
710   UINT32    OffsetToData;
711   UINT32    Size;
712   UINT32    CodePage;
713   UINT32    Reserved;
714 } EFI_IMAGE_RESOURCE_DATA_ENTRY;
715 
716 ///
717 /// Header format for TE images, defined in the PI Specification, 1.0.
718 ///
719 typedef struct {
720   UINT16                      Signature;           ///< The signature for TE format = "VZ".
721   UINT16                      Machine;             ///< From the original file header.
722   UINT8                       NumberOfSections;    ///< From the original file header.
723   UINT8                       Subsystem;           ///< From original optional header.
724   UINT16                      StrippedSize;        ///< Number of bytes we removed from the header.
725   UINT32                      AddressOfEntryPoint; ///< Offset to entry point -- from original optional header.
726   UINT32                      BaseOfCode;          ///< From original image -- required for ITP debug.
727   UINT64                      ImageBase;           ///< From original file header.
728   EFI_IMAGE_DATA_DIRECTORY    DataDirectory[2];    ///< Only base relocation and debug directory.
729 } EFI_TE_IMAGE_HEADER;
730 
731 #define EFI_TE_IMAGE_HEADER_SIGNATURE  SIGNATURE_16('V', 'Z')
732 
733 //
734 // Data directory indexes in our TE image header
735 //
736 #define EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC  0
737 #define EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG      1
738 
739 ///
740 /// Union of PE32, PE32+, and TE headers.
741 ///
742 typedef union {
743   EFI_IMAGE_NT_HEADERS32    Pe32;
744   EFI_IMAGE_NT_HEADERS64    Pe32Plus;
745   EFI_TE_IMAGE_HEADER       Te;
746 } EFI_IMAGE_OPTIONAL_HEADER_UNION;
747 
748 typedef union {
749   EFI_IMAGE_NT_HEADERS32             *Pe32;
750   EFI_IMAGE_NT_HEADERS64             *Pe32Plus;
751   EFI_TE_IMAGE_HEADER                *Te;
752   EFI_IMAGE_OPTIONAL_HEADER_UNION    *Union;
753 } EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION;
754 
755 #endif
756