From ab7d54b9dd2945730410d7bd8ef9d0c659d4574c Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 23 May 2023 19:00:52 +0200 Subject: [PATCH] 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. --- tools/elf2efi.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) 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)