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