]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
Minor grammatical work--mostly adding periods. Items with ONLY period added did...
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / BasePeCoff.c
index 197e1dba0080f84d57b8ec5f37793bca60610e16..7337bc359a0aecea2f416e3993b2d87cfa6f1c04 100644 (file)
@@ -2,12 +2,12 @@
   Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but\r
   only supports relocating IA32, x64, IPF, and EBC images.\r
 \r
-  Copyright (c) 2006 - 2008, Intel Corporation<BR>\r
-  Portions Copyright (c) 2008-2009 Apple Inc.<BR>\r
-  All rights reserved. This program and the accompanying materials\r
+  Copyright (c) 2006 - 2008, Intel Corporation. All rights reserved.<BR>\r
+  Portions copyright (c) 2008 - 2009, Apple Inc. All rights reserved.<BR>\r
+  This program and the accompanying materials\r
   are licensed and made available under the terms and conditions of the BSD License\r
   which accompanies this distribution.  The full text of the license may be found at\r
-  http://opensource.org/licenses/bsd-license.php\r
+  http://opensource.org/licenses/bsd-license.php.\r
 \r
   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
@@ -185,7 +185,7 @@ PeCoffLoaderGetPeHeader (
   The ImageRead and Handle fields of ImageContext structure must be valid prior \r
   to invoking this service.\r
 \r
-  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
+  @param  ImageContext              The pointer to the image context structure that describes the PE/COFF\r
                                     image that needs to be examined by this function.\r
 \r
   @retval RETURN_SUCCESS            The information on the PE/COFF image was collected.\r
@@ -270,7 +270,7 @@ PeCoffLoaderGetImageInfo (
   // Obviously having base relocations with RELOCS_STRIPPED==1 is invalid.\r
   //\r
   // Look at the file header to determine if relocations have been stripped, and\r
-  // save this info in the image context for later use.\r
+  // save this information in the image context for later use.\r
   //\r
   if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) {\r
     ImageContext->RelocationsStripped = TRUE;\r
@@ -500,7 +500,7 @@ PeCoffLoaderImageAddress (
   cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
   prior to transferring control to a PE/COFF image that is loaded using this library.\r
 \r
-  @param  ImageContext        Pointer to the image context structure that describes the PE/COFF\r
+  @param  ImageContext        The pointer to the image context structure that describes the PE/COFF\r
                               image that is being relocated.\r
 \r
   @retval RETURN_SUCCESS      The PE/COFF image was relocated.\r
@@ -548,7 +548,7 @@ PeCoffLoaderRelocateImage (
   if (ImageContext->RelocationsStripped) {\r
     // Applies additional environment specific actions to relocate fixups \r
     // to a PE/COFF image if needed\r
-    PeCoffLoaderRelocateImageExtraAction (ImageContext);       \r
+    PeCoffLoaderRelocateImageExtraAction (ImageContext);  \r
     return RETURN_SUCCESS;\r
   }\r
 \r
@@ -572,7 +572,9 @@ PeCoffLoaderRelocateImage (
       // Use PE32 offset\r
       //\r
       Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.ImageBase;\r
-      Hdr.Pe32->OptionalHeader.ImageBase = (UINT32)BaseAddress;\r
+      if (Adjust != 0) {\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
@@ -581,7 +583,9 @@ PeCoffLoaderRelocateImage (
       // Use PE32+ offset\r
       //\r
       Adjust = (UINT64) BaseAddress - Hdr.Pe32Plus->OptionalHeader.ImageBase;\r
-      Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;\r
+      if (Adjust != 0) {\r
+        Hdr.Pe32Plus->OptionalHeader.ImageBase = (UINT64)BaseAddress;\r
+      }\r
 \r
       NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;\r
       RelocDir  = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];\r
@@ -612,7 +616,9 @@ PeCoffLoaderRelocateImage (
   } else {\r
     Hdr.Te             = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);\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
+    if (Adjust != 0) {\r
+      Hdr.Te->ImageBase  = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER));\r
+    }\r
 \r
     //\r
     // Find the relocation block\r
@@ -635,115 +641,120 @@ PeCoffLoaderRelocateImage (
   }\r
 \r
   //\r
-  // Run the relocation information and apply the fixups\r
+  // If Adjust is not zero, then apply fix ups to the image\r
   //\r
-  FixupData = ImageContext->FixupData;\r
-  while (RelocBase < RelocBaseEnd) {\r
-\r
-    Reloc     = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
-    RelocEnd  = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);\r
-    \r
+  if (Adjust != 0) {\r
     //\r
-    // Make sure RelocEnd is in the Image range.\r
+    // Run the relocation information and apply the fixups\r
     //\r
-    if ((CHAR8 *) RelocEnd < (CHAR8 *)((UINTN) ImageContext->ImageAddress) ||\r
-        (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress + (UINTN)ImageContext->ImageSize)) {\r
-      ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
-      return RETURN_LOAD_ERROR;\r
-    }\r
+    FixupData = ImageContext->FixupData;\r
+    while (RelocBase < RelocBaseEnd) {\r
 \r
-    if (!(ImageContext->IsTeImage)) {\r
-      FixupBase = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress);\r
-      if (FixupBase == NULL) {\r
+      Reloc     = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
+      RelocEnd  = (UINT16 *) ((CHAR8 *) RelocBase + RelocBase->SizeOfBlock);\r
+      \r
+      //\r
+      // Make sure RelocEnd is in the Image range.\r
+      //\r
+      if ((CHAR8 *) RelocEnd < (CHAR8 *)((UINTN) ImageContext->ImageAddress) ||\r
+          (CHAR8 *) RelocEnd > (CHAR8 *)((UINTN)ImageContext->ImageAddress + (UINTN)ImageContext->ImageSize)) {\r
+        ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
         return RETURN_LOAD_ERROR;\r
       }\r
-    } else {\r
-      FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +\r
-                    RelocBase->VirtualAddress +\r
-                    sizeof(EFI_TE_IMAGE_HEADER) -\r
-                    Hdr.Te->StrippedSize\r
-                    );\r
-    }    \r
 \r
-    //\r
-    // Run this relocation record\r
-    //\r
-    while (Reloc < RelocEnd) {\r
+      if (!(ImageContext->IsTeImage)) {\r
+        FixupBase = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress);\r
+        if (FixupBase == NULL) {\r
+          return RETURN_LOAD_ERROR;\r
+        }\r
+      } else {\r
+        FixupBase = (CHAR8 *)(UINTN)(ImageContext->ImageAddress +\r
+                      RelocBase->VirtualAddress +\r
+                      sizeof(EFI_TE_IMAGE_HEADER) -\r
+                      Hdr.Te->StrippedSize\r
+                      );\r
+      }    \r
 \r
-      Fixup = FixupBase + (*Reloc & 0xFFF);\r
-      switch ((*Reloc) >> 12) {\r
-      case EFI_IMAGE_REL_BASED_ABSOLUTE:\r
-        break;\r
+      //\r
+      // Run this relocation record\r
+      //\r
+      while (Reloc < RelocEnd) {\r
 \r
-      case EFI_IMAGE_REL_BASED_HIGH:\r
-        Fixup16   = (UINT16 *) Fixup;\r
-        *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
-        if (FixupData != NULL) {\r
-          *(UINT16 *) FixupData = *Fixup16;\r
-          FixupData             = FixupData + sizeof (UINT16);\r
-        }\r
-        break;\r
+        Fixup = FixupBase + (*Reloc & 0xFFF);\r
+        switch ((*Reloc) >> 12) {\r
+        case EFI_IMAGE_REL_BASED_ABSOLUTE:\r
+          break;\r
 \r
-      case EFI_IMAGE_REL_BASED_LOW:\r
-        Fixup16   = (UINT16 *) Fixup;\r
-        *Fixup16  = (UINT16) (*Fixup16 + (UINT16) Adjust);\r
-        if (FixupData != NULL) {\r
-          *(UINT16 *) FixupData = *Fixup16;\r
-          FixupData             = FixupData + sizeof (UINT16);\r
-        }\r
-        break;\r
+        case EFI_IMAGE_REL_BASED_HIGH:\r
+          Fixup16   = (UINT16 *) Fixup;\r
+          *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
+          if (FixupData != NULL) {\r
+            *(UINT16 *) FixupData = *Fixup16;\r
+            FixupData             = FixupData + sizeof (UINT16);\r
+          }\r
+          break;\r
 \r
-      case EFI_IMAGE_REL_BASED_HIGHLOW:\r
-        Fixup32   = (UINT32 *) Fixup;\r
-        *Fixup32  = *Fixup32 + (UINT32) Adjust;\r
-        if (FixupData != NULL) {\r
-          FixupData             = ALIGN_POINTER (FixupData, sizeof (UINT32));\r
-          *(UINT32 *)FixupData  = *Fixup32;\r
-          FixupData             = FixupData + sizeof (UINT32);\r
-        }\r
-        break;\r
+        case EFI_IMAGE_REL_BASED_LOW:\r
+          Fixup16   = (UINT16 *) Fixup;\r
+          *Fixup16  = (UINT16) (*Fixup16 + (UINT16) Adjust);\r
+          if (FixupData != NULL) {\r
+            *(UINT16 *) FixupData = *Fixup16;\r
+            FixupData             = FixupData + sizeof (UINT16);\r
+          }\r
+          break;\r
 \r
-      case EFI_IMAGE_REL_BASED_DIR64:\r
-        Fixup64 = (UINT64 *) Fixup;\r
-        *Fixup64 = *Fixup64 + (UINT64) Adjust;\r
-        if (FixupData != NULL) {\r
-          FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));\r
-          *(UINT64 *)(FixupData) = *Fixup64;\r
-          FixupData = FixupData + sizeof(UINT64);\r
+        case EFI_IMAGE_REL_BASED_HIGHLOW:\r
+          Fixup32   = (UINT32 *) Fixup;\r
+          *Fixup32  = *Fixup32 + (UINT32) Adjust;\r
+          if (FixupData != NULL) {\r
+            FixupData             = ALIGN_POINTER (FixupData, sizeof (UINT32));\r
+            *(UINT32 *)FixupData  = *Fixup32;\r
+            FixupData             = FixupData + sizeof (UINT32);\r
+          }\r
+          break;\r
+\r
+        case EFI_IMAGE_REL_BASED_DIR64:\r
+          Fixup64 = (UINT64 *) Fixup;\r
+          *Fixup64 = *Fixup64 + (UINT64) Adjust;\r
+          if (FixupData != NULL) {\r
+            FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));\r
+            *(UINT64 *)(FixupData) = *Fixup64;\r
+            FixupData = FixupData + sizeof(UINT64);\r
+          }\r
+          break;\r
+\r
+        default:\r
+          //\r
+          // The common code does not handle some of the stranger IPF relocations\r
+          // PeCoffLoaderRelocateImageEx () adds support for these complex fixups\r
+          // on IPF and is a No-Op on other architectures.\r
+          //\r
+          Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r
+          if (RETURN_ERROR (Status)) {\r
+            ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
+            return Status;\r
+          }\r
         }\r
-        break;\r
 \r
-      default:\r
         //\r
-        // The common code does not handle some of the stranger IPF relocations\r
-        // PeCoffLoaderRelocateImageEx () adds support for these complex fixups\r
-        // on IPF and is a No-Op on other architectures.\r
+        // Next relocation record\r
         //\r
-        Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r
-        if (RETURN_ERROR (Status)) {\r
-          ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
-          return Status;\r
-        }\r
+        Reloc += 1;\r
       }\r
 \r
       //\r
-      // Next relocation record\r
+      // Next reloc block\r
       //\r
-      Reloc += 1;\r
+      RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r
     }\r
 \r
     //\r
-    // Next reloc block\r
+    // Adjust the EntryPoint to match the linked-to address\r
     //\r
-    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
+    if (ImageContext->DestinationAddress != 0) {\r
+       ImageContext->EntryPoint -= (UINT64) ImageContext->ImageAddress;\r
+       ImageContext->EntryPoint += (UINT64) ImageContext->DestinationAddress;\r
+    }\r
   }\r
   \r
   // Applies additional environment specific actions to relocate fixups \r
@@ -770,7 +781,7 @@ PeCoffLoaderRelocateImage (
   cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
   prior to transferring control to a PE/COFF image that is loaded using this library.\r
 \r
-  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
+  @param  ImageContext              The pointer to the image context structure that describes the PE/COFF\r
                                     image that is being loaded.\r
 \r
   @retval RETURN_SUCCESS            The PE/COFF image was loaded into the buffer specified by\r
@@ -820,7 +831,7 @@ PeCoffLoaderLoadImage (
   ImageContext->ImageError = IMAGE_ERROR_SUCCESS;\r
 \r
   //\r
-  // Copy the provided context info into our local version, get what we\r
+  // Copy the provided context information into our local version, get what we\r
   // can from the original image, and then use that to make sure everything\r
   // is legit.\r
   //\r
@@ -925,6 +936,14 @@ PeCoffLoaderLoadImage (
   //\r
   Section = FirstSection;\r
   for (Index = 0, MaxEnd = NULL; Index < NumberOfSections; Index++) {\r
+    //\r
+    // Read the section\r
+    //\r
+    Size = (UINTN) Section->Misc.VirtualSize;\r
+    if ((Size == 0) || (Size > Section->SizeOfRawData)) {\r
+      Size = (UINTN) Section->SizeOfRawData;\r
+    }\r
+\r
     //\r
     // Compute sections address\r
     //\r
@@ -935,9 +954,9 @@ PeCoffLoaderLoadImage (
             );\r
 \r
     //\r
-    // If the base start or end address resolved to 0, then fail.\r
+    // If the size of the section is non-zero and the base address or end address resolved to 0, then fail.\r
     //\r
-    if ((Base == NULL) || (End == NULL)) {\r
+    if ((Size > 0) && ((Base == NULL) || (End == NULL))) {\r
       ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;\r
       return RETURN_LOAD_ERROR;\r
     }\r
@@ -951,14 +970,6 @@ PeCoffLoaderLoadImage (
       MaxEnd = End;\r
     }\r
 \r
-    //\r
-    // Read the section\r
-    //\r
-    Size = (UINTN) Section->Misc.VirtualSize;\r
-    if ((Size == 0) || (Size > Section->SizeOfRawData)) {\r
-      Size = (UINTN) Section->SizeOfRawData;\r
-    }\r
-\r
     if (Section->SizeOfRawData > 0) {\r
       if (!(ImageContext->IsTeImage)) {\r
         Status = ImageContext->ImageRead (\r
@@ -1068,7 +1079,7 @@ PeCoffLoaderLoadImage (
   ImageContext->FixupData = NULL;\r
 \r
   //\r
-  // Load the Codeview info if present\r
+  // Load the Codeview information if present\r
   //\r
   if (ImageContext->DebugDirectoryEntryRva != 0) {\r
     if (!(ImageContext->IsTeImage)) {\r
@@ -1248,7 +1259,7 @@ PeCoffLoaderLoadImage (
   cache(s) in hardware, then the caller is responsible for performing cache maintenance operations\r
   prior to transferring control to a PE/COFF image that is loaded using this library.\r
 \r
-  @param  ImageBase          Base address of a PE/COFF image that has been loaded \r
+  @param  ImageBase          The base address of a PE/COFF image that has been loaded \r
                              and relocated into system memory.\r
   @param  VirtImageBase      The request virtual address that the PE/COFF image is to\r
                              be fixed up for.\r
@@ -1460,7 +1471,7 @@ PeCoffLoaderRelocateImageForRuntime (
   If ReadSize is NULL, then ASSERT().\r
   If Buffer is NULL, then ASSERT().\r
 \r
-  @param  FileHandle        Pointer to base of the input stream\r
+  @param  FileHandle        The pointer to base of the input stream\r
   @param  FileOffset        Offset into the PE/COFF image to begin the read operation.\r
   @param  ReadSize          On input, the size in bytes of the requested read operation.  \r
                             On output, the number of bytes actually read.\r
@@ -1497,7 +1508,7 @@ PeCoffLoaderImageReadFromMemory (
   \r
   If ImageContext is NULL, then ASSERT().\r
   \r
-  @param  ImageContext              Pointer to the image context structure that describes the PE/COFF\r
+  @param  ImageContext              The 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