1 /* ELF linking support for BFD. 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 3 2005, 2006 Free Software Foundation, Inc. 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 2 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ 20 21 #include "bfd.h" 22 #include "sysdep.h" 23 #include "bfdlink.h" 24 #include "libbfd.h" 25 #define ARCH_SIZE 0 26 #include "elf-bfd.h" 27 #include "safe-ctype.h" 28 #include "libiberty.h" 29 #include "objalloc.h" 30 31 /* Define a symbol in a dynamic linkage section. */ 32 33 struct elf_link_hash_entry * 34 _bfd_elf_define_linkage_sym (bfd *abfd, 35 struct bfd_link_info *info, 36 asection *sec, 37 const char *name) 38 { 39 struct elf_link_hash_entry *h; 40 struct bfd_link_hash_entry *bh; 41 const struct elf_backend_data *bed; 42 43 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 44 if (h != NULL) 45 { 46 /* Zap symbol defined in an as-needed lib that wasn't linked. 47 This is a symptom of a larger problem: Absolute symbols 48 defined in shared libraries can't be overridden, because we 49 lose the link to the bfd which is via the symbol section. */ 50 h->root.type = bfd_link_hash_new; 51 } 52 53 bh = &h->root; 54 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, 55 sec, 0, NULL, FALSE, 56 get_elf_backend_data (abfd)->collect, 57 &bh)) 58 return NULL; 59 h = (struct elf_link_hash_entry *) bh; 60 h->def_regular = 1; 61 h->type = STT_OBJECT; 62 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 63 64 bed = get_elf_backend_data (abfd); 65 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 66 return h; 67 } 68 69 bfd_boolean 70 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) 71 { 72 flagword flags; 73 asection *s; 74 struct elf_link_hash_entry *h; 75 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 76 int ptralign; 77 78 /* This function may be called more than once. */ 79 s = bfd_get_section_by_name (abfd, ".got"); 80 if (s != NULL && (s->flags & SEC_LINKER_CREATED) != 0) 81 return TRUE; 82 83 switch (bed->s->arch_size) 84 { 85 case 32: 86 ptralign = 2; 87 break; 88 89 case 64: 90 ptralign = 3; 91 break; 92 93 default: 94 bfd_set_error (bfd_error_bad_value); 95 return FALSE; 96 } 97 98 flags = bed->dynamic_sec_flags; 99 100 s = bfd_make_section_with_flags (abfd, ".got", flags); 101 if (s == NULL 102 || !bfd_set_section_alignment (abfd, s, ptralign)) 103 return FALSE; 104 105 if (bed->want_got_plt) 106 { 107 s = bfd_make_section_with_flags (abfd, ".got.plt", flags); 108 if (s == NULL 109 || !bfd_set_section_alignment (abfd, s, ptralign)) 110 return FALSE; 111 } 112 113 if (bed->want_got_sym) 114 { 115 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 116 (or .got.plt) section. We don't do this in the linker script 117 because we don't want to define the symbol if we are not creating 118 a global offset table. */ 119 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); 120 elf_hash_table (info)->hgot = h; 121 if (h == NULL) 122 return FALSE; 123 } 124 125 /* The first bit of the global offset table is the header. */ 126 s->size += bed->got_header_size; 127 128 return TRUE; 129 } 130 131 /* Create a strtab to hold the dynamic symbol names. */ 132 static bfd_boolean 133 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) 134 { 135 struct elf_link_hash_table *hash_table; 136 137 hash_table = elf_hash_table (info); 138 if (hash_table->dynobj == NULL) 139 hash_table->dynobj = abfd; 140 141 if (hash_table->dynstr == NULL) 142 { 143 hash_table->dynstr = _bfd_elf_strtab_init (); 144 if (hash_table->dynstr == NULL) 145 return FALSE; 146 } 147 return TRUE; 148 } 149 150 /* Create some sections which will be filled in with dynamic linking 151 information. ABFD is an input file which requires dynamic sections 152 to be created. The dynamic sections take up virtual memory space 153 when the final executable is run, so we need to create them before 154 addresses are assigned to the output sections. We work out the 155 actual contents and size of these sections later. */ 156 157 bfd_boolean 158 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 159 { 160 flagword flags; 161 register asection *s; 162 const struct elf_backend_data *bed; 163 164 if (! is_elf_hash_table (info->hash)) 165 return FALSE; 166 167 if (elf_hash_table (info)->dynamic_sections_created) 168 return TRUE; 169 170 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 171 return FALSE; 172 173 abfd = elf_hash_table (info)->dynobj; 174 bed = get_elf_backend_data (abfd); 175 176 flags = bed->dynamic_sec_flags; 177 178 /* A dynamically linked executable has a .interp section, but a 179 shared library does not. */ 180 if (info->executable && !info->static_link) 181 { 182 s = bfd_make_section_with_flags (abfd, ".interp", 183 flags | SEC_READONLY); 184 if (s == NULL) 185 return FALSE; 186 } 187 188 if (! info->traditional_format) 189 { 190 s = bfd_make_section_with_flags (abfd, ".eh_frame_hdr", 191 flags | SEC_READONLY); 192 if (s == NULL 193 || ! bfd_set_section_alignment (abfd, s, 2)) 194 return FALSE; 195 elf_hash_table (info)->eh_info.hdr_sec = s; 196 } 197 198 /* Create sections to hold version informations. These are removed 199 if they are not needed. */ 200 s = bfd_make_section_with_flags (abfd, ".gnu.version_d", 201 flags | SEC_READONLY); 202 if (s == NULL 203 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 204 return FALSE; 205 206 s = bfd_make_section_with_flags (abfd, ".gnu.version", 207 flags | SEC_READONLY); 208 if (s == NULL 209 || ! bfd_set_section_alignment (abfd, s, 1)) 210 return FALSE; 211 212 s = bfd_make_section_with_flags (abfd, ".gnu.version_r", 213 flags | SEC_READONLY); 214 if (s == NULL 215 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 216 return FALSE; 217 218 s = bfd_make_section_with_flags (abfd, ".dynsym", 219 flags | SEC_READONLY); 220 if (s == NULL 221 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 222 return FALSE; 223 224 s = bfd_make_section_with_flags (abfd, ".dynstr", 225 flags | SEC_READONLY); 226 if (s == NULL) 227 return FALSE; 228 229 s = bfd_make_section_with_flags (abfd, ".dynamic", flags); 230 if (s == NULL 231 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 232 return FALSE; 233 234 /* The special symbol _DYNAMIC is always set to the start of the 235 .dynamic section. We could set _DYNAMIC in a linker script, but we 236 only want to define it if we are, in fact, creating a .dynamic 237 section. We don't want to define it if there is no .dynamic 238 section, since on some ELF platforms the start up code examines it 239 to decide how to initialize the process. */ 240 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) 241 return FALSE; 242 243 s = bfd_make_section_with_flags (abfd, ".hash", 244 flags | SEC_READONLY); 245 if (s == NULL 246 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 247 return FALSE; 248 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; 249 250 /* Let the backend create the rest of the sections. This lets the 251 backend set the right flags. The backend will normally create 252 the .got and .plt sections. */ 253 if (! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) 254 return FALSE; 255 256 elf_hash_table (info)->dynamic_sections_created = TRUE; 257 258 return TRUE; 259 } 260 261 /* Create dynamic sections when linking against a dynamic object. */ 262 263 bfd_boolean 264 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 265 { 266 flagword flags, pltflags; 267 struct elf_link_hash_entry *h; 268 asection *s; 269 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 270 271 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 272 .rel[a].bss sections. */ 273 flags = bed->dynamic_sec_flags; 274 275 pltflags = flags; 276 if (bed->plt_not_loaded) 277 /* We do not clear SEC_ALLOC here because we still want the OS to 278 allocate space for the section; it's just that there's nothing 279 to read in from the object file. */ 280 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); 281 else 282 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; 283 if (bed->plt_readonly) 284 pltflags |= SEC_READONLY; 285 286 s = bfd_make_section_with_flags (abfd, ".plt", pltflags); 287 if (s == NULL 288 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 289 return FALSE; 290 291 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 292 .plt section. */ 293 if (bed->want_plt_sym) 294 { 295 h = _bfd_elf_define_linkage_sym (abfd, info, s, 296 "_PROCEDURE_LINKAGE_TABLE_"); 297 elf_hash_table (info)->hplt = h; 298 if (h == NULL) 299 return FALSE; 300 } 301 302 s = bfd_make_section_with_flags (abfd, 303 (bed->default_use_rela_p 304 ? ".rela.plt" : ".rel.plt"), 305 flags | SEC_READONLY); 306 if (s == NULL 307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 308 return FALSE; 309 310 if (! _bfd_elf_create_got_section (abfd, info)) 311 return FALSE; 312 313 if (bed->want_dynbss) 314 { 315 /* The .dynbss section is a place to put symbols which are defined 316 by dynamic objects, are referenced by regular objects, and are 317 not functions. We must allocate space for them in the process 318 image and use a R_*_COPY reloc to tell the dynamic linker to 319 initialize them at run time. The linker script puts the .dynbss 320 section into the .bss section of the final image. */ 321 s = bfd_make_section_with_flags (abfd, ".dynbss", 322 (SEC_ALLOC 323 | SEC_LINKER_CREATED)); 324 if (s == NULL) 325 return FALSE; 326 327 /* The .rel[a].bss section holds copy relocs. This section is not 328 normally needed. We need to create it here, though, so that the 329 linker will map it to an output section. We can't just create it 330 only if we need it, because we will not know whether we need it 331 until we have seen all the input files, and the first time the 332 main linker code calls BFD after examining all the input files 333 (size_dynamic_sections) the input sections have already been 334 mapped to the output sections. If the section turns out not to 335 be needed, we can discard it later. We will never need this 336 section when generating a shared object, since they do not use 337 copy relocs. */ 338 if (! info->shared) 339 { 340 s = bfd_make_section_with_flags (abfd, 341 (bed->default_use_rela_p 342 ? ".rela.bss" : ".rel.bss"), 343 flags | SEC_READONLY); 344 if (s == NULL 345 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 346 return FALSE; 347 } 348 } 349 350 return TRUE; 351 } 352 353 /* Record a new dynamic symbol. We record the dynamic symbols as we 354 read the input files, since we need to have a list of all of them 355 before we can determine the final sizes of the output sections. 356 Note that we may actually call this function even though we are not 357 going to output any dynamic symbols; in some cases we know that a 358 symbol should be in the dynamic symbol table, but only if there is 359 one. */ 360 361 bfd_boolean 362 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, 363 struct elf_link_hash_entry *h) 364 { 365 if (h->dynindx == -1) 366 { 367 struct elf_strtab_hash *dynstr; 368 char *p; 369 const char *name; 370 bfd_size_type indx; 371 372 /* XXX: The ABI draft says the linker must turn hidden and 373 internal symbols into STB_LOCAL symbols when producing the 374 DSO. However, if ld.so honors st_other in the dynamic table, 375 this would not be necessary. */ 376 switch (ELF_ST_VISIBILITY (h->other)) 377 { 378 case STV_INTERNAL: 379 case STV_HIDDEN: 380 if (h->root.type != bfd_link_hash_undefined 381 && h->root.type != bfd_link_hash_undefweak) 382 { 383 h->forced_local = 1; 384 if (!elf_hash_table (info)->is_relocatable_executable) 385 return TRUE; 386 } 387 388 default: 389 break; 390 } 391 392 h->dynindx = elf_hash_table (info)->dynsymcount; 393 ++elf_hash_table (info)->dynsymcount; 394 395 dynstr = elf_hash_table (info)->dynstr; 396 if (dynstr == NULL) 397 { 398 /* Create a strtab to hold the dynamic symbol names. */ 399 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 400 if (dynstr == NULL) 401 return FALSE; 402 } 403 404 /* We don't put any version information in the dynamic string 405 table. */ 406 name = h->root.root.string; 407 p = strchr (name, ELF_VER_CHR); 408 if (p != NULL) 409 /* We know that the p points into writable memory. In fact, 410 there are only a few symbols that have read-only names, being 411 those like _GLOBAL_OFFSET_TABLE_ that are created specially 412 by the backends. Most symbols will have names pointing into 413 an ELF string table read from a file, or to objalloc memory. */ 414 *p = 0; 415 416 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); 417 418 if (p != NULL) 419 *p = ELF_VER_CHR; 420 421 if (indx == (bfd_size_type) -1) 422 return FALSE; 423 h->dynstr_index = indx; 424 } 425 426 return TRUE; 427 } 428 429 /* Record an assignment to a symbol made by a linker script. We need 430 this in case some dynamic object refers to this symbol. */ 431 432 bfd_boolean 433 bfd_elf_record_link_assignment (bfd *output_bfd, 434 struct bfd_link_info *info, 435 const char *name, 436 bfd_boolean provide, 437 bfd_boolean hidden) 438 { 439 struct elf_link_hash_entry *h; 440 struct elf_link_hash_table *htab; 441 442 if (!is_elf_hash_table (info->hash)) 443 return TRUE; 444 445 htab = elf_hash_table (info); 446 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); 447 if (h == NULL) 448 return provide; 449 450 /* Since we're defining the symbol, don't let it seem to have not 451 been defined. record_dynamic_symbol and size_dynamic_sections 452 may depend on this. */ 453 if (h->root.type == bfd_link_hash_undefweak 454 || h->root.type == bfd_link_hash_undefined) 455 { 456 h->root.type = bfd_link_hash_new; 457 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) 458 bfd_link_repair_undef_list (&htab->root); 459 } 460 461 if (h->root.type == bfd_link_hash_new) 462 h->non_elf = 0; 463 464 /* If this symbol is being provided by the linker script, and it is 465 currently defined by a dynamic object, but not by a regular 466 object, then mark it as undefined so that the generic linker will 467 force the correct value. */ 468 if (provide 469 && h->def_dynamic 470 && !h->def_regular) 471 h->root.type = bfd_link_hash_undefined; 472 473 /* If this symbol is not being provided by the linker script, and it is 474 currently defined by a dynamic object, but not by a regular object, 475 then clear out any version information because the symbol will not be 476 associated with the dynamic object any more. */ 477 if (!provide 478 && h->def_dynamic 479 && !h->def_regular) 480 h->verinfo.verdef = NULL; 481 482 h->def_regular = 1; 483 484 if (provide && hidden) 485 { 486 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 487 488 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 489 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 490 } 491 492 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects 493 and executables. */ 494 if (!info->relocatable 495 && h->dynindx != -1 496 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 497 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) 498 h->forced_local = 1; 499 500 if ((h->def_dynamic 501 || h->ref_dynamic 502 || info->shared 503 || (info->executable && elf_hash_table (info)->is_relocatable_executable)) 504 && h->dynindx == -1) 505 { 506 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 507 return FALSE; 508 509 /* If this is a weak defined symbol, and we know a corresponding 510 real symbol from the same dynamic object, make sure the real 511 symbol is also made into a dynamic symbol. */ 512 if (h->u.weakdef != NULL 513 && h->u.weakdef->dynindx == -1) 514 { 515 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) 516 return FALSE; 517 } 518 } 519 520 return TRUE; 521 } 522 523 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on 524 success, and 2 on a failure caused by attempting to record a symbol 525 in a discarded section, eg. a discarded link-once section symbol. */ 526 527 int 528 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, 529 bfd *input_bfd, 530 long input_indx) 531 { 532 bfd_size_type amt; 533 struct elf_link_local_dynamic_entry *entry; 534 struct elf_link_hash_table *eht; 535 struct elf_strtab_hash *dynstr; 536 unsigned long dynstr_index; 537 char *name; 538 Elf_External_Sym_Shndx eshndx; 539 char esym[sizeof (Elf64_External_Sym)]; 540 541 if (! is_elf_hash_table (info->hash)) 542 return 0; 543 544 /* See if the entry exists already. */ 545 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) 546 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) 547 return 1; 548 549 amt = sizeof (*entry); 550 entry = bfd_alloc (input_bfd, amt); 551 if (entry == NULL) 552 return 0; 553 554 /* Go find the symbol, so that we can find it's name. */ 555 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, 556 1, input_indx, &entry->isym, esym, &eshndx)) 557 { 558 bfd_release (input_bfd, entry); 559 return 0; 560 } 561 562 if (entry->isym.st_shndx != SHN_UNDEF 563 && (entry->isym.st_shndx < SHN_LORESERVE 564 || entry->isym.st_shndx > SHN_HIRESERVE)) 565 { 566 asection *s; 567 568 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); 569 if (s == NULL || bfd_is_abs_section (s->output_section)) 570 { 571 /* We can still bfd_release here as nothing has done another 572 bfd_alloc. We can't do this later in this function. */ 573 bfd_release (input_bfd, entry); 574 return 2; 575 } 576 } 577 578 name = (bfd_elf_string_from_elf_section 579 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, 580 entry->isym.st_name)); 581 582 dynstr = elf_hash_table (info)->dynstr; 583 if (dynstr == NULL) 584 { 585 /* Create a strtab to hold the dynamic symbol names. */ 586 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 587 if (dynstr == NULL) 588 return 0; 589 } 590 591 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); 592 if (dynstr_index == (unsigned long) -1) 593 return 0; 594 entry->isym.st_name = dynstr_index; 595 596 eht = elf_hash_table (info); 597 598 entry->next = eht->dynlocal; 599 eht->dynlocal = entry; 600 entry->input_bfd = input_bfd; 601 entry->input_indx = input_indx; 602 eht->dynsymcount++; 603 604 /* Whatever binding the symbol had before, it's now local. */ 605 entry->isym.st_info 606 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); 607 608 /* The dynindx will be set at the end of size_dynamic_sections. */ 609 610 return 1; 611 } 612 613 /* Return the dynindex of a local dynamic symbol. */ 614 615 long 616 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, 617 bfd *input_bfd, 618 long input_indx) 619 { 620 struct elf_link_local_dynamic_entry *e; 621 622 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 623 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 624 return e->dynindx; 625 return -1; 626 } 627 628 /* This function is used to renumber the dynamic symbols, if some of 629 them are removed because they are marked as local. This is called 630 via elf_link_hash_traverse. */ 631 632 static bfd_boolean 633 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, 634 void *data) 635 { 636 size_t *count = data; 637 638 if (h->root.type == bfd_link_hash_warning) 639 h = (struct elf_link_hash_entry *) h->root.u.i.link; 640 641 if (h->forced_local) 642 return TRUE; 643 644 if (h->dynindx != -1) 645 h->dynindx = ++(*count); 646 647 return TRUE; 648 } 649 650 651 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with 652 STB_LOCAL binding. */ 653 654 static bfd_boolean 655 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, 656 void *data) 657 { 658 size_t *count = data; 659 660 if (h->root.type == bfd_link_hash_warning) 661 h = (struct elf_link_hash_entry *) h->root.u.i.link; 662 663 if (!h->forced_local) 664 return TRUE; 665 666 if (h->dynindx != -1) 667 h->dynindx = ++(*count); 668 669 return TRUE; 670 } 671 672 /* Return true if the dynamic symbol for a given section should be 673 omitted when creating a shared library. */ 674 bfd_boolean 675 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, 676 struct bfd_link_info *info, 677 asection *p) 678 { 679 switch (elf_section_data (p)->this_hdr.sh_type) 680 { 681 case SHT_PROGBITS: 682 case SHT_NOBITS: 683 /* If sh_type is yet undecided, assume it could be 684 SHT_PROGBITS/SHT_NOBITS. */ 685 case SHT_NULL: 686 if (strcmp (p->name, ".got") == 0 687 || strcmp (p->name, ".got.plt") == 0 688 || strcmp (p->name, ".plt") == 0) 689 { 690 asection *ip; 691 bfd *dynobj = elf_hash_table (info)->dynobj; 692 693 if (dynobj != NULL 694 && (ip = bfd_get_section_by_name (dynobj, p->name)) != NULL 695 && (ip->flags & SEC_LINKER_CREATED) 696 && ip->output_section == p) 697 return TRUE; 698 } 699 return FALSE; 700 701 /* There shouldn't be section relative relocations 702 against any other section. */ 703 default: 704 return TRUE; 705 } 706 } 707 708 /* Assign dynsym indices. In a shared library we generate a section 709 symbol for each output section, which come first. Next come symbols 710 which have been forced to local binding. Then all of the back-end 711 allocated local dynamic syms, followed by the rest of the global 712 symbols. */ 713 714 static unsigned long 715 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, 716 struct bfd_link_info *info, 717 unsigned long *section_sym_count) 718 { 719 unsigned long dynsymcount = 0; 720 721 if (info->shared || elf_hash_table (info)->is_relocatable_executable) 722 { 723 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 724 asection *p; 725 for (p = output_bfd->sections; p ; p = p->next) 726 if ((p->flags & SEC_EXCLUDE) == 0 727 && (p->flags & SEC_ALLOC) != 0 728 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) 729 elf_section_data (p)->dynindx = ++dynsymcount; 730 } 731 *section_sym_count = dynsymcount; 732 733 elf_link_hash_traverse (elf_hash_table (info), 734 elf_link_renumber_local_hash_table_dynsyms, 735 &dynsymcount); 736 737 if (elf_hash_table (info)->dynlocal) 738 { 739 struct elf_link_local_dynamic_entry *p; 740 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 741 p->dynindx = ++dynsymcount; 742 } 743 744 elf_link_hash_traverse (elf_hash_table (info), 745 elf_link_renumber_hash_table_dynsyms, 746 &dynsymcount); 747 748 /* There is an unused NULL entry at the head of the table which 749 we must account for in our count. Unless there weren't any 750 symbols, which means we'll have no table at all. */ 751 if (dynsymcount != 0) 752 ++dynsymcount; 753 754 elf_hash_table (info)->dynsymcount = dynsymcount; 755 return dynsymcount; 756 } 757 758 /* This function is called when we want to define a new symbol. It 759 handles the various cases which arise when we find a definition in 760 a dynamic object, or when there is already a definition in a 761 dynamic object. The new symbol is described by NAME, SYM, PSEC, 762 and PVALUE. We set SYM_HASH to the hash table entry. We set 763 OVERRIDE if the old symbol is overriding a new definition. We set 764 TYPE_CHANGE_OK if it is OK for the type to change. We set 765 SIZE_CHANGE_OK if it is OK for the size to change. By OK to 766 change, we mean that we shouldn't warn if the type or size does 767 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic 768 object is overridden by a regular object. */ 769 770 bfd_boolean 771 _bfd_elf_merge_symbol (bfd *abfd, 772 struct bfd_link_info *info, 773 const char *name, 774 Elf_Internal_Sym *sym, 775 asection **psec, 776 bfd_vma *pvalue, 777 unsigned int *pold_alignment, 778 struct elf_link_hash_entry **sym_hash, 779 bfd_boolean *skip, 780 bfd_boolean *override, 781 bfd_boolean *type_change_ok, 782 bfd_boolean *size_change_ok) 783 { 784 asection *sec, *oldsec; 785 struct elf_link_hash_entry *h; 786 struct elf_link_hash_entry *flip; 787 int bind; 788 bfd *oldbfd; 789 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; 790 bfd_boolean newweak, oldweak; 791 const struct elf_backend_data *bed; 792 793 *skip = FALSE; 794 *override = FALSE; 795 796 sec = *psec; 797 bind = ELF_ST_BIND (sym->st_info); 798 799 if (! bfd_is_und_section (sec)) 800 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); 801 else 802 h = ((struct elf_link_hash_entry *) 803 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); 804 if (h == NULL) 805 return FALSE; 806 *sym_hash = h; 807 808 /* This code is for coping with dynamic objects, and is only useful 809 if we are doing an ELF link. */ 810 if (info->hash->creator != abfd->xvec) 811 return TRUE; 812 813 /* For merging, we only care about real symbols. */ 814 815 while (h->root.type == bfd_link_hash_indirect 816 || h->root.type == bfd_link_hash_warning) 817 h = (struct elf_link_hash_entry *) h->root.u.i.link; 818 819 /* If we just created the symbol, mark it as being an ELF symbol. 820 Other than that, there is nothing to do--there is no merge issue 821 with a newly defined symbol--so we just return. */ 822 823 if (h->root.type == bfd_link_hash_new) 824 { 825 h->non_elf = 0; 826 return TRUE; 827 } 828 829 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the 830 existing symbol. */ 831 832 switch (h->root.type) 833 { 834 default: 835 oldbfd = NULL; 836 oldsec = NULL; 837 break; 838 839 case bfd_link_hash_undefined: 840 case bfd_link_hash_undefweak: 841 oldbfd = h->root.u.undef.abfd; 842 oldsec = NULL; 843 break; 844 845 case bfd_link_hash_defined: 846 case bfd_link_hash_defweak: 847 oldbfd = h->root.u.def.section->owner; 848 oldsec = h->root.u.def.section; 849 break; 850 851 case bfd_link_hash_common: 852 oldbfd = h->root.u.c.p->section->owner; 853 oldsec = h->root.u.c.p->section; 854 break; 855 } 856 857 /* In cases involving weak versioned symbols, we may wind up trying 858 to merge a symbol with itself. Catch that here, to avoid the 859 confusion that results if we try to override a symbol with 860 itself. The additional tests catch cases like 861 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a 862 dynamic object, which we do want to handle here. */ 863 if (abfd == oldbfd 864 && ((abfd->flags & DYNAMIC) == 0 865 || !h->def_regular)) 866 return TRUE; 867 868 /* NEWDYN and OLDDYN indicate whether the new or old symbol, 869 respectively, is from a dynamic object. */ 870 871 newdyn = (abfd->flags & DYNAMIC) != 0; 872 873 olddyn = FALSE; 874 if (oldbfd != NULL) 875 olddyn = (oldbfd->flags & DYNAMIC) != 0; 876 else if (oldsec != NULL) 877 { 878 /* This handles the special SHN_MIPS_{TEXT,DATA} section 879 indices used by MIPS ELF. */ 880 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; 881 } 882 883 /* NEWDEF and OLDDEF indicate whether the new or old symbol, 884 respectively, appear to be a definition rather than reference. */ 885 886 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); 887 888 olddef = (h->root.type != bfd_link_hash_undefined 889 && h->root.type != bfd_link_hash_undefweak 890 && h->root.type != bfd_link_hash_common); 891 892 /* When we try to create a default indirect symbol from the dynamic 893 definition with the default version, we skip it if its type and 894 the type of existing regular definition mismatch. We only do it 895 if the existing regular definition won't be dynamic. */ 896 if (pold_alignment == NULL 897 && !info->shared 898 && !info->export_dynamic 899 && !h->ref_dynamic 900 && newdyn 901 && newdef 902 && !olddyn 903 && (olddef || h->root.type == bfd_link_hash_common) 904 && ELF_ST_TYPE (sym->st_info) != h->type 905 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 906 && h->type != STT_NOTYPE) 907 { 908 *skip = TRUE; 909 return TRUE; 910 } 911 912 /* Check TLS symbol. We don't check undefined symbol introduced by 913 "ld -u". */ 914 if ((ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS) 915 && ELF_ST_TYPE (sym->st_info) != h->type 916 && oldbfd != NULL) 917 { 918 bfd *ntbfd, *tbfd; 919 bfd_boolean ntdef, tdef; 920 asection *ntsec, *tsec; 921 922 if (h->type == STT_TLS) 923 { 924 ntbfd = abfd; 925 ntsec = sec; 926 ntdef = newdef; 927 tbfd = oldbfd; 928 tsec = oldsec; 929 tdef = olddef; 930 } 931 else 932 { 933 ntbfd = oldbfd; 934 ntsec = oldsec; 935 ntdef = olddef; 936 tbfd = abfd; 937 tsec = sec; 938 tdef = newdef; 939 } 940 941 if (tdef && ntdef) 942 (*_bfd_error_handler) 943 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), 944 tbfd, tsec, ntbfd, ntsec, h->root.root.string); 945 else if (!tdef && !ntdef) 946 (*_bfd_error_handler) 947 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), 948 tbfd, ntbfd, h->root.root.string); 949 else if (tdef) 950 (*_bfd_error_handler) 951 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), 952 tbfd, tsec, ntbfd, h->root.root.string); 953 else 954 (*_bfd_error_handler) 955 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), 956 tbfd, ntbfd, ntsec, h->root.root.string); 957 958 bfd_set_error (bfd_error_bad_value); 959 return FALSE; 960 } 961 962 /* We need to remember if a symbol has a definition in a dynamic 963 object or is weak in all dynamic objects. Internal and hidden 964 visibility will make it unavailable to dynamic objects. */ 965 if (newdyn && !h->dynamic_def) 966 { 967 if (!bfd_is_und_section (sec)) 968 h->dynamic_def = 1; 969 else 970 { 971 /* Check if this symbol is weak in all dynamic objects. If it 972 is the first time we see it in a dynamic object, we mark 973 if it is weak. Otherwise, we clear it. */ 974 if (!h->ref_dynamic) 975 { 976 if (bind == STB_WEAK) 977 h->dynamic_weak = 1; 978 } 979 else if (bind != STB_WEAK) 980 h->dynamic_weak = 0; 981 } 982 } 983 984 /* If the old symbol has non-default visibility, we ignore the new 985 definition from a dynamic object. */ 986 if (newdyn 987 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 988 && !bfd_is_und_section (sec)) 989 { 990 *skip = TRUE; 991 /* Make sure this symbol is dynamic. */ 992 h->ref_dynamic = 1; 993 /* A protected symbol has external availability. Make sure it is 994 recorded as dynamic. 995 996 FIXME: Should we check type and size for protected symbol? */ 997 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 998 return bfd_elf_link_record_dynamic_symbol (info, h); 999 else 1000 return TRUE; 1001 } 1002 else if (!newdyn 1003 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT 1004 && h->def_dynamic) 1005 { 1006 /* If the new symbol with non-default visibility comes from a 1007 relocatable file and the old definition comes from a dynamic 1008 object, we remove the old definition. */ 1009 if ((*sym_hash)->root.type == bfd_link_hash_indirect) 1010 h = *sym_hash; 1011 1012 if ((h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1013 && bfd_is_und_section (sec)) 1014 { 1015 /* If the new symbol is undefined and the old symbol was 1016 also undefined before, we need to make sure 1017 _bfd_generic_link_add_one_symbol doesn't mess 1018 up the linker hash table undefs list. Since the old 1019 definition came from a dynamic object, it is still on the 1020 undefs list. */ 1021 h->root.type = bfd_link_hash_undefined; 1022 h->root.u.undef.abfd = abfd; 1023 } 1024 else 1025 { 1026 h->root.type = bfd_link_hash_new; 1027 h->root.u.undef.abfd = NULL; 1028 } 1029 1030 if (h->def_dynamic) 1031 { 1032 h->def_dynamic = 0; 1033 h->ref_dynamic = 1; 1034 h->dynamic_def = 1; 1035 } 1036 /* FIXME: Should we check type and size for protected symbol? */ 1037 h->size = 0; 1038 h->type = 0; 1039 return TRUE; 1040 } 1041 1042 /* Differentiate strong and weak symbols. */ 1043 newweak = bind == STB_WEAK; 1044 oldweak = (h->root.type == bfd_link_hash_defweak 1045 || h->root.type == bfd_link_hash_undefweak); 1046 1047 /* If a new weak symbol definition comes from a regular file and the 1048 old symbol comes from a dynamic library, we treat the new one as 1049 strong. Similarly, an old weak symbol definition from a regular 1050 file is treated as strong when the new symbol comes from a dynamic 1051 library. Further, an old weak symbol from a dynamic library is 1052 treated as strong if the new symbol is from a dynamic library. 1053 This reflects the way glibc's ld.so works. 1054 1055 Do this before setting *type_change_ok or *size_change_ok so that 1056 we warn properly when dynamic library symbols are overridden. */ 1057 1058 if (newdef && !newdyn && olddyn) 1059 newweak = FALSE; 1060 if (olddef && newdyn) 1061 oldweak = FALSE; 1062 1063 /* It's OK to change the type if either the existing symbol or the 1064 new symbol is weak. A type change is also OK if the old symbol 1065 is undefined and the new symbol is defined. */ 1066 1067 if (oldweak 1068 || newweak 1069 || (newdef 1070 && h->root.type == bfd_link_hash_undefined)) 1071 *type_change_ok = TRUE; 1072 1073 /* It's OK to change the size if either the existing symbol or the 1074 new symbol is weak, or if the old symbol is undefined. */ 1075 1076 if (*type_change_ok 1077 || h->root.type == bfd_link_hash_undefined) 1078 *size_change_ok = TRUE; 1079 1080 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old 1081 symbol, respectively, appears to be a common symbol in a dynamic 1082 object. If a symbol appears in an uninitialized section, and is 1083 not weak, and is not a function, then it may be a common symbol 1084 which was resolved when the dynamic object was created. We want 1085 to treat such symbols specially, because they raise special 1086 considerations when setting the symbol size: if the symbol 1087 appears as a common symbol in a regular object, and the size in 1088 the regular object is larger, we must make sure that we use the 1089 larger size. This problematic case can always be avoided in C, 1090 but it must be handled correctly when using Fortran shared 1091 libraries. 1092 1093 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and 1094 likewise for OLDDYNCOMMON and OLDDEF. 1095 1096 Note that this test is just a heuristic, and that it is quite 1097 possible to have an uninitialized symbol in a shared object which 1098 is really a definition, rather than a common symbol. This could 1099 lead to some minor confusion when the symbol really is a common 1100 symbol in some regular object. However, I think it will be 1101 harmless. */ 1102 1103 if (newdyn 1104 && newdef 1105 && !newweak 1106 && (sec->flags & SEC_ALLOC) != 0 1107 && (sec->flags & SEC_LOAD) == 0 1108 && sym->st_size > 0 1109 && ELF_ST_TYPE (sym->st_info) != STT_FUNC) 1110 newdyncommon = TRUE; 1111 else 1112 newdyncommon = FALSE; 1113 1114 if (olddyn 1115 && olddef 1116 && h->root.type == bfd_link_hash_defined 1117 && h->def_dynamic 1118 && (h->root.u.def.section->flags & SEC_ALLOC) != 0 1119 && (h->root.u.def.section->flags & SEC_LOAD) == 0 1120 && h->size > 0 1121 && h->type != STT_FUNC) 1122 olddyncommon = TRUE; 1123 else 1124 olddyncommon = FALSE; 1125 1126 /* We now know everything about the old and new symbols. We ask the 1127 backend to check if we can merge them. */ 1128 bed = get_elf_backend_data (abfd); 1129 if (bed->merge_symbol 1130 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, 1131 pold_alignment, skip, override, 1132 type_change_ok, size_change_ok, 1133 &newdyn, &newdef, &newdyncommon, &newweak, 1134 abfd, &sec, 1135 &olddyn, &olddef, &olddyncommon, &oldweak, 1136 oldbfd, &oldsec)) 1137 return FALSE; 1138 1139 /* If both the old and the new symbols look like common symbols in a 1140 dynamic object, set the size of the symbol to the larger of the 1141 two. */ 1142 1143 if (olddyncommon 1144 && newdyncommon 1145 && sym->st_size != h->size) 1146 { 1147 /* Since we think we have two common symbols, issue a multiple 1148 common warning if desired. Note that we only warn if the 1149 size is different. If the size is the same, we simply let 1150 the old symbol override the new one as normally happens with 1151 symbols defined in dynamic objects. */ 1152 1153 if (! ((*info->callbacks->multiple_common) 1154 (info, h->root.root.string, oldbfd, bfd_link_hash_common, 1155 h->size, abfd, bfd_link_hash_common, sym->st_size))) 1156 return FALSE; 1157 1158 if (sym->st_size > h->size) 1159 h->size = sym->st_size; 1160 1161 *size_change_ok = TRUE; 1162 } 1163 1164 /* If we are looking at a dynamic object, and we have found a 1165 definition, we need to see if the symbol was already defined by 1166 some other object. If so, we want to use the existing 1167 definition, and we do not want to report a multiple symbol 1168 definition error; we do this by clobbering *PSEC to be 1169 bfd_und_section_ptr. 1170 1171 We treat a common symbol as a definition if the symbol in the 1172 shared library is a function, since common symbols always 1173 represent variables; this can cause confusion in principle, but 1174 any such confusion would seem to indicate an erroneous program or 1175 shared library. We also permit a common symbol in a regular 1176 object to override a weak symbol in a shared object. */ 1177 1178 if (newdyn 1179 && newdef 1180 && (olddef 1181 || (h->root.type == bfd_link_hash_common 1182 && (newweak 1183 || ELF_ST_TYPE (sym->st_info) == STT_FUNC)))) 1184 { 1185 *override = TRUE; 1186 newdef = FALSE; 1187 newdyncommon = FALSE; 1188 1189 *psec = sec = bfd_und_section_ptr; 1190 *size_change_ok = TRUE; 1191 1192 /* If we get here when the old symbol is a common symbol, then 1193 we are explicitly letting it override a weak symbol or 1194 function in a dynamic object, and we don't want to warn about 1195 a type change. If the old symbol is a defined symbol, a type 1196 change warning may still be appropriate. */ 1197 1198 if (h->root.type == bfd_link_hash_common) 1199 *type_change_ok = TRUE; 1200 } 1201 1202 /* Handle the special case of an old common symbol merging with a 1203 new symbol which looks like a common symbol in a shared object. 1204 We change *PSEC and *PVALUE to make the new symbol look like a 1205 common symbol, and let _bfd_generic_link_add_one_symbol do the 1206 right thing. */ 1207 1208 if (newdyncommon 1209 && h->root.type == bfd_link_hash_common) 1210 { 1211 *override = TRUE; 1212 newdef = FALSE; 1213 newdyncommon = FALSE; 1214 *pvalue = sym->st_size; 1215 *psec = sec = bed->common_section (oldsec); 1216 *size_change_ok = TRUE; 1217 } 1218 1219 /* Skip weak definitions of symbols that are already defined. */ 1220 if (newdef && olddef && newweak) 1221 *skip = TRUE; 1222 1223 /* If the old symbol is from a dynamic object, and the new symbol is 1224 a definition which is not from a dynamic object, then the new 1225 symbol overrides the old symbol. Symbols from regular files 1226 always take precedence over symbols from dynamic objects, even if 1227 they are defined after the dynamic object in the link. 1228 1229 As above, we again permit a common symbol in a regular object to 1230 override a definition in a shared object if the shared object 1231 symbol is a function or is weak. */ 1232 1233 flip = NULL; 1234 if (!newdyn 1235 && (newdef 1236 || (bfd_is_com_section (sec) 1237 && (oldweak 1238 || h->type == STT_FUNC))) 1239 && olddyn 1240 && olddef 1241 && h->def_dynamic) 1242 { 1243 /* Change the hash table entry to undefined, and let 1244 _bfd_generic_link_add_one_symbol do the right thing with the 1245 new definition. */ 1246 1247 h->root.type = bfd_link_hash_undefined; 1248 h->root.u.undef.abfd = h->root.u.def.section->owner; 1249 *size_change_ok = TRUE; 1250 1251 olddef = FALSE; 1252 olddyncommon = FALSE; 1253 1254 /* We again permit a type change when a common symbol may be 1255 overriding a function. */ 1256 1257 if (bfd_is_com_section (sec)) 1258 *type_change_ok = TRUE; 1259 1260 if ((*sym_hash)->root.type == bfd_link_hash_indirect) 1261 flip = *sym_hash; 1262 else 1263 /* This union may have been set to be non-NULL when this symbol 1264 was seen in a dynamic object. We must force the union to be 1265 NULL, so that it is correct for a regular symbol. */ 1266 h->verinfo.vertree = NULL; 1267 } 1268 1269 /* Handle the special case of a new common symbol merging with an 1270 old symbol that looks like it might be a common symbol defined in 1271 a shared object. Note that we have already handled the case in 1272 which a new common symbol should simply override the definition 1273 in the shared library. */ 1274 1275 if (! newdyn 1276 && bfd_is_com_section (sec) 1277 && olddyncommon) 1278 { 1279 /* It would be best if we could set the hash table entry to a 1280 common symbol, but we don't know what to use for the section 1281 or the alignment. */ 1282 if (! ((*info->callbacks->multiple_common) 1283 (info, h->root.root.string, oldbfd, bfd_link_hash_common, 1284 h->size, abfd, bfd_link_hash_common, sym->st_size))) 1285 return FALSE; 1286 1287 /* If the presumed common symbol in the dynamic object is 1288 larger, pretend that the new symbol has its size. */ 1289 1290 if (h->size > *pvalue) 1291 *pvalue = h->size; 1292 1293 /* We need to remember the alignment required by the symbol 1294 in the dynamic object. */ 1295 BFD_ASSERT (pold_alignment); 1296 *pold_alignment = h->root.u.def.section->alignment_power; 1297 1298 olddef = FALSE; 1299 olddyncommon = FALSE; 1300 1301 h->root.type = bfd_link_hash_undefined; 1302 h->root.u.undef.abfd = h->root.u.def.section->owner; 1303 1304 *size_change_ok = TRUE; 1305 *type_change_ok = TRUE; 1306 1307 if ((*sym_hash)->root.type == bfd_link_hash_indirect) 1308 flip = *sym_hash; 1309 else 1310 h->verinfo.vertree = NULL; 1311 } 1312 1313 if (flip != NULL) 1314 { 1315 /* Handle the case where we had a versioned symbol in a dynamic 1316 library and now find a definition in a normal object. In this 1317 case, we make the versioned symbol point to the normal one. */ 1318 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1319 flip->root.type = h->root.type; 1320 h->root.type = bfd_link_hash_indirect; 1321 h->root.u.i.link = (struct bfd_link_hash_entry *) flip; 1322 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); 1323 flip->root.u.undef.abfd = h->root.u.undef.abfd; 1324 if (h->def_dynamic) 1325 { 1326 h->def_dynamic = 0; 1327 flip->ref_dynamic = 1; 1328 } 1329 } 1330 1331 return TRUE; 1332 } 1333 1334 /* This function is called to create an indirect symbol from the 1335 default for the symbol with the default version if needed. The 1336 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We 1337 set DYNSYM if the new indirect symbol is dynamic. */ 1338 1339 bfd_boolean 1340 _bfd_elf_add_default_symbol (bfd *abfd, 1341 struct bfd_link_info *info, 1342 struct elf_link_hash_entry *h, 1343 const char *name, 1344 Elf_Internal_Sym *sym, 1345 asection **psec, 1346 bfd_vma *value, 1347 bfd_boolean *dynsym, 1348 bfd_boolean override) 1349 { 1350 bfd_boolean type_change_ok; 1351 bfd_boolean size_change_ok; 1352 bfd_boolean skip; 1353 char *shortname; 1354 struct elf_link_hash_entry *hi; 1355 struct bfd_link_hash_entry *bh; 1356 const struct elf_backend_data *bed; 1357 bfd_boolean collect; 1358 bfd_boolean dynamic; 1359 char *p; 1360 size_t len, shortlen; 1361 asection *sec; 1362 1363 /* If this symbol has a version, and it is the default version, we 1364 create an indirect symbol from the default name to the fully 1365 decorated name. This will cause external references which do not 1366 specify a version to be bound to this version of the symbol. */ 1367 p = strchr (name, ELF_VER_CHR); 1368 if (p == NULL || p[1] != ELF_VER_CHR) 1369 return TRUE; 1370 1371 if (override) 1372 { 1373 /* We are overridden by an old definition. We need to check if we 1374 need to create the indirect symbol from the default name. */ 1375 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, 1376 FALSE, FALSE); 1377 BFD_ASSERT (hi != NULL); 1378 if (hi == h) 1379 return TRUE; 1380 while (hi->root.type == bfd_link_hash_indirect 1381 || hi->root.type == bfd_link_hash_warning) 1382 { 1383 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1384 if (hi == h) 1385 return TRUE; 1386 } 1387 } 1388 1389 bed = get_elf_backend_data (abfd); 1390 collect = bed->collect; 1391 dynamic = (abfd->flags & DYNAMIC) != 0; 1392 1393 shortlen = p - name; 1394 shortname = bfd_hash_allocate (&info->hash->table, shortlen + 1); 1395 if (shortname == NULL) 1396 return FALSE; 1397 memcpy (shortname, name, shortlen); 1398 shortname[shortlen] = '\0'; 1399 1400 /* We are going to create a new symbol. Merge it with any existing 1401 symbol with this name. For the purposes of the merge, act as 1402 though we were defining the symbol we just defined, although we 1403 actually going to define an indirect symbol. */ 1404 type_change_ok = FALSE; 1405 size_change_ok = FALSE; 1406 sec = *psec; 1407 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, 1408 NULL, &hi, &skip, &override, 1409 &type_change_ok, &size_change_ok)) 1410 return FALSE; 1411 1412 if (skip) 1413 goto nondefault; 1414 1415 if (! override) 1416 { 1417 bh = &hi->root; 1418 if (! (_bfd_generic_link_add_one_symbol 1419 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, 1420 0, name, FALSE, collect, &bh))) 1421 return FALSE; 1422 hi = (struct elf_link_hash_entry *) bh; 1423 } 1424 else 1425 { 1426 /* In this case the symbol named SHORTNAME is overriding the 1427 indirect symbol we want to add. We were planning on making 1428 SHORTNAME an indirect symbol referring to NAME. SHORTNAME 1429 is the name without a version. NAME is the fully versioned 1430 name, and it is the default version. 1431 1432 Overriding means that we already saw a definition for the 1433 symbol SHORTNAME in a regular object, and it is overriding 1434 the symbol defined in the dynamic object. 1435 1436 When this happens, we actually want to change NAME, the 1437 symbol we just added, to refer to SHORTNAME. This will cause 1438 references to NAME in the shared object to become references 1439 to SHORTNAME in the regular object. This is what we expect 1440 when we override a function in a shared object: that the 1441 references in the shared object will be mapped to the 1442 definition in the regular object. */ 1443 1444 while (hi->root.type == bfd_link_hash_indirect 1445 || hi->root.type == bfd_link_hash_warning) 1446 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1447 1448 h->root.type = bfd_link_hash_indirect; 1449 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1450 if (h->def_dynamic) 1451 { 1452 h->def_dynamic = 0; 1453 hi->ref_dynamic = 1; 1454 if (hi->ref_regular 1455 || hi->def_regular) 1456 { 1457 if (! bfd_elf_link_record_dynamic_symbol (info, hi)) 1458 return FALSE; 1459 } 1460 } 1461 1462 /* Now set HI to H, so that the following code will set the 1463 other fields correctly. */ 1464 hi = h; 1465 } 1466 1467 /* If there is a duplicate definition somewhere, then HI may not 1468 point to an indirect symbol. We will have reported an error to 1469 the user in that case. */ 1470 1471 if (hi->root.type == bfd_link_hash_indirect) 1472 { 1473 struct elf_link_hash_entry *ht; 1474 1475 ht = (struct elf_link_hash_entry *) hi->root.u.i.link; 1476 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); 1477 1478 /* See if the new flags lead us to realize that the symbol must 1479 be dynamic. */ 1480 if (! *dynsym) 1481 { 1482 if (! dynamic) 1483 { 1484 if (info->shared 1485 || hi->ref_dynamic) 1486 *dynsym = TRUE; 1487 } 1488 else 1489 { 1490 if (hi->ref_regular) 1491 *dynsym = TRUE; 1492 } 1493 } 1494 } 1495 1496 /* We also need to define an indirection from the nondefault version 1497 of the symbol. */ 1498 1499 nondefault: 1500 len = strlen (name); 1501 shortname = bfd_hash_allocate (&info->hash->table, len); 1502 if (shortname == NULL) 1503 return FALSE; 1504 memcpy (shortname, name, shortlen); 1505 memcpy (shortname + shortlen, p + 1, len - shortlen); 1506 1507 /* Once again, merge with any existing symbol. */ 1508 type_change_ok = FALSE; 1509 size_change_ok = FALSE; 1510 sec = *psec; 1511 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, 1512 NULL, &hi, &skip, &override, 1513 &type_change_ok, &size_change_ok)) 1514 return FALSE; 1515 1516 if (skip) 1517 return TRUE; 1518 1519 if (override) 1520 { 1521 /* Here SHORTNAME is a versioned name, so we don't expect to see 1522 the type of override we do in the case above unless it is 1523 overridden by a versioned definition. */ 1524 if (hi->root.type != bfd_link_hash_defined 1525 && hi->root.type != bfd_link_hash_defweak) 1526 (*_bfd_error_handler) 1527 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), 1528 abfd, shortname); 1529 } 1530 else 1531 { 1532 bh = &hi->root; 1533 if (! (_bfd_generic_link_add_one_symbol 1534 (info, abfd, shortname, BSF_INDIRECT, 1535 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) 1536 return FALSE; 1537 hi = (struct elf_link_hash_entry *) bh; 1538 1539 /* If there is a duplicate definition somewhere, then HI may not 1540 point to an indirect symbol. We will have reported an error 1541 to the user in that case. */ 1542 1543 if (hi->root.type == bfd_link_hash_indirect) 1544 { 1545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 1546 1547 /* See if the new flags lead us to realize that the symbol 1548 must be dynamic. */ 1549 if (! *dynsym) 1550 { 1551 if (! dynamic) 1552 { 1553 if (info->shared 1554 || hi->ref_dynamic) 1555 *dynsym = TRUE; 1556 } 1557 else 1558 { 1559 if (hi->ref_regular) 1560 *dynsym = TRUE; 1561 } 1562 } 1563 } 1564 } 1565 1566 return TRUE; 1567 } 1568 1569 /* This routine is used to export all defined symbols into the dynamic 1570 symbol table. It is called via elf_link_hash_traverse. */ 1571 1572 bfd_boolean 1573 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) 1574 { 1575 struct elf_info_failed *eif = data; 1576 1577 /* Ignore indirect symbols. These are added by the versioning code. */ 1578 if (h->root.type == bfd_link_hash_indirect) 1579 return TRUE; 1580 1581 if (h->root.type == bfd_link_hash_warning) 1582 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1583 1584 if (h->dynindx == -1 1585 && (h->def_regular 1586 || h->ref_regular)) 1587 { 1588 struct bfd_elf_version_tree *t; 1589 struct bfd_elf_version_expr *d; 1590 1591 for (t = eif->verdefs; t != NULL; t = t->next) 1592 { 1593 if (t->globals.list != NULL) 1594 { 1595 d = (*t->match) (&t->globals, NULL, h->root.root.string); 1596 if (d != NULL) 1597 goto doit; 1598 } 1599 1600 if (t->locals.list != NULL) 1601 { 1602 d = (*t->match) (&t->locals, NULL, h->root.root.string); 1603 if (d != NULL) 1604 return TRUE; 1605 } 1606 } 1607 1608 if (!eif->verdefs) 1609 { 1610 doit: 1611 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 1612 { 1613 eif->failed = TRUE; 1614 return FALSE; 1615 } 1616 } 1617 } 1618 1619 return TRUE; 1620 } 1621 1622 /* Look through the symbols which are defined in other shared 1623 libraries and referenced here. Update the list of version 1624 dependencies. This will be put into the .gnu.version_r section. 1625 This function is called via elf_link_hash_traverse. */ 1626 1627 bfd_boolean 1628 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, 1629 void *data) 1630 { 1631 struct elf_find_verdep_info *rinfo = data; 1632 Elf_Internal_Verneed *t; 1633 Elf_Internal_Vernaux *a; 1634 bfd_size_type amt; 1635 1636 if (h->root.type == bfd_link_hash_warning) 1637 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1638 1639 /* We only care about symbols defined in shared objects with version 1640 information. */ 1641 if (!h->def_dynamic 1642 || h->def_regular 1643 || h->dynindx == -1 1644 || h->verinfo.verdef == NULL) 1645 return TRUE; 1646 1647 /* See if we already know about this version. */ 1648 for (t = elf_tdata (rinfo->output_bfd)->verref; t != NULL; t = t->vn_nextref) 1649 { 1650 if (t->vn_bfd != h->verinfo.verdef->vd_bfd) 1651 continue; 1652 1653 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1654 if (a->vna_nodename == h->verinfo.verdef->vd_nodename) 1655 return TRUE; 1656 1657 break; 1658 } 1659 1660 /* This is a new version. Add it to tree we are building. */ 1661 1662 if (t == NULL) 1663 { 1664 amt = sizeof *t; 1665 t = bfd_zalloc (rinfo->output_bfd, amt); 1666 if (t == NULL) 1667 { 1668 rinfo->failed = TRUE; 1669 return FALSE; 1670 } 1671 1672 t->vn_bfd = h->verinfo.verdef->vd_bfd; 1673 t->vn_nextref = elf_tdata (rinfo->output_bfd)->verref; 1674 elf_tdata (rinfo->output_bfd)->verref = t; 1675 } 1676 1677 amt = sizeof *a; 1678 a = bfd_zalloc (rinfo->output_bfd, amt); 1679 1680 /* Note that we are copying a string pointer here, and testing it 1681 above. If bfd_elf_string_from_elf_section is ever changed to 1682 discard the string data when low in memory, this will have to be 1683 fixed. */ 1684 a->vna_nodename = h->verinfo.verdef->vd_nodename; 1685 1686 a->vna_flags = h->verinfo.verdef->vd_flags; 1687 a->vna_nextptr = t->vn_auxptr; 1688 1689 h->verinfo.verdef->vd_exp_refno = rinfo->vers; 1690 ++rinfo->vers; 1691 1692 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; 1693 1694 t->vn_auxptr = a; 1695 1696 return TRUE; 1697 } 1698 1699 /* Figure out appropriate versions for all the symbols. We may not 1700 have the version number script until we have read all of the input 1701 files, so until that point we don't know which symbols should be 1702 local. This function is called via elf_link_hash_traverse. */ 1703 1704 bfd_boolean 1705 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) 1706 { 1707 struct elf_assign_sym_version_info *sinfo; 1708 struct bfd_link_info *info; 1709 const struct elf_backend_data *bed; 1710 struct elf_info_failed eif; 1711 char *p; 1712 bfd_size_type amt; 1713 1714 sinfo = data; 1715 info = sinfo->info; 1716 1717 if (h->root.type == bfd_link_hash_warning) 1718 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1719 1720 /* Fix the symbol flags. */ 1721 eif.failed = FALSE; 1722 eif.info = info; 1723 if (! _bfd_elf_fix_symbol_flags (h, &eif)) 1724 { 1725 if (eif.failed) 1726 sinfo->failed = TRUE; 1727 return FALSE; 1728 } 1729 1730 /* We only need version numbers for symbols defined in regular 1731 objects. */ 1732 if (!h->def_regular) 1733 return TRUE; 1734 1735 bed = get_elf_backend_data (sinfo->output_bfd); 1736 p = strchr (h->root.root.string, ELF_VER_CHR); 1737 if (p != NULL && h->verinfo.vertree == NULL) 1738 { 1739 struct bfd_elf_version_tree *t; 1740 bfd_boolean hidden; 1741 1742 hidden = TRUE; 1743 1744 /* There are two consecutive ELF_VER_CHR characters if this is 1745 not a hidden symbol. */ 1746 ++p; 1747 if (*p == ELF_VER_CHR) 1748 { 1749 hidden = FALSE; 1750 ++p; 1751 } 1752 1753 /* If there is no version string, we can just return out. */ 1754 if (*p == '\0') 1755 { 1756 if (hidden) 1757 h->hidden = 1; 1758 return TRUE; 1759 } 1760 1761 /* Look for the version. If we find it, it is no longer weak. */ 1762 for (t = sinfo->verdefs; t != NULL; t = t->next) 1763 { 1764 if (strcmp (t->name, p) == 0) 1765 { 1766 size_t len; 1767 char *alc; 1768 struct bfd_elf_version_expr *d; 1769 1770 len = p - h->root.root.string; 1771 alc = bfd_malloc (len); 1772 if (alc == NULL) 1773 return FALSE; 1774 memcpy (alc, h->root.root.string, len - 1); 1775 alc[len - 1] = '\0'; 1776 if (alc[len - 2] == ELF_VER_CHR) 1777 alc[len - 2] = '\0'; 1778 1779 h->verinfo.vertree = t; 1780 t->used = TRUE; 1781 d = NULL; 1782 1783 if (t->globals.list != NULL) 1784 d = (*t->match) (&t->globals, NULL, alc); 1785 1786 /* See if there is anything to force this symbol to 1787 local scope. */ 1788 if (d == NULL && t->locals.list != NULL) 1789 { 1790 d = (*t->match) (&t->locals, NULL, alc); 1791 if (d != NULL 1792 && h->dynindx != -1 1793 && ! info->export_dynamic) 1794 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1795 } 1796 1797 free (alc); 1798 break; 1799 } 1800 } 1801 1802 /* If we are building an application, we need to create a 1803 version node for this version. */ 1804 if (t == NULL && info->executable) 1805 { 1806 struct bfd_elf_version_tree **pp; 1807 int version_index; 1808 1809 /* If we aren't going to export this symbol, we don't need 1810 to worry about it. */ 1811 if (h->dynindx == -1) 1812 return TRUE; 1813 1814 amt = sizeof *t; 1815 t = bfd_zalloc (sinfo->output_bfd, amt); 1816 if (t == NULL) 1817 { 1818 sinfo->failed = TRUE; 1819 return FALSE; 1820 } 1821 1822 t->name = p; 1823 t->name_indx = (unsigned int) -1; 1824 t->used = TRUE; 1825 1826 version_index = 1; 1827 /* Don't count anonymous version tag. */ 1828 if (sinfo->verdefs != NULL && sinfo->verdefs->vernum == 0) 1829 version_index = 0; 1830 for (pp = &sinfo->verdefs; *pp != NULL; pp = &(*pp)->next) 1831 ++version_index; 1832 t->vernum = version_index; 1833 1834 *pp = t; 1835 1836 h->verinfo.vertree = t; 1837 } 1838 else if (t == NULL) 1839 { 1840 /* We could not find the version for a symbol when 1841 generating a shared archive. Return an error. */ 1842 (*_bfd_error_handler) 1843 (_("%B: undefined versioned symbol name %s"), 1844 sinfo->output_bfd, h->root.root.string); 1845 bfd_set_error (bfd_error_bad_value); 1846 sinfo->failed = TRUE; 1847 return FALSE; 1848 } 1849 1850 if (hidden) 1851 h->hidden = 1; 1852 } 1853 1854 /* If we don't have a version for this symbol, see if we can find 1855 something. */ 1856 if (h->verinfo.vertree == NULL && sinfo->verdefs != NULL) 1857 { 1858 struct bfd_elf_version_tree *t; 1859 struct bfd_elf_version_tree *local_ver; 1860 struct bfd_elf_version_expr *d; 1861 1862 /* See if can find what version this symbol is in. If the 1863 symbol is supposed to be local, then don't actually register 1864 it. */ 1865 local_ver = NULL; 1866 for (t = sinfo->verdefs; t != NULL; t = t->next) 1867 { 1868 if (t->globals.list != NULL) 1869 { 1870 bfd_boolean matched; 1871 1872 matched = FALSE; 1873 d = NULL; 1874 while ((d = (*t->match) (&t->globals, d, 1875 h->root.root.string)) != NULL) 1876 if (d->symver) 1877 matched = TRUE; 1878 else 1879 { 1880 /* There is a version without definition. Make 1881 the symbol the default definition for this 1882 version. */ 1883 h->verinfo.vertree = t; 1884 local_ver = NULL; 1885 d->script = 1; 1886 break; 1887 } 1888 if (d != NULL) 1889 break; 1890 else if (matched) 1891 /* There is no undefined version for this symbol. Hide the 1892 default one. */ 1893 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1894 } 1895 1896 if (t->locals.list != NULL) 1897 { 1898 d = NULL; 1899 while ((d = (*t->match) (&t->locals, d, 1900 h->root.root.string)) != NULL) 1901 { 1902 local_ver = t; 1903 /* If the match is "*", keep looking for a more 1904 explicit, perhaps even global, match. 1905 XXX: Shouldn't this be !d->wildcard instead? */ 1906 if (d->pattern[0] != '*' || d->pattern[1] != '\0') 1907 break; 1908 } 1909 1910 if (d != NULL) 1911 break; 1912 } 1913 } 1914 1915 if (local_ver != NULL) 1916 { 1917 h->verinfo.vertree = local_ver; 1918 if (h->dynindx != -1 1919 && ! info->export_dynamic) 1920 { 1921 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1922 } 1923 } 1924 } 1925 1926 return TRUE; 1927 } 1928 1929 /* Read and swap the relocs from the section indicated by SHDR. This 1930 may be either a REL or a RELA section. The relocations are 1931 translated into RELA relocations and stored in INTERNAL_RELOCS, 1932 which should have already been allocated to contain enough space. 1933 The EXTERNAL_RELOCS are a buffer where the external form of the 1934 relocations should be stored. 1935 1936 Returns FALSE if something goes wrong. */ 1937 1938 static bfd_boolean 1939 elf_link_read_relocs_from_section (bfd *abfd, 1940 asection *sec, 1941 Elf_Internal_Shdr *shdr, 1942 void *external_relocs, 1943 Elf_Internal_Rela *internal_relocs) 1944 { 1945 const struct elf_backend_data *bed; 1946 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 1947 const bfd_byte *erela; 1948 const bfd_byte *erelaend; 1949 Elf_Internal_Rela *irela; 1950 Elf_Internal_Shdr *symtab_hdr; 1951 size_t nsyms; 1952 1953 /* Position ourselves at the start of the section. */ 1954 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) 1955 return FALSE; 1956 1957 /* Read the relocations. */ 1958 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) 1959 return FALSE; 1960 1961 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1962 nsyms = symtab_hdr->sh_size / symtab_hdr->sh_entsize; 1963 1964 bed = get_elf_backend_data (abfd); 1965 1966 /* Convert the external relocations to the internal format. */ 1967 if (shdr->sh_entsize == bed->s->sizeof_rel) 1968 swap_in = bed->s->swap_reloc_in; 1969 else if (shdr->sh_entsize == bed->s->sizeof_rela) 1970 swap_in = bed->s->swap_reloca_in; 1971 else 1972 { 1973 bfd_set_error (bfd_error_wrong_format); 1974 return FALSE; 1975 } 1976 1977 erela = external_relocs; 1978 erelaend = erela + shdr->sh_size; 1979 irela = internal_relocs; 1980 while (erela < erelaend) 1981 { 1982 bfd_vma r_symndx; 1983 1984 (*swap_in) (abfd, erela, irela); 1985 r_symndx = ELF32_R_SYM (irela->r_info); 1986 if (bed->s->arch_size == 64) 1987 r_symndx >>= 24; 1988 if ((size_t) r_symndx >= nsyms) 1989 { 1990 (*_bfd_error_handler) 1991 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" 1992 " for offset 0x%lx in section `%A'"), 1993 abfd, sec, 1994 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); 1995 bfd_set_error (bfd_error_bad_value); 1996 return FALSE; 1997 } 1998 irela += bed->s->int_rels_per_ext_rel; 1999 erela += shdr->sh_entsize; 2000 } 2001 2002 return TRUE; 2003 } 2004 2005 /* Read and swap the relocs for a section O. They may have been 2006 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are 2007 not NULL, they are used as buffers to read into. They are known to 2008 be large enough. If the INTERNAL_RELOCS relocs argument is NULL, 2009 the return value is allocated using either malloc or bfd_alloc, 2010 according to the KEEP_MEMORY argument. If O has two relocation 2011 sections (both REL and RELA relocations), then the REL_HDR 2012 relocations will appear first in INTERNAL_RELOCS, followed by the 2013 REL_HDR2 relocations. */ 2014 2015 Elf_Internal_Rela * 2016 _bfd_elf_link_read_relocs (bfd *abfd, 2017 asection *o, 2018 void *external_relocs, 2019 Elf_Internal_Rela *internal_relocs, 2020 bfd_boolean keep_memory) 2021 { 2022 Elf_Internal_Shdr *rel_hdr; 2023 void *alloc1 = NULL; 2024 Elf_Internal_Rela *alloc2 = NULL; 2025 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2026 2027 if (elf_section_data (o)->relocs != NULL) 2028 return elf_section_data (o)->relocs; 2029 2030 if (o->reloc_count == 0) 2031 return NULL; 2032 2033 rel_hdr = &elf_section_data (o)->rel_hdr; 2034 2035 if (internal_relocs == NULL) 2036 { 2037 bfd_size_type size; 2038 2039 size = o->reloc_count; 2040 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); 2041 if (keep_memory) 2042 internal_relocs = bfd_alloc (abfd, size); 2043 else 2044 internal_relocs = alloc2 = bfd_malloc (size); 2045 if (internal_relocs == NULL) 2046 goto error_return; 2047 } 2048 2049 if (external_relocs == NULL) 2050 { 2051 bfd_size_type size = rel_hdr->sh_size; 2052 2053 if (elf_section_data (o)->rel_hdr2) 2054 size += elf_section_data (o)->rel_hdr2->sh_size; 2055 alloc1 = bfd_malloc (size); 2056 if (alloc1 == NULL) 2057 goto error_return; 2058 external_relocs = alloc1; 2059 } 2060 2061 if (!elf_link_read_relocs_from_section (abfd, o, rel_hdr, 2062 external_relocs, 2063 internal_relocs)) 2064 goto error_return; 2065 if (elf_section_data (o)->rel_hdr2 2066 && (!elf_link_read_relocs_from_section 2067 (abfd, o, 2068 elf_section_data (o)->rel_hdr2, 2069 ((bfd_byte *) external_relocs) + rel_hdr->sh_size, 2070 internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr) 2071 * bed->s->int_rels_per_ext_rel)))) 2072 goto error_return; 2073 2074 /* Cache the results for next time, if we can. */ 2075 if (keep_memory) 2076 elf_section_data (o)->relocs = internal_relocs; 2077 2078 if (alloc1 != NULL) 2079 free (alloc1); 2080 2081 /* Don't free alloc2, since if it was allocated we are passing it 2082 back (under the name of internal_relocs). */ 2083 2084 return internal_relocs; 2085 2086 error_return: 2087 if (alloc1 != NULL) 2088 free (alloc1); 2089 if (alloc2 != NULL) 2090 free (alloc2); 2091 return NULL; 2092 } 2093 2094 /* Compute the size of, and allocate space for, REL_HDR which is the 2095 section header for a section containing relocations for O. */ 2096 2097 bfd_boolean 2098 _bfd_elf_link_size_reloc_section (bfd *abfd, 2099 Elf_Internal_Shdr *rel_hdr, 2100 asection *o) 2101 { 2102 bfd_size_type reloc_count; 2103 bfd_size_type num_rel_hashes; 2104 2105 /* Figure out how many relocations there will be. */ 2106 if (rel_hdr == &elf_section_data (o)->rel_hdr) 2107 reloc_count = elf_section_data (o)->rel_count; 2108 else 2109 reloc_count = elf_section_data (o)->rel_count2; 2110 2111 num_rel_hashes = o->reloc_count; 2112 if (num_rel_hashes < reloc_count) 2113 num_rel_hashes = reloc_count; 2114 2115 /* That allows us to calculate the size of the section. */ 2116 rel_hdr->sh_size = rel_hdr->sh_entsize * reloc_count; 2117 2118 /* The contents field must last into write_object_contents, so we 2119 allocate it with bfd_alloc rather than malloc. Also since we 2120 cannot be sure that the contents will actually be filled in, 2121 we zero the allocated space. */ 2122 rel_hdr->contents = bfd_zalloc (abfd, rel_hdr->sh_size); 2123 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) 2124 return FALSE; 2125 2126 /* We only allocate one set of hash entries, so we only do it the 2127 first time we are called. */ 2128 if (elf_section_data (o)->rel_hashes == NULL 2129 && num_rel_hashes) 2130 { 2131 struct elf_link_hash_entry **p; 2132 2133 p = bfd_zmalloc (num_rel_hashes * sizeof (struct elf_link_hash_entry *)); 2134 if (p == NULL) 2135 return FALSE; 2136 2137 elf_section_data (o)->rel_hashes = p; 2138 } 2139 2140 return TRUE; 2141 } 2142 2143 /* Copy the relocations indicated by the INTERNAL_RELOCS (which 2144 originated from the section given by INPUT_REL_HDR) to the 2145 OUTPUT_BFD. */ 2146 2147 bfd_boolean 2148 _bfd_elf_link_output_relocs (bfd *output_bfd, 2149 asection *input_section, 2150 Elf_Internal_Shdr *input_rel_hdr, 2151 Elf_Internal_Rela *internal_relocs, 2152 struct elf_link_hash_entry **rel_hash 2153 ATTRIBUTE_UNUSED) 2154 { 2155 Elf_Internal_Rela *irela; 2156 Elf_Internal_Rela *irelaend; 2157 bfd_byte *erel; 2158 Elf_Internal_Shdr *output_rel_hdr; 2159 asection *output_section; 2160 unsigned int *rel_countp = NULL; 2161 const struct elf_backend_data *bed; 2162 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 2163 2164 output_section = input_section->output_section; 2165 output_rel_hdr = NULL; 2166 2167 if (elf_section_data (output_section)->rel_hdr.sh_entsize 2168 == input_rel_hdr->sh_entsize) 2169 { 2170 output_rel_hdr = &elf_section_data (output_section)->rel_hdr; 2171 rel_countp = &elf_section_data (output_section)->rel_count; 2172 } 2173 else if (elf_section_data (output_section)->rel_hdr2 2174 && (elf_section_data (output_section)->rel_hdr2->sh_entsize 2175 == input_rel_hdr->sh_entsize)) 2176 { 2177 output_rel_hdr = elf_section_data (output_section)->rel_hdr2; 2178 rel_countp = &elf_section_data (output_section)->rel_count2; 2179 } 2180 else 2181 { 2182 (*_bfd_error_handler) 2183 (_("%B: relocation size mismatch in %B section %A"), 2184 output_bfd, input_section->owner, input_section); 2185 bfd_set_error (bfd_error_wrong_object_format); 2186 return FALSE; 2187 } 2188 2189 bed = get_elf_backend_data (output_bfd); 2190 if (input_rel_hdr->sh_entsize == bed->s->sizeof_rel) 2191 swap_out = bed->s->swap_reloc_out; 2192 else if (input_rel_hdr->sh_entsize == bed->s->sizeof_rela) 2193 swap_out = bed->s->swap_reloca_out; 2194 else 2195 abort (); 2196 2197 erel = output_rel_hdr->contents; 2198 erel += *rel_countp * input_rel_hdr->sh_entsize; 2199 irela = internal_relocs; 2200 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) 2201 * bed->s->int_rels_per_ext_rel); 2202 while (irela < irelaend) 2203 { 2204 (*swap_out) (output_bfd, irela, erel); 2205 irela += bed->s->int_rels_per_ext_rel; 2206 erel += input_rel_hdr->sh_entsize; 2207 } 2208 2209 /* Bump the counter, so that we know where to add the next set of 2210 relocations. */ 2211 *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr); 2212 2213 return TRUE; 2214 } 2215 2216 /* Make weak undefined symbols in PIE dynamic. */ 2217 2218 bfd_boolean 2219 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, 2220 struct elf_link_hash_entry *h) 2221 { 2222 if (info->pie 2223 && h->dynindx == -1 2224 && h->root.type == bfd_link_hash_undefweak) 2225 return bfd_elf_link_record_dynamic_symbol (info, h); 2226 2227 return TRUE; 2228 } 2229 2230 /* Fix up the flags for a symbol. This handles various cases which 2231 can only be fixed after all the input files are seen. This is 2232 currently called by both adjust_dynamic_symbol and 2233 assign_sym_version, which is unnecessary but perhaps more robust in 2234 the face of future changes. */ 2235 2236 bfd_boolean 2237 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, 2238 struct elf_info_failed *eif) 2239 { 2240 const struct elf_backend_data *bed = NULL; 2241 2242 /* If this symbol was mentioned in a non-ELF file, try to set 2243 DEF_REGULAR and REF_REGULAR correctly. This is the only way to 2244 permit a non-ELF file to correctly refer to a symbol defined in 2245 an ELF dynamic object. */ 2246 if (h->non_elf) 2247 { 2248 while (h->root.type == bfd_link_hash_indirect) 2249 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2250 2251 if (h->root.type != bfd_link_hash_defined 2252 && h->root.type != bfd_link_hash_defweak) 2253 { 2254 h->ref_regular = 1; 2255 h->ref_regular_nonweak = 1; 2256 } 2257 else 2258 { 2259 if (h->root.u.def.section->owner != NULL 2260 && (bfd_get_flavour (h->root.u.def.section->owner) 2261 == bfd_target_elf_flavour)) 2262 { 2263 h->ref_regular = 1; 2264 h->ref_regular_nonweak = 1; 2265 } 2266 else 2267 h->def_regular = 1; 2268 } 2269 2270 if (h->dynindx == -1 2271 && (h->def_dynamic 2272 || h->ref_dynamic)) 2273 { 2274 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2275 { 2276 eif->failed = TRUE; 2277 return FALSE; 2278 } 2279 } 2280 } 2281 else 2282 { 2283 /* Unfortunately, NON_ELF is only correct if the symbol 2284 was first seen in a non-ELF file. Fortunately, if the symbol 2285 was first seen in an ELF file, we're probably OK unless the 2286 symbol was defined in a non-ELF file. Catch that case here. 2287 FIXME: We're still in trouble if the symbol was first seen in 2288 a dynamic object, and then later in a non-ELF regular object. */ 2289 if ((h->root.type == bfd_link_hash_defined 2290 || h->root.type == bfd_link_hash_defweak) 2291 && !h->def_regular 2292 && (h->root.u.def.section->owner != NULL 2293 ? (bfd_get_flavour (h->root.u.def.section->owner) 2294 != bfd_target_elf_flavour) 2295 : (bfd_is_abs_section (h->root.u.def.section) 2296 && !h->def_dynamic))) 2297 h->def_regular = 1; 2298 } 2299 2300 /* Backend specific symbol fixup. */ 2301 if (elf_hash_table (eif->info)->dynobj) 2302 { 2303 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 2304 if (bed->elf_backend_fixup_symbol 2305 && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) 2306 return FALSE; 2307 } 2308 2309 /* If this is a final link, and the symbol was defined as a common 2310 symbol in a regular object file, and there was no definition in 2311 any dynamic object, then the linker will have allocated space for 2312 the symbol in a common section but the DEF_REGULAR 2313 flag will not have been set. */ 2314 if (h->root.type == bfd_link_hash_defined 2315 && !h->def_regular 2316 && h->ref_regular 2317 && !h->def_dynamic 2318 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) 2319 h->def_regular = 1; 2320 2321 /* If -Bsymbolic was used (which means to bind references to global 2322 symbols to the definition within the shared object), and this 2323 symbol was defined in a regular object, then it actually doesn't 2324 need a PLT entry. Likewise, if the symbol has non-default 2325 visibility. If the symbol has hidden or internal visibility, we 2326 will force it local. */ 2327 if (h->needs_plt 2328 && eif->info->shared 2329 && is_elf_hash_table (eif->info->hash) 2330 && (eif->info->symbolic || eif->info->static_link 2331 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2332 && h->def_regular) 2333 { 2334 bfd_boolean force_local; 2335 2336 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 2337 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); 2338 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); 2339 } 2340 2341 /* If a weak undefined symbol has non-default visibility, we also 2342 hide it from the dynamic linker. */ 2343 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2344 && h->root.type == bfd_link_hash_undefweak) 2345 { 2346 const struct elf_backend_data *bed; 2347 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 2348 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2349 } 2350 2351 /* If this is a weak defined symbol in a dynamic object, and we know 2352 the real definition in the dynamic object, copy interesting flags 2353 over to the real definition. */ 2354 if (h->u.weakdef != NULL) 2355 { 2356 struct elf_link_hash_entry *weakdef; 2357 2358 weakdef = h->u.weakdef; 2359 if (h->root.type == bfd_link_hash_indirect) 2360 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2361 2362 BFD_ASSERT (h->root.type == bfd_link_hash_defined 2363 || h->root.type == bfd_link_hash_defweak); 2364 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined 2365 || weakdef->root.type == bfd_link_hash_defweak); 2366 BFD_ASSERT (weakdef->def_dynamic); 2367 2368 /* If the real definition is defined by a regular object file, 2369 don't do anything special. See the longer description in 2370 _bfd_elf_adjust_dynamic_symbol, below. */ 2371 if (weakdef->def_regular) 2372 h->u.weakdef = NULL; 2373 else 2374 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, 2375 h); 2376 } 2377 2378 return TRUE; 2379 } 2380 2381 /* Make the backend pick a good value for a dynamic symbol. This is 2382 called via elf_link_hash_traverse, and also calls itself 2383 recursively. */ 2384 2385 bfd_boolean 2386 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) 2387 { 2388 struct elf_info_failed *eif = data; 2389 bfd *dynobj; 2390 const struct elf_backend_data *bed; 2391 2392 if (! is_elf_hash_table (eif->info->hash)) 2393 return FALSE; 2394 2395 if (h->root.type == bfd_link_hash_warning) 2396 { 2397 h->got = elf_hash_table (eif->info)->init_got_offset; 2398 h->plt = elf_hash_table (eif->info)->init_plt_offset; 2399 2400 /* When warning symbols are created, they **replace** the "real" 2401 entry in the hash table, thus we never get to see the real 2402 symbol in a hash traversal. So look at it now. */ 2403 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2404 } 2405 2406 /* Ignore indirect symbols. These are added by the versioning code. */ 2407 if (h->root.type == bfd_link_hash_indirect) 2408 return TRUE; 2409 2410 /* Fix the symbol flags. */ 2411 if (! _bfd_elf_fix_symbol_flags (h, eif)) 2412 return FALSE; 2413 2414 /* If this symbol does not require a PLT entry, and it is not 2415 defined by a dynamic object, or is not referenced by a regular 2416 object, ignore it. We do have to handle a weak defined symbol, 2417 even if no regular object refers to it, if we decided to add it 2418 to the dynamic symbol table. FIXME: Do we normally need to worry 2419 about symbols which are defined by one dynamic object and 2420 referenced by another one? */ 2421 if (!h->needs_plt 2422 && (h->def_regular 2423 || !h->def_dynamic 2424 || (!h->ref_regular 2425 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) 2426 { 2427 h->plt = elf_hash_table (eif->info)->init_plt_offset; 2428 return TRUE; 2429 } 2430 2431 /* If we've already adjusted this symbol, don't do it again. This 2432 can happen via a recursive call. */ 2433 if (h->dynamic_adjusted) 2434 return TRUE; 2435 2436 /* Don't look at this symbol again. Note that we must set this 2437 after checking the above conditions, because we may look at a 2438 symbol once, decide not to do anything, and then get called 2439 recursively later after REF_REGULAR is set below. */ 2440 h->dynamic_adjusted = 1; 2441 2442 /* If this is a weak definition, and we know a real definition, and 2443 the real symbol is not itself defined by a regular object file, 2444 then get a good value for the real definition. We handle the 2445 real symbol first, for the convenience of the backend routine. 2446 2447 Note that there is a confusing case here. If the real definition 2448 is defined by a regular object file, we don't get the real symbol 2449 from the dynamic object, but we do get the weak symbol. If the 2450 processor backend uses a COPY reloc, then if some routine in the 2451 dynamic object changes the real symbol, we will not see that 2452 change in the corresponding weak symbol. This is the way other 2453 ELF linkers work as well, and seems to be a result of the shared 2454 library model. 2455 2456 I will clarify this issue. Most SVR4 shared libraries define the 2457 variable _timezone and define timezone as a weak synonym. The 2458 tzset call changes _timezone. If you write 2459 extern int timezone; 2460 int _timezone = 5; 2461 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } 2462 you might expect that, since timezone is a synonym for _timezone, 2463 the same number will print both times. However, if the processor 2464 backend uses a COPY reloc, then actually timezone will be copied 2465 into your process image, and, since you define _timezone 2466 yourself, _timezone will not. Thus timezone and _timezone will 2467 wind up at different memory locations. The tzset call will set 2468 _timezone, leaving timezone unchanged. */ 2469 2470 if (h->u.weakdef != NULL) 2471 { 2472 /* If we get to this point, we know there is an implicit 2473 reference by a regular object file via the weak symbol H. 2474 FIXME: Is this really true? What if the traversal finds 2475 H->U.WEAKDEF before it finds H? */ 2476 h->u.weakdef->ref_regular = 1; 2477 2478 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) 2479 return FALSE; 2480 } 2481 2482 /* If a symbol has no type and no size and does not require a PLT 2483 entry, then we are probably about to do the wrong thing here: we 2484 are probably going to create a COPY reloc for an empty object. 2485 This case can arise when a shared object is built with assembly 2486 code, and the assembly code fails to set the symbol type. */ 2487 if (h->size == 0 2488 && h->type == STT_NOTYPE 2489 && !h->needs_plt) 2490 (*_bfd_error_handler) 2491 (_("warning: type and size of dynamic symbol `%s' are not defined"), 2492 h->root.root.string); 2493 2494 dynobj = elf_hash_table (eif->info)->dynobj; 2495 bed = get_elf_backend_data (dynobj); 2496 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) 2497 { 2498 eif->failed = TRUE; 2499 return FALSE; 2500 } 2501 2502 return TRUE; 2503 } 2504 2505 /* Adjust all external symbols pointing into SEC_MERGE sections 2506 to reflect the object merging within the sections. */ 2507 2508 bfd_boolean 2509 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) 2510 { 2511 asection *sec; 2512 2513 if (h->root.type == bfd_link_hash_warning) 2514 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2515 2516 if ((h->root.type == bfd_link_hash_defined 2517 || h->root.type == bfd_link_hash_defweak) 2518 && ((sec = h->root.u.def.section)->flags & SEC_MERGE) 2519 && sec->sec_info_type == ELF_INFO_TYPE_MERGE) 2520 { 2521 bfd *output_bfd = data; 2522 2523 h->root.u.def.value = 2524 _bfd_merged_section_offset (output_bfd, 2525 &h->root.u.def.section, 2526 elf_section_data (sec)->sec_info, 2527 h->root.u.def.value); 2528 } 2529 2530 return TRUE; 2531 } 2532 2533 /* Returns false if the symbol referred to by H should be considered 2534 to resolve local to the current module, and true if it should be 2535 considered to bind dynamically. */ 2536 2537 bfd_boolean 2538 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, 2539 struct bfd_link_info *info, 2540 bfd_boolean ignore_protected) 2541 { 2542 bfd_boolean binding_stays_local_p; 2543 2544 if (h == NULL) 2545 return FALSE; 2546 2547 while (h->root.type == bfd_link_hash_indirect 2548 || h->root.type == bfd_link_hash_warning) 2549 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2550 2551 /* If it was forced local, then clearly it's not dynamic. */ 2552 if (h->dynindx == -1) 2553 return FALSE; 2554 if (h->forced_local) 2555 return FALSE; 2556 2557 /* Identify the cases where name binding rules say that a 2558 visible symbol resolves locally. */ 2559 binding_stays_local_p = info->executable || info->symbolic; 2560 2561 switch (ELF_ST_VISIBILITY (h->other)) 2562 { 2563 case STV_INTERNAL: 2564 case STV_HIDDEN: 2565 return FALSE; 2566 2567 case STV_PROTECTED: 2568 /* Proper resolution for function pointer equality may require 2569 that these symbols perhaps be resolved dynamically, even though 2570 we should be resolving them to the current module. */ 2571 if (!ignore_protected || h->type != STT_FUNC) 2572 binding_stays_local_p = TRUE; 2573 break; 2574 2575 default: 2576 break; 2577 } 2578 2579 /* If it isn't defined locally, then clearly it's dynamic. */ 2580 if (!h->def_regular) 2581 return TRUE; 2582 2583 /* Otherwise, the symbol is dynamic if binding rules don't tell 2584 us that it remains local. */ 2585 return !binding_stays_local_p; 2586 } 2587 2588 /* Return true if the symbol referred to by H should be considered 2589 to resolve local to the current module, and false otherwise. Differs 2590 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of 2591 undefined symbols and weak symbols. */ 2592 2593 bfd_boolean 2594 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, 2595 struct bfd_link_info *info, 2596 bfd_boolean local_protected) 2597 { 2598 /* If it's a local sym, of course we resolve locally. */ 2599 if (h == NULL) 2600 return TRUE; 2601 2602 /* Common symbols that become definitions don't get the DEF_REGULAR 2603 flag set, so test it first, and don't bail out. */ 2604 if (ELF_COMMON_DEF_P (h)) 2605 /* Do nothing. */; 2606 /* If we don't have a definition in a regular file, then we can't 2607 resolve locally. The sym is either undefined or dynamic. */ 2608 else if (!h->def_regular) 2609 return FALSE; 2610 2611 /* Forced local symbols resolve locally. */ 2612 if (h->forced_local) 2613 return TRUE; 2614 2615 /* As do non-dynamic symbols. */ 2616 if (h->dynindx == -1) 2617 return TRUE; 2618 2619 /* At this point, we know the symbol is defined and dynamic. In an 2620 executable it must resolve locally, likewise when building symbolic 2621 shared libraries. */ 2622 if (info->executable || info->symbolic) 2623 return TRUE; 2624 2625 /* Now deal with defined dynamic symbols in shared libraries. Ones 2626 with default visibility might not resolve locally. */ 2627 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 2628 return FALSE; 2629 2630 /* However, STV_HIDDEN or STV_INTERNAL ones must be local. */ 2631 if (ELF_ST_VISIBILITY (h->other) != STV_PROTECTED) 2632 return TRUE; 2633 2634 /* STV_PROTECTED non-function symbols are local. */ 2635 if (h->type != STT_FUNC) 2636 return TRUE; 2637 2638 /* Function pointer equality tests may require that STV_PROTECTED 2639 symbols be treated as dynamic symbols, even when we know that the 2640 dynamic linker will resolve them locally. */ 2641 return local_protected; 2642 } 2643 2644 /* Caches some TLS segment info, and ensures that the TLS segment vma is 2645 aligned. Returns the first TLS output section. */ 2646 2647 struct bfd_section * 2648 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) 2649 { 2650 struct bfd_section *sec, *tls; 2651 unsigned int align = 0; 2652 2653 for (sec = obfd->sections; sec != NULL; sec = sec->next) 2654 if ((sec->flags & SEC_THREAD_LOCAL) != 0) 2655 break; 2656 tls = sec; 2657 2658 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) 2659 if (sec->alignment_power > align) 2660 align = sec->alignment_power; 2661 2662 elf_hash_table (info)->tls_sec = tls; 2663 2664 /* Ensure the alignment of the first section is the largest alignment, 2665 so that the tls segment starts aligned. */ 2666 if (tls != NULL) 2667 tls->alignment_power = align; 2668 2669 return tls; 2670 } 2671 2672 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ 2673 static bfd_boolean 2674 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, 2675 Elf_Internal_Sym *sym) 2676 { 2677 const struct elf_backend_data *bed; 2678 2679 /* Local symbols do not count, but target specific ones might. */ 2680 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL 2681 && ELF_ST_BIND (sym->st_info) < STB_LOOS) 2682 return FALSE; 2683 2684 /* Function symbols do not count. */ 2685 if (ELF_ST_TYPE (sym->st_info) == STT_FUNC) 2686 return FALSE; 2687 2688 /* If the section is undefined, then so is the symbol. */ 2689 if (sym->st_shndx == SHN_UNDEF) 2690 return FALSE; 2691 2692 /* If the symbol is defined in the common section, then 2693 it is a common definition and so does not count. */ 2694 bed = get_elf_backend_data (abfd); 2695 if (bed->common_definition (sym)) 2696 return FALSE; 2697 2698 /* If the symbol is in a target specific section then we 2699 must rely upon the backend to tell us what it is. */ 2700 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) 2701 /* FIXME - this function is not coded yet: 2702 2703 return _bfd_is_global_symbol_definition (abfd, sym); 2704 2705 Instead for now assume that the definition is not global, 2706 Even if this is wrong, at least the linker will behave 2707 in the same way that it used to do. */ 2708 return FALSE; 2709 2710 return TRUE; 2711 } 2712 2713 /* Search the symbol table of the archive element of the archive ABFD 2714 whose archive map contains a mention of SYMDEF, and determine if 2715 the symbol is defined in this element. */ 2716 static bfd_boolean 2717 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) 2718 { 2719 Elf_Internal_Shdr * hdr; 2720 bfd_size_type symcount; 2721 bfd_size_type extsymcount; 2722 bfd_size_type extsymoff; 2723 Elf_Internal_Sym *isymbuf; 2724 Elf_Internal_Sym *isym; 2725 Elf_Internal_Sym *isymend; 2726 bfd_boolean result; 2727 2728 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 2729 if (abfd == NULL) 2730 return FALSE; 2731 2732 if (! bfd_check_format (abfd, bfd_object)) 2733 return FALSE; 2734 2735 /* If we have already included the element containing this symbol in the 2736 link then we do not need to include it again. Just claim that any symbol 2737 it contains is not a definition, so that our caller will not decide to 2738 (re)include this element. */ 2739 if (abfd->archive_pass) 2740 return FALSE; 2741 2742 /* Select the appropriate symbol table. */ 2743 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) 2744 hdr = &elf_tdata (abfd)->symtab_hdr; 2745 else 2746 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2747 2748 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 2749 2750 /* The sh_info field of the symtab header tells us where the 2751 external symbols start. We don't care about the local symbols. */ 2752 if (elf_bad_symtab (abfd)) 2753 { 2754 extsymcount = symcount; 2755 extsymoff = 0; 2756 } 2757 else 2758 { 2759 extsymcount = symcount - hdr->sh_info; 2760 extsymoff = hdr->sh_info; 2761 } 2762 2763 if (extsymcount == 0) 2764 return FALSE; 2765 2766 /* Read in the symbol table. */ 2767 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 2768 NULL, NULL, NULL); 2769 if (isymbuf == NULL) 2770 return FALSE; 2771 2772 /* Scan the symbol table looking for SYMDEF. */ 2773 result = FALSE; 2774 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) 2775 { 2776 const char *name; 2777 2778 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 2779 isym->st_name); 2780 if (name == NULL) 2781 break; 2782 2783 if (strcmp (name, symdef->name) == 0) 2784 { 2785 result = is_global_data_symbol_definition (abfd, isym); 2786 break; 2787 } 2788 } 2789 2790 free (isymbuf); 2791 2792 return result; 2793 } 2794 2795 /* Add an entry to the .dynamic table. */ 2796 2797 bfd_boolean 2798 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, 2799 bfd_vma tag, 2800 bfd_vma val) 2801 { 2802 struct elf_link_hash_table *hash_table; 2803 const struct elf_backend_data *bed; 2804 asection *s; 2805 bfd_size_type newsize; 2806 bfd_byte *newcontents; 2807 Elf_Internal_Dyn dyn; 2808 2809 hash_table = elf_hash_table (info); 2810 if (! is_elf_hash_table (hash_table)) 2811 return FALSE; 2812 2813 bed = get_elf_backend_data (hash_table->dynobj); 2814 s = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); 2815 BFD_ASSERT (s != NULL); 2816 2817 newsize = s->size + bed->s->sizeof_dyn; 2818 newcontents = bfd_realloc (s->contents, newsize); 2819 if (newcontents == NULL) 2820 return FALSE; 2821 2822 dyn.d_tag = tag; 2823 dyn.d_un.d_val = val; 2824 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); 2825 2826 s->size = newsize; 2827 s->contents = newcontents; 2828 2829 return TRUE; 2830 } 2831 2832 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, 2833 otherwise just check whether one already exists. Returns -1 on error, 2834 1 if a DT_NEEDED tag already exists, and 0 on success. */ 2835 2836 static int 2837 elf_add_dt_needed_tag (bfd *abfd, 2838 struct bfd_link_info *info, 2839 const char *soname, 2840 bfd_boolean do_it) 2841 { 2842 struct elf_link_hash_table *hash_table; 2843 bfd_size_type oldsize; 2844 bfd_size_type strindex; 2845 2846 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 2847 return -1; 2848 2849 hash_table = elf_hash_table (info); 2850 oldsize = _bfd_elf_strtab_size (hash_table->dynstr); 2851 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); 2852 if (strindex == (bfd_size_type) -1) 2853 return -1; 2854 2855 if (oldsize == _bfd_elf_strtab_size (hash_table->dynstr)) 2856 { 2857 asection *sdyn; 2858 const struct elf_backend_data *bed; 2859 bfd_byte *extdyn; 2860 2861 bed = get_elf_backend_data (hash_table->dynobj); 2862 sdyn = bfd_get_section_by_name (hash_table->dynobj, ".dynamic"); 2863 if (sdyn != NULL) 2864 for (extdyn = sdyn->contents; 2865 extdyn < sdyn->contents + sdyn->size; 2866 extdyn += bed->s->sizeof_dyn) 2867 { 2868 Elf_Internal_Dyn dyn; 2869 2870 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 2871 if (dyn.d_tag == DT_NEEDED 2872 && dyn.d_un.d_val == strindex) 2873 { 2874 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 2875 return 1; 2876 } 2877 } 2878 } 2879 2880 if (do_it) 2881 { 2882 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) 2883 return -1; 2884 2885 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) 2886 return -1; 2887 } 2888 else 2889 /* We were just checking for existence of the tag. */ 2890 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 2891 2892 return 0; 2893 } 2894 2895 /* Sort symbol by value and section. */ 2896 static int 2897 elf_sort_symbol (const void *arg1, const void *arg2) 2898 { 2899 const struct elf_link_hash_entry *h1; 2900 const struct elf_link_hash_entry *h2; 2901 bfd_signed_vma vdiff; 2902 2903 h1 = *(const struct elf_link_hash_entry **) arg1; 2904 h2 = *(const struct elf_link_hash_entry **) arg2; 2905 vdiff = h1->root.u.def.value - h2->root.u.def.value; 2906 if (vdiff != 0) 2907 return vdiff > 0 ? 1 : -1; 2908 else 2909 { 2910 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; 2911 if (sdiff != 0) 2912 return sdiff > 0 ? 1 : -1; 2913 } 2914 return 0; 2915 } 2916 2917 /* This function is used to adjust offsets into .dynstr for 2918 dynamic symbols. This is called via elf_link_hash_traverse. */ 2919 2920 static bfd_boolean 2921 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) 2922 { 2923 struct elf_strtab_hash *dynstr = data; 2924 2925 if (h->root.type == bfd_link_hash_warning) 2926 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2927 2928 if (h->dynindx != -1) 2929 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); 2930 return TRUE; 2931 } 2932 2933 /* Assign string offsets in .dynstr, update all structures referencing 2934 them. */ 2935 2936 static bfd_boolean 2937 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) 2938 { 2939 struct elf_link_hash_table *hash_table = elf_hash_table (info); 2940 struct elf_link_local_dynamic_entry *entry; 2941 struct elf_strtab_hash *dynstr = hash_table->dynstr; 2942 bfd *dynobj = hash_table->dynobj; 2943 asection *sdyn; 2944 bfd_size_type size; 2945 const struct elf_backend_data *bed; 2946 bfd_byte *extdyn; 2947 2948 _bfd_elf_strtab_finalize (dynstr); 2949 size = _bfd_elf_strtab_size (dynstr); 2950 2951 bed = get_elf_backend_data (dynobj); 2952 sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); 2953 BFD_ASSERT (sdyn != NULL); 2954 2955 /* Update all .dynamic entries referencing .dynstr strings. */ 2956 for (extdyn = sdyn->contents; 2957 extdyn < sdyn->contents + sdyn->size; 2958 extdyn += bed->s->sizeof_dyn) 2959 { 2960 Elf_Internal_Dyn dyn; 2961 2962 bed->s->swap_dyn_in (dynobj, extdyn, &dyn); 2963 switch (dyn.d_tag) 2964 { 2965 case DT_STRSZ: 2966 dyn.d_un.d_val = size; 2967 break; 2968 case DT_NEEDED: 2969 case DT_SONAME: 2970 case DT_RPATH: 2971 case DT_RUNPATH: 2972 case DT_FILTER: 2973 case DT_AUXILIARY: 2974 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); 2975 break; 2976 default: 2977 continue; 2978 } 2979 bed->s->swap_dyn_out (dynobj, &dyn, extdyn); 2980 } 2981 2982 /* Now update local dynamic symbols. */ 2983 for (entry = hash_table->dynlocal; entry ; entry = entry->next) 2984 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, 2985 entry->isym.st_name); 2986 2987 /* And the rest of dynamic symbols. */ 2988 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); 2989 2990 /* Adjust version definitions. */ 2991 if (elf_tdata (output_bfd)->cverdefs) 2992 { 2993 asection *s; 2994 bfd_byte *p; 2995 bfd_size_type i; 2996 Elf_Internal_Verdef def; 2997 Elf_Internal_Verdaux defaux; 2998 2999 s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); 3000 p = s->contents; 3001 do 3002 { 3003 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, 3004 &def); 3005 p += sizeof (Elf_External_Verdef); 3006 if (def.vd_aux != sizeof (Elf_External_Verdef)) 3007 continue; 3008 for (i = 0; i < def.vd_cnt; ++i) 3009 { 3010 _bfd_elf_swap_verdaux_in (output_bfd, 3011 (Elf_External_Verdaux *) p, &defaux); 3012 defaux.vda_name = _bfd_elf_strtab_offset (dynstr, 3013 defaux.vda_name); 3014 _bfd_elf_swap_verdaux_out (output_bfd, 3015 &defaux, (Elf_External_Verdaux *) p); 3016 p += sizeof (Elf_External_Verdaux); 3017 } 3018 } 3019 while (def.vd_next); 3020 } 3021 3022 /* Adjust version references. */ 3023 if (elf_tdata (output_bfd)->verref) 3024 { 3025 asection *s; 3026 bfd_byte *p; 3027 bfd_size_type i; 3028 Elf_Internal_Verneed need; 3029 Elf_Internal_Vernaux needaux; 3030 3031 s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); 3032 p = s->contents; 3033 do 3034 { 3035 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, 3036 &need); 3037 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); 3038 _bfd_elf_swap_verneed_out (output_bfd, &need, 3039 (Elf_External_Verneed *) p); 3040 p += sizeof (Elf_External_Verneed); 3041 for (i = 0; i < need.vn_cnt; ++i) 3042 { 3043 _bfd_elf_swap_vernaux_in (output_bfd, 3044 (Elf_External_Vernaux *) p, &needaux); 3045 needaux.vna_name = _bfd_elf_strtab_offset (dynstr, 3046 needaux.vna_name); 3047 _bfd_elf_swap_vernaux_out (output_bfd, 3048 &needaux, 3049 (Elf_External_Vernaux *) p); 3050 p += sizeof (Elf_External_Vernaux); 3051 } 3052 } 3053 while (need.vn_next); 3054 } 3055 3056 return TRUE; 3057 } 3058 3059 /* Add symbols from an ELF object file to the linker hash table. */ 3060 3061 static bfd_boolean 3062 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 3063 { 3064 Elf_Internal_Shdr *hdr; 3065 bfd_size_type symcount; 3066 bfd_size_type extsymcount; 3067 bfd_size_type extsymoff; 3068 struct elf_link_hash_entry **sym_hash; 3069 bfd_boolean dynamic; 3070 Elf_External_Versym *extversym = NULL; 3071 Elf_External_Versym *ever; 3072 struct elf_link_hash_entry *weaks; 3073 struct elf_link_hash_entry **nondeflt_vers = NULL; 3074 bfd_size_type nondeflt_vers_cnt = 0; 3075 Elf_Internal_Sym *isymbuf = NULL; 3076 Elf_Internal_Sym *isym; 3077 Elf_Internal_Sym *isymend; 3078 const struct elf_backend_data *bed; 3079 bfd_boolean add_needed; 3080 struct elf_link_hash_table *htab; 3081 bfd_size_type amt; 3082 void *alloc_mark = NULL; 3083 void *old_tab = NULL; 3084 void *old_hash; 3085 void *old_ent; 3086 struct bfd_link_hash_entry *old_undefs = NULL; 3087 struct bfd_link_hash_entry *old_undefs_tail = NULL; 3088 long old_dynsymcount = 0; 3089 size_t tabsize = 0; 3090 size_t hashsize = 0; 3091 3092 htab = elf_hash_table (info); 3093 bed = get_elf_backend_data (abfd); 3094 3095 if ((abfd->flags & DYNAMIC) == 0) 3096 dynamic = FALSE; 3097 else 3098 { 3099 dynamic = TRUE; 3100 3101 /* You can't use -r against a dynamic object. Also, there's no 3102 hope of using a dynamic object which does not exactly match 3103 the format of the output file. */ 3104 if (info->relocatable 3105 || !is_elf_hash_table (htab) 3106 || htab->root.creator != abfd->xvec) 3107 { 3108 if (info->relocatable) 3109 bfd_set_error (bfd_error_invalid_operation); 3110 else 3111 bfd_set_error (bfd_error_wrong_format); 3112 goto error_return; 3113 } 3114 } 3115 3116 /* As a GNU extension, any input sections which are named 3117 .gnu.warning.SYMBOL are treated as warning symbols for the given 3118 symbol. This differs from .gnu.warning sections, which generate 3119 warnings when they are included in an output file. */ 3120 if (info->executable) 3121 { 3122 asection *s; 3123 3124 for (s = abfd->sections; s != NULL; s = s->next) 3125 { 3126 const char *name; 3127 3128 name = bfd_get_section_name (abfd, s); 3129 if (strncmp (name, ".gnu.warning.", sizeof ".gnu.warning." - 1) == 0) 3130 { 3131 char *msg; 3132 bfd_size_type sz; 3133 3134 name += sizeof ".gnu.warning." - 1; 3135 3136 /* If this is a shared object, then look up the symbol 3137 in the hash table. If it is there, and it is already 3138 been defined, then we will not be using the entry 3139 from this shared object, so we don't need to warn. 3140 FIXME: If we see the definition in a regular object 3141 later on, we will warn, but we shouldn't. The only 3142 fix is to keep track of what warnings we are supposed 3143 to emit, and then handle them all at the end of the 3144 link. */ 3145 if (dynamic) 3146 { 3147 struct elf_link_hash_entry *h; 3148 3149 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 3150 3151 /* FIXME: What about bfd_link_hash_common? */ 3152 if (h != NULL 3153 && (h->root.type == bfd_link_hash_defined 3154 || h->root.type == bfd_link_hash_defweak)) 3155 { 3156 /* We don't want to issue this warning. Clobber 3157 the section size so that the warning does not 3158 get copied into the output file. */ 3159 s->size = 0; 3160 continue; 3161 } 3162 } 3163 3164 sz = s->size; 3165 msg = bfd_alloc (abfd, sz + 1); 3166 if (msg == NULL) 3167 goto error_return; 3168 3169 if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) 3170 goto error_return; 3171 3172 msg[sz] = '\0'; 3173 3174 if (! (_bfd_generic_link_add_one_symbol 3175 (info, abfd, name, BSF_WARNING, s, 0, msg, 3176 FALSE, bed->collect, NULL))) 3177 goto error_return; 3178 3179 if (! info->relocatable) 3180 { 3181 /* Clobber the section size so that the warning does 3182 not get copied into the output file. */ 3183 s->size = 0; 3184 3185 /* Also set SEC_EXCLUDE, so that symbols defined in 3186 the warning section don't get copied to the output. */ 3187 s->flags |= SEC_EXCLUDE; 3188 } 3189 } 3190 } 3191 } 3192 3193 add_needed = TRUE; 3194 if (! dynamic) 3195 { 3196 /* If we are creating a shared library, create all the dynamic 3197 sections immediately. We need to attach them to something, 3198 so we attach them to this BFD, provided it is the right 3199 format. FIXME: If there are no input BFD's of the same 3200 format as the output, we can't make a shared library. */ 3201 if (info->shared 3202 && is_elf_hash_table (htab) 3203 && htab->root.creator == abfd->xvec 3204 && !htab->dynamic_sections_created) 3205 { 3206 if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) 3207 goto error_return; 3208 } 3209 } 3210 else if (!is_elf_hash_table (htab)) 3211 goto error_return; 3212 else 3213 { 3214 asection *s; 3215 const char *soname = NULL; 3216 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; 3217 int ret; 3218 3219 /* ld --just-symbols and dynamic objects don't mix very well. 3220 ld shouldn't allow it. */ 3221 if ((s = abfd->sections) != NULL 3222 && s->sec_info_type == ELF_INFO_TYPE_JUST_SYMS) 3223 abort (); 3224 3225 /* If this dynamic lib was specified on the command line with 3226 --as-needed in effect, then we don't want to add a DT_NEEDED 3227 tag unless the lib is actually used. Similary for libs brought 3228 in by another lib's DT_NEEDED. When --no-add-needed is used 3229 on a dynamic lib, we don't want to add a DT_NEEDED entry for 3230 any dynamic library in DT_NEEDED tags in the dynamic lib at 3231 all. */ 3232 add_needed = (elf_dyn_lib_class (abfd) 3233 & (DYN_AS_NEEDED | DYN_DT_NEEDED 3234 | DYN_NO_NEEDED)) == 0; 3235 3236 s = bfd_get_section_by_name (abfd, ".dynamic"); 3237 if (s != NULL) 3238 { 3239 bfd_byte *dynbuf; 3240 bfd_byte *extdyn; 3241 int elfsec; 3242 unsigned long shlink; 3243 3244 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 3245 goto error_free_dyn; 3246 3247 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 3248 if (elfsec == -1) 3249 goto error_free_dyn; 3250 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 3251 3252 for (extdyn = dynbuf; 3253 extdyn < dynbuf + s->size; 3254 extdyn += bed->s->sizeof_dyn) 3255 { 3256 Elf_Internal_Dyn dyn; 3257 3258 bed->s->swap_dyn_in (abfd, extdyn, &dyn); 3259 if (dyn.d_tag == DT_SONAME) 3260 { 3261 unsigned int tagv = dyn.d_un.d_val; 3262 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3263 if (soname == NULL) 3264 goto error_free_dyn; 3265 } 3266 if (dyn.d_tag == DT_NEEDED) 3267 { 3268 struct bfd_link_needed_list *n, **pn; 3269 char *fnm, *anm; 3270 unsigned int tagv = dyn.d_un.d_val; 3271 3272 amt = sizeof (struct bfd_link_needed_list); 3273 n = bfd_alloc (abfd, amt); 3274 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3275 if (n == NULL || fnm == NULL) 3276 goto error_free_dyn; 3277 amt = strlen (fnm) + 1; 3278 anm = bfd_alloc (abfd, amt); 3279 if (anm == NULL) 3280 goto error_free_dyn; 3281 memcpy (anm, fnm, amt); 3282 n->name = anm; 3283 n->by = abfd; 3284 n->next = NULL; 3285 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) 3286 ; 3287 *pn = n; 3288 } 3289 if (dyn.d_tag == DT_RUNPATH) 3290 { 3291 struct bfd_link_needed_list *n, **pn; 3292 char *fnm, *anm; 3293 unsigned int tagv = dyn.d_un.d_val; 3294 3295 amt = sizeof (struct bfd_link_needed_list); 3296 n = bfd_alloc (abfd, amt); 3297 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3298 if (n == NULL || fnm == NULL) 3299 goto error_free_dyn; 3300 amt = strlen (fnm) + 1; 3301 anm = bfd_alloc (abfd, amt); 3302 if (anm == NULL) 3303 goto error_free_dyn; 3304 memcpy (anm, fnm, amt); 3305 n->name = anm; 3306 n->by = abfd; 3307 n->next = NULL; 3308 for (pn = & runpath; 3309 *pn != NULL; 3310 pn = &(*pn)->next) 3311 ; 3312 *pn = n; 3313 } 3314 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ 3315 if (!runpath && dyn.d_tag == DT_RPATH) 3316 { 3317 struct bfd_link_needed_list *n, **pn; 3318 char *fnm, *anm; 3319 unsigned int tagv = dyn.d_un.d_val; 3320 3321 amt = sizeof (struct bfd_link_needed_list); 3322 n = bfd_alloc (abfd, amt); 3323 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3324 if (n == NULL || fnm == NULL) 3325 goto error_free_dyn; 3326 amt = strlen (fnm) + 1; 3327 anm = bfd_alloc (abfd, amt); 3328 if (anm == NULL) 3329 { 3330 error_free_dyn: 3331 free (dynbuf); 3332 goto error_return; 3333 } 3334 memcpy (anm, fnm, amt); 3335 n->name = anm; 3336 n->by = abfd; 3337 n->next = NULL; 3338 for (pn = & rpath; 3339 *pn != NULL; 3340 pn = &(*pn)->next) 3341 ; 3342 *pn = n; 3343 } 3344 } 3345 3346 free (dynbuf); 3347 } 3348 3349 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that 3350 frees all more recently bfd_alloc'd blocks as well. */ 3351 if (runpath) 3352 rpath = runpath; 3353 3354 if (rpath) 3355 { 3356 struct bfd_link_needed_list **pn; 3357 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) 3358 ; 3359 *pn = rpath; 3360 } 3361 3362 /* We do not want to include any of the sections in a dynamic 3363 object in the output file. We hack by simply clobbering the 3364 list of sections in the BFD. This could be handled more 3365 cleanly by, say, a new section flag; the existing 3366 SEC_NEVER_LOAD flag is not the one we want, because that one 3367 still implies that the section takes up space in the output 3368 file. */ 3369 bfd_section_list_clear (abfd); 3370 3371 /* Find the name to use in a DT_NEEDED entry that refers to this 3372 object. If the object has a DT_SONAME entry, we use it. 3373 Otherwise, if the generic linker stuck something in 3374 elf_dt_name, we use that. Otherwise, we just use the file 3375 name. */ 3376 if (soname == NULL || *soname == '\0') 3377 { 3378 soname = elf_dt_name (abfd); 3379 if (soname == NULL || *soname == '\0') 3380 soname = bfd_get_filename (abfd); 3381 } 3382 3383 /* Save the SONAME because sometimes the linker emulation code 3384 will need to know it. */ 3385 elf_dt_name (abfd) = soname; 3386 3387 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 3388 if (ret < 0) 3389 goto error_return; 3390 3391 /* If we have already included this dynamic object in the 3392 link, just ignore it. There is no reason to include a 3393 particular dynamic object more than once. */ 3394 if (ret > 0) 3395 return TRUE; 3396 } 3397 3398 /* If this is a dynamic object, we always link against the .dynsym 3399 symbol table, not the .symtab symbol table. The dynamic linker 3400 will only see the .dynsym symbol table, so there is no reason to 3401 look at .symtab for a dynamic object. */ 3402 3403 if (! dynamic || elf_dynsymtab (abfd) == 0) 3404 hdr = &elf_tdata (abfd)->symtab_hdr; 3405 else 3406 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3407 3408 symcount = hdr->sh_size / bed->s->sizeof_sym; 3409 3410 /* The sh_info field of the symtab header tells us where the 3411 external symbols start. We don't care about the local symbols at 3412 this point. */ 3413 if (elf_bad_symtab (abfd)) 3414 { 3415 extsymcount = symcount; 3416 extsymoff = 0; 3417 } 3418 else 3419 { 3420 extsymcount = symcount - hdr->sh_info; 3421 extsymoff = hdr->sh_info; 3422 } 3423 3424 sym_hash = NULL; 3425 if (extsymcount != 0) 3426 { 3427 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3428 NULL, NULL, NULL); 3429 if (isymbuf == NULL) 3430 goto error_return; 3431 3432 /* We store a pointer to the hash table entry for each external 3433 symbol. */ 3434 amt = extsymcount * sizeof (struct elf_link_hash_entry *); 3435 sym_hash = bfd_alloc (abfd, amt); 3436 if (sym_hash == NULL) 3437 goto error_free_sym; 3438 elf_sym_hashes (abfd) = sym_hash; 3439 } 3440 3441 if (dynamic) 3442 { 3443 /* Read in any version definitions. */ 3444 if (!_bfd_elf_slurp_version_tables (abfd, 3445 info->default_imported_symver)) 3446 goto error_free_sym; 3447 3448 /* Read in the symbol versions, but don't bother to convert them 3449 to internal format. */ 3450 if (elf_dynversym (abfd) != 0) 3451 { 3452 Elf_Internal_Shdr *versymhdr; 3453 3454 versymhdr = &elf_tdata (abfd)->dynversym_hdr; 3455 extversym = bfd_malloc (versymhdr->sh_size); 3456 if (extversym == NULL) 3457 goto error_free_sym; 3458 amt = versymhdr->sh_size; 3459 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 3460 || bfd_bread (extversym, amt, abfd) != amt) 3461 goto error_free_vers; 3462 } 3463 } 3464 3465 /* If we are loading an as-needed shared lib, save the symbol table 3466 state before we start adding symbols. If the lib turns out 3467 to be unneeded, restore the state. */ 3468 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 3469 { 3470 unsigned int i; 3471 size_t entsize; 3472 3473 for (entsize = 0, i = 0; i < htab->root.table.size; i++) 3474 { 3475 struct bfd_hash_entry *p; 3476 struct elf_link_hash_entry *h; 3477 3478 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 3479 { 3480 h = (struct elf_link_hash_entry *) p; 3481 entsize += htab->root.table.entsize; 3482 if (h->root.type == bfd_link_hash_warning) 3483 entsize += htab->root.table.entsize; 3484 } 3485 } 3486 3487 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); 3488 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); 3489 old_tab = bfd_malloc (tabsize + entsize + hashsize); 3490 if (old_tab == NULL) 3491 goto error_free_vers; 3492 3493 /* Remember the current objalloc pointer, so that all mem for 3494 symbols added can later be reclaimed. */ 3495 alloc_mark = bfd_hash_allocate (&htab->root.table, 1); 3496 if (alloc_mark == NULL) 3497 goto error_free_vers; 3498 3499 /* Clone the symbol table and sym hashes. Remember some 3500 pointers into the symbol table, and dynamic symbol count. */ 3501 old_hash = (char *) old_tab + tabsize; 3502 old_ent = (char *) old_hash + hashsize; 3503 memcpy (old_tab, htab->root.table.table, tabsize); 3504 memcpy (old_hash, sym_hash, hashsize); 3505 old_undefs = htab->root.undefs; 3506 old_undefs_tail = htab->root.undefs_tail; 3507 old_dynsymcount = htab->dynsymcount; 3508 3509 for (i = 0; i < htab->root.table.size; i++) 3510 { 3511 struct bfd_hash_entry *p; 3512 struct elf_link_hash_entry *h; 3513 3514 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 3515 { 3516 memcpy (old_ent, p, htab->root.table.entsize); 3517 old_ent = (char *) old_ent + htab->root.table.entsize; 3518 h = (struct elf_link_hash_entry *) p; 3519 if (h->root.type == bfd_link_hash_warning) 3520 { 3521 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); 3522 old_ent = (char *) old_ent + htab->root.table.entsize; 3523 } 3524 } 3525 } 3526 } 3527 3528 weaks = NULL; 3529 ever = extversym != NULL ? extversym + extsymoff : NULL; 3530 for (isym = isymbuf, isymend = isymbuf + extsymcount; 3531 isym < isymend; 3532 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) 3533 { 3534 int bind; 3535 bfd_vma value; 3536 asection *sec, *new_sec; 3537 flagword flags; 3538 const char *name; 3539 struct elf_link_hash_entry *h; 3540 bfd_boolean definition; 3541 bfd_boolean size_change_ok; 3542 bfd_boolean type_change_ok; 3543 bfd_boolean new_weakdef; 3544 bfd_boolean override; 3545 bfd_boolean common; 3546 unsigned int old_alignment; 3547 bfd *old_bfd; 3548 3549 override = FALSE; 3550 3551 flags = BSF_NO_FLAGS; 3552 sec = NULL; 3553 value = isym->st_value; 3554 *sym_hash = NULL; 3555 common = bed->common_definition (isym); 3556 3557 bind = ELF_ST_BIND (isym->st_info); 3558 if (bind == STB_LOCAL) 3559 { 3560 /* This should be impossible, since ELF requires that all 3561 global symbols follow all local symbols, and that sh_info 3562 point to the first global symbol. Unfortunately, Irix 5 3563 screws this up. */ 3564 continue; 3565 } 3566 else if (bind == STB_GLOBAL) 3567 { 3568 if (isym->st_shndx != SHN_UNDEF && !common) 3569 flags = BSF_GLOBAL; 3570 } 3571 else if (bind == STB_WEAK) 3572 flags = BSF_WEAK; 3573 else 3574 { 3575 /* Leave it up to the processor backend. */ 3576 } 3577 3578 if (isym->st_shndx == SHN_UNDEF) 3579 sec = bfd_und_section_ptr; 3580 else if (isym->st_shndx < SHN_LORESERVE 3581 || isym->st_shndx > SHN_HIRESERVE) 3582 { 3583 sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 3584 if (sec == NULL) 3585 sec = bfd_abs_section_ptr; 3586 else if (sec->kept_section) 3587 { 3588 /* Symbols from discarded section are undefined. We keep 3589 its visibility. */ 3590 sec = bfd_und_section_ptr; 3591 isym->st_shndx = SHN_UNDEF; 3592 } 3593 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 3594 value -= sec->vma; 3595 } 3596 else if (isym->st_shndx == SHN_ABS) 3597 sec = bfd_abs_section_ptr; 3598 else if (isym->st_shndx == SHN_COMMON) 3599 { 3600 sec = bfd_com_section_ptr; 3601 /* What ELF calls the size we call the value. What ELF 3602 calls the value we call the alignment. */ 3603 value = isym->st_size; 3604 } 3605 else 3606 { 3607 /* Leave it up to the processor backend. */ 3608 } 3609 3610 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3611 isym->st_name); 3612 if (name == NULL) 3613 goto error_free_vers; 3614 3615 if (isym->st_shndx == SHN_COMMON 3616 && ELF_ST_TYPE (isym->st_info) == STT_TLS) 3617 { 3618 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); 3619 3620 if (tcomm == NULL) 3621 { 3622 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", 3623 (SEC_ALLOC 3624 | SEC_IS_COMMON 3625 | SEC_LINKER_CREATED 3626 | SEC_THREAD_LOCAL)); 3627 if (tcomm == NULL) 3628 goto error_free_vers; 3629 } 3630 sec = tcomm; 3631 } 3632 else if (bed->elf_add_symbol_hook) 3633 { 3634 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, 3635 &sec, &value)) 3636 goto error_free_vers; 3637 3638 /* The hook function sets the name to NULL if this symbol 3639 should be skipped for some reason. */ 3640 if (name == NULL) 3641 continue; 3642 } 3643 3644 /* Sanity check that all possibilities were handled. */ 3645 if (sec == NULL) 3646 { 3647 bfd_set_error (bfd_error_bad_value); 3648 goto error_free_vers; 3649 } 3650 3651 if (bfd_is_und_section (sec) 3652 || bfd_is_com_section (sec)) 3653 definition = FALSE; 3654 else 3655 definition = TRUE; 3656 3657 size_change_ok = FALSE; 3658 type_change_ok = bed->type_change_ok; 3659 old_alignment = 0; 3660 old_bfd = NULL; 3661 new_sec = sec; 3662 3663 if (is_elf_hash_table (htab)) 3664 { 3665 Elf_Internal_Versym iver; 3666 unsigned int vernum = 0; 3667 bfd_boolean skip; 3668 3669 if (ever == NULL) 3670 { 3671 if (info->default_imported_symver) 3672 /* Use the default symbol version created earlier. */ 3673 iver.vs_vers = elf_tdata (abfd)->cverdefs; 3674 else 3675 iver.vs_vers = 0; 3676 } 3677 else 3678 _bfd_elf_swap_versym_in (abfd, ever, &iver); 3679 3680 vernum = iver.vs_vers & VERSYM_VERSION; 3681 3682 /* If this is a hidden symbol, or if it is not version 3683 1, we append the version name to the symbol name. 3684 However, we do not modify a non-hidden absolute symbol 3685 if it is not a function, because it might be the version 3686 symbol itself. FIXME: What if it isn't? */ 3687 if ((iver.vs_vers & VERSYM_HIDDEN) != 0 3688 || (vernum > 1 && (! bfd_is_abs_section (sec) 3689 || ELF_ST_TYPE (isym->st_info) == STT_FUNC))) 3690 { 3691 const char *verstr; 3692 size_t namelen, verlen, newlen; 3693 char *newname, *p; 3694 3695 if (isym->st_shndx != SHN_UNDEF) 3696 { 3697 if (vernum > elf_tdata (abfd)->cverdefs) 3698 verstr = NULL; 3699 else if (vernum > 1) 3700 verstr = 3701 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 3702 else 3703 verstr = ""; 3704 3705 if (verstr == NULL) 3706 { 3707 (*_bfd_error_handler) 3708 (_("%B: %s: invalid version %u (max %d)"), 3709 abfd, name, vernum, 3710 elf_tdata (abfd)->cverdefs); 3711 bfd_set_error (bfd_error_bad_value); 3712 goto error_free_vers; 3713 } 3714 } 3715 else 3716 { 3717 /* We cannot simply test for the number of 3718 entries in the VERNEED section since the 3719 numbers for the needed versions do not start 3720 at 0. */ 3721 Elf_Internal_Verneed *t; 3722 3723 verstr = NULL; 3724 for (t = elf_tdata (abfd)->verref; 3725 t != NULL; 3726 t = t->vn_nextref) 3727 { 3728 Elf_Internal_Vernaux *a; 3729 3730 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 3731 { 3732 if (a->vna_other == vernum) 3733 { 3734 verstr = a->vna_nodename; 3735 break; 3736 } 3737 } 3738 if (a != NULL) 3739 break; 3740 } 3741 if (verstr == NULL) 3742 { 3743 (*_bfd_error_handler) 3744 (_("%B: %s: invalid needed version %d"), 3745 abfd, name, vernum); 3746 bfd_set_error (bfd_error_bad_value); 3747 goto error_free_vers; 3748 } 3749 } 3750 3751 namelen = strlen (name); 3752 verlen = strlen (verstr); 3753 newlen = namelen + verlen + 2; 3754 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 3755 && isym->st_shndx != SHN_UNDEF) 3756 ++newlen; 3757 3758 newname = bfd_hash_allocate (&htab->root.table, newlen); 3759 if (newname == NULL) 3760 goto error_free_vers; 3761 memcpy (newname, name, namelen); 3762 p = newname + namelen; 3763 *p++ = ELF_VER_CHR; 3764 /* If this is a defined non-hidden version symbol, 3765 we add another @ to the name. This indicates the 3766 default version of the symbol. */ 3767 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 3768 && isym->st_shndx != SHN_UNDEF) 3769 *p++ = ELF_VER_CHR; 3770 memcpy (p, verstr, verlen + 1); 3771 3772 name = newname; 3773 } 3774 3775 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, 3776 &value, &old_alignment, 3777 sym_hash, &skip, &override, 3778 &type_change_ok, &size_change_ok)) 3779 goto error_free_vers; 3780 3781 if (skip) 3782 continue; 3783 3784 if (override) 3785 definition = FALSE; 3786 3787 h = *sym_hash; 3788 while (h->root.type == bfd_link_hash_indirect 3789 || h->root.type == bfd_link_hash_warning) 3790 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3791 3792 /* Remember the old alignment if this is a common symbol, so 3793 that we don't reduce the alignment later on. We can't 3794 check later, because _bfd_generic_link_add_one_symbol 3795 will set a default for the alignment which we want to 3796 override. We also remember the old bfd where the existing 3797 definition comes from. */ 3798 switch (h->root.type) 3799 { 3800 default: 3801 break; 3802 3803 case bfd_link_hash_defined: 3804 case bfd_link_hash_defweak: 3805 old_bfd = h->root.u.def.section->owner; 3806 break; 3807 3808 case bfd_link_hash_common: 3809 old_bfd = h->root.u.c.p->section->owner; 3810 old_alignment = h->root.u.c.p->alignment_power; 3811 break; 3812 } 3813 3814 if (elf_tdata (abfd)->verdef != NULL 3815 && ! override 3816 && vernum > 1 3817 && definition) 3818 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; 3819 } 3820 3821 if (! (_bfd_generic_link_add_one_symbol 3822 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, 3823 (struct bfd_link_hash_entry **) sym_hash))) 3824 goto error_free_vers; 3825 3826 h = *sym_hash; 3827 while (h->root.type == bfd_link_hash_indirect 3828 || h->root.type == bfd_link_hash_warning) 3829 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3830 *sym_hash = h; 3831 3832 new_weakdef = FALSE; 3833 if (dynamic 3834 && definition 3835 && (flags & BSF_WEAK) != 0 3836 && ELF_ST_TYPE (isym->st_info) != STT_FUNC 3837 && is_elf_hash_table (htab) 3838 && h->u.weakdef == NULL) 3839 { 3840 /* Keep a list of all weak defined non function symbols from 3841 a dynamic object, using the weakdef field. Later in this 3842 function we will set the weakdef field to the correct 3843 value. We only put non-function symbols from dynamic 3844 objects on this list, because that happens to be the only 3845 time we need to know the normal symbol corresponding to a 3846 weak symbol, and the information is time consuming to 3847 figure out. If the weakdef field is not already NULL, 3848 then this symbol was already defined by some previous 3849 dynamic object, and we will be using that previous 3850 definition anyhow. */ 3851 3852 h->u.weakdef = weaks; 3853 weaks = h; 3854 new_weakdef = TRUE; 3855 } 3856 3857 /* Set the alignment of a common symbol. */ 3858 if ((common || bfd_is_com_section (sec)) 3859 && h->root.type == bfd_link_hash_common) 3860 { 3861 unsigned int align; 3862 3863 if (common) 3864 align = bfd_log2 (isym->st_value); 3865 else 3866 { 3867 /* The new symbol is a common symbol in a shared object. 3868 We need to get the alignment from the section. */ 3869 align = new_sec->alignment_power; 3870 } 3871 if (align > old_alignment 3872 /* Permit an alignment power of zero if an alignment of one 3873 is specified and no other alignments have been specified. */ 3874 || (isym->st_value == 1 && old_alignment == 0)) 3875 h->root.u.c.p->alignment_power = align; 3876 else 3877 h->root.u.c.p->alignment_power = old_alignment; 3878 } 3879 3880 if (is_elf_hash_table (htab)) 3881 { 3882 bfd_boolean dynsym; 3883 3884 /* Check the alignment when a common symbol is involved. This 3885 can change when a common symbol is overridden by a normal 3886 definition or a common symbol is ignored due to the old 3887 normal definition. We need to make sure the maximum 3888 alignment is maintained. */ 3889 if ((old_alignment || common) 3890 && h->root.type != bfd_link_hash_common) 3891 { 3892 unsigned int common_align; 3893 unsigned int normal_align; 3894 unsigned int symbol_align; 3895 bfd *normal_bfd; 3896 bfd *common_bfd; 3897 3898 symbol_align = ffs (h->root.u.def.value) - 1; 3899 if (h->root.u.def.section->owner != NULL 3900 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) 3901 { 3902 normal_align = h->root.u.def.section->alignment_power; 3903 if (normal_align > symbol_align) 3904 normal_align = symbol_align; 3905 } 3906 else 3907 normal_align = symbol_align; 3908 3909 if (old_alignment) 3910 { 3911 common_align = old_alignment; 3912 common_bfd = old_bfd; 3913 normal_bfd = abfd; 3914 } 3915 else 3916 { 3917 common_align = bfd_log2 (isym->st_value); 3918 common_bfd = abfd; 3919 normal_bfd = old_bfd; 3920 } 3921 3922 if (normal_align < common_align) 3923 (*_bfd_error_handler) 3924 (_("Warning: alignment %u of symbol `%s' in %B" 3925 " is smaller than %u in %B"), 3926 normal_bfd, common_bfd, 3927 1 << normal_align, name, 1 << common_align); 3928 } 3929 3930 /* Remember the symbol size and type. */ 3931 if (isym->st_size != 0 3932 && (definition || h->size == 0)) 3933 { 3934 if (h->size != 0 && h->size != isym->st_size && ! size_change_ok) 3935 (*_bfd_error_handler) 3936 (_("Warning: size of symbol `%s' changed" 3937 " from %lu in %B to %lu in %B"), 3938 old_bfd, abfd, 3939 name, (unsigned long) h->size, 3940 (unsigned long) isym->st_size); 3941 3942 h->size = isym->st_size; 3943 } 3944 3945 /* If this is a common symbol, then we always want H->SIZE 3946 to be the size of the common symbol. The code just above 3947 won't fix the size if a common symbol becomes larger. We 3948 don't warn about a size change here, because that is 3949 covered by --warn-common. */ 3950 if (h->root.type == bfd_link_hash_common) 3951 h->size = h->root.u.c.size; 3952 3953 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE 3954 && (definition || h->type == STT_NOTYPE)) 3955 { 3956 if (h->type != STT_NOTYPE 3957 && h->type != ELF_ST_TYPE (isym->st_info) 3958 && ! type_change_ok) 3959 (*_bfd_error_handler) 3960 (_("Warning: type of symbol `%s' changed" 3961 " from %d to %d in %B"), 3962 abfd, name, h->type, ELF_ST_TYPE (isym->st_info)); 3963 3964 h->type = ELF_ST_TYPE (isym->st_info); 3965 } 3966 3967 /* If st_other has a processor-specific meaning, specific 3968 code might be needed here. We never merge the visibility 3969 attribute with the one from a dynamic object. */ 3970 if (bed->elf_backend_merge_symbol_attribute) 3971 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, 3972 dynamic); 3973 3974 /* If this symbol has default visibility and the user has requested 3975 we not re-export it, then mark it as hidden. */ 3976 if (definition && !dynamic 3977 && (abfd->no_export 3978 || (abfd->my_archive && abfd->my_archive->no_export)) 3979 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) 3980 isym->st_other = (STV_HIDDEN 3981 | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); 3982 3983 if (isym->st_other != 0 && !dynamic) 3984 { 3985 unsigned char hvis, symvis, other, nvis; 3986 3987 /* Take the balance of OTHER from the definition. */ 3988 other = (definition ? isym->st_other : h->other); 3989 other &= ~ ELF_ST_VISIBILITY (-1); 3990 3991 /* Combine visibilities, using the most constraining one. */ 3992 hvis = ELF_ST_VISIBILITY (h->other); 3993 symvis = ELF_ST_VISIBILITY (isym->st_other); 3994 if (! hvis) 3995 nvis = symvis; 3996 else if (! symvis) 3997 nvis = hvis; 3998 else 3999 nvis = hvis < symvis ? hvis : symvis; 4000 4001 h->other = other | nvis; 4002 } 4003 4004 /* Set a flag in the hash table entry indicating the type of 4005 reference or definition we just found. Keep a count of 4006 the number of dynamic symbols we find. A dynamic symbol 4007 is one which is referenced or defined by both a regular 4008 object and a shared object. */ 4009 dynsym = FALSE; 4010 if (! dynamic) 4011 { 4012 if (! definition) 4013 { 4014 h->ref_regular = 1; 4015 if (bind != STB_WEAK) 4016 h->ref_regular_nonweak = 1; 4017 } 4018 else 4019 h->def_regular = 1; 4020 if (! info->executable 4021 || h->def_dynamic 4022 || h->ref_dynamic) 4023 dynsym = TRUE; 4024 } 4025 else 4026 { 4027 if (! definition) 4028 h->ref_dynamic = 1; 4029 else 4030 h->def_dynamic = 1; 4031 if (h->def_regular 4032 || h->ref_regular 4033 || (h->u.weakdef != NULL 4034 && ! new_weakdef 4035 && h->u.weakdef->dynindx != -1)) 4036 dynsym = TRUE; 4037 } 4038 4039 /* Check to see if we need to add an indirect symbol for 4040 the default name. */ 4041 if (definition || h->root.type == bfd_link_hash_common) 4042 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, 4043 &sec, &value, &dynsym, 4044 override)) 4045 goto error_free_vers; 4046 4047 if (definition && !dynamic) 4048 { 4049 char *p = strchr (name, ELF_VER_CHR); 4050 if (p != NULL && p[1] != ELF_VER_CHR) 4051 { 4052 /* Queue non-default versions so that .symver x, x@FOO 4053 aliases can be checked. */ 4054 if (!nondeflt_vers) 4055 { 4056 amt = ((isymend - isym + 1) 4057 * sizeof (struct elf_link_hash_entry *)); 4058 nondeflt_vers = bfd_malloc (amt); 4059 } 4060 nondeflt_vers[nondeflt_vers_cnt++] = h; 4061 } 4062 } 4063 4064 if (dynsym && h->dynindx == -1) 4065 { 4066 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4067 goto error_free_vers; 4068 if (h->u.weakdef != NULL 4069 && ! new_weakdef 4070 && h->u.weakdef->dynindx == -1) 4071 { 4072 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) 4073 goto error_free_vers; 4074 } 4075 } 4076 else if (dynsym && h->dynindx != -1) 4077 /* If the symbol already has a dynamic index, but 4078 visibility says it should not be visible, turn it into 4079 a local symbol. */ 4080 switch (ELF_ST_VISIBILITY (h->other)) 4081 { 4082 case STV_INTERNAL: 4083 case STV_HIDDEN: 4084 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 4085 dynsym = FALSE; 4086 break; 4087 } 4088 4089 if (!add_needed 4090 && definition 4091 && dynsym 4092 && h->ref_regular) 4093 { 4094 int ret; 4095 const char *soname = elf_dt_name (abfd); 4096 4097 /* A symbol from a library loaded via DT_NEEDED of some 4098 other library is referenced by a regular object. 4099 Add a DT_NEEDED entry for it. Issue an error if 4100 --no-add-needed is used. */ 4101 if ((elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) 4102 { 4103 (*_bfd_error_handler) 4104 (_("%s: invalid DSO for symbol `%s' definition"), 4105 abfd, name); 4106 bfd_set_error (bfd_error_bad_value); 4107 goto error_free_vers; 4108 } 4109 4110 elf_dyn_lib_class (abfd) &= ~DYN_AS_NEEDED; 4111 4112 add_needed = TRUE; 4113 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4114 if (ret < 0) 4115 goto error_free_vers; 4116 4117 BFD_ASSERT (ret == 0); 4118 } 4119 } 4120 } 4121 4122 if (extversym != NULL) 4123 { 4124 free (extversym); 4125 extversym = NULL; 4126 } 4127 4128 if (isymbuf != NULL) 4129 { 4130 free (isymbuf); 4131 isymbuf = NULL; 4132 } 4133 4134 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4135 { 4136 unsigned int i; 4137 4138 /* Restore the symbol table. */ 4139 old_hash = (char *) old_tab + tabsize; 4140 old_ent = (char *) old_hash + hashsize; 4141 sym_hash = elf_sym_hashes (abfd); 4142 memcpy (htab->root.table.table, old_tab, tabsize); 4143 memcpy (sym_hash, old_hash, hashsize); 4144 htab->root.undefs = old_undefs; 4145 htab->root.undefs_tail = old_undefs_tail; 4146 for (i = 0; i < htab->root.table.size; i++) 4147 { 4148 struct bfd_hash_entry *p; 4149 struct elf_link_hash_entry *h; 4150 4151 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4152 { 4153 h = (struct elf_link_hash_entry *) p; 4154 if (h->root.type == bfd_link_hash_warning) 4155 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4156 if (h->dynindx >= old_dynsymcount) 4157 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); 4158 4159 memcpy (p, old_ent, htab->root.table.entsize); 4160 old_ent = (char *) old_ent + htab->root.table.entsize; 4161 h = (struct elf_link_hash_entry *) p; 4162 if (h->root.type == bfd_link_hash_warning) 4163 { 4164 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); 4165 old_ent = (char *) old_ent + htab->root.table.entsize; 4166 } 4167 } 4168 } 4169 4170 free (old_tab); 4171 objalloc_free_block ((struct objalloc *) htab->root.table.memory, 4172 alloc_mark); 4173 if (nondeflt_vers != NULL) 4174 free (nondeflt_vers); 4175 return TRUE; 4176 } 4177 4178 if (old_tab != NULL) 4179 { 4180 free (old_tab); 4181 old_tab = NULL; 4182 } 4183 4184 /* Now that all the symbols from this input file are created, handle 4185 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ 4186 if (nondeflt_vers != NULL) 4187 { 4188 bfd_size_type cnt, symidx; 4189 4190 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) 4191 { 4192 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; 4193 char *shortname, *p; 4194 4195 p = strchr (h->root.root.string, ELF_VER_CHR); 4196 if (p == NULL 4197 || (h->root.type != bfd_link_hash_defined 4198 && h->root.type != bfd_link_hash_defweak)) 4199 continue; 4200 4201 amt = p - h->root.root.string; 4202 shortname = bfd_malloc (amt + 1); 4203 memcpy (shortname, h->root.root.string, amt); 4204 shortname[amt] = '\0'; 4205 4206 hi = (struct elf_link_hash_entry *) 4207 bfd_link_hash_lookup (&htab->root, shortname, 4208 FALSE, FALSE, FALSE); 4209 if (hi != NULL 4210 && hi->root.type == h->root.type 4211 && hi->root.u.def.value == h->root.u.def.value 4212 && hi->root.u.def.section == h->root.u.def.section) 4213 { 4214 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 4215 hi->root.type = bfd_link_hash_indirect; 4216 hi->root.u.i.link = (struct bfd_link_hash_entry *) h; 4217 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 4218 sym_hash = elf_sym_hashes (abfd); 4219 if (sym_hash) 4220 for (symidx = 0; symidx < extsymcount; ++symidx) 4221 if (sym_hash[symidx] == hi) 4222 { 4223 sym_hash[symidx] = h; 4224 break; 4225 } 4226 } 4227 free (shortname); 4228 } 4229 free (nondeflt_vers); 4230 nondeflt_vers = NULL; 4231 } 4232 4233 /* Now set the weakdefs field correctly for all the weak defined 4234 symbols we found. The only way to do this is to search all the 4235 symbols. Since we only need the information for non functions in 4236 dynamic objects, that's the only time we actually put anything on 4237 the list WEAKS. We need this information so that if a regular 4238 object refers to a symbol defined weakly in a dynamic object, the 4239 real symbol in the dynamic object is also put in the dynamic 4240 symbols; we also must arrange for both symbols to point to the 4241 same memory location. We could handle the general case of symbol 4242 aliasing, but a general symbol alias can only be generated in 4243 assembler code, handling it correctly would be very time 4244 consuming, and other ELF linkers don't handle general aliasing 4245 either. */ 4246 if (weaks != NULL) 4247 { 4248 struct elf_link_hash_entry **hpp; 4249 struct elf_link_hash_entry **hppend; 4250 struct elf_link_hash_entry **sorted_sym_hash; 4251 struct elf_link_hash_entry *h; 4252 size_t sym_count; 4253 4254 /* Since we have to search the whole symbol list for each weak 4255 defined symbol, search time for N weak defined symbols will be 4256 O(N^2). Binary search will cut it down to O(NlogN). */ 4257 amt = extsymcount * sizeof (struct elf_link_hash_entry *); 4258 sorted_sym_hash = bfd_malloc (amt); 4259 if (sorted_sym_hash == NULL) 4260 goto error_return; 4261 sym_hash = sorted_sym_hash; 4262 hpp = elf_sym_hashes (abfd); 4263 hppend = hpp + extsymcount; 4264 sym_count = 0; 4265 for (; hpp < hppend; hpp++) 4266 { 4267 h = *hpp; 4268 if (h != NULL 4269 && h->root.type == bfd_link_hash_defined 4270 && h->type != STT_FUNC) 4271 { 4272 *sym_hash = h; 4273 sym_hash++; 4274 sym_count++; 4275 } 4276 } 4277 4278 qsort (sorted_sym_hash, sym_count, 4279 sizeof (struct elf_link_hash_entry *), 4280 elf_sort_symbol); 4281 4282 while (weaks != NULL) 4283 { 4284 struct elf_link_hash_entry *hlook; 4285 asection *slook; 4286 bfd_vma vlook; 4287 long ilook; 4288 size_t i, j, idx; 4289 4290 hlook = weaks; 4291 weaks = hlook->u.weakdef; 4292 hlook->u.weakdef = NULL; 4293 4294 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined 4295 || hlook->root.type == bfd_link_hash_defweak 4296 || hlook->root.type == bfd_link_hash_common 4297 || hlook->root.type == bfd_link_hash_indirect); 4298 slook = hlook->root.u.def.section; 4299 vlook = hlook->root.u.def.value; 4300 4301 ilook = -1; 4302 i = 0; 4303 j = sym_count; 4304 while (i < j) 4305 { 4306 bfd_signed_vma vdiff; 4307 idx = (i + j) / 2; 4308 h = sorted_sym_hash [idx]; 4309 vdiff = vlook - h->root.u.def.value; 4310 if (vdiff < 0) 4311 j = idx; 4312 else if (vdiff > 0) 4313 i = idx + 1; 4314 else 4315 { 4316 long sdiff = slook->id - h->root.u.def.section->id; 4317 if (sdiff < 0) 4318 j = idx; 4319 else if (sdiff > 0) 4320 i = idx + 1; 4321 else 4322 { 4323 ilook = idx; 4324 break; 4325 } 4326 } 4327 } 4328 4329 /* We didn't find a value/section match. */ 4330 if (ilook == -1) 4331 continue; 4332 4333 for (i = ilook; i < sym_count; i++) 4334 { 4335 h = sorted_sym_hash [i]; 4336 4337 /* Stop if value or section doesn't match. */ 4338 if (h->root.u.def.value != vlook 4339 || h->root.u.def.section != slook) 4340 break; 4341 else if (h != hlook) 4342 { 4343 hlook->u.weakdef = h; 4344 4345 /* If the weak definition is in the list of dynamic 4346 symbols, make sure the real definition is put 4347 there as well. */ 4348 if (hlook->dynindx != -1 && h->dynindx == -1) 4349 { 4350 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4351 goto error_return; 4352 } 4353 4354 /* If the real definition is in the list of dynamic 4355 symbols, make sure the weak definition is put 4356 there as well. If we don't do this, then the 4357 dynamic loader might not merge the entries for the 4358 real definition and the weak definition. */ 4359 if (h->dynindx != -1 && hlook->dynindx == -1) 4360 { 4361 if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) 4362 goto error_return; 4363 } 4364 break; 4365 } 4366 } 4367 } 4368 4369 free (sorted_sym_hash); 4370 } 4371 4372 if (bed->check_directives) 4373 (*bed->check_directives) (abfd, info); 4374 4375 /* If this object is the same format as the output object, and it is 4376 not a shared library, then let the backend look through the 4377 relocs. 4378 4379 This is required to build global offset table entries and to 4380 arrange for dynamic relocs. It is not required for the 4381 particular common case of linking non PIC code, even when linking 4382 against shared libraries, but unfortunately there is no way of 4383 knowing whether an object file has been compiled PIC or not. 4384 Looking through the relocs is not particularly time consuming. 4385 The problem is that we must either (1) keep the relocs in memory, 4386 which causes the linker to require additional runtime memory or 4387 (2) read the relocs twice from the input file, which wastes time. 4388 This would be a good case for using mmap. 4389 4390 I have no idea how to handle linking PIC code into a file of a 4391 different format. It probably can't be done. */ 4392 if (! dynamic 4393 && is_elf_hash_table (htab) 4394 && htab->root.creator == abfd->xvec 4395 && bed->check_relocs != NULL) 4396 { 4397 asection *o; 4398 4399 for (o = abfd->sections; o != NULL; o = o->next) 4400 { 4401 Elf_Internal_Rela *internal_relocs; 4402 bfd_boolean ok; 4403 4404 if ((o->flags & SEC_RELOC) == 0 4405 || o->reloc_count == 0 4406 || ((info->strip == strip_all || info->strip == strip_debugger) 4407 && (o->flags & SEC_DEBUGGING) != 0) 4408 || bfd_is_abs_section (o->output_section)) 4409 continue; 4410 4411 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, 4412 info->keep_memory); 4413 if (internal_relocs == NULL) 4414 goto error_return; 4415 4416 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); 4417 4418 if (elf_section_data (o)->relocs != internal_relocs) 4419 free (internal_relocs); 4420 4421 if (! ok) 4422 goto error_return; 4423 } 4424 } 4425 4426 /* If this is a non-traditional link, try to optimize the handling 4427 of the .stab/.stabstr sections. */ 4428 if (! dynamic 4429 && ! info->traditional_format 4430 && is_elf_hash_table (htab) 4431 && (info->strip != strip_all && info->strip != strip_debugger)) 4432 { 4433 asection *stabstr; 4434 4435 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 4436 if (stabstr != NULL) 4437 { 4438 bfd_size_type string_offset = 0; 4439 asection *stab; 4440 4441 for (stab = abfd->sections; stab; stab = stab->next) 4442 if (strncmp (".stab", stab->name, 5) == 0 4443 && (!stab->name[5] || 4444 (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) 4445 && (stab->flags & SEC_MERGE) == 0 4446 && !bfd_is_abs_section (stab->output_section)) 4447 { 4448 struct bfd_elf_section_data *secdata; 4449 4450 secdata = elf_section_data (stab); 4451 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, 4452 stabstr, &secdata->sec_info, 4453 &string_offset)) 4454 goto error_return; 4455 if (secdata->sec_info) 4456 stab->sec_info_type = ELF_INFO_TYPE_STABS; 4457 } 4458 } 4459 } 4460 4461 if (is_elf_hash_table (htab) && add_needed) 4462 { 4463 /* Add this bfd to the loaded list. */ 4464 struct elf_link_loaded_list *n; 4465 4466 n = bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); 4467 if (n == NULL) 4468 goto error_return; 4469 n->abfd = abfd; 4470 n->next = htab->loaded; 4471 htab->loaded = n; 4472 } 4473 4474 return TRUE; 4475 4476 error_free_vers: 4477 if (old_tab != NULL) 4478 free (old_tab); 4479 if (nondeflt_vers != NULL) 4480 free (nondeflt_vers); 4481 if (extversym != NULL) 4482 free (extversym); 4483 error_free_sym: 4484 if (isymbuf != NULL) 4485 free (isymbuf); 4486 error_return: 4487 return FALSE; 4488 } 4489 4490 /* Return the linker hash table entry of a symbol that might be 4491 satisfied by an archive symbol. Return -1 on error. */ 4492 4493 struct elf_link_hash_entry * 4494 _bfd_elf_archive_symbol_lookup (bfd *abfd, 4495 struct bfd_link_info *info, 4496 const char *name) 4497 { 4498 struct elf_link_hash_entry *h; 4499 char *p, *copy; 4500 size_t len, first; 4501 4502 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 4503 if (h != NULL) 4504 return h; 4505 4506 /* If this is a default version (the name contains @@), look up the 4507 symbol again with only one `@' as well as without the version. 4508 The effect is that references to the symbol with and without the 4509 version will be matched by the default symbol in the archive. */ 4510 4511 p = strchr (name, ELF_VER_CHR); 4512 if (p == NULL || p[1] != ELF_VER_CHR) 4513 return h; 4514 4515 /* First check with only one `@'. */ 4516 len = strlen (name); 4517 copy = bfd_alloc (abfd, len); 4518 if (copy == NULL) 4519 return (struct elf_link_hash_entry *) 0 - 1; 4520 4521 first = p - name + 1; 4522 memcpy (copy, name, first); 4523 memcpy (copy + first, name + first + 1, len - first); 4524 4525 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, FALSE); 4526 if (h == NULL) 4527 { 4528 /* We also need to check references to the symbol without the 4529 version. */ 4530 copy[first - 1] = '\0'; 4531 h = elf_link_hash_lookup (elf_hash_table (info), copy, 4532 FALSE, FALSE, FALSE); 4533 } 4534 4535 bfd_release (abfd, copy); 4536 return h; 4537 } 4538 4539 /* Add symbols from an ELF archive file to the linker hash table. We 4540 don't use _bfd_generic_link_add_archive_symbols because of a 4541 problem which arises on UnixWare. The UnixWare libc.so is an 4542 archive which includes an entry libc.so.1 which defines a bunch of 4543 symbols. The libc.so archive also includes a number of other 4544 object files, which also define symbols, some of which are the same 4545 as those defined in libc.so.1. Correct linking requires that we 4546 consider each object file in turn, and include it if it defines any 4547 symbols we need. _bfd_generic_link_add_archive_symbols does not do 4548 this; it looks through the list of undefined symbols, and includes 4549 any object file which defines them. When this algorithm is used on 4550 UnixWare, it winds up pulling in libc.so.1 early and defining a 4551 bunch of symbols. This means that some of the other objects in the 4552 archive are not included in the link, which is incorrect since they 4553 precede libc.so.1 in the archive. 4554 4555 Fortunately, ELF archive handling is simpler than that done by 4556 _bfd_generic_link_add_archive_symbols, which has to allow for a.out 4557 oddities. In ELF, if we find a symbol in the archive map, and the 4558 symbol is currently undefined, we know that we must pull in that 4559 object file. 4560 4561 Unfortunately, we do have to make multiple passes over the symbol 4562 table until nothing further is resolved. */ 4563 4564 static bfd_boolean 4565 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 4566 { 4567 symindex c; 4568 bfd_boolean *defined = NULL; 4569 bfd_boolean *included = NULL; 4570 carsym *symdefs; 4571 bfd_boolean loop; 4572 bfd_size_type amt; 4573 const struct elf_backend_data *bed; 4574 struct elf_link_hash_entry * (*archive_symbol_lookup) 4575 (bfd *, struct bfd_link_info *, const char *); 4576 4577 if (! bfd_has_map (abfd)) 4578 { 4579 /* An empty archive is a special case. */ 4580 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 4581 return TRUE; 4582 bfd_set_error (bfd_error_no_armap); 4583 return FALSE; 4584 } 4585 4586 /* Keep track of all symbols we know to be already defined, and all 4587 files we know to be already included. This is to speed up the 4588 second and subsequent passes. */ 4589 c = bfd_ardata (abfd)->symdef_count; 4590 if (c == 0) 4591 return TRUE; 4592 amt = c; 4593 amt *= sizeof (bfd_boolean); 4594 defined = bfd_zmalloc (amt); 4595 included = bfd_zmalloc (amt); 4596 if (defined == NULL || included == NULL) 4597 goto error_return; 4598 4599 symdefs = bfd_ardata (abfd)->symdefs; 4600 bed = get_elf_backend_data (abfd); 4601 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; 4602 4603 do 4604 { 4605 file_ptr last; 4606 symindex i; 4607 carsym *symdef; 4608 carsym *symdefend; 4609 4610 loop = FALSE; 4611 last = -1; 4612 4613 symdef = symdefs; 4614 symdefend = symdef + c; 4615 for (i = 0; symdef < symdefend; symdef++, i++) 4616 { 4617 struct elf_link_hash_entry *h; 4618 bfd *element; 4619 struct bfd_link_hash_entry *undefs_tail; 4620 symindex mark; 4621 4622 if (defined[i] || included[i]) 4623 continue; 4624 if (symdef->file_offset == last) 4625 { 4626 included[i] = TRUE; 4627 continue; 4628 } 4629 4630 h = archive_symbol_lookup (abfd, info, symdef->name); 4631 if (h == (struct elf_link_hash_entry *) 0 - 1) 4632 goto error_return; 4633 4634 if (h == NULL) 4635 continue; 4636 4637 if (h->root.type == bfd_link_hash_common) 4638 { 4639 /* We currently have a common symbol. The archive map contains 4640 a reference to this symbol, so we may want to include it. We 4641 only want to include it however, if this archive element 4642 contains a definition of the symbol, not just another common 4643 declaration of it. 4644 4645 Unfortunately some archivers (including GNU ar) will put 4646 declarations of common symbols into their archive maps, as 4647 well as real definitions, so we cannot just go by the archive 4648 map alone. Instead we must read in the element's symbol 4649 table and check that to see what kind of symbol definition 4650 this is. */ 4651 if (! elf_link_is_defined_archive_symbol (abfd, symdef)) 4652 continue; 4653 } 4654 else if (h->root.type != bfd_link_hash_undefined) 4655 { 4656 if (h->root.type != bfd_link_hash_undefweak) 4657 defined[i] = TRUE; 4658 continue; 4659 } 4660 4661 /* We need to include this archive member. */ 4662 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 4663 if (element == NULL) 4664 goto error_return; 4665 4666 if (! bfd_check_format (element, bfd_object)) 4667 goto error_return; 4668 4669 /* Doublecheck that we have not included this object 4670 already--it should be impossible, but there may be 4671 something wrong with the archive. */ 4672 if (element->archive_pass != 0) 4673 { 4674 bfd_set_error (bfd_error_bad_value); 4675 goto error_return; 4676 } 4677 element->archive_pass = 1; 4678 4679 undefs_tail = info->hash->undefs_tail; 4680 4681 if (! (*info->callbacks->add_archive_element) (info, element, 4682 symdef->name)) 4683 goto error_return; 4684 if (! bfd_link_add_symbols (element, info)) 4685 goto error_return; 4686 4687 /* If there are any new undefined symbols, we need to make 4688 another pass through the archive in order to see whether 4689 they can be defined. FIXME: This isn't perfect, because 4690 common symbols wind up on undefs_tail and because an 4691 undefined symbol which is defined later on in this pass 4692 does not require another pass. This isn't a bug, but it 4693 does make the code less efficient than it could be. */ 4694 if (undefs_tail != info->hash->undefs_tail) 4695 loop = TRUE; 4696 4697 /* Look backward to mark all symbols from this object file 4698 which we have already seen in this pass. */ 4699 mark = i; 4700 do 4701 { 4702 included[mark] = TRUE; 4703 if (mark == 0) 4704 break; 4705 --mark; 4706 } 4707 while (symdefs[mark].file_offset == symdef->file_offset); 4708 4709 /* We mark subsequent symbols from this object file as we go 4710 on through the loop. */ 4711 last = symdef->file_offset; 4712 } 4713 } 4714 while (loop); 4715 4716 free (defined); 4717 free (included); 4718 4719 return TRUE; 4720 4721 error_return: 4722 if (defined != NULL) 4723 free (defined); 4724 if (included != NULL) 4725 free (included); 4726 return FALSE; 4727 } 4728 4729 /* Given an ELF BFD, add symbols to the global hash table as 4730 appropriate. */ 4731 4732 bfd_boolean 4733 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 4734 { 4735 switch (bfd_get_format (abfd)) 4736 { 4737 case bfd_object: 4738 return elf_link_add_object_symbols (abfd, info); 4739 case bfd_archive: 4740 return elf_link_add_archive_symbols (abfd, info); 4741 default: 4742 bfd_set_error (bfd_error_wrong_format); 4743 return FALSE; 4744 } 4745 } 4746 4747 /* This function will be called though elf_link_hash_traverse to store 4748 all hash value of the exported symbols in an array. */ 4749 4750 static bfd_boolean 4751 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) 4752 { 4753 unsigned long **valuep = data; 4754 const char *name; 4755 char *p; 4756 unsigned long ha; 4757 char *alc = NULL; 4758 4759 if (h->root.type == bfd_link_hash_warning) 4760 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4761 4762 /* Ignore indirect symbols. These are added by the versioning code. */ 4763 if (h->dynindx == -1) 4764 return TRUE; 4765 4766 name = h->root.root.string; 4767 p = strchr (name, ELF_VER_CHR); 4768 if (p != NULL) 4769 { 4770 alc = bfd_malloc (p - name + 1); 4771 memcpy (alc, name, p - name); 4772 alc[p - name] = '\0'; 4773 name = alc; 4774 } 4775 4776 /* Compute the hash value. */ 4777 ha = bfd_elf_hash (name); 4778 4779 /* Store the found hash value in the array given as the argument. */ 4780 *(*valuep)++ = ha; 4781 4782 /* And store it in the struct so that we can put it in the hash table 4783 later. */ 4784 h->u.elf_hash_value = ha; 4785 4786 if (alc != NULL) 4787 free (alc); 4788 4789 return TRUE; 4790 } 4791 4792 /* Array used to determine the number of hash table buckets to use 4793 based on the number of symbols there are. If there are fewer than 4794 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, 4795 fewer than 37 we use 17 buckets, and so forth. We never use more 4796 than 32771 buckets. */ 4797 4798 static const size_t elf_buckets[] = 4799 { 4800 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 4801 16411, 32771, 0 4802 }; 4803 4804 /* Compute bucket count for hashing table. We do not use a static set 4805 of possible tables sizes anymore. Instead we determine for all 4806 possible reasonable sizes of the table the outcome (i.e., the 4807 number of collisions etc) and choose the best solution. The 4808 weighting functions are not too simple to allow the table to grow 4809 without bounds. Instead one of the weighting factors is the size. 4810 Therefore the result is always a good payoff between few collisions 4811 (= short chain lengths) and table size. */ 4812 static size_t 4813 compute_bucket_count (struct bfd_link_info *info) 4814 { 4815 size_t dynsymcount = elf_hash_table (info)->dynsymcount; 4816 size_t best_size = 0; 4817 unsigned long int *hashcodes; 4818 unsigned long int *hashcodesp; 4819 unsigned long int i; 4820 bfd_size_type amt; 4821 4822 /* Compute the hash values for all exported symbols. At the same 4823 time store the values in an array so that we could use them for 4824 optimizations. */ 4825 amt = dynsymcount; 4826 amt *= sizeof (unsigned long int); 4827 hashcodes = bfd_malloc (amt); 4828 if (hashcodes == NULL) 4829 return 0; 4830 hashcodesp = hashcodes; 4831 4832 /* Put all hash values in HASHCODES. */ 4833 elf_link_hash_traverse (elf_hash_table (info), 4834 elf_collect_hash_codes, &hashcodesp); 4835 4836 /* We have a problem here. The following code to optimize the table 4837 size requires an integer type with more the 32 bits. If 4838 BFD_HOST_U_64_BIT is set we know about such a type. */ 4839 #ifdef BFD_HOST_U_64_BIT 4840 if (info->optimize) 4841 { 4842 unsigned long int nsyms = hashcodesp - hashcodes; 4843 size_t minsize; 4844 size_t maxsize; 4845 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); 4846 unsigned long int *counts ; 4847 bfd *dynobj = elf_hash_table (info)->dynobj; 4848 const struct elf_backend_data *bed = get_elf_backend_data (dynobj); 4849 4850 /* Possible optimization parameters: if we have NSYMS symbols we say 4851 that the hashing table must at least have NSYMS/4 and at most 4852 2*NSYMS buckets. */ 4853 minsize = nsyms / 4; 4854 if (minsize == 0) 4855 minsize = 1; 4856 best_size = maxsize = nsyms * 2; 4857 4858 /* Create array where we count the collisions in. We must use bfd_malloc 4859 since the size could be large. */ 4860 amt = maxsize; 4861 amt *= sizeof (unsigned long int); 4862 counts = bfd_malloc (amt); 4863 if (counts == NULL) 4864 { 4865 free (hashcodes); 4866 return 0; 4867 } 4868 4869 /* Compute the "optimal" size for the hash table. The criteria is a 4870 minimal chain length. The minor criteria is (of course) the size 4871 of the table. */ 4872 for (i = minsize; i < maxsize; ++i) 4873 { 4874 /* Walk through the array of hashcodes and count the collisions. */ 4875 BFD_HOST_U_64_BIT max; 4876 unsigned long int j; 4877 unsigned long int fact; 4878 4879 memset (counts, '\0', i * sizeof (unsigned long int)); 4880 4881 /* Determine how often each hash bucket is used. */ 4882 for (j = 0; j < nsyms; ++j) 4883 ++counts[hashcodes[j] % i]; 4884 4885 /* For the weight function we need some information about the 4886 pagesize on the target. This is information need not be 100% 4887 accurate. Since this information is not available (so far) we 4888 define it here to a reasonable default value. If it is crucial 4889 to have a better value some day simply define this value. */ 4890 # ifndef BFD_TARGET_PAGESIZE 4891 # define BFD_TARGET_PAGESIZE (4096) 4892 # endif 4893 4894 /* We in any case need 2 + NSYMS entries for the size values and 4895 the chains. */ 4896 max = (2 + nsyms) * (bed->s->arch_size / 8); 4897 4898 # if 1 4899 /* Variant 1: optimize for short chains. We add the squares 4900 of all the chain lengths (which favors many small chain 4901 over a few long chains). */ 4902 for (j = 0; j < i; ++j) 4903 max += counts[j] * counts[j]; 4904 4905 /* This adds penalties for the overall size of the table. */ 4906 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; 4907 max *= fact * fact; 4908 # else 4909 /* Variant 2: Optimize a lot more for small table. Here we 4910 also add squares of the size but we also add penalties for 4911 empty slots (the +1 term). */ 4912 for (j = 0; j < i; ++j) 4913 max += (1 + counts[j]) * (1 + counts[j]); 4914 4915 /* The overall size of the table is considered, but not as 4916 strong as in variant 1, where it is squared. */ 4917 fact = i / (BFD_TARGET_PAGESIZE / (bed->s->arch_size / 8)) + 1; 4918 max *= fact; 4919 # endif 4920 4921 /* Compare with current best results. */ 4922 if (max < best_chlen) 4923 { 4924 best_chlen = max; 4925 best_size = i; 4926 } 4927 } 4928 4929 free (counts); 4930 } 4931 else 4932 #endif /* defined (BFD_HOST_U_64_BIT) */ 4933 { 4934 /* This is the fallback solution if no 64bit type is available or if we 4935 are not supposed to spend much time on optimizations. We select the 4936 bucket count using a fixed set of numbers. */ 4937 for (i = 0; elf_buckets[i] != 0; i++) 4938 { 4939 best_size = elf_buckets[i]; 4940 if (dynsymcount < elf_buckets[i + 1]) 4941 break; 4942 } 4943 } 4944 4945 /* Free the arrays we needed. */ 4946 free (hashcodes); 4947 4948 return best_size; 4949 } 4950 4951 /* Set up the sizes and contents of the ELF dynamic sections. This is 4952 called by the ELF linker emulation before_allocation routine. We 4953 must set the sizes of the sections before the linker sets the 4954 addresses of the various sections. */ 4955 4956 bfd_boolean 4957 bfd_elf_size_dynamic_sections (bfd *output_bfd, 4958 const char *soname, 4959 const char *rpath, 4960 const char *filter_shlib, 4961 const char * const *auxiliary_filters, 4962 struct bfd_link_info *info, 4963 asection **sinterpptr, 4964 struct bfd_elf_version_tree *verdefs) 4965 { 4966 bfd_size_type soname_indx; 4967 bfd *dynobj; 4968 const struct elf_backend_data *bed; 4969 struct elf_assign_sym_version_info asvinfo; 4970 4971 *sinterpptr = NULL; 4972 4973 soname_indx = (bfd_size_type) -1; 4974 4975 if (!is_elf_hash_table (info->hash)) 4976 return TRUE; 4977 4978 elf_tdata (output_bfd)->relro = info->relro; 4979 elf_tdata (output_bfd)->wxneeded = info->wxneeded; 4980 elf_tdata (output_bfd)->executable = info->executable; 4981 if (info->execstack) 4982 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; 4983 else if (info->noexecstack) 4984 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; 4985 else 4986 { 4987 bfd *inputobj; 4988 asection *notesec = NULL; 4989 int exec = 0; 4990 4991 for (inputobj = info->input_bfds; 4992 inputobj; 4993 inputobj = inputobj->link_next) 4994 { 4995 asection *s; 4996 4997 if (inputobj->flags & (DYNAMIC | BFD_LINKER_CREATED)) 4998 continue; 4999 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); 5000 if (s) 5001 { 5002 if (s->flags & SEC_CODE) 5003 exec = PF_X; 5004 notesec = s; 5005 } 5006 else 5007 exec = PF_X; 5008 } 5009 if (notesec) 5010 { 5011 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; 5012 if (exec && info->relocatable 5013 && notesec->output_section != bfd_abs_section_ptr) 5014 notesec->output_section->flags |= SEC_CODE; 5015 } 5016 } 5017 5018 /* Any syms created from now on start with -1 in 5019 got.refcount/offset and plt.refcount/offset. */ 5020 elf_hash_table (info)->init_got_refcount 5021 = elf_hash_table (info)->init_got_offset; 5022 elf_hash_table (info)->init_plt_refcount 5023 = elf_hash_table (info)->init_plt_offset; 5024 5025 /* The backend may have to create some sections regardless of whether 5026 we're dynamic or not. */ 5027 bed = get_elf_backend_data (output_bfd); 5028 if (bed->elf_backend_always_size_sections 5029 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) 5030 return FALSE; 5031 5032 dynobj = elf_hash_table (info)->dynobj; 5033 5034 /* If there were no dynamic objects in the link, there is nothing to 5035 do here. */ 5036 if (dynobj == NULL) 5037 return TRUE; 5038 5039 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) 5040 return FALSE; 5041 5042 if (elf_hash_table (info)->dynamic_sections_created) 5043 { 5044 struct elf_info_failed eif; 5045 struct elf_link_hash_entry *h; 5046 asection *dynstr; 5047 struct bfd_elf_version_tree *t; 5048 struct bfd_elf_version_expr *d; 5049 asection *s; 5050 bfd_boolean all_defined; 5051 5052 *sinterpptr = bfd_get_section_by_name (dynobj, ".interp"); 5053 5054 if (soname != NULL) 5055 { 5056 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5057 soname, TRUE); 5058 if (soname_indx == (bfd_size_type) -1 5059 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) 5060 return FALSE; 5061 } 5062 5063 if (info->symbolic) 5064 { 5065 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) 5066 return FALSE; 5067 info->flags |= DF_SYMBOLIC; 5068 } 5069 5070 if (rpath != NULL) 5071 { 5072 bfd_size_type indx; 5073 5074 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, 5075 TRUE); 5076 if (indx == (bfd_size_type) -1 5077 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) 5078 return FALSE; 5079 5080 if (info->new_dtags) 5081 { 5082 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); 5083 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) 5084 return FALSE; 5085 } 5086 } 5087 5088 if (filter_shlib != NULL) 5089 { 5090 bfd_size_type indx; 5091 5092 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5093 filter_shlib, TRUE); 5094 if (indx == (bfd_size_type) -1 5095 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) 5096 return FALSE; 5097 } 5098 5099 if (auxiliary_filters != NULL) 5100 { 5101 const char * const *p; 5102 5103 for (p = auxiliary_filters; *p != NULL; p++) 5104 { 5105 bfd_size_type indx; 5106 5107 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5108 *p, TRUE); 5109 if (indx == (bfd_size_type) -1 5110 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) 5111 return FALSE; 5112 } 5113 } 5114 5115 eif.info = info; 5116 eif.verdefs = verdefs; 5117 eif.failed = FALSE; 5118 5119 /* If we are supposed to export all symbols into the dynamic symbol 5120 table (this is not the normal case), then do so. */ 5121 if (info->export_dynamic) 5122 { 5123 elf_link_hash_traverse (elf_hash_table (info), 5124 _bfd_elf_export_symbol, 5125 &eif); 5126 if (eif.failed) 5127 return FALSE; 5128 } 5129 5130 /* Make all global versions with definition. */ 5131 for (t = verdefs; t != NULL; t = t->next) 5132 for (d = t->globals.list; d != NULL; d = d->next) 5133 if (!d->symver && d->symbol) 5134 { 5135 const char *verstr, *name; 5136 size_t namelen, verlen, newlen; 5137 char *newname, *p; 5138 struct elf_link_hash_entry *newh; 5139 5140 name = d->symbol; 5141 namelen = strlen (name); 5142 verstr = t->name; 5143 verlen = strlen (verstr); 5144 newlen = namelen + verlen + 3; 5145 5146 newname = bfd_malloc (newlen); 5147 if (newname == NULL) 5148 return FALSE; 5149 memcpy (newname, name, namelen); 5150 5151 /* Check the hidden versioned definition. */ 5152 p = newname + namelen; 5153 *p++ = ELF_VER_CHR; 5154 memcpy (p, verstr, verlen + 1); 5155 newh = elf_link_hash_lookup (elf_hash_table (info), 5156 newname, FALSE, FALSE, 5157 FALSE); 5158 if (newh == NULL 5159 || (newh->root.type != bfd_link_hash_defined 5160 && newh->root.type != bfd_link_hash_defweak)) 5161 { 5162 /* Check the default versioned definition. */ 5163 *p++ = ELF_VER_CHR; 5164 memcpy (p, verstr, verlen + 1); 5165 newh = elf_link_hash_lookup (elf_hash_table (info), 5166 newname, FALSE, FALSE, 5167 FALSE); 5168 } 5169 free (newname); 5170 5171 /* Mark this version if there is a definition and it is 5172 not defined in a shared object. */ 5173 if (newh != NULL 5174 && !newh->def_dynamic 5175 && (newh->root.type == bfd_link_hash_defined 5176 || newh->root.type == bfd_link_hash_defweak)) 5177 d->symver = 1; 5178 } 5179 5180 /* Attach all the symbols to their version information. */ 5181 asvinfo.output_bfd = output_bfd; 5182 asvinfo.info = info; 5183 asvinfo.verdefs = verdefs; 5184 asvinfo.failed = FALSE; 5185 5186 elf_link_hash_traverse (elf_hash_table (info), 5187 _bfd_elf_link_assign_sym_version, 5188 &asvinfo); 5189 if (asvinfo.failed) 5190 return FALSE; 5191 5192 if (!info->allow_undefined_version) 5193 { 5194 /* Check if all global versions have a definition. */ 5195 all_defined = TRUE; 5196 for (t = verdefs; t != NULL; t = t->next) 5197 for (d = t->globals.list; d != NULL; d = d->next) 5198 if (!d->symver && !d->script) 5199 { 5200 (*_bfd_error_handler) 5201 (_("%s: undefined version: %s"), 5202 d->pattern, t->name); 5203 all_defined = FALSE; 5204 } 5205 5206 if (!all_defined) 5207 { 5208 bfd_set_error (bfd_error_bad_value); 5209 return FALSE; 5210 } 5211 } 5212 5213 /* Find all symbols which were defined in a dynamic object and make 5214 the backend pick a reasonable value for them. */ 5215 elf_link_hash_traverse (elf_hash_table (info), 5216 _bfd_elf_adjust_dynamic_symbol, 5217 &eif); 5218 if (eif.failed) 5219 return FALSE; 5220 5221 /* Add some entries to the .dynamic section. We fill in some of the 5222 values later, in bfd_elf_final_link, but we must add the entries 5223 now so that we know the final size of the .dynamic section. */ 5224 5225 /* If there are initialization and/or finalization functions to 5226 call then add the corresponding DT_INIT/DT_FINI entries. */ 5227 h = (info->init_function 5228 ? elf_link_hash_lookup (elf_hash_table (info), 5229 info->init_function, FALSE, 5230 FALSE, FALSE) 5231 : NULL); 5232 if (h != NULL 5233 && (h->ref_regular 5234 || h->def_regular)) 5235 { 5236 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) 5237 return FALSE; 5238 } 5239 h = (info->fini_function 5240 ? elf_link_hash_lookup (elf_hash_table (info), 5241 info->fini_function, FALSE, 5242 FALSE, FALSE) 5243 : NULL); 5244 if (h != NULL 5245 && (h->ref_regular 5246 || h->def_regular)) 5247 { 5248 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) 5249 return FALSE; 5250 } 5251 5252 s = bfd_get_section_by_name (output_bfd, ".preinit_array"); 5253 if (s != NULL && s->linker_has_input) 5254 { 5255 /* DT_PREINIT_ARRAY is not allowed in shared library. */ 5256 if (! info->executable) 5257 { 5258 bfd *sub; 5259 asection *o; 5260 5261 for (sub = info->input_bfds; sub != NULL; 5262 sub = sub->link_next) 5263 for (o = sub->sections; o != NULL; o = o->next) 5264 if (elf_section_data (o)->this_hdr.sh_type 5265 == SHT_PREINIT_ARRAY) 5266 { 5267 (*_bfd_error_handler) 5268 (_("%B: .preinit_array section is not allowed in DSO"), 5269 sub); 5270 break; 5271 } 5272 5273 bfd_set_error (bfd_error_nonrepresentable_section); 5274 return FALSE; 5275 } 5276 5277 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) 5278 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) 5279 return FALSE; 5280 } 5281 s = bfd_get_section_by_name (output_bfd, ".init_array"); 5282 if (s != NULL && s->linker_has_input) 5283 { 5284 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) 5285 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) 5286 return FALSE; 5287 } 5288 s = bfd_get_section_by_name (output_bfd, ".fini_array"); 5289 if (s != NULL && s->linker_has_input) 5290 { 5291 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) 5292 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) 5293 return FALSE; 5294 } 5295 5296 dynstr = bfd_get_section_by_name (dynobj, ".dynstr"); 5297 /* If .dynstr is excluded from the link, we don't want any of 5298 these tags. Strictly, we should be checking each section 5299 individually; This quick check covers for the case where 5300 someone does a /DISCARD/ : { *(*) }. */ 5301 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) 5302 { 5303 bfd_size_type strsize; 5304 5305 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 5306 if (!_bfd_elf_add_dynamic_entry (info, DT_HASH, 0) 5307 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) 5308 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) 5309 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) 5310 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, 5311 bed->s->sizeof_sym)) 5312 return FALSE; 5313 } 5314 } 5315 5316 /* The backend must work out the sizes of all the other dynamic 5317 sections. */ 5318 if (bed->elf_backend_size_dynamic_sections 5319 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) 5320 return FALSE; 5321 5322 if (elf_hash_table (info)->dynamic_sections_created) 5323 { 5324 unsigned long section_sym_count; 5325 asection *s; 5326 5327 /* Set up the version definition section. */ 5328 s = bfd_get_section_by_name (dynobj, ".gnu.version_d"); 5329 BFD_ASSERT (s != NULL); 5330 5331 /* We may have created additional version definitions if we are 5332 just linking a regular application. */ 5333 verdefs = asvinfo.verdefs; 5334 5335 /* Skip anonymous version tag. */ 5336 if (verdefs != NULL && verdefs->vernum == 0) 5337 verdefs = verdefs->next; 5338 5339 if (verdefs == NULL && !info->create_default_symver) 5340 s->flags |= SEC_EXCLUDE; 5341 else 5342 { 5343 unsigned int cdefs; 5344 bfd_size_type size; 5345 struct bfd_elf_version_tree *t; 5346 bfd_byte *p; 5347 Elf_Internal_Verdef def; 5348 Elf_Internal_Verdaux defaux; 5349 struct bfd_link_hash_entry *bh; 5350 struct elf_link_hash_entry *h; 5351 const char *name; 5352 5353 cdefs = 0; 5354 size = 0; 5355 5356 /* Make space for the base version. */ 5357 size += sizeof (Elf_External_Verdef); 5358 size += sizeof (Elf_External_Verdaux); 5359 ++cdefs; 5360 5361 /* Make space for the default version. */ 5362 if (info->create_default_symver) 5363 { 5364 size += sizeof (Elf_External_Verdef); 5365 ++cdefs; 5366 } 5367 5368 for (t = verdefs; t != NULL; t = t->next) 5369 { 5370 struct bfd_elf_version_deps *n; 5371 5372 size += sizeof (Elf_External_Verdef); 5373 size += sizeof (Elf_External_Verdaux); 5374 ++cdefs; 5375 5376 for (n = t->deps; n != NULL; n = n->next) 5377 size += sizeof (Elf_External_Verdaux); 5378 } 5379 5380 s->size = size; 5381 s->contents = bfd_alloc (output_bfd, s->size); 5382 if (s->contents == NULL && s->size != 0) 5383 return FALSE; 5384 5385 /* Fill in the version definition section. */ 5386 5387 p = s->contents; 5388 5389 def.vd_version = VER_DEF_CURRENT; 5390 def.vd_flags = VER_FLG_BASE; 5391 def.vd_ndx = 1; 5392 def.vd_cnt = 1; 5393 if (info->create_default_symver) 5394 { 5395 def.vd_aux = 2 * sizeof (Elf_External_Verdef); 5396 def.vd_next = sizeof (Elf_External_Verdef); 5397 } 5398 else 5399 { 5400 def.vd_aux = sizeof (Elf_External_Verdef); 5401 def.vd_next = (sizeof (Elf_External_Verdef) 5402 + sizeof (Elf_External_Verdaux)); 5403 } 5404 5405 if (soname_indx != (bfd_size_type) -1) 5406 { 5407 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 5408 soname_indx); 5409 def.vd_hash = bfd_elf_hash (soname); 5410 defaux.vda_name = soname_indx; 5411 name = soname; 5412 } 5413 else 5414 { 5415 bfd_size_type indx; 5416 5417 name = lbasename (output_bfd->filename); 5418 def.vd_hash = bfd_elf_hash (name); 5419 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5420 name, FALSE); 5421 if (indx == (bfd_size_type) -1) 5422 return FALSE; 5423 defaux.vda_name = indx; 5424 } 5425 defaux.vda_next = 0; 5426 5427 _bfd_elf_swap_verdef_out (output_bfd, &def, 5428 (Elf_External_Verdef *) p); 5429 p += sizeof (Elf_External_Verdef); 5430 if (info->create_default_symver) 5431 { 5432 /* Add a symbol representing this version. */ 5433 bh = NULL; 5434 if (! (_bfd_generic_link_add_one_symbol 5435 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, 5436 0, NULL, FALSE, 5437 get_elf_backend_data (dynobj)->collect, &bh))) 5438 return FALSE; 5439 h = (struct elf_link_hash_entry *) bh; 5440 h->non_elf = 0; 5441 h->def_regular = 1; 5442 h->type = STT_OBJECT; 5443 h->verinfo.vertree = NULL; 5444 5445 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 5446 return FALSE; 5447 5448 /* Create a duplicate of the base version with the same 5449 aux block, but different flags. */ 5450 def.vd_flags = 0; 5451 def.vd_ndx = 2; 5452 def.vd_aux = sizeof (Elf_External_Verdef); 5453 if (verdefs) 5454 def.vd_next = (sizeof (Elf_External_Verdef) 5455 + sizeof (Elf_External_Verdaux)); 5456 else 5457 def.vd_next = 0; 5458 _bfd_elf_swap_verdef_out (output_bfd, &def, 5459 (Elf_External_Verdef *) p); 5460 p += sizeof (Elf_External_Verdef); 5461 } 5462 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 5463 (Elf_External_Verdaux *) p); 5464 p += sizeof (Elf_External_Verdaux); 5465 5466 for (t = verdefs; t != NULL; t = t->next) 5467 { 5468 unsigned int cdeps; 5469 struct bfd_elf_version_deps *n; 5470 5471 cdeps = 0; 5472 for (n = t->deps; n != NULL; n = n->next) 5473 ++cdeps; 5474 5475 /* Add a symbol representing this version. */ 5476 bh = NULL; 5477 if (! (_bfd_generic_link_add_one_symbol 5478 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, 5479 0, NULL, FALSE, 5480 get_elf_backend_data (dynobj)->collect, &bh))) 5481 return FALSE; 5482 h = (struct elf_link_hash_entry *) bh; 5483 h->non_elf = 0; 5484 h->def_regular = 1; 5485 h->type = STT_OBJECT; 5486 h->verinfo.vertree = t; 5487 5488 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 5489 return FALSE; 5490 5491 def.vd_version = VER_DEF_CURRENT; 5492 def.vd_flags = 0; 5493 if (t->globals.list == NULL 5494 && t->locals.list == NULL 5495 && ! t->used) 5496 def.vd_flags |= VER_FLG_WEAK; 5497 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); 5498 def.vd_cnt = cdeps + 1; 5499 def.vd_hash = bfd_elf_hash (t->name); 5500 def.vd_aux = sizeof (Elf_External_Verdef); 5501 def.vd_next = 0; 5502 if (t->next != NULL) 5503 def.vd_next = (sizeof (Elf_External_Verdef) 5504 + (cdeps + 1) * sizeof (Elf_External_Verdaux)); 5505 5506 _bfd_elf_swap_verdef_out (output_bfd, &def, 5507 (Elf_External_Verdef *) p); 5508 p += sizeof (Elf_External_Verdef); 5509 5510 defaux.vda_name = h->dynstr_index; 5511 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 5512 h->dynstr_index); 5513 defaux.vda_next = 0; 5514 if (t->deps != NULL) 5515 defaux.vda_next = sizeof (Elf_External_Verdaux); 5516 t->name_indx = defaux.vda_name; 5517 5518 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 5519 (Elf_External_Verdaux *) p); 5520 p += sizeof (Elf_External_Verdaux); 5521 5522 for (n = t->deps; n != NULL; n = n->next) 5523 { 5524 if (n->version_needed == NULL) 5525 { 5526 /* This can happen if there was an error in the 5527 version script. */ 5528 defaux.vda_name = 0; 5529 } 5530 else 5531 { 5532 defaux.vda_name = n->version_needed->name_indx; 5533 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 5534 defaux.vda_name); 5535 } 5536 if (n->next == NULL) 5537 defaux.vda_next = 0; 5538 else 5539 defaux.vda_next = sizeof (Elf_External_Verdaux); 5540 5541 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 5542 (Elf_External_Verdaux *) p); 5543 p += sizeof (Elf_External_Verdaux); 5544 } 5545 } 5546 5547 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) 5548 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) 5549 return FALSE; 5550 5551 elf_tdata (output_bfd)->cverdefs = cdefs; 5552 } 5553 5554 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) 5555 { 5556 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) 5557 return FALSE; 5558 } 5559 else if (info->flags & DF_BIND_NOW) 5560 { 5561 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) 5562 return FALSE; 5563 } 5564 5565 if (info->flags_1) 5566 { 5567 if (info->executable) 5568 info->flags_1 &= ~ (DF_1_INITFIRST 5569 | DF_1_NODELETE 5570 | DF_1_NOOPEN); 5571 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) 5572 return FALSE; 5573 } 5574 5575 /* Work out the size of the version reference section. */ 5576 5577 s = bfd_get_section_by_name (dynobj, ".gnu.version_r"); 5578 BFD_ASSERT (s != NULL); 5579 { 5580 struct elf_find_verdep_info sinfo; 5581 5582 sinfo.output_bfd = output_bfd; 5583 sinfo.info = info; 5584 sinfo.vers = elf_tdata (output_bfd)->cverdefs; 5585 if (sinfo.vers == 0) 5586 sinfo.vers = 1; 5587 sinfo.failed = FALSE; 5588 5589 elf_link_hash_traverse (elf_hash_table (info), 5590 _bfd_elf_link_find_version_dependencies, 5591 &sinfo); 5592 5593 if (elf_tdata (output_bfd)->verref == NULL) 5594 s->flags |= SEC_EXCLUDE; 5595 else 5596 { 5597 Elf_Internal_Verneed *t; 5598 unsigned int size; 5599 unsigned int crefs; 5600 bfd_byte *p; 5601 5602 /* Build the version definition section. */ 5603 size = 0; 5604 crefs = 0; 5605 for (t = elf_tdata (output_bfd)->verref; 5606 t != NULL; 5607 t = t->vn_nextref) 5608 { 5609 Elf_Internal_Vernaux *a; 5610 5611 size += sizeof (Elf_External_Verneed); 5612 ++crefs; 5613 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 5614 size += sizeof (Elf_External_Vernaux); 5615 } 5616 5617 s->size = size; 5618 s->contents = bfd_alloc (output_bfd, s->size); 5619 if (s->contents == NULL) 5620 return FALSE; 5621 5622 p = s->contents; 5623 for (t = elf_tdata (output_bfd)->verref; 5624 t != NULL; 5625 t = t->vn_nextref) 5626 { 5627 unsigned int caux; 5628 Elf_Internal_Vernaux *a; 5629 bfd_size_type indx; 5630 5631 caux = 0; 5632 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 5633 ++caux; 5634 5635 t->vn_version = VER_NEED_CURRENT; 5636 t->vn_cnt = caux; 5637 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5638 elf_dt_name (t->vn_bfd) != NULL 5639 ? elf_dt_name (t->vn_bfd) 5640 : lbasename (t->vn_bfd->filename), 5641 FALSE); 5642 if (indx == (bfd_size_type) -1) 5643 return FALSE; 5644 t->vn_file = indx; 5645 t->vn_aux = sizeof (Elf_External_Verneed); 5646 if (t->vn_nextref == NULL) 5647 t->vn_next = 0; 5648 else 5649 t->vn_next = (sizeof (Elf_External_Verneed) 5650 + caux * sizeof (Elf_External_Vernaux)); 5651 5652 _bfd_elf_swap_verneed_out (output_bfd, t, 5653 (Elf_External_Verneed *) p); 5654 p += sizeof (Elf_External_Verneed); 5655 5656 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 5657 { 5658 a->vna_hash = bfd_elf_hash (a->vna_nodename); 5659 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5660 a->vna_nodename, FALSE); 5661 if (indx == (bfd_size_type) -1) 5662 return FALSE; 5663 a->vna_name = indx; 5664 if (a->vna_nextptr == NULL) 5665 a->vna_next = 0; 5666 else 5667 a->vna_next = sizeof (Elf_External_Vernaux); 5668 5669 _bfd_elf_swap_vernaux_out (output_bfd, a, 5670 (Elf_External_Vernaux *) p); 5671 p += sizeof (Elf_External_Vernaux); 5672 } 5673 } 5674 5675 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) 5676 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) 5677 return FALSE; 5678 5679 elf_tdata (output_bfd)->cverrefs = crefs; 5680 } 5681 } 5682 5683 if ((elf_tdata (output_bfd)->cverrefs == 0 5684 && elf_tdata (output_bfd)->cverdefs == 0) 5685 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, 5686 §ion_sym_count) == 0) 5687 { 5688 s = bfd_get_section_by_name (dynobj, ".gnu.version"); 5689 s->flags |= SEC_EXCLUDE; 5690 } 5691 } 5692 return TRUE; 5693 } 5694 5695 bfd_boolean 5696 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) 5697 { 5698 if (!is_elf_hash_table (info->hash)) 5699 return TRUE; 5700 5701 if (elf_hash_table (info)->dynamic_sections_created) 5702 { 5703 bfd *dynobj; 5704 const struct elf_backend_data *bed; 5705 asection *s; 5706 bfd_size_type dynsymcount; 5707 unsigned long section_sym_count; 5708 size_t bucketcount = 0; 5709 size_t hash_entry_size; 5710 unsigned int dtagcount; 5711 5712 dynobj = elf_hash_table (info)->dynobj; 5713 5714 /* Assign dynsym indicies. In a shared library we generate a 5715 section symbol for each output section, which come first. 5716 Next come all of the back-end allocated local dynamic syms, 5717 followed by the rest of the global symbols. */ 5718 5719 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, 5720 §ion_sym_count); 5721 5722 /* Work out the size of the symbol version section. */ 5723 s = bfd_get_section_by_name (dynobj, ".gnu.version"); 5724 BFD_ASSERT (s != NULL); 5725 if (dynsymcount != 0 5726 && (s->flags & SEC_EXCLUDE) == 0) 5727 { 5728 s->size = dynsymcount * sizeof (Elf_External_Versym); 5729 s->contents = bfd_zalloc (output_bfd, s->size); 5730 if (s->contents == NULL) 5731 return FALSE; 5732 5733 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) 5734 return FALSE; 5735 } 5736 5737 /* Set the size of the .dynsym and .hash sections. We counted 5738 the number of dynamic symbols in elf_link_add_object_symbols. 5739 We will build the contents of .dynsym and .hash when we build 5740 the final symbol table, because until then we do not know the 5741 correct value to give the symbols. We built the .dynstr 5742 section as we went along in elf_link_add_object_symbols. */ 5743 s = bfd_get_section_by_name (dynobj, ".dynsym"); 5744 BFD_ASSERT (s != NULL); 5745 bed = get_elf_backend_data (output_bfd); 5746 s->size = dynsymcount * bed->s->sizeof_sym; 5747 5748 if (dynsymcount != 0) 5749 { 5750 s->contents = bfd_alloc (output_bfd, s->size); 5751 if (s->contents == NULL) 5752 return FALSE; 5753 5754 /* The first entry in .dynsym is a dummy symbol. 5755 Clear all the section syms, in case we don't output them all. */ 5756 ++section_sym_count; 5757 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); 5758 } 5759 5760 /* Compute the size of the hashing table. As a side effect this 5761 computes the hash values for all the names we export. */ 5762 bucketcount = compute_bucket_count (info); 5763 5764 s = bfd_get_section_by_name (dynobj, ".hash"); 5765 BFD_ASSERT (s != NULL); 5766 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; 5767 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); 5768 s->contents = bfd_zalloc (output_bfd, s->size); 5769 if (s->contents == NULL) 5770 return FALSE; 5771 5772 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); 5773 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, 5774 s->contents + hash_entry_size); 5775 5776 elf_hash_table (info)->bucketcount = bucketcount; 5777 5778 s = bfd_get_section_by_name (dynobj, ".dynstr"); 5779 BFD_ASSERT (s != NULL); 5780 5781 elf_finalize_dynstr (output_bfd, info); 5782 5783 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 5784 5785 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) 5786 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) 5787 return FALSE; 5788 } 5789 5790 return TRUE; 5791 } 5792 5793 /* Final phase of ELF linker. */ 5794 5795 /* A structure we use to avoid passing large numbers of arguments. */ 5796 5797 struct elf_final_link_info 5798 { 5799 /* General link information. */ 5800 struct bfd_link_info *info; 5801 /* Output BFD. */ 5802 bfd *output_bfd; 5803 /* Symbol string table. */ 5804 struct bfd_strtab_hash *symstrtab; 5805 /* .dynsym section. */ 5806 asection *dynsym_sec; 5807 /* .hash section. */ 5808 asection *hash_sec; 5809 /* symbol version section (.gnu.version). */ 5810 asection *symver_sec; 5811 /* Buffer large enough to hold contents of any section. */ 5812 bfd_byte *contents; 5813 /* Buffer large enough to hold external relocs of any section. */ 5814 void *external_relocs; 5815 /* Buffer large enough to hold internal relocs of any section. */ 5816 Elf_Internal_Rela *internal_relocs; 5817 /* Buffer large enough to hold external local symbols of any input 5818 BFD. */ 5819 bfd_byte *external_syms; 5820 /* And a buffer for symbol section indices. */ 5821 Elf_External_Sym_Shndx *locsym_shndx; 5822 /* Buffer large enough to hold internal local symbols of any input 5823 BFD. */ 5824 Elf_Internal_Sym *internal_syms; 5825 /* Array large enough to hold a symbol index for each local symbol 5826 of any input BFD. */ 5827 long *indices; 5828 /* Array large enough to hold a section pointer for each local 5829 symbol of any input BFD. */ 5830 asection **sections; 5831 /* Buffer to hold swapped out symbols. */ 5832 bfd_byte *symbuf; 5833 /* And one for symbol section indices. */ 5834 Elf_External_Sym_Shndx *symshndxbuf; 5835 /* Number of swapped out symbols in buffer. */ 5836 size_t symbuf_count; 5837 /* Number of symbols which fit in symbuf. */ 5838 size_t symbuf_size; 5839 /* And same for symshndxbuf. */ 5840 size_t shndxbuf_size; 5841 }; 5842 5843 /* This struct is used to pass information to elf_link_output_extsym. */ 5844 5845 struct elf_outext_info 5846 { 5847 bfd_boolean failed; 5848 bfd_boolean localsyms; 5849 struct elf_final_link_info *finfo; 5850 }; 5851 5852 /* When performing a relocatable link, the input relocations are 5853 preserved. But, if they reference global symbols, the indices 5854 referenced must be updated. Update all the relocations in 5855 REL_HDR (there are COUNT of them), using the data in REL_HASH. */ 5856 5857 static void 5858 elf_link_adjust_relocs (bfd *abfd, 5859 Elf_Internal_Shdr *rel_hdr, 5860 unsigned int count, 5861 struct elf_link_hash_entry **rel_hash) 5862 { 5863 unsigned int i; 5864 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5865 bfd_byte *erela; 5866 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 5867 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 5868 bfd_vma r_type_mask; 5869 int r_sym_shift; 5870 5871 if (rel_hdr->sh_entsize == bed->s->sizeof_rel) 5872 { 5873 swap_in = bed->s->swap_reloc_in; 5874 swap_out = bed->s->swap_reloc_out; 5875 } 5876 else if (rel_hdr->sh_entsize == bed->s->sizeof_rela) 5877 { 5878 swap_in = bed->s->swap_reloca_in; 5879 swap_out = bed->s->swap_reloca_out; 5880 } 5881 else 5882 abort (); 5883 5884 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) 5885 abort (); 5886 5887 if (bed->s->arch_size == 32) 5888 { 5889 r_type_mask = 0xff; 5890 r_sym_shift = 8; 5891 } 5892 else 5893 { 5894 r_type_mask = 0xffffffff; 5895 r_sym_shift = 32; 5896 } 5897 5898 erela = rel_hdr->contents; 5899 for (i = 0; i < count; i++, rel_hash++, erela += rel_hdr->sh_entsize) 5900 { 5901 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; 5902 unsigned int j; 5903 5904 if (*rel_hash == NULL) 5905 continue; 5906 5907 BFD_ASSERT ((*rel_hash)->indx >= 0); 5908 5909 (*swap_in) (abfd, erela, irela); 5910 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) 5911 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift 5912 | (irela[j].r_info & r_type_mask)); 5913 (*swap_out) (abfd, irela, erela); 5914 } 5915 } 5916 5917 struct elf_link_sort_rela 5918 { 5919 union { 5920 bfd_vma offset; 5921 bfd_vma sym_mask; 5922 } u; 5923 enum elf_reloc_type_class type; 5924 /* We use this as an array of size int_rels_per_ext_rel. */ 5925 Elf_Internal_Rela rela[1]; 5926 }; 5927 5928 static int 5929 elf_link_sort_cmp1 (const void *A, const void *B) 5930 { 5931 const struct elf_link_sort_rela *a = A; 5932 const struct elf_link_sort_rela *b = B; 5933 int relativea, relativeb; 5934 5935 relativea = a->type == reloc_class_relative; 5936 relativeb = b->type == reloc_class_relative; 5937 5938 if (relativea < relativeb) 5939 return 1; 5940 if (relativea > relativeb) 5941 return -1; 5942 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) 5943 return -1; 5944 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) 5945 return 1; 5946 if (a->rela->r_offset < b->rela->r_offset) 5947 return -1; 5948 if (a->rela->r_offset > b->rela->r_offset) 5949 return 1; 5950 return 0; 5951 } 5952 5953 static int 5954 elf_link_sort_cmp2 (const void *A, const void *B) 5955 { 5956 const struct elf_link_sort_rela *a = A; 5957 const struct elf_link_sort_rela *b = B; 5958 int copya, copyb; 5959 5960 if (a->u.offset < b->u.offset) 5961 return -1; 5962 if (a->u.offset > b->u.offset) 5963 return 1; 5964 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); 5965 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); 5966 if (copya < copyb) 5967 return -1; 5968 if (copya > copyb) 5969 return 1; 5970 if (a->rela->r_offset < b->rela->r_offset) 5971 return -1; 5972 if (a->rela->r_offset > b->rela->r_offset) 5973 return 1; 5974 return 0; 5975 } 5976 5977 static size_t 5978 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) 5979 { 5980 asection *reldyn; 5981 bfd_size_type count, size; 5982 size_t i, ret, sort_elt, ext_size; 5983 bfd_byte *sort, *s_non_relative, *p; 5984 struct elf_link_sort_rela *sq; 5985 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 5986 int i2e = bed->s->int_rels_per_ext_rel; 5987 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 5988 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 5989 struct bfd_link_order *lo; 5990 bfd_vma r_sym_mask; 5991 5992 reldyn = bfd_get_section_by_name (abfd, ".rela.dyn"); 5993 if (reldyn == NULL || reldyn->size == 0) 5994 { 5995 reldyn = bfd_get_section_by_name (abfd, ".rel.dyn"); 5996 if (reldyn == NULL || reldyn->size == 0) 5997 return 0; 5998 ext_size = bed->s->sizeof_rel; 5999 swap_in = bed->s->swap_reloc_in; 6000 swap_out = bed->s->swap_reloc_out; 6001 } 6002 else 6003 { 6004 ext_size = bed->s->sizeof_rela; 6005 swap_in = bed->s->swap_reloca_in; 6006 swap_out = bed->s->swap_reloca_out; 6007 } 6008 count = reldyn->size / ext_size; 6009 6010 size = 0; 6011 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) 6012 if (lo->type == bfd_indirect_link_order) 6013 { 6014 asection *o = lo->u.indirect.section; 6015 size += o->size; 6016 } 6017 6018 if (size != reldyn->size) 6019 return 0; 6020 6021 sort_elt = (sizeof (struct elf_link_sort_rela) 6022 + (i2e - 1) * sizeof (Elf_Internal_Rela)); 6023 sort = bfd_zmalloc (sort_elt * count); 6024 if (sort == NULL) 6025 { 6026 (*info->callbacks->warning) 6027 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); 6028 return 0; 6029 } 6030 6031 if (bed->s->arch_size == 32) 6032 r_sym_mask = ~(bfd_vma) 0xff; 6033 else 6034 r_sym_mask = ~(bfd_vma) 0xffffffff; 6035 6036 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) 6037 if (lo->type == bfd_indirect_link_order) 6038 { 6039 bfd_byte *erel, *erelend; 6040 asection *o = lo->u.indirect.section; 6041 6042 if (o->contents == NULL && o->size != 0) 6043 { 6044 /* This is a reloc section that is being handled as a normal 6045 section. See bfd_section_from_shdr. We can't combine 6046 relocs in this case. */ 6047 free (sort); 6048 return 0; 6049 } 6050 erel = o->contents; 6051 erelend = o->contents + o->size; 6052 p = sort + o->output_offset / ext_size * sort_elt; 6053 while (erel < erelend) 6054 { 6055 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 6056 (*swap_in) (abfd, erel, s->rela); 6057 s->type = (*bed->elf_backend_reloc_type_class) (s->rela); 6058 s->u.sym_mask = r_sym_mask; 6059 p += sort_elt; 6060 erel += ext_size; 6061 } 6062 } 6063 6064 qsort (sort, count, sort_elt, elf_link_sort_cmp1); 6065 6066 for (i = 0, p = sort; i < count; i++, p += sort_elt) 6067 { 6068 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 6069 if (s->type != reloc_class_relative) 6070 break; 6071 } 6072 ret = i; 6073 s_non_relative = p; 6074 6075 sq = (struct elf_link_sort_rela *) s_non_relative; 6076 for (; i < count; i++, p += sort_elt) 6077 { 6078 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; 6079 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) 6080 sq = sp; 6081 sp->u.offset = sq->rela->r_offset; 6082 } 6083 6084 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); 6085 6086 for (lo = reldyn->map_head.link_order; lo != NULL; lo = lo->next) 6087 if (lo->type == bfd_indirect_link_order) 6088 { 6089 bfd_byte *erel, *erelend; 6090 asection *o = lo->u.indirect.section; 6091 6092 erel = o->contents; 6093 erelend = o->contents + o->size; 6094 p = sort + o->output_offset / ext_size * sort_elt; 6095 while (erel < erelend) 6096 { 6097 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 6098 (*swap_out) (abfd, s->rela, erel); 6099 p += sort_elt; 6100 erel += ext_size; 6101 } 6102 } 6103 6104 free (sort); 6105 *psec = reldyn; 6106 return ret; 6107 } 6108 6109 /* Flush the output symbols to the file. */ 6110 6111 static bfd_boolean 6112 elf_link_flush_output_syms (struct elf_final_link_info *finfo, 6113 const struct elf_backend_data *bed) 6114 { 6115 if (finfo->symbuf_count > 0) 6116 { 6117 Elf_Internal_Shdr *hdr; 6118 file_ptr pos; 6119 bfd_size_type amt; 6120 6121 hdr = &elf_tdata (finfo->output_bfd)->symtab_hdr; 6122 pos = hdr->sh_offset + hdr->sh_size; 6123 amt = finfo->symbuf_count * bed->s->sizeof_sym; 6124 if (bfd_seek (finfo->output_bfd, pos, SEEK_SET) != 0 6125 || bfd_bwrite (finfo->symbuf, amt, finfo->output_bfd) != amt) 6126 return FALSE; 6127 6128 hdr->sh_size += amt; 6129 finfo->symbuf_count = 0; 6130 } 6131 6132 return TRUE; 6133 } 6134 6135 /* Add a symbol to the output symbol table. */ 6136 6137 static bfd_boolean 6138 elf_link_output_sym (struct elf_final_link_info *finfo, 6139 const char *name, 6140 Elf_Internal_Sym *elfsym, 6141 asection *input_sec, 6142 struct elf_link_hash_entry *h) 6143 { 6144 bfd_byte *dest; 6145 Elf_External_Sym_Shndx *destshndx; 6146 bfd_boolean (*output_symbol_hook) 6147 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, 6148 struct elf_link_hash_entry *); 6149 const struct elf_backend_data *bed; 6150 6151 bed = get_elf_backend_data (finfo->output_bfd); 6152 output_symbol_hook = bed->elf_backend_link_output_symbol_hook; 6153 if (output_symbol_hook != NULL) 6154 { 6155 if (! (*output_symbol_hook) (finfo->info, name, elfsym, input_sec, h)) 6156 return FALSE; 6157 } 6158 6159 if (name == NULL || *name == '\0') 6160 elfsym->st_name = 0; 6161 else if (input_sec->flags & SEC_EXCLUDE) 6162 elfsym->st_name = 0; 6163 else 6164 { 6165 elfsym->st_name = (unsigned long) _bfd_stringtab_add (finfo->symstrtab, 6166 name, TRUE, FALSE); 6167 if (elfsym->st_name == (unsigned long) -1) 6168 return FALSE; 6169 } 6170 6171 if (finfo->symbuf_count >= finfo->symbuf_size) 6172 { 6173 if (! elf_link_flush_output_syms (finfo, bed)) 6174 return FALSE; 6175 } 6176 6177 dest = finfo->symbuf + finfo->symbuf_count * bed->s->sizeof_sym; 6178 destshndx = finfo->symshndxbuf; 6179 if (destshndx != NULL) 6180 { 6181 if (bfd_get_symcount (finfo->output_bfd) >= finfo->shndxbuf_size) 6182 { 6183 bfd_size_type amt; 6184 6185 amt = finfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); 6186 finfo->symshndxbuf = destshndx = bfd_realloc (destshndx, amt * 2); 6187 if (destshndx == NULL) 6188 return FALSE; 6189 memset ((char *) destshndx + amt, 0, amt); 6190 finfo->shndxbuf_size *= 2; 6191 } 6192 destshndx += bfd_get_symcount (finfo->output_bfd); 6193 } 6194 6195 bed->s->swap_symbol_out (finfo->output_bfd, elfsym, dest, destshndx); 6196 finfo->symbuf_count += 1; 6197 bfd_get_symcount (finfo->output_bfd) += 1; 6198 6199 return TRUE; 6200 } 6201 6202 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ 6203 6204 static bfd_boolean 6205 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) 6206 { 6207 if (sym->st_shndx > SHN_HIRESERVE) 6208 { 6209 /* The gABI doesn't support dynamic symbols in output sections 6210 beyond 64k. */ 6211 (*_bfd_error_handler) 6212 (_("%B: Too many sections: %d (>= %d)"), 6213 abfd, bfd_count_sections (abfd), SHN_LORESERVE); 6214 bfd_set_error (bfd_error_nonrepresentable_section); 6215 return FALSE; 6216 } 6217 return TRUE; 6218 } 6219 6220 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in 6221 allowing an unsatisfied unversioned symbol in the DSO to match a 6222 versioned symbol that would normally require an explicit version. 6223 We also handle the case that a DSO references a hidden symbol 6224 which may be satisfied by a versioned symbol in another DSO. */ 6225 6226 static bfd_boolean 6227 elf_link_check_versioned_symbol (struct bfd_link_info *info, 6228 const struct elf_backend_data *bed, 6229 struct elf_link_hash_entry *h) 6230 { 6231 bfd *abfd; 6232 struct elf_link_loaded_list *loaded; 6233 6234 if (!is_elf_hash_table (info->hash)) 6235 return FALSE; 6236 6237 switch (h->root.type) 6238 { 6239 default: 6240 abfd = NULL; 6241 break; 6242 6243 case bfd_link_hash_undefined: 6244 case bfd_link_hash_undefweak: 6245 abfd = h->root.u.undef.abfd; 6246 if ((abfd->flags & DYNAMIC) == 0 6247 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) 6248 return FALSE; 6249 break; 6250 6251 case bfd_link_hash_defined: 6252 case bfd_link_hash_defweak: 6253 abfd = h->root.u.def.section->owner; 6254 break; 6255 6256 case bfd_link_hash_common: 6257 abfd = h->root.u.c.p->section->owner; 6258 break; 6259 } 6260 BFD_ASSERT (abfd != NULL); 6261 6262 for (loaded = elf_hash_table (info)->loaded; 6263 loaded != NULL; 6264 loaded = loaded->next) 6265 { 6266 bfd *input; 6267 Elf_Internal_Shdr *hdr; 6268 bfd_size_type symcount; 6269 bfd_size_type extsymcount; 6270 bfd_size_type extsymoff; 6271 Elf_Internal_Shdr *versymhdr; 6272 Elf_Internal_Sym *isym; 6273 Elf_Internal_Sym *isymend; 6274 Elf_Internal_Sym *isymbuf; 6275 Elf_External_Versym *ever; 6276 Elf_External_Versym *extversym; 6277 6278 input = loaded->abfd; 6279 6280 /* We check each DSO for a possible hidden versioned definition. */ 6281 if (input == abfd 6282 || (input->flags & DYNAMIC) == 0 6283 || elf_dynversym (input) == 0) 6284 continue; 6285 6286 hdr = &elf_tdata (input)->dynsymtab_hdr; 6287 6288 symcount = hdr->sh_size / bed->s->sizeof_sym; 6289 if (elf_bad_symtab (input)) 6290 { 6291 extsymcount = symcount; 6292 extsymoff = 0; 6293 } 6294 else 6295 { 6296 extsymcount = symcount - hdr->sh_info; 6297 extsymoff = hdr->sh_info; 6298 } 6299 6300 if (extsymcount == 0) 6301 continue; 6302 6303 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, 6304 NULL, NULL, NULL); 6305 if (isymbuf == NULL) 6306 return FALSE; 6307 6308 /* Read in any version definitions. */ 6309 versymhdr = &elf_tdata (input)->dynversym_hdr; 6310 extversym = bfd_malloc (versymhdr->sh_size); 6311 if (extversym == NULL) 6312 goto error_ret; 6313 6314 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 6315 || (bfd_bread (extversym, versymhdr->sh_size, input) 6316 != versymhdr->sh_size)) 6317 { 6318 free (extversym); 6319 error_ret: 6320 free (isymbuf); 6321 return FALSE; 6322 } 6323 6324 ever = extversym + extsymoff; 6325 isymend = isymbuf + extsymcount; 6326 for (isym = isymbuf; isym < isymend; isym++, ever++) 6327 { 6328 const char *name; 6329 Elf_Internal_Versym iver; 6330 unsigned short version_index; 6331 6332 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL 6333 || isym->st_shndx == SHN_UNDEF) 6334 continue; 6335 6336 name = bfd_elf_string_from_elf_section (input, 6337 hdr->sh_link, 6338 isym->st_name); 6339 if (strcmp (name, h->root.root.string) != 0) 6340 continue; 6341 6342 _bfd_elf_swap_versym_in (input, ever, &iver); 6343 6344 if ((iver.vs_vers & VERSYM_HIDDEN) == 0) 6345 { 6346 /* If we have a non-hidden versioned sym, then it should 6347 have provided a definition for the undefined sym. */ 6348 abort (); 6349 } 6350 6351 version_index = iver.vs_vers & VERSYM_VERSION; 6352 if (version_index == 1 || version_index == 2) 6353 { 6354 /* This is the base or first version. We can use it. */ 6355 free (extversym); 6356 free (isymbuf); 6357 return TRUE; 6358 } 6359 } 6360 6361 free (extversym); 6362 free (isymbuf); 6363 } 6364 6365 return FALSE; 6366 } 6367 6368 /* Add an external symbol to the symbol table. This is called from 6369 the hash table traversal routine. When generating a shared object, 6370 we go through the symbol table twice. The first time we output 6371 anything that might have been forced to local scope in a version 6372 script. The second time we output the symbols that are still 6373 global symbols. */ 6374 6375 static bfd_boolean 6376 elf_link_output_extsym (struct elf_link_hash_entry *h, void *data) 6377 { 6378 struct elf_outext_info *eoinfo = data; 6379 struct elf_final_link_info *finfo = eoinfo->finfo; 6380 bfd_boolean strip; 6381 Elf_Internal_Sym sym; 6382 asection *input_sec; 6383 const struct elf_backend_data *bed; 6384 6385 if (h->root.type == bfd_link_hash_warning) 6386 { 6387 h = (struct elf_link_hash_entry *) h->root.u.i.link; 6388 if (h->root.type == bfd_link_hash_new) 6389 return TRUE; 6390 } 6391 6392 /* Decide whether to output this symbol in this pass. */ 6393 if (eoinfo->localsyms) 6394 { 6395 if (!h->forced_local) 6396 return TRUE; 6397 } 6398 else 6399 { 6400 if (h->forced_local) 6401 return TRUE; 6402 } 6403 6404 bed = get_elf_backend_data (finfo->output_bfd); 6405 6406 if (h->root.type == bfd_link_hash_undefined) 6407 { 6408 /* If we have an undefined symbol reference here then it must have 6409 come from a shared library that is being linked in. (Undefined 6410 references in regular files have already been handled). */ 6411 bfd_boolean ignore_undef = FALSE; 6412 6413 /* Some symbols may be special in that the fact that they're 6414 undefined can be safely ignored - let backend determine that. */ 6415 if (bed->elf_backend_ignore_undef_symbol) 6416 ignore_undef = bed->elf_backend_ignore_undef_symbol (h); 6417 6418 /* If we are reporting errors for this situation then do so now. */ 6419 if (ignore_undef == FALSE 6420 && h->ref_dynamic 6421 && ! h->ref_regular 6422 && ! elf_link_check_versioned_symbol (finfo->info, bed, h) 6423 && finfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) 6424 { 6425 if (! (finfo->info->callbacks->undefined_symbol 6426 (finfo->info, h->root.root.string, h->root.u.undef.abfd, 6427 NULL, 0, finfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR))) 6428 { 6429 eoinfo->failed = TRUE; 6430 return FALSE; 6431 } 6432 } 6433 } 6434 6435 /* We should also warn if a forced local symbol is referenced from 6436 shared libraries. */ 6437 if (! finfo->info->relocatable 6438 && (! finfo->info->shared) 6439 && h->forced_local 6440 && h->ref_dynamic 6441 && !h->dynamic_def 6442 && !h->dynamic_weak 6443 && ! elf_link_check_versioned_symbol (finfo->info, bed, h)) 6444 { 6445 (*_bfd_error_handler) 6446 (_("%B: %s symbol `%s' in %B is referenced by DSO"), 6447 finfo->output_bfd, 6448 h->root.u.def.section == bfd_abs_section_ptr 6449 ? finfo->output_bfd : h->root.u.def.section->owner, 6450 ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 6451 ? "internal" 6452 : ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 6453 ? "hidden" : "local", 6454 h->root.root.string); 6455 eoinfo->failed = TRUE; 6456 return FALSE; 6457 } 6458 6459 /* We don't want to output symbols that have never been mentioned by 6460 a regular file, or that we have been told to strip. However, if 6461 h->indx is set to -2, the symbol is used by a reloc and we must 6462 output it. */ 6463 if (h->indx == -2) 6464 strip = FALSE; 6465 else if ((h->def_dynamic 6466 || h->ref_dynamic 6467 || h->root.type == bfd_link_hash_new) 6468 && !h->def_regular 6469 && !h->ref_regular) 6470 strip = TRUE; 6471 else if (finfo->info->strip == strip_all) 6472 strip = TRUE; 6473 else if (finfo->info->strip == strip_some 6474 && bfd_hash_lookup (finfo->info->keep_hash, 6475 h->root.root.string, FALSE, FALSE) == NULL) 6476 strip = TRUE; 6477 else if (finfo->info->strip_discarded 6478 && (h->root.type == bfd_link_hash_defined 6479 || h->root.type == bfd_link_hash_defweak) 6480 && elf_discarded_section (h->root.u.def.section)) 6481 strip = TRUE; 6482 else 6483 strip = FALSE; 6484 6485 /* If we're stripping it, and it's not a dynamic symbol, there's 6486 nothing else to do unless it is a forced local symbol. */ 6487 if (strip 6488 && h->dynindx == -1 6489 && !h->forced_local) 6490 return TRUE; 6491 6492 sym.st_value = 0; 6493 sym.st_size = h->size; 6494 sym.st_other = h->other; 6495 if (h->forced_local) 6496 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); 6497 else if (h->root.type == bfd_link_hash_undefweak 6498 || h->root.type == bfd_link_hash_defweak) 6499 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); 6500 else 6501 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); 6502 6503 switch (h->root.type) 6504 { 6505 default: 6506 case bfd_link_hash_new: 6507 case bfd_link_hash_warning: 6508 abort (); 6509 return FALSE; 6510 6511 case bfd_link_hash_undefined: 6512 case bfd_link_hash_undefweak: 6513 input_sec = bfd_und_section_ptr; 6514 sym.st_shndx = SHN_UNDEF; 6515 break; 6516 6517 case bfd_link_hash_defined: 6518 case bfd_link_hash_defweak: 6519 { 6520 input_sec = h->root.u.def.section; 6521 if (input_sec->output_section != NULL) 6522 { 6523 sym.st_shndx = 6524 _bfd_elf_section_from_bfd_section (finfo->output_bfd, 6525 input_sec->output_section); 6526 if (sym.st_shndx == SHN_BAD) 6527 { 6528 (*_bfd_error_handler) 6529 (_("%B: could not find output section %A for input section %A"), 6530 finfo->output_bfd, input_sec->output_section, input_sec); 6531 eoinfo->failed = TRUE; 6532 return FALSE; 6533 } 6534 6535 /* ELF symbols in relocatable files are section relative, 6536 but in nonrelocatable files they are virtual 6537 addresses. */ 6538 sym.st_value = h->root.u.def.value + input_sec->output_offset; 6539 if (! finfo->info->relocatable) 6540 { 6541 sym.st_value += input_sec->output_section->vma; 6542 if (h->type == STT_TLS) 6543 { 6544 /* STT_TLS symbols are relative to PT_TLS segment 6545 base. */ 6546 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); 6547 sym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; 6548 } 6549 } 6550 } 6551 else 6552 { 6553 BFD_ASSERT (input_sec->owner == NULL 6554 || (input_sec->owner->flags & DYNAMIC) != 0); 6555 sym.st_shndx = SHN_UNDEF; 6556 input_sec = bfd_und_section_ptr; 6557 } 6558 } 6559 break; 6560 6561 case bfd_link_hash_common: 6562 input_sec = h->root.u.c.p->section; 6563 sym.st_shndx = bed->common_section_index (input_sec); 6564 sym.st_value = 1 << h->root.u.c.p->alignment_power; 6565 break; 6566 6567 case bfd_link_hash_indirect: 6568 /* These symbols are created by symbol versioning. They point 6569 to the decorated version of the name. For example, if the 6570 symbol foo@@GNU_1.2 is the default, which should be used when 6571 foo is used with no version, then we add an indirect symbol 6572 foo which points to foo@@GNU_1.2. We ignore these symbols, 6573 since the indirected symbol is already in the hash table. */ 6574 return TRUE; 6575 } 6576 6577 /* Give the processor backend a chance to tweak the symbol value, 6578 and also to finish up anything that needs to be done for this 6579 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for 6580 forced local syms when non-shared is due to a historical quirk. */ 6581 if ((h->dynindx != -1 6582 || h->forced_local) 6583 && ((finfo->info->shared 6584 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 6585 || h->root.type != bfd_link_hash_undefweak)) 6586 || !h->forced_local) 6587 && elf_hash_table (finfo->info)->dynamic_sections_created) 6588 { 6589 if (! ((*bed->elf_backend_finish_dynamic_symbol) 6590 (finfo->output_bfd, finfo->info, h, &sym))) 6591 { 6592 eoinfo->failed = TRUE; 6593 return FALSE; 6594 } 6595 } 6596 6597 /* If we are marking the symbol as undefined, and there are no 6598 non-weak references to this symbol from a regular object, then 6599 mark the symbol as weak undefined; if there are non-weak 6600 references, mark the symbol as strong. We can't do this earlier, 6601 because it might not be marked as undefined until the 6602 finish_dynamic_symbol routine gets through with it. */ 6603 if (sym.st_shndx == SHN_UNDEF 6604 && h->ref_regular 6605 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL 6606 || ELF_ST_BIND (sym.st_info) == STB_WEAK)) 6607 { 6608 int bindtype; 6609 6610 if (h->ref_regular_nonweak) 6611 bindtype = STB_GLOBAL; 6612 else 6613 bindtype = STB_WEAK; 6614 sym.st_info = ELF_ST_INFO (bindtype, ELF_ST_TYPE (sym.st_info)); 6615 } 6616 6617 /* If a non-weak symbol with non-default visibility is not defined 6618 locally, it is a fatal error. */ 6619 if (! finfo->info->relocatable 6620 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT 6621 && ELF_ST_BIND (sym.st_info) != STB_WEAK 6622 && h->root.type == bfd_link_hash_undefined 6623 && !h->def_regular) 6624 { 6625 (*_bfd_error_handler) 6626 (_("%B: %s symbol `%s' isn't defined"), 6627 finfo->output_bfd, 6628 ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED 6629 ? "protected" 6630 : ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL 6631 ? "internal" : "hidden", 6632 h->root.root.string); 6633 eoinfo->failed = TRUE; 6634 return FALSE; 6635 } 6636 6637 /* If this symbol should be put in the .dynsym section, then put it 6638 there now. We already know the symbol index. We also fill in 6639 the entry in the .hash section. */ 6640 if (h->dynindx != -1 6641 && elf_hash_table (finfo->info)->dynamic_sections_created) 6642 { 6643 size_t bucketcount; 6644 size_t bucket; 6645 size_t hash_entry_size; 6646 bfd_byte *bucketpos; 6647 bfd_vma chain; 6648 bfd_byte *esym; 6649 6650 sym.st_name = h->dynstr_index; 6651 esym = finfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; 6652 if (! check_dynsym (finfo->output_bfd, &sym)) 6653 { 6654 eoinfo->failed = TRUE; 6655 return FALSE; 6656 } 6657 bed->s->swap_symbol_out (finfo->output_bfd, &sym, esym, 0); 6658 6659 bucketcount = elf_hash_table (finfo->info)->bucketcount; 6660 bucket = h->u.elf_hash_value % bucketcount; 6661 hash_entry_size 6662 = elf_section_data (finfo->hash_sec)->this_hdr.sh_entsize; 6663 bucketpos = ((bfd_byte *) finfo->hash_sec->contents 6664 + (bucket + 2) * hash_entry_size); 6665 chain = bfd_get (8 * hash_entry_size, finfo->output_bfd, bucketpos); 6666 bfd_put (8 * hash_entry_size, finfo->output_bfd, h->dynindx, bucketpos); 6667 bfd_put (8 * hash_entry_size, finfo->output_bfd, chain, 6668 ((bfd_byte *) finfo->hash_sec->contents 6669 + (bucketcount + 2 + h->dynindx) * hash_entry_size)); 6670 6671 if (finfo->symver_sec != NULL && finfo->symver_sec->contents != NULL) 6672 { 6673 Elf_Internal_Versym iversym; 6674 Elf_External_Versym *eversym; 6675 6676 if (!h->def_regular) 6677 { 6678 if (h->verinfo.verdef == NULL) 6679 iversym.vs_vers = 0; 6680 else 6681 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; 6682 } 6683 else 6684 { 6685 if (h->verinfo.vertree == NULL) 6686 iversym.vs_vers = 1; 6687 else 6688 iversym.vs_vers = h->verinfo.vertree->vernum + 1; 6689 if (finfo->info->create_default_symver) 6690 iversym.vs_vers++; 6691 } 6692 6693 if (h->hidden) 6694 iversym.vs_vers |= VERSYM_HIDDEN; 6695 6696 eversym = (Elf_External_Versym *) finfo->symver_sec->contents; 6697 eversym += h->dynindx; 6698 _bfd_elf_swap_versym_out (finfo->output_bfd, &iversym, eversym); 6699 } 6700 } 6701 6702 /* If we're stripping it, then it was just a dynamic symbol, and 6703 there's nothing else to do. */ 6704 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) 6705 return TRUE; 6706 6707 h->indx = bfd_get_symcount (finfo->output_bfd); 6708 6709 if (! elf_link_output_sym (finfo, h->root.root.string, &sym, input_sec, h)) 6710 { 6711 eoinfo->failed = TRUE; 6712 return FALSE; 6713 } 6714 6715 return TRUE; 6716 } 6717 6718 /* Return TRUE if special handling is done for relocs in SEC against 6719 symbols defined in discarded sections. */ 6720 6721 static bfd_boolean 6722 elf_section_ignore_discarded_relocs (asection *sec) 6723 { 6724 const struct elf_backend_data *bed; 6725 6726 switch (sec->sec_info_type) 6727 { 6728 case ELF_INFO_TYPE_STABS: 6729 case ELF_INFO_TYPE_EH_FRAME: 6730 return TRUE; 6731 default: 6732 break; 6733 } 6734 6735 bed = get_elf_backend_data (sec->owner); 6736 if (bed->elf_backend_ignore_discarded_relocs != NULL 6737 && (*bed->elf_backend_ignore_discarded_relocs) (sec)) 6738 return TRUE; 6739 6740 return FALSE; 6741 } 6742 6743 /* Return a mask saying how ld should treat relocations in SEC against 6744 symbols defined in discarded sections. If this function returns 6745 COMPLAIN set, ld will issue a warning message. If this function 6746 returns PRETEND set, and the discarded section was link-once and the 6747 same size as the kept link-once section, ld will pretend that the 6748 symbol was actually defined in the kept section. Otherwise ld will 6749 zero the reloc (at least that is the intent, but some cooperation by 6750 the target dependent code is needed, particularly for REL targets). */ 6751 6752 unsigned int 6753 _bfd_elf_default_action_discarded (asection *sec) 6754 { 6755 if (sec->flags & SEC_DEBUGGING) 6756 return PRETEND; 6757 6758 if (strcmp (".eh_frame", sec->name) == 0) 6759 return 0; 6760 6761 if (strcmp (".gcc_except_table", sec->name) == 0) 6762 return 0; 6763 6764 return COMPLAIN | PRETEND; 6765 } 6766 6767 /* Find a match between a section and a member of a section group. */ 6768 6769 static asection * 6770 match_group_member (asection *sec, asection *group, 6771 struct bfd_link_info *info) 6772 { 6773 asection *first = elf_next_in_group (group); 6774 asection *s = first; 6775 6776 while (s != NULL) 6777 { 6778 if (bfd_elf_match_symbols_in_sections (s, sec, info)) 6779 return s; 6780 6781 s = elf_next_in_group (s); 6782 if (s == first) 6783 break; 6784 } 6785 6786 return NULL; 6787 } 6788 6789 /* Check if the kept section of a discarded section SEC can be used 6790 to replace it. Return the replacement if it is OK. Otherwise return 6791 NULL. */ 6792 6793 asection * 6794 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) 6795 { 6796 asection *kept; 6797 6798 kept = sec->kept_section; 6799 if (kept != NULL) 6800 { 6801 if (elf_sec_group (sec) != NULL) 6802 kept = match_group_member (sec, kept, info); 6803 if (kept != NULL && sec->size != kept->size) 6804 kept = NULL; 6805 } 6806 return kept; 6807 } 6808 6809 /* Link an input file into the linker output file. This function 6810 handles all the sections and relocations of the input file at once. 6811 This is so that we only have to read the local symbols once, and 6812 don't have to keep them in memory. */ 6813 6814 static bfd_boolean 6815 elf_link_input_bfd (struct elf_final_link_info *finfo, bfd *input_bfd) 6816 { 6817 bfd_boolean (*relocate_section) 6818 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 6819 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); 6820 bfd *output_bfd; 6821 Elf_Internal_Shdr *symtab_hdr; 6822 size_t locsymcount; 6823 size_t extsymoff; 6824 Elf_Internal_Sym *isymbuf; 6825 Elf_Internal_Sym *isym; 6826 Elf_Internal_Sym *isymend; 6827 long *pindex; 6828 asection **ppsection; 6829 asection *o; 6830 const struct elf_backend_data *bed; 6831 bfd_boolean emit_relocs; 6832 struct elf_link_hash_entry **sym_hashes; 6833 6834 output_bfd = finfo->output_bfd; 6835 bed = get_elf_backend_data (output_bfd); 6836 relocate_section = bed->elf_backend_relocate_section; 6837 6838 /* If this is a dynamic object, we don't want to do anything here: 6839 we don't want the local symbols, and we don't want the section 6840 contents. */ 6841 if ((input_bfd->flags & DYNAMIC) != 0) 6842 return TRUE; 6843 6844 emit_relocs = (finfo->info->relocatable 6845 || finfo->info->emitrelocations); 6846 6847 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 6848 if (elf_bad_symtab (input_bfd)) 6849 { 6850 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 6851 extsymoff = 0; 6852 } 6853 else 6854 { 6855 locsymcount = symtab_hdr->sh_info; 6856 extsymoff = symtab_hdr->sh_info; 6857 } 6858 6859 /* Read the local symbols. */ 6860 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 6861 if (isymbuf == NULL && locsymcount != 0) 6862 { 6863 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 6864 finfo->internal_syms, 6865 finfo->external_syms, 6866 finfo->locsym_shndx); 6867 if (isymbuf == NULL) 6868 return FALSE; 6869 } 6870 6871 /* Find local symbol sections and adjust values of symbols in 6872 SEC_MERGE sections. Write out those local symbols we know are 6873 going into the output file. */ 6874 isymend = isymbuf + locsymcount; 6875 for (isym = isymbuf, pindex = finfo->indices, ppsection = finfo->sections; 6876 isym < isymend; 6877 isym++, pindex++, ppsection++) 6878 { 6879 asection *isec; 6880 const char *name; 6881 Elf_Internal_Sym osym; 6882 6883 *pindex = -1; 6884 6885 if (elf_bad_symtab (input_bfd)) 6886 { 6887 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) 6888 { 6889 *ppsection = NULL; 6890 continue; 6891 } 6892 } 6893 6894 if (isym->st_shndx == SHN_UNDEF) 6895 isec = bfd_und_section_ptr; 6896 else if (isym->st_shndx < SHN_LORESERVE 6897 || isym->st_shndx > SHN_HIRESERVE) 6898 { 6899 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 6900 if (isec 6901 && isec->sec_info_type == ELF_INFO_TYPE_MERGE 6902 && ELF_ST_TYPE (isym->st_info) != STT_SECTION) 6903 isym->st_value = 6904 _bfd_merged_section_offset (output_bfd, &isec, 6905 elf_section_data (isec)->sec_info, 6906 isym->st_value); 6907 } 6908 else if (isym->st_shndx == SHN_ABS) 6909 isec = bfd_abs_section_ptr; 6910 else if (isym->st_shndx == SHN_COMMON) 6911 isec = bfd_com_section_ptr; 6912 else 6913 { 6914 /* Don't attempt to output symbols with st_shnx in the 6915 reserved range other than SHN_ABS and SHN_COMMON. */ 6916 *ppsection = NULL; 6917 continue; 6918 } 6919 6920 *ppsection = isec; 6921 6922 /* Don't output the first, undefined, symbol. */ 6923 if (ppsection == finfo->sections) 6924 continue; 6925 6926 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 6927 { 6928 /* We never output section symbols. Instead, we use the 6929 section symbol of the corresponding section in the output 6930 file. */ 6931 continue; 6932 } 6933 6934 /* If we are stripping all symbols, we don't want to output this 6935 one. */ 6936 if (finfo->info->strip == strip_all) 6937 continue; 6938 6939 /* If we are discarding all local symbols, we don't want to 6940 output this one. If we are generating a relocatable output 6941 file, then some of the local symbols may be required by 6942 relocs; we output them below as we discover that they are 6943 needed. */ 6944 if (finfo->info->discard == discard_all) 6945 continue; 6946 6947 /* If this symbol is defined in a section which we are 6948 discarding, we don't need to keep it. */ 6949 if (isym->st_shndx != SHN_UNDEF 6950 && (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) 6951 && (isec == NULL 6952 || bfd_section_removed_from_list (output_bfd, 6953 isec->output_section))) 6954 continue; 6955 6956 /* Get the name of the symbol. */ 6957 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, 6958 isym->st_name); 6959 if (name == NULL) 6960 return FALSE; 6961 6962 /* See if we are discarding symbols with this name. */ 6963 if ((finfo->info->strip == strip_some 6964 && (bfd_hash_lookup (finfo->info->keep_hash, name, FALSE, FALSE) 6965 == NULL)) 6966 || (((finfo->info->discard == discard_sec_merge 6967 && (isec->flags & SEC_MERGE) && ! finfo->info->relocatable) 6968 || finfo->info->discard == discard_l) 6969 && bfd_is_local_label_name (input_bfd, name))) 6970 continue; 6971 6972 /* If we get here, we are going to output this symbol. */ 6973 6974 osym = *isym; 6975 6976 /* Adjust the section index for the output file. */ 6977 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 6978 isec->output_section); 6979 if (osym.st_shndx == SHN_BAD) 6980 return FALSE; 6981 6982 *pindex = bfd_get_symcount (output_bfd); 6983 6984 /* ELF symbols in relocatable files are section relative, but 6985 in executable files they are virtual addresses. Note that 6986 this code assumes that all ELF sections have an associated 6987 BFD section with a reasonable value for output_offset; below 6988 we assume that they also have a reasonable value for 6989 output_section. Any special sections must be set up to meet 6990 these requirements. */ 6991 osym.st_value += isec->output_offset; 6992 if (! finfo->info->relocatable) 6993 { 6994 osym.st_value += isec->output_section->vma; 6995 if (ELF_ST_TYPE (osym.st_info) == STT_TLS) 6996 { 6997 /* STT_TLS symbols are relative to PT_TLS segment base. */ 6998 BFD_ASSERT (elf_hash_table (finfo->info)->tls_sec != NULL); 6999 osym.st_value -= elf_hash_table (finfo->info)->tls_sec->vma; 7000 } 7001 } 7002 7003 if (! elf_link_output_sym (finfo, name, &osym, isec, NULL)) 7004 return FALSE; 7005 } 7006 7007 /* Relocate the contents of each section. */ 7008 sym_hashes = elf_sym_hashes (input_bfd); 7009 for (o = input_bfd->sections; o != NULL; o = o->next) 7010 { 7011 bfd_byte *contents; 7012 7013 if (! o->linker_mark) 7014 { 7015 /* This section was omitted from the link. */ 7016 continue; 7017 } 7018 7019 if ((o->flags & SEC_HAS_CONTENTS) == 0 7020 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 7021 continue; 7022 7023 if ((o->flags & SEC_LINKER_CREATED) != 0) 7024 { 7025 /* Section was created by _bfd_elf_link_create_dynamic_sections 7026 or somesuch. */ 7027 continue; 7028 } 7029 7030 /* Get the contents of the section. They have been cached by a 7031 relaxation routine. Note that o is a section in an input 7032 file, so the contents field will not have been set by any of 7033 the routines which work on output files. */ 7034 if (elf_section_data (o)->this_hdr.contents != NULL) 7035 contents = elf_section_data (o)->this_hdr.contents; 7036 else 7037 { 7038 bfd_size_type amt = o->rawsize ? o->rawsize : o->size; 7039 7040 contents = finfo->contents; 7041 if (! bfd_get_section_contents (input_bfd, o, contents, 0, amt)) 7042 return FALSE; 7043 } 7044 7045 if ((o->flags & SEC_RELOC) != 0) 7046 { 7047 Elf_Internal_Rela *internal_relocs; 7048 bfd_vma r_type_mask; 7049 int r_sym_shift; 7050 7051 /* Get the swapped relocs. */ 7052 internal_relocs 7053 = _bfd_elf_link_read_relocs (input_bfd, o, finfo->external_relocs, 7054 finfo->internal_relocs, FALSE); 7055 if (internal_relocs == NULL 7056 && o->reloc_count > 0) 7057 return FALSE; 7058 7059 if (bed->s->arch_size == 32) 7060 { 7061 r_type_mask = 0xff; 7062 r_sym_shift = 8; 7063 } 7064 else 7065 { 7066 r_type_mask = 0xffffffff; 7067 r_sym_shift = 32; 7068 } 7069 7070 /* Run through the relocs looking for any against symbols 7071 from discarded sections and section symbols from 7072 removed link-once sections. Complain about relocs 7073 against discarded sections. Zero relocs against removed 7074 link-once sections. */ 7075 if (!elf_section_ignore_discarded_relocs (o)) 7076 { 7077 Elf_Internal_Rela *rel, *relend; 7078 unsigned int action = (*bed->action_discarded) (o); 7079 7080 rel = internal_relocs; 7081 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; 7082 for ( ; rel < relend; rel++) 7083 { 7084 unsigned long r_symndx = rel->r_info >> r_sym_shift; 7085 asection **ps, *sec; 7086 struct elf_link_hash_entry *h = NULL; 7087 const char *sym_name; 7088 7089 if (r_symndx == STN_UNDEF) 7090 continue; 7091 7092 if (r_symndx >= locsymcount 7093 || (elf_bad_symtab (input_bfd) 7094 && finfo->sections[r_symndx] == NULL)) 7095 { 7096 h = sym_hashes[r_symndx - extsymoff]; 7097 7098 /* Badly formatted input files can contain relocs that 7099 reference non-existant symbols. Check here so that 7100 we do not seg fault. */ 7101 if (h == NULL) 7102 { 7103 char buffer [32]; 7104 7105 sprintf_vma (buffer, rel->r_info); 7106 (*_bfd_error_handler) 7107 (_("error: %B contains a reloc (0x%s) for section %A " 7108 "that references a non-existent global symbol"), 7109 input_bfd, o, buffer); 7110 bfd_set_error (bfd_error_bad_value); 7111 return FALSE; 7112 } 7113 7114 while (h->root.type == bfd_link_hash_indirect 7115 || h->root.type == bfd_link_hash_warning) 7116 h = (struct elf_link_hash_entry *) h->root.u.i.link; 7117 7118 if (h->root.type != bfd_link_hash_defined 7119 && h->root.type != bfd_link_hash_defweak) 7120 continue; 7121 7122 ps = &h->root.u.def.section; 7123 sym_name = h->root.root.string; 7124 } 7125 else 7126 { 7127 Elf_Internal_Sym *sym = isymbuf + r_symndx; 7128 ps = &finfo->sections[r_symndx]; 7129 sym_name = bfd_elf_sym_name (input_bfd, 7130 symtab_hdr, 7131 sym, *ps); 7132 } 7133 7134 /* Complain if the definition comes from a 7135 discarded section. */ 7136 if ((sec = *ps) != NULL && elf_discarded_section (sec)) 7137 { 7138 BFD_ASSERT (r_symndx != 0); 7139 if (action & COMPLAIN) 7140 (*finfo->info->callbacks->einfo) 7141 (_("%X`%s' referenced in section `%A' of %B: " 7142 "defined in discarded section `%A' of %B\n"), 7143 sym_name, o, input_bfd, sec, sec->owner); 7144 7145 /* Try to do the best we can to support buggy old 7146 versions of gcc. Pretend that the symbol is 7147 really defined in the kept linkonce section. 7148 FIXME: This is quite broken. Modifying the 7149 symbol here means we will be changing all later 7150 uses of the symbol, not just in this section. */ 7151 if (action & PRETEND) 7152 { 7153 asection *kept; 7154 7155 kept = _bfd_elf_check_kept_section (sec, 7156 finfo->info); 7157 if (kept != NULL) 7158 { 7159 *ps = kept; 7160 continue; 7161 } 7162 } 7163 7164 /* Remove the symbol reference from the reloc, but 7165 don't kill the reloc completely. This is so that 7166 a zero value will be written into the section, 7167 which may have non-zero contents put there by the 7168 assembler. Zero in things like an eh_frame fde 7169 pc_begin allows stack unwinders to recognize the 7170 fde as bogus. */ 7171 rel->r_info &= r_type_mask; 7172 rel->r_addend = 0; 7173 } 7174 } 7175 } 7176 7177 /* Relocate the section by invoking a back end routine. 7178 7179 The back end routine is responsible for adjusting the 7180 section contents as necessary, and (if using Rela relocs 7181 and generating a relocatable output file) adjusting the 7182 reloc addend as necessary. 7183 7184 The back end routine does not have to worry about setting 7185 the reloc address or the reloc symbol index. 7186 7187 The back end routine is given a pointer to the swapped in 7188 internal symbols, and can access the hash table entries 7189 for the external symbols via elf_sym_hashes (input_bfd). 7190 7191 When generating relocatable output, the back end routine 7192 must handle STB_LOCAL/STT_SECTION symbols specially. The 7193 output symbol is going to be a section symbol 7194 corresponding to the output section, which will require 7195 the addend to be adjusted. */ 7196 7197 if (! (*relocate_section) (output_bfd, finfo->info, 7198 input_bfd, o, contents, 7199 internal_relocs, 7200 isymbuf, 7201 finfo->sections)) 7202 return FALSE; 7203 7204 if (emit_relocs) 7205 { 7206 Elf_Internal_Rela *irela; 7207 Elf_Internal_Rela *irelaend; 7208 bfd_vma last_offset; 7209 struct elf_link_hash_entry **rel_hash; 7210 struct elf_link_hash_entry **rel_hash_list; 7211 Elf_Internal_Shdr *input_rel_hdr, *input_rel_hdr2; 7212 unsigned int next_erel; 7213 bfd_boolean rela_normal; 7214 7215 input_rel_hdr = &elf_section_data (o)->rel_hdr; 7216 rela_normal = (bed->rela_normal 7217 && (input_rel_hdr->sh_entsize 7218 == bed->s->sizeof_rela)); 7219 7220 /* Adjust the reloc addresses and symbol indices. */ 7221 7222 irela = internal_relocs; 7223 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; 7224 rel_hash = (elf_section_data (o->output_section)->rel_hashes 7225 + elf_section_data (o->output_section)->rel_count 7226 + elf_section_data (o->output_section)->rel_count2); 7227 rel_hash_list = rel_hash; 7228 last_offset = o->output_offset; 7229 if (!finfo->info->relocatable) 7230 last_offset += o->output_section->vma; 7231 for (next_erel = 0; irela < irelaend; irela++, next_erel++) 7232 { 7233 unsigned long r_symndx; 7234 asection *sec; 7235 Elf_Internal_Sym sym; 7236 7237 if (next_erel == bed->s->int_rels_per_ext_rel) 7238 { 7239 rel_hash++; 7240 next_erel = 0; 7241 } 7242 7243 irela->r_offset = _bfd_elf_section_offset (output_bfd, 7244 finfo->info, o, 7245 irela->r_offset); 7246 if (irela->r_offset >= (bfd_vma) -2) 7247 { 7248 /* This is a reloc for a deleted entry or somesuch. 7249 Turn it into an R_*_NONE reloc, at the same 7250 offset as the last reloc. elf_eh_frame.c and 7251 elf_bfd_discard_info rely on reloc offsets 7252 being ordered. */ 7253 irela->r_offset = last_offset; 7254 irela->r_info = 0; 7255 irela->r_addend = 0; 7256 continue; 7257 } 7258 7259 irela->r_offset += o->output_offset; 7260 7261 /* Relocs in an executable have to be virtual addresses. */ 7262 if (!finfo->info->relocatable) 7263 irela->r_offset += o->output_section->vma; 7264 7265 last_offset = irela->r_offset; 7266 7267 r_symndx = irela->r_info >> r_sym_shift; 7268 if (r_symndx == STN_UNDEF) 7269 continue; 7270 7271 if (r_symndx >= locsymcount 7272 || (elf_bad_symtab (input_bfd) 7273 && finfo->sections[r_symndx] == NULL)) 7274 { 7275 struct elf_link_hash_entry *rh; 7276 unsigned long indx; 7277 7278 /* This is a reloc against a global symbol. We 7279 have not yet output all the local symbols, so 7280 we do not know the symbol index of any global 7281 symbol. We set the rel_hash entry for this 7282 reloc to point to the global hash table entry 7283 for this symbol. The symbol index is then 7284 set at the end of bfd_elf_final_link. */ 7285 indx = r_symndx - extsymoff; 7286 rh = elf_sym_hashes (input_bfd)[indx]; 7287 while (rh->root.type == bfd_link_hash_indirect 7288 || rh->root.type == bfd_link_hash_warning) 7289 rh = (struct elf_link_hash_entry *) rh->root.u.i.link; 7290 7291 /* Setting the index to -2 tells 7292 elf_link_output_extsym that this symbol is 7293 used by a reloc. */ 7294 BFD_ASSERT (rh->indx < 0); 7295 rh->indx = -2; 7296 7297 *rel_hash = rh; 7298 7299 continue; 7300 } 7301 7302 /* This is a reloc against a local symbol. */ 7303 7304 *rel_hash = NULL; 7305 sym = isymbuf[r_symndx]; 7306 sec = finfo->sections[r_symndx]; 7307 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) 7308 { 7309 /* I suppose the backend ought to fill in the 7310 section of any STT_SECTION symbol against a 7311 processor specific section. */ 7312 r_symndx = 0; 7313 if (bfd_is_abs_section (sec)) 7314 ; 7315 else if (sec == NULL || sec->owner == NULL) 7316 { 7317 bfd_set_error (bfd_error_bad_value); 7318 return FALSE; 7319 } 7320 else 7321 { 7322 asection *osec = sec->output_section; 7323 7324 /* If we have discarded a section, the output 7325 section will be the absolute section. In 7326 case of discarded link-once and discarded 7327 SEC_MERGE sections, use the kept section. */ 7328 if (bfd_is_abs_section (osec) 7329 && sec->kept_section != NULL 7330 && sec->kept_section->output_section != NULL) 7331 { 7332 osec = sec->kept_section->output_section; 7333 irela->r_addend -= osec->vma; 7334 } 7335 7336 if (!bfd_is_abs_section (osec)) 7337 { 7338 r_symndx = osec->target_index; 7339 BFD_ASSERT (r_symndx != 0); 7340 } 7341 } 7342 7343 /* Adjust the addend according to where the 7344 section winds up in the output section. */ 7345 if (rela_normal) 7346 irela->r_addend += sec->output_offset; 7347 } 7348 else 7349 { 7350 if (finfo->indices[r_symndx] == -1) 7351 { 7352 unsigned long shlink; 7353 const char *name; 7354 asection *osec; 7355 7356 if (finfo->info->strip == strip_all) 7357 { 7358 /* You can't do ld -r -s. */ 7359 bfd_set_error (bfd_error_invalid_operation); 7360 return FALSE; 7361 } 7362 7363 /* This symbol was skipped earlier, but 7364 since it is needed by a reloc, we 7365 must output it now. */ 7366 shlink = symtab_hdr->sh_link; 7367 name = (bfd_elf_string_from_elf_section 7368 (input_bfd, shlink, sym.st_name)); 7369 if (name == NULL) 7370 return FALSE; 7371 7372 osec = sec->output_section; 7373 sym.st_shndx = 7374 _bfd_elf_section_from_bfd_section (output_bfd, 7375 osec); 7376 if (sym.st_shndx == SHN_BAD) 7377 return FALSE; 7378 7379 sym.st_value += sec->output_offset; 7380 if (! finfo->info->relocatable) 7381 { 7382 sym.st_value += osec->vma; 7383 if (ELF_ST_TYPE (sym.st_info) == STT_TLS) 7384 { 7385 /* STT_TLS symbols are relative to PT_TLS 7386 segment base. */ 7387 BFD_ASSERT (elf_hash_table (finfo->info) 7388 ->tls_sec != NULL); 7389 sym.st_value -= (elf_hash_table (finfo->info) 7390 ->tls_sec->vma); 7391 } 7392 } 7393 7394 finfo->indices[r_symndx] 7395 = bfd_get_symcount (output_bfd); 7396 7397 if (! elf_link_output_sym (finfo, name, &sym, sec, 7398 NULL)) 7399 return FALSE; 7400 } 7401 7402 r_symndx = finfo->indices[r_symndx]; 7403 } 7404 7405 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift 7406 | (irela->r_info & r_type_mask)); 7407 } 7408 7409 /* Swap out the relocs. */ 7410 if (input_rel_hdr->sh_size != 0 7411 && !bed->elf_backend_emit_relocs (output_bfd, o, 7412 input_rel_hdr, 7413 internal_relocs, 7414 rel_hash_list)) 7415 return FALSE; 7416 7417 input_rel_hdr2 = elf_section_data (o)->rel_hdr2; 7418 if (input_rel_hdr2 && input_rel_hdr2->sh_size != 0) 7419 { 7420 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) 7421 * bed->s->int_rels_per_ext_rel); 7422 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); 7423 if (!bed->elf_backend_emit_relocs (output_bfd, o, 7424 input_rel_hdr2, 7425 internal_relocs, 7426 rel_hash_list)) 7427 return FALSE; 7428 } 7429 } 7430 } 7431 7432 /* Write out the modified section contents. */ 7433 if (bed->elf_backend_write_section 7434 && (*bed->elf_backend_write_section) (output_bfd, o, contents)) 7435 { 7436 /* Section written out. */ 7437 } 7438 else switch (o->sec_info_type) 7439 { 7440 case ELF_INFO_TYPE_STABS: 7441 if (! (_bfd_write_section_stabs 7442 (output_bfd, 7443 &elf_hash_table (finfo->info)->stab_info, 7444 o, &elf_section_data (o)->sec_info, contents))) 7445 return FALSE; 7446 break; 7447 case ELF_INFO_TYPE_MERGE: 7448 if (! _bfd_write_merged_section (output_bfd, o, 7449 elf_section_data (o)->sec_info)) 7450 return FALSE; 7451 break; 7452 case ELF_INFO_TYPE_EH_FRAME: 7453 { 7454 if (! _bfd_elf_write_section_eh_frame (output_bfd, finfo->info, 7455 o, contents)) 7456 return FALSE; 7457 } 7458 break; 7459 default: 7460 { 7461 if (! (o->flags & SEC_EXCLUDE) 7462 && ! bfd_set_section_contents (output_bfd, o->output_section, 7463 contents, 7464 (file_ptr) o->output_offset, 7465 o->size)) 7466 return FALSE; 7467 } 7468 break; 7469 } 7470 } 7471 7472 return TRUE; 7473 } 7474 7475 /* Generate a reloc when linking an ELF file. This is a reloc 7476 requested by the linker, and does not come from any input file. This 7477 is used to build constructor and destructor tables when linking 7478 with -Ur. */ 7479 7480 static bfd_boolean 7481 elf_reloc_link_order (bfd *output_bfd, 7482 struct bfd_link_info *info, 7483 asection *output_section, 7484 struct bfd_link_order *link_order) 7485 { 7486 reloc_howto_type *howto; 7487 long indx; 7488 bfd_vma offset; 7489 bfd_vma addend; 7490 struct elf_link_hash_entry **rel_hash_ptr; 7491 Elf_Internal_Shdr *rel_hdr; 7492 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 7493 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; 7494 bfd_byte *erel; 7495 unsigned int i; 7496 7497 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 7498 if (howto == NULL) 7499 { 7500 bfd_set_error (bfd_error_bad_value); 7501 return FALSE; 7502 } 7503 7504 addend = link_order->u.reloc.p->addend; 7505 7506 /* Figure out the symbol index. */ 7507 rel_hash_ptr = (elf_section_data (output_section)->rel_hashes 7508 + elf_section_data (output_section)->rel_count 7509 + elf_section_data (output_section)->rel_count2); 7510 if (link_order->type == bfd_section_reloc_link_order) 7511 { 7512 indx = link_order->u.reloc.p->u.section->target_index; 7513 BFD_ASSERT (indx != 0); 7514 *rel_hash_ptr = NULL; 7515 } 7516 else 7517 { 7518 struct elf_link_hash_entry *h; 7519 7520 /* Treat a reloc against a defined symbol as though it were 7521 actually against the section. */ 7522 h = ((struct elf_link_hash_entry *) 7523 bfd_wrapped_link_hash_lookup (output_bfd, info, 7524 link_order->u.reloc.p->u.name, 7525 FALSE, FALSE, TRUE)); 7526 if (h != NULL 7527 && (h->root.type == bfd_link_hash_defined 7528 || h->root.type == bfd_link_hash_defweak)) 7529 { 7530 asection *section; 7531 7532 section = h->root.u.def.section; 7533 indx = section->output_section->target_index; 7534 *rel_hash_ptr = NULL; 7535 /* It seems that we ought to add the symbol value to the 7536 addend here, but in practice it has already been added 7537 because it was passed to constructor_callback. */ 7538 addend += section->output_section->vma + section->output_offset; 7539 } 7540 else if (h != NULL) 7541 { 7542 /* Setting the index to -2 tells elf_link_output_extsym that 7543 this symbol is used by a reloc. */ 7544 h->indx = -2; 7545 *rel_hash_ptr = h; 7546 indx = 0; 7547 } 7548 else 7549 { 7550 if (! ((*info->callbacks->unattached_reloc) 7551 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) 7552 return FALSE; 7553 indx = 0; 7554 } 7555 } 7556 7557 /* If this is an inplace reloc, we must write the addend into the 7558 object file. */ 7559 if (howto->partial_inplace && addend != 0) 7560 { 7561 bfd_size_type size; 7562 bfd_reloc_status_type rstat; 7563 bfd_byte *buf; 7564 bfd_boolean ok; 7565 const char *sym_name; 7566 7567 size = bfd_get_reloc_size (howto); 7568 buf = bfd_zmalloc (size); 7569 if (buf == NULL) 7570 return FALSE; 7571 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 7572 switch (rstat) 7573 { 7574 case bfd_reloc_ok: 7575 break; 7576 7577 default: 7578 case bfd_reloc_outofrange: 7579 abort (); 7580 7581 case bfd_reloc_overflow: 7582 if (link_order->type == bfd_section_reloc_link_order) 7583 sym_name = bfd_section_name (output_bfd, 7584 link_order->u.reloc.p->u.section); 7585 else 7586 sym_name = link_order->u.reloc.p->u.name; 7587 if (! ((*info->callbacks->reloc_overflow) 7588 (info, NULL, sym_name, howto->name, addend, NULL, 7589 NULL, (bfd_vma) 0))) 7590 { 7591 free (buf); 7592 return FALSE; 7593 } 7594 break; 7595 } 7596 ok = bfd_set_section_contents (output_bfd, output_section, buf, 7597 link_order->offset, size); 7598 free (buf); 7599 if (! ok) 7600 return FALSE; 7601 } 7602 7603 /* The address of a reloc is relative to the section in a 7604 relocatable file, and is a virtual address in an executable 7605 file. */ 7606 offset = link_order->offset; 7607 if (! info->relocatable) 7608 offset += output_section->vma; 7609 7610 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) 7611 { 7612 irel[i].r_offset = offset; 7613 irel[i].r_info = 0; 7614 irel[i].r_addend = 0; 7615 } 7616 if (bed->s->arch_size == 32) 7617 irel[0].r_info = ELF32_R_INFO (indx, howto->type); 7618 else 7619 irel[0].r_info = ELF64_R_INFO (indx, howto->type); 7620 7621 rel_hdr = &elf_section_data (output_section)->rel_hdr; 7622 erel = rel_hdr->contents; 7623 if (rel_hdr->sh_type == SHT_REL) 7624 { 7625 erel += (elf_section_data (output_section)->rel_count 7626 * bed->s->sizeof_rel); 7627 (*bed->s->swap_reloc_out) (output_bfd, irel, erel); 7628 } 7629 else 7630 { 7631 irel[0].r_addend = addend; 7632 erel += (elf_section_data (output_section)->rel_count 7633 * bed->s->sizeof_rela); 7634 (*bed->s->swap_reloca_out) (output_bfd, irel, erel); 7635 } 7636 7637 ++elf_section_data (output_section)->rel_count; 7638 7639 return TRUE; 7640 } 7641 7642 7643 /* Get the output vma of the section pointed to by the sh_link field. */ 7644 7645 static bfd_vma 7646 elf_get_linked_section_vma (struct bfd_link_order *p) 7647 { 7648 Elf_Internal_Shdr **elf_shdrp; 7649 asection *s; 7650 int elfsec; 7651 7652 s = p->u.indirect.section; 7653 elf_shdrp = elf_elfsections (s->owner); 7654 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); 7655 elfsec = elf_shdrp[elfsec]->sh_link; 7656 /* PR 290: 7657 The Intel C compiler generates SHT_IA_64_UNWIND with 7658 SHF_LINK_ORDER. But it doesn't set the sh_link or 7659 sh_info fields. Hence we could get the situation 7660 where elfsec is 0. */ 7661 if (elfsec == 0) 7662 { 7663 const struct elf_backend_data *bed 7664 = get_elf_backend_data (s->owner); 7665 if (bed->link_order_error_handler) 7666 bed->link_order_error_handler 7667 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); 7668 return 0; 7669 } 7670 else 7671 { 7672 s = elf_shdrp[elfsec]->bfd_section; 7673 return s->output_section->vma + s->output_offset; 7674 } 7675 } 7676 7677 7678 /* Compare two sections based on the locations of the sections they are 7679 linked to. Used by elf_fixup_link_order. */ 7680 7681 static int 7682 compare_link_order (const void * a, const void * b) 7683 { 7684 bfd_vma apos; 7685 bfd_vma bpos; 7686 7687 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); 7688 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); 7689 if (apos < bpos) 7690 return -1; 7691 return apos > bpos; 7692 } 7693 7694 7695 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same 7696 order as their linked sections. Returns false if this could not be done 7697 because an output section includes both ordered and unordered 7698 sections. Ideally we'd do this in the linker proper. */ 7699 7700 static bfd_boolean 7701 elf_fixup_link_order (bfd *abfd, asection *o) 7702 { 7703 int seen_linkorder; 7704 int seen_other; 7705 int n; 7706 struct bfd_link_order *p; 7707 bfd *sub; 7708 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7709 unsigned elfsec; 7710 struct bfd_link_order **sections; 7711 asection *s, *other_sec, *linkorder_sec; 7712 bfd_vma offset; 7713 7714 other_sec = NULL; 7715 linkorder_sec = NULL; 7716 seen_other = 0; 7717 seen_linkorder = 0; 7718 for (p = o->map_head.link_order; p != NULL; p = p->next) 7719 { 7720 if (p->type == bfd_indirect_link_order) 7721 { 7722 s = p->u.indirect.section; 7723 sub = s->owner; 7724 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 7725 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass 7726 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) 7727 && elfsec < elf_numsections (sub) 7728 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER) 7729 { 7730 seen_linkorder++; 7731 linkorder_sec = s; 7732 } 7733 else 7734 { 7735 seen_other++; 7736 other_sec = s; 7737 } 7738 } 7739 else 7740 seen_other++; 7741 7742 if (seen_other && seen_linkorder) 7743 { 7744 if (other_sec && linkorder_sec) 7745 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), 7746 o, linkorder_sec, 7747 linkorder_sec->owner, other_sec, 7748 other_sec->owner); 7749 else 7750 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), 7751 o); 7752 bfd_set_error (bfd_error_bad_value); 7753 return FALSE; 7754 } 7755 } 7756 7757 if (!seen_linkorder) 7758 return TRUE; 7759 7760 sections = (struct bfd_link_order **) 7761 xmalloc (seen_linkorder * sizeof (struct bfd_link_order *)); 7762 seen_linkorder = 0; 7763 7764 for (p = o->map_head.link_order; p != NULL; p = p->next) 7765 { 7766 sections[seen_linkorder++] = p; 7767 } 7768 /* Sort the input sections in the order of their linked section. */ 7769 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), 7770 compare_link_order); 7771 7772 /* Change the offsets of the sections. */ 7773 offset = 0; 7774 for (n = 0; n < seen_linkorder; n++) 7775 { 7776 s = sections[n]->u.indirect.section; 7777 offset &= ~(bfd_vma)((1 << s->alignment_power) - 1); 7778 s->output_offset = offset; 7779 sections[n]->offset = offset; 7780 offset += sections[n]->size; 7781 } 7782 7783 return TRUE; 7784 } 7785 7786 7787 /* Do the final step of an ELF link. */ 7788 7789 bfd_boolean 7790 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) 7791 { 7792 bfd_boolean dynamic; 7793 bfd_boolean emit_relocs; 7794 bfd *dynobj; 7795 struct elf_final_link_info finfo; 7796 register asection *o; 7797 register struct bfd_link_order *p; 7798 register bfd *sub; 7799 bfd_size_type max_contents_size; 7800 bfd_size_type max_external_reloc_size; 7801 bfd_size_type max_internal_reloc_count; 7802 bfd_size_type max_sym_count; 7803 bfd_size_type max_sym_shndx_count; 7804 file_ptr off; 7805 Elf_Internal_Sym elfsym; 7806 unsigned int i; 7807 Elf_Internal_Shdr *symtab_hdr; 7808 Elf_Internal_Shdr *symtab_shndx_hdr; 7809 Elf_Internal_Shdr *symstrtab_hdr; 7810 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7811 struct elf_outext_info eoinfo; 7812 bfd_boolean merged; 7813 size_t relativecount = 0; 7814 asection *reldyn = 0; 7815 bfd_size_type amt; 7816 7817 if (! is_elf_hash_table (info->hash)) 7818 return FALSE; 7819 7820 if (info->shared) 7821 abfd->flags |= DYNAMIC; 7822 7823 dynamic = elf_hash_table (info)->dynamic_sections_created; 7824 dynobj = elf_hash_table (info)->dynobj; 7825 7826 emit_relocs = (info->relocatable 7827 || info->emitrelocations); 7828 7829 finfo.info = info; 7830 finfo.output_bfd = abfd; 7831 finfo.symstrtab = _bfd_elf_stringtab_init (); 7832 if (finfo.symstrtab == NULL) 7833 return FALSE; 7834 7835 if (! dynamic) 7836 { 7837 finfo.dynsym_sec = NULL; 7838 finfo.hash_sec = NULL; 7839 finfo.symver_sec = NULL; 7840 } 7841 else 7842 { 7843 finfo.dynsym_sec = bfd_get_section_by_name (dynobj, ".dynsym"); 7844 finfo.hash_sec = bfd_get_section_by_name (dynobj, ".hash"); 7845 BFD_ASSERT (finfo.dynsym_sec != NULL && finfo.hash_sec != NULL); 7846 finfo.symver_sec = bfd_get_section_by_name (dynobj, ".gnu.version"); 7847 /* Note that it is OK if symver_sec is NULL. */ 7848 } 7849 7850 finfo.contents = NULL; 7851 finfo.external_relocs = NULL; 7852 finfo.internal_relocs = NULL; 7853 finfo.external_syms = NULL; 7854 finfo.locsym_shndx = NULL; 7855 finfo.internal_syms = NULL; 7856 finfo.indices = NULL; 7857 finfo.sections = NULL; 7858 finfo.symbuf = NULL; 7859 finfo.symshndxbuf = NULL; 7860 finfo.symbuf_count = 0; 7861 finfo.shndxbuf_size = 0; 7862 7863 /* Count up the number of relocations we will output for each output 7864 section, so that we know the sizes of the reloc sections. We 7865 also figure out some maximum sizes. */ 7866 max_contents_size = 0; 7867 max_external_reloc_size = 0; 7868 max_internal_reloc_count = 0; 7869 max_sym_count = 0; 7870 max_sym_shndx_count = 0; 7871 merged = FALSE; 7872 for (o = abfd->sections; o != NULL; o = o->next) 7873 { 7874 struct bfd_elf_section_data *esdo = elf_section_data (o); 7875 o->reloc_count = 0; 7876 7877 for (p = o->map_head.link_order; p != NULL; p = p->next) 7878 { 7879 unsigned int reloc_count = 0; 7880 struct bfd_elf_section_data *esdi = NULL; 7881 unsigned int *rel_count1; 7882 7883 if (p->type == bfd_section_reloc_link_order 7884 || p->type == bfd_symbol_reloc_link_order) 7885 reloc_count = 1; 7886 else if (p->type == bfd_indirect_link_order) 7887 { 7888 asection *sec; 7889 7890 sec = p->u.indirect.section; 7891 esdi = elf_section_data (sec); 7892 7893 /* Mark all sections which are to be included in the 7894 link. This will normally be every section. We need 7895 to do this so that we can identify any sections which 7896 the linker has decided to not include. */ 7897 sec->linker_mark = TRUE; 7898 7899 if (sec->flags & SEC_MERGE) 7900 merged = TRUE; 7901 7902 if (info->relocatable || info->emitrelocations) 7903 reloc_count = sec->reloc_count; 7904 else if (bed->elf_backend_count_relocs) 7905 { 7906 Elf_Internal_Rela * relocs; 7907 7908 relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 7909 info->keep_memory); 7910 7911 reloc_count = (*bed->elf_backend_count_relocs) (sec, relocs); 7912 7913 if (elf_section_data (o)->relocs != relocs) 7914 free (relocs); 7915 } 7916 7917 if (sec->rawsize > max_contents_size) 7918 max_contents_size = sec->rawsize; 7919 if (sec->size > max_contents_size) 7920 max_contents_size = sec->size; 7921 7922 /* We are interested in just local symbols, not all 7923 symbols. */ 7924 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour 7925 && (sec->owner->flags & DYNAMIC) == 0) 7926 { 7927 size_t sym_count; 7928 7929 if (elf_bad_symtab (sec->owner)) 7930 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size 7931 / bed->s->sizeof_sym); 7932 else 7933 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; 7934 7935 if (sym_count > max_sym_count) 7936 max_sym_count = sym_count; 7937 7938 if (sym_count > max_sym_shndx_count 7939 && elf_symtab_shndx (sec->owner) != 0) 7940 max_sym_shndx_count = sym_count; 7941 7942 if ((sec->flags & SEC_RELOC) != 0) 7943 { 7944 size_t ext_size; 7945 7946 ext_size = elf_section_data (sec)->rel_hdr.sh_size; 7947 if (ext_size > max_external_reloc_size) 7948 max_external_reloc_size = ext_size; 7949 if (sec->reloc_count > max_internal_reloc_count) 7950 max_internal_reloc_count = sec->reloc_count; 7951 } 7952 } 7953 } 7954 7955 if (reloc_count == 0) 7956 continue; 7957 7958 o->reloc_count += reloc_count; 7959 7960 /* MIPS may have a mix of REL and RELA relocs on sections. 7961 To support this curious ABI we keep reloc counts in 7962 elf_section_data too. We must be careful to add the 7963 relocations from the input section to the right output 7964 count. FIXME: Get rid of one count. We have 7965 o->reloc_count == esdo->rel_count + esdo->rel_count2. */ 7966 rel_count1 = &esdo->rel_count; 7967 if (esdi != NULL) 7968 { 7969 bfd_boolean same_size; 7970 bfd_size_type entsize1; 7971 7972 entsize1 = esdi->rel_hdr.sh_entsize; 7973 BFD_ASSERT (entsize1 == bed->s->sizeof_rel 7974 || entsize1 == bed->s->sizeof_rela); 7975 same_size = !o->use_rela_p == (entsize1 == bed->s->sizeof_rel); 7976 7977 if (!same_size) 7978 rel_count1 = &esdo->rel_count2; 7979 7980 if (esdi->rel_hdr2 != NULL) 7981 { 7982 bfd_size_type entsize2 = esdi->rel_hdr2->sh_entsize; 7983 unsigned int alt_count; 7984 unsigned int *rel_count2; 7985 7986 BFD_ASSERT (entsize2 != entsize1 7987 && (entsize2 == bed->s->sizeof_rel 7988 || entsize2 == bed->s->sizeof_rela)); 7989 7990 rel_count2 = &esdo->rel_count2; 7991 if (!same_size) 7992 rel_count2 = &esdo->rel_count; 7993 7994 /* The following is probably too simplistic if the 7995 backend counts output relocs unusually. */ 7996 BFD_ASSERT (bed->elf_backend_count_relocs == NULL); 7997 alt_count = NUM_SHDR_ENTRIES (esdi->rel_hdr2); 7998 *rel_count2 += alt_count; 7999 reloc_count -= alt_count; 8000 } 8001 } 8002 *rel_count1 += reloc_count; 8003 } 8004 8005 if (o->reloc_count > 0) 8006 o->flags |= SEC_RELOC; 8007 else 8008 { 8009 /* Explicitly clear the SEC_RELOC flag. The linker tends to 8010 set it (this is probably a bug) and if it is set 8011 assign_section_numbers will create a reloc section. */ 8012 o->flags &=~ SEC_RELOC; 8013 } 8014 8015 /* If the SEC_ALLOC flag is not set, force the section VMA to 8016 zero. This is done in elf_fake_sections as well, but forcing 8017 the VMA to 0 here will ensure that relocs against these 8018 sections are handled correctly. */ 8019 if ((o->flags & SEC_ALLOC) == 0 8020 && ! o->user_set_vma) 8021 o->vma = 0; 8022 } 8023 8024 if (! info->relocatable && merged) 8025 elf_link_hash_traverse (elf_hash_table (info), 8026 _bfd_elf_link_sec_merge_syms, abfd); 8027 8028 /* Figure out the file positions for everything but the symbol table 8029 and the relocs. We set symcount to force assign_section_numbers 8030 to create a symbol table. */ 8031 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; 8032 BFD_ASSERT (! abfd->output_has_begun); 8033 if (! _bfd_elf_compute_section_file_positions (abfd, info)) 8034 goto error_return; 8035 8036 /* Set sizes, and assign file positions for reloc sections. */ 8037 for (o = abfd->sections; o != NULL; o = o->next) 8038 { 8039 if ((o->flags & SEC_RELOC) != 0) 8040 { 8041 if (!(_bfd_elf_link_size_reloc_section 8042 (abfd, &elf_section_data (o)->rel_hdr, o))) 8043 goto error_return; 8044 8045 if (elf_section_data (o)->rel_hdr2 8046 && !(_bfd_elf_link_size_reloc_section 8047 (abfd, elf_section_data (o)->rel_hdr2, o))) 8048 goto error_return; 8049 } 8050 8051 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them 8052 to count upwards while actually outputting the relocations. */ 8053 elf_section_data (o)->rel_count = 0; 8054 elf_section_data (o)->rel_count2 = 0; 8055 } 8056 8057 _bfd_elf_assign_file_positions_for_relocs (abfd); 8058 8059 /* We have now assigned file positions for all the sections except 8060 .symtab and .strtab. We start the .symtab section at the current 8061 file position, and write directly to it. We build the .strtab 8062 section in memory. */ 8063 bfd_get_symcount (abfd) = 0; 8064 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 8065 /* sh_name is set in prep_headers. */ 8066 symtab_hdr->sh_type = SHT_SYMTAB; 8067 /* sh_flags, sh_addr and sh_size all start off zero. */ 8068 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 8069 /* sh_link is set in assign_section_numbers. */ 8070 /* sh_info is set below. */ 8071 /* sh_offset is set just below. */ 8072 symtab_hdr->sh_addralign = 1 << bed->s->log_file_align; 8073 8074 off = elf_tdata (abfd)->next_file_pos; 8075 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); 8076 8077 /* Note that at this point elf_tdata (abfd)->next_file_pos is 8078 incorrect. We do not yet know the size of the .symtab section. 8079 We correct next_file_pos below, after we do know the size. */ 8080 8081 /* Allocate a buffer to hold swapped out symbols. This is to avoid 8082 continuously seeking to the right position in the file. */ 8083 if (! info->keep_memory || max_sym_count < 20) 8084 finfo.symbuf_size = 20; 8085 else 8086 finfo.symbuf_size = max_sym_count; 8087 amt = finfo.symbuf_size; 8088 amt *= bed->s->sizeof_sym; 8089 finfo.symbuf = bfd_malloc (amt); 8090 if (finfo.symbuf == NULL) 8091 goto error_return; 8092 if (elf_numsections (abfd) > SHN_LORESERVE) 8093 { 8094 /* Wild guess at number of output symbols. realloc'd as needed. */ 8095 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; 8096 finfo.shndxbuf_size = amt; 8097 amt *= sizeof (Elf_External_Sym_Shndx); 8098 finfo.symshndxbuf = bfd_zmalloc (amt); 8099 if (finfo.symshndxbuf == NULL) 8100 goto error_return; 8101 } 8102 8103 /* Start writing out the symbol table. The first symbol is always a 8104 dummy symbol. */ 8105 if (info->strip != strip_all 8106 || emit_relocs) 8107 { 8108 elfsym.st_value = 0; 8109 elfsym.st_size = 0; 8110 elfsym.st_info = 0; 8111 elfsym.st_other = 0; 8112 elfsym.st_shndx = SHN_UNDEF; 8113 if (! elf_link_output_sym (&finfo, NULL, &elfsym, bfd_und_section_ptr, 8114 NULL)) 8115 goto error_return; 8116 } 8117 8118 /* Output a symbol for each section. We output these even if we are 8119 discarding local symbols, since they are used for relocs. These 8120 symbols have no names. We store the index of each one in the 8121 index field of the section, so that we can find it again when 8122 outputting relocs. */ 8123 if (info->strip != strip_all 8124 || emit_relocs) 8125 { 8126 elfsym.st_size = 0; 8127 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 8128 elfsym.st_other = 0; 8129 for (i = 1; i < elf_numsections (abfd); i++) 8130 { 8131 o = bfd_section_from_elf_index (abfd, i); 8132 if (o != NULL) 8133 o->target_index = bfd_get_symcount (abfd); 8134 elfsym.st_shndx = i; 8135 if (info->relocatable || o == NULL) 8136 elfsym.st_value = 0; 8137 else 8138 elfsym.st_value = o->vma; 8139 if (! elf_link_output_sym (&finfo, NULL, &elfsym, o, NULL)) 8140 goto error_return; 8141 if (i == SHN_LORESERVE - 1) 8142 i += SHN_HIRESERVE + 1 - SHN_LORESERVE; 8143 } 8144 } 8145 8146 /* Allocate some memory to hold information read in from the input 8147 files. */ 8148 if (max_contents_size != 0) 8149 { 8150 finfo.contents = bfd_malloc (max_contents_size); 8151 if (finfo.contents == NULL) 8152 goto error_return; 8153 } 8154 8155 if (max_external_reloc_size != 0) 8156 { 8157 finfo.external_relocs = bfd_malloc (max_external_reloc_size); 8158 if (finfo.external_relocs == NULL) 8159 goto error_return; 8160 } 8161 8162 if (max_internal_reloc_count != 0) 8163 { 8164 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; 8165 amt *= sizeof (Elf_Internal_Rela); 8166 finfo.internal_relocs = bfd_malloc (amt); 8167 if (finfo.internal_relocs == NULL) 8168 goto error_return; 8169 } 8170 8171 if (max_sym_count != 0) 8172 { 8173 amt = max_sym_count * bed->s->sizeof_sym; 8174 finfo.external_syms = bfd_malloc (amt); 8175 if (finfo.external_syms == NULL) 8176 goto error_return; 8177 8178 amt = max_sym_count * sizeof (Elf_Internal_Sym); 8179 finfo.internal_syms = bfd_malloc (amt); 8180 if (finfo.internal_syms == NULL) 8181 goto error_return; 8182 8183 amt = max_sym_count * sizeof (long); 8184 finfo.indices = bfd_malloc (amt); 8185 if (finfo.indices == NULL) 8186 goto error_return; 8187 8188 amt = max_sym_count * sizeof (asection *); 8189 finfo.sections = bfd_malloc (amt); 8190 if (finfo.sections == NULL) 8191 goto error_return; 8192 } 8193 8194 if (max_sym_shndx_count != 0) 8195 { 8196 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); 8197 finfo.locsym_shndx = bfd_malloc (amt); 8198 if (finfo.locsym_shndx == NULL) 8199 goto error_return; 8200 } 8201 8202 if (elf_hash_table (info)->tls_sec) 8203 { 8204 bfd_vma base, end = 0; 8205 asection *sec; 8206 8207 for (sec = elf_hash_table (info)->tls_sec; 8208 sec && (sec->flags & SEC_THREAD_LOCAL); 8209 sec = sec->next) 8210 { 8211 bfd_size_type size = sec->size; 8212 8213 if (size == 0 8214 && (sec->flags & SEC_HAS_CONTENTS) == 0) 8215 { 8216 struct bfd_link_order *o = sec->map_tail.link_order; 8217 if (o != NULL) 8218 size = o->offset + o->size; 8219 } 8220 end = sec->vma + size; 8221 } 8222 base = elf_hash_table (info)->tls_sec->vma; 8223 end = align_power (end, elf_hash_table (info)->tls_sec->alignment_power); 8224 elf_hash_table (info)->tls_size = end - base; 8225 } 8226 8227 /* Reorder SHF_LINK_ORDER sections. */ 8228 for (o = abfd->sections; o != NULL; o = o->next) 8229 { 8230 if (!elf_fixup_link_order (abfd, o)) 8231 return FALSE; 8232 } 8233 8234 /* Since ELF permits relocations to be against local symbols, we 8235 must have the local symbols available when we do the relocations. 8236 Since we would rather only read the local symbols once, and we 8237 would rather not keep them in memory, we handle all the 8238 relocations for a single input file at the same time. 8239 8240 Unfortunately, there is no way to know the total number of local 8241 symbols until we have seen all of them, and the local symbol 8242 indices precede the global symbol indices. This means that when 8243 we are generating relocatable output, and we see a reloc against 8244 a global symbol, we can not know the symbol index until we have 8245 finished examining all the local symbols to see which ones we are 8246 going to output. To deal with this, we keep the relocations in 8247 memory, and don't output them until the end of the link. This is 8248 an unfortunate waste of memory, but I don't see a good way around 8249 it. Fortunately, it only happens when performing a relocatable 8250 link, which is not the common case. FIXME: If keep_memory is set 8251 we could write the relocs out and then read them again; I don't 8252 know how bad the memory loss will be. */ 8253 8254 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 8255 sub->output_has_begun = FALSE; 8256 for (o = abfd->sections; o != NULL; o = o->next) 8257 { 8258 for (p = o->map_head.link_order; p != NULL; p = p->next) 8259 { 8260 if (p->type == bfd_indirect_link_order 8261 && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) 8262 == bfd_target_elf_flavour) 8263 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) 8264 { 8265 if (! sub->output_has_begun) 8266 { 8267 if (! elf_link_input_bfd (&finfo, sub)) 8268 goto error_return; 8269 sub->output_has_begun = TRUE; 8270 } 8271 } 8272 else if (p->type == bfd_section_reloc_link_order 8273 || p->type == bfd_symbol_reloc_link_order) 8274 { 8275 if (! elf_reloc_link_order (abfd, info, o, p)) 8276 goto error_return; 8277 } 8278 else 8279 { 8280 if (! _bfd_default_link_order (abfd, info, o, p)) 8281 goto error_return; 8282 } 8283 } 8284 } 8285 8286 /* Free symbol buffer if needed. */ 8287 if (!info->reduce_memory_overheads) 8288 { 8289 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 8290 if (elf_tdata (sub)->symbuf) 8291 free (elf_tdata (sub)->symbuf); 8292 } 8293 8294 /* Output any global symbols that got converted to local in a 8295 version script or due to symbol visibility. We do this in a 8296 separate step since ELF requires all local symbols to appear 8297 prior to any global symbols. FIXME: We should only do this if 8298 some global symbols were, in fact, converted to become local. 8299 FIXME: Will this work correctly with the Irix 5 linker? */ 8300 eoinfo.failed = FALSE; 8301 eoinfo.finfo = &finfo; 8302 eoinfo.localsyms = TRUE; 8303 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, 8304 &eoinfo); 8305 if (eoinfo.failed) 8306 return FALSE; 8307 8308 /* That wrote out all the local symbols. Finish up the symbol table 8309 with the global symbols. Even if we want to strip everything we 8310 can, we still need to deal with those global symbols that got 8311 converted to local in a version script. */ 8312 8313 /* The sh_info field records the index of the first non local symbol. */ 8314 symtab_hdr->sh_info = bfd_get_symcount (abfd); 8315 8316 if (dynamic 8317 && finfo.dynsym_sec->output_section != bfd_abs_section_ptr) 8318 { 8319 Elf_Internal_Sym sym; 8320 bfd_byte *dynsym = finfo.dynsym_sec->contents; 8321 long last_local = 0; 8322 8323 /* Write out the section symbols for the output sections. */ 8324 if (info->shared || elf_hash_table (info)->is_relocatable_executable) 8325 { 8326 asection *s; 8327 8328 sym.st_size = 0; 8329 sym.st_name = 0; 8330 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 8331 sym.st_other = 0; 8332 8333 for (s = abfd->sections; s != NULL; s = s->next) 8334 { 8335 int indx; 8336 bfd_byte *dest; 8337 long dynindx; 8338 8339 dynindx = elf_section_data (s)->dynindx; 8340 if (dynindx <= 0) 8341 continue; 8342 indx = elf_section_data (s)->this_idx; 8343 BFD_ASSERT (indx > 0); 8344 sym.st_shndx = indx; 8345 if (! check_dynsym (abfd, &sym)) 8346 return FALSE; 8347 sym.st_value = s->vma; 8348 dest = dynsym + dynindx * bed->s->sizeof_sym; 8349 if (last_local < dynindx) 8350 last_local = dynindx; 8351 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 8352 } 8353 } 8354 8355 /* Write out the local dynsyms. */ 8356 if (elf_hash_table (info)->dynlocal) 8357 { 8358 struct elf_link_local_dynamic_entry *e; 8359 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 8360 { 8361 asection *s; 8362 bfd_byte *dest; 8363 8364 sym.st_size = e->isym.st_size; 8365 sym.st_other = e->isym.st_other; 8366 8367 /* Copy the internal symbol as is. 8368 Note that we saved a word of storage and overwrote 8369 the original st_name with the dynstr_index. */ 8370 sym = e->isym; 8371 8372 if (e->isym.st_shndx != SHN_UNDEF 8373 && (e->isym.st_shndx < SHN_LORESERVE 8374 || e->isym.st_shndx > SHN_HIRESERVE)) 8375 { 8376 s = bfd_section_from_elf_index (e->input_bfd, 8377 e->isym.st_shndx); 8378 8379 sym.st_shndx = 8380 elf_section_data (s->output_section)->this_idx; 8381 if (! check_dynsym (abfd, &sym)) 8382 return FALSE; 8383 sym.st_value = (s->output_section->vma 8384 + s->output_offset 8385 + e->isym.st_value); 8386 } 8387 8388 if (last_local < e->dynindx) 8389 last_local = e->dynindx; 8390 8391 dest = dynsym + e->dynindx * bed->s->sizeof_sym; 8392 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 8393 } 8394 } 8395 8396 elf_section_data (finfo.dynsym_sec->output_section)->this_hdr.sh_info = 8397 last_local + 1; 8398 } 8399 8400 /* We get the global symbols from the hash table. */ 8401 eoinfo.failed = FALSE; 8402 eoinfo.localsyms = FALSE; 8403 eoinfo.finfo = &finfo; 8404 elf_link_hash_traverse (elf_hash_table (info), elf_link_output_extsym, 8405 &eoinfo); 8406 if (eoinfo.failed) 8407 return FALSE; 8408 8409 /* If backend needs to output some symbols not present in the hash 8410 table, do it now. */ 8411 if (bed->elf_backend_output_arch_syms) 8412 { 8413 typedef bfd_boolean (*out_sym_func) 8414 (void *, const char *, Elf_Internal_Sym *, asection *, 8415 struct elf_link_hash_entry *); 8416 8417 if (! ((*bed->elf_backend_output_arch_syms) 8418 (abfd, info, &finfo, (out_sym_func) elf_link_output_sym))) 8419 return FALSE; 8420 } 8421 8422 /* Flush all symbols to the file. */ 8423 if (! elf_link_flush_output_syms (&finfo, bed)) 8424 return FALSE; 8425 8426 /* Now we know the size of the symtab section. */ 8427 off += symtab_hdr->sh_size; 8428 8429 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 8430 if (symtab_shndx_hdr->sh_name != 0) 8431 { 8432 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 8433 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 8434 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 8435 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); 8436 symtab_shndx_hdr->sh_size = amt; 8437 8438 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, 8439 off, TRUE); 8440 8441 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 8442 || (bfd_bwrite (finfo.symshndxbuf, amt, abfd) != amt)) 8443 return FALSE; 8444 } 8445 8446 8447 /* Finish up and write out the symbol string table (.strtab) 8448 section. */ 8449 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 8450 /* sh_name was set in prep_headers. */ 8451 symstrtab_hdr->sh_type = SHT_STRTAB; 8452 symstrtab_hdr->sh_flags = 0; 8453 symstrtab_hdr->sh_addr = 0; 8454 symstrtab_hdr->sh_size = _bfd_stringtab_size (finfo.symstrtab); 8455 symstrtab_hdr->sh_entsize = 0; 8456 symstrtab_hdr->sh_link = 0; 8457 symstrtab_hdr->sh_info = 0; 8458 /* sh_offset is set just below. */ 8459 symstrtab_hdr->sh_addralign = 1; 8460 8461 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); 8462 elf_tdata (abfd)->next_file_pos = off; 8463 8464 if (bfd_get_symcount (abfd) > 0) 8465 { 8466 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 8467 || ! _bfd_stringtab_emit (abfd, finfo.symstrtab)) 8468 return FALSE; 8469 } 8470 8471 /* Adjust the relocs to have the correct symbol indices. */ 8472 for (o = abfd->sections; o != NULL; o = o->next) 8473 { 8474 if ((o->flags & SEC_RELOC) == 0) 8475 continue; 8476 8477 elf_link_adjust_relocs (abfd, &elf_section_data (o)->rel_hdr, 8478 elf_section_data (o)->rel_count, 8479 elf_section_data (o)->rel_hashes); 8480 if (elf_section_data (o)->rel_hdr2 != NULL) 8481 elf_link_adjust_relocs (abfd, elf_section_data (o)->rel_hdr2, 8482 elf_section_data (o)->rel_count2, 8483 (elf_section_data (o)->rel_hashes 8484 + elf_section_data (o)->rel_count)); 8485 8486 /* Set the reloc_count field to 0 to prevent write_relocs from 8487 trying to swap the relocs out itself. */ 8488 o->reloc_count = 0; 8489 } 8490 8491 if (dynamic && info->combreloc && dynobj != NULL) 8492 relativecount = elf_link_sort_relocs (abfd, info, &reldyn); 8493 8494 /* If we are linking against a dynamic object, or generating a 8495 shared library, finish up the dynamic linking information. */ 8496 if (dynamic) 8497 { 8498 bfd_byte *dyncon, *dynconend; 8499 8500 /* Fix up .dynamic entries. */ 8501 o = bfd_get_section_by_name (dynobj, ".dynamic"); 8502 BFD_ASSERT (o != NULL); 8503 8504 dyncon = o->contents; 8505 dynconend = o->contents + o->size; 8506 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 8507 { 8508 Elf_Internal_Dyn dyn; 8509 const char *name; 8510 unsigned int type; 8511 8512 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 8513 8514 switch (dyn.d_tag) 8515 { 8516 default: 8517 continue; 8518 case DT_NULL: 8519 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) 8520 { 8521 switch (elf_section_data (reldyn)->this_hdr.sh_type) 8522 { 8523 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; 8524 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; 8525 default: continue; 8526 } 8527 dyn.d_un.d_val = relativecount; 8528 relativecount = 0; 8529 break; 8530 } 8531 continue; 8532 8533 case DT_INIT: 8534 name = info->init_function; 8535 goto get_sym; 8536 case DT_FINI: 8537 name = info->fini_function; 8538 get_sym: 8539 { 8540 struct elf_link_hash_entry *h; 8541 8542 h = elf_link_hash_lookup (elf_hash_table (info), name, 8543 FALSE, FALSE, TRUE); 8544 if (h != NULL 8545 && (h->root.type == bfd_link_hash_defined 8546 || h->root.type == bfd_link_hash_defweak)) 8547 { 8548 dyn.d_un.d_val = h->root.u.def.value; 8549 o = h->root.u.def.section; 8550 if (o->output_section != NULL) 8551 dyn.d_un.d_val += (o->output_section->vma 8552 + o->output_offset); 8553 else 8554 { 8555 /* The symbol is imported from another shared 8556 library and does not apply to this one. */ 8557 dyn.d_un.d_val = 0; 8558 } 8559 break; 8560 } 8561 } 8562 continue; 8563 8564 case DT_PREINIT_ARRAYSZ: 8565 name = ".preinit_array"; 8566 goto get_size; 8567 case DT_INIT_ARRAYSZ: 8568 name = ".init_array"; 8569 goto get_size; 8570 case DT_FINI_ARRAYSZ: 8571 name = ".fini_array"; 8572 get_size: 8573 o = bfd_get_section_by_name (abfd, name); 8574 if (o == NULL) 8575 { 8576 (*_bfd_error_handler) 8577 (_("%B: could not find output section %s"), abfd, name); 8578 goto error_return; 8579 } 8580 if (o->size == 0) 8581 (*_bfd_error_handler) 8582 (_("warning: %s section has zero size"), name); 8583 dyn.d_un.d_val = o->size; 8584 break; 8585 8586 case DT_PREINIT_ARRAY: 8587 name = ".preinit_array"; 8588 goto get_vma; 8589 case DT_INIT_ARRAY: 8590 name = ".init_array"; 8591 goto get_vma; 8592 case DT_FINI_ARRAY: 8593 name = ".fini_array"; 8594 goto get_vma; 8595 8596 case DT_HASH: 8597 name = ".hash"; 8598 goto get_vma; 8599 case DT_STRTAB: 8600 name = ".dynstr"; 8601 goto get_vma; 8602 case DT_SYMTAB: 8603 name = ".dynsym"; 8604 goto get_vma; 8605 case DT_VERDEF: 8606 name = ".gnu.version_d"; 8607 goto get_vma; 8608 case DT_VERNEED: 8609 name = ".gnu.version_r"; 8610 goto get_vma; 8611 case DT_VERSYM: 8612 name = ".gnu.version"; 8613 get_vma: 8614 o = bfd_get_section_by_name (abfd, name); 8615 if (o == NULL) 8616 { 8617 (*_bfd_error_handler) 8618 (_("%B: could not find output section %s"), abfd, name); 8619 goto error_return; 8620 } 8621 dyn.d_un.d_ptr = o->vma; 8622 break; 8623 8624 case DT_REL: 8625 case DT_RELA: 8626 case DT_RELSZ: 8627 case DT_RELASZ: 8628 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) 8629 type = SHT_REL; 8630 else 8631 type = SHT_RELA; 8632 dyn.d_un.d_val = 0; 8633 for (i = 1; i < elf_numsections (abfd); i++) 8634 { 8635 Elf_Internal_Shdr *hdr; 8636 8637 hdr = elf_elfsections (abfd)[i]; 8638 if (hdr->sh_type == type 8639 && (hdr->sh_flags & SHF_ALLOC) != 0) 8640 { 8641 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) 8642 dyn.d_un.d_val += hdr->sh_size; 8643 else 8644 { 8645 if (dyn.d_un.d_val == 0 8646 || hdr->sh_addr < dyn.d_un.d_val) 8647 dyn.d_un.d_val = hdr->sh_addr; 8648 } 8649 } 8650 } 8651 break; 8652 } 8653 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 8654 } 8655 } 8656 8657 /* If we have created any dynamic sections, then output them. */ 8658 if (dynobj != NULL) 8659 { 8660 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) 8661 goto error_return; 8662 8663 /* Check for DT_TEXTREL (late, in case the backend removes it). */ 8664 if (!info->allow_textrel || (info->warn_shared_textrel && info->shared)) 8665 { 8666 bfd_byte *dyncon, *dynconend; 8667 8668 /* Fix up .dynamic entries. */ 8669 o = bfd_get_section_by_name (dynobj, ".dynamic"); 8670 if (o != NULL) 8671 { 8672 dyncon = o->contents; 8673 dynconend = o->contents + o->size; 8674 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 8675 { 8676 Elf_Internal_Dyn dyn; 8677 8678 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 8679 8680 if (dyn.d_tag == DT_TEXTREL) 8681 { 8682 _bfd_error_handler 8683 (_("warning: creating a DT_TEXTREL in a shared object.")); 8684 #if 0 8685 if (!info->allow_textrel) 8686 goto error_return; 8687 #endif 8688 break; 8689 } 8690 } 8691 } 8692 } 8693 8694 for (o = dynobj->sections; o != NULL; o = o->next) 8695 { 8696 if ((o->flags & SEC_HAS_CONTENTS) == 0 8697 || o->size == 0 8698 || o->output_section == bfd_abs_section_ptr) 8699 continue; 8700 if ((o->flags & SEC_LINKER_CREATED) == 0) 8701 { 8702 /* At this point, we are only interested in sections 8703 created by _bfd_elf_link_create_dynamic_sections. */ 8704 continue; 8705 } 8706 if (elf_hash_table (info)->stab_info.stabstr == o) 8707 continue; 8708 if (elf_hash_table (info)->eh_info.hdr_sec == o) 8709 continue; 8710 if ((elf_section_data (o->output_section)->this_hdr.sh_type 8711 != SHT_STRTAB) 8712 || strcmp (bfd_get_section_name (abfd, o), ".dynstr") != 0) 8713 { 8714 if (! bfd_set_section_contents (abfd, o->output_section, 8715 o->contents, 8716 (file_ptr) o->output_offset, 8717 o->size)) 8718 goto error_return; 8719 } 8720 else 8721 { 8722 /* The contents of the .dynstr section are actually in a 8723 stringtab. */ 8724 off = elf_section_data (o->output_section)->this_hdr.sh_offset; 8725 if (bfd_seek (abfd, off, SEEK_SET) != 0 8726 || ! _bfd_elf_strtab_emit (abfd, 8727 elf_hash_table (info)->dynstr)) 8728 goto error_return; 8729 } 8730 } 8731 } 8732 8733 if (info->relocatable) 8734 { 8735 bfd_boolean failed = FALSE; 8736 8737 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 8738 if (failed) 8739 goto error_return; 8740 } 8741 8742 /* If we have optimized stabs strings, output them. */ 8743 if (elf_hash_table (info)->stab_info.stabstr != NULL) 8744 { 8745 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) 8746 goto error_return; 8747 } 8748 8749 if (info->eh_frame_hdr) 8750 { 8751 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) 8752 goto error_return; 8753 } 8754 8755 if (finfo.symstrtab != NULL) 8756 _bfd_stringtab_free (finfo.symstrtab); 8757 if (finfo.contents != NULL) 8758 free (finfo.contents); 8759 if (finfo.external_relocs != NULL) 8760 free (finfo.external_relocs); 8761 if (finfo.internal_relocs != NULL) 8762 free (finfo.internal_relocs); 8763 if (finfo.external_syms != NULL) 8764 free (finfo.external_syms); 8765 if (finfo.locsym_shndx != NULL) 8766 free (finfo.locsym_shndx); 8767 if (finfo.internal_syms != NULL) 8768 free (finfo.internal_syms); 8769 if (finfo.indices != NULL) 8770 free (finfo.indices); 8771 if (finfo.sections != NULL) 8772 free (finfo.sections); 8773 if (finfo.symbuf != NULL) 8774 free (finfo.symbuf); 8775 if (finfo.symshndxbuf != NULL) 8776 free (finfo.symshndxbuf); 8777 for (o = abfd->sections; o != NULL; o = o->next) 8778 { 8779 if ((o->flags & SEC_RELOC) != 0 8780 && elf_section_data (o)->rel_hashes != NULL) 8781 free (elf_section_data (o)->rel_hashes); 8782 } 8783 8784 elf_tdata (abfd)->linker = TRUE; 8785 8786 return TRUE; 8787 8788 error_return: 8789 if (finfo.symstrtab != NULL) 8790 _bfd_stringtab_free (finfo.symstrtab); 8791 if (finfo.contents != NULL) 8792 free (finfo.contents); 8793 if (finfo.external_relocs != NULL) 8794 free (finfo.external_relocs); 8795 if (finfo.internal_relocs != NULL) 8796 free (finfo.internal_relocs); 8797 if (finfo.external_syms != NULL) 8798 free (finfo.external_syms); 8799 if (finfo.locsym_shndx != NULL) 8800 free (finfo.locsym_shndx); 8801 if (finfo.internal_syms != NULL) 8802 free (finfo.internal_syms); 8803 if (finfo.indices != NULL) 8804 free (finfo.indices); 8805 if (finfo.sections != NULL) 8806 free (finfo.sections); 8807 if (finfo.symbuf != NULL) 8808 free (finfo.symbuf); 8809 if (finfo.symshndxbuf != NULL) 8810 free (finfo.symshndxbuf); 8811 for (o = abfd->sections; o != NULL; o = o->next) 8812 { 8813 if ((o->flags & SEC_RELOC) != 0 8814 && elf_section_data (o)->rel_hashes != NULL) 8815 free (elf_section_data (o)->rel_hashes); 8816 } 8817 8818 return FALSE; 8819 } 8820 8821 /* Garbage collect unused sections. */ 8822 8823 /* The mark phase of garbage collection. For a given section, mark 8824 it and any sections in this section's group, and all the sections 8825 which define symbols to which it refers. */ 8826 8827 typedef asection * (*gc_mark_hook_fn) 8828 (asection *, struct bfd_link_info *, Elf_Internal_Rela *, 8829 struct elf_link_hash_entry *, Elf_Internal_Sym *); 8830 8831 bfd_boolean 8832 _bfd_elf_gc_mark (struct bfd_link_info *info, 8833 asection *sec, 8834 gc_mark_hook_fn gc_mark_hook) 8835 { 8836 bfd_boolean ret; 8837 bfd_boolean is_eh; 8838 asection *group_sec; 8839 8840 sec->gc_mark = 1; 8841 8842 /* Mark all the sections in the group. */ 8843 group_sec = elf_section_data (sec)->next_in_group; 8844 if (group_sec && !group_sec->gc_mark) 8845 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) 8846 return FALSE; 8847 8848 /* Look through the section relocs. */ 8849 ret = TRUE; 8850 is_eh = strcmp (sec->name, ".eh_frame") == 0; 8851 if ((sec->flags & SEC_RELOC) != 0 && sec->reloc_count > 0) 8852 { 8853 Elf_Internal_Rela *relstart, *rel, *relend; 8854 Elf_Internal_Shdr *symtab_hdr; 8855 struct elf_link_hash_entry **sym_hashes; 8856 size_t nlocsyms; 8857 size_t extsymoff; 8858 bfd *input_bfd = sec->owner; 8859 const struct elf_backend_data *bed = get_elf_backend_data (input_bfd); 8860 Elf_Internal_Sym *isym = NULL; 8861 int r_sym_shift; 8862 8863 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 8864 sym_hashes = elf_sym_hashes (input_bfd); 8865 8866 /* Read the local symbols. */ 8867 if (elf_bad_symtab (input_bfd)) 8868 { 8869 nlocsyms = symtab_hdr->sh_size / bed->s->sizeof_sym; 8870 extsymoff = 0; 8871 } 8872 else 8873 extsymoff = nlocsyms = symtab_hdr->sh_info; 8874 8875 isym = (Elf_Internal_Sym *) symtab_hdr->contents; 8876 if (isym == NULL && nlocsyms != 0) 8877 { 8878 isym = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, nlocsyms, 0, 8879 NULL, NULL, NULL); 8880 if (isym == NULL) 8881 return FALSE; 8882 } 8883 8884 /* Read the relocations. */ 8885 relstart = _bfd_elf_link_read_relocs (input_bfd, sec, NULL, NULL, 8886 info->keep_memory); 8887 if (relstart == NULL) 8888 { 8889 ret = FALSE; 8890 goto out1; 8891 } 8892 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; 8893 8894 if (bed->s->arch_size == 32) 8895 r_sym_shift = 8; 8896 else 8897 r_sym_shift = 32; 8898 8899 for (rel = relstart; rel < relend; rel++) 8900 { 8901 unsigned long r_symndx; 8902 asection *rsec; 8903 struct elf_link_hash_entry *h; 8904 8905 r_symndx = rel->r_info >> r_sym_shift; 8906 if (r_symndx == 0) 8907 continue; 8908 8909 if (r_symndx >= nlocsyms 8910 || ELF_ST_BIND (isym[r_symndx].st_info) != STB_LOCAL) 8911 { 8912 h = sym_hashes[r_symndx - extsymoff]; 8913 while (h->root.type == bfd_link_hash_indirect 8914 || h->root.type == bfd_link_hash_warning) 8915 h = (struct elf_link_hash_entry *) h->root.u.i.link; 8916 rsec = (*gc_mark_hook) (sec, info, rel, h, NULL); 8917 } 8918 else 8919 { 8920 rsec = (*gc_mark_hook) (sec, info, rel, NULL, &isym[r_symndx]); 8921 } 8922 8923 if (rsec && !rsec->gc_mark) 8924 { 8925 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour) 8926 rsec->gc_mark = 1; 8927 else if (is_eh) 8928 rsec->gc_mark_from_eh = 1; 8929 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) 8930 { 8931 ret = FALSE; 8932 goto out2; 8933 } 8934 } 8935 } 8936 8937 out2: 8938 if (elf_section_data (sec)->relocs != relstart) 8939 free (relstart); 8940 out1: 8941 if (isym != NULL && symtab_hdr->contents != (unsigned char *) isym) 8942 { 8943 if (! info->keep_memory) 8944 free (isym); 8945 else 8946 symtab_hdr->contents = (unsigned char *) isym; 8947 } 8948 } 8949 8950 return ret; 8951 } 8952 8953 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ 8954 8955 struct elf_gc_sweep_symbol_info { 8956 struct bfd_link_info *info; 8957 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, 8958 bfd_boolean); 8959 }; 8960 8961 static bfd_boolean 8962 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) 8963 { 8964 if (h->root.type == bfd_link_hash_warning) 8965 h = (struct elf_link_hash_entry *) h->root.u.i.link; 8966 8967 if ((h->root.type == bfd_link_hash_defined 8968 || h->root.type == bfd_link_hash_defweak) 8969 && !h->root.u.def.section->gc_mark 8970 && !(h->root.u.def.section->owner->flags & DYNAMIC)) 8971 { 8972 struct elf_gc_sweep_symbol_info *inf = data; 8973 (*inf->hide_symbol) (inf->info, h, TRUE); 8974 } 8975 8976 return TRUE; 8977 } 8978 8979 /* The sweep phase of garbage collection. Remove all garbage sections. */ 8980 8981 typedef bfd_boolean (*gc_sweep_hook_fn) 8982 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); 8983 8984 static bfd_boolean 8985 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) 8986 { 8987 bfd *sub; 8988 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8989 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; 8990 unsigned long section_sym_count; 8991 struct elf_gc_sweep_symbol_info sweep_info; 8992 8993 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 8994 { 8995 asection *o; 8996 8997 if (bfd_get_flavour (sub) != bfd_target_elf_flavour) 8998 continue; 8999 9000 for (o = sub->sections; o != NULL; o = o->next) 9001 { 9002 /* Keep debug and special sections. */ 9003 if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0 9004 || elf_section_data (o)->this_hdr.sh_type == SHT_NOTE 9005 || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 9006 o->gc_mark = 1; 9007 9008 if (o->gc_mark) 9009 continue; 9010 9011 /* Skip sweeping sections already excluded. */ 9012 if (o->flags & SEC_EXCLUDE) 9013 continue; 9014 9015 /* Since this is early in the link process, it is simple 9016 to remove a section from the output. */ 9017 o->flags |= SEC_EXCLUDE; 9018 9019 /* But we also have to update some of the relocation 9020 info we collected before. */ 9021 if (gc_sweep_hook 9022 && (o->flags & SEC_RELOC) != 0 9023 && o->reloc_count > 0 9024 && !bfd_is_abs_section (o->output_section)) 9025 { 9026 Elf_Internal_Rela *internal_relocs; 9027 bfd_boolean r; 9028 9029 internal_relocs 9030 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, 9031 info->keep_memory); 9032 if (internal_relocs == NULL) 9033 return FALSE; 9034 9035 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); 9036 9037 if (elf_section_data (o)->relocs != internal_relocs) 9038 free (internal_relocs); 9039 9040 if (!r) 9041 return FALSE; 9042 } 9043 } 9044 } 9045 9046 /* Remove the symbols that were in the swept sections from the dynamic 9047 symbol table. GCFIXME: Anyone know how to get them out of the 9048 static symbol table as well? */ 9049 sweep_info.info = info; 9050 sweep_info.hide_symbol = bed->elf_backend_hide_symbol; 9051 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, 9052 &sweep_info); 9053 9054 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); 9055 return TRUE; 9056 } 9057 9058 /* Propagate collected vtable information. This is called through 9059 elf_link_hash_traverse. */ 9060 9061 static bfd_boolean 9062 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) 9063 { 9064 if (h->root.type == bfd_link_hash_warning) 9065 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9066 9067 /* Those that are not vtables. */ 9068 if (h->vtable == NULL || h->vtable->parent == NULL) 9069 return TRUE; 9070 9071 /* Those vtables that do not have parents, we cannot merge. */ 9072 if (h->vtable->parent == (struct elf_link_hash_entry *) -1) 9073 return TRUE; 9074 9075 /* If we've already been done, exit. */ 9076 if (h->vtable->used && h->vtable->used[-1]) 9077 return TRUE; 9078 9079 /* Make sure the parent's table is up to date. */ 9080 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); 9081 9082 if (h->vtable->used == NULL) 9083 { 9084 /* None of this table's entries were referenced. Re-use the 9085 parent's table. */ 9086 h->vtable->used = h->vtable->parent->vtable->used; 9087 h->vtable->size = h->vtable->parent->vtable->size; 9088 } 9089 else 9090 { 9091 size_t n; 9092 bfd_boolean *cu, *pu; 9093 9094 /* Or the parent's entries into ours. */ 9095 cu = h->vtable->used; 9096 cu[-1] = TRUE; 9097 pu = h->vtable->parent->vtable->used; 9098 if (pu != NULL) 9099 { 9100 const struct elf_backend_data *bed; 9101 unsigned int log_file_align; 9102 9103 bed = get_elf_backend_data (h->root.u.def.section->owner); 9104 log_file_align = bed->s->log_file_align; 9105 n = h->vtable->parent->vtable->size >> log_file_align; 9106 while (n--) 9107 { 9108 if (*pu) 9109 *cu = TRUE; 9110 pu++; 9111 cu++; 9112 } 9113 } 9114 } 9115 9116 return TRUE; 9117 } 9118 9119 static bfd_boolean 9120 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) 9121 { 9122 asection *sec; 9123 bfd_vma hstart, hend; 9124 Elf_Internal_Rela *relstart, *relend, *rel; 9125 const struct elf_backend_data *bed; 9126 unsigned int log_file_align; 9127 9128 if (h->root.type == bfd_link_hash_warning) 9129 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9130 9131 /* Take care of both those symbols that do not describe vtables as 9132 well as those that are not loaded. */ 9133 if (h->vtable == NULL || h->vtable->parent == NULL) 9134 return TRUE; 9135 9136 BFD_ASSERT (h->root.type == bfd_link_hash_defined 9137 || h->root.type == bfd_link_hash_defweak); 9138 9139 sec = h->root.u.def.section; 9140 hstart = h->root.u.def.value; 9141 hend = hstart + h->size; 9142 9143 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); 9144 if (!relstart) 9145 return *(bfd_boolean *) okp = FALSE; 9146 bed = get_elf_backend_data (sec->owner); 9147 log_file_align = bed->s->log_file_align; 9148 9149 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; 9150 9151 for (rel = relstart; rel < relend; ++rel) 9152 if (rel->r_offset >= hstart && rel->r_offset < hend) 9153 { 9154 /* If the entry is in use, do nothing. */ 9155 if (h->vtable->used 9156 && (rel->r_offset - hstart) < h->vtable->size) 9157 { 9158 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; 9159 if (h->vtable->used[entry]) 9160 continue; 9161 } 9162 /* Otherwise, kill it. */ 9163 rel->r_offset = rel->r_info = rel->r_addend = 0; 9164 } 9165 9166 return TRUE; 9167 } 9168 9169 /* Mark sections containing dynamically referenced symbols. When 9170 building shared libraries, we must assume that any visible symbol is 9171 referenced. */ 9172 9173 bfd_boolean 9174 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) 9175 { 9176 struct bfd_link_info *info = (struct bfd_link_info *) inf; 9177 9178 if (h->root.type == bfd_link_hash_warning) 9179 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9180 9181 if ((h->root.type == bfd_link_hash_defined 9182 || h->root.type == bfd_link_hash_defweak) 9183 && (h->ref_dynamic 9184 || (!info->executable 9185 && h->def_regular 9186 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 9187 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN))) 9188 h->root.u.def.section->flags |= SEC_KEEP; 9189 9190 return TRUE; 9191 } 9192 9193 /* Do mark and sweep of unused sections. */ 9194 9195 bfd_boolean 9196 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) 9197 { 9198 bfd_boolean ok = TRUE; 9199 bfd *sub; 9200 asection * (*gc_mark_hook) 9201 (asection *, struct bfd_link_info *, Elf_Internal_Rela *, 9202 struct elf_link_hash_entry *h, Elf_Internal_Sym *); 9203 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9204 9205 if (!bed->can_gc_sections 9206 || info->relocatable 9207 || info->emitrelocations 9208 || !is_elf_hash_table (info->hash)) 9209 { 9210 (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); 9211 return TRUE; 9212 } 9213 9214 /* Apply transitive closure to the vtable entry usage info. */ 9215 elf_link_hash_traverse (elf_hash_table (info), 9216 elf_gc_propagate_vtable_entries_used, 9217 &ok); 9218 if (!ok) 9219 return FALSE; 9220 9221 /* Kill the vtable relocations that were not used. */ 9222 elf_link_hash_traverse (elf_hash_table (info), 9223 elf_gc_smash_unused_vtentry_relocs, 9224 &ok); 9225 if (!ok) 9226 return FALSE; 9227 9228 /* Mark dynamically referenced symbols. */ 9229 if (elf_hash_table (info)->dynamic_sections_created) 9230 elf_link_hash_traverse (elf_hash_table (info), 9231 bed->gc_mark_dynamic_ref, 9232 info); 9233 9234 /* Grovel through relocs to find out who stays ... */ 9235 gc_mark_hook = bed->gc_mark_hook; 9236 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 9237 { 9238 asection *o; 9239 9240 if (bfd_get_flavour (sub) != bfd_target_elf_flavour) 9241 continue; 9242 9243 for (o = sub->sections; o != NULL; o = o->next) 9244 if ((o->flags & SEC_KEEP) != 0 && !o->gc_mark) 9245 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 9246 return FALSE; 9247 } 9248 9249 /* ... again for sections marked from eh_frame. */ 9250 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 9251 { 9252 asection *o; 9253 9254 if (bfd_get_flavour (sub) != bfd_target_elf_flavour) 9255 continue; 9256 9257 /* Keep .gcc_except_table.* if the associated .text.* is 9258 marked. This isn't very nice, but the proper solution, 9259 splitting .eh_frame up and using comdat doesn't pan out 9260 easily due to needing special relocs to handle the 9261 difference of two symbols in separate sections. 9262 Don't keep code sections referenced by .eh_frame. */ 9263 for (o = sub->sections; o != NULL; o = o->next) 9264 if (!o->gc_mark && o->gc_mark_from_eh && (o->flags & SEC_CODE) == 0) 9265 { 9266 if (strncmp (o->name, ".gcc_except_table.", 18) == 0) 9267 { 9268 unsigned long len; 9269 char *fn_name; 9270 asection *fn_text; 9271 9272 len = strlen (o->name + 18) + 1; 9273 fn_name = bfd_malloc (len + 6); 9274 if (fn_name == NULL) 9275 return FALSE; 9276 memcpy (fn_name, ".text.", 6); 9277 memcpy (fn_name + 6, o->name + 18, len); 9278 fn_text = bfd_get_section_by_name (sub, fn_name); 9279 free (fn_name); 9280 if (fn_text == NULL || !fn_text->gc_mark) 9281 continue; 9282 } 9283 9284 /* If not using specially named exception table section, 9285 then keep whatever we are using. */ 9286 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 9287 return FALSE; 9288 } 9289 } 9290 9291 /* ... and mark SEC_EXCLUDE for those that go. */ 9292 return elf_gc_sweep (abfd, info); 9293 } 9294 9295 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ 9296 9297 bfd_boolean 9298 bfd_elf_gc_record_vtinherit (bfd *abfd, 9299 asection *sec, 9300 struct elf_link_hash_entry *h, 9301 bfd_vma offset) 9302 { 9303 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 9304 struct elf_link_hash_entry **search, *child; 9305 bfd_size_type extsymcount; 9306 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9307 9308 /* The sh_info field of the symtab header tells us where the 9309 external symbols start. We don't care about the local symbols at 9310 this point. */ 9311 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; 9312 if (!elf_bad_symtab (abfd)) 9313 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; 9314 9315 sym_hashes = elf_sym_hashes (abfd); 9316 sym_hashes_end = sym_hashes + extsymcount; 9317 9318 /* Hunt down the child symbol, which is in this section at the same 9319 offset as the relocation. */ 9320 for (search = sym_hashes; search != sym_hashes_end; ++search) 9321 { 9322 if ((child = *search) != NULL 9323 && (child->root.type == bfd_link_hash_defined 9324 || child->root.type == bfd_link_hash_defweak) 9325 && child->root.u.def.section == sec 9326 && child->root.u.def.value == offset) 9327 goto win; 9328 } 9329 9330 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", 9331 abfd, sec, (unsigned long) offset); 9332 bfd_set_error (bfd_error_invalid_operation); 9333 return FALSE; 9334 9335 win: 9336 if (!child->vtable) 9337 { 9338 child->vtable = bfd_zalloc (abfd, sizeof (*child->vtable)); 9339 if (!child->vtable) 9340 return FALSE; 9341 } 9342 if (!h) 9343 { 9344 /* This *should* only be the absolute section. It could potentially 9345 be that someone has defined a non-global vtable though, which 9346 would be bad. It isn't worth paging in the local symbols to be 9347 sure though; that case should simply be handled by the assembler. */ 9348 9349 child->vtable->parent = (struct elf_link_hash_entry *) -1; 9350 } 9351 else 9352 child->vtable->parent = h; 9353 9354 return TRUE; 9355 } 9356 9357 /* Called from check_relocs to record the existence of a VTENTRY reloc. */ 9358 9359 bfd_boolean 9360 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, 9361 asection *sec ATTRIBUTE_UNUSED, 9362 struct elf_link_hash_entry *h, 9363 bfd_vma addend) 9364 { 9365 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9366 unsigned int log_file_align = bed->s->log_file_align; 9367 9368 if (!h->vtable) 9369 { 9370 h->vtable = bfd_zalloc (abfd, sizeof (*h->vtable)); 9371 if (!h->vtable) 9372 return FALSE; 9373 } 9374 9375 if (addend >= h->vtable->size) 9376 { 9377 size_t size, bytes, file_align; 9378 bfd_boolean *ptr = h->vtable->used; 9379 9380 /* While the symbol is undefined, we have to be prepared to handle 9381 a zero size. */ 9382 file_align = 1 << log_file_align; 9383 if (h->root.type == bfd_link_hash_undefined) 9384 size = addend + file_align; 9385 else 9386 { 9387 size = h->size; 9388 if (addend >= size) 9389 { 9390 /* Oops! We've got a reference past the defined end of 9391 the table. This is probably a bug -- shall we warn? */ 9392 size = addend + file_align; 9393 } 9394 } 9395 size = (size + file_align - 1) & -file_align; 9396 9397 /* Allocate one extra entry for use as a "done" flag for the 9398 consolidation pass. */ 9399 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); 9400 9401 if (ptr) 9402 { 9403 ptr = bfd_realloc (ptr - 1, bytes); 9404 9405 if (ptr != NULL) 9406 { 9407 size_t oldbytes; 9408 9409 oldbytes = (((h->vtable->size >> log_file_align) + 1) 9410 * sizeof (bfd_boolean)); 9411 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); 9412 } 9413 } 9414 else 9415 ptr = bfd_zmalloc (bytes); 9416 9417 if (ptr == NULL) 9418 return FALSE; 9419 9420 /* And arrange for that done flag to be at index -1. */ 9421 h->vtable->used = ptr + 1; 9422 h->vtable->size = size; 9423 } 9424 9425 h->vtable->used[addend >> log_file_align] = TRUE; 9426 9427 return TRUE; 9428 } 9429 9430 struct alloc_got_off_arg { 9431 bfd_vma gotoff; 9432 unsigned int got_elt_size; 9433 }; 9434 9435 /* We need a special top-level link routine to convert got reference counts 9436 to real got offsets. */ 9437 9438 static bfd_boolean 9439 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) 9440 { 9441 struct alloc_got_off_arg *gofarg = arg; 9442 9443 if (h->root.type == bfd_link_hash_warning) 9444 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9445 9446 if (h->got.refcount > 0) 9447 { 9448 h->got.offset = gofarg->gotoff; 9449 gofarg->gotoff += gofarg->got_elt_size; 9450 } 9451 else 9452 h->got.offset = (bfd_vma) -1; 9453 9454 return TRUE; 9455 } 9456 9457 /* And an accompanying bit to work out final got entry offsets once 9458 we're done. Should be called from final_link. */ 9459 9460 bfd_boolean 9461 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, 9462 struct bfd_link_info *info) 9463 { 9464 bfd *i; 9465 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9466 bfd_vma gotoff; 9467 unsigned int got_elt_size = bed->s->arch_size / 8; 9468 struct alloc_got_off_arg gofarg; 9469 9470 if (! is_elf_hash_table (info->hash)) 9471 return FALSE; 9472 9473 /* The GOT offset is relative to the .got section, but the GOT header is 9474 put into the .got.plt section, if the backend uses it. */ 9475 if (bed->want_got_plt) 9476 gotoff = 0; 9477 else 9478 gotoff = bed->got_header_size; 9479 9480 /* Do the local .got entries first. */ 9481 for (i = info->input_bfds; i; i = i->link_next) 9482 { 9483 bfd_signed_vma *local_got; 9484 bfd_size_type j, locsymcount; 9485 Elf_Internal_Shdr *symtab_hdr; 9486 9487 if (bfd_get_flavour (i) != bfd_target_elf_flavour) 9488 continue; 9489 9490 local_got = elf_local_got_refcounts (i); 9491 if (!local_got) 9492 continue; 9493 9494 symtab_hdr = &elf_tdata (i)->symtab_hdr; 9495 if (elf_bad_symtab (i)) 9496 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 9497 else 9498 locsymcount = symtab_hdr->sh_info; 9499 9500 for (j = 0; j < locsymcount; ++j) 9501 { 9502 if (local_got[j] > 0) 9503 { 9504 local_got[j] = gotoff; 9505 gotoff += got_elt_size; 9506 } 9507 else 9508 local_got[j] = (bfd_vma) -1; 9509 } 9510 } 9511 9512 /* Then the global .got entries. .plt refcounts are handled by 9513 adjust_dynamic_symbol */ 9514 gofarg.gotoff = gotoff; 9515 gofarg.got_elt_size = got_elt_size; 9516 elf_link_hash_traverse (elf_hash_table (info), 9517 elf_gc_allocate_got_offsets, 9518 &gofarg); 9519 return TRUE; 9520 } 9521 9522 /* Many folk need no more in the way of final link than this, once 9523 got entry reference counting is enabled. */ 9524 9525 bfd_boolean 9526 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) 9527 { 9528 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) 9529 return FALSE; 9530 9531 /* Invoke the regular ELF backend linker to do all the work. */ 9532 return bfd_elf_final_link (abfd, info); 9533 } 9534 9535 bfd_boolean 9536 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) 9537 { 9538 struct elf_reloc_cookie *rcookie = cookie; 9539 9540 if (rcookie->bad_symtab) 9541 rcookie->rel = rcookie->rels; 9542 9543 for (; rcookie->rel < rcookie->relend; rcookie->rel++) 9544 { 9545 unsigned long r_symndx; 9546 9547 if (! rcookie->bad_symtab) 9548 if (rcookie->rel->r_offset > offset) 9549 return FALSE; 9550 if (rcookie->rel->r_offset != offset) 9551 continue; 9552 9553 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; 9554 if (r_symndx == SHN_UNDEF) 9555 return TRUE; 9556 9557 if (r_symndx >= rcookie->locsymcount 9558 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) 9559 { 9560 struct elf_link_hash_entry *h; 9561 9562 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; 9563 9564 while (h->root.type == bfd_link_hash_indirect 9565 || h->root.type == bfd_link_hash_warning) 9566 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9567 9568 if ((h->root.type == bfd_link_hash_defined 9569 || h->root.type == bfd_link_hash_defweak) 9570 && elf_discarded_section (h->root.u.def.section)) 9571 return TRUE; 9572 else 9573 return FALSE; 9574 } 9575 else 9576 { 9577 /* It's not a relocation against a global symbol, 9578 but it could be a relocation against a local 9579 symbol for a discarded section. */ 9580 asection *isec; 9581 Elf_Internal_Sym *isym; 9582 9583 /* Need to: get the symbol; get the section. */ 9584 isym = &rcookie->locsyms[r_symndx]; 9585 if (isym->st_shndx < SHN_LORESERVE || isym->st_shndx > SHN_HIRESERVE) 9586 { 9587 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); 9588 if (isec != NULL && elf_discarded_section (isec)) 9589 return TRUE; 9590 } 9591 } 9592 return FALSE; 9593 } 9594 return FALSE; 9595 } 9596 9597 /* Discard unneeded references to discarded sections. 9598 Returns TRUE if any section's size was changed. */ 9599 /* This function assumes that the relocations are in sorted order, 9600 which is true for all known assemblers. */ 9601 9602 bfd_boolean 9603 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) 9604 { 9605 struct elf_reloc_cookie cookie; 9606 asection *stab, *eh; 9607 Elf_Internal_Shdr *symtab_hdr; 9608 const struct elf_backend_data *bed; 9609 bfd *abfd; 9610 unsigned int count; 9611 bfd_boolean ret = FALSE; 9612 9613 if (info->traditional_format 9614 || !is_elf_hash_table (info->hash)) 9615 return FALSE; 9616 9617 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) 9618 { 9619 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 9620 continue; 9621 9622 bed = get_elf_backend_data (abfd); 9623 9624 if ((abfd->flags & DYNAMIC) != 0) 9625 continue; 9626 9627 eh = bfd_get_section_by_name (abfd, ".eh_frame"); 9628 if (info->relocatable 9629 || (eh != NULL 9630 && (eh->size == 0 9631 || bfd_is_abs_section (eh->output_section)))) 9632 eh = NULL; 9633 9634 stab = bfd_get_section_by_name (abfd, ".stab"); 9635 if (stab != NULL 9636 && (stab->size == 0 9637 || bfd_is_abs_section (stab->output_section) 9638 || stab->sec_info_type != ELF_INFO_TYPE_STABS)) 9639 stab = NULL; 9640 9641 if (stab == NULL 9642 && eh == NULL 9643 && bed->elf_backend_discard_info == NULL) 9644 continue; 9645 9646 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 9647 cookie.abfd = abfd; 9648 cookie.sym_hashes = elf_sym_hashes (abfd); 9649 cookie.bad_symtab = elf_bad_symtab (abfd); 9650 if (cookie.bad_symtab) 9651 { 9652 cookie.locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 9653 cookie.extsymoff = 0; 9654 } 9655 else 9656 { 9657 cookie.locsymcount = symtab_hdr->sh_info; 9658 cookie.extsymoff = symtab_hdr->sh_info; 9659 } 9660 9661 if (bed->s->arch_size == 32) 9662 cookie.r_sym_shift = 8; 9663 else 9664 cookie.r_sym_shift = 32; 9665 9666 cookie.locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; 9667 if (cookie.locsyms == NULL && cookie.locsymcount != 0) 9668 { 9669 cookie.locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, 9670 cookie.locsymcount, 0, 9671 NULL, NULL, NULL); 9672 if (cookie.locsyms == NULL) 9673 return FALSE; 9674 } 9675 9676 if (stab != NULL) 9677 { 9678 cookie.rels = NULL; 9679 count = stab->reloc_count; 9680 if (count != 0) 9681 cookie.rels = _bfd_elf_link_read_relocs (abfd, stab, NULL, NULL, 9682 info->keep_memory); 9683 if (cookie.rels != NULL) 9684 { 9685 cookie.rel = cookie.rels; 9686 cookie.relend = cookie.rels; 9687 cookie.relend += count * bed->s->int_rels_per_ext_rel; 9688 if (_bfd_discard_section_stabs (abfd, stab, 9689 elf_section_data (stab)->sec_info, 9690 bfd_elf_reloc_symbol_deleted_p, 9691 &cookie)) 9692 ret = TRUE; 9693 if (elf_section_data (stab)->relocs != cookie.rels) 9694 free (cookie.rels); 9695 } 9696 } 9697 9698 if (eh != NULL) 9699 { 9700 cookie.rels = NULL; 9701 count = eh->reloc_count; 9702 if (count != 0) 9703 cookie.rels = _bfd_elf_link_read_relocs (abfd, eh, NULL, NULL, 9704 info->keep_memory); 9705 cookie.rel = cookie.rels; 9706 cookie.relend = cookie.rels; 9707 if (cookie.rels != NULL) 9708 cookie.relend += count * bed->s->int_rels_per_ext_rel; 9709 9710 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, 9711 bfd_elf_reloc_symbol_deleted_p, 9712 &cookie)) 9713 ret = TRUE; 9714 9715 if (cookie.rels != NULL 9716 && elf_section_data (eh)->relocs != cookie.rels) 9717 free (cookie.rels); 9718 } 9719 9720 if (bed->elf_backend_discard_info != NULL 9721 && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) 9722 ret = TRUE; 9723 9724 if (cookie.locsyms != NULL 9725 && symtab_hdr->contents != (unsigned char *) cookie.locsyms) 9726 { 9727 if (! info->keep_memory) 9728 free (cookie.locsyms); 9729 else 9730 symtab_hdr->contents = (unsigned char *) cookie.locsyms; 9731 } 9732 } 9733 9734 if (info->eh_frame_hdr 9735 && !info->relocatable 9736 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) 9737 ret = TRUE; 9738 9739 return ret; 9740 } 9741 9742 void 9743 _bfd_elf_section_already_linked (bfd *abfd, struct bfd_section *sec, 9744 struct bfd_link_info *info) 9745 { 9746 flagword flags; 9747 const char *name, *p; 9748 struct bfd_section_already_linked *l; 9749 struct bfd_section_already_linked_hash_entry *already_linked_list; 9750 asection *group; 9751 9752 /* A single member comdat group section may be discarded by a 9753 linkonce section. See below. */ 9754 if (sec->output_section == bfd_abs_section_ptr) 9755 return; 9756 9757 flags = sec->flags; 9758 9759 /* Check if it belongs to a section group. */ 9760 group = elf_sec_group (sec); 9761 9762 /* Return if it isn't a linkonce section nor a member of a group. A 9763 comdat group section also has SEC_LINK_ONCE set. */ 9764 if ((flags & SEC_LINK_ONCE) == 0 && group == NULL) 9765 return; 9766 9767 if (group) 9768 { 9769 /* If this is the member of a single member comdat group, check if 9770 the group should be discarded. */ 9771 if (elf_next_in_group (sec) == sec 9772 && (group->flags & SEC_LINK_ONCE) != 0) 9773 sec = group; 9774 else 9775 return; 9776 } 9777 9778 /* FIXME: When doing a relocatable link, we may have trouble 9779 copying relocations in other sections that refer to local symbols 9780 in the section being discarded. Those relocations will have to 9781 be converted somehow; as of this writing I'm not sure that any of 9782 the backends handle that correctly. 9783 9784 It is tempting to instead not discard link once sections when 9785 doing a relocatable link (technically, they should be discarded 9786 whenever we are building constructors). However, that fails, 9787 because the linker winds up combining all the link once sections 9788 into a single large link once section, which defeats the purpose 9789 of having link once sections in the first place. 9790 9791 Also, not merging link once sections in a relocatable link 9792 causes trouble for MIPS ELF, which relies on link once semantics 9793 to handle the .reginfo section correctly. */ 9794 9795 name = bfd_get_section_name (abfd, sec); 9796 9797 if (strncmp (name, ".gnu.linkonce.", sizeof (".gnu.linkonce.") - 1) == 0 9798 && (p = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 9799 p++; 9800 else 9801 p = name; 9802 9803 already_linked_list = bfd_section_already_linked_table_lookup (p); 9804 9805 for (l = already_linked_list->entry; l != NULL; l = l->next) 9806 { 9807 /* We may have 3 different sections on the list: group section, 9808 comdat section and linkonce section. SEC may be a linkonce or 9809 group section. We match a group section with a group section, 9810 a linkonce section with a linkonce section, and ignore comdat 9811 section. */ 9812 if ((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) 9813 && strcmp (name, l->sec->name) == 0 9814 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL) 9815 { 9816 /* The section has already been linked. See if we should 9817 issue a warning. */ 9818 switch (flags & SEC_LINK_DUPLICATES) 9819 { 9820 default: 9821 abort (); 9822 9823 case SEC_LINK_DUPLICATES_DISCARD: 9824 break; 9825 9826 case SEC_LINK_DUPLICATES_ONE_ONLY: 9827 (*_bfd_error_handler) 9828 (_("%B: ignoring duplicate section `%A'"), 9829 abfd, sec); 9830 break; 9831 9832 case SEC_LINK_DUPLICATES_SAME_SIZE: 9833 if (sec->size != l->sec->size) 9834 (*_bfd_error_handler) 9835 (_("%B: duplicate section `%A' has different size"), 9836 abfd, sec); 9837 break; 9838 9839 case SEC_LINK_DUPLICATES_SAME_CONTENTS: 9840 if (sec->size != l->sec->size) 9841 (*_bfd_error_handler) 9842 (_("%B: duplicate section `%A' has different size"), 9843 abfd, sec); 9844 else if (sec->size != 0) 9845 { 9846 bfd_byte *sec_contents = NULL, *l_sec_contents = NULL; 9847 9848 if (!bfd_malloc_and_get_section (abfd, sec, &sec_contents)) 9849 (*_bfd_error_handler) 9850 (_("%B: warning: could not read contents of section `%A'"), 9851 abfd, sec); 9852 else if (!bfd_malloc_and_get_section (l->sec->owner, l->sec, 9853 &l_sec_contents)) 9854 (*_bfd_error_handler) 9855 (_("%B: warning: could not read contents of section `%A'"), 9856 l->sec->owner, l->sec); 9857 else if (memcmp (sec_contents, l_sec_contents, sec->size) != 0) 9858 (*_bfd_error_handler) 9859 (_("%B: warning: duplicate section `%A' has different contents"), 9860 abfd, sec); 9861 9862 if (sec_contents) 9863 free (sec_contents); 9864 if (l_sec_contents) 9865 free (l_sec_contents); 9866 } 9867 break; 9868 } 9869 9870 /* Set the output_section field so that lang_add_section 9871 does not create a lang_input_section structure for this 9872 section. Since there might be a symbol in the section 9873 being discarded, we must retain a pointer to the section 9874 which we are really going to use. */ 9875 sec->output_section = bfd_abs_section_ptr; 9876 sec->kept_section = l->sec; 9877 9878 if (flags & SEC_GROUP) 9879 { 9880 asection *first = elf_next_in_group (sec); 9881 asection *s = first; 9882 9883 while (s != NULL) 9884 { 9885 s->output_section = bfd_abs_section_ptr; 9886 /* Record which group discards it. */ 9887 s->kept_section = l->sec; 9888 s = elf_next_in_group (s); 9889 /* These lists are circular. */ 9890 if (s == first) 9891 break; 9892 } 9893 } 9894 9895 return; 9896 } 9897 } 9898 9899 if (group) 9900 { 9901 /* If this is the member of a single member comdat group and the 9902 group hasn't be discarded, we check if it matches a linkonce 9903 section. We only record the discarded comdat group. Otherwise 9904 the undiscarded group will be discarded incorrectly later since 9905 itself has been recorded. */ 9906 for (l = already_linked_list->entry; l != NULL; l = l->next) 9907 if ((l->sec->flags & SEC_GROUP) == 0 9908 && bfd_coff_get_comdat_section (l->sec->owner, l->sec) == NULL 9909 && bfd_elf_match_symbols_in_sections (l->sec, 9910 elf_next_in_group (sec), 9911 info)) 9912 { 9913 elf_next_in_group (sec)->output_section = bfd_abs_section_ptr; 9914 elf_next_in_group (sec)->kept_section = l->sec; 9915 group->output_section = bfd_abs_section_ptr; 9916 break; 9917 } 9918 if (l == NULL) 9919 return; 9920 } 9921 else 9922 /* There is no direct match. But for linkonce section, we should 9923 check if there is a match with comdat group member. We always 9924 record the linkonce section, discarded or not. */ 9925 for (l = already_linked_list->entry; l != NULL; l = l->next) 9926 if (l->sec->flags & SEC_GROUP) 9927 { 9928 asection *first = elf_next_in_group (l->sec); 9929 9930 if (first != NULL 9931 && elf_next_in_group (first) == first 9932 && bfd_elf_match_symbols_in_sections (first, sec, info)) 9933 { 9934 sec->output_section = bfd_abs_section_ptr; 9935 sec->kept_section = l->sec; 9936 break; 9937 } 9938 } 9939 9940 /* This is the first section with this name. Record it. */ 9941 bfd_section_already_linked_table_insert (already_linked_list, sec); 9942 } 9943 9944 bfd_boolean 9945 _bfd_elf_common_definition (Elf_Internal_Sym *sym) 9946 { 9947 return sym->st_shndx == SHN_COMMON; 9948 } 9949 9950 unsigned int 9951 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) 9952 { 9953 return SHN_COMMON; 9954 } 9955 9956 asection * 9957 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) 9958 { 9959 return bfd_com_section_ptr; 9960 } 9961