]> git.proxmox.com Git - mirror_edk2.git/commitdiff
Fixed FwImage/fwimage.c to guarantee that the raw size of a section is a multiple...
authormdkinney <mdkinney@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 21 Sep 2006 03:11:13 +0000 (03:11 +0000)
committermdkinney <mdkinney@6f19259b-4bc3-4df7-8a09-765794883524>
Thu, 21 Sep 2006 03:11:13 +0000 (03:11 +0000)
Revert PeCoffLoader/BasePeCoff.c and PeiRebase/PeiRebaseExe.c back to the previous revisions.

git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@1582 6f19259b-4bc3-4df7-8a09-765794883524

Tools/Source/TianoTools/FwImage/fwimage.c
Tools/Source/TianoTools/PeCoffLoader/BasePeCoff.c
Tools/Source/TianoTools/PeiRebase/PeiRebaseExe.c

index ce3a8127b7ca84e8807c2cf5bb967f5cca9dad65..0097a945507b25a1dc9b7313aa68d7fad6980a54 100644 (file)
@@ -165,7 +165,7 @@ Returns:
   ULONG             Index;\r
   ULONG             Index1;\r
   BOOLEAN           TimeStampPresent;\r
-  UINTN                                 RelocSize;\r
+  UINTN                                 AllignedRelocSize;\r
   UINTN                                 Delta;\r
   EFI_IMAGE_SECTION_HEADER              *SectionHeader;\r
   UINT8      *FileBuffer;\r
@@ -356,7 +356,6 @@ Returns:
     PeHdr->FileHeader.TimeDateStamp = (UINT32) TimeStamp;\r
   }\r
 \r
