+union _bfd_doprnt_args
+{
+ int i;
+ long l;
+ long long ll;
+ double d;
+ long double ld;
+ void *p;
+ enum
+ {
+ Bad,
+ Int,
+ Long,
+ LongLong,
+ Double,
+ LongDouble,
+ Ptr
+ } type;
+};
+
+/* This macro and _bfd_doprnt taken from libiberty _doprnt.c, tidied a
+ little and extended to handle '%pA', '%pB' and positional parameters. */
+
+#define PRINT_TYPE(TYPE, FIELD) \
+ do \
+ { \
+ TYPE value = (TYPE) args[arg_no].FIELD; \
+ result = fprintf (stream, specifier, value); \
+ } while (0)
+
+static int
+_bfd_doprnt (FILE *stream, const char *format, union _bfd_doprnt_args *args)
+{
+ const char *ptr = format;
+ char specifier[128];
+ int total_printed = 0;
+ unsigned int arg_count = 0;
+
+ while (*ptr != '\0')
+ {
+ int result;
+
+ if (*ptr != '%')
+ {
+ /* While we have regular characters, print them. */
+ char *end = strchr (ptr, '%');
+ if (end != NULL)
+ result = fprintf (stream, "%.*s", (int) (end - ptr), ptr);
+ else
+ result = fprintf (stream, "%s", ptr);
+ ptr += result;
+ }
+ else if (ptr[1] == '%')
+ {
+ fputc ('%', stream);
+ result = 1;
+ ptr += 2;
+ }
+ else
+ {
+ /* We have a format specifier! */
+ char *sptr = specifier;
+ int wide_width = 0, short_width = 0;
+ unsigned int arg_no;
+
+ /* Copy the % and move forward. */
+ *sptr++ = *ptr++;
+
+ /* Check for a positional parameter. */
+ arg_no = -1u;
+ if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
+ {
+ arg_no = *ptr - '1';
+ ptr += 2;
+ }
+
+ /* Move past flags. */
+ while (strchr ("-+ #0'I", *ptr))
+ *sptr++ = *ptr++;
+
+ if (*ptr == '*')
+ {
+ int value;
+ unsigned int arg_index;
+
+ ptr++;
+ arg_index = arg_count;
+ if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
+ {
+ arg_index = *ptr - '1';
+ ptr += 2;
+ }
+ value = abs (args[arg_index].i);
+ arg_count++;
+ sptr += sprintf (sptr, "%d", value);
+ }
+ else
+ /* Handle explicit numeric value. */
+ while (ISDIGIT (*ptr))
+ *sptr++ = *ptr++;
+
+ /* Precision. */
+ if (*ptr == '.')
+ {
+ /* Copy and go past the period. */
+ *sptr++ = *ptr++;
+ if (*ptr == '*')
+ {
+ int value;
+ unsigned int arg_index;
+
+ ptr++;
+ arg_index = arg_count;
+ if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
+ {
+ arg_index = *ptr - '1';
+ ptr += 2;
+ }
+ value = abs (args[arg_index].i);
+ arg_count++;
+ sptr += sprintf (sptr, "%d", value);
+ }
+ else
+ /* Handle explicit numeric value. */
+ while (ISDIGIT (*ptr))
+ *sptr++ = *ptr++;
+ }
+ while (strchr ("hlL", *ptr))
+ {
+ switch (*ptr)
+ {
+ case 'h':
+ short_width = 1;
+ break;
+ case 'l':
+ wide_width++;
+ break;
+ case 'L':
+ wide_width = 2;
+ break;
+ default:
+ abort();
+ }
+ *sptr++ = *ptr++;
+ }
+
+ /* Copy the type specifier, and NULL terminate. */
+ *sptr++ = *ptr++;
+ *sptr = '\0';
+ if ((int) arg_no < 0)
+ arg_no = arg_count;
+
+ switch (ptr[-1])
+ {
+ case 'd':
+ case 'i':
+ case 'o':
+ case 'u':
+ case 'x':
+ case 'X':
+ case 'c':
+ {
+ /* Short values are promoted to int, so just copy it
+ as an int and trust the C library printf to cast it
+ to the right width. */
+ if (short_width)
+ PRINT_TYPE (int, i);
+ else
+ {
+ switch (wide_width)
+ {
+ case 0:
+ PRINT_TYPE (int, i);
+ break;
+ case 1:
+ PRINT_TYPE (long, l);
+ break;
+ case 2:
+ default:
+#if defined (__MSVCRT__)
+ sptr[-3] = 'I';
+ sptr[-2] = '6';
+ sptr[-1] = '4';
+ *sptr++ = ptr[-1];
+ *sptr = '\0';
+#endif
+#if defined (__GNUC__) || defined (HAVE_LONG_LONG)
+ PRINT_TYPE (long long, ll);
+#else
+ /* Fake it and hope for the best. */
+ PRINT_TYPE (long, l);
+#endif
+ break;
+ }
+ }
+ }
+ break;
+ case 'f':
+ case 'e':
+ case 'E':
+ case 'g':
+ case 'G':
+ {
+ if (wide_width == 0)
+ PRINT_TYPE (double, d);
+ else
+ {
+#if defined (__GNUC__) || defined (HAVE_LONG_DOUBLE)
+ PRINT_TYPE (long double, ld);
+#else
+ /* Fake it and hope for the best. */
+ PRINT_TYPE (double, d);
+#endif
+ }
+ }
+ break;
+ case 's':
+ PRINT_TYPE (char *, p);
+ break;
+ case 'p':
+ if (*ptr == 'A')
+ {
+ asection *sec;
+ bfd *abfd;
+ const char *group = NULL;
+ struct coff_comdat_info *ci;
+
+ ptr++;
+ sec = (asection *) args[arg_no].p;
+ if (sec == NULL)
+ /* Invoking %pA with a null section pointer is an
+ internal error. */
+ abort ();
+ abfd = sec->owner;
+ if (abfd != NULL
+ && bfd_get_flavour (abfd) == bfd_target_elf_flavour
+ && elf_next_in_group (sec) != NULL
+ && (sec->flags & SEC_GROUP) == 0)
+ group = elf_group_name (sec);
+ else if (abfd != NULL
+ && bfd_get_flavour (abfd) == bfd_target_coff_flavour
+ && (ci = bfd_coff_get_comdat_section (sec->owner,
+ sec)) != NULL)
+ group = ci->name;
+ if (group != NULL)
+ result = fprintf (stream, "%s[%s]", sec->name, group);
+ else
+ result = fprintf (stream, "%s", sec->name);
+ }
+ else if (*ptr == 'B')
+ {
+ bfd *abfd;
+
+ ptr++;
+ abfd = (bfd *) args[arg_no].p;
+ if (abfd == NULL)
+ /* Invoking %pB with a null bfd pointer is an
+ internal error. */
+ abort ();
+ else if (abfd->my_archive
+ && !bfd_is_thin_archive (abfd->my_archive))
+ result = fprintf (stream, "%s(%s)",
+ bfd_get_filename (abfd->my_archive),
+ bfd_get_filename (abfd));
+ else
+ result = fprintf (stream, "%s", bfd_get_filename (abfd));
+ }
+ else
+ PRINT_TYPE (void *, p);
+ break;
+ default:
+ abort();
+ }
+ arg_count++;
+ }
+ if (result == -1)
+ return -1;
+ total_printed += result;
+ }
+
+ return total_printed;
+}
+
+/* First pass over FORMAT to gather ARGS. Returns number of args. */
+
+static unsigned int
+_bfd_doprnt_scan (const char *format, union _bfd_doprnt_args *args)
+{
+ const char *ptr = format;
+ unsigned int arg_count = 0;
+
+ while (*ptr != '\0')
+ {
+ if (*ptr != '%')
+ {
+ ptr = strchr (ptr, '%');
+ if (ptr == NULL)
+ break;
+ }
+ else if (ptr[1] == '%')
+ ptr += 2;
+ else
+ {
+ int wide_width = 0, short_width = 0;
+ unsigned int arg_no;
+ int arg_type;
+
+ ptr++;
+
+ /* Check for a positional parameter. */
+ arg_no = -1u;
+ if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
+ {
+ arg_no = *ptr - '1';
+ ptr += 2;
+ }
+
+ /* Move past flags. */
+ while (strchr ("-+ #0'I", *ptr))
+ ptr++;
+
+ if (*ptr == '*')
+ {
+ unsigned int arg_index;
+
+ ptr++;
+ arg_index = arg_count;
+ if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
+ {
+ arg_index = *ptr - '1';
+ ptr += 2;
+ }
+ if (arg_index >= 9)
+ abort ();
+ args[arg_index].type = Int;
+ arg_count++;
+ }
+ else
+ /* Handle explicit numeric value. */
+ while (ISDIGIT (*ptr))
+ ptr++;
+
+ /* Precision. */
+ if (*ptr == '.')
+ {
+ ptr++;
+ if (*ptr == '*')
+ {
+ unsigned int arg_index;
+
+ ptr++;
+ arg_index = arg_count;
+ if (*ptr != '0' && ISDIGIT (*ptr) && ptr[1] == '$')
+ {
+ arg_index = *ptr - '1';
+ ptr += 2;
+ }
+ if (arg_index >= 9)
+ abort ();
+ args[arg_index].type = Int;
+ arg_count++;
+ }
+ else
+ /* Handle explicit numeric value. */
+ while (ISDIGIT (*ptr))
+ ptr++;
+ }
+ while (strchr ("hlL", *ptr))
+ {
+ switch (*ptr)
+ {
+ case 'h':
+ short_width = 1;
+ break;
+ case 'l':
+ wide_width++;
+ break;
+ case 'L':
+ wide_width = 2;
+ break;
+ default:
+ abort();
+ }
+ ptr++;
+ }
+
+ ptr++;
+ if ((int) arg_no < 0)
+ arg_no = arg_count;
+
+ arg_type = Bad;
+ switch (ptr[-1])
+ {
+ case 'd':
+ case 'i':
+ case 'o':
+ case 'u':
+ case 'x':
+ case 'X':
+ case 'c':
+ {
+ if (short_width)
+ arg_type = Int;
+ else
+ {
+ switch (wide_width)
+ {
+ case 0:
+ arg_type = Int;
+ break;
+ case 1:
+ arg_type = Long;
+ break;
+ case 2:
+ default:
+#if defined (__GNUC__) || defined (HAVE_LONG_LONG)
+ arg_type = LongLong;
+#else
+ arg_type = Long;
+#endif
+ break;
+ }
+ }
+ }
+ break;
+ case 'f':
+ case 'e':
+ case 'E':
+ case 'g':
+ case 'G':
+ {
+ if (wide_width == 0)
+ arg_type = Double;
+ else
+ {
+#if defined (__GNUC__) || defined (HAVE_LONG_DOUBLE)
+ arg_type = LongDouble;
+#else
+ arg_type = Double;
+#endif
+ }
+ }
+ break;
+ case 's':
+ arg_type = Ptr;
+ break;
+ case 'p':
+ if (*ptr == 'A' || *ptr == 'B')
+ ptr++;
+ arg_type = Ptr;
+ break;
+ default:
+ abort();
+ }
+
+ if (arg_no >= 9)
+ abort ();
+ args[arg_no].type = arg_type;
+ arg_count++;
+ }
+ }
+
+ return arg_count;
+}