0139-util-mkimage-Improve-data_size-value-calculation.patch 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. From ff406eff25465932b97a2857ee5a75fd0957e9b9 Mon Sep 17 00:00:00 2001
  2. From: Peter Jones <pjones@redhat.com>
  3. Date: Thu, 11 Feb 2021 17:07:33 +0100
  4. Subject: [PATCH] util/mkimage: Improve data_size value calculation
  5. According to "Microsoft Portable Executable and Common Object File Format
  6. Specification", the Optional Header SizeOfInitializedData field contains:
  7. Size of the initialized data section, or the sum of all such sections if
  8. there are multiple data sections.
  9. Make this explicit by adding the GRUB kernel data size to the sum of all
  10. the modules sizes. The ALIGN_UP() is not required by the PE spec but do
  11. it to avoid alignment issues.
  12. Signed-off-by: Peter Jones <pjones@redhat.com>
  13. Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
  14. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  15. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  16. ---
  17. util/mkimage.c | 6 +++++-
  18. 1 file changed, 5 insertions(+), 1 deletion(-)
  19. diff --git a/util/mkimage.c b/util/mkimage.c
  20. index deaef56..853a521 100644
  21. --- a/util/mkimage.c
  22. +++ b/util/mkimage.c
  23. @@ -1260,6 +1260,7 @@ grub_install_generate_image (const char *dir, const char *prefix,
  24. void *pe_img;
  25. grub_uint8_t *header;
  26. void *sections;
  27. + size_t scn_size;
  28. size_t pe_size;
  29. struct grub_pe32_coff_header *c;
  30. struct grub_pe32_section_table *text_section, *data_section;
  31. @@ -1362,7 +1363,10 @@ grub_install_generate_image (const char *dir, const char *prefix,
  32. | GRUB_PE32_SCN_MEM_EXECUTE
  33. | GRUB_PE32_SCN_MEM_READ);
  34. - PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (reloc_addr - layout.exec_size - header_size);
  35. + scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
  36. + PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
  37. + ALIGN_UP (total_module_size,
  38. + GRUB_PE32_FILE_ALIGNMENT));
  39. data_section = text_section + 1;
  40. strcpy (data_section->name, ".data");
  41. --
  42. 2.14.2