Blame SOURCES/0473-util-mkimage-Refactor-section-setup-to-use-a-helper.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Peter Jones <pjones@redhat.com>
80913e
Date: Mon, 15 Feb 2021 14:58:06 +0100
80913e
Subject: [PATCH] util/mkimage: Refactor section setup to use a helper
80913e
80913e
Add a init_pe_section() helper function to setup PE sections. This makes
80913e
the code simpler and easier to read.
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 | 141 +++++++++++++++++++++++++++++++--------------------------
80913e
 1 file changed, 76 insertions(+), 65 deletions(-)
80913e
80913e
diff --git a/util/mkimage.c b/util/mkimage.c
b32e65
index f22b398d9..0f5ae2a76 100644
80913e
--- a/util/mkimage.c
80913e
+++ b/util/mkimage.c
80913e
@@ -771,6 +771,38 @@ grub_install_get_image_targets_string (void)
80913e
   return formats;
80913e
 }
80913e
 
80913e
+/*
80913e
+ * The image_target parameter is used by the grub_host_to_target32() macro.
80913e
+ */
80913e
+static struct grub_pe32_section_table *
80913e
+init_pe_section(const struct grub_install_image_target_desc *image_target,
80913e
+		struct grub_pe32_section_table *section,
80913e
+		const char * const name,
80913e
+		grub_uint32_t *vma, grub_uint32_t vsz, grub_uint32_t valign,
80913e
+		grub_uint32_t *rda, grub_uint32_t rsz,
80913e
+		grub_uint32_t characteristics)
80913e
+{
80913e
+  size_t len = strlen (name);
80913e
+
80913e
+  if (len > sizeof (section->name))
80913e
+    grub_util_error (_("section name %s length is bigger than %lu"),
80913e
+                    name, (unsigned long) sizeof (section->name));
80913e
+
80913e
+  memcpy (section->name, name, len);
80913e
+
80913e
+  section->virtual_address = grub_host_to_target32 (*vma);
80913e
+  section->virtual_size = grub_host_to_target32 (vsz);
80913e
+  (*vma) = ALIGN_UP (*vma + vsz, valign);
80913e
+
80913e
+  section->raw_data_offset = grub_host_to_target32 (*rda);
80913e
+  section->raw_data_size = grub_host_to_target32 (rsz);
80913e
+  (*rda) = ALIGN_UP (*rda + rsz, GRUB_PE32_FILE_ALIGNMENT);
80913e
+
80913e
+  section->characteristics = grub_host_to_target32 (characteristics);
80913e
+
80913e
+  return section + 1;
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
@@ -1245,17 +1277,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
       break;
80913e
     case IMAGE_EFI:
80913e
       {
80913e
-	void *pe_img;
80913e
-	grub_uint8_t *header;
80913e
-	void *sections;
80913e
+	char *pe_img, *header;
80913e
+	struct grub_pe32_section_table *section;
80913e
 	size_t scn_size;
80913e
-	size_t pe_size;
80913e
+	grub_uint32_t vma, raw_data;
80913e
+	size_t pe_size, header_size;
80913e
 	struct grub_pe32_coff_header *c;
80913e
-	struct grub_pe32_section_table *text_section, *data_section;
80913e
-	struct grub_pe32_section_table *mods_section, *reloc_section;
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
@@ -1264,17 +1292,12 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 	else
80913e
 	  header_size = EFI64_HEADER_SIZE;
80913e
 
80913e
-	reloc_addr = ALIGN_UP (header_size + core_size,
80913e
-			       image_target->section_align);
80913e
+	vma = raw_data = header_size;
80913e
+	pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
80913e
+          ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT);
80913e
+	header = pe_img = xcalloc (1, pe_size);
80913e
 
80913e
-	pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
80913e
-			    image_target->section_align);
80913e
-	pe_img = xmalloc (reloc_addr + layout.reloc_size);
80913e
-	memset (pe_img, 0, header_size);
80913e
-	memcpy ((char *) pe_img + header_size, core_img, core_size);
80913e
-	memset ((char *) pe_img + header_size + core_size, 0, reloc_addr - (header_size + core_size));
80913e
-	memcpy ((char *) pe_img + reloc_addr, layout.reloc_section, layout.reloc_size);
80913e
-	header = pe_img;
80913e
+	memcpy (pe_img + raw_data, core_img, core_size);
80913e
 
80913e
 	/* The magic.  */
80913e
 	memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
80913e
@@ -1307,18 +1330,17 @@ grub_install_generate_image (const char *dir, const char *prefix,
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
-	    sections = o32 + 1;
80913e
+	    section = (struct grub_pe32_section_table *)(o32 + 1);
80913e
 	  }
80913e
 	else
80913e
 	  {
80913e
 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
80913e
-
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
-	    sections = o64 + 1;
80913e
+	    section = (struct grub_pe32_section_table *)(o64 + 1);
80913e
 	  }
80913e
 
80913e
 	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
80913e
@@ -1338,58 +1360,47 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 	PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
80913e
 
