public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: "Zhiguang Liu" <zhiguang.liu@intel.com>
To: devel@edk2.groups.io
Cc: Guo Dong <guo.dong@intel.com>, Ray Ni <ray.ni@intel.com>,
	Maurice Ma <maurice.ma@intel.com>,
	Benjamin You <benjamin.you@intel.com>,
	Sean Rhodes <sean@starlabs.systems>
Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case
Date: Wed,  6 Apr 2022 21:51:55 +0800	[thread overview]
Message-ID: <20220406135155.822-1-zhiguang.liu@intel.com> (raw)

Current implementation of the Elf loader reuses the same memory range if
the desired memory range is covered by [file base, file base + file size].
However, there is a potentil bug, for example:
If the first segment is located at 0x1000, and the desired address is
0x2000. While the second segment is located at 0x2000, and the desired
address is 0x2000. When we parse and try to load the elf file, current
implementation may load the first segment at 0x2000, and override second
segment.

Cc: Guo Dong <guo.dong@intel.com>
Cc: Ray Ni <ray.ni@intel.com>
Cc: Maurice Ma <maurice.ma@intel.com>
Cc: Benjamin You <benjamin.you@intel.com>
Cc: Sean Rhodes <sean@starlabs.systems>

Signed-off-by: Zhiguang Liu <zhiguang.liu@intel.com>
---
 UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
index 2a6305c67b..c7dfae14af 100644
--- a/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
+++ b/UefiPayloadPkg/PayloadLoaderPeim/ElfLib/ElfLib.c
@@ -312,6 +312,13 @@ ParseElfImage (
       ElfCt->ReloadRequired = TRUE;
     }
 
+    if (SegInfo.MemAddr != ((UINTN)ElfCt->FileBase + SegInfo.Offset)) {
+      //
+      // Need to relocate if the desired address is not the current address
+      //
+      ElfCt->ReloadRequired = TRUE;
+    }
+
     if (Base > (SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1))) {
       Base = SegInfo.MemAddr & ~(EFI_PAGE_SIZE - 1);
     }
-- 
2.32.0.windows.2


             reply	other threads:[~2022-04-06 13:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-06 13:51 Zhiguang Liu [this message]
2022-04-07  0:06 ` [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case Ni, Ray
2022-04-07  0:57   ` Zhiguang Liu
2022-04-11  2:05     ` Ni, Ray
2022-04-10 21:01 ` Guo Dong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220406135155.822-1-zhiguang.liu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox