Blame SOURCES/0472-util-mkimage-Improve-data_size-value-calculation.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Peter Jones <pjones@redhat.com>
80913e
Date: Thu, 11 Feb 2021 17:07:33 +0100
80913e
Subject: [PATCH] util/mkimage: Improve data_size value calculation
80913e
80913e
According to "Microsoft Portable Executable and Common Object File Format
80913e
Specification", the Optional Header SizeOfInitializedData field contains:
80913e
80913e
  Size of the initialized data section, or the sum of all such sections if
80913e
  there are multiple data sections.
80913e
80913e
Make this explicit by adding the GRUB kernel data size to the sum of all
80913e
the modules sizes. The ALIGN_UP() is not required by the PE spec but do
80913e
it to avoid alignment issues.
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 | 6 +++++-
80913e
 1 file changed, 5 insertions(+), 1 deletion(-)
80913e
80913e
diff --git a/util/mkimage.c b/util/mkimage.c
b32e65
index e73a5864b..f22b398d9 100644
80913e
--- a/util/mkimage.c
80913e
+++ b/util/mkimage.c
80913e
@@ -1248,6 +1248,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 	void *pe_img;
80913e
 	grub_uint8_t *header;
80913e
 	void *sections;
80913e
+	size_t scn_size;
80913e
 	size_t pe_size;
80913e
 	struct grub_pe32_coff_header *c;
80913e
 	struct grub_pe32_section_table *text_section, *data_section;
80913e
@@ -1350,7 +1351,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
80913e
 						| GRUB_PE32_SCN_MEM_EXECUTE
80913e
 						| GRUB_PE32_SCN_MEM_READ);
80913e
 
80913e
-	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - layout.exec_size - header_size);
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");