xref: /netbsd-src/external/gpl3/binutils.old/dist/binutils/debug.c (revision e992f068c547fd6e84b3f104dc2340adcc955732)
1 /* debug.c -- Handle generic debugging information.
2    Copyright (C) 1995-2022 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4 
5    This file is part of GNU Binutils.
6 
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11 
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16 
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20    02110-1301, USA.  */
21 
22 
23 /* This file implements a generic debugging format.  We may eventually
24    have readers which convert different formats into this generic
25    format, and writers which write it out.  The initial impetus for
26    this was writing a converter from stabs to HP IEEE-695 debugging
27    format.  */
28 
29 #include "sysdep.h"
30 #include <assert.h>
31 #include "bfd.h"
32 #include "libiberty.h"
33 #include "filenames.h"
34 #include "debug.h"
35 
36 /* Global information we keep for debugging.  A pointer to this
37    structure is the debugging handle passed to all the routines.  */
38 
39 struct debug_handle
40 {
41   /* A linked list of compilation units.  */
42   struct debug_unit *units;
43   /* The current compilation unit.  */
44   struct debug_unit *current_unit;
45   /* The current source file.  */
46   struct debug_file *current_file;
47   /* The current function.  */
48   struct debug_function *current_function;
49   /* The current block.  */
50   struct debug_block *current_block;
51   /* The current line number information for the current unit.  */
52   struct debug_lineno *current_lineno;
53   /* Mark.  This is used by debug_write.  */
54   unsigned int mark;
55   /* A struct/class ID used by debug_write.  */
56   unsigned int class_id;
57   /* The base for class_id for this call to debug_write.  */
58   unsigned int base_id;
59   /* The current line number in debug_write.  */
60   struct debug_lineno *current_write_lineno;
61   unsigned int current_write_lineno_index;
62   /* A list of classes which have assigned ID's during debug_write.
63      This is linked through the next_id field of debug_class_type.  */
64   struct debug_class_id *id_list;
65   /* A list used to avoid recursion during debug_type_samep.  */
66   struct debug_type_compare_list *compare_list;
67 };
68 
69 /* Information we keep for a single compilation unit.  */
70 
71 struct debug_unit
72 {
73   /* The next compilation unit.  */
74   struct debug_unit *next;
75   /* A list of files included in this compilation unit.  The first
76      file is always the main one, and that is where the main file name
77      is stored.  */
78   struct debug_file *files;
79   /* Line number information for this compilation unit.  This is not
80      stored by function, because assembler code may have line number
81      information without function information.  */
82   struct debug_lineno *linenos;
83 };
84 
85 /* Information kept for a single source file.  */
86 
87 struct debug_file
88 {
89   /* The next source file in this compilation unit.  */
90   struct debug_file *next;
91   /* The name of the source file.  */
92   const char *filename;
93   /* Global functions, variables, types, etc.  */
94   struct debug_namespace *globals;
95 };
96 
97 /* A type.  */
98 
99 struct debug_type_s
100 {
101   /* Kind of type.  */
102   enum debug_type_kind kind;
103   /* Size of type (0 if not known).  */
104   unsigned int size;
105   /* Type which is a pointer to this type.  */
106   debug_type pointer;
107   /* Tagged union with additional information about the type.  */
108   union
109     {
110       /* DEBUG_KIND_INDIRECT.  */
111       struct debug_indirect_type *kindirect;
112       /* DEBUG_KIND_INT.  */
113       /* Whether the integer is unsigned.  */
114       bool kint;
115       /* DEBUG_KIND_STRUCT, DEBUG_KIND_UNION, DEBUG_KIND_CLASS,
116          DEBUG_KIND_UNION_CLASS.  */
117       struct debug_class_type *kclass;
118       /* DEBUG_KIND_ENUM.  */
119       struct debug_enum_type *kenum;
120       /* DEBUG_KIND_POINTER.  */
121       struct debug_type_s *kpointer;
122       /* DEBUG_KIND_FUNCTION.  */
123       struct debug_function_type *kfunction;
124       /* DEBUG_KIND_REFERENCE.  */
125       struct debug_type_s *kreference;
126       /* DEBUG_KIND_RANGE.  */
127       struct debug_range_type *krange;
128       /* DEBUG_KIND_ARRAY.  */
129       struct debug_array_type *karray;
130       /* DEBUG_KIND_SET.  */
131       struct debug_set_type *kset;
132       /* DEBUG_KIND_OFFSET.  */
133       struct debug_offset_type *koffset;
134       /* DEBUG_KIND_METHOD.  */
135       struct debug_method_type *kmethod;
136       /* DEBUG_KIND_CONST.  */
137       struct debug_type_s *kconst;
138       /* DEBUG_KIND_VOLATILE.  */
139       struct debug_type_s *kvolatile;
140       /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED.  */
141       struct debug_named_type *knamed;
142     } u;
143 };
144 
145 /* Information kept for an indirect type.  */
146 
147 struct debug_indirect_type
148 {
149   /* Slot where the final type will appear.  */
150   debug_type *slot;
151   /* Tag.  */
152   const char *tag;
153 };
154 
155 /* Information kept for a struct, union, or class.  */
156 
157 struct debug_class_type
158 {
159   /* NULL terminated array of fields.  */
160   debug_field *fields;
161   /* A mark field which indicates whether the struct has already been
162      printed.  */
163   unsigned int mark;
164   /* This is used to uniquely identify unnamed structs when printing.  */
165   unsigned int id;
166   /* The remaining fields are only used for DEBUG_KIND_CLASS and
167      DEBUG_KIND_UNION_CLASS.  */
168   /* NULL terminated array of base classes.  */
169   debug_baseclass *baseclasses;
170   /* NULL terminated array of methods.  */
171   debug_method *methods;
172   /* The type of the class providing the virtual function table for
173      this class.  This may point to the type itself.  */
174   debug_type vptrbase;
175 };
176 
177 /* Information kept for an enum.  */
178 
179 struct debug_enum_type
180 {
181   /* NULL terminated array of names.  */
182   const char **names;
183   /* Array of corresponding values.  */
184   bfd_signed_vma *values;
185 };
186 
187 /* Information kept for a function.  FIXME: We should be able to
188    record the parameter types.  */
189 
190 struct debug_function_type
191 {
192   /* Return type.  */
193   debug_type return_type;
194   /* NULL terminated array of argument types.  */
195   debug_type *arg_types;
196   /* Whether the function takes a variable number of arguments.  */
197   bool varargs;
198 };
199 
200 /* Information kept for a range.  */
201 
202 struct debug_range_type
203 {
204   /* Range base type.  */
205   debug_type type;
206   /* Lower bound.  */
207   bfd_signed_vma lower;
208   /* Upper bound.  */
209   bfd_signed_vma upper;
210 };
211 
212 /* Information kept for an array.  */
213 
214 struct debug_array_type
215 {
216   /* Element type.  */
217   debug_type element_type;
218   /* Range type.  */
219   debug_type range_type;
220   /* Lower bound.  */
221   bfd_signed_vma lower;
222   /* Upper bound.  */
223   bfd_signed_vma upper;
224   /* Whether this array is really a string.  */
225   bool stringp;
226 };
227 
228 /* Information kept for a set.  */
229 
230 struct debug_set_type
231 {
232   /* Base type.  */
233   debug_type type;
234   /* Whether this set is really a bitstring.  */
235   bool bitstringp;
236 };
237 
238 /* Information kept for an offset type (a based pointer).  */
239 
240 struct debug_offset_type
241 {
242   /* The type the pointer is an offset from.  */
243   debug_type base_type;
244   /* The type the pointer points to.  */
245   debug_type target_type;
246 };
247 
248 /* Information kept for a method type.  */
249 
250 struct debug_method_type
251 {
252   /* The return type.  */
253   debug_type return_type;
254   /* The object type which this method is for.  */
255   debug_type domain_type;
256   /* A NULL terminated array of argument types.  */
257   debug_type *arg_types;
258   /* Whether the method takes a variable number of arguments.  */
259   bool varargs;
260 };
261 
262 /* Information kept for a named type.  */
263 
264 struct debug_named_type
265 {
266   /* Name.  */
267   struct debug_name *name;
268   /* Real type.  */
269   debug_type type;
270 };
271 
272 /* A field in a struct or union.  */
273 
274 struct debug_field_s
275 {
276   /* Name of the field.  */
277   const char *name;
278   /* Type of the field.  */
279   struct debug_type_s *type;
280   /* Visibility of the field.  */
281   enum debug_visibility visibility;
282   /* Whether this is a static member.  */
283   bool static_member;
284   union
285     {
286       /* If static_member is false.  */
287       struct
288 	{
289 	  /* Bit position of the field in the struct.  */
290 	  unsigned int bitpos;
291 	  /* Size of the field in bits.  */
292 	  unsigned int bitsize;
293 	} f;
294       /* If static_member is true.  */
295       struct
296 	{
297 	  const char *physname;
298 	} s;
299     } u;
300 };
301 
302 /* A base class for an object.  */
303 
304 struct debug_baseclass_s
305 {
306   /* Type of the base class.  */
307   struct debug_type_s *type;
308   /* Bit position of the base class in the object.  */
309   unsigned int bitpos;
310   /* Whether the base class is virtual.  */
311   bool is_virtual;
312   /* Visibility of the base class.  */
313   enum debug_visibility visibility;
314 };
315 
316 /* A method of an object.  */
317 
318 struct debug_method_s
319 {
320   /* The name of the method.  */
321   const char *name;
322   /* A NULL terminated array of different types of variants.  */
323   struct debug_method_variant_s **variants;
324 };
325 
326 /* The variants of a method function of an object.  These indicate
327    which method to run.  */
328 
329 struct debug_method_variant_s
330 {
331   /* The physical name of the function.  */
332   const char *physname;
333   /* The type of the function.  */
334   struct debug_type_s *type;
335   /* The visibility of the function.  */
336   enum debug_visibility visibility;
337   /* Whether the function is const.  */
338   bool constp;
339   /* Whether the function is volatile.  */
340   bool volatilep;
341   /* The offset to the function in the virtual function table.  */
342   bfd_vma voffset;
343   /* If voffset is VOFFSET_STATIC_METHOD, this is a static method.  */
344 #define VOFFSET_STATIC_METHOD ((bfd_vma) -1)
345   /* Context of a virtual method function.  */
346   struct debug_type_s *context;
347 };
348 
349 /* A variable.  This is the information we keep for a variable object.
350    This has no name; a name is associated with a variable in a
351    debug_name structure.  */
352 
353 struct debug_variable
354 {
355   /* Kind of variable.  */
356   enum debug_var_kind kind;
357   /* Type.  */
358   debug_type type;
359   /* Value.  The interpretation of the value depends upon kind.  */
360   bfd_vma val;
361 };
362 
363 /* A function.  This has no name; a name is associated with a function
364    in a debug_name structure.  */
365 
366 struct debug_function
367 {
368   /* Return type.  */
369   debug_type return_type;
370   /* Parameter information.  */
371   struct debug_parameter *parameters;
372   /* Block information.  The first structure on the list is the main
373      block of the function, and describes function local variables.  */
374   struct debug_block *blocks;
375 };
376 
377 /* A function parameter.  */
378 
379 struct debug_parameter
380 {
381   /* Next parameter.  */
382   struct debug_parameter *next;
383   /* Name.  */
384   const char *name;
385   /* Type.  */
386   debug_type type;
387   /* Kind.  */
388   enum debug_parm_kind kind;
389   /* Value (meaning depends upon kind).  */
390   bfd_vma val;
391 };
392 
393 /* A typed constant.  */
394 
395 struct debug_typed_constant
396 {
397   /* Type.  */
398   debug_type type;
399   /* Value.  FIXME: We may eventually need to support non-integral
400      values.  */
401   bfd_vma val;
402 };
403 
404 /* Information about a block within a function.  */
405 
406 struct debug_block
407 {
408   /* Next block with the same parent.  */
409   struct debug_block *next;
410   /* Parent block.  */
411   struct debug_block *parent;
412   /* List of child blocks.  */
413   struct debug_block *children;
414   /* Start address of the block.  */
415   bfd_vma start;
416   /* End address of the block.  */
417   bfd_vma end;
418   /* Local variables.  */
419   struct debug_namespace *locals;
420 };
421 
422 /* Line number information we keep for a compilation unit.  FIXME:
423    This structure is easy to create, but can be very space
424    inefficient.  */
425 
426 struct debug_lineno
427 {
428   /* More line number information for this block.  */
429   struct debug_lineno *next;
430   /* Source file.  */
431   struct debug_file *file;
432   /* Line numbers, terminated by a -1 or the end of the array.  */
433 #define DEBUG_LINENO_COUNT 10
434   unsigned long linenos[DEBUG_LINENO_COUNT];
435   /* Addresses for the line numbers.  */
436   bfd_vma addrs[DEBUG_LINENO_COUNT];
437 };
438 
439 /* A namespace.  This is a mapping from names to objects.  FIXME: This
440    should be implemented as a hash table.  */
441 
442 struct debug_namespace
443 {
444   /* List of items in this namespace.  */
445   struct debug_name *list;
446   /* Pointer to where the next item in this namespace should go.  */
447   struct debug_name **tail;
448 };
449 
450 /* Kinds of objects that appear in a namespace.  */
451 
452 enum debug_object_kind
453 {
454   /* A type.  */
455   DEBUG_OBJECT_TYPE,
456   /* A tagged type (really a different sort of namespace).  */
457   DEBUG_OBJECT_TAG,
458   /* A variable.  */
459   DEBUG_OBJECT_VARIABLE,
460   /* A function.  */
461   DEBUG_OBJECT_FUNCTION,
462   /* An integer constant.  */
463   DEBUG_OBJECT_INT_CONSTANT,
464   /* A floating point constant.  */
465   DEBUG_OBJECT_FLOAT_CONSTANT,
466   /* A typed constant.  */
467   DEBUG_OBJECT_TYPED_CONSTANT
468 };
469 
470 /* Linkage of an object that appears in a namespace.  */
471 
472 enum debug_object_linkage
473 {
474   /* Local variable.  */
475   DEBUG_LINKAGE_AUTOMATIC,
476   /* Static--either file static or function static, depending upon the
477      namespace is.  */
478   DEBUG_LINKAGE_STATIC,
479   /* Global.  */
480   DEBUG_LINKAGE_GLOBAL,
481   /* No linkage.  */
482   DEBUG_LINKAGE_NONE
483 };
484 
485 /* A name in a namespace.  */
486 
487 struct debug_name
488 {
489   /* Next name in this namespace.  */
490   struct debug_name *next;
491   /* Name.  */
492   const char *name;
493   /* Mark.  This is used by debug_write.  */
494   unsigned int mark;
495   /* Kind of object.  */
496   enum debug_object_kind kind;
497   /* Linkage of object.  */
498   enum debug_object_linkage linkage;
499   /* Tagged union with additional information about the object.  */
500   union
501     {
502       /* DEBUG_OBJECT_TYPE.  */
503       struct debug_type_s *type;
504       /* DEBUG_OBJECT_TAG.  */
505       struct debug_type_s *tag;
506       /* DEBUG_OBJECT_VARIABLE.  */
507       struct debug_variable *variable;
508       /* DEBUG_OBJECT_FUNCTION.  */
509       struct debug_function *function;
510       /* DEBUG_OBJECT_INT_CONSTANT.  */
511       bfd_vma int_constant;
512       /* DEBUG_OBJECT_FLOAT_CONSTANT.  */
513       double float_constant;
514       /* DEBUG_OBJECT_TYPED_CONSTANT.  */
515       struct debug_typed_constant *typed_constant;
516     } u;
517 };
518 
519 /* During debug_write, a linked list of these structures is used to
520    keep track of ID numbers that have been assigned to classes.  */
521 
522 struct debug_class_id
523 {
524   /* Next ID number.  */
525   struct debug_class_id *next;
526   /* The type with the ID.  */
527   struct debug_type_s *type;
528   /* The tag; NULL if no tag.  */
529   const char *tag;
530 };
531 
532 /* During debug_type_samep, a linked list of these structures is kept
533    on the stack to avoid infinite recursion.  */
534 
535 struct debug_type_compare_list
536 {
537   /* Next type on list.  */
538   struct debug_type_compare_list *next;
539   /* The types we are comparing.  */
540   struct debug_type_s *t1;
541   struct debug_type_s *t2;
542 };
543 
544 /* During debug_get_real_type, a linked list of these structures is
545    kept on the stack to avoid infinite recursion.  */
546 
547 struct debug_type_real_list
548 {
549   /* Next type on list.  */
550   struct debug_type_real_list *next;
551   /* The type we are checking.  */
552   struct debug_type_s *t;
553 };
554 
555 /* Local functions.  */
556 
557 static void debug_error (const char *);
558 static struct debug_name *debug_add_to_namespace
559   (struct debug_handle *, struct debug_namespace **, const char *,
560    enum debug_object_kind, enum debug_object_linkage);
561 static struct debug_name *debug_add_to_current_namespace
562   (struct debug_handle *, const char *, enum debug_object_kind,
563    enum debug_object_linkage);
564 static struct debug_type_s *debug_make_type
565   (struct debug_handle *, enum debug_type_kind, unsigned int);
566 static struct debug_type_s *debug_get_real_type
567   (void *, debug_type, struct debug_type_real_list *);
568 static bool debug_write_name
569   (struct debug_handle *, const struct debug_write_fns *, void *,
570    struct debug_name *);
571 static bool debug_write_type
572   (struct debug_handle *, const struct debug_write_fns *, void *,
573    struct debug_type_s *, struct debug_name *);
574 static bool debug_write_class_type
575   (struct debug_handle *, const struct debug_write_fns *, void *,
576    struct debug_type_s *, const char *);
577 static bool debug_write_function
578   (struct debug_handle *, const struct debug_write_fns *, void *,
579    const char *, enum debug_object_linkage, struct debug_function *);
580 static bool debug_write_block
581   (struct debug_handle *, const struct debug_write_fns *, void *,
582    struct debug_block *);
583 static bool debug_write_linenos
584   (struct debug_handle *, const struct debug_write_fns *, void *, bfd_vma);
585 static bool debug_set_class_id
586   (struct debug_handle *, const char *, struct debug_type_s *);
587 static bool debug_type_samep
588   (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
589 static bool debug_class_type_samep
590   (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
591 
592 /* Issue an error message.  */
593 
594 static void
debug_error(const char * message)595 debug_error (const char *message)
596 {
597   fprintf (stderr, "%s\n", message);
598 }
599 
600 /* Add an object to a namespace.  */
601 
602 static struct debug_name *
debug_add_to_namespace(struct debug_handle * info ATTRIBUTE_UNUSED,struct debug_namespace ** nsp,const char * name,enum debug_object_kind kind,enum debug_object_linkage linkage)603 debug_add_to_namespace (struct debug_handle *info ATTRIBUTE_UNUSED,
604 			struct debug_namespace **nsp, const char *name,
605 			enum debug_object_kind kind,
606 			enum debug_object_linkage linkage)
607 {
608   struct debug_name *n;
609   struct debug_namespace *ns;
610 
611   n = (struct debug_name *) xmalloc (sizeof *n);
612   memset (n, 0, sizeof *n);
613 
614   n->name = name;
615   n->kind = kind;
616   n->linkage = linkage;
617 
618   ns = *nsp;
619   if (ns == NULL)
620     {
621       ns = (struct debug_namespace *) xmalloc (sizeof *ns);
622       memset (ns, 0, sizeof *ns);
623 
624       ns->tail = &ns->list;
625 
626       *nsp = ns;
627     }
628 
629   *ns->tail = n;
630   ns->tail = &n->next;
631 
632   return n;
633 }
634 
635 /* Add an object to the current namespace.  */
636 
637 static struct debug_name *
debug_add_to_current_namespace(struct debug_handle * info,const char * name,enum debug_object_kind kind,enum debug_object_linkage linkage)638 debug_add_to_current_namespace (struct debug_handle *info, const char *name,
639 				enum debug_object_kind kind,
640 				enum debug_object_linkage linkage)
641 {
642   struct debug_namespace **nsp;
643 
644   if (info->current_unit == NULL
645       || info->current_file == NULL)
646     {
647       debug_error (_("debug_add_to_current_namespace: no current file"));
648       return NULL;
649     }
650 
651   if (info->current_block != NULL)
652     nsp = &info->current_block->locals;
653   else
654     nsp = &info->current_file->globals;
655 
656   return debug_add_to_namespace (info, nsp, name, kind, linkage);
657 }
658 
659 /* Return a handle for debugging information.  */
660 
661 void *
debug_init(void)662 debug_init (void)
663 {
664   struct debug_handle *ret;
665 
666   ret = (struct debug_handle *) xmalloc (sizeof *ret);
667   memset (ret, 0, sizeof *ret);
668   return (void *) ret;
669 }
670 
671 /* Set the source filename.  This implicitly starts a new compilation
672    unit.  */
673 
674 bool
debug_set_filename(void * handle,const char * name)675 debug_set_filename (void *handle, const char *name)
676 {
677   struct debug_handle *info = (struct debug_handle *) handle;
678   struct debug_file *nfile;
679   struct debug_unit *nunit;
680 
681   if (name == NULL)
682     name = "";
683 
684   nfile = (struct debug_file *) xmalloc (sizeof *nfile);
685   memset (nfile, 0, sizeof *nfile);
686 
687   nfile->filename = name;
688 
689   nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
690   memset (nunit, 0, sizeof *nunit);
691 
692   nunit->files = nfile;
693   info->current_file = nfile;
694 
695   if (info->current_unit != NULL)
696     info->current_unit->next = nunit;
697   else
698     {
699       assert (info->units == NULL);
700       info->units = nunit;
701     }
702 
703   info->current_unit = nunit;
704 
705   info->current_function = NULL;
706   info->current_block = NULL;
707   info->current_lineno = NULL;
708 
709   return true;
710 }
711 
712 /* Change source files to the given file name.  This is used for
713    include files in a single compilation unit.  */
714 
715 bool
debug_start_source(void * handle,const char * name)716 debug_start_source (void *handle, const char *name)
717 {
718   struct debug_handle *info = (struct debug_handle *) handle;
719   struct debug_file *f, **pf;
720 
721   if (name == NULL)
722     name = "";
723 
724   if (info->current_unit == NULL)
725     {
726       debug_error (_("debug_start_source: no debug_set_filename call"));
727       return false;
728     }
729 
730   for (f = info->current_unit->files; f != NULL; f = f->next)
731     {
732       if (filename_cmp (f->filename, name) == 0)
733 	{
734 	  info->current_file = f;
735 	  return true;
736 	}
737     }
738 
739   f = (struct debug_file *) xmalloc (sizeof *f);
740   memset (f, 0, sizeof *f);
741 
742   f->filename = name;
743 
744   for (pf = &info->current_file->next;
745        *pf != NULL;
746        pf = &(*pf)->next)
747     ;
748   *pf = f;
749 
750   info->current_file = f;
751 
752   return true;
753 }
754 
755 /* Record a function definition.  This implicitly starts a function
756    block.  The debug_type argument is the type of the return value.
757    The boolean indicates whether the function is globally visible.
758    The bfd_vma is the address of the start of the function.  Currently
759    the parameter types are specified by calls to
760    debug_record_parameter.  FIXME: There is no way to specify nested
761    functions.  */
762 
763 bool
debug_record_function(void * handle,const char * name,debug_type return_type,bool global,bfd_vma addr)764 debug_record_function (void *handle, const char *name,
765 		       debug_type return_type, bool global,
766 		       bfd_vma addr)
767 {
768   struct debug_handle *info = (struct debug_handle *) handle;
769   struct debug_function *f;
770   struct debug_block *b;
771   struct debug_name *n;
772 
773   if (name == NULL)
774     name = "";
775   if (return_type == NULL)
776     return false;
777 
778   if (info->current_unit == NULL)
779     {
780       debug_error (_("debug_record_function: no debug_set_filename call"));
781       return false;
782     }
783 
784   f = (struct debug_function *) xmalloc (sizeof *f);
785   memset (f, 0, sizeof *f);
786 
787   f->return_type = return_type;
788 
789   b = (struct debug_block *) xmalloc (sizeof *b);
790   memset (b, 0, sizeof *b);
791 
792   b->start = addr;
793   b->end = (bfd_vma) -1;
794 
795   f->blocks = b;
796 
797   info->current_function = f;
798   info->current_block = b;
799 
800   /* FIXME: If we could handle nested functions, this would be the
801      place: we would want to use a different namespace.  */
802   n = debug_add_to_namespace (info,
803 			      &info->current_file->globals,
804 			      name,
805 			      DEBUG_OBJECT_FUNCTION,
806 			      (global
807 			       ? DEBUG_LINKAGE_GLOBAL
808 			       : DEBUG_LINKAGE_STATIC));
809   if (n == NULL)
810     return false;
811 
812   n->u.function = f;
813 
814   return true;
815 }
816 
817 /* Record a parameter for the current function.  */
818 
819 bool
debug_record_parameter(void * handle,const char * name,debug_type type,enum debug_parm_kind kind,bfd_vma val)820 debug_record_parameter (void *handle, const char *name, debug_type type,
821 			enum debug_parm_kind kind, bfd_vma val)
822 {
823   struct debug_handle *info = (struct debug_handle *) handle;
824   struct debug_parameter *p, **pp;
825 
826   if (name == NULL || type == NULL)
827     return false;
828 
829   if (info->current_unit == NULL
830       || info->current_function == NULL)
831     {
832       debug_error (_("debug_record_parameter: no current function"));
833       return false;
834     }
835 
836   p = (struct debug_parameter *) xmalloc (sizeof *p);
837   memset (p, 0, sizeof *p);
838 
839   p->name = name;
840   p->type = type;
841   p->kind = kind;
842   p->val = val;
843 
844   for (pp = &info->current_function->parameters;
845        *pp != NULL;
846        pp = &(*pp)->next)
847     ;
848   *pp = p;
849 
850   return true;
851 }
852 
853 /* End a function.  FIXME: This should handle function nesting.  */
854 
855 bool
debug_end_function(void * handle,bfd_vma addr)856 debug_end_function (void *handle, bfd_vma addr)
857 {
858   struct debug_handle *info = (struct debug_handle *) handle;
859 
860   if (info->current_unit == NULL
861       || info->current_block == NULL
862       || info->current_function == NULL)
863     {
864       debug_error (_("debug_end_function: no current function"));
865       return false;
866     }
867 
868   if (info->current_block->parent != NULL)
869     {
870       debug_error (_("debug_end_function: some blocks were not closed"));
871       return false;
872     }
873 
874   info->current_block->end = addr;
875 
876   info->current_function = NULL;
877   info->current_block = NULL;
878 
879   return true;
880 }
881 
882 /* Start a block in a function.  All local information will be
883    recorded in this block, until the matching call to debug_end_block.
884    debug_start_block and debug_end_block may be nested.  The bfd_vma
885    argument is the address at which this block starts.  */
886 
887 bool
debug_start_block(void * handle,bfd_vma addr)888 debug_start_block (void *handle, bfd_vma addr)
889 {
890   struct debug_handle *info = (struct debug_handle *) handle;
891   struct debug_block *b, **pb;
892 
893   /* We must always have a current block: debug_record_function sets
894      one up.  */
895   if (info->current_unit == NULL
896       || info->current_block == NULL)
897     {
898       debug_error (_("debug_start_block: no current block"));
899       return false;
900     }
901 
902   b = (struct debug_block *) xmalloc (sizeof *b);
903   memset (b, 0, sizeof *b);
904 
905   b->parent = info->current_block;
906   b->start = addr;
907   b->end = (bfd_vma) -1;
908 
909   /* This new block is a child of the current block.  */
910   for (pb = &info->current_block->children;
911        *pb != NULL;
912        pb = &(*pb)->next)
913     ;
914   *pb = b;
915 
916   info->current_block = b;
917 
918   return true;
919 }
920 
921 /* Finish a block in a function.  This matches the call to
922    debug_start_block.  The argument is the address at which this block
923    ends.  */
924 
925 bool
debug_end_block(void * handle,bfd_vma addr)926 debug_end_block (void *handle, bfd_vma addr)
927 {
928   struct debug_handle *info = (struct debug_handle *) handle;
929   struct debug_block *parent;
930 
931   if (info->current_unit == NULL
932       || info->current_block == NULL)
933     {
934       debug_error (_("debug_end_block: no current block"));
935       return false;
936     }
937 
938   parent = info->current_block->parent;
939   if (parent == NULL)
940     {
941       debug_error (_("debug_end_block: attempt to close top level block"));
942       return false;
943     }
944 
945   info->current_block->end = addr;
946 
947   info->current_block = parent;
948 
949   return true;
950 }
951 
952 /* Associate a line number in the current source file and function
953    with a given address.  */
954 
955 bool
debug_record_line(void * handle,unsigned long lineno,bfd_vma addr)956 debug_record_line (void *handle, unsigned long lineno, bfd_vma addr)
957 {
958   struct debug_handle *info = (struct debug_handle *) handle;
959   struct debug_lineno *l;
960   unsigned int i;
961 
962   if (info->current_unit == NULL)
963     {
964       debug_error (_("debug_record_line: no current unit"));
965       return false;
966     }
967 
968   l = info->current_lineno;
969   if (l != NULL && l->file == info->current_file)
970     {
971       for (i = 0; i < DEBUG_LINENO_COUNT; i++)
972 	{
973 	  if (l->linenos[i] == (unsigned long) -1)
974 	    {
975 	      l->linenos[i] = lineno;
976 	      l->addrs[i] = addr;
977 	      return true;
978 	    }
979 	}
980     }
981 
982   /* If we get here, then either 1) there is no current_lineno
983      structure, which means this is the first line number in this
984      compilation unit, 2) the current_lineno structure is for a
985      different file, or 3) the current_lineno structure is full.
986      Regardless, we want to allocate a new debug_lineno structure, put
987      it in the right place, and make it the new current_lineno
988      structure.  */
989 
990   l = (struct debug_lineno *) xmalloc (sizeof *l);
991   memset (l, 0, sizeof *l);
992 
993   l->file = info->current_file;
994   l->linenos[0] = lineno;
995   l->addrs[0] = addr;
996   for (i = 1; i < DEBUG_LINENO_COUNT; i++)
997     l->linenos[i] = (unsigned long) -1;
998 
999   if (info->current_lineno != NULL)
1000     info->current_lineno->next = l;
1001   else
1002     info->current_unit->linenos = l;
1003 
1004   info->current_lineno = l;
1005 
1006   return true;
1007 }
1008 
1009 /* Start a named common block.  This is a block of variables that may
1010    move in memory.  */
1011 
1012 bool
debug_start_common_block(void * handle ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED)1013 debug_start_common_block (void *handle ATTRIBUTE_UNUSED,
1014 			  const char *name ATTRIBUTE_UNUSED)
1015 {
1016   /* FIXME */
1017   debug_error (_("debug_start_common_block: not implemented"));
1018   return false;
1019 }
1020 
1021 /* End a named common block.  */
1022 
1023 bool
debug_end_common_block(void * handle ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED)1024 debug_end_common_block (void *handle ATTRIBUTE_UNUSED,
1025 			const char *name ATTRIBUTE_UNUSED)
1026 {
1027   /* FIXME */
1028   debug_error (_("debug_end_common_block: not implemented"));
1029   return false;
1030 }
1031 
1032 /* Record a named integer constant.  */
1033 
1034 bool
debug_record_int_const(void * handle,const char * name,bfd_vma val)1035 debug_record_int_const (void *handle, const char *name, bfd_vma val)
1036 {
1037   struct debug_handle *info = (struct debug_handle *) handle;
1038   struct debug_name *n;
1039 
1040   if (name == NULL)
1041     return false;
1042 
1043   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_INT_CONSTANT,
1044 				      DEBUG_LINKAGE_NONE);
1045   if (n == NULL)
1046     return false;
1047 
1048   n->u.int_constant = val;
1049 
1050   return true;
1051 }
1052 
1053 /* Record a named floating point constant.  */
1054 
1055 bool
debug_record_float_const(void * handle,const char * name,double val)1056 debug_record_float_const (void *handle, const char *name, double val)
1057 {
1058   struct debug_handle *info = (struct debug_handle *) handle;
1059   struct debug_name *n;
1060 
1061   if (name == NULL)
1062     return false;
1063 
1064   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_FLOAT_CONSTANT,
1065 				      DEBUG_LINKAGE_NONE);
1066   if (n == NULL)
1067     return false;
1068 
1069   n->u.float_constant = val;
1070 
1071   return true;
1072 }
1073 
1074 /* Record a typed constant with an integral value.  */
1075 
1076 bool
debug_record_typed_const(void * handle,const char * name,debug_type type,bfd_vma val)1077 debug_record_typed_const (void *handle, const char *name, debug_type type,
1078 			  bfd_vma val)
1079 {
1080   struct debug_handle *info = (struct debug_handle *) handle;
1081   struct debug_name *n;
1082   struct debug_typed_constant *tc;
1083 
1084   if (name == NULL || type == NULL)
1085     return false;
1086 
1087   n = debug_add_to_current_namespace (info, name, DEBUG_OBJECT_TYPED_CONSTANT,
1088 				      DEBUG_LINKAGE_NONE);
1089   if (n == NULL)
1090     return false;
1091 
1092   tc = (struct debug_typed_constant *) xmalloc (sizeof *tc);
1093   memset (tc, 0, sizeof *tc);
1094 
1095   tc->type = type;
1096   tc->val = val;
1097 
1098   n->u.typed_constant = tc;
1099 
1100   return true;
1101 }
1102 
1103 /* Record a label.  */
1104 
1105 bool
debug_record_label(void * handle ATTRIBUTE_UNUSED,const char * name ATTRIBUTE_UNUSED,debug_type type ATTRIBUTE_UNUSED,bfd_vma addr ATTRIBUTE_UNUSED)1106 debug_record_label (void *handle ATTRIBUTE_UNUSED,
1107 		    const char *name ATTRIBUTE_UNUSED,
1108 		    debug_type type ATTRIBUTE_UNUSED,
1109 		    bfd_vma addr ATTRIBUTE_UNUSED)
1110 {
1111   /* FIXME.  */
1112   debug_error (_("debug_record_label: not implemented"));
1113   return false;
1114 }
1115 
1116 /* Record a variable.  */
1117 
1118 bool
debug_record_variable(void * handle,const char * name,debug_type type,enum debug_var_kind kind,bfd_vma val)1119 debug_record_variable (void *handle, const char *name, debug_type type,
1120 		       enum debug_var_kind kind, bfd_vma val)
1121 {
1122   struct debug_handle *info = (struct debug_handle *) handle;
1123   struct debug_namespace **nsp;
1124   enum debug_object_linkage linkage;
1125   struct debug_name *n;
1126   struct debug_variable *v;
1127 
1128   if (name == NULL || type == NULL)
1129     return false;
1130 
1131   if (info->current_unit == NULL
1132       || info->current_file == NULL)
1133     {
1134       debug_error (_("debug_record_variable: no current file"));
1135       return false;
1136     }
1137 
1138   if (kind == DEBUG_GLOBAL || kind == DEBUG_STATIC)
1139     {
1140       nsp = &info->current_file->globals;
1141       if (kind == DEBUG_GLOBAL)
1142 	linkage = DEBUG_LINKAGE_GLOBAL;
1143       else
1144 	linkage = DEBUG_LINKAGE_STATIC;
1145     }
1146   else
1147     {
1148       if (info->current_block == NULL)
1149 	nsp = &info->current_file->globals;
1150       else
1151 	nsp = &info->current_block->locals;
1152       linkage = DEBUG_LINKAGE_AUTOMATIC;
1153     }
1154 
1155   n = debug_add_to_namespace (info, nsp, name, DEBUG_OBJECT_VARIABLE, linkage);
1156   if (n == NULL)
1157     return false;
1158 
1159   v = (struct debug_variable *) xmalloc (sizeof *v);
1160   memset (v, 0, sizeof *v);
1161 
1162   v->kind = kind;
1163   v->type = type;
1164   v->val = val;
1165 
1166   n->u.variable = v;
1167 
1168   return true;
1169 }
1170 
1171 /* Make a type with a given kind and size.  */
1172 
1173 static struct debug_type_s *
debug_make_type(struct debug_handle * info ATTRIBUTE_UNUSED,enum debug_type_kind kind,unsigned int size)1174 debug_make_type (struct debug_handle *info ATTRIBUTE_UNUSED,
1175 		 enum debug_type_kind kind, unsigned int size)
1176 {
1177   struct debug_type_s *t;
1178 
1179   t = (struct debug_type_s *) xmalloc (sizeof *t);
1180   memset (t, 0, sizeof *t);
1181 
1182   t->kind = kind;
1183   t->size = size;
1184 
1185   return t;
1186 }
1187 
1188 /* Make an indirect type which may be used as a placeholder for a type
1189    which is referenced before it is defined.  */
1190 
1191 debug_type
debug_make_indirect_type(void * handle,debug_type * slot,const char * tag)1192 debug_make_indirect_type (void *handle, debug_type *slot, const char *tag)
1193 {
1194   struct debug_handle *info = (struct debug_handle *) handle;
1195   struct debug_type_s *t;
1196   struct debug_indirect_type *i;
1197 
1198   t = debug_make_type (info, DEBUG_KIND_INDIRECT, 0);
1199   if (t == NULL)
1200     return DEBUG_TYPE_NULL;
1201 
1202   i = (struct debug_indirect_type *) xmalloc (sizeof *i);
1203   memset (i, 0, sizeof *i);
1204 
1205   i->slot = slot;
1206   i->tag = tag;
1207 
1208   t->u.kindirect = i;
1209 
1210   return t;
1211 }
1212 
1213 /* Make a void type.  There is only one of these.  */
1214 
1215 debug_type
debug_make_void_type(void * handle)1216 debug_make_void_type (void *handle)
1217 {
1218   struct debug_handle *info = (struct debug_handle *) handle;
1219 
1220   return debug_make_type (info, DEBUG_KIND_VOID, 0);
1221 }
1222 
1223 /* Make an integer type of a given size.  The boolean argument is true
1224    if the integer is unsigned.  */
1225 
1226 debug_type
debug_make_int_type(void * handle,unsigned int size,bool unsignedp)1227 debug_make_int_type (void *handle, unsigned int size, bool unsignedp)
1228 {
1229   struct debug_handle *info = (struct debug_handle *) handle;
1230   struct debug_type_s *t;
1231 
1232   t = debug_make_type (info, DEBUG_KIND_INT, size);
1233   if (t == NULL)
1234     return DEBUG_TYPE_NULL;
1235 
1236   t->u.kint = unsignedp;
1237 
1238   return t;
1239 }
1240 
1241 /* Make a floating point type of a given size.  FIXME: On some
1242    platforms, like an Alpha, you probably need to be able to specify
1243    the format.  */
1244 
1245 debug_type
debug_make_float_type(void * handle,unsigned int size)1246 debug_make_float_type (void *handle, unsigned int size)
1247 {
1248   struct debug_handle *info = (struct debug_handle *) handle;
1249 
1250   return debug_make_type (info, DEBUG_KIND_FLOAT, size);
1251 }
1252 
1253 /* Make a boolean type of a given size.  */
1254 
1255 debug_type
debug_make_bool_type(void * handle,unsigned int size)1256 debug_make_bool_type (void *handle, unsigned int size)
1257 {
1258   struct debug_handle *info = (struct debug_handle *) handle;
1259 
1260   return debug_make_type (info, DEBUG_KIND_BOOL, size);
1261 }
1262 
1263 /* Make a complex type of a given size.  */
1264 
1265 debug_type
debug_make_complex_type(void * handle,unsigned int size)1266 debug_make_complex_type (void *handle, unsigned int size)
1267 {
1268   struct debug_handle *info = (struct debug_handle *) handle;
1269 
1270   return debug_make_type (info, DEBUG_KIND_COMPLEX, size);
1271 }
1272 
1273 /* Make a structure type.  The second argument is true for a struct,
1274    false for a union.  The third argument is the size of the struct.
1275    The fourth argument is a NULL terminated array of fields.  */
1276 
1277 debug_type
debug_make_struct_type(void * handle,bool structp,bfd_vma size,debug_field * fields)1278 debug_make_struct_type (void *handle, bool structp, bfd_vma size,
1279 			debug_field *fields)
1280 {
1281   struct debug_handle *info = (struct debug_handle *) handle;
1282   struct debug_type_s *t;
1283   struct debug_class_type *c;
1284 
1285   t = debug_make_type (info,
1286 		       structp ? DEBUG_KIND_STRUCT : DEBUG_KIND_UNION,
1287 		       size);
1288   if (t == NULL)
1289     return DEBUG_TYPE_NULL;
1290 
1291   c = (struct debug_class_type *) xmalloc (sizeof *c);
1292   memset (c, 0, sizeof *c);
1293 
1294   c->fields = fields;
1295 
1296   t->u.kclass = c;
1297 
1298   return t;
1299 }
1300 
1301 /* Make an object type.  The first three arguments after the handle
1302    are the same as for debug_make_struct_type.  The next arguments are
1303    a NULL terminated array of base classes, a NULL terminated array of
1304    methods, the type of the object holding the virtual function table
1305    if it is not this object, and a boolean which is true if this
1306    object has its own virtual function table.  */
1307 
1308 debug_type
debug_make_object_type(void * handle,bool structp,bfd_vma size,debug_field * fields,debug_baseclass * baseclasses,debug_method * methods,debug_type vptrbase,bool ownvptr)1309 debug_make_object_type (void *handle, bool structp, bfd_vma size,
1310 			debug_field *fields, debug_baseclass *baseclasses,
1311 			debug_method *methods, debug_type vptrbase,
1312 			bool ownvptr)
1313 {
1314   struct debug_handle *info = (struct debug_handle *) handle;
1315   struct debug_type_s *t;
1316   struct debug_class_type *c;
1317 
1318   t = debug_make_type (info,
1319 		       structp ? DEBUG_KIND_CLASS : DEBUG_KIND_UNION_CLASS,
1320 		       size);
1321   if (t == NULL)
1322     return DEBUG_TYPE_NULL;
1323 
1324   c = (struct debug_class_type *) xmalloc (sizeof *c);
1325   memset (c, 0, sizeof *c);
1326 
1327   c->fields = fields;
1328   c->baseclasses = baseclasses;
1329   c->methods = methods;
1330   if (ownvptr)
1331     c->vptrbase = t;
1332   else
1333     c->vptrbase = vptrbase;
1334 
1335   t->u.kclass = c;
1336 
1337   return t;
1338 }
1339 
1340 /* Make an enumeration type.  The arguments are a null terminated
1341    array of strings, and an array of corresponding values.  */
1342 
1343 debug_type
debug_make_enum_type(void * handle,const char ** names,bfd_signed_vma * values)1344 debug_make_enum_type (void *handle, const char **names,
1345 		      bfd_signed_vma *values)
1346 {
1347   struct debug_handle *info = (struct debug_handle *) handle;
1348   struct debug_type_s *t;
1349   struct debug_enum_type *e;
1350 
1351   t = debug_make_type (info, DEBUG_KIND_ENUM, 0);
1352   if (t == NULL)
1353     return DEBUG_TYPE_NULL;
1354 
1355   e = (struct debug_enum_type *) xmalloc (sizeof *e);
1356   memset (e, 0, sizeof *e);
1357 
1358   e->names = names;
1359   e->values = values;
1360 
1361   t->u.kenum = e;
1362 
1363   return t;
1364 }
1365 
1366 /* Make a pointer to a given type.  */
1367 
1368 debug_type
debug_make_pointer_type(void * handle,debug_type type)1369 debug_make_pointer_type (void *handle, debug_type type)
1370 {
1371   struct debug_handle *info = (struct debug_handle *) handle;
1372   struct debug_type_s *t;
1373 
1374   if (type == NULL)
1375     return DEBUG_TYPE_NULL;
1376 
1377   if (type->pointer != DEBUG_TYPE_NULL)
1378     return type->pointer;
1379 
1380   t = debug_make_type (info, DEBUG_KIND_POINTER, 0);
1381   if (t == NULL)
1382     return DEBUG_TYPE_NULL;
1383 
1384   t->u.kpointer = type;
1385 
1386   type->pointer = t;
1387 
1388   return t;
1389 }
1390 
1391 /* Make a function returning a given type.  FIXME: We should be able
1392    to record the parameter types.  */
1393 
1394 debug_type
debug_make_function_type(void * handle,debug_type type,debug_type * arg_types,bool varargs)1395 debug_make_function_type (void *handle, debug_type type,
1396 			  debug_type *arg_types, bool varargs)
1397 {
1398   struct debug_handle *info = (struct debug_handle *) handle;
1399   struct debug_type_s *t;
1400   struct debug_function_type *f;
1401 
1402   if (type == NULL)
1403     return DEBUG_TYPE_NULL;
1404 
1405   t = debug_make_type (info, DEBUG_KIND_FUNCTION, 0);
1406   if (t == NULL)
1407     return DEBUG_TYPE_NULL;
1408 
1409   f = (struct debug_function_type *) xmalloc (sizeof *f);
1410   memset (f, 0, sizeof *f);
1411 
1412   f->return_type = type;
1413   f->arg_types = arg_types;
1414   f->varargs = varargs;
1415 
1416   t->u.kfunction = f;
1417 
1418   return t;
1419 }
1420 
1421 /* Make a reference to a given type.  */
1422 
1423 debug_type
debug_make_reference_type(void * handle,debug_type type)1424 debug_make_reference_type (void *handle, debug_type type)
1425 {
1426   struct debug_handle *info = (struct debug_handle *) handle;
1427   struct debug_type_s *t;
1428 
1429   if (type == NULL)
1430     return DEBUG_TYPE_NULL;
1431 
1432   t = debug_make_type (info, DEBUG_KIND_REFERENCE, 0);
1433   if (t == NULL)
1434     return DEBUG_TYPE_NULL;
1435 
1436   t->u.kreference = type;
1437 
1438   return t;
1439 }
1440 
1441 /* Make a range of a given type from a lower to an upper bound.  */
1442 
1443 debug_type
debug_make_range_type(void * handle,debug_type type,bfd_signed_vma lower,bfd_signed_vma upper)1444 debug_make_range_type (void *handle, debug_type type, bfd_signed_vma lower,
1445 		       bfd_signed_vma upper)
1446 {
1447   struct debug_handle *info = (struct debug_handle *) handle;
1448   struct debug_type_s *t;
1449   struct debug_range_type *r;
1450 
1451   if (type == NULL)
1452     return DEBUG_TYPE_NULL;
1453 
1454   t = debug_make_type (info, DEBUG_KIND_RANGE, 0);
1455   if (t == NULL)
1456     return DEBUG_TYPE_NULL;
1457 
1458   r = (struct debug_range_type *) xmalloc (sizeof *r);
1459   memset (r, 0, sizeof *r);
1460 
1461   r->type = type;
1462   r->lower = lower;
1463   r->upper = upper;
1464 
1465   t->u.krange = r;
1466 
1467   return t;
1468 }
1469 
1470 /* Make an array type.  The second argument is the type of an element
1471    of the array.  The third argument is the type of a range of the
1472    array.  The fourth and fifth argument are the lower and upper
1473    bounds, respectively.  The sixth argument is true if this array is
1474    actually a string, as in C.  */
1475 
1476 debug_type
debug_make_array_type(void * handle,debug_type element_type,debug_type range_type,bfd_signed_vma lower,bfd_signed_vma upper,bool stringp)1477 debug_make_array_type (void *handle, debug_type element_type,
1478 		       debug_type range_type, bfd_signed_vma lower,
1479 		       bfd_signed_vma upper, bool stringp)
1480 {
1481   struct debug_handle *info = (struct debug_handle *) handle;
1482   struct debug_type_s *t;
1483   struct debug_array_type *a;
1484 
1485   if (element_type == NULL || range_type == NULL)
1486     return DEBUG_TYPE_NULL;
1487 
1488   t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
1489   if (t == NULL)
1490     return DEBUG_TYPE_NULL;
1491 
1492   a = (struct debug_array_type *) xmalloc (sizeof *a);
1493   memset (a, 0, sizeof *a);
1494 
1495   a->element_type = element_type;
1496   a->range_type = range_type;
1497   a->lower = lower;
1498   a->upper = upper;
1499   a->stringp = stringp;
1500 
1501   t->u.karray = a;
1502 
1503   return t;
1504 }
1505 
1506 /* Make a set of a given type.  For example, a Pascal set type.  The
1507    boolean argument is true if this set is actually a bitstring, as in
1508    CHILL.  */
1509 
1510 debug_type
debug_make_set_type(void * handle,debug_type type,bool bitstringp)1511 debug_make_set_type (void *handle, debug_type type, bool bitstringp)
1512 {
1513   struct debug_handle *info = (struct debug_handle *) handle;
1514   struct debug_type_s *t;
1515   struct debug_set_type *s;
1516 
1517   if (type == NULL)
1518     return DEBUG_TYPE_NULL;
1519 
1520   t = debug_make_type (info, DEBUG_KIND_SET, 0);
1521   if (t == NULL)
1522     return DEBUG_TYPE_NULL;
1523 
1524   s = (struct debug_set_type *) xmalloc (sizeof *s);
1525   memset (s, 0, sizeof *s);
1526 
1527   s->type = type;
1528   s->bitstringp = bitstringp;
1529 
1530   t->u.kset = s;
1531 
1532   return t;
1533 }
1534 
1535 /* Make a type for a pointer which is relative to an object.  The
1536    second argument is the type of the object to which the pointer is
1537    relative.  The third argument is the type that the pointer points
1538    to.  */
1539 
1540 debug_type
debug_make_offset_type(void * handle,debug_type base_type,debug_type target_type)1541 debug_make_offset_type (void *handle, debug_type base_type,
1542 			debug_type target_type)
1543 {
1544   struct debug_handle *info = (struct debug_handle *) handle;
1545   struct debug_type_s *t;
1546   struct debug_offset_type *o;
1547 
1548   if (base_type == NULL || target_type == NULL)
1549     return DEBUG_TYPE_NULL;
1550 
1551   t = debug_make_type (info, DEBUG_KIND_OFFSET, 0);
1552   if (t == NULL)
1553     return DEBUG_TYPE_NULL;
1554 
1555   o = (struct debug_offset_type *) xmalloc (sizeof *o);
1556   memset (o, 0, sizeof *o);
1557 
1558   o->base_type = base_type;
1559   o->target_type = target_type;
1560 
1561   t->u.koffset = o;
1562 
1563   return t;
1564 }
1565 
1566 /* Make a type for a method function.  The second argument is the
1567    return type, the third argument is the domain, and the fourth
1568    argument is a NULL terminated array of argument types.  */
1569 
1570 debug_type
debug_make_method_type(void * handle,debug_type return_type,debug_type domain_type,debug_type * arg_types,bool varargs)1571 debug_make_method_type (void *handle, debug_type return_type,
1572 			debug_type domain_type, debug_type *arg_types,
1573 			bool varargs)
1574 {
1575   struct debug_handle *info = (struct debug_handle *) handle;
1576   struct debug_type_s *t;
1577   struct debug_method_type *m;
1578 
1579   if (return_type == NULL)
1580     return DEBUG_TYPE_NULL;
1581 
1582   t = debug_make_type (info, DEBUG_KIND_METHOD, 0);
1583   if (t == NULL)
1584     return DEBUG_TYPE_NULL;
1585 
1586   m = (struct debug_method_type *) xmalloc (sizeof *m);
1587   memset (m, 0, sizeof *m);
1588 
1589   m->return_type = return_type;
1590   m->domain_type = domain_type;
1591   m->arg_types = arg_types;
1592   m->varargs = varargs;
1593 
1594   t->u.kmethod = m;
1595 
1596   return t;
1597 }
1598 
1599 /* Make a const qualified version of a given type.  */
1600 
1601 debug_type
debug_make_const_type(void * handle,debug_type type)1602 debug_make_const_type (void *handle, debug_type type)
1603 {
1604   struct debug_handle *info = (struct debug_handle *) handle;
1605   struct debug_type_s *t;
1606 
1607   if (type == NULL)
1608     return DEBUG_TYPE_NULL;
1609 
1610   t = debug_make_type (info, DEBUG_KIND_CONST, 0);
1611   if (t == NULL)
1612     return DEBUG_TYPE_NULL;
1613 
1614   t->u.kconst = type;
1615 
1616   return t;
1617 }
1618 
1619 /* Make a volatile qualified version of a given type.  */
1620 
1621 debug_type
debug_make_volatile_type(void * handle,debug_type type)1622 debug_make_volatile_type (void *handle, debug_type type)
1623 {
1624   struct debug_handle *info = (struct debug_handle *) handle;
1625   struct debug_type_s *t;
1626 
1627   if (type == NULL)
1628     return DEBUG_TYPE_NULL;
1629 
1630   t = debug_make_type (info, DEBUG_KIND_VOLATILE, 0);
1631   if (t == NULL)
1632     return DEBUG_TYPE_NULL;
1633 
1634   t->u.kvolatile = type;
1635 
1636   return t;
1637 }
1638 
1639 /* Make an undefined tagged type.  For example, a struct which has
1640    been mentioned, but not defined.  */
1641 
1642 debug_type
debug_make_undefined_tagged_type(void * handle,const char * name,enum debug_type_kind kind)1643 debug_make_undefined_tagged_type (void *handle, const char *name,
1644 				  enum debug_type_kind kind)
1645 {
1646   struct debug_handle *info = (struct debug_handle *) handle;
1647   struct debug_type_s *t;
1648 
1649   if (name == NULL)
1650     return DEBUG_TYPE_NULL;
1651 
1652   switch (kind)
1653     {
1654     case DEBUG_KIND_STRUCT:
1655     case DEBUG_KIND_UNION:
1656     case DEBUG_KIND_CLASS:
1657     case DEBUG_KIND_UNION_CLASS:
1658     case DEBUG_KIND_ENUM:
1659       break;
1660 
1661     default:
1662       debug_error (_("debug_make_undefined_type: unsupported kind"));
1663       return DEBUG_TYPE_NULL;
1664     }
1665 
1666   t = debug_make_type (info, kind, 0);
1667   if (t == NULL)
1668     return DEBUG_TYPE_NULL;
1669 
1670   return debug_tag_type (handle, name, t);
1671 }
1672 
1673 /* Make a base class for an object.  The second argument is the base
1674    class type.  The third argument is the bit position of this base
1675    class in the object (always 0 unless doing multiple inheritance).
1676    The fourth argument is whether this is a virtual class.  The fifth
1677    argument is the visibility of the base class.  */
1678 
1679 debug_baseclass
debug_make_baseclass(void * handle ATTRIBUTE_UNUSED,debug_type type,bfd_vma bitpos,bool is_virtual,enum debug_visibility visibility)1680 debug_make_baseclass (void *handle ATTRIBUTE_UNUSED, debug_type type,
1681 		      bfd_vma bitpos, bool is_virtual,
1682 		      enum debug_visibility visibility)
1683 {
1684   struct debug_baseclass_s *b;
1685 
1686   b = (struct debug_baseclass_s *) xmalloc (sizeof *b);
1687   memset (b, 0, sizeof *b);
1688 
1689   b->type = type;
1690   b->bitpos = bitpos;
1691   b->is_virtual = is_virtual;
1692   b->visibility = visibility;
1693 
1694   return b;
1695 }
1696 
1697 /* Make a field for a struct.  The second argument is the name.  The
1698    third argument is the type of the field.  The fourth argument is
1699    the bit position of the field.  The fifth argument is the size of
1700    the field (it may be zero).  The sixth argument is the visibility
1701    of the field.  */
1702 
1703 debug_field
debug_make_field(void * handle ATTRIBUTE_UNUSED,const char * name,debug_type type,bfd_vma bitpos,bfd_vma bitsize,enum debug_visibility visibility)1704 debug_make_field (void *handle ATTRIBUTE_UNUSED, const char *name,
1705 		  debug_type type, bfd_vma bitpos, bfd_vma bitsize,
1706 		  enum debug_visibility visibility)
1707 {
1708   struct debug_field_s *f;
1709 
1710   f = (struct debug_field_s *) xmalloc (sizeof *f);
1711   memset (f, 0, sizeof *f);
1712 
1713   f->name = name;
1714   f->type = type;
1715   f->static_member = false;
1716   f->u.f.bitpos = bitpos;
1717   f->u.f.bitsize = bitsize;
1718   f->visibility = visibility;
1719 
1720   return f;
1721 }
1722 
1723 /* Make a static member of an object.  The second argument is the
1724    name.  The third argument is the type of the member.  The fourth
1725    argument is the physical name of the member (i.e., the name as a
1726    global variable).  The fifth argument is the visibility of the
1727    member.  */
1728 
1729 debug_field
debug_make_static_member(void * handle ATTRIBUTE_UNUSED,const char * name,debug_type type,const char * physname,enum debug_visibility visibility)1730 debug_make_static_member (void *handle ATTRIBUTE_UNUSED, const char *name,
1731 			  debug_type type, const char *physname,
1732 			  enum debug_visibility visibility)
1733 {
1734   struct debug_field_s *f;
1735 
1736   f = (struct debug_field_s *) xmalloc (sizeof *f);
1737   memset (f, 0, sizeof *f);
1738 
1739   f->name = name;
1740   f->type = type;
1741   f->static_member = true;
1742   f->u.s.physname = physname;
1743   f->visibility = visibility;
1744 
1745   return f;
1746 }
1747 
1748 /* Make a method.  The second argument is the name, and the third
1749    argument is a NULL terminated array of method variants.  */
1750 
1751 debug_method
debug_make_method(void * handle ATTRIBUTE_UNUSED,const char * name,debug_method_variant * variants)1752 debug_make_method (void *handle ATTRIBUTE_UNUSED, const char *name,
1753 		   debug_method_variant *variants)
1754 {
1755   struct debug_method_s *m;
1756 
1757   m = (struct debug_method_s *) xmalloc (sizeof *m);
1758   memset (m, 0, sizeof *m);
1759 
1760   m->name = name;
1761   m->variants = variants;
1762 
1763   return m;
1764 }
1765 
1766 /* Make a method argument.  The second argument is the real name of
1767    the function.  The third argument is the type of the function.  The
1768    fourth argument is the visibility.  The fifth argument is whether
1769    this is a const function.  The sixth argument is whether this is a
1770    volatile function.  The seventh argument is the offset in the
1771    virtual function table, if any.  The eighth argument is the virtual
1772    function context.  FIXME: Are the const and volatile arguments
1773    necessary?  Could we just use debug_make_const_type?  */
1774 
1775 debug_method_variant
debug_make_method_variant(void * handle ATTRIBUTE_UNUSED,const char * physname,debug_type type,enum debug_visibility visibility,bool constp,bool volatilep,bfd_vma voffset,debug_type context)1776 debug_make_method_variant (void *handle ATTRIBUTE_UNUSED,
1777 			   const char *physname, debug_type type,
1778 			   enum debug_visibility visibility,
1779 			   bool constp, bool volatilep,
1780 			   bfd_vma voffset, debug_type context)
1781 {
1782   struct debug_method_variant_s *m;
1783 
1784   m = (struct debug_method_variant_s *) xmalloc (sizeof *m);
1785   memset (m, 0, sizeof *m);
1786 
1787   m->physname = physname;
1788   m->type = type;
1789   m->visibility = visibility;
1790   m->constp = constp;
1791   m->volatilep = volatilep;
1792   m->voffset = voffset;
1793   m->context = context;
1794 
1795   return m;
1796 }
1797 
1798 /* Make a static method argument.  The arguments are the same as for
1799    debug_make_method_variant, except that the last two are omitted
1800    since a static method can not also be virtual.  */
1801 
1802 debug_method_variant
debug_make_static_method_variant(void * handle ATTRIBUTE_UNUSED,const char * physname,debug_type type,enum debug_visibility visibility,bool constp,bool volatilep)1803 debug_make_static_method_variant (void *handle ATTRIBUTE_UNUSED,
1804 				  const char *physname, debug_type type,
1805 				  enum debug_visibility visibility,
1806 				  bool constp, bool volatilep)
1807 {
1808   struct debug_method_variant_s *m;
1809 
1810   m = (struct debug_method_variant_s *) xmalloc (sizeof *m);
1811   memset (m, 0, sizeof *m);
1812 
1813   m->physname = physname;
1814   m->type = type;
1815   m->visibility = visibility;
1816   m->constp = constp;
1817   m->volatilep = volatilep;
1818   m->voffset = VOFFSET_STATIC_METHOD;
1819 
1820   return m;
1821 }
1822 
1823 /* Name a type.  */
1824 
1825 debug_type
debug_name_type(void * handle,const char * name,debug_type type)1826 debug_name_type (void *handle, const char *name, debug_type type)
1827 {
1828   struct debug_handle *info = (struct debug_handle *) handle;
1829   struct debug_type_s *t;
1830   struct debug_named_type *n;
1831   struct debug_name *nm;
1832 
1833   if (name == NULL || type == NULL)
1834     return DEBUG_TYPE_NULL;
1835 
1836   if (info->current_unit == NULL
1837       || info->current_file == NULL)
1838     {
1839       debug_error (_("debug_name_type: no current file"));
1840       return DEBUG_TYPE_NULL;
1841     }
1842 
1843   t = debug_make_type (info, DEBUG_KIND_NAMED, 0);
1844   if (t == NULL)
1845     return DEBUG_TYPE_NULL;
1846 
1847   n = (struct debug_named_type *) xmalloc (sizeof *n);
1848   memset (n, 0, sizeof *n);
1849 
1850   n->type = type;
1851 
1852   t->u.knamed = n;
1853 
1854   /* We always add the name to the global namespace.  This is probably
1855      wrong in some cases, but it seems to be right for stabs.  FIXME.  */
1856 
1857   nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1858 			       DEBUG_OBJECT_TYPE, DEBUG_LINKAGE_NONE);
1859   if (nm == NULL)
1860     return DEBUG_TYPE_NULL;
1861 
1862   nm->u.type = t;
1863 
1864   n->name = nm;
1865 
1866   return t;
1867 }
1868 
1869 /* Tag a type.  */
1870 
1871 debug_type
debug_tag_type(void * handle,const char * name,debug_type type)1872 debug_tag_type (void *handle, const char *name, debug_type type)
1873 {
1874   struct debug_handle *info = (struct debug_handle *) handle;
1875   struct debug_type_s *t;
1876   struct debug_named_type *n;
1877   struct debug_name *nm;
1878 
1879   if (name == NULL || type == NULL)
1880     return DEBUG_TYPE_NULL;
1881 
1882   if (info->current_file == NULL)
1883     {
1884       debug_error (_("debug_tag_type: no current file"));
1885       return DEBUG_TYPE_NULL;
1886     }
1887 
1888   if (type->kind == DEBUG_KIND_TAGGED)
1889     {
1890       if (strcmp (type->u.knamed->name->name, name) == 0)
1891 	return type;
1892       debug_error (_("debug_tag_type: extra tag attempted"));
1893       return DEBUG_TYPE_NULL;
1894     }
1895 
1896   t = debug_make_type (info, DEBUG_KIND_TAGGED, 0);
1897   if (t == NULL)
1898     return DEBUG_TYPE_NULL;
1899 
1900   n = (struct debug_named_type *) xmalloc (sizeof *n);
1901   memset (n, 0, sizeof *n);
1902 
1903   n->type = type;
1904 
1905   t->u.knamed = n;
1906 
1907   /* We keep a global namespace of tags for each compilation unit.  I
1908      don't know if that is the right thing to do.  */
1909 
1910   nm = debug_add_to_namespace (info, &info->current_file->globals, name,
1911 			       DEBUG_OBJECT_TAG, DEBUG_LINKAGE_NONE);
1912   if (nm == NULL)
1913     return DEBUG_TYPE_NULL;
1914 
1915   nm->u.tag = t;
1916 
1917   n->name = nm;
1918 
1919   return t;
1920 }
1921 
1922 /* Record the size of a given type.  */
1923 
1924 bool
debug_record_type_size(void * handle ATTRIBUTE_UNUSED,debug_type type,unsigned int size)1925 debug_record_type_size (void *handle ATTRIBUTE_UNUSED, debug_type type,
1926 			unsigned int size)
1927 {
1928   if (type->size != 0 && type->size != size)
1929     fprintf (stderr, _("Warning: changing type size from %d to %d\n"),
1930 	     type->size, size);
1931 
1932   type->size = size;
1933 
1934   return true;
1935 }
1936 
1937 /* Find a named type.  */
1938 
1939 debug_type
debug_find_named_type(void * handle,const char * name)1940 debug_find_named_type (void *handle, const char *name)
1941 {
1942   struct debug_handle *info = (struct debug_handle *) handle;
1943   struct debug_block *b;
1944   struct debug_file *f;
1945 
1946   /* We only search the current compilation unit.  I don't know if
1947      this is right or not.  */
1948 
1949   if (info->current_unit == NULL)
1950     {
1951       debug_error (_("debug_find_named_type: no current compilation unit"));
1952       return DEBUG_TYPE_NULL;
1953     }
1954 
1955   for (b = info->current_block; b != NULL; b = b->parent)
1956     {
1957       if (b->locals != NULL)
1958 	{
1959 	  struct debug_name *n;
1960 
1961 	  for (n = b->locals->list; n != NULL; n = n->next)
1962 	    {
1963 	      if (n->kind == DEBUG_OBJECT_TYPE
1964 		  && n->name[0] == name[0]
1965 		  && strcmp (n->name, name) == 0)
1966 		return n->u.type;
1967 	    }
1968 	}
1969     }
1970 
1971   for (f = info->current_unit->files; f != NULL; f = f->next)
1972     {
1973       if (f->globals != NULL)
1974 	{
1975 	  struct debug_name *n;
1976 
1977 	  for (n = f->globals->list; n != NULL; n = n->next)
1978 	    {
1979 	      if (n->kind == DEBUG_OBJECT_TYPE
1980 		  && n->name[0] == name[0]
1981 		  && strcmp (n->name, name) == 0)
1982 		return n->u.type;
1983 	    }
1984 	}
1985     }
1986 
1987   return DEBUG_TYPE_NULL;
1988 }
1989 
1990 /* Find a tagged type.  */
1991 
1992 debug_type
debug_find_tagged_type(void * handle,const char * name,enum debug_type_kind kind)1993 debug_find_tagged_type (void *handle, const char *name,
1994 			enum debug_type_kind kind)
1995 {
1996   struct debug_handle *info = (struct debug_handle *) handle;
1997   struct debug_unit *u;
1998 
1999   /* We search the globals of all the compilation units.  I don't know
2000      if this is correct or not.  It would be easy to change.  */
2001 
2002   for (u = info->units; u != NULL; u = u->next)
2003     {
2004       struct debug_file *f;
2005 
2006       for (f = u->files; f != NULL; f = f->next)
2007 	{
2008 	  struct debug_name *n;
2009 
2010 	  if (f->globals != NULL)
2011 	    {
2012 	      for (n = f->globals->list; n != NULL; n = n->next)
2013 		{
2014 		  if (n->kind == DEBUG_OBJECT_TAG
2015 		      && (kind == DEBUG_KIND_ILLEGAL
2016 			  || n->u.tag->kind == kind)
2017 		      && n->name[0] == name[0]
2018 		      && strcmp (n->name, name) == 0)
2019 		    return n->u.tag;
2020 		}
2021 	    }
2022 	}
2023     }
2024 
2025   return DEBUG_TYPE_NULL;
2026 }
2027 
2028 /* Get a base type.  We build a linked list on the stack to avoid
2029    crashing if the type is defined circularly.  */
2030 
2031 static struct debug_type_s *
debug_get_real_type(void * handle,debug_type type,struct debug_type_real_list * list)2032 debug_get_real_type (void *handle, debug_type type,
2033 		     struct debug_type_real_list *list)
2034 {
2035   struct debug_type_real_list *l;
2036   struct debug_type_real_list rl;
2037 
2038   switch (type->kind)
2039     {
2040     default:
2041       return type;
2042 
2043     case DEBUG_KIND_INDIRECT:
2044     case DEBUG_KIND_NAMED:
2045     case DEBUG_KIND_TAGGED:
2046       break;
2047     }
2048 
2049   for (l = list; l != NULL; l = l->next)
2050     {
2051       if (l->t == type || l == l->next)
2052 	{
2053 	  fprintf (stderr,
2054 		   _("debug_get_real_type: circular debug information for %s\n"),
2055 		   debug_get_type_name (handle, type));
2056 	  return NULL;
2057 	}
2058     }
2059 
2060   rl.next = list;
2061   rl.t = type;
2062 
2063   switch (type->kind)
2064     {
2065       /* The default case is just here to avoid warnings.  */
2066     default:
2067     case DEBUG_KIND_INDIRECT:
2068       /* A valid non-self-referencing indirect type.  */
2069       if (*type->u.kindirect->slot != NULL
2070 	  && *type->u.kindirect->slot != type)
2071 	return debug_get_real_type (handle, *type->u.kindirect->slot, &rl);
2072       return type;
2073     case DEBUG_KIND_NAMED:
2074     case DEBUG_KIND_TAGGED:
2075       return debug_get_real_type (handle, type->u.knamed->type, &rl);
2076     }
2077   /*NOTREACHED*/
2078 }
2079 
2080 /* Get the kind of a type.  */
2081 
2082 enum debug_type_kind
debug_get_type_kind(void * handle,debug_type type)2083 debug_get_type_kind (void *handle, debug_type type)
2084 {
2085   if (type == NULL)
2086     return DEBUG_KIND_ILLEGAL;
2087   type = debug_get_real_type (handle, type, NULL);
2088   if (type == NULL)
2089     return DEBUG_KIND_ILLEGAL;
2090   return type->kind;
2091 }
2092 
2093 /* Get the name of a type.  */
2094 
2095 const char *
debug_get_type_name(void * handle,debug_type type)2096 debug_get_type_name (void *handle, debug_type type)
2097 {
2098   if (type->kind == DEBUG_KIND_INDIRECT)
2099     {
2100       /* A valid non-self-referencing indirect type.  */
2101       if (*type->u.kindirect->slot != NULL
2102 	  && *type->u.kindirect->slot != type)
2103 	return debug_get_type_name (handle, *type->u.kindirect->slot);
2104       return type->u.kindirect->tag;
2105     }
2106   if (type->kind == DEBUG_KIND_NAMED
2107       || type->kind == DEBUG_KIND_TAGGED)
2108     return type->u.knamed->name->name;
2109   return NULL;
2110 }
2111 
2112 /* Get the size of a type.  */
2113 
2114 bfd_vma
debug_get_type_size(void * handle,debug_type type)2115 debug_get_type_size (void *handle, debug_type type)
2116 {
2117   if (type == NULL)
2118     return 0;
2119 
2120   /* We don't call debug_get_real_type, because somebody might have
2121      called debug_record_type_size on a named or indirect type.  */
2122 
2123   if (type->size != 0)
2124     return type->size;
2125 
2126   switch (type->kind)
2127     {
2128     default:
2129       return 0;
2130     case DEBUG_KIND_INDIRECT:
2131       /* A valid non-self-referencing indirect type.  */
2132       if (*type->u.kindirect->slot != NULL
2133 	  && *type->u.kindirect->slot != type)
2134 	return debug_get_type_size (handle, *type->u.kindirect->slot);
2135       return 0;
2136     case DEBUG_KIND_NAMED:
2137     case DEBUG_KIND_TAGGED:
2138       return debug_get_type_size (handle, type->u.knamed->type);
2139     }
2140   /*NOTREACHED*/
2141 }
2142 
2143 /* Get the return type of a function or method type.  */
2144 
2145 debug_type
debug_get_return_type(void * handle,debug_type type)2146 debug_get_return_type (void *handle, debug_type type)
2147 {
2148   if (type == NULL)
2149     return DEBUG_TYPE_NULL;
2150 
2151   type = debug_get_real_type (handle, type, NULL);
2152   if (type == NULL)
2153     return DEBUG_TYPE_NULL;
2154 
2155   switch (type->kind)
2156     {
2157     default:
2158       return DEBUG_TYPE_NULL;
2159     case DEBUG_KIND_FUNCTION:
2160       return type->u.kfunction->return_type;
2161     case DEBUG_KIND_METHOD:
2162       return type->u.kmethod->return_type;
2163     }
2164   /*NOTREACHED*/
2165 }
2166 
2167 /* Get the parameter types of a function or method type (except that
2168    we don't currently store the parameter types of a function).  */
2169 
2170 const debug_type *
debug_get_parameter_types(void * handle,debug_type type,bool * pvarargs)2171 debug_get_parameter_types (void *handle, debug_type type,
2172 			   bool *pvarargs)
2173 {
2174   if (type == NULL)
2175     return NULL;
2176 
2177   type = debug_get_real_type (handle, type, NULL);
2178   if (type == NULL)
2179     return NULL;
2180 
2181   switch (type->kind)
2182     {
2183     default:
2184       return NULL;
2185     case DEBUG_KIND_FUNCTION:
2186       *pvarargs = type->u.kfunction->varargs;
2187       return type->u.kfunction->arg_types;
2188     case DEBUG_KIND_METHOD:
2189       *pvarargs = type->u.kmethod->varargs;
2190       return type->u.kmethod->arg_types;
2191     }
2192   /*NOTREACHED*/
2193 }
2194 
2195 /* Get the target type of a type.  */
2196 
2197 debug_type
debug_get_target_type(void * handle,debug_type type)2198 debug_get_target_type (void *handle, debug_type type)
2199 {
2200   if (type == NULL)
2201     return NULL;
2202 
2203   type = debug_get_real_type (handle, type, NULL);
2204   if (type == NULL)
2205     return NULL;
2206 
2207   switch (type->kind)
2208     {
2209     default:
2210       return NULL;
2211     case DEBUG_KIND_POINTER:
2212       return type->u.kpointer;
2213     case DEBUG_KIND_REFERENCE:
2214       return type->u.kreference;
2215     case DEBUG_KIND_CONST:
2216       return type->u.kconst;
2217     case DEBUG_KIND_VOLATILE:
2218       return type->u.kvolatile;
2219     }
2220   /*NOTREACHED*/
2221 }
2222 
2223 /* Get the NULL terminated array of fields for a struct, union, or
2224    class.  */
2225 
2226 const debug_field *
debug_get_fields(void * handle,debug_type type)2227 debug_get_fields (void *handle, debug_type type)
2228 {
2229   if (type == NULL)
2230     return NULL;
2231 
2232   type = debug_get_real_type (handle, type, NULL);
2233   if (type == NULL)
2234     return NULL;
2235 
2236   switch (type->kind)
2237     {
2238     default:
2239       return NULL;
2240     case DEBUG_KIND_STRUCT:
2241     case DEBUG_KIND_UNION:
2242     case DEBUG_KIND_CLASS:
2243     case DEBUG_KIND_UNION_CLASS:
2244       return type->u.kclass->fields;
2245     }
2246   /*NOTREACHED*/
2247 }
2248 
2249 /* Get the type of a field.  */
2250 
2251 debug_type
debug_get_field_type(void * handle ATTRIBUTE_UNUSED,debug_field field)2252 debug_get_field_type (void *handle ATTRIBUTE_UNUSED, debug_field field)
2253 {
2254   if (field == NULL)
2255     return NULL;
2256   return field->type;
2257 }
2258 
2259 /* Get the name of a field.  */
2260 
2261 const char *
debug_get_field_name(void * handle ATTRIBUTE_UNUSED,debug_field field)2262 debug_get_field_name (void *handle ATTRIBUTE_UNUSED, debug_field field)
2263 {
2264   if (field == NULL)
2265     return NULL;
2266   return field->name;
2267 }
2268 
2269 /* Get the bit position of a field.  */
2270 
2271 bfd_vma
debug_get_field_bitpos(void * handle ATTRIBUTE_UNUSED,debug_field field)2272 debug_get_field_bitpos (void *handle ATTRIBUTE_UNUSED, debug_field field)
2273 {
2274   if (field == NULL || field->static_member)
2275     return (bfd_vma) -1;
2276   return field->u.f.bitpos;
2277 }
2278 
2279 /* Get the bit size of a field.  */
2280 
2281 bfd_vma
debug_get_field_bitsize(void * handle ATTRIBUTE_UNUSED,debug_field field)2282 debug_get_field_bitsize (void *handle ATTRIBUTE_UNUSED, debug_field field)
2283 {
2284   if (field == NULL || field->static_member)
2285     return (bfd_vma) -1;
2286   return field->u.f.bitsize;
2287 }
2288 
2289 /* Get the visibility of a field.  */
2290 
2291 enum debug_visibility
debug_get_field_visibility(void * handle ATTRIBUTE_UNUSED,debug_field field)2292 debug_get_field_visibility (void *handle ATTRIBUTE_UNUSED, debug_field field)
2293 {
2294   if (field == NULL)
2295     return DEBUG_VISIBILITY_IGNORE;
2296   return field->visibility;
2297 }
2298 
2299 /* Get the physical name of a field.  */
2300 
2301 const char *
debug_get_field_physname(void * handle ATTRIBUTE_UNUSED,debug_field field)2302 debug_get_field_physname (void *handle ATTRIBUTE_UNUSED, debug_field field)
2303 {
2304   if (field == NULL || ! field->static_member)
2305     return NULL;
2306   return field->u.s.physname;
2307 }
2308 
2309 /* Write out the debugging information.  This is given a handle to
2310    debugging information, and a set of function pointers to call.  */
2311 
2312 bool
debug_write(void * handle,const struct debug_write_fns * fns,void * fhandle)2313 debug_write (void *handle, const struct debug_write_fns *fns, void *fhandle)
2314 {
2315   struct debug_handle *info = (struct debug_handle *) handle;
2316   struct debug_unit *u;
2317 
2318   /* We use a mark to tell whether we have already written out a
2319      particular name.  We use an integer, so that we don't have to
2320      clear the mark fields if we happen to write out the same
2321      information more than once.  */
2322   ++info->mark;
2323 
2324   /* The base_id field holds an ID value which will never be used, so
2325      that we can tell whether we have assigned an ID during this call
2326      to debug_write.  */
2327   info->base_id = info->class_id;
2328 
2329   /* We keep a linked list of classes for which was have assigned ID's
2330      during this call to debug_write.  */
2331   info->id_list = NULL;
2332 
2333   for (u = info->units; u != NULL; u = u->next)
2334     {
2335       struct debug_file *f;
2336       bool first_file;
2337 
2338       info->current_write_lineno = u->linenos;
2339       info->current_write_lineno_index = 0;
2340 
2341       if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
2342 	return false;
2343 
2344       first_file = true;
2345       for (f = u->files; f != NULL; f = f->next)
2346 	{
2347 	  struct debug_name *n;
2348 
2349 	  if (first_file)
2350 	    first_file = false;
2351 	  else if (! (*fns->start_source) (fhandle, f->filename))
2352 	    return false;
2353 
2354 	  if (f->globals != NULL)
2355 	    for (n = f->globals->list; n != NULL; n = n->next)
2356 	      if (! debug_write_name (info, fns, fhandle, n))
2357 		return false;
2358 	}
2359 
2360       /* Output any line number information which hasn't already been
2361          handled.  */
2362       if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
2363 	return false;
2364     }
2365 
2366   return true;
2367 }
2368 
2369 /* Write out an element in a namespace.  */
2370 
2371 static bool
debug_write_name(struct debug_handle * info,const struct debug_write_fns * fns,void * fhandle,struct debug_name * n)2372 debug_write_name (struct debug_handle *info,
2373 		  const struct debug_write_fns *fns, void *fhandle,
2374 		  struct debug_name *n)
2375 {
2376   switch (n->kind)
2377     {
2378     case DEBUG_OBJECT_TYPE:
2379       if (! debug_write_type (info, fns, fhandle, n->u.type, n)
2380 	  || ! (*fns->typdef) (fhandle, n->name))
2381 	return false;
2382       return true;
2383     case DEBUG_OBJECT_TAG:
2384       if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
2385 	return false;
2386       return (*fns->tag) (fhandle, n->name);
2387     case DEBUG_OBJECT_VARIABLE:
2388       if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
2389 			      (struct debug_name *) NULL))
2390 	return false;
2391       return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
2392 			       n->u.variable->val);
2393     case DEBUG_OBJECT_FUNCTION:
2394       return debug_write_function (info, fns, fhandle, n->name,
2395 				   n->linkage, n->u.function);
2396     case DEBUG_OBJECT_INT_CONSTANT:
2397       return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
2398     case DEBUG_OBJECT_FLOAT_CONSTANT:
2399       return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
2400     case DEBUG_OBJECT_TYPED_CONSTANT:
2401       if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
2402 			      (struct debug_name *) NULL))
2403 	return false;
2404       return (*fns->typed_constant) (fhandle, n->name,
2405 				     n->u.typed_constant->val);
2406     default:
2407       abort ();
2408       return false;
2409     }
2410   /*NOTREACHED*/
2411 }
2412 
2413 /* Write out a type.  If the type is DEBUG_KIND_NAMED or
2414    DEBUG_KIND_TAGGED, then the name argument is the name for which we
2415    are about to call typedef or tag.  If the type is anything else,
2416    then the name argument is a tag from a DEBUG_KIND_TAGGED type which
2417    points to this one.  */
2418 
2419 static bool
debug_write_type(struct debug_handle * info,const struct debug_write_fns * fns,void * fhandle,struct debug_type_s * type,struct debug_name * name)2420 debug_write_type (struct debug_handle *info,
2421 		  const struct debug_write_fns *fns, void *fhandle,
2422 		  struct debug_type_s *type, struct debug_name *name)
2423 {
2424   unsigned int i;
2425   int is;
2426   const char *tag = NULL;
2427 
2428   if (type == DEBUG_TYPE_NULL)
2429     return (*fns->empty_type) (fhandle);
2430 
2431   /* If we have a name for this type, just output it.  We only output
2432      typedef names after they have been defined.  We output type tags
2433      whenever we are not actually defining them.  */
2434   if ((type->kind == DEBUG_KIND_NAMED
2435        || type->kind == DEBUG_KIND_TAGGED)
2436       && (type->u.knamed->name->mark == info->mark
2437 	  || (type->kind == DEBUG_KIND_TAGGED
2438 	      && type->u.knamed->name != name)))
2439     {
2440       if (type->kind == DEBUG_KIND_NAMED)
2441 	return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
2442       else
2443 	{
2444 	  struct debug_type_s *real;
2445 	  unsigned int id;
2446 
2447 	  real = debug_get_real_type ((void *) info, type, NULL);
2448 	  if (real == NULL)
2449 	    return (*fns->empty_type) (fhandle);
2450 	  id = 0;
2451 	  if ((real->kind == DEBUG_KIND_STRUCT
2452 	       || real->kind == DEBUG_KIND_UNION
2453 	       || real->kind == DEBUG_KIND_CLASS
2454 	       || real->kind == DEBUG_KIND_UNION_CLASS)
2455 	      && real->u.kclass != NULL)
2456 	    {
2457 	      if (real->u.kclass->id <= info->base_id)
2458 		{
2459 		  if (! debug_set_class_id (info,
2460 					    type->u.knamed->name->name,
2461 					    real))
2462 		    return false;
2463 		}
2464 	      id = real->u.kclass->id;
2465 	    }
2466 
2467 	  return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
2468 				   real->kind);
2469 	}
2470     }
2471 
2472   /* Mark the name after we have already looked for a known name, so
2473      that we don't just define a type in terms of itself.  We need to
2474      mark the name here so that a struct containing a pointer to
2475      itself will work.  */
2476   if (name != NULL)
2477     name->mark = info->mark;
2478 
2479   if (name != NULL
2480       && type->kind != DEBUG_KIND_NAMED
2481       && type->kind != DEBUG_KIND_TAGGED)
2482     {
2483       assert (name->kind == DEBUG_OBJECT_TAG);
2484       tag = name->name;
2485     }
2486 
2487   switch (type->kind)
2488     {
2489     case DEBUG_KIND_ILLEGAL:
2490       debug_error (_("debug_write_type: illegal type encountered"));
2491       return false;
2492     case DEBUG_KIND_INDIRECT:
2493       /* Prevent infinite recursion.  */
2494       if (*type->u.kindirect->slot == type)
2495 	return (*fns->empty_type) (fhandle);
2496       return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
2497 			       name);
2498     case DEBUG_KIND_VOID:
2499       return (*fns->void_type) (fhandle);
2500     case DEBUG_KIND_INT:
2501       return (*fns->int_type) (fhandle, type->size, type->u.kint);
2502     case DEBUG_KIND_FLOAT:
2503       return (*fns->float_type) (fhandle, type->size);
2504     case DEBUG_KIND_COMPLEX:
2505       return (*fns->complex_type) (fhandle, type->size);
2506     case DEBUG_KIND_BOOL:
2507       return (*fns->bool_type) (fhandle, type->size);
2508     case DEBUG_KIND_STRUCT:
2509     case DEBUG_KIND_UNION:
2510       if (type->u.kclass != NULL)
2511 	{
2512 	  if (type->u.kclass->id <= info->base_id)
2513 	    {
2514 	      if (! debug_set_class_id (info, tag, type))
2515 		return false;
2516 	    }
2517 
2518 	  if (info->mark == type->u.kclass->mark)
2519 	    {
2520 	      /* We are currently outputting this struct, or we have
2521 		 already output it.  I don't know if this can happen,
2522 		 but it can happen for a class.  */
2523 	      assert (type->u.kclass->id > info->base_id);
2524 	      return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2525 				       type->kind);
2526 	    }
2527 	  type->u.kclass->mark = info->mark;
2528 	}
2529 
2530       if (! (*fns->start_struct_type) (fhandle, tag,
2531 				       (type->u.kclass != NULL
2532 					? type->u.kclass->id
2533 					: 0),
2534 				       type->kind == DEBUG_KIND_STRUCT,
2535 				       type->size))
2536 	return false;
2537       if (type->u.kclass != NULL
2538 	  && type->u.kclass->fields != NULL)
2539 	{
2540 	  for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2541 	    {
2542 	      struct debug_field_s *f;
2543 
2544 	      f = type->u.kclass->fields[i];
2545 	      if (! debug_write_type (info, fns, fhandle, f->type,
2546 				      (struct debug_name *) NULL)
2547 		  || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2548 					     f->u.f.bitsize, f->visibility))
2549 		return false;
2550 	    }
2551 	}
2552       return (*fns->end_struct_type) (fhandle);
2553     case DEBUG_KIND_CLASS:
2554     case DEBUG_KIND_UNION_CLASS:
2555       return debug_write_class_type (info, fns, fhandle, type, tag);
2556     case DEBUG_KIND_ENUM:
2557       if (type->u.kenum == NULL)
2558 	return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
2559 				  (bfd_signed_vma *) NULL);
2560       return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
2561 				type->u.kenum->values);
2562     case DEBUG_KIND_POINTER:
2563       if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
2564 			      (struct debug_name *) NULL))
2565 	return false;
2566       return (*fns->pointer_type) (fhandle);
2567     case DEBUG_KIND_FUNCTION:
2568       if (! debug_write_type (info, fns, fhandle,
2569 			      type->u.kfunction->return_type,
2570 			      (struct debug_name *) NULL))
2571 	return false;
2572       if (type->u.kfunction->arg_types == NULL)
2573 	is = -1;
2574       else
2575 	{
2576 	  for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
2577 	    if (! debug_write_type (info, fns, fhandle,
2578 				    type->u.kfunction->arg_types[is],
2579 				    (struct debug_name *) NULL))
2580 	      return false;
2581 	}
2582       return (*fns->function_type) (fhandle, is,
2583 				    type->u.kfunction->varargs);
2584     case DEBUG_KIND_REFERENCE:
2585       if (! debug_write_type (info, fns, fhandle, type->u.kreference,
2586 			      (struct debug_name *) NULL))
2587 	return false;
2588       return (*fns->reference_type) (fhandle);
2589     case DEBUG_KIND_RANGE:
2590       if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
2591 			      (struct debug_name *) NULL))
2592 	return false;
2593       return (*fns->range_type) (fhandle, type->u.krange->lower,
2594 				 type->u.krange->upper);
2595     case DEBUG_KIND_ARRAY:
2596       if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
2597 			      (struct debug_name *) NULL)
2598 	  || ! debug_write_type (info, fns, fhandle,
2599 				 type->u.karray->range_type,
2600 				 (struct debug_name *) NULL))
2601 	return false;
2602       return (*fns->array_type) (fhandle, type->u.karray->lower,
2603 				 type->u.karray->upper,
2604 				 type->u.karray->stringp);
2605     case DEBUG_KIND_SET:
2606       if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
2607 			      (struct debug_name *) NULL))
2608 	return false;
2609       return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
2610     case DEBUG_KIND_OFFSET:
2611       if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
2612 			      (struct debug_name *) NULL)
2613 	  || ! debug_write_type (info, fns, fhandle,
2614 				 type->u.koffset->target_type,
2615 				 (struct debug_name *) NULL))
2616 	return false;
2617       return (*fns->offset_type) (fhandle);
2618     case DEBUG_KIND_METHOD:
2619       if (! debug_write_type (info, fns, fhandle,
2620 			      type->u.kmethod->return_type,
2621 			      (struct debug_name *) NULL))
2622 	return false;
2623       if (type->u.kmethod->arg_types == NULL)
2624 	is = -1;
2625       else
2626 	{
2627 	  for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
2628 	    if (! debug_write_type (info, fns, fhandle,
2629 				    type->u.kmethod->arg_types[is],
2630 				    (struct debug_name *) NULL))
2631 	      return false;
2632 	}
2633       if (type->u.kmethod->domain_type != NULL)
2634 	{
2635 	  if (! debug_write_type (info, fns, fhandle,
2636 				  type->u.kmethod->domain_type,
2637 				  (struct debug_name *) NULL))
2638 	    return false;
2639 	}
2640       return (*fns->method_type) (fhandle,
2641 				  type->u.kmethod->domain_type != NULL,
2642 				  is,
2643 				  type->u.kmethod->varargs);
2644     case DEBUG_KIND_CONST:
2645       if (! debug_write_type (info, fns, fhandle, type->u.kconst,
2646 			      (struct debug_name *) NULL))
2647 	return false;
2648       return (*fns->const_type) (fhandle);
2649     case DEBUG_KIND_VOLATILE:
2650       if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
2651 			      (struct debug_name *) NULL))
2652 	return false;
2653       return (*fns->volatile_type) (fhandle);
2654     case DEBUG_KIND_NAMED:
2655       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2656 			       (struct debug_name *) NULL);
2657     case DEBUG_KIND_TAGGED:
2658       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
2659 			       type->u.knamed->name);
2660     default:
2661       abort ();
2662       return false;
2663     }
2664 }
2665 
2666 /* Write out a class type.  */
2667 
2668 static bool
debug_write_class_type(struct debug_handle * info,const struct debug_write_fns * fns,void * fhandle,struct debug_type_s * type,const char * tag)2669 debug_write_class_type (struct debug_handle *info,
2670 			const struct debug_write_fns *fns, void *fhandle,
2671 			struct debug_type_s *type, const char *tag)
2672 {
2673   unsigned int i;
2674   unsigned int id;
2675   struct debug_type_s *vptrbase;
2676 
2677   if (type->u.kclass == NULL)
2678     {
2679       id = 0;
2680       vptrbase = NULL;
2681     }
2682   else
2683     {
2684       if (type->u.kclass->id <= info->base_id)
2685 	{
2686 	  if (! debug_set_class_id (info, tag, type))
2687 	    return false;
2688 	}
2689 
2690       if (info->mark == type->u.kclass->mark)
2691 	{
2692 	  /* We are currently outputting this class, or we have
2693 	     already output it.  This can happen when there are
2694 	     methods for an anonymous class.  */
2695 	  assert (type->u.kclass->id > info->base_id);
2696 	  return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
2697 				   type->kind);
2698 	}
2699       type->u.kclass->mark = info->mark;
2700       id = type->u.kclass->id;
2701 
2702       vptrbase = type->u.kclass->vptrbase;
2703       if (vptrbase != NULL && vptrbase != type)
2704 	{
2705 	  if (! debug_write_type (info, fns, fhandle, vptrbase,
2706 				  (struct debug_name *) NULL))
2707 	    return false;
2708 	}
2709     }
2710 
2711   if (! (*fns->start_class_type) (fhandle, tag, id,
2712 				  type->kind == DEBUG_KIND_CLASS,
2713 				  type->size,
2714 				  vptrbase != NULL,
2715 				  vptrbase == type))
2716     return false;
2717 
2718   if (type->u.kclass != NULL)
2719     {
2720       if (type->u.kclass->fields != NULL)
2721 	{
2722 	  for (i = 0; type->u.kclass->fields[i] != NULL; i++)
2723 	    {
2724 	      struct debug_field_s *f;
2725 
2726 	      f = type->u.kclass->fields[i];
2727 	      if (! debug_write_type (info, fns, fhandle, f->type,
2728 				      (struct debug_name *) NULL))
2729 		return false;
2730 	      if (f->static_member)
2731 		{
2732 		  if (! (*fns->class_static_member) (fhandle, f->name,
2733 						     f->u.s.physname,
2734 						     f->visibility))
2735 		    return false;
2736 		}
2737 	      else
2738 		{
2739 		  if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
2740 					      f->u.f.bitsize, f->visibility))
2741 		    return false;
2742 		}
2743 	    }
2744 	}
2745 
2746       if (type->u.kclass->baseclasses != NULL)
2747 	{
2748 	  for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
2749 	    {
2750 	      struct debug_baseclass_s *b;
2751 
2752 	      b = type->u.kclass->baseclasses[i];
2753 	      if (! debug_write_type (info, fns, fhandle, b->type,
2754 				      (struct debug_name *) NULL))
2755 		return false;
2756 	      if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->is_virtual,
2757 					     b->visibility))
2758 		return false;
2759 	    }
2760 	}
2761 
2762       if (type->u.kclass->methods != NULL)
2763 	{
2764 	  for (i = 0; type->u.kclass->methods[i] != NULL; i++)
2765 	    {
2766 	      struct debug_method_s *m;
2767 	      unsigned int j;
2768 
2769 	      m = type->u.kclass->methods[i];
2770 	      if (! (*fns->class_start_method) (fhandle, m->name))
2771 		return false;
2772 	      for (j = 0; m->variants[j] != NULL; j++)
2773 		{
2774 		  struct debug_method_variant_s *v;
2775 
2776 		  v = m->variants[j];
2777 		  if (v->context != NULL)
2778 		    {
2779 		      if (! debug_write_type (info, fns, fhandle, v->context,
2780 					      (struct debug_name *) NULL))
2781 			return false;
2782 		    }
2783 		  if (! debug_write_type (info, fns, fhandle, v->type,
2784 					  (struct debug_name *) NULL))
2785 		    return false;
2786 		  if (v->voffset != VOFFSET_STATIC_METHOD)
2787 		    {
2788 		      if (! (*fns->class_method_variant) (fhandle, v->physname,
2789 							  v->visibility,
2790 							  v->constp,
2791 							  v->volatilep,
2792 							  v->voffset,
2793 							  v->context != NULL))
2794 			return false;
2795 		    }
2796 		  else
2797 		    {
2798 		      if (! (*fns->class_static_method_variant) (fhandle,
2799 								 v->physname,
2800 								 v->visibility,
2801 								 v->constp,
2802 								 v->volatilep))
2803 			return false;
2804 		    }
2805 		}
2806 	      if (! (*fns->class_end_method) (fhandle))
2807 		return false;
2808 	    }
2809 	}
2810     }
2811 
2812   return (*fns->end_class_type) (fhandle);
2813 }
2814 
2815 /* Write out information for a function.  */
2816 
2817 static bool
debug_write_function(struct debug_handle * info,const struct debug_write_fns * fns,void * fhandle,const char * name,enum debug_object_linkage linkage,struct debug_function * function)2818 debug_write_function (struct debug_handle *info,
2819 		      const struct debug_write_fns *fns, void *fhandle,
2820 		      const char *name, enum debug_object_linkage linkage,
2821 		      struct debug_function *function)
2822 {
2823   struct debug_parameter *p;
2824   struct debug_block *b;
2825 
2826   if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
2827     return false;
2828 
2829   if (! debug_write_type (info, fns, fhandle, function->return_type,
2830 			  (struct debug_name *) NULL))
2831     return false;
2832 
2833   if (! (*fns->start_function) (fhandle, name,
2834 				linkage == DEBUG_LINKAGE_GLOBAL))
2835     return false;
2836 
2837   for (p = function->parameters; p != NULL; p = p->next)
2838     {
2839       if (! debug_write_type (info, fns, fhandle, p->type,
2840 			      (struct debug_name *) NULL)
2841 	  || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
2842 	return false;
2843     }
2844 
2845   for (b = function->blocks; b != NULL; b = b->next)
2846     {
2847       if (! debug_write_block (info, fns, fhandle, b))
2848 	return false;
2849     }
2850 
2851   return (*fns->end_function) (fhandle);
2852 }
2853 
2854 /* Write out information for a block.  */
2855 
2856 static bool
debug_write_block(struct debug_handle * info,const struct debug_write_fns * fns,void * fhandle,struct debug_block * block)2857 debug_write_block (struct debug_handle *info,
2858 		   const struct debug_write_fns *fns, void *fhandle,
2859 		   struct debug_block *block)
2860 {
2861   struct debug_name *n;
2862   struct debug_block *b;
2863 
2864   if (! debug_write_linenos (info, fns, fhandle, block->start))
2865     return false;
2866 
2867   /* I can't see any point to writing out a block with no local
2868      variables, so we don't bother, except for the top level block.  */
2869   if (block->locals != NULL || block->parent == NULL)
2870     {
2871       if (! (*fns->start_block) (fhandle, block->start))
2872 	return false;
2873     }
2874 
2875   if (block->locals != NULL)
2876     {
2877       for (n = block->locals->list; n != NULL; n = n->next)
2878 	{
2879 	  if (! debug_write_name (info, fns, fhandle, n))
2880 	    return false;
2881 	}
2882     }
2883 
2884   for (b = block->children; b != NULL; b = b->next)
2885     {
2886       if (! debug_write_block (info, fns, fhandle, b))
2887 	return false;
2888     }
2889 
2890   if (! debug_write_linenos (info, fns, fhandle, block->end))
2891     return false;
2892 
2893   if (block->locals != NULL || block->parent == NULL)
2894     {
2895       if (! (*fns->end_block) (fhandle, block->end))
2896 	return false;
2897     }
2898 
2899   return true;
2900 }
2901 
2902 /* Write out line number information up to ADDRESS.  */
2903 
2904 static bool
debug_write_linenos(struct debug_handle * info,const struct debug_write_fns * fns,void * fhandle,bfd_vma address)2905 debug_write_linenos (struct debug_handle *info,
2906 		     const struct debug_write_fns *fns, void *fhandle,
2907 		     bfd_vma address)
2908 {
2909   while (info->current_write_lineno != NULL)
2910     {
2911       struct debug_lineno *l;
2912 
2913       l = info->current_write_lineno;
2914 
2915       while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
2916 	{
2917 	  if (l->linenos[info->current_write_lineno_index]
2918 	      == (unsigned long) -1)
2919 	    break;
2920 
2921 	  if (l->addrs[info->current_write_lineno_index] >= address)
2922 	    return true;
2923 
2924 	  if (! (*fns->lineno) (fhandle, l->file->filename,
2925 				l->linenos[info->current_write_lineno_index],
2926 				l->addrs[info->current_write_lineno_index]))
2927 	    return false;
2928 
2929 	  ++info->current_write_lineno_index;
2930 	}
2931 
2932       info->current_write_lineno = l->next;
2933       info->current_write_lineno_index = 0;
2934     }
2935 
2936   return true;
2937 }
2938 
2939 /* Get the ID number for a class.  If during the same call to
2940    debug_write we find a struct with the same definition with the same
2941    name, we use the same ID.  This type of things happens because the
2942    same struct will be defined by multiple compilation units.  */
2943 
2944 static bool
debug_set_class_id(struct debug_handle * info,const char * tag,struct debug_type_s * type)2945 debug_set_class_id (struct debug_handle *info, const char *tag,
2946 		    struct debug_type_s *type)
2947 {
2948   struct debug_class_type *c;
2949   struct debug_class_id *l;
2950 
2951   assert (type->kind == DEBUG_KIND_STRUCT
2952 	  || type->kind == DEBUG_KIND_UNION
2953 	  || type->kind == DEBUG_KIND_CLASS
2954 	  || type->kind == DEBUG_KIND_UNION_CLASS);
2955 
2956   c = type->u.kclass;
2957 
2958   if (c->id > info->base_id)
2959     return true;
2960 
2961   for (l = info->id_list; l != NULL; l = l->next)
2962     {
2963       if (l->type->kind != type->kind)
2964 	continue;
2965 
2966       if (tag == NULL)
2967 	{
2968 	  if (l->tag != NULL)
2969 	    continue;
2970 	}
2971       else
2972 	{
2973 	  if (l->tag == NULL
2974 	      || l->tag[0] != tag[0]
2975 	      || strcmp (l->tag, tag) != 0)
2976 	    continue;
2977 	}
2978 
2979       if (debug_type_samep (info, l->type, type))
2980 	{
2981 	  c->id = l->type->u.kclass->id;
2982 	  return true;
2983 	}
2984     }
2985 
2986   /* There are no identical types.  Use a new ID, and add it to the
2987      list.  */
2988   ++info->class_id;
2989   c->id = info->class_id;
2990 
2991   l = (struct debug_class_id *) xmalloc (sizeof *l);
2992   memset (l, 0, sizeof *l);
2993 
2994   l->type = type;
2995   l->tag = tag;
2996 
2997   l->next = info->id_list;
2998   info->id_list = l;
2999 
3000   return true;
3001 }
3002 
3003 /* See if two types are the same.  At this point, we don't care about
3004    tags and the like.  */
3005 
3006 static bool
debug_type_samep(struct debug_handle * info,struct debug_type_s * t1,struct debug_type_s * t2)3007 debug_type_samep (struct debug_handle *info, struct debug_type_s *t1,
3008 		  struct debug_type_s *t2)
3009 {
3010   struct debug_type_compare_list *l;
3011   struct debug_type_compare_list top;
3012   bool ret;
3013 
3014   if (t1 == NULL)
3015     return t2 == NULL;
3016   if (t2 == NULL)
3017     return false;
3018 
3019   while (t1->kind == DEBUG_KIND_INDIRECT)
3020     {
3021       t1 = *t1->u.kindirect->slot;
3022       if (t1 == NULL)
3023 	return false;
3024     }
3025   while (t2->kind == DEBUG_KIND_INDIRECT)
3026     {
3027       t2 = *t2->u.kindirect->slot;
3028       if (t2 == NULL)
3029 	return false;
3030     }
3031 
3032   if (t1 == t2)
3033     return true;
3034 
3035   /* As a special case, permit a typedef to match a tag, since C++
3036      debugging output will sometimes add a typedef where C debugging
3037      output will not.  */
3038   if (t1->kind == DEBUG_KIND_NAMED
3039       && t2->kind == DEBUG_KIND_TAGGED)
3040     return debug_type_samep (info, t1->u.knamed->type, t2);
3041   else if (t1->kind == DEBUG_KIND_TAGGED
3042 	   && t2->kind == DEBUG_KIND_NAMED)
3043     return debug_type_samep (info, t1, t2->u.knamed->type);
3044 
3045   if (t1->kind != t2->kind
3046       || t1->size != t2->size)
3047     return false;
3048 
3049   /* Get rid of the trivial cases first.  */
3050   switch (t1->kind)
3051     {
3052     default:
3053       break;
3054     case DEBUG_KIND_VOID:
3055     case DEBUG_KIND_FLOAT:
3056     case DEBUG_KIND_COMPLEX:
3057     case DEBUG_KIND_BOOL:
3058       return true;
3059     case DEBUG_KIND_INT:
3060       return t1->u.kint == t2->u.kint;
3061     }
3062 
3063   /* We have to avoid an infinite recursion.  We do this by keeping a
3064      list of types which we are comparing.  We just keep the list on
3065      the stack.  If we encounter a pair of types we are currently
3066      comparing, we just assume that they are equal.  */
3067   for (l = info->compare_list; l != NULL; l = l->next)
3068     {
3069       if (l->t1 == t1 && l->t2 == t2)
3070 	return true;
3071     }
3072 
3073   top.t1 = t1;
3074   top.t2 = t2;
3075   top.next = info->compare_list;
3076   info->compare_list = &top;
3077 
3078   switch (t1->kind)
3079     {
3080     default:
3081       abort ();
3082       ret = false;
3083       break;
3084 
3085     case DEBUG_KIND_STRUCT:
3086     case DEBUG_KIND_UNION:
3087     case DEBUG_KIND_CLASS:
3088     case DEBUG_KIND_UNION_CLASS:
3089       if (t1->u.kclass == NULL)
3090 	ret = t2->u.kclass == NULL;
3091       else if (t2->u.kclass == NULL)
3092 	ret = false;
3093       else if (t1->u.kclass->id > info->base_id
3094 	       && t1->u.kclass->id == t2->u.kclass->id)
3095 	ret = true;
3096       else
3097 	ret = debug_class_type_samep (info, t1, t2);
3098       break;
3099 
3100     case DEBUG_KIND_ENUM:
3101       if (t1->u.kenum == NULL)
3102 	ret = t2->u.kenum == NULL;
3103       else if (t2->u.kenum == NULL)
3104 	ret = false;
3105       else
3106 	{
3107 	  const char **pn1, **pn2;
3108 	  bfd_signed_vma *pv1, *pv2;
3109 
3110 	  pn1 = t1->u.kenum->names;
3111 	  pn2 = t2->u.kenum->names;
3112 	  pv1 = t1->u.kenum->values;
3113 	  pv2 = t2->u.kenum->values;
3114 	  while (*pn1 != NULL && *pn2 != NULL)
3115 	    {
3116 	      if (**pn1 != **pn2
3117 		  || *pv1 != *pv2
3118 		  || strcmp (*pn1, *pn2) != 0)
3119 		break;
3120 	      ++pn1;
3121 	      ++pn2;
3122 	      ++pv1;
3123 	      ++pv2;
3124 	    }
3125 	  ret = *pn1 == NULL && *pn2 == NULL;
3126 	}
3127       break;
3128 
3129     case DEBUG_KIND_POINTER:
3130       ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
3131       break;
3132 
3133     case DEBUG_KIND_FUNCTION:
3134       if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
3135 	  || ! debug_type_samep (info, t1->u.kfunction->return_type,
3136 				 t2->u.kfunction->return_type)
3137 	  || ((t1->u.kfunction->arg_types == NULL)
3138 	      != (t2->u.kfunction->arg_types == NULL)))
3139 	ret = false;
3140       else if (t1->u.kfunction->arg_types == NULL)
3141 	ret = true;
3142       else
3143 	{
3144 	  struct debug_type_s **a1, **a2;
3145 
3146 	  a1 = t1->u.kfunction->arg_types;
3147 	  a2 = t2->u.kfunction->arg_types;
3148 	  while (*a1 != NULL && *a2 != NULL)
3149 	    {
3150 	      if (! debug_type_samep (info, *a1, *a2))
3151 		break;
3152 	      ++a1;
3153 	      ++a2;
3154 	    }
3155 	  ret = *a1 == NULL && *a2 == NULL;
3156 	}
3157       break;
3158 
3159     case DEBUG_KIND_REFERENCE:
3160       ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
3161       break;
3162 
3163     case DEBUG_KIND_RANGE:
3164       ret = (t1->u.krange->lower == t2->u.krange->lower
3165 	     && t1->u.krange->upper == t2->u.krange->upper
3166 	     && debug_type_samep (info, t1->u.krange->type,
3167 				  t2->u.krange->type));
3168       break;
3169 
3170     case DEBUG_KIND_ARRAY:
3171       ret = (t1->u.karray->lower == t2->u.karray->lower
3172 	     && t1->u.karray->upper == t2->u.karray->upper
3173 	     && t1->u.karray->stringp == t2->u.karray->stringp
3174 	     && debug_type_samep (info, t1->u.karray->element_type,
3175 				  t2->u.karray->element_type));
3176       break;
3177 
3178     case DEBUG_KIND_SET:
3179       ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
3180 	     && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
3181       break;
3182 
3183     case DEBUG_KIND_OFFSET:
3184       ret = (debug_type_samep (info, t1->u.koffset->base_type,
3185 			       t2->u.koffset->base_type)
3186 	     && debug_type_samep (info, t1->u.koffset->target_type,
3187 				  t2->u.koffset->target_type));
3188       break;
3189 
3190     case DEBUG_KIND_METHOD:
3191       if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
3192 	  || ! debug_type_samep (info, t1->u.kmethod->return_type,
3193 				 t2->u.kmethod->return_type)
3194 	  || ! debug_type_samep (info, t1->u.kmethod->domain_type,
3195 				 t2->u.kmethod->domain_type)
3196 	  || ((t1->u.kmethod->arg_types == NULL)
3197 	      != (t2->u.kmethod->arg_types == NULL)))
3198 	ret = false;
3199       else if (t1->u.kmethod->arg_types == NULL)
3200 	ret = true;
3201       else
3202 	{
3203 	  struct debug_type_s **a1, **a2;
3204 
3205 	  a1 = t1->u.kmethod->arg_types;
3206 	  a2 = t2->u.kmethod->arg_types;
3207 	  while (*a1 != NULL && *a2 != NULL)
3208 	    {
3209 	      if (! debug_type_samep (info, *a1, *a2))
3210 		break;
3211 	      ++a1;
3212 	      ++a2;
3213 	    }
3214 	  ret = *a1 == NULL && *a2 == NULL;
3215 	}
3216       break;
3217 
3218     case DEBUG_KIND_CONST:
3219       ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
3220       break;
3221 
3222     case DEBUG_KIND_VOLATILE:
3223       ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
3224       break;
3225 
3226     case DEBUG_KIND_NAMED:
3227     case DEBUG_KIND_TAGGED:
3228       ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
3229 	     && debug_type_samep (info, t1->u.knamed->type,
3230 				  t2->u.knamed->type));
3231       break;
3232     }
3233 
3234   info->compare_list = top.next;
3235 
3236   return ret;
3237 }
3238 
3239 /* See if two classes are the same.  This is a subroutine of
3240    debug_type_samep.  */
3241 
3242 static bool
debug_class_type_samep(struct debug_handle * info,struct debug_type_s * t1,struct debug_type_s * t2)3243 debug_class_type_samep (struct debug_handle *info, struct debug_type_s *t1,
3244 			struct debug_type_s *t2)
3245 {
3246   struct debug_class_type *c1, *c2;
3247 
3248   c1 = t1->u.kclass;
3249   c2 = t2->u.kclass;
3250 
3251   if ((c1->fields == NULL) != (c2->fields == NULL)
3252       || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
3253       || (c1->methods == NULL) != (c2->methods == NULL)
3254       || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
3255     return false;
3256 
3257   if (c1->fields != NULL)
3258     {
3259       struct debug_field_s **pf1, **pf2;
3260 
3261       for (pf1 = c1->fields, pf2 = c2->fields;
3262 	   *pf1 != NULL && *pf2 != NULL;
3263 	   pf1++, pf2++)
3264 	{
3265 	  struct debug_field_s *f1, *f2;
3266 
3267 	  f1 = *pf1;
3268 	  f2 = *pf2;
3269 	  if (f1->name[0] != f2->name[0]
3270 	      || f1->visibility != f2->visibility
3271 	      || f1->static_member != f2->static_member)
3272 	    return false;
3273 	  if (f1->static_member)
3274 	    {
3275 	      if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
3276 		return false;
3277 	    }
3278 	  else
3279 	    {
3280 	      if (f1->u.f.bitpos != f2->u.f.bitpos
3281 		  || f1->u.f.bitsize != f2->u.f.bitsize)
3282 		return false;
3283 	    }
3284 	  /* We do the checks which require function calls last.  We
3285              don't require that the types of fields have the same
3286              names, since that sometimes fails in the presence of
3287              typedefs and we really don't care.  */
3288 	  if (strcmp (f1->name, f2->name) != 0
3289 	      || f1->type == NULL
3290 	      || f2->type == NULL
3291 	      || ! debug_type_samep (info,
3292 				     debug_get_real_type ((void *) info,
3293 							  f1->type, NULL),
3294 				     debug_get_real_type ((void *) info,
3295 							  f2->type, NULL)))
3296 	    return false;
3297 	}
3298       if (*pf1 != NULL || *pf2 != NULL)
3299 	return false;
3300     }
3301 
3302   if (c1->vptrbase != NULL)
3303     {
3304       if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
3305 	return false;
3306     }
3307 
3308   if (c1->baseclasses != NULL)
3309     {
3310       struct debug_baseclass_s **pb1, **pb2;
3311 
3312       for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
3313 	   *pb1 != NULL && *pb2 != NULL;
3314 	   ++pb1, ++pb2)
3315 	{
3316 	  struct debug_baseclass_s *b1, *b2;
3317 
3318 	  b1 = *pb1;
3319 	  b2 = *pb2;
3320 	  if (b1->bitpos != b2->bitpos
3321 	      || b1->is_virtual != b2->is_virtual
3322 	      || b1->visibility != b2->visibility
3323 	      || ! debug_type_samep (info, b1->type, b2->type))
3324 	    return false;
3325 	}
3326       if (*pb1 != NULL || *pb2 != NULL)
3327 	return false;
3328     }
3329 
3330   if (c1->methods != NULL)
3331     {
3332       struct debug_method_s **pm1, **pm2;
3333 
3334       for (pm1 = c1->methods, pm2 = c2->methods;
3335 	   *pm1 != NULL && *pm2 != NULL;
3336 	   ++pm1, ++pm2)
3337 	{
3338 	  struct debug_method_s *m1, *m2;
3339 
3340 	  m1 = *pm1;
3341 	  m2 = *pm2;
3342 	  if (m1->name[0] != m2->name[0]
3343 	      || strcmp (m1->name, m2->name) != 0
3344 	      || (m1->variants == NULL) != (m2->variants == NULL))
3345 	    return false;
3346 	  if (m1->variants != NULL)
3347 	    {
3348 	      struct debug_method_variant_s **pv1, **pv2;
3349 
3350 	      for (pv1 = m1->variants, pv2 = m2->variants;
3351 		   *pv1 != NULL && *pv2 != NULL;
3352 		   ++pv1, ++pv2)
3353 		{
3354 		  struct debug_method_variant_s *v1, *v2;
3355 
3356 		  v1 = *pv1;
3357 		  v2 = *pv2;
3358 		  if (v1->physname[0] != v2->physname[0]
3359 		      || v1->visibility != v2->visibility
3360 		      || v1->constp != v2->constp
3361 		      || v1->volatilep != v2->volatilep
3362 		      || v1->voffset != v2->voffset
3363 		      || (v1->context == NULL) != (v2->context == NULL)
3364 		      || strcmp (v1->physname, v2->physname) != 0
3365 		      || ! debug_type_samep (info, v1->type, v2->type))
3366 		    return false;
3367 		  if (v1->context != NULL)
3368 		    {
3369 		      if (! debug_type_samep (info, v1->context,
3370 					      v2->context))
3371 			return false;
3372 		    }
3373 		}
3374 	      if (*pv1 != NULL || *pv2 != NULL)
3375 		return false;
3376 	    }
3377 	}
3378       if (*pm1 != NULL || *pm2 != NULL)
3379 	return false;
3380     }
3381 
3382   return true;
3383 }
3384