1 /* Stabs in sections linking support.
2 Copyright 1996, 1997, 1998, 1999 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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* This file contains support for linking stabs in sections, as used
27 #include "aout/stab_gnu.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
61 struct stab_link_includes_totals
63 struct stab_link_includes_totals
*next
;
67 /* An entry in the header file hash table. */
69 struct stab_link_includes_entry
71 struct bfd_hash_entry root
;
72 /* List of totals we have found for this file. */
73 struct stab_link_includes_totals
*totals
;
76 /* Look up an entry in an the header file hash table. */
78 #define stab_link_includes_lookup(table, string, create, copy) \
79 ((struct stab_link_includes_entry *) \
80 bfd_hash_lookup (&(table)->root, (string), (create), (copy)))
82 /* This structure is used to hold a list of N_BINCL symbols, some of
83 which might be converted into N_EXCL symbols. */
87 /* The next symbol to convert. */
88 struct stab_excl_list
*next
;
89 /* The offset to this symbol in the section contents. */
91 /* The value to use for the symbol. */
93 /* The type of this symbol (N_BINCL or N_EXCL). */
97 /* This structure is stored with each .stab section. */
99 struct stab_section_info
101 /* This is a linked list of N_BINCL symbols which should be
102 converted into N_EXCL symbols. */
103 struct stab_excl_list
*excls
;
105 /* This is used to map input stab offsets within their sections
106 to output stab offsets, to take into account stabs that have
107 been deleted. If it is NULL, the output offsets are the same
108 as the input offsets, because no stabs have been deleted from
109 this section. Otherwise the i'th entry is the number of
110 bytes of stabs that have been deleted prior to the i'th
112 bfd_size_type
*cumulative_skips
;
114 /* This is an array of string indices. For each stab symbol, we
115 store the string index here. If a stab symbol should not be
116 included in the final output, the string index is -1. */
117 bfd_size_type stridxs
[1];
120 /* This structure is used to keep track of stabs in sections
121 information while linking. */
125 /* A hash table used to hold stabs strings. */
126 struct bfd_strtab_hash
*strings
;
127 /* The header file hash table. */
128 struct stab_link_includes_table includes
;
129 /* The first .stabstr section. */
133 static struct bfd_hash_entry
*stab_link_includes_newfunc
134 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
136 /* The function to create a new entry in the header file hash table. */
138 static struct bfd_hash_entry
*
139 stab_link_includes_newfunc (entry
, table
, string
)
140 struct bfd_hash_entry
*entry
;
141 struct bfd_hash_table
*table
;
144 struct stab_link_includes_entry
*ret
=
145 (struct stab_link_includes_entry
*) entry
;
147 /* Allocate the structure if it has not already been allocated by a
149 if (ret
== (struct stab_link_includes_entry
*) NULL
)
150 ret
= ((struct stab_link_includes_entry
*)
151 bfd_hash_allocate (table
,
152 sizeof (struct stab_link_includes_entry
)));
153 if (ret
== (struct stab_link_includes_entry
*) NULL
)
154 return (struct bfd_hash_entry
*) ret
;
156 /* Call the allocation method of the superclass. */
157 ret
= ((struct stab_link_includes_entry
*)
158 bfd_hash_newfunc ((struct bfd_hash_entry
*) ret
, table
, string
));
161 /* Set local fields. */
165 return (struct bfd_hash_entry
*) ret
;
168 /* This function is called for each input file from the add_symbols
169 pass of the linker. */
172 _bfd_link_section_stabs (abfd
, psinfo
, stabsec
, stabstrsec
, psecinfo
)
176 asection
*stabstrsec
;
180 struct stab_info
*sinfo
;
182 struct stab_section_info
*secinfo
;
183 bfd_byte
*stabbuf
= NULL
;
184 bfd_byte
*stabstrbuf
= NULL
;
185 bfd_byte
*sym
, *symend
;
186 bfd_size_type stroff
, next_stroff
, skip
;
187 bfd_size_type
*pstridx
;
189 if (stabsec
->_raw_size
== 0
190 || stabstrsec
->_raw_size
== 0)
192 /* This file does not contain stabs debugging information. */
196 if (stabsec
->_raw_size
% STABSIZE
!= 0)
198 /* Something is wrong with the format of these stab symbols.
199 Don't try to optimize them. */
203 if ((stabstrsec
->flags
& SEC_RELOC
) != 0)
205 /* We shouldn't see relocations in the strings, and we aren't
206 prepared to handle them. */
210 if ((stabsec
->output_section
!= NULL
211 && bfd_is_abs_section (stabsec
->output_section
))
212 || (stabstrsec
->output_section
!= NULL
213 && bfd_is_abs_section (stabstrsec
->output_section
)))
215 /* At least one of the sections is being discarded from the
216 link, so we should just ignore them. */
224 /* Initialize the stabs information we need to keep track of. */
226 *psinfo
= (PTR
) bfd_alloc (abfd
, sizeof (struct stab_info
));
229 sinfo
= (struct stab_info
*) *psinfo
;
230 sinfo
->strings
= _bfd_stringtab_init ();
231 if (sinfo
->strings
== NULL
)
233 /* Make sure the first byte is zero. */
234 (void) _bfd_stringtab_add (sinfo
->strings
, "", true, true);
235 if (! bfd_hash_table_init_n (&sinfo
->includes
.root
,
236 stab_link_includes_newfunc
,
239 sinfo
->stabstr
= bfd_make_section_anyway (abfd
, ".stabstr");
240 sinfo
->stabstr
->flags
|= SEC_HAS_CONTENTS
| SEC_READONLY
| SEC_DEBUGGING
;
243 sinfo
= (struct stab_info
*) *psinfo
;
245 /* Initialize the information we are going to store for this .stab
248 count
= stabsec
->_raw_size
/ STABSIZE
;
250 *psecinfo
= bfd_alloc (abfd
,
251 (sizeof (struct stab_section_info
)
252 + (count
- 1) * sizeof (bfd_size_type
)));
253 if (*psecinfo
== NULL
)
256 secinfo
= (struct stab_section_info
*) *psecinfo
;
257 secinfo
->excls
= NULL
;
258 secinfo
->cumulative_skips
= NULL
;
259 memset (secinfo
->stridxs
, 0, count
* sizeof (bfd_size_type
));
261 /* Read the stabs information from abfd. */
263 stabbuf
= (bfd_byte
*) bfd_malloc (stabsec
->_raw_size
);
264 stabstrbuf
= (bfd_byte
*) bfd_malloc (stabstrsec
->_raw_size
);
265 if (stabbuf
== NULL
|| stabstrbuf
== NULL
)
268 if (! bfd_get_section_contents (abfd
, stabsec
, stabbuf
, 0,
270 || ! bfd_get_section_contents (abfd
, stabstrsec
, stabstrbuf
, 0,
271 stabstrsec
->_raw_size
))
274 /* Look through the stabs symbols, work out the new string indices,
275 and identify N_BINCL symbols which can be eliminated. */
281 symend
= stabbuf
+ stabsec
->_raw_size
;
282 for (sym
= stabbuf
, pstridx
= secinfo
->stridxs
;
284 sym
+= STABSIZE
, ++pstridx
)
291 /* This symbol has already been handled by an N_BINCL pass. */
299 /* Special type 0 stabs indicate the offset to the next
300 string table. We only copy the very first one. */
301 stroff
= next_stroff
;
302 next_stroff
+= bfd_get_32 (abfd
, sym
+ 8);
305 *pstridx
= (bfd_size_type
) -1;
312 /* Store the string in the hash table, and record the index. */
313 string
= ((char *) stabstrbuf
315 + bfd_get_32 (abfd
, sym
+ STRDXOFF
));
316 *pstridx
= _bfd_stringtab_add (sinfo
->strings
, string
, true, true);
318 /* An N_BINCL symbol indicates the start of the stabs entries
319 for a header file. We need to scan ahead to the next N_EINCL
320 symbol, ignoring nesting, adding up all the characters in the
321 symbol names, not including the file numbers in types (the
322 first number after an open parenthesis). */
328 struct stab_link_includes_entry
*incl_entry
;
329 struct stab_link_includes_totals
*t
;
330 struct stab_excl_list
*ne
;
334 for (incl_sym
= sym
+ STABSIZE
;
336 incl_sym
+= STABSIZE
)
340 incl_type
= incl_sym
[TYPEOFF
];
343 else if (incl_type
== N_EINCL
)
349 else if (incl_type
== N_BINCL
)
355 str
= ((char *) stabstrbuf
357 + bfd_get_32 (abfd
, incl_sym
+ STRDXOFF
));
358 for (; *str
!= '\0'; str
++)
363 /* Skip the file number. */
365 while (isdigit ((unsigned char) *str
))
373 /* If we have already included a header file with the same
374 value, then replaced this one with an N_EXCL symbol. */
375 incl_entry
= stab_link_includes_lookup (&sinfo
->includes
, string
,
377 if (incl_entry
== NULL
)
380 for (t
= incl_entry
->totals
; t
!= NULL
; t
= t
->next
)
384 /* Record this symbol, so that we can set the value
386 ne
= (struct stab_excl_list
*) bfd_alloc (abfd
, sizeof *ne
);
389 ne
->offset
= sym
- stabbuf
;
392 ne
->next
= secinfo
->excls
;
397 /* This is the first time we have seen this header file
398 with this set of stabs strings. */
399 t
= ((struct stab_link_includes_totals
*)
400 bfd_hash_allocate (&sinfo
->includes
.root
, sizeof *t
));
404 t
->next
= incl_entry
->totals
;
405 incl_entry
->totals
= t
;
409 bfd_size_type
*incl_pstridx
;
411 /* We have seen this header file before. Tell the final
412 pass to change the type to N_EXCL. */
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
== N_EINCL
)
430 *incl_pstridx
= (bfd_size_type
) -1;
436 else if (incl_type
== N_BINCL
)
440 *incl_pstridx
= (bfd_size_type
) -1;
453 /* We need to set the section sizes such that the linker will
454 compute the output section sizes correctly. We set the .stab
455 size to not include the entries we don't want. We set
456 SEC_EXCLUDE for the .stabstr section, so that it will be dropped
457 from the link. We record the size of the strtab in the first
458 .stabstr section we saw, and make sure we don't set SEC_EXCLUDE
460 stabsec
->_cooked_size
= (count
- skip
) * STABSIZE
;
461 if (stabsec
->_cooked_size
== 0)
462 stabsec
->flags
|= SEC_EXCLUDE
;
463 stabstrsec
->flags
|= SEC_EXCLUDE
;
464 sinfo
->stabstr
->_cooked_size
= _bfd_stringtab_size (sinfo
->strings
);
466 /* Calculate the `cumulative_skips' array now that stabs have been
467 deleted for this section. */
471 bfd_size_type i
, offset
;
472 bfd_size_type
*pskips
;
474 secinfo
->cumulative_skips
=
475 (bfd_size_type
*) bfd_alloc (abfd
, count
* sizeof (bfd_size_type
));
476 if (secinfo
->cumulative_skips
== NULL
)
479 pskips
= secinfo
->cumulative_skips
;
480 pstridx
= secinfo
->stridxs
;
483 for (i
= 0; i
< count
; i
++, pskips
++, pstridx
++)
486 if (*pstridx
== (bfd_size_type
) -1)
490 BFD_ASSERT (offset
!= 0);
498 if (stabstrbuf
!= NULL
)
503 /* Write out the stab section. This is called with the relocated
507 _bfd_write_section_stabs (output_bfd
, psinfo
, stabsec
, psecinfo
, contents
)
514 struct stab_info
*sinfo
;
515 struct stab_section_info
*secinfo
;
516 struct stab_excl_list
*e
;
517 bfd_byte
*sym
, *tosym
, *symend
;
518 bfd_size_type
*pstridx
;
520 sinfo
= (struct stab_info
*) *psinfo
;
521 secinfo
= (struct stab_section_info
*) *psecinfo
;
524 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
525 contents
, stabsec
->output_offset
,
528 /* Handle each N_BINCL entry. */
529 for (e
= secinfo
->excls
; e
!= NULL
; e
= e
->next
)
533 BFD_ASSERT (e
->offset
< stabsec
->_raw_size
);
534 excl_sym
= contents
+ e
->offset
;
535 bfd_put_32 (output_bfd
, e
->val
, excl_sym
+ VALOFF
);
536 excl_sym
[TYPEOFF
] = e
->type
;
539 /* Copy over all the stabs symbols, omitting the ones we don't want,
540 and correcting the string indices for those we do want. */
542 symend
= contents
+ stabsec
->_raw_size
;
543 for (sym
= contents
, pstridx
= secinfo
->stridxs
;
545 sym
+= STABSIZE
, ++pstridx
)
547 if (*pstridx
!= (bfd_size_type
) -1)
550 memcpy (tosym
, sym
, STABSIZE
);
551 bfd_put_32 (output_bfd
, *pstridx
, tosym
+ STRDXOFF
);
553 if (sym
[TYPEOFF
] == 0)
555 /* This is the header symbol for the stabs section. We
556 don't really need one, since we have merged all the
557 input stabs sections into one, but we generate one
558 for the benefit of readers which expect to see one. */
559 BFD_ASSERT (sym
== contents
);
560 bfd_put_32 (output_bfd
, _bfd_stringtab_size (sinfo
->strings
),
562 bfd_put_16 (output_bfd
,
563 stabsec
->output_section
->_raw_size
/ STABSIZE
- 1,
571 BFD_ASSERT ((bfd_size_type
) (tosym
- contents
) == stabsec
->_cooked_size
);
573 return bfd_set_section_contents (output_bfd
, stabsec
->output_section
,
574 contents
, stabsec
->output_offset
,
575 stabsec
->_cooked_size
);
578 /* Write out the .stabstr section. */
581 _bfd_write_stab_strings (output_bfd
, psinfo
)
585 struct stab_info
*sinfo
;
587 sinfo
= (struct stab_info
*) *psinfo
;
592 if (bfd_is_abs_section (sinfo
->stabstr
->output_section
))
594 /* The section was discarded from the link. */
598 BFD_ASSERT ((sinfo
->stabstr
->output_offset
599 + _bfd_stringtab_size (sinfo
->strings
))
600 <= sinfo
->stabstr
->output_section
->_raw_size
);
602 if (bfd_seek (output_bfd
,
603 (sinfo
->stabstr
->output_section
->filepos
604 + sinfo
->stabstr
->output_offset
),
608 if (! _bfd_stringtab_emit (output_bfd
, sinfo
->strings
))
611 /* We no longer need the stabs information. */
612 _bfd_stringtab_free (sinfo
->strings
);
613 bfd_hash_table_free (&sinfo
->includes
.root
);
618 /* Adjust an address in the .stab section. Given OFFSET within
619 STABSEC, this returns the new offset in the adjusted stab section,
620 or -1 if the address refers to a stab which has been removed. */
623 _bfd_stab_section_offset (output_bfd
, psinfo
, stabsec
, psecinfo
, offset
)
624 bfd
*output_bfd ATTRIBUTE_UNUSED
;
625 PTR
*psinfo ATTRIBUTE_UNUSED
;
630 struct stab_section_info
*secinfo
;
632 secinfo
= (struct stab_section_info
*) *psecinfo
;
637 if (offset
>= stabsec
->_raw_size
)
638 return offset
- (stabsec
->_cooked_size
- stabsec
->_raw_size
);
640 if (secinfo
->cumulative_skips
)
644 i
= offset
/ STABSIZE
;
646 if (secinfo
->stridxs
[i
] == (bfd_size_type
) -1)
649 return offset
- secinfo
->cumulative_skips
[i
];