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
22 /* This file contains code which writes out stabs debugging
31 #include "libiberty.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. */
52 /* Size of type if this is a typedef. */
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. */
71 /* The type index of this element. */
73 /* The size of the type. */
75 /* Whether type string defines a new type. */
77 /* String defining struct fields. */
79 /* NULL terminated array of strings defining base classes for a
82 /* String defining class methods. */
84 /* String defining vtable pointer for a class. */
88 /* This structure is used to keep track of type indices for tagged
97 /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
99 enum debug_type_kind kind;
100 /* The size of the struct. */
104 /* We remember various sorts of type indices. They are not related,
105 but, for convenience, we keep all the information in this
108 struct stab_type_cache
110 /* The void type index. */
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. */
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
138 /* This buffer holds the symbols. */
141 size_t symbols_alloc;
142 /* This buffer holds the strings. */
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. */
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. */
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. */
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. */
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,
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,
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,
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,
274 stab_start_struct_type,
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,
294 stab_function_parameter,
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;
309 struct string_hash_entry *ret = (struct string_hash_entry *) entry;
311 /* Allocate the structure if it has not already been allocated by a
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)
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));
325 /* Initialize the local fields. */
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. */
342 stab_write_symbol (info, type, desc, value, string)
343 struct stab_write_handle *info;
350 bfd_byte sym[STAB_SYMBOL_SIZE];
356 struct string_hash_entry *h;
358 h = string_hash_lookup (&info->strhash, string, true, false);
361 fprintf (stderr, "string_hash_lookup failed: %s\n",
362 bfd_errmsg (bfd_get_error ()));
371 strx = info->strings_size;
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;
407 /* Push a string on to the type stack. */
410 stab_push_string (info, string, index, definition, size)
411 struct stab_write_handle *info;
417 struct stab_type_stack *s;
419 s = (struct stab_type_stack *) xmalloc (sizeof *s);
420 s->string = xstrdup (string);
422 s->definition = definition;
426 s->baseclasses = NULL;
430 s->next = info->type_stack;
431 info->type_stack = s;
436 /* Push a type index which has already been defined. */
439 stab_push_defined_type (info, index, size)
440 struct stab_write_handle *info;
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. */
455 struct stab_write_handle *info;
457 struct stab_type_stack *s;
460 s = info->type_stack;
463 info->type_stack = s->next;
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. */
482 write_stabs_in_sections_debugging_info (abfd, dhandle, psyms, psymsize,
483 pstrings, pstringsize)
487 bfd_size_type *psymsize;
489 bfd_size_type *pstringsize;
491 struct stab_write_handle info;
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 ()));
512 info.type_stack = NULL;
514 memset (&info.type_cache, 0, sizeof info.type_cache);
516 info.fun_offset = -1;
517 info.last_text_address = 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))
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)))
531 if (! debug_write (dhandle, &stab_fns, (PTR) &info))
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))
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;
553 /* Start writing out information for a compilation unit. */
556 stab_start_compilation_unit (p, filename)
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. */
575 stab_start_source (p, filename)
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
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);
609 index = 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. */
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);
633 index = 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. */
647 stab_int_type (p, size, unsignedp)
652 struct stab_write_handle *info = (struct stab_write_handle *) p;
655 if (size <= 0 || (size > sizeof (long) && size != 8))
657 fprintf (stderr, "stab_int_type: bad size %u\n", size);
662 cache = info->type_cache.signed_integer_types;
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);
673 index = info->type_index;
676 cache[size - 1] = index;
678 sprintf (buf, "%ld=r%ld;", index, index);
682 if (size < sizeof (long))
683 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
684 else if (size == sizeof (long))
687 strcat (buf, "01777777777777777777777;");
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));
698 strcat (buf, "01000000000000000000000;0777777777777777777777;");
703 return stab_push_string (info, buf, index, true, size);
707 /* Push a floating point type. */
710 stab_float_type (p, size)
714 struct stab_write_handle *info = (struct stab_write_handle *) p;
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],
729 /* Floats are defined as a subrange of int. */
730 if (! stab_int_type (info, 4, false))
732 int_type = stab_pop_type (info);
734 index = info->type_index;
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);
746 return stab_push_string (info, buf, index, true, size);
750 /* Push a complex type. */
753 stab_complex_type (p, size)
757 struct stab_write_handle *info = (struct stab_write_handle *) p;
761 index = 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. */
773 stab_bool_type (p, size)
777 struct stab_write_handle *info = (struct stab_write_handle *) p;
800 return stab_push_defined_type (info, index, size);
803 /* Push an enum type. */
806 stab_enum_type (p, tag, names, vals)
810 bfd_signed_vma *vals;
812 struct stab_write_handle *info = (struct stab_write_handle *) p;
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))
835 for (pn = names; *pn != NULL; pn++)
836 len += strlen (*pn) + 20;
842 index = 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);
854 /* FIXME: The size is just a guess. */
855 if (! stab_push_string (info, buf, 0, false, 4))
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))
871 /* Push a modification of the top type on the stack. Cache the
872 results in CACHE and CACHE_ALLOC. */
875 stab_modify_type (info, mod, size, cache, cache_alloc)
876 struct stab_write_handle *info;
886 assert (info->type_stack != NULL);
887 targindex = info->type_stack->index;
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);
902 if (! stab_push_string (info, buf, 0, definition, size))
908 if (targindex >= *cache_alloc)
912 alloc = *cache_alloc;
915 while (targindex >= alloc)
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];
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))
936 index = 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);
944 (*cache)[targindex] = index;
946 if (! stab_push_string (info, buf, index, true, size))
956 /* Push a pointer type. */
959 stab_pointer_type (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. */
972 stab_function_type (p, argcount, varargs)
977 struct stab_write_handle *info = (struct stab_write_handle *) p;
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));
992 s = stab_pop_type (info);
994 buf = (char *) xmalloc (strlen (s) + 40);
995 index = info->type_index;
997 sprintf (buf, "__fake_type_%ld:t%s", index, s);
1001 if (! stab_write_symbol (info, N_LSYM, 0, 0, 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. */
1015 stab_reference_type (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. */
1028 stab_range_type (p, low, high)
1031 bfd_signed_vma high;
1033 struct stab_write_handle *info = (struct stab_write_handle *) p;
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);
1046 if (! stab_push_string (info, buf, 0, definition, size))
1054 /* Push an array type. */
1057 stab_array_type (p, low, high, stringp)
1060 bfd_signed_vma high;
1063 struct stab_write_handle *info = (struct stab_write_handle *) p;
1065 unsigned int element_size;
1066 char *range, *element, *buf;
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);
1086 /* We need to define a type in order to include the string
1088 index = info->type_index;
1091 sprintf (buf, "%ld=@S;", index);
1094 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s", range, low, high, element);
1101 size = element_size * ((high - low) + 1);
1102 if (! stab_push_string (info, buf, index, definition, size))
1110 /* Push a set type. */
1113 stab_set_type (p, bitstringp)
1117 struct stab_write_handle *info = (struct stab_write_handle *) p;
1122 definition = info->type_stack->definition;
1124 s = stab_pop_type (info);
1125 buf = (char *) xmalloc (strlen (s) + 30);
1134 /* We need to define a type in order to include the string
1136 index = info->type_index;
1139 sprintf (buf, "%ld=@S;", index);
1142 sprintf (buf + strlen (buf), "S%s", s);
1145 if (! stab_push_string (info, buf, index, definition, 0))
1153 /* Push an offset type. */
1156 stab_offset_type (p)
1159 struct stab_write_handle *info = (struct stab_write_handle *) p;
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);
1174 if (! stab_push_string (info, buf, 0, definition, 0))
1182 /* Push a method type. */
1185 stab_method_type (p, domainp, argcount, varargs)
1191 struct stab_write_handle *info = (struct stab_write_handle *) p;
1193 char *domain, *return_type, *buf;
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,
1206 if (! stab_empty_type (p))
1210 definition = info->type_stack->definition;
1211 domain = stab_pop_type (info);
1213 /* A non-varargs function is indicated by making the last parameter
1221 else if (argcount == 0)
1227 args = (char **) xmalloc (1 * sizeof (*args));
1228 if (! stab_empty_type (p))
1230 definition = definition || info->type_stack->definition;
1231 args[0] = stab_pop_type (info);
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);
1245 if (! stab_empty_type (p))
1247 definition = definition || info->type_stack->definition;
1248 args[argcount] = stab_pop_type (info);
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);
1265 for (i = 0; i < argcount; i++)
1268 strcat (buf, args[i]);
1276 if (! stab_push_string (info, buf, 0, definition, 0))
1284 /* Push a const version of a type. */
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. */
1299 stab_volatile_type (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. */
1312 stab_get_struct_index (info, tag, id, kind, psize)
1313 struct stab_write_handle *info;
1316 enum debug_type_kind kind;
1317 unsigned int *psize;
1319 if (id >= info->type_cache.struct_types_alloc)
1323 alloc = info->type_cache.struct_types_alloc;
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;
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;
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
1364 stab_start_struct_type (p, tag, id, structp, size)
1371 struct stab_write_handle *info = (struct stab_write_handle *) p;
1376 buf = (char *) xmalloc (40);
1386 index = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1390 sprintf (buf, "%ld=", index);
1394 sprintf (buf + strlen (buf), "%c%u",
1395 structp ? 's' : 'u',
1398 if (! stab_push_string (info, buf, index, definition, size))
1401 info->type_stack->fields = (char *) xmalloc (1);
1402 info->type_stack->fields[0] = '\0';
1407 /* Add a field to a struct. */
1410 stab_struct_field (p, name, bitpos, bitsize, visibility)
1415 enum debug_visibility visibility;
1417 struct stab_write_handle *info = (struct stab_write_handle *) p;
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)
1441 case DEBUG_VISIBILITY_PUBLIC:
1445 case DEBUG_VISIBILITY_PRIVATE:
1449 case DEBUG_VISIBILITY_PROTECTED:
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;
1470 info->type_stack->definition = true;
1475 /* Finish up a struct. */
1478 stab_end_struct_type (p)
1481 struct stab_write_handle *info = (struct stab_write_handle *) p;
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);
1500 if (! stab_push_string (info, buf, index, definition, size))
1508 /* Start outputting a class. */
1511 stab_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1520 struct stab_write_handle *info = (struct stab_write_handle *) p;
1524 if (! vptr || ownvptr)
1531 definition = info->type_stack->definition;
1532 vstring = stab_pop_type (info);
1535 if (! stab_start_struct_type (p, tag, id, structp, size))
1544 assert (info->type_stack->index > 0);
1545 vtable = (char *) xmalloc (20);
1546 sprintf (vtable, "~%%%ld", info->type_stack->index);
1550 vtable = (char *) xmalloc (strlen (vstring) + 3);
1551 sprintf (vtable, "~%%%s", vstring);
1555 info->type_stack->vtable = vtable;
1559 info->type_stack->definition = true;
1564 /* Add a static member to the class on the type stack. */
1567 stab_class_static_member (p, name, physname, visibility)
1570 const char *physname;
1571 enum debug_visibility visibility;
1573 struct stab_write_handle *info = (struct stab_write_handle *) p;
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)
1596 case DEBUG_VISIBILITY_PUBLIC:
1600 case DEBUG_VISIBILITY_PRIVATE:
1604 case DEBUG_VISIBILITY_PROTECTED:
1609 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1612 free (info->type_stack->fields);
1613 info->type_stack->fields = n;
1616 info->type_stack->definition = true;
1621 /* Add a base class to the class on the type stack. */
1624 stab_class_baseclass (p, bitpos, virtual, visibility)
1628 enum debug_visibility visibility;
1630 struct stab_write_handle *info = (struct stab_write_handle *) p;
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';
1649 case DEBUG_VISIBILITY_PRIVATE:
1653 case DEBUG_VISIBILITY_PROTECTED:
1657 case DEBUG_VISIBILITY_PUBLIC:
1662 sprintf (buf + 2, "%ld,%s;", (long) bitpos, 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)
1674 while (info->type_stack->baseclasses[c] != NULL)
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;
1686 info->type_stack->definition = true;
1691 /* Start adding a method to the class on the type stack. */
1694 stab_class_start_method (p, name)
1698 struct stab_write_handle *info = (struct stab_write_handle *) p;
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);
1710 m = (char *) xrealloc (info->type_stack->methods,
1711 (strlen (info->type_stack->methods)
1716 sprintf (m + strlen (m), "%s::", name);
1718 info->type_stack->methods = m;
1723 /* Add a variant, either static or not, to the current method. */
1726 stab_class_method_var (info, physname, visibility, staticp, constp, volatilep,
1728 struct stab_write_handle *info;
1729 const char *physname;
1730 enum debug_visibility visibility;
1739 char *context = NULL;
1740 char visc, qualc, typec;
1742 definition = info->type_stack->definition;
1743 type = stab_pop_type (info);
1747 definition = definition || info->type_stack->definition;
1748 context = stab_pop_type (info);
1751 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1758 case DEBUG_VISIBILITY_PRIVATE:
1762 case DEBUG_VISIBILITY_PROTECTED:
1766 case DEBUG_VISIBILITY_PUBLIC:
1788 else if (! contextp)
1793 info->type_stack->methods =
1794 (char *) xrealloc (info->type_stack->methods,
1795 (strlen (info->type_stack->methods)
1798 + (contextp ? strlen (context) : 0)
1801 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1802 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1807 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1808 "%ld;%s;", (long) voffset, context);
1813 info->type_stack->definition = true;
1818 /* Add a variant to the current method. */
1821 stab_class_method_variant (p, physname, visibility, constp, volatilep,
1824 const char *physname;
1825 enum debug_visibility visibility;
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. */
1840 stab_class_static_method_variant (p, physname, visibility, constp, volatilep)
1842 const char *physname;
1843 enum debug_visibility visibility;
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. */
1856 stab_class_end_method (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, ";");
1870 /* Finish up a class. */
1873 stab_end_class_type (p)
1876 struct stab_write_handle *info = (struct stab_write_handle *) p;
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)
1888 if (info->type_stack->baseclasses != NULL)
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;
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;
1945 /* Push a typedef which was previously defined. */
1948 stab_typedef_type (p, 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. */
1964 stab_tag_type (p, name, id, kind)
1968 enum debug_type_kind kind;
1970 struct stab_write_handle *info = (struct stab_write_handle *) p;
1974 index = stab_get_struct_index (info, name, id, kind, &size);
1978 return stab_push_defined_type (info, index, size);
1981 /* Define a typedef. */
1984 stab_typdef (p, name)
1988 struct stab_write_handle *info = (struct stab_write_handle *) p;
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);
2001 sprintf (buf, "%s:t%s", name, s);
2004 index = info->type_index;
2006 sprintf (buf, "%s:t%ld=%s", name, index, s);
2011 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2016 h = string_hash_lookup (&info->typedef_hash, name, true, false);
2019 fprintf (stderr, "string_hash_lookup failed: %s\n",
2020 bfd_errmsg (bfd_get_error ()));
2024 /* I don't think we care about redefinitions. */
2039 struct stab_write_handle *info = (struct stab_write_handle *) p;
2042 s = stab_pop_type (info);
2044 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
2046 sprintf (buf, "%s:T%s", tag, s);
2049 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2057 /* Define an integer constant. */
2060 stab_int_constant (p, name, val)
2065 struct stab_write_handle *info = (struct stab_write_handle *) p;
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))
2079 /* Define a floating point constant. */
2082 stab_float_constant (p, name, val)
2087 struct stab_write_handle *info = (struct stab_write_handle *) p;
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))
2101 /* Define a typed constant. */
2104 stab_typed_constant (p, name, val)
2109 struct stab_write_handle *info = (struct stab_write_handle *) p;
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);
2118 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2126 /* Record a variable. */
2129 stab_variable (p, name, kind, val)
2132 enum debug_var_kind kind;
2135 struct stab_write_handle *info = (struct stab_write_handle *) p;
2138 const char *kindstr;
2140 s = stab_pop_type (info);
2153 stab_type = N_STSYM;
2157 case DEBUG_LOCAL_STATIC:
2158 stab_type = N_STSYM;
2166 /* Make sure that this is a type reference or definition. */
2172 index = info->type_index;
2174 n = (char *) xmalloc (strlen (s) + 20);
2175 sprintf (n, "%ld=%s", index, s);
2181 case DEBUG_REGISTER:
2187 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2188 sprintf (buf, "%s:%s%s", name, kindstr, s);
2191 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2199 /* Start outputting a function. */
2202 stab_start_function (p, name, 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',
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))
2230 /* Output a function parameter. */
2233 stab_function_parameter (p, name, kind, val)
2236 enum debug_parm_kind kind;
2239 struct stab_write_handle *info = (struct stab_write_handle *) p;
2244 s = stab_pop_type (info);
2251 case DEBUG_PARM_STACK:
2256 case DEBUG_PARM_REG:
2261 case DEBUG_PARM_REFERENCE:
2266 case DEBUG_PARM_REF_REG:
2272 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2273 sprintf (buf, "%s:%c%s", name, kindc, s);
2276 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2284 /* Start a block. */
2287 stab_start_block (p, 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
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;
2310 /* We will be called with a top level block surrounding the
2311 function, but stabs information does not output that block, so we
2314 if (info->nesting == 1)
2316 info->fnaddr = addr;
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))
2332 /* Remember the address and output it later. */
2334 info->pending_lbrac = addr - info->fnaddr;
2342 stab_end_block (p, 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))
2357 info->pending_lbrac = (bfd_vma) -1;
2360 assert (info->nesting > 0);
2364 /* We ignore the outermost block. */
2365 if (info->nesting == 0)
2368 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2369 (const char *) NULL);
2372 /* End a function. */
2376 stab_end_function (p)
2382 /* Output a line number. */
2385 stab_lineno (p, file, lineno, addr)
2388 unsigned long lineno;
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))
2402 info->lineno_filename = file;
2405 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2406 (const char *) NULL);