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 42BABAC14D7 for ; Tue, 26 Sep 2023 20:07:41 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=tfnXnjmzaw7CIhHRzrz2HrIHop7JvKkXCQfllrRFNNM=; 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=1695758860; v=1; b=jq9s5DGBLNdjZ5MKvQH/p4eC3W1yu7wUVsS6xRAK5XXWdUc+SEePjllF7Q+tV9zvn1P2E+c7 pA5IK+nQuR67vrgRNSw/1JedDH/NGKrPGDoEVkEq2VCS1hggnYsyXhPPfAGJYZyeQM6ylapxUyx VKXvVihbfpTyQU1YoxZsKNr8= X-Received: by 127.0.0.2 with SMTP id kq6fYY7687511xGdczdDJTg0; Tue, 26 Sep 2023 13:07:40 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web10.1423.1695758858578993135 for ; Tue, 26 Sep 2023 13:07:39 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="385509785" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="385509785" X-Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 13:07:38 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="864527697" X-IronPort-AV: E=Sophos;i="6.03,178,1694761200"; d="scan'208";a="864527697" X-Received: from orsmsx601.amr.corp.intel.com ([10.22.229.14]) by fmsmga002.fm.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 26 Sep 2023 13:07:37 -0700 X-Received: from orsmsx612.amr.corp.intel.com (10.22.229.25) by ORSMSX601.amr.corp.intel.com (10.22.229.14) 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:07:37 -0700 X-Received: from orsmsx612.amr.corp.intel.com (10.22.229.25) by ORSMSX612.amr.corp.intel.com (10.22.229.25) 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:07:37 -0700 X-Received: from ORSEDG601.ED.cps.intel.com (10.7.248.6) by orsmsx612.amr.corp.intel.com (10.22.229.25) 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:07:37 -0700 X-Received: from NAM10-BN7-obe.outbound.protection.outlook.com (104.47.70.101) by edgegateway.intel.com (134.134.137.102) 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:07:36 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=oboVZZJ49FdQF7bgYcKHHHNrxk08XNFpwGpE8ps1LuVGlnbkCz1+PDjV0q+FR/Rpn3erKmtol05YzPvwu9YES2NC1NLHRAaqYRL0OMVCW9QQDruTG9TP55ktqt1o8rSZqrjg65FF6TftAQpgqOKsZKd635noVKevWle6G4Clur0PN3UAjLr+DSyIcaTKKeIK7eNX1G5p0zHic9vMOaT/f4pxGY0gS0CATnYxcFEYkwj5rVKaHEj47OJY/hgSfC4wgvKba3NTzVS2d6DOYjnwmBNPNJtQySsaS/dC7G5qmOWwWr+Gism+ZxTMB+L/rT+8kxNjltjfnHKJaKLZWHJCIQ== 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=jtueVbb0F5Pjk9IZFgeXzVs+3bveB3lp1CEXiypieXk=; b=E4oIYkUbg90XEE4vbgPoabxJzr2qlwyFjWOwCbyrJjumh0Em2yd+TKWYAsplVbgKjak4y5st+z8IxHG1AZ1NV49qqpd9voHIeIGgUFT7hjz2iyJPthG0R26VuOH2bRz1PAV+wiYDsq7sXSj1yNb65Aq+Sk9zefwRr6fkSIuNl1F6X2azaJJ5eAOjKVDB1HjPP5hZAtXPmNnMr8exWWYXdgrJm+wrVaGuUud0aBoqbV26/xCVXgcSZk2ljeWV3zATAlIFZ9ainFZLOrPfwLDSSlQsu/h1Yleg6+M1TT1PBc6qik0eRozHs4absFKevohuUUz8+4QYh+OTG7/MyQ7kZg== 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 SA2PR11MB4938.namprd11.prod.outlook.com (2603:10b6:806:fb::14) by MW6PR11MB8389.namprd11.prod.outlook.com (2603:10b6:303:23d::10) 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:07:33 +0000 X-Received: from SA2PR11MB4938.namprd11.prod.outlook.com ([fe80::ca3:8eed:9332:b6e0]) by SA2PR11MB4938.namprd11.prod.outlook.com ([fe80::ca3:8eed:9332:b6e0%6]) with mapi id 15.20.6813.017; Tue, 26 Sep 2023 20:07:33 +0000 From: "Michael D Kinney" To: "Desimone, Nathaniel L" , "devel@edk2.groups.io" CC: Andrew Fish , "Ni, Ray" , "Chiu, Chasel" , "Kinney, Michael D" 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+4eW5jkeRufIXz2NvSLAtje2w Date: Tue, 26 Sep 2023 20:07:33 +0000 Message-ID: References: <20230922224923.1978-1-nathaniel.l.desimone@intel.com> In-Reply-To: <20230922224923.1978-1-nathaniel.l.desimone@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-publictraffictype: Email x-ms-traffictypediagnostic: SA2PR11MB4938:EE_|MW6PR11MB8389:EE_ x-ms-office365-filtering-correlation-id: dbaaa01a-d821-4a88-8ca7-08dbbecc3853 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: 3iI3SsHtBfB08smZ03d47c2hipddqVqiKLcolTFzI//QOpQZ2vIo2boYIoRVp1EYf5sdkubH0lTKmppXqnt3oyZvcwDL8uTun6luTvXBAWWmmJ3GMQG8yVg9ANDinGNaJbH1my4VDX7xmE3SiJ/rqDxuI3wyptMYWadmgQjzKutZzP5MI7j/66LelmONgraQBss4JU9liA+SIqaJWp0YNccxACNC9JSd+qH6Pn+sNZL9cyv1G/EDQz/Zz9e+a4n/llm8UiCErw+f2N4/ZqPm1rIqANkthlKpisbS9Jesz/qNdhUH6m13g/u4ixyDnsy/lOV0nmWJFgQply4/ftk/cq14J3je/3gjcmg/Ud3wSihDeLyx1mnkmq//PcgioCoe66DFDFft6YTVCJy64Wphq9RLzrrkeEMIinCz2z9ZeQs9LgBOo8bJQTg73C9sqERvaefW+ZDMliu3rNCOHFpGd/vXqoXqN2Xe48nHA7qtm7CDU8vN6LJqegUdeC+8UVQCo/1vO1PjGr3sMkn3utCwBt4uTCq8w8x9dup4h0gZq3wsh5XWzm+tpCL0vXScgc6TxYhMWJooIUdieckOVycOw8yk4zZ+diPiX6yk5Jg3YuR7BlvProPDiXr/er/HgaTOZgJL3LhOOg3aQVJWhbcfSw== x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?w+i+eTMLSMp5enLbSJX3t7u9kFg9dRBIdQE0+GGdN9tXycs59GYj511p3aDg?= =?us-ascii?Q?6NzTrR380mGLnyx3dFK47Qdh0muYqfoKJjuGTiuplXxFDzg9BKGRETjgbdnM?= =?us-ascii?Q?9Ezzo93WcatLx8WjI2q7FyUBLc1OKe6dsfMruVZxpatGKUWA0cA0RzOcDKEn?= =?us-ascii?Q?AL8fx+YsaA5UXmFgi8XJIN3EBHlsvZLgyZsZmQCQ3FCG4gm5PGD23aVDpP8/?= =?us-ascii?Q?orgu6dvjuN7HlSJ5QonNqFXeSWEf5G0waeG7wK4GSyK3s/x80LLkL5HvP3fJ?= =?us-ascii?Q?JzFQU/P2UNYrzWuWG+rdYUz9TyjuKxNelGNX6aq35NQ9SeQwZf2YYVC7L4VR?= =?us-ascii?Q?9/glv8wt09rfxa1U3O/3D5MicHFnmJne+nzJCZj+H2wN+ltXj9GuuZHcA7JV?= =?us-ascii?Q?/1jD1+w4pZMvhWgNJbLz47lB4vR/X2iYX2PUfT3IJ/HRtY4rnXePhjIjG9+C?= =?us-ascii?Q?8RO9r45aS1xAz2Pfrf+8pgdteEZF+cVrw6daAQ5lxeaovP70GnLt/krlNCtn?= =?us-ascii?Q?zVsVg0OKZh95zIBZrZHwH5TiRTl6Xe6s3S9uWyaUbycRlpybbAisIsTdt/UD?= =?us-ascii?Q?xkDvoh2Qdtww9U2GAFnpuUe1YI1gPR1cP+xVV1NomYJSHKOEYcOiCKYJQzRy?= =?us-ascii?Q?Y0PmH1LOyd32IUrG06dFH5XpHZnSP19TgHyMe3rrwu1N6Cv+YhB3SMzAmswb?= =?us-ascii?Q?QcfdR3aEt53+oVjUm2JXCNFVmvo/P4izxGsd8q40xkNz2jUQL5NA3okbVzod?= =?us-ascii?Q?MHN1lwuEzbCZ+YoWa5T6+qP0m6mU9nx9qCHVSWRIWDzu7k7YHl/+HdRql862?= =?us-ascii?Q?kGVlndVa6Ee8r2Qj+PY73M+rJKwlhdaOYRCOBB3vIqyNiocAoKEgwhaj3BNl?= =?us-ascii?Q?DuD+N/KA72hNOOtfPWBGdP2baEwAytt7rI/jW9FcbyB+XE2AOCGChVCjv9Nn?= =?us-ascii?Q?6G3VmC1zF4NKmyRBaKNnfKGagMYW4nSgELLomdHoo0a7yjl/5VqLIgZJzjNV?= =?us-ascii?Q?3VqJ/naRXpBW7xzPljgnJBGyNMF5eNx9mwUckKM7+zONT5yAwA3BC5/CLOJG?= =?us-ascii?Q?npFM3LithnqB2a8mgMNmJC03IZ0o2VS9QkkA/H/K09nCxdqXz3tjnZNBiMSk?= =?us-ascii?Q?02J1axiGFhvl3YnZG5vwLEDAJHcyludFtpFCRlA1Gbaa8l6KZqa319wY8Oxc?= =?us-ascii?Q?HC163guqgbT8w8VrKuPaif3HzMj4KZfPYdz+LiTBVH32geO2IaaDR4iX/gw5?= =?us-ascii?Q?ZRYk+DAu5M58zFwgf7T26BWWyBkrxrzBhXhrnTLsMCgMjMX5Gnm/ghf/jCAW?= =?us-ascii?Q?nbf52AjMHKaNTCdD0bLIsIoF+UjdVplpyXHPBel9K/ZsQ4cLxQ8qCncXy8F9?= =?us-ascii?Q?q9u6liUTTtN6EpXUVNUE07vA2LyajUEQPnA9moxRLNrxJDCa0m5ZwmOOxswN?= =?us-ascii?Q?Pbsg2xb5vMlo36Tq9zjobN+vHCiRqavN8Egl/jDqk0g4oLyp6MlBTZylmUyP?= =?us-ascii?Q?tTCV7hHANXWliSLfapN9sYuyoUdYJzfym03udlzcC8pe1DGiX9oP0KrxX0eE?= =?us-ascii?Q?uVVuYlL9GJX5KXAV/1KrJb44rKNLax/cYJquhqdS?= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: SA2PR11MB4938.namprd11.prod.outlook.com X-MS-Exchange-CrossTenant-Network-Message-Id: dbaaa01a-d821-4a88-8ca7-08dbbecc3853 X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Sep 2023 20:07:33.0277 (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: +nSwQgXtfsTq8B3d7pRXWSZ2pV0SMfZ/7BsuNf9VLM1HvbMGnV9zTv+f/TXEQbIsmA+rmH15e16N9NN6aJY2PsHn6WAnuxfIkBjoyKfvr8U= X-MS-Exchange-Transport-CrossTenantHeadersStamped: MW6PR11MB8389 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,michael.d.kinney@intel.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: HPZN1wABkuO1D6CYgTxifKEFx7686176AA= 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=jq9s5DGB; arc=reject ("signature check failed: fail, {[1] = sig:microsoft.com:dns request to arcselector9901._domainkey.microsoft.com failed: no records with this name}"); dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=intel.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io 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=20 does not match the PI spec behavior. It also appears that this change can support PE/COFF images=20 that do not have sections that are 4KB aligned and handles the page protection settings for 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, > Michael D ; Chiu, Chasel > > Subject: [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows >=20 > The Visual Studio Windows debugger will only load symbols for PE/COFF > images > that Windows is aware of. Therefore, to enable source level debugging, > all > PEI/DXE modules must be loaded via LoadLibrary() or LoadLibraryEx() > and the > the instance in memory created by LoadLibrary() must be the one that > is > actually executed. >=20 > The current source level debug implementation in EmulatorPkg for > Windows is > inherited from the old Nt32Pkg. This implementation makes the > assumption that > all PEI/DXE modules have a DLL export tables with a symbol named > InitializeDriver. Therefore, this source level debug implementation > requires > all modules to be linked in a non-PI spec defined manner. Support for > adding > the InitializeDriver symbol was removed in EmulatorPkg, which broke > source > level debugging. >=20 > To fix this, the source level debugging implementation has been > modified to > use the PE/COFF entry point directly. This brings the implementation > into > compliance with the PI spec and should work with any PEIM/DXE driver. > Implementing this requires parsing the in-memory instance of the > PE/COFF image > created by Windows to find the entrypoint and since PEIMs/DXE drivers > are not > garunteed to have 4KB aligned sections, it also requires explicit > configuration > of the page table using VirtualProtect(). >=20 > With this fix, the debugging experience is now so good it is > 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 > EmulatorPkg. > Now, just add breakpoints directly in Visual Studio Code the way you > would with > any other software project. When you start the debugger, it will halt > at the > breakpoint automatically without any extra 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 > 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 > directly > allocate memory space with ReadWrite and Execute attribute. >=20 > -Copyright (c) 2006 - 2022, Intel Corporation. All rights > 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 @@ 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 > 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 > - // source level debug. The image is still loaded and relocated > - // in 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. > + // 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 > + // source level debug. The image is still loaded and relocated > + // in 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, > 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 +1300,7 @@ PeCoffLoaderRelocateImageExtraAction ( > // This DLL is not already loaded, so source level debugging > 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) > Library); > } > } else { > SecPrint ("WARNING: No source level debug %S. \n\r", > 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 (#109088): https://edk2.groups.io/g/devel/message/109088 Mute This Topic: https://groups.io/mt/101531560/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/leave/12367111/7686176/19134562= 12/xyzzy [rebecca@openfw.io] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-