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
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
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
//\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
+ RelocBase = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext, RelocDir->VirtualAddress, 0);\r
+ RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *) PeCoffLoaderImageAddress (&ImageContext,\r
+ RelocDir->VirtualAddress + RelocDir->Size,\r
+ 0\r
+ );\r
} else {\r
//\r
// Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image.\r
//\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 (RelocBase < 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 (Reloc < 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