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