Changeset 58466 in vbox for trunk/src/VBox/Devices/EFI/Firmware/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
- Timestamp:
- Oct 29, 2015 4:30:44 AM (9 years ago)
- Location:
- trunk/src/VBox/Devices/EFI/Firmware
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Devices/EFI/Firmware
- Property svn:mergeinfo changed
/vendor/edk2/current merged: 103769-103776
- Property svn:mergeinfo changed
-
trunk/src/VBox/Devices/EFI/Firmware/MdePkg/Library/BasePeCoffLib/BasePeCoff.c
r58459 r58466 62 62 { 63 63 // 64 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value 65 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the 64 // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value 65 // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the 66 66 // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC 67 67 // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC … … 78 78 79 79 /** 80 Retrieves the PE or TE Header from a PE/COFF or TE image. 80 Retrieves the PE or TE Header from a PE/COFF or TE image. 81 81 82 82 Caution: This function may receive untrusted input. 83 PE/COFF image is external input, so this routine will 84 also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, 85 SizeOfHeader, Section Data Region and Security Data Region be in PE image range. 83 PE/COFF image is external input, so this routine will 84 also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, 85 SizeOfHeader, Section Data Region and Security Data Region be in PE image range. 86 86 87 87 @param ImageContext The context of the image being loaded. … … 212 212 // 213 213 // TE Image Data Directory Entry size is non-zero, but the Data Directory Virtual Address is zero. 214 // This case is not a valid TE image. 214 // This case is not a valid TE image. 215 215 // 216 216 if ((Hdr.Te->DataDirectory[0].Size != 0 && Hdr.Te->DataDirectory[0].VirtualAddress == 0) || … … 236 236 // 237 237 // 2. Check the FileHeader.SizeOfOptionalHeader field. 238 // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so 238 // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so 239 239 // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow. 240 240 // … … 350 350 // 351 351 // 2. Check the FileHeader.SizeOfOptionalHeader field. 352 // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so 352 // OptionalHeader.NumberOfRvaAndSizes is not bigger than 16, so 353 353 // OptionalHeader.NumberOfRvaAndSizes * sizeof (EFI_IMAGE_DATA_DIRECTORY) will not overflow. 354 354 // … … 514 514 // Section data should bigger than the Pe header. 515 515 // 516 if (SectionHeader.VirtualAddress < ImageContext->SizeOfHeaders || 516 if (SectionHeader.VirtualAddress < ImageContext->SizeOfHeaders || 517 517 SectionHeader.PointerToRawData < ImageContext->SizeOfHeaders) { 518 518 ImageContext->ImageError = IMAGE_ERROR_UNSUPPORTED; … … 562 562 Retrieves information about a PE/COFF image. 563 563 564 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, 565 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and 566 DebugDirectoryEntryRva fields of the ImageContext structure. 567 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. 568 If the PE/COFF image accessed through the ImageRead service in the ImageContext 569 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. 570 If any errors occur while computing the fields of ImageContext, 571 then the error status is returned in the ImageError field of ImageContext. 564 Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, 565 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and 566 DebugDirectoryEntryRva fields of the ImageContext structure. 567 If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. 568 If the PE/COFF image accessed through the ImageRead service in the ImageContext 569 structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. 570 If any errors occur while computing the fields of ImageContext, 571 then the error status is returned in the ImageError field of ImageContext. 572 572 If the image is a TE image, then SectionAlignment is set to 0. 573 The ImageRead and Handle fields of ImageContext structure must be valid prior 573 The ImageRead and Handle fields of ImageContext structure must be valid prior 574 574 to invoking this service. 575 575 576 576 Caution: This function may receive untrusted input. 577 PE/COFF image is external input, so this routine will 578 also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, 579 SizeOfHeader, Section Data Region and Security Data Region be in PE image range. 577 PE/COFF image is external input, so this routine will 578 also done many checks in PE image to make sure PE image DosHeader, PeOptionHeader, 579 SizeOfHeader, Section Data Region and Security Data Region be in PE image range. 580 580 581 581 @param ImageContext The pointer to the image context structure that describes the PE/COFF … … 832 832 // values for the corresponding sections. So the ImageSize can be determined 833 833 // by the RVA and the VirtualSize of the last section header in the 834 // Section Table. 834 // Section Table. 835 835 // 836 836 if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) { … … 887 887 PeCoffLoaderImageAddress ( 888 888 IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, 889 IN UINTN Address, 889 IN UINTN Address, 890 890 IN UINTN TeStrippedOffset 891 891 ) … … 909 909 of ImageContext as the relocation base address. The caller must allocate the relocation 910 910 fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function. 911 912 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, 913 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, 914 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of 911 912 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, 913 ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, 914 DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of 915 915 the ImageContext structure must be valid prior to invoking this service. 916 916 917 917 If ImageContext is NULL, then ASSERT(). 918 918 … … 969 969 // 970 970 if (ImageContext->RelocationsStripped) { 971 // Applies additional environment specific actions to relocate fixups 971 // Applies additional environment specific actions to relocate fixups 972 972 // to a PE/COFF image if needed 973 PeCoffLoaderRelocateImageExtraAction (ImageContext); 973 PeCoffLoaderRelocateImageExtraAction (ImageContext); 974 974 return RETURN_SUCCESS; 975 975 } … … 1051 1051 // Set base and end to bypass processing below. 1052 1052 // 1053 RelocBase = RelocBaseEnd = NULL; 1053 RelocBase = RelocBaseEnd = NULL; 1054 1054 } 1055 1055 RelocBaseOrg = RelocBase; … … 1087 1087 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION; 1088 1088 return RETURN_LOAD_ERROR; 1089 } 1089 } 1090 1090 1091 1091 // … … 1174 1174 } 1175 1175 } 1176 1177 // Applies additional environment specific actions to relocate fixups 1176 1177 // Applies additional environment specific actions to relocate fixups 1178 1178 // to a PE/COFF image if needed 1179 1179 PeCoffLoaderRelocateImageExtraAction (ImageContext); 1180 1180 1181 1181 return RETURN_SUCCESS; 1182 1182 } … … 1189 1189 the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function. 1190 1190 The EntryPoint, FixupDataSize, CodeView, PdbPointer and HiiResourceData fields of ImageContext are computed. 1191 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize, 1192 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva 1191 The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize, 1192 DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva 1193 1193 fields of the ImageContext structure must be valid prior to invoking this service. 1194 1194 1195 1195 If ImageContext is NULL, then ASSERT(). 1196 1196 … … 1504 1504 1505 1505 if (TempDebugEntryRva != 0) { 1506 ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva, TeStrippedOffset); 1506 ImageContext->CodeView = PeCoffLoaderImageAddress (ImageContext, TempDebugEntryRva, TeStrippedOffset); 1507 1507 if (ImageContext->CodeView == NULL) { 1508 1508 ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION; … … 1586 1586 if (Base != NULL) { 1587 1587 ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) Base; 1588 Offset = sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * 1588 Offset = sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * 1589 1589 (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries); 1590 1590 if (Offset > DirectoryEntry->Size) { … … 1622 1622 } 1623 1623 ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirectory); 1624 Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + 1624 Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + 1625 1625 sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries); 1626 1626 if (Offset > DirectoryEntry->Size) { … … 1639 1639 } 1640 1640 ResourceDirectory = (EFI_IMAGE_RESOURCE_DIRECTORY *) (Base + ResourceDirectoryEntry->u2.s.OffsetToDirectory); 1641 Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + 1641 Offset = ResourceDirectoryEntry->u2.s.OffsetToDirectory + sizeof (EFI_IMAGE_RESOURCE_DIRECTORY) + 1642 1642 sizeof (EFI_IMAGE_RESOURCE_DIRECTORY_ENTRY) * (ResourceDirectory->NumberOfNamedEntries + ResourceDirectory->NumberOfIdEntries); 1643 1643 if (Offset > DirectoryEntry->Size) { … … 1668 1668 } 1669 1669 } 1670 1670 1671 1671 return Status; 1672 1672 } … … 1675 1675 /** 1676 1676 Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI 1677 runtime. 1678 1679 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase 1680 and ImageSize so the image will execute correctly when the PE/COFF image is mapped 1681 to the address specified by VirtualImageBase. RelocationData must be identical 1682 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure 1677 runtime. 1678 1679 This function reapplies relocation fixups to the PE/COFF image specified by ImageBase 1680 and ImageSize so the image will execute correctly when the PE/COFF image is mapped 1681 to the address specified by VirtualImageBase. RelocationData must be identical 1682 to the FiuxupData buffer from the PE_COFF_LOADER_IMAGE_CONTEXT structure 1683 1683 after this PE/COFF image was relocated with PeCoffLoaderRelocateImage(). 1684 1684 … … 1687 1687 prior to transferring control to a PE/COFF image that is loaded using this library. 1688 1688 1689 @param ImageBase The base address of a PE/COFF image that has been loaded 1689 @param ImageBase The base address of a PE/COFF image that has been loaded 1690 1690 and relocated into system memory. 1691 1691 @param VirtImageBase The request virtual address that the PE/COFF image is to 1692 1692 be fixed up for. 1693 1693 @param ImageSize The size, in bytes, of the PE/COFF image. 1694 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF 1694 @param RelocationData A pointer to the relocation data that was collected when the PE/COFF 1695 1695 image was relocated using PeCoffLoaderRelocateImage(). 1696 1696 1697 1697 **/ 1698 1698 VOID … … 1787 1787 return ; 1788 1788 } 1789 1789 1790 1790 // 1791 1791 // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL. … … 1889 1889 /** 1890 1890 Reads contents of a PE/COFF image from a buffer in system memory. 1891 1892 This is the default implementation of a PE_COFF_LOADER_READ_FILE function 1893 that assumes FileHandle pointer to the beginning of a PE/COFF image. 1894 This function reads contents of the PE/COFF image that starts at the system memory 1895 address specified by FileHandle. The read operation copies ReadSize bytes from the 1896 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer. 1891 1892 This is the default implementation of a PE_COFF_LOADER_READ_FILE function 1893 that assumes FileHandle pointer to the beginning of a PE/COFF image. 1894 This function reads contents of the PE/COFF image that starts at the system memory 1895 address specified by FileHandle. The read operation copies ReadSize bytes from the 1896 PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer. 1897 1897 The size of the buffer actually read is returned in ReadSize. 1898 1898 1899 1899 The caller must make sure the FileOffset and ReadSize within the file scope. 1900 1900 … … 1905 1905 @param FileHandle The pointer to base of the input stream 1906 1906 @param FileOffset Offset into the PE/COFF image to begin the read operation. 1907 @param ReadSize On input, the size in bytes of the requested read operation. 1907 @param ReadSize On input, the size in bytes of the requested read operation. 1908 1908 On output, the number of bytes actually read. 1909 1909 @param Buffer Output buffer that contains the data read from the PE/COFF image. 1910 1910 1911 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into 1911 @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into 1912 1912 the buffer. 1913 1913 **/ … … 1931 1931 /** 1932 1932 Unloads a loaded PE/COFF image from memory and releases its taken resource. 1933 Releases any environment specific resources that were allocated when the image 1934 specified by ImageContext was loaded using PeCoffLoaderLoadImage(). 1935 1933 Releases any environment specific resources that were allocated when the image 1934 specified by ImageContext was loaded using PeCoffLoaderLoadImage(). 1935 1936 1936 For NT32 emulator, the PE/COFF image loaded by system needs to release. 1937 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, 1937 For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, 1938 1938 this function can simply return RETURN_SUCCESS. 1939 1939 1940 1940 If ImageContext is NULL, then ASSERT(). 1941 1941 1942 1942 @param ImageContext The pointer to the image context structure that describes the PE/COFF 1943 1943 image to be unloaded. … … 1952 1952 { 1953 1953 // 1954 // Applies additional environment specific actions to unload a 1954 // Applies additional environment specific actions to unload a 1955 1955 // PE/COFF image if needed 1956 1956 //
Note:
See TracChangeset
for help on using the changeset viewer.