Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Stabs in sections linking support. |
b34976b6 | 2 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 |
dc810e39 | 3 | Free Software Foundation, Inc. |
252b5132 RH |
4 | Written by Ian Lance Taylor, Cygnus Support. |
5 | ||
6 | This file is part of BFD, the Binary File Descriptor library. | |
7 | ||
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. | |
12 | ||
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. | |
17 | ||
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. */ | |
21 | ||
22 | /* This file contains support for linking stabs in sections, as used | |
23 | on COFF and ELF. */ | |
24 | ||
25 | #include "bfd.h" | |
26 | #include "sysdep.h" | |
27 | #include "libbfd.h" | |
28 | #include "aout/stab_gnu.h" | |
3882b010 | 29 | #include "safe-ctype.h" |
252b5132 RH |
30 | |
31 | /* Stabs entries use a 12 byte format: | |
32 | 4 byte string table index | |
33 | 1 byte stab type | |
34 | 1 byte stab other field | |
35 | 2 byte stab desc field | |
36 | 4 byte stab value | |
37 | FIXME: This will have to change for a 64 bit object format. | |
38 | ||
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. */ | |
43 | ||
44 | #define STRDXOFF (0) | |
45 | #define TYPEOFF (4) | |
46 | #define OTHEROFF (5) | |
47 | #define DESCOFF (6) | |
48 | #define VALOFF (8) | |
49 | #define STABSIZE (12) | |
50 | ||
51 | /* A hash table used for header files with N_BINCL entries. */ | |
52 | ||
53 | struct stab_link_includes_table | |
54 | { | |
55 | struct bfd_hash_table root; | |
56 | }; | |
57 | ||
58 | /* A linked list of totals that we have found for a particular header | |
59 | file. */ | |
60 | ||
61 | struct stab_link_includes_totals | |
62 | { | |
63 | struct stab_link_includes_totals *next; | |
64 | bfd_vma total; | |
65 | }; | |
66 | ||
67 | /* An entry in the header file hash table. */ | |
68 | ||
69 | struct stab_link_includes_entry | |
70 | { | |
71 | struct bfd_hash_entry root; | |
72 | /* List of totals we have found for this file. */ | |
73 | struct stab_link_includes_totals *totals; | |
74 | }; | |
75 | ||
76 | /* Look up an entry in an the header file hash table. */ | |
77 | ||
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))) | |
81 | ||
82 | /* This structure is used to hold a list of N_BINCL symbols, some of | |
83 | which might be converted into N_EXCL symbols. */ | |
84 | ||
85 | struct stab_excl_list | |
86 | { | |
87 | /* The next symbol to convert. */ | |
88 | struct stab_excl_list *next; | |
89 | /* The offset to this symbol in the section contents. */ | |
90 | bfd_size_type offset; | |
91 | /* The value to use for the symbol. */ | |
92 | bfd_vma val; | |
93 | /* The type of this symbol (N_BINCL or N_EXCL). */ | |
94 | int type; | |
95 | }; | |
96 | ||
97 | /* This structure is stored with each .stab section. */ | |
98 | ||
99 | struct stab_section_info | |
100 | { | |
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; | |
104 | ||
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 | |
7b82c249 | 111 | stab. */ |
252b5132 RH |
112 | bfd_size_type *cumulative_skips; |
113 | ||
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]; | |
118 | }; | |
119 | ||
120 | /* This structure is used to keep track of stabs in sections | |
121 | information while linking. */ | |
122 | ||
123 | struct stab_info | |
124 | { | |
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. */ | |
130 | asection *stabstr; | |
131 | }; | |
132 | ||
133 | static struct bfd_hash_entry *stab_link_includes_newfunc | |
134 | PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); | |
135 | \f | |
136 | /* The function to create a new entry in the header file hash table. */ | |
137 | ||
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; | |
142 | const char *string; | |
143 | { | |
144 | struct stab_link_includes_entry *ret = | |
145 | (struct stab_link_includes_entry *) entry; | |
146 | ||
147 | /* Allocate the structure if it has not already been allocated by a | |
148 | subclass. */ | |
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; | |
155 | ||
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)); | |
159 | if (ret) | |
160 | { | |
161 | /* Set local fields. */ | |
162 | ret->totals = NULL; | |
163 | } | |
164 | ||
165 | return (struct bfd_hash_entry *) ret; | |
166 | } | |
167 | \f | |
168 | /* This function is called for each input file from the add_symbols | |
169 | pass of the linker. */ | |
170 | ||
b34976b6 | 171 | bfd_boolean |
29ca8dc5 | 172 | _bfd_link_section_stabs (abfd, psinfo, stabsec, stabstrsec, psecinfo, pstring_offset) |
252b5132 RH |
173 | bfd *abfd; |
174 | PTR *psinfo; | |
175 | asection *stabsec; | |
176 | asection *stabstrsec; | |
177 | PTR *psecinfo; | |
29ca8dc5 | 178 | bfd_size_type *pstring_offset; |
252b5132 | 179 | { |
b34976b6 | 180 | bfd_boolean first; |
252b5132 | 181 | struct stab_info *sinfo; |
dc810e39 | 182 | bfd_size_type count, amt; |
252b5132 RH |
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; | |
189 | ||
190 | if (stabsec->_raw_size == 0 | |
191 | || stabstrsec->_raw_size == 0) | |
192 | { | |
193 | /* This file does not contain stabs debugging information. */ | |
b34976b6 | 194 | return TRUE; |
252b5132 RH |
195 | } |
196 | ||
197 | if (stabsec->_raw_size % STABSIZE != 0) | |
198 | { | |
199 | /* Something is wrong with the format of these stab symbols. | |
b34976b6 AM |
200 | Don't try to optimize them. */ |
201 | return TRUE; | |
252b5132 RH |
202 | } |
203 | ||
204 | if ((stabstrsec->flags & SEC_RELOC) != 0) | |
205 | { | |
206 | /* We shouldn't see relocations in the strings, and we aren't | |
b34976b6 AM |
207 | prepared to handle them. */ |
208 | return TRUE; | |
252b5132 RH |
209 | } |
210 | ||
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))) | |
215 | { | |
216 | /* At least one of the sections is being discarded from the | |
b34976b6 AM |
217 | link, so we should just ignore them. */ |
218 | return TRUE; | |
252b5132 RH |
219 | } |
220 | ||
b34976b6 | 221 | first = FALSE; |
252b5132 RH |
222 | |
223 | if (*psinfo == NULL) | |
224 | { | |
225 | /* Initialize the stabs information we need to keep track of. */ | |
b34976b6 | 226 | first = TRUE; |
dc810e39 AM |
227 | amt = sizeof (struct stab_info); |
228 | *psinfo = (PTR) bfd_alloc (abfd, amt); | |
252b5132 RH |
229 | if (*psinfo == NULL) |
230 | goto error_return; | |
231 | sinfo = (struct stab_info *) *psinfo; | |
232 | sinfo->strings = _bfd_stringtab_init (); | |
233 | if (sinfo->strings == NULL) | |
234 | goto error_return; | |
8140b664 | 235 | /* Make sure the first byte is zero. */ |
b34976b6 | 236 | (void) _bfd_stringtab_add (sinfo->strings, "", TRUE, TRUE); |
252b5132 RH |
237 | if (! bfd_hash_table_init_n (&sinfo->includes.root, |
238 | stab_link_includes_newfunc, | |
239 | 251)) | |
240 | goto error_return; | |
241 | sinfo->stabstr = bfd_make_section_anyway (abfd, ".stabstr"); | |
242 | sinfo->stabstr->flags |= SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING; | |
243 | } | |
244 | ||
245 | sinfo = (struct stab_info *) *psinfo; | |
246 | ||
247 | /* Initialize the information we are going to store for this .stab | |
248 | section. */ | |
249 | ||
250 | count = stabsec->_raw_size / STABSIZE; | |
251 | ||
dc810e39 AM |
252 | amt = sizeof (struct stab_section_info); |
253 | amt += (count - 1) * sizeof (bfd_size_type); | |
254 | *psecinfo = bfd_alloc (abfd, amt); | |
252b5132 RH |
255 | if (*psecinfo == NULL) |
256 | goto error_return; | |
257 | ||
258 | secinfo = (struct stab_section_info *) *psecinfo; | |
259 | secinfo->excls = NULL; | |
260 | secinfo->cumulative_skips = NULL; | |
dc810e39 | 261 | memset (secinfo->stridxs, 0, (size_t) count * sizeof (bfd_size_type)); |
252b5132 RH |
262 | |
263 | /* Read the stabs information from abfd. */ | |
264 | ||
265 | stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size); | |
266 | stabstrbuf = (bfd_byte *) bfd_malloc (stabstrsec->_raw_size); | |
267 | if (stabbuf == NULL || stabstrbuf == NULL) | |
268 | goto error_return; | |
269 | ||
dc810e39 | 270 | if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0, |
252b5132 | 271 | stabsec->_raw_size) |
dc810e39 | 272 | || ! bfd_get_section_contents (abfd, stabstrsec, stabstrbuf, (bfd_vma) 0, |
252b5132 RH |
273 | stabstrsec->_raw_size)) |
274 | goto error_return; | |
275 | ||
276 | /* Look through the stabs symbols, work out the new string indices, | |
277 | and identify N_BINCL symbols which can be eliminated. */ | |
278 | ||
279 | stroff = 0; | |
29ca8dc5 NS |
280 | /* The stabs sections can be split when |
281 | -split-by-reloc/-split-by-file is used. We must keep track of | |
282 | each stab section's place in the single concatenated string | |
283 | table. */ | |
284 | next_stroff = pstring_offset ? *pstring_offset : 0; | |
252b5132 RH |
285 | skip = 0; |
286 | ||
287 | symend = stabbuf + stabsec->_raw_size; | |
288 | for (sym = stabbuf, pstridx = secinfo->stridxs; | |
289 | sym < symend; | |
290 | sym += STABSIZE, ++pstridx) | |
291 | { | |
644c4c80 | 292 | bfd_size_type symstroff; |
252b5132 RH |
293 | int type; |
294 | const char *string; | |
295 | ||
296 | if (*pstridx != 0) | |
297 | { | |
298 | /* This symbol has already been handled by an N_BINCL pass. */ | |
299 | continue; | |
300 | } | |
301 | ||
302 | type = sym[TYPEOFF]; | |
303 | ||
304 | if (type == 0) | |
305 | { | |
306 | /* Special type 0 stabs indicate the offset to the next | |
b34976b6 | 307 | string table. We only copy the very first one. */ |
252b5132 RH |
308 | stroff = next_stroff; |
309 | next_stroff += bfd_get_32 (abfd, sym + 8); | |
29ca8dc5 NS |
310 | if (pstring_offset) |
311 | *pstring_offset = next_stroff; | |
252b5132 RH |
312 | if (! first) |
313 | { | |
314 | *pstridx = (bfd_size_type) -1; | |
315 | ++skip; | |
316 | continue; | |
317 | } | |
b34976b6 | 318 | first = FALSE; |
252b5132 RH |
319 | } |
320 | ||
321 | /* Store the string in the hash table, and record the index. */ | |
644c4c80 RS |
322 | symstroff = stroff + bfd_get_32 (abfd, sym + STRDXOFF); |
323 | if (symstroff >= stabstrsec->_raw_size) | |
324 | { | |
325 | (*_bfd_error_handler) | |
326 | (_("%s(%s+0x%lx): Stabs entry has invalid string index."), | |
327 | bfd_archive_filename (abfd), | |
328 | bfd_get_section_name (abfd, stabsec), | |
329 | (long) (sym - stabbuf)); | |
330 | bfd_set_error (bfd_error_bad_value); | |
331 | goto error_return; | |
332 | } | |
333 | string = (char *) stabstrbuf + symstroff; | |
b34976b6 | 334 | *pstridx = _bfd_stringtab_add (sinfo->strings, string, TRUE, TRUE); |
252b5132 RH |
335 | |
336 | /* An N_BINCL symbol indicates the start of the stabs entries | |
337 | for a header file. We need to scan ahead to the next N_EINCL | |
338 | symbol, ignoring nesting, adding up all the characters in the | |
339 | symbol names, not including the file numbers in types (the | |
340 | first number after an open parenthesis). */ | |
d45913a0 | 341 | if (type == (int) N_BINCL) |
252b5132 RH |
342 | { |
343 | bfd_vma val; | |
344 | int nest; | |
345 | bfd_byte *incl_sym; | |
346 | struct stab_link_includes_entry *incl_entry; | |
347 | struct stab_link_includes_totals *t; | |
348 | struct stab_excl_list *ne; | |
349 | ||
350 | val = 0; | |
351 | nest = 0; | |
352 | for (incl_sym = sym + STABSIZE; | |
353 | incl_sym < symend; | |
354 | incl_sym += STABSIZE) | |
355 | { | |
356 | int incl_type; | |
357 | ||
358 | incl_type = incl_sym[TYPEOFF]; | |
359 | if (incl_type == 0) | |
360 | break; | |
d45913a0 | 361 | else if (incl_type == (int) N_EINCL) |
252b5132 RH |
362 | { |
363 | if (nest == 0) | |
364 | break; | |
365 | --nest; | |
366 | } | |
d45913a0 | 367 | else if (incl_type == (int) N_BINCL) |
252b5132 RH |
368 | ++nest; |
369 | else if (nest == 0) | |
370 | { | |
371 | const char *str; | |
372 | ||
373 | str = ((char *) stabstrbuf | |
374 | + stroff | |
375 | + bfd_get_32 (abfd, incl_sym + STRDXOFF)); | |
376 | for (; *str != '\0'; str++) | |
377 | { | |
378 | val += *str; | |
379 | if (*str == '(') | |
380 | { | |
381 | /* Skip the file number. */ | |
382 | ++str; | |
3882b010 | 383 | while (ISDIGIT (*str)) |
252b5132 RH |
384 | ++str; |
385 | --str; | |
386 | } | |
387 | } | |
388 | } | |
389 | } | |
390 | ||
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 = stab_link_includes_lookup (&sinfo->includes, string, | |
b34976b6 | 394 | TRUE, TRUE); |
252b5132 RH |
395 | if (incl_entry == NULL) |
396 | goto error_return; | |
397 | ||
398 | for (t = incl_entry->totals; t != NULL; t = t->next) | |
399 | if (t->total == val) | |
400 | break; | |
401 | ||
402 | /* Record this symbol, so that we can set the value | |
b34976b6 | 403 | correctly. */ |
dc810e39 AM |
404 | amt = sizeof *ne; |
405 | ne = (struct stab_excl_list *) bfd_alloc (abfd, amt); | |
252b5132 RH |
406 | if (ne == NULL) |
407 | goto error_return; | |
408 | ne->offset = sym - stabbuf; | |
409 | ne->val = val; | |
d45913a0 | 410 | ne->type = (int) N_BINCL; |
252b5132 RH |
411 | ne->next = secinfo->excls; |
412 | secinfo->excls = ne; | |
413 | ||
414 | if (t == NULL) | |
415 | { | |
416 | /* This is the first time we have seen this header file | |
417 | with this set of stabs strings. */ | |
418 | t = ((struct stab_link_includes_totals *) | |
419 | bfd_hash_allocate (&sinfo->includes.root, sizeof *t)); | |
420 | if (t == NULL) | |
421 | goto error_return; | |
422 | t->total = val; | |
423 | t->next = incl_entry->totals; | |
424 | incl_entry->totals = t; | |
425 | } | |
426 | else | |
427 | { | |
428 | bfd_size_type *incl_pstridx; | |
429 | ||
430 | /* We have seen this header file before. Tell the final | |
431 | pass to change the type to N_EXCL. */ | |
d45913a0 | 432 | ne->type = (int) N_EXCL; |
252b5132 RH |
433 | |
434 | /* Mark the skipped symbols. */ | |
435 | ||
436 | nest = 0; | |
437 | for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1; | |
438 | incl_sym < symend; | |
439 | incl_sym += STABSIZE, ++incl_pstridx) | |
440 | { | |
441 | int incl_type; | |
442 | ||
443 | incl_type = incl_sym[TYPEOFF]; | |
444 | ||
d45913a0 | 445 | if (incl_type == (int) N_EINCL) |
252b5132 RH |
446 | { |
447 | if (nest == 0) | |
448 | { | |
449 | *incl_pstridx = (bfd_size_type) -1; | |
450 | ++skip; | |
451 | break; | |
452 | } | |
453 | --nest; | |
454 | } | |
d45913a0 | 455 | else if (incl_type == (int) N_BINCL) |
252b5132 RH |
456 | ++nest; |
457 | else if (nest == 0) | |
458 | { | |
459 | *incl_pstridx = (bfd_size_type) -1; | |
460 | ++skip; | |
461 | } | |
462 | } | |
463 | } | |
464 | } | |
465 | } | |
466 | ||
467 | free (stabbuf); | |
468 | stabbuf = NULL; | |
469 | free (stabstrbuf); | |
470 | stabstrbuf = NULL; | |
471 | ||
472 | /* We need to set the section sizes such that the linker will | |
473 | compute the output section sizes correctly. We set the .stab | |
474 | size to not include the entries we don't want. We set | |
475 | SEC_EXCLUDE for the .stabstr section, so that it will be dropped | |
476 | from the link. We record the size of the strtab in the first | |
477 | .stabstr section we saw, and make sure we don't set SEC_EXCLUDE | |
478 | for that section. */ | |
479 | stabsec->_cooked_size = (count - skip) * STABSIZE; | |
480 | if (stabsec->_cooked_size == 0) | |
481 | stabsec->flags |= SEC_EXCLUDE; | |
482 | stabstrsec->flags |= SEC_EXCLUDE; | |
483 | sinfo->stabstr->_cooked_size = _bfd_stringtab_size (sinfo->strings); | |
484 | ||
485 | /* Calculate the `cumulative_skips' array now that stabs have been | |
7b82c249 | 486 | deleted for this section. */ |
252b5132 RH |
487 | |
488 | if (skip != 0) | |
489 | { | |
490 | bfd_size_type i, offset; | |
491 | bfd_size_type *pskips; | |
492 | ||
dc810e39 AM |
493 | amt = count * sizeof (bfd_size_type); |
494 | secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt); | |
252b5132 RH |
495 | if (secinfo->cumulative_skips == NULL) |
496 | goto error_return; | |
497 | ||
498 | pskips = secinfo->cumulative_skips; | |
499 | pstridx = secinfo->stridxs; | |
500 | offset = 0; | |
501 | ||
502 | for (i = 0; i < count; i++, pskips++, pstridx++) | |
503 | { | |
504 | *pskips = offset; | |
505 | if (*pstridx == (bfd_size_type) -1) | |
506 | offset += STABSIZE; | |
507 | } | |
508 | ||
509 | BFD_ASSERT (offset != 0); | |
510 | } | |
511 | ||
b34976b6 | 512 | return TRUE; |
252b5132 RH |
513 | |
514 | error_return: | |
515 | if (stabbuf != NULL) | |
516 | free (stabbuf); | |
517 | if (stabstrbuf != NULL) | |
518 | free (stabstrbuf); | |
b34976b6 | 519 | return FALSE; |
252b5132 RH |
520 | } |
521 | ||
73d074b4 DJ |
522 | \f |
523 | /* This function is called for each input file before the stab | |
524 | section is relocated. It discards stab entries for discarded | |
b34976b6 | 525 | functions and variables. The function returns TRUE iff |
73d074b4 DJ |
526 | any entries have been deleted. |
527 | */ | |
528 | ||
b34976b6 | 529 | bfd_boolean |
73d074b4 DJ |
530 | _bfd_discard_section_stabs (abfd, stabsec, psecinfo, |
531 | reloc_symbol_deleted_p, cookie) | |
532 | bfd *abfd; | |
533 | asection *stabsec; | |
534 | PTR psecinfo; | |
b34976b6 | 535 | bfd_boolean (*reloc_symbol_deleted_p) PARAMS ((bfd_vma, PTR)); |
73d074b4 DJ |
536 | PTR cookie; |
537 | { | |
538 | bfd_size_type count, amt; | |
539 | struct stab_section_info *secinfo; | |
540 | bfd_byte *stabbuf = NULL; | |
541 | bfd_byte *sym, *symend; | |
542 | bfd_size_type skip; | |
543 | bfd_size_type *pstridx; | |
544 | int deleting; | |
545 | ||
546 | if (stabsec->_raw_size == 0) | |
547 | { | |
548 | /* This file does not contain stabs debugging information. */ | |
b34976b6 | 549 | return FALSE; |
73d074b4 DJ |
550 | } |
551 | ||
552 | if (stabsec->_raw_size % STABSIZE != 0) | |
553 | { | |
554 | /* Something is wrong with the format of these stab symbols. | |
b34976b6 AM |
555 | Don't try to optimize them. */ |
556 | return FALSE; | |
73d074b4 DJ |
557 | } |
558 | ||
559 | if ((stabsec->output_section != NULL | |
560 | && bfd_is_abs_section (stabsec->output_section))) | |
561 | { | |
562 | /* At least one of the sections is being discarded from the | |
b34976b6 AM |
563 | link, so we should just ignore them. */ |
564 | return FALSE; | |
73d074b4 DJ |
565 | } |
566 | ||
567 | /* We should have initialized our data in _bfd_link_stab_sections. | |
568 | If there was some bizarre error reading the string sections, though, | |
569 | we might not have. Bail rather than asserting. */ | |
570 | if (psecinfo == NULL) | |
b34976b6 | 571 | return FALSE; |
73d074b4 DJ |
572 | |
573 | count = stabsec->_raw_size / STABSIZE; | |
574 | secinfo = (struct stab_section_info *) psecinfo; | |
575 | ||
576 | /* Read the stabs information from abfd. */ | |
577 | ||
578 | stabbuf = (bfd_byte *) bfd_malloc (stabsec->_raw_size); | |
579 | if (stabbuf == NULL) | |
580 | goto error_return; | |
581 | ||
582 | if (! bfd_get_section_contents (abfd, stabsec, stabbuf, (bfd_vma) 0, | |
583 | stabsec->_raw_size)) | |
584 | goto error_return; | |
585 | ||
586 | /* Look through the stabs symbols and discard any information for | |
587 | discarded functions. */ | |
588 | ||
589 | skip = 0; | |
590 | deleting = -1; | |
591 | ||
592 | symend = stabbuf + stabsec->_raw_size; | |
593 | for (sym = stabbuf, pstridx = secinfo->stridxs; | |
594 | sym < symend; | |
595 | sym += STABSIZE, ++pstridx) | |
596 | { | |
597 | int type; | |
598 | ||
599 | if (*pstridx == (bfd_size_type) -1) | |
600 | { | |
601 | /* This stab was deleted in a previous pass. */ | |
602 | continue; | |
603 | } | |
604 | ||
605 | type = sym[TYPEOFF]; | |
606 | ||
d45913a0 | 607 | if (type == (int) N_FUN) |
73d074b4 DJ |
608 | { |
609 | int strx = bfd_get_32 (abfd, sym + STRDXOFF); | |
610 | ||
611 | if (strx == 0) | |
612 | { | |
613 | if (deleting) | |
614 | { | |
615 | skip++; | |
616 | *pstridx = -1; | |
617 | } | |
618 | deleting = -1; | |
619 | continue; | |
620 | } | |
621 | deleting = 0; | |
622 | if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie)) | |
623 | deleting = 1; | |
624 | } | |
625 | ||
626 | if (deleting == 1) | |
627 | { | |
628 | *pstridx = -1; | |
629 | skip++; | |
630 | } | |
631 | else if (deleting == -1) | |
632 | { | |
633 | /* Outside of a function. Check for deleted variables. */ | |
d45913a0 | 634 | if (type == (int) N_STSYM || type == (int) N_LCSYM) |
73d074b4 DJ |
635 | if ((*reloc_symbol_deleted_p) (sym + VALOFF - stabbuf, cookie)) |
636 | { | |
637 | *pstridx = -1; | |
638 | skip ++; | |
639 | } | |
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. */ | |
643 | } | |
644 | } | |
645 | ||
646 | free (stabbuf); | |
647 | stabbuf = NULL; | |
648 | ||
649 | /* Shrink the stabsec as needed. */ | |
650 | stabsec->_cooked_size -= skip * STABSIZE; | |
651 | if (stabsec->_cooked_size == 0) | |
652 | stabsec->flags |= SEC_EXCLUDE; | |
653 | ||
654 | /* Recalculate the `cumulative_skips' array now that stabs have been | |
655 | deleted for this section. */ | |
656 | ||
657 | if (skip != 0) | |
658 | { | |
659 | bfd_size_type i, offset; | |
660 | bfd_size_type *pskips; | |
661 | ||
662 | if (secinfo->cumulative_skips == NULL) | |
663 | { | |
664 | amt = count * sizeof (bfd_size_type); | |
665 | secinfo->cumulative_skips = (bfd_size_type *) bfd_alloc (abfd, amt); | |
666 | if (secinfo->cumulative_skips == NULL) | |
667 | goto error_return; | |
668 | } | |
669 | ||
670 | pskips = secinfo->cumulative_skips; | |
671 | pstridx = secinfo->stridxs; | |
672 | offset = 0; | |
673 | ||
674 | for (i = 0; i < count; i++, pskips++, pstridx++) | |
675 | { | |
676 | *pskips = offset; | |
677 | if (*pstridx == (bfd_size_type) -1) | |
678 | offset += STABSIZE; | |
679 | } | |
680 | ||
681 | BFD_ASSERT (offset != 0); | |
682 | } | |
683 | ||
b34976b6 | 684 | return skip > 0; |
73d074b4 DJ |
685 | |
686 | error_return: | |
687 | if (stabbuf != NULL) | |
688 | free (stabbuf); | |
b34976b6 | 689 | return FALSE; |
73d074b4 DJ |
690 | } |
691 | ||
252b5132 RH |
692 | /* Write out the stab section. This is called with the relocated |
693 | contents. */ | |
694 | ||
b34976b6 | 695 | bfd_boolean |
252b5132 RH |
696 | _bfd_write_section_stabs (output_bfd, psinfo, stabsec, psecinfo, contents) |
697 | bfd *output_bfd; | |
698 | PTR *psinfo; | |
699 | asection *stabsec; | |
700 | PTR *psecinfo; | |
701 | bfd_byte *contents; | |
702 | { | |
703 | struct stab_info *sinfo; | |
704 | struct stab_section_info *secinfo; | |
705 | struct stab_excl_list *e; | |
706 | bfd_byte *sym, *tosym, *symend; | |
707 | bfd_size_type *pstridx; | |
708 | ||
709 | sinfo = (struct stab_info *) *psinfo; | |
710 | secinfo = (struct stab_section_info *) *psecinfo; | |
711 | ||
712 | if (secinfo == NULL) | |
713 | return bfd_set_section_contents (output_bfd, stabsec->output_section, | |
dc810e39 AM |
714 | contents, |
715 | (file_ptr) stabsec->output_offset, | |
252b5132 RH |
716 | stabsec->_raw_size); |
717 | ||
718 | /* Handle each N_BINCL entry. */ | |
719 | for (e = secinfo->excls; e != NULL; e = e->next) | |
720 | { | |
721 | bfd_byte *excl_sym; | |
722 | ||
723 | BFD_ASSERT (e->offset < stabsec->_raw_size); | |
724 | excl_sym = contents + e->offset; | |
725 | bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF); | |
726 | excl_sym[TYPEOFF] = e->type; | |
727 | } | |
728 | ||
729 | /* Copy over all the stabs symbols, omitting the ones we don't want, | |
730 | and correcting the string indices for those we do want. */ | |
731 | tosym = contents; | |
732 | symend = contents + stabsec->_raw_size; | |
733 | for (sym = contents, pstridx = secinfo->stridxs; | |
734 | sym < symend; | |
735 | sym += STABSIZE, ++pstridx) | |
736 | { | |
737 | if (*pstridx != (bfd_size_type) -1) | |
738 | { | |
739 | if (tosym != sym) | |
740 | memcpy (tosym, sym, STABSIZE); | |
741 | bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF); | |
742 | ||
743 | if (sym[TYPEOFF] == 0) | |
744 | { | |
745 | /* This is the header symbol for the stabs section. We | |
b34976b6 AM |
746 | don't really need one, since we have merged all the |
747 | input stabs sections into one, but we generate one | |
748 | for the benefit of readers which expect to see one. */ | |
252b5132 RH |
749 | BFD_ASSERT (sym == contents); |
750 | bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings), | |
751 | tosym + VALOFF); | |
752 | bfd_put_16 (output_bfd, | |
753 | stabsec->output_section->_raw_size / STABSIZE - 1, | |
754 | tosym + DESCOFF); | |
755 | } | |
756 | ||
757 | tosym += STABSIZE; | |
758 | } | |
759 | } | |
760 | ||
761 | BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->_cooked_size); | |
762 | ||
763 | return bfd_set_section_contents (output_bfd, stabsec->output_section, | |
dc810e39 | 764 | contents, (file_ptr) stabsec->output_offset, |
252b5132 RH |
765 | stabsec->_cooked_size); |
766 | } | |
767 | ||
768 | /* Write out the .stabstr section. */ | |
769 | ||
b34976b6 | 770 | bfd_boolean |
252b5132 RH |
771 | _bfd_write_stab_strings (output_bfd, psinfo) |
772 | bfd *output_bfd; | |
773 | PTR *psinfo; | |
774 | { | |
775 | struct stab_info *sinfo; | |
776 | ||
777 | sinfo = (struct stab_info *) *psinfo; | |
778 | ||
779 | if (sinfo == NULL) | |
b34976b6 | 780 | return TRUE; |
252b5132 RH |
781 | |
782 | if (bfd_is_abs_section (sinfo->stabstr->output_section)) | |
783 | { | |
784 | /* The section was discarded from the link. */ | |
b34976b6 | 785 | return TRUE; |
252b5132 RH |
786 | } |
787 | ||
788 | BFD_ASSERT ((sinfo->stabstr->output_offset | |
789 | + _bfd_stringtab_size (sinfo->strings)) | |
790 | <= sinfo->stabstr->output_section->_raw_size); | |
791 | ||
792 | if (bfd_seek (output_bfd, | |
dc810e39 AM |
793 | (file_ptr) (sinfo->stabstr->output_section->filepos |
794 | + sinfo->stabstr->output_offset), | |
252b5132 | 795 | SEEK_SET) != 0) |
b34976b6 | 796 | return FALSE; |
252b5132 RH |
797 | |
798 | if (! _bfd_stringtab_emit (output_bfd, sinfo->strings)) | |
b34976b6 | 799 | return FALSE; |
252b5132 RH |
800 | |
801 | /* We no longer need the stabs information. */ | |
802 | _bfd_stringtab_free (sinfo->strings); | |
803 | bfd_hash_table_free (&sinfo->includes.root); | |
804 | ||
b34976b6 | 805 | return TRUE; |
252b5132 RH |
806 | } |
807 | ||
808 | /* Adjust an address in the .stab section. Given OFFSET within | |
809 | STABSEC, this returns the new offset in the adjusted stab section, | |
810 | or -1 if the address refers to a stab which has been removed. */ | |
811 | ||
812 | bfd_vma | |
813 | _bfd_stab_section_offset (output_bfd, psinfo, stabsec, psecinfo, offset) | |
7442e600 ILT |
814 | bfd *output_bfd ATTRIBUTE_UNUSED; |
815 | PTR *psinfo ATTRIBUTE_UNUSED; | |
252b5132 RH |
816 | asection *stabsec; |
817 | PTR *psecinfo; | |
818 | bfd_vma offset; | |
819 | { | |
820 | struct stab_section_info *secinfo; | |
821 | ||
822 | secinfo = (struct stab_section_info *) *psecinfo; | |
823 | ||
824 | if (secinfo == NULL) | |
825 | return offset; | |
826 | ||
827 | if (offset >= stabsec->_raw_size) | |
828 | return offset - (stabsec->_cooked_size - stabsec->_raw_size); | |
829 | ||
830 | if (secinfo->cumulative_skips) | |
831 | { | |
832 | bfd_vma i; | |
833 | ||
834 | i = offset / STABSIZE; | |
835 | ||
836 | if (secinfo->stridxs [i] == (bfd_size_type) -1) | |
837 | return (bfd_vma) -1; | |
838 | ||
839 | return offset - secinfo->cumulative_skips [i]; | |
840 | } | |
841 | ||
842 | return offset; | |
843 | } |