From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 9B58AD80A5F for ; Tue, 26 Sep 2023 20:35:26 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=Z9IiW/vyE0PkDitibfl9Jz+9ypcvy+wzw9AQDc44A1s=; c=relaxed/simple; d=groups.io; h=ARC-Seal:ARC-Message-Signature:ARC-Authentication-Results:From:To:CC:Subject:Thread-Topic:Thread-Index:Date:Message-ID:References:In-Reply-To:Accept-Language:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Language:Content-Type:Content-Transfer-Encoding; s=20140610; t=1695760525; v=1; b=ExPPqy7HCjTPA4DPXbDVWN4hZgagtwueok0uvfNof9ojQYFf63CaT/o469RIU+9WUKiV0qlj 6l1M+47urIpUnwQrUX6RwC8SJqlUh/qm9xD0ELNmxvdLZO/bf9vhX7BlVnbqtJBLWGtrQYggT+5 WkgIQZdilLB714k9B/h6DuJ4= X-Received: by 127.0.0.2 with SMTP id DdhWYY7687511xjG8orNW72a; Tue, 26 Sep 2023 13:35:25 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.115]) by mx.groups.io with SMTP id smtpd.web11.2070.1695760524217076294 for ; Tue, 26 Sep 2023 13:35:24 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="381563217" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="381563217" X-Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 13:32:31 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="285879" X-Received: from orsmsx602.amr.corp.intel.com ([10.22.229.15]) by fmviesa001.fm.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 26 Sep 2023 13:32:32 -0700 X-Received: from orsmsx610.amr.corp.intel.com (10.22.229.23) by ORSMSX602.amr.corp.intel.com (10.22.229.15) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32; Tue, 26 Sep 2023 13:32:30 -0700 X-Received: from orsedg603.ED.cps.intel.com (10.7.248.4) by orsmsx610.amr.corp.intel.com (10.22.229.23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.32 via Frontend Transport; Tue, 26 Sep 2023 13:32:30 -0700 X-Received: from NAM02-DM3-obe.outbound.protection.outlook.com (104.47.56.42) by edgegateway.intel.com (134.134.137.100) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.32; Tue, 26 Sep 2023 13:32:30 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=dnmewqqeXGFlrHVRWPeAgUv9mlRyeUzrBbbzpOv4FrgkQ+s9O0cxZXb16m1J1wN65oU4RVjJhPJ/klJQ9YsKj06HmOOFmdLHe09szwxl5pVzaObVUEJT9wgDwgVYejejdTbpvEcW289AdypjtD7egSCqzns2Ovwb2HegUO8BuY8xAJsw/Q6UExu476bzweUUbV9JlKluWZUrzSY0RipE3gCjxlJmZEYYEISeYPkALTg8KSSkMQc5P9eLyiVEtwsyi9JXNW7begV5u/neBk9eGCyXtaxky6v0XkZ8wEGyG8JjMFwkdK3k0Bww2RL7anb7GFbf8rUOVY3ghsxYx3+Y6Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=RJK/WqPFM1v0MsAEzzrhzsb3eVzbL1t81QNzjE0ROCU=; b=cr++2w4slHNl+YSxsxbo4+uKv+gutStAL/9EGwx7ypJHu/aA+n2sD2HHyMS57XVMnRV5MeUkJo6YQI+EuHLt3NiLErvHeOLWOcX4VrFZ6DE0myatX9ZZXqlZRkXx7IrWuaGLTxp+aP5ra8mb+si5cXl+5dkxy/X14pA6WgYKA690PNDBoxcStM/PuELNzp/alk+tblJztFhQ8JpD9PeidrpWWaU8+U6ivPpyRYQwi4+k2sNeXcfv7mOQwQte6IPUa9EyxYoskVNMw5z3ocyQ8VmvHdhLNzcJ7ehpo4v/QMWwWIiuX3cp5VXwFMbI7fKhvq0JZZ9dhb2rqsO6KQ5kXA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=intel.com; dmarc=pass action=none header.from=intel.com; dkim=pass header.d=intel.com; arc=none X-Received: from MW4PR11MB5821.namprd11.prod.outlook.com (2603:10b6:303:184::5) by DM4PR11MB5390.namprd11.prod.outlook.com (2603:10b6:5:395::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.28; Tue, 26 Sep 2023 20:32:28 +0000 X-Received: from MW4PR11MB5821.namprd11.prod.outlook.com ([fe80::89db:ecf0:29f5:9f3c]) by MW4PR11MB5821.namprd11.prod.outlook.com ([fe80::89db:ecf0:29f5:9f3c%4]) with mapi id 15.20.6813.017; Tue, 26 Sep 2023 20:32:28 +0000 From: "Nate DeSimone" To: "Kinney, Michael D" , "devel@edk2.groups.io" CC: Andrew Fish , "Ni, Ray" , "Chiu, Chasel" Subject: Re: [edk2-devel] [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows Thread-Topic: [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows Thread-Index: AQHZ7acXLHA+4eW5jkeRufIXz2NvSLAtje2wgAAFmzA= Date: Tue, 26 Sep 2023 20:32:28 +0000 Message-ID: References: <20230922224923.1978-1-nathaniel.l.desimone@intel.com> In-Reply-To: Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: MW4PR11MB5821:EE_|DM4PR11MB5390:EE_ x-ms-office365-filtering-correlation-id: ac95f170-3396-425e-9bbd-08dbbecfb3c8 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: Vaf4G3R0YkLwhvCC3Mx1WlANBooeq8vusi+0HFAfnlO9pxcnLVGtM3x1krqOMh5yrep8yIzXhkP7VnxRrrhC0yDza7H7rFSbQ0T/z488sXlkHATe2Qv1aQ8vDrU5wRSY8BY1m9yg1SZK9gy3qB0LszExbYygRDa1muJe1ZYo24X0HzLlN40w5/cfwzqqsXWeUmzv5h1k3HhcLcZ5217Sv9Gk1u9FfruBQ+2ZZ4LilB6Fge+XLIGlW533temYy3yly/SS9W2lttBh2UuMbHNk80nRsMBeM17NH5QxNVdY0+brzEdiUDfjbyO9Bot79xe3qfFSBp4doAWQ+h9PaLe/lB65xBZ5YuP5tCIh5tz/F26zi2G3Gzg1512/Lbc2rK7hP0+e3YXyvrIZ0vebg26Iq4PnQbUUgYGE6a2I7FZNXaLFXOJclA9IEpSkn74vRIHE0OEMWVLz77frKwcF2rsad8+9bnoX7DtmvuBu7VULAQXgMFeFQlXZLL17Rhj3VDBQnxJ6RzVqI8wazW4/AKszPndAsiOvsPp/ZVMAU/Iqtu32Mr3bgQQb9aFmsdg+a0yh4HbSn11Z6CN/aJrIKKq6bxX+k36dkgTz+hxl4b34d+RVJF1GYb3Ldza1/4QRAMBY0vStLct9b0Jg3EMQXtr61Q== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?j3l9CGrAJaN+k/mDGw7+gsWIw7TawjSM33VRijqGqaqTLWvgs0kH1325bdMd?= =?us-ascii?Q?5HSIqxxgg9//hAi4FKhW3XT3LMm/eObwySVlKYSldC+xNn8oyFEDavx/LW16?= =?us-ascii?Q?8OZEN558uKvUQjMzcIFK6kE3MnHdNHrjFIZwoUnzhGW4Jt5jyR1g6An7BuNb?= =?us-ascii?Q?kQyvOqxYLIEFLHy3HBqMIeyV2Qogog3xqTltVCNPiqrkcDZ/AUo90vBUY8gQ?= =?us-ascii?Q?twHoAzIXUBd9eRFfVOtnG2lws6c2iI7rB+wBU9+fR1ptIhlxI/jqyCwLM+Bz?= =?us-ascii?Q?voFxpwiyg1k/Cu6+ZnumINZHH//E7wk9SBb5kkYfBP7VSoc07KV8l8bE3owp?= =?us-ascii?Q?U+KySu9+29vFbMSGn5Z3Nb8giFiX9aMkDJQBPXlZIpDImkg4KaWq/qwgs2Yz?= =?us-ascii?Q?n3/GtBKWPzWfgGsGMjlHJslC169UiL+lD86wpOmnInhC0RPNBFij7mXUFdAQ?= =?us-ascii?Q?yUwZuiAoFFt+hHmYvomPrBtC5oERgbsAZjx4ppDPH2ZEW5g6Ui6LEiKMCYz4?= =?us-ascii?Q?gGi1tiKgSfNV2R/oJ0dWZYEs8oaUAUwN3rqxWUa3JXpVrtfDGTDIndaKP3v7?= =?us-ascii?Q?vTsSqFrM2jOwUdYGzdNDdXaKQLhepd3uRFYKQe3EWIlwh/1vEgm4r7TrDdMM?= =?us-ascii?Q?deOzG9JcvGhtK8Ye/lMmyKmXgHDR8svzyjG+faOL3cW1zbKuZcKtLrFMzawW?= =?us-ascii?Q?AaRuKEZSoGJJZYIvS2flfvVQs/mAVEIAqP3KKpfQZQ6NpIn0kFMsRVF94XO/?= =?us-ascii?Q?axI8fsS5U8YJ7iCP0llJ4yhBAOhKzUFFYyc/+/eA8vJj1TD7NPjih4F7SFiU?= =?us-ascii?Q?riogZbyALQP07UA5fMyv57Up5ni/sZuiakj44NRvCG96oY4FHYPLGfpq+hmD?= =?us-ascii?Q?upzTlUAQsG0UcQQ3SePHXxUTyXc2PYoqVHNOO8ZO9GiOTZxZc4qlmWporPVT?= =?us-ascii?Q?RfHVE4RGPd5/mDrvBG2ob6v0veoBmQ2GHMVJlfP1AS3c/L0sq2DVaWlp5I3a?= =?us-ascii?Q?VPXji3+An3ql7FmW5QU8dgm1eyjebOmaaT97zyKOK/39Ub7SpDiNHBuVa0q8?= =?us-ascii?Q?u3OyC8Mobv81Sg5ssX15/4ozT8RM6NHzsZluASUlMXCZlp6/q+if6ELP3sDM?= =?us-ascii?Q?CKMxLfr7MVz9eU3woaR8wzUDoWHDfiJR1JhYzNcORcAAWeWXwaMqAJ9BD9Ox?= =?us-ascii?Q?x3T4f/uSU9QLcnxciBN401zTQ22ouXkDys7qQ+jV6nj0es4L3+pT5uo9UZT4?= =?us-ascii?Q?mnntw6Ekxdo0sgDkjtDn/bGd0jTmMp+NSNSQNsX6TZKufAwgv3JoSZ/l4oly?= =?us-ascii?Q?Mr/f0LFeG4PVsefPchc0XtZFeAsqVNhmux8a7dBw9QxmUDLLePIqgLUGuOKG?= =?us-ascii?Q?oYkyKS2RBcVB4vfNTO3mq/c2OlObUKkrHI2QH2FGfwHc9cJOJfAEkkaOM9en?= =?us-ascii?Q?gd3bM9eI1iMiHXw3BOzYfQXDgNcm6Dbk7CSsDPotMqW/0ZJeL3XX0GidH+z6?= =?us-ascii?Q?/qrWU/r6Eg6/XNrLAmCweRdT3x1XunO69r6kH7al8vKPUvTwAtyPCDmw1Dd3?= =?us-ascii?Q?oY/OySMwEBbXEKImIMHIC4tck5jYXAnWtc0Y5ZJ2WndyyslAHZbwJpgBcAke?= =?us-ascii?Q?Rw=3D=3D?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: MW4PR11MB5821.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: ac95f170-3396-425e-9bbd-08dbbecfb3c8 X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Sep 2023 20:32:28.6490 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 46c98d88-e344-4ed4-8496-4ed7712e255d X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: W1lvVusxtrQlwzv0+IBRxwPaaLxWuRyQp8gcLY7AhHS6MvOjRnYoSpj2NGPOz7lrQessjohnoUSEp88YWc2GzIfmNZAwjmnSytHskmi6ce8= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM4PR11MB5390 X-OriginatorOrg: intel.com Precedence: Bulk 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,nathaniel.l.desimone@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: DtRMYBxREall0VgcdqtrFyRYx7686176AA= Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=ExPPqy7H; spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); arc=reject ("signature check failed: fail, {[1] = sig:microsoft.com:dns request to arcselector9901._domainkey.microsoft.com failed: no records with this name}") Hi Mike, Source level debug with VS Code does indeed work today with gdb or lldb. Th= is change makes the Visual Studio Windows debugger work as well. You are correct that if the same DLL is loaded more than once that this met= hod cannot perform source level debug on the second instance; but this chan= ge won't break that scenario either. If the same DLL occurs twice, then we = will use the PE/COFF image loaded by either the PEI core or DXE core instea= d of the one loaded by Windows. This means that the second instance of the = DLL will not be source level debug-able by Visual Studio; but PI-spec compl= iance is maintained. This behavior is unchanged from the original code. Yes, this code enables PE/COFF images that do not have sections that are 4K= B aligned. It will setup page protection for user mode. Without this change= you must turn off the NX bit when using the Visual Studio Windows debugger= . Thanks, Nate -----Original Message----- From: Kinney, Michael D =20 Sent: Tuesday, September 26, 2023 1:08 PM To: Desimone, Nathaniel L ; devel@edk2.grou= ps.io Cc: Andrew Fish ; Ni, Ray ; Chiu, Chasel= ; Kinney, Michael D Subject: RE: [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows Hi Nate, I am able to do source level debug of EmulatorPkg using VS Code today. What scenarios are broken? I do know that the DLL based approach would only allow a single instance of= the module to be loaded and debugged. If, for example, a driver is loaded= more than once from the UEFI Shell in the EmulatorPkg env, the 2nd driver = would use the first DLL which does not match the PI spec behavior. It also appears that this change can support PE/COFF images that do not hav= e sections that are 4KB aligned and handles the page protection settings fo= r the user mode application env. Is that correct? Mike > -----Original Message----- > From: Desimone, Nathaniel L > Sent: Friday, September 22, 2023 3:49 PM > To: devel@edk2.groups.io > Cc: Andrew Fish ; Ni, Ray ; Kinney,=20 > Michael D ; Chiu, Chasel=20 > > Subject: [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows >=20 > The Visual Studio Windows debugger will only load symbols for PE/COFF=20 > images that Windows is aware of. Therefore, to enable source level=20 > debugging, all PEI/DXE modules must be loaded via LoadLibrary() or=20 > LoadLibraryEx() and the the instance in memory created by=20 > LoadLibrary() must be the one that is actually executed. >=20 > The current source level debug implementation in EmulatorPkg for=20 > Windows is inherited from the old Nt32Pkg. This implementation makes=20 > the assumption that all PEI/DXE modules have a DLL export tables with=20 > a symbol named InitializeDriver. Therefore, this source level debug=20 > implementation requires all modules to be linked in a non-PI spec=20 > defined manner. Support for adding the InitializeDriver symbol was=20 > removed in EmulatorPkg, which broke source level debugging. >=20 > To fix this, the source level debugging implementation has been=20 > modified to use the PE/COFF entry point directly. This brings the=20 > implementation into compliance with the PI spec and should work with=20 > any PEIM/DXE driver. > Implementing this requires parsing the in-memory instance of the=20 > PE/COFF image created by Windows to find the entrypoint and since=20 > PEIMs/DXE drivers are not garunteed to have 4KB aligned sections, it=20 > also requires explicit configuration of the page table using=20 > VirtualProtect(). >=20 > With this fix, the debugging experience is now so good it is=20 > unprecedented! > In Visual Studio Code, add the following to launch.json: >=20 > { > "version": "0.2.0", > "configurations": [ > { > "name": "EmulatorPkg Launch", > "type": "cppvsdbg", > "request": "launch", > "program": > "${workspaceFolder}//Build/EmulatorX64/DEBUG_ n>/X64/WinHost", > "args": [], > "stopAtEntry": false, > "cwd": > "${workspaceFolder}//Build/EmulatorX64/DEBUG_ n>/X64/", > "environment": [], > "console": false, > } > ] > } >=20 > Make modifications to the above template as nessesary and build=20 > EmulatorPkg. > Now, just add breakpoints directly in Visual Studio Code the way you=20 > would with any other software project. When you start the debugger, it=20 > will halt at the breakpoint automatically without any extra=20 > configuration required. >=20 > Cc: Andrew Fish > Cc: Ray Ni > Cc: Michael D Kinney > Cc: Chasel Chiu > Signed-off-by: Nate DeSimone > --- > EmulatorPkg/Win/Host/WinHost.c | 206 +++++++++++++++++++++++++++++--- > - > 1 file changed, 182 insertions(+), 24 deletions(-) >=20 > diff --git a/EmulatorPkg/Win/Host/WinHost.c=20 > b/EmulatorPkg/Win/Host/WinHost.c index 193a947fbd..e414da6c55 100644 > --- a/EmulatorPkg/Win/Host/WinHost.c > +++ b/EmulatorPkg/Win/Host/WinHost.c > @@ -8,7 +8,7 @@ > This code produces 128 K of temporary memory for the SEC stack by=20 > directly > allocate memory space with ReadWrite and Execute attribute. >=20 > -Copyright (c) 2006 - 2022, Intel Corporation. All rights=20 > reserved.
> +Copyright (c) 2006 - 2023, Intel Corporation. All rights > reserved.
> (C) Copyright 2016-2020 Hewlett Packard Enterprise Development LP
> SPDX-License-Identifier: BSD-2-Clause-Patent **/ @@ -977,7 +977,7 @@=20 > AddModHandle ( > for (Index =3D 0; Index < mPdbNameModHandleArraySize; Index++, > Array++) { > if (Array->PdbPointer =3D=3D NULL) { > // > - // Make a copy of the stirng and store the ModHandle > + // Make a copy of the string and store the ModHandle > // > Handle =3D GetProcessHeap (); > Size =3D AsciiStrLen (ImageContext->PdbPointer) + 1; > @@ -1056,26 +1056,45 @@ RemoveModHandle ( > return NULL; > } >=20 > +typedef struct { > + UINTN Base; > + UINT32 Size; > + UINT32 Flags; > +} IMAGE_SECTION_DATA; > + > VOID > EFIAPI > PeCoffLoaderRelocateImageExtraAction ( > IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext > ) > { > - EFI_STATUS Status; > - VOID *DllEntryPoint; > - CHAR16 *DllFileName; > - HMODULE Library; > - UINTN Index; > + EFI_STATUS Status; > + VOID *DllEntryPoint; > + CHAR16 *DllFileName; > + HMODULE Library; > + UINTN Index; > + PE_COFF_LOADER_IMAGE_CONTEXT PeCoffImageContext; > + EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr; > + EFI_IMAGE_SECTION_HEADER *FirstSection; > + EFI_IMAGE_SECTION_HEADER *Section; > + IMAGE_SECTION_DATA *SectionData; > + UINTN NumberOfSections; > + UINTN Base; > + UINTN End; > + UINTN RegionBase; > + UINTN RegionSize; > + UINT32 Flags; > + DWORD NewProtection; > + DWORD OldProtection; >=20 > ASSERT (ImageContext !=3D NULL); > // > - // If we load our own PE COFF images the Windows debugger can not=20 > source > - // level debug our code. If a valid PDB pointer exists use it to=20 > load > - // the *.dll file as a library using Windows* APIs. This allows > - // source level debug. The image is still loaded and relocated > - // in the Framework memory space like on a real system (by the=20 > code above), > - // but the entry point points into the DLL loaded by the code=20 > below. > + // If we load our own PE/COFF images the Windows debugger can not > source > + // level debug our code. If a valid PDB pointer exists use it to > load > + // the *.dll file as a library using Windows* APIs. This allows //=20 > + source level debug. The image is still loaded and relocated // in=20 > + the Framework memory space like on a real system (by the code > above), > + // but the entry point points into the DLL loaded by the code > below. > // >=20 > DllEntryPoint =3D NULL; > @@ -1106,27 +1125,166 @@ PeCoffLoaderRelocateImageExtraAction ( > } >=20 > // > - // Replace .PDB with .DLL on the filename > + // Replace .PDB with .DLL in the filename > // > DllFileName[Index - 3] =3D 'D'; > DllFileName[Index - 2] =3D 'L'; > DllFileName[Index - 1] =3D 'L'; >=20 > // > - // Load the .DLL file into the user process's address space for > source > - // level debug > + // Load the .DLL file into the process's address space for source > level > + // debug. > + // > + // EFI modules use the PE32 entry point for a different purpose > than > + // Windows. For Windows DLLs, the PE entry point is used for the > DllMain() > + // function. DllMain() has a very specific purpose; it > initializes runtime > + // libraries, instance data, and thread local storage. > LoadLibrary()/ > + // LoadLibraryEx() will run the PE32 entry point and assume it to > be a > + // DllMain() implementation by default. By passing the > + // DONT_RESOLVE_DLL_REFERENCES argument to LoadLibraryEx(), the > execution > + // of the entry point as a DllMain() function will be suppressed. > This > + // also prevents other modules that are referenced by the DLL > from being > + // loaded. We use LoadLibraryEx() to create a copy of the PE32 > + // image that the OS (and therefore the debugger) is aware of. > + // Source level debugging is the only reason to do this. > // > Library =3D LoadLibraryEx (DllFileName, NULL,=20 > DONT_RESOLVE_DLL_REFERENCES); > if (Library !=3D NULL) { > // > - // InitializeDriver is the entry point we put in all our EFI > DLL's. The > - // DONT_RESOLVE_DLL_REFERENCES argument to LoadLIbraryEx() > suppresses the > - // normal DLL entry point of DllMain, and prevents other > modules that are > - // referenced in side the DllFileName from being loaded. There > is no error > - // checking as the we can point to the PE32 image loaded by > Tiano. This > - // step is only needed for source level debugging > + // Parse the PE32 image loaded by the OS and find the entry > point > // > - DllEntryPoint =3D (VOID *)(UINTN)GetProcAddress (Library, > "InitializeDriver"); > + ZeroMem (&PeCoffImageContext, sizeof (PeCoffImageContext)); > + PeCoffImageContext.Handle =3D Library; > + PeCoffImageContext.ImageRead =3D PeCoffLoaderImageReadFromMemory; > + Status =3D PeCoffLoaderGetImageInfo (&PeCoffImageContext); > + if (EFI_ERROR (Status) || (PeCoffImageContext.ImageError !=3D > IMAGE_ERROR_SUCCESS)) { > + SecPrint ("DLL is not a valid PE/COFF image.\n\r"); > + FreeLibrary (Library); > + Library =3D NULL; > + } else { > + Hdr.Pe32 =3D (EFI_IMAGE_NT_HEADERS32 *)((UINTN)Library + > (UINTN)PeCoffImageContext.PeCoffHeaderOffset); > + if (Hdr.Pe32->OptionalHeader.Magic =3D=3D > EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { > + // > + // Use PE32 offset > + // > + DllEntryPoint =3D (VOID *) ((UINTN)Library + (UINTN)Hdr.Pe32- > >OptionalHeader.AddressOfEntryPoint); > + } else { > + // > + // Use PE32+ offset > + // > + DllEntryPoint =3D (VOID *) ((UINTN)Library + > (UINTN)Hdr.Pe32Plus->OptionalHeader.AddressOfEntryPoint); > + } > + // > + // Now we need to configure memory access for the copy of the > PE32 image > + // loaded by the OS. > + // > + // Most Windows DLLs are linked with sections 4KB aligned but > EFI > + // modules are not to reduce size. Because of this we need to > compute > + // the union of memory access attributes and explicitly > configure > + // each page. > + // > + FirstSection =3D (EFI_IMAGE_SECTION_HEADER *)( > + (UINTN)Library + > + > PeCoffImageContext.PeCoffHeaderOffset + > + sizeof (UINT32) + > + sizeof > (EFI_IMAGE_FILE_HEADER) + > + Hdr.Pe32- > >FileHeader.SizeOfOptionalHeader > + ); > + NumberOfSections =3D (UINTN)(Hdr.Pe32- > >FileHeader.NumberOfSections); > + Section =3D FirstSection; > + SectionData =3D malloc (NumberOfSections * sizeof > (IMAGE_SECTION_DATA)); > + if (SectionData =3D=3D NULL) { > + FreeLibrary (Library); > + Library =3D NULL; > + DllEntryPoint =3D NULL; > + } > + ZeroMem (SectionData, NumberOfSections * sizeof > (IMAGE_SECTION_DATA)); > + // > + // Extract the section data from the PE32 image > + // > + for (Index =3D 0; Index < NumberOfSections; Index++) { > + SectionData[Index].Base =3D (UINTN)Library + Section- > >VirtualAddress; > + SectionData[Index].Size =3D Section->Misc.VirtualSize; > + if (SectionData[Index].Size =3D=3D 0) { > + SectionData[Index].Size =3D Section->SizeOfRawData; > + } > + SectionData[Index].Flags =3D (Section->Characteristics & > + (EFI_IMAGE_SCN_MEM_EXECUTE | > EFI_IMAGE_SCN_MEM_WRITE)); > + Section +=3D 1; > + } > + // > + // Loop over every DWORD in memory and compute the union of > the memory > + // access bits. > + // > + End =3D (UINTN)Library + (UINTN)PeCoffImageContext.ImageSize; > + RegionBase =3D (UINTN)Library; > + RegionSize =3D 0; > + Flags =3D 0; > + for (Base =3D (UINTN)Library + sizeof (UINT32); Base < End; > Base +=3D sizeof (UINT32)) { > + for (Index =3D 0; Index < NumberOfSections; Index++) { > + if (SectionData[Index].Base <=3D Base && > + (SectionData[Index].Base + SectionData[Index].Size) > > Base) { > + Flags |=3D SectionData[Index].Flags; > + } > + } > + // > + // When a new page is reached configure the memory access > for the > + // previous page. > + // > + if (Base % SIZE_4KB =3D=3D 0) { > + RegionSize +=3D SIZE_4KB; > + if ((Flags & EFI_IMAGE_SCN_MEM_WRITE) =3D=3D > EFI_IMAGE_SCN_MEM_WRITE) { > + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D > EFI_IMAGE_SCN_MEM_EXECUTE) { > + NewProtection =3D PAGE_EXECUTE_READWRITE; > + } else { > + NewProtection =3D PAGE_READWRITE; > + } > + } else { > + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D > EFI_IMAGE_SCN_MEM_EXECUTE) { > + NewProtection =3D PAGE_EXECUTE_READ; > + } else { > + NewProtection =3D PAGE_READONLY; > + } > + } > + if (!VirtualProtect ((LPVOID)RegionBase, (SIZE_T) > RegionSize, NewProtection, &OldProtection)) { > + SecPrint ("Setting PE32 Section Access Failed\n\r"); > + FreeLibrary (Library); > + free (SectionData); > + Library =3D NULL; > + DllEntryPoint =3D NULL; > + break; > + } > + Flags =3D 0; > + RegionBase =3D Base; > + RegionSize =3D 0; > + } > + } > + free (SectionData); > + // > + // Configure the last partial page > + // > + if (Library !=3D NULL && (End - RegionBase) > 0) { > + if ((Flags & EFI_IMAGE_SCN_MEM_WRITE) =3D=3D > EFI_IMAGE_SCN_MEM_WRITE) { > + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D > EFI_IMAGE_SCN_MEM_EXECUTE) { > + NewProtection =3D PAGE_EXECUTE_READWRITE; > + } else { > + NewProtection =3D PAGE_READWRITE; > + } > + } else { > + if ((Flags & EFI_IMAGE_SCN_MEM_EXECUTE) =3D=3D > EFI_IMAGE_SCN_MEM_EXECUTE) { > + NewProtection =3D PAGE_EXECUTE_READ; > + } else { > + NewProtection =3D PAGE_READONLY; > + } > + } > + if (!VirtualProtect ((LPVOID)RegionBase, (SIZE_T) (End - > RegionBase), NewProtection, &OldProtection)) { > + SecPrint ("Setting PE32 Section Access Failed\n\r"); > + FreeLibrary (Library); > + Library =3D NULL; > + DllEntryPoint =3D NULL; > + } > + } > + } > } >=20 > if ((Library !=3D NULL) && (DllEntryPoint !=3D NULL)) { @@ -1142,7= =20 > +1300,7 @@ PeCoffLoaderRelocateImageExtraAction ( > // This DLL is not already loaded, so source level debugging=20 > is supported. > // > ImageContext->EntryPoint =3D > (EFI_PHYSICAL_ADDRESS)(UINTN)DllEntryPoint; > - SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, > DONT_RESOLVE_DLL_REFERENCES)\n\r", DllFileName); > + SecPrint ("LoadLibraryEx (\n\r %S,\n\r NULL, > DONT_RESOLVE_DLL_REFERENCES) @ 0x%X\n\r", DllFileName, (int) (UINTN)=20 > Library); > } > } else { > SecPrint ("WARNING: No source level debug %S. \n\r",=20 > DllFileName); > -- > 2.39.2.windows.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 (#109089): https://edk2.groups.io/g/devel/message/109089 Mute This Topic: https://groups.io/mt/101531560/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-