]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
elf2efi: Check ELF image base if possible
authorJan Janssen <medhefgo@web.de>
Thu, 28 Sep 2023 14:22:13 +0000 (16:22 +0200)
committerJan Janssen <medhefgo@web.de>
Fri, 29 Sep 2023 14:56:30 +0000 (16:56 +0200)
tools/elf2efi.py

index f1fbf1e630f5f8d6e054112ffe4887abeb715019..8dbf881a0c522f42c58fe6729d2c962d081bc2e4 100755 (executable)
@@ -348,7 +348,6 @@ def apply_elf_relative_relocation(
         addend = target.data[addend_offset : addend_offset + addend_size]
         addend = int.from_bytes(addend, byteorder="little")
 
-    # This currently assumes that the ELF file has an image base of 0.
     value = (image_base + addend).to_bytes(addend_size, byteorder="little")
     target.data[addend_offset : addend_offset + addend_size] = value
 
@@ -417,6 +416,13 @@ def convert_elf_relocations(
     if not flags_tag["d_val"] & ENUM_DT_FLAGS_1["DF_1_PIE"]:
         raise RuntimeError("ELF file is not a PIE.")
 
+    # This checks that the ELF image base is 0.
+    symtab = elf.get_section_by_name(".symtab")
+    if symtab:
+        exe_start = symtab.get_symbol_by_name("__executable_start")
+        if exe_start and exe_start[0]["st_value"] != 0:
+            raise RuntimeError("Unexpected ELF image base.")
+
     opt.SizeOfHeaders = align_to(
         PE_OFFSET
         + len(PE_MAGIC)