0137-util-mkimage-Unify-more-of-the-PE32-and-PE32-header-.patch 7.5 KB

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