* ieee.c (struct ieee_handle): Add fields complex_float_index and
[platform/upstream/binutils.git] / binutils / ieee.c
1 /* ieee.c -- Read and write IEEE-695 debugging information.
2    Copyright (C) 1996 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 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., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* This file reads and writes IEEE-695 debugging information.  */
23
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "bfd.h"
28 #include "ieee.h"
29 #include "bucomm.h"
30 #include "libiberty.h"
31 #include "debug.h"
32 #include "budbg.h"
33
34 /* This structure holds an entry on the block stack.  */
35
36 struct ieee_block
37 {
38   /* The kind of block.  */
39   int kind;
40   /* The source file name, for a BB5 block.  */
41   const char *filename;
42   /* The index of the function type, for a BB4 or BB6 block.  */
43   unsigned int fnindx;
44   /* True if this function is being skipped.  */
45   boolean skip;
46 };
47
48 /* This structure is the block stack.  */
49
50 #define BLOCKSTACK_SIZE (16)
51
52 struct ieee_blockstack
53 {
54   /* The stack pointer.  */
55   struct ieee_block *bsp;
56   /* The stack.  */
57   struct ieee_block stack[BLOCKSTACK_SIZE];
58 };
59
60 /* This structure holds information for a variable.  */
61
62 struct ieee_var
63 {
64   /* Start of name.  */
65   const char *name;
66   /* Length of name.  */
67   unsigned long namlen;
68   /* Type.  */
69   debug_type type;
70   /* Slot if we make an indirect type.  */
71   debug_type *pslot;
72   /* Kind of variable or function.  */
73   enum
74     {
75       IEEE_UNKNOWN,
76       IEEE_EXTERNAL,
77       IEEE_GLOBAL,
78       IEEE_STATIC,
79       IEEE_LOCAL,
80       IEEE_FUNCTION
81     } kind;
82 };
83
84 /* This structure holds all the variables.  */
85
86 struct ieee_vars
87 {
88   /* Number of slots allocated.  */
89   unsigned int alloc;
90   /* Variables.  */
91   struct ieee_var *vars;
92 };
93
94 /* This structure holds information for a type.  We need this because
95    we don't want to represent bitfields as real types.  */
96
97 struct ieee_type
98 {
99   /* Type.  */
100   debug_type type;
101   /* Slot if this is type is referenced before it is defined.  */
102   debug_type *pslot;
103   /* Slots for arguments if we make indirect types for them.  */
104   debug_type *arg_slots;
105   /* If this is a bitfield, this is the size in bits.  If this is not
106      a bitfield, this is zero.  */
107   unsigned long bitsize;
108 };
109
110 /* This structure holds all the type information.  */
111
112 struct ieee_types
113 {
114   /* Number of slots allocated.  */
115   unsigned int alloc;
116   /* Types.  */
117   struct ieee_type *types;
118   /* Builtin types.  */
119 #define BUILTIN_TYPE_COUNT (60)
120   debug_type builtins[BUILTIN_TYPE_COUNT];
121 };
122
123 /* This structure holds a linked last of structs with their tag names,
124    so that we can convert them to C++ classes if necessary.  */
125
126 struct ieee_tag
127 {
128   /* Next tag.  */
129   struct ieee_tag *next;
130   /* This tag name.  */
131   const char *name;
132   /* The type of the tag.  */
133   debug_type type;
134   /* The tagged type is an indirect type pointing at this slot.  */
135   debug_type slot;
136   /* This is an array of slots used when a field type is converted
137      into a indirect type, in case it needs to be later converted into
138      a reference type.  */
139   debug_type *fslots;
140 };
141
142 /* This structure holds the information we pass around to the parsing
143    functions.  */
144
145 struct ieee_info
146 {
147   /* The debugging handle.  */
148   PTR dhandle;
149   /* The BFD.  */
150   bfd *abfd;
151   /* The start of the bytes to be parsed.  */
152   const bfd_byte *bytes;
153   /* The end of the bytes to be parsed.  */
154   const bfd_byte *pend;
155   /* The block stack.  */
156   struct ieee_blockstack blockstack;
157   /* The variables.  */
158   struct ieee_vars vars;
159   /* The types.  */
160   struct ieee_types types;
161   /* The list of tagged structs.  */
162   struct ieee_tag *tags;
163 };
164
165 /* Basic builtin types, not including the pointers.  */
166
167 enum builtin_types
168 {
169   builtin_unknown = 0,
170   builtin_void = 1,
171   builtin_signed_char = 2,
172   builtin_unsigned_char = 3,
173   builtin_signed_short_int = 4,
174   builtin_unsigned_short_int = 5,
175   builtin_signed_long = 6,
176   builtin_unsigned_long = 7,
177   builtin_signed_long_long = 8,
178   builtin_unsigned_long_long = 9,
179   builtin_float = 10,
180   builtin_double = 11,
181   builtin_long_double = 12,
182   builtin_long_long_double = 13,
183   builtin_quoted_string = 14,
184   builtin_instruction_address = 15,
185   builtin_int = 16,
186   builtin_unsigned = 17,
187   builtin_unsigned_int = 18,
188   builtin_char = 19,
189   builtin_long = 20,
190   builtin_short = 21,
191   builtin_unsigned_short = 22,
192   builtin_short_int = 23,
193   builtin_signed_short = 24,
194   builtin_bcd_float = 25
195 };
196
197 /* These are the values found in the derivation flags of a 'b'
198    component record of a 'T' type extension record in a C++ pmisc
199    record.  These are bitmasks.  */
200
201 /* Set for a private base class, clear for a public base class.
202    Protected base classes are not supported.  */
203 #define BASEFLAGS_PRIVATE (0x1)
204 /* Set for a virtual base class.  */
205 #define BASEFLAGS_VIRTUAL (0x2)
206 /* Set for a friend class, clear for a base class.  */
207 #define BASEFLAGS_FRIEND (0x10)
208
209 /* These are the values found in the specs flags of a 'd', 'm', or 'v'
210    component record of a 'T' type extension record in a C++ pmisc
211    record.  The same flags are used for a 'M' record in a C++ pmisc
212    record.  */
213
214 /* The lower two bits hold visibility information.  */
215 #define CXXFLAGS_VISIBILITY (0x3)
216 /* This value in the lower two bits indicates a public member.  */
217 #define CXXFLAGS_VISIBILITY_PUBLIC (0x0)
218 /* This value in the lower two bits indicates a private member.  */
219 #define CXXFLAGS_VISIBILITY_PRIVATE (0x1)
220 /* This value in the lower two bits indicates a protected member.  */
221 #define CXXFLAGS_VISIBILITY_PROTECTED (0x2)
222 /* Set for a static member.  */
223 #define CXXFLAGS_STATIC (0x4)
224 /* Set for a virtual override.  */
225 #define CXXFLAGS_OVERRIDE (0x8)
226 /* Set for a friend function.  */
227 #define CXXFLAGS_FRIEND (0x10)
228 /* Set for a const function.  */
229 #define CXXFLAGS_CONST (0x20)
230 /* Set for a volatile function.  */
231 #define CXXFLAGS_VOLATILE (0x40)
232 /* Set for an overloaded function.  */
233 #define CXXFLAGS_OVERLOADED (0x80)
234 /* Set for an operator function.  */
235 #define CXXFLAGS_OPERATOR (0x100)
236 /* Set for a constructor or destructor.  */
237 #define CXXFLAGS_CTORDTOR (0x400)
238 /* Set for a constructor.  */
239 #define CXXFLAGS_CTOR (0x200)
240 /* Set for an inline function.  */
241 #define CXXFLAGS_INLINE (0x800)
242
243 /* Local functions.  */
244
245 static void ieee_error
246   PARAMS ((struct ieee_info *, const bfd_byte *, const char *));
247 static void ieee_eof PARAMS ((struct ieee_info *));
248 static char *savestring PARAMS ((const char *, unsigned long));
249 static boolean ieee_read_number
250   PARAMS ((struct ieee_info *, const bfd_byte **, bfd_vma *));
251 static boolean ieee_read_optional_number
252   PARAMS ((struct ieee_info *, const bfd_byte **, bfd_vma *, boolean *));
253 static boolean ieee_read_id
254   PARAMS ((struct ieee_info *, const bfd_byte **, const char **,
255            unsigned long *));
256 static boolean ieee_read_optional_id
257   PARAMS ((struct ieee_info *, const bfd_byte **, const char **,
258            unsigned long *, boolean *));
259 static boolean ieee_read_expression
260   PARAMS ((struct ieee_info *, const bfd_byte **, bfd_vma *));
261 static debug_type ieee_builtin_type
262   PARAMS ((struct ieee_info *, const bfd_byte *, unsigned int));
263 static boolean ieee_alloc_type
264   PARAMS ((struct ieee_info *, unsigned int, boolean));
265 static boolean ieee_read_type_index
266   PARAMS ((struct ieee_info *, const bfd_byte **, debug_type *));
267 static int ieee_regno_to_genreg PARAMS ((bfd *, int));
268 static int ieee_genreg_to_regno PARAMS ((bfd *, int));
269 static boolean parse_ieee_bb PARAMS ((struct ieee_info *, const bfd_byte **));
270 static boolean parse_ieee_be PARAMS ((struct ieee_info *, const bfd_byte **));
271 static boolean parse_ieee_nn PARAMS ((struct ieee_info *, const bfd_byte **));
272 static boolean parse_ieee_ty PARAMS ((struct ieee_info *, const bfd_byte **));
273 static boolean parse_ieee_atn PARAMS ((struct ieee_info *, const bfd_byte **));
274 static boolean ieee_read_cxx_misc
275   PARAMS ((struct ieee_info *, const bfd_byte **, unsigned long));
276 static boolean ieee_read_cxx_class
277   PARAMS ((struct ieee_info *, const bfd_byte **, unsigned long));
278 static boolean ieee_read_cxx_defaults
279   PARAMS ((struct ieee_info *, const bfd_byte **, unsigned long));
280 static boolean ieee_read_reference
281   PARAMS ((struct ieee_info *, const bfd_byte **));
282 static boolean ieee_require_asn
283   PARAMS ((struct ieee_info *, const bfd_byte **, bfd_vma *));
284 static boolean ieee_require_atn65
285   PARAMS ((struct ieee_info *, const bfd_byte **, const char **,
286            unsigned long *));
287
288 /* Report an error in the IEEE debugging information.  */
289
290 static void
291 ieee_error (info, p, s)
292      struct ieee_info *info;
293      const bfd_byte *p;
294      const char *s;
295 {
296   if (p != NULL)
297     fprintf (stderr, "%s: 0x%lx: %s (0x%x)\n", bfd_get_filename (info->abfd),
298              (unsigned long) (p - info->bytes), s, *p);
299   else
300     fprintf (stderr, "%s: %s\n", bfd_get_filename (info->abfd), s);
301 }
302
303 /* Report an unexpected EOF in the IEEE debugging information.  */
304
305 static void
306 ieee_eof (info)
307      struct ieee_info *info;
308 {
309   ieee_error (info, (const bfd_byte *) NULL,
310               "unexpected end of debugging information");
311 }
312
313 /* Save a string in memory.  */
314
315 static char *
316 savestring (start, len)
317      const char *start;
318      unsigned long len;
319 {
320   char *ret;
321
322   ret = (char *) xmalloc (len + 1);
323   memcpy (ret, start, len);
324   ret[len] = '\0';
325   return ret;
326 }
327
328 /* Read a number which must be present in an IEEE file.  */
329
330 static boolean
331 ieee_read_number (info, pp, pv)
332      struct ieee_info *info;
333      const bfd_byte **pp;
334      bfd_vma *pv;
335 {
336   return ieee_read_optional_number (info, pp, pv, (boolean *) NULL);
337 }
338
339 /* Read a number in an IEEE file.  If ppresent is not NULL, the number
340    need not be there. */
341
342 static boolean
343 ieee_read_optional_number (info, pp, pv, ppresent)
344      struct ieee_info *info;
345      const bfd_byte **pp;
346      bfd_vma *pv;
347      boolean *ppresent;
348 {
349   ieee_record_enum_type b;
350
351   if (*pp >= info->pend)
352     {
353       if (ppresent != NULL)
354         {
355           *ppresent = false;
356           return true;
357         }
358       ieee_eof (info);
359       return false;
360     }
361
362   b = (ieee_record_enum_type) **pp;
363   ++*pp;
364
365   if (b <= ieee_number_end_enum)
366     {
367       *pv = (bfd_vma) b;
368       if (ppresent != NULL)
369         *ppresent = true;
370       return true;
371     }
372
373   if (b >= ieee_number_repeat_start_enum && b <= ieee_number_repeat_end_enum)
374     {
375       unsigned int i;
376
377       i = (int) b - (int) ieee_number_repeat_start_enum;
378       if (*pp + i - 1 >= info->pend)
379         {
380           ieee_eof (info);
381           return false;
382         }
383
384       *pv = 0;
385       for (; i > 0; i--)
386         {
387           *pv <<= 8;
388           *pv += **pp;
389           ++*pp;
390         }
391
392       if (ppresent != NULL)
393         *ppresent = true;
394
395       return true;
396     }
397
398   if (ppresent != NULL)
399     {
400       --*pp;
401       *ppresent = false;
402       return true;
403     }
404
405   ieee_error (info, *pp - 1, "invalid number");
406   return false;  
407 }
408
409 /* Read a required string from an IEEE file.  */
410
411 static boolean
412 ieee_read_id (info, pp, pname, pnamlen)
413      struct ieee_info *info;
414      const bfd_byte **pp;
415      const char **pname;
416      unsigned long *pnamlen;
417 {
418   return ieee_read_optional_id (info, pp, pname, pnamlen, (boolean *) NULL);
419 }
420
421 /* Read a string from an IEEE file.  If ppresent is not NULL, the
422    string is optional.  */
423
424 static boolean
425 ieee_read_optional_id (info, pp, pname, pnamlen, ppresent)
426      struct ieee_info *info;
427      const bfd_byte **pp;
428      const char **pname;
429      unsigned long *pnamlen;
430      boolean *ppresent;
431 {
432   bfd_byte b;
433   unsigned long len;
434
435   if (*pp >= info->pend)
436     {
437       ieee_eof (info);
438       return false;
439     }
440
441   b = **pp;
442   ++*pp;
443
444   if (b <= 0x7f)
445     len = b;
446   else if ((ieee_record_enum_type) b == ieee_extension_length_1_enum)
447     {
448       len = **pp;
449       ++*pp;
450     }
451   else if ((ieee_record_enum_type) b == ieee_extension_length_2_enum)
452     {
453       len = (**pp << 8) + (*pp)[1];
454       *pp += 2;
455     }
456   else
457     {
458       if (ppresent != NULL)
459         {
460           --*pp;
461           *ppresent = false;
462           return true;
463         }
464       ieee_error (info, *pp - 1, "invalid string length");
465       return false;
466     }
467
468   if ((unsigned long) (info->pend - *pp) < len)
469     {
470       ieee_eof (info);
471       return false;
472     }
473
474   *pname = (const char *) *pp;
475   *pnamlen = len;
476   *pp += len;
477
478   if (ppresent != NULL)
479     *ppresent = true;
480
481   return true;
482 }
483
484 /* Read an expression from an IEEE file.  Since this code is only used
485    to parse debugging information, I haven't bothered to write a full
486    blown IEEE expression parser.  I've only thrown in the things I've
487    seen in debugging information.  This can be easily extended if
488    necessary.  */
489
490 static boolean
491 ieee_read_expression (info, pp, pv)
492      struct ieee_info *info;
493      const bfd_byte **pp;
494      bfd_vma *pv;
495 {
496   const bfd_byte *expr_start;
497 #define EXPR_STACK_SIZE (10)
498   bfd_vma expr_stack[EXPR_STACK_SIZE];
499   bfd_vma *esp;
500
501   expr_start = *pp;
502
503   esp = expr_stack;
504
505   while (1)
506     {
507       const bfd_byte *start;
508       bfd_vma val;
509       boolean present;
510       ieee_record_enum_type c;
511
512       start = *pp;
513
514       if (! ieee_read_optional_number (info, pp, &val, &present))
515         return false;
516
517       if (present)
518         {
519           if (esp - expr_stack >= EXPR_STACK_SIZE)
520             {
521               ieee_error (info, start, "expression stack overflow");
522               return false;
523             }
524           *esp++ = val;
525           continue;
526         }
527
528       c = (ieee_record_enum_type) **pp;
529
530       if (c >= ieee_module_beginning_enum)
531         break;
532
533       ++*pp;
534
535       if (c == ieee_comma)
536         break;
537
538       switch (c)
539         {
540         default:
541           ieee_error (info, start, "unsupported IEEE expression operator");
542           break;
543
544         case ieee_variable_R_enum:
545           {
546             bfd_vma indx;
547             asection *s;
548
549             if (! ieee_read_number (info, pp, &indx))
550               return false;
551             for (s = info->abfd->sections; s != NULL; s = s->next)
552               if ((bfd_vma) s->target_index == indx)
553                 break;
554             if (s == NULL)
555               {
556                 ieee_error (info, start, "unknown section");
557                 return false;
558               }
559             
560             if (esp - expr_stack >= EXPR_STACK_SIZE)
561               {
562                 ieee_error (info, start, "expression stack overflow");
563                 return false;
564               }
565
566             *esp++ = bfd_get_section_vma (info->abfd, s);
567           }
568           break;
569
570         case ieee_function_plus_enum:
571         case ieee_function_minus_enum:
572           {
573             bfd_vma v1, v2;
574
575             if (esp - expr_stack < 2)
576               {
577                 ieee_error (info, start, "expression stack underflow");
578                 return false;
579               }
580
581             v1 = *--esp;
582             v2 = *--esp;
583             *esp++ = v1 + v2;
584           }
585           break;
586         }
587     }
588
589   if (esp - 1 != expr_stack)
590     {
591       ieee_error (info, expr_start, "expression stack mismatch");
592       return false;
593     }
594
595   *pv = *--esp;
596
597   return true;
598 }
599
600 /* Return an IEEE builtin type.  */
601
602 static debug_type
603 ieee_builtin_type (info, p, indx)
604      struct ieee_info *info;
605      const bfd_byte *p;
606      unsigned int indx;
607 {
608   PTR dhandle;
609   debug_type type;
610   const char *name;
611
612   if (indx < BUILTIN_TYPE_COUNT
613       && info->types.builtins[indx] != DEBUG_TYPE_NULL)
614     return info->types.builtins[indx];
615
616   dhandle = info->dhandle;
617
618   if (indx >= 32 && indx < 64)
619     {
620       type = debug_make_pointer_type (dhandle,
621                                       ieee_builtin_type (info, p, indx - 32));
622       assert (indx < BUILTIN_TYPE_COUNT);
623       info->types.builtins[indx] = type;
624       return type;
625     }
626
627   switch ((enum builtin_types) indx)
628     {
629     default:
630       ieee_error (info, p, "unknown builtin type");
631       return NULL;
632
633     case builtin_unknown:
634       type = debug_make_void_type (dhandle);
635       name = NULL;
636       break;
637
638     case builtin_void:
639       type = debug_make_void_type (dhandle);
640       name = "void";
641       break;
642
643     case builtin_signed_char:
644       type = debug_make_int_type (dhandle, 1, false);
645       name = "signed char";
646       break;
647
648     case builtin_unsigned_char:
649       type = debug_make_int_type (dhandle, 1, true);
650       name = "unsigned char";
651       break;
652
653     case builtin_signed_short_int:
654       type = debug_make_int_type (dhandle, 2, false);
655       name = "signed short int";
656       break;
657
658     case builtin_unsigned_short_int:
659       type = debug_make_int_type (dhandle, 2, true);
660       name = "unsigned short int";
661       break;
662
663     case builtin_signed_long:
664       type = debug_make_int_type (dhandle, 4, false);
665       name = "signed long";
666       break;
667
668     case builtin_unsigned_long:
669       type = debug_make_int_type (dhandle, 4, true);
670       name = "unsigned long";
671       break;
672
673     case builtin_signed_long_long:
674       type = debug_make_int_type (dhandle, 8, false);
675       name = "signed long long";
676       break;
677
678     case builtin_unsigned_long_long:
679       type = debug_make_int_type (dhandle, 8, true);
680       name = "unsigned long long";
681       break;
682
683     case builtin_float:
684       type = debug_make_float_type (dhandle, 4);
685       name = "float";
686       break;
687
688     case builtin_double:
689       type = debug_make_float_type (dhandle, 8);
690       name = "double";
691       break;
692
693     case builtin_long_double:
694       /* FIXME: The size for this type should depend upon the
695          processor.  */
696       type = debug_make_float_type (dhandle, 12);
697       name = "long double";
698       break;
699
700     case builtin_long_long_double:
701       type = debug_make_float_type (dhandle, 16);
702       name = "long long double";
703       break;
704
705     case builtin_quoted_string:
706       type = debug_make_array_type (dhandle,
707                                     ieee_builtin_type (info, p,
708                                                        ((unsigned int)
709                                                         builtin_char)),
710                                     ieee_builtin_type (info, p,
711                                                        ((unsigned int)
712                                                         builtin_int)),
713                                     0, -1, true);
714       name = "QUOTED STRING";
715       break;
716
717     case builtin_instruction_address:
718       /* FIXME: This should be a code address.  */
719       type = debug_make_int_type (dhandle, 4, true);
720       name = "instruction address";
721       break;
722
723     case builtin_int:
724       /* FIXME: The size for this type should depend upon the
725          processor.  */
726       type = debug_make_int_type (dhandle, 4, false);
727       name = "int";
728       break;
729
730     case builtin_unsigned:
731       /* FIXME: The size for this type should depend upon the
732          processor.  */
733       type = debug_make_int_type (dhandle, 4, true);
734       name = "unsigned";
735       break;
736
737     case builtin_unsigned_int:
738       /* FIXME: The size for this type should depend upon the
739          processor.  */
740       type = debug_make_int_type (dhandle, 4, true);
741       name = "unsigned int";
742       break;
743
744     case builtin_char:
745       type = debug_make_int_type (dhandle, 1, false);
746       name = "char";
747       break;
748
749     case builtin_long:
750       type = debug_make_int_type (dhandle, 4, false);
751       name = "long";
752       break;
753
754     case builtin_short:
755       type = debug_make_int_type (dhandle, 2, false);
756       name = "short";
757       break;
758
759     case builtin_unsigned_short:
760       type = debug_make_int_type (dhandle, 2, true);
761       name = "unsigned short";
762       break;
763
764     case builtin_short_int:
765       type = debug_make_int_type (dhandle, 2, false);
766       name = "short int";
767       break;
768
769     case builtin_signed_short:
770       type = debug_make_int_type (dhandle, 2, false);
771       name = "signed short";
772       break;
773
774     case builtin_bcd_float:
775       ieee_error (info, p, "BCD float type not supported");
776       return false;
777     }
778
779   if (name != NULL)
780     type = debug_name_type (dhandle, name, type);
781
782   assert (indx < BUILTIN_TYPE_COUNT);
783
784   info->types.builtins[indx] = type;
785
786   return type;
787 }
788
789 /* Allocate more space in the type table.  If ref is true, this is a
790    reference to the type; if it is not already defined, we should set
791    up an indirect type.  */
792
793 static boolean
794 ieee_alloc_type (info, indx, ref)
795      struct ieee_info *info;
796      unsigned int indx;
797      boolean ref;
798 {
799   unsigned int nalloc;
800   register struct ieee_type *t;
801   struct ieee_type *tend;
802
803   if (indx >= info->types.alloc)
804     {
805       nalloc = info->types.alloc;
806       if (nalloc == 0)
807         nalloc = 4;
808       while (indx >= nalloc)
809         nalloc *= 2;
810
811       info->types.types = ((struct ieee_type *)
812                            xrealloc (info->types.types,
813                                      nalloc * sizeof *info->types.types));
814
815       memset (info->types.types + info->types.alloc, 0,
816               (nalloc - info->types.alloc) * sizeof *info->types.types);
817
818       tend = info->types.types + nalloc;
819       for (t = info->types.types + info->types.alloc; t < tend; t++)
820         t->type = DEBUG_TYPE_NULL;
821
822       info->types.alloc = nalloc;
823     }
824
825   if (ref)
826     {
827       t = info->types.types + indx;
828       if (t->type == NULL)
829         {
830           t->pslot = (debug_type *) xmalloc (sizeof *t->pslot);
831           *t->pslot = DEBUG_TYPE_NULL;
832           t->type = debug_make_indirect_type (info->dhandle, t->pslot,
833                                               (const char *) NULL);
834           if (t->type == NULL)
835             return false;
836         }
837     }
838
839   return true;
840 }
841
842 /* Read a type index and return the corresponding type.  */
843
844 static boolean
845 ieee_read_type_index (info, pp, ptype)
846      struct ieee_info *info;
847      const bfd_byte **pp;
848      debug_type *ptype;
849 {
850   const bfd_byte *start;
851   bfd_vma indx;
852
853   start = *pp;
854
855   if (! ieee_read_number (info, pp, &indx))
856     return false;
857
858   if (indx < 256)
859     {
860       *ptype = ieee_builtin_type (info, start, indx);
861       if (*ptype == NULL)
862         return false;
863       return true;
864     }
865
866   indx -= 256;
867   if (! ieee_alloc_type (info, indx, true))
868     return false;
869
870   *ptype = info->types.types[indx].type;
871
872   return true;
873 }
874
875 /* Parse IEEE debugging information for a file.  This is passed the
876    bytes which compose the Debug Information Part of an IEEE file.  */
877
878 boolean
879 parse_ieee (dhandle, abfd, bytes, len)
880      PTR dhandle;
881      bfd *abfd;
882      const bfd_byte *bytes;
883      bfd_size_type len;
884 {
885   struct ieee_info info;
886   unsigned int i;
887   const bfd_byte *p, *pend;
888
889   info.dhandle = dhandle;
890   info.abfd = abfd;
891   info.bytes = bytes;
892   info.pend = bytes + len;
893   info.blockstack.bsp = info.blockstack.stack;
894   info.vars.alloc = 0;
895   info.vars.vars = NULL;
896   info.types.alloc = 0;
897   info.types.types = NULL;
898   info.tags = NULL;
899   for (i = 0; i < BUILTIN_TYPE_COUNT; i++)
900     info.types.builtins[i] = DEBUG_TYPE_NULL;
901
902   p = bytes;
903   pend = info.pend;
904   while (p < pend)
905     {
906       const bfd_byte *record_start;
907       ieee_record_enum_type c;
908
909       record_start = p;
910
911       c = (ieee_record_enum_type) *p++;
912
913       if (c == ieee_at_record_enum)
914         c = (ieee_record_enum_type) (((unsigned int) c << 8) | *p++);
915
916       if (c <= ieee_number_repeat_end_enum)
917         {
918           ieee_error (&info, record_start, "unexpected number");
919           return false;
920         }
921
922       switch (c)
923         {
924         default:
925           ieee_error (&info, record_start, "unexpected record type");
926           return false;
927
928         case ieee_bb_record_enum:
929           if (! parse_ieee_bb (&info, &p))
930             return false;
931           break;
932
933         case ieee_be_record_enum:
934           if (! parse_ieee_be (&info, &p))
935             return false;
936           break;
937
938         case ieee_nn_record:
939           if (! parse_ieee_nn (&info, &p))
940             return false;
941           break;
942
943         case ieee_ty_record_enum:
944           if (! parse_ieee_ty (&info, &p))
945             return false;
946           break;
947
948         case ieee_atn_record_enum:
949           if (! parse_ieee_atn (&info, &p))
950             return false;
951           break;
952         }
953     }
954
955   if (info.blockstack.bsp != info.blockstack.stack)
956     {
957       ieee_error (&info, (const bfd_byte *) NULL,
958                   "blocks left on stack at end");
959       return false;
960     }
961
962   return true;
963 }
964
965 /* Handle an IEEE BB record.  */
966
967 static boolean
968 parse_ieee_bb (info, pp)
969      struct ieee_info *info;
970      const bfd_byte **pp;
971 {
972   const bfd_byte *block_start;
973   bfd_byte b;
974   bfd_vma size;
975   const char *name;
976   unsigned long namlen;
977   char *namcopy;
978   unsigned int fnindx;
979   boolean skip;
980
981   block_start = *pp;
982
983   b = **pp;
984   ++*pp;
985
986   if (! ieee_read_number (info, pp, &size)
987       || ! ieee_read_id (info, pp, &name, &namlen))
988     return false;
989
990   fnindx = (unsigned int) -1;
991   skip = false;
992
993   switch (b)
994     {
995     case 1:
996       /* BB1: Type definitions local to a module.  */
997       namcopy = savestring (name, namlen);
998       if (namcopy == NULL)
999         return false;
1000       if (! debug_set_filename (info->dhandle, namcopy))
1001         return false;
1002       break;
1003
1004     case 2:
1005       /* BB2: Global type definitions.  The name is supposed to be
1006          empty, but we don't check. */
1007       if (! debug_set_filename (info->dhandle, "*global*"))
1008         return false;
1009       break;
1010
1011     case 3:
1012       /* BB3: High level module block begin.  We don't have to do
1013          anything here.  The name is supposed to be the same as for
1014          the BB1, but we don't check.  */
1015       break;
1016
1017     case 4:
1018       /* BB4: Global function.  */
1019       {
1020         bfd_vma stackspace, typindx, offset;
1021         debug_type return_type;
1022
1023         if (! ieee_read_number (info, pp, &stackspace)
1024             || ! ieee_read_number (info, pp, &typindx)
1025             || ! ieee_read_expression (info, pp, &offset))
1026           return false;
1027
1028         /* We have no way to record the stack space.  FIXME.  */
1029
1030         if (typindx < 256)
1031           {
1032             return_type = ieee_builtin_type (info, block_start, typindx);
1033             if (return_type == DEBUG_TYPE_NULL)
1034               return false;
1035           }
1036         else
1037           {
1038             typindx -= 256;
1039             if (! ieee_alloc_type (info, typindx, true))
1040               return false;
1041             fnindx = typindx;
1042             return_type = info->types.types[typindx].type;
1043             if (debug_get_type_kind (info->dhandle, return_type)
1044                 == DEBUG_KIND_FUNCTION)
1045               return_type = debug_get_return_type (info->dhandle,
1046                                                    return_type);
1047           }
1048
1049         namcopy = savestring (name, namlen);
1050         if (namcopy == NULL)
1051           return false;
1052         if (! debug_record_function (info->dhandle, namcopy, return_type,
1053                                      true, offset))
1054           return false;
1055       }
1056       break;
1057
1058     case 5:
1059       /* BB5: File name for source line numbers.  */
1060       {
1061         unsigned int i;
1062
1063         /* We ignore the date and time.  FIXME.  */
1064         for (i = 0; i < 6; i++)
1065           {
1066             bfd_vma ignore;
1067             boolean present;
1068
1069             if (! ieee_read_optional_number (info, pp, &ignore, &present))
1070               return false;
1071             if (! present)
1072               break;
1073           }
1074
1075         namcopy = savestring (name, namlen);
1076         if (namcopy == NULL)
1077           return false;
1078         if (! debug_start_source (info->dhandle, namcopy))
1079           return false;
1080       }
1081       break;
1082
1083     case 6:
1084       /* BB6: Local function or block.  */
1085       {
1086         bfd_vma stackspace, typindx, offset;
1087
1088         if (! ieee_read_number (info, pp, &stackspace)
1089             || ! ieee_read_number (info, pp, &typindx)
1090             || ! ieee_read_expression (info, pp, &offset))
1091           return false;
1092
1093         /* We have no way to record the stack space.  FIXME.  */
1094
1095         if (namlen == 0)
1096           {
1097             if (! debug_start_block (info->dhandle, offset))
1098               return false;
1099             /* Change b to indicate that this is a block
1100                rather than a function.  */
1101             b = 0x86;
1102           }
1103         else
1104           {
1105             /* The MRI C++ compiler will output a fake function named
1106                __XRYCPP to hold C++ debugging information.  We skip
1107                that function.  This is not crucial, but it makes
1108                converting from IEEE to other debug formats work
1109                better.  */
1110             if (strncmp (name, "__XRYCPP", namlen) == 0)
1111               skip = true;
1112             else
1113               {
1114                 debug_type return_type;
1115
1116                 if (typindx < 256)
1117                   {
1118                     return_type = ieee_builtin_type (info, block_start,
1119                                                      typindx);
1120                     if (return_type == NULL)
1121                       return false;
1122                   }
1123                 else
1124                   {
1125                     typindx -= 256;
1126                     if (! ieee_alloc_type (info, typindx, true))
1127                       return false;
1128                     fnindx = typindx;
1129                     return_type = info->types.types[typindx].type;
1130                     if (debug_get_type_kind (info->dhandle, return_type)
1131                         == DEBUG_KIND_FUNCTION)
1132                       return_type = debug_get_return_type (info->dhandle,
1133                                                            return_type);
1134                   }
1135
1136                 namcopy = savestring (name, namlen);
1137                 if (namcopy == NULL)
1138                   return false;
1139                 if (! debug_record_function (info->dhandle, namcopy,
1140                                              return_type, false, offset))
1141                   return false;
1142               }
1143           }
1144       }
1145       break;
1146
1147     case 10:
1148       /* BB10: Assembler module scope.  We completely ignore all this
1149          information.  FIXME.  */
1150       {
1151         const char *inam, *vstr;
1152         unsigned long inamlen, vstrlen;
1153         bfd_vma tool_type;
1154         boolean present;
1155         unsigned int i;
1156
1157         if (! ieee_read_id (info, pp, &inam, &inamlen)
1158             || ! ieee_read_number (info, pp, &tool_type)
1159             || ! ieee_read_optional_id (info, pp, &vstr, &vstrlen, &present))
1160           return false;
1161         for (i = 0; i < 6; i++)
1162           {
1163             bfd_vma ignore;
1164
1165             if (! ieee_read_optional_number (info, pp, &ignore, &present))
1166               return false;
1167             if (! present)
1168               break;
1169           }
1170       }
1171       break;
1172
1173     case 11:
1174       /* BB11: Module section.  We completely ignore all this
1175          information.  FIXME.  */
1176       {
1177         bfd_vma sectype, secindx, offset, map;
1178         boolean present;
1179
1180         if (! ieee_read_number (info, pp, &sectype)
1181             || ! ieee_read_number (info, pp, &secindx)
1182             || ! ieee_read_expression (info, pp, &offset)
1183             || ! ieee_read_optional_number (info, pp, &map, &present))
1184           return false;
1185       }
1186       break;
1187
1188     default:
1189       ieee_error (info, block_start, "unknown BB type");
1190       return false;
1191     }
1192
1193
1194   /* Push this block on the block stack.  */
1195
1196   if (info->blockstack.bsp >= info->blockstack.stack + BLOCKSTACK_SIZE)
1197     {
1198       ieee_error (info, (const bfd_byte *) NULL, "stack overflow");
1199       return false;
1200     }
1201
1202   info->blockstack.bsp->kind = b;
1203   if (b == 5)
1204     info->blockstack.bsp->filename = namcopy;
1205   info->blockstack.bsp->fnindx = fnindx;
1206   info->blockstack.bsp->skip = skip;
1207   ++info->blockstack.bsp;
1208
1209   return true;
1210 }
1211
1212 /* Handle an IEEE BE record.  */
1213
1214 static boolean
1215 parse_ieee_be (info, pp)
1216      struct ieee_info *info;
1217      const bfd_byte **pp;
1218 {
1219   bfd_vma offset;
1220
1221   if (info->blockstack.bsp <= info->blockstack.stack)
1222     {
1223       ieee_error (info, *pp, "stack underflow");
1224       return false;
1225     }
1226   --info->blockstack.bsp;
1227
1228   switch (info->blockstack.bsp->kind)
1229     {
1230     case 4:
1231     case 6:
1232       if (! ieee_read_expression (info, pp, &offset))
1233         return false;
1234       if (! info->blockstack.bsp->skip)
1235         {
1236           if (! debug_end_function (info->dhandle, offset + 1))
1237             return false;
1238         }
1239       break;
1240
1241     case 0x86:
1242       /* This is BE6 when BB6 started a block rather than a local
1243          function.  */
1244       if (! ieee_read_expression (info, pp, &offset))
1245         return false;
1246       if (! debug_end_block (info->dhandle, offset + 1))
1247         return false;
1248       break;
1249
1250     case 5:
1251       /* When we end a BB5, we look up the stack for the last BB5, if
1252          there is one, so that we can call debug_start_source.  */
1253       if (info->blockstack.bsp > info->blockstack.stack)
1254         {
1255           struct ieee_block *bl;
1256
1257           bl = info->blockstack.bsp;
1258           do
1259             {
1260               --bl;
1261               if (bl->kind == 5)
1262                 {
1263                   if (! debug_start_source (info->dhandle, bl->filename))
1264                     return false;
1265                   break;
1266                 }
1267             }
1268           while (bl != info->blockstack.stack);
1269         }
1270       break;
1271
1272     case 11:
1273       if (! ieee_read_expression (info, pp, &offset))
1274         return false;
1275       /* We just ignore the module size.  FIXME.  */
1276       break;
1277
1278     default:
1279       /* Other block types do not have any trailing information.  */
1280       break;
1281     }
1282
1283   return true;
1284 }
1285
1286 /* Parse an NN record.  */
1287
1288 static boolean
1289 parse_ieee_nn (info, pp)
1290      struct ieee_info *info;
1291      const bfd_byte **pp;
1292 {
1293   const bfd_byte *nn_start;
1294   bfd_vma varindx;
1295   const char *name;
1296   unsigned long namlen;
1297
1298   nn_start = *pp;
1299
1300   if (! ieee_read_number (info, pp, &varindx)
1301       || ! ieee_read_id (info, pp, &name, &namlen))
1302     return false;
1303
1304   if (varindx < 32)
1305     {
1306       ieee_error (info, nn_start, "illegal variable index");
1307       return false;
1308     }
1309   varindx -= 32;
1310
1311   if (varindx >= info->vars.alloc)
1312     {
1313       unsigned int alloc;
1314
1315       alloc = info->vars.alloc;
1316       if (alloc == 0)
1317         alloc = 4;
1318       while (varindx >= alloc)
1319         alloc *= 2;
1320       info->vars.vars = ((struct ieee_var *)
1321                          xrealloc (info->vars.vars,
1322                                    alloc * sizeof *info->vars.vars));
1323       memset (info->vars.vars + info->vars.alloc, 0,
1324               (alloc - info->vars.alloc) * sizeof *info->vars.vars);
1325       info->vars.alloc = alloc;
1326     }
1327
1328   info->vars.vars[varindx].name = name;
1329   info->vars.vars[varindx].namlen = namlen;
1330
1331   return true;
1332 }
1333
1334 /* Parse a TY record.  */
1335
1336 static boolean
1337 parse_ieee_ty (info, pp)
1338      struct ieee_info *info;
1339      const bfd_byte **pp;
1340 {
1341   const bfd_byte *ty_start, *ty_var_start, *ty_code_start;
1342   bfd_vma typeindx, varindx, tc;
1343   PTR dhandle;
1344   boolean tag, typdef;
1345   debug_type *arg_slots;
1346   unsigned long type_bitsize;
1347   debug_type type;
1348
1349   ty_start = *pp;
1350
1351   if (! ieee_read_number (info, pp, &typeindx))
1352     return false;
1353
1354   if (typeindx < 256)
1355     {
1356       ieee_error (info, ty_start, "illegal type index");
1357       return false;
1358     }
1359
1360   typeindx -= 256;
1361   if (! ieee_alloc_type (info, typeindx, false))
1362     return false;
1363
1364   if (**pp != 0xce)
1365     {
1366       ieee_error (info, *pp, "unknown TY code");
1367       return false;
1368     }
1369   ++*pp;
1370
1371   ty_var_start = *pp;
1372
1373   if (! ieee_read_number (info, pp, &varindx))
1374     return false;
1375
1376   if (varindx < 32)
1377     {
1378       ieee_error (info, ty_var_start, "illegal variable index");
1379       return false;
1380     }
1381   varindx -= 32;
1382
1383   if (varindx >= info->vars.alloc || info->vars.vars[varindx].name == NULL)
1384     {
1385       ieee_error (info, ty_var_start, "undefined variable in TY");
1386       return false;
1387     }
1388
1389   ty_code_start = *pp;
1390
1391   if (! ieee_read_number (info, pp, &tc))
1392     return false;
1393
1394   dhandle = info->dhandle;
1395
1396   tag = false;
1397   typdef = false;
1398   arg_slots = NULL;
1399   type_bitsize = 0;
1400   switch (tc)
1401     {
1402     default:
1403       ieee_error (info, ty_code_start, "unknown TY code");
1404       return false;
1405
1406     case '!':
1407       /* Unknown type, with size.  We treat it as int.  FIXME.  */
1408       {
1409         bfd_vma size;
1410
1411         if (! ieee_read_number (info, pp, &size))
1412           return false;
1413         type = debug_make_int_type (dhandle, size, false);
1414       }
1415       break;
1416
1417     case 'A': /* Array.  */
1418     case 'a': /* FORTRAN array in column/row order.  FIXME: Not
1419                  distinguished from normal array.  */
1420       {
1421         debug_type ele_type;
1422         bfd_vma lower, upper;
1423
1424         if (! ieee_read_type_index (info, pp, &ele_type)
1425             || ! ieee_read_number (info, pp, &lower)
1426             || ! ieee_read_number (info, pp, &upper))
1427           return false;
1428         type = debug_make_array_type (dhandle, ele_type,
1429                                       ieee_builtin_type (info, ty_code_start,
1430                                                          ((unsigned int)
1431                                                           builtin_int)),
1432                                       (bfd_signed_vma) lower,
1433                                       (bfd_signed_vma) upper,
1434                                       false);
1435       }
1436       break;
1437
1438     case 'E':
1439       /* Simple enumeration.  */
1440       {
1441         bfd_vma size;
1442         unsigned int alloc;
1443         const char **names;
1444         unsigned int c;
1445         bfd_signed_vma *vals;
1446         unsigned int i;
1447
1448         if (! ieee_read_number (info, pp, &size))
1449           return false;
1450         /* FIXME: we ignore the enumeration size.  */
1451
1452         alloc = 10;
1453         names = (const char **) xmalloc (alloc * sizeof *names);
1454         memset (names, 0, alloc * sizeof *names);
1455         c = 0;
1456         while (1)
1457           {
1458             const char *name;
1459             unsigned long namlen;
1460             boolean present;
1461
1462             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
1463               return false;
1464             if (! present)
1465               break;
1466
1467             if (c + 1 >= alloc)
1468               {
1469                 alloc += 10;
1470                 names = ((const char **)
1471                          xrealloc (names, alloc * sizeof *names));
1472               }
1473
1474             names[c] = savestring (name, namlen);
1475             if (names[c] == NULL)
1476               return false;
1477             ++c;
1478           }
1479
1480         names[c] = NULL;
1481
1482         vals = (bfd_signed_vma *) xmalloc (c * sizeof *vals);
1483         for (i = 0; i < c; i++)
1484           vals[i] = i;
1485
1486         type = debug_make_enum_type (dhandle, names, vals);
1487         tag = true;
1488       }
1489       break;
1490
1491     case 'G':
1492       /* Struct with bit fields.  */
1493       {
1494         bfd_vma size;
1495         unsigned int alloc;
1496         debug_field *fields;
1497         unsigned int c;
1498
1499         if (! ieee_read_number (info, pp, &size))
1500           return false;
1501
1502         alloc = 10;
1503         fields = (debug_field *) xmalloc (alloc * sizeof *fields);
1504         c = 0;
1505         while (1)
1506           {
1507             const char *name;
1508             unsigned long namlen;
1509             boolean present;
1510             debug_type ftype;
1511             bfd_vma bitpos, bitsize;
1512
1513             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
1514               return false;
1515             if (! present)
1516               break;
1517             if (! ieee_read_type_index (info, pp, &ftype)
1518                 || ! ieee_read_number (info, pp, &bitpos)
1519                 || ! ieee_read_number (info, pp, &bitsize))
1520               return false;
1521
1522             if (c + 1 >= alloc)
1523               {
1524                 alloc += 10;
1525                 fields = ((debug_field *)
1526                           xrealloc (fields, alloc * sizeof *fields));
1527               }
1528
1529             fields[c] = debug_make_field (dhandle, savestring (name, namlen),
1530                                           ftype, bitpos, bitsize,
1531                                           DEBUG_VISIBILITY_PUBLIC);
1532             if (fields[c] == NULL)
1533               return false;
1534             ++c;
1535           }
1536
1537         fields[c] = NULL;
1538
1539         type = debug_make_struct_type (dhandle, true, size, fields);
1540         tag = true;
1541       }
1542       break;
1543
1544     case 'N':
1545       /* Enumeration.  */
1546       {
1547         unsigned int alloc;
1548         const char **names;
1549         bfd_signed_vma *vals;
1550         unsigned int c;
1551
1552         alloc = 10;
1553         names = (const char **) xmalloc (alloc * sizeof *names);
1554         vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *names);
1555         c = 0;
1556         while (1)
1557           {
1558             const char *name;
1559             unsigned long namlen;
1560             boolean present;
1561             bfd_vma val;
1562
1563             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
1564               return false;
1565             if (! present)
1566               break;
1567             if (! ieee_read_number (info, pp, &val))
1568               return false;
1569
1570             /* If the length of the name is zero, then the value is
1571                actually the size of the enum.  We ignore this
1572                information.  FIXME.  */
1573             if (namlen == 0)
1574               continue;
1575
1576             if (c + 1 >= alloc)
1577               {
1578                 alloc += 10;
1579                 names = ((const char **)
1580                          xrealloc (names, alloc * sizeof *names));
1581                 vals = ((bfd_signed_vma *)
1582                         xrealloc (vals, alloc * sizeof *vals));
1583               }
1584
1585             names[c] = savestring (name, namlen);
1586             if (names[c] == NULL)
1587               return false;
1588             vals[c] = (bfd_signed_vma) val;
1589             ++c;
1590           }
1591
1592         names[c] = NULL;
1593
1594         type = debug_make_enum_type (dhandle, names, vals);
1595         tag = true;
1596       }
1597       break;
1598
1599     case 'O': /* Small pointer.  We don't distinguish small and large
1600                  pointers.  FIXME.  */
1601     case 'P': /* Large pointer.  */
1602       {
1603         debug_type t;
1604
1605         if (! ieee_read_type_index (info, pp, &t))
1606           return false;
1607         type = debug_make_pointer_type (dhandle, t);
1608       }
1609       break;
1610
1611     case 'R':
1612       /* Range.  */
1613       {
1614         bfd_vma low, high, signedp, size;
1615
1616         if (! ieee_read_number (info, pp, &low)
1617             || ! ieee_read_number (info, pp, &high)
1618             || ! ieee_read_number (info, pp, &signedp)
1619             || ! ieee_read_number (info, pp, &size))
1620           return false;
1621
1622         type = debug_make_range_type (dhandle,
1623                                       debug_make_int_type (dhandle, size,
1624                                                            ! signedp),
1625                                       (bfd_signed_vma) low,
1626                                       (bfd_signed_vma) high);
1627       }
1628       break;
1629
1630     case 'S': /* Struct.  */
1631     case 'U': /* Union.  */
1632       {
1633         bfd_vma size;
1634         unsigned int alloc;
1635         debug_field *fields;
1636         unsigned int c;
1637
1638         if (! ieee_read_number (info, pp, &size))
1639           return false;
1640
1641         alloc = 10;
1642         fields = (debug_field *) xmalloc (alloc * sizeof *fields);
1643         c = 0;
1644         while (1)
1645           {
1646             const char *name;
1647             unsigned long namlen;
1648             boolean present;
1649             bfd_vma tindx;
1650             bfd_vma offset;
1651             debug_type ftype;
1652             bfd_vma bitsize;
1653
1654             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
1655               return false;
1656             if (! present)
1657               break;
1658             if (! ieee_read_number (info, pp, &tindx)
1659                 || ! ieee_read_number (info, pp, &offset))
1660               return false;
1661
1662             if (tindx < 256)
1663               {
1664                 ftype = ieee_builtin_type (info, ty_code_start, tindx);
1665                 bitsize = 0;
1666                 offset *= 8;
1667               }
1668             else
1669               {
1670                 struct ieee_type *t;
1671
1672                 tindx -= 256;
1673                 if (! ieee_alloc_type (info, tindx, true))
1674                   return false;
1675                 t = info->types.types + tindx;
1676                 ftype = t->type;
1677                 bitsize = t->bitsize;
1678                 if (bitsize == 0)
1679                   offset *= 8;
1680               }
1681
1682             if (c + 1 >= alloc)
1683               {
1684                 alloc += 10;
1685                 fields = ((debug_field *)
1686                           xrealloc (fields, alloc * sizeof *fields));
1687               }
1688
1689             fields[c] = debug_make_field (dhandle, savestring (name, namlen),
1690                                           ftype, offset, bitsize,
1691                                           DEBUG_VISIBILITY_PUBLIC);
1692             if (fields[c] == NULL)
1693               return false;
1694             ++c;
1695           }
1696
1697         fields[c] = NULL;
1698
1699         type = debug_make_struct_type (dhandle, tc == 'S', size, fields);
1700         tag = true;
1701       }
1702       break;
1703
1704     case 'T':
1705       /* Typedef.  */
1706       if (! ieee_read_type_index (info, pp, &type))
1707         return false;
1708       typdef = true;
1709       break;
1710
1711     case 'X':
1712       /* Procedure.  FIXME: This is an extern declaration, which we
1713          have no way of representing.  */
1714       {
1715         bfd_vma attr;
1716         debug_type rtype;
1717         bfd_vma nargs;
1718         boolean present;
1719         struct ieee_var *pv;
1720
1721         /* FIXME: We ignore the attribute and the argument names.  */
1722
1723         if (! ieee_read_number (info, pp, &attr)
1724             || ! ieee_read_type_index (info, pp, &rtype)
1725             || ! ieee_read_number (info, pp, &nargs))
1726           return false;
1727         do
1728           {
1729             const char *name;
1730             unsigned long namlen;
1731
1732             if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
1733               return false;
1734           }
1735         while (present);
1736
1737         pv = info->vars.vars + varindx;
1738         pv->kind = IEEE_EXTERNAL;
1739         if (pv->namlen > 0
1740             && debug_get_type_kind (dhandle, rtype) == DEBUG_KIND_POINTER)
1741           {
1742             /* Set up the return type as an indirect type pointing to
1743                the variable slot, so that we can change it to a
1744                reference later if appropriate.  */
1745             pv->pslot = (debug_type *) xmalloc (sizeof *pv->pslot);
1746             *pv->pslot = rtype;
1747             rtype = debug_make_indirect_type (dhandle, pv->pslot,
1748                                               (const char *) NULL);
1749           }
1750
1751         type = debug_make_function_type (dhandle, rtype, (debug_type *) NULL,
1752                                          false);
1753       }
1754       break;
1755
1756     case 'V':
1757       /* Void.  This is not documented, but the MRI compiler emits it.  */
1758       type = debug_make_void_type (dhandle);
1759       break;
1760
1761     case 'Z':
1762       /* Array with 0 lower bound.  */
1763       {
1764         debug_type etype;
1765         bfd_vma high;
1766
1767         if (! ieee_read_type_index (info, pp, &etype)
1768             || ! ieee_read_number (info, pp, &high))
1769           return false;
1770
1771         type = debug_make_array_type (dhandle, etype,
1772                                       ieee_builtin_type (info, ty_code_start,
1773                                                          ((unsigned int)
1774                                                           builtin_int)),
1775                                       0, (bfd_signed_vma) high, false);
1776       }
1777       break;
1778
1779     case 'c': /* Complex.  */
1780     case 'd': /* Double complex.  */
1781       {
1782         const char *name;
1783         unsigned long namlen;
1784
1785         /* FIXME: I don't know what the name means.  */
1786
1787         if (! ieee_read_id (info, pp, &name, &namlen))
1788           return false;
1789
1790         type = debug_make_complex_type (dhandle, tc == 'c' ? 4 : 8);
1791       }
1792       break;
1793
1794     case 'f':
1795       /* Pascal file name.  FIXME.  */
1796       ieee_error (info, ty_code_start, "Pascal file name not supported");
1797       return false;
1798
1799     case 'g':
1800       /* Bitfield type.  */
1801       {
1802         bfd_vma signedp, bitsize;
1803
1804         if (! ieee_read_number (info, pp, &signedp)
1805             || ! ieee_read_number (info, pp, &bitsize)
1806             || ! ieee_read_type_index (info, pp, &type))
1807           return false;
1808
1809         /* FIXME: This is just a guess.  */
1810         if (! signedp)
1811           type = debug_make_int_type (dhandle, 4, true);
1812         type_bitsize = bitsize;
1813       }
1814       break;
1815
1816     case 'n':
1817       /* Qualifier.  */
1818       {
1819         bfd_vma kind;
1820         debug_type t;
1821
1822         if (! ieee_read_number (info, pp, &kind)
1823             || ! ieee_read_type_index (info, pp, &t))
1824           return false;
1825
1826         switch (kind)
1827           {
1828           default:
1829             ieee_error (info, ty_start, "unsupported qualifer");
1830             return false;
1831
1832           case 1:
1833             type = debug_make_const_type (dhandle, t);
1834             break;
1835
1836           case 2:
1837             type = debug_make_volatile_type (dhandle, t);
1838             break;
1839           }
1840       }
1841       break;
1842
1843     case 's':
1844       /* Set.  */
1845       {
1846         bfd_vma size;
1847         debug_type etype;
1848
1849         if (! ieee_read_number (info, pp, &size)
1850             || ! ieee_read_type_index (info, pp, &etype))
1851           return false;
1852
1853         /* FIXME: We ignore the size.  */
1854
1855         type = debug_make_set_type (dhandle, etype, false);
1856       }
1857       break;
1858
1859     case 'x':
1860       /* Procedure with compiler dependencies.  */
1861       {
1862         struct ieee_var *pv;
1863         bfd_vma attr, frame_type, push_mask, nargs, level, father;
1864         debug_type rtype;
1865         debug_type *arg_types;
1866         boolean varargs;
1867         boolean present;
1868
1869         /* FIXME: We ignore some of this information.  */
1870
1871         pv = info->vars.vars + varindx;
1872
1873         if (! ieee_read_number (info, pp, &attr)
1874             || ! ieee_read_number (info, pp, &frame_type)
1875             || ! ieee_read_number (info, pp, &push_mask)
1876             || ! ieee_read_type_index (info, pp, &rtype)
1877             || ! ieee_read_number (info, pp, &nargs))
1878           return false;
1879         if (nargs == (bfd_vma) -1)
1880           {
1881             arg_types = NULL;
1882             varargs = false;
1883           }
1884         else
1885           {
1886             unsigned int i;
1887
1888             arg_types = ((debug_type *)
1889                          xmalloc ((nargs + 1) * sizeof *arg_types));
1890             for (i = 0; i < nargs; i++)
1891               if (! ieee_read_type_index (info, pp, arg_types + i))
1892                 return false;
1893
1894             /* If the last type is pointer to void, this is really a
1895                varargs function.  */
1896             varargs = false;
1897             if (nargs > 0)
1898               {
1899                 debug_type last;
1900
1901                 last = arg_types[nargs - 1];
1902                 if (debug_get_type_kind (dhandle, last) == DEBUG_KIND_POINTER
1903                     && (debug_get_type_kind (dhandle,
1904                                              debug_get_target_type (dhandle,
1905                                                                     last))
1906                         == DEBUG_KIND_VOID))
1907                   {
1908                     --nargs;
1909                     varargs = true;
1910                   }
1911               }
1912
1913             /* If there are any pointer arguments, turn them into
1914                indirect types in case we later need to convert them to
1915                reference types.  */
1916             for (i = 0; i < nargs; i++)
1917               {
1918                 if (debug_get_type_kind (dhandle, arg_types[i])
1919                     == DEBUG_KIND_POINTER)
1920                   {
1921                     if (arg_slots == NULL)
1922                       {
1923                         arg_slots = ((debug_type *)
1924                                      xmalloc (nargs * sizeof *arg_slots));
1925                         memset (arg_slots, 0, nargs * sizeof *arg_slots);
1926                       }
1927                     arg_slots[i] = arg_types[i];
1928                     arg_types[i] =
1929                       debug_make_indirect_type (dhandle,
1930                                                 arg_slots + i,
1931                                                 (const char *) NULL);
1932                   }
1933               }
1934
1935             arg_types[nargs] = DEBUG_TYPE_NULL;
1936           }
1937         if (! ieee_read_number (info, pp, &level)
1938             || ! ieee_read_optional_number (info, pp, &father, &present))
1939           return false;
1940
1941         /* We can't distinguish between a global function and a static
1942            function.  */
1943         pv->kind = IEEE_FUNCTION;
1944
1945         if (pv->namlen > 0
1946             && debug_get_type_kind (dhandle, rtype) == DEBUG_KIND_POINTER)
1947           {
1948             /* Set up the return type as an indirect type pointing to
1949                the variable slot, so that we can change it to a
1950                reference later if appropriate.  */
1951             pv->pslot = (debug_type *) xmalloc (sizeof *pv->pslot);
1952             *pv->pslot = rtype;
1953             rtype = debug_make_indirect_type (dhandle, pv->pslot,
1954                                               (const char *) NULL);
1955           }
1956
1957         type = debug_make_function_type (dhandle, rtype, arg_types, varargs);
1958       }
1959       break;
1960     }
1961
1962   /* Record the type in the table.  If the corresponding NN record has
1963      a name, name it.  FIXME: Is this always correct?  */
1964
1965   if (type == DEBUG_TYPE_NULL)
1966     return false;
1967
1968   info->vars.vars[varindx].type = type;
1969
1970   if ((tag || typdef)
1971       && info->vars.vars[varindx].namlen > 0)
1972     {
1973       const char *name;
1974
1975       name = savestring (info->vars.vars[varindx].name,
1976                          info->vars.vars[varindx].namlen);
1977       if (typdef)
1978         type = debug_name_type (dhandle, name, type);
1979       else if (tc == 'E' || tc == 'N')
1980         type = debug_tag_type (dhandle, name, type);
1981       else
1982         {
1983           struct ieee_tag *it;
1984
1985           /* We must allocate all struct tags as indirect types, so
1986              that if we later see a definition of the tag as a C++
1987              record we can update the indirect slot and automatically
1988              change all the existing references.  */
1989           it = (struct ieee_tag *) xmalloc (sizeof *it);
1990           memset (it, 0, sizeof *it);
1991           it->next = info->tags;
1992           info->tags = it;
1993           it->name = name;
1994           it->slot = type;
1995
1996           type = debug_make_indirect_type (dhandle, &it->slot, name);
1997           type = debug_tag_type (dhandle, name, type);
1998
1999           it->type = type;
2000         }
2001       if (type == NULL)
2002         return false;
2003     }
2004
2005   info->types.types[typeindx].type = type;
2006   info->types.types[typeindx].arg_slots = arg_slots;
2007   info->types.types[typeindx].bitsize = type_bitsize;
2008
2009   /* We may have already allocated type as an indirect type pointing
2010      to slot.  It does no harm to replace the indirect type with the
2011      real type.  Filling in slot as well handles the indirect types
2012      which are already hanging around.  */
2013   if (info->types.types[typeindx].pslot != NULL)
2014     *info->types.types[typeindx].pslot = type;
2015
2016   return true;
2017 }
2018
2019 /* Parse an ATN record.  */
2020
2021 static boolean
2022 parse_ieee_atn (info, pp)
2023      struct ieee_info *info;
2024      const bfd_byte **pp;
2025 {
2026   const bfd_byte *atn_start, *atn_code_start;
2027   bfd_vma varindx;
2028   struct ieee_var *pvar;
2029   debug_type type;
2030   bfd_vma atn_code;
2031   PTR dhandle;
2032   bfd_vma v, v2, v3, v4, v5;
2033   const char *name;
2034   unsigned long namlen;
2035   char *namcopy;
2036   boolean present;
2037   int blocktype;
2038
2039   atn_start = *pp;
2040
2041   if (! ieee_read_number (info, pp, &varindx)
2042       || ! ieee_read_type_index (info, pp, &type))
2043     return false;
2044
2045   atn_code_start = *pp;
2046
2047   if (! ieee_read_number (info, pp, &atn_code))
2048     return false;
2049
2050   if (varindx == 0)
2051     {
2052       pvar = NULL;
2053       name = "";
2054       namlen = 0;
2055     }
2056   else if (varindx < 32)
2057     {
2058       ieee_error (info, atn_start, "illegal variable index");
2059       return false;
2060     }
2061   else
2062     {
2063       varindx -= 32;
2064       if (varindx >= info->vars.alloc
2065           || info->vars.vars[varindx].name == NULL)
2066         {
2067           ieee_error (info, atn_start, "undefined variable in ATN");
2068           return false;
2069         }
2070
2071       pvar = info->vars.vars + varindx;
2072
2073       pvar->type = type;
2074
2075       name = pvar->name;
2076       namlen = pvar->namlen;
2077     }
2078
2079   dhandle = info->dhandle;
2080
2081   /* If we are going to call debug_record_variable with a pointer
2082      type, change the type to an indirect type so that we can later
2083      change it to a reference type if we encounter a C++ pmisc 'R'
2084      record.  */
2085   if (pvar != NULL
2086       && type != DEBUG_TYPE_NULL
2087       && debug_get_type_kind (dhandle, type) == DEBUG_KIND_POINTER)
2088     {
2089       switch (atn_code)
2090         {
2091         case 1:
2092         case 2:
2093         case 3:
2094         case 5:
2095         case 8:
2096         case 10:
2097           pvar->pslot = (debug_type *) xmalloc (sizeof *pvar->pslot);
2098           *pvar->pslot = type;
2099           type = debug_make_indirect_type (dhandle, pvar->pslot,
2100                                            (const char *) NULL);
2101           pvar->type = type;
2102           break;
2103         }
2104     }
2105
2106   switch (atn_code)
2107     {
2108     default:
2109       ieee_error (info, atn_code_start, "unknown ATN type");
2110       return false;
2111
2112     case 1:
2113       /* Automatic variable.  */
2114       if (! ieee_read_number (info, pp, &v))
2115         return false;
2116       namcopy = savestring (name, namlen);
2117       if (type == NULL)
2118         type = debug_make_void_type (dhandle);
2119       if (pvar != NULL)
2120         pvar->kind = IEEE_LOCAL;
2121       return debug_record_variable (dhandle, namcopy, type, DEBUG_LOCAL, v);
2122
2123     case 2:
2124       /* Register variable.  */
2125       if (! ieee_read_number (info, pp, &v))
2126         return false;
2127       namcopy = savestring (name, namlen);
2128       if (type == NULL)
2129         type = debug_make_void_type (dhandle);
2130       if (pvar != NULL)
2131         pvar->kind = IEEE_LOCAL;
2132       return debug_record_variable (dhandle, namcopy, type, DEBUG_REGISTER,
2133                                     ieee_regno_to_genreg (info->abfd, v));
2134
2135     case 3:
2136       /* Static variable.  */
2137       if (! ieee_require_asn (info, pp, &v))
2138         return false;
2139       namcopy = savestring (name, namlen);
2140       if (type == NULL)
2141         type = debug_make_void_type (dhandle);
2142       if (info->blockstack.bsp <= info->blockstack.stack)
2143         blocktype = 0;
2144       else
2145         blocktype = info->blockstack.bsp[-1].kind;
2146       if (pvar != NULL)
2147         {
2148           if (blocktype == 4 || blocktype == 6)
2149             pvar->kind = IEEE_LOCAL;
2150           else
2151             pvar->kind = IEEE_STATIC;
2152         }
2153       return debug_record_variable (dhandle, namcopy, type,
2154                                     (blocktype == 4 || blocktype == 6
2155                                      ? DEBUG_LOCAL_STATIC
2156                                      : DEBUG_STATIC),
2157                                     v);
2158
2159     case 4:
2160       /* External function.  We don't currently record these.  FIXME.  */
2161       if (pvar != NULL)
2162         pvar->kind = IEEE_EXTERNAL;
2163       return true;
2164
2165     case 5:
2166       /* External variable.  We don't currently record these.  FIXME.  */
2167       if (pvar != NULL)
2168         pvar->kind = IEEE_EXTERNAL;
2169       return true;
2170
2171     case 7:
2172       if (! ieee_read_number (info, pp, &v)
2173           || ! ieee_read_number (info, pp, &v2)
2174           || ! ieee_read_optional_number (info, pp, &v3, &present))
2175         return false;
2176       if (present)
2177         {
2178           if (! ieee_read_optional_number (info, pp, &v4, &present))
2179             return false;
2180         }
2181
2182       /* We just ignore the two optional fields in v3 and v4, since
2183          they are not defined.  */
2184
2185       if (! ieee_require_asn (info, pp, &v3))
2186         return false;
2187
2188       /* We have no way to record the column number.  FIXME.  */
2189
2190       return debug_record_line (dhandle, v, v3);
2191
2192     case 8:
2193       /* Global variable.  */
2194       if (! ieee_require_asn (info, pp, &v))
2195         return false;
2196       namcopy = savestring (name, namlen);
2197       if (type == NULL)
2198         type = debug_make_void_type (dhandle);
2199       if (pvar != NULL)
2200         pvar->kind = IEEE_GLOBAL;
2201       return debug_record_variable (dhandle, namcopy, type, DEBUG_GLOBAL, v);
2202
2203     case 9:
2204       /* Variable lifetime information.  */
2205       if (! ieee_read_number (info, pp, &v))
2206         return false;
2207
2208       /* We have no way to record this information.  FIXME.  */
2209       return true;
2210
2211     case 10:
2212       /* Locked register.  The spec says that there are two required
2213          fields, but at least on occasion the MRI compiler only emits
2214          one.  */
2215       if (! ieee_read_number (info, pp, &v)
2216           || ! ieee_read_optional_number (info, pp, &v2, &present))
2217         return false;
2218
2219       /* I think this means a variable that is both in a register and
2220          a frame slot.  We ignore the frame slot.  FIXME.  */
2221
2222       namcopy = savestring (name, namlen);
2223       if (type == NULL)
2224         type = debug_make_void_type (dhandle);
2225       if (pvar != NULL)
2226         pvar->kind = IEEE_LOCAL;
2227       return debug_record_variable (dhandle, namcopy, type, DEBUG_REGISTER, v);
2228
2229     case 11:
2230       /* Reserved for FORTRAN common.  */
2231       ieee_error (info, atn_code_start, "unsupported ATN11");
2232
2233       /* Return true to keep going.  */
2234       return true;
2235
2236     case 12:
2237       /* Based variable.  */
2238       v3 = 0;
2239       v4 = 0x80;
2240       v5 = 0;
2241       if (! ieee_read_number (info, pp, &v)
2242           || ! ieee_read_number (info, pp, &v2)
2243           || ! ieee_read_optional_number (info, pp, &v3, &present))
2244         return false;
2245       if (present)
2246         {
2247           if (! ieee_read_optional_number (info, pp, &v4, &present))
2248             return false;
2249           if (present)
2250             {
2251               if (! ieee_read_optional_number (info, pp, &v5, &present))
2252                 return false;
2253             }
2254         }
2255
2256       /* We have no way to record this information.  FIXME.  */
2257
2258       ieee_error (info, atn_code_start, "unsupported ATN12");
2259
2260       /* Return true to keep going.  */
2261       return true;
2262
2263     case 16:
2264       /* Constant.  The description of this that I have is ambiguous,
2265          so I'm not going to try to implement it.  */
2266       if (! ieee_read_number (info, pp, &v)
2267           || ! ieee_read_optional_number (info, pp, &v2, &present))
2268         return false;
2269       if (present)
2270         {
2271           if (! ieee_read_optional_number (info, pp, &v2, &present))
2272             return false;
2273           if (present)
2274             {
2275               if (! ieee_read_optional_id (info, pp, &name, &namlen, &present))
2276                 return false;
2277             }
2278         }
2279
2280       if ((ieee_record_enum_type) **pp == ieee_e2_first_byte_enum)
2281         {
2282           if (! ieee_require_asn (info, pp, &v3))
2283             return false;
2284         }
2285
2286       return true;
2287
2288     case 19:
2289       /* Static variable from assembler.  */
2290       v2 = 0;
2291       if (! ieee_read_number (info, pp, &v)
2292           || ! ieee_read_optional_number (info, pp, &v2, &present)
2293           || ! ieee_require_asn (info, pp, &v3))
2294         return false;
2295       namcopy = savestring (name, namlen);
2296       /* We don't really handle this correctly.  FIXME.  */
2297       return debug_record_variable (dhandle, namcopy,
2298                                     debug_make_void_type (dhandle),
2299                                     v2 != 0 ? DEBUG_GLOBAL : DEBUG_STATIC,
2300                                     v3);
2301
2302     case 62:
2303       /* Procedure miscellaneous information.  */
2304     case 63:
2305       /* Variable miscellaneous information.  */
2306     case 64:
2307       /* Module miscellaneous information.  */
2308       if (! ieee_read_number (info, pp, &v)
2309           || ! ieee_read_number (info, pp, &v2)
2310           || ! ieee_read_optional_id (info, pp, &name, &namlen, &present))
2311         return false;
2312
2313       if (atn_code == 62 && v == 80)
2314         {
2315           if (present)
2316             {
2317               ieee_error (info, atn_code_start,
2318                           "unexpected string in C++ misc");
2319               return false;
2320             }
2321           return ieee_read_cxx_misc (info, pp, v2);
2322         }
2323
2324       /* We just ignore all of this stuff.  FIXME.  */
2325
2326       for (; v2 > 0; --v2)
2327         {
2328           switch ((ieee_record_enum_type) **pp)
2329             {
2330             default:
2331               ieee_error (info, *pp, "bad misc record");
2332               return false;
2333
2334             case ieee_at_record_enum:
2335               if (! ieee_require_atn65 (info, pp, &name, &namlen))
2336                 return false;
2337               break;
2338
2339             case ieee_e2_first_byte_enum:
2340               if (! ieee_require_asn (info, pp, &v3))
2341                 return false;
2342               break;
2343             }
2344         }
2345
2346       return true;
2347     }
2348
2349   /*NOTREACHED*/
2350 }
2351
2352 /* Handle C++ debugging miscellaneous records.  This is called for
2353    procedure miscellaneous records of type 80.  */
2354
2355 static boolean
2356 ieee_read_cxx_misc (info, pp, count)
2357      struct ieee_info *info;
2358      const bfd_byte **pp;
2359      unsigned long count;
2360 {
2361   const bfd_byte *start;
2362   bfd_vma category;
2363
2364   start = *pp;
2365
2366   /* Get the category of C++ misc record.  */
2367   if (! ieee_require_asn (info, pp, &category))
2368     return false;
2369   --count;
2370
2371   switch (category)
2372     {
2373     default:
2374       ieee_error (info, start, "unrecognized C++ misc record");
2375       return false;
2376
2377     case 'T':
2378       if (! ieee_read_cxx_class (info, pp, count))
2379         return false;
2380       break;
2381
2382     case 'M':
2383       {
2384         bfd_vma flags;
2385         const char *name;
2386         unsigned long namlen;
2387
2388         /* The IEEE spec indicates that the 'M' record only has a
2389            flags field.  The MRI compiler also emits the name of the
2390            function.  */
2391
2392         if (! ieee_require_asn (info, pp, &flags))
2393           return false;
2394         if (*pp < info->pend
2395             && (ieee_record_enum_type) **pp == ieee_at_record_enum)
2396           {
2397             if (! ieee_require_atn65 (info, pp, &name, &namlen))
2398               return false;
2399           }
2400
2401         /* This is emitted for method functions, but I don't think we
2402            care very much.  It might help if it told us useful
2403            information like the class with which this function is
2404            associated, but it doesn't, so it isn't helpful.  */
2405       }
2406       break;
2407
2408     case 'B':
2409       if (! ieee_read_cxx_defaults (info, pp, count))
2410         return false;
2411       break;
2412
2413     case 'z':
2414       {
2415         const char *name, *mangled, *class;
2416         unsigned long namlen, mangledlen, classlen;
2417         bfd_vma control;
2418
2419         /* Pointer to member.  */
2420
2421         if (! ieee_require_atn65 (info, pp, &name, &namlen)
2422             || ! ieee_require_atn65 (info, pp, &mangled, &mangledlen)
2423             || ! ieee_require_atn65 (info, pp, &class, &classlen)
2424             || ! ieee_require_asn (info, pp, &control))
2425           return false;
2426
2427         /* FIXME: We should now track down name and change its type.  */
2428       }
2429       break;
2430
2431     case 'R':
2432       if (! ieee_read_reference (info, pp))
2433         return false;
2434       break;
2435     }
2436
2437   return true;
2438 }
2439
2440 /* Read a C++ class definition.  This is a pmisc type 80 record of
2441    category 'T'.  */
2442
2443 static boolean
2444 ieee_read_cxx_class (info, pp, count)
2445      struct ieee_info *info;
2446      const bfd_byte **pp;
2447      unsigned long count;
2448 {
2449   const bfd_byte *start;
2450   bfd_vma class;
2451   const char *tag;
2452   unsigned long taglen;
2453   struct ieee_tag *it;
2454   PTR dhandle;
2455   debug_field *fields;
2456   unsigned int field_count, field_alloc;
2457   debug_baseclass *baseclasses;
2458   unsigned int baseclasses_count, baseclasses_alloc;
2459   const debug_field *structfields;
2460   struct ieee_method
2461     {
2462       const char *name;
2463       unsigned long namlen;
2464       debug_method_variant *variants;
2465       unsigned count;
2466       unsigned int alloc;
2467     } *methods;
2468   unsigned int methods_count, methods_alloc;
2469   debug_type vptrbase;
2470   boolean ownvptr;
2471   debug_method *dmethods;
2472
2473   start = *pp;
2474
2475   if (! ieee_require_asn (info, pp, &class))
2476     return false;
2477   --count;
2478
2479   if (! ieee_require_atn65 (info, pp, &tag, &taglen))
2480     return false;
2481   --count;
2482
2483   /* Find the C struct with this name.  */
2484   for (it = info->tags; it != NULL; it = it->next)
2485     if (it->name[0] == tag[0]
2486         && strncmp (it->name, tag, taglen) == 0
2487         && strlen (it->name) == taglen)
2488       break;
2489   if (it == NULL)
2490     {
2491       ieee_error (info, start, "undefined C++ object");
2492       return false;
2493     }
2494
2495   dhandle = info->dhandle;
2496
2497   fields = NULL;
2498   field_count = 0;
2499   field_alloc = 0;
2500   baseclasses = NULL;
2501   baseclasses_count = 0;
2502   baseclasses_alloc = 0;
2503   methods = NULL;
2504   methods_count = 0;
2505   methods_alloc = 0;
2506   vptrbase = DEBUG_TYPE_NULL;
2507   ownvptr = false;
2508
2509   structfields = debug_get_fields (dhandle, it->type);
2510
2511   while (count > 0)
2512     {
2513       bfd_vma id;
2514       const bfd_byte *spec_start;
2515
2516       spec_start = *pp;
2517
2518       if (! ieee_require_asn (info, pp, &id))
2519         return false;
2520       --count;
2521
2522       switch (id)
2523         {
2524         default:
2525           ieee_error (info, spec_start, "unrecognized C++ object spec");
2526           return false;
2527
2528         case 'b':
2529           {
2530             bfd_vma flags, cinline;
2531             const char *basename, *fieldname;
2532             unsigned long baselen, fieldlen;
2533             char *basecopy;
2534             debug_type basetype;
2535             bfd_vma bitpos;
2536             boolean virtualp;
2537             enum debug_visibility visibility;
2538             debug_baseclass baseclass;
2539
2540             /* This represents a base or friend class.  */
2541
2542             if (! ieee_require_asn (info, pp, &flags)
2543                 || ! ieee_require_atn65 (info, pp, &basename, &baselen)
2544                 || ! ieee_require_asn (info, pp, &cinline)
2545                 || ! ieee_require_atn65 (info, pp, &fieldname, &fieldlen))
2546               return false;
2547             count -= 4;
2548
2549             /* We have no way of recording friend information, so we
2550                just ignore it.  */
2551             if ((flags & BASEFLAGS_FRIEND) != 0)
2552               break;
2553
2554             /* I assume that either all of the members of the
2555                baseclass are included in the object, starting at the
2556                beginning of the object, or that none of them are
2557                included.  */
2558
2559             if ((fieldlen == 0) == (cinline == 0))
2560               {
2561                 ieee_error (info, start, "unsupported C++ object type");
2562                 return false;
2563               }
2564
2565             basecopy = savestring (basename, baselen);
2566             basetype = debug_find_tagged_type (dhandle, basecopy,
2567                                                DEBUG_KIND_ILLEGAL);
2568             free (basecopy);
2569             if (basetype == DEBUG_TYPE_NULL)
2570               {
2571                 ieee_error (info, start, "C++ base class not defined");
2572                 return false;
2573               }
2574
2575             if (fieldlen == 0)
2576               bitpos = 0;
2577             else
2578               {
2579                 const debug_field *pf;
2580
2581                 if (structfields == NULL)
2582                   {
2583                     ieee_error (info, start, "C++ object has no fields");
2584                     return false;
2585                   }
2586
2587                 for (pf = structfields; *pf != DEBUG_FIELD_NULL; pf++)
2588                   {
2589                     const char *fname;
2590
2591                     fname = debug_get_field_name (dhandle, *pf);
2592                     if (fname == NULL)
2593                       return false;
2594                     if (fname[0] == fieldname[0]
2595                         && strncmp (fname, fieldname, fieldlen) == 0
2596                         && strlen (fname) == fieldlen)
2597                       break;
2598                   }
2599                 if (*pf == DEBUG_FIELD_NULL)
2600                   {
2601                     ieee_error (info, start,
2602                                 "C++ base class not found in container");
2603                     return false;
2604                   }
2605
2606                 bitpos = debug_get_field_bitpos (dhandle, *pf);
2607               }
2608
2609             if ((flags & BASEFLAGS_VIRTUAL) != 0)
2610               virtualp = true;
2611             else
2612               virtualp = false;
2613             if ((flags & BASEFLAGS_PRIVATE) != 0)
2614               visibility = DEBUG_VISIBILITY_PRIVATE;
2615             else
2616               visibility = DEBUG_VISIBILITY_PUBLIC;
2617
2618             baseclass = debug_make_baseclass (dhandle, basetype, bitpos,
2619                                               virtualp, visibility);
2620             if (baseclass == DEBUG_BASECLASS_NULL)
2621               return false;
2622
2623             if (baseclasses_count + 1 >= baseclasses_alloc)
2624               {
2625                 baseclasses_alloc += 10;
2626                 baseclasses = ((debug_baseclass *)
2627                                xrealloc (baseclasses,
2628                                          (baseclasses_alloc
2629                                           * sizeof *baseclasses)));
2630               }
2631
2632             baseclasses[baseclasses_count] = baseclass;
2633             ++baseclasses_count;
2634             baseclasses[baseclasses_count] = DEBUG_BASECLASS_NULL;
2635           }
2636           break;
2637
2638         case 'd':
2639           {
2640             bfd_vma flags;
2641             const char *fieldname, *mangledname;
2642             unsigned long fieldlen, mangledlen;
2643             char *fieldcopy;
2644             boolean staticp;
2645             debug_type ftype;
2646             const debug_field *pf;
2647             enum debug_visibility visibility;
2648             debug_field field;
2649
2650             /* This represents a data member.  */
2651
2652             if (! ieee_require_asn (info, pp, &flags)
2653                 || ! ieee_require_atn65 (info, pp, &fieldname, &fieldlen)
2654                 || ! ieee_require_atn65 (info, pp, &mangledname, &mangledlen))
2655               return false;
2656             count -= 3;
2657
2658             fieldcopy = savestring (fieldname, fieldlen);
2659
2660             staticp = (flags & CXXFLAGS_STATIC) != 0 ? true : false;
2661
2662             if (staticp)
2663               {
2664                 struct ieee_var *pv, *pvend;
2665
2666                 /* See if we can find a definition for this variable.  */
2667                 pv = info->vars.vars;
2668                 pvend = pv + info->vars.alloc;
2669                 for (; pv < pvend; pv++)
2670                   if (pv->namlen == mangledlen
2671                       && strncmp (pv->name, mangledname, mangledlen) == 0)
2672                     break;
2673                 if (pv < pvend)
2674                   ftype = pv->type;
2675                 else
2676                   {
2677                     /* This can happen if the variable is never used.  */
2678                     ftype = ieee_builtin_type (info, start,
2679                                                (unsigned int) builtin_void);
2680                   }
2681               }
2682             else
2683               {
2684                 unsigned int findx;
2685
2686                 if (structfields == NULL)
2687                   {
2688                     ieee_error (info, start, "C++ object has no fields");
2689                     return false;
2690                   }
2691
2692                 for (pf = structfields, findx = 0;
2693                      *pf != DEBUG_FIELD_NULL;
2694                      pf++, findx++)
2695                   {
2696                     const char *fname;
2697
2698                     fname = debug_get_field_name (dhandle, *pf);
2699                     if (fname == NULL)
2700                       return false;
2701                     if (fname[0] == mangledname[0]
2702                         && strncmp (fname, mangledname, mangledlen) == 0
2703                         && strlen (fname) == mangledlen)
2704                       break;
2705                   }
2706                 if (*pf == DEBUG_FIELD_NULL)
2707                   {
2708                     ieee_error (info, start,
2709                                 "C++ data member not found in container");
2710                     return false;
2711                   }
2712
2713                 ftype = debug_get_field_type (dhandle, *pf);
2714
2715                 if (debug_get_type_kind (dhandle, ftype) == DEBUG_KIND_POINTER)
2716                   {
2717                     /* We might need to convert this field into a
2718                        reference type later on, so make it an indirect
2719                        type.  */
2720                     if (it->fslots == NULL)
2721                       {
2722                         unsigned int fcnt;
2723                         const debug_field *pfcnt;
2724
2725                         fcnt = 0;
2726                         for (pfcnt = structfields;
2727                              *pfcnt != DEBUG_FIELD_NULL;
2728                              pfcnt++)
2729                           ++fcnt;
2730                         it->fslots = ((debug_type *)
2731                                       xmalloc (fcnt * sizeof *it->fslots));
2732                         memset (it->fslots, 0,
2733                                 fcnt * sizeof *it->fslots);
2734                       }
2735
2736                     if (ftype == DEBUG_TYPE_NULL)
2737                       return false;
2738                     it->fslots[findx] = ftype;
2739                     ftype = debug_make_indirect_type (dhandle,
2740                                                       it->fslots + findx,
2741                                                       (const char *) NULL);
2742                   }
2743               }
2744             if (ftype == DEBUG_TYPE_NULL)
2745               return false;
2746
2747             switch (flags & CXXFLAGS_VISIBILITY)
2748               {
2749               default:
2750                 ieee_error (info, start, "unknown C++ visibility");
2751                 return false;
2752
2753               case CXXFLAGS_VISIBILITY_PUBLIC:
2754                 visibility = DEBUG_VISIBILITY_PUBLIC;
2755                 break;
2756
2757               case CXXFLAGS_VISIBILITY_PRIVATE:
2758                 visibility = DEBUG_VISIBILITY_PRIVATE;
2759                 break;
2760
2761               case CXXFLAGS_VISIBILITY_PROTECTED:
2762                 visibility = DEBUG_VISIBILITY_PROTECTED;
2763                 break;
2764               }
2765
2766             if (staticp)
2767               {
2768                 char *mangledcopy;
2769
2770                 mangledcopy = savestring (mangledname, mangledlen);
2771
2772                 field = debug_make_static_member (dhandle, fieldcopy,
2773                                                   ftype, mangledcopy,
2774                                                   visibility);
2775               }
2776             else
2777               {
2778                 bfd_vma bitpos, bitsize;
2779
2780                 bitpos = debug_get_field_bitpos (dhandle, *pf);
2781                 bitsize = debug_get_field_bitsize (dhandle, *pf);
2782                 if (bitpos == (bfd_vma) -1 || bitsize == (bfd_vma) -1)
2783                   {
2784                     ieee_error (info, start, "bad C++ field bit pos or size");
2785                     return false;
2786                   }
2787                 field = debug_make_field (dhandle, fieldcopy, ftype, bitpos,
2788                                           bitsize, visibility);
2789               }
2790
2791             if (field == DEBUG_FIELD_NULL)
2792               return false;
2793
2794             if (field_count + 1 >= field_alloc)
2795               {
2796                 field_alloc += 10;
2797                 fields = ((debug_field *)
2798                           xrealloc (fields, field_alloc * sizeof *fields));
2799               }
2800
2801             fields[field_count] = field;
2802             ++field_count;
2803             fields[field_count] = DEBUG_FIELD_NULL;
2804           }
2805           break;
2806
2807         case 'm':
2808         case 'v':
2809           {
2810             bfd_vma flags, voffset, control;
2811             const char *name, *mangled;
2812             unsigned long namlen, mangledlen;
2813             struct ieee_var *pv, *pvend;
2814             debug_type type;
2815             enum debug_visibility visibility;
2816             boolean constp, volatilep;
2817             char *mangledcopy;
2818             debug_method_variant mv;
2819             struct ieee_method *meth;
2820             unsigned int im;
2821
2822             if (! ieee_require_asn (info, pp, &flags)
2823                 || ! ieee_require_atn65 (info, pp, &name, &namlen)
2824                 || ! ieee_require_atn65 (info, pp, &mangled, &mangledlen))
2825               return false;
2826             count -= 3;
2827             if (id != 'v')
2828               voffset = 0;
2829             else
2830               {
2831                 if (! ieee_require_asn (info, pp, &voffset))
2832                   return false;
2833                 --count;
2834               }
2835             if (! ieee_require_asn (info, pp, &control))
2836               return false;
2837             --count;
2838
2839             /* We just ignore the control information.  */
2840
2841             /* We have no way to represent friend information, so we
2842                just ignore it.  */
2843             if ((flags & CXXFLAGS_FRIEND) != 0)
2844               break;
2845
2846             /* We should already have seen a type for the function.  */
2847             pv = info->vars.vars;
2848             pvend = pv + info->vars.alloc;
2849             for (; pv < pvend; pv++)
2850               if (pv->namlen == mangledlen
2851                   && strncmp (pv->name, mangled, mangledlen) == 0)
2852                 break;
2853
2854             if (pv >= pvend)
2855               {
2856                 /* We won't have type information for this function if
2857                    it is not included in this file.  We don't try to
2858                    handle this case.  FIXME.  */
2859                 type = (debug_make_function_type
2860                         (dhandle,
2861                          ieee_builtin_type (info, start,
2862                                             (unsigned int) builtin_void),
2863                          (debug_type *) NULL,
2864                          false));
2865               }
2866             else
2867               {
2868                 debug_type return_type;
2869                 const debug_type *arg_types;
2870                 boolean varargs;
2871
2872                 if (debug_get_type_kind (dhandle, pv->type)
2873                     != DEBUG_KIND_FUNCTION)
2874                   {
2875                     ieee_error (info, start,
2876                                 "bad type for C++ method function");
2877                     return false;
2878                   }
2879
2880                 return_type = debug_get_return_type (dhandle, pv->type);
2881                 arg_types = debug_get_parameter_types (dhandle, pv->type,
2882                                                        &varargs);
2883                 if (return_type == DEBUG_TYPE_NULL || arg_types == NULL)
2884                   {
2885                     ieee_error (info, start,
2886                                 "no type information for C++ method function");
2887                     return false;
2888                   }
2889
2890                 type = debug_make_method_type (dhandle, return_type, it->type,
2891                                                (debug_type *) arg_types,
2892                                                varargs);
2893               }
2894             if (type == DEBUG_TYPE_NULL)
2895               return false;
2896
2897             switch (flags & CXXFLAGS_VISIBILITY)
2898               {
2899               default:
2900                 ieee_error (info, start, "unknown C++ visibility");
2901                 return false;
2902
2903               case CXXFLAGS_VISIBILITY_PUBLIC:
2904                 visibility = DEBUG_VISIBILITY_PUBLIC;
2905                 break;
2906
2907               case CXXFLAGS_VISIBILITY_PRIVATE:
2908                 visibility = DEBUG_VISIBILITY_PRIVATE;
2909                 break;
2910
2911               case CXXFLAGS_VISIBILITY_PROTECTED:
2912                 visibility = DEBUG_VISIBILITY_PROTECTED;
2913                 break;
2914               }
2915
2916             constp = (flags & CXXFLAGS_CONST) != 0 ? true : false;
2917             volatilep = (flags & CXXFLAGS_VOLATILE) != 0 ? true : false;
2918
2919             mangledcopy = savestring (mangled, mangledlen);
2920
2921             if ((flags & CXXFLAGS_STATIC) != 0)
2922               {
2923                 if (id == 'v')
2924                   {
2925                     ieee_error (info, start, "C++ static virtual method");
2926                     return false;
2927                   }
2928                 mv = debug_make_static_method_variant (dhandle, mangledcopy,
2929                                                        type, visibility,
2930                                                        constp, volatilep);
2931               }
2932             else
2933               {
2934                 debug_type vcontext;
2935
2936                 if (id != 'v')
2937                   vcontext = DEBUG_TYPE_NULL;
2938                 else
2939                   {
2940                     /* FIXME: How can we calculate this correctly?  */
2941                     vcontext = it->type;
2942                   }
2943                 mv = debug_make_method_variant (dhandle, mangledcopy, type,
2944                                                 visibility, constp,
2945                                                 volatilep, voffset,
2946                                                 vcontext);
2947               }
2948             if (mv == DEBUG_METHOD_VARIANT_NULL)
2949               return false;
2950
2951             for (meth = methods, im = 0; im < methods_count; meth++, im++)
2952               if (meth->namlen == namlen
2953                   && strncmp (meth->name, name, namlen) == 0)
2954                 break;
2955             if (im >= methods_count)
2956               {
2957                 if (methods_count >= methods_alloc)
2958                   {
2959                     methods_alloc += 10;
2960                     methods = ((struct ieee_method *)
2961                                xrealloc (methods,
2962                                          methods_alloc * sizeof *methods));
2963                   }
2964                 methods[methods_count].name = name;
2965                 methods[methods_count].namlen = namlen;
2966                 methods[methods_count].variants = NULL;
2967                 methods[methods_count].count = 0;
2968                 methods[methods_count].alloc = 0;
2969                 meth = methods + methods_count;
2970                 ++methods_count;
2971               }
2972
2973             if (meth->count + 1 >= meth->alloc)
2974               {
2975                 meth->alloc += 10;
2976                 meth->variants = ((debug_method_variant *)
2977                                   xrealloc (meth->variants,
2978                                             (meth->alloc
2979                                              * sizeof *meth->variants)));
2980               }
2981
2982             meth->variants[meth->count] = mv;
2983             ++meth->count;
2984             meth->variants[meth->count] = DEBUG_METHOD_VARIANT_NULL;
2985           }
2986           break;
2987
2988         case 'o':
2989           {
2990             bfd_vma spec;
2991
2992             /* We have no way to store this information, so we just
2993                ignore it.  */
2994             if (! ieee_require_asn (info, pp, &spec))
2995               return false;
2996             --count;
2997             if ((spec & 4) != 0)
2998               {
2999                 const char *filename;
3000                 unsigned long filenamlen;
3001                 bfd_vma lineno;
3002
3003                 if (! ieee_require_atn65 (info, pp, &filename, &filenamlen)
3004                     || ! ieee_require_asn (info, pp, &lineno))
3005                   return false;
3006                 count -= 2;
3007               }
3008             else if ((spec & 8) != 0)
3009               {
3010                 const char *mangled;
3011                 unsigned long mangledlen;
3012
3013                 if (! ieee_require_atn65 (info, pp, &mangled, &mangledlen))
3014                   return false;
3015                 --count;
3016               }
3017             else
3018               {
3019                 ieee_error (info, start,
3020                             "unrecognized C++ object overhead spec");
3021                 return false;
3022               }
3023           }
3024           break;
3025
3026         case 'z':
3027           {
3028             const char *vname, *basename;
3029             unsigned long vnamelen, baselen;
3030             bfd_vma vsize, control;
3031
3032             /* A virtual table pointer.  */
3033
3034             if (! ieee_require_atn65 (info, pp, &vname, &vnamelen)
3035                 || ! ieee_require_asn (info, pp, &vsize)
3036                 || ! ieee_require_atn65 (info, pp, &basename, &baselen)
3037                 || ! ieee_require_asn (info, pp, &control))
3038               return false;
3039             count -= 4;
3040
3041             /* We just ignore the control number.  We don't care what
3042                the virtual table name is.  We have no way to store the
3043                virtual table size, and I don't think we care anyhow.  */
3044
3045             /* FIXME: We can't handle multiple virtual table pointers.  */
3046
3047             if (baselen == 0)
3048               ownvptr = true;
3049             else
3050               {
3051                 char *basecopy;
3052
3053                 basecopy = savestring (basename, baselen);
3054                 vptrbase = debug_find_tagged_type (dhandle, basecopy,
3055                                                    DEBUG_KIND_ILLEGAL);
3056                 free (basecopy);
3057                 if (vptrbase == DEBUG_TYPE_NULL)
3058                   {
3059                     ieee_error (info, start, "undefined C++ vtable");
3060                     return false;
3061                   }
3062               }
3063           }
3064           break;
3065         }
3066     }
3067
3068   /* Now that we have seen all the method variants, we can call
3069      debug_make_method for each one.  */
3070
3071   if (methods_count == 0)
3072     dmethods = NULL;
3073   else
3074     {
3075       unsigned int i;
3076
3077       dmethods = ((debug_method *)
3078                   xmalloc ((methods_count + 1) * sizeof *dmethods));
3079       for (i = 0; i < methods_count; i++)
3080         {
3081           char *namcopy;
3082
3083           namcopy = savestring (methods[i].name, methods[i].namlen);
3084           dmethods[i] = debug_make_method (dhandle, namcopy,
3085                                            methods[i].variants);
3086           if (dmethods[i] == DEBUG_METHOD_NULL)
3087             return false;
3088         }
3089       dmethods[i] = DEBUG_METHOD_NULL;
3090       free (methods);
3091     }
3092
3093   /* The struct type was created as an indirect type pointing at
3094      it->slot.  We update it->slot to automatically update all
3095      references to this struct.  */
3096   it->slot = debug_make_object_type (dhandle,
3097                                      class != 'u',
3098                                      debug_get_type_size (dhandle,
3099                                                           it->slot),
3100                                      fields, baseclasses, dmethods,
3101                                      vptrbase, ownvptr);
3102   if (it->slot == DEBUG_TYPE_NULL)
3103     return false;
3104
3105   return true;
3106 }
3107
3108 /* Read C++ default argument value and reference type information.  */
3109
3110 static boolean
3111 ieee_read_cxx_defaults (info, pp, count)
3112      struct ieee_info *info;
3113      const bfd_byte **pp;
3114      unsigned long count;
3115 {
3116   const bfd_byte *start;
3117   const char *fnname;
3118   unsigned long fnlen;
3119   bfd_vma defcount;
3120
3121   start = *pp;
3122
3123   /* Giving the function name before the argument count is an addendum
3124      to the spec.  The function name is demangled, though, so this
3125      record must always refer to the current function.  */
3126
3127   if (info->blockstack.bsp <= info->blockstack.stack
3128       || info->blockstack.bsp[-1].fnindx == (unsigned int) -1)
3129     {
3130       ieee_error (info, start, "C++ default values not in a function");
3131       return false;
3132     }
3133
3134   if (! ieee_require_atn65 (info, pp, &fnname, &fnlen)
3135       || ! ieee_require_asn (info, pp, &defcount))
3136     return false;
3137   count -= 2;
3138
3139   while (defcount-- > 0)
3140     {
3141       bfd_vma type, val;
3142       const char *strval;
3143       unsigned long strvallen;
3144
3145       if (! ieee_require_asn (info, pp, &type))
3146         return false;
3147       --count;
3148
3149       switch (type)
3150         {
3151         case 0:
3152         case 4:
3153           break;
3154
3155         case 1:
3156         case 2:
3157           if (! ieee_require_asn (info, pp, &val))
3158             return false;
3159           --count;
3160           break;
3161
3162         case 3:
3163         case 7:
3164           if (! ieee_require_atn65 (info, pp, &strval, &strvallen))
3165             return false;
3166           --count;
3167           break;
3168
3169         default:
3170           ieee_error (info, start, "unrecognized C++ default type");
3171           return false;
3172         }
3173
3174       /* We have no way to record the default argument values, so we
3175          just ignore them.  FIXME.  */
3176     }
3177
3178   /* Any remaining arguments are indices of parameters that are really
3179      reference type.  */
3180   if (count > 0)
3181     {
3182       PTR dhandle;
3183       debug_type *arg_slots;
3184
3185       dhandle = info->dhandle;
3186       arg_slots = info->types.types[info->blockstack.bsp[-1].fnindx].arg_slots;
3187       while (count-- > 0)
3188         {
3189           bfd_vma indx;
3190           debug_type target;
3191
3192           if (! ieee_require_asn (info, pp, &indx))
3193             return false;
3194           /* The index is 1 based.  */
3195           --indx;
3196           if (arg_slots == NULL
3197               || arg_slots[indx] == DEBUG_TYPE_NULL
3198               || (debug_get_type_kind (dhandle, arg_slots[indx])
3199                   != DEBUG_KIND_POINTER))
3200             {
3201               ieee_error (info, start, "reference parameter is not a pointer");
3202               return false;
3203             }
3204
3205           target = debug_get_target_type (dhandle, arg_slots[indx]);
3206           arg_slots[indx] = debug_make_reference_type (dhandle, target);
3207           if (arg_slots[indx] == DEBUG_TYPE_NULL)
3208             return false;
3209         }
3210     }
3211
3212   return true;
3213 }
3214
3215 /* Read a C++ reference definition.  */
3216
3217 static boolean
3218 ieee_read_reference (info, pp)
3219      struct ieee_info *info;
3220      const bfd_byte **pp;
3221 {
3222   const bfd_byte *start;
3223   bfd_vma flags;
3224   const char *class, *name;
3225   unsigned long classlen, namlen;
3226   debug_type *pslot;
3227   debug_type target;
3228
3229   start = *pp;
3230
3231   if (! ieee_require_asn (info, pp, &flags))
3232     return false;
3233
3234   /* Giving the class name before the member name is in an addendum to
3235      the spec.  */
3236   if (flags == 3)
3237     {
3238       if (! ieee_require_atn65 (info, pp, &class, &classlen))
3239         return false;
3240     }
3241
3242   if (! ieee_require_atn65 (info, pp, &name, &namlen))
3243     return false;
3244
3245   pslot = NULL;
3246   if (flags != 3)
3247     {
3248       int i;
3249       struct ieee_var *pv = NULL;
3250
3251       /* We search from the last variable indices to the first in
3252          hopes of finding local variables correctly.  FIXME: This
3253          probably won't work in all cases.  On the other hand, I don't
3254          know what will.  */
3255       for (i = (int) info->vars.alloc - 1; i >= 0; i--)
3256         {
3257           boolean found;
3258
3259           pv = info->vars.vars + i;
3260
3261           if (pv->pslot == NULL
3262               || pv->namlen != namlen
3263               || strncmp (pv->name, name, namlen) != 0)
3264             continue;
3265
3266           found = false;
3267           switch (flags)
3268             {
3269             default:
3270               ieee_error (info, start,
3271                           "unrecognized C++ reference type");
3272               return false;
3273
3274             case 0:
3275               /* Global variable or function.  */
3276               if (pv->kind == IEEE_GLOBAL
3277                   || pv->kind == IEEE_EXTERNAL
3278                   || pv->kind == IEEE_FUNCTION)
3279                 found = true;
3280               break;
3281
3282             case 1:
3283               /* Global static variable or function.  */
3284               if (pv->kind == IEEE_STATIC
3285                   || pv->kind == IEEE_FUNCTION)
3286                 found = true;
3287               break;
3288
3289             case 2:
3290               /* Local variable.  */
3291               if (pv->kind == IEEE_LOCAL)
3292                 found = true;
3293               break;
3294             }
3295
3296           if (found)
3297             break;
3298         }
3299
3300       if (i >= 0)
3301         pslot = pv->pslot;
3302     }
3303   else
3304     {
3305       struct ieee_tag *it;
3306
3307       for (it = info->tags; it != NULL; it = it->next)
3308         {
3309           if (it->name[0] == class[0]
3310               && strncmp (it->name, class, classlen) == 0
3311               && strlen (it->name) == classlen)
3312             {
3313               if (it->fslots != NULL)
3314                 {
3315                   const debug_field *pf;
3316                   unsigned int findx;
3317
3318                   pf = debug_get_fields (info->dhandle, it->type);
3319                   if (pf == NULL)
3320                     {
3321                       ieee_error (info, start,
3322                                   "C++ reference in class with no fields");
3323                       return false;
3324                     }
3325
3326                   for (findx = 0; *pf != DEBUG_FIELD_NULL; pf++, findx++)
3327                     {
3328                       const char *fname;
3329
3330                       fname = debug_get_field_name (info->dhandle, *pf);
3331                       if (fname == NULL)
3332                         return false;
3333                       if (strncmp (fname, name, namlen) == 0
3334                           && strlen (fname) == namlen)
3335                         {
3336                           pslot = it->fslots + findx;
3337                           break;
3338                         }
3339                     }
3340                 }
3341
3342               break;
3343             }
3344         }
3345     }
3346
3347   if (pslot == NULL)
3348     {
3349       ieee_error (info, start, "C++ reference not found");
3350       return false;
3351     }
3352
3353   /* We allocated the type of the object as an indirect type pointing
3354      to *pslot, which we can now update to be a reference type.  */
3355   if (debug_get_type_kind (info->dhandle, *pslot) != DEBUG_KIND_POINTER)
3356     {
3357       ieee_error (info, start, "C++ reference is not pointer");
3358       return false;
3359     }
3360
3361   target = debug_get_target_type (info->dhandle, *pslot);
3362   *pslot = debug_make_reference_type (info->dhandle, target);
3363   if (*pslot == DEBUG_TYPE_NULL)
3364     return false;
3365
3366   return true;
3367 }
3368
3369 /* Require an ASN record.  */
3370
3371 static boolean
3372 ieee_require_asn (info, pp, pv)
3373      struct ieee_info *info;
3374      const bfd_byte **pp;
3375      bfd_vma *pv;
3376 {
3377   const bfd_byte *start;
3378   ieee_record_enum_type c;
3379   bfd_vma varindx;
3380
3381   start = *pp;
3382
3383   c = (ieee_record_enum_type) **pp;
3384   if (c != ieee_e2_first_byte_enum)
3385     {
3386       ieee_error (info, start, "missing required ASN");
3387       return false;
3388     }
3389   ++*pp;
3390
3391   c = (ieee_record_enum_type) (((unsigned int) c << 8) | **pp);
3392   if (c != ieee_asn_record_enum)
3393     {
3394       ieee_error (info, start, "missing required ASN");
3395       return false;
3396     }
3397   ++*pp;
3398
3399   /* Just ignore the variable index.  */
3400   if (! ieee_read_number (info, pp, &varindx))
3401     return false;
3402
3403   return ieee_read_expression (info, pp, pv);
3404 }
3405
3406 /* Require an ATN65 record.  */
3407
3408 static boolean
3409 ieee_require_atn65 (info, pp, pname, pnamlen)
3410      struct ieee_info *info;
3411      const bfd_byte **pp;
3412      const char **pname;
3413      unsigned long *pnamlen;
3414 {
3415   const bfd_byte *start;
3416   ieee_record_enum_type c;
3417   bfd_vma name_indx, type_indx, atn_code;
3418
3419   start = *pp;
3420
3421   c = (ieee_record_enum_type) **pp;
3422   if (c != ieee_at_record_enum)
3423     {
3424       ieee_error (info, start, "missing required ATN65");
3425       return false;
3426     }
3427   ++*pp;
3428
3429   c = (ieee_record_enum_type) (((unsigned int) c << 8) | **pp);
3430   if (c != ieee_atn_record_enum)
3431     {
3432       ieee_error (info, start, "missing required ATN65");
3433       return false;
3434     }
3435   ++*pp;
3436
3437   if (! ieee_read_number (info, pp, &name_indx)
3438       || ! ieee_read_number (info, pp, &type_indx)
3439       || ! ieee_read_number (info, pp, &atn_code))
3440     return false;
3441
3442   /* Just ignore name_indx.  */
3443
3444   if (type_indx != 0 || atn_code != 65)
3445     {
3446       ieee_error (info, start, "bad ATN65 record");
3447       return false;
3448     }
3449
3450   return ieee_read_id (info, pp, pname, pnamlen);
3451 }
3452 \f
3453 /* Convert a register number in IEEE debugging information into a
3454    generic register number.  */
3455
3456 static int
3457 ieee_regno_to_genreg (abfd, r)
3458      bfd *abfd;
3459      int r;
3460 {
3461   switch (bfd_get_arch (abfd))
3462     {
3463     case bfd_arch_m68k:
3464       /* For some reasons stabs adds 2 to the floating point register
3465          numbers.  */
3466       if (r >= 16)
3467         r += 2;
3468       break;
3469
3470     case bfd_arch_i960:
3471       /* Stabs uses 0 to 15 for r0 to r15, 16 to 31 for g0 to g15, and
3472          32 to 35 for fp0 to fp3.  */
3473       --r;
3474       break;
3475
3476     default:
3477       break;
3478     }
3479
3480   return r;
3481 }
3482
3483 /* Convert a generic register number to an IEEE specific one.  */
3484
3485 static int
3486 ieee_genreg_to_regno (abfd, r)
3487      bfd *abfd;
3488      int r;
3489 {
3490   switch (bfd_get_arch (abfd))
3491     {
3492     case bfd_arch_m68k:
3493       /* For some reason stabs add 2 to the floating point register
3494          numbers.  */
3495       if (r >= 18)
3496         r -= 2;
3497       break;
3498
3499     case bfd_arch_i960:
3500       /* Stabs uses 0 to 15 for r0 to r15, 16 to 31 for g0 to g15, and
3501          32 to 35 for fp0 to fp3.  */
3502       ++r;
3503       break;
3504
3505     default:
3506       break;
3507     }
3508
3509   return r;
3510 }
3511 \f
3512 /* These routines build IEEE debugging information out of the generic
3513    debugging information.  */
3514
3515 /* We build the IEEE debugging information byte by byte.  Rather than
3516    waste time copying data around, we use a linked list of buffers to
3517    hold the data.  */
3518
3519 #define IEEE_BUFSIZE (490)
3520
3521 struct ieee_buf
3522 {
3523   /* Next buffer.  */
3524   struct ieee_buf *next;
3525   /* Number of data bytes in this buffer.  */
3526   unsigned int c;
3527   /* Bytes.  */
3528   bfd_byte buf[IEEE_BUFSIZE];
3529 };
3530
3531 /* A list of buffers.  */
3532
3533 struct ieee_buflist
3534 {
3535   /* Head of list.  */
3536   struct ieee_buf *head;
3537   /* Tail--last buffer on list.  */
3538   struct ieee_buf *tail;
3539 };
3540
3541 /* In order to generate the BB11 blocks required by the HP emulator,
3542    we keep track of ranges of addresses which correspond to a given
3543    compilation unit.  */
3544
3545 struct ieee_range
3546 {
3547   /* Next range.  */
3548   struct ieee_range *next;
3549   /* Low address.  */
3550   bfd_vma low;
3551   /* High address.  */
3552   bfd_vma high;
3553 };
3554
3555 /* This structure holds information for a class on the type stack.  */
3556
3557 struct ieee_type_class
3558 {
3559   /* The name index in the debugging information.  */
3560   unsigned int indx;
3561   /* The pmisc records for the class.  */
3562   struct ieee_buflist pmiscbuf;
3563   /* The number of pmisc records.  */
3564   unsigned int pmisccount;
3565   /* The name of the class holding the virtual table, if not this
3566      class.  */
3567   const char *vclass;
3568   /* Whether this class holds its own virtual table.  */
3569   boolean ownvptr;
3570   /* The largest virtual table offset seen so far.  */
3571   bfd_vma voffset;
3572   /* The current method.  */
3573   const char *method;
3574   /* Additional pmisc records used to record fields of reference type.  */
3575   struct ieee_buflist refs;
3576 };
3577
3578 /* This is how we store types for the writing routines.  Most types
3579    are simply represented by a type index.  */
3580
3581 struct ieee_write_type
3582 {
3583   /* Type index.  */
3584   unsigned int indx;
3585   /* The size of the type, if known.  */
3586   unsigned int size;
3587   /* The name of the type, if any.  */
3588   const char *name;
3589   /* If this is a function or method type, we build the type here, and
3590      only add it to the output buffers if we need it.  */
3591   struct ieee_buflist fndef;
3592   /* If this is a struct, this is where the struct definition is
3593      built.  */
3594   struct ieee_buflist strdef;
3595   /* If this is a class, this is where the class information is built.  */
3596   struct ieee_type_class *classdef;
3597   /* Whether the type is unsigned.  */
3598   unsigned int unsignedp : 1;
3599   /* Whether this is a reference type.  */
3600   unsigned int referencep : 1;
3601   /* Whether this is in the local type block.  */
3602   unsigned int localp : 1;
3603   /* Whether this is a duplicate struct definition which we are
3604      ignoring.  */
3605   unsigned int ignorep : 1;
3606 };
3607
3608 /* This is the type stack used by the debug writing routines.  FIXME:
3609    We could generate more efficient output if we remembered when we
3610    have output a particular type before.  */
3611
3612 struct ieee_type_stack
3613 {
3614   /* Next entry on stack.  */
3615   struct ieee_type_stack *next;
3616   /* Type information.  */
3617   struct ieee_write_type type;
3618 };
3619
3620 /* This is a list of associations between a name and some types.
3621    These are used for typedefs and tags.  */
3622
3623 struct ieee_name_type
3624 {
3625   /* Next type for this name.  */
3626   struct ieee_name_type *next;
3627   /* ID number.  For a typedef, this is the index of the type to which
3628      this name is typedefed.  */
3629   unsigned int id;
3630   /* Type.  */
3631   struct ieee_write_type type;
3632   /* If this is a tag which has not yet been defined, this is the
3633      kind.  If the tag has been defined, this is DEBUG_KIND_ILLEGAL.  */
3634   enum debug_type_kind kind;
3635 };
3636
3637 /* We use a hash table to associate names and types.  */
3638
3639 struct ieee_name_type_hash_table
3640 {
3641   struct bfd_hash_table root;
3642 };
3643
3644 struct ieee_name_type_hash_entry
3645 {
3646   struct bfd_hash_entry root;
3647   /* Information for this name.  */
3648   struct ieee_name_type *types;
3649 };
3650
3651 /* This is a list of enums.  */
3652
3653 struct ieee_defined_enum
3654 {
3655   /* Next enum.  */
3656   struct ieee_defined_enum *next;
3657   /* Type index.  */
3658   unsigned int indx;
3659   /* Tag.  */
3660   const char *tag;
3661   /* Names.  */
3662   const char **names;
3663   /* Values.  */
3664   bfd_signed_vma *vals;
3665 };
3666
3667 /* We keep a list of modified versions of types, so that we don't
3668    output them more than once.  */
3669
3670 struct ieee_modified_type
3671 {
3672   /* Pointer to this type.  */
3673   unsigned int pointer;
3674   /* Function with unknown arguments returning this type.  */
3675   unsigned int function;
3676   /* Const version of this type.  */
3677   unsigned int const_qualified;
3678   /* Volatile version of this type.  */
3679   unsigned int volatile_qualified;
3680   /* List of arrays of this type of various bounds.  */
3681   struct ieee_modified_array_type *arrays;
3682 };
3683
3684 /* A list of arrays bounds.  */
3685
3686 struct ieee_modified_array_type
3687 {
3688   /* Next array bounds.  */
3689   struct ieee_modified_array_type *next;
3690   /* Type index with these bounds.  */
3691   unsigned int indx;
3692   /* Low bound.  */
3693   bfd_signed_vma low;
3694   /* High bound.  */
3695   bfd_signed_vma high;
3696 };
3697
3698 /* This is a list of pending function parameter information.  We don't
3699    output them until we see the first block.  */
3700
3701 struct ieee_pending_parm
3702 {
3703   /* Next pending parameter.  */
3704   struct ieee_pending_parm *next;
3705   /* Name.  */
3706   const char *name;
3707   /* Type index.  */
3708   unsigned int type;
3709   /* Whether the type is a reference.  */
3710   boolean referencep;
3711   /* Kind.  */
3712   enum debug_parm_kind kind;
3713   /* Value.  */
3714   bfd_vma val;
3715 };
3716
3717 /* This is the handle passed down by debug_write.  */
3718
3719 struct ieee_handle
3720 {
3721   /* BFD we are writing to.  */
3722   bfd *abfd;
3723   /* Whether we got an error in a subroutine called via traverse or
3724      map_over_sections.  */
3725   boolean error;
3726   /* Current data buffer list.  */
3727   struct ieee_buflist *current;
3728   /* Current data buffer.  */
3729   struct ieee_buf *curbuf;
3730   /* Filename of current compilation unit.  */
3731   const char *filename;
3732   /* Module name of current compilation unit.  */
3733   const char *modname;
3734   /* List of buffer for global types.  */
3735   struct ieee_buflist global_types;
3736   /* List of finished data buffers.  */
3737   struct ieee_buflist data;
3738   /* List of buffers for typedefs in the current compilation unit.  */
3739   struct ieee_buflist types;
3740   /* List of buffers for variables and functions in the current
3741      compilation unit.  */
3742   struct ieee_buflist vars;
3743   /* List of buffers for C++ class definitions in the current
3744      compilation unit.  */
3745   struct ieee_buflist cxx;
3746   /* List of buffers for line numbers in the current compilation unit.  */
3747   struct ieee_buflist linenos;
3748   /* Ranges for the current compilation unit.  */
3749   struct ieee_range *ranges;
3750   /* Ranges for all debugging information.  */
3751   struct ieee_range *global_ranges;
3752   /* Nested pending ranges.  */
3753   struct ieee_range *pending_ranges;
3754   /* Type stack.  */
3755   struct ieee_type_stack *type_stack;
3756   /* Next unallocated type index.  */
3757   unsigned int type_indx;
3758   /* Next unallocated name index.  */
3759   unsigned int name_indx;
3760   /* Typedefs.  */
3761   struct ieee_name_type_hash_table typedefs;
3762   /* Tags.  */
3763   struct ieee_name_type_hash_table tags;
3764   /* Enums.  */
3765   struct ieee_defined_enum *enums;
3766   /* Modified versions of types.  */
3767   struct ieee_modified_type *modified;
3768   /* Number of entries allocated in modified.  */
3769   unsigned int modified_alloc;
3770   /* 4 byte complex type.  */
3771   unsigned int complex_float_index;
3772   /* 8 byte complex type.  */
3773   unsigned int complex_double_index;
3774   /* The depth of block nesting.  This is 0 outside a function, and 1
3775      just after start_function is called.  */
3776   unsigned int block_depth;
3777   /* The name of the current function.  */
3778   const char *fnname;
3779   /* List of buffers for the type of the function we are currently
3780      writing out.  */
3781   struct ieee_buflist fntype;
3782   /* List of buffers for the parameters of the function we are
3783      currently writing out.  */
3784   struct ieee_buflist fnargs;
3785   /* Number of arguments written to fnargs.  */
3786   unsigned int fnargcount;
3787   /* Pending function parameters.  */
3788   struct ieee_pending_parm *pending_parms;
3789   /* Current line number filename.  */
3790   const char *lineno_filename;
3791   /* Line number name index.  */
3792   unsigned int lineno_name_indx;
3793   /* Filename of pending line number.  */
3794   const char *pending_lineno_filename;
3795   /* Pending line number.  */
3796   unsigned long pending_lineno;
3797   /* Address of pending line number.  */
3798   bfd_vma pending_lineno_addr;
3799   /* Highest address seen at end of procedure.  */
3800   bfd_vma highaddr;
3801 };
3802
3803 static boolean ieee_init_buffer
3804   PARAMS ((struct ieee_handle *, struct ieee_buflist *));
3805 static boolean ieee_change_buffer
3806   PARAMS ((struct ieee_handle *, struct ieee_buflist *));
3807 static boolean ieee_append_buffer
3808   PARAMS ((struct ieee_handle *, struct ieee_buflist *,
3809            struct ieee_buflist *));
3810 static boolean ieee_real_write_byte PARAMS ((struct ieee_handle *, int));
3811 static boolean ieee_write_2bytes PARAMS ((struct ieee_handle *, int));
3812 static boolean ieee_write_number PARAMS ((struct ieee_handle *, bfd_vma));
3813 static boolean ieee_write_id PARAMS ((struct ieee_handle *, const char *));
3814 static boolean ieee_write_asn
3815   PARAMS ((struct ieee_handle *, unsigned int, bfd_vma));
3816 static boolean ieee_write_atn65
3817   PARAMS ((struct ieee_handle *, unsigned int, const char *));
3818 static boolean ieee_push_type
3819   PARAMS ((struct ieee_handle *, unsigned int, unsigned int, boolean,
3820            boolean));
3821 static unsigned int ieee_pop_type PARAMS ((struct ieee_handle *));
3822 static void ieee_pop_unused_type PARAMS ((struct ieee_handle *));
3823 static unsigned int ieee_pop_type_used
3824   PARAMS ((struct ieee_handle *, boolean));
3825 static boolean ieee_add_range
3826   PARAMS ((struct ieee_handle *, boolean, bfd_vma, bfd_vma));
3827 static boolean ieee_start_range PARAMS ((struct ieee_handle *, bfd_vma));
3828 static boolean ieee_end_range PARAMS ((struct ieee_handle *, bfd_vma));
3829 static boolean ieee_define_type
3830   PARAMS ((struct ieee_handle *, unsigned int, boolean, boolean));
3831 static boolean ieee_define_named_type
3832   PARAMS ((struct ieee_handle *, const char *, unsigned int, unsigned int,
3833            boolean, boolean, struct ieee_buflist *));
3834 static struct ieee_modified_type *ieee_get_modified_info
3835   PARAMS ((struct ieee_handle *, unsigned int));
3836 static struct bfd_hash_entry *ieee_name_type_newfunc
3837   PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
3838 static boolean ieee_write_undefined_tag
3839   PARAMS ((struct ieee_name_type_hash_entry *, PTR));
3840 static boolean ieee_finish_compilation_unit PARAMS ((struct ieee_handle *));
3841 static void ieee_add_bb11_blocks PARAMS ((bfd *, asection *, PTR));
3842 static boolean ieee_add_bb11
3843   PARAMS ((struct ieee_handle *, asection *, bfd_vma, bfd_vma));
3844 static boolean ieee_output_pending_parms PARAMS ((struct ieee_handle *));
3845 static unsigned int ieee_vis_to_flags PARAMS ((enum debug_visibility));
3846 static boolean ieee_class_method_var
3847   PARAMS ((struct ieee_handle *, const char *, enum debug_visibility, boolean,
3848            boolean, boolean, bfd_vma, boolean));
3849
3850 static boolean ieee_start_compilation_unit PARAMS ((PTR, const char *));
3851 static boolean ieee_start_source PARAMS ((PTR, const char *));
3852 static boolean ieee_empty_type PARAMS ((PTR));
3853 static boolean ieee_void_type PARAMS ((PTR));
3854 static boolean ieee_int_type PARAMS ((PTR, unsigned int, boolean));
3855 static boolean ieee_float_type PARAMS ((PTR, unsigned int));
3856 static boolean ieee_complex_type PARAMS ((PTR, unsigned int));
3857 static boolean ieee_bool_type PARAMS ((PTR, unsigned int));
3858 static boolean ieee_enum_type
3859   PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
3860 static boolean ieee_pointer_type PARAMS ((PTR));
3861 static boolean ieee_function_type PARAMS ((PTR, int, boolean));
3862 static boolean ieee_reference_type PARAMS ((PTR));
3863 static boolean ieee_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
3864 static boolean ieee_array_type
3865   PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean));
3866 static boolean ieee_set_type PARAMS ((PTR, boolean));
3867 static boolean ieee_offset_type PARAMS ((PTR));
3868 static boolean ieee_method_type PARAMS ((PTR, boolean, int, boolean));
3869 static boolean ieee_const_type PARAMS ((PTR));
3870 static boolean ieee_volatile_type PARAMS ((PTR));
3871 static boolean ieee_start_struct_type
3872   PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int));
3873 static boolean ieee_struct_field
3874   PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
3875 static boolean ieee_end_struct_type PARAMS ((PTR));
3876 static boolean ieee_start_class_type
3877   PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int, boolean,
3878            boolean));
3879 static boolean ieee_class_static_member
3880   PARAMS ((PTR, const char *, const char *, enum debug_visibility));
3881 static boolean ieee_class_baseclass
3882   PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility));
3883 static boolean ieee_class_start_method PARAMS ((PTR, const char *));
3884 static boolean ieee_class_method_variant
3885   PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean,
3886            bfd_vma, boolean));
3887 static boolean ieee_class_static_method_variant
3888   PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean));
3889 static boolean ieee_class_end_method PARAMS ((PTR));
3890 static boolean ieee_end_class_type PARAMS ((PTR));
3891 static boolean ieee_typedef_type PARAMS ((PTR, const char *));
3892 static boolean ieee_tag_type
3893   PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
3894 static boolean ieee_typdef PARAMS ((PTR, const char *));
3895 static boolean ieee_tag PARAMS ((PTR, const char *));
3896 static boolean ieee_int_constant PARAMS ((PTR, const char *, bfd_vma));
3897 static boolean ieee_float_constant PARAMS ((PTR, const char *, double));
3898 static boolean ieee_typed_constant PARAMS ((PTR, const char *, bfd_vma));
3899 static boolean ieee_variable
3900   PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
3901 static boolean ieee_start_function PARAMS ((PTR, const char *, boolean));
3902 static boolean ieee_function_parameter
3903   PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
3904 static boolean ieee_start_block PARAMS ((PTR, bfd_vma));
3905 static boolean ieee_end_block PARAMS ((PTR, bfd_vma));
3906 static boolean ieee_end_function PARAMS ((PTR));
3907 static boolean ieee_lineno
3908   PARAMS ((PTR, const char *, unsigned long, bfd_vma));
3909
3910 static const struct debug_write_fns ieee_fns =
3911 {
3912   ieee_start_compilation_unit,
3913   ieee_start_source,
3914   ieee_empty_type,
3915   ieee_void_type,
3916   ieee_int_type,
3917   ieee_float_type,
3918   ieee_complex_type,
3919   ieee_bool_type,
3920   ieee_enum_type,
3921   ieee_pointer_type,
3922   ieee_function_type,
3923   ieee_reference_type,
3924   ieee_range_type,
3925   ieee_array_type,
3926   ieee_set_type,
3927   ieee_offset_type,
3928   ieee_method_type,
3929   ieee_const_type,
3930   ieee_volatile_type,
3931   ieee_start_struct_type,
3932   ieee_struct_field,
3933   ieee_end_struct_type,
3934   ieee_start_class_type,
3935   ieee_class_static_member,
3936   ieee_class_baseclass,
3937   ieee_class_start_method,
3938   ieee_class_method_variant,
3939   ieee_class_static_method_variant,
3940   ieee_class_end_method,
3941   ieee_end_class_type,
3942   ieee_typedef_type,
3943   ieee_tag_type,
3944   ieee_typdef,
3945   ieee_tag,
3946   ieee_int_constant,
3947   ieee_float_constant,
3948   ieee_typed_constant,
3949   ieee_variable,
3950   ieee_start_function,
3951   ieee_function_parameter,
3952   ieee_start_block,
3953   ieee_end_block,
3954   ieee_end_function,
3955   ieee_lineno
3956 };
3957
3958 /* Initialize a buffer to be empty.  */
3959
3960 /*ARGSUSED*/
3961 static boolean
3962 ieee_init_buffer (info, buflist)
3963      struct ieee_handle *info;
3964      struct ieee_buflist *buflist;
3965 {
3966   buflist->head = NULL;
3967   buflist->tail = NULL;
3968   return true;
3969 }
3970
3971 /* See whether a buffer list has any data.  */
3972
3973 #define ieee_buffer_emptyp(buflist) ((buflist)->head == NULL)
3974
3975 /* Change the current buffer to a specified buffer chain.  */
3976
3977 static boolean
3978 ieee_change_buffer (info, buflist)
3979      struct ieee_handle *info;
3980      struct ieee_buflist *buflist;
3981 {
3982   if (buflist->head == NULL)
3983     {
3984       struct ieee_buf *buf;
3985
3986       buf = (struct ieee_buf *) xmalloc (sizeof *buf);
3987       buf->next = NULL;
3988       buf->c = 0;
3989       buflist->head = buf;
3990       buflist->tail = buf;
3991     }
3992
3993   info->current = buflist;
3994   info->curbuf = buflist->tail;
3995
3996   return true;
3997 }
3998
3999 /* Append a buffer chain.  */
4000
4001 /*ARGSUSED*/
4002 static boolean
4003 ieee_append_buffer (info, mainbuf, newbuf)
4004      struct ieee_handle *info;
4005      struct ieee_buflist *mainbuf;
4006      struct ieee_buflist *newbuf;
4007 {
4008   if (newbuf->head != NULL)
4009     {
4010       if (mainbuf->head == NULL)
4011         mainbuf->head = newbuf->head;
4012       else
4013         mainbuf->tail->next = newbuf->head;
4014       mainbuf->tail = newbuf->tail;
4015     }
4016   return true;
4017 }
4018
4019 /* Write a byte into the buffer.  We use a macro for speed and a
4020    function for the complex cases.  */
4021
4022 #define ieee_write_byte(info, b)                                \
4023   ((info)->curbuf->c < IEEE_BUFSIZE                             \
4024    ? ((info)->curbuf->buf[(info)->curbuf->c++] = (b), true)     \
4025    : ieee_real_write_byte ((info), (b)))
4026
4027 static boolean
4028 ieee_real_write_byte (info, b)
4029      struct ieee_handle *info;
4030      int b;
4031 {
4032   if (info->curbuf->c >= IEEE_BUFSIZE)
4033     {
4034       struct ieee_buf *n;
4035
4036       n = (struct ieee_buf *) xmalloc (sizeof *n);
4037       n->next = NULL;
4038       n->c = 0;
4039       if (info->current->head == NULL)
4040         info->current->head = n;
4041       else
4042         info->current->tail->next = n;
4043       info->current->tail = n;
4044       info->curbuf = n;
4045     }
4046
4047   info->curbuf->buf[info->curbuf->c] = b;
4048   ++info->curbuf->c;
4049
4050   return true;
4051 }
4052
4053 /* Write out two bytes.  */
4054
4055 static boolean
4056 ieee_write_2bytes (info, i)
4057      struct ieee_handle *info;
4058      int i;
4059 {
4060   return (ieee_write_byte (info, i >> 8)
4061           && ieee_write_byte (info, i & 0xff));
4062 }
4063
4064 /* Write out an integer.  */
4065
4066 static boolean
4067 ieee_write_number (info, v)
4068      struct ieee_handle *info;
4069      bfd_vma v;
4070 {
4071   bfd_vma t;
4072   bfd_byte ab[20];
4073   bfd_byte *p;
4074   unsigned int c;
4075
4076   if (v <= (bfd_vma) ieee_number_end_enum)
4077     return ieee_write_byte (info, (int) v);
4078
4079   t = v;
4080   p = ab + sizeof ab;
4081   while (t != 0)
4082     {
4083       *--p = t & 0xff;
4084       t >>= 8;
4085     }
4086   c = (ab + 20) - p;
4087
4088   if (c > (unsigned int) (ieee_number_repeat_end_enum
4089                           - ieee_number_repeat_start_enum))
4090     {
4091       fprintf (stderr, "IEEE numeric overflow: 0x");
4092       fprintf_vma (stderr, v);
4093       fprintf (stderr, "\n");
4094       return false;
4095     }
4096
4097   if (! ieee_write_byte (info, (int) ieee_number_repeat_start_enum + c))
4098     return false;
4099   for (; c > 0; --c, ++p)
4100     {
4101       if (! ieee_write_byte (info, *p))
4102         return false;
4103     }
4104
4105   return true;
4106 }
4107
4108 /* Write out a string.  */
4109
4110 static boolean
4111 ieee_write_id (info, s)
4112      struct ieee_handle *info;
4113      const char *s;
4114 {
4115   unsigned int len;
4116
4117   len = strlen (s);
4118   if (len <= 0x7f)
4119     {
4120       if (! ieee_write_byte (info, len))
4121         return false;
4122     }
4123   else if (len <= 0xff)
4124     {
4125       if (! ieee_write_byte (info, (int) ieee_extension_length_1_enum)
4126           || ! ieee_write_byte (info, len))
4127         return false;
4128     }
4129   else if (len <= 0xffff)
4130     {
4131       if (! ieee_write_byte (info, (int) ieee_extension_length_2_enum)
4132           || ! ieee_write_2bytes (info, len))
4133         return false;
4134     }
4135   else
4136     {
4137       fprintf (stderr, "IEEE string length overflow: %u\n", len);
4138       return false;
4139     }
4140
4141   for (; *s != '\0'; s++)
4142     if (! ieee_write_byte (info, *s))
4143       return false;
4144
4145   return true;
4146 }
4147
4148 /* Write out an ASN record.  */
4149
4150 static boolean
4151 ieee_write_asn (info, indx, val)
4152      struct ieee_handle *info;
4153      unsigned int indx;
4154      bfd_vma val;
4155 {
4156   return (ieee_write_2bytes (info, (int) ieee_asn_record_enum)
4157           && ieee_write_number (info, indx)
4158           && ieee_write_number (info, val));
4159 }
4160
4161 /* Write out an ATN65 record.  */
4162
4163 static boolean
4164 ieee_write_atn65 (info, indx, s)
4165      struct ieee_handle *info;
4166      unsigned int indx;
4167      const char *s;
4168 {
4169   return (ieee_write_2bytes (info, (int) ieee_atn_record_enum)
4170           && ieee_write_number (info, indx)
4171           && ieee_write_number (info, 0)
4172           && ieee_write_number (info, 65)
4173           && ieee_write_id (info, s));
4174 }
4175
4176 /* Push a type index onto the type stack.  */
4177
4178 static boolean
4179 ieee_push_type (info, indx, size, unsignedp, localp)
4180      struct ieee_handle *info;
4181      unsigned int indx;
4182      unsigned int size;
4183      boolean unsignedp;
4184      boolean localp;
4185 {
4186   struct ieee_type_stack *ts;
4187
4188   ts = (struct ieee_type_stack *) xmalloc (sizeof *ts);
4189   memset (ts, 0, sizeof *ts);
4190
4191   ts->type.indx = indx;
4192   ts->type.size = size;
4193   ts->type.unsignedp = unsignedp;
4194   ts->type.localp = localp;
4195
4196   ts->next = info->type_stack;
4197   info->type_stack = ts;
4198
4199   return true;
4200 }
4201
4202 /* Pop a type index off the type stack.  */
4203
4204 static unsigned int
4205 ieee_pop_type (info)
4206      struct ieee_handle *info;
4207 {
4208   return ieee_pop_type_used (info, true);
4209 }
4210
4211 /* Pop an unused type index off the type stack.  */
4212
4213 static void
4214 ieee_pop_unused_type (info)
4215      struct ieee_handle *info;
4216 {
4217   (void) ieee_pop_type_used (info, false);
4218 }
4219
4220 /* Pop a used or unused type index off the type stack.  */
4221
4222 static unsigned int
4223 ieee_pop_type_used (info, used)
4224      struct ieee_handle *info;
4225      boolean used;
4226 {
4227   struct ieee_type_stack *ts;
4228   unsigned int ret;
4229
4230   ts = info->type_stack;
4231   assert (ts != NULL);
4232
4233   /* If this is a function type, and we need it, we need to append the
4234      actual definition to the typedef block now.  */
4235   if (used && ! ieee_buffer_emptyp (&ts->type.fndef))
4236     {
4237       struct ieee_buflist *buflist;
4238
4239       if (ts->type.localp)
4240         {
4241           /* Make sure we have started the types block.  */
4242           if (ieee_buffer_emptyp (&info->types))
4243             {
4244               if (! ieee_change_buffer (info, &info->types)
4245                   || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4246                   || ! ieee_write_byte (info, 1)
4247                   || ! ieee_write_number (info, 0)
4248                   || ! ieee_write_id (info, info->modname))
4249                 return false;
4250             }
4251           buflist = &info->types;
4252         }
4253       else
4254         {
4255           /* Make sure we started the global type block.  */
4256           if (ieee_buffer_emptyp (&info->global_types))
4257             {
4258               if (! ieee_change_buffer (info, &info->global_types)
4259                   || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4260                   || ! ieee_write_byte (info, 2)
4261                   || ! ieee_write_number (info, 0)
4262                   || ! ieee_write_id (info, ""))
4263                 return false;
4264             }
4265           buflist = &info->global_types;
4266         }
4267
4268       if (! ieee_append_buffer (info, buflist, &ts->type.fndef))
4269         return false;
4270     }
4271
4272   ret = ts->type.indx;
4273   info->type_stack = ts->next;
4274   free (ts);
4275   return ret;
4276 }
4277
4278 /* Add a range of bytes included in the current compilation unit.  */
4279
4280 static boolean
4281 ieee_add_range (info, global, low, high)
4282      struct ieee_handle *info;
4283      boolean global;
4284      bfd_vma low;
4285      bfd_vma high;
4286 {
4287   struct ieee_range **plist, *r, **pr;
4288
4289   if (low == (bfd_vma) -1 || high == (bfd_vma) -1 || low == high)
4290     return true;
4291
4292   if (global)
4293     plist = &info->global_ranges;
4294   else
4295     plist = &info->ranges;
4296
4297   for (r = *plist; r != NULL; r = r->next)
4298     {
4299       if (high >= r->low && low <= r->high)
4300         {
4301           /* The new range overlaps r.  */
4302           if (low < r->low)
4303             r->low = low;
4304           if (high > r->high)
4305             r->high = high;
4306           pr = &r->next;
4307           while (*pr != NULL && (*pr)->low <= r->high)
4308             {
4309               struct ieee_range *n;
4310
4311               if ((*pr)->high > r->high)
4312                 r->high = (*pr)->high;
4313               n = (*pr)->next;
4314               free (*pr);
4315               *pr = n;
4316             }
4317           return true;
4318         }
4319     }
4320
4321   r = (struct ieee_range *) xmalloc (sizeof *r);
4322   memset (r, 0, sizeof *r);
4323
4324   r->low = low;
4325   r->high = high;
4326
4327   /* Store the ranges sorted by address.  */
4328   for (pr = plist; *pr != NULL; pr = &(*pr)->next)
4329     if ((*pr)->low > high)
4330       break;
4331   r->next = *pr;
4332   *pr = r;
4333
4334   return true;
4335 }
4336
4337 /* Start a new range for which we only have the low address.  */
4338
4339 static boolean
4340 ieee_start_range (info, low)
4341      struct ieee_handle *info;
4342      bfd_vma low;
4343 {
4344   struct ieee_range *r;
4345
4346   r = (struct ieee_range *) xmalloc (sizeof *r);
4347   memset (r, 0, sizeof *r);
4348   r->low = low;
4349   r->next = info->pending_ranges;
4350   info->pending_ranges = r;
4351   return true;
4352 }  
4353
4354 /* Finish a range started by ieee_start_range.  */
4355
4356 static boolean
4357 ieee_end_range (info, high)
4358      struct ieee_handle *info;
4359      bfd_vma high;
4360 {
4361   struct ieee_range *r;
4362   bfd_vma low;
4363
4364   assert (info->pending_ranges != NULL);
4365   r = info->pending_ranges;
4366   low = r->low;
4367   info->pending_ranges = r->next;
4368   free (r);
4369   return ieee_add_range (info, false, low, high);
4370 }
4371
4372 /* Start defining a type.  */
4373
4374 static boolean
4375 ieee_define_type (info, size, unsignedp, localp)
4376      struct ieee_handle *info;
4377      unsigned int size;
4378      boolean unsignedp;
4379      boolean localp;
4380 {
4381   return ieee_define_named_type (info, (const char *) NULL,
4382                                  (unsigned int) -1, size, unsignedp,
4383                                  localp, (struct ieee_buflist *) NULL);
4384 }
4385
4386 /* Start defining a named type.  */
4387
4388 static boolean
4389 ieee_define_named_type (info, name, indx, size, unsignedp, localp, buflist)
4390      struct ieee_handle *info;
4391      const char *name;
4392      unsigned int indx;
4393      unsigned int size;
4394      boolean unsignedp;
4395      boolean localp;
4396      struct ieee_buflist *buflist;
4397 {
4398   unsigned int type_indx;
4399   unsigned int name_indx;
4400
4401   if (indx != (unsigned int) -1)
4402     type_indx = indx;
4403   else
4404     {
4405       type_indx = info->type_indx;
4406       ++info->type_indx;
4407     }
4408
4409   name_indx = info->name_indx;
4410   ++info->name_indx;
4411
4412   if (name == NULL)
4413     name = "";
4414
4415   /* If we were given a buffer, use it; otherwise, use either the
4416      local or the global type information, and make sure that the type
4417      block is started.  */
4418   if (buflist != NULL)
4419     {
4420       if (! ieee_change_buffer (info, buflist))
4421         return false;
4422     }
4423   else if (localp)
4424     {
4425       if (! ieee_buffer_emptyp (&info->types))
4426         {
4427           if (! ieee_change_buffer (info, &info->types))
4428             return false;
4429         }
4430       else
4431         {
4432           if (! ieee_change_buffer (info, &info->types)
4433               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4434               || ! ieee_write_byte (info, 1)
4435               || ! ieee_write_number (info, 0)
4436               || ! ieee_write_id (info, info->modname))
4437             return false;
4438         }
4439     }
4440   else
4441     {
4442       if (! ieee_buffer_emptyp (&info->global_types))
4443         {
4444           if (! ieee_change_buffer (info, &info->global_types))
4445             return false;
4446         }
4447       else
4448         {
4449           if (! ieee_change_buffer (info, &info->global_types)
4450               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4451               || ! ieee_write_byte (info, 2)
4452               || ! ieee_write_number (info, 0)
4453               || ! ieee_write_id (info, ""))
4454             return false;
4455         }
4456     }
4457
4458   /* Push the new type on the type stack, write out an NN record, and
4459      write out the start of a TY record.  The caller will then finish
4460      the TY record.  */
4461   if (! ieee_push_type (info, type_indx, size, unsignedp, localp))
4462     return false;
4463
4464   return (ieee_write_byte (info, (int) ieee_nn_record)
4465           && ieee_write_number (info, name_indx)
4466           && ieee_write_id (info, name)
4467           && ieee_write_byte (info, (int) ieee_ty_record_enum)
4468           && ieee_write_number (info, type_indx)
4469           && ieee_write_byte (info, 0xce)
4470           && ieee_write_number (info, name_indx));
4471 }
4472
4473 /* Get an entry to the list of modified versions of a type.  */
4474
4475 static struct ieee_modified_type *
4476 ieee_get_modified_info (info, indx)
4477      struct ieee_handle *info;
4478      unsigned int indx;
4479 {
4480   if (indx >= info->modified_alloc)
4481     {
4482       unsigned int nalloc;
4483
4484       nalloc = info->modified_alloc;
4485       if (nalloc == 0)
4486         nalloc = 16;
4487       while (indx >= nalloc)
4488         nalloc *= 2;
4489       info->modified = ((struct ieee_modified_type *)
4490                         xrealloc (info->modified,
4491                                   nalloc * sizeof *info->modified));
4492       memset (info->modified + info->modified_alloc, 0,
4493               (nalloc - info->modified_alloc) * sizeof *info->modified);
4494       info->modified_alloc = nalloc;
4495     }
4496
4497   return info->modified + indx;
4498 }
4499 \f
4500 /* Routines for the hash table mapping names to types.  */
4501
4502 /* Initialize an entry in the hash table.  */
4503
4504 static struct bfd_hash_entry *
4505 ieee_name_type_newfunc (entry, table, string)
4506      struct bfd_hash_entry *entry;
4507      struct bfd_hash_table *table;
4508      const char *string;
4509 {
4510   struct ieee_name_type_hash_entry *ret =
4511     (struct ieee_name_type_hash_entry *) entry;
4512
4513   /* Allocate the structure if it has not already been allocated by a
4514      subclass.  */
4515   if (ret == NULL)
4516     ret = ((struct ieee_name_type_hash_entry *)
4517            bfd_hash_allocate (table, sizeof *ret));
4518   if (ret == NULL)
4519     return NULL;
4520
4521   /* Call the allocation method of the superclass.  */
4522   ret = ((struct ieee_name_type_hash_entry *)
4523          bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
4524   if (ret)
4525     {
4526       /* Set local fields.  */
4527       ret->types = NULL;
4528     }
4529
4530   return (struct bfd_hash_entry *) ret;
4531 }
4532
4533 /* Look up an entry in the hash table.  */
4534
4535 #define ieee_name_type_hash_lookup(table, string, create, copy) \
4536   ((struct ieee_name_type_hash_entry *) \
4537    bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
4538
4539 /* Traverse the hash table.  */
4540
4541 #define ieee_name_type_hash_traverse(table, func, info)                 \
4542   (bfd_hash_traverse                                                    \
4543    (&(table)->root,                                                     \
4544     (boolean (*) PARAMS ((struct bfd_hash_entry *, PTR))) (func),       \
4545     (info)))
4546 \f
4547 /* The general routine to write out IEEE debugging information.  */
4548
4549 boolean
4550 write_ieee_debugging_info (abfd, dhandle)
4551      bfd *abfd;
4552      PTR dhandle;
4553 {
4554   struct ieee_handle info;
4555   asection *s;
4556   const char *err;
4557   struct ieee_buf *b;
4558
4559   memset (&info, 0, sizeof info);
4560   info.abfd = abfd;
4561   info.type_indx = 256;
4562   info.name_indx = 32;
4563
4564   if (! bfd_hash_table_init (&info.typedefs.root, ieee_name_type_newfunc)
4565       || ! bfd_hash_table_init (&info.tags.root, ieee_name_type_newfunc))
4566     return false;
4567
4568   if (! ieee_init_buffer (&info, &info.global_types)
4569       || ! ieee_init_buffer (&info, &info.data)
4570       || ! ieee_init_buffer (&info, &info.types)
4571       || ! ieee_init_buffer (&info, &info.vars)
4572       || ! ieee_init_buffer (&info, &info.cxx)
4573       || ! ieee_init_buffer (&info, &info.linenos)
4574       || ! ieee_init_buffer (&info, &info.fntype)
4575       || ! ieee_init_buffer (&info, &info.fnargs))
4576     return false;
4577
4578   if (! debug_write (dhandle, &ieee_fns, (PTR) &info))
4579     return false;
4580
4581   if (info.filename != NULL)
4582     {
4583       if (! ieee_finish_compilation_unit (&info))
4584         return false;
4585     }
4586
4587   /* Put any undefined tags in the global typedef information.  */
4588   info.error = false;
4589   ieee_name_type_hash_traverse (&info.tags,
4590                                 ieee_write_undefined_tag,
4591                                 (PTR) &info);
4592   if (info.error)
4593     return false;
4594
4595   /* Prepend the global typedef information to the other data.  */
4596   if (! ieee_buffer_emptyp (&info.global_types))
4597     {
4598       if (! ieee_change_buffer (&info, &info.global_types)
4599           || ! ieee_write_byte (&info, (int) ieee_be_record_enum))
4600         return false;
4601
4602       if (! ieee_append_buffer (&info, &info.global_types, &info.data))
4603         return false;
4604       info.data = info.global_types;
4605     }
4606
4607   /* Make sure that we have declare BB11 blocks for each range in the
4608      file.  They are added to info->vars.  */
4609   info.error = false;
4610   if (! ieee_init_buffer (&info, &info.vars))
4611     return false;
4612   bfd_map_over_sections (abfd, ieee_add_bb11_blocks, (PTR) &info);
4613   if (info.error)
4614     return false;
4615   if (! ieee_buffer_emptyp (&info.vars))
4616     {
4617       if (! ieee_change_buffer (&info, &info.vars)
4618           || ! ieee_write_byte (&info, (int) ieee_be_record_enum))
4619         return false;
4620
4621       if (! ieee_append_buffer (&info, &info.data, &info.vars))
4622         return false;
4623     }
4624
4625   /* Now all the data is in info.data.  Write it out to the BFD.  We
4626      normally would need to worry about whether all the other sections
4627      are set up yet, but the IEEE backend will handle this particular
4628      case correctly regardless.  */
4629   if (ieee_buffer_emptyp (&info.data))
4630     {
4631       /* There is no debugging information.  */
4632       return true;
4633     }
4634   err = NULL;
4635   s = bfd_make_section (abfd, ".debug");
4636   if (s == NULL)
4637     err = "bfd_make_section";
4638   if (err == NULL)
4639     {
4640       if (! bfd_set_section_flags (abfd, s, SEC_DEBUGGING | SEC_HAS_CONTENTS))
4641         err = "bfd_set_section_flags";
4642     }
4643   if (err == NULL)
4644     {
4645       bfd_size_type size;
4646
4647       size = 0;
4648       for (b = info.data.head; b != NULL; b = b->next)
4649         size += b->c;
4650       if (! bfd_set_section_size (abfd, s, size))
4651         err = "bfd_set_section_size";
4652     }
4653   if (err == NULL)
4654     {
4655       file_ptr offset;
4656
4657       offset = 0;
4658       for (b = info.data.head; b != NULL; b = b->next)
4659         {
4660           if (! bfd_set_section_contents (abfd, s, b->buf, offset, b->c))
4661             {
4662               err = "bfd_set_section_contents";
4663               break;
4664             }
4665           offset += b->c;
4666         }
4667     }
4668
4669   if (err != NULL)
4670     {
4671       fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), err,
4672                bfd_errmsg (bfd_get_error ()));
4673       return false;
4674     }
4675
4676   bfd_hash_table_free (&info.typedefs.root);
4677   bfd_hash_table_free (&info.tags.root);
4678
4679   return true;
4680 }
4681
4682 /* Write out information for an undefined tag.  This is called via
4683    ieee_name_type_hash_traverse.  */
4684
4685 static boolean
4686 ieee_write_undefined_tag (h, p)
4687      struct ieee_name_type_hash_entry *h;
4688      PTR p;
4689 {
4690   struct ieee_handle *info = (struct ieee_handle *) p;
4691   struct ieee_name_type *nt;
4692
4693   for (nt = h->types; nt != NULL; nt = nt->next)
4694     {
4695       unsigned int name_indx;
4696       char code;
4697
4698       if (nt->kind == DEBUG_KIND_ILLEGAL)
4699         continue;
4700
4701       if (ieee_buffer_emptyp (&info->global_types))
4702         {
4703           if (! ieee_change_buffer (info, &info->global_types)
4704               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4705               || ! ieee_write_byte (info, 2)
4706               || ! ieee_write_number (info, 0)
4707               || ! ieee_write_id (info, ""))
4708             {
4709               info->error = true;
4710               return false;
4711             }
4712         }
4713       else
4714         {
4715           if (! ieee_change_buffer (info, &info->global_types))
4716             {
4717               info->error = true;
4718               return false;
4719             }
4720         }
4721
4722       name_indx = info->name_indx;
4723       ++info->name_indx;
4724       if (! ieee_write_byte (info, (int) ieee_nn_record)
4725           || ! ieee_write_number (info, name_indx)
4726           || ! ieee_write_id (info, nt->type.name)
4727           || ! ieee_write_byte (info, (int) ieee_ty_record_enum)
4728           || ! ieee_write_number (info, nt->type.indx)
4729           || ! ieee_write_byte (info, 0xce)
4730           || ! ieee_write_number (info, name_indx))
4731         {
4732           info->error = true;
4733           return false;
4734         }
4735
4736       switch (nt->kind)
4737         {
4738         default:
4739           abort ();
4740           info->error = true;
4741           return false;
4742         case DEBUG_KIND_STRUCT:
4743         case DEBUG_KIND_CLASS:
4744           code = 'S';
4745           break;
4746         case DEBUG_KIND_UNION:
4747         case DEBUG_KIND_UNION_CLASS:
4748           code = 'U';
4749           break;
4750         case DEBUG_KIND_ENUM:
4751           code = 'E';
4752           break;
4753         }
4754       if (! ieee_write_number (info, code)
4755           || ! ieee_write_number (info, 0))
4756         {
4757           info->error = true;
4758           return false;
4759         }
4760     }
4761
4762   return true;
4763 }
4764
4765 /* Start writing out information for a compilation unit.  */
4766
4767 static boolean
4768 ieee_start_compilation_unit (p, filename)
4769      PTR p;
4770      const char *filename;
4771 {
4772   struct ieee_handle *info = (struct ieee_handle *) p;
4773   const char *modname;
4774   char *c, *s;
4775   unsigned int nindx;
4776
4777   if (info->filename != NULL)
4778     {
4779       if (! ieee_finish_compilation_unit (info))
4780         return false;
4781     }
4782
4783   info->filename = filename;
4784   modname = strrchr (filename, '/');
4785   if (modname != NULL)
4786     ++modname;
4787   else
4788     {
4789       modname = strrchr (filename, '\\');
4790       if (modname != NULL)
4791         ++modname;
4792       else
4793         modname = filename;
4794     }
4795   c = xstrdup (modname);
4796   s = strrchr (c, '.');
4797   if (s != NULL)
4798     *s = '\0';
4799   info->modname = c;
4800
4801   if (! ieee_init_buffer (info, &info->types)
4802       || ! ieee_init_buffer (info, &info->vars)
4803       || ! ieee_init_buffer (info, &info->cxx)
4804       || ! ieee_init_buffer (info, &info->linenos))
4805     return false;
4806   info->ranges = NULL;
4807
4808   /* Always include a BB1 and a BB3 block.  That is what the output of
4809      the MRI linker seems to look like.  */
4810   if (! ieee_change_buffer (info, &info->types)
4811       || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4812       || ! ieee_write_byte (info, 1)
4813       || ! ieee_write_number (info, 0)
4814       || ! ieee_write_id (info, info->modname))
4815     return false;
4816
4817   nindx = info->name_indx;
4818   ++info->name_indx;
4819   if (! ieee_change_buffer (info, &info->vars)
4820       || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4821       || ! ieee_write_byte (info, 3)
4822       || ! ieee_write_number (info, 0)
4823       || ! ieee_write_id (info, info->modname))
4824     return false;
4825
4826   return true;
4827 }
4828
4829 /* Finish up a compilation unit.  */
4830
4831 static boolean
4832 ieee_finish_compilation_unit (info)
4833      struct ieee_handle *info;
4834 {
4835   struct ieee_range *r;
4836
4837   if (! ieee_buffer_emptyp (&info->types))
4838     {
4839       if (! ieee_change_buffer (info, &info->types)
4840           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
4841         return false;
4842     }
4843
4844   if (! ieee_buffer_emptyp (&info->cxx))
4845     {
4846       /* Append any C++ information to the global function and
4847          variable information.  */
4848       assert (! ieee_buffer_emptyp (&info->vars));
4849       if (! ieee_change_buffer (info, &info->vars))
4850         return false;
4851
4852       /* We put the pmisc records in a dummy procedure, just as the
4853          MRI compiler does.  */
4854       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
4855           || ! ieee_write_byte (info, 6)
4856           || ! ieee_write_number (info, 0)
4857           || ! ieee_write_id (info, "__XRYCPP")
4858           || ! ieee_write_number (info, 0)
4859           || ! ieee_write_number (info, 0)
4860           || ! ieee_write_number (info, info->highaddr - 1)
4861           || ! ieee_append_buffer (info, &info->vars, &info->cxx)
4862           || ! ieee_change_buffer (info, &info->vars)
4863           || ! ieee_write_byte (info, (int) ieee_be_record_enum)
4864           || ! ieee_write_number (info, info->highaddr - 1))
4865         return false;
4866     }
4867
4868   if (! ieee_buffer_emptyp (&info->vars))
4869     {
4870       if (! ieee_change_buffer (info, &info->vars)
4871           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
4872         return false;
4873     }
4874
4875   if (info->pending_lineno_filename != NULL)
4876     {
4877       /* Force out the pending line number.  */
4878       if (! ieee_lineno ((PTR) info, (const char *) NULL, 0, (bfd_vma) -1))
4879         return false;
4880     }
4881   if (! ieee_buffer_emptyp (&info->linenos))
4882     {
4883       if (! ieee_change_buffer (info, &info->linenos)
4884           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
4885         return false;
4886       if (strcmp (info->filename, info->lineno_filename) != 0)
4887         {
4888           /* We were not in the main file.  We just closed the
4889              included line number block, and now we must close the
4890              main line number block.  */
4891           if (! ieee_write_byte (info, (int) ieee_be_record_enum))
4892             return false;
4893         }
4894     }
4895
4896   if (! ieee_append_buffer (info, &info->data, &info->types)
4897       || ! ieee_append_buffer (info, &info->data, &info->vars)
4898       || ! ieee_append_buffer (info, &info->data, &info->linenos))
4899     return false;
4900
4901   /* Build BB10/BB11 blocks based on the ranges we recorded.  */
4902   if (! ieee_change_buffer (info, &info->data))
4903     return false;
4904
4905   if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
4906       || ! ieee_write_byte (info, 10)
4907       || ! ieee_write_number (info, 0)
4908       || ! ieee_write_id (info, info->modname)
4909       || ! ieee_write_id (info, "")
4910       || ! ieee_write_number (info, 0)
4911       || ! ieee_write_id (info, "GNU objcopy"))
4912     return false;
4913
4914   for (r = info->ranges; r != NULL; r = r->next)
4915     {
4916       bfd_vma low, high;
4917       asection *s;
4918       int kind;
4919
4920       low = r->low;
4921       high = r->high;
4922
4923       /* Find the section corresponding to this range.  */
4924       for (s = info->abfd->sections; s != NULL; s = s->next)
4925         {
4926           if (bfd_get_section_vma (info->abfd, s) <= low
4927               && high <= (bfd_get_section_vma (info->abfd, s)
4928                           + bfd_section_size (info->abfd, s)))
4929             break;
4930         }
4931
4932       if (s == NULL)
4933         {
4934           /* Just ignore this range.  */
4935           continue;
4936         }
4937
4938       /* Coalesce ranges if it seems reasonable.  */
4939       while (r->next != NULL
4940              && high + 64 >= r->next->low
4941              && (r->next->high
4942                  <= (bfd_get_section_vma (info->abfd, s)
4943                      + bfd_section_size (info->abfd, s))))
4944         {
4945           r = r->next;
4946           high = r->high;
4947         }
4948
4949       if ((s->flags & SEC_CODE) != 0)
4950         kind = 1;
4951       else if ((s->flags & SEC_READONLY) != 0)
4952         kind = 3;
4953       else
4954         kind = 2;
4955
4956       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
4957           || ! ieee_write_byte (info, 11)
4958           || ! ieee_write_number (info, 0)
4959           || ! ieee_write_id (info, "")
4960           || ! ieee_write_number (info, kind)
4961           || ! ieee_write_number (info, s->index + IEEE_SECTION_NUMBER_BASE)
4962           || ! ieee_write_number (info, low)
4963           || ! ieee_write_byte (info, (int) ieee_be_record_enum)
4964           || ! ieee_write_number (info, high - low))
4965         return false;
4966
4967       /* Add this range to the list of global ranges.  */
4968       if (! ieee_add_range (info, true, low, high))
4969         return false;
4970     }
4971
4972   if (! ieee_write_byte (info, (int) ieee_be_record_enum))
4973     return false;
4974
4975   return true;
4976 }
4977
4978 /* Add BB11 blocks describing each range that we have not already
4979    described.  */
4980
4981 static void
4982 ieee_add_bb11_blocks (abfd, sec, data)
4983      bfd *abfd;
4984      asection *sec;
4985      PTR data;
4986 {
4987   struct ieee_handle *info = (struct ieee_handle *) data;
4988   bfd_vma low, high;
4989   struct ieee_range *r;
4990
4991   low = bfd_get_section_vma (abfd, sec);
4992   high = low + bfd_section_size (abfd, sec);
4993
4994   /* Find the first range at or after this section.  The ranges are
4995      sorted by address.  */
4996   for (r = info->global_ranges; r != NULL; r = r->next)
4997     if (r->high > low)
4998       break;
4999
5000   while (low < high)
5001     {
5002       if (r == NULL || r->low >= high)
5003         {
5004           if (! ieee_add_bb11 (info, sec, low, high))
5005             info->error = true;
5006           return;
5007         }
5008
5009       if (low < r->low)
5010         {
5011           if (! ieee_add_bb11 (info, sec, low, r->low))
5012             {
5013               info->error = true;
5014               return;
5015             }
5016         }
5017       low = r->high;
5018
5019       r = r->next;
5020     }
5021 }
5022
5023 /* Add a single BB11 block for a range.  We add it to info->vars.  */
5024
5025 static boolean
5026 ieee_add_bb11 (info, sec, low, high)
5027      struct ieee_handle *info;
5028      asection *sec;
5029      bfd_vma low;
5030      bfd_vma high;
5031 {
5032   int kind;
5033
5034   if (! ieee_buffer_emptyp (&info->vars))
5035     {
5036       if (! ieee_change_buffer (info, &info->vars))
5037         return false;
5038     }
5039   else
5040     {
5041       const char *filename, *modname;
5042       char *c, *s;
5043
5044       /* Start the enclosing BB10 block.  */
5045       filename = bfd_get_filename (info->abfd);
5046       modname = strrchr (filename, '/');
5047       if (modname != NULL)
5048         ++modname;
5049       else
5050         {
5051           modname = strrchr (filename, '\\');
5052           if (modname != NULL)
5053             ++modname;
5054           else
5055             modname = filename;
5056         }
5057       c = xstrdup (modname);
5058       s = strrchr (c, '.');
5059       if (s != NULL)
5060         *s = '\0';
5061
5062       if (! ieee_change_buffer (info, &info->vars)
5063           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
5064           || ! ieee_write_byte (info, 10)
5065           || ! ieee_write_number (info, 0)
5066           || ! ieee_write_id (info, c)
5067           || ! ieee_write_id (info, "")
5068           || ! ieee_write_number (info, 0)
5069           || ! ieee_write_id (info, "GNU objcopy"))
5070         return false;
5071
5072       free (c);
5073     }
5074
5075   if ((sec->flags & SEC_CODE) != 0)
5076     kind = 1;
5077   else if ((sec->flags & SEC_READONLY) != 0)
5078     kind = 3;
5079   else
5080     kind = 2;
5081
5082   if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
5083       || ! ieee_write_byte (info, 11)
5084       || ! ieee_write_number (info, 0)
5085       || ! ieee_write_id (info, "")
5086       || ! ieee_write_number (info, kind)
5087       || ! ieee_write_number (info, sec->index + IEEE_SECTION_NUMBER_BASE)
5088       || ! ieee_write_number (info, low)
5089       || ! ieee_write_byte (info, (int) ieee_be_record_enum)
5090       || ! ieee_write_number (info, high - low))
5091     return false;
5092
5093   return true;
5094 }
5095
5096 /* Start recording information from a particular source file.  This is
5097    used to record which file defined which types, variables, etc.  It
5098    is not used for line numbers, since the lineno entry point passes
5099    down the file name anyhow.  IEEE debugging information doesn't seem
5100    to store this information anywhere.  */
5101
5102 /*ARGSUSED*/
5103 static boolean
5104 ieee_start_source (p, filename)
5105      PTR p;
5106      const char *filename;
5107 {
5108   return true;
5109 }
5110
5111 /* Make an empty type.  */
5112
5113 static boolean
5114 ieee_empty_type (p)
5115      PTR p;
5116 {
5117   struct ieee_handle *info = (struct ieee_handle *) p;
5118
5119   return ieee_push_type (info, (int) builtin_unknown, 0, false, false);
5120 }
5121
5122 /* Make a void type.  */
5123
5124 static boolean
5125 ieee_void_type (p)
5126      PTR p;
5127 {
5128   struct ieee_handle *info = (struct ieee_handle *) p;
5129
5130   return ieee_push_type (info, (int) builtin_void, 0, false, false);
5131 }
5132
5133 /* Make an integer type.  */
5134
5135 static boolean
5136 ieee_int_type (p, size, unsignedp)
5137      PTR p;
5138      unsigned int size;
5139      boolean unsignedp;
5140 {
5141   struct ieee_handle *info = (struct ieee_handle *) p;
5142   unsigned int indx;
5143
5144   switch (size)
5145     {
5146     case 1:
5147       indx = (int) builtin_signed_char;
5148       break;
5149     case 2:
5150       indx = (int) builtin_signed_short_int;
5151       break;
5152     case 4:
5153       indx = (int) builtin_signed_long;
5154       break;
5155     case 8:
5156       indx = (int) builtin_signed_long_long;
5157       break;
5158     default:
5159       fprintf (stderr, "IEEE unsupported integer type size %u\n", size);
5160       return false;
5161     }
5162
5163   if (unsignedp)
5164     ++indx;
5165
5166   return ieee_push_type (info, indx, size, unsignedp, false);
5167 }
5168
5169 /* Make a floating point type.  */
5170
5171 static boolean
5172 ieee_float_type (p, size)
5173      PTR p;
5174      unsigned int size;
5175 {
5176   struct ieee_handle *info = (struct ieee_handle *) p;
5177   unsigned int indx;
5178
5179   switch (size)
5180     {
5181     case 4:
5182       indx = (int) builtin_float;
5183       break;
5184     case 8:
5185       indx = (int) builtin_double;
5186       break;
5187     case 12:
5188       /* FIXME: This size really depends upon the processor.  */
5189       indx = (int) builtin_long_double;
5190       break;
5191     case 16:
5192       indx = (int) builtin_long_long_double;
5193       break;
5194     default:
5195       fprintf (stderr, "IEEE unsupported float type size %u\n", size);
5196       return false;
5197     }
5198
5199   return ieee_push_type (info, indx, size, false, false);
5200 }
5201
5202 /* Make a complex type.  */
5203
5204 static boolean
5205 ieee_complex_type (p, size)
5206      PTR p;
5207      unsigned int size;
5208 {
5209   struct ieee_handle *info = (struct ieee_handle *) p;
5210   char code;
5211
5212   switch (size)
5213     {
5214     case 4:
5215       if (info->complex_float_index != 0)
5216         return ieee_push_type (info, info->complex_float_index, size * 2,
5217                                false, false);
5218       code = 'c';
5219       break;
5220     case 12:
5221     case 16:
5222       /* These cases can be output by gcc -gstabs.  Outputting the
5223          wrong type is better than crashing.  */
5224     case 8:
5225       if (info->complex_double_index != 0)
5226         return ieee_push_type (info, info->complex_double_index, size * 2,
5227                                false, false);
5228       code = 'd';
5229       break;
5230     default:
5231       fprintf (stderr, "IEEE unsupported complex type size %u\n", size);
5232       return false;
5233     }
5234
5235   /* FIXME: I don't know what the string is for.  */
5236   if (! ieee_define_type (info, size * 2, false, false)
5237       || ! ieee_write_number (info, code)
5238       || ! ieee_write_id (info, ""))
5239     return false;
5240
5241   if (size == 4)
5242     info->complex_float_index = info->type_stack->type.indx;
5243   else
5244     info->complex_double_index = info->type_stack->type.indx;
5245
5246   return true;
5247 }
5248
5249 /* Make a boolean type.  IEEE doesn't support these, so we just make
5250    an integer type instead.  */
5251
5252 static boolean
5253 ieee_bool_type (p, size)
5254      PTR p;
5255      unsigned int size;
5256 {
5257   return ieee_int_type (p, size, true);
5258 }
5259
5260 /* Make an enumeration.  */
5261
5262 static boolean
5263 ieee_enum_type (p, tag, names, vals)
5264      PTR p;
5265      const char *tag;
5266      const char **names;
5267      bfd_signed_vma *vals;
5268 {
5269   struct ieee_handle *info = (struct ieee_handle *) p;
5270   struct ieee_defined_enum *e;
5271   boolean localp, simple;
5272   int i;
5273
5274   localp = false;
5275   for (e = info->enums; e != NULL; e = e->next)
5276     {
5277       if (tag == NULL)
5278         {
5279           if (e->tag != NULL)
5280             continue;
5281         }
5282       else
5283         {
5284           if (e->tag == NULL
5285               || tag[0] != e->tag[0]
5286               || strcmp (tag, e->tag) != 0)
5287             continue;
5288         }
5289
5290       if (names != NULL && e->names != NULL)
5291         {
5292           for (i = 0; names[i] != NULL && e->names[i] != NULL; i++)
5293             {
5294               if (names[i][0] != e->names[i][0]
5295                   || vals[i] != e->vals[i]
5296                   || strcmp (names[i], e->names[i]) != 0)
5297                 break;
5298             }
5299         }
5300
5301       if ((names == NULL && e->names == NULL)
5302           || (names[i] == NULL && e->names[i] == NULL))
5303         {
5304           /* We've seen this enum before.  */
5305           return ieee_push_type (info, e->indx, 0, true, false);
5306         }
5307
5308       if (tag != NULL)
5309         {
5310           /* We've already seen an enum of the same name, so we must make
5311              sure to output this one locally.  */
5312           localp = true;
5313           break;
5314         }
5315     }
5316
5317   /* If this is a simple enumeration, in which the values start at 0
5318      and always increment by 1, we can use type E.  Otherwise we must
5319      use type N.  */
5320
5321   simple = true;
5322   if (names != NULL)
5323     {
5324       for (i = 0; names[i] != NULL; i++)
5325         {
5326           if (vals[i] != i)
5327             {
5328               simple = false;
5329               break;
5330             }
5331         }
5332     }
5333
5334   if (! ieee_define_named_type (info, tag, (unsigned int) -1, 0,
5335                                 true, localp, (struct ieee_buflist *) NULL)
5336       || ! ieee_write_number (info, simple ? 'E' : 'N'))
5337     return false;
5338   if (simple)
5339     {
5340       /* FIXME: This is supposed to be the enumeration size, but we
5341          don't store that.  */
5342       if (! ieee_write_number (info, 4))
5343         return false;
5344     }
5345   if (names != NULL)
5346     {
5347       for (i = 0; names[i] != NULL; i++)
5348         {
5349           if (! ieee_write_id (info, names[i]))
5350             return false;
5351           if (! simple)
5352             {
5353               if (! ieee_write_number (info, vals[i]))
5354                 return false;
5355             }
5356         }
5357     }
5358
5359   if (! localp)
5360     {
5361       e = (struct ieee_defined_enum *) xmalloc (sizeof *e);
5362       memset (e, 0, sizeof *e);
5363
5364       e->indx = info->type_stack->type.indx;
5365       e->tag = tag;
5366       e->names = names;
5367       e->vals = vals;
5368
5369       e->next = info->enums;
5370       info->enums = e;
5371     }
5372
5373   return true;
5374 }
5375
5376 /* Make a pointer type.  */
5377
5378 static boolean
5379 ieee_pointer_type (p)
5380      PTR p;
5381 {
5382   struct ieee_handle *info = (struct ieee_handle *) p;
5383   boolean localp;
5384   unsigned int indx;
5385   struct ieee_modified_type *m = NULL;
5386
5387   localp = info->type_stack->type.localp;
5388   indx = ieee_pop_type (info);
5389
5390   /* A pointer to a simple builtin type can be obtained by adding 32.
5391      FIXME: Will this be a short pointer, and will that matter?  */
5392   if (indx < 32)
5393     return ieee_push_type (info, indx + 32, 0, true, false);
5394
5395   if (! localp)
5396     {
5397       m = ieee_get_modified_info (p, indx);
5398       if (m == NULL)
5399         return false;
5400
5401       /* FIXME: The size should depend upon the architecture.  */
5402       if (m->pointer > 0)
5403         return ieee_push_type (info, m->pointer, 4, true, false);
5404     }
5405
5406   if (! ieee_define_type (info, 4, true, localp)
5407       || ! ieee_write_number (info, 'P')
5408       || ! ieee_write_number (info, indx))
5409     return false;
5410
5411   if (! localp)
5412     m->pointer = info->type_stack->type.indx;
5413
5414   return true;
5415 }
5416
5417 /* Make a function type.  This will be called for a method, but we
5418    don't want to actually add it to the type table in that case.  We
5419    handle this by defining the type in a private buffer, and only
5420    adding that buffer to the typedef block if we are going to use it.  */
5421
5422 static boolean
5423 ieee_function_type (p, argcount, varargs)
5424      PTR p;
5425      int argcount;
5426      boolean varargs;
5427 {
5428   struct ieee_handle *info = (struct ieee_handle *) p;
5429   boolean localp;
5430   unsigned int *args = NULL;
5431   int i;
5432   unsigned int retindx;
5433   struct ieee_buflist fndef;
5434   struct ieee_modified_type *m;
5435
5436   localp = false;
5437
5438   if (argcount > 0)
5439     {
5440       args = (unsigned int *) xmalloc (argcount * sizeof *args);
5441       for (i = argcount - 1; i >= 0; i--)
5442         {
5443           if (info->type_stack->type.localp)
5444             localp = true;
5445           args[i] = ieee_pop_type (info);
5446         }
5447     }
5448   else if (argcount < 0)
5449     varargs = false;
5450
5451   if (info->type_stack->type.localp)
5452     localp = true;
5453   retindx = ieee_pop_type (info);
5454
5455   m = NULL;
5456   if (argcount < 0 && ! localp)
5457     {
5458       m = ieee_get_modified_info (p, retindx);
5459       if (m == NULL)
5460         return false;
5461
5462       if (m->function > 0)
5463         return ieee_push_type (info, m->function, 0, true, false);
5464     }
5465
5466   /* An attribute of 0x41 means that the frame and push mask are
5467      unknown.  */
5468   if (! ieee_init_buffer (info, &fndef)
5469       || ! ieee_define_named_type (info, (const char *) NULL,
5470                                    (unsigned int) -1, 0, true, localp,
5471                                    &fndef)
5472       || ! ieee_write_number (info, 'x')
5473       || ! ieee_write_number (info, 0x41)
5474       || ! ieee_write_number (info, 0)
5475       || ! ieee_write_number (info, 0)
5476       || ! ieee_write_number (info, retindx)
5477       || ! ieee_write_number (info, (bfd_vma) argcount + (varargs ? 1 : 0)))
5478     return false;
5479   if (argcount > 0)
5480     {
5481       for (i = 0; i < argcount; i++)
5482         if (! ieee_write_number (info, args[i]))
5483           return false;
5484       free (args);
5485     }
5486   if (varargs)
5487     {
5488       /* A varargs function is represented by writing out the last
5489          argument as type void *, although this makes little sense.  */
5490       if (! ieee_write_number (info, (bfd_vma) builtin_void + 32))
5491         return false;
5492     }
5493
5494   if (! ieee_write_number (info, 0))
5495     return false;
5496
5497   /* We wrote the information into fndef, in case we don't need it.
5498      It will be appended to info->types by ieee_pop_type.  */
5499   info->type_stack->type.fndef = fndef;
5500
5501   if (m != NULL)
5502     m->function = info->type_stack->type.indx;
5503
5504   return true;
5505 }
5506
5507 /* Make a reference type.  */
5508
5509 static boolean
5510 ieee_reference_type (p)
5511      PTR p;
5512 {
5513   struct ieee_handle *info = (struct ieee_handle *) p;
5514
5515   /* IEEE appears to record a normal pointer type, and then use a
5516      pmisc record to indicate that it is really a reference.  */
5517
5518   if (! ieee_pointer_type (p))
5519     return false;
5520   info->type_stack->type.referencep = true;
5521   return true;
5522 }
5523
5524 /* Make a range type.  */
5525
5526 static boolean
5527 ieee_range_type (p, low, high)
5528      PTR p;
5529      bfd_signed_vma low;
5530      bfd_signed_vma high;
5531 {
5532   struct ieee_handle *info = (struct ieee_handle *) p;
5533   unsigned int size;
5534   boolean unsignedp, localp;
5535
5536   size = info->type_stack->type.size;
5537   unsignedp = info->type_stack->type.unsignedp;
5538   localp = info->type_stack->type.localp;
5539   ieee_pop_unused_type (info);
5540   return (ieee_define_type (info, size, unsignedp, localp)
5541           && ieee_write_number (info, 'R')
5542           && ieee_write_number (info, (bfd_vma) low)
5543           && ieee_write_number (info, (bfd_vma) high)
5544           && ieee_write_number (info, unsignedp ? 0 : 1)
5545           && ieee_write_number (info, size));
5546 }
5547
5548 /* Make an array type.  */
5549
5550 /*ARGSUSED*/
5551 static boolean
5552 ieee_array_type (p, low, high, stringp)
5553      PTR p;
5554      bfd_signed_vma low;
5555      bfd_signed_vma high;
5556      boolean stringp;
5557 {
5558   struct ieee_handle *info = (struct ieee_handle *) p;
5559   unsigned int eleindx;
5560   boolean localp;
5561   struct ieee_modified_type *m = NULL;
5562   struct ieee_modified_array_type *a;
5563
5564   /* IEEE does not store the range, so we just ignore it.  */
5565   ieee_pop_unused_type (info);
5566   localp = info->type_stack->type.localp;
5567   eleindx = ieee_pop_type (info);
5568
5569   if (! localp)
5570     {
5571       m = ieee_get_modified_info (info, eleindx);
5572       if (m == NULL)
5573         return false;
5574
5575       for (a = m->arrays; a != NULL; a = a->next)
5576         {
5577           if (a->low == low && a->high == high)
5578             return ieee_push_type (info, a->indx, 0, false, false);
5579         }
5580     }
5581
5582   if (! ieee_define_type (info, 0, false, localp)
5583       || ! ieee_write_number (info, low == 0 ? 'Z' : 'C')
5584       || ! ieee_write_number (info, eleindx))
5585     return false;
5586   if (low != 0)
5587     {
5588       if (! ieee_write_number (info, low))
5589         return false;
5590     }
5591
5592   if (! ieee_write_number (info, high + 1))
5593     return false;
5594
5595   if (! localp)
5596     {
5597       a = (struct ieee_modified_array_type *) xmalloc (sizeof *a);
5598       memset (a, 0, sizeof *a);
5599
5600       a->indx = info->type_stack->type.indx;
5601       a->low = low;
5602       a->high = high;
5603
5604       a->next = m->arrays;
5605       m->arrays = a;
5606     }
5607
5608   return true;
5609 }
5610
5611 /* Make a set type.  */
5612
5613 static boolean
5614 ieee_set_type (p, bitstringp)
5615      PTR p;
5616      boolean bitstringp;
5617 {
5618   struct ieee_handle *info = (struct ieee_handle *) p;
5619   boolean localp;
5620   unsigned int eleindx;
5621
5622   localp = info->type_stack->type.localp;
5623   eleindx = ieee_pop_type (info);
5624
5625   /* FIXME: We don't know the size, so we just use 4.  */
5626
5627   return (ieee_define_type (info, 0, true, localp)
5628           && ieee_write_number (info, 's')
5629           && ieee_write_number (info, 4)
5630           && ieee_write_number (info, eleindx));
5631 }
5632
5633 /* Make an offset type.  */
5634
5635 static boolean
5636 ieee_offset_type (p)
5637      PTR p;
5638 {
5639   struct ieee_handle *info = (struct ieee_handle *) p;
5640   unsigned int targetindx, baseindx;
5641
5642   targetindx = ieee_pop_type (info);
5643   baseindx = ieee_pop_type (info);
5644
5645   /* FIXME: The MRI C++ compiler does not appear to generate any
5646      useful type information about an offset type.  It just records a
5647      pointer to member as an integer.  The MRI/HP IEEE spec does
5648      describe a pmisc record which can be used for a pointer to
5649      member.  Unfortunately, it does not describe the target type,
5650      which seems pretty important.  I'm going to punt this for now.  */
5651
5652   return ieee_int_type (p, 4, true);
5653 }  
5654
5655 /* Make a method type.  */
5656
5657 static boolean
5658 ieee_method_type (p, domain, argcount, varargs)
5659      PTR p;
5660      boolean domain;
5661      int argcount;
5662      boolean varargs;
5663 {
5664   struct ieee_handle *info = (struct ieee_handle *) p;
5665
5666   /* FIXME: The MRI/HP IEEE spec defines a pmisc record to use for a
5667      method, but the definition is incomplete.  We just output an 'x'
5668      type.  */
5669
5670   if (domain)
5671     ieee_pop_unused_type (info);
5672
5673   return ieee_function_type (p, argcount, varargs);
5674 }
5675
5676 /* Make a const qualified type.  */
5677
5678 static boolean
5679 ieee_const_type (p)
5680      PTR p;
5681 {
5682   struct ieee_handle *info = (struct ieee_handle *) p;
5683   unsigned int size;
5684   boolean unsignedp, localp;
5685   unsigned int indx;
5686   struct ieee_modified_type *m = NULL;
5687
5688   size = info->type_stack->type.size;
5689   unsignedp = info->type_stack->type.unsignedp;
5690   localp = info->type_stack->type.localp;
5691   indx = ieee_pop_type (info);
5692
5693   if (! localp)
5694     {
5695       m = ieee_get_modified_info (info, indx);
5696       if (m == NULL)
5697         return false;
5698
5699       if (m->const_qualified > 0)
5700         return ieee_push_type (info, m->const_qualified, size, unsignedp,
5701                                false);
5702     }
5703
5704   if (! ieee_define_type (info, size, unsignedp, localp)
5705       || ! ieee_write_number (info, 'n')
5706       || ! ieee_write_number (info, 1)
5707       || ! ieee_write_number (info, indx))
5708     return false;
5709
5710   if (! localp)
5711     m->const_qualified = info->type_stack->type.indx;
5712
5713   return true;
5714 }
5715
5716 /* Make a volatile qualified type.  */
5717
5718 static boolean
5719 ieee_volatile_type (p)
5720      PTR p;
5721 {
5722   struct ieee_handle *info = (struct ieee_handle *) p;
5723   unsigned int size;
5724   boolean unsignedp, localp;
5725   unsigned int indx;
5726   struct ieee_modified_type *m = NULL;
5727
5728   size = info->type_stack->type.size;
5729   unsignedp = info->type_stack->type.unsignedp;
5730   localp = info->type_stack->type.localp;
5731   indx = ieee_pop_type (info);
5732
5733   if (! localp)
5734     {
5735       m = ieee_get_modified_info (info, indx);
5736       if (m == NULL)
5737         return false;
5738
5739       if (m->volatile_qualified > 0)
5740         return ieee_push_type (info, m->volatile_qualified, size, unsignedp,
5741                                false);
5742     }
5743
5744   if (! ieee_define_type (info, size, unsignedp, localp)
5745       || ! ieee_write_number (info, 'n')
5746       || ! ieee_write_number (info, 2)
5747       || ! ieee_write_number (info, indx))
5748     return false;
5749
5750   if (! localp)
5751     m->volatile_qualified = info->type_stack->type.indx;
5752
5753   return true;
5754 }
5755
5756 /* Convert an enum debug_visibility into a CXXFLAGS value.  */
5757
5758 static unsigned int
5759 ieee_vis_to_flags (visibility)
5760      enum debug_visibility visibility;
5761 {
5762   switch (visibility)
5763     {
5764     default:
5765       abort ();
5766     case DEBUG_VISIBILITY_PUBLIC:
5767       return CXXFLAGS_VISIBILITY_PUBLIC;
5768     case DEBUG_VISIBILITY_PRIVATE:
5769       return CXXFLAGS_VISIBILITY_PRIVATE;
5770     case DEBUG_VISIBILITY_PROTECTED:
5771       return CXXFLAGS_VISIBILITY_PROTECTED;
5772     }
5773   /*NOTREACHED*/
5774 }
5775
5776 /* Start defining a struct type.  We build it in the strdef field on
5777    the stack, to avoid confusing type definitions required by the
5778    fields with the struct type itself.  */
5779
5780 static boolean
5781 ieee_start_struct_type (p, tag, id, structp, size)
5782      PTR p;
5783      const char *tag;
5784      unsigned int id;
5785      boolean structp;
5786      unsigned int size;
5787 {
5788   struct ieee_handle *info = (struct ieee_handle *) p;
5789   boolean localp, ignorep;
5790   boolean copy;
5791   char ab[20];
5792   const char *look;
5793   struct ieee_name_type_hash_entry *h;
5794   struct ieee_name_type *nt, *ntlook;
5795   struct ieee_buflist strdef;
5796
5797   localp = false;
5798   ignorep = false;
5799
5800   /* We need to create a tag for internal use even if we don't want
5801      one for external use.  This will let us refer to an anonymous
5802      struct.  */
5803   if (tag != NULL)
5804     {
5805       look = tag;
5806       copy = false;
5807     }
5808   else
5809     {
5810       sprintf (ab, "__anon%u", id);
5811       look = ab;
5812       copy = true;
5813     }
5814
5815   /* If we already have references to the tag, we must use the
5816      existing type index.  */
5817   h = ieee_name_type_hash_lookup (&info->tags, look, true, copy);
5818   if (h == NULL)
5819     return false;
5820
5821   nt = NULL;
5822   for (ntlook = h->types; ntlook != NULL; ntlook = ntlook->next)
5823     {
5824       if (ntlook->id == id)
5825         nt = ntlook;
5826       else if (! ntlook->type.localp)
5827         {
5828           /* We are creating a duplicate definition of a globally
5829              defined tag.  Force it to be local to avoid
5830              confusion.  */
5831           localp = true;
5832         }
5833     }
5834
5835   if (nt != NULL)
5836     {
5837       assert (localp == nt->type.localp);
5838       if (nt->kind == DEBUG_KIND_ILLEGAL && ! localp)
5839         {
5840           /* We've already seen a global definition of the type.
5841              Ignore this new definition.  */
5842           ignorep = true;
5843         }
5844     }
5845   else
5846     {
5847       nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
5848       memset (nt, 0, sizeof *nt);
5849       nt->id = id;
5850       nt->type.name = h->root.string;
5851       nt->next = h->types;
5852       h->types = nt;
5853       nt->type.indx = info->type_indx;
5854       ++info->type_indx;
5855     }
5856
5857   nt->kind = DEBUG_KIND_ILLEGAL;
5858
5859   if (! ieee_init_buffer (info, &strdef)
5860       || ! ieee_define_named_type (info, tag, nt->type.indx, size, true,
5861                                    localp, &strdef)
5862       || ! ieee_write_number (info, structp ? 'S' : 'U')
5863       || ! ieee_write_number (info, size))
5864     return false;
5865
5866   if (! ignorep)
5867     {
5868       const char *hold;
5869
5870       /* We never want nt->type.name to be NULL.  We want the rest of
5871          the type to be the object set up on the type stack; it will
5872          have a NULL name if tag is NULL.  */
5873       hold = nt->type.name;
5874       nt->type = info->type_stack->type;
5875       nt->type.name = hold;
5876     }
5877
5878   info->type_stack->type.name = tag;
5879   info->type_stack->type.strdef = strdef;
5880   info->type_stack->type.ignorep = ignorep;
5881
5882   return true;
5883 }
5884
5885 /* Add a field to a struct.  */
5886
5887 static boolean
5888 ieee_struct_field (p, name, bitpos, bitsize, visibility)
5889      PTR p;
5890      const char *name;
5891      bfd_vma bitpos;
5892      bfd_vma bitsize;
5893      enum debug_visibility visibility;
5894 {
5895   struct ieee_handle *info = (struct ieee_handle *) p;
5896   unsigned int size;
5897   boolean unsignedp;
5898   boolean referencep;
5899   boolean localp;
5900   unsigned int indx;
5901   bfd_vma offset;
5902
5903   assert (info->type_stack != NULL
5904           && info->type_stack->next != NULL
5905           && ! ieee_buffer_emptyp (&info->type_stack->next->type.strdef));
5906
5907   /* If we are ignoring this struct definition, just pop and ignore
5908      the type.  */
5909   if (info->type_stack->next->type.ignorep)
5910     {
5911       ieee_pop_unused_type (info);
5912       return true;
5913     }
5914
5915   size = info->type_stack->type.size;
5916   unsignedp = info->type_stack->type.unsignedp;
5917   referencep = info->type_stack->type.referencep;
5918   localp = info->type_stack->type.localp;
5919   indx = ieee_pop_type (info);
5920
5921   if (localp)
5922     info->type_stack->type.localp = true;
5923
5924   if (info->type_stack->type.classdef != NULL)
5925     {
5926       unsigned int flags;
5927       unsigned int nindx;
5928
5929       /* This is a class.  We must add a description of this field to
5930          the class records we are building.  */
5931
5932       flags = ieee_vis_to_flags (visibility);
5933       nindx = info->type_stack->type.classdef->indx;
5934       if (! ieee_change_buffer (info,
5935                                 &info->type_stack->type.classdef->pmiscbuf)
5936           || ! ieee_write_asn (info, nindx, 'd')
5937           || ! ieee_write_asn (info, nindx, flags)
5938           || ! ieee_write_atn65 (info, nindx, name)
5939           || ! ieee_write_atn65 (info, nindx, name))
5940         return false;
5941       info->type_stack->type.classdef->pmisccount += 4;
5942
5943       if (referencep)
5944         {
5945           unsigned int nindx;
5946
5947           /* We need to output a record recording that this field is
5948              really of reference type.  We put this on the refs field
5949              of classdef, so that it can be appended to the C++
5950              records after the class is defined.  */
5951
5952           nindx = info->name_indx;
5953           ++info->name_indx;
5954
5955           if (! ieee_change_buffer (info,
5956                                     &info->type_stack->type.classdef->refs)
5957               || ! ieee_write_byte (info, (int) ieee_nn_record)
5958               || ! ieee_write_number (info, nindx)
5959               || ! ieee_write_id (info, "")
5960               || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
5961               || ! ieee_write_number (info, nindx)
5962               || ! ieee_write_number (info, 0)
5963               || ! ieee_write_number (info, 62)
5964               || ! ieee_write_number (info, 80)
5965               || ! ieee_write_number (info, 4)
5966               || ! ieee_write_asn (info, nindx, 'R')
5967               || ! ieee_write_asn (info, nindx, 3)
5968               || ! ieee_write_atn65 (info, nindx, info->type_stack->type.name)
5969               || ! ieee_write_atn65 (info, nindx, name))
5970             return false;
5971         }
5972     }
5973
5974   /* If the bitsize doesn't match the expected size, we need to output
5975      a bitfield type.  */
5976   if (size == 0 || bitsize == 0 || bitsize == size * 8)
5977     offset = bitpos / 8;
5978   else
5979     {
5980       if (! ieee_define_type (info, 0, unsignedp,
5981                               info->type_stack->type.localp)
5982           || ! ieee_write_number (info, 'g')
5983           || ! ieee_write_number (info, unsignedp ? 0 : 1)
5984           || ! ieee_write_number (info, bitsize)
5985           || ! ieee_write_number (info, indx))
5986         return false;
5987       indx = ieee_pop_type (info);
5988       offset = bitpos;
5989     }
5990
5991   /* Switch to the struct we are building in order to output this
5992      field definition.  */
5993   return (ieee_change_buffer (info, &info->type_stack->type.strdef)
5994           && ieee_write_id (info, name)
5995           && ieee_write_number (info, indx)
5996           && ieee_write_number (info, offset));
5997 }
5998
5999 /* Finish up a struct type.  */
6000
6001 static boolean
6002 ieee_end_struct_type (p)
6003      PTR p;
6004 {
6005   struct ieee_handle *info = (struct ieee_handle *) p;
6006   struct ieee_buflist *pb;
6007
6008   assert (info->type_stack != NULL
6009           && ! ieee_buffer_emptyp (&info->type_stack->type.strdef));
6010
6011   /* If we were ignoring this struct definition because it was a
6012      duplicate defintion, just through away whatever bytes we have
6013      accumulated.  Leave the type on the stack. */
6014   if (info->type_stack->type.ignorep)
6015     return true;
6016
6017   /* If this is not a duplicate definition of this tag, then localp
6018      will be false, and we can put it in the global type block.
6019      FIXME: We should avoid outputting duplicate definitions which are
6020      the same.  */
6021   if (! info->type_stack->type.localp)
6022     {
6023       /* Make sure we have started the global type block.  */
6024       if (ieee_buffer_emptyp (&info->global_types))
6025         {
6026           if (! ieee_change_buffer (info, &info->global_types)
6027               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
6028               || ! ieee_write_byte (info, 2)
6029               || ! ieee_write_number (info, 0)
6030               || ! ieee_write_id (info, ""))
6031             return false;
6032         }
6033       pb = &info->global_types;
6034     }
6035   else
6036     {
6037       /* Make sure we have started the types block.  */
6038       if (ieee_buffer_emptyp (&info->types))
6039         {
6040           if (! ieee_change_buffer (info, &info->types)
6041               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
6042               || ! ieee_write_byte (info, 1)
6043               || ! ieee_write_number (info, 0)
6044               || ! ieee_write_id (info, info->modname))
6045             return false;
6046         }
6047       pb = &info->types;
6048     }
6049
6050   /* Append the struct definition to the types.  */
6051   if (! ieee_append_buffer (info, pb, &info->type_stack->type.strdef)
6052       || ! ieee_init_buffer (info, &info->type_stack->type.strdef))
6053     return false;
6054
6055   /* Leave the struct on the type stack.  */
6056
6057   return true;
6058 }
6059
6060 /* Start a class type.  */
6061
6062 static boolean
6063 ieee_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
6064      PTR p;
6065      const char *tag;
6066      unsigned int id;
6067      boolean structp;
6068      unsigned int size;
6069      boolean vptr;
6070      boolean ownvptr;
6071 {
6072   struct ieee_handle *info = (struct ieee_handle *) p;
6073   const char *vclass;
6074   struct ieee_buflist pmiscbuf;
6075   unsigned int indx;
6076   struct ieee_type_class *classdef;
6077
6078   /* A C++ class is output as a C++ struct along with a set of pmisc
6079      records describing the class.  */
6080
6081   /* We need to have a name so that we can associate the struct and
6082      the class.  */
6083   if (tag == NULL)
6084     {
6085       char *t;
6086
6087       t = (char *) xmalloc (20);
6088       sprintf (t, "__anon%u", id);
6089       tag = t;
6090     }
6091
6092   /* We can't write out the virtual table information until we have
6093      finished the class, because we don't know the virtual table size.
6094      We get the size from the largest voffset we see.  */
6095   vclass = NULL;
6096   if (vptr && ! ownvptr)
6097     {
6098       vclass = info->type_stack->type.name;
6099       assert (vclass != NULL);
6100       /* We don't call ieee_pop_unused_type, since the class should
6101          get defined.  */
6102       (void) ieee_pop_type (info);
6103     }
6104
6105   if (! ieee_start_struct_type (p, tag, id, structp, size))
6106     return false;
6107
6108   indx = info->name_indx;
6109   ++info->name_indx;
6110
6111   /* We write out pmisc records into the classdef field.  We will
6112      write out the pmisc start after we know the number of records we
6113      need.  */
6114   if (! ieee_init_buffer (info, &pmiscbuf)
6115       || ! ieee_change_buffer (info, &pmiscbuf)
6116       || ! ieee_write_asn (info, indx, 'T')
6117       || ! ieee_write_asn (info, indx, structp ? 'o' : 'u')
6118       || ! ieee_write_atn65 (info, indx, tag))
6119     return false;
6120
6121   classdef = (struct ieee_type_class *) xmalloc (sizeof *classdef);
6122   memset (classdef, 0, sizeof *classdef);
6123
6124   classdef->indx = indx;
6125   classdef->pmiscbuf = pmiscbuf;
6126   classdef->pmisccount = 3;
6127   classdef->vclass = vclass;
6128   classdef->ownvptr = ownvptr;
6129
6130   info->type_stack->type.classdef = classdef;
6131
6132   return true;
6133 }
6134
6135 /* Add a static member to a class.  */
6136
6137 static boolean
6138 ieee_class_static_member (p, name, physname, visibility)
6139      PTR p;
6140      const char *name;
6141      const char *physname;
6142      enum debug_visibility visibility;
6143 {
6144   struct ieee_handle *info = (struct ieee_handle *) p;
6145   unsigned int flags;
6146   unsigned int nindx;
6147
6148   /* We don't care about the type.  Hopefully there will be a call to
6149      ieee_variable declaring the physical name and the type, since
6150      that is where an IEEE consumer must get the type.  */
6151   ieee_pop_unused_type (info);
6152
6153   assert (info->type_stack != NULL
6154           && info->type_stack->type.classdef != NULL);
6155
6156   flags = ieee_vis_to_flags (visibility);
6157   flags |= CXXFLAGS_STATIC;
6158
6159   nindx = info->type_stack->type.classdef->indx;
6160
6161   if (! ieee_change_buffer (info, &info->type_stack->type.classdef->pmiscbuf)
6162       || ! ieee_write_asn (info, nindx, 'd')
6163       || ! ieee_write_asn (info, nindx, flags)
6164       || ! ieee_write_atn65 (info, nindx, name)
6165       || ! ieee_write_atn65 (info, nindx, physname))
6166     return false;
6167   info->type_stack->type.classdef->pmisccount += 4;
6168
6169   return true;
6170 }
6171
6172 /* Add a base class to a class.  */
6173
6174 static boolean
6175 ieee_class_baseclass (p, bitpos, virtual, visibility)
6176      PTR p;
6177      bfd_vma bitpos;
6178      boolean virtual;
6179      enum debug_visibility visibility;
6180 {
6181   struct ieee_handle *info = (struct ieee_handle *) p;
6182   const char *bname;
6183   boolean localp;
6184   unsigned int bindx;
6185   char *fname;
6186   unsigned int flags;
6187   unsigned int nindx;
6188
6189   assert (info->type_stack != NULL
6190           && info->type_stack->type.name != NULL
6191           && info->type_stack->next != NULL
6192           && info->type_stack->next->type.classdef != NULL
6193           && ! ieee_buffer_emptyp (&info->type_stack->next->type.strdef));
6194
6195   bname = info->type_stack->type.name;
6196   localp = info->type_stack->type.localp;
6197   bindx = ieee_pop_type (info);
6198
6199   /* We are currently defining both a struct and a class.  We must
6200      write out a field definition in the struct which holds the base
6201      class.  The stabs debugging reader will create a field named
6202      _vb$CLASS for a virtual base class, so we just use that.  FIXME:
6203      we should not depend upon a detail of stabs debugging.  */
6204   if (virtual)
6205     {
6206       fname = (char *) xmalloc (strlen (bname) + sizeof "_vb$");
6207       sprintf (fname, "_vb$%s", bname);
6208       flags = BASEFLAGS_VIRTUAL;
6209     }
6210   else
6211     {
6212       if (localp)
6213         info->type_stack->type.localp = true;
6214
6215       fname = (char *) xmalloc (strlen (bname) + sizeof "_b$");
6216       sprintf (fname, "_b$%s", bname);
6217
6218       if (! ieee_change_buffer (info, &info->type_stack->type.strdef)
6219           || ! ieee_write_id (info, fname)
6220           || ! ieee_write_number (info, bindx)
6221           || ! ieee_write_number (info, bitpos / 8))
6222         return false;
6223       flags = 0;
6224     }
6225
6226   if (visibility == DEBUG_VISIBILITY_PRIVATE)
6227     flags |= BASEFLAGS_PRIVATE;
6228
6229   nindx = info->type_stack->type.classdef->indx;
6230
6231   if (! ieee_change_buffer (info, &info->type_stack->type.classdef->pmiscbuf)
6232       || ! ieee_write_asn (info, nindx, 'b')
6233       || ! ieee_write_asn (info, nindx, flags)
6234       || ! ieee_write_atn65 (info, nindx, bname)
6235       || ! ieee_write_asn (info, nindx, 0)
6236       || ! ieee_write_atn65 (info, nindx, fname))
6237     return false;
6238   info->type_stack->type.classdef->pmisccount += 5;
6239
6240   free (fname);
6241
6242   return true;
6243 }
6244
6245 /* Start building a method for a class.  */
6246
6247 static boolean
6248 ieee_class_start_method (p, name)
6249      PTR p;
6250      const char *name;
6251 {
6252   struct ieee_handle *info = (struct ieee_handle *) p;
6253
6254   assert (info->type_stack != NULL
6255           && info->type_stack->type.classdef != NULL
6256           && info->type_stack->type.classdef->method == NULL);
6257
6258   info->type_stack->type.classdef->method = name;
6259
6260   return true;
6261 }
6262
6263 /* Define a new method variant, either static or not.  */
6264
6265 static boolean
6266 ieee_class_method_var (info, physname, visibility, staticp, constp,
6267                        volatilep, voffset, context)
6268      struct ieee_handle *info;
6269      const char *physname;
6270      enum debug_visibility visibility;
6271      boolean staticp;
6272      boolean constp;
6273      boolean volatilep;
6274      bfd_vma voffset;
6275      boolean context;
6276 {
6277   unsigned int flags;
6278   unsigned int nindx;
6279   boolean virtual;
6280
6281   /* We don't need the type of the method.  An IEEE consumer which
6282      wants the type must track down the function by the physical name
6283      and get the type from that.  */
6284   ieee_pop_unused_type (info);
6285
6286   /* We don't use the context.  FIXME: We probably ought to use it to
6287      adjust the voffset somehow, but I don't really know how.  */
6288   if (context)
6289     ieee_pop_unused_type (info);
6290
6291   assert (info->type_stack != NULL
6292           && info->type_stack->type.classdef != NULL
6293           && info->type_stack->type.classdef->method != NULL);
6294
6295   flags = ieee_vis_to_flags (visibility);
6296
6297   /* FIXME: We never set CXXFLAGS_OVERRIDE, CXXFLAGS_OPERATOR,
6298      CXXFLAGS_CTORDTOR, CXXFLAGS_CTOR, or CXXFLAGS_INLINE.  */
6299
6300   if (staticp)
6301     flags |= CXXFLAGS_STATIC;
6302   if (constp)
6303     flags |= CXXFLAGS_CONST;
6304   if (volatilep)
6305     flags |= CXXFLAGS_VOLATILE;
6306
6307   nindx = info->type_stack->type.classdef->indx;
6308
6309   virtual = context || voffset > 0;
6310
6311   if (! ieee_change_buffer (info,
6312                             &info->type_stack->type.classdef->pmiscbuf)
6313       || ! ieee_write_asn (info, nindx, virtual ? 'v' : 'm')
6314       || ! ieee_write_asn (info, nindx, flags)
6315       || ! ieee_write_atn65 (info, nindx,
6316                              info->type_stack->type.classdef->method)
6317       || ! ieee_write_atn65 (info, nindx, physname))
6318     return false;
6319
6320   if (virtual)
6321     {
6322       if (voffset > info->type_stack->type.classdef->voffset)
6323         info->type_stack->type.classdef->voffset = voffset;
6324       if (! ieee_write_asn (info, nindx, voffset))
6325         return false;
6326       ++info->type_stack->type.classdef->pmisccount;
6327     }
6328
6329   if (! ieee_write_asn (info, nindx, 0))
6330     return false;
6331
6332   info->type_stack->type.classdef->pmisccount += 5;
6333
6334   return true;
6335 }
6336
6337 /* Define a new method variant.  */
6338
6339 static boolean
6340 ieee_class_method_variant (p, physname, visibility, constp, volatilep,
6341                            voffset, context)
6342      PTR p;
6343      const char *physname;
6344      enum debug_visibility visibility;
6345      boolean constp;
6346      boolean volatilep;
6347      bfd_vma voffset;
6348      boolean context;
6349 {
6350   struct ieee_handle *info = (struct ieee_handle *) p;
6351
6352   return ieee_class_method_var (info, physname, visibility, false, constp,
6353                                 volatilep, voffset, context);
6354 }
6355
6356 /* Define a new static method variant.  */
6357
6358 static boolean
6359 ieee_class_static_method_variant (p, physname, visibility, constp, volatilep)
6360      PTR p;
6361      const char *physname;
6362      enum debug_visibility visibility;
6363      boolean constp;
6364      boolean volatilep;
6365 {
6366   struct ieee_handle *info = (struct ieee_handle *) p;
6367
6368   return ieee_class_method_var (info, physname, visibility, true, constp,
6369                                 volatilep, 0, false);
6370 }
6371
6372 /* Finish up a method.  */
6373
6374 static boolean
6375 ieee_class_end_method (p)
6376      PTR p;
6377 {
6378   struct ieee_handle *info = (struct ieee_handle *) p;
6379
6380   assert (info->type_stack != NULL
6381           && info->type_stack->type.classdef != NULL
6382           && info->type_stack->type.classdef->method != NULL);
6383
6384   info->type_stack->type.classdef->method = NULL;
6385
6386   return true;
6387 }
6388
6389 /* Finish up a class.  */
6390
6391 static boolean
6392 ieee_end_class_type (p)
6393      PTR p;
6394 {
6395   struct ieee_handle *info = (struct ieee_handle *) p;
6396   unsigned int nindx;
6397
6398   assert (info->type_stack != NULL
6399           && info->type_stack->type.classdef != NULL);
6400
6401   /* If we were ignoring this class definition because it was a
6402      duplicate definition, just through away whatever bytes we have
6403      accumulated.  Leave the type on the stack.  */
6404   if (info->type_stack->type.ignorep)
6405     return true;
6406
6407   nindx = info->type_stack->type.classdef->indx;
6408
6409   /* If we have a virtual table, we can write out the information now.  */
6410   if (info->type_stack->type.classdef->vclass != NULL
6411       || info->type_stack->type.classdef->ownvptr)
6412     {
6413       if (! ieee_change_buffer (info,
6414                                 &info->type_stack->type.classdef->pmiscbuf)
6415           || ! ieee_write_asn (info, nindx, 'z')
6416           || ! ieee_write_atn65 (info, nindx, "")
6417           || ! ieee_write_asn (info, nindx,
6418                                info->type_stack->type.classdef->voffset))
6419         return false;
6420       if (info->type_stack->type.classdef->ownvptr)
6421         {
6422           if (! ieee_write_atn65 (info, nindx, ""))
6423             return false;
6424         }
6425       else
6426         {
6427           if (! ieee_write_atn65 (info, nindx,
6428                                   info->type_stack->type.classdef->vclass))
6429             return false;
6430         }
6431       if (! ieee_write_asn (info, nindx, 0))
6432         return false;
6433       info->type_stack->type.classdef->pmisccount += 5;
6434     }
6435
6436   /* Now that we know the number of pmisc records, we can write out
6437      the atn62 which starts the pmisc records, and append them to the
6438      C++ buffers.  */
6439
6440   if (! ieee_change_buffer (info, &info->cxx)
6441       || ! ieee_write_byte (info, (int) ieee_nn_record)
6442       || ! ieee_write_number (info, nindx)
6443       || ! ieee_write_id (info, "")
6444       || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
6445       || ! ieee_write_number (info, nindx)
6446       || ! ieee_write_number (info, 0)
6447       || ! ieee_write_number (info, 62)
6448       || ! ieee_write_number (info, 80)
6449       || ! ieee_write_number (info,
6450                               info->type_stack->type.classdef->pmisccount))
6451     return false;
6452
6453   if (! ieee_append_buffer (info, &info->cxx,
6454                             &info->type_stack->type.classdef->pmiscbuf))
6455     return false;
6456   if (! ieee_buffer_emptyp (&info->type_stack->type.classdef->refs))
6457     {
6458       if (! ieee_append_buffer (info, &info->cxx,
6459                                 &info->type_stack->type.classdef->refs))
6460         return false;
6461     }
6462
6463   return ieee_end_struct_type (p);
6464 }
6465
6466 /* Push a previously seen typedef onto the type stack.  */
6467
6468 static boolean
6469 ieee_typedef_type (p, name)
6470      PTR p;
6471      const char *name;
6472 {
6473   struct ieee_handle *info = (struct ieee_handle *) p;
6474   struct ieee_name_type_hash_entry *h;
6475   struct ieee_name_type *nt;
6476
6477   h = ieee_name_type_hash_lookup (&info->typedefs, name, false, false);
6478
6479   /* h should never be NULL, since that would imply that the generic
6480      debugging code has asked for a typedef which it has not yet
6481      defined.  */
6482   assert (h != NULL);
6483
6484   /* We always use the most recently defined type for this name, which
6485      will be the first one on the list.  */
6486
6487   nt = h->types;
6488   if (! ieee_push_type (info, nt->type.indx, nt->type.size,
6489                         nt->type.unsignedp, nt->type.localp))
6490     return false;
6491
6492   /* Copy over any other type information we may have.  */
6493   info->type_stack->type = nt->type;
6494
6495   return true;
6496 }
6497
6498 /* Push a tagged type onto the type stack.  */
6499
6500 static boolean
6501 ieee_tag_type (p, name, id, kind)
6502      PTR p;
6503      const char *name;
6504      unsigned int id;
6505      enum debug_type_kind kind;
6506 {
6507   struct ieee_handle *info = (struct ieee_handle *) p;
6508   boolean localp;
6509   boolean copy;
6510   char ab[20];
6511   struct ieee_name_type_hash_entry *h;
6512   struct ieee_name_type *nt;
6513
6514   if (kind == DEBUG_KIND_ENUM)
6515     {
6516       struct ieee_defined_enum *e;
6517
6518       if (name == NULL)
6519         abort ();
6520       for (e = info->enums; e != NULL; e = e->next)
6521         if (e->tag != NULL && strcmp (e->tag, name) == 0)
6522           return ieee_push_type (info, e->indx, 0, true, false);
6523       abort ();
6524     }
6525
6526   localp = false;
6527
6528   copy = false;
6529   if (name == NULL)
6530     {
6531       sprintf (ab, "__anon%u", id);
6532       name = ab;
6533       copy = true;
6534     }
6535
6536   h = ieee_name_type_hash_lookup (&info->tags, name, true, copy);
6537   if (h == NULL)
6538     return false;
6539
6540   for (nt = h->types; nt != NULL; nt = nt->next)
6541     {
6542       if (nt->id == id)
6543         {
6544           if (! ieee_push_type (info, nt->type.indx, nt->type.size,
6545                                 nt->type.unsignedp, nt->type.localp))
6546             return false;
6547           /* Copy over any other type information we may have.  */
6548           info->type_stack->type = nt->type;
6549           return true;
6550         }
6551
6552       if (! nt->type.localp)
6553         {
6554           /* This is a duplicate of a global type, so it must be
6555              local. */
6556           localp = true;
6557         }
6558     }
6559
6560   nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
6561   memset (nt, 0, sizeof *nt);
6562
6563   nt->id = id;
6564   nt->type.name = h->root.string;
6565   nt->type.indx = info->type_indx;
6566   nt->type.localp = localp;
6567   ++info->type_indx;
6568   nt->kind = kind;
6569
6570   nt->next = h->types;
6571   h->types = nt;
6572
6573   if (! ieee_push_type (info, nt->type.indx, 0, false, localp))
6574     return false;
6575
6576   info->type_stack->type.name = h->root.string;
6577
6578   return true;
6579 }
6580
6581 /* Output a typedef.  */
6582
6583 static boolean
6584 ieee_typdef (p, name)
6585      PTR p;
6586      const char *name;
6587 {
6588   struct ieee_handle *info = (struct ieee_handle *) p;
6589   struct ieee_write_type type;
6590   unsigned int indx;
6591   boolean found;
6592   boolean localp;
6593   struct ieee_name_type_hash_entry *h;
6594   struct ieee_name_type *nt;
6595
6596   type = info->type_stack->type;
6597   indx = type.indx;
6598
6599   /* If this is a simple builtin type using a builtin name, we don't
6600      want to output the typedef itself.  We also want to change the
6601      type index to correspond to the name being used.  We recognize
6602      names used in stabs debugging output even if they don't exactly
6603      correspond to the names used for the IEEE builtin types.  */
6604   found = false;
6605   if (indx <= (unsigned int) builtin_bcd_float)
6606     {
6607       switch ((enum builtin_types) indx)
6608         {
6609         default:
6610           break;
6611
6612         case builtin_void:
6613           if (strcmp (name, "void") == 0)
6614             found = true;
6615           break;
6616
6617         case builtin_signed_char:
6618         case builtin_char:
6619           if (strcmp (name, "signed char") == 0)
6620             {
6621               indx = (unsigned int) builtin_signed_char;
6622               found = true;
6623             }
6624           else if (strcmp (name, "char") == 0)
6625             {
6626               indx = (unsigned int) builtin_char;
6627               found = true;
6628             }
6629           break;
6630
6631         case builtin_unsigned_char:
6632           if (strcmp (name, "unsigned char") == 0)
6633             found = true;
6634           break;
6635
6636         case builtin_signed_short_int:
6637         case builtin_short:
6638         case builtin_short_int:
6639         case builtin_signed_short:
6640           if (strcmp (name, "signed short int") == 0)
6641             {
6642               indx = (unsigned int) builtin_signed_short_int;
6643               found = true;
6644             }
6645           else if (strcmp (name, "short") == 0)
6646             {
6647               indx = (unsigned int) builtin_short;
6648               found = true;
6649             }
6650           else if (strcmp (name, "short int") == 0)
6651             {
6652               indx = (unsigned int) builtin_short_int;
6653               found = true;
6654             }
6655           else if (strcmp (name, "signed short") == 0)
6656             {
6657               indx = (unsigned int) builtin_signed_short;
6658               found = true;
6659             }
6660           break;
6661
6662         case builtin_unsigned_short_int:
6663         case builtin_unsigned_short:
6664           if (strcmp (name, "unsigned short int") == 0
6665               || strcmp (name, "short unsigned int") == 0)
6666             {
6667               indx = builtin_unsigned_short_int;
6668               found = true;
6669             }
6670           else if (strcmp (name, "unsigned short") == 0)
6671             {
6672               indx = builtin_unsigned_short;
6673               found = true;
6674             }
6675           break;
6676
6677         case builtin_signed_long:
6678         case builtin_int: /* FIXME: Size depends upon architecture.  */
6679         case builtin_long:
6680           if (strcmp (name, "signed long") == 0)
6681             {
6682               indx = builtin_signed_long;
6683               found = true;
6684             }
6685           else if (strcmp (name, "int") == 0)
6686             {
6687               indx = builtin_int;
6688               found = true;
6689             }
6690           else if (strcmp (name, "long") == 0
6691                    || strcmp (name, "long int") == 0)
6692             {
6693               indx = builtin_long;
6694               found = true;
6695             }
6696           break;
6697
6698         case builtin_unsigned_long:
6699         case builtin_unsigned: /* FIXME: Size depends upon architecture.  */
6700         case builtin_unsigned_int: /* FIXME: Like builtin_unsigned.  */
6701           if (strcmp (name, "unsigned long") == 0
6702               || strcmp (name, "long unsigned int") == 0)
6703             {
6704               indx = builtin_unsigned_long;
6705               found = true;
6706             }
6707           else if (strcmp (name, "unsigned") == 0)
6708             {
6709               indx = builtin_unsigned;
6710               found = true;
6711             }
6712           else if (strcmp (name, "unsigned int") == 0)
6713             {
6714               indx = builtin_unsigned_int;
6715               found = true;
6716             }
6717           break;
6718
6719         case builtin_signed_long_long:
6720           if (strcmp (name, "signed long long") == 0
6721               || strcmp (name, "long long int") == 0)
6722             found = true;
6723           break;
6724
6725         case builtin_unsigned_long_long:
6726           if (strcmp (name, "unsigned long long") == 0
6727               || strcmp (name, "long long unsigned int") == 0)
6728             found = true;
6729           break;
6730
6731         case builtin_float:
6732           if (strcmp (name, "float") == 0)
6733             found = true;
6734           break;
6735
6736         case builtin_double:
6737           if (strcmp (name, "double") == 0)
6738             found = true;
6739           break;
6740
6741         case builtin_long_double:
6742           if (strcmp (name, "long double") == 0)
6743             found = true;
6744           break;
6745
6746         case builtin_long_long_double:
6747           if (strcmp (name, "long long double") == 0)
6748             found = true;
6749           break;
6750         }
6751
6752       if (found)
6753         type.indx = indx;
6754     }
6755
6756   h = ieee_name_type_hash_lookup (&info->typedefs, name, true, false);
6757   if (h == NULL)
6758     return false;
6759
6760   /* See if we have already defined this type with this name.  */
6761   localp = type.localp;
6762   for (nt = h->types; nt != NULL; nt = nt->next)
6763     {
6764       if (nt->id == indx)
6765         {
6766           /* If this is a global definition, then we don't need to
6767              do anything here.  */
6768           if (! nt->type.localp)
6769             {
6770               ieee_pop_unused_type (info);
6771               return true;
6772             }
6773         }
6774       else
6775         {
6776           /* This is a duplicate definition, so make this one local.  */
6777           localp = true;
6778         }
6779     }
6780
6781   /* We need to add a new typedef for this type.  */
6782
6783   nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
6784   memset (nt, 0, sizeof *nt);
6785   nt->id = indx;
6786   nt->type = type;
6787   nt->type.name = name;
6788   nt->type.localp = localp;
6789   nt->kind = DEBUG_KIND_ILLEGAL;
6790
6791   nt->next = h->types;
6792   h->types = nt;
6793
6794   if (found)
6795     {
6796       /* This is one of the builtin typedefs, so we don't need to
6797          actually define it.  */
6798       ieee_pop_unused_type (info);
6799       return true;
6800     }
6801
6802   indx = ieee_pop_type (info);
6803
6804   if (! ieee_define_named_type (info, name, (unsigned int) -1, type.size,
6805                                 type.unsignedp, localp,
6806                                 (struct ieee_buflist *) NULL)
6807       || ! ieee_write_number (info, 'T')
6808       || ! ieee_write_number (info, indx))
6809     return false;
6810
6811   /* Remove the type we just added to the type stack.  This should not
6812      be ieee_pop_unused_type, since the type is used, we just don't
6813      need it now.  */
6814   (void) ieee_pop_type (info);
6815
6816   return true;
6817 }
6818
6819 /* Output a tag for a type.  We don't have to do anything here.  */
6820
6821 static boolean
6822 ieee_tag (p, name)
6823      PTR p;
6824      const char *name;
6825 {
6826   struct ieee_handle *info = (struct ieee_handle *) p;
6827
6828   /* This should not be ieee_pop_unused_type, since we want the type
6829      to be defined.  */
6830   (void) ieee_pop_type (info);
6831   return true;
6832 }
6833
6834 /* Output an integer constant.  */
6835
6836 static boolean
6837 ieee_int_constant (p, name, val)
6838      PTR p;
6839      const char *name;
6840      bfd_vma val;
6841 {
6842   /* FIXME.  */
6843   return true;
6844 }
6845
6846 /* Output a floating point constant.  */
6847
6848 static boolean
6849 ieee_float_constant (p, name, val)
6850      PTR p;
6851      const char *name;
6852      double val;
6853 {
6854   /* FIXME.  */
6855   return true;
6856 }
6857
6858 /* Output a typed constant.  */
6859
6860 static boolean
6861 ieee_typed_constant (p, name, val)
6862      PTR p;
6863      const char *name;
6864      bfd_vma val;
6865 {
6866   struct ieee_handle *info = (struct ieee_handle *) p;
6867
6868   /* FIXME.  */
6869   ieee_pop_unused_type (info);
6870   return true;
6871 }
6872
6873 /* Output a variable.  */
6874
6875 static boolean
6876 ieee_variable (p, name, kind, val)
6877      PTR p;
6878      const char *name;
6879      enum debug_var_kind kind;
6880      bfd_vma val;
6881 {
6882   struct ieee_handle *info = (struct ieee_handle *) p;
6883   unsigned int name_indx;
6884   unsigned int size;
6885   boolean referencep;
6886   unsigned int type_indx;
6887   boolean asn;
6888   int refflag;
6889
6890   size = info->type_stack->type.size;
6891   referencep = info->type_stack->type.referencep;
6892   type_indx = ieee_pop_type (info);
6893
6894   assert (! ieee_buffer_emptyp (&info->vars));
6895   if (! ieee_change_buffer (info, &info->vars))
6896     return false;
6897
6898   name_indx = info->name_indx;
6899   ++info->name_indx;
6900
6901   /* Write out an NN and an ATN record for this variable.  */
6902   if (! ieee_write_byte (info, (int) ieee_nn_record)
6903       || ! ieee_write_number (info, name_indx)
6904       || ! ieee_write_id (info, name)
6905       || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
6906       || ! ieee_write_number (info, name_indx)
6907       || ! ieee_write_number (info, type_indx))
6908     return false;
6909   switch (kind)
6910     {
6911     default:
6912       abort ();
6913       return false;
6914     case DEBUG_GLOBAL:
6915       if (! ieee_write_number (info, 8)
6916           || ! ieee_add_range (info, false, val, val + size))
6917         return false;
6918       refflag = 0;
6919       asn = true;
6920       break;
6921     case DEBUG_STATIC:
6922       if (! ieee_write_number (info, 3)
6923           || ! ieee_add_range (info, false, val, val + size))
6924         return false;
6925       refflag = 1;
6926       asn = true;
6927       break;
6928     case DEBUG_LOCAL_STATIC:
6929       if (! ieee_write_number (info, 3)
6930           || ! ieee_add_range (info, false, val, val + size))
6931         return false;
6932       refflag = 2;
6933       asn = true;
6934       break;
6935     case DEBUG_LOCAL:
6936       if (! ieee_write_number (info, 1)
6937           || ! ieee_write_number (info, val))
6938         return false;
6939       refflag = 2;
6940       asn = false;
6941       break;
6942     case DEBUG_REGISTER:
6943       if (! ieee_write_number (info, 2)
6944           || ! ieee_write_number (info,
6945                                   ieee_genreg_to_regno (info->abfd, val)))
6946         return false;
6947       refflag = 2;
6948       asn = false;
6949       break;
6950     }
6951
6952   if (asn)
6953     {
6954       if (! ieee_write_asn (info, name_indx, val))
6955         return false;
6956     }
6957
6958   /* If this is really a reference type, then we just output it with
6959      pointer type, and must now output a C++ record indicating that it
6960      is really reference type.  */
6961   if (referencep)
6962     {
6963       unsigned int nindx;
6964
6965       nindx = info->name_indx;
6966       ++info->name_indx;
6967
6968       /* If this is a global variable, we want to output the misc
6969          record in the C++ misc record block.  Otherwise, we want to
6970          output it just after the variable definition, which is where
6971          the current buffer is.  */
6972       if (refflag != 2)
6973         {
6974           if (! ieee_change_buffer (info, &info->cxx))
6975             return false;
6976         }
6977
6978       if (! ieee_write_byte (info, (int) ieee_nn_record)
6979           || ! ieee_write_number (info, nindx)
6980           || ! ieee_write_id (info, "")
6981           || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
6982           || ! ieee_write_number (info, nindx)
6983           || ! ieee_write_number (info, 0)
6984           || ! ieee_write_number (info, 62)
6985           || ! ieee_write_number (info, 80)
6986           || ! ieee_write_number (info, 3)
6987           || ! ieee_write_asn (info, nindx, 'R')
6988           || ! ieee_write_asn (info, nindx, refflag)
6989           || ! ieee_write_atn65 (info, nindx, name))
6990         return false;
6991     }
6992
6993   return true;
6994 }
6995
6996 /* Start outputting information for a function.  */
6997
6998 static boolean
6999 ieee_start_function (p, name, global)
7000      PTR p;
7001      const char *name;
7002      boolean global;
7003 {
7004   struct ieee_handle *info = (struct ieee_handle *) p;
7005   boolean referencep;
7006   unsigned int retindx, typeindx;
7007
7008   referencep = info->type_stack->type.referencep;
7009   retindx = ieee_pop_type (info);
7010
7011   /* Besides recording a BB4 or BB6 block, we record the type of the
7012      function in the BB1 typedef block.  We can't write out the full
7013      type until we have seen all the parameters, so we accumulate it
7014      in info->fntype and info->fnargs.  */
7015   if (! ieee_buffer_emptyp (&info->fntype))
7016     {
7017       /* FIXME: This might happen someday if we support nested
7018          functions.  */
7019       abort ();
7020     }
7021
7022   info->fnname = name;
7023
7024   /* An attribute of 0x40 means that the push mask is unknown.  */
7025   if (! ieee_define_named_type (info, name, (unsigned int) -1, 0, false, true,
7026                                 &info->fntype)
7027       || ! ieee_write_number (info, 'x')
7028       || ! ieee_write_number (info, 0x40)
7029       || ! ieee_write_number (info, 0)
7030       || ! ieee_write_number (info, 0)
7031       || ! ieee_write_number (info, retindx))
7032     return false;
7033
7034   typeindx = ieee_pop_type (info);
7035
7036   if (! ieee_init_buffer (info, &info->fnargs))
7037     return false;
7038   info->fnargcount = 0;
7039
7040   /* If the function return value is actually a reference type, we
7041      must add a record indicating that.  */
7042   if (referencep)
7043     {
7044       unsigned int nindx;
7045
7046       nindx = info->name_indx;
7047       ++info->name_indx;
7048       if (! ieee_change_buffer (info, &info->cxx)
7049           || ! ieee_write_byte (info, (int) ieee_nn_record)
7050           || ! ieee_write_number (info, nindx)
7051           || ! ieee_write_id (info, "")
7052           || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
7053           || ! ieee_write_number (info, nindx)
7054           || ! ieee_write_number (info, 0)
7055           || ! ieee_write_number (info, 62)
7056           || ! ieee_write_number (info, 80)
7057           || ! ieee_write_number (info, 3)
7058           || ! ieee_write_asn (info, nindx, 'R')
7059           || ! ieee_write_asn (info, nindx, global ? 0 : 1)
7060           || ! ieee_write_atn65 (info, nindx, name))
7061         return false;
7062     }
7063
7064   assert (! ieee_buffer_emptyp (&info->vars));
7065   if (! ieee_change_buffer (info, &info->vars))
7066     return false;
7067
7068   /* The address is written out as the first block.  */
7069
7070   ++info->block_depth;
7071
7072   return (ieee_write_byte (info, (int) ieee_bb_record_enum)
7073           && ieee_write_byte (info, global ? 4 : 6)
7074           && ieee_write_number (info, 0)
7075           && ieee_write_id (info, name)
7076           && ieee_write_number (info, 0)
7077           && ieee_write_number (info, typeindx));
7078 }
7079
7080 /* Add a function parameter.  This will normally be called before the
7081    first block, so we postpone them until we see the block.  */
7082
7083 static boolean
7084 ieee_function_parameter (p, name, kind, val)
7085      PTR p;
7086      const char *name;
7087      enum debug_parm_kind kind;
7088      bfd_vma val;
7089 {
7090   struct ieee_handle *info = (struct ieee_handle *) p;
7091   struct ieee_pending_parm *m, **pm;
7092
7093   assert (info->block_depth == 1);
7094
7095   m = (struct ieee_pending_parm *) xmalloc (sizeof *m);
7096   memset (m, 0, sizeof *m);
7097
7098   m->next = NULL;
7099   m->name = name;
7100   m->referencep = info->type_stack->type.referencep;
7101   m->type = ieee_pop_type (info);
7102   m->kind = kind;
7103   m->val = val;
7104
7105   for (pm = &info->pending_parms; *pm != NULL; pm = &(*pm)->next)
7106     ;
7107   *pm = m;
7108
7109   /* Add the type to the fnargs list.  */
7110   if (! ieee_change_buffer (info, &info->fnargs)
7111       || ! ieee_write_number (info, m->type))
7112     return false;
7113   ++info->fnargcount;
7114
7115   return true;  
7116 }
7117
7118 /* Output pending function parameters.  */
7119
7120 static boolean
7121 ieee_output_pending_parms (info)
7122      struct ieee_handle *info;
7123 {
7124   struct ieee_pending_parm *m;
7125   unsigned int refcount;
7126
7127   refcount = 0;
7128   for (m = info->pending_parms; m != NULL; m = m->next)
7129     {
7130       enum debug_var_kind vkind;
7131
7132       switch (m->kind)
7133         {
7134         default:
7135           abort ();
7136           return false;
7137         case DEBUG_PARM_STACK:
7138         case DEBUG_PARM_REFERENCE:
7139           vkind = DEBUG_LOCAL;
7140           break;
7141         case DEBUG_PARM_REG:
7142         case DEBUG_PARM_REF_REG:
7143           vkind = DEBUG_REGISTER;
7144           break;
7145         }
7146
7147       if (! ieee_push_type (info, m->type, 0, false, false))
7148         return false;
7149       info->type_stack->type.referencep = m->referencep;
7150       if (m->referencep)
7151         ++refcount;
7152       if (! ieee_variable ((PTR) info, m->name, vkind, m->val))
7153         return false;
7154     }
7155
7156   /* If there are any reference parameters, we need to output a
7157      miscellaneous record indicating them.  */
7158   if (refcount > 0)
7159     {
7160       unsigned int nindx, varindx;
7161
7162       /* FIXME: The MRI compiler outputs the demangled function name
7163          here, but we are outputting the mangled name.  */
7164       nindx = info->name_indx;
7165       ++info->name_indx;
7166       if (! ieee_change_buffer (info, &info->vars)
7167           || ! ieee_write_byte (info, (int) ieee_nn_record)
7168           || ! ieee_write_number (info, nindx)
7169           || ! ieee_write_id (info, "")
7170           || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
7171           || ! ieee_write_number (info, nindx)
7172           || ! ieee_write_number (info, 0)
7173           || ! ieee_write_number (info, 62)
7174           || ! ieee_write_number (info, 80)
7175           || ! ieee_write_number (info, refcount + 3)
7176           || ! ieee_write_asn (info, nindx, 'B')
7177           || ! ieee_write_atn65 (info, nindx, info->fnname)
7178           || ! ieee_write_asn (info, nindx, 0))
7179         return false;
7180       for (m = info->pending_parms, varindx = 1;
7181            m != NULL;
7182            m = m->next, varindx++)
7183         {
7184           if (m->referencep)
7185             {
7186               if (! ieee_write_asn (info, nindx, varindx))
7187                 return false;
7188             }
7189         }
7190     }
7191
7192   m = info->pending_parms;
7193   while (m != NULL)
7194     {
7195       struct ieee_pending_parm *next;
7196
7197       next = m->next;
7198       free (m);
7199       m = next;
7200     }
7201
7202   info->pending_parms = NULL;
7203
7204   return true;
7205 }
7206
7207 /* Start a block.  If this is the first block, we output the address
7208    to finish the BB4 or BB6, and then output the function parameters.  */
7209
7210 static boolean
7211 ieee_start_block (p, addr)
7212      PTR p;
7213      bfd_vma addr;
7214 {
7215   struct ieee_handle *info = (struct ieee_handle *) p;
7216
7217   if (! ieee_change_buffer (info, &info->vars))
7218     return false;
7219
7220   if (info->block_depth == 1)
7221     {
7222       if (! ieee_write_number (info, addr)
7223           || ! ieee_output_pending_parms (info))
7224         return false;
7225     }
7226   else
7227     {
7228       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
7229           || ! ieee_write_byte (info, 6)
7230           || ! ieee_write_number (info, 0)
7231           || ! ieee_write_id (info, "")
7232           || ! ieee_write_number (info, 0)
7233           || ! ieee_write_number (info, 0)
7234           || ! ieee_write_number (info, addr))
7235         return false;
7236     }
7237
7238   if (! ieee_start_range (info, addr))
7239     return false;
7240
7241   ++info->block_depth;
7242
7243   return true;
7244 }
7245
7246 /* End a block.  */
7247
7248 static boolean
7249 ieee_end_block (p, addr)
7250      PTR p;
7251      bfd_vma addr;
7252 {
7253   struct ieee_handle *info = (struct ieee_handle *) p;
7254
7255   /* The address we are given is the end of the block, but IEEE seems
7256      to want to the address of the last byte in the block, so we
7257      subtract one.  */
7258   if (! ieee_change_buffer (info, &info->vars)
7259       || ! ieee_write_byte (info, (int) ieee_be_record_enum)
7260       || ! ieee_write_number (info, addr - 1))
7261     return false;
7262
7263   if (! ieee_end_range (info, addr))
7264     return false;
7265
7266   --info->block_depth;
7267
7268   if (addr > info->highaddr)
7269     info->highaddr = addr;
7270
7271   return true;
7272 }
7273
7274 /* End a function.  */
7275
7276 static boolean
7277 ieee_end_function (p)
7278      PTR p;
7279 {
7280   struct ieee_handle *info = (struct ieee_handle *) p;
7281
7282   assert (info->block_depth == 1);
7283
7284   --info->block_depth;
7285
7286   /* Now we can finish up fntype, and add it to the typdef section.
7287      At this point, fntype is the 'x' type up to the argument count,
7288      and fnargs is the argument types.  We must add the argument
7289      count, and we must add the level.  FIXME: We don't record varargs
7290      functions correctly.  In fact, stabs debugging does not give us
7291      enough information to do so.  */
7292   if (! ieee_change_buffer (info, &info->fntype)
7293       || ! ieee_write_number (info, info->fnargcount)
7294       || ! ieee_change_buffer (info, &info->fnargs)
7295       || ! ieee_write_number (info, 0))
7296     return false;
7297
7298   /* Make sure the typdef block has been started.  */
7299   if (ieee_buffer_emptyp (&info->types))
7300     {
7301       if (! ieee_change_buffer (info, &info->types)
7302           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
7303           || ! ieee_write_byte (info, 1)
7304           || ! ieee_write_number (info, 0)
7305           || ! ieee_write_id (info, info->modname))
7306         return false;
7307     }
7308
7309   if (! ieee_append_buffer (info, &info->types, &info->fntype)
7310       || ! ieee_append_buffer (info, &info->types, &info->fnargs))
7311     return false;
7312
7313   info->fnname = NULL;
7314   if (! ieee_init_buffer (info, &info->fntype)
7315       || ! ieee_init_buffer (info, &info->fnargs))
7316     return false;
7317   info->fnargcount = 0;
7318
7319   return true;
7320 }
7321
7322 /* Record line number information.  */
7323
7324 static boolean
7325 ieee_lineno (p, filename, lineno, addr)
7326      PTR p;
7327      const char *filename;
7328      unsigned long lineno;
7329      bfd_vma addr;
7330 {
7331   struct ieee_handle *info = (struct ieee_handle *) p;
7332
7333   assert (info->filename != NULL);
7334
7335   /* The HP simulator seems to get confused when more than one line is
7336      listed for the same address, at least if they are in different
7337      files.  We handle this by always listing the last line for a
7338      given address, since that seems to be the one that gdb uses.  */
7339   if (info->pending_lineno_filename != NULL
7340       && addr != info->pending_lineno_addr)
7341     {
7342       /* Make sure we have a line number block.  */
7343       if (! ieee_buffer_emptyp (&info->linenos))
7344         {
7345           if (! ieee_change_buffer (info, &info->linenos))
7346             return false;
7347         }
7348       else
7349         {
7350           info->lineno_name_indx = info->name_indx;
7351           ++info->name_indx;
7352           if (! ieee_change_buffer (info, &info->linenos)
7353               || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
7354               || ! ieee_write_byte (info, 5)
7355               || ! ieee_write_number (info, 0)
7356               || ! ieee_write_id (info, info->filename)
7357               || ! ieee_write_byte (info, (int) ieee_nn_record)
7358               || ! ieee_write_number (info, info->lineno_name_indx)
7359               || ! ieee_write_id (info, ""))
7360             return false;
7361           info->lineno_filename = info->filename;
7362         }
7363
7364       if (strcmp (info->pending_lineno_filename, info->lineno_filename) != 0)
7365         {
7366           if (strcmp (info->filename, info->lineno_filename) != 0)
7367             {
7368               /* We were not in the main file.  Close the block for the
7369                  included file.  */
7370               if (! ieee_write_byte (info, (int) ieee_be_record_enum))
7371                 return false;
7372               if (strcmp (info->filename, info->pending_lineno_filename) == 0)
7373                 {
7374                   /* We need a new NN record, and we aren't about to
7375                      output one.  */
7376                   info->lineno_name_indx = info->name_indx;
7377                   ++info->name_indx;
7378                   if (! ieee_write_byte (info, (int) ieee_nn_record)
7379                       || ! ieee_write_number (info, info->lineno_name_indx)
7380                       || ! ieee_write_id (info, ""))
7381                     return false;
7382                 }
7383             }
7384           if (strcmp (info->filename, info->pending_lineno_filename) != 0)
7385             {
7386               /* We are not changing to the main file.  Open a block for
7387                  the new included file.  */
7388               info->lineno_name_indx = info->name_indx;
7389               ++info->name_indx;
7390               if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
7391                   || ! ieee_write_byte (info, 5)
7392                   || ! ieee_write_number (info, 0)
7393                   || ! ieee_write_id (info, info->pending_lineno_filename)
7394                   || ! ieee_write_byte (info, (int) ieee_nn_record)
7395                   || ! ieee_write_number (info, info->lineno_name_indx)
7396                   || ! ieee_write_id (info, ""))
7397                 return false;
7398             }
7399           info->lineno_filename = info->pending_lineno_filename;
7400         }
7401
7402       if (! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
7403           || ! ieee_write_number (info, info->lineno_name_indx)
7404           || ! ieee_write_number (info, 0)
7405           || ! ieee_write_number (info, 7)
7406           || ! ieee_write_number (info, info->pending_lineno)
7407           || ! ieee_write_number (info, 0)
7408           || ! ieee_write_asn (info, info->lineno_name_indx,
7409                                info->pending_lineno_addr))
7410         return false;
7411     }
7412
7413   info->pending_lineno_filename = filename;
7414   info->pending_lineno = lineno;
7415   info->pending_lineno_addr = addr;
7416
7417   return true;
7418 }