1 /* Support for the generic parts of COFF, for BFD. 2 Copyright (C) 1990-2022 Free Software Foundation, Inc. 3 Written by 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 3 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., 51 Franklin Street - Fifth Floor, Boston, 20 MA 02110-1301, USA. */ 21 22 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com. 23 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */ 24 25 /* This file contains COFF code that is not dependent on any 26 particular COFF target. There is only one version of this file in 27 libbfd.a, so no target specific code may be put in here. Or, to 28 put it another way, 29 30 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE ********** 31 32 If you need to add some target specific behaviour, add a new hook 33 function to bfd_coff_backend_data. 34 35 Some of these functions are also called by the ECOFF routines. 36 Those functions may not use any COFF specific information, such as 37 coff_data (abfd). */ 38 39 #include "sysdep.h" 40 #include <limits.h> 41 #include "bfd.h" 42 #include "libbfd.h" 43 #include "coff/internal.h" 44 #include "libcoff.h" 45 46 /* Take a section header read from a coff file (in HOST byte order), 47 and make a BFD "section" out of it. This is used by ECOFF. */ 48 49 static bool 50 make_a_section_from_file (bfd *abfd, 51 struct internal_scnhdr *hdr, 52 unsigned int target_index) 53 { 54 asection *newsect; 55 char *name; 56 bool result = true; 57 flagword flags; 58 59 name = NULL; 60 61 /* Handle long section names as in PE. On reading, we want to 62 accept long names if the format permits them at all, regardless 63 of the current state of the flag that dictates if we would generate 64 them in outputs; this construct checks if that is the case by 65 attempting to set the flag, without changing its state; the call 66 will fail for formats that do not support long names at all. */ 67 if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd)) 68 && hdr->s_name[0] == '/') 69 { 70 char buf[SCNNMLEN]; 71 long strindex; 72 char *p; 73 const char *strings; 74 75 /* Flag that this BFD uses long names, even though the format might 76 expect them to be off by default. This won't directly affect the 77 format of any output BFD created from this one, but the information 78 can be used to decide what to do. */ 79 bfd_coff_set_long_section_names (abfd, true); 80 memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1); 81 buf[SCNNMLEN - 1] = '\0'; 82 strindex = strtol (buf, &p, 10); 83 if (*p == '\0' && strindex >= 0) 84 { 85 strings = _bfd_coff_read_string_table (abfd); 86 if (strings == NULL) 87 return false; 88 if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd)) 89 return false; 90 strings += strindex; 91 name = (char *) bfd_alloc (abfd, 92 (bfd_size_type) strlen (strings) + 1 + 1); 93 if (name == NULL) 94 return false; 95 strcpy (name, strings); 96 } 97 } 98 99 if (name == NULL) 100 { 101 /* Assorted wastage to null-terminate the name, thanks AT&T! */ 102 name = (char *) bfd_alloc (abfd, 103 (bfd_size_type) sizeof (hdr->s_name) + 1 + 1); 104 if (name == NULL) 105 return false; 106 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name)); 107 name[sizeof (hdr->s_name)] = 0; 108 } 109 110 newsect = bfd_make_section_anyway (abfd, name); 111 if (newsect == NULL) 112 return false; 113 114 newsect->vma = hdr->s_vaddr; 115 newsect->lma = hdr->s_paddr; 116 newsect->size = hdr->s_size; 117 newsect->filepos = hdr->s_scnptr; 118 newsect->rel_filepos = hdr->s_relptr; 119 newsect->reloc_count = hdr->s_nreloc; 120 121 bfd_coff_set_alignment_hook (abfd, newsect, hdr); 122 123 newsect->line_filepos = hdr->s_lnnoptr; 124 125 newsect->lineno_count = hdr->s_nlnno; 126 newsect->userdata = NULL; 127 newsect->next = NULL; 128 newsect->target_index = target_index; 129 130 if (!bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, newsect, &flags)) 131 result = false; 132 133 /* At least on i386-coff, the line number count for a shared library 134 section must be ignored. */ 135 if ((flags & SEC_COFF_SHARED_LIBRARY) != 0) 136 newsect->lineno_count = 0; 137 138 if (hdr->s_nreloc != 0) 139 flags |= SEC_RELOC; 140 /* FIXME: should this check 'hdr->s_size > 0'. */ 141 if (hdr->s_scnptr != 0) 142 flags |= SEC_HAS_CONTENTS; 143 144 newsect->flags = flags; 145 146 /* Compress/decompress DWARF debug sections. */ 147 if ((flags & SEC_DEBUGGING) != 0 148 && (flags & SEC_HAS_CONTENTS) != 0 149 && (startswith (name, ".debug_") 150 || startswith (name, ".zdebug_") 151 || startswith (name, ".gnu.debuglto_.debug_") 152 || startswith (name, ".gnu.linkonce.wi."))) 153 { 154 enum { nothing, compress, decompress } action = nothing; 155 156 if (bfd_is_section_compressed (abfd, newsect)) 157 { 158 /* Compressed section. Check if we should decompress. */ 159 if ((abfd->flags & BFD_DECOMPRESS)) 160 action = decompress; 161 } 162 else 163 { 164 /* Normal section. Check if we should compress. */ 165 if ((abfd->flags & BFD_COMPRESS) && newsect->size != 0) 166 action = compress; 167 } 168 169 if (action == compress) 170 { 171 if (!bfd_init_section_compress_status (abfd, newsect)) 172 { 173 _bfd_error_handler 174 /* xgettext:c-format */ 175 (_("%pB: unable to compress section %s"), abfd, name); 176 return false; 177 } 178 } 179 else if (action == decompress) 180 { 181 if (!bfd_init_section_decompress_status (abfd, newsect)) 182 { 183 _bfd_error_handler 184 /* xgettext:c-format */ 185 (_("%pB: unable to decompress section %s"), abfd, name); 186 return false; 187 } 188 if (abfd->is_linker_input 189 && name[1] == 'z') 190 { 191 /* Rename section from .zdebug_* to .debug_* so that ld 192 scripts will see this section as a debug section. */ 193 char *new_name = bfd_zdebug_name_to_debug (abfd, name); 194 if (new_name == NULL) 195 return false; 196 bfd_rename_section (newsect, new_name); 197 } 198 } 199 } 200 201 return result; 202 } 203 204 /* Read in a COFF object and make it into a BFD. This is used by 205 ECOFF as well. */ 206 bfd_cleanup 207 coff_real_object_p (bfd *, 208 unsigned, 209 struct internal_filehdr *, 210 struct internal_aouthdr *); 211 bfd_cleanup 212 coff_real_object_p (bfd *abfd, 213 unsigned nscns, 214 struct internal_filehdr *internal_f, 215 struct internal_aouthdr *internal_a) 216 { 217 flagword oflags = abfd->flags; 218 bfd_vma ostart = bfd_get_start_address (abfd); 219 void * tdata; 220 void * tdata_save; 221 bfd_size_type readsize; /* Length of file_info. */ 222 unsigned int scnhsz; 223 char *external_sections; 224 225 if (!(internal_f->f_flags & F_RELFLG)) 226 abfd->flags |= HAS_RELOC; 227 if ((internal_f->f_flags & F_EXEC)) 228 abfd->flags |= EXEC_P; 229 if (!(internal_f->f_flags & F_LNNO)) 230 abfd->flags |= HAS_LINENO; 231 if (!(internal_f->f_flags & F_LSYMS)) 232 abfd->flags |= HAS_LOCALS; 233 234 /* FIXME: How can we set D_PAGED correctly? */ 235 if ((internal_f->f_flags & F_EXEC) != 0) 236 abfd->flags |= D_PAGED; 237 238 abfd->symcount = internal_f->f_nsyms; 239 if (internal_f->f_nsyms) 240 abfd->flags |= HAS_SYMS; 241 242 if (internal_a != (struct internal_aouthdr *) NULL) 243 abfd->start_address = internal_a->entry; 244 else 245 abfd->start_address = 0; 246 247 /* Set up the tdata area. ECOFF uses its own routine, and overrides 248 abfd->flags. */ 249 tdata_save = abfd->tdata.any; 250 tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a); 251 if (tdata == NULL) 252 goto fail2; 253 254 scnhsz = bfd_coff_scnhsz (abfd); 255 readsize = (bfd_size_type) nscns * scnhsz; 256 external_sections = (char *) _bfd_alloc_and_read (abfd, readsize, readsize); 257 if (!external_sections) 258 goto fail; 259 260 /* Set the arch/mach *before* swapping in sections; section header swapping 261 may depend on arch/mach info. */ 262 if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f)) 263 goto fail; 264 265 /* Now copy data as required; construct all asections etc. */ 266 if (nscns != 0) 267 { 268 unsigned int i; 269 for (i = 0; i < nscns; i++) 270 { 271 struct internal_scnhdr tmp; 272 bfd_coff_swap_scnhdr_in (abfd, 273 (void *) (external_sections + i * scnhsz), 274 (void *) & tmp); 275 if (! make_a_section_from_file (abfd, &tmp, i + 1)) 276 goto fail; 277 } 278 } 279 280 _bfd_coff_free_symbols (abfd); 281 return _bfd_no_cleanup; 282 283 fail: 284 _bfd_coff_free_symbols (abfd); 285 bfd_release (abfd, tdata); 286 fail2: 287 abfd->tdata.any = tdata_save; 288 abfd->flags = oflags; 289 abfd->start_address = ostart; 290 return NULL; 291 } 292 293 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is 294 not a COFF file. This is also used by ECOFF. */ 295 296 bfd_cleanup 297 coff_object_p (bfd *abfd) 298 { 299 bfd_size_type filhsz; 300 bfd_size_type aoutsz; 301 unsigned int nscns; 302 void * filehdr; 303 struct internal_filehdr internal_f; 304 struct internal_aouthdr internal_a; 305 306 /* Figure out how much to read. */ 307 filhsz = bfd_coff_filhsz (abfd); 308 aoutsz = bfd_coff_aoutsz (abfd); 309 310 filehdr = _bfd_alloc_and_read (abfd, filhsz, filhsz); 311 if (filehdr == NULL) 312 { 313 if (bfd_get_error () != bfd_error_system_call) 314 bfd_set_error (bfd_error_wrong_format); 315 return NULL; 316 } 317 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f); 318 bfd_release (abfd, filehdr); 319 320 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ 321 (less than aoutsz) used in object files and AOUTSZ (equal to 322 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function 323 expects this header to be aoutsz bytes in length, so we use that 324 value in the call to bfd_alloc below. But we must be careful to 325 only read in f_opthdr bytes in the call to bfd_bread. We should 326 also attempt to catch corrupt or non-COFF binaries with a strange 327 value for f_opthdr. */ 328 if (! bfd_coff_bad_format_hook (abfd, &internal_f) 329 || internal_f.f_opthdr > aoutsz) 330 { 331 bfd_set_error (bfd_error_wrong_format); 332 return NULL; 333 } 334 nscns = internal_f.f_nscns; 335 336 if (internal_f.f_opthdr) 337 { 338 void * opthdr; 339 340 opthdr = _bfd_alloc_and_read (abfd, aoutsz, internal_f.f_opthdr); 341 if (opthdr == NULL) 342 return NULL; 343 /* PR 17512: file: 11056-1136-0.004. */ 344 if (internal_f.f_opthdr < aoutsz) 345 memset (((char *) opthdr) + internal_f.f_opthdr, 0, 346 aoutsz - internal_f.f_opthdr); 347 348 bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a); 349 bfd_release (abfd, opthdr); 350 } 351 352 return coff_real_object_p (abfd, nscns, &internal_f, 353 (internal_f.f_opthdr != 0 354 ? &internal_a 355 : (struct internal_aouthdr *) NULL)); 356 } 357 358 /* Get the BFD section from a COFF symbol section number. */ 359 360 asection * 361 coff_section_from_bfd_index (bfd *abfd, int section_index) 362 { 363 struct bfd_section *answer = abfd->sections; 364 365 if (section_index == N_ABS) 366 return bfd_abs_section_ptr; 367 if (section_index == N_UNDEF) 368 return bfd_und_section_ptr; 369 if (section_index == N_DEBUG) 370 return bfd_abs_section_ptr; 371 372 while (answer) 373 { 374 if (answer->target_index == section_index) 375 return answer; 376 answer = answer->next; 377 } 378 379 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a 380 has a bad symbol table in biglitpow.o. */ 381 return bfd_und_section_ptr; 382 } 383 384 /* Get the upper bound of a COFF symbol table. */ 385 386 long 387 coff_get_symtab_upper_bound (bfd *abfd) 388 { 389 if (!bfd_coff_slurp_symbol_table (abfd)) 390 return -1; 391 392 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *)); 393 } 394 395 /* Canonicalize a COFF symbol table. */ 396 397 long 398 coff_canonicalize_symtab (bfd *abfd, asymbol **alocation) 399 { 400 unsigned int counter; 401 coff_symbol_type *symbase; 402 coff_symbol_type **location = (coff_symbol_type **) alocation; 403 404 if (!bfd_coff_slurp_symbol_table (abfd)) 405 return -1; 406 407 symbase = obj_symbols (abfd); 408 counter = bfd_get_symcount (abfd); 409 while (counter-- > 0) 410 *location++ = symbase++; 411 412 *location = NULL; 413 414 return bfd_get_symcount (abfd); 415 } 416 417 /* Get the name of a symbol. The caller must pass in a buffer of size 418 >= SYMNMLEN + 1. */ 419 420 const char * 421 _bfd_coff_internal_syment_name (bfd *abfd, 422 const struct internal_syment *sym, 423 char *buf) 424 { 425 /* FIXME: It's not clear this will work correctly if sizeof 426 (_n_zeroes) != 4. */ 427 if (sym->_n._n_n._n_zeroes != 0 428 || sym->_n._n_n._n_offset == 0) 429 { 430 memcpy (buf, sym->_n._n_name, SYMNMLEN); 431 buf[SYMNMLEN] = '\0'; 432 return buf; 433 } 434 else 435 { 436 const char *strings; 437 438 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE); 439 strings = obj_coff_strings (abfd); 440 if (strings == NULL) 441 { 442 strings = _bfd_coff_read_string_table (abfd); 443 if (strings == NULL) 444 return NULL; 445 } 446 /* PR 17910: Only check for string overflow if the length has been set. 447 Some DLLs, eg those produced by Visual Studio, may not set the length field. */ 448 if (obj_coff_strings_len (abfd) > 0 449 && sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd)) 450 return NULL; 451 return strings + sym->_n._n_n._n_offset; 452 } 453 } 454 455 /* Read in and swap the relocs. This returns a buffer holding the 456 relocs for section SEC in file ABFD. If CACHE is TRUE and 457 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case 458 the function is called again. If EXTERNAL_RELOCS is not NULL, it 459 is a buffer large enough to hold the unswapped relocs. If 460 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold 461 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return 462 value must be INTERNAL_RELOCS. The function returns NULL on error. */ 463 464 struct internal_reloc * 465 _bfd_coff_read_internal_relocs (bfd *abfd, 466 asection *sec, 467 bool cache, 468 bfd_byte *external_relocs, 469 bool require_internal, 470 struct internal_reloc *internal_relocs) 471 { 472 bfd_size_type relsz; 473 bfd_byte *free_external = NULL; 474 struct internal_reloc *free_internal = NULL; 475 bfd_byte *erel; 476 bfd_byte *erel_end; 477 struct internal_reloc *irel; 478 bfd_size_type amt; 479 480 if (sec->reloc_count == 0) 481 return internal_relocs; /* Nothing to do. */ 482 483 if (coff_section_data (abfd, sec) != NULL 484 && coff_section_data (abfd, sec)->relocs != NULL) 485 { 486 if (! require_internal) 487 return coff_section_data (abfd, sec)->relocs; 488 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs, 489 sec->reloc_count * sizeof (struct internal_reloc)); 490 return internal_relocs; 491 } 492 493 relsz = bfd_coff_relsz (abfd); 494 495 amt = sec->reloc_count * relsz; 496 if (external_relocs == NULL) 497 { 498 free_external = (bfd_byte *) bfd_malloc (amt); 499 if (free_external == NULL) 500 goto error_return; 501 external_relocs = free_external; 502 } 503 504 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0 505 || bfd_bread (external_relocs, amt, abfd) != amt) 506 goto error_return; 507 508 if (internal_relocs == NULL) 509 { 510 amt = sec->reloc_count; 511 amt *= sizeof (struct internal_reloc); 512 free_internal = (struct internal_reloc *) bfd_malloc (amt); 513 if (free_internal == NULL) 514 goto error_return; 515 internal_relocs = free_internal; 516 } 517 518 /* Swap in the relocs. */ 519 erel = external_relocs; 520 erel_end = erel + relsz * sec->reloc_count; 521 irel = internal_relocs; 522 for (; erel < erel_end; erel += relsz, irel++) 523 bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel); 524 525 free (free_external); 526 free_external = NULL; 527 528 if (cache && free_internal != NULL) 529 { 530 if (coff_section_data (abfd, sec) == NULL) 531 { 532 amt = sizeof (struct coff_section_tdata); 533 sec->used_by_bfd = bfd_zalloc (abfd, amt); 534 if (sec->used_by_bfd == NULL) 535 goto error_return; 536 coff_section_data (abfd, sec)->contents = NULL; 537 } 538 coff_section_data (abfd, sec)->relocs = free_internal; 539 } 540 541 return internal_relocs; 542 543 error_return: 544 free (free_external); 545 free (free_internal); 546 return NULL; 547 } 548 549 /* Set lineno_count for the output sections of a COFF file. */ 550 551 int 552 coff_count_linenumbers (bfd *abfd) 553 { 554 unsigned int limit = bfd_get_symcount (abfd); 555 unsigned int i; 556 int total = 0; 557 asymbol **p; 558 asection *s; 559 560 if (limit == 0) 561 { 562 /* This may be from the backend linker, in which case the 563 lineno_count in the sections is correct. */ 564 for (s = abfd->sections; s != NULL; s = s->next) 565 total += s->lineno_count; 566 return total; 567 } 568 569 for (s = abfd->sections; s != NULL; s = s->next) 570 BFD_ASSERT (s->lineno_count == 0); 571 572 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) 573 { 574 asymbol *q_maybe = *p; 575 576 if (bfd_asymbol_bfd (q_maybe) != NULL 577 && bfd_family_coff (bfd_asymbol_bfd (q_maybe))) 578 { 579 coff_symbol_type *q = coffsymbol (q_maybe); 580 581 /* The AIX 4.1 compiler can sometimes generate line numbers 582 attached to debugging symbols. We try to simply ignore 583 those here. */ 584 if (q->lineno != NULL 585 && q->symbol.section->owner != NULL) 586 { 587 /* This symbol has line numbers. Increment the owning 588 section's linenumber count. */ 589 alent *l = q->lineno; 590 591 do 592 { 593 asection * sec = q->symbol.section->output_section; 594 595 /* Do not try to update fields in read-only sections. */ 596 if (! bfd_is_const_section (sec)) 597 sec->lineno_count ++; 598 599 ++total; 600 ++l; 601 } 602 while (l->line_number != 0); 603 } 604 } 605 } 606 607 return total; 608 } 609 610 static void 611 fixup_symbol_value (bfd *abfd, 612 coff_symbol_type *coff_symbol_ptr, 613 struct internal_syment *syment) 614 { 615 /* Normalize the symbol flags. */ 616 if (coff_symbol_ptr->symbol.section 617 && bfd_is_com_section (coff_symbol_ptr->symbol.section)) 618 { 619 /* A common symbol is undefined with a value. */ 620 syment->n_scnum = N_UNDEF; 621 syment->n_value = coff_symbol_ptr->symbol.value; 622 } 623 else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0 624 && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0) 625 { 626 syment->n_value = coff_symbol_ptr->symbol.value; 627 } 628 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section)) 629 { 630 syment->n_scnum = N_UNDEF; 631 syment->n_value = 0; 632 } 633 /* FIXME: Do we need to handle the absolute section here? */ 634 else 635 { 636 if (coff_symbol_ptr->symbol.section) 637 { 638 syment->n_scnum = 639 coff_symbol_ptr->symbol.section->output_section->target_index; 640 641 syment->n_value = (coff_symbol_ptr->symbol.value 642 + coff_symbol_ptr->symbol.section->output_offset); 643 if (! obj_pe (abfd)) 644 { 645 syment->n_value += (syment->n_sclass == C_STATLAB) 646 ? coff_symbol_ptr->symbol.section->output_section->lma 647 : coff_symbol_ptr->symbol.section->output_section->vma; 648 } 649 } 650 else 651 { 652 BFD_ASSERT (0); 653 /* This can happen, but I don't know why yet (steve@cygnus.com) */ 654 syment->n_scnum = N_ABS; 655 syment->n_value = coff_symbol_ptr->symbol.value; 656 } 657 } 658 } 659 660 /* Run through all the symbols in the symbol table and work out what 661 their indexes into the symbol table will be when output. 662 663 Coff requires that each C_FILE symbol points to the next one in the 664 chain, and that the last one points to the first external symbol. We 665 do that here too. */ 666 667 bool 668 coff_renumber_symbols (bfd *bfd_ptr, int *first_undef) 669 { 670 unsigned int symbol_count = bfd_get_symcount (bfd_ptr); 671 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; 672 unsigned int native_index = 0; 673 struct internal_syment *last_file = NULL; 674 unsigned int symbol_index; 675 676 /* COFF demands that undefined symbols come after all other symbols. 677 Since we don't need to impose this extra knowledge on all our 678 client programs, deal with that here. Sort the symbol table; 679 just move the undefined symbols to the end, leaving the rest 680 alone. The O'Reilly book says that defined global symbols come 681 at the end before the undefined symbols, so we do that here as 682 well. */ 683 /* @@ Do we have some condition we could test for, so we don't always 684 have to do this? I don't think relocatability is quite right, but 685 I'm not certain. [raeburn:19920508.1711EST] */ 686 { 687 asymbol **newsyms; 688 unsigned int i; 689 bfd_size_type amt; 690 691 amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1); 692 newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt); 693 if (!newsyms) 694 return false; 695 bfd_ptr->outsymbols = newsyms; 696 for (i = 0; i < symbol_count; i++) 697 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0 698 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section) 699 && !bfd_is_com_section (symbol_ptr_ptr[i]->section) 700 && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0 701 || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) 702 == 0)))) 703 *newsyms++ = symbol_ptr_ptr[i]; 704 705 for (i = 0; i < symbol_count; i++) 706 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 707 && !bfd_is_und_section (symbol_ptr_ptr[i]->section) 708 && (bfd_is_com_section (symbol_ptr_ptr[i]->section) 709 || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0 710 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK)) 711 != 0)))) 712 *newsyms++ = symbol_ptr_ptr[i]; 713 714 *first_undef = newsyms - bfd_ptr->outsymbols; 715 716 for (i = 0; i < symbol_count; i++) 717 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0 718 && bfd_is_und_section (symbol_ptr_ptr[i]->section)) 719 *newsyms++ = symbol_ptr_ptr[i]; 720 *newsyms = (asymbol *) NULL; 721 symbol_ptr_ptr = bfd_ptr->outsymbols; 722 } 723 724 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) 725 { 726 coff_symbol_type *coff_symbol_ptr; 727 728 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]); 729 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index; 730 if (coff_symbol_ptr && coff_symbol_ptr->native) 731 { 732 combined_entry_type *s = coff_symbol_ptr->native; 733 int i; 734 735 BFD_ASSERT (s->is_sym); 736 if (s->u.syment.n_sclass == C_FILE) 737 { 738 if (last_file != NULL) 739 last_file->n_value = native_index; 740 last_file = &(s->u.syment); 741 } 742 else 743 /* Modify the symbol values according to their section and 744 type. */ 745 fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment)); 746 747 for (i = 0; i < s->u.syment.n_numaux + 1; i++) 748 s[i].offset = native_index++; 749 } 750 else 751 native_index++; 752 } 753 754 obj_conv_table_size (bfd_ptr) = native_index; 755 756 return true; 757 } 758 759 /* Run thorough the symbol table again, and fix it so that all 760 pointers to entries are changed to the entries' index in the output 761 symbol table. */ 762 763 void 764 coff_mangle_symbols (bfd *bfd_ptr) 765 { 766 unsigned int symbol_count = bfd_get_symcount (bfd_ptr); 767 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols; 768 unsigned int symbol_index; 769 770 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++) 771 { 772 coff_symbol_type *coff_symbol_ptr; 773 774 coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]); 775 if (coff_symbol_ptr && coff_symbol_ptr->native) 776 { 777 int i; 778 combined_entry_type *s = coff_symbol_ptr->native; 779 780 BFD_ASSERT (s->is_sym); 781 if (s->fix_value) 782 { 783 /* FIXME: We should use a union here. */ 784 s->u.syment.n_value = 785 (uintptr_t) ((combined_entry_type *) 786 (uintptr_t) s->u.syment.n_value)->offset; 787 s->fix_value = 0; 788 } 789 if (s->fix_line) 790 { 791 /* The value is the offset into the line number entries 792 for the symbol's section. On output, the symbol's 793 section should be N_DEBUG. */ 794 s->u.syment.n_value = 795 (coff_symbol_ptr->symbol.section->output_section->line_filepos 796 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr)); 797 coff_symbol_ptr->symbol.section = 798 coff_section_from_bfd_index (bfd_ptr, N_DEBUG); 799 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING); 800 } 801 for (i = 0; i < s->u.syment.n_numaux; i++) 802 { 803 combined_entry_type *a = s + i + 1; 804 805 BFD_ASSERT (! a->is_sym); 806 if (a->fix_tag) 807 { 808 a->u.auxent.x_sym.x_tagndx.l = 809 a->u.auxent.x_sym.x_tagndx.p->offset; 810 a->fix_tag = 0; 811 } 812 if (a->fix_end) 813 { 814 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l = 815 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset; 816 a->fix_end = 0; 817 } 818 if (a->fix_scnlen) 819 { 820 a->u.auxent.x_csect.x_scnlen.l = 821 a->u.auxent.x_csect.x_scnlen.p->offset; 822 a->fix_scnlen = 0; 823 } 824 } 825 } 826 } 827 } 828 829 static bool 830 coff_write_auxent_fname (bfd *abfd, 831 char *str, 832 union internal_auxent *auxent, 833 struct bfd_strtab_hash *strtab, 834 bool hash) 835 { 836 unsigned int str_length = strlen (str); 837 unsigned int filnmlen = bfd_coff_filnmlen (abfd); 838 839 if (bfd_coff_long_filenames (abfd)) 840 { 841 if (str_length <= filnmlen) 842 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen); 843 else 844 { 845 bfd_size_type indx = _bfd_stringtab_add (strtab, str, hash, false); 846 847 if (indx == (bfd_size_type) -1) 848 return false; 849 850 auxent->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx; 851 auxent->x_file.x_n.x_n.x_zeroes = 0; 852 } 853 } 854 else 855 { 856 strncpy (auxent->x_file.x_n.x_fname, str, filnmlen); 857 if (str_length > filnmlen) 858 str[filnmlen] = '\0'; 859 } 860 861 return true; 862 } 863 864 static bool 865 coff_fix_symbol_name (bfd *abfd, 866 asymbol *symbol, 867 combined_entry_type *native, 868 struct bfd_strtab_hash *strtab, 869 bool hash, 870 asection **debug_string_section_p, 871 bfd_size_type *debug_string_size_p) 872 { 873 unsigned int name_length; 874 char *name = (char *) (symbol->name); 875 bfd_size_type indx; 876 877 if (name == NULL) 878 { 879 /* COFF symbols always have names, so we'll make one up. */ 880 symbol->name = "strange"; 881 name = (char *) symbol->name; 882 } 883 name_length = strlen (name); 884 885 BFD_ASSERT (native->is_sym); 886 if (native->u.syment.n_sclass == C_FILE 887 && native->u.syment.n_numaux > 0) 888 { 889 if (bfd_coff_force_symnames_in_strings (abfd)) 890 { 891 indx = _bfd_stringtab_add (strtab, ".file", hash, false); 892 if (indx == (bfd_size_type) -1) 893 return false; 894 895 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 896 native->u.syment._n._n_n._n_zeroes = 0; 897 } 898 else 899 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN); 900 901 BFD_ASSERT (! (native + 1)->is_sym); 902 if (!coff_write_auxent_fname (abfd, name, &(native + 1)->u.auxent, 903 strtab, hash)) 904 return false; 905 } 906 else 907 { 908 if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd)) 909 /* This name will fit into the symbol neatly. */ 910 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN); 911 912 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment)) 913 { 914 indx = _bfd_stringtab_add (strtab, name, hash, false); 915 if (indx == (bfd_size_type) -1) 916 return false; 917 918 native->u.syment._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 919 native->u.syment._n._n_n._n_zeroes = 0; 920 } 921 else 922 { 923 file_ptr filepos; 924 bfd_byte buf[4]; 925 int prefix_len = bfd_coff_debug_string_prefix_length (abfd); 926 927 /* This name should be written into the .debug section. For 928 some reason each name is preceded by a two byte length 929 and also followed by a null byte. FIXME: We assume that 930 the .debug section has already been created, and that it 931 is large enough. */ 932 if (*debug_string_section_p == (asection *) NULL) 933 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug"); 934 filepos = bfd_tell (abfd); 935 if (prefix_len == 4) 936 bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf); 937 else 938 bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf); 939 940 if (!bfd_set_section_contents (abfd, 941 *debug_string_section_p, 942 (void *) buf, 943 (file_ptr) *debug_string_size_p, 944 (bfd_size_type) prefix_len) 945 || !bfd_set_section_contents (abfd, 946 *debug_string_section_p, 947 (void *) symbol->name, 948 (file_ptr) (*debug_string_size_p 949 + prefix_len), 950 (bfd_size_type) name_length + 1)) 951 abort (); 952 if (bfd_seek (abfd, filepos, SEEK_SET) != 0) 953 abort (); 954 native->u.syment._n._n_n._n_offset = 955 *debug_string_size_p + prefix_len; 956 native->u.syment._n._n_n._n_zeroes = 0; 957 *debug_string_size_p += name_length + 1 + prefix_len; 958 } 959 } 960 961 return true; 962 } 963 964 /* We need to keep track of the symbol index so that when we write out 965 the relocs we can get the index for a symbol. This method is a 966 hack. FIXME. */ 967 968 #define set_index(symbol, idx) ((symbol)->udata.i = (idx)) 969 970 /* Write a symbol out to a COFF file. */ 971 972 static bool 973 coff_write_symbol (bfd *abfd, 974 asymbol *symbol, 975 combined_entry_type *native, 976 bfd_vma *written, 977 struct bfd_strtab_hash *strtab, 978 bool hash, 979 asection **debug_string_section_p, 980 bfd_size_type *debug_string_size_p) 981 { 982 unsigned int numaux = native->u.syment.n_numaux; 983 int type = native->u.syment.n_type; 984 int n_sclass = (int) native->u.syment.n_sclass; 985 asection *output_section = symbol->section->output_section 986 ? symbol->section->output_section 987 : symbol->section; 988 void * buf; 989 bfd_size_type symesz; 990 991 BFD_ASSERT (native->is_sym); 992 993 if (native->u.syment.n_sclass == C_FILE) 994 symbol->flags |= BSF_DEBUGGING; 995 996 if (symbol->flags & BSF_DEBUGGING 997 && bfd_is_abs_section (symbol->section)) 998 native->u.syment.n_scnum = N_DEBUG; 999 1000 else if (bfd_is_abs_section (symbol->section)) 1001 native->u.syment.n_scnum = N_ABS; 1002 1003 else if (bfd_is_und_section (symbol->section)) 1004 native->u.syment.n_scnum = N_UNDEF; 1005 1006 else 1007 native->u.syment.n_scnum = 1008 output_section->target_index; 1009 1010 if (!coff_fix_symbol_name (abfd, symbol, native, strtab, hash, 1011 debug_string_section_p, debug_string_size_p)) 1012 return false; 1013 1014 symesz = bfd_coff_symesz (abfd); 1015 buf = bfd_alloc (abfd, symesz); 1016 if (!buf) 1017 return false; 1018 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf); 1019 if (bfd_bwrite (buf, symesz, abfd) != symesz) 1020 return false; 1021 bfd_release (abfd, buf); 1022 1023 if (native->u.syment.n_numaux > 0) 1024 { 1025 bfd_size_type auxesz; 1026 unsigned int j; 1027 1028 auxesz = bfd_coff_auxesz (abfd); 1029 buf = bfd_alloc (abfd, auxesz); 1030 if (!buf) 1031 return false; 1032 for (j = 0; j < native->u.syment.n_numaux; j++) 1033 { 1034 BFD_ASSERT (! (native + j + 1)->is_sym); 1035 1036 /* Adjust auxent only if this isn't the filename 1037 auxiliary entry. */ 1038 if (native->u.syment.n_sclass == C_FILE 1039 && (native + j + 1)->u.auxent.x_file.x_ftype 1040 && (native + j + 1)->extrap) 1041 coff_write_auxent_fname (abfd, (char *) (native + j + 1)->extrap, 1042 &(native + j + 1)->u.auxent, strtab, hash); 1043 1044 bfd_coff_swap_aux_out (abfd, 1045 &((native + j + 1)->u.auxent), 1046 type, n_sclass, (int) j, 1047 native->u.syment.n_numaux, 1048 buf); 1049 if (bfd_bwrite (buf, auxesz, abfd) != auxesz) 1050 return false; 1051 } 1052 bfd_release (abfd, buf); 1053 } 1054 1055 /* Store the index for use when we write out the relocs. */ 1056 set_index (symbol, *written); 1057 1058 *written += numaux + 1; 1059 return true; 1060 } 1061 1062 /* Write out a symbol to a COFF file that does not come from a COFF 1063 file originally. This symbol may have been created by the linker, 1064 or we may be linking a non COFF file to a COFF file. */ 1065 1066 bool 1067 coff_write_alien_symbol (bfd *abfd, 1068 asymbol *symbol, 1069 struct internal_syment *isym, 1070 bfd_vma *written, 1071 struct bfd_strtab_hash *strtab, 1072 bool hash, 1073 asection **debug_string_section_p, 1074 bfd_size_type *debug_string_size_p) 1075 { 1076 combined_entry_type *native; 1077 combined_entry_type dummy[2]; 1078 asection *output_section = symbol->section->output_section 1079 ? symbol->section->output_section 1080 : symbol->section; 1081 struct bfd_link_info *link_info = coff_data (abfd)->link_info; 1082 bool ret; 1083 1084 if ((!link_info || link_info->strip_discarded) 1085 && !bfd_is_abs_section (symbol->section) 1086 && symbol->section->output_section == bfd_abs_section_ptr) 1087 { 1088 symbol->name = ""; 1089 if (isym != NULL) 1090 memset (isym, 0, sizeof (*isym)); 1091 return true; 1092 } 1093 memset (dummy, 0, sizeof dummy); 1094 native = dummy; 1095 native->is_sym = true; 1096 native[1].is_sym = false; 1097 native->u.syment.n_type = T_NULL; 1098 native->u.syment.n_flags = 0; 1099 native->u.syment.n_numaux = 0; 1100 if (bfd_is_und_section (symbol->section)) 1101 { 1102 native->u.syment.n_scnum = N_UNDEF; 1103 native->u.syment.n_value = symbol->value; 1104 } 1105 else if (bfd_is_com_section (symbol->section)) 1106 { 1107 native->u.syment.n_scnum = N_UNDEF; 1108 native->u.syment.n_value = symbol->value; 1109 } 1110 else if (symbol->flags & BSF_FILE) 1111 { 1112 native->u.syment.n_scnum = N_DEBUG; 1113 native->u.syment.n_numaux = 1; 1114 } 1115 else if (symbol->flags & BSF_DEBUGGING) 1116 { 1117 /* There isn't much point to writing out a debugging symbol 1118 unless we are prepared to convert it into COFF debugging 1119 format. So, we just ignore them. We must clobber the symbol 1120 name to keep it from being put in the string table. */ 1121 symbol->name = ""; 1122 if (isym != NULL) 1123 memset (isym, 0, sizeof (*isym)); 1124 return true; 1125 } 1126 else 1127 { 1128 native->u.syment.n_scnum = output_section->target_index; 1129 native->u.syment.n_value = (symbol->value 1130 + symbol->section->output_offset); 1131 if (! obj_pe (abfd)) 1132 native->u.syment.n_value += output_section->vma; 1133 1134 /* Copy the any flags from the file header into the symbol. 1135 FIXME: Why? */ 1136 { 1137 coff_symbol_type *c = coff_symbol_from (symbol); 1138 if (c != (coff_symbol_type *) NULL) 1139 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags; 1140 } 1141 } 1142 1143 native->u.syment.n_type = 0; 1144 if (symbol->flags & BSF_FILE) 1145 native->u.syment.n_sclass = C_FILE; 1146 else if (symbol->flags & BSF_LOCAL) 1147 native->u.syment.n_sclass = C_STAT; 1148 else if (symbol->flags & BSF_WEAK) 1149 native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; 1150 else 1151 native->u.syment.n_sclass = C_EXT; 1152 1153 ret = coff_write_symbol (abfd, symbol, native, written, strtab, hash, 1154 debug_string_section_p, debug_string_size_p); 1155 if (isym != NULL) 1156 *isym = native->u.syment; 1157 return ret; 1158 } 1159 1160 /* Write a native symbol to a COFF file. */ 1161 1162 static bool 1163 coff_write_native_symbol (bfd *abfd, 1164 coff_symbol_type *symbol, 1165 bfd_vma *written, 1166 struct bfd_strtab_hash *strtab, 1167 asection **debug_string_section_p, 1168 bfd_size_type *debug_string_size_p) 1169 { 1170 combined_entry_type *native = symbol->native; 1171 alent *lineno = symbol->lineno; 1172 struct bfd_link_info *link_info = coff_data (abfd)->link_info; 1173 1174 if ((!link_info || link_info->strip_discarded) 1175 && !bfd_is_abs_section (symbol->symbol.section) 1176 && symbol->symbol.section->output_section == bfd_abs_section_ptr) 1177 { 1178 symbol->symbol.name = ""; 1179 return true; 1180 } 1181 1182 BFD_ASSERT (native->is_sym); 1183 /* If this symbol has an associated line number, we must store the 1184 symbol index in the line number field. We also tag the auxent to 1185 point to the right place in the lineno table. */ 1186 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL) 1187 { 1188 unsigned int count = 0; 1189 1190 lineno[count].u.offset = *written; 1191 if (native->u.syment.n_numaux) 1192 { 1193 union internal_auxent *a = &((native + 1)->u.auxent); 1194 1195 a->x_sym.x_fcnary.x_fcn.x_lnnoptr = 1196 symbol->symbol.section->output_section->moving_line_filepos; 1197 } 1198 1199 /* Count and relocate all other linenumbers. */ 1200 count++; 1201 while (lineno[count].line_number != 0) 1202 { 1203 lineno[count].u.offset += 1204 (symbol->symbol.section->output_section->vma 1205 + symbol->symbol.section->output_offset); 1206 count++; 1207 } 1208 symbol->done_lineno = true; 1209 1210 if (! bfd_is_const_section (symbol->symbol.section->output_section)) 1211 symbol->symbol.section->output_section->moving_line_filepos += 1212 count * bfd_coff_linesz (abfd); 1213 } 1214 1215 return coff_write_symbol (abfd, &(symbol->symbol), native, written, 1216 strtab, true, debug_string_section_p, 1217 debug_string_size_p); 1218 } 1219 1220 static void 1221 null_error_handler (const char *fmt ATTRIBUTE_UNUSED, 1222 va_list ap ATTRIBUTE_UNUSED) 1223 { 1224 } 1225 1226 /* Write out the COFF symbols. */ 1227 1228 bool 1229 coff_write_symbols (bfd *abfd) 1230 { 1231 struct bfd_strtab_hash *strtab; 1232 asection *debug_string_section; 1233 bfd_size_type debug_string_size; 1234 unsigned int i; 1235 unsigned int limit = bfd_get_symcount (abfd); 1236 bfd_vma written = 0; 1237 asymbol **p; 1238 1239 debug_string_section = NULL; 1240 debug_string_size = 0; 1241 1242 strtab = _bfd_stringtab_init (); 1243 if (strtab == NULL) 1244 return false; 1245 1246 /* If this target supports long section names, they must be put into 1247 the string table. This is supported by PE. This code must 1248 handle section names just as they are handled in 1249 coff_write_object_contents. This is why we pass hash as FALSE below. */ 1250 if (bfd_coff_long_section_names (abfd)) 1251 { 1252 asection *o; 1253 1254 for (o = abfd->sections; o != NULL; o = o->next) 1255 if (strlen (o->name) > SCNNMLEN 1256 && _bfd_stringtab_add (strtab, o->name, false, false) 1257 == (bfd_size_type) -1) 1258 return false; 1259 } 1260 1261 /* Seek to the right place. */ 1262 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) 1263 return false; 1264 1265 /* Output all the symbols we have. */ 1266 written = 0; 1267 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++) 1268 { 1269 asymbol *symbol = *p; 1270 coff_symbol_type *c_symbol = coff_symbol_from (symbol); 1271 1272 if (c_symbol == (coff_symbol_type *) NULL 1273 || c_symbol->native == (combined_entry_type *) NULL) 1274 { 1275 if (!coff_write_alien_symbol (abfd, symbol, NULL, &written, 1276 strtab, true, &debug_string_section, 1277 &debug_string_size)) 1278 return false; 1279 } 1280 else 1281 { 1282 if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL) 1283 { 1284 bfd_error_handler_type current_error_handler; 1285 enum coff_symbol_classification sym_class; 1286 unsigned char *n_sclass; 1287 1288 /* Suppress error reporting by bfd_coff_classify_symbol. 1289 Error messages can be generated when we are processing a local 1290 symbol which has no associated section and we do not have to 1291 worry about this, all we need to know is that it is local. */ 1292 current_error_handler = bfd_set_error_handler (null_error_handler); 1293 BFD_ASSERT (c_symbol->native->is_sym); 1294 sym_class = bfd_coff_classify_symbol (abfd, 1295 &c_symbol->native->u.syment); 1296 (void) bfd_set_error_handler (current_error_handler); 1297 1298 n_sclass = &c_symbol->native->u.syment.n_sclass; 1299 1300 /* If the symbol class has been changed (eg objcopy/ld script/etc) 1301 we cannot retain the existing sclass from the original symbol. 1302 Weak symbols only have one valid sclass, so just set it always. 1303 If it is not local class and should be, set it C_STAT. 1304 If it is global and not classified as global, or if it is 1305 weak (which is also classified as global), set it C_EXT. */ 1306 1307 if (symbol->flags & BSF_WEAK) 1308 *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT; 1309 else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL) 1310 *n_sclass = C_STAT; 1311 else if (symbol->flags & BSF_GLOBAL 1312 && (sym_class != COFF_SYMBOL_GLOBAL 1313 #ifdef COFF_WITH_PE 1314 || *n_sclass == C_NT_WEAK 1315 #endif 1316 || *n_sclass == C_WEAKEXT)) 1317 c_symbol->native->u.syment.n_sclass = C_EXT; 1318 } 1319 1320 if (!coff_write_native_symbol (abfd, c_symbol, &written, 1321 strtab, &debug_string_section, 1322 &debug_string_size)) 1323 return false; 1324 } 1325 } 1326 1327 obj_raw_syment_count (abfd) = written; 1328 1329 /* Now write out strings. 1330 1331 We would normally not write anything here if there are no strings, but 1332 we'll write out 4 so that any stupid coff reader which tries to read the 1333 string table even when there isn't one won't croak. */ 1334 { 1335 bfd_byte buffer[STRING_SIZE_SIZE]; 1336 1337 #if STRING_SIZE_SIZE == 4 1338 H_PUT_32 (abfd, _bfd_stringtab_size (strtab) + STRING_SIZE_SIZE, buffer); 1339 #else 1340 #error Change H_PUT_32 1341 #endif 1342 if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd) 1343 != sizeof (buffer)) 1344 return false; 1345 1346 if (! _bfd_stringtab_emit (abfd, strtab)) 1347 return false; 1348 } 1349 1350 _bfd_stringtab_free (strtab); 1351 1352 /* Make sure the .debug section was created to be the correct size. 1353 We should create it ourselves on the fly, but we don't because 1354 BFD won't let us write to any section until we know how large all 1355 the sections are. We could still do it by making another pass 1356 over the symbols. FIXME. */ 1357 BFD_ASSERT (debug_string_size == 0 1358 || (debug_string_section != (asection *) NULL 1359 && (BFD_ALIGN (debug_string_size, 1360 1 << debug_string_section->alignment_power) 1361 == debug_string_section->size))); 1362 1363 return true; 1364 } 1365 1366 bool 1367 coff_write_linenumbers (bfd *abfd) 1368 { 1369 asection *s; 1370 bfd_size_type linesz; 1371 void * buff; 1372 1373 linesz = bfd_coff_linesz (abfd); 1374 buff = bfd_alloc (abfd, linesz); 1375 if (!buff) 1376 return false; 1377 for (s = abfd->sections; s != (asection *) NULL; s = s->next) 1378 { 1379 if (s->lineno_count) 1380 { 1381 asymbol **q = abfd->outsymbols; 1382 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0) 1383 return false; 1384 /* Find all the linenumbers in this section. */ 1385 while (*q) 1386 { 1387 asymbol *p = *q; 1388 if (p->section->output_section == s) 1389 { 1390 alent *l = 1391 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno, 1392 (bfd_asymbol_bfd (p), p)); 1393 if (l) 1394 { 1395 /* Found a linenumber entry, output. */ 1396 struct internal_lineno out; 1397 1398 memset ((void *) & out, 0, sizeof (out)); 1399 out.l_lnno = 0; 1400 out.l_addr.l_symndx = l->u.offset; 1401 bfd_coff_swap_lineno_out (abfd, &out, buff); 1402 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd) 1403 != linesz) 1404 return false; 1405 l++; 1406 while (l->line_number) 1407 { 1408 out.l_lnno = l->line_number; 1409 out.l_addr.l_symndx = l->u.offset; 1410 bfd_coff_swap_lineno_out (abfd, &out, buff); 1411 if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd) 1412 != linesz) 1413 return false; 1414 l++; 1415 } 1416 } 1417 } 1418 q++; 1419 } 1420 } 1421 } 1422 bfd_release (abfd, buff); 1423 return true; 1424 } 1425 1426 alent * 1427 coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol) 1428 { 1429 return coffsymbol (symbol)->lineno; 1430 } 1431 1432 /* This function transforms the offsets into the symbol table into 1433 pointers to syments. */ 1434 1435 static void 1436 coff_pointerize_aux (bfd *abfd, 1437 combined_entry_type *table_base, 1438 combined_entry_type *symbol, 1439 unsigned int indaux, 1440 combined_entry_type *auxent, 1441 combined_entry_type *table_end) 1442 { 1443 unsigned int type = symbol->u.syment.n_type; 1444 unsigned int n_sclass = symbol->u.syment.n_sclass; 1445 1446 BFD_ASSERT (symbol->is_sym); 1447 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) 1448 { 1449 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook) 1450 (abfd, table_base, symbol, indaux, auxent)) 1451 return; 1452 } 1453 1454 /* Don't bother if this is a file or a section. */ 1455 if (n_sclass == C_STAT && type == T_NULL) 1456 return; 1457 if (n_sclass == C_FILE) 1458 return; 1459 if (n_sclass == C_DWARF) 1460 return; 1461 1462 BFD_ASSERT (! auxent->is_sym); 1463 /* Otherwise patch up. */ 1464 #define N_TMASK coff_data (abfd)->local_n_tmask 1465 #define N_BTSHFT coff_data (abfd)->local_n_btshft 1466 1467 if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK 1468 || n_sclass == C_FCN) 1469 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0 1470 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l 1471 < (long) obj_raw_syment_count (abfd) 1472 && table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l 1473 < table_end) 1474 { 1475 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p = 1476 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; 1477 auxent->fix_end = 1; 1478 } 1479 1480 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can 1481 generate one, so we must be careful to ignore it. */ 1482 if ((unsigned long) auxent->u.auxent.x_sym.x_tagndx.l 1483 < obj_raw_syment_count (abfd) 1484 && table_base + auxent->u.auxent.x_sym.x_tagndx.l < table_end) 1485 { 1486 auxent->u.auxent.x_sym.x_tagndx.p = 1487 table_base + auxent->u.auxent.x_sym.x_tagndx.l; 1488 auxent->fix_tag = 1; 1489 } 1490 } 1491 1492 /* Allocate space for the ".debug" section, and read it. 1493 We did not read the debug section until now, because 1494 we didn't want to go to the trouble until someone needed it. */ 1495 1496 static char * 1497 build_debug_section (bfd *abfd, asection ** sect_return) 1498 { 1499 char *debug_section; 1500 file_ptr position; 1501 bfd_size_type sec_size; 1502 1503 asection *sect = bfd_get_section_by_name (abfd, ".debug"); 1504 1505 if (!sect) 1506 { 1507 bfd_set_error (bfd_error_no_debug_section); 1508 return NULL; 1509 } 1510 1511 /* Seek to the beginning of the `.debug' section and read it. 1512 Save the current position first; it is needed by our caller. 1513 Then read debug section and reset the file pointer. */ 1514 1515 position = bfd_tell (abfd); 1516 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0) 1517 return NULL; 1518 1519 sec_size = sect->size; 1520 debug_section = (char *) _bfd_alloc_and_read (abfd, sec_size + 1, sec_size); 1521 if (debug_section == NULL) 1522 return NULL; 1523 debug_section[sec_size] = 0; 1524 1525 if (bfd_seek (abfd, position, SEEK_SET) != 0) 1526 return NULL; 1527 1528 * sect_return = sect; 1529 return debug_section; 1530 } 1531 1532 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be 1533 \0-terminated, but will not exceed 'maxlen' characters. The copy *will* 1534 be \0-terminated. */ 1535 1536 static char * 1537 copy_name (bfd *abfd, char *name, size_t maxlen) 1538 { 1539 size_t len; 1540 char *newname; 1541 1542 for (len = 0; len < maxlen; ++len) 1543 if (name[len] == '\0') 1544 break; 1545 1546 if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL) 1547 return NULL; 1548 1549 strncpy (newname, name, len); 1550 newname[len] = '\0'; 1551 return newname; 1552 } 1553 1554 /* Read in the external symbols. */ 1555 1556 bool 1557 _bfd_coff_get_external_symbols (bfd *abfd) 1558 { 1559 size_t symesz; 1560 size_t size; 1561 void * syms; 1562 1563 if (obj_coff_external_syms (abfd) != NULL) 1564 return true; 1565 1566 symesz = bfd_coff_symesz (abfd); 1567 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size)) 1568 { 1569 bfd_set_error (bfd_error_file_truncated); 1570 return false; 1571 } 1572 1573 if (size == 0) 1574 return true; 1575 1576 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0) 1577 return false; 1578 syms = _bfd_malloc_and_read (abfd, size, size); 1579 obj_coff_external_syms (abfd) = syms; 1580 return syms != NULL; 1581 } 1582 1583 /* Read in the external strings. The strings are not loaded until 1584 they are needed. This is because we have no simple way of 1585 detecting a missing string table in an archive. If the strings 1586 are loaded then the STRINGS and STRINGS_LEN fields in the 1587 coff_tdata structure will be set. */ 1588 1589 const char * 1590 _bfd_coff_read_string_table (bfd *abfd) 1591 { 1592 char extstrsize[STRING_SIZE_SIZE]; 1593 bfd_size_type strsize; 1594 char *strings; 1595 ufile_ptr pos; 1596 ufile_ptr filesize; 1597 size_t symesz; 1598 size_t size; 1599 1600 if (obj_coff_strings (abfd) != NULL) 1601 return obj_coff_strings (abfd); 1602 1603 if (obj_sym_filepos (abfd) == 0) 1604 { 1605 bfd_set_error (bfd_error_no_symbols); 1606 return NULL; 1607 } 1608 1609 symesz = bfd_coff_symesz (abfd); 1610 pos = obj_sym_filepos (abfd); 1611 if (_bfd_mul_overflow (obj_raw_syment_count (abfd), symesz, &size) 1612 || pos + size < pos) 1613 { 1614 bfd_set_error (bfd_error_file_truncated); 1615 return NULL; 1616 } 1617 1618 if (bfd_seek (abfd, pos + size, SEEK_SET) != 0) 1619 return NULL; 1620 1621 if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd) 1622 != sizeof extstrsize) 1623 { 1624 if (bfd_get_error () != bfd_error_file_truncated) 1625 return NULL; 1626 1627 /* There is no string table. */ 1628 strsize = STRING_SIZE_SIZE; 1629 } 1630 else 1631 { 1632 #if STRING_SIZE_SIZE == 4 1633 strsize = H_GET_32 (abfd, extstrsize); 1634 #else 1635 #error Change H_GET_32 1636 #endif 1637 } 1638 1639 filesize = bfd_get_file_size (abfd); 1640 if (strsize < STRING_SIZE_SIZE 1641 || (filesize != 0 && strsize > filesize)) 1642 { 1643 _bfd_error_handler 1644 /* xgettext: c-format */ 1645 (_("%pB: bad string table size %" PRIu64), abfd, (uint64_t) strsize); 1646 bfd_set_error (bfd_error_bad_value); 1647 return NULL; 1648 } 1649 1650 strings = (char *) bfd_malloc (strsize + 1); 1651 if (strings == NULL) 1652 return NULL; 1653 1654 /* PR 17521 file: 079-54929-0.004. 1655 A corrupt file could contain an index that points into the first 1656 STRING_SIZE_SIZE bytes of the string table, so make sure that 1657 they are zero. */ 1658 memset (strings, 0, STRING_SIZE_SIZE); 1659 1660 if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd) 1661 != strsize - STRING_SIZE_SIZE) 1662 { 1663 free (strings); 1664 return NULL; 1665 } 1666 1667 obj_coff_strings (abfd) = strings; 1668 obj_coff_strings_len (abfd) = strsize; 1669 /* Terminate the string table, just in case. */ 1670 strings[strsize] = 0; 1671 return strings; 1672 } 1673 1674 /* Free up the external symbols and strings read from a COFF file. */ 1675 1676 bool 1677 _bfd_coff_free_symbols (bfd *abfd) 1678 { 1679 if (! bfd_family_coff (abfd)) 1680 return false; 1681 1682 if (obj_coff_external_syms (abfd) != NULL 1683 && ! obj_coff_keep_syms (abfd)) 1684 { 1685 free (obj_coff_external_syms (abfd)); 1686 obj_coff_external_syms (abfd) = NULL; 1687 } 1688 1689 if (obj_coff_strings (abfd) != NULL 1690 && ! obj_coff_keep_strings (abfd)) 1691 { 1692 free (obj_coff_strings (abfd)); 1693 obj_coff_strings (abfd) = NULL; 1694 obj_coff_strings_len (abfd) = 0; 1695 } 1696 1697 return true; 1698 } 1699 1700 /* Read a symbol table into freshly bfd_allocated memory, swap it, and 1701 knit the symbol names into a normalized form. By normalized here I 1702 mean that all symbols have an n_offset pointer that points to a null- 1703 terminated string. */ 1704 1705 combined_entry_type * 1706 coff_get_normalized_symtab (bfd *abfd) 1707 { 1708 combined_entry_type *internal; 1709 combined_entry_type *internal_ptr; 1710 combined_entry_type *symbol_ptr; 1711 combined_entry_type *internal_end; 1712 size_t symesz; 1713 char *raw_src; 1714 char *raw_end; 1715 const char *string_table = NULL; 1716 asection * debug_sec = NULL; 1717 char *debug_sec_data = NULL; 1718 bfd_size_type size; 1719 1720 if (obj_raw_syments (abfd) != NULL) 1721 return obj_raw_syments (abfd); 1722 1723 if (! _bfd_coff_get_external_symbols (abfd)) 1724 return NULL; 1725 1726 size = obj_raw_syment_count (abfd); 1727 /* Check for integer overflow. */ 1728 if (size > (bfd_size_type) -1 / sizeof (combined_entry_type)) 1729 return NULL; 1730 size *= sizeof (combined_entry_type); 1731 internal = (combined_entry_type *) bfd_zalloc (abfd, size); 1732 if (internal == NULL && size != 0) 1733 return NULL; 1734 internal_end = internal + obj_raw_syment_count (abfd); 1735 1736 raw_src = (char *) obj_coff_external_syms (abfd); 1737 1738 /* Mark the end of the symbols. */ 1739 symesz = bfd_coff_symesz (abfd); 1740 raw_end = PTR_ADD (raw_src, obj_raw_syment_count (abfd) * symesz); 1741 1742 /* FIXME SOMEDAY. A string table size of zero is very weird, but 1743 probably possible. If one shows up, it will probably kill us. */ 1744 1745 /* Swap all the raw entries. */ 1746 for (internal_ptr = internal; 1747 raw_src < raw_end; 1748 raw_src += symesz, internal_ptr++) 1749 { 1750 unsigned int i; 1751 1752 bfd_coff_swap_sym_in (abfd, (void *) raw_src, 1753 (void *) & internal_ptr->u.syment); 1754 symbol_ptr = internal_ptr; 1755 internal_ptr->is_sym = true; 1756 1757 /* PR 17512: Prevent buffer overrun. */ 1758 if (symbol_ptr->u.syment.n_numaux > ((raw_end - 1) - raw_src) / symesz) 1759 { 1760 bfd_release (abfd, internal); 1761 return NULL; 1762 } 1763 1764 for (i = 0; 1765 i < symbol_ptr->u.syment.n_numaux; 1766 i++) 1767 { 1768 internal_ptr++; 1769 raw_src += symesz; 1770 1771 bfd_coff_swap_aux_in (abfd, (void *) raw_src, 1772 symbol_ptr->u.syment.n_type, 1773 symbol_ptr->u.syment.n_sclass, 1774 (int) i, symbol_ptr->u.syment.n_numaux, 1775 &(internal_ptr->u.auxent)); 1776 1777 internal_ptr->is_sym = false; 1778 coff_pointerize_aux (abfd, internal, symbol_ptr, i, 1779 internal_ptr, internal_end); 1780 } 1781 } 1782 1783 /* Free the raw symbols. */ 1784 if (obj_coff_external_syms (abfd) != NULL 1785 && ! obj_coff_keep_syms (abfd)) 1786 { 1787 free (obj_coff_external_syms (abfd)); 1788 obj_coff_external_syms (abfd) = NULL; 1789 } 1790 1791 for (internal_ptr = internal; internal_ptr < internal_end; 1792 internal_ptr++) 1793 { 1794 BFD_ASSERT (internal_ptr->is_sym); 1795 1796 if (internal_ptr->u.syment.n_sclass == C_FILE 1797 && internal_ptr->u.syment.n_numaux > 0) 1798 { 1799 combined_entry_type * aux = internal_ptr + 1; 1800 1801 /* Make a file symbol point to the name in the auxent, since 1802 the text ".file" is redundant. */ 1803 BFD_ASSERT (! aux->is_sym); 1804 1805 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0) 1806 { 1807 /* The filename is a long one, point into the string table. */ 1808 if (string_table == NULL) 1809 { 1810 string_table = _bfd_coff_read_string_table (abfd); 1811 if (string_table == NULL) 1812 return NULL; 1813 } 1814 1815 if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset) 1816 >= obj_coff_strings_len (abfd)) 1817 internal_ptr->u.syment._n._n_n._n_offset = 1818 (uintptr_t) _("<corrupt>"); 1819 else 1820 internal_ptr->u.syment._n._n_n._n_offset = 1821 (uintptr_t) (string_table 1822 + aux->u.auxent.x_file.x_n.x_n.x_offset); 1823 } 1824 else 1825 { 1826 /* Ordinary short filename, put into memory anyway. The 1827 Microsoft PE tools sometimes store a filename in 1828 multiple AUX entries. */ 1829 if (internal_ptr->u.syment.n_numaux > 1 1830 && coff_data (abfd)->pe) 1831 internal_ptr->u.syment._n._n_n._n_offset = 1832 ((uintptr_t) 1833 copy_name (abfd, 1834 aux->u.auxent.x_file.x_n.x_fname, 1835 internal_ptr->u.syment.n_numaux * symesz)); 1836 else 1837 internal_ptr->u.syment._n._n_n._n_offset = 1838 ((uintptr_t) 1839 copy_name (abfd, 1840 aux->u.auxent.x_file.x_n.x_fname, 1841 (size_t) bfd_coff_filnmlen (abfd))); 1842 } 1843 1844 /* Normalize other strings available in C_FILE aux entries. */ 1845 if (!coff_data (abfd)->pe) 1846 for (int numaux = 1; numaux < internal_ptr->u.syment.n_numaux; numaux++) 1847 { 1848 aux = internal_ptr + numaux + 1; 1849 BFD_ASSERT (! aux->is_sym); 1850 1851 if (aux->u.auxent.x_file.x_n.x_n.x_zeroes == 0) 1852 { 1853 /* The string information is a long one, point into the string table. */ 1854 if (string_table == NULL) 1855 { 1856 string_table = _bfd_coff_read_string_table (abfd); 1857 if (string_table == NULL) 1858 return NULL; 1859 } 1860 1861 if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_n.x_offset) 1862 >= obj_coff_strings_len (abfd)) 1863 aux->u.auxent.x_file.x_n.x_n.x_offset = 1864 (uintptr_t) _("<corrupt>"); 1865 else 1866 aux->u.auxent.x_file.x_n.x_n.x_offset = 1867 (uintptr_t) (string_table 1868 + (aux->u.auxent.x_file.x_n.x_n.x_offset)); 1869 } 1870 else 1871 aux->u.auxent.x_file.x_n.x_n.x_offset = 1872 ((uintptr_t) 1873 copy_name (abfd, 1874 aux->u.auxent.x_file.x_n.x_fname, 1875 (size_t) bfd_coff_filnmlen (abfd))); 1876 } 1877 1878 } 1879 else 1880 { 1881 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0) 1882 { 1883 /* This is a "short" name. Make it long. */ 1884 size_t i; 1885 char *newstring; 1886 1887 /* Find the length of this string without walking into memory 1888 that isn't ours. */ 1889 for (i = 0; i < 8; ++i) 1890 if (internal_ptr->u.syment._n._n_name[i] == '\0') 1891 break; 1892 1893 newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1)); 1894 if (newstring == NULL) 1895 return NULL; 1896 strncpy (newstring, internal_ptr->u.syment._n._n_name, i); 1897 internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) newstring; 1898 internal_ptr->u.syment._n._n_n._n_zeroes = 0; 1899 } 1900 else if (internal_ptr->u.syment._n._n_n._n_offset == 0) 1901 internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) ""; 1902 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment)) 1903 { 1904 /* Long name already. Point symbol at the string in the 1905 table. */ 1906 if (string_table == NULL) 1907 { 1908 string_table = _bfd_coff_read_string_table (abfd); 1909 if (string_table == NULL) 1910 return NULL; 1911 } 1912 if (internal_ptr->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd) 1913 || string_table + internal_ptr->u.syment._n._n_n._n_offset < string_table) 1914 internal_ptr->u.syment._n._n_n._n_offset = 1915 (uintptr_t) _("<corrupt>"); 1916 else 1917 internal_ptr->u.syment._n._n_n._n_offset = 1918 ((uintptr_t) (string_table 1919 + internal_ptr->u.syment._n._n_n._n_offset)); 1920 } 1921 else 1922 { 1923 /* Long name in debug section. Very similar. */ 1924 if (debug_sec_data == NULL) 1925 debug_sec_data = build_debug_section (abfd, & debug_sec); 1926 if (debug_sec_data != NULL) 1927 { 1928 BFD_ASSERT (debug_sec != NULL); 1929 /* PR binutils/17512: Catch out of range offsets into the debug data. */ 1930 if (internal_ptr->u.syment._n._n_n._n_offset > debug_sec->size 1931 || debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset < debug_sec_data) 1932 internal_ptr->u.syment._n._n_n._n_offset = 1933 (uintptr_t) _("<corrupt>"); 1934 else 1935 internal_ptr->u.syment._n._n_n._n_offset = 1936 (uintptr_t) (debug_sec_data 1937 + internal_ptr->u.syment._n._n_n._n_offset); 1938 } 1939 else 1940 internal_ptr->u.syment._n._n_n._n_offset = (uintptr_t) ""; 1941 } 1942 } 1943 internal_ptr += internal_ptr->u.syment.n_numaux; 1944 } 1945 1946 obj_raw_syments (abfd) = internal; 1947 BFD_ASSERT (obj_raw_syment_count (abfd) 1948 == (unsigned int) (internal_ptr - internal)); 1949 1950 return internal; 1951 } 1952 1953 long 1954 coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect) 1955 { 1956 size_t count, raw; 1957 1958 count = asect->reloc_count; 1959 if (count >= LONG_MAX / sizeof (arelent *) 1960 || _bfd_mul_overflow (count, bfd_coff_relsz (abfd), &raw)) 1961 { 1962 bfd_set_error (bfd_error_file_too_big); 1963 return -1; 1964 } 1965 if (!bfd_write_p (abfd)) 1966 { 1967 ufile_ptr filesize = bfd_get_file_size (abfd); 1968 if (filesize != 0 && raw > filesize) 1969 { 1970 bfd_set_error (bfd_error_file_truncated); 1971 return -1; 1972 } 1973 } 1974 return (count + 1) * sizeof (arelent *); 1975 } 1976 1977 asymbol * 1978 coff_make_empty_symbol (bfd *abfd) 1979 { 1980 size_t amt = sizeof (coff_symbol_type); 1981 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt); 1982 1983 if (new_symbol == NULL) 1984 return NULL; 1985 new_symbol->symbol.section = 0; 1986 new_symbol->native = NULL; 1987 new_symbol->lineno = NULL; 1988 new_symbol->done_lineno = false; 1989 new_symbol->symbol.the_bfd = abfd; 1990 1991 return & new_symbol->symbol; 1992 } 1993 1994 /* Make a debugging symbol. */ 1995 1996 asymbol * 1997 coff_bfd_make_debug_symbol (bfd *abfd, 1998 void * ptr ATTRIBUTE_UNUSED, 1999 unsigned long sz ATTRIBUTE_UNUSED) 2000 { 2001 size_t amt = sizeof (coff_symbol_type); 2002 coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt); 2003 2004 if (new_symbol == NULL) 2005 return NULL; 2006 /* @@ The 10 is a guess at a plausible maximum number of aux entries 2007 (but shouldn't be a constant). */ 2008 amt = sizeof (combined_entry_type) * 10; 2009 new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt); 2010 if (!new_symbol->native) 2011 return NULL; 2012 new_symbol->native->is_sym = true; 2013 new_symbol->symbol.section = bfd_abs_section_ptr; 2014 new_symbol->symbol.flags = BSF_DEBUGGING; 2015 new_symbol->lineno = NULL; 2016 new_symbol->done_lineno = false; 2017 new_symbol->symbol.the_bfd = abfd; 2018 2019 return & new_symbol->symbol; 2020 } 2021 2022 void 2023 coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret) 2024 { 2025 bfd_symbol_info (symbol, ret); 2026 2027 if (coffsymbol (symbol)->native != NULL 2028 && coffsymbol (symbol)->native->fix_value 2029 && coffsymbol (symbol)->native->is_sym) 2030 ret->value 2031 = (((uintptr_t) coffsymbol (symbol)->native->u.syment.n_value 2032 - (uintptr_t) obj_raw_syments (abfd)) 2033 / sizeof (combined_entry_type)); 2034 } 2035 2036 /* Print out information about COFF symbol. */ 2037 2038 void 2039 coff_print_symbol (bfd *abfd, 2040 void * filep, 2041 asymbol *symbol, 2042 bfd_print_symbol_type how) 2043 { 2044 FILE * file = (FILE *) filep; 2045 2046 switch (how) 2047 { 2048 case bfd_print_symbol_name: 2049 fprintf (file, "%s", symbol->name); 2050 break; 2051 2052 case bfd_print_symbol_more: 2053 fprintf (file, "coff %s %s", 2054 coffsymbol (symbol)->native ? "n" : "g", 2055 coffsymbol (symbol)->lineno ? "l" : " "); 2056 break; 2057 2058 case bfd_print_symbol_all: 2059 if (coffsymbol (symbol)->native) 2060 { 2061 bfd_vma val; 2062 unsigned int aux; 2063 combined_entry_type *combined = coffsymbol (symbol)->native; 2064 combined_entry_type *root = obj_raw_syments (abfd); 2065 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno; 2066 2067 fprintf (file, "[%3ld]", (long) (combined - root)); 2068 2069 /* PR 17512: file: 079-33786-0.001:0.1. */ 2070 if (combined < obj_raw_syments (abfd) 2071 || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd)) 2072 { 2073 fprintf (file, _("<corrupt info> %s"), symbol->name); 2074 break; 2075 } 2076 2077 BFD_ASSERT (combined->is_sym); 2078 if (! combined->fix_value) 2079 val = (bfd_vma) combined->u.syment.n_value; 2080 else 2081 val = (((uintptr_t) combined->u.syment.n_value - (uintptr_t) root) 2082 / sizeof (combined_entry_type)); 2083 2084 fprintf (file, "(sec %2d)(fl 0x%02x)(ty %4x)(scl %3d) (nx %d) 0x", 2085 combined->u.syment.n_scnum, 2086 combined->u.syment.n_flags, 2087 combined->u.syment.n_type, 2088 combined->u.syment.n_sclass, 2089 combined->u.syment.n_numaux); 2090 bfd_fprintf_vma (abfd, file, val); 2091 fprintf (file, " %s", symbol->name); 2092 2093 for (aux = 0; aux < combined->u.syment.n_numaux; aux++) 2094 { 2095 combined_entry_type *auxp = combined + aux + 1; 2096 long tagndx; 2097 2098 BFD_ASSERT (! auxp->is_sym); 2099 if (auxp->fix_tag) 2100 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root; 2101 else 2102 tagndx = auxp->u.auxent.x_sym.x_tagndx.l; 2103 2104 fprintf (file, "\n"); 2105 2106 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux)) 2107 continue; 2108 2109 switch (combined->u.syment.n_sclass) 2110 { 2111 case C_FILE: 2112 fprintf (file, "File "); 2113 /* Add additional information if this isn't the filename 2114 auxiliary entry. */ 2115 if (auxp->u.auxent.x_file.x_ftype) 2116 fprintf (file, "ftype %d fname \"%s\"", 2117 auxp->u.auxent.x_file.x_ftype, 2118 (char *) auxp->u.auxent.x_file.x_n.x_n.x_offset); 2119 break; 2120 2121 case C_DWARF: 2122 fprintf (file, "AUX scnlen 0x%lx nreloc %ld", 2123 (unsigned long) auxp->u.auxent.x_sect.x_scnlen, 2124 auxp->u.auxent.x_sect.x_nreloc); 2125 break; 2126 2127 case C_STAT: 2128 if (combined->u.syment.n_type == T_NULL) 2129 /* Probably a section symbol ? */ 2130 { 2131 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d", 2132 (unsigned long) auxp->u.auxent.x_scn.x_scnlen, 2133 auxp->u.auxent.x_scn.x_nreloc, 2134 auxp->u.auxent.x_scn.x_nlinno); 2135 if (auxp->u.auxent.x_scn.x_checksum != 0 2136 || auxp->u.auxent.x_scn.x_associated != 0 2137 || auxp->u.auxent.x_scn.x_comdat != 0) 2138 fprintf (file, " checksum 0x%lx assoc %d comdat %d", 2139 auxp->u.auxent.x_scn.x_checksum, 2140 auxp->u.auxent.x_scn.x_associated, 2141 auxp->u.auxent.x_scn.x_comdat); 2142 break; 2143 } 2144 /* Fall through. */ 2145 case C_EXT: 2146 case C_AIX_WEAKEXT: 2147 if (ISFCN (combined->u.syment.n_type)) 2148 { 2149 long next, llnos; 2150 2151 if (auxp->fix_end) 2152 next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p 2153 - root); 2154 else 2155 next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l; 2156 llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr; 2157 fprintf (file, 2158 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld", 2159 tagndx, 2160 (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize, 2161 llnos, next); 2162 break; 2163 } 2164 /* Fall through. */ 2165 default: 2166 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld", 2167 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno, 2168 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size, 2169 tagndx); 2170 if (auxp->fix_end) 2171 fprintf (file, " endndx %ld", 2172 ((long) 2173 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p 2174 - root))); 2175 break; 2176 } 2177 } 2178 2179 if (l) 2180 { 2181 fprintf (file, "\n%s :", l->u.sym->name); 2182 l++; 2183 while (l->line_number) 2184 { 2185 if (l->line_number > 0) 2186 { 2187 fprintf (file, "\n%4d : ", l->line_number); 2188 bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma); 2189 } 2190 l++; 2191 } 2192 } 2193 } 2194 else 2195 { 2196 bfd_print_symbol_vandf (abfd, (void *) file, symbol); 2197 fprintf (file, " %-5s %s %s %s", 2198 symbol->section->name, 2199 coffsymbol (symbol)->native ? "n" : "g", 2200 coffsymbol (symbol)->lineno ? "l" : " ", 2201 symbol->name); 2202 } 2203 } 2204 } 2205 2206 /* Return whether a symbol name implies a local symbol. In COFF, 2207 local symbols generally start with ``.L''. Most targets use this 2208 function for the is_local_label_name entry point, but some may 2209 override it. */ 2210 2211 bool 2212 _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, 2213 const char *name) 2214 { 2215 return name[0] == '.' && name[1] == 'L'; 2216 } 2217 2218 /* Provided a BFD, a section and an offset (in bytes, not octets) into the 2219 section, calculate and return the name of the source file and the line 2220 nearest to the wanted location. */ 2221 2222 bool 2223 coff_find_nearest_line_with_names (bfd *abfd, 2224 asymbol **symbols, 2225 asection *section, 2226 bfd_vma offset, 2227 const char **filename_ptr, 2228 const char **functionname_ptr, 2229 unsigned int *line_ptr, 2230 const struct dwarf_debug_section *debug_sections) 2231 { 2232 bool found; 2233 unsigned int i; 2234 unsigned int line_base; 2235 coff_data_type *cof = coff_data (abfd); 2236 /* Run through the raw syments if available. */ 2237 combined_entry_type *p; 2238 combined_entry_type *pend; 2239 alent *l; 2240 struct coff_section_tdata *sec_data; 2241 size_t amt; 2242 2243 /* Before looking through the symbol table, try to use a .stab 2244 section to find the information. */ 2245 if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset, 2246 &found, filename_ptr, 2247 functionname_ptr, line_ptr, 2248 &coff_data(abfd)->line_info)) 2249 return false; 2250 2251 if (found) 2252 return true; 2253 2254 /* Also try examining DWARF2 debugging information. */ 2255 if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset, 2256 filename_ptr, functionname_ptr, 2257 line_ptr, NULL, debug_sections, 2258 &coff_data(abfd)->dwarf2_find_line_info)) 2259 return true; 2260 2261 sec_data = coff_section_data (abfd, section); 2262 2263 /* If the DWARF lookup failed, but there is DWARF information available 2264 then the problem might be that the file has been rebased. This tool 2265 changes the VMAs of all the sections, but it does not update the DWARF 2266 information. So try again, using a bias against the address sought. */ 2267 if (coff_data (abfd)->dwarf2_find_line_info != NULL) 2268 { 2269 bfd_signed_vma bias = 0; 2270 2271 /* Create a cache of the result for the next call. */ 2272 if (sec_data == NULL && section->owner == abfd) 2273 { 2274 amt = sizeof (struct coff_section_tdata); 2275 section->used_by_bfd = bfd_zalloc (abfd, amt); 2276 sec_data = (struct coff_section_tdata *) section->used_by_bfd; 2277 } 2278 2279 if (sec_data != NULL && sec_data->saved_bias) 2280 bias = sec_data->bias; 2281 else if (symbols) 2282 { 2283 bias = _bfd_dwarf2_find_symbol_bias (symbols, 2284 & coff_data (abfd)->dwarf2_find_line_info); 2285 2286 if (sec_data) 2287 { 2288 sec_data->saved_bias = true; 2289 sec_data->bias = bias; 2290 } 2291 } 2292 2293 if (bias 2294 && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, 2295 offset + bias, 2296 filename_ptr, functionname_ptr, 2297 line_ptr, NULL, debug_sections, 2298 &coff_data(abfd)->dwarf2_find_line_info)) 2299 return true; 2300 } 2301 2302 *filename_ptr = 0; 2303 *functionname_ptr = 0; 2304 *line_ptr = 0; 2305 2306 /* Don't try and find line numbers in a non coff file. */ 2307 if (!bfd_family_coff (abfd)) 2308 return false; 2309 2310 if (cof == NULL) 2311 return false; 2312 2313 /* Find the first C_FILE symbol. */ 2314 p = cof->raw_syments; 2315 if (!p) 2316 return false; 2317 2318 pend = p + cof->raw_syment_count; 2319 while (p < pend) 2320 { 2321 BFD_ASSERT (p->is_sym); 2322 if (p->u.syment.n_sclass == C_FILE) 2323 break; 2324 p += 1 + p->u.syment.n_numaux; 2325 } 2326 2327 if (p < pend) 2328 { 2329 bfd_vma sec_vma; 2330 bfd_vma maxdiff; 2331 2332 /* Look through the C_FILE symbols to find the best one. */ 2333 sec_vma = bfd_section_vma (section); 2334 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; 2335 maxdiff = (bfd_vma) 0 - (bfd_vma) 1; 2336 while (1) 2337 { 2338 bfd_vma file_addr; 2339 combined_entry_type *p2; 2340 2341 for (p2 = p + 1 + p->u.syment.n_numaux; 2342 p2 < pend; 2343 p2 += 1 + p2->u.syment.n_numaux) 2344 { 2345 BFD_ASSERT (p2->is_sym); 2346 if (p2->u.syment.n_scnum > 0 2347 && (section 2348 == coff_section_from_bfd_index (abfd, 2349 p2->u.syment.n_scnum))) 2350 break; 2351 if (p2->u.syment.n_sclass == C_FILE) 2352 { 2353 p2 = pend; 2354 break; 2355 } 2356 } 2357 if (p2 >= pend) 2358 break; 2359 2360 file_addr = (bfd_vma) p2->u.syment.n_value; 2361 /* PR 11512: Include the section address of the function name symbol. */ 2362 if (p2->u.syment.n_scnum > 0) 2363 file_addr += coff_section_from_bfd_index (abfd, 2364 p2->u.syment.n_scnum)->vma; 2365 /* We use <= MAXDIFF here so that if we get a zero length 2366 file, we actually use the next file entry. */ 2367 if (p2 < pend 2368 && offset + sec_vma >= file_addr 2369 && offset + sec_vma - file_addr <= maxdiff) 2370 { 2371 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset; 2372 maxdiff = offset + sec_vma - p2->u.syment.n_value; 2373 } 2374 2375 if (p->u.syment.n_value >= cof->raw_syment_count) 2376 break; 2377 2378 /* Avoid endless loops on erroneous files by ensuring that 2379 we always move forward in the file. */ 2380 if (p >= cof->raw_syments + p->u.syment.n_value) 2381 break; 2382 2383 p = cof->raw_syments + p->u.syment.n_value; 2384 if (!p->is_sym || p->u.syment.n_sclass != C_FILE) 2385 break; 2386 } 2387 } 2388 2389 if (section->lineno_count == 0) 2390 { 2391 *functionname_ptr = NULL; 2392 *line_ptr = 0; 2393 return true; 2394 } 2395 2396 /* Now wander though the raw linenumbers of the section. 2397 If we have been called on this section before, and the offset 2398 we want is further down then we can prime the lookup loop. */ 2399 if (sec_data != NULL 2400 && sec_data->i > 0 2401 && offset >= sec_data->offset) 2402 { 2403 i = sec_data->i; 2404 *functionname_ptr = sec_data->function; 2405 line_base = sec_data->line_base; 2406 } 2407 else 2408 { 2409 i = 0; 2410 line_base = 0; 2411 } 2412 2413 if (section->lineno != NULL) 2414 { 2415 bfd_vma last_value = 0; 2416 2417 l = §ion->lineno[i]; 2418 2419 for (; i < section->lineno_count; i++) 2420 { 2421 if (l->line_number == 0) 2422 { 2423 /* Get the symbol this line number points at. */ 2424 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym); 2425 if (coff->symbol.value > offset) 2426 break; 2427 2428 *functionname_ptr = coff->symbol.name; 2429 last_value = coff->symbol.value; 2430 if (coff->native) 2431 { 2432 combined_entry_type *s = coff->native; 2433 2434 BFD_ASSERT (s->is_sym); 2435 s = s + 1 + s->u.syment.n_numaux; 2436 2437 /* In XCOFF a debugging symbol can follow the 2438 function symbol. */ 2439 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd)) 2440 < obj_raw_syment_count (abfd) * sizeof (*s)) 2441 && s->u.syment.n_scnum == N_DEBUG) 2442 s = s + 1 + s->u.syment.n_numaux; 2443 2444 /* S should now point to the .bf of the function. */ 2445 if (((size_t) ((char *) s - (char *) obj_raw_syments (abfd)) 2446 < obj_raw_syment_count (abfd) * sizeof (*s)) 2447 && s->u.syment.n_numaux) 2448 { 2449 /* The linenumber is stored in the auxent. */ 2450 union internal_auxent *a = &((s + 1)->u.auxent); 2451 2452 line_base = a->x_sym.x_misc.x_lnsz.x_lnno; 2453 *line_ptr = line_base; 2454 } 2455 } 2456 } 2457 else 2458 { 2459 if (l->u.offset > offset) 2460 break; 2461 *line_ptr = l->line_number + line_base - 1; 2462 } 2463 l++; 2464 } 2465 2466 /* If we fell off the end of the loop, then assume that this 2467 symbol has no line number info. Otherwise, symbols with no 2468 line number info get reported with the line number of the 2469 last line of the last symbol which does have line number 2470 info. We use 0x100 as a slop to account for cases where the 2471 last line has executable code. */ 2472 if (i >= section->lineno_count 2473 && last_value != 0 2474 && offset - last_value > 0x100) 2475 { 2476 *functionname_ptr = NULL; 2477 *line_ptr = 0; 2478 } 2479 } 2480 2481 /* Cache the results for the next call. */ 2482 if (sec_data == NULL && section->owner == abfd) 2483 { 2484 amt = sizeof (struct coff_section_tdata); 2485 section->used_by_bfd = bfd_zalloc (abfd, amt); 2486 sec_data = (struct coff_section_tdata *) section->used_by_bfd; 2487 } 2488 2489 if (sec_data != NULL) 2490 { 2491 sec_data->offset = offset; 2492 sec_data->i = i - 1; 2493 sec_data->function = *functionname_ptr; 2494 sec_data->line_base = line_base; 2495 } 2496 2497 return true; 2498 } 2499 2500 bool 2501 coff_find_nearest_line (bfd *abfd, 2502 asymbol **symbols, 2503 asection *section, 2504 bfd_vma offset, 2505 const char **filename_ptr, 2506 const char **functionname_ptr, 2507 unsigned int *line_ptr, 2508 unsigned int *discriminator_ptr) 2509 { 2510 if (discriminator_ptr) 2511 *discriminator_ptr = 0; 2512 return coff_find_nearest_line_with_names (abfd, symbols, section, offset, 2513 filename_ptr, functionname_ptr, 2514 line_ptr, dwarf_debug_sections); 2515 } 2516 2517 bool 2518 coff_find_inliner_info (bfd *abfd, 2519 const char **filename_ptr, 2520 const char **functionname_ptr, 2521 unsigned int *line_ptr) 2522 { 2523 bool found; 2524 2525 found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr, 2526 functionname_ptr, line_ptr, 2527 &coff_data(abfd)->dwarf2_find_line_info); 2528 return (found); 2529 } 2530 2531 int 2532 coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info) 2533 { 2534 size_t size; 2535 2536 if (!bfd_link_relocatable (info)) 2537 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd); 2538 else 2539 size = bfd_coff_filhsz (abfd); 2540 2541 size += abfd->section_count * bfd_coff_scnhsz (abfd); 2542 return size; 2543 } 2544 2545 /* Change the class of a coff symbol held by BFD. */ 2546 2547 bool 2548 bfd_coff_set_symbol_class (bfd * abfd, 2549 asymbol * symbol, 2550 unsigned int symbol_class) 2551 { 2552 coff_symbol_type * csym; 2553 2554 csym = coff_symbol_from (symbol); 2555 if (csym == NULL) 2556 { 2557 bfd_set_error (bfd_error_invalid_operation); 2558 return false; 2559 } 2560 else if (csym->native == NULL) 2561 { 2562 /* This is an alien symbol which no native coff backend data. 2563 We cheat here by creating a fake native entry for it and 2564 then filling in the class. This code is based on that in 2565 coff_write_alien_symbol(). */ 2566 2567 combined_entry_type * native; 2568 size_t amt = sizeof (* native); 2569 2570 native = (combined_entry_type *) bfd_zalloc (abfd, amt); 2571 if (native == NULL) 2572 return false; 2573 2574 native->is_sym = true; 2575 native->u.syment.n_type = T_NULL; 2576 native->u.syment.n_sclass = symbol_class; 2577 2578 if (bfd_is_und_section (symbol->section)) 2579 { 2580 native->u.syment.n_scnum = N_UNDEF; 2581 native->u.syment.n_value = symbol->value; 2582 } 2583 else if (bfd_is_com_section (symbol->section)) 2584 { 2585 native->u.syment.n_scnum = N_UNDEF; 2586 native->u.syment.n_value = symbol->value; 2587 } 2588 else 2589 { 2590 native->u.syment.n_scnum = 2591 symbol->section->output_section->target_index; 2592 native->u.syment.n_value = (symbol->value 2593 + symbol->section->output_offset); 2594 if (! obj_pe (abfd)) 2595 native->u.syment.n_value += symbol->section->output_section->vma; 2596 2597 /* Copy the any flags from the file header into the symbol. 2598 FIXME: Why? */ 2599 native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags; 2600 } 2601 2602 csym->native = native; 2603 } 2604 else 2605 csym->native->u.syment.n_sclass = symbol_class; 2606 2607 return true; 2608 } 2609 2610 bool 2611 _bfd_coff_section_already_linked (bfd *abfd, 2612 asection *sec, 2613 struct bfd_link_info *info) 2614 { 2615 flagword flags; 2616 const char *name, *key; 2617 struct bfd_section_already_linked *l; 2618 struct bfd_section_already_linked_hash_entry *already_linked_list; 2619 struct coff_comdat_info *s_comdat; 2620 2621 if (sec->output_section == bfd_abs_section_ptr) 2622 return false; 2623 2624 flags = sec->flags; 2625 if ((flags & SEC_LINK_ONCE) == 0) 2626 return false; 2627 2628 /* The COFF backend linker doesn't support group sections. */ 2629 if ((flags & SEC_GROUP) != 0) 2630 return false; 2631 2632 name = bfd_section_name (sec); 2633 s_comdat = bfd_coff_get_comdat_section (abfd, sec); 2634 2635 if (s_comdat != NULL) 2636 key = s_comdat->name; 2637 else 2638 { 2639 if (startswith (name, ".gnu.linkonce.") 2640 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 2641 key++; 2642 else 2643 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>, 2644 .xdata$<key> and .pdata$<key> only the first of which has a 2645 comdat key. Should these all match the LTO IR key? */ 2646 key = name; 2647 } 2648 2649 already_linked_list = bfd_section_already_linked_table_lookup (key); 2650 2651 for (l = already_linked_list->entry; l != NULL; l = l->next) 2652 { 2653 struct coff_comdat_info *l_comdat; 2654 2655 l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec); 2656 2657 /* The section names must match, and both sections must be 2658 comdat and have the same comdat name, or both sections must 2659 be non-comdat. LTO IR plugin sections are an exception. They 2660 are always named .gnu.linkonce.t.<key> (<key> is some string) 2661 and match any comdat section with comdat name of <key>, and 2662 any linkonce section with the same suffix, ie. 2663 .gnu.linkonce.*.<key>. */ 2664 if (((s_comdat != NULL) == (l_comdat != NULL) 2665 && strcmp (name, l->sec->name) == 0) 2666 || (l->sec->owner->flags & BFD_PLUGIN) != 0 2667 || (sec->owner->flags & BFD_PLUGIN) != 0) 2668 { 2669 /* The section has already been linked. See if we should 2670 issue a warning. */ 2671 return _bfd_handle_already_linked (sec, l, info); 2672 } 2673 } 2674 2675 /* This is the first section with this name. Record it. */ 2676 if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) 2677 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); 2678 return false; 2679 } 2680 2681 /* Initialize COOKIE for input bfd ABFD. */ 2682 2683 static bool 2684 init_reloc_cookie (struct coff_reloc_cookie *cookie, 2685 struct bfd_link_info *info ATTRIBUTE_UNUSED, 2686 bfd *abfd) 2687 { 2688 /* Sometimes the symbol table does not yet have been loaded here. */ 2689 bfd_coff_slurp_symbol_table (abfd); 2690 2691 cookie->abfd = abfd; 2692 cookie->sym_hashes = obj_coff_sym_hashes (abfd); 2693 2694 cookie->symbols = obj_symbols (abfd); 2695 2696 return true; 2697 } 2698 2699 /* Free the memory allocated by init_reloc_cookie, if appropriate. */ 2700 2701 static void 2702 fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED, 2703 bfd *abfd ATTRIBUTE_UNUSED) 2704 { 2705 /* Nothing to do. */ 2706 } 2707 2708 /* Initialize the relocation information in COOKIE for input section SEC 2709 of input bfd ABFD. */ 2710 2711 static bool 2712 init_reloc_cookie_rels (struct coff_reloc_cookie *cookie, 2713 struct bfd_link_info *info ATTRIBUTE_UNUSED, 2714 bfd *abfd, 2715 asection *sec) 2716 { 2717 if (sec->reloc_count == 0) 2718 { 2719 cookie->rels = NULL; 2720 cookie->relend = NULL; 2721 cookie->rel = NULL; 2722 return true; 2723 } 2724 2725 cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, false, NULL, 2726 0, NULL); 2727 2728 if (cookie->rels == NULL) 2729 return false; 2730 2731 cookie->rel = cookie->rels; 2732 cookie->relend = (cookie->rels + sec->reloc_count); 2733 return true; 2734 } 2735 2736 /* Free the memory allocated by init_reloc_cookie_rels, 2737 if appropriate. */ 2738 2739 static void 2740 fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie, 2741 asection *sec) 2742 { 2743 if (cookie->rels 2744 /* PR 20401. The relocs may not have been cached, so check first. 2745 If the relocs were loaded by init_reloc_cookie_rels() then this 2746 will be the case. FIXME: Would performance be improved if the 2747 relocs *were* cached ? */ 2748 && coff_section_data (NULL, sec) 2749 && coff_section_data (NULL, sec)->relocs != cookie->rels) 2750 free (cookie->rels); 2751 } 2752 2753 /* Initialize the whole of COOKIE for input section SEC. */ 2754 2755 static bool 2756 init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie, 2757 struct bfd_link_info *info, 2758 asection *sec) 2759 { 2760 if (!init_reloc_cookie (cookie, info, sec->owner)) 2761 return false; 2762 2763 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) 2764 { 2765 fini_reloc_cookie (cookie, sec->owner); 2766 return false; 2767 } 2768 return true; 2769 } 2770 2771 /* Free the memory allocated by init_reloc_cookie_for_section, 2772 if appropriate. */ 2773 2774 static void 2775 fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie, 2776 asection *sec) 2777 { 2778 fini_reloc_cookie_rels (cookie, sec); 2779 fini_reloc_cookie (cookie, sec->owner); 2780 } 2781 2782 static asection * 2783 _bfd_coff_gc_mark_hook (asection *sec, 2784 struct bfd_link_info *info ATTRIBUTE_UNUSED, 2785 struct internal_reloc *rel ATTRIBUTE_UNUSED, 2786 struct coff_link_hash_entry *h, 2787 struct internal_syment *sym) 2788 { 2789 if (h != NULL) 2790 { 2791 switch (h->root.type) 2792 { 2793 case bfd_link_hash_defined: 2794 case bfd_link_hash_defweak: 2795 return h->root.u.def.section; 2796 2797 case bfd_link_hash_common: 2798 return h->root.u.c.p->section; 2799 2800 case bfd_link_hash_undefweak: 2801 if (h->symbol_class == C_NT_WEAK && h->numaux == 1) 2802 { 2803 /* PE weak externals. A weak symbol may include an auxiliary 2804 record indicating that if the weak symbol is not resolved, 2805 another external symbol is used instead. */ 2806 struct coff_link_hash_entry *h2 = 2807 h->auxbfd->tdata.coff_obj_data->sym_hashes[ 2808 h->aux->x_sym.x_tagndx.l]; 2809 2810 if (h2 && h2->root.type != bfd_link_hash_undefined) 2811 return h2->root.u.def.section; 2812 } 2813 break; 2814 2815 case bfd_link_hash_undefined: 2816 default: 2817 break; 2818 } 2819 return NULL; 2820 } 2821 2822 return coff_section_from_bfd_index (sec->owner, sym->n_scnum); 2823 } 2824 2825 /* COOKIE->rel describes a relocation against section SEC, which is 2826 a section we've decided to keep. Return the section that contains 2827 the relocation symbol, or NULL if no section contains it. */ 2828 2829 static asection * 2830 _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec, 2831 coff_gc_mark_hook_fn gc_mark_hook, 2832 struct coff_reloc_cookie *cookie) 2833 { 2834 struct coff_link_hash_entry *h; 2835 2836 h = cookie->sym_hashes[cookie->rel->r_symndx]; 2837 if (h != NULL) 2838 { 2839 while (h->root.type == bfd_link_hash_indirect 2840 || h->root.type == bfd_link_hash_warning) 2841 h = (struct coff_link_hash_entry *) h->root.u.i.link; 2842 2843 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); 2844 } 2845 2846 return (*gc_mark_hook) (sec, info, cookie->rel, NULL, 2847 &(cookie->symbols 2848 + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment); 2849 } 2850 2851 static bool _bfd_coff_gc_mark 2852 (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn); 2853 2854 /* COOKIE->rel describes a relocation against section SEC, which is 2855 a section we've decided to keep. Mark the section that contains 2856 the relocation symbol. */ 2857 2858 static bool 2859 _bfd_coff_gc_mark_reloc (struct bfd_link_info *info, 2860 asection *sec, 2861 coff_gc_mark_hook_fn gc_mark_hook, 2862 struct coff_reloc_cookie *cookie) 2863 { 2864 asection *rsec; 2865 2866 rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie); 2867 if (rsec && !rsec->gc_mark) 2868 { 2869 if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour) 2870 rsec->gc_mark = 1; 2871 else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook)) 2872 return false; 2873 } 2874 return true; 2875 } 2876 2877 /* The mark phase of garbage collection. For a given section, mark 2878 it and any sections in this section's group, and all the sections 2879 which define symbols to which it refers. */ 2880 2881 static bool 2882 _bfd_coff_gc_mark (struct bfd_link_info *info, 2883 asection *sec, 2884 coff_gc_mark_hook_fn gc_mark_hook) 2885 { 2886 bool ret = true; 2887 2888 sec->gc_mark = 1; 2889 2890 /* Look through the section relocs. */ 2891 if ((sec->flags & SEC_RELOC) != 0 2892 && sec->reloc_count > 0) 2893 { 2894 struct coff_reloc_cookie cookie; 2895 2896 if (!init_reloc_cookie_for_section (&cookie, info, sec)) 2897 ret = false; 2898 else 2899 { 2900 for (; cookie.rel < cookie.relend; cookie.rel++) 2901 { 2902 if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) 2903 { 2904 ret = false; 2905 break; 2906 } 2907 } 2908 fini_reloc_cookie_for_section (&cookie, sec); 2909 } 2910 } 2911 2912 return ret; 2913 } 2914 2915 static bool 2916 _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info, 2917 coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) 2918 { 2919 bfd *ibfd; 2920 2921 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 2922 { 2923 asection *isec; 2924 bool some_kept; 2925 2926 if (bfd_get_flavour (ibfd) != bfd_target_coff_flavour) 2927 continue; 2928 2929 /* Ensure all linker created sections are kept, and see whether 2930 any other section is already marked. */ 2931 some_kept = false; 2932 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 2933 { 2934 if ((isec->flags & SEC_LINKER_CREATED) != 0) 2935 isec->gc_mark = 1; 2936 else if (isec->gc_mark) 2937 some_kept = true; 2938 } 2939 2940 /* If no section in this file will be kept, then we can 2941 toss out debug sections. */ 2942 if (!some_kept) 2943 continue; 2944 2945 /* Keep debug and special sections like .comment when they are 2946 not part of a group, or when we have single-member groups. */ 2947 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 2948 if ((isec->flags & SEC_DEBUGGING) != 0 2949 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 2950 isec->gc_mark = 1; 2951 } 2952 return true; 2953 } 2954 2955 /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */ 2956 2957 static bool 2958 coff_gc_sweep_symbol (struct coff_link_hash_entry *h, 2959 void *data ATTRIBUTE_UNUSED) 2960 { 2961 if (h->root.type == bfd_link_hash_warning) 2962 h = (struct coff_link_hash_entry *) h->root.u.i.link; 2963 2964 if ((h->root.type == bfd_link_hash_defined 2965 || h->root.type == bfd_link_hash_defweak) 2966 && !h->root.u.def.section->gc_mark 2967 && !(h->root.u.def.section->owner->flags & DYNAMIC)) 2968 { 2969 /* Do our best to hide the symbol. */ 2970 h->root.u.def.section = bfd_und_section_ptr; 2971 h->symbol_class = C_HIDDEN; 2972 } 2973 2974 return true; 2975 } 2976 2977 /* The sweep phase of garbage collection. Remove all garbage sections. */ 2978 2979 typedef bool (*gc_sweep_hook_fn) 2980 (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *); 2981 2982 static bool 2983 coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info) 2984 { 2985 bfd *sub; 2986 2987 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 2988 { 2989 asection *o; 2990 2991 if (bfd_get_flavour (sub) != bfd_target_coff_flavour) 2992 continue; 2993 2994 for (o = sub->sections; o != NULL; o = o->next) 2995 { 2996 /* Keep debug and special sections. */ 2997 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 2998 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 2999 o->gc_mark = 1; 3000 else if (startswith (o->name, ".idata") 3001 || startswith (o->name, ".pdata") 3002 || startswith (o->name, ".xdata") 3003 || startswith (o->name, ".rsrc")) 3004 o->gc_mark = 1; 3005 3006 if (o->gc_mark) 3007 continue; 3008 3009 /* Skip sweeping sections already excluded. */ 3010 if (o->flags & SEC_EXCLUDE) 3011 continue; 3012 3013 /* Since this is early in the link process, it is simple 3014 to remove a section from the output. */ 3015 o->flags |= SEC_EXCLUDE; 3016 3017 if (info->print_gc_sections && o->size != 0) 3018 /* xgettext: c-format */ 3019 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), 3020 o, sub); 3021 3022 #if 0 3023 /* But we also have to update some of the relocation 3024 info we collected before. */ 3025 if (gc_sweep_hook 3026 && (o->flags & SEC_RELOC) != 0 3027 && o->reloc_count > 0 3028 && !bfd_is_abs_section (o->output_section)) 3029 { 3030 struct internal_reloc *internal_relocs; 3031 bool r; 3032 3033 internal_relocs 3034 = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL, 3035 info->keep_memory); 3036 if (internal_relocs == NULL) 3037 return false; 3038 3039 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); 3040 3041 if (coff_section_data (o)->relocs != internal_relocs) 3042 free (internal_relocs); 3043 3044 if (!r) 3045 return false; 3046 } 3047 #endif 3048 } 3049 } 3050 3051 /* Remove the symbols that were in the swept sections from the dynamic 3052 symbol table. */ 3053 coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol, 3054 NULL); 3055 3056 return true; 3057 } 3058 3059 /* Keep all sections containing symbols undefined on the command-line, 3060 and the section containing the entry symbol. */ 3061 3062 static void 3063 _bfd_coff_gc_keep (struct bfd_link_info *info) 3064 { 3065 struct bfd_sym_chain *sym; 3066 3067 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) 3068 { 3069 struct coff_link_hash_entry *h; 3070 3071 h = coff_link_hash_lookup (coff_hash_table (info), sym->name, 3072 false, false, false); 3073 3074 if (h != NULL 3075 && (h->root.type == bfd_link_hash_defined 3076 || h->root.type == bfd_link_hash_defweak) 3077 && !bfd_is_abs_section (h->root.u.def.section)) 3078 h->root.u.def.section->flags |= SEC_KEEP; 3079 } 3080 } 3081 3082 /* Do mark and sweep of unused sections. */ 3083 3084 bool 3085 bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info) 3086 { 3087 bfd *sub; 3088 3089 /* FIXME: Should we implement this? */ 3090 #if 0 3091 const bfd_coff_backend_data *bed = coff_backend_info (abfd); 3092 3093 if (!bed->can_gc_sections 3094 || !is_coff_hash_table (info->hash)) 3095 { 3096 _bfd_error_handler(_("warning: gc-sections option ignored")); 3097 return true; 3098 } 3099 #endif 3100 3101 _bfd_coff_gc_keep (info); 3102 3103 /* Grovel through relocs to find out who stays ... */ 3104 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 3105 { 3106 asection *o; 3107 3108 if (bfd_get_flavour (sub) != bfd_target_coff_flavour) 3109 continue; 3110 3111 for (o = sub->sections; o != NULL; o = o->next) 3112 { 3113 if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP 3114 || startswith (o->name, ".vectors") 3115 || startswith (o->name, ".ctors") 3116 || startswith (o->name, ".dtors")) 3117 && !o->gc_mark) 3118 { 3119 if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook)) 3120 return false; 3121 } 3122 } 3123 } 3124 3125 /* Allow the backend to mark additional target specific sections. */ 3126 _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook); 3127 3128 /* ... and mark SEC_EXCLUDE for those that go. */ 3129 return coff_gc_sweep (abfd, info); 3130 } 3131 3132 /* Return name used to identify a comdat group. */ 3133 3134 const char * 3135 bfd_coff_group_name (bfd *abfd, const asection *sec) 3136 { 3137 struct coff_comdat_info *ci = bfd_coff_get_comdat_section (abfd, sec); 3138 if (ci != NULL) 3139 return ci->name; 3140 return NULL; 3141 } 3142 3143 bool 3144 _bfd_coff_close_and_cleanup (bfd *abfd) 3145 { 3146 struct coff_tdata *tdata = coff_data (abfd); 3147 3148 if (tdata != NULL) 3149 { 3150 /* PR 25447: 3151 Do not clear the keep_syms and keep_strings flags. 3152 These may have been set by pe_ILF_build_a_bfd() indicating 3153 that the syms and strings pointers are not to be freed. */ 3154 if (bfd_get_format (abfd) == bfd_object 3155 && bfd_family_coff (abfd) 3156 && !_bfd_coff_free_symbols (abfd)) 3157 return false; 3158 3159 if (bfd_get_format (abfd) == bfd_object 3160 || bfd_get_format (abfd) == bfd_core) 3161 { 3162 _bfd_dwarf2_cleanup_debug_info (abfd, &tdata->dwarf2_find_line_info); 3163 _bfd_stab_cleanup (abfd, &tdata->line_info); 3164 } 3165 } 3166 return _bfd_generic_close_and_cleanup (abfd); 3167 } 3168