readelf: Add option to display the names of sections referenced by symbols.
[binutils-gdb.git] / binutils / prdbg.c
blobfb351476893c2c42d9289259237f7450bfab10e7
1 /* prdbg.c -- Print out generic debugging information.
2 Copyright (C) 1995-2023 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
4 Tags style generation written by Salvador E. Tropea <set@computer.org>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* This file prints out the generic debugging information, by
24 supplying a set of routines to debug_write. */
26 #include "sysdep.h"
27 #include <assert.h>
28 #include "bfd.h"
29 #include "libiberty.h"
30 #include "demangle.h"
31 #include "debug.h"
32 #include "budbg.h"
34 /* This is the structure we use as a handle for these routines. */
36 struct pr_handle
38 /* File to print information to. */
39 FILE *f;
40 /* Current indentation level. */
41 unsigned int indent;
42 /* Type stack. */
43 struct pr_stack *stack;
44 /* Parameter number we are about to output. */
45 int parameter;
46 /* The following are used only by the tags code (tg_). */
47 /* Name of the file we are using. */
48 char *filename;
49 /* The BFD. */
50 bfd *abfd;
51 /* The symbols table for this BFD. */
52 asymbol **syms;
53 /* Pointer to a function to demangle symbols. */
54 char *(*demangler) (bfd *, const char *, int);
57 /* The type stack. */
59 struct pr_stack
61 /* Next element on the stack. */
62 struct pr_stack *next;
63 /* This element. */
64 char *type;
65 /* Current visibility of fields if this is a class. */
66 enum debug_visibility visibility;
67 /* Name of the current method we are handling. */
68 char *method;
69 /* The following are used only by the tags code (tg_). */
70 /* Type for the container (struct, union, class, union class). */
71 const char *flavor;
72 /* A comma separated list of parent classes. */
73 char *parents;
76 static bool pr_start_compilation_unit (void *, const char *);
77 static bool pr_start_source (void *, const char *);
78 static bool pr_empty_type (void *);
79 static bool pr_void_type (void *);
80 static bool pr_int_type (void *, unsigned int, bool);
81 static bool pr_float_type (void *, unsigned int);
82 static bool pr_complex_type (void *, unsigned int);
83 static bool pr_bool_type (void *, unsigned int);
84 static bool pr_enum_type
85 (void *, const char *, const char **, bfd_signed_vma *);
86 static bool pr_pointer_type (void *);
87 static bool pr_function_type (void *, int, bool);
88 static bool pr_reference_type (void *);
89 static bool pr_range_type (void *, bfd_signed_vma, bfd_signed_vma);
90 static bool pr_array_type (void *, bfd_signed_vma, bfd_signed_vma, bool);
91 static bool pr_set_type (void *, bool);
92 static bool pr_offset_type (void *);
93 static bool pr_method_type (void *, bool, int, bool);
94 static bool pr_const_type (void *);
95 static bool pr_volatile_type (void *);
96 static bool pr_start_struct_type
97 (void *, const char *, unsigned int, bool, unsigned int);
98 static bool pr_struct_field
99 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
100 static bool pr_end_struct_type (void *);
101 static bool pr_start_class_type
102 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
103 static bool pr_class_static_member
104 (void *, const char *, const char *, enum debug_visibility);
105 static bool pr_class_baseclass
106 (void *, bfd_vma, bool, enum debug_visibility);
107 static bool pr_class_start_method (void *, const char *);
108 static bool pr_class_method_variant
109 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
110 static bool pr_class_static_method_variant
111 (void *, const char *, enum debug_visibility, bool, bool);
112 static bool pr_class_end_method (void *);
113 static bool pr_end_class_type (void *);
114 static bool pr_typedef_type (void *, const char *);
115 static bool pr_tag_type
116 (void *, const char *, unsigned int, enum debug_type_kind);
117 static bool pr_typdef (void *, const char *);
118 static bool pr_tag (void *, const char *);
119 static bool pr_int_constant (void *, const char *, bfd_vma);
120 static bool pr_float_constant (void *, const char *, double);
121 static bool pr_typed_constant (void *, const char *, bfd_vma);
122 static bool pr_variable (void *, const char *, enum debug_var_kind, bfd_vma);
123 static bool pr_start_function (void *, const char *, bool);
124 static bool pr_function_parameter
125 (void *, const char *, enum debug_parm_kind, bfd_vma);
126 static bool pr_start_block (void *, bfd_vma);
127 static bool pr_end_block (void *, bfd_vma);
128 static bool pr_end_function (void *);
129 static bool pr_lineno (void *, const char *, unsigned long, bfd_vma);
131 static const char *visibility_name (enum debug_visibility);
133 /* Tags style replacements. */
134 static bool tg_start_compilation_unit (void *, const char *);
135 static bool tg_start_source (void *, const char *);
136 static bool tg_enum_type
137 (void *, const char *, const char **, bfd_signed_vma *);
138 static bool tg_start_struct_type
139 (void *, const char *, unsigned int, bool, unsigned int);
140 static bool pr_struct_field
141 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
142 static bool tg_struct_field
143 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
144 static bool tg_struct_field
145 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
146 static bool tg_end_struct_type (void *);
147 static bool tg_start_class_type
148 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
149 static bool tg_class_static_member
150 (void *, const char *, const char *, enum debug_visibility);
151 static bool tg_class_baseclass (void *, bfd_vma, bool, enum debug_visibility);
152 static bool tg_class_method_variant
153 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
154 static bool tg_class_static_method_variant
155 (void *, const char *, enum debug_visibility, bool, bool);
156 static bool tg_end_class_type (void *);
157 static bool tg_tag_type
158 (void *, const char *, unsigned int, enum debug_type_kind);
159 static bool tg_typdef (void *, const char *);
160 static bool tg_tag (void *, const char *);
161 static bool tg_int_constant (void *, const char *, bfd_vma);
162 static bool tg_float_constant (void *, const char *, double);
163 static bool tg_typed_constant (void *, const char *, bfd_vma);
164 static bool tg_variable (void *, const char *, enum debug_var_kind, bfd_vma);
165 static bool tg_start_function (void *, const char *, bool);
166 static bool tg_function_parameter
167 (void *, const char *, enum debug_parm_kind, bfd_vma);
168 static bool tg_start_block (void *, bfd_vma);
169 static bool tg_end_block (void *, bfd_vma);
170 static bool tg_lineno (void *, const char *, unsigned long, bfd_vma);
172 static const struct debug_write_fns pr_fns =
174 pr_start_compilation_unit,
175 pr_start_source,
176 pr_empty_type,
177 pr_void_type,
178 pr_int_type,
179 pr_float_type,
180 pr_complex_type,
181 pr_bool_type,
182 pr_enum_type,
183 pr_pointer_type,
184 pr_function_type,
185 pr_reference_type,
186 pr_range_type,
187 pr_array_type,
188 pr_set_type,
189 pr_offset_type,
190 pr_method_type,
191 pr_const_type,
192 pr_volatile_type,
193 pr_start_struct_type,
194 pr_struct_field,
195 pr_end_struct_type,
196 pr_start_class_type,
197 pr_class_static_member,
198 pr_class_baseclass,
199 pr_class_start_method,
200 pr_class_method_variant,
201 pr_class_static_method_variant,
202 pr_class_end_method,
203 pr_end_class_type,
204 pr_typedef_type,
205 pr_tag_type,
206 pr_typdef,
207 pr_tag,
208 pr_int_constant,
209 pr_float_constant,
210 pr_typed_constant,
211 pr_variable,
212 pr_start_function,
213 pr_function_parameter,
214 pr_start_block,
215 pr_end_block,
216 pr_end_function,
217 pr_lineno
220 static const struct debug_write_fns tg_fns =
222 tg_start_compilation_unit,
223 tg_start_source,
224 pr_empty_type, /* Same, push_type. */
225 pr_void_type, /* Same, push_type. */
226 pr_int_type, /* Same, push_type. */
227 pr_float_type, /* Same, push_type. */
228 pr_complex_type, /* Same, push_type. */
229 pr_bool_type, /* Same, push_type. */
230 tg_enum_type,
231 pr_pointer_type, /* Same, changes to pointer. */
232 pr_function_type, /* Same, push_type. */
233 pr_reference_type, /* Same, changes to reference. */
234 pr_range_type, /* FIXME: What's that?. */
235 pr_array_type, /* Same, push_type. */
236 pr_set_type, /* FIXME: What's that?. */
237 pr_offset_type, /* FIXME: What's that?. */
238 pr_method_type, /* Same. */
239 pr_const_type, /* Same, changes to const. */
240 pr_volatile_type, /* Same, changes to volatile. */
241 tg_start_struct_type,
242 tg_struct_field,
243 tg_end_struct_type,
244 tg_start_class_type,
245 tg_class_static_member,
246 tg_class_baseclass,
247 pr_class_start_method, /* Same, remembers that's a method. */
248 tg_class_method_variant,
249 tg_class_static_method_variant,
250 pr_class_end_method, /* Same, forgets that's a method. */
251 tg_end_class_type,
252 pr_typedef_type, /* Same, just push type. */
253 tg_tag_type,
254 tg_typdef,
255 tg_tag,
256 tg_int_constant, /* Untested. */
257 tg_float_constant, /* Untested. */
258 tg_typed_constant, /* Untested. */
259 tg_variable,
260 tg_start_function,
261 tg_function_parameter,
262 tg_start_block,
263 tg_end_block,
264 pr_end_function, /* Same, does nothing. */
265 tg_lineno
268 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
270 /* Print out the generic debugging information recorded in dhandle. */
272 bool
273 print_debugging_info (FILE *f, void *dhandle, bfd *abfd, asymbol **syms,
274 char * (*demangler) (struct bfd *, const char *, int),
275 bool as_tags)
277 struct pr_handle info;
279 info.f = f;
280 info.indent = 0;
281 info.stack = NULL;
282 info.parameter = 0;
283 info.filename = NULL;
284 info.abfd = abfd;
285 info.syms = syms;
286 info.demangler = demangler;
288 if (as_tags)
290 fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
291 fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
292 fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
293 fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
296 bool ret = debug_write (dhandle, as_tags ? &tg_fns : &pr_fns, &info);
297 while (info.stack != NULL)
299 struct pr_stack *s = info.stack;
300 info.stack = s->next;
301 free (s->type);
302 free (s->method);
303 free (s->parents);
304 free (s);
306 free (info.filename);
307 return ret;
310 /* Indent to the current indentation level. */
312 static void
313 indent (struct pr_handle *info)
315 unsigned int i;
317 for (i = 0; i < info->indent; i++)
318 putc (' ', info->f);
321 /* Push a type on the type stack. */
323 static bool
324 push_type (struct pr_handle *info, const char *type)
326 struct pr_stack *n;
328 if (type == NULL)
329 return false;
331 n = xmalloc (sizeof *n);
332 memset (n, 0, sizeof *n);
334 n->type = xstrdup (type);
335 n->visibility = DEBUG_VISIBILITY_IGNORE;
336 n->method = NULL;
337 n->next = info->stack;
338 info->stack = n;
340 return true;
343 /* Prepend a string onto the type on the top of the type stack. */
345 static bool
346 prepend_type (struct pr_handle *info, const char *s)
348 char *n;
350 assert (info->stack != NULL);
352 n = xmalloc (strlen (s) + strlen (info->stack->type) + 1);
353 sprintf (n, "%s%s", s, info->stack->type);
354 free (info->stack->type);
355 info->stack->type = n;
357 return true;
360 /* Append a string to the type on the top of the type stack. */
362 static bool
363 append_type (struct pr_handle *info, const char *s)
365 unsigned int len;
367 if (s == NULL)
368 return false;
370 assert (info->stack != NULL);
372 len = strlen (info->stack->type);
373 info->stack->type = xrealloc (info->stack->type, len + strlen (s) + 1);
374 strcpy (info->stack->type + len, s);
376 return true;
379 /* Append a string to the parents on the top of the type stack. */
381 static bool
382 append_parent (struct pr_handle *info, const char *s)
384 unsigned int len;
386 if (s == NULL)
387 return false;
389 assert (info->stack != NULL);
391 len = info->stack->parents ? strlen (info->stack->parents) : 0;
392 info->stack->parents = xrealloc (info->stack->parents, len + strlen (s) + 1);
393 strcpy (info->stack->parents + len, s);
395 return true;
398 /* We use an underscore to indicate where the name should go in a type
399 string. This function substitutes a string for the underscore. If
400 there is no underscore, the name follows the type. */
402 static bool
403 substitute_type (struct pr_handle *info, const char *s)
405 char *u;
407 assert (info->stack != NULL);
409 u = strchr (info->stack->type, '|');
410 if (u != NULL)
412 char *n;
414 n = xmalloc (strlen (info->stack->type) + strlen (s));
416 memcpy (n, info->stack->type, u - info->stack->type);
417 strcpy (n + (u - info->stack->type), s);
418 strcat (n, u + 1);
420 free (info->stack->type);
421 info->stack->type = n;
423 return true;
426 if (strchr (s, '|') != NULL
427 && (strchr (info->stack->type, '{') != NULL
428 || strchr (info->stack->type, '(') != NULL))
430 if (! prepend_type (info, "(")
431 || ! append_type (info, ")"))
432 return false;
435 if (*s == '\0')
436 return true;
438 return (append_type (info, " ")
439 && append_type (info, s));
442 /* Indent the type at the top of the stack by appending spaces. */
444 static bool
445 indent_type (struct pr_handle *info)
447 unsigned int i;
449 for (i = 0; i < info->indent; i++)
451 if (! append_type (info, " "))
452 return false;
455 return true;
458 /* Pop a type from the type stack. */
460 static char *
461 pop_type (struct pr_handle *info)
463 struct pr_stack *o;
464 char *ret;
466 assert (info->stack != NULL);
468 o = info->stack;
469 info->stack = o->next;
470 ret = o->type;
471 free (o);
473 return ret;
476 /* Print a VMA value into a string. */
478 static void
479 print_vma (bfd_vma vma, char *buf, bool unsignedp, bool hexp)
481 if (hexp)
482 sprintf (buf, "%#" PRIx64, (uint64_t) vma);
483 else if (unsignedp)
484 sprintf (buf, "%" PRIu64, (uint64_t) vma);
485 else
486 sprintf (buf, "%" PRId64, (int64_t) vma);
489 /* Start a new compilation unit. */
491 static bool
492 pr_start_compilation_unit (void *p, const char *filename)
494 struct pr_handle *info = (struct pr_handle *) p;
496 assert (info->indent == 0);
498 fprintf (info->f, "%s:\n", filename);
500 return true;
503 /* Start a source file within a compilation unit. */
505 static bool
506 pr_start_source (void *p, const char *filename)
508 struct pr_handle *info = (struct pr_handle *) p;
510 assert (info->indent == 0);
512 fprintf (info->f, " %s:\n", filename);
514 return true;
517 /* Push an empty type onto the type stack. */
519 static bool
520 pr_empty_type (void *p)
522 struct pr_handle *info = (struct pr_handle *) p;
524 return push_type (info, "<undefined>");
527 /* Push a void type onto the type stack. */
529 static bool
530 pr_void_type (void *p)
532 struct pr_handle *info = (struct pr_handle *) p;
534 return push_type (info, "void");
537 /* Push an integer type onto the type stack. */
539 static bool
540 pr_int_type (void *p, unsigned int size, bool unsignedp)
542 struct pr_handle *info = (struct pr_handle *) p;
543 char ab[40];
545 sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
546 return push_type (info, ab);
549 /* Push a floating type onto the type stack. */
551 static bool
552 pr_float_type (void *p, unsigned int size)
554 struct pr_handle *info = (struct pr_handle *) p;
555 char ab[40];
557 if (size == 4)
558 return push_type (info, "float");
559 else if (size == 8)
560 return push_type (info, "double");
562 sprintf (ab, "float%d", size * 8);
563 return push_type (info, ab);
566 /* Push a complex type onto the type stack. */
568 static bool
569 pr_complex_type (void *p, unsigned int size)
571 struct pr_handle *info = (struct pr_handle *) p;
573 if (! pr_float_type (p, size))
574 return false;
576 return prepend_type (info, "complex ");
579 /* Push a bool type onto the type stack. */
581 static bool
582 pr_bool_type (void *p, unsigned int size)
584 struct pr_handle *info = (struct pr_handle *) p;
585 char ab[40];
587 sprintf (ab, "bool%d", size * 8);
589 return push_type (info, ab);
592 /* Push an enum type onto the type stack. */
594 static bool
595 pr_enum_type (void *p, const char *tag, const char **names,
596 bfd_signed_vma *values)
598 struct pr_handle *info = (struct pr_handle *) p;
599 unsigned int i;
600 bfd_signed_vma val;
602 if (! push_type (info, "enum "))
603 return false;
604 if (tag != NULL)
606 if (! append_type (info, tag)
607 || ! append_type (info, " "))
608 return false;
610 if (! append_type (info, "{ "))
611 return false;
613 if (names == NULL)
615 if (! append_type (info, "/* undefined */"))
616 return false;
618 else
620 val = 0;
621 for (i = 0; names[i] != NULL; i++)
623 if (i > 0)
625 if (! append_type (info, ", "))
626 return false;
629 if (! append_type (info, names[i]))
630 return false;
632 if (values[i] != val)
634 char ab[22];
636 print_vma (values[i], ab, false, false);
637 if (! append_type (info, " = ")
638 || ! append_type (info, ab))
639 return false;
640 val = values[i];
643 ++val;
647 return append_type (info, " }");
650 /* Turn the top type on the stack into a pointer. */
652 static bool
653 pr_pointer_type (void *p)
655 struct pr_handle *info = (struct pr_handle *) p;
656 char *s;
658 assert (info->stack != NULL);
660 s = strchr (info->stack->type, '|');
661 if (s != NULL && s[1] == '[')
662 return substitute_type (info, "(*|)");
663 return substitute_type (info, "*|");
666 /* Turn the top type on the stack into a function returning that type. */
668 static bool
669 pr_function_type (void *p, int argcount, bool varargs)
671 struct pr_handle *info = (struct pr_handle *) p;
672 char **arg_types;
673 unsigned int len;
674 char *s;
676 assert (info->stack != NULL);
678 len = 10;
680 if (argcount <= 0)
682 arg_types = NULL;
683 len += 15;
685 else
687 int i;
689 arg_types = xmalloc (argcount * sizeof (*arg_types));
690 for (i = argcount - 1; i >= 0; i--)
692 if (!substitute_type (info, "")
693 || (arg_types[i] = pop_type (info)) == NULL)
695 for (int j = i + 1; j < argcount; j++)
696 free (arg_types[j]);
697 free (arg_types);
698 return false;
700 len += strlen (arg_types[i]) + 2;
702 if (varargs)
703 len += 5;
706 /* Now the return type is on the top of the stack. */
708 s = xmalloc (len);
709 strcpy (s, "(|) (");
711 if (argcount < 0)
712 strcat (s, "/* unknown */");
713 else
715 int i;
717 for (i = 0; i < argcount; i++)
719 if (i > 0)
720 strcat (s, ", ");
721 strcat (s, arg_types[i]);
722 free (arg_types[i]);
724 if (varargs)
726 if (i > 0)
727 strcat (s, ", ");
728 strcat (s, "...");
730 free (arg_types);
733 strcat (s, ")");
735 bool ret = substitute_type (info, s);
736 free (s);
737 return ret;
740 /* Turn the top type on the stack into a reference to that type. */
742 static bool
743 pr_reference_type (void *p)
745 struct pr_handle *info = (struct pr_handle *) p;
747 assert (info->stack != NULL);
749 return substitute_type (info, "&|");
752 /* Make a range type. */
754 static bool
755 pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
757 struct pr_handle *info = (struct pr_handle *) p;
758 char abl[22], abu[22];
760 assert (info->stack != NULL);
762 if (! substitute_type (info, ""))
763 return false;
765 print_vma (lower, abl, false, false);
766 print_vma (upper, abu, false, false);
768 return (prepend_type (info, "range (")
769 && append_type (info, "):")
770 && append_type (info, abl)
771 && append_type (info, ":")
772 && append_type (info, abu));
775 /* Make an array type. */
777 static bool
778 pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
779 bool stringp)
781 struct pr_handle *info = (struct pr_handle *) p;
782 char *range_type;
783 char abl[22], abu[22], ab[50];
785 range_type = pop_type (info);
786 if (range_type == NULL)
787 return false;
789 if (lower == 0)
791 if (upper == -1)
792 sprintf (ab, "|[]");
793 else
795 print_vma (upper + 1, abu, false, false);
796 sprintf (ab, "|[%s]", abu);
799 else
801 print_vma (lower, abl, false, false);
802 print_vma (upper, abu, false, false);
803 sprintf (ab, "|[%s:%s]", abl, abu);
806 if (! substitute_type (info, ab))
807 goto fail;
809 if (strcmp (range_type, "int") != 0)
811 if (! append_type (info, ":")
812 || ! append_type (info, range_type))
813 goto fail;
816 if (stringp)
818 if (! append_type (info, " /* string */"))
819 goto fail;
822 free (range_type);
823 return true;
825 fail:
826 free (range_type);
827 return false;
830 /* Make a set type. */
832 static bool
833 pr_set_type (void *p, bool bitstringp)
835 struct pr_handle *info = (struct pr_handle *) p;
837 if (! substitute_type (info, ""))
838 return false;
840 if (! prepend_type (info, "set { ")
841 || ! append_type (info, " }"))
842 return false;
844 if (bitstringp)
846 if (! append_type (info, "/* bitstring */"))
847 return false;
850 return true;
853 /* Make an offset type. */
855 static bool
856 pr_offset_type (void *p)
858 struct pr_handle *info = (struct pr_handle *) p;
859 char *t;
861 if (! substitute_type (info, ""))
862 return false;
864 t = pop_type (info);
865 if (t == NULL)
866 return false;
868 bool ret = (substitute_type (info, "")
869 && prepend_type (info, " ")
870 && prepend_type (info, t)
871 && append_type (info, "::|"));
872 free (t);
873 return ret;
876 /* Make a method type. */
878 static bool
879 pr_method_type (void *p, bool domain, int argcount, bool varargs)
881 struct pr_handle *info = (struct pr_handle *) p;
882 unsigned int len;
883 char *domain_type = NULL, *free_domain = NULL;
884 char **arg_types;
885 char *s;
887 len = 10;
889 if (domain)
891 if (! substitute_type (info, ""))
892 return false;
893 domain_type = pop_type (info);
894 if (domain_type == NULL)
895 return false;
896 free_domain = domain_type;
897 if (startswith (domain_type, "class ")
898 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
899 domain_type += sizeof "class " - 1;
900 else if (startswith (domain_type, "union class ")
901 && (strchr (domain_type + sizeof "union class " - 1, ' ')
902 == NULL))
903 domain_type += sizeof "union class " - 1;
904 len += strlen (domain_type);
907 if (argcount <= 0)
909 arg_types = NULL;
910 len += 15;
912 else
914 int i;
916 arg_types = xmalloc (argcount * sizeof (*arg_types));
917 for (i = argcount - 1; i >= 0; i--)
919 if (!substitute_type (info, "")
920 || (arg_types[i] = pop_type (info)) == NULL)
922 for (int j = i + 1; j < argcount; ++j)
923 free (arg_types[j]);
924 free (arg_types);
925 return false;
927 len += strlen (arg_types[i]) + 2;
929 if (varargs)
930 len += 5;
933 /* Now the return type is on the top of the stack. */
935 s = xmalloc (len);
936 *s = 0;
937 if (domain)
939 strcpy (s, domain_type);
940 free (free_domain);
942 strcat (s, "::| (");
944 if (argcount < 0)
945 strcat (s, "/* unknown */");
946 else
948 int i;
950 for (i = 0; i < argcount; i++)
952 if (i > 0)
953 strcat (s, ", ");
954 strcat (s, arg_types[i]);
955 free (arg_types[i]);
957 if (varargs)
959 if (i > 0)
960 strcat (s, ", ");
961 strcat (s, "...");
963 free (arg_types);
966 strcat (s, ")");
968 bool ret = substitute_type (info, s);
969 free (s);
970 return ret;
973 /* Make a const qualified type. */
975 static bool
976 pr_const_type (void *p)
978 struct pr_handle *info = (struct pr_handle *) p;
980 return substitute_type (info, "const |");
983 /* Make a volatile qualified type. */
985 static bool
986 pr_volatile_type (void *p)
988 struct pr_handle *info = (struct pr_handle *) p;
990 return substitute_type (info, "volatile |");
993 /* Start accumulating a struct type. */
995 static bool
996 pr_start_struct_type (void *p, const char *tag, unsigned int id,
997 bool structp, unsigned int size)
999 struct pr_handle *info = (struct pr_handle *) p;
1001 info->indent += 2;
1003 if (! push_type (info, structp ? "struct " : "union "))
1004 return false;
1005 if (tag != NULL)
1007 if (! append_type (info, tag))
1008 return false;
1010 else
1012 char idbuf[20];
1014 sprintf (idbuf, "%%anon%u", id);
1015 if (! append_type (info, idbuf))
1016 return false;
1019 if (! append_type (info, " {"))
1020 return false;
1021 if (size != 0 || tag != NULL)
1023 char ab[30];
1025 if (! append_type (info, " /*"))
1026 return false;
1028 if (size != 0)
1030 sprintf (ab, " size %u", size);
1031 if (! append_type (info, ab))
1032 return false;
1034 if (tag != NULL)
1036 sprintf (ab, " id %u", id);
1037 if (! append_type (info, ab))
1038 return false;
1040 if (! append_type (info, " */"))
1041 return false;
1043 if (! append_type (info, "\n"))
1044 return false;
1046 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1048 return indent_type (info);
1051 /* Output the visibility of a field in a struct. */
1053 static bool
1054 pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
1056 const char *s = NULL;
1057 char *t;
1058 unsigned int len;
1060 assert (info->stack != NULL);
1062 if (info->stack->visibility == visibility)
1063 return true;
1065 switch (visibility)
1067 case DEBUG_VISIBILITY_PUBLIC:
1068 s = "public";
1069 break;
1070 case DEBUG_VISIBILITY_PRIVATE:
1071 s = "private";
1072 break;
1073 case DEBUG_VISIBILITY_PROTECTED:
1074 s = "protected";
1075 break;
1076 case DEBUG_VISIBILITY_IGNORE:
1077 s = "/* ignore */";
1078 break;
1079 default:
1080 abort ();
1081 return false;
1084 /* Trim off a trailing space in the struct string, to make the
1085 output look a bit better, then stick on the visibility string. */
1087 t = info->stack->type;
1088 len = strlen (t);
1089 assert (t[len - 1] == ' ');
1090 t[len - 1] = '\0';
1092 if (! append_type (info, s)
1093 || ! append_type (info, ":\n")
1094 || ! indent_type (info))
1095 return false;
1097 info->stack->visibility = visibility;
1099 return true;
1102 /* Add a field to a struct type. */
1104 static bool
1105 pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
1106 enum debug_visibility visibility)
1108 struct pr_handle *info = (struct pr_handle *) p;
1109 char ab[22];
1110 char *t;
1112 if (! substitute_type (info, name))
1113 return false;
1115 if (! append_type (info, "; /* "))
1116 return false;
1118 if (bitsize != 0)
1120 print_vma (bitsize, ab, true, false);
1121 if (! append_type (info, "bitsize ")
1122 || ! append_type (info, ab)
1123 || ! append_type (info, ", "))
1124 return false;
1127 print_vma (bitpos, ab, true, false);
1128 if (! append_type (info, "bitpos ")
1129 || ! append_type (info, ab)
1130 || ! append_type (info, " */\n")
1131 || ! indent_type (info))
1132 return false;
1134 t = pop_type (info);
1135 if (t == NULL)
1136 return false;
1138 bool ret = pr_fix_visibility (info, visibility) && append_type (info, t);
1139 free (t);
1140 return ret;
1143 /* Finish a struct type. */
1145 static bool
1146 pr_end_struct_type (void *p)
1148 struct pr_handle *info = (struct pr_handle *) p;
1149 char *s;
1151 assert (info->stack != NULL);
1152 assert (info->indent >= 2);
1154 info->indent -= 2;
1156 /* Change the trailing indentation to have a close brace. */
1157 s = info->stack->type + strlen (info->stack->type) - 2;
1158 assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1160 *s++ = '}';
1161 *s = '\0';
1163 return true;
1166 /* Start a class type. */
1168 static bool
1169 pr_start_class_type (void *p, const char *tag, unsigned int id,
1170 bool structp, unsigned int size,
1171 bool vptr, bool ownvptr)
1173 struct pr_handle *info = (struct pr_handle *) p;
1174 char *tv = NULL;
1175 bool ret = false;
1177 info->indent += 2;
1179 if (vptr && ! ownvptr)
1181 tv = pop_type (info);
1182 if (tv == NULL)
1183 return false;
1186 if (! push_type (info, structp ? "class " : "union class "))
1187 goto out;
1188 if (tag != NULL)
1190 if (! append_type (info, tag))
1191 goto out;
1193 else
1195 char idbuf[20];
1197 sprintf (idbuf, "%%anon%u", id);
1198 if (! append_type (info, idbuf))
1199 goto out;
1202 if (! append_type (info, " {"))
1203 goto out;
1204 if (size != 0 || vptr || ownvptr || tag != NULL)
1206 if (! append_type (info, " /*"))
1207 goto out;
1209 if (size != 0)
1211 char ab[20];
1213 sprintf (ab, "%u", size);
1214 if (! append_type (info, " size ")
1215 || ! append_type (info, ab))
1216 goto out;
1219 if (vptr)
1221 if (! append_type (info, " vtable "))
1222 goto out;
1223 if (ownvptr)
1225 if (! append_type (info, "self "))
1226 goto out;
1228 else
1230 if (! append_type (info, tv)
1231 || ! append_type (info, " "))
1232 goto out;
1236 if (tag != NULL)
1238 char ab[30];
1240 sprintf (ab, " id %u", id);
1241 if (! append_type (info, ab))
1242 goto out;
1245 if (! append_type (info, " */"))
1246 goto out;
1249 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1251 ret = append_type (info, "\n") && indent_type (info);
1252 out:
1253 free (tv);
1254 return ret;
1257 /* Add a static member to a class. */
1259 static bool
1260 pr_class_static_member (void *p, const char *name, const char *physname,
1261 enum debug_visibility visibility)
1263 struct pr_handle *info = (struct pr_handle *) p;
1264 char *t;
1266 if (! substitute_type (info, name))
1267 return false;
1269 if (! prepend_type (info, "static ")
1270 || ! append_type (info, "; /* ")
1271 || ! append_type (info, physname)
1272 || ! append_type (info, " */\n")
1273 || ! indent_type (info))
1274 return false;
1276 t = pop_type (info);
1277 if (t == NULL)
1278 return false;
1280 bool ret = pr_fix_visibility (info, visibility) && append_type (info, t);
1281 free (t);
1282 return ret;
1285 /* Add a base class to a class. */
1287 static bool
1288 pr_class_baseclass (void *p, bfd_vma bitpos, bool is_virtual,
1289 enum debug_visibility visibility)
1291 struct pr_handle *info = (struct pr_handle *) p;
1292 char *t;
1293 const char *prefix;
1294 char ab[22];
1295 char *s, *l, *n;
1297 assert (info->stack != NULL && info->stack->next != NULL);
1299 if (! substitute_type (info, ""))
1300 return false;
1302 t = pop_type (info);
1303 if (t == NULL)
1304 return false;
1306 /* Push it back on to take advantage of the prepend_type and
1307 append_type routines. */
1308 if (! push_type (info, t + (startswith (t, "class ")
1309 ? sizeof "class " - 1 : 0)))
1311 free (t);
1312 return false;
1314 free (t);
1316 if (is_virtual)
1318 if (! prepend_type (info, "virtual "))
1319 return false;
1322 switch (visibility)
1324 case DEBUG_VISIBILITY_PUBLIC:
1325 prefix = "public ";
1326 break;
1327 case DEBUG_VISIBILITY_PROTECTED:
1328 prefix = "protected ";
1329 break;
1330 case DEBUG_VISIBILITY_PRIVATE:
1331 prefix = "private ";
1332 break;
1333 default:
1334 prefix = "/* unknown visibility */ ";
1335 break;
1338 if (! prepend_type (info, prefix))
1339 return false;
1341 if (bitpos != 0)
1343 print_vma (bitpos, ab, true, false);
1344 if (! append_type (info, " /* bitpos ")
1345 || ! append_type (info, ab)
1346 || ! append_type (info, " */"))
1347 return false;
1350 /* Now the top of the stack is something like "public A / * bitpos
1351 10 * /". The next element on the stack is something like "class
1352 xx { / * size 8 * /\n...". We want to substitute the top of the
1353 stack in before the {. */
1354 s = strchr (info->stack->next->type, '{');
1355 assert (s != NULL);
1356 --s;
1358 /* If there is already a ':', then we already have a baseclass, and
1359 we must append this one after a comma. */
1360 for (l = info->stack->next->type; l != s; l++)
1361 if (*l == ':')
1362 break;
1363 if (! prepend_type (info, l == s ? " : " : ", "))
1364 return false;
1366 t = pop_type (info);
1367 if (t == NULL)
1368 return false;
1370 n = xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1371 memcpy (n, info->stack->type, s - info->stack->type);
1372 strcpy (n + (s - info->stack->type), t);
1373 strcat (n, s);
1375 free (info->stack->type);
1376 info->stack->type = n;
1378 free (t);
1380 return true;
1383 /* Start adding a method to a class. */
1385 static bool
1386 pr_class_start_method (void *p, const char *name)
1388 struct pr_handle *info = (struct pr_handle *) p;
1390 assert (info->stack != NULL);
1391 free (info->stack->method);
1392 info->stack->method = xstrdup (name);
1393 return true;
1396 /* Add a variant to a method. */
1398 static bool
1399 pr_class_method_variant (void *p, const char *physname,
1400 enum debug_visibility visibility,
1401 bool constp, bool volatilep,
1402 bfd_vma voffset, bool context)
1404 struct pr_handle *info = (struct pr_handle *) p;
1405 char *method_type;
1406 char *context_type;
1407 bool ret = false;
1409 assert (info->stack != NULL);
1410 assert (info->stack->next != NULL);
1412 /* Put the const and volatile qualifiers on the type. */
1413 if (volatilep)
1415 if (! append_type (info, " volatile"))
1416 return false;
1418 if (constp)
1420 if (! append_type (info, " const"))
1421 return false;
1424 /* Stick the name of the method into its type. */
1425 if (! substitute_type (info,
1426 (context
1427 ? info->stack->next->next->method
1428 : info->stack->next->method)))
1429 return false;
1431 /* Get the type. */
1432 method_type = pop_type (info);
1433 if (method_type == NULL)
1434 return false;
1436 /* Pull off the context type if there is one. */
1437 if (! context)
1438 context_type = NULL;
1439 else
1441 context_type = pop_type (info);
1442 if (context_type == NULL)
1443 goto out;
1446 /* Now the top of the stack is the class. */
1448 if (! pr_fix_visibility (info, visibility))
1449 goto out;
1451 if (! append_type (info, method_type)
1452 || ! append_type (info, " /* ")
1453 || ! append_type (info, physname)
1454 || ! append_type (info, " "))
1455 goto out;
1456 if (context || voffset != 0)
1458 char ab[22];
1460 if (context)
1462 if (! append_type (info, "context ")
1463 || ! append_type (info, context_type)
1464 || ! append_type (info, " "))
1465 goto out;
1467 print_vma (voffset, ab, true, false);
1468 if (! append_type (info, "voffset ")
1469 || ! append_type (info, ab))
1470 goto out;
1473 ret = append_type (info, " */;\n") && indent_type (info);
1474 out:
1475 free (method_type);
1476 free (context_type);
1477 return ret;
1480 /* Add a static variant to a method. */
1482 static bool
1483 pr_class_static_method_variant (void *p, const char *physname,
1484 enum debug_visibility visibility,
1485 bool constp, bool volatilep)
1487 struct pr_handle *info = (struct pr_handle *) p;
1488 char *method_type;
1490 assert (info->stack != NULL);
1491 assert (info->stack->next != NULL);
1492 assert (info->stack->next->method != NULL);
1494 /* Put the const and volatile qualifiers on the type. */
1495 if (volatilep)
1497 if (! append_type (info, " volatile"))
1498 return false;
1500 if (constp)
1502 if (! append_type (info, " const"))
1503 return false;
1506 /* Mark it as static. */
1507 if (! prepend_type (info, "static "))
1508 return false;
1510 /* Stick the name of the method into its type. */
1511 if (! substitute_type (info, info->stack->next->method))
1512 return false;
1514 /* Get the type. */
1515 method_type = pop_type (info);
1516 if (method_type == NULL)
1517 return false;
1519 /* Now the top of the stack is the class. */
1521 bool ret = (pr_fix_visibility (info, visibility)
1522 && append_type (info, method_type)
1523 && append_type (info, " /* ")
1524 && append_type (info, physname)
1525 && append_type (info, " */;\n")
1526 && indent_type (info));
1527 free (method_type);
1528 return ret;
1531 /* Finish up a method. */
1533 static bool
1534 pr_class_end_method (void *p)
1536 struct pr_handle *info = (struct pr_handle *) p;
1538 free (info->stack->method);
1539 info->stack->method = NULL;
1540 return true;
1543 /* Finish up a class. */
1545 static bool
1546 pr_end_class_type (void *p)
1548 return pr_end_struct_type (p);
1551 /* Push a type on the stack using a typedef name. */
1553 static bool
1554 pr_typedef_type (void *p, const char *name)
1556 struct pr_handle *info = (struct pr_handle *) p;
1558 return push_type (info, name);
1561 /* Push a type on the stack using a tag name. */
1563 static bool
1564 pr_tag_type (void *p, const char *name, unsigned int id,
1565 enum debug_type_kind kind)
1567 struct pr_handle *info = (struct pr_handle *) p;
1568 const char *t, *tag;
1569 char idbuf[22];
1571 switch (kind)
1573 case DEBUG_KIND_STRUCT:
1574 t = "struct ";
1575 break;
1576 case DEBUG_KIND_UNION:
1577 t = "union ";
1578 break;
1579 case DEBUG_KIND_ENUM:
1580 t = "enum ";
1581 break;
1582 case DEBUG_KIND_CLASS:
1583 t = "class ";
1584 break;
1585 case DEBUG_KIND_UNION_CLASS:
1586 t = "union class ";
1587 break;
1588 default:
1589 /* PR 25625: Corrupt input can trigger this case. */
1590 return false;
1593 if (! push_type (info, t))
1594 return false;
1595 if (name != NULL)
1596 tag = name;
1597 else
1599 sprintf (idbuf, "%%anon%u", id);
1600 tag = idbuf;
1603 if (! append_type (info, tag))
1604 return false;
1605 if (name != NULL && kind != DEBUG_KIND_ENUM)
1607 sprintf (idbuf, " /* id %u */", id);
1608 if (! append_type (info, idbuf))
1609 return false;
1612 return true;
1615 /* Output a typedef. */
1617 static bool
1618 pr_typdef (void *p, const char *name)
1620 struct pr_handle *info = (struct pr_handle *) p;
1621 char *s;
1623 if (! substitute_type (info, name))
1624 return false;
1626 s = pop_type (info);
1627 if (s == NULL)
1628 return false;
1630 indent (info);
1631 fprintf (info->f, "typedef %s;\n", s);
1633 free (s);
1635 return true;
1638 /* Output a tag. The tag should already be in the string on the
1639 stack, so all we have to do here is print it out. */
1641 static bool
1642 pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
1644 struct pr_handle *info = (struct pr_handle *) p;
1645 char *t;
1647 t = pop_type (info);
1648 if (t == NULL)
1649 return false;
1651 indent (info);
1652 fprintf (info->f, "%s;\n", t);
1654 free (t);
1656 return true;
1659 /* Output an integer constant. */
1661 static bool
1662 pr_int_constant (void *p, const char *name, bfd_vma val)
1664 struct pr_handle *info = (struct pr_handle *) p;
1665 char ab[22];
1667 indent (info);
1668 print_vma (val, ab, false, false);
1669 fprintf (info->f, "const int %s = %s;\n", name, ab);
1670 return true;
1673 /* Output a floating point constant. */
1675 static bool
1676 pr_float_constant (void *p, const char *name, double val)
1678 struct pr_handle *info = (struct pr_handle *) p;
1680 indent (info);
1681 fprintf (info->f, "const double %s = %g;\n", name, val);
1682 return true;
1685 /* Output a typed constant. */
1687 static bool
1688 pr_typed_constant (void *p, const char *name, bfd_vma val)
1690 struct pr_handle *info = (struct pr_handle *) p;
1691 char *t;
1692 char ab[22];
1694 t = pop_type (info);
1695 if (t == NULL)
1696 return false;
1698 indent (info);
1699 print_vma (val, ab, false, false);
1700 fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1702 free (t);
1704 return true;
1707 /* Output a variable. */
1709 static bool
1710 pr_variable (void *p, const char *name, enum debug_var_kind kind,
1711 bfd_vma val)
1713 struct pr_handle *info = (struct pr_handle *) p;
1714 char *t;
1715 char ab[22];
1717 if (! substitute_type (info, name))
1718 return false;
1720 t = pop_type (info);
1721 if (t == NULL)
1722 return false;
1724 indent (info);
1725 switch (kind)
1727 case DEBUG_STATIC:
1728 case DEBUG_LOCAL_STATIC:
1729 fprintf (info->f, "static ");
1730 break;
1731 case DEBUG_REGISTER:
1732 fprintf (info->f, "register ");
1733 break;
1734 default:
1735 break;
1737 print_vma (val, ab, true, true);
1738 fprintf (info->f, "%s /* %s */;\n", t, ab);
1740 free (t);
1742 return true;
1745 /* Start outputting a function. */
1747 static bool
1748 pr_start_function (void *p, const char *name, bool global)
1750 struct pr_handle *info = (struct pr_handle *) p;
1751 char *t;
1753 if (! substitute_type (info, name))
1754 return false;
1756 t = pop_type (info);
1757 if (t == NULL)
1758 return false;
1760 indent (info);
1761 if (! global)
1762 fprintf (info->f, "static ");
1763 fprintf (info->f, "%s (", t);
1765 free (t);
1767 info->parameter = 1;
1769 return true;
1772 /* Output a function parameter. */
1774 static bool
1775 pr_function_parameter (void *p, const char *name,
1776 enum debug_parm_kind kind, bfd_vma val)
1778 struct pr_handle *info = (struct pr_handle *) p;
1779 char *t;
1780 char ab[22];
1782 if (kind == DEBUG_PARM_REFERENCE
1783 || kind == DEBUG_PARM_REF_REG)
1785 if (! pr_reference_type (p))
1786 return false;
1789 if (! substitute_type (info, name))
1790 return false;
1792 t = pop_type (info);
1793 if (t == NULL)
1794 return false;
1796 if (info->parameter != 1)
1797 fprintf (info->f, ", ");
1799 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1800 fprintf (info->f, "register ");
1802 print_vma (val, ab, true, true);
1803 fprintf (info->f, "%s /* %s */", t, ab);
1805 free (t);
1807 ++info->parameter;
1809 return true;
1812 /* Start writing out a block. */
1814 static bool
1815 pr_start_block (void *p, bfd_vma addr)
1817 struct pr_handle *info = (struct pr_handle *) p;
1818 char ab[22];
1820 if (info->parameter > 0)
1822 fprintf (info->f, ")\n");
1823 info->parameter = 0;
1826 indent (info);
1827 print_vma (addr, ab, true, true);
1828 fprintf (info->f, "{ /* %s */\n", ab);
1830 info->indent += 2;
1832 return true;
1835 /* Write out line number information. */
1837 static bool
1838 pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
1840 struct pr_handle *info = (struct pr_handle *) p;
1841 char ab[22];
1843 indent (info);
1844 print_vma (addr, ab, true, true);
1845 fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1847 return true;
1850 /* Finish writing out a block. */
1852 static bool
1853 pr_end_block (void *p, bfd_vma addr)
1855 struct pr_handle *info = (struct pr_handle *) p;
1856 char ab[22];
1858 info->indent -= 2;
1860 indent (info);
1861 print_vma (addr, ab, true, true);
1862 fprintf (info->f, "} /* %s */\n", ab);
1864 return true;
1867 /* Finish writing out a function. */
1869 static bool
1870 pr_end_function (void *p ATTRIBUTE_UNUSED)
1872 return true;
1875 /* Tags style generation functions start here. */
1877 /* Variables for address to line translation. */
1878 static bfd_vma pc;
1879 static const char *filename;
1880 static const char *functionname;
1881 static unsigned int line;
1882 static bool found;
1884 /* Look for an address in a section. This is called via
1885 bfd_map_over_sections. */
1887 static void
1888 find_address_in_section (bfd *abfd, asection *section, void *data)
1890 bfd_vma vma;
1891 bfd_size_type size;
1892 asymbol **syms = (asymbol **) data;
1894 if (found)
1895 return;
1897 if ((bfd_section_flags (section) & SEC_ALLOC) == 0)
1898 return;
1900 vma = bfd_section_vma (section);
1901 if (pc < vma)
1902 return;
1904 size = bfd_section_size (section);
1905 if (pc >= vma + size)
1906 return;
1908 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
1909 &filename, &functionname, &line);
1912 static void
1913 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
1915 pc = bfd_scan_vma (addr_hex, NULL, 16);
1916 found = false;
1917 bfd_map_over_sections (abfd, find_address_in_section, syms);
1919 if (! found)
1920 fprintf (f, "??");
1921 else
1922 fprintf (f, "%u", line);
1925 /* Start a new compilation unit. */
1927 static bool
1928 tg_start_compilation_unit (void * p, const char *fname ATTRIBUTE_UNUSED)
1930 struct pr_handle *info = (struct pr_handle *) p;
1932 free (info->filename);
1933 /* Should it be relative? best way to do it here?. */
1934 info->filename = xstrdup (fname);
1936 return true;
1939 /* Start a source file within a compilation unit. */
1941 static bool
1942 tg_start_source (void *p, const char *fname)
1944 struct pr_handle *info = (struct pr_handle *) p;
1946 free (info->filename);
1947 /* Should it be relative? best way to do it here?. */
1948 info->filename = xstrdup (fname);
1950 return true;
1953 /* Push an enum type onto the type stack. */
1955 static bool
1956 tg_enum_type (void *p, const char *tag, const char **names,
1957 bfd_signed_vma *values)
1959 struct pr_handle *info = (struct pr_handle *) p;
1960 unsigned int i;
1961 const char *name;
1962 char ab[22];
1964 if (! pr_enum_type (p, tag, names, values))
1965 return false;
1967 name = tag ? tag : "unknown";
1968 /* Generate an entry for the enum. */
1969 if (tag)
1970 fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
1971 info->filename, info->stack->type);
1973 /* Generate entries for the values. */
1974 if (names != NULL)
1976 for (i = 0; names[i] != NULL; i++)
1978 print_vma (values[i], ab, false, false);
1979 fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
1980 names[i], info->filename, name, ab);
1984 return true;
1987 /* Start accumulating a struct type. */
1989 static bool
1990 tg_start_struct_type (void *p, const char *tag, unsigned int id,
1991 bool structp,
1992 unsigned int size ATTRIBUTE_UNUSED)
1994 struct pr_handle *info = (struct pr_handle *) p;
1995 const char *name;
1996 char idbuf[20];
1998 if (tag != NULL)
1999 name = tag;
2000 else
2002 name = idbuf;
2003 sprintf (idbuf, "%%anon%u", id);
2006 if (! push_type (info, name))
2007 return false;
2009 info->stack->flavor = structp ? "struct" : "union";
2011 fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2012 info->stack->flavor[0]);
2014 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2016 return indent_type (info);
2019 /* Output the visibility of a field in a struct. */
2021 static bool
2022 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2024 assert (info->stack != NULL);
2026 if (info->stack->visibility == visibility)
2027 return true;
2029 assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2031 info->stack->visibility = visibility;
2033 return true;
2036 /* Add a field to a struct type. */
2038 static bool
2039 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2040 bfd_vma bitsize ATTRIBUTE_UNUSED,
2041 enum debug_visibility visibility)
2043 struct pr_handle *info = (struct pr_handle *) p;
2044 char *t;
2046 t = pop_type (info);
2047 if (t == NULL)
2048 return false;
2050 if (! tg_fix_visibility (info, visibility))
2052 free (t);
2053 return false;
2056 /* It happens, a bug? */
2057 if (! name[0])
2059 free (t);
2060 return true;
2063 fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2064 name, info->filename, t, info->stack->flavor, info->stack->type,
2065 visibility_name (visibility));
2067 free (t);
2069 return true;
2072 /* Finish a struct type. */
2074 static bool
2075 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2077 assert (((struct pr_handle *) p)->stack != NULL);
2079 return true;
2082 /* Start a class type. */
2084 static bool
2085 tg_start_class_type (void *p, const char *tag, unsigned int id,
2086 bool structp, unsigned int size,
2087 bool vptr, bool ownvptr)
2089 struct pr_handle *info = (struct pr_handle *) p;
2090 char *tv = NULL;
2091 const char *name;
2092 char idbuf[20];
2093 bool ret = false;
2095 info->indent += 2;
2097 if (vptr && ! ownvptr)
2099 tv = pop_type (info);
2100 if (tv == NULL)
2101 return false;
2104 if (tag != NULL)
2105 name = tag;
2106 else
2108 sprintf (idbuf, "%%anon%u", id);
2109 name = idbuf;
2112 if (! push_type (info, name))
2113 goto out;
2115 info->stack->flavor = structp ? "class" : "union class";
2116 free (info->stack->parents);
2117 info->stack->parents = NULL;
2119 if (size != 0 || vptr || ownvptr || tag != NULL)
2121 if (vptr)
2123 if (! append_type (info, " vtable "))
2124 goto out;
2125 if (ownvptr)
2127 if (! append_type (info, "self "))
2128 goto out;
2130 else
2132 if (! append_type (info, tv)
2133 || ! append_type (info, " "))
2134 goto out;
2139 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2141 ret = true;
2142 out:
2143 free (tv);
2144 return ret;
2147 /* Add a static member to a class. */
2149 static bool
2150 tg_class_static_member (void *p, const char *name,
2151 const char *physname ATTRIBUTE_UNUSED,
2152 enum debug_visibility visibility)
2154 struct pr_handle *info = (struct pr_handle *) p;
2155 char *t;
2156 int len_var, len_class;
2157 char *full_name;
2159 len_var = strlen (name);
2160 len_class = strlen (info->stack->next->type);
2161 full_name = xmalloc (len_var + len_class + 3);
2162 sprintf (full_name, "%s::%s", info->stack->next->type, name);
2164 if (! substitute_type (info, full_name))
2166 free (full_name);
2167 return false;
2170 if (! prepend_type (info, "static "))
2172 free (full_name);
2173 return false;
2176 t = pop_type (info);
2177 if (t == NULL)
2179 free (full_name);
2180 return false;
2183 if (! tg_fix_visibility (info, visibility))
2185 free (t);
2186 free (full_name);
2187 return false;
2190 fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2191 name, info->filename, t, info->stack->type,
2192 visibility_name (visibility));
2193 free (t);
2194 free (full_name);
2196 return true;
2199 /* Add a base class to a class. */
2201 static bool
2202 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2203 bool is_virtual, enum debug_visibility visibility)
2205 struct pr_handle *info = (struct pr_handle *) p;
2206 char *t;
2207 const char *prefix;
2209 assert (info->stack != NULL && info->stack->next != NULL);
2211 t = pop_type (info);
2212 if (t == NULL)
2213 return false;
2215 /* Push it back on to take advantage of the prepend_type and
2216 append_type routines. */
2217 if (! push_type (info, t + (startswith (t, "class ")
2218 ? sizeof "class " - 1 : 0)))
2220 free (t);
2221 return false;
2223 free (t);
2225 if (is_virtual)
2227 if (! prepend_type (info, "virtual "))
2228 return false;
2231 switch (visibility)
2233 case DEBUG_VISIBILITY_PUBLIC:
2234 prefix = "public ";
2235 break;
2236 case DEBUG_VISIBILITY_PROTECTED:
2237 prefix = "protected ";
2238 break;
2239 case DEBUG_VISIBILITY_PRIVATE:
2240 prefix = "private ";
2241 break;
2242 default:
2243 prefix = "/* unknown visibility */ ";
2244 break;
2247 if (! prepend_type (info, prefix))
2248 return false;
2250 t = pop_type (info);
2251 if (t == NULL)
2252 return false;
2254 bool ret = ((!info->stack->parents || append_parent (info, ", "))
2255 && append_parent (info, t));
2256 free (t);
2257 return ret;
2260 /* Add a variant to a method. */
2262 static bool
2263 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2264 enum debug_visibility visibility,
2265 bool constp, bool volatilep,
2266 bfd_vma voffset ATTRIBUTE_UNUSED,
2267 bool context)
2269 struct pr_handle *info = (struct pr_handle *) p;
2270 char *method_type;
2271 char *context_type;
2272 char *method_name;
2274 assert (info->stack != NULL);
2275 assert (info->stack->next != NULL);
2277 /* Put the const and volatile qualifiers on the type. */
2278 if (volatilep)
2280 if (! append_type (info, " volatile"))
2281 return false;
2283 if (constp)
2285 if (! append_type (info, " const"))
2286 return false;
2289 method_name = strdup (context ? info->stack->next->next->method
2290 : info->stack->next->method);
2292 /* Stick the name of the method into its type. */
2293 if (! substitute_type (info, method_name))
2295 free (method_name);
2296 return false;
2299 /* Get the type. */
2300 method_type = pop_type (info);
2301 if (method_type == NULL)
2303 free (method_name);
2304 return false;
2307 /* Pull off the context type if there is one. */
2308 if (! context)
2309 context_type = NULL;
2310 else
2312 context_type = pop_type (info);
2313 if (context_type == NULL)
2315 free (method_type);
2316 free (method_name);
2317 return false;
2321 /* Now the top of the stack is the class. */
2322 if (! tg_fix_visibility (info, visibility))
2324 free (method_type);
2325 free (method_name);
2326 free (context_type);
2327 return false;
2330 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2331 method_name, info->filename, method_type, info->stack->type);
2332 free (method_type);
2333 free (method_name);
2334 free (context_type);
2336 return true;
2339 /* Add a static variant to a method. */
2341 static bool
2342 tg_class_static_method_variant (void *p,
2343 const char *physname ATTRIBUTE_UNUSED,
2344 enum debug_visibility visibility,
2345 bool constp, bool volatilep)
2347 struct pr_handle *info = (struct pr_handle *) p;
2348 char *method_type;
2349 char *method_name;
2351 assert (info->stack != NULL);
2352 assert (info->stack->next != NULL);
2353 assert (info->stack->next->method != NULL);
2355 /* Put the const and volatile qualifiers on the type. */
2356 if (volatilep)
2358 if (! append_type (info, " volatile"))
2359 return false;
2361 if (constp)
2363 if (! append_type (info, " const"))
2364 return false;
2367 /* Mark it as static. */
2368 if (! prepend_type (info, "static "))
2369 return false;
2371 method_name = strdup (info->stack->next->method);
2372 /* Stick the name of the method into its type. */
2373 if (! substitute_type (info, info->stack->next->method))
2375 free (method_name);
2376 return false;
2379 /* Get the type. */
2380 method_type = pop_type (info);
2381 if (method_type == NULL)
2383 free (method_name);
2384 return false;
2387 /* Now the top of the stack is the class. */
2388 if (! tg_fix_visibility (info, visibility))
2390 free (method_type);
2391 free (method_name);
2392 return false;
2395 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2396 method_name, info->filename, method_type, info->stack->type,
2397 visibility_name (visibility));
2398 free (method_type);
2399 free (method_name);
2401 return true;
2404 /* Finish up a class. */
2406 static bool
2407 tg_end_class_type (void *p)
2409 struct pr_handle *info = (struct pr_handle *) p;
2411 fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2412 info->filename, info->stack->flavor);
2413 if (info->stack->parents)
2415 fprintf (info->f, "\tinherits:%s", info->stack->parents);
2416 free (info->stack->parents);
2417 info->stack->parents = NULL;
2419 fputc ('\n', info->f);
2421 return tg_end_struct_type (p);
2424 /* Push a type on the stack using a tag name. */
2426 static bool
2427 tg_tag_type (void *p, const char *name, unsigned int id,
2428 enum debug_type_kind kind)
2430 struct pr_handle *info = (struct pr_handle *) p;
2431 const char *t, *tag;
2432 char idbuf[20];
2434 switch (kind)
2436 case DEBUG_KIND_STRUCT:
2437 t = "struct ";
2438 break;
2439 case DEBUG_KIND_UNION:
2440 t = "union ";
2441 break;
2442 case DEBUG_KIND_ENUM:
2443 t = "enum ";
2444 break;
2445 case DEBUG_KIND_CLASS:
2446 t = "class ";
2447 break;
2448 case DEBUG_KIND_UNION_CLASS:
2449 t = "union class ";
2450 break;
2451 default:
2452 abort ();
2453 return false;
2456 if (! push_type (info, t))
2457 return false;
2458 if (name != NULL)
2459 tag = name;
2460 else
2462 sprintf (idbuf, "%%anon%u", id);
2463 tag = idbuf;
2466 if (! append_type (info, tag))
2467 return false;
2469 return true;
2472 /* Output a typedef. */
2474 static bool
2475 tg_typdef (void *p, const char *name)
2477 struct pr_handle *info = (struct pr_handle *) p;
2478 char *s;
2480 s = pop_type (info);
2481 if (s == NULL)
2482 return false;
2484 fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2485 info->filename, s);
2487 free (s);
2489 return true;
2492 /* Output a tag. The tag should already be in the string on the
2493 stack, so all we have to do here is print it out. */
2495 static bool
2496 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2498 struct pr_handle *info = (struct pr_handle *) p;
2499 char *t;
2501 t = pop_type (info);
2502 if (t == NULL)
2503 return false;
2504 free (t);
2506 return true;
2509 /* Output an integer constant. */
2511 static bool
2512 tg_int_constant (void *p, const char *name, bfd_vma val)
2514 struct pr_handle *info = (struct pr_handle *) p;
2515 char ab[22];
2517 indent (info);
2518 print_vma (val, ab, false, false);
2519 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2520 name, info->filename, ab);
2521 return true;
2524 /* Output a floating point constant. */
2526 static bool
2527 tg_float_constant (void *p, const char *name, double val)
2529 struct pr_handle *info = (struct pr_handle *) p;
2531 indent (info);
2532 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2533 name, info->filename, val);
2534 return true;
2537 /* Output a typed constant. */
2539 static bool
2540 tg_typed_constant (void *p, const char *name, bfd_vma val)
2542 struct pr_handle *info = (struct pr_handle *) p;
2543 char *t;
2544 char ab[22];
2546 t = pop_type (info);
2547 if (t == NULL)
2548 return false;
2550 indent (info);
2551 print_vma (val, ab, false, false);
2552 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2553 name, info->filename, t, ab);
2555 free (t);
2557 return true;
2560 /* Output a variable. */
2562 static bool
2563 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2564 bfd_vma val ATTRIBUTE_UNUSED)
2566 struct pr_handle *info = (struct pr_handle *) p;
2567 char *t, *dname, *from_class;
2569 t = pop_type (info);
2570 if (t == NULL)
2571 return false;
2573 dname = NULL;
2574 if (info->demangler)
2575 dname = info->demangler (info->abfd, name, demangle_flags);
2577 from_class = NULL;
2578 if (dname != NULL)
2580 char *sep;
2581 sep = strstr (dname, "::");
2582 if (sep)
2584 *sep = 0;
2585 name = sep + 2;
2586 from_class = dname;
2588 else
2589 /* Obscure types as vts and type_info nodes. */
2590 name = dname;
2593 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2595 switch (kind)
2597 case DEBUG_STATIC:
2598 case DEBUG_LOCAL_STATIC:
2599 fprintf (info->f, "\tfile:");
2600 break;
2601 case DEBUG_REGISTER:
2602 fprintf (info->f, "\tregister:");
2603 break;
2604 default:
2605 break;
2608 if (from_class)
2609 fprintf (info->f, "\tclass:%s", from_class);
2611 if (dname)
2612 free (dname);
2614 fprintf (info->f, "\n");
2616 free (t);
2618 return true;
2621 /* Start outputting a function. */
2623 static bool
2624 tg_start_function (void *p, const char *name, bool global)
2626 struct pr_handle *info = (struct pr_handle *) p;
2627 char *dname;
2629 if (! global)
2630 info->stack->flavor = "static";
2631 else
2632 info->stack->flavor = NULL;
2634 dname = NULL;
2635 if (info->demangler)
2636 dname = info->demangler (info->abfd, name, demangle_flags);
2638 if (! substitute_type (info, dname ? dname : name))
2639 return false;
2641 free (info->stack->method);
2642 info->stack->method = NULL;
2643 if (dname != NULL)
2645 char *sep;
2646 sep = strstr (dname, "::");
2647 if (sep)
2649 info->stack->method = dname;
2650 dname = NULL;
2651 *sep = 0;
2652 name = sep + 2;
2654 else
2656 info->stack->method = xstrdup ("");
2657 name = dname;
2659 sep = strchr (name, '(');
2660 if (sep)
2661 *sep = 0;
2662 /* Obscure functions as type_info function. */
2665 free (info->stack->parents);
2666 info->stack->parents = strdup (name);
2667 free (dname);
2669 if (! info->stack->method && ! append_type (info, "("))
2670 return false;
2672 info->parameter = 1;
2674 return true;
2677 /* Output a function parameter. */
2679 static bool
2680 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2681 bfd_vma val ATTRIBUTE_UNUSED)
2683 struct pr_handle *info = (struct pr_handle *) p;
2684 char *t;
2686 if (kind == DEBUG_PARM_REFERENCE
2687 || kind == DEBUG_PARM_REF_REG)
2689 if (! pr_reference_type (p))
2690 return false;
2693 if (! substitute_type (info, name))
2694 return false;
2696 t = pop_type (info);
2697 if (t == NULL)
2698 return false;
2700 if (! info->stack->method)
2702 if (info->parameter != 1 && ! append_type (info, ", "))
2704 free (t);
2705 return false;
2708 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2709 if (! append_type (info, "register "))
2711 free (t);
2712 return false;
2715 if (! append_type (info, t))
2717 free (t);
2718 return false;
2722 free (t);
2724 ++info->parameter;
2726 return true;
2729 /* Start writing out a block. */
2731 static bool
2732 tg_start_block (void *p, bfd_vma addr)
2734 struct pr_handle *info = (struct pr_handle *) p;
2735 char ab[22], kind, *partof;
2736 char *t;
2737 bool local;
2739 if (info->parameter > 0)
2741 info->parameter = 0;
2743 /* Delayed name. */
2744 fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2745 free (info->stack->parents);
2746 info->stack->parents = NULL;
2748 print_vma (addr, ab, true, true);
2749 translate_addresses (info->abfd, ab, info->f, info->syms);
2750 local = info->stack->flavor != NULL;
2751 if (info->stack->method && *info->stack->method)
2753 kind = 'm';
2754 partof = (char *) info->stack->method;
2756 else
2758 kind = 'f';
2759 partof = NULL;
2760 if (! info->stack->method && ! append_type (info, ")"))
2761 return false;
2763 t = pop_type (info);
2764 if (t == NULL)
2765 return false;
2766 fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2767 free (t);
2768 if (local)
2769 fputs ("\tfile:", info->f);
2770 if (partof)
2771 fprintf (info->f, "\tclass:%s", partof);
2772 fputc ('\n', info->f);
2773 free (info->stack->method);
2774 info->stack->method = NULL;
2777 return true;
2780 /* Write out line number information. */
2782 static bool
2783 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
2784 unsigned long lineno ATTRIBUTE_UNUSED,
2785 bfd_vma addr ATTRIBUTE_UNUSED)
2787 return true;
2790 /* Finish writing out a block. */
2792 static bool
2793 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2795 return true;
2798 /* Convert the visibility value into a human readable name. */
2800 static const char *
2801 visibility_name (enum debug_visibility visibility)
2803 const char *s;
2805 switch (visibility)
2807 case DEBUG_VISIBILITY_PUBLIC:
2808 s = "public";
2809 break;
2810 case DEBUG_VISIBILITY_PRIVATE:
2811 s = "private";
2812 break;
2813 case DEBUG_VISIBILITY_PROTECTED:
2814 s = "protected";
2815 break;
2816 case DEBUG_VISIBILITY_IGNORE:
2817 s = "/* ignore */";
2818 break;
2819 default:
2820 abort ();
2821 return false;
2823 return s;