+ if (cie->fde_encoding == DW_EH_PE_omit)
+ cie->fde_encoding = DW_EH_PE_absptr;
+
+ initial_insn_length = end - buf;
+ cie->initial_insn_length = initial_insn_length;
+ memcpy (cie->initial_instructions, buf,
+ initial_insn_length <= sizeof (cie->initial_instructions)
+ ? initial_insn_length : sizeof (cie->initial_instructions));
+ insns = buf;
+ buf += initial_insn_length;
+ ENSURE_NO_RELOCS (buf);
+
+ if (!bfd_link_relocatable (info))
+ {
+ /* Keep info for merging cies. */
+ this_inf->u.cie.u.full_cie = cie;
+ this_inf->u.cie.per_encoding_relative
+ = (cie->per_encoding & 0x70) == DW_EH_PE_pcrel;
+ }
+ }
+ else
+ {
+ /* Find the corresponding CIE. */
+ unsigned int cie_offset = this_inf->offset + 4 - hdr_id;
+ for (cie = local_cies; cie < local_cies + cie_count; cie++)
+ if (cie_offset == cie->cie_inf->offset)
+ break;
+
+ /* Ensure this FDE references one of the CIEs in this input
+ section. */
+ REQUIRE (cie != local_cies + cie_count);
+ this_inf->u.fde.cie_inf = cie->cie_inf;
+ this_inf->make_relative = cie->cie_inf->make_relative;
+ this_inf->add_augmentation_size
+ = cie->cie_inf->add_augmentation_size;
+
+ ENSURE_NO_RELOCS (buf);
+ if ((sec->flags & SEC_LINKER_CREATED) == 0 || cookie->rels != NULL)
+ {
+ asection *rsec;
+
+ REQUIRE (GET_RELOC (buf));
+
+ /* Chain together the FDEs for each section. */
+ rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook,
+ cookie, NULL);
+ /* RSEC will be NULL if FDE was cleared out as it was belonging to
+ a discarded SHT_GROUP. */
+ if (rsec)
+ {
+ REQUIRE (rsec->owner == abfd);
+ this_inf->u.fde.next_for_section = elf_fde_list (rsec);
+ elf_fde_list (rsec) = this_inf;
+ }
+ }
+
+ /* Skip the initial location and address range. */
+ start = buf;
+ length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
+ REQUIRE (skip_bytes (&buf, end, 2 * length));
+
+ SKIP_RELOCS (buf - length);
+ if (!GET_RELOC (buf - length)
+ && read_value (abfd, buf - length, length, FALSE) == 0)
+ {
+ (*info->callbacks->minfo)
+ (_("discarding zero address range FDE in %B(%A).\n"),
+ abfd, sec);
+ this_inf->u.fde.cie_inf = NULL;
+ }
+
+ /* Skip the augmentation size, if present. */
+ if (cie->augmentation[0] == 'z')
+ REQUIRE (read_uleb128 (&buf, end, &length));
+ else
+ length = 0;
+
+ /* Of the supported augmentation characters above, only 'L'
+ adds augmentation data to the FDE. This code would need to
+ be adjusted if any future augmentations do the same thing. */
+ if (cie->lsda_encoding != DW_EH_PE_omit)
+ {
+ SKIP_RELOCS (buf);
+ if (cie->can_make_lsda_relative && GET_RELOC (buf))
+ cie->cie_inf->u.cie.make_lsda_relative = 1;
+ this_inf->lsda_offset = buf - start;
+ /* If there's no 'z' augmentation, we don't know where the
+ CFA insns begin. Assume no padding. */
+ if (cie->augmentation[0] != 'z')
+ length = end - buf;
+ }
+
+ /* Skip over the augmentation data. */
+ REQUIRE (skip_bytes (&buf, end, length));
+ insns = buf;
+
+ buf = last_fde + 4 + hdr_length;
+
+ /* For NULL RSEC (cleared FDE belonging to a discarded section)
+ the relocations are commonly cleared. We do not sanity check if
+ all these relocations are cleared as (1) relocations to
+ .gcc_except_table will remain uncleared (they will get dropped
+ with the drop of this unused FDE) and (2) BFD already safely drops
+ relocations of any type to .eh_frame by
+ elf_section_ignore_discarded_relocs.
+ TODO: The .gcc_except_table entries should be also filtered as
+ .eh_frame entries; or GCC could rather use COMDAT for them. */
+ SKIP_RELOCS (buf);
+ }
+
+ /* Try to interpret the CFA instructions and find the first
+ padding nop. Shrink this_inf's size so that it doesn't
+ include the padding. */
+ length = get_DW_EH_PE_width (cie->fde_encoding, ptr_size);
+ set_loc_count = 0;
+ insns_end = skip_non_nops (insns, end, length, &set_loc_count);
+ /* If we don't understand the CFA instructions, we can't know
+ what needs to be adjusted there. */
+ if (insns_end == NULL
+ /* For the time being we don't support DW_CFA_set_loc in
+ CIE instructions. */
+ || (set_loc_count && this_inf->cie))
+ goto free_no_table;
+ this_inf->size -= end - insns_end;
+ if (insns_end != end && this_inf->cie)
+ {
+ cie->initial_insn_length -= end - insns_end;
+ cie->length -= end - insns_end;
+ }
+ if (set_loc_count
+ && ((cie->fde_encoding & 0x70) == DW_EH_PE_pcrel
+ || this_inf->make_relative))
+ {
+ unsigned int cnt;
+ bfd_byte *p;
+
+ this_inf->set_loc = (unsigned int *)
+ bfd_malloc ((set_loc_count + 1) * sizeof (unsigned int));
+ REQUIRE (this_inf->set_loc);
+ this_inf->set_loc[0] = set_loc_count;
+ p = insns;
+ cnt = 0;
+ while (p < end)
+ {
+ if (*p == DW_CFA_set_loc)
+ this_inf->set_loc[++cnt] = p + 1 - start;
+ REQUIRE (skip_cfa_op (&p, end, length));
+ }
+ }
+
+ this_inf->removed = 1;
+ this_inf->fde_encoding = cie->fde_encoding;
+ this_inf->lsda_encoding = cie->lsda_encoding;
+ sec_info->count++;
+ }
+ BFD_ASSERT (sec_info->count == num_entries);
+ BFD_ASSERT (cie_count == num_cies);
+
+ elf_section_data (sec)->sec_info = sec_info;
+ sec->sec_info_type = SEC_INFO_TYPE_EH_FRAME;
+ if (!bfd_link_relocatable (info))
+ {
+ /* Keep info for merging cies. */
+ sec_info->cies = local_cies;
+ local_cies = NULL;
+ }
+ goto success;
+
+ free_no_table:
+ (*info->callbacks->einfo)
+ (_("%P: error in %B(%A); no .eh_frame_hdr table will be created.\n"),
+ abfd, sec);
+ hdr_info->u.dwarf.table = FALSE;
+ if (sec_info)
+ free (sec_info);
+ success:
+ if (ehbuf)
+ free (ehbuf);
+ if (local_cies)
+ free (local_cies);
+#undef REQUIRE
+}
+
+/* Order eh_frame_hdr entries by the VMA of their text section. */
+
+static int
+cmp_eh_frame_hdr (const void *a, const void *b)
+{
+ bfd_vma text_a;
+ bfd_vma text_b;
+ asection *sec;
+
+ sec = *(asection *const *)a;
+ sec = (asection *) elf_section_data (sec)->sec_info;
+ text_a = sec->output_section->vma + sec->output_offset;
+ sec = *(asection *const *)b;
+ sec = (asection *) elf_section_data (sec)->sec_info;
+ text_b = sec->output_section->vma + sec->output_offset;
+
+ if (text_a < text_b)
+ return -1;
+ return text_a > text_b;
+
+}
+
+/* Add space for a CANTUNWIND terminator to SEC if the text sections
+ referenced by it and NEXT are not contiguous, or NEXT is NULL. */
+
+static void
+add_eh_frame_hdr_terminator (asection *sec,
+ asection *next)
+{
+ bfd_vma end;
+ bfd_vma next_start;
+ asection *text_sec;
+
+ if (next)
+ {
+ /* See if there is a gap (presumably a text section without unwind info)
+ between these two entries. */
+ text_sec = (asection *) elf_section_data (sec)->sec_info;
+ end = text_sec->output_section->vma + text_sec->output_offset
+ + text_sec->size;
+ text_sec = (asection *) elf_section_data (next)->sec_info;
+ next_start = text_sec->output_section->vma + text_sec->output_offset;
+ if (end == next_start)
+ return;
+ }
+
+ /* Add space for a CANTUNWIND terminator. */
+ if (!sec->rawsize)
+ sec->rawsize = sec->size;
+
+ bfd_set_section_size (sec->owner, sec, sec->size + 8);
+}
+
+/* Finish a pass over all .eh_frame_entry sections. */
+
+bfd_boolean
+_bfd_elf_end_eh_frame_parsing (struct bfd_link_info *info)
+{
+ struct eh_frame_hdr_info *hdr_info;
+ unsigned int i;
+
+ hdr_info = &elf_hash_table (info)->eh_info;
+
+ if (info->eh_frame_hdr_type != COMPACT_EH_HDR
+ || hdr_info->array_count == 0)
+ return FALSE;
+
+ bfd_elf_discard_eh_frame_entry (hdr_info);
+
+ qsort (hdr_info->u.compact.entries, hdr_info->array_count,
+ sizeof (asection *), cmp_eh_frame_hdr);
+
+ for (i = 0; i < hdr_info->array_count - 1; i++)
+ {
+ add_eh_frame_hdr_terminator (hdr_info->u.compact.entries[i],
+ hdr_info->u.compact.entries[i + 1]);
+ }
+
+ /* Add a CANTUNWIND terminator after the last entry. */
+ add_eh_frame_hdr_terminator (hdr_info->u.compact.entries[i], NULL);
+ return TRUE;
+}
+
+/* Mark all relocations against CIE or FDE ENT, which occurs in
+ .eh_frame section SEC. COOKIE describes the relocations in SEC;
+ its "rel" field can be changed freely. */
+
+static bfd_boolean
+mark_entry (struct bfd_link_info *info, asection *sec,
+ struct eh_cie_fde *ent, elf_gc_mark_hook_fn gc_mark_hook,
+ struct elf_reloc_cookie *cookie)
+{
+ /* FIXME: octets_per_byte. */
+ for (cookie->rel = cookie->rels + ent->reloc_index;
+ cookie->rel < cookie->relend
+ && cookie->rel->r_offset < ent->offset + ent->size;
+ cookie->rel++)
+ if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, cookie))
+ return FALSE;
+
+ return TRUE;
+}
+
+/* Mark all the relocations against FDEs that relate to code in input
+ section SEC. The FDEs belong to .eh_frame section EH_FRAME, whose
+ relocations are described by COOKIE. */
+
+bfd_boolean
+_bfd_elf_gc_mark_fdes (struct bfd_link_info *info, asection *sec,
+ asection *eh_frame, elf_gc_mark_hook_fn gc_mark_hook,
+ struct elf_reloc_cookie *cookie)
+{
+ struct eh_cie_fde *fde, *cie;
+
+ for (fde = elf_fde_list (sec); fde; fde = fde->u.fde.next_for_section)
+ {
+ if (!mark_entry (info, eh_frame, fde, gc_mark_hook, cookie))
+ return FALSE;
+
+ /* At this stage, all cie_inf fields point to local CIEs, so we
+ can use the same cookie to refer to them. */
+ cie = fde->u.fde.cie_inf;
+ if (cie != NULL && !cie->u.cie.gc_mark)
+ {
+ cie->u.cie.gc_mark = 1;
+ if (!mark_entry (info, eh_frame, cie, gc_mark_hook, cookie))
+ return FALSE;
+ }
+ }
+ return TRUE;
+}
+
+/* Input section SEC of ABFD is an .eh_frame section that contains the
+ CIE described by CIE_INF. Return a version of CIE_INF that is going
+ to be kept in the output, adding CIE_INF to the output if necessary.
+
+ HDR_INFO is the .eh_frame_hdr information and COOKIE describes the
+ relocations in REL. */
+
+static struct eh_cie_fde *
+find_merged_cie (bfd *abfd, struct bfd_link_info *info, asection *sec,
+ struct eh_frame_hdr_info *hdr_info,
+ struct elf_reloc_cookie *cookie,
+ struct eh_cie_fde *cie_inf)
+{
+ unsigned long r_symndx;
+ struct cie *cie, *new_cie;
+ Elf_Internal_Rela *rel;
+ void **loc;
+
+ /* Use CIE_INF if we have already decided to keep it. */
+ if (!cie_inf->removed)
+ return cie_inf;
+
+ /* If we have merged CIE_INF with another CIE, use that CIE instead. */
+ if (cie_inf->u.cie.merged)
+ return cie_inf->u.cie.u.merged_with;
+
+ cie = cie_inf->u.cie.u.full_cie;
+
+ /* Assume we will need to keep CIE_INF. */
+ cie_inf->removed = 0;
+ cie_inf->u.cie.u.sec = sec;
+
+ /* If we are not merging CIEs, use CIE_INF. */
+ if (cie == NULL)
+ return cie_inf;
+
+ if (cie->per_encoding != DW_EH_PE_omit)
+ {
+ bfd_boolean per_binds_local;
+
+ /* Work out the address of personality routine, or at least
+ enough info that we could calculate the address had we made a
+ final section layout. The symbol on the reloc is enough,
+ either the hash for a global, or (bfd id, index) pair for a
+ local. The assumption here is that no one uses addends on
+ the reloc. */
+ rel = cookie->rels + cie->personality.reloc_index;
+ memset (&cie->personality, 0, sizeof (cie->personality));
+#ifdef BFD64
+ if (elf_elfheader (abfd)->e_ident[EI_CLASS] == ELFCLASS64)
+ r_symndx = ELF64_R_SYM (rel->r_info);
+ else
+#endif
+ r_symndx = ELF32_R_SYM (rel->r_info);
+ if (r_symndx >= cookie->locsymcount
+ || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
+ {
+ struct elf_link_hash_entry *h;
+
+ r_symndx -= cookie->extsymoff;
+ h = cookie->sym_hashes[r_symndx];