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