iPXE
PeImage.h
Go to the documentation of this file.
00001 /** @file
00002   EFI image format for PE32, PE32+ and TE. Please note some data structures are
00003   different for PE32 and PE32+. EFI_IMAGE_NT_HEADERS32 is for PE32 and
00004   EFI_IMAGE_NT_HEADERS64 is for PE32+.
00005 
00006   This file is coded to the Visual Studio, Microsoft Portable Executable and
00007   Common Object File Format Specification, Revision 8.3 - February 6, 2013.
00008   This file also includes some definitions in PI Specification, Revision 1.0.
00009 
00010 Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
00011 Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>
00012 This program and the accompanying materials
00013 are licensed and made available under the terms and conditions of the BSD License
00014 which accompanies this distribution.  The full text of the license may be found at
00015 http://opensource.org/licenses/bsd-license.php.
00016 
00017 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
00018 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
00019 
00020 **/
00021 
00022 #ifndef __PE_IMAGE_H__
00023 #define __PE_IMAGE_H__
00024 
00025 FILE_LICENCE ( BSD3 );
00026 
00027 //
00028 // PE32+ Subsystem type for EFI images
00029 //
00030 #define EFI_IMAGE_SUBSYSTEM_EFI_APPLICATION         10
00031 #define EFI_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER 11
00032 #define EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER      12
00033 #define EFI_IMAGE_SUBSYSTEM_SAL_RUNTIME_DRIVER      13 ///< defined PI Specification, 1.0
00034 
00035 
00036 //
00037 // PE32+ Machine type for EFI images
00038 //
00039 #define IMAGE_FILE_MACHINE_I386            0x014c
00040 #define IMAGE_FILE_MACHINE_IA64            0x0200
00041 #define IMAGE_FILE_MACHINE_EBC             0x0EBC
00042 #define IMAGE_FILE_MACHINE_X64             0x8664
00043 #define IMAGE_FILE_MACHINE_ARMTHUMB_MIXED  0x01c2
00044 #define IMAGE_FILE_MACHINE_ARM64           0xAA64
00045 
00046 //
00047 // EXE file formats
00048 //
00049 #define EFI_IMAGE_DOS_SIGNATURE     SIGNATURE_16('M', 'Z')
00050 #define EFI_IMAGE_OS2_SIGNATURE     SIGNATURE_16('N', 'E')
00051 #define EFI_IMAGE_OS2_SIGNATURE_LE  SIGNATURE_16('L', 'E')
00052 #define EFI_IMAGE_NT_SIGNATURE      SIGNATURE_32('P', 'E', '\0', '\0')
00053 
00054 ///
00055 /// PE images can start with an optional DOS header, so if an image is run
00056 /// under DOS it can print an error message.
00057 ///
00058 typedef struct {
00059   UINT16  e_magic;    ///< Magic number.
00060   UINT16  e_cblp;     ///< Bytes on last page of file.
00061   UINT16  e_cp;       ///< Pages in file.
00062   UINT16  e_crlc;     ///< Relocations.
00063   UINT16  e_cparhdr;  ///< Size of header in paragraphs.
00064   UINT16  e_minalloc; ///< Minimum extra paragraphs needed.
00065   UINT16  e_maxalloc; ///< Maximum extra paragraphs needed.
00066   UINT16  e_ss;       ///< Initial (relative) SS value.
00067   UINT16  e_sp;       ///< Initial SP value.
00068   UINT16  e_csum;     ///< Checksum.
00069   UINT16  e_ip;       ///< Initial IP value.
00070   UINT16  e_cs;       ///< Initial (relative) CS value.
00071   UINT16  e_lfarlc;   ///< File address of relocation table.
00072   UINT16  e_ovno;     ///< Overlay number.
00073   UINT16  e_res[4];   ///< Reserved words.
00074   UINT16  e_oemid;    ///< OEM identifier (for e_oeminfo).
00075   UINT16  e_oeminfo;  ///< OEM information; e_oemid specific.
00076   UINT16  e_res2[10]; ///< Reserved words.
00077   UINT32  e_lfanew;   ///< File address of new exe header.
00078 } EFI_IMAGE_DOS_HEADER;
00079 
00080 ///
00081 /// COFF File Header (Object and Image).
00082 ///
00083 typedef struct {
00084   UINT16  Machine;
00085   UINT16  NumberOfSections;
00086   UINT32  TimeDateStamp;
00087   UINT32  PointerToSymbolTable;
00088   UINT32  NumberOfSymbols;
00089   UINT16  SizeOfOptionalHeader;
00090   UINT16  Characteristics;
00091 } EFI_IMAGE_FILE_HEADER;
00092 
00093 ///
00094 /// Size of EFI_IMAGE_FILE_HEADER.
00095 ///
00096 #define EFI_IMAGE_SIZEOF_FILE_HEADER        20
00097 
00098 //
00099 // Characteristics
00100 //
00101 #define EFI_IMAGE_FILE_RELOCS_STRIPPED      BIT0     ///< 0x0001  Relocation info stripped from file.
00102 #define EFI_IMAGE_FILE_EXECUTABLE_IMAGE     BIT1     ///< 0x0002  File is executable  (i.e. no unresolved externel references).
00103 #define EFI_IMAGE_FILE_LINE_NUMS_STRIPPED   BIT2     ///< 0x0004  Line nunbers stripped from file.
00104 #define EFI_IMAGE_FILE_LOCAL_SYMS_STRIPPED  BIT3     ///< 0x0008  Local symbols stripped from file.
00105 #define EFI_IMAGE_FILE_BYTES_REVERSED_LO    BIT7     ///< 0x0080  Bytes of machine word are reversed.
00106 #define EFI_IMAGE_FILE_32BIT_MACHINE        BIT8     ///< 0x0100  32 bit word machine.
00107 #define EFI_IMAGE_FILE_DEBUG_STRIPPED       BIT9     ///< 0x0200  Debugging info stripped from file in .DBG file.
00108 #define EFI_IMAGE_FILE_SYSTEM               BIT12    ///< 0x1000  System File.
00109 #define EFI_IMAGE_FILE_DLL                  BIT13    ///< 0x2000  File is a DLL.
00110 #define EFI_IMAGE_FILE_BYTES_REVERSED_HI    BIT15    ///< 0x8000  Bytes of machine word are reversed.
00111 
00112 ///
00113 /// Header Data Directories.
00114 ///
00115 typedef struct {
00116   UINT32  VirtualAddress;
00117   UINT32  Size;
00118 } EFI_IMAGE_DATA_DIRECTORY;
00119 
00120 //
00121 // Directory Entries
00122 //
00123 #define EFI_IMAGE_DIRECTORY_ENTRY_EXPORT      0
00124 #define EFI_IMAGE_DIRECTORY_ENTRY_IMPORT      1
00125 #define EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE    2
00126 #define EFI_IMAGE_DIRECTORY_ENTRY_EXCEPTION   3
00127 #define EFI_IMAGE_DIRECTORY_ENTRY_SECURITY    4
00128 #define EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC   5
00129 #define EFI_IMAGE_DIRECTORY_ENTRY_DEBUG       6
00130 #define EFI_IMAGE_DIRECTORY_ENTRY_COPYRIGHT   7
00131 #define EFI_IMAGE_DIRECTORY_ENTRY_GLOBALPTR   8
00132 #define EFI_IMAGE_DIRECTORY_ENTRY_TLS         9
00133 #define EFI_IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10
00134 
00135 #define EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES 16
00136 
00137 ///
00138 /// @attention
00139 /// EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC means PE32 and
00140 /// EFI_IMAGE_OPTIONAL_HEADER32 must be used. The data structures only vary
00141 /// after NT additional fields.
00142 ///
00143 #define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC 0x10b
00144 
00145 ///
00146 /// Optional Header Standard Fields for PE32.
00147 ///
00148 typedef struct {
00149   ///
00150   /// Standard fields.
00151   ///
00152   UINT16                    Magic;
00153   UINT8                     MajorLinkerVersion;
00154   UINT8                     MinorLinkerVersion;
00155   UINT32                    SizeOfCode;
00156   UINT32                    SizeOfInitializedData;
00157   UINT32                    SizeOfUninitializedData;
00158   UINT32                    AddressOfEntryPoint;
00159   UINT32                    BaseOfCode;
00160   UINT32                    BaseOfData;  ///< PE32 contains this additional field, which is absent in PE32+.
00161   ///
00162   /// Optional Header Windows-Specific Fields.
00163   ///
00164   UINT32                    ImageBase;
00165   UINT32                    SectionAlignment;
00166   UINT32                    FileAlignment;
00167   UINT16                    MajorOperatingSystemVersion;
00168   UINT16                    MinorOperatingSystemVersion;
00169   UINT16                    MajorImageVersion;
00170   UINT16                    MinorImageVersion;
00171   UINT16                    MajorSubsystemVersion;
00172   UINT16                    MinorSubsystemVersion;
00173   UINT32                    Win32VersionValue;
00174   UINT32                    SizeOfImage;
00175   UINT32                    SizeOfHeaders;
00176   UINT32                    CheckSum;
00177   UINT16                    Subsystem;
00178   UINT16                    DllCharacteristics;
00179   UINT32                    SizeOfStackReserve;
00180   UINT32                    SizeOfStackCommit;
00181   UINT32                    SizeOfHeapReserve;
00182   UINT32                    SizeOfHeapCommit;
00183   UINT32                    LoaderFlags;
00184   UINT32                    NumberOfRvaAndSizes;
00185   EFI_IMAGE_DATA_DIRECTORY  DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES];
00186 } EFI_IMAGE_OPTIONAL_HEADER32;
00187 
00188 ///
00189 /// @attention
00190 /// EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC means PE32+ and
00191 /// EFI_IMAGE_OPTIONAL_HEADER64 must be used. The data structures only vary
00192 /// after NT additional fields.
00193 ///
00194 #define EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC 0x20b
00195 
00196 ///
00197 /// Optional Header Standard Fields for PE32+.
00198 ///
00199 typedef struct {
00200   ///
00201   /// Standard fields.
00202   ///
00203   UINT16                    Magic;
00204   UINT8                     MajorLinkerVersion;
00205   UINT8                     MinorLinkerVersion;
00206   UINT32                    SizeOfCode;
00207   UINT32                    SizeOfInitializedData;
00208   UINT32                    SizeOfUninitializedData;
00209   UINT32                    AddressOfEntryPoint;
00210   UINT32                    BaseOfCode;
00211   ///
00212   /// Optional Header Windows-Specific Fields.
00213   ///
00214   UINT64                    ImageBase;
00215   UINT32                    SectionAlignment;
00216   UINT32                    FileAlignment;
00217   UINT16                    MajorOperatingSystemVersion;
00218   UINT16                    MinorOperatingSystemVersion;
00219   UINT16                    MajorImageVersion;
00220   UINT16                    MinorImageVersion;
00221   UINT16                    MajorSubsystemVersion;
00222   UINT16                    MinorSubsystemVersion;
00223   UINT32                    Win32VersionValue;
00224   UINT32                    SizeOfImage;
00225   UINT32                    SizeOfHeaders;
00226   UINT32                    CheckSum;
00227   UINT16                    Subsystem;
00228   UINT16                    DllCharacteristics;
00229   UINT64                    SizeOfStackReserve;
00230   UINT64                    SizeOfStackCommit;
00231   UINT64                    SizeOfHeapReserve;
00232   UINT64                    SizeOfHeapCommit;
00233   UINT32                    LoaderFlags;
00234   UINT32                    NumberOfRvaAndSizes;
00235   EFI_IMAGE_DATA_DIRECTORY  DataDirectory[EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES];
00236 } EFI_IMAGE_OPTIONAL_HEADER64;
00237 
00238 
00239 ///
00240 /// @attention
00241 /// EFI_IMAGE_NT_HEADERS32 is for use ONLY by tools.
00242 ///
00243 typedef struct {
00244   UINT32                      Signature;
00245   EFI_IMAGE_FILE_HEADER       FileHeader;
00246   EFI_IMAGE_OPTIONAL_HEADER32 OptionalHeader;
00247 } EFI_IMAGE_NT_HEADERS32;
00248 
00249 #define EFI_IMAGE_SIZEOF_NT_OPTIONAL32_HEADER sizeof (EFI_IMAGE_NT_HEADERS32)
00250 
00251 ///
00252 /// @attention
00253 /// EFI_IMAGE_HEADERS64 is for use ONLY by tools.
00254 ///
00255 typedef struct {
00256   UINT32                      Signature;
00257   EFI_IMAGE_FILE_HEADER       FileHeader;
00258   EFI_IMAGE_OPTIONAL_HEADER64 OptionalHeader;
00259 } EFI_IMAGE_NT_HEADERS64;
00260 
00261 #define EFI_IMAGE_SIZEOF_NT_OPTIONAL64_HEADER sizeof (EFI_IMAGE_NT_HEADERS64)
00262 
00263 //
00264 // Other Windows Subsystem Values
00265 //
00266 #define EFI_IMAGE_SUBSYSTEM_UNKNOWN     0
00267 #define EFI_IMAGE_SUBSYSTEM_NATIVE      1
00268 #define EFI_IMAGE_SUBSYSTEM_WINDOWS_GUI 2
00269 #define EFI_IMAGE_SUBSYSTEM_WINDOWS_CUI 3
00270 #define EFI_IMAGE_SUBSYSTEM_OS2_CUI     5
00271 #define EFI_IMAGE_SUBSYSTEM_POSIX_CUI   7
00272 
00273 ///
00274 /// Length of ShortName.
00275 ///
00276 #define EFI_IMAGE_SIZEOF_SHORT_NAME 8
00277 
00278 ///
00279 /// Section Table. This table immediately follows the optional header.
00280 ///
00281 typedef struct {
00282   UINT8 Name[EFI_IMAGE_SIZEOF_SHORT_NAME];
00283   union {
00284     UINT32  PhysicalAddress;
00285     UINT32  VirtualSize;
00286   } Misc;
00287   UINT32  VirtualAddress;
00288   UINT32  SizeOfRawData;
00289   UINT32  PointerToRawData;
00290   UINT32  PointerToRelocations;
00291   UINT32  PointerToLinenumbers;
00292   UINT16  NumberOfRelocations;
00293   UINT16  NumberOfLinenumbers;
00294   UINT32  Characteristics;
00295 } EFI_IMAGE_SECTION_HEADER;
00296 
00297 ///
00298 /// Size of EFI_IMAGE_SECTION_HEADER.
00299 ///
00300 #define EFI_IMAGE_SIZEOF_SECTION_HEADER       40
00301 
00302 //
00303 // Section Flags Values
00304 //
00305 #define EFI_IMAGE_SCN_TYPE_NO_PAD                  BIT3   ///< 0x00000008  ///< Reserved.
00306 #define EFI_IMAGE_SCN_CNT_CODE                     BIT5   ///< 0x00000020
00307 #define EFI_IMAGE_SCN_CNT_INITIALIZED_DATA         BIT6   ///< 0x00000040
00308 #define EFI_IMAGE_SCN_CNT_UNINITIALIZED_DATA       BIT7   ///< 0x00000080
00309 
00310 #define EFI_IMAGE_SCN_LNK_OTHER                    BIT8   ///< 0x00000100  ///< Reserved.
00311 #define EFI_IMAGE_SCN_LNK_INFO                     BIT9   ///< 0x00000200  ///< Section contains comments or some other type of information.
00312 #define EFI_IMAGE_SCN_LNK_REMOVE                   BIT11  ///< 0x00000800  ///< Section contents will not become part of image.
00313 #define EFI_IMAGE_SCN_LNK_COMDAT                   BIT12  ///< 0x00001000
00314 
00315 #define EFI_IMAGE_SCN_ALIGN_1BYTES                 BIT20  ///< 0x00100000
00316 #define EFI_IMAGE_SCN_ALIGN_2BYTES                 BIT21  ///< 0x00200000
00317 #define EFI_IMAGE_SCN_ALIGN_4BYTES          (BIT20|BIT21) ///< 0x00300000
00318 #define EFI_IMAGE_SCN_ALIGN_8BYTES                 BIT22  ///< 0x00400000
00319 #define EFI_IMAGE_SCN_ALIGN_16BYTES         (BIT20|BIT22) ///< 0x00500000
00320 #define EFI_IMAGE_SCN_ALIGN_32BYTES         (BIT21|BIT22) ///< 0x00600000
00321 #define EFI_IMAGE_SCN_ALIGN_64BYTES   (BIT20|BIT21|BIT22) ///< 0x00700000
00322 
00323 #define EFI_IMAGE_SCN_MEM_DISCARDABLE              BIT25  ///< 0x02000000
00324 #define EFI_IMAGE_SCN_MEM_NOT_CACHED               BIT26  ///< 0x04000000
00325 #define EFI_IMAGE_SCN_MEM_NOT_PAGED                BIT27  ///< 0x08000000
00326 #define EFI_IMAGE_SCN_MEM_SHARED                   BIT28  ///< 0x10000000
00327 #define EFI_IMAGE_SCN_MEM_EXECUTE                  BIT29  ///< 0x20000000
00328 #define EFI_IMAGE_SCN_MEM_READ                     BIT30  ///< 0x40000000
00329 #define EFI_IMAGE_SCN_MEM_WRITE                    BIT31  ///< 0x80000000
00330 
00331 ///
00332 /// Size of a Symbol Table Record.
00333 ///
00334 #define EFI_IMAGE_SIZEOF_SYMBOL 18
00335 
00336 //
00337 // Symbols have a section number of the section in which they are
00338 // defined. Otherwise, section numbers have the following meanings:
00339 //
00340 #define EFI_IMAGE_SYM_UNDEFINED (UINT16) 0  ///< Symbol is undefined or is common.
00341 #define EFI_IMAGE_SYM_ABSOLUTE  (UINT16) -1 ///< Symbol is an absolute value.
00342 #define EFI_IMAGE_SYM_DEBUG     (UINT16) -2 ///< Symbol is a special debug item.
00343 
00344 //
00345 // Symbol Type (fundamental) values.
00346 //
00347 #define EFI_IMAGE_SYM_TYPE_NULL   0   ///< no type.
00348 #define EFI_IMAGE_SYM_TYPE_VOID   1   ///< no valid type.
00349 #define EFI_IMAGE_SYM_TYPE_CHAR   2   ///< type character.
00350 #define EFI_IMAGE_SYM_TYPE_SHORT  3   ///< type short integer.
00351 #define EFI_IMAGE_SYM_TYPE_INT    4
00352 #define EFI_IMAGE_SYM_TYPE_LONG   5
00353 #define EFI_IMAGE_SYM_TYPE_FLOAT  6
00354 #define EFI_IMAGE_SYM_TYPE_DOUBLE 7
00355 #define EFI_IMAGE_SYM_TYPE_STRUCT 8
00356 #define EFI_IMAGE_SYM_TYPE_UNION  9
00357 #define EFI_IMAGE_SYM_TYPE_ENUM   10  ///< enumeration.
00358 #define EFI_IMAGE_SYM_TYPE_MOE    11  ///< member of enumeration.
00359 #define EFI_IMAGE_SYM_TYPE_BYTE   12
00360 #define EFI_IMAGE_SYM_TYPE_WORD   13
00361 #define EFI_IMAGE_SYM_TYPE_UINT   14
00362 #define EFI_IMAGE_SYM_TYPE_DWORD  15
00363 
00364 //
00365 // Symbol Type (derived) values.
00366 //
00367 #define EFI_IMAGE_SYM_DTYPE_NULL      0 ///< no derived type.
00368 #define EFI_IMAGE_SYM_DTYPE_POINTER   1
00369 #define EFI_IMAGE_SYM_DTYPE_FUNCTION  2
00370 #define EFI_IMAGE_SYM_DTYPE_ARRAY     3
00371 
00372 //
00373 // Storage classes.
00374 //
00375 #define EFI_IMAGE_SYM_CLASS_END_OF_FUNCTION   ((UINT8) -1)
00376 #define EFI_IMAGE_SYM_CLASS_NULL              0
00377 #define EFI_IMAGE_SYM_CLASS_AUTOMATIC         1
00378 #define EFI_IMAGE_SYM_CLASS_EXTERNAL          2
00379 #define EFI_IMAGE_SYM_CLASS_STATIC            3
00380 #define EFI_IMAGE_SYM_CLASS_REGISTER          4
00381 #define EFI_IMAGE_SYM_CLASS_EXTERNAL_DEF      5
00382 #define EFI_IMAGE_SYM_CLASS_LABEL             6
00383 #define EFI_IMAGE_SYM_CLASS_UNDEFINED_LABEL   7
00384 #define EFI_IMAGE_SYM_CLASS_MEMBER_OF_STRUCT  8
00385 #define EFI_IMAGE_SYM_CLASS_ARGUMENT          9
00386 #define EFI_IMAGE_SYM_CLASS_STRUCT_TAG        10
00387 #define EFI_IMAGE_SYM_CLASS_MEMBER_OF_UNION   11
00388 #define EFI_IMAGE_SYM_CLASS_UNION_TAG         12
00389 #define EFI_IMAGE_SYM_CLASS_TYPE_DEFINITION   13
00390 #define EFI_IMAGE_SYM_CLASS_UNDEFINED_STATIC  14
00391 #define EFI_IMAGE_SYM_CLASS_ENUM_TAG          15
00392 #define EFI_IMAGE_SYM_CLASS_MEMBER_OF_ENUM    16
00393 #define EFI_IMAGE_SYM_CLASS_REGISTER_PARAM    17
00394 #define EFI_IMAGE_SYM_CLASS_BIT_FIELD         18
00395 #define EFI_IMAGE_SYM_CLASS_BLOCK             100
00396 #define EFI_IMAGE_SYM_CLASS_FUNCTION          101
00397 #define EFI_IMAGE_SYM_CLASS_END_OF_STRUCT     102
00398 #define EFI_IMAGE_SYM_CLASS_FILE              103
00399 #define EFI_IMAGE_SYM_CLASS_SECTION           104
00400 #define EFI_IMAGE_SYM_CLASS_WEAK_EXTERNAL     105
00401 
00402 //
00403 // type packing constants
00404 //
00405 #define EFI_IMAGE_N_BTMASK  017
00406 #define EFI_IMAGE_N_TMASK   060
00407 #define EFI_IMAGE_N_TMASK1  0300
00408 #define EFI_IMAGE_N_TMASK2  0360
00409 #define EFI_IMAGE_N_BTSHFT  4
00410 #define EFI_IMAGE_N_TSHIFT  2
00411 
00412 //
00413 // Communal selection types.
00414 //
00415 #define EFI_IMAGE_COMDAT_SELECT_NODUPLICATES    1
00416 #define EFI_IMAGE_COMDAT_SELECT_ANY             2
00417 #define EFI_IMAGE_COMDAT_SELECT_SAME_SIZE       3
00418 #define EFI_IMAGE_COMDAT_SELECT_EXACT_MATCH     4
00419 #define EFI_IMAGE_COMDAT_SELECT_ASSOCIATIVE     5
00420 
00421 //
00422 // the following values only be referred in PeCoff, not defined in PECOFF.
00423 //
00424 #define EFI_IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY  1
00425 #define EFI_IMAGE_WEAK_EXTERN_SEARCH_LIBRARY    2
00426 #define EFI_IMAGE_WEAK_EXTERN_SEARCH_ALIAS      3
00427 
00428 ///
00429 /// Relocation format.
00430 ///
00431 typedef struct {
00432   UINT32  VirtualAddress;
00433   UINT32  SymbolTableIndex;
00434   UINT16  Type;
00435 } EFI_IMAGE_RELOCATION;
00436 
00437 ///
00438 /// Size of EFI_IMAGE_RELOCATION
00439 ///
00440 #define EFI_IMAGE_SIZEOF_RELOCATION 10
00441 
00442 //
00443 // I386 relocation types.
00444 //
00445 #define EFI_IMAGE_REL_I386_ABSOLUTE 0x0000  ///< Reference is absolute, no relocation is necessary.
00446 #define EFI_IMAGE_REL_I386_DIR16    0x0001  ///< Direct 16-bit reference to the symbols virtual address.
00447 #define EFI_IMAGE_REL_I386_REL16    0x0002  ///< PC-relative 16-bit reference to the symbols virtual address.
00448 #define EFI_IMAGE_REL_I386_DIR32    0x0006  ///< Direct 32-bit reference to the symbols virtual address.
00449 #define EFI_IMAGE_REL_I386_DIR32NB  0x0007  ///< Direct 32-bit reference to the symbols virtual address, base not included.
00450 #define EFI_IMAGE_REL_I386_SEG12    0x0009  ///< Direct 16-bit reference to the segment-selector bits of a 32-bit virtual address.
00451 #define EFI_IMAGE_REL_I386_SECTION  0x000A
00452 #define EFI_IMAGE_REL_I386_SECREL   0x000B
00453 #define EFI_IMAGE_REL_I386_REL32    0x0014  ///< PC-relative 32-bit reference to the symbols virtual address.
00454 
00455 //
00456 // x64 processor relocation types.
00457 //
00458 #define IMAGE_REL_AMD64_ABSOLUTE  0x0000
00459 #define IMAGE_REL_AMD64_ADDR64    0x0001
00460 #define IMAGE_REL_AMD64_ADDR32    0x0002
00461 #define IMAGE_REL_AMD64_ADDR32NB  0x0003
00462 #define IMAGE_REL_AMD64_REL32     0x0004
00463 #define IMAGE_REL_AMD64_REL32_1   0x0005
00464 #define IMAGE_REL_AMD64_REL32_2   0x0006
00465 #define IMAGE_REL_AMD64_REL32_3   0x0007
00466 #define IMAGE_REL_AMD64_REL32_4   0x0008
00467 #define IMAGE_REL_AMD64_REL32_5   0x0009
00468 #define IMAGE_REL_AMD64_SECTION   0x000A
00469 #define IMAGE_REL_AMD64_SECREL    0x000B
00470 #define IMAGE_REL_AMD64_SECREL7   0x000C
00471 #define IMAGE_REL_AMD64_TOKEN     0x000D
00472 #define IMAGE_REL_AMD64_SREL32    0x000E
00473 #define IMAGE_REL_AMD64_PAIR      0x000F
00474 #define IMAGE_REL_AMD64_SSPAN32   0x0010
00475 
00476 ///
00477 /// Based relocation format.
00478 ///
00479 typedef struct {
00480   UINT32  VirtualAddress;
00481   UINT32  SizeOfBlock;
00482 } EFI_IMAGE_BASE_RELOCATION;
00483 
00484 ///
00485 /// Size of EFI_IMAGE_BASE_RELOCATION.
00486 ///
00487 #define EFI_IMAGE_SIZEOF_BASE_RELOCATION  8
00488 
00489 //
00490 // Based relocation types.
00491 //
00492 #define EFI_IMAGE_REL_BASED_ABSOLUTE        0
00493 #define EFI_IMAGE_REL_BASED_HIGH            1
00494 #define EFI_IMAGE_REL_BASED_LOW             2
00495 #define EFI_IMAGE_REL_BASED_HIGHLOW         3
00496 #define EFI_IMAGE_REL_BASED_HIGHADJ         4
00497 #define EFI_IMAGE_REL_BASED_MIPS_JMPADDR    5
00498 #define EFI_IMAGE_REL_BASED_ARM_MOV32A      5
00499 #define EFI_IMAGE_REL_BASED_ARM_MOV32T      7
00500 #define EFI_IMAGE_REL_BASED_IA64_IMM64      9
00501 #define EFI_IMAGE_REL_BASED_MIPS_JMPADDR16  9
00502 #define EFI_IMAGE_REL_BASED_DIR64           10
00503 
00504 ///
00505 /// Line number format.
00506 ///
00507 typedef struct {
00508   union {
00509     UINT32  SymbolTableIndex; ///< Symbol table index of function name if Linenumber is 0.
00510     UINT32  VirtualAddress;   ///< Virtual address of line number.
00511   } Type;
00512   UINT16  Linenumber;         ///< Line number.
00513 } EFI_IMAGE_LINENUMBER;
00514 
00515 ///
00516 /// Size of EFI_IMAGE_LINENUMBER.
00517 ///
00518 #define EFI_IMAGE_SIZEOF_LINENUMBER 6
00519 
00520 //
00521 // Archive format.
00522 //
00523 #define EFI_IMAGE_ARCHIVE_START_SIZE        8
00524 #define EFI_IMAGE_ARCHIVE_START             "!<arch>\n"
00525 #define EFI_IMAGE_ARCHIVE_END               "`\n"
00526 #define EFI_IMAGE_ARCHIVE_PAD               "\n"
00527 #define EFI_IMAGE_ARCHIVE_LINKER_MEMBER     "/               "
00528 #define EFI_IMAGE_ARCHIVE_LONGNAMES_MEMBER  "//              "
00529 
00530 ///
00531 /// Archive Member Headers
00532 ///
00533 typedef struct {
00534   UINT8 Name[16];     ///< File member name - `/' terminated.
00535   UINT8 Date[12];     ///< File member date - decimal.
00536   UINT8 UserID[6];    ///< File member user id - decimal.
00537   UINT8 GroupID[6];   ///< File member group id - decimal.
00538   UINT8 Mode[8];      ///< File member mode - octal.
00539   UINT8 Size[10];     ///< File member size - decimal.
00540   UINT8 EndHeader[2]; ///< String to end header. (0x60 0x0A).
00541 } EFI_IMAGE_ARCHIVE_MEMBER_HEADER;
00542 
00543 ///
00544 /// Size of EFI_IMAGE_ARCHIVE_MEMBER_HEADER.
00545 ///
00546 #define EFI_IMAGE_SIZEOF_ARCHIVE_MEMBER_HDR 60
00547 
00548 
00549 //
00550 // DLL Support
00551 //
00552 
00553 ///
00554 /// Export Directory Table.
00555 ///
00556 typedef struct {
00557   UINT32  Characteristics;
00558   UINT32  TimeDateStamp;
00559   UINT16  MajorVersion;
00560   UINT16  MinorVersion;
00561   UINT32  Name;
00562   UINT32  Base;
00563   UINT32  NumberOfFunctions;
00564   UINT32  NumberOfNames;
00565   UINT32  AddressOfFunctions;
00566   UINT32  AddressOfNames;
00567   UINT32  AddressOfNameOrdinals;
00568 } EFI_IMAGE_EXPORT_DIRECTORY;
00569 
00570 ///
00571 /// Hint/Name Table.
00572 ///
00573 typedef struct {
00574   UINT16  Hint;
00575   UINT8   Name[1];
00576 } EFI_IMAGE_IMPORT_BY_NAME;
00577 
00578 ///
00579 /// Import Address Table RVA (Thunk Table).
00580 ///
00581 typedef struct {
00582   union {
00583     UINT32                    Function;
00584     UINT32                    Ordinal;
00585     EFI_IMAGE_IMPORT_BY_NAME  *AddressOfData;
00586   } u1;
00587 } EFI_IMAGE_THUNK_DATA;
00588 
00589 #define EFI_IMAGE_ORDINAL_FLAG              BIT31    ///< Flag for PE32.
00590 #define EFI_IMAGE_SNAP_BY_ORDINAL(Ordinal)  ((Ordinal & EFI_IMAGE_ORDINAL_FLAG) != 0)
00591 #define EFI_IMAGE_ORDINAL(Ordinal)          (Ordinal & 0xffff)
00592 
00593 ///
00594 /// Import Directory Table
00595 ///
00596 typedef struct {
00597   UINT32                Characteristics;
00598   UINT32                TimeDateStamp;
00599   UINT32                ForwarderChain;
00600   UINT32                Name;
00601   EFI_IMAGE_THUNK_DATA  *FirstThunk;
00602 } EFI_IMAGE_IMPORT_DESCRIPTOR;
00603 
00604 
00605 ///
00606 /// Debug Directory Format.
00607 ///
00608 typedef struct {
00609   UINT32  Characteristics;
00610   UINT32  TimeDateStamp;
00611   UINT16  MajorVersion;
00612   UINT16  MinorVersion;
00613   UINT32  Type;
00614   UINT32  SizeOfData;
00615   UINT32  RVA;           ///< The address of the debug data when loaded, relative to the image base.
00616   UINT32  FileOffset;    ///< The file pointer to the debug data.
00617 } EFI_IMAGE_DEBUG_DIRECTORY_ENTRY;
00618 
00619 #define EFI_IMAGE_DEBUG_TYPE_CODEVIEW 2     ///< The Visual C++ debug information.
00620 
00621 ///
00622 /// Debug Data Structure defined in Microsoft C++.
00623 ///
00624 #define CODEVIEW_SIGNATURE_NB10  SIGNATURE_32('N', 'B', '1', '0')
00625 typedef struct {
00626   UINT32  Signature;                        ///< "NB10"
00627   UINT32  Unknown;
00628   UINT32  Unknown2;
00629   UINT32  Unknown3;
00630   //
00631   // Filename of .PDB goes here
00632   //
00633 } EFI_IMAGE_DEBUG_CODEVIEW_NB10_ENTRY;
00634 
00635 ///
00636 /// Debug Data Structure defined in Microsoft C++.
00637 ///
00638 #define CODEVIEW_SIGNATURE_RSDS  SIGNATURE_32('R', 'S', 'D', 'S')
00639 typedef struct {
00640   UINT32  Signature;                        ///< "RSDS".
00641   UINT32  Unknown;
00642   UINT32  Unknown2;
00643   UINT32  Unknown3;
00644   UINT32  Unknown4;
00645   UINT32  Unknown5;
00646   //
00647   // Filename of .PDB goes here
00648   //
00649 } EFI_IMAGE_DEBUG_CODEVIEW_RSDS_ENTRY;
00650 
00651 
00652 ///
00653 /// Debug Data Structure defined by Apple Mach-O to Coff utility.
00654 ///
00655 #define CODEVIEW_SIGNATURE_MTOC  SIGNATURE_32('M', 'T', 'O', 'C')
00656 typedef struct {
00657   UINT32    Signature;                       ///< "MTOC".
00658   GUID      MachOUuid;
00659   //
00660   //  Filename of .DLL (Mach-O with debug info) goes here
00661   //
00662 } EFI_IMAGE_DEBUG_CODEVIEW_MTOC_ENTRY;
00663 
00664 ///
00665 /// Resource format.
00666 ///
00667 typedef struct {
00668   UINT32  Characteristics;
00669   UINT32  TimeDateStamp;
00670   UINT16  MajorVersion;
00671   UINT16  MinorVersion;
00672   UINT16  NumberOfNamedEntries;
00673   UINT16  NumberOfIdEntries;
00674   //
00675   // Array of EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY entries goes here.
00676   //
00677 } EFI_IMAGE_RESOURCE_DIRECTORY;
00678 
00679 ///
00680 /// Resource directory entry format.
00681 ///
00682 typedef struct {
00683   union {
00684     struct {
00685       UINT32  NameOffset:31;
00686       UINT32  NameIsString:1;
00687     } s;
00688     UINT32  Id;
00689   } u1;
00690   union {
00691     UINT32  OffsetToData;
00692     struct {
00693       UINT32  OffsetToDirectory:31;
00694       UINT32  DataIsDirectory:1;
00695     } s;
00696   } u2;
00697 } EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY;
00698 
00699 ///
00700 /// Resource directory entry for string.
00701 ///
00702 typedef struct {
00703   UINT16  Length;
00704   CHAR16  String[1];
00705 } EFI_IMAGE_RESOURCE_DIRECTORY_STRING;
00706 
00707 ///
00708 /// Resource directory entry for data array.
00709 ///
00710 typedef struct {
00711   UINT32  OffsetToData;
00712   UINT32  Size;
00713   UINT32  CodePage;
00714   UINT32  Reserved;
00715 } EFI_IMAGE_RESOURCE_DATA_ENTRY;
00716 
00717 ///
00718 /// Header format for TE images, defined in the PI Specification, 1.0.
00719 ///
00720 typedef struct {
00721   UINT16                    Signature;            ///< The signature for TE format = "VZ".
00722   UINT16                    Machine;              ///< From the original file header.
00723   UINT8                     NumberOfSections;     ///< From the original file header.
00724   UINT8                     Subsystem;            ///< From original optional header.
00725   UINT16                    StrippedSize;         ///< Number of bytes we removed from the header.
00726   UINT32                    AddressOfEntryPoint;  ///< Offset to entry point -- from original optional header.
00727   UINT32                    BaseOfCode;           ///< From original image -- required for ITP debug.
00728   UINT64                    ImageBase;            ///< From original file header.
00729   EFI_IMAGE_DATA_DIRECTORY  DataDirectory[2];     ///< Only base relocation and debug directory.
00730 } EFI_TE_IMAGE_HEADER;
00731 
00732 
00733 #define EFI_TE_IMAGE_HEADER_SIGNATURE  SIGNATURE_16('V', 'Z')
00734 
00735 //
00736 // Data directory indexes in our TE image header
00737 //
00738 #define EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC  0
00739 #define EFI_TE_IMAGE_DIRECTORY_ENTRY_DEBUG      1
00740 
00741 
00742 ///
00743 /// Union of PE32, PE32+, and TE headers.
00744 ///
00745 typedef union {
00746   EFI_IMAGE_NT_HEADERS32   Pe32;
00747   EFI_IMAGE_NT_HEADERS64   Pe32Plus;
00748   EFI_TE_IMAGE_HEADER      Te;
00749 } EFI_IMAGE_OPTIONAL_HEADER_UNION;
00750 
00751 typedef union {
00752   EFI_IMAGE_NT_HEADERS32            *Pe32;
00753   EFI_IMAGE_NT_HEADERS64            *Pe32Plus;
00754   EFI_TE_IMAGE_HEADER               *Te;
00755   EFI_IMAGE_OPTIONAL_HEADER_UNION   *Union;
00756 } EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION;
00757 
00758 #endif