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 D8B10D80470 for ; Tue, 26 Sep 2023 21:28:40 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=rakaHAYJb/g4VHhN1VBmTWPKQuSDUdPipTQbuGy1gnM=; 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=1695763719; v=1; b=e+0C+oK5ii/u7tjCpoQKBSU88Uc3CW1jreXXKH7eOOI0VMDfXq7v7B35VUQWZk5r7okWjVwT H1khry8imbODMkAj7UXpXgTMQuB1+/3N0sE1WXGmFyPjqI0n1rmRXmN66aB///XNITG4IuqmGEk 7AtffBF0C+nTYWvQAJRrQ2bk= X-Received: by 127.0.0.2 with SMTP id ruRbYY7687511xm49RnKXdnk; Tue, 26 Sep 2023 14:28:39 -0700 X-Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web11.3308.1695763718475865521 for ; Tue, 26 Sep 2023 14:28:38 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="467959317" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="467959317" X-Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Sep 2023 14:28:37 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10845"; a="872645683" X-IronPort-AV: E=Sophos;i="6.03,179,1694761200"; d="scan'208";a="872645683" X-Received: from fmsmsx603.amr.corp.intel.com ([10.18.126.83]) by orsmga004.jf.intel.com with ESMTP/TLS/AES256-GCM-SHA384; 26 Sep 2023 14:28:37 -0700 X-Received: from fmsmsx612.amr.corp.intel.com (10.18.126.92) by fmsmsx603.amr.corp.intel.com (10.18.126.83) 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 14:28:37 -0700 X-Received: from fmsmsx610.amr.corp.intel.com (10.18.126.90) by fmsmsx612.amr.corp.intel.com (10.18.126.92) 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 14:28:36 -0700 X-Received: from fmsedg601.ED.cps.intel.com (10.1.192.135) by fmsmsx610.amr.corp.intel.com (10.18.126.90) 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 14:28:36 -0700 X-Received: from NAM04-BN8-obe.outbound.protection.outlook.com (104.47.74.45) by edgegateway.intel.com (192.55.55.70) 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 14:28:36 -0700 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=XoGxio+pyEeUxjVY0gVYxNFEAcLPuveHgR2/+VT/NvpmhW0GT8y3E2i4jGft46Apg812QTjxTxRaZzeZs+aDKYaZ5W7paEPdmVvTyCsOGda7ZNOFwKD358wsRuPCn6MTVPihG0BbNjNnLA8ltg868sKL0TNFQt1HJ/uOPOHlqjVyqAKjvAHOoH485UDv8z3ZxKUNEFd+MuyqpAE5D5GRNHpjufgfPzm7gSTF7QxkH1FNic9K6k4HBs93qdRed1Ev1sS+6zE/wwgOlHpWzQgpVMaCIQZ7eb7xaJxDzEvTFtjk9st2ZrQjBX6kfOeDcRm8+SQeC1kDVOEwO4sj0BYcSQ== 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=O6gFxsI9G3dbdTs5WRS+v3147Rpz+L9OuDJBBKBJ5So=; b=HOcwmn/fTmt07AGZRlz0gGPddL222elwgIVA7eQeawDB/bDB7w07oWyP8f85MGHD2woCTYWtuVqtav83nFIku/EM9jOwm1cn4+QV9Hl5Sy3VS1RTR3EdUsERjfS8B2Aj7pnVYgK2Me+CnVYsRZpfSBi6KAGIknH+wy+zf4ttAL4QU5zrcINU5UGTqjb5J5pFpOyd9jN9tBmutyBVj5+pmKCfY/PQ/kjJpd6CvE7j5+jBGB2d6Ju9QATmJoRtAgy2v5HWjmFUE2Rtdz62Ty5oVbeRO4KdnFfHZ+DBNMmpiXR+W0NCW1z64iIOdSKprxu9pEHgFdmnzR7IYHLKTXyjTg== 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 DS0PR11MB7969.namprd11.prod.outlook.com (2603:10b6:8:120::12) 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 21:28: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 21:28: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+4eW5jkeRufIXz2NvSLAtje2wgAAFmzCAAA/OkA== Date: Tue, 26 Sep 2023 21:28:33 +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: SA2PR11MB4938:EE_|DS0PR11MB7969:EE_ x-ms-office365-filtering-correlation-id: 1868765f-c3c2-4638-1ec3-08dbbed78966 x-ms-exchange-senderadcheck: 1 x-ms-exchange-antispam-relay: 0 x-microsoft-antispam-message-info: OGRrFIEE+kzbaBJaeMBCnxFwAtR+pifE2PoIM9Idhu/PyUxxOVOqv5SWJEOFYIbRCHrvkYno4lZXYqYGW6QS1CpYQ3DoZ5g2OhiljN11pOuhesFCORq5EU7QZT3VF2upXymgeYJpPuoI0iALsq8YobIt7mNFQ6WVkcSKlfroPbBT7JShxzdAElQJ81QdrziQ+98LvHRiM13m2eMAcPiunMlum0961X8+cBE09JXV1BnWNbskHi+eB3wQMaNLFGo8T3vXBeQJB1lviWH4o51ch/3wJYHqw1JlmCnMW9K6Bli/2/KonCGiZdQcyTjaCY67sjqJHActg1s2MaFayZ5I586YrKcm04E410fr+9eXMK7Z3TgefOo3pT5nR0QBYr/n/OWABK9iFsGVQ27/4i/+oDMi6m9c3rJxlZizgIypi1wmVjw8v+c4EU7dkfA7s3w2ATlZcnwmk0nK+nUgcZPw6ob/ge9Cyhyi3dCUSYYTLAdUjA87ZMd/gxg97NuCJ391PQ96gv6khAFsbXf5NA5Iuaqt07xhnL/grkzXKlFTf/lcGsz8qcgglLNoZ3vQhtDJLDoYb7VAZwyK0EtIH3HidLGLlnebqF0wEV/JP79EXVMqhBCD/2r6xkPBQLO5ZXnBL5rNmRMzFdx6xi7MYMlZ5+WCBZ0WWS6EfyIZ7/WAJvG7gfyIJO3PIq+B4imvW550 x-ms-exchange-antispam-messagedata-chunkcount: 1 x-ms-exchange-antispam-messagedata-0: =?us-ascii?Q?FMxsjG4BPQaF04Y3Xu6Qoya3sc5XDOioKO6xWmW1tS5VlpbFV1RJiecEE6pQ?= =?us-ascii?Q?9wGSU7DtDe6BO8L+edt9YivEQohP6JDld6Qp9JjPF2sXfkKTiwL9hytDSPsb?= =?us-ascii?Q?GHkZ7fsreWTXBvsvMbngHNjnGvB86zRgsbD0nDb+lBh9Z8UccoKW6ye3w0gi?= =?us-ascii?Q?DZ39fIMn+c4LDywSAv6WAS2HZ6K4NqS3VU0xkT/tdxA8dc+C3j33tsoFbiQ+?= =?us-ascii?Q?sQe0DSDh12HX3fUfhTDH7AOyjC8MPfppbF+BgrzhVSkTh2c5qPaLUC2XsoFz?= =?us-ascii?Q?C6qArUEykOTvOT7drhj3/tVwYOlQEld3d9PTDQjIdgVPDxkBkcN7UzbFrI08?= =?us-ascii?Q?jMRA8vLli4vT37XhK26f9A4RXE3J4Itb0jaGuwTwT9vlkdaDZw2G8azrGDzm?= =?us-ascii?Q?DQyQjwmt8tY0O6zg5mDWp8o61sOMwBir+nSGSHRXuWIThiIA3cAii7yhYSJA?= =?us-ascii?Q?ziGWkzznbx9KTt+IvlBXDkSyAvW8ygzJKrq2Vyc7O+y4WQPJq/8M22xzIdHM?= =?us-ascii?Q?SKJJkaZixP3ff7zCo4uAgf4i+XOJrF8NNp/0plpiUMm6mrWxJIbM2PhlIX4R?= =?us-ascii?Q?TSC40cm1ISxwZXeqvEMjfR3nTI2ZAg7RhDaw9WVEfn86QDzyXVfnvrEpffyh?= =?us-ascii?Q?13ImeVh5Wy/SAiBmlzSyWIOw32YqOO1gPLn791lEsAvCOJWf9NiM5IzRqCoN?= =?us-ascii?Q?GH6+HnPfsOlfrKB6J0Txgp1jakBWwUgZlFC7gW/t4C+wxHEI2vgmiKlsP7R8?= =?us-ascii?Q?VfryswQ2mAH5lHg+b/ZW+IhSVfVfHxav7MrrFD020czPMNuwrTxvra/tdaI1?= =?us-ascii?Q?Rxz0fdMT2CSjk/G2qC/W8pT2pWpz/lDZMGqEWK4e++2eR7exAGV5sQsxygXw?= =?us-ascii?Q?Dxr1C0SgV7B2nGWI0GbkAWq3IiveWtek5mQMeSGeXZ64Sozolv9+F6u9HP/C?= =?us-ascii?Q?+BZGBhKd6+P5PuV1hHkAv1wj9VZ67ol7yicBmvDEE9reEZubRdx3g0HEyoIQ?= =?us-ascii?Q?iB/KkuEL018KiFymOvO3geq+9ai6V8FJJWB1NTlCb+MaoiEa9XnA/0rom3b5?= =?us-ascii?Q?fY4I2/2f6pCnbRKKBMMFCfZARcE8FPdDE/fkPrKkQFcwBdoW9clgKARjA+xo?= =?us-ascii?Q?y7gx6ma4mXUa96SDKx91ItFG16Nf3KNntJmjNvvlqWpdvuXHuIh+4PpeFkdD?= =?us-ascii?Q?CyDjcVSBFip2MvuyWd2Gep/1U1qRjLGhqF7KaiV750jHTjadDzQJE3YJ1BAg?= =?us-ascii?Q?Rc/W+qadrm9KPqD4TS5D6WUVKG8nXkGliTOGFCJIErKIuEXH0+8UamKIar4s?= =?us-ascii?Q?okbfE7v+Vq+Iw0fcngrceWfO8Dm5FPKXlkvRibT3vwiRac3PPa2psERbx0km?= =?us-ascii?Q?8BNx05iLLdTqqFStVHURsEqdxJC023Jn+g29Xb0HY/Al7zZCFmyKbSBDQMbG?= =?us-ascii?Q?JtBZz6HZSfIspGIuV/PN4iSTFAj7ewe/tzOoojF7m5Ljr/AGWN+GKFOQZPcT?= =?us-ascii?Q?92e/g5CKNhBvUbjQRmvEy2Jkqg9ps6jjDBUz2X2GEb57Ik774opxO8EP6Lsu?= =?us-ascii?Q?6+wEzz92FxjXcz415J46GlEIELRHpFDxXfpyAZVY?= 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: 1868765f-c3c2-4638-1ec3-08dbbed78966 X-MS-Exchange-CrossTenant-originalarrivaltime: 26 Sep 2023 21:28:33.5383 (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: lnSG0VtVf94R+kpgmG7eH7e8UPJZZyBH1nBZ2zQRH/0y4tt5c+y+8P4bWvtpgfcUw2MDykOmORq83sX6PEp6YLOxRnw3I979z7bSo9WBwEs= X-MS-Exchange-Transport-CrossTenantHeadersStamped: DS0PR11MB7969 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: 2xbyxZiFagdcYnqDvD9RQOdgx7686176AA= 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=e+0C+oK5; 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}") I have VS Code on Windows working today using the PDB based symbol file. The following lines in the EmulatorPkg DSC file force 4KB alignment and the= DLL export of the InitializeDriver symbol https://github.com/tianocore/edk2/blob/bf0bdacdd6f6cdd2e9ac5db14b6daf19a5a5= bd57/EmulatorPkg/EmulatorPkg.dsc#L497 MSFT:*_*_*_DLINK_FLAGS =3D /ALIGN:4096 /FILEALIGN:4096 /SUBSYSTEM:CON= SOLE MSFT:DEBUG_*_*_DLINK_FLAGS =3D /EXPORT:InitializeDriver=3D$(IMAGE_ENTRY_P= OINT) /BASE:0x10000 MSFT:NOOPT_*_*_DLINK_FLAGS =3D /EXPORT:InitializeDriver=3D$(IMAGE_ENTRY_P= OINT) /BASE:0x10000 I think this is why it is working for me without this change. Are you suggesting that with this change these can be removed? Mike > -----Original Message----- > From: Desimone, Nathaniel L > Sent: Tuesday, September 26, 2023 1:32 PM > To: Kinney, Michael D ; > devel@edk2.groups.io > Cc: Andrew Fish ; Ni, Ray ; Chiu, > Chasel > Subject: RE: [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows >=20 > Hi Mike, >=20 > Source level debug with VS Code does indeed work today with gdb or > lldb. This change makes the Visual Studio Windows debugger work as > well. >=20 > You are correct that if the same DLL is loaded more than once that > this method cannot perform source level debug on the second instance; > but this change 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 instead 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 compliance is maintained. This > behavior is unchanged from the original code. >=20 > Yes, this code enables PE/COFF images that do not have sections that > are 4KB 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. >=20 > Thanks, > Nate >=20 > -----Original Message----- > From: Kinney, Michael D > Sent: Tuesday, September 26, 2023 1:08 PM > To: Desimone, Nathaniel L ; > devel@edk2.groups.io > Cc: Andrew Fish ; Ni, Ray ; Chiu, > Chasel ; Kinney, Michael D > > Subject: RE: [PATCH v1] EmulatorPkg: Fix Source Level Debug on Windows >=20 > Hi Nate, >=20 > I am able to do source level debug of EmulatorPkg using VS Code today. >=20 > What scenarios are broken? >=20 > 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. >=20 > It also appears that this change can support PE/COFF images that do > not have sections that are 4KB aligned and handles the page protection > settings for the user mode application env. Is that correct? >=20 > Mike >=20 > > -----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 > > > > 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. > > > > 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. > > > > 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(). > > > > With this fix, the debugging experience is now so good it is > > unprecedented! > > In Visual Studio Code, add the following to launch.json: > > > > { > > "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, > > } > > ] > > } > > > > 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. > > > > 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(-) > > > > 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. > > > > -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; > > } > > > > +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; > > > > 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. > > // > > > > DllEntryPoint =3D NULL; > > @@ -1106,27 +1125,166 @@ PeCoffLoaderRelocateImageExtraAction ( > > } > > > > // > > - // 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'; > > > > // > > - // 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; > > + } > > + } > > + } > > } > > > > 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 (#109093): https://edk2.groups.io/g/devel/message/109093 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-