1 /* prdbg.c -- Print out generic debugging information.
2    Copyright (C) 1995-2014 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>.
5 
6    This file is part of GNU Binutils.
7 
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.
12 
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.
17 
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.  */
22 
23 /* This file prints out the generic debugging information, by
24    supplying a set of routines to debug_write.  */
25 
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"
33 
34 /* This is the structure we use as a handle for these routines.  */
35 
36 struct pr_handle
37 {
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);
55 };
56 
57 /* The type stack.  */
58 
59 struct pr_stack
60 {
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;
76 };
77 
78 static void indent (struct pr_handle *);
79 static bfd_boolean push_type (struct pr_handle *, const char *);
80 static bfd_boolean prepend_type (struct pr_handle *, const char *);
81 static bfd_boolean append_type (struct pr_handle *, const char *);
82 static bfd_boolean substitute_type (struct pr_handle *, const char *);
83 static bfd_boolean indent_type (struct pr_handle *);
84 static char *pop_type (struct pr_handle *);
85 static void print_vma (bfd_vma, char *, bfd_boolean, bfd_boolean);
86 static bfd_boolean pr_fix_visibility
87   (struct pr_handle *, enum debug_visibility);
88 static bfd_boolean pr_start_compilation_unit (void *, const char *);
89 static bfd_boolean pr_start_source (void *, const char *);
90 static bfd_boolean pr_empty_type (void *);
91 static bfd_boolean pr_void_type (void *);
92 static bfd_boolean pr_int_type (void *, unsigned int, bfd_boolean);
93 static bfd_boolean pr_float_type (void *, unsigned int);
94 static bfd_boolean pr_complex_type (void *, unsigned int);
95 static bfd_boolean pr_bool_type (void *, unsigned int);
96 static bfd_boolean pr_enum_type
97   (void *, const char *, const char **, bfd_signed_vma *);
98 static bfd_boolean pr_pointer_type (void *);
99 static bfd_boolean pr_function_type (void *, int, bfd_boolean);
100 static bfd_boolean pr_reference_type (void *);
101 static bfd_boolean pr_range_type (void *, bfd_signed_vma, bfd_signed_vma);
102 static bfd_boolean pr_array_type
103   (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
104 static bfd_boolean pr_set_type (void *, bfd_boolean);
105 static bfd_boolean pr_offset_type (void *);
106 static bfd_boolean pr_method_type (void *, bfd_boolean, int, bfd_boolean);
107 static bfd_boolean pr_const_type (void *);
108 static bfd_boolean pr_volatile_type (void *);
109 static bfd_boolean pr_start_struct_type
110   (void *, const char *, unsigned int, bfd_boolean, unsigned int);
111 static bfd_boolean pr_struct_field
112   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
113 static bfd_boolean pr_end_struct_type (void *);
114 static bfd_boolean pr_start_class_type
115   (void *, const char *, unsigned int, bfd_boolean, unsigned int,
116    bfd_boolean, bfd_boolean);
117 static bfd_boolean pr_class_static_member
118   (void *, const char *, const char *, enum debug_visibility);
119 static bfd_boolean pr_class_baseclass
120   (void *, bfd_vma, bfd_boolean, enum debug_visibility);
121 static bfd_boolean pr_class_start_method (void *, const char *);
122 static bfd_boolean pr_class_method_variant
123   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
124    bfd_vma, bfd_boolean);
125 static bfd_boolean pr_class_static_method_variant
126   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
127 static bfd_boolean pr_class_end_method (void *);
128 static bfd_boolean pr_end_class_type (void *);
129 static bfd_boolean pr_typedef_type (void *, const char *);
130 static bfd_boolean pr_tag_type
131   (void *, const char *, unsigned int, enum debug_type_kind);
132 static bfd_boolean pr_typdef (void *, const char *);
133 static bfd_boolean pr_tag (void *, const char *);
134 static bfd_boolean pr_int_constant (void *, const char *, bfd_vma);
135 static bfd_boolean pr_float_constant (void *, const char *, double);
136 static bfd_boolean pr_typed_constant (void *, const char *, bfd_vma);
137 static bfd_boolean pr_variable
138   (void *, const char *, enum debug_var_kind, bfd_vma);
139 static bfd_boolean pr_start_function (void *, const char *, bfd_boolean);
140 static bfd_boolean pr_function_parameter
141   (void *, const char *, enum debug_parm_kind, bfd_vma);
142 static bfd_boolean pr_start_block (void *, bfd_vma);
143 static bfd_boolean pr_end_block (void *, bfd_vma);
144 static bfd_boolean pr_end_function (void *);
145 static bfd_boolean pr_lineno (void *, const char *, unsigned long, bfd_vma);
146 static bfd_boolean append_parent (struct pr_handle *, const char *);
147 /* Only used by tg_ code.  */
148 static bfd_boolean tg_fix_visibility
149   (struct pr_handle *, enum debug_visibility);
150 static void find_address_in_section (bfd *, asection *, void *);
151 static void translate_addresses (bfd *, char *, FILE *, asymbol **);
152 static const char *visibility_name (enum debug_visibility);
153 /* Tags style replacements.  */
154 static bfd_boolean tg_start_compilation_unit (void *, const char *);
155 static bfd_boolean tg_start_source (void *, const char *);
156 static bfd_boolean tg_enum_type
157   (void *, const char *, const char **, bfd_signed_vma *);
158 static bfd_boolean tg_start_struct_type
159   (void *, const char *, unsigned int, bfd_boolean, unsigned int);
160 static bfd_boolean pr_struct_field
161   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
162 static bfd_boolean tg_struct_field
163   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
164 static bfd_boolean tg_struct_field
165   (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
166 static bfd_boolean tg_end_struct_type (void *);
167 static bfd_boolean tg_start_class_type
168   (void *, const char *, unsigned int, bfd_boolean, unsigned int, bfd_boolean, bfd_boolean);
169 static bfd_boolean tg_class_static_member
170   (void *, const char *, const char *, enum debug_visibility);
171 static bfd_boolean tg_class_baseclass
172   (void *, bfd_vma, bfd_boolean, enum debug_visibility);
173 static bfd_boolean tg_class_method_variant
174   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
175 static bfd_boolean tg_class_static_method_variant
176   (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
177 static bfd_boolean tg_end_class_type (void *);
178 static bfd_boolean tg_tag_type
179   (void *, const char *, unsigned int, enum debug_type_kind);
180 static bfd_boolean tg_typdef (void *, const char *);
181 static bfd_boolean tg_tag (void *, const char *);
182 static bfd_boolean tg_int_constant (void *, const char *, bfd_vma);
183 static bfd_boolean tg_float_constant (void *, const char *, double);
184 static bfd_boolean tg_typed_constant (void *, const char *, bfd_vma);
185 static bfd_boolean tg_variable
186   (void *, const char *, enum debug_var_kind, bfd_vma);
187 static bfd_boolean tg_start_function (void *, const char *, bfd_boolean);
188 static bfd_boolean tg_function_parameter
189   (void *, const char *, enum debug_parm_kind, bfd_vma);
190 static bfd_boolean tg_start_block (void *, bfd_vma);
191 static bfd_boolean tg_end_block (void *, bfd_vma);
192 static bfd_boolean tg_lineno (void *, const char *, unsigned long, bfd_vma);
193 
194 static const struct debug_write_fns pr_fns =
195 {
196   pr_start_compilation_unit,
197   pr_start_source,
198   pr_empty_type,
199   pr_void_type,
200   pr_int_type,
201   pr_float_type,
202   pr_complex_type,
203   pr_bool_type,
204   pr_enum_type,
205   pr_pointer_type,
206   pr_function_type,
207   pr_reference_type,
208   pr_range_type,
209   pr_array_type,
210   pr_set_type,
211   pr_offset_type,
212   pr_method_type,
213   pr_const_type,
214   pr_volatile_type,
215   pr_start_struct_type,
216   pr_struct_field,
217   pr_end_struct_type,
218   pr_start_class_type,
219   pr_class_static_member,
220   pr_class_baseclass,
221   pr_class_start_method,
222   pr_class_method_variant,
223   pr_class_static_method_variant,
224   pr_class_end_method,
225   pr_end_class_type,
226   pr_typedef_type,
227   pr_tag_type,
228   pr_typdef,
229   pr_tag,
230   pr_int_constant,
231   pr_float_constant,
232   pr_typed_constant,
233   pr_variable,
234   pr_start_function,
235   pr_function_parameter,
236   pr_start_block,
237   pr_end_block,
238   pr_end_function,
239   pr_lineno
240 };
241 
242 static const struct debug_write_fns tg_fns =
243 {
244   tg_start_compilation_unit,
245   tg_start_source,
246   pr_empty_type,		/* Same, push_type.  */
247   pr_void_type,			/* Same, push_type.  */
248   pr_int_type,			/* Same, push_type.  */
249   pr_float_type,		/* Same, push_type.  */
250   pr_complex_type,		/* Same, push_type.  */
251   pr_bool_type,			/* Same, push_type.  */
252   tg_enum_type,
253   pr_pointer_type,		/* Same, changes to pointer.  */
254   pr_function_type,		/* Same, push_type.  */
255   pr_reference_type,		/* Same, changes to reference.  */
256   pr_range_type,		/* FIXME: What's that?.  */
257   pr_array_type,		/* Same, push_type.  */
258   pr_set_type,			/* FIXME: What's that?.  */
259   pr_offset_type,		/* FIXME: What's that?.  */
260   pr_method_type,		/* Same.  */
261   pr_const_type,		/* Same, changes to const.  */
262   pr_volatile_type,		/* Same, changes to volatile.  */
263   tg_start_struct_type,
264   tg_struct_field,
265   tg_end_struct_type,
266   tg_start_class_type,
267   tg_class_static_member,
268   tg_class_baseclass,
269   pr_class_start_method,	/* Same, remembers that's a method.  */
270   tg_class_method_variant,
271   tg_class_static_method_variant,
272   pr_class_end_method,		/* Same, forgets that's a method.  */
273   tg_end_class_type,
274   pr_typedef_type,		/* Same, just push type.  */
275   tg_tag_type,
276   tg_typdef,
277   tg_tag,
278   tg_int_constant,		/* Untested.  */
279   tg_float_constant,		/* Untested.  */
280   tg_typed_constant,		/* Untested.  */
281   tg_variable,
282   tg_start_function,
283   tg_function_parameter,
284   tg_start_block,
285   tg_end_block,
286   pr_end_function,		/* Same, does nothing.  */
287   tg_lineno
288 };
289 
290 /* Print out the generic debugging information recorded in dhandle.  */
291 
292 bfd_boolean
print_debugging_info(FILE * f,void * dhandle,bfd * abfd,asymbol ** syms,void * demangler,bfd_boolean as_tags)293 print_debugging_info (FILE *f, void *dhandle, bfd *abfd, asymbol **syms,
294 		      void *demangler, bfd_boolean as_tags)
295 {
296   struct pr_handle info;
297 
298   info.f = f;
299   info.indent = 0;
300   info.stack = NULL;
301   info.parameter = 0;
302   info.filename = NULL;
303   info.abfd = abfd;
304   info.syms = syms;
305   info.demangler = (char * (*)(struct bfd *, const char *, int)) demangler;
306 
307   if (as_tags)
308     {
309       fputs ("!_TAG_FILE_FORMAT\t2\t/extended format/\n", f);
310       fputs ("!_TAG_FILE_SORTED\t0\t/0=unsorted, 1=sorted/\n", f);
311       fputs ("!_TAG_PROGRAM_AUTHOR\tIan Lance Taylor, Salvador E. Tropea and others\t//\n", f);
312       fputs ("!_TAG_PROGRAM_NAME\tobjdump\t/From GNU binutils/\n", f);
313     }
314 
315   return as_tags ? debug_write (dhandle, &tg_fns, (void *) & info)
316     : debug_write (dhandle, &pr_fns, (void *) & info);
317 }
318 
319 /* Indent to the current indentation level.  */
320 
321 static void
indent(struct pr_handle * info)322 indent (struct pr_handle *info)
323 {
324   unsigned int i;
325 
326   for (i = 0; i < info->indent; i++)
327     putc (' ', info->f);
328 }
329 
330 /* Push a type on the type stack.  */
331 
332 static bfd_boolean
push_type(struct pr_handle * info,const char * type)333 push_type (struct pr_handle *info, const char *type)
334 {
335   struct pr_stack *n;
336 
337   if (type == NULL)
338     return FALSE;
339 
340   n = (struct pr_stack *) xmalloc (sizeof *n);
341   memset (n, 0, sizeof *n);
342 
343   n->type = xstrdup (type);
344   n->visibility = DEBUG_VISIBILITY_IGNORE;
345   n->method = NULL;
346   n->next = info->stack;
347   info->stack = n;
348 
349   return TRUE;
350 }
351 
352 /* Prepend a string onto the type on the top of the type stack.  */
353 
354 static bfd_boolean
prepend_type(struct pr_handle * info,const char * s)355 prepend_type (struct pr_handle *info, const char *s)
356 {
357   char *n;
358 
359   assert (info->stack != NULL);
360 
361   n = (char *) xmalloc (strlen (s) + strlen (info->stack->type) + 1);
362   sprintf (n, "%s%s", s, info->stack->type);
363   free (info->stack->type);
364   info->stack->type = n;
365 
366   return TRUE;
367 }
368 
369 /* Append a string to the type on the top of the type stack.  */
370 
371 static bfd_boolean
append_type(struct pr_handle * info,const char * s)372 append_type (struct pr_handle *info, const char *s)
373 {
374   unsigned int len;
375 
376   if (s == NULL)
377     return FALSE;
378 
379   assert (info->stack != NULL);
380 
381   len = strlen (info->stack->type);
382   info->stack->type = (char *) xrealloc (info->stack->type,
383 					 len + strlen (s) + 1);
384   strcpy (info->stack->type + len, s);
385 
386   return TRUE;
387 }
388 
389 /* Append a string to the parents on the top of the type stack.  */
390 
391 static bfd_boolean
append_parent(struct pr_handle * info,const char * s)392 append_parent (struct pr_handle *info, const char *s)
393 {
394   unsigned int len;
395 
396   if (s == NULL)
397     return FALSE;
398 
399   assert (info->stack != NULL);
400 
401   len = info->stack->parents ? strlen (info->stack->parents) : 0;
402   info->stack->parents = (char *) xrealloc (info->stack->parents,
403 					    len + strlen (s) + 1);
404   strcpy (info->stack->parents + len, s);
405 
406   return TRUE;
407 }
408 
409 /* We use an underscore to indicate where the name should go in a type
410    string.  This function substitutes a string for the underscore.  If
411    there is no underscore, the name follows the type.  */
412 
413 static bfd_boolean
substitute_type(struct pr_handle * info,const char * s)414 substitute_type (struct pr_handle *info, const char *s)
415 {
416   char *u;
417 
418   assert (info->stack != NULL);
419 
420   u = strchr (info->stack->type, '|');
421   if (u != NULL)
422     {
423       char *n;
424 
425       n = (char *) xmalloc (strlen (info->stack->type) + strlen (s));
426 
427       memcpy (n, info->stack->type, u - info->stack->type);
428       strcpy (n + (u - info->stack->type), s);
429       strcat (n, u + 1);
430 
431       free (info->stack->type);
432       info->stack->type = n;
433 
434       return TRUE;
435     }
436 
437   if (strchr (s, '|') != NULL
438       && (strchr (info->stack->type, '{') != NULL
439 	  || strchr (info->stack->type, '(') != NULL))
440     {
441       if (! prepend_type (info, "(")
442 	  || ! append_type (info, ")"))
443 	return FALSE;
444     }
445 
446   if (*s == '\0')
447     return TRUE;
448 
449   return (append_type (info, " ")
450 	  && append_type (info, s));
451 }
452 
453 /* Indent the type at the top of the stack by appending spaces.  */
454 
455 static bfd_boolean
indent_type(struct pr_handle * info)456 indent_type (struct pr_handle *info)
457 {
458   unsigned int i;
459 
460   for (i = 0; i < info->indent; i++)
461     {
462       if (! append_type (info, " "))
463 	return FALSE;
464     }
465 
466   return TRUE;
467 }
468 
469 /* Pop a type from the type stack.  */
470 
471 static char *
pop_type(struct pr_handle * info)472 pop_type (struct pr_handle *info)
473 {
474   struct pr_stack *o;
475   char *ret;
476 
477   assert (info->stack != NULL);
478 
479   o = info->stack;
480   info->stack = o->next;
481   ret = o->type;
482   free (o);
483 
484   return ret;
485 }
486 
487 /* Print a VMA value into a string.  */
488 
489 static void
print_vma(bfd_vma vma,char * buf,bfd_boolean unsignedp,bfd_boolean hexp)490 print_vma (bfd_vma vma, char *buf, bfd_boolean unsignedp, bfd_boolean hexp)
491 {
492   if (sizeof (vma) <= sizeof (unsigned long))
493     {
494       if (hexp)
495 	sprintf (buf, "0x%lx", (unsigned long) vma);
496       else if (unsignedp)
497 	sprintf (buf, "%lu", (unsigned long) vma);
498       else
499 	sprintf (buf, "%ld", (long) vma);
500     }
501 #if BFD_HOST_64BIT_LONG_LONG
502   else if (sizeof (vma) <= sizeof (unsigned long long))
503     {
504 #ifndef __MSVCRT__
505       if (hexp)
506 	sprintf (buf, "0x%llx", (unsigned long long) vma);
507       else if (unsignedp)
508 	sprintf (buf, "%llu", (unsigned long long) vma);
509       else
510 	sprintf (buf, "%lld", (long long) vma);
511 #else
512       if (hexp)
513 	sprintf (buf, "0x%I64x", (unsigned long long) vma);
514       else if (unsignedp)
515 	sprintf (buf, "%I64u", (unsigned long long) vma);
516       else
517 	sprintf (buf, "%I64d", (long long) vma);
518 #endif
519     }
520 #endif
521   else
522     {
523       buf[0] = '0';
524       buf[1] = 'x';
525       sprintf_vma (buf + 2, vma);
526     }
527 }
528 
529 /* Start a new compilation unit.  */
530 
531 static bfd_boolean
pr_start_compilation_unit(void * p,const char * filename)532 pr_start_compilation_unit (void *p, const char *filename)
533 {
534   struct pr_handle *info = (struct pr_handle *) p;
535 
536   assert (info->indent == 0);
537 
538   fprintf (info->f, "%s:\n", filename);
539 
540   return TRUE;
541 }
542 
543 /* Start a source file within a compilation unit.  */
544 
545 static bfd_boolean
pr_start_source(void * p,const char * filename)546 pr_start_source (void *p, const char *filename)
547 {
548   struct pr_handle *info = (struct pr_handle *) p;
549 
550   assert (info->indent == 0);
551 
552   fprintf (info->f, " %s:\n", filename);
553 
554   return TRUE;
555 }
556 
557 /* Push an empty type onto the type stack.  */
558 
559 static bfd_boolean
pr_empty_type(void * p)560 pr_empty_type (void *p)
561 {
562   struct pr_handle *info = (struct pr_handle *) p;
563 
564   return push_type (info, "<undefined>");
565 }
566 
567 /* Push a void type onto the type stack.  */
568 
569 static bfd_boolean
pr_void_type(void * p)570 pr_void_type (void *p)
571 {
572   struct pr_handle *info = (struct pr_handle *) p;
573 
574   return push_type (info, "void");
575 }
576 
577 /* Push an integer type onto the type stack.  */
578 
579 static bfd_boolean
pr_int_type(void * p,unsigned int size,bfd_boolean unsignedp)580 pr_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
581 {
582   struct pr_handle *info = (struct pr_handle *) p;
583   char ab[10];
584 
585   sprintf (ab, "%sint%d", unsignedp ? "u" : "", size * 8);
586   return push_type (info, ab);
587 }
588 
589 /* Push a floating type onto the type stack.  */
590 
591 static bfd_boolean
pr_float_type(void * p,unsigned int size)592 pr_float_type (void *p, unsigned int size)
593 {
594   struct pr_handle *info = (struct pr_handle *) p;
595   char ab[10];
596 
597   if (size == 4)
598     return push_type (info, "float");
599   else if (size == 8)
600     return push_type (info, "double");
601 
602   sprintf (ab, "float%d", size * 8);
603   return push_type (info, ab);
604 }
605 
606 /* Push a complex type onto the type stack.  */
607 
608 static bfd_boolean
pr_complex_type(void * p,unsigned int size)609 pr_complex_type (void *p, unsigned int size)
610 {
611   struct pr_handle *info = (struct pr_handle *) p;
612 
613   if (! pr_float_type (p, size))
614     return FALSE;
615 
616   return prepend_type (info, "complex ");
617 }
618 
619 /* Push a bfd_boolean type onto the type stack.  */
620 
621 static bfd_boolean
pr_bool_type(void * p,unsigned int size)622 pr_bool_type (void *p, unsigned int size)
623 {
624   struct pr_handle *info = (struct pr_handle *) p;
625   char ab[10];
626 
627   sprintf (ab, "bool%d", size * 8);
628 
629   return push_type (info, ab);
630 }
631 
632 /* Push an enum type onto the type stack.  */
633 
634 static bfd_boolean
pr_enum_type(void * p,const char * tag,const char ** names,bfd_signed_vma * values)635 pr_enum_type (void *p, const char *tag, const char **names,
636 	      bfd_signed_vma *values)
637 {
638   struct pr_handle *info = (struct pr_handle *) p;
639   unsigned int i;
640   bfd_signed_vma val;
641 
642   if (! push_type (info, "enum "))
643     return FALSE;
644   if (tag != NULL)
645     {
646       if (! append_type (info, tag)
647 	  || ! append_type (info, " "))
648 	return FALSE;
649     }
650   if (! append_type (info, "{ "))
651     return FALSE;
652 
653   if (names == NULL)
654     {
655       if (! append_type (info, "/* undefined */"))
656 	return FALSE;
657     }
658   else
659     {
660       val = 0;
661       for (i = 0; names[i] != NULL; i++)
662 	{
663 	  if (i > 0)
664 	    {
665 	      if (! append_type (info, ", "))
666 		return FALSE;
667 	    }
668 
669 	  if (! append_type (info, names[i]))
670 	    return FALSE;
671 
672 	  if (values[i] != val)
673 	    {
674 	      char ab[20];
675 
676 	      print_vma (values[i], ab, FALSE, FALSE);
677 	      if (! append_type (info, " = ")
678 		  || ! append_type (info, ab))
679 		return FALSE;
680 	      val = values[i];
681 	    }
682 
683 	  ++val;
684 	}
685     }
686 
687   return append_type (info, " }");
688 }
689 
690 /* Turn the top type on the stack into a pointer.  */
691 
692 static bfd_boolean
pr_pointer_type(void * p)693 pr_pointer_type (void *p)
694 {
695   struct pr_handle *info = (struct pr_handle *) p;
696   char *s;
697 
698   assert (info->stack != NULL);
699 
700   s = strchr (info->stack->type, '|');
701   if (s != NULL && s[1] == '[')
702     return substitute_type (info, "(*|)");
703   return substitute_type (info, "*|");
704 }
705 
706 /* Turn the top type on the stack into a function returning that type.  */
707 
708 static bfd_boolean
pr_function_type(void * p,int argcount,bfd_boolean varargs)709 pr_function_type (void *p, int argcount, bfd_boolean varargs)
710 {
711   struct pr_handle *info = (struct pr_handle *) p;
712   char **arg_types;
713   unsigned int len;
714   char *s;
715 
716   assert (info->stack != NULL);
717 
718   len = 10;
719 
720   if (argcount <= 0)
721     {
722       arg_types = NULL;
723       len += 15;
724     }
725   else
726     {
727       int i;
728 
729       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
730       for (i = argcount - 1; i >= 0; i--)
731 	{
732 	  if (! substitute_type (info, ""))
733 	    {
734 	      free (arg_types);
735 	      return FALSE;
736 	    }
737 	  arg_types[i] = pop_type (info);
738 	  if (arg_types[i] == NULL)
739 	    {
740 	      free (arg_types);
741 	      return FALSE;
742 	    }
743 	  len += strlen (arg_types[i]) + 2;
744 	}
745       if (varargs)
746 	len += 5;
747     }
748 
749   /* Now the return type is on the top of the stack.  */
750 
751   s = (char *) xmalloc (len);
752   LITSTRCPY (s, "(|) (");
753 
754   if (argcount < 0)
755     strcat (s, "/* unknown */");
756   else
757     {
758       int i;
759 
760       for (i = 0; i < argcount; i++)
761 	{
762 	  if (i > 0)
763 	    strcat (s, ", ");
764 	  strcat (s, arg_types[i]);
765 	}
766       if (varargs)
767 	{
768 	  if (i > 0)
769 	    strcat (s, ", ");
770 	  strcat (s, "...");
771 	}
772       if (argcount > 0)
773 	free (arg_types);
774     }
775 
776   strcat (s, ")");
777 
778   if (! substitute_type (info, s))
779     return FALSE;
780 
781   free (s);
782 
783   return TRUE;
784 }
785 
786 /* Turn the top type on the stack into a reference to that type.  */
787 
788 static bfd_boolean
pr_reference_type(void * p)789 pr_reference_type (void *p)
790 {
791   struct pr_handle *info = (struct pr_handle *) p;
792 
793   assert (info->stack != NULL);
794 
795   return substitute_type (info, "&|");
796 }
797 
798 /* Make a range type.  */
799 
800 static bfd_boolean
pr_range_type(void * p,bfd_signed_vma lower,bfd_signed_vma upper)801 pr_range_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper)
802 {
803   struct pr_handle *info = (struct pr_handle *) p;
804   char abl[20], abu[20];
805 
806   assert (info->stack != NULL);
807 
808   if (! substitute_type (info, ""))
809     return FALSE;
810 
811   print_vma (lower, abl, FALSE, FALSE);
812   print_vma (upper, abu, FALSE, FALSE);
813 
814   return (prepend_type (info, "range (")
815 	  && append_type (info, "):")
816 	  && append_type (info, abl)
817 	  && append_type (info, ":")
818 	  && append_type (info, abu));
819 }
820 
821 /* Make an array type.  */
822 
823 static bfd_boolean
pr_array_type(void * p,bfd_signed_vma lower,bfd_signed_vma upper,bfd_boolean stringp)824 pr_array_type (void *p, bfd_signed_vma lower, bfd_signed_vma upper,
825 	       bfd_boolean stringp)
826 {
827   struct pr_handle *info = (struct pr_handle *) p;
828   char *range_type;
829   char abl[20], abu[20], ab[50];
830 
831   range_type = pop_type (info);
832   if (range_type == NULL)
833     return FALSE;
834 
835   if (lower == 0)
836     {
837       if (upper == -1)
838 	sprintf (ab, "|[]");
839       else
840 	{
841 	  print_vma (upper + 1, abu, FALSE, FALSE);
842 	  sprintf (ab, "|[%s]", abu);
843 	}
844     }
845   else
846     {
847       print_vma (lower, abl, FALSE, FALSE);
848       print_vma (upper, abu, FALSE, FALSE);
849       sprintf (ab, "|[%s:%s]", abl, abu);
850     }
851 
852   if (! substitute_type (info, ab))
853     return FALSE;
854 
855   if (strcmp (range_type, "int") != 0)
856     {
857       if (! append_type (info, ":")
858 	  || ! append_type (info, range_type))
859 	return FALSE;
860     }
861 
862   if (stringp)
863     {
864       if (! append_type (info, " /* string */"))
865 	return FALSE;
866     }
867 
868   return TRUE;
869 }
870 
871 /* Make a set type.  */
872 
873 static bfd_boolean
pr_set_type(void * p,bfd_boolean bitstringp)874 pr_set_type (void *p, bfd_boolean bitstringp)
875 {
876   struct pr_handle *info = (struct pr_handle *) p;
877 
878   if (! substitute_type (info, ""))
879     return FALSE;
880 
881   if (! prepend_type (info, "set { ")
882       || ! append_type (info, " }"))
883     return FALSE;
884 
885   if (bitstringp)
886     {
887       if (! append_type (info, "/* bitstring */"))
888 	return FALSE;
889     }
890 
891   return TRUE;
892 }
893 
894 /* Make an offset type.  */
895 
896 static bfd_boolean
pr_offset_type(void * p)897 pr_offset_type (void *p)
898 {
899   struct pr_handle *info = (struct pr_handle *) p;
900   char *t;
901 
902   if (! substitute_type (info, ""))
903     return FALSE;
904 
905   t = pop_type (info);
906   if (t == NULL)
907     return FALSE;
908 
909   return (substitute_type (info, "")
910 	  && prepend_type (info, " ")
911 	  && prepend_type (info, t)
912 	  && append_type (info, "::|"));
913 }
914 
915 /* Make a method type.  */
916 
917 static bfd_boolean
pr_method_type(void * p,bfd_boolean domain,int argcount,bfd_boolean varargs)918 pr_method_type (void *p, bfd_boolean domain, int argcount, bfd_boolean varargs)
919 {
920   struct pr_handle *info = (struct pr_handle *) p;
921   unsigned int len;
922   char *domain_type;
923   char **arg_types;
924   char *s;
925 
926   len = 10;
927 
928   if (! domain)
929     domain_type = NULL;
930   else
931     {
932       if (! substitute_type (info, ""))
933 	return FALSE;
934       domain_type = pop_type (info);
935       if (domain_type == NULL)
936 	return FALSE;
937       if (CONST_STRNEQ (domain_type, "class ")
938 	  && strchr (domain_type + sizeof "class " - 1, ' ') == NULL)
939 	domain_type += sizeof "class " - 1;
940       else if (CONST_STRNEQ (domain_type, "union class ")
941 	       && (strchr (domain_type + sizeof "union class " - 1, ' ')
942 		   == NULL))
943 	domain_type += sizeof "union class " - 1;
944       len += strlen (domain_type);
945     }
946 
947   if (argcount <= 0)
948     {
949       arg_types = NULL;
950       len += 15;
951     }
952   else
953     {
954       int i;
955 
956       arg_types = (char **) xmalloc (argcount * sizeof *arg_types);
957       for (i = argcount - 1; i >= 0; i--)
958 	{
959 	  if (! substitute_type (info, ""))
960 	    {
961 	      free (arg_types);
962 	      return FALSE;
963 	    }
964 	  arg_types[i] = pop_type (info);
965 	  if (arg_types[i] == NULL)
966 	    {
967 	      free (arg_types);
968 	      return FALSE;
969 	    }
970 	  len += strlen (arg_types[i]) + 2;
971 	}
972       if (varargs)
973 	len += 5;
974     }
975 
976   /* Now the return type is on the top of the stack.  */
977 
978   s = (char *) xmalloc (len);
979   if (! domain)
980     *s = '\0';
981   else
982     strcpy (s, domain_type);
983   strcat (s, "::| (");
984 
985   if (argcount < 0)
986     strcat (s, "/* unknown */");
987   else
988     {
989       int i;
990 
991       for (i = 0; i < argcount; i++)
992 	{
993 	  if (i > 0)
994 	    strcat (s, ", ");
995 	  strcat (s, arg_types[i]);
996 	}
997       if (varargs)
998 	{
999 	  if (i > 0)
1000 	    strcat (s, ", ");
1001 	  strcat (s, "...");
1002 	}
1003       if (argcount > 0)
1004 	free (arg_types);
1005     }
1006 
1007   strcat (s, ")");
1008 
1009   if (! substitute_type (info, s))
1010     return FALSE;
1011 
1012   free (s);
1013 
1014   return TRUE;
1015 }
1016 
1017 /* Make a const qualified type.  */
1018 
1019 static bfd_boolean
pr_const_type(void * p)1020 pr_const_type (void *p)
1021 {
1022   struct pr_handle *info = (struct pr_handle *) p;
1023 
1024   return substitute_type (info, "const |");
1025 }
1026 
1027 /* Make a volatile qualified type.  */
1028 
1029 static bfd_boolean
pr_volatile_type(void * p)1030 pr_volatile_type (void *p)
1031 {
1032   struct pr_handle *info = (struct pr_handle *) p;
1033 
1034   return substitute_type (info, "volatile |");
1035 }
1036 
1037 /* Start accumulating a struct type.  */
1038 
1039 static bfd_boolean
pr_start_struct_type(void * p,const char * tag,unsigned int id,bfd_boolean structp,unsigned int size)1040 pr_start_struct_type (void *p, const char *tag, unsigned int id,
1041 		      bfd_boolean structp, unsigned int size)
1042 {
1043   struct pr_handle *info = (struct pr_handle *) p;
1044 
1045   info->indent += 2;
1046 
1047   if (! push_type (info, structp ? "struct " : "union "))
1048     return FALSE;
1049   if (tag != NULL)
1050     {
1051       if (! append_type (info, tag))
1052 	return FALSE;
1053     }
1054   else
1055     {
1056       char idbuf[20];
1057 
1058       sprintf (idbuf, "%%anon%u", id);
1059       if (! append_type (info, idbuf))
1060 	return FALSE;
1061     }
1062 
1063   if (! append_type (info, " {"))
1064     return FALSE;
1065   if (size != 0 || tag != NULL)
1066     {
1067       char ab[30];
1068 
1069       if (! append_type (info, " /*"))
1070 	return FALSE;
1071 
1072       if (size != 0)
1073 	{
1074 	  sprintf (ab, " size %u", size);
1075 	  if (! append_type (info, ab))
1076 	    return FALSE;
1077 	}
1078       if (tag != NULL)
1079 	{
1080 	  sprintf (ab, " id %u", id);
1081 	  if (! append_type (info, ab))
1082 	    return FALSE;
1083 	}
1084       if (! append_type (info, " */"))
1085 	return FALSE;
1086     }
1087   if (! append_type (info, "\n"))
1088     return FALSE;
1089 
1090   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
1091 
1092   return indent_type (info);
1093 }
1094 
1095 /* Output the visibility of a field in a struct.  */
1096 
1097 static bfd_boolean
pr_fix_visibility(struct pr_handle * info,enum debug_visibility visibility)1098 pr_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
1099 {
1100   const char *s = NULL;
1101   char *t;
1102   unsigned int len;
1103 
1104   assert (info->stack != NULL);
1105 
1106   if (info->stack->visibility == visibility)
1107     return TRUE;
1108 
1109   switch (visibility)
1110     {
1111     case DEBUG_VISIBILITY_PUBLIC:
1112       s = "public";
1113       break;
1114     case DEBUG_VISIBILITY_PRIVATE:
1115       s = "private";
1116       break;
1117     case DEBUG_VISIBILITY_PROTECTED:
1118       s = "protected";
1119       break;
1120     case DEBUG_VISIBILITY_IGNORE:
1121       s = "/* ignore */";
1122       break;
1123     default:
1124       abort ();
1125       return FALSE;
1126     }
1127 
1128   /* Trim off a trailing space in the struct string, to make the
1129      output look a bit better, then stick on the visibility string.  */
1130 
1131   t = info->stack->type;
1132   len = strlen (t);
1133   assert (t[len - 1] == ' ');
1134   t[len - 1] = '\0';
1135 
1136   if (! append_type (info, s)
1137       || ! append_type (info, ":\n")
1138       || ! indent_type (info))
1139     return FALSE;
1140 
1141   info->stack->visibility = visibility;
1142 
1143   return TRUE;
1144 }
1145 
1146 /* Add a field to a struct type.  */
1147 
1148 static bfd_boolean
pr_struct_field(void * p,const char * name,bfd_vma bitpos,bfd_vma bitsize,enum debug_visibility visibility)1149 pr_struct_field (void *p, const char *name, bfd_vma bitpos, bfd_vma bitsize,
1150 		 enum debug_visibility visibility)
1151 {
1152   struct pr_handle *info = (struct pr_handle *) p;
1153   char ab[20];
1154   char *t;
1155 
1156   if (! substitute_type (info, name))
1157     return FALSE;
1158 
1159   if (! append_type (info, "; /* "))
1160     return FALSE;
1161 
1162   if (bitsize != 0)
1163     {
1164       print_vma (bitsize, ab, TRUE, FALSE);
1165       if (! append_type (info, "bitsize ")
1166 	  || ! append_type (info, ab)
1167 	  || ! append_type (info, ", "))
1168 	return FALSE;
1169     }
1170 
1171   print_vma (bitpos, ab, TRUE, FALSE);
1172   if (! append_type (info, "bitpos ")
1173       || ! append_type (info, ab)
1174       || ! append_type (info, " */\n")
1175       || ! indent_type (info))
1176     return FALSE;
1177 
1178   t = pop_type (info);
1179   if (t == NULL)
1180     return FALSE;
1181 
1182   if (! pr_fix_visibility (info, visibility))
1183     return FALSE;
1184 
1185   return append_type (info, t);
1186 }
1187 
1188 /* Finish a struct type.  */
1189 
1190 static bfd_boolean
pr_end_struct_type(void * p)1191 pr_end_struct_type (void *p)
1192 {
1193   struct pr_handle *info = (struct pr_handle *) p;
1194   char *s;
1195 
1196   assert (info->stack != NULL);
1197   assert (info->indent >= 2);
1198 
1199   info->indent -= 2;
1200 
1201   /* Change the trailing indentation to have a close brace.  */
1202   s = info->stack->type + strlen (info->stack->type) - 2;
1203   assert (s[0] == ' ' && s[1] == ' ' && s[2] == '\0');
1204 
1205   *s++ = '}';
1206   *s = '\0';
1207 
1208   return TRUE;
1209 }
1210 
1211 /* Start a class type.  */
1212 
1213 static bfd_boolean
pr_start_class_type(void * p,const char * tag,unsigned int id,bfd_boolean structp,unsigned int size,bfd_boolean vptr,bfd_boolean ownvptr)1214 pr_start_class_type (void *p, const char *tag, unsigned int id,
1215 		     bfd_boolean structp, unsigned int size,
1216 		     bfd_boolean vptr, bfd_boolean ownvptr)
1217 {
1218   struct pr_handle *info = (struct pr_handle *) p;
1219   char *tv = NULL;
1220 
1221   info->indent += 2;
1222 
1223   if (vptr && ! ownvptr)
1224     {
1225       tv = pop_type (info);
1226       if (tv == NULL)
1227 	return FALSE;
1228     }
1229 
1230   if (! push_type (info, structp ? "class " : "union class "))
1231     return FALSE;
1232   if (tag != NULL)
1233     {
1234       if (! append_type (info, tag))
1235 	return FALSE;
1236     }
1237   else
1238     {
1239       char idbuf[20];
1240 
1241       sprintf (idbuf, "%%anon%u", id);
1242       if (! append_type (info, idbuf))
1243 	return FALSE;
1244     }
1245 
1246   if (! append_type (info, " {"))
1247     return FALSE;
1248   if (size != 0 || vptr || ownvptr || tag != NULL)
1249     {
1250       if (! append_type (info, " /*"))
1251 	return FALSE;
1252 
1253       if (size != 0)
1254 	{
1255 	  char ab[20];
1256 
1257 	  sprintf (ab, "%u", size);
1258 	  if (! append_type (info, " size ")
1259 	      || ! append_type (info, ab))
1260 	    return FALSE;
1261 	}
1262 
1263       if (vptr)
1264 	{
1265 	  if (! append_type (info, " vtable "))
1266 	    return FALSE;
1267 	  if (ownvptr)
1268 	    {
1269 	      if (! append_type (info, "self "))
1270 		return FALSE;
1271 	    }
1272 	  else
1273 	    {
1274 	      if (! append_type (info, tv)
1275 		  || ! append_type (info, " "))
1276 		return FALSE;
1277 	    }
1278 	}
1279 
1280       if (tag != NULL)
1281 	{
1282 	  char ab[30];
1283 
1284 	  sprintf (ab, " id %u", id);
1285 	  if (! append_type (info, ab))
1286 	    return FALSE;
1287 	}
1288 
1289       if (! append_type (info, " */"))
1290 	return FALSE;
1291     }
1292 
1293   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
1294 
1295   return (append_type (info, "\n")
1296 	  && indent_type (info));
1297 }
1298 
1299 /* Add a static member to a class.  */
1300 
1301 static bfd_boolean
pr_class_static_member(void * p,const char * name,const char * physname,enum debug_visibility visibility)1302 pr_class_static_member (void *p, const char *name, const char *physname,
1303 			enum debug_visibility visibility)
1304 {
1305   struct pr_handle *info = (struct pr_handle *) p;
1306   char *t;
1307 
1308   if (! substitute_type (info, name))
1309     return FALSE;
1310 
1311   if (! prepend_type (info, "static ")
1312       || ! append_type (info, "; /* ")
1313       || ! append_type (info, physname)
1314       || ! append_type (info, " */\n")
1315       || ! indent_type (info))
1316     return FALSE;
1317 
1318   t = pop_type (info);
1319   if (t == NULL)
1320     return FALSE;
1321 
1322   if (! pr_fix_visibility (info, visibility))
1323     return FALSE;
1324 
1325   return append_type (info, t);
1326 }
1327 
1328 /* Add a base class to a class.  */
1329 
1330 static bfd_boolean
pr_class_baseclass(void * p,bfd_vma bitpos,bfd_boolean is_virtual,enum debug_visibility visibility)1331 pr_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
1332 		    enum debug_visibility visibility)
1333 {
1334   struct pr_handle *info = (struct pr_handle *) p;
1335   char *t;
1336   const char *prefix;
1337   char ab[20];
1338   char *s, *l, *n;
1339 
1340   assert (info->stack != NULL && info->stack->next != NULL);
1341 
1342   if (! substitute_type (info, ""))
1343     return FALSE;
1344 
1345   t = pop_type (info);
1346   if (t == NULL)
1347     return FALSE;
1348 
1349   if (CONST_STRNEQ (t, "class "))
1350     t += sizeof "class " - 1;
1351 
1352   /* Push it back on to take advantage of the prepend_type and
1353      append_type routines.  */
1354   if (! push_type (info, t))
1355     return FALSE;
1356 
1357   if (is_virtual)
1358     {
1359       if (! prepend_type (info, "virtual "))
1360 	return FALSE;
1361     }
1362 
1363   switch (visibility)
1364     {
1365     case DEBUG_VISIBILITY_PUBLIC:
1366       prefix = "public ";
1367       break;
1368     case DEBUG_VISIBILITY_PROTECTED:
1369       prefix = "protected ";
1370       break;
1371     case DEBUG_VISIBILITY_PRIVATE:
1372       prefix = "private ";
1373       break;
1374     default:
1375       prefix = "/* unknown visibility */ ";
1376       break;
1377     }
1378 
1379   if (! prepend_type (info, prefix))
1380     return FALSE;
1381 
1382   if (bitpos != 0)
1383     {
1384       print_vma (bitpos, ab, TRUE, FALSE);
1385       if (! append_type (info, " /* bitpos ")
1386 	  || ! append_type (info, ab)
1387 	  || ! append_type (info, " */"))
1388 	return FALSE;
1389     }
1390 
1391   /* Now the top of the stack is something like "public A / * bitpos
1392      10 * /".  The next element on the stack is something like "class
1393      xx { / * size 8 * /\n...".  We want to substitute the top of the
1394      stack in before the {.  */
1395   s = strchr (info->stack->next->type, '{');
1396   assert (s != NULL);
1397   --s;
1398 
1399   /* If there is already a ':', then we already have a baseclass, and
1400      we must append this one after a comma.  */
1401   for (l = info->stack->next->type; l != s; l++)
1402     if (*l == ':')
1403       break;
1404   if (! prepend_type (info, l == s ? " : " : ", "))
1405     return FALSE;
1406 
1407   t = pop_type (info);
1408   if (t == NULL)
1409     return FALSE;
1410 
1411   n = (char *) xmalloc (strlen (info->stack->type) + strlen (t) + 1);
1412   memcpy (n, info->stack->type, s - info->stack->type);
1413   strcpy (n + (s - info->stack->type), t);
1414   strcat (n, s);
1415 
1416   free (info->stack->type);
1417   info->stack->type = n;
1418 
1419   free (t);
1420 
1421   return TRUE;
1422 }
1423 
1424 /* Start adding a method to a class.  */
1425 
1426 static bfd_boolean
pr_class_start_method(void * p,const char * name)1427 pr_class_start_method (void *p, const char *name)
1428 {
1429   struct pr_handle *info = (struct pr_handle *) p;
1430 
1431   assert (info->stack != NULL);
1432   info->stack->method = name;
1433   return TRUE;
1434 }
1435 
1436 /* Add a variant to a method.  */
1437 
1438 static bfd_boolean
pr_class_method_variant(void * p,const char * physname,enum debug_visibility visibility,bfd_boolean constp,bfd_boolean volatilep,bfd_vma voffset,bfd_boolean context)1439 pr_class_method_variant (void *p, const char *physname,
1440 			 enum debug_visibility visibility,
1441 			 bfd_boolean constp, bfd_boolean volatilep,
1442 			 bfd_vma voffset, bfd_boolean context)
1443 {
1444   struct pr_handle *info = (struct pr_handle *) p;
1445   char *method_type;
1446   char *context_type;
1447 
1448   assert (info->stack != NULL);
1449   assert (info->stack->next != NULL);
1450 
1451   /* Put the const and volatile qualifiers on the type.  */
1452   if (volatilep)
1453     {
1454       if (! append_type (info, " volatile"))
1455 	return FALSE;
1456     }
1457   if (constp)
1458     {
1459       if (! append_type (info, " const"))
1460 	return FALSE;
1461     }
1462 
1463   /* Stick the name of the method into its type.  */
1464   if (! substitute_type (info,
1465 			 (context
1466 			  ? info->stack->next->next->method
1467 			  : info->stack->next->method)))
1468     return FALSE;
1469 
1470   /* Get the type.  */
1471   method_type = pop_type (info);
1472   if (method_type == NULL)
1473     return FALSE;
1474 
1475   /* Pull off the context type if there is one.  */
1476   if (! context)
1477     context_type = NULL;
1478   else
1479     {
1480       context_type = pop_type (info);
1481       if (context_type == NULL)
1482 	return FALSE;
1483     }
1484 
1485   /* Now the top of the stack is the class.  */
1486 
1487   if (! pr_fix_visibility (info, visibility))
1488     return FALSE;
1489 
1490   if (! append_type (info, method_type)
1491       || ! append_type (info, " /* ")
1492       || ! append_type (info, physname)
1493       || ! append_type (info, " "))
1494     return FALSE;
1495   if (context || voffset != 0)
1496     {
1497       char ab[20];
1498 
1499       if (context)
1500 	{
1501 	  if (! append_type (info, "context ")
1502 	      || ! append_type (info, context_type)
1503 	      || ! append_type (info, " "))
1504 	    return FALSE;
1505 	}
1506       print_vma (voffset, ab, TRUE, FALSE);
1507       if (! append_type (info, "voffset ")
1508 	  || ! append_type (info, ab))
1509 	return FALSE;
1510     }
1511 
1512   return (append_type (info, " */;\n")
1513 	  && indent_type (info));
1514 }
1515 
1516 /* Add a static variant to a method.  */
1517 
1518 static bfd_boolean
pr_class_static_method_variant(void * p,const char * physname,enum debug_visibility visibility,bfd_boolean constp,bfd_boolean volatilep)1519 pr_class_static_method_variant (void *p, const char *physname,
1520 				enum debug_visibility visibility,
1521 				bfd_boolean constp, bfd_boolean volatilep)
1522 {
1523   struct pr_handle *info = (struct pr_handle *) p;
1524   char *method_type;
1525 
1526   assert (info->stack != NULL);
1527   assert (info->stack->next != NULL);
1528   assert (info->stack->next->method != NULL);
1529 
1530   /* Put the const and volatile qualifiers on the type.  */
1531   if (volatilep)
1532     {
1533       if (! append_type (info, " volatile"))
1534 	return FALSE;
1535     }
1536   if (constp)
1537     {
1538       if (! append_type (info, " const"))
1539 	return FALSE;
1540     }
1541 
1542   /* Mark it as static.  */
1543   if (! prepend_type (info, "static "))
1544     return FALSE;
1545 
1546   /* Stick the name of the method into its type.  */
1547   if (! substitute_type (info, info->stack->next->method))
1548     return FALSE;
1549 
1550   /* Get the type.  */
1551   method_type = pop_type (info);
1552   if (method_type == NULL)
1553     return FALSE;
1554 
1555   /* Now the top of the stack is the class.  */
1556 
1557   if (! pr_fix_visibility (info, visibility))
1558     return FALSE;
1559 
1560   return (append_type (info, method_type)
1561 	  && append_type (info, " /* ")
1562 	  && append_type (info, physname)
1563 	  && append_type (info, " */;\n")
1564 	  && indent_type (info));
1565 }
1566 
1567 /* Finish up a method.  */
1568 
1569 static bfd_boolean
pr_class_end_method(void * p)1570 pr_class_end_method (void *p)
1571 {
1572   struct pr_handle *info = (struct pr_handle *) p;
1573 
1574   info->stack->method = NULL;
1575   return TRUE;
1576 }
1577 
1578 /* Finish up a class.  */
1579 
1580 static bfd_boolean
pr_end_class_type(void * p)1581 pr_end_class_type (void *p)
1582 {
1583   return pr_end_struct_type (p);
1584 }
1585 
1586 /* Push a type on the stack using a typedef name.  */
1587 
1588 static bfd_boolean
pr_typedef_type(void * p,const char * name)1589 pr_typedef_type (void *p, const char *name)
1590 {
1591   struct pr_handle *info = (struct pr_handle *) p;
1592 
1593   return push_type (info, name);
1594 }
1595 
1596 /* Push a type on the stack using a tag name.  */
1597 
1598 static bfd_boolean
pr_tag_type(void * p,const char * name,unsigned int id,enum debug_type_kind kind)1599 pr_tag_type (void *p, const char *name, unsigned int id,
1600 	     enum debug_type_kind kind)
1601 {
1602   struct pr_handle *info = (struct pr_handle *) p;
1603   const char *t, *tag;
1604   char idbuf[20];
1605 
1606   switch (kind)
1607     {
1608     case DEBUG_KIND_STRUCT:
1609       t = "struct ";
1610       break;
1611     case DEBUG_KIND_UNION:
1612       t = "union ";
1613       break;
1614     case DEBUG_KIND_ENUM:
1615       t = "enum ";
1616       break;
1617     case DEBUG_KIND_CLASS:
1618       t = "class ";
1619       break;
1620     case DEBUG_KIND_UNION_CLASS:
1621       t = "union class ";
1622       break;
1623     default:
1624       abort ();
1625       return FALSE;
1626     }
1627 
1628   if (! push_type (info, t))
1629     return FALSE;
1630   if (name != NULL)
1631     tag = name;
1632   else
1633     {
1634       sprintf (idbuf, "%%anon%u", id);
1635       tag = idbuf;
1636     }
1637 
1638   if (! append_type (info, tag))
1639     return FALSE;
1640   if (name != NULL && kind != DEBUG_KIND_ENUM)
1641     {
1642       sprintf (idbuf, " /* id %u */", id);
1643       if (! append_type (info, idbuf))
1644 	return FALSE;
1645     }
1646 
1647   return TRUE;
1648 }
1649 
1650 /* Output a typedef.  */
1651 
1652 static bfd_boolean
pr_typdef(void * p,const char * name)1653 pr_typdef (void *p, const char *name)
1654 {
1655   struct pr_handle *info = (struct pr_handle *) p;
1656   char *s;
1657 
1658   if (! substitute_type (info, name))
1659     return FALSE;
1660 
1661   s = pop_type (info);
1662   if (s == NULL)
1663     return FALSE;
1664 
1665   indent (info);
1666   fprintf (info->f, "typedef %s;\n", s);
1667 
1668   free (s);
1669 
1670   return TRUE;
1671 }
1672 
1673 /* Output a tag.  The tag should already be in the string on the
1674    stack, so all we have to do here is print it out.  */
1675 
1676 static bfd_boolean
pr_tag(void * p,const char * name ATTRIBUTE_UNUSED)1677 pr_tag (void *p, const char *name ATTRIBUTE_UNUSED)
1678 {
1679   struct pr_handle *info = (struct pr_handle *) p;
1680   char *t;
1681 
1682   t = pop_type (info);
1683   if (t == NULL)
1684     return FALSE;
1685 
1686   indent (info);
1687   fprintf (info->f, "%s;\n", t);
1688 
1689   free (t);
1690 
1691   return TRUE;
1692 }
1693 
1694 /* Output an integer constant.  */
1695 
1696 static bfd_boolean
pr_int_constant(void * p,const char * name,bfd_vma val)1697 pr_int_constant (void *p, const char *name, bfd_vma val)
1698 {
1699   struct pr_handle *info = (struct pr_handle *) p;
1700   char ab[20];
1701 
1702   indent (info);
1703   print_vma (val, ab, FALSE, FALSE);
1704   fprintf (info->f, "const int %s = %s;\n", name, ab);
1705   return TRUE;
1706 }
1707 
1708 /* Output a floating point constant.  */
1709 
1710 static bfd_boolean
pr_float_constant(void * p,const char * name,double val)1711 pr_float_constant (void *p, const char *name, double val)
1712 {
1713   struct pr_handle *info = (struct pr_handle *) p;
1714 
1715   indent (info);
1716   fprintf (info->f, "const double %s = %g;\n", name, val);
1717   return TRUE;
1718 }
1719 
1720 /* Output a typed constant.  */
1721 
1722 static bfd_boolean
pr_typed_constant(void * p,const char * name,bfd_vma val)1723 pr_typed_constant (void *p, const char *name, bfd_vma val)
1724 {
1725   struct pr_handle *info = (struct pr_handle *) p;
1726   char *t;
1727   char ab[20];
1728 
1729   t = pop_type (info);
1730   if (t == NULL)
1731     return FALSE;
1732 
1733   indent (info);
1734   print_vma (val, ab, FALSE, FALSE);
1735   fprintf (info->f, "const %s %s = %s;\n", t, name, ab);
1736 
1737   free (t);
1738 
1739   return TRUE;
1740 }
1741 
1742 /* Output a variable.  */
1743 
1744 static bfd_boolean
pr_variable(void * p,const char * name,enum debug_var_kind kind,bfd_vma val)1745 pr_variable (void *p, const char *name, enum debug_var_kind kind,
1746 	     bfd_vma val)
1747 {
1748   struct pr_handle *info = (struct pr_handle *) p;
1749   char *t;
1750   char ab[20];
1751 
1752   if (! substitute_type (info, name))
1753     return FALSE;
1754 
1755   t = pop_type (info);
1756   if (t == NULL)
1757     return FALSE;
1758 
1759   indent (info);
1760   switch (kind)
1761     {
1762     case DEBUG_STATIC:
1763     case DEBUG_LOCAL_STATIC:
1764       fprintf (info->f, "static ");
1765       break;
1766     case DEBUG_REGISTER:
1767       fprintf (info->f, "register ");
1768       break;
1769     default:
1770       break;
1771     }
1772   print_vma (val, ab, TRUE, TRUE);
1773   fprintf (info->f, "%s /* %s */;\n", t, ab);
1774 
1775   free (t);
1776 
1777   return TRUE;
1778 }
1779 
1780 /* Start outputting a function.  */
1781 
1782 static bfd_boolean
pr_start_function(void * p,const char * name,bfd_boolean global)1783 pr_start_function (void *p, const char *name, bfd_boolean global)
1784 {
1785   struct pr_handle *info = (struct pr_handle *) p;
1786   char *t;
1787 
1788   if (! substitute_type (info, name))
1789     return FALSE;
1790 
1791   t = pop_type (info);
1792   if (t == NULL)
1793     return FALSE;
1794 
1795   indent (info);
1796   if (! global)
1797     fprintf (info->f, "static ");
1798   fprintf (info->f, "%s (", t);
1799 
1800   info->parameter = 1;
1801 
1802   return TRUE;
1803 }
1804 
1805 /* Output a function parameter.  */
1806 
1807 static bfd_boolean
pr_function_parameter(void * p,const char * name,enum debug_parm_kind kind,bfd_vma val)1808 pr_function_parameter (void *p, const char *name,
1809 		       enum debug_parm_kind kind, bfd_vma val)
1810 {
1811   struct pr_handle *info = (struct pr_handle *) p;
1812   char *t;
1813   char ab[20];
1814 
1815   if (kind == DEBUG_PARM_REFERENCE
1816       || kind == DEBUG_PARM_REF_REG)
1817     {
1818       if (! pr_reference_type (p))
1819 	return FALSE;
1820     }
1821 
1822   if (! substitute_type (info, name))
1823     return FALSE;
1824 
1825   t = pop_type (info);
1826   if (t == NULL)
1827     return FALSE;
1828 
1829   if (info->parameter != 1)
1830     fprintf (info->f, ", ");
1831 
1832   if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
1833     fprintf (info->f, "register ");
1834 
1835   print_vma (val, ab, TRUE, TRUE);
1836   fprintf (info->f, "%s /* %s */", t, ab);
1837 
1838   free (t);
1839 
1840   ++info->parameter;
1841 
1842   return TRUE;
1843 }
1844 
1845 /* Start writing out a block.  */
1846 
1847 static bfd_boolean
pr_start_block(void * p,bfd_vma addr)1848 pr_start_block (void *p, bfd_vma addr)
1849 {
1850   struct pr_handle *info = (struct pr_handle *) p;
1851   char ab[20];
1852 
1853   if (info->parameter > 0)
1854     {
1855       fprintf (info->f, ")\n");
1856       info->parameter = 0;
1857     }
1858 
1859   indent (info);
1860   print_vma (addr, ab, TRUE, TRUE);
1861   fprintf (info->f, "{ /* %s */\n", ab);
1862 
1863   info->indent += 2;
1864 
1865   return TRUE;
1866 }
1867 
1868 /* Write out line number information.  */
1869 
1870 static bfd_boolean
pr_lineno(void * p,const char * filename,unsigned long lineno,bfd_vma addr)1871 pr_lineno (void *p, const char *filename, unsigned long lineno, bfd_vma addr)
1872 {
1873   struct pr_handle *info = (struct pr_handle *) p;
1874   char ab[20];
1875 
1876   indent (info);
1877   print_vma (addr, ab, TRUE, TRUE);
1878   fprintf (info->f, "/* file %s line %lu addr %s */\n", filename, lineno, ab);
1879 
1880   return TRUE;
1881 }
1882 
1883 /* Finish writing out a block.  */
1884 
1885 static bfd_boolean
pr_end_block(void * p,bfd_vma addr)1886 pr_end_block (void *p, bfd_vma addr)
1887 {
1888   struct pr_handle *info = (struct pr_handle *) p;
1889   char ab[20];
1890 
1891   info->indent -= 2;
1892 
1893   indent (info);
1894   print_vma (addr, ab, TRUE, TRUE);
1895   fprintf (info->f, "} /* %s */\n", ab);
1896 
1897   return TRUE;
1898 }
1899 
1900 /* Finish writing out a function.  */
1901 
1902 static bfd_boolean
pr_end_function(void * p ATTRIBUTE_UNUSED)1903 pr_end_function (void *p ATTRIBUTE_UNUSED)
1904 {
1905   return TRUE;
1906 }
1907 
1908 /* Tags style generation functions start here.  */
1909 
1910 /* Variables for address to line translation.  */
1911 static bfd_vma pc;
1912 static const char *filename;
1913 static const char *functionname;
1914 static unsigned int line;
1915 static bfd_boolean found;
1916 
1917 /* Look for an address in a section.  This is called via
1918    bfd_map_over_sections.  */
1919 
1920 static void
find_address_in_section(bfd * abfd,asection * section,void * data)1921 find_address_in_section (bfd *abfd, asection *section, void *data)
1922 {
1923   bfd_vma vma;
1924   bfd_size_type size;
1925   asymbol **syms = (asymbol **) data;
1926 
1927   if (found)
1928     return;
1929 
1930   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0)
1931     return;
1932 
1933   vma = bfd_get_section_vma (abfd, section);
1934   if (pc < vma)
1935     return;
1936 
1937   size = bfd_get_section_size (section);
1938   if (pc >= vma + size)
1939     return;
1940 
1941   found = bfd_find_nearest_line (abfd, section, syms, pc - vma,
1942 				 &filename, &functionname, &line);
1943 }
1944 
1945 static void
translate_addresses(bfd * abfd,char * addr_hex,FILE * f,asymbol ** syms)1946 translate_addresses (bfd *abfd, char *addr_hex, FILE *f, asymbol **syms)
1947 {
1948   pc = bfd_scan_vma (addr_hex, NULL, 16);
1949   found = FALSE;
1950   bfd_map_over_sections (abfd, find_address_in_section, syms);
1951 
1952   if (! found)
1953     fprintf (f, "??");
1954   else
1955     fprintf (f, "%u", line);
1956 }
1957 
1958 /* Start a new compilation unit.  */
1959 
1960 static bfd_boolean
tg_start_compilation_unit(void * p,const char * fname ATTRIBUTE_UNUSED)1961 tg_start_compilation_unit (void * p, const char *fname ATTRIBUTE_UNUSED)
1962 {
1963   struct pr_handle *info = (struct pr_handle *) p;
1964 
1965   free (info->filename);
1966   /* Should it be relative? best way to do it here?.  */
1967   info->filename = strdup (fname);
1968 
1969   return TRUE;
1970 }
1971 
1972 /* Start a source file within a compilation unit.  */
1973 
1974 static bfd_boolean
tg_start_source(void * p,const char * fname)1975 tg_start_source (void *p, const char *fname)
1976 {
1977   struct pr_handle *info = (struct pr_handle *) p;
1978 
1979   free (info->filename);
1980   /* Should it be relative? best way to do it here?.  */
1981   info->filename = strdup (fname);
1982 
1983   return TRUE;
1984 }
1985 
1986 /* Push an enum type onto the type stack.  */
1987 
1988 static bfd_boolean
tg_enum_type(void * p,const char * tag,const char ** names,bfd_signed_vma * values)1989 tg_enum_type (void *p, const char *tag, const char **names,
1990 	      bfd_signed_vma *values)
1991 {
1992   struct pr_handle *info = (struct pr_handle *) p;
1993   unsigned int i;
1994   const char *name;
1995   char ab[20];
1996 
1997   if (! pr_enum_type (p, tag, names, values))
1998     return FALSE;
1999 
2000   name = tag ? tag : "unknown";
2001   /* Generate an entry for the enum.  */
2002   if (tag)
2003     fprintf (info->f, "%s\t%s\t0;\"\tkind:e\ttype:%s\n", tag,
2004 	     info->filename, info->stack->type);
2005 
2006   /* Generate entries for the values.  */
2007   if (names != NULL)
2008     {
2009       for (i = 0; names[i] != NULL; i++)
2010 	{
2011 	  print_vma (values[i], ab, FALSE, FALSE);
2012 	  fprintf (info->f, "%s\t%s\t0;\"\tkind:g\tenum:%s\tvalue:%s\n",
2013 		   names[i], info->filename, name, ab);
2014 	}
2015     }
2016 
2017   return TRUE;
2018 }
2019 
2020 /* Start accumulating a struct type.  */
2021 
2022 static bfd_boolean
tg_start_struct_type(void * p,const char * tag,unsigned int id,bfd_boolean structp,unsigned int size ATTRIBUTE_UNUSED)2023 tg_start_struct_type (void *p, const char *tag, unsigned int id,
2024 		      bfd_boolean structp,
2025 		      unsigned int size ATTRIBUTE_UNUSED)
2026 {
2027   struct pr_handle *info = (struct pr_handle *) p;
2028   const char *name;
2029   char idbuf[20];
2030 
2031   if (tag != NULL)
2032     name = tag;
2033   else
2034     {
2035       name = idbuf;
2036       sprintf (idbuf, "%%anon%u", id);
2037     }
2038 
2039   if (! push_type (info, name))
2040     return FALSE;
2041 
2042   info->stack->flavor = structp ? "struct" : "union";
2043 
2044   fprintf (info->f, "%s\t%s\t0;\"\tkind:%c\n", name, info->filename,
2045 	   info->stack->flavor[0]);
2046 
2047   info->stack->visibility = DEBUG_VISIBILITY_PUBLIC;
2048 
2049   return indent_type (info);
2050 }
2051 
2052 /* Output the visibility of a field in a struct.  */
2053 
2054 static bfd_boolean
tg_fix_visibility(struct pr_handle * info,enum debug_visibility visibility)2055 tg_fix_visibility (struct pr_handle *info, enum debug_visibility visibility)
2056 {
2057   assert (info->stack != NULL);
2058 
2059   if (info->stack->visibility == visibility)
2060     return TRUE;
2061 
2062   assert (info->stack->visibility != DEBUG_VISIBILITY_IGNORE);
2063 
2064   info->stack->visibility = visibility;
2065 
2066   return TRUE;
2067 }
2068 
2069 /* Add a field to a struct type.  */
2070 
2071 static bfd_boolean
tg_struct_field(void * p,const char * name,bfd_vma bitpos ATTRIBUTE_UNUSED,bfd_vma bitsize ATTRIBUTE_UNUSED,enum debug_visibility visibility)2072 tg_struct_field (void *p, const char *name, bfd_vma bitpos ATTRIBUTE_UNUSED,
2073 		 bfd_vma bitsize ATTRIBUTE_UNUSED,
2074 		 enum debug_visibility visibility)
2075 {
2076   struct pr_handle *info = (struct pr_handle *) p;
2077   char *t;
2078 
2079   t = pop_type (info);
2080   if (t == NULL)
2081     return FALSE;
2082 
2083   if (! tg_fix_visibility (info, visibility))
2084     return FALSE;
2085 
2086   /* It happens, a bug? */
2087   if (! name[0])
2088     return TRUE;
2089 
2090   fprintf (info->f, "%s\t%s\t0;\"\tkind:m\ttype:%s\t%s:%s\taccess:%s\n",
2091 	   name, info->filename, t, info->stack->flavor, info->stack->type,
2092 	   visibility_name (visibility));
2093 
2094   return TRUE;
2095 }
2096 
2097 /* Finish a struct type.  */
2098 
2099 static bfd_boolean
tg_end_struct_type(void * p ATTRIBUTE_UNUSED)2100 tg_end_struct_type (void *p ATTRIBUTE_UNUSED)
2101 {
2102   assert (((struct pr_handle *) p)->stack != NULL);
2103 
2104   return TRUE;
2105 }
2106 
2107 /* Start a class type.  */
2108 
2109 static bfd_boolean
tg_start_class_type(void * p,const char * tag,unsigned int id,bfd_boolean structp,unsigned int size,bfd_boolean vptr,bfd_boolean ownvptr)2110 tg_start_class_type (void *p, const char *tag, unsigned int id,
2111 		     bfd_boolean structp, unsigned int size,
2112 		     bfd_boolean vptr, bfd_boolean ownvptr)
2113 {
2114   struct pr_handle *info = (struct pr_handle *) p;
2115   char *tv = NULL;
2116   const char *name;
2117 
2118   info->indent += 2;
2119 
2120   if (vptr && ! ownvptr)
2121     {
2122       tv = pop_type (info);
2123       if (tv == NULL)
2124 	return FALSE;
2125     }
2126 
2127   if (tag != NULL)
2128     name = tag;
2129   else
2130     {
2131       char idbuf[20];
2132 
2133       sprintf (idbuf, "%%anon%u", id);
2134       name = idbuf;
2135     }
2136 
2137   if (! push_type (info, name))
2138     return FALSE;
2139 
2140   info->stack->flavor = structp ? "class" : "union class";
2141   info->stack->parents = NULL;
2142   info->stack->num_parents = 0;
2143 
2144   if (size != 0 || vptr || ownvptr || tag != NULL)
2145     {
2146       if (vptr)
2147 	{
2148 	  if (! append_type (info, " vtable "))
2149 	    return FALSE;
2150 	  if (ownvptr)
2151 	    {
2152 	      if (! append_type (info, "self "))
2153 		return FALSE;
2154 	    }
2155 	  else
2156 	    {
2157 	      if (! append_type (info, tv)
2158 		  || ! append_type (info, " "))
2159 		return FALSE;
2160 	    }
2161 	}
2162     }
2163 
2164   info->stack->visibility = DEBUG_VISIBILITY_PRIVATE;
2165 
2166   return TRUE;
2167 }
2168 
2169 /* Add a static member to a class.  */
2170 
2171 static bfd_boolean
tg_class_static_member(void * p,const char * name,const char * physname ATTRIBUTE_UNUSED,enum debug_visibility visibility)2172 tg_class_static_member (void *p, const char *name,
2173 			const char *physname ATTRIBUTE_UNUSED,
2174 			enum debug_visibility visibility)
2175 {
2176   struct pr_handle *info = (struct pr_handle *) p;
2177   char *t;
2178   int len_var, len_class;
2179   char *full_name;
2180 
2181   len_var = strlen (name);
2182   len_class = strlen (info->stack->next->type);
2183   full_name = (char *) xmalloc (len_var + len_class + 3);
2184   if (! full_name)
2185     return FALSE;
2186   sprintf (full_name, "%s::%s", info->stack->next->type, name);
2187 
2188   if (! substitute_type (info, full_name))
2189     {
2190       free (full_name);
2191       return FALSE;
2192     }
2193 
2194   if (! prepend_type (info, "static "))
2195     {
2196       free (full_name);
2197       return FALSE;
2198     }
2199 
2200   t = pop_type (info);
2201   if (t == NULL)
2202     {
2203       free (full_name);
2204       return FALSE;
2205     }
2206 
2207   if (! tg_fix_visibility (info, visibility))
2208     {
2209       free (t);
2210       free (full_name);
2211       return FALSE;
2212     }
2213 
2214   fprintf (info->f, "%s\t%s\t0;\"\tkind:x\ttype:%s\tclass:%s\taccess:%s\n",
2215 	   name, info->filename, t, info->stack->type,
2216 	   visibility_name (visibility));
2217   free (t);
2218   free (full_name);
2219 
2220   return TRUE;
2221 }
2222 
2223 /* Add a base class to a class.  */
2224 
2225 static bfd_boolean
tg_class_baseclass(void * p,bfd_vma bitpos ATTRIBUTE_UNUSED,bfd_boolean is_virtual,enum debug_visibility visibility)2226 tg_class_baseclass (void *p, bfd_vma bitpos ATTRIBUTE_UNUSED,
2227 		    bfd_boolean is_virtual, enum debug_visibility visibility)
2228 {
2229   struct pr_handle *info = (struct pr_handle *) p;
2230   char *t;
2231   const char *prefix;
2232 
2233   assert (info->stack != NULL && info->stack->next != NULL);
2234 
2235   t = pop_type (info);
2236   if (t == NULL)
2237     return FALSE;
2238 
2239   if (CONST_STRNEQ (t, "class "))
2240     t += sizeof "class " - 1;
2241 
2242   /* Push it back on to take advantage of the prepend_type and
2243      append_type routines.  */
2244   if (! push_type (info, t))
2245     return FALSE;
2246 
2247   if (is_virtual)
2248     {
2249       if (! prepend_type (info, "virtual "))
2250 	return FALSE;
2251     }
2252 
2253   switch (visibility)
2254     {
2255     case DEBUG_VISIBILITY_PUBLIC:
2256       prefix = "public ";
2257       break;
2258     case DEBUG_VISIBILITY_PROTECTED:
2259       prefix = "protected ";
2260       break;
2261     case DEBUG_VISIBILITY_PRIVATE:
2262       prefix = "private ";
2263       break;
2264     default:
2265       prefix = "/* unknown visibility */ ";
2266       break;
2267     }
2268 
2269   if (! prepend_type (info, prefix))
2270     return FALSE;
2271 
2272   t = pop_type (info);
2273   if (t == NULL)
2274     return FALSE;
2275 
2276   if (info->stack->num_parents && ! append_parent (info, ", "))
2277     return FALSE;
2278 
2279   if (! append_parent (info, t))
2280     return FALSE;
2281   info->stack->num_parents++;
2282 
2283   free (t);
2284 
2285   return TRUE;
2286 }
2287 
2288 /* Add a variant to a method.  */
2289 
2290 static bfd_boolean
tg_class_method_variant(void * p,const char * physname ATTRIBUTE_UNUSED,enum debug_visibility visibility,bfd_boolean constp,bfd_boolean volatilep,bfd_vma voffset ATTRIBUTE_UNUSED,bfd_boolean context)2291 tg_class_method_variant (void *p, const char *physname ATTRIBUTE_UNUSED,
2292 			 enum debug_visibility visibility,
2293 			 bfd_boolean constp, bfd_boolean volatilep,
2294 			 bfd_vma voffset ATTRIBUTE_UNUSED,
2295 			 bfd_boolean context)
2296 {
2297   struct pr_handle *info = (struct pr_handle *) p;
2298   char *method_type;
2299   char *context_type;
2300   char *method_name;
2301 
2302   assert (info->stack != NULL);
2303   assert (info->stack->next != NULL);
2304 
2305   /* Put the const and volatile qualifiers on the type.  */
2306   if (volatilep)
2307     {
2308       if (! append_type (info, " volatile"))
2309 	return FALSE;
2310     }
2311   if (constp)
2312     {
2313       if (! append_type (info, " const"))
2314 	return FALSE;
2315     }
2316 
2317   method_name = strdup (context ? info->stack->next->next->method
2318 			: info->stack->next->method);
2319 
2320   /* Stick the name of the method into its type.  */
2321   if (! substitute_type (info, method_name))
2322     {
2323       free (method_name);
2324       return FALSE;
2325     }
2326 
2327   /* Get the type.  */
2328   method_type = pop_type (info);
2329   if (method_type == NULL)
2330     {
2331       free (method_name);
2332       return FALSE;
2333     }
2334 
2335   /* Pull off the context type if there is one.  */
2336   if (! context)
2337     context_type = NULL;
2338   else
2339     {
2340       context_type = pop_type (info);
2341       if (context_type == NULL)
2342 	{
2343 	  free (method_type);
2344 	  free (method_name);
2345 	  return FALSE;
2346 	}
2347     }
2348 
2349   /* Now the top of the stack is the class.  */
2350   if (! tg_fix_visibility (info, visibility))
2351     {
2352       free (method_type);
2353       free (method_name);
2354       free (context_type);
2355       return FALSE;
2356     }
2357 
2358   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\n",
2359 	   method_name, info->filename, method_type, info->stack->type);
2360   free (method_type);
2361   free (method_name);
2362   free (context_type);
2363 
2364   return TRUE;
2365 }
2366 
2367 /* Add a static variant to a method.  */
2368 
2369 static bfd_boolean
tg_class_static_method_variant(void * p,const char * physname ATTRIBUTE_UNUSED,enum debug_visibility visibility,bfd_boolean constp,bfd_boolean volatilep)2370 tg_class_static_method_variant (void *p,
2371 				const char *physname ATTRIBUTE_UNUSED,
2372 				enum debug_visibility visibility,
2373 				bfd_boolean constp, bfd_boolean volatilep)
2374 {
2375   struct pr_handle *info = (struct pr_handle *) p;
2376   char *method_type;
2377   char *method_name;
2378 
2379   assert (info->stack != NULL);
2380   assert (info->stack->next != NULL);
2381   assert (info->stack->next->method != NULL);
2382 
2383   /* Put the const and volatile qualifiers on the type.  */
2384   if (volatilep)
2385     {
2386       if (! append_type (info, " volatile"))
2387 	return FALSE;
2388     }
2389   if (constp)
2390     {
2391       if (! append_type (info, " const"))
2392 	return FALSE;
2393     }
2394 
2395   /* Mark it as static.  */
2396   if (! prepend_type (info, "static "))
2397     return FALSE;
2398 
2399   method_name = strdup (info->stack->next->method);
2400   /* Stick the name of the method into its type.  */
2401   if (! substitute_type (info, info->stack->next->method))
2402     {
2403       free (method_name);
2404       return FALSE;
2405     }
2406 
2407   /* Get the type.  */
2408   method_type = pop_type (info);
2409   if (method_type == NULL)
2410     {
2411       free (method_name);
2412       return FALSE;
2413     }
2414 
2415   /* Now the top of the stack is the class.  */
2416   if (! tg_fix_visibility (info, visibility))
2417     {
2418       free (method_type);
2419       free (method_name);
2420       return FALSE;
2421     }
2422 
2423   fprintf (info->f, "%s\t%s\t0;\"\tkind:p\ttype:%s\tclass:%s\taccess:%s\n",
2424 	   method_name, info->filename, method_type, info->stack->type,
2425 	   visibility_name (visibility));
2426   free (method_type);
2427   free (method_name);
2428 
2429   return TRUE;
2430 }
2431 
2432 /* Finish up a class.  */
2433 
2434 static bfd_boolean
tg_end_class_type(void * p)2435 tg_end_class_type (void *p)
2436 {
2437   struct pr_handle *info = (struct pr_handle *) p;
2438 
2439   fprintf (info->f, "%s\t%s\t0;\"\tkind:c\ttype:%s", info->stack->type,
2440 	   info->filename, info->stack->flavor);
2441   if (info->stack->num_parents)
2442     {
2443       fprintf  (info->f, "\tinherits:%s", info->stack->parents);
2444       free (info->stack->parents);
2445     }
2446   fputc ('\n', info->f);
2447 
2448   return tg_end_struct_type (p);
2449 }
2450 
2451 /* Push a type on the stack using a tag name.  */
2452 
2453 static bfd_boolean
tg_tag_type(void * p,const char * name,unsigned int id,enum debug_type_kind kind)2454 tg_tag_type (void *p, const char *name, unsigned int id,
2455 	     enum debug_type_kind kind)
2456 {
2457   struct pr_handle *info = (struct pr_handle *) p;
2458   const char *t, *tag;
2459   char idbuf[20];
2460 
2461   switch (kind)
2462     {
2463     case DEBUG_KIND_STRUCT:
2464       t = "struct ";
2465       break;
2466     case DEBUG_KIND_UNION:
2467       t = "union ";
2468       break;
2469     case DEBUG_KIND_ENUM:
2470       t = "enum ";
2471       break;
2472     case DEBUG_KIND_CLASS:
2473       t = "class ";
2474       break;
2475     case DEBUG_KIND_UNION_CLASS:
2476       t = "union class ";
2477       break;
2478     default:
2479       abort ();
2480       return FALSE;
2481     }
2482 
2483   if (! push_type (info, t))
2484     return FALSE;
2485   if (name != NULL)
2486     tag = name;
2487   else
2488     {
2489       sprintf (idbuf, "%%anon%u", id);
2490       tag = idbuf;
2491     }
2492 
2493   if (! append_type (info, tag))
2494     return FALSE;
2495 
2496   return TRUE;
2497 }
2498 
2499 /* Output a typedef.  */
2500 
2501 static bfd_boolean
tg_typdef(void * p,const char * name)2502 tg_typdef (void *p, const char *name)
2503 {
2504   struct pr_handle *info = (struct pr_handle *) p;
2505   char *s;
2506 
2507   s = pop_type (info);
2508   if (s == NULL)
2509     return FALSE;
2510 
2511   fprintf (info->f, "%s\t%s\t0;\"\tkind:t\ttype:%s\n", name,
2512 	   info->filename, s);
2513 
2514   free (s);
2515 
2516   return TRUE;
2517 }
2518 
2519 /* Output a tag.  The tag should already be in the string on the
2520    stack, so all we have to do here is print it out.  */
2521 
2522 static bfd_boolean
tg_tag(void * p ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED)2523 tg_tag (void *p ATTRIBUTE_UNUSED, const char *name ATTRIBUTE_UNUSED)
2524 {
2525   struct pr_handle *info = (struct pr_handle *) p;
2526   char *t;
2527 
2528   t = pop_type (info);
2529   if (t == NULL)
2530     return FALSE;
2531   free (t);
2532 
2533   return TRUE;
2534 }
2535 
2536 /* Output an integer constant.  */
2537 
2538 static bfd_boolean
tg_int_constant(void * p,const char * name,bfd_vma val)2539 tg_int_constant (void *p, const char *name, bfd_vma val)
2540 {
2541   struct pr_handle *info = (struct pr_handle *) p;
2542   char ab[20];
2543 
2544   indent (info);
2545   print_vma (val, ab, FALSE, FALSE);
2546   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const int\tvalue:%s\n",
2547 	   name, info->filename, ab);
2548   return TRUE;
2549 }
2550 
2551 /* Output a floating point constant.  */
2552 
2553 static bfd_boolean
tg_float_constant(void * p,const char * name,double val)2554 tg_float_constant (void *p, const char *name, double val)
2555 {
2556   struct pr_handle *info = (struct pr_handle *) p;
2557 
2558   indent (info);
2559   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const double\tvalue:%g\n",
2560 	   name, info->filename, val);
2561   return TRUE;
2562 }
2563 
2564 /* Output a typed constant.  */
2565 
2566 static bfd_boolean
tg_typed_constant(void * p,const char * name,bfd_vma val)2567 tg_typed_constant (void *p, const char *name, bfd_vma val)
2568 {
2569   struct pr_handle *info = (struct pr_handle *) p;
2570   char *t;
2571   char ab[20];
2572 
2573   t = pop_type (info);
2574   if (t == NULL)
2575     return FALSE;
2576 
2577   indent (info);
2578   print_vma (val, ab, FALSE, FALSE);
2579   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:const %s\tvalue:%s\n",
2580 	   name, info->filename, t, ab);
2581 
2582   free (t);
2583 
2584   return TRUE;
2585 }
2586 
2587 /* Output a variable.  */
2588 
2589 static bfd_boolean
tg_variable(void * p,const char * name,enum debug_var_kind kind,bfd_vma val ATTRIBUTE_UNUSED)2590 tg_variable (void *p, const char *name, enum debug_var_kind kind,
2591 	     bfd_vma val ATTRIBUTE_UNUSED)
2592 {
2593   struct pr_handle *info = (struct pr_handle *) p;
2594   char *t, *dname, *from_class;
2595 
2596   t = pop_type (info);
2597   if (t == NULL)
2598     return FALSE;
2599 
2600   dname = NULL;
2601   if (info->demangler)
2602     dname = info->demangler (info->abfd, name, DMGL_ANSI | DMGL_PARAMS);
2603 
2604   from_class = NULL;
2605   if (dname != NULL)
2606     {
2607       char *sep;
2608       sep = strstr (dname, "::");
2609       if (sep)
2610 	{
2611 	  *sep = 0;
2612 	  name = sep + 2;
2613 	  from_class = dname;
2614 	}
2615       else
2616 	/* Obscure types as vts and type_info nodes.  */
2617 	name = dname;
2618     }
2619 
2620   fprintf (info->f, "%s\t%s\t0;\"\tkind:v\ttype:%s", name, info->filename, t);
2621 
2622   switch (kind)
2623     {
2624     case DEBUG_STATIC:
2625     case DEBUG_LOCAL_STATIC:
2626       fprintf (info->f, "\tfile:");
2627       break;
2628     case DEBUG_REGISTER:
2629       fprintf (info->f, "\tregister:");
2630       break;
2631     default:
2632       break;
2633     }
2634 
2635   if (from_class)
2636     fprintf (info->f, "\tclass:%s", from_class);
2637 
2638   if (dname)
2639     free (dname);
2640 
2641   fprintf (info->f, "\n");
2642 
2643   free (t);
2644 
2645   return TRUE;
2646 }
2647 
2648 /* Start outputting a function.  */
2649 
2650 static bfd_boolean
tg_start_function(void * p,const char * name,bfd_boolean global)2651 tg_start_function (void *p, const char *name, bfd_boolean global)
2652 {
2653   struct pr_handle *info = (struct pr_handle *) p;
2654   char *dname;
2655 
2656   if (! global)
2657     info->stack->flavor = "static";
2658   else
2659     info->stack->flavor = NULL;
2660 
2661   dname = NULL;
2662   if (info->demangler)
2663     dname = info->demangler (info->abfd, name, DMGL_ANSI | DMGL_PARAMS);
2664 
2665   if (! substitute_type (info, dname ? dname : name))
2666     return FALSE;
2667 
2668   info->stack->method = NULL;
2669   if (dname != NULL)
2670     {
2671       char *sep;
2672       sep = strstr (dname, "::");
2673       if (sep)
2674 	{
2675 	  info->stack->method = dname;
2676 	  *sep = 0;
2677 	  name = sep + 2;
2678 	}
2679       else
2680 	{
2681 	  info->stack->method = "";
2682 	  name = dname;
2683 	}
2684       sep = strchr (name, '(');
2685       if (sep)
2686 	*sep = 0;
2687       /* Obscure functions as type_info function.  */
2688     }
2689 
2690   info->stack->parents = strdup (name);
2691 
2692   if (! info->stack->method && ! append_type (info, "("))
2693     return FALSE;
2694 
2695   info->parameter = 1;
2696 
2697   return TRUE;
2698 }
2699 
2700 /* Output a function parameter.  */
2701 
2702 static bfd_boolean
tg_function_parameter(void * p,const char * name,enum debug_parm_kind kind,bfd_vma val ATTRIBUTE_UNUSED)2703 tg_function_parameter (void *p, const char *name, enum debug_parm_kind kind,
2704 		       bfd_vma val ATTRIBUTE_UNUSED)
2705 {
2706   struct pr_handle *info = (struct pr_handle *) p;
2707   char *t;
2708 
2709   if (kind == DEBUG_PARM_REFERENCE
2710       || kind == DEBUG_PARM_REF_REG)
2711     {
2712       if (! pr_reference_type (p))
2713 	return FALSE;
2714     }
2715 
2716   if (! substitute_type (info, name))
2717     return FALSE;
2718 
2719   t = pop_type (info);
2720   if (t == NULL)
2721     return FALSE;
2722 
2723   if (! info->stack->method)
2724     {
2725       if (info->parameter != 1 && ! append_type (info, ", "))
2726 	return FALSE;
2727 
2728       if (kind == DEBUG_PARM_REG || kind == DEBUG_PARM_REF_REG)
2729 	if (! append_type (info, "register "))
2730 	  return FALSE;
2731 
2732       if (! append_type (info, t))
2733 	return FALSE;
2734     }
2735 
2736   free (t);
2737 
2738   ++info->parameter;
2739 
2740   return TRUE;
2741 }
2742 
2743 /* Start writing out a block.  */
2744 
2745 static bfd_boolean
tg_start_block(void * p,bfd_vma addr)2746 tg_start_block (void *p, bfd_vma addr)
2747 {
2748   struct pr_handle *info = (struct pr_handle *) p;
2749   char ab[20], kind, *partof;
2750   char *t;
2751   bfd_boolean local;
2752 
2753   if (info->parameter > 0)
2754     {
2755       info->parameter = 0;
2756 
2757       /* Delayed name.  */
2758       fprintf (info->f, "%s\t%s\t", info->stack->parents, info->filename);
2759       free (info->stack->parents);
2760 
2761       print_vma (addr, ab, TRUE, TRUE);
2762       translate_addresses (info->abfd, ab, info->f, info->syms);
2763       local = info->stack->flavor != NULL;
2764       if (info->stack->method && *info->stack->method)
2765 	{
2766 	  kind = 'm';
2767 	  partof = (char *) info->stack->method;
2768 	}
2769       else
2770 	{
2771 	  kind = 'f';
2772 	  partof = NULL;
2773 	  if (! info->stack->method && ! append_type (info, ")"))
2774 	    return FALSE;
2775 	}
2776       t = pop_type (info);
2777       if (t == NULL)
2778 	return FALSE;
2779       fprintf (info->f, ";\"\tkind:%c\ttype:%s", kind, t);
2780       if (local)
2781 	fputs ("\tfile:", info->f);
2782       if (partof)
2783 	{
2784 	  fprintf (info->f, "\tclass:%s", partof);
2785 	  free (partof);
2786 	}
2787       fputc ('\n', info->f);
2788     }
2789 
2790   return TRUE;
2791 }
2792 
2793 /* Write out line number information.  */
2794 
2795 static bfd_boolean
tg_lineno(void * p ATTRIBUTE_UNUSED,const char * fname ATTRIBUTE_UNUSED,unsigned long lineno ATTRIBUTE_UNUSED,bfd_vma addr ATTRIBUTE_UNUSED)2796 tg_lineno (void *p ATTRIBUTE_UNUSED, const char *fname ATTRIBUTE_UNUSED,
2797 	   unsigned long lineno ATTRIBUTE_UNUSED,
2798 	   bfd_vma addr ATTRIBUTE_UNUSED)
2799 {
2800   return TRUE;
2801 }
2802 
2803 /* Finish writing out a block.  */
2804 
2805 static bfd_boolean
tg_end_block(void * p ATTRIBUTE_UNUSED,bfd_vma addr ATTRIBUTE_UNUSED)2806 tg_end_block (void *p ATTRIBUTE_UNUSED, bfd_vma addr ATTRIBUTE_UNUSED)
2807 {
2808   return TRUE;
2809 }
2810 
2811 /* Convert the visibility value into a human readable name.  */
2812 
2813 static const char *
visibility_name(enum debug_visibility visibility)2814 visibility_name (enum debug_visibility visibility)
2815 {
2816   const char *s;
2817 
2818   switch (visibility)
2819     {
2820     case DEBUG_VISIBILITY_PUBLIC:
2821       s = "public";
2822       break;
2823     case DEBUG_VISIBILITY_PRIVATE:
2824       s = "private";
2825       break;
2826     case DEBUG_VISIBILITY_PROTECTED:
2827       s = "protected";
2828       break;
2829     case DEBUG_VISIBILITY_IGNORE:
2830       s = "/* ignore */";
2831       break;
2832     default:
2833       abort ();
2834       return FALSE;
2835     }
2836   return s;
2837 }
2838