+struct bfd_preserve
+{
+ void *marker;
+ void *tdata;
+ flagword flags;
+ const struct bfd_arch_info *arch_info;
+ struct bfd_section *sections;
+ struct bfd_section *section_last;
+ unsigned int section_count;
+ struct bfd_hash_table section_htab;
+};
+
+/* When testing an object for compatibility with a particular target
+ back-end, the back-end object_p function needs to set up certain
+ fields in the bfd on successfully recognizing the object. This
+ typically happens in a piecemeal fashion, with failures possible at
+ many points. On failure, the bfd is supposed to be restored to its
+ initial state, which is virtually impossible. However, restoring a
+ subset of the bfd state works in practice. This function stores
+ the subset. */
+
+static bfd_boolean
+bfd_preserve_save (bfd *abfd, struct bfd_preserve *preserve)
+{
+ preserve->tdata = abfd->tdata.any;
+ preserve->arch_info = abfd->arch_info;
+ preserve->flags = abfd->flags;
+ preserve->sections = abfd->sections;
+ preserve->section_last = abfd->section_last;
+ preserve->section_count = abfd->section_count;
+ preserve->section_htab = abfd->section_htab;
+ preserve->marker = bfd_alloc (abfd, 1);
+ if (preserve->marker == NULL)
+ return FALSE;
+
+ return bfd_hash_table_init (&abfd->section_htab, bfd_section_hash_newfunc,
+ sizeof (struct section_hash_entry));
+}
+
+/* Clear out a subset of BFD state. */
+
+static void
+bfd_reinit (bfd *abfd)
+{
+ abfd->tdata.any = NULL;
+ abfd->arch_info = &bfd_default_arch_struct;
+ abfd->flags &= BFD_FLAGS_SAVED;
+ bfd_section_list_clear (abfd);
+}
+
+/* Restores bfd state saved by bfd_preserve_save. */
+
+static void
+bfd_preserve_restore (bfd *abfd, struct bfd_preserve *preserve)
+{
+ bfd_hash_table_free (&abfd->section_htab);
+
+ abfd->tdata.any = preserve->tdata;
+ abfd->arch_info = preserve->arch_info;
+ abfd->flags = preserve->flags;
+ abfd->section_htab = preserve->section_htab;
+ abfd->sections = preserve->sections;
+ abfd->section_last = preserve->section_last;
+ abfd->section_count = preserve->section_count;
+
+ /* bfd_release frees all memory more recently bfd_alloc'd than
+ its arg, as well as its arg. */
+ bfd_release (abfd, preserve->marker);
+ preserve->marker = NULL;
+}
+
+/* Called when the bfd state saved by bfd_preserve_save is no longer
+ needed. */
+
+static void
+bfd_preserve_finish (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_preserve *preserve)
+{
+ /* It would be nice to be able to free more memory here, eg. old
+ tdata, but that's not possible since these blocks are sitting
+ inside bfd_alloc'd memory. The section hash is on a separate
+ objalloc. */
+ bfd_hash_table_free (&preserve->section_htab);
+ preserve->marker = NULL;
+}
+