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

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