]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
MdePkg: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / BasePeCoff.c
index c57816a80887fe1b8a339515c5a6b2b4a4c227c8..07bb62f8601dacda640a482edbd138dabdaa41cf 100644 (file)
   PeCoffLoaderGetPeHeader() routine will do basic check for PE/COFF header.\r
   PeCoffLoaderGetImageInfo() routine will do basic check for whole PE/COFF image.\r
 \r
-  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2019, 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
-\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
+  SPDX-License-Identifier: BSD-2-Clause-Patent\r
 \r
 **/\r
 \r
@@ -1002,7 +996,7 @@ PeCoffLoaderRelocateImage (
                                                                             RelocDir->VirtualAddress + RelocDir->Size - 1,\r
                                                                             TeStrippedOffset\r
                                                                             );\r
-    if (RelocBase == NULL || RelocBaseEnd == NULL || RelocBaseEnd < RelocBase) {\r
+    if (RelocBase == NULL || RelocBaseEnd == NULL || (UINTN) RelocBaseEnd < (UINTN) RelocBase) {\r
       ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
       return RETURN_LOAD_ERROR;\r
     }\r
@@ -1022,7 +1016,7 @@ PeCoffLoaderRelocateImage (
     // Run the relocation information and apply the fixups\r
     //\r
     FixupData = ImageContext->FixupData;\r
-    while (RelocBase < RelocBaseEnd) {\r
+    while ((UINTN) RelocBase < (UINTN) RelocBaseEnd) {\r
 \r
       Reloc     = (UINT16 *) ((CHAR8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
       //\r
@@ -1051,7 +1045,7 @@ PeCoffLoaderRelocateImage (
       //\r
       // Run this relocation record\r
       //\r
-      while (Reloc < RelocEnd) {\r
+      while ((UINTN) Reloc < (UINTN) RelocEnd) {\r
         Fixup = PeCoffLoaderImageAddress (ImageContext, RelocBase->VirtualAddress + (*Reloc & 0xFFF), TeStrippedOffset);\r
         if (Fixup == NULL) {\r
           ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
@@ -1671,6 +1665,7 @@ PeCoffLoaderRelocateImageForRuntime (
   EFI_IMAGE_DATA_DIRECTORY            *RelocDir;\r
   EFI_IMAGE_BASE_RELOCATION           *RelocBase;\r
   EFI_IMAGE_BASE_RELOCATION           *RelocBaseEnd;\r
+  EFI_IMAGE_BASE_RELOCATION           *RelocBaseOrig;\r
   UINT16                              *Reloc;\r
   UINT16                              *RelocEnd;\r
   CHAR8                               *Fixup;\r
@@ -1681,11 +1676,19 @@ PeCoffLoaderRelocateImageForRuntime (
   CHAR8                               *FixupData;\r
   UINTN                               Adjust;\r
   RETURN_STATUS                       Status;\r
+  PE_COFF_LOADER_IMAGE_CONTEXT        ImageContext;\r
+\r
+  if (RelocationData == NULL || ImageBase == 0x0 || VirtImageBase == 0x0) {\r
+    return;\r
+  }\r
 \r
   OldBase = (CHAR8 *)((UINTN)ImageBase);\r
   NewBase = (CHAR8 *)((UINTN)VirtImageBase);\r
   Adjust = (UINTN) NewBase - (UINTN) OldBase;\r
 \r
+  ImageContext.ImageAddress = ImageBase;\r
+  ImageContext.ImageSize = ImageSize;\r
+\r
   //\r
   // Find the image's relocate dir info\r
   //\r
@@ -1730,10 +1733,23 @@ PeCoffLoaderRelocateImageForRuntime (
   // 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
+  RelocBase = NULL;\r
+  RelocBaseEnd = NULL;\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
-    RelocBaseEnd  = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size);\r
+    if ((RelocDir != NULL) && (RelocDir->Size > 0)) {\r
+      RelocBase     = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext, RelocDir->VirtualAddress, 0);\r
+      RelocBaseEnd  = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext,\r
+                                                                              RelocDir->VirtualAddress + RelocDir->Size - 1,\r
+                                                                              0\r
+                                                                              );\r
+    }\r
+    if (RelocBase == NULL || RelocBaseEnd == NULL || (UINTN) RelocBaseEnd < (UINTN) RelocBase) {\r
+      //\r
+      // relocation block is not valid, just return\r
+      //\r
+      return;\r
+    }\r
   } else {\r
     //\r
     // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.\r
@@ -1747,96 +1763,109 @@ PeCoffLoaderRelocateImageForRuntime (
   //\r
   ASSERT (RelocBase != NULL && RelocBaseEnd != NULL);\r
 \r
-  //\r
-  // Run the whole relocation block. And re-fixup data that has not been\r
-  // modified. The FixupData is used to see if the image has been modified\r
-  // since it was relocated. This is so data sections that have been updated\r
-  // by code will not be fixed up, since that would set them back to\r
-  // defaults.\r
-  //\r
-  FixupData = RelocationData;\r
-  while (RelocBase < RelocBaseEnd) {\r
+  if (Adjust != 0) {\r
     //\r
-    // Add check for RelocBase->SizeOfBlock field.\r
+    // Run the whole relocation block. And re-fixup data that has not been\r
+    // modified. The FixupData is used to see if the image has been modified\r
+    // since it was relocated. This is so data sections that have been updated\r
+    // by code will not be fixed up, since that would set them back to\r
+    // defaults.\r
     //\r
-    if ((RelocBase->SizeOfBlock == 0) || (RelocBase->SizeOfBlock > RelocDir->Size)) {\r
+    FixupData = RelocationData;\r
+    RelocBaseOrig = RelocBase;\r
+    while ((UINTN) RelocBase < (UINTN) RelocBaseEnd) {\r
       //\r
-      // Data invalid, cannot continue to relocate the image, just return.\r
+      // Add check for RelocBase->SizeOfBlock field.\r
       //\r
-      return;\r
-    }\r
-\r
-    Reloc     = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
-    RelocEnd  = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);\r
-    FixupBase = (CHAR8 *) ((UINTN)ImageBase) + RelocBase->VirtualAddress;\r
+      if ((RelocBase->SizeOfBlock == 0) || (RelocBase->SizeOfBlock > RelocDir->Size)) {\r
+        //\r
+        // Data invalid, cannot continue to relocate the image, just return.\r
+        //\r
+        return;\r
+      }\r
 \r
-    //\r
-    // Run this relocation record\r
-    //\r
-    while (Reloc < RelocEnd) {\r
+      Reloc     = (UINT16 *) ((UINT8 *) RelocBase + sizeof (EFI_IMAGE_BASE_RELOCATION));\r
+      RelocEnd  = (UINT16 *) ((UINT8 *) RelocBase + RelocBase->SizeOfBlock);\r
+      if ((UINTN)RelocEnd > (UINTN)RelocBaseOrig + RelocDir->Size) {\r
+        return;\r
+      }\r
 \r
-      Fixup = FixupBase + (*Reloc & 0xFFF);\r
-      switch ((*Reloc) >> 12) {\r
+      FixupBase = PeCoffLoaderImageAddress (&ImageContext, RelocBase->VirtualAddress, 0);\r
+      if (FixupBase == NULL) {\r
+        return;\r
+      }\r
 \r
-      case EFI_IMAGE_REL_BASED_ABSOLUTE:\r
-        break;\r
+      //\r
+      // Run this relocation record\r
+      //\r
+      while ((UINTN) Reloc < (UINTN) RelocEnd) {\r
 \r
-      case EFI_IMAGE_REL_BASED_HIGH:\r
-        Fixup16 = (UINT16 *) Fixup;\r
-        if (*(UINT16 *) FixupData == *Fixup16) {\r
-          *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
+        Fixup = PeCoffLoaderImageAddress (&ImageContext, RelocBase->VirtualAddress + (*Reloc & 0xFFF), 0);\r
+        if (Fixup == NULL) {\r
+          return;\r
         }\r
+        switch ((*Reloc) >> 12) {\r
 \r
-        FixupData = FixupData + sizeof (UINT16);\r
-        break;\r
+        case EFI_IMAGE_REL_BASED_ABSOLUTE:\r
+          break;\r
 \r
-      case EFI_IMAGE_REL_BASED_LOW:\r
-        Fixup16 = (UINT16 *) Fixup;\r
-        if (*(UINT16 *) FixupData == *Fixup16) {\r
-          *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) Adjust & 0xffff));\r
-        }\r
+        case EFI_IMAGE_REL_BASED_HIGH:\r
+          Fixup16 = (UINT16 *) Fixup;\r
+          if (*(UINT16 *) FixupData == *Fixup16) {\r
+            *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16)));\r
+          }\r
 \r
-        FixupData = FixupData + sizeof (UINT16);\r
-        break;\r
+          FixupData = FixupData + sizeof (UINT16);\r
+          break;\r
 \r
-      case EFI_IMAGE_REL_BASED_HIGHLOW:\r
-        Fixup32       = (UINT32 *) Fixup;\r
-        FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r
-        if (*(UINT32 *) FixupData == *Fixup32) {\r
-          *Fixup32 = *Fixup32 + (UINT32) Adjust;\r
-        }\r
+        case EFI_IMAGE_REL_BASED_LOW:\r
+          Fixup16 = (UINT16 *) Fixup;\r
+          if (*(UINT16 *) FixupData == *Fixup16) {\r
+            *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) Adjust & 0xffff));\r
+          }\r
 \r
-        FixupData = FixupData + sizeof (UINT32);\r
-        break;\r
+          FixupData = FixupData + sizeof (UINT16);\r
+          break;\r
 \r
-      case EFI_IMAGE_REL_BASED_DIR64:\r
-        Fixup64       = (UINT64 *)Fixup;\r
-        FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));\r
-        if (*(UINT64 *) FixupData == *Fixup64) {\r
-          *Fixup64 = *Fixup64 + (UINT64)Adjust;\r
-        }\r
+        case EFI_IMAGE_REL_BASED_HIGHLOW:\r
+          Fixup32       = (UINT32 *) Fixup;\r
+          FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));\r
+          if (*(UINT32 *) FixupData == *Fixup32) {\r
+            *Fixup32 = *Fixup32 + (UINT32) Adjust;\r
+          }\r
 \r
-        FixupData = FixupData + sizeof (UINT64);\r
-        break;\r
+          FixupData = FixupData + sizeof (UINT32);\r
+          break;\r
 \r
-      default:\r
+        case EFI_IMAGE_REL_BASED_DIR64:\r
+          Fixup64       = (UINT64 *)Fixup;\r
+          FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64));\r
+          if (*(UINT64 *) FixupData == *Fixup64) {\r
+            *Fixup64 = *Fixup64 + (UINT64)Adjust;\r
+          }\r
+\r
+          FixupData = FixupData + sizeof (UINT64);\r
+          break;\r
+\r
+        default:\r
+          //\r
+          // Only Itanium requires ConvertPeImage_Ex\r
+          //\r
+          Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r
+          if (RETURN_ERROR (Status)) {\r
+            return ;\r
+          }\r
+        }\r
         //\r
-        // Only Itanium requires ConvertPeImage_Ex\r
+        // Next relocation record\r
         //\r
-        Status = PeHotRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);\r
-        if (RETURN_ERROR (Status)) {\r
-          return ;\r
-        }\r
+        Reloc += 1;\r
       }\r
       //\r
-      // Next relocation record\r
+      // next reloc block\r
       //\r
-      Reloc += 1;\r
+      RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r
     }\r
-    //\r
-    // next reloc block\r
-    //\r
-    RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;\r
   }\r
 }\r
 \r