Coverage Report

Created: 2025-06-24 06:45

/src/binutils-gdb/bfd/stabs.c
Line
Count
Source (jump to first uncovered line)
1
/* Stabs in sections linking support.
2
   Copyright (C) 1996-2025 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 *, void **,
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
       void * *psecinfo,
180
       bfd_size_type *pstring_offset)
181
0
{
182
0
  bool first;
183
0
  bfd_size_type count, amt;
184
0
  struct stab_section_info *secinfo;
185
0
  bfd_byte *stabbuf = NULL;
186
0
  bfd_byte *stabstrbuf = NULL;
187
0
  bfd_byte *sym, *symend;
188
0
  bfd_size_type stroff, next_stroff, skip;
189
0
  bfd_size_type *pstridx;
190
191
0
  if (stabsec->size == 0
192
0
      || stabstrsec->size == 0
193
0
      || (stabsec->flags & SEC_HAS_CONTENTS) == 0
194
0
      || (stabstrsec->flags & SEC_HAS_CONTENTS) == 0)
195
    /* This file does not contain stabs debugging information.  */
196
0
    return true;
197
198
0
  if (stabsec->size % STABSIZE != 0)
199
    /* Something is wrong with the format of these stab symbols.
200
       Don't try to optimize them.  */
201
0
    return true;
202
203
0
  if ((stabstrsec->flags & SEC_RELOC) != 0)
204
    /* We shouldn't see relocations in the strings, and we aren't
205
       prepared to handle them.  */
206
0
    return true;
207
208
0
  if (bfd_is_abs_section (stabsec->output_section)
209
0
      || bfd_is_abs_section (stabstrsec->output_section))
210
    /* At least one of the sections is being discarded from the
211
       link, so we should just ignore them.  */
212
0
    return true;
213
214
0
  first = false;
215
216
0
  if (sinfo->stabstr == NULL)
217
0
    {
218
0
      flagword flags;
219
220
      /* Initialize the stabs information we need to keep track of.  */
221
0
      first = true;
222
0
      sinfo->strings = _bfd_stringtab_init ();
223
0
      if (sinfo->strings == NULL)
224
0
  goto error_return;
225
      /* Make sure the first byte is zero.  */
226
0
      (void) _bfd_stringtab_add (sinfo->strings, "", true, true);
227
0
      if (! bfd_hash_table_init (&sinfo->includes,
228
0
         stab_link_includes_newfunc,
229
0
         sizeof (struct stab_link_includes_entry)))
230
0
  goto error_return;
231
0
      flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING
232
0
         | SEC_LINKER_CREATED);
233
0
      sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr",
234
0
                 flags);
235
0
      if (sinfo->stabstr == NULL)
236
0
  goto error_return;
237
0
    }
238
239
  /* Initialize the information we are going to store for this .stab
240
     section.  */
241
0
  count = stabsec->size / STABSIZE;
242
243
0
  amt = sizeof (struct stab_section_info);
244
0
  amt += (count - 1) * sizeof (bfd_size_type);
245
0
  *psecinfo = bfd_alloc (abfd, amt);
246
0
  if (*psecinfo == NULL)
247
0
    goto error_return;
248
249
0
  secinfo = (struct stab_section_info *) *psecinfo;
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 *, void *, 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
          void * psecinfo,
550
          bool (*reloc_symbol_deleted_p) (bfd_vma, void *),
551
          void * cookie)
552
0
{
553
0
  bfd_size_type count, amt;
554
0
  struct stab_section_info *secinfo;
555
0
  bfd_byte *stabbuf = NULL;
556
0
  bfd_byte *sym, *symend;
557
0
  bfd_size_type skip;
558
0
  bfd_size_type *pstridx;
559
0
  int deleting;
560
561
0
  if (stabsec->size == 0 || (stabsec->flags & SEC_HAS_CONTENTS) == 0)
562
    /* This file does not contain stabs debugging information.  */
563
0
    return false;
564
565
0
  if (stabsec->size % STABSIZE != 0)
566
    /* Something is wrong with the format of these stab symbols.
567
       Don't try to optimize them.  */
568
0
    return false;
569
570
0
  if ((stabsec->output_section != NULL
571
0
       && bfd_is_abs_section (stabsec->output_section)))
572
    /* At least one of the sections is being discarded from the
573
       link, so we should just ignore them.  */
574
0
    return false;
575
576
  /* We should have initialized our data in _bfd_link_section_stabs.
577
     If there was some bizarre error reading the string sections, though,
578
     we might not have.  Bail rather than asserting.  */
579
0
  if (psecinfo == NULL)
580
0
    return false;
581
582
0
  count = stabsec->rawsize / STABSIZE;
583
0
  secinfo = (struct stab_section_info *) psecinfo;
584
585
  /* Read the stabs information from abfd.  */
586
0
  if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf))
587
0
    goto error_return;
588
589
  /* Look through the stabs symbols and discard any information for
590
     discarded functions.  */
591
0
  skip = 0;
592
0
  deleting = -1;
593
594
0
  symend = stabbuf + stabsec->rawsize;
595
0
  for (sym = stabbuf, pstridx = secinfo->stridxs;
596
0
       sym < symend;
597
0
       sym += STABSIZE, ++pstridx)
598
0
    {
599
0
      int type;
600
601
0
      if (*pstridx == (bfd_size_type) -1)
602
  /* This stab was deleted in a previous pass.  */
603
0
  continue;
604
605
0
      type = sym[TYPEOFF];
606
607
0
      if (type == (int) N_FUN)
608
0
  {
609
0
    int strx = bfd_get_32 (abfd, sym + STRDXOFF);
610
611
0
    if (strx == 0)
612
0
      {
613
0
        if (deleting)
614
0
    {
615
0
      skip++;
616
0
      *pstridx = -1;
617
0
    }
618
0
        deleting = -1;
619
0
        continue;
620
0
      }
621
0
    deleting = 0;
622
0
    if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
623
0
      deleting = 1;
624
0
  }
625
626
0
      if (deleting == 1)
627
0
  {
628
0
    *pstridx = -1;
629
0
    skip++;
630
0
  }
631
0
      else if (deleting == -1)
632
0
  {
633
    /* Outside of a function.  Check for deleted variables.  */
634
0
    if (type == (int) N_STSYM || type == (int) N_LCSYM)
635
0
      if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
636
0
        {
637
0
    *pstridx = -1;
638
0
    skip ++;
639
0
        }
640
    /* We should also check for N_GSYM entries which reference a
641
       deleted global, but those are less harmful to debuggers
642
       and would require parsing the stab strings.  */
643
0
  }
644
0
    }
645
646
0
  free (stabbuf);
647
0
  stabbuf = NULL;
648
649
  /* Shrink the stabsec as needed.  */
650
0
  stabsec->size -= skip * STABSIZE;
651
0
  if (stabsec->size == 0)
652
0
    stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
653
654
  /* Recalculate the `cumulative_skips' array now that stabs have been
655
     deleted for this section.  */
656
657
0
  if (skip != 0)
658
0
    {
659
0
      bfd_size_type i, offset;
660
0
      bfd_size_type *pskips;
661
662
0
      if (secinfo->cumulative_skips == NULL)
663
0
  {
664
0
    amt = count * sizeof (bfd_size_type);
665
0
    secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt);
666
0
    if (secinfo->cumulative_skips == NULL)
667
0
      goto error_return;
668
0
  }
669
670
0
      pskips = secinfo->cumulative_skips;
671
0
      pstridx = secinfo->stridxs;
672
0
      offset = 0;
673
674
0
      for (i = 0; i < count; i++, pskips++, pstridx++)
675
0
  {
676
0
    *pskips = offset;
677
0
    if (*pstridx == (bfd_size_type) -1)
678
0
      offset += STABSIZE;
679
0
  }
680
681
0
      BFD_ASSERT (offset != 0);
682
0
    }
683
684
0
  return skip > 0;
685
686
0
 error_return:
687
0
  free (stabbuf);
688
0
  return false;
689
0
}
690
691
/*
692
INTERNAL_FUNCTION
693
  _bfd_write_section_stabs
694
695
SYNOPSIS
696
  bool _bfd_write_section_stabs
697
    (bfd *, struct stab_info *, asection *, void **, bfd_byte *);
698
699
DESCRIPTION
700
  Write out the stab section.  This is called with the relocated
701
  contents.
702
*/
703
704
bool
705
_bfd_write_section_stabs (bfd *output_bfd,
706
        struct stab_info *sinfo,
707
        asection *stabsec,
708
        void * *psecinfo,
709
        bfd_byte *contents)
710
0
{
711
0
  struct stab_section_info *secinfo;
712
0
  struct stab_excl_list *e;
713
0
  bfd_byte *sym, *tosym, *symend;
714
0
  bfd_size_type *pstridx;
715
716
0
  secinfo = (struct stab_section_info *) *psecinfo;
717
718
0
  if (secinfo == NULL)
719
0
    return bfd_set_section_contents (output_bfd, stabsec->output_section,
720
0
             contents, stabsec->output_offset,
721
0
             stabsec->size);
722
723
  /* Handle each N_BINCL entry.  */
724
0
  for (e = secinfo->excls; e != NULL; e = e->next)
725
0
    {
726
0
      bfd_byte *excl_sym;
727
728
0
      BFD_ASSERT (e->offset < stabsec->rawsize);
729
0
      excl_sym = contents + e->offset;
730
0
      bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
731
0
      excl_sym[TYPEOFF] = e->type;
732
0
    }
733
734
  /* Copy over all the stabs symbols, omitting the ones we don't want,
735
     and correcting the string indices for those we do want.  */
736
0
  tosym = contents;
737
0
  symend = contents + stabsec->rawsize;
738
0
  for (sym = contents, pstridx = secinfo->stridxs;
739
0
       sym < symend;
740
0
       sym += STABSIZE, ++pstridx)
741
0
    {
742
0
      if (*pstridx != (bfd_size_type) -1)
743
0
  {
744
0
    if (tosym != sym)
745
0
      memcpy (tosym, sym, STABSIZE);
746
0
    bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
747
748
0
    if (sym[TYPEOFF] == 0)
749
0
      {
750
        /* This is the header symbol for the stabs section.  We
751
     don't really need one, since we have merged all the
752
     input stabs sections into one, but we generate one
753
     for the benefit of readers which expect to see one.  */
754
0
        BFD_ASSERT (sym == contents);
755
0
        bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
756
0
        tosym + VALOFF);
757
0
        bfd_put_16 (output_bfd,
758
0
        stabsec->output_section->size / STABSIZE - 1,
759
0
        tosym + DESCOFF);
760
0
      }
761
762
0
    tosym += STABSIZE;
763
0
  }
764
0
    }
765
766
0
  BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->size);
767
768
0
  return bfd_set_section_contents (output_bfd, stabsec->output_section,
769
0
           contents, (file_ptr) stabsec->output_offset,
770
0
           stabsec->size);
771
0
}
772
773
/*
774
INTERNAL_FUNCTION
775
  _bfd_write_stab_strings
776
777
SYNOPSIS
778
  bool _bfd_write_stab_strings (bfd *, struct stab_info *);
779
780
DESCRIPTION
781
  Write out the .stabstr section.
782
*/
783
784
bool
785
_bfd_write_stab_strings (bfd *output_bfd, struct stab_info *sinfo)
786
0
{
787
0
  if (bfd_is_abs_section (sinfo->stabstr->output_section))
788
    /* The section was discarded from the link.  */
789
0
    return true;
790
791
0
  BFD_ASSERT ((sinfo->stabstr->output_offset
792
0
         + _bfd_stringtab_size (sinfo->strings))
793
0
        <= sinfo->stabstr->output_section->size);
794
795
0
  if (bfd_seek (output_bfd,
796
0
    (file_ptr) (sinfo->stabstr->output_section->filepos
797
0
          + sinfo->stabstr->output_offset),
798
0
    SEEK_SET) != 0)
799
0
    return false;
800
801
0
  if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
802
0
    return false;
803
804
  /* We no longer need the stabs information.  */
805
0
  _bfd_stringtab_free (sinfo->strings);
806
0
  bfd_hash_table_free (&sinfo->includes);
807
808
0
  return true;
809
0
}
810
811
/*
812
INTERNAL_FUNCTION
813
  _bfd_stab_section_offset
814
815
SYNOPSIS
816
  bfd_vma _bfd_stab_section_offset (asection *, void *, bfd_vma);
817
818
DESCRIPTION
819
  Adjust an address in the .stab section.  Given OFFSET within
820
  STABSEC, this returns the new offset in the adjusted stab section,
821
  or -1 if the address refers to a stab which has been removed.
822
*/
823
824
bfd_vma
825
_bfd_stab_section_offset (asection *stabsec,
826
        void * psecinfo,
827
        bfd_vma offset)
828
0
{
829
0
  struct stab_section_info *secinfo;
830
831
0
  secinfo = (struct stab_section_info *) psecinfo;
832
833
0
  if (secinfo == NULL)
834
0
    return offset;
835
836
0
  if (offset >= stabsec->rawsize)
837
0
    return offset - stabsec->rawsize + stabsec->size;
838
839
0
  if (secinfo->cumulative_skips)
840
0
    {
841
0
      bfd_vma i;
842
843
0
      i = offset / STABSIZE;
844
845
0
      if (secinfo->stridxs [i] == (bfd_size_type) -1)
846
0
  return (bfd_vma) -1;
847
848
0
      return offset - secinfo->cumulative_skips [i];
849
0
    }
850
851
0
  return offset;
852
0
}