-  RelocSize = 0;\r
   if (PeHdr->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) {\r
     Optional32 = (EFI_IMAGE_OPTIONAL_HEADER32 *)&PeHdr->OptionalHeader;\r
     Optional32->MajorLinkerVersion          = 0;\r
@@ -401,10 +400,29 @@ Returns:
       if (Optional32->DataDirectory[5].Size != 0) {\r
         SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(FileBuffer + DosHdr->e_lfanew + sizeof(UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + PeHdr->FileHeader.SizeOfOptionalHeader);\r
         for (Index = 0; Index < PeHdr->FileHeader.NumberOfSections; Index++, SectionHeader++) {\r
+          //\r
+          // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory\r
+          //\r
           if (SectionHeader->VirtualAddress == Optional32->DataDirectory[5].VirtualAddress) {\r
-            FileLength = SectionHeader->PointerToRawData + Optional32->DataDirectory[5].Size;\r
-            FileLength = (FileLength + 7) & 0xfffffff8;\r
-            RelocSize = FileLength - SectionHeader->PointerToRawData;\r
+            SectionHeader->Misc.VirtualSize = Optional32->DataDirectory[5].Size;\r
+            AllignedRelocSize = (Optional32->DataDirectory[5].Size + Optional32->FileAlignment - 1) & (~(Optional32->FileAlignment - 1));\r
+            //\r
+            // Check to see if there is zero padding at the end of the base relocations\r
+            //\r
+            if (AllignedRelocSize < SectionHeader->SizeOfRawData) {\r
+              //\r
+              // Check to see if the base relocations are at the end of the file\r
+              //\r
+              if (SectionHeader->PointerToRawData + SectionHeader->SizeOfRawData == Optional32->SizeOfImage) {\r
+                //\r
+                // All the required conditions are met to strip the zero padding of the end of the base relocations section\r
+                //\r
+                Optional32->SizeOfImage -= (SectionHeader->SizeOfRawData - AllignedRelocSize);\r
+                Optional32->SizeOfInitializedData -= (SectionHeader->SizeOfRawData - AllignedRelocSize);\r
+                SectionHeader->SizeOfRawData = AllignedRelocSize;\r
+                FileLength = Optional32->SizeOfImage;\r
+              }\r
+            }\r
           }\r
         }\r
       }\r
@@ -454,26 +472,35 @@ Returns:
       if (Optional64->DataDirectory[5].Size != 0) {\r
         SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(FileBuffer + DosHdr->e_lfanew + sizeof(UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + PeHdr->FileHeader.SizeOfOptionalHeader);\r
         for (Index = 0; Index < PeHdr->FileHeader.NumberOfSections; Index++, SectionHeader++) {\r
+          //\r
+          // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory\r
+          //\r
           if (SectionHeader->VirtualAddress == Optional64->DataDirectory[5].VirtualAddress) {\r
-            FileLength = SectionHeader->PointerToRawData + Optional64->DataDirectory[5].Size;\r
-            FileLength = (FileLength + 7) & 0xfffffff8;\r
-            RelocSize = FileLength - SectionHeader->PointerToRawData;\r
+            SectionHeader->Misc.VirtualSize = Optional64->DataDirectory[5].Size;\r
+            AllignedRelocSize = (Optional64->DataDirectory[5].Size + Optional64->FileAlignment - 1) & (~(Optional64->FileAlignment - 1));\r
+            //\r
+            // Check to see if there is zero padding at the end of the base relocations\r
+            //\r
+            if (AllignedRelocSize < SectionHeader->SizeOfRawData) {\r
+              //\r
+              // Check to see if the base relocations are at the end of the file\r
+              //\r
+              if (SectionHeader->PointerToRawData + SectionHeader->SizeOfRawData == Optional64->SizeOfImage) {\r
+                //\r
+                // All the required conditions are met to strip the zero padding of the end of the base relocations section\r
+                //\r
+                Optional64->SizeOfImage -= (SectionHeader->SizeOfRawData - AllignedRelocSize);\r
+                Optional64->SizeOfInitializedData -= (SectionHeader->SizeOfRawData - AllignedRelocSize);\r
+                SectionHeader->SizeOfRawData = AllignedRelocSize;\r
+                FileLength = Optional64->SizeOfImage;\r
+              }\r
+            }\r
           }\r
         }\r
       }\r
     }\r
   }\r
 \r
-  if (RelocSize != 0) {\r
-    SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(FileBuffer + DosHdr->e_lfanew + sizeof(UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + PeHdr->FileHeader.SizeOfOptionalHeader);\r
-    for (Index = 0; Index < PeHdr->FileHeader.NumberOfSections; Index++, SectionHeader++) {\r
-      if (strcmp(SectionHeader->Name, ".reloc") == 0) {\r
-        SectionHeader->Misc.VirtualSize = (RelocSize + 0x1f) & 0xffffffe0;\r
-        SectionHeader->SizeOfRawData = RelocSize;\r
-      }\r
-    }\r
-  }\r
-\r
   FWriteFile (fpOut, FileBuffer, FileLength);\r
 \r
   //\r
index 556b2d73c9575a8f06078676f7938ce52e8790a2..9c25e1f4b8f1162f7b60ae5b85efb606eed657b9 100644 (file)
@@ -858,7 +858,7 @@ Returns:
     Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress);\r
     End = PeCoffLoaderImageAddress (\r
             ImageContext,\r
-            Section->VirtualAddress + Section->SizeOfRawData - 1\r
+            Section->VirtualAddress + Section->Misc.VirtualSize - 1\r
             );\r
     if (ImageContext->IsTeImage) {\r
       Base  = (CHAR8 *) ((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN) TeHdr->StrippedSize);\r
index 35329c74d69cbf08c53b5118f3dc341244fee4f2..27c646e486fab1b86699270498f41ec2e0fe001b 100644 (file)
@@ -624,12 +624,12 @@ Returns:
     // Allocate a buffer for the image to be loaded into.\r
     //\r
     Pe32ImageSize       = GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION);\r
-    MemoryImagePointer  = (UINTN) (malloc (Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment));\r
+    MemoryImagePointer  = (UINTN) (malloc (Pe32ImageSize + 0x1000));\r
     if (MemoryImagePointer == 0) {\r
       Error (NULL, 0, 0, "memory allocation failure", NULL);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
-    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment);\r
+    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000);\r
     MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12);\r
     \r
 \r
@@ -876,13 +876,13 @@ Returns:
     //\r
     // Allocate a buffer for the image to be loaded into.\r
     //\r
-    MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment));\r
+    MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000));\r
     if (MemoryImagePointer == 0) {\r
       Error (NULL, 0, 0, "memory allocation error on rebase of TE image", FileGuidString);\r
       free (TEBuffer);\r
       return EFI_OUT_OF_RESOURCES;\r
     }\r
-    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment);\r
+    memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000);\r
     MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12);\r
     \r
 \r