]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
ukify: Fix section offset calculation
authorJan Janssen <medhefgo@web.de>
Sun, 1 Jan 2023 10:32:55 +0000 (11:32 +0100)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 2 Jan 2023 14:59:17 +0000 (15:59 +0100)
objcopy seems to expect that the offset passed to --change-section-vma
is absolute instead of relative to ImageBase. If this is not accounted
for an invalid image is created that cannot be loaded:

  0 .osrel        0000016b  0000000200016000  0000000200016000  00000400  2**2
  …
  6 .text         0000d242  0000000140001000  0000000140001000  00c6e800  2**4

This isn't an issue with gnu-efi based PE images, but natively created
ones will have a non-zero ImageBase.

src/ukify/ukify.py

index ad2f8a2c718417370e1ac285e890c48fcee4a0db..d22772e40312f0bb70581d10f0c4ca381cdf62ac 100755 (executable)
@@ -73,12 +73,12 @@ def path_is_readable(s: str | None) -> pathlib.Path | None:
     return p
 
 
-def pe_executable_size(filename):
+def pe_next_section_offset(filename):
     import pefile
 
     pe = pefile.PE(filename)
     section = pe.sections[-1]
-    return section.VirtualAddress + section.Misc_VirtualSize
+    return pe.OPTIONAL_HEADER.ImageBase + section.VirtualAddress + section.Misc_VirtualSize
 
 
 def round_up(x, blocksize=4096):
@@ -262,7 +262,7 @@ class UKI:
     offset: int | None = dataclasses.field(default=None, init=False)
 
     def __post_init__(self):
-        self.offset = round_up(pe_executable_size(self.executable))
+        self.offset = round_up(pe_next_section_offset(self.executable))
 
     def add_section(self, section):
         assert self.offset