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