1 /* Stabs in sections linking support.
2 Copyright (C) 1996-2023 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];
116 /* The function to create a new entry in the header file hash table. */
118 static struct bfd_hash_entry
*
119 stab_link_includes_newfunc (struct bfd_hash_entry
*entry
,
120 struct bfd_hash_table
*table
,
123 struct stab_link_includes_entry
*ret
=
124 (struct stab_link_includes_entry
*) entry
;
126 /* Allocate the structure if it has not already been allocated by a
129 ret
= (struct stab_link_includes_entry
*)
130 bfd_hash_allocate (table
, sizeof (struct stab_link_includes_entry
));
134 /* Call the allocation method of the superclass. */
135 ret
= ((struct stab_link_includes_entry
*)
136 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
138 /* Set local fields. */
141 return (struct bfd_hash_entry
*) ret
;
144 /* This function is called for each input file from the add_symbols
145 pass of the linker. */
148 _bfd_link_section_stabs (bfd
*abfd
,
149 struct stab_info
*sinfo
,
151 asection
*stabstrsec
,
153 bfd_size_type
*pstring_offset
)
156 bfd_size_type count
, amt
;
157 struct stab_section_info
*secinfo
;
158 bfd_byte
*stabbuf
= NULL
;
159 bfd_byte
*stabstrbuf
= NULL
;
160 bfd_byte
*sym
, *symend
;
161 bfd_size_type stroff
, next_stroff
, skip
;
162 bfd_size_type
*pstridx
;
164 if (stabsec
->size
== 0
165 || stabstrsec
->size
== 0
166 || (stabsec
->flags
& SEC_HAS_CONTENTS
) == 0
167 || (stabstrsec
->flags
& SEC_HAS_CONTENTS
) == 0)
168 /* This file does not contain stabs debugging information. */
171 if (stabsec
->size
% STABSIZE
!= 0)
172 /* Something is wrong with the format of these stab symbols.
173 Don't try to optimize them. */
176 if ((stabstrsec
->flags
& SEC_RELOC
) != 0)
177 /* We shouldn't see relocations in the strings, and we aren't
178 prepared to handle them. */
181 if (bfd_is_abs_section (stabsec
->output_section
)
182 || bfd_is_abs_section (stabstrsec
->output_section
))
183 /* At least one of the sections is being discarded from the
184 link, so we should just ignore them. */
189 if (sinfo
->stabstr
== NULL
)
193 /* Initialize the stabs information we need to keep track of. */
195 sinfo
->strings
= _bfd_stringtab_init ();
196 if (sinfo
->strings
== NULL
)
198 /* Make sure the first byte is zero. */
199 (void) _bfd_stringtab_add (sinfo
->strings
, "", true, true);
200 if (! bfd_hash_table_init (&sinfo
->includes
,
201 stab_link_includes_newfunc
,
202 sizeof (struct stab_link_includes_entry
)))
204 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
205 | SEC_LINKER_CREATED
);
206 sinfo
->stabstr
= bfd_make_section_anyway_with_flags (abfd
, ".stabstr",
208 if (sinfo
->stabstr
== NULL
)
212 /* Initialize the information we are going to store for this .stab
214 count
= stabsec
->size
/ STABSIZE
;
216 amt
= sizeof (struct stab_section_info
);
217 amt
+= (count
- 1) * sizeof (bfd_size_type
);
218 *psecinfo
= bfd_alloc (abfd
, amt
);
219 if (*psecinfo
== NULL
)
222 secinfo
= (struct stab_section_info
*) *psecinfo
;
223 secinfo
->excls
= NULL
;
224 stabsec
->rawsize
= stabsec
->size
;
225 secinfo
->cumulative_skips
= NULL
;
226 memset (secinfo
->stridxs
, 0, (size_t) count
* sizeof (bfd_size_type
));
228 /* Read the stabs information from abfd. */
229 if (!bfd_malloc_and_get_section (abfd
, stabsec
, &stabbuf
)
230 || !bfd_malloc_and_get_section (abfd
, stabstrsec
, &stabstrbuf
))
233 /* Look through the stabs symbols, work out the new string indices,
234 and identify N_BINCL symbols which can be eliminated. */
236 /* The stabs sections can be split when
237 -split-by-reloc/-split-by-file is used. We must keep track of
238 each stab section's place in the single concatenated string
240 next_stroff
= pstring_offset
? *pstring_offset
: 0;
243 symend
= stabbuf
+ stabsec
->size
;
244 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
246 sym
+= STABSIZE
, ++pstridx
)
248 bfd_size_type symstroff
;
253 /* This symbol has already been handled by an N_BINCL pass. */
260 /* Special type 0 stabs indicate the offset to the next
261 string table. We only copy the very first one. */
262 stroff
= next_stroff
;
263 next_stroff
+= bfd_get_32 (abfd
, sym
+ 8);
265 *pstring_offset
= next_stroff
;
268 *pstridx
= (bfd_size_type
) -1;
275 /* Store the string in the hash table, and record the index. */
276 symstroff
= stroff
+ bfd_get_32 (abfd
, sym
+ STRDXOFF
);
277 if (symstroff
>= stabstrsec
->size
)
280 /* xgettext:c-format */
281 (_("%pB(%pA+%#lx): stabs entry has invalid string index"),
282 abfd
, stabsec
, (long) (sym
- stabbuf
));
283 bfd_set_error (bfd_error_bad_value
);
286 string
= (char *) stabstrbuf
+ symstroff
;
287 *pstridx
= _bfd_stringtab_add (sinfo
->strings
, string
, true, true);
289 /* An N_BINCL symbol indicates the start of the stabs entries
290 for a header file. We need to scan ahead to the next N_EINCL
291 symbol, ignoring nesting, adding up all the characters in the
292 symbol names, not including the file numbers in types (the
293 first number after an open parenthesis). */
294 if (type
== (int) N_BINCL
)
303 struct stab_link_includes_entry
* incl_entry
;
304 struct stab_link_includes_totals
* t
;
305 struct stab_excl_list
* ne
;
307 symb
= symb_rover
= NULL
;
308 sum_chars
= num_chars
= 0;
311 for (incl_sym
= sym
+ STABSIZE
;
313 incl_sym
+= STABSIZE
)
317 incl_type
= incl_sym
[TYPEOFF
];
320 else if (incl_type
== (int) N_EXCL
)
322 else if (incl_type
== (int) N_EINCL
)
328 else if (incl_type
== (int) N_BINCL
)
334 str
= ((char *) stabstrbuf
336 + bfd_get_32 (abfd
, incl_sym
+ STRDXOFF
));
337 for (; *str
!= '\0'; str
++)
339 if (num_chars
>= buf_len
)
341 buf_len
+= 32 * 1024;
342 symb
= (char *) bfd_realloc_or_free (symb
, buf_len
);
345 symb_rover
= symb
+ num_chars
;
347 * symb_rover
++ = * str
;
352 /* Skip the file number. */
354 while (ISDIGIT (*str
))
362 BFD_ASSERT (num_chars
== (bfd_vma
) (symb_rover
- symb
));
364 /* If we have already included a header file with the same
365 value, then replaced this one with an N_EXCL symbol. */
366 incl_entry
= (struct stab_link_includes_entry
* )
367 bfd_hash_lookup (&sinfo
->includes
, string
, true, true);
368 if (incl_entry
== NULL
)
371 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
372 if (t
->sum_chars
== sum_chars
373 && t
->num_chars
== num_chars
374 && memcmp (t
->symb
, symb
, num_chars
) == 0)
377 /* Record this symbol, so that we can set the value
380 ne
= (struct stab_excl_list
*) bfd_alloc (abfd
, amt
);
383 ne
->offset
= sym
- stabbuf
;
385 ne
->type
= (int) N_BINCL
;
386 ne
->next
= secinfo
->excls
;
391 /* This is the first time we have seen this header file
392 with this set of stabs strings. */
393 t
= (struct stab_link_includes_totals
*)
394 bfd_hash_allocate (&sinfo
->includes
, sizeof *t
);
397 t
->sum_chars
= sum_chars
;
398 t
->num_chars
= num_chars
;
399 /* Trim data down. */
400 t
->symb
= symb
= (char *) bfd_realloc_or_free (symb
, num_chars
);
401 t
->next
= incl_entry
->totals
;
402 incl_entry
->totals
= t
;
406 bfd_size_type
*incl_pstridx
;
408 /* We have seen this header file before. Tell the final
409 pass to change the type to N_EXCL. */
410 ne
->type
= (int) N_EXCL
;
412 /* Free off superfluous symbols. */
415 /* Mark the skipped symbols. */
418 for (incl_sym
= sym
+ STABSIZE
, incl_pstridx
= pstridx
+ 1;
420 incl_sym
+= STABSIZE
, ++incl_pstridx
)
424 incl_type
= incl_sym
[TYPEOFF
];
426 if (incl_type
== (int) N_EINCL
)
430 *incl_pstridx
= (bfd_size_type
) -1;
436 else if (incl_type
== (int) N_BINCL
)
438 else if (incl_type
== (int) N_EXCL
)
439 /* Keep existing exclusion marks. */
443 *incl_pstridx
= (bfd_size_type
) -1;
456 /* We need to set the section sizes such that the linker will
457 compute the output section sizes correctly. We set the .stab
458 size to not include the entries we don't want. We set
459 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
460 from the link. We record the size of the strtab in the first
461 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
463 stabsec
->size
= (count
- skip
) * STABSIZE
;
464 if (stabsec
->size
== 0)
465 stabsec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
466 stabstrsec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
467 sinfo
->stabstr
->size
= _bfd_stringtab_size (sinfo
->strings
);
469 /* Calculate the `cumulative_skips' array now that stabs have been
470 deleted for this section. */
474 bfd_size_type i
, offset
;
475 bfd_size_type
*pskips
;
477 amt
= count
* sizeof (bfd_size_type
);
478 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
479 if (secinfo
->cumulative_skips
== NULL
)
482 pskips
= secinfo
->cumulative_skips
;
483 pstridx
= secinfo
->stridxs
;
486 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
489 if (*pstridx
== (bfd_size_type
) -1)
493 BFD_ASSERT (offset
!= 0);
504 /* This function is called for each input file before the stab
505 section is relocated. It discards stab entries for discarded
506 functions and variables. The function returns TRUE iff
507 any entries have been deleted.
511 _bfd_discard_section_stabs (bfd
*abfd
,
514 bool (*reloc_symbol_deleted_p
) (bfd_vma
, void *),
517 bfd_size_type count
, amt
;
518 struct stab_section_info
*secinfo
;
519 bfd_byte
*stabbuf
= NULL
;
520 bfd_byte
*sym
, *symend
;
522 bfd_size_type
*pstridx
;
525 if (stabsec
->size
== 0 || (stabsec
->flags
& SEC_HAS_CONTENTS
) == 0)
526 /* This file does not contain stabs debugging information. */
529 if (stabsec
->size
% STABSIZE
!= 0)
530 /* Something is wrong with the format of these stab symbols.
531 Don't try to optimize them. */
534 if ((stabsec
->output_section
!= NULL
535 && bfd_is_abs_section (stabsec
->output_section
)))
536 /* At least one of the sections is being discarded from the
537 link, so we should just ignore them. */
540 /* We should have initialized our data in _bfd_link_section_stabs.
541 If there was some bizarre error reading the string sections, though,
542 we might not have. Bail rather than asserting. */
543 if (psecinfo
== NULL
)
546 count
= stabsec
->rawsize
/ STABSIZE
;
547 secinfo
= (struct stab_section_info
*) psecinfo
;
549 /* Read the stabs information from abfd. */
550 if (!bfd_malloc_and_get_section (abfd
, stabsec
, &stabbuf
))
553 /* Look through the stabs symbols and discard any information for
554 discarded functions. */
558 symend
= stabbuf
+ stabsec
->rawsize
;
559 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
561 sym
+= STABSIZE
, ++pstridx
)
565 if (*pstridx
== (bfd_size_type
) -1)
566 /* This stab was deleted in a previous pass. */
571 if (type
== (int) N_FUN
)
573 int strx
= bfd_get_32 (abfd
, sym
+ STRDXOFF
);
586 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
595 else if (deleting
== -1)
597 /* Outside of a function. Check for deleted variables. */
598 if (type
== (int) N_STSYM
|| type
== (int) N_LCSYM
)
599 if ((*reloc_symbol_deleted_p
) (sym
+ VALOFF
- stabbuf
, cookie
))
604 /* We should also check for N_GSYM entries which reference a
605 deleted global, but those are less harmful to debuggers
606 and would require parsing the stab strings. */
613 /* Shrink the stabsec as needed. */
614 stabsec
->size
-= skip
* STABSIZE
;
615 if (stabsec
->size
== 0)
616 stabsec
->flags
|= SEC_EXCLUDE
| SEC_KEEP
;
618 /* Recalculate the `cumulative_skips' array now that stabs have been
619 deleted for this section. */
623 bfd_size_type i
, offset
;
624 bfd_size_type
*pskips
;
626 if (secinfo
->cumulative_skips
== NULL
)
628 amt
= count
* sizeof (bfd_size_type
);
629 secinfo
->cumulative_skips
= (bfd_size_type
*) bfd_alloc (abfd
, amt
);
630 if (secinfo
->cumulative_skips
== NULL
)
634 pskips
= secinfo
->cumulative_skips
;
635 pstridx
= secinfo
->stridxs
;
638 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
641 if (*pstridx
== (bfd_size_type
) -1)
645 BFD_ASSERT (offset
!= 0);
655 /* Write out the stab section. This is called with the relocated
659 _bfd_write_section_stabs (bfd
*output_bfd
,
660 struct stab_info
*sinfo
,
665 struct stab_section_info
*secinfo
;
666 struct stab_excl_list
*e
;
667 bfd_byte
*sym
, *tosym
, *symend
;
668 bfd_size_type
*pstridx
;
670 secinfo
= (struct stab_section_info
*) *psecinfo
;
673 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
674 contents
, stabsec
->output_offset
,
677 /* Handle each N_BINCL entry. */
678 for (e
= secinfo
->excls
; e
!= NULL
; e
= e
->next
)
682 BFD_ASSERT (e
->offset
< stabsec
->rawsize
);
683 excl_sym
= contents
+ e
->offset
;
684 bfd_put_32 (output_bfd
, e
->val
, excl_sym
+ VALOFF
);
685 excl_sym
[TYPEOFF
] = e
->type
;
688 /* Copy over all the stabs symbols, omitting the ones we don't want,
689 and correcting the string indices for those we do want. */
691 symend
= contents
+ stabsec
->rawsize
;
692 for (sym
= contents
, pstridx
= secinfo
->stridxs
;
694 sym
+= STABSIZE
, ++pstridx
)
696 if (*pstridx
!= (bfd_size_type
) -1)
699 memcpy (tosym
, sym
, STABSIZE
);
700 bfd_put_32 (output_bfd
, *pstridx
, tosym
+ STRDXOFF
);
702 if (sym
[TYPEOFF
] == 0)
704 /* This is the header symbol for the stabs section. We
705 don't really need one, since we have merged all the
706 input stabs sections into one, but we generate one
707 for the benefit of readers which expect to see one. */
708 BFD_ASSERT (sym
== contents
);
709 bfd_put_32 (output_bfd
, _bfd_stringtab_size (sinfo
->strings
),
711 bfd_put_16 (output_bfd
,
712 stabsec
->output_section
->size
/ STABSIZE
- 1,
720 BFD_ASSERT ((bfd_size_type
) (tosym
- contents
) == stabsec
->size
);
722 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
723 contents
, (file_ptr
) stabsec
->output_offset
,
727 /* Write out the .stabstr section. */
730 _bfd_write_stab_strings (bfd
*output_bfd
, struct stab_info
*sinfo
)
732 if (bfd_is_abs_section (sinfo
->stabstr
->output_section
))
733 /* The section was discarded from the link. */
736 BFD_ASSERT ((sinfo
->stabstr
->output_offset
737 + _bfd_stringtab_size (sinfo
->strings
))
738 <= sinfo
->stabstr
->output_section
->size
);
740 if (bfd_seek (output_bfd
,
741 (file_ptr
) (sinfo
->stabstr
->output_section
->filepos
742 + sinfo
->stabstr
->output_offset
),
746 if (! _bfd_stringtab_emit (output_bfd
, sinfo
->strings
))
749 /* We no longer need the stabs information. */
750 _bfd_stringtab_free (sinfo
->strings
);
751 bfd_hash_table_free (&sinfo
->includes
);
756 /* Adjust an address in the .stab section. Given OFFSET within
757 STABSEC, this returns the new offset in the adjusted stab section,
758 or -1 if the address refers to a stab which has been removed. */
761 _bfd_stab_section_offset (asection
*stabsec
,
765 struct stab_section_info
*secinfo
;
767 secinfo
= (struct stab_section_info
*) psecinfo
;
772 if (offset
>= stabsec
->rawsize
)
773 return offset
- stabsec
->rawsize
+ stabsec
->size
;
775 if (secinfo
->cumulative_skips
)
779 i
= offset
/ STABSIZE
;
781 if (secinfo
->stridxs
[i
] == (bfd_size_type
) -1)
784 return offset
- secinfo
->cumulative_skips
[i
];