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.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 /* This file contains support for linking stabs in sections, as used
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
34 1 byte stab other field
35 2 byte stab desc field
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. */
51 /* A hash table used for header files with N_BINCL entries. */
53 struct stab_link_includes_table
55 struct bfd_hash_table root
;
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. */
65 struct stab_link_includes_totals
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. */
72 /* An entry in the header file hash table. */
74 struct stab_link_includes_entry
76 struct bfd_hash_entry root
;
77 /* List of totals we have found for this file. */
78 struct stab_link_includes_totals
*totals
;
81 /* Look up an entry in an the header file hash table. */
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)))
87 /* This structure is used to hold a list of N_BINCL symbols, some of
88 which might be converted into N_EXCL symbols. */
92 /* The next symbol to convert. */
93 struct stab_excl_list
*next
;
94 /* The offset to this symbol in the section contents. */
96 /* The value to use for the symbol. */
98 /* The type of this symbol (N_BINCL or N_EXCL). */
102 /* This structure is stored with each .stab section. */
104 struct stab_section_info
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
;
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
117 bfd_size_type
*cumulative_skips
;
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];
125 /* This structure is used to keep track of stabs in sections
126 information while linking. */
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. */
138 static struct bfd_hash_entry
*stab_link_includes_newfunc
139 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
141 /* The function to create a new entry in the header file hash table. */
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
;
149 struct stab_link_includes_entry
*ret
=
150 (struct stab_link_includes_entry
*) entry
;
152 /* Allocate the structure if it has not already been allocated by a
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
;
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
));
166 /* Set local fields. */
170 return (struct bfd_hash_entry
*) ret
;
173 /* This function is called for each input file from the add_symbols
174 pass of the linker. */
177 _bfd_link_section_stabs (abfd
, psinfo
, stabsec
, stabstrsec
, psecinfo
, pstring_offset
)
181 asection
*stabstrsec
;
183 bfd_size_type
*pstring_offset
;
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
;
195 if (stabsec
->_raw_size
== 0
196 || stabstrsec
->_raw_size
== 0)
198 /* This file does not contain stabs debugging information. */
202 if (stabsec
->_raw_size
% STABSIZE
!= 0)
204 /* Something is wrong with the format of these stab symbols.
205 Don't try to optimize them. */
209 if ((stabstrsec
->flags
& SEC_RELOC
) != 0)
211 /* We shouldn't see relocations in the strings, and we aren't
212 prepared to handle them. */
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
)))
221 /* At least one of the sections is being discarded from the
222 link, so we should just ignore them. */
230 /* Initialize the stabs information we need to keep track of. */
232 amt
= sizeof (struct stab_info
);
233 *psinfo
= (PTR
) bfd_alloc (abfd
, amt
);
236 sinfo
= (struct stab_info
*) *psinfo
;
237 sinfo
->strings
= _bfd_stringtab_init ();
238 if (sinfo
->strings
== NULL
)
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
,
246 sinfo
->stabstr
= bfd_make_section_anyway (abfd
, ".stabstr");
247 sinfo
->stabstr
->flags
|= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
250 sinfo
= (struct stab_info
*) *psinfo
;
252 /* Initialize the information we are going to store for this .stab
255 count
= stabsec
->_raw_size
/ STABSIZE
;
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
)
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
));
268 /* Read the stabs information from abfd. */
270 stabbuf
= (bfd_byte
*) bfd_malloc (stabsec
->_raw_size
);
271 stabstrbuf
= (bfd_byte
*) bfd_malloc (stabstrsec
->_raw_size
);
272 if (stabbuf
== NULL
|| stabstrbuf
== NULL
)
275 if (! bfd_get_section_contents (abfd
, stabsec
, stabbuf
, (bfd_vma
) 0,
277 || ! bfd_get_section_contents (abfd
, stabstrsec
, stabstrbuf
, (bfd_vma
) 0,
278 stabstrsec
->_raw_size
))
281 /* Look through the stabs symbols, work out the new string indices,
282 and identify N_BINCL symbols which can be eliminated. */
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
289 next_stroff
= pstring_offset
? *pstring_offset
: 0;
292 symend
= stabbuf
+ stabsec
->_raw_size
;
293 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
295 sym
+= STABSIZE
, ++pstridx
)
297 bfd_size_type symstroff
;
303 /* This symbol has already been handled by an N_BINCL pass. */
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);
316 *pstring_offset
= next_stroff
;
319 *pstridx
= (bfd_size_type
) -1;
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
)
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
);
338 string
= (char *) stabstrbuf
+ symstroff
;
339 *pstridx
= _bfd_stringtab_add (sinfo
->strings
, string
, TRUE
, TRUE
);
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
)
352 struct stab_link_includes_entry
*incl_entry
;
353 struct stab_link_includes_totals
*t
;
354 struct stab_excl_list
*ne
;
356 sum_chars
= num_chars
= 0;
358 for (incl_sym
= sym
+ STABSIZE
;
360 incl_sym
+= STABSIZE
)
364 incl_type
= incl_sym
[TYPEOFF
];
367 else if (incl_type
== (int) N_EXCL
)
369 else if (incl_type
== (int) N_EINCL
)
375 else if (incl_type
== (int) N_BINCL
)
381 str
= ((char *) stabstrbuf
383 + bfd_get_32 (abfd
, incl_sym
+ STRDXOFF
));
384 for (; *str
!= '\0'; str
++)
390 /* Skip the file number. */
392 while (ISDIGIT (*str
))
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
,
404 if (incl_entry
== NULL
)
407 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
408 if (t
->sum_chars
== sum_chars
&& t
->num_chars
== num_chars
)
411 /* Record this symbol, so that we can set the value
414 ne
= (struct stab_excl_list
*) bfd_alloc (abfd
, amt
);
417 ne
->offset
= sym
- stabbuf
;
419 ne
->type
= (int) N_BINCL
;
420 ne
->next
= secinfo
->excls
;
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
));
431 t
->sum_chars
= sum_chars
;
432 t
->num_chars
= num_chars
;
433 t
->next
= incl_entry
->totals
;
434 incl_entry
->totals
= t
;
438 bfd_size_type
*incl_pstridx
;
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
;
444 /* Mark the skipped symbols. */
447 for (incl_sym
= sym
+ STABSIZE
, incl_pstridx
= pstridx
+ 1;
449 incl_sym
+= STABSIZE
, ++incl_pstridx
)
453 incl_type
= incl_sym
[TYPEOFF
];
455 if (incl_type
== (int) N_EINCL
)
459 *incl_pstridx
= (bfd_size_type
) -1;
465 else if (incl_type
== (int) N_BINCL
)
467 else if (incl_type
== (int) N_EXCL
)
468 /* Keep existing exclusion marks. */
472 *incl_pstridx
= (bfd_size_type
) -1;
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
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
);
498 /* Calculate the `cumulative_skips' array now that stabs have been
499 deleted for this section. */
503 bfd_size_type i
, offset
;
504 bfd_size_type
*pskips
;
506 amt
= count
* sizeof (bfd_size_type
);
507 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
508 if (secinfo
->cumulative_skips
== NULL
)
511 pskips
= secinfo
->cumulative_skips
;
512 pstridx
= secinfo
->stridxs
;
515 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
518 if (*pstridx
== (bfd_size_type
) -1)
522 BFD_ASSERT (offset
!= 0);
530 if (stabstrbuf
!= NULL
)
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.
543 _bfd_discard_section_stabs (abfd
, stabsec
, psecinfo
,
544 reloc_symbol_deleted_p
, cookie
)
548 bfd_boolean (*reloc_symbol_deleted_p
) PARAMS ((bfd_vma
, PTR
));
551 bfd_size_type count
, amt
;
552 struct stab_section_info
*secinfo
;
553 bfd_byte
*stabbuf
= NULL
;
554 bfd_byte
*sym
, *symend
;
556 bfd_size_type
*pstridx
;
559 if (stabsec
->_raw_size
== 0)
561 /* This file does not contain stabs debugging information. */
565 if (stabsec
->_raw_size
% STABSIZE
!= 0)
567 /* Something is wrong with the format of these stab symbols.
568 Don't try to optimize them. */
572 if ((stabsec
->output_section
!= NULL
573 && bfd_is_abs_section (stabsec
->output_section
)))
575 /* At least one of the sections is being discarded from the
576 link, so we should just ignore them. */
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
)
586 count
= stabsec
->_raw_size
/ STABSIZE
;
587 secinfo
= (struct stab_section_info
*) psecinfo
;
589 /* Read the stabs information from abfd. */
591 stabbuf
= (bfd_byte
*) bfd_malloc (stabsec
->_raw_size
);
595 if (! bfd_get_section_contents (abfd
, stabsec
, stabbuf
, (bfd_vma
) 0,
599 /* Look through the stabs symbols and discard any information for
600 discarded functions. */
605 symend
= stabbuf
+ stabsec
->_raw_size
;
606 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
608 sym
+= STABSIZE
, ++pstridx
)
612 if (*pstridx
== (bfd_size_type
) -1)
614 /* This stab was deleted in a previous pass. */
620 if (type
== (int) N_FUN
)
622 int strx
= bfd_get_32 (abfd
, sym
+ STRDXOFF
);
635 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
644 else if (deleting
== -1)
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
))
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. */
662 /* Shrink the stabsec as needed. */
663 stabsec
->_cooked_size
-= skip
* STABSIZE
;
664 if (stabsec
->_cooked_size
== 0)
665 stabsec
->flags
|= SEC_EXCLUDE
;
667 /* Recalculate the `cumulative_skips' array now that stabs have been
668 deleted for this section. */
672 bfd_size_type i
, offset
;
673 bfd_size_type
*pskips
;
675 if (secinfo
->cumulative_skips
== NULL
)
677 amt
= count
* sizeof (bfd_size_type
);
678 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
679 if (secinfo
->cumulative_skips
== NULL
)
683 pskips
= secinfo
->cumulative_skips
;
684 pstridx
= secinfo
->stridxs
;
687 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
690 if (*pstridx
== (bfd_size_type
) -1)
694 BFD_ASSERT (offset
!= 0);
705 /* Write out the stab section. This is called with the relocated
709 _bfd_write_section_stabs (output_bfd
, psinfo
, stabsec
, psecinfo
, contents
)
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
;
722 sinfo
= (struct stab_info
*) *psinfo
;
723 secinfo
= (struct stab_section_info
*) *psecinfo
;
726 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
728 (file_ptr
) stabsec
->output_offset
,
731 /* Handle each N_BINCL entry. */
732 for (e
= secinfo
->excls
; e
!= NULL
; e
= e
->next
)
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
;
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. */
745 symend
= contents
+ stabsec
->_raw_size
;
746 for (sym
= contents
, pstridx
= secinfo
->stridxs
;
748 sym
+= STABSIZE
, ++pstridx
)
750 if (*pstridx
!= (bfd_size_type
) -1)
753 memcpy (tosym
, sym
, STABSIZE
);
754 bfd_put_32 (output_bfd
, *pstridx
, tosym
+ STRDXOFF
);
756 if (sym
[TYPEOFF
] == 0)
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
),
765 bfd_put_16 (output_bfd
,
766 stabsec
->output_section
->_raw_size
/ STABSIZE
- 1,
774 BFD_ASSERT ((bfd_size_type
) (tosym
- contents
) == stabsec
->_cooked_size
);
776 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
777 contents
, (file_ptr
) stabsec
->output_offset
,
778 stabsec
->_cooked_size
);
781 /* Write out the .stabstr section. */
784 _bfd_write_stab_strings (output_bfd
, psinfo
)
788 struct stab_info
*sinfo
;
790 sinfo
= (struct stab_info
*) *psinfo
;
795 if (bfd_is_abs_section (sinfo
->stabstr
->output_section
))
797 /* The section was discarded from the link. */
801 BFD_ASSERT ((sinfo
->stabstr
->output_offset
802 + _bfd_stringtab_size (sinfo
->strings
))
803 <= sinfo
->stabstr
->output_section
->_raw_size
);
805 if (bfd_seek (output_bfd
,
806 (file_ptr
) (sinfo
->stabstr
->output_section
->filepos
807 + sinfo
->stabstr
->output_offset
),
811 if (! _bfd_stringtab_emit (output_bfd
, sinfo
->strings
))
814 /* We no longer need the stabs information. */
815 _bfd_stringtab_free (sinfo
->strings
);
816 bfd_hash_table_free (&sinfo
->includes
.root
);
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. */
826 _bfd_stab_section_offset (output_bfd
, psinfo
, stabsec
, psecinfo
, offset
)
827 bfd
*output_bfd ATTRIBUTE_UNUSED
;
828 PTR
*psinfo ATTRIBUTE_UNUSED
;
833 struct stab_section_info
*secinfo
;
835 secinfo
= (struct stab_section_info
*) *psecinfo
;
840 if (offset
>= stabsec
->_raw_size
)
841 return offset
- (stabsec
->_cooked_size
- stabsec
->_raw_size
);
843 if (secinfo
->cumulative_skips
)
847 i
= offset
/ STABSIZE
;
849 if (secinfo
->stridxs
[i
] == (bfd_size_type
) -1)
852 return offset
- secinfo
->cumulative_skips
[i
];