| 1 | /* Stabs in sections linking support. | 
|---|
| 2 | Copyright 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc. | 
|---|
| 3 | Written by Ian Lance Taylor, Cygnus Support. | 
|---|
| 4 |  | 
|---|
| 5 | This file is part of BFD, the Binary File Descriptor library. | 
|---|
| 6 |  | 
|---|
| 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. | 
|---|
| 11 |  | 
|---|
| 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. | 
|---|
| 16 |  | 
|---|
| 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.  */ | 
|---|
| 20 |  | 
|---|
| 21 | /* This file contains support for linking stabs in sections, as used | 
|---|
| 22 | on COFF and ELF.  */ | 
|---|
| 23 |  | 
|---|
| 24 | #include "bfd.h" | 
|---|
| 25 | #include "sysdep.h" | 
|---|
| 26 | #include "libbfd.h" | 
|---|
| 27 | #include "aout/stab_gnu.h" | 
|---|
| 28 |  | 
|---|
| 29 | #include <ctype.h> | 
|---|
| 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 | 
|---|
| 111 | stab.  */ | 
|---|
| 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 |  | 
|---|
| 136 |  | 
|---|
| 137 | /* The function to create a new entry in the header file hash table.  */ | 
|---|
| 138 |  | 
|---|
| 139 | static struct bfd_hash_entry * | 
|---|
| 140 | stab_link_includes_newfunc (entry, table, string) | 
|---|
| 141 | struct bfd_hash_entry *entry; | 
|---|
| 142 | struct bfd_hash_table *table; | 
|---|
| 143 | const char *string; | 
|---|
| 144 | { | 
|---|
| 145 | struct stab_link_includes_entry *ret = | 
|---|
| 146 | (struct stab_link_includes_entry *) entry; | 
|---|
| 147 |  | 
|---|
| 148 | /* Allocate the structure if it has not already been allocated by a | 
|---|
| 149 | subclass.  */ | 
|---|
| 150 | if (ret == (struct stab_link_includes_entry *) NULL) | 
|---|
| 151 | ret = ((struct stab_link_includes_entry *) | 
|---|
| 152 | bfd_hash_allocate (table, | 
|---|
| 153 | sizeof (struct stab_link_includes_entry))); | 
|---|
| 154 | if (ret == (struct stab_link_includes_entry *) NULL) | 
|---|
| 155 | return (struct bfd_hash_entry *) ret; | 
|---|
| 156 |  | 
|---|
| 157 | /* Call the allocation method of the superclass.  */ | 
|---|
| 158 | ret = ((struct stab_link_includes_entry *) | 
|---|
| 159 | bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string)); | 
|---|
| 160 | if (ret) | 
|---|
| 161 | { | 
|---|
| 162 | /* Set local fields.  */ | 
|---|
| 163 | ret->totals = NULL; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | return (struct bfd_hash_entry *) ret; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 |  | 
|---|
| 170 | /* This function is called for each input file from the add_symbols | 
|---|
| 171 | pass of the linker.  */ | 
|---|
| 172 |  | 
|---|
| 173 | boolean | 
|---|
| 174 | _bfd_link_section_stabs (abfd, psinfo, stabsec, stabstrsec, psecinfo) | 
|---|
| 175 | bfd *abfd; | 
|---|
| 176 | PTR *psinfo; | 
|---|
| 177 | asection *stabsec; | 
|---|
| 178 | asection *stabstrsec; | 
|---|
| 179 | PTR *psecinfo; | 
|---|
| 180 | { | 
|---|
| 181 | boolean first; | 
|---|
| 182 | struct stab_info *sinfo; | 
|---|
| 183 | bfd_size_type count; | 
|---|
| 184 | struct stab_section_info *secinfo; | 
|---|
| 185 | bfd_byte *stabbuf = NULL; | 
|---|
| 186 | bfd_byte *stabstrbuf = NULL; | 
|---|
| 187 | bfd_byte *sym, *symend; | 
|---|
| 188 | bfd_size_type stroff, next_stroff, skip; | 
|---|
| 189 | bfd_size_type *pstridx; | 
|---|
| 190 |  | 
|---|
| 191 | if (stabsec->_raw_size == 0 | 
|---|
| 192 | || stabstrsec->_raw_size == 0) | 
|---|
| 193 | { | 
|---|
| 194 | /* This file does not contain stabs debugging information.  */ | 
|---|
| 195 | return true; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 | if (stabsec->_raw_size % STABSIZE != 0) | 
|---|
| 199 | { | 
|---|
| 200 | /* Something is wrong with the format of these stab symbols. | 
|---|
| 201 | Don't try to optimize them.  */ | 
|---|
| 202 | return true; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 | if ((stabstrsec->flags & SEC_RELOC) != 0) | 
|---|
| 206 | { | 
|---|
| 207 | /* We shouldn't see relocations in the strings, and we aren't | 
|---|
| 208 | prepared to handle them.  */ | 
|---|
| 209 | return true; | 
|---|
| 210 | } | 
|---|
| 211 |  | 
|---|
| 212 | if ((stabsec->output_section != NULL | 
|---|
| 213 | && bfd_is_abs_section (stabsec->output_section)) | 
|---|
| 214 | || (stabstrsec->output_section != NULL | 
|---|
| 215 | && bfd_is_abs_section (stabstrsec->output_section))) | 
|---|
| 216 | { | 
|---|
| 217 | /* At least one of the sections is being discarded from the | 
|---|
| 218 | link, so we should just ignore them.  */ | 
|---|
| 219 | return true; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | first = false; | 
|---|
| 223 |  | 
|---|
| 224 | if (*psinfo == NULL) | 
|---|
| 225 | { | 
|---|
| 226 | /* Initialize the stabs information we need to keep track of.  */ | 
|---|
| 227 | first = true; | 
|---|
| 228 | *psinfo = (PTR) bfd_alloc (abfd, sizeof (struct stab_info)); | 
|---|
| 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; | 
|---|
| 235 | /* Make sure the first byte is zero.  */ | 
|---|
| 236 | (void) _bfd_stringtab_add (sinfo->strings, "", true, true); | 
|---|
| 237 | if (! bfd_hash_table_init_n (&sinfo->includes.root, | 
|---|
| 238 | stab_link_includes_newfunc, | 
|---|
| 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 |  | 
|---|
| 252 | *psecinfo = bfd_alloc (abfd, | 
|---|
| 253 | (sizeof (struct stab_section_info) | 
|---|
| 254 | + (count - 1) * sizeof (bfd_size_type))); | 
|---|
| 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; | 
|---|
| 261 | memset (secinfo->stridxs, 0, count * sizeof (bfd_size_type)); | 
|---|
| 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 |  | 
|---|
| 270 | if (! bfd_get_section_contents (abfd, stabsec, stabbuf, 0, | 
|---|
| 271 | stabsec->_raw_size) | 
|---|
| 272 | || ! bfd_get_section_contents (abfd, stabstrsec, stabstrbuf, 0, | 
|---|
| 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; | 
|---|
| 280 | next_stroff = 0; | 
|---|
| 281 | skip = 0; | 
|---|
| 282 |  | 
|---|
| 283 | symend = stabbuf + stabsec->_raw_size; | 
|---|
| 284 | for (sym = stabbuf, pstridx = secinfo->stridxs; | 
|---|
| 285 | sym < symend; | 
|---|
| 286 | sym += STABSIZE, ++pstridx) | 
|---|
| 287 | { | 
|---|
| 288 | int type; | 
|---|
| 289 | const char *string; | 
|---|
| 290 |  | 
|---|
| 291 | if (*pstridx != 0) | 
|---|
| 292 | { | 
|---|
| 293 | /* This symbol has already been handled by an N_BINCL pass.  */ | 
|---|
| 294 | continue; | 
|---|
| 295 | } | 
|---|
| 296 |  | 
|---|
| 297 | type = sym[TYPEOFF]; | 
|---|
| 298 |  | 
|---|
| 299 | if (type == 0) | 
|---|
| 300 | { | 
|---|
| 301 | /* Special type 0 stabs indicate the offset to the next | 
|---|
| 302 | string table.  We only copy the very first one.  */ | 
|---|
| 303 | stroff = next_stroff; | 
|---|
| 304 | next_stroff += bfd_get_32 (abfd, sym + 8); | 
|---|
| 305 | if (! first) | 
|---|
| 306 | { | 
|---|
| 307 | *pstridx = (bfd_size_type) -1; | 
|---|
| 308 | ++skip; | 
|---|
| 309 | continue; | 
|---|
| 310 | } | 
|---|
| 311 | first = false; | 
|---|
| 312 | } | 
|---|
| 313 |  | 
|---|
| 314 | /* Store the string in the hash table, and record the index.  */ | 
|---|
| 315 | string = ((char *) stabstrbuf | 
|---|
| 316 | + stroff | 
|---|
| 317 | + bfd_get_32 (abfd, sym + STRDXOFF)); | 
|---|
| 318 | *pstridx = _bfd_stringtab_add (sinfo->strings, string, true, true); | 
|---|
| 319 |  | 
|---|
| 320 | /* An N_BINCL symbol indicates the start of the stabs entries | 
|---|
| 321 | for a header file.  We need to scan ahead to the next N_EINCL | 
|---|
| 322 | symbol, ignoring nesting, adding up all the characters in the | 
|---|
| 323 | symbol names, not including the file numbers in types (the | 
|---|
| 324 | first number after an open parenthesis).  */ | 
|---|
| 325 | if (type == N_BINCL) | 
|---|
| 326 | { | 
|---|
| 327 | bfd_vma val; | 
|---|
| 328 | int nest; | 
|---|
| 329 | bfd_byte *incl_sym; | 
|---|
| 330 | struct stab_link_includes_entry *incl_entry; | 
|---|
| 331 | struct stab_link_includes_totals *t; | 
|---|
| 332 | struct stab_excl_list *ne; | 
|---|
| 333 |  | 
|---|
| 334 | val = 0; | 
|---|
| 335 | nest = 0; | 
|---|
| 336 | for (incl_sym = sym + STABSIZE; | 
|---|
| 337 | incl_sym < symend; | 
|---|
| 338 | incl_sym += STABSIZE) | 
|---|
| 339 | { | 
|---|
| 340 | int incl_type; | 
|---|
| 341 |  | 
|---|
| 342 | incl_type = incl_sym[TYPEOFF]; | 
|---|
| 343 | if (incl_type == 0) | 
|---|
| 344 | break; | 
|---|
| 345 | else if (incl_type == N_EINCL) | 
|---|
| 346 | { | 
|---|
| 347 | if (nest == 0) | 
|---|
| 348 | break; | 
|---|
| 349 | --nest; | 
|---|
| 350 | } | 
|---|
| 351 | else if (incl_type == N_BINCL) | 
|---|
| 352 | ++nest; | 
|---|
| 353 | else if (nest == 0) | 
|---|
| 354 | { | 
|---|
| 355 | const char *str; | 
|---|
| 356 |  | 
|---|
| 357 | str = ((char *) stabstrbuf | 
|---|
| 358 | + stroff | 
|---|
| 359 | + bfd_get_32 (abfd, incl_sym + STRDXOFF)); | 
|---|
| 360 | for (; *str != '\0'; str++) | 
|---|
| 361 | { | 
|---|
| 362 | val += *str; | 
|---|
| 363 | if (*str == '(') | 
|---|
| 364 | { | 
|---|
| 365 | /* Skip the file number.  */ | 
|---|
| 366 | ++str; | 
|---|
| 367 | while (isdigit ((unsigned char) *str)) | 
|---|
| 368 | ++str; | 
|---|
| 369 | --str; | 
|---|
| 370 | } | 
|---|
| 371 | } | 
|---|
| 372 | } | 
|---|
| 373 | } | 
|---|
| 374 |  | 
|---|
| 375 | /* If we have already included a header file with the same | 
|---|
| 376 | value, then replaced this one with an N_EXCL symbol.  */ | 
|---|
| 377 | incl_entry = stab_link_includes_lookup (&sinfo->includes, string, | 
|---|
| 378 | true, true); | 
|---|
| 379 | if (incl_entry == NULL) | 
|---|
| 380 | goto error_return; | 
|---|
| 381 |  | 
|---|
| 382 | for (t = incl_entry->totals; t != NULL; t = t->next) | 
|---|
| 383 | if (t->total == val) | 
|---|
| 384 | break; | 
|---|
| 385 |  | 
|---|
| 386 | /* Record this symbol, so that we can set the value | 
|---|
| 387 | correctly.  */ | 
|---|
| 388 | ne = (struct stab_excl_list *) bfd_alloc (abfd, sizeof *ne); | 
|---|
| 389 | if (ne == NULL) | 
|---|
| 390 | goto error_return; | 
|---|
| 391 | ne->offset = sym - stabbuf; | 
|---|
| 392 | ne->val = val; | 
|---|
| 393 | ne->type = N_BINCL; | 
|---|
| 394 | ne->next = secinfo->excls; | 
|---|
| 395 | secinfo->excls = ne; | 
|---|
| 396 |  | 
|---|
| 397 | if (t == NULL) | 
|---|
| 398 | { | 
|---|
| 399 | /* This is the first time we have seen this header file | 
|---|
| 400 | with this set of stabs strings.  */ | 
|---|
| 401 | t = ((struct stab_link_includes_totals *) | 
|---|
| 402 | bfd_hash_allocate (&sinfo->includes.root, sizeof *t)); | 
|---|
| 403 | if (t == NULL) | 
|---|
| 404 | goto error_return; | 
|---|
| 405 | t->total = val; | 
|---|
| 406 | t->next = incl_entry->totals; | 
|---|
| 407 | incl_entry->totals = t; | 
|---|
| 408 | } | 
|---|
| 409 | else | 
|---|
| 410 | { | 
|---|
| 411 | bfd_size_type *incl_pstridx; | 
|---|
| 412 |  | 
|---|
| 413 | /* We have seen this header file before.  Tell the final | 
|---|
| 414 | pass to change the type to N_EXCL.  */ | 
|---|
| 415 | ne->type = N_EXCL; | 
|---|
| 416 |  | 
|---|
| 417 | /* Mark the skipped symbols.  */ | 
|---|
| 418 |  | 
|---|
| 419 | nest = 0; | 
|---|
| 420 | for (incl_sym = sym + STABSIZE, incl_pstridx = pstridx + 1; | 
|---|
| 421 | incl_sym < symend; | 
|---|
| 422 | incl_sym += STABSIZE, ++incl_pstridx) | 
|---|
| 423 | { | 
|---|
| 424 | int incl_type; | 
|---|
| 425 |  | 
|---|
| 426 | incl_type = incl_sym[TYPEOFF]; | 
|---|
| 427 |  | 
|---|
| 428 | if (incl_type == N_EINCL) | 
|---|
| 429 | { | 
|---|
| 430 | if (nest == 0) | 
|---|
| 431 | { | 
|---|
| 432 | *incl_pstridx = (bfd_size_type) -1; | 
|---|
| 433 | ++skip; | 
|---|
| 434 | break; | 
|---|
| 435 | } | 
|---|
| 436 | --nest; | 
|---|
| 437 | } | 
|---|
| 438 | else if (incl_type == N_BINCL) | 
|---|
| 439 | ++nest; | 
|---|
| 440 | else if (nest == 0) | 
|---|
| 441 | { | 
|---|
| 442 | *incl_pstridx = (bfd_size_type) -1; | 
|---|
| 443 | ++skip; | 
|---|
| 444 | } | 
|---|
| 445 | } | 
|---|
| 446 | } | 
|---|
| 447 | } | 
|---|
| 448 | } | 
|---|
| 449 |  | 
|---|
| 450 | free (stabbuf); | 
|---|
| 451 | stabbuf = NULL; | 
|---|
| 452 | free (stabstrbuf); | 
|---|
| 453 | stabstrbuf = NULL; | 
|---|
| 454 |  | 
|---|
| 455 | /* We need to set the section sizes such that the linker will | 
|---|
| 456 | compute the output section sizes correctly.  We set the .stab | 
|---|
| 457 | size to not include the entries we don't want.  We set | 
|---|
| 458 | SEC_EXCLUDE for the .stabstr section, so that it will be dropped | 
|---|
| 459 | from the link.  We record the size of the strtab in the first | 
|---|
| 460 | .stabstr section we saw, and make sure we don't set SEC_EXCLUDE | 
|---|
| 461 | for that section.  */ | 
|---|
| 462 | stabsec->_cooked_size = (count - skip) * STABSIZE; | 
|---|
| 463 | if (stabsec->_cooked_size == 0) | 
|---|
| 464 | stabsec->flags |= SEC_EXCLUDE; | 
|---|
| 465 | stabstrsec->flags |= SEC_EXCLUDE; | 
|---|
| 466 | sinfo->stabstr->_cooked_size = _bfd_stringtab_size (sinfo->strings); | 
|---|
| 467 |  | 
|---|
| 468 | /* Calculate the `cumulative_skips' array now that stabs have been | 
|---|
| 469 | deleted for this section.  */ | 
|---|
| 470 |  | 
|---|
| 471 | if (skip != 0) | 
|---|
| 472 | { | 
|---|
| 473 | bfd_size_type i, offset; | 
|---|
| 474 | bfd_size_type *pskips; | 
|---|
| 475 |  | 
|---|
| 476 | secinfo->cumulative_skips = | 
|---|
| 477 | (bfd_size_type *) bfd_alloc (abfd, count * sizeof (bfd_size_type)); | 
|---|
| 478 | if (secinfo->cumulative_skips == NULL) | 
|---|
| 479 | goto error_return; | 
|---|
| 480 |  | 
|---|
| 481 | pskips = secinfo->cumulative_skips; | 
|---|
| 482 | pstridx = secinfo->stridxs; | 
|---|
| 483 | offset = 0; | 
|---|
| 484 |  | 
|---|
| 485 | for (i = 0; i < count; i++, pskips++, pstridx++) | 
|---|
| 486 | { | 
|---|
| 487 | *pskips = offset; | 
|---|
| 488 | if (*pstridx == (bfd_size_type) -1) | 
|---|
| 489 | offset += STABSIZE; | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | BFD_ASSERT (offset != 0); | 
|---|
| 493 | } | 
|---|
| 494 |  | 
|---|
| 495 | return true; | 
|---|
| 496 |  | 
|---|
| 497 | error_return: | 
|---|
| 498 | if (stabbuf != NULL) | 
|---|
| 499 | free (stabbuf); | 
|---|
| 500 | if (stabstrbuf != NULL) | 
|---|
| 501 | free (stabstrbuf); | 
|---|
| 502 | return false; | 
|---|
| 503 | } | 
|---|
| 504 |  | 
|---|
| 505 | /* Write out the stab section.  This is called with the relocated | 
|---|
| 506 | contents.  */ | 
|---|
| 507 |  | 
|---|
| 508 | boolean | 
|---|
| 509 | _bfd_write_section_stabs (output_bfd, psinfo, stabsec, psecinfo, contents) | 
|---|
| 510 | bfd *output_bfd; | 
|---|
| 511 | PTR *psinfo; | 
|---|
| 512 | asection *stabsec; | 
|---|
| 513 | PTR *psecinfo; | 
|---|
| 514 | bfd_byte *contents; | 
|---|
| 515 | { | 
|---|
| 516 | struct stab_info *sinfo; | 
|---|
| 517 | struct stab_section_info *secinfo; | 
|---|
| 518 | struct stab_excl_list *e; | 
|---|
| 519 | bfd_byte *sym, *tosym, *symend; | 
|---|
| 520 | bfd_size_type *pstridx; | 
|---|
| 521 |  | 
|---|
| 522 | sinfo = (struct stab_info *) *psinfo; | 
|---|
| 523 | secinfo = (struct stab_section_info *) *psecinfo; | 
|---|
| 524 |  | 
|---|
| 525 | if (secinfo == NULL) | 
|---|
| 526 | return bfd_set_section_contents (output_bfd, stabsec->output_section, | 
|---|
| 527 | contents, stabsec->output_offset, | 
|---|
| 528 | stabsec->_raw_size); | 
|---|
| 529 |  | 
|---|
| 530 | /* Handle each N_BINCL entry.  */ | 
|---|
| 531 | for (e = secinfo->excls; e != NULL; e = e->next) | 
|---|
| 532 | { | 
|---|
| 533 | bfd_byte *excl_sym; | 
|---|
| 534 |  | 
|---|
| 535 | BFD_ASSERT (e->offset < stabsec->_raw_size); | 
|---|
| 536 | excl_sym = contents + e->offset; | 
|---|
| 537 | bfd_put_32 (output_bfd, e->val, excl_sym + VALOFF); | 
|---|
| 538 | excl_sym[TYPEOFF] = e->type; | 
|---|
| 539 | } | 
|---|
| 540 |  | 
|---|
| 541 | /* Copy over all the stabs symbols, omitting the ones we don't want, | 
|---|
| 542 | and correcting the string indices for those we do want.  */ | 
|---|
| 543 | tosym = contents; | 
|---|
| 544 | symend = contents + stabsec->_raw_size; | 
|---|
| 545 | for (sym = contents, pstridx = secinfo->stridxs; | 
|---|
| 546 | sym < symend; | 
|---|
| 547 | sym += STABSIZE, ++pstridx) | 
|---|
| 548 | { | 
|---|
| 549 | if (*pstridx != (bfd_size_type) -1) | 
|---|
| 550 | { | 
|---|
| 551 | if (tosym != sym) | 
|---|
| 552 | memcpy (tosym, sym, STABSIZE); | 
|---|
| 553 | bfd_put_32 (output_bfd, *pstridx, tosym + STRDXOFF); | 
|---|
| 554 |  | 
|---|
| 555 | if (sym[TYPEOFF] == 0) | 
|---|
| 556 | { | 
|---|
| 557 | /* This is the header symbol for the stabs section.  We | 
|---|
| 558 | don't really need one, since we have merged all the | 
|---|
| 559 | input stabs sections into one, but we generate one | 
|---|
| 560 | for the benefit of readers which expect to see one.  */ | 
|---|
| 561 | BFD_ASSERT (sym == contents); | 
|---|
| 562 | bfd_put_32 (output_bfd, _bfd_stringtab_size (sinfo->strings), | 
|---|
| 563 | tosym + VALOFF); | 
|---|
| 564 | bfd_put_16 (output_bfd, | 
|---|
| 565 | stabsec->output_section->_raw_size / STABSIZE - 1, | 
|---|
| 566 | tosym + DESCOFF); | 
|---|
| 567 | } | 
|---|
| 568 |  | 
|---|
| 569 | tosym += STABSIZE; | 
|---|
| 570 | } | 
|---|
| 571 | } | 
|---|
| 572 |  | 
|---|
| 573 | BFD_ASSERT ((bfd_size_type) (tosym - contents) == stabsec->_cooked_size); | 
|---|
| 574 |  | 
|---|
| 575 | return bfd_set_section_contents (output_bfd, stabsec->output_section, | 
|---|
| 576 | contents, stabsec->output_offset, | 
|---|
| 577 | stabsec->_cooked_size); | 
|---|
| 578 | } | 
|---|
| 579 |  | 
|---|
| 580 | /* Write out the .stabstr section.  */ | 
|---|
| 581 |  | 
|---|
| 582 | boolean | 
|---|
| 583 | _bfd_write_stab_strings (output_bfd, psinfo) | 
|---|
| 584 | bfd *output_bfd; | 
|---|
| 585 | PTR *psinfo; | 
|---|
| 586 | { | 
|---|
| 587 | struct stab_info *sinfo; | 
|---|
| 588 |  | 
|---|
| 589 | sinfo = (struct stab_info *) *psinfo; | 
|---|
| 590 |  | 
|---|
| 591 | if (sinfo == NULL) | 
|---|
| 592 | return true; | 
|---|
| 593 |  | 
|---|
| 594 | if (bfd_is_abs_section (sinfo->stabstr->output_section)) | 
|---|
| 595 | { | 
|---|
| 596 | /* The section was discarded from the link.  */ | 
|---|
| 597 | return true; | 
|---|
| 598 | } | 
|---|
| 599 |  | 
|---|
| 600 | BFD_ASSERT ((sinfo->stabstr->output_offset | 
|---|
| 601 | + _bfd_stringtab_size (sinfo->strings)) | 
|---|
| 602 | <= sinfo->stabstr->output_section->_raw_size); | 
|---|
| 603 |  | 
|---|
| 604 | if (bfd_seek (output_bfd, | 
|---|
| 605 | (sinfo->stabstr->output_section->filepos | 
|---|
| 606 | + sinfo->stabstr->output_offset), | 
|---|
| 607 | SEEK_SET) != 0) | 
|---|
| 608 | return false; | 
|---|
| 609 |  | 
|---|
| 610 | if (! _bfd_stringtab_emit (output_bfd, sinfo->strings)) | 
|---|
| 611 | return false; | 
|---|
| 612 |  | 
|---|
| 613 | /* We no longer need the stabs information.  */ | 
|---|
| 614 | _bfd_stringtab_free (sinfo->strings); | 
|---|
| 615 | bfd_hash_table_free (&sinfo->includes.root); | 
|---|
| 616 |  | 
|---|
| 617 | return true; | 
|---|
| 618 | } | 
|---|
| 619 |  | 
|---|
| 620 | /* Adjust an address in the .stab section.  Given OFFSET within | 
|---|
| 621 | STABSEC, this returns the new offset in the adjusted stab section, | 
|---|
| 622 | or -1 if the address refers to a stab which has been removed.  */ | 
|---|
| 623 |  | 
|---|
| 624 | bfd_vma | 
|---|
| 625 | _bfd_stab_section_offset (output_bfd, psinfo, stabsec, psecinfo, offset) | 
|---|
| 626 | bfd *output_bfd ATTRIBUTE_UNUSED; | 
|---|
| 627 | PTR *psinfo ATTRIBUTE_UNUSED; | 
|---|
| 628 | asection *stabsec; | 
|---|
| 629 | PTR *psecinfo; | 
|---|
| 630 | bfd_vma offset; | 
|---|
| 631 | { | 
|---|
| 632 | struct stab_section_info *secinfo; | 
|---|
| 633 |  | 
|---|
| 634 | secinfo = (struct stab_section_info *) *psecinfo; | 
|---|
| 635 |  | 
|---|
| 636 | if (secinfo == NULL) | 
|---|
| 637 | return offset; | 
|---|
| 638 |  | 
|---|
| 639 | if (offset >= stabsec->_raw_size) | 
|---|
| 640 | return offset - (stabsec->_cooked_size - stabsec->_raw_size); | 
|---|
| 641 |  | 
|---|
| 642 | if (secinfo->cumulative_skips) | 
|---|
| 643 | { | 
|---|
| 644 | bfd_vma i; | 
|---|
| 645 |  | 
|---|
| 646 | i = offset / STABSIZE; | 
|---|
| 647 |  | 
|---|
| 648 | if (secinfo->stridxs [i] == (bfd_size_type) -1) | 
|---|
| 649 | return (bfd_vma) -1; | 
|---|
| 650 |  | 
|---|
| 651 | return offset - secinfo->cumulative_skips [i]; | 
|---|
| 652 | } | 
|---|
| 653 |  | 
|---|
| 654 | return offset; | 
|---|
| 655 | } | 
|---|