Hackfix and re-enable strtoull and wcstoull, see bug #3798.
[sdcc.git] / sdcc / support / sdbinutils / binutils / prdbg.c
blobd6cbab8578be09262750b267da4008983b2b3763
1 /* prdbg.c -- Print out generic debugging information.
2 Copyright (C) 1995-2022 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 const 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;
74 /* How many parents contains parents. */
75 int num_parents;
78 static void indent (struct pr_handle *);
79 static bool push_type (struct pr_handle *, const char *);
80 static bool prepend_type (struct pr_handle *, const char *);
81 static bool append_type (struct pr_handle *, const char *);
82 static bool substitute_type (struct pr_handle *, const char *);
83 static bool indent_type (struct pr_handle *);
84 static char *pop_type (struct pr_handle *);
85 static void print_vma (bfd_vma, char *, bool, bool);
86 static bool pr_fix_visibility (struct pr_handle *, enum debug_visibility);
87 static bool pr_start_compilation_unit (void *, const char *);
88 static bool pr_start_source (void *, const char *);
89 static bool pr_empty_type (void *);
90 static bool pr_void_type (void *);
91 static bool pr_int_type (void *, unsigned int, bool);
92 static bool pr_float_type (void *, unsigned int);
93 static bool pr_complex_type (void *, unsigned int);
94 static bool pr_bool_type (void *, unsigned int);
95 static bool pr_enum_type
96 (void *, const char *, const char **, bfd_signed_vma *);
97 static bool pr_pointer_type (void *);
98 static bool pr_function_type (void *, int, bool);
99 static bool pr_reference_type (void *);
100 static bool pr_range_type (void *, bfd_signed_vma, bfd_signed_vma);
101 static bool pr_array_type (void *, bfd_signed_vma, bfd_signed_vma, bool);
102 static bool pr_set_type (void *, bool);
103 static bool pr_offset_type (void *);
104 static bool pr_method_type (void *, bool, int, bool);
105 static bool pr_const_type (void *);
106 static bool pr_volatile_type (void *);
107 static bool pr_start_struct_type
108 (void *, const char *, unsigned int, bool, unsigned int);
109 static bool pr_struct_field
110 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
111 static bool pr_end_struct_type (void *);
112 static bool pr_start_class_type
113 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
114 static bool pr_class_static_member
115 (void *, const char *, const char *, enum debug_visibility);
116 static bool pr_class_baseclass
117 (void *, bfd_vma, bool, enum debug_visibility);
118 static bool pr_class_start_method (void *, const char *);
119 static bool pr_class_method_variant
120 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
121 static bool pr_class_static_method_variant
122 (void *, const char *, enum debug_visibility, bool, bool);
123 static bool pr_class_end_method (void *);
124 static bool pr_end_class_type (void *);
125 static bool pr_typedef_type (void *, const char *);
126 static bool pr_tag_type
127 (void *, const char *, unsigned int, enum debug_type_kind);
128 static bool pr_typdef (void *, const char *);
129 static bool pr_tag (void *, const char *);
130 static bool pr_int_constant (void *, const char *, bfd_vma);
131 static bool pr_float_constant (void *, const char *, double);
132 static bool pr_typed_constant (void *, const char *, bfd_vma);
133 static bool pr_variable (void *, const char *, enum debug_var_kind, bfd_vma);
134 static bool pr_start_function (void *, const char *, bool);
135 static bool pr_function_parameter
136 (void *, const char *, enum debug_parm_kind, bfd_vma);
137 static bool pr_start_block (void *, bfd_vma);
138 static bool pr_end_block (void *, bfd_vma);
139 static bool pr_end_function (void *);
140 static bool pr_lineno (void *, const char *, unsigned long, bfd_vma);
141 static bool append_parent (struct pr_handle *, const char *);
142 /* Only used by tg_ code. */
143 static bool tg_fix_visibility
144 (struct pr_handle *, enum debug_visibility);
145 static void find_address_in_section (bfd *, asection *, void *);
146 static void translate_addresses (bfd *, char *, FILE *, asymbol **);
147 static const char *visibility_name (enum debug_visibility);
148 /* Tags style replacements. */
149 static bool tg_start_compilation_unit (void *, const char *);
150 static bool tg_start_source (void *, const char *);
151 static bool tg_enum_type
152 (void *, const char *, const char **, bfd_signed_vma *);
153 static bool tg_start_struct_type
154 (void *, const char *, unsigned int, bool, unsigned int);
155 static bool pr_struct_field
156 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
157 static bool tg_struct_field
158 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
159 static bool tg_struct_field
160 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
161 static bool tg_end_struct_type (void *);
162 static bool tg_start_class_type
163 (void *, const char *, unsigned int, bool, unsigned int, bool, bool);
164 static bool tg_class_static_member
165 (void *, const char *, const char *, enum debug_visibility);
166 static bool tg_class_baseclass (void *, bfd_vma, bool, enum debug_visibility);
167 static bool tg_class_method_variant
168 (void *, const char *, enum debug_visibility, bool, bool, bfd_vma, bool);
169 static bool tg_class_static_method_variant
170 (void *, const char *, enum debug_visibility, bool, bool);
171 static bool tg_end_class_type (void *);
172 static bool tg_tag_type
173 (void *, const char *, unsigned int, enum debug_type_kind);
174 static bool tg_typdef (void *, const char *);
175 static bool tg_tag (void *, const char *);
176 static bool tg_int_constant (void *, const char *, bfd_vma);
177 static bool tg_float_constant (void *, const char *, double);
178 static bool tg_typed_constant (void *, const char *, bfd_vma);
179 static bool tg_variable (void *, const char *, enum debug_var_kind, bfd_vma);
180 static bool tg_start_function (void *, const char *, bool);
181 static bool tg_function_parameter
182 (void *, const char *, enum debug_parm_kind, bfd_vma);
183 static bool tg_start_block (void *, bfd_vma);
184 static bool tg_end_block (void *, bfd_vma);
185 static bool tg_lineno (void *, const char *, unsigned long, bfd_vma);
187 static const struct debug_write_fns pr_fns =
189 pr_start_compilation_unit,
190 pr_start_source,
191 pr_empty_type,
192 pr_void_type,
193 pr_int_type,
194 pr_float_type,
195 pr_complex_type,
196 pr_bool_type,
197 pr_enum_type,
198 pr_pointer_type,
199 pr_function_type,
200 pr_reference_type,
201 pr_range_type,
202 pr_array_type,
203 pr_set_type,
204 pr_offset_type,
205 pr_method_type,
206 pr_const_type,
207 pr_volatile_type,
208 pr_start_struct_type,
209 pr_struct_field,
210 pr_end_struct_type,
211 pr_start_class_type,
212 pr_class_static_member,
213 pr_class_baseclass,
214 pr_class_start_method,
215 pr_class_method_variant,
216 pr_class_static_method_variant,
217 pr_class_end_method,
218 pr_end_class_type,
219 pr_typedef_type,
220 pr_tag_type,
221 pr_typdef,
222 pr_tag,
223 pr_int_constant,
224 pr_float_constant,
225 pr_typed_constant,
226 pr_variable,
227 pr_start_function,
228 pr_function_parameter,
229 pr_start_block,
230 pr_end_block,
231 pr_end_function,
232 pr_lineno
235 static const struct debug_write_fns tg_fns =
237 tg_start_compilation_unit,
238 tg_start_source,
239 pr_empty_type, /* Same, push_type. */
240 pr_void_type, /* Same, push_type. */
241 pr_int_type, /* Same, push_type. */
242 pr_float_type, /* Same, push_type. */
243 pr_complex_type, /* Same, push_type. */
244 pr_bool_type, /* Same, push_type. */
245 tg_enum_type,
246 pr_pointer_type, /* Same, changes to pointer. */
247 pr_function_type, /* Same, push_type. */
248 pr_reference_type, /* Same, changes to reference. */
249 pr_range_type, /* FIXME: What's that?. */
250 pr_array_type, /* Same, push_type. */
251 pr_set_type, /* FIXME: What's that?. */
252 pr_offset_type, /* FIXME: What's that?. */
253 pr_method_type, /* Same. */
254 pr_const_type, /* Same, changes to const. */
255 pr_volatile_type, /* Same, changes to volatile. */
256 tg_start_struct_type,
257 tg_struct_field,
258 tg_end_struct_type,
259 tg_start_class_type,
260 tg_class_static_member,
261 tg_class_baseclass,
262 pr_class_start_method, /* Same, remembers that's a method. */
263 tg_class_method_variant,
264 tg_class_static_method_variant,
265 pr_class_end_method, /* Same, forgets that's a method. */
266 tg_end_class_type,
267 pr_typedef_type, /* Same, just push type. */
268 tg_tag_type,
269 tg_typdef,
270 tg_tag,
271 tg_int_constant, /* Untested. */
272 tg_float_constant, /* Untested. */
273 tg_typed_constant, /* Untested. */
274 tg_variable,
275 tg_start_function,
276 tg_function_parameter,
277 tg_start_block,
278 tg_end_block,
279 pr_end_function, /* Same, does nothing. */
280 tg_lineno
283 static int demangle_flags = DMGL_ANSI | DMGL_PARAMS;
285 /* Print out the generic debugging information recorded in dhandle. */
287 bool
288 print_debugging_info (FILE *f, void *dhandle, bfd *abfd, asymbol **syms,
289 char * (*demangler) (struct bfd *, const char *, int),
290 bool as_tags)
292 struct pr_handle info;
294 info.f = f;
295 info.indent = 0;
296 info.stack = NULL;
297 info.parameter = 0;
298 info.filename = NULL;
299 info.abfd = abfd;
300 info.syms = syms;
301 info.demangler = demangler;
303 if (as_tags)
305 fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
306 fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
307 fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
308 fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
311 return as_tags ? debug_write (dhandle, &tg_fns, (void *) & info)
312 : debug_write (dhandle, &pr_fns, (void *) & info);
315 /* Indent to the current indentation level. */
317 static void
318 indent (struct pr_handle *info)
320 unsigned int i;
322 for (i = 0; i < info->indent; i++)
323 putc (' ', info->f);
326 /* Push a type on the type stack. */
328 static bool
329 push_type (struct pr_handle *info, const char *type)
331 struct pr_stack *n;
333 if (type == NULL)
334 return false;
336 n = (struct pr_stack *) xmalloc (sizeof *n);
337 memset (n, 0, sizeof *n);
339 n->type = xstrdup (type);
340 n->visibility = DEBUG_VISIBILITY_IGNORE;
341 n->method = NULL;
342 n->next = info->stack;
343 info->stack = n;
345 return true;
348 /* Prepend a string onto the type on the top of the type stack. */
350 static bool
351 prepend_type (struct pr_handle *info, const char *s)
353 char *n;
355 assert (info->stack != NULL);
357 n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
358 sprintf (n, "%s%s", s, info->stack->type);
359 free (info->stack->type);
360 info->stack->type = n;
362 return true;
365 /* Append a string to the type on the top of the type stack. */
367 static bool
368 append_type (struct pr_handle *info, const char *s)
370 unsigned int len;
372 if (s == NULL)
373 return false;
375 assert (info->stack != NULL);
377 len = strlen (info->stack->type);
378 info->stack->type = (char *) xrealloc (info->stack->type,
379 len + strlen (s) + 1);
380 strcpy (info->stack->type + len, s);
382 return true;
385 /* Append a string to the parents on the top of the type stack. */
387 static bool
388 append_parent (struct pr_handle *info, const char *s)
390 unsigned int len;
392 if (s == NULL)
393 return false;
395 assert (info->stack != NULL);
397 len = info->stack->parents ? strlen (info->stack->parents) : 0;
398 info->stack->parents = (char *) xrealloc (info->stack->parents,
399 len + strlen (s) + 1);
400 strcpy (info->stack->parents + len, s);
402 return true;
405 /* We use an underscore to indicate where the name should go in a type
406 string. This function substitutes a string for the underscore. If
407 there is no underscore, the name follows the type. */
409 static bool
410 substitute_type (struct pr_handle *info, const char *s)
412 char *u;
414 assert (info->stack != NULL);
416 u = strchr (info->stack->type, '|');
417 if (u != NULL)
419 char *n;
421 n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
423 memcpy (n, info->stack->type, u - info->stack->type);
424 strcpy (n + (u - info->stack->type), s);
425 strcat (n, u + 1);
427 free (info->stack->type);
428 info->stack->type = n;
430 return true;
433 if (strchr (s, '|') != NULL
434 && (strchr (info->stack->type, '{') != NULL
435 || strchr (info->stack->type, '(') != NULL))
437 if (! prepend_type (info, "(")
438 || ! append_type (info, ")"))
439 return false;
442 if (*s == '\0')
443 return true;
445 return (append_type (info, " ")
446 && append_type (info, s));
449 /* Indent the type at the top of the stack by appending spaces. */
451 static bool
452 indent_type (struct pr_handle *info)
454 unsigned int i;
456 for (i = 0; i < info->indent; i++)
458 if (! append_type (info, " "))
459 return false;
462 return true;
465 /* Pop a type from the type stack. */
467 static char *
468 pop_type (struct pr_handle *info)
470 struct pr_stack *o;
471 char *ret;
473 assert (info->stack != NULL);
475 o = info->stack;
476 info->stack = o->next;
477 ret = o->type;
478 free (o);
480 return ret;
483 /* Print a VMA value into a string. */
485 static void
486 print_vma (bfd_vma vma, char *buf, bool unsignedp, bool hexp)
488 if (sizeof (vma) <= sizeof (unsigned long))
490 if (hexp)
491 sprintf (buf, "0x%lx", (unsigned long) vma);
492 else if (unsignedp)
493 sprintf (buf, "%lu", (unsigned long) vma);
494 else
495 sprintf (buf, "%ld", (long) vma);
497 #if BFD_HOST_64BIT_LONG_LONG
498 else if (sizeof (vma) <= sizeof (unsigned long long))
500 #ifndef __MSVCRT__
501 if (hexp)
502 sprintf (buf, "0x%llx", (unsigned long long) vma);
503 else if (unsignedp)
504 sprintf (buf, "%llu", (unsigned long long) vma);
505 else
506 sprintf (buf, "%lld", (long long) vma);
507 #else
508 if (hexp)
509 sprintf (buf, "0x%I64x", (unsigned long long) vma);
510 else if (unsignedp)
511 sprintf (buf, "%I64u", (unsigned long long) vma);
512 else
513 sprintf (buf, "%I64d", (long long) vma);
514 #endif
516 #endif
517 else
519 buf[0] = '0';
520 buf[1] = 'x';
521 sprintf_vma (buf + 2, vma);
525 /* Start a new compilation unit. */
527 static bool
528 pr_start_compilation_unit (void *p, const char *filename)
530 struct pr_handle *info = (struct pr_handle *) p;
532 assert (info->indent == 0);
534 fprintf (info->f, "%s:\n", filename);
536 return true;
539 /* Start a source file within a compilation unit. */
541 static bool
542 pr_start_source (void *p, const char *filename)
544 struct pr_handle *info = (struct pr_handle *) p;
546 assert (info->indent == 0);
548 fprintf (info->f, " %s:\n", filename);
550 return true;
553 /* Push an empty type onto the type stack. */
555 static bool
556 pr_empty_type (void *p)
558 struct pr_handle *info = (struct pr_handle *) p;
560 return push_type (info, "<undefined>");
563 /* Push a void type onto the type stack. */
565 static bool
566 pr_void_type (void *p)
568 struct pr_handle *info = (struct pr_handle *) p;
570 return push_type (info, "void");
573 /* Push an integer type onto the type stack. */
575 static bool
576 pr_int_type (void *p, unsigned int size, bool unsignedp)
578 struct pr_handle *info = (struct pr_handle *) p;
579 char ab[40];
581 sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
582 return push_type (info, ab);
585 /* Push a floating type onto the type stack. */
587 static bool
588 pr_float_type (void *p, unsigned int size)
590 struct pr_handle *info = (struct pr_handle *) p;
591 char ab[40];
593 if (size == 4)
594 return push_type (info, "float");
595 else if (size == 8)
596 return push_type (info, "double");
598 sprintf (ab, "float%d", size * 8);
599 return push_type (info, ab);
602 /* Push a complex type onto the type stack. */
604 static bool
605 pr_complex_type (void *p, unsigned int size)
607 struct pr_handle *info = (struct pr_handle *) p;
609 if (! pr_float_type (p, size))
610 return false;
612 return prepend_type (info, "complex ");
615 /* Push a bool type onto the type stack. */
617 static bool
618 pr_bool_type (void *p, unsigned int size)
620 struct pr_handle *info = (struct pr_handle *) p;
621 char ab[40];
623 sprintf (ab, "bool%d", size * 8);
625 return push_type (info, ab);
628 /* Push an enum type onto the type stack. */
630 static bool
631 pr_enum_type (void *p, const char *tag, const char **names,
632 bfd_signed_vma *values)
634 struct pr_handle *info = (struct pr_handle *) p;
635 unsigned int i;
636 bfd_signed_vma val;
638 if (! push_type (info, "enum "))
639 return false;
640 if (tag != NULL)
642 if (! append_type (info, tag)
643 || ! append_type (info, " "))
644 return false;
646 if (! append_type (info, "{ "))
647 return false;
649 if (names == NULL)
651 if (! append_type (info, "/* undefined */"))
652 return false;
654 else
656 val = 0;
657 for (i = 0; names[i] != NULL; i++)
659 if (i > 0)
661 if (! append_type (info, ", "))
662 return false;
665 if (! append_type (info, names[i]))
666 return false;
668 if (values[i] != val)
670 char ab[22];
672 print_vma (values[i], ab, false, false);
673 if (! append_type (info, " = ")
674 || ! append_type (info, ab))
675 return false;
676 val = values[i];
679 ++val;
683 return append_type (info, " }");
686 /* Turn the top type on the stack into a pointer. */
688 static bool
689 pr_pointer_type (void *p)
691 struct pr_handle *info = (struct pr_handle *) p;
692 char *s;
694 assert (info->stack != NULL);
696 s = strchr (info->stack->type, '|');
697 if (s != NULL && s[1] == '[')
698 return substitute_type (info, "(*|)");
699 return substitute_type (info, "*|");
702 /* Turn the top type on the stack into a function returning that type. */
704 static bool
705 pr_function_type (void *p, int argcount, bool varargs)
707 struct pr_handle *info = (struct pr_handle *) p;
708 char **arg_types;
709 unsigned int len;
710 char *s;
712 assert (info->stack != NULL);
714 len = 10;
716 if (argcount <= 0)
718 arg_types = NULL;
719 len += 15;
721 else
723 int i;
725 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
726 for (i = argcount - 1; i >= 0; i--)
728 if (! substitute_type (info, ""))
730 free (arg_types);
731 return false;
733 arg_types[i] = pop_type (info);
734 if (arg_types[i] == NULL)
736 free (arg_types);
737 return false;
739 len += strlen (arg_types[i]) + 2;
741 if (varargs)
742 len += 5;
745 /* Now the return type is on the top of the stack. */
747 s = (char *) xmalloc (len);
748 strcpy (s, "(|) (");
750 if (argcount < 0)
751 strcat (s, "/* unknown */");
752 else
754 int i;
756 for (i = 0; i < argcount; i++)
758 if (i > 0)
759 strcat (s, ", ");
760 strcat (s, arg_types[i]);
762 if (varargs)
764 if (i > 0)
765 strcat (s, ", ");
766 strcat (s, "...");
768 if (argcount > 0)
769 free (arg_types);
772 strcat (s, ")");
774 if (! substitute_type (info, s))
775 return false;
777 free (s);
779 return true;
782 /* Turn the top type on the stack into a reference to that type. */
784 static bool
785 pr_reference_type (void *p)
787 struct pr_handle *info = (struct pr_handle *) p;
789 assert (info->stack != NULL);
791 return substitute_type (info, "&|");
794 /* Make a range type. */
796 static bool
797 pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
799 struct pr_handle *info = (struct pr_handle *) p;
800 char abl[22], abu[22];
802 assert (info->stack != NULL);
804 if (! substitute_type (info, ""))
805 return false;
807 print_vma (lower, abl, false, false);
808 print_vma (upper, abu, false, false);
810 return (prepend_type (info, "range (")
811 && append_type (info, "):")
812 && append_type (info, abl)
813 && append_type (info, ":")
814 && append_type (info, abu));
817 /* Make an array type. */
819 static bool
820 pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
821 bool stringp)
823 struct pr_handle *info = (struct pr_handle *) p;
824 char *range_type;
825 char abl[22], abu[22], ab[50];
827 range_type = pop_type (info);
828 if (range_type == NULL)
829 return false;
831 if (lower == 0)
833 if (upper == -1)
834 sprintf (ab, "|[]");
835 else
837 print_vma (upper + 1, abu, false, false);
838 sprintf (ab, "|[%s]", abu);
841 else
843 print_vma (lower, abl, false, false);
844 print_vma (upper, abu, false, false);
845 sprintf (ab, "|[%s:%s]", abl, abu);
848 if (! substitute_type (info, ab))
849 return false;
851 if (strcmp (range_type, "int") != 0)
853 if (! append_type (info, ":")
854 || ! append_type (info, range_type))
855 return false;
858 if (stringp)
860 if (! append_type (info, " /* string */"))
861 return false;
864 return true;
867 /* Make a set type. */
869 static bool
870 pr_set_type (void *p, bool bitstringp)
872 struct pr_handle *info = (struct pr_handle *) p;
874 if (! substitute_type (info, ""))
875 return false;
877 if (! prepend_type (info, "set { ")
878 || ! append_type (info, " }"))
879 return false;
881 if (bitstringp)
883 if (! append_type (info, "/* bitstring */"))
884 return false;
887 return true;
890 /* Make an offset type. */
892 static bool
893 pr_offset_type (void *p)
895 struct pr_handle *info = (struct pr_handle *) p;
896 char *t;
898 if (! substitute_type (info, ""))
899 return false;
901 t = pop_type (info);
902 if (t == NULL)
903 return false;
905 return (substitute_type (info, "")
906 && prepend_type (info, " ")
907 && prepend_type (info, t)
908 && append_type (info, "::|"));
911 /* Make a method type. */
913 static bool
914 pr_method_type (void *p, bool domain, int argcount, bool varargs)
916 struct pr_handle *info = (struct pr_handle *) p;
917 unsigned int len;
918 char *domain_type;
919 char **arg_types;
920 char *s;
922 len = 10;
924 if (! domain)
925 domain_type = NULL;
926 else
928 if (! substitute_type (info, ""))
929 return false;
930 domain_type = pop_type (info);
931 if (domain_type == NULL)
932 return false;
933 if (startswith (domain_type, "class ")
934 && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
935 domain_type += sizeof "class " - 1;
936 else if (startswith (domain_type, "union class ")
937 && (strchr (domain_type + sizeof "union class " - 1, ' ')
938 == NULL))
939 domain_type += sizeof "union class " - 1;
940 len += strlen (domain_type);
943 if (argcount <= 0)
945 arg_types = NULL;
946 len += 15;
948 else
950 int i;
952 arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
953 for (i = argcount - 1; i >= 0; i--)
955 if (! substitute_type (info, ""))
957 free (arg_types);
958 return false;
960 arg_types[i] = pop_type (info);
961 if (arg_types[i] == NULL)
963 free (arg_types);
964 return false;
966 len += strlen (arg_types[i]) + 2;
968 if (varargs)
969 len += 5;
972 /* Now the return type is on the top of the stack. */
974 s = (char *) xmalloc (len);
975 if (! domain)
976 *s = '\0';
977 else
978 strcpy (s, domain_type);
979 strcat (s, "::| (");
981 if (argcount < 0)
982 strcat (s, "/* unknown */");
983 else
985 int i;
987 for (i = 0; i < argcount; i++)
989 if (i > 0)
990 strcat (s, ", ");
991 strcat (s, arg_types[i]);
993 if (varargs)
995 if (i > 0)
996 strcat (s, ", ");
997 strcat (s, "...");
999 if (argcount > 0)
1000 free (arg_types);
1003 strcat (s, ")");
1005 if (! substitute_type (info, s))
1006 return false;
1008 free (s);
1010 return true;
1013 /* Make a const qualified type. */
1015 static bool
1016 pr_const_type (void *p)
1018 struct pr_handle *info = (struct pr_handle *) p;
1020 return substitute_type (info, "const |");
1023 /* Make a volatile qualified type. */
1025 static bool
1026 pr_volatile_type (void *p)
1028 struct pr_handle *info = (struct pr_handle *) p;
1030 return substitute_type (info, "volatile |");
1033 /* Start accumulating a struct type. */
1035 static bool
1036 pr_start_struct_type (void *p, const char *tag, unsigned int id,
1037 bool structp, unsigned int size)
1039 struct pr_handle *info = (struct pr_handle *) p;
1041 info->indent += 2;
1043 if (! push_type (info, structp ? "struct " : "union "))
1044 return false;
1045 if (tag != NULL)
1047 if (! append_type (info, tag))
1048 return false;
1050 else
1052 char idbuf[20];
1054 sprintf (idbuf, "%%anon%u", id);
1055 if (! append_type (info, idbuf))
1056 return false;
1059 if (! append_type (info, " {"))
1060 return false;
1061 if (size != 0 || tag != NULL)
1063 char ab[30];
1065 if (! append_type (info, " /*"))
1066 return false;
1068 if (size != 0)
1070 sprintf (ab, " size %u", size);
1071 if (! append_type (info, ab))
1072 return false;
1074 if (tag != NULL)
1076 sprintf (ab, " id %u", id);
1077 if (! append_type (info, ab))
1078 return false;
1080 if (! append_type (info, " */"))
1081 return false;
1083 if (! append_type (info, "\n"))
1084 return false;
1086 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1088 return indent_type (info);
1091 /* Output the visibility of a field in a struct. */
1093 static bool
1094 pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
1096 const char *s = NULL;
1097 char *t;
1098 unsigned int len;
1100 assert (info->stack != NULL);
1102 if (info->stack->visibility == visibility)
1103 return true;
1105 switch (visibility)
1107 case DEBUG_VISIBILITY_PUBLIC:
1108 s = "public";
1109 break;
1110 case DEBUG_VISIBILITY_PRIVATE:
1111 s = "private";
1112 break;
1113 case DEBUG_VISIBILITY_PROTECTED:
1114 s = "protected";
1115 break;
1116 case DEBUG_VISIBILITY_IGNORE:
1117 s = "/* ignore */";
1118 break;
1119 default:
1120 abort ();
1121 return false;
1124 /* Trim off a trailing space in the struct string, to make the
1125 output look a bit better, then stick on the visibility string. */
1127 t = info->stack->type;
1128 len = strlen (t);
1129 assert (t[len - 1] == ' ');
1130 t[len - 1] = '\0';
1132 if (! append_type (info, s)
1133 || ! append_type (info, ":\n")
1134 || ! indent_type (info))
1135 return false;
1137 info->stack->visibility = visibility;
1139 return true;
1142 /* Add a field to a struct type. */
1144 static bool
1145 pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
1146 enum debug_visibility visibility)
1148 struct pr_handle *info = (struct pr_handle *) p;
1149 char ab[22];
1150 char *t;
1152 if (! substitute_type (info, name))
1153 return false;
1155 if (! append_type (info, "; /* "))
1156 return false;
1158 if (bitsize != 0)
1160 print_vma (bitsize, ab, true, false);
1161 if (! append_type (info, "bitsize ")
1162 || ! append_type (info, ab)
1163 || ! append_type (info, ", "))
1164 return false;
1167 print_vma (bitpos, ab, true, false);
1168 if (! append_type (info, "bitpos ")
1169 || ! append_type (info, ab)
1170 || ! append_type (info, " */\n")
1171 || ! indent_type (info))
1172 return false;
1174 t = pop_type (info);
1175 if (t == NULL)
1176 return false;
1178 if (! pr_fix_visibility (info, visibility))
1179 return false;
1181 return append_type (info, t);
1184 /* Finish a struct type. */
1186 static bool
1187 pr_end_struct_type (void *p)
1189 struct pr_handle *info = (struct pr_handle *) p;
1190 char *s;
1192 assert (info->stack != NULL);
1193 assert (info->indent >= 2);
1195 info->indent -= 2;
1197 /* Change the trailing indentation to have a close brace. */
1198 s = info->stack->type + strlen (info->stack->type) - 2;
1199 assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1201 *s++ = '}';
1202 *s = '\0';
1204 return true;
1207 /* Start a class type. */
1209 static bool
1210 pr_start_class_type (void *p, const char *tag, unsigned int id,
1211 bool structp, unsigned int size,
1212 bool vptr, bool ownvptr)
1214 struct pr_handle *info = (struct pr_handle *) p;
1215 char *tv = NULL;
1217 info->indent += 2;
1219 if (vptr && ! ownvptr)
1221 tv = pop_type (info);
1222 if (tv == NULL)
1223 return false;
1226 if (! push_type (info, structp ? "class " : "union class "))
1227 return false;
1228 if (tag != NULL)
1230 if (! append_type (info, tag))
1231 return false;
1233 else
1235 char idbuf[20];
1237 sprintf (idbuf, "%%anon%u", id);
1238 if (! append_type (info, idbuf))
1239 return false;
1242 if (! append_type (info, " {"))
1243 return false;
1244 if (size != 0 || vptr || ownvptr || tag != NULL)
1246 if (! append_type (info, " /*"))
1247 return false;
1249 if (size != 0)
1251 char ab[20];
1253 sprintf (ab, "%u", size);
1254 if (! append_type (info, " size ")
1255 || ! append_type (info, ab))
1256 return false;
1259 if (vptr)
1261 if (! append_type (info, " vtable "))
1262 return false;
1263 if (ownvptr)
1265 if (! append_type (info, "self "))
1266 return false;
1268 else
1270 if (! append_type (info, tv)
1271 || ! append_type (info, " "))
1272 return false;
1276 if (tag != NULL)
1278 char ab[30];
1280 sprintf (ab, " id %u", id);
1281 if (! append_type (info, ab))
1282 return false;
1285 if (! append_type (info, " */"))
1286 return false;
1289 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1291 return (append_type (info, "\n")
1292 && indent_type (info));
1295 /* Add a static member to a class. */
1297 static bool
1298 pr_class_static_member (void *p, const char *name, const char *physname,
1299 enum debug_visibility visibility)
1301 struct pr_handle *info = (struct pr_handle *) p;
1302 char *t;
1304 if (! substitute_type (info, name))
1305 return false;
1307 if (! prepend_type (info, "static ")
1308 || ! append_type (info, "; /* ")
1309 || ! append_type (info, physname)
1310 || ! append_type (info, " */\n")
1311 || ! indent_type (info))
1312 return false;
1314 t = pop_type (info);
1315 if (t == NULL)
1316 return false;
1318 if (! pr_fix_visibility (info, visibility))
1319 return false;
1321 return append_type (info, t);
1324 /* Add a base class to a class. */
1326 static bool
1327 pr_class_baseclass (void *p, bfd_vma bitpos, bool is_virtual,
1328 enum debug_visibility visibility)
1330 struct pr_handle *info = (struct pr_handle *) p;
1331 char *t;
1332 const char *prefix;
1333 char ab[22];
1334 char *s, *l, *n;
1336 assert (info->stack != NULL && info->stack->next != NULL);
1338 if (! substitute_type (info, ""))
1339 return false;
1341 t = pop_type (info);
1342 if (t == NULL)
1343 return false;
1345 if (startswith (t, "class "))
1346 t += sizeof "class " - 1;
1348 /* Push it back on to take advantage of the prepend_type and
1349 append_type routines. */
1350 if (! push_type (info, t))
1351 return false;
1353 if (is_virtual)
1355 if (! prepend_type (info, "virtual "))
1356 return false;
1359 switch (visibility)
1361 case DEBUG_VISIBILITY_PUBLIC:
1362 prefix = "public ";
1363 break;
1364 case DEBUG_VISIBILITY_PROTECTED:
1365 prefix = "protected ";
1366 break;
1367 case DEBUG_VISIBILITY_PRIVATE:
1368 prefix = "private ";
1369 break;
1370 default:
1371 prefix = "/* unknown visibility */ ";
1372 break;
1375 if (! prepend_type (info, prefix))
1376 return false;
1378 if (bitpos != 0)
1380 print_vma (bitpos, ab, true, false);
1381 if (! append_type (info, " /* bitpos ")
1382 || ! append_type (info, ab)
1383 || ! append_type (info, " */"))
1384 return false;
1387 /* Now the top of the stack is something like "public A / * bitpos
1388 10 * /". The next element on the stack is something like "class
1389 xx { / * size 8 * /\n...". We want to substitute the top of the
1390 stack in before the {. */
1391 s = strchr (info->stack->next->type, '{');
1392 assert (s != NULL);
1393 --s;
1395 /* If there is already a ':', then we already have a baseclass, and
1396 we must append this one after a comma. */
1397 for (l = info->stack->next->type; l != s; l++)
1398 if (*l == ':')
1399 break;
1400 if (! prepend_type (info, l == s ? " : " : ", "))
1401 return false;
1403 t = pop_type (info);
1404 if (t == NULL)
1405 return false;
1407 n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1408 memcpy (n, info->stack->type, s - info->stack->type);
1409 strcpy (n + (s - info->stack->type), t);
1410 strcat (n, s);
1412 free (info->stack->type);
1413 info->stack->type = n;
1415 free (t);
1417 return true;
1420 /* Start adding a method to a class. */
1422 static bool
1423 pr_class_start_method (void *p, const char *name)
1425 struct pr_handle *info = (struct pr_handle *) p;
1427 assert (info->stack != NULL);
1428 info->stack->method = name;
1429 return true;
1432 /* Add a variant to a method. */
1434 static bool
1435 pr_class_method_variant (void *p, const char *physname,
1436 enum debug_visibility visibility,
1437 bool constp, bool volatilep,
1438 bfd_vma voffset, bool context)
1440 struct pr_handle *info = (struct pr_handle *) p;
1441 char *method_type;
1442 char *context_type;
1444 assert (info->stack != NULL);
1445 assert (info->stack->next != NULL);
1447 /* Put the const and volatile qualifiers on the type. */
1448 if (volatilep)
1450 if (! append_type (info, " volatile"))
1451 return false;
1453 if (constp)
1455 if (! append_type (info, " const"))
1456 return false;
1459 /* Stick the name of the method into its type. */
1460 if (! substitute_type (info,
1461 (context
1462 ? info->stack->next->next->method
1463 : info->stack->next->method)))
1464 return false;
1466 /* Get the type. */
1467 method_type = pop_type (info);
1468 if (method_type == NULL)
1469 return false;
1471 /* Pull off the context type if there is one. */
1472 if (! context)
1473 context_type = NULL;
1474 else
1476 context_type = pop_type (info);
1477 if (context_type == NULL)
1478 return false;
1481 /* Now the top of the stack is the class. */
1483 if (! pr_fix_visibility (info, visibility))
1484 return false;
1486 if (! append_type (info, method_type)
1487 || ! append_type (info, " /* ")
1488 || ! append_type (info, physname)
1489 || ! append_type (info, " "))
1490 return false;
1491 if (context || voffset != 0)
1493 char ab[22];
1495 if (context)
1497 if (! append_type (info, "context ")
1498 || ! append_type (info, context_type)
1499 || ! append_type (info, " "))
1500 return false;
1502 print_vma (voffset, ab, true, false);
1503 if (! append_type (info, "voffset ")
1504 || ! append_type (info, ab))
1505 return false;
1508 return (append_type (info, " */;\n")
1509 && indent_type (info));
1512 /* Add a static variant to a method. */
1514 static bool
1515 pr_class_static_method_variant (void *p, const char *physname,
1516 enum debug_visibility visibility,
1517 bool constp, bool volatilep)
1519 struct pr_handle *info = (struct pr_handle *) p;
1520 char *method_type;
1522 assert (info->stack != NULL);
1523 assert (info->stack->next != NULL);
1524 assert (info->stack->next->method != NULL);
1526 /* Put the const and volatile qualifiers on the type. */
1527 if (volatilep)
1529 if (! append_type (info, " volatile"))
1530 return false;
1532 if (constp)
1534 if (! append_type (info, " const"))
1535 return false;
1538 /* Mark it as static. */
1539 if (! prepend_type (info, "static "))
1540 return false;
1542 /* Stick the name of the method into its type. */
1543 if (! substitute_type (info, info->stack->next->method))
1544 return false;
1546 /* Get the type. */
1547 method_type = pop_type (info);
1548 if (method_type == NULL)
1549 return false;
1551 /* Now the top of the stack is the class. */
1553 if (! pr_fix_visibility (info, visibility))
1554 return false;
1556 return (append_type (info, method_type)
1557 && append_type (info, " /* ")
1558 && append_type (info, physname)
1559 && append_type (info, " */;\n")
1560 && indent_type (info));
1563 /* Finish up a method. */
1565 static bool
1566 pr_class_end_method (void *p)
1568 struct pr_handle *info = (struct pr_handle *) p;
1570 info->stack->method = NULL;
1571 return true;
1574 /* Finish up a class. */
1576 static bool
1577 pr_end_class_type (void *p)
1579 return pr_end_struct_type (p);
1582 /* Push a type on the stack using a typedef name. */
1584 static bool
1585 pr_typedef_type (void *p, const char *name)
1587 struct pr_handle *info = (struct pr_handle *) p;
1589 return push_type (info, name);
1592 /* Push a type on the stack using a tag name. */
1594 static bool
1595 pr_tag_type (void *p, const char *name, unsigned int id,
1596 enum debug_type_kind kind)
1598 struct pr_handle *info = (struct pr_handle *) p;
1599 const char *t, *tag;
1600 char idbuf[22];
1602 switch (kind)
1604 case DEBUG_KIND_STRUCT:
1605 t = "struct ";
1606 break;
1607 case DEBUG_KIND_UNION:
1608 t = "union ";
1609 break;
1610 case DEBUG_KIND_ENUM:
1611 t = "enum ";
1612 break;
1613 case DEBUG_KIND_CLASS:
1614 t = "class ";
1615 break;
1616 case DEBUG_KIND_UNION_CLASS:
1617 t = "union class ";
1618 break;
1619 default:
1620 /* PR 25625: Corrupt input can trigger this case. */
1621 return false;
1624 if (! push_type (info, t))
1625 return false;
1626 if (name != NULL)
1627 tag = name;
1628 else
1630 sprintf (idbuf, "%%anon%u", id);
1631 tag = idbuf;
1634 if (! append_type (info, tag))
1635 return false;
1636 if (name != NULL && kind != DEBUG_KIND_ENUM)
1638 sprintf (idbuf, " /* id %u */", id);
1639 if (! append_type (info, idbuf))
1640 return false;
1643 return true;
1646 /* Output a typedef. */
1648 static bool
1649 pr_typdef (void *p, const char *name)
1651 struct pr_handle *info = (struct pr_handle *) p;
1652 char *s;
1654 if (! substitute_type (info, name))
1655 return false;
1657 s = pop_type (info);
1658 if (s == NULL)
1659 return false;
1661 indent (info);
1662 fprintf (info->f, "typedef %s;\n", s);
1664 free (s);
1666 return true;
1669 /* Output a tag. The tag should already be in the string on the
1670 stack, so all we have to do here is print it out. */
1672 static bool
1673 pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
1675 struct pr_handle *info = (struct pr_handle *) p;
1676 char *t;
1678 t = pop_type (info);
1679 if (t == NULL)
1680 return false;
1682 indent (info);
1683 fprintf (info->f, "%s;\n", t);
1685 free (t);
1687 return true;
1690 /* Output an integer constant. */
1692 static bool
1693 pr_int_constant (void *p, const char *name, bfd_vma val)
1695 struct pr_handle *info = (struct pr_handle *) p;
1696 char ab[22];
1698 indent (info);
1699 print_vma (val, ab, false, false);
1700 fprintf (info->f, "const int %s = %s;\n", name, ab);
1701 return true;
1704 /* Output a floating point constant. */
1706 static bool
1707 pr_float_constant (void *p, const char *name, double val)
1709 struct pr_handle *info = (struct pr_handle *) p;
1711 indent (info);
1712 fprintf (info->f, "const double %s = %g;\n", name, val);
1713 return true;
1716 /* Output a typed constant. */
1718 static bool
1719 pr_typed_constant (void *p, const char *name, bfd_vma val)
1721 struct pr_handle *info = (struct pr_handle *) p;
1722 char *t;
1723 char ab[22];
1725 t = pop_type (info);
1726 if (t == NULL)
1727 return false;
1729 indent (info);
1730 print_vma (val, ab, false, false);
1731 fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1733 free (t);
1735 return true;
1738 /* Output a variable. */
1740 static bool
1741 pr_variable (void *p, const char *name, enum debug_var_kind kind,
1742 bfd_vma val)
1744 struct pr_handle *info = (struct pr_handle *) p;
1745 char *t;
1746 char ab[22];
1748 if (! substitute_type (info, name))
1749 return false;
1751 t = pop_type (info);
1752 if (t == NULL)
1753 return false;
1755 indent (info);
1756 switch (kind)
1758 case DEBUG_STATIC:
1759 case DEBUG_LOCAL_STATIC:
1760 fprintf (info->f, "static ");
1761 break;
1762 case DEBUG_REGISTER:
1763 fprintf (info->f, "register ");
1764 break;
1765 default:
1766 break;
1768 print_vma (val, ab, true, true);
1769 fprintf (info->f, "%s /* %s */;\n", t, ab);
1771 free (t);
1773 return true;
1776 /* Start outputting a function. */
1778 static bool
1779 pr_start_function (void *p, const char *name, bool global)
1781 struct pr_handle *info = (struct pr_handle *) p;
1782 char *t;
1784 if (! substitute_type (info, name))
1785 return false;
1787 t = pop_type (info);
1788 if (t == NULL)
1789 return false;
1791 indent (info);
1792 if (! global)
1793 fprintf (info->f, "static ");
1794 fprintf (info->f, "%s (", t);
1796 info->parameter = 1;
1798 return true;
1801 /* Output a function parameter. */
1803 static bool
1804 pr_function_parameter (void *p, const char *name,
1805 enum debug_parm_kind kind, bfd_vma val)
1807 struct pr_handle *info = (struct pr_handle *) p;
1808 char *t;
1809 char ab[22];
1811 if (kind == DEBUG_PARM_REFERENCE
1812 || kind == DEBUG_PARM_REF_REG)
1814 if (! pr_reference_type (p))
1815 return false;
1818 if (! substitute_type (info, name))
1819 return false;
1821 t = pop_type (info);
1822 if (t == NULL)
1823 return false;
1825 if (info->parameter != 1)
1826 fprintf (info->f, ", ");
1828 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1829 fprintf (info->f, "register ");
1831 print_vma (val, ab, true, true);
1832 fprintf (info->f, "%s /* %s */", t, ab);
1834 free (t);
1836 ++info->parameter;
1838 return true;
1841 /* Start writing out a block. */
1843 static bool
1844 pr_start_block (void *p, bfd_vma addr)
1846 struct pr_handle *info = (struct pr_handle *) p;
1847 char ab[22];
1849 if (info->parameter > 0)
1851 fprintf (info->f, ")\n");
1852 info->parameter = 0;
1855 indent (info);
1856 print_vma (addr, ab, true, true);
1857 fprintf (info->f, "{ /* %s */\n", ab);
1859 info->indent += 2;
1861 return true;
1864 /* Write out line number information. */
1866 static bool
1867 pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
1869 struct pr_handle *info = (struct pr_handle *) p;
1870 char ab[22];
1872 indent (info);
1873 print_vma (addr, ab, true, true);
1874 fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1876 return true;
1879 /* Finish writing out a block. */
1881 static bool
1882 pr_end_block (void *p, bfd_vma addr)
1884 struct pr_handle *info = (struct pr_handle *) p;
1885 char ab[22];
1887 info->indent -= 2;
1889 indent (info);
1890 print_vma (addr, ab, true, true);
1891 fprintf (info->f, "} /* %s */\n", ab);
1893 return true;
1896 /* Finish writing out a function. */
1898 static bool
1899 pr_end_function (void *p ATTRIBUTE_UNUSED)
1901 return true;
1904 /* Tags style generation functions start here. */
1906 /* Variables for address to line translation. */
1907 static bfd_vma pc;
1908 static const char *filename;
1909 static const char *functionname;
1910 static unsigned int line;
1911 static bool found;
1913 /* Look for an address in a section. This is called via
1914 bfd_map_over_sections. */
1916 static void
1917 find_address_in_section (bfd *abfd, asection *section, void *data)
1919 bfd_vma vma;
1920 bfd_size_type size;
1921 asymbol **syms = (asymbol **) data;
1923 if (found)
1924 return;
1926 if ((bfd_section_flags (section) & SEC_ALLOC) == 0)
1927 return;
1929 vma = bfd_section_vma (section);
1930 if (pc < vma)
1931 return;
1933 size = bfd_section_size (section);
1934 if (pc >= vma + size)
1935 return;
1937 found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
1938 &filename, &functionname, &line);
1941 static void
1942 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
1944 pc = bfd_scan_vma (addr_hex, NULL, 16);
1945 found = false;
1946 bfd_map_over_sections (abfd, find_address_in_section, syms);
1948 if (! found)
1949 fprintf (f, "??");
1950 else
1951 fprintf (f, "%u", line);
1954 /* Start a new compilation unit. */
1956 static bool
1957 tg_start_compilation_unit (void * p, const char *fname ATTRIBUTE_UNUSED)
1959 struct pr_handle *info = (struct pr_handle *) p;
1961 free (info->filename);
1962 /* Should it be relative? best way to do it here?. */
1963 info->filename = strdup (fname);
1965 return true;
1968 /* Start a source file within a compilation unit. */
1970 static bool
1971 tg_start_source (void *p, const char *fname)
1973 struct pr_handle *info = (struct pr_handle *) p;
1975 free (info->filename);
1976 /* Should it be relative? best way to do it here?. */
1977 info->filename = strdup (fname);
1979 return true;
1982 /* Push an enum type onto the type stack. */
1984 static bool
1985 tg_enum_type (void *p, const char *tag, const char **names,
1986 bfd_signed_vma *values)
1988 struct pr_handle *info = (struct pr_handle *) p;
1989 unsigned int i;
1990 const char *name;
1991 char ab[22];
1993 if (! pr_enum_type (p, tag, names, values))
1994 return false;
1996 name = tag ? tag : "unknown";
1997 /* Generate an entry for the enum. */
1998 if (tag)
1999 fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
2000 info->filename, info->stack->type);
2002 /* Generate entries for the values. */
2003 if (names != NULL)
2005 for (i = 0; names[i] != NULL; i++)
2007 print_vma (values[i], ab, false, false);
2008 fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
2009 names[i], info->filename, name, ab);
2013 return true;
2016 /* Start accumulating a struct type. */
2018 static bool
2019 tg_start_struct_type (void *p, const char *tag, unsigned int id,
2020 bool structp,
2021 unsigned int size ATTRIBUTE_UNUSED)
2023 struct pr_handle *info = (struct pr_handle *) p;
2024 const char *name;
2025 char idbuf[20];
2027 if (tag != NULL)
2028 name = tag;
2029 else
2031 name = idbuf;
2032 sprintf (idbuf, "%%anon%u", id);
2035 if (! push_type (info, name))
2036 return false;
2038 info->stack->flavor = structp ? "struct" : "union";
2040 fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2041 info->stack->flavor[0]);
2043 info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2045 return indent_type (info);
2048 /* Output the visibility of a field in a struct. */
2050 static bool
2051 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2053 assert (info->stack != NULL);
2055 if (info->stack->visibility == visibility)
2056 return true;
2058 assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2060 info->stack->visibility = visibility;
2062 return true;
2065 /* Add a field to a struct type. */
2067 static bool
2068 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2069 bfd_vma bitsize ATTRIBUTE_UNUSED,
2070 enum debug_visibility visibility)
2072 struct pr_handle *info = (struct pr_handle *) p;
2073 char *t;
2075 t = pop_type (info);
2076 if (t == NULL)
2077 return false;
2079 if (! tg_fix_visibility (info, visibility))
2080 return false;
2082 /* It happens, a bug? */
2083 if (! name[0])
2084 return true;
2086 fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2087 name, info->filename, t, info->stack->flavor, info->stack->type,
2088 visibility_name (visibility));
2090 return true;
2093 /* Finish a struct type. */
2095 static bool
2096 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2098 assert (((struct pr_handle *) p)->stack != NULL);
2100 return true;
2103 /* Start a class type. */
2105 static bool
2106 tg_start_class_type (void *p, const char *tag, unsigned int id,
2107 bool structp, unsigned int size,
2108 bool vptr, bool ownvptr)
2110 struct pr_handle *info = (struct pr_handle *) p;
2111 char *tv = NULL;
2112 const char *name;
2113 char idbuf[20];
2115 info->indent += 2;
2117 if (vptr && ! ownvptr)
2119 tv = pop_type (info);
2120 if (tv == NULL)
2121 return false;
2124 if (tag != NULL)
2125 name = tag;
2126 else
2128 sprintf (idbuf, "%%anon%u", id);
2129 name = idbuf;
2132 if (! push_type (info, name))
2133 return false;
2135 info->stack->flavor = structp ? "class" : "union class";
2136 info->stack->parents = NULL;
2137 info->stack->num_parents = 0;
2139 if (size != 0 || vptr || ownvptr || tag != NULL)
2141 if (vptr)
2143 if (! append_type (info, " vtable "))
2144 return false;
2145 if (ownvptr)
2147 if (! append_type (info, "self "))
2148 return false;
2150 else
2152 if (! append_type (info, tv)
2153 || ! append_type (info, " "))
2154 return false;
2159 info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2161 return true;
2164 /* Add a static member to a class. */
2166 static bool
2167 tg_class_static_member (void *p, const char *name,
2168 const char *physname ATTRIBUTE_UNUSED,
2169 enum debug_visibility visibility)
2171 struct pr_handle *info = (struct pr_handle *) p;
2172 char *t;
2173 int len_var, len_class;
2174 char *full_name;
2176 len_var = strlen (name);
2177 len_class = strlen (info->stack->next->type);
2178 full_name = (char *) xmalloc (len_var + len_class + 3);
2179 if (! full_name)
2180 return false;
2181 sprintf (full_name, "%s::%s", info->stack->next->type, name);
2183 if (! substitute_type (info, full_name))
2185 free (full_name);
2186 return false;
2189 if (! prepend_type (info, "static "))
2191 free (full_name);
2192 return false;
2195 t = pop_type (info);
2196 if (t == NULL)
2198 free (full_name);
2199 return false;
2202 if (! tg_fix_visibility (info, visibility))
2204 free (t);
2205 free (full_name);
2206 return false;
2209 fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2210 name, info->filename, t, info->stack->type,
2211 visibility_name (visibility));
2212 free (t);
2213 free (full_name);
2215 return true;
2218 /* Add a base class to a class. */
2220 static bool
2221 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2222 bool is_virtual, enum debug_visibility visibility)
2224 struct pr_handle *info = (struct pr_handle *) p;
2225 char *t;
2226 const char *prefix;
2228 assert (info->stack != NULL && info->stack->next != NULL);
2230 t = pop_type (info);
2231 if (t == NULL)
2232 return false;
2234 if (startswith (t, "class "))
2235 t += sizeof "class " - 1;
2237 /* Push it back on to take advantage of the prepend_type and
2238 append_type routines. */
2239 if (! push_type (info, t))
2240 return false;
2242 if (is_virtual)
2244 if (! prepend_type (info, "virtual "))
2245 return false;
2248 switch (visibility)
2250 case DEBUG_VISIBILITY_PUBLIC:
2251 prefix = "public ";
2252 break;
2253 case DEBUG_VISIBILITY_PROTECTED:
2254 prefix = "protected ";
2255 break;
2256 case DEBUG_VISIBILITY_PRIVATE:
2257 prefix = "private ";
2258 break;
2259 default:
2260 prefix = "/* unknown visibility */ ";
2261 break;
2264 if (! prepend_type (info, prefix))
2265 return false;
2267 t = pop_type (info);
2268 if (t == NULL)
2269 return false;
2271 if (info->stack->num_parents && ! append_parent (info, ", "))
2272 return false;
2274 if (! append_parent (info, t))
2275 return false;
2276 info->stack->num_parents++;
2278 free (t);
2280 return true;
2283 /* Add a variant to a method. */
2285 static bool
2286 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2287 enum debug_visibility visibility,
2288 bool constp, bool volatilep,
2289 bfd_vma voffset ATTRIBUTE_UNUSED,
2290 bool context)
2292 struct pr_handle *info = (struct pr_handle *) p;
2293 char *method_type;
2294 char *context_type;
2295 char *method_name;
2297 assert (info->stack != NULL);
2298 assert (info->stack->next != NULL);
2300 /* Put the const and volatile qualifiers on the type. */
2301 if (volatilep)
2303 if (! append_type (info, " volatile"))
2304 return false;
2306 if (constp)
2308 if (! append_type (info, " const"))
2309 return false;
2312 method_name = strdup (context ? info->stack->next->next->method
2313 : info->stack->next->method);
2315 /* Stick the name of the method into its type. */
2316 if (! substitute_type (info, method_name))
2318 free (method_name);
2319 return false;
2322 /* Get the type. */
2323 method_type = pop_type (info);
2324 if (method_type == NULL)
2326 free (method_name);
2327 return false;
2330 /* Pull off the context type if there is one. */
2331 if (! context)
2332 context_type = NULL;
2333 else
2335 context_type = pop_type (info);
2336 if (context_type == NULL)
2338 free (method_type);
2339 free (method_name);
2340 return false;
2344 /* Now the top of the stack is the class. */
2345 if (! tg_fix_visibility (info, visibility))
2347 free (method_type);
2348 free (method_name);
2349 free (context_type);
2350 return false;
2353 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2354 method_name, info->filename, method_type, info->stack->type);
2355 free (method_type);
2356 free (method_name);
2357 free (context_type);
2359 return true;
2362 /* Add a static variant to a method. */
2364 static bool
2365 tg_class_static_method_variant (void *p,
2366 const char *physname ATTRIBUTE_UNUSED,
2367 enum debug_visibility visibility,
2368 bool constp, bool volatilep)
2370 struct pr_handle *info = (struct pr_handle *) p;
2371 char *method_type;
2372 char *method_name;
2374 assert (info->stack != NULL);
2375 assert (info->stack->next != NULL);
2376 assert (info->stack->next->method != NULL);
2378 /* Put the const and volatile qualifiers on the type. */
2379 if (volatilep)
2381 if (! append_type (info, " volatile"))
2382 return false;
2384 if (constp)
2386 if (! append_type (info, " const"))
2387 return false;
2390 /* Mark it as static. */
2391 if (! prepend_type (info, "static "))
2392 return false;
2394 method_name = strdup (info->stack->next->method);
2395 /* Stick the name of the method into its type. */
2396 if (! substitute_type (info, info->stack->next->method))
2398 free (method_name);
2399 return false;
2402 /* Get the type. */
2403 method_type = pop_type (info);
2404 if (method_type == NULL)
2406 free (method_name);
2407 return false;
2410 /* Now the top of the stack is the class. */
2411 if (! tg_fix_visibility (info, visibility))
2413 free (method_type);
2414 free (method_name);
2415 return false;
2418 fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2419 method_name, info->filename, method_type, info->stack->type,
2420 visibility_name (visibility));
2421 free (method_type);
2422 free (method_name);
2424 return true;
2427 /* Finish up a class. */
2429 static bool
2430 tg_end_class_type (void *p)
2432 struct pr_handle *info = (struct pr_handle *) p;
2434 fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2435 info->filename, info->stack->flavor);
2436 if (info->stack->num_parents)
2438 fprintf (info->f, "\tinherits:%s", info->stack->parents);
2439 free (info->stack->parents);
2441 fputc ('\n', info->f);
2443 return tg_end_struct_type (p);
2446 /* Push a type on the stack using a tag name. */
2448 static bool
2449 tg_tag_type (void *p, const char *name, unsigned int id,
2450 enum debug_type_kind kind)
2452 struct pr_handle *info = (struct pr_handle *) p;
2453 const char *t, *tag;
2454 char idbuf[20];
2456 switch (kind)
2458 case DEBUG_KIND_STRUCT:
2459 t = "struct ";
2460 break;
2461 case DEBUG_KIND_UNION:
2462 t = "union ";
2463 break;
2464 case DEBUG_KIND_ENUM:
2465 t = "enum ";
2466 break;
2467 case DEBUG_KIND_CLASS:
2468 t = "class ";
2469 break;
2470 case DEBUG_KIND_UNION_CLASS:
2471 t = "union class ";
2472 break;
2473 default:
2474 abort ();
2475 return false;
2478 if (! push_type (info, t))
2479 return false;
2480 if (name != NULL)
2481 tag = name;
2482 else
2484 sprintf (idbuf, "%%anon%u", id);
2485 tag = idbuf;
2488 if (! append_type (info, tag))
2489 return false;
2491 return true;
2494 /* Output a typedef. */
2496 static bool
2497 tg_typdef (void *p, const char *name)
2499 struct pr_handle *info = (struct pr_handle *) p;
2500 char *s;
2502 s = pop_type (info);
2503 if (s == NULL)
2504 return false;
2506 fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2507 info->filename, s);
2509 free (s);
2511 return true;
2514 /* Output a tag. The tag should already be in the string on the
2515 stack, so all we have to do here is print it out. */
2517 static bool
2518 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2520 struct pr_handle *info = (struct pr_handle *) p;
2521 char *t;
2523 t = pop_type (info);
2524 if (t == NULL)
2525 return false;
2526 free (t);
2528 return true;
2531 /* Output an integer constant. */
2533 static bool
2534 tg_int_constant (void *p, const char *name, bfd_vma val)
2536 struct pr_handle *info = (struct pr_handle *) p;
2537 char ab[22];
2539 indent (info);
2540 print_vma (val, ab, false, false);
2541 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2542 name, info->filename, ab);
2543 return true;
2546 /* Output a floating point constant. */
2548 static bool
2549 tg_float_constant (void *p, const char *name, double val)
2551 struct pr_handle *info = (struct pr_handle *) p;
2553 indent (info);
2554 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2555 name, info->filename, val);
2556 return true;
2559 /* Output a typed constant. */
2561 static bool
2562 tg_typed_constant (void *p, const char *name, bfd_vma val)
2564 struct pr_handle *info = (struct pr_handle *) p;
2565 char *t;
2566 char ab[22];
2568 t = pop_type (info);
2569 if (t == NULL)
2570 return false;
2572 indent (info);
2573 print_vma (val, ab, false, false);
2574 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2575 name, info->filename, t, ab);
2577 free (t);
2579 return true;
2582 /* Output a variable. */
2584 static bool
2585 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2586 bfd_vma val ATTRIBUTE_UNUSED)
2588 struct pr_handle *info = (struct pr_handle *) p;
2589 char *t, *dname, *from_class;
2591 t = pop_type (info);
2592 if (t == NULL)
2593 return false;
2595 dname = NULL;
2596 if (info->demangler)
2597 dname = info->demangler (info->abfd, name, demangle_flags);
2599 from_class = NULL;
2600 if (dname != NULL)
2602 char *sep;
2603 sep = strstr (dname, "::");
2604 if (sep)
2606 *sep = 0;
2607 name = sep + 2;
2608 from_class = dname;
2610 else
2611 /* Obscure types as vts and type_info nodes. */
2612 name = dname;
2615 fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2617 switch (kind)
2619 case DEBUG_STATIC:
2620 case DEBUG_LOCAL_STATIC:
2621 fprintf (info->f, "\tfile:");
2622 break;
2623 case DEBUG_REGISTER:
2624 fprintf (info->f, "\tregister:");
2625 break;
2626 default:
2627 break;
2630 if (from_class)
2631 fprintf (info->f, "\tclass:%s", from_class);
2633 if (dname)
2634 free (dname);
2636 fprintf (info->f, "\n");
2638 free (t);
2640 return true;
2643 /* Start outputting a function. */
2645 static bool
2646 tg_start_function (void *p, const char *name, bool global)
2648 struct pr_handle *info = (struct pr_handle *) p;
2649 char *dname;
2651 if (! global)
2652 info->stack->flavor = "static";
2653 else
2654 info->stack->flavor = NULL;
2656 dname = NULL;
2657 if (info->demangler)
2658 dname = info->demangler (info->abfd, name, demangle_flags);
2660 if (! substitute_type (info, dname ? dname : name))
2661 return false;
2663 info->stack->method = NULL;
2664 if (dname != NULL)
2666 char *sep;
2667 sep = strstr (dname, "::");
2668 if (sep)
2670 info->stack->method = dname;
2671 *sep = 0;
2672 name = sep + 2;
2674 else
2676 info->stack->method = "";
2677 name = dname;
2679 sep = strchr (name, '(');
2680 if (sep)
2681 *sep = 0;
2682 /* Obscure functions as type_info function. */
2685 info->stack->parents = strdup (name);
2687 if (! info->stack->method && ! append_type (info, "("))
2688 return false;
2690 info->parameter = 1;
2692 return true;
2695 /* Output a function parameter. */
2697 static bool
2698 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2699 bfd_vma val ATTRIBUTE_UNUSED)
2701 struct pr_handle *info = (struct pr_handle *) p;
2702 char *t;
2704 if (kind == DEBUG_PARM_REFERENCE
2705 || kind == DEBUG_PARM_REF_REG)
2707 if (! pr_reference_type (p))
2708 return false;
2711 if (! substitute_type (info, name))
2712 return false;
2714 t = pop_type (info);
2715 if (t == NULL)
2716 return false;
2718 if (! info->stack->method)
2720 if (info->parameter != 1 && ! append_type (info, ", "))
2721 return false;
2723 if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2724 if (! append_type (info, "register "))
2725 return false;
2727 if (! append_type (info, t))
2728 return false;
2731 free (t);
2733 ++info->parameter;
2735 return true;
2738 /* Start writing out a block. */
2740 static bool
2741 tg_start_block (void *p, bfd_vma addr)
2743 struct pr_handle *info = (struct pr_handle *) p;
2744 char ab[22], kind, *partof;
2745 char *t;
2746 bool local;
2748 if (info->parameter > 0)
2750 info->parameter = 0;
2752 /* Delayed name. */
2753 fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2754 free (info->stack->parents);
2756 print_vma (addr, ab, true, true);
2757 translate_addresses (info->abfd, ab, info->f, info->syms);
2758 local = info->stack->flavor != NULL;
2759 if (info->stack->method && *info->stack->method)
2761 kind = 'm';
2762 partof = (char *) info->stack->method;
2764 else
2766 kind = 'f';
2767 partof = NULL;
2768 if (! info->stack->method && ! append_type (info, ")"))
2769 return false;
2771 t = pop_type (info);
2772 if (t == NULL)
2773 return false;
2774 fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2775 if (local)
2776 fputs ("\tfile:", info->f);
2777 if (partof)
2779 fprintf (info->f, "\tclass:%s", partof);
2780 free (partof);
2782 fputc ('\n', info->f);
2785 return true;
2788 /* Write out line number information. */
2790 static bool
2791 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
2792 unsigned long lineno ATTRIBUTE_UNUSED,
2793 bfd_vma addr ATTRIBUTE_UNUSED)
2795 return true;
2798 /* Finish writing out a block. */
2800 static bool
2801 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2803 return true;
2806 /* Convert the visibility value into a human readable name. */
2808 static const char *
2809 visibility_name (enum debug_visibility visibility)
2811 const char *s;
2813 switch (visibility)
2815 case DEBUG_VISIBILITY_PUBLIC:
2816 s = "public";
2817 break;
2818 case DEBUG_VISIBILITY_PRIVATE:
2819 s = "private";
2820 break;
2821 case DEBUG_VISIBILITY_PROTECTED:
2822 s = "protected";
2823 break;
2824 case DEBUG_VISIBILITY_IGNORE:
2825 s = "/* ignore */";
2826 break;
2827 default:
2828 abort ();
2829 return false;
2831 return s;