diff --git a/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c b/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c index cffb1410db..0209c0d847 100644 --- a/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c +++ b/MdePkg/Library/BasePeCoffLib2/PeCoffInit.c @@ -840,6 +840,13 @@ PeCoffInitializeContext ( } Context->ExeHdrOffset = DosHdr->e_lfanew; + // + // Verify the Execution Header offset is sufficiently aligned. + // + if (!PcdGetBool (PcdImageLoaderAllowMisalignedOffset) + && !IS_ALIGNED (Context->ExeHdrOffset, ALIGNOF (EFI_IMAGE_NT_HEADERS_COMMON_HDR))) { + return RETURN_UNSUPPORTED; + } } else if (!PcdGetBool (PcdImageLoaderProhibitTe)) { // // Assume the Image starts with the Executable Header, determine whether it @@ -865,13 +872,6 @@ PeCoffInitializeContext ( if (FileSize - Context->ExeHdrOffset < sizeof (EFI_IMAGE_NT_HEADERS_COMMON_HDR) + sizeof (UINT16)) { return RETURN_UNSUPPORTED; } - // - // Verify the Execution Header offset is sufficiently aligned. - // - if (!PcdGetBool (PcdImageLoaderAllowMisalignedOffset) - && !IS_ALIGNED (Context->ExeHdrOffset, ALIGNOF (EFI_IMAGE_NT_HEADERS_COMMON_HDR))) { - return RETURN_UNSUPPORTED; - } STATIC_ASSERT ( ALIGNOF (UINT32) <= ALIGNOF (EFI_IMAGE_NT_HEADERS_COMMON_HDR),