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