Received: from list by lists.gnu.org with archive (Exim 4.71) id 1eoHIK-0001Uj-Et for mharc-grub-devel@gnu.org; Tue, 20 Feb 2018 18:25:56 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:38352) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eoHID-0001OI-Fp for grub-devel@gnu.org; Tue, 20 Feb 2018 18:25:54 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eoHI9-0002kc-59 for grub-devel@gnu.org; Tue, 20 Feb 2018 18:25:49 -0500 Received: from mx3-rdu2.redhat.com ([66.187.233.73]:57554 helo=mx1.redhat.com) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eoHI8-0002jc-Vu for grub-devel@gnu.org; Tue, 20 Feb 2018 18:25:45 -0500 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.rdu2.redhat.com [10.11.54.6]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 8BD018182D15; Tue, 20 Feb 2018 23:25:40 +0000 (UTC) Received: from trillian.uncooperative.org.com (dhcp-10-20-1-221.bss.redhat.com [10.20.1.221]) by smtp.corp.redhat.com (Postfix) with ESMTP id 68A5D213AEE2; Tue, 20 Feb 2018 23:25:40 +0000 (UTC) From: Peter Jones To: grub-devel@gnu.org Cc: Daniel Kiper , Peter Jones Subject: [PATCH v2 2/3] mkimage: avoid copying relocations for sections that won't be copied. Date: Tue, 20 Feb 2018 18:25:32 -0500 Message-Id: <20180220232533.6896-2-pjones@redhat.com> In-Reply-To: <20180220232533.6896-1-pjones@redhat.com> References: <20180220144844.GA29532@router-fw-old.local.net-space.pl> <20180220232533.6896-1-pjones@redhat.com> X-Scanned-By: MIMEDefang 2.78 on 10.11.54.6 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 20 Feb 2018 23:25:40 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.11.55.8]); Tue, 20 Feb 2018 23:25:40 +0000 (UTC) for IP:'10.11.54.6' DOMAIN:'int-mx06.intmail.prod.int.rdu2.redhat.com' HELO:'smtp.corp.redhat.com' FROM:'pjones@redhat.com' RCPT:'' X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 66.187.233.73 X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: The development of GNU GRUB List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Feb 2018 23:25:54 -0000 Some versions of gcc include a plugin called "annobin", and in some build systems this is enabled by default. This plugin creates special ELF note sections to track which ABI-breaking features are used by a binary, as well as a series of relocations to annotate where. If grub is compiled with this feature, then when grub-mkimage translates the binary to another file format which does not strongly associate relocation data with sections (i.e. when platform is *-efi), these relocations appear to be against the .text section rather than the original note section. When the binary is loaded by the PE runtime loader, hilarity ensues. This issue is not necessarily limited to the annobin, but could arise any time there are relocations in sections that are not represented in grub-mkimage's output. This patch seeks to avoid this issue by only including relocations that refer to sections which will be included in the final binary. As an aside, this should also obviate the need to avoid -funwind-tables, -fasynchronous-unwind-tables, and any sections similar to .eh_frame in the future. I've tested it on x86-64-efi with the following gcc command line options (as recorded by -grecord-gcc-flags), but I still need to test the result on some other platforms that have been problematic in the past (especially ARM Aarch64) before I feel comfortable making changes to the configure.ac bits: GNU C11 7.2.1 20180116 (Red Hat 7.2.1-7) -mno-mmx -mno-sse -mno-sse2 -mno-sse3 -mno-3dnow -msoft-float -mno-stack-arg-probe -mcmodel=large -mno-red-zone -m64 -mtune=generic -march=x86-64 -g3 -Os -freg-struct-return -fno-stack-protector -ffreestanding -funwind-tables -fasynchronous-unwind-tables -fno-strict-aliasing -fstack-clash-protection -fno-ident -fplugin=annobin Signed-off-by: Peter Jones --- util/grub-mkimagexx.c | 79 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/util/grub-mkimagexx.c b/util/grub-mkimagexx.c index 5c787ec56bf..c15079c96ba 100644 --- a/util/grub-mkimagexx.c +++ b/util/grub-mkimagexx.c @@ -716,6 +716,12 @@ arm_get_trampoline_size (Elf_Ehdr *e, } #endif +static int +SUFFIX (is_kept_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target); +static int +SUFFIX (is_kept_reloc_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target, + struct section_metadata *sdata); + /* Deal with relocation information. This function relocates addresses within the virtual address space starting from 0. So only relative addresses can be fully resolved. Absolute addresses must be relocated @@ -750,6 +756,14 @@ SUFFIX (relocate_addresses) (Elf_Ehdr *e, struct section_metadata *sdata, Elf_Shdr *target_section; Elf_Word j; + if (!SUFFIX (is_kept_section) (s, image_target) && + !SUFFIX (is_kept_reloc_section) (s, image_target, sdata)) + { + grub_util_info ("not translating relocations for omitted section %s", + sdata->strtab + grub_le_to_cpu32 (s->sh_name)); + continue; + } + target_section_index = grub_target_to_host32 (s->sh_info); target_section_addr = sdata->addresses[target_section_index]; target_section = (Elf_Shdr *) ((char *) sdata->sections @@ -1654,6 +1668,13 @@ make_reloc_section (Elf_Ehdr *e, struct grub_mkimage_layout *layout, Elf_Addr section_address; Elf_Word j; + if (!SUFFIX (is_kept_reloc_section) (s, image_target, sdata)) + { + grub_util_info ("not translating the skipped relocation section %s", + sdata->strtab + grub_le_to_cpu32 (s->sh_name)); + continue; + } + grub_util_info ("translating the relocation section %s", sdata->strtab + grub_le_to_cpu32 (s->sh_name)); @@ -1729,6 +1750,55 @@ SUFFIX (is_bss_section) (Elf_Shdr *s, const struct grub_install_image_target_des == SHF_ALLOC) && (grub_target_to_host32 (s->sh_type) == SHT_NOBITS); } +/* Determine if a section is going to be in the final output */ +static int +SUFFIX (is_kept_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target) +{ + /* We keep .text and .data */ + if (SUFFIX (is_text_section) (s, image_target) + || SUFFIX (is_data_section) (s, image_target)) + return 1; + + /* And we keep .bss if we're producing PE binaries or the target doesn't + * have a relocating loader. Platforms other than EFI and U-boot shouldn't + * have .bss in their binaries as we build with -Wl,-Ttext. + */ + if (SUFFIX (is_bss_section) (s, image_target) + && (image_target->id == IMAGE_EFI || !is_relocatable (image_target))) + return 1; + + /* Otherwise this is not a section we're keeping in the final output. */ + return 0; +} + +static int +SUFFIX (is_kept_reloc_section) (Elf_Shdr *s, const struct grub_install_image_target_desc *image_target, + struct section_metadata *sdata) +{ + int i; + int r = 0; + const char *name = sdata->strtab + grub_host_to_target32 (s->sh_name); + + if (!strncmp (name, ".rela.", 6)) + name += 5; + else if (!strncmp (name, ".rel.", 5)) + name += 4; + else + return 1; + + for (i = 0, s = sdata->sections; i < sdata->num_sections; + i++, s = (Elf_Shdr *) ((char *) s + sdata->section_entsize)) + { + const char *sname = sdata->strtab + grub_host_to_target32 (s->sh_name); + if (strcmp (sname, name)) + continue; + + return SUFFIX (is_kept_section) (s, image_target); + } + + return r; +} + /* Return if the ELF header is valid. */ static int SUFFIX (check_elf_header) (Elf_Ehdr *e, size_t size, const struct grub_install_image_target_desc *image_target) @@ -2051,14 +2121,7 @@ SUFFIX (grub_mkimage_load_image) (const char *kernel_path, for (i = 0, s = sdata.sections; i < sdata.num_sections; i++, s = (Elf_Shdr *) ((char *) s + sdata.section_entsize)) - if (SUFFIX (is_data_section) (s, image_target) - /* Explicitly initialize BSS - when producing PE32 to avoid a bug in EFI implementations. - Platforms other than EFI and U-boot shouldn't have .bss in - their binaries as we build with -Wl,-Ttext. - */ - || (SUFFIX (is_bss_section) (s, image_target) && (image_target->id == IMAGE_EFI || !is_relocatable (image_target))) - || SUFFIX (is_text_section) (s, image_target)) + if (SUFFIX (is_kept_section) (s, image_target)) { if (grub_target_to_host32 (s->sh_type) == SHT_NOBITS) memset (out_img + sdata.addresses[i], 0, -- 2.15.0