+/* Reorder .eh_frame_entry sections to match the associated text sections.
+ This routine is called during the final linking step, just before writing
+ the contents. At this stage, sections in the eh_frame_hdr_info are already
+ sorted in order of increasing text section address and so we simply need
+ to make the .eh_frame_entrys follow that same order. Note that it is
+ invalid for a linker script to try to force a particular order of
+ .eh_frame_entry sections. */
+
+bfd_boolean
+_bfd_elf_fixup_eh_frame_hdr (struct bfd_link_info *info)
+{
+ asection *sec = NULL;
+ asection *osec;
+ struct eh_frame_hdr_info *hdr_info;
+ unsigned int i;
+ bfd_vma offset;
+ struct bfd_link_order *p;
+
+ hdr_info = &elf_hash_table (info)->eh_info;
+
+ if (hdr_info->hdr_sec == NULL
+ || info->eh_frame_hdr_type != COMPACT_EH_HDR
+ || hdr_info->array_count == 0)
+ return TRUE;
+
+ /* Change section output offsets to be in text section order. */
+ offset = 8;
+ osec = hdr_info->u.compact.entries[0]->output_section;
+ for (i = 0; i < hdr_info->array_count; i++)
+ {
+ sec = hdr_info->u.compact.entries[i];
+ if (sec->output_section != osec)
+ {
+ (*_bfd_error_handler)
+ (_("Invalid output section for .eh_frame_entry: %s"),
+ sec->output_section->name);
+ return FALSE;
+ }
+ sec->output_offset = offset;
+ offset += sec->size;
+ }
+
+
+ /* Fix the link_order to match. */
+ for (p = sec->output_section->map_head.link_order; p != NULL; p = p->next)
+ {
+ if (p->type != bfd_indirect_link_order)
+ abort();
+
+ p->offset = p->u.indirect.section->output_offset;
+ if (p->next != NULL)
+ i--;
+ }
+
+ if (i != 0)
+ {
+ (*_bfd_error_handler)
+ (_("Invalid contents in %s section"), osec->name);
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
+/* The .eh_frame_hdr format for Compact EH frames:
+ ubyte version (2)
+ ubyte eh_ref_enc (DW_EH_PE_* encoding of typinfo references)
+ uint32_t count (Number of entries in table)
+ [array from .eh_frame_entry sections] */
+
+static bfd_boolean
+write_compact_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;
+ const struct elf_backend_data *bed;
+ bfd_vma count;
+ bfd_byte contents[8];
+ unsigned int i;
+
+ htab = elf_hash_table (info);
+ hdr_info = &htab->eh_info;
+ sec = hdr_info->hdr_sec;
+
+ if (sec->size != 8)
+ abort();
+
+ for (i = 0; i < sizeof (contents); i++)
+ contents[i] = 0;
+
+ contents[0] = COMPACT_EH_HDR;
+ bed = get_elf_backend_data (abfd);
+
+ BFD_ASSERT (bed->compact_eh_encoding);
+ contents[1] = (*bed->compact_eh_encoding) (info);
+
+ count = (sec->output_section->size - 8) / 8;
+ bfd_put_32 (abfd, count, contents + 4);
+ return bfd_set_section_contents (abfd, sec->output_section, contents,
+ (file_ptr) sec->output_offset, sec->size);
+}
+
+/* The .eh_frame_hdr format for DWARF frames:
+