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