wrstabs.c revision 1.5 1 /* wrstabs.c -- Output stabs debugging information
2 Copyright (C) 1996-2016 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor <ian (at) cygnus.com>.
4
5 This file is part of GNU Binutils.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20 02110-1301, USA. */
21
22 /* This file contains code which writes out stabs debugging
23 information. */
24
25 #include "sysdep.h"
26 #include <assert.h>
27 #include "bfd.h"
28 #include "libiberty.h"
29 #include "filenames.h"
30 #include "safe-ctype.h"
31 #include "bucomm.h"
32 #include "debug.h"
33 #include "budbg.h"
34 #include "aout/aout64.h"
35 #include "aout/stab_gnu.h"
36
37 /* The size of a stabs symbol. This presumes 32 bit values. */
38
39 #define STAB_SYMBOL_SIZE (12)
40
41 /* An entry in a string hash table. */
42
43 struct string_hash_entry
44 {
45 struct bfd_hash_entry root;
46 /* Next string in this table. */
47 struct string_hash_entry *next;
48 /* Index in string table. */
49 long index;
50 /* Size of type if this is a typedef. */
51 unsigned int size;
52 };
53
54 /* A string hash table. */
55
56 struct string_hash_table
57 {
58 struct bfd_hash_table table;
59 };
60
61 /* The type stack. Each element on the stack is a string. */
62
63 struct stab_type_stack
64 {
65 /* The next element on the stack. */
66 struct stab_type_stack *next;
67 /* This element as a string. */
68 char *string;
69 /* The type index of this element. */
70 long index;
71 /* The size of the type. */
72 unsigned int size;
73 /* Whether type string defines a new type. */
74 bfd_boolean definition;
75 /* String defining struct fields. */
76 char *fields;
77 /* NULL terminated array of strings defining base classes for a
78 class. */
79 char **baseclasses;
80 /* String defining class methods. */
81 char *methods;
82 /* String defining vtable pointer for a class. */
83 char *vtable;
84 };
85
86 /* This structure is used to keep track of type indices for tagged
87 types. */
88
89 struct stab_tag
90 {
91 /* The type index. */
92 long index;
93 /* The tag name. */
94 const char *tag;
95 /* The kind of type. This is set to DEBUG_KIND_ILLEGAL when the
96 type is defined. */
97 enum debug_type_kind kind;
98 /* The size of the struct. */
99 unsigned int size;
100 };
101
102 /* We remember various sorts of type indices. They are not related,
103 but, for convenience, we keep all the information in this
104 structure. */
105
106 struct stab_type_cache
107 {
108 /* The void type index. */
109 long void_type;
110 /* Signed integer type indices, indexed by size - 1. */
111 long signed_integer_types[8];
112 /* Unsigned integer type indices, indexed by size - 1. */
113 long unsigned_integer_types[8];
114 /* Floating point types, indexed by size - 1. */
115 long float_types[16];
116 /* Pointers to types, indexed by the type index. */
117 long *pointer_types;
118 size_t pointer_types_alloc;
119 /* Functions returning types, indexed by the type index. */
120 long *function_types;
121 size_t function_types_alloc;
122 /* References to types, indexed by the type index. */
123 long *reference_types;
124 size_t reference_types_alloc;
125 /* Struct/union/class type indices, indexed by the struct id. */
126 struct stab_tag *struct_types;
127 size_t struct_types_alloc;
128 };
129
130 /* This is the handle passed through debug_write. */
131
132 struct stab_write_handle
133 {
134 /* The BFD. */
135 bfd *abfd;
136 /* This buffer holds the symbols. */
137 bfd_byte *symbols;
138 size_t symbols_size;
139 size_t symbols_alloc;
140 /* This is a list of hash table entries for the strings. */
141 struct string_hash_entry *strings;
142 /* The last string hash table entry. */
143 struct string_hash_entry *last_string;
144 /* The size of the strings. */
145 size_t strings_size;
146 /* This hash table eliminates duplicate strings. */
147 struct string_hash_table strhash;
148 /* The type stack. */
149 struct stab_type_stack *type_stack;
150 /* The next type index. */
151 long type_index;
152 /* The type cache. */
153 struct stab_type_cache type_cache;
154 /* A mapping from typedef names to type indices. */
155 struct string_hash_table typedef_hash;
156 /* If this is not -1, it is the offset to the most recent N_SO
157 symbol, and the value of that symbol needs to be set. */
158 long so_offset;
159 /* If this is not -1, it is the offset to the most recent N_FUN
160 symbol, and the value of that symbol needs to be set. */
161 long fun_offset;
162 /* The last text section address seen. */
163 bfd_vma last_text_address;
164 /* The block nesting depth. */
165 unsigned int nesting;
166 /* The function address. */
167 bfd_vma fnaddr;
168 /* A pending LBRAC symbol. */
169 bfd_vma pending_lbrac;
170 /* The current line number file name. */
171 const char *lineno_filename;
172 };
173
174 static struct bfd_hash_entry *string_hash_newfunc
175 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
176 static bfd_boolean stab_write_symbol
177 (struct stab_write_handle *, int, int, bfd_vma, const char *);
178 static bfd_boolean stab_push_string
179 (struct stab_write_handle *, const char *, long, bfd_boolean, unsigned int);
180 static bfd_boolean stab_push_defined_type
181 (struct stab_write_handle *, long, unsigned int);
182 static char *stab_pop_type (struct stab_write_handle *);
183 static bfd_boolean stab_modify_type
184 (struct stab_write_handle *, int, unsigned int, long **, size_t *);
185 static long stab_get_struct_index
186 (struct stab_write_handle *, const char *, unsigned int,
187 enum debug_type_kind, unsigned int *);
188 static bfd_boolean stab_class_method_var
189 (struct stab_write_handle *, const char *, enum debug_visibility,
190 bfd_boolean, bfd_boolean, bfd_boolean, bfd_vma, bfd_boolean);
191 static bfd_boolean stab_start_compilation_unit (void *, const char *);
192 static bfd_boolean stab_start_source (void *, const char *);
193 static bfd_boolean stab_empty_type (void *);
194 static bfd_boolean stab_void_type (void *);
195 static bfd_boolean stab_int_type (void *, unsigned int, bfd_boolean);
196 static bfd_boolean stab_float_type (void *, unsigned int);
197 static bfd_boolean stab_complex_type (void *, unsigned int);
198 static bfd_boolean stab_bool_type (void *, unsigned int);
199 static bfd_boolean stab_enum_type
200 (void *, const char *, const char **, bfd_signed_vma *);
201 static bfd_boolean stab_pointer_type (void *);
202 static bfd_boolean stab_function_type (void *, int, bfd_boolean);
203 static bfd_boolean stab_reference_type (void *);
204 static bfd_boolean stab_range_type (void *, bfd_signed_vma, bfd_signed_vma);
205 static bfd_boolean stab_array_type
206 (void *, bfd_signed_vma, bfd_signed_vma, bfd_boolean);
207 static bfd_boolean stab_set_type (void *, bfd_boolean);
208 static bfd_boolean stab_offset_type (void *);
209 static bfd_boolean stab_method_type (void *, bfd_boolean, int, bfd_boolean);
210 static bfd_boolean stab_const_type (void *);
211 static bfd_boolean stab_volatile_type (void *);
212 static bfd_boolean stab_start_struct_type
213 (void *, const char *, unsigned int, bfd_boolean, unsigned int);
214 static bfd_boolean stab_struct_field
215 (void *, const char *, bfd_vma, bfd_vma, enum debug_visibility);
216 static bfd_boolean stab_end_struct_type (void *);
217 static bfd_boolean stab_start_class_type
218 (void *, const char *, unsigned int, bfd_boolean, unsigned int,
219 bfd_boolean, bfd_boolean);
220 static bfd_boolean stab_class_static_member
221 (void *, const char *, const char *, enum debug_visibility);
222 static bfd_boolean stab_class_baseclass
223 (void *, bfd_vma, bfd_boolean, enum debug_visibility);
224 static bfd_boolean stab_class_start_method (void *, const char *);
225 static bfd_boolean stab_class_method_variant
226 (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean,
227 bfd_vma, bfd_boolean);
228 static bfd_boolean stab_class_static_method_variant
229 (void *, const char *, enum debug_visibility, bfd_boolean, bfd_boolean);
230 static bfd_boolean stab_class_end_method (void *);
231 static bfd_boolean stab_end_class_type (void *);
232 static bfd_boolean stab_typedef_type (void *, const char *);
233 static bfd_boolean stab_tag_type
234 (void *, const char *, unsigned int, enum debug_type_kind);
235 static bfd_boolean stab_typdef (void *, const char *);
236 static bfd_boolean stab_tag (void *, const char *);
237 static bfd_boolean stab_int_constant (void *, const char *, bfd_vma);
238 static bfd_boolean stab_float_constant (void *, const char *, double);
239 static bfd_boolean stab_typed_constant (void *, const char *, bfd_vma);
240 static bfd_boolean stab_variable
241 (void *, const char *, enum debug_var_kind, bfd_vma);
242 static bfd_boolean stab_start_function (void *, const char *, bfd_boolean);
243 static bfd_boolean stab_function_parameter
244 (void *, const char *, enum debug_parm_kind, bfd_vma);
245 static bfd_boolean stab_start_block (void *, bfd_vma);
246 static bfd_boolean stab_end_block (void *, bfd_vma);
247 static bfd_boolean stab_end_function (void *);
248 static bfd_boolean stab_lineno (void *, const char *, unsigned long, bfd_vma);
249
250 static const struct debug_write_fns stab_fns =
251 {
252 stab_start_compilation_unit,
253 stab_start_source,
254 stab_empty_type,
255 stab_void_type,
256 stab_int_type,
257 stab_float_type,
258 stab_complex_type,
259 stab_bool_type,
260 stab_enum_type,
261 stab_pointer_type,
262 stab_function_type,
263 stab_reference_type,
264 stab_range_type,
265 stab_array_type,
266 stab_set_type,
267 stab_offset_type,
268 stab_method_type,
269 stab_const_type,
270 stab_volatile_type,
271 stab_start_struct_type,
272 stab_struct_field,
273 stab_end_struct_type,
274 stab_start_class_type,
275 stab_class_static_member,
276 stab_class_baseclass,
277 stab_class_start_method,
278 stab_class_method_variant,
279 stab_class_static_method_variant,
280 stab_class_end_method,
281 stab_end_class_type,
282 stab_typedef_type,
283 stab_tag_type,
284 stab_typdef,
285 stab_tag,
286 stab_int_constant,
287 stab_float_constant,
288 stab_typed_constant,
289 stab_variable,
290 stab_start_function,
291 stab_function_parameter,
292 stab_start_block,
293 stab_end_block,
294 stab_end_function,
295 stab_lineno
296 };
297
298 /* Routine to create an entry in a string hash table. */
300
301 static struct bfd_hash_entry *
302 string_hash_newfunc (struct bfd_hash_entry *entry,
303 struct bfd_hash_table *table, const char *string)
304 {
305 struct string_hash_entry *ret = (struct string_hash_entry *) entry;
306
307 /* Allocate the structure if it has not already been allocated by a
308 subclass. */
309 if (ret == (struct string_hash_entry *) NULL)
310 ret = ((struct string_hash_entry *)
311 bfd_hash_allocate (table, sizeof (struct string_hash_entry)));
312 if (ret == (struct string_hash_entry *) NULL)
313 return NULL;
314
315 /* Call the allocation method of the superclass. */
316 ret = ((struct string_hash_entry *)
317 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
318
319 if (ret)
320 {
321 /* Initialize the local fields. */
322 ret->next = NULL;
323 ret->index = -1;
324 ret->size = 0;
325 }
326
327 return (struct bfd_hash_entry *) ret;
328 }
329
330 /* Look up an entry in a string hash table. */
331
332 #define string_hash_lookup(t, string, create, copy) \
333 ((struct string_hash_entry *) \
334 bfd_hash_lookup (&(t)->table, (string), (create), (copy)))
335
336 /* Add a symbol to the stabs debugging information we are building. */
337
338 static bfd_boolean
339 stab_write_symbol (struct stab_write_handle *info, int type, int desc,
340 bfd_vma value, const char *string)
341 {
342 bfd_size_type strx;
343 bfd_byte sym[STAB_SYMBOL_SIZE];
344
345 if (string == NULL)
346 strx = 0;
347 else
348 {
349 struct string_hash_entry *h;
350
351 h = string_hash_lookup (&info->strhash, string, TRUE, TRUE);
352 if (h == NULL)
353 {
354 non_fatal (_("string_hash_lookup failed: %s"),
355 bfd_errmsg (bfd_get_error ()));
356 return FALSE;
357 }
358 if (h->index != -1)
359 strx = h->index;
360 else
361 {
362 strx = info->strings_size;
363 h->index = strx;
364 if (info->last_string == NULL)
365 info->strings = h;
366 else
367 info->last_string->next = h;
368 info->last_string = h;
369 info->strings_size += strlen (string) + 1;
370 }
371 }
372
373 /* This presumes 32 bit values. */
374 bfd_put_32 (info->abfd, strx, sym);
375 bfd_put_8 (info->abfd, type, sym + 4);
376 bfd_put_8 (info->abfd, 0, sym + 5);
377 bfd_put_16 (info->abfd, desc, sym + 6);
378 bfd_put_32 (info->abfd, value, sym + 8);
379
380 if (info->symbols_size + STAB_SYMBOL_SIZE > info->symbols_alloc)
381 {
382 info->symbols_alloc *= 2;
383 info->symbols = (bfd_byte *) xrealloc (info->symbols,
384 info->symbols_alloc);
385 }
386
387 memcpy (info->symbols + info->symbols_size, sym, STAB_SYMBOL_SIZE);
388
389 info->symbols_size += STAB_SYMBOL_SIZE;
390
391 return TRUE;
392 }
393
394 /* Push a string on to the type stack. */
395
396 static bfd_boolean
397 stab_push_string (struct stab_write_handle *info, const char *string,
398 long tindex, bfd_boolean definition, unsigned int size)
399 {
400 struct stab_type_stack *s;
401
402 s = (struct stab_type_stack *) xmalloc (sizeof *s);
403 s->string = xstrdup (string);
404 s->index = tindex;
405 s->definition = definition;
406 s->size = size;
407
408 s->fields = NULL;
409 s->baseclasses = NULL;
410 s->methods = NULL;
411 s->vtable = NULL;
412
413 s->next = info->type_stack;
414 info->type_stack = s;
415
416 return TRUE;
417 }
418
419 /* Push a type index which has already been defined. */
420
421 static bfd_boolean
422 stab_push_defined_type (struct stab_write_handle *info, long tindex,
423 unsigned int size)
424 {
425 char buf[20];
426
427 sprintf (buf, "%ld", tindex);
428 return stab_push_string (info, buf, tindex, FALSE, size);
429 }
430
431 /* Pop a type off the type stack. The caller is responsible for
432 freeing the string. */
433
434 static char *
435 stab_pop_type (struct stab_write_handle *info)
436 {
437 struct stab_type_stack *s;
438 char *ret;
439
440 s = info->type_stack;
441 assert (s != NULL);
442
443 info->type_stack = s->next;
444
445 ret = s->string;
446
447 free (s);
448
449 return ret;
450 }
451
452 /* The general routine to write out stabs in sections debugging
454 information. This accumulates the stabs symbols and the strings in
455 two obstacks. We can't easily write out the information as we go
456 along, because we need to know the section sizes before we can
457 write out the section contents. ABFD is the BFD and DHANDLE is the
458 handle for the debugging information. This sets *PSYMS to point to
459 the symbols, *PSYMSIZE the size of the symbols, *PSTRINGS to the
460 strings, and *PSTRINGSIZE to the size of the strings. */
461
462 bfd_boolean
463 write_stabs_in_sections_debugging_info (bfd *abfd, void *dhandle,
464 bfd_byte **psyms,
465 bfd_size_type *psymsize,
466 bfd_byte **pstrings,
467 bfd_size_type *pstringsize)
468 {
469 struct stab_write_handle info;
470 struct string_hash_entry *h;
471 bfd_byte *p;
472
473 info.abfd = abfd;
474
475 info.symbols_size = 0;
476 info.symbols_alloc = 500;
477 info.symbols = (bfd_byte *) xmalloc (info.symbols_alloc);
478
479 info.strings = NULL;
480 info.last_string = NULL;
481 /* Reserve 1 byte for a null byte. */
482 info.strings_size = 1;
483
484 if (!bfd_hash_table_init (&info.strhash.table, string_hash_newfunc,
485 sizeof (struct string_hash_entry))
486 || !bfd_hash_table_init (&info.typedef_hash.table, string_hash_newfunc,
487 sizeof (struct string_hash_entry)))
488 {
489 non_fatal ("bfd_hash_table_init_failed: %s",
490 bfd_errmsg (bfd_get_error ()));
491 return FALSE;
492 }
493
494 info.type_stack = NULL;
495 info.type_index = 1;
496 memset (&info.type_cache, 0, sizeof info.type_cache);
497 info.so_offset = -1;
498 info.fun_offset = -1;
499 info.last_text_address = 0;
500 info.nesting = 0;
501 info.fnaddr = 0;
502 info.pending_lbrac = (bfd_vma) -1;
503
504 /* The initial symbol holds the string size. */
505 if (! stab_write_symbol (&info, 0, 0, 0, (const char *) NULL))
506 return FALSE;
507
508 /* Output an initial N_SO symbol. */
509 info.so_offset = info.symbols_size;
510 if (! stab_write_symbol (&info, N_SO, 0, 0, bfd_get_filename (abfd)))
511 return FALSE;
512
513 if (! debug_write (dhandle, &stab_fns, (void *) &info))
514 return FALSE;
515
516 assert (info.pending_lbrac == (bfd_vma) -1);
517
518 /* Output a trailing N_SO. */
519 if (! stab_write_symbol (&info, N_SO, 0, info.last_text_address,
520 (const char *) NULL))
521 return FALSE;
522
523 /* Put the string size in the initial symbol. */
524 bfd_put_32 (abfd, info.strings_size, info.symbols + 8);
525
526 *psyms = info.symbols;
527 *psymsize = info.symbols_size;
528
529 *pstringsize = info.strings_size;
530 *pstrings = (bfd_byte *) xmalloc (info.strings_size);
531
532 p = *pstrings;
533 *p++ = '\0';
534 for (h = info.strings; h != NULL; h = h->next)
535 {
536 strcpy ((char *) p, h->root.string);
537 p += strlen ((char *) p) + 1;
538 }
539
540 return TRUE;
541 }
542
543 /* Start writing out information for a compilation unit. */
544
545 static bfd_boolean
546 stab_start_compilation_unit (void *p, const char *filename)
547 {
548 struct stab_write_handle *info = (struct stab_write_handle *) p;
549
550 /* We would normally output an N_SO symbol here. However, that
551 would force us to reset all of our type information. I think we
552 will be better off just outputting an N_SOL symbol, and not
553 worrying about splitting information between files. */
554
555 info->lineno_filename = filename;
556
557 return stab_write_symbol (info, N_SOL, 0, 0, filename);
558 }
559
560 /* Start writing out information for a particular source file. */
561
562 static bfd_boolean
563 stab_start_source (void *p, const char *filename)
564 {
565 struct stab_write_handle *info = (struct stab_write_handle *) p;
566
567 /* FIXME: The symbol's value is supposed to be the text section
568 address. However, we would have to fill it in later, and gdb
569 doesn't care, so we don't bother with it. */
570
571 info->lineno_filename = filename;
572
573 return stab_write_symbol (info, N_SOL, 0, 0, filename);
574 }
575
576 /* Push an empty type. This shouldn't normally happen. We just use a
577 void type. */
578
579 static bfd_boolean
580 stab_empty_type (void *p)
581 {
582 struct stab_write_handle *info = (struct stab_write_handle *) p;
583
584 /* We don't call stab_void_type if the type is not yet defined,
585 because that might screw up the typedef. */
586
587 if (info->type_cache.void_type != 0)
588 return stab_push_defined_type (info, info->type_cache.void_type, 0);
589 else
590 {
591 long tindex;
592 char buf[40];
593
594 tindex = info->type_index;
595 ++info->type_index;
596
597 sprintf (buf, "%ld=%ld", tindex, tindex);
598
599 return stab_push_string (info, buf, tindex, FALSE, 0);
600 }
601 }
602
603 /* Push a void type. */
604
605 static bfd_boolean
606 stab_void_type (void *p)
607 {
608 struct stab_write_handle *info = (struct stab_write_handle *) p;
609
610 if (info->type_cache.void_type != 0)
611 return stab_push_defined_type (info, info->type_cache.void_type, 0);
612 else
613 {
614 long tindex;
615 char buf[40];
616
617 tindex = info->type_index;
618 ++info->type_index;
619
620 info->type_cache.void_type = tindex;
621
622 sprintf (buf, "%ld=%ld", tindex, tindex);
623
624 return stab_push_string (info, buf, tindex, TRUE, 0);
625 }
626 }
627
628 /* Push an integer type. */
629
630 static bfd_boolean
631 stab_int_type (void *p, unsigned int size, bfd_boolean unsignedp)
632 {
633 struct stab_write_handle *info = (struct stab_write_handle *) p;
634 long *cache;
635
636 if (size <= 0 || (size > sizeof (long) && size != 8))
637 {
638 non_fatal (_("stab_int_type: bad size %u"), size);
639 return FALSE;
640 }
641
642 if (unsignedp)
643 cache = info->type_cache.signed_integer_types;
644 else
645 cache = info->type_cache.unsigned_integer_types;
646
647 if (cache[size - 1] != 0)
648 return stab_push_defined_type (info, cache[size - 1], size);
649 else
650 {
651 long tindex;
652 char buf[100];
653
654 tindex = info->type_index;
655 ++info->type_index;
656
657 cache[size - 1] = tindex;
658
659 sprintf (buf, "%ld=r%ld;", tindex, tindex);
660 if (unsignedp)
661 {
662 strcat (buf, "0;");
663 if (size < sizeof (long))
664 sprintf (buf + strlen (buf), "%ld;", ((long) 1 << (size * 8)) - 1);
665 else if (size == sizeof (long))
666 strcat (buf, "-1;");
667 else if (size == 8)
668 strcat (buf, "01777777777777777777777;");
669 else
670 abort ();
671 }
672 else
673 {
674 if (size <= sizeof (long))
675 sprintf (buf + strlen (buf), "%ld;%ld;",
676 (long) - ((unsigned long) 1 << (size * 8 - 1)),
677 (long) (((unsigned long) 1 << (size * 8 - 1)) - 1));
678 else if (size == 8)
679 strcat (buf, "01000000000000000000000;0777777777777777777777;");
680 else
681 abort ();
682 }
683
684 return stab_push_string (info, buf, tindex, TRUE, size);
685 }
686 }
687
688 /* Push a floating point type. */
689
690 static bfd_boolean
691 stab_float_type (void *p, unsigned int size)
692 {
693 struct stab_write_handle *info = (struct stab_write_handle *) p;
694
695 if (size > 0
696 && size - 1 < (sizeof info->type_cache.float_types
697 / sizeof info->type_cache.float_types[0])
698 && info->type_cache.float_types[size - 1] != 0)
699 return stab_push_defined_type (info,
700 info->type_cache.float_types[size - 1],
701 size);
702 else
703 {
704 long tindex;
705 char *int_type;
706 char buf[50];
707
708 /* Floats are defined as a subrange of int. */
709 if (! stab_int_type (info, 4, FALSE))
710 return FALSE;
711 int_type = stab_pop_type (info);
712
713 tindex = info->type_index;
714 ++info->type_index;
715
716 if (size > 0
717 && size - 1 < (sizeof info->type_cache.float_types
718 / sizeof info->type_cache.float_types[0]))
719 info->type_cache.float_types[size - 1] = tindex;
720
721 sprintf (buf, "%ld=r%s;%u;0;", tindex, int_type, size);
722
723 free (int_type);
724
725 return stab_push_string (info, buf, tindex, TRUE, size);
726 }
727 }
728
729 /* Push a complex type. */
730
731 static bfd_boolean
732 stab_complex_type (void *p, unsigned int size)
733 {
734 struct stab_write_handle *info = (struct stab_write_handle *) p;
735 char buf[50];
736 long tindex;
737
738 tindex = info->type_index;
739 ++info->type_index;
740
741 sprintf (buf, "%ld=r%ld;%u;0;", tindex, tindex, size);
742
743 return stab_push_string (info, buf, tindex, TRUE, size * 2);
744 }
745
746 /* Push a bfd_boolean type. We use an XCOFF predefined type, since gdb
747 always recognizes them. */
748
749 static bfd_boolean
750 stab_bool_type (void *p, unsigned int size)
751 {
752 struct stab_write_handle *info = (struct stab_write_handle *) p;
753 long tindex;
754
755 switch (size)
756 {
757 case 1:
758 tindex = -21;
759 break;
760
761 case 2:
762 tindex = -22;
763 break;
764
765 default:
766 case 4:
767 tindex = -16;
768 break;
769
770 case 8:
771 tindex = -33;
772 break;
773 }
774
775 return stab_push_defined_type (info, tindex, size);
776 }
777
778 /* Push an enum type. */
779
780 static bfd_boolean
781 stab_enum_type (void *p, const char *tag, const char **names,
782 bfd_signed_vma *vals)
783 {
784 struct stab_write_handle *info = (struct stab_write_handle *) p;
785 size_t len;
786 const char **pn;
787 char *buf;
788 long tindex = 0;
789 bfd_signed_vma *pv;
790
791 if (names == NULL)
792 {
793 assert (tag != NULL);
794
795 buf = (char *) xmalloc (10 + strlen (tag));
796 sprintf (buf, "xe%s:", tag);
797 /* FIXME: The size is just a guess. */
798 if (! stab_push_string (info, buf, 0, FALSE, 4))
799 return FALSE;
800 free (buf);
801 return TRUE;
802 }
803
804 len = 10;
805 if (tag != NULL)
806 len += strlen (tag);
807 for (pn = names; *pn != NULL; pn++)
808 len += strlen (*pn) + 20;
809
810 buf = (char *) xmalloc (len);
811
812 if (tag == NULL)
813 strcpy (buf, "e");
814 else
815 {
816 tindex = info->type_index;
817 ++info->type_index;
818 sprintf (buf, "%s:T%ld=e", tag, tindex);
819 }
820
821 for (pn = names, pv = vals; *pn != NULL; pn++, pv++)
822 sprintf (buf + strlen (buf), "%s:%ld,", *pn, (long) *pv);
823 strcat (buf, ";");
824
825 if (tag == NULL)
826 {
827 /* FIXME: The size is just a guess. */
828 if (! stab_push_string (info, buf, 0, FALSE, 4))
829 return FALSE;
830 }
831 else
832 {
833 /* FIXME: The size is just a guess. */
834 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf)
835 || ! stab_push_defined_type (info, tindex, 4))
836 return FALSE;
837 }
838
839 free (buf);
840
841 return TRUE;
842 }
843
844 /* Push a modification of the top type on the stack. Cache the
845 results in CACHE and CACHE_ALLOC. */
846
847 static bfd_boolean
848 stab_modify_type (struct stab_write_handle *info, int mod,
849 unsigned int size, long **cache, size_t *cache_alloc)
850 {
851 long targindex;
852 long tindex;
853 char *s, *buf;
854
855 assert (info->type_stack != NULL);
856 targindex = info->type_stack->index;
857
858 if (targindex <= 0
859 || cache == NULL)
860 {
861 bfd_boolean definition;
862
863 /* Either the target type has no index, or we aren't caching
864 this modifier. Either way we have no way of recording the
865 new type, so we don't bother to define one. */
866 definition = info->type_stack->definition;
867 s = stab_pop_type (info);
868 buf = (char *) xmalloc (strlen (s) + 2);
869 sprintf (buf, "%c%s", mod, s);
870 free (s);
871 if (! stab_push_string (info, buf, 0, definition, size))
872 return FALSE;
873 free (buf);
874 }
875 else
876 {
877 if ((size_t) targindex >= *cache_alloc)
878 {
879 size_t alloc;
880
881 alloc = *cache_alloc;
882 if (alloc == 0)
883 alloc = 10;
884 while ((size_t) targindex >= alloc)
885 alloc *= 2;
886 *cache = (long *) xrealloc (*cache, alloc * sizeof (long));
887 memset (*cache + *cache_alloc, 0,
888 (alloc - *cache_alloc) * sizeof (long));
889 *cache_alloc = alloc;
890 }
891
892 tindex = (*cache)[targindex];
893 if (tindex != 0 && ! info->type_stack->definition)
894 {
895 /* We have already defined a modification of this type, and
896 the entry on the type stack is not a definition, so we
897 can safely discard it (we may have a definition on the
898 stack, even if we already defined a modification, if it
899 is a struct which we did not define at the time it was
900 referenced). */
901 free (stab_pop_type (info));
902 if (! stab_push_defined_type (info, tindex, size))
903 return FALSE;
904 }
905 else
906 {
907 tindex = info->type_index;
908 ++info->type_index;
909
910 s = stab_pop_type (info);
911 buf = (char *) xmalloc (strlen (s) + 20);
912 sprintf (buf, "%ld=%c%s", tindex, mod, s);
913 free (s);
914
915 (*cache)[targindex] = tindex;
916
917 if (! stab_push_string (info, buf, tindex, TRUE, size))
918 return FALSE;
919
920 free (buf);
921 }
922 }
923
924 return TRUE;
925 }
926
927 /* Push a pointer type. */
928
929 static bfd_boolean
930 stab_pointer_type (void *p)
931 {
932 struct stab_write_handle *info = (struct stab_write_handle *) p;
933
934 /* FIXME: The size should depend upon the architecture. */
935 return stab_modify_type (info, '*', 4, &info->type_cache.pointer_types,
936 &info->type_cache.pointer_types_alloc);
937 }
938
939 /* Push a function type. */
940
941 static bfd_boolean
942 stab_function_type (void *p, int argcount,
943 bfd_boolean varargs ATTRIBUTE_UNUSED)
944 {
945 struct stab_write_handle *info = (struct stab_write_handle *) p;
946 int i;
947
948 /* We have no way to represent the argument types, so we just
949 discard them. However, if they define new types, we must output
950 them. We do this by producing empty typedefs. */
951 for (i = 0; i < argcount; i++)
952 {
953 if (! info->type_stack->definition)
954 free (stab_pop_type (info));
955 else
956 {
957 char *s, *buf;
958
959 s = stab_pop_type (info);
960
961 buf = (char *) xmalloc (strlen (s) + 3);
962 sprintf (buf, ":t%s", s);
963 free (s);
964
965 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
966 return FALSE;
967
968 free (buf);
969 }
970 }
971
972 return stab_modify_type (info, 'f', 0, &info->type_cache.function_types,
973 &info->type_cache.function_types_alloc);
974 }
975
976 /* Push a reference type. */
977
978 static bfd_boolean
979 stab_reference_type (void *p)
980 {
981 struct stab_write_handle *info = (struct stab_write_handle *) p;
982
983 /* FIXME: The size should depend upon the architecture. */
984 return stab_modify_type (info, '&', 4, &info->type_cache.reference_types,
985 &info->type_cache.reference_types_alloc);
986 }
987
988 /* Push a range type. */
989
990 static bfd_boolean
991 stab_range_type (void *p, bfd_signed_vma low, bfd_signed_vma high)
992 {
993 struct stab_write_handle *info = (struct stab_write_handle *) p;
994 bfd_boolean definition;
995 unsigned int size;
996 char *s, *buf;
997
998 definition = info->type_stack->definition;
999 size = info->type_stack->size;
1000
1001 s = stab_pop_type (info);
1002 buf = (char *) xmalloc (strlen (s) + 100);
1003 sprintf (buf, "r%s;%ld;%ld;", s, (long) low, (long) high);
1004 free (s);
1005
1006 if (! stab_push_string (info, buf, 0, definition, size))
1007 return FALSE;
1008
1009 free (buf);
1010
1011 return TRUE;
1012 }
1013
1014 /* Push an array type. */
1015
1016 static bfd_boolean
1017 stab_array_type (void *p, bfd_signed_vma low, bfd_signed_vma high,
1018 bfd_boolean stringp)
1019 {
1020 struct stab_write_handle *info = (struct stab_write_handle *) p;
1021 bfd_boolean definition;
1022 unsigned int element_size;
1023 char *range, *element, *buf;
1024 long tindex;
1025 unsigned int size;
1026
1027 definition = info->type_stack->definition;
1028 range = stab_pop_type (info);
1029
1030 definition = definition || info->type_stack->definition;
1031 element_size = info->type_stack->size;
1032 element = stab_pop_type (info);
1033
1034 buf = (char *) xmalloc (strlen (range) + strlen (element) + 100);
1035
1036 if (! stringp)
1037 {
1038 tindex = 0;
1039 *buf = '\0';
1040 }
1041 else
1042 {
1043 /* We need to define a type in order to include the string
1044 attribute. */
1045 tindex = info->type_index;
1046 ++info->type_index;
1047 definition = TRUE;
1048 sprintf (buf, "%ld=@S;", tindex);
1049 }
1050
1051 sprintf (buf + strlen (buf), "ar%s;%ld;%ld;%s",
1052 range, (long) low, (long) high, element);
1053 free (range);
1054 free (element);
1055
1056 if (high < low)
1057 size = 0;
1058 else
1059 size = element_size * ((high - low) + 1);
1060 if (! stab_push_string (info, buf, tindex, definition, size))
1061 return FALSE;
1062
1063 free (buf);
1064
1065 return TRUE;
1066 }
1067
1068 /* Push a set type. */
1069
1070 static bfd_boolean
1071 stab_set_type (void *p, bfd_boolean bitstringp)
1072 {
1073 struct stab_write_handle *info = (struct stab_write_handle *) p;
1074 bfd_boolean definition;
1075 char *s, *buf;
1076 long tindex;
1077
1078 definition = info->type_stack->definition;
1079
1080 s = stab_pop_type (info);
1081 buf = (char *) xmalloc (strlen (s) + 30);
1082
1083 if (! bitstringp)
1084 {
1085 *buf = '\0';
1086 tindex = 0;
1087 }
1088 else
1089 {
1090 /* We need to define a type in order to include the string
1091 attribute. */
1092 tindex = info->type_index;
1093 ++info->type_index;
1094 definition = TRUE;
1095 sprintf (buf, "%ld=@S;", tindex);
1096 }
1097
1098 sprintf (buf + strlen (buf), "S%s", s);
1099 free (s);
1100
1101 if (! stab_push_string (info, buf, tindex, definition, 0))
1102 return FALSE;
1103
1104 free (buf);
1105
1106 return TRUE;
1107 }
1108
1109 /* Push an offset type. */
1110
1111 static bfd_boolean
1112 stab_offset_type (void *p)
1113 {
1114 struct stab_write_handle *info = (struct stab_write_handle *) p;
1115 bfd_boolean definition;
1116 char *target, *base, *buf;
1117
1118 definition = info->type_stack->definition;
1119 target = stab_pop_type (info);
1120
1121 definition = definition || info->type_stack->definition;
1122 base = stab_pop_type (info);
1123
1124 buf = (char *) xmalloc (strlen (target) + strlen (base) + 3);
1125 sprintf (buf, "@%s,%s", base, target);
1126 free (base);
1127 free (target);
1128
1129 if (! stab_push_string (info, buf, 0, definition, 0))
1130 return FALSE;
1131
1132 free (buf);
1133
1134 return TRUE;
1135 }
1136
1137 /* Push a method type. */
1138
1139 static bfd_boolean
1140 stab_method_type (void *p, bfd_boolean domainp, int argcount,
1141 bfd_boolean varargs)
1142 {
1143 struct stab_write_handle *info = (struct stab_write_handle *) p;
1144 bfd_boolean definition;
1145 char *domain, *return_type, *buf;
1146 char **args;
1147 int i;
1148 size_t len;
1149
1150 /* We don't bother with stub method types, because that would
1151 require a mangler for C++ argument types. This will waste space
1152 in the debugging output. */
1153
1154 /* We need a domain. I'm not sure DOMAINP can ever be false,
1155 anyhow. */
1156 if (! domainp)
1157 {
1158 if (! stab_empty_type (p))
1159 return FALSE;
1160 }
1161
1162 definition = info->type_stack->definition;
1163 domain = stab_pop_type (info);
1164
1165 /* A non-varargs function is indicated by making the last parameter
1166 type be void. */
1167
1168 if (argcount < 0)
1169 {
1170 args = NULL;
1171 argcount = 0;
1172 }
1173 else if (argcount == 0)
1174 {
1175 if (varargs)
1176 args = NULL;
1177 else
1178 {
1179 args = (char **) xmalloc (1 * sizeof (*args));
1180 if (! stab_empty_type (p))
1181 return FALSE;
1182 definition = definition || info->type_stack->definition;
1183 args[0] = stab_pop_type (info);
1184 argcount = 1;
1185 }
1186 }
1187 else
1188 {
1189 args = (char **) xmalloc ((argcount + 1) * sizeof (*args));
1190 for (i = argcount - 1; i >= 0; i--)
1191 {
1192 definition = definition || info->type_stack->definition;
1193 args[i] = stab_pop_type (info);
1194 }
1195 if (! varargs)
1196 {
1197 if (! stab_empty_type (p))
1198 return FALSE;
1199 definition = definition || info->type_stack->definition;
1200 args[argcount] = stab_pop_type (info);
1201 ++argcount;
1202 }
1203 }
1204
1205 definition = definition || info->type_stack->definition;
1206 return_type = stab_pop_type (info);
1207
1208 len = strlen (domain) + strlen (return_type) + 10;
1209 for (i = 0; i < argcount; i++)
1210 len += strlen (args[i]);
1211
1212 buf = (char *) xmalloc (len);
1213
1214 sprintf (buf, "#%s,%s", domain, return_type);
1215 free (domain);
1216 free (return_type);
1217 for (i = 0; i < argcount; i++)
1218 {
1219 strcat (buf, ",");
1220 strcat (buf, args[i]);
1221 free (args[i]);
1222 }
1223 strcat (buf, ";");
1224
1225 if (args != NULL)
1226 free (args);
1227
1228 if (! stab_push_string (info, buf, 0, definition, 0))
1229 return FALSE;
1230
1231 free (buf);
1232
1233 return TRUE;
1234 }
1235
1236 /* Push a const version of a type. */
1237
1238 static bfd_boolean
1239 stab_const_type (void *p)
1240 {
1241 struct stab_write_handle *info = (struct stab_write_handle *) p;
1242
1243 return stab_modify_type (info, 'k', info->type_stack->size,
1244 (long **) NULL, (size_t *) NULL);
1245 }
1246
1247 /* Push a volatile version of a type. */
1248
1249 static bfd_boolean
1250 stab_volatile_type (void *p)
1251 {
1252 struct stab_write_handle *info = (struct stab_write_handle *) p;
1253
1254 return stab_modify_type (info, 'B', info->type_stack->size,
1255 (long **) NULL, (size_t *) NULL);
1256 }
1257
1258 /* Get the type index to use for a struct/union/class ID. This should
1259 return -1 if it fails. */
1260
1261 static long
1262 stab_get_struct_index (struct stab_write_handle *info, const char *tag,
1263 unsigned int id, enum debug_type_kind kind,
1264 unsigned int *psize)
1265 {
1266 if (id >= info->type_cache.struct_types_alloc)
1267 {
1268 size_t alloc;
1269
1270 alloc = info->type_cache.struct_types_alloc;
1271 if (alloc == 0)
1272 alloc = 10;
1273 while (id >= alloc)
1274 alloc *= 2;
1275 info->type_cache.struct_types =
1276 (struct stab_tag *) xrealloc (info->type_cache.struct_types,
1277 alloc * sizeof (struct stab_tag));
1278 memset ((info->type_cache.struct_types
1279 + info->type_cache.struct_types_alloc),
1280 0,
1281 ((alloc - info->type_cache.struct_types_alloc)
1282 * sizeof (struct stab_tag)));
1283 info->type_cache.struct_types_alloc = alloc;
1284 }
1285
1286 if (info->type_cache.struct_types[id].index == 0)
1287 {
1288 info->type_cache.struct_types[id].index = info->type_index;
1289 ++info->type_index;
1290 info->type_cache.struct_types[id].tag = tag;
1291 info->type_cache.struct_types[id].kind = kind;
1292 }
1293
1294 if (kind == DEBUG_KIND_ILLEGAL)
1295 {
1296 /* This is a definition of the struct. */
1297 info->type_cache.struct_types[id].kind = kind;
1298 info->type_cache.struct_types[id].size = *psize;
1299 }
1300 else
1301 *psize = info->type_cache.struct_types[id].size;
1302
1303 return info->type_cache.struct_types[id].index;
1304 }
1305
1306 /* Start outputting a struct. We ignore the tag, and handle it in
1307 stab_tag. */
1308
1309 static bfd_boolean
1310 stab_start_struct_type (void *p, const char *tag, unsigned int id,
1311 bfd_boolean structp, unsigned int size)
1312 {
1313 struct stab_write_handle *info = (struct stab_write_handle *) p;
1314 long tindex;
1315 bfd_boolean definition;
1316 char buf[40];
1317
1318 if (id == 0)
1319 {
1320 tindex = 0;
1321 *buf = '\0';
1322 definition = FALSE;
1323 }
1324 else
1325 {
1326 tindex = stab_get_struct_index (info, tag, id, DEBUG_KIND_ILLEGAL,
1327 &size);
1328 if (tindex < 0)
1329 return FALSE;
1330 sprintf (buf, "%ld=", tindex);
1331 definition = TRUE;
1332 }
1333
1334 sprintf (buf + strlen (buf), "%c%u",
1335 structp ? 's' : 'u',
1336 size);
1337
1338 if (! stab_push_string (info, buf, tindex, definition, size))
1339 return FALSE;
1340
1341 info->type_stack->fields = (char *) xmalloc (1);
1342 info->type_stack->fields[0] = '\0';
1343
1344 return TRUE;
1345 }
1346
1347 /* Add a field to a struct. */
1348
1349 static bfd_boolean
1350 stab_struct_field (void *p, const char *name, bfd_vma bitpos,
1351 bfd_vma bitsize, enum debug_visibility visibility)
1352 {
1353 struct stab_write_handle *info = (struct stab_write_handle *) p;
1354 bfd_boolean definition;
1355 unsigned int size;
1356 char *s, *n;
1357 const char *vis;
1358
1359 definition = info->type_stack->definition;
1360 size = info->type_stack->size;
1361 s = stab_pop_type (info);
1362
1363 /* Add this field to the end of the current struct fields, which is
1364 currently on the top of the stack. */
1365
1366 assert (info->type_stack->fields != NULL);
1367 n = (char *) xmalloc (strlen (info->type_stack->fields)
1368 + strlen (name)
1369 + strlen (s)
1370 + 50);
1371
1372 switch (visibility)
1373 {
1374 default:
1375 abort ();
1376
1377 case DEBUG_VISIBILITY_PUBLIC:
1378 vis = "";
1379 break;
1380
1381 case DEBUG_VISIBILITY_PRIVATE:
1382 vis = "/0";
1383 break;
1384
1385 case DEBUG_VISIBILITY_PROTECTED:
1386 vis = "/1";
1387 break;
1388 }
1389
1390 if (bitsize == 0)
1391 {
1392 bitsize = size * 8;
1393 if (bitsize == 0)
1394 non_fatal (_("%s: warning: unknown size for field `%s' in struct"),
1395 bfd_get_filename (info->abfd), name);
1396 }
1397
1398 sprintf (n, "%s%s:%s%s,%ld,%ld;", info->type_stack->fields, name, vis, s,
1399 (long) bitpos, (long) bitsize);
1400
1401 free (info->type_stack->fields);
1402 info->type_stack->fields = n;
1403
1404 if (definition)
1405 info->type_stack->definition = TRUE;
1406
1407 return TRUE;
1408 }
1409
1410 /* Finish up a struct. */
1411
1412 static bfd_boolean
1413 stab_end_struct_type (void *p)
1414 {
1415 struct stab_write_handle *info = (struct stab_write_handle *) p;
1416 bfd_boolean definition;
1417 long tindex;
1418 unsigned int size;
1419 char *fields, *first, *buf;
1420
1421 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1422
1423 definition = info->type_stack->definition;
1424 tindex = info->type_stack->index;
1425 size = info->type_stack->size;
1426 fields = info->type_stack->fields;
1427 first = stab_pop_type (info);
1428
1429 buf = (char *) xmalloc (strlen (first) + strlen (fields) + 2);
1430 sprintf (buf, "%s%s;", first, fields);
1431 free (first);
1432 free (fields);
1433
1434 if (! stab_push_string (info, buf, tindex, definition, size))
1435 return FALSE;
1436
1437 free (buf);
1438
1439 return TRUE;
1440 }
1441
1442 /* Start outputting a class. */
1443
1444 static bfd_boolean
1445 stab_start_class_type (void *p, const char *tag, unsigned int id, bfd_boolean structp, unsigned int size, bfd_boolean vptr, bfd_boolean ownvptr)
1446 {
1447 struct stab_write_handle *info = (struct stab_write_handle *) p;
1448 bfd_boolean definition;
1449 char *vstring;
1450
1451 if (! vptr || ownvptr)
1452 {
1453 definition = FALSE;
1454 vstring = NULL;
1455 }
1456 else
1457 {
1458 definition = info->type_stack->definition;
1459 vstring = stab_pop_type (info);
1460 }
1461
1462 if (! stab_start_struct_type (p, tag, id, structp, size))
1463 return FALSE;
1464
1465 if (vptr)
1466 {
1467 char *vtable;
1468
1469 if (ownvptr)
1470 {
1471 assert (info->type_stack->index > 0);
1472 vtable = (char *) xmalloc (20);
1473 sprintf (vtable, "~%%%ld", info->type_stack->index);
1474 }
1475 else
1476 {
1477 vtable = (char *) xmalloc (strlen (vstring) + 3);
1478 sprintf (vtable, "~%%%s", vstring);
1479 free (vstring);
1480 }
1481
1482 info->type_stack->vtable = vtable;
1483 }
1484
1485 if (definition)
1486 info->type_stack->definition = TRUE;
1487
1488 return TRUE;
1489 }
1490
1491 /* Add a static member to the class on the type stack. */
1492
1493 static bfd_boolean
1494 stab_class_static_member (void *p, const char *name, const char *physname,
1495 enum debug_visibility visibility)
1496 {
1497 struct stab_write_handle *info = (struct stab_write_handle *) p;
1498 bfd_boolean definition;
1499 char *s, *n;
1500 const char *vis;
1501
1502 definition = info->type_stack->definition;
1503 s = stab_pop_type (info);
1504
1505 /* Add this field to the end of the current struct fields, which is
1506 currently on the top of the stack. */
1507
1508 assert (info->type_stack->fields != NULL);
1509 n = (char *) xmalloc (strlen (info->type_stack->fields)
1510 + strlen (name)
1511 + strlen (s)
1512 + strlen (physname)
1513 + 10);
1514
1515 switch (visibility)
1516 {
1517 default:
1518 abort ();
1519
1520 case DEBUG_VISIBILITY_PUBLIC:
1521 vis = "";
1522 break;
1523
1524 case DEBUG_VISIBILITY_PRIVATE:
1525 vis = "/0";
1526 break;
1527
1528 case DEBUG_VISIBILITY_PROTECTED:
1529 vis = "/1";
1530 break;
1531 }
1532
1533 sprintf (n, "%s%s:%s%s:%s;", info->type_stack->fields, name, vis, s,
1534 physname);
1535
1536 free (info->type_stack->fields);
1537 info->type_stack->fields = n;
1538
1539 if (definition)
1540 info->type_stack->definition = TRUE;
1541
1542 return TRUE;
1543 }
1544
1545 /* Add a base class to the class on the type stack. */
1546
1547 static bfd_boolean
1548 stab_class_baseclass (void *p, bfd_vma bitpos, bfd_boolean is_virtual,
1549 enum debug_visibility visibility)
1550 {
1551 struct stab_write_handle *info = (struct stab_write_handle *) p;
1552 bfd_boolean definition;
1553 char *s;
1554 char *buf;
1555 unsigned int c;
1556 char **baseclasses;
1557
1558 definition = info->type_stack->definition;
1559 s = stab_pop_type (info);
1560
1561 /* Build the base class specifier. */
1562
1563 buf = (char *) xmalloc (strlen (s) + 25);
1564 buf[0] = is_virtual ? '1' : '0';
1565 switch (visibility)
1566 {
1567 default:
1568 abort ();
1569
1570 case DEBUG_VISIBILITY_PRIVATE:
1571 buf[1] = '0';
1572 break;
1573
1574 case DEBUG_VISIBILITY_PROTECTED:
1575 buf[1] = '1';
1576 break;
1577
1578 case DEBUG_VISIBILITY_PUBLIC:
1579 buf[1] = '2';
1580 break;
1581 }
1582
1583 sprintf (buf + 2, "%ld,%s;", (long) bitpos, s);
1584 free (s);
1585
1586 /* Add the new baseclass to the existing ones. */
1587
1588 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1589
1590 if (info->type_stack->baseclasses == NULL)
1591 c = 0;
1592 else
1593 {
1594 c = 0;
1595 while (info->type_stack->baseclasses[c] != NULL)
1596 ++c;
1597 }
1598
1599 baseclasses = (char **) xrealloc (info->type_stack->baseclasses,
1600 (c + 2) * sizeof (*baseclasses));
1601 baseclasses[c] = buf;
1602 baseclasses[c + 1] = NULL;
1603
1604 info->type_stack->baseclasses = baseclasses;
1605
1606 if (definition)
1607 info->type_stack->definition = TRUE;
1608
1609 return TRUE;
1610 }
1611
1612 /* Start adding a method to the class on the type stack. */
1613
1614 static bfd_boolean
1615 stab_class_start_method (void *p, const char *name)
1616 {
1617 struct stab_write_handle *info = (struct stab_write_handle *) p;
1618 char *m;
1619
1620 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1621
1622 if (info->type_stack->methods == NULL)
1623 {
1624 m = (char *) xmalloc (strlen (name) + 3);
1625 *m = '\0';
1626 }
1627 else
1628 {
1629 m = (char *) xrealloc (info->type_stack->methods,
1630 (strlen (info->type_stack->methods)
1631 + strlen (name)
1632 + 4));
1633 }
1634
1635 sprintf (m + strlen (m), "%s::", name);
1636
1637 info->type_stack->methods = m;
1638
1639 return TRUE;
1640 }
1641
1642 /* Add a variant, either static or not, to the current method. */
1643
1644 static bfd_boolean
1645 stab_class_method_var (struct stab_write_handle *info, const char *physname,
1646 enum debug_visibility visibility,
1647 bfd_boolean staticp, bfd_boolean constp,
1648 bfd_boolean volatilep, bfd_vma voffset,
1649 bfd_boolean contextp)
1650 {
1651 bfd_boolean definition;
1652 char *type;
1653 char *context = NULL;
1654 char visc, qualc, typec;
1655
1656 definition = info->type_stack->definition;
1657 type = stab_pop_type (info);
1658
1659 if (contextp)
1660 {
1661 definition = definition || info->type_stack->definition;
1662 context = stab_pop_type (info);
1663 }
1664
1665 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1666
1667 switch (visibility)
1668 {
1669 default:
1670 abort ();
1671
1672 case DEBUG_VISIBILITY_PRIVATE:
1673 visc = '0';
1674 break;
1675
1676 case DEBUG_VISIBILITY_PROTECTED:
1677 visc = '1';
1678 break;
1679
1680 case DEBUG_VISIBILITY_PUBLIC:
1681 visc = '2';
1682 break;
1683 }
1684
1685 if (constp)
1686 {
1687 if (volatilep)
1688 qualc = 'D';
1689 else
1690 qualc = 'B';
1691 }
1692 else
1693 {
1694 if (volatilep)
1695 qualc = 'C';
1696 else
1697 qualc = 'A';
1698 }
1699
1700 if (staticp)
1701 typec = '?';
1702 else if (! contextp)
1703 typec = '.';
1704 else
1705 typec = '*';
1706
1707 info->type_stack->methods =
1708 (char *) xrealloc (info->type_stack->methods,
1709 (strlen (info->type_stack->methods)
1710 + strlen (type)
1711 + strlen (physname)
1712 + (contextp ? strlen (context) : 0)
1713 + 40));
1714
1715 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1716 "%s:%s;%c%c%c", type, physname, visc, qualc, typec);
1717 free (type);
1718
1719 if (contextp)
1720 {
1721 sprintf (info->type_stack->methods + strlen (info->type_stack->methods),
1722 "%ld;%s;", (long) voffset, context);
1723 free (context);
1724 }
1725
1726 if (definition)
1727 info->type_stack->definition = TRUE;
1728
1729 return TRUE;
1730 }
1731
1732 /* Add a variant to the current method. */
1733
1734 static bfd_boolean
1735 stab_class_method_variant (void *p, const char *physname,
1736 enum debug_visibility visibility,
1737 bfd_boolean constp, bfd_boolean volatilep,
1738 bfd_vma voffset, bfd_boolean contextp)
1739 {
1740 struct stab_write_handle *info = (struct stab_write_handle *) p;
1741
1742 return stab_class_method_var (info, physname, visibility, FALSE, constp,
1743 volatilep, voffset, contextp);
1744 }
1745
1746 /* Add a static variant to the current method. */
1747
1748 static bfd_boolean
1749 stab_class_static_method_variant (void *p, const char *physname,
1750 enum debug_visibility visibility,
1751 bfd_boolean constp, bfd_boolean volatilep)
1752 {
1753 struct stab_write_handle *info = (struct stab_write_handle *) p;
1754
1755 return stab_class_method_var (info, physname, visibility, TRUE, constp,
1756 volatilep, 0, FALSE);
1757 }
1758
1759 /* Finish up a method. */
1760
1761 static bfd_boolean
1762 stab_class_end_method (void *p)
1763 {
1764 struct stab_write_handle *info = (struct stab_write_handle *) p;
1765
1766 assert (info->type_stack != NULL && info->type_stack->methods != NULL);
1767
1768 /* We allocated enough room on info->type_stack->methods to add the
1769 trailing semicolon. */
1770 strcat (info->type_stack->methods, ";");
1771
1772 return TRUE;
1773 }
1774
1775 /* Finish up a class. */
1776
1777 static bfd_boolean
1778 stab_end_class_type (void *p)
1779 {
1780 struct stab_write_handle *info = (struct stab_write_handle *) p;
1781 size_t len;
1782 unsigned int i = 0;
1783 char *buf;
1784
1785 assert (info->type_stack != NULL && info->type_stack->fields != NULL);
1786
1787 /* Work out the size we need to allocate for the class definition. */
1788
1789 len = (strlen (info->type_stack->string)
1790 + strlen (info->type_stack->fields)
1791 + 10);
1792 if (info->type_stack->baseclasses != NULL)
1793 {
1794 len += 20;
1795 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1796 len += strlen (info->type_stack->baseclasses[i]);
1797 }
1798 if (info->type_stack->methods != NULL)
1799 len += strlen (info->type_stack->methods);
1800 if (info->type_stack->vtable != NULL)
1801 len += strlen (info->type_stack->vtable);
1802
1803 /* Build the class definition. */
1804
1805 buf = (char *) xmalloc (len);
1806
1807 strcpy (buf, info->type_stack->string);
1808
1809 if (info->type_stack->baseclasses != NULL)
1810 {
1811 sprintf (buf + strlen (buf), "!%u,", i);
1812 for (i = 0; info->type_stack->baseclasses[i] != NULL; i++)
1813 {
1814 strcat (buf, info->type_stack->baseclasses[i]);
1815 free (info->type_stack->baseclasses[i]);
1816 }
1817 free (info->type_stack->baseclasses);
1818 info->type_stack->baseclasses = NULL;
1819 }
1820
1821 strcat (buf, info->type_stack->fields);
1822 free (info->type_stack->fields);
1823 info->type_stack->fields = NULL;
1824
1825 if (info->type_stack->methods != NULL)
1826 {
1827 strcat (buf, info->type_stack->methods);
1828 free (info->type_stack->methods);
1829 info->type_stack->methods = NULL;
1830 }
1831
1832 strcat (buf, ";");
1833
1834 if (info->type_stack->vtable != NULL)
1835 {
1836 strcat (buf, info->type_stack->vtable);
1837 free (info->type_stack->vtable);
1838 info->type_stack->vtable = NULL;
1839 }
1840
1841 /* Replace the string on the top of the stack with the complete
1842 class definition. */
1843 free (info->type_stack->string);
1844 info->type_stack->string = buf;
1845
1846 return TRUE;
1847 }
1848
1849 /* Push a typedef which was previously defined. */
1850
1851 static bfd_boolean
1852 stab_typedef_type (void *p, const char *name)
1853 {
1854 struct stab_write_handle *info = (struct stab_write_handle *) p;
1855 struct string_hash_entry *h;
1856
1857 h = string_hash_lookup (&info->typedef_hash, name, FALSE, FALSE);
1858 assert (h != NULL && h->index > 0);
1859
1860 return stab_push_defined_type (info, h->index, h->size);
1861 }
1862
1863 /* Push a struct, union or class tag. */
1864
1865 static bfd_boolean
1866 stab_tag_type (void *p, const char *name, unsigned int id,
1867 enum debug_type_kind kind)
1868 {
1869 struct stab_write_handle *info = (struct stab_write_handle *) p;
1870 long tindex;
1871 unsigned int size = 0;
1872
1873 tindex = stab_get_struct_index (info, name, id, kind, &size);
1874 if (tindex < 0)
1875 return FALSE;
1876
1877 return stab_push_defined_type (info, tindex, size);
1878 }
1879
1880 /* Define a typedef. */
1881
1882 static bfd_boolean
1883 stab_typdef (void *p, const char *name)
1884 {
1885 struct stab_write_handle *info = (struct stab_write_handle *) p;
1886 long tindex;
1887 unsigned int size;
1888 char *s, *buf;
1889 struct string_hash_entry *h;
1890
1891 tindex = info->type_stack->index;
1892 size = info->type_stack->size;
1893 s = stab_pop_type (info);
1894
1895 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
1896
1897 if (tindex > 0)
1898 sprintf (buf, "%s:t%s", name, s);
1899 else
1900 {
1901 tindex = info->type_index;
1902 ++info->type_index;
1903 sprintf (buf, "%s:t%ld=%s", name, tindex, s);
1904 }
1905
1906 free (s);
1907
1908 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1909 return FALSE;
1910
1911 free (buf);
1912
1913 h = string_hash_lookup (&info->typedef_hash, name, TRUE, FALSE);
1914 if (h == NULL)
1915 {
1916 non_fatal (_("string_hash_lookup failed: %s"),
1917 bfd_errmsg (bfd_get_error ()));
1918 return FALSE;
1919 }
1920
1921 /* I don't think we care about redefinitions. */
1922
1923 h->index = tindex;
1924 h->size = size;
1925
1926 return TRUE;
1927 }
1928
1929 /* Define a tag. */
1930
1931 static bfd_boolean
1932 stab_tag (void *p, const char *tag)
1933 {
1934 struct stab_write_handle *info = (struct stab_write_handle *) p;
1935 char *s, *buf;
1936
1937 s = stab_pop_type (info);
1938
1939 buf = (char *) xmalloc (strlen (tag) + strlen (s) + 3);
1940
1941 sprintf (buf, "%s:T%s", tag, s);
1942 free (s);
1943
1944 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1945 return FALSE;
1946
1947 free (buf);
1948
1949 return TRUE;
1950 }
1951
1952 /* Define an integer constant. */
1953
1954 static bfd_boolean
1955 stab_int_constant (void *p, const char *name, bfd_vma val)
1956 {
1957 struct stab_write_handle *info = (struct stab_write_handle *) p;
1958 char *buf;
1959
1960 buf = (char *) xmalloc (strlen (name) + 20);
1961 sprintf (buf, "%s:c=i%ld", name, (long) val);
1962
1963 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1964 return FALSE;
1965
1966 free (buf);
1967
1968 return TRUE;
1969 }
1970
1971 /* Define a floating point constant. */
1972
1973 static bfd_boolean
1974 stab_float_constant (void *p, const char *name, double val)
1975 {
1976 struct stab_write_handle *info = (struct stab_write_handle *) p;
1977 char *buf;
1978
1979 buf = (char *) xmalloc (strlen (name) + 20);
1980 sprintf (buf, "%s:c=f%g", name, val);
1981
1982 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
1983 return FALSE;
1984
1985 free (buf);
1986
1987 return TRUE;
1988 }
1989
1990 /* Define a typed constant. */
1991
1992 static bfd_boolean
1993 stab_typed_constant (void *p, const char *name, bfd_vma val)
1994 {
1995 struct stab_write_handle *info = (struct stab_write_handle *) p;
1996 char *s, *buf;
1997
1998 s = stab_pop_type (info);
1999
2000 buf = (char *) xmalloc (strlen (name) + strlen (s) + 20);
2001 sprintf (buf, "%s:c=e%s,%ld", name, s, (long) val);
2002 free (s);
2003
2004 if (! stab_write_symbol (info, N_LSYM, 0, 0, buf))
2005 return FALSE;
2006
2007 free (buf);
2008
2009 return TRUE;
2010 }
2011
2012 /* Record a variable. */
2013
2014 static bfd_boolean
2015 stab_variable (void *p, const char *name, enum debug_var_kind kind,
2016 bfd_vma val)
2017 {
2018 struct stab_write_handle *info = (struct stab_write_handle *) p;
2019 char *s, *buf;
2020 int stab_type;
2021 const char *kindstr;
2022
2023 s = stab_pop_type (info);
2024
2025 switch (kind)
2026 {
2027 default:
2028 abort ();
2029
2030 case DEBUG_GLOBAL:
2031 stab_type = N_GSYM;
2032 kindstr = "G";
2033 break;
2034
2035 case DEBUG_STATIC:
2036 stab_type = N_STSYM;
2037 kindstr = "S";
2038 break;
2039
2040 case DEBUG_LOCAL_STATIC:
2041 stab_type = N_STSYM;
2042 kindstr = "V";
2043 break;
2044
2045 case DEBUG_LOCAL:
2046 stab_type = N_LSYM;
2047 kindstr = "";
2048
2049 /* Make sure that this is a type reference or definition. */
2050 if (! ISDIGIT (*s))
2051 {
2052 char *n;
2053 long tindex;
2054
2055 tindex = info->type_index;
2056 ++info->type_index;
2057 n = (char *) xmalloc (strlen (s) + 20);
2058 sprintf (n, "%ld=%s", tindex, s);
2059 free (s);
2060 s = n;
2061 }
2062 break;
2063
2064 case DEBUG_REGISTER:
2065 stab_type = N_RSYM;
2066 kindstr = "r";
2067 break;
2068 }
2069
2070 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2071 sprintf (buf, "%s:%s%s", name, kindstr, s);
2072 free (s);
2073
2074 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2075 return FALSE;
2076
2077 free (buf);
2078
2079 return TRUE;
2080 }
2081
2082 /* Start outputting a function. */
2083
2084 static bfd_boolean
2085 stab_start_function (void *p, const char *name, bfd_boolean globalp)
2086 {
2087 struct stab_write_handle *info = (struct stab_write_handle *) p;
2088 char *rettype, *buf;
2089
2090 assert (info->nesting == 0 && info->fun_offset == -1);
2091
2092 rettype = stab_pop_type (info);
2093
2094 buf = (char *) xmalloc (strlen (name) + strlen (rettype) + 3);
2095 sprintf (buf, "%s:%c%s", name,
2096 globalp ? 'F' : 'f',
2097 rettype);
2098
2099 /* We don't know the value now, so we set it in start_block. */
2100 info->fun_offset = info->symbols_size;
2101
2102 if (! stab_write_symbol (info, N_FUN, 0, 0, buf))
2103 return FALSE;
2104
2105 free (buf);
2106
2107 return TRUE;
2108 }
2109
2110 /* Output a function parameter. */
2111
2112 static bfd_boolean
2113 stab_function_parameter (void *p, const char *name, enum debug_parm_kind kind, bfd_vma val)
2114 {
2115 struct stab_write_handle *info = (struct stab_write_handle *) p;
2116 char *s, *buf;
2117 int stab_type;
2118 char kindc;
2119
2120 s = stab_pop_type (info);
2121
2122 switch (kind)
2123 {
2124 default:
2125 abort ();
2126
2127 case DEBUG_PARM_STACK:
2128 stab_type = N_PSYM;
2129 kindc = 'p';
2130 break;
2131
2132 case DEBUG_PARM_REG:
2133 stab_type = N_RSYM;
2134 kindc = 'P';
2135 break;
2136
2137 case DEBUG_PARM_REFERENCE:
2138 stab_type = N_PSYM;
2139 kindc = 'v';
2140 break;
2141
2142 case DEBUG_PARM_REF_REG:
2143 stab_type = N_RSYM;
2144 kindc = 'a';
2145 break;
2146 }
2147
2148 buf = (char *) xmalloc (strlen (name) + strlen (s) + 3);
2149 sprintf (buf, "%s:%c%s", name, kindc, s);
2150 free (s);
2151
2152 if (! stab_write_symbol (info, stab_type, 0, val, buf))
2153 return FALSE;
2154
2155 free (buf);
2156
2157 return TRUE;
2158 }
2159
2160 /* Start a block. */
2161
2162 static bfd_boolean
2163 stab_start_block (void *p, bfd_vma addr)
2164 {
2165 struct stab_write_handle *info = (struct stab_write_handle *) p;
2166
2167 /* Fill in any slots which have been waiting for the first known
2168 text address. */
2169
2170 if (info->so_offset != -1)
2171 {
2172 bfd_put_32 (info->abfd, addr, info->symbols + info->so_offset + 8);
2173 info->so_offset = -1;
2174 }
2175
2176 if (info->fun_offset != -1)
2177 {
2178 bfd_put_32 (info->abfd, addr, info->symbols + info->fun_offset + 8);
2179 info->fun_offset = -1;
2180 }
2181
2182 ++info->nesting;
2183
2184 /* We will be called with a top level block surrounding the
2185 function, but stabs information does not output that block, so we
2186 ignore it. */
2187
2188 if (info->nesting == 1)
2189 {
2190 info->fnaddr = addr;
2191 return TRUE;
2192 }
2193
2194 /* We have to output the LBRAC symbol after any variables which are
2195 declared inside the block. We postpone the LBRAC until the next
2196 start_block or end_block. */
2197
2198 /* If we have postponed an LBRAC, output it now. */
2199 if (info->pending_lbrac != (bfd_vma) -1)
2200 {
2201 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2202 (const char *) NULL))
2203 return FALSE;
2204 }
2205
2206 /* Remember the address and output it later. */
2207
2208 info->pending_lbrac = addr - info->fnaddr;
2209
2210 return TRUE;
2211 }
2212
2213 /* End a block. */
2214
2215 static bfd_boolean
2216 stab_end_block (void *p, bfd_vma addr)
2217 {
2218 struct stab_write_handle *info = (struct stab_write_handle *) p;
2219
2220 if (addr > info->last_text_address)
2221 info->last_text_address = addr;
2222
2223 /* If we have postponed an LBRAC, output it now. */
2224 if (info->pending_lbrac != (bfd_vma) -1)
2225 {
2226 if (! stab_write_symbol (info, N_LBRAC, 0, info->pending_lbrac,
2227 (const char *) NULL))
2228 return FALSE;
2229 info->pending_lbrac = (bfd_vma) -1;
2230 }
2231
2232 assert (info->nesting > 0);
2233
2234 --info->nesting;
2235
2236 /* We ignore the outermost block. */
2237 if (info->nesting == 0)
2238 return TRUE;
2239
2240 return stab_write_symbol (info, N_RBRAC, 0, addr - info->fnaddr,
2241 (const char *) NULL);
2242 }
2243
2244 /* End a function. */
2245
2246 static bfd_boolean
2247 stab_end_function (void *p ATTRIBUTE_UNUSED)
2248 {
2249 return TRUE;
2250 }
2251
2252 /* Output a line number. */
2253
2254 static bfd_boolean
2255 stab_lineno (void *p, const char *file, unsigned long lineno, bfd_vma addr)
2256 {
2257 struct stab_write_handle *info = (struct stab_write_handle *) p;
2258
2259 assert (info->lineno_filename != NULL);
2260
2261 if (addr > info->last_text_address)
2262 info->last_text_address = addr;
2263
2264 if (filename_cmp (file, info->lineno_filename) != 0)
2265 {
2266 if (! stab_write_symbol (info, N_SOL, 0, addr, file))
2267 return FALSE;
2268 info->lineno_filename = file;
2269 }
2270
2271 return stab_write_symbol (info, N_SLINE, lineno, addr - info->fnaddr,
2272 (const char *) NULL);
2273 }
2274