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