0140-util-mkimage-Refactor-section-setup-to-use-a-helper.patch 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. From f60ba9e5945892e835e53f0619406d96002f7f70 Mon Sep 17 00:00:00 2001
  2. From: Peter Jones <pjones@redhat.com>
  3. Date: Mon, 15 Feb 2021 14:58:06 +0100
  4. Subject: [PATCH] util/mkimage: Refactor section setup to use a helper
  5. Add a init_pe_section() helper function to setup PE sections. This makes
  6. the code simpler and easier to read.
  7. Signed-off-by: Peter Jones <pjones@redhat.com>
  8. Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
  9. Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
  10. Signed-off-by: Stefan Sørensen <stefan.sorensen@spectralink.com>
  11. ---
  12. util/mkimage.c | 143 +++++++++++++++++++++++++++++++--------------------------
  13. 1 file changed, 77 insertions(+), 66 deletions(-)
  14. diff --git a/util/mkimage.c b/util/mkimage.c
  15. index 853a521..8b475a6 100644
  16. --- a/util/mkimage.c
  17. +++ b/util/mkimage.c
  18. @@ -816,6 +816,38 @@ grub_install_get_image_targets_string (void)
  19. return formats;
  20. }
  21. +/*
  22. + * The image_target parameter is used by the grub_host_to_target32() macro.
  23. + */
  24. +static struct grub_pe32_section_table *
  25. +init_pe_section(const struct grub_install_image_target_desc *image_target,
  26. + struct grub_pe32_section_table *section,
  27. + const char * const name,
  28. + grub_uint32_t *vma, grub_uint32_t vsz, grub_uint32_t valign,
  29. + grub_uint32_t *rda, grub_uint32_t rsz,
  30. + grub_uint32_t characteristics)
  31. +{
  32. + size_t len = strlen (name);
  33. +
  34. + if (len > sizeof (section->name))
  35. + grub_util_error (_("section name %s length is bigger than %lu"),
  36. + name, (unsigned long) sizeof (section->name));
  37. +
  38. + memcpy (section->name, name, len);
  39. +
  40. + section->virtual_address = grub_host_to_target32 (*vma);
  41. + section->virtual_size = grub_host_to_target32 (vsz);
  42. + (*vma) = ALIGN_UP (*vma + vsz, valign);
  43. +
  44. + section->raw_data_offset = grub_host_to_target32 (*rda);
  45. + section->raw_data_size = grub_host_to_target32 (rsz);
  46. + (*rda) = ALIGN_UP (*rda + rsz, GRUB_PE32_FILE_ALIGNMENT);
  47. +
  48. + section->characteristics = grub_host_to_target32 (characteristics);
  49. +
  50. + return section + 1;
  51. +}
  52. +
  53. /*
  54. * tmp_ is just here so the compiler knows we'll never derefernce a NULL.
  55. * It should get fully optimized away.
  56. @@ -1257,17 +1289,13 @@ grub_install_generate_image (const char *dir, const char *prefix,
  57. break;
  58. case IMAGE_EFI:
  59. {
  60. - void *pe_img;
  61. - grub_uint8_t *header;
  62. - void *sections;
  63. + char *pe_img, *header;
  64. + struct grub_pe32_section_table *section;
  65. size_t scn_size;
  66. - size_t pe_size;
  67. + grub_uint32_t vma, raw_data;
  68. + size_t pe_size, header_size;
  69. struct grub_pe32_coff_header *c;
  70. - struct grub_pe32_section_table *text_section, *data_section;
  71. - struct grub_pe32_section_table *mods_section, *reloc_section;
  72. static const grub_uint8_t stub[] = GRUB_PE32_MSDOS_STUB;
  73. - int header_size;
  74. - int reloc_addr;
  75. struct grub_pe32_optional_header *o32 = NULL;
  76. struct grub_pe64_optional_header *o64 = NULL;
  77. @@ -1276,17 +1304,12 @@ grub_install_generate_image (const char *dir, const char *prefix,
  78. else
  79. header_size = EFI64_HEADER_SIZE;
  80. - reloc_addr = ALIGN_UP (header_size + core_size,
  81. - GRUB_PE32_FILE_ALIGNMENT);
  82. + vma = raw_data = header_size;
  83. + pe_size = ALIGN_UP (header_size + core_size, GRUB_PE32_FILE_ALIGNMENT) +
  84. + ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT);
  85. + header = pe_img = xcalloc (1, pe_size);
  86. - pe_size = ALIGN_UP (reloc_addr + layout.reloc_size,
  87. - GRUB_PE32_FILE_ALIGNMENT);
  88. - pe_img = xmalloc (reloc_addr + layout.reloc_size);
  89. - memset (pe_img, 0, header_size);
  90. - memcpy ((char *) pe_img + header_size, core_img, core_size);
  91. - memset ((char *) pe_img + header_size + core_size, 0, reloc_addr - (header_size + core_size));
  92. - memcpy ((char *) pe_img + reloc_addr, layout.reloc_section, layout.reloc_size);
  93. - header = pe_img;
  94. + memcpy (pe_img + raw_data, core_img, core_size);
  95. /* The magic. */
  96. memcpy (header, stub, GRUB_PE32_MSDOS_STUB_SIZE);
  97. @@ -1319,18 +1342,17 @@ grub_install_generate_image (const char *dir, const char *prefix,
  98. o32->magic = grub_host_to_target16 (GRUB_PE32_PE32_MAGIC);
  99. o32->data_base = grub_host_to_target32 (header_size + layout.exec_size);
  100. - sections = o32 + 1;
  101. + section = (struct grub_pe32_section_table *)(o32 + 1);
  102. }
  103. else
  104. {
  105. c->optional_header_size = grub_host_to_target16 (sizeof (struct grub_pe64_optional_header));
  106. -
  107. o64 = (struct grub_pe64_optional_header *)
  108. (header + GRUB_PE32_MSDOS_STUB_SIZE + GRUB_PE32_SIGNATURE_SIZE +
  109. sizeof (struct grub_pe32_coff_header));
  110. o64->magic = grub_host_to_target16 (GRUB_PE32_PE64_MAGIC);
  111. - sections = o64 + 1;
  112. + section = (struct grub_pe32_section_table *)(o64 + 1);
  113. }
  114. PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size);
  115. @@ -1350,58 +1372,47 @@ grub_install_generate_image (const char *dir, const char *prefix,
  116. PE_OHDR (o32, o64, num_data_directories) = grub_host_to_target32 (GRUB_PE32_NUM_DATA_DIRECTORIES);
  117. /* The sections. */
  118. - PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (header_size);
  119. + PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma);
  120. PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size);
  121. - text_section = sections;
  122. - strcpy (text_section->name, ".text");
  123. - text_section->virtual_size = grub_host_to_target32 (layout.exec_size);
  124. - text_section->virtual_address = grub_host_to_target32 (header_size);
  125. - text_section->raw_data_size = grub_host_to_target32 (layout.exec_size);
  126. - text_section->raw_data_offset = grub_host_to_target32 (header_size);
  127. - text_section->characteristics = grub_cpu_to_le32_compile_time (
  128. - GRUB_PE32_SCN_CNT_CODE
  129. - | GRUB_PE32_SCN_MEM_EXECUTE
  130. - | GRUB_PE32_SCN_MEM_READ);
  131. + section = init_pe_section (image_target, section, ".text",
  132. + &vma, layout.exec_size,
  133. + image_target->section_align,
  134. + &raw_data, layout.exec_size,
  135. + GRUB_PE32_SCN_CNT_CODE |
  136. + GRUB_PE32_SCN_MEM_EXECUTE |
  137. + GRUB_PE32_SCN_MEM_READ);
  138. scn_size = ALIGN_UP (layout.kernel_size - layout.exec_size, GRUB_PE32_FILE_ALIGNMENT);
  139. PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size +
  140. ALIGN_UP (total_module_size,
  141. GRUB_PE32_FILE_ALIGNMENT));
  142. - data_section = text_section + 1;
  143. - strcpy (data_section->name, ".data");
  144. - data_section->virtual_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
  145. - data_section->virtual_address = grub_host_to_target32 (header_size + layout.exec_size);
  146. - data_section->raw_data_size = grub_host_to_target32 (layout.kernel_size - layout.exec_size);
  147. - data_section->raw_data_offset = grub_host_to_target32 (header_size + layout.exec_size);
  148. - data_section->characteristics
  149. - = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  150. - | GRUB_PE32_SCN_MEM_READ
  151. - | GRUB_PE32_SCN_MEM_WRITE);
  152. -
  153. - mods_section = data_section + 1;
  154. - strcpy (mods_section->name, "mods");
  155. - mods_section->virtual_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
  156. - mods_section->virtual_address = grub_host_to_target32 (header_size + layout.kernel_size + layout.bss_size);
  157. - mods_section->raw_data_size = grub_host_to_target32 (reloc_addr - layout.kernel_size - header_size);
  158. - mods_section->raw_data_offset = grub_host_to_target32 (header_size + layout.kernel_size);
  159. - mods_section->characteristics
  160. - = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  161. - | GRUB_PE32_SCN_MEM_READ
  162. - | GRUB_PE32_SCN_MEM_WRITE);
  163. -
  164. - PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (reloc_addr);
  165. - PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (layout.reloc_size);
  166. - reloc_section = mods_section + 1;
  167. - strcpy (reloc_section->name, ".reloc");
  168. - reloc_section->virtual_size = grub_host_to_target32 (layout.reloc_size);
  169. - reloc_section->virtual_address = grub_host_to_target32 (reloc_addr + layout.bss_size);
  170. - reloc_section->raw_data_size = grub_host_to_target32 (layout.reloc_size);
  171. - reloc_section->raw_data_offset = grub_host_to_target32 (reloc_addr);
  172. - reloc_section->characteristics
  173. - = grub_cpu_to_le32_compile_time (GRUB_PE32_SCN_CNT_INITIALIZED_DATA
  174. - | GRUB_PE32_SCN_MEM_DISCARDABLE
  175. - | GRUB_PE32_SCN_MEM_READ);
  176. + section = init_pe_section (image_target, section, ".data",
  177. + &vma, scn_size, image_target->section_align,
  178. + &raw_data, scn_size,
  179. + GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  180. + GRUB_PE32_SCN_MEM_READ |
  181. + GRUB_PE32_SCN_MEM_WRITE);
  182. +
  183. + scn_size = pe_size - layout.reloc_size - raw_data;
  184. + section = init_pe_section (image_target, section, "mods",
  185. + &vma, scn_size, image_target->section_align,
  186. + &raw_data, scn_size,
  187. + GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  188. + GRUB_PE32_SCN_MEM_READ |
  189. + GRUB_PE32_SCN_MEM_WRITE);
  190. +
  191. + scn_size = layout.reloc_size;
  192. + PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma);
  193. + PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size);
  194. + memcpy (pe_img + raw_data, layout.reloc_section, scn_size);
  195. + init_pe_section (image_target, section, ".reloc",
  196. + &vma, scn_size, image_target->section_align,
  197. + &raw_data, scn_size,
  198. + GRUB_PE32_SCN_CNT_INITIALIZED_DATA |
  199. + GRUB_PE32_SCN_MEM_DISCARDABLE |
  200. + GRUB_PE32_SCN_MEM_READ);
  201. +
  202. free (core_img);
  203. core_img = pe_img;
  204. core_size = pe_size;
  205. --
  206. 2.14.2