From 0411bcafa05c2b51a3671463b7ea1ddf5581f78c Mon Sep 17 00:00:00 2001 From: mdkinney Date: Thu, 21 Sep 2006 03:11:13 +0000 Subject: [PATCH] Fixed FwImage/fwimage.c to guarantee that the raw size of a section is a multiple of the file alignment. 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 | 63 +++++++++++++------ .../TianoTools/PeCoffLoader/BasePeCoff.c | 2 +- .../TianoTools/PeiRebase/PeiRebaseExe.c | 8 +-- 3 files changed, 50 insertions(+), 23 deletions(-) diff --git a/Tools/Source/TianoTools/FwImage/fwimage.c b/Tools/Source/TianoTools/FwImage/fwimage.c index ce3a8127b7..0097a94550 100644 --- a/Tools/Source/TianoTools/FwImage/fwimage.c +++ b/Tools/Source/TianoTools/FwImage/fwimage.c @@ -165,7 +165,7 @@ Returns: ULONG Index; ULONG Index1; BOOLEAN TimeStampPresent; - UINTN RelocSize; + UINTN AllignedRelocSize; UINTN Delta; EFI_IMAGE_SECTION_HEADER *SectionHeader; UINT8 *FileBuffer; @@ -356,7 +356,6 @@ Returns: PeHdr->FileHeader.TimeDateStamp = (UINT32) TimeStamp; } - RelocSize = 0; if (PeHdr->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { Optional32 = (EFI_IMAGE_OPTIONAL_HEADER32 *)&PeHdr->OptionalHeader; Optional32->MajorLinkerVersion = 0; @@ -401,10 +400,29 @@ Returns: if (Optional32->DataDirectory[5].Size != 0) { SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(FileBuffer + DosHdr->e_lfanew + sizeof(UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + PeHdr->FileHeader.SizeOfOptionalHeader); for (Index = 0; Index < PeHdr->FileHeader.NumberOfSections; Index++, SectionHeader++) { + // + // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory + // if (SectionHeader->VirtualAddress == Optional32->DataDirectory[5].VirtualAddress) { - FileLength = SectionHeader->PointerToRawData + Optional32->DataDirectory[5].Size; - FileLength = (FileLength + 7) & 0xfffffff8; - RelocSize = FileLength - SectionHeader->PointerToRawData; + SectionHeader->Misc.VirtualSize = Optional32->DataDirectory[5].Size; + AllignedRelocSize = (Optional32->DataDirectory[5].Size + Optional32->FileAlignment - 1) & (~(Optional32->FileAlignment - 1)); + // + // Check to see if there is zero padding at the end of the base relocations + // + if (AllignedRelocSize < SectionHeader->SizeOfRawData) { + // + // Check to see if the base relocations are at the end of the file + // + if (SectionHeader->PointerToRawData + SectionHeader->SizeOfRawData == Optional32->SizeOfImage) { + // + // All the required conditions are met to strip the zero padding of the end of the base relocations section + // + Optional32->SizeOfImage -= (SectionHeader->SizeOfRawData - AllignedRelocSize); + Optional32->SizeOfInitializedData -= (SectionHeader->SizeOfRawData - AllignedRelocSize); + SectionHeader->SizeOfRawData = AllignedRelocSize; + FileLength = Optional32->SizeOfImage; + } + } } } } @@ -454,26 +472,35 @@ Returns: if (Optional64->DataDirectory[5].Size != 0) { SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(FileBuffer + DosHdr->e_lfanew + sizeof(UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + PeHdr->FileHeader.SizeOfOptionalHeader); for (Index = 0; Index < PeHdr->FileHeader.NumberOfSections; Index++, SectionHeader++) { + // + // Look for the Section Header that starts as the same virtual address as the Base Relocation Data Directory + // if (SectionHeader->VirtualAddress == Optional64->DataDirectory[5].VirtualAddress) { - FileLength = SectionHeader->PointerToRawData + Optional64->DataDirectory[5].Size; - FileLength = (FileLength + 7) & 0xfffffff8; - RelocSize = FileLength - SectionHeader->PointerToRawData; + SectionHeader->Misc.VirtualSize = Optional64->DataDirectory[5].Size; + AllignedRelocSize = (Optional64->DataDirectory[5].Size + Optional64->FileAlignment - 1) & (~(Optional64->FileAlignment - 1)); + // + // Check to see if there is zero padding at the end of the base relocations + // + if (AllignedRelocSize < SectionHeader->SizeOfRawData) { + // + // Check to see if the base relocations are at the end of the file + // + if (SectionHeader->PointerToRawData + SectionHeader->SizeOfRawData == Optional64->SizeOfImage) { + // + // All the required conditions are met to strip the zero padding of the end of the base relocations section + // + Optional64->SizeOfImage -= (SectionHeader->SizeOfRawData - AllignedRelocSize); + Optional64->SizeOfInitializedData -= (SectionHeader->SizeOfRawData - AllignedRelocSize); + SectionHeader->SizeOfRawData = AllignedRelocSize; + FileLength = Optional64->SizeOfImage; + } + } } } } } } - if (RelocSize != 0) { - SectionHeader = (EFI_IMAGE_SECTION_HEADER *)(FileBuffer + DosHdr->e_lfanew + sizeof(UINT32) + sizeof (EFI_IMAGE_FILE_HEADER) + PeHdr->FileHeader.SizeOfOptionalHeader); - for (Index = 0; Index < PeHdr->FileHeader.NumberOfSections; Index++, SectionHeader++) { - if (strcmp(SectionHeader->Name, ".reloc") == 0) { - SectionHeader->Misc.VirtualSize = (RelocSize + 0x1f) & 0xffffffe0; - SectionHeader->SizeOfRawData = RelocSize; - } - } - } - FWriteFile (fpOut, FileBuffer, FileLength); // diff --git a/Tools/Source/TianoTools/PeCoffLoader/BasePeCoff.c b/Tools/Source/TianoTools/PeCoffLoader/BasePeCoff.c index 556b2d73c9..9c25e1f4b8 100644 --- a/Tools/Source/TianoTools/PeCoffLoader/BasePeCoff.c +++ b/Tools/Source/TianoTools/PeCoffLoader/BasePeCoff.c @@ -858,7 +858,7 @@ Returns: Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress); End = PeCoffLoaderImageAddress ( ImageContext, - Section->VirtualAddress + Section->SizeOfRawData - 1 + Section->VirtualAddress + Section->Misc.VirtualSize - 1 ); if (ImageContext->IsTeImage) { Base = (CHAR8 *) ((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN) TeHdr->StrippedSize); diff --git a/Tools/Source/TianoTools/PeiRebase/PeiRebaseExe.c b/Tools/Source/TianoTools/PeiRebase/PeiRebaseExe.c index 35329c74d6..27c646e486 100644 --- a/Tools/Source/TianoTools/PeiRebase/PeiRebaseExe.c +++ b/Tools/Source/TianoTools/PeiRebase/PeiRebaseExe.c @@ -624,12 +624,12 @@ Returns: // Allocate a buffer for the image to be loaded into. // Pe32ImageSize = GetLength (CurrentPe32Section.Pe32Section->CommonHeader.Size) - sizeof (EFI_PE32_SECTION); - MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment)); + MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000)); if (MemoryImagePointer == 0) { Error (NULL, 0, 0, "memory allocation failure", NULL); return EFI_OUT_OF_RESOURCES; } - memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment); + memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000); MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12); @@ -876,13 +876,13 @@ Returns: // // Allocate a buffer for the image to be loaded into. // - MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment)); + MemoryImagePointer = (UINTN) (malloc (Pe32ImageSize + 0x1000)); if (MemoryImagePointer == 0) { Error (NULL, 0, 0, "memory allocation error on rebase of TE image", FileGuidString); free (TEBuffer); return EFI_OUT_OF_RESOURCES; } - memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000 + ImageContext.SectionAlignment); + memset ((void *) MemoryImagePointer, 0, Pe32ImageSize + 0x1000); MemoryImagePointerAligned = (MemoryImagePointer + 0x0FFF) & (-1 << 12); -- 2.39.2