* COP2 testing changes.
[binutils-gdb.git] / binutils / wrstabs.c
blob985b7009632000809f06ddabb07e3b5c866cb4e0
1 /* wrstabs.c -- Output stabs debugging information
2 Copyright (C) 1996 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian@cygnus.com>.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /* This file contains code which writes out stabs debugging
23 information. */
25 #include <stdio.h>
26 #include <ctype.h>
27 #include <assert.h>
29 #include "bfd.h"
30 #include "bucomm.h"
31 #include "libiberty.h"
32 #include "debug.h"
33 #include "budbg.h"
35 /* Meaningless definition needs by aout64.h. FIXME. */
36 #define BYTES_IN_WORD 4
38 #include "aout/aout64.h"
39 #include "aout/stab_gnu.h"
41 /* The size of a stabs symbol. This presumes 32 bit values. */
43 #define STAB_SYMBOL_SIZE (12)
45 /* An entry in a string hash table. */
47 struct string_hash_entry
49 struct bfd_hash_entry root;
50 /* Index in string table. */
51 long index;
52 /* Size of type if this is a typedef. */
53 unsigned int size;
56 /* A string hash table. */
58 struct string_hash_table
60 struct bfd_hash_table table;
63 /* The type stack. Each element on the stack is a string. */
65 struct stab_type_stack
67 /* The next element on the stack. */
68 struct stab_type_stack *next;
69 /* This element as a string. */
70 char *string;
71 /* The type index of this element. */
72 long index;
73 /* The size of the type. */
74 unsigned int size;
75 /* Whether type string defines a new type. */
76 boolean definition;
77 /* String defining struct fields. */
78 char *fields;
79 /* NULL terminated array of strings defining base classes for a
80 class. */
81 char **baseclasses;
82 /* String defining class methods. */
83 char *methods;
84 /* String defining vtable pointer for a class. */
85 char *vtable;
88 /* This structure is used to keep track of type indices for tagged
89 types. */
91 struct stab_tag
93 /* The type index. */
94 long index;
95 /* The tag name. */
96 const char *tag;
97 /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
98 type is defined. */
99 enum debug_type_kind kind;
100 /* The size of the struct. */
101 unsigned int size;
104 /* We remember various sorts of type indices. They are not related,
105 but, for convenience, we keep all the information in this
106 structure. */
108 struct stab_type_cache
110 /* The void type index. */
111 long void_type;
112 /* Signed integer type indices, indexed by size - 1. */
113 long signed_integer_types[8];
114 /* Unsigned integer type indices, indexed by size - 1. */
115 long unsigned_integer_types[8];
116 /* Floating point types, indexed by size - 1. */
117 long float_types[16];
118 /* Pointers to types, indexed by the type index. */
119 long *pointer_types;
120 size_t pointer_types_alloc;
121 /* Functions returning types, indexed by the type index. */
122 long *function_types;
123 size_t function_types_alloc;
124 /* References to types, indexed by the type index. */
125 long *reference_types;
126 size_t reference_types_alloc;
127 /* Struct/union/class type indices, indexed by the struct id. */
128 struct stab_tag *struct_types;
129 size_t struct_types_alloc;
132 /* This is the handle passed through debug_write. */
134 struct stab_write_handle
136 /* The BFD. */
137 bfd *abfd;
138 /* This buffer holds the symbols. */
139 bfd_byte *symbols;
140 size_t symbols_size;
141 size_t symbols_alloc;
142 /* This buffer holds the strings. */
143 bfd_byte *strings;
144 size_t strings_size;
145 size_t strings_alloc;
146 /* This hash table eliminates duplicate strings. */
147 struct string_hash_table strhash;
148 /* The type stack. */
149 struct stab_type_stack *type_stack;
150 /* The next type index. */
151 long type_index;
152 /* The type cache. */
153 struct stab_type_cache type_cache;
154 /* A mapping from typedef names to type indices. */
155 struct string_hash_table typedef_hash;
156 /* If this is not -1, it is the offset to the most recent N_SO
157 symbol, and the value of that symbol needs to be set. */
158 long so_offset;
159 /* If this is not -1, it is the offset to the most recent N_FUN
160 symbol, and the value of that symbol needs to be set. */
161 long fun_offset;
162 /* The last text section address seen. */
163 bfd_vma last_text_address;
164 /* The block nesting depth. */
165 unsigned int nesting;
166 /* The function address. */
167 bfd_vma fnaddr;
168 /* A pending LBRAC symbol. */
169 bfd_vma pending_lbrac;
170 /* The current line number file name. */
171 const char *lineno_filename;
174 static struct bfd_hash_entry *string_hash_newfunc
175 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
176 static boolean stab_write_symbol
177 PARAMS ((struct stab_write_handle *, int, int, bfd_vma, const char *));
178 static boolean stab_push_string
179 PARAMS ((struct stab_write_handle *, const char *, long, boolean,
180 unsigned int));
181 static boolean stab_push_defined_type
182 PARAMS ((struct stab_write_handle *, long, unsigned int));
183 static char *stab_pop_type PARAMS ((struct stab_write_handle *));
184 static boolean stab_modify_type
185 PARAMS ((struct stab_write_handle *, int, unsigned int, long **, size_t *));
186 static long stab_get_struct_index
187 PARAMS ((struct stab_write_handle *, const char *, unsigned int,
188 enum debug_type_kind, unsigned int *));
189 static boolean stab_class_method_var
190 PARAMS ((struct stab_write_handle *, const char *, enum debug_visibility,
191 boolean, boolean, boolean, bfd_vma, boolean));
193 static boolean stab_start_compilation_unit PARAMS ((PTR, const char *));
194 static boolean stab_start_source PARAMS ((PTR, const char *));
195 static boolean stab_empty_type PARAMS ((PTR));
196 static boolean stab_void_type PARAMS ((PTR));
197 static boolean stab_int_type PARAMS ((PTR, unsigned int, boolean));
198 static boolean stab_float_type PARAMS ((PTR, unsigned int));
199 static boolean stab_complex_type PARAMS ((PTR, unsigned int));
200 static boolean stab_bool_type PARAMS ((PTR, unsigned int));
201 static boolean stab_enum_type
202 PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
203 static boolean stab_pointer_type PARAMS ((PTR));
204 static boolean stab_function_type PARAMS ((PTR, int, boolean));
205 static boolean stab_reference_type PARAMS ((PTR));
206 static boolean stab_range_type PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
207 static boolean stab_array_type
208 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, boolean));
209 static boolean stab_set_type PARAMS ((PTR, boolean));
210 static boolean stab_offset_type PARAMS ((PTR));
211 static boolean stab_method_type PARAMS ((PTR, boolean, int, boolean));
212 static boolean stab_const_type PARAMS ((PTR));
213 static boolean stab_volatile_type PARAMS ((PTR));
214 static boolean stab_start_struct_type
215 PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int));
216 static boolean stab_struct_field
217 PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
218 static boolean stab_end_struct_type PARAMS ((PTR));
219 static boolean stab_start_class_type
220 PARAMS ((PTR, const char *, unsigned int, boolean, unsigned int, boolean,
221 boolean));
222 static boolean stab_class_static_member
223 PARAMS ((PTR, const char *, const char *, enum debug_visibility));
224 static boolean stab_class_baseclass
225 PARAMS ((PTR, bfd_vma, boolean, enum debug_visibility));
226 static boolean stab_class_start_method PARAMS ((PTR, const char *));
227 static boolean stab_class_method_variant
228 PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean,
229 bfd_vma, boolean));
230 static boolean stab_class_static_method_variant
231 PARAMS ((PTR, const char *, enum debug_visibility, boolean, boolean));
232 static boolean stab_class_end_method PARAMS ((PTR));
233 static boolean stab_end_class_type PARAMS ((PTR));
234 static boolean stab_typedef_type PARAMS ((PTR, const char *));
235 static boolean stab_tag_type
236 PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
237 static boolean stab_typdef PARAMS ((PTR, const char *));
238 static boolean stab_tag PARAMS ((PTR, const char *));
239 static boolean stab_int_constant PARAMS ((PTR, const char *, bfd_vma));
240 static boolean stab_float_constant PARAMS ((PTR, const char *, double));
241 static boolean stab_typed_constant PARAMS ((PTR, const char *, bfd_vma));
242 static boolean stab_variable
243 PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
244 static boolean stab_start_function PARAMS ((PTR, const char *, boolean));
245 static boolean stab_function_parameter
246 PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
247 static boolean stab_start_block PARAMS ((PTR, bfd_vma));
248 static boolean stab_end_block PARAMS ((PTR, bfd_vma));
249 static boolean stab_end_function PARAMS ((PTR));
250 static boolean stab_lineno
251 PARAMS ((PTR, const char *, unsigned long, bfd_vma));
253 static const struct debug_write_fns stab_fns =
255 stab_start_compilation_unit,
256 stab_start_source,
257 stab_empty_type,
258 stab_void_type,
259 stab_int_type,
260 stab_float_type,
261 stab_complex_type,
262 stab_bool_type,
263 stab_enum_type,
264 stab_pointer_type,
265 stab_function_type,
266 stab_reference_type,
267 stab_range_type,
268 stab_array_type,
269 stab_set_type,
270 stab_offset_type,
271 stab_method_type,
272 stab_const_type,
273 stab_volatile_type,
274 stab_start_struct_type,
275 stab_struct_field,
276 stab_end_struct_type,
277 stab_start_class_type,
278 stab_class_static_member,
279 stab_class_baseclass,
280 stab_class_start_method,
281 stab_class_method_variant,
282 stab_class_static_method_variant,
283 stab_class_end_method,
284 stab_end_class_type,
285 stab_typedef_type,
286 stab_tag_type,
287 stab_typdef,
288 stab_tag,
289 stab_int_constant,
290 stab_float_constant,
291 stab_typed_constant,
292 stab_variable,
293 stab_start_function,
294 stab_function_parameter,
295 stab_start_block,
296 stab_end_block,
297 stab_end_function,
298 stab_lineno
301 /* Routine to create an entry in a string hash table. */
303 static struct bfd_hash_entry *
304 string_hash_newfunc (entry, table, string)
305 struct bfd_hash_entry *entry;
306 struct bfd_hash_table *table;
307 const char *string;
309 struct string_hash_entry *ret = (struct string_hash_entry *) entry;
311 /* Allocate the structure if it has not already been allocated by a
312 subclass. */
313 if (ret == (struct string_hash_entry *) NULL)
314 ret = ((struct string_hash_entry *)
315 bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
316 if (ret == (struct string_hash_entry *) NULL)
317 return NULL;
319 /* Call the allocation method of the superclass. */
320 ret = ((struct string_hash_entry *)
321 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
323 if (ret)
325 /* Initialize the local fields. */
326 ret->index = -1;
327 ret->size = 0;
330 return (struct bfd_hash_entry *) ret;
333 /* Look up an entry in a string hash table. */
335 #define string_hash_lookup(t, string, create, copy) \
336 ((struct string_hash_entry *) \
337 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
339 /* Add a symbol to the stabs debugging information we are building. */
341 static boolean
342 stab_write_symbol (info, type, desc, value, string)
343 struct stab_write_handle *info;
344 int type;
345 int desc;
346 bfd_vma value;
347 const char *string;
349 bfd_size_type strx;
350 bfd_byte sym[STAB_SYMBOL_SIZE];
352 if (string == NULL)
353 strx = 0;
354 else
356 struct string_hash_entry *h;
358 h = string_hash_lookup (&info->strhash, string, true, false);
359 if (h == NULL)
361 fprintf (stderr, "string_hash_lookup failed: %s\n",
362 bfd_errmsg (bfd_get_error ()));
363 return false;
365 if (h->index != -1)
366 strx = h->index;
367 else
369 size_t len;
371 strx = info->strings_size;
372 h->index = strx;
374 len = strlen (string);
375 while (info->strings_size + len + 1 > info->strings_alloc)
377 info->strings_alloc *= 2;
378 info->strings = (bfd_byte *) xrealloc (info->strings,
379 info->strings_alloc);
381 strcpy (info->strings + info->strings_size, string);
382 info->strings_size += len + 1;
386 /* This presumes 32 bit values. */
387 bfd_put_32 (info->abfd, strx, sym);
388 bfd_put_8 (info->abfd, type, sym + 4);
389 bfd_put_8 (info->abfd, 0, sym + 5);
390 bfd_put_16 (info->abfd, desc, sym + 6);
391 bfd_put_32 (info->abfd, value, sym + 8);
393 if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
395 info->symbols_alloc *= 2;
396 info->symbols = (bfd_byte *) xrealloc (info->symbols,
397 info->symbols_alloc);
400 memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
402 info->symbols_size += STAB_SYMBOL_SIZE;
404 return true;
407 /* Push a string on to the type stack. */
409 static boolean
410 stab_push_string (info, string, index, definition, size)
411 struct stab_write_handle *info;
412 const char *string;
413 long index;
414 boolean definition;
415 unsigned int size;
417 struct stab_type_stack *s;
419 s = (struct stab_type_stack *) xmalloc (sizeof *s);
420 s->string = xstrdup (string);
421 s->index = index;
422 s->definition = definition;
423 s->size = size;
425 s->fields = NULL;
426 s->baseclasses = NULL;
427 s->methods = NULL;
428 s->vtable = NULL;
430 s->next = info->type_stack;
431 info->type_stack = s;
433 return true;
436 /* Push a type index which has already been defined. */
438 static boolean
439 stab_push_defined_type (info, index, size)
440 struct stab_write_handle *info;
441 long index;
442 unsigned int size;
444 char buf[20];
446 sprintf (buf, "%ld", index);
447 return stab_push_string (info, buf, index, false, size);
450 /* Pop a type off the type stack. The caller is responsible for
451 freeing the string. */
453 static char *
454 stab_pop_type (info)
455 struct stab_write_handle *info;
457 struct stab_type_stack *s;
458 char *ret;
460 s = info->type_stack;
461 assert (s != NULL);
463 info->type_stack = s->next;
465 ret = s->string;
467 free (s);
469 return ret;
472 /* The general routine to write out stabs in sections debugging
473 information. This accumulates the stabs symbols and the strings in
474 two obstacks. We can't easily write out the information as we go
475 along, because we need to know the section sizes before we can
476 write out the section contents. ABFD is the BFD and DHANDLE is the
477 handle for the debugging information. This sets *PSYMS to point to
478 the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
479 strings, and *PSTRINGSIZE to the size of the strings. */
481 boolean
482 write_stabs_in_sections_debugging_info (abfd, dhandle, psyms, psymsize,
483 pstrings, pstringsize)
484 bfd *abfd;
485 PTR dhandle;
486 bfd_byte **psyms;
487 bfd_size_type *psymsize;
488 bfd_byte **pstrings;
489 bfd_size_type *pstringsize;
491 struct stab_write_handle info;
493 info.abfd = abfd;
495 info.symbols_size = 0;
496 info.symbols_alloc = 500;
497 info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
499 info.strings_size = 1;
500 info.strings_alloc = 500;
501 info.strings = (bfd_byte *) xmalloc (info.strings_alloc);
502 info.strings[0] = '\0';
504 if (! bfd_hash_table_init (&info.strhash.table, string_hash_newfunc)
505 || ! bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc))
507 fprintf (stderr, "bfd_hash_table_init_failed: %s\n",
508 bfd_errmsg (bfd_get_error ()));
509 return false;
512 info.type_stack = NULL;
513 info.type_index = 1;
514 memset (&info.type_cache, 0, sizeof info.type_cache);
515 info.so_offset = -1;
516 info.fun_offset = -1;
517 info.last_text_address = 0;
518 info.nesting = 0;
519 info.fnaddr = 0;
520 info.pending_lbrac = (bfd_vma) -1;
522 /* The initial symbol holds the string size. */
523 if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
524 return false;
526 /* Output an initial N_SO symbol. */
527 info.so_offset = info.symbols_size;
528 if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
529 return false;
531 if (! debug_write (dhandle, &stab_fns, (PTR) &info))
532 return false;
534 assert (info.pending_lbrac == (bfd_vma) -1);
536 /* Output a trailing N_SO. */
537 if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
538 (const char *) NULL))
539 return false;
541 /* Put the string size in the initial symbol. */
542 bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
544 *psyms = info.symbols;
545 *psymsize = info.symbols_size;
547 *pstrings = info.strings;
548 *pstringsize = info.strings_size;
550 return true;
553 /* Start writing out information for a compilation unit. */
555 static boolean
556 stab_start_compilation_unit (p, filename)
557 PTR p;
558 const char *filename;
560 struct stab_write_handle *info = (struct stab_write_handle *) p;
562 /* We would normally output an N_SO symbol here. However, that
563 would force us to reset all of our type information. I think we
564 will be better off just outputting an N_SOL symbol, and not
565 worrying about splitting information between files. */
567 info->lineno_filename = filename;
569 return stab_write_symbol (info, N_SOL, 0, 0, filename);
572 /* Start writing out information for a particular source file. */
574 static boolean
575 stab_start_source (p, filename)
576 PTR p;
577 const char *filename;
579 struct stab_write_handle *info = (struct stab_write_handle *) p;
581 /* FIXME: The symbol's value is supposed to be the text section
582 address. However, we would have to fill it in later, and gdb
583 doesn't care, so we don't bother with it. */
585 info->lineno_filename = filename;
587 return stab_write_symbol (info, N_SOL, 0, 0, filename);
590 /* Push an empty type. This shouldn't normally happen. We just use a
591 void type. */
593 static boolean
594 stab_empty_type (p)
595 PTR p;
597 struct stab_write_handle *info = (struct stab_write_handle *) p;
599 /* We don't call stab_void_type if the type is not yet defined,
600 because that might screw up the typedef. */
602 if (info->type_cache.void_type != 0)
603 return stab_push_defined_type (info, info->type_cache.void_type, 0);
604 else
606 long index;
607 char buf[40];
609 index = info->type_index;
610 ++info->type_index;
612 sprintf (buf, "%ld=%ld", index, index);
614 return stab_push_string (info, buf, index, false, 0);
618 /* Push a void type. */
620 static boolean
621 stab_void_type (p)
622 PTR p;
624 struct stab_write_handle *info = (struct stab_write_handle *) p;
626 if (info->type_cache.void_type != 0)
627 return stab_push_defined_type (info, info->type_cache.void_type, 0);
628 else
630 long index;
631 char buf[40];
633 index = info->type_index;
634 ++info->type_index;
636 info->type_cache.void_type = index;
638 sprintf (buf, "%ld=%ld", index, index);
640 return stab_push_string (info, buf, index, true, 0);
644 /* Push an integer type. */
646 static boolean
647 stab_int_type (p, size, unsignedp)
648 PTR p;
649 unsigned int size;
650 boolean unsignedp;
652 struct stab_write_handle *info = (struct stab_write_handle *) p;
653 long *cache;
655 if (size <= 0 || (size > sizeof (long) && size != 8))
657 fprintf (stderr, "stab_int_type: bad size %u\n", size);
658 return false;
661 if (unsignedp)
662 cache = info->type_cache.signed_integer_types;
663 else
664 cache = info->type_cache.unsigned_integer_types;
666 if (cache[size - 1] != 0)
667 return stab_push_defined_type (info, cache[size - 1], size);
668 else
670 long index;
671 char buf[100];
673 index = info->type_index;
674 ++info->type_index;
676 cache[size - 1] = index;
678 sprintf (buf, "%ld=r%ld;", index, index);
679 if (unsignedp)
681 strcat (buf, "0;");
682 if (size < sizeof (long))
683 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
684 else if (size == sizeof (long))
685 strcat (buf, "-1;");
686 else if (size == 8)
687 strcat (buf, "01777777777777777777777;");
688 else
689 abort ();
691 else
693 if (size <= sizeof (long))
694 sprintf (buf + strlen (buf), "%ld;%ld;",
695 (long) - ((unsigned long) 1 << (size * 8 - 1)),
696 (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
697 else if (size == 8)
698 strcat (buf, "01000000000000000000000;0777777777777777777777;");
699 else
700 abort ();
703 return stab_push_string (info, buf, index, true, size);
707 /* Push a floating point type. */
709 static boolean
710 stab_float_type (p, size)
711 PTR p;
712 unsigned int size;
714 struct stab_write_handle *info = (struct stab_write_handle *) p;
716 if (size > 0
717 && size - 1 < (sizeof info->type_cache.float_types
718 / sizeof info->type_cache.float_types[0])
719 && info->type_cache.float_types[size - 1] != 0)
720 return stab_push_defined_type (info,
721 info->type_cache.float_types[size - 1],
722 size);
723 else
725 long index;
726 char *int_type;
727 char buf[50];
729 /* Floats are defined as a subrange of int. */
730 if (! stab_int_type (info, 4, false))
731 return false;
732 int_type = stab_pop_type (info);
734 index = info->type_index;
735 ++info->type_index;
737 if (size > 0
738 && size - 1 < (sizeof info->type_cache.float_types
739 / sizeof info->type_cache.float_types[0]))
740 info->type_cache.float_types[size - 1] = index;
742 sprintf (buf, "%ld=r%s;%u;0;", index, int_type, size);
744 free (int_type);
746 return stab_push_string (info, buf, index, true, size);
750 /* Push a complex type. */
752 static boolean
753 stab_complex_type (p, size)
754 PTR p;
755 unsigned int size;
757 struct stab_write_handle *info = (struct stab_write_handle *) p;
758 char buf[50];
759 long index;
761 index = info->type_index;
762 ++info->type_index;
764 sprintf (buf, "%ld=r%ld;%u;0;", index, index, size);
766 return stab_push_string (info, buf, index, true, size * 2);
769 /* Push a boolean type. We use an XCOFF predefined type, since gdb
770 always recognizes them. */
772 static boolean
773 stab_bool_type (p, size)
774 PTR p;
775 unsigned int size;
777 struct stab_write_handle *info = (struct stab_write_handle *) p;
778 long index;
780 switch (size)
782 case 1:
783 index = -21;
784 break;
786 case 2:
787 index = -22;
788 break;
790 default:
791 case 4:
792 index = -16;
793 break;
795 case 8:
796 index = -33;
797 break;
800 return stab_push_defined_type (info, index, size);
803 /* Push an enum type. */
805 static boolean
806 stab_enum_type (p, tag, names, vals)
807 PTR p;
808 const char *tag;
809 const char **names;
810 bfd_signed_vma *vals;
812 struct stab_write_handle *info = (struct stab_write_handle *) p;
813 size_t len;
814 const char **pn;
815 char *buf;
816 long index = 0;
817 bfd_signed_vma *pv;
819 if (names == NULL)
821 assert (tag != NULL);
823 buf = (char *) xmalloc (10 + strlen (tag));
824 sprintf (buf, "xe%s:", tag);
825 /* FIXME: The size is just a guess. */
826 if (! stab_push_string (info, buf, 0, false, 4))
827 return false;
828 free (buf);
829 return true;
832 len = 10;
833 if (tag != NULL)
834 len += strlen (tag);
835 for (pn = names; *pn != NULL; pn++)
836 len += strlen (*pn) + 20;
838 if (tag == NULL)
839 strcpy (buf, "e");
840 else
842 index = info->type_index;
843 ++info->type_index;
844 sprintf (buf, "%s:T%ld=e", tag, index);
847 buf = (char *) xmalloc (len);
848 for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
849 sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
850 strcat (buf, ";");
852 if (tag == NULL)
854 /* FIXME: The size is just a guess. */
855 if (! stab_push_string (info, buf, 0, false, 4))
856 return false;
858 else
860 /* FIXME: The size is just a guess. */
861 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
862 || ! stab_push_defined_type (info, index, 4))
863 return false;
866 free (buf);
868 return true;
871 /* Push a modification of the top type on the stack. Cache the
872 results in CACHE and CACHE_ALLOC. */
874 static boolean
875 stab_modify_type (info, mod, size, cache, cache_alloc)
876 struct stab_write_handle *info;
877 int mod;
878 unsigned int size;
879 long **cache;
880 size_t *cache_alloc;
882 long targindex;
883 long index;
884 char *s, *buf;
886 assert (info->type_stack != NULL);
887 targindex = info->type_stack->index;
889 if (targindex <= 0
890 || cache == NULL)
892 boolean definition;
894 /* Either the target type has no index, or we aren't caching
895 this modifier. Either way we have no way of recording the
896 new type, so we don't bother to define one. */
897 definition = info->type_stack->definition;
898 s = stab_pop_type (info);
899 buf = (char *) xmalloc (strlen (s) + 2);
900 sprintf (buf, "%c%s", mod, s);
901 free (s);
902 if (! stab_push_string (info, buf, 0, definition, size))
903 return false;
904 free (buf);
906 else
908 if (targindex >= *cache_alloc)
910 size_t alloc;
912 alloc = *cache_alloc;
913 if (alloc == 0)
914 alloc = 10;
915 while (targindex >= alloc)
916 alloc *= 2;
917 *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
918 memset (*cache + *cache_alloc, 0,
919 (alloc - *cache_alloc) * sizeof (long));
920 *cache_alloc = alloc;
923 index = (*cache)[targindex];
924 if (index != 0)
926 /* If we have already defined a modification of this type,
927 then the entry on the type stack can not be a definition,
928 so we can safely discard it. */
929 assert (! info->type_stack->definition);
930 free (stab_pop_type (info));
931 if (! stab_push_defined_type (info, index, size))
932 return false;
934 else
936 index = info->type_index;
937 ++info->type_index;
939 s = stab_pop_type (info);
940 buf = (char *) xmalloc (strlen (s) + 20);
941 sprintf (buf, "%ld=%c%s", index, mod, s);
942 free (s);
944 (*cache)[targindex] = index;
946 if (! stab_push_string (info, buf, index, true, size))
947 return false;
949 free (buf);
953 return true;
956 /* Push a pointer type. */
958 static boolean
959 stab_pointer_type (p)
960 PTR p;
962 struct stab_write_handle *info = (struct stab_write_handle *) p;
964 /* FIXME: The size should depend upon the architecture. */
965 return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
966 &info->type_cache.pointer_types_alloc);
969 /* Push a function type. */
971 static boolean
972 stab_function_type (p, argcount, varargs)
973 PTR p;
974 int argcount;
975 boolean varargs;
977 struct stab_write_handle *info = (struct stab_write_handle *) p;
978 int i;
980 /* We have no way to represent the argument types, so we just
981 discard them. However, if they define new types, we must output
982 them. We do this by producing meaningless typedefs. */
983 for (i = 0; i < argcount; i++)
985 if (! info->type_stack->definition)
986 free (stab_pop_type (info));
987 else
989 char *s, *buf;
990 long index;
992 s = stab_pop_type (info);
994 buf = (char *) xmalloc (strlen (s) + 40);
995 index = info->type_index;
996 ++info->type_index;
997 sprintf (buf, "__fake_type_%ld:t%s", index, s);
999 free (s);
1001 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1002 return false;
1004 free (buf);
1008 return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
1009 &info->type_cache.function_types_alloc);
1012 /* Push a reference type. */
1014 static boolean
1015 stab_reference_type (p)
1016 PTR p;
1018 struct stab_write_handle *info = (struct stab_write_handle *) p;
1020 /* FIXME: The size should depend upon the architecture. */
1021 return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
1022 &info->type_cache.reference_types_alloc);
1025 /* Push a range type. */
1027 static boolean
1028 stab_range_type (p, low, high)
1029 PTR p;
1030 bfd_signed_vma low;
1031 bfd_signed_vma high;
1033 struct stab_write_handle *info = (struct stab_write_handle *) p;
1034 boolean definition;
1035 unsigned int size;
1036 char *s, *buf;
1038 definition = info->type_stack->definition;
1039 size = info->type_stack->size;
1041 s = stab_pop_type (info);
1042 buf = (char *) xmalloc (strlen (s) + 100);
1043 sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1044 free (s);
1046 if (! stab_push_string (info, buf, 0, definition, size))
1047 return false;
1049 free (buf);
1051 return true;
1054 /* Push an array type. */
1056 static boolean
1057 stab_array_type (p, low, high, stringp)
1058 PTR p;
1059 bfd_signed_vma low;
1060 bfd_signed_vma high;
1061 boolean stringp;
1063 struct stab_write_handle *info = (struct stab_write_handle *) p;
1064 boolean definition;
1065 unsigned int element_size;
1066 char *range, *element, *buf;
1067 long index;
1068 unsigned int size;
1070 definition = info->type_stack->definition;
1071 range = stab_pop_type (info);
1073 definition = definition || info->type_stack->definition;
1074 element_size = info->type_stack->size;
1075 element = stab_pop_type (info);
1077 buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1079 if (! stringp)
1081 index = 0;
1082 *buf = '\0';
1084 else
1086 /* We need to define a type in order to include the string
1087 attribute. */
1088 index = info->type_index;
1089 ++info->type_index;
1090 definition = true;
1091 sprintf (buf, "%ld=@S;", index);
1094 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s", range, low, high, element);
1095 free (range);
1096 free (element);
1098 if (high <= low)
1099 size = 0;
1100 else
1101 size = element_size * ((high - low) + 1);
1102 if (! stab_push_string (info, buf, index, definition, size))
1103 return false;
1105 free (buf);
1107 return true;
1110 /* Push a set type. */
1112 static boolean
1113 stab_set_type (p, bitstringp)
1114 PTR p;
1115 boolean bitstringp;
1117 struct stab_write_handle *info = (struct stab_write_handle *) p;
1118 boolean definition;
1119 char *s, *buf;
1120 long index;
1122 definition = info->type_stack->definition;
1124 s = stab_pop_type (info);
1125 buf = (char *) xmalloc (strlen (s) + 30);
1127 if (! bitstringp)
1129 *buf = '\0';
1130 index = 0;
1132 else
1134 /* We need to define a type in order to include the string
1135 attribute. */
1136 index = info->type_index;
1137 ++info->type_index;
1138 definition = true;
1139 sprintf (buf, "%ld=@S;", index);
1142 sprintf (buf + strlen (buf), "S%s", s);
1143 free (s);
1145 if (! stab_push_string (info, buf, index, definition, 0))
1146 return false;
1148 free (buf);
1150 return true;
1153 /* Push an offset type. */
1155 static boolean
1156 stab_offset_type (p)
1157 PTR p;
1159 struct stab_write_handle *info = (struct stab_write_handle *) p;
1160 boolean definition;
1161 char *target, *base, *buf;
1163 definition = info->type_stack->definition;
1164 target = stab_pop_type (info);
1166 definition = definition || info->type_stack->definition;
1167 base = stab_pop_type (info);
1169 buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1170 sprintf (buf, "@%s,%s", base, target);
1171 free (base);
1172 free (target);
1174 if (! stab_push_string (info, buf, 0, definition, 0))
1175 return false;
1177 free (buf);
1179 return true;
1182 /* Push a method type. */
1184 static boolean
1185 stab_method_type (p, domainp, argcount, varargs)
1186 PTR p;
1187 boolean domainp;
1188 int argcount;
1189 boolean varargs;
1191 struct stab_write_handle *info = (struct stab_write_handle *) p;
1192 boolean definition;
1193 char *domain, *return_type, *buf;
1194 char **args;
1195 int i;
1196 size_t len;
1198 /* We don't bother with stub method types, because that would
1199 require a mangler for C++ argument types. This will waste space
1200 in the debugging output. */
1202 /* We need a domain. I'm not sure DOMAINP can ever be false,
1203 anyhow. */
1204 if (! domainp)
1206 if (! stab_empty_type (p))
1207 return false;
1210 definition = info->type_stack->definition;
1211 domain = stab_pop_type (info);
1213 /* A non-varargs function is indicated by making the last parameter
1214 type be void. */
1216 if (argcount < 0)
1218 args = NULL;
1219 argcount = 0;
1221 else if (argcount == 0)
1223 if (varargs)
1224 args = NULL;
1225 else
1227 args = (char **) xmalloc (1 * sizeof (*args));
1228 if (! stab_empty_type (p))
1229 return false;
1230 definition = definition || info->type_stack->definition;
1231 args[0] = stab_pop_type (info);
1232 argcount = 1;
1235 else
1237 args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1238 for (i = argcount - 1; i >= 0; i--)
1240 definition = definition || info->type_stack->definition;
1241 args[i] = stab_pop_type (info);
1243 if (! varargs)
1245 if (! stab_empty_type (p))
1246 return false;
1247 definition = definition || info->type_stack->definition;
1248 args[argcount] = stab_pop_type (info);
1249 ++argcount;
1253 definition = definition || info->type_stack->definition;
1254 return_type = stab_pop_type (info);
1256 len = strlen (domain) + strlen (return_type) + 10;
1257 for (i = 0; i < argcount; i++)
1258 len += strlen (args[i]);
1260 buf = (char *) xmalloc (len);
1262 sprintf (buf, "#%s,%s", domain, return_type);
1263 free (domain);
1264 free (return_type);
1265 for (i = 0; i < argcount; i++)
1267 strcat (buf, ",");
1268 strcat (buf, args[i]);
1269 free (args[i]);
1271 strcat (buf, ";");
1273 if (args != NULL)
1274 free (args);
1276 if (! stab_push_string (info, buf, 0, definition, 0))
1277 return false;
1279 free (buf);
1281 return true;
1284 /* Push a const version of a type. */
1286 static boolean
1287 stab_const_type (p)
1288 PTR p;
1290 struct stab_write_handle *info = (struct stab_write_handle *) p;
1292 return stab_modify_type (info, 'k', info->type_stack->size,
1293 (long **) NULL, (size_t *) NULL);
1296 /* Push a volatile version of a type. */
1298 static boolean
1299 stab_volatile_type (p)
1300 PTR p;
1302 struct stab_write_handle *info = (struct stab_write_handle *) p;
1304 return stab_modify_type (info, 'B', info->type_stack->size,
1305 (long **) NULL, (size_t *) NULL);
1308 /* Get the type index to use for a struct/union/class ID. This should
1309 return -1 if it fails. */
1311 static long
1312 stab_get_struct_index (info, tag, id, kind, psize)
1313 struct stab_write_handle *info;
1314 const char *tag;
1315 unsigned int id;
1316 enum debug_type_kind kind;
1317 unsigned int *psize;
1319 if (id >= info->type_cache.struct_types_alloc)
1321 size_t alloc;
1323 alloc = info->type_cache.struct_types_alloc;
1324 if (alloc == 0)
1325 alloc = 10;
1326 while (id >= alloc)
1327 alloc *= 2;
1328 info->type_cache.struct_types =
1329 (struct stab_tag *) xrealloc (info->type_cache.struct_types,
1330 alloc * sizeof (struct stab_tag));
1331 memset ((info->type_cache.struct_types
1332 + info->type_cache.struct_types_alloc),
1334 ((alloc - info->type_cache.struct_types_alloc)
1335 * sizeof (struct stab_tag)));
1336 info->type_cache.struct_types_alloc = alloc;
1339 if (info->type_cache.struct_types[id].index == 0)
1341 info->type_cache.struct_types[id].index = info->type_index;
1342 ++info->type_index;
1343 info->type_cache.struct_types[id].tag = tag;
1344 info->type_cache.struct_types[id].kind = kind;
1347 if (kind == DEBUG_KIND_ILLEGAL)
1349 /* This is a definition of the struct. */
1350 info->type_cache.struct_types[id].kind = kind;
1351 info->type_cache.struct_types[id].size = *psize;
1353 else
1354 *psize = info->type_cache.struct_types[id].size;
1356 return info->type_cache.struct_types[id].index;
1359 /* Start outputting a struct. We ignore the tag, and handle it in
1360 stab_tag. */
1362 /*ARGSUSED*/
1363 static boolean
1364 stab_start_struct_type (p, tag, id, structp, size)
1365 PTR p;
1366 const char *tag;
1367 unsigned int id;
1368 boolean structp;
1369 unsigned int size;
1371 struct stab_write_handle *info = (struct stab_write_handle *) p;
1372 long index;
1373 boolean definition;
1374 char *buf;
1376 buf = (char *) xmalloc (40);
1378 if (id == 0)
1380 index = 0;
1381 *buf = '\0';
1382 definition = false;
1384 else
1386 index = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1387 &size);
1388 if (index < 0)
1389 return false;
1390 sprintf (buf, "%ld=", index);
1391 definition = true;
1394 sprintf (buf + strlen (buf), "%c%u",
1395 structp ? 's' : 'u',
1396 size);
1398 if (! stab_push_string (info, buf, index, definition, size))
1399 return false;
1401 info->type_stack->fields = (char *) xmalloc (1);
1402 info->type_stack->fields[0] = '\0';
1404 return true;
1407 /* Add a field to a struct. */
1409 static boolean
1410 stab_struct_field (p, name, bitpos, bitsize, visibility)
1411 PTR p;
1412 const char *name;
1413 bfd_vma bitpos;
1414 bfd_vma bitsize;
1415 enum debug_visibility visibility;
1417 struct stab_write_handle *info = (struct stab_write_handle *) p;
1418 boolean definition;
1419 unsigned int size;
1420 char *s, *n;
1421 const char *vis;
1423 definition = info->type_stack->definition;
1424 size = info->type_stack->size;
1425 s = stab_pop_type (info);
1427 /* Add this field to the end of the current struct fields, which is
1428 currently on the top of the stack. */
1430 assert (info->type_stack->fields != NULL);
1431 n = (char *) xmalloc (strlen (info->type_stack->fields)
1432 + strlen (name)
1433 + strlen (s)
1434 + 50);
1436 switch (visibility)
1438 default:
1439 abort ();
1441 case DEBUG_VISIBILITY_PUBLIC:
1442 vis = "";
1443 break;
1445 case DEBUG_VISIBILITY_PRIVATE:
1446 vis = "/0";
1447 break;
1449 case DEBUG_VISIBILITY_PROTECTED:
1450 vis = "/1";
1451 break;
1454 if (bitsize == 0)
1456 bitsize = size * 8;
1457 if (bitsize == 0)
1458 fprintf (stderr,
1459 "%s: warning: unknown size for field `%s' in struct\n",
1460 bfd_get_filename (info->abfd), name);
1463 sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1464 (long) bitpos, (long) bitsize);
1466 free (info->type_stack->fields);
1467 info->type_stack->fields = n;
1469 if (definition)
1470 info->type_stack->definition = true;
1472 return true;
1475 /* Finish up a struct. */
1477 static boolean
1478 stab_end_struct_type (p)
1479 PTR p;
1481 struct stab_write_handle *info = (struct stab_write_handle *) p;
1482 boolean definition;
1483 long index;
1484 unsigned int size;
1485 char *fields, *first, *buf;
1487 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1489 definition = info->type_stack->definition;
1490 index = info->type_stack->index;
1491 size = info->type_stack->size;
1492 fields = info->type_stack->fields;
1493 first = stab_pop_type (info);
1495 buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1496 sprintf (buf, "%s%s;", first, fields);
1497 free (first);
1498 free (fields);
1500 if (! stab_push_string (info, buf, index, definition, size))
1501 return false;
1503 free (buf);
1505 return true;
1508 /* Start outputting a class. */
1510 static boolean
1511 stab_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1512 PTR p;
1513 const char *tag;
1514 unsigned int id;
1515 boolean structp;
1516 unsigned int size;
1517 boolean vptr;
1518 boolean ownvptr;
1520 struct stab_write_handle *info = (struct stab_write_handle *) p;
1521 boolean definition;
1522 char *vstring;
1524 if (! vptr || ownvptr)
1526 definition = false;
1527 vstring = NULL;
1529 else
1531 definition = info->type_stack->definition;
1532 vstring = stab_pop_type (info);
1535 if (! stab_start_struct_type (p, tag, id, structp, size))
1536 return false;
1538 if (vptr)
1540 char *vtable;
1542 if (ownvptr)
1544 assert (info->type_stack->index > 0);
1545 vtable = (char *) xmalloc (20);
1546 sprintf (vtable, "~%%%ld", info->type_stack->index);
1548 else
1550 vtable = (char *) xmalloc (strlen (vstring) + 3);
1551 sprintf (vtable, "~%%%s", vstring);
1552 free (vstring);
1555 info->type_stack->vtable = vtable;
1558 if (definition)
1559 info->type_stack->definition = true;
1561 return true;
1564 /* Add a static member to the class on the type stack. */
1566 static boolean
1567 stab_class_static_member (p, name, physname, visibility)
1568 PTR p;
1569 const char *name;
1570 const char *physname;
1571 enum debug_visibility visibility;
1573 struct stab_write_handle *info = (struct stab_write_handle *) p;
1574 boolean definition;
1575 char *s, *n;
1576 const char *vis;
1578 definition = info->type_stack->definition;
1579 s = stab_pop_type (info);
1581 /* Add this field to the end of the current struct fields, which is
1582 currently on the top of the stack. */
1584 assert (info->type_stack->fields != NULL);
1585 n = (char *) xmalloc (strlen (info->type_stack->fields)
1586 + strlen (name)
1587 + strlen (s)
1588 + strlen (physname)
1589 + 10);
1591 switch (visibility)
1593 default:
1594 abort ();
1596 case DEBUG_VISIBILITY_PUBLIC:
1597 vis = "";
1598 break;
1600 case DEBUG_VISIBILITY_PRIVATE:
1601 vis = "/0";
1602 break;
1604 case DEBUG_VISIBILITY_PROTECTED:
1605 vis = "/1";
1606 break;
1609 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1610 physname);
1612 free (info->type_stack->fields);
1613 info->type_stack->fields = n;
1615 if (definition)
1616 info->type_stack->definition = true;
1618 return true;
1621 /* Add a base class to the class on the type stack. */
1623 static boolean
1624 stab_class_baseclass (p, bitpos, virtual, visibility)
1625 PTR p;
1626 bfd_vma bitpos;
1627 boolean virtual;
1628 enum debug_visibility visibility;
1630 struct stab_write_handle *info = (struct stab_write_handle *) p;
1631 boolean definition;
1632 char *s;
1633 char *buf;
1634 unsigned int c;
1635 char **baseclasses;
1637 definition = info->type_stack->definition;
1638 s = stab_pop_type (info);
1640 /* Build the base class specifier. */
1642 buf = (char *) xmalloc (strlen (s) + 25);
1643 buf[0] = virtual ? '1' : '0';
1644 switch (visibility)
1646 default:
1647 abort ();
1649 case DEBUG_VISIBILITY_PRIVATE:
1650 buf[1] = '0';
1651 break;
1653 case DEBUG_VISIBILITY_PROTECTED:
1654 buf[1] = '1';
1655 break;
1657 case DEBUG_VISIBILITY_PUBLIC:
1658 buf[1] = '2';
1659 break;
1662 sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1663 free (s);
1665 /* Add the new baseclass to the existing ones. */
1667 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1669 if (info->type_stack->baseclasses == NULL)
1670 c = 0;
1671 else
1673 c = 0;
1674 while (info->type_stack->baseclasses[c] != NULL)
1675 ++c;
1678 baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1679 (c + 2) * sizeof (*baseclasses));
1680 baseclasses[c] = buf;
1681 baseclasses[c + 1] = NULL;
1683 info->type_stack->baseclasses = baseclasses;
1685 if (definition)
1686 info->type_stack->definition = true;
1688 return true;
1691 /* Start adding a method to the class on the type stack. */
1693 static boolean
1694 stab_class_start_method (p, name)
1695 PTR p;
1696 const char *name;
1698 struct stab_write_handle *info = (struct stab_write_handle *) p;
1699 char *m;
1701 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1703 if (info->type_stack->methods == NULL)
1705 m = (char *) xmalloc (strlen (name) + 3);
1706 *m = '\0';
1708 else
1710 m = (char *) xrealloc (info->type_stack->methods,
1711 (strlen (info->type_stack->methods)
1712 + strlen (name)
1713 + 4));
1716 sprintf (m + strlen (m), "%s::", name);
1718 info->type_stack->methods = m;
1720 return true;
1723 /* Add a variant, either static or not, to the current method. */
1725 static boolean
1726 stab_class_method_var (info, physname, visibility, staticp, constp, volatilep,
1727 voffset, contextp)
1728 struct stab_write_handle *info;
1729 const char *physname;
1730 enum debug_visibility visibility;
1731 boolean staticp;
1732 boolean constp;
1733 boolean volatilep;
1734 bfd_vma voffset;
1735 boolean contextp;
1737 boolean definition;
1738 char *type;
1739 char *context = NULL;
1740 char visc, qualc, typec;
1742 definition = info->type_stack->definition;
1743 type = stab_pop_type (info);
1745 if (contextp)
1747 definition = definition || info->type_stack->definition;
1748 context = stab_pop_type (info);
1751 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1753 switch (visibility)
1755 default:
1756 abort ();
1758 case DEBUG_VISIBILITY_PRIVATE:
1759 visc = '0';
1760 break;
1762 case DEBUG_VISIBILITY_PROTECTED:
1763 visc = '1';
1764 break;
1766 case DEBUG_VISIBILITY_PUBLIC:
1767 visc = '2';
1768 break;
1771 if (constp)
1773 if (volatilep)
1774 qualc = 'D';
1775 else
1776 qualc = 'B';
1778 else
1780 if (volatilep)
1781 qualc = 'C';
1782 else
1783 qualc = 'A';
1786 if (staticp)
1787 typec = '?';
1788 else if (! contextp)
1789 typec = '.';
1790 else
1791 typec = '*';
1793 info->type_stack->methods =
1794 (char *) xrealloc (info->type_stack->methods,
1795 (strlen (info->type_stack->methods)
1796 + strlen (type)
1797 + strlen (physname)
1798 + (contextp ? strlen (context) : 0)
1799 + 40));
1801 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1802 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1803 free (type);
1805 if (contextp)
1807 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1808 "%ld;%s;", (long) voffset, context);
1809 free (context);
1812 if (definition)
1813 info->type_stack->definition = true;
1815 return true;
1818 /* Add a variant to the current method. */
1820 static boolean
1821 stab_class_method_variant (p, physname, visibility, constp, volatilep,
1822 voffset, contextp)
1823 PTR p;
1824 const char *physname;
1825 enum debug_visibility visibility;
1826 boolean constp;
1827 boolean volatilep;
1828 bfd_vma voffset;
1829 boolean contextp;
1831 struct stab_write_handle *info = (struct stab_write_handle *) p;
1833 return stab_class_method_var (info, physname, visibility, false, constp,
1834 volatilep, voffset, contextp);
1837 /* Add a static variant to the current method. */
1839 static boolean
1840 stab_class_static_method_variant (p, physname, visibility, constp, volatilep)
1841 PTR p;
1842 const char *physname;
1843 enum debug_visibility visibility;
1844 boolean constp;
1845 boolean volatilep;
1847 struct stab_write_handle *info = (struct stab_write_handle *) p;
1849 return stab_class_method_var (info, physname, visibility, true, constp,
1850 volatilep, 0, false);
1853 /* Finish up a method. */
1855 static boolean
1856 stab_class_end_method (p)
1857 PTR p;
1859 struct stab_write_handle *info = (struct stab_write_handle *) p;
1861 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1863 /* We allocated enough room on info->type_stack->methods to add the
1864 trailing semicolon. */
1865 strcat (info->type_stack->methods, ";");
1867 return true;
1870 /* Finish up a class. */
1872 static boolean
1873 stab_end_class_type (p)
1874 PTR p;
1876 struct stab_write_handle *info = (struct stab_write_handle *) p;
1877 size_t len;
1878 unsigned int i;
1879 char *buf;
1881 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1883 /* Work out the size we need to allocate for the class definition. */
1885 len = (strlen (info->type_stack->string)
1886 + strlen (info->type_stack->fields)
1887 + 10);
1888 if (info->type_stack->baseclasses != NULL)
1890 len += 20;
1891 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1892 len += strlen (info->type_stack->baseclasses[i]);
1894 if (info->type_stack->methods != NULL)
1895 len += strlen (info->type_stack->methods);
1896 if (info->type_stack->vtable != NULL)
1897 len += strlen (info->type_stack->vtable);
1899 /* Build the class definition. */
1901 buf = (char *) xmalloc (len);
1903 strcpy (buf, info->type_stack->string);
1905 if (info->type_stack->baseclasses != NULL)
1907 sprintf (buf + strlen (buf), "!%u,", i);
1908 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1910 strcat (buf, info->type_stack->baseclasses[i]);
1911 free (info->type_stack->baseclasses[i]);
1913 free (info->type_stack->baseclasses);
1914 info->type_stack->baseclasses = NULL;
1917 strcat (buf, info->type_stack->fields);
1918 free (info->type_stack->fields);
1919 info->type_stack->fields = NULL;
1921 if (info->type_stack->methods != NULL)
1923 strcat (buf, info->type_stack->methods);
1924 free (info->type_stack->methods);
1925 info->type_stack->methods = NULL;
1928 strcat (buf, ";");
1930 if (info->type_stack->vtable != NULL)
1932 strcat (buf, info->type_stack->vtable);
1933 free (info->type_stack->vtable);
1934 info->type_stack->vtable = NULL;
1937 /* Replace the string on the top of the stack with the complete
1938 class definition. */
1939 free (info->type_stack->string);
1940 info->type_stack->string = buf;
1942 return true;
1945 /* Push a typedef which was previously defined. */
1947 static boolean
1948 stab_typedef_type (p, name)
1949 PTR p;
1950 const char *name;
1952 struct stab_write_handle *info = (struct stab_write_handle *) p;
1953 struct string_hash_entry *h;
1955 h = string_hash_lookup (&info->typedef_hash, name, false, false);
1956 assert (h != NULL && h->index > 0);
1958 return stab_push_defined_type (info, h->index, h->size);
1961 /* Push a struct, union or class tag. */
1963 static boolean
1964 stab_tag_type (p, name, id, kind)
1965 PTR p;
1966 const char *name;
1967 unsigned int id;
1968 enum debug_type_kind kind;
1970 struct stab_write_handle *info = (struct stab_write_handle *) p;
1971 long index;
1972 unsigned int size;
1974 index = stab_get_struct_index (info, name, id, kind, &size);
1975 if (index < 0)
1976 return false;
1978 return stab_push_defined_type (info, index, size);
1981 /* Define a typedef. */
1983 static boolean
1984 stab_typdef (p, name)
1985 PTR p;
1986 const char *name;
1988 struct stab_write_handle *info = (struct stab_write_handle *) p;
1989 long index;
1990 unsigned int size;
1991 char *s, *buf;
1992 struct string_hash_entry *h;
1994 index = info->type_stack->index;
1995 size = info->type_stack->size;
1996 s = stab_pop_type (info);
1998 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2000 if (index > 0)
2001 sprintf (buf, "%s:t%s", name, s);
2002 else
2004 index = info->type_index;
2005 ++info->type_index;
2006 sprintf (buf, "%s:t%ld=%s", name, index, s);
2009 free (s);
2011 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2012 return false;
2014 free (buf);
2016 h = string_hash_lookup (&info->typedef_hash, name, true, false);
2017 if (h == NULL)
2019 fprintf (stderr, "string_hash_lookup failed: %s\n",
2020 bfd_errmsg (bfd_get_error ()));
2021 return false;
2024 /* I don't think we care about redefinitions. */
2026 h->index = index;
2027 h->size = size;
2029 return true;
2032 /* Define a tag. */
2034 static boolean
2035 stab_tag (p, tag)
2036 PTR p;
2037 const char *tag;
2039 struct stab_write_handle *info = (struct stab_write_handle *) p;
2040 char *s, *buf;
2042 s = stab_pop_type (info);
2044 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
2046 sprintf (buf, "%s:T%s", tag, s);
2047 free (s);
2049 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2050 return false;
2052 free (buf);
2054 return true;
2057 /* Define an integer constant. */
2059 static boolean
2060 stab_int_constant (p, name, val)
2061 PTR p;
2062 const char *name;
2063 bfd_vma val;
2065 struct stab_write_handle *info = (struct stab_write_handle *) p;
2066 char *buf;
2068 buf = (char *) xmalloc (strlen (name) + 20);
2069 sprintf (buf, "%s:c=i%ld", name, (long) val);
2071 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2072 return false;
2074 free (buf);
2076 return true;
2079 /* Define a floating point constant. */
2081 static boolean
2082 stab_float_constant (p, name, val)
2083 PTR p;
2084 const char *name;
2085 double val;
2087 struct stab_write_handle *info = (struct stab_write_handle *) p;
2088 char *buf;
2090 buf = (char *) xmalloc (strlen (name) + 20);
2091 sprintf (buf, "%s:c=f%g", name, val);
2093 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2094 return false;
2096 free (buf);
2098 return true;
2101 /* Define a typed constant. */
2103 static boolean
2104 stab_typed_constant (p, name, val)
2105 PTR p;
2106 const char *name;
2107 bfd_vma val;
2109 struct stab_write_handle *info = (struct stab_write_handle *) p;
2110 char *s, *buf;
2112 s = stab_pop_type (info);
2114 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2115 sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
2116 free (s);
2118 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2119 return false;
2121 free (buf);
2123 return true;
2126 /* Record a variable. */
2128 static boolean
2129 stab_variable (p, name, kind, val)
2130 PTR p;
2131 const char *name;
2132 enum debug_var_kind kind;
2133 bfd_vma val;
2135 struct stab_write_handle *info = (struct stab_write_handle *) p;
2136 char *s, *buf;
2137 int stab_type;
2138 const char *kindstr;
2140 s = stab_pop_type (info);
2142 switch (kind)
2144 default:
2145 abort ();
2147 case DEBUG_GLOBAL:
2148 stab_type = N_GSYM;
2149 kindstr = "G";
2150 break;
2152 case DEBUG_STATIC:
2153 stab_type = N_STSYM;
2154 kindstr = "S";
2155 break;
2157 case DEBUG_LOCAL_STATIC:
2158 stab_type = N_STSYM;
2159 kindstr = "V";
2160 break;
2162 case DEBUG_LOCAL:
2163 stab_type = N_LSYM;
2164 kindstr = "";
2166 /* Make sure that this is a type reference or definition. */
2167 if (! isdigit (*s))
2169 char *n;
2170 long index;
2172 index = info->type_index;
2173 ++info->type_index;
2174 n = (char *) xmalloc (strlen (s) + 20);
2175 sprintf (n, "%ld=%s", index, s);
2176 free (s);
2177 s = n;
2179 break;
2181 case DEBUG_REGISTER:
2182 stab_type = N_LSYM;
2183 kindstr = "r";
2184 break;
2187 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2188 sprintf (buf, "%s:%s%s", name, kindstr, s);
2189 free (s);
2191 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2192 return false;
2194 free (buf);
2196 return true;
2199 /* Start outputting a function. */
2201 static boolean
2202 stab_start_function (p, name, globalp)
2203 PTR p;
2204 const char *name;
2205 boolean globalp;
2207 struct stab_write_handle *info = (struct stab_write_handle *) p;
2208 char *rettype, *buf;
2210 assert (info->nesting == 0 && info->fun_offset == -1);
2212 rettype = stab_pop_type (info);
2214 buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2215 sprintf (buf, "%s:%c%s", name,
2216 globalp ? 'F' : 'f',
2217 rettype);
2219 /* We don't know the value now, so we set it in start_block. */
2220 info->fun_offset = info->symbols_size;
2222 if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2223 return false;
2225 free (buf);
2227 return true;
2230 /* Output a function parameter. */
2232 static boolean
2233 stab_function_parameter (p, name, kind, val)
2234 PTR p;
2235 const char *name;
2236 enum debug_parm_kind kind;
2237 bfd_vma val;
2239 struct stab_write_handle *info = (struct stab_write_handle *) p;
2240 char *s, *buf;
2241 int stab_type;
2242 char kindc;
2244 s = stab_pop_type (info);
2246 switch (kind)
2248 default:
2249 abort ();
2251 case DEBUG_PARM_STACK:
2252 stab_type = N_PSYM;
2253 kindc = 'p';
2254 break;
2256 case DEBUG_PARM_REG:
2257 stab_type = N_RSYM;
2258 kindc = 'P';
2259 break;
2261 case DEBUG_PARM_REFERENCE:
2262 stab_type = N_PSYM;
2263 kindc = 'v';
2264 break;
2266 case DEBUG_PARM_REF_REG:
2267 stab_type = N_RSYM;
2268 kindc = 'a';
2269 break;
2272 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2273 sprintf (buf, "%s:%c%s", name, kindc, s);
2274 free (s);
2276 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2277 return false;
2279 free (buf);
2281 return true;
2284 /* Start a block. */
2286 static boolean
2287 stab_start_block (p, addr)
2288 PTR p;
2289 bfd_vma addr;
2291 struct stab_write_handle *info = (struct stab_write_handle *) p;
2293 /* Fill in any slots which have been waiting for the first known
2294 text address. */
2296 if (info->so_offset != -1)
2298 bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2299 info->so_offset = -1;
2302 if (info->fun_offset != -1)
2304 bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2305 info->fun_offset = -1;
2308 ++info->nesting;
2310 /* We will be called with a top level block surrounding the
2311 function, but stabs information does not output that block, so we
2312 ignore it. */
2314 if (info->nesting == 1)
2316 info->fnaddr = addr;
2317 return true;
2320 /* We have to output the LBRAC symbol after any variables which are
2321 declared inside the block. We postpone the LBRAC until the next
2322 start_block or end_block. */
2324 /* If we have postponed an LBRAC, output it now. */
2325 if (info->pending_lbrac != (bfd_vma) -1)
2327 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2328 (const char *) NULL))
2329 return false;
2332 /* Remember the address and output it later. */
2334 info->pending_lbrac = addr - info->fnaddr;
2336 return true;
2339 /* End a block. */
2341 static boolean
2342 stab_end_block (p, addr)
2343 PTR p;
2344 bfd_vma addr;
2346 struct stab_write_handle *info = (struct stab_write_handle *) p;
2348 if (addr > info->last_text_address)
2349 info->last_text_address = addr;
2351 /* If we have postponed an LBRAC, output it now. */
2352 if (info->pending_lbrac != (bfd_vma) -1)
2354 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2355 (const char *) NULL))
2356 return false;
2357 info->pending_lbrac = (bfd_vma) -1;
2360 assert (info->nesting > 0);
2362 --info->nesting;
2364 /* We ignore the outermost block. */
2365 if (info->nesting == 0)
2366 return true;
2368 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2369 (const char *) NULL);
2372 /* End a function. */
2374 /*ARGSUSED*/
2375 static boolean
2376 stab_end_function (p)
2377 PTR p;
2379 return true;
2382 /* Output a line number. */
2384 static boolean
2385 stab_lineno (p, file, lineno, addr)
2386 PTR p;
2387 const char *file;
2388 unsigned long lineno;
2389 bfd_vma addr;
2391 struct stab_write_handle *info = (struct stab_write_handle *) p;
2393 assert (info->lineno_filename != NULL);
2395 if (addr > info->last_text_address)
2396 info->last_text_address = addr;
2398 if (strcmp (file, info->lineno_filename) != 0)
2400 if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2401 return false;
2402 info->lineno_filename = file;
2405 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2406 (const char *) NULL);