Home | History | Annotate | Download | only in binutils
      1 /* debug.c -- Handle generic debugging information.
      2    Copyright (C) 1995-2016 Free Software Foundation, Inc.
      3    Written by Ian Lance Taylor <ian (at) cygnus.com>.
      4 
      5    This file is part of GNU Binutils.
      6 
      7    This program is free software; you can redistribute it and/or modify
      8    it under the terms of the GNU General Public License as published by
      9    the Free Software Foundation; either version 3 of the License, or
     10    (at your option) any later version.
     11 
     12    This program is distributed in the hope that it will be useful,
     13    but WITHOUT ANY WARRANTY; without even the implied warranty of
     14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15    GNU General Public License for more details.
     16 
     17    You should have received a copy of the GNU General Public License
     18    along with this program; if not, write to the Free Software
     19    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
     20    02110-1301, USA.  */
     21 
     22 
     23 /* This file implements a generic debugging format.  We may eventually
     24    have readers which convert different formats into this generic
     25    format, and writers which write it out.  The initial impetus for
     26    this was writing a converter from stabs to HP IEEE-695 debugging
     27    format.  */
     28 
     29 #include "sysdep.h"
     30 #include <assert.h>
     31 #include "bfd.h"
     32 #include "libiberty.h"
     33 #include "filenames.h"
     34 #include "debug.h"
     35 
     36 /* Global information we keep for debugging.  A pointer to this
     37    structure is the debugging handle passed to all the routines.  */
     38 
     39 struct debug_handle
     40 {
     41   /* A linked list of compilation units.  */
     42   struct debug_unit *units;
     43   /* The current compilation unit.  */
     44   struct debug_unit *current_unit;
     45   /* The current source file.  */
     46   struct debug_file *current_file;
     47   /* The current function.  */
     48   struct debug_function *current_function;
     49   /* The current block.  */
     50   struct debug_block *current_block;
     51   /* The current line number information for the current unit.  */
     52   struct debug_lineno *current_lineno;
     53   /* Mark.  This is used by debug_write.  */
     54   unsigned int mark;
     55   /* A struct/class ID used by debug_write.  */
     56   unsigned int class_id;
     57   /* The base for class_id for this call to debug_write.  */
     58   unsigned int base_id;
     59   /* The current line number in debug_write.  */
     60   struct debug_lineno *current_write_lineno;
     61   unsigned int current_write_lineno_index;
     62   /* A list of classes which have assigned ID's during debug_write.
     63      This is linked through the next_id field of debug_class_type.  */
     64   struct debug_class_id *id_list;
     65   /* A list used to avoid recursion during debug_type_samep.  */
     66   struct debug_type_compare_list *compare_list;
     67 };
     68 
     69 /* Information we keep for a single compilation unit.  */
     70 
     71 struct debug_unit
     72 {
     73   /* The next compilation unit.  */
     74   struct debug_unit *next;
     75   /* A list of files included in this compilation unit.  The first
     76      file is always the main one, and that is where the main file name
     77      is stored.  */
     78   struct debug_file *files;
     79   /* Line number information for this compilation unit.  This is not
     80      stored by function, because assembler code may have line number
     81      information without function information.  */
     82   struct debug_lineno *linenos;
     83 };
     84 
     85 /* Information kept for a single source file.  */
     86 
     87 struct debug_file
     88 {
     89   /* The next source file in this compilation unit.  */
     90   struct debug_file *next;
     91   /* The name of the source file.  */
     92   const char *filename;
     93   /* Global functions, variables, types, etc.  */
     94   struct debug_namespace *globals;
     95 };
     96 
     97 /* A type.  */
     98 
     99 struct debug_type_s
    100 {
    101   /* Kind of type.  */
    102   enum debug_type_kind kind;
    103   /* Size of type (0 if not known).  */
    104   unsigned int size;
    105   /* Type which is a pointer to this type.  */
    106   debug_type pointer;
    107   /* Tagged union with additional information about the type.  */
    108   union
    109     {
    110       /* DEBUG_KIND_INDIRECT.  */
    111       struct debug_indirect_type *kindirect;
    112       /* DEBUG_KIND_INT.  */
    113       /* Whether the integer is unsigned.  */
    114       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_s *kpointer;
    122       /* DEBUG_KIND_FUNCTION.  */
    123       struct debug_function_type *kfunction;
    124       /* DEBUG_KIND_REFERENCE.  */
    125       struct debug_type_s *kreference;
    126       /* DEBUG_KIND_RANGE.  */
    127       struct debug_range_type *krange;
    128       /* DEBUG_KIND_ARRAY.  */
    129       struct debug_array_type *karray;
    130       /* DEBUG_KIND_SET.  */
    131       struct debug_set_type *kset;
    132       /* DEBUG_KIND_OFFSET.  */
    133       struct debug_offset_type *koffset;
    134       /* DEBUG_KIND_METHOD.  */
    135       struct debug_method_type *kmethod;
    136       /* DEBUG_KIND_CONST.  */
    137       struct debug_type_s *kconst;
    138       /* DEBUG_KIND_VOLATILE.  */
    139       struct debug_type_s *kvolatile;
    140       /* DEBUG_KIND_NAMED, DEBUG_KIND_TAGGED.  */
    141       struct debug_named_type *knamed;
    142     } u;
    143 };
    144 
    145 /* Information kept for an indirect type.  */
    146 
    147 struct debug_indirect_type
    148 {
    149   /* Slot where the final type will appear.  */
    150   debug_type *slot;
    151   /* Tag.  */
    152   const char *tag;
    153 };
    154 
    155 /* Information kept for a struct, union, or class.  */
    156 
    157 struct debug_class_type
    158 {
    159   /* NULL terminated array of fields.  */
    160   debug_field *fields;
    161   /* A mark field which indicates whether the struct has already been
    162      printed.  */
    163   unsigned int mark;
    164   /* This is used to uniquely identify unnamed structs when printing.  */
    165   unsigned int id;
    166   /* The remaining fields are only used for DEBUG_KIND_CLASS and
    167      DEBUG_KIND_UNION_CLASS.  */
    168   /* NULL terminated array of base classes.  */
    169   debug_baseclass *baseclasses;
    170   /* NULL terminated array of methods.  */
    171   debug_method *methods;
    172   /* The type of the class providing the virtual function table for
    173      this class.  This may point to the type itself.  */
    174   debug_type vptrbase;
    175 };
    176 
    177 /* Information kept for an enum.  */
    178 
    179 struct debug_enum_type
    180 {
    181   /* NULL terminated array of names.  */
    182   const char **names;
    183   /* Array of corresponding values.  */
    184   bfd_signed_vma *values;
    185 };
    186 
    187 /* Information kept for a function.  FIXME: We should be able to
    188    record the parameter types.  */
    189 
    190 struct debug_function_type
    191 {
    192   /* Return type.  */
    193   debug_type return_type;
    194   /* NULL terminated array of argument types.  */
    195   debug_type *arg_types;
    196   /* Whether the function takes a variable number of arguments.  */
    197   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_s
    275 {
    276   /* Name of the field.  */
    277   const char *name;
    278   /* Type of the field.  */
    279   struct debug_type_s *type;
    280   /* Visibility of the field.  */
    281   enum debug_visibility visibility;
    282   /* Whether this is a static member.  */
    283   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_s
    305 {
    306   /* Type of the base class.  */
    307   struct debug_type_s *type;
    308   /* Bit position of the base class in the object.  */
    309   unsigned int bitpos;
    310   /* Whether the base class is virtual.  */
    311   bfd_boolean is_virtual;
    312   /* Visibility of the base class.  */
    313   enum debug_visibility visibility;
    314 };
    315 
    316 /* A method of an object.  */
    317 
    318 struct debug_method_s
    319 {
    320   /* The name of the method.  */
    321   const char *name;
    322   /* A NULL terminated array of different types of variants.  */
    323   struct debug_method_variant_s **variants;
    324 };
    325 
    326 /* The variants of a method function of an object.  These indicate
    327    which method to run.  */
    328 
    329 struct debug_method_variant_s
    330 {
    331   /* The physical name of the function.  */
    332   const char *physname;
    333   /* The type of the function.  */
    334   struct debug_type_s *type;
    335   /* The visibility of the function.  */
    336   enum debug_visibility visibility;
    337   /* Whether the function is const.  */
    338   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_s *context;
    347 };
    348 
    349 /* A variable.  This is the information we keep for a variable object.
    350    This has no name; a name is associated with a variable in a
    351    debug_name structure.  */
    352 
    353 struct debug_variable
    354 {
    355   /* Kind of variable.  */
    356   enum debug_var_kind kind;
    357   /* Type.  */
    358   debug_type type;
    359   /* Value.  The interpretation of the value depends upon kind.  */
    360   bfd_vma val;
    361 };
    362 
    363 /* A function.  This has no name; a name is associated with a function
    364    in a debug_name structure.  */
    365 
    366 struct debug_function
    367 {
    368   /* Return type.  */
    369   debug_type return_type;
    370   /* Parameter information.  */
    371   struct debug_parameter *parameters;
    372   /* Block information.  The first structure on the list is the main
    373      block of the function, and describes function local variables.  */
    374   struct debug_block *blocks;
    375 };
    376 
    377 /* A function parameter.  */
    378 
    379 struct debug_parameter
    380 {
    381   /* Next parameter.  */
    382   struct debug_parameter *next;
    383   /* Name.  */
    384   const char *name;
    385   /* Type.  */
    386   debug_type type;
    387   /* Kind.  */
    388   enum debug_parm_kind kind;
    389   /* Value (meaning depends upon kind).  */
    390   bfd_vma val;
    391 };
    392 
    393 /* A typed constant.  */
    394 
    395 struct debug_typed_constant
    396 {
    397   /* Type.  */
    398   debug_type type;
    399   /* Value.  FIXME: We may eventually need to support non-integral
    400      values.  */
    401   bfd_vma val;
    402 };
    403 
    404 /* Information about a block within a function.  */
    405 
    406 struct debug_block
    407 {
    408   /* Next block with the same parent.  */
    409   struct debug_block *next;
    410   /* Parent block.  */
    411   struct debug_block *parent;
    412   /* List of child blocks.  */
    413   struct debug_block *children;
    414   /* Start address of the block.  */
    415   bfd_vma start;
    416   /* End address of the block.  */
    417   bfd_vma end;
    418   /* Local variables.  */
    419   struct debug_namespace *locals;
    420 };
    421 
    422 /* Line number information we keep for a compilation unit.  FIXME:
    423    This structure is easy to create, but can be very space
    424    inefficient.  */
    425 
    426 struct debug_lineno
    427 {
    428   /* More line number information for this block.  */
    429   struct debug_lineno *next;
    430   /* Source file.  */
    431   struct debug_file *file;
    432   /* Line numbers, terminated by a -1 or the end of the array.  */
    433 #define DEBUG_LINENO_COUNT 10
    434   unsigned long linenos[DEBUG_LINENO_COUNT];
    435   /* Addresses for the line numbers.  */
    436   bfd_vma addrs[DEBUG_LINENO_COUNT];
    437 };
    438 
    439 /* A namespace.  This is a mapping from names to objects.  FIXME: This
    440    should be implemented as a hash table.  */
    441 
    442 struct debug_namespace
    443 {
    444   /* List of items in this namespace.  */
    445   struct debug_name *list;
    446   /* Pointer to where the next item in this namespace should go.  */
    447   struct debug_name **tail;
    448 };
    449 
    450 /* Kinds of objects that appear in a namespace.  */
    451 
    452 enum debug_object_kind
    453 {
    454   /* A type.  */
    455   DEBUG_OBJECT_TYPE,
    456   /* A tagged type (really a different sort of namespace).  */
    457   DEBUG_OBJECT_TAG,
    458   /* A variable.  */
    459   DEBUG_OBJECT_VARIABLE,
    460   /* A function.  */
    461   DEBUG_OBJECT_FUNCTION,
    462   /* An integer constant.  */
    463   DEBUG_OBJECT_INT_CONSTANT,
    464   /* A floating point constant.  */
    465   DEBUG_OBJECT_FLOAT_CONSTANT,
    466   /* A typed constant.  */
    467   DEBUG_OBJECT_TYPED_CONSTANT
    468 };
    469 
    470 /* Linkage of an object that appears in a namespace.  */
    471 
    472 enum debug_object_linkage
    473 {
    474   /* Local variable.  */
    475   DEBUG_LINKAGE_AUTOMATIC,
    476   /* Static--either file static or function static, depending upon the
    477      namespace is.  */
    478   DEBUG_LINKAGE_STATIC,
    479   /* Global.  */
    480   DEBUG_LINKAGE_GLOBAL,
    481   /* No linkage.  */
    482   DEBUG_LINKAGE_NONE
    483 };
    484 
    485 /* A name in a namespace.  */
    486 
    487 struct debug_name
    488 {
    489   /* Next name in this namespace.  */
    490   struct debug_name *next;
    491   /* Name.  */
    492   const char *name;
    493   /* Mark.  This is used by debug_write.  */
    494   unsigned int mark;
    495   /* Kind of object.  */
    496   enum debug_object_kind kind;
    497   /* Linkage of object.  */
    498   enum debug_object_linkage linkage;
    499   /* Tagged union with additional information about the object.  */
    500   union
    501     {
    502       /* DEBUG_OBJECT_TYPE.  */
    503       struct debug_type_s *type;
    504       /* DEBUG_OBJECT_TAG.  */
    505       struct debug_type_s *tag;
    506       /* DEBUG_OBJECT_VARIABLE.  */
    507       struct debug_variable *variable;
    508       /* DEBUG_OBJECT_FUNCTION.  */
    509       struct debug_function *function;
    510       /* DEBUG_OBJECT_INT_CONSTANT.  */
    511       bfd_vma int_constant;
    512       /* DEBUG_OBJECT_FLOAT_CONSTANT.  */
    513       double float_constant;
    514       /* DEBUG_OBJECT_TYPED_CONSTANT.  */
    515       struct debug_typed_constant *typed_constant;
    516     } u;
    517 };
    518 
    519 /* During debug_write, a linked list of these structures is used to
    520    keep track of ID numbers that have been assigned to classes.  */
    521 
    522 struct debug_class_id
    523 {
    524   /* Next ID number.  */
    525   struct debug_class_id *next;
    526   /* The type with the ID.  */
    527   struct debug_type_s *type;
    528   /* The tag; NULL if no tag.  */
    529   const char *tag;
    530 };
    531 
    532 /* During debug_type_samep, a linked list of these structures is kept
    533    on the stack to avoid infinite recursion.  */
    534 
    535 struct debug_type_compare_list
    536 {
    537   /* Next type on list.  */
    538   struct debug_type_compare_list *next;
    539   /* The types we are comparing.  */
    540   struct debug_type_s *t1;
    541   struct debug_type_s *t2;
    542 };
    543 
    544 /* During debug_get_real_type, a linked list of these structures is
    545    kept on the stack to avoid infinite recursion.  */
    546 
    547 struct debug_type_real_list
    548 {
    549   /* Next type on list.  */
    550   struct debug_type_real_list *next;
    551   /* The type we are checking.  */
    552   struct debug_type_s *t;
    553 };
    554 
    555 /* Local functions.  */
    556 
    557 static void debug_error (const char *);
    558 static struct debug_name *debug_add_to_namespace
    559   (struct debug_handle *, struct debug_namespace **, const char *,
    560    enum debug_object_kind, enum debug_object_linkage);
    561 static struct debug_name *debug_add_to_current_namespace
    562   (struct debug_handle *, const char *, enum debug_object_kind,
    563    enum debug_object_linkage);
    564 static struct debug_type_s *debug_make_type
    565   (struct debug_handle *, enum debug_type_kind, unsigned int);
    566 static struct debug_type_s *debug_get_real_type
    567   (void *, debug_type, struct debug_type_real_list *);
    568 static 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_s *, 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_s *, 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_s *);
    587 static bfd_boolean debug_type_samep
    588   (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
    589 static bfd_boolean debug_class_type_samep
    590   (struct debug_handle *, struct debug_type_s *, struct debug_type_s *);
    591 
    592 /* Issue an error message.  */
    594 
    595 static void
    596 debug_error (const char *message)
    597 {
    598   fprintf (stderr, "%s\n", message);
    599 }
    600 
    601 /* Add an object to a namespace.  */
    602 
    603 static struct debug_name *
    604 debug_add_to_namespace (struct debug_handle *info ATTRIBUTE_UNUSED,
    605 			struct debug_namespace **nsp, const char *name,
    606 			enum debug_object_kind kind,
    607 			enum debug_object_linkage linkage)
    608 {
    609   struct debug_name *n;
    610   struct debug_namespace *ns;
    611 
    612   n = (struct debug_name *) xmalloc (sizeof *n);
    613   memset (n, 0, sizeof *n);
    614 
    615   n->name = name;
    616   n->kind = kind;
    617   n->linkage = linkage;
    618 
    619   ns = *nsp;
    620   if (ns == NULL)
    621     {
    622       ns = (struct debug_namespace *) xmalloc (sizeof *ns);
    623       memset (ns, 0, sizeof *ns);
    624 
    625       ns->tail = &ns->list;
    626 
    627       *nsp = ns;
    628     }
    629 
    630   *ns->tail = n;
    631   ns->tail = &n->next;
    632 
    633   return n;
    634 }
    635 
    636 /* Add an object to the current namespace.  */
    637 
    638 static struct debug_name *
    639 debug_add_to_current_namespace (struct debug_handle *info, const char *name,
    640 				enum debug_object_kind kind,
    641 				enum debug_object_linkage linkage)
    642 {
    643   struct debug_namespace **nsp;
    644 
    645   if (info->current_unit == NULL
    646       || info->current_file == NULL)
    647     {
    648       debug_error (_("debug_add_to_current_namespace: no current file"));
    649       return NULL;
    650     }
    651 
    652   if (info->current_block != NULL)
    653     nsp = &info->current_block->locals;
    654   else
    655     nsp = &info->current_file->globals;
    656 
    657   return debug_add_to_namespace (info, nsp, name, kind, linkage);
    658 }
    659 
    660 /* Return a handle for debugging information.  */
    662 
    663 void *
    664 debug_init (void)
    665 {
    666   struct debug_handle *ret;
    667 
    668   ret = (struct debug_handle *) xmalloc (sizeof *ret);
    669   memset (ret, 0, sizeof *ret);
    670   return (void *) ret;
    671 }
    672 
    673 /* Set the source filename.  This implicitly starts a new compilation
    674    unit.  */
    675 
    676 bfd_boolean
    677 debug_set_filename (void *handle, const char *name)
    678 {
    679   struct debug_handle *info = (struct debug_handle *) handle;
    680   struct debug_file *nfile;
    681   struct debug_unit *nunit;
    682 
    683   if (name == NULL)
    684     name = "";
    685 
    686   nfile = (struct debug_file *) xmalloc (sizeof *nfile);
    687   memset (nfile, 0, sizeof *nfile);
    688 
    689   nfile->filename = name;
    690 
    691   nunit = (struct debug_unit *) xmalloc (sizeof *nunit);
    692   memset (nunit, 0, sizeof *nunit);
    693 
    694   nunit->files = nfile;
    695   info->current_file = nfile;
    696 
    697   if (info->current_unit != NULL)
    698     info->current_unit->next = nunit;
    699   else
    700     {
    701       assert (info->units == NULL);
    702       info->units = nunit;
    703     }
    704 
    705   info->current_unit = nunit;
    706 
    707   info->current_function = NULL;
    708   info->current_block = NULL;
    709   info->current_lineno = NULL;
    710 
    711   return TRUE;
    712 }
    713 
    714 /* Change source files to the given file name.  This is used for
    715    include files in a single compilation unit.  */
    716 
    717 bfd_boolean
    718 debug_start_source (void *handle, const char *name)
    719 {
    720   struct debug_handle *info = (struct debug_handle *) handle;
    721   struct debug_file *f, **pf;
    722 
    723   if (name == NULL)
    724     name = "";
    725 
    726   if (info->current_unit == NULL)
    727     {
    728       debug_error (_("debug_start_source: no debug_set_filename call"));
    729       return FALSE;
    730     }
    731 
    732   for (f = info->current_unit->files; f != NULL; f = f->next)
    733     {
    734       if (filename_cmp (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_s *
   1176 debug_make_type (struct debug_handle *info ATTRIBUTE_UNUSED,
   1177 		 enum debug_type_kind kind, unsigned int size)
   1178 {
   1179   struct debug_type_s *t;
   1180 
   1181   t = (struct debug_type_s *) 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_s *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_s *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_s *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_s *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_s *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_s *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_s *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_s *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_s *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_s *t;
   1485   struct debug_array_type *a;
   1486 
   1487   if (element_type == NULL || range_type == NULL)
   1488     return DEBUG_TYPE_NULL;
   1489 
   1490   t = debug_make_type (info, DEBUG_KIND_ARRAY, 0);
   1491   if (t == NULL)
   1492     return DEBUG_TYPE_NULL;
   1493 
   1494   a = (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_s *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_s *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_s *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_s *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_s *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_s *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 is_virtual,
   1684 		      enum debug_visibility visibility)
   1685 {
   1686   struct debug_baseclass_s *b;
   1687 
   1688   b = (struct debug_baseclass_s *) xmalloc (sizeof *b);
   1689   memset (b, 0, sizeof *b);
   1690 
   1691   b->type = type;
   1692   b->bitpos = bitpos;
   1693   b->is_virtual = is_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_s *f;
   1711 
   1712   f = (struct debug_field_s *) 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_s *f;
   1737 
   1738   f = (struct debug_field_s *) 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_s *m;
   1758 
   1759   m = (struct debug_method_s *) 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_s *m;
   1785 
   1786   m = (struct debug_method_variant_s *) 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_s *m;
   1811 
   1812   m = (struct debug_method_variant_s *) 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_s *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_s *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_s *
   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
   2307    debugging information, and a set of function pointers to call.  */
   2308 
   2309 bfd_boolean
   2310 debug_write (void *handle, const struct debug_write_fns *fns, void *fhandle)
   2311 {
   2312   struct debug_handle *info = (struct debug_handle *) handle;
   2313   struct debug_unit *u;
   2314 
   2315   /* We use a mark to tell whether we have already written out a
   2316      particular name.  We use an integer, so that we don't have to
   2317      clear the mark fields if we happen to write out the same
   2318      information more than once.  */
   2319   ++info->mark;
   2320 
   2321   /* The base_id field holds an ID value which will never be used, so
   2322      that we can tell whether we have assigned an ID during this call
   2323      to debug_write.  */
   2324   info->base_id = info->class_id;
   2325 
   2326   /* We keep a linked list of classes for which was have assigned ID's
   2327      during this call to debug_write.  */
   2328   info->id_list = NULL;
   2329 
   2330   for (u = info->units; u != NULL; u = u->next)
   2331     {
   2332       struct debug_file *f;
   2333       bfd_boolean first_file;
   2334 
   2335       info->current_write_lineno = u->linenos;
   2336       info->current_write_lineno_index = 0;
   2337 
   2338       if (! (*fns->start_compilation_unit) (fhandle, u->files->filename))
   2339 	return FALSE;
   2340 
   2341       first_file = TRUE;
   2342       for (f = u->files; f != NULL; f = f->next)
   2343 	{
   2344 	  struct debug_name *n;
   2345 
   2346 	  if (first_file)
   2347 	    first_file = FALSE;
   2348 	  else if (! (*fns->start_source) (fhandle, f->filename))
   2349 	    return FALSE;
   2350 
   2351 	  if (f->globals != NULL)
   2352 	    for (n = f->globals->list; n != NULL; n = n->next)
   2353 	      if (! debug_write_name (info, fns, fhandle, n))
   2354 		return FALSE;
   2355 	}
   2356 
   2357       /* Output any line number information which hasn't already been
   2358          handled.  */
   2359       if (! debug_write_linenos (info, fns, fhandle, (bfd_vma) -1))
   2360 	return FALSE;
   2361     }
   2362 
   2363   return TRUE;
   2364 }
   2365 
   2366 /* Write out an element in a namespace.  */
   2367 
   2368 static bfd_boolean
   2369 debug_write_name (struct debug_handle *info,
   2370 		  const struct debug_write_fns *fns, void *fhandle,
   2371 		  struct debug_name *n)
   2372 {
   2373   switch (n->kind)
   2374     {
   2375     case DEBUG_OBJECT_TYPE:
   2376       if (! debug_write_type (info, fns, fhandle, n->u.type, n)
   2377 	  || ! (*fns->typdef) (fhandle, n->name))
   2378 	return FALSE;
   2379       return TRUE;
   2380     case DEBUG_OBJECT_TAG:
   2381       if (! debug_write_type (info, fns, fhandle, n->u.tag, n))
   2382 	return FALSE;
   2383       return (*fns->tag) (fhandle, n->name);
   2384     case DEBUG_OBJECT_VARIABLE:
   2385       if (! debug_write_type (info, fns, fhandle, n->u.variable->type,
   2386 			      (struct debug_name *) NULL))
   2387 	return FALSE;
   2388       return (*fns->variable) (fhandle, n->name, n->u.variable->kind,
   2389 			       n->u.variable->val);
   2390     case DEBUG_OBJECT_FUNCTION:
   2391       return debug_write_function (info, fns, fhandle, n->name,
   2392 				   n->linkage, n->u.function);
   2393     case DEBUG_OBJECT_INT_CONSTANT:
   2394       return (*fns->int_constant) (fhandle, n->name, n->u.int_constant);
   2395     case DEBUG_OBJECT_FLOAT_CONSTANT:
   2396       return (*fns->float_constant) (fhandle, n->name, n->u.float_constant);
   2397     case DEBUG_OBJECT_TYPED_CONSTANT:
   2398       if (! debug_write_type (info, fns, fhandle, n->u.typed_constant->type,
   2399 			      (struct debug_name *) NULL))
   2400 	return FALSE;
   2401       return (*fns->typed_constant) (fhandle, n->name,
   2402 				     n->u.typed_constant->val);
   2403     default:
   2404       abort ();
   2405       return FALSE;
   2406     }
   2407   /*NOTREACHED*/
   2408 }
   2409 
   2410 /* Write out a type.  If the type is DEBUG_KIND_NAMED or
   2411    DEBUG_KIND_TAGGED, then the name argument is the name for which we
   2412    are about to call typedef or tag.  If the type is anything else,
   2413    then the name argument is a tag from a DEBUG_KIND_TAGGED type which
   2414    points to this one.  */
   2415 
   2416 static bfd_boolean
   2417 debug_write_type (struct debug_handle *info,
   2418 		  const struct debug_write_fns *fns, void *fhandle,
   2419 		  struct debug_type_s *type, struct debug_name *name)
   2420 {
   2421   unsigned int i;
   2422   int is;
   2423   const char *tag = NULL;
   2424 
   2425   /* If we have a name for this type, just output it.  We only output
   2426      typedef names after they have been defined.  We output type tags
   2427      whenever we are not actually defining them.  */
   2428   if ((type->kind == DEBUG_KIND_NAMED
   2429        || type->kind == DEBUG_KIND_TAGGED)
   2430       && (type->u.knamed->name->mark == info->mark
   2431 	  || (type->kind == DEBUG_KIND_TAGGED
   2432 	      && type->u.knamed->name != name)))
   2433     {
   2434       if (type->kind == DEBUG_KIND_NAMED)
   2435 	return (*fns->typedef_type) (fhandle, type->u.knamed->name->name);
   2436       else
   2437 	{
   2438 	  struct debug_type_s *real;
   2439 	  unsigned int id;
   2440 
   2441 	  real = debug_get_real_type ((void *) info, type, NULL);
   2442 	  if (real == NULL)
   2443 	    return (*fns->empty_type) (fhandle);
   2444 	  id = 0;
   2445 	  if ((real->kind == DEBUG_KIND_STRUCT
   2446 	       || real->kind == DEBUG_KIND_UNION
   2447 	       || real->kind == DEBUG_KIND_CLASS
   2448 	       || real->kind == DEBUG_KIND_UNION_CLASS)
   2449 	      && real->u.kclass != NULL)
   2450 	    {
   2451 	      if (real->u.kclass->id <= info->base_id)
   2452 		{
   2453 		  if (! debug_set_class_id (info,
   2454 					    type->u.knamed->name->name,
   2455 					    real))
   2456 		    return FALSE;
   2457 		}
   2458 	      id = real->u.kclass->id;
   2459 	    }
   2460 
   2461 	  return (*fns->tag_type) (fhandle, type->u.knamed->name->name, id,
   2462 				   real->kind);
   2463 	}
   2464     }
   2465 
   2466   /* Mark the name after we have already looked for a known name, so
   2467      that we don't just define a type in terms of itself.  We need to
   2468      mark the name here so that a struct containing a pointer to
   2469      itself will work.  */
   2470   if (name != NULL)
   2471     name->mark = info->mark;
   2472 
   2473   if (name != NULL
   2474       && type->kind != DEBUG_KIND_NAMED
   2475       && type->kind != DEBUG_KIND_TAGGED)
   2476     {
   2477       assert (name->kind == DEBUG_OBJECT_TAG);
   2478       tag = name->name;
   2479     }
   2480 
   2481   switch (type->kind)
   2482     {
   2483     case DEBUG_KIND_ILLEGAL:
   2484       debug_error (_("debug_write_type: illegal type encountered"));
   2485       return FALSE;
   2486     case DEBUG_KIND_INDIRECT:
   2487       if (*type->u.kindirect->slot == DEBUG_TYPE_NULL)
   2488 	return (*fns->empty_type) (fhandle);
   2489       return debug_write_type (info, fns, fhandle, *type->u.kindirect->slot,
   2490 			       name);
   2491     case DEBUG_KIND_VOID:
   2492       return (*fns->void_type) (fhandle);
   2493     case DEBUG_KIND_INT:
   2494       return (*fns->int_type) (fhandle, type->size, type->u.kint);
   2495     case DEBUG_KIND_FLOAT:
   2496       return (*fns->float_type) (fhandle, type->size);
   2497     case DEBUG_KIND_COMPLEX:
   2498       return (*fns->complex_type) (fhandle, type->size);
   2499     case DEBUG_KIND_BOOL:
   2500       return (*fns->bool_type) (fhandle, type->size);
   2501     case DEBUG_KIND_STRUCT:
   2502     case DEBUG_KIND_UNION:
   2503       if (type->u.kclass != NULL)
   2504 	{
   2505 	  if (type->u.kclass->id <= info->base_id)
   2506 	    {
   2507 	      if (! debug_set_class_id (info, tag, type))
   2508 		return FALSE;
   2509 	    }
   2510 
   2511 	  if (info->mark == type->u.kclass->mark)
   2512 	    {
   2513 	      /* We are currently outputting this struct, or we have
   2514 		 already output it.  I don't know if this can happen,
   2515 		 but it can happen for a class.  */
   2516 	      assert (type->u.kclass->id > info->base_id);
   2517 	      return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
   2518 				       type->kind);
   2519 	    }
   2520 	  type->u.kclass->mark = info->mark;
   2521 	}
   2522 
   2523       if (! (*fns->start_struct_type) (fhandle, tag,
   2524 				       (type->u.kclass != NULL
   2525 					? type->u.kclass->id
   2526 					: 0),
   2527 				       type->kind == DEBUG_KIND_STRUCT,
   2528 				       type->size))
   2529 	return FALSE;
   2530       if (type->u.kclass != NULL
   2531 	  && type->u.kclass->fields != NULL)
   2532 	{
   2533 	  for (i = 0; type->u.kclass->fields[i] != NULL; i++)
   2534 	    {
   2535 	      struct debug_field_s *f;
   2536 
   2537 	      f = type->u.kclass->fields[i];
   2538 	      if (! debug_write_type (info, fns, fhandle, f->type,
   2539 				      (struct debug_name *) NULL)
   2540 		  || ! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
   2541 					     f->u.f.bitsize, f->visibility))
   2542 		return FALSE;
   2543 	    }
   2544 	}
   2545       return (*fns->end_struct_type) (fhandle);
   2546     case DEBUG_KIND_CLASS:
   2547     case DEBUG_KIND_UNION_CLASS:
   2548       return debug_write_class_type (info, fns, fhandle, type, tag);
   2549     case DEBUG_KIND_ENUM:
   2550       if (type->u.kenum == NULL)
   2551 	return (*fns->enum_type) (fhandle, tag, (const char **) NULL,
   2552 				  (bfd_signed_vma *) NULL);
   2553       return (*fns->enum_type) (fhandle, tag, type->u.kenum->names,
   2554 				type->u.kenum->values);
   2555     case DEBUG_KIND_POINTER:
   2556       if (! debug_write_type (info, fns, fhandle, type->u.kpointer,
   2557 			      (struct debug_name *) NULL))
   2558 	return FALSE;
   2559       return (*fns->pointer_type) (fhandle);
   2560     case DEBUG_KIND_FUNCTION:
   2561       if (! debug_write_type (info, fns, fhandle,
   2562 			      type->u.kfunction->return_type,
   2563 			      (struct debug_name *) NULL))
   2564 	return FALSE;
   2565       if (type->u.kfunction->arg_types == NULL)
   2566 	is = -1;
   2567       else
   2568 	{
   2569 	  for (is = 0; type->u.kfunction->arg_types[is] != NULL; is++)
   2570 	    if (! debug_write_type (info, fns, fhandle,
   2571 				    type->u.kfunction->arg_types[is],
   2572 				    (struct debug_name *) NULL))
   2573 	      return FALSE;
   2574 	}
   2575       return (*fns->function_type) (fhandle, is,
   2576 				    type->u.kfunction->varargs);
   2577     case DEBUG_KIND_REFERENCE:
   2578       if (! debug_write_type (info, fns, fhandle, type->u.kreference,
   2579 			      (struct debug_name *) NULL))
   2580 	return FALSE;
   2581       return (*fns->reference_type) (fhandle);
   2582     case DEBUG_KIND_RANGE:
   2583       if (! debug_write_type (info, fns, fhandle, type->u.krange->type,
   2584 			      (struct debug_name *) NULL))
   2585 	return FALSE;
   2586       return (*fns->range_type) (fhandle, type->u.krange->lower,
   2587 				 type->u.krange->upper);
   2588     case DEBUG_KIND_ARRAY:
   2589       if (! debug_write_type (info, fns, fhandle, type->u.karray->element_type,
   2590 			      (struct debug_name *) NULL)
   2591 	  || ! debug_write_type (info, fns, fhandle,
   2592 				 type->u.karray->range_type,
   2593 				 (struct debug_name *) NULL))
   2594 	return FALSE;
   2595       return (*fns->array_type) (fhandle, type->u.karray->lower,
   2596 				 type->u.karray->upper,
   2597 				 type->u.karray->stringp);
   2598     case DEBUG_KIND_SET:
   2599       if (! debug_write_type (info, fns, fhandle, type->u.kset->type,
   2600 			      (struct debug_name *) NULL))
   2601 	return FALSE;
   2602       return (*fns->set_type) (fhandle, type->u.kset->bitstringp);
   2603     case DEBUG_KIND_OFFSET:
   2604       if (! debug_write_type (info, fns, fhandle, type->u.koffset->base_type,
   2605 			      (struct debug_name *) NULL)
   2606 	  || ! debug_write_type (info, fns, fhandle,
   2607 				 type->u.koffset->target_type,
   2608 				 (struct debug_name *) NULL))
   2609 	return FALSE;
   2610       return (*fns->offset_type) (fhandle);
   2611     case DEBUG_KIND_METHOD:
   2612       if (! debug_write_type (info, fns, fhandle,
   2613 			      type->u.kmethod->return_type,
   2614 			      (struct debug_name *) NULL))
   2615 	return FALSE;
   2616       if (type->u.kmethod->arg_types == NULL)
   2617 	is = -1;
   2618       else
   2619 	{
   2620 	  for (is = 0; type->u.kmethod->arg_types[is] != NULL; is++)
   2621 	    if (! debug_write_type (info, fns, fhandle,
   2622 				    type->u.kmethod->arg_types[is],
   2623 				    (struct debug_name *) NULL))
   2624 	      return FALSE;
   2625 	}
   2626       if (type->u.kmethod->domain_type != NULL)
   2627 	{
   2628 	  if (! debug_write_type (info, fns, fhandle,
   2629 				  type->u.kmethod->domain_type,
   2630 				  (struct debug_name *) NULL))
   2631 	    return FALSE;
   2632 	}
   2633       return (*fns->method_type) (fhandle,
   2634 				  type->u.kmethod->domain_type != NULL,
   2635 				  is,
   2636 				  type->u.kmethod->varargs);
   2637     case DEBUG_KIND_CONST:
   2638       if (! debug_write_type (info, fns, fhandle, type->u.kconst,
   2639 			      (struct debug_name *) NULL))
   2640 	return FALSE;
   2641       return (*fns->const_type) (fhandle);
   2642     case DEBUG_KIND_VOLATILE:
   2643       if (! debug_write_type (info, fns, fhandle, type->u.kvolatile,
   2644 			      (struct debug_name *) NULL))
   2645 	return FALSE;
   2646       return (*fns->volatile_type) (fhandle);
   2647     case DEBUG_KIND_NAMED:
   2648       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
   2649 			       (struct debug_name *) NULL);
   2650     case DEBUG_KIND_TAGGED:
   2651       return debug_write_type (info, fns, fhandle, type->u.knamed->type,
   2652 			       type->u.knamed->name);
   2653     default:
   2654       abort ();
   2655       return FALSE;
   2656     }
   2657 }
   2658 
   2659 /* Write out a class type.  */
   2660 
   2661 static bfd_boolean
   2662 debug_write_class_type (struct debug_handle *info,
   2663 			const struct debug_write_fns *fns, void *fhandle,
   2664 			struct debug_type_s *type, const char *tag)
   2665 {
   2666   unsigned int i;
   2667   unsigned int id;
   2668   struct debug_type_s *vptrbase;
   2669 
   2670   if (type->u.kclass == NULL)
   2671     {
   2672       id = 0;
   2673       vptrbase = NULL;
   2674     }
   2675   else
   2676     {
   2677       if (type->u.kclass->id <= info->base_id)
   2678 	{
   2679 	  if (! debug_set_class_id (info, tag, type))
   2680 	    return FALSE;
   2681 	}
   2682 
   2683       if (info->mark == type->u.kclass->mark)
   2684 	{
   2685 	  /* We are currently outputting this class, or we have
   2686 	     already output it.  This can happen when there are
   2687 	     methods for an anonymous class.  */
   2688 	  assert (type->u.kclass->id > info->base_id);
   2689 	  return (*fns->tag_type) (fhandle, tag, type->u.kclass->id,
   2690 				   type->kind);
   2691 	}
   2692       type->u.kclass->mark = info->mark;
   2693       id = type->u.kclass->id;
   2694 
   2695       vptrbase = type->u.kclass->vptrbase;
   2696       if (vptrbase != NULL && vptrbase != type)
   2697 	{
   2698 	  if (! debug_write_type (info, fns, fhandle, vptrbase,
   2699 				  (struct debug_name *) NULL))
   2700 	    return FALSE;
   2701 	}
   2702     }
   2703 
   2704   if (! (*fns->start_class_type) (fhandle, tag, id,
   2705 				  type->kind == DEBUG_KIND_CLASS,
   2706 				  type->size,
   2707 				  vptrbase != NULL,
   2708 				  vptrbase == type))
   2709     return FALSE;
   2710 
   2711   if (type->u.kclass != NULL)
   2712     {
   2713       if (type->u.kclass->fields != NULL)
   2714 	{
   2715 	  for (i = 0; type->u.kclass->fields[i] != NULL; i++)
   2716 	    {
   2717 	      struct debug_field_s *f;
   2718 
   2719 	      f = type->u.kclass->fields[i];
   2720 	      if (! debug_write_type (info, fns, fhandle, f->type,
   2721 				      (struct debug_name *) NULL))
   2722 		return FALSE;
   2723 	      if (f->static_member)
   2724 		{
   2725 		  if (! (*fns->class_static_member) (fhandle, f->name,
   2726 						     f->u.s.physname,
   2727 						     f->visibility))
   2728 		    return FALSE;
   2729 		}
   2730 	      else
   2731 		{
   2732 		  if (! (*fns->struct_field) (fhandle, f->name, f->u.f.bitpos,
   2733 					      f->u.f.bitsize, f->visibility))
   2734 		    return FALSE;
   2735 		}
   2736 	    }
   2737 	}
   2738 
   2739       if (type->u.kclass->baseclasses != NULL)
   2740 	{
   2741 	  for (i = 0; type->u.kclass->baseclasses[i] != NULL; i++)
   2742 	    {
   2743 	      struct debug_baseclass_s *b;
   2744 
   2745 	      b = type->u.kclass->baseclasses[i];
   2746 	      if (! debug_write_type (info, fns, fhandle, b->type,
   2747 				      (struct debug_name *) NULL))
   2748 		return FALSE;
   2749 	      if (! (*fns->class_baseclass) (fhandle, b->bitpos, b->is_virtual,
   2750 					     b->visibility))
   2751 		return FALSE;
   2752 	    }
   2753 	}
   2754 
   2755       if (type->u.kclass->methods != NULL)
   2756 	{
   2757 	  for (i = 0; type->u.kclass->methods[i] != NULL; i++)
   2758 	    {
   2759 	      struct debug_method_s *m;
   2760 	      unsigned int j;
   2761 
   2762 	      m = type->u.kclass->methods[i];
   2763 	      if (! (*fns->class_start_method) (fhandle, m->name))
   2764 		return FALSE;
   2765 	      for (j = 0; m->variants[j] != NULL; j++)
   2766 		{
   2767 		  struct debug_method_variant_s *v;
   2768 
   2769 		  v = m->variants[j];
   2770 		  if (v->context != NULL)
   2771 		    {
   2772 		      if (! debug_write_type (info, fns, fhandle, v->context,
   2773 					      (struct debug_name *) NULL))
   2774 			return FALSE;
   2775 		    }
   2776 		  if (! debug_write_type (info, fns, fhandle, v->type,
   2777 					  (struct debug_name *) NULL))
   2778 		    return FALSE;
   2779 		  if (v->voffset != VOFFSET_STATIC_METHOD)
   2780 		    {
   2781 		      if (! (*fns->class_method_variant) (fhandle, v->physname,
   2782 							  v->visibility,
   2783 							  v->constp,
   2784 							  v->volatilep,
   2785 							  v->voffset,
   2786 							  v->context != NULL))
   2787 			return FALSE;
   2788 		    }
   2789 		  else
   2790 		    {
   2791 		      if (! (*fns->class_static_method_variant) (fhandle,
   2792 								 v->physname,
   2793 								 v->visibility,
   2794 								 v->constp,
   2795 								 v->volatilep))
   2796 			return FALSE;
   2797 		    }
   2798 		}
   2799 	      if (! (*fns->class_end_method) (fhandle))
   2800 		return FALSE;
   2801 	    }
   2802 	}
   2803     }
   2804 
   2805   return (*fns->end_class_type) (fhandle);
   2806 }
   2807 
   2808 /* Write out information for a function.  */
   2809 
   2810 static bfd_boolean
   2811 debug_write_function (struct debug_handle *info,
   2812 		      const struct debug_write_fns *fns, void *fhandle,
   2813 		      const char *name, enum debug_object_linkage linkage,
   2814 		      struct debug_function *function)
   2815 {
   2816   struct debug_parameter *p;
   2817   struct debug_block *b;
   2818 
   2819   if (! debug_write_linenos (info, fns, fhandle, function->blocks->start))
   2820     return FALSE;
   2821 
   2822   if (! debug_write_type (info, fns, fhandle, function->return_type,
   2823 			  (struct debug_name *) NULL))
   2824     return FALSE;
   2825 
   2826   if (! (*fns->start_function) (fhandle, name,
   2827 				linkage == DEBUG_LINKAGE_GLOBAL))
   2828     return FALSE;
   2829 
   2830   for (p = function->parameters; p != NULL; p = p->next)
   2831     {
   2832       if (! debug_write_type (info, fns, fhandle, p->type,
   2833 			      (struct debug_name *) NULL)
   2834 	  || ! (*fns->function_parameter) (fhandle, p->name, p->kind, p->val))
   2835 	return FALSE;
   2836     }
   2837 
   2838   for (b = function->blocks; b != NULL; b = b->next)
   2839     {
   2840       if (! debug_write_block (info, fns, fhandle, b))
   2841 	return FALSE;
   2842     }
   2843 
   2844   return (*fns->end_function) (fhandle);
   2845 }
   2846 
   2847 /* Write out information for a block.  */
   2848 
   2849 static bfd_boolean
   2850 debug_write_block (struct debug_handle *info,
   2851 		   const struct debug_write_fns *fns, void *fhandle,
   2852 		   struct debug_block *block)
   2853 {
   2854   struct debug_name *n;
   2855   struct debug_block *b;
   2856 
   2857   if (! debug_write_linenos (info, fns, fhandle, block->start))
   2858     return FALSE;
   2859 
   2860   /* I can't see any point to writing out a block with no local
   2861      variables, so we don't bother, except for the top level block.  */
   2862   if (block->locals != NULL || block->parent == NULL)
   2863     {
   2864       if (! (*fns->start_block) (fhandle, block->start))
   2865 	return FALSE;
   2866     }
   2867 
   2868   if (block->locals != NULL)
   2869     {
   2870       for (n = block->locals->list; n != NULL; n = n->next)
   2871 	{
   2872 	  if (! debug_write_name (info, fns, fhandle, n))
   2873 	    return FALSE;
   2874 	}
   2875     }
   2876 
   2877   for (b = block->children; b != NULL; b = b->next)
   2878     {
   2879       if (! debug_write_block (info, fns, fhandle, b))
   2880 	return FALSE;
   2881     }
   2882 
   2883   if (! debug_write_linenos (info, fns, fhandle, block->end))
   2884     return FALSE;
   2885 
   2886   if (block->locals != NULL || block->parent == NULL)
   2887     {
   2888       if (! (*fns->end_block) (fhandle, block->end))
   2889 	return FALSE;
   2890     }
   2891 
   2892   return TRUE;
   2893 }
   2894 
   2895 /* Write out line number information up to ADDRESS.  */
   2896 
   2897 static bfd_boolean
   2898 debug_write_linenos (struct debug_handle *info,
   2899 		     const struct debug_write_fns *fns, void *fhandle,
   2900 		     bfd_vma address)
   2901 {
   2902   while (info->current_write_lineno != NULL)
   2903     {
   2904       struct debug_lineno *l;
   2905 
   2906       l = info->current_write_lineno;
   2907 
   2908       while (info->current_write_lineno_index < DEBUG_LINENO_COUNT)
   2909 	{
   2910 	  if (l->linenos[info->current_write_lineno_index]
   2911 	      == (unsigned long) -1)
   2912 	    break;
   2913 
   2914 	  if (l->addrs[info->current_write_lineno_index] >= address)
   2915 	    return TRUE;
   2916 
   2917 	  if (! (*fns->lineno) (fhandle, l->file->filename,
   2918 				l->linenos[info->current_write_lineno_index],
   2919 				l->addrs[info->current_write_lineno_index]))
   2920 	    return FALSE;
   2921 
   2922 	  ++info->current_write_lineno_index;
   2923 	}
   2924 
   2925       info->current_write_lineno = l->next;
   2926       info->current_write_lineno_index = 0;
   2927     }
   2928 
   2929   return TRUE;
   2930 }
   2931 
   2932 /* Get the ID number for a class.  If during the same call to
   2933    debug_write we find a struct with the same definition with the same
   2934    name, we use the same ID.  This type of things happens because the
   2935    same struct will be defined by multiple compilation units.  */
   2936 
   2937 static bfd_boolean
   2938 debug_set_class_id (struct debug_handle *info, const char *tag,
   2939 		    struct debug_type_s *type)
   2940 {
   2941   struct debug_class_type *c;
   2942   struct debug_class_id *l;
   2943 
   2944   assert (type->kind == DEBUG_KIND_STRUCT
   2945 	  || type->kind == DEBUG_KIND_UNION
   2946 	  || type->kind == DEBUG_KIND_CLASS
   2947 	  || type->kind == DEBUG_KIND_UNION_CLASS);
   2948 
   2949   c = type->u.kclass;
   2950 
   2951   if (c->id > info->base_id)
   2952     return TRUE;
   2953 
   2954   for (l = info->id_list; l != NULL; l = l->next)
   2955     {
   2956       if (l->type->kind != type->kind)
   2957 	continue;
   2958 
   2959       if (tag == NULL)
   2960 	{
   2961 	  if (l->tag != NULL)
   2962 	    continue;
   2963 	}
   2964       else
   2965 	{
   2966 	  if (l->tag == NULL
   2967 	      || l->tag[0] != tag[0]
   2968 	      || strcmp (l->tag, tag) != 0)
   2969 	    continue;
   2970 	}
   2971 
   2972       if (debug_type_samep (info, l->type, type))
   2973 	{
   2974 	  c->id = l->type->u.kclass->id;
   2975 	  return TRUE;
   2976 	}
   2977     }
   2978 
   2979   /* There are no identical types.  Use a new ID, and add it to the
   2980      list.  */
   2981   ++info->class_id;
   2982   c->id = info->class_id;
   2983 
   2984   l = (struct debug_class_id *) xmalloc (sizeof *l);
   2985   memset (l, 0, sizeof *l);
   2986 
   2987   l->type = type;
   2988   l->tag = tag;
   2989 
   2990   l->next = info->id_list;
   2991   info->id_list = l;
   2992 
   2993   return TRUE;
   2994 }
   2995 
   2996 /* See if two types are the same.  At this point, we don't care about
   2997    tags and the like.  */
   2998 
   2999 static bfd_boolean
   3000 debug_type_samep (struct debug_handle *info, struct debug_type_s *t1,
   3001 		  struct debug_type_s *t2)
   3002 {
   3003   struct debug_type_compare_list *l;
   3004   struct debug_type_compare_list top;
   3005   bfd_boolean ret;
   3006 
   3007   if (t1 == NULL)
   3008     return t2 == NULL;
   3009   if (t2 == NULL)
   3010     return FALSE;
   3011 
   3012   while (t1->kind == DEBUG_KIND_INDIRECT)
   3013     {
   3014       t1 = *t1->u.kindirect->slot;
   3015       if (t1 == NULL)
   3016 	return FALSE;
   3017     }
   3018   while (t2->kind == DEBUG_KIND_INDIRECT)
   3019     {
   3020       t2 = *t2->u.kindirect->slot;
   3021       if (t2 == NULL)
   3022 	return FALSE;
   3023     }
   3024 
   3025   if (t1 == t2)
   3026     return TRUE;
   3027 
   3028   /* As a special case, permit a typedef to match a tag, since C++
   3029      debugging output will sometimes add a typedef where C debugging
   3030      output will not.  */
   3031   if (t1->kind == DEBUG_KIND_NAMED
   3032       && t2->kind == DEBUG_KIND_TAGGED)
   3033     return debug_type_samep (info, t1->u.knamed->type, t2);
   3034   else if (t1->kind == DEBUG_KIND_TAGGED
   3035 	   && t2->kind == DEBUG_KIND_NAMED)
   3036     return debug_type_samep (info, t1, t2->u.knamed->type);
   3037 
   3038   if (t1->kind != t2->kind
   3039       || t1->size != t2->size)
   3040     return FALSE;
   3041 
   3042   /* Get rid of the trivial cases first.  */
   3043   switch (t1->kind)
   3044     {
   3045     default:
   3046       break;
   3047     case DEBUG_KIND_VOID:
   3048     case DEBUG_KIND_FLOAT:
   3049     case DEBUG_KIND_COMPLEX:
   3050     case DEBUG_KIND_BOOL:
   3051       return TRUE;
   3052     case DEBUG_KIND_INT:
   3053       return t1->u.kint == t2->u.kint;
   3054     }
   3055 
   3056   /* We have to avoid an infinite recursion.  We do this by keeping a
   3057      list of types which we are comparing.  We just keep the list on
   3058      the stack.  If we encounter a pair of types we are currently
   3059      comparing, we just assume that they are equal.  */
   3060   for (l = info->compare_list; l != NULL; l = l->next)
   3061     {
   3062       if (l->t1 == t1 && l->t2 == t2)
   3063 	return TRUE;
   3064     }
   3065 
   3066   top.t1 = t1;
   3067   top.t2 = t2;
   3068   top.next = info->compare_list;
   3069   info->compare_list = &top;
   3070 
   3071   switch (t1->kind)
   3072     {
   3073     default:
   3074       abort ();
   3075       ret = FALSE;
   3076       break;
   3077 
   3078     case DEBUG_KIND_STRUCT:
   3079     case DEBUG_KIND_UNION:
   3080     case DEBUG_KIND_CLASS:
   3081     case DEBUG_KIND_UNION_CLASS:
   3082       if (t1->u.kclass == NULL)
   3083 	ret = t2->u.kclass == NULL;
   3084       else if (t2->u.kclass == NULL)
   3085 	ret = FALSE;
   3086       else if (t1->u.kclass->id > info->base_id
   3087 	       && t1->u.kclass->id == t2->u.kclass->id)
   3088 	ret = TRUE;
   3089       else
   3090 	ret = debug_class_type_samep (info, t1, t2);
   3091       break;
   3092 
   3093     case DEBUG_KIND_ENUM:
   3094       if (t1->u.kenum == NULL)
   3095 	ret = t2->u.kenum == NULL;
   3096       else if (t2->u.kenum == NULL)
   3097 	ret = FALSE;
   3098       else
   3099 	{
   3100 	  const char **pn1, **pn2;
   3101 	  bfd_signed_vma *pv1, *pv2;
   3102 
   3103 	  pn1 = t1->u.kenum->names;
   3104 	  pn2 = t2->u.kenum->names;
   3105 	  pv1 = t1->u.kenum->values;
   3106 	  pv2 = t2->u.kenum->values;
   3107 	  while (*pn1 != NULL && *pn2 != NULL)
   3108 	    {
   3109 	      if (**pn1 != **pn2
   3110 		  || *pv1 != *pv2
   3111 		  || strcmp (*pn1, *pn2) != 0)
   3112 		break;
   3113 	      ++pn1;
   3114 	      ++pn2;
   3115 	      ++pv1;
   3116 	      ++pv2;
   3117 	    }
   3118 	  ret = *pn1 == NULL && *pn2 == NULL;
   3119 	}
   3120       break;
   3121 
   3122     case DEBUG_KIND_POINTER:
   3123       ret = debug_type_samep (info, t1->u.kpointer, t2->u.kpointer);
   3124       break;
   3125 
   3126     case DEBUG_KIND_FUNCTION:
   3127       if (t1->u.kfunction->varargs != t2->u.kfunction->varargs
   3128 	  || ! debug_type_samep (info, t1->u.kfunction->return_type,
   3129 				 t2->u.kfunction->return_type)
   3130 	  || ((t1->u.kfunction->arg_types == NULL)
   3131 	      != (t2->u.kfunction->arg_types == NULL)))
   3132 	ret = FALSE;
   3133       else if (t1->u.kfunction->arg_types == NULL)
   3134 	ret = TRUE;
   3135       else
   3136 	{
   3137 	  struct debug_type_s **a1, **a2;
   3138 
   3139 	  a1 = t1->u.kfunction->arg_types;
   3140 	  a2 = t2->u.kfunction->arg_types;
   3141 	  while (*a1 != NULL && *a2 != NULL)
   3142 	    {
   3143 	      if (! debug_type_samep (info, *a1, *a2))
   3144 		break;
   3145 	      ++a1;
   3146 	      ++a2;
   3147 	    }
   3148 	  ret = *a1 == NULL && *a2 == NULL;
   3149 	}
   3150       break;
   3151 
   3152     case DEBUG_KIND_REFERENCE:
   3153       ret = debug_type_samep (info, t1->u.kreference, t2->u.kreference);
   3154       break;
   3155 
   3156     case DEBUG_KIND_RANGE:
   3157       ret = (t1->u.krange->lower == t2->u.krange->lower
   3158 	     && t1->u.krange->upper == t2->u.krange->upper
   3159 	     && debug_type_samep (info, t1->u.krange->type,
   3160 				  t2->u.krange->type));
   3161       break;
   3162 
   3163     case DEBUG_KIND_ARRAY:
   3164       ret = (t1->u.karray->lower == t2->u.karray->lower
   3165 	     && t1->u.karray->upper == t2->u.karray->upper
   3166 	     && t1->u.karray->stringp == t2->u.karray->stringp
   3167 	     && debug_type_samep (info, t1->u.karray->element_type,
   3168 				  t2->u.karray->element_type));
   3169       break;
   3170 
   3171     case DEBUG_KIND_SET:
   3172       ret = (t1->u.kset->bitstringp == t2->u.kset->bitstringp
   3173 	     && debug_type_samep (info, t1->u.kset->type, t2->u.kset->type));
   3174       break;
   3175 
   3176     case DEBUG_KIND_OFFSET:
   3177       ret = (debug_type_samep (info, t1->u.koffset->base_type,
   3178 			       t2->u.koffset->base_type)
   3179 	     && debug_type_samep (info, t1->u.koffset->target_type,
   3180 				  t2->u.koffset->target_type));
   3181       break;
   3182 
   3183     case DEBUG_KIND_METHOD:
   3184       if (t1->u.kmethod->varargs != t2->u.kmethod->varargs
   3185 	  || ! debug_type_samep (info, t1->u.kmethod->return_type,
   3186 				 t2->u.kmethod->return_type)
   3187 	  || ! debug_type_samep (info, t1->u.kmethod->domain_type,
   3188 				 t2->u.kmethod->domain_type)
   3189 	  || ((t1->u.kmethod->arg_types == NULL)
   3190 	      != (t2->u.kmethod->arg_types == NULL)))
   3191 	ret = FALSE;
   3192       else if (t1->u.kmethod->arg_types == NULL)
   3193 	ret = TRUE;
   3194       else
   3195 	{
   3196 	  struct debug_type_s **a1, **a2;
   3197 
   3198 	  a1 = t1->u.kmethod->arg_types;
   3199 	  a2 = t2->u.kmethod->arg_types;
   3200 	  while (*a1 != NULL && *a2 != NULL)
   3201 	    {
   3202 	      if (! debug_type_samep (info, *a1, *a2))
   3203 		break;
   3204 	      ++a1;
   3205 	      ++a2;
   3206 	    }
   3207 	  ret = *a1 == NULL && *a2 == NULL;
   3208 	}
   3209       break;
   3210 
   3211     case DEBUG_KIND_CONST:
   3212       ret = debug_type_samep (info, t1->u.kconst, t2->u.kconst);
   3213       break;
   3214 
   3215     case DEBUG_KIND_VOLATILE:
   3216       ret = debug_type_samep (info, t1->u.kvolatile, t2->u.kvolatile);
   3217       break;
   3218 
   3219     case DEBUG_KIND_NAMED:
   3220     case DEBUG_KIND_TAGGED:
   3221       ret = (strcmp (t1->u.knamed->name->name, t2->u.knamed->name->name) == 0
   3222 	     && debug_type_samep (info, t1->u.knamed->type,
   3223 				  t2->u.knamed->type));
   3224       break;
   3225     }
   3226 
   3227   info->compare_list = top.next;
   3228 
   3229   return ret;
   3230 }
   3231 
   3232 /* See if two classes are the same.  This is a subroutine of
   3233    debug_type_samep.  */
   3234 
   3235 static bfd_boolean
   3236 debug_class_type_samep (struct debug_handle *info, struct debug_type_s *t1,
   3237 			struct debug_type_s *t2)
   3238 {
   3239   struct debug_class_type *c1, *c2;
   3240 
   3241   c1 = t1->u.kclass;
   3242   c2 = t2->u.kclass;
   3243 
   3244   if ((c1->fields == NULL) != (c2->fields == NULL)
   3245       || (c1->baseclasses == NULL) != (c2->baseclasses == NULL)
   3246       || (c1->methods == NULL) != (c2->methods == NULL)
   3247       || (c1->vptrbase == NULL) != (c2->vptrbase == NULL))
   3248     return FALSE;
   3249 
   3250   if (c1->fields != NULL)
   3251     {
   3252       struct debug_field_s **pf1, **pf2;
   3253 
   3254       for (pf1 = c1->fields, pf2 = c2->fields;
   3255 	   *pf1 != NULL && *pf2 != NULL;
   3256 	   pf1++, pf2++)
   3257 	{
   3258 	  struct debug_field_s *f1, *f2;
   3259 
   3260 	  f1 = *pf1;
   3261 	  f2 = *pf2;
   3262 	  if (f1->name[0] != f2->name[0]
   3263 	      || f1->visibility != f2->visibility
   3264 	      || f1->static_member != f2->static_member)
   3265 	    return FALSE;
   3266 	  if (f1->static_member)
   3267 	    {
   3268 	      if (strcmp (f1->u.s.physname, f2->u.s.physname) != 0)
   3269 		return FALSE;
   3270 	    }
   3271 	  else
   3272 	    {
   3273 	      if (f1->u.f.bitpos != f2->u.f.bitpos
   3274 		  || f1->u.f.bitsize != f2->u.f.bitsize)
   3275 		return FALSE;
   3276 	    }
   3277 	  /* We do the checks which require function calls last.  We
   3278              don't require that the types of fields have the same
   3279              names, since that sometimes fails in the presence of
   3280              typedefs and we really don't care.  */
   3281 	  if (strcmp (f1->name, f2->name) != 0
   3282 	      || ! debug_type_samep (info,
   3283 				     debug_get_real_type ((void *) info,
   3284 							  f1->type, NULL),
   3285 				     debug_get_real_type ((void *) info,
   3286 							  f2->type, NULL)))
   3287 	    return FALSE;
   3288 	}
   3289       if (*pf1 != NULL || *pf2 != NULL)
   3290 	return FALSE;
   3291     }
   3292 
   3293   if (c1->vptrbase != NULL)
   3294     {
   3295       if (! debug_type_samep (info, c1->vptrbase, c2->vptrbase))
   3296 	return FALSE;
   3297     }
   3298 
   3299   if (c1->baseclasses != NULL)
   3300     {
   3301       struct debug_baseclass_s **pb1, **pb2;
   3302 
   3303       for (pb1 = c1->baseclasses, pb2 = c2->baseclasses;
   3304 	   *pb1 != NULL && *pb2 != NULL;
   3305 	   ++pb1, ++pb2)
   3306 	{
   3307 	  struct debug_baseclass_s *b1, *b2;
   3308 
   3309 	  b1 = *pb1;
   3310 	  b2 = *pb2;
   3311 	  if (b1->bitpos != b2->bitpos
   3312 	      || b1->is_virtual != b2->is_virtual
   3313 	      || b1->visibility != b2->visibility
   3314 	      || ! debug_type_samep (info, b1->type, b2->type))
   3315 	    return FALSE;
   3316 	}
   3317       if (*pb1 != NULL || *pb2 != NULL)
   3318 	return FALSE;
   3319     }
   3320 
   3321   if (c1->methods != NULL)
   3322     {
   3323       struct debug_method_s **pm1, **pm2;
   3324 
   3325       for (pm1 = c1->methods, pm2 = c2->methods;
   3326 	   *pm1 != NULL && *pm2 != NULL;
   3327 	   ++pm1, ++pm2)
   3328 	{
   3329 	  struct debug_method_s *m1, *m2;
   3330 
   3331 	  m1 = *pm1;
   3332 	  m2 = *pm2;
   3333 	  if (m1->name[0] != m2->name[0]
   3334 	      || strcmp (m1->name, m2->name) != 0
   3335 	      || (m1->variants == NULL) != (m2->variants == NULL))
   3336 	    return FALSE;
   3337 	  if (m1->variants == NULL)
   3338 	    {
   3339 	      struct debug_method_variant_s **pv1, **pv2;
   3340 
   3341 	      for (pv1 = m1->variants, pv2 = m2->variants;
   3342 		   *pv1 != NULL && *pv2 != NULL;
   3343 		   ++pv1, ++pv2)
   3344 		{
   3345 		  struct debug_method_variant_s *v1, *v2;
   3346 
   3347 		  v1 = *pv1;
   3348 		  v2 = *pv2;
   3349 		  if (v1->physname[0] != v2->physname[0]
   3350 		      || v1->visibility != v2->visibility
   3351 		      || v1->constp != v2->constp
   3352 		      || v1->volatilep != v2->volatilep
   3353 		      || v1->voffset != v2->voffset
   3354 		      || (v1->context == NULL) != (v2->context == NULL)
   3355 		      || strcmp (v1->physname, v2->physname) != 0
   3356 		      || ! debug_type_samep (info, v1->type, v2->type))
   3357 		    return FALSE;
   3358 		  if (v1->context != NULL)
   3359 		    {
   3360 		      if (! debug_type_samep (info, v1->context,
   3361 					      v2->context))
   3362 			return FALSE;
   3363 		    }
   3364 		}
   3365 	      if (*pv1 != NULL || *pv2 != NULL)
   3366 		return FALSE;
   3367 	    }
   3368 	}
   3369       if (*pm1 != NULL || *pm2 != NULL)
   3370 	return FALSE;
   3371     }
   3372 
   3373   return TRUE;
   3374 }
   3375