+\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 = ((struct objfile_per_bfd_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
+ = ((struct objfile_per_bfd_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 ((struct 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
+ = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack, name,
+ strlen (name));
+ objfile->per_bfd->language_of_main = lang;
+}
+
+/* Helper structure to map blocks to static link properties in hash tables. */
+
+struct static_link_htab_entry
+{
+ const struct block *block;
+ const struct dynamic_prop *static_link;
+};
+
+/* Return a hash code for struct static_link_htab_entry *P. */
+
+static hashval_t
+static_link_htab_entry_hash (const void *p)
+{
+ const struct static_link_htab_entry *e
+ = (const struct static_link_htab_entry *) p;
+
+ return htab_hash_pointer (e->block);
+}
+
+/* Return whether P1 an P2 (pointers to struct static_link_htab_entry) are
+ mappings for the same block. */
+
+static int
+static_link_htab_entry_eq (const void *p1, const void *p2)
+{
+ const struct static_link_htab_entry *e1
+ = (const struct static_link_htab_entry *) p1;
+ const struct static_link_htab_entry *e2
+ = (const struct static_link_htab_entry *) p2;
+
+ return e1->block == e2->block;
+}
+
+/* Register STATIC_LINK as the static link for BLOCK, which is part of OBJFILE.
+ Must not be called more than once for each BLOCK. */
+
+void
+objfile_register_static_link (struct objfile *objfile,
+ const struct block *block,
+ const struct dynamic_prop *static_link)
+{
+ void **slot;
+ struct static_link_htab_entry lookup_entry;
+ struct static_link_htab_entry *entry;
+
+ if (objfile->static_links == NULL)
+ objfile->static_links = htab_create_alloc
+ (1, &static_link_htab_entry_hash, static_link_htab_entry_eq, NULL,
+ xcalloc, xfree);
+
+ /* Create a slot for the mapping, make sure it's the first mapping for this
+ block and then create the mapping itself. */
+ lookup_entry.block = block;
+ slot = htab_find_slot (objfile->static_links, &lookup_entry, INSERT);
+ gdb_assert (*slot == NULL);
+
+ entry = (struct static_link_htab_entry *) obstack_alloc
+ (&objfile->objfile_obstack, sizeof (*entry));
+ entry->block = block;
+ entry->static_link = static_link;
+ *slot = (void *) entry;
+}
+
+/* Look for a static link for BLOCK, which is part of OBJFILE. Return NULL if
+ none was found. */
+
+const struct dynamic_prop *
+objfile_lookup_static_link (struct objfile *objfile,
+ const struct block *block)
+{
+ struct static_link_htab_entry *entry;
+ struct static_link_htab_entry lookup_entry;
+
+ if (objfile->static_links == NULL)
+ return NULL;
+ lookup_entry.block = block;
+ entry
+ = (struct static_link_htab_entry *) htab_find (objfile->static_links,
+ &lookup_entry);
+ if (entry == NULL)
+ return NULL;
+
+ gdb_assert (entry->block == block);
+ return entry->static_link;
+}
+
+\f
+