]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
remove unnecessary comments introduced by tools from MdePkg. The regular express...
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / BasePeCoff.c
index eee4dc713fe746684f080a2ae07eab64be272f9f..ea45ff745df2420d305bd3ea9514fe9062e511e4 100644 (file)
   THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
   WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
 \r
-  Module Name:  PeCoffLoader.c\r
-\r
-**/\r
-\r
-\r
-/**\r
-  Performs an Itanium-based specific relocation fixup and is a no-op on other\r
-  instruction sets.\r
-\r
-  @param  Reloc       Pointer to the relocation record.\r
-  @param  Fixup       Pointer to the address to fix up.\r
-  @param  FixupData   Pointer to a buffer to log the fixups.\r
-  @param  Adjust      The offset to adjust the fixup.\r
-\r
-  @return Status code.\r
-\r
 **/\r
-RETURN_STATUS\r
-PeCoffLoaderRelocateImageEx (\r
-  IN UINT16      *Reloc,\r
-  IN OUT CHAR8   *Fixup,\r
-  IN OUT CHAR8   **FixupData,\r
-  IN UINT64      Adjust\r
-  );\r
-\r
-\r
-/**\r
-  Performs an Itanium-based specific re-relocation fixup and is a no-op on other\r
-  instruction sets. This is used to re-relocated the image into the EFI virtual\r
-  space for runtime calls.\r
 \r
-  @param  Reloc       Pointer to the relocation record.\r
-  @param  Fixup       Pointer to the address to fix up.\r
-  @param  FixupData   Pointer to a buffer to log the fixups.\r
-  @param  Adjust      The offset to adjust the fixup.\r
 \r
-  @return Status code.\r
 \r
-**/\r
-RETURN_STATUS\r
-PeHotRelocateImageEx (\r
-  IN UINT16      *Reloc,\r
-  IN OUT CHAR8   *Fixup,\r
-  IN OUT CHAR8   **FixupData,\r
-  IN UINT64      Adjust\r
-  );\r
 \r
+#include "BasePeCoffLibInternals.h"\r
 \r
 /**\r
-  Returns TRUE if the machine type of PE/COFF image is supported. Supported \r
-  does not mean the image can be executed it means the PE/COFF loader supports\r
-  loading and relocating of the image type. It's up to the caller to support\r
-  the entry point. \r
+  Retrieves the magic value from the PE/COFF header.\r
 \r
-  @param  Machine   Machine type from the PE Header.\r
+  @param  Hdr             The buffer in which to return the PE32, PE32+, or TE header.\r
 \r
-  @return TRUE if this PE/COFF loader can load the image\r
+  @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32\r
+  @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+\r
 \r
 **/\r
