basepecoff.c
来自「EFI BIOS是Intel提出的下一代的BIOS标准。这里上传的Edk源代码是」· C语言 代码 · 共 1,355 行 · 第 1/4 页
C
1,355 行
F16 = (UINT16 *) Fixup;
*F16 = (UINT16) (*F16 + (UINT16) Adjust);
if (FixupData != NULL) {
*(UINT16 *) FixupData = *F16;
FixupData = FixupData + sizeof (UINT16);
}
break;
case EFI_IMAGE_REL_BASED_HIGHLOW:
F32 = (UINT32 *) Fixup;
*F32 = *F32 + (UINT32) Adjust;
if (FixupData != NULL) {
FixupData = ALIGN_POINTER (FixupData, sizeof (UINT32));
*(UINT32 *)FixupData = *F32;
FixupData = FixupData + sizeof (UINT32);
}
break;
case EFI_IMAGE_REL_BASED_DIR64:
F64 = (UINT64 *) Fixup;
*F64 = *F64 + (UINT64) Adjust;
if (FixupData != NULL) {
FixupData = ALIGN_POINTER (FixupData, sizeof(UINT64));
*(UINT64 *)(FixupData) = *F64;
FixupData = FixupData + sizeof(UINT64);
}
break;
default:
//
// The common code does not handle some of the stranger IPF relocations
// PeCoffLoaderRelocateImageEx () addes support for these complex fixups
// on IPF and is a No-Op on other archtiectures.
//
Status = PeCoffLoaderRelocateImageEx (Reloc, Fixup, &FixupData, Adjust);
if (RETURN_ERROR (Status)) {
ImageContext->ImageError = IMAGE_ERROR_FAILED_RELOCATION;
return Status;
}
}
//
// Next relocation record
//
Reloc += 1;
}
//
// Next reloc block
//
RelocBase = (EFI_IMAGE_BASE_RELOCATION *) RelocEnd;
}
return RETURN_SUCCESS;
}
/**
Loads a PE/COFF image into memory.
Loads the PE/COFF image accessed through the ImageRead service of ImageContext into the buffer
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.
If ImageContext is NULL, then ASSERT().
@param ImageContext Pointer to the image context structure that describes the PE/COFF
image that is being loaded.
@retval RETURN_SUCCESS The PE/COFF image was loaded into the buffer specified by
the ImageAddress and ImageSize fields of ImageContext.
Extended status information is in the ImageError field of ImageContext.
@retval RETURN_BUFFER_TOO_SMALL The caller did not provide a large enough buffer.
Extended status information is in the ImageError field of ImageContext.
@retval RETURN_LOAD_ERROR The PE/COFF image is an EFI Runtime image with no relocations.
Extended status information is in the ImageError field of ImageContext.
@retval RETURN_INVALID_PARAMETER The image address is invalid.
Extended status information is in the ImageError field of ImageContext.
**/
RETURN_STATUS
EFIAPI
GluePeCoffLoaderLoadImage (
IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext
)
{
RETURN_STATUS Status;
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
PE_COFF_LOADER_IMAGE_CONTEXT CheckContext;
EFI_IMAGE_SECTION_HEADER *FirstSection;
EFI_IMAGE_SECTION_HEADER *Section;
UINTN NumberOfSections;
UINTN Index;
CHAR8 *Base;
CHAR8 *End;
CHAR8 *MaxEnd;
EFI_IMAGE_DATA_DIRECTORY *DirectoryEntry;
EFI_IMAGE_DEBUG_DIRECTORY_ENTRY *DebugEntry;
UINTN Size;
UINT32 TempDebugEntryRva;
UINT32 NumberOfRvaAndSizes;
ASSERT (ImageContext != NULL);
//
// Assume success
//
ImageContext->ImageError = IMAGE_ERROR_SUCCESS;
//
// Copy the provided context info into our local version, get what we
// can from the original image, and then use that to make sure everything
// is legit.
//
CopyMem (&CheckContext, ImageContext, sizeof (PE_COFF_LOADER_IMAGE_CONTEXT));
Status = PeCoffLoaderGetImageInfo (&CheckContext);
if (RETURN_ERROR (Status)) {
return Status;
}
//
// Make sure there is enough allocated space for the image being loaded
//
if (ImageContext->ImageSize < CheckContext.ImageSize) {
ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_SIZE;
return RETURN_BUFFER_TOO_SMALL;
}
if (ImageContext->ImageAddress == 0) {
//
// Image cannot be loaded into 0 address.
//
ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
return RETURN_INVALID_PARAMETER;
}
//
// If there's no relocations, then make sure it's not a runtime driver,
// and that it's being loaded at the linked address.
//
if (CheckContext.RelocationsStripped) {
//
// If the image does not contain relocations and it is a runtime driver
// then return an error.
//
if (CheckContext.ImageType == EFI_IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER) {
ImageContext->ImageError = IMAGE_ERROR_INVALID_SUBSYSTEM;
return RETURN_LOAD_ERROR;
}
//
// If the image does not contain relocations, and the requested load address
// is not the linked address, then return an error.
//
if (CheckContext.ImageAddress != ImageContext->ImageAddress) {
ImageContext->ImageError = IMAGE_ERROR_INVALID_IMAGE_ADDRESS;
return RETURN_INVALID_PARAMETER;
}
}
//
// Make sure the allocated space has the proper section alignment
//
if (!(ImageContext->IsTeImage)) {
if ((ImageContext->ImageAddress & (CheckContext.SectionAlignment - 1)) != 0) {
ImageContext->ImageError = IMAGE_ERROR_INVALID_SECTION_ALIGNMENT;
return RETURN_INVALID_PARAMETER;
}
}
//
// Read the entire PE/COFF or TE header into memory
//
if (!(ImageContext->IsTeImage)) {
Status = ImageContext->ImageRead (
ImageContext->Handle,
0,
&ImageContext->SizeOfHeaders,
(VOID *) (UINTN) ImageContext->ImageAddress
);
Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)((UINTN)ImageContext->ImageAddress + ImageContext->PeCoffHeaderOffset);
FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
(UINTN)ImageContext->ImageAddress +
ImageContext->PeCoffHeaderOffset +
sizeof(UINT32) +
sizeof(EFI_IMAGE_FILE_HEADER) +
Hdr.Pe32->FileHeader.SizeOfOptionalHeader
);
NumberOfSections = (UINTN) (Hdr.Pe32->FileHeader.NumberOfSections);
} else {
Status = ImageContext->ImageRead (
ImageContext->Handle,
0,
&ImageContext->SizeOfHeaders,
(void *)(UINTN)ImageContext->ImageAddress
);
Hdr.Te = (EFI_TE_IMAGE_HEADER *)(UINTN)(ImageContext->ImageAddress);
FirstSection = (EFI_IMAGE_SECTION_HEADER *) (
(UINTN)ImageContext->ImageAddress +
sizeof(EFI_TE_IMAGE_HEADER)
);
NumberOfSections = (UINTN) (Hdr.Te->NumberOfSections);
}
if (RETURN_ERROR (Status)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return RETURN_LOAD_ERROR;
}
//
// Load each section of the image
//
Section = FirstSection;
for (Index = 0, MaxEnd = NULL; Index < NumberOfSections; Index++) {
//
// Compute sections address
//
Base = PeCoffLoaderImageAddress (ImageContext, Section->VirtualAddress);
End = PeCoffLoaderImageAddress (
ImageContext,
Section->VirtualAddress + Section->Misc.VirtualSize - 1
);
if (ImageContext->IsTeImage) {
Base = (CHAR8 *)((UINTN) Base + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
End = (CHAR8 *)((UINTN) End + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize);
}
if (End > MaxEnd) {
MaxEnd = End;
}
//
// If the base start or end address resolved to 0, then fail.
//
if ((Base == NULL) || (End == NULL)) {
ImageContext->ImageError = IMAGE_ERROR_SECTION_NOT_LOADED;
return RETURN_LOAD_ERROR;
}
//
// Read the section
//
Size = (UINTN) Section->Misc.VirtualSize;
if ((Size == 0) || (Size > Section->SizeOfRawData)) {
Size = (UINTN) Section->SizeOfRawData;
}
if (Section->SizeOfRawData) {
if (!(ImageContext->IsTeImage)) {
Status = ImageContext->ImageRead (
ImageContext->Handle,
Section->PointerToRawData,
&Size,
Base
);
} else {
Status = ImageContext->ImageRead (
ImageContext->Handle,
Section->PointerToRawData + sizeof (EFI_TE_IMAGE_HEADER) - (UINTN)Hdr.Te->StrippedSize,
&Size,
Base
);
}
if (RETURN_ERROR (Status)) {
ImageContext->ImageError = IMAGE_ERROR_IMAGE_READ;
return Status;
}
}
//
// If raw size is less then virt size, zero fill the remaining
//
if (Size < Section->Misc.VirtualSize) {
ZeroMem (Base + Size, Section->Misc.VirtualSize - Size);
}
//
// Next Section
//
Section += 1;
}
//
// Get image's entry point
//
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) {
//
// Use PE32 offset
//
ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
ImageContext,
(UINTN)Hdr.Pe32->OptionalHeader.AddressOfEntryPoint
);
} else {
//
// Use PE32+ offset
//
ImageContext->EntryPoint = (PHYSICAL_ADDRESS)(UINTN)PeCoffLoaderImageAddress (
ImageContext,
(UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint
);
}
} else {
ImageContext->EntryPoint = (PHYSICAL_ADDRESS) (
(UINTN)ImageContext->ImageAddress +
(UINTN)Hdr.Te->AddressOfEntryPoint +
(UINTN)sizeof(EFI_TE_IMAGE_HEADER) -
(UINTN)Hdr.Te->StrippedSize
);
}
//
// Determine the size of the fixup data
//
// Per the PE/COFF spec, you can't assume that a given data directory
// is present in the image. You have to check the NumberOfRvaAndSizes in
// 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) {
//
// Use PE32 offset
//
NumberOfRvaAndSizes = Hdr.Pe32->OptionalHeader.NumberOfRvaAndSizes;
DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
} else {
//
// Use PE32+ offset
//
NumberOfRvaAndSizes = Hdr.Pe32Plus->OptionalHeader.NumberOfRvaAndSizes;
DirectoryEntry = (EFI_IMAGE_DATA_DIRECTORY *)&Hdr.Pe32Plus->OptionalHeader.DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC];
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?