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