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