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