Skip to contentSkip to conversation

util/mkimage: Fix wrong PE32+ section sizes for some arches

RFC PATCH Superseded Javier Martinez Canillas 1 participant 1 message Apr 21, 2021

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 util/mkimage: Fix wrong PE32+ section sizes for some arches

Commit f60ba9e5945 (util/mkimage: Refactor section setup to use a helper) added a helper function to setup PE sections. But it also changed how the raw data offsets were calculated since all the section sizes are aligned.

But for some platforms (i.e: ia64 and aa64) the kernel image size wasn't aligned using the section alignment, which causes the PE section headers to not match the actual section sizes in the PE32+ binary file.

This caused problems on ia64 EFI machines, since the .data section size is bigger than the actual section in the PE32+ binary, overlapping with part of the mods section. That leads to GRUB not being able to load any built-in module.

Fix it by aligning the kernel_size to the section alignment, that makes the sizes and offsets in the PE section headers to match the sections in the PE32+ binary file.

---

Hello, this is an RFC because I want someone else more familiar with this to double check that this approach is sane.

It would be also useful if someone can test on an aarch64 machine, I have compared the generated EFI binaries and are the same in both cases. But still it seems to me that an explicit alignment is needed for EM_AARCH64.

Best regards,
Javier

 util/grub-mkimagexx.c | 9 +++++++++
 1 file changed, 9 insertions(+)
util/grub-mkimagexx.c +9 -0
index 00f49ccaaaf..2d0c9a0cb6b 100644
@@ -2375,6 +2375,10 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
 
 	  layout->got_off = layout->kernel_size;
 	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
+
+          if (image_target->id == IMAGE_EFI)
+            layout->kernel_size = ALIGN_UP (layout->kernel_size,
+                                            image_target->section_align);
 	}
       if (image_target->elf_target == EM_AARCH64)
 	{
@@ -2386,6 +2390,11 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path,
 
 	  layout->got_off = layout->kernel_size;
 	  layout->kernel_size += ALIGN_UP (layout->got_size, 16);
+
+
+          if (image_target->id == IMAGE_EFI)
+            layout->kernel_size = ALIGN_UP (layout->kernel_size,
+                                            image_target->section_align);
 	}
 #endif
     }
Signature · 1 line
2.31.1