-BOOLEAN\r
-PeCoffLoaderImageFormatSupported (\r
-  IN  UINT16  Machine\r
-  );\r
-\r
+UINT16\r
+PeCoffLoaderGetPeHeaderMagicValue (\r
+  IN  EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION  Hdr\r
+  )\r
+{\r
+  //\r
+  // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value \r
+  //       in the PE/COFF Header.  If the MachineType is Itanium(IA64) and the \r
+  //       Magic value in the OptionalHeader is  EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC\r
+  //       then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC\r
+  //\r
+  if (Hdr.Pe32->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC;\r
+  }\r
+  //\r
+  // Return the magic value from the PC/COFF Optional Header\r
+  //\r
+  return Hdr.Pe32->OptionalHeader.Magic;\r
+}\r
 \r
 \r
 /**\r
@@ -98,6 +69,7 @@ PeCoffLoaderGetPeHeader (
   RETURN_STATUS         Status;\r
   EFI_IMAGE_DOS_HEADER  DosHdr;\r
   UINTN                 Size;\r
+  UINT16                Magic;\r
 \r
   //\r
   // Read the DOS image header to check for it's existance\r
@@ -117,16 +89,16 @@ PeCoffLoaderGetPeHeader (
   ImageContext->PeCoffHeaderOffset = 0;\r
   if (DosHdr.e_magic == EFI_IMAGE_DOS_SIGNATURE) {\r
     //\r
-    // DOS image header is present, so read the PE header after the DOS image \r
+    // DOS image header is present, so read the PE header after the DOS image\r
     // header\r
     //\r
     ImageContext->PeCoffHeaderOffset = DosHdr.e_lfanew;\r
   }\r
 \r
   //\r
-  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much \r
+  // Read the PE/COFF Header. For PE32 (32-bit) this will read in too much\r
   // data, but that should not hurt anythine. Hdr.Pe32->OptionalHeader.Magic\r
-  // determins if this is a PE32 or PE32+ image. The magic is in the same \r
+  // determins if this is a PE32 or PE32+ image. The magic is in the same\r
   // location in both images.\r
   //\r
   Size = sizeof (EFI_IMAGE_OPTIONAL_HEADER_UNION);\r
@@ -155,8 +127,10 @@ PeCoffLoaderGetPeHeader (
   } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE)  {\r
     ImageContext->IsTeImage = FALSE;\r
     ImageContext->Machine = Hdr.Pe32->FileHeader.Machine;\r
-    \r
-    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+\r
+    Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r
+\r
+    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
       //\r
@@ -165,7 +139,7 @@ PeCoffLoaderGetPeHeader (
       ImageContext->SectionAlignment  = Hdr.Pe32->OptionalHeader.SectionAlignment;\r
       ImageContext->SizeOfHeaders     = Hdr.Pe32->OptionalHeader.SizeOfHeaders;\r
 \r
-    } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
+    } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) {\r
       //\r
       // Use PE32+ offset\r
       //\r
@@ -175,7 +149,7 @@ PeCoffLoaderGetPeHeader (
       ImageContext->SizeOfHeaders     = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;\r
     } else {\r
       ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;\r
-      return RETURN_UNSUPPORTED;    \r
+      return RETURN_UNSUPPORTED;\r
     }\r
   } else {\r
     ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;\r
@@ -187,7 +161,7 @@ PeCoffLoaderGetPeHeader (
     // If the PE/COFF loader does not support the image type return\r
     // unsupported. This library can suport lots of types of images\r
     // this does not mean the user of this library can call the entry\r
-    // point of the image. \r
+    // point of the image.\r
     //\r
     return RETURN_UNSUPPORTED;\r
   }\r
@@ -205,7 +179,7 @@ PeCoffLoaderGetPeHeader (
   If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not\r
   a supported PE/COFF image type, then return RETURN_UNSUPPORTED.  If any errors occur while\r
   computing the fields of ImageContext, then the error status is returned in the ImageError field of\r
-  ImageContext. \r
+  ImageContext.\r
 \r
   @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
                                     image that needs to be examined by this function.\r
@@ -233,6 +207,7 @@ PeCoffLoaderGetImageInfo (
   EFI_IMAGE_SECTION_HEADER              SectionHeader;\r
   EFI_IMAGE_DEBUG_DIRECTORY_ENTRY       DebugEntry;\r
   UINT32                                NumberOfRvaAndSizes;\r
+  UINT16                                Magic;\r
 \r
   if (NULL == ImageContext) {\r
     return RETURN_INVALID_PARAMETER;\r
@@ -248,11 +223,13 @@ PeCoffLoaderGetImageInfo (
     return Status;\r
   }\r
 \r
+  Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r
+\r
   //\r
   // Retrieve the base address of the image\r
   //\r
   if (!(ImageContext->IsTeImage)) {\r
-    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
       //\r
@@ -264,7 +241,7 @@ PeCoffLoaderGetImageInfo (
       ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase;\r
     }\r
   } else {\r
-    ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase);\r
+    ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER));\r
   }\r
 \r
   //\r
@@ -292,25 +269,27 @@ PeCoffLoaderGetImageInfo (
   //\r
   if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {\r
     ImageContext->RelocationsStripped = TRUE;\r
+  } else if ((ImageContext->IsTeImage) && (Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) {\r
+    ImageContext->RelocationsStripped = TRUE;\r
   } else {\r
     ImageContext->RelocationsStripped = FALSE;\r
   }\r
 \r
   if (!(ImageContext->IsTeImage)) {\r
-    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
-      //     \r
+    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+      //\r
       // Use PE32 offset\r
       //\r
       NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
       DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
     } else {\r
-      //     \r
+      //\r
       // Use PE32+ offset\r
       //\r
       NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
       DebugDirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);\r
-    }    \r
-    \r
+    }\r
+\r
     if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {\r
 \r
       DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;\r
@@ -324,8 +303,8 @@ PeCoffLoaderGetImageInfo (
 \r
       SectionHeaderOffset = (UINTN)(\r
                                ImageContext->PeCoffHeaderOffset +\r
-                               sizeof (UINT32) + \r
-                               sizeof (EFI_IMAGE_FILE_HEADER) + \r
+                               sizeof (UINT32) +\r
+                               sizeof (EFI_IMAGE_FILE_HEADER) +\r
                                Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r
                                );\r
 \r
@@ -356,7 +335,7 @@ PeCoffLoaderGetImageInfo (
       }\r
 \r
       if (DebugDirectoryEntryFileOffset != 0) {\r
-        for (Index = 0; Index < DebugDirectoryEntry->Size; Index++) {\r
+        for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r
           //\r
           // Read next debug directory entry\r
           //\r
@@ -371,9 +350,8 @@ PeCoffLoaderGetImageInfo (
             ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;\r
             return Status;\r
           }\r
-\r
           if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r
-            ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY));\r
+            ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);\r
             if (DebugEntry.RVA == 0 && DebugEntry.FileOffset != 0) {\r
               ImageContext->ImageSize += DebugEntry.SizeOfData;\r
             }\r
@@ -428,8 +406,8 @@ PeCoffLoaderGetImageInfo (
 \r
       //\r
       // In Te image header there is not a field to describe the ImageSize.\r
-      // Actually, the ImageSize equals the RVA plus the VirtualSize of \r
-      // the last section mapped into memory (Must be rounded up to \r
+      // Actually, the ImageSize equals the RVA plus the VirtualSize of\r
+      // the last section mapped into memory (Must be rounded up to\r
       // a mulitple of Section Alignment). Per the PE/COFF specification, the\r
       // section headers in the Section Table must appear in order of the RVA\r
       // values for the corresponding sections. So the ImageSize can be determined\r
@@ -445,7 +423,7 @@ PeCoffLoaderGetImageInfo (
     }\r
 \r
     if (DebugDirectoryEntryFileOffset != 0) {\r
-      for (Index = 0; Index < DebugDirectoryEntry->Size; Index++) {\r
+      for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r
         //\r
         // Read next debug directory entry\r
         //\r
@@ -462,7 +440,7 @@ PeCoffLoaderGetImageInfo (
         }\r
 \r
         if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) {\r
-          ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY));\r
+          ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index);\r
           return RETURN_SUCCESS;\r
         }\r
       }\r
@@ -489,7 +467,7 @@ PeCoffLoaderImageAddress (
   )\r
 {\r
   //\r
-  // @bug Check to make sure ImageSize is correct for the relocated image. \r
+  // @bug Check to make sure ImageSize is correct for the relocated image.\r
   //      it may only work for the file we start with and not the relocated image\r
   //\r
   if (Address >= ImageContext->ImageSize) {\r
@@ -506,7 +484,7 @@ PeCoffLoaderImageAddress (
   If the DestinationAddress field of ImageContext is 0, then use the ImageAddress field of\r
   ImageContext as the relocation base address.  Otherwise, use the DestinationAddress field\r
   of ImageContext as the relocation base address.  The caller must allocate the relocation\r
-  fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.  \r
+  fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function.\r
   If ImageContext is NULL, then ASSERT().\r
 \r
   @param  ImageContext        Pointer to the image context structure that describes the PE/COFF\r
@@ -537,11 +515,12 @@ PeCoffLoaderRelocateImage (
   CHAR8                                 *Fixup;\r
   CHAR8                                 *FixupBase;\r
   UINT16                                *F16;\r
-  UINT32                                *F32;  \r
+  UINT32                                *F32;\r
   UINT64                                *F64;\r
   CHAR8                                 *FixupData;\r
   PHYSICAL_ADDRESS                      BaseAddress;\r
   UINT32                                NumberOfRvaAndSizes;\r
+  UINT16                                Magic;\r
 \r
   ASSERT (ImageContext != NULL);\r
 \r
@@ -569,13 +548,16 @@ PeCoffLoaderRelocateImage (
 \r
   if (!(ImageContext->IsTeImage)) {\r
     Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);\r
-    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+\r
+    Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r
+\r
+    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
       //\r
       Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase;\r
       Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;\r
-  \r
+\r
       NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
       RelocDir  = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r
     } else {\r
@@ -610,22 +592,22 @@ PeCoffLoaderRelocateImage (
     }\r
   } else {\r
     Hdr.Te             = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);\r
-    Adjust             = (UINT64) (BaseAddress - Hdr.Te->ImageBase);\r
-    Hdr.Te->ImageBase  = (UINT64) (BaseAddress);\r
+    Adjust             = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->ImageBase);\r
+    Hdr.Te->ImageBase  = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER));\r
 \r
     //\r
     // Find the relocation block\r
     //\r
     RelocDir = &Hdr.Te->DataDirectory[0];\r
     RelocBase = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(\r
-                                    ImageContext->ImageAddress + \r
+                                    ImageContext->ImageAddress +\r
                                     RelocDir->VirtualAddress +\r
-                                    sizeof(EFI_TE_IMAGE_HEADER) - \r
+                                    sizeof(EFI_TE_IMAGE_HEADER) -\r
                                     Hdr.Te->StrippedSize\r
                                     );\r
     RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) ((UINTN) RelocBase + (UINTN) RelocDir->Size - 1);\r
   }\r
-  \r
+\r
   //\r
   // Run the relocation information and apply the fixups\r
   //\r
@@ -639,13 +621,13 @@ PeCoffLoaderRelocateImage (
     } else {\r
       FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +\r
                     RelocBase->VirtualAddress +\r
-                    sizeof(EFI_TE_IMAGE_HEADER) - \r
+                    sizeof(EFI_TE_IMAGE_HEADER) -\r
                     Hdr.Te->StrippedSize\r
                     );\r
     }\r
 \r
     if ((CHAR8 *) RelocEnd < (CHAR8 *) ((UINTN) ImageContext->ImageAddress) ||\r
-        (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress + \r
+        (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress +\r
           (UINTN)ImageContext->ImageSize)) {\r
       ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
       return RETURN_LOAD_ERROR;\r
@@ -663,7 +645,7 @@ PeCoffLoaderRelocateImage (
 \r
       case EFI_IMAGE_REL_BASED_HIGH:\r
         F16   = (UINT16 *) Fixup;\r
-        *F16  = (UINT16) ((*F16 << 16) + (UINT16) Adjust);\r
+        *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
         if (FixupData != NULL) {\r
           *(UINT16 *) FixupData = *F16;\r
           FixupData             = FixupData + sizeof (UINT16);\r
@@ -724,6 +706,13 @@ PeCoffLoaderRelocateImage (
     RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r
   }\r
 \r
+  //\r
+  // Adjust the EntryPoint to match the linked-to address\r
+  //\r
+  if (ImageContext->DestinationAddress != 0) {\r
+     ImageContext->EntryPoint -= (UINT64) ImageContext->ImageAddress;\r
+     ImageContext->EntryPoint += (UINT64) ImageContext->DestinationAddress;\r
+  }\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -771,6 +760,7 @@ PeCoffLoaderLoadImage (
   UINTN                                 Size;\r
   UINT32                                TempDebugEntryRva;\r
   UINT32                                NumberOfRvaAndSizes;\r
+  UINT16                                Magic;\r
 \r
   ASSERT (ImageContext != NULL);\r
 \r
@@ -852,8 +842,8 @@ PeCoffLoaderLoadImage (
     FirstSection = (EFI_IMAGE_SECTION_HEADER *) (\r
                       (UINTN)ImageContext->ImageAddress +\r
                       ImageContext->PeCoffHeaderOffset +\r
-                      sizeof(UINT32) + \r
-                      sizeof(EFI_IMAGE_FILE_HEADER) + \r
+                      sizeof(UINT32) +\r
+                      sizeof(EFI_IMAGE_FILE_HEADER) +\r
                       Hdr.Pe32->FileHeader.SizeOfOptionalHeader\r
       );\r
     NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);\r
@@ -958,14 +948,15 @@ PeCoffLoaderLoadImage (
   //\r
   // Get image's entry point\r
   //\r
+  Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r
   if (!(ImageContext->IsTeImage)) {\r
     //\r
     // Sizes of AddressOfEntryPoint are different so we need to do this safely\r
     //\r
-    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
-      //      \r
+      //\r
       ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (\r
                                                             ImageContext,\r
                                                             (UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint\r
@@ -996,7 +987,7 @@ PeCoffLoaderLoadImage (
   // the optional header to verify a desired directory entry is there.\r
   //\r
   if (!(ImageContext->IsTeImage)) {\r
-    if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
       //\r
       // Use PE32 offset\r
       //\r
@@ -1009,7 +1000,7 @@ PeCoffLoaderLoadImage (
       NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
       DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r
     }\r
\r
+\r
     if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r
       ImageContext->FixupDataSize = DirectoryEntry->Size / sizeof (UINT16) * sizeof (UINTN);\r
     } else {\r
@@ -1124,8 +1115,8 @@ PeCoffLoaderLoadImage (
 \r
 /**\r
   Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
-  runtime. \r
-  \r
+  runtime.\r
+\r
   PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply\r
   the fixups with a virtual mapping.\r
 \r
@@ -1134,7 +1125,7 @@ PeCoffLoaderLoadImage (
   @param  VirtImageBase      Virtual mapping for ImageBase\r
   @param  ImageSize          Size of the image to relocate\r
   @param  RelocationData     Location to place results of read\r
-  \r
+\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1164,6 +1155,7 @@ PeCoffLoaderRelocateImageForRuntime (
   CHAR8                               *FixupData;\r
   UINTN                               Adjust;\r
   RETURN_STATUS                       Status;\r
+  UINT16                              Magic;\r
 \r
   OldBase = (CHAR8 *)((UINTN)ImageBase);\r
   NewBase = (CHAR8 *)((UINTN)VirtImageBase);\r
@@ -1192,22 +1184,21 @@ PeCoffLoaderRelocateImageForRuntime (
     return ;\r
   }\r
 \r
-  //\r
-  // Get some data from the PE type dependent data\r
-  //\r
-  if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
-    //     \r
+  Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr);\r
+\r
+  if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
+    //\r
     // Use PE32 offset\r
     //\r
     NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;\r
     DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[0]);\r
   } else {\r
-    //     \r
+    //\r
     // Use PE32+ offset\r
     //\r
     NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
     DataDirectory = (EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[0]);\r
-  }    \r
+  }\r
 \r
   //\r
   // Find the relocation block\r
@@ -1215,7 +1206,7 @@ PeCoffLoaderRelocateImageForRuntime (
   // Per the PE/COFF spec, you can't assume that a given data directory\r
   // is present in the image. You have to check the NumberOfRvaAndSizes in\r
   // the optional header to verify a desired directory entry is there.\r
-  //    \r
+  //\r
   if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {\r
     RelocDir      = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;\r
     RelocBase     = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress);\r
@@ -1258,7 +1249,7 @@ PeCoffLoaderRelocateImageForRuntime (
       case EFI_IMAGE_REL_BASED_HIGH:\r
         F16 = (UINT16 *) Fixup;\r
         if (*(UINT16 *) FixupData == *F16) {\r
-          *F16 = (UINT16) (*F16 + ((UINT16)(Adjust >> 16)));\r
+          *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
         }\r
 \r
         FixupData = FixupData + sizeof (UINT16);\r
@@ -1295,7 +1286,7 @@ PeCoffLoaderRelocateImageForRuntime (
 \r
       case EFI_IMAGE_REL_BASED_HIGHADJ:\r
         //\r
-        // Not implemented, but not used in EFI 1.0\r
+        // Not implemented, but not used in UEFI 2.0\r
         //\r
         ASSERT (FALSE);\r
         break;\r
@@ -1324,14 +1315,14 @@ PeCoffLoaderRelocateImageForRuntime (
 \r
 /**\r
   ImageRead function that operates on a memory buffer whos base is passed into\r
-  FileHandle. \r
+  FileHandle.\r
 \r
   @param  FileHandle        Ponter to baes of the input stream\r
   @param  FileOffset        Offset to the start of the buffer\r
   @param  ReadSize          Number of bytes to copy into the buffer\r
   @param  Buffer            Location to place results of read\r
 \r
-  @retval RETURN_SUCCESS    Data is read from FileOffset from the Handle into \r
+  @retval RETURN_SUCCESS    Data is read from FileOffset from the Handle into\r
                             the buffer.\r
 **/\r
 RETURN_STATUS\r
@@ -1347,3 +1338,23 @@ PeCoffLoaderImageReadFromMemory (
   return RETURN_SUCCESS;\r
 }\r
 \r
+/**\r
+  Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
+   \r
+  For NT32 emulator, the PE/COFF image loaded by system needs to release.\r
+  For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, \r
+  this function can simply return RETURN_SUCCESS.\r
+\r
+  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
+                                    image to be unloaded.\r
+\r
+  @retval RETURN_SUCCESS            The PE/COFF image was unloaded successfully.\r
+**/\r
+RETURN_STATUS\r
+EFIAPI\r
+PeCoffLoaderUnloadImage (\r
+  IN PE_COFF_LOADER_IMAGE_CONTEXT  *ImageContext\r
+  )\r
+{\r
+  return RETURN_SUCCESS;\r
+}\r