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