summaryrefslogtreecommitdiff
path: root/src/ukify
diff options
context:
space:
mode:
authorDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-27 13:41:59 +0200
committerDaan De Meyer <daan.j.demeyer@gmail.com>2023-03-27 19:33:16 +0200
commit23428bb19e49cf510c65e2896f1a7e4b12ca1dbc (patch)
tree87ee456413a5311b87043d35e8471b7effdc46bf /src/ukify
parentffa859388b755c24c1c02d72e89637e49df337db (diff)
downloadsystemd-23428bb19e49cf510c65e2896f1a7e4b12ca1dbc.tar.gz
ukify: Weaken file alignment assertions
Older versions of the stub are not aligned to the PE file alignment size. If we remove the assertions, the UKI still boots without issues, so let's drop the assertions and print a message about it instead.
Diffstat (limited to 'src/ukify')
-rwxr-xr-xsrc/ukify/ukify.py8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py
index 2cdfc01ad4..99128014d9 100755
--- a/src/ukify/ukify.py
+++ b/src/ukify/ukify.py
@@ -449,7 +449,7 @@ class PeError(Exception):
def pe_add_sections(uki: UKI, output: str):
pe = pefile.PE(uki.executable, fast_load=True)
- assert len(pe.__data__) % pe.OPTIONAL_HEADER.FileAlignment == 0
+ assert pe.FILE_HEADER.PointerToSymbolTable != 0 or len(pe.__data__) % pe.OPTIONAL_HEADER.FileAlignment == 0
warnings = pe.get_warnings()
if warnings:
@@ -460,6 +460,12 @@ def pe_add_sections(uki: UKI, output: str):
# We could strip the signatures, but why would anyone sign the stub?
raise PeError(f'Stub image is signed, refusing.')
+ # If the executable has not been stripped, it might not be aligned to a multiple of the file alignment so
+ # let's make sure it is by padding it.
+ if pe.FILE_HEADER.PointerToSymbolTable != 0:
+ padlen = round_up(len(pe.__data__), pe.OPTIONAL_HEADER.FileAlignment) - len(pe.__data__)
+ pe.__data__ = pe.__data__[:] + padlen * b'\0'
+
for section in uki.sections:
new_section = pefile.SectionStructure(pe.__IMAGE_SECTION_HEADER_format__, pe=pe)
new_section.__unpack__(b'\0' * new_section.sizeof())