Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/stabs.c
Line
Count
Source
1
/* Stabs in sections linking support.
2
   Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
   Written by Ian Lance Taylor, Cygnus Support.
4
5
   This file is part of BFD, the Binary File Descriptor library.
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,
20
   MA 02110-1301, USA.  */
21
22
23
/* This file contains support for linking stabs in sections, as used
24
   on COFF and ELF.  */
25
26
#include "sysdep.h"
27
#include "bfd.h"
28
#include "libbfd.h"
29
#include "aout/stab_gnu.h"
30
#include "safe-ctype.h"
31
32
/* Stabs entries use a 12 byte format:
33
     4 byte string table index
34
     1 byte stab type
35
     1 byte stab other field
36
     2 byte stab desc field
37
     4 byte stab value
38
   FIXME: This will have to change for a 64 bit object format.
39
40
   The stabs symbols are divided into compilation units.  For the
41
   first entry in each unit, the type of 0, the value is the length of
42
   the string table for this unit, and the desc field is the number of
43
   stabs symbols for this unit.  */
44
45
#define STRDXOFF  0
46
0
#define TYPEOFF   4
47
#define OTHEROFF  5
48
#define DESCOFF   6
49
0
#define VALOFF    8
50
0
#define STABSIZE  12
51
52
/* A linked list of totals that we have found for a particular header
53
   file.  A total is a unique identifier for a particular BINCL...EINCL
54
   sequence of STABs that can be used to identify duplicate sequences.
55
   It consists of three fields, 'sum_chars' which is the sum of all the
56
   STABS characters; 'num_chars' which is the number of these charactes
57
   and 'symb' which is a buffer of all the symbols in the sequence.  This
58
   buffer is only checked as a last resort.  */
59
60
struct stab_link_includes_totals
61
{
62
  struct stab_link_includes_totals *next;
63
  bfd_vma sum_chars;  /* Accumulated sum of STABS characters.  */
64
  bfd_vma num_chars;  /* Number of STABS characters.  */
65
  const char* symb;   /* The STABS characters themselves.  */
66
};
67
68
/* An entry in the header file hash table.  */
69
70
struct stab_link_includes_entry
71
{
72
  struct bfd_hash_entry root;
73
  /* List of totals we have found for this file.  */
74
  struct stab_link_includes_totals *totals;
75
};
76
77
/* This structure is used to hold a list of N_BINCL symbols, some of
78
   which might be converted into N_EXCL symbols.  */
79
80
struct stab_excl_list
81
{
82
  /* The next symbol to convert.  */
83
  struct stab_excl_list *next;
84
  /* The offset to this symbol in the section contents.  */
85
  bfd_size_type offset;
86
  /* The value to use for the symbol.  */
87
  bfd_vma val;
88
  /* The type of this symbol (N_BINCL or N_EXCL).  */
89
  int type;
90
};
91
92
/* This structure is stored with each .stab section.  */
93
94
struct stab_section_info
95
{
96
  /* This is a linked list of N_BINCL symbols which should be
97
     converted into N_EXCL symbols.  */
98
  struct stab_excl_list *excls;
99
100
  /* This is used to map input stab offsets within their sections
101
     to output stab offsets, to take into account stabs that have
102
     been deleted.  If it is NULL, the output offsets are the same
103
     as the input offsets, because no stabs have been deleted from
104
     this section.  Otherwise the i'th entry is the number of
105
     bytes of stabs that have been deleted prior to the i'th
106
     stab.  */
107
  bfd_size_type *cumulative_skips;
108
109
  /* This is an array of string indices.  For each stab symbol, we
110
     store the string index here.  If a stab symbol should not be
111
     included in the final output, the string index is -1.  */
112
  bfd_size_type stridxs[1];
113
};
114
115
/*
116
EXTERNAL
117
.{* This structure is used to keep track of stabs in sections
118
.   information while linking.  *}
119
.
120
.struct stab_info
121
.{
122
.  {* A hash table used to hold stabs strings.  *}
123
.  struct bfd_strtab_hash *strings;
124
.  {* The header file hash table.  *}
125
.  struct bfd_hash_table includes;
126
.  {* The first .stabstr section.  *}
127
.  struct bfd_section *stabstr;
128
.};
129
.
130
*/
131
132
/* The function to create a new entry in the header file hash table.  */
133
134
static struct bfd_hash_entry *
135
stab_link_includes_newfunc (struct bfd_hash_entry *entry,
136
          struct bfd_hash_table *table,
137
          const char *string)
138
0
{
139
0
  struct stab_link_includes_entry *ret =
140
0
    (struct stab_link_includes_entry *) entry;
141
142
  /* Allocate the structure if it has not already been allocated by a
143
     subclass.  */
144
0
  if (ret == NULL)
145
0
    ret = (struct stab_link_includes_entry *)
146
0
  bfd_hash_allocate (table, sizeof (struct stab_link_includes_entry));
147
0
  if (ret == NULL)
148
0
    return NULL;
149
150
  /* Call the allocation method of the superclass.  */
151
0
  ret = ((struct stab_link_includes_entry *)
152
0
   bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
153
0
  if (ret)
154
    /* Set local fields.  */
155
0
    ret->totals = NULL;
156
157
0
  return (struct bfd_hash_entry *) ret;
158
0
}
159
160
/*
161
INTERNAL_FUNCTION
162
  _bfd_link_section_stabs
163
164
SYNOPSIS
165
  bool _bfd_link_section_stabs
166
    (bfd *, struct stab_info *, asection *, asection *,
167
     bfd_size_type *);
168
169
DESCRIPTION
170
  This function is called for each input file from the add_symbols
171
  pass of the linker.
172
*/
173
174
bool
175
_bfd_link_section_stabs (bfd *abfd,
176
       struct stab_info *sinfo,
177
       asection *stabsec,
178
       asection *stabstrsec,
179
       bfd_size_type *pstring_offset)
180
0
{
181
0
  bool first;
182
0
  bfd_size_type count, amt;
183
0
  struct stab_section_info *secinfo;
184
0
  bfd_byte *stabbuf = NULL;
185
0
  bfd_byte *stabstrbuf = NULL;
186
0
  bfd_byte *sym, *symend;
187
0
  bfd_size_type stroff, next_stroff, skip;
188
0
  bfd_size_type *pstridx;
189
190
0
  if (stabsec->size == 0
191
0
      || stabstrsec->size == 0
192
0
      || (stabsec->flags & SEC_HAS_CONTENTS) == 0
193
0
      || (stabstrsec->flags & SEC_HAS_CONTENTS) == 0)
194
    /* This file does not contain stabs debugging information.  */
195
0
    return true;
196
197
0
  if (stabsec->size % STABSIZE != 0)
198
    /* Something is wrong with the format of these stab symbols.
199
       Don't try to optimize them.  */
200
0
    return true;
201
202
0
  if ((stabstrsec->flags & SEC_RELOC) != 0)
203
    /* We shouldn't see relocations in the strings, and we aren't
204
       prepared to handle them.  */
205
0
    return true;
206
207
0
  if (bfd_is_abs_section (stabsec->output_section)
208
0
      || bfd_is_abs_section (stabstrsec->output_section))
209
    /* At least one of the sections is being discarded from the
210
       link, so we should just ignore them.  */
211
0
    return true;
212
213
0
  first = false;
214
215
0
  if (sinfo->stabstr == NULL)
216
0
    {
217
0
      flagword flags;
218
219
      /* Initialize the stabs information we need to keep track of.  */
220
0
      first = true;
221
0
      sinfo->strings = _bfd_stringtab_init ();
222
0
      if (sinfo->strings == NULL)
223
0
  goto error_return;
224
      /* Make sure the first byte is zero.  */
225
0
      (void) _bfd_stringtab_add (sinfo->strings, "", true, true);
226
0
      if (! bfd_hash_table_init (&sinfo->includes,
227
0
         stab_link_includes_newfunc,
228
0
         sizeof (struct stab_link_includes_entry)))
229
0
  goto error_return;
230
0
      flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING
231
0
         | SEC_LINKER_CREATED);
232
0
      sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr",
233
0
                 flags);
234
0
      if (sinfo->stabstr == NULL)
235
0
  goto error_return;
236
0
    }
237
238
  /* Initialize the information we are going to store for this .stab
239
     section.  */
240
0
  count = stabsec->size / STABSIZE;
241
242
0
  amt = sizeof (struct stab_section_info);
243
0
  amt += (count - 1) * sizeof (bfd_size_type);
244
0
  secinfo = bfd_alloc (abfd, amt);
245
0
  if (secinfo == NULL)
246
0
    goto error_return;
247
248
0
  stabsec->sec_info = secinfo;
249
0
  stabsec->sec_info_type = SEC_INFO_TYPE_STABS;
250
0
  secinfo->excls = NULL;
251
0
  stabsec->rawsize = stabsec->size;
252
0
  secinfo->cumulative_skips = NULL;
253
0
  memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
254
255
  /* Read the stabs information from abfd.  */
256
0
  if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf)
257
0
      || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf))
258
0
    goto error_return;
259
260
  /* Look through the stabs symbols, work out the new string indices,
261
     and identify N_BINCL symbols which can be eliminated.  */
262
0
  stroff = 0;
263
  /* The stabs sections can be split when
264
     -split-by-reloc/-split-by-file is used.  We must keep track of
265
     each stab section's place in the single concatenated string
266
     table.  */
267
0
  next_stroff = pstring_offset ? *pstring_offset : 0;
268
0
  skip = 0;
269
270
0
  symend = stabbuf + stabsec->size;
271
0
  for (sym = stabbuf, pstridx = secinfo->stridxs;
272
0
       sym < symend;
273
0
       sym += STABSIZE, ++pstridx)
274
0
    {
275
0
      bfd_size_type symstroff;
276
0
      int type;
277
0
      const char *string;
278
279
0
      if (*pstridx != 0)
280
  /* This symbol has already been handled by an N_BINCL pass.  */
281
0
  continue;
282
283
0
      type = sym[TYPEOFF];
284
285
0
      if (type == 0)
286
0
  {
287
    /* Special type 0 stabs indicate the offset to the next
288
       string table.  We only copy the very first one.  */
289
0
    stroff = next_stroff;
290
0
    next_stroff += bfd_get_32 (abfd, sym + 8);
291
0
    if (pstring_offset)
292
0
      *pstring_offset = next_stroff;
293
0
    if (! first)
294
0
      {
295
0
        *pstridx = (bfd_size_type) -1;
296
0
        ++skip;
297
0
        continue;
298
0
      }
299
0
    first = false;
300
0
  }
301
302
      /* Store the string in the hash table, and record the index.  */
303
0
      symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
304
0
      if (symstroff >= stabstrsec->size)
305
0
  {
306
0
    _bfd_error_handler
307
      /* xgettext:c-format */
308
0
      (_("%pB(%pA+%#lx): stabs entry has invalid string index"),
309
0
       abfd, stabsec, (long) (sym - stabbuf));
310
0
    bfd_set_error (bfd_error_bad_value);
311
0
    goto error_return;
312
0
  }
313
0
      string = (char *) stabstrbuf + symstroff;
314
0
      *pstridx = _bfd_stringtab_add (sinfo->strings, string, true, true);
315
316
      /* An N_BINCL symbol indicates the start of the stabs entries
317
   for a header file.  We need to scan ahead to the next N_EINCL
318
   symbol, ignoring nesting, adding up all the characters in the
319
   symbol names, not including the file numbers in types (the
320
   first number after an open parenthesis).  */
321
0
      if (type == (int) N_BINCL)
322
0
  {
323
0
    bfd_vma sum_chars;
324
0
    bfd_vma num_chars;
325
0
    bfd_vma buf_len = 0;
326
0
    char * symb;
327
0
    char * symb_rover;
328
0
    int nest;
329
0
    bfd_byte * incl_sym;
330
0
    struct stab_link_includes_entry * incl_entry;
331
0
    struct stab_link_includes_totals * t;
332
0
    struct stab_excl_list * ne;
333
334
0
    symb = symb_rover = NULL;
335
0
    sum_chars = num_chars = 0;
336
0
    nest = 0;
337
338
0
    for (incl_sym = sym + STABSIZE;
339
0
         incl_sym < symend;
340
0
         incl_sym += STABSIZE)
341
0
      {
342
0
        int incl_type;
343
344
0
        incl_type = incl_sym[TYPEOFF];
345
0
        if (incl_type == 0)
346
0
    break;
347
0
        else if (incl_type == (int) N_EXCL)
348
0
    continue;
349
0
        else if (incl_type == (int) N_EINCL)
350
0
    {
351
0
      if (nest == 0)
352
0
        break;
353
0
      --nest;
354
0
    }
355
0
        else if (incl_type == (int) N_BINCL)
356
0
    ++nest;
357
0
        else if (nest == 0)
358
0
    {
359
0
      const char *str;
360
361
0
      str = ((char *) stabstrbuf
362
0
       + stroff
363
0
       + bfd_get_32 (abfd, incl_sym + STRDXOFF));
364
0
      for (; *str != '\0'; str++)
365
0
        {
366
0
          if (num_chars >= buf_len)
367
0
      {
368
0
        buf_len += 32 * 1024;
369
0
        symb = (char *) bfd_realloc_or_free (symb, buf_len);
370
0
        if (symb == NULL)
371
0
          goto error_return;
372
0
        symb_rover = symb + num_chars;
373
0
      }
374
0
          * symb_rover ++ = * str;
375
0
          sum_chars += *str;
376
0
          num_chars ++;
377
0
          if (*str == '(')
378
0
      {
379
        /* Skip the file number.  */
380
0
        ++str;
381
0
        while (ISDIGIT (*str))
382
0
          ++str;
383
0
        --str;
384
0
      }
385
0
        }
386
0
    }
387
0
      }
388
389
0
    BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb));
390
391
    /* If we have already included a header file with the same
392
       value, then replaced this one with an N_EXCL symbol.  */
393
0
    incl_entry = (struct stab_link_includes_entry * )
394
0
      bfd_hash_lookup (&sinfo->includes, string, true, true);
395
0
    if (incl_entry == NULL)
396
0
      goto error_return;
397
398
0
    for (t = incl_entry->totals; t != NULL; t = t->next)
399
0
      if (t->sum_chars == sum_chars
400
0
    && t->num_chars == num_chars
401
0
    && memcmp (t->symb, symb, num_chars) == 0)
402
0
        break;
403
404
    /* Record this symbol, so that we can set the value
405
       correctly.  */
406
0
    amt = sizeof *ne;
407
0
    ne = (struct stab_excl_list *) bfd_alloc (abfd, amt);
408
0
    if (ne == NULL)
409
0
      goto error_return;
410
0
    ne->offset = sym - stabbuf;
411
0
    ne->val = sum_chars;
412
0
    ne->type = (int) N_BINCL;
413
0
    ne->next = secinfo->excls;
414
0
    secinfo->excls = ne;
415
416
0
    if (t == NULL)
417
0
      {
418
        /* This is the first time we have seen this header file
419
     with this set of stabs strings.  */
420
0
        t = (struct stab_link_includes_totals *)
421
0
      bfd_hash_allocate (&sinfo->includes, sizeof *t);
422
0
        if (t == NULL)
423
0
    goto error_return;
424
0
        t->sum_chars = sum_chars;
425
0
        t->num_chars = num_chars;
426
        /* Trim data down.  */
427
0
        t->symb = symb = (char *) bfd_realloc_or_free (symb, num_chars);
428
0
        t->next = incl_entry->totals;
429
0
        incl_entry->totals = t;
430
0
      }
431
0
    else
432
0
      {
433
0
        bfd_size_type *incl_pstridx;
434
435
        /* We have seen this header file before.  Tell the final
436
     pass to change the type to N_EXCL.  */
437
0
        ne->type = (int) N_EXCL;
438
439
        /* Free off superfluous symbols.  */
440
0
        free (symb);
441
442
        /* Mark the skipped symbols.  */
443
444
0
        nest = 0;
445
0
        for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
446
0
       incl_sym < symend;
447
0
       incl_sym += STABSIZE, ++incl_pstridx)
448
0
    {
449
0
      int incl_type;
450
451
0
      incl_type = incl_sym[TYPEOFF];
452
453
0
      if (incl_type == (int) N_EINCL)
454
0
        {
455
0
          if (nest == 0)
456
0
      {
457
0
        *incl_pstridx = (bfd_size_type) -1;
458
0
        ++skip;
459
0
        break;
460
0
      }
461
0
          --nest;
462
0
        }
463
0
      else if (incl_type == (int) N_BINCL)
464
0
        ++nest;
465
0
      else if (incl_type == (int) N_EXCL)
466
        /* Keep existing exclusion marks.  */
467
0
        continue;
468
0
      else if (nest == 0)
469
0
        {
470
0
          *incl_pstridx = (bfd_size_type) -1;
471
0
          ++skip;
472
0
        }
473
0
    }
474
0
      }
475
0
  }
476
0
    }
477
478
0
  free (stabbuf);
479
0
  stabbuf = NULL;
480
0
  free (stabstrbuf);
481
0
  stabstrbuf = NULL;
482
483
  /* We need to set the section sizes such that the linker will
484
     compute the output section sizes correctly.  We set the .stab
485
     size to not include the entries we don't want.  We set
486
     SEC_EXCLUDE for the .stabstr section, so that it will be dropped
487
     from the link.  We record the size of the strtab in the first
488
     .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
489
     for that section.  */
490
0
  stabsec->size = (count - skip) * STABSIZE;
491
0
  if (stabsec->size == 0)
492
0
    stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
493
0
  stabstrsec->flags |= SEC_EXCLUDE | SEC_KEEP;
494
0
  sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings);
495
496
  /* Calculate the `cumulative_skips' array now that stabs have been
497
     deleted for this section.  */
498
499
0
  if (skip != 0)
500
0
    {
501
0
      bfd_size_type i, offset;
502
0
      bfd_size_type *pskips;
503
504
0
      amt = count * sizeof (bfd_size_type);
505
0
      secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
506
0
      if (secinfo->cumulative_skips == NULL)
507
0
  goto error_return;
508
509
0
      pskips = secinfo->cumulative_skips;
510
0
      pstridx = secinfo->stridxs;
511
0
      offset = 0;
512
513
0
      for (i = 0; i < count; i++, pskips++, pstridx++)
514
0
  {
515
0
    *pskips = offset;
516
0
    if (*pstridx == (bfd_size_type) -1)
517
0
      offset += STABSIZE;
518
0
  }
519
520
0
      BFD_ASSERT (offset != 0);
521
0
    }
522
523
0
  return true;
524
525
0
 error_return:
526
0
  free (stabbuf);
527
0
  free (stabstrbuf);
528
0
  return false;
529
0
}
530
531
/*
532
INTERNAL_FUNCTION
533
  _bfd_discard_section_stabs
534
535
SYNOPSIS
536
  bool _bfd_discard_section_stabs
537
    (bfd *, asection *, bool (*) (bfd_vma, void *), void *);
538
539
DESCRIPTION
540
  This function is called for each input file before the stab
541
  section is relocated.  It discards stab entries for discarded
542
  functions and variables.  The function returns TRUE iff
543
  any entries have been deleted.
544
*/
545
546
bool
547
_bfd_discard_section_stabs (bfd *abfd,
548
          asection *stabsec,
549
          bool (*reloc_symbol_deleted_p) (bfd_vma, void *),
550
          void * cookie)
551
0
{
552
0
  bfd_size_type count, amt;
553
0
  struct stab_section_info *secinfo;
554
0
  bfd_byte *stabbuf = NULL;
555
0
  bfd_byte *sym, *symend;
556
0
  bfd_size_type skip;
557
0
  bfd_size_type *pstridx;
558
0
  int deleting;
559
560
0
  if (stabsec->size == 0 || (stabsec->flags & SEC_HAS_CONTENTS) == 0)
561
    /* This file does not contain stabs debugging information.  */
562
0
    return false;
563
564
0
  if (stabsec->size % STABSIZE != 0)
565
    /* Something is wrong with the format of these stab symbols.
566
       Don't try to optimize them.  */
567
0
    return false;
568
569
0
  if ((stabsec->output_section != NULL
570
0
       && bfd_is_abs_section (stabsec->output_section)))
571
    /* At least one of the sections is being discarded from the
572
       link, so we should just ignore them.  */
573
0
    return false;
574
575
  /* We should have initialized our data in _bfd_link_section_stabs.
576
     If there was some bizarre error reading the string sections, though,
577
     we might not have.  Bail rather than asserting.  */
578
0
  if (stabsec->sec_info_type != SEC_INFO_TYPE_STABS)
579
0
    return false;
580
581
0
  count = stabsec->rawsize / STABSIZE;
582
0
  secinfo = stabsec->sec_info;
583
584
  /* Read the stabs information from abfd.  */
585
0
  if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf))
586
0
    goto error_return;
587
588
  /* Look through the stabs symbols and discard any information for
589
     discarded functions.  */
590
0
  skip = 0;
591
0
  deleting = -1;
592
593
0
  symend = stabbuf + stabsec->rawsize;
594
0
  for (sym = stabbuf, pstridx = secinfo->stridxs;
595
0
       sym < symend;
596
0
       sym += STABSIZE, ++pstridx)
597
0
    {
598
0
      int type;
599
600
0
      if (*pstridx == (bfd_size_type) -1)
601
  /* This stab was deleted in a previous pass.  */
602
0
  continue;
603
604
0
      type = sym[TYPEOFF];
605
606
0
      if (type == (int) N_FUN)
607
0
  {
608
0
    int strx = bfd_get_32 (abfd, sym + STRDXOFF);
609
610
0
    if (strx == 0)
611
0
      {
612
0
        if (deleting)
613
0
    {
614
0
      skip++;
615
0
      *pstridx = -1;
616
0
    }
617
0
        deleting = -1;
618
0
        continue;
619
0
      }
620
0
    deleting = 0;
621
0
    if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
622
0
      deleting = 1;
623
0
  }
624
625
0
      if (deleting == 1)
626
0
  {
627
0
    *pstridx = -1;
628
0
    skip++;
629
0
  }
630
0
      else if (deleting == -1)
631
0
  {
632
    /* Outside of a function.  Check for deleted variables.  */
633
0
    if (type == (int) N_STSYM || type == (int) N_LCSYM)
634
0
      if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
635
0
        {
636
0
    *pstridx = -1;
637
0
    skip ++;
638
0
        }
639
    /* We should also check for N_GSYM entries which reference a
640
       deleted global, but those are less harmful to debuggers
641
       and would require parsing the stab strings.  */
642
0
  }
643
0
    }
644
645
0
  free (stabbuf);
646
0
  stabbuf = NULL;
647
648
  /* Shrink the stabsec as needed.  */
649
0
  stabsec->size -= skip * STABSIZE;
650
0
  if (stabsec->size == 0)
651
0
    stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
652
653
  /* Recalculate the `cumulative_skips' array now that stabs have been
654
     deleted for this section.  */
655
656
0
  if (skip != 0)
657
0
    {
658
0
      bfd_size_type i, offset;
659
0
      bfd_size_type *pskips;
660
661
0
      if (secinfo->cumulative_skips == NULL)
662
0
  {
663
0
    amt = count * sizeof (bfd_size_type);
664
0
    secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
665
0
    if (secinfo->cumulative_skips == NULL)
666
0
      goto error_return;
667
0
  }
668
669
0
      pskips = secinfo->cumulative_skips;
670
0
      pstridx = secinfo->stridxs;
671
0
      offset = 0;
672
673
0
      for (i = 0; i < count; i++, pskips++, pstridx++)
674
0
  {
675
0
    *pskips = offset;
676
0
    if (*pstridx == (bfd_size_type) -1)
677
0
      offset += STABSIZE;
678
0
  }
679
680
0
      BFD_ASSERT (offset != 0);
681
0
    }
682
683
0
  return skip > 0;
684
685
0
 error_return:
686
0
  free (stabbuf);
687
0
  return false;
688
0
}
689
690
/*
691
INTERNAL_FUNCTION
692
  _bfd_write_section_stabs
693
694
SYNOPSIS
695
  bool _bfd_write_section_stabs
696
    (bfd *, struct stab_info *, asection *, bfd_byte *);
697
698
DESCRIPTION
699
  Write out the stab section.  This is called with the relocated
700
  contents.
701
*/
702
703
bool
704
_bfd_write_section_stabs (bfd *output_bfd,
705
        struct stab_info *sinfo,
706
        asection *stabsec,
707
        bfd_byte *contents)
708
0
{
709
0
  struct stab_section_info *secinfo = stabsec->sec_info;
710
0
  struct stab_excl_list *e;
711
0
  bfd_byte *sym, *tosym, *symend;
712
0
  bfd_size_type *pstridx;
713
714
0
  if (stabsec->sec_info_type != SEC_INFO_TYPE_STABS)
715
0
    return bfd_set_section_contents (output_bfd, stabsec->output_section,
716
0
             contents, stabsec->output_offset,
717
0
             stabsec->size);
718
719
  /* Handle each N_BINCL entry.  */
720
0
  for (e = secinfo->excls; e != NULL; e = e->next)
721
0
    {
722
0
      bfd_byte *excl_sym;
723
724
0
      BFD_ASSERT (e->offset < stabsec->rawsize);
725
0
      excl_sym = contents + e->offset;
726
0
      bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
727
0
      excl_sym[TYPEOFF] = e->type;
728
0
    }
729
730
  /* Copy over all the stabs symbols, omitting the ones we don't want,
731
     and correcting the string indices for those we do want.  */
732
0
  tosym = contents;
733
0
  symend = contents + stabsec->rawsize;
734
0
  for (sym = contents, pstridx = secinfo->stridxs;
735
0
       sym < symend;
736
0
       sym += STABSIZE, ++pstridx)
737
0
    {
738
0
      if (*pstridx != (bfd_size_type) -1)
739
0
  {
740
0
    if (tosym != sym)
741
0
      memcpy (tosym, sym, STABSIZE);
742
0
    bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
743
744
0
    if (sym[TYPEOFF] == 0)
745
0
      {
746
        /* This is the header symbol for the stabs section.  We
747
     don't really need one, since we have merged all the
748
     input stabs sections into one, but we generate one
749
     for the benefit of readers which expect to see one.  */
750
0
        BFD_ASSERT (sym == contents);
751
0
        bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
752
0
        tosym + VALOFF);
753
0
        bfd_put_16 (output_bfd,
754
0
        stabsec->output_section->size / STABSIZE - 1,
755
0
        tosym + DESCOFF);
756
0
      }
757
758
0
    tosym += STABSIZE;
759
0
  }
760
0
    }
761
762
0
  BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->size);
763
764
0
  return bfd_set_section_contents (output_bfd, stabsec->output_section,
765
0
           contents, (file_ptr) stabsec->output_offset,
766
0
           stabsec->size);
767
0
}
768
769
/*
770
INTERNAL_FUNCTION
771
  _bfd_write_stab_strings
772
773
SYNOPSIS
774
  bool _bfd_write_stab_strings (bfd *, struct stab_info *);
775
776
DESCRIPTION
777
  Write out the .stabstr section.
778
*/
779
780
bool
781
_bfd_write_stab_strings (bfd *output_bfd, struct stab_info *sinfo)
782
0
{
783
0
  if (bfd_is_abs_section (sinfo->stabstr->output_section))
784
    /* The section was discarded from the link.  */
785
0
    return true;
786
787
0
  BFD_ASSERT ((sinfo->stabstr->output_offset
788
0
         + _bfd_stringtab_size (sinfo->strings))
789
0
        <= sinfo->stabstr->output_section->size);
790
791
0
  if (bfd_seek (output_bfd,
792
0
    (file_ptr) (sinfo->stabstr->output_section->filepos
793
0
          + sinfo->stabstr->output_offset),
794
0
    SEEK_SET) != 0)
795
0
    return false;
796
797
0
  if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
798
0
    return false;
799
800
  /* We no longer need the stabs information.  */
801
0
  _bfd_stringtab_free (sinfo->strings);
802
0
  bfd_hash_table_free (&sinfo->includes);
803
804
0
  return true;
805
0
}
806
807
/*
808
INTERNAL_FUNCTION
809
  _bfd_stab_section_offset
810
811
SYNOPSIS
812
  bfd_vma _bfd_stab_section_offset (asection *, bfd_vma);
813
814
DESCRIPTION
815
  Adjust an address in the .stab section.  Given OFFSET within
816
  STABSEC, this returns the new offset in the adjusted stab section,
817
  or -1 if the address refers to a stab which has been removed.
818
*/
819
820
bfd_vma
821
_bfd_stab_section_offset (asection *stabsec,
822
        bfd_vma offset)
823
0
{
824
0
  struct stab_section_info *secinfo = stabsec->sec_info;
825
826
0
  if (stabsec->sec_info_type != SEC_INFO_TYPE_STABS)
827
0
    return offset;
828
829
0
  if (offset >= stabsec->rawsize)
830
0
    return offset - stabsec->rawsize + stabsec->size;
831
832
0
  if (secinfo->cumulative_skips)
833
0
    {
834
0
      bfd_vma i;
835
836
0
      i = offset / STABSIZE;
837
838
0
      if (secinfo->stridxs [i] == (bfd_size_type) -1)
839
0
  return (bfd_vma) -1;
840
841
0
      return offset - secinfo->cumulative_skips [i];
842
0
    }
843
844
0
  return offset;
845
0
}