+\f
+
+/* Per-BFD data key. */
+
+static const struct bfd_data *objfiles_bfd_data;
+
+/* Create the per-BFD storage object for OBJFILE. If ABFD is not
+ NULL, and it already has a per-BFD storage object, use that.
+ Otherwise, allocate a new per-BFD storage object. If ABFD is not
+ NULL, the object is allocated on the BFD; otherwise it is allocated
+ on OBJFILE's obstack. Note that it is not safe to call this
+ multiple times for a given OBJFILE -- it can only be called when
+ allocating or re-initializing OBJFILE. */
+
+static struct objfile_per_bfd_storage *
+get_objfile_bfd_data (struct objfile *objfile, struct bfd *abfd)
+{
+ struct objfile_per_bfd_storage *storage = NULL;
+
+ if (abfd != NULL)
+ storage = bfd_data (abfd, objfiles_bfd_data);
+
+ if (storage == NULL)
+ {
+ /* If the object requires gdb to do relocations, we simply fall
+ back to not sharing data across users. These cases are rare
+ enough that this seems reasonable. */
+ if (abfd != NULL && !gdb_bfd_requires_relocations (abfd))
+ {
+ storage = bfd_zalloc (abfd, sizeof (struct objfile_per_bfd_storage));
+ set_bfd_data (abfd, objfiles_bfd_data, storage);
+ }
+ else
+ storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
+ struct objfile_per_bfd_storage);
+
+ /* Look up the gdbarch associated with the BFD. */
+ if (abfd != NULL)
+ storage->gdbarch = gdbarch_from_bfd (abfd);
+
+ obstack_init (&storage->storage_obstack);
+ storage->filename_cache = bcache_xmalloc (NULL, NULL);
+ storage->macro_cache = bcache_xmalloc (NULL, NULL);
+ storage->language_of_main = language_unknown;
+ }
+
+ return storage;
+}
+
+/* Free STORAGE. */
+
+static void
+free_objfile_per_bfd_storage (struct objfile_per_bfd_storage *storage)
+{
+ bcache_xfree (storage->filename_cache);
+ bcache_xfree (storage->macro_cache);
+ if (storage->demangled_names_hash)
+ htab_delete (storage->demangled_names_hash);
+ obstack_free (&storage->storage_obstack, 0);
+}
+
+/* A wrapper for free_objfile_per_bfd_storage that can be passed as a
+ cleanup function to the BFD registry. */
+
+static void
+objfile_bfd_data_free (struct bfd *unused, void *d)
+{
+ free_objfile_per_bfd_storage (d);
+}
+
+/* See objfiles.h. */
+
+void
+set_objfile_per_bfd (struct objfile *objfile)
+{
+ objfile->per_bfd = get_objfile_bfd_data (objfile, objfile->obfd);
+}
+
+/* Set the objfile's per-BFD notion of the "main" name and
+ language. */
+
+void
+set_objfile_main_name (struct objfile *objfile,
+ const char *name, enum language lang)
+{
+ if (objfile->per_bfd->name_of_main == NULL
+ || strcmp (objfile->per_bfd->name_of_main, name) != 0)
+ objfile->per_bfd->name_of_main
+ = obstack_copy0 (&objfile->per_bfd->storage_obstack, name, strlen (name));
+ objfile->per_bfd->language_of_main = lang;
+}
+
+\f
+