1From a4e8936f010a8e928e973b80390c8f83ad6b8000 Mon Sep 17 00:00:00 2001
2From: Peter Jones <pjones@redhat.com>
3Date: Mon, 15 Feb 2021 14:19:31 +0100
4Subject: [PATCH] util/mkimage: Unify more of the PE32 and PE32+ header set-up
5
6There's quite a bit of code duplication in the code that sets the optional
7header for PE32 and PE32+. The two are very similar with the exception of
8a few fields that have type grub_uint64_t instead of grub_uint32_t.
9
10Factor out the common code and add a PE_OHDR() macro that simplifies the
11set-up and make the code more readable.
12
13Signed-off-by: Peter Jones <pjones@redhat.com>
14Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
15Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
16Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
17---
18 util/mkimage.c | 111 ++++++++++++++++++++++++++-------------------------------
19 1 file changed, 51 insertions(+), 60 deletions(-)
20
21diff --git a/util/mkimage.c b/util/mkimage.c
22index b94bfb7..a039039 100644
23--- a/util/mkimage.c
24+++ b/util/mkimage.c
25@@ -816,6 +816,21 @@ grub_install_get_image_targets_string (void)
26   return formats;
27 }
28
29+/*
30+ * tmp_ is just here so the compiler knows we'll never derefernce a NULL.
31+ * It should get fully optimized away.
32+ */
33+#define PE_OHDR(o32, o64, field) (*(		\
34+{						\
35+  __typeof__((o64)->field) tmp_;		\
36+  __typeof__((o64)->field) *ret_ = &tmp_;	\
37+  if (o32)					\
38+    ret_ = (void *)(&((o32)->field));		\
39+  else if (o64)				\
40+    ret_ = (void *)(&((o64)->field));		\
41+  ret_;					\
42+}))
43+
44 void
45 grub_install_generate_image (const char *dir, const char *prefix,
46 			     FILE *out, const char *outname, char *mods[],
47@@ -1252,6 +1267,8 @@ grub_install_generate_image (const char *dir, const char *prefix,
48 	static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
49 	int header_size;
50 	int reloc_addr;
51+	struct grub_pe32_optional_header *o32 = NULL;
52+	struct grub_pe64_optional_header *o64 = NULL;
53
54 	if (image_target->voidp_sizeof == 4)
55 	  header_size = EFI32_HEADER_SIZE;
56@@ -1293,76 +1310,50 @@ grub_install_generate_image (const char *dir, const char *prefix,
57 	/* The PE Optional header.  */
58 	if (image_target->voidp_sizeof == 4)
59 	  {
60-	    struct grub_pe32_optional_header *o;
61-
62 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe32_optional_header));
63
64-	    o = (struct grub_pe32_optional_header *)
65-	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE
66-	       + sizeof (struct grub_pe32_coff_header));
67-	    o->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
68-	    o->code_size = grub_host_to_target32 (layout.exec_size);
69-	    o->data_size = grub_host_to_target32 (reloc_addr - layout.exec_size
70-					     - header_size);
71-	    o->entry_addr = grub_host_to_target32 (layout.start_address);
72-	    o->code_base = grub_host_to_target32 (header_size);
73-
74-	    o->data_base = grub_host_to_target32 (header_size + layout.exec_size);
75-
76-	    o->image_base = 0;
77-	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
78-	    o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
79-	    o->image_size = grub_host_to_target32 (pe_size);
80-	    o->header_size = grub_host_to_target32 (header_size);
81-	    o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
82-
83-	    /* Do these really matter? */
84-	    o->stack_reserve_size = grub_host_to_target32 (0x10000);
85-	    o->stack_commit_size = grub_host_to_target32 (0x10000);
86-	    o->heap_reserve_size = grub_host_to_target32 (0x10000);
87-	    o->heap_commit_size = grub_host_to_target32 (0x10000);
88-
89-	    o->num_data_directories = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
90+	    o32 = (struct grub_pe32_optional_header *)
91+              (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
92+               sizeof (struct grub_pe32_coff_header));
93+	    o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
94+	    o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
95
96-	    o->base_relocation_table.rva = grub_host_to_target32 (reloc_addr);
97-	    o->base_relocation_table.size = grub_host_to_target32 (layout.reloc_size);
98-	    sections = o + 1;
99+	    sections = o32 + 1;
100 	  }
101 	else
102 	  {
103-	    struct grub_pe64_optional_header *o;
104-
105 	    c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
106
107-	    o = (struct grub_pe64_optional_header *)
108-	      (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE
109-	       + sizeof (struct grub_pe32_coff_header));
110-	    o->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
111-	    o->code_size = grub_host_to_target32 (layout.exec_size);
112-	    o->data_size = grub_host_to_target32 (reloc_addr - layout.exec_size
113-					     - header_size);
114-	    o->entry_addr = grub_host_to_target32 (layout.start_address);
115-	    o->code_base = grub_host_to_target32 (header_size);
116-	    o->image_base = 0;
117-	    o->section_alignment = grub_host_to_target32 (image_target->section_align);
118-	    o->file_alignment = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
119-	    o->image_size = grub_host_to_target32 (pe_size);
120-	    o->header_size = grub_host_to_target32 (header_size);
121-	    o->subsystem = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
122-
123-	    /* Do these really matter? */
124-	    o->stack_reserve_size = grub_host_to_target32 (0x10000);
125-	    o->stack_commit_size = grub_host_to_target32 (0x10000);
126-	    o->heap_reserve_size = grub_host_to_target32 (0x10000);
127-	    o->heap_commit_size = grub_host_to_target32 (0x10000);
128-
129-	    o->num_data_directories
130-	      = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
131+	    o64 = (struct grub_pe64_optional_header *)
132+		  (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
133+                   sizeof (struct grub_pe32_coff_header));
134+	    o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
135
136-	    o->base_relocation_table.rva = grub_host_to_target32 (reloc_addr);
137-	    o->base_relocation_table.size = grub_host_to_target32 (layout.reloc_size);
138-	    sections = o + 1;
139+	    sections = o64 + 1;
140 	  }
141+
142+	PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
143+	PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - layout.exec_size - header_size);
144+	PE_OHDR (o32, o64, entry_addr) = grub_host_to_target32 (layout.start_address);
145+	PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
146+
147+	PE_OHDR (o32, o64, image_base) = 0;
148+	PE_OHDR (o32, o64, section_alignment) = grub_host_to_target32 (image_target->section_align);
149+	PE_OHDR (o32, o64, file_alignment) = grub_host_to_target32 (GRUB_PE32_FILE_ALIGNMENT);
150+	PE_OHDR (o32, o64, image_size) = grub_host_to_target32 (pe_size);
151+	PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
152+	PE_OHDR (o32, o64, subsystem) = grub_host_to_target16 (GRUB_PE32_SUBSYSTEM_EFI_APPLICATION);
153+
154+	/* Do these really matter? */
155+	PE_OHDR (o32, o64, stack_reserve_size) = grub_host_to_target32 (0x10000);
156+	PE_OHDR (o32, o64, stack_commit_size) = grub_host_to_target32 (0x10000);
157+	PE_OHDR (o32, o64, heap_reserve_size) = grub_host_to_target32 (0x10000);
158+	PE_OHDR (o32, o64, heap_commit_size) = grub_host_to_target32 (0x10000);
159+
160+	PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
161+	PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
162+	PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
163+
164 	/* The sections.  */
165 	text_section = sections;
166 	strcpy (text_section->name, ".text");
167--
1682.14.2
169
170