[edk2-devel] [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case

Zhiguang Liu zhiguang.liu at intel.com
Wed Apr 6 13:51:55 UTC 2022


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 at intel.com>
Cc: Ray Ni <ray.ni at intel.com>
Cc: Maurice Ma <maurice.ma at intel.com>
Cc: Benjamin You <benjamin.you at intel.com>
Cc: Sean Rhodes <sean at starlabs.systems>

Signed-off-by: Zhiguang Liu <zhiguang.liu at 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



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#88456): https://edk2.groups.io/g/devel/message/88456
Mute This Topic: https://groups.io/mt/90289885/1813853
Group Owner: devel+owner at edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [edk2-devel-archive at redhat.com]
-=-=-=-=-=-=-=-=-=-=-=-




More information about the edk2-devel-archive mailing list