* [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case @ 2022-04-06 13:51 Zhiguang Liu 2022-04-07 0:06 ` Ni, Ray 2022-04-10 21:01 ` Guo Dong 0 siblings, 2 replies; 5+ messages in thread From: Zhiguang Liu @ 2022-04-06 13:51 UTC (permalink / raw) To: devel; +Cc: Guo Dong, Ray Ni, Maurice Ma, Benjamin You, Sean Rhodes 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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case 2022-04-06 13:51 [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case Zhiguang Liu @ 2022-04-07 0:06 ` Ni, Ray 2022-04-07 0:57 ` Zhiguang Liu 2022-04-10 21:01 ` Guo Dong 1 sibling, 1 reply; 5+ messages in thread From: Ni, Ray @ 2022-04-07 0:06 UTC (permalink / raw) To: Liu, Zhiguang, devel@edk2.groups.io Cc: Dong, Guo, Ma, Maurice, You, Benjamin, Rhodes, Sean > -----Original Message----- > From: Liu, Zhiguang <zhiguang.liu@intel.com> > Sent: Wednesday, April 6, 2022 9:52 PM > To: devel@edk2.groups.io > Cc: Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin > <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems> > Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case > > 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 Why the desired address for both segments is 0x2000? ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case 2022-04-07 0:06 ` Ni, Ray @ 2022-04-07 0:57 ` Zhiguang Liu 2022-04-11 2:05 ` Ni, Ray 0 siblings, 1 reply; 5+ messages in thread From: Zhiguang Liu @ 2022-04-07 0:57 UTC (permalink / raw) To: Ni, Ray, devel@edk2.groups.io Cc: Dong, Guo, Ma, Maurice, You, Benjamin, Rhodes, Sean Sorry, it is a typo. the example should be The first segment is located at 0x1000, and the desired address is 0x2000. The second segment is located at 0x2000, and the desired address is 0x1000. The current ELF loader may copy first segment to 0x2000, which will erase the second segment. Thanks Zhiguang > -----Original Message----- > From: Ni, Ray <ray.ni@intel.com> > Sent: Thursday, April 7, 2022 8:06 AM > To: Liu, Zhiguang <zhiguang.liu@intel.com>; devel@edk2.groups.io > Cc: Dong, Guo <guo.dong@intel.com>; Ma, Maurice <maurice.ma@intel.com>; > You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean > <sean@starlabs.systems> > Subject: RE: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in > some case > > > > > -----Original Message----- > > From: Liu, Zhiguang <zhiguang.liu@intel.com> > > Sent: Wednesday, April 6, 2022 9:52 PM > > To: devel@edk2.groups.io > > Cc: Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, > > Maurice <maurice.ma@intel.com>; You, Benjamin > > <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems> > > Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't > > relocate in some case > > > > 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 > > Why the desired address for both segments is 0x2000? ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case 2022-04-07 0:57 ` Zhiguang Liu @ 2022-04-11 2:05 ` Ni, Ray 0 siblings, 0 replies; 5+ messages in thread From: Ni, Ray @ 2022-04-11 2:05 UTC (permalink / raw) To: Liu, Zhiguang, devel@edk2.groups.io Cc: Dong, Guo, Ma, Maurice, You, Benjamin, Rhodes, Sean With the updated commit message, Reviewed-by: Ray Ni <ray.ni@Intel.com> > -----Original Message----- > From: Liu, Zhiguang <zhiguang.liu@intel.com> > Sent: Thursday, April 7, 2022 8:57 AM > To: Ni, Ray <ray.ni@intel.com>; devel@edk2.groups.io > Cc: Dong, Guo <guo.dong@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; > Rhodes, Sean <sean@starlabs.systems> > Subject: RE: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case > > Sorry, it is a typo. the example should be > The first segment is located at 0x1000, and the desired address is 0x2000. > The second segment is located at 0x2000, and the desired address is 0x1000. > The current ELF loader may copy first segment to 0x2000, which will erase the second segment. > > Thanks > Zhiguang > > > > -----Original Message----- > > From: Ni, Ray <ray.ni@intel.com> > > Sent: Thursday, April 7, 2022 8:06 AM > > To: Liu, Zhiguang <zhiguang.liu@intel.com>; devel@edk2.groups.io > > Cc: Dong, Guo <guo.dong@intel.com>; Ma, Maurice <maurice.ma@intel.com>; > > You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean > > <sean@starlabs.systems> > > Subject: RE: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in > > some case > > > > > > > > > -----Original Message----- > > > From: Liu, Zhiguang <zhiguang.liu@intel.com> > > > Sent: Wednesday, April 6, 2022 9:52 PM > > > To: devel@edk2.groups.io > > > Cc: Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, > > > Maurice <maurice.ma@intel.com>; You, Benjamin > > > <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems> > > > Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't > > > relocate in some case > > > > > > 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 > > > > Why the desired address for both segments is 0x2000? ^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case 2022-04-06 13:51 [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case Zhiguang Liu 2022-04-07 0:06 ` Ni, Ray @ 2022-04-10 21:01 ` Guo Dong 1 sibling, 0 replies; 5+ messages in thread From: Guo Dong @ 2022-04-10 21:01 UTC (permalink / raw) To: Liu, Zhiguang, devel@edk2.groups.io Cc: Ni, Ray, Ma, Maurice, You, Benjamin, Rhodes, Sean Reviewed-by: Guo Dong <guo.dong@intel.com> -----Original Message----- From: Liu, Zhiguang <zhiguang.liu@intel.com> Sent: Wednesday, April 6, 2022 6:52 AM To: devel@edk2.groups.io Cc: Dong, Guo <guo.dong@intel.com>; Ni, Ray <ray.ni@intel.com>; Ma, Maurice <maurice.ma@intel.com>; You, Benjamin <benjamin.you@intel.com>; Rhodes, Sean <sean@starlabs.systems> Subject: [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case 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 ^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2022-04-11 2:05 UTC | newest] Thread overview: 5+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2022-04-06 13:51 [PATCH] UefiPayloadPkg: Fix bug that Elf loader doesn't relocate in some case Zhiguang Liu 2022-04-07 0:06 ` Ni, Ray 2022-04-07 0:57 ` Zhiguang Liu 2022-04-11 2:05 ` Ni, Ray 2022-04-10 21:01 ` Guo Dong
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox