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