Borrowed and tweaked fix from: commit be8eb0eed69f8bc9ac20837eae58e55218011880 Author: Michael Chang Date: Mon Mar 28 15:00:52 2022 +0800 util/mkimage: Fix dangling pointer may be used error diff --git a/util/mkimage.c b/util/mkimage.c index a26cf76f7..58c199f7c 100644 --- a/util/mkimage 2022-12-11 15:41:56.717934782 +0000 +++ b/util/mkimage.c 2022-12-11 15:43:05.318432532 +0000 @@ -1383,6 +1383,10 @@ section = (struct grub_pe32_section_table *)(o64 + 1); } +#if __GNUC__ >= 12 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdangling-pointer" +#endif PE_OHDR (o32, o64, header_size) = grub_host_to_target32 (header_size); PE_OHDR (o32, o64, entry_addr) = grub_host_to_target32 (layout.start_address); PE_OHDR (o32, o64, image_base) = 0; @@ -1402,6 +1406,9 @@ /* The sections. */ PE_OHDR (o32, o64, code_base) = grub_host_to_target32 (vma); PE_OHDR (o32, o64, code_size) = grub_host_to_target32 (layout.exec_size); +#if __GNUC__ >= 12 +#pragma GCC diagnostic pop +#endif section = init_pe_section (image_target, section, ".text", &vma, layout.exec_size, image_target->section_align, @@ -1413,10 +1420,17 @@ raw_size = layout.kernel_size - layout.exec_size; scn_size = ALIGN_UP (raw_size, GRUB_PE32_FILE_ALIGNMENT); /* ALIGN_UP (sbat_size, GRUB_PE32_FILE_ALIGNMENT) is done earlier. */ +#if __GNUC__ >= 12 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdangling-pointer" +#endif PE_OHDR (o32, o64, data_size) = grub_host_to_target32 (scn_size + sbat_size + ALIGN_UP (total_module_size, GRUB_PE32_FILE_ALIGNMENT)); +#if __GNUC__ >= 12 +#pragma GCC diagnostic pop +#endif section = init_pe_section (image_target, section, ".data", &vma, scn_size, image_target->section_align, &raw_data, raw_size, @@ -1448,8 +1462,15 @@ } scn_size = ALIGN_UP (layout.reloc_size, GRUB_PE32_FILE_ALIGNMENT); +#if __GNUC__ >= 12 +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdangling-pointer" +#endif PE_OHDR (o32, o64, base_relocation_table.rva) = grub_host_to_target32 (vma); PE_OHDR (o32, o64, base_relocation_table.size) = grub_host_to_target32 (scn_size); +#if __GNUC__ >= 12 +#pragma GCC diagnostic pop +#endif memcpy (pe_img + raw_data, layout.reloc_section, scn_size); init_pe_section (image_target, section, ".reloc", &vma, scn_size, image_target->section_align,