From mboxrd@z Thu Jan 1 00:00:00 1970 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 Authentication-Results: mx.groups.io; dkim=missing; spf=pass (domain: intel.com, ip: 134.134.136.65, mailfrom: ray.ni@intel.com) 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" 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" 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: [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> X-Mailer: git-send-email 2.31.1.windows.1 In-Reply-To: <20210629062707.1855-1-ray.ni@intel.com> References: <20210629062707.1855-1-ray.ni@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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 --- .../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;=0D Elf32_Shdr *RelShdr;=0D Elf32_Dyn *Dyn;=0D - UINT32 RelaOffset;=0D + UINT32 RelaAddress;=0D UINT32 RelaCount;=0D UINT32 RelaSize;=0D UINT32 RelaEntrySize;=0D @@ -246,7 +246,7 @@ RelocateElf32Dynamic ( //=0D // 2. Locate the relocation section from the dynamic section.=0D //=0D - RelaOffset =3D MAX_UINT32;=0D + RelaAddress =3D MAX_UINT32;=0D RelaSize =3D 0;=0D RelaCount =3D 0;=0D RelaEntrySize =3D 0;=0D @@ -265,8 +265,8 @@ RelocateElf32Dynamic ( // based on the original file value and the memory base address.=0D // For consistency, files do not contain relocation entries to ``c= orrect'' addresses in the dynamic structure.=0D //=0D - RelaOffset =3D Dyn->d_un.d_ptr - (UINT32) (UINTN) ElfCt->Preferred= ImageAddress;=0D - RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL;=0D + RelaAddress =3D Dyn->d_un.d_ptr;=0D + RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL;=0D break;=0D case DT_RELACOUNT:=0D case DT_RELCOUNT:=0D @@ -285,7 +285,7 @@ RelocateElf32Dynamic ( }=0D }=0D =0D - if (RelaOffset =3D=3D MAX_UINT64) {=0D + if (RelaAddress =3D=3D MAX_UINT64) {=0D ASSERT (RelaCount =3D=3D 0);=0D ASSERT (RelaEntrySize =3D=3D 0);=0D ASSERT (RelaSize =3D=3D 0);=0D @@ -298,8 +298,16 @@ RelocateElf32Dynamic ( //=0D // Verify the existence of the relocation section.=0D //=0D - RelShdr =3D GetElf32SectionByRange (ElfCt->FileBase, RelaOffset, RelaSiz= e);=0D - ASSERT (RelShdr !=3D NULL);=0D + RelShdr =3D NULL;=0D + for (Index =3D 0; Index < ElfCt->ShNum; Index++) {=0D + RelShdr =3D GetElf32SectionByIndex (ElfCt->FileBase, Index);=0D + ASSERT (RelShdr !=3D NULL);=0D + if ((RelShdr->sh_addr =3D=3D RelaAddress) && (RelShdr->sh_size =3D=3D = RelaSize)) {=0D + break;=0D + }=0D + RelShdr =3D NULL;=0D + }=0D +=0D if (RelShdr =3D=3D NULL) {=0D return EFI_UNSUPPORTED;=0D }=0D 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;=0D Elf64_Shdr *RelShdr;=0D Elf64_Dyn *Dyn;=0D - UINT64 RelaOffset;=0D + UINT64 RelaAddress;=0D UINT64 RelaCount;=0D UINT64 RelaSize;=0D UINT64 RelaEntrySize;=0D @@ -255,7 +255,7 @@ RelocateElf64Dynamic ( //=0D // 2. Locate the relocation section from the dynamic section.=0D //=0D - RelaOffset =3D MAX_UINT64;=0D + RelaAddress =3D MAX_UINT64;=0D RelaSize =3D 0;=0D RelaCount =3D 0;=0D RelaEntrySize =3D 0;=0D @@ -274,8 +274,8 @@ RelocateElf64Dynamic ( // based on the original file value and the memory base address.=0D // For consistency, files do not contain relocation entries to ``c= orrect'' addresses in the dynamic structure.=0D //=0D - RelaOffset =3D Dyn->d_un.d_ptr - (UINTN) ElfCt->PreferredImageAddr= ess;=0D - RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL;=0D + RelaAddress =3D Dyn->d_un.d_ptr;=0D + RelaType =3D (Dyn->d_tag =3D=3D DT_RELA) ? SHT_RELA: SHT_REL;=0D break;=0D case DT_RELACOUNT:=0D case DT_RELCOUNT:=0D @@ -294,7 +294,7 @@ RelocateElf64Dynamic ( }=0D }=0D =0D - if (RelaOffset =3D=3D MAX_UINT64) {=0D + if (RelaAddress =3D=3D MAX_UINT64) {=0D ASSERT (RelaCount =3D=3D 0);=0D ASSERT (RelaEntrySize =3D=3D 0);=0D ASSERT (RelaSize =3D=3D 0);=0D @@ -307,8 +307,16 @@ RelocateElf64Dynamic ( //=0D // Verify the existence of the relocation section.=0D //=0D - RelShdr =3D GetElf64SectionByRange (ElfCt->FileBase, RelaOffset, RelaSiz= e);=0D - ASSERT (RelShdr !=3D NULL);=0D + RelShdr =3D NULL;=0D + for (Index =3D 0; Index < ElfCt->ShNum; Index++) {=0D + RelShdr =3D GetElf64SectionByIndex (ElfCt->FileBase, Index);=0D + ASSERT (RelShdr !=3D NULL);=0D + if ((RelShdr->sh_addr =3D=3D RelaAddress) && (RelShdr->sh_size =3D=3D = RelaSize)) {=0D + break;=0D + }=0D + RelShdr =3D NULL;=0D + }=0D +=0D if (RelShdr =3D=3D NULL) {=0D return EFI_UNSUPPORTED;=0D }=0D --=20 2.31.1.windows.1