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 7108A941E5F for ; Wed, 20 Dec 2023 21:26:56 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=bv7q0IMA3SKivkZlBcK7aKOLhYzNs1Ql6f4M2N3c5NY=; c=relaxed/simple; d=groups.io; h=Feedback-ID:Message-ID:Date:MIME-Version:User-Agent:Subject:To:Cc:References:From:In-Reply-To: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=1703107615; v=1; b=ekfq+jT4QW5wHmV3rzxxzcdRdZVNN1uMhD0LP0Ofz3hEY2bbMTIcyMT/P175YUPPBXwHpM// k0JZQX74Glz0RWocn0TfubtcgyOVAhAdaU9oKb0u4MW5lAhbsjgH7+ZiUrne9lr0l3UpI86bmbL 01KYPXOPnKcI3x8wN22ZifNI= X-Received: by 127.0.0.2 with SMTP id 3lBwYY7687511xNNHxnYyPaR; Wed, 20 Dec 2023 13:26:55 -0800 X-Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) by mx.groups.io with SMTP id smtpd.web10.33111.1703107614238852225 for ; Wed, 20 Dec 2023 13:26:54 -0800 X-Received: from compute6.internal (compute6.nyi.internal [10.202.2.47]) by mailout.nyi.internal (Postfix) with ESMTP id 87AF55C0CC2; Wed, 20 Dec 2023 16:26:53 -0500 (EST) X-Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 20 Dec 2023 16:26:53 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdduvddgudegkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefkffggfgfuvfevfhfhjggtgfesthejredttddvjeenucfhrhhomheptfgv sggvtggtrgcuvehrrghnuceorhgvsggvtggtrgessghsughiohdrtghomheqnecuggftrf grthhtvghrnhephfekvdekvdfhtddvteehueeuleetjefhieehjeeuhfdtuddtvdeguddt keevlefhnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh eprhgvsggvtggtrgessghsughiohdrtghomh X-ME-Proxy: Feedback-ID: i5b994698:Fastmail X-Received: by mail.messagingengine.com (Postfix) with ESMTPA; Wed, 20 Dec 2023 16:26:51 -0500 (EST) Message-ID: <739beb9c-a10d-4dec-b228-3b064bc1e358@bsdio.com> Date: Wed, 20 Dec 2023 14:26:47 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [edk2-devel] [PATCH v2] BaseTools/GenFw: Correct offset when relocating an ADR To: Jake Garver , devel@edk2.groups.io Cc: gaoliming@byosoft.com.cn, bob.c.feng@intel.com, yuwei.chen@intel.com, ardb+tianocore@kernel.org, pedro.falcato@gmail.com References: <1089e51f1e60222d591d92de518e664be7843123.1703099891.git.jake@nvidia.com> From: "Rebecca Cran" In-Reply-To: <1089e51f1e60222d591d92de518e664be7843123.1703099891.git.jake@nvidia.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,rebecca@bsdio.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: g9vRDZFqRJve9VfunlF63Ygwx7686176AA= Content-Language: en-US Content-Type: text/plain; charset=UTF-8; format=flowed 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=ekfq+jT4; dmarc=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 Reviewed-by: Rebecca Cran On 12/20/2023 12:31 PM, Jake Garver wrote: > In the R_AARCH64_ADR_GOT_PAGE case on AARCH64, we may encounter an ADR > instead of an ADRP when the toolchain is working around Cortex-A53 > erratum #843419. If that's the case, be sure to calculate the offset > appropriately. > > This resolves an issue experienced when building a StandaloneMm image > with stack protection enabled on GCC compiled with > "--enable-fix-cortex-a53-843419". This scenario sometimes generates an > ADR with a R_AARCH64_ADR_GOT_PAGE relocation. > > In this scenario, the following code is being generated by the > toolchain: > > # Load to set the stack canary > 2ffc: 10028020 adr x0, 8000 > 3008: f940d400 ldr x0, [x0, #424] > > # Load to check the stack canary > 30cc: b0000020 adrp x0, 8000 > 30d0: f940d400 ldr x0, [x0, #424] > > GenFw rewrote that to: > > # Load to set the stack canary > 2ffc: 10000480 adr x0, 0x308c > 3008: 912ec000 add x0, x0, #0xbb0 > > # Load to check the stack canary > 30cc: f0000460 adrp x0, 0x92000 > 30d0: 912ec000 add x0, x0, #0xbb0 > > Note that we're now setting the stack canary from the wrong address, > resulting in an erroneous stack fault. > > After this fix, the offset will be calculated correctly for an ADR and > the stack canary is set correctly. > > Signed-off-by: Jake Garver > --- > > Notes: > v2: Implement approach proposed by Ard Biesheuvel. > - title changed to: Correct offset when relocating an ADR > v1: Original title: Change opcode when converting ADR to ADRP > > BaseTools/Source/C/GenFw/Elf64Convert.c | 22 +++++++++++++++++++++- > 1 file changed, 21 insertions(+), 1 deletion(-) > > diff --git a/BaseTools/Source/C/GenFw/Elf64Convert.c b/BaseTools/Source/C= /GenFw/Elf64Convert.c > index 9911db65af..9d04fc612e 100644 > --- a/BaseTools/Source/C/GenFw/Elf64Convert.c > +++ b/BaseTools/Source/C/GenFw/Elf64Convert.c > @@ -1562,7 +1562,27 @@ WriteSections64 ( > // subsequent LDR instruction (covered by a R_AARCH64_LD64_= GOT_LO12_NC > // relocation) into an ADD instruction - this is handled ab= ove. > // > - Offset =3D (Sym->st_value - (Rel->r_offset & ~0xfff)) >> 12; > + // In order to handle Cortex-A53 erratum #843419, the GCC to= olchain > + // may convert an ADRP instruction at the end of a page (0xf= fc > + // offset) into an ADR instruction. If so, be sure to calcul= ate the > + // offset for an ADR instead of ADRP. > + // > + if ((*(UINT32 *)Targ & BIT31) =3D=3D 0) { > + // > + // Calculate the offset for an ADR. > + // > + Offset =3D (Sym->st_value & ~0xfff) - Rel->r_offset; > + if (Offset < -0x100000 || Offset > 0xfffff) { > + Error (NULL, 0, 3000, "Invalid", "WriteSections64(): %s = due to its size (> 1 MB), unable to relocate ADR.", > + mInImageName); > + break; > + } > + } else { > + // > + // Calculate the offset for an ADRP. > + // > + Offset =3D (Sym->st_value - (Rel->r_offset & ~0xfff)) >> 1= 2; > + } > =20 > *(UINT32 *)Targ &=3D 0x9000001f; > *(UINT32 *)Targ |=3D ((Offset & 0x1ffffc) << (5 - 2)) | ((O= ffset & 0x3) << 29); -=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 (#112770): https://edk2.groups.io/g/devel/message/112770 Mute This Topic: https://groups.io/mt/103287393/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-