From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mout02.posteo.de (mout02.posteo.de [185.67.36.66]) by mx.groups.io with SMTP id smtpd.web10.13761.1628451632611887237 for ; Sun, 08 Aug 2021 12:40:33 -0700 Authentication-Results: mx.groups.io; dkim=pass header.i=@posteo.de header.s=2017 header.b=sLciTTSO; spf=pass (domain: posteo.de, ip: 185.67.36.66, mailfrom: mhaeuser@posteo.de) Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id D98A0240105 for ; Sun, 8 Aug 2021 21:40:30 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.de; s=2017; t=1628451630; bh=UYMB23blScT4ZU2DYW1jUv9VaOQytqpAikS8U2KkZ7Q=; h=From:To:Cc:Subject:Date:From; b=sLciTTSOO4Bvc4LjqY/xvVtxcdrK8bLcH2DChiCmH2bY3DQxjOFfGnFhQlY6UkJ48 I6XmiZJ97VJuHh6G1hsjv8UEtCB5gJcUhE+nqm32EGOcNenYfXVULY55bV1AK5D0Jg OQelndt69xPOrJNIIqc5ecfwt1/B5t6LvvZ11Xx6Cpb91nCAeb3wk6TqNGV9/csdjx qOhJnQX+z6EWz6xoa47f0XDmFFMlwv9ZBMAsPm8KIru7IRgNuO1FAyNv5WQQr74Tbb WqcCzD9V2JtYroUExYN157LIfRDbXDP2zKOlCiI3QTw8tdDAK1lkFnjjnSNCaxI7rn gUsjK1r1DnBXA== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GjV1V2m6Wz6tmH; Sun, 8 Aug 2021 21:40:30 +0200 (CEST) From: =?UTF-8?B?TWFydmluIEjDpHVzZXI=?= To: devel@edk2.groups.io Cc: Andrew Fish , Ray Ni , Vitaly Cheptsov Subject: [PATCH] EmulatorPkg/Host/Unix: Drop dlopen() usage Date: Sun, 8 Aug 2021 19:39:37 +0000 Message-Id: <615b2bc7290df836fa613b83edab660001054037.1628366631.git.mhaeuser@posteo.de> In-Reply-To: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> References: <5df11a13422732b9c03c120775a2b4dd0a49182f.1628444003.git.mhaeuser@posteo.de> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable To assist debugging, i.e. to have the OS load the image debug symbols, EmulatorPkg/Unix used to load most UEFI executables with OS APIs, namely dlopen() and dlclose(). After support was added for GDB and LLDB debugging of images loaded by the EDK II code, this route became orphaned. Namely, it does not work for a variety of reasons: 1) Support is explicitly disabled for Xcode builds. 2) The dlopen() route is only attempted if the PDB path name ends with ".pdb", which has not been the case with recent GCC toolchains. 3) By default, the images are not 4K-aligned, which causes some OSes to deny the image executioon. 4) With recent GCC toolchains, UEFI images are built as ELF executables, which cannot be dynamically loaded via dlopen(). Drop support for the dlopen() path as realistically it has not been used or functioning in a long time. Cc: Andrew Fish Cc: Ray Ni Cc: Vitaly Cheptsov Signed-off-by: Marvin H=C3=A4user --- EmulatorPkg/Unix/Host/Host.c | 194 +------------------- 1 file changed, 2 insertions(+), 192 deletions(-) diff --git a/EmulatorPkg/Unix/Host/Host.c b/EmulatorPkg/Unix/Host/Host.c index b4e5510613c8..e19e20ed2902 100644 --- a/EmulatorPkg/Unix/Host/Host.c +++ b/EmulatorPkg/Unix/Host/Host.c @@ -48,9 +48,6 @@ EMU_SYSTEM_MEMORY *gSystemMemory; =0D =0D =0D -UINTN mImageContextModHandleArraySize =3D 0;=0D -IMAGE_CONTEXT_TO_MOD_HANDLE *mImageContextModHandleArray =3D NULL;=0D -=0D EFI_PEI_PPI_DESCRIPTOR *gPpiList;=0D =0D =0D @@ -118,8 +115,7 @@ main ( SecGdbConfigBreak ();=0D =0D //=0D - // If dlopen doesn't work, then we build a gdb script to allow the=0D - // symbols to be loaded.=0D + // We build a gdb script to allow the symbols to be loaded.=0D //=0D Index =3D strlen (*Argv);=0D gGdbWorkingFileName =3D AllocatePool (Index + strlen(".gdb") + 1);=0D @@ -762,7 +758,6 @@ SecPeCoffGetEntryPoint ( ImageContext.EntryPoint =3D (UINTN)*EntryPoint;=0D }=0D =0D - // On Unix a dlopen is done that will change the entry point=0D SecPeCoffRelocateImageExtraAction (&ImageContext);=0D *EntryPoint =3D (VOID *)(UINTN)ImageContext.EntryPoint;=0D =0D @@ -891,115 +886,6 @@ Returns: }=0D =0D =0D -/*++=0D -=0D -Routine Description:=0D - Store the ModHandle in an array indexed by the Pdb File name.=0D - The ModHandle is needed to unload the image.=0D -=0D -Arguments:=0D - ImageContext - Input data returned from PE Loader Library. Used to find = the=0D - .PDB file name of the PE Image.=0D - ModHandle - Returned from LoadLibraryEx() and stored for call to=0D - FreeLibrary().=0D -=0D -Returns:=0D - EFI_SUCCESS - ModHandle was stored.=0D -=0D -**/=0D -EFI_STATUS=0D -AddHandle (=0D - IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext,=0D - IN VOID *ModHandle=0D - )=0D -{=0D - UINTN Index;=0D - IMAGE_CONTEXT_TO_MOD_HANDLE *Array;=0D - UINTN PreviousSize;=0D -=0D -=0D - Array =3D mImageContextModHandleArray;=0D - for (Index =3D 0; Index < mImageContextModHandleArraySize; Index++, Arra= y++) {=0D - if (Array->ImageContext =3D=3D NULL) {=0D - //=0D - // Make a copy of the string and store the ModHandle=0D - //=0D - Array->ImageContext =3D ImageContext;=0D - Array->ModHandle =3D ModHandle;=0D - return EFI_SUCCESS;=0D - }=0D - }=0D -=0D - //=0D - // No free space in mImageContextModHandleArray so grow it by=0D - // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will=0D - // copy the old values to the new location. But it does=0D - // not zero the new memory area.=0D - //=0D - PreviousSize =3D mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT= _TO_MOD_HANDLE);=0D - mImageContextModHandleArraySize +=3D MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARR= AY_SIZE;=0D -=0D - mImageContextModHandleArray =3D ReallocatePool (=0D - (mImageContextModHandleArraySize - 1) * = sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE),=0D - mImageContextModHandleArraySize * sizeof= (IMAGE_CONTEXT_TO_MOD_HANDLE),=0D - mImageContextModHandleArray=0D - );=0D - if (mImageContextModHandleArray =3D=3D NULL) {=0D - ASSERT (FALSE);=0D - return EFI_OUT_OF_RESOURCES;=0D - }=0D -=0D - memset (mImageContextModHandleArray + PreviousSize, 0, MAX_IMAGE_CONTEXT= _TO_MOD_HANDLE_ARRAY_SIZE * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE));=0D -=0D - return AddHandle (ImageContext, ModHandle);=0D -}=0D -=0D -=0D -/*++=0D -=0D -Routine Description:=0D - Return the ModHandle and delete the entry in the array.=0D -=0D -Arguments:=0D - ImageContext - Input data returned from PE Loader Library. Used to find = the=0D - .PDB file name of the PE Image.=0D -=0D -Returns:=0D - ModHandle - ModHandle associated with ImageContext is returned=0D - NULL - No ModHandle associated with ImageContext=0D -=0D -**/=0D -VOID *=0D -RemoveHandle (=0D - IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D - )=0D -{=0D - UINTN Index;=0D - IMAGE_CONTEXT_TO_MOD_HANDLE *Array;=0D -=0D - if (ImageContext->PdbPointer =3D=3D NULL) {=0D - //=0D - // If no PDB pointer there is no ModHandle so return NULL=0D - //=0D - return NULL;=0D - }=0D -=0D - Array =3D mImageContextModHandleArray;=0D - for (Index =3D 0; Index < mImageContextModHandleArraySize; Index++, Arra= y++) {=0D - if (Array->ImageContext =3D=3D ImageContext) {=0D - //=0D - // If you find a match return it and delete the entry=0D - //=0D - Array->ImageContext =3D NULL;=0D - return Array->ModHandle;=0D - }=0D - }=0D -=0D - return NULL;=0D -}=0D -=0D -=0D -=0D BOOLEAN=0D IsPdbFile (=0D IN CHAR8 *PdbFileName=0D @@ -1052,67 +938,6 @@ PrintLoadAddress ( }=0D =0D =0D -/**=0D - Loads the image using dlopen so symbols will be automatically=0D - loaded by gdb.=0D -=0D - @param ImageContext The PE/COFF image context=0D -=0D - @retval TRUE - The image was successfully loaded=0D - @retval FALSE - The image was successfully loaded=0D -=0D -**/=0D -BOOLEAN=0D -DlLoadImage (=0D - IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D - )=0D -{=0D -=0D -#ifdef __APPLE__=0D -=0D - return FALSE;=0D -=0D -#else=0D -=0D - void *Handle =3D NULL;=0D - void *Entry =3D NULL;=0D -=0D - if (ImageContext->PdbPointer =3D=3D NULL) {=0D - return FALSE;=0D - }=0D -=0D - if (!IsPdbFile (ImageContext->PdbPointer)) {=0D - return FALSE;=0D - }=0D -=0D - fprintf (=0D - stderr,=0D - "Loading %s 0x%08lx - entry point 0x%08lx\n",=0D - ImageContext->PdbPointer,=0D - (unsigned long)ImageContext->ImageAddress,=0D - (unsigned long)ImageContext->EntryPoint=0D - );=0D -=0D - Handle =3D dlopen (ImageContext->PdbPointer, RTLD_NOW);=0D - if (Handle !=3D NULL) {=0D - Entry =3D dlsym (Handle, "_ModuleEntryPoint");=0D - AddHandle (ImageContext, Handle);=0D - } else {=0D - printf("%s\n", dlerror());=0D - }=0D -=0D - if (Entry !=3D NULL) {=0D - ImageContext->EntryPoint =3D (UINTN)Entry;=0D - printf ("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer= , (unsigned long)Entry);=0D - return TRUE;=0D - } else {=0D - return FALSE;=0D - }=0D -=0D -#endif=0D -}=0D -=0D -=0D #ifdef __APPLE__=0D __attribute__((noinline))=0D #endif=0D @@ -1195,9 +1020,7 @@ SecPeCoffRelocateImageExtraAction ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D )=0D {=0D - if (!DlLoadImage (ImageContext)) {=0D - GdbScriptAddImage (ImageContext);=0D - }=0D + GdbScriptAddImage (ImageContext);=0D }=0D =0D =0D @@ -1264,19 +1087,6 @@ SecPeCoffUnloadImageExtraAction ( IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext=0D )=0D {=0D - VOID *Handle;=0D -=0D - //=0D - // Check to see if the image symbols were loaded with gdb script, or dlo= pen=0D - //=0D - Handle =3D RemoveHandle (ImageContext);=0D - if (Handle !=3D NULL) {=0D -#ifndef __APPLE__=0D - dlclose (Handle);=0D -#endif=0D - return;=0D - }=0D -=0D GdbScriptRemoveImage (ImageContext);=0D }=0D =0D --=20 2.31.1