From: Jan Janssen Date: Fri, 22 Sep 2023 10:13:09 +0000 (+0200) Subject: elf2efi: Add next_section_address helper X-Git-Tag: v255-rc1~379^2~4 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7d6fd7f0993c0f3d05da2b6b8aefca38a3bd7b60;p=thirdparty%2Fsystemd.git elf2efi: Add next_section_address helper --- diff --git a/tools/elf2efi.py b/tools/elf2efi.py index 8dbf881a0c5..ce5b04e9945 100755 --- a/tools/elf2efi.py +++ b/tools/elf2efi.py @@ -245,6 +245,12 @@ def align_down(x: int, align: int) -> int: return x & ~(align - 1) +def next_section_address(sections: typing.List[PeSection]) -> int: + return align_to( + sections[-1].VirtualAddress + sections[-1].VirtualSize, SECTION_ALIGNMENT + ) + + def iter_copy_sections(elf: ELFFile) -> typing.Iterator[PeSection]: pe_s = None @@ -484,11 +490,9 @@ def convert_elf_relocations( pe_reloc_s = PeSection() pe_reloc_s.Name = b".reloc" pe_reloc_s.data = data + pe_reloc_s.VirtualAddress = next_section_address(sections) pe_reloc_s.VirtualSize = len(data) pe_reloc_s.SizeOfRawData = align_to(len(data), FILE_ALIGNMENT) - pe_reloc_s.VirtualAddress = align_to( - sections[-1].VirtualAddress + sections[-1].VirtualSize, SECTION_ALIGNMENT - ) # CNT_INITIALIZED_DATA|MEM_READ|MEM_DISCARDABLE pe_reloc_s.Characteristics = 0x42000040 @@ -575,9 +579,8 @@ def elf2efi(args: argparse.Namespace): opt.MinorSubsystemVersion = args.efi_minor opt.Subsystem = args.subsystem opt.Magic = 0x10B if elf.elfclass == 32 else 0x20B - opt.SizeOfImage = align_to( - sections[-1].VirtualAddress + sections[-1].VirtualSize, SECTION_ALIGNMENT - ) + opt.SizeOfImage = next_section_address(sections) + # DYNAMIC_BASE|NX_COMPAT|HIGH_ENTROPY_VA or DYNAMIC_BASE|NX_COMPAT opt.DllCharacteristics = 0x160 if elf.elfclass == 64 else 0x140