* elf32-m68k.c (elf_m68k_check_relocs): Don't set DF_TEXTREL for
[binutils.git] / binutils / wrstabs.c
blobf22d8fa8335422ee4a6154d6362dde6052c096a6
1 /* wrstabs.c -- Output stabs debugging information
2 Copyright 1996, 1997, 1998, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor <ian@cygnus.com>.
6 This file is part of GNU Binutils.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* This file contains code which writes out stabs debugging
24 information. */
26 #include <stdio.h>
27 #include <assert.h>
29 #include "bfd.h"
30 #include "bucomm.h"
31 #include "libiberty.h"
32 #include "safe-ctype.h"
33 #include "debug.h"
34 #include "budbg.h"
36 /* Meaningless definition needs by aout64.h. FIXME. */
37 #define BYTES_IN_WORD 4
39 #include "aout/aout64.h"
40 #include "aout/stab_gnu.h"
42 /* The size of a stabs symbol. This presumes 32 bit values. */
44 #define STAB_SYMBOL_SIZE (12)
46 /* An entry in a string hash table. */
48 struct string_hash_entry
50 struct bfd_hash_entry root;
51 /* Next string in this table. */
52 struct string_hash_entry *next;
53 /* Index in string table. */
54 long index;
55 /* Size of type if this is a typedef. */
56 unsigned int size;
59 /* A string hash table. */
61 struct string_hash_table
63 struct bfd_hash_table table;
66 /* The type stack. Each element on the stack is a string. */
68 struct stab_type_stack
70 /* The next element on the stack. */
71 struct stab_type_stack *next;
72 /* This element as a string. */
73 char *string;
74 /* The type index of this element. */
75 long index;
76 /* The size of the type. */
77 unsigned int size;
78 /* Whether type string defines a new type. */
79 bfd_boolean definition;
80 /* String defining struct fields. */
81 char *fields;
82 /* NULL terminated array of strings defining base classes for a
83 class. */
84 char **baseclasses;
85 /* String defining class methods. */
86 char *methods;
87 /* String defining vtable pointer for a class. */
88 char *vtable;
91 /* This structure is used to keep track of type indices for tagged
92 types. */
94 struct stab_tag
96 /* The type index. */
97 long index;
98 /* The tag name. */
99 const char *tag;
100 /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
101 type is defined. */
102 enum debug_type_kind kind;
103 /* The size of the struct. */
104 unsigned int size;
107 /* We remember various sorts of type indices. They are not related,
108 but, for convenience, we keep all the information in this
109 structure. */
111 struct stab_type_cache
113 /* The void type index. */
114 long void_type;
115 /* Signed integer type indices, indexed by size - 1. */
116 long signed_integer_types[8];
117 /* Unsigned integer type indices, indexed by size - 1. */
118 long unsigned_integer_types[8];
119 /* Floating point types, indexed by size - 1. */
120 long float_types[16];
121 /* Pointers to types, indexed by the type index. */
122 long *pointer_types;
123 size_t pointer_types_alloc;
124 /* Functions returning types, indexed by the type index. */
125 long *function_types;
126 size_t function_types_alloc;
127 /* References to types, indexed by the type index. */
128 long *reference_types;
129 size_t reference_types_alloc;
130 /* Struct/union/class type indices, indexed by the struct id. */
131 struct stab_tag *struct_types;
132 size_t struct_types_alloc;
135 /* This is the handle passed through debug_write. */
137 struct stab_write_handle
139 /* The BFD. */
140 bfd *abfd;
141 /* This buffer holds the symbols. */
142 bfd_byte *symbols;
143 size_t symbols_size;
144 size_t symbols_alloc;
145 /* This is a list of hash table entries for the strings. */
146 struct string_hash_entry *strings;
147 /* The last string hash table entry. */
148 struct string_hash_entry *last_string;
149 /* The size of the strings. */
150 size_t strings_size;
151 /* This hash table eliminates duplicate strings. */
152 struct string_hash_table strhash;
153 /* The type stack. */
154 struct stab_type_stack *type_stack;
155 /* The next type index. */
156 long type_index;
157 /* The type cache. */
158 struct stab_type_cache type_cache;
159 /* A mapping from typedef names to type indices. */
160 struct string_hash_table typedef_hash;
161 /* If this is not -1, it is the offset to the most recent N_SO
162 symbol, and the value of that symbol needs to be set. */
163 long so_offset;
164 /* If this is not -1, it is the offset to the most recent N_FUN
165 symbol, and the value of that symbol needs to be set. */
166 long fun_offset;
167 /* The last text section address seen. */
168 bfd_vma last_text_address;
169 /* The block nesting depth. */
170 unsigned int nesting;
171 /* The function address. */
172 bfd_vma fnaddr;
173 /* A pending LBRAC symbol. */
174 bfd_vma pending_lbrac;
175 /* The current line number file name. */
176 const char *lineno_filename;
179 static struct bfd_hash_entry *string_hash_newfunc
180 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
181 static bfd_boolean stab_write_symbol
182 PARAMS ((struct stab_write_handle *, int, int, bfd_vma, const char *));
183 static bfd_boolean stab_push_string
184 PARAMS ((struct stab_write_handle *, const char *, long, bfd_boolean,
185 unsigned int));
186 static bfd_boolean stab_push_defined_type
187 PARAMS ((struct stab_write_handle *, long, unsigned int));
188 static char *stab_pop_type
189 PARAMS ((struct stab_write_handle *));
190 static bfd_boolean stab_modify_type
191 PARAMS ((struct stab_write_handle *, int, unsigned int, long **, size_t *));
192 static long stab_get_struct_index
193 PARAMS ((struct stab_write_handle *, const char *, unsigned int,
194 enum debug_type_kind, unsigned int *));
195 static bfd_boolean stab_class_method_var
196 PARAMS ((struct stab_write_handle *, const char *, enum debug_visibility,
197 bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean));
198 static bfd_boolean stab_start_compilation_unit
199 PARAMS ((PTR, const char *));
200 static bfd_boolean stab_start_source
201 PARAMS ((PTR, const char *));
202 static bfd_boolean stab_empty_type
203 PARAMS ((PTR));
204 static bfd_boolean stab_void_type
205 PARAMS ((PTR));
206 static bfd_boolean stab_int_type
207 PARAMS ((PTR, unsigned int, bfd_boolean));
208 static bfd_boolean stab_float_type
209 PARAMS ((PTR, unsigned int));
210 static bfd_boolean stab_complex_type
211 PARAMS ((PTR, unsigned int));
212 static bfd_boolean stab_bool_type
213 PARAMS ((PTR, unsigned int));
214 static bfd_boolean stab_enum_type
215 PARAMS ((PTR, const char *, const char **, bfd_signed_vma *));
216 static bfd_boolean stab_pointer_type
217 PARAMS ((PTR));
218 static bfd_boolean stab_function_type
219 PARAMS ((PTR, int, bfd_boolean));
220 static bfd_boolean stab_reference_type
221 PARAMS ((PTR));
222 static bfd_boolean stab_range_type
223 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma));
224 static bfd_boolean stab_array_type
225 PARAMS ((PTR, bfd_signed_vma, bfd_signed_vma, bfd_boolean));
226 static bfd_boolean stab_set_type
227 PARAMS ((PTR, bfd_boolean));
228 static bfd_boolean stab_offset_type
229 PARAMS ((PTR));
230 static bfd_boolean stab_method_type
231 PARAMS ((PTR, bfd_boolean, int, bfd_boolean));
232 static bfd_boolean stab_const_type
233 PARAMS ((PTR));
234 static bfd_boolean stab_volatile_type
235 PARAMS ((PTR));
236 static bfd_boolean stab_start_struct_type
237 PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int));
238 static bfd_boolean stab_struct_field
239 PARAMS ((PTR, const char *, bfd_vma, bfd_vma, enum debug_visibility));
240 static bfd_boolean stab_end_struct_type
241 PARAMS ((PTR));
242 static bfd_boolean stab_start_class_type
243 PARAMS ((PTR, const char *, unsigned int, bfd_boolean, unsigned int,
244 bfd_boolean, bfd_boolean));
245 static bfd_boolean stab_class_static_member
246 PARAMS ((PTR, const char *, const char *, enum debug_visibility));
247 static bfd_boolean stab_class_baseclass
248 PARAMS ((PTR, bfd_vma, bfd_boolean, enum debug_visibility));
249 static bfd_boolean stab_class_start_method
250 PARAMS ((PTR, const char *));
251 static bfd_boolean stab_class_method_variant
252 PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
253 bfd_vma, bfd_boolean));
254 static bfd_boolean stab_class_static_method_variant
255 PARAMS ((PTR, const char *, enum debug_visibility, bfd_boolean,
256 bfd_boolean));
257 static bfd_boolean stab_class_end_method
258 PARAMS ((PTR));
259 static bfd_boolean stab_end_class_type
260 PARAMS ((PTR));
261 static bfd_boolean stab_typedef_type
262 PARAMS ((PTR, const char *));
263 static bfd_boolean stab_tag_type
264 PARAMS ((PTR, const char *, unsigned int, enum debug_type_kind));
265 static bfd_boolean stab_typdef
266 PARAMS ((PTR, const char *));
267 static bfd_boolean stab_tag
268 PARAMS ((PTR, const char *));
269 static bfd_boolean stab_int_constant
270 PARAMS ((PTR, const char *, bfd_vma));
271 static bfd_boolean stab_float_constant
272 PARAMS ((PTR, const char *, double));
273 static bfd_boolean stab_typed_constant
274 PARAMS ((PTR, const char *, bfd_vma));
275 static bfd_boolean stab_variable
276 PARAMS ((PTR, const char *, enum debug_var_kind, bfd_vma));
277 static bfd_boolean stab_start_function
278 PARAMS ((PTR, const char *, bfd_boolean));
279 static bfd_boolean stab_function_parameter
280 PARAMS ((PTR, const char *, enum debug_parm_kind, bfd_vma));
281 static bfd_boolean stab_start_block
282 PARAMS ((PTR, bfd_vma));
283 static bfd_boolean stab_end_block
284 PARAMS ((PTR, bfd_vma));
285 static bfd_boolean stab_end_function
286 PARAMS ((PTR));
287 static bfd_boolean stab_lineno
288 PARAMS ((PTR, const char *, unsigned long, bfd_vma));
290 static const struct debug_write_fns stab_fns =
292 stab_start_compilation_unit,
293 stab_start_source,
294 stab_empty_type,
295 stab_void_type,
296 stab_int_type,
297 stab_float_type,
298 stab_complex_type,
299 stab_bool_type,
300 stab_enum_type,
301 stab_pointer_type,
302 stab_function_type,
303 stab_reference_type,
304 stab_range_type,
305 stab_array_type,
306 stab_set_type,
307 stab_offset_type,
308 stab_method_type,
309 stab_const_type,
310 stab_volatile_type,
311 stab_start_struct_type,
312 stab_struct_field,
313 stab_end_struct_type,
314 stab_start_class_type,
315 stab_class_static_member,
316 stab_class_baseclass,
317 stab_class_start_method,
318 stab_class_method_variant,
319 stab_class_static_method_variant,
320 stab_class_end_method,
321 stab_end_class_type,
322 stab_typedef_type,
323 stab_tag_type,
324 stab_typdef,
325 stab_tag,
326 stab_int_constant,
327 stab_float_constant,
328 stab_typed_constant,
329 stab_variable,
330 stab_start_function,
331 stab_function_parameter,
332 stab_start_block,
333 stab_end_block,
334 stab_end_function,
335 stab_lineno
338 /* Routine to create an entry in a string hash table. */
340 static struct bfd_hash_entry *
341 string_hash_newfunc (entry, table, string)
342 struct bfd_hash_entry *entry;
343 struct bfd_hash_table *table;
344 const char *string;
346 struct string_hash_entry *ret = (struct string_hash_entry *) entry;
348 /* Allocate the structure if it has not already been allocated by a
349 subclass. */
350 if (ret == (struct string_hash_entry *) NULL)
351 ret = ((struct string_hash_entry *)
352 bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
353 if (ret == (struct string_hash_entry *) NULL)
354 return NULL;
356 /* Call the allocation method of the superclass. */
357 ret = ((struct string_hash_entry *)
358 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
360 if (ret)
362 /* Initialize the local fields. */
363 ret->next = NULL;
364 ret->index = -1;
365 ret->size = 0;
368 return (struct bfd_hash_entry *) ret;
371 /* Look up an entry in a string hash table. */
373 #define string_hash_lookup(t, string, create, copy) \
374 ((struct string_hash_entry *) \
375 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
377 /* Add a symbol to the stabs debugging information we are building. */
379 static bfd_boolean
380 stab_write_symbol (info, type, desc, value, string)
381 struct stab_write_handle *info;
382 int type;
383 int desc;
384 bfd_vma value;
385 const char *string;
387 bfd_size_type strx;
388 bfd_byte sym[STAB_SYMBOL_SIZE];
390 if (string == NULL)
391 strx = 0;
392 else
394 struct string_hash_entry *h;
396 h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
397 if (h == NULL)
399 non_fatal (_("string_hash_lookup failed: %s"),
400 bfd_errmsg (bfd_get_error ()));
401 return FALSE;
403 if (h->index != -1)
404 strx = h->index;
405 else
407 strx = info->strings_size;
408 h->index = strx;
409 if (info->last_string == NULL)
410 info->strings = h;
411 else
412 info->last_string->next = h;
413 info->last_string = h;
414 info->strings_size += strlen (string) + 1;
418 /* This presumes 32 bit values. */
419 bfd_put_32 (info->abfd, strx, sym);
420 bfd_put_8 (info->abfd, type, sym + 4);
421 bfd_put_8 (info->abfd, 0, sym + 5);
422 bfd_put_16 (info->abfd, desc, sym + 6);
423 bfd_put_32 (info->abfd, value, sym + 8);
425 if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
427 info->symbols_alloc *= 2;
428 info->symbols = (bfd_byte *) xrealloc (info->symbols,
429 info->symbols_alloc);
432 memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
434 info->symbols_size += STAB_SYMBOL_SIZE;
436 return TRUE;
439 /* Push a string on to the type stack. */
441 static bfd_boolean
442 stab_push_string (info, string, index, definition, size)
443 struct stab_write_handle *info;
444 const char *string;
445 long index;
446 bfd_boolean definition;
447 unsigned int size;
449 struct stab_type_stack *s;
451 s = (struct stab_type_stack *) xmalloc (sizeof *s);
452 s->string = xstrdup (string);
453 s->index = index;
454 s->definition = definition;
455 s->size = size;
457 s->fields = NULL;
458 s->baseclasses = NULL;
459 s->methods = NULL;
460 s->vtable = NULL;
462 s->next = info->type_stack;
463 info->type_stack = s;
465 return TRUE;
468 /* Push a type index which has already been defined. */
470 static bfd_boolean
471 stab_push_defined_type (info, index, size)
472 struct stab_write_handle *info;
473 long index;
474 unsigned int size;
476 char buf[20];
478 sprintf (buf, "%ld", index);
479 return stab_push_string (info, buf, index, FALSE, size);
482 /* Pop a type off the type stack. The caller is responsible for
483 freeing the string. */
485 static char *
486 stab_pop_type (info)
487 struct stab_write_handle *info;
489 struct stab_type_stack *s;
490 char *ret;
492 s = info->type_stack;
493 assert (s != NULL);
495 info->type_stack = s->next;
497 ret = s->string;
499 free (s);
501 return ret;
504 /* The general routine to write out stabs in sections debugging
505 information. This accumulates the stabs symbols and the strings in
506 two obstacks. We can't easily write out the information as we go
507 along, because we need to know the section sizes before we can
508 write out the section contents. ABFD is the BFD and DHANDLE is the
509 handle for the debugging information. This sets *PSYMS to point to
510 the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
511 strings, and *PSTRINGSIZE to the size of the strings. */
513 bfd_boolean
514 write_stabs_in_sections_debugging_info (abfd, dhandle, psyms, psymsize,
515 pstrings, pstringsize)
516 bfd *abfd;
517 PTR dhandle;
518 bfd_byte **psyms;
519 bfd_size_type *psymsize;
520 bfd_byte **pstrings;
521 bfd_size_type *pstringsize;
523 struct stab_write_handle info;
524 struct string_hash_entry *h;
525 bfd_byte *p;
527 info.abfd = abfd;
529 info.symbols_size = 0;
530 info.symbols_alloc = 500;
531 info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
533 info.strings = NULL;
534 info.last_string = NULL;
535 /* Reserve 1 byte for a null byte. */
536 info.strings_size = 1;
538 if (! bfd_hash_table_init (&info.strhash.table, string_hash_newfunc)
539 || ! bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc))
541 non_fatal ("bfd_hash_table_init_failed: %s",
542 bfd_errmsg (bfd_get_error ()));
543 return FALSE;
546 info.type_stack = NULL;
547 info.type_index = 1;
548 memset (&info.type_cache, 0, sizeof info.type_cache);
549 info.so_offset = -1;
550 info.fun_offset = -1;
551 info.last_text_address = 0;
552 info.nesting = 0;
553 info.fnaddr = 0;
554 info.pending_lbrac = (bfd_vma) -1;
556 /* The initial symbol holds the string size. */
557 if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
558 return FALSE;
560 /* Output an initial N_SO symbol. */
561 info.so_offset = info.symbols_size;
562 if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
563 return FALSE;
565 if (! debug_write (dhandle, &stab_fns, (PTR) &info))
566 return FALSE;
568 assert (info.pending_lbrac == (bfd_vma) -1);
570 /* Output a trailing N_SO. */
571 if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
572 (const char *) NULL))
573 return FALSE;
575 /* Put the string size in the initial symbol. */
576 bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
578 *psyms = info.symbols;
579 *psymsize = info.symbols_size;
581 *pstringsize = info.strings_size;
582 *pstrings = (bfd_byte *) xmalloc (info.strings_size);
584 p = *pstrings;
585 *p++ = '\0';
586 for (h = info.strings; h != NULL; h = h->next)
588 strcpy ((char *) p, h->root.string);
589 p += strlen ((char *) p) + 1;
592 return TRUE;
595 /* Start writing out information for a compilation unit. */
597 static bfd_boolean
598 stab_start_compilation_unit (p, filename)
599 PTR p;
600 const char *filename;
602 struct stab_write_handle *info = (struct stab_write_handle *) p;
604 /* We would normally output an N_SO symbol here. However, that
605 would force us to reset all of our type information. I think we
606 will be better off just outputting an N_SOL symbol, and not
607 worrying about splitting information between files. */
609 info->lineno_filename = filename;
611 return stab_write_symbol (info, N_SOL, 0, 0, filename);
614 /* Start writing out information for a particular source file. */
616 static bfd_boolean
617 stab_start_source (p, filename)
618 PTR p;
619 const char *filename;
621 struct stab_write_handle *info = (struct stab_write_handle *) p;
623 /* FIXME: The symbol's value is supposed to be the text section
624 address. However, we would have to fill it in later, and gdb
625 doesn't care, so we don't bother with it. */
627 info->lineno_filename = filename;
629 return stab_write_symbol (info, N_SOL, 0, 0, filename);
632 /* Push an empty type. This shouldn't normally happen. We just use a
633 void type. */
635 static bfd_boolean
636 stab_empty_type (p)
637 PTR p;
639 struct stab_write_handle *info = (struct stab_write_handle *) p;
641 /* We don't call stab_void_type if the type is not yet defined,
642 because that might screw up the typedef. */
644 if (info->type_cache.void_type != 0)
645 return stab_push_defined_type (info, info->type_cache.void_type, 0);
646 else
648 long index;
649 char buf[40];
651 index = info->type_index;
652 ++info->type_index;
654 sprintf (buf, "%ld=%ld", index, index);
656 return stab_push_string (info, buf, index, FALSE, 0);
660 /* Push a void type. */
662 static bfd_boolean
663 stab_void_type (p)
664 PTR p;
666 struct stab_write_handle *info = (struct stab_write_handle *) p;
668 if (info->type_cache.void_type != 0)
669 return stab_push_defined_type (info, info->type_cache.void_type, 0);
670 else
672 long index;
673 char buf[40];
675 index = info->type_index;
676 ++info->type_index;
678 info->type_cache.void_type = index;
680 sprintf (buf, "%ld=%ld", index, index);
682 return stab_push_string (info, buf, index, TRUE, 0);
686 /* Push an integer type. */
688 static bfd_boolean
689 stab_int_type (p, size, unsignedp)
690 PTR p;
691 unsigned int size;
692 bfd_boolean unsignedp;
694 struct stab_write_handle *info = (struct stab_write_handle *) p;
695 long *cache;
697 if (size <= 0 || (size > sizeof (long) && size != 8))
699 non_fatal (_("stab_int_type: bad size %u"), size);
700 return FALSE;
703 if (unsignedp)
704 cache = info->type_cache.signed_integer_types;
705 else
706 cache = info->type_cache.unsigned_integer_types;
708 if (cache[size - 1] != 0)
709 return stab_push_defined_type (info, cache[size - 1], size);
710 else
712 long index;
713 char buf[100];
715 index = info->type_index;
716 ++info->type_index;
718 cache[size - 1] = index;
720 sprintf (buf, "%ld=r%ld;", index, index);
721 if (unsignedp)
723 strcat (buf, "0;");
724 if (size < sizeof (long))
725 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
726 else if (size == sizeof (long))
727 strcat (buf, "-1;");
728 else if (size == 8)
729 strcat (buf, "01777777777777777777777;");
730 else
731 abort ();
733 else
735 if (size <= sizeof (long))
736 sprintf (buf + strlen (buf), "%ld;%ld;",
737 (long) - ((unsigned long) 1 << (size * 8 - 1)),
738 (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
739 else if (size == 8)
740 strcat (buf, "01000000000000000000000;0777777777777777777777;");
741 else
742 abort ();
745 return stab_push_string (info, buf, index, TRUE, size);
749 /* Push a floating point type. */
751 static bfd_boolean
752 stab_float_type (p, size)
753 PTR p;
754 unsigned int size;
756 struct stab_write_handle *info = (struct stab_write_handle *) p;
758 if (size > 0
759 && size - 1 < (sizeof info->type_cache.float_types
760 / sizeof info->type_cache.float_types[0])
761 && info->type_cache.float_types[size - 1] != 0)
762 return stab_push_defined_type (info,
763 info->type_cache.float_types[size - 1],
764 size);
765 else
767 long index;
768 char *int_type;
769 char buf[50];
771 /* Floats are defined as a subrange of int. */
772 if (! stab_int_type (info, 4, FALSE))
773 return FALSE;
774 int_type = stab_pop_type (info);
776 index = info->type_index;
777 ++info->type_index;
779 if (size > 0
780 && size - 1 < (sizeof info->type_cache.float_types
781 / sizeof info->type_cache.float_types[0]))
782 info->type_cache.float_types[size - 1] = index;
784 sprintf (buf, "%ld=r%s;%u;0;", index, int_type, size);
786 free (int_type);
788 return stab_push_string (info, buf, index, TRUE, size);
792 /* Push a complex type. */
794 static bfd_boolean
795 stab_complex_type (p, size)
796 PTR p;
797 unsigned int size;
799 struct stab_write_handle *info = (struct stab_write_handle *) p;
800 char buf[50];
801 long index;
803 index = info->type_index;
804 ++info->type_index;
806 sprintf (buf, "%ld=r%ld;%u;0;", index, index, size);
808 return stab_push_string (info, buf, index, TRUE, size * 2);
811 /* Push a bfd_boolean type. We use an XCOFF predefined type, since gdb
812 always recognizes them. */
814 static bfd_boolean
815 stab_bool_type (p, size)
816 PTR p;
817 unsigned int size;
819 struct stab_write_handle *info = (struct stab_write_handle *) p;
820 long index;
822 switch (size)
824 case 1:
825 index = -21;
826 break;
828 case 2:
829 index = -22;
830 break;
832 default:
833 case 4:
834 index = -16;
835 break;
837 case 8:
838 index = -33;
839 break;
842 return stab_push_defined_type (info, index, size);
845 /* Push an enum type. */
847 static bfd_boolean
848 stab_enum_type (p, tag, names, vals)
849 PTR p;
850 const char *tag;
851 const char **names;
852 bfd_signed_vma *vals;
854 struct stab_write_handle *info = (struct stab_write_handle *) p;
855 size_t len;
856 const char **pn;
857 char *buf;
858 long index = 0;
859 bfd_signed_vma *pv;
861 if (names == NULL)
863 assert (tag != NULL);
865 buf = (char *) xmalloc (10 + strlen (tag));
866 sprintf (buf, "xe%s:", tag);
867 /* FIXME: The size is just a guess. */
868 if (! stab_push_string (info, buf, 0, FALSE, 4))
869 return FALSE;
870 free (buf);
871 return TRUE;
874 len = 10;
875 if (tag != NULL)
876 len += strlen (tag);
877 for (pn = names; *pn != NULL; pn++)
878 len += strlen (*pn) + 20;
880 buf = (char *) xmalloc (len);
882 if (tag == NULL)
883 strcpy (buf, "e");
884 else
886 index = info->type_index;
887 ++info->type_index;
888 sprintf (buf, "%s:T%ld=e", tag, index);
891 for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
892 sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
893 strcat (buf, ";");
895 if (tag == NULL)
897 /* FIXME: The size is just a guess. */
898 if (! stab_push_string (info, buf, 0, FALSE, 4))
899 return FALSE;
901 else
903 /* FIXME: The size is just a guess. */
904 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
905 || ! stab_push_defined_type (info, index, 4))
906 return FALSE;
909 free (buf);
911 return TRUE;
914 /* Push a modification of the top type on the stack. Cache the
915 results in CACHE and CACHE_ALLOC. */
917 static bfd_boolean
918 stab_modify_type (info, mod, size, cache, cache_alloc)
919 struct stab_write_handle *info;
920 int mod;
921 unsigned int size;
922 long **cache;
923 size_t *cache_alloc;
925 long targindex;
926 long index;
927 char *s, *buf;
929 assert (info->type_stack != NULL);
930 targindex = info->type_stack->index;
932 if (targindex <= 0
933 || cache == NULL)
935 bfd_boolean definition;
937 /* Either the target type has no index, or we aren't caching
938 this modifier. Either way we have no way of recording the
939 new type, so we don't bother to define one. */
940 definition = info->type_stack->definition;
941 s = stab_pop_type (info);
942 buf = (char *) xmalloc (strlen (s) + 2);
943 sprintf (buf, "%c%s", mod, s);
944 free (s);
945 if (! stab_push_string (info, buf, 0, definition, size))
946 return FALSE;
947 free (buf);
949 else
951 if ((size_t) targindex >= *cache_alloc)
953 size_t alloc;
955 alloc = *cache_alloc;
956 if (alloc == 0)
957 alloc = 10;
958 while ((size_t) targindex >= alloc)
959 alloc *= 2;
960 *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
961 memset (*cache + *cache_alloc, 0,
962 (alloc - *cache_alloc) * sizeof (long));
963 *cache_alloc = alloc;
966 index = (*cache)[targindex];
967 if (index != 0 && ! info->type_stack->definition)
969 /* We have already defined a modification of this type, and
970 the entry on the type stack is not a definition, so we
971 can safely discard it (we may have a definition on the
972 stack, even if we already defined a modification, if it
973 is a struct which we did not define at the time it was
974 referenced). */
975 free (stab_pop_type (info));
976 if (! stab_push_defined_type (info, index, size))
977 return FALSE;
979 else
981 index = info->type_index;
982 ++info->type_index;
984 s = stab_pop_type (info);
985 buf = (char *) xmalloc (strlen (s) + 20);
986 sprintf (buf, "%ld=%c%s", index, mod, s);
987 free (s);
989 (*cache)[targindex] = index;
991 if (! stab_push_string (info, buf, index, TRUE, size))
992 return FALSE;
994 free (buf);
998 return TRUE;
1001 /* Push a pointer type. */
1003 static bfd_boolean
1004 stab_pointer_type (p)
1005 PTR p;
1007 struct stab_write_handle *info = (struct stab_write_handle *) p;
1009 /* FIXME: The size should depend upon the architecture. */
1010 return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
1011 &info->type_cache.pointer_types_alloc);
1014 /* Push a function type. */
1016 static bfd_boolean
1017 stab_function_type (p, argcount, varargs)
1018 PTR p;
1019 int argcount;
1020 bfd_boolean varargs ATTRIBUTE_UNUSED;
1022 struct stab_write_handle *info = (struct stab_write_handle *) p;
1023 int i;
1025 /* We have no way to represent the argument types, so we just
1026 discard them. However, if they define new types, we must output
1027 them. We do this by producing empty typedefs. */
1028 for (i = 0; i < argcount; i++)
1030 if (! info->type_stack->definition)
1031 free (stab_pop_type (info));
1032 else
1034 char *s, *buf;
1036 s = stab_pop_type (info);
1038 buf = (char *) xmalloc (strlen (s) + 3);
1039 sprintf (buf, ":t%s", s);
1040 free (s);
1042 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1043 return FALSE;
1045 free (buf);
1049 return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
1050 &info->type_cache.function_types_alloc);
1053 /* Push a reference type. */
1055 static bfd_boolean
1056 stab_reference_type (p)
1057 PTR p;
1059 struct stab_write_handle *info = (struct stab_write_handle *) p;
1061 /* FIXME: The size should depend upon the architecture. */
1062 return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
1063 &info->type_cache.reference_types_alloc);
1066 /* Push a range type. */
1068 static bfd_boolean
1069 stab_range_type (p, low, high)
1070 PTR p;
1071 bfd_signed_vma low;
1072 bfd_signed_vma high;
1074 struct stab_write_handle *info = (struct stab_write_handle *) p;
1075 bfd_boolean definition;
1076 unsigned int size;
1077 char *s, *buf;
1079 definition = info->type_stack->definition;
1080 size = info->type_stack->size;
1082 s = stab_pop_type (info);
1083 buf = (char *) xmalloc (strlen (s) + 100);
1084 sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1085 free (s);
1087 if (! stab_push_string (info, buf, 0, definition, size))
1088 return FALSE;
1090 free (buf);
1092 return TRUE;
1095 /* Push an array type. */
1097 static bfd_boolean
1098 stab_array_type (p, low, high, stringp)
1099 PTR p;
1100 bfd_signed_vma low;
1101 bfd_signed_vma high;
1102 bfd_boolean stringp;
1104 struct stab_write_handle *info = (struct stab_write_handle *) p;
1105 bfd_boolean definition;
1106 unsigned int element_size;
1107 char *range, *element, *buf;
1108 long index;
1109 unsigned int size;
1111 definition = info->type_stack->definition;
1112 range = stab_pop_type (info);
1114 definition = definition || info->type_stack->definition;
1115 element_size = info->type_stack->size;
1116 element = stab_pop_type (info);
1118 buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1120 if (! stringp)
1122 index = 0;
1123 *buf = '\0';
1125 else
1127 /* We need to define a type in order to include the string
1128 attribute. */
1129 index = info->type_index;
1130 ++info->type_index;
1131 definition = TRUE;
1132 sprintf (buf, "%ld=@S;", index);
1135 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1136 range, (long) low, (long) high, element);
1137 free (range);
1138 free (element);
1140 if (high < low)
1141 size = 0;
1142 else
1143 size = element_size * ((high - low) + 1);
1144 if (! stab_push_string (info, buf, index, definition, size))
1145 return FALSE;
1147 free (buf);
1149 return TRUE;
1152 /* Push a set type. */
1154 static bfd_boolean
1155 stab_set_type (p, bitstringp)
1156 PTR p;
1157 bfd_boolean bitstringp;
1159 struct stab_write_handle *info = (struct stab_write_handle *) p;
1160 bfd_boolean definition;
1161 char *s, *buf;
1162 long index;
1164 definition = info->type_stack->definition;
1166 s = stab_pop_type (info);
1167 buf = (char *) xmalloc (strlen (s) + 30);
1169 if (! bitstringp)
1171 *buf = '\0';
1172 index = 0;
1174 else
1176 /* We need to define a type in order to include the string
1177 attribute. */
1178 index = info->type_index;
1179 ++info->type_index;
1180 definition = TRUE;
1181 sprintf (buf, "%ld=@S;", index);
1184 sprintf (buf + strlen (buf), "S%s", s);
1185 free (s);
1187 if (! stab_push_string (info, buf, index, definition, 0))
1188 return FALSE;
1190 free (buf);
1192 return TRUE;
1195 /* Push an offset type. */
1197 static bfd_boolean
1198 stab_offset_type (p)
1199 PTR p;
1201 struct stab_write_handle *info = (struct stab_write_handle *) p;
1202 bfd_boolean definition;
1203 char *target, *base, *buf;
1205 definition = info->type_stack->definition;
1206 target = stab_pop_type (info);
1208 definition = definition || info->type_stack->definition;
1209 base = stab_pop_type (info);
1211 buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1212 sprintf (buf, "@%s,%s", base, target);
1213 free (base);
1214 free (target);
1216 if (! stab_push_string (info, buf, 0, definition, 0))
1217 return FALSE;
1219 free (buf);
1221 return TRUE;
1224 /* Push a method type. */
1226 static bfd_boolean
1227 stab_method_type (p, domainp, argcount, varargs)
1228 PTR p;
1229 bfd_boolean domainp;
1230 int argcount;
1231 bfd_boolean varargs;
1233 struct stab_write_handle *info = (struct stab_write_handle *) p;
1234 bfd_boolean definition;
1235 char *domain, *return_type, *buf;
1236 char **args;
1237 int i;
1238 size_t len;
1240 /* We don't bother with stub method types, because that would
1241 require a mangler for C++ argument types. This will waste space
1242 in the debugging output. */
1244 /* We need a domain. I'm not sure DOMAINP can ever be false,
1245 anyhow. */
1246 if (! domainp)
1248 if (! stab_empty_type (p))
1249 return FALSE;
1252 definition = info->type_stack->definition;
1253 domain = stab_pop_type (info);
1255 /* A non-varargs function is indicated by making the last parameter
1256 type be void. */
1258 if (argcount < 0)
1260 args = NULL;
1261 argcount = 0;
1263 else if (argcount == 0)
1265 if (varargs)
1266 args = NULL;
1267 else
1269 args = (char **) xmalloc (1 * sizeof (*args));
1270 if (! stab_empty_type (p))
1271 return FALSE;
1272 definition = definition || info->type_stack->definition;
1273 args[0] = stab_pop_type (info);
1274 argcount = 1;
1277 else
1279 args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1280 for (i = argcount - 1; i >= 0; i--)
1282 definition = definition || info->type_stack->definition;
1283 args[i] = stab_pop_type (info);
1285 if (! varargs)
1287 if (! stab_empty_type (p))
1288 return FALSE;
1289 definition = definition || info->type_stack->definition;
1290 args[argcount] = stab_pop_type (info);
1291 ++argcount;
1295 definition = definition || info->type_stack->definition;
1296 return_type = stab_pop_type (info);
1298 len = strlen (domain) + strlen (return_type) + 10;
1299 for (i = 0; i < argcount; i++)
1300 len += strlen (args[i]);
1302 buf = (char *) xmalloc (len);
1304 sprintf (buf, "#%s,%s", domain, return_type);
1305 free (domain);
1306 free (return_type);
1307 for (i = 0; i < argcount; i++)
1309 strcat (buf, ",");
1310 strcat (buf, args[i]);
1311 free (args[i]);
1313 strcat (buf, ";");
1315 if (args != NULL)
1316 free (args);
1318 if (! stab_push_string (info, buf, 0, definition, 0))
1319 return FALSE;
1321 free (buf);
1323 return TRUE;
1326 /* Push a const version of a type. */
1328 static bfd_boolean
1329 stab_const_type (p)
1330 PTR p;
1332 struct stab_write_handle *info = (struct stab_write_handle *) p;
1334 return stab_modify_type (info, 'k', info->type_stack->size,
1335 (long **) NULL, (size_t *) NULL);
1338 /* Push a volatile version of a type. */
1340 static bfd_boolean
1341 stab_volatile_type (p)
1342 PTR p;
1344 struct stab_write_handle *info = (struct stab_write_handle *) p;
1346 return stab_modify_type (info, 'B', info->type_stack->size,
1347 (long **) NULL, (size_t *) NULL);
1350 /* Get the type index to use for a struct/union/class ID. This should
1351 return -1 if it fails. */
1353 static long
1354 stab_get_struct_index (info, tag, id, kind, psize)
1355 struct stab_write_handle *info;
1356 const char *tag;
1357 unsigned int id;
1358 enum debug_type_kind kind;
1359 unsigned int *psize;
1361 if (id >= info->type_cache.struct_types_alloc)
1363 size_t alloc;
1365 alloc = info->type_cache.struct_types_alloc;
1366 if (alloc == 0)
1367 alloc = 10;
1368 while (id >= alloc)
1369 alloc *= 2;
1370 info->type_cache.struct_types =
1371 (struct stab_tag *) xrealloc (info->type_cache.struct_types,
1372 alloc * sizeof (struct stab_tag));
1373 memset ((info->type_cache.struct_types
1374 + info->type_cache.struct_types_alloc),
1376 ((alloc - info->type_cache.struct_types_alloc)
1377 * sizeof (struct stab_tag)));
1378 info->type_cache.struct_types_alloc = alloc;
1381 if (info->type_cache.struct_types[id].index == 0)
1383 info->type_cache.struct_types[id].index = info->type_index;
1384 ++info->type_index;
1385 info->type_cache.struct_types[id].tag = tag;
1386 info->type_cache.struct_types[id].kind = kind;
1389 if (kind == DEBUG_KIND_ILLEGAL)
1391 /* This is a definition of the struct. */
1392 info->type_cache.struct_types[id].kind = kind;
1393 info->type_cache.struct_types[id].size = *psize;
1395 else
1396 *psize = info->type_cache.struct_types[id].size;
1398 return info->type_cache.struct_types[id].index;
1401 /* Start outputting a struct. We ignore the tag, and handle it in
1402 stab_tag. */
1404 static bfd_boolean
1405 stab_start_struct_type (p, tag, id, structp, size)
1406 PTR p;
1407 const char *tag;
1408 unsigned int id;
1409 bfd_boolean structp;
1410 unsigned int size;
1412 struct stab_write_handle *info = (struct stab_write_handle *) p;
1413 long index;
1414 bfd_boolean definition;
1415 char *buf;
1417 buf = (char *) xmalloc (40);
1419 if (id == 0)
1421 index = 0;
1422 *buf = '\0';
1423 definition = FALSE;
1425 else
1427 index = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1428 &size);
1429 if (index < 0)
1430 return FALSE;
1431 sprintf (buf, "%ld=", index);
1432 definition = TRUE;
1435 sprintf (buf + strlen (buf), "%c%u",
1436 structp ? 's' : 'u',
1437 size);
1439 if (! stab_push_string (info, buf, index, definition, size))
1440 return FALSE;
1442 info->type_stack->fields = (char *) xmalloc (1);
1443 info->type_stack->fields[0] = '\0';
1445 return TRUE;
1448 /* Add a field to a struct. */
1450 static bfd_boolean
1451 stab_struct_field (p, name, bitpos, bitsize, visibility)
1452 PTR p;
1453 const char *name;
1454 bfd_vma bitpos;
1455 bfd_vma bitsize;
1456 enum debug_visibility visibility;
1458 struct stab_write_handle *info = (struct stab_write_handle *) p;
1459 bfd_boolean definition;
1460 unsigned int size;
1461 char *s, *n;
1462 const char *vis;
1464 definition = info->type_stack->definition;
1465 size = info->type_stack->size;
1466 s = stab_pop_type (info);
1468 /* Add this field to the end of the current struct fields, which is
1469 currently on the top of the stack. */
1471 assert (info->type_stack->fields != NULL);
1472 n = (char *) xmalloc (strlen (info->type_stack->fields)
1473 + strlen (name)
1474 + strlen (s)
1475 + 50);
1477 switch (visibility)
1479 default:
1480 abort ();
1482 case DEBUG_VISIBILITY_PUBLIC:
1483 vis = "";
1484 break;
1486 case DEBUG_VISIBILITY_PRIVATE:
1487 vis = "/0";
1488 break;
1490 case DEBUG_VISIBILITY_PROTECTED:
1491 vis = "/1";
1492 break;
1495 if (bitsize == 0)
1497 bitsize = size * 8;
1498 if (bitsize == 0)
1499 non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
1500 bfd_get_filename (info->abfd), name);
1503 sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1504 (long) bitpos, (long) bitsize);
1506 free (info->type_stack->fields);
1507 info->type_stack->fields = n;
1509 if (definition)
1510 info->type_stack->definition = TRUE;
1512 return TRUE;
1515 /* Finish up a struct. */
1517 static bfd_boolean
1518 stab_end_struct_type (p)
1519 PTR p;
1521 struct stab_write_handle *info = (struct stab_write_handle *) p;
1522 bfd_boolean definition;
1523 long index;
1524 unsigned int size;
1525 char *fields, *first, *buf;
1527 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1529 definition = info->type_stack->definition;
1530 index = info->type_stack->index;
1531 size = info->type_stack->size;
1532 fields = info->type_stack->fields;
1533 first = stab_pop_type (info);
1535 buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1536 sprintf (buf, "%s%s;", first, fields);
1537 free (first);
1538 free (fields);
1540 if (! stab_push_string (info, buf, index, definition, size))
1541 return FALSE;
1543 free (buf);
1545 return TRUE;
1548 /* Start outputting a class. */
1550 static bfd_boolean
1551 stab_start_class_type (p, tag, id, structp, size, vptr, ownvptr)
1552 PTR p;
1553 const char *tag;
1554 unsigned int id;
1555 bfd_boolean structp;
1556 unsigned int size;
1557 bfd_boolean vptr;
1558 bfd_boolean ownvptr;
1560 struct stab_write_handle *info = (struct stab_write_handle *) p;
1561 bfd_boolean definition;
1562 char *vstring;
1564 if (! vptr || ownvptr)
1566 definition = FALSE;
1567 vstring = NULL;
1569 else
1571 definition = info->type_stack->definition;
1572 vstring = stab_pop_type (info);
1575 if (! stab_start_struct_type (p, tag, id, structp, size))
1576 return FALSE;
1578 if (vptr)
1580 char *vtable;
1582 if (ownvptr)
1584 assert (info->type_stack->index > 0);
1585 vtable = (char *) xmalloc (20);
1586 sprintf (vtable, "~%%%ld", info->type_stack->index);
1588 else
1590 vtable = (char *) xmalloc (strlen (vstring) + 3);
1591 sprintf (vtable, "~%%%s", vstring);
1592 free (vstring);
1595 info->type_stack->vtable = vtable;
1598 if (definition)
1599 info->type_stack->definition = TRUE;
1601 return TRUE;
1604 /* Add a static member to the class on the type stack. */
1606 static bfd_boolean
1607 stab_class_static_member (p, name, physname, visibility)
1608 PTR p;
1609 const char *name;
1610 const char *physname;
1611 enum debug_visibility visibility;
1613 struct stab_write_handle *info = (struct stab_write_handle *) p;
1614 bfd_boolean definition;
1615 char *s, *n;
1616 const char *vis;
1618 definition = info->type_stack->definition;
1619 s = stab_pop_type (info);
1621 /* Add this field to the end of the current struct fields, which is
1622 currently on the top of the stack. */
1624 assert (info->type_stack->fields != NULL);
1625 n = (char *) xmalloc (strlen (info->type_stack->fields)
1626 + strlen (name)
1627 + strlen (s)
1628 + strlen (physname)
1629 + 10);
1631 switch (visibility)
1633 default:
1634 abort ();
1636 case DEBUG_VISIBILITY_PUBLIC:
1637 vis = "";
1638 break;
1640 case DEBUG_VISIBILITY_PRIVATE:
1641 vis = "/0";
1642 break;
1644 case DEBUG_VISIBILITY_PROTECTED:
1645 vis = "/1";
1646 break;
1649 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1650 physname);
1652 free (info->type_stack->fields);
1653 info->type_stack->fields = n;
1655 if (definition)
1656 info->type_stack->definition = TRUE;
1658 return TRUE;
1661 /* Add a base class to the class on the type stack. */
1663 static bfd_boolean
1664 stab_class_baseclass (p, bitpos, virtual, visibility)
1665 PTR p;
1666 bfd_vma bitpos;
1667 bfd_boolean virtual;
1668 enum debug_visibility visibility;
1670 struct stab_write_handle *info = (struct stab_write_handle *) p;
1671 bfd_boolean definition;
1672 char *s;
1673 char *buf;
1674 unsigned int c;
1675 char **baseclasses;
1677 definition = info->type_stack->definition;
1678 s = stab_pop_type (info);
1680 /* Build the base class specifier. */
1682 buf = (char *) xmalloc (strlen (s) + 25);
1683 buf[0] = virtual ? '1' : '0';
1684 switch (visibility)
1686 default:
1687 abort ();
1689 case DEBUG_VISIBILITY_PRIVATE:
1690 buf[1] = '0';
1691 break;
1693 case DEBUG_VISIBILITY_PROTECTED:
1694 buf[1] = '1';
1695 break;
1697 case DEBUG_VISIBILITY_PUBLIC:
1698 buf[1] = '2';
1699 break;
1702 sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1703 free (s);
1705 /* Add the new baseclass to the existing ones. */
1707 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1709 if (info->type_stack->baseclasses == NULL)
1710 c = 0;
1711 else
1713 c = 0;
1714 while (info->type_stack->baseclasses[c] != NULL)
1715 ++c;
1718 baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1719 (c + 2) * sizeof (*baseclasses));
1720 baseclasses[c] = buf;
1721 baseclasses[c + 1] = NULL;
1723 info->type_stack->baseclasses = baseclasses;
1725 if (definition)
1726 info->type_stack->definition = TRUE;
1728 return TRUE;
1731 /* Start adding a method to the class on the type stack. */
1733 static bfd_boolean
1734 stab_class_start_method (p, name)
1735 PTR p;
1736 const char *name;
1738 struct stab_write_handle *info = (struct stab_write_handle *) p;
1739 char *m;
1741 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1743 if (info->type_stack->methods == NULL)
1745 m = (char *) xmalloc (strlen (name) + 3);
1746 *m = '\0';
1748 else
1750 m = (char *) xrealloc (info->type_stack->methods,
1751 (strlen (info->type_stack->methods)
1752 + strlen (name)
1753 + 4));
1756 sprintf (m + strlen (m), "%s::", name);
1758 info->type_stack->methods = m;
1760 return TRUE;
1763 /* Add a variant, either static or not, to the current method. */
1765 static bfd_boolean
1766 stab_class_method_var (info, physname, visibility, staticp, constp, volatilep,
1767 voffset, contextp)
1768 struct stab_write_handle *info;
1769 const char *physname;
1770 enum debug_visibility visibility;
1771 bfd_boolean staticp;
1772 bfd_boolean constp;
1773 bfd_boolean volatilep;
1774 bfd_vma voffset;
1775 bfd_boolean contextp;
1777 bfd_boolean definition;
1778 char *type;
1779 char *context = NULL;
1780 char visc, qualc, typec;
1782 definition = info->type_stack->definition;
1783 type = stab_pop_type (info);
1785 if (contextp)
1787 definition = definition || info->type_stack->definition;
1788 context = stab_pop_type (info);
1791 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1793 switch (visibility)
1795 default:
1796 abort ();
1798 case DEBUG_VISIBILITY_PRIVATE:
1799 visc = '0';
1800 break;
1802 case DEBUG_VISIBILITY_PROTECTED:
1803 visc = '1';
1804 break;
1806 case DEBUG_VISIBILITY_PUBLIC:
1807 visc = '2';
1808 break;
1811 if (constp)
1813 if (volatilep)
1814 qualc = 'D';
1815 else
1816 qualc = 'B';
1818 else
1820 if (volatilep)
1821 qualc = 'C';
1822 else
1823 qualc = 'A';
1826 if (staticp)
1827 typec = '?';
1828 else if (! contextp)
1829 typec = '.';
1830 else
1831 typec = '*';
1833 info->type_stack->methods =
1834 (char *) xrealloc (info->type_stack->methods,
1835 (strlen (info->type_stack->methods)
1836 + strlen (type)
1837 + strlen (physname)
1838 + (contextp ? strlen (context) : 0)
1839 + 40));
1841 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1842 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1843 free (type);
1845 if (contextp)
1847 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1848 "%ld;%s;", (long) voffset, context);
1849 free (context);
1852 if (definition)
1853 info->type_stack->definition = TRUE;
1855 return TRUE;
1858 /* Add a variant to the current method. */
1860 static bfd_boolean
1861 stab_class_method_variant (p, physname, visibility, constp, volatilep,
1862 voffset, contextp)
1863 PTR p;
1864 const char *physname;
1865 enum debug_visibility visibility;
1866 bfd_boolean constp;
1867 bfd_boolean volatilep;
1868 bfd_vma voffset;
1869 bfd_boolean contextp;
1871 struct stab_write_handle *info = (struct stab_write_handle *) p;
1873 return stab_class_method_var (info, physname, visibility, FALSE, constp,
1874 volatilep, voffset, contextp);
1877 /* Add a static variant to the current method. */
1879 static bfd_boolean
1880 stab_class_static_method_variant (p, physname, visibility, constp, volatilep)
1881 PTR p;
1882 const char *physname;
1883 enum debug_visibility visibility;
1884 bfd_boolean constp;
1885 bfd_boolean volatilep;
1887 struct stab_write_handle *info = (struct stab_write_handle *) p;
1889 return stab_class_method_var (info, physname, visibility, TRUE, constp,
1890 volatilep, 0, FALSE);
1893 /* Finish up a method. */
1895 static bfd_boolean
1896 stab_class_end_method (p)
1897 PTR p;
1899 struct stab_write_handle *info = (struct stab_write_handle *) p;
1901 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1903 /* We allocated enough room on info->type_stack->methods to add the
1904 trailing semicolon. */
1905 strcat (info->type_stack->methods, ";");
1907 return TRUE;
1910 /* Finish up a class. */
1912 static bfd_boolean
1913 stab_end_class_type (p)
1914 PTR p;
1916 struct stab_write_handle *info = (struct stab_write_handle *) p;
1917 size_t len;
1918 unsigned int i = 0;
1919 char *buf;
1921 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1923 /* Work out the size we need to allocate for the class definition. */
1925 len = (strlen (info->type_stack->string)
1926 + strlen (info->type_stack->fields)
1927 + 10);
1928 if (info->type_stack->baseclasses != NULL)
1930 len += 20;
1931 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1932 len += strlen (info->type_stack->baseclasses[i]);
1934 if (info->type_stack->methods != NULL)
1935 len += strlen (info->type_stack->methods);
1936 if (info->type_stack->vtable != NULL)
1937 len += strlen (info->type_stack->vtable);
1939 /* Build the class definition. */
1941 buf = (char *) xmalloc (len);
1943 strcpy (buf, info->type_stack->string);
1945 if (info->type_stack->baseclasses != NULL)
1947 sprintf (buf + strlen (buf), "!%u,", i);
1948 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1950 strcat (buf, info->type_stack->baseclasses[i]);
1951 free (info->type_stack->baseclasses[i]);
1953 free (info->type_stack->baseclasses);
1954 info->type_stack->baseclasses = NULL;
1957 strcat (buf, info->type_stack->fields);
1958 free (info->type_stack->fields);
1959 info->type_stack->fields = NULL;
1961 if (info->type_stack->methods != NULL)
1963 strcat (buf, info->type_stack->methods);
1964 free (info->type_stack->methods);
1965 info->type_stack->methods = NULL;
1968 strcat (buf, ";");
1970 if (info->type_stack->vtable != NULL)
1972 strcat (buf, info->type_stack->vtable);
1973 free (info->type_stack->vtable);
1974 info->type_stack->vtable = NULL;
1977 /* Replace the string on the top of the stack with the complete
1978 class definition. */
1979 free (info->type_stack->string);
1980 info->type_stack->string = buf;
1982 return TRUE;
1985 /* Push a typedef which was previously defined. */
1987 static bfd_boolean
1988 stab_typedef_type (p, name)
1989 PTR p;
1990 const char *name;
1992 struct stab_write_handle *info = (struct stab_write_handle *) p;
1993 struct string_hash_entry *h;
1995 h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
1996 assert (h != NULL && h->index > 0);
1998 return stab_push_defined_type (info, h->index, h->size);
2001 /* Push a struct, union or class tag. */
2003 static bfd_boolean
2004 stab_tag_type (p, name, id, kind)
2005 PTR p;
2006 const char *name;
2007 unsigned int id;
2008 enum debug_type_kind kind;
2010 struct stab_write_handle *info = (struct stab_write_handle *) p;
2011 long index;
2012 unsigned int size;
2014 index = stab_get_struct_index (info, name, id, kind, &size);
2015 if (index < 0)
2016 return FALSE;
2018 return stab_push_defined_type (info, index, size);
2021 /* Define a typedef. */
2023 static bfd_boolean
2024 stab_typdef (p, name)
2025 PTR p;
2026 const char *name;
2028 struct stab_write_handle *info = (struct stab_write_handle *) p;
2029 long index;
2030 unsigned int size;
2031 char *s, *buf;
2032 struct string_hash_entry *h;
2034 index = info->type_stack->index;
2035 size = info->type_stack->size;
2036 s = stab_pop_type (info);
2038 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2040 if (index > 0)
2041 sprintf (buf, "%s:t%s", name, s);
2042 else
2044 index = info->type_index;
2045 ++info->type_index;
2046 sprintf (buf, "%s:t%ld=%s", name, index, s);
2049 free (s);
2051 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2052 return FALSE;
2054 free (buf);
2056 h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
2057 if (h == NULL)
2059 non_fatal (_("string_hash_lookup failed: %s"),
2060 bfd_errmsg (bfd_get_error ()));
2061 return FALSE;
2064 /* I don't think we care about redefinitions. */
2066 h->index = index;
2067 h->size = size;
2069 return TRUE;
2072 /* Define a tag. */
2074 static bfd_boolean
2075 stab_tag (p, tag)
2076 PTR p;
2077 const char *tag;
2079 struct stab_write_handle *info = (struct stab_write_handle *) p;
2080 char *s, *buf;
2082 s = stab_pop_type (info);
2084 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
2086 sprintf (buf, "%s:T%s", tag, s);
2087 free (s);
2089 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2090 return FALSE;
2092 free (buf);
2094 return TRUE;
2097 /* Define an integer constant. */
2099 static bfd_boolean
2100 stab_int_constant (p, name, val)
2101 PTR p;
2102 const char *name;
2103 bfd_vma val;
2105 struct stab_write_handle *info = (struct stab_write_handle *) p;
2106 char *buf;
2108 buf = (char *) xmalloc (strlen (name) + 20);
2109 sprintf (buf, "%s:c=i%ld", name, (long) val);
2111 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2112 return FALSE;
2114 free (buf);
2116 return TRUE;
2119 /* Define a floating point constant. */
2121 static bfd_boolean
2122 stab_float_constant (p, name, val)
2123 PTR p;
2124 const char *name;
2125 double val;
2127 struct stab_write_handle *info = (struct stab_write_handle *) p;
2128 char *buf;
2130 buf = (char *) xmalloc (strlen (name) + 20);
2131 sprintf (buf, "%s:c=f%g", name, val);
2133 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2134 return FALSE;
2136 free (buf);
2138 return TRUE;
2141 /* Define a typed constant. */
2143 static bfd_boolean
2144 stab_typed_constant (p, name, val)
2145 PTR p;
2146 const char *name;
2147 bfd_vma val;
2149 struct stab_write_handle *info = (struct stab_write_handle *) p;
2150 char *s, *buf;
2152 s = stab_pop_type (info);
2154 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2155 sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
2156 free (s);
2158 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2159 return FALSE;
2161 free (buf);
2163 return TRUE;
2166 /* Record a variable. */
2168 static bfd_boolean
2169 stab_variable (p, name, kind, val)
2170 PTR p;
2171 const char *name;
2172 enum debug_var_kind kind;
2173 bfd_vma val;
2175 struct stab_write_handle *info = (struct stab_write_handle *) p;
2176 char *s, *buf;
2177 int stab_type;
2178 const char *kindstr;
2180 s = stab_pop_type (info);
2182 switch (kind)
2184 default:
2185 abort ();
2187 case DEBUG_GLOBAL:
2188 stab_type = N_GSYM;
2189 kindstr = "G";
2190 break;
2192 case DEBUG_STATIC:
2193 stab_type = N_STSYM;
2194 kindstr = "S";
2195 break;
2197 case DEBUG_LOCAL_STATIC:
2198 stab_type = N_STSYM;
2199 kindstr = "V";
2200 break;
2202 case DEBUG_LOCAL:
2203 stab_type = N_LSYM;
2204 kindstr = "";
2206 /* Make sure that this is a type reference or definition. */
2207 if (! ISDIGIT (*s))
2209 char *n;
2210 long index;
2212 index = info->type_index;
2213 ++info->type_index;
2214 n = (char *) xmalloc (strlen (s) + 20);
2215 sprintf (n, "%ld=%s", index, s);
2216 free (s);
2217 s = n;
2219 break;
2221 case DEBUG_REGISTER:
2222 stab_type = N_RSYM;
2223 kindstr = "r";
2224 break;
2227 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2228 sprintf (buf, "%s:%s%s", name, kindstr, s);
2229 free (s);
2231 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2232 return FALSE;
2234 free (buf);
2236 return TRUE;
2239 /* Start outputting a function. */
2241 static bfd_boolean
2242 stab_start_function (p, name, globalp)
2243 PTR p;
2244 const char *name;
2245 bfd_boolean globalp;
2247 struct stab_write_handle *info = (struct stab_write_handle *) p;
2248 char *rettype, *buf;
2250 assert (info->nesting == 0 && info->fun_offset == -1);
2252 rettype = stab_pop_type (info);
2254 buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2255 sprintf (buf, "%s:%c%s", name,
2256 globalp ? 'F' : 'f',
2257 rettype);
2259 /* We don't know the value now, so we set it in start_block. */
2260 info->fun_offset = info->symbols_size;
2262 if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2263 return FALSE;
2265 free (buf);
2267 return TRUE;
2270 /* Output a function parameter. */
2272 static bfd_boolean
2273 stab_function_parameter (p, name, kind, val)
2274 PTR p;
2275 const char *name;
2276 enum debug_parm_kind kind;
2277 bfd_vma val;
2279 struct stab_write_handle *info = (struct stab_write_handle *) p;
2280 char *s, *buf;
2281 int stab_type;
2282 char kindc;
2284 s = stab_pop_type (info);
2286 switch (kind)
2288 default:
2289 abort ();
2291 case DEBUG_PARM_STACK:
2292 stab_type = N_PSYM;
2293 kindc = 'p';
2294 break;
2296 case DEBUG_PARM_REG:
2297 stab_type = N_RSYM;
2298 kindc = 'P';
2299 break;
2301 case DEBUG_PARM_REFERENCE:
2302 stab_type = N_PSYM;
2303 kindc = 'v';
2304 break;
2306 case DEBUG_PARM_REF_REG:
2307 stab_type = N_RSYM;
2308 kindc = 'a';
2309 break;
2312 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2313 sprintf (buf, "%s:%c%s", name, kindc, s);
2314 free (s);
2316 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2317 return FALSE;
2319 free (buf);
2321 return TRUE;
2324 /* Start a block. */
2326 static bfd_boolean
2327 stab_start_block (p, addr)
2328 PTR p;
2329 bfd_vma addr;
2331 struct stab_write_handle *info = (struct stab_write_handle *) p;
2333 /* Fill in any slots which have been waiting for the first known
2334 text address. */
2336 if (info->so_offset != -1)
2338 bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2339 info->so_offset = -1;
2342 if (info->fun_offset != -1)
2344 bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2345 info->fun_offset = -1;
2348 ++info->nesting;
2350 /* We will be called with a top level block surrounding the
2351 function, but stabs information does not output that block, so we
2352 ignore it. */
2354 if (info->nesting == 1)
2356 info->fnaddr = addr;
2357 return TRUE;
2360 /* We have to output the LBRAC symbol after any variables which are
2361 declared inside the block. We postpone the LBRAC until the next
2362 start_block or end_block. */
2364 /* If we have postponed an LBRAC, output it now. */
2365 if (info->pending_lbrac != (bfd_vma) -1)
2367 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2368 (const char *) NULL))
2369 return FALSE;
2372 /* Remember the address and output it later. */
2374 info->pending_lbrac = addr - info->fnaddr;
2376 return TRUE;
2379 /* End a block. */
2381 static bfd_boolean
2382 stab_end_block (p, addr)
2383 PTR p;
2384 bfd_vma addr;
2386 struct stab_write_handle *info = (struct stab_write_handle *) p;
2388 if (addr > info->last_text_address)
2389 info->last_text_address = addr;
2391 /* If we have postponed an LBRAC, output it now. */
2392 if (info->pending_lbrac != (bfd_vma) -1)
2394 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2395 (const char *) NULL))
2396 return FALSE;
2397 info->pending_lbrac = (bfd_vma) -1;
2400 assert (info->nesting > 0);
2402 --info->nesting;
2404 /* We ignore the outermost block. */
2405 if (info->nesting == 0)
2406 return TRUE;
2408 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2409 (const char *) NULL);
2412 /* End a function. */
2414 static bfd_boolean
2415 stab_end_function (p)
2416 PTR p ATTRIBUTE_UNUSED;
2418 return TRUE;
2421 /* Output a line number. */
2423 static bfd_boolean
2424 stab_lineno (p, file, lineno, addr)
2425 PTR p;
2426 const char *file;
2427 unsigned long lineno;
2428 bfd_vma addr;
2430 struct stab_write_handle *info = (struct stab_write_handle *) p;
2432 assert (info->lineno_filename != NULL);
2434 if (addr > info->last_text_address)
2435 info->last_text_address = addr;
2437 if (strcmp (file, info->lineno_filename) != 0)
2439 if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2440 return FALSE;
2441 info->lineno_filename = file;
2444 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2445 (const char *) NULL);