1 /* Stabs in sections linking support.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 Free Software Foundation, Inc.
4 Written by Ian Lance Taylor, Cygnus Support.
6 This file is part of BFD, the Binary File Descriptor library.
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 3 of the License, or
11 (at your option) any later version.
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.
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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 /* This file contains support for linking stabs in sections, as used
30 #include "aout/stab_gnu.h"
31 #include "safe-ctype.h"
33 /* Stabs entries use a 12 byte format:
34 4 byte string table index
36 1 byte stab other field
37 2 byte stab desc field
39 FIXME: This will have to change for a 64 bit object format.
41 The stabs symbols are divided into compilation units. For the
42 first entry in each unit, the type of 0, the value is the length of
43 the string table for this unit, and the desc field is the number of
44 stabs symbols for this unit. */
53 /* A linked list of totals that we have found for a particular header
54 file. A total is a unique identifier for a particular BINCL...EINCL
55 sequence of STABs that can be used to identify duplicate sequences.
56 It consists of three fields, 'sum_chars' which is the sum of all the
57 STABS characters; 'num_chars' which is the number of these charactes
58 and 'symb' which is a buffer of all the symbols in the sequence. This
59 buffer is only checked as a last resort. */
61 struct stab_link_includes_totals
63 struct stab_link_includes_totals *next;
64 bfd_vma sum_chars; /* Accumulated sum of STABS characters. */
65 bfd_vma num_chars; /* Number of STABS characters. */
66 const char* symb; /* The STABS characters themselves. */
69 /* An entry in the header file hash table. */
71 struct stab_link_includes_entry
73 struct bfd_hash_entry root;
74 /* List of totals we have found for this file. */
75 struct stab_link_includes_totals *totals;
78 /* This structure is used to hold a list of N_BINCL symbols, some of
79 which might be converted into N_EXCL symbols. */
83 /* The next symbol to convert. */
84 struct stab_excl_list *next;
85 /* The offset to this symbol in the section contents. */
87 /* The value to use for the symbol. */
89 /* The type of this symbol (N_BINCL or N_EXCL). */
93 /* This structure is stored with each .stab section. */
95 struct stab_section_info
97 /* This is a linked list of N_BINCL symbols which should be
98 converted into N_EXCL symbols. */
99 struct stab_excl_list *excls;
101 /* This is used to map input stab offsets within their sections
102 to output stab offsets, to take into account stabs that have
103 been deleted. If it is NULL, the output offsets are the same
104 as the input offsets, because no stabs have been deleted from
105 this section. Otherwise the i'th entry is the number of
106 bytes of stabs that have been deleted prior to the i'th
108 bfd_size_type *cumulative_skips;
110 /* This is an array of string indices. For each stab symbol, we
111 store the string index here. If a stab symbol should not be
112 included in the final output, the string index is -1. */
113 bfd_size_type stridxs[1];
117 /* The function to create a new entry in the header file hash table. */
119 static struct bfd_hash_entry *
120 stab_link_includes_newfunc (struct bfd_hash_entry *entry,
121 struct bfd_hash_table *table,
124 struct stab_link_includes_entry *ret =
125 (struct stab_link_includes_entry *) entry;
127 /* Allocate the structure if it has not already been allocated by a
130 ret = bfd_hash_allocate (table,
131 sizeof (struct stab_link_includes_entry));
135 /* Call the allocation method of the superclass. */
136 ret = ((struct stab_link_includes_entry *)
137 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
139 /* Set local fields. */
142 return (struct bfd_hash_entry *) ret;
145 /* This function is called for each input file from the add_symbols
146 pass of the linker. */
149 _bfd_link_section_stabs (bfd *abfd,
150 struct stab_info *sinfo,
152 asection *stabstrsec,
154 bfd_size_type *pstring_offset)
157 bfd_size_type count, amt;
158 struct stab_section_info *secinfo;
159 bfd_byte *stabbuf = NULL;
160 bfd_byte *stabstrbuf = NULL;
161 bfd_byte *sym, *symend;
162 bfd_size_type stroff, next_stroff, skip;
163 bfd_size_type *pstridx;
165 if (stabsec->size == 0
166 || stabstrsec->size == 0)
167 /* This file does not contain stabs debugging information. */
170 if (stabsec->size % STABSIZE != 0)
171 /* Something is wrong with the format of these stab symbols.
172 Don't try to optimize them. */
175 if ((stabstrsec->flags & SEC_RELOC) != 0)
176 /* We shouldn't see relocations in the strings, and we aren't
177 prepared to handle them. */
180 if (bfd_is_abs_section (stabsec->output_section)
181 || bfd_is_abs_section (stabstrsec->output_section))
182 /* At least one of the sections is being discarded from the
183 link, so we should just ignore them. */
188 if (sinfo->stabstr == NULL)
192 /* Initialize the stabs information we need to keep track of. */
194 sinfo->strings = _bfd_stringtab_init ();
195 if (sinfo->strings == NULL)
197 /* Make sure the first byte is zero. */
198 (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE);
199 if (! bfd_hash_table_init (&sinfo->includes,
200 stab_link_includes_newfunc,
201 sizeof (struct stab_link_includes_entry)))
203 flags = (SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING
204 | SEC_LINKER_CREATED);
205 sinfo->stabstr = bfd_make_section_anyway_with_flags (abfd, ".stabstr",
207 if (sinfo->stabstr == NULL)
211 /* Initialize the information we are going to store for this .stab
213 count = stabsec->size / STABSIZE;
215 amt = sizeof (struct stab_section_info);
216 amt += (count - 1) * sizeof (bfd_size_type);
217 *psecinfo = bfd_alloc (abfd, amt);
218 if (*psecinfo == NULL)
221 secinfo = (struct stab_section_info *) *psecinfo;
222 secinfo->excls = NULL;
223 stabsec->rawsize = stabsec->size;
224 secinfo->cumulative_skips = NULL;
225 memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
227 /* Read the stabs information from abfd. */
228 if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf)
229 || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf))
232 /* Look through the stabs symbols, work out the new string indices,
233 and identify N_BINCL symbols which can be eliminated. */
235 /* The stabs sections can be split when
236 -split-by-reloc/-split-by-file is used. We must keep track of
237 each stab section's place in the single concatenated string
239 next_stroff = pstring_offset ? *pstring_offset : 0;
242 symend = stabbuf + stabsec->size;
243 for (sym = stabbuf, pstridx = secinfo->stridxs;
245 sym += STABSIZE, ++pstridx)
247 bfd_size_type symstroff;
252 /* This symbol has already been handled by an N_BINCL pass. */
259 /* Special type 0 stabs indicate the offset to the next
260 string table. We only copy the very first one. */
261 stroff = next_stroff;
262 next_stroff += bfd_get_32 (abfd, sym + 8);
264 *pstring_offset = next_stroff;
267 *pstridx = (bfd_size_type) -1;
274 /* Store the string in the hash table, and record the index. */
275 symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
276 if (symstroff >= stabstrsec->size)
278 (*_bfd_error_handler)
279 (_("%B(%A+0x%lx): Stabs entry has invalid string index."),
280 abfd, stabsec, (long) (sym - stabbuf));
281 bfd_set_error (bfd_error_bad_value);
284 string = (char *) stabstrbuf + symstroff;
285 *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE);
287 /* An N_BINCL symbol indicates the start of the stabs entries
288 for a header file. We need to scan ahead to the next N_EINCL
289 symbol, ignoring nesting, adding up all the characters in the
290 symbol names, not including the file numbers in types (the
291 first number after an open parenthesis). */
292 if (type == (int) N_BINCL)
301 struct stab_link_includes_entry * incl_entry;
302 struct stab_link_includes_totals * t;
303 struct stab_excl_list * ne;
305 symb = symb_rover = NULL;
306 sum_chars = num_chars = 0;
309 for (incl_sym = sym + STABSIZE;
311 incl_sym += STABSIZE)
315 incl_type = incl_sym[TYPEOFF];
318 else if (incl_type == (int) N_EXCL)
320 else if (incl_type == (int) N_EINCL)
326 else if (incl_type == (int) N_BINCL)
332 str = ((char *) stabstrbuf
334 + bfd_get_32 (abfd, incl_sym + STRDXOFF));
335 for (; *str != '\0'; str++)
337 if (num_chars >= buf_len)
339 buf_len += 32 * 1024;
340 symb = bfd_realloc_or_free (symb, buf_len);
343 symb_rover = symb + num_chars;
345 * symb_rover ++ = * str;
350 /* Skip the file number. */
352 while (ISDIGIT (*str))
360 BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb));
362 /* If we have already included a header file with the same
363 value, then replaced this one with an N_EXCL symbol. */
364 incl_entry = (struct stab_link_includes_entry * )
365 bfd_hash_lookup (&sinfo->includes, string, TRUE, TRUE);
366 if (incl_entry == NULL)
369 for (t = incl_entry->totals; t != NULL; t = t->next)
370 if (t->sum_chars == sum_chars
371 && t->num_chars == num_chars
372 && memcmp (t->symb, symb, num_chars) == 0)
375 /* Record this symbol, so that we can set the value
378 ne = bfd_alloc (abfd, amt);
381 ne->offset = sym - stabbuf;
383 ne->type = (int) N_BINCL;
384 ne->next = secinfo->excls;
389 /* This is the first time we have seen this header file
390 with this set of stabs strings. */
391 t = bfd_hash_allocate (&sinfo->includes, sizeof *t);
394 t->sum_chars = sum_chars;
395 t->num_chars = num_chars;
396 t->symb = symb = bfd_realloc_or_free (symb, num_chars); /* Trim data down. */
397 t->next = incl_entry->totals;
398 incl_entry->totals = t;
402 bfd_size_type *incl_pstridx;
404 /* We have seen this header file before. Tell the final
405 pass to change the type to N_EXCL. */
406 ne->type = (int) N_EXCL;
408 /* Free off superfluous symbols. */
411 /* Mark the skipped symbols. */
414 for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
416 incl_sym += STABSIZE, ++incl_pstridx)
420 incl_type = incl_sym[TYPEOFF];
422 if (incl_type == (int) N_EINCL)
426 *incl_pstridx = (bfd_size_type) -1;
432 else if (incl_type == (int) N_BINCL)
434 else if (incl_type == (int) N_EXCL)
435 /* Keep existing exclusion marks. */
439 *incl_pstridx = (bfd_size_type) -1;
452 /* We need to set the section sizes such that the linker will
453 compute the output section sizes correctly. We set the .stab
454 size to not include the entries we don't want. We set
455 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
456 from the link. We record the size of the strtab in the first
457 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
459 stabsec->size = (count - skip) * STABSIZE;
460 if (stabsec->size == 0)
461 stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
462 stabstrsec->flags |= SEC_EXCLUDE | SEC_KEEP;
463 sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings);
465 /* Calculate the `cumulative_skips' array now that stabs have been
466 deleted for this section. */
470 bfd_size_type i, offset;
471 bfd_size_type *pskips;
473 amt = count * sizeof (bfd_size_type);
474 secinfo->cumulative_skips = bfd_alloc (abfd, amt);
475 if (secinfo->cumulative_skips == NULL)
478 pskips = secinfo->cumulative_skips;
479 pstridx = secinfo->stridxs;
482 for (i = 0; i < count; i++, pskips++, pstridx++)
485 if (*pstridx == (bfd_size_type) -1)
489 BFD_ASSERT (offset != 0);
497 if (stabstrbuf != NULL)
502 /* This function is called for each input file before the stab
503 section is relocated. It discards stab entries for discarded
504 functions and variables. The function returns TRUE iff
505 any entries have been deleted.
509 _bfd_discard_section_stabs (bfd *abfd,
512 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
515 bfd_size_type count, amt;
516 struct stab_section_info *secinfo;
517 bfd_byte *stabbuf = NULL;
518 bfd_byte *sym, *symend;
520 bfd_size_type *pstridx;
523 if (stabsec->size == 0)
524 /* This file does not contain stabs debugging information. */
527 if (stabsec->size % STABSIZE != 0)
528 /* Something is wrong with the format of these stab symbols.
529 Don't try to optimize them. */
532 if ((stabsec->output_section != NULL
533 && bfd_is_abs_section (stabsec->output_section)))
534 /* At least one of the sections is being discarded from the
535 link, so we should just ignore them. */
538 /* We should have initialized our data in _bfd_link_stab_sections.
539 If there was some bizarre error reading the string sections, though,
540 we might not have. Bail rather than asserting. */
541 if (psecinfo == NULL)
544 count = stabsec->rawsize / STABSIZE;
545 secinfo = (struct stab_section_info *) psecinfo;
547 /* Read the stabs information from abfd. */
548 if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf))
551 /* Look through the stabs symbols and discard any information for
552 discarded functions. */
556 symend = stabbuf + stabsec->rawsize;
557 for (sym = stabbuf, pstridx = secinfo->stridxs;
559 sym += STABSIZE, ++pstridx)
563 if (*pstridx == (bfd_size_type) -1)
564 /* This stab was deleted in a previous pass. */
569 if (type == (int) N_FUN)
571 int strx = bfd_get_32 (abfd, sym + STRDXOFF);
584 if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
593 else if (deleting == -1)
595 /* Outside of a function. Check for deleted variables. */
596 if (type == (int) N_STSYM || type == (int) N_LCSYM)
597 if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
602 /* We should also check for N_GSYM entries which reference a
603 deleted global, but those are less harmful to debuggers
604 and would require parsing the stab strings. */
611 /* Shrink the stabsec as needed. */
612 stabsec->size -= skip * STABSIZE;
613 if (stabsec->size == 0)
614 stabsec->flags |= SEC_EXCLUDE | SEC_KEEP;
616 /* Recalculate the `cumulative_skips' array now that stabs have been
617 deleted for this section. */
621 bfd_size_type i, offset;
622 bfd_size_type *pskips;
624 if (secinfo->cumulative_skips == NULL)
626 amt = count * sizeof (bfd_size_type);
627 secinfo->cumulative_skips = bfd_alloc (abfd, amt);
628 if (secinfo->cumulative_skips == NULL)
632 pskips = secinfo->cumulative_skips;
633 pstridx = secinfo->stridxs;
636 for (i = 0; i < count; i++, pskips++, pstridx++)
639 if (*pstridx == (bfd_size_type) -1)
643 BFD_ASSERT (offset != 0);
654 /* Write out the stab section. This is called with the relocated
658 _bfd_write_section_stabs (bfd *output_bfd,
659 struct stab_info *sinfo,
664 struct stab_section_info *secinfo;
665 struct stab_excl_list *e;
666 bfd_byte *sym, *tosym, *symend;
667 bfd_size_type *pstridx;
669 secinfo = (struct stab_section_info *) *psecinfo;
672 return bfd_set_section_contents (output_bfd, stabsec->output_section,
673 contents, stabsec->output_offset,
676 /* Handle each N_BINCL entry. */
677 for (e = secinfo->excls; e != NULL; e = e->next)
681 BFD_ASSERT (e->offset < stabsec->rawsize);
682 excl_sym = contents + e->offset;
683 bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
684 excl_sym[TYPEOFF] = e->type;
687 /* Copy over all the stabs symbols, omitting the ones we don't want,
688 and correcting the string indices for those we do want. */
690 symend = contents + stabsec->rawsize;
691 for (sym = contents, pstridx = secinfo->stridxs;
693 sym += STABSIZE, ++pstridx)
695 if (*pstridx != (bfd_size_type) -1)
698 memcpy (tosym, sym, STABSIZE);
699 bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
701 if (sym[TYPEOFF] == 0)
703 /* This is the header symbol for the stabs section. We
704 don't really need one, since we have merged all the
705 input stabs sections into one, but we generate one
706 for the benefit of readers which expect to see one. */
707 BFD_ASSERT (sym == contents);
708 bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
710 bfd_put_16 (output_bfd,
711 stabsec->output_section->size / STABSIZE - 1,
719 BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->size);
721 return bfd_set_section_contents (output_bfd, stabsec->output_section,
722 contents, (file_ptr) stabsec->output_offset,
726 /* Write out the .stabstr section. */
729 _bfd_write_stab_strings (bfd *output_bfd, struct stab_info *sinfo)
731 if (bfd_is_abs_section (sinfo->stabstr->output_section))
732 /* The section was discarded from the link. */
735 BFD_ASSERT ((sinfo->stabstr->output_offset
736 + _bfd_stringtab_size (sinfo->strings))
737 <= sinfo->stabstr->output_section->size);
739 if (bfd_seek (output_bfd,
740 (file_ptr) (sinfo->stabstr->output_section->filepos
741 + sinfo->stabstr->output_offset),
745 if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
748 /* We no longer need the stabs information. */
749 _bfd_stringtab_free (sinfo->strings);
750 bfd_hash_table_free (&sinfo->includes);
755 /* Adjust an address in the .stab section. Given OFFSET within
756 STABSEC, this returns the new offset in the adjusted stab section,
757 or -1 if the address refers to a stab which has been removed. */
760 _bfd_stab_section_offset (asection *stabsec,
764 struct stab_section_info *secinfo;
766 secinfo = (struct stab_section_info *) psecinfo;
771 if (offset >= stabsec->rawsize)
772 return offset - stabsec->rawsize + stabsec->size;
774 if (secinfo->cumulative_skips)
778 i = offset / STABSIZE;
780 if (secinfo->stridxs [i] == (bfd_size_type) -1)
783 return offset - secinfo->cumulative_skips [i];