Coverage Report

Created: 2023-06-29 07:13

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