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