Skip to content

Commit

Permalink
efi: check if all sections of our EFI binaries are properly aligned
Browse files Browse the repository at this point in the history
(cherry picked from commit 7ff3b88)

Related: RHEL-30372
  • Loading branch information
mrc0mmand committed Apr 18, 2024
1 parent e100e38 commit 9bd51ec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1913,6 +1913,7 @@ catalogs = []

############################################################

check_efi_alignment_py = find_program('tools/check-efi-alignment.py')
# Include these now as they provide gnu-efi detection.
subdir('src/fundamental')
subdir('src/boot/efi')
Expand Down
5 changes: 5 additions & 0 deletions src/boot/efi/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -494,4 +494,9 @@ foreach tuple : [['systemd-boot@0@.@1@', systemd_boot_objects, false, 'systemd-b
install_dir : bootlibdir)

alias_target(tuple[3], efi)

test('check-alignment-@0@'.format(tuple[0].format(efi_arch[0], 'efi')),
check_efi_alignment_py,
args : efi.full_path(),
suite : 'efi')
endforeach
32 changes: 32 additions & 0 deletions tools/check-efi-alignment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/python3
# SPDX-License-Identifier: LGPL-2.1-or-later
# vi: set tw=110 sw=4 ts=4 et:

import sys

import pefile


def main():
pe = pefile.PE(sys.argv[1], fast_load=True)

for section in pe.sections:
name = section.Name.rstrip(b"\x00").decode()
file_addr = section.PointerToRawData
virt_addr = section.VirtualAddress
print(f"{name:10s} file=0x{file_addr:08x} virt=0x{virt_addr:08x}")

if file_addr % 512 != 0:
print(f"File address of {name} section is not aligned to 512 bytes", file=sys.stderr)
return 1

if virt_addr % 512 != 0:
print(f"Virt address of {name} section is not aligned to 512 bytes", file=sys.stderr)
return 1

if __name__ == '__main__':
if len(sys.argv) != 2:
print(f"Usage: {sys.argv[0]} pe-image")
sys.exit(1)

sys.exit(main())

0 comments on commit 9bd51ec

Please sign in to comment.