From: Jan Janssen Date: Tue, 23 May 2023 17:00:52 +0000 (+0200) Subject: elf2efi: Do not emit an empty relocation section X-Git-Tag: v254-rc1~401 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ab7d54b9dd2945730410d7bd8ef9d0c659d4574c;p=thirdparty%2Fsystemd.git elf2efi: Do not emit an empty relocation section At least shim will choke on an empty relocation section when loading the binary. Note that the binary is still considered relocatable (just with no base relocations to apply) as we do not set the IMAGE_FILE_RELOCS_STRIPPED DLL characteristic. --- diff --git a/tools/elf2efi.py b/tools/elf2efi.py index 2ca9d248e7d..6179ba82132 100755 --- a/tools/elf2efi.py +++ b/tools/elf2efi.py @@ -27,6 +27,7 @@ import io import os import pathlib import time +import typing from ctypes import ( c_char, c_uint8, @@ -377,7 +378,7 @@ def convert_elf_reloc_table( def convert_elf_relocations( elf: ELFFile, opt: PeOptionalHeader, sections: list[PeSection] -) -> PeSection: +) -> typing.Optional[PeSection]: dynamic = elf.get_section_by_name(".dynamic") if dynamic is None: raise RuntimeError("ELF .dynamic section is missing.") @@ -394,6 +395,9 @@ def convert_elf_relocations( elf, reloc_table, opt.ImageBase, sections, pe_reloc_blocks ) + if len(pe_reloc_blocks) == 0: + return None + data = bytearray() for rva in sorted(pe_reloc_blocks): block = pe_reloc_blocks[rva] @@ -524,9 +528,10 @@ def elf2efi(args: argparse.Namespace): opt.SizeOfHeapCommit = 0x001000 opt.NumberOfRvaAndSizes = N_DATA_DIRECTORY_ENTRIES - opt.BaseRelocationTable = PeDataDirectory( - pe_reloc_s.VirtualAddress, pe_reloc_s.VirtualSize - ) + if pe_reloc_s: + opt.BaseRelocationTable = PeDataDirectory( + pe_reloc_s.VirtualAddress, pe_reloc_s.VirtualSize + ) write_pe(args.PE, coff, opt, sections)