[PATCH 1/2] UefiPayloadPkg/PayloadLoader: Fix bug in locating relocation section


Ni, Ray
 

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 <ray.ni@...>
Cc: Maurice Ma <maurice.ma@...>
Cc: Guo Dong <guo.dong@...>
Cc: Benjamin You <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

Join devel@edk2.groups.io to automatically receive all group messages.