nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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