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