mirror of
https://github.com/acidanthera/audk.git
synced 2025-07-26 15:14:02 +02:00
EmulatorPkg: Fix some image loader related issues
This commit is contained in:
parent
91526b5ef2
commit
f4778d6fdd
@ -96,7 +96,7 @@ EFI_STATUS
|
|||||||
typedef
|
typedef
|
||||||
VOID
|
VOID
|
||||||
(EFIAPI *EMU_PE_COFF_RELOCATE_EXTRA_ACTION)(
|
(EFIAPI *EMU_PE_COFF_RELOCATE_EXTRA_ACTION)(
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
);
|
);
|
||||||
|
|
||||||
typedef
|
typedef
|
||||||
|
@ -64,7 +64,7 @@ DxeEmuUefiImageLibExtraActionConstructor (
|
|||||||
VOID
|
VOID
|
||||||
EFIAPI
|
EFIAPI
|
||||||
UefiImageLoaderRelocateImageExtraAction (
|
UefiImageLoaderRelocateImageExtraAction (
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (mThunk != NULL) {
|
if (mThunk != NULL) {
|
||||||
|
@ -67,7 +67,7 @@ EmuUefiImageGetThunkStucture (
|
|||||||
VOID
|
VOID
|
||||||
EFIAPI
|
EFIAPI
|
||||||
UefiImageLoaderRelocateImageExtraAction (
|
UefiImageLoaderRelocateImageExtraAction (
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
if (EMU_MAGIC_PAGE ()->Thunk == NULL) {
|
if (EMU_MAGIC_PAGE ()->Thunk == NULL) {
|
||||||
|
@ -76,7 +76,7 @@ GasketSecUefiImageGetEntryPoint (
|
|||||||
VOID
|
VOID
|
||||||
EFIAPI
|
EFIAPI
|
||||||
GasketSecUefiImageRelocateImageExtraAction (
|
GasketSecUefiImageRelocateImageExtraAction (
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
);
|
);
|
||||||
|
|
||||||
VOID
|
VOID
|
||||||
|
@ -746,12 +746,15 @@ SecUefiImageGetEntryPoint (
|
|||||||
EFI_STATUS Status;
|
EFI_STATUS Status;
|
||||||
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
|
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
|
||||||
|
|
||||||
Status = UefiImageInitializeContext (&ImageContext, Pe32Data, Pe32Size);
|
Status = UefiImageInitializeContext (&ImageContext, Pe32Data, Pe32Size);
|
||||||
if (EFI_ERROR (Status)) {
|
if (EFI_ERROR (Status)) {
|
||||||
return Status;
|
return Status;
|
||||||
}
|
}
|
||||||
|
//
|
||||||
// FIXME: Why cannot the Image be in-place already?
|
// FIXME: This modifies the FD data (which is not possible on real platforms)
|
||||||
|
// and thus re-relocation (i.e., PEIM shadowing) fails badly due to
|
||||||
|
// not updating ImageBase.
|
||||||
|
//
|
||||||
Status = UefiImageRelocateImageInplaceForExecution (&ImageContext);
|
Status = UefiImageRelocateImageInplaceForExecution (&ImageContext);
|
||||||
if (EFI_ERROR (Status)) {
|
if (EFI_ERROR (Status)) {
|
||||||
return Status;
|
return Status;
|
||||||
@ -872,7 +875,7 @@ IsPdbFile (
|
|||||||
|
|
||||||
void
|
void
|
||||||
PrintLoadAddress (
|
PrintLoadAddress (
|
||||||
IN UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
EFI_STATUS Status;
|
EFI_STATUS Status;
|
||||||
@ -925,7 +928,7 @@ SecGdbScriptBreak (
|
|||||||
**/
|
**/
|
||||||
VOID
|
VOID
|
||||||
GdbScriptAddImage (
|
GdbScriptAddImage (
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
EFI_STATUS Status;
|
EFI_STATUS Status;
|
||||||
@ -934,7 +937,7 @@ GdbScriptAddImage (
|
|||||||
|
|
||||||
PrintLoadAddress (ImageContext);
|
PrintLoadAddress (ImageContext);
|
||||||
|
|
||||||
Status = UefiImageGetSymbolsPath ((ImageContext, &PdbPath,) &PdbPathSize);
|
Status = UefiImageGetSymbolsPath (ImageContext, &PdbPath, &PdbPathSize);
|
||||||
if (EFI_ERROR (Status)) {
|
if (EFI_ERROR (Status)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -987,7 +990,7 @@ GdbScriptAddImage (
|
|||||||
VOID
|
VOID
|
||||||
EFIAPI
|
EFIAPI
|
||||||
SecUefiImageRelocateImageExtraAction (
|
SecUefiImageRelocateImageExtraAction (
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
GdbScriptAddImage (ImageContext);
|
GdbScriptAddImage (ImageContext);
|
||||||
|
@ -267,7 +267,7 @@ SecUefiImageGetEntryPoint (
|
|||||||
VOID
|
VOID
|
||||||
EFIAPI
|
EFIAPI
|
||||||
SecUefiImageRelocateImageExtraAction (
|
SecUefiImageRelocateImageExtraAction (
|
||||||
IN OUT UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
||||||
);
|
);
|
||||||
|
|
||||||
VOID
|
VOID
|
||||||
|
@ -801,37 +801,21 @@ SecUefiImageGetEntryPoint (
|
|||||||
IN OUT VOID **EntryPoint
|
IN OUT VOID **EntryPoint
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
EFI_STATUS Status;
|
EFI_STATUS Status;
|
||||||
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
|
UEFI_IMAGE_LOADER_IMAGE_CONTEXT ImageContext;
|
||||||
VOID *Dest;
|
|
||||||
UINT32 DestSize;
|
|
||||||
|
|
||||||
Status = UefiImageInitializeContext (&ImageContext, Pe32Data, Pe32Size);
|
Status = UefiImageInitializeContext (&ImageContext, Pe32Data, Pe32Size);
|
||||||
if (EFI_ERROR (Status)) {
|
if (EFI_ERROR (Status)) {
|
||||||
return Status;
|
return Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Allocate space in NT (not emulator) memory with ReadWrite and Execute attribute.
|
// FIXME: This modifies the FD data (which is not possible on real platforms)
|
||||||
// Extra space is for alignment
|
// and thus re-relocation (i.e., PEIM shadowing) fails badly due to
|
||||||
|
// not updating ImageBase.
|
||||||
//
|
//
|
||||||
Status = UefiImageLoaderGetDestinationSize(&ImageContext, &DestSize);
|
Status = UefiImageRelocateImageInplaceForExecution (&ImageContext);
|
||||||
if (EFI_ERROR (Status)) {
|
if (RETURN_ERROR (Status)) {
|
||||||
return Status;
|
DEBUG_RAISE ();
|
||||||
}
|
|
||||||
|
|
||||||
Dest = VirtualAlloc (NULL, (SIZE_T) DestSize, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
|
|
||||||
if (Dest == NULL) {
|
|
||||||
return EFI_OUT_OF_RESOURCES;
|
|
||||||
}
|
|
||||||
|
|
||||||
Status = UefiImageLoadImage (&ImageContext, Dest, DestSize);
|
|
||||||
if (EFI_ERROR (Status)) {
|
|
||||||
return Status;
|
|
||||||
}
|
|
||||||
|
|
||||||
Status = UefiImageRelocateImage (&ImageContext, (UINTN) Dest, NULL, 0);
|
|
||||||
if (EFI_ERROR (Status)) {
|
|
||||||
return Status;
|
return Status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -842,8 +826,8 @@ SecUefiImageGetEntryPoint (
|
|||||||
|
|
||||||
CHAR16 *
|
CHAR16 *
|
||||||
AsciiToUnicode (
|
AsciiToUnicode (
|
||||||
IN CHAR8 *Ascii,
|
IN CONST CHAR8 *Ascii,
|
||||||
IN UINTN *StrLen OPTIONAL
|
IN UINTN *StrLen OPTIONAL
|
||||||
)
|
)
|
||||||
|
|
||||||
/*++
|
/*++
|
||||||
@ -930,8 +914,8 @@ Returns:
|
|||||||
--*/
|
--*/
|
||||||
EFI_STATUS
|
EFI_STATUS
|
||||||
AddModHandle (
|
AddModHandle (
|
||||||
IN UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext,
|
IN CONST CHAR8 *PdbPointer,
|
||||||
IN VOID *ModHandle
|
IN VOID *ModHandle
|
||||||
)
|
)
|
||||||
|
|
||||||
{
|
{
|
||||||
@ -959,11 +943,11 @@ AddModHandle (
|
|||||||
// Make a copy of the string and store the ModHandle
|
// Make a copy of the string and store the ModHandle
|
||||||
//
|
//
|
||||||
Handle = GetProcessHeap ();
|
Handle = GetProcessHeap ();
|
||||||
Size = AsciiStrLen (ImageContext->PdbPointer) + 1;
|
Size = AsciiStrLen (PdbPointer) + 1;
|
||||||
Array->PdbPointer = HeapAlloc (Handle, HEAP_ZERO_MEMORY, Size);
|
Array->PdbPointer = HeapAlloc (Handle, HEAP_ZERO_MEMORY, Size);
|
||||||
ASSERT (Array->PdbPointer != NULL);
|
ASSERT (Array->PdbPointer != NULL);
|
||||||
|
|
||||||
AsciiStrCpyS (Array->PdbPointer, Size, ImageContext->PdbPointer);
|
AsciiStrCpyS (Array->PdbPointer, Size, PdbPointer);
|
||||||
Array->ModHandle = ModHandle;
|
Array->ModHandle = ModHandle;
|
||||||
return EFI_SUCCESS;
|
return EFI_SUCCESS;
|
||||||
}
|
}
|
||||||
@ -994,7 +978,7 @@ AddModHandle (
|
|||||||
return EFI_OUT_OF_RESOURCES;
|
return EFI_OUT_OF_RESOURCES;
|
||||||
}
|
}
|
||||||
|
|
||||||
return AddModHandle (ImageContext, ModHandle);
|
return AddModHandle (PdbPointer, ModHandle);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1007,13 +991,13 @@ AddModHandle (
|
|||||||
**/
|
**/
|
||||||
VOID *
|
VOID *
|
||||||
RemoveModHandle (
|
RemoveModHandle (
|
||||||
IN UEFI_IMAGE_LOADER_IMAGE_CONTEXT *ImageContext
|
IN CONST CHAR8 *PdbPointer
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
UINTN Index;
|
UINTN Index;
|
||||||
PDB_NAME_TO_MOD_HANDLE *Array;
|
PDB_NAME_TO_MOD_HANDLE *Array;
|
||||||
|
|
||||||
if (ImageContext->PdbPointer == NULL) {
|
if (PdbPointer == NULL) {
|
||||||
//
|
//
|
||||||
// If no PDB pointer there is no ModHandle so return NULL
|
// If no PDB pointer there is no ModHandle so return NULL
|
||||||
//
|
//
|
||||||
@ -1022,7 +1006,7 @@ RemoveModHandle (
|
|||||||
|
|
||||||
Array = mPdbNameModHandleArray;
|
Array = mPdbNameModHandleArray;
|
||||||
for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
|
for (Index = 0; Index < mPdbNameModHandleArraySize; Index++, Array++) {
|
||||||
if ((Array->PdbPointer != NULL) && (AsciiStrCmp (Array->PdbPointer, ImageContext->PdbPointer) == 0)) {
|
if ((Array->PdbPointer != NULL) && (AsciiStrCmp (Array->PdbPointer, PdbPointer) == 0)) {
|
||||||
//
|
//
|
||||||
// If you find a match return it and delete the entry
|
// If you find a match return it and delete the entry
|
||||||
//
|
//
|
||||||
@ -1065,6 +1049,8 @@ UefiImageLoaderRelocateImageExtraAction (
|
|||||||
UINT32 Flags;
|
UINT32 Flags;
|
||||||
DWORD NewProtection;
|
DWORD NewProtection;
|
||||||
DWORD OldProtection;
|
DWORD OldProtection;
|
||||||
|
CONST CHAR8 *PdbPointer;
|
||||||
|
UINT32 PdbPointerSize;
|
||||||
|
|
||||||
ASSERT (ImageContext != NULL);
|
ASSERT (ImageContext != NULL);
|
||||||
//
|
//
|
||||||
@ -1078,16 +1064,22 @@ UefiImageLoaderRelocateImageExtraAction (
|
|||||||
|
|
||||||
DllEntryPoint = NULL;
|
DllEntryPoint = NULL;
|
||||||
|
|
||||||
|
Status = UefiImageGetSymbolsPath (ImageContext, &PdbPointer, &PdbPointerSize);
|
||||||
|
if (RETURN_ERROR (Status)) {
|
||||||
|
PdbPointer = NULL;
|
||||||
|
PdbPointerSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Load the DLL if it's not an EBC image.
|
// Load the DLL if it's not an EBC image.
|
||||||
//
|
//
|
||||||
if ((ImageContext->PdbPointer != NULL) &&
|
if ((PdbPointer != NULL) &&
|
||||||
(UefiImageGetMachine (ImageContext) != EFI_IMAGE_MACHINE_EBC))
|
(UefiImageGetMachine (ImageContext) != EFI_IMAGE_MACHINE_EBC))
|
||||||
{
|
{
|
||||||
//
|
//
|
||||||
// Convert filename from ASCII to Unicode
|
// Convert filename from ASCII to Unicode
|
||||||
//
|
//
|
||||||
DllFileName = AsciiToUnicode (ImageContext->PdbPointer, &Index);
|
DllFileName = AsciiToUnicode (PdbPointer, &Index);
|
||||||
|
|
||||||
//
|
//
|
||||||
// Check that we have a valid filename
|
// Check that we have a valid filename
|
||||||
@ -1277,12 +1269,12 @@ UefiImageLoaderRelocateImageExtraAction (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((Library != NULL) && (DllEntryPoint != NULL)) {
|
if ((Library != NULL) && (DllEntryPoint != NULL)) {
|
||||||
Status = AddModHandle (ImageContext, Library);
|
Status = AddModHandle (PdbPointer, Library);
|
||||||
if ((Status == EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED)) {
|
if ((Status == EFI_SUCCESS) || (Status == EFI_ALREADY_STARTED)) {
|
||||||
//
|
//
|
||||||
// This DLL is either not loaded or already started, so source level debugging is supported.
|
// This DLL is either not loaded or already started, so source level debugging is supported.
|
||||||
//
|
//
|
||||||
ImageContext->EntryPoint = (EFI_PHYSICAL_ADDRESS)(UINTN)DllEntryPoint;
|
ImageContext->ImageBuffer = (VOID *)((UINTN)DllEntryPoint - UefiImageGetEntryPointAddress (ImageContext));
|
||||||
SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_REFERENCES) @ 0x%X\n\r", DllFileName, (int)(UINTN)Library);
|
SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, DONT_RESOLVE_DLL_REFERENCES) @ 0x%X\n\r", DllFileName, (int)(UINTN)Library);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -1300,13 +1292,22 @@ UefiImageLoaderUnloadImageExtraAction (
|
|||||||
)
|
)
|
||||||
{
|
{
|
||||||
VOID *ModHandle;
|
VOID *ModHandle;
|
||||||
|
RETURN_STATUS Status;
|
||||||
|
CONST CHAR8 *PdbPointer;
|
||||||
|
UINT32 PdbPointerSize;
|
||||||
|
|
||||||
ASSERT (ImageContext != NULL);
|
ASSERT (ImageContext != NULL);
|
||||||
|
|
||||||
ModHandle = RemoveModHandle (ImageContext);
|
Status = UefiImageGetSymbolsPath (ImageContext, &PdbPointer, &PdbPointerSize);
|
||||||
|
if (RETURN_ERROR (Status)) {
|
||||||
|
PdbPointer = NULL;
|
||||||
|
PdbPointerSize = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ModHandle = RemoveModHandle (PdbPointer);
|
||||||
if (ModHandle != NULL) {
|
if (ModHandle != NULL) {
|
||||||
FreeLibrary (ModHandle);
|
FreeLibrary (ModHandle);
|
||||||
SecPrint ("FreeLibrary (\n\r %s)\n\r", ImageContext->PdbPointer);
|
SecPrint ("FreeLibrary (\n\r %s)\n\r", PdbPointer);
|
||||||
} else {
|
} else {
|
||||||
SecPrint ("WARNING: Unload image without source level debug\n\r");
|
SecPrint ("WARNING: Unload image without source level debug\n\r");
|
||||||
}
|
}
|
||||||
|
@ -233,6 +233,9 @@ CoreInitializeImageServices (
|
|||||||
//
|
//
|
||||||
Image = &mCorePrivateImage;
|
Image = &mCorePrivateImage;
|
||||||
|
|
||||||
|
//
|
||||||
|
// FIXME: This is not a proper solution, because DxeCore may not be XIP
|
||||||
|
//
|
||||||
Status = UefiImageInitializeContext (
|
Status = UefiImageInitializeContext (
|
||||||
ImageContext,
|
ImageContext,
|
||||||
(VOID *) (UINTN) DxeCoreImageBaseAddress,
|
(VOID *) (UINTN) DxeCoreImageBaseAddress,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user