1 /* Stabs in sections linking support.
2 Copyright (C) 1996-2024 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 /* This file contains support for linking stabs in sections, as used
29 #include "aout/stab_gnu.h"
30 #include "safe-ctype.h"
32 /* Stabs entries use a 12 byte format:
33 4 byte string table index
35 1 byte stab other field
36 2 byte stab desc field
38 FIXME: This will have to change for a 64 bit object format.
40 The stabs symbols are divided into compilation units. For the
41 first entry in each unit, the type of 0, the value is the length of
42 the string table for this unit, and the desc field is the number of
43 stabs symbols for this unit. */
52 /* A linked list of totals that we have found for a particular header
53 file. A total is a unique identifier for a particular BINCL...EINCL
54 sequence of STABs that can be used to identify duplicate sequences.
55 It consists of three fields, 'sum_chars' which is the sum of all the
56 STABS characters; 'num_chars' which is the number of these charactes
57 and 'symb' which is a buffer of all the symbols in the sequence. This
58 buffer is only checked as a last resort. */
60 struct stab_link_includes_totals
62 struct stab_link_includes_totals
*next
;
63 bfd_vma sum_chars
; /* Accumulated sum of STABS characters. */
64 bfd_vma num_chars
; /* Number of STABS characters. */
65 const char* symb
; /* The STABS characters themselves. */
68 /* An entry in the header file hash table. */
70 struct stab_link_includes_entry
72 struct bfd_hash_entry root
;
73 /* List of totals we have found for this file. */
74 struct stab_link_includes_totals
*totals
;
77 /* This structure is used to hold a list of N_BINCL symbols, some of
78 which might be converted into N_EXCL symbols. */
82 /* The next symbol to convert. */
83 struct stab_excl_list
*next
;
84 /* The offset to this symbol in the section contents. */
86 /* The value to use for the symbol. */
88 /* The type of this symbol (N_BINCL or N_EXCL). */
92 /* This structure is stored with each .stab section. */
94 struct stab_section_info
96 /* This is a linked list of N_BINCL symbols which should be
97 converted into N_EXCL symbols. */
98 struct stab_excl_list
*excls
;
100 /* This is used to map input stab offsets within their sections
101 to output stab offsets, to take into account stabs that have
102 been deleted. If it is NULL, the output offsets are the same
103 as the input offsets, because no stabs have been deleted from
104 this section. Otherwise the i'th entry is the number of
105 bytes of stabs that have been deleted prior to the i'th
107 bfd_size_type
*cumulative_skips
;
109 /* This is an array of string indices. For each stab symbol, we
110 store the string index here. If a stab symbol should not be
111 included in the final output, the string index is -1. */
112 bfd_size_type stridxs
[1];
117 .{* This structure is used to keep track of stabs in sections
118 . information while linking. *}
122 . {* A hash table used to hold stabs strings. *}
123 . struct bfd_strtab_hash *strings;
124 . {* The header file hash table. *}
125 . struct bfd_hash_table includes;
126 . {* The first .stabstr section. *}
127 . struct bfd_section *stabstr;
132 /* The function to create a new entry in the header file hash table. */
134 static struct bfd_hash_entry
*
135 stab_link_includes_newfunc (struct bfd_hash_entry
*entry
,
136 struct bfd_hash_table
*table
,
139 struct stab_link_includes_entry
*ret
=
140 (struct stab_link_includes_entry
*) entry
;
142 /* Allocate the structure if it has not already been allocated by a
145 ret
= (struct stab_link_includes_entry
*)
146 bfd_hash_allocate (table
, sizeof (struct stab_link_includes_entry
));
150 /* Call the allocation method of the superclass. */
151 ret
= ((struct stab_link_includes_entry
*)
152 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
154 /* Set local fields. */
157 return (struct bfd_hash_entry
*) ret
;
162 _bfd_link_section_stabs
165 bool _bfd_link_section_stabs
166 (bfd *, struct stab_info *, asection *, asection *, void **,
170 This function is called for each input file from the add_symbols
175 _bfd_link_section_stabs (bfd
*abfd
,
176 struct stab_info
*sinfo
,
178 asection
*stabstrsec
,
180 bfd_size_type
*pstring_offset
)
183 bfd_size_type count
, amt
;
184 struct stab_section_info
*secinfo
;
185 bfd_byte
*stabbuf
= NULL
;
186 bfd_byte
*stabstrbuf
= NULL
;
187 bfd_byte
*sym
, *symend
;
188 bfd_size_type stroff
, next_stroff
, skip
;
189 bfd_size_type
*pstridx
;
191 if (stabsec
->size
== 0
192 || stabstrsec
->size
== 0
193 || (stabsec
->flags
& SEC_HAS_CONTENTS
) == 0
194 || (stabstrsec
->flags
& SEC_HAS_CONTENTS
) == 0)
195 /* This file does not contain stabs debugging information. */
198 if (stabsec
->size
% STABSIZE
!= 0)
199 /* Something is wrong with the format of these stab symbols.
200 Don't try to optimize them. */
203 if ((stabstrsec
->flags
& SEC_RELOC
) != 0)
204 /* We shouldn't see relocations in the strings, and we aren't
205 prepared to handle them. */
208 if (bfd_is_abs_section (stabsec
->output_section
)
209 || bfd_is_abs_section (stabstrsec
->output_section
))
210 /* At least one of the sections is being discarded from the
211 link, so we should just ignore them. */
216 if (sinfo
->stabstr
== NULL
)
220 /* Initialize the stabs information we need to keep track of. */
222 sinfo
->strings
= _bfd_stringtab_init ();
223 if (sinfo
->strings
== NULL
)
225 /* Make sure the first byte is zero. */
226 (void) _bfd_stringtab_add (sinfo
->strings
, "", true, true);
227 if (! bfd_hash_table_init (&sinfo
->includes
,
228 stab_link_includes_newfunc
,
229 sizeof (struct stab_link_includes_entry
)))
231 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
232 | SEC_LINKER_CREATED
);
233 sinfo
->stabstr
= bfd_make_section_anyway_with_flags (abfd
, ".stabstr",
235 if (sinfo
->stabstr
== NULL
)
239 /* Initialize the information we are going to store for this .stab
241 count
= stabsec
->size
/ STABSIZE
;
243 amt
= sizeof (struct stab_section_info
);
244 amt
+= (count
- 1) * sizeof (bfd_size_type
);
245 *psecinfo
= bfd_alloc (abfd
, amt
);
246 if (*psecinfo
== NULL
)
249 secinfo
= (struct stab_section_info
*) *psecinfo
;
250 secinfo
->excls
= NULL
;
251 stabsec
->rawsize
= stabsec
->size
;
252 secinfo
->cumulative_skips
= NULL
;
253 memset (secinfo
->stridxs
, 0, (size_t) count
* sizeof (bfd_size_type
));
255 /* Read the stabs information from abfd. */
256 if (!bfd_malloc_and_get_section (abfd
, stabsec
, &stabbuf
)
257 || !bfd_malloc_and_get_section (abfd
, stabstrsec
, &stabstrbuf
))
260 /* Look through the stabs symbols, work out the new string indices,
261 and identify N_BINCL symbols which can be eliminated. */
263 /* The stabs sections can be split when
264 -split-by-reloc/-split-by-file is used. We must keep track of
265 each stab section's place in the single concatenated string
267 next_stroff
= pstring_offset
? *pstring_offset
: 0;
270 symend
= stabbuf
+ stabsec
->size
;
271 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
273 sym
+= STABSIZE
, ++pstridx
)
275 bfd_size_type symstroff
;
280 /* This symbol has already been handled by an N_BINCL pass. */
287 /* Special type 0 stabs indicate the offset to the next
288 string table. We only copy the very first one. */
289 stroff
= next_stroff
;
290 next_stroff
+= bfd_get_32 (abfd
, sym
+ 8);
292 *pstring_offset
= next_stroff
;
295 *pstridx
= (bfd_size_type
) -1;
302 /* Store the string in the hash table, and record the index. */
303 symstroff
= stroff
+ bfd_get_32 (abfd
, sym
+ STRDXOFF
);
304 if (symstroff
>= stabstrsec
->size
)
307 /* xgettext:c-format */
308 (_("%pB(%pA+%#lx): stabs entry has invalid string index"),
309 abfd
, stabsec
, (long) (sym
- stabbuf
));
310 bfd_set_error (bfd_error_bad_value
);
313 string
= (char *) stabstrbuf
+ symstroff
;
314 *pstridx
= _bfd_stringtab_add (sinfo
->strings
, string
, true, true);
316 /* An N_BINCL symbol indicates the start of the stabs entries
317 for a header file. We need to scan ahead to the next N_EINCL
318 symbol, ignoring nesting, adding up all the characters in the
319 symbol names, not including the file numbers in types (the
320 first number after an open parenthesis). */
321 if (type
== (int) N_BINCL
)
330 struct stab_link_includes_entry
* incl_entry
;
331 struct stab_link_includes_totals
* t
;
332 struct stab_excl_list
* ne
;
334 symb
= symb_rover
= NULL
;
335 sum_chars
= num_chars
= 0;
338 for (incl_sym
= sym
+ STABSIZE
;
340 incl_sym
+= STABSIZE
)
344 incl_type
= incl_sym
[TYPEOFF
];
347 else if (incl_type
== (int) N_EXCL
)
349 else if (incl_type
== (int) N_EINCL
)
355 else if (incl_type
== (int) N_BINCL
)
361 str
= ((char *) stabstrbuf
363 + bfd_get_32 (abfd
, incl_sym
+ STRDXOFF
));
364 for (; *str
!= '\0'; str
++)
366 if (num_chars
>= buf_len
)
368 buf_len
+= 32 * 1024;
369 symb
= (char *) bfd_realloc_or_free (symb
, buf_len
);
372 symb_rover
= symb
+ num_chars
;
374 * symb_rover
++ = * str
;
379 /* Skip the file number. */
381 while (ISDIGIT (*str
))
389 BFD_ASSERT (num_chars
== (bfd_vma
) (symb_rover
- symb
));
391 /* If we have already included a header file with the same
392 value, then replaced this one with an N_EXCL symbol. */
393 incl_entry
= (struct stab_link_includes_entry
* )
394 bfd_hash_lookup (&sinfo
->includes
, string
, true, true);
395 if (incl_entry
== NULL
)
398 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
399 if (t
->sum_chars
== sum_chars
400 && t
->num_chars
== num_chars
401 && memcmp (t
->symb
, symb
, num_chars
) == 0)
404 /* Record this symbol, so that we can set the value
407 ne
= (struct stab_excl_list
*) bfd_alloc (abfd
, amt
);
410 ne
->offset
= sym
- stabbuf
;
412 ne
->type
= (int) N_BINCL
;
413 ne
->next
= secinfo
->excls
;
418 /* This is the first time we have seen this header file
419 with this set of stabs strings. */
420 t
= (struct stab_link_includes_totals
*)
421 bfd_hash_allocate (&sinfo
->includes
, sizeof *t
);
424 t
->sum_chars
= sum_chars
;
425 t
->num_chars
= num_chars
;
426 /* Trim data down. */
427 t
->symb
= symb
= (char *) bfd_realloc_or_free (symb
, num_chars
);
428 t
->next
= incl_entry
->totals
;
429 incl_entry
->totals
= t
;
433 bfd_size_type
*incl_pstridx
;
435 /* We have seen this header file before. Tell the final
436 pass to change the type to N_EXCL. */
437 ne
->type
= (int) N_EXCL
;
439 /* Free off superfluous symbols. */
442 /* Mark the skipped symbols. */
445 for (incl_sym
= sym
+ STABSIZE
, incl_pstridx
= pstridx
+ 1;
447 incl_sym
+= STABSIZE
, ++incl_pstridx
)
451 incl_type
= incl_sym
[TYPEOFF
];
453 if (incl_type
== (int) N_EINCL
)
457 *incl_pstridx
= (bfd_size_type
) -1;
463 else if (incl_type
== (int) N_BINCL
)
465 else if (incl_type
== (int) N_EXCL
)
466 /* Keep existing exclusion marks. */
470 *incl_pstridx
= (bfd_size_type
) -1;
483 /* We need to set the section sizes such that the linker will
484 compute the output section sizes correctly. We set the .stab
485 size to not include the entries we don't want. We set
486 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
487 from the link. We record the size of the strtab in the first
488 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
490 stabsec
->size
= (count
- skip
) * STABSIZE
;
491 if (stabsec
->size
== 0)
492 stabsec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
493 stabstrsec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
494 sinfo
->stabstr
->size
= _bfd_stringtab_size (sinfo
->strings
);
496 /* Calculate the `cumulative_skips' array now that stabs have been
497 deleted for this section. */
501 bfd_size_type i
, offset
;
502 bfd_size_type
*pskips
;
504 amt
= count
* sizeof (bfd_size_type
);
505 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
506 if (secinfo
->cumulative_skips
== NULL
)
509 pskips
= secinfo
->cumulative_skips
;
510 pstridx
= secinfo
->stridxs
;
513 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
516 if (*pstridx
== (bfd_size_type
) -1)
520 BFD_ASSERT (offset
!= 0);
533 _bfd_discard_section_stabs
536 bool _bfd_discard_section_stabs
537 (bfd *, asection *, void *, bool (*) (bfd_vma, void *), void *);
540 This function is called for each input file before the stab
541 section is relocated. It discards stab entries for discarded
542 functions and variables. The function returns TRUE iff
543 any entries have been deleted.
547 _bfd_discard_section_stabs (bfd
*abfd
,
550 bool (*reloc_symbol_deleted_p
) (bfd_vma
, void *),
553 bfd_size_type count
, amt
;
554 struct stab_section_info
*secinfo
;
555 bfd_byte
*stabbuf
= NULL
;
556 bfd_byte
*sym
, *symend
;
558 bfd_size_type
*pstridx
;
561 if (stabsec
->size
== 0 || (stabsec
->flags
& SEC_HAS_CONTENTS
) == 0)
562 /* This file does not contain stabs debugging information. */
565 if (stabsec
->size
% STABSIZE
!= 0)
566 /* Something is wrong with the format of these stab symbols.
567 Don't try to optimize them. */
570 if ((stabsec
->output_section
!= NULL
571 && bfd_is_abs_section (stabsec
->output_section
)))
572 /* At least one of the sections is being discarded from the
573 link, so we should just ignore them. */
576 /* We should have initialized our data in _bfd_link_section_stabs.
577 If there was some bizarre error reading the string sections, though,
578 we might not have. Bail rather than asserting. */
579 if (psecinfo
== NULL
)
582 count
= stabsec
->rawsize
/ STABSIZE
;
583 secinfo
= (struct stab_section_info
*) psecinfo
;
585 /* Read the stabs information from abfd. */
586 if (!bfd_malloc_and_get_section (abfd
, stabsec
, &stabbuf
))
589 /* Look through the stabs symbols and discard any information for
590 discarded functions. */
594 symend
= stabbuf
+ stabsec
->rawsize
;
595 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
597 sym
+= STABSIZE
, ++pstridx
)
601 if (*pstridx
== (bfd_size_type
) -1)
602 /* This stab was deleted in a previous pass. */
607 if (type
== (int) N_FUN
)
609 int strx
= bfd_get_32 (abfd
, sym
+ STRDXOFF
);
622 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
631 else if (deleting
== -1)
633 /* Outside of a function. Check for deleted variables. */
634 if (type
== (int) N_STSYM
|| type
== (int) N_LCSYM
)
635 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
640 /* We should also check for N_GSYM entries which reference a
641 deleted global, but those are less harmful to debuggers
642 and would require parsing the stab strings. */
649 /* Shrink the stabsec as needed. */
650 stabsec
->size
-= skip
* STABSIZE
;
651 if (stabsec
->size
== 0)
652 stabsec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
654 /* Recalculate the `cumulative_skips' array now that stabs have been
655 deleted for this section. */
659 bfd_size_type i
, offset
;
660 bfd_size_type
*pskips
;
662 if (secinfo
->cumulative_skips
== NULL
)
664 amt
= count
* sizeof (bfd_size_type
);
665 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
666 if (secinfo
->cumulative_skips
== NULL
)
670 pskips
= secinfo
->cumulative_skips
;
671 pstridx
= secinfo
->stridxs
;
674 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
677 if (*pstridx
== (bfd_size_type
) -1)
681 BFD_ASSERT (offset
!= 0);
693 _bfd_write_section_stabs
696 bool _bfd_write_section_stabs
697 (bfd *, struct stab_info *, asection *, void **, bfd_byte *);
700 Write out the stab section. This is called with the relocated
705 _bfd_write_section_stabs (bfd
*output_bfd
,
706 struct stab_info
*sinfo
,
711 struct stab_section_info
*secinfo
;
712 struct stab_excl_list
*e
;
713 bfd_byte
*sym
, *tosym
, *symend
;
714 bfd_size_type
*pstridx
;
716 secinfo
= (struct stab_section_info
*) *psecinfo
;
719 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
720 contents
, stabsec
->output_offset
,
723 /* Handle each N_BINCL entry. */
724 for (e
= secinfo
->excls
; e
!= NULL
; e
= e
->next
)
728 BFD_ASSERT (e
->offset
< stabsec
->rawsize
);
729 excl_sym
= contents
+ e
->offset
;
730 bfd_put_32 (output_bfd
, e
->val
, excl_sym
+ VALOFF
);
731 excl_sym
[TYPEOFF
] = e
->type
;
734 /* Copy over all the stabs symbols, omitting the ones we don't want,
735 and correcting the string indices for those we do want. */
737 symend
= contents
+ stabsec
->rawsize
;
738 for (sym
= contents
, pstridx
= secinfo
->stridxs
;
740 sym
+= STABSIZE
, ++pstridx
)
742 if (*pstridx
!= (bfd_size_type
) -1)
745 memcpy (tosym
, sym
, STABSIZE
);
746 bfd_put_32 (output_bfd
, *pstridx
, tosym
+ STRDXOFF
);
748 if (sym
[TYPEOFF
] == 0)
750 /* This is the header symbol for the stabs section. We
751 don't really need one, since we have merged all the
752 input stabs sections into one, but we generate one
753 for the benefit of readers which expect to see one. */
754 BFD_ASSERT (sym
== contents
);
755 bfd_put_32 (output_bfd
, _bfd_stringtab_size (sinfo
->strings
),
757 bfd_put_16 (output_bfd
,
758 stabsec
->output_section
->size
/ STABSIZE
- 1,
766 BFD_ASSERT ((bfd_size_type
) (tosym
- contents
) == stabsec
->size
);
768 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
769 contents
, (file_ptr
) stabsec
->output_offset
,
775 _bfd_write_stab_strings
778 bool _bfd_write_stab_strings (bfd *, struct stab_info *);
781 Write out the .stabstr section.
785 _bfd_write_stab_strings (bfd
*output_bfd
, struct stab_info
*sinfo
)
787 if (bfd_is_abs_section (sinfo
->stabstr
->output_section
))
788 /* The section was discarded from the link. */
791 BFD_ASSERT ((sinfo
->stabstr
->output_offset
792 + _bfd_stringtab_size (sinfo
->strings
))
793 <= sinfo
->stabstr
->output_section
->size
);
795 if (bfd_seek (output_bfd
,
796 (file_ptr
) (sinfo
->stabstr
->output_section
->filepos
797 + sinfo
->stabstr
->output_offset
),
801 if (! _bfd_stringtab_emit (output_bfd
, sinfo
->strings
))
804 /* We no longer need the stabs information. */
805 _bfd_stringtab_free (sinfo
->strings
);
806 bfd_hash_table_free (&sinfo
->includes
);
813 _bfd_stab_section_offset
816 bfd_vma _bfd_stab_section_offset (asection *, void *, bfd_vma);
819 Adjust an address in the .stab section. Given OFFSET within
820 STABSEC, this returns the new offset in the adjusted stab section,
821 or -1 if the address refers to a stab which has been removed.
825 _bfd_stab_section_offset (asection
*stabsec
,
829 struct stab_section_info
*secinfo
;
831 secinfo
= (struct stab_section_info
*) psecinfo
;
836 if (offset
>= stabsec
->rawsize
)
837 return offset
- stabsec
->rawsize
+ stabsec
->size
;
839 if (secinfo
->cumulative_skips
)
843 i
= offset
/ STABSIZE
;
845 if (secinfo
->stridxs
[i
] == (bfd_size_type
) -1)
848 return offset
- secinfo
->cumulative_skips
[i
];