+/* 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];
+
+ while (h->root.type == bfd_link_hash_indirect
+ || h->root.type == bfd_link_hash_warning)
+ h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+ cie->personality.h = h;
+ per_binds_local = SYMBOL_REFERENCES_LOCAL (info, h);
+ }
+ else
+ {
+ Elf_Internal_Sym *sym;
+ asection *sym_sec;
+
+ sym = &cookie->locsyms[r_symndx];
+ sym_sec = bfd_section_from_elf_index (abfd, sym->st_shndx);
+ if (sym_sec == NULL)
+ return cie_inf;
+
+ if (sym_sec->kept_section != NULL)
+ sym_sec = sym_sec->kept_section;
+ if (sym_sec->output_section == NULL)
+ return cie_inf;
+
+ cie->local_personality = 1;
+ cie->personality.sym.bfd_id = abfd->id;
+ cie->personality.sym.index = r_symndx;
+ per_binds_local = TRUE;
+ }
+
+ if (per_binds_local
+ && bfd_link_pic (info)
+ && (cie->per_encoding & 0x70) == DW_EH_PE_absptr
+ && (get_elf_backend_data (abfd)
+ ->elf_backend_can_make_relative_eh_frame (abfd, info, sec)))
+ {
+ cie_inf->u.cie.make_per_encoding_relative = 1;
+ cie_inf->u.cie.per_encoding_relative = 1;
+ }
+ }
+
+ /* See if we can merge this CIE with an earlier one. */
+ cie_compute_hash (cie);
+ if (hdr_info->u.dwarf.cies == NULL)
+ {
+ hdr_info->u.dwarf.cies = htab_try_create (1, cie_hash, cie_eq, free);
+ if (hdr_info->u.dwarf.cies == NULL)
+ return cie_inf;
+ }
+ loc = htab_find_slot_with_hash (hdr_info->u.dwarf.cies, cie,
+ cie->hash, INSERT);
+ if (loc == NULL)
+ return cie_inf;
+
+ new_cie = (struct cie *) *loc;
+ if (new_cie == NULL)
+ {
+ /* Keep CIE_INF and record it in the hash table. */
+ new_cie = (struct cie *) malloc (sizeof (struct cie));
+ if (new_cie == NULL)
+ return cie_inf;
+
+ memcpy (new_cie, cie, sizeof (struct cie));
+ *loc = new_cie;
+ }
+ else
+ {
+ /* Merge CIE_INF with NEW_CIE->CIE_INF. */
+ cie_inf->removed = 1;
+ cie_inf->u.cie.merged = 1;
+ cie_inf->u.cie.u.merged_with = new_cie->cie_inf;
+ if (cie_inf->u.cie.make_lsda_relative)
+ new_cie->cie_inf->u.cie.make_lsda_relative = 1;
+ }
+ return new_cie->cie_inf;
+}
+
+/* For a given OFFSET in SEC, return the delta to the new location
+ after .eh_frame editing. */
+
+static bfd_signed_vma
+offset_adjust (bfd_vma offset, const asection *sec)
+{
+ struct eh_frame_sec_info *sec_info
+ = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
+ unsigned int lo, hi, mid;
+ struct eh_cie_fde *ent = NULL;
+ bfd_signed_vma delta;
+
+ lo = 0;
+ hi = sec_info->count;
+ if (hi == 0)
+ return 0;
+
+ while (lo < hi)
+ {
+ mid = (lo + hi) / 2;
+ ent = &sec_info->entry[mid];
+ if (offset < ent->offset)
+ hi = mid;
+ else if (mid + 1 >= hi)
+ break;
+ else if (offset >= ent[1].offset)
+ lo = mid + 1;
+ else
+ break;
+ }
+
+ if (!ent->removed)
+ delta = (bfd_vma) ent->new_offset - (bfd_vma) ent->offset;
+ else if (ent->cie && ent->u.cie.merged)
+ {
+ struct eh_cie_fde *cie = ent->u.cie.u.merged_with;
+ delta = ((bfd_vma) cie->new_offset + cie->u.cie.u.sec->output_offset
+ - (bfd_vma) ent->offset - sec->output_offset);
+ }
+ else
+ {
+ /* Is putting the symbol on the next entry best for a deleted
+ CIE/FDE? */
+ struct eh_cie_fde *last = sec_info->entry + sec_info->count;
+ delta = ((bfd_vma) next_cie_fde_offset (ent, last, sec)
+ - (bfd_vma) ent->offset);
+ return delta;
+ }
+
+ /* Account for editing within this CIE/FDE. */
+ offset -= ent->offset;
+ if (ent->cie)
+ {
+ unsigned int extra
+ = ent->add_augmentation_size + ent->u.cie.add_fde_encoding;
+ if (extra == 0
+ || offset <= 9u + ent->u.cie.aug_str_len)
+ return delta;
+ delta += extra;
+ if (offset <= 9u + ent->u.cie.aug_str_len + ent->u.cie.aug_data_len)
+ return delta;
+ delta += extra;
+ }
+ else
+ {
+ unsigned int ptr_size, width, extra = ent->add_augmentation_size;
+ if (offset <= 12 || extra == 0)
+ return delta;
+ ptr_size = (get_elf_backend_data (sec->owner)
+ ->elf_backend_eh_frame_address_size (sec->owner, sec));
+ width = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
+ if (offset <= 8 + 2 * width)
+ return delta;
+ delta += extra;
+ }
+
+ return delta;
+}
+
+/* Adjust a global symbol defined in .eh_frame, so that it stays
+ relative to its original CIE/FDE. It is assumed that a symbol
+ defined at the beginning of a CIE/FDE belongs to that CIE/FDE
+ rather than marking the end of the previous CIE/FDE. This matters
+ when a CIE is merged with a previous CIE, since the symbol is
+ moved to the merged CIE. */
+
+bfd_boolean
+_bfd_elf_adjust_eh_frame_global_symbol (struct elf_link_hash_entry *h,
+ void *arg ATTRIBUTE_UNUSED)
+{
+ asection *sym_sec;
+ bfd_signed_vma delta;
+
+ if (h->root.type != bfd_link_hash_defined
+ && h->root.type != bfd_link_hash_defweak)
+ return TRUE;
+
+ sym_sec = h->root.u.def.section;
+ if (sym_sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME
+ || elf_section_data (sym_sec)->sec_info == NULL)
+ return TRUE;
+
+ delta = offset_adjust (h->root.u.def.value, sym_sec);
+ h->root.u.def.value += delta;
+
+ return TRUE;
+}
+
+/* The same for all local symbols defined in .eh_frame. Returns true
+ if any symbol was changed. */
+
+static int
+adjust_eh_frame_local_symbols (const asection *sec,
+ struct elf_reloc_cookie *cookie)
+{
+ unsigned int shndx;
+ Elf_Internal_Sym *sym;
+ Elf_Internal_Sym *end_sym;
+ int adjusted = 0;
+
+ shndx = elf_section_data (sec)->this_idx;
+ end_sym = cookie->locsyms + cookie->locsymcount;
+ for (sym = cookie->locsyms + 1; sym < end_sym; ++sym)
+ if (sym->st_info <= ELF_ST_INFO (STB_LOCAL, STT_OBJECT)
+ && sym->st_shndx == shndx)
+ {
+ bfd_signed_vma delta = offset_adjust (sym->st_value, sec);
+
+ if (delta != 0)
+ {
+ adjusted = 1;
+ sym->st_value += delta;
+ }
+ }
+ return adjusted;
+}
+
+/* This function is called for each input file before the .eh_frame
+ section is relocated. It discards duplicate CIEs and FDEs for discarded
+ functions. The function returns TRUE iff any entries have been
+ deleted. */
+
+bfd_boolean
+_bfd_elf_discard_section_eh_frame
+ (bfd *abfd, struct bfd_link_info *info, asection *sec,
+ bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
+ struct elf_reloc_cookie *cookie)
+{
+ struct eh_cie_fde *ent;
+ struct eh_frame_sec_info *sec_info;
+ struct eh_frame_hdr_info *hdr_info;
+ unsigned int ptr_size, offset, eh_alignment;
+ int changed;
+
+ if (sec->sec_info_type != SEC_INFO_TYPE_EH_FRAME)
+ return FALSE;
+
+ sec_info = (struct eh_frame_sec_info *) elf_section_data (sec)->sec_info;
+ if (sec_info == NULL)
+ return FALSE;
+
+ ptr_size = (get_elf_backend_data (sec->owner)
+ ->elf_backend_eh_frame_address_size (sec->owner, sec));
+
+ hdr_info = &elf_hash_table (info)->eh_info;
+ for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
+ if (ent->size == 4)
+ /* There should only be one zero terminator, on the last input
+ file supplying .eh_frame (crtend.o). Remove any others. */
+ ent->removed = sec->map_head.s != NULL;
+ else if (!ent->cie && ent->u.fde.cie_inf != NULL)
+ {
+ bfd_boolean keep;
+ if ((sec->flags & SEC_LINKER_CREATED) != 0 && cookie->rels == NULL)
+ {
+ unsigned int width
+ = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
+ bfd_vma value
+ = read_value (abfd, sec->contents + ent->offset + 8 + width,
+ width, get_DW_EH_PE_signed (ent->fde_encoding));
+ keep = value != 0;
+ }
+ else
+ {
+ cookie->rel = cookie->rels + ent->reloc_index;
+ /* FIXME: octets_per_byte. */
+ BFD_ASSERT (cookie->rel < cookie->relend
+ && cookie->rel->r_offset == ent->offset + 8);
+ keep = !(*reloc_symbol_deleted_p) (ent->offset + 8, cookie);
+ }
+ if (keep)
+ {
+ if (bfd_link_pic (info)
+ && (((ent->fde_encoding & 0x70) == DW_EH_PE_absptr
+ && ent->make_relative == 0)
+ || (ent->fde_encoding & 0x70) == DW_EH_PE_aligned))
+ {
+ static int num_warnings_issued = 0;
+
+ /* If a shared library uses absolute pointers
+ which we cannot turn into PC relative,
+ don't create the binary search table,
+ since it is affected by runtime relocations. */
+ hdr_info->u.dwarf.table = FALSE;
+ /* Only warn if --eh-frame-hdr was specified. */
+ if (info->eh_frame_hdr_type != 0)
+ {
+ if (num_warnings_issued < 10)
+ {
+ _bfd_error_handler
+ /* xgettext:c-format */
+ (_("FDE encoding in %pB(%pA) prevents .eh_frame_hdr"
+ " table being created"), abfd, sec);
+ num_warnings_issued ++;
+ }
+ else if (num_warnings_issued == 10)
+ {
+ _bfd_error_handler
+ (_("further warnings about FDE encoding preventing .eh_frame_hdr generation dropped"));
+ num_warnings_issued ++;
+ }
+ }
+ }
+ ent->removed = 0;
+ hdr_info->u.dwarf.fde_count++;
+ ent->u.fde.cie_inf = find_merged_cie (abfd, info, sec, hdr_info,
+ cookie, ent->u.fde.cie_inf);
+ }
+ }
+
+ free (sec_info->cies);
+ sec_info->cies = NULL;
+
+ /* It may be that some .eh_frame input section has greater alignment
+ than other .eh_frame sections. In that case we run the risk of
+ padding with zeros before that section, which would be seen as a
+ zero terminator. Alignment padding must be added *inside* the
+ last FDE instead. For other FDEs we align according to their
+ encoding, in order to align FDE address range entries naturally. */
+ offset = 0;
+ changed = 0;
+ for (ent = sec_info->entry; ent < sec_info->entry + sec_info->count; ++ent)
+ if (!ent->removed)
+ {
+ eh_alignment = 4;
+ if (ent->size == 4)
+ ;
+ else if (ent->cie)
+ {
+ if (ent->u.cie.per_encoding_aligned8)
+ eh_alignment = 8;
+ }
+ else
+ {
+ eh_alignment = get_DW_EH_PE_width (ent->fde_encoding, ptr_size);
+ if (eh_alignment < 4)
+ eh_alignment = 4;
+ }
+ offset = (offset + eh_alignment - 1) & -eh_alignment;
+ ent->new_offset = offset;
+ if (ent->new_offset != ent->offset)
+ changed = 1;
+ offset += size_of_output_cie_fde (ent);
+ }
+
+ eh_alignment = 4;
+ offset = (offset + eh_alignment - 1) & -eh_alignment;
+ sec->rawsize = sec->size;
+ sec->size = offset;
+ if (sec->size != sec->rawsize)
+ changed = 1;
+
+ if (changed && adjust_eh_frame_local_symbols (sec, cookie))
+ {
+ Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+ symtab_hdr->contents = (unsigned char *) cookie->locsyms;
+ }
+ return changed;
+}
+
+/* This function is called for .eh_frame_hdr section after
+ _bfd_elf_discard_section_eh_frame has been called on all .eh_frame
+ input sections. It finalizes the size of .eh_frame_hdr section. */
+
+bfd_boolean
+_bfd_elf_discard_section_eh_frame_hdr (bfd *abfd, struct bfd_link_info *info)
+{
+ struct elf_link_hash_table *htab;
+ struct eh_frame_hdr_info *hdr_info;
+ asection *sec;
+
+ htab = elf_hash_table (info);
+ hdr_info = &htab->eh_info;
+
+ if (!hdr_info->frame_hdr_is_compact && hdr_info->u.dwarf.cies != NULL)
+ {
+ htab_delete (hdr_info->u.dwarf.cies);
+ hdr_info->u.dwarf.cies = NULL;
+ }
+
+ sec = hdr_info->hdr_sec;
+ if (sec == NULL)
+ return FALSE;
+
+ if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
+ {
+ /* For compact frames we only add the header. The actual table comes
+ from the .eh_frame_entry sections. */
+ sec->size = 8;
+ }
+ else
+ {
+ sec->size = EH_FRAME_HDR_SIZE;
+ if (hdr_info->u.dwarf.table)
+ sec->size += 4 + hdr_info->u.dwarf.fde_count * 8;
+ }
+
+ elf_eh_frame_hdr (abfd) = sec;
+ return TRUE;
+}
+
+/* Return true if there is at least one non-empty .eh_frame section in
+ input files. Can only be called after ld has mapped input to
+ output sections, and before sections are stripped. */
+
+bfd_boolean
+_bfd_elf_eh_frame_present (struct bfd_link_info *info)
+{
+ asection *eh = bfd_get_section_by_name (info->output_bfd, ".eh_frame");
+
+ if (eh == NULL)
+ return FALSE;
+
+ /* Count only sections which have at least a single CIE or FDE.
+ There cannot be any CIE or FDE <= 8 bytes. */
+ for (eh = eh->map_head.s; eh != NULL; eh = eh->map_head.s)
+ if (eh->size > 8)
+ return TRUE;
+
+ return FALSE;
+}
+
+/* Return true if there is at least one .eh_frame_entry section in
+ input files. */
+
+bfd_boolean
+_bfd_elf_eh_frame_entry_present (struct bfd_link_info *info)
+{
+ asection *o;
+ bfd *abfd;
+
+ for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next)
+ {
+ for (o = abfd->sections; o; o = o->next)
+ {
+ const char *name = bfd_section_name (o);
+
+ if (strcmp (name, ".eh_frame_entry")
+ && !bfd_is_abs_section (o->output_section))
+ return TRUE;
+ }
+ }
+ return FALSE;
+}
+
+/* This function is called from size_dynamic_sections.
+ It needs to decide whether .eh_frame_hdr should be output or not,
+ because when the dynamic symbol table has been sized it is too late
+ to strip sections. */
+
+bfd_boolean
+_bfd_elf_maybe_strip_eh_frame_hdr (struct bfd_link_info *info)
+{
+ struct elf_link_hash_table *htab;
+ struct eh_frame_hdr_info *hdr_info;
+ struct bfd_link_hash_entry *bh = NULL;
+ struct elf_link_hash_entry *h;
+
+ htab = elf_hash_table (info);
+ hdr_info = &htab->eh_info;
+ if (hdr_info->hdr_sec == NULL)
+ return TRUE;
+
+ if (bfd_is_abs_section (hdr_info->hdr_sec->output_section)
+ || info->eh_frame_hdr_type == 0
+ || (info->eh_frame_hdr_type == DWARF2_EH_HDR
+ && !_bfd_elf_eh_frame_present (info))
+ || (info->eh_frame_hdr_type == COMPACT_EH_HDR
+ && !_bfd_elf_eh_frame_entry_present (info)))
+ {
+ hdr_info->hdr_sec->flags |= SEC_EXCLUDE;
+ hdr_info->hdr_sec = NULL;
+ return TRUE;
+ }
+
+ /* Add a hidden symbol so that systems without access to PHDRs can
+ find the table. */
+ if (! (_bfd_generic_link_add_one_symbol
+ (info, info->output_bfd, "__GNU_EH_FRAME_HDR", BSF_LOCAL,
+ hdr_info->hdr_sec, 0, NULL, FALSE, FALSE, &bh)))
+ return FALSE;
+
+ h = (struct elf_link_hash_entry *) bh;
+ h->def_regular = 1;
+ h->other = STV_HIDDEN;
+ get_elf_backend_data
+ (info->output_bfd)->elf_backend_hide_symbol (info, h, TRUE);
+
+ if (!hdr_info->frame_hdr_is_compact)
+ hdr_info->u.dwarf.table = TRUE;
+ return TRUE;
+}
+
+/* Adjust an address in the .eh_frame section. Given OFFSET within
+ SEC, this returns the new offset in the adjusted .eh_frame section,