* debug.h (struct debug_write_fns): Remove ellipsis_type. Add int
[platform/upstream/binutils.git] / binutils / ieee.c
1 /* ieee.c -- Write out IEEE-695 debugging information.
2    Copyright (C) 1996 Free Software Foundation, Inc.
3    Written by Ian Lance Taylor <ian@cygnus.com>.
4
5    This file is part of GNU Binutils.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 /* This file reads and writes IEEE-695 debugging information.  */
23
24 #include <stdio.h>
25 #include <assert.h>
26
27 #include "bfd.h"
28 #include "ieee.h"
29 #include "bucomm.h"
30 #include "libiberty.h"
31 #include "debug.h"
32 #include "budbg.h"
33
34 /* This structure holds an entry on the block stack.  */
35
36 struct ieee_block
37 {
38   /* The kind of block.  */
39   int kind;
40   /* The source file name, for a BB5 block.  */
41   const char *filename;
42 };
43
44 /* This structure is the block stack.  */
45
46 #define BLOCKSTACK_SIZE (16)
47
48 struct ieee_blockstack
49 {
50   /* The stack pointer.  */
51   struct ieee_block *bsp;
52   /* The stack.  */
53   struct ieee_block stack[BLOCKSTACK_SIZE];
54 };
55
56 /* This structure holds information for a variable.  */
57
58 struct ieee_var
59 {
60   /* Start of name.  */
61   const char *name;
62   /* Length of name.  */
63   unsigned long namlen;
64   /* Type.  */
65   debug_type type;
66 };
67
68 /* This structure holds all the variables.  */
69
70 struct ieee_vars
71 {
72   /* Number of slots allocated.  */
73   unsigned int alloc;
74   /* Variables.  */
75   struct ieee_var *vars;
76 };
77
78 /* This structure holds information for a type.  We need this because
79    we don't want to represent bitfields as real types.  */
80
81 struct ieee_type
82 {
83   /* Type.  */
84   debug_type type;
85   /* Slot if this is type is referenced before it is defined.  */
86   debug_type *pslot;
87   /* If this is a bitfield, this is the size in bits.  If this is not
88      a bitfield, this is zero.  */
89   unsigned long bitsize;
90   /* If this is a function type ('x' or 'X') this is the return type.  */
91   debug_type return_type;
92 };
93
94 /* This structure holds all the type information.  */
95
96 struct ieee_types
97 {
98   /* Number of slots allocated.  */
99   unsigned int alloc;
100   /* Types.  */
101   struct ieee_type *types;
102   /* Builtin types.  */
103 #define BUILTIN_TYPE_COUNT (60)
104   debug_type builtins[BUILTIN_TYPE_COUNT];
105 };
106
107 /* Basic builtin types, not including the pointers.  */
108
109 enum builtin_types
110 {
111   builtin_unknown = 0,
112   builtin_void = 1,
113   builtin_signed_char = 2,
114   builtin_unsigned_char = 3,
115   builtin_signed_short_int = 4,
116   builtin_unsigned_short_int = 5,
117   builtin_signed_long = 6,
118   builtin_unsigned_long = 7,
119   builtin_signed_long_long = 8,
120   builtin_unsigned_long_long = 9,
121   builtin_float = 10,
122   builtin_double = 11,
123   builtin_long_double = 12,
124   builtin_long_long_double = 13,
125   builtin_quoted_string = 14,
126   builtin_instruction_address = 15,
127   builtin_int = 16,
128   builtin_unsigned = 17,
129   builtin_unsigned_int = 18,
130   builtin_char = 19,
131   builtin_long = 20,
132   builtin_short = 21,
133   builtin_unsigned_short = 22,
134   builtin_short_int = 23,
135   builtin_signed_short = 24,
136   builtin_bcd_float = 25
137 };
138
139 static void ieee_error
140   PARAMS ((bfd *, const bfd_byte *, const bfd_byte *, const char *));
141 static void ieee_eof PARAMS ((bfd *));
142 static char *savestring PARAMS ((const char *, unsigned long));
143 static boolean ieee_read_number
144   PARAMS ((bfd *, const bfd_byte *, const bfd_byte **, const bfd_byte *,
145            bfd_vma *));
146 static boolean ieee_read_optional_number
147   PARAMS ((bfd *, const bfd_byte *, const bfd_byte **, const bfd_byte *,
148            bfd_vma *, boolean *));
149 static boolean ieee_read_id
150   PARAMS ((bfd *, const bfd_byte *, const bfd_byte **, const bfd_byte *,
151            const char **, unsigned long *));
152 static boolean ieee_read_optional_id
153   PARAMS ((bfd *, const bfd_byte *, const bfd_byte **, const bfd_byte *,
154            const char **, unsigned long *, boolean *));
155 static boolean ieee_read_expression
156   PARAMS ((bfd *, const bfd_byte *, const bfd_byte **, const bfd_byte *,
157            bfd_vma *));
158 static debug_type ieee_builtin_type
159   PARAMS ((PTR, bfd *, struct ieee_types *, const bfd_byte *,
160            const bfd_byte *, unsigned int));
161 static boolean ieee_alloc_type
162   PARAMS ((PTR, struct ieee_types *, unsigned int, boolean));
163 static boolean ieee_read_type_index
164   PARAMS ((PTR, bfd *, struct ieee_types *, const bfd_byte *,
165            const bfd_byte **, const bfd_byte *, debug_type *));
166 static int ieee_regno_to_genreg PARAMS ((bfd *, int));
167 static int ieee_genreg_to_regno PARAMS ((bfd *, int));
168 static boolean parse_ieee_bb
169   PARAMS ((PTR, bfd *, struct ieee_types *, struct ieee_blockstack *,
170            const bfd_byte *, const bfd_byte **, const bfd_byte *));
171 static boolean parse_ieee_be
172   PARAMS ((PTR, bfd *, struct ieee_blockstack *, const bfd_byte *,
173            const bfd_byte **, const bfd_byte *));
174 static boolean parse_ieee_nn
175   PARAMS ((PTR, bfd *, struct ieee_vars *, const bfd_byte *,
176            const bfd_byte **, const bfd_byte *));
177 static boolean parse_ieee_ty
178   PARAMS ((PTR, bfd *, struct ieee_types *, struct ieee_vars *,
179            const bfd_byte *, const bfd_byte **, const bfd_byte *));
180 static boolean parse_ieee_atn
181   PARAMS ((PTR, bfd *, struct ieee_types *, struct ieee_vars *, int,
182            const bfd_byte *, const bfd_byte **, const bfd_byte *));
183 static boolean ieee_require_asn
184   PARAMS ((bfd *, const bfd_byte *, const bfd_byte **, const bfd_byte *,
185            bfd_vma *));
186
187 /* Report an error in the IEEE debugging information.  */
188
189 static void
190 ieee_error (abfd, bytes, p, s)
191      bfd *abfd;
192      const bfd_byte *bytes;
193      const bfd_byte *p;
194      const char *s;
195 {
196   if (p != NULL)
197     fprintf (stderr, "%s: 0x%lx: %s (0x%x)\n", bfd_get_filename (abfd),
198              (unsigned long) (p - bytes), s, *p);
199   else
200     fprintf (stderr, "%s: %s\n", bfd_get_filename (abfd), s);
201 }
202
203 /* Report an unexpected EOF in the IEEE debugging information.  */
204
205 static void
206 ieee_eof (abfd)
207      bfd *abfd;
208 {
209   ieee_error (abfd, (const bfd_byte *) NULL, (const bfd_byte *) NULL,
210               "unexpected end of debugging information");
211 }
212
213 /* Save a string in memory.  */
214
215 static char *
216 savestring (start, len)
217      const char *start;
218      unsigned long len;
219 {
220   char *ret;
221
222   ret = (char *) xmalloc (len + 1);
223   memcpy (ret, start, len);
224   ret[len] = '\0';
225   return ret;
226 }
227
228 /* Read a number which must be present in an IEEE file.  */
229
230 static boolean
231 ieee_read_number (abfd, bytes, pp, pend, pv)
232      bfd *abfd;
233      const bfd_byte *bytes;
234      const bfd_byte **pp;
235      const bfd_byte *pend;
236      bfd_vma *pv;
237 {
238   return ieee_read_optional_number (abfd, bytes, pp, pend, pv,
239                                     (boolean *) NULL);
240 }
241
242 /* Read a number in an IEEE file.  If ppresent is not NULL, the number
243    need not be there. */
244
245 static boolean
246 ieee_read_optional_number (abfd, bytes, pp, pend, pv, ppresent)
247      bfd *abfd;
248      const bfd_byte *bytes;
249      const bfd_byte **pp;
250      const bfd_byte *pend;
251      bfd_vma *pv;
252      boolean *ppresent;
253 {
254   ieee_record_enum_type b;
255
256   if (*pp >= pend)
257     {
258       if (ppresent != NULL)
259         {
260           *ppresent = false;
261           return true;
262         }
263       ieee_eof (abfd);
264       return false;
265     }
266
267   b = (ieee_record_enum_type) **pp;
268   ++*pp;
269
270   if (b <= ieee_number_end_enum)
271     {
272       *pv = (bfd_vma) b;
273       if (ppresent != NULL)
274         *ppresent = true;
275       return true;
276     }
277
278   if (b >= ieee_number_repeat_start_enum && b <= ieee_number_repeat_end_enum)
279     {
280       unsigned int i;
281
282       i = (int) b - (int) ieee_number_repeat_start_enum;
283       if (*pp + i - 1 >= pend)
284         {
285           ieee_eof (abfd);
286           return false;
287         }
288
289       *pv = 0;
290       for (; i > 0; i--)
291         {
292           *pv <<= 8;
293           *pv += **pp;
294           ++*pp;
295         }
296
297       if (ppresent != NULL)
298         *ppresent = true;
299
300       return true;
301     }
302
303   if (ppresent != NULL)
304     {
305       --*pp;
306       *ppresent = false;
307       return true;
308     }
309
310   ieee_error (abfd, bytes, *pp - 1, "invalid number");
311   return false;  
312 }
313
314 /* Read a required string from an IEEE file.  */
315
316 static boolean
317 ieee_read_id (abfd, bytes, pp, pend, pname, pnamlen)
318      bfd *abfd;
319      const bfd_byte *bytes;
320      const bfd_byte **pp;
321      const bfd_byte *pend;
322      const char **pname;
323      unsigned long *pnamlen;
324 {
325   return ieee_read_optional_id (abfd, bytes, pp, pend, pname, pnamlen,
326                                 (boolean *) NULL);
327 }
328
329 /* Read a string from an IEEE file.  If ppresent is not NULL, the
330    string is optional.  */
331
332 static boolean
333 ieee_read_optional_id (abfd, bytes, pp, pend, pname, pnamlen, ppresent)
334      bfd *abfd;
335      const bfd_byte *bytes;
336      const bfd_byte **pp;
337      const bfd_byte *pend;
338      const char **pname;
339      unsigned long *pnamlen;
340      boolean *ppresent;
341 {
342   bfd_byte b;
343   unsigned long len;
344
345   if (*pp >= pend)
346     {
347       ieee_eof (abfd);
348       return false;
349     }
350
351   b = **pp;
352   ++*pp;
353
354   if (b <= 0x7f)
355     len = b;
356   else if ((ieee_record_enum_type) b == ieee_extension_length_1_enum)
357     {
358       len = **pp;
359       ++*pp;
360     }
361   else if ((ieee_record_enum_type) b == ieee_extension_length_2_enum)
362     {
363       len = (**pp << 8) + (*pp)[1];
364       *pp += 2;
365     }
366   else
367     {
368       if (ppresent != NULL)
369         {
370           --*pp;
371           *ppresent = false;
372           return true;
373         }
374       ieee_error (abfd, bytes, *pp - 1, "invalid string length");
375       return false;
376     }
377
378   if ((unsigned long) (pend - *pp) < len)
379     {
380       ieee_eof (abfd);
381       return false;
382     }
383
384   *pname = (const char *) *pp;
385   *pnamlen = len;
386   *pp += len;
387
388   if (ppresent != NULL)
389     *ppresent = true;
390
391   return true;
392 }
393
394 /* Read an expression from an IEEE file.  Since this code is only used
395    to parse debugging information, I haven't bothered to write a full
396    blown IEEE expression parser.  I've only thrown in the things I've
397    seen in debugging information.  This can be easily extended if
398    necessary.  */
399
400 static boolean
401 ieee_read_expression (abfd, bytes, pp, pend, pv)
402      bfd *abfd;
403      const bfd_byte *bytes;
404      const bfd_byte **pp;
405      const bfd_byte *pend;
406      bfd_vma *pv;
407 {
408   const bfd_byte *expr_start;
409 #define EXPR_STACK_SIZE (10)
410   bfd_vma expr_stack[EXPR_STACK_SIZE];
411   bfd_vma *esp;
412
413   expr_start = *pp;
414
415   esp = expr_stack;
416
417   while (1)
418     {
419       const bfd_byte *start;
420       bfd_vma val;
421       boolean present;
422       ieee_record_enum_type c;
423
424       start = *pp;
425
426       if (! ieee_read_optional_number (abfd, bytes, pp, pend, &val, &present))
427         return false;
428
429       if (present)
430         {
431           if (esp - expr_stack >= EXPR_STACK_SIZE)
432             {
433               ieee_error (abfd, bytes, start, "expression stack overflow");
434               return false;
435             }
436           *esp++ = val;
437           continue;
438         }
439
440       c = (ieee_record_enum_type) **pp;
441
442       if (c >= ieee_module_beginning_enum)
443         break;
444
445       ++*pp;
446
447       if (c == ieee_comma)
448         break;
449
450       switch (c)
451         {
452         default:
453           ieee_error (abfd, bytes, start,
454                       "unsupported IEEE expression operator");
455           break;
456
457         case ieee_variable_R_enum:
458           {
459             bfd_vma indx;
460             asection *s;
461
462             if (! ieee_read_number (abfd, bytes, pp, pend, &indx))
463               return false;
464             for (s = abfd->sections; s != NULL; s = s->next)
465               if ((bfd_vma) s->target_index == indx)
466                 break;
467             if (s == NULL)
468               {
469                 ieee_error (abfd, bytes, start, "unknown section");
470                 return false;
471               }
472             
473             if (esp - expr_stack >= EXPR_STACK_SIZE)
474               {
475                 ieee_error (abfd, bytes, start, "expression stack overflow");
476                 return false;
477               }
478
479             *esp++ = bfd_get_section_vma (abfd, s);
480           }
481           break;
482
483         case ieee_function_plus_enum:
484         case ieee_function_minus_enum:
485           {
486             bfd_vma v1, v2;
487
488             if (esp - expr_stack < 2)
489               {
490                 ieee_error (abfd, bytes, start, "expression stack underflow");
491                 return false;
492               }
493
494             v1 = *--esp;
495             v2 = *--esp;
496             *esp++ = v1 + v2;
497           }
498           break;
499         }
500     }
501
502   if (esp - 1 != expr_stack)
503     {
504       ieee_error (abfd, bytes, expr_start, "expression stack mismatch");
505       return false;
506     }
507
508   *pv = *--esp;
509
510   return true;
511 }
512
513 /* Return an IEEE builtin type.  */
514
515 static debug_type
516 ieee_builtin_type (dhandle, abfd, types, bytes, p, indx)
517      PTR dhandle;
518      bfd *abfd;
519      struct ieee_types *types;
520      const bfd_byte *bytes;
521      const bfd_byte *p;
522      unsigned int indx;
523 {
524   debug_type type;
525   const char *name;
526
527   if (indx < BUILTIN_TYPE_COUNT
528       && types->builtins[indx] != DEBUG_TYPE_NULL)
529     return types->builtins[indx];
530
531   if (indx >= 32 && indx < 64)
532     {
533       type = debug_make_pointer_type (dhandle,
534                                       ieee_builtin_type (dhandle, abfd,
535                                                          types, bytes, p,
536                                                          indx - 32));
537       assert (indx < BUILTIN_TYPE_COUNT);
538       types->builtins[indx] = type;
539       return type;
540     }
541
542   switch ((enum builtin_types) indx)
543     {
544     default:
545       ieee_error (abfd, bytes, p, "unknown builtin type");
546       return NULL;
547
548     case builtin_unknown:
549       type = debug_make_void_type (dhandle);
550       name = NULL;
551       break;
552
553     case builtin_void:
554       type = debug_make_void_type (dhandle);
555       name = "void";
556       break;
557
558     case builtin_signed_char:
559       type = debug_make_int_type (dhandle, 1, false);
560       name = "signed char";
561       break;
562
563     case builtin_unsigned_char:
564       type = debug_make_int_type (dhandle, 1, true);
565       name = "unsigned char";
566       break;
567
568     case builtin_signed_short_int:
569       type = debug_make_int_type (dhandle, 2, false);
570       name = "signed short int";
571       break;
572
573     case builtin_unsigned_short_int:
574       type = debug_make_int_type (dhandle, 2, true);
575       name = "unsigned short int";
576       break;
577
578     case builtin_signed_long:
579       type = debug_make_int_type (dhandle, 4, false);
580       name = "signed long";
581       break;
582
583     case builtin_unsigned_long:
584       type = debug_make_int_type (dhandle, 4, true);
585       name = "unsigned long";
586       break;
587
588     case builtin_signed_long_long:
589       type = debug_make_int_type (dhandle, 8, false);
590       name = "signed long long";
591       break;
592
593     case builtin_unsigned_long_long:
594       type = debug_make_int_type (dhandle, 8, true);
595       name = "unsigned long long";
596       break;
597
598     case builtin_float:
599       type = debug_make_float_type (dhandle, 4);
600       name = "float";
601       break;
602
603     case builtin_double:
604       type = debug_make_float_type (dhandle, 8);
605       name = "double";
606       break;
607
608     case builtin_long_double:
609       /* FIXME: The size for this type should depend upon the
610          processor.  */
611       type = debug_make_float_type (dhandle, 12);
612       name = "long double";
613       break;
614
615     case builtin_long_long_double:
616       type = debug_make_float_type (dhandle, 16);
617       name = "long long double";
618       break;
619
620     case builtin_quoted_string:
621       type = debug_make_array_type (dhandle,
622                                     ieee_builtin_type (dhandle, abfd, types,
623                                                        bytes, p,
624                                                        ((unsigned int)
625                                                         builtin_char)),
626                                     ieee_builtin_type (dhandle, abfd, types,
627                                                        bytes, p,
628                                                        ((unsigned int)
629                                                         builtin_int)),
630                                     0, -1, true);
631       name = "QUOTED STRING";
632       break;
633
634     case builtin_instruction_address:
635       /* FIXME: This should be a code address.  */
636       type = debug_make_int_type (dhandle, 4, true);
637       name = "instruction address";
638       break;
639
640     case builtin_int:
641       /* FIXME: The size for this type should depend upon the
642          processor.  */
643       type = debug_make_int_type (dhandle, 4, false);
644       name = "int";
645       break;
646
647     case builtin_unsigned:
648       /* FIXME: The size for this type should depend upon the
649          processor.  */
650       type = debug_make_int_type (dhandle, 4, true);
651       name = "unsigned";
652       break;
653
654     case builtin_unsigned_int:
655       /* FIXME: The size for this type should depend upon the
656          processor.  */
657       type = debug_make_int_type (dhandle, 4, true);
658       name = "unsigned int";
659       break;
660
661     case builtin_char:
662       type = debug_make_int_type (dhandle, 1, false);
663       name = "char";
664       break;
665
666     case builtin_long:
667       type = debug_make_int_type (dhandle, 4, false);
668       name = "long";
669       break;
670
671     case builtin_short:
672       type = debug_make_int_type (dhandle, 2, false);
673       name = "short";
674       break;
675
676     case builtin_unsigned_short:
677       type = debug_make_int_type (dhandle, 2, true);
678       name = "unsigned short";
679       break;
680
681     case builtin_short_int:
682       type = debug_make_int_type (dhandle, 2, false);
683       name = "short int";
684       break;
685
686     case builtin_signed_short:
687       type = debug_make_int_type (dhandle, 2, false);
688       name = "signed short";
689       break;
690
691     case builtin_bcd_float:
692       ieee_error (abfd, bytes, p, "BCD float type not supported");
693       return false;
694     }
695
696   if (name != NULL)
697     type = debug_name_type (dhandle, name, type);
698
699   assert (indx < BUILTIN_TYPE_COUNT);
700
701   types->builtins[indx] = type;
702
703   return type;
704 }
705
706 /* Allocate more space in the type table.  If ref is true, this is a
707    reference to the type; if it is not already defined, we should set
708    up an indirect type.  */
709
710 static boolean
711 ieee_alloc_type (dhandle, types, indx, ref)
712      PTR dhandle;
713      struct ieee_types *types;
714      unsigned int indx;
715      boolean ref;
716 {
717   unsigned int nalloc;
718   register struct ieee_type *t;
719   struct ieee_type *tend;
720
721   if (indx >= types->alloc)
722     {
723       nalloc = types->alloc;
724       if (nalloc == 0)
725         nalloc = 4;
726       while (indx >= nalloc)
727         nalloc *= 2;
728
729       types->types = ((struct ieee_type *)
730                       xrealloc (types->types, nalloc * sizeof *types->types));
731
732       memset (types->types + types->alloc, 0,
733               (nalloc - types->alloc) * sizeof *types->types);
734
735       tend = types->types + nalloc;
736       for (t = types->types + types->alloc; t < tend; t++)
737         {
738           t->type = DEBUG_TYPE_NULL;
739           t->return_type = DEBUG_TYPE_NULL;
740         }
741
742       types->alloc = nalloc;
743     }
744
745   if (ref)
746     {
747       t = types->types + indx;
748       if (t->type == NULL)
749         {
750           t->pslot = (debug_type *) xmalloc (sizeof *t->pslot);
751           *t->pslot = DEBUG_TYPE_NULL;
752           t->type = debug_make_indirect_type (dhandle, t->pslot,
753                                               (const char *) NULL);
754           if (t->type == NULL)
755             return false;
756         }
757     }
758
759   return true;
760 }
761
762 /* Read a type index and return the corresponding type.  */
763
764 static boolean
765 ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend, ptype)
766      PTR dhandle;
767      bfd *abfd;
768      struct ieee_types *types;
769      const bfd_byte *bytes;
770      const bfd_byte **pp;
771      const bfd_byte *pend;
772      debug_type *ptype;
773 {
774   const bfd_byte *start;
775   bfd_vma indx;
776
777   start = *pp;
778
779   if (! ieee_read_number (abfd, bytes, pp, pend, &indx))
780     return false;
781
782   if (indx < 256)
783     {
784       *ptype = ieee_builtin_type (dhandle, abfd, types, bytes, start, indx);
785       if (*ptype == NULL)
786         return false;
787       return true;
788     }
789
790   indx -= 256;
791   if (! ieee_alloc_type (dhandle, types, indx, true))
792     return false;
793
794   *ptype = types->types[indx].type;
795
796   return true;
797 }
798
799 /* Parse IEEE debugging information for a file.  This is passed the
800    bytes which compose the Debug Information Part of an IEEE file.  */
801
802 boolean
803 parse_ieee (dhandle, abfd, bytes, len)
804      PTR dhandle;
805      bfd *abfd;
806      const bfd_byte *bytes;
807      bfd_size_type len;
808 {
809   struct ieee_blockstack blockstack;
810   struct ieee_vars vars;
811   struct ieee_types types;
812   unsigned int i;
813   const bfd_byte *p, *pend;
814
815   blockstack.bsp = blockstack.stack;
816   vars.alloc = 0;
817   vars.vars = NULL;
818   types.alloc = 0;
819   types.types = NULL;
820   for (i = 0; i < BUILTIN_TYPE_COUNT; i++)
821     types.builtins[i] = DEBUG_TYPE_NULL;
822
823   p = bytes;
824   pend = bytes + len;
825   while (p < pend)
826     {
827       const bfd_byte *record_start;
828       ieee_record_enum_type c;
829
830       record_start = p;
831
832       c = (ieee_record_enum_type) *p++;
833
834       if (c == ieee_at_record_enum)
835         c = (ieee_record_enum_type) (((unsigned int) c << 8) | *p++);
836
837       if (c <= ieee_number_repeat_end_enum)
838         {
839           ieee_error (abfd, bytes, record_start, "unexpected number");
840           return false;
841         }
842
843       switch (c)
844         {
845         default:
846           ieee_error (abfd, bytes, record_start, "unexpected record type");
847           return false;
848
849         case ieee_bb_record_enum:
850           if (! parse_ieee_bb (dhandle, abfd, &types, &blockstack, bytes,
851                                &p, pend))
852             return false;
853           break;
854
855         case ieee_be_record_enum:
856           if (! parse_ieee_be (dhandle, abfd, &blockstack, bytes, &p, pend))
857             return false;
858           break;
859
860         case ieee_nn_record:
861           if (! parse_ieee_nn (dhandle, abfd, &vars, bytes, &p, pend))
862             return false;
863           break;
864
865         case ieee_ty_record_enum:
866           if (! parse_ieee_ty (dhandle, abfd, &types, &vars, bytes, &p, pend))
867             return false;
868           break;
869
870         case ieee_atn_record_enum:
871           if (! parse_ieee_atn (dhandle, abfd, &types, &vars,
872                                 (blockstack.bsp <= blockstack.stack
873                                  ? 0
874                                  : blockstack.bsp[-1].kind),
875                                 bytes, &p, pend))
876             return false;
877           break;
878         }
879     }
880
881   if (blockstack.bsp != blockstack.stack)
882     {
883       ieee_error (abfd, (const bfd_byte *) NULL, (const bfd_byte *) NULL,
884                   "blocks left on stack at end");
885       return false;
886     }
887
888   return true;
889 }
890
891 /* Handle an IEEE BB record.  */
892
893 static boolean
894 parse_ieee_bb (dhandle, abfd, types, blockstack, bytes, pp, pend)
895      PTR dhandle;
896      bfd *abfd;
897      struct ieee_types *types;
898      struct ieee_blockstack *blockstack;
899      const bfd_byte *bytes;
900      const bfd_byte **pp;
901      const bfd_byte *pend;
902 {
903   const bfd_byte *block_start;
904   bfd_byte b;
905   bfd_vma size;
906   const char *name;
907   unsigned long namlen;
908   char *namcopy;
909             
910   block_start = *pp;
911
912   b = **pp;
913   ++*pp;
914
915   if (! ieee_read_number (abfd, bytes, pp, pend, &size)
916       || ! ieee_read_id (abfd, bytes, pp, pend, &name, &namlen))
917     return false;
918
919   switch (b)
920     {
921     case 1:
922       /* BB1: Type definitions local to a module.  */
923       namcopy = savestring (name, namlen);
924       if (namcopy == NULL)
925         return false;
926       if (! debug_set_filename (dhandle, namcopy))
927         return false;
928       break;
929
930     case 2:
931       /* BB2: Global type definitions.  The name is supposed to be
932          empty, but we don't check. */
933       if (! debug_set_filename (dhandle, "*global*"))
934         return false;
935       break;
936
937     case 3:
938       /* BB3: High level module block begin.  We don't have to do
939          anything here.  The name is supposed to be the same as for
940          the BB1, but we don't check.  */
941       break;
942
943     case 4:
944       /* BB4: Global function.  */
945       {
946         bfd_vma stackspace, typindx, offset;
947         debug_type return_type;
948
949         if (! ieee_read_number (abfd, bytes, pp, pend, &stackspace)
950             || ! ieee_read_number (abfd, bytes, pp, pend, &typindx)
951             || ! ieee_read_expression (abfd, bytes, pp, pend, &offset))
952           return false;
953
954         /* We have no way to record the stack space.  FIXME.  */
955
956         if (typindx < 256)
957           {
958             return_type = ieee_builtin_type (dhandle, abfd, types, bytes,
959                                              block_start, typindx);
960             if (return_type == NULL)
961               return false;
962           }
963         else
964           {
965             typindx -= 256;
966             if (! ieee_alloc_type (dhandle, types, typindx, true))
967               return false;
968             return_type = types->types[typindx].return_type;
969             if (return_type == NULL)
970               return_type = types->types[typindx].type;
971           }
972
973         namcopy = savestring (name, namlen);
974         if (namcopy == NULL)
975           return false;
976         if (! debug_record_function (dhandle, namcopy, return_type,
977                                      true, offset))
978           return false;
979       }
980       break;
981
982     case 5:
983       /* BB5: File name for source line numbers.  */
984       {
985         unsigned int i;
986
987         /* We ignore the date and time.  FIXME.  */
988         for (i = 0; i < 6; i++)
989           {
990             bfd_vma ignore;
991             boolean present;
992
993             if (! ieee_read_optional_number (abfd, bytes, pp, pend, &ignore,
994                                              &present))
995               return false;
996             if (! present)
997               break;
998           }
999
1000         namcopy = savestring (name, namlen);
1001         if (namcopy == NULL)
1002           return false;
1003         if (! debug_start_source (dhandle, namcopy))
1004           return false;
1005       }
1006       break;
1007
1008     case 6:
1009       /* BB6: Local function or block.  */
1010       {
1011         bfd_vma stackspace, typindx, offset;
1012
1013         if (! ieee_read_number (abfd, bytes, pp, pend, &stackspace)
1014             || ! ieee_read_number (abfd, bytes, pp, pend, &typindx)
1015             || ! ieee_read_expression (abfd, bytes, pp, pend, &offset))
1016           return false;
1017
1018         /* We have no way to record the stack space.  FIXME.  */
1019
1020         if (namlen == 0)
1021           {
1022             if (! debug_start_block (dhandle, offset))
1023               return false;
1024             /* Change b to indicate that this is a block
1025                rather than a function.  */
1026             b = 0x86;
1027           }
1028         else
1029           {
1030             debug_type return_type;
1031
1032             if (typindx < 256)
1033               {
1034                 return_type = ieee_builtin_type (dhandle, abfd, types, bytes,
1035                                                  block_start, typindx);
1036                 if (return_type == NULL)
1037                   return false;
1038               }
1039             else
1040               {
1041                 typindx -= 256;
1042                 if (! ieee_alloc_type (dhandle, types, typindx, true))
1043                       return false;
1044                 return_type = types->types[typindx].return_type;
1045                 if (return_type == NULL)
1046                   return_type = types->types[typindx].type;
1047               }
1048
1049             namcopy = savestring (name, namlen);
1050             if (namcopy == NULL)
1051               return false;
1052             if (! debug_record_function (dhandle, namcopy, return_type,
1053                                          false, offset))
1054               return false;
1055           }
1056       }
1057       break;
1058
1059     case 10:
1060       /* BB10: Assembler module scope.  We completely ignore all this
1061          information.  FIXME.  */
1062       {
1063         const char *inam, *vstr;
1064         unsigned long inamlen, vstrlen;
1065         bfd_vma tool_type;
1066         boolean present;
1067         unsigned int i;
1068
1069         if (! ieee_read_id (abfd, bytes, pp, pend, &inam, &inamlen)
1070             || ! ieee_read_number (abfd, bytes, pp, pend, &tool_type)
1071             || ! ieee_read_optional_id (abfd, bytes, pp, pend, &vstr, &vstrlen,
1072                                         &present))
1073           return false;
1074         for (i = 0; i < 6; i++)
1075           {
1076             bfd_vma ignore;
1077
1078             if (! ieee_read_optional_number (abfd, bytes, pp, pend, &ignore,
1079                                              &present))
1080               return false;
1081             if (! present)
1082               break;
1083           }
1084       }
1085       break;
1086
1087     case 11:
1088       /* BB11: Module section.  We completely ignore all this
1089          information.  FIXME.  */
1090       {
1091         bfd_vma sectype, secindx, offset, map;
1092         boolean present;
1093
1094         if (! ieee_read_number (abfd, bytes, pp, pend, &sectype)
1095             || ! ieee_read_number (abfd, bytes, pp, pend, &secindx)
1096             || ! ieee_read_expression (abfd, bytes, pp, pend, &offset)
1097             || ! ieee_read_optional_number (abfd, bytes, pp, pend, &map,
1098                                             &present))
1099           return false;
1100       }
1101       break;
1102
1103     default:
1104       ieee_error (abfd, bytes, block_start, "unknown BB type");
1105       return false;
1106     }
1107
1108
1109   /* Push this block on the block stack.  */
1110
1111   if (blockstack->bsp >= blockstack->stack + BLOCKSTACK_SIZE)
1112     {
1113       ieee_error (abfd, (const bfd_byte *) NULL, (const bfd_byte *) NULL,
1114                   "stack overflow");
1115       return false;
1116     }
1117
1118   blockstack->bsp->kind = b;
1119   if (b == 5)
1120     blockstack->bsp->filename = namcopy;
1121   ++blockstack->bsp;
1122
1123   return true;
1124 }
1125
1126 /* Handle an IEEE BE record.  */
1127
1128 static boolean
1129 parse_ieee_be (dhandle, abfd, blockstack, bytes, pp, pend)
1130      PTR dhandle;
1131      bfd *abfd;
1132      struct ieee_blockstack *blockstack;
1133      const bfd_byte *bytes;
1134      const bfd_byte **pp;
1135      const bfd_byte *pend;
1136 {
1137   bfd_vma offset;
1138
1139   if (blockstack->bsp <= blockstack->stack)
1140     {
1141       ieee_error (abfd, bytes, *pp, "stack underflow");
1142       return false;
1143     }
1144   --blockstack->bsp;
1145
1146   switch (blockstack->bsp->kind)
1147     {
1148     case 4:
1149     case 6:
1150       if (! ieee_read_expression (abfd, bytes, pp, pend, &offset))
1151         return false;
1152       if (! debug_end_function (dhandle, offset))
1153         return false;
1154       break;
1155
1156     case 0x86:
1157       /* This is BE6 when BB6 started a block rather than a local
1158          function.  */
1159       if (! ieee_read_expression (abfd, bytes, pp, pend, &offset))
1160         return false;
1161       if (! debug_end_block (dhandle, offset))
1162         return false;
1163       break;
1164
1165     case 5:
1166       /* When we end a BB5, we look up the stack for the last BB5, if
1167          there is one, so that we can call debug_start_source.  */
1168       if (blockstack->bsp > blockstack->stack)
1169         {
1170           struct ieee_block *bl;
1171
1172           bl = blockstack->bsp;
1173           do
1174             {
1175               --bl;
1176               if (bl->kind == 5)
1177                 {
1178                   if (! debug_start_source (dhandle, bl->filename))
1179                     return false;
1180                   break;
1181                 }
1182             }
1183           while (bl != blockstack->stack);
1184         }
1185       break;
1186
1187     case 11:
1188       if (! ieee_read_expression (abfd, bytes, pp, pend, &offset))
1189         return false;
1190       /* We just ignore the module size.  FIXME.  */
1191       break;
1192
1193     default:
1194       /* Other block types do not have any trailing information.  */
1195       break;
1196     }
1197
1198   return true;
1199 }
1200
1201 /* Parse an NN record.  */
1202
1203 static boolean
1204 parse_ieee_nn (dhandle, abfd, vars, bytes, pp, pend)
1205      PTR dhandle;
1206      bfd *abfd;
1207      struct ieee_vars *vars;
1208      const bfd_byte *bytes;
1209      const bfd_byte **pp;
1210      const bfd_byte *pend;
1211 {
1212   const bfd_byte *nn_start;
1213   bfd_vma varindx;
1214   const char *name;
1215   unsigned long namlen;
1216
1217   nn_start = *pp;
1218
1219   if (! ieee_read_number (abfd, bytes, pp, pend, &varindx)
1220       || ! ieee_read_id (abfd, bytes, pp, pend, &name, &namlen))
1221     return false;
1222
1223   if (varindx < 32)
1224     {
1225       ieee_error (abfd, bytes, nn_start, "illegal variable index");
1226       return false;
1227     }
1228   varindx -= 32;
1229
1230   if (varindx >= vars->alloc)
1231     {
1232       unsigned int alloc;
1233
1234       alloc = vars->alloc;
1235       if (alloc == 0)
1236         alloc = 4;
1237       while (varindx >= alloc)
1238         alloc *= 2;
1239       vars->vars = ((struct ieee_var *)
1240                     xrealloc (vars->vars, alloc * sizeof *vars->vars));
1241       memset (vars->vars + vars->alloc, 0,
1242               (alloc - vars->alloc) * sizeof *vars->vars);
1243       vars->alloc = alloc;
1244     }
1245
1246   vars->vars[varindx].name = name;
1247   vars->vars[varindx].namlen = namlen;
1248
1249   return true;
1250 }
1251
1252 /* Parse a TY record.  */
1253
1254 static boolean
1255 parse_ieee_ty (dhandle, abfd, types, vars, bytes, pp, pend)
1256      PTR dhandle;
1257      bfd *abfd;
1258      struct ieee_types *types;
1259      struct ieee_vars *vars;
1260      const bfd_byte *bytes;
1261      const bfd_byte **pp;
1262      const bfd_byte *pend;
1263 {
1264   const bfd_byte *ty_start, *ty_var_start, *ty_code_start;
1265   bfd_vma typeindx, varindx, tc;
1266   debug_type type;
1267   boolean tag, typdef;
1268   unsigned long type_bitsize;
1269   debug_type return_type;
1270
1271   ty_start = *pp;
1272
1273   if (! ieee_read_number (abfd, bytes, pp, pend, &typeindx))
1274     return false;
1275
1276   if (typeindx < 256)
1277     {
1278       ieee_error (abfd, bytes, ty_start, "illegal type index");
1279       return false;
1280     }
1281
1282   typeindx -= 256;
1283   if (! ieee_alloc_type (dhandle, types, typeindx, false))
1284     return false;
1285
1286   if (**pp != 0xce)
1287     {
1288       ieee_error (abfd, bytes, *pp, "unknown TY code");
1289       return false;
1290     }
1291   ++*pp;
1292
1293   ty_var_start = *pp;
1294
1295   if (! ieee_read_number (abfd, bytes, pp, pend, &varindx))
1296     return false;
1297
1298   if (varindx < 32)
1299     {
1300       ieee_error (abfd, bytes, ty_var_start, "illegal variable index");
1301       return false;
1302     }
1303   varindx -= 32;
1304
1305   if (varindx >= vars->alloc || vars->vars[varindx].name == NULL)
1306     {
1307       ieee_error (abfd, bytes, ty_var_start, "undefined variable in TY");
1308       return false;
1309     }
1310
1311   ty_code_start = *pp;
1312
1313   if (! ieee_read_number (abfd, bytes, pp, pend, &tc))
1314     return false;
1315
1316   tag = false;
1317   typdef = false;
1318   type_bitsize = 0;
1319   return_type = DEBUG_TYPE_NULL;
1320   switch (tc)
1321     {
1322     default:
1323       ieee_error (abfd, bytes, ty_code_start, "unknown TY code");
1324       return false;
1325
1326     case '!':
1327       /* Unknown type, with size.  We treat it as int.  FIXME.  */
1328       {
1329         bfd_vma size;
1330
1331         if (! ieee_read_number (abfd, bytes, pp, pend, &size))
1332           return false;
1333         type = debug_make_int_type (dhandle, size, false);
1334       }
1335       break;
1336
1337     case 'A': /* Array.  */
1338     case 'a': /* FORTRAN array in column/row order.  FIXME: Not
1339                  distinguished from normal array.  */
1340       {
1341         debug_type ele_type;
1342         bfd_vma lower, upper;
1343
1344         if (! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1345                                     &ele_type)
1346             || ! ieee_read_number (abfd, bytes, pp, pend, &lower)
1347             || ! ieee_read_number (abfd, bytes, pp, pend, &upper))
1348           return false;
1349         type = debug_make_array_type (dhandle, ele_type,
1350                                       ieee_builtin_type (dhandle, abfd, types,
1351                                                          bytes, ty_code_start,
1352                                                          ((unsigned int)
1353                                                           builtin_int)),
1354                                       (bfd_signed_vma) lower,
1355                                       (bfd_signed_vma) upper,
1356                                       false);
1357       }
1358       break;
1359
1360     case 'E':
1361       /* Simple enumeration.  */
1362       {
1363         bfd_vma size;
1364         unsigned int alloc;
1365         const char **names;
1366         unsigned int c;
1367         bfd_signed_vma *vals;
1368         unsigned int i;
1369
1370         if (! ieee_read_number (abfd, bytes, pp, pend, &size))
1371           return false;
1372         /* FIXME: we ignore the enumeration size.  */
1373
1374         alloc = 10;
1375         names = (const char **) xmalloc (alloc * sizeof *names);
1376         memset (names, 0, alloc * sizeof *names);
1377         c = 0;
1378         while (1)
1379           {
1380             const char *name;
1381             unsigned long namlen;
1382             boolean present;
1383
1384             if (! ieee_read_optional_id (abfd, bytes, pp, pend, &name,
1385                                          &namlen, &present))
1386               return false;
1387             if (! present)
1388               break;
1389
1390             if (c + 1 >= alloc)
1391               {
1392                 alloc += 10;
1393                 names = ((const char **)
1394                          xrealloc (names, alloc * sizeof *names));
1395               }
1396
1397             names[c] = savestring (name, namlen);
1398             if (names[c] == NULL)
1399               return false;
1400             ++c;
1401           }
1402
1403         names[c] = NULL;
1404
1405         vals = (bfd_signed_vma *) xmalloc (c * sizeof *vals);
1406         for (i = 0; i < c; i++)
1407           vals[i] = i;
1408
1409         type = debug_make_enum_type (dhandle, names, vals);
1410         tag = true;
1411       }
1412       break;
1413
1414     case 'G':
1415       /* Struct with bit fields.  */
1416       {
1417         bfd_vma size;
1418         unsigned int alloc;
1419         debug_field *fields;
1420         unsigned int c;
1421
1422         if (! ieee_read_number (abfd, bytes, pp, pend, &size))
1423           return false;
1424
1425         alloc = 10;
1426         fields = (debug_field *) xmalloc (alloc * sizeof *fields);
1427         c = 0;
1428         while (1)
1429           {
1430             const char *name;
1431             unsigned long namlen;
1432             boolean present;
1433             debug_type ftype;
1434             bfd_vma bitpos, bitsize;
1435
1436             if (! ieee_read_optional_id (abfd, bytes, pp, pend, &name,
1437                                          &namlen, &present))
1438               return false;
1439             if (! present)
1440               break;
1441             if (! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1442                                         &ftype)
1443                 || ! ieee_read_number (abfd, bytes, pp, pend, &bitpos)
1444                 || ! ieee_read_number (abfd, bytes, pp, pend, &bitsize))
1445               return false;
1446
1447             if (c + 1 >= alloc)
1448               {
1449                 alloc += 10;
1450                 fields = ((debug_field *)
1451                           xrealloc (fields, alloc * sizeof *fields));
1452               }
1453
1454             fields[c] = debug_make_field (dhandle, savestring (name, namlen),
1455                                           ftype, bitpos, bitsize,
1456                                           DEBUG_VISIBILITY_PUBLIC);
1457             if (fields[c] == NULL)
1458               return false;
1459             ++c;
1460           }
1461
1462         fields[c] = NULL;
1463
1464         type = debug_make_struct_type (dhandle, true, size, fields);
1465         tag = true;
1466       }
1467       break;
1468
1469     case 'N':
1470       /* Enumeration.  */
1471       {
1472         unsigned int alloc;
1473         const char **names;
1474         bfd_signed_vma *vals;
1475         unsigned int c;
1476
1477         alloc = 10;
1478         names = (const char **) xmalloc (alloc * sizeof *names);
1479         vals = (bfd_signed_vma *) xmalloc (alloc * sizeof *names);
1480         c = 0;
1481         while (1)
1482           {
1483             const char *name;
1484             unsigned long namlen;
1485             boolean present;
1486             bfd_vma val;
1487
1488             if (! ieee_read_optional_id (abfd, bytes, pp, pend, &name,
1489                                          &namlen, &present))
1490               return false;
1491             if (! present)
1492               break;
1493             if (! ieee_read_number (abfd, bytes, pp, pend, &val))
1494               return false;
1495
1496             /* If the length of the name is zero, then the value is
1497                actually the size of the enum.  We ignore this
1498                information.  FIXME.  */
1499             if (namlen == 0)
1500               continue;
1501
1502             if (c + 1 >= alloc)
1503               {
1504                 alloc += 10;
1505                 names = ((const char **)
1506                          xrealloc (names, alloc * sizeof *names));
1507                 vals = ((bfd_signed_vma *)
1508                         xrealloc (vals, alloc * sizeof *vals));
1509               }
1510
1511             names[c] = savestring (name, namlen);
1512             if (names[c] == NULL)
1513               return false;
1514             vals[c] = (bfd_signed_vma) val;
1515             ++c;
1516           }
1517
1518         names[c] = NULL;
1519
1520         type = debug_make_enum_type (dhandle, names, vals);
1521         tag = true;
1522       }
1523       break;
1524
1525     case 'O': /* Small pointer.  We don't distinguish small and large
1526                  pointers.  FIXME.  */
1527     case 'P': /* Large pointer.  */
1528       {
1529         debug_type t;
1530
1531         if (! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend, &t))
1532           return false;
1533         type = debug_make_pointer_type (dhandle, t);
1534       }
1535       break;
1536
1537     case 'R':
1538       /* Range.  */
1539       {
1540         bfd_vma low, high, signedp, size;
1541
1542         if (! ieee_read_number (abfd, bytes, pp, pend, &low)
1543             || ! ieee_read_number (abfd, bytes, pp, pend, &high)
1544             || ! ieee_read_number (abfd, bytes, pp, pend, &signedp)
1545             || ! ieee_read_number (abfd, bytes, pp, pend, &size))
1546           return false;
1547
1548         type = debug_make_range_type (dhandle,
1549                                       debug_make_int_type (dhandle, size,
1550                                                            ! signedp),
1551                                       (bfd_signed_vma) low,
1552                                       (bfd_signed_vma) high);
1553       }
1554       break;
1555
1556     case 'S': /* Struct.  */
1557     case 'U': /* Union.  */
1558       {
1559         bfd_vma size;
1560         unsigned int alloc;
1561         debug_field *fields;
1562         unsigned int c;
1563
1564         if (! ieee_read_number (abfd, bytes, pp, pend, &size))
1565           return false;
1566
1567         alloc = 10;
1568         fields = (debug_field *) xmalloc (alloc * sizeof *fields);
1569         c = 0;
1570         while (1)
1571           {
1572             const char *name;
1573             unsigned long namlen;
1574             boolean present;
1575             bfd_vma tindx;
1576             bfd_vma offset;
1577             debug_type ftype;
1578             bfd_vma bitsize;
1579
1580             if (! ieee_read_optional_id (abfd, bytes, pp, pend, &name,
1581                                          &namlen, &present))
1582               return false;
1583             if (! present)
1584               break;
1585             if (! ieee_read_number (abfd, bytes, pp, pend, &tindx)
1586                 || ! ieee_read_number (abfd, bytes, pp, pend, &offset))
1587               return false;
1588
1589             if (tindx < 256)
1590               {
1591                 ftype = ieee_builtin_type (dhandle, abfd, types, bytes,
1592                                            ty_code_start, tindx);
1593                 bitsize = 0;
1594                 offset *= 8;
1595               }
1596             else
1597               {
1598                 struct ieee_type *t;
1599
1600                 tindx -= 256;
1601                 if (! ieee_alloc_type (dhandle, types, tindx, true))
1602                   return false;
1603                 t = types->types + tindx;
1604                 ftype = t->type;
1605                 bitsize = t->bitsize;
1606                 if (bitsize == 0)
1607                   offset *= 8;
1608               }
1609
1610             if (c + 1 >= alloc)
1611               {
1612                 alloc += 10;
1613                 fields = ((debug_field *)
1614                           xrealloc (fields, alloc * sizeof *fields));
1615               }
1616
1617             fields[c] = debug_make_field (dhandle, savestring (name, namlen),
1618                                           ftype, offset, bitsize,
1619                                           DEBUG_VISIBILITY_PUBLIC);
1620             if (fields[c] == NULL)
1621               return false;
1622             ++c;
1623           }
1624
1625         fields[c] = NULL;
1626
1627         type = debug_make_struct_type (dhandle, tc == 'S', size, fields);
1628         tag = true;
1629       }
1630       break;
1631
1632     case 'T':
1633       /* Typedef.  */
1634       if (! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1635                                   &type))
1636         return false;
1637       typdef = true;
1638       break;
1639
1640     case 'X':
1641       /* Procedure.  FIXME: This is an extern declaration, which we
1642          have no way of representing.  */
1643       {
1644         bfd_vma attr;
1645         debug_type rtype;
1646         bfd_vma nargs;
1647         boolean present;
1648
1649         /* FIXME: We ignore the attribute and the argument names.  */
1650
1651         if (! ieee_read_number (abfd, bytes, pp, pend, &attr)
1652             || ! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1653                                        &rtype)
1654             || ! ieee_read_number (abfd, bytes, pp, pend, &nargs))
1655           return false;
1656         do
1657           {
1658             const char *name;
1659             unsigned long namlen;
1660
1661             if (! ieee_read_optional_id (abfd, bytes, pp, pend, &name,
1662                                          &namlen, &present))
1663               return false;
1664           }
1665         while (present);
1666
1667         type = debug_make_function_type (dhandle, rtype, (debug_type *) NULL,
1668                                          false);
1669         return_type = rtype;
1670       }
1671       break;
1672
1673     case 'Z':
1674       /* Array with 0 lower bound.  */
1675       {
1676         debug_type etype;
1677         bfd_vma high;
1678
1679         if (! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1680                                     &etype)
1681             || ! ieee_read_number (abfd, bytes, pp, pend, &high))
1682           return false;
1683
1684         type = debug_make_array_type (dhandle, etype,
1685                                       ieee_builtin_type (dhandle, abfd, types,
1686                                                          bytes, ty_code_start,
1687                                                          ((unsigned int)
1688                                                           builtin_int)),
1689                                       0, (bfd_signed_vma) high, false);
1690       }
1691       break;
1692
1693     case 'c': /* Complex.  */
1694     case 'd': /* Double complex.  */
1695       {
1696         const char *name;
1697         unsigned long namlen;
1698
1699         /* FIXME: I don't know what the name means.  */
1700
1701         if (! ieee_read_id (abfd, bytes, pp, pend, &name, &namlen))
1702           return false;
1703
1704         type = debug_make_complex_type (dhandle, tc == 'c' ? 4 : 8);
1705       }
1706       break;
1707
1708     case 'f':
1709       /* Pascal file name.  FIXME.  */
1710       ieee_error (abfd, bytes, ty_code_start,
1711                   "Pascal file name not supported");
1712       return false;
1713
1714     case 'g':
1715       /* Bitfield type.  */
1716       {
1717         bfd_vma signedp, bitsize;
1718
1719         if (! ieee_read_number (abfd, bytes, pp, pend, &signedp)
1720             || ! ieee_read_number (abfd, bytes, pp, pend, &bitsize)
1721             || ! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1722                                        &type))
1723           return false;
1724
1725         /* FIXME: This is just a guess.  */
1726         if (! signedp)
1727           type = debug_make_int_type (dhandle, 4, true);
1728         type_bitsize = bitsize;
1729       }
1730       break;
1731
1732     case 'n':
1733       /* Qualifier.  */
1734       {
1735         bfd_vma kind;
1736         debug_type t;
1737
1738         if (! ieee_read_number (abfd, bytes, pp, pend, &kind)
1739             || ! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1740                                        &t))
1741           return false;
1742
1743         switch (kind)
1744           {
1745           default:
1746             ieee_error (abfd, bytes, ty_start, "unsupported qualifer");
1747             return false;
1748
1749           case 1:
1750             type = debug_make_const_type (dhandle, t);
1751             break;
1752
1753           case 2:
1754             type = debug_make_volatile_type (dhandle, t);
1755             break;
1756           }
1757       }
1758       break;
1759
1760     case 's':
1761       /* Set.  */
1762       {
1763         bfd_vma size;
1764         debug_type etype;
1765
1766         if (! ieee_read_number (abfd, bytes, pp, pend, &size)
1767             || ! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1768                                        &etype))
1769           return false;
1770
1771         /* FIXME: We ignore the size.  */
1772
1773         type = debug_make_set_type (dhandle, etype, false);
1774       }
1775       break;
1776
1777     case 'x':
1778       /* Procedure with compiler dependencies.  FIXME: This is an
1779          extern declaration, which we have no way of representing.  */
1780       {
1781         bfd_vma attr, frame_type, push_mask, nargs, level, father;
1782         debug_type rtype;
1783         debug_type *arg_types;
1784         boolean varargs;
1785         boolean present;
1786
1787         /* FIXME: We ignore almost all this information.  */
1788
1789         if (! ieee_read_number (abfd, bytes, pp, pend, &attr)
1790             || ! ieee_read_number (abfd, bytes, pp, pend, &frame_type)
1791             || ! ieee_read_number (abfd, bytes, pp, pend, &push_mask)
1792             || ! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend,
1793                                        &rtype)
1794             || ! ieee_read_number (abfd, bytes, pp, pend, &nargs))
1795           return false;
1796         if (nargs == (bfd_vma) -1)
1797           {
1798             arg_types = NULL;
1799             varargs = false;
1800           }
1801         else
1802           {
1803             unsigned int i;
1804
1805             arg_types = ((debug_type *)
1806                          xmalloc ((nargs + 1) * sizeof *arg_types));
1807             for (i = 0; i < nargs; i++)
1808               if (! ieee_read_type_index (dhandle, abfd, types, bytes, pp,
1809                                           pend, arg_types + i))
1810                 return false;
1811
1812             /* If the last type is pointer to void, this is really a
1813                varargs function.  */
1814             varargs = false;
1815             if (nargs > 0)
1816               {
1817                 debug_type last;
1818
1819                 last = arg_types[nargs - 1];
1820                 if (debug_get_type_kind (dhandle, last) == DEBUG_KIND_POINTER
1821                     && (debug_get_type_kind (dhandle,
1822                                              debug_get_target_type (dhandle,
1823                                                                     last))
1824                         == DEBUG_KIND_VOID))
1825                   {
1826                     --nargs;
1827                     varargs = true;
1828                   }
1829               }
1830
1831             arg_types[nargs] = DEBUG_TYPE_NULL;
1832           }
1833         if (! ieee_read_number (abfd, bytes, pp, pend, &level)
1834             || ! ieee_read_optional_number (abfd, bytes, pp, pend, &father,
1835                                             &present))
1836           return false;
1837
1838         type = debug_make_function_type (dhandle, rtype, arg_types, varargs);
1839         return_type = rtype;
1840       }
1841       break;
1842     }
1843
1844   /* Record the type in the table.  If the corresponding NN record has
1845      a name, name it.  FIXME: Is this always correct?  */
1846
1847   if (type == NULL)
1848     return false;
1849
1850   if ((tag || typdef)
1851       && vars->vars[varindx].namlen > 0)
1852     {
1853       const char *name;
1854
1855       name = savestring (vars->vars[varindx].name,
1856                          vars->vars[varindx].namlen);
1857       if (tag)
1858         type = debug_tag_type (dhandle, name, type);
1859       else
1860         type = debug_name_type (dhandle, name, type);
1861       if (type == NULL)
1862         return false;
1863     }
1864
1865   types->types[typeindx].type = type;
1866   types->types[typeindx].bitsize = type_bitsize;
1867   types->types[typeindx].return_type = return_type;
1868
1869   /* We may have already allocated type as an indirect type pointing
1870      to slot.  It does no harm to replace the indirect type with the
1871      real type.  Filling in slot as well handles the indirect types
1872      which are already hanging around.  */
1873   if (types->types[typeindx].pslot != NULL)
1874     *types->types[typeindx].pslot = type;
1875
1876   return true;
1877 }
1878
1879 /* Parse an ATN record.  */
1880
1881 static boolean
1882 parse_ieee_atn (dhandle, abfd, types, vars, blocktype, bytes, pp, pend)
1883      PTR dhandle;
1884      bfd *abfd;
1885      struct ieee_types *types;
1886      struct ieee_vars *vars;
1887      int blocktype;
1888      const bfd_byte *bytes;
1889      const bfd_byte **pp;
1890      const bfd_byte *pend;
1891 {
1892   const bfd_byte *atn_start, *atn_code_start;
1893   bfd_vma varindx;
1894   boolean zeroindx;
1895   debug_type type;
1896   bfd_vma atn_code;
1897   bfd_vma v, v2, v3, v4, v5;
1898   const char *name;
1899   unsigned long namlen;
1900   char *namcopy;
1901   boolean present;
1902
1903   atn_start = *pp;
1904
1905   if (! ieee_read_number (abfd, bytes, pp, pend, &varindx)
1906       || ! ieee_read_type_index (dhandle, abfd, types, bytes, pp, pend, &type))
1907     return false;
1908
1909   atn_code_start = *pp;
1910
1911   if (! ieee_read_number (abfd, bytes, pp, pend, &atn_code))
1912     return false;
1913
1914   if (varindx == 0)
1915     {
1916       zeroindx = true;
1917       name = "";
1918       namlen = 0;
1919     }
1920   else if (varindx < 32)
1921     {
1922       ieee_error (abfd, bytes, atn_start, "illegal variable index");
1923       return false;
1924     }
1925   else
1926     {
1927       varindx -= 32;
1928       zeroindx = false;
1929       if (varindx >= vars->alloc || vars->vars[varindx].name == NULL)
1930         {
1931           ieee_error (abfd, bytes, atn_start, "undefined variable in ATN");
1932           return false;
1933         }
1934
1935       vars->vars[varindx].type = type;
1936
1937       name = vars->vars[varindx].name;
1938       namlen = vars->vars[varindx].namlen;
1939     }
1940
1941   switch (atn_code)
1942     {
1943     default:
1944       ieee_error (abfd, bytes, atn_code_start, "unknown ATN type");
1945       return false;
1946
1947     case 1:
1948       /* Automatic variable.  */
1949       if (! ieee_read_number (abfd, bytes, pp, pend, &v))
1950         return false;
1951       namcopy = savestring (name, namlen);
1952       if (type == NULL)
1953         type = debug_make_void_type (dhandle);
1954       return debug_record_variable (dhandle, namcopy, type, DEBUG_LOCAL, v);
1955
1956     case 2:
1957       /* Register variable.  */
1958       if (! ieee_read_number (abfd, bytes, pp, pend, &v))
1959         return false;
1960       namcopy = savestring (name, namlen);
1961       if (type == NULL)
1962         type = debug_make_void_type (dhandle);
1963       return debug_record_variable (dhandle, namcopy, type, DEBUG_REGISTER,
1964                                     ieee_regno_to_genreg (abfd, v));
1965
1966     case 3:
1967       /* Static variable.  */
1968       if (! ieee_require_asn (abfd, bytes, pp, pend, &v))
1969         return false;
1970       namcopy = savestring (name, namlen);
1971       if (type == NULL)
1972         type = debug_make_void_type (dhandle);
1973       return debug_record_variable (dhandle, namcopy, type,
1974                                     (blocktype == 4 || blocktype == 6
1975                                      ? DEBUG_LOCAL_STATIC
1976                                      : DEBUG_STATIC),
1977                                     v);
1978
1979     case 4:
1980       /* External function.  We don't currently record these.  FIXME.  */
1981       return true;
1982
1983     case 5:
1984       /* External variable.  We don't currently record these.  FIXME.  */
1985       return true;
1986
1987     case 7:
1988       if (! ieee_read_number (abfd, bytes, pp, pend, &v)
1989           || ! ieee_read_number (abfd, bytes, pp, pend, &v2)
1990           || ! ieee_read_optional_number (abfd, bytes, pp, pend, &v3,
1991                                           &present))
1992         return false;
1993       if (present)
1994         {
1995           if (! ieee_read_optional_number (abfd, bytes, pp, pend, &v4,
1996                                            &present))
1997             return false;
1998         }
1999
2000       /* We just ignore the two optional fields in v3 and v4, since
2001          they are not defined.  */
2002
2003       if (! ieee_require_asn (abfd, bytes, pp, pend, &v3))
2004         return false;
2005
2006       /* We have no way to record the column number.  FIXME.  */
2007
2008       return debug_record_line (dhandle, v, v3);
2009
2010     case 8:
2011       /* Global variable.  */
2012       if (! ieee_require_asn (abfd, bytes, pp, pend, &v))
2013         return false;
2014       namcopy = savestring (name, namlen);
2015       if (type == NULL)
2016         type = debug_make_void_type (dhandle);
2017       return debug_record_variable (dhandle, namcopy, type, DEBUG_GLOBAL, v);
2018
2019     case 9:
2020       /* Variable lifetime information.  */
2021       if (! ieee_read_number (abfd, bytes, pp, pend, &v))
2022         return false;
2023
2024       /* We have no way to record this information.  FIXME.  */
2025       return true;
2026
2027     case 10:
2028       /* Locked register.  */
2029       if (! ieee_read_number (abfd, bytes, pp, pend, &v)
2030           || ! ieee_read_number (abfd, bytes, pp, pend, &v2))
2031         return false;
2032
2033       /* I think this means a variable that is both in a register and
2034          a frame slot.  We ignore the frame slot.  FIXME.  */
2035
2036       namcopy = savestring (name, namlen);
2037       if (type == NULL)
2038         type = debug_make_void_type (dhandle);
2039       return debug_record_variable (dhandle, namcopy, type, DEBUG_REGISTER, v);
2040
2041     case 11:
2042       /* Reserved for FORTRAN common.  */
2043       ieee_error (abfd, bytes, atn_code_start, "unsupported ATN11");
2044
2045       /* Return true to keep going.  */
2046       return true;
2047
2048     case 12:
2049       /* Based variable.  */
2050       v3 = 0;
2051       v4 = 0x80;
2052       v5 = 0;
2053       if (! ieee_read_number (abfd, bytes, pp, pend, &v)
2054           || ! ieee_read_number (abfd, bytes, pp, pend, &v2)
2055           || ! ieee_read_optional_number (abfd, bytes, pp, pend, &v3,
2056                                           &present))
2057         return false;
2058       if (present)
2059         {
2060           if (! ieee_read_optional_number (abfd, bytes, pp, pend, &v4,
2061                                            &present))
2062             return false;
2063           if (present)
2064             {
2065               if (! ieee_read_optional_number (abfd, bytes, pp, pend, &v5,
2066                                                &present))
2067                 return false;
2068             }
2069         }
2070
2071       /* We have no way to record this information.  FIXME.  */
2072
2073       ieee_error (abfd, bytes, atn_code_start, "unsupported ATN12");
2074
2075       /* Return true to keep going.  */
2076       return true;
2077
2078     case 16:
2079       /* Constant.  The description of this that I have is ambiguous,
2080          so I'm not going to try to implement it.  */
2081       ieee_error (abfd, bytes, atn_code_start, "unsupported ATN16");
2082       return false;
2083
2084     case 19:
2085       /* Static variable from assembler.  */
2086       v2 = 0;
2087       if (! ieee_read_number (abfd, bytes, pp, pend, &v)
2088           || ! ieee_read_optional_number (abfd, bytes, pp, pend, &v2,
2089                                           &present)
2090           || ! ieee_require_asn (abfd, bytes, pp, pend, &v3))
2091         return false;
2092       namcopy = savestring (name, namlen);
2093       /* We don't really handle this correctly.  FIXME.  */
2094       return debug_record_variable (dhandle, namcopy,
2095                                     debug_make_void_type (dhandle),
2096                                     v2 != 0 ? DEBUG_GLOBAL : DEBUG_STATIC,
2097                                     v3);
2098
2099     case 62:
2100       /* Procedure miscellaneous information.  */
2101     case 63:
2102       /* Variable miscellaneous information.  */
2103     case 64:
2104       /* Module miscellaneous information.  */
2105       if (! ieee_read_number (abfd, bytes, pp, pend, &v)
2106           || ! ieee_read_number (abfd, bytes, pp, pend, &v2)
2107           || ! ieee_read_optional_id (abfd, bytes, pp, pend, &name, &namlen,
2108                                       &present))
2109         return false;
2110
2111       /* We just ignore all of this stuff.  FIXME.  */
2112
2113       for (; v2 > 0; --v2)
2114         {
2115           ieee_record_enum_type c;
2116           bfd_vma vindx;
2117           const char *str;
2118           unsigned long strlen;
2119
2120           c = (ieee_record_enum_type) **pp;
2121           ++*pp;
2122           if (c != ieee_at_record_enum
2123               && c != ieee_e2_first_byte_enum)
2124             {
2125               ieee_error (abfd, bytes, *pp - 1, "bad misc record");
2126               return false;
2127             }
2128
2129           c = (ieee_record_enum_type) (((unsigned int) c << 8) | **pp);
2130           ++*pp;
2131           switch (c)
2132             {
2133             default:
2134               ieee_error (abfd, bytes, *pp - 2, "bad misc record");
2135               return false;
2136
2137             case ieee_atn_record_enum:
2138               if (! ieee_read_number (abfd, bytes, pp, pend, &vindx))
2139                 return false;
2140               if ((*pp)[0] != 0 || (*pp)[1] != 65)
2141                 {
2142                   ieee_error (abfd, bytes, *pp, "bad atn in misc");
2143                   return false;
2144                 }
2145               *pp += 2;
2146               if (! ieee_read_id (abfd, bytes, pp, pend, &str, &strlen))
2147                 return false;
2148               break;
2149
2150             case ieee_asn_record_enum:
2151               if (! ieee_read_number (abfd, bytes, pp, pend, &vindx)
2152                   || ! ieee_read_expression (abfd, bytes, pp, pend, &v3))
2153                 return false;
2154               break;
2155             }
2156         }
2157
2158       return true;
2159     }
2160
2161   /*NOTREACHED*/
2162 }
2163
2164 /* Require an ASN record.  */
2165
2166 static boolean
2167 ieee_require_asn (abfd, bytes, pp, pend, pv)
2168      bfd *abfd;
2169      const bfd_byte *bytes;
2170      const bfd_byte **pp;
2171      const bfd_byte *pend;
2172      bfd_vma *pv;
2173 {
2174   const bfd_byte *start;
2175   ieee_record_enum_type c;
2176   bfd_vma varindx;
2177
2178   start = *pp;
2179
2180   c = (ieee_record_enum_type) **pp;
2181   if (c != ieee_e2_first_byte_enum)
2182     {
2183       ieee_error (abfd, bytes, start, "missing required ASN");
2184       return false;
2185     }
2186   ++*pp;
2187
2188   c = (ieee_record_enum_type) (((unsigned int) c << 8) | **pp);
2189   if (c != ieee_asn_record_enum)
2190     {
2191       ieee_error (abfd, bytes, start, "missing required ASN");
2192       return false;
2193     }
2194   ++*pp;
2195
2196   /* Just ignore the variable index.  */
2197   if (! ieee_read_number (abfd, bytes, pp, pend, &varindx))
2198     return false;
2199
2200   return ieee_read_expression (abfd, bytes, pp, pend, pv);
2201 }
2202 \f
2203 /* Convert a register number in IEEE debugging information into a
2204    generic register number.  */
2205
2206 static int
2207 ieee_regno_to_genreg (abfd, r)
2208      bfd *abfd;
2209      int r;
2210 {
2211   return r;
2212 }
2213
2214 /* Convert a generic register number to an IEEE specific one.  */
2215
2216 static int
2217 ieee_genreg_to_regno (abfd, r)
2218      bfd *abfd;
2219      int r;
2220 {
2221   return r;
2222 }
2223 \f
2224 /* These routines build IEEE debugging information out of the generic
2225    debugging information.  */
2226
2227 /* We build the IEEE debugging information byte by byte.  Rather than
2228    waste time copying data around, we use a linked list of buffers to
2229    hold the data.  */
2230
2231 #define IEEE_BUFSIZE (490)
2232
2233 struct ieee_buf
2234 {
2235   /* Next buffer.  */
2236   struct ieee_buf *next;
2237   /* Number of data bytes in this buffer.  */
2238   unsigned int c;
2239   /* Bytes.  */
2240   bfd_byte buf[IEEE_BUFSIZE];
2241 };
2242
2243 /* In order to generate the BB11 blocks required by the HP emulator,
2244    we keep track of ranges of addresses which correspond to a given
2245    compilation unit.  */
2246
2247 struct ieee_range
2248 {
2249   /* Next range.  */
2250   struct ieee_range *next;
2251   /* Low address.  */
2252   bfd_vma low;
2253   /* High address.  */
2254   bfd_vma high;
2255 };
2256
2257 /* This is how we store types for the writing routines.  Most types
2258    are simply represented by a type index.  */
2259
2260 struct ieee_write_type
2261 {
2262   /* Type index.  */
2263   unsigned int indx;
2264   /* The size of the type, if known.  */
2265   unsigned int size;
2266   /* If this is a struct, this is where the struct definition is
2267      built.  */
2268   struct ieee_buf *strdef;
2269   /* Whether the type is unsigned.  */
2270   unsigned int unsignedp : 1;
2271   /* Whether this is a reference type.  */
2272   unsigned int referencep : 1;
2273 };
2274
2275 /* This is the type stack used by the debug writing routines.  FIXME:
2276    We could generate more efficient output if we remembered when we
2277    have output a particular type before.  */
2278
2279 struct ieee_type_stack
2280 {
2281   /* Next entry on stack.  */
2282   struct ieee_type_stack *next;
2283   /* Type information.  */
2284   struct ieee_write_type type;
2285 };
2286
2287 /* This is a list of associations between names and types.  This could
2288    be more efficiently implemented as a hash table.  */
2289
2290 struct ieee_name_type
2291 {
2292   /* Next name/type assocation.  */
2293   struct ieee_name_type *next;
2294   /* Name.  */
2295   const char *name;
2296   /* Type.  */
2297   struct ieee_write_type type;
2298   /* If this is a tag which has not yet been defined, this is the
2299      kind.  If the tag has been defined, this is DEBUG_KIND_ILLEGAL.  */
2300   enum debug_type_kind kind;
2301 };
2302
2303 /* This is a list of pending function parameter information.  We don't
2304    output them until we see the first block.  */
2305
2306 struct ieee_pending_parm
2307 {
2308   /* Next pending parameter.  */
2309   struct ieee_pending_parm *next;
2310   /* Name.  */
2311   const char *name;
2312   /* Type index.  */
2313   unsigned int type;
2314   /* Kind.  */
2315   enum debug_parm_kind kind;
2316   /* Value.  */
2317   bfd_vma val;
2318 };
2319
2320 /* This is the handle passed down by debug_write.  */
2321
2322 struct ieee_handle
2323 {
2324   /* BFD we are writing to.  */
2325   bfd *abfd;
2326   /* Current data buffer.  */
2327   struct ieee_buf *current;
2328   /* Filename of current compilation unit.  */
2329   const char *filename;
2330   /* Module name of current compilation unit.  */
2331   const char *modname;
2332   /* List of finished data buffers.  */
2333   struct ieee_buf *data;
2334   /* List of buffers for typedefs in the current compilation unit.  */
2335   struct ieee_buf *types;
2336   /* List of buffers for variables and functions in the current
2337      compilation unit.  */
2338   struct ieee_buf *vars;
2339   /* List of buffers for line numbers in the current compilation unit.  */
2340   struct ieee_buf *linenos;
2341   /* Ranges for the current compilation unit.  */
2342   struct ieee_range *ranges;
2343   /* Nested pending ranges.  */
2344   struct ieee_range *pending_ranges;
2345   /* Type stack.  */
2346   struct ieee_type_stack *type_stack;
2347   /* Next unallocated type index.  */
2348   unsigned int type_indx;
2349   /* Next unallocated name index.  */
2350   unsigned int name_indx;
2351   /* Typedefs.  */
2352   struct ieee_name_type *typedefs;
2353   /* Tags.  */
2354   struct ieee_name_type *tags;
2355   /* The depth of block nesting.  This is 0 outside a function, and 1
2356      just after start_function is called.  */
2357   unsigned int block_depth;
2358   /* Pending function parameters.  */
2359   struct ieee_pending_parm *pending_parms;
2360   /* Current line number filename.  */
2361   const char *lineno_filename;
2362   /* Line number name index.  */
2363   unsigned int lineno_name_indx;
2364 };
2365
2366 static boolean ieee_change_buffer
2367   PARAMS ((struct ieee_handle *, struct ieee_buf **));
2368 static boolean ieee_push_type
2369   PARAMS ((struct ieee_handle *, unsigned int, unsigned int, boolean));
2370 static unsigned int ieee_pop_type PARAMS ((struct ieee_handle *));
2371 static boolean ieee_add_range
2372   PARAMS ((struct ieee_handle *, bfd_vma, bfd_vma));
2373 static boolean ieee_start_range PARAMS ((struct ieee_handle *, bfd_vma));
2374 static boolean ieee_end_range PARAMS ((struct ieee_handle *, bfd_vma));
2375 static boolean ieee_real_write_byte PARAMS ((struct ieee_handle *, int));
2376 static boolean ieee_write_2bytes PARAMS ((struct ieee_handle *, int));
2377 static boolean ieee_write_number PARAMS ((struct ieee_handle *, bfd_vma));
2378 static boolean ieee_write_id PARAMS ((struct ieee_handle *, const char *));
2379 static boolean ieee_define_type
2380   PARAMS ((struct ieee_handle *, unsigned int, boolean));
2381 static boolean ieee_define_named_type
2382   PARAMS ((struct ieee_handle *, const char *, boolean, unsigned int, boolean,
2383            struct ieee_buf **));
2384 static boolean ieee_finish_compilation_unit PARAMS ((struct ieee_handle *));
2385 static boolean ieee_output_pending_parms PARAMS ((struct ieee_handle *));
2386
2387 static boolean ieee_start_compilation_unit PARAMS ((PTR, const char *));
2388 static boolean ieee_start_source PARAMS ((PTR, const char *));
2389 static boolean ieee_empty_type PARAMS ((PTR));
2390 static boolean ieee_void_type PARAMS ((PTR));
2391 static boolean ieee_int_type PARAMS ((PTR, unsigned int, boolean));
2392 static boolean ieee_float_type PARAMS ((PTR, unsigned int));
2393 static boolean ieee_complex_type PARAMS ((PTR, unsigned int));
2394 static boolean ieee_bool_type PARAMS ((PTR, unsigned int));
2395 static boolean ieee_enum_type
2396   PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
2397 static boolean ieee_pointer_type PARAMS ((PTR));
2398 static boolean ieee_function_type PARAMS ((PTR, int, boolean));
2399 static boolean ieee_reference_type PARAMS ((PTR));
2400 static boolean ieee_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
2401 static boolean ieee_array_type
2402   PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean));
2403 static boolean ieee_set_type PARAMS ((PTR, boolean));
2404 static boolean ieee_offset_type PARAMS ((PTR));
2405 static boolean ieee_method_type PARAMS ((PTR, boolean, int, boolean));
2406 static boolean ieee_const_type PARAMS ((PTR));
2407 static boolean ieee_volatile_type PARAMS ((PTR));
2408 static boolean ieee_start_struct_type
2409   PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int));
2410 static boolean ieee_struct_field
2411   PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
2412 static boolean ieee_end_struct_type PARAMS ((PTR));
2413 static boolean ieee_start_class_type
2414   PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int, boolean,
2415            boolean));
2416 static boolean ieee_class_static_member
2417   PARAMS ((PTR, const char *, const char *, enum debug_visibility));
2418 static boolean ieee_class_baseclass
2419   PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility));
2420 static boolean ieee_class_start_method PARAMS ((PTR, const char *));
2421 static boolean ieee_class_method_variant
2422   PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean,
2423            bfd_vma, boolean));
2424 static boolean ieee_class_static_method_variant
2425   PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean));
2426 static boolean ieee_class_end_method PARAMS ((PTR));
2427 static boolean ieee_end_class_type PARAMS ((PTR));
2428 static boolean ieee_typedef_type PARAMS ((PTR, const char *));
2429 static boolean ieee_tag_type
2430   PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
2431 static boolean ieee_typdef PARAMS ((PTR, const char *));
2432 static boolean ieee_tag PARAMS ((PTR, const char *));
2433 static boolean ieee_int_constant PARAMS ((PTR, const char *, bfd_vma));
2434 static boolean ieee_float_constant PARAMS ((PTR, const char *, double));
2435 static boolean ieee_typed_constant PARAMS ((PTR, const char *, bfd_vma));
2436 static boolean ieee_variable
2437   PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
2438 static boolean ieee_start_function PARAMS ((PTR, const char *, boolean));
2439 static boolean ieee_function_parameter
2440   PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
2441 static boolean ieee_start_block PARAMS ((PTR, bfd_vma));
2442 static boolean ieee_end_block PARAMS ((PTR, bfd_vma));
2443 static boolean ieee_end_function PARAMS ((PTR));
2444 static boolean ieee_lineno
2445   PARAMS ((PTR, const char *, unsigned long, bfd_vma));
2446
2447 static const struct debug_write_fns ieee_fns =
2448 {
2449   ieee_start_compilation_unit,
2450   ieee_start_source,
2451   ieee_empty_type,
2452   ieee_void_type,
2453   ieee_int_type,
2454   ieee_float_type,
2455   ieee_complex_type,
2456   ieee_bool_type,
2457   ieee_enum_type,
2458   ieee_pointer_type,
2459   ieee_function_type,
2460   ieee_reference_type,
2461   ieee_range_type,
2462   ieee_array_type,
2463   ieee_set_type,
2464   ieee_offset_type,
2465   ieee_method_type,
2466   ieee_const_type,
2467   ieee_volatile_type,
2468   ieee_start_struct_type,
2469   ieee_struct_field,
2470   ieee_end_struct_type,
2471   ieee_start_class_type,
2472   ieee_class_static_member,
2473   ieee_class_baseclass,
2474   ieee_class_start_method,
2475   ieee_class_method_variant,
2476   ieee_class_static_method_variant,
2477   ieee_class_end_method,
2478   ieee_end_class_type,
2479   ieee_typedef_type,
2480   ieee_tag_type,
2481   ieee_typdef,
2482   ieee_tag,
2483   ieee_int_constant,
2484   ieee_float_constant,
2485   ieee_typed_constant,
2486   ieee_variable,
2487   ieee_start_function,
2488   ieee_function_parameter,
2489   ieee_start_block,
2490   ieee_end_block,
2491   ieee_end_function,
2492   ieee_lineno
2493 };
2494
2495 /* Change the current buffer to a specified buffer chain.  */
2496
2497 static boolean
2498 ieee_change_buffer (info, ppbuf)
2499      struct ieee_handle *info;
2500      struct ieee_buf **ppbuf;
2501 {
2502   struct ieee_buf *buf;
2503
2504   if (*ppbuf != NULL)
2505     {
2506       for (buf = *ppbuf; buf->next != NULL; buf = buf->next)
2507         ;
2508     }
2509   else
2510     {
2511       buf = (struct ieee_buf *) xmalloc (sizeof *buf);
2512       buf->next = NULL;
2513       buf->c = 0;
2514       *ppbuf = buf;
2515     }
2516
2517   info->current = buf;
2518   return true;
2519 }
2520
2521 /* Push a type index onto the type stack.  */
2522
2523 static boolean
2524 ieee_push_type (info, indx, size, unsignedp)
2525      struct ieee_handle *info;
2526      unsigned int indx;
2527      unsigned int size;
2528      boolean unsignedp;
2529 {
2530   struct ieee_type_stack *ts;
2531
2532   ts = (struct ieee_type_stack *) xmalloc (sizeof *ts);
2533   memset (ts, 0, sizeof *ts);
2534
2535   ts->type.indx = indx;
2536   ts->type.size = size;
2537   ts->type.unsignedp = unsignedp;
2538
2539   ts->next = info->type_stack;
2540   info->type_stack = ts;
2541
2542   return true;
2543 }
2544
2545 /* Pop a type index off the type stack.  */
2546
2547 static unsigned int
2548 ieee_pop_type (info)
2549      struct ieee_handle *info;
2550 {
2551   struct ieee_type_stack *ts;
2552   unsigned int ret;
2553
2554   ts = info->type_stack;
2555   assert (ts != NULL);
2556   ret = ts->type.indx;
2557   info->type_stack = ts->next;
2558   free (ts);
2559   return ret;
2560 }
2561
2562 /* Add a range of bytes included in the current compilation unit.  */
2563
2564 static boolean
2565 ieee_add_range (info, low, high)
2566      struct ieee_handle *info;
2567      bfd_vma low;
2568      bfd_vma high;
2569 {
2570   struct ieee_range *r, **pr;
2571
2572   if (low == (bfd_vma) -1 || high == (bfd_vma) -1)
2573     return true;
2574
2575   for (r = info->ranges; r != NULL; r = r->next)
2576     {
2577       if (high >= r->low && low <= r->high)
2578         {
2579           /* The new range overlaps r.  */
2580           if (low < r->low)
2581             r->low = low;
2582           if (high > r->high)
2583             r->high = high;
2584           pr = &r->next;
2585           while (*pr != NULL && (*pr)->low <= r->high)
2586             {
2587               struct ieee_range *n;
2588
2589               if ((*pr)->high > r->high)
2590                 r->high = (*pr)->high;
2591               n = (*pr)->next;
2592               free (*pr);
2593               *pr = n;
2594             }
2595           return true;
2596         }
2597     }
2598
2599   r = (struct ieee_range *) xmalloc (sizeof *r);
2600   memset (r, 0, sizeof *r);
2601
2602   r->low = low;
2603   r->high = high;
2604
2605   /* Store the ranges sorted by address.  */
2606   for (pr = &info->ranges; *pr != NULL; pr = &(*pr)->next)
2607     if ((*pr)->next != NULL && (*pr)->next->low > high)
2608       break;
2609   r->next = *pr;
2610   *pr = r;
2611
2612   return true;
2613 }
2614
2615 /* Start a new range for which we only have the low address.  */
2616
2617 static boolean
2618 ieee_start_range (info, low)
2619      struct ieee_handle *info;
2620      bfd_vma low;
2621 {
2622   struct ieee_range *r;
2623
2624   r = (struct ieee_range *) xmalloc (sizeof *r);
2625   memset (r, 0, sizeof *r);
2626   r->low = low;
2627   r->next = info->pending_ranges;
2628   info->pending_ranges = r;
2629   return true;
2630 }  
2631
2632 /* Finish a range started by ieee_start_range.  */
2633
2634 static boolean
2635 ieee_end_range (info, high)
2636      struct ieee_handle *info;
2637      bfd_vma high;
2638 {
2639   struct ieee_range *r;
2640   bfd_vma low;
2641
2642   assert (info->pending_ranges != NULL);
2643   r = info->pending_ranges;
2644   low = r->low;
2645   info->pending_ranges = r->next;
2646   free (r);
2647   return ieee_add_range (info, low, high);
2648 }
2649
2650 /* Write a byte into the buffer.  We use a macro for speed and a
2651    function for the complex cases.  */
2652
2653 #define ieee_write_byte(info, b)                                \
2654   ((info)->current->c < IEEE_BUFSIZE                            \
2655    ? ((info)->current->buf[(info)->current->c++] = (b), true)   \
2656    : ieee_real_write_byte ((info), (b)))
2657
2658 static boolean
2659 ieee_real_write_byte (info, b)
2660      struct ieee_handle *info;
2661      int b;
2662 {
2663   if (info->current->c >= IEEE_BUFSIZE)
2664     {
2665       struct ieee_buf *n;
2666
2667       n = (struct ieee_buf *) xmalloc (sizeof *n);
2668       n->next = NULL;
2669       n->c = 0;
2670       info->current->next = n;
2671       info->current = n;
2672     }
2673
2674   info->current->buf[info->current->c] = b;
2675   ++info->current->c;
2676
2677   return true;
2678 }
2679
2680 /* Write out two bytes.  */
2681
2682 static boolean
2683 ieee_write_2bytes (info, i)
2684      struct ieee_handle *info;
2685      int i;
2686 {
2687   return (ieee_write_byte (info, i >> 8)
2688           && ieee_write_byte (info, i & 0xff));
2689 }
2690
2691 /* Write out an integer.  */
2692
2693 static boolean
2694 ieee_write_number (info, v)
2695      struct ieee_handle *info;
2696      bfd_vma v;
2697 {
2698   bfd_vma t;
2699   bfd_byte ab[20];
2700   bfd_byte *p;
2701   unsigned int c;
2702
2703   if (v <= (bfd_vma) ieee_number_end_enum)
2704     return ieee_write_byte (info, (int) v);
2705
2706   t = v;
2707   p = ab + sizeof ab;
2708   while (t != 0)
2709     {
2710       *--p = t & 0xff;
2711       t >>= 8;
2712     }
2713   c = (ab + 20) - p;
2714
2715   if (c > (unsigned int) (ieee_number_repeat_end_enum
2716                           - ieee_number_repeat_start_enum))
2717     {
2718       fprintf (stderr, "IEEE numeric overflow: 0x");
2719       fprintf_vma (stderr, v);
2720       fprintf (stderr, "\n");
2721       return false;
2722     }
2723
2724   if (! ieee_write_byte (info, (int) ieee_number_repeat_start_enum + c))
2725     return false;
2726   for (; c > 0; --c, ++p)
2727     {
2728       if (! ieee_write_byte (info, *p))
2729         return false;
2730     }
2731
2732   return true;
2733 }
2734
2735 /* Write out a string.  */
2736
2737 static boolean
2738 ieee_write_id (info, s)
2739      struct ieee_handle *info;
2740      const char *s;
2741 {
2742   unsigned int len;
2743
2744   len = strlen (s);
2745   if (len <= 0x7f)
2746     {
2747       if (! ieee_write_byte (info, len))
2748         return false;
2749     }
2750   else if (len <= 0xff)
2751     {
2752       if (! ieee_write_byte (info, (int) ieee_extension_length_1_enum)
2753           || ! ieee_write_byte (info, len))
2754         return false;
2755     }
2756   else if (len <= 0xffff)
2757     {
2758       if (! ieee_write_byte (info, (int) ieee_extension_length_2_enum)
2759           || ! ieee_write_2bytes (info, len))
2760         return false;
2761     }
2762   else
2763     {
2764       fprintf (stderr, "IEEE string length overflow: %u\n", len);
2765       return false;
2766     }
2767
2768   for (; *s != '\0'; s++)
2769     if (! ieee_write_byte (info, *s))
2770       return false;
2771
2772   return true;
2773 }
2774
2775 /* Start defining a type.  */
2776
2777 static boolean
2778 ieee_define_type (info, size, unsignedp)
2779      struct ieee_handle *info;
2780      unsigned int size;
2781      boolean unsignedp;
2782 {
2783   return ieee_define_named_type (info, (const char *) NULL, false, size,
2784                                  unsignedp, (struct ieee_buf **) NULL);
2785 }
2786
2787 /* Start defining a named type.  */
2788
2789 static boolean
2790 ieee_define_named_type (info, name, tagp, size, unsignedp, ppbuf)
2791      struct ieee_handle *info;
2792      const char *name;
2793      boolean tagp;
2794      unsigned int size;
2795      boolean unsignedp;
2796      struct ieee_buf **ppbuf;
2797 {
2798   unsigned int type_indx;
2799   unsigned int name_indx;
2800
2801   if (! tagp || name == NULL || *name == '\0')
2802     {
2803       type_indx = info->type_indx;
2804       ++info->type_indx;
2805     }
2806   else
2807     {
2808       struct ieee_name_type *nt;
2809
2810       /* The name is a tag.  If we have already defined the tag, we
2811          must use the existing type index.  */
2812       for (nt = info->tags; nt != NULL; nt = nt->next)
2813         if (nt->name[0] == name[0]
2814             && strcmp (nt->name, name) == 0)
2815           break;
2816
2817       if (nt == NULL)
2818         {
2819           nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
2820           memset (nt, 0, sizeof *nt);
2821           nt->name = name;
2822           nt->next = info->tags;
2823           info->tags = nt;
2824           nt->type.indx = info->type_indx;
2825           ++info->type_indx;
2826         }
2827
2828       nt->type.size = size;
2829       nt->type.unsignedp = unsignedp;
2830       nt->kind = DEBUG_KIND_ILLEGAL;
2831
2832       type_indx = nt->type.indx;
2833     }
2834
2835   name_indx = info->name_indx;
2836   ++info->name_indx;
2837
2838   if (name == NULL)
2839     name = "";
2840
2841   /* If we were given a buffer, use it; otherwise, use the general
2842      type information, and make sure that the type block is started.  */
2843   if (ppbuf != NULL)
2844     {
2845       if (! ieee_change_buffer (info, ppbuf))
2846         return false;
2847     }
2848   else if (info->types != NULL)
2849     {
2850       if (! ieee_change_buffer (info, &info->types))
2851         return false;
2852     }
2853   else
2854     {
2855       if (! ieee_change_buffer (info, &info->types)
2856           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
2857           || ! ieee_write_byte (info, 1)
2858           || ! ieee_write_number (info, 0)
2859           || ! ieee_write_id (info, info->modname))
2860         return false;
2861     }
2862
2863   /* Push the new type on the type stack, write out an NN record, and
2864      write out the start of a TY record.  The caller will then finish
2865      the TY record.  */
2866   return (ieee_push_type (info, type_indx, size, unsignedp)
2867           && ieee_write_byte (info, (int) ieee_nn_record)
2868           && ieee_write_number (info, name_indx)
2869           && ieee_write_id (info, name)
2870           && ieee_write_byte (info, (int) ieee_ty_record_enum)
2871           && ieee_write_number (info, type_indx)
2872           && ieee_write_byte (info, 0xce)
2873           && ieee_write_number (info, name_indx));
2874 }
2875 \f
2876 /* The general routine to write out IEEE debugging information.  */
2877
2878 boolean
2879 write_ieee_debugging_info (abfd, dhandle)
2880      bfd *abfd;
2881      PTR dhandle;
2882 {
2883   struct ieee_handle info;
2884   struct ieee_buf *tags;
2885   struct ieee_name_type *nt;
2886   asection *s;
2887   const char *err;
2888   struct ieee_buf *b;
2889
2890   memset (&info, 0, sizeof info);
2891   info.abfd = abfd;
2892   info.type_indx = 256;
2893   info.name_indx = 32;
2894
2895   if (! debug_write (dhandle, &ieee_fns, (PTR) &info))
2896     return false;
2897
2898   if (info.filename != NULL)
2899     {
2900       if (! ieee_finish_compilation_unit (&info))
2901         return false;
2902     }
2903
2904   /* Put any undefined tags in the global typedef information.  */
2905   tags = NULL;
2906   for (nt = info.tags; nt != NULL; nt = nt->next)
2907     {
2908       unsigned int name_indx;
2909       char code;
2910
2911       if (nt->kind == DEBUG_KIND_ILLEGAL)
2912         continue;
2913       if (tags == NULL)
2914         {
2915           if (! ieee_change_buffer (&info, &tags)
2916               || ! ieee_write_byte (&info, (int) ieee_bb_record_enum)
2917               || ! ieee_write_byte (&info, 2)
2918               || ! ieee_write_number (&info, 0)
2919               || ! ieee_write_id (&info, ""))
2920             return false;
2921         }
2922       name_indx = info.name_indx;
2923       ++info.name_indx;
2924       if (! ieee_write_byte (&info, (int) ieee_nn_record)
2925           || ! ieee_write_number (&info, name_indx)
2926           || ! ieee_write_id (&info, nt->name)
2927           || ! ieee_write_byte (&info, (int) ieee_ty_record_enum)
2928           || ! ieee_write_number (&info, nt->type.indx)
2929           || ! ieee_write_byte (&info, 0xce)
2930           || ! ieee_write_number (&info, name_indx))
2931         return false;
2932       switch (nt->kind)
2933         {
2934         default:
2935           abort ();
2936           return false;
2937         case DEBUG_KIND_STRUCT:
2938         case DEBUG_KIND_CLASS:
2939           code = 'S';
2940           break;
2941         case DEBUG_KIND_UNION:
2942         case DEBUG_KIND_UNION_CLASS:
2943           code = 'U';
2944           break;
2945         case DEBUG_KIND_ENUM:
2946           code = 'E';
2947           break;
2948         }
2949       if (! ieee_write_number (&info, code)
2950           || ! ieee_write_number (&info, 0))
2951         return false;
2952     }
2953   if (tags != NULL)
2954     {
2955       struct ieee_buf **pb;
2956
2957       if (! ieee_write_byte (&info, (int) ieee_be_record_enum))
2958         return false;
2959
2960       for (pb = &tags; *pb != NULL; pb = &(*pb)->next)
2961         ;
2962       *pb = info.data;
2963       info.data = tags;
2964     }
2965
2966   /* Now all the data is in info.data.  Write it out to the BFD.  We
2967      normally would need to worry about whether all the other sections
2968      are set up yet, but the IEEE backend will handle this particular
2969      case correctly regardless.  */
2970   if (info.data == NULL)
2971     {
2972       /* There is no debugging information.  */
2973       return true;
2974     }
2975   err = NULL;
2976   s = bfd_make_section (abfd, ".debug");
2977   if (s == NULL)
2978     err = "bfd_make_section";
2979   if (err == NULL)
2980     {
2981       if (! bfd_set_section_flags (abfd, s, SEC_DEBUGGING | SEC_HAS_CONTENTS))
2982         err = "bfd_set_section_flags";
2983     }
2984   if (err == NULL)
2985     {
2986       bfd_size_type size;
2987
2988       size = 0;
2989       for (b = info.data; b != NULL; b = b->next)
2990         size += b->c;
2991       if (! bfd_set_section_size (abfd, s, size))
2992         err = "bfd_set_section_size";
2993     }
2994   if (err == NULL)
2995     {
2996       file_ptr offset;
2997
2998       offset = 0;
2999       for (b = info.data; b != NULL; b = b->next)
3000         {
3001           if (! bfd_set_section_contents (abfd, s, b->buf, offset, b->c))
3002             {
3003               err = "bfd_set_section_contents";
3004               break;
3005             }
3006           offset += b->c;
3007         }
3008     }
3009
3010   if (err != NULL)
3011     {
3012       fprintf (stderr, "%s: %s: %s\n", bfd_get_filename (abfd), err,
3013                bfd_errmsg (bfd_get_error ()));
3014       return false;
3015     }
3016
3017   return true;
3018 }
3019
3020 /* Start writing out information for a compilation unit.  */
3021
3022 static boolean
3023 ieee_start_compilation_unit (p, filename)
3024      PTR p;
3025      const char *filename;
3026 {
3027   struct ieee_handle *info = (struct ieee_handle *) p;
3028   const char *modname;
3029   char *c, *s;
3030
3031   if (info->filename != NULL)
3032     {
3033       if (! ieee_finish_compilation_unit (info))
3034         return false;
3035     }
3036
3037   info->filename = filename;
3038   modname = strrchr (filename, '/');
3039   if (modname != NULL)
3040     ++modname;
3041   else
3042     {
3043       modname = strrchr (filename, '\\');
3044       if (modname != NULL)
3045         ++modname;
3046       else
3047         modname = filename;
3048     }
3049   c = xstrdup (modname);
3050   s = strrchr (c, '.');
3051   if (s != NULL)
3052     *s = '\0';
3053   info->modname = c;
3054
3055   info->types = NULL;
3056   info->vars = NULL;
3057   info->linenos = NULL;
3058   info->ranges = NULL;
3059
3060   return true;
3061 }
3062
3063 /* Finish up a compilation unit.  */
3064
3065 static boolean
3066 ieee_finish_compilation_unit (info)
3067      struct ieee_handle *info;
3068 {
3069   struct ieee_buf **pp;
3070   struct ieee_range *r;
3071
3072   if (info->types != NULL)
3073     {
3074       if (! ieee_change_buffer (info, &info->types)
3075           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
3076         return false;
3077     }
3078
3079   if (info->vars != NULL)
3080     {
3081       if (! ieee_change_buffer (info, &info->vars)
3082           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
3083         return false;
3084     }
3085
3086   if (info->linenos != NULL)
3087     {
3088       if (! ieee_change_buffer (info, &info->linenos)
3089           || ! ieee_write_byte (info, (int) ieee_be_record_enum))
3090         return false;
3091     }
3092
3093   for (pp = &info->data; *pp != NULL; pp = &(*pp)->next)
3094     ;
3095   *pp = info->types;
3096   for (; *pp != NULL; pp = &(*pp)->next)
3097     ;
3098   *pp = info->vars;
3099   for (; *pp != NULL; pp = &(*pp)->next)
3100     ;
3101   *pp = info->linenos;
3102
3103   /* Build BB10/BB11 blocks based on the ranges we recorded.  */
3104   if (! ieee_change_buffer (info, &info->data))
3105     return false;
3106
3107   if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
3108       || ! ieee_write_byte (info, 10)
3109       || ! ieee_write_number (info, 0)
3110       || ! ieee_write_id (info, info->modname)
3111       || ! ieee_write_id (info, "")
3112       || ! ieee_write_number (info, 0)
3113       || ! ieee_write_id (info, "GNU objcopy"))
3114     return false;
3115
3116   for (r = info->ranges; r != NULL; r = r->next)
3117     {
3118       bfd_vma low, high;
3119       asection *s;
3120       int kind;
3121
3122       low = r->low;
3123       high = r->high;
3124
3125       /* Find the section corresponding to this range.  */
3126       for (s = info->abfd->sections; s != NULL; s = s->next)
3127         {
3128           if (bfd_get_section_vma (info->abfd, s) <= low
3129               && high <= (bfd_get_section_vma (info->abfd, s)
3130                           + bfd_section_size (info->abfd, s)))
3131             break;
3132         }
3133
3134       if (s == NULL)
3135         {
3136           /* Just ignore this range.  */
3137           continue;
3138         }
3139
3140       /* Coalesce ranges if it seems reasonable.  */
3141       while (r->next != NULL
3142              && high + 64 >= r->next->low
3143              && (r->next->high
3144                  <= (bfd_get_section_vma (info->abfd, s)
3145                      + bfd_section_size (info->abfd, s))))
3146         {
3147           r = r->next;
3148           high = r->next->high;
3149         }
3150
3151       if ((s->flags & SEC_CODE) != 0)
3152         kind = 1;
3153       else if ((s->flags & SEC_READONLY) != 0)
3154         kind = 3;
3155       else
3156         kind = 2;
3157
3158       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
3159           || ! ieee_write_byte (info, 11)
3160           || ! ieee_write_number (info, 0)
3161           || ! ieee_write_id (info, "")
3162           || ! ieee_write_number (info, kind)
3163           || ! ieee_write_number (info, s->index)
3164           || ! ieee_write_number (info, low)
3165           || ! ieee_write_byte (info, (int) ieee_be_record_enum)
3166           || ! ieee_write_number (info, high - low))
3167         return false;
3168     }
3169
3170   if (! ieee_write_byte (info, (int) ieee_be_record_enum))
3171     return false;
3172
3173   return true;
3174 }
3175
3176 /* Start recording information from a particular source file.  This is
3177    used to record which file defined which types, variables, etc.  It
3178    is not used for line numbers, since the lineno entry point passes
3179    down the file name anyhow.  IEEE debugging information doesn't seem
3180    to store this information anywhere.  */
3181
3182 /*ARGSUSED*/
3183 static boolean
3184 ieee_start_source (p, filename)
3185      PTR p;
3186      const char *filename;
3187 {
3188   return true;
3189 }
3190
3191 /* Make an empty type.  */
3192
3193 static boolean
3194 ieee_empty_type (p)
3195      PTR p;
3196 {
3197   struct ieee_handle *info = (struct ieee_handle *) p;
3198
3199   return ieee_push_type (info, 0, 0, false);
3200 }
3201
3202 /* Make a void type.  */
3203
3204 static boolean
3205 ieee_void_type (p)
3206      PTR p;
3207 {
3208   struct ieee_handle *info = (struct ieee_handle *) p;
3209
3210   return ieee_push_type (info, 1, 0, false);
3211 }
3212
3213 /* Make an integer type.  */
3214
3215 static boolean
3216 ieee_int_type (p, size, unsignedp)
3217      PTR p;
3218      unsigned int size;
3219      boolean unsignedp;
3220 {
3221   struct ieee_handle *info = (struct ieee_handle *) p;
3222   unsigned int indx;
3223
3224   switch (size)
3225     {
3226     case 1:
3227       indx = (int) builtin_signed_char;
3228       break;
3229     case 2:
3230       indx = (int) builtin_signed_short_int;
3231       break;
3232     case 4:
3233       indx = (int) builtin_signed_long;
3234       break;
3235     case 8:
3236       indx = (int) builtin_signed_long_long;
3237       break;
3238     default:
3239       fprintf (stderr, "IEEE unsupported integer type size %u\n", size);
3240       return false;
3241     }
3242
3243   if (unsignedp)
3244     ++indx;
3245
3246   return ieee_push_type (info, indx, size, unsignedp);
3247 }
3248
3249 /* Make a floating point type.  */
3250
3251 static boolean
3252 ieee_float_type (p, size)
3253      PTR p;
3254      unsigned int size;
3255 {
3256   struct ieee_handle *info = (struct ieee_handle *) p;
3257   unsigned int indx;
3258
3259   switch (size)
3260     {
3261     case 4:
3262       indx = (int) builtin_float;
3263       break;
3264     case 8:
3265       indx = (int) builtin_double;
3266       break;
3267     case 12:
3268       /* FIXME: This size really depends upon the processor.  */
3269       indx = (int) builtin_long_double;
3270       break;
3271     case 16:
3272       indx = (int) builtin_long_long_double;
3273       break;
3274     default:
3275       fprintf (stderr, "IEEE unsupported float type size %u\n", size);
3276       return false;
3277     }
3278
3279   return ieee_push_type (info, indx, size, false);
3280 }
3281
3282 /* Make a complex type.  */
3283
3284 static boolean
3285 ieee_complex_type (p, size)
3286      PTR p;
3287      unsigned int size;
3288 {
3289   struct ieee_handle *info = (struct ieee_handle *) p;
3290   char code;
3291
3292   switch (size)
3293     {
3294     case 4:
3295       code = 'c';
3296       break;
3297     case 8:
3298       code = 'd';
3299       break;
3300     default:
3301       fprintf (stderr, "IEEE unsupported complex type size %u\n", size);
3302       return false;
3303     }
3304
3305   /* FIXME: I don't know what the string is for.  */
3306   return (ieee_define_type (info, size, false)
3307           && ieee_write_number (info, code)
3308           && ieee_write_id (info, ""));
3309 }
3310
3311 /* Make a boolean type.  IEEE doesn't support these, so we just make
3312    an integer type instead.  */
3313
3314 static boolean
3315 ieee_bool_type (p, size)
3316      PTR p;
3317      unsigned int size;
3318 {
3319   return ieee_int_type (p, size, true);
3320 }
3321
3322 /* Make an enumeration.  */
3323
3324 static boolean
3325 ieee_enum_type (p, tag, names, vals)
3326      PTR p;
3327      const char *tag;
3328      const char **names;
3329      bfd_signed_vma *vals;
3330 {
3331   struct ieee_handle *info = (struct ieee_handle *) p;
3332   boolean simple;
3333   int i;
3334
3335   /* If this is a simple enumeration, in which the values start at 0
3336      and always increment by 1, we can use type E.  Otherwise we must
3337      use type N.  */
3338
3339   simple = true;
3340   if (names != NULL)
3341     {
3342       for (i = 0; names[i] != NULL; i++)
3343         {
3344           if (vals[i] != i)
3345             {
3346               simple = false;
3347               break;
3348             }
3349         }
3350     }
3351
3352   if (! ieee_define_named_type (info, tag, true, 0, true,
3353                                 (struct ieee_buf **) NULL)
3354       || ! ieee_write_number (info, simple ? 'E' : 'N'))
3355     return false;
3356   if (simple)
3357     {
3358       /* FIXME: This is supposed to be the enumeration size, but we
3359          don't store that.  */
3360       if (! ieee_write_number (info, 4))
3361         return false;
3362     }
3363   if (names != NULL)
3364     {
3365       for (i = 0; names[i] != NULL; i++)
3366         {
3367           if (! ieee_write_id (info, names[i]))
3368             return false;
3369           if (! simple)
3370             {
3371               if (! ieee_write_number (info, vals[i]))
3372                 return false;
3373             }
3374         }
3375     }
3376
3377   return true;
3378 }
3379
3380 /* Make a pointer type.  */
3381
3382 static boolean
3383 ieee_pointer_type (p)
3384      PTR p;
3385 {
3386   struct ieee_handle *info = (struct ieee_handle *) p;
3387   unsigned int indx;
3388
3389   indx = ieee_pop_type (info);
3390
3391   /* A pointer to a simple builtin type can be obtained by adding 32.  */
3392   if (indx < 32)
3393     return ieee_push_type (info, indx + 32, 0, true);
3394
3395   return (ieee_define_type (info, 0, true)
3396           && ieee_write_number (info, 'P')
3397           && ieee_write_number (info, indx));
3398 }
3399
3400 /* Make a function type.  */
3401
3402 static boolean
3403 ieee_function_type (p, argcount, varargs)
3404      PTR p;
3405      int argcount;
3406      boolean varargs;
3407 {
3408   struct ieee_handle *info = (struct ieee_handle *) p;
3409   unsigned int *args = NULL;
3410   int i;
3411   unsigned int retindx;
3412
3413   if (argcount > 0)
3414     {
3415       args = (unsigned int *) xmalloc (argcount * sizeof *args);
3416       for (i = argcount - 1; i >= 0; i--)
3417         args[i] = ieee_pop_type (info);
3418     }
3419   else if (argcount < 0)
3420     varargs = false;
3421
3422   retindx = ieee_pop_type (info);
3423
3424   /* An attribute of 0x41 means that the frame and push mask are
3425      unknown.  */
3426   if (! ieee_define_type (info, 0, true)
3427       || ! ieee_write_number (info, 'x')
3428       || ! ieee_write_number (info, 0x41)
3429       || ! ieee_write_number (info, 0)
3430       || ! ieee_write_number (info, 0)
3431       || ! ieee_write_number (info, retindx)
3432       || ! ieee_write_number (info, (bfd_vma) argcount + (varargs ? 1 : 0)))
3433     return false;
3434   if (argcount > 0)
3435     {
3436       for (i = 0; i < argcount; i++)
3437         if (! ieee_write_number (info, args[i]))
3438           return false;
3439       free (args);
3440     }
3441   if (varargs)
3442     {
3443       /* A varargs function is represented by writing out the last
3444          argument as type void *, although this makes little sense.  */
3445       if (! ieee_write_number (info, (bfd_vma) builtin_void + 32))
3446         return false;
3447     }
3448
3449   return ieee_write_number (info, 0);
3450 }
3451
3452 /* Make a reference type.  */
3453
3454 static boolean
3455 ieee_reference_type (p)
3456      PTR p;
3457 {
3458   struct ieee_handle *info = (struct ieee_handle *) p;
3459
3460   /* IEEE appears to record a normal pointer type, and then use a
3461      pmisc record to indicate that it is really a reference.  */
3462
3463   if (! ieee_pointer_type (p))
3464     return false;
3465   info->type_stack->type.referencep = true;
3466   return true;
3467 }
3468
3469 /* Make a range type.  */
3470
3471 static boolean
3472 ieee_range_type (p, low, high)
3473      PTR p;
3474      bfd_signed_vma low;
3475      bfd_signed_vma high;
3476 {
3477   struct ieee_handle *info = (struct ieee_handle *) p;
3478   unsigned int size;
3479   boolean unsignedp;
3480
3481   size = info->type_stack->type.size;
3482   unsignedp = info->type_stack->type.unsignedp;
3483   (void) ieee_pop_type (info);
3484   return (ieee_define_type (info, size, unsignedp)
3485           && ieee_write_number (info, 'R')
3486           && ieee_write_number (info, (bfd_vma) low)
3487           && ieee_write_number (info, (bfd_vma) high)
3488           && ieee_write_number (info, unsignedp ? 0 : 1)
3489           && ieee_write_number (info, size));
3490 }
3491
3492 /* Make an array type.  */
3493
3494 /*ARGSUSED*/
3495 static boolean
3496 ieee_array_type (p, low, high, stringp)
3497      PTR p;
3498      bfd_signed_vma low;
3499      bfd_signed_vma high;
3500      boolean stringp;
3501 {
3502   struct ieee_handle *info = (struct ieee_handle *) p;
3503   unsigned int eleindx;
3504
3505   /* IEEE does not store the range, so we just ignore it.  */
3506   (void) ieee_pop_type (info);
3507   eleindx = ieee_pop_type (info);
3508
3509   if (! ieee_define_type (info, 0, false)
3510       || ! ieee_write_number (info, low == 0 ? 'Z' : 'C')
3511       || ! ieee_write_number (info, eleindx))
3512     return false;
3513   if (low != 0)
3514     {
3515       if (! ieee_write_number (info, low))
3516         return false;
3517     }
3518
3519   return ieee_write_number (info, high);
3520 }
3521
3522 /* Make a set type.  */
3523
3524 static boolean
3525 ieee_set_type (p, bitstringp)
3526      PTR p;
3527      boolean bitstringp;
3528 {
3529   struct ieee_handle *info = (struct ieee_handle *) p;
3530   unsigned int eleindx;
3531
3532   eleindx = ieee_pop_type (info);
3533
3534   /* FIXME: We don't know the size, so we just use 4.  */
3535
3536   return (ieee_define_type (info, 0, true)
3537           && ieee_write_number (info, 's')
3538           && ieee_write_number (info, 4)
3539           && ieee_write_number (info, eleindx));
3540 }
3541
3542 /* Make an offset type.  */
3543
3544 static boolean
3545 ieee_offset_type (p)
3546      PTR p;
3547 {
3548   struct ieee_handle *info = (struct ieee_handle *) p;
3549   unsigned int targetindx, baseindx;
3550
3551   targetindx = ieee_pop_type (info);
3552   baseindx = ieee_pop_type (info);
3553
3554   /* FIXME: The MRI C++ compiler does not appear to generate any
3555      useful type information about an offset type.  It just records a
3556      pointer to member as an integer.  The MRI/HP IEEE spec does
3557      describe a pmisc record which can be used for a pointer to
3558      member.  Unfortunately, it does not describe the target type,
3559      which seems pretty important.  I'm going to punt this for now.  */
3560
3561   return ieee_int_type (p, 4, true);
3562 }  
3563
3564 /* Make a method type.  */
3565
3566 static boolean
3567 ieee_method_type (p, domain, argcount, varargs)
3568      PTR p;
3569      boolean domain;
3570      int argcount;
3571      boolean varargs;
3572 {
3573   struct ieee_handle *info = (struct ieee_handle *) p;
3574
3575   /* FIXME: The MRI/HP IEEE spec defines a pmisc record to use for a
3576      method, but the definition is incomplete.  We just output an 'x'
3577      type.  */
3578
3579   if (domain)
3580     (void) ieee_pop_type (info);
3581
3582   return ieee_function_type (p, argcount, varargs);
3583 }
3584
3585 /* Make a const qualified type.  */
3586
3587 static boolean
3588 ieee_const_type (p)
3589      PTR p;
3590 {
3591   struct ieee_handle *info = (struct ieee_handle *) p;
3592   unsigned int size;
3593   boolean unsignedp;
3594   unsigned int indx;
3595
3596   size = info->type_stack->type.size;
3597   unsignedp = info->type_stack->type.unsignedp;
3598   indx = ieee_pop_type (info);
3599   return (ieee_define_type (info, size, unsignedp)
3600           && ieee_write_number (info, 'n')
3601           && ieee_write_number (info, 1)
3602           && ieee_write_number (info, indx));
3603 }
3604
3605 /* Make a volatile qualified type.  */
3606
3607 static boolean
3608 ieee_volatile_type (p)
3609      PTR p;
3610 {
3611   struct ieee_handle *info = (struct ieee_handle *) p;
3612   unsigned int size;
3613   boolean unsignedp;
3614   unsigned int indx;
3615
3616   size = info->type_stack->type.size;
3617   unsignedp = info->type_stack->type.unsignedp;
3618   indx = ieee_pop_type (info);
3619   return (ieee_define_type (info, size, unsignedp)
3620           && ieee_write_number (info, 'n')
3621           && ieee_write_number (info, 2)
3622           && ieee_write_number (info, indx));
3623 }
3624
3625 /* Start defining a struct type.  We build it in the strdef field on
3626    the stack, to avoid confusing type definitions required by the
3627    fields with the struct type itself.  */
3628
3629 static boolean
3630 ieee_start_struct_type (p, tag, id, structp, size)
3631      PTR p;
3632      const char *tag;
3633      unsigned int id;
3634      boolean structp;
3635      unsigned int size;
3636 {
3637   struct ieee_handle *info = (struct ieee_handle *) p;
3638   struct ieee_buf *strdef;
3639
3640   strdef = NULL;
3641   if (! ieee_define_named_type (info, tag, true, size, true, &strdef)
3642       || ! ieee_write_number (info, structp ? 'S' : 'U')
3643       || ! ieee_write_number (info, size))
3644     return false;
3645
3646   info->type_stack->type.strdef = strdef;
3647
3648   return true;
3649 }
3650
3651 /* Add a field to a struct.  */
3652
3653 static boolean
3654 ieee_struct_field (p, name, bitpos, bitsize, visibility)
3655      PTR p;
3656      const char *name;
3657      bfd_vma bitpos;
3658      bfd_vma bitsize;
3659      enum debug_visibility visibility;
3660 {
3661   struct ieee_handle *info = (struct ieee_handle *) p;
3662   unsigned int size;
3663   boolean unsignedp;
3664   unsigned int indx;
3665   bfd_vma offset;
3666
3667   size = info->type_stack->type.size;
3668   unsignedp = info->type_stack->type.unsignedp;
3669   indx = ieee_pop_type (info);
3670
3671   assert (info->type_stack != NULL && info->type_stack->type.strdef != NULL);
3672
3673   /* If the bitsize doesn't match the expected size, we need to output
3674      a bitfield type.  */
3675   if (size == 0 || bitsize == size * 8)
3676     offset = bitpos / 8;
3677   else
3678     {
3679       if (! ieee_define_type (info, 0, unsignedp)
3680           || ! ieee_write_number (info, 'g')
3681           || ! ieee_write_number (info, unsignedp ? 0 : 1)
3682           || ! ieee_write_number (info, indx))
3683         return false;
3684       indx = ieee_pop_type (info);
3685       offset = bitpos;
3686     }
3687
3688   /* Switch to the struct we are building in order to output this
3689      field definition.  */
3690   return (ieee_change_buffer (info, &info->type_stack->type.strdef)
3691           && ieee_write_id (info, name)
3692           && ieee_write_number (info, indx)
3693           && ieee_write_number (info, offset));
3694 }
3695
3696 /* Finish up a struct type.  */
3697
3698 static boolean
3699 ieee_end_struct_type (p)
3700      PTR p;
3701 {
3702   struct ieee_handle *info = (struct ieee_handle *) p;
3703   struct ieee_buf **pb;
3704
3705   assert (info->type_stack != NULL && info->type_stack->type.strdef != NULL);
3706
3707   /* Make sure we have started the types block.  */
3708   if (info->types == NULL)
3709     {
3710       if (! ieee_change_buffer (info, &info->types)
3711           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
3712           || ! ieee_write_byte (info, 1)
3713           || ! ieee_write_number (info, 0)
3714           || ! ieee_write_id (info, info->modname))
3715         return false;
3716     }
3717
3718   /* Append the struct definition to the types.  */
3719   for (pb = &info->types; *pb != NULL; pb = &(*pb)->next)
3720     ;
3721   *pb = info->type_stack->type.strdef;
3722   info->type_stack->type.strdef = NULL;
3723
3724   /* Leave the struct on the type stack.  */
3725
3726   return true;
3727 }
3728
3729 /* Start a class type.  */
3730
3731 static boolean
3732 ieee_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
3733      PTR p;
3734      const char *tag;
3735      unsigned int id;
3736      boolean structp;
3737      unsigned int size;
3738      boolean vptr;
3739      boolean ownvptr;
3740 {
3741   struct ieee_handle *info = (struct ieee_handle *) p;
3742
3743   /* FIXME.  */
3744   if (vptr && ! ownvptr)
3745     (void) ieee_pop_type (info);
3746   return ieee_start_struct_type (p, tag, id, structp, size);
3747 }
3748
3749 /* Add a static member to a class.  */
3750
3751 static boolean
3752 ieee_class_static_member (p, name, physname, visibility)
3753      PTR p;
3754      const char *name;
3755      const char *physname;
3756      enum debug_visibility visibility;
3757 {
3758   struct ieee_handle *info = (struct ieee_handle *) p;
3759
3760   /* FIXME.  */
3761   (void) ieee_pop_type (info);
3762   return true;
3763 }
3764
3765 /* Add a base class to a class.  */
3766
3767 static boolean
3768 ieee_class_baseclass (p, bitpos, virtual, visibility)
3769      PTR p;
3770      bfd_vma bitpos;
3771      boolean virtual;
3772      enum debug_visibility visibility;
3773 {
3774   struct ieee_handle *info = (struct ieee_handle *) p;
3775
3776   /* FIXME.  */
3777   (void) ieee_pop_type (info);
3778   return true;
3779 }
3780
3781 /* Start building a method for a class.  */
3782
3783 static boolean
3784 ieee_class_start_method (p, name)
3785      PTR p;
3786      const char *name;
3787 {
3788   /* FIXME.  */
3789   return true;
3790 }
3791
3792 /* Define a new method variant.  */
3793
3794 static boolean
3795 ieee_class_method_variant (p, physname, visibility, constp, volatilep,
3796                            voffset, context)
3797      PTR p;
3798      const char *physname;
3799      enum debug_visibility visibility;
3800      boolean constp;
3801      boolean volatilep;
3802      bfd_vma voffset;
3803      boolean context;
3804 {
3805   struct ieee_handle *info = (struct ieee_handle *) p;
3806
3807   /* FIXME.  */
3808   (void) ieee_pop_type (info);
3809   if (context)
3810     (void) ieee_pop_type (info);
3811   return true;
3812 }
3813
3814 /* Define a new static method variant.  */
3815
3816 static boolean
3817 ieee_class_static_method_variant (p, physname, visibility, constp, volatilep)
3818      PTR p;
3819      const char *physname;
3820      enum debug_visibility visibility;
3821      boolean constp;
3822      boolean volatilep;
3823 {
3824   struct ieee_handle *info = (struct ieee_handle *) p;
3825
3826   /* FIXME.  */
3827   (void) ieee_pop_type (info);
3828   return true;
3829 }
3830
3831 /* Finish up a method.  */
3832
3833 static boolean
3834 ieee_class_end_method (p)
3835      PTR p;
3836 {
3837   /* FIXME.  */
3838   return true;
3839 }
3840
3841 /* Finish up a class.  */
3842
3843 static boolean
3844 ieee_end_class_type (p)
3845      PTR p;
3846 {
3847   return ieee_end_struct_type (p);
3848 }
3849
3850 /* Push a previously seen typedef onto the type stack.  */
3851
3852 static boolean
3853 ieee_typedef_type (p, name)
3854      PTR p;
3855      const char *name;
3856 {
3857   struct ieee_handle *info = (struct ieee_handle *) p;
3858   register struct ieee_name_type *nt;
3859
3860   for (nt = info->typedefs; nt != NULL; nt = nt->next)
3861     {
3862       if (nt->name[0] == name[0]
3863           && strcmp (nt->name, name) == 0)
3864         {
3865           if (! ieee_push_type (info, nt->type.indx, nt->type.size,
3866                                 nt->type.unsignedp))
3867             return false;
3868           /* Copy over any other type information we may have.  */
3869           info->type_stack->type = nt->type;
3870           return true;
3871         }
3872     }
3873
3874   abort ();
3875 }
3876
3877 /* Push a tagged type onto the type stack.  */
3878
3879 static boolean
3880 ieee_tag_type (p, name, id, kind)
3881      PTR p;
3882      const char *name;
3883      unsigned int id;
3884      enum debug_type_kind kind;
3885 {
3886   struct ieee_handle *info = (struct ieee_handle *) p;
3887   register struct ieee_name_type *nt;
3888
3889   if (name == NULL)
3890     return true;
3891
3892   for (nt = info->tags; nt != NULL; nt = nt->next)
3893     {
3894       if (nt->name[0] == name[0]
3895           && strcmp (nt->name, name) == 0)
3896         {
3897           if (! ieee_push_type (info, nt->type.indx, nt->type.size,
3898                                 nt->type.unsignedp))
3899             return false;
3900           /* Copy over any other type information we may have.  */
3901           info->type_stack->type = nt->type;
3902           return true;
3903         }
3904     }
3905
3906   nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
3907   memset (nt, 0, sizeof *nt);
3908
3909   nt->name = name;
3910   nt->type.indx = info->type_indx;
3911   ++info->type_indx;
3912   nt->kind = kind;
3913
3914   nt->next = info->tags;
3915   info->tags = nt;
3916
3917   return ieee_push_type (info, nt->type.indx, 0, false);
3918 }
3919
3920 /* Output a typedef.  */
3921
3922 static boolean
3923 ieee_typdef (p, name)
3924      PTR p;
3925      const char *name;
3926 {
3927   struct ieee_handle *info = (struct ieee_handle *) p;
3928   struct ieee_name_type *nt;
3929   unsigned int size;
3930   boolean unsignedp;
3931   unsigned int indx;
3932
3933   nt = (struct ieee_name_type *) xmalloc (sizeof *nt);
3934   memset (nt, 0, sizeof *nt);
3935   nt->name = name;
3936   nt->type = info->type_stack->type;
3937   nt->kind = DEBUG_KIND_ILLEGAL;
3938
3939   nt->next = info->typedefs;
3940   info->typedefs = nt;
3941
3942   size = info->type_stack->type.size;
3943   unsignedp = info->type_stack->type.unsignedp;
3944   indx = ieee_pop_type (info);
3945
3946   /* If this is a simple builtin type using a builtin name, we don't
3947      want to output the typedef itself.  We also want to change the
3948      type index to correspond to the name being used.  We recognize
3949      names used in stabs debugging output even if they don't exactly
3950      correspond to the names used for the IEEE builtin types.  */
3951   if (indx <= (unsigned int) builtin_bcd_float)
3952     {
3953       boolean found;
3954
3955       found = false;
3956       switch ((enum builtin_types) indx)
3957         {
3958         default:
3959           break;
3960
3961         case builtin_void:
3962           if (strcmp (name, "void") == 0)
3963             found = true;
3964           break;
3965
3966         case builtin_signed_char:
3967         case builtin_char:
3968           if (strcmp (name, "signed char") == 0)
3969             {
3970               indx = (unsigned int) builtin_signed_char;
3971               found = true;
3972             }
3973           else if (strcmp (name, "char") == 0)
3974             {
3975               indx = (unsigned int) builtin_char;
3976               found = true;
3977             }
3978           break;
3979
3980         case builtin_unsigned_char:
3981           if (strcmp (name, "unsigned char") == 0)
3982             found = true;
3983           break;
3984
3985         case builtin_signed_short_int:
3986         case builtin_short:
3987         case builtin_short_int:
3988         case builtin_signed_short:
3989           if (strcmp (name, "signed short int") == 0)
3990             {
3991               indx = (unsigned int) builtin_signed_short_int;
3992               found = true;
3993             }
3994           else if (strcmp (name, "short") == 0)
3995             {
3996               indx = (unsigned int) builtin_short;
3997               found = true;
3998             }
3999           else if (strcmp (name, "short int") == 0)
4000             {
4001               indx = (unsigned int) builtin_short_int;
4002               found = true;
4003             }
4004           else if (strcmp (name, "signed short") == 0)
4005             {
4006               indx = (unsigned int) builtin_signed_short;
4007               found = true;
4008             }
4009           break;
4010
4011         case builtin_unsigned_short_int:
4012         case builtin_unsigned_short:
4013           if (strcmp (name, "unsigned short int") == 0
4014               || strcmp (name, "short unsigned int") == 0)
4015             {
4016               indx = builtin_unsigned_short_int;
4017               found = true;
4018             }
4019           else if (strcmp (name, "unsigned short") == 0)
4020             {
4021               indx = builtin_unsigned_short;
4022               found = true;
4023             }
4024           break;
4025
4026         case builtin_signed_long:
4027         case builtin_int: /* FIXME: Size depends upon architecture.  */
4028         case builtin_long:
4029           if (strcmp (name, "signed long") == 0)
4030             {
4031               indx = builtin_signed_long;
4032               found = true;
4033             }
4034           else if (strcmp (name, "int") == 0)
4035             {
4036               indx = builtin_int;
4037               found = true;
4038             }
4039           else if (strcmp (name, "long") == 0
4040                    || strcmp (name, "long int") == 0)
4041             {
4042               indx = builtin_long;
4043               found = true;
4044             }
4045           break;
4046
4047         case builtin_unsigned_long:
4048         case builtin_unsigned: /* FIXME: Size depends upon architecture.  */
4049         case builtin_unsigned_int: /* FIXME: Like builtin_unsigned.  */
4050           if (strcmp (name, "unsigned long") == 0
4051               || strcmp (name, "long unsigned int") == 0)
4052             {
4053               indx = builtin_unsigned_long;
4054               found = true;
4055             }
4056           else if (strcmp (name, "unsigned") == 0)
4057             {
4058               indx = builtin_unsigned;
4059               found = true;
4060             }
4061           else if (strcmp (name, "unsigned int") == 0)
4062             {
4063               indx = builtin_unsigned_int;
4064               found = true;
4065             }
4066           break;
4067
4068         case builtin_signed_long_long:
4069           if (strcmp (name, "signed long long") == 0
4070               || strcmp (name, "long long int") == 0)
4071             found = true;
4072           break;
4073
4074         case builtin_unsigned_long_long:
4075           if (strcmp (name, "unsigned long long") == 0
4076               || strcmp (name, "long long unsigned int") == 0)
4077             found = true;
4078           break;
4079
4080         case builtin_float:
4081           if (strcmp (name, "float") == 0)
4082             found = true;
4083           break;
4084
4085         case builtin_double:
4086           if (strcmp (name, "double") == 0)
4087             found = true;
4088           break;
4089
4090         case builtin_long_double:
4091           if (strcmp (name, "long double") == 0)
4092             found = true;
4093           break;
4094
4095         case builtin_long_long_double:
4096           if (strcmp (name, "long long double") == 0)
4097             found = true;
4098           break;
4099         }
4100
4101       if (found)
4102         {
4103           nt->type.indx = indx;
4104           return true;
4105         }
4106     }
4107
4108   if (! ieee_define_named_type (info, name, false, size, unsignedp,
4109                                 (struct ieee_buf **) NULL)
4110       || ! ieee_write_number (info, 'T')
4111       || ! ieee_write_number (info, indx))
4112     return false;
4113
4114   /* Remove the type we just added to the type stack.  */
4115   (void) ieee_pop_type (info);
4116
4117   return true;
4118 }
4119
4120 /* Output a tag for a type.  We don't have to do anything here.  */
4121
4122 static boolean
4123 ieee_tag (p, name)
4124      PTR p;
4125      const char *name;
4126 {
4127   struct ieee_handle *info = (struct ieee_handle *) p;
4128
4129   (void) ieee_pop_type (info);
4130   return true;
4131 }
4132
4133 /* Output an integer constant.  */
4134
4135 static boolean
4136 ieee_int_constant (p, name, val)
4137      PTR p;
4138      const char *name;
4139      bfd_vma val;
4140 {
4141   /* FIXME.  */
4142   return true;
4143 }
4144
4145 /* Output a floating point constant.  */
4146
4147 static boolean
4148 ieee_float_constant (p, name, val)
4149      PTR p;
4150      const char *name;
4151      double val;
4152 {
4153   /* FIXME.  */
4154   return true;
4155 }
4156
4157 /* Output a typed constant.  */
4158
4159 static boolean
4160 ieee_typed_constant (p, name, val)
4161      PTR p;
4162      const char *name;
4163      bfd_vma val;
4164 {
4165   struct ieee_handle *info = (struct ieee_handle *) p;
4166
4167   /* FIXME.  */
4168   (void) ieee_pop_type (info);
4169   return true;
4170 }
4171
4172 /* Output a variable.  */
4173
4174 static boolean
4175 ieee_variable (p, name, kind, val)
4176      PTR p;
4177      const char *name;
4178      enum debug_var_kind kind;
4179      bfd_vma val;
4180 {
4181   struct ieee_handle *info = (struct ieee_handle *) p;
4182   unsigned int name_indx;
4183   unsigned int size;
4184   unsigned int type_indx;
4185   boolean asn;
4186
4187   /* Make sure the variable section is started.  */
4188   if (info->vars != NULL)
4189     {
4190       if (! ieee_change_buffer (info, &info->vars))
4191         return false;
4192     }
4193   else
4194     {
4195       if (! ieee_change_buffer (info, &info->vars)
4196           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4197           || ! ieee_write_byte (info, 3)
4198           || ! ieee_write_number (info, 0)
4199           || ! ieee_write_id (info, info->modname))
4200         return false;
4201     }
4202
4203   name_indx = info->name_indx;
4204   ++info->name_indx;
4205
4206   size = info->type_stack->type.size;
4207   type_indx = ieee_pop_type (info);
4208
4209   /* Write out an NN and an ATN record for this variable.  */
4210   if (! ieee_write_byte (info, (int) ieee_nn_record)
4211       || ! ieee_write_number (info, name_indx)
4212       || ! ieee_write_id (info, name)
4213       || ! ieee_write_2bytes (info, (int) ieee_atn_record_enum)
4214       || ! ieee_write_number (info, name_indx)
4215       || ! ieee_write_number (info, type_indx))
4216     return false;
4217   switch (kind)
4218     {
4219     default:
4220       abort ();
4221       return false;
4222     case DEBUG_GLOBAL:
4223       if (! ieee_write_number (info, 8)
4224           || ! ieee_add_range (info, val, val + size))
4225         return false;
4226       asn = true;
4227       break;
4228     case DEBUG_STATIC:
4229     case DEBUG_LOCAL_STATIC:
4230       if (! ieee_write_number (info, 3)
4231           || ! ieee_add_range (info, val, val + size))
4232         return false;
4233       asn = true;
4234       break;
4235     case DEBUG_LOCAL:
4236       if (! ieee_write_number (info, 1)
4237           || ! ieee_write_number (info, val))
4238         return false;
4239       asn = false;
4240       break;
4241     case DEBUG_REGISTER:
4242       if (! ieee_write_number (info, 2)
4243           || ! ieee_write_number (info,
4244                                   ieee_genreg_to_regno (info->abfd, val)))
4245         return false;
4246       asn = false;
4247       break;
4248     }
4249
4250   if (asn)
4251     {
4252       if (! ieee_write_2bytes (info, (int) ieee_asn_record_enum)
4253           || ! ieee_write_number (info, name_indx)
4254           || ! ieee_write_number (info, val))
4255         return false;
4256     }
4257
4258   return true;
4259 }
4260
4261 /* Start outputting information for a function.  */
4262
4263 static boolean
4264 ieee_start_function (p, name, global)
4265      PTR p;
4266      const char *name;
4267      boolean global;
4268 {
4269   struct ieee_handle *info = (struct ieee_handle *) p;
4270   unsigned int indx;
4271
4272   /* Make sure the variable section is started.  */
4273   if (info->vars != NULL)
4274     {
4275       if (! ieee_change_buffer (info, &info->vars))
4276         return false;
4277     }
4278   else
4279     {
4280       if (! ieee_change_buffer (info, &info->vars)
4281           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4282           || ! ieee_write_byte (info, 3)
4283           || ! ieee_write_number (info, 0)
4284           || ! ieee_write_id (info, info->modname))
4285         return false;
4286     }
4287
4288   indx = ieee_pop_type (info);
4289
4290   /* The address is written out as the first block.  */
4291
4292   ++info->block_depth;
4293
4294   return (ieee_write_byte (info, (int) ieee_bb_record_enum)
4295           && ieee_write_byte (info, global ? 4 : 6)
4296           && ieee_write_number (info, 0)
4297           && ieee_write_id (info, name)
4298           && ieee_write_number (info, 0)
4299           && ieee_write_number (info, indx));
4300 }
4301
4302 /* Add a function parameter.  This will normally be called before the
4303    first block, so we postpone them until we see the block.  */
4304
4305 static boolean
4306 ieee_function_parameter (p, name, kind, val)
4307      PTR p;
4308      const char *name;
4309      enum debug_parm_kind kind;
4310      bfd_vma val;
4311 {
4312   struct ieee_handle *info = (struct ieee_handle *) p;
4313   struct ieee_pending_parm *m, **pm;
4314
4315   assert (info->block_depth == 1);
4316
4317   m = (struct ieee_pending_parm *) xmalloc (sizeof *m);
4318   memset (m, 0, sizeof *m);
4319
4320   m->next = NULL;
4321   m->name = name;
4322   m->type = ieee_pop_type (info);
4323   m->kind = kind;
4324   m->val = val;
4325
4326   for (pm = &info->pending_parms; *pm != NULL; pm = &(*pm)->next)
4327     ;
4328   *pm = m;
4329
4330   return true;  
4331 }
4332
4333 /* Output pending function parameters.  */
4334
4335 static boolean
4336 ieee_output_pending_parms (info)
4337      struct ieee_handle *info;
4338 {
4339   struct ieee_pending_parm *m;
4340
4341   m = info->pending_parms;
4342   while (m != NULL)
4343     {
4344       struct ieee_pending_parm *next;
4345       enum debug_var_kind vkind;
4346
4347       switch (m->kind)
4348         {
4349         default:
4350           abort ();
4351           return false;
4352         case DEBUG_PARM_STACK:
4353         case DEBUG_PARM_REFERENCE:
4354           vkind = DEBUG_LOCAL;
4355           break;
4356         case DEBUG_PARM_REG:
4357         case DEBUG_PARM_REF_REG:
4358           vkind = DEBUG_REGISTER;
4359           break;
4360         }
4361
4362       if (! ieee_push_type (info, m->type, 0, false)
4363           || ! ieee_variable ((PTR) info, m->name, vkind, m->val))
4364         return false;
4365
4366       /* FIXME: We should output a pmisc note here for reference
4367          parameters.  */
4368
4369       next = m->next;
4370       free (m);
4371       m = next;
4372     }
4373   info->pending_parms = NULL;
4374
4375   return true;
4376 }
4377
4378 /* Start a block.  If this is the first block, we output the address
4379    to finish the BB4 or BB6, and then output the function parameters.  */
4380
4381 static boolean
4382 ieee_start_block (p, addr)
4383      PTR p;
4384      bfd_vma addr;
4385 {
4386   struct ieee_handle *info = (struct ieee_handle *) p;
4387
4388   if (! ieee_change_buffer (info, &info->vars))
4389     return false;
4390
4391   if (info->block_depth == 1)
4392     {
4393       if (! ieee_write_number (info, addr)
4394           || ! ieee_output_pending_parms (info))
4395         return false;
4396     }
4397   else
4398     {
4399       if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
4400           || ! ieee_write_byte (info, 6)
4401           || ! ieee_write_byte (info, 0)
4402           || ! ieee_write_id (info, "")
4403           || ! ieee_write_number (info, 0)
4404           || ! ieee_write_number (info, 0)
4405           || ! ieee_write_number (info, addr))
4406         return false;
4407     }
4408
4409   if (! ieee_start_range (info, addr))
4410     return false;
4411
4412   ++info->block_depth;
4413
4414   return true;
4415 }
4416
4417 /* End a block.  */
4418
4419 static boolean
4420 ieee_end_block (p, addr)
4421      PTR p;
4422      bfd_vma addr;
4423 {
4424   struct ieee_handle *info = (struct ieee_handle *) p;
4425
4426   if (! ieee_change_buffer (info, &info->vars)
4427       || ! ieee_write_byte (info, (int) ieee_be_record_enum)
4428       || ! ieee_write_number (info, addr))
4429     return false;
4430
4431   if (! ieee_end_range (info, addr))
4432     return false;
4433
4434   --info->block_depth;
4435
4436   return true;
4437 }
4438
4439 /* End a function.  */
4440
4441 static boolean
4442 ieee_end_function (p)
4443      PTR p;
4444 {
4445   struct ieee_handle *info = (struct ieee_handle *) p;
4446
4447   assert (info->block_depth == 1);
4448
4449   --info->block_depth;
4450
4451   return true;
4452 }
4453
4454 /* Record line number information.  */
4455
4456 static boolean
4457 ieee_lineno (p, filename, lineno, addr)
4458      PTR p;
4459      const char *filename;
4460      unsigned long lineno;
4461      bfd_vma addr;
4462 {
4463   struct ieee_handle *info = (struct ieee_handle *) p;
4464
4465   assert (info->filename != NULL);
4466
4467   /* Make sure we have a line number block.  */
4468   if (info->linenos != NULL)
4469     {
4470       if (! ieee_change_buffer (info, &info->linenos))
4471         return false;
4472     }
4473   else
4474     {
4475       info->lineno_name_indx = info->name_indx;
4476       ++info->name_indx;
4477       if (! ieee_change_buffer (info, &info->linenos)
4478           || ! ieee_write_byte (info, (int) ieee_bb_record_enum)
4479           || ! ieee_write_byte (info, 5)
4480           || ! ieee_write_number (info, 0)
4481           || ! ieee_write_id (info, info->filename)
4482           || ! ieee_write_byte (info, (int) ieee_nn_record)
4483           || ! ieee_write_number (info, info->lineno_name_indx)
4484           || ! ieee_write_id (info, ""))
4485         return false;
4486       info->lineno_filename = info->filename;
4487     }
4488
4489   if (strcmp (filename, info->lineno_filename) != 0)
4490     {
4491       if (strcmp (info->filename, info->lineno_filename) != 0)
4492         {
4493           /* We were not in the main file.  Close the block for the
4494              included file.  */
4495           if (! ieee_write_byte (info, (int) ieee_be_record_enum))
4496             return false;
4497         }
4498       if (strcmp (info->filename, filename) != 0)
4499         {
4500           /* We are not changing to the main file.  Open a block for
4501              the new included file.  */
4502           if (! ieee_write_byte (info, (int) ieee_bb_record_enum)
4503               || ! ieee_write_byte (info, 5)
4504               || ! ieee_write_number (info, 0)
4505               || ! ieee_write_id (info, filename))
4506             return false;
4507         }
4508       info->lineno_filename = filename;
4509     }
4510
4511   return (ieee_write_2bytes (info, (int) ieee_atn_record_enum)
4512           && ieee_write_number (info, info->lineno_name_indx)
4513           && ieee_write_number (info, 0)
4514           && ieee_write_number (info, 7)
4515           && ieee_write_number (info, lineno)
4516           && ieee_write_number (info, 0)
4517           && ieee_write_2bytes (info, (int) ieee_asn_record_enum)
4518           && ieee_write_number (info, info->lineno_name_indx)
4519           && ieee_write_number (info, addr));
4520 }