daily update
[binutils.git] / bfd / stabs.c
blob25fae29ccc76331ae1272461729e423207157765
1 /* Stabs in sections linking support.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
3 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 2 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, MA 02110-1301, USA. */
22 /* This file contains support for linking stabs in sections, as used
23 on COFF and ELF. */
25 #include "bfd.h"
26 #include "sysdep.h"
27 #include "libbfd.h"
28 #include "aout/stab_gnu.h"
29 #include "safe-ctype.h"
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.
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. */
44 #define STRDXOFF 0
45 #define TYPEOFF 4
46 #define OTHEROFF 5
47 #define DESCOFF 6
48 #define VALOFF 8
49 #define STABSIZE 12
51 /* A linked list of totals that we have found for a particular header
52 file. A total is a unique identifier for a particular BINCL...EINCL
53 sequence of STABs that can be used to identify duplicate sequences.
54 It consists of three fields, 'sum_chars' which is the sum of all the
55 STABS characters; 'num_chars' which is the number of these charactes
56 and 'symb' which is a buffer of all the symbols in the sequence. This
57 buffer is only checked as a last resort. */
59 struct stab_link_includes_totals
61 struct stab_link_includes_totals *next;
62 bfd_vma sum_chars; /* Accumulated sum of STABS characters. */
63 bfd_vma num_chars; /* Number of STABS characters. */
64 const char* symb; /* The STABS characters themselves. */
67 /* An entry in the header file hash table. */
69 struct stab_link_includes_entry
71 struct bfd_hash_entry root;
72 /* List of totals we have found for this file. */
73 struct stab_link_includes_totals *totals;
76 /* This structure is used to hold a list of N_BINCL symbols, some of
77 which might be converted into N_EXCL symbols. */
79 struct stab_excl_list
81 /* The next symbol to convert. */
82 struct stab_excl_list *next;
83 /* The offset to this symbol in the section contents. */
84 bfd_size_type offset;
85 /* The value to use for the symbol. */
86 bfd_vma val;
87 /* The type of this symbol (N_BINCL or N_EXCL). */
88 int type;
91 /* This structure is stored with each .stab section. */
93 struct stab_section_info
95 /* This is a linked list of N_BINCL symbols which should be
96 converted into N_EXCL symbols. */
97 struct stab_excl_list *excls;
99 /* This is used to map input stab offsets within their sections
100 to output stab offsets, to take into account stabs that have
101 been deleted. If it is NULL, the output offsets are the same
102 as the input offsets, because no stabs have been deleted from
103 this section. Otherwise the i'th entry is the number of
104 bytes of stabs that have been deleted prior to the i'th
105 stab. */
106 bfd_size_type *cumulative_skips;
108 /* This is an array of string indices. For each stab symbol, we
109 store the string index here. If a stab symbol should not be
110 included in the final output, the string index is -1. */
111 bfd_size_type stridxs[1];
115 /* The function to create a new entry in the header file hash table. */
117 static struct bfd_hash_entry *
118 stab_link_includes_newfunc (struct bfd_hash_entry *entry,
119 struct bfd_hash_table *table,
120 const char *string)
122 struct stab_link_includes_entry *ret =
123 (struct stab_link_includes_entry *) entry;
125 /* Allocate the structure if it has not already been allocated by a
126 subclass. */
127 if (ret == NULL)
128 ret = bfd_hash_allocate (table,
129 sizeof (struct stab_link_includes_entry));
130 if (ret == NULL)
131 return NULL;
133 /* Call the allocation method of the superclass. */
134 ret = ((struct stab_link_includes_entry *)
135 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
136 if (ret)
137 /* Set local fields. */
138 ret->totals = NULL;
140 return (struct bfd_hash_entry *) ret;
143 /* This function is called for each input file from the add_symbols
144 pass of the linker. */
146 bfd_boolean
147 _bfd_link_section_stabs (bfd *abfd,
148 struct stab_info *sinfo,
149 asection *stabsec,
150 asection *stabstrsec,
151 void * *psecinfo,
152 bfd_size_type *pstring_offset)
154 bfd_boolean first;
155 bfd_size_type count, amt;
156 struct stab_section_info *secinfo;
157 bfd_byte *stabbuf = NULL;
158 bfd_byte *stabstrbuf = NULL;
159 bfd_byte *sym, *symend;
160 bfd_size_type stroff, next_stroff, skip;
161 bfd_size_type *pstridx;
163 if (stabsec->size == 0
164 || stabstrsec->size == 0)
165 /* This file does not contain stabs debugging information. */
166 return TRUE;
168 if (stabsec->size % STABSIZE != 0)
169 /* Something is wrong with the format of these stab symbols.
170 Don't try to optimize them. */
171 return TRUE;
173 if ((stabstrsec->flags & SEC_RELOC) != 0)
174 /* We shouldn't see relocations in the strings, and we aren't
175 prepared to handle them. */
176 return TRUE;
178 if ((stabsec->output_section != NULL
179 && bfd_is_abs_section (stabsec->output_section))
180 || (stabstrsec->output_section != NULL
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. */
184 return TRUE;
186 first = FALSE;
188 if (sinfo->stabstr == NULL)
190 /* Initialize the stabs information we need to keep track of. */
191 first = TRUE;
192 sinfo->strings = _bfd_stringtab_init ();
193 if (sinfo->strings == NULL)
194 goto error_return;
195 /* Make sure the first byte is zero. */
196 (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE);
197 if (! bfd_hash_table_init (&sinfo->includes,
198 stab_link_includes_newfunc))
199 goto error_return;
200 sinfo->stabstr = bfd_make_section_anyway (abfd, ".stabstr");
201 if (sinfo->stabstr == NULL)
202 goto error_return;
203 sinfo->stabstr->flags |= (SEC_HAS_CONTENTS | SEC_READONLY
204 | SEC_DEBUGGING | SEC_LINKER_CREATED);
207 /* Initialize the information we are going to store for this .stab
208 section. */
209 count = stabsec->size / STABSIZE;
211 amt = sizeof (struct stab_section_info);
212 amt += (count - 1) * sizeof (bfd_size_type);
213 *psecinfo = bfd_alloc (abfd, amt);
214 if (*psecinfo == NULL)
215 goto error_return;
217 secinfo = (struct stab_section_info *) *psecinfo;
218 secinfo->excls = NULL;
219 stabsec->rawsize = stabsec->size;
220 secinfo->cumulative_skips = NULL;
221 memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type));
223 /* Read the stabs information from abfd. */
224 if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf)
225 || !bfd_malloc_and_get_section (abfd, stabstrsec, &stabstrbuf))
226 goto error_return;
228 /* Look through the stabs symbols, work out the new string indices,
229 and identify N_BINCL symbols which can be eliminated. */
230 stroff = 0;
231 /* The stabs sections can be split when
232 -split-by-reloc/-split-by-file is used. We must keep track of
233 each stab section's place in the single concatenated string
234 table. */
235 next_stroff = pstring_offset ? *pstring_offset : 0;
236 skip = 0;
238 symend = stabbuf + stabsec->size;
239 for (sym = stabbuf, pstridx = secinfo->stridxs;
240 sym < symend;
241 sym += STABSIZE, ++pstridx)
243 bfd_size_type symstroff;
244 int type;
245 const char *string;
247 if (*pstridx != 0)
248 /* This symbol has already been handled by an N_BINCL pass. */
249 continue;
251 type = sym[TYPEOFF];
253 if (type == 0)
255 /* Special type 0 stabs indicate the offset to the next
256 string table. We only copy the very first one. */
257 stroff = next_stroff;
258 next_stroff += bfd_get_32 (abfd, sym + 8);
259 if (pstring_offset)
260 *pstring_offset = next_stroff;
261 if (! first)
263 *pstridx = (bfd_size_type) -1;
264 ++skip;
265 continue;
267 first = FALSE;
270 /* Store the string in the hash table, and record the index. */
271 symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF);
272 if (symstroff >= stabstrsec->size)
274 (*_bfd_error_handler)
275 (_("%B(%A+0x%lx): Stabs entry has invalid string index."),
276 abfd, stabsec, (long) (sym - stabbuf));
277 bfd_set_error (bfd_error_bad_value);
278 goto error_return;
280 string = (char *) stabstrbuf + symstroff;
281 *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE);
283 /* An N_BINCL symbol indicates the start of the stabs entries
284 for a header file. We need to scan ahead to the next N_EINCL
285 symbol, ignoring nesting, adding up all the characters in the
286 symbol names, not including the file numbers in types (the
287 first number after an open parenthesis). */
288 if (type == (int) N_BINCL)
290 bfd_vma sum_chars;
291 bfd_vma num_chars;
292 bfd_vma buf_len = 0;
293 char * symb;
294 char * symb_rover;
295 int nest;
296 bfd_byte * incl_sym;
297 struct stab_link_includes_entry * incl_entry;
298 struct stab_link_includes_totals * t;
299 struct stab_excl_list * ne;
301 symb = symb_rover = NULL;
302 sum_chars = num_chars = 0;
303 nest = 0;
305 for (incl_sym = sym + STABSIZE;
306 incl_sym < symend;
307 incl_sym += STABSIZE)
309 int incl_type;
311 incl_type = incl_sym[TYPEOFF];
312 if (incl_type == 0)
313 break;
314 else if (incl_type == (int) N_EXCL)
315 continue;
316 else if (incl_type == (int) N_EINCL)
318 if (nest == 0)
319 break;
320 --nest;
322 else if (incl_type == (int) N_BINCL)
323 ++nest;
324 else if (nest == 0)
326 const char *str;
328 str = ((char *) stabstrbuf
329 + stroff
330 + bfd_get_32 (abfd, incl_sym + STRDXOFF));
331 for (; *str != '\0'; str++)
333 if (num_chars >= buf_len)
335 buf_len += 32 * 1024;
336 symb = bfd_realloc (symb, buf_len);
337 if (symb == NULL)
338 goto error_return;
339 symb_rover = symb + num_chars;
341 * symb_rover ++ = * str;
342 sum_chars += *str;
343 num_chars ++;
344 if (*str == '(')
346 /* Skip the file number. */
347 ++str;
348 while (ISDIGIT (*str))
349 ++str;
350 --str;
356 BFD_ASSERT (num_chars == (bfd_vma) (symb_rover - symb));
358 /* If we have already included a header file with the same
359 value, then replaced this one with an N_EXCL symbol. */
360 incl_entry = (struct stab_link_includes_entry * )
361 bfd_hash_lookup (&sinfo->includes, string, TRUE, TRUE);
362 if (incl_entry == NULL)
363 goto error_return;
365 for (t = incl_entry->totals; t != NULL; t = t->next)
366 if (t->sum_chars == sum_chars
367 && t->num_chars == num_chars
368 && memcmp (t->symb, symb, num_chars) == 0)
369 break;
371 /* Record this symbol, so that we can set the value
372 correctly. */
373 amt = sizeof *ne;
374 ne = bfd_alloc (abfd, amt);
375 if (ne == NULL)
376 goto error_return;
377 ne->offset = sym - stabbuf;
378 ne->val = sum_chars;
379 ne->type = (int) N_BINCL;
380 ne->next = secinfo->excls;
381 secinfo->excls = ne;
383 if (t == NULL)
385 /* This is the first time we have seen this header file
386 with this set of stabs strings. */
387 t = bfd_hash_allocate (&sinfo->includes, sizeof *t);
388 if (t == NULL)
389 goto error_return;
390 t->sum_chars = sum_chars;
391 t->num_chars = num_chars;
392 t->symb = bfd_realloc (symb, num_chars); /* Trim data down. */
393 t->next = incl_entry->totals;
394 incl_entry->totals = t;
396 else
398 bfd_size_type *incl_pstridx;
400 /* We have seen this header file before. Tell the final
401 pass to change the type to N_EXCL. */
402 ne->type = (int) N_EXCL;
404 /* Free off superfluous symbols. */
405 free (symb);
407 /* Mark the skipped symbols. */
409 nest = 0;
410 for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1;
411 incl_sym < symend;
412 incl_sym += STABSIZE, ++incl_pstridx)
414 int incl_type;
416 incl_type = incl_sym[TYPEOFF];
418 if (incl_type == (int) N_EINCL)
420 if (nest == 0)
422 *incl_pstridx = (bfd_size_type) -1;
423 ++skip;
424 break;
426 --nest;
428 else if (incl_type == (int) N_BINCL)
429 ++nest;
430 else if (incl_type == (int) N_EXCL)
431 /* Keep existing exclusion marks. */
432 continue;
433 else if (nest == 0)
435 *incl_pstridx = (bfd_size_type) -1;
436 ++skip;
443 free (stabbuf);
444 stabbuf = NULL;
445 free (stabstrbuf);
446 stabstrbuf = NULL;
448 /* We need to set the section sizes such that the linker will
449 compute the output section sizes correctly. We set the .stab
450 size to not include the entries we don't want. We set
451 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
452 from the link. We record the size of the strtab in the first
453 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
454 for that section. */
455 stabsec->size = (count - skip) * STABSIZE;
456 if (stabsec->size == 0)
457 stabsec->flags |= SEC_EXCLUDE;
458 stabstrsec->flags |= SEC_EXCLUDE;
459 sinfo->stabstr->size = _bfd_stringtab_size (sinfo->strings);
461 /* Calculate the `cumulative_skips' array now that stabs have been
462 deleted for this section. */
464 if (skip != 0)
466 bfd_size_type i, offset;
467 bfd_size_type *pskips;
469 amt = count * sizeof (bfd_size_type);
470 secinfo->cumulative_skips = bfd_alloc (abfd, amt);
471 if (secinfo->cumulative_skips == NULL)
472 goto error_return;
474 pskips = secinfo->cumulative_skips;
475 pstridx = secinfo->stridxs;
476 offset = 0;
478 for (i = 0; i < count; i++, pskips++, pstridx++)
480 *pskips = offset;
481 if (*pstridx == (bfd_size_type) -1)
482 offset += STABSIZE;
485 BFD_ASSERT (offset != 0);
488 return TRUE;
490 error_return:
491 if (stabbuf != NULL)
492 free (stabbuf);
493 if (stabstrbuf != NULL)
494 free (stabstrbuf);
495 return FALSE;
498 /* This function is called for each input file before the stab
499 section is relocated. It discards stab entries for discarded
500 functions and variables. The function returns TRUE iff
501 any entries have been deleted.
504 bfd_boolean
505 _bfd_discard_section_stabs (bfd *abfd,
506 asection *stabsec,
507 void * psecinfo,
508 bfd_boolean (*reloc_symbol_deleted_p) (bfd_vma, void *),
509 void * cookie)
511 bfd_size_type count, amt;
512 struct stab_section_info *secinfo;
513 bfd_byte *stabbuf = NULL;
514 bfd_byte *sym, *symend;
515 bfd_size_type skip;
516 bfd_size_type *pstridx;
517 int deleting;
519 if (stabsec->size == 0)
520 /* This file does not contain stabs debugging information. */
521 return FALSE;
523 if (stabsec->size % STABSIZE != 0)
524 /* Something is wrong with the format of these stab symbols.
525 Don't try to optimize them. */
526 return FALSE;
528 if ((stabsec->output_section != NULL
529 && bfd_is_abs_section (stabsec->output_section)))
530 /* At least one of the sections is being discarded from the
531 link, so we should just ignore them. */
532 return FALSE;
534 /* We should have initialized our data in _bfd_link_stab_sections.
535 If there was some bizarre error reading the string sections, though,
536 we might not have. Bail rather than asserting. */
537 if (psecinfo == NULL)
538 return FALSE;
540 count = stabsec->rawsize / STABSIZE;
541 secinfo = (struct stab_section_info *) psecinfo;
543 /* Read the stabs information from abfd. */
544 if (!bfd_malloc_and_get_section (abfd, stabsec, &stabbuf))
545 goto error_return;
547 /* Look through the stabs symbols and discard any information for
548 discarded functions. */
549 skip = 0;
550 deleting = -1;
552 symend = stabbuf + stabsec->rawsize;
553 for (sym = stabbuf, pstridx = secinfo->stridxs;
554 sym < symend;
555 sym += STABSIZE, ++pstridx)
557 int type;
559 if (*pstridx == (bfd_size_type) -1)
560 /* This stab was deleted in a previous pass. */
561 continue;
563 type = sym[TYPEOFF];
565 if (type == (int) N_FUN)
567 int strx = bfd_get_32 (abfd, sym + STRDXOFF);
569 if (strx == 0)
571 if (deleting)
573 skip++;
574 *pstridx = -1;
576 deleting = -1;
577 continue;
579 deleting = 0;
580 if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
581 deleting = 1;
584 if (deleting == 1)
586 *pstridx = -1;
587 skip++;
589 else if (deleting == -1)
591 /* Outside of a function. Check for deleted variables. */
592 if (type == (int) N_STSYM || type == (int) N_LCSYM)
593 if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie))
595 *pstridx = -1;
596 skip ++;
598 /* We should also check for N_GSYM entries which reference a
599 deleted global, but those are less harmful to debuggers
600 and would require parsing the stab strings. */
604 free (stabbuf);
605 stabbuf = NULL;
607 /* Shrink the stabsec as needed. */
608 stabsec->size -= skip * STABSIZE;
609 if (stabsec->size == 0)
610 stabsec->flags |= SEC_EXCLUDE;
612 /* Recalculate the `cumulative_skips' array now that stabs have been
613 deleted for this section. */
615 if (skip != 0)
617 bfd_size_type i, offset;
618 bfd_size_type *pskips;
620 if (secinfo->cumulative_skips == NULL)
622 amt = count * sizeof (bfd_size_type);
623 secinfo->cumulative_skips = bfd_alloc (abfd, amt);
624 if (secinfo->cumulative_skips == NULL)
625 goto error_return;
628 pskips = secinfo->cumulative_skips;
629 pstridx = secinfo->stridxs;
630 offset = 0;
632 for (i = 0; i < count; i++, pskips++, pstridx++)
634 *pskips = offset;
635 if (*pstridx == (bfd_size_type) -1)
636 offset += STABSIZE;
639 BFD_ASSERT (offset != 0);
642 return skip > 0;
644 error_return:
645 if (stabbuf != NULL)
646 free (stabbuf);
647 return FALSE;
650 /* Write out the stab section. This is called with the relocated
651 contents. */
653 bfd_boolean
654 _bfd_write_section_stabs (bfd *output_bfd,
655 struct stab_info *sinfo,
656 asection *stabsec,
657 void * *psecinfo,
658 bfd_byte *contents)
660 struct stab_section_info *secinfo;
661 struct stab_excl_list *e;
662 bfd_byte *sym, *tosym, *symend;
663 bfd_size_type *pstridx;
665 secinfo = (struct stab_section_info *) *psecinfo;
667 if (secinfo == NULL)
668 return bfd_set_section_contents (output_bfd, stabsec->output_section,
669 contents, stabsec->output_offset,
670 stabsec->size);
672 /* Handle each N_BINCL entry. */
673 for (e = secinfo->excls; e != NULL; e = e->next)
675 bfd_byte *excl_sym;
677 BFD_ASSERT (e->offset < stabsec->rawsize);
678 excl_sym = contents + e->offset;
679 bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF);
680 excl_sym[TYPEOFF] = e->type;
683 /* Copy over all the stabs symbols, omitting the ones we don't want,
684 and correcting the string indices for those we do want. */
685 tosym = contents;
686 symend = contents + stabsec->rawsize;
687 for (sym = contents, pstridx = secinfo->stridxs;
688 sym < symend;
689 sym += STABSIZE, ++pstridx)
691 if (*pstridx != (bfd_size_type) -1)
693 if (tosym != sym)
694 memcpy (tosym, sym, STABSIZE);
695 bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF);
697 if (sym[TYPEOFF] == 0)
699 /* This is the header symbol for the stabs section. We
700 don't really need one, since we have merged all the
701 input stabs sections into one, but we generate one
702 for the benefit of readers which expect to see one. */
703 BFD_ASSERT (sym == contents);
704 bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings),
705 tosym + VALOFF);
706 bfd_put_16 (output_bfd,
707 stabsec->output_section->size / STABSIZE - 1,
708 tosym + DESCOFF);
711 tosym += STABSIZE;
715 BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->size);
717 return bfd_set_section_contents (output_bfd, stabsec->output_section,
718 contents, (file_ptr) stabsec->output_offset,
719 stabsec->size);
722 /* Write out the .stabstr section. */
724 bfd_boolean
725 _bfd_write_stab_strings (bfd *output_bfd, struct stab_info *sinfo)
727 if (bfd_is_abs_section (sinfo->stabstr->output_section))
728 /* The section was discarded from the link. */
729 return TRUE;
731 BFD_ASSERT ((sinfo->stabstr->output_offset
732 + _bfd_stringtab_size (sinfo->strings))
733 <= sinfo->stabstr->output_section->size);
735 if (bfd_seek (output_bfd,
736 (file_ptr) (sinfo->stabstr->output_section->filepos
737 + sinfo->stabstr->output_offset),
738 SEEK_SET) != 0)
739 return FALSE;
741 if (! _bfd_stringtab_emit (output_bfd, sinfo->strings))
742 return FALSE;
744 /* We no longer need the stabs information. */
745 _bfd_stringtab_free (sinfo->strings);
746 bfd_hash_table_free (&sinfo->includes);
748 return TRUE;
751 /* Adjust an address in the .stab section. Given OFFSET within
752 STABSEC, this returns the new offset in the adjusted stab section,
753 or -1 if the address refers to a stab which has been removed. */
755 bfd_vma
756 _bfd_stab_section_offset (asection *stabsec,
757 void * psecinfo,
758 bfd_vma offset)
760 struct stab_section_info *secinfo;
762 secinfo = (struct stab_section_info *) psecinfo;
764 if (secinfo == NULL)
765 return offset;
767 if (offset >= stabsec->rawsize)
768 return offset - stabsec->rawsize + stabsec->size;
770 if (secinfo->cumulative_skips)
772 bfd_vma i;
774 i = offset / STABSIZE;
776 if (secinfo->stridxs [i] == (bfd_size_type) -1)
777 return (bfd_vma) -1;
779 return offset - secinfo->cumulative_skips [i];
782 return offset;