From 36687351648e68beb76495d323a771a9ca53ddf8 Mon Sep 17 00:00:00 2001 From: Mikhail Krichanov Date: Mon, 18 Sep 2023 11:23:39 +0300 Subject: [PATCH] BaseUeImageLib: Fixed buggy alignment in unchaining process. --- MdePkg/Library/BaseUeImageLib/UeImageLib.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/MdePkg/Library/BaseUeImageLib/UeImageLib.c b/MdePkg/Library/BaseUeImageLib/UeImageLib.c index 738221b58f..2ef011281b 100644 --- a/MdePkg/Library/BaseUeImageLib/UeImageLib.c +++ b/MdePkg/Library/BaseUeImageLib/UeImageLib.c @@ -614,6 +614,7 @@ UnchainReloc ( UINT32 OldSize; UINT16 FixupHdr; UINT32 FixupIndex; + UINT8 Addend; if ((RuntimeContext != NULL) && !IsRuntime) { if (MetaSource == NULL) { @@ -624,6 +625,22 @@ UnchainReloc ( *(UINT16 *)&RuntimeContext->UnchainedRelocs[FixupIndex] = FixupHdr; + Addend = ALIGN_VALUE_ADDEND(RuntimeContext->UnchainedRelocsSize, ALIGNOF(UE_FIXUP_ROOT)); + if ((RelocOffset == UE_HEAD_FIXUP_OFFSET_END) && (Addend != 0)) { + OldSize = RuntimeContext->UnchainedRelocsSize; + RuntimeContext->UnchainedRelocs = ReallocateRuntimePool ( + OldSize, + OldSize + Addend, + RuntimeContext->UnchainedRelocs + ); + if (RuntimeContext->UnchainedRelocs == NULL) { + return RETURN_OUT_OF_RESOURCES; + } + + ZeroMem (RuntimeContext->UnchainedRelocs + OldSize, Addend); + RuntimeContext->UnchainedRelocsSize += Addend; + } + return RETURN_SUCCESS; } @@ -742,7 +759,7 @@ InternalProcessRelocChain ( // Imitate the common header of UE chained relocation fixups, // as for 32-bit files all relocs have the same type. // - FixupInfo.Value32 = FixupInfo.Value32 << 4; + FixupInfo.Value32 = FixupInfo.Value32 << 4U; FixupInfo.Value32 |= UeReloc32; } else { // @@ -762,7 +779,7 @@ InternalProcessRelocChain ( RelocTarget += FixupSize; RelocOffset = UE_CHAINED_RELOC_FIXUP_NEXT_OFFSET (FixupInfo.Value32); - FixupHdr = (RelocOffset << 4) | RelocType; + FixupHdr = (RelocOffset << 4U) | RelocType; Status = UnchainReloc ( RuntimeContext,