]> 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 ad9c6277270642e04aca2fa1c0f78b796c1d0402..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
-\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
-\r
-  @param  Machine   Machine type from the PE Header.\r
-\r
-  @return TRUE if this PE/COFF loader can load the image\r
-\r
-**/\r
-BOOLEAN\r
-PeCoffLoaderImageFormatSupported (\r
-  IN  UINT16  Machine\r
-  );\r
 \r
+#include "BasePeCoffLibInternals.h"\r
 \r
 /**\r
   Retrieves the magic value from the PE/COFF header.\r
@@ -299,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
@@ -327,6 +269,8 @@ 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
@@ -391,7 +335,7 @@ PeCoffLoaderGetImageInfo (
       }\r
 \r
       if (DebugDirectoryEntryFileOffset != 0) {\r
-        for (Index = 0; Index < DebugDirectoryEntry->Size; Index += Size) {\r
+        for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r
           //\r
           // Read next debug directory entry\r
           //\r
@@ -479,7 +423,7 @@ PeCoffLoaderGetImageInfo (
     }\r
 \r
     if (DebugDirectoryEntryFileOffset != 0) {\r
-      for (Index = 0; Index < DebugDirectoryEntry->Size; Index += Size) {\r
+      for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) {\r
         //\r
         // Read next debug directory entry\r
         //\r
@@ -648,8 +592,8 @@ 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
@@ -762,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
@@ -1335,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
@@ -1387,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