From nobody Mon Apr 29 07:47:57 2024 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+77248+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+77248+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1624948067; cv=none; d=zohomail.com; s=zohoarc; b=ZXceF2l/x+6x0MEzL1FhtVbwkMMWoc1KIa3O6yTNTbHS/TDzCoJ4XhnJJJ19fO3uG+UtDlixLq6ChAlzGhirPAIusjVO6eliqYMFk0vWRgaa2lFQmQssNZJbM2BAFPOg/dLOu4OZ15bnXr71zhYecN/dsR3m1K5DiOIHCc6Qx1I= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624948067; 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=SM6g8AzdBA8P0dZVI1tWIsQddqpjq5mN71W3DehC4Co=; b=ded52viDZg3p7lwNiLQRioGGhWJJQ7DmeV0FNjqpqVQXX9z9YdjU0wwnRPRyubp8c49+0xq5yJmNrp3WpWDcDqx2GIUb8ThudymZe82iMMA1LVHsjZcDkKPkuv6kO/+yq2homm0aOXQgema+rAysSicmtqt4AxWpqhQ+6M4n11g= 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+77248+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 1624948066999637.7334264053768; Mon, 28 Jun 2021 23:27:46 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 9k4xYY1788612xuFBVppzuwn; Mon, 28 Jun 2021 23:27:46 -0700 X-Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web12.4521.1624948062539165550 for ; Mon, 28 Jun 2021 23:27:45 -0700 X-IronPort-AV: E=McAfee;i="6200,9189,10029"; a="208130358" X-IronPort-AV: E=Sophos;i="5.83,308,1616482800"; d="scan'208";a="208130358" X-Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jun 2021 23:27:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,308,1616482800"; d="scan'208";a="446914482" X-Received: from ray-dev.ccr.corp.intel.com ([10.239.158.87]) by orsmga007.jf.intel.com with ESMTP; 28 Jun 2021 23:27:41 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Maurice Ma , Guo Dong , Benjamin You Subject: [edk2-devel] [PATCH 1/2] UefiPayloadPkg/PayloadLoader: Fix bug in locating relocation section Date: Tue, 29 Jun 2021 14:27:06 +0800 Message-Id: <20210629062707.1855-2-ray.ni@intel.com> In-Reply-To: <20210629062707.1855-1-ray.ni@intel.com> References: <20210629062707.1855-1-ray.ni@intel.com> 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,ray.ni@intel.com X-Gm-Message-State: pJj601KtoRjdVV1uLQayt1Xkx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1624948066; bh=vgVryTjG5We4TlpzKqfAk9wNhrI0pn7sIW+WHGXmVZc=; h=Cc:Date:From:Reply-To:Subject:To; b=NBu3WlWmoGqwZdcnyV1M62uOah62WeqY5ay3hRkWuOwq+8u4xmdCJ35ChkUsZny6sqG bkvd27cMOAnumUOHJqTp4cT6c05tc0ECtYLw6knvvuZzpnepdmbITtaahsI49izUUPMmm O2v+mYnZs34xEm02OdDMfsMKwPW2zwFYSqo= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" Per ELF spec, the DT_REL/DT_RELA tag in dynamic section stores the virtual address of the relocation section. But today's code logic treats it as the section offset and finds the relocation section whose offset equals to DT_REL/DT_RELA. The logic can work when the section offset equals to the section virtual address. But when the ELF is generated from the link script that reserves a sizeof(pe_header) in the file beginning, the section offset doesn't equal to section virtual address. Such logic can not find the relocation section. The patch fixes this bug. Signed-off-by: Ray Ni Cc: Maurice Ma Cc: Guo Dong Cc: Benjamin You Reviewed-by: Guo Dong --- .../PayloadLoaderPeim/ElfLib/Elf32Lib.c | 22 +++++++++++++------ .../PayloadLoaderPeim/ElfLib/Elf64Lib.c | 22 +++++++++++++------ 2 files changed, 30 insertions(+), 14 deletions(-) diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c b/UefiPaylo= adPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c index 3fa100ce4a..dd27d3ce59 100644 --- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c +++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c @@ -206,7 +206,7 @@ RelocateElf32Dynamic ( Elf32_Shdr *DynShdr; Elf32_Shdr *RelShdr; Elf32_Dyn *Dyn; - UINT32 RelaOffset; + UINT32 RelaAddress; UINT32 RelaCount; UINT32 RelaSize; UINT32 RelaEntrySize; @@ -246,7 +246,7 @@ RelocateElf32Dynamic ( // // 2. Locate the relocation section from the dynamic section. // - RelaOffset =3D MAX_UINT32; + RelaAddress =3D MAX_UINT32; RelaSize =3D 0; RelaCount =3D 0; RelaEntrySize =3D 0; @@ -265,8 +265,8 @@ RelocateElf32Dynamic ( // based on the original file value and the memory base address. // For consistency, files do not contain relocation entries to ``c= orrect'' addresses in the dynamic structure. // - RelaOffset =3D Dyn->d_un.d_ptr - (UINT32) (UINTN) ElfCt->Preferred= ImageAddress; - RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL; + RelaAddress =3D Dyn->d_un.d_ptr; + RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL; break; case DT_RELACOUNT: case DT_RELCOUNT: @@ -285,7 +285,7 @@ RelocateElf32Dynamic ( } } =20 - if (RelaOffset =3D=3D MAX_UINT64) { + if (RelaAddress =3D=3D MAX_UINT64) { ASSERT (RelaCount =3D=3D 0); ASSERT (RelaEntrySize =3D=3D 0); ASSERT (RelaSize =3D=3D 0); @@ -298,8 +298,16 @@ RelocateElf32Dynamic ( // // Verify the existence of the relocation section. // - RelShdr =3D GetElf32SectionByRange (ElfCt->FileBase, RelaOffset, RelaSiz= e); - ASSERT (RelShdr !=3D NULL); + RelShdr =3D NULL; + for (Index =3D 0; Index < ElfCt->ShNum; Index++) { + RelShdr =3D GetElf32SectionByIndex (ElfCt->FileBase, Index); + ASSERT (RelShdr !=3D NULL); + if ((RelShdr->sh_addr =3D=3D RelaAddress) && (RelShdr->sh_size =3D=3D = RelaSize)) { + break; + } + RelShdr =3D NULL; + } + if (RelShdr =3D=3D NULL) { return EFI_UNSUPPORTED; } diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c b/UefiPaylo= adPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c index e364807007..3f4f12903c 100644 --- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c +++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c @@ -215,7 +215,7 @@ RelocateElf64Dynamic ( Elf64_Shdr *DynShdr; Elf64_Shdr *RelShdr; Elf64_Dyn *Dyn; - UINT64 RelaOffset; + UINT64 RelaAddress; UINT64 RelaCount; UINT64 RelaSize; UINT64 RelaEntrySize; @@ -255,7 +255,7 @@ RelocateElf64Dynamic ( // // 2. Locate the relocation section from the dynamic section. // - RelaOffset =3D MAX_UINT64; + RelaAddress =3D MAX_UINT64; RelaSize =3D 0; RelaCount =3D 0; RelaEntrySize =3D 0; @@ -274,8 +274,8 @@ RelocateElf64Dynamic ( // based on the original file value and the memory base address. // For consistency, files do not contain relocation entries to ``c= orrect'' addresses in the dynamic structure. // - RelaOffset =3D Dyn->d_un.d_ptr - (UINTN) ElfCt->PreferredImageAddr= ess; - RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL; + RelaAddress =3D Dyn->d_un.d_ptr; + RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL; break; case DT_RELACOUNT: case DT_RELCOUNT: @@ -294,7 +294,7 @@ RelocateElf64Dynamic ( } } =20 - if (RelaOffset =3D=3D MAX_UINT64) { + if (RelaAddress =3D=3D MAX_UINT64) { ASSERT (RelaCount =3D=3D 0); ASSERT (RelaEntrySize =3D=3D 0); ASSERT (RelaSize =3D=3D 0); @@ -307,8 +307,16 @@ RelocateElf64Dynamic ( // // Verify the existence of the relocation section. // - RelShdr =3D GetElf64SectionByRange (ElfCt->FileBase, RelaOffset, RelaSiz= e); - ASSERT (RelShdr !=3D NULL); + RelShdr =3D NULL; + for (Index =3D 0; Index < ElfCt->ShNum; Index++) { + RelShdr =3D GetElf64SectionByIndex (ElfCt->FileBase, Index); + ASSERT (RelShdr !=3D NULL); + if ((RelShdr->sh_addr =3D=3D RelaAddress) && (RelShdr->sh_size =3D=3D = RelaSize)) { + break; + } + RelShdr =3D NULL; + } + if (RelShdr =3D=3D NULL) { return EFI_UNSUPPORTED; } --=20 2.31.1.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 (#77248): https://edk2.groups.io/g/devel/message/77248 Mute This Topic: https://groups.io/mt/83863241/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- From nobody Mon Apr 29 07:47:57 2024 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+77249+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+77249+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1624948067; cv=none; d=zohomail.com; s=zohoarc; b=SRHH+Wh7AkEVNrcMiVWOR0xgjmSRNxVusvpuAYK0nsIE8Ml02f5DZ7Pc9uQ9iWEGHnGpk8tmyPxyKGXUiE+sSiOZSVg7X9G/bOtbyF03ZcrXk21b1Kj9JOhMkE/Rj/4ZuNH3sqbH7V9ikWUn6c2Ov6gnrMZKHYBYu2P2NjJdpvY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1624948067; 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=M7p8DSKdkIaqe0ZsT4YV9a8Fa+qdjD24PMJtbhCtZPk=; b=jTRx9nNHi+Px8cc7HiyD1JkFRXc8lO2RLbpXBNZ4uLmt/6UFiYNCIV08YG9rPxMRObn7+HA+XjQQqLyN4+zvSndy2pZMzA9ZnC0qGMVXfJC0m2zhXovPu42VK3Lp3GLuu7fCfwHdVeeEt+EdDDb+tSaE82nIyH9MsBf0gYDPirw= 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+77249+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 1624948067522801.7649206740155; Mon, 28 Jun 2021 23:27:47 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id 6G1wYY1788612xpO1FemIxBz; Mon, 28 Jun 2021 23:27:47 -0700 X-Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by mx.groups.io with SMTP id smtpd.web08.4463.1624948065928071925 for ; Mon, 28 Jun 2021 23:27:46 -0700 X-IronPort-AV: E=McAfee;i="6200,9189,10029"; a="187779258" X-IronPort-AV: E=Sophos;i="5.83,308,1616482800"; d="scan'208";a="187779258" X-Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jun 2021 23:27:45 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.83,308,1616482800"; d="scan'208";a="446914487" X-Received: from ray-dev.ccr.corp.intel.com ([10.239.158.87]) by orsmga007.jf.intel.com with ESMTP; 28 Jun 2021 23:27:43 -0700 From: "Ni, Ray" To: devel@edk2.groups.io Cc: Maurice Ma , Guo Dong , Benjamin You Subject: [edk2-devel] [PATCH 2/2] UefiPayloadPkg/PayloadLoader: Remove assertion Date: Tue, 29 Jun 2021 14:27:07 +0800 Message-Id: <20210629062707.1855-3-ray.ni@intel.com> In-Reply-To: <20210629062707.1855-1-ray.ni@intel.com> References: <20210629062707.1855-1-ray.ni@intel.com> 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,ray.ni@intel.com X-Gm-Message-State: QTmkq9miRPvklKlgQUtDEpgrx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1624948067; bh=Q68XSoQg+NCvBSafO4ELT5HBWQEMTnGFVIksZhZX+KM=; h=Cc:Date:From:Reply-To:Subject:To; b=EW7sXBJlFDppI6MS8ffzYCjdJbmmURvdyryllJM/sabFxXRBDfztjvEywBQdAZtH/oU l0s9i1sLFDiCjvk4f1QZCF8kgwQMAvZRMw2lCO5UCywmmhxT4eFy/zEgyxnlYSqDL7pxn MO+/GUV96DQte5/1jeKQaJ6QDIugnaDergQ= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" For R_386_RELATIVE and R_X86_64_RELATIVE, today's logic assumes that the content pointed by the Rela->r_offset is 0 but it's not always TRUE. We observed that linker may set the content to Rela->r_addend. The patch removes the assertion. There is no functionality impact for this patch. Signed-off-by: Ray Ni Cc: Maurice Ma Cc: Guo Dong Cc: Benjamin You Reviewed-by: Guo Dong --- UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c | 1 - UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c | 1 - 2 files changed, 2 deletions(-) diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c b/UefiPaylo= adPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c index dd27d3ce59..780f2d9507 100644 --- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c +++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf32Lib.c @@ -164,7 +164,6 @@ ProcessRelocation32 ( // Calculation: B + A // if (RelaType =3D=3D SHT_RELA) { - ASSERT (*Ptr =3D=3D 0); *Ptr =3D (UINT32) Delta + Rela->r_addend; } else { // diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c b/UefiPaylo= adPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c index 3f4f12903c..0f1b06e8cc 100644 --- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c +++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/Elf64Lib.c @@ -173,7 +173,6 @@ ProcessRelocation64 ( // Calculation: B + A // if (RelaType =3D=3D SHT_RELA) { - ASSERT (*Ptr =3D=3D 0); *Ptr =3D Delta + Rela->r_addend; } else { // --=20 2.31.1.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 (#77249): https://edk2.groups.io/g/devel/message/77249 Mute This Topic: https://groups.io/mt/83863242/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-