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

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