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