1From ba44c87e56a8bccde235ebb7d41d5aa54604d241 Mon Sep 17 00:00:00 2001
2From: Peter Jones <pjones@redhat.com>
3Date: Mon, 15 Feb 2021 14:21:48 +0100
4Subject: [PATCH] util/mkimage: Reorder PE optional header fields set-up
5
6This makes the PE32 and PE32+ header fields set-up easier to follow by
7setting them closer to the initialization of their related sections.
8
9Signed-off-by: Peter Jones <pjones@redhat.com>
10Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
11Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
12Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
13---
14 util/mkimage.c | 16 ++++++++--------
15 1 file changed, 8 insertions(+), 8 deletions(-)
16
17diff --git a/util/mkimage.c b/util/mkimage.c
18index a039039..deaef56 100644
19--- a/util/mkimage.c
20+++ b/util/mkimage.c
21@@ -1332,16 +1332,12 @@ grub_install_generate_image (const char *dir, const char *prefix,
22 	    sections = o64 + 1;
23 	  }
24
25-	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
26-	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - layout.exec_size - header_size);
27+	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
28 	PE_OHDR (o32, o64, entry_addr) = grub_host_to_target32 (layout.start_address);
29-	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
30-
31 	PE_OHDR (o32, o64, image_base) = 0;
32+	PE_OHDR (o32, o64, image_size) = grub_host_to_target32 (pe_size);
33 	PE_OHDR (o32, o64, section_alignment) = grub_host_to_target32 (image_target->section_align);
34 	PE_OHDR (o32, o64, file_alignment) = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
35-	PE_OHDR (o32, o64, image_size) = grub_host_to_target32 (pe_size);
36-	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
37 	PE_OHDR (o32, o64, subsystem) = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
38
39 	/* Do these really matter? */
40@@ -1351,10 +1347,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
41 	PE_OHDR (o32, o64, heap_commit_size) = grub_host_to_target32 (0x10000);
42
43 	PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
44-	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
45-	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
46
47 	/* The sections.  */
48+	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
49+	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
50 	text_section = sections;
51 	strcpy (text_section->name, ".text");
52 	text_section->virtual_size = grub_host_to_target32 (layout.exec_size);
53@@ -1366,6 +1362,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
54 						| GRUB_PE32_SCN_MEM_EXECUTE
55 						| GRUB_PE32_SCN_MEM_READ);
56
57+	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - layout.exec_size - header_size);
58+
59 	data_section = text_section + 1;
60 	strcpy (data_section->name, ".data");
61 	data_section->virtual_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
62@@ -1388,6 +1386,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
63 			      | GRUB_PE32_SCN_MEM_READ
64 			      | GRUB_PE32_SCN_MEM_WRITE);
65
66+	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
67+	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
68 	reloc_section = mods_section + 1;
69 	strcpy (reloc_section->name, ".reloc");
70 	reloc_section->virtual_size = grub_host_to_target32 (layout.reloc_size);
71--
722.14.2
73
74