From nobody Sat Feb 7 08:43:51 2026 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+100090+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+100090+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; t=1676301519; cv=none; d=zohomail.com; s=zohoarc; b=KR4CGJbn+LKwFF/oIE7P5UvY5qXyXcP1XkUI6GJ/7aupBoiwqoMkU/RQ4EQwonP+Yt98kJxIbL5J4p3jMM5tYXBTIOo0SyeIoAK0mWebbdqcUe1hFgKvShaahKnSQ1+sC3jd2nHHdDRSrGjE6HjOS/5sHyndpfmZ4Psd08+84oI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1676301519; h=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=OjubfHLiwkvsqADAayaZZL/gwMW+99LtJKmZuItVfv4=; b=WW8gfmRai1YHtyyRn76vks7TR/KNaUgqSblvMcqQ9p92eOH187ZfrjjV2iwBYvir/hwsPZkd0x8Uu9TLccod39tQePISY2sv+UCJpEsmGtfGbs4IRIIKmpPaV4fHT53ioFwTw2NunU8epFe9/ICOcv8409XAa0K6KlwXJXexkFs= 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+100090+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 1676301519351784.2482547965734; Mon, 13 Feb 2023 07:18:39 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id 7q5gYY1788612xUZzDIhknyl; Mon, 13 Feb 2023 07:18:39 -0800 X-Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by mx.groups.io with SMTP id smtpd.web11.16976.1676301517872236489 for ; Mon, 13 Feb 2023 07:18:38 -0800 X-Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 551D461124; Mon, 13 Feb 2023 15:18:37 +0000 (UTC) X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 21314C433A0; Mon, 13 Feb 2023 15:18:33 +0000 (UTC) From: "Ard Biesheuvel" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Michael Kinney , Liming Gao , Jiewen Yao , Michael Kubacki , Sean Brogan , Rebecca Cran , Leif Lindholm , Sami Mujawar , Taylor Beebe , Matthew Garrett , Peter Jones , Kees Cook Subject: [edk2-devel] [RFC 03/13] MdePkg/BasePeCoffLib: Add API to keep track of relocation range Date: Mon, 13 Feb 2023 16:18:00 +0100 Message-Id: <20230213151810.2301480-4-ardb@kernel.org> In-Reply-To: <20230213151810.2301480-1-ardb@kernel.org> References: <20230213151810.2301480-1-ardb@kernel.org> 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,ardb@kernel.org X-Gm-Message-State: iUUidWqxZAqbPfci0bwMFNk8x1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1676301519; bh=9KoPivDTnbsxUXfYZn1Xq1sweGHkHZn7sicTQKd+6+s=; h=Cc:Date:From:Reply-To:Subject:To; b=vuYbSGOj/KqNX2yGB73ftn8+y4LrEmzkRRUDTNGCMsx4QwD+RgBLtetfUE/DVTX2xq3 CD4Pyj3iWUNroBadVGUeGEqdTNbc1EljlfX26q21ZXEfhItOxBx/5rTNK6FQFnJAHB4d6 vsqtQ0lqTq/D1YoMV6qA9funN7UryLh52qs= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1676301521551100013 Content-Type: text/plain; charset="utf-8" Add a library call to obtain the start and end of the region covered by relocation fixups. This will be used in a future patch to limit the range of memory that needs to be remapped with read-write-execute permissions at ExitBootServices() time. Signed-off-by: Ard Biesheuvel --- MdePkg/Include/Library/PeCoffLib.h | 25 ++++++ MdePkg/Library/BasePeCoffLib/BasePeCoff.c | 83 +++++++++++++++++++- 2 files changed, 107 insertions(+), 1 deletion(-) diff --git a/MdePkg/Include/Library/PeCoffLib.h b/MdePkg/Include/Library/Pe= CoffLib.h index b45879453785..3706c8a4e858 100644 --- a/MdePkg/Include/Library/PeCoffLib.h +++ b/MdePkg/Include/Library/PeCoffLib.h @@ -382,4 +382,29 @@ PeCoffLoaderUnloadImage ( IN OUT PE_COFF_LOADER_IMAGE_CONTEXT *ImageContext ); =20 +/** + Retrieve the range subject to relocation fixups from the recorded fixup = data + of a runtime image + + @param ImageBase The base address of a PE/COFF image tha= t has been loaded + and relocated into system memory. + @param VirtImageBase The request virtual address that the PE= /COFF image is to + be fixed up for. + @param ImageSize The size, in bytes, of the PE/COFF imag= e. + @param RelocationData A pointer to the relocation data that w= as collected when the + PE/COFF image was relocated using PeCof= fLoaderRelocateImage(). + @param[out] RelocationRangeMin The start of the relocated range. + @param[out] RelocationRangeMax The end of the relocated range. + +**/ +VOID +EFIAPI +PeCoffLoaderGetRelocationRange ( + IN PHYSICAL_ADDRESS ImageBase, + IN UINTN ImageSize, + IN VOID *RelocationData, + OUT PHYSICAL_ADDRESS *RelocationRangeMin, + OUT PHYSICAL_ADDRESS *RelocationRangeMax + ); + #endif diff --git a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c b/MdePkg/Library/Bas= ePeCoffLib/BasePeCoff.c index 97a8aaf8c73d..10f3d04d2490 100644 --- a/MdePkg/Library/BasePeCoffLib/BasePeCoff.c +++ b/MdePkg/Library/BasePeCoffLib/BasePeCoff.c @@ -936,6 +936,8 @@ PeCoffLoaderRelocateImage ( PHYSICAL_ADDRESS BaseAddress; UINT32 NumberOfRvaAndSizes; UINT32 TeStrippedOffset; + PHYSICAL_ADDRESS *RelocRangeStart; + PHYSICAL_ADDRESS *RelocRangeEnd; =20 ASSERT (ImageContext !=3D NULL); =20 @@ -1043,6 +1045,21 @@ PeCoffLoaderRelocateImage ( // Run the relocation information and apply the fixups // FixupData =3D ImageContext->FixupData; + if (FixupData !=3D NULL) { + FixupData =3D ALIGN_POINTER (FixupData, sizeof (PHYSICAL_ADDRESS)); + + // + // Use the first two UINT64s in the fixup data to keep track of the = start + // and end of the region that is subject to relocation fixups. + // + RelocRangeStart =3D (PHYSICAL_ADDRESS *)FixupData; + RelocRangeEnd =3D RelocRangeStart + 1; + FixupData +=3D 2 * sizeof (PHYSICAL_ADDRESS); + + *RelocRangeStart =3D MAX_UINT64; + *RelocRangeEnd =3D 0; + } + while ((UINTN)RelocBase < (UINTN)RelocBaseEnd) { Reloc =3D (UINT16 *)((CHAR8 *)RelocBase + sizeof (EFI_IMAGE_BASE_REL= OCATION)); // @@ -1070,6 +1087,14 @@ PeCoffLoaderRelocateImage ( return RETURN_LOAD_ERROR; } =20 + // + // Capture this page in the recorded relocation range + // + if (FixupData !=3D NULL) { + *RelocRangeStart =3D MIN (*RelocRangeStart, (UINTN)FixupBase); + *RelocRangeEnd =3D MAX (*RelocRangeEnd, (UINTN)FixupBase + SIZE_= 4KB); + } + // // Run this relocation record // @@ -1470,6 +1495,9 @@ PeCoffLoaderLoadImage ( // ImageContext->FixupData =3D NULL; =20 + // Add two UINT64s at the start to carry the min/max of the relocated ra= nge + ImageContext->FixupDataSize +=3D 2 * sizeof (PHYSICAL_ADDRESS); + // // Load the Codeview information if present // @@ -1824,7 +1852,8 @@ PeCoffLoaderRelocateImageForRuntime ( // by code will not be fixed up, since that would set them back to // defaults. // - FixupData =3D RelocationData; + FixupData =3D ALIGN_POINTER (RelocationData, sizeof (PHYSICAL_ADDR= ESS)); + FixupData +=3D 2 * sizeof (PHYSICAL_ADDRESS); RelocBaseOrig =3D RelocBase; while ((UINTN)RelocBase < (UINTN)RelocBaseEnd) { // @@ -1994,3 +2023,55 @@ PeCoffLoaderUnloadImage ( PeCoffLoaderUnloadImageExtraAction (ImageContext); return RETURN_SUCCESS; } + +/** + Retrieve the range subject to relocation fixups from the recorded fixup = data + of a runtime image + + @param ImageBase The base address of a PE/COFF image tha= t has been loaded + and relocated into system memory. + @param VirtImageBase The request virtual address that the PE= /COFF image is to + be fixed up for. + @param ImageSize The size, in bytes, of the PE/COFF imag= e. + @param RelocationData A pointer to the relocation data that w= as collected when the + PE/COFF image was relocated using PeCof= fLoaderRelocateImage(). + @param[out] RelocationRangeMin The start of the relocated range. + @param[out] RelocationRangeMax The end of the relocated range. + +**/ +VOID +EFIAPI +PeCoffLoaderGetRelocationRange ( + IN PHYSICAL_ADDRESS ImageBase, + IN UINTN ImageSize, + IN VOID *RelocationData, + OUT PHYSICAL_ADDRESS *RelocationRangeMin, + OUT PHYSICAL_ADDRESS *RelocationRangeMax + ) +{ + PHYSICAL_ADDRESS *FixupData; + + if ((RelocationData =3D=3D NULL) || (ImageBase =3D=3D 0x0)) { + return; + } + + FixupData =3D ALIGN_POINTER (RelocationData, sizeof (PHYSICAL_ADDRESS)); + + if ((FixupData[0] =3D=3D MAX_UINT64) && (FixupData[1] =3D=3D 0)) { + // No fixups recorded + *RelocationRangeMin =3D ImageBase; + *RelocationRangeMax =3D ImageBase; + return; + } + + if ((FixupData[0] < ImageBase) || + (FixupData[1] > (ImageBase + ImageSize))) { + ASSERT (FALSE); + *RelocationRangeMin =3D ImageBase; + *RelocationRangeMax =3D ImageBase + ImageSize; + return; + } + + *RelocationRangeMin =3D FixupData[0]; + *RelocationRangeMax =3D FixupData[1]; +} --=20 2.39.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 (#100090): https://edk2.groups.io/g/devel/message/100090 Mute This Topic: https://groups.io/mt/96937476/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-