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