From: Daan De Meyer Date: Wed, 29 Jan 2025 13:38:34 +0000 (+0100) Subject: Calculate PE section size correctly X-Git-Tag: v25.3~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=962a2b9f80ae0be6a86ae23b959f6f56ecdab33f;p=thirdparty%2Fmkosi.git Calculate PE section size correctly --- diff --git a/mkosi/bootloader.py b/mkosi/bootloader.py index 87a56cc03..b1b3d7949 100644 --- a/mkosi/bootloader.py +++ b/mkosi/bootloader.py @@ -277,6 +277,10 @@ def extract_pe_section(context: Context, binary: Path, section: str, output: Pat # TODO: Use ignore_padding=True instead of length once we can depend on a newer pefile. # TODO: Drop KeyError logic once we drop support for Ubuntu Jammy and sdmagic will always be available. + # Misc_VirtualSize is the section size in memory, which can be bigger or smaller than SizeOfRawData, + # which is the aligned section size on disk. The closest approximation of the actual section size will be + # the minimum of these two. If Misc_VirtualSize < SizeOfRawData, we'll get the actual size. Otherwise + # padding might be inclduded. pefile = textwrap.dedent( f"""\ import pefile @@ -286,7 +290,9 @@ def extract_pe_section(context: Context, binary: Path, section: str, output: Pat section = {{s.Name.decode().strip("\\0"): s for s in pe.sections}}.get("{section}") if not section: sys.exit(67) - sys.stdout.buffer.write(section.get_data(length=section.Misc_VirtualSize)) + sys.stdout.buffer.write( + section.get_data(length=min(section.Misc_VirtualSize, section.SizeOfRawData)) + ) """ )