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 a unique identifier for a particular BINCL...EINCL
60 sequence of STABs that can be used to identify duplicate sequences.
61 It consists of three fields, 'sum_chars' which is the sum of all the
62 STABS characters; 'num_chars' which is the number of these charactes
63 and 'symb' which is a buffer of all the symbols in the sequence. This
64 buffer is only checked as a last resort. */
66 struct stab_link_includes_totals
68 struct stab_link_includes_totals
*next
;
69 bfd_vma sum_chars
; /* Accumulated sum of STABS characters. */
70 bfd_vma num_chars
; /* Number of STABS characters. */
71 const char* symb
; /* The STABS characters themselves. */
74 /* An entry in the header file hash table. */
76 struct stab_link_includes_entry
78 struct bfd_hash_entry root
;
79 /* List of totals we have found for this file. */
80 struct stab_link_includes_totals
*totals
;
83 /* Look up an entry in an the header file hash table. */
85 #define stab_link_includes_lookup(table, string, create, copy) \
86 ((struct stab_link_includes_entry *) \
87 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
89 /* This structure is used to hold a list of N_BINCL symbols, some of
90 which might be converted into N_EXCL symbols. */
94 /* The next symbol to convert. */
95 struct stab_excl_list
*next
;
96 /* The offset to this symbol in the section contents. */
98 /* The value to use for the symbol. */
100 /* The type of this symbol (N_BINCL or N_EXCL). */
104 /* This structure is stored with each .stab section. */
106 struct stab_section_info
108 /* This is a linked list of N_BINCL symbols which should be
109 converted into N_EXCL symbols. */
110 struct stab_excl_list
*excls
;
112 /* This is used to map input stab offsets within their sections
113 to output stab offsets, to take into account stabs that have
114 been deleted. If it is NULL, the output offsets are the same
115 as the input offsets, because no stabs have been deleted from
116 this section. Otherwise the i'th entry is the number of
117 bytes of stabs that have been deleted prior to the i'th
119 bfd_size_type
*cumulative_skips
;
121 /* This is an array of string indices. For each stab symbol, we
122 store the string index here. If a stab symbol should not be
123 included in the final output, the string index is -1. */
124 bfd_size_type stridxs
[1];
127 /* This structure is used to keep track of stabs in sections
128 information while linking. */
132 /* A hash table used to hold stabs strings. */
133 struct bfd_strtab_hash
*strings
;
134 /* The header file hash table. */
135 struct stab_link_includes_table includes
;
136 /* The first .stabstr section. */
140 static struct bfd_hash_entry
*stab_link_includes_newfunc
141 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
143 /* The function to create a new entry in the header file hash table. */
145 static struct bfd_hash_entry
*
146 stab_link_includes_newfunc (entry
, table
, string
)
147 struct bfd_hash_entry
*entry
;
148 struct bfd_hash_table
*table
;
151 struct stab_link_includes_entry
*ret
=
152 (struct stab_link_includes_entry
*) entry
;
154 /* Allocate the structure if it has not already been allocated by a
156 if (ret
== (struct stab_link_includes_entry
*) NULL
)
157 ret
= ((struct stab_link_includes_entry
*)
158 bfd_hash_allocate (table
,
159 sizeof (struct stab_link_includes_entry
)));
160 if (ret
== (struct stab_link_includes_entry
*) NULL
)
161 return (struct bfd_hash_entry
*) ret
;
163 /* Call the allocation method of the superclass. */
164 ret
= ((struct stab_link_includes_entry
*)
165 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
168 /* Set local fields. */
172 return (struct bfd_hash_entry
*) ret
;
175 /* This function is called for each input file from the add_symbols
176 pass of the linker. */
179 _bfd_link_section_stabs (abfd
, psinfo
, stabsec
, stabstrsec
, psecinfo
, pstring_offset
)
183 asection
*stabstrsec
;
185 bfd_size_type
*pstring_offset
;
188 struct stab_info
*sinfo
;
189 bfd_size_type count
, amt
;
190 struct stab_section_info
*secinfo
;
191 bfd_byte
*stabbuf
= NULL
;
192 bfd_byte
*stabstrbuf
= NULL
;
193 bfd_byte
*sym
, *symend
;
194 bfd_size_type stroff
, next_stroff
, skip
;
195 bfd_size_type
*pstridx
;
197 if (stabsec
->size
== 0
198 || stabstrsec
->size
== 0)
200 /* This file does not contain stabs debugging information. */
204 if (stabsec
->size
% STABSIZE
!= 0)
206 /* Something is wrong with the format of these stab symbols.
207 Don't try to optimize them. */
211 if ((stabstrsec
->flags
& SEC_RELOC
) != 0)
213 /* We shouldn't see relocations in the strings, and we aren't
214 prepared to handle them. */
218 if ((stabsec
->output_section
!= NULL
219 && bfd_is_abs_section (stabsec
->output_section
))
220 || (stabstrsec
->output_section
!= NULL
221 && bfd_is_abs_section (stabstrsec
->output_section
)))
223 /* At least one of the sections is being discarded from the
224 link, so we should just ignore them. */
232 /* Initialize the stabs information we need to keep track of. */
234 amt
= sizeof (struct stab_info
);
235 *psinfo
= (PTR
) bfd_alloc (abfd
, amt
);
238 sinfo
= (struct stab_info
*) *psinfo
;
239 sinfo
->strings
= _bfd_stringtab_init ();
240 if (sinfo
->strings
== NULL
)
242 /* Make sure the first byte is zero. */
243 (void) _bfd_stringtab_add (sinfo
->strings
, "", TRUE
, TRUE
);
244 if (! bfd_hash_table_init_n (&sinfo
->includes
.root
,
245 stab_link_includes_newfunc
,
248 sinfo
->stabstr
= bfd_make_section_anyway (abfd
, ".stabstr");
249 sinfo
->stabstr
->flags
|= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
252 sinfo
= (struct stab_info
*) *psinfo
;
254 /* Initialize the information we are going to store for this .stab
257 count
= stabsec
->size
/ STABSIZE
;
259 amt
= sizeof (struct stab_section_info
);
260 amt
+= (count
- 1) * sizeof (bfd_size_type
);
261 *psecinfo
= bfd_alloc (abfd
, amt
);
262 if (*psecinfo
== NULL
)
265 secinfo
= (struct stab_section_info
*) *psecinfo
;
266 secinfo
->excls
= NULL
;
267 stabsec
->rawsize
= stabsec
->size
;
268 secinfo
->cumulative_skips
= NULL
;
269 memset (secinfo
->stridxs
, 0, (size_t) count
* sizeof (bfd_size_type
));
271 /* Read the stabs information from abfd. */
273 if (!bfd_malloc_and_get_section (abfd
, stabsec
, &stabbuf
)
274 || !bfd_malloc_and_get_section (abfd
, stabstrsec
, &stabstrbuf
))
277 /* Look through the stabs symbols, work out the new string indices,
278 and identify N_BINCL symbols which can be eliminated. */
281 /* The stabs sections can be split when
282 -split-by-reloc/-split-by-file is used. We must keep track of
283 each stab section's place in the single concatenated string
285 next_stroff
= pstring_offset
? *pstring_offset
: 0;
288 symend
= stabbuf
+ stabsec
->size
;
289 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
291 sym
+= STABSIZE
, ++pstridx
)
293 bfd_size_type symstroff
;
299 /* This symbol has already been handled by an N_BINCL pass. */
307 /* Special type 0 stabs indicate the offset to the next
308 string table. We only copy the very first one. */
309 stroff
= next_stroff
;
310 next_stroff
+= bfd_get_32 (abfd
, sym
+ 8);
312 *pstring_offset
= next_stroff
;
315 *pstridx
= (bfd_size_type
) -1;
322 /* Store the string in the hash table, and record the index. */
323 symstroff
= stroff
+ bfd_get_32 (abfd
, sym
+ STRDXOFF
);
324 if (symstroff
>= stabstrsec
->size
)
326 (*_bfd_error_handler
)
327 (_("%s(%s+0x%lx): Stabs entry has invalid string index."),
328 bfd_archive_filename (abfd
),
329 bfd_get_section_name (abfd
, stabsec
),
330 (long) (sym
- stabbuf
));
331 bfd_set_error (bfd_error_bad_value
);
334 string
= (char *) stabstrbuf
+ symstroff
;
335 *pstridx
= _bfd_stringtab_add (sinfo
->strings
, string
, TRUE
, TRUE
);
337 /* An N_BINCL symbol indicates the start of the stabs entries
338 for a header file. We need to scan ahead to the next N_EINCL
339 symbol, ignoring nesting, adding up all the characters in the
340 symbol names, not including the file numbers in types (the
341 first number after an open parenthesis). */
342 if (type
== (int) N_BINCL
)
351 struct stab_link_includes_entry
* incl_entry
;
352 struct stab_link_includes_totals
* t
;
353 struct stab_excl_list
* ne
;
355 symb
= symb_rover
= NULL
;
356 sum_chars
= num_chars
= 0;
359 for (incl_sym
= sym
+ STABSIZE
;
361 incl_sym
+= STABSIZE
)
365 incl_type
= incl_sym
[TYPEOFF
];
368 else if (incl_type
== (int) N_EXCL
)
370 else if (incl_type
== (int) N_EINCL
)
376 else if (incl_type
== (int) N_BINCL
)
382 str
= ((char *) stabstrbuf
384 + bfd_get_32 (abfd
, incl_sym
+ STRDXOFF
));
385 for (; *str
!= '\0'; str
++)
387 if (num_chars
>= buf_len
)
389 buf_len
+= 32 * 1024;
390 symb
= bfd_realloc (symb
, buf_len
);
393 symb_rover
= symb
+ num_chars
;
395 * symb_rover
++ = * str
;
400 /* Skip the file number. */
402 while (ISDIGIT (*str
))
410 BFD_ASSERT (num_chars
== (bfd_vma
) (symb_rover
- symb
));
412 /* If we have already included a header file with the same
413 value, then replaced this one with an N_EXCL symbol. */
414 incl_entry
= stab_link_includes_lookup (&sinfo
->includes
, string
,
416 if (incl_entry
== NULL
)
419 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
420 if (t
->sum_chars
== sum_chars
421 && t
->num_chars
== num_chars
422 && memcmp (t
->symb
, symb
, num_chars
) == 0)
425 /* Record this symbol, so that we can set the value
428 ne
= (struct stab_excl_list
*) bfd_alloc (abfd
, amt
);
431 ne
->offset
= sym
- stabbuf
;
433 ne
->type
= (int) N_BINCL
;
434 ne
->next
= secinfo
->excls
;
439 /* This is the first time we have seen this header file
440 with this set of stabs strings. */
441 t
= ((struct stab_link_includes_totals
*)
442 bfd_hash_allocate (&sinfo
->includes
.root
, sizeof *t
));
445 t
->sum_chars
= sum_chars
;
446 t
->num_chars
= num_chars
;
447 t
->symb
= bfd_realloc (symb
, num_chars
); /* Trim data down. */
448 t
->next
= incl_entry
->totals
;
449 incl_entry
->totals
= t
;
453 bfd_size_type
*incl_pstridx
;
455 /* We have seen this header file before. Tell the final
456 pass to change the type to N_EXCL. */
457 ne
->type
= (int) N_EXCL
;
459 /* Free off superfluous symbols. */
462 /* Mark the skipped symbols. */
465 for (incl_sym
= sym
+ STABSIZE
, incl_pstridx
= pstridx
+ 1;
467 incl_sym
+= STABSIZE
, ++incl_pstridx
)
471 incl_type
= incl_sym
[TYPEOFF
];
473 if (incl_type
== (int) N_EINCL
)
477 *incl_pstridx
= (bfd_size_type
) -1;
483 else if (incl_type
== (int) N_BINCL
)
485 else if (incl_type
== (int) N_EXCL
)
486 /* Keep existing exclusion marks. */
490 *incl_pstridx
= (bfd_size_type
) -1;
503 /* We need to set the section sizes such that the linker will
504 compute the output section sizes correctly. We set the .stab
505 size to not include the entries we don't want. We set
506 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
507 from the link. We record the size of the strtab in the first
508 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
510 stabsec
->size
= (count
- skip
) * STABSIZE
;
511 if (stabsec
->size
== 0)
512 stabsec
->flags
|= SEC_EXCLUDE
;
513 stabstrsec
->flags
|= SEC_EXCLUDE
;
514 sinfo
->stabstr
->size
= _bfd_stringtab_size (sinfo
->strings
);
516 /* Calculate the `cumulative_skips' array now that stabs have been
517 deleted for this section. */
521 bfd_size_type i
, offset
;
522 bfd_size_type
*pskips
;
524 amt
= count
* sizeof (bfd_size_type
);
525 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
526 if (secinfo
->cumulative_skips
== NULL
)
529 pskips
= secinfo
->cumulative_skips
;
530 pstridx
= secinfo
->stridxs
;
533 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
536 if (*pstridx
== (bfd_size_type
) -1)
540 BFD_ASSERT (offset
!= 0);
548 if (stabstrbuf
!= NULL
)
553 /* This function is called for each input file before the stab
554 section is relocated. It discards stab entries for discarded
555 functions and variables. The function returns TRUE iff
556 any entries have been deleted.
560 _bfd_discard_section_stabs (abfd
, stabsec
, psecinfo
,
561 reloc_symbol_deleted_p
, cookie
)
565 bfd_boolean (*reloc_symbol_deleted_p
) PARAMS ((bfd_vma
, PTR
));
568 bfd_size_type count
, amt
;
569 struct stab_section_info
*secinfo
;
570 bfd_byte
*stabbuf
= NULL
;
571 bfd_byte
*sym
, *symend
;
573 bfd_size_type
*pstridx
;
576 if (stabsec
->size
== 0)
578 /* This file does not contain stabs debugging information. */
582 if (stabsec
->size
% STABSIZE
!= 0)
584 /* Something is wrong with the format of these stab symbols.
585 Don't try to optimize them. */
589 if ((stabsec
->output_section
!= NULL
590 && bfd_is_abs_section (stabsec
->output_section
)))
592 /* At least one of the sections is being discarded from the
593 link, so we should just ignore them. */
597 /* We should have initialized our data in _bfd_link_stab_sections.
598 If there was some bizarre error reading the string sections, though,
599 we might not have. Bail rather than asserting. */
600 if (psecinfo
== NULL
)
603 count
= stabsec
->rawsize
/ STABSIZE
;
604 secinfo
= (struct stab_section_info
*) psecinfo
;
606 /* Read the stabs information from abfd. */
608 if (!bfd_malloc_and_get_section (abfd
, stabsec
, &stabbuf
))
611 /* Look through the stabs symbols and discard any information for
612 discarded functions. */
617 symend
= stabbuf
+ stabsec
->rawsize
;
618 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
620 sym
+= STABSIZE
, ++pstridx
)
624 if (*pstridx
== (bfd_size_type
) -1)
626 /* This stab was deleted in a previous pass. */
632 if (type
== (int) N_FUN
)
634 int strx
= bfd_get_32 (abfd
, sym
+ STRDXOFF
);
647 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
656 else if (deleting
== -1)
658 /* Outside of a function. Check for deleted variables. */
659 if (type
== (int) N_STSYM
|| type
== (int) N_LCSYM
)
660 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
665 /* We should also check for N_GSYM entries which reference a
666 deleted global, but those are less harmful to debuggers
667 and would require parsing the stab strings. */
674 /* Shrink the stabsec as needed. */
675 stabsec
->size
-= skip
* STABSIZE
;
676 if (stabsec
->size
== 0)
677 stabsec
->flags
|= SEC_EXCLUDE
;
679 /* Recalculate the `cumulative_skips' array now that stabs have been
680 deleted for this section. */
684 bfd_size_type i
, offset
;
685 bfd_size_type
*pskips
;
687 if (secinfo
->cumulative_skips
== NULL
)
689 amt
= count
* sizeof (bfd_size_type
);
690 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
691 if (secinfo
->cumulative_skips
== NULL
)
695 pskips
= secinfo
->cumulative_skips
;
696 pstridx
= secinfo
->stridxs
;
699 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
702 if (*pstridx
== (bfd_size_type
) -1)
706 BFD_ASSERT (offset
!= 0);
717 /* Write out the stab section. This is called with the relocated
721 _bfd_write_section_stabs (output_bfd
, psinfo
, stabsec
, psecinfo
, contents
)
728 struct stab_info
*sinfo
;
729 struct stab_section_info
*secinfo
;
730 struct stab_excl_list
*e
;
731 bfd_byte
*sym
, *tosym
, *symend
;
732 bfd_size_type
*pstridx
;
734 sinfo
= (struct stab_info
*) *psinfo
;
735 secinfo
= (struct stab_section_info
*) *psecinfo
;
738 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
739 contents
, stabsec
->output_offset
,
742 /* Handle each N_BINCL entry. */
743 for (e
= secinfo
->excls
; e
!= NULL
; e
= e
->next
)
747 BFD_ASSERT (e
->offset
< stabsec
->rawsize
);
748 excl_sym
= contents
+ e
->offset
;
749 bfd_put_32 (output_bfd
, e
->val
, excl_sym
+ VALOFF
);
750 excl_sym
[TYPEOFF
] = e
->type
;
753 /* Copy over all the stabs symbols, omitting the ones we don't want,
754 and correcting the string indices for those we do want. */
756 symend
= contents
+ stabsec
->rawsize
;
757 for (sym
= contents
, pstridx
= secinfo
->stridxs
;
759 sym
+= STABSIZE
, ++pstridx
)
761 if (*pstridx
!= (bfd_size_type
) -1)
764 memcpy (tosym
, sym
, STABSIZE
);
765 bfd_put_32 (output_bfd
, *pstridx
, tosym
+ STRDXOFF
);
767 if (sym
[TYPEOFF
] == 0)
769 /* This is the header symbol for the stabs section. We
770 don't really need one, since we have merged all the
771 input stabs sections into one, but we generate one
772 for the benefit of readers which expect to see one. */
773 BFD_ASSERT (sym
== contents
);
774 bfd_put_32 (output_bfd
, _bfd_stringtab_size (sinfo
->strings
),
776 bfd_put_16 (output_bfd
,
777 stabsec
->output_section
->size
/ STABSIZE
- 1,
785 BFD_ASSERT ((bfd_size_type
) (tosym
- contents
) == stabsec
->size
);
787 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
788 contents
, (file_ptr
) stabsec
->output_offset
,
792 /* Write out the .stabstr section. */
795 _bfd_write_stab_strings (output_bfd
, psinfo
)
799 struct stab_info
*sinfo
;
801 sinfo
= (struct stab_info
*) *psinfo
;
806 if (bfd_is_abs_section (sinfo
->stabstr
->output_section
))
808 /* The section was discarded from the link. */
812 BFD_ASSERT ((sinfo
->stabstr
->output_offset
813 + _bfd_stringtab_size (sinfo
->strings
))
814 <= sinfo
->stabstr
->output_section
->size
);
816 if (bfd_seek (output_bfd
,
817 (file_ptr
) (sinfo
->stabstr
->output_section
->filepos
818 + sinfo
->stabstr
->output_offset
),
822 if (! _bfd_stringtab_emit (output_bfd
, sinfo
->strings
))
825 /* We no longer need the stabs information. */
826 _bfd_stringtab_free (sinfo
->strings
);
827 bfd_hash_table_free (&sinfo
->includes
.root
);
832 /* Adjust an address in the .stab section. Given OFFSET within
833 STABSEC, this returns the new offset in the adjusted stab section,
834 or -1 if the address refers to a stab which has been removed. */
837 _bfd_stab_section_offset (stabsec
, psecinfo
, offset
)
842 struct stab_section_info
*secinfo
;
844 secinfo
= (struct stab_section_info
*) psecinfo
;
849 if (offset
>= stabsec
->rawsize
)
850 return offset
- stabsec
->rawsize
+ stabsec
->size
;
852 if (secinfo
->cumulative_skips
)
856 i
= offset
/ STABSIZE
;
858 if (secinfo
->stridxs
[i
] == (bfd_size_type
) -1)
861 return offset
- secinfo
->cumulative_skips
[i
];