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

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