80913e
 	/* The sections.  */
80913e
-	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
80913e
+	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
80913e
 	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
80913e
-	text_section = sections;
80913e
-	strcpy (text_section->name, ".text");
80913e
-	text_section->virtual_size = grub_host_to_target32 (layout.exec_size);
80913e
-	text_section->virtual_address = grub_host_to_target32 (header_size);
80913e
-	text_section->raw_data_size = grub_host_to_target32 (layout.exec_size);
80913e
-	text_section->raw_data_offset = grub_host_to_target32 (header_size);
80913e
-	text_section->characteristics = grub_cpu_to_le32_compile_time (
80913e
-						  GRUB_PE32_SCN_CNT_CODE
80913e
-						| GRUB_PE32_SCN_MEM_EXECUTE
80913e
-						| GRUB_PE32_SCN_MEM_READ);
80913e
+	section = init_pe_section (image_target, section, ".text",
80913e
+				   &vma, layout.exec_size,
80913e
+				   image_target->section_align,
80913e
+				   &raw_data, layout.exec_size,
80913e
+				   GRUB_PE32_SCN_CNT_CODE |
80913e
+				   GRUB_PE32_SCN_MEM_EXECUTE |
80913e
+				   GRUB_PE32_SCN_MEM_READ);
80913e
 
80913e
 	scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
80913e
 	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
80913e
 							       ALIGN_UP (total_module_size,
80913e
 									 GRUB_PE32_FILE_ALIGNMENT));
80913e
 
80913e
-	data_section = text_section + 1;
80913e
-	strcpy (data_section->name, ".data");
80913e
-	data_section->virtual_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
80913e
-	data_section->virtual_address = grub_host_to_target32 (header_size + layout.exec_size);
80913e
-	data_section->raw_data_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
80913e
-	data_section->raw_data_offset = grub_host_to_target32 (header_size + layout.exec_size);
80913e
-	data_section->characteristics
80913e
-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
80913e
-			      | GRUB_PE32_SCN_MEM_READ
80913e
-			      | GRUB_PE32_SCN_MEM_WRITE);
80913e
-    
80913e
-	mods_section = data_section + 1;
80913e
-	strcpy (mods_section->name, "mods");
80913e
-	mods_section->virtual_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
80913e
-	mods_section->virtual_address = grub_host_to_target32 (header_size + layout.kernel_size + layout.bss_size);
80913e
-	mods_section->raw_data_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
80913e
-	mods_section->raw_data_offset = grub_host_to_target32 (header_size + layout.kernel_size);
80913e
-	mods_section->characteristics
80913e
-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
80913e
-			      | GRUB_PE32_SCN_MEM_READ
80913e
-			      | GRUB_PE32_SCN_MEM_WRITE);
80913e
+	section = init_pe_section (image_target, section, ".data",
80913e
+				   &vma, scn_size, image_target->section_align,
80913e
+				   &raw_data, scn_size,
80913e
+				   GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
80913e
+				   GRUB_PE32_SCN_MEM_READ |
80913e
+				   GRUB_PE32_SCN_MEM_WRITE);
80913e
+
80913e
+	scn_size = pe_size - layout.reloc_size - raw_data;
80913e
+	section = init_pe_section (image_target, section, "mods",
80913e
+				   &vma, scn_size, image_target->section_align,
80913e
+				   &raw_data, scn_size,
80913e
+				   GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
80913e
+				   GRUB_PE32_SCN_MEM_READ |
80913e
+				   GRUB_PE32_SCN_MEM_WRITE);
80913e
+
80913e
+	scn_size = layout.reloc_size;
80913e
+	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
80913e
+	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
80913e
+	memcpy (pe_img + raw_data, layout.reloc_section, scn_size);
80913e
+	init_pe_section (image_target, section, ".reloc",
80913e
+			 &vma, scn_size, image_target->section_align,
80913e
+			 &raw_data, scn_size,
80913e
+			 GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
80913e
+			 GRUB_PE32_SCN_MEM_DISCARDABLE |
80913e
+			 GRUB_PE32_SCN_MEM_READ);
80913e
 
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
-	reloc_section = mods_section + 1;
80913e
-	strcpy (reloc_section->name, ".reloc");
80913e
-	reloc_section->virtual_size = grub_host_to_target32 (layout.reloc_size);
80913e
-	reloc_section->virtual_address = grub_host_to_target32 (reloc_addr + layout.bss_size);
80913e
-	reloc_section->raw_data_size = grub_host_to_target32 (layout.reloc_size);
80913e
-	reloc_section->raw_data_offset = grub_host_to_target32 (reloc_addr);
80913e
-	reloc_section->characteristics
80913e
-	  = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
80913e
-			      | GRUB_PE32_SCN_MEM_DISCARDABLE
80913e
-			      | GRUB_PE32_SCN_MEM_READ);
80913e
 	free (core_img);
80913e
 	core_img = pe_img;
80913e
 	core_size = pe_size;