From nobody Wed May 8 15:12:23 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+78877+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+78877+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=posteo.de ARC-Seal: i=1; a=rsa-sha256; t=1628451634; cv=none; d=zohomail.com; s=zohoarc; b=VsnJBp52LNiIe/pf6UhMpAsJW2gugrHi0NO3/IfBfVBIccgAI+riXLSyd09Z6Hk93IZcIYKv3UHMK4HOkt26+nE1o0ZM69jzkhK5SuRFXrlsQW1fucIX6mxGA3OiSqMLxHEimHbpWGLOe5mYnEpkahKXk8nQ2+TwXRS2p0j+fXg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1628451634; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=9SWLcXKZeDf9vf8SKW3hrPGuVqPUr73CfGkkqBXY8bY=; b=PAm0KQDSrWeSkUqZ3k7EslK5Z6+bSfn57HzkOGRCpLhp/kiVyBrklOJj7C6qrsNrR4zkLJotGjR5jpWu0nMyyvvp8UkAYcdrEkmwPOLMOP33FO3+4n5agUNbNbrgyVJ5Lc4XDGJ9LAHFGxpHxoFryCEn8R8/ri5iWl5G1B3BC78= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+78877+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1628451634443704.763458943985; Sun, 8 Aug 2021 12:40:34 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id E76TYY1788612xEzrVibdtqs; Sun, 08 Aug 2021 12:40:34 -0700 X-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 X-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) X-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: [edk2-devel] [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 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,mhaeuser@posteo.de X-Gm-Message-State: 4d4svT74hyrsxoS85JlfeJsZx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1628451634; bh=KdyFilS17JCfRuzfXMA030mkpRJA58BOcj0HqrN2F1E=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=O5JrKAiXpCOn4RwNRBn9iPITrkTCpXCIbP1knQpelR44iKK5EwroysM7XFEYos12kUy REOttUAwwMnl22bFHec/l/UpjRmiJEB6CzPY9exepJ7bYK+gEfMrkc0nytjyB7eFCb8MX lQb+S7wKpP+yHaHRvVJ6jFN5M6DcUfmqGNo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1628451635476100004 Content-Type: text/plain; charset="utf-8" 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; =20 =20 =20 -UINTN mImageContextModHandleArraySize =3D 0; -IMAGE_CONTEXT_TO_MOD_HANDLE *mImageContextModHandleArray =3D NULL; - EFI_PEI_PPI_DESCRIPTOR *gPpiList; =20 =20 @@ -118,8 +115,7 @@ main ( SecGdbConfigBreak (); =20 // - // If dlopen doesn't work, then we build a gdb script to allow the - // symbols to be loaded. + // We build a gdb script to allow the symbols to be loaded. // Index =3D strlen (*Argv); gGdbWorkingFileName =3D AllocatePool (Index + strlen(".gdb") + 1); @@ -762,7 +758,6 @@ SecPeCoffGetEntryPoint ( ImageContext.EntryPoint =3D (UINTN)*EntryPoint; } =20 - // On Unix a dlopen is done that will change the entry point SecPeCoffRelocateImageExtraAction (&ImageContext); *EntryPoint =3D (VOID *)(UINTN)ImageContext.EntryPoint; =20 @@ -891,115 +886,6 @@ Returns: } =20 =20 -/*++ - -Routine Description: - Store the ModHandle in an array indexed by the Pdb File name. - The ModHandle is needed to unload the image. - -Arguments: - ImageContext - Input data returned from PE Loader Library. Used to find = the - .PDB file name of the PE Image. - ModHandle - Returned from LoadLibraryEx() and stored for call to - FreeLibrary(). - -Returns: - EFI_SUCCESS - ModHandle was stored. - -**/ -EFI_STATUS -AddHandle ( - IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext, - IN VOID *ModHandle - ) -{ - UINTN Index; - IMAGE_CONTEXT_TO_MOD_HANDLE *Array; - UINTN PreviousSize; - - - Array =3D mImageContextModHandleArray; - for (Index =3D 0; Index < mImageContextModHandleArraySize; Index++, Arra= y++) { - if (Array->ImageContext =3D=3D NULL) { - // - // Make a copy of the string and store the ModHandle - // - Array->ImageContext =3D ImageContext; - Array->ModHandle =3D ModHandle; - return EFI_SUCCESS; - } - } - - // - // No free space in mImageContextModHandleArray so grow it by - // IMAGE_CONTEXT_TO_MOD_HANDLE entires. realloc will - // copy the old values to the new location. But it does - // not zero the new memory area. - // - PreviousSize =3D mImageContextModHandleArraySize * sizeof (IMAGE_CONTEXT= _TO_MOD_HANDLE); - mImageContextModHandleArraySize +=3D MAX_IMAGE_CONTEXT_TO_MOD_HANDLE_ARR= AY_SIZE; - - mImageContextModHandleArray =3D ReallocatePool ( - (mImageContextModHandleArraySize - 1) * = sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE), - mImageContextModHandleArraySize * sizeof= (IMAGE_CONTEXT_TO_MOD_HANDLE), - mImageContextModHandleArray - ); - if (mImageContextModHandleArray =3D=3D NULL) { - ASSERT (FALSE); - return EFI_OUT_OF_RESOURCES; - } - - memset (mImageContextModHandleArray + PreviousSize, 0, MAX_IMAGE_CONTEXT= _TO_MOD_HANDLE_ARRAY_SIZE * sizeof (IMAGE_CONTEXT_TO_MOD_HANDLE)); - - return AddHandle (ImageContext, ModHandle); -} - - -/*++ - -Routine Description: - Return the ModHandle and delete the entry in the array. - -Arguments: - ImageContext - Input data returned from PE Loader Library. Used to find = the - .PDB file name of the PE Image. - -Returns: - ModHandle - ModHandle associated with ImageContext is returned - NULL - No ModHandle associated with ImageContext - -**/ -VOID * -RemoveHandle ( - IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext - ) -{ - UINTN Index; - IMAGE_CONTEXT_TO_MOD_HANDLE *Array; - - if (ImageContext->PdbPointer =3D=3D NULL) { - // - // If no PDB pointer there is no ModHandle so return NULL - // - return NULL; - } - - Array =3D mImageContextModHandleArray; - for (Index =3D 0; Index < mImageContextModHandleArraySize; Index++, Arra= y++) { - if (Array->ImageContext =3D=3D ImageContext) { - // - // If you find a match return it and delete the entry - // - Array->ImageContext =3D NULL; - return Array->ModHandle; - } - } - - return NULL; -} - - - BOOLEAN IsPdbFile ( IN CHAR8 *PdbFileName @@ -1052,67 +938,6 @@ PrintLoadAddress ( } =20 =20 -/** - Loads the image using dlopen so symbols will be automatically - loaded by gdb. - - @param ImageContext The PE/COFF image context - - @retval TRUE - The image was successfully loaded - @retval FALSE - The image was successfully loaded - -**/ -BOOLEAN -DlLoadImage ( - IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext - ) -{ - -#ifdef __APPLE__ - - return FALSE; - -#else - - void *Handle =3D NULL; - void *Entry =3D NULL; - - if (ImageContext->PdbPointer =3D=3D NULL) { - return FALSE; - } - - if (!IsPdbFile (ImageContext->PdbPointer)) { - return FALSE; - } - - fprintf ( - stderr, - "Loading %s 0x%08lx - entry point 0x%08lx\n", - ImageContext->PdbPointer, - (unsigned long)ImageContext->ImageAddress, - (unsigned long)ImageContext->EntryPoint - ); - - Handle =3D dlopen (ImageContext->PdbPointer, RTLD_NOW); - if (Handle !=3D NULL) { - Entry =3D dlsym (Handle, "_ModuleEntryPoint"); - AddHandle (ImageContext, Handle); - } else { - printf("%s\n", dlerror()); - } - - if (Entry !=3D NULL) { - ImageContext->EntryPoint =3D (UINTN)Entry; - printf ("Change %s Entrypoint to :0x%08lx\n", ImageContext->PdbPointer= , (unsigned long)Entry); - return TRUE; - } else { - return FALSE; - } - -#endif -} - - #ifdef __APPLE__ __attribute__((noinline)) #endif @@ -1195,9 +1020,7 @@ SecPeCoffRelocateImageExtraAction ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ) { - if (!DlLoadImage (ImageContext)) { - GdbScriptAddImage (ImageContext); - } + GdbScriptAddImage (ImageContext); } =20 =20 @@ -1264,19 +1087,6 @@ SecPeCoffUnloadImageExtraAction ( IN PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ) { - VOID *Handle; - - // - // Check to see if the image symbols were loaded with gdb script, or dlo= pen - // - Handle =3D RemoveHandle (ImageContext); - if (Handle !=3D NULL) { -#ifndef __APPLE__ - dlclose (Handle); -#endif - return; - } - GdbScriptRemoveImage (ImageContext); } =20 --=20 2.31.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#78877): https://edk2.groups.io/g/devel/message/78877 Mute This Topic: https://groups.io/mt/84754049/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-