Blame SOURCES/0470-util-mkimage-Unify-more-of-the-PE32-and-PE32-header-.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Peter Jones <pjones@redhat.com>
80913e
Date: Fri, 19 Feb 2021 14:22:13 +0100
80913e
Subject: [PATCH] util/mkimage: Unify more of the PE32 and PE32+ header set-up
80913e
80913e
There's quite a bit of code duplication in the code that sets the optional
80913e
header for PE32 and PE32+. The two are very similar with the exception of
80913e
a few fields that have type grub_uint64_t instead of grub_uint32_t.
80913e
80913e
Factor out the common code and add a PE_OHDR() macro that simplifies the
80913e
set-up and make the code more readable.
80913e
80913e
Signed-off-by: Peter Jones <pjones@redhat.com>
80913e
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
---
80913e
 util/mkimage.c | 111 ++++++++++++++++++++++++++-------------------------------
80913e
 1 file changed, 51 insertions(+), 60 deletions(-)
80913e
80913e
diff --git a/util/mkimage.c b/util/mkimage.c
b32e65
index d2876cdb5..ff5462c98 100644
80913e
--- a/util/mkimage.c
80913e
+++ b/util/mkimage.c
80913e
@@ -771,6 +771,21 @@ grub_install_get_image_targets_string (void)
80913e
   return formats;
80913e
 }
80913e
 
80913e
+/*
80913e
+ * tmp_ is just here so the compiler knows we'll never derefernce a NULL.
80913e
+ * It should get fully optimized away.
80913e
+ */
80913e
+#define PE_OHDR(o32, o64, field) (*(		\
80913e
+{						\
80913e
+  __typeof__((o64)->field) tmp_;		\
80913e
+  __typeof__((o64)->field) *ret_ = &tmp_;	\
80913e
+  if (o32)					\
80913e
+    ret_ = (void *)(&((o32)->field));		\
80913e
+  else if (o64)				\
80913e
+    ret_ = (void *)(&((o64)->field));		\
80913e
+  ret_;					\
80913e
+}))
80913e
+
80913e
 void
80913e
 grub_install_generate_image (const char *dir, const char *prefix,
80913e
 			     FILE *out, const char *outname, char *mods[],
80913e
@@ -1240,6 +1255,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 	static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
80913e
 	int header_size;
80913e
 	int reloc_addr;
80913e
+	struct grub_pe32_optional_header *o32 = NULL;
80913e
+	struct grub_pe64_optional_header *o64 = NULL;
80913e
 
80913e
 	if (image_target->voidp_sizeof == 4)
80913e
 	  header_size = EFI32_HEADER_SIZE;
80913e
@@ -1281,76 +1298,50 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 	/* The PE Optional header.  */
80913e
 	if (image_target->voidp_sizeof == 4)
80913e
 	  {
80913e
-	    struct grub_pe32_optional_header *o;
80913e
-
80913e
 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe32_optional_header));
80913e
 
80913e
-	    o = (struct grub_pe32_optional_header *)
80913e
-	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE
80913e
-	       + sizeof (struct grub_pe32_coff_header));
80913e
-	    o->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
80913e
-	    o->code_size = grub_host_to_target32 (layout.exec_size);
80913e
-	    o->data_size = grub_host_to_target32 (reloc_addr - layout.exec_size
80913e
-					     - header_size);
80913e
-	    o->entry_addr = grub_host_to_target32 (layout.start_address);
80913e
-	    o->code_base = grub_host_to_target32 (header_size);
80913e
+	    o32 = (struct grub_pe32_optional_header *)
80913e
+	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
80913e
+	       sizeof (struct grub_pe32_coff_header));
80913e
+	    o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
80913e
+	    o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
80913e
 
