38 UINT32 TeStrippedOffset
41 SectionHeader->VirtualAddress -= TeStrippedOffset;
42 SectionHeader->PointerToRawData -= TeStrippedOffset;
70 UINT32 SectionHeaderOffset;
72 UINT32 HeaderWithoutDataDir;
74 UINTN NumberOfSections;
82 Status = ImageContext->ImageRead (
89 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
90 if (Size != ReadSize) {
97 ImageContext->PeCoffHeaderOffset = 0;
98 if (DosHdr.
e_magic == EFI_IMAGE_DOS_SIGNATURE) {
103 ImageContext->PeCoffHeaderOffset = DosHdr.
e_lfanew;
114 Status = ImageContext->ImageRead (
115 ImageContext->Handle,
116 ImageContext->PeCoffHeaderOffset,
121 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
122 if (Size != ReadSize) {
132 if (Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) {
133 ImageContext->IsTeImage =
TRUE;
134 ImageContext->Machine = Hdr.Te->Machine;
135 ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem);
140 ImageContext->ImageSize = 0;
141 ImageContext->SectionAlignment = 0;
148 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
155 if (Hdr.Te->BaseOfCode <= Hdr.Te->StrippedSize) {
156 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
165 Status = ImageContext->ImageRead (
166 ImageContext->Handle,
167 ImageContext->SizeOfHeaders - 1,
172 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
173 if (Size != ReadSize) {
184 if (((Hdr.Te->DataDirectory[0].Size != 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) ||
185 ((Hdr.Te->DataDirectory[1].Size != 0) && (Hdr.Te->DataDirectory[1].VirtualAddress == 0)))
187 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
190 }
else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
191 ImageContext->IsTeImage =
FALSE;
192 ImageContext->Machine = Hdr.Pe32->FileHeader.Machine;
198 if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES < Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes) {
199 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
209 if (((UINT32)Hdr.Pe32->FileHeader.SizeOfOptionalHeader - HeaderWithoutDataDir) !=
212 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
216 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
sizeof (UINT32) +
sizeof (
EFI_IMAGE_FILE_HEADER) + Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
220 if (Hdr.Pe32->OptionalHeader.SizeOfImage <= SectionHeaderOffset) {
221 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
226 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
233 if (Hdr.Pe32->OptionalHeader.SizeOfHeaders <= SectionHeaderOffset) {
234 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
238 if (Hdr.Pe32->OptionalHeader.SizeOfHeaders >= Hdr.Pe32->OptionalHeader.SizeOfImage) {
239 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
243 if ((Hdr.Pe32->OptionalHeader.SizeOfHeaders - SectionHeaderOffset) /
EFI_IMAGE_SIZEOF_SECTION_HEADER < (UINT32)Hdr.Pe32->FileHeader.NumberOfSections) {
244 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
253 Status = ImageContext->ImageRead (
254 ImageContext->Handle,
255 Hdr.Pe32->OptionalHeader.SizeOfHeaders - 1,
260 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
261 if (Size != ReadSize) {
273 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY < Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes) {
274 if (Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size != 0) {
278 if ((UINT32)(~0) - Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress <
279 Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size)
281 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
290 Status = ImageContext->ImageRead (
291 ImageContext->Handle,
292 Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +
293 Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size - 1,
298 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
299 if (Size != ReadSize) {
311 ImageContext->ImageType = Hdr.Pe32->OptionalHeader.Subsystem;
312 ImageContext->ImageSize = (UINT64)Hdr.Pe32->OptionalHeader.SizeOfImage;
313 ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment;
314 ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders;
315 ImageContext->DllCharacteristics = Hdr.Pe32->OptionalHeader.DllCharacteristics;
320 if (EFI_IMAGE_NUMBER_OF_DIRECTORY_ENTRIES < Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes) {
321 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
331 if (((UINT32)Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader - HeaderWithoutDataDir) !=
334 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
338 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
sizeof (UINT32) +
sizeof (
EFI_IMAGE_FILE_HEADER) + Hdr.Pe32Plus->FileHeader.SizeOfOptionalHeader;
342 if (Hdr.Pe32Plus->OptionalHeader.SizeOfImage <= SectionHeaderOffset) {
343 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
348 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
355 if (Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders <= SectionHeaderOffset) {
356 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
360 if (Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders >= Hdr.Pe32Plus->OptionalHeader.SizeOfImage) {
361 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
365 if ((Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - SectionHeaderOffset) /
EFI_IMAGE_SIZEOF_SECTION_HEADER < (UINT32)Hdr.Pe32Plus->FileHeader.NumberOfSections) {
366 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
375 Status = ImageContext->ImageRead (
376 ImageContext->Handle,
377 Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders - 1,
382 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
383 if (Size != ReadSize) {
395 if (EFI_IMAGE_DIRECTORY_ENTRY_SECURITY < Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes) {
396 if (Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size != 0) {
400 if ((UINT32)(~0) - Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress <
401 Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size)
403 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
412 Status = ImageContext->ImageRead (
413 ImageContext->Handle,
414 Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].VirtualAddress +
415 Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_SECURITY].Size - 1,
420 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
421 if (Size != ReadSize) {
433 ImageContext->ImageType = Hdr.Pe32Plus->OptionalHeader.Subsystem;
434 ImageContext->ImageSize = (UINT64)Hdr.Pe32Plus->OptionalHeader.SizeOfImage;
435 ImageContext->SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment;
436 ImageContext->SizeOfHeaders = Hdr.Pe32Plus->OptionalHeader.SizeOfHeaders;
437 ImageContext->DllCharacteristics = Hdr.Pe32Plus->OptionalHeader.DllCharacteristics;
439 ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;
443 ImageContext->ImageError = IMAGE_ERROR_INVALID_MACHINE_TYPE;
460 if (ImageContext->IsTeImage) {
462 NumberOfSections = (
UINTN)(Hdr.Te->NumberOfSections);
464 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
sizeof (UINT32) +
sizeof (
EFI_IMAGE_FILE_HEADER) + Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
465 NumberOfSections = (
UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
468 for (Index = 0; Index < NumberOfSections; Index++) {
474 Status = ImageContext->ImageRead (
475 ImageContext->Handle,
481 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
482 if (Size != ReadSize) {
492 if (ImageContext->IsTeImage) {
496 if (SectionHeader.SizeOfRawData > 0) {
500 if ((SectionHeader.VirtualAddress < ImageContext->SizeOfHeaders) ||
501 (SectionHeader.PointerToRawData < ImageContext->SizeOfHeaders))
503 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
510 if ((UINT32)(~0) - SectionHeader.PointerToRawData < SectionHeader.SizeOfRawData) {
511 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
521 Status = ImageContext->ImageRead (
522 ImageContext->Handle,
523 SectionHeader.PointerToRawData + SectionHeader.SizeOfRawData - 1,
528 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
529 if (Size != ReadSize) {
581 RETURN_STATUS Status;
589 UINTN DebugDirectoryEntryRva;
590 UINTN DebugDirectoryEntryFileOffset;
591 UINTN SectionHeaderOffset;
594 UINT32 NumberOfRvaAndSizes;
595 UINT32 TeStrippedOffset;
597 if (ImageContext ==
NULL) {
604 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
606 Hdr.Union = &HdrData;
615 if (!(ImageContext->IsTeImage)) {
616 TeStrippedOffset = 0;
621 ImageContext->ImageAddress = Hdr.Pe32->OptionalHeader.
ImageBase;
626 ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.
ImageBase;
630 ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->
ImageBase + TeStrippedOffset);
637 ImageContext->DestinationAddress = 0;
642 ImageContext->DebugDirectoryEntryRva = 0;
643 ImageContext->CodeView =
NULL;
644 ImageContext->PdbPointer =
NULL;
658 ImageContext->RelocationsStripped =
TRUE;
659 }
else if ((ImageContext->IsTeImage) && (Hdr.Te->
DataDirectory[0].Size == 0) && (Hdr.Te->
DataDirectory[0].VirtualAddress == 0)) {
660 ImageContext->RelocationsStripped =
TRUE;
662 ImageContext->RelocationsStripped =
FALSE;
665 if (!(ImageContext->IsTeImage)) {
670 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
671 DebugDirectoryEntry = (
EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
676 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
677 DebugDirectoryEntry = (
EFI_IMAGE_DATA_DIRECTORY *)&(Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_DEBUG]);
680 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_DEBUG) {
681 DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;
688 DebugDirectoryEntryFileOffset = 0;
690 SectionHeaderOffset = ImageContext->PeCoffHeaderOffset +
693 Hdr.Pe32->FileHeader.SizeOfOptionalHeader;
695 for (Index = 0; Index < Hdr.Pe32->FileHeader.NumberOfSections; Index++) {
701 Status = ImageContext->ImageRead (
702 ImageContext->Handle,
708 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
709 if (Size != ReadSize) {
716 if ((DebugDirectoryEntryRva >= SectionHeader.VirtualAddress) &&
717 (DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize))
719 DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva - SectionHeader.VirtualAddress + SectionHeader.PointerToRawData;
726 if (DebugDirectoryEntryFileOffset != 0) {
733 Status = ImageContext->ImageRead (
734 ImageContext->Handle,
735 DebugDirectoryEntryFileOffset + Index,
740 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
741 if (Size != ReadSize) {
754 ImageContext->DebugDirectoryEntryRva = (UINT32)(DebugDirectoryEntryRva + Index);
756 ImageContext->ImageSize += DebugEntry.SizeOfData;
767 if ((NextIndex < DebugDirectoryEntry->Size) &&
768 (DebugEntry.
FileOffset == (DebugDirectoryEntryFileOffset + NextIndex)))
776 if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_EX_DLLCHARACTERISTICS) {
779 Status = ImageContext->ImageRead (
780 ImageContext->Handle,
783 &ImageContext->DllCharacteristicsEx
786 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
787 if (Size != ReadSize) {
801 DebugDirectoryEntryRva = DebugDirectoryEntry->VirtualAddress;
804 DebugDirectoryEntryFileOffset = 0;
812 Status = ImageContext->ImageRead (
813 ImageContext->Handle,
819 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
820 if (Size != ReadSize) {
827 if ((DebugDirectoryEntryRva >= SectionHeader.VirtualAddress) &&
828 (DebugDirectoryEntryRva < SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize))
830 DebugDirectoryEntryFileOffset = DebugDirectoryEntryRva -
831 SectionHeader.VirtualAddress +
832 SectionHeader.PointerToRawData -
857 ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize) - TeStrippedOffset;
863 if (DebugDirectoryEntryFileOffset != 0) {
870 Status = ImageContext->ImageRead (
871 ImageContext->Handle,
872 DebugDirectoryEntryFileOffset + Index,
877 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
878 if (Size != ReadSize) {
886 ImageContext->DebugDirectoryEntryRva = (UINT32)(DebugDirectoryEntryRva + Index);
916 if (Address >= ImageContext->ImageSize + TeStrippedOffset) {
917 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
921 return (CHAR8 *)((
UINTN)ImageContext->ImageAddress + Address - TeStrippedOffset);
960 RETURN_STATUS Status;
975 PHYSICAL_ADDRESS BaseAddress;
976 UINT32 NumberOfRvaAndSizes;
977 UINT32 TeStrippedOffset;
979 ASSERT (ImageContext !=
NULL);
984 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
989 if (ImageContext->RelocationsStripped) {
1000 if (ImageContext->DestinationAddress != 0) {
1001 BaseAddress = ImageContext->DestinationAddress;
1003 BaseAddress = ImageContext->ImageAddress;
1006 if (!(ImageContext->IsTeImage)) {
1008 TeStrippedOffset = 0;
1014 Adjust = (UINT64)BaseAddress - Hdr.Pe32->OptionalHeader.
ImageBase;
1016 Hdr.Pe32->OptionalHeader.
ImageBase = (UINT32)BaseAddress;
1019 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1020 RelocDir = &Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1025 Adjust = (UINT64)BaseAddress - Hdr.Pe32Plus->OptionalHeader.
ImageBase;
1027 Hdr.Pe32Plus->OptionalHeader.
ImageBase = (UINT64)BaseAddress;
1030 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1031 RelocDir = &Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1040 if ((NumberOfRvaAndSizes < EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC)) {
1046 Adjust = (UINT64)(BaseAddress - (Hdr.Te->
ImageBase + TeStrippedOffset));
1048 Hdr.Te->
ImageBase = (UINT64)(BaseAddress - TeStrippedOffset);
1057 if ((RelocDir !=
NULL) && (RelocDir->Size > 0) && ((RelocDir->Size - 1) < (MAX_UINT32 - RelocDir->VirtualAddress))) {
1061 RelocDir->VirtualAddress + RelocDir->Size - 1,
1064 if ((RelocBase ==
NULL) || (RelocBaseEnd ==
NULL) || ((
UINTN)RelocBaseEnd < (
UINTN)RelocBase)) {
1065 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1072 RelocBase = RelocBaseEnd =
NULL;
1075 RelocBaseOrg = RelocBase;
1084 FixupData = ImageContext->FixupData;
1085 while ((
UINTN)RelocBase < (
UINTN)RelocBaseEnd) {
1090 if (RelocBase->SizeOfBlock == 0) {
1091 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1096 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1100 RelocEnd = (UINT16 *)((CHAR8 *)RelocBase + RelocBase->SizeOfBlock);
1101 if ((
UINTN)RelocEnd > (
UINTN)RelocBaseOrg + RelocDir->Size) {
1102 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1107 if (FixupBase ==
NULL) {
1108 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1117 if (Fixup ==
NULL) {
1118 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1122 switch ((*Reloc) >> 12) {
1123 case EFI_IMAGE_REL_BASED_ABSOLUTE:
1126 case EFI_IMAGE_REL_BASED_HIGH:
1127 Fixup16 = (UINT16 *)Fixup;
1128 *Fixup16 = (UINT16)(*Fixup16 + ((UINT16)((UINT32)Adjust >> 16)));
1129 if (FixupData !=
NULL) {
1130 *(UINT16 *)FixupData = *Fixup16;
1131 FixupData = FixupData +
sizeof (UINT16);
1136 case EFI_IMAGE_REL_BASED_LOW:
1137 Fixup16 = (UINT16 *)Fixup;
1138 *Fixup16 = (UINT16)(*Fixup16 + (UINT16)Adjust);
1139 if (FixupData !=
NULL) {
1140 *(UINT16 *)FixupData = *Fixup16;
1141 FixupData = FixupData +
sizeof (UINT16);
1146 case EFI_IMAGE_REL_BASED_HIGHLOW:
1147 Fixup32 = (UINT32 *)Fixup;
1148 *Fixup32 = *Fixup32 + (UINT32)Adjust;
1149 if (FixupData !=
NULL) {
1151 *(UINT32 *)FixupData = *Fixup32;
1152 FixupData = FixupData +
sizeof (UINT32);
1157 case EFI_IMAGE_REL_BASED_DIR64:
1158 Fixup64 = (UINT64 *)Fixup;
1159 *Fixup64 = *Fixup64 + (UINT64)Adjust;
1160 if (FixupData !=
NULL) {
1162 *(UINT64 *)(FixupData) = *Fixup64;
1163 FixupData = FixupData +
sizeof (UINT64);
1176 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1193 ASSERT ((
UINTN)FixupData <= (
UINTN)ImageContext->FixupData + ImageContext->FixupDataSize);
1198 if (ImageContext->DestinationAddress != 0) {
1199 ImageContext->EntryPoint -= (UINT64)ImageContext->ImageAddress;
1200 ImageContext->EntryPoint += (UINT64)ImageContext->DestinationAddress;
1248 RETURN_STATUS Status;
1253 UINTN NumberOfSections;
1260 UINT32 TempDebugEntryRva;
1261 UINT32 NumberOfRvaAndSizes;
1268 UINT32 TeStrippedOffset;
1270 ASSERT (ImageContext !=
NULL);
1275 ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
1292 if (ImageContext->ImageSize < CheckContext.
ImageSize) {
1293 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;
1297 if (ImageContext->ImageAddress == 0) {
1301 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
1314 if (CheckContext.
ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
1315 ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
1323 if (CheckContext.
ImageAddress != ImageContext->ImageAddress) {
1324 ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
1332 if (!(ImageContext->IsTeImage)) {
1333 if ((ImageContext->ImageAddress & (CheckContext.
SectionAlignment - 1)) != 0) {
1334 ImageContext->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;
1342 if (!(ImageContext->IsTeImage)) {
1343 Status = ImageContext->ImageRead (
1344 ImageContext->Handle,
1346 &ImageContext->SizeOfHeaders,
1347 (VOID *)(
UINTN)ImageContext->ImageAddress
1353 (
UINTN)ImageContext->ImageAddress +
1354 ImageContext->PeCoffHeaderOffset +
1357 Hdr.Pe32->FileHeader.SizeOfOptionalHeader
1359 NumberOfSections = (
UINTN)(Hdr.Pe32->FileHeader.NumberOfSections);
1360 TeStrippedOffset = 0;
1362 Status = ImageContext->ImageRead (
1363 ImageContext->Handle,
1365 &ImageContext->SizeOfHeaders,
1366 (
void *)(
UINTN)ImageContext->ImageAddress
1371 (
UINTN)ImageContext->ImageAddress +
1379 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1386 Section = FirstSection;
1387 for (Index = 0; Index < NumberOfSections; Index++) {
1391 Size = (
UINTN)Section->Misc.VirtualSize;
1392 if ((Size == 0) || (Size > Section->SizeOfRawData)) {
1393 Size = (
UINTN)Section->SizeOfRawData;
1400 End =
PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress + Section->Misc.VirtualSize - 1, TeStrippedOffset);
1405 if ((Size > 0) && ((Base ==
NULL) || (End ==
NULL))) {
1406 ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;
1410 if ((Section->SizeOfRawData > 0) && (Base !=
NULL)) {
1411 Status = ImageContext->ImageRead (
1412 ImageContext->Handle,
1413 Section->PointerToRawData - TeStrippedOffset,
1418 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1427 if ((Size < Section->Misc.VirtualSize) && (Base !=
NULL)) {
1428 ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
1440 if (!(ImageContext->IsTeImage)) {
1450 (
UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint,
1459 (
UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint,
1478 if (!(ImageContext->IsTeImage)) {
1483 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1484 DirectoryEntry = (
EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1489 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1490 DirectoryEntry = (
EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
1496 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1497 ImageContext->FixupDataSize = DirectoryEntry->Size /
sizeof (UINT16) *
sizeof (UINT64);
1499 ImageContext->FixupDataSize = 0;
1503 ImageContext->FixupDataSize = DirectoryEntry->Size /
sizeof (UINT16) *
sizeof (UINT64);
1510 ImageContext->FixupData =
NULL;
1515 if (ImageContext->DebugDirectoryEntryRva != 0) {
1518 ImageContext->DebugDirectoryEntryRva,
1521 if (DebugEntry ==
NULL) {
1522 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1526 TempDebugEntryRva = DebugEntry->
RVA;
1527 if ((DebugEntry->
RVA == 0) && (DebugEntry->
FileOffset != 0)) {
1529 if ((
UINTN)Section->SizeOfRawData < Section->Misc.VirtualSize) {
1530 TempDebugEntryRva = Section->VirtualAddress + Section->Misc.VirtualSize;
1532 TempDebugEntryRva = Section->VirtualAddress + Section->SizeOfRawData;
1536 if (TempDebugEntryRva != 0) {
1538 if (ImageContext->CodeView ==
NULL) {
1539 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
1543 if (DebugEntry->
RVA == 0) {
1544 Size = DebugEntry->SizeOfData;
1545 Status = ImageContext->ImageRead (
1546 ImageContext->Handle,
1549 ImageContext->CodeView
1558 ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
1562 DebugEntry->
RVA = TempDebugEntryRva;
1565 switch (*(UINT32 *)ImageContext->CodeView) {
1568 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1577 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1586 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1602 ImageContext->HiiResourceData = 0;
1603 if (!(ImageContext->IsTeImage)) {
1608 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1609 DirectoryEntry = (
EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE];
1614 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1615 DirectoryEntry = (
EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE];
1618 if ((NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_RESOURCE) && (DirectoryEntry->Size != 0)) {
1623 (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries);
1624 if (Offset > DirectoryEntry->Size) {
1625 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1631 for (Index = 0; Index < ResourceDirectory->NumberOfNamedEntries; Index++) {
1632 if (ResourceDirectoryEntry->u1.s.NameIsString) {
1636 if (ResourceDirectoryEntry->u1.s.NameOffset >= DirectoryEntry->Size) {
1637 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1642 String = &ResourceDirectoryString->String[0];
1644 if ((ResourceDirectoryString->Length == 3) &&
1645 (String[0] == L
'H') &&
1646 (String[1] == L
'I') &&
1647 (String[2] == L
'I'))
1652 if (ResourceDirectoryEntry->u2.s.DataIsDirectory) {
1656 if (ResourceDirectoryEntry->u2.s.OffsetToDirectory >= DirectoryEntry->Size) {
1657 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1664 if (Offset > DirectoryEntry->Size) {
1665 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1671 if (ResourceDirectoryEntry->u2.s.DataIsDirectory) {
1675 if (ResourceDirectoryEntry->u2.s.OffsetToDirectory >= DirectoryEntry->Size) {
1676 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1683 if (Offset > DirectoryEntry->Size) {
1684 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1695 if (!ResourceDirectoryEntry->u2.s.DataIsDirectory) {
1696 if (ResourceDirectoryEntry->u2.OffsetToData >= DirectoryEntry->Size) {
1697 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED;
1708 ResourceDirectoryEntry++;
1743 IN PHYSICAL_ADDRESS ImageBase,
1744 IN PHYSICAL_ADDRESS VirtImageBase,
1746 IN VOID *RelocationData
1753 UINT32 NumberOfRvaAndSizes;
1768 RETURN_STATUS Status;
1771 if ((RelocationData ==
NULL) || (ImageBase == 0x0) || (VirtImageBase == 0x0)) {
1775 OldBase = (CHAR8 *)((
UINTN)ImageBase);
1776 NewBase = (CHAR8 *)((
UINTN)VirtImageBase);
1786 if (DosHdr->
e_magic == EFI_IMAGE_DOS_SIGNATURE) {
1798 if (Hdr.Pe32->Signature != EFI_IMAGE_NT_SIGNATURE) {
1809 NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
1815 NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
1827 RelocBaseEnd =
NULL;
1828 if (NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
1829 RelocDir = DataDirectory + EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC;
1830 if ((RelocDir !=
NULL) && (RelocDir->Size > 0)) {
1834 RelocDir->VirtualAddress + RelocDir->Size - 1,
1839 if ((RelocBase ==
NULL) || (RelocBaseEnd ==
NULL) || ((
UINTN)RelocBaseEnd < (
UINTN)RelocBase)) {
1856 ASSERT (RelocBase !=
NULL && RelocBaseEnd !=
NULL);
1866 FixupData = RelocationData;
1867 RelocBaseOrig = RelocBase;
1868 while ((
UINTN)RelocBase < (
UINTN)RelocBaseEnd) {
1872 if ((RelocBase->SizeOfBlock == 0) || (RelocBase->SizeOfBlock > RelocDir->Size)) {
1880 RelocEnd = (UINT16 *)((UINT8 *)RelocBase + RelocBase->SizeOfBlock);
1881 if ((
UINTN)RelocEnd > (
UINTN)RelocBaseOrig + RelocDir->Size) {
1886 if (FixupBase ==
NULL) {
1895 if (Fixup ==
NULL) {
1899 switch ((*Reloc) >> 12) {
1900 case EFI_IMAGE_REL_BASED_ABSOLUTE:
1903 case EFI_IMAGE_REL_BASED_HIGH:
1904 Fixup16 = (UINT16 *)Fixup;
1905 if (*(UINT16 *)FixupData == *Fixup16) {
1906 *Fixup16 = (UINT16)(*Fixup16 + ((UINT16)((UINT32)Adjust >> 16)));
1909 FixupData = FixupData +
sizeof (UINT16);
1912 case EFI_IMAGE_REL_BASED_LOW:
1913 Fixup16 = (UINT16 *)Fixup;
1914 if (*(UINT16 *)FixupData == *Fixup16) {
1915 *Fixup16 = (UINT16)(*Fixup16 + ((UINT16)Adjust & 0xffff));
1918 FixupData = FixupData +
sizeof (UINT16);
1921 case EFI_IMAGE_REL_BASED_HIGHLOW:
1922 Fixup32 = (UINT32 *)Fixup;
1924 if (*(UINT32 *)FixupData == *Fixup32) {
1925 *Fixup32 = *Fixup32 + (UINT32)Adjust;
1928 FixupData = FixupData +
sizeof (UINT32);
1931 case EFI_IMAGE_REL_BASED_DIR64:
1932 Fixup64 = (UINT64 *)Fixup;
1934 if (*(UINT64 *)FixupData == *Fixup64) {
1935 *Fixup64 = *Fixup64 + (UINT64)Adjust;
1938 FixupData = FixupData +
sizeof (UINT64);
1993 IN VOID *FileHandle,
1999 ASSERT (ReadSize !=
NULL);
2000 ASSERT (FileHandle !=
NULL);
2001 ASSERT (Buffer !=
NULL);
2003 CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize);
RETURN_STATUS PeHotRelocateImageEx(IN UINT16 *Reloc, IN OUT CHAR8 *Fixup, IN OUT CHAR8 **FixupData, IN UINT64 Adjust)
RETURN_STATUS PeCoffLoaderRelocateImageEx(IN UINT16 *Reloc, IN OUT CHAR8 *Fixup, IN OUT CHAR8 **FixupData, IN UINT64 Adjust)
BOOLEAN PeCoffLoaderImageFormatSupported(IN UINT16 Machine)
VOID *EFIAPI CopyMem(OUT VOID *DestinationBuffer, IN CONST VOID *SourceBuffer, IN UINTN Length)
VOID *EFIAPI ZeroMem(OUT VOID *Buffer, IN UINTN Length)
RETURN_STATUS EFIAPI PeCoffLoaderLoadImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
RETURN_STATUS EFIAPI PeCoffLoaderRelocateImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
VOID EFIAPI PeCoffLoaderRelocateImageForRuntime(IN PHYSICAL_ADDRESS ImageBase, IN PHYSICAL_ADDRESS VirtImageBase, IN UINTN ImageSize, IN VOID *RelocationData)
VOID PeCoffLoaderAdjustOffsetForTeImage(EFI_IMAGE_SECTION_HEADER *SectionHeader, UINT32 TeStrippedOffset)
RETURN_STATUS EFIAPI PeCoffLoaderUnloadImage(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
VOID * PeCoffLoaderImageAddress(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, IN UINTN Address, IN UINTN TeStrippedOffset)
RETURN_STATUS PeCoffLoaderGetPeHeader(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, OUT EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr)
RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo(IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext)
RETURN_STATUS EFIAPI PeCoffLoaderImageReadFromMemory(IN VOID *FileHandle, IN UINTN FileOffset, IN OUT UINTN *ReadSize, OUT VOID *Buffer)
#define RETURN_LOAD_ERROR
#define RETURN_BUFFER_TOO_SMALL
#define RETURN_ERROR(StatusCode)
#define RETURN_UNSUPPORTED
#define ALIGN_POINTER(Pointer, Alignment)
#define RETURN_INVALID_PARAMETER
#define CODEVIEW_SIGNATURE_NB10
#define EFI_IMAGE_DEBUG_TYPE_CODEVIEW
The Visual C++ debug information.
#define CODEVIEW_SIGNATURE_RSDS
#define EFI_IMAGE_SIZEOF_SECTION_HEADER
#define EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC
#define EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC
#define EFI_IMAGE_FILE_RELOCS_STRIPPED
0x0001 Relocation info stripped from file.
#define CODEVIEW_SIGNATURE_MTOC
UINT32 FileOffset
The file pointer to the debug data.
UINT32 RVA
The address of the debug data when loaded, relative to the image base.
UINT32 e_lfanew
File address of new exe header.
UINT16 e_magic
Magic number.
UINT32 AddressOfEntryPoint
Offset to entry point – from original optional header.
UINT64 ImageBase
From original file header.
EFI_IMAGE_DATA_DIRECTORY DataDirectory[2]
Only base relocation and debug directory.
UINT16 StrippedSize
Number of bytes we removed from the header.
UINT8 NumberOfSections
From the original file header.
BOOLEAN RelocationsStripped
PHYSICAL_ADDRESS ImageAddress