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
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
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
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
// 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
//\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