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