1 /* Stabs in sections linking support.
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
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"
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 hash table used for header files with N_BINCL entries. */
54 struct stab_link_includes_table
56 struct bfd_hash_table root
;
59 /* A linked list of totals that we have found for a particular header
62 struct stab_link_includes_totals
64 struct stab_link_includes_totals
*next
;
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 /* Look up an entry in an the header file hash table. */
79 #define stab_link_includes_lookup(table, string, create, copy) \
80 ((struct stab_link_includes_entry *) \
81 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
83 /* This structure is used to hold a list of N_BINCL symbols, some of
84 which might be converted into N_EXCL symbols. */
88 /* The next symbol to convert. */
89 struct stab_excl_list
*next
;
90 /* The offset to this symbol in the section contents. */
92 /* The value to use for the symbol. */
94 /* The type of this symbol (N_BINCL or N_EXCL). */
98 /* This structure is stored with each .stab section. */
100 struct stab_section_info
102 /* This is a linked list of N_BINCL symbols which should be
103 converted into N_EXCL symbols. */
104 struct stab_excl_list
*excls
;
106 /* This is used to map input stab offsets within their sections
107 to output stab offsets, to take into account stabs that have
108 been deleted. If it is NULL, the output offsets are the same
109 as the input offsets, because no stabs have been deleted from
110 this section. Otherwise the i'th entry is the number of
111 bytes of stabs that have been deleted prior to the i'th
113 bfd_size_type
*cumulative_skips
;
115 /* This is an array of string indices. For each stab symbol, we
116 store the string index here. If a stab symbol should not be
117 included in the final output, the string index is -1. */
118 bfd_size_type stridxs
[1];
121 /* This structure is used to keep track of stabs in sections
122 information while linking. */
126 /* A hash table used to hold stabs strings. */
127 struct bfd_strtab_hash
*strings
;
128 /* The header file hash table. */
129 struct stab_link_includes_table includes
;
130 /* The first .stabstr section. */
134 static struct bfd_hash_entry
*stab_link_includes_newfunc
135 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
137 /* The function to create a new entry in the header file hash table. */
139 static struct bfd_hash_entry
*
140 stab_link_includes_newfunc (entry
, table
, string
)
141 struct bfd_hash_entry
*entry
;
142 struct bfd_hash_table
*table
;
145 struct stab_link_includes_entry
*ret
=
146 (struct stab_link_includes_entry
*) entry
;
148 /* Allocate the structure if it has not already been allocated by a
150 if (ret
== (struct stab_link_includes_entry
*) NULL
)
151 ret
= ((struct stab_link_includes_entry
*)
152 bfd_hash_allocate (table
,
153 sizeof (struct stab_link_includes_entry
)));
154 if (ret
== (struct stab_link_includes_entry
*) NULL
)
155 return (struct bfd_hash_entry
*) ret
;
157 /* Call the allocation method of the superclass. */
158 ret
= ((struct stab_link_includes_entry
*)
159 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
162 /* Set local fields. */
166 return (struct bfd_hash_entry
*) ret
;
169 /* This function is called for each input file from the add_symbols
170 pass of the linker. */
173 _bfd_link_section_stabs (abfd
, psinfo
, stabsec
, stabstrsec
, psecinfo
)
177 asection
*stabstrsec
;
181 struct stab_info
*sinfo
;
182 bfd_size_type count
, amt
;
183 struct stab_section_info
*secinfo
;
184 bfd_byte
*stabbuf
= NULL
;
185 bfd_byte
*stabstrbuf
= NULL
;
186 bfd_byte
*sym
, *symend
;
187 bfd_size_type stroff
, next_stroff
, skip
;
188 bfd_size_type
*pstridx
;
190 if (stabsec
->_raw_size
== 0
191 || stabstrsec
->_raw_size
== 0)
193 /* This file does not contain stabs debugging information. */
197 if (stabsec
->_raw_size
% STABSIZE
!= 0)
199 /* Something is wrong with the format of these stab symbols.
200 Don't try to optimize them. */
204 if ((stabstrsec
->flags
& SEC_RELOC
) != 0)
206 /* We shouldn't see relocations in the strings, and we aren't
207 prepared to handle them. */
211 if ((stabsec
->output_section
!= NULL
212 && bfd_is_abs_section (stabsec
->output_section
))
213 || (stabstrsec
->output_section
!= NULL
214 && bfd_is_abs_section (stabstrsec
->output_section
)))
216 /* At least one of the sections is being discarded from the
217 link, so we should just ignore them. */
225 /* Initialize the stabs information we need to keep track of. */
227 amt
= sizeof (struct stab_info
);
228 *psinfo
= (PTR
) bfd_alloc (abfd
, amt
);
231 sinfo
= (struct stab_info
*) *psinfo
;
232 sinfo
->strings
= _bfd_stringtab_init ();
233 if (sinfo
->strings
== NULL
)
235 /* Make sure the first byte is zero. */
236 (void) _bfd_stringtab_add (sinfo
->strings
, "", true, true);
237 if (! bfd_hash_table_init_n (&sinfo
->includes
.root
,
238 stab_link_includes_newfunc
,
241 sinfo
->stabstr
= bfd_make_section_anyway (abfd
, ".stabstr");
242 sinfo
->stabstr
->flags
|= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
245 sinfo
= (struct stab_info
*) *psinfo
;
247 /* Initialize the information we are going to store for this .stab
250 count
= stabsec
->_raw_size
/ STABSIZE
;
252 amt
= sizeof (struct stab_section_info
);
253 amt
+= (count
- 1) * sizeof (bfd_size_type
);
254 *psecinfo
= bfd_alloc (abfd
, amt
);
255 if (*psecinfo
== NULL
)
258 secinfo
= (struct stab_section_info
*) *psecinfo
;
259 secinfo
->excls
= NULL
;
260 secinfo
->cumulative_skips
= NULL
;
261 memset (secinfo
->stridxs
, 0, (size_t) count
* sizeof (bfd_size_type
));
263 /* Read the stabs information from abfd. */
265 stabbuf
= (bfd_byte
*) bfd_malloc (stabsec
->_raw_size
);
266 stabstrbuf
= (bfd_byte
*) bfd_malloc (stabstrsec
->_raw_size
);
267 if (stabbuf
== NULL
|| stabstrbuf
== NULL
)
270 if (! bfd_get_section_contents (abfd
, stabsec
, stabbuf
, (bfd_vma
) 0,
272 || ! bfd_get_section_contents (abfd
, stabstrsec
, stabstrbuf
, (bfd_vma
) 0,
273 stabstrsec
->_raw_size
))
276 /* Look through the stabs symbols, work out the new string indices,
277 and identify N_BINCL symbols which can be eliminated. */
283 symend
= stabbuf
+ stabsec
->_raw_size
;
284 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
286 sym
+= STABSIZE
, ++pstridx
)
293 /* This symbol has already been handled by an N_BINCL pass. */
301 /* Special type 0 stabs indicate the offset to the next
302 string table. We only copy the very first one. */
303 stroff
= next_stroff
;
304 next_stroff
+= bfd_get_32 (abfd
, sym
+ 8);
307 *pstridx
= (bfd_size_type
) -1;
314 /* Store the string in the hash table, and record the index. */
315 string
= ((char *) stabstrbuf
317 + bfd_get_32 (abfd
, sym
+ STRDXOFF
));
318 *pstridx
= _bfd_stringtab_add (sinfo
->strings
, string
, true, true);
320 /* An N_BINCL symbol indicates the start of the stabs entries
321 for a header file. We need to scan ahead to the next N_EINCL
322 symbol, ignoring nesting, adding up all the characters in the
323 symbol names, not including the file numbers in types (the
324 first number after an open parenthesis). */
330 struct stab_link_includes_entry
*incl_entry
;
331 struct stab_link_includes_totals
*t
;
332 struct stab_excl_list
*ne
;
336 for (incl_sym
= sym
+ STABSIZE
;
338 incl_sym
+= STABSIZE
)
342 incl_type
= incl_sym
[TYPEOFF
];
345 else if (incl_type
== N_EINCL
)
351 else if (incl_type
== N_BINCL
)
357 str
= ((char *) stabstrbuf
359 + bfd_get_32 (abfd
, incl_sym
+ STRDXOFF
));
360 for (; *str
!= '\0'; str
++)
365 /* Skip the file number. */
367 while (isdigit ((unsigned char) *str
))
375 /* If we have already included a header file with the same
376 value, then replaced this one with an N_EXCL symbol. */
377 incl_entry
= stab_link_includes_lookup (&sinfo
->includes
, string
,
379 if (incl_entry
== NULL
)
382 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
386 /* Record this symbol, so that we can set the value
389 ne
= (struct stab_excl_list
*) bfd_alloc (abfd
, amt
);
392 ne
->offset
= sym
- stabbuf
;
395 ne
->next
= secinfo
->excls
;
400 /* This is the first time we have seen this header file
401 with this set of stabs strings. */
402 t
= ((struct stab_link_includes_totals
*)
403 bfd_hash_allocate (&sinfo
->includes
.root
, sizeof *t
));
407 t
->next
= incl_entry
->totals
;
408 incl_entry
->totals
= t
;
412 bfd_size_type
*incl_pstridx
;
414 /* We have seen this header file before. Tell the final
415 pass to change the type to N_EXCL. */
418 /* Mark the skipped symbols. */
421 for (incl_sym
= sym
+ STABSIZE
, incl_pstridx
= pstridx
+ 1;
423 incl_sym
+= STABSIZE
, ++incl_pstridx
)
427 incl_type
= incl_sym
[TYPEOFF
];
429 if (incl_type
== N_EINCL
)
433 *incl_pstridx
= (bfd_size_type
) -1;
439 else if (incl_type
== N_BINCL
)
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
->_cooked_size
= (count
- skip
) * STABSIZE
;
464 if (stabsec
->_cooked_size
== 0)
465 stabsec
->flags
|= SEC_EXCLUDE
;
466 stabstrsec
->flags
|= SEC_EXCLUDE
;
467 sinfo
->stabstr
->_cooked_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);
501 if (stabstrbuf
!= NULL
)
506 /* Write out the stab section. This is called with the relocated
510 _bfd_write_section_stabs (output_bfd
, psinfo
, stabsec
, psecinfo
, contents
)
517 struct stab_info
*sinfo
;
518 struct stab_section_info
*secinfo
;
519 struct stab_excl_list
*e
;
520 bfd_byte
*sym
, *tosym
, *symend
;
521 bfd_size_type
*pstridx
;
523 sinfo
= (struct stab_info
*) *psinfo
;
524 secinfo
= (struct stab_section_info
*) *psecinfo
;
527 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
529 (file_ptr
) stabsec
->output_offset
,
532 /* Handle each N_BINCL entry. */
533 for (e
= secinfo
->excls
; e
!= NULL
; e
= e
->next
)
537 BFD_ASSERT (e
->offset
< stabsec
->_raw_size
);
538 excl_sym
= contents
+ e
->offset
;
539 bfd_put_32 (output_bfd
, e
->val
, excl_sym
+ VALOFF
);
540 excl_sym
[TYPEOFF
] = e
->type
;
543 /* Copy over all the stabs symbols, omitting the ones we don't want,
544 and correcting the string indices for those we do want. */
546 symend
= contents
+ stabsec
->_raw_size
;
547 for (sym
= contents
, pstridx
= secinfo
->stridxs
;
549 sym
+= STABSIZE
, ++pstridx
)
551 if (*pstridx
!= (bfd_size_type
) -1)
554 memcpy (tosym
, sym
, STABSIZE
);
555 bfd_put_32 (output_bfd
, *pstridx
, tosym
+ STRDXOFF
);
557 if (sym
[TYPEOFF
] == 0)
559 /* This is the header symbol for the stabs section. We
560 don't really need one, since we have merged all the
561 input stabs sections into one, but we generate one
562 for the benefit of readers which expect to see one. */
563 BFD_ASSERT (sym
== contents
);
564 bfd_put_32 (output_bfd
, _bfd_stringtab_size (sinfo
->strings
),
566 bfd_put_16 (output_bfd
,
567 stabsec
->output_section
->_raw_size
/ STABSIZE
- 1,
575 BFD_ASSERT ((bfd_size_type
) (tosym
- contents
) == stabsec
->_cooked_size
);
577 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
578 contents
, (file_ptr
) stabsec
->output_offset
,
579 stabsec
->_cooked_size
);
582 /* Write out the .stabstr section. */
585 _bfd_write_stab_strings (output_bfd
, psinfo
)
589 struct stab_info
*sinfo
;
591 sinfo
= (struct stab_info
*) *psinfo
;
596 if (bfd_is_abs_section (sinfo
->stabstr
->output_section
))
598 /* The section was discarded from the link. */
602 BFD_ASSERT ((sinfo
->stabstr
->output_offset
603 + _bfd_stringtab_size (sinfo
->strings
))
604 <= sinfo
->stabstr
->output_section
->_raw_size
);
606 if (bfd_seek (output_bfd
,
607 (file_ptr
) (sinfo
->stabstr
->output_section
->filepos
608 + sinfo
->stabstr
->output_offset
),
612 if (! _bfd_stringtab_emit (output_bfd
, sinfo
->strings
))
615 /* We no longer need the stabs information. */
616 _bfd_stringtab_free (sinfo
->strings
);
617 bfd_hash_table_free (&sinfo
->includes
.root
);
622 /* Adjust an address in the .stab section. Given OFFSET within
623 STABSEC, this returns the new offset in the adjusted stab section,
624 or -1 if the address refers to a stab which has been removed. */
627 _bfd_stab_section_offset (output_bfd
, psinfo
, stabsec
, psecinfo
, offset
)
628 bfd
*output_bfd ATTRIBUTE_UNUSED
;
629 PTR
*psinfo ATTRIBUTE_UNUSED
;
634 struct stab_section_info
*secinfo
;
636 secinfo
= (struct stab_section_info
*) *psecinfo
;
641 if (offset
>= stabsec
->_raw_size
)
642 return offset
- (stabsec
->_cooked_size
- stabsec
->_raw_size
);
644 if (secinfo
->cumulative_skips
)
648 i
= offset
/ STABSIZE
;
650 if (secinfo
->stridxs
[i
] == (bfd_size_type
) -1)
653 return offset
- secinfo
->cumulative_skips
[i
];