+\f
+
+/* Formatting callback function passed to ctf_dump. Returns either the pointer
+ it is passed, or a pointer to newly-allocated storage, in which case
+ dump_ctf() will free it when it no longer needs it. */
+
+static char *
+dump_ctf_indent_lines (ctf_sect_names_t sect ATTRIBUTE_UNUSED,
+ char *s, void *arg)
+{
+ const char *blanks = arg;
+ char *new_s;
+
+ if (asprintf (&new_s, "%s%s", blanks, s) < 0)
+ return s;
+ return new_s;
+}
+
+/* Make a ctfsect suitable for ctf_bfdopen_ctfsect(). */
+static ctf_sect_t
+make_ctfsect (const char *name, bfd_byte *data,
+ bfd_size_type size)
+{
+ ctf_sect_t ctfsect;
+
+ ctfsect.cts_name = name;
+ ctfsect.cts_entsize = 1;
+ ctfsect.cts_size = size;
+ ctfsect.cts_data = data;
+
+ return ctfsect;
+}
+
+/* Dump one CTF archive member. */
+
+static int
+dump_ctf_archive_member (ctf_file_t *ctf, const char *name, void *arg)
+{
+ ctf_file_t *parent = (ctf_file_t *) arg;
+ const char *things[] = {"Header", "Labels", "Data objects",
+ "Function objects", "Variables", "Types", "Strings",
+ ""};
+ const char **thing;
+ size_t i;
+
+ /* Only print out the name of non-default-named archive members.
+ The name .ctf appears everywhere, even for things that aren't
+ really archives, so printing it out is liable to be confusing.
+
+ The parent, if there is one, is the default-owned archive member:
+ avoid importing it into itself. (This does no harm, but looks
+ confusing.) */
+
+ if (strcmp (name, ".ctf") != 0)
+ {
+ printf (_("\nCTF archive member: %s:\n"), sanitize_string (name));
+ ctf_import (ctf, parent);
+ }
+
+ for (i = 0, thing = things; *thing[0]; thing++, i++)
+ {
+ ctf_dump_state_t *s = NULL;
+ char *item;
+
+ printf ("\n %s:\n", *thing);
+ while ((item = ctf_dump (ctf, &s, i, dump_ctf_indent_lines,
+ (void *) " ")) != NULL)
+ {
+ printf ("%s\n", item);
+ free (item);
+ }
+
+ if (ctf_errno (ctf))
+ {
+ non_fatal (_("Iteration failed: %s, %s\n"), *thing,
+ ctf_errmsg (ctf_errno (ctf)));
+ break;
+ }
+ }
+ return 0;
+}
+
+/* Dump the CTF debugging information. */
+
+static void
+dump_ctf (bfd *abfd, const char *sect_name, const char *parent_name)
+{
+ ctf_archive_t *ctfa, *parenta = NULL, *lookparent;
+ bfd_byte *ctfdata, *parentdata = NULL;
+ bfd_size_type ctfsize, parentsize;
+ ctf_sect_t ctfsect;
+ ctf_file_t *parent = NULL;
+ int err;
+
+ if ((ctfdata = read_section_stabs (abfd, sect_name, &ctfsize, NULL)) == NULL)
+ bfd_fatal (bfd_get_filename (abfd));
+
+ if (parent_name
+ && (parentdata = read_section_stabs (abfd, parent_name, &parentsize,
+ NULL)) == NULL)
+ bfd_fatal (bfd_get_filename (abfd));
+
+ /* Load the CTF file and dump it. */
+
+ ctfsect = make_ctfsect (sect_name, ctfdata, ctfsize);
+ if ((ctfa = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
+ {
+ non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
+ bfd_fatal (bfd_get_filename (abfd));
+ }
+
+ if (parentdata)
+ {
+ ctfsect = make_ctfsect (parent_name, parentdata, parentsize);
+ if ((parenta = ctf_bfdopen_ctfsect (abfd, &ctfsect, &err)) == NULL)
+ {
+ non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
+ bfd_fatal (bfd_get_filename (abfd));
+ }
+
+ lookparent = parenta;
+ }
+ else
+ lookparent = ctfa;
+
+ /* Assume that the applicable parent archive member is the default one.
+ (This is what all known implementations are expected to do, if they
+ put CTFs and their parents in archives together.) */
+ if ((parent = ctf_arc_open_by_name (lookparent, NULL, &err)) == NULL)
+ {
+ non_fatal (_("CTF open failure: %s\n"), ctf_errmsg (err));
+ bfd_fatal (bfd_get_filename (abfd));
+ }
+
+ printf (_("Contents of CTF section %s:\n"), sanitize_string (sect_name));
+
+ ctf_archive_iter (ctfa, dump_ctf_archive_member, parent);
+ ctf_file_close (parent);
+ ctf_close (ctfa);
+ ctf_close (parenta);
+ free (parentdata);
+ free (ctfdata);
+}