+/* 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 *smd)
+{
+ int i;
+ int r = 0;
+ const char *name = smd->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 = smd->sections; i < smd->num_sections;
+ i++, s = (Elf_Shdr *) ((char *) s + smd->section_entsize))
+ {
+ const char *sname = smd->strtab + grub_host_to_target32 (s->sh_name);
+ if (strcmp (sname, name))
+ continue;
+
+ return SUFFIX (is_kept_section) (s, image_target);
+ }
+
+ return r;
+}
+