]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
elf2efi: Do not emit an empty relocation section
authorJan Janssen <medhefgo@web.de>
Tue, 23 May 2023 17:00:52 +0000 (19:00 +0200)
committerLuca Boccassi <luca.boccassi@gmail.com>
Tue, 23 May 2023 21:47:28 +0000 (22:47 +0100)
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

index 2ca9d248e7da975802c1454ed5a4a28cad10193a..6179ba821327f13a4db33ff7cf5a963b66d58b9e 100755 (executable)
@@ -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)