]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: Use SizeOfImage from linux image as virtual size of .linux section
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 30 Sep 2024 11:42:50 +0000 (13:42 +0200)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Mon, 30 Sep 2024 13:52:59 +0000 (15:52 +0200)
The SizeOfImage is bigger than the image itself so that space is
guaranteed to be available for in place execution of the linux image. Let's
make sure we take this into account and use SizeOfImage as the section's virtual
size instead of the size of the image itself.

Fixes #34578

src/ukify/ukify.py

index fc391995c42a112cd6022c9b358ee0057e69c68a..86117cd56318bd435b637a07ddb596e85c0710ae 100755 (executable)
@@ -323,6 +323,7 @@ class Section:
     tmpfile: Optional[IO] = None
     measure: bool = False
     output_mode: Optional[str] = None
+    virtual_size: Optional[int] = None
 
     @classmethod
     def create(cls, name, contents, **kwargs):
@@ -709,7 +710,10 @@ def pe_add_sections(uki: UKI, output: str):
 
         new_section.set_file_offset(offset)
         new_section.Name = section.name.encode()
-        new_section.Misc_VirtualSize = len(data)
+        if section.virtual_size is not None:
+            new_section.Misc_VirtualSize = section.virtual_size
+        else:
+            new_section.Misc_VirtualSize = len(data)
         # Non-stripped stubs might still have an unaligned symbol table at the end, making their size
         # unaligned, so we make sure to explicitly pad the pointer to new sections to an aligned offset.
         new_section.PointerToRawData = round_up(len(pe.__data__), pe.OPTIONAL_HEADER.FileAlignment)
@@ -988,7 +992,13 @@ uki-addon,1,UKI Addon,addon,1,https://www.freedesktop.org/software/systemd/man/l
     # UKI creation
 
     if linux is not None:
-        uki.add_section(Section.create('.linux', linux, measure=True))
+        try:
+            virtual_size = pefile.PE(linux, fast_load=True).OPTIONAL_HEADER.SizeOfImage
+        except pefile.PEFormatError:
+            print(f"{f} is not a valid PE file, not using SizeOfImage.")
+            virtual_size = None
+
+        uki.add_section(Section.create('.linux', linux, measure=True, virtual_size=virtual_size))
 
     if sign_args_present:
         unsigned = tempfile.NamedTemporaryFile(prefix='uki')