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