80913e
-	    o->data_base = grub_host_to_target32 (header_size + layout.exec_size);
80913e
-
80913e
-	    o->image_base = 0;
80913e
-	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
80913e
-	    o->file_alignment = grub_host_to_target32 (image_target->section_align);
80913e
-	    o->image_size = grub_host_to_target32 (pe_size);
80913e
-	    o->header_size = grub_host_to_target32 (header_size);
80913e
-	    o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
80913e
-
80913e
-	    /* Do these really matter? */
80913e
-	    o->stack_reserve_size = grub_host_to_target32 (0x10000);
80913e
-	    o->stack_commit_size = grub_host_to_target32 (0x10000);
80913e
-	    o->heap_reserve_size = grub_host_to_target32 (0x10000);
80913e
-	    o->heap_commit_size = grub_host_to_target32 (0x10000);
80913e
-    
80913e
-	    o->num_data_directories = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
80913e
-
80913e
-	    o->base_relocation_table.rva = grub_host_to_target32 (reloc_addr);
80913e
-	    o->base_relocation_table.size = grub_host_to_target32 (layout.reloc_size);
80913e
-	    sections = o + 1;
80913e
+	    sections = o32 + 1;
80913e
 	  }
80913e
 	else
80913e
 	  {
80913e
-	    struct grub_pe64_optional_header *o;
80913e
-
80913e
 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
80913e
 
80913e
-	    o = (struct grub_pe64_optional_header *) 
80913e
-	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE
80913e
-	       + sizeof (struct grub_pe32_coff_header));
80913e
-	    o->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
80913e
-	    o->code_size = grub_host_to_target32 (layout.exec_size);
80913e
-	    o->data_size = grub_host_to_target32 (reloc_addr - layout.exec_size
80913e
-					     - header_size);
80913e
-	    o->entry_addr = grub_host_to_target32 (layout.start_address);
80913e
-	    o->code_base = grub_host_to_target32 (header_size);
80913e
-	    o->image_base = 0;
80913e
-	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
80913e
-	    o->file_alignment = grub_host_to_target32 (image_target->section_align);
80913e
-	    o->image_size = grub_host_to_target32 (pe_size);
80913e
-	    o->header_size = grub_host_to_target32 (header_size);
80913e
-	    o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
80913e
+	    o64 = (struct grub_pe64_optional_header *)
80913e
+	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
80913e
+	       sizeof (struct grub_pe32_coff_header));
80913e
+	    o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
80913e
 
80913e
-	    /* Do these really matter? */
80913e
-	    o->stack_reserve_size = grub_host_to_target32 (0x10000);
80913e
-	    o->stack_commit_size = grub_host_to_target32 (0x10000);
80913e
-	    o->heap_reserve_size = grub_host_to_target32 (0x10000);
80913e
-	    o->heap_commit_size = grub_host_to_target32 (0x10000);
80913e
-    
80913e
-	    o->num_data_directories
80913e
-	      = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
80913e
-
80913e
-	    o->base_relocation_table.rva = grub_host_to_target32 (reloc_addr);
80913e
-	    o->base_relocation_table.size = grub_host_to_target32 (layout.reloc_size);
80913e
-	    sections = o + 1;
80913e
+	    sections = o64 + 1;
80913e
 	  }
80913e
+
80913e
+	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
80913e
+	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - layout.exec_size - header_size);
80913e
+	PE_OHDR (o32, o64, entry_addr) = grub_host_to_target32 (layout.start_address);
80913e
+	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
80913e
+
80913e
+	PE_OHDR (o32, o64, image_base) = 0;
80913e
+	PE_OHDR (o32, o64, section_alignment) = grub_host_to_target32 (image_target->section_align);
80913e
+	PE_OHDR (o32, o64, file_alignment) = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
80913e
+	PE_OHDR (o32, o64, image_size) = grub_host_to_target32 (pe_size);
80913e
+	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
80913e
+	PE_OHDR (o32, o64, subsystem) = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
80913e
+
80913e
+	/* Do these really matter? */
80913e
+	PE_OHDR (o32, o64, stack_reserve_size) = grub_host_to_target32 (0x10000);
80913e
+	PE_OHDR (o32, o64, stack_commit_size) = grub_host_to_target32 (0x10000);
80913e
+	PE_OHDR (o32, o64, heap_reserve_size) = grub_host_to_target32 (0x10000);
80913e
+	PE_OHDR (o32, o64, heap_commit_size) = grub_host_to_target32 (0x10000);
80913e
+
80913e
+	PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
80913e
+	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
80913e
+	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
80913e
+
80913e
 	/* The sections.  */
80913e
 	text_section = sections;
80913e
 	strcpy (text_section->name, ".text");