]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdePkg/Library/BasePeCoffLib/BasePeCoff.c
MdePkg: Clean up source files
[mirror_edk2.git] / MdePkg / Library / BasePeCoffLib / BasePeCoff.c
index 8d1daba4bbe5cc5a2361ff2d3b08b1f7ad8cd160..32eca0ad2ef4062bd02c5f5c0edff04076fdf170 100644 (file)
@@ -15,7 +15,7 @@
   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 - 2017, Intel Corporation. All rights reserved.<BR>\r
+  Copyright (c) 2006 - 2018, 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
@@ -61,8 +61,8 @@ PeCoffLoaderGetPeHeaderMagicValue (
   )\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
+  // 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
@@ -77,12 +77,12 @@ PeCoffLoaderGetPeHeaderMagicValue (
 \r
 \r
 /**\r
-  Retrieves the PE or TE Header from a PE/COFF or TE image. \r
+  Retrieves the PE or TE Header from a PE/COFF or TE image.\r
 \r
   Caution: This function may receive untrusted input.\r
-  PE/COFF image is external input, so this routine will \r
-  also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, \r
-  SizeOfHeader, Section Data Region and Security Data Region be in PE image range. \r
+  PE/COFF image is external input, so this routine will\r
+  also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader,\r
+  SizeOfHeader, Section Data Region and Security Data Region be in PE image range.\r
 \r
   @param  ImageContext    The context of the image being loaded.\r
   @param  Hdr             The buffer in which to return the PE32, PE32+, or TE header.\r
@@ -211,7 +211,7 @@ PeCoffLoaderGetPeHeader (
 \r
     //\r
     // TE Image Data Directory Entry size is non-zero, but the Data Directory Virtual Address is zero.\r
-    // This case is not a valid TE image. \r
+    // This case is not a valid TE image.\r
     //\r
     if ((Hdr.Te->DataDirectory[0].Size != 0 && Hdr.Te->DataDirectory[0].VirtualAddress == 0) ||\r
         (Hdr.Te->DataDirectory[1].Size != 0 && Hdr.Te->DataDirectory[1].VirtualAddress == 0)) {\r
@@ -235,7 +235,7 @@ PeCoffLoaderGetPeHeader (
 \r
       //\r
       // 2. Check the FileHeader.SizeOfOptionalHeader field.\r
-      // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so \r
+      // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so\r
       // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow.\r
       //\r
       HeaderWithoutDataDir = sizeof (EFI_IMAGE_OPTIONAL_HEADER32) - sizeof (EFI_IMAGE_DATA_DIRECTORY) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;\r
@@ -349,7 +349,7 @@ PeCoffLoaderGetPeHeader (
       }\r
       //\r
       // 2. Check the FileHeader.SizeOfOptionalHeader field.\r
-      // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so \r
+      // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so\r
       // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow.\r
       //\r
       HeaderWithoutDataDir = sizeof (EFI_IMAGE_OPTIONAL_HEADER64) - sizeof (EFI_IMAGE_DATA_DIRECTORY) * EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES;\r
@@ -513,7 +513,7 @@ PeCoffLoaderGetPeHeader (
       //\r
       // Section data should bigger than the Pe header.\r
       //\r
-      if (SectionHeader.VirtualAddress < ImageContext->SizeOfHeaders || \r
+      if (SectionHeader.VirtualAddress < ImageContext->SizeOfHeaders ||\r
           SectionHeader.PointerToRawData < ImageContext->SizeOfHeaders) {\r
         ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r
         return RETURN_UNSUPPORTED;\r
@@ -561,22 +561,22 @@ PeCoffLoaderGetPeHeader (
 /**\r
   Retrieves information about a PE/COFF image.\r
 \r
-  Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, \r
-  DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and \r
-  DebugDirectoryEntryRva fields of the ImageContext structure.  \r
-  If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.  \r
-  If the PE/COFF image accessed through the ImageRead service in the ImageContext \r
-  structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.  \r
-  If any errors occur while computing the fields of ImageContext, \r
-  then the error status is returned in the ImageError field of ImageContext.  \r
+  Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize,\r
+  DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and\r
+  DebugDirectoryEntryRva fields of the ImageContext structure.\r
+  If ImageContext is NULL, then return RETURN_INVALID_PARAMETER.\r
+  If the PE/COFF image accessed through the ImageRead service in the ImageContext\r
+  structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED.\r
+  If any errors occur while computing the fields of ImageContext,\r
+  then the error status is returned in the ImageError field of ImageContext.\r
   If the image is a TE image, then SectionAlignment is set to 0.\r
-  The ImageRead and Handle fields of ImageContext structure must be valid prior \r
+  The ImageRead and Handle fields of ImageContext structure must be valid prior\r
   to invoking this service.\r
 \r
   Caution: This function may receive untrusted input.\r
-  PE/COFF image is external input, so this routine will \r
-  also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, \r
-  SizeOfHeader, Section Data Region and Security Data Region be in PE image range. \r
+  PE/COFF image is external input, so this routine will\r
+  also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader,\r
+  SizeOfHeader, Section Data Region and Security Data Region be in PE image range.\r
 \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
@@ -829,7 +829,7 @@ PeCoffLoaderGetImageInfo (
       // 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
       // by the RVA and the VirtualSize of the last section header in the\r
-      // Section Table.  \r
+      // Section Table.\r
       //\r
       if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) {\r
         ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) - TeStrippedOffset;\r
@@ -884,7 +884,7 @@ PeCoffLoaderGetImageInfo (
 VOID *\r
 PeCoffLoaderImageAddress (\r
   IN OUT PE_COFF_LOADER_IMAGE_CONTEXT          *ImageContext,\r
-  IN     UINTN                                 Address, \r
+  IN     UINTN                                 Address,\r
   IN     UINTN                                 TeStrippedOffset\r
   )\r
 {\r
@@ -906,12 +906,12 @@ PeCoffLoaderImageAddress (
   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
-  \r
-  The ImageRead, Handle, PeCoffHeaderOffset,  IsTeImage, Machine, ImageType, ImageAddress, \r
-  ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, \r
-  DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of \r
+\r
+  The ImageRead, Handle, PeCoffHeaderOffset,  IsTeImage, Machine, ImageType, ImageAddress,\r
+  ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders,\r
+  DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of\r
   the ImageContext structure must be valid prior to invoking this service.\r
-    \r
+\r
   If ImageContext is NULL, then ASSERT().\r
 \r
   Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
@@ -966,9 +966,9 @@ PeCoffLoaderRelocateImage (
   // If there are no relocation entries, then we are done\r
   //\r
   if (ImageContext->RelocationsStripped) {\r
-    // Applies additional environment specific actions to relocate fixups \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
@@ -1048,7 +1048,7 @@ PeCoffLoaderRelocateImage (
     //\r
     // Set base and end to bypass processing below.\r
     //\r
-    RelocBase = RelocBaseEnd = NULL;    \r
+    RelocBase = RelocBaseEnd = NULL;\r
   }\r
   RelocBaseOrg = RelocBase;\r
 \r
@@ -1084,7 +1084,7 @@ PeCoffLoaderRelocateImage (
       if (FixupBase == NULL) {\r
         ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
         return RETURN_LOAD_ERROR;\r
-      }  \r
+      }\r
 \r
       //\r
       // Run this relocation record\r
@@ -1171,11 +1171,11 @@ PeCoffLoaderRelocateImage (
        ImageContext->EntryPoint += (UINT64) ImageContext->DestinationAddress;\r
     }\r
   }\r
-  \r
-  // Applies additional environment specific actions to relocate fixups \r
+\r
+  // Applies additional environment specific actions to relocate fixups\r
   // to a PE/COFF image if needed\r
   PeCoffLoaderRelocateImageExtraAction (ImageContext);\r
-  \r
+\r
   return RETURN_SUCCESS;\r
 }\r
 \r
@@ -1186,10 +1186,10 @@ PeCoffLoaderRelocateImage (
   specified by the ImageAddress and ImageSize fields of ImageContext.  The caller must allocate\r
   the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function.\r
   The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed.\r
-  The ImageRead, Handle, PeCoffHeaderOffset,  IsTeImage,  Machine, ImageType, ImageAddress, ImageSize, \r
-  DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva \r
+  The ImageRead, Handle, PeCoffHeaderOffset,  IsTeImage,  Machine, ImageType, ImageAddress, ImageSize,\r
+  DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva\r
   fields of the ImageContext structure must be valid prior to invoking this service.\r
-  \r
+\r
   If ImageContext is NULL, then ASSERT().\r
 \r
   Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
@@ -1501,7 +1501,7 @@ PeCoffLoaderLoadImage (
     }\r
 \r
     if (TempDebugEntryRva != 0) {\r
-      ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva, TeStrippedOffset); \r
+      ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva, TeStrippedOffset);\r
       if (ImageContext->CodeView == NULL) {\r
         ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;\r
         return RETURN_LOAD_ERROR;\r
@@ -1583,7 +1583,7 @@ PeCoffLoaderLoadImage (
       Base = PeCoffLoaderImageAddress (ImageContext, DirectoryEntry->VirtualAddress, 0);\r
       if (Base != NULL) {\r
         ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) Base;\r
-        Offset = sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * \r
+        Offset = sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) *\r
                (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);\r
         if (Offset > DirectoryEntry->Size) {\r
           ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r
@@ -1619,7 +1619,7 @@ PeCoffLoaderLoadImage (
                   return RETURN_UNSUPPORTED;\r
                 }\r
                 ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirectory);\r
-                Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + \r
+                Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) +\r
                          sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);\r
                 if (Offset > DirectoryEntry->Size) {\r
                   ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r
@@ -1636,7 +1636,7 @@ PeCoffLoaderLoadImage (
                     return RETURN_UNSUPPORTED;\r
                   }\r
                   ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirectory);\r
-                  Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + \r
+                  Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) +\r
                            sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);\r
                   if (Offset > DirectoryEntry->Size) {\r
                     ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;\r
@@ -1665,33 +1665,33 @@ PeCoffLoaderLoadImage (
       }\r
     }\r
   }\r
\r
+\r
   return Status;\r
 }\r
 \r
 \r
 /**\r
   Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI\r
-  runtime. \r
-  \r
-  This function reapplies relocation fixups to the PE/COFF image specified by ImageBase \r
-  and ImageSize so the image will execute correctly when the PE/COFF image is mapped \r
-  to the address specified by VirtualImageBase.  RelocationData must be identical \r
-  to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure \r
+  runtime.\r
+\r
+  This function reapplies relocation fixups to the PE/COFF image specified by ImageBase\r
+  and ImageSize so the image will execute correctly when the PE/COFF image is mapped\r
+  to the address specified by VirtualImageBase.  RelocationData must be identical\r
+  to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure\r
   after this PE/COFF image was relocated with PeCoffLoaderRelocateImage().\r
 \r
   Note that if the platform does not maintain coherency between the instruction cache(s) and the data\r
   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          The 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
   @param  ImageSize          The size, in bytes, of the PE/COFF image.\r
-  @param  RelocationData     A pointer to the relocation data that was collected when the PE/COFF \r
+  @param  RelocationData     A pointer to the relocation data that was collected when the PE/COFF\r
                              image was relocated using PeCoffLoaderRelocateImage().\r
-  \r
+\r
 **/\r
 VOID\r
 EFIAPI\r
@@ -1784,7 +1784,7 @@ PeCoffLoaderRelocateImageForRuntime (
     ASSERT (FALSE);\r
     return ;\r
   }\r
-  \r
+\r
   //\r
   // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL.\r
   //\r
@@ -1886,14 +1886,14 @@ PeCoffLoaderRelocateImageForRuntime (
 \r
 /**\r
   Reads contents of a PE/COFF image from a buffer in system memory.\r
-   \r
-  This is the default implementation of a PE_COFF_LOADER_READ_FILE function \r
-  that assumes FileHandle pointer to the beginning of a PE/COFF image.   \r
-  This function reads contents of the PE/COFF image that starts at the system memory \r
-  address specified by FileHandle.  The read operation copies ReadSize bytes from the \r
-  PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.  \r
+\r
+  This is the default implementation of a PE_COFF_LOADER_READ_FILE function\r
+  that assumes FileHandle pointer to the beginning of a PE/COFF image.\r
+  This function reads contents of the PE/COFF image that starts at the system memory\r
+  address specified by FileHandle.  The read operation copies ReadSize bytes from the\r
+  PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer.\r
   The size of the buffer actually read is returned in ReadSize.\r
-  \r
+\r
   The caller must make sure the FileOffset and ReadSize within the file scope.\r
 \r
   If FileHandle is NULL, then ASSERT().\r
@@ -1902,11 +1902,11 @@ PeCoffLoaderRelocateImageForRuntime (
 \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
+  @param  ReadSize          On input, the size in bytes of the requested read operation.\r
                             On output, the number of bytes actually read.\r
   @param  Buffer            Output buffer that contains the data read from the PE/COFF image.\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
@@ -1928,15 +1928,15 @@ PeCoffLoaderImageReadFromMemory (
 \r
 /**\r
   Unloads a loaded PE/COFF image from memory and releases its taken resource.\r
-  Releases any environment specific resources that were allocated when the image \r
-  specified by ImageContext was loaded using PeCoffLoaderLoadImage(). \r
\r
+  Releases any environment specific resources that were allocated when the image\r
+  specified by ImageContext was loaded using PeCoffLoaderLoadImage().\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
+  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
+\r
   If ImageContext is NULL, then ASSERT().\r
-  \r
+\r
   @param  ImageContext              The pointer to the image context structure that describes the PE/COFF\r
                                     image to be unloaded.\r
 \r
@@ -1949,7 +1949,7 @@ PeCoffLoaderUnloadImage (
   )\r
 {\r
   //\r
-  // Applies additional environment specific actions to unload a \r
+  // Applies additional environment specific actions to unload a\r
   // PE/COFF image if needed\r
   //\r
   PeCoffLoaderUnloadImageExtraAction (ImageContext);\r