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