+/* Backward compatibility. */
+ctf_dict_t *
+ctf_arc_open_by_name (const ctf_archive_t *arc, const char *name,
+ int *errp)
+{
+ return ctf_dict_open (arc, name, errp);
+}
+
+ctf_dict_t *
+ctf_arc_open_by_name_sections (const ctf_archive_t *arc,
+ const ctf_sect_t *symsect,
+ const ctf_sect_t *strsect,
+ const char *name,
+ int *errp)
+{
+ return ctf_dict_open_sections (arc, symsect, strsect, name, errp);
+}
+
+/* Import the parent into a ctf archive, if this is a child, the parent is not
+ already set, and a suitable archive member exists. No error is raised if
+ this is not possible: this is just a best-effort helper operation to give
+ people useful dicts to start with. */
+static void
+ctf_arc_import_parent (const ctf_archive_t *arc, ctf_dict_t *fp)
+{
+ if ((fp->ctf_flags & LCTF_CHILD) && fp->ctf_parname && !fp->ctf_parent)
+ {
+ ctf_dict_t *parent = ctf_dict_open_cached ((ctf_archive_t *) arc,
+ fp->ctf_parname, NULL);
+ if (parent)
+ {
+ ctf_import (fp, parent);
+ ctf_dict_close (parent);
+ }
+ }
+}
+
+/* Return the number of members in an archive. */
+size_t
+ctf_archive_count (const ctf_archive_t *wrapper)
+{
+ if (!wrapper->ctfi_is_archive)
+ return 1;
+
+ return wrapper->ctfi_archive->ctfa_ndicts;
+}
+
+/* Look up a symbol in an archive by name or index (if the name is set, a lookup
+ by name is done). Return the dict in the archive that the symbol is found
+ in, and (optionally) the ctf_id_t of the symbol in that dict (so you don't
+ have to look it up yourself). The dict is cached, so repeated lookups are
+ nearly free.
+
+ As usual, you should ctf_dict_close() the returned dict once you are done
+ with it.
+
+ Returns NULL on error, and an error in errp (if set). */
+
+static ctf_dict_t *
+ctf_arc_lookup_sym_or_name (ctf_archive_t *wrapper, unsigned long symidx,
+ const char *symname, ctf_id_t *typep, int *errp)
+{
+ ctf_dict_t *fp;
+ void *fpkey;
+ ctf_id_t type;
+
+ /* The usual non-archive-transparent-wrapper special case. */
+ if (!wrapper->ctfi_is_archive)
+ {
+ if (!symname)
+ {
+ if ((type = ctf_lookup_by_symbol (wrapper->ctfi_dict, symidx)) == CTF_ERR)
+ {
+ if (errp)
+ *errp = ctf_errno (wrapper->ctfi_dict);
+ return NULL;
+ }
+ }
+ else
+ {
+ if ((type = ctf_lookup_by_symbol_name (wrapper->ctfi_dict,
+ symname)) == CTF_ERR)
+ {
+ if (errp)
+ *errp = ctf_errno (wrapper->ctfi_dict);
+ return NULL;
+ }
+ }
+ if (typep)
+ *typep = type;
+ wrapper->ctfi_dict->ctf_refcnt++;
+ return wrapper->ctfi_dict;
+ }
+
+ if (wrapper->ctfi_symsect.cts_name == NULL
+ || wrapper->ctfi_symsect.cts_data == NULL
+ || wrapper->ctfi_symsect.cts_size == 0
+ || wrapper->ctfi_symsect.cts_entsize == 0)
+ {
+ if (errp)
+ *errp = ECTF_NOSYMTAB;
+ return NULL;
+ }
+
+ /* Make enough space for all possible symbol indexes, if not already done. We
+ cache the originating dictionary of all symbols. The dict links are weak,
+ to the dictionaries cached in ctfi_dicts: their refcnts are *not* bumped.
+ We also cache similar mappings for symbol names: these are ordinary
+ dynhashes, with weak links to dicts. */
+
+ if (!wrapper->ctfi_symdicts)
+ {
+ if ((wrapper->ctfi_symdicts = calloc (wrapper->ctfi_symsect.cts_size
+ / wrapper->ctfi_symsect.cts_entsize,
+ sizeof (ctf_dict_t *))) == NULL)
+ {
+ if (errp)
+ *errp = ENOMEM;
+ return NULL;
+ }
+ }
+ if (!wrapper->ctfi_symnamedicts)
+ {
+ if ((wrapper->ctfi_symnamedicts = ctf_dynhash_create (ctf_hash_string,
+ ctf_hash_eq_string,
+ free, NULL)) == NULL)
+ {
+ if (errp)
+ *errp = ENOMEM;
+ return NULL;
+ }
+ }
+
+ /* Perhaps the dict in which we found a previous lookup is cached. If it's
+ supposed to be cached but we don't find it, pretend it was always not
+ found: this should never happen, but shouldn't be allowed to cause trouble
+ if it does. */
+
+ if ((symname && ctf_dynhash_lookup_kv (wrapper->ctfi_symnamedicts,
+ symname, NULL, &fpkey))
+ || (!symname && wrapper->ctfi_symdicts[symidx] != NULL))
+ {
+ if (symname)
+ fp = (ctf_dict_t *) fpkey;
+ else
+ fp = wrapper->ctfi_symdicts[symidx];
+
+ if (fp == &enosym)
+ goto no_sym;
+
+ if (symname)
+ {
+ if ((type = ctf_lookup_by_symbol_name (fp, symname)) == CTF_ERR)
+ goto cache_no_sym;
+ }
+ else
+ {
+ if ((type = ctf_lookup_by_symbol (fp, symidx)) == CTF_ERR)
+ goto cache_no_sym;
+ }
+
+ if (typep)
+ *typep = type;
+ fp->ctf_refcnt++;
+ return fp;
+ }
+
+ /* Not cached: find it and cache it. We must track open errors ourselves even
+ if our caller doesn't, to be able to distinguish no-error end-of-iteration
+ from open errors. */
+
+ int local_err;
+ int *local_errp;
+ ctf_next_t *i = NULL;
+ const char *name;
+
+ if (errp)
+ local_errp = errp;
+ else
+ local_errp = &local_err;
+
+ while ((fp = ctf_archive_next (wrapper, &i, &name, 0, local_errp)) != NULL)
+ {
+ if (!symname)
+ {
+ if ((type = ctf_lookup_by_symbol (fp, symidx)) != CTF_ERR)
+ wrapper->ctfi_symdicts[symidx] = fp;
+ }
+ else
+ {
+ if ((type = ctf_lookup_by_symbol_name (fp, symname)) != CTF_ERR)
+ {
+ char *tmp;
+ /* No error checking, as above. */
+ if ((tmp = strdup (symname)) != NULL)
+ ctf_dynhash_insert (wrapper->ctfi_symnamedicts, tmp, fp);
+ }
+ }
+
+ if (type != CTF_ERR)
+ {
+ if (typep)
+ *typep = type;
+ ctf_next_destroy (i);
+ return fp;
+ }
+ if (ctf_errno (fp) != ECTF_NOTYPEDAT)
+ {
+ if (errp)
+ *errp = ctf_errno (fp);
+ ctf_next_destroy (i);
+ return NULL; /* errno is set for us. */
+ }
+ ctf_dict_close (fp);
+ }
+ if (*local_errp != ECTF_NEXT_END)
+ {
+ ctf_next_destroy (i);
+ return NULL;
+ }
+
+ /* Don't leak end-of-iteration to the caller. */
+ *local_errp = 0;
+
+ cache_no_sym:
+ if (!symname)
+ wrapper->ctfi_symdicts[symidx] = &enosym;
+ else
+ {
+ char *tmp;
+
+ /* No error checking: if caching fails, there is only a slight performance
+ impact. */
+ if ((tmp = strdup (symname)) != NULL)
+ if (ctf_dynhash_insert (wrapper->ctfi_symnamedicts, tmp, &enosym) < 0)
+ free (tmp);
+ }
+
+ no_sym:
+ if (errp)
+ *errp = ECTF_NOTYPEDAT;
+ if (typep)
+ *typep = CTF_ERR;
+ return NULL;
+}
+
+/* The public API for looking up a symbol by index. */
+ctf_dict_t *
+ctf_arc_lookup_symbol (ctf_archive_t *wrapper, unsigned long symidx,
+ ctf_id_t *typep, int *errp)
+{
+ return ctf_arc_lookup_sym_or_name (wrapper, symidx, NULL, typep, errp);
+}
+
+/* The public API for looking up a symbol by name. */
+
+ctf_dict_t *
+ctf_arc_lookup_symbol_name (ctf_archive_t *wrapper, const char *symname,
+ ctf_id_t *typep, int *errp)
+{
+ return ctf_arc_lookup_sym_or_name (wrapper, 0, symname, typep, errp);
+}
+