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