1 /* POWER/PowerPC XCOFF linker support. 2 Copyright (C) 1995-2019 Free Software Foundation, Inc. 3 Written by Ian Lance Taylor <ian@cygnus.com>, 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 #include "sysdep.h" 23 #include "bfd.h" 24 #include "bfdlink.h" 25 #include "libbfd.h" 26 #include "coff/internal.h" 27 #include "coff/xcoff.h" 28 #include "libcoff.h" 29 #include "libxcoff.h" 30 #include "libiberty.h" 31 32 /* This file holds the XCOFF linker code. */ 33 34 #undef STRING_SIZE_SIZE 35 #define STRING_SIZE_SIZE 4 36 37 /* We reuse the SEC_ROM flag as a mark flag for garbage collection. 38 This flag will only be used on input sections. */ 39 40 #define SEC_MARK (SEC_ROM) 41 42 /* The list of import files. */ 43 44 struct xcoff_import_file 45 { 46 /* The next entry in the list. */ 47 struct xcoff_import_file *next; 48 /* The path. */ 49 const char *path; 50 /* The file name. */ 51 const char *file; 52 /* The member name. */ 53 const char *member; 54 }; 55 56 /* Information we keep for each section in the output file during the 57 final link phase. */ 58 59 struct xcoff_link_section_info 60 { 61 /* The relocs to be output. */ 62 struct internal_reloc *relocs; 63 /* For each reloc against a global symbol whose index was not known 64 when the reloc was handled, the global hash table entry. */ 65 struct xcoff_link_hash_entry **rel_hashes; 66 /* If there is a TOC relative reloc against a global symbol, and the 67 index of the TOC symbol is not known when the reloc was handled, 68 an entry is added to this linked list. This is not an array, 69 like rel_hashes, because this case is quite uncommon. */ 70 struct xcoff_toc_rel_hash 71 { 72 struct xcoff_toc_rel_hash *next; 73 struct xcoff_link_hash_entry *h; 74 struct internal_reloc *rel; 75 } *toc_rel_hashes; 76 }; 77 78 /* Information that the XCOFF linker collects about an archive. */ 79 struct xcoff_archive_info 80 { 81 /* The archive described by this entry. */ 82 bfd *archive; 83 84 /* The import path and import filename to use when referring to 85 this archive in the .loader section. */ 86 const char *imppath; 87 const char *impfile; 88 89 /* True if the archive contains a dynamic object. */ 90 unsigned int contains_shared_object_p : 1; 91 92 /* True if the previous field is valid. */ 93 unsigned int know_contains_shared_object_p : 1; 94 }; 95 96 struct xcoff_link_hash_table 97 { 98 struct bfd_link_hash_table root; 99 100 /* The .debug string hash table. We need to compute this while 101 reading the input files, so that we know how large the .debug 102 section will be before we assign section positions. */ 103 struct bfd_strtab_hash *debug_strtab; 104 105 /* The .debug section we will use for the final output. */ 106 asection *debug_section; 107 108 /* The .loader section we will use for the final output. */ 109 asection *loader_section; 110 111 /* A count of non TOC relative relocs which will need to be 112 allocated in the .loader section. */ 113 size_t ldrel_count; 114 115 /* The .loader section header. */ 116 struct internal_ldhdr ldhdr; 117 118 /* The .gl section we use to hold global linkage code. */ 119 asection *linkage_section; 120 121 /* The .tc section we use to hold toc entries we build for global 122 linkage code. */ 123 asection *toc_section; 124 125 /* The .ds section we use to hold function descriptors which we 126 create for exported symbols. */ 127 asection *descriptor_section; 128 129 /* The list of import files. */ 130 struct xcoff_import_file *imports; 131 132 /* Required alignment of sections within the output file. */ 133 unsigned long file_align; 134 135 /* Whether the .text section must be read-only. */ 136 bfd_boolean textro; 137 138 /* Whether -brtl was specified. */ 139 bfd_boolean rtld; 140 141 /* Whether garbage collection was done. */ 142 bfd_boolean gc; 143 144 /* A linked list of symbols for which we have size information. */ 145 struct xcoff_link_size_list 146 { 147 struct xcoff_link_size_list *next; 148 struct xcoff_link_hash_entry *h; 149 bfd_size_type size; 150 } 151 *size_list; 152 153 /* Information about archives. */ 154 htab_t archive_info; 155 156 /* Magic sections: _text, _etext, _data, _edata, _end, end. */ 157 asection *special_sections[XCOFF_NUMBER_OF_SPECIAL_SECTIONS]; 158 }; 159 160 /* Information that we pass around while doing the final link step. */ 161 162 struct xcoff_final_link_info 163 { 164 /* General link information. */ 165 struct bfd_link_info *info; 166 /* Output BFD. */ 167 bfd *output_bfd; 168 /* Hash table for long symbol names. */ 169 struct bfd_strtab_hash *strtab; 170 /* Array of information kept for each output section, indexed by the 171 target_index field. */ 172 struct xcoff_link_section_info *section_info; 173 /* Symbol index of last C_FILE symbol (-1 if none). */ 174 long last_file_index; 175 /* Contents of last C_FILE symbol. */ 176 struct internal_syment last_file; 177 /* Symbol index of TOC symbol. */ 178 long toc_symindx; 179 /* Start of .loader symbols. */ 180 bfd_byte *ldsym; 181 /* Next .loader reloc to swap out. */ 182 bfd_byte *ldrel; 183 /* File position of start of line numbers. */ 184 file_ptr line_filepos; 185 /* Buffer large enough to hold swapped symbols of any input file. */ 186 struct internal_syment *internal_syms; 187 /* Buffer large enough to hold output indices of symbols of any 188 input file. */ 189 long *sym_indices; 190 /* Buffer large enough to hold output symbols for any input file. */ 191 bfd_byte *outsyms; 192 /* Buffer large enough to hold external line numbers for any input 193 section. */ 194 bfd_byte *linenos; 195 /* Buffer large enough to hold any input section. */ 196 bfd_byte *contents; 197 /* Buffer large enough to hold external relocs of any input section. */ 198 bfd_byte *external_relocs; 199 }; 200 201 static bfd_boolean xcoff_mark (struct bfd_link_info *, asection *); 202 203 204 205 /* Routines to read XCOFF dynamic information. This don't really 206 belong here, but we already have the ldsym manipulation routines 207 here. */ 208 209 /* Read the contents of a section. */ 210 211 static bfd_boolean 212 xcoff_get_section_contents (bfd *abfd, asection *sec) 213 { 214 if (coff_section_data (abfd, sec) == NULL) 215 { 216 bfd_size_type amt = sizeof (struct coff_section_tdata); 217 218 sec->used_by_bfd = bfd_zalloc (abfd, amt); 219 if (sec->used_by_bfd == NULL) 220 return FALSE; 221 } 222 223 if (coff_section_data (abfd, sec)->contents == NULL) 224 { 225 bfd_byte *contents; 226 227 if (! bfd_malloc_and_get_section (abfd, sec, &contents)) 228 { 229 if (contents != NULL) 230 free (contents); 231 return FALSE; 232 } 233 coff_section_data (abfd, sec)->contents = contents; 234 } 235 236 return TRUE; 237 } 238 239 /* Get the size required to hold the dynamic symbols. */ 240 241 long 242 _bfd_xcoff_get_dynamic_symtab_upper_bound (bfd *abfd) 243 { 244 asection *lsec; 245 bfd_byte *contents; 246 struct internal_ldhdr ldhdr; 247 248 if ((abfd->flags & DYNAMIC) == 0) 249 { 250 bfd_set_error (bfd_error_invalid_operation); 251 return -1; 252 } 253 254 lsec = bfd_get_section_by_name (abfd, ".loader"); 255 if (lsec == NULL) 256 { 257 bfd_set_error (bfd_error_no_symbols); 258 return -1; 259 } 260 261 if (! xcoff_get_section_contents (abfd, lsec)) 262 return -1; 263 contents = coff_section_data (abfd, lsec)->contents; 264 265 bfd_xcoff_swap_ldhdr_in (abfd, (void *) contents, &ldhdr); 266 267 return (ldhdr.l_nsyms + 1) * sizeof (asymbol *); 268 } 269 270 /* Get the dynamic symbols. */ 271 272 long 273 _bfd_xcoff_canonicalize_dynamic_symtab (bfd *abfd, asymbol **psyms) 274 { 275 asection *lsec; 276 bfd_byte *contents; 277 struct internal_ldhdr ldhdr; 278 const char *strings; 279 bfd_byte *elsym, *elsymend; 280 coff_symbol_type *symbuf; 281 282 if ((abfd->flags & DYNAMIC) == 0) 283 { 284 bfd_set_error (bfd_error_invalid_operation); 285 return -1; 286 } 287 288 lsec = bfd_get_section_by_name (abfd, ".loader"); 289 if (lsec == NULL) 290 { 291 bfd_set_error (bfd_error_no_symbols); 292 return -1; 293 } 294 295 if (! xcoff_get_section_contents (abfd, lsec)) 296 return -1; 297 contents = coff_section_data (abfd, lsec)->contents; 298 299 coff_section_data (abfd, lsec)->keep_contents = TRUE; 300 301 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); 302 303 strings = (char *) contents + ldhdr.l_stoff; 304 305 symbuf = bfd_zalloc (abfd, ldhdr.l_nsyms * sizeof (* symbuf)); 306 if (symbuf == NULL) 307 return -1; 308 309 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr); 310 311 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd); 312 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd), symbuf++, psyms++) 313 { 314 struct internal_ldsym ldsym; 315 316 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); 317 318 symbuf->symbol.the_bfd = abfd; 319 320 if (ldsym._l._l_l._l_zeroes == 0) 321 symbuf->symbol.name = strings + ldsym._l._l_l._l_offset; 322 else 323 { 324 char *c; 325 326 c = bfd_alloc (abfd, (bfd_size_type) SYMNMLEN + 1); 327 if (c == NULL) 328 return -1; 329 memcpy (c, ldsym._l._l_name, SYMNMLEN); 330 c[SYMNMLEN] = '\0'; 331 symbuf->symbol.name = c; 332 } 333 334 if (ldsym.l_smclas == XMC_XO) 335 symbuf->symbol.section = bfd_abs_section_ptr; 336 else 337 symbuf->symbol.section = coff_section_from_bfd_index (abfd, 338 ldsym.l_scnum); 339 symbuf->symbol.value = ldsym.l_value - symbuf->symbol.section->vma; 340 341 symbuf->symbol.flags = BSF_NO_FLAGS; 342 if ((ldsym.l_smtype & L_EXPORT) != 0) 343 { 344 if ((ldsym.l_smtype & L_WEAK) != 0) 345 symbuf->symbol.flags |= BSF_WEAK; 346 else 347 symbuf->symbol.flags |= BSF_GLOBAL; 348 } 349 350 /* FIXME: We have no way to record the other information stored 351 with the loader symbol. */ 352 *psyms = (asymbol *) symbuf; 353 } 354 355 *psyms = NULL; 356 357 return ldhdr.l_nsyms; 358 } 359 360 /* Get the size required to hold the dynamic relocs. */ 361 362 long 363 _bfd_xcoff_get_dynamic_reloc_upper_bound (bfd *abfd) 364 { 365 asection *lsec; 366 bfd_byte *contents; 367 struct internal_ldhdr ldhdr; 368 369 if ((abfd->flags & DYNAMIC) == 0) 370 { 371 bfd_set_error (bfd_error_invalid_operation); 372 return -1; 373 } 374 375 lsec = bfd_get_section_by_name (abfd, ".loader"); 376 if (lsec == NULL) 377 { 378 bfd_set_error (bfd_error_no_symbols); 379 return -1; 380 } 381 382 if (! xcoff_get_section_contents (abfd, lsec)) 383 return -1; 384 contents = coff_section_data (abfd, lsec)->contents; 385 386 bfd_xcoff_swap_ldhdr_in (abfd, (struct external_ldhdr *) contents, &ldhdr); 387 388 return (ldhdr.l_nreloc + 1) * sizeof (arelent *); 389 } 390 391 /* Get the dynamic relocs. */ 392 393 long 394 _bfd_xcoff_canonicalize_dynamic_reloc (bfd *abfd, 395 arelent **prelocs, 396 asymbol **syms) 397 { 398 asection *lsec; 399 bfd_byte *contents; 400 struct internal_ldhdr ldhdr; 401 arelent *relbuf; 402 bfd_byte *elrel, *elrelend; 403 404 if ((abfd->flags & DYNAMIC) == 0) 405 { 406 bfd_set_error (bfd_error_invalid_operation); 407 return -1; 408 } 409 410 lsec = bfd_get_section_by_name (abfd, ".loader"); 411 if (lsec == NULL) 412 { 413 bfd_set_error (bfd_error_no_symbols); 414 return -1; 415 } 416 417 if (! xcoff_get_section_contents (abfd, lsec)) 418 return -1; 419 contents = coff_section_data (abfd, lsec)->contents; 420 421 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); 422 423 relbuf = bfd_alloc (abfd, ldhdr.l_nreloc * sizeof (arelent)); 424 if (relbuf == NULL) 425 return -1; 426 427 elrel = contents + bfd_xcoff_loader_reloc_offset(abfd, &ldhdr); 428 429 elrelend = elrel + ldhdr.l_nreloc * bfd_xcoff_ldrelsz(abfd); 430 for (; elrel < elrelend; elrel += bfd_xcoff_ldrelsz(abfd), relbuf++, 431 prelocs++) 432 { 433 struct internal_ldrel ldrel; 434 435 bfd_xcoff_swap_ldrel_in (abfd, elrel, &ldrel); 436 437 if (ldrel.l_symndx >= 3) 438 relbuf->sym_ptr_ptr = syms + (ldrel.l_symndx - 3); 439 else 440 { 441 const char *name; 442 asection *sec; 443 444 switch (ldrel.l_symndx) 445 { 446 case 0: 447 name = ".text"; 448 break; 449 case 1: 450 name = ".data"; 451 break; 452 case 2: 453 name = ".bss"; 454 break; 455 default: 456 abort (); 457 break; 458 } 459 460 sec = bfd_get_section_by_name (abfd, name); 461 if (sec == NULL) 462 { 463 bfd_set_error (bfd_error_bad_value); 464 return -1; 465 } 466 467 relbuf->sym_ptr_ptr = sec->symbol_ptr_ptr; 468 } 469 470 relbuf->address = ldrel.l_vaddr; 471 relbuf->addend = 0; 472 473 /* Most dynamic relocs have the same type. FIXME: This is only 474 correct if ldrel.l_rtype == 0. In other cases, we should use 475 a different howto. */ 476 relbuf->howto = bfd_xcoff_dynamic_reloc_howto(abfd); 477 478 /* FIXME: We have no way to record the l_rsecnm field. */ 479 480 *prelocs = relbuf; 481 } 482 483 *prelocs = NULL; 484 485 return ldhdr.l_nreloc; 486 } 487 488 /* Hash functions for xcoff_link_hash_table's archive_info. */ 489 490 static hashval_t 491 xcoff_archive_info_hash (const void *data) 492 { 493 const struct xcoff_archive_info *info; 494 495 info = (const struct xcoff_archive_info *) data; 496 return htab_hash_pointer (info->archive); 497 } 498 499 static int 500 xcoff_archive_info_eq (const void *data1, const void *data2) 501 { 502 const struct xcoff_archive_info *info1; 503 const struct xcoff_archive_info *info2; 504 505 info1 = (const struct xcoff_archive_info *) data1; 506 info2 = (const struct xcoff_archive_info *) data2; 507 return info1->archive == info2->archive; 508 } 509 510 /* Return information about archive ARCHIVE. Return NULL on error. */ 511 512 static struct xcoff_archive_info * 513 xcoff_get_archive_info (struct bfd_link_info *info, bfd *archive) 514 { 515 struct xcoff_link_hash_table *htab; 516 struct xcoff_archive_info *entryp, entry; 517 void **slot; 518 519 htab = xcoff_hash_table (info); 520 entry.archive = archive; 521 slot = htab_find_slot (htab->archive_info, &entry, INSERT); 522 if (!slot) 523 return NULL; 524 525 entryp = *slot; 526 if (!entryp) 527 { 528 entryp = bfd_zalloc (archive, sizeof (entry)); 529 if (!entryp) 530 return NULL; 531 532 entryp->archive = archive; 533 *slot = entryp; 534 } 535 return entryp; 536 } 537 538 /* Routine to create an entry in an XCOFF link hash table. */ 539 540 static struct bfd_hash_entry * 541 xcoff_link_hash_newfunc (struct bfd_hash_entry *entry, 542 struct bfd_hash_table *table, 543 const char *string) 544 { 545 struct xcoff_link_hash_entry *ret = (struct xcoff_link_hash_entry *) entry; 546 547 /* Allocate the structure if it has not already been allocated by a 548 subclass. */ 549 if (ret == NULL) 550 ret = bfd_hash_allocate (table, sizeof (* ret)); 551 if (ret == NULL) 552 return NULL; 553 554 /* Call the allocation method of the superclass. */ 555 ret = ((struct xcoff_link_hash_entry *) 556 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret, 557 table, string)); 558 if (ret != NULL) 559 { 560 /* Set local fields. */ 561 ret->indx = -1; 562 ret->toc_section = NULL; 563 ret->u.toc_indx = -1; 564 ret->descriptor = NULL; 565 ret->ldsym = NULL; 566 ret->ldindx = -1; 567 ret->flags = 0; 568 ret->smclas = XMC_UA; 569 } 570 571 return (struct bfd_hash_entry *) ret; 572 } 573 574 /* Destroy an XCOFF link hash table. */ 575 576 static void 577 _bfd_xcoff_bfd_link_hash_table_free (bfd *obfd) 578 { 579 struct xcoff_link_hash_table *ret; 580 581 ret = (struct xcoff_link_hash_table *) obfd->link.hash; 582 if (ret->archive_info) 583 htab_delete (ret->archive_info); 584 if (ret->debug_strtab) 585 _bfd_stringtab_free (ret->debug_strtab); 586 _bfd_generic_link_hash_table_free (obfd); 587 } 588 589 /* Create an XCOFF link hash table. */ 590 591 struct bfd_link_hash_table * 592 _bfd_xcoff_bfd_link_hash_table_create (bfd *abfd) 593 { 594 struct xcoff_link_hash_table *ret; 595 bfd_size_type amt = sizeof (* ret); 596 597 ret = bfd_zmalloc (amt); 598 if (ret == NULL) 599 return NULL; 600 if (!_bfd_link_hash_table_init (&ret->root, abfd, xcoff_link_hash_newfunc, 601 sizeof (struct xcoff_link_hash_entry))) 602 { 603 free (ret); 604 return NULL; 605 } 606 607 ret->debug_strtab = _bfd_xcoff_stringtab_init (); 608 ret->archive_info = htab_create (37, xcoff_archive_info_hash, 609 xcoff_archive_info_eq, NULL); 610 if (!ret->debug_strtab || !ret->archive_info) 611 { 612 _bfd_xcoff_bfd_link_hash_table_free (abfd); 613 return NULL; 614 } 615 ret->root.hash_table_free = _bfd_xcoff_bfd_link_hash_table_free; 616 617 /* The linker will always generate a full a.out header. We need to 618 record that fact now, before the sizeof_headers routine could be 619 called. */ 620 xcoff_data (abfd)->full_aouthdr = TRUE; 621 622 return &ret->root; 623 } 624 625 /* Read internal relocs for an XCOFF csect. This is a wrapper around 626 _bfd_coff_read_internal_relocs which tries to take advantage of any 627 relocs which may have been cached for the enclosing section. */ 628 629 static struct internal_reloc * 630 xcoff_read_internal_relocs (bfd *abfd, 631 asection *sec, 632 bfd_boolean cache, 633 bfd_byte *external_relocs, 634 bfd_boolean require_internal, 635 struct internal_reloc *internal_relocs) 636 { 637 if (coff_section_data (abfd, sec) != NULL 638 && coff_section_data (abfd, sec)->relocs == NULL 639 && xcoff_section_data (abfd, sec) != NULL) 640 { 641 asection *enclosing; 642 643 enclosing = xcoff_section_data (abfd, sec)->enclosing; 644 645 if (enclosing != NULL 646 && (coff_section_data (abfd, enclosing) == NULL 647 || coff_section_data (abfd, enclosing)->relocs == NULL) 648 && cache 649 && enclosing->reloc_count > 0) 650 { 651 if (_bfd_coff_read_internal_relocs (abfd, enclosing, TRUE, 652 external_relocs, FALSE, NULL) 653 == NULL) 654 return NULL; 655 } 656 657 if (enclosing != NULL 658 && coff_section_data (abfd, enclosing) != NULL 659 && coff_section_data (abfd, enclosing)->relocs != NULL) 660 { 661 size_t off; 662 663 off = ((sec->rel_filepos - enclosing->rel_filepos) 664 / bfd_coff_relsz (abfd)); 665 666 if (! require_internal) 667 return coff_section_data (abfd, enclosing)->relocs + off; 668 memcpy (internal_relocs, 669 coff_section_data (abfd, enclosing)->relocs + off, 670 sec->reloc_count * sizeof (struct internal_reloc)); 671 return internal_relocs; 672 } 673 } 674 675 return _bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs, 676 require_internal, internal_relocs); 677 } 678 679 /* Split FILENAME into an import path and an import filename, 680 storing them in *IMPPATH and *IMPFILE respectively. */ 681 682 bfd_boolean 683 bfd_xcoff_split_import_path (bfd *abfd, const char *filename, 684 const char **imppath, const char **impfile) 685 { 686 const char *base; 687 size_t length; 688 char *path; 689 690 base = lbasename (filename); 691 length = base - filename; 692 if (length == 0) 693 /* The filename has no directory component, so use an empty path. */ 694 *imppath = ""; 695 else if (length == 1) 696 /* The filename is in the root directory. */ 697 *imppath = "/"; 698 else 699 { 700 /* Extract the (non-empty) directory part. Note that we don't 701 need to strip duplicate directory separators from any part 702 of the string; the native linker doesn't do that either. */ 703 path = bfd_alloc (abfd, length); 704 if (path == NULL) 705 return FALSE; 706 memcpy (path, filename, length - 1); 707 path[length - 1] = 0; 708 *imppath = path; 709 } 710 *impfile = base; 711 return TRUE; 712 } 713 714 /* Set ARCHIVE's import path as though its filename had been given 715 as FILENAME. */ 716 717 bfd_boolean 718 bfd_xcoff_set_archive_import_path (struct bfd_link_info *info, 719 bfd *archive, const char *filename) 720 { 721 struct xcoff_archive_info *archive_info; 722 723 archive_info = xcoff_get_archive_info (info, archive); 724 return (archive_info != NULL 725 && bfd_xcoff_split_import_path (archive, filename, 726 &archive_info->imppath, 727 &archive_info->impfile)); 728 } 729 730 /* H is an imported symbol. Set the import module's path, file and member 731 to IMPATH, IMPFILE and IMPMEMBER respectively. All three are null if 732 no specific import module is specified. */ 733 734 static bfd_boolean 735 xcoff_set_import_path (struct bfd_link_info *info, 736 struct xcoff_link_hash_entry *h, 737 const char *imppath, const char *impfile, 738 const char *impmember) 739 { 740 unsigned int c; 741 struct xcoff_import_file **pp; 742 743 /* We overload the ldindx field to hold the l_ifile value for this 744 symbol. */ 745 BFD_ASSERT (h->ldsym == NULL); 746 BFD_ASSERT ((h->flags & XCOFF_BUILT_LDSYM) == 0); 747 if (imppath == NULL) 748 h->ldindx = -1; 749 else 750 { 751 /* We start c at 1 because the first entry in the import list is 752 reserved for the library search path. */ 753 for (pp = &xcoff_hash_table (info)->imports, c = 1; 754 *pp != NULL; 755 pp = &(*pp)->next, ++c) 756 { 757 if (filename_cmp ((*pp)->path, imppath) == 0 758 && filename_cmp ((*pp)->file, impfile) == 0 759 && filename_cmp ((*pp)->member, impmember) == 0) 760 break; 761 } 762 763 if (*pp == NULL) 764 { 765 struct xcoff_import_file *n; 766 bfd_size_type amt = sizeof (* n); 767 768 n = bfd_alloc (info->output_bfd, amt); 769 if (n == NULL) 770 return FALSE; 771 n->next = NULL; 772 n->path = imppath; 773 n->file = impfile; 774 n->member = impmember; 775 *pp = n; 776 } 777 h->ldindx = c; 778 } 779 return TRUE; 780 } 781 782 /* H is the bfd symbol associated with exported .loader symbol LDSYM. 783 Return true if LDSYM defines H. */ 784 785 static bfd_boolean 786 xcoff_dynamic_definition_p (struct xcoff_link_hash_entry *h, 787 struct internal_ldsym *ldsym) 788 { 789 /* If we didn't know about H before processing LDSYM, LDSYM 790 definitely defines H. */ 791 if (h->root.type == bfd_link_hash_new) 792 return TRUE; 793 794 /* If H is currently a weak dynamic symbol, and if LDSYM is a strong 795 dynamic symbol, LDSYM trumps the current definition of H. */ 796 if ((ldsym->l_smtype & L_WEAK) == 0 797 && (h->flags & XCOFF_DEF_DYNAMIC) != 0 798 && (h->flags & XCOFF_DEF_REGULAR) == 0 799 && (h->root.type == bfd_link_hash_defweak 800 || h->root.type == bfd_link_hash_undefweak)) 801 return TRUE; 802 803 /* If H is currently undefined, LDSYM defines it. */ 804 if ((h->flags & XCOFF_DEF_DYNAMIC) == 0 805 && (h->root.type == bfd_link_hash_undefined 806 || h->root.type == bfd_link_hash_undefweak)) 807 return TRUE; 808 809 return FALSE; 810 } 811 812 /* This function is used to add symbols from a dynamic object to the 813 global symbol table. */ 814 815 static bfd_boolean 816 xcoff_link_add_dynamic_symbols (bfd *abfd, struct bfd_link_info *info) 817 { 818 asection *lsec; 819 bfd_byte *contents; 820 struct internal_ldhdr ldhdr; 821 const char *strings; 822 bfd_byte *elsym, *elsymend; 823 struct xcoff_import_file *n; 824 unsigned int c; 825 struct xcoff_import_file **pp; 826 827 /* We can only handle a dynamic object if we are generating an XCOFF 828 output file. */ 829 if (info->output_bfd->xvec != abfd->xvec) 830 { 831 _bfd_error_handler 832 (_("%pB: XCOFF shared object when not producing XCOFF output"), 833 abfd); 834 bfd_set_error (bfd_error_invalid_operation); 835 return FALSE; 836 } 837 838 /* The symbols we use from a dynamic object are not the symbols in 839 the normal symbol table, but, rather, the symbols in the export 840 table. If there is a global symbol in a dynamic object which is 841 not in the export table, the loader will not be able to find it, 842 so we don't want to find it either. Also, on AIX 4.1.3, shr.o in 843 libc.a has symbols in the export table which are not in the 844 symbol table. */ 845 846 /* Read in the .loader section. FIXME: We should really use the 847 o_snloader field in the a.out header, rather than grabbing the 848 section by name. */ 849 lsec = bfd_get_section_by_name (abfd, ".loader"); 850 if (lsec == NULL) 851 { 852 _bfd_error_handler 853 (_("%pB: dynamic object with no .loader section"), 854 abfd); 855 bfd_set_error (bfd_error_no_symbols); 856 return FALSE; 857 } 858 859 if (! xcoff_get_section_contents (abfd, lsec)) 860 return FALSE; 861 contents = coff_section_data (abfd, lsec)->contents; 862 863 /* Remove the sections from this object, so that they do not get 864 included in the link. */ 865 bfd_section_list_clear (abfd); 866 867 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); 868 869 strings = (char *) contents + ldhdr.l_stoff; 870 871 elsym = contents + bfd_xcoff_loader_symbol_offset(abfd, &ldhdr); 872 873 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz(abfd); 874 875 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz(abfd)) 876 { 877 struct internal_ldsym ldsym; 878 char nambuf[SYMNMLEN + 1]; 879 const char *name; 880 struct xcoff_link_hash_entry *h; 881 882 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); 883 884 /* We are only interested in exported symbols. */ 885 if ((ldsym.l_smtype & L_EXPORT) == 0) 886 continue; 887 888 if (ldsym._l._l_l._l_zeroes == 0) 889 name = strings + ldsym._l._l_l._l_offset; 890 else 891 { 892 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN); 893 nambuf[SYMNMLEN] = '\0'; 894 name = nambuf; 895 } 896 897 /* Normally we could not call xcoff_link_hash_lookup in an add 898 symbols routine, since we might not be using an XCOFF hash 899 table. However, we verified above that we are using an XCOFF 900 hash table. */ 901 902 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, 903 TRUE, TRUE); 904 if (h == NULL) 905 return FALSE; 906 907 if (!xcoff_dynamic_definition_p (h, &ldsym)) 908 continue; 909 910 h->flags |= XCOFF_DEF_DYNAMIC; 911 h->smclas = ldsym.l_smclas; 912 if (h->smclas == XMC_XO) 913 { 914 /* This symbol has an absolute value. */ 915 if ((ldsym.l_smtype & L_WEAK) != 0) 916 h->root.type = bfd_link_hash_defweak; 917 else 918 h->root.type = bfd_link_hash_defined; 919 h->root.u.def.section = bfd_abs_section_ptr; 920 h->root.u.def.value = ldsym.l_value; 921 } 922 else 923 { 924 /* Otherwise, we don't bother to actually define the symbol, 925 since we don't have a section to put it in anyhow. 926 We assume instead that an undefined XCOFF_DEF_DYNAMIC symbol 927 should be imported from the symbol's undef.abfd. */ 928 if ((ldsym.l_smtype & L_WEAK) != 0) 929 h->root.type = bfd_link_hash_undefweak; 930 else 931 h->root.type = bfd_link_hash_undefined; 932 h->root.u.undef.abfd = abfd; 933 } 934 935 /* If this symbol defines a function descriptor, then it 936 implicitly defines the function code as well. */ 937 if (h->smclas == XMC_DS 938 || (h->smclas == XMC_XO && name[0] != '.')) 939 h->flags |= XCOFF_DESCRIPTOR; 940 if ((h->flags & XCOFF_DESCRIPTOR) != 0) 941 { 942 struct xcoff_link_hash_entry *hds; 943 944 hds = h->descriptor; 945 if (hds == NULL) 946 { 947 char *dsnm; 948 949 dsnm = bfd_malloc ((bfd_size_type) strlen (name) + 2); 950 if (dsnm == NULL) 951 return FALSE; 952 dsnm[0] = '.'; 953 strcpy (dsnm + 1, name); 954 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), dsnm, 955 TRUE, TRUE, TRUE); 956 free (dsnm); 957 if (hds == NULL) 958 return FALSE; 959 960 hds->descriptor = h; 961 h->descriptor = hds; 962 } 963 964 if (xcoff_dynamic_definition_p (hds, &ldsym)) 965 { 966 hds->root.type = h->root.type; 967 hds->flags |= XCOFF_DEF_DYNAMIC; 968 if (h->smclas == XMC_XO) 969 { 970 /* An absolute symbol appears to actually define code, not a 971 function descriptor. This is how some math functions are 972 implemented on AIX 4.1. */ 973 hds->smclas = XMC_XO; 974 hds->root.u.def.section = bfd_abs_section_ptr; 975 hds->root.u.def.value = ldsym.l_value; 976 } 977 else 978 { 979 hds->smclas = XMC_PR; 980 hds->root.u.undef.abfd = abfd; 981 /* We do not want to add this to the undefined 982 symbol list. */ 983 } 984 } 985 } 986 } 987 988 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents) 989 { 990 free (coff_section_data (abfd, lsec)->contents); 991 coff_section_data (abfd, lsec)->contents = NULL; 992 } 993 994 /* Record this file in the import files. */ 995 n = bfd_alloc (abfd, (bfd_size_type) sizeof (struct xcoff_import_file)); 996 if (n == NULL) 997 return FALSE; 998 n->next = NULL; 999 1000 if (abfd->my_archive == NULL || bfd_is_thin_archive (abfd->my_archive)) 1001 { 1002 if (!bfd_xcoff_split_import_path (abfd, abfd->filename, 1003 &n->path, &n->file)) 1004 return FALSE; 1005 n->member = ""; 1006 } 1007 else 1008 { 1009 struct xcoff_archive_info *archive_info; 1010 1011 archive_info = xcoff_get_archive_info (info, abfd->my_archive); 1012 if (!archive_info->impfile) 1013 { 1014 if (!bfd_xcoff_split_import_path (archive_info->archive, 1015 archive_info->archive->filename, 1016 &archive_info->imppath, 1017 &archive_info->impfile)) 1018 return FALSE; 1019 } 1020 n->path = archive_info->imppath; 1021 n->file = archive_info->impfile; 1022 n->member = bfd_get_filename (abfd); 1023 } 1024 1025 /* We start c at 1 because the first import file number is reserved 1026 for LIBPATH. */ 1027 for (pp = &xcoff_hash_table (info)->imports, c = 1; 1028 *pp != NULL; 1029 pp = &(*pp)->next, ++c) 1030 ; 1031 *pp = n; 1032 1033 xcoff_data (abfd)->import_file_id = c; 1034 1035 return TRUE; 1036 } 1037 1038 /* xcoff_link_create_extra_sections 1039 1040 Takes care of creating the .loader, .gl, .ds, .debug and sections. */ 1041 1042 static bfd_boolean 1043 xcoff_link_create_extra_sections (bfd * abfd, struct bfd_link_info *info) 1044 { 1045 bfd_boolean return_value = FALSE; 1046 1047 if (info->output_bfd->xvec == abfd->xvec) 1048 { 1049 /* We need to build a .loader section, so we do it here. This 1050 won't work if we're producing an XCOFF output file with no 1051 XCOFF input files. FIXME. */ 1052 1053 if (!bfd_link_relocatable (info) 1054 && xcoff_hash_table (info)->loader_section == NULL) 1055 { 1056 asection *lsec; 1057 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY; 1058 1059 lsec = bfd_make_section_anyway_with_flags (abfd, ".loader", flags); 1060 if (lsec == NULL) 1061 goto end_return; 1062 1063 xcoff_hash_table (info)->loader_section = lsec; 1064 } 1065 1066 /* Likewise for the linkage section. */ 1067 if (xcoff_hash_table (info)->linkage_section == NULL) 1068 { 1069 asection *lsec; 1070 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS 1071 | SEC_IN_MEMORY); 1072 1073 lsec = bfd_make_section_anyway_with_flags (abfd, ".gl", flags); 1074 if (lsec == NULL) 1075 goto end_return; 1076 1077 xcoff_hash_table (info)->linkage_section = lsec; 1078 lsec->alignment_power = 2; 1079 } 1080 1081 /* Likewise for the TOC section. */ 1082 if (xcoff_hash_table (info)->toc_section == NULL) 1083 { 1084 asection *tsec; 1085 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS 1086 | SEC_IN_MEMORY); 1087 1088 tsec = bfd_make_section_anyway_with_flags (abfd, ".tc", flags); 1089 if (tsec == NULL) 1090 goto end_return; 1091 1092 xcoff_hash_table (info)->toc_section = tsec; 1093 tsec->alignment_power = 2; 1094 } 1095 1096 /* Likewise for the descriptor section. */ 1097 if (xcoff_hash_table (info)->descriptor_section == NULL) 1098 { 1099 asection *dsec; 1100 flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS 1101 | SEC_IN_MEMORY); 1102 1103 dsec = bfd_make_section_anyway_with_flags (abfd, ".ds", flags); 1104 if (dsec == NULL) 1105 goto end_return; 1106 1107 xcoff_hash_table (info)->descriptor_section = dsec; 1108 dsec->alignment_power = 2; 1109 } 1110 1111 /* Likewise for the .debug section. */ 1112 if (xcoff_hash_table (info)->debug_section == NULL 1113 && info->strip != strip_all) 1114 { 1115 asection *dsec; 1116 flagword flags = SEC_HAS_CONTENTS | SEC_IN_MEMORY; 1117 1118 dsec = bfd_make_section_anyway_with_flags (abfd, ".debug", flags); 1119 if (dsec == NULL) 1120 goto end_return; 1121 1122 xcoff_hash_table (info)->debug_section = dsec; 1123 } 1124 } 1125 1126 return_value = TRUE; 1127 1128 end_return: 1129 1130 return return_value; 1131 } 1132 1133 /* Returns the index of reloc in RELOCS with the least address greater 1134 than or equal to ADDRESS. The relocs are sorted by address. */ 1135 1136 static bfd_size_type 1137 xcoff_find_reloc (struct internal_reloc *relocs, 1138 bfd_size_type count, 1139 bfd_vma address) 1140 { 1141 bfd_size_type min, max, this; 1142 1143 if (count < 2) 1144 { 1145 if (count == 1 && relocs[0].r_vaddr < address) 1146 return 1; 1147 else 1148 return 0; 1149 } 1150 1151 min = 0; 1152 max = count; 1153 1154 /* Do a binary search over (min,max]. */ 1155 while (min + 1 < max) 1156 { 1157 bfd_vma raddr; 1158 1159 this = (max + min) / 2; 1160 raddr = relocs[this].r_vaddr; 1161 if (raddr > address) 1162 max = this; 1163 else if (raddr < address) 1164 min = this; 1165 else 1166 { 1167 min = this; 1168 break; 1169 } 1170 } 1171 1172 if (relocs[min].r_vaddr < address) 1173 return min + 1; 1174 1175 while (min > 0 1176 && relocs[min - 1].r_vaddr == address) 1177 --min; 1178 1179 return min; 1180 } 1181 1182 /* Add all the symbols from an object file to the hash table. 1183 1184 XCOFF is a weird format. A normal XCOFF .o files will have three 1185 COFF sections--.text, .data, and .bss--but each COFF section will 1186 contain many csects. These csects are described in the symbol 1187 table. From the linker's point of view, each csect must be 1188 considered a section in its own right. For example, a TOC entry is 1189 handled as a small XMC_TC csect. The linker must be able to merge 1190 different TOC entries together, which means that it must be able to 1191 extract the XMC_TC csects from the .data section of the input .o 1192 file. 1193 1194 From the point of view of our linker, this is, of course, a hideous 1195 nightmare. We cope by actually creating sections for each csect, 1196 and discarding the original sections. We then have to handle the 1197 relocation entries carefully, since the only way to tell which 1198 csect they belong to is to examine the address. */ 1199 1200 static bfd_boolean 1201 xcoff_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 1202 { 1203 unsigned int n_tmask; 1204 unsigned int n_btshft; 1205 bfd_boolean default_copy; 1206 bfd_size_type symcount; 1207 struct xcoff_link_hash_entry **sym_hash; 1208 asection **csect_cache; 1209 unsigned int *lineno_counts; 1210 bfd_size_type linesz; 1211 asection *o; 1212 asection *last_real; 1213 bfd_boolean keep_syms; 1214 asection *csect; 1215 unsigned int csect_index; 1216 asection *first_csect; 1217 bfd_size_type symesz; 1218 bfd_byte *esym; 1219 bfd_byte *esym_end; 1220 struct reloc_info_struct 1221 { 1222 struct internal_reloc *relocs; 1223 asection **csects; 1224 bfd_byte *linenos; 1225 } *reloc_info = NULL; 1226 bfd_size_type amt; 1227 1228 keep_syms = obj_coff_keep_syms (abfd); 1229 1230 if ((abfd->flags & DYNAMIC) != 0 1231 && ! info->static_link) 1232 { 1233 if (! xcoff_link_add_dynamic_symbols (abfd, info)) 1234 return FALSE; 1235 } 1236 1237 /* Create the loader, toc, gl, ds and debug sections, if needed. */ 1238 if (! xcoff_link_create_extra_sections (abfd, info)) 1239 goto error_return; 1240 1241 if ((abfd->flags & DYNAMIC) != 0 1242 && ! info->static_link) 1243 return TRUE; 1244 1245 n_tmask = coff_data (abfd)->local_n_tmask; 1246 n_btshft = coff_data (abfd)->local_n_btshft; 1247 1248 /* Define macros so that ISFCN, et. al., macros work correctly. */ 1249 #define N_TMASK n_tmask 1250 #define N_BTSHFT n_btshft 1251 1252 if (info->keep_memory) 1253 default_copy = FALSE; 1254 else 1255 default_copy = TRUE; 1256 1257 symcount = obj_raw_syment_count (abfd); 1258 1259 /* We keep a list of the linker hash table entries that correspond 1260 to each external symbol. */ 1261 amt = symcount * sizeof (struct xcoff_link_hash_entry *); 1262 sym_hash = bfd_zalloc (abfd, amt); 1263 if (sym_hash == NULL && symcount != 0) 1264 goto error_return; 1265 coff_data (abfd)->sym_hashes = (struct coff_link_hash_entry **) sym_hash; 1266 1267 /* Because of the weird stuff we are doing with XCOFF csects, we can 1268 not easily determine which section a symbol is in, so we store 1269 the information in the tdata for the input file. */ 1270 amt = symcount * sizeof (asection *); 1271 csect_cache = bfd_zalloc (abfd, amt); 1272 if (csect_cache == NULL && symcount != 0) 1273 goto error_return; 1274 xcoff_data (abfd)->csects = csect_cache; 1275 1276 /* We garbage-collect line-number information on a symbol-by-symbol 1277 basis, so we need to have quick access to the number of entries 1278 per symbol. */ 1279 amt = symcount * sizeof (unsigned int); 1280 lineno_counts = bfd_zalloc (abfd, amt); 1281 if (lineno_counts == NULL && symcount != 0) 1282 goto error_return; 1283 xcoff_data (abfd)->lineno_counts = lineno_counts; 1284 1285 /* While splitting sections into csects, we need to assign the 1286 relocs correctly. The relocs and the csects must both be in 1287 order by VMA within a given section, so we handle this by 1288 scanning along the relocs as we process the csects. We index 1289 into reloc_info using the section target_index. */ 1290 amt = abfd->section_count + 1; 1291 amt *= sizeof (struct reloc_info_struct); 1292 reloc_info = bfd_zmalloc (amt); 1293 if (reloc_info == NULL) 1294 goto error_return; 1295 1296 /* Read in the relocs and line numbers for each section. */ 1297 linesz = bfd_coff_linesz (abfd); 1298 last_real = NULL; 1299 for (o = abfd->sections; o != NULL; o = o->next) 1300 { 1301 last_real = o; 1302 1303 if ((o->flags & SEC_RELOC) != 0) 1304 { 1305 reloc_info[o->target_index].relocs = 1306 xcoff_read_internal_relocs (abfd, o, TRUE, NULL, FALSE, NULL); 1307 amt = o->reloc_count; 1308 amt *= sizeof (asection *); 1309 reloc_info[o->target_index].csects = bfd_zmalloc (amt); 1310 if (reloc_info[o->target_index].csects == NULL) 1311 goto error_return; 1312 } 1313 1314 if ((info->strip == strip_none || info->strip == strip_some) 1315 && o->lineno_count > 0) 1316 { 1317 bfd_byte *linenos; 1318 1319 amt = linesz * o->lineno_count; 1320 linenos = bfd_malloc (amt); 1321 if (linenos == NULL) 1322 goto error_return; 1323 reloc_info[o->target_index].linenos = linenos; 1324 if (bfd_seek (abfd, o->line_filepos, SEEK_SET) != 0 1325 || bfd_bread (linenos, amt, abfd) != amt) 1326 goto error_return; 1327 } 1328 } 1329 1330 /* Don't let the linker relocation routines discard the symbols. */ 1331 obj_coff_keep_syms (abfd) = TRUE; 1332 1333 csect = NULL; 1334 csect_index = 0; 1335 first_csect = NULL; 1336 1337 symesz = bfd_coff_symesz (abfd); 1338 BFD_ASSERT (symesz == bfd_coff_auxesz (abfd)); 1339 esym = (bfd_byte *) obj_coff_external_syms (abfd); 1340 esym_end = esym + symcount * symesz; 1341 1342 while (esym < esym_end) 1343 { 1344 struct internal_syment sym; 1345 union internal_auxent aux; 1346 const char *name; 1347 char buf[SYMNMLEN + 1]; 1348 int smtyp; 1349 asection *section; 1350 bfd_vma value; 1351 struct xcoff_link_hash_entry *set_toc; 1352 1353 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym); 1354 1355 /* In this pass we are only interested in symbols with csect 1356 information. */ 1357 if (!CSECT_SYM_P (sym.n_sclass)) 1358 { 1359 /* Set csect_cache, 1360 Normally csect is a .pr, .rw etc. created in the loop 1361 If C_FILE or first time, handle special 1362 1363 Advance esym, sym_hash, csect_hash ptrs. */ 1364 if (sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF) 1365 csect = NULL; 1366 if (csect != NULL) 1367 *csect_cache = csect; 1368 else if (first_csect == NULL 1369 || sym.n_sclass == C_FILE || sym.n_sclass == C_DWARF) 1370 *csect_cache = coff_section_from_bfd_index (abfd, sym.n_scnum); 1371 else 1372 *csect_cache = NULL; 1373 esym += (sym.n_numaux + 1) * symesz; 1374 sym_hash += sym.n_numaux + 1; 1375 csect_cache += sym.n_numaux + 1; 1376 lineno_counts += sym.n_numaux + 1; 1377 1378 continue; 1379 } 1380 1381 name = _bfd_coff_internal_syment_name (abfd, &sym, buf); 1382 1383 if (name == NULL) 1384 goto error_return; 1385 1386 /* If this symbol has line number information attached to it, 1387 and we're not stripping it, count the number of entries and 1388 add them to the count for this csect. In the final link pass 1389 we are going to attach line number information by symbol, 1390 rather than by section, in order to more easily handle 1391 garbage collection. */ 1392 if ((info->strip == strip_none || info->strip == strip_some) 1393 && sym.n_numaux > 1 1394 && csect != NULL 1395 && ISFCN (sym.n_type)) 1396 { 1397 union internal_auxent auxlin; 1398 1399 bfd_coff_swap_aux_in (abfd, (void *) (esym + symesz), 1400 sym.n_type, sym.n_sclass, 1401 0, sym.n_numaux, (void *) &auxlin); 1402 1403 if (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) 1404 { 1405 asection *enclosing; 1406 bfd_signed_vma linoff; 1407 1408 enclosing = xcoff_section_data (abfd, csect)->enclosing; 1409 if (enclosing == NULL) 1410 { 1411 _bfd_error_handler 1412 /* xgettext:c-format */ 1413 (_("%pB: `%s' has line numbers but no enclosing section"), 1414 abfd, name); 1415 bfd_set_error (bfd_error_bad_value); 1416 goto error_return; 1417 } 1418 linoff = (auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr 1419 - enclosing->line_filepos); 1420 /* Explicit cast to bfd_signed_vma for compiler. */ 1421 if (linoff < (bfd_signed_vma) (enclosing->lineno_count * linesz)) 1422 { 1423 struct internal_lineno lin; 1424 bfd_byte *linpstart; 1425 1426 linpstart = (reloc_info[enclosing->target_index].linenos 1427 + linoff); 1428 bfd_coff_swap_lineno_in (abfd, (void *) linpstart, (void *) &lin); 1429 if (lin.l_lnno == 0 1430 && ((bfd_size_type) lin.l_addr.l_symndx 1431 == ((esym 1432 - (bfd_byte *) obj_coff_external_syms (abfd)) 1433 / symesz))) 1434 { 1435 bfd_byte *linpend, *linp; 1436 1437 linpend = (reloc_info[enclosing->target_index].linenos 1438 + enclosing->lineno_count * linesz); 1439 for (linp = linpstart + linesz; 1440 linp < linpend; 1441 linp += linesz) 1442 { 1443 bfd_coff_swap_lineno_in (abfd, (void *) linp, 1444 (void *) &lin); 1445 if (lin.l_lnno == 0) 1446 break; 1447 } 1448 *lineno_counts = (linp - linpstart) / linesz; 1449 /* The setting of line_filepos will only be 1450 useful if all the line number entries for a 1451 csect are contiguous; this only matters for 1452 error reporting. */ 1453 if (csect->line_filepos == 0) 1454 csect->line_filepos = 1455 auxlin.x_sym.x_fcnary.x_fcn.x_lnnoptr; 1456 } 1457 } 1458 } 1459 } 1460 1461 /* Pick up the csect auxiliary information. */ 1462 if (sym.n_numaux == 0) 1463 { 1464 _bfd_error_handler 1465 /* xgettext:c-format */ 1466 (_("%pB: class %d symbol `%s' has no aux entries"), 1467 abfd, sym.n_sclass, name); 1468 bfd_set_error (bfd_error_bad_value); 1469 goto error_return; 1470 } 1471 1472 bfd_coff_swap_aux_in (abfd, 1473 (void *) (esym + symesz * sym.n_numaux), 1474 sym.n_type, sym.n_sclass, 1475 sym.n_numaux - 1, sym.n_numaux, 1476 (void *) &aux); 1477 1478 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); 1479 1480 section = NULL; 1481 value = 0; 1482 set_toc = NULL; 1483 1484 switch (smtyp) 1485 { 1486 default: 1487 _bfd_error_handler 1488 /* xgettext:c-format */ 1489 (_("%pB: symbol `%s' has unrecognized csect type %d"), 1490 abfd, name, smtyp); 1491 bfd_set_error (bfd_error_bad_value); 1492 goto error_return; 1493 1494 case XTY_ER: 1495 /* This is an external reference. */ 1496 if (sym.n_sclass == C_HIDEXT 1497 || sym.n_scnum != N_UNDEF 1498 || aux.x_csect.x_scnlen.l != 0) 1499 { 1500 _bfd_error_handler 1501 /* xgettext:c-format */ 1502 (_("%pB: bad XTY_ER symbol `%s': class %d scnum %d " 1503 "scnlen %" PRId64), 1504 abfd, name, sym.n_sclass, sym.n_scnum, 1505 (int64_t) aux.x_csect.x_scnlen.l); 1506 bfd_set_error (bfd_error_bad_value); 1507 goto error_return; 1508 } 1509 1510 /* An XMC_XO external reference is actually a reference to 1511 an absolute location. */ 1512 if (aux.x_csect.x_smclas != XMC_XO) 1513 section = bfd_und_section_ptr; 1514 else 1515 { 1516 section = bfd_abs_section_ptr; 1517 value = sym.n_value; 1518 } 1519 break; 1520 1521 case XTY_SD: 1522 csect = NULL; 1523 csect_index = -(unsigned) 1; 1524 1525 /* When we see a TOC anchor, we record the TOC value. */ 1526 if (aux.x_csect.x_smclas == XMC_TC0) 1527 { 1528 if (sym.n_sclass != C_HIDEXT 1529 || aux.x_csect.x_scnlen.l != 0) 1530 { 1531 _bfd_error_handler 1532 /* xgettext:c-format */ 1533 (_("%pB: XMC_TC0 symbol `%s' is class %d scnlen %" PRId64), 1534 abfd, name, sym.n_sclass, (int64_t) aux.x_csect.x_scnlen.l); 1535 bfd_set_error (bfd_error_bad_value); 1536 goto error_return; 1537 } 1538 xcoff_data (abfd)->toc = sym.n_value; 1539 } 1540 1541 /* We must merge TOC entries for the same symbol. We can 1542 merge two TOC entries if they are both C_HIDEXT, they 1543 both have the same name, they are both 4 or 8 bytes long, and 1544 they both have a relocation table entry for an external 1545 symbol with the same name. Unfortunately, this means 1546 that we must look through the relocations. Ick. 1547 1548 Logic for 32 bit vs 64 bit. 1549 32 bit has a csect length of 4 for TOC 1550 64 bit has a csect length of 8 for TOC 1551 1552 The conditions to get past the if-check are not that bad. 1553 They are what is used to create the TOC csects in the first 1554 place. */ 1555 if (aux.x_csect.x_smclas == XMC_TC 1556 && sym.n_sclass == C_HIDEXT 1557 && info->output_bfd->xvec == abfd->xvec 1558 && ((bfd_xcoff_is_xcoff32 (abfd) 1559 && aux.x_csect.x_scnlen.l == 4) 1560 || (bfd_xcoff_is_xcoff64 (abfd) 1561 && aux.x_csect.x_scnlen.l == 8))) 1562 { 1563 asection *enclosing; 1564 struct internal_reloc *relocs; 1565 bfd_size_type relindx; 1566 struct internal_reloc *rel; 1567 1568 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); 1569 if (enclosing == NULL) 1570 goto error_return; 1571 1572 relocs = reloc_info[enclosing->target_index].relocs; 1573 amt = enclosing->reloc_count; 1574 relindx = xcoff_find_reloc (relocs, amt, sym.n_value); 1575 rel = relocs + relindx; 1576 1577 /* 32 bit R_POS r_size is 31 1578 64 bit R_POS r_size is 63 */ 1579 if (relindx < enclosing->reloc_count 1580 && rel->r_vaddr == (bfd_vma) sym.n_value 1581 && rel->r_type == R_POS 1582 && ((bfd_xcoff_is_xcoff32 (abfd) 1583 && rel->r_size == 31) 1584 || (bfd_xcoff_is_xcoff64 (abfd) 1585 && rel->r_size == 63))) 1586 { 1587 bfd_byte *erelsym; 1588 1589 struct internal_syment relsym; 1590 1591 erelsym = ((bfd_byte *) obj_coff_external_syms (abfd) 1592 + rel->r_symndx * symesz); 1593 bfd_coff_swap_sym_in (abfd, (void *) erelsym, (void *) &relsym); 1594 if (EXTERN_SYM_P (relsym.n_sclass)) 1595 { 1596 const char *relname; 1597 char relbuf[SYMNMLEN + 1]; 1598 bfd_boolean copy; 1599 struct xcoff_link_hash_entry *h; 1600 1601 /* At this point we know that the TOC entry is 1602 for an externally visible symbol. */ 1603 relname = _bfd_coff_internal_syment_name (abfd, &relsym, 1604 relbuf); 1605 if (relname == NULL) 1606 goto error_return; 1607 1608 /* We only merge TOC entries if the TC name is 1609 the same as the symbol name. This handles 1610 the normal case, but not common cases like 1611 SYM.P4 which gcc generates to store SYM + 4 1612 in the TOC. FIXME. */ 1613 if (strcmp (name, relname) == 0) 1614 { 1615 copy = (! info->keep_memory 1616 || relsym._n._n_n._n_zeroes != 0 1617 || relsym._n._n_n._n_offset == 0); 1618 h = xcoff_link_hash_lookup (xcoff_hash_table (info), 1619 relname, TRUE, copy, 1620 FALSE); 1621 if (h == NULL) 1622 goto error_return; 1623 1624 /* At this point h->root.type could be 1625 bfd_link_hash_new. That should be OK, 1626 since we know for sure that we will come 1627 across this symbol as we step through the 1628 file. */ 1629 1630 /* We store h in *sym_hash for the 1631 convenience of the relocate_section 1632 function. */ 1633 *sym_hash = h; 1634 1635 if (h->toc_section != NULL) 1636 { 1637 asection **rel_csects; 1638 1639 /* We already have a TOC entry for this 1640 symbol, so we can just ignore this 1641 one. */ 1642 rel_csects = 1643 reloc_info[enclosing->target_index].csects; 1644 rel_csects[relindx] = bfd_und_section_ptr; 1645 break; 1646 } 1647 1648 /* We are about to create a TOC entry for 1649 this symbol. */ 1650 set_toc = h; 1651 } 1652 } 1653 } 1654 } 1655 1656 { 1657 asection *enclosing; 1658 1659 /* We need to create a new section. We get the name from 1660 the csect storage mapping class, so that the linker can 1661 accumulate similar csects together. */ 1662 1663 csect = bfd_xcoff_create_csect_from_smclas(abfd, &aux, name); 1664 if (NULL == csect) 1665 goto error_return; 1666 1667 /* The enclosing section is the main section : .data, .text 1668 or .bss that the csect is coming from. */ 1669 enclosing = coff_section_from_bfd_index (abfd, sym.n_scnum); 1670 if (enclosing == NULL) 1671 goto error_return; 1672 1673 if (! bfd_is_abs_section (enclosing) 1674 && ((bfd_vma) sym.n_value < enclosing->vma 1675 || ((bfd_vma) sym.n_value + aux.x_csect.x_scnlen.l 1676 > enclosing->vma + enclosing->size))) 1677 { 1678 _bfd_error_handler 1679 /* xgettext:c-format */ 1680 (_("%pB: csect `%s' not in enclosing section"), 1681 abfd, name); 1682 bfd_set_error (bfd_error_bad_value); 1683 goto error_return; 1684 } 1685 csect->vma = sym.n_value; 1686 csect->filepos = (enclosing->filepos 1687 + sym.n_value 1688 - enclosing->vma); 1689 csect->size = aux.x_csect.x_scnlen.l; 1690 csect->flags |= SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS; 1691 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); 1692 1693 /* Record the enclosing section in the tdata for this new 1694 section. */ 1695 amt = sizeof (struct coff_section_tdata); 1696 csect->used_by_bfd = bfd_zalloc (abfd, amt); 1697 if (csect->used_by_bfd == NULL) 1698 goto error_return; 1699 amt = sizeof (struct xcoff_section_tdata); 1700 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt); 1701 if (coff_section_data (abfd, csect)->tdata == NULL) 1702 goto error_return; 1703 xcoff_section_data (abfd, csect)->enclosing = enclosing; 1704 xcoff_section_data (abfd, csect)->lineno_count = 1705 enclosing->lineno_count; 1706 1707 if (enclosing->owner == abfd) 1708 { 1709 struct internal_reloc *relocs; 1710 bfd_size_type relindx; 1711 struct internal_reloc *rel; 1712 asection **rel_csect; 1713 1714 relocs = reloc_info[enclosing->target_index].relocs; 1715 amt = enclosing->reloc_count; 1716 relindx = xcoff_find_reloc (relocs, amt, csect->vma); 1717 1718 rel = relocs + relindx; 1719 rel_csect = (reloc_info[enclosing->target_index].csects 1720 + relindx); 1721 1722 csect->rel_filepos = (enclosing->rel_filepos 1723 + relindx * bfd_coff_relsz (abfd)); 1724 while (relindx < enclosing->reloc_count 1725 && *rel_csect == NULL 1726 && rel->r_vaddr < csect->vma + csect->size) 1727 { 1728 1729 *rel_csect = csect; 1730 csect->flags |= SEC_RELOC; 1731 ++csect->reloc_count; 1732 ++relindx; 1733 ++rel; 1734 ++rel_csect; 1735 } 1736 } 1737 1738 /* There are a number of other fields and section flags 1739 which we do not bother to set. */ 1740 1741 csect_index = ((esym 1742 - (bfd_byte *) obj_coff_external_syms (abfd)) 1743 / symesz); 1744 1745 xcoff_section_data (abfd, csect)->first_symndx = csect_index; 1746 1747 if (first_csect == NULL) 1748 first_csect = csect; 1749 1750 /* If this symbol is external, we treat it as starting at the 1751 beginning of the newly created section. */ 1752 if (EXTERN_SYM_P (sym.n_sclass)) 1753 { 1754 section = csect; 1755 value = 0; 1756 } 1757 1758 /* If this is a TOC section for a symbol, record it. */ 1759 if (set_toc != NULL) 1760 set_toc->toc_section = csect; 1761 } 1762 break; 1763 1764 case XTY_LD: 1765 /* This is a label definition. The x_scnlen field is the 1766 symbol index of the csect. Usually the XTY_LD symbol will 1767 follow its appropriate XTY_SD symbol. The .set pseudo op can 1768 cause the XTY_LD to not follow the XTY_SD symbol. */ 1769 { 1770 bfd_boolean bad; 1771 1772 bad = FALSE; 1773 if (aux.x_csect.x_scnlen.l < 0 1774 || (aux.x_csect.x_scnlen.l 1775 >= esym - (bfd_byte *) obj_coff_external_syms (abfd))) 1776 bad = TRUE; 1777 if (! bad) 1778 { 1779 section = xcoff_data (abfd)->csects[aux.x_csect.x_scnlen.l]; 1780 if (section == NULL 1781 || (section->flags & SEC_HAS_CONTENTS) == 0) 1782 bad = TRUE; 1783 } 1784 if (bad) 1785 { 1786 _bfd_error_handler 1787 /* xgettext:c-format */ 1788 (_("%pB: misplaced XTY_LD `%s'"), 1789 abfd, name); 1790 bfd_set_error (bfd_error_bad_value); 1791 goto error_return; 1792 } 1793 csect = section; 1794 value = sym.n_value - csect->vma; 1795 } 1796 break; 1797 1798 case XTY_CM: 1799 /* This is an unitialized csect. We could base the name on 1800 the storage mapping class, but we don't bother except for 1801 an XMC_TD symbol. If this csect is externally visible, 1802 it is a common symbol. We put XMC_TD symbols in sections 1803 named .tocbss, and rely on the linker script to put that 1804 in the TOC area. */ 1805 1806 if (aux.x_csect.x_smclas == XMC_TD) 1807 { 1808 /* The linker script puts the .td section in the data 1809 section after the .tc section. */ 1810 csect = bfd_make_section_anyway_with_flags (abfd, ".td", 1811 SEC_ALLOC); 1812 } 1813 else 1814 csect = bfd_make_section_anyway_with_flags (abfd, ".bss", 1815 SEC_ALLOC); 1816 1817 if (csect == NULL) 1818 goto error_return; 1819 csect->vma = sym.n_value; 1820 csect->size = aux.x_csect.x_scnlen.l; 1821 csect->alignment_power = SMTYP_ALIGN (aux.x_csect.x_smtyp); 1822 /* There are a number of other fields and section flags 1823 which we do not bother to set. */ 1824 1825 csect_index = ((esym 1826 - (bfd_byte *) obj_coff_external_syms (abfd)) 1827 / symesz); 1828 1829 amt = sizeof (struct coff_section_tdata); 1830 csect->used_by_bfd = bfd_zalloc (abfd, amt); 1831 if (csect->used_by_bfd == NULL) 1832 goto error_return; 1833 amt = sizeof (struct xcoff_section_tdata); 1834 coff_section_data (abfd, csect)->tdata = bfd_zalloc (abfd, amt); 1835 if (coff_section_data (abfd, csect)->tdata == NULL) 1836 goto error_return; 1837 xcoff_section_data (abfd, csect)->first_symndx = csect_index; 1838 1839 if (first_csect == NULL) 1840 first_csect = csect; 1841 1842 if (EXTERN_SYM_P (sym.n_sclass)) 1843 { 1844 csect->flags |= SEC_IS_COMMON; 1845 csect->size = 0; 1846 section = csect; 1847 value = aux.x_csect.x_scnlen.l; 1848 } 1849 1850 break; 1851 } 1852 1853 /* Check for magic symbol names. */ 1854 if ((smtyp == XTY_SD || smtyp == XTY_CM) 1855 && aux.x_csect.x_smclas != XMC_TC 1856 && aux.x_csect.x_smclas != XMC_TD) 1857 { 1858 int i = -1; 1859 1860 if (name[0] == '_') 1861 { 1862 if (strcmp (name, "_text") == 0) 1863 i = XCOFF_SPECIAL_SECTION_TEXT; 1864 else if (strcmp (name, "_etext") == 0) 1865 i = XCOFF_SPECIAL_SECTION_ETEXT; 1866 else if (strcmp (name, "_data") == 0) 1867 i = XCOFF_SPECIAL_SECTION_DATA; 1868 else if (strcmp (name, "_edata") == 0) 1869 i = XCOFF_SPECIAL_SECTION_EDATA; 1870 else if (strcmp (name, "_end") == 0) 1871 i = XCOFF_SPECIAL_SECTION_END; 1872 } 1873 else if (name[0] == 'e' && strcmp (name, "end") == 0) 1874 i = XCOFF_SPECIAL_SECTION_END2; 1875 1876 if (i != -1) 1877 xcoff_hash_table (info)->special_sections[i] = csect; 1878 } 1879 1880 /* Now we have enough information to add the symbol to the 1881 linker hash table. */ 1882 1883 if (EXTERN_SYM_P (sym.n_sclass)) 1884 { 1885 bfd_boolean copy; 1886 flagword flags; 1887 1888 BFD_ASSERT (section != NULL); 1889 1890 /* We must copy the name into memory if we got it from the 1891 syment itself, rather than the string table. */ 1892 copy = default_copy; 1893 if (sym._n._n_n._n_zeroes != 0 1894 || sym._n._n_n._n_offset == 0) 1895 copy = TRUE; 1896 1897 /* Ignore global linkage code when linking statically. */ 1898 if (info->static_link 1899 && (smtyp == XTY_SD || smtyp == XTY_LD) 1900 && aux.x_csect.x_smclas == XMC_GL) 1901 { 1902 section = bfd_und_section_ptr; 1903 value = 0; 1904 } 1905 1906 /* The AIX linker appears to only detect multiple symbol 1907 definitions when there is a reference to the symbol. If 1908 a symbol is defined multiple times, and the only 1909 references are from the same object file, the AIX linker 1910 appears to permit it. It does not merge the different 1911 definitions, but handles them independently. On the 1912 other hand, if there is a reference, the linker reports 1913 an error. 1914 1915 This matters because the AIX <net/net_globals.h> header 1916 file actually defines an initialized array, so we have to 1917 actually permit that to work. 1918 1919 Just to make matters even more confusing, the AIX linker 1920 appears to permit multiple symbol definitions whenever 1921 the second definition is in an archive rather than an 1922 object file. This may be a consequence of the manner in 1923 which it handles archives: I think it may load the entire 1924 archive in as separate csects, and then let garbage 1925 collection discard symbols. 1926 1927 We also have to handle the case of statically linking a 1928 shared object, which will cause symbol redefinitions, 1929 although this is an easier case to detect. */ 1930 else if (info->output_bfd->xvec == abfd->xvec) 1931 { 1932 if (! bfd_is_und_section (section)) 1933 *sym_hash = xcoff_link_hash_lookup (xcoff_hash_table (info), 1934 name, TRUE, copy, FALSE); 1935 else 1936 /* Make a copy of the symbol name to prevent problems with 1937 merging symbols. */ 1938 *sym_hash = ((struct xcoff_link_hash_entry *) 1939 bfd_wrapped_link_hash_lookup (abfd, info, name, 1940 TRUE, TRUE, FALSE)); 1941 1942 if (*sym_hash == NULL) 1943 goto error_return; 1944 if (((*sym_hash)->root.type == bfd_link_hash_defined 1945 || (*sym_hash)->root.type == bfd_link_hash_defweak) 1946 && ! bfd_is_und_section (section) 1947 && ! bfd_is_com_section (section)) 1948 { 1949 /* This is a second definition of a defined symbol. */ 1950 if (((*sym_hash)->flags & XCOFF_DEF_REGULAR) == 0 1951 && ((*sym_hash)->flags & XCOFF_DEF_DYNAMIC) != 0) 1952 { 1953 /* The existing symbol is from a shared library. 1954 Replace it. */ 1955 (*sym_hash)->root.type = bfd_link_hash_undefined; 1956 (*sym_hash)->root.u.undef.abfd = 1957 (*sym_hash)->root.u.def.section->owner; 1958 } 1959 else if (abfd->my_archive != NULL) 1960 { 1961 /* This is a redefinition in an object contained 1962 in an archive. Just ignore it. See the 1963 comment above. */ 1964 section = bfd_und_section_ptr; 1965 value = 0; 1966 } 1967 else if (sym.n_sclass == C_AIX_WEAKEXT 1968 || (*sym_hash)->root.type == bfd_link_hash_defweak) 1969 { 1970 /* At least one of the definitions is weak. 1971 Allow the normal rules to take effect. */ 1972 } 1973 else if ((*sym_hash)->root.u.undef.next != NULL 1974 || info->hash->undefs_tail == &(*sym_hash)->root) 1975 { 1976 /* This symbol has been referenced. In this 1977 case, we just continue and permit the 1978 multiple definition error. See the comment 1979 above about the behaviour of the AIX linker. */ 1980 } 1981 else if ((*sym_hash)->smclas == aux.x_csect.x_smclas) 1982 { 1983 /* The symbols are both csects of the same 1984 class. There is at least a chance that this 1985 is a semi-legitimate redefinition. */ 1986 section = bfd_und_section_ptr; 1987 value = 0; 1988 (*sym_hash)->flags |= XCOFF_MULTIPLY_DEFINED; 1989 } 1990 } 1991 else if (((*sym_hash)->flags & XCOFF_MULTIPLY_DEFINED) != 0 1992 && (*sym_hash)->root.type == bfd_link_hash_defined 1993 && (bfd_is_und_section (section) 1994 || bfd_is_com_section (section))) 1995 { 1996 /* This is a reference to a multiply defined symbol. 1997 Report the error now. See the comment above 1998 about the behaviour of the AIX linker. We could 1999 also do this with warning symbols, but I'm not 2000 sure the XCOFF linker is wholly prepared to 2001 handle them, and that would only be a warning, 2002 not an error. */ 2003 (*info->callbacks->multiple_definition) (info, 2004 &(*sym_hash)->root, 2005 NULL, NULL, 2006 (bfd_vma) 0); 2007 /* Try not to give this error too many times. */ 2008 (*sym_hash)->flags &= ~XCOFF_MULTIPLY_DEFINED; 2009 } 2010 } 2011 2012 /* _bfd_generic_link_add_one_symbol may call the linker to 2013 generate an error message, and the linker may try to read 2014 the symbol table to give a good error. Right now, the 2015 line numbers are in an inconsistent state, since they are 2016 counted both in the real sections and in the new csects. 2017 We need to leave the count in the real sections so that 2018 the linker can report the line number of the error 2019 correctly, so temporarily clobber the link to the csects 2020 so that the linker will not try to read the line numbers 2021 a second time from the csects. */ 2022 BFD_ASSERT (last_real->next == first_csect); 2023 last_real->next = NULL; 2024 flags = (sym.n_sclass == C_EXT ? BSF_GLOBAL : BSF_WEAK); 2025 if (! (_bfd_generic_link_add_one_symbol 2026 (info, abfd, name, flags, section, value, 2027 NULL, copy, TRUE, 2028 (struct bfd_link_hash_entry **) sym_hash))) 2029 goto error_return; 2030 last_real->next = first_csect; 2031 2032 if (smtyp == XTY_CM) 2033 { 2034 if ((*sym_hash)->root.type != bfd_link_hash_common 2035 || (*sym_hash)->root.u.c.p->section != csect) 2036 /* We don't need the common csect we just created. */ 2037 csect->size = 0; 2038 else 2039 (*sym_hash)->root.u.c.p->alignment_power 2040 = csect->alignment_power; 2041 } 2042 2043 if (info->output_bfd->xvec == abfd->xvec) 2044 { 2045 int flag; 2046 2047 if (smtyp == XTY_ER 2048 || smtyp == XTY_CM 2049 || section == bfd_und_section_ptr) 2050 flag = XCOFF_REF_REGULAR; 2051 else 2052 flag = XCOFF_DEF_REGULAR; 2053 (*sym_hash)->flags |= flag; 2054 2055 if ((*sym_hash)->smclas == XMC_UA 2056 || flag == XCOFF_DEF_REGULAR) 2057 (*sym_hash)->smclas = aux.x_csect.x_smclas; 2058 } 2059 } 2060 2061 if (smtyp == XTY_ER) 2062 *csect_cache = section; 2063 else 2064 { 2065 *csect_cache = csect; 2066 if (csect != NULL) 2067 xcoff_section_data (abfd, csect)->last_symndx 2068 = (esym - (bfd_byte *) obj_coff_external_syms (abfd)) / symesz; 2069 } 2070 2071 esym += (sym.n_numaux + 1) * symesz; 2072 sym_hash += sym.n_numaux + 1; 2073 csect_cache += sym.n_numaux + 1; 2074 lineno_counts += sym.n_numaux + 1; 2075 } 2076 2077 BFD_ASSERT (last_real == NULL || last_real->next == first_csect); 2078 2079 /* Make sure that we have seen all the relocs. */ 2080 for (o = abfd->sections; o != first_csect; o = o->next) 2081 { 2082 /* Debugging sections have no csects. */ 2083 if (bfd_get_section_flags (abfd, o) & SEC_DEBUGGING) 2084 continue; 2085 2086 /* Reset the section size and the line number count, since the 2087 data is now attached to the csects. Don't reset the size of 2088 the .debug section, since we need to read it below in 2089 bfd_xcoff_size_dynamic_sections. */ 2090 if (strcmp (bfd_get_section_name (abfd, o), ".debug") != 0) 2091 o->size = 0; 2092 o->lineno_count = 0; 2093 2094 if ((o->flags & SEC_RELOC) != 0) 2095 { 2096 bfd_size_type i; 2097 struct internal_reloc *rel; 2098 asection **rel_csect; 2099 2100 rel = reloc_info[o->target_index].relocs; 2101 rel_csect = reloc_info[o->target_index].csects; 2102 2103 for (i = 0; i < o->reloc_count; i++, rel++, rel_csect++) 2104 { 2105 if (*rel_csect == NULL) 2106 { 2107 _bfd_error_handler 2108 /* xgettext:c-format */ 2109 (_("%pB: reloc %s:%" PRId64 " not in csect"), 2110 abfd, o->name, (int64_t) i); 2111 bfd_set_error (bfd_error_bad_value); 2112 goto error_return; 2113 } 2114 2115 /* We identify all function symbols that are the target 2116 of a relocation, so that we can create glue code for 2117 functions imported from dynamic objects. */ 2118 if (info->output_bfd->xvec == abfd->xvec 2119 && *rel_csect != bfd_und_section_ptr 2120 && obj_xcoff_sym_hashes (abfd)[rel->r_symndx] != NULL) 2121 { 2122 struct xcoff_link_hash_entry *h; 2123 2124 h = obj_xcoff_sym_hashes (abfd)[rel->r_symndx]; 2125 /* If the symbol name starts with a period, it is 2126 the code of a function. If the symbol is 2127 currently undefined, then add an undefined symbol 2128 for the function descriptor. This should do no 2129 harm, because any regular object that defines the 2130 function should also define the function 2131 descriptor. It helps, because it means that we 2132 will identify the function descriptor with a 2133 dynamic object if a dynamic object defines it. */ 2134 if (h->root.root.string[0] == '.' 2135 && h->descriptor == NULL) 2136 { 2137 struct xcoff_link_hash_entry *hds; 2138 struct bfd_link_hash_entry *bh; 2139 2140 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), 2141 h->root.root.string + 1, 2142 TRUE, FALSE, TRUE); 2143 if (hds == NULL) 2144 goto error_return; 2145 if (hds->root.type == bfd_link_hash_new) 2146 { 2147 bh = &hds->root; 2148 if (! (_bfd_generic_link_add_one_symbol 2149 (info, abfd, hds->root.root.string, 2150 (flagword) 0, bfd_und_section_ptr, 2151 (bfd_vma) 0, NULL, FALSE, 2152 TRUE, &bh))) 2153 goto error_return; 2154 hds = (struct xcoff_link_hash_entry *) bh; 2155 } 2156 hds->flags |= XCOFF_DESCRIPTOR; 2157 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0); 2158 hds->descriptor = h; 2159 h->descriptor = hds; 2160 } 2161 if (h->root.root.string[0] == '.') 2162 h->flags |= XCOFF_CALLED; 2163 } 2164 } 2165 2166 free (reloc_info[o->target_index].csects); 2167 reloc_info[o->target_index].csects = NULL; 2168 2169 /* Reset SEC_RELOC and the reloc_count, since the reloc 2170 information is now attached to the csects. */ 2171 o->flags &=~ SEC_RELOC; 2172 o->reloc_count = 0; 2173 2174 /* If we are not keeping memory, free the reloc information. */ 2175 if (! info->keep_memory 2176 && coff_section_data (abfd, o) != NULL 2177 && coff_section_data (abfd, o)->relocs != NULL 2178 && ! coff_section_data (abfd, o)->keep_relocs) 2179 { 2180 free (coff_section_data (abfd, o)->relocs); 2181 coff_section_data (abfd, o)->relocs = NULL; 2182 } 2183 } 2184 2185 /* Free up the line numbers. FIXME: We could cache these 2186 somewhere for the final link, to avoid reading them again. */ 2187 if (reloc_info[o->target_index].linenos != NULL) 2188 { 2189 free (reloc_info[o->target_index].linenos); 2190 reloc_info[o->target_index].linenos = NULL; 2191 } 2192 } 2193 2194 free (reloc_info); 2195 2196 obj_coff_keep_syms (abfd) = keep_syms; 2197 2198 return TRUE; 2199 2200 error_return: 2201 if (reloc_info != NULL) 2202 { 2203 for (o = abfd->sections; o != NULL; o = o->next) 2204 { 2205 if (reloc_info[o->target_index].csects != NULL) 2206 free (reloc_info[o->target_index].csects); 2207 if (reloc_info[o->target_index].linenos != NULL) 2208 free (reloc_info[o->target_index].linenos); 2209 } 2210 free (reloc_info); 2211 } 2212 obj_coff_keep_syms (abfd) = keep_syms; 2213 return FALSE; 2214 } 2215 2216 #undef N_TMASK 2217 #undef N_BTSHFT 2218 2219 /* Add symbols from an XCOFF object file. */ 2220 2221 static bfd_boolean 2222 xcoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 2223 { 2224 if (! _bfd_coff_get_external_symbols (abfd)) 2225 return FALSE; 2226 if (! xcoff_link_add_symbols (abfd, info)) 2227 return FALSE; 2228 if (! info->keep_memory) 2229 { 2230 if (! _bfd_coff_free_symbols (abfd)) 2231 return FALSE; 2232 } 2233 return TRUE; 2234 } 2235 2236 /* Look through the loader symbols to see if this dynamic object 2237 should be included in the link. The native linker uses the loader 2238 symbols, not the normal symbol table, so we do too. */ 2239 2240 static bfd_boolean 2241 xcoff_link_check_dynamic_ar_symbols (bfd *abfd, 2242 struct bfd_link_info *info, 2243 bfd_boolean *pneeded, 2244 bfd **subsbfd) 2245 { 2246 asection *lsec; 2247 bfd_byte *contents; 2248 struct internal_ldhdr ldhdr; 2249 const char *strings; 2250 bfd_byte *elsym, *elsymend; 2251 2252 *pneeded = FALSE; 2253 2254 lsec = bfd_get_section_by_name (abfd, ".loader"); 2255 if (lsec == NULL) 2256 /* There are no symbols, so don't try to include it. */ 2257 return TRUE; 2258 2259 if (! xcoff_get_section_contents (abfd, lsec)) 2260 return FALSE; 2261 contents = coff_section_data (abfd, lsec)->contents; 2262 2263 bfd_xcoff_swap_ldhdr_in (abfd, contents, &ldhdr); 2264 2265 strings = (char *) contents + ldhdr.l_stoff; 2266 2267 elsym = contents + bfd_xcoff_loader_symbol_offset (abfd, &ldhdr); 2268 2269 elsymend = elsym + ldhdr.l_nsyms * bfd_xcoff_ldsymsz (abfd); 2270 for (; elsym < elsymend; elsym += bfd_xcoff_ldsymsz (abfd)) 2271 { 2272 struct internal_ldsym ldsym; 2273 char nambuf[SYMNMLEN + 1]; 2274 const char *name; 2275 struct bfd_link_hash_entry *h; 2276 2277 bfd_xcoff_swap_ldsym_in (abfd, elsym, &ldsym); 2278 2279 /* We are only interested in exported symbols. */ 2280 if ((ldsym.l_smtype & L_EXPORT) == 0) 2281 continue; 2282 2283 if (ldsym._l._l_l._l_zeroes == 0) 2284 name = strings + ldsym._l._l_l._l_offset; 2285 else 2286 { 2287 memcpy (nambuf, ldsym._l._l_name, SYMNMLEN); 2288 nambuf[SYMNMLEN] = '\0'; 2289 name = nambuf; 2290 } 2291 2292 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); 2293 2294 /* We are only interested in symbols that are currently 2295 undefined. At this point we know that we are using an XCOFF 2296 hash table. */ 2297 if (h != NULL 2298 && h->type == bfd_link_hash_undefined 2299 && (((struct xcoff_link_hash_entry *) h)->flags 2300 & XCOFF_DEF_DYNAMIC) == 0) 2301 { 2302 if (!(*info->callbacks 2303 ->add_archive_element) (info, abfd, name, subsbfd)) 2304 continue; 2305 *pneeded = TRUE; 2306 return TRUE; 2307 } 2308 } 2309 2310 /* We do not need this shared object. */ 2311 if (contents != NULL && ! coff_section_data (abfd, lsec)->keep_contents) 2312 { 2313 free (coff_section_data (abfd, lsec)->contents); 2314 coff_section_data (abfd, lsec)->contents = NULL; 2315 } 2316 2317 return TRUE; 2318 } 2319 2320 /* Look through the symbols to see if this object file should be 2321 included in the link. */ 2322 2323 static bfd_boolean 2324 xcoff_link_check_ar_symbols (bfd *abfd, 2325 struct bfd_link_info *info, 2326 bfd_boolean *pneeded, 2327 bfd **subsbfd) 2328 { 2329 bfd_size_type symesz; 2330 bfd_byte *esym; 2331 bfd_byte *esym_end; 2332 2333 *pneeded = FALSE; 2334 2335 if ((abfd->flags & DYNAMIC) != 0 2336 && ! info->static_link 2337 && info->output_bfd->xvec == abfd->xvec) 2338 return xcoff_link_check_dynamic_ar_symbols (abfd, info, pneeded, subsbfd); 2339 2340 symesz = bfd_coff_symesz (abfd); 2341 esym = (bfd_byte *) obj_coff_external_syms (abfd); 2342 esym_end = esym + obj_raw_syment_count (abfd) * symesz; 2343 while (esym < esym_end) 2344 { 2345 struct internal_syment sym; 2346 2347 bfd_coff_swap_sym_in (abfd, (void *) esym, (void *) &sym); 2348 2349 if (EXTERN_SYM_P (sym.n_sclass) && sym.n_scnum != N_UNDEF) 2350 { 2351 const char *name; 2352 char buf[SYMNMLEN + 1]; 2353 struct bfd_link_hash_entry *h; 2354 2355 /* This symbol is externally visible, and is defined by this 2356 object file. */ 2357 name = _bfd_coff_internal_syment_name (abfd, &sym, buf); 2358 2359 if (name == NULL) 2360 return FALSE; 2361 h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE); 2362 2363 /* We are only interested in symbols that are currently 2364 undefined. If a symbol is currently known to be common, 2365 XCOFF linkers do not bring in an object file which 2366 defines it. We also don't bring in symbols to satisfy 2367 undefined references in shared objects. */ 2368 if (h != NULL 2369 && h->type == bfd_link_hash_undefined 2370 && (info->output_bfd->xvec != abfd->xvec 2371 || (((struct xcoff_link_hash_entry *) h)->flags 2372 & XCOFF_DEF_DYNAMIC) == 0)) 2373 { 2374 if (!(*info->callbacks 2375 ->add_archive_element) (info, abfd, name, subsbfd)) 2376 continue; 2377 *pneeded = TRUE; 2378 return TRUE; 2379 } 2380 } 2381 2382 esym += (sym.n_numaux + 1) * symesz; 2383 } 2384 2385 /* We do not need this object file. */ 2386 return TRUE; 2387 } 2388 2389 /* Check a single archive element to see if we need to include it in 2390 the link. *PNEEDED is set according to whether this element is 2391 needed in the link or not. This is called via 2392 _bfd_generic_link_add_archive_symbols. */ 2393 2394 static bfd_boolean 2395 xcoff_link_check_archive_element (bfd *abfd, 2396 struct bfd_link_info *info, 2397 struct bfd_link_hash_entry *h ATTRIBUTE_UNUSED, 2398 const char *name ATTRIBUTE_UNUSED, 2399 bfd_boolean *pneeded) 2400 { 2401 bfd_boolean keep_syms_p; 2402 bfd *oldbfd; 2403 2404 keep_syms_p = (obj_coff_external_syms (abfd) != NULL); 2405 if (!_bfd_coff_get_external_symbols (abfd)) 2406 return FALSE; 2407 2408 oldbfd = abfd; 2409 if (!xcoff_link_check_ar_symbols (abfd, info, pneeded, &abfd)) 2410 return FALSE; 2411 2412 if (*pneeded) 2413 { 2414 /* Potentially, the add_archive_element hook may have set a 2415 substitute BFD for us. */ 2416 if (abfd != oldbfd) 2417 { 2418 if (!keep_syms_p 2419 && !_bfd_coff_free_symbols (oldbfd)) 2420 return FALSE; 2421 keep_syms_p = (obj_coff_external_syms (abfd) != NULL); 2422 if (!_bfd_coff_get_external_symbols (abfd)) 2423 return FALSE; 2424 } 2425 if (!xcoff_link_add_symbols (abfd, info)) 2426 return FALSE; 2427 if (info->keep_memory) 2428 keep_syms_p = TRUE; 2429 } 2430 2431 if (!keep_syms_p) 2432 { 2433 if (!_bfd_coff_free_symbols (abfd)) 2434 return FALSE; 2435 } 2436 2437 return TRUE; 2438 } 2439 2440 /* Given an XCOFF BFD, add symbols to the global hash table as 2441 appropriate. */ 2442 2443 bfd_boolean 2444 _bfd_xcoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 2445 { 2446 switch (bfd_get_format (abfd)) 2447 { 2448 case bfd_object: 2449 return xcoff_link_add_object_symbols (abfd, info); 2450 2451 case bfd_archive: 2452 /* If the archive has a map, do the usual search. We then need 2453 to check the archive for dynamic objects, because they may not 2454 appear in the archive map even though they should, perhaps, be 2455 included. If the archive has no map, we just consider each object 2456 file in turn, since that apparently is what the AIX native linker 2457 does. */ 2458 if (bfd_has_map (abfd)) 2459 { 2460 if (! (_bfd_generic_link_add_archive_symbols 2461 (abfd, info, xcoff_link_check_archive_element))) 2462 return FALSE; 2463 } 2464 2465 { 2466 bfd *member; 2467 2468 member = bfd_openr_next_archived_file (abfd, NULL); 2469 while (member != NULL) 2470 { 2471 if (bfd_check_format (member, bfd_object) 2472 && (info->output_bfd->xvec == member->xvec) 2473 && (! bfd_has_map (abfd) || (member->flags & DYNAMIC) != 0)) 2474 { 2475 bfd_boolean needed; 2476 2477 if (! xcoff_link_check_archive_element (member, info, 2478 NULL, NULL, &needed)) 2479 return FALSE; 2480 if (needed) 2481 member->archive_pass = -1; 2482 } 2483 member = bfd_openr_next_archived_file (abfd, member); 2484 } 2485 } 2486 2487 return TRUE; 2488 2489 default: 2490 bfd_set_error (bfd_error_wrong_format); 2491 return FALSE; 2492 } 2493 } 2494 2495 bfd_boolean 2496 _bfd_xcoff_define_common_symbol (bfd *output_bfd ATTRIBUTE_UNUSED, 2497 struct bfd_link_info *info ATTRIBUTE_UNUSED, 2498 struct bfd_link_hash_entry *harg) 2499 { 2500 struct xcoff_link_hash_entry *h; 2501 2502 if (!bfd_generic_define_common_symbol (output_bfd, info, harg)) 2503 return FALSE; 2504 2505 h = (struct xcoff_link_hash_entry *) harg; 2506 h->flags |= XCOFF_DEF_REGULAR; 2507 return TRUE; 2508 } 2509 2510 /* If symbol H has not been interpreted as a function descriptor, 2511 see whether it should be. Set up its descriptor information if so. */ 2512 2513 static bfd_boolean 2514 xcoff_find_function (struct bfd_link_info *info, 2515 struct xcoff_link_hash_entry *h) 2516 { 2517 if ((h->flags & XCOFF_DESCRIPTOR) == 0 2518 && h->root.root.string[0] != '.') 2519 { 2520 char *fnname; 2521 struct xcoff_link_hash_entry *hfn; 2522 bfd_size_type amt; 2523 2524 amt = strlen (h->root.root.string) + 2; 2525 fnname = bfd_malloc (amt); 2526 if (fnname == NULL) 2527 return FALSE; 2528 fnname[0] = '.'; 2529 strcpy (fnname + 1, h->root.root.string); 2530 hfn = xcoff_link_hash_lookup (xcoff_hash_table (info), 2531 fnname, FALSE, FALSE, TRUE); 2532 free (fnname); 2533 if (hfn != NULL 2534 && hfn->smclas == XMC_PR 2535 && (hfn->root.type == bfd_link_hash_defined 2536 || hfn->root.type == bfd_link_hash_defweak)) 2537 { 2538 h->flags |= XCOFF_DESCRIPTOR; 2539 h->descriptor = hfn; 2540 hfn->descriptor = h; 2541 } 2542 } 2543 return TRUE; 2544 } 2545 2546 /* Return true if the given bfd contains at least one shared object. */ 2547 2548 static bfd_boolean 2549 xcoff_archive_contains_shared_object_p (struct bfd_link_info *info, 2550 bfd *archive) 2551 { 2552 struct xcoff_archive_info *archive_info; 2553 bfd *member; 2554 2555 archive_info = xcoff_get_archive_info (info, archive); 2556 if (!archive_info->know_contains_shared_object_p) 2557 { 2558 member = bfd_openr_next_archived_file (archive, NULL); 2559 while (member != NULL && (member->flags & DYNAMIC) == 0) 2560 member = bfd_openr_next_archived_file (archive, member); 2561 2562 archive_info->contains_shared_object_p = (member != NULL); 2563 archive_info->know_contains_shared_object_p = 1; 2564 } 2565 return archive_info->contains_shared_object_p; 2566 } 2567 2568 /* Symbol H qualifies for export by -bexpfull. Return true if it also 2569 qualifies for export by -bexpall. */ 2570 2571 static bfd_boolean 2572 xcoff_covered_by_expall_p (struct xcoff_link_hash_entry *h) 2573 { 2574 /* Exclude symbols beginning with '_'. */ 2575 if (h->root.root.string[0] == '_') 2576 return FALSE; 2577 2578 /* Exclude archive members that would otherwise be unreferenced. */ 2579 if ((h->flags & XCOFF_MARK) == 0 2580 && (h->root.type == bfd_link_hash_defined 2581 || h->root.type == bfd_link_hash_defweak) 2582 && h->root.u.def.section->owner != NULL 2583 && h->root.u.def.section->owner->my_archive != NULL) 2584 return FALSE; 2585 2586 return TRUE; 2587 } 2588 2589 /* Return true if symbol H qualifies for the forms of automatic export 2590 specified by AUTO_EXPORT_FLAGS. */ 2591 2592 static bfd_boolean 2593 xcoff_auto_export_p (struct bfd_link_info *info, 2594 struct xcoff_link_hash_entry *h, 2595 unsigned int auto_export_flags) 2596 { 2597 /* Don't automatically export things that were explicitly exported. */ 2598 if ((h->flags & XCOFF_EXPORT) != 0) 2599 return FALSE; 2600 2601 /* Don't export things that we don't define. */ 2602 if ((h->flags & XCOFF_DEF_REGULAR) == 0) 2603 return FALSE; 2604 2605 /* Don't export functions; export their descriptors instead. */ 2606 if (h->root.root.string[0] == '.') 2607 return FALSE; 2608 2609 /* We don't export a symbol which is being defined by an object 2610 included from an archive which contains a shared object. The 2611 rationale is that if an archive contains both an unshared and 2612 a shared object, then there must be some reason that the 2613 unshared object is unshared, and we don't want to start 2614 providing a shared version of it. In particular, this solves 2615 a bug involving the _savefNN set of functions. gcc will call 2616 those functions without providing a slot to restore the TOC, 2617 so it is essential that these functions be linked in directly 2618 and not from a shared object, which means that a shared 2619 object which also happens to link them in must not export 2620 them. This is confusing, but I haven't been able to think of 2621 a different approach. Note that the symbols can, of course, 2622 be exported explicitly. */ 2623 if (h->root.type == bfd_link_hash_defined 2624 || h->root.type == bfd_link_hash_defweak) 2625 { 2626 bfd *owner; 2627 2628 owner = h->root.u.def.section->owner; 2629 if (owner != NULL 2630 && owner->my_archive != NULL 2631 && xcoff_archive_contains_shared_object_p (info, owner->my_archive)) 2632 return FALSE; 2633 } 2634 2635 /* Otherwise, all symbols are exported by -bexpfull. */ 2636 if ((auto_export_flags & XCOFF_EXPFULL) != 0) 2637 return TRUE; 2638 2639 /* Despite its name, -bexpall exports most but not all symbols. */ 2640 if ((auto_export_flags & XCOFF_EXPALL) != 0 2641 && xcoff_covered_by_expall_p (h)) 2642 return TRUE; 2643 2644 return FALSE; 2645 } 2646 2647 /* Return true if relocation REL needs to be copied to the .loader section. 2648 If REL is against a global symbol, H is that symbol, otherwise it 2649 is null. */ 2650 2651 static bfd_boolean 2652 xcoff_need_ldrel_p (struct bfd_link_info *info, struct internal_reloc *rel, 2653 struct xcoff_link_hash_entry *h) 2654 { 2655 if (!xcoff_hash_table (info)->loader_section) 2656 return FALSE; 2657 2658 switch (rel->r_type) 2659 { 2660 case R_TOC: 2661 case R_GL: 2662 case R_TCL: 2663 case R_TRL: 2664 case R_TRLA: 2665 /* We should never need a .loader reloc for a TOC-relative reloc. */ 2666 return FALSE; 2667 2668 default: 2669 /* In this case, relocations against defined symbols can be resolved 2670 statically. */ 2671 if (h == NULL 2672 || h->root.type == bfd_link_hash_defined 2673 || h->root.type == bfd_link_hash_defweak 2674 || h->root.type == bfd_link_hash_common) 2675 return FALSE; 2676 2677 /* We will always provide a local definition of function symbols, 2678 even if we don't have one yet. */ 2679 if ((h->flags & XCOFF_CALLED) != 0) 2680 return FALSE; 2681 2682 return TRUE; 2683 2684 case R_POS: 2685 case R_NEG: 2686 case R_RL: 2687 case R_RLA: 2688 /* Absolute relocations against absolute symbols can be 2689 resolved statically. */ 2690 if (h != NULL && bfd_is_abs_symbol (&h->root)) 2691 return FALSE; 2692 2693 return TRUE; 2694 } 2695 } 2696 2697 /* Mark a symbol as not being garbage, including the section in which 2698 it is defined. */ 2699 2700 static inline bfd_boolean 2701 xcoff_mark_symbol (struct bfd_link_info *info, struct xcoff_link_hash_entry *h) 2702 { 2703 if ((h->flags & XCOFF_MARK) != 0) 2704 return TRUE; 2705 2706 h->flags |= XCOFF_MARK; 2707 2708 /* If we're marking an undefined symbol, try find some way of 2709 defining it. */ 2710 if (!bfd_link_relocatable (info) 2711 && (h->flags & XCOFF_IMPORT) == 0 2712 && (h->flags & XCOFF_DEF_REGULAR) == 0 2713 && (h->root.type == bfd_link_hash_undefined 2714 || h->root.type == bfd_link_hash_undefweak)) 2715 { 2716 /* First check whether this symbol can be interpreted as an 2717 undefined function descriptor for a defined function symbol. */ 2718 if (!xcoff_find_function (info, h)) 2719 return FALSE; 2720 2721 if ((h->flags & XCOFF_DESCRIPTOR) != 0 2722 && (h->descriptor->root.type == bfd_link_hash_defined 2723 || h->descriptor->root.type == bfd_link_hash_defweak)) 2724 { 2725 /* This is a descriptor for a defined symbol, but the input 2726 objects have not defined the descriptor itself. Fill in 2727 the definition automatically. 2728 2729 Note that we do this even if we found a dynamic definition 2730 of H. The local function definition logically overrides 2731 the dynamic one. */ 2732 asection *sec; 2733 2734 sec = xcoff_hash_table (info)->descriptor_section; 2735 h->root.type = bfd_link_hash_defined; 2736 h->root.u.def.section = sec; 2737 h->root.u.def.value = sec->size; 2738 h->smclas = XMC_DS; 2739 h->flags |= XCOFF_DEF_REGULAR; 2740 2741 /* The size of the function descriptor depends on whether this 2742 is xcoff32 (12) or xcoff64 (24). */ 2743 sec->size += bfd_xcoff_function_descriptor_size (sec->owner); 2744 2745 /* A function descriptor uses two relocs: one for the 2746 associated code, and one for the TOC address. */ 2747 xcoff_hash_table (info)->ldrel_count += 2; 2748 sec->reloc_count += 2; 2749 2750 /* Mark the function itself. */ 2751 if (!xcoff_mark_symbol (info, h->descriptor)) 2752 return FALSE; 2753 2754 /* Mark the TOC section, so that we get an anchor 2755 to relocate against. */ 2756 if (!xcoff_mark (info, xcoff_hash_table (info)->toc_section)) 2757 return FALSE; 2758 2759 /* We handle writing out the contents of the descriptor in 2760 xcoff_write_global_symbol. */ 2761 } 2762 else if (info->static_link) 2763 /* We can't get a symbol value dynamically, so just assume 2764 that it's undefined. */ 2765 h->flags |= XCOFF_WAS_UNDEFINED; 2766 else if ((h->flags & XCOFF_CALLED) != 0) 2767 { 2768 /* This is a function symbol for which we need to create 2769 linkage code. */ 2770 asection *sec; 2771 struct xcoff_link_hash_entry *hds; 2772 2773 /* Mark the descriptor (and its TOC section). */ 2774 hds = h->descriptor; 2775 BFD_ASSERT ((hds->root.type == bfd_link_hash_undefined 2776 || hds->root.type == bfd_link_hash_undefweak) 2777 && (hds->flags & XCOFF_DEF_REGULAR) == 0); 2778 if (!xcoff_mark_symbol (info, hds)) 2779 return FALSE; 2780 2781 /* Treat this symbol as undefined if the descriptor was. */ 2782 if ((hds->flags & XCOFF_WAS_UNDEFINED) != 0) 2783 h->flags |= XCOFF_WAS_UNDEFINED; 2784 2785 /* Allocate room for the global linkage code itself. */ 2786 sec = xcoff_hash_table (info)->linkage_section; 2787 h->root.type = bfd_link_hash_defined; 2788 h->root.u.def.section = sec; 2789 h->root.u.def.value = sec->size; 2790 h->smclas = XMC_GL; 2791 h->flags |= XCOFF_DEF_REGULAR; 2792 sec->size += bfd_xcoff_glink_code_size (info->output_bfd); 2793 2794 /* The global linkage code requires a TOC entry for the 2795 descriptor. */ 2796 if (hds->toc_section == NULL) 2797 { 2798 int byte_size; 2799 2800 /* 32 vs 64 2801 xcoff32 uses 4 bytes in the toc. 2802 xcoff64 uses 8 bytes in the toc. */ 2803 if (bfd_xcoff_is_xcoff64 (info->output_bfd)) 2804 byte_size = 8; 2805 else if (bfd_xcoff_is_xcoff32 (info->output_bfd)) 2806 byte_size = 4; 2807 else 2808 return FALSE; 2809 2810 /* Allocate room in the fallback TOC section. */ 2811 hds->toc_section = xcoff_hash_table (info)->toc_section; 2812 hds->u.toc_offset = hds->toc_section->size; 2813 hds->toc_section->size += byte_size; 2814 if (!xcoff_mark (info, hds->toc_section)) 2815 return FALSE; 2816 2817 /* Allocate room for a static and dynamic R_TOC 2818 relocation. */ 2819 ++xcoff_hash_table (info)->ldrel_count; 2820 ++hds->toc_section->reloc_count; 2821 2822 /* Set the index to -2 to force this symbol to 2823 get written out. */ 2824 hds->indx = -2; 2825 hds->flags |= XCOFF_SET_TOC | XCOFF_LDREL; 2826 } 2827 } 2828 else if ((h->flags & XCOFF_DEF_DYNAMIC) == 0) 2829 { 2830 /* Record that the symbol was undefined, then import it. 2831 -brtl links use a special fake import file. */ 2832 h->flags |= XCOFF_WAS_UNDEFINED | XCOFF_IMPORT; 2833 if (xcoff_hash_table (info)->rtld) 2834 { 2835 if (!xcoff_set_import_path (info, h, "", "..", "")) 2836 return FALSE; 2837 } 2838 else 2839 { 2840 if (!xcoff_set_import_path (info, h, NULL, NULL, NULL)) 2841 return FALSE; 2842 } 2843 } 2844 } 2845 2846 if (h->root.type == bfd_link_hash_defined 2847 || h->root.type == bfd_link_hash_defweak) 2848 { 2849 asection *hsec; 2850 2851 hsec = h->root.u.def.section; 2852 if (! bfd_is_abs_section (hsec) 2853 && (hsec->flags & SEC_MARK) == 0) 2854 { 2855 if (! xcoff_mark (info, hsec)) 2856 return FALSE; 2857 } 2858 } 2859 2860 if (h->toc_section != NULL 2861 && (h->toc_section->flags & SEC_MARK) == 0) 2862 { 2863 if (! xcoff_mark (info, h->toc_section)) 2864 return FALSE; 2865 } 2866 2867 return TRUE; 2868 } 2869 2870 /* Look for a symbol called NAME. If the symbol is defined, mark it. 2871 If the symbol exists, set FLAGS. */ 2872 2873 static bfd_boolean 2874 xcoff_mark_symbol_by_name (struct bfd_link_info *info, 2875 const char *name, unsigned int flags) 2876 { 2877 struct xcoff_link_hash_entry *h; 2878 2879 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, 2880 FALSE, FALSE, TRUE); 2881 if (h != NULL) 2882 { 2883 h->flags |= flags; 2884 if (h->root.type == bfd_link_hash_defined 2885 || h->root.type == bfd_link_hash_defweak) 2886 { 2887 if (!xcoff_mark (info, h->root.u.def.section)) 2888 return FALSE; 2889 } 2890 } 2891 return TRUE; 2892 } 2893 2894 /* The mark phase of garbage collection. For a given section, mark 2895 it, and all the sections which define symbols to which it refers. 2896 Because this function needs to look at the relocs, we also count 2897 the number of relocs which need to be copied into the .loader 2898 section. */ 2899 2900 static bfd_boolean 2901 xcoff_mark (struct bfd_link_info *info, asection *sec) 2902 { 2903 if (bfd_is_abs_section (sec) 2904 || (sec->flags & SEC_MARK) != 0) 2905 return TRUE; 2906 2907 sec->flags |= SEC_MARK; 2908 2909 if (sec->owner->xvec == info->output_bfd->xvec 2910 && coff_section_data (sec->owner, sec) != NULL 2911 && xcoff_section_data (sec->owner, sec) != NULL) 2912 { 2913 struct xcoff_link_hash_entry **syms; 2914 struct internal_reloc *rel, *relend; 2915 asection **csects; 2916 unsigned long i, first, last; 2917 2918 /* Mark all the symbols in this section. */ 2919 syms = obj_xcoff_sym_hashes (sec->owner); 2920 csects = xcoff_data (sec->owner)->csects; 2921 first = xcoff_section_data (sec->owner, sec)->first_symndx; 2922 last = xcoff_section_data (sec->owner, sec)->last_symndx; 2923 for (i = first; i <= last; i++) 2924 if (csects[i] == sec 2925 && syms[i] != NULL 2926 && (syms[i]->flags & XCOFF_MARK) == 0) 2927 { 2928 if (!xcoff_mark_symbol (info, syms[i])) 2929 return FALSE; 2930 } 2931 2932 /* Look through the section relocs. */ 2933 if ((sec->flags & SEC_RELOC) != 0 2934 && sec->reloc_count > 0) 2935 { 2936 rel = xcoff_read_internal_relocs (sec->owner, sec, TRUE, 2937 NULL, FALSE, NULL); 2938 if (rel == NULL) 2939 return FALSE; 2940 relend = rel + sec->reloc_count; 2941 for (; rel < relend; rel++) 2942 { 2943 struct xcoff_link_hash_entry *h; 2944 2945 if ((unsigned int) rel->r_symndx 2946 > obj_raw_syment_count (sec->owner)) 2947 continue; 2948 2949 h = obj_xcoff_sym_hashes (sec->owner)[rel->r_symndx]; 2950 if (h != NULL) 2951 { 2952 if ((h->flags & XCOFF_MARK) == 0) 2953 { 2954 if (!xcoff_mark_symbol (info, h)) 2955 return FALSE; 2956 } 2957 } 2958 else 2959 { 2960 asection *rsec; 2961 2962 rsec = xcoff_data (sec->owner)->csects[rel->r_symndx]; 2963 if (rsec != NULL 2964 && (rsec->flags & SEC_MARK) == 0) 2965 { 2966 if (!xcoff_mark (info, rsec)) 2967 return FALSE; 2968 } 2969 } 2970 2971 /* See if this reloc needs to be copied into the .loader 2972 section. */ 2973 if (xcoff_need_ldrel_p (info, rel, h)) 2974 { 2975 ++xcoff_hash_table (info)->ldrel_count; 2976 if (h != NULL) 2977 h->flags |= XCOFF_LDREL; 2978 } 2979 } 2980 2981 if (! info->keep_memory 2982 && coff_section_data (sec->owner, sec) != NULL 2983 && coff_section_data (sec->owner, sec)->relocs != NULL 2984 && ! coff_section_data (sec->owner, sec)->keep_relocs) 2985 { 2986 free (coff_section_data (sec->owner, sec)->relocs); 2987 coff_section_data (sec->owner, sec)->relocs = NULL; 2988 } 2989 } 2990 } 2991 2992 return TRUE; 2993 } 2994 2995 /* Routines that are called after all the input files have been 2996 handled, but before the sections are laid out in memory. */ 2997 2998 /* The sweep phase of garbage collection. Remove all garbage 2999 sections. */ 3000 3001 static void 3002 xcoff_sweep (struct bfd_link_info *info) 3003 { 3004 bfd *sub; 3005 3006 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 3007 { 3008 asection *o; 3009 3010 for (o = sub->sections; o != NULL; o = o->next) 3011 { 3012 if ((o->flags & SEC_MARK) == 0) 3013 { 3014 /* Keep all sections from non-XCOFF input files. Keep 3015 special sections. Keep .debug sections for the 3016 moment. */ 3017 if (sub->xvec != info->output_bfd->xvec 3018 || o == xcoff_hash_table (info)->debug_section 3019 || o == xcoff_hash_table (info)->loader_section 3020 || o == xcoff_hash_table (info)->linkage_section 3021 || o == xcoff_hash_table (info)->descriptor_section 3022 || (bfd_get_section_flags (sub, o) & SEC_DEBUGGING) 3023 || strcmp (o->name, ".debug") == 0) 3024 o->flags |= SEC_MARK; 3025 else 3026 { 3027 o->size = 0; 3028 o->reloc_count = 0; 3029 } 3030 } 3031 } 3032 } 3033 } 3034 3035 /* Record the number of elements in a set. This is used to output the 3036 correct csect length. */ 3037 3038 bfd_boolean 3039 bfd_xcoff_link_record_set (bfd *output_bfd, 3040 struct bfd_link_info *info, 3041 struct bfd_link_hash_entry *harg, 3042 bfd_size_type size) 3043 { 3044 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; 3045 struct xcoff_link_size_list *n; 3046 bfd_size_type amt; 3047 3048 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) 3049 return TRUE; 3050 3051 /* This will hardly ever be called. I don't want to burn four bytes 3052 per global symbol, so instead the size is kept on a linked list 3053 attached to the hash table. */ 3054 amt = sizeof (* n); 3055 n = bfd_alloc (output_bfd, amt); 3056 if (n == NULL) 3057 return FALSE; 3058 n->next = xcoff_hash_table (info)->size_list; 3059 n->h = h; 3060 n->size = size; 3061 xcoff_hash_table (info)->size_list = n; 3062 3063 h->flags |= XCOFF_HAS_SIZE; 3064 3065 return TRUE; 3066 } 3067 3068 /* Import a symbol. */ 3069 3070 bfd_boolean 3071 bfd_xcoff_import_symbol (bfd *output_bfd, 3072 struct bfd_link_info *info, 3073 struct bfd_link_hash_entry *harg, 3074 bfd_vma val, 3075 const char *imppath, 3076 const char *impfile, 3077 const char *impmember, 3078 unsigned int syscall_flag) 3079 { 3080 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; 3081 3082 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) 3083 return TRUE; 3084 3085 /* A symbol name which starts with a period is the code for a 3086 function. If the symbol is undefined, then add an undefined 3087 symbol for the function descriptor, and import that instead. */ 3088 if (h->root.root.string[0] == '.' 3089 && h->root.type == bfd_link_hash_undefined 3090 && val == (bfd_vma) -1) 3091 { 3092 struct xcoff_link_hash_entry *hds; 3093 3094 hds = h->descriptor; 3095 if (hds == NULL) 3096 { 3097 hds = xcoff_link_hash_lookup (xcoff_hash_table (info), 3098 h->root.root.string + 1, 3099 TRUE, FALSE, TRUE); 3100 if (hds == NULL) 3101 return FALSE; 3102 if (hds->root.type == bfd_link_hash_new) 3103 { 3104 hds->root.type = bfd_link_hash_undefined; 3105 hds->root.u.undef.abfd = h->root.u.undef.abfd; 3106 } 3107 hds->flags |= XCOFF_DESCRIPTOR; 3108 BFD_ASSERT ((h->flags & XCOFF_DESCRIPTOR) == 0); 3109 hds->descriptor = h; 3110 h->descriptor = hds; 3111 } 3112 3113 /* Now, if the descriptor is undefined, import the descriptor 3114 rather than the symbol we were told to import. FIXME: Is 3115 this correct in all cases? */ 3116 if (hds->root.type == bfd_link_hash_undefined) 3117 h = hds; 3118 } 3119 3120 h->flags |= (XCOFF_IMPORT | syscall_flag); 3121 3122 if (val != (bfd_vma) -1) 3123 { 3124 if (h->root.type == bfd_link_hash_defined 3125 && (!bfd_is_abs_symbol (&h->root) 3126 || h->root.u.def.value != val)) 3127 (*info->callbacks->multiple_definition) (info, &h->root, output_bfd, 3128 bfd_abs_section_ptr, val); 3129 3130 h->root.type = bfd_link_hash_defined; 3131 h->root.u.def.section = bfd_abs_section_ptr; 3132 h->root.u.def.value = val; 3133 h->smclas = XMC_XO; 3134 } 3135 3136 if (!xcoff_set_import_path (info, h, imppath, impfile, impmember)) 3137 return FALSE; 3138 3139 return TRUE; 3140 } 3141 3142 /* Export a symbol. */ 3143 3144 bfd_boolean 3145 bfd_xcoff_export_symbol (bfd *output_bfd, 3146 struct bfd_link_info *info, 3147 struct bfd_link_hash_entry *harg) 3148 { 3149 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) harg; 3150 3151 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) 3152 return TRUE; 3153 3154 h->flags |= XCOFF_EXPORT; 3155 3156 /* FIXME: I'm not at all sure what syscall is supposed to mean, so 3157 I'm just going to ignore it until somebody explains it. */ 3158 3159 /* Make sure we don't garbage collect this symbol. */ 3160 if (! xcoff_mark_symbol (info, h)) 3161 return FALSE; 3162 3163 /* If this is a function descriptor, make sure we don't garbage 3164 collect the associated function code. We normally don't have to 3165 worry about this, because the descriptor will be attached to a 3166 section with relocs, but if we are creating the descriptor 3167 ourselves those relocs will not be visible to the mark code. */ 3168 if ((h->flags & XCOFF_DESCRIPTOR) != 0) 3169 { 3170 if (! xcoff_mark_symbol (info, h->descriptor)) 3171 return FALSE; 3172 } 3173 3174 return TRUE; 3175 } 3176 3177 /* Count a reloc against a symbol. This is called for relocs 3178 generated by the linker script, typically for global constructors 3179 and destructors. */ 3180 3181 bfd_boolean 3182 bfd_xcoff_link_count_reloc (bfd *output_bfd, 3183 struct bfd_link_info *info, 3184 const char *name) 3185 { 3186 struct xcoff_link_hash_entry *h; 3187 3188 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) 3189 return TRUE; 3190 3191 h = ((struct xcoff_link_hash_entry *) 3192 bfd_wrapped_link_hash_lookup (output_bfd, info, name, FALSE, FALSE, 3193 FALSE)); 3194 if (h == NULL) 3195 { 3196 _bfd_error_handler (_("%s: no such symbol"), name); 3197 bfd_set_error (bfd_error_no_symbols); 3198 return FALSE; 3199 } 3200 3201 h->flags |= XCOFF_REF_REGULAR; 3202 if (xcoff_hash_table (info)->loader_section) 3203 { 3204 h->flags |= XCOFF_LDREL; 3205 ++xcoff_hash_table (info)->ldrel_count; 3206 } 3207 3208 /* Mark the symbol to avoid garbage collection. */ 3209 if (! xcoff_mark_symbol (info, h)) 3210 return FALSE; 3211 3212 return TRUE; 3213 } 3214 3215 /* This function is called for each symbol to which the linker script 3216 assigns a value. */ 3217 3218 bfd_boolean 3219 bfd_xcoff_record_link_assignment (bfd *output_bfd, 3220 struct bfd_link_info *info, 3221 const char *name) 3222 { 3223 struct xcoff_link_hash_entry *h; 3224 3225 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) 3226 return TRUE; 3227 3228 h = xcoff_link_hash_lookup (xcoff_hash_table (info), name, TRUE, TRUE, 3229 FALSE); 3230 if (h == NULL) 3231 return FALSE; 3232 3233 h->flags |= XCOFF_DEF_REGULAR; 3234 3235 return TRUE; 3236 } 3237 3238 /* An xcoff_link_hash_traverse callback for which DATA points to an 3239 xcoff_loader_info. Mark all symbols that should be automatically 3240 exported. */ 3241 3242 static bfd_boolean 3243 xcoff_mark_auto_exports (struct xcoff_link_hash_entry *h, void *data) 3244 { 3245 struct xcoff_loader_info *ldinfo; 3246 3247 ldinfo = (struct xcoff_loader_info *) data; 3248 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags)) 3249 { 3250 if (!xcoff_mark_symbol (ldinfo->info, h)) 3251 ldinfo->failed = TRUE; 3252 } 3253 return TRUE; 3254 } 3255 3256 /* Add a symbol to the .loader symbols, if necessary. */ 3257 3258 /* INPUT_BFD has an external symbol associated with hash table entry H 3259 and csect CSECT. Return true if INPUT_BFD defines H. */ 3260 3261 static bfd_boolean 3262 xcoff_final_definition_p (bfd *input_bfd, struct xcoff_link_hash_entry *h, 3263 asection *csect) 3264 { 3265 switch (h->root.type) 3266 { 3267 case bfd_link_hash_defined: 3268 case bfd_link_hash_defweak: 3269 /* No input bfd owns absolute symbols. They are written by 3270 xcoff_write_global_symbol instead. */ 3271 return (!bfd_is_abs_section (csect) 3272 && h->root.u.def.section == csect); 3273 3274 case bfd_link_hash_common: 3275 return h->root.u.c.p->section->owner == input_bfd; 3276 3277 case bfd_link_hash_undefined: 3278 case bfd_link_hash_undefweak: 3279 /* We can't treat undef.abfd as the owner because that bfd 3280 might be a dynamic object. Allow any bfd to claim it. */ 3281 return TRUE; 3282 3283 default: 3284 abort (); 3285 } 3286 } 3287 3288 /* See if H should have a loader symbol associated with it. */ 3289 3290 static bfd_boolean 3291 xcoff_build_ldsym (struct xcoff_loader_info *ldinfo, 3292 struct xcoff_link_hash_entry *h) 3293 { 3294 bfd_size_type amt; 3295 3296 /* Warn if this symbol is exported but not defined. */ 3297 if ((h->flags & XCOFF_EXPORT) != 0 3298 && (h->flags & XCOFF_WAS_UNDEFINED) != 0) 3299 { 3300 _bfd_error_handler 3301 (_("warning: attempt to export undefined symbol `%s'"), 3302 h->root.root.string); 3303 return TRUE; 3304 } 3305 3306 /* We need to add a symbol to the .loader section if it is mentioned 3307 in a reloc which we are copying to the .loader section and it was 3308 not defined or common, or if it is the entry point, or if it is 3309 being exported. */ 3310 if (((h->flags & XCOFF_LDREL) == 0 3311 || h->root.type == bfd_link_hash_defined 3312 || h->root.type == bfd_link_hash_defweak 3313 || h->root.type == bfd_link_hash_common) 3314 && (h->flags & XCOFF_ENTRY) == 0 3315 && (h->flags & XCOFF_EXPORT) == 0) 3316 return TRUE; 3317 3318 /* We need to add this symbol to the .loader symbols. */ 3319 3320 BFD_ASSERT (h->ldsym == NULL); 3321 amt = sizeof (struct internal_ldsym); 3322 h->ldsym = bfd_zalloc (ldinfo->output_bfd, amt); 3323 if (h->ldsym == NULL) 3324 { 3325 ldinfo->failed = TRUE; 3326 return FALSE; 3327 } 3328 3329 if ((h->flags & XCOFF_IMPORT) != 0) 3330 { 3331 /* Give imported descriptors class XMC_DS rather than XMC_UA. */ 3332 if ((h->flags & XCOFF_DESCRIPTOR) != 0) 3333 h->smclas = XMC_DS; 3334 h->ldsym->l_ifile = h->ldindx; 3335 } 3336 3337 /* The first 3 symbol table indices are reserved to indicate the 3338 data, text and bss sections. */ 3339 h->ldindx = ldinfo->ldsym_count + 3; 3340 3341 ++ldinfo->ldsym_count; 3342 3343 if (! bfd_xcoff_put_ldsymbol_name (ldinfo->output_bfd, ldinfo, 3344 h->ldsym, h->root.root.string)) 3345 return FALSE; 3346 3347 h->flags |= XCOFF_BUILT_LDSYM; 3348 return TRUE; 3349 } 3350 3351 /* An xcoff_htab_traverse callback that is called for each symbol 3352 once garbage collection is complete. */ 3353 3354 static bfd_boolean 3355 xcoff_post_gc_symbol (struct xcoff_link_hash_entry *h, void * p) 3356 { 3357 struct xcoff_loader_info *ldinfo = (struct xcoff_loader_info *) p; 3358 3359 /* __rtinit, this symbol has special handling. */ 3360 if (h->flags & XCOFF_RTINIT) 3361 return TRUE; 3362 3363 /* We don't want to garbage collect symbols which are not defined in 3364 XCOFF files. This is a convenient place to mark them. */ 3365 if (xcoff_hash_table (ldinfo->info)->gc 3366 && (h->flags & XCOFF_MARK) == 0 3367 && (h->root.type == bfd_link_hash_defined 3368 || h->root.type == bfd_link_hash_defweak) 3369 && (h->root.u.def.section->owner == NULL 3370 || (h->root.u.def.section->owner->xvec 3371 != ldinfo->info->output_bfd->xvec))) 3372 h->flags |= XCOFF_MARK; 3373 3374 /* Skip discarded symbols. */ 3375 if (xcoff_hash_table (ldinfo->info)->gc 3376 && (h->flags & XCOFF_MARK) == 0) 3377 return TRUE; 3378 3379 /* If this is still a common symbol, and it wasn't garbage 3380 collected, we need to actually allocate space for it in the .bss 3381 section. */ 3382 if (h->root.type == bfd_link_hash_common 3383 && h->root.u.c.p->section->size == 0) 3384 { 3385 BFD_ASSERT (bfd_is_com_section (h->root.u.c.p->section)); 3386 h->root.u.c.p->section->size = h->root.u.c.size; 3387 } 3388 3389 if (xcoff_hash_table (ldinfo->info)->loader_section) 3390 { 3391 if (xcoff_auto_export_p (ldinfo->info, h, ldinfo->auto_export_flags)) 3392 h->flags |= XCOFF_EXPORT; 3393 3394 if (!xcoff_build_ldsym (ldinfo, h)) 3395 return FALSE; 3396 } 3397 3398 return TRUE; 3399 } 3400 3401 /* INPUT_BFD includes XCOFF symbol ISYM, which is associated with linker 3402 hash table entry H and csect CSECT. AUX contains ISYM's auxillary 3403 csect information, if any. NAME is the function's name if the name 3404 is stored in the .debug section, otherwise it is null. 3405 3406 Return 1 if we should include an appropriately-adjusted ISYM 3407 in the output file, 0 if we should discard ISYM, or -1 if an 3408 error occured. */ 3409 3410 static int 3411 xcoff_keep_symbol_p (struct bfd_link_info *info, bfd *input_bfd, 3412 struct internal_syment *isym, 3413 union internal_auxent *aux, 3414 struct xcoff_link_hash_entry *h, 3415 asection *csect, const char *name) 3416 { 3417 int smtyp; 3418 3419 /* If we are skipping this csect, we want to strip the symbol too. */ 3420 if (csect == NULL) 3421 return 0; 3422 3423 /* Likewise if we garbage-collected the csect. */ 3424 if (xcoff_hash_table (info)->gc 3425 && !bfd_is_abs_section (csect) 3426 && !bfd_is_und_section (csect) 3427 && (csect->flags & SEC_MARK) == 0) 3428 return 0; 3429 3430 /* An XCOFF linker always removes C_STAT symbols. */ 3431 if (isym->n_sclass == C_STAT) 3432 return 0; 3433 3434 /* We generate the TOC anchor separately. */ 3435 if (isym->n_sclass == C_HIDEXT 3436 && aux->x_csect.x_smclas == XMC_TC0) 3437 return 0; 3438 3439 /* If we are stripping all symbols, we want to discard this one. */ 3440 if (info->strip == strip_all) 3441 return 0; 3442 3443 /* Discard symbols that are defined elsewhere. */ 3444 if (EXTERN_SYM_P (isym->n_sclass)) 3445 { 3446 if ((h->flags & XCOFF_ALLOCATED) != 0) 3447 return 0; 3448 if (!xcoff_final_definition_p (input_bfd, h, csect)) 3449 return 0; 3450 } 3451 3452 /* If we're discarding local symbols, check whether ISYM is local. */ 3453 smtyp = SMTYP_SMTYP (aux->x_csect.x_smtyp); 3454 if (info->discard == discard_all 3455 && !EXTERN_SYM_P (isym->n_sclass) 3456 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD)) 3457 return 0; 3458 3459 /* If we're stripping debugging symbols, check whether ISYM is one. */ 3460 if (info->strip == strip_debugger 3461 && isym->n_scnum == N_DEBUG) 3462 return 0; 3463 3464 /* If we are stripping symbols based on name, check how ISYM's 3465 name should be handled. */ 3466 if (info->strip == strip_some 3467 || info->discard == discard_l) 3468 { 3469 char buf[SYMNMLEN + 1]; 3470 3471 if (name == NULL) 3472 { 3473 name = _bfd_coff_internal_syment_name (input_bfd, isym, buf); 3474 if (name == NULL) 3475 return -1; 3476 } 3477 3478 if (info->strip == strip_some 3479 && bfd_hash_lookup (info->keep_hash, name, FALSE, FALSE) == NULL) 3480 return 0; 3481 3482 if (info->discard == discard_l 3483 && !EXTERN_SYM_P (isym->n_sclass) 3484 && (isym->n_sclass != C_HIDEXT || smtyp != XTY_SD) 3485 && bfd_is_local_label_name (input_bfd, name)) 3486 return 0; 3487 } 3488 3489 return 1; 3490 } 3491 3492 /* Lay out the .loader section, filling in the header and the import paths. 3493 LIBPATH is as for bfd_xcoff_size_dynamic_sections. */ 3494 3495 static bfd_boolean 3496 xcoff_build_loader_section (struct xcoff_loader_info *ldinfo, 3497 const char *libpath) 3498 { 3499 bfd *output_bfd; 3500 struct xcoff_link_hash_table *htab; 3501 struct internal_ldhdr *ldhdr; 3502 struct xcoff_import_file *fl; 3503 bfd_size_type stoff; 3504 size_t impsize, impcount; 3505 asection *lsec; 3506 char *out; 3507 3508 /* Work out the size of the import file names. Each import file ID 3509 consists of three null terminated strings: the path, the file 3510 name, and the archive member name. The first entry in the list 3511 of names is the path to use to find objects, which the linker has 3512 passed in as the libpath argument. For some reason, the path 3513 entry in the other import file names appears to always be empty. */ 3514 output_bfd = ldinfo->output_bfd; 3515 htab = xcoff_hash_table (ldinfo->info); 3516 impsize = strlen (libpath) + 3; 3517 impcount = 1; 3518 for (fl = htab->imports; fl != NULL; fl = fl->next) 3519 { 3520 ++impcount; 3521 impsize += (strlen (fl->path) 3522 + strlen (fl->file) 3523 + strlen (fl->member) 3524 + 3); 3525 } 3526 3527 /* Set up the .loader section header. */ 3528 ldhdr = &htab->ldhdr; 3529 ldhdr->l_version = bfd_xcoff_ldhdr_version(output_bfd); 3530 ldhdr->l_nsyms = ldinfo->ldsym_count; 3531 ldhdr->l_nreloc = htab->ldrel_count; 3532 ldhdr->l_istlen = impsize; 3533 ldhdr->l_nimpid = impcount; 3534 ldhdr->l_impoff = (bfd_xcoff_ldhdrsz (output_bfd) 3535 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd) 3536 + ldhdr->l_nreloc * bfd_xcoff_ldrelsz (output_bfd)); 3537 ldhdr->l_stlen = ldinfo->string_size; 3538 stoff = ldhdr->l_impoff + impsize; 3539 if (ldinfo->string_size == 0) 3540 ldhdr->l_stoff = 0; 3541 else 3542 ldhdr->l_stoff = stoff; 3543 3544 /* 64 bit elements to ldhdr 3545 The swap out routine for 32 bit will ignore them. 3546 Nothing fancy, symbols come after the header and relocs come 3547 after symbols. */ 3548 ldhdr->l_symoff = bfd_xcoff_ldhdrsz (output_bfd); 3549 ldhdr->l_rldoff = (bfd_xcoff_ldhdrsz (output_bfd) 3550 + ldhdr->l_nsyms * bfd_xcoff_ldsymsz (output_bfd)); 3551 3552 /* We now know the final size of the .loader section. Allocate 3553 space for it. */ 3554 lsec = htab->loader_section; 3555 lsec->size = stoff + ldhdr->l_stlen; 3556 lsec->contents = bfd_zalloc (output_bfd, lsec->size); 3557 if (lsec->contents == NULL) 3558 return FALSE; 3559 3560 /* Set up the header. */ 3561 bfd_xcoff_swap_ldhdr_out (output_bfd, ldhdr, lsec->contents); 3562 3563 /* Set up the import file names. */ 3564 out = (char *) lsec->contents + ldhdr->l_impoff; 3565 strcpy (out, libpath); 3566 out += strlen (libpath) + 1; 3567 *out++ = '\0'; 3568 *out++ = '\0'; 3569 for (fl = htab->imports; fl != NULL; fl = fl->next) 3570 { 3571 const char *s; 3572 3573 s = fl->path; 3574 while ((*out++ = *s++) != '\0') 3575 ; 3576 s = fl->file; 3577 while ((*out++ = *s++) != '\0') 3578 ; 3579 s = fl->member; 3580 while ((*out++ = *s++) != '\0') 3581 ; 3582 } 3583 3584 BFD_ASSERT ((bfd_size_type) ((bfd_byte *) out - lsec->contents) == stoff); 3585 3586 /* Set up the symbol string table. */ 3587 if (ldinfo->string_size > 0) 3588 { 3589 memcpy (out, ldinfo->strings, ldinfo->string_size); 3590 free (ldinfo->strings); 3591 ldinfo->strings = NULL; 3592 } 3593 3594 /* We can't set up the symbol table or the relocs yet, because we 3595 don't yet know the final position of the various sections. The 3596 .loader symbols are written out when the corresponding normal 3597 symbols are written out in xcoff_link_input_bfd or 3598 xcoff_write_global_symbol. The .loader relocs are written out 3599 when the corresponding normal relocs are handled in 3600 xcoff_link_input_bfd. */ 3601 3602 return TRUE; 3603 } 3604 3605 /* Build the .loader section. This is called by the XCOFF linker 3606 emulation before_allocation routine. We must set the size of the 3607 .loader section before the linker lays out the output file. 3608 LIBPATH is the library path to search for shared objects; this is 3609 normally built from the -L arguments passed to the linker. ENTRY 3610 is the name of the entry point symbol (the -e linker option). 3611 FILE_ALIGN is the alignment to use for sections within the file 3612 (the -H linker option). MAXSTACK is the maximum stack size (the 3613 -bmaxstack linker option). MAXDATA is the maximum data size (the 3614 -bmaxdata linker option). GC is whether to do garbage collection 3615 (the -bgc linker option). MODTYPE is the module type (the 3616 -bmodtype linker option). TEXTRO is whether the text section must 3617 be read only (the -btextro linker option). AUTO_EXPORT_FLAGS 3618 is a mask of XCOFF_EXPALL and XCOFF_EXPFULL. SPECIAL_SECTIONS 3619 is set by this routine to csects with magic names like _end. */ 3620 3621 bfd_boolean 3622 bfd_xcoff_size_dynamic_sections (bfd *output_bfd, 3623 struct bfd_link_info *info, 3624 const char *libpath, 3625 const char *entry, 3626 unsigned long file_align, 3627 unsigned long maxstack, 3628 unsigned long maxdata, 3629 bfd_boolean gc, 3630 int modtype, 3631 bfd_boolean textro, 3632 unsigned int auto_export_flags, 3633 asection **special_sections, 3634 bfd_boolean rtld) 3635 { 3636 struct xcoff_loader_info ldinfo; 3637 int i; 3638 asection *sec; 3639 bfd *sub; 3640 struct bfd_strtab_hash *debug_strtab; 3641 bfd_byte *debug_contents = NULL; 3642 bfd_size_type amt; 3643 3644 if (bfd_get_flavour (output_bfd) != bfd_target_xcoff_flavour) 3645 { 3646 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) 3647 special_sections[i] = NULL; 3648 return TRUE; 3649 } 3650 3651 ldinfo.failed = FALSE; 3652 ldinfo.output_bfd = output_bfd; 3653 ldinfo.info = info; 3654 ldinfo.auto_export_flags = auto_export_flags; 3655 ldinfo.ldsym_count = 0; 3656 ldinfo.string_size = 0; 3657 ldinfo.strings = NULL; 3658 ldinfo.string_alc = 0; 3659 3660 xcoff_data (output_bfd)->maxstack = maxstack; 3661 xcoff_data (output_bfd)->maxdata = maxdata; 3662 xcoff_data (output_bfd)->modtype = modtype; 3663 3664 xcoff_hash_table (info)->file_align = file_align; 3665 xcoff_hash_table (info)->textro = textro; 3666 xcoff_hash_table (info)->rtld = rtld; 3667 3668 /* __rtinit */ 3669 if (xcoff_hash_table (info)->loader_section 3670 && (info->init_function || info->fini_function || rtld)) 3671 { 3672 struct xcoff_link_hash_entry *hsym; 3673 struct internal_ldsym *ldsym; 3674 3675 hsym = xcoff_link_hash_lookup (xcoff_hash_table (info), 3676 "__rtinit", FALSE, FALSE, TRUE); 3677 if (hsym == NULL) 3678 { 3679 _bfd_error_handler 3680 (_("error: undefined symbol __rtinit")); 3681 return FALSE; 3682 } 3683 3684 xcoff_mark_symbol (info, hsym); 3685 hsym->flags |= (XCOFF_DEF_REGULAR | XCOFF_RTINIT); 3686 3687 /* __rtinit initialized. */ 3688 amt = sizeof (* ldsym); 3689 ldsym = bfd_malloc (amt); 3690 3691 ldsym->l_value = 0; /* Will be filled in later. */ 3692 ldsym->l_scnum = 2; /* Data section. */ 3693 ldsym->l_smtype = XTY_SD; /* Csect section definition. */ 3694 ldsym->l_smclas = 5; /* .rw. */ 3695 ldsym->l_ifile = 0; /* Special system loader symbol. */ 3696 ldsym->l_parm = 0; /* NA. */ 3697 3698 /* Force __rtinit to be the first symbol in the loader symbol table 3699 See xcoff_build_ldsyms 3700 3701 The first 3 symbol table indices are reserved to indicate the data, 3702 text and bss sections. */ 3703 BFD_ASSERT (0 == ldinfo.ldsym_count); 3704 3705 hsym->ldindx = 3; 3706 ldinfo.ldsym_count = 1; 3707 hsym->ldsym = ldsym; 3708 3709 if (! bfd_xcoff_put_ldsymbol_name (ldinfo.output_bfd, &ldinfo, 3710 hsym->ldsym, hsym->root.root.string)) 3711 return FALSE; 3712 3713 /* This symbol is written out by xcoff_write_global_symbol 3714 Set stuff up so xcoff_write_global_symbol logic works. */ 3715 hsym->flags |= XCOFF_DEF_REGULAR | XCOFF_MARK; 3716 hsym->root.type = bfd_link_hash_defined; 3717 hsym->root.u.def.value = 0; 3718 } 3719 3720 /* Garbage collect unused sections. */ 3721 if (bfd_link_relocatable (info) || !gc) 3722 { 3723 gc = FALSE; 3724 xcoff_hash_table (info)->gc = FALSE; 3725 3726 /* We still need to call xcoff_mark, in order to set ldrel_count 3727 correctly. */ 3728 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 3729 { 3730 asection *o; 3731 3732 for (o = sub->sections; o != NULL; o = o->next) 3733 { 3734 /* We shouldn't unconditionaly mark the TOC section. 3735 The output file should only have a TOC if either 3736 (a) one of the input files did or (b) we end up 3737 creating TOC references as part of the link process. */ 3738 if (o != xcoff_hash_table (info)->toc_section 3739 && (o->flags & SEC_MARK) == 0) 3740 { 3741 if (! xcoff_mark (info, o)) 3742 goto error_return; 3743 } 3744 } 3745 } 3746 } 3747 else 3748 { 3749 if (entry != NULL 3750 && !xcoff_mark_symbol_by_name (info, entry, XCOFF_ENTRY)) 3751 goto error_return; 3752 if (info->init_function != NULL 3753 && !xcoff_mark_symbol_by_name (info, info->init_function, 0)) 3754 goto error_return; 3755 if (info->fini_function != NULL 3756 && !xcoff_mark_symbol_by_name (info, info->fini_function, 0)) 3757 goto error_return; 3758 if (auto_export_flags != 0) 3759 { 3760 xcoff_link_hash_traverse (xcoff_hash_table (info), 3761 xcoff_mark_auto_exports, &ldinfo); 3762 if (ldinfo.failed) 3763 goto error_return; 3764 } 3765 xcoff_sweep (info); 3766 xcoff_hash_table (info)->gc = TRUE; 3767 } 3768 3769 /* Return special sections to the caller. */ 3770 for (i = 0; i < XCOFF_NUMBER_OF_SPECIAL_SECTIONS; i++) 3771 { 3772 sec = xcoff_hash_table (info)->special_sections[i]; 3773 3774 if (sec != NULL 3775 && gc 3776 && (sec->flags & SEC_MARK) == 0) 3777 sec = NULL; 3778 3779 special_sections[i] = sec; 3780 } 3781 3782 if (info->input_bfds == NULL) 3783 /* I'm not sure what to do in this bizarre case. */ 3784 return TRUE; 3785 3786 xcoff_link_hash_traverse (xcoff_hash_table (info), xcoff_post_gc_symbol, 3787 (void *) &ldinfo); 3788 if (ldinfo.failed) 3789 goto error_return; 3790 3791 if (xcoff_hash_table (info)->loader_section 3792 && !xcoff_build_loader_section (&ldinfo, libpath)) 3793 goto error_return; 3794 3795 /* Allocate space for the magic sections. */ 3796 sec = xcoff_hash_table (info)->linkage_section; 3797 if (sec->size > 0) 3798 { 3799 sec->contents = bfd_zalloc (output_bfd, sec->size); 3800 if (sec->contents == NULL) 3801 goto error_return; 3802 } 3803 sec = xcoff_hash_table (info)->toc_section; 3804 if (sec->size > 0) 3805 { 3806 sec->contents = bfd_zalloc (output_bfd, sec->size); 3807 if (sec->contents == NULL) 3808 goto error_return; 3809 } 3810 sec = xcoff_hash_table (info)->descriptor_section; 3811 if (sec->size > 0) 3812 { 3813 sec->contents = bfd_zalloc (output_bfd, sec->size); 3814 if (sec->contents == NULL) 3815 goto error_return; 3816 } 3817 3818 /* Now that we've done garbage collection, decide which symbols to keep, 3819 and figure out the contents of the .debug section. */ 3820 debug_strtab = xcoff_hash_table (info)->debug_strtab; 3821 3822 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 3823 { 3824 asection *subdeb; 3825 bfd_size_type symcount; 3826 long *debug_index; 3827 asection **csectpp; 3828 unsigned int *lineno_counts; 3829 struct xcoff_link_hash_entry **sym_hash; 3830 bfd_byte *esym, *esymend; 3831 bfd_size_type symesz; 3832 3833 if (sub->xvec != info->output_bfd->xvec) 3834 continue; 3835 3836 if ((sub->flags & DYNAMIC) != 0 3837 && !info->static_link) 3838 continue; 3839 3840 if (! _bfd_coff_get_external_symbols (sub)) 3841 goto error_return; 3842 3843 symcount = obj_raw_syment_count (sub); 3844 debug_index = bfd_zalloc (sub, symcount * sizeof (long)); 3845 if (debug_index == NULL) 3846 goto error_return; 3847 xcoff_data (sub)->debug_indices = debug_index; 3848 3849 if (info->strip == strip_all 3850 || info->strip == strip_debugger 3851 || info->discard == discard_all) 3852 /* We're stripping all debugging information, so there's no need 3853 to read SUB's .debug section. */ 3854 subdeb = NULL; 3855 else 3856 { 3857 /* Grab the contents of SUB's .debug section, if any. */ 3858 subdeb = bfd_get_section_by_name (sub, ".debug"); 3859 if (subdeb != NULL && subdeb->size > 0) 3860 { 3861 /* We use malloc and copy the names into the debug 3862 stringtab, rather than bfd_alloc, because I expect 3863 that, when linking many files together, many of the 3864 strings will be the same. Storing the strings in the 3865 hash table should save space in this case. */ 3866 if (!bfd_malloc_and_get_section (sub, subdeb, &debug_contents)) 3867 goto error_return; 3868 } 3869 } 3870 3871 csectpp = xcoff_data (sub)->csects; 3872 lineno_counts = xcoff_data (sub)->lineno_counts; 3873 sym_hash = obj_xcoff_sym_hashes (sub); 3874 symesz = bfd_coff_symesz (sub); 3875 esym = (bfd_byte *) obj_coff_external_syms (sub); 3876 esymend = esym + symcount * symesz; 3877 3878 while (esym < esymend) 3879 { 3880 struct internal_syment sym; 3881 union internal_auxent aux; 3882 asection *csect; 3883 const char *name; 3884 int keep_p; 3885 3886 bfd_coff_swap_sym_in (sub, esym, &sym); 3887 3888 /* Read in the csect information, if any. */ 3889 if (CSECT_SYM_P (sym.n_sclass)) 3890 { 3891 BFD_ASSERT (sym.n_numaux > 0); 3892 bfd_coff_swap_aux_in (sub, esym + symesz * sym.n_numaux, 3893 sym.n_type, sym.n_sclass, 3894 sym.n_numaux - 1, sym.n_numaux, &aux); 3895 } 3896 3897 /* If this symbol's name is stored in the debug section, 3898 get a pointer to it. */ 3899 if (debug_contents != NULL 3900 && sym._n._n_n._n_zeroes == 0 3901 && bfd_coff_symname_in_debug (sub, &sym)) 3902 name = (const char *) debug_contents + sym._n._n_n._n_offset; 3903 else 3904 name = NULL; 3905 3906 /* Decide whether to copy this symbol to the output file. */ 3907 csect = *csectpp; 3908 keep_p = xcoff_keep_symbol_p (info, sub, &sym, &aux, 3909 *sym_hash, csect, name); 3910 if (keep_p < 0) 3911 return FALSE; 3912 3913 if (!keep_p) 3914 /* Use a debug_index of -2 to record that a symbol should 3915 be stripped. */ 3916 *debug_index = -2; 3917 else 3918 { 3919 /* See whether we should store the symbol name in the 3920 output .debug section. */ 3921 if (name != NULL) 3922 { 3923 bfd_size_type indx; 3924 3925 indx = _bfd_stringtab_add (debug_strtab, name, TRUE, TRUE); 3926 if (indx == (bfd_size_type) -1) 3927 goto error_return; 3928 *debug_index = indx; 3929 } 3930 else 3931 *debug_index = -1; 3932 if (*sym_hash != 0) 3933 (*sym_hash)->flags |= XCOFF_ALLOCATED; 3934 if (*lineno_counts > 0) 3935 csect->output_section->lineno_count += *lineno_counts; 3936 } 3937 3938 esym += (sym.n_numaux + 1) * symesz; 3939 csectpp += sym.n_numaux + 1; 3940 sym_hash += sym.n_numaux + 1; 3941 lineno_counts += sym.n_numaux + 1; 3942 debug_index += sym.n_numaux + 1; 3943 } 3944 3945 if (debug_contents) 3946 { 3947 free (debug_contents); 3948 debug_contents = NULL; 3949 3950 /* Clear the size of subdeb, so that it is not included directly 3951 in the output file. */ 3952 subdeb->size = 0; 3953 } 3954 3955 if (! info->keep_memory) 3956 { 3957 if (! _bfd_coff_free_symbols (sub)) 3958 goto error_return; 3959 } 3960 } 3961 3962 if (info->strip != strip_all) 3963 xcoff_hash_table (info)->debug_section->size = 3964 _bfd_stringtab_size (debug_strtab); 3965 3966 return TRUE; 3967 3968 error_return: 3969 if (ldinfo.strings != NULL) 3970 free (ldinfo.strings); 3971 if (debug_contents != NULL) 3972 free (debug_contents); 3973 return FALSE; 3974 } 3975 3976 bfd_boolean 3977 bfd_xcoff_link_generate_rtinit (bfd *abfd, 3978 const char *init, 3979 const char *fini, 3980 bfd_boolean rtld) 3981 { 3982 struct bfd_in_memory *bim; 3983 3984 bim = bfd_malloc ((bfd_size_type) sizeof (* bim)); 3985 if (bim == NULL) 3986 return FALSE; 3987 3988 bim->size = 0; 3989 bim->buffer = 0; 3990 3991 abfd->link.next = 0; 3992 abfd->format = bfd_object; 3993 abfd->iostream = (void *) bim; 3994 abfd->flags = BFD_IN_MEMORY; 3995 abfd->iovec = &_bfd_memory_iovec; 3996 abfd->direction = write_direction; 3997 abfd->origin = 0; 3998 abfd->where = 0; 3999 4000 if (! bfd_xcoff_generate_rtinit (abfd, init, fini, rtld)) 4001 return FALSE; 4002 4003 /* need to reset to unknown or it will not be read back in correctly */ 4004 abfd->format = bfd_unknown; 4005 abfd->direction = read_direction; 4006 abfd->where = 0; 4007 4008 return TRUE; 4009 } 4010 4011 /* Return the section that defines H. Return null if no section does. */ 4012 4013 static asection * 4014 xcoff_symbol_section (struct xcoff_link_hash_entry *h) 4015 { 4016 switch (h->root.type) 4017 { 4018 case bfd_link_hash_defined: 4019 case bfd_link_hash_defweak: 4020 return h->root.u.def.section; 4021 4022 case bfd_link_hash_common: 4023 return h->root.u.c.p->section; 4024 4025 default: 4026 return NULL; 4027 } 4028 } 4029 4030 /* Add a .loader relocation for input relocation IREL. If the loader 4031 relocation should be against an output section, HSEC points to the 4032 input section that IREL is against, otherwise HSEC is null. H is the 4033 symbol that IREL is against, or null if it isn't against a global symbol. 4034 REFERENCE_BFD is the bfd to use in error messages about the relocation. */ 4035 4036 static bfd_boolean 4037 xcoff_create_ldrel (bfd *output_bfd, struct xcoff_final_link_info *flinfo, 4038 asection *output_section, bfd *reference_bfd, 4039 struct internal_reloc *irel, asection *hsec, 4040 struct xcoff_link_hash_entry *h) 4041 { 4042 struct internal_ldrel ldrel; 4043 4044 ldrel.l_vaddr = irel->r_vaddr; 4045 if (hsec != NULL) 4046 { 4047 const char *secname; 4048 4049 secname = hsec->output_section->name; 4050 if (strcmp (secname, ".text") == 0) 4051 ldrel.l_symndx = 0; 4052 else if (strcmp (secname, ".data") == 0) 4053 ldrel.l_symndx = 1; 4054 else if (strcmp (secname, ".bss") == 0) 4055 ldrel.l_symndx = 2; 4056 else 4057 { 4058 _bfd_error_handler 4059 /* xgettext:c-format */ 4060 (_("%pB: loader reloc in unrecognized section `%s'"), 4061 reference_bfd, secname); 4062 bfd_set_error (bfd_error_nonrepresentable_section); 4063 return FALSE; 4064 } 4065 } 4066 else if (h != NULL) 4067 { 4068 if (h->ldindx < 0) 4069 { 4070 _bfd_error_handler 4071 /* xgettext:c-format */ 4072 (_("%pB: `%s' in loader reloc but not loader sym"), 4073 reference_bfd, h->root.root.string); 4074 bfd_set_error (bfd_error_bad_value); 4075 return FALSE; 4076 } 4077 ldrel.l_symndx = h->ldindx; 4078 } 4079 else 4080 ldrel.l_symndx = -(bfd_size_type) 1; 4081 4082 ldrel.l_rtype = (irel->r_size << 8) | irel->r_type; 4083 ldrel.l_rsecnm = output_section->target_index; 4084 if (xcoff_hash_table (flinfo->info)->textro 4085 && strcmp (output_section->name, ".text") == 0) 4086 { 4087 _bfd_error_handler 4088 /* xgettext:c-format */ 4089 (_("%pB: loader reloc in read-only section %pA"), 4090 reference_bfd, output_section); 4091 bfd_set_error (bfd_error_invalid_operation); 4092 return FALSE; 4093 } 4094 bfd_xcoff_swap_ldrel_out (output_bfd, &ldrel, flinfo->ldrel); 4095 flinfo->ldrel += bfd_xcoff_ldrelsz (output_bfd); 4096 return TRUE; 4097 } 4098 4099 /* Link an input file into the linker output file. This function 4100 handles all the sections and relocations of the input file at once. */ 4101 4102 static bfd_boolean 4103 xcoff_link_input_bfd (struct xcoff_final_link_info *flinfo, 4104 bfd *input_bfd) 4105 { 4106 bfd *output_bfd; 4107 const char *strings; 4108 bfd_size_type syment_base; 4109 unsigned int n_tmask; 4110 unsigned int n_btshft; 4111 bfd_boolean copy, hash; 4112 bfd_size_type isymesz; 4113 bfd_size_type osymesz; 4114 bfd_size_type linesz; 4115 bfd_byte *esym; 4116 bfd_byte *esym_end; 4117 struct xcoff_link_hash_entry **sym_hash; 4118 struct internal_syment *isymp; 4119 asection **csectpp; 4120 unsigned int *lineno_counts; 4121 long *debug_index; 4122 long *indexp; 4123 unsigned long output_index; 4124 bfd_byte *outsym; 4125 unsigned int incls; 4126 asection *oline; 4127 bfd_boolean keep_syms; 4128 asection *o; 4129 4130 /* We can just skip DYNAMIC files, unless this is a static link. */ 4131 if ((input_bfd->flags & DYNAMIC) != 0 4132 && ! flinfo->info->static_link) 4133 return TRUE; 4134 4135 /* Move all the symbols to the output file. */ 4136 output_bfd = flinfo->output_bfd; 4137 strings = NULL; 4138 syment_base = obj_raw_syment_count (output_bfd); 4139 isymesz = bfd_coff_symesz (input_bfd); 4140 osymesz = bfd_coff_symesz (output_bfd); 4141 linesz = bfd_coff_linesz (input_bfd); 4142 BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd)); 4143 4144 n_tmask = coff_data (input_bfd)->local_n_tmask; 4145 n_btshft = coff_data (input_bfd)->local_n_btshft; 4146 4147 /* Define macros so that ISFCN, et. al., macros work correctly. */ 4148 #define N_TMASK n_tmask 4149 #define N_BTSHFT n_btshft 4150 4151 copy = FALSE; 4152 if (! flinfo->info->keep_memory) 4153 copy = TRUE; 4154 hash = TRUE; 4155 if (flinfo->info->traditional_format) 4156 hash = FALSE; 4157 4158 if (! _bfd_coff_get_external_symbols (input_bfd)) 4159 return FALSE; 4160 4161 /* Make one pass over the symbols and assign indices to symbols that 4162 we have decided to keep. Also use create .loader symbol information 4163 and update information in hash table entries. */ 4164 esym = (bfd_byte *) obj_coff_external_syms (input_bfd); 4165 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; 4166 sym_hash = obj_xcoff_sym_hashes (input_bfd); 4167 csectpp = xcoff_data (input_bfd)->csects; 4168 debug_index = xcoff_data (input_bfd)->debug_indices; 4169 isymp = flinfo->internal_syms; 4170 indexp = flinfo->sym_indices; 4171 output_index = syment_base; 4172 while (esym < esym_end) 4173 { 4174 union internal_auxent aux; 4175 int smtyp = 0; 4176 int add; 4177 4178 bfd_coff_swap_sym_in (input_bfd, (void *) esym, (void *) isymp); 4179 4180 /* Read in the csect information, if any. */ 4181 if (CSECT_SYM_P (isymp->n_sclass)) 4182 { 4183 BFD_ASSERT (isymp->n_numaux > 0); 4184 bfd_coff_swap_aux_in (input_bfd, 4185 (void *) (esym + isymesz * isymp->n_numaux), 4186 isymp->n_type, isymp->n_sclass, 4187 isymp->n_numaux - 1, isymp->n_numaux, 4188 (void *) &aux); 4189 4190 smtyp = SMTYP_SMTYP (aux.x_csect.x_smtyp); 4191 } 4192 4193 /* If this symbol is in the .loader section, swap out the 4194 .loader symbol information. If this is an external symbol 4195 reference to a defined symbol, though, then wait until we get 4196 to the definition. */ 4197 if (EXTERN_SYM_P (isymp->n_sclass) 4198 && *sym_hash != NULL 4199 && (*sym_hash)->ldsym != NULL 4200 && xcoff_final_definition_p (input_bfd, *sym_hash, *csectpp)) 4201 { 4202 struct xcoff_link_hash_entry *h; 4203 struct internal_ldsym *ldsym; 4204 4205 h = *sym_hash; 4206 ldsym = h->ldsym; 4207 if (isymp->n_scnum > 0) 4208 { 4209 ldsym->l_scnum = (*csectpp)->output_section->target_index; 4210 ldsym->l_value = (isymp->n_value 4211 + (*csectpp)->output_section->vma 4212 + (*csectpp)->output_offset 4213 - (*csectpp)->vma); 4214 } 4215 else 4216 { 4217 ldsym->l_scnum = isymp->n_scnum; 4218 ldsym->l_value = isymp->n_value; 4219 } 4220 4221 ldsym->l_smtype = smtyp; 4222 if (((h->flags & XCOFF_DEF_REGULAR) == 0 4223 && (h->flags & XCOFF_DEF_DYNAMIC) != 0) 4224 || (h->flags & XCOFF_IMPORT) != 0) 4225 ldsym->l_smtype |= L_IMPORT; 4226 if (((h->flags & XCOFF_DEF_REGULAR) != 0 4227 && (h->flags & XCOFF_DEF_DYNAMIC) != 0) 4228 || (h->flags & XCOFF_EXPORT) != 0) 4229 ldsym->l_smtype |= L_EXPORT; 4230 if ((h->flags & XCOFF_ENTRY) != 0) 4231 ldsym->l_smtype |= L_ENTRY; 4232 if (isymp->n_sclass == C_AIX_WEAKEXT) 4233 ldsym->l_smtype |= L_WEAK; 4234 4235 ldsym->l_smclas = aux.x_csect.x_smclas; 4236 4237 if (ldsym->l_ifile == (bfd_size_type) -1) 4238 ldsym->l_ifile = 0; 4239 else if (ldsym->l_ifile == 0) 4240 { 4241 if ((ldsym->l_smtype & L_IMPORT) == 0) 4242 ldsym->l_ifile = 0; 4243 else 4244 { 4245 bfd *impbfd; 4246 4247 if (h->root.type == bfd_link_hash_defined 4248 || h->root.type == bfd_link_hash_defweak) 4249 impbfd = h->root.u.def.section->owner; 4250 else if (h->root.type == bfd_link_hash_undefined 4251 || h->root.type == bfd_link_hash_undefweak) 4252 impbfd = h->root.u.undef.abfd; 4253 else 4254 impbfd = NULL; 4255 4256 if (impbfd == NULL) 4257 ldsym->l_ifile = 0; 4258 else 4259 { 4260 BFD_ASSERT (impbfd->xvec == flinfo->output_bfd->xvec); 4261 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; 4262 } 4263 } 4264 } 4265 4266 ldsym->l_parm = 0; 4267 4268 BFD_ASSERT (h->ldindx >= 0); 4269 bfd_xcoff_swap_ldsym_out (flinfo->output_bfd, ldsym, 4270 (flinfo->ldsym 4271 + ((h->ldindx - 3) 4272 * bfd_xcoff_ldsymsz (flinfo->output_bfd)))); 4273 h->ldsym = NULL; 4274 4275 /* Fill in snentry now that we know the target_index. */ 4276 if ((h->flags & XCOFF_ENTRY) != 0 4277 && (h->root.type == bfd_link_hash_defined 4278 || h->root.type == bfd_link_hash_defweak)) 4279 { 4280 xcoff_data (output_bfd)->snentry = 4281 h->root.u.def.section->output_section->target_index; 4282 } 4283 } 4284 4285 add = 1 + isymp->n_numaux; 4286 4287 if (*debug_index == -2) 4288 /* We've decided to strip this symbol. */ 4289 *indexp = -1; 4290 else 4291 { 4292 /* Assign the next unused index to this symbol. */ 4293 *indexp = output_index; 4294 4295 if (EXTERN_SYM_P (isymp->n_sclass)) 4296 { 4297 BFD_ASSERT (*sym_hash != NULL); 4298 (*sym_hash)->indx = output_index; 4299 } 4300 4301 /* If this is a symbol in the TOC which we may have merged 4302 (class XMC_TC), remember the symbol index of the TOC 4303 symbol. */ 4304 if (isymp->n_sclass == C_HIDEXT 4305 && aux.x_csect.x_smclas == XMC_TC 4306 && *sym_hash != NULL) 4307 { 4308 BFD_ASSERT (((*sym_hash)->flags & XCOFF_SET_TOC) == 0); 4309 BFD_ASSERT ((*sym_hash)->toc_section != NULL); 4310 (*sym_hash)->u.toc_indx = output_index; 4311 } 4312 4313 output_index += add; 4314 } 4315 4316 esym += add * isymesz; 4317 isymp += add; 4318 csectpp += add; 4319 sym_hash += add; 4320 debug_index += add; 4321 ++indexp; 4322 for (--add; add > 0; --add) 4323 *indexp++ = -1; 4324 } 4325 4326 /* Now write out the symbols that we decided to keep. */ 4327 4328 esym = (bfd_byte *) obj_coff_external_syms (input_bfd); 4329 esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz; 4330 sym_hash = obj_xcoff_sym_hashes (input_bfd); 4331 isymp = flinfo->internal_syms; 4332 indexp = flinfo->sym_indices; 4333 csectpp = xcoff_data (input_bfd)->csects; 4334 lineno_counts = xcoff_data (input_bfd)->lineno_counts; 4335 debug_index = xcoff_data (input_bfd)->debug_indices; 4336 outsym = flinfo->outsyms; 4337 incls = 0; 4338 oline = NULL; 4339 while (esym < esym_end) 4340 { 4341 int add; 4342 4343 add = 1 + isymp->n_numaux; 4344 4345 if (*indexp < 0) 4346 esym += add * isymesz; 4347 else 4348 { 4349 struct internal_syment isym; 4350 int i; 4351 4352 /* Adjust the symbol in order to output it. */ 4353 isym = *isymp; 4354 if (isym._n._n_n._n_zeroes == 0 4355 && isym._n._n_n._n_offset != 0) 4356 { 4357 /* This symbol has a long name. Enter it in the string 4358 table we are building. If *debug_index != -1, the 4359 name has already been entered in the .debug section. */ 4360 if (*debug_index >= 0) 4361 isym._n._n_n._n_offset = *debug_index; 4362 else 4363 { 4364 const char *name; 4365 bfd_size_type indx; 4366 4367 name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL); 4368 4369 if (name == NULL) 4370 return FALSE; 4371 indx = _bfd_stringtab_add (flinfo->strtab, name, hash, copy); 4372 if (indx == (bfd_size_type) -1) 4373 return FALSE; 4374 isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx; 4375 } 4376 } 4377 4378 /* Make __rtinit C_HIDEXT rather than C_EXT. This avoids 4379 multiple definition problems when linking a shared object 4380 statically. (The native linker doesn't enter __rtinit into 4381 the normal table at all, but having a local symbol can make 4382 the objdump output easier to read.) */ 4383 if (isym.n_sclass == C_EXT 4384 && *sym_hash 4385 && ((*sym_hash)->flags & XCOFF_RTINIT) != 0) 4386 isym.n_sclass = C_HIDEXT; 4387 4388 /* The value of a C_FILE symbol is the symbol index of the 4389 next C_FILE symbol. The value of the last C_FILE symbol 4390 is -1. We try to get this right, below, just before we 4391 write the symbols out, but in the general case we may 4392 have to write the symbol out twice. */ 4393 if (isym.n_sclass == C_FILE) 4394 { 4395 if (flinfo->last_file_index != -1 4396 && flinfo->last_file.n_value != (bfd_vma) *indexp) 4397 { 4398 /* We must correct the value of the last C_FILE entry. */ 4399 flinfo->last_file.n_value = *indexp; 4400 if ((bfd_size_type) flinfo->last_file_index >= syment_base) 4401 { 4402 /* The last C_FILE symbol is in this input file. */ 4403 bfd_coff_swap_sym_out (output_bfd, 4404 (void *) &flinfo->last_file, 4405 (void *) (flinfo->outsyms 4406 + ((flinfo->last_file_index 4407 - syment_base) 4408 * osymesz))); 4409 } 4410 else 4411 { 4412 /* We have already written out the last C_FILE 4413 symbol. We need to write it out again. We 4414 borrow *outsym temporarily. */ 4415 file_ptr pos; 4416 4417 bfd_coff_swap_sym_out (output_bfd, 4418 (void *) &flinfo->last_file, 4419 (void *) outsym); 4420 4421 pos = obj_sym_filepos (output_bfd); 4422 pos += flinfo->last_file_index * osymesz; 4423 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 4424 || (bfd_bwrite (outsym, osymesz, output_bfd) 4425 != osymesz)) 4426 return FALSE; 4427 } 4428 } 4429 4430 flinfo->last_file_index = *indexp; 4431 flinfo->last_file = isym; 4432 } 4433 4434 /* The value of a C_BINCL or C_EINCL symbol is a file offset 4435 into the line numbers. We update the symbol values when 4436 we handle the line numbers. */ 4437 if (isym.n_sclass == C_BINCL 4438 || isym.n_sclass == C_EINCL) 4439 { 4440 isym.n_value = flinfo->line_filepos; 4441 ++incls; 4442 } 4443 /* The value of a C_BSTAT symbol is the symbol table 4444 index of the containing csect. */ 4445 else if (isym.n_sclass == C_BSTAT) 4446 { 4447 bfd_vma indx; 4448 4449 indx = isym.n_value; 4450 if (indx < obj_raw_syment_count (input_bfd)) 4451 { 4452 long symindx; 4453 4454 symindx = flinfo->sym_indices[indx]; 4455 if (symindx < 0) 4456 isym.n_value = 0; 4457 else 4458 isym.n_value = symindx; 4459 } 4460 } 4461 else if (isym.n_sclass != C_ESTAT 4462 && isym.n_sclass != C_DECL 4463 && isym.n_scnum > 0) 4464 { 4465 isym.n_scnum = (*csectpp)->output_section->target_index; 4466 isym.n_value += ((*csectpp)->output_section->vma 4467 + (*csectpp)->output_offset 4468 - (*csectpp)->vma); 4469 } 4470 4471 /* Output the symbol. */ 4472 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym); 4473 4474 esym += isymesz; 4475 outsym += osymesz; 4476 4477 for (i = 0; i < isymp->n_numaux && esym < esym_end; i++) 4478 { 4479 union internal_auxent aux; 4480 4481 bfd_coff_swap_aux_in (input_bfd, (void *) esym, isymp->n_type, 4482 isymp->n_sclass, i, isymp->n_numaux, 4483 (void *) &aux); 4484 4485 if (isymp->n_sclass == C_FILE) 4486 { 4487 /* This is the file name (or some comment put in by 4488 the compiler). If it is long, we must put it in 4489 the string table. */ 4490 if (aux.x_file.x_n.x_zeroes == 0 4491 && aux.x_file.x_n.x_offset != 0) 4492 { 4493 const char *filename; 4494 bfd_size_type indx; 4495 4496 BFD_ASSERT (aux.x_file.x_n.x_offset 4497 >= STRING_SIZE_SIZE); 4498 if (strings == NULL) 4499 { 4500 strings = _bfd_coff_read_string_table (input_bfd); 4501 if (strings == NULL) 4502 return FALSE; 4503 } 4504 if ((bfd_size_type) aux.x_file.x_n.x_offset >= obj_coff_strings_len (input_bfd)) 4505 filename = _("<corrupt>"); 4506 else 4507 filename = strings + aux.x_file.x_n.x_offset; 4508 indx = _bfd_stringtab_add (flinfo->strtab, filename, 4509 hash, copy); 4510 if (indx == (bfd_size_type) -1) 4511 return FALSE; 4512 aux.x_file.x_n.x_offset = STRING_SIZE_SIZE + indx; 4513 } 4514 } 4515 else if (CSECT_SYM_P (isymp->n_sclass) 4516 && i + 1 == isymp->n_numaux) 4517 { 4518 4519 /* We don't support type checking. I don't know if 4520 anybody does. */ 4521 aux.x_csect.x_parmhash = 0; 4522 /* I don't think anybody uses these fields, but we'd 4523 better clobber them just in case. */ 4524 aux.x_csect.x_stab = 0; 4525 aux.x_csect.x_snstab = 0; 4526 4527 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_LD) 4528 { 4529 unsigned long indx; 4530 4531 indx = aux.x_csect.x_scnlen.l; 4532 if (indx < obj_raw_syment_count (input_bfd)) 4533 { 4534 long symindx; 4535 4536 symindx = flinfo->sym_indices[indx]; 4537 if (symindx < 0) 4538 { 4539 aux.x_csect.x_scnlen.l = 0; 4540 } 4541 else 4542 { 4543 aux.x_csect.x_scnlen.l = symindx; 4544 } 4545 } 4546 } 4547 } 4548 else if (isymp->n_sclass != C_STAT || isymp->n_type != T_NULL) 4549 { 4550 unsigned long indx; 4551 4552 if (ISFCN (isymp->n_type) 4553 || ISTAG (isymp->n_sclass) 4554 || isymp->n_sclass == C_BLOCK 4555 || isymp->n_sclass == C_FCN) 4556 { 4557 indx = aux.x_sym.x_fcnary.x_fcn.x_endndx.l; 4558 if (indx > 0 4559 && indx < obj_raw_syment_count (input_bfd)) 4560 { 4561 /* We look forward through the symbol for 4562 the index of the next symbol we are going 4563 to include. I don't know if this is 4564 entirely right. */ 4565 while (flinfo->sym_indices[indx] < 0 4566 && indx < obj_raw_syment_count (input_bfd)) 4567 ++indx; 4568 if (indx >= obj_raw_syment_count (input_bfd)) 4569 indx = output_index; 4570 else 4571 indx = flinfo->sym_indices[indx]; 4572 aux.x_sym.x_fcnary.x_fcn.x_endndx.l = indx; 4573 4574 } 4575 } 4576 4577 indx = aux.x_sym.x_tagndx.l; 4578 if (indx > 0 && indx < obj_raw_syment_count (input_bfd)) 4579 { 4580 long symindx; 4581 4582 symindx = flinfo->sym_indices[indx]; 4583 if (symindx < 0) 4584 aux.x_sym.x_tagndx.l = 0; 4585 else 4586 aux.x_sym.x_tagndx.l = symindx; 4587 } 4588 4589 } 4590 4591 /* Copy over the line numbers, unless we are stripping 4592 them. We do this on a symbol by symbol basis in 4593 order to more easily handle garbage collection. */ 4594 if (CSECT_SYM_P (isymp->n_sclass) 4595 && i == 0 4596 && isymp->n_numaux > 1 4597 && ISFCN (isymp->n_type) 4598 && aux.x_sym.x_fcnary.x_fcn.x_lnnoptr != 0) 4599 { 4600 if (*lineno_counts == 0) 4601 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = 0; 4602 else 4603 { 4604 asection *enclosing; 4605 unsigned int enc_count; 4606 bfd_signed_vma linoff; 4607 struct internal_lineno lin; 4608 bfd_byte *linp; 4609 bfd_byte *linpend; 4610 bfd_vma offset; 4611 file_ptr pos; 4612 bfd_size_type amt; 4613 4614 /* Read in the enclosing section's line-number 4615 information, if we haven't already. */ 4616 o = *csectpp; 4617 enclosing = xcoff_section_data (abfd, o)->enclosing; 4618 enc_count = xcoff_section_data (abfd, o)->lineno_count; 4619 if (oline != enclosing) 4620 { 4621 pos = enclosing->line_filepos; 4622 amt = linesz * enc_count; 4623 if (bfd_seek (input_bfd, pos, SEEK_SET) != 0 4624 || (bfd_bread (flinfo->linenos, amt, input_bfd) 4625 != amt)) 4626 return FALSE; 4627 oline = enclosing; 4628 } 4629 4630 /* Copy across the first entry, adjusting its 4631 symbol index. */ 4632 linoff = (aux.x_sym.x_fcnary.x_fcn.x_lnnoptr 4633 - enclosing->line_filepos); 4634 linp = flinfo->linenos + linoff; 4635 bfd_coff_swap_lineno_in (input_bfd, linp, &lin); 4636 lin.l_addr.l_symndx = *indexp; 4637 bfd_coff_swap_lineno_out (output_bfd, &lin, linp); 4638 4639 /* Copy the other entries, adjusting their addresses. */ 4640 linpend = linp + *lineno_counts * linesz; 4641 offset = (o->output_section->vma 4642 + o->output_offset 4643 - o->vma); 4644 for (linp += linesz; linp < linpend; linp += linesz) 4645 { 4646 bfd_coff_swap_lineno_in (input_bfd, linp, &lin); 4647 lin.l_addr.l_paddr += offset; 4648 bfd_coff_swap_lineno_out (output_bfd, &lin, linp); 4649 } 4650 4651 /* Write out the entries we've just processed. */ 4652 pos = (o->output_section->line_filepos 4653 + o->output_section->lineno_count * linesz); 4654 amt = linesz * *lineno_counts; 4655 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 4656 || bfd_bwrite (flinfo->linenos + linoff, 4657 amt, output_bfd) != amt) 4658 return FALSE; 4659 o->output_section->lineno_count += *lineno_counts; 4660 4661 /* Record the offset of the symbol's line numbers 4662 in the output file. */ 4663 aux.x_sym.x_fcnary.x_fcn.x_lnnoptr = pos; 4664 4665 if (incls > 0) 4666 { 4667 struct internal_syment *iisp, *iispend; 4668 long *iindp; 4669 bfd_byte *oos; 4670 bfd_vma range_start, range_end; 4671 int iiadd; 4672 4673 /* Update any C_BINCL or C_EINCL symbols 4674 that refer to a line number in the 4675 range we just output. */ 4676 iisp = flinfo->internal_syms; 4677 iispend = iisp + obj_raw_syment_count (input_bfd); 4678 iindp = flinfo->sym_indices; 4679 oos = flinfo->outsyms; 4680 range_start = enclosing->line_filepos + linoff; 4681 range_end = range_start + *lineno_counts * linesz; 4682 while (iisp < iispend) 4683 { 4684 if (*iindp >= 0 4685 && (iisp->n_sclass == C_BINCL 4686 || iisp->n_sclass == C_EINCL) 4687 && iisp->n_value >= range_start 4688 && iisp->n_value < range_end) 4689 { 4690 struct internal_syment iis; 4691 4692 bfd_coff_swap_sym_in (output_bfd, oos, &iis); 4693 iis.n_value = (iisp->n_value 4694 - range_start 4695 + pos); 4696 bfd_coff_swap_sym_out (output_bfd, 4697 &iis, oos); 4698 --incls; 4699 } 4700 4701 iiadd = 1 + iisp->n_numaux; 4702 if (*iindp >= 0) 4703 oos += iiadd * osymesz; 4704 iisp += iiadd; 4705 iindp += iiadd; 4706 } 4707 } 4708 } 4709 } 4710 4711 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, isymp->n_type, 4712 isymp->n_sclass, i, isymp->n_numaux, 4713 (void *) outsym); 4714 outsym += osymesz; 4715 esym += isymesz; 4716 } 4717 } 4718 4719 sym_hash += add; 4720 indexp += add; 4721 isymp += add; 4722 csectpp += add; 4723 lineno_counts += add; 4724 debug_index += add; 4725 } 4726 4727 /* If we swapped out a C_FILE symbol, guess that the next C_FILE 4728 symbol will be the first symbol in the next input file. In the 4729 normal case, this will save us from writing out the C_FILE symbol 4730 again. */ 4731 if (flinfo->last_file_index != -1 4732 && (bfd_size_type) flinfo->last_file_index >= syment_base) 4733 { 4734 flinfo->last_file.n_value = output_index; 4735 bfd_coff_swap_sym_out (output_bfd, (void *) &flinfo->last_file, 4736 (void *) (flinfo->outsyms 4737 + ((flinfo->last_file_index - syment_base) 4738 * osymesz))); 4739 } 4740 4741 /* Write the modified symbols to the output file. */ 4742 if (outsym > flinfo->outsyms) 4743 { 4744 file_ptr pos = obj_sym_filepos (output_bfd) + syment_base * osymesz; 4745 bfd_size_type amt = outsym - flinfo->outsyms; 4746 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 4747 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt) 4748 return FALSE; 4749 4750 BFD_ASSERT ((obj_raw_syment_count (output_bfd) 4751 + (outsym - flinfo->outsyms) / osymesz) 4752 == output_index); 4753 4754 obj_raw_syment_count (output_bfd) = output_index; 4755 } 4756 4757 /* Don't let the linker relocation routines discard the symbols. */ 4758 keep_syms = obj_coff_keep_syms (input_bfd); 4759 obj_coff_keep_syms (input_bfd) = TRUE; 4760 4761 /* Relocate the contents of each section. */ 4762 for (o = input_bfd->sections; o != NULL; o = o->next) 4763 { 4764 bfd_byte *contents; 4765 4766 if (! o->linker_mark) 4767 /* This section was omitted from the link. */ 4768 continue; 4769 4770 if ((o->flags & SEC_HAS_CONTENTS) == 0 4771 || o->size == 0 4772 || (o->flags & SEC_IN_MEMORY) != 0) 4773 continue; 4774 4775 /* We have set filepos correctly for the sections we created to 4776 represent csects, so bfd_get_section_contents should work. */ 4777 if (coff_section_data (input_bfd, o) != NULL 4778 && coff_section_data (input_bfd, o)->contents != NULL) 4779 contents = coff_section_data (input_bfd, o)->contents; 4780 else 4781 { 4782 bfd_size_type sz = o->rawsize ? o->rawsize : o->size; 4783 if (!bfd_get_section_contents (input_bfd, o, flinfo->contents, 0, sz)) 4784 return FALSE; 4785 contents = flinfo->contents; 4786 } 4787 4788 if ((o->flags & SEC_RELOC) != 0) 4789 { 4790 int target_index; 4791 struct internal_reloc *internal_relocs; 4792 struct internal_reloc *irel; 4793 bfd_vma offset; 4794 struct internal_reloc *irelend; 4795 struct xcoff_link_hash_entry **rel_hash; 4796 long r_symndx; 4797 4798 /* Read in the relocs. */ 4799 target_index = o->output_section->target_index; 4800 internal_relocs = (xcoff_read_internal_relocs 4801 (input_bfd, o, FALSE, flinfo->external_relocs, 4802 TRUE, 4803 (flinfo->section_info[target_index].relocs 4804 + o->output_section->reloc_count))); 4805 if (internal_relocs == NULL) 4806 return FALSE; 4807 4808 /* Call processor specific code to relocate the section 4809 contents. */ 4810 if (! bfd_coff_relocate_section (output_bfd, flinfo->info, 4811 input_bfd, o, 4812 contents, 4813 internal_relocs, 4814 flinfo->internal_syms, 4815 xcoff_data (input_bfd)->csects)) 4816 return FALSE; 4817 4818 offset = o->output_section->vma + o->output_offset - o->vma; 4819 irel = internal_relocs; 4820 irelend = irel + o->reloc_count; 4821 rel_hash = (flinfo->section_info[target_index].rel_hashes 4822 + o->output_section->reloc_count); 4823 for (; irel < irelend; irel++, rel_hash++) 4824 { 4825 struct xcoff_link_hash_entry *h = NULL; 4826 4827 *rel_hash = NULL; 4828 4829 /* Adjust the reloc address and symbol index. */ 4830 4831 irel->r_vaddr += offset; 4832 4833 r_symndx = irel->r_symndx; 4834 4835 if (r_symndx == -1) 4836 h = NULL; 4837 else 4838 h = obj_xcoff_sym_hashes (input_bfd)[r_symndx]; 4839 4840 if (r_symndx != -1 && flinfo->info->strip != strip_all) 4841 { 4842 if (h != NULL 4843 && h->smclas != XMC_TD 4844 && (irel->r_type == R_TOC 4845 || irel->r_type == R_GL 4846 || irel->r_type == R_TCL 4847 || irel->r_type == R_TRL 4848 || irel->r_type == R_TRLA)) 4849 { 4850 /* This is a TOC relative reloc with a symbol 4851 attached. The symbol should be the one which 4852 this reloc is for. We want to make this 4853 reloc against the TOC address of the symbol, 4854 not the symbol itself. */ 4855 BFD_ASSERT (h->toc_section != NULL); 4856 BFD_ASSERT ((h->flags & XCOFF_SET_TOC) == 0); 4857 if (h->u.toc_indx != -1) 4858 irel->r_symndx = h->u.toc_indx; 4859 else 4860 { 4861 struct xcoff_toc_rel_hash *n; 4862 struct xcoff_link_section_info *si; 4863 bfd_size_type amt; 4864 4865 amt = sizeof (* n); 4866 n = bfd_alloc (flinfo->output_bfd, amt); 4867 if (n == NULL) 4868 return FALSE; 4869 si = flinfo->section_info + target_index; 4870 n->next = si->toc_rel_hashes; 4871 n->h = h; 4872 n->rel = irel; 4873 si->toc_rel_hashes = n; 4874 } 4875 } 4876 else if (h != NULL) 4877 { 4878 /* This is a global symbol. */ 4879 if (h->indx >= 0) 4880 irel->r_symndx = h->indx; 4881 else 4882 { 4883 /* This symbol is being written at the end 4884 of the file, and we do not yet know the 4885 symbol index. We save the pointer to the 4886 hash table entry in the rel_hash list. 4887 We set the indx field to -2 to indicate 4888 that this symbol must not be stripped. */ 4889 *rel_hash = h; 4890 h->indx = -2; 4891 } 4892 } 4893 else 4894 { 4895 long indx; 4896 4897 indx = flinfo->sym_indices[r_symndx]; 4898 4899 if (indx == -1) 4900 { 4901 struct internal_syment *is; 4902 4903 /* Relocations against a TC0 TOC anchor are 4904 automatically transformed to be against 4905 the TOC anchor in the output file. */ 4906 is = flinfo->internal_syms + r_symndx; 4907 if (is->n_sclass == C_HIDEXT 4908 && is->n_numaux > 0) 4909 { 4910 void * auxptr; 4911 union internal_auxent aux; 4912 4913 auxptr = ((void *) 4914 (((bfd_byte *) 4915 obj_coff_external_syms (input_bfd)) 4916 + ((r_symndx + is->n_numaux) 4917 * isymesz))); 4918 bfd_coff_swap_aux_in (input_bfd, auxptr, 4919 is->n_type, is->n_sclass, 4920 is->n_numaux - 1, 4921 is->n_numaux, 4922 (void *) &aux); 4923 if (SMTYP_SMTYP (aux.x_csect.x_smtyp) == XTY_SD 4924 && aux.x_csect.x_smclas == XMC_TC0) 4925 indx = flinfo->toc_symindx; 4926 } 4927 } 4928 4929 if (indx != -1) 4930 irel->r_symndx = indx; 4931 else 4932 { 4933 4934 struct internal_syment *is; 4935 4936 const char *name; 4937 char buf[SYMNMLEN + 1]; 4938 4939 /* This reloc is against a symbol we are 4940 stripping. It would be possible to handle 4941 this case, but I don't think it's worth it. */ 4942 is = flinfo->internal_syms + r_symndx; 4943 4944 if (is->n_sclass != C_DWARF) 4945 { 4946 name = (_bfd_coff_internal_syment_name 4947 (input_bfd, is, buf)); 4948 4949 if (name == NULL) 4950 return FALSE; 4951 4952 (*flinfo->info->callbacks->unattached_reloc) 4953 (flinfo->info, name, 4954 input_bfd, o, irel->r_vaddr); 4955 } 4956 } 4957 } 4958 } 4959 4960 if ((o->flags & SEC_DEBUGGING) == 0 4961 && xcoff_need_ldrel_p (flinfo->info, irel, h)) 4962 { 4963 asection *sec; 4964 4965 if (r_symndx == -1) 4966 sec = NULL; 4967 else if (h == NULL) 4968 sec = xcoff_data (input_bfd)->csects[r_symndx]; 4969 else 4970 sec = xcoff_symbol_section (h); 4971 if (!xcoff_create_ldrel (output_bfd, flinfo, 4972 o->output_section, input_bfd, 4973 irel, sec, h)) 4974 return FALSE; 4975 } 4976 } 4977 4978 o->output_section->reloc_count += o->reloc_count; 4979 } 4980 4981 /* Write out the modified section contents. */ 4982 if (! bfd_set_section_contents (output_bfd, o->output_section, 4983 contents, (file_ptr) o->output_offset, 4984 o->size)) 4985 return FALSE; 4986 } 4987 4988 obj_coff_keep_syms (input_bfd) = keep_syms; 4989 4990 if (! flinfo->info->keep_memory) 4991 { 4992 if (! _bfd_coff_free_symbols (input_bfd)) 4993 return FALSE; 4994 } 4995 4996 return TRUE; 4997 } 4998 4999 #undef N_TMASK 5000 #undef N_BTSHFT 5001 5002 /* Sort relocs by VMA. This is called via qsort. */ 5003 5004 static int 5005 xcoff_sort_relocs (const void * p1, const void * p2) 5006 { 5007 const struct internal_reloc *r1 = (const struct internal_reloc *) p1; 5008 const struct internal_reloc *r2 = (const struct internal_reloc *) p2; 5009 5010 if (r1->r_vaddr > r2->r_vaddr) 5011 return 1; 5012 else if (r1->r_vaddr < r2->r_vaddr) 5013 return -1; 5014 else 5015 return 0; 5016 } 5017 5018 /* Return true if section SEC is a TOC section. */ 5019 5020 static inline bfd_boolean 5021 xcoff_toc_section_p (asection *sec) 5022 { 5023 const char *name; 5024 5025 name = sec->name; 5026 if (name[0] == '.' && name[1] == 't') 5027 { 5028 if (name[2] == 'c') 5029 { 5030 if (name[3] == '0' && name[4] == 0) 5031 return TRUE; 5032 if (name[3] == 0) 5033 return TRUE; 5034 } 5035 if (name[2] == 'd' && name[3] == 0) 5036 return TRUE; 5037 } 5038 return FALSE; 5039 } 5040 5041 /* See if the link requires a TOC (it usually does!). If so, find a 5042 good place to put the TOC anchor csect, and write out the associated 5043 symbol. */ 5044 5045 static bfd_boolean 5046 xcoff_find_tc0 (bfd *output_bfd, struct xcoff_final_link_info *flinfo) 5047 { 5048 bfd_vma toc_start, toc_end, start, end, best_address; 5049 asection *sec; 5050 bfd *input_bfd; 5051 int section_index; 5052 struct internal_syment irsym; 5053 union internal_auxent iraux; 5054 file_ptr pos; 5055 size_t size; 5056 5057 /* Set [TOC_START, TOC_END) to the range of the TOC. Record the 5058 index of a csect at the beginning of the TOC. */ 5059 toc_start = ~(bfd_vma) 0; 5060 toc_end = 0; 5061 section_index = -1; 5062 for (input_bfd = flinfo->info->input_bfds; 5063 input_bfd != NULL; 5064 input_bfd = input_bfd->link.next) 5065 for (sec = input_bfd->sections; sec != NULL; sec = sec->next) 5066 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec)) 5067 { 5068 start = sec->output_section->vma + sec->output_offset; 5069 if (toc_start > start) 5070 { 5071 toc_start = start; 5072 section_index = sec->output_section->target_index; 5073 } 5074 5075 end = start + sec->size; 5076 if (toc_end < end) 5077 toc_end = end; 5078 } 5079 5080 /* There's no need for a TC0 symbol if we don't have a TOC. */ 5081 if (toc_end < toc_start) 5082 { 5083 xcoff_data (output_bfd)->toc = toc_start; 5084 return TRUE; 5085 } 5086 5087 if (toc_end - toc_start < 0x8000) 5088 /* Every TOC csect can be accessed from TOC_START. */ 5089 best_address = toc_start; 5090 else 5091 { 5092 /* Find the lowest TOC csect that is still within range of TOC_END. */ 5093 best_address = toc_end; 5094 for (input_bfd = flinfo->info->input_bfds; 5095 input_bfd != NULL; 5096 input_bfd = input_bfd->link.next) 5097 for (sec = input_bfd->sections; sec != NULL; sec = sec->next) 5098 if ((sec->flags & SEC_MARK) != 0 && xcoff_toc_section_p (sec)) 5099 { 5100 start = sec->output_section->vma + sec->output_offset; 5101 if (start < best_address 5102 && start + 0x8000 >= toc_end) 5103 { 5104 best_address = start; 5105 section_index = sec->output_section->target_index; 5106 } 5107 } 5108 5109 /* Make sure that the start of the TOC is also within range. */ 5110 if (best_address > toc_start + 0x8000) 5111 { 5112 _bfd_error_handler 5113 (_("TOC overflow: %#" PRIx64 " > 0x10000; try -mminimal-toc " 5114 "when compiling"), 5115 (uint64_t) (toc_end - toc_start)); 5116 bfd_set_error (bfd_error_file_too_big); 5117 return FALSE; 5118 } 5119 } 5120 5121 /* Record the chosen TOC value. */ 5122 flinfo->toc_symindx = obj_raw_syment_count (output_bfd); 5123 xcoff_data (output_bfd)->toc = best_address; 5124 xcoff_data (output_bfd)->sntoc = section_index; 5125 5126 /* Fill out the TC0 symbol. */ 5127 if (!bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab, 5128 &irsym, "TOC")) 5129 return FALSE; 5130 irsym.n_value = best_address; 5131 irsym.n_scnum = section_index; 5132 irsym.n_sclass = C_HIDEXT; 5133 irsym.n_type = T_NULL; 5134 irsym.n_numaux = 1; 5135 bfd_coff_swap_sym_out (output_bfd, &irsym, flinfo->outsyms); 5136 5137 /* Fill out the auxillary csect information. */ 5138 memset (&iraux, 0, sizeof iraux); 5139 iraux.x_csect.x_smtyp = XTY_SD; 5140 iraux.x_csect.x_smclas = XMC_TC0; 5141 iraux.x_csect.x_scnlen.l = 0; 5142 bfd_coff_swap_aux_out (output_bfd, &iraux, T_NULL, C_HIDEXT, 0, 1, 5143 flinfo->outsyms + bfd_coff_symesz (output_bfd)); 5144 5145 /* Write the contents to the file. */ 5146 pos = obj_sym_filepos (output_bfd); 5147 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd); 5148 size = 2 * bfd_coff_symesz (output_bfd); 5149 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 5150 || bfd_bwrite (flinfo->outsyms, size, output_bfd) != size) 5151 return FALSE; 5152 obj_raw_syment_count (output_bfd) += 2; 5153 5154 return TRUE; 5155 } 5156 5157 /* Write out a non-XCOFF global symbol. */ 5158 5159 static bfd_boolean 5160 xcoff_write_global_symbol (struct bfd_hash_entry *bh, void * inf) 5161 { 5162 struct xcoff_link_hash_entry *h = (struct xcoff_link_hash_entry *) bh; 5163 struct xcoff_final_link_info *flinfo = (struct xcoff_final_link_info *) inf; 5164 bfd *output_bfd; 5165 bfd_byte *outsym; 5166 struct internal_syment isym; 5167 union internal_auxent aux; 5168 bfd_boolean result; 5169 file_ptr pos; 5170 bfd_size_type amt; 5171 5172 output_bfd = flinfo->output_bfd; 5173 outsym = flinfo->outsyms; 5174 5175 if (h->root.type == bfd_link_hash_warning) 5176 { 5177 h = (struct xcoff_link_hash_entry *) h->root.u.i.link; 5178 if (h->root.type == bfd_link_hash_new) 5179 return TRUE; 5180 } 5181 5182 /* If this symbol was garbage collected, just skip it. */ 5183 if (xcoff_hash_table (flinfo->info)->gc 5184 && (h->flags & XCOFF_MARK) == 0) 5185 return TRUE; 5186 5187 /* If we need a .loader section entry, write it out. */ 5188 if (h->ldsym != NULL) 5189 { 5190 struct internal_ldsym *ldsym; 5191 bfd *impbfd; 5192 5193 ldsym = h->ldsym; 5194 5195 if (h->root.type == bfd_link_hash_undefined 5196 || h->root.type == bfd_link_hash_undefweak) 5197 { 5198 5199 ldsym->l_value = 0; 5200 ldsym->l_scnum = N_UNDEF; 5201 ldsym->l_smtype = XTY_ER; 5202 impbfd = h->root.u.undef.abfd; 5203 5204 } 5205 else if (h->root.type == bfd_link_hash_defined 5206 || h->root.type == bfd_link_hash_defweak) 5207 { 5208 asection *sec; 5209 5210 sec = h->root.u.def.section; 5211 ldsym->l_value = (sec->output_section->vma 5212 + sec->output_offset 5213 + h->root.u.def.value); 5214 ldsym->l_scnum = sec->output_section->target_index; 5215 ldsym->l_smtype = XTY_SD; 5216 impbfd = sec->owner; 5217 5218 } 5219 else 5220 abort (); 5221 5222 if (((h->flags & XCOFF_DEF_REGULAR) == 0 5223 && (h->flags & XCOFF_DEF_DYNAMIC) != 0) 5224 || (h->flags & XCOFF_IMPORT) != 0) 5225 /* Clear l_smtype 5226 Import symbols are defined so the check above will make 5227 the l_smtype XTY_SD. But this is not correct, it should 5228 be cleared. */ 5229 ldsym->l_smtype |= L_IMPORT; 5230 5231 if (((h->flags & XCOFF_DEF_REGULAR) != 0 5232 && (h->flags & XCOFF_DEF_DYNAMIC) != 0) 5233 || (h->flags & XCOFF_EXPORT) != 0) 5234 ldsym->l_smtype |= L_EXPORT; 5235 5236 if ((h->flags & XCOFF_ENTRY) != 0) 5237 ldsym->l_smtype |= L_ENTRY; 5238 5239 if ((h->flags & XCOFF_RTINIT) != 0) 5240 ldsym->l_smtype = XTY_SD; 5241 5242 ldsym->l_smclas = h->smclas; 5243 5244 if (ldsym->l_smtype & L_IMPORT) 5245 { 5246 if ((h->root.type == bfd_link_hash_defined 5247 || h->root.type == bfd_link_hash_defweak) 5248 && (h->root.u.def.value != 0)) 5249 ldsym->l_smclas = XMC_XO; 5250 5251 else if ((h->flags & (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) == 5252 (XCOFF_SYSCALL32 | XCOFF_SYSCALL64)) 5253 ldsym->l_smclas = XMC_SV3264; 5254 5255 else if (h->flags & XCOFF_SYSCALL32) 5256 ldsym->l_smclas = XMC_SV; 5257 5258 else if (h->flags & XCOFF_SYSCALL64) 5259 ldsym->l_smclas = XMC_SV64; 5260 } 5261 5262 if (ldsym->l_ifile == -(bfd_size_type) 1) 5263 { 5264 ldsym->l_ifile = 0; 5265 } 5266 else if (ldsym->l_ifile == 0) 5267 { 5268 if ((ldsym->l_smtype & L_IMPORT) == 0) 5269 ldsym->l_ifile = 0; 5270 else if (impbfd == NULL) 5271 ldsym->l_ifile = 0; 5272 else 5273 { 5274 BFD_ASSERT (impbfd->xvec == output_bfd->xvec); 5275 ldsym->l_ifile = xcoff_data (impbfd)->import_file_id; 5276 } 5277 } 5278 5279 ldsym->l_parm = 0; 5280 5281 BFD_ASSERT (h->ldindx >= 0); 5282 5283 bfd_xcoff_swap_ldsym_out (output_bfd, ldsym, 5284 (flinfo->ldsym + 5285 (h->ldindx - 3) 5286 * bfd_xcoff_ldsymsz(flinfo->output_bfd))); 5287 h->ldsym = NULL; 5288 } 5289 5290 /* If this symbol needs global linkage code, write it out. */ 5291 if (h->root.type == bfd_link_hash_defined 5292 && (h->root.u.def.section 5293 == xcoff_hash_table (flinfo->info)->linkage_section)) 5294 { 5295 bfd_byte *p; 5296 bfd_vma tocoff; 5297 unsigned int i; 5298 5299 p = h->root.u.def.section->contents + h->root.u.def.value; 5300 5301 /* The first instruction in the global linkage code loads a 5302 specific TOC element. */ 5303 tocoff = (h->descriptor->toc_section->output_section->vma 5304 + h->descriptor->toc_section->output_offset 5305 - xcoff_data (output_bfd)->toc); 5306 5307 if ((h->descriptor->flags & XCOFF_SET_TOC) != 0) 5308 tocoff += h->descriptor->u.toc_offset; 5309 5310 /* The first instruction in the glink code needs to be 5311 cooked to hold the correct offset in the toc. The 5312 rest are just output raw. */ 5313 bfd_put_32 (output_bfd, 5314 bfd_xcoff_glink_code(output_bfd, 0) | (tocoff & 0xffff), p); 5315 5316 /* Start with i == 1 to get past the first instruction done above 5317 The /4 is because the glink code is in bytes and we are going 5318 4 at a pop. */ 5319 for (i = 1; i < bfd_xcoff_glink_code_size(output_bfd) / 4; i++) 5320 bfd_put_32 (output_bfd, 5321 (bfd_vma) bfd_xcoff_glink_code(output_bfd, i), 5322 &p[4 * i]); 5323 } 5324 5325 /* If we created a TOC entry for this symbol, write out the required 5326 relocs. */ 5327 if ((h->flags & XCOFF_SET_TOC) != 0) 5328 { 5329 asection *tocsec; 5330 asection *osec; 5331 int oindx; 5332 struct internal_reloc *irel; 5333 struct internal_syment irsym; 5334 union internal_auxent iraux; 5335 5336 tocsec = h->toc_section; 5337 osec = tocsec->output_section; 5338 oindx = osec->target_index; 5339 irel = flinfo->section_info[oindx].relocs + osec->reloc_count; 5340 irel->r_vaddr = (osec->vma 5341 + tocsec->output_offset 5342 + h->u.toc_offset); 5343 5344 if (h->indx >= 0) 5345 irel->r_symndx = h->indx; 5346 else 5347 { 5348 h->indx = -2; 5349 irel->r_symndx = obj_raw_syment_count (output_bfd); 5350 } 5351 5352 BFD_ASSERT (h->ldindx >= 0); 5353 5354 /* Initialize the aux union here instead of closer to when it is 5355 written out below because the length of the csect depends on 5356 whether the output is 32 or 64 bit. */ 5357 memset (&iraux, 0, sizeof iraux); 5358 iraux.x_csect.x_smtyp = XTY_SD; 5359 /* iraux.x_csect.x_scnlen.l = 4 or 8, see below. */ 5360 iraux.x_csect.x_smclas = XMC_TC; 5361 5362 /* 32 bit uses a 32 bit R_POS to do the relocations 5363 64 bit uses a 64 bit R_POS to do the relocations 5364 5365 Also needs to change the csect size : 4 for 32 bit, 8 for 64 bit 5366 5367 Which one is determined by the backend. */ 5368 if (bfd_xcoff_is_xcoff64 (output_bfd)) 5369 { 5370 irel->r_size = 63; 5371 iraux.x_csect.x_scnlen.l = 8; 5372 } 5373 else if (bfd_xcoff_is_xcoff32 (output_bfd)) 5374 { 5375 irel->r_size = 31; 5376 iraux.x_csect.x_scnlen.l = 4; 5377 } 5378 else 5379 return FALSE; 5380 5381 irel->r_type = R_POS; 5382 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; 5383 ++osec->reloc_count; 5384 5385 if (!xcoff_create_ldrel (output_bfd, flinfo, osec, 5386 output_bfd, irel, NULL, h)) 5387 return FALSE; 5388 5389 /* We need to emit a symbol to define a csect which holds 5390 the reloc. */ 5391 if (flinfo->info->strip != strip_all) 5392 { 5393 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, 5394 flinfo->strtab, 5395 &irsym, h->root.root.string); 5396 if (!result) 5397 return FALSE; 5398 5399 irsym.n_value = irel->r_vaddr; 5400 irsym.n_scnum = osec->target_index; 5401 irsym.n_sclass = C_HIDEXT; 5402 irsym.n_type = T_NULL; 5403 irsym.n_numaux = 1; 5404 5405 bfd_coff_swap_sym_out (output_bfd, (void *) &irsym, (void *) outsym); 5406 outsym += bfd_coff_symesz (output_bfd); 5407 5408 /* Note : iraux is initialized above. */ 5409 bfd_coff_swap_aux_out (output_bfd, (void *) &iraux, T_NULL, C_HIDEXT, 5410 0, 1, (void *) outsym); 5411 outsym += bfd_coff_auxesz (output_bfd); 5412 5413 if (h->indx >= 0) 5414 { 5415 /* We aren't going to write out the symbols below, so we 5416 need to write them out now. */ 5417 pos = obj_sym_filepos (output_bfd); 5418 pos += (obj_raw_syment_count (output_bfd) 5419 * bfd_coff_symesz (output_bfd)); 5420 amt = outsym - flinfo->outsyms; 5421 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 5422 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt) 5423 return FALSE; 5424 obj_raw_syment_count (output_bfd) += 5425 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd); 5426 5427 outsym = flinfo->outsyms; 5428 } 5429 } 5430 } 5431 5432 /* If this symbol is a specially defined function descriptor, write 5433 it out. The first word is the address of the function code 5434 itself, the second word is the address of the TOC, and the third 5435 word is zero. 5436 5437 32 bit vs 64 bit 5438 The addresses for the 32 bit will take 4 bytes and the addresses 5439 for 64 bit will take 8 bytes. Similar for the relocs. This type 5440 of logic was also done above to create a TOC entry in 5441 xcoff_write_global_symbol. */ 5442 if ((h->flags & XCOFF_DESCRIPTOR) != 0 5443 && h->root.type == bfd_link_hash_defined 5444 && (h->root.u.def.section 5445 == xcoff_hash_table (flinfo->info)->descriptor_section)) 5446 { 5447 asection *sec; 5448 asection *osec; 5449 int oindx; 5450 bfd_byte *p; 5451 struct xcoff_link_hash_entry *hentry; 5452 asection *esec; 5453 struct internal_reloc *irel; 5454 asection *tsec; 5455 unsigned int reloc_size, byte_size; 5456 5457 if (bfd_xcoff_is_xcoff64 (output_bfd)) 5458 { 5459 reloc_size = 63; 5460 byte_size = 8; 5461 } 5462 else if (bfd_xcoff_is_xcoff32 (output_bfd)) 5463 { 5464 reloc_size = 31; 5465 byte_size = 4; 5466 } 5467 else 5468 return FALSE; 5469 5470 sec = h->root.u.def.section; 5471 osec = sec->output_section; 5472 oindx = osec->target_index; 5473 p = sec->contents + h->root.u.def.value; 5474 5475 hentry = h->descriptor; 5476 BFD_ASSERT (hentry != NULL 5477 && (hentry->root.type == bfd_link_hash_defined 5478 || hentry->root.type == bfd_link_hash_defweak)); 5479 esec = hentry->root.u.def.section; 5480 5481 irel = flinfo->section_info[oindx].relocs + osec->reloc_count; 5482 irel->r_vaddr = (osec->vma 5483 + sec->output_offset 5484 + h->root.u.def.value); 5485 irel->r_symndx = esec->output_section->target_index; 5486 irel->r_type = R_POS; 5487 irel->r_size = reloc_size; 5488 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; 5489 ++osec->reloc_count; 5490 5491 if (!xcoff_create_ldrel (output_bfd, flinfo, osec, 5492 output_bfd, irel, esec, NULL)) 5493 return FALSE; 5494 5495 /* There are three items to write out, 5496 the address of the code 5497 the address of the toc anchor 5498 the environment pointer. 5499 We are ignoring the environment pointer. So set it to zero. */ 5500 if (bfd_xcoff_is_xcoff64 (output_bfd)) 5501 { 5502 bfd_put_64 (output_bfd, 5503 (esec->output_section->vma + esec->output_offset 5504 + hentry->root.u.def.value), 5505 p); 5506 bfd_put_64 (output_bfd, xcoff_data (output_bfd)->toc, p + 8); 5507 bfd_put_64 (output_bfd, (bfd_vma) 0, p + 16); 5508 } 5509 else 5510 { 5511 /* 32 bit backend 5512 This logic was already called above so the error case where 5513 the backend is neither has already been checked. */ 5514 bfd_put_32 (output_bfd, 5515 (esec->output_section->vma + esec->output_offset 5516 + hentry->root.u.def.value), 5517 p); 5518 bfd_put_32 (output_bfd, xcoff_data (output_bfd)->toc, p + 4); 5519 bfd_put_32 (output_bfd, (bfd_vma) 0, p + 8); 5520 } 5521 5522 tsec = coff_section_from_bfd_index (output_bfd, 5523 xcoff_data (output_bfd)->sntoc); 5524 5525 ++irel; 5526 irel->r_vaddr = (osec->vma 5527 + sec->output_offset 5528 + h->root.u.def.value 5529 + byte_size); 5530 irel->r_symndx = tsec->output_section->target_index; 5531 irel->r_type = R_POS; 5532 irel->r_size = reloc_size; 5533 flinfo->section_info[oindx].rel_hashes[osec->reloc_count] = NULL; 5534 ++osec->reloc_count; 5535 5536 if (!xcoff_create_ldrel (output_bfd, flinfo, osec, 5537 output_bfd, irel, tsec, NULL)) 5538 return FALSE; 5539 } 5540 5541 if (h->indx >= 0 || flinfo->info->strip == strip_all) 5542 { 5543 BFD_ASSERT (outsym == flinfo->outsyms); 5544 return TRUE; 5545 } 5546 5547 if (h->indx != -2 5548 && (flinfo->info->strip == strip_all 5549 || (flinfo->info->strip == strip_some 5550 && bfd_hash_lookup (flinfo->info->keep_hash, h->root.root.string, 5551 FALSE, FALSE) == NULL))) 5552 { 5553 BFD_ASSERT (outsym == flinfo->outsyms); 5554 return TRUE; 5555 } 5556 5557 if (h->indx != -2 5558 && (h->flags & (XCOFF_REF_REGULAR | XCOFF_DEF_REGULAR)) == 0) 5559 { 5560 BFD_ASSERT (outsym == flinfo->outsyms); 5561 return TRUE; 5562 } 5563 5564 memset (&aux, 0, sizeof aux); 5565 5566 h->indx = obj_raw_syment_count (output_bfd); 5567 5568 result = bfd_xcoff_put_symbol_name (output_bfd, flinfo->info, flinfo->strtab, 5569 &isym, h->root.root.string); 5570 if (!result) 5571 return FALSE; 5572 5573 if (h->root.type == bfd_link_hash_undefined 5574 || h->root.type == bfd_link_hash_undefweak) 5575 { 5576 isym.n_value = 0; 5577 isym.n_scnum = N_UNDEF; 5578 if (h->root.type == bfd_link_hash_undefweak 5579 && C_WEAKEXT == C_AIX_WEAKEXT) 5580 isym.n_sclass = C_WEAKEXT; 5581 else 5582 isym.n_sclass = C_EXT; 5583 aux.x_csect.x_smtyp = XTY_ER; 5584 } 5585 else if ((h->root.type == bfd_link_hash_defined 5586 || h->root.type == bfd_link_hash_defweak) 5587 && h->smclas == XMC_XO) 5588 { 5589 BFD_ASSERT (bfd_is_abs_symbol (&h->root)); 5590 isym.n_value = h->root.u.def.value; 5591 isym.n_scnum = N_UNDEF; 5592 if (h->root.type == bfd_link_hash_defweak 5593 && C_WEAKEXT == C_AIX_WEAKEXT) 5594 isym.n_sclass = C_WEAKEXT; 5595 else 5596 isym.n_sclass = C_EXT; 5597 aux.x_csect.x_smtyp = XTY_ER; 5598 } 5599 else if (h->root.type == bfd_link_hash_defined 5600 || h->root.type == bfd_link_hash_defweak) 5601 { 5602 struct xcoff_link_size_list *l; 5603 5604 isym.n_value = (h->root.u.def.section->output_section->vma 5605 + h->root.u.def.section->output_offset 5606 + h->root.u.def.value); 5607 if (bfd_is_abs_section (h->root.u.def.section->output_section)) 5608 isym.n_scnum = N_ABS; 5609 else 5610 isym.n_scnum = h->root.u.def.section->output_section->target_index; 5611 isym.n_sclass = C_HIDEXT; 5612 aux.x_csect.x_smtyp = XTY_SD; 5613 5614 if ((h->flags & XCOFF_HAS_SIZE) != 0) 5615 { 5616 for (l = xcoff_hash_table (flinfo->info)->size_list; 5617 l != NULL; 5618 l = l->next) 5619 { 5620 if (l->h == h) 5621 { 5622 aux.x_csect.x_scnlen.l = l->size; 5623 break; 5624 } 5625 } 5626 } 5627 } 5628 else if (h->root.type == bfd_link_hash_common) 5629 { 5630 isym.n_value = (h->root.u.c.p->section->output_section->vma 5631 + h->root.u.c.p->section->output_offset); 5632 isym.n_scnum = h->root.u.c.p->section->output_section->target_index; 5633 isym.n_sclass = C_EXT; 5634 aux.x_csect.x_smtyp = XTY_CM; 5635 aux.x_csect.x_scnlen.l = h->root.u.c.size; 5636 } 5637 else 5638 abort (); 5639 5640 isym.n_type = T_NULL; 5641 isym.n_numaux = 1; 5642 5643 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym); 5644 outsym += bfd_coff_symesz (output_bfd); 5645 5646 aux.x_csect.x_smclas = h->smclas; 5647 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, isym.n_sclass, 0, 1, 5648 (void *) outsym); 5649 outsym += bfd_coff_auxesz (output_bfd); 5650 5651 if ((h->root.type == bfd_link_hash_defined 5652 || h->root.type == bfd_link_hash_defweak) 5653 && h->smclas != XMC_XO) 5654 { 5655 /* We just output an SD symbol. Now output an LD symbol. */ 5656 h->indx += 2; 5657 5658 if (h->root.type == bfd_link_hash_defweak 5659 && C_WEAKEXT == C_AIX_WEAKEXT) 5660 isym.n_sclass = C_WEAKEXT; 5661 else 5662 isym.n_sclass = C_EXT; 5663 bfd_coff_swap_sym_out (output_bfd, (void *) &isym, (void *) outsym); 5664 outsym += bfd_coff_symesz (output_bfd); 5665 5666 aux.x_csect.x_smtyp = XTY_LD; 5667 aux.x_csect.x_scnlen.l = obj_raw_syment_count (output_bfd); 5668 bfd_coff_swap_aux_out (output_bfd, (void *) &aux, T_NULL, C_EXT, 0, 1, 5669 (void *) outsym); 5670 outsym += bfd_coff_auxesz (output_bfd); 5671 } 5672 5673 pos = obj_sym_filepos (output_bfd); 5674 pos += obj_raw_syment_count (output_bfd) * bfd_coff_symesz (output_bfd); 5675 amt = outsym - flinfo->outsyms; 5676 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0 5677 || bfd_bwrite (flinfo->outsyms, amt, output_bfd) != amt) 5678 return FALSE; 5679 obj_raw_syment_count (output_bfd) += 5680 (outsym - flinfo->outsyms) / bfd_coff_symesz (output_bfd); 5681 5682 return TRUE; 5683 } 5684 5685 /* Handle a link order which is supposed to generate a reloc. */ 5686 5687 static bfd_boolean 5688 xcoff_reloc_link_order (bfd *output_bfd, 5689 struct xcoff_final_link_info *flinfo, 5690 asection *output_section, 5691 struct bfd_link_order *link_order) 5692 { 5693 reloc_howto_type *howto; 5694 struct xcoff_link_hash_entry *h; 5695 asection *hsec; 5696 bfd_vma hval; 5697 bfd_vma addend; 5698 struct internal_reloc *irel; 5699 struct xcoff_link_hash_entry **rel_hash_ptr; 5700 5701 if (link_order->type == bfd_section_reloc_link_order) 5702 /* We need to somehow locate a symbol in the right section. The 5703 symbol must either have a value of zero, or we must adjust 5704 the addend by the value of the symbol. FIXME: Write this 5705 when we need it. The old linker couldn't handle this anyhow. */ 5706 abort (); 5707 5708 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 5709 if (howto == NULL) 5710 { 5711 bfd_set_error (bfd_error_bad_value); 5712 return FALSE; 5713 } 5714 5715 h = ((struct xcoff_link_hash_entry *) 5716 bfd_wrapped_link_hash_lookup (output_bfd, flinfo->info, 5717 link_order->u.reloc.p->u.name, 5718 FALSE, FALSE, TRUE)); 5719 if (h == NULL) 5720 { 5721 (*flinfo->info->callbacks->unattached_reloc) 5722 (flinfo->info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0); 5723 return TRUE; 5724 } 5725 5726 hsec = xcoff_symbol_section (h); 5727 if (h->root.type == bfd_link_hash_defined 5728 || h->root.type == bfd_link_hash_defweak) 5729 hval = h->root.u.def.value; 5730 else 5731 hval = 0; 5732 5733 addend = link_order->u.reloc.p->addend; 5734 if (hsec != NULL) 5735 addend += (hsec->output_section->vma 5736 + hsec->output_offset 5737 + hval); 5738 5739 if (addend != 0) 5740 { 5741 bfd_size_type size; 5742 bfd_byte *buf; 5743 bfd_reloc_status_type rstat; 5744 bfd_boolean ok; 5745 5746 size = bfd_get_reloc_size (howto); 5747 buf = bfd_zmalloc (size); 5748 if (buf == NULL && size != 0) 5749 return FALSE; 5750 5751 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 5752 switch (rstat) 5753 { 5754 case bfd_reloc_ok: 5755 break; 5756 default: 5757 case bfd_reloc_outofrange: 5758 abort (); 5759 case bfd_reloc_overflow: 5760 (*flinfo->info->callbacks->reloc_overflow) 5761 (flinfo->info, NULL, link_order->u.reloc.p->u.name, 5762 howto->name, addend, NULL, NULL, (bfd_vma) 0); 5763 break; 5764 } 5765 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf, 5766 (file_ptr) link_order->offset, size); 5767 free (buf); 5768 if (! ok) 5769 return FALSE; 5770 } 5771 5772 /* Store the reloc information in the right place. It will get 5773 swapped and written out at the end of the final_link routine. */ 5774 irel = (flinfo->section_info[output_section->target_index].relocs 5775 + output_section->reloc_count); 5776 rel_hash_ptr = (flinfo->section_info[output_section->target_index].rel_hashes 5777 + output_section->reloc_count); 5778 5779 memset (irel, 0, sizeof (struct internal_reloc)); 5780 *rel_hash_ptr = NULL; 5781 5782 irel->r_vaddr = output_section->vma + link_order->offset; 5783 5784 if (h->indx >= 0) 5785 irel->r_symndx = h->indx; 5786 else 5787 { 5788 /* Set the index to -2 to force this symbol to get written out. */ 5789 h->indx = -2; 5790 *rel_hash_ptr = h; 5791 irel->r_symndx = 0; 5792 } 5793 5794 irel->r_type = howto->type; 5795 irel->r_size = howto->bitsize - 1; 5796 if (howto->complain_on_overflow == complain_overflow_signed) 5797 irel->r_size |= 0x80; 5798 5799 ++output_section->reloc_count; 5800 5801 /* Now output the reloc to the .loader section. */ 5802 if (xcoff_hash_table (flinfo->info)->loader_section) 5803 { 5804 if (!xcoff_create_ldrel (output_bfd, flinfo, output_section, 5805 output_bfd, irel, hsec, h)) 5806 return FALSE; 5807 } 5808 5809 return TRUE; 5810 } 5811 5812 /* Do the final link step. */ 5813 5814 bfd_boolean 5815 _bfd_xcoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info) 5816 { 5817 bfd_size_type symesz; 5818 struct xcoff_final_link_info flinfo; 5819 asection *o; 5820 struct bfd_link_order *p; 5821 bfd_size_type max_contents_size; 5822 bfd_size_type max_sym_count; 5823 bfd_size_type max_lineno_count; 5824 bfd_size_type max_reloc_count; 5825 bfd_size_type max_output_reloc_count; 5826 file_ptr rel_filepos; 5827 unsigned int relsz; 5828 file_ptr line_filepos; 5829 unsigned int linesz; 5830 bfd *sub; 5831 bfd_byte *external_relocs = NULL; 5832 char strbuf[STRING_SIZE_SIZE]; 5833 file_ptr pos; 5834 bfd_size_type amt; 5835 5836 if (bfd_link_pic (info)) 5837 abfd->flags |= DYNAMIC; 5838 5839 symesz = bfd_coff_symesz (abfd); 5840 5841 flinfo.info = info; 5842 flinfo.output_bfd = abfd; 5843 flinfo.strtab = NULL; 5844 flinfo.section_info = NULL; 5845 flinfo.last_file_index = -1; 5846 flinfo.toc_symindx = -1; 5847 flinfo.internal_syms = NULL; 5848 flinfo.sym_indices = NULL; 5849 flinfo.outsyms = NULL; 5850 flinfo.linenos = NULL; 5851 flinfo.contents = NULL; 5852 flinfo.external_relocs = NULL; 5853 5854 if (xcoff_hash_table (info)->loader_section) 5855 { 5856 flinfo.ldsym = (xcoff_hash_table (info)->loader_section->contents 5857 + bfd_xcoff_ldhdrsz (abfd)); 5858 flinfo.ldrel = (xcoff_hash_table (info)->loader_section->contents 5859 + bfd_xcoff_ldhdrsz (abfd) 5860 + (xcoff_hash_table (info)->ldhdr.l_nsyms 5861 * bfd_xcoff_ldsymsz (abfd))); 5862 } 5863 else 5864 { 5865 flinfo.ldsym = NULL; 5866 flinfo.ldrel = NULL; 5867 } 5868 5869 xcoff_data (abfd)->coff.link_info = info; 5870 5871 flinfo.strtab = _bfd_stringtab_init (); 5872 if (flinfo.strtab == NULL) 5873 goto error_return; 5874 5875 /* Count the relocation entries required for the output file. 5876 (We've already counted the line numbers.) Determine a few 5877 maximum sizes. */ 5878 max_contents_size = 0; 5879 max_lineno_count = 0; 5880 max_reloc_count = 0; 5881 for (o = abfd->sections; o != NULL; o = o->next) 5882 { 5883 o->reloc_count = 0; 5884 for (p = o->map_head.link_order; p != NULL; p = p->next) 5885 { 5886 if (p->type == bfd_indirect_link_order) 5887 { 5888 asection *sec; 5889 5890 sec = p->u.indirect.section; 5891 5892 /* Mark all sections which are to be included in the 5893 link. This will normally be every section. We need 5894 to do this so that we can identify any sections which 5895 the linker has decided to not include. */ 5896 sec->linker_mark = TRUE; 5897 5898 o->reloc_count += sec->reloc_count; 5899 5900 if ((sec->flags & SEC_IN_MEMORY) == 0) 5901 { 5902 if (sec->rawsize > max_contents_size) 5903 max_contents_size = sec->rawsize; 5904 if (sec->size > max_contents_size) 5905 max_contents_size = sec->size; 5906 } 5907 if (coff_section_data (sec->owner, sec) != NULL 5908 && xcoff_section_data (sec->owner, sec) != NULL 5909 && (xcoff_section_data (sec->owner, sec)->lineno_count 5910 > max_lineno_count)) 5911 max_lineno_count = 5912 xcoff_section_data (sec->owner, sec)->lineno_count; 5913 if (sec->reloc_count > max_reloc_count) 5914 max_reloc_count = sec->reloc_count; 5915 } 5916 else if (p->type == bfd_section_reloc_link_order 5917 || p->type == bfd_symbol_reloc_link_order) 5918 ++o->reloc_count; 5919 } 5920 } 5921 5922 /* Compute the file positions for all the sections. */ 5923 if (abfd->output_has_begun) 5924 { 5925 if (xcoff_hash_table (info)->file_align != 0) 5926 abort (); 5927 } 5928 else 5929 { 5930 bfd_vma file_align; 5931 5932 file_align = xcoff_hash_table (info)->file_align; 5933 if (file_align != 0) 5934 { 5935 bfd_boolean saw_contents; 5936 int indx; 5937 file_ptr sofar; 5938 5939 /* Insert .pad sections before every section which has 5940 contents and is loaded, if it is preceded by some other 5941 section which has contents and is loaded. */ 5942 saw_contents = TRUE; 5943 for (o = abfd->sections; o != NULL; o = o->next) 5944 { 5945 if (strcmp (o->name, ".pad") == 0) 5946 saw_contents = FALSE; 5947 else if ((o->flags & SEC_HAS_CONTENTS) != 0 5948 && (o->flags & SEC_LOAD) != 0) 5949 { 5950 if (! saw_contents) 5951 saw_contents = TRUE; 5952 else 5953 { 5954 asection *n; 5955 5956 /* Create a pad section and place it before the section 5957 that needs padding. This requires unlinking and 5958 relinking the bfd's section list. */ 5959 5960 n = bfd_make_section_anyway_with_flags (abfd, ".pad", 5961 SEC_HAS_CONTENTS); 5962 n->alignment_power = 0; 5963 5964 bfd_section_list_remove (abfd, n); 5965 bfd_section_list_insert_before (abfd, o, n); 5966 saw_contents = FALSE; 5967 } 5968 } 5969 } 5970 5971 /* Reset the section indices after inserting the new 5972 sections. */ 5973 indx = 0; 5974 for (o = abfd->sections; o != NULL; o = o->next) 5975 { 5976 ++indx; 5977 o->target_index = indx; 5978 } 5979 BFD_ASSERT ((unsigned int) indx == abfd->section_count); 5980 5981 /* Work out appropriate sizes for the .pad sections to force 5982 each section to land on a page boundary. This bit of 5983 code knows what compute_section_file_positions is going 5984 to do. */ 5985 sofar = bfd_coff_filhsz (abfd); 5986 sofar += bfd_coff_aoutsz (abfd); 5987 sofar += abfd->section_count * bfd_coff_scnhsz (abfd); 5988 for (o = abfd->sections; o != NULL; o = o->next) 5989 if ((bfd_xcoff_is_reloc_count_overflow 5990 (abfd, (bfd_vma) o->reloc_count)) 5991 || (bfd_xcoff_is_lineno_count_overflow 5992 (abfd, (bfd_vma) o->lineno_count))) 5993 /* 64 does not overflow, need to check if 32 does */ 5994 sofar += bfd_coff_scnhsz (abfd); 5995 5996 for (o = abfd->sections; o != NULL; o = o->next) 5997 { 5998 if (strcmp (o->name, ".pad") == 0) 5999 { 6000 bfd_vma pageoff; 6001 6002 BFD_ASSERT (o->size == 0); 6003 pageoff = sofar & (file_align - 1); 6004 if (pageoff != 0) 6005 { 6006 o->size = file_align - pageoff; 6007 sofar += file_align - pageoff; 6008 o->flags |= SEC_HAS_CONTENTS; 6009 } 6010 } 6011 else 6012 { 6013 if ((o->flags & SEC_HAS_CONTENTS) != 0) 6014 sofar += BFD_ALIGN (o->size, 6015 1 << o->alignment_power); 6016 } 6017 } 6018 } 6019 6020 if (! bfd_coff_compute_section_file_positions (abfd)) 6021 goto error_return; 6022 } 6023 6024 /* Allocate space for the pointers we need to keep for the relocs. */ 6025 { 6026 unsigned int i; 6027 6028 /* We use section_count + 1, rather than section_count, because 6029 the target_index fields are 1 based. */ 6030 amt = abfd->section_count + 1; 6031 amt *= sizeof (struct xcoff_link_section_info); 6032 flinfo.section_info = bfd_malloc (amt); 6033 if (flinfo.section_info == NULL) 6034 goto error_return; 6035 for (i = 0; i <= abfd->section_count; i++) 6036 { 6037 flinfo.section_info[i].relocs = NULL; 6038 flinfo.section_info[i].rel_hashes = NULL; 6039 flinfo.section_info[i].toc_rel_hashes = NULL; 6040 } 6041 } 6042 6043 /* Set the file positions for the relocs. */ 6044 rel_filepos = obj_relocbase (abfd); 6045 relsz = bfd_coff_relsz (abfd); 6046 max_output_reloc_count = 0; 6047 for (o = abfd->sections; o != NULL; o = o->next) 6048 { 6049 if (o->reloc_count == 0) 6050 o->rel_filepos = 0; 6051 else 6052 { 6053 /* A stripped file has no relocs. However, we still 6054 allocate the buffers, so that later code doesn't have to 6055 worry about whether we are stripping or not. */ 6056 if (info->strip == strip_all) 6057 o->rel_filepos = 0; 6058 else 6059 { 6060 o->flags |= SEC_RELOC; 6061 o->rel_filepos = rel_filepos; 6062 rel_filepos += o->reloc_count * relsz; 6063 } 6064 6065 /* We don't know the indices of global symbols until we have 6066 written out all the local symbols. For each section in 6067 the output file, we keep an array of pointers to hash 6068 table entries. Each entry in the array corresponds to a 6069 reloc. When we find a reloc against a global symbol, we 6070 set the corresponding entry in this array so that we can 6071 fix up the symbol index after we have written out all the 6072 local symbols. 6073 6074 Because of this problem, we also keep the relocs in 6075 memory until the end of the link. This wastes memory. 6076 We could backpatch the file later, I suppose, although it 6077 would be slow. */ 6078 amt = o->reloc_count; 6079 amt *= sizeof (struct internal_reloc); 6080 flinfo.section_info[o->target_index].relocs = bfd_malloc (amt); 6081 6082 amt = o->reloc_count; 6083 amt *= sizeof (struct xcoff_link_hash_entry *); 6084 flinfo.section_info[o->target_index].rel_hashes = bfd_malloc (amt); 6085 6086 if (flinfo.section_info[o->target_index].relocs == NULL 6087 || flinfo.section_info[o->target_index].rel_hashes == NULL) 6088 goto error_return; 6089 6090 if (o->reloc_count > max_output_reloc_count) 6091 max_output_reloc_count = o->reloc_count; 6092 } 6093 } 6094 6095 /* We now know the size of the relocs, so we can determine the file 6096 positions of the line numbers. */ 6097 line_filepos = rel_filepos; 6098 flinfo.line_filepos = line_filepos; 6099 linesz = bfd_coff_linesz (abfd); 6100 for (o = abfd->sections; o != NULL; o = o->next) 6101 { 6102 if (o->lineno_count == 0) 6103 o->line_filepos = 0; 6104 else 6105 { 6106 o->line_filepos = line_filepos; 6107 line_filepos += o->lineno_count * linesz; 6108 } 6109 6110 /* Reset the reloc and lineno counts, so that we can use them to 6111 count the number of entries we have output so far. */ 6112 o->reloc_count = 0; 6113 o->lineno_count = 0; 6114 } 6115 6116 obj_sym_filepos (abfd) = line_filepos; 6117 6118 /* Figure out the largest number of symbols in an input BFD. Take 6119 the opportunity to clear the output_has_begun fields of all the 6120 input BFD's. We want at least 6 symbols, since that is the 6121 number which xcoff_write_global_symbol may need. */ 6122 max_sym_count = 6; 6123 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 6124 { 6125 bfd_size_type sz; 6126 6127 sub->output_has_begun = FALSE; 6128 sz = obj_raw_syment_count (sub); 6129 if (sz > max_sym_count) 6130 max_sym_count = sz; 6131 } 6132 6133 /* Allocate some buffers used while linking. */ 6134 amt = max_sym_count * sizeof (struct internal_syment); 6135 flinfo.internal_syms = bfd_malloc (amt); 6136 6137 amt = max_sym_count * sizeof (long); 6138 flinfo.sym_indices = bfd_malloc (amt); 6139 6140 amt = (max_sym_count + 1) * symesz; 6141 flinfo.outsyms = bfd_malloc (amt); 6142 6143 amt = max_lineno_count * bfd_coff_linesz (abfd); 6144 flinfo.linenos = bfd_malloc (amt); 6145 6146 amt = max_contents_size; 6147 flinfo.contents = bfd_malloc (amt); 6148 6149 amt = max_reloc_count * relsz; 6150 flinfo.external_relocs = bfd_malloc (amt); 6151 6152 if ((flinfo.internal_syms == NULL && max_sym_count > 0) 6153 || (flinfo.sym_indices == NULL && max_sym_count > 0) 6154 || flinfo.outsyms == NULL 6155 || (flinfo.linenos == NULL && max_lineno_count > 0) 6156 || (flinfo.contents == NULL && max_contents_size > 0) 6157 || (flinfo.external_relocs == NULL && max_reloc_count > 0)) 6158 goto error_return; 6159 6160 obj_raw_syment_count (abfd) = 0; 6161 6162 /* Find a TOC symbol, if we need one. */ 6163 if (!xcoff_find_tc0 (abfd, &flinfo)) 6164 goto error_return; 6165 6166 /* We now know the position of everything in the file, except that 6167 we don't know the size of the symbol table and therefore we don't 6168 know where the string table starts. We just build the string 6169 table in memory as we go along. We process all the relocations 6170 for a single input file at once. */ 6171 for (o = abfd->sections; o != NULL; o = o->next) 6172 { 6173 for (p = o->map_head.link_order; p != NULL; p = p->next) 6174 { 6175 if (p->type == bfd_indirect_link_order 6176 && p->u.indirect.section->owner->xvec == abfd->xvec) 6177 { 6178 sub = p->u.indirect.section->owner; 6179 if (! sub->output_has_begun) 6180 { 6181 if (! xcoff_link_input_bfd (&flinfo, sub)) 6182 goto error_return; 6183 sub->output_has_begun = TRUE; 6184 } 6185 } 6186 else if (p->type == bfd_section_reloc_link_order 6187 || p->type == bfd_symbol_reloc_link_order) 6188 { 6189 if (! xcoff_reloc_link_order (abfd, &flinfo, o, p)) 6190 goto error_return; 6191 } 6192 else 6193 { 6194 if (! _bfd_default_link_order (abfd, info, o, p)) 6195 goto error_return; 6196 } 6197 } 6198 } 6199 6200 /* Free up the buffers used by xcoff_link_input_bfd. */ 6201 if (flinfo.internal_syms != NULL) 6202 { 6203 free (flinfo.internal_syms); 6204 flinfo.internal_syms = NULL; 6205 } 6206 if (flinfo.sym_indices != NULL) 6207 { 6208 free (flinfo.sym_indices); 6209 flinfo.sym_indices = NULL; 6210 } 6211 if (flinfo.linenos != NULL) 6212 { 6213 free (flinfo.linenos); 6214 flinfo.linenos = NULL; 6215 } 6216 if (flinfo.contents != NULL) 6217 { 6218 free (flinfo.contents); 6219 flinfo.contents = NULL; 6220 } 6221 if (flinfo.external_relocs != NULL) 6222 { 6223 free (flinfo.external_relocs); 6224 flinfo.external_relocs = NULL; 6225 } 6226 6227 /* The value of the last C_FILE symbol is supposed to be -1. Write 6228 it out again. */ 6229 if (flinfo.last_file_index != -1) 6230 { 6231 flinfo.last_file.n_value = -(bfd_vma) 1; 6232 bfd_coff_swap_sym_out (abfd, (void *) &flinfo.last_file, 6233 (void *) flinfo.outsyms); 6234 pos = obj_sym_filepos (abfd) + flinfo.last_file_index * symesz; 6235 if (bfd_seek (abfd, pos, SEEK_SET) != 0 6236 || bfd_bwrite (flinfo.outsyms, symesz, abfd) != symesz) 6237 goto error_return; 6238 } 6239 6240 /* Write out all the global symbols which do not come from XCOFF 6241 input files. */ 6242 bfd_hash_traverse (&info->hash->table, xcoff_write_global_symbol, &flinfo); 6243 6244 if (flinfo.outsyms != NULL) 6245 { 6246 free (flinfo.outsyms); 6247 flinfo.outsyms = NULL; 6248 } 6249 6250 /* Now that we have written out all the global symbols, we know the 6251 symbol indices to use for relocs against them, and we can finally 6252 write out the relocs. */ 6253 amt = max_output_reloc_count * relsz; 6254 external_relocs = bfd_malloc (amt); 6255 if (external_relocs == NULL && max_output_reloc_count != 0) 6256 goto error_return; 6257 6258 for (o = abfd->sections; o != NULL; o = o->next) 6259 { 6260 struct internal_reloc *irel; 6261 struct internal_reloc *irelend; 6262 struct xcoff_link_hash_entry **rel_hash; 6263 struct xcoff_toc_rel_hash *toc_rel_hash; 6264 bfd_byte *erel; 6265 bfd_size_type rel_size; 6266 6267 /* A stripped file has no relocs. */ 6268 if (info->strip == strip_all) 6269 { 6270 o->reloc_count = 0; 6271 continue; 6272 } 6273 6274 if (o->reloc_count == 0) 6275 continue; 6276 6277 irel = flinfo.section_info[o->target_index].relocs; 6278 irelend = irel + o->reloc_count; 6279 rel_hash = flinfo.section_info[o->target_index].rel_hashes; 6280 for (; irel < irelend; irel++, rel_hash++) 6281 { 6282 if (*rel_hash != NULL) 6283 { 6284 if ((*rel_hash)->indx < 0) 6285 { 6286 (*info->callbacks->unattached_reloc) 6287 (info, (*rel_hash)->root.root.string, 6288 NULL, o, irel->r_vaddr); 6289 (*rel_hash)->indx = 0; 6290 } 6291 irel->r_symndx = (*rel_hash)->indx; 6292 } 6293 } 6294 6295 for (toc_rel_hash = flinfo.section_info[o->target_index].toc_rel_hashes; 6296 toc_rel_hash != NULL; 6297 toc_rel_hash = toc_rel_hash->next) 6298 { 6299 if (toc_rel_hash->h->u.toc_indx < 0) 6300 { 6301 (*info->callbacks->unattached_reloc) 6302 (info, toc_rel_hash->h->root.root.string, 6303 NULL, o, toc_rel_hash->rel->r_vaddr); 6304 toc_rel_hash->h->u.toc_indx = 0; 6305 } 6306 toc_rel_hash->rel->r_symndx = toc_rel_hash->h->u.toc_indx; 6307 } 6308 6309 /* XCOFF requires that the relocs be sorted by address. We tend 6310 to produce them in the order in which their containing csects 6311 appear in the symbol table, which is not necessarily by 6312 address. So we sort them here. There may be a better way to 6313 do this. */ 6314 qsort ((void *) flinfo.section_info[o->target_index].relocs, 6315 o->reloc_count, sizeof (struct internal_reloc), 6316 xcoff_sort_relocs); 6317 6318 irel = flinfo.section_info[o->target_index].relocs; 6319 irelend = irel + o->reloc_count; 6320 erel = external_relocs; 6321 for (; irel < irelend; irel++, rel_hash++, erel += relsz) 6322 bfd_coff_swap_reloc_out (abfd, (void *) irel, (void *) erel); 6323 6324 rel_size = relsz * o->reloc_count; 6325 if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0 6326 || bfd_bwrite ((void *) external_relocs, rel_size, abfd) != rel_size) 6327 goto error_return; 6328 } 6329 6330 if (external_relocs != NULL) 6331 { 6332 free (external_relocs); 6333 external_relocs = NULL; 6334 } 6335 6336 /* Free up the section information. */ 6337 if (flinfo.section_info != NULL) 6338 { 6339 unsigned int i; 6340 6341 for (i = 0; i < abfd->section_count; i++) 6342 { 6343 if (flinfo.section_info[i].relocs != NULL) 6344 free (flinfo.section_info[i].relocs); 6345 if (flinfo.section_info[i].rel_hashes != NULL) 6346 free (flinfo.section_info[i].rel_hashes); 6347 } 6348 free (flinfo.section_info); 6349 flinfo.section_info = NULL; 6350 } 6351 6352 /* Write out the loader section contents. */ 6353 o = xcoff_hash_table (info)->loader_section; 6354 if (o) 6355 { 6356 BFD_ASSERT ((bfd_byte *) flinfo.ldrel 6357 == (xcoff_hash_table (info)->loader_section->contents 6358 + xcoff_hash_table (info)->ldhdr.l_impoff)); 6359 if (!bfd_set_section_contents (abfd, o->output_section, o->contents, 6360 (file_ptr) o->output_offset, o->size)) 6361 goto error_return; 6362 } 6363 6364 /* Write out the magic sections. */ 6365 o = xcoff_hash_table (info)->linkage_section; 6366 if (o->size > 0 6367 && ! bfd_set_section_contents (abfd, o->output_section, o->contents, 6368 (file_ptr) o->output_offset, 6369 o->size)) 6370 goto error_return; 6371 o = xcoff_hash_table (info)->toc_section; 6372 if (o->size > 0 6373 && ! bfd_set_section_contents (abfd, o->output_section, o->contents, 6374 (file_ptr) o->output_offset, 6375 o->size)) 6376 goto error_return; 6377 o = xcoff_hash_table (info)->descriptor_section; 6378 if (o->size > 0 6379 && ! bfd_set_section_contents (abfd, o->output_section, o->contents, 6380 (file_ptr) o->output_offset, 6381 o->size)) 6382 goto error_return; 6383 6384 /* Write out the string table. */ 6385 pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz; 6386 if (bfd_seek (abfd, pos, SEEK_SET) != 0) 6387 goto error_return; 6388 H_PUT_32 (abfd, 6389 _bfd_stringtab_size (flinfo.strtab) + STRING_SIZE_SIZE, 6390 strbuf); 6391 amt = STRING_SIZE_SIZE; 6392 if (bfd_bwrite (strbuf, amt, abfd) != amt) 6393 goto error_return; 6394 if (! _bfd_stringtab_emit (abfd, flinfo.strtab)) 6395 goto error_return; 6396 6397 _bfd_stringtab_free (flinfo.strtab); 6398 6399 /* Write out the debugging string table. */ 6400 o = xcoff_hash_table (info)->debug_section; 6401 if (o != NULL) 6402 { 6403 struct bfd_strtab_hash *debug_strtab; 6404 6405 debug_strtab = xcoff_hash_table (info)->debug_strtab; 6406 BFD_ASSERT (o->output_section->size - o->output_offset 6407 >= _bfd_stringtab_size (debug_strtab)); 6408 pos = o->output_section->filepos + o->output_offset; 6409 if (bfd_seek (abfd, pos, SEEK_SET) != 0) 6410 goto error_return; 6411 if (! _bfd_stringtab_emit (abfd, debug_strtab)) 6412 goto error_return; 6413 } 6414 6415 /* Setting bfd_get_symcount to 0 will cause write_object_contents to 6416 not try to write out the symbols. */ 6417 bfd_get_symcount (abfd) = 0; 6418 6419 return TRUE; 6420 6421 error_return: 6422 if (flinfo.strtab != NULL) 6423 _bfd_stringtab_free (flinfo.strtab); 6424 6425 if (flinfo.section_info != NULL) 6426 { 6427 unsigned int i; 6428 6429 for (i = 0; i < abfd->section_count; i++) 6430 { 6431 if (flinfo.section_info[i].relocs != NULL) 6432 free (flinfo.section_info[i].relocs); 6433 if (flinfo.section_info[i].rel_hashes != NULL) 6434 free (flinfo.section_info[i].rel_hashes); 6435 } 6436 free (flinfo.section_info); 6437 } 6438 6439 if (flinfo.internal_syms != NULL) 6440 free (flinfo.internal_syms); 6441 if (flinfo.sym_indices != NULL) 6442 free (flinfo.sym_indices); 6443 if (flinfo.outsyms != NULL) 6444 free (flinfo.outsyms); 6445 if (flinfo.linenos != NULL) 6446 free (flinfo.linenos); 6447 if (flinfo.contents != NULL) 6448 free (flinfo.contents); 6449 if (flinfo.external_relocs != NULL) 6450 free (flinfo.external_relocs); 6451 if (external_relocs != NULL) 6452 free (external_relocs); 6453 return FALSE; 6454 } 6455