X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=MdePkg%2FLibrary%2FBasePeCoffLib%2FBasePeCoff.c;h=60f04703190b147d85266218c92b991355f56358;hb=a4f31dafa7a736b72b9c443e4ac6618382db748f;hp=2ecd76eab47f9481372fee7197dedc3d8912d14d;hpb=9c7790d2cb87584ac2a844f0757142edebd35f71;p=mirror_edk2.git diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c index 2ecd76eab4..60f0470319 100644 --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c @@ -1,10 +1,8 @@ /** @file - Tiano PE/COFF loader. + Base PE/COFF loader supports loading any PE32/PE32+ or TE image, but + only supports relocating IA32, x64, IPF, and EBC images. - This PE/COFF loader supports loading any PE32 or PE32+ image type, but - only supports relocating IA32, X64, IPF, and EBC images. - - Copyright (c) 2006, Intel Corporation + Copyright (c) 2006 - 2008, Intel Corporation All rights reserved. This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -13,70 +11,38 @@ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. - Module Name: PeCoffLoader.c - -**/ - - -/** - Performs an Itanium-based specific relocation fixup and is a no-op on other - instruction sets. - - @param Reloc Pointer to the relocation record. - @param Fixup Pointer to the address to fix up. - @param FixupData Pointer to a buffer to log the fixups. - @param Adjust The offset to adjust the fixup. - - @return Status code. - -**/ -RETURN_STATUS -PeCoffLoaderRelocateImageEx ( - IN UINT16 *Reloc, - IN OUT CHAR8 *Fixup, - IN OUT CHAR8 **FixupData, - IN UINT64 Adjust - ); - - -/** - Performs an Itanium-based specific re-relocation fixup and is a no-op on other - instruction sets. This is used to re-relocated the image into the EFI virtual - space for runtime calls. - - @param Reloc Pointer to the relocation record. - @param Fixup Pointer to the address to fix up. - @param FixupData Pointer to a buffer to log the fixups. - @param Adjust The offset to adjust the fixup. - - @return Status code. - **/ -RETURN_STATUS -PeHotRelocateImageEx ( - IN UINT16 *Reloc, - IN OUT CHAR8 *Fixup, - IN OUT CHAR8 **FixupData, - IN UINT64 Adjust - ); +#include "BasePeCoffLibInternals.h" /** - Returns TRUE if the machine type of PE/COFF image is supported. Supported - does not mean the image can be executed it means the PE/COFF loader supports - loading and relocating of the image type. It's up to the caller to support - the entry point. + Retrieves the magic value from the PE/COFF header. - @param Machine Machine type from the PE Header. + @param Hdr The buffer in which to return the PE32, PE32+, or TE header. - @return TRUE if this PE/COFF loader can load the image + @return EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - Image is PE32 + @return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - Image is PE32+ **/ -BOOLEAN -PeCoffLoaderImageFormatSupported ( - IN UINT16 Machine - ); - +UINT16 +PeCoffLoaderGetPeHeaderMagicValue ( + IN EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr + ) +{ + // + // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value + // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the + // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC + // then override the returned value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC + // + if (Hdr.Pe32->FileHeader.Machine == EFI_IMAGE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + return EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC; + } + // + // Return the magic value from the PC/COFF Optional Header + // + return Hdr.Pe32->OptionalHeader.Magic; +} /** @@ -98,6 +64,7 @@ PeCoffLoaderGetPeHeader ( RETURN_STATUS Status; EFI_IMAGE_DOS_HEADER DosHdr; UINTN Size; + UINT16 Magic; // // Read the DOS image header to check for it's existance @@ -148,15 +115,21 @@ PeCoffLoaderGetPeHeader ( ImageContext->IsTeImage = TRUE; ImageContext->Machine = Hdr.Te->Machine; ImageContext->ImageType = (UINT16)(Hdr.Te->Subsystem); + // + // For TeImage, SectionAlignment is undefined to be set to Zero + // ImageSize can be calculated. + // ImageContext->ImageSize = 0; - ImageContext->SectionAlignment = 4096; + ImageContext->SectionAlignment = 0; ImageContext->SizeOfHeaders = sizeof (EFI_TE_IMAGE_HEADER) + (UINTN)Hdr.Te->BaseOfCode - (UINTN)Hdr.Te->StrippedSize; } else if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) { ImageContext->IsTeImage = FALSE; ImageContext->Machine = Hdr.Pe32->FileHeader.Machine; - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr); + + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -165,7 +138,7 @@ PeCoffLoaderGetPeHeader ( ImageContext->SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment; ImageContext->SizeOfHeaders = Hdr.Pe32->OptionalHeader.SizeOfHeaders; - } else if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { + } else if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC) { // // Use PE32+ offset // @@ -199,13 +172,17 @@ PeCoffLoaderGetPeHeader ( /** Retrieves information about a PE/COFF image. - Computes the PeCoffHeaderOffset, ImageAddress, ImageSize, DestinationAddress, CodeView, - PdbPointer, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva - fields of the ImageContext structure. If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. - If the PE/COFF image accessed through the ImageRead service in the ImageContext structure is not - a supported PE/COFF image type, then return RETURN_UNSUPPORTED. If any errors occur while - computing the fields of ImageContext, then the error status is returned in the ImageError field of - ImageContext. + Computes the PeCoffHeaderOffset, IsTeImage, ImageType, ImageAddress, ImageSize, + DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and + DebugDirectoryEntryRva fields of the ImageContext structure. + If ImageContext is NULL, then return RETURN_INVALID_PARAMETER. + If the PE/COFF image accessed through the ImageRead service in the ImageContext + structure is not a supported PE/COFF image type, then return RETURN_UNSUPPORTED. + If any errors occur while computing the fields of ImageContext, + then the error status is returned in the ImageError field of ImageContext. + If the image is a TE image, then SectionAlignment is set to 0. + The ImageRead and Handle fields of ImageContext structure must be valid prior + to invoking this service. @param ImageContext Pointer to the image context structure that describes the PE/COFF image that needs to be examined by this function. @@ -218,7 +195,7 @@ PeCoffLoaderGetPeHeader ( RETURN_STATUS EFIAPI PeCoffLoaderGetImageInfo ( - IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext + IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ) { RETURN_STATUS Status; @@ -233,8 +210,9 @@ PeCoffLoaderGetImageInfo ( EFI_IMAGE_SECTION_HEADER SectionHeader; EFI_IMAGE_DEBUG_DIRECTORY_ENTRY DebugEntry; UINT32 NumberOfRvaAndSizes; + UINT16 Magic; - if (NULL == ImageContext) { + if (ImageContext == NULL) { return RETURN_INVALID_PARAMETER; } // @@ -248,11 +226,13 @@ PeCoffLoaderGetImageInfo ( return Status; } + Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr); + // // Retrieve the base address of the image // if (!(ImageContext->IsTeImage)) { - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -264,7 +244,7 @@ PeCoffLoaderGetImageInfo ( ImageContext->ImageAddress = Hdr.Pe32Plus->OptionalHeader.ImageBase; } } else { - ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase); + ImageContext->ImageAddress = (PHYSICAL_ADDRESS)(Hdr.Te->ImageBase + Hdr.Te->StrippedSize - sizeof (EFI_TE_IMAGE_HEADER)); } // @@ -292,12 +272,14 @@ PeCoffLoaderGetImageInfo ( // if ((!(ImageContext->IsTeImage)) && ((Hdr.Pe32->FileHeader.Characteristics & EFI_IMAGE_FILE_RELOCS_STRIPPED) != 0)) { ImageContext->RelocationsStripped = TRUE; + } else if ((ImageContext->IsTeImage) && (Hdr.Te->DataDirectory[0].Size == 0) && (Hdr.Te->DataDirectory[0].VirtualAddress == 0)) { + ImageContext->RelocationsStripped = TRUE; } else { ImageContext->RelocationsStripped = FALSE; } if (!(ImageContext->IsTeImage)) { - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -356,7 +338,7 @@ PeCoffLoaderGetImageInfo ( } if (DebugDirectoryEntryFileOffset != 0) { - for (Index = 0; Index < DebugDirectoryEntry->Size; Index++) { + for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) { // // Read next debug directory entry // @@ -371,9 +353,8 @@ PeCoffLoaderGetImageInfo ( ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ; return Status; } - if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) { - ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)); + ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index); if (DebugEntry.RVA == 0 && DebugEntry.FileOffset != 0) { ImageContext->ImageSize += DebugEntry.SizeOfData; } @@ -434,18 +415,17 @@ PeCoffLoaderGetImageInfo ( // section headers in the Section Table must appear in order of the RVA // values for the corresponding sections. So the ImageSize can be determined // by the RVA and the VirtualSize of the last section header in the - // Section Table. + // Section Table. // if ((++Index) == (UINTN)Hdr.Te->NumberOfSections) { - ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize + - ImageContext->SectionAlignment - 1) & ~(ImageContext->SectionAlignment - 1); + ImageContext->ImageSize = (SectionHeader.VirtualAddress + SectionHeader.Misc.VirtualSize); } SectionHeaderOffset += sizeof (EFI_IMAGE_SECTION_HEADER); } if (DebugDirectoryEntryFileOffset != 0) { - for (Index = 0; Index < DebugDirectoryEntry->Size; Index++) { + for (Index = 0; Index < DebugDirectoryEntry->Size; Index += sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)) { // // Read next debug directory entry // @@ -462,7 +442,7 @@ PeCoffLoaderGetImageInfo ( } if (DebugEntry.Type == EFI_IMAGE_DEBUG_TYPE_CODEVIEW) { - ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index * sizeof (EFI_IMAGE_DEBUG_DIRECTORY_ENTRY)); + ImageContext->DebugDirectoryEntryRva = (UINT32) (DebugDirectoryEntryRva + Index); return RETURN_SUCCESS; } } @@ -507,6 +487,12 @@ PeCoffLoaderImageAddress ( ImageContext as the relocation base address. Otherwise, use the DestinationAddress field of ImageContext as the relocation base address. The caller must allocate the relocation fixup log buffer and fill in the FixupData field of ImageContext prior to calling this function. + + The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, + ImageSize, DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, + DebugDirectoryEntryRva, EntryPoint, FixupDataSize, CodeView, PdbPointer, and FixupData of + the ImageContext structure must be valid prior to invoking this service. + If ImageContext is NULL, then ASSERT(). @param ImageContext Pointer to the image context structure that describes the PE/COFF @@ -536,12 +522,13 @@ PeCoffLoaderRelocateImage ( UINT16 *RelocEnd; CHAR8 *Fixup; CHAR8 *FixupBase; - UINT16 *F16; - UINT32 *F32; - UINT64 *F64; + UINT16 *Fixup16; + UINT32 *Fixup32; + UINT64 *Fixup64; CHAR8 *FixupData; PHYSICAL_ADDRESS BaseAddress; UINT32 NumberOfRvaAndSizes; + UINT16 Magic; ASSERT (ImageContext != NULL); @@ -569,7 +556,10 @@ PeCoffLoaderRelocateImage ( if (!(ImageContext->IsTeImage)) { Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset); - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + + Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr); + + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -610,8 +600,8 @@ PeCoffLoaderRelocateImage ( } } else { Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress); - Adjust = (UINT64) (BaseAddress - Hdr.Te->ImageBase); - Hdr.Te->ImageBase = (UINT64) (BaseAddress); + Adjust = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER) - Hdr.Te->ImageBase); + Hdr.Te->ImageBase = (UINT64) (BaseAddress - Hdr.Te->StrippedSize + sizeof (EFI_TE_IMAGE_HEADER)); // // Find the relocation block @@ -662,39 +652,39 @@ PeCoffLoaderRelocateImage ( break; case EFI_IMAGE_REL_BASED_HIGH: - F16 = (UINT16 *) Fixup; - *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16))); + Fixup16 = (UINT16 *) Fixup; + *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16))); if (FixupData != NULL) { - *(UINT16 *) FixupData = *F16; + *(UINT16 *) FixupData = *Fixup16; FixupData = FixupData + sizeof (UINT16); } break; case EFI_IMAGE_REL_BASED_LOW: - F16 = (UINT16 *) Fixup; - *F16 = (UINT16) (*F16 + (UINT16) Adjust); + Fixup16 = (UINT16 *) Fixup; + *Fixup16 = (UINT16) (*Fixup16 + (UINT16) Adjust); if (FixupData != NULL) { - *(UINT16 *) FixupData = *F16; + *(UINT16 *) FixupData = *Fixup16; FixupData = FixupData + sizeof (UINT16); } break; case EFI_IMAGE_REL_BASED_HIGHLOW: - F32 = (UINT32 *) Fixup; - *F32 = *F32 + (UINT32) Adjust; + Fixup32 = (UINT32 *) Fixup; + *Fixup32 = *Fixup32 + (UINT32) Adjust; if (FixupData != NULL) { FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32)); - *(UINT32 *)FixupData = *F32; + *(UINT32 *)FixupData = *Fixup32; FixupData = FixupData + sizeof (UINT32); } break; case EFI_IMAGE_REL_BASED_DIR64: - F64 = (UINT64 *) Fixup; - *F64 = *F64 + (UINT64) Adjust; + Fixup64 = (UINT64 *) Fixup; + *Fixup64 = *Fixup64 + (UINT64) Adjust; if (FixupData != NULL) { FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64)); - *(UINT64 *)(FixupData) = *F64; + *(UINT64 *)(FixupData) = *Fixup64; FixupData = FixupData + sizeof(UINT64); } break; @@ -724,6 +714,13 @@ PeCoffLoaderRelocateImage ( RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd; } + // + // Adjust the EntryPoint to match the linked-to address + // + if (ImageContext->DestinationAddress != 0) { + ImageContext->EntryPoint -= (UINT64) ImageContext->ImageAddress; + ImageContext->EntryPoint += (UINT64) ImageContext->DestinationAddress; + } return RETURN_SUCCESS; } @@ -734,6 +731,10 @@ PeCoffLoaderRelocateImage ( specified by the ImageAddress and ImageSize fields of ImageContext. The caller must allocate the load buffer and fill in the ImageAddress and ImageSize fields prior to calling this function. The EntryPoint, FixupDataSize, CodeView, and PdbPointer fields of ImageContext are computed. + The ImageRead, Handle, PeCoffHeaderOffset, IsTeImage, Machine, ImageType, ImageAddress, ImageSize, + DestinationAddress, RelocationsStripped, SectionAlignment, SizeOfHeaders, and DebugDirectoryEntryRva + fields of the ImageContext structure must be valid prior to invoking this service. + If ImageContext is NULL, then ASSERT(). @param ImageContext Pointer to the image context structure that describes the PE/COFF @@ -771,6 +772,7 @@ PeCoffLoaderLoadImage ( UINTN Size; UINT32 TempDebugEntryRva; UINT32 NumberOfRvaAndSizes; + UINT16 Magic; ASSERT (ImageContext != NULL); @@ -918,7 +920,7 @@ PeCoffLoaderLoadImage ( Size = (UINTN) Section->SizeOfRawData; } - if (Section->SizeOfRawData) { + if (Section->SizeOfRawData > 0) { if (!(ImageContext->IsTeImage)) { Status = ImageContext->ImageRead ( ImageContext->Handle, @@ -958,11 +960,12 @@ PeCoffLoaderLoadImage ( // // Get image's entry point // + Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr); if (!(ImageContext->IsTeImage)) { // // Sizes of AddressOfEntryPoint are different so we need to do this safely // - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -996,7 +999,7 @@ PeCoffLoaderLoadImage ( // the optional header to verify a desired directory entry is there. // if (!(ImageContext->IsTeImage)) { - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -1124,17 +1127,20 @@ PeCoffLoaderLoadImage ( /** Reapply fixups on a fixed up PE32/PE32+ image to allow virutal calling at EFI - runtime. - + runtime. + PE_COFF_LOADER_IMAGE_CONTEXT.FixupData stores information needed to reapply the fixups with a virtual mapping. - @param ImageBase Base address of relocated image - @param VirtImageBase Virtual mapping for ImageBase - @param ImageSize Size of the image to relocate - @param RelocationData Location to place results of read - + @param ImageBase Base address of a PE/COFF image that has been loaded + and relocated into system memory. + @param VirtImageBase The request virtual address that the PE/COFF image is to + be fixed up for. + @param ImageSize The size, in bytes, of the PE/COFF image. + @param RelocationData A pointer to the relocation data that was collected when the PE/COFF + image was relocated using PeCoffLoaderRelocateImage(). + **/ VOID EFIAPI @@ -1158,12 +1164,13 @@ PeCoffLoaderRelocateImageForRuntime ( UINT16 *RelocEnd; CHAR8 *Fixup; CHAR8 *FixupBase; - UINT16 *F16; - UINT32 *F32; - UINT64 *F64; + UINT16 *Fixup16; + UINT32 *Fixup32; + UINT64 *Fixup64; CHAR8 *FixupData; UINTN Adjust; RETURN_STATUS Status; + UINT16 Magic; OldBase = (CHAR8 *)((UINTN)ImageBase); NewBase = (CHAR8 *)((UINTN)VirtImageBase); @@ -1192,10 +1199,9 @@ PeCoffLoaderRelocateImageForRuntime ( return ; } - // - // Get some data from the PE type dependent data - // - if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + Magic = PeCoffLoaderGetPeHeaderMagicValue (Hdr); + + if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { // // Use PE32 offset // @@ -1222,12 +1228,15 @@ PeCoffLoaderRelocateImageForRuntime ( RelocBaseEnd = (EFI_IMAGE_BASE_RELOCATION *)(UINTN)(ImageBase + RelocDir->VirtualAddress + RelocDir->Size); } else { // - // Cannot find relocations, cannot continue + // Cannot find relocations, cannot continue to relocate the image, ASSERT for this invalid image. // ASSERT (FALSE); return ; } - + + // + // ASSERT for the invalid image when RelocBase and RelocBaseEnd are both NULL. + // ASSERT (RelocBase != NULL && RelocBaseEnd != NULL); // @@ -1256,38 +1265,38 @@ PeCoffLoaderRelocateImageForRuntime ( break; case EFI_IMAGE_REL_BASED_HIGH: - F16 = (UINT16 *) Fixup; - if (*(UINT16 *) FixupData == *F16) { - *F16 = (UINT16) (*F16 + ((UINT16) ((UINT32) Adjust >> 16))); + Fixup16 = (UINT16 *) Fixup; + if (*(UINT16 *) FixupData == *Fixup16) { + *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) ((UINT32) Adjust >> 16))); } FixupData = FixupData + sizeof (UINT16); break; case EFI_IMAGE_REL_BASED_LOW: - F16 = (UINT16 *) Fixup; - if (*(UINT16 *) FixupData == *F16) { - *F16 = (UINT16) (*F16 + ((UINT16) Adjust & 0xffff)); + Fixup16 = (UINT16 *) Fixup; + if (*(UINT16 *) FixupData == *Fixup16) { + *Fixup16 = (UINT16) (*Fixup16 + ((UINT16) Adjust & 0xffff)); } FixupData = FixupData + sizeof (UINT16); break; case EFI_IMAGE_REL_BASED_HIGHLOW: - F32 = (UINT32 *) Fixup; + Fixup32 = (UINT32 *) Fixup; FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32)); - if (*(UINT32 *) FixupData == *F32) { - *F32 = *F32 + (UINT32) Adjust; + if (*(UINT32 *) FixupData == *Fixup32) { + *Fixup32 = *Fixup32 + (UINT32) Adjust; } FixupData = FixupData + sizeof (UINT32); break; case EFI_IMAGE_REL_BASED_DIR64: - F64 = (UINT64 *)Fixup; + Fixup64 = (UINT64 *)Fixup; FixupData = ALIGN_POINTER (FixupData, sizeof (UINT64)); - if (*(UINT64 *) FixupData == *F64) { - *F64 = *F64 + (UINT64)Adjust; + if (*(UINT64 *) FixupData == *Fixup64) { + *Fixup64 = *Fixup64 + (UINT64)Adjust; } FixupData = FixupData + sizeof (UINT64); @@ -1295,7 +1304,7 @@ PeCoffLoaderRelocateImageForRuntime ( case EFI_IMAGE_REL_BASED_HIGHADJ: // - // Not implemented, but not used in EFI 1.0 + // Not valid Relocation type for UEFI image, ASSERT // ASSERT (FALSE); break; @@ -1323,15 +1332,26 @@ PeCoffLoaderRelocateImageForRuntime ( /** - ImageRead function that operates on a memory buffer whos base is passed into - FileHandle. - - @param FileHandle Ponter to baes of the input stream - @param FileOffset Offset to the start of the buffer - @param ReadSize Number of bytes to copy into the buffer - @param Buffer Location to place results of read - - @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into + Reads contents of a PE/COFF image from a buffer in system memory. + + This is the default implementation of a PE_COFF_LOADER_READ_FILE function + that assumes FileHandle pointer to the beginning of a PE/COFF image. + This function reads contents of the PE/COFF image that starts at the system memory + address specified by FileHandle. The read operation copies ReadSize bytes from the + PE/COFF image starting at byte offset FileOffset into the buffer specified by Buffer. + The size of the buffer actually read is returned in ReadSize. + + If FileHandle is NULL, then ASSERT(). + If ReadSize is NULL, then ASSERT(). + If Buffer is NULL, then ASSERT(). + + @param FileHandle Pointer to base of the input stream + @param FileOffset Offset into the PE/COFF image to begin the read operation. + @param ReadSize On input, the size in bytes of the requested read operation. + On output, the number of bytes actually read. + @param Buffer Output buffer that contains the data read from the PE/COFF image. + + @retval RETURN_SUCCESS Data is read from FileOffset from the Handle into the buffer. **/ RETURN_STATUS @@ -1343,7 +1363,35 @@ PeCoffLoaderImageReadFromMemory ( OUT VOID *Buffer ) { + ASSERT (ReadSize != NULL); + ASSERT (FileHandle != NULL); + ASSERT (Buffer != NULL); + CopyMem (Buffer, ((UINT8 *)FileHandle) + FileOffset, *ReadSize); return RETURN_SUCCESS; } +/** + Unloads a loaded PE/COFF image from memory and releases its taken resource. + Releases any environment specific resources that were allocated when the image + specified by ImageContext was loaded using PeCoffLoaderLoadImage(). + + For NT32 emulator, the PE/COFF image loaded by system needs to release. + For real platform, the PE/COFF image loaded by Core doesn't needs to be unloaded, + this function can simply return RETURN_SUCCESS. + + If ImageContext is NULL, then ASSERT(). + + @param ImageContext Pointer to the image context structure that describes the PE/COFF + image to be unloaded. + + @retval RETURN_SUCCESS The PE/COFF image was unloaded successfully. +**/ +RETURN_STATUS +EFIAPI +PeCoffLoaderUnloadImage ( + IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext + ) +{ + return RETURN_SUCCESS; +}