+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;
+ unsigned int section_id;
+ struct bfd_hash_table section_htab;
+ const struct bfd_build_id *build_id;
+ bfd_cleanup cleanup;
+};
+
+/* 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,
+ bfd_cleanup cleanup)
+{
+ 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_id = _bfd_section_id;
+ preserve->section_htab = abfd->section_htab;
+ preserve->marker = bfd_alloc (abfd, 1);
+ preserve->build_id = abfd->build_id;
+ preserve->cleanup = cleanup;
+ 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, unsigned int section_id, bfd_cleanup cleanup)
+{
+ _bfd_section_id = section_id;
+ if (cleanup)
+ cleanup (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 bfd_cleanup
+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_section_id = preserve->section_id;
+ abfd->build_id = preserve->build_id;
+
+ /* 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;
+ return preserve->cleanup;
+}
+
+/* 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)
+{
+ if (preserve->cleanup)
+ {
+ /* Run the cleanup, assuming that all it will need is the
+ tdata at the time the cleanup was returned. */
+ void *tdata = abfd->tdata.any;
+ abfd->tdata.any = preserve->tdata;
+ preserve->cleanup (abfd);
+ abfd->tdata.any = tdata;
+ }
+ /* 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;
+}
+