1 /* ELF linking support for BFD. 2 Copyright (C) 1995-2025 Free Software Foundation, Inc. 3 4 This file is part of BFD, the Binary File Descriptor library. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21 #include "sysdep.h" 22 #include "bfd.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 #if BFD_SUPPORTS_PLUGINS 31 #include "plugin-api.h" 32 #include "plugin.h" 33 #endif 34 35 #include <limits.h> 36 #ifndef CHAR_BIT 37 #define CHAR_BIT 8 38 #endif 39 40 /* This struct is used to pass information to routines called via 41 elf_link_hash_traverse which must return failure. */ 42 43 struct elf_info_failed 44 { 45 struct bfd_link_info *info; 46 bool failed; 47 }; 48 49 static bool _bfd_elf_fix_symbol_flags 50 (struct elf_link_hash_entry *, struct elf_info_failed *); 51 52 /* Return false if linker should avoid caching relocation information 53 and symbol tables of input files in memory. */ 54 55 static bool 56 _bfd_elf_link_keep_memory (struct bfd_link_info *info) 57 { 58 #ifdef USE_MMAP 59 /* Don't cache symbol nor relocation tables if they are mapped in. 60 NB: Since the --no-keep-memory linker option causes: 61 62 https://sourceware.org/bugzilla/show_bug.cgi?id=31458 63 64 this is opt-in by each backend. */ 65 const struct elf_backend_data *bed 66 = get_elf_backend_data (info->output_bfd); 67 if (bed != NULL && bed->use_mmap) 68 return false; 69 #endif 70 bfd *abfd; 71 bfd_size_type size; 72 73 if (!info->keep_memory) 74 return false; 75 76 if (info->max_cache_size == (bfd_size_type) -1) 77 return true; 78 79 abfd = info->input_bfds; 80 size = info->cache_size; 81 do 82 { 83 if (size >= info->max_cache_size) 84 { 85 /* Over the limit. Reduce the memory usage. */ 86 info->keep_memory = false; 87 return false; 88 } 89 if (!abfd) 90 break; 91 size += abfd->alloc_size; 92 abfd = abfd->link.next; 93 } 94 while (1); 95 96 return true; 97 } 98 99 static struct elf_link_hash_entry * 100 get_link_hash_entry (struct elf_link_hash_entry ** sym_hashes, 101 unsigned int symndx, 102 unsigned int ext_sym_start) 103 { 104 if (sym_hashes == NULL 105 /* Guard against corrupt input. See PR 32636 for an example. */ 106 || symndx < ext_sym_start) 107 return NULL; 108 109 struct elf_link_hash_entry *h = sym_hashes[symndx - ext_sym_start]; 110 111 /* The hash might be empty. See PR 32641 for an example of this. */ 112 if (h == NULL) 113 return NULL; 114 115 while (h->root.type == bfd_link_hash_indirect 116 || h->root.type == bfd_link_hash_warning) 117 h = (struct elf_link_hash_entry *) h->root.u.i.link; 118 119 return h; 120 } 121 122 struct elf_link_hash_entry * 123 _bfd_elf_get_link_hash_entry (struct elf_link_hash_entry ** sym_hashes, 124 unsigned int symndx, 125 Elf_Internal_Shdr * symtab_hdr) 126 { 127 if (symtab_hdr == NULL) 128 return NULL; 129 130 return get_link_hash_entry (sym_hashes, symndx, symtab_hdr->sh_info); 131 } 132 133 static struct elf_link_hash_entry * 134 get_ext_sym_hash_from_cookie (struct elf_reloc_cookie *cookie, unsigned long r_symndx) 135 { 136 if (cookie == NULL || cookie->sym_hashes == NULL) 137 return NULL; 138 139 if (r_symndx >= cookie->locsymcount 140 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 141 return get_link_hash_entry (cookie->sym_hashes, r_symndx, cookie->extsymoff); 142 143 return NULL; 144 } 145 146 asection * 147 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, 148 unsigned long r_symndx, 149 bool discard) 150 { 151 struct elf_link_hash_entry *h; 152 153 h = get_ext_sym_hash_from_cookie (cookie, r_symndx); 154 155 if (h != NULL) 156 { 157 if ((h->root.type == bfd_link_hash_defined 158 || h->root.type == bfd_link_hash_defweak) 159 && discarded_section (h->root.u.def.section)) 160 return h->root.u.def.section; 161 else 162 return NULL; 163 } 164 165 /* It's not a relocation against a global symbol, 166 but it could be a relocation against a local 167 symbol for a discarded section. */ 168 asection *isec; 169 Elf_Internal_Sym *isym; 170 171 /* Need to: get the symbol; get the section. */ 172 isym = &cookie->locsyms[r_symndx]; 173 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); 174 if (isec != NULL 175 && discard ? discarded_section (isec) : 1) 176 return isec; 177 178 return NULL; 179 } 180 181 /* Define a symbol in a dynamic linkage section. */ 182 183 struct elf_link_hash_entry * 184 _bfd_elf_define_linkage_sym (bfd *abfd, 185 struct bfd_link_info *info, 186 asection *sec, 187 const char *name) 188 { 189 struct elf_link_hash_entry *h; 190 struct bfd_link_hash_entry *bh; 191 const struct elf_backend_data *bed; 192 193 h = elf_link_hash_lookup (elf_hash_table (info), name, false, false, false); 194 if (h != NULL) 195 { 196 /* Zap symbol defined in an as-needed lib that wasn't linked. 197 This is a symptom of a larger problem: Absolute symbols 198 defined in shared libraries can't be overridden, because we 199 lose the link to the bfd which is via the symbol section. */ 200 h->root.type = bfd_link_hash_new; 201 bh = &h->root; 202 } 203 else 204 bh = NULL; 205 206 bed = get_elf_backend_data (abfd); 207 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, 208 sec, 0, NULL, false, bed->collect, 209 &bh)) 210 return NULL; 211 h = (struct elf_link_hash_entry *) bh; 212 BFD_ASSERT (h != NULL); 213 h->def_regular = 1; 214 h->non_elf = 0; 215 h->root.linker_def = 1; 216 h->type = STT_OBJECT; 217 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 218 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 219 220 (*bed->elf_backend_hide_symbol) (info, h, true); 221 return h; 222 } 223 224 bool 225 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) 226 { 227 flagword flags; 228 asection *s; 229 struct elf_link_hash_entry *h; 230 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 231 struct elf_link_hash_table *htab = elf_hash_table (info); 232 233 /* This function may be called more than once. */ 234 if (htab->sgot != NULL) 235 return true; 236 237 flags = bed->dynamic_sec_flags; 238 239 s = bfd_make_section_anyway_with_flags (abfd, 240 (bed->rela_plts_and_copies_p 241 ? ".rela.got" : ".rel.got"), 242 flags | SEC_READONLY); 243 if (s == NULL 244 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 245 return false; 246 htab->srelgot = s; 247 248 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 249 if (s == NULL 250 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 251 return false; 252 htab->sgot = s; 253 254 if (bed->want_got_plt) 255 { 256 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); 257 if (s == NULL 258 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 259 return false; 260 htab->sgotplt = s; 261 } 262 263 /* The first bit of the global offset table is the header. */ 264 s->size += bed->got_header_size; 265 266 if (bed->want_got_sym) 267 { 268 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 269 (or .got.plt) section. We don't do this in the linker script 270 because we don't want to define the symbol if we are not creating 271 a global offset table. */ 272 h = _bfd_elf_define_linkage_sym (abfd, info, s, 273 "_GLOBAL_OFFSET_TABLE_"); 274 elf_hash_table (info)->hgot = h; 275 if (h == NULL) 276 return false; 277 } 278 279 return true; 280 } 281 282 /* Create a strtab to hold the dynamic symbol names. */ 284 static bool 285 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) 286 { 287 struct elf_link_hash_table *hash_table; 288 289 hash_table = elf_hash_table (info); 290 if (hash_table->dynobj == NULL) 291 { 292 /* We may not set dynobj, an input file holding linker created 293 dynamic sections to abfd, which may be a dynamic object with 294 its own dynamic sections. We need to find a normal input file 295 to hold linker created sections if possible. */ 296 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) 297 { 298 bfd *ibfd; 299 asection *s; 300 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) 301 if ((ibfd->flags 302 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 303 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour 304 && elf_object_id (ibfd) == elf_hash_table_id (hash_table) 305 && !((s = ibfd->sections) != NULL 306 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) 307 { 308 abfd = ibfd; 309 break; 310 } 311 } 312 hash_table->dynobj = abfd; 313 } 314 315 if (hash_table->dynstr == NULL) 316 { 317 hash_table->dynstr = _bfd_elf_strtab_init (); 318 if (hash_table->dynstr == NULL) 319 return false; 320 } 321 return true; 322 } 323 324 /* Create some sections which will be filled in with dynamic linking 325 information. ABFD is an input file which requires dynamic sections 326 to be created. The dynamic sections take up virtual memory space 327 when the final executable is run, so we need to create them before 328 addresses are assigned to the output sections. We work out the 329 actual contents and size of these sections later. */ 330 331 bool 332 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 333 { 334 flagword flags; 335 asection *s; 336 const struct elf_backend_data *bed; 337 struct elf_link_hash_entry *h; 338 339 if (! is_elf_hash_table (info->hash)) 340 return false; 341 342 if (elf_hash_table (info)->dynamic_sections_created) 343 return true; 344 345 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 346 return false; 347 348 abfd = elf_hash_table (info)->dynobj; 349 bed = get_elf_backend_data (abfd); 350 351 flags = bed->dynamic_sec_flags; 352 353 /* A dynamically linked executable has a .interp section, but a 354 shared library does not. */ 355 if (bfd_link_executable (info) && !info->nointerp) 356 { 357 s = bfd_make_section_anyway_with_flags (abfd, ".interp", 358 flags | SEC_READONLY); 359 if (s == NULL) 360 return false; 361 } 362 363 /* Create sections to hold version informations. These are removed 364 if they are not needed. */ 365 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", 366 flags | SEC_READONLY); 367 if (s == NULL 368 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 369 return false; 370 371 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", 372 flags | SEC_READONLY); 373 if (s == NULL 374 || !bfd_set_section_alignment (s, 1)) 375 return false; 376 377 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", 378 flags | SEC_READONLY); 379 if (s == NULL 380 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 381 return false; 382 383 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", 384 flags | SEC_READONLY); 385 if (s == NULL 386 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 387 return false; 388 elf_hash_table (info)->dynsym = s; 389 390 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", 391 flags | SEC_READONLY); 392 if (s == NULL) 393 return false; 394 395 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); 396 if (s == NULL 397 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 398 return false; 399 elf_hash_table (info)->dynamic = s; 400 401 /* The special symbol _DYNAMIC is always set to the start of the 402 .dynamic section. We could set _DYNAMIC in a linker script, but we 403 only want to define it if we are, in fact, creating a .dynamic 404 section. We don't want to define it if there is no .dynamic 405 section, since on some ELF platforms the start up code examines it 406 to decide how to initialize the process. */ 407 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); 408 elf_hash_table (info)->hdynamic = h; 409 if (h == NULL) 410 return false; 411 412 if (info->emit_hash) 413 { 414 s = bfd_make_section_anyway_with_flags (abfd, ".hash", 415 flags | SEC_READONLY); 416 if (s == NULL 417 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 418 return false; 419 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; 420 } 421 422 if (info->emit_gnu_hash && bed->record_xhash_symbol == NULL) 423 { 424 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", 425 flags | SEC_READONLY); 426 if (s == NULL 427 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 428 return false; 429 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: 430 4 32-bit words followed by variable count of 64-bit words, then 431 variable count of 32-bit words. */ 432 if (bed->s->arch_size == 64) 433 elf_section_data (s)->this_hdr.sh_entsize = 0; 434 else 435 elf_section_data (s)->this_hdr.sh_entsize = 4; 436 } 437 438 if (info->enable_dt_relr) 439 { 440 s = bfd_make_section_anyway_with_flags (abfd, ".relr.dyn", 441 flags | SEC_READONLY); 442 if (s == NULL 443 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 444 return false; 445 elf_hash_table (info)->srelrdyn = s; 446 } 447 448 /* Let the backend create the rest of the sections. This lets the 449 backend set the right flags. The backend will normally create 450 the .got and .plt sections. */ 451 if (bed->elf_backend_create_dynamic_sections == NULL 452 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) 453 return false; 454 455 elf_hash_table (info)->dynamic_sections_created = true; 456 457 return true; 458 } 459 460 /* Create dynamic sections when linking against a dynamic object. */ 461 462 bool 463 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 464 { 465 flagword flags, pltflags; 466 struct elf_link_hash_entry *h; 467 asection *s; 468 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 469 struct elf_link_hash_table *htab = elf_hash_table (info); 470 471 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 472 .rel[a].bss sections. */ 473 flags = bed->dynamic_sec_flags; 474 475 pltflags = flags; 476 if (bed->plt_not_loaded) 477 /* We do not clear SEC_ALLOC here because we still want the OS to 478 allocate space for the section; it's just that there's nothing 479 to read in from the object file. */ 480 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); 481 else 482 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; 483 if (bed->plt_readonly) 484 pltflags |= SEC_READONLY; 485 486 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); 487 if (s == NULL 488 || !bfd_set_section_alignment (s, bed->plt_alignment)) 489 return false; 490 htab->splt = s; 491 492 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 493 .plt section. */ 494 if (bed->want_plt_sym) 495 { 496 h = _bfd_elf_define_linkage_sym (abfd, info, s, 497 "_PROCEDURE_LINKAGE_TABLE_"); 498 elf_hash_table (info)->hplt = h; 499 if (h == NULL) 500 return false; 501 } 502 503 s = bfd_make_section_anyway_with_flags (abfd, 504 (bed->rela_plts_and_copies_p 505 ? ".rela.plt" : ".rel.plt"), 506 flags | SEC_READONLY); 507 if (s == NULL 508 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 509 return false; 510 htab->srelplt = s; 511 512 if (! _bfd_elf_create_got_section (abfd, info)) 513 return false; 514 515 if (bed->want_dynbss) 516 { 517 /* The .dynbss section is a place to put symbols which are defined 518 by dynamic objects, are referenced by regular objects, and are 519 not functions. We must allocate space for them in the process 520 image and use a R_*_COPY reloc to tell the dynamic linker to 521 initialize them at run time. The linker script puts the .dynbss 522 section into the .bss section of the final image. */ 523 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", 524 SEC_ALLOC | SEC_LINKER_CREATED); 525 if (s == NULL) 526 return false; 527 htab->sdynbss = s; 528 529 if (bed->want_dynrelro) 530 { 531 /* Similarly, but for symbols that were originally in read-only 532 sections. This section doesn't really need to have contents, 533 but make it like other .data.rel.ro sections. */ 534 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", 535 flags); 536 if (s == NULL) 537 return false; 538 htab->sdynrelro = s; 539 } 540 541 /* The .rel[a].bss section holds copy relocs. This section is not 542 normally needed. We need to create it here, though, so that the 543 linker will map it to an output section. We can't just create it 544 only if we need it, because we will not know whether we need it 545 until we have seen all the input files, and the first time the 546 main linker code calls BFD after examining all the input files 547 (size_dynamic_sections) the input sections have already been 548 mapped to the output sections. If the section turns out not to 549 be needed, we can discard it later. We will never need this 550 section when generating a shared object, since they do not use 551 copy relocs. */ 552 if (bfd_link_executable (info)) 553 { 554 s = bfd_make_section_anyway_with_flags (abfd, 555 (bed->rela_plts_and_copies_p 556 ? ".rela.bss" : ".rel.bss"), 557 flags | SEC_READONLY); 558 if (s == NULL 559 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 560 return false; 561 htab->srelbss = s; 562 563 if (bed->want_dynrelro) 564 { 565 s = (bfd_make_section_anyway_with_flags 566 (abfd, (bed->rela_plts_and_copies_p 567 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), 568 flags | SEC_READONLY)); 569 if (s == NULL 570 || !bfd_set_section_alignment (s, bed->s->log_file_align)) 571 return false; 572 htab->sreldynrelro = s; 573 } 574 } 575 } 576 577 return true; 578 } 579 580 /* Record a new dynamic symbol. We record the dynamic symbols as we 582 read the input files, since we need to have a list of all of them 583 before we can determine the final sizes of the output sections. 584 Note that we may actually call this function even though we are not 585 going to output any dynamic symbols; in some cases we know that a 586 symbol should be in the dynamic symbol table, but only if there is 587 one. */ 588 589 bool 590 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, 591 struct elf_link_hash_entry *h) 592 { 593 if (h->dynindx == -1) 594 { 595 struct elf_strtab_hash *dynstr; 596 char *p; 597 const char *name; 598 size_t indx; 599 600 if (h->root.type == bfd_link_hash_defined 601 || h->root.type == bfd_link_hash_defweak) 602 { 603 /* An IR symbol should not be made dynamic. */ 604 if (h->root.u.def.section != NULL 605 && h->root.u.def.section->owner != NULL 606 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0) 607 return true; 608 } 609 610 /* XXX: The ABI draft says the linker must turn hidden and 611 internal symbols into STB_LOCAL symbols when producing the 612 DSO. However, if ld.so honors st_other in the dynamic table, 613 this would not be necessary. */ 614 switch (ELF_ST_VISIBILITY (h->other)) 615 { 616 case STV_INTERNAL: 617 case STV_HIDDEN: 618 if (h->root.type != bfd_link_hash_undefined 619 && h->root.type != bfd_link_hash_undefweak) 620 { 621 h->forced_local = 1; 622 return true; 623 } 624 625 default: 626 break; 627 } 628 629 h->dynindx = elf_hash_table (info)->dynsymcount; 630 ++elf_hash_table (info)->dynsymcount; 631 632 dynstr = elf_hash_table (info)->dynstr; 633 if (dynstr == NULL) 634 { 635 /* Create a strtab to hold the dynamic symbol names. */ 636 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 637 if (dynstr == NULL) 638 return false; 639 } 640 641 char *unversioned_name = NULL; 642 643 /* We don't put any version information in the dynamic string 644 table. */ 645 name = h->root.root.string; 646 p = strchr (name, ELF_VER_CHR); 647 if (p != NULL) 648 { 649 unversioned_name = bfd_malloc (p - name + 1); 650 memcpy (unversioned_name, name, p - name); 651 unversioned_name[p - name] = 0; 652 name = unversioned_name; 653 } 654 655 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); 656 657 if (p != NULL) 658 free (unversioned_name); 659 660 if (indx == (size_t) -1) 661 return false; 662 h->dynstr_index = indx; 663 } 664 665 return true; 666 } 667 668 /* Mark a symbol dynamic. */ 670 671 static void 672 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, 673 struct elf_link_hash_entry *h, 674 Elf_Internal_Sym *sym) 675 { 676 struct bfd_elf_dynamic_list *d = info->dynamic_list; 677 678 /* It may be called more than once on the same H. */ 679 if(h->dynamic || bfd_link_relocatable (info)) 680 return; 681 682 if ((info->dynamic_data 683 && (h->type == STT_OBJECT 684 || h->type == STT_COMMON 685 || (sym != NULL 686 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT 687 || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) 688 || (d != NULL 689 && h->non_elf 690 && (*d->match) (&d->head, NULL, h->root.root.string))) 691 { 692 h->dynamic = 1; 693 /* NB: If a symbol is made dynamic by --dynamic-list, it has 694 non-IR reference. */ 695 h->root.non_ir_ref_dynamic = 1; 696 } 697 } 698 699 /* Record an assignment to a symbol made by a linker script. We need 700 this in case some dynamic object refers to this symbol. */ 701 702 bool 703 bfd_elf_record_link_assignment (bfd *output_bfd, 704 struct bfd_link_info *info, 705 const char *name, 706 bool provide, 707 bool hidden) 708 { 709 struct elf_link_hash_entry *h, *hv; 710 struct elf_link_hash_table *htab; 711 const struct elf_backend_data *bed; 712 713 if (!is_elf_hash_table (info->hash)) 714 return true; 715 716 htab = elf_hash_table (info); 717 h = elf_link_hash_lookup (htab, name, !provide, true, false); 718 if (h == NULL) 719 return provide; 720 721 if (h->root.type == bfd_link_hash_warning) 722 h = (struct elf_link_hash_entry *) h->root.u.i.link; 723 724 if (h->versioned == unknown) 725 { 726 /* Set versioned if symbol version is unknown. */ 727 char *version = strrchr (name, ELF_VER_CHR); 728 if (version) 729 { 730 if (version > name && version[-1] != ELF_VER_CHR) 731 h->versioned = versioned_hidden; 732 else 733 h->versioned = versioned; 734 } 735 } 736 737 /* Symbols defined in a linker script but not referenced anywhere 738 else will have non_elf set. */ 739 if (h->non_elf) 740 { 741 bfd_elf_link_mark_dynamic_symbol (info, h, NULL); 742 h->non_elf = 0; 743 } 744 745 switch (h->root.type) 746 { 747 case bfd_link_hash_defined: 748 case bfd_link_hash_defweak: 749 case bfd_link_hash_common: 750 break; 751 case bfd_link_hash_undefweak: 752 case bfd_link_hash_undefined: 753 /* Since we're defining the symbol, don't let it seem to have not 754 been defined. record_dynamic_symbol and size_dynamic_sections 755 may depend on this. */ 756 h->root.type = bfd_link_hash_new; 757 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) 758 bfd_link_repair_undef_list (&htab->root); 759 break; 760 case bfd_link_hash_new: 761 break; 762 case bfd_link_hash_indirect: 763 /* We had a versioned symbol in a dynamic library. We make the 764 the versioned symbol point to this one. */ 765 bed = get_elf_backend_data (output_bfd); 766 hv = h; 767 while (hv->root.type == bfd_link_hash_indirect 768 || hv->root.type == bfd_link_hash_warning) 769 hv = (struct elf_link_hash_entry *) hv->root.u.i.link; 770 /* We don't need to update h->root.u since linker will set them 771 later. */ 772 h->root.type = bfd_link_hash_undefined; 773 hv->root.type = bfd_link_hash_indirect; 774 hv->root.u.i.link = (struct bfd_link_hash_entry *) h; 775 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); 776 break; 777 default: 778 BFD_FAIL (); 779 return false; 780 } 781 782 /* If this symbol is being provided by the linker script, and it is 783 currently defined by a dynamic object, but not by a regular 784 object, then mark it as undefined so that the generic linker will 785 force the correct value. */ 786 if (provide 787 && h->def_dynamic 788 && !h->def_regular) 789 h->root.type = bfd_link_hash_undefined; 790 791 /* If this symbol is currently defined by a dynamic object, but not 792 by a regular object, then clear out any version information because 793 the symbol will not be associated with the dynamic object any 794 more. */ 795 if (h->def_dynamic && !h->def_regular) 796 h->verinfo.verdef = NULL; 797 798 /* Make sure this symbol is not garbage collected. */ 799 h->mark = 1; 800 801 h->def_regular = 1; 802 803 if (hidden) 804 { 805 bed = get_elf_backend_data (output_bfd); 806 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 807 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 808 (*bed->elf_backend_hide_symbol) (info, h, true); 809 } 810 811 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects 812 and executables. */ 813 if (!bfd_link_relocatable (info) 814 && h->dynindx != -1 815 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 816 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) 817 h->forced_local = 1; 818 819 if ((h->def_dynamic 820 || h->ref_dynamic 821 || bfd_link_dll (info)) 822 && !h->forced_local 823 && h->dynindx == -1) 824 { 825 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 826 return false; 827 828 /* If this is a weak defined symbol, and we know a corresponding 829 real symbol from the same dynamic object, make sure the real 830 symbol is also made into a dynamic symbol. */ 831 if (h->is_weakalias) 832 { 833 struct elf_link_hash_entry *def = weakdef (h); 834 835 if (def->dynindx == -1 836 && !bfd_elf_link_record_dynamic_symbol (info, def)) 837 return false; 838 } 839 } 840 841 return true; 842 } 843 844 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on 845 success, and 2 on a failure caused by attempting to record a symbol 846 in a discarded section, eg. a discarded link-once section symbol. */ 847 848 int 849 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, 850 bfd *input_bfd, 851 long input_indx) 852 { 853 size_t amt; 854 struct elf_link_local_dynamic_entry *entry; 855 struct elf_link_hash_table *eht; 856 struct elf_strtab_hash *dynstr; 857 size_t dynstr_index; 858 char *name; 859 Elf_External_Sym_Shndx eshndx; 860 char esym[sizeof (Elf64_External_Sym)]; 861 862 if (! is_elf_hash_table (info->hash)) 863 return 0; 864 865 /* See if the entry exists already. */ 866 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) 867 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) 868 return 1; 869 870 amt = sizeof (*entry); 871 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); 872 if (entry == NULL) 873 return 0; 874 875 /* Go find the symbol, so that we can find it's name. */ 876 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, 877 1, input_indx, &entry->isym, esym, &eshndx)) 878 { 879 bfd_release (input_bfd, entry); 880 return 0; 881 } 882 883 if (entry->isym.st_shndx != SHN_UNDEF 884 && entry->isym.st_shndx < SHN_LORESERVE) 885 { 886 asection *s; 887 888 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); 889 if (s == NULL || bfd_is_abs_section (s->output_section)) 890 { 891 /* We can still bfd_release here as nothing has done another 892 bfd_alloc. We can't do this later in this function. */ 893 bfd_release (input_bfd, entry); 894 return 2; 895 } 896 } 897 898 name = (bfd_elf_string_from_elf_section 899 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, 900 entry->isym.st_name)); 901 902 dynstr = elf_hash_table (info)->dynstr; 903 if (dynstr == NULL) 904 { 905 /* Create a strtab to hold the dynamic symbol names. */ 906 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 907 if (dynstr == NULL) 908 return 0; 909 } 910 911 dynstr_index = _bfd_elf_strtab_add (dynstr, name, false); 912 if (dynstr_index == (size_t) -1) 913 return 0; 914 entry->isym.st_name = dynstr_index; 915 916 eht = elf_hash_table (info); 917 918 entry->next = eht->dynlocal; 919 eht->dynlocal = entry; 920 entry->input_bfd = input_bfd; 921 entry->input_indx = input_indx; 922 eht->dynsymcount++; 923 924 /* Whatever binding the symbol had before, it's now local. */ 925 entry->isym.st_info 926 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); 927 928 /* The dynindx will be set at the end of size_dynamic_sections. */ 929 930 return 1; 931 } 932 933 /* Return the dynindex of a local dynamic symbol. */ 934 935 long 936 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, 937 bfd *input_bfd, 938 long input_indx) 939 { 940 struct elf_link_local_dynamic_entry *e; 941 942 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 943 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 944 return e->dynindx; 945 return -1; 946 } 947 948 /* This function is used to renumber the dynamic symbols, if some of 949 them are removed because they are marked as local. This is called 950 via elf_link_hash_traverse. */ 951 952 static bool 953 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, 954 void *data) 955 { 956 size_t *count = (size_t *) data; 957 958 if (h->forced_local) 959 return true; 960 961 if (h->dynindx != -1) 962 h->dynindx = ++(*count); 963 964 return true; 965 } 966 967 968 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with 969 STB_LOCAL binding. */ 970 971 static bool 972 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, 973 void *data) 974 { 975 size_t *count = (size_t *) data; 976 977 if (!h->forced_local) 978 return true; 979 980 if (h->dynindx != -1) 981 h->dynindx = ++(*count); 982 983 return true; 984 } 985 986 /* Return true if the dynamic symbol for a given section should be 987 omitted when creating a shared library. */ 988 bool 989 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED, 990 struct bfd_link_info *info, 991 asection *p) 992 { 993 struct elf_link_hash_table *htab; 994 asection *ip; 995 996 switch (elf_section_data (p)->this_hdr.sh_type) 997 { 998 case SHT_PROGBITS: 999 case SHT_NOBITS: 1000 /* If sh_type is yet undecided, assume it could be 1001 SHT_PROGBITS/SHT_NOBITS. */ 1002 case SHT_NULL: 1003 htab = elf_hash_table (info); 1004 if (htab->text_index_section != NULL) 1005 return p != htab->text_index_section && p != htab->data_index_section; 1006 1007 return (htab->dynobj != NULL 1008 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL 1009 && ip->output_section == p); 1010 1011 /* There shouldn't be section relative relocations 1012 against any other section. */ 1013 default: 1014 return true; 1015 } 1016 } 1017 1018 bool 1019 _bfd_elf_omit_section_dynsym_all 1020 (bfd *output_bfd ATTRIBUTE_UNUSED, 1021 struct bfd_link_info *info ATTRIBUTE_UNUSED, 1022 asection *p ATTRIBUTE_UNUSED) 1023 { 1024 return true; 1025 } 1026 1027 /* Assign dynsym indices. In a shared library we generate a section 1028 symbol for each output section, which come first. Next come symbols 1029 which have been forced to local binding. Then all of the back-end 1030 allocated local dynamic syms, followed by the rest of the global 1031 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. 1032 (This prevents the early call before elf_backend_init_index_section 1033 and strip_excluded_output_sections setting dynindx for sections 1034 that are stripped.) */ 1035 1036 static unsigned long 1037 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, 1038 struct bfd_link_info *info, 1039 unsigned long *section_sym_count) 1040 { 1041 unsigned long dynsymcount = 0; 1042 bool do_sec = section_sym_count != NULL; 1043 1044 if (bfd_link_pic (info) 1045 || elf_hash_table (info)->is_relocatable_executable) 1046 { 1047 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 1048 asection *p; 1049 for (p = output_bfd->sections; p ; p = p->next) 1050 if ((p->flags & SEC_EXCLUDE) == 0 1051 && (p->flags & SEC_ALLOC) != 0 1052 && elf_hash_table (info)->dynamic_relocs 1053 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) 1054 { 1055 ++dynsymcount; 1056 if (do_sec) 1057 elf_section_data (p)->dynindx = dynsymcount; 1058 } 1059 else if (do_sec) 1060 elf_section_data (p)->dynindx = 0; 1061 } 1062 if (do_sec) 1063 *section_sym_count = dynsymcount; 1064 1065 elf_link_hash_traverse (elf_hash_table (info), 1066 elf_link_renumber_local_hash_table_dynsyms, 1067 &dynsymcount); 1068 1069 if (elf_hash_table (info)->dynlocal) 1070 { 1071 struct elf_link_local_dynamic_entry *p; 1072 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 1073 p->dynindx = ++dynsymcount; 1074 } 1075 elf_hash_table (info)->local_dynsymcount = dynsymcount; 1076 1077 elf_link_hash_traverse (elf_hash_table (info), 1078 elf_link_renumber_hash_table_dynsyms, 1079 &dynsymcount); 1080 1081 /* There is an unused NULL entry at the head of the table which we 1082 must account for in our count even if the table is empty since it 1083 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in 1084 .dynamic section. */ 1085 dynsymcount++; 1086 1087 elf_hash_table (info)->dynsymcount = dynsymcount; 1088 return dynsymcount; 1089 } 1090 1091 /* Merge st_other field. */ 1092 1093 static void 1094 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, 1095 unsigned int st_other, asection *sec, 1096 bool definition, bool dynamic) 1097 { 1098 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1099 1100 /* If st_other has a processor-specific meaning, specific 1101 code might be needed here. */ 1102 if (bed->elf_backend_merge_symbol_attribute) 1103 (*bed->elf_backend_merge_symbol_attribute) (h, st_other, definition, 1104 dynamic); 1105 1106 if (!dynamic) 1107 { 1108 unsigned symvis = ELF_ST_VISIBILITY (st_other); 1109 unsigned hvis = ELF_ST_VISIBILITY (h->other); 1110 1111 /* Keep the most constraining visibility. Leave the remainder 1112 of the st_other field to elf_backend_merge_symbol_attribute. */ 1113 if (symvis - 1 < hvis - 1) 1114 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); 1115 } 1116 else if (definition 1117 && ELF_ST_VISIBILITY (st_other) != STV_DEFAULT 1118 && (sec->flags & SEC_READONLY) == 0) 1119 h->protected_def = 1; 1120 } 1121 1122 /* This function is called when we want to merge a new symbol with an 1123 existing symbol. It handles the various cases which arise when we 1124 find a definition in a dynamic object, or when there is already a 1125 definition in a dynamic object. The new symbol is described by 1126 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table 1127 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK 1128 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment 1129 of an old common symbol. We set OVERRIDE if the old symbol is 1130 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for 1131 the type to change. We set SIZE_CHANGE_OK if it is OK for the size 1132 to change. By OK to change, we mean that we shouldn't warn if the 1133 type or size does change. */ 1134 1135 static bool 1136 _bfd_elf_merge_symbol (bfd *abfd, 1137 struct bfd_link_info *info, 1138 const char *name, 1139 Elf_Internal_Sym *sym, 1140 asection **psec, 1141 bfd_vma *pvalue, 1142 struct elf_link_hash_entry **sym_hash, 1143 bfd **poldbfd, 1144 bool *pold_weak, 1145 unsigned int *pold_alignment, 1146 bool *skip, 1147 bfd **override, 1148 bool *type_change_ok, 1149 bool *size_change_ok, 1150 bool *matched) 1151 { 1152 asection *sec, *oldsec; 1153 struct elf_link_hash_entry *h; 1154 struct elf_link_hash_entry *hi; 1155 struct elf_link_hash_entry *flip; 1156 int bind; 1157 bfd *oldbfd; 1158 bool newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; 1159 bool newweak, oldweak, newfunc, oldfunc; 1160 const struct elf_backend_data *bed; 1161 char *new_version; 1162 bool default_sym = *matched; 1163 struct elf_link_hash_table *htab; 1164 1165 *skip = false; 1166 *override = NULL; 1167 1168 sec = *psec; 1169 bind = ELF_ST_BIND (sym->st_info); 1170 1171 if (! bfd_is_und_section (sec)) 1172 h = elf_link_hash_lookup (elf_hash_table (info), name, true, false, false); 1173 else 1174 h = ((struct elf_link_hash_entry *) 1175 bfd_wrapped_link_hash_lookup (abfd, info, name, true, false, false)); 1176 if (h == NULL) 1177 return false; 1178 *sym_hash = h; 1179 1180 bed = get_elf_backend_data (abfd); 1181 1182 /* NEW_VERSION is the symbol version of the new symbol. */ 1183 if (h->versioned != unversioned) 1184 { 1185 /* Symbol version is unknown or versioned. */ 1186 new_version = strrchr (name, ELF_VER_CHR); 1187 if (new_version) 1188 { 1189 if (h->versioned == unknown) 1190 { 1191 if (new_version > name && new_version[-1] != ELF_VER_CHR) 1192 h->versioned = versioned_hidden; 1193 else 1194 h->versioned = versioned; 1195 } 1196 new_version += 1; 1197 if (new_version[0] == '\0') 1198 new_version = NULL; 1199 } 1200 else 1201 h->versioned = unversioned; 1202 } 1203 else 1204 new_version = NULL; 1205 1206 /* For merging, we only care about real symbols. But we need to make 1207 sure that indirect symbol dynamic flags are updated. */ 1208 hi = h; 1209 while (h->root.type == bfd_link_hash_indirect 1210 || h->root.type == bfd_link_hash_warning) 1211 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1212 1213 if (!*matched) 1214 { 1215 if (hi == h || h->root.type == bfd_link_hash_new) 1216 *matched = true; 1217 else 1218 { 1219 /* OLD_HIDDEN is true if the existing symbol is only visible 1220 to the symbol with the same symbol version. NEW_HIDDEN is 1221 true if the new symbol is only visible to the symbol with 1222 the same symbol version. */ 1223 bool old_hidden = h->versioned == versioned_hidden; 1224 bool new_hidden = hi->versioned == versioned_hidden; 1225 if (!old_hidden && !new_hidden) 1226 /* The new symbol matches the existing symbol if both 1227 aren't hidden. */ 1228 *matched = true; 1229 else 1230 { 1231 /* OLD_VERSION is the symbol version of the existing 1232 symbol. */ 1233 char *old_version; 1234 1235 if (h->versioned >= versioned) 1236 old_version = strrchr (h->root.root.string, 1237 ELF_VER_CHR) + 1; 1238 else 1239 old_version = NULL; 1240 1241 /* The new symbol matches the existing symbol if they 1242 have the same symbol version. */ 1243 *matched = (old_version == new_version 1244 || (old_version != NULL 1245 && new_version != NULL 1246 && strcmp (old_version, new_version) == 0)); 1247 } 1248 } 1249 } 1250 1251 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the 1252 existing symbol. */ 1253 1254 oldbfd = NULL; 1255 oldsec = NULL; 1256 switch (h->root.type) 1257 { 1258 default: 1259 break; 1260 1261 case bfd_link_hash_undefined: 1262 case bfd_link_hash_undefweak: 1263 oldbfd = h->root.u.undef.abfd; 1264 break; 1265 1266 case bfd_link_hash_defined: 1267 case bfd_link_hash_defweak: 1268 oldbfd = h->root.u.def.section->owner; 1269 oldsec = h->root.u.def.section; 1270 break; 1271 1272 case bfd_link_hash_common: 1273 oldbfd = h->root.u.c.p->section->owner; 1274 oldsec = h->root.u.c.p->section; 1275 if (pold_alignment) 1276 *pold_alignment = h->root.u.c.p->alignment_power; 1277 break; 1278 } 1279 if (poldbfd && *poldbfd == NULL) 1280 *poldbfd = oldbfd; 1281 1282 /* Differentiate strong and weak symbols. */ 1283 newweak = bind == STB_WEAK; 1284 oldweak = (h->root.type == bfd_link_hash_defweak 1285 || h->root.type == bfd_link_hash_undefweak); 1286 if (pold_weak) 1287 *pold_weak = oldweak; 1288 1289 /* We have to check it for every instance since the first few may be 1290 references and not all compilers emit symbol type for undefined 1291 symbols. */ 1292 bfd_elf_link_mark_dynamic_symbol (info, h, sym); 1293 1294 htab = elf_hash_table (info); 1295 1296 /* NEWDYN and OLDDYN indicate whether the new or old symbol, 1297 respectively, is from a dynamic object. */ 1298 1299 newdyn = (abfd->flags & DYNAMIC) != 0; 1300 1301 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined 1302 syms and defined syms in dynamic libraries respectively. 1303 ref_dynamic on the other hand can be set for a symbol defined in 1304 a dynamic library, and def_dynamic may not be set; When the 1305 definition in a dynamic lib is overridden by a definition in the 1306 executable use of the symbol in the dynamic lib becomes a 1307 reference to the executable symbol. */ 1308 if (newdyn) 1309 { 1310 if (bfd_is_und_section (sec)) 1311 { 1312 if (bind != STB_WEAK) 1313 { 1314 h->ref_dynamic_nonweak = 1; 1315 hi->ref_dynamic_nonweak = 1; 1316 } 1317 } 1318 else 1319 { 1320 /* Update the existing symbol only if they match. */ 1321 if (*matched) 1322 h->dynamic_def = 1; 1323 hi->dynamic_def = 1; 1324 } 1325 } 1326 1327 /* If we just created the symbol, mark it as being an ELF symbol. 1328 Other than that, there is nothing to do--there is no merge issue 1329 with a newly defined symbol--so we just return. */ 1330 1331 if (h->root.type == bfd_link_hash_new) 1332 { 1333 h->non_elf = 0; 1334 return true; 1335 } 1336 1337 /* In cases involving weak versioned symbols, we may wind up trying 1338 to merge a symbol with itself. Catch that here, to avoid the 1339 confusion that results if we try to override a symbol with 1340 itself. The additional tests catch cases like 1341 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a 1342 dynamic object, which we do want to handle here. */ 1343 if (abfd == oldbfd 1344 && (newweak || oldweak) 1345 && ((abfd->flags & DYNAMIC) == 0 1346 || !h->def_regular)) 1347 return true; 1348 1349 olddyn = false; 1350 if (oldbfd != NULL) 1351 olddyn = (oldbfd->flags & DYNAMIC) != 0; 1352 else if (oldsec != NULL) 1353 { 1354 /* This handles the special SHN_MIPS_{TEXT,DATA} section 1355 indices used by MIPS ELF. */ 1356 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; 1357 } 1358 1359 /* Set non_ir_ref_dynamic only when not handling DT_NEEDED entries. */ 1360 if (!htab->handling_dt_needed 1361 && oldbfd != NULL 1362 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN)) 1363 { 1364 if (newdyn != olddyn) 1365 { 1366 /* Handle a case where plugin_notice won't be called and thus 1367 won't set the non_ir_ref flags on the first pass over 1368 symbols. */ 1369 h->root.non_ir_ref_dynamic = true; 1370 hi->root.non_ir_ref_dynamic = true; 1371 } 1372 else if ((oldbfd->flags & BFD_PLUGIN) != 0 1373 && hi->root.type == bfd_link_hash_indirect) 1374 { 1375 /* Change indirect symbol from IR to undefined. */ 1376 hi->root.type = bfd_link_hash_undefined; 1377 hi->root.u.undef.abfd = oldbfd; 1378 } 1379 } 1380 1381 /* NEWDEF and OLDDEF indicate whether the new or old symbol, 1382 respectively, appear to be a definition rather than reference. */ 1383 1384 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); 1385 1386 olddef = (h->root.type != bfd_link_hash_undefined 1387 && h->root.type != bfd_link_hash_undefweak 1388 && h->root.type != bfd_link_hash_common); 1389 1390 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, 1391 respectively, appear to be a function. */ 1392 1393 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1394 && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); 1395 1396 oldfunc = (h->type != STT_NOTYPE 1397 && bed->is_function_type (h->type)); 1398 1399 if (!(newfunc && oldfunc) 1400 && ELF_ST_TYPE (sym->st_info) != h->type 1401 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1402 && h->type != STT_NOTYPE 1403 && (newdef || bfd_is_com_section (sec)) 1404 && (olddef || h->root.type == bfd_link_hash_common)) 1405 { 1406 /* If creating a default indirect symbol ("foo" or "foo@") from 1407 a dynamic versioned definition ("foo@@") skip doing so if 1408 there is an existing regular definition with a different 1409 type. We don't want, for example, a "time" variable in the 1410 executable overriding a "time" function in a shared library. */ 1411 if (newdyn 1412 && !olddyn) 1413 { 1414 *skip = true; 1415 return true; 1416 } 1417 1418 /* When adding a symbol from a regular object file after we have 1419 created indirect symbols, undo the indirection and any 1420 dynamic state. */ 1421 if (hi != h 1422 && !newdyn 1423 && olddyn) 1424 { 1425 h = hi; 1426 (*bed->elf_backend_hide_symbol) (info, h, true); 1427 h->forced_local = 0; 1428 h->ref_dynamic = 0; 1429 h->def_dynamic = 0; 1430 h->dynamic_def = 0; 1431 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1432 { 1433 h->root.type = bfd_link_hash_undefined; 1434 h->root.u.undef.abfd = abfd; 1435 } 1436 else 1437 { 1438 h->root.type = bfd_link_hash_new; 1439 h->root.u.undef.abfd = NULL; 1440 } 1441 return true; 1442 } 1443 } 1444 1445 /* Check TLS symbols. We don't check undefined symbols introduced 1446 by "ld -u" which have no type (and oldbfd NULL), and we don't 1447 check symbols from plugins because they also have no type. */ 1448 if (oldbfd != NULL 1449 && (oldbfd->flags & BFD_PLUGIN) == 0 1450 && (abfd->flags & BFD_PLUGIN) == 0 1451 && ELF_ST_TYPE (sym->st_info) != h->type 1452 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) 1453 { 1454 bfd *ntbfd, *tbfd; 1455 bool ntdef, tdef; 1456 asection *ntsec, *tsec; 1457 1458 if (h->type == STT_TLS) 1459 { 1460 ntbfd = abfd; 1461 ntsec = sec; 1462 ntdef = newdef; 1463 tbfd = oldbfd; 1464 tsec = oldsec; 1465 tdef = olddef; 1466 } 1467 else 1468 { 1469 ntbfd = oldbfd; 1470 ntsec = oldsec; 1471 ntdef = olddef; 1472 tbfd = abfd; 1473 tsec = sec; 1474 tdef = newdef; 1475 } 1476 1477 if (tdef && ntdef) 1478 _bfd_error_handler 1479 /* xgettext:c-format */ 1480 (_("%s: TLS definition in %pB section %pA " 1481 "mismatches non-TLS definition in %pB section %pA"), 1482 h->root.root.string, tbfd, tsec, ntbfd, ntsec); 1483 else if (!tdef && !ntdef) 1484 _bfd_error_handler 1485 /* xgettext:c-format */ 1486 (_("%s: TLS reference in %pB " 1487 "mismatches non-TLS reference in %pB"), 1488 h->root.root.string, tbfd, ntbfd); 1489 else if (tdef) 1490 _bfd_error_handler 1491 /* xgettext:c-format */ 1492 (_("%s: TLS definition in %pB section %pA " 1493 "mismatches non-TLS reference in %pB"), 1494 h->root.root.string, tbfd, tsec, ntbfd); 1495 else 1496 _bfd_error_handler 1497 /* xgettext:c-format */ 1498 (_("%s: TLS reference in %pB " 1499 "mismatches non-TLS definition in %pB section %pA"), 1500 h->root.root.string, tbfd, ntbfd, ntsec); 1501 1502 bfd_set_error (bfd_error_bad_value); 1503 return false; 1504 } 1505 1506 /* If the old symbol has non-default visibility, we ignore the new 1507 definition from a dynamic object. */ 1508 if (newdyn 1509 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 1510 && !bfd_is_und_section (sec)) 1511 { 1512 *skip = true; 1513 /* Make sure this symbol is dynamic. */ 1514 h->ref_dynamic = 1; 1515 hi->ref_dynamic = 1; 1516 /* A protected symbol has external availability. Make sure it is 1517 recorded as dynamic. 1518 1519 FIXME: Should we check type and size for protected symbol? */ 1520 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 1521 return bfd_elf_link_record_dynamic_symbol (info, h); 1522 else 1523 return true; 1524 } 1525 else if (!newdyn 1526 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT 1527 && h->def_dynamic) 1528 { 1529 /* If the new symbol with non-default visibility comes from a 1530 relocatable file and the old definition comes from a dynamic 1531 object, we remove the old definition. */ 1532 if (hi->root.type == bfd_link_hash_indirect) 1533 { 1534 /* Handle the case where the old dynamic definition is 1535 default versioned. We need to copy the symbol info from 1536 the symbol with default version to the normal one if it 1537 was referenced before. */ 1538 if (h->ref_regular) 1539 { 1540 hi->root.type = h->root.type; 1541 h->root.type = bfd_link_hash_indirect; 1542 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); 1543 1544 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1545 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1546 { 1547 /* If the new symbol is hidden or internal, completely undo 1548 any dynamic link state. */ 1549 (*bed->elf_backend_hide_symbol) (info, h, true); 1550 h->forced_local = 0; 1551 h->ref_dynamic = 0; 1552 } 1553 else 1554 h->ref_dynamic = 1; 1555 1556 h->def_dynamic = 0; 1557 /* FIXME: Should we check type and size for protected symbol? */ 1558 h->size = 0; 1559 h->type = 0; 1560 1561 h = hi; 1562 } 1563 else 1564 h = hi; 1565 } 1566 1567 /* If the old symbol was undefined before, then it will still be 1568 on the undefs list. If the new symbol is undefined or 1569 common, we can't make it bfd_link_hash_new here, because new 1570 undefined or common symbols will be added to the undefs list 1571 by _bfd_generic_link_add_one_symbol. Symbols may not be 1572 added twice to the undefs list. Also, if the new symbol is 1573 undefweak then we don't want to lose the strong undef. */ 1574 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1575 { 1576 h->root.type = bfd_link_hash_undefined; 1577 h->root.u.undef.abfd = abfd; 1578 } 1579 else 1580 { 1581 h->root.type = bfd_link_hash_new; 1582 h->root.u.undef.abfd = NULL; 1583 } 1584 1585 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1586 { 1587 /* If the new symbol is hidden or internal, completely undo 1588 any dynamic link state. */ 1589 (*bed->elf_backend_hide_symbol) (info, h, true); 1590 h->forced_local = 0; 1591 h->ref_dynamic = 0; 1592 } 1593 else 1594 h->ref_dynamic = 1; 1595 h->def_dynamic = 0; 1596 /* FIXME: Should we check type and size for protected symbol? */ 1597 h->size = 0; 1598 h->type = 0; 1599 return true; 1600 } 1601 1602 /* If a new weak symbol definition comes from a regular file and the 1603 old symbol comes from a dynamic library, we treat the new one as 1604 strong. Similarly, an old weak symbol definition from a regular 1605 file is treated as strong when the new symbol comes from a dynamic 1606 library. Further, an old weak symbol from a dynamic library is 1607 treated as strong if the new symbol is from a dynamic library. 1608 This reflects the way glibc's ld.so works. 1609 1610 Also allow a weak symbol to override a linker script symbol 1611 defined by an early pass over the script. This is done so the 1612 linker knows the symbol is defined in an object file, for the 1613 DEFINED script function. 1614 1615 Do this before setting *type_change_ok or *size_change_ok so that 1616 we warn properly when dynamic library symbols are overridden. */ 1617 1618 if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) 1619 newweak = false; 1620 if (olddef && newdyn) 1621 oldweak = false; 1622 1623 /* Allow changes between different types of function symbol. */ 1624 if (newfunc && oldfunc) 1625 *type_change_ok = true; 1626 1627 /* It's OK to change the type if either the existing symbol or the 1628 new symbol is weak. A type change is also OK if the old symbol 1629 is undefined and the new symbol is defined. */ 1630 1631 if (oldweak 1632 || newweak 1633 || (newdef 1634 && h->root.type == bfd_link_hash_undefined)) 1635 *type_change_ok = true; 1636 1637 /* It's OK to change the size if either the existing symbol or the 1638 new symbol is weak, or if the old symbol is undefined. */ 1639 1640 if (*type_change_ok 1641 || h->root.type == bfd_link_hash_undefined) 1642 *size_change_ok = true; 1643 1644 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old 1645 symbol, respectively, appears to be a common symbol in a dynamic 1646 object. If a symbol appears in an uninitialized section, and is 1647 not weak, and is not a function, then it may be a common symbol 1648 which was resolved when the dynamic object was created. We want 1649 to treat such symbols specially, because they raise special 1650 considerations when setting the symbol size: if the symbol 1651 appears as a common symbol in a regular object, and the size in 1652 the regular object is larger, we must make sure that we use the 1653 larger size. This problematic case can always be avoided in C, 1654 but it must be handled correctly when using Fortran shared 1655 libraries. 1656 1657 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and 1658 likewise for OLDDYNCOMMON and OLDDEF. 1659 1660 Note that this test is just a heuristic, and that it is quite 1661 possible to have an uninitialized symbol in a shared object which 1662 is really a definition, rather than a common symbol. This could 1663 lead to some minor confusion when the symbol really is a common 1664 symbol in some regular object. However, I think it will be 1665 harmless. */ 1666 1667 if (newdyn 1668 && newdef 1669 && !newweak 1670 && (sec->flags & SEC_ALLOC) != 0 1671 && (sec->flags & SEC_LOAD) == 0 1672 && sym->st_size > 0 1673 && !newfunc) 1674 newdyncommon = true; 1675 else 1676 newdyncommon = false; 1677 1678 if (olddyn 1679 && olddef 1680 && h->root.type == bfd_link_hash_defined 1681 && h->def_dynamic 1682 && (h->root.u.def.section->flags & SEC_ALLOC) != 0 1683 && (h->root.u.def.section->flags & SEC_LOAD) == 0 1684 && h->size > 0 1685 && !oldfunc) 1686 olddyncommon = true; 1687 else 1688 olddyncommon = false; 1689 1690 /* We now know everything about the old and new symbols. We ask the 1691 backend to check if we can merge them. */ 1692 if (bed->merge_symbol != NULL) 1693 { 1694 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) 1695 return false; 1696 sec = *psec; 1697 } 1698 1699 /* There are multiple definitions of a normal symbol. Skip the 1700 default symbol as well as definition from an IR object. */ 1701 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak 1702 && !default_sym && h->def_regular 1703 && !(oldbfd != NULL 1704 && (oldbfd->flags & BFD_PLUGIN) != 0 1705 && (abfd->flags & BFD_PLUGIN) == 0)) 1706 { 1707 /* Handle a multiple definition. */ 1708 (*info->callbacks->multiple_definition) (info, &h->root, 1709 abfd, sec, *pvalue); 1710 *skip = true; 1711 return true; 1712 } 1713 1714 /* If both the old and the new symbols look like common symbols in a 1715 dynamic object, set the size of the symbol to the larger of the 1716 two. */ 1717 1718 if (olddyncommon 1719 && newdyncommon 1720 && sym->st_size != h->size) 1721 { 1722 /* Since we think we have two common symbols, issue a multiple 1723 common warning if desired. Note that we only warn if the 1724 size is different. If the size is the same, we simply let 1725 the old symbol override the new one as normally happens with 1726 symbols defined in dynamic objects. */ 1727 1728 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1729 bfd_link_hash_common, sym->st_size); 1730 if (sym->st_size > h->size) 1731 h->size = sym->st_size; 1732 1733 *size_change_ok = true; 1734 } 1735 1736 /* If we are looking at a dynamic object, and we have found a 1737 definition, we need to see if the symbol was already defined by 1738 some other object. If so, we want to use the existing 1739 definition, and we do not want to report a multiple symbol 1740 definition error; we do this by clobbering *PSEC to be 1741 bfd_und_section_ptr. 1742 1743 We treat a common symbol as a definition if the symbol in the 1744 shared library is a function, since common symbols always 1745 represent variables; this can cause confusion in principle, but 1746 any such confusion would seem to indicate an erroneous program or 1747 shared library. We also permit a common symbol in a regular 1748 object to override a weak symbol in a shared object. */ 1749 1750 if (newdyn 1751 && newdef 1752 && (olddef 1753 || (h->root.type == bfd_link_hash_common 1754 && (newweak || newfunc)))) 1755 { 1756 *override = abfd; 1757 newdef = false; 1758 newdyncommon = false; 1759 1760 *psec = sec = bfd_und_section_ptr; 1761 *size_change_ok = true; 1762 1763 /* If we get here when the old symbol is a common symbol, then 1764 we are explicitly letting it override a weak symbol or 1765 function in a dynamic object, and we don't want to warn about 1766 a type change. If the old symbol is a defined symbol, a type 1767 change warning may still be appropriate. */ 1768 1769 if (h->root.type == bfd_link_hash_common) 1770 *type_change_ok = true; 1771 } 1772 1773 /* Handle the special case of an old common symbol merging with a 1774 new symbol which looks like a common symbol in a shared object. 1775 We change *PSEC and *PVALUE to make the new symbol look like a 1776 common symbol, and let _bfd_generic_link_add_one_symbol do the 1777 right thing. */ 1778 1779 if (newdyncommon 1780 && h->root.type == bfd_link_hash_common) 1781 { 1782 *override = oldbfd; 1783 newdef = false; 1784 newdyncommon = false; 1785 *pvalue = sym->st_size; 1786 *psec = sec = bed->common_section (oldsec); 1787 *size_change_ok = true; 1788 } 1789 1790 /* Skip weak definitions of symbols that are already defined. */ 1791 if (newdef && olddef && newweak) 1792 { 1793 /* Don't skip new non-IR weak syms. */ 1794 if (!(oldbfd != NULL 1795 && (oldbfd->flags & BFD_PLUGIN) != 0 1796 && (abfd->flags & BFD_PLUGIN) == 0)) 1797 { 1798 newdef = false; 1799 *skip = true; 1800 } 1801 1802 /* Merge st_other. If the symbol already has a dynamic index, 1803 but visibility says it should not be visible, turn it into a 1804 local symbol. */ 1805 elf_merge_st_other (abfd, h, sym->st_other, sec, newdef, newdyn); 1806 if (h->dynindx != -1) 1807 switch (ELF_ST_VISIBILITY (h->other)) 1808 { 1809 case STV_INTERNAL: 1810 case STV_HIDDEN: 1811 (*bed->elf_backend_hide_symbol) (info, h, true); 1812 break; 1813 } 1814 } 1815 1816 /* If the old symbol is from a dynamic object, and the new symbol is 1817 a definition which is not from a dynamic object, then the new 1818 symbol overrides the old symbol. Symbols from regular files 1819 always take precedence over symbols from dynamic objects, even if 1820 they are defined after the dynamic object in the link. 1821 1822 As above, we again permit a common symbol in a regular object to 1823 override a definition in a shared object if the shared object 1824 symbol is a function or is weak. */ 1825 1826 flip = NULL; 1827 if (!newdyn 1828 && (newdef 1829 || (bfd_is_com_section (sec) 1830 && (oldweak || oldfunc))) 1831 && olddyn 1832 && olddef 1833 && h->def_dynamic) 1834 { 1835 /* Change the hash table entry to undefined, and let 1836 _bfd_generic_link_add_one_symbol do the right thing with the 1837 new definition. */ 1838 1839 h->root.type = bfd_link_hash_undefined; 1840 h->root.u.undef.abfd = h->root.u.def.section->owner; 1841 *size_change_ok = true; 1842 1843 olddef = false; 1844 olddyncommon = false; 1845 1846 /* We again permit a type change when a common symbol may be 1847 overriding a function. */ 1848 1849 if (bfd_is_com_section (sec)) 1850 { 1851 if (oldfunc) 1852 { 1853 /* If a common symbol overrides a function, make sure 1854 that it isn't defined dynamically nor has type 1855 function. */ 1856 h->def_dynamic = 0; 1857 h->type = STT_NOTYPE; 1858 } 1859 *type_change_ok = true; 1860 } 1861 1862 if (hi->root.type == bfd_link_hash_indirect) 1863 flip = hi; 1864 else 1865 /* This union may have been set to be non-NULL when this symbol 1866 was seen in a dynamic object. We must force the union to be 1867 NULL, so that it is correct for a regular symbol. */ 1868 h->verinfo.vertree = NULL; 1869 } 1870 1871 /* Handle the special case of a new common symbol merging with an 1872 old symbol that looks like it might be a common symbol defined in 1873 a shared object. Note that we have already handled the case in 1874 which a new common symbol should simply override the definition 1875 in the shared library. */ 1876 1877 if (! newdyn 1878 && bfd_is_com_section (sec) 1879 && olddyncommon) 1880 { 1881 /* It would be best if we could set the hash table entry to a 1882 common symbol, but we don't know what to use for the section 1883 or the alignment. */ 1884 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1885 bfd_link_hash_common, sym->st_size); 1886 1887 /* If the presumed common symbol in the dynamic object is 1888 larger, pretend that the new symbol has its size. */ 1889 1890 if (h->size > *pvalue) 1891 *pvalue = h->size; 1892 1893 /* We need to remember the alignment required by the symbol 1894 in the dynamic object. */ 1895 BFD_ASSERT (pold_alignment); 1896 *pold_alignment = h->root.u.def.section->alignment_power; 1897 1898 olddef = false; 1899 olddyncommon = false; 1900 1901 h->root.type = bfd_link_hash_undefined; 1902 h->root.u.undef.abfd = h->root.u.def.section->owner; 1903 1904 *size_change_ok = true; 1905 *type_change_ok = true; 1906 1907 if (hi->root.type == bfd_link_hash_indirect) 1908 flip = hi; 1909 else 1910 h->verinfo.vertree = NULL; 1911 } 1912 1913 if (flip != NULL) 1914 { 1915 /* Handle the case where we had a versioned symbol in a dynamic 1916 library and now find a definition in a normal object. In this 1917 case, we make the versioned symbol point to the normal one. */ 1918 flip->root.type = h->root.type; 1919 flip->root.u.undef.abfd = h->root.u.undef.abfd; 1920 h->root.type = bfd_link_hash_indirect; 1921 h->root.u.i.link = (struct bfd_link_hash_entry *) flip; 1922 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); 1923 if (h->def_dynamic) 1924 { 1925 h->def_dynamic = 0; 1926 flip->ref_dynamic = 1; 1927 } 1928 } 1929 1930 return true; 1931 } 1932 1933 /* This function is called to create an indirect symbol from the 1934 default for the symbol with the default version if needed. The 1935 symbol is described by H, NAME, SYM, SEC, and VALUE. We 1936 set DYNSYM if the new indirect symbol is dynamic. */ 1937 1938 static bool 1939 _bfd_elf_add_default_symbol (bfd *abfd, 1940 struct bfd_link_info *info, 1941 struct elf_link_hash_entry *h, 1942 const char *name, 1943 Elf_Internal_Sym *sym, 1944 asection *sec, 1945 bfd_vma value, 1946 bfd **poldbfd, 1947 bool *dynsym) 1948 { 1949 bool type_change_ok; 1950 bool size_change_ok; 1951 bool skip; 1952 char *shortname; 1953 struct elf_link_hash_entry *hi; 1954 struct bfd_link_hash_entry *bh; 1955 const struct elf_backend_data *bed; 1956 bool collect; 1957 bool dynamic; 1958 bfd *override; 1959 char *p; 1960 size_t len, shortlen; 1961 asection *tmp_sec; 1962 bool matched; 1963 1964 if (h->versioned == unversioned || h->versioned == versioned_hidden) 1965 return true; 1966 1967 /* If this symbol has a version, and it is the default version, we 1968 create an indirect symbol from the default name to the fully 1969 decorated name. This will cause external references which do not 1970 specify a version to be bound to this version of the symbol. */ 1971 p = strchr (name, ELF_VER_CHR); 1972 if (h->versioned == unknown) 1973 { 1974 if (p == NULL) 1975 { 1976 h->versioned = unversioned; 1977 return true; 1978 } 1979 else 1980 { 1981 if (p[1] != ELF_VER_CHR) 1982 { 1983 h->versioned = versioned_hidden; 1984 return true; 1985 } 1986 else 1987 h->versioned = versioned; 1988 } 1989 } 1990 else 1991 { 1992 /* PR ld/19073: We may see an unversioned definition after the 1993 default version. */ 1994 if (p == NULL) 1995 return true; 1996 } 1997 1998 bed = get_elf_backend_data (abfd); 1999 collect = bed->collect; 2000 dynamic = (abfd->flags & DYNAMIC) != 0; 2001 2002 shortlen = p - name; 2003 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); 2004 if (shortname == NULL) 2005 return false; 2006 memcpy (shortname, name, shortlen); 2007 shortname[shortlen] = '\0'; 2008 2009 /* We are going to create a new symbol. Merge it with any existing 2010 symbol with this name. For the purposes of the merge, act as 2011 though we were defining the symbol we just defined, although we 2012 actually going to define an indirect symbol. */ 2013 type_change_ok = false; 2014 size_change_ok = false; 2015 matched = true; 2016 tmp_sec = sec; 2017 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 2018 &hi, poldbfd, NULL, NULL, &skip, &override, 2019 &type_change_ok, &size_change_ok, &matched)) 2020 return false; 2021 2022 if (skip) 2023 goto nondefault; 2024 2025 if (hi->def_regular || ELF_COMMON_DEF_P (hi)) 2026 { 2027 /* If the undecorated symbol will have a version added by a 2028 script different to H, then don't indirect to/from the 2029 undecorated symbol. This isn't ideal because we may not yet 2030 have seen symbol versions, if given by a script on the 2031 command line rather than via --version-script. */ 2032 if (hi->verinfo.vertree == NULL && info->version_info != NULL) 2033 { 2034 bool hide; 2035 2036 hi->verinfo.vertree 2037 = bfd_find_version_for_sym (info->version_info, 2038 hi->root.root.string, &hide); 2039 if (hi->verinfo.vertree != NULL && hide) 2040 { 2041 (*bed->elf_backend_hide_symbol) (info, hi, true); 2042 goto nondefault; 2043 } 2044 } 2045 if (hi->verinfo.vertree != NULL 2046 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) 2047 goto nondefault; 2048 } 2049 2050 if (! override) 2051 { 2052 /* Add the default symbol if not performing a relocatable link. */ 2053 if (! bfd_link_relocatable (info)) 2054 { 2055 bh = &hi->root; 2056 if (bh->type == bfd_link_hash_defined 2057 && bh->u.def.section->owner != NULL 2058 && (bh->u.def.section->owner->flags & BFD_PLUGIN) != 0) 2059 { 2060 /* Mark the previous definition from IR object as 2061 undefined so that the generic linker will override 2062 it. */ 2063 bh->type = bfd_link_hash_undefined; 2064 bh->u.undef.abfd = bh->u.def.section->owner; 2065 } 2066 if (! (_bfd_generic_link_add_one_symbol 2067 (info, abfd, shortname, BSF_INDIRECT, 2068 bfd_ind_section_ptr, 2069 0, name, false, collect, &bh))) 2070 return false; 2071 hi = (struct elf_link_hash_entry *) bh; 2072 } 2073 } 2074 else 2075 { 2076 /* In this case the symbol named SHORTNAME is overriding the 2077 indirect symbol we want to add. We were planning on making 2078 SHORTNAME an indirect symbol referring to NAME. SHORTNAME 2079 is the name without a version. NAME is the fully versioned 2080 name, and it is the default version. 2081 2082 Overriding means that we already saw a definition for the 2083 symbol SHORTNAME in a regular object, and it is overriding 2084 the symbol defined in the dynamic object. 2085 2086 When this happens, we actually want to change NAME, the 2087 symbol we just added, to refer to SHORTNAME. This will cause 2088 references to NAME in the shared object to become references 2089 to SHORTNAME in the regular object. This is what we expect 2090 when we override a function in a shared object: that the 2091 references in the shared object will be mapped to the 2092 definition in the regular object. */ 2093 2094 while (hi->root.type == bfd_link_hash_indirect 2095 || hi->root.type == bfd_link_hash_warning) 2096 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 2097 2098 h->root.type = bfd_link_hash_indirect; 2099 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 2100 if (h->def_dynamic) 2101 { 2102 h->def_dynamic = 0; 2103 hi->ref_dynamic = 1; 2104 if (hi->ref_regular 2105 || hi->def_regular) 2106 { 2107 if (! bfd_elf_link_record_dynamic_symbol (info, hi)) 2108 return false; 2109 } 2110 } 2111 2112 /* Now set HI to H, so that the following code will set the 2113 other fields correctly. */ 2114 hi = h; 2115 } 2116 2117 /* Check if HI is a warning symbol. */ 2118 if (hi->root.type == bfd_link_hash_warning) 2119 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 2120 2121 /* If there is a duplicate definition somewhere, then HI may not 2122 point to an indirect symbol. We will have reported an error to 2123 the user in that case. */ 2124 2125 if (hi->root.type == bfd_link_hash_indirect) 2126 { 2127 struct elf_link_hash_entry *ht; 2128 2129 ht = (struct elf_link_hash_entry *) hi->root.u.i.link; 2130 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); 2131 2132 /* If we first saw a reference to SHORTNAME with non-default 2133 visibility, merge that visibility to the @@VER symbol. */ 2134 elf_merge_st_other (abfd, ht, hi->other, sec, true, dynamic); 2135 2136 /* A reference to the SHORTNAME symbol from a dynamic library 2137 will be satisfied by the versioned symbol at runtime. In 2138 effect, we have a reference to the versioned symbol. */ 2139 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 2140 hi->dynamic_def |= ht->dynamic_def; 2141 2142 /* See if the new flags lead us to realize that the symbol must 2143 be dynamic. */ 2144 if (! *dynsym) 2145 { 2146 if (! dynamic) 2147 { 2148 if (! bfd_link_executable (info) 2149 || hi->def_dynamic 2150 || hi->ref_dynamic) 2151 *dynsym = true; 2152 } 2153 else 2154 { 2155 if (hi->ref_regular) 2156 *dynsym = true; 2157 } 2158 } 2159 } 2160 2161 /* We also need to define an indirection from the nondefault version 2162 of the symbol. */ 2163 2164 nondefault: 2165 len = strlen (name); 2166 shortname = (char *) bfd_hash_allocate (&info->hash->table, len); 2167 if (shortname == NULL) 2168 return false; 2169 memcpy (shortname, name, shortlen); 2170 memcpy (shortname + shortlen, p + 1, len - shortlen); 2171 2172 /* Once again, merge with any existing symbol. */ 2173 type_change_ok = false; 2174 size_change_ok = false; 2175 tmp_sec = sec; 2176 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 2177 &hi, poldbfd, NULL, NULL, &skip, &override, 2178 &type_change_ok, &size_change_ok, &matched)) 2179 return false; 2180 2181 if (skip) 2182 { 2183 if (!dynamic 2184 && h->root.type == bfd_link_hash_defweak 2185 && hi->root.type == bfd_link_hash_defined) 2186 { 2187 /* We are handling a weak sym@@ver and attempting to define 2188 a weak sym@ver, but _bfd_elf_merge_symbol said to skip the 2189 new weak sym@ver because there is already a strong sym@ver. 2190 However, sym@ver and sym@@ver are really the same symbol. 2191 The existing strong sym@ver ought to override sym@@ver. */ 2192 h->root.type = bfd_link_hash_defined; 2193 h->root.u.def.section = hi->root.u.def.section; 2194 h->root.u.def.value = hi->root.u.def.value; 2195 hi->root.type = bfd_link_hash_indirect; 2196 hi->root.u.i.link = &h->root; 2197 } 2198 else 2199 return true; 2200 } 2201 else if (override) 2202 { 2203 /* Here SHORTNAME is a versioned name, so we don't expect to see 2204 the type of override we do in the case above unless it is 2205 overridden by a versioned definition. */ 2206 if (hi->root.type != bfd_link_hash_defined 2207 && hi->root.type != bfd_link_hash_defweak) 2208 _bfd_error_handler 2209 /* xgettext:c-format */ 2210 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), 2211 abfd, shortname); 2212 return true; 2213 } 2214 else 2215 { 2216 bh = &hi->root; 2217 if (! (_bfd_generic_link_add_one_symbol 2218 (info, abfd, shortname, BSF_INDIRECT, 2219 bfd_ind_section_ptr, 0, name, false, collect, &bh))) 2220 return false; 2221 hi = (struct elf_link_hash_entry *) bh; 2222 } 2223 2224 /* If there is a duplicate definition somewhere, then HI may not 2225 point to an indirect symbol. We will have reported an error 2226 to the user in that case. */ 2227 if (hi->root.type == bfd_link_hash_indirect) 2228 { 2229 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 2230 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 2231 hi->dynamic_def |= h->dynamic_def; 2232 2233 /* If we first saw a reference to @VER symbol with 2234 non-default visibility, merge that visibility to the 2235 @@VER symbol. */ 2236 elf_merge_st_other (abfd, h, hi->other, sec, true, dynamic); 2237 2238 /* See if the new flags lead us to realize that the symbol 2239 must be dynamic. */ 2240 if (! *dynsym) 2241 { 2242 if (! dynamic) 2243 { 2244 if (! bfd_link_executable (info) 2245 || hi->ref_dynamic) 2246 *dynsym = true; 2247 } 2248 else 2249 { 2250 if (hi->ref_regular) 2251 *dynsym = true; 2252 } 2253 } 2254 } 2255 2256 return true; 2257 } 2258 2259 /* This routine is used to export all defined symbols into the dynamic 2261 symbol table. It is called via elf_link_hash_traverse. */ 2262 2263 static bool 2264 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) 2265 { 2266 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2267 2268 /* Ignore indirect symbols. These are added by the versioning code. */ 2269 if (h->root.type == bfd_link_hash_indirect) 2270 return true; 2271 2272 /* Ignore this if we won't export it. */ 2273 if (!eif->info->export_dynamic && !h->dynamic) 2274 return true; 2275 2276 if (h->dynindx == -1 2277 && (h->def_regular || h->ref_regular) 2278 && ! bfd_hide_sym_by_version (eif->info->version_info, 2279 h->root.root.string)) 2280 { 2281 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2282 { 2283 eif->failed = true; 2284 return false; 2285 } 2286 } 2287 2288 return true; 2289 } 2290 2291 /* Return the glibc version reference if VERSION_DEP is added to the 2293 list of glibc version dependencies successfully. VERSION_DEP will 2294 be put into the .gnu.version_r section. GLIBC_MINOR_BASE is the 2295 pointer to the glibc minor base version. */ 2296 2297 static Elf_Internal_Verneed * 2298 elf_link_add_glibc_verneed (struct elf_find_verdep_info *rinfo, 2299 Elf_Internal_Verneed *glibc_verref, 2300 const char *version_dep, 2301 int *glibc_minor_base) 2302 { 2303 Elf_Internal_Verneed *t; 2304 Elf_Internal_Vernaux *a; 2305 size_t amt; 2306 int minor_version = -1; 2307 2308 if (glibc_verref != NULL) 2309 { 2310 t = glibc_verref; 2311 2312 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2313 { 2314 /* Return if VERSION_DEP dependency has been added. */ 2315 if (a->vna_nodename == version_dep 2316 || strcmp (a->vna_nodename, version_dep) == 0) 2317 return t; 2318 } 2319 } 2320 else 2321 { 2322 for (t = elf_tdata (rinfo->info->output_bfd)->verref; 2323 t != NULL; 2324 t = t->vn_nextref) 2325 { 2326 const char *soname = bfd_elf_get_dt_soname (t->vn_bfd); 2327 if (soname != NULL && startswith (soname, "libc.so.")) 2328 break; 2329 } 2330 2331 /* Skip the shared library if it isn't libc.so. */ 2332 if (t == NULL) 2333 return t; 2334 2335 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2336 { 2337 /* Return if VERSION_DEP dependency has been added. */ 2338 if (a->vna_nodename == version_dep 2339 || strcmp (a->vna_nodename, version_dep) == 0) 2340 return t; 2341 2342 /* Check if libc.so provides GLIBC_2.XX version. */ 2343 if (startswith (a->vna_nodename, "GLIBC_2.")) 2344 { 2345 minor_version = strtol (a->vna_nodename + 8, NULL, 10); 2346 if (minor_version < *glibc_minor_base) 2347 *glibc_minor_base = minor_version; 2348 } 2349 } 2350 2351 /* Skip if it isn't linked against glibc. */ 2352 if (minor_version < 0) 2353 return NULL; 2354 } 2355 2356 /* Skip if 2.GLIBC_MINOR_BASE includes VERSION_DEP. */ 2357 if (startswith (version_dep, "GLIBC_2.")) 2358 { 2359 minor_version = strtol (version_dep + 8, NULL, 10); 2360 if (minor_version <= *glibc_minor_base) 2361 return NULL; 2362 } 2363 2364 amt = sizeof *a; 2365 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); 2366 if (a == NULL) 2367 { 2368 rinfo->failed = true; 2369 return NULL; 2370 } 2371 2372 a->vna_nodename = version_dep; 2373 a->vna_flags = 0; 2374 a->vna_nextptr = t->vn_auxptr; 2375 a->vna_other = rinfo->vers + 1; 2376 ++rinfo->vers; 2377 2378 t->vn_auxptr = a; 2379 2380 return t; 2381 } 2382 2383 /* Add VERSION_DEP to the list of version dependencies when linked 2384 against glibc. */ 2385 2386 void 2387 _bfd_elf_link_add_glibc_version_dependency 2388 (struct elf_find_verdep_info *rinfo, 2389 const char *version_dep[]) 2390 { 2391 Elf_Internal_Verneed *t = NULL; 2392 int glibc_minor_base = INT_MAX; 2393 2394 do 2395 { 2396 t = elf_link_add_glibc_verneed (rinfo, t, *version_dep, 2397 &glibc_minor_base); 2398 /* Return if there is no glibc version reference. */ 2399 if (t == NULL) 2400 return; 2401 version_dep++; 2402 } 2403 while (*version_dep != NULL); 2404 } 2405 2406 /* Add GLIBC_ABI_DT_RELR to the list of version dependencies when 2407 linked against glibc. */ 2408 2409 void 2410 _bfd_elf_link_add_dt_relr_dependency (struct elf_find_verdep_info *rinfo) 2411 { 2412 if (rinfo->info->enable_dt_relr) 2413 { 2414 const char *version[] = 2415 { 2416 "GLIBC_ABI_DT_RELR", 2417 NULL 2418 }; 2419 _bfd_elf_link_add_glibc_version_dependency (rinfo, version); 2420 } 2421 } 2422 2423 /* Look through the symbols which are defined in other shared 2424 libraries and referenced here. Update the list of version 2425 dependencies. This will be put into the .gnu.version_r section. 2426 This function is called via elf_link_hash_traverse. */ 2427 2428 static bool 2429 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, 2430 void *data) 2431 { 2432 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; 2433 Elf_Internal_Verneed *t; 2434 Elf_Internal_Vernaux *a; 2435 size_t amt; 2436 2437 /* We only care about symbols defined in shared objects with version 2438 information. */ 2439 if (!h->def_dynamic 2440 || h->def_regular 2441 || h->dynindx == -1 2442 || h->verinfo.verdef == NULL 2443 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 2444 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 2445 return true; 2446 2447 /* See if we already know about this version. */ 2448 for (t = elf_tdata (rinfo->info->output_bfd)->verref; 2449 t != NULL; 2450 t = t->vn_nextref) 2451 { 2452 if (t->vn_bfd != h->verinfo.verdef->vd_bfd) 2453 continue; 2454 2455 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2456 if (a->vna_nodename == h->verinfo.verdef->vd_nodename) 2457 return true; 2458 2459 break; 2460 } 2461 2462 /* This is a new version. Add it to tree we are building. */ 2463 2464 if (t == NULL) 2465 { 2466 amt = sizeof *t; 2467 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); 2468 if (t == NULL) 2469 { 2470 rinfo->failed = true; 2471 return false; 2472 } 2473 2474 t->vn_bfd = h->verinfo.verdef->vd_bfd; 2475 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; 2476 elf_tdata (rinfo->info->output_bfd)->verref = t; 2477 } 2478 2479 amt = sizeof *a; 2480 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); 2481 if (a == NULL) 2482 { 2483 rinfo->failed = true; 2484 return false; 2485 } 2486 2487 /* Note that we are copying a string pointer here, and testing it 2488 above. If bfd_elf_string_from_elf_section is ever changed to 2489 discard the string data when low in memory, this will have to be 2490 fixed. */ 2491 a->vna_nodename = h->verinfo.verdef->vd_nodename; 2492 2493 a->vna_flags = h->verinfo.verdef->vd_flags; 2494 a->vna_nextptr = t->vn_auxptr; 2495 2496 h->verinfo.verdef->vd_exp_refno = rinfo->vers; 2497 ++rinfo->vers; 2498 2499 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; 2500 2501 t->vn_auxptr = a; 2502 2503 return true; 2504 } 2505 2506 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is 2507 hidden. Set *T_P to NULL if there is no match. */ 2508 2509 static bool 2510 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, 2511 struct elf_link_hash_entry *h, 2512 const char *version_p, 2513 struct bfd_elf_version_tree **t_p, 2514 bool *hide) 2515 { 2516 struct bfd_elf_version_tree *t; 2517 2518 /* Look for the version. If we find it, it is no longer weak. */ 2519 for (t = info->version_info; t != NULL; t = t->next) 2520 { 2521 if (strcmp (t->name, version_p) == 0) 2522 { 2523 size_t len; 2524 char *alc; 2525 struct bfd_elf_version_expr *d; 2526 2527 len = version_p - h->root.root.string; 2528 alc = (char *) bfd_malloc (len); 2529 if (alc == NULL) 2530 return false; 2531 memcpy (alc, h->root.root.string, len - 1); 2532 alc[len - 1] = '\0'; 2533 if (alc[len - 2] == ELF_VER_CHR) 2534 alc[len - 2] = '\0'; 2535 2536 h->verinfo.vertree = t; 2537 t->used = true; 2538 d = NULL; 2539 2540 if (t->globals.list != NULL) 2541 d = (*t->match) (&t->globals, NULL, alc); 2542 2543 /* See if there is anything to force this symbol to 2544 local scope. */ 2545 if (d == NULL && t->locals.list != NULL) 2546 { 2547 d = (*t->match) (&t->locals, NULL, alc); 2548 if (d != NULL 2549 && h->dynindx != -1 2550 && ! info->export_dynamic) 2551 *hide = true; 2552 } 2553 2554 free (alc); 2555 break; 2556 } 2557 } 2558 2559 *t_p = t; 2560 2561 return true; 2562 } 2563 2564 /* Return TRUE if the symbol H is hidden by version script. */ 2565 2566 bool 2567 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, 2568 struct elf_link_hash_entry *h) 2569 { 2570 const char *p; 2571 bool hide = false; 2572 const struct elf_backend_data *bed 2573 = get_elf_backend_data (info->output_bfd); 2574 2575 /* Version script only hides symbols defined in regular objects. */ 2576 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 2577 return true; 2578 2579 p = strchr (h->root.root.string, ELF_VER_CHR); 2580 if (p != NULL && h->verinfo.vertree == NULL) 2581 { 2582 struct bfd_elf_version_tree *t; 2583 2584 ++p; 2585 if (*p == ELF_VER_CHR) 2586 ++p; 2587 2588 if (*p != '\0' 2589 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) 2590 && hide) 2591 { 2592 if (hide) 2593 (*bed->elf_backend_hide_symbol) (info, h, true); 2594 return true; 2595 } 2596 } 2597 2598 /* If we don't have a version for this symbol, see if we can find 2599 something. */ 2600 if (h->verinfo.vertree == NULL && info->version_info != NULL) 2601 { 2602 h->verinfo.vertree 2603 = bfd_find_version_for_sym (info->version_info, 2604 h->root.root.string, &hide); 2605 if (h->verinfo.vertree != NULL && hide) 2606 { 2607 (*bed->elf_backend_hide_symbol) (info, h, true); 2608 return true; 2609 } 2610 } 2611 2612 return false; 2613 } 2614 2615 /* Figure out appropriate versions for all the symbols. We may not 2616 have the version number script until we have read all of the input 2617 files, so until that point we don't know which symbols should be 2618 local. This function is called via elf_link_hash_traverse. */ 2619 2620 static bool 2621 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) 2622 { 2623 struct elf_info_failed *sinfo; 2624 struct bfd_link_info *info; 2625 const struct elf_backend_data *bed; 2626 struct elf_info_failed eif; 2627 char *p; 2628 bool hide; 2629 2630 sinfo = (struct elf_info_failed *) data; 2631 info = sinfo->info; 2632 2633 /* Fix the symbol flags. */ 2634 eif.failed = false; 2635 eif.info = info; 2636 if (! _bfd_elf_fix_symbol_flags (h, &eif)) 2637 { 2638 if (eif.failed) 2639 sinfo->failed = true; 2640 return false; 2641 } 2642 2643 bed = get_elf_backend_data (info->output_bfd); 2644 2645 /* We only need version numbers for symbols defined in regular 2646 objects. */ 2647 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 2648 { 2649 /* Hide symbols defined in discarded input sections. */ 2650 if ((h->root.type == bfd_link_hash_defined 2651 || h->root.type == bfd_link_hash_defweak) 2652 && discarded_section (h->root.u.def.section)) 2653 (*bed->elf_backend_hide_symbol) (info, h, true); 2654 return true; 2655 } 2656 2657 hide = false; 2658 p = strchr (h->root.root.string, ELF_VER_CHR); 2659 if (p != NULL && h->verinfo.vertree == NULL) 2660 { 2661 struct bfd_elf_version_tree *t; 2662 2663 ++p; 2664 if (*p == ELF_VER_CHR) 2665 ++p; 2666 2667 /* If there is no version string, we can just return out. */ 2668 if (*p == '\0') 2669 return true; 2670 2671 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) 2672 { 2673 sinfo->failed = true; 2674 return false; 2675 } 2676 2677 if (hide) 2678 (*bed->elf_backend_hide_symbol) (info, h, true); 2679 2680 /* If we are building an application, we need to create a 2681 version node for this version. */ 2682 if (t == NULL && bfd_link_executable (info)) 2683 { 2684 struct bfd_elf_version_tree **pp; 2685 int version_index; 2686 2687 /* If we aren't going to export this symbol, we don't need 2688 to worry about it. */ 2689 if (h->dynindx == -1) 2690 return true; 2691 2692 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, 2693 sizeof *t); 2694 if (t == NULL) 2695 { 2696 sinfo->failed = true; 2697 return false; 2698 } 2699 2700 t->name = p; 2701 t->name_indx = (unsigned int) -1; 2702 t->used = true; 2703 2704 version_index = 1; 2705 /* Don't count anonymous version tag. */ 2706 if (sinfo->info->version_info != NULL 2707 && sinfo->info->version_info->vernum == 0) 2708 version_index = 0; 2709 for (pp = &sinfo->info->version_info; 2710 *pp != NULL; 2711 pp = &(*pp)->next) 2712 ++version_index; 2713 t->vernum = version_index; 2714 2715 *pp = t; 2716 2717 h->verinfo.vertree = t; 2718 } 2719 else if (t == NULL) 2720 { 2721 /* We could not find the version for a symbol when 2722 generating a shared archive. Return an error. */ 2723 _bfd_error_handler 2724 /* xgettext:c-format */ 2725 (_("%pB: version node not found for symbol %s"), 2726 info->output_bfd, h->root.root.string); 2727 bfd_set_error (bfd_error_bad_value); 2728 sinfo->failed = true; 2729 return false; 2730 } 2731 } 2732 2733 /* If we don't have a version for this symbol, see if we can find 2734 something. */ 2735 if (!hide 2736 && h->verinfo.vertree == NULL 2737 && sinfo->info->version_info != NULL) 2738 { 2739 h->verinfo.vertree 2740 = bfd_find_version_for_sym (sinfo->info->version_info, 2741 h->root.root.string, &hide); 2742 if (h->verinfo.vertree != NULL && hide) 2743 (*bed->elf_backend_hide_symbol) (info, h, true); 2744 } 2745 2746 return true; 2747 } 2748 2749 /* Read and swap the relocs from the section indicated by SHDR. This 2751 may be either a REL or a RELA section. The relocations are 2752 translated into RELA relocations and stored in INTERNAL_RELOCS, 2753 which should have already been allocated to contain enough space. 2754 The *EXTERNAL_RELOCS_P are a buffer where the external form of the 2755 relocations should be stored. If *EXTERNAL_RELOCS_ADDR is NULL, 2756 *EXTERNAL_RELOCS_ADDR and *EXTERNAL_RELOCS_SIZE returns the mmap 2757 memory address and size. Otherwise, *EXTERNAL_RELOCS_ADDR is 2758 unchanged and *EXTERNAL_RELOCS_SIZE returns 0. 2759 2760 Returns FALSE if something goes wrong. */ 2761 2762 static bool 2763 elf_link_read_relocs_from_section (bfd *abfd, 2764 const asection *sec, 2765 Elf_Internal_Shdr *shdr, 2766 void **external_relocs_addr, 2767 size_t *external_relocs_size, 2768 Elf_Internal_Rela *internal_relocs) 2769 { 2770 const struct elf_backend_data *bed; 2771 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 2772 const bfd_byte *erela; 2773 const bfd_byte *erelaend; 2774 Elf_Internal_Rela *irela; 2775 Elf_Internal_Shdr *symtab_hdr; 2776 size_t nsyms; 2777 void *external_relocs = *external_relocs_addr; 2778 2779 /* Position ourselves at the start of the section. */ 2780 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) 2781 return false; 2782 2783 /* Read the relocations. */ 2784 *external_relocs_size = shdr->sh_size; 2785 if (!_bfd_mmap_read_temporary (&external_relocs, 2786 external_relocs_size, 2787 external_relocs_addr, abfd, true)) 2788 return false; 2789 2790 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2791 nsyms = NUM_SHDR_ENTRIES (symtab_hdr); 2792 2793 bed = get_elf_backend_data (abfd); 2794 2795 /* Convert the external relocations to the internal format. */ 2796 if (shdr->sh_entsize == bed->s->sizeof_rel) 2797 swap_in = bed->s->swap_reloc_in; 2798 else if (shdr->sh_entsize == bed->s->sizeof_rela) 2799 swap_in = bed->s->swap_reloca_in; 2800 else 2801 { 2802 bfd_set_error (bfd_error_wrong_format); 2803 return false; 2804 } 2805 2806 erela = (const bfd_byte *) external_relocs; 2807 /* Setting erelaend like this and comparing with <= handles case of 2808 a fuzzed object with sh_size not a multiple of sh_entsize. */ 2809 erelaend = erela + shdr->sh_size - shdr->sh_entsize; 2810 irela = internal_relocs; 2811 while (erela <= erelaend) 2812 { 2813 bfd_vma r_symndx; 2814 2815 (*swap_in) (abfd, erela, irela); 2816 r_symndx = ELF32_R_SYM (irela->r_info); 2817 if (bed->s->arch_size == 64) 2818 r_symndx >>= 24; 2819 if (nsyms > 0) 2820 { 2821 if ((size_t) r_symndx >= nsyms) 2822 { 2823 _bfd_error_handler 2824 /* xgettext:c-format */ 2825 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" 2826 " for offset %#" PRIx64 " in section `%pA'"), 2827 abfd, (uint64_t) r_symndx, (unsigned long) nsyms, 2828 (uint64_t) irela->r_offset, sec); 2829 bfd_set_error (bfd_error_bad_value); 2830 return false; 2831 } 2832 } 2833 else if (r_symndx != STN_UNDEF) 2834 { 2835 _bfd_error_handler 2836 /* xgettext:c-format */ 2837 (_("%pB: non-zero symbol index (%#" PRIx64 ")" 2838 " for offset %#" PRIx64 " in section `%pA'" 2839 " when the object file has no symbol table"), 2840 abfd, (uint64_t) r_symndx, 2841 (uint64_t) irela->r_offset, sec); 2842 bfd_set_error (bfd_error_bad_value); 2843 return false; 2844 } 2845 irela += bed->s->int_rels_per_ext_rel; 2846 erela += shdr->sh_entsize; 2847 } 2848 2849 return true; 2850 } 2851 2852 /* Read and swap the relocs for a section O. They may have been 2853 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are 2854 not NULL, they are used as buffers to read into. They are known to 2855 be large enough. If the INTERNAL_RELOCS relocs argument is NULL, 2856 the return value is allocated using either malloc or bfd_alloc, 2857 according to the KEEP_MEMORY argument. If O has two relocation 2858 sections (both REL and RELA relocations), then the REL_HDR 2859 relocations will appear first in INTERNAL_RELOCS, followed by the 2860 RELA_HDR relocations. If INFO isn't NULL and KEEP_MEMORY is true, 2861 update cache_size. */ 2862 2863 Elf_Internal_Rela * 2864 _bfd_elf_link_info_read_relocs (bfd *abfd, 2865 struct bfd_link_info *info, 2866 const asection *o, 2867 void *external_relocs, 2868 Elf_Internal_Rela *internal_relocs, 2869 bool keep_memory) 2870 { 2871 void *alloc1 = NULL; 2872 size_t alloc1_size; 2873 Elf_Internal_Rela *alloc2 = NULL; 2874 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2875 struct bfd_elf_section_data *esdo = elf_section_data (o); 2876 Elf_Internal_Rela *internal_rela_relocs; 2877 2878 if (esdo->relocs != NULL) 2879 return esdo->relocs; 2880 2881 if (o->reloc_count == 0) 2882 return NULL; 2883 2884 if (internal_relocs == NULL) 2885 { 2886 bfd_size_type size; 2887 2888 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); 2889 if (keep_memory && info) 2890 info->cache_size += size; 2891 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); 2892 if (internal_relocs == NULL) 2893 return NULL; 2894 } 2895 2896 alloc1 = external_relocs; 2897 internal_rela_relocs = internal_relocs; 2898 if (esdo->rel.hdr) 2899 { 2900 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, 2901 &alloc1, &alloc1_size, 2902 internal_relocs)) 2903 goto error_return; 2904 external_relocs = (((bfd_byte *) external_relocs) 2905 + esdo->rel.hdr->sh_size); 2906 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) 2907 * bed->s->int_rels_per_ext_rel); 2908 } 2909 2910 if (esdo->rela.hdr 2911 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, 2912 &alloc1, &alloc1_size, 2913 internal_rela_relocs))) 2914 goto error_return; 2915 2916 /* Cache the results for next time, if we can. */ 2917 if (keep_memory) 2918 esdo->relocs = internal_relocs; 2919 2920 _bfd_munmap_temporary (alloc1, alloc1_size); 2921 2922 /* Don't free alloc2, since if it was allocated we are passing it 2923 back (under the name of internal_relocs). */ 2924 2925 return internal_relocs; 2926 2927 error_return: 2928 _bfd_munmap_temporary (alloc1, alloc1_size); 2929 free (alloc2); 2930 return NULL; 2931 } 2932 2933 /* This is similar to _bfd_elf_link_info_read_relocs, except for that 2934 NULL is passed to _bfd_elf_link_info_read_relocs for pointer to 2935 struct bfd_link_info. */ 2936 2937 Elf_Internal_Rela * 2938 _bfd_elf_link_read_relocs (bfd *abfd, 2939 const asection *o, 2940 void *external_relocs, 2941 Elf_Internal_Rela *internal_relocs, 2942 bool keep_memory) 2943 { 2944 return _bfd_elf_link_info_read_relocs (abfd, NULL, o, external_relocs, 2945 internal_relocs, keep_memory); 2946 2947 } 2948 2949 /* Compute the size of, and allocate space for, REL_HDR which is the 2950 section header for a section containing relocations for O. */ 2951 2952 static bool 2953 _bfd_elf_link_size_reloc_section (bfd *abfd, 2954 struct bfd_elf_section_reloc_data *reldata) 2955 { 2956 Elf_Internal_Shdr *rel_hdr = reldata->hdr; 2957 2958 /* That allows us to calculate the size of the section. */ 2959 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; 2960 2961 /* The contents field must last into write_object_contents, so we 2962 allocate it with bfd_alloc rather than malloc. Also since we 2963 cannot be sure that the contents will actually be filled in, 2964 we zero the allocated space. */ 2965 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); 2966 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) 2967 return false; 2968 2969 if (reldata->hashes == NULL && reldata->count) 2970 { 2971 struct elf_link_hash_entry **p; 2972 2973 p = ((struct elf_link_hash_entry **) 2974 bfd_zmalloc (reldata->count * sizeof (*p))); 2975 if (p == NULL) 2976 return false; 2977 2978 reldata->hashes = p; 2979 } 2980 2981 return true; 2982 } 2983 2984 /* Copy the relocations indicated by the INTERNAL_RELOCS (which 2985 originated from the section given by INPUT_REL_HDR) to the 2986 OUTPUT_BFD. */ 2987 2988 bool 2989 _bfd_elf_link_output_relocs (bfd *output_bfd, 2990 asection *input_section, 2991 Elf_Internal_Shdr *input_rel_hdr, 2992 Elf_Internal_Rela *internal_relocs, 2993 struct elf_link_hash_entry **rel_hash) 2994 { 2995 Elf_Internal_Rela *irela; 2996 Elf_Internal_Rela *irelaend; 2997 bfd_byte *erel; 2998 struct bfd_elf_section_reloc_data *output_reldata; 2999 asection *output_section; 3000 const struct elf_backend_data *bed; 3001 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 3002 struct bfd_elf_section_data *esdo; 3003 3004 output_section = input_section->output_section; 3005 3006 bed = get_elf_backend_data (output_bfd); 3007 esdo = elf_section_data (output_section); 3008 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) 3009 { 3010 output_reldata = &esdo->rel; 3011 swap_out = bed->s->swap_reloc_out; 3012 } 3013 else if (esdo->rela.hdr 3014 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) 3015 { 3016 output_reldata = &esdo->rela; 3017 swap_out = bed->s->swap_reloca_out; 3018 } 3019 else 3020 { 3021 _bfd_error_handler 3022 /* xgettext:c-format */ 3023 (_("%pB: relocation size mismatch in %pB section %pA"), 3024 output_bfd, input_section->owner, input_section); 3025 bfd_set_error (bfd_error_wrong_format); 3026 return false; 3027 } 3028 3029 erel = output_reldata->hdr->contents; 3030 erel += output_reldata->count * input_rel_hdr->sh_entsize; 3031 irela = internal_relocs; 3032 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) 3033 * bed->s->int_rels_per_ext_rel); 3034 while (irela < irelaend) 3035 { 3036 if (rel_hash && *rel_hash) 3037 (*rel_hash)->has_reloc = 1; 3038 (*swap_out) (output_bfd, irela, erel); 3039 irela += bed->s->int_rels_per_ext_rel; 3040 erel += input_rel_hdr->sh_entsize; 3041 if (rel_hash) 3042 rel_hash++; 3043 } 3044 3045 /* Bump the counter, so that we know where to add the next set of 3046 relocations. */ 3047 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); 3048 3049 return true; 3050 } 3051 3052 /* Make weak undefined symbols in PIE dynamic. */ 3054 3055 bool 3056 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, 3057 struct elf_link_hash_entry *h) 3058 { 3059 if (bfd_link_pie (info) 3060 && h->dynindx == -1 3061 && h->root.type == bfd_link_hash_undefweak) 3062 return bfd_elf_link_record_dynamic_symbol (info, h); 3063 3064 return true; 3065 } 3066 3067 /* Fix up the flags for a symbol. This handles various cases which 3068 can only be fixed after all the input files are seen. This is 3069 currently called by both adjust_dynamic_symbol and 3070 assign_sym_version, which is unnecessary but perhaps more robust in 3071 the face of future changes. */ 3072 3073 static bool 3074 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, 3075 struct elf_info_failed *eif) 3076 { 3077 const struct elf_backend_data *bed; 3078 3079 /* If this symbol was mentioned in a non-ELF file, try to set 3080 DEF_REGULAR and REF_REGULAR correctly. This is the only way to 3081 permit a non-ELF file to correctly refer to a symbol defined in 3082 an ELF dynamic object. */ 3083 if (h->non_elf) 3084 { 3085 while (h->root.type == bfd_link_hash_indirect) 3086 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3087 3088 if (h->root.type != bfd_link_hash_defined 3089 && h->root.type != bfd_link_hash_defweak) 3090 { 3091 h->ref_regular = 1; 3092 h->ref_regular_nonweak = 1; 3093 } 3094 else 3095 { 3096 if (h->root.u.def.section->owner != NULL 3097 && (bfd_get_flavour (h->root.u.def.section->owner) 3098 == bfd_target_elf_flavour)) 3099 { 3100 h->ref_regular = 1; 3101 h->ref_regular_nonweak = 1; 3102 } 3103 else 3104 h->def_regular = 1; 3105 } 3106 3107 if (h->dynindx == -1 3108 && (h->def_dynamic 3109 || h->ref_dynamic)) 3110 { 3111 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 3112 { 3113 eif->failed = true; 3114 return false; 3115 } 3116 } 3117 } 3118 else 3119 { 3120 /* Unfortunately, NON_ELF is only correct if the symbol 3121 was first seen in a non-ELF file. Fortunately, if the symbol 3122 was first seen in an ELF file, we're probably OK unless the 3123 symbol was defined in a non-ELF file. Catch that case here. 3124 FIXME: We're still in trouble if the symbol was first seen in 3125 a dynamic object, and then later in a non-ELF regular object. */ 3126 if ((h->root.type == bfd_link_hash_defined 3127 || h->root.type == bfd_link_hash_defweak) 3128 && !h->def_regular 3129 && (h->root.u.def.section->owner != NULL 3130 ? (bfd_get_flavour (h->root.u.def.section->owner) 3131 != bfd_target_elf_flavour) 3132 : (bfd_is_abs_section (h->root.u.def.section) 3133 && !h->def_dynamic))) 3134 h->def_regular = 1; 3135 } 3136 3137 /* Backend specific symbol fixup. */ 3138 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 3139 if (bed->elf_backend_fixup_symbol 3140 && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) 3141 return false; 3142 3143 /* If this is a final link, and the symbol was defined as a common 3144 symbol in a regular object file, and there was no definition in 3145 any dynamic object, then the linker will have allocated space for 3146 the symbol in a common section but the DEF_REGULAR 3147 flag will not have been set. */ 3148 if (h->root.type == bfd_link_hash_defined 3149 && !h->def_regular 3150 && h->ref_regular 3151 && !h->def_dynamic 3152 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) 3153 h->def_regular = 1; 3154 3155 /* Symbols defined in discarded sections shouldn't be dynamic. */ 3156 if (h->root.type == bfd_link_hash_undefined && h->indx == -3) 3157 (*bed->elf_backend_hide_symbol) (eif->info, h, true); 3158 3159 /* If a weak undefined symbol has non-default visibility, we also 3160 hide it from the dynamic linker. */ 3161 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 3162 && h->root.type == bfd_link_hash_undefweak) 3163 (*bed->elf_backend_hide_symbol) (eif->info, h, true); 3164 3165 /* A hidden versioned symbol in executable should be forced local if 3166 it is is locally defined, not referenced by shared library and not 3167 exported. */ 3168 else if (bfd_link_executable (eif->info) 3169 && h->versioned == versioned_hidden 3170 && !eif->info->export_dynamic 3171 && !h->dynamic 3172 && !h->ref_dynamic 3173 && h->def_regular) 3174 (*bed->elf_backend_hide_symbol) (eif->info, h, true); 3175 3176 /* If -Bsymbolic was used (which means to bind references to global 3177 symbols to the definition within the shared object), and this 3178 symbol was defined in a regular object, then it actually doesn't 3179 need a PLT entry. Likewise, if the symbol has non-default 3180 visibility. If the symbol has hidden or internal visibility, we 3181 will force it local. */ 3182 else if (h->needs_plt 3183 && bfd_link_pic (eif->info) 3184 && is_elf_hash_table (eif->info->hash) 3185 && (SYMBOLIC_BIND (eif->info, h) 3186 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 3187 && h->def_regular) 3188 { 3189 bool force_local; 3190 3191 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 3192 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); 3193 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); 3194 } 3195 3196 /* If this is a weak defined symbol in a dynamic object, and we know 3197 the real definition in the dynamic object, copy interesting flags 3198 over to the real definition. */ 3199 if (h->is_weakalias) 3200 { 3201 struct elf_link_hash_entry *def = weakdef (h); 3202 while (def->root.type == bfd_link_hash_indirect) 3203 def = (struct elf_link_hash_entry *) def->root.u.i.link; 3204 3205 /* If the real definition is defined by a regular object file, 3206 don't do anything special. See the longer description in 3207 _bfd_elf_adjust_dynamic_symbol, below. If the def is not 3208 bfd_link_hash_defined as it was when put on the alias list 3209 then it must have originally been a versioned symbol (for 3210 which a non-versioned indirect symbol is created) and later 3211 a definition for the non-versioned symbol is found. In that 3212 case the indirection is flipped with the versioned symbol 3213 becoming an indirect pointing at the non-versioned symbol. 3214 Thus, not an alias any more. */ 3215 if (def->def_regular 3216 || def->root.type != bfd_link_hash_defined) 3217 { 3218 h = def; 3219 while ((h = h->u.alias) != def) 3220 h->is_weakalias = 0; 3221 } 3222 else 3223 { 3224 while (h->root.type == bfd_link_hash_indirect) 3225 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3226 BFD_ASSERT (h->root.type == bfd_link_hash_defined 3227 || h->root.type == bfd_link_hash_defweak); 3228 BFD_ASSERT (def->def_dynamic); 3229 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); 3230 } 3231 } 3232 3233 return true; 3234 } 3235 3236 /* Make the backend pick a good value for a dynamic symbol. This is 3237 called via elf_link_hash_traverse, and also calls itself 3238 recursively. */ 3239 3240 static bool 3241 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) 3242 { 3243 struct elf_info_failed *eif = (struct elf_info_failed *) data; 3244 struct elf_link_hash_table *htab; 3245 const struct elf_backend_data *bed; 3246 3247 if (! is_elf_hash_table (eif->info->hash)) 3248 return false; 3249 3250 /* Ignore indirect symbols. These are added by the versioning code. */ 3251 if (h->root.type == bfd_link_hash_indirect) 3252 return true; 3253 3254 /* Fix the symbol flags. */ 3255 if (! _bfd_elf_fix_symbol_flags (h, eif)) 3256 return false; 3257 3258 htab = elf_hash_table (eif->info); 3259 bed = get_elf_backend_data (htab->dynobj); 3260 3261 if (h->root.type == bfd_link_hash_undefweak) 3262 { 3263 if (eif->info->dynamic_undefined_weak == 0) 3264 (*bed->elf_backend_hide_symbol) (eif->info, h, true); 3265 else if (eif->info->dynamic_undefined_weak > 0 3266 && h->ref_regular 3267 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 3268 && !bfd_hide_sym_by_version (eif->info->version_info, 3269 h->root.root.string)) 3270 { 3271 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) 3272 { 3273 eif->failed = true; 3274 return false; 3275 } 3276 } 3277 } 3278 3279 /* If this symbol does not require a PLT entry, and it is not 3280 defined by a dynamic object, or is not referenced by a regular 3281 object, ignore it. We do have to handle a weak defined symbol, 3282 even if no regular object refers to it, if we decided to add it 3283 to the dynamic symbol table. FIXME: Do we normally need to worry 3284 about symbols which are defined by one dynamic object and 3285 referenced by another one? */ 3286 if (!h->needs_plt 3287 && h->type != STT_GNU_IFUNC 3288 && (h->def_regular 3289 || !h->def_dynamic 3290 || (!h->ref_regular 3291 && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) 3292 { 3293 h->plt = elf_hash_table (eif->info)->init_plt_offset; 3294 return true; 3295 } 3296 3297 /* If we've already adjusted this symbol, don't do it again. This 3298 can happen via a recursive call. */ 3299 if (h->dynamic_adjusted) 3300 return true; 3301 3302 /* Don't look at this symbol again. Note that we must set this 3303 after checking the above conditions, because we may look at a 3304 symbol once, decide not to do anything, and then get called 3305 recursively later after REF_REGULAR is set below. */ 3306 h->dynamic_adjusted = 1; 3307 3308 /* If this is a weak definition, and we know a real definition, and 3309 the real symbol is not itself defined by a regular object file, 3310 then get a good value for the real definition. We handle the 3311 real symbol first, for the convenience of the backend routine. 3312 3313 Note that there is a confusing case here. If the real definition 3314 is defined by a regular object file, we don't get the real symbol 3315 from the dynamic object, but we do get the weak symbol. If the 3316 processor backend uses a COPY reloc, then if some routine in the 3317 dynamic object changes the real symbol, we will not see that 3318 change in the corresponding weak symbol. This is the way other 3319 ELF linkers work as well, and seems to be a result of the shared 3320 library model. 3321 3322 I will clarify this issue. Most SVR4 shared libraries define the 3323 variable _timezone and define timezone as a weak synonym. The 3324 tzset call changes _timezone. If you write 3325 extern int timezone; 3326 int _timezone = 5; 3327 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } 3328 you might expect that, since timezone is a synonym for _timezone, 3329 the same number will print both times. However, if the processor 3330 backend uses a COPY reloc, then actually timezone will be copied 3331 into your process image, and, since you define _timezone 3332 yourself, _timezone will not. Thus timezone and _timezone will 3333 wind up at different memory locations. The tzset call will set 3334 _timezone, leaving timezone unchanged. */ 3335 3336 if (h->is_weakalias) 3337 { 3338 struct elf_link_hash_entry *def = weakdef (h); 3339 3340 /* If we get to this point, there is an implicit reference to 3341 the alias by a regular object file via the weak symbol H. */ 3342 def->ref_regular = 1; 3343 3344 /* Ensure that the backend adjust_dynamic_symbol function sees 3345 the strong alias before H by recursively calling ourselves. */ 3346 if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) 3347 return false; 3348 } 3349 3350 /* If a symbol has no type and no size and does not require a PLT 3351 entry, then we are probably about to do the wrong thing here: we 3352 are probably going to create a COPY reloc for an empty object. 3353 This case can arise when a shared object is built with assembly 3354 code, and the assembly code fails to set the symbol type. */ 3355 if (h->size == 0 3356 && h->type == STT_NOTYPE 3357 && !h->needs_plt) 3358 _bfd_error_handler 3359 (_("warning: type and size of dynamic symbol `%s' are not defined"), 3360 h->root.root.string); 3361 3362 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) 3363 { 3364 eif->failed = true; 3365 return false; 3366 } 3367 3368 return true; 3369 } 3370 3371 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, 3372 DYNBSS. */ 3373 3374 bool 3375 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, 3376 struct elf_link_hash_entry *h, 3377 asection *dynbss) 3378 { 3379 unsigned int power_of_two; 3380 bfd_vma mask; 3381 asection *sec = h->root.u.def.section; 3382 3383 /* The section alignment of the definition is the maximum alignment 3384 requirement of symbols defined in the section. Since we don't 3385 know the symbol alignment requirement, we start with the 3386 maximum alignment and check low bits of the symbol address 3387 for the minimum alignment. */ 3388 power_of_two = bfd_section_alignment (sec); 3389 mask = ((bfd_vma) 1 << power_of_two) - 1; 3390 while ((h->root.u.def.value & mask) != 0) 3391 { 3392 mask >>= 1; 3393 --power_of_two; 3394 } 3395 3396 /* Adjust the section alignment if needed. */ 3397 if (!bfd_link_align_section (dynbss, power_of_two)) 3398 return false; 3399 3400 /* We make sure that the symbol will be aligned properly. */ 3401 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); 3402 3403 /* Define the symbol as being at this point in DYNBSS. */ 3404 h->root.u.def.section = dynbss; 3405 h->root.u.def.value = dynbss->size; 3406 3407 /* Increment the size of DYNBSS to make room for the symbol. */ 3408 dynbss->size += h->size; 3409 3410 /* No error if extern_protected_data is true. */ 3411 if (h->protected_def 3412 && (!info->extern_protected_data 3413 || (info->extern_protected_data < 0 3414 && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) 3415 info->callbacks->einfo 3416 (_("%P: copy reloc against protected `%pT' is dangerous\n"), 3417 h->root.root.string); 3418 3419 return true; 3420 } 3421 3422 /* Adjust all external symbols pointing into SEC_MERGE sections 3423 to reflect the object merging within the sections. */ 3424 3425 static bool 3426 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) 3427 { 3428 asection *sec; 3429 3430 if ((h->root.type == bfd_link_hash_defined 3431 || h->root.type == bfd_link_hash_defweak) 3432 && ((sec = h->root.u.def.section)->flags & SEC_MERGE) 3433 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 3434 { 3435 bfd *output_bfd = (bfd *) data; 3436 3437 h->root.u.def.value = 3438 _bfd_merged_section_offset (output_bfd, 3439 &h->root.u.def.section, 3440 elf_section_data (sec)->sec_info, 3441 h->root.u.def.value); 3442 } 3443 3444 return true; 3445 } 3446 3447 /* Returns false if the symbol referred to by H should be considered 3448 to resolve local to the current module, and true if it should be 3449 considered to bind dynamically. */ 3450 3451 bool 3452 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, 3453 struct bfd_link_info *info, 3454 bool not_local_protected) 3455 { 3456 bool binding_stays_local_p; 3457 const struct elf_backend_data *bed; 3458 struct elf_link_hash_table *hash_table; 3459 3460 if (h == NULL) 3461 return false; 3462 3463 while (h->root.type == bfd_link_hash_indirect 3464 || h->root.type == bfd_link_hash_warning) 3465 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3466 3467 /* If it was forced local, then clearly it's not dynamic. */ 3468 if (h->dynindx == -1) 3469 return false; 3470 if (h->forced_local) 3471 return false; 3472 3473 /* Identify the cases where name binding rules say that a 3474 visible symbol resolves locally. */ 3475 binding_stays_local_p = (bfd_link_executable (info) 3476 || SYMBOLIC_BIND (info, h)); 3477 3478 switch (ELF_ST_VISIBILITY (h->other)) 3479 { 3480 case STV_INTERNAL: 3481 case STV_HIDDEN: 3482 return false; 3483 3484 case STV_PROTECTED: 3485 hash_table = elf_hash_table (info); 3486 if (!is_elf_hash_table (&hash_table->root)) 3487 return false; 3488 3489 bed = get_elf_backend_data (hash_table->dynobj); 3490 3491 /* Proper resolution for function pointer equality may require 3492 that these symbols perhaps be resolved dynamically, even though 3493 we should be resolving them to the current module. */ 3494 if (!not_local_protected || !bed->is_function_type (h->type)) 3495 binding_stays_local_p = true; 3496 break; 3497 3498 default: 3499 break; 3500 } 3501 3502 /* If it isn't defined locally, then clearly it's dynamic. */ 3503 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 3504 return true; 3505 3506 /* Otherwise, the symbol is dynamic if binding rules don't tell 3507 us that it remains local. */ 3508 return !binding_stays_local_p; 3509 } 3510 3511 /* Return true if the symbol referred to by H should be considered 3512 to resolve local to the current module, and false otherwise. Differs 3513 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of 3514 undefined symbols. The two functions are virtually identical except 3515 for the place where dynindx == -1 is tested. If that test is true, 3516 _bfd_elf_dynamic_symbol_p will say the symbol is local, while 3517 _bfd_elf_symbol_refs_local_p will say the symbol is local only for 3518 defined symbols. 3519 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as 3520 !_bfd_elf_symbol_refs_local_p, except that targets differ in their 3521 treatment of undefined weak symbols. For those that do not make 3522 undefined weak symbols dynamic, both functions may return false. */ 3523 3524 bool 3525 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, 3526 struct bfd_link_info *info, 3527 bool local_protected) 3528 { 3529 const struct elf_backend_data *bed; 3530 struct elf_link_hash_table *hash_table; 3531 3532 /* If it's a local sym, of course we resolve locally. */ 3533 if (h == NULL) 3534 return true; 3535 3536 /* STV_HIDDEN or STV_INTERNAL ones must be local. */ 3537 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 3538 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 3539 return true; 3540 3541 /* Forced local symbols resolve locally. */ 3542 if (h->forced_local) 3543 return true; 3544 3545 /* Common symbols that become definitions don't get the DEF_REGULAR 3546 flag set, so test it first, and don't bail out. */ 3547 if (ELF_COMMON_DEF_P (h)) 3548 /* Do nothing. */; 3549 /* If we don't have a definition in a regular file, then we can't 3550 resolve locally. The sym is either undefined or dynamic. */ 3551 else if (!h->def_regular) 3552 return false; 3553 3554 /* Non-dynamic symbols resolve locally. */ 3555 if (h->dynindx == -1) 3556 return true; 3557 3558 /* At this point, we know the symbol is defined and dynamic. In an 3559 executable it must resolve locally, likewise when building symbolic 3560 shared libraries. */ 3561 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) 3562 return true; 3563 3564 /* Now deal with defined dynamic symbols in shared libraries. Ones 3565 with default visibility might not resolve locally. */ 3566 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 3567 return false; 3568 3569 hash_table = elf_hash_table (info); 3570 if (!is_elf_hash_table (&hash_table->root)) 3571 return true; 3572 3573 /* STV_PROTECTED symbols with indirect external access are local. */ 3574 if (info->indirect_extern_access > 0) 3575 return true; 3576 3577 bed = get_elf_backend_data (hash_table->dynobj); 3578 3579 /* If extern_protected_data is false, STV_PROTECTED non-function 3580 symbols are local. */ 3581 if ((!info->extern_protected_data 3582 || (info->extern_protected_data < 0 3583 && !bed->extern_protected_data)) 3584 && !bed->is_function_type (h->type)) 3585 return true; 3586 3587 /* Function pointer equality tests may require that STV_PROTECTED 3588 symbols be treated as dynamic symbols. If the address of a 3589 function not defined in an executable is set to that function's 3590 plt entry in the executable, then the address of the function in 3591 a shared library must also be the plt entry in the executable. */ 3592 return local_protected; 3593 } 3594 3595 /* Caches some TLS segment info, and ensures that the TLS segment vma is 3596 aligned. Returns the first TLS output section. */ 3597 3598 struct bfd_section * 3599 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) 3600 { 3601 struct bfd_section *sec, *tls; 3602 unsigned int align = 0; 3603 3604 for (sec = obfd->sections; sec != NULL; sec = sec->next) 3605 if ((sec->flags & SEC_THREAD_LOCAL) != 0) 3606 break; 3607 tls = sec; 3608 3609 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) 3610 if (sec->alignment_power > align) 3611 align = sec->alignment_power; 3612 3613 elf_hash_table (info)->tls_sec = tls; 3614 3615 /* Ensure the alignment of the first section (usually .tdata) is the largest 3616 alignment, so that the tls segment starts aligned. */ 3617 if (tls != NULL) 3618 (void) bfd_link_align_section (tls, align); 3619 3620 return tls; 3621 } 3622 3623 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ 3624 static bool 3625 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, 3626 Elf_Internal_Sym *sym) 3627 { 3628 const struct elf_backend_data *bed; 3629 3630 /* Local symbols do not count, but target specific ones might. */ 3631 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL 3632 && ELF_ST_BIND (sym->st_info) < STB_LOOS) 3633 return false; 3634 3635 bed = get_elf_backend_data (abfd); 3636 /* Function symbols do not count. */ 3637 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) 3638 return false; 3639 3640 /* If the section is undefined, then so is the symbol. */ 3641 if (sym->st_shndx == SHN_UNDEF) 3642 return false; 3643 3644 /* If the symbol is defined in the common section, then 3645 it is a common definition and so does not count. */ 3646 if (bed->common_definition (sym)) 3647 return false; 3648 3649 /* If the symbol is in a target specific section then we 3650 must rely upon the backend to tell us what it is. */ 3651 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) 3652 /* FIXME - this function is not coded yet: 3653 3654 return _bfd_is_global_symbol_definition (abfd, sym); 3655 3656 Instead for now assume that the definition is not global, 3657 Even if this is wrong, at least the linker will behave 3658 in the same way that it used to do. */ 3659 return false; 3660 3661 return true; 3662 } 3663 3664 /* Search the symbol table of the archive element of the archive ABFD 3665 whose archive map contains a mention of SYMDEF, and determine if 3666 the symbol is defined in this element. */ 3667 static bool 3668 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) 3669 { 3670 Elf_Internal_Shdr * hdr; 3671 size_t symcount; 3672 size_t extsymcount; 3673 size_t extsymoff; 3674 Elf_Internal_Sym *isymbuf; 3675 Elf_Internal_Sym *isym; 3676 Elf_Internal_Sym *isymend; 3677 bool result; 3678 3679 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL); 3680 if (abfd == NULL) 3681 return false; 3682 3683 if (! bfd_check_format (abfd, bfd_object)) 3684 return false; 3685 3686 /* Select the appropriate symbol table. If we don't know if the 3687 object file is an IR object, give linker LTO plugin a chance to 3688 get the correct symbol table. */ 3689 if (abfd->plugin_format == bfd_plugin_yes 3690 || abfd->plugin_format == bfd_plugin_yes_unused 3691 #if BFD_SUPPORTS_PLUGINS 3692 || (abfd->plugin_format == bfd_plugin_unknown 3693 && bfd_link_plugin_object_p (abfd)) 3694 #endif 3695 ) 3696 { 3697 /* Use the IR symbol table if the object has been claimed by 3698 plugin. */ 3699 abfd = abfd->plugin_dummy_bfd; 3700 hdr = &elf_tdata (abfd)->symtab_hdr; 3701 } 3702 else 3703 { 3704 if (elf_use_dt_symtab_p (abfd)) 3705 { 3706 bfd_set_error (bfd_error_wrong_format); 3707 return false; 3708 } 3709 3710 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) 3711 hdr = &elf_tdata (abfd)->symtab_hdr; 3712 else 3713 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3714 } 3715 3716 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 3717 3718 /* The sh_info field of the symtab header tells us where the 3719 external symbols start. We don't care about the local symbols. */ 3720 if (elf_bad_symtab (abfd)) 3721 { 3722 extsymcount = symcount; 3723 extsymoff = 0; 3724 } 3725 else 3726 { 3727 extsymcount = symcount - hdr->sh_info; 3728 extsymoff = hdr->sh_info; 3729 } 3730 3731 if (extsymcount == 0) 3732 return false; 3733 3734 /* Read in the symbol table. */ 3735 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3736 NULL, NULL, NULL); 3737 if (isymbuf == NULL) 3738 return false; 3739 3740 /* Scan the symbol table looking for SYMDEF. */ 3741 result = false; 3742 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) 3743 { 3744 const char *name; 3745 3746 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3747 isym->st_name); 3748 if (name == NULL) 3749 break; 3750 3751 if (strcmp (name, symdef->name) == 0) 3752 { 3753 result = is_global_data_symbol_definition (abfd, isym); 3754 break; 3755 } 3756 } 3757 3758 free (isymbuf); 3759 3760 return result; 3761 } 3762 3763 /* Add an entry to the .dynamic table. */ 3765 3766 bool 3767 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, 3768 bfd_vma tag, 3769 bfd_vma val) 3770 { 3771 struct elf_link_hash_table *hash_table; 3772 const struct elf_backend_data *bed; 3773 asection *s; 3774 bfd_size_type newsize; 3775 bfd_byte *newcontents; 3776 Elf_Internal_Dyn dyn; 3777 3778 hash_table = elf_hash_table (info); 3779 if (! is_elf_hash_table (&hash_table->root)) 3780 return false; 3781 3782 if (tag == DT_RELA || tag == DT_REL) 3783 hash_table->dynamic_relocs = true; 3784 3785 bed = get_elf_backend_data (hash_table->dynobj); 3786 s = hash_table->dynamic; 3787 BFD_ASSERT (s != NULL); 3788 3789 newsize = s->size + bed->s->sizeof_dyn; 3790 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); 3791 if (newcontents == NULL) 3792 return false; 3793 3794 dyn.d_tag = tag; 3795 dyn.d_un.d_val = val; 3796 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); 3797 3798 s->size = newsize; 3799 s->contents = newcontents; 3800 3801 return true; 3802 } 3803 3804 /* Strip zero-sized dynamic sections. */ 3805 3806 bool 3807 _bfd_elf_strip_zero_sized_dynamic_sections (struct bfd_link_info *info) 3808 { 3809 struct elf_link_hash_table *hash_table; 3810 const struct elf_backend_data *bed; 3811 asection *s, *sdynamic, **pp; 3812 asection *rela_dyn, *rel_dyn; 3813 Elf_Internal_Dyn dyn; 3814 bfd_byte *extdyn, *next; 3815 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 3816 bool strip_zero_sized; 3817 bool strip_zero_sized_plt; 3818 3819 if (bfd_link_relocatable (info)) 3820 return true; 3821 3822 hash_table = elf_hash_table (info); 3823 if (!is_elf_hash_table (&hash_table->root)) 3824 return false; 3825 3826 if (!hash_table->dynobj) 3827 return true; 3828 3829 sdynamic= hash_table->dynamic; 3830 if (!sdynamic) 3831 return true; 3832 3833 bed = get_elf_backend_data (hash_table->dynobj); 3834 swap_dyn_in = bed->s->swap_dyn_in; 3835 3836 strip_zero_sized = false; 3837 strip_zero_sized_plt = false; 3838 3839 /* Strip zero-sized dynamic sections. */ 3840 rela_dyn = bfd_get_section_by_name (info->output_bfd, ".rela.dyn"); 3841 rel_dyn = bfd_get_section_by_name (info->output_bfd, ".rel.dyn"); 3842 for (pp = &info->output_bfd->sections; (s = *pp) != NULL;) 3843 if (s->size == 0 3844 && (s == rela_dyn 3845 || s == rel_dyn 3846 || s == hash_table->srelplt->output_section 3847 || s == hash_table->splt->output_section)) 3848 { 3849 *pp = s->next; 3850 info->output_bfd->section_count--; 3851 strip_zero_sized = true; 3852 if (s == rela_dyn) 3853 s = rela_dyn; 3854 if (s == rel_dyn) 3855 s = rel_dyn; 3856 else if (s == hash_table->splt->output_section) 3857 { 3858 s = hash_table->splt; 3859 strip_zero_sized_plt = true; 3860 } 3861 else 3862 s = hash_table->srelplt; 3863 s->flags |= SEC_EXCLUDE; 3864 s->output_section = bfd_abs_section_ptr; 3865 } 3866 else 3867 pp = &s->next; 3868 3869 if (strip_zero_sized_plt && sdynamic->size != 0) 3870 for (extdyn = sdynamic->contents; 3871 extdyn < sdynamic->contents + sdynamic->size; 3872 extdyn = next) 3873 { 3874 next = extdyn + bed->s->sizeof_dyn; 3875 swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 3876 switch (dyn.d_tag) 3877 { 3878 default: 3879 break; 3880 case DT_JMPREL: 3881 case DT_PLTRELSZ: 3882 case DT_PLTREL: 3883 /* Strip DT_PLTRELSZ, DT_JMPREL and DT_PLTREL entries if 3884 the procedure linkage table (the .plt section) has been 3885 removed. */ 3886 memmove (extdyn, next, 3887 sdynamic->size - (next - sdynamic->contents)); 3888 next = extdyn; 3889 } 3890 } 3891 3892 if (strip_zero_sized) 3893 { 3894 /* Regenerate program headers. */ 3895 elf_seg_map (info->output_bfd) = NULL; 3896 return _bfd_elf_map_sections_to_segments (info->output_bfd, info, 3897 NULL); 3898 } 3899 3900 return true; 3901 } 3902 3903 /* Add a DT_NEEDED entry for this dynamic object. Returns -1 on error, 3904 1 if a DT_NEEDED tag already exists, and 0 on success. */ 3905 3906 int 3907 bfd_elf_add_dt_needed_tag (bfd *abfd, struct bfd_link_info *info) 3908 { 3909 struct elf_link_hash_table *hash_table; 3910 size_t strindex; 3911 const char *soname; 3912 3913 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 3914 return -1; 3915 3916 hash_table = elf_hash_table (info); 3917 soname = elf_dt_name (abfd); 3918 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, false); 3919 if (strindex == (size_t) -1) 3920 return -1; 3921 3922 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) 3923 { 3924 asection *sdyn; 3925 const struct elf_backend_data *bed; 3926 bfd_byte *extdyn; 3927 3928 bed = get_elf_backend_data (hash_table->dynobj); 3929 sdyn = hash_table->dynamic; 3930 if (sdyn != NULL && sdyn->size != 0) 3931 for (extdyn = sdyn->contents; 3932 extdyn < sdyn->contents + sdyn->size; 3933 extdyn += bed->s->sizeof_dyn) 3934 { 3935 Elf_Internal_Dyn dyn; 3936 3937 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 3938 if (dyn.d_tag == DT_NEEDED 3939 && dyn.d_un.d_val == strindex) 3940 { 3941 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3942 return 1; 3943 } 3944 } 3945 } 3946 3947 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) 3948 return -1; 3949 3950 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) 3951 return -1; 3952 3953 return 0; 3954 } 3955 3956 /* Return true if SONAME is on the needed list between NEEDED and STOP 3957 (or the end of list if STOP is NULL), and needed by a library that 3958 will be loaded. */ 3959 3960 static bool 3961 on_needed_list (const char *soname, 3962 struct bfd_link_needed_list *needed, 3963 struct bfd_link_needed_list *stop) 3964 { 3965 struct bfd_link_needed_list *look; 3966 for (look = needed; look != stop; look = look->next) 3967 if (strcmp (soname, look->name) == 0 3968 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 3969 /* If needed by a library that itself is not directly 3970 needed, recursively check whether that library is 3971 indirectly needed. Since we add DT_NEEDED entries to 3972 the end of the list, library dependencies appear after 3973 the library. Therefore search prior to the current 3974 LOOK, preventing possible infinite recursion. */ 3975 || on_needed_list (elf_dt_name (look->by), needed, look))) 3976 return true; 3977 3978 return false; 3979 } 3980 3981 /* Sort symbol by value, section, size, and type. */ 3982 static int 3983 elf_sort_symbol (const void *arg1, const void *arg2) 3984 { 3985 const struct elf_link_hash_entry *h1; 3986 const struct elf_link_hash_entry *h2; 3987 bfd_signed_vma vdiff; 3988 int sdiff; 3989 const char *n1; 3990 const char *n2; 3991 3992 h1 = *(const struct elf_link_hash_entry **) arg1; 3993 h2 = *(const struct elf_link_hash_entry **) arg2; 3994 vdiff = h1->root.u.def.value - h2->root.u.def.value; 3995 if (vdiff != 0) 3996 return vdiff > 0 ? 1 : -1; 3997 3998 sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; 3999 if (sdiff != 0) 4000 return sdiff; 4001 4002 /* Sort so that sized symbols are selected over zero size symbols. */ 4003 vdiff = h1->size - h2->size; 4004 if (vdiff != 0) 4005 return vdiff > 0 ? 1 : -1; 4006 4007 /* Sort so that STT_OBJECT is selected over STT_NOTYPE. */ 4008 if (h1->type != h2->type) 4009 return h1->type - h2->type; 4010 4011 /* If symbols are properly sized and typed, and multiple strong 4012 aliases are not defined in a shared library by the user we 4013 shouldn't get here. Unfortunately linker script symbols like 4014 __bss_start sometimes match a user symbol defined at the start of 4015 .bss without proper size and type. We'd like to preference the 4016 user symbol over reserved system symbols. Sort on leading 4017 underscores. */ 4018 n1 = h1->root.root.string; 4019 n2 = h2->root.root.string; 4020 while (*n1 == *n2) 4021 { 4022 if (*n1 == 0) 4023 break; 4024 ++n1; 4025 ++n2; 4026 } 4027 if (*n1 == '_') 4028 return -1; 4029 if (*n2 == '_') 4030 return 1; 4031 4032 /* Final sort on name selects user symbols like '_u' over reserved 4033 system symbols like '_Z' and also will avoid qsort instability. */ 4034 return *n1 - *n2; 4035 } 4036 4037 /* This function is used to adjust offsets into .dynstr for 4038 dynamic symbols. This is called via elf_link_hash_traverse. */ 4039 4040 static bool 4041 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) 4042 { 4043 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; 4044 4045 if (h->dynindx != -1) 4046 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); 4047 return true; 4048 } 4049 4050 /* Assign string offsets in .dynstr, update all structures referencing 4051 them. */ 4052 4053 static bool 4054 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) 4055 { 4056 struct elf_link_hash_table *hash_table = elf_hash_table (info); 4057 struct elf_link_local_dynamic_entry *entry; 4058 struct elf_strtab_hash *dynstr = hash_table->dynstr; 4059 bfd *dynobj = hash_table->dynobj; 4060 asection *sdyn; 4061 bfd_size_type size; 4062 const struct elf_backend_data *bed; 4063 bfd_byte *extdyn; 4064 4065 _bfd_elf_strtab_finalize (dynstr); 4066 size = _bfd_elf_strtab_size (dynstr); 4067 4068 /* Allow the linker to examine the dynsymtab now it's fully populated. */ 4069 4070 if (info->callbacks->examine_strtab) 4071 info->callbacks->examine_strtab (dynstr); 4072 4073 bed = get_elf_backend_data (dynobj); 4074 sdyn = hash_table->dynamic; 4075 BFD_ASSERT (sdyn != NULL); 4076 4077 /* Update all .dynamic entries referencing .dynstr strings. */ 4078 for (extdyn = sdyn->contents; 4079 extdyn < PTR_ADD (sdyn->contents, sdyn->size); 4080 extdyn += bed->s->sizeof_dyn) 4081 { 4082 Elf_Internal_Dyn dyn; 4083 4084 bed->s->swap_dyn_in (dynobj, extdyn, &dyn); 4085 switch (dyn.d_tag) 4086 { 4087 case DT_STRSZ: 4088 dyn.d_un.d_val = size; 4089 break; 4090 case DT_NEEDED: 4091 case DT_SONAME: 4092 case DT_RPATH: 4093 case DT_RUNPATH: 4094 case DT_FILTER: 4095 case DT_AUXILIARY: 4096 case DT_AUDIT: 4097 case DT_DEPAUDIT: 4098 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); 4099 break; 4100 default: 4101 continue; 4102 } 4103 bed->s->swap_dyn_out (dynobj, &dyn, extdyn); 4104 } 4105 4106 /* Now update local dynamic symbols. */ 4107 for (entry = hash_table->dynlocal; entry ; entry = entry->next) 4108 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, 4109 entry->isym.st_name); 4110 4111 /* And the rest of dynamic symbols. */ 4112 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); 4113 4114 /* Adjust version definitions. */ 4115 if (elf_tdata (output_bfd)->cverdefs) 4116 { 4117 asection *s; 4118 bfd_byte *p; 4119 size_t i; 4120 Elf_Internal_Verdef def; 4121 Elf_Internal_Verdaux defaux; 4122 4123 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 4124 p = s->contents; 4125 do 4126 { 4127 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, 4128 &def); 4129 p += sizeof (Elf_External_Verdef); 4130 if (def.vd_aux != sizeof (Elf_External_Verdef)) 4131 continue; 4132 for (i = 0; i < def.vd_cnt; ++i) 4133 { 4134 _bfd_elf_swap_verdaux_in (output_bfd, 4135 (Elf_External_Verdaux *) p, &defaux); 4136 defaux.vda_name = _bfd_elf_strtab_offset (dynstr, 4137 defaux.vda_name); 4138 _bfd_elf_swap_verdaux_out (output_bfd, 4139 &defaux, (Elf_External_Verdaux *) p); 4140 p += sizeof (Elf_External_Verdaux); 4141 } 4142 } 4143 while (def.vd_next); 4144 } 4145 4146 /* Adjust version references. */ 4147 if (elf_tdata (output_bfd)->verref) 4148 { 4149 asection *s; 4150 bfd_byte *p; 4151 size_t i; 4152 Elf_Internal_Verneed need; 4153 Elf_Internal_Vernaux needaux; 4154 4155 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 4156 p = s->contents; 4157 do 4158 { 4159 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, 4160 &need); 4161 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); 4162 _bfd_elf_swap_verneed_out (output_bfd, &need, 4163 (Elf_External_Verneed *) p); 4164 p += sizeof (Elf_External_Verneed); 4165 for (i = 0; i < need.vn_cnt; ++i) 4166 { 4167 _bfd_elf_swap_vernaux_in (output_bfd, 4168 (Elf_External_Vernaux *) p, &needaux); 4169 needaux.vna_name = _bfd_elf_strtab_offset (dynstr, 4170 needaux.vna_name); 4171 _bfd_elf_swap_vernaux_out (output_bfd, 4172 &needaux, 4173 (Elf_External_Vernaux *) p); 4174 p += sizeof (Elf_External_Vernaux); 4175 } 4176 } 4177 while (need.vn_next); 4178 } 4179 4180 return true; 4181 } 4182 4183 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 4185 The default is to only match when the INPUT and OUTPUT are exactly 4186 the same target. */ 4187 4188 bool 4189 _bfd_elf_default_relocs_compatible (const bfd_target *input, 4190 const bfd_target *output) 4191 { 4192 return input == output; 4193 } 4194 4195 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 4196 This version is used when different targets for the same architecture 4197 are virtually identical. */ 4198 4199 bool 4200 _bfd_elf_relocs_compatible (const bfd_target *input, 4201 const bfd_target *output) 4202 { 4203 const struct elf_backend_data *obed, *ibed; 4204 4205 if (input == output) 4206 return true; 4207 4208 ibed = xvec_get_elf_backend_data (input); 4209 obed = xvec_get_elf_backend_data (output); 4210 4211 if (ibed->arch != obed->arch) 4212 return false; 4213 4214 /* If both backends are using this function, deem them compatible. */ 4215 return ibed->relocs_compatible == obed->relocs_compatible; 4216 } 4217 4218 /* Make a special call to the linker "notice" function to tell it that 4219 we are about to handle an as-needed lib, or have finished 4220 processing the lib. */ 4221 4222 bool 4223 _bfd_elf_notice_as_needed (bfd *ibfd, 4224 struct bfd_link_info *info, 4225 enum notice_asneeded_action act) 4226 { 4227 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); 4228 } 4229 4230 /* Call ACTION on each relocation in an ELF object file. */ 4231 4232 bool 4233 _bfd_elf_link_iterate_on_relocs 4234 (bfd *abfd, struct bfd_link_info *info, 4235 bool (*action) (bfd *, struct bfd_link_info *, asection *, 4236 const Elf_Internal_Rela *)) 4237 { 4238 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4239 struct elf_link_hash_table *htab = elf_hash_table (info); 4240 4241 /* If this object is the same format as the output object, and it is 4242 not a shared library, then let the backend look through the 4243 relocs. 4244 4245 This is required to build global offset table entries and to 4246 arrange for dynamic relocs. It is not required for the 4247 particular common case of linking non PIC code, even when linking 4248 against shared libraries, but unfortunately there is no way of 4249 knowing whether an object file has been compiled PIC or not. 4250 Looking through the relocs is not particularly time consuming. 4251 The problem is that we must either (1) keep the relocs in memory, 4252 which causes the linker to require additional runtime memory or 4253 (2) read the relocs twice from the input file, which wastes time. 4254 This would be a good case for using mmap. 4255 4256 I have no idea how to handle linking PIC code into a file of a 4257 different format. It probably can't be done. */ 4258 if ((abfd->flags & DYNAMIC) == 0 4259 && is_elf_hash_table (&htab->root) 4260 && elf_object_id (abfd) == elf_hash_table_id (htab) 4261 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 4262 { 4263 asection *o; 4264 4265 for (o = abfd->sections; o != NULL; o = o->next) 4266 { 4267 Elf_Internal_Rela *internal_relocs; 4268 bool ok; 4269 4270 /* Don't check relocations in excluded sections. Don't do 4271 anything special with non-loaded, non-alloced sections. 4272 In particular, any relocs in such sections should not 4273 affect GOT and PLT reference counting (ie. we don't 4274 allow them to create GOT or PLT entries), there's no 4275 possibility or desire to optimize TLS relocs, and 4276 there's not much point in propagating relocs to shared 4277 libs that the dynamic linker won't relocate. */ 4278 if ((o->flags & SEC_ALLOC) == 0 4279 || (o->flags & SEC_RELOC) == 0 4280 || (o->flags & SEC_EXCLUDE) != 0 4281 || o->reloc_count == 0 4282 || ((info->strip == strip_all || info->strip == strip_debugger) 4283 && (o->flags & SEC_DEBUGGING) != 0) 4284 || bfd_is_abs_section (o->output_section)) 4285 continue; 4286 4287 internal_relocs = _bfd_elf_link_info_read_relocs 4288 (abfd, info, o, NULL, NULL, 4289 _bfd_elf_link_keep_memory (info)); 4290 if (internal_relocs == NULL) 4291 return false; 4292 4293 ok = action (abfd, info, o, internal_relocs); 4294 4295 if (elf_section_data (o)->relocs != internal_relocs) 4296 free (internal_relocs); 4297 4298 if (! ok) 4299 return false; 4300 } 4301 } 4302 4303 return true; 4304 } 4305 4306 /* Check relocations in an ELF object file. This is called after 4307 all input files have been opened. */ 4308 4309 bool 4310 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) 4311 { 4312 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 4313 if (bed->check_relocs != NULL) 4314 return _bfd_elf_link_iterate_on_relocs (abfd, info, 4315 bed->check_relocs); 4316 return true; 4317 } 4318 4319 /* An entry in the first definition hash table. */ 4320 4321 struct elf_link_first_hash_entry 4322 { 4323 struct bfd_hash_entry root; 4324 /* The object of the first definition. */ 4325 bfd *abfd; 4326 }; 4327 4328 /* The function to create a new entry in the first definition hash 4329 table. */ 4330 4331 static struct bfd_hash_entry * 4332 elf_link_first_hash_newfunc (struct bfd_hash_entry *entry, 4333 struct bfd_hash_table *table, 4334 const char *string) 4335 { 4336 struct elf_link_first_hash_entry *ret = 4337 (struct elf_link_first_hash_entry *) entry; 4338 4339 /* Allocate the structure if it has not already been allocated by a 4340 subclass. */ 4341 if (ret == NULL) 4342 ret = (struct elf_link_first_hash_entry *) 4343 bfd_hash_allocate (table, 4344 sizeof (struct elf_link_first_hash_entry)); 4345 if (ret == NULL) 4346 return NULL; 4347 4348 /* Call the allocation method of the superclass. */ 4349 ret = ((struct elf_link_first_hash_entry *) 4350 bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, 4351 string)); 4352 if (ret != NULL) 4353 ret->abfd = NULL; 4354 4355 return (struct bfd_hash_entry *) ret; 4356 } 4357 4358 /* Add the symbol NAME from ABFD to first hash. */ 4359 4360 static void 4361 elf_link_add_to_first_hash (bfd *abfd, struct bfd_link_info *info, 4362 const char *name, bool copy) 4363 { 4364 struct elf_link_hash_table *htab = elf_hash_table (info); 4365 /* Skip if there is no first hash. */ 4366 if (htab->first_hash == NULL) 4367 return; 4368 4369 struct elf_link_first_hash_entry *e 4370 = ((struct elf_link_first_hash_entry *) 4371 bfd_hash_lookup (htab->first_hash, name, true, copy)); 4372 if (e == NULL) 4373 info->callbacks->fatal 4374 (_("%P: %pB: failed to add %s to first hash\n"), abfd, name); 4375 4376 if (e->abfd == NULL) 4377 /* Store ABFD in abfd. */ 4378 e->abfd = abfd; 4379 } 4380 4381 /* Add symbols from an ELF object file to the linker hash table. */ 4382 4383 static bool 4384 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 4385 { 4386 Elf_Internal_Ehdr *ehdr; 4387 Elf_Internal_Shdr *hdr; 4388 size_t symcount; 4389 size_t extsymcount; 4390 size_t extsymoff; 4391 struct elf_link_hash_entry **sym_hash; 4392 bool dynamic; 4393 Elf_External_Versym *extversym = NULL; 4394 Elf_External_Versym *extversym_end = NULL; 4395 Elf_External_Versym *ever; 4396 struct elf_link_hash_entry *weaks; 4397 struct elf_link_hash_entry **nondeflt_vers = NULL; 4398 size_t nondeflt_vers_cnt = 0; 4399 Elf_Internal_Sym *isymbuf = NULL; 4400 Elf_Internal_Sym *isym; 4401 Elf_Internal_Sym *isymend; 4402 const struct elf_backend_data *bed; 4403 bool add_needed; 4404 struct elf_link_hash_table *htab; 4405 void *alloc_mark = NULL; 4406 struct bfd_hash_entry **old_table = NULL; 4407 unsigned int old_size = 0; 4408 unsigned int old_count = 0; 4409 void *old_tab = NULL; 4410 void *old_ent; 4411 struct bfd_link_hash_entry *old_undefs = NULL; 4412 struct bfd_link_hash_entry *old_undefs_tail = NULL; 4413 void *old_strtab = NULL; 4414 size_t tabsize = 0; 4415 asection *s; 4416 bool just_syms; 4417 4418 htab = elf_hash_table (info); 4419 bed = get_elf_backend_data (abfd); 4420 4421 if (elf_use_dt_symtab_p (abfd)) 4422 { 4423 bfd_set_error (bfd_error_wrong_format); 4424 return false; 4425 } 4426 4427 if ((abfd->flags & DYNAMIC) == 0) 4428 { 4429 dynamic = false; 4430 if ((abfd->flags & BFD_PLUGIN) != 0 4431 && is_elf_hash_table (&htab->root) 4432 && htab->first_hash == NULL) 4433 { 4434 /* Initialize first_hash for an IR input. */ 4435 htab->first_hash = (struct bfd_hash_table *) 4436 bfd_malloc (sizeof (struct bfd_hash_table)); 4437 if (htab->first_hash == NULL 4438 || !bfd_hash_table_init 4439 (htab->first_hash, elf_link_first_hash_newfunc, 4440 sizeof (struct elf_link_first_hash_entry))) 4441 info->callbacks->fatal 4442 (_("%P: first_hash failed to create: %E\n")); 4443 } 4444 } 4445 else 4446 { 4447 dynamic = true; 4448 4449 /* You can't use -r against a dynamic object. Also, there's no 4450 hope of using a dynamic object which does not exactly match 4451 the format of the output file. */ 4452 if (bfd_link_relocatable (info) 4453 || !is_elf_hash_table (&htab->root) 4454 || info->output_bfd->xvec != abfd->xvec) 4455 { 4456 if (bfd_link_relocatable (info)) 4457 bfd_set_error (bfd_error_invalid_operation); 4458 else 4459 bfd_set_error (bfd_error_wrong_format); 4460 goto error_return; 4461 } 4462 } 4463 4464 ehdr = elf_elfheader (abfd); 4465 if (info->warn_alternate_em 4466 && bed->elf_machine_code != ehdr->e_machine 4467 && ((bed->elf_machine_alt1 != 0 4468 && ehdr->e_machine == bed->elf_machine_alt1) 4469 || (bed->elf_machine_alt2 != 0 4470 && ehdr->e_machine == bed->elf_machine_alt2))) 4471 _bfd_error_handler 4472 /* xgettext:c-format */ 4473 (_("alternate ELF machine code found (%d) in %pB, expecting %d"), 4474 ehdr->e_machine, abfd, bed->elf_machine_code); 4475 4476 /* As a GNU extension, any input sections which are named 4477 .gnu.warning.SYMBOL are treated as warning symbols for the given 4478 symbol. This differs from .gnu.warning sections, which generate 4479 warnings when they are included in an output file. */ 4480 /* PR 12761: Also generate this warning when building shared libraries. */ 4481 for (s = abfd->sections; s != NULL; s = s->next) 4482 { 4483 const char *name; 4484 4485 name = bfd_section_name (s); 4486 if (startswith (name, ".gnu.warning.")) 4487 { 4488 char *msg; 4489 bfd_size_type sz; 4490 4491 name += sizeof ".gnu.warning." - 1; 4492 4493 /* If this is a shared object, then look up the symbol 4494 in the hash table. If it is there, and it is already 4495 been defined, then we will not be using the entry 4496 from this shared object, so we don't need to warn. 4497 FIXME: If we see the definition in a regular object 4498 later on, we will warn, but we shouldn't. The only 4499 fix is to keep track of what warnings we are supposed 4500 to emit, and then handle them all at the end of the 4501 link. */ 4502 if (dynamic) 4503 { 4504 struct elf_link_hash_entry *h; 4505 4506 h = elf_link_hash_lookup (htab, name, false, false, true); 4507 4508 /* FIXME: What about bfd_link_hash_common? */ 4509 if (h != NULL 4510 && (h->root.type == bfd_link_hash_defined 4511 || h->root.type == bfd_link_hash_defweak)) 4512 continue; 4513 } 4514 4515 sz = s->size; 4516 msg = (char *) bfd_alloc (abfd, sz + 1); 4517 if (msg == NULL) 4518 goto error_return; 4519 4520 if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) 4521 goto error_return; 4522 4523 msg[sz] = '\0'; 4524 4525 if (! (_bfd_generic_link_add_one_symbol 4526 (info, abfd, name, BSF_WARNING, s, 0, msg, 4527 false, bed->collect, NULL))) 4528 goto error_return; 4529 4530 if (bfd_link_executable (info)) 4531 { 4532 /* Clobber the section size so that the warning does 4533 not get copied into the output file. */ 4534 s->size = 0; 4535 4536 /* Also set SEC_EXCLUDE, so that symbols defined in 4537 the warning section don't get copied to the output. */ 4538 s->flags |= SEC_EXCLUDE; 4539 } 4540 } 4541 } 4542 4543 just_syms = ((s = abfd->sections) != NULL 4544 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); 4545 4546 add_needed = true; 4547 if (! dynamic) 4548 { 4549 /* If we are creating a shared library, create all the dynamic 4550 sections immediately. We need to attach them to something, 4551 so we attach them to this BFD, provided it is the right 4552 format and is not from ld --just-symbols. Always create the 4553 dynamic sections for -E/--dynamic-list. FIXME: If there 4554 are no input BFD's of the same format as the output, we can't 4555 make a shared library. */ 4556 if (!just_syms 4557 && (bfd_link_pic (info) 4558 || (!bfd_link_relocatable (info) 4559 && info->nointerp 4560 && (info->export_dynamic || info->dynamic))) 4561 && is_elf_hash_table (&htab->root) 4562 && info->output_bfd->xvec == abfd->xvec 4563 && !htab->dynamic_sections_created) 4564 { 4565 if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) 4566 goto error_return; 4567 } 4568 } 4569 else if (!is_elf_hash_table (&htab->root)) 4570 goto error_return; 4571 else 4572 { 4573 const char *soname = NULL; 4574 char *audit = NULL; 4575 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; 4576 const Elf_Internal_Phdr *phdr; 4577 struct elf_link_loaded_list *loaded_lib; 4578 4579 /* ld --just-symbols and dynamic objects don't mix very well. 4580 ld shouldn't allow it. */ 4581 if (just_syms) 4582 abort (); 4583 4584 /* If this dynamic lib was specified on the command line with 4585 --as-needed in effect, then we don't want to add a DT_NEEDED 4586 tag unless the lib is actually used. Similary for libs brought 4587 in by another lib's DT_NEEDED. When --no-add-needed is used 4588 on a dynamic lib, we don't want to add a DT_NEEDED entry for 4589 any dynamic library in DT_NEEDED tags in the dynamic lib at 4590 all. */ 4591 add_needed = (elf_dyn_lib_class (abfd) 4592 & (DYN_AS_NEEDED | DYN_DT_NEEDED 4593 | DYN_NO_NEEDED)) == 0; 4594 4595 s = bfd_get_section_by_name (abfd, ".dynamic"); 4596 if (s != NULL && s->size != 0 && (s->flags & SEC_HAS_CONTENTS) != 0) 4597 { 4598 bfd_byte *dynbuf; 4599 bfd_byte *extdyn; 4600 unsigned int elfsec; 4601 unsigned long shlink; 4602 4603 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf)) 4604 { 4605 error_free_dyn: 4606 _bfd_elf_munmap_section_contents (s, dynbuf); 4607 goto error_return; 4608 } 4609 4610 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 4611 if (elfsec == SHN_BAD) 4612 goto error_free_dyn; 4613 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 4614 4615 for (extdyn = dynbuf; 4616 (size_t) (dynbuf + s->size - extdyn) >= bed->s->sizeof_dyn; 4617 extdyn += bed->s->sizeof_dyn) 4618 { 4619 Elf_Internal_Dyn dyn; 4620 4621 bed->s->swap_dyn_in (abfd, extdyn, &dyn); 4622 if (dyn.d_tag == DT_SONAME) 4623 { 4624 unsigned int tagv = dyn.d_un.d_val; 4625 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4626 if (soname == NULL) 4627 goto error_free_dyn; 4628 } 4629 if (dyn.d_tag == DT_NEEDED) 4630 { 4631 struct bfd_link_needed_list *n, **pn; 4632 char *fnm, *anm; 4633 unsigned int tagv = dyn.d_un.d_val; 4634 size_t amt = sizeof (struct bfd_link_needed_list); 4635 4636 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4637 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4638 if (n == NULL || fnm == NULL) 4639 goto error_free_dyn; 4640 amt = strlen (fnm) + 1; 4641 anm = (char *) bfd_alloc (abfd, amt); 4642 if (anm == NULL) 4643 goto error_free_dyn; 4644 memcpy (anm, fnm, amt); 4645 n->name = anm; 4646 n->by = abfd; 4647 n->next = NULL; 4648 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) 4649 ; 4650 *pn = n; 4651 } 4652 if (dyn.d_tag == DT_RUNPATH) 4653 { 4654 struct bfd_link_needed_list *n, **pn; 4655 char *fnm, *anm; 4656 unsigned int tagv = dyn.d_un.d_val; 4657 size_t amt = sizeof (struct bfd_link_needed_list); 4658 4659 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4660 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4661 if (n == NULL || fnm == NULL) 4662 goto error_free_dyn; 4663 amt = strlen (fnm) + 1; 4664 anm = (char *) bfd_alloc (abfd, amt); 4665 if (anm == NULL) 4666 goto error_free_dyn; 4667 memcpy (anm, fnm, amt); 4668 n->name = anm; 4669 n->by = abfd; 4670 n->next = NULL; 4671 for (pn = & runpath; 4672 *pn != NULL; 4673 pn = &(*pn)->next) 4674 ; 4675 *pn = n; 4676 } 4677 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ 4678 if (!runpath && dyn.d_tag == DT_RPATH) 4679 { 4680 struct bfd_link_needed_list *n, **pn; 4681 char *fnm, *anm; 4682 unsigned int tagv = dyn.d_un.d_val; 4683 size_t amt = sizeof (struct bfd_link_needed_list); 4684 4685 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4686 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4687 if (n == NULL || fnm == NULL) 4688 goto error_free_dyn; 4689 amt = strlen (fnm) + 1; 4690 anm = (char *) bfd_alloc (abfd, amt); 4691 if (anm == NULL) 4692 goto error_free_dyn; 4693 memcpy (anm, fnm, amt); 4694 n->name = anm; 4695 n->by = abfd; 4696 n->next = NULL; 4697 for (pn = & rpath; 4698 *pn != NULL; 4699 pn = &(*pn)->next) 4700 ; 4701 *pn = n; 4702 } 4703 if (dyn.d_tag == DT_AUDIT) 4704 { 4705 unsigned int tagv = dyn.d_un.d_val; 4706 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4707 } 4708 if (dyn.d_tag == DT_FLAGS_1) 4709 elf_tdata (abfd)->is_pie = (dyn.d_un.d_val & DF_1_PIE) != 0; 4710 } 4711 4712 _bfd_elf_munmap_section_contents (s, dynbuf); 4713 } 4714 4715 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that 4716 frees all more recently bfd_alloc'd blocks as well. */ 4717 if (runpath) 4718 rpath = runpath; 4719 4720 if (rpath) 4721 { 4722 struct bfd_link_needed_list **pn; 4723 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) 4724 ; 4725 *pn = rpath; 4726 } 4727 4728 /* If we have a PT_GNU_RELRO program header, mark as read-only 4729 all sections contained fully therein. This makes relro 4730 shared library sections appear as they will at run-time. */ 4731 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; 4732 while (phdr-- > elf_tdata (abfd)->phdr) 4733 if (phdr->p_type == PT_GNU_RELRO) 4734 { 4735 for (s = abfd->sections; s != NULL; s = s->next) 4736 { 4737 unsigned int opb = bfd_octets_per_byte (abfd, s); 4738 4739 if ((s->flags & SEC_ALLOC) != 0 4740 && s->vma * opb >= phdr->p_vaddr 4741 && s->vma * opb + s->size <= phdr->p_vaddr + phdr->p_memsz) 4742 s->flags |= SEC_READONLY; 4743 } 4744 break; 4745 } 4746 4747 /* We do not want to include any of the sections in a dynamic 4748 object in the output file. We hack by simply clobbering the 4749 list of sections in the BFD. This could be handled more 4750 cleanly by, say, a new section flag; the existing 4751 SEC_NEVER_LOAD flag is not the one we want, because that one 4752 still implies that the section takes up space in the output 4753 file. */ 4754 bfd_section_list_clear (abfd); 4755 4756 /* Find the name to use in a DT_NEEDED entry that refers to this 4757 object. If the object has a DT_SONAME entry, we use it. 4758 Otherwise, if the generic linker stuck something in 4759 elf_dt_name, we use that. Otherwise, we just use the file 4760 name. */ 4761 if (soname == NULL || *soname == '\0') 4762 { 4763 soname = elf_dt_name (abfd); 4764 if (soname == NULL || *soname == '\0') 4765 soname = bfd_get_filename (abfd); 4766 } 4767 4768 /* Save the SONAME because sometimes the linker emulation code 4769 will need to know it. */ 4770 elf_dt_name (abfd) = soname; 4771 4772 /* If we have already included this dynamic object in the 4773 link, just ignore it. There is no reason to include a 4774 particular dynamic object more than once. */ 4775 for (loaded_lib = htab->dyn_loaded; 4776 loaded_lib != NULL; 4777 loaded_lib = loaded_lib->next) 4778 { 4779 if (strcmp (elf_dt_name (loaded_lib->abfd), soname) == 0) 4780 return true; 4781 } 4782 4783 /* Create dynamic sections for backends that require that be done 4784 before setup_gnu_properties. */ 4785 if (add_needed 4786 && !_bfd_elf_link_create_dynamic_sections (abfd, info)) 4787 return false; 4788 4789 /* Save the DT_AUDIT entry for the linker emulation code. */ 4790 elf_dt_audit (abfd) = audit; 4791 } 4792 4793 /* If this is a dynamic object, we always link against the .dynsym 4794 symbol table, not the .symtab symbol table. The dynamic linker 4795 will only see the .dynsym symbol table, so there is no reason to 4796 look at .symtab for a dynamic object. */ 4797 4798 if (! dynamic || elf_dynsymtab (abfd) == 0) 4799 hdr = &elf_tdata (abfd)->symtab_hdr; 4800 else 4801 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 4802 4803 symcount = hdr->sh_size / bed->s->sizeof_sym; 4804 4805 /* The sh_info field of the symtab header tells us where the 4806 external symbols start. We don't care about the local symbols at 4807 this point. */ 4808 if (elf_bad_symtab (abfd)) 4809 { 4810 extsymcount = symcount; 4811 extsymoff = 0; 4812 } 4813 else 4814 { 4815 extsymcount = symcount - hdr->sh_info; 4816 extsymoff = hdr->sh_info; 4817 } 4818 4819 sym_hash = elf_sym_hashes (abfd); 4820 if (extsymcount != 0) 4821 { 4822 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 4823 NULL, NULL, NULL); 4824 if (isymbuf == NULL) 4825 goto error_return; 4826 4827 if (sym_hash == NULL) 4828 { 4829 /* We store a pointer to the hash table entry for each 4830 external symbol. */ 4831 size_t amt = extsymcount * sizeof (struct elf_link_hash_entry *); 4832 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); 4833 if (sym_hash == NULL) 4834 goto error_free_sym; 4835 elf_sym_hashes (abfd) = sym_hash; 4836 } 4837 } 4838 4839 if (dynamic) 4840 { 4841 /* Read in any version definitions. */ 4842 if (!_bfd_elf_slurp_version_tables (abfd, 4843 info->default_imported_symver)) 4844 goto error_free_sym; 4845 4846 /* Read in the symbol versions, but don't bother to convert them 4847 to internal format. */ 4848 if (elf_dynversym (abfd) != 0) 4849 { 4850 Elf_Internal_Shdr *versymhdr = &elf_tdata (abfd)->dynversym_hdr; 4851 bfd_size_type amt = versymhdr->sh_size; 4852 4853 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0) 4854 goto error_free_sym; 4855 extversym = (Elf_External_Versym *) 4856 _bfd_malloc_and_read (abfd, amt, amt); 4857 if (extversym == NULL) 4858 goto error_free_sym; 4859 extversym_end = extversym + amt / sizeof (*extversym); 4860 } 4861 } 4862 4863 /* If we are loading an as-needed shared lib, save the symbol table 4864 state before we start adding symbols. If the lib turns out 4865 to be unneeded, restore the state. */ 4866 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4867 { 4868 unsigned int i; 4869 size_t entsize; 4870 4871 for (entsize = 0, i = 0; i < htab->root.table.size; i++) 4872 { 4873 struct bfd_hash_entry *p; 4874 struct elf_link_hash_entry *h; 4875 4876 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4877 { 4878 h = (struct elf_link_hash_entry *) p; 4879 entsize += htab->root.table.entsize; 4880 if (h->root.type == bfd_link_hash_warning) 4881 { 4882 entsize += htab->root.table.entsize; 4883 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4884 } 4885 if (h->root.type == bfd_link_hash_common) 4886 entsize += sizeof (*h->root.u.c.p); 4887 } 4888 } 4889 4890 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); 4891 old_tab = bfd_malloc (tabsize + entsize); 4892 if (old_tab == NULL) 4893 goto error_free_vers; 4894 4895 /* Remember the current objalloc pointer, so that all mem for 4896 symbols added can later be reclaimed. */ 4897 alloc_mark = bfd_hash_allocate (&htab->root.table, 1); 4898 if (alloc_mark == NULL) 4899 goto error_free_vers; 4900 4901 /* Make a special call to the linker "notice" function to 4902 tell it that we are about to handle an as-needed lib. */ 4903 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) 4904 goto error_free_vers; 4905 4906 /* Clone the symbol table. Remember some pointers into the 4907 symbol table, and dynamic symbol count. */ 4908 old_ent = (char *) old_tab + tabsize; 4909 memcpy (old_tab, htab->root.table.table, tabsize); 4910 old_undefs = htab->root.undefs; 4911 old_undefs_tail = htab->root.undefs_tail; 4912 old_table = htab->root.table.table; 4913 old_size = htab->root.table.size; 4914 old_count = htab->root.table.count; 4915 old_strtab = NULL; 4916 if (htab->dynstr != NULL) 4917 { 4918 old_strtab = _bfd_elf_strtab_save (htab->dynstr); 4919 if (old_strtab == NULL) 4920 goto error_free_vers; 4921 } 4922 4923 for (i = 0; i < htab->root.table.size; i++) 4924 { 4925 struct bfd_hash_entry *p; 4926 struct elf_link_hash_entry *h; 4927 4928 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4929 { 4930 h = (struct elf_link_hash_entry *) p; 4931 memcpy (old_ent, h, htab->root.table.entsize); 4932 old_ent = (char *) old_ent + htab->root.table.entsize; 4933 if (h->root.type == bfd_link_hash_warning) 4934 { 4935 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4936 memcpy (old_ent, h, htab->root.table.entsize); 4937 old_ent = (char *) old_ent + htab->root.table.entsize; 4938 } 4939 if (h->root.type == bfd_link_hash_common) 4940 { 4941 memcpy (old_ent, h->root.u.c.p, sizeof (*h->root.u.c.p)); 4942 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p); 4943 } 4944 } 4945 } 4946 } 4947 4948 weaks = NULL; 4949 if (extversym == NULL) 4950 ever = NULL; 4951 else if (extversym + extsymoff < extversym_end) 4952 ever = extversym + extsymoff; 4953 else 4954 { 4955 /* xgettext:c-format */ 4956 _bfd_error_handler (_("%pB: invalid version offset %lx (max %lx)"), 4957 abfd, (long) extsymoff, 4958 (long) (extversym_end - extversym) / sizeof (* extversym)); 4959 bfd_set_error (bfd_error_bad_value); 4960 goto error_free_vers; 4961 } 4962 4963 if (!bfd_link_relocatable (info) 4964 && bfd_get_lto_type (abfd) == lto_slim_ir_object) 4965 { 4966 _bfd_error_handler 4967 (_("%pB: plugin needed to handle lto object"), abfd); 4968 } 4969 4970 for (isym = isymbuf, isymend = PTR_ADD (isymbuf, extsymcount); 4971 isym < isymend; 4972 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) 4973 { 4974 int bind; 4975 bfd_vma value; 4976 asection *sec, *new_sec; 4977 flagword flags; 4978 const char *name; 4979 const char *defvername; 4980 bool must_copy_name = false; 4981 struct elf_link_hash_entry *h; 4982 struct elf_link_hash_entry *hi; 4983 bool definition; 4984 bool size_change_ok; 4985 bool type_change_ok; 4986 bool new_weak; 4987 bool old_weak; 4988 bfd *override; 4989 bool common; 4990 bool discarded; 4991 unsigned int old_alignment; 4992 unsigned int shindex; 4993 bfd *old_bfd; 4994 bool matched; 4995 4996 override = NULL; 4997 4998 flags = BSF_NO_FLAGS; 4999 sec = NULL; 5000 value = isym->st_value; 5001 common = bed->common_definition (isym); 5002 if (common && info->inhibit_common_definition) 5003 { 5004 /* Treat common symbol as undefined for --no-define-common. */ 5005 isym->st_shndx = SHN_UNDEF; 5006 common = false; 5007 } 5008 discarded = false; 5009 5010 bind = ELF_ST_BIND (isym->st_info); 5011 switch (bind) 5012 { 5013 case STB_LOCAL: 5014 /* This should be impossible, since ELF requires that all 5015 global symbols follow all local symbols, and that sh_info 5016 point to the first global symbol. Unfortunately, Irix 5 5017 screws this up. */ 5018 if (elf_bad_symtab (abfd)) 5019 continue; 5020 5021 /* If we aren't prepared to handle locals within the globals 5022 then we'll likely segfault on a NULL symbol hash if the 5023 symbol is ever referenced in relocations. */ 5024 shindex = elf_elfheader (abfd)->e_shstrndx; 5025 name = bfd_elf_string_from_elf_section (abfd, shindex, hdr->sh_name); 5026 _bfd_error_handler (_("%pB: %s local symbol at index %lu" 5027 " (>= sh_info of %lu)"), 5028 abfd, name, (long) (isym - isymbuf + extsymoff), 5029 (long) extsymoff); 5030 5031 /* Dynamic object relocations are not processed by ld, so 5032 ld won't run into the problem mentioned above. */ 5033 if (dynamic) 5034 continue; 5035 bfd_set_error (bfd_error_bad_value); 5036 goto error_free_vers; 5037 5038 case STB_GLOBAL: 5039 if (isym->st_shndx != SHN_UNDEF && !common) 5040 flags = BSF_GLOBAL; 5041 break; 5042 5043 case STB_WEAK: 5044 flags = BSF_WEAK; 5045 break; 5046 5047 case STB_GNU_UNIQUE: 5048 flags = BSF_GNU_UNIQUE; 5049 break; 5050 5051 default: 5052 /* Leave it up to the processor backend. */ 5053 break; 5054 } 5055 5056 if (isym->st_shndx == SHN_UNDEF) 5057 sec = bfd_und_section_ptr; 5058 else if (isym->st_shndx == SHN_ABS) 5059 sec = bfd_abs_section_ptr; 5060 else if (isym->st_shndx == SHN_COMMON) 5061 { 5062 sec = bfd_com_section_ptr; 5063 /* What ELF calls the size we call the value. What ELF 5064 calls the value we call the alignment. */ 5065 value = isym->st_size; 5066 } 5067 else 5068 { 5069 sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 5070 if (sec == NULL) 5071 sec = bfd_abs_section_ptr; 5072 else if (discarded_section (sec)) 5073 { 5074 /* Symbols from discarded section are undefined. We keep 5075 its visibility. */ 5076 sec = bfd_und_section_ptr; 5077 discarded = true; 5078 isym->st_shndx = SHN_UNDEF; 5079 } 5080 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 5081 value -= sec->vma; 5082 } 5083 5084 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 5085 isym->st_name); 5086 if (name == NULL) 5087 goto error_free_vers; 5088 5089 if (isym->st_shndx == SHN_COMMON 5090 && (abfd->flags & BFD_PLUGIN) != 0) 5091 { 5092 asection *xc = bfd_get_section_by_name (abfd, "COMMON"); 5093 5094 if (xc == NULL) 5095 { 5096 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP 5097 | SEC_EXCLUDE); 5098 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); 5099 if (xc == NULL) 5100 goto error_free_vers; 5101 } 5102 sec = xc; 5103 } 5104 else if (isym->st_shndx == SHN_COMMON 5105 && ELF_ST_TYPE (isym->st_info) == STT_TLS 5106 && !bfd_link_relocatable (info)) 5107 { 5108 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); 5109 5110 if (tcomm == NULL) 5111 { 5112 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON 5113 | SEC_LINKER_CREATED); 5114 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); 5115 if (tcomm == NULL) 5116 goto error_free_vers; 5117 } 5118 sec = tcomm; 5119 } 5120 else if (bed->elf_add_symbol_hook) 5121 { 5122 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, 5123 &sec, &value)) 5124 goto error_free_vers; 5125 5126 /* The hook function sets the name to NULL if this symbol 5127 should be skipped for some reason. */ 5128 if (name == NULL) 5129 continue; 5130 } 5131 5132 /* Sanity check that all possibilities were handled. */ 5133 if (sec == NULL) 5134 abort (); 5135 5136 /* Silently discard TLS symbols from --just-syms. There's 5137 no way to combine a static TLS block with a new TLS block 5138 for this executable. */ 5139 if (ELF_ST_TYPE (isym->st_info) == STT_TLS 5140 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 5141 continue; 5142 5143 if (bfd_is_und_section (sec) 5144 || bfd_is_com_section (sec)) 5145 definition = false; 5146 else 5147 definition = true; 5148 5149 size_change_ok = false; 5150 type_change_ok = bed->type_change_ok; 5151 old_weak = false; 5152 matched = false; 5153 old_alignment = 0; 5154 old_bfd = NULL; 5155 new_sec = sec; 5156 defvername = NULL; 5157 5158 if (is_elf_hash_table (&htab->root)) 5159 { 5160 Elf_Internal_Versym iver; 5161 unsigned int vernum = 0; 5162 bool skip; 5163 5164 if (ever == NULL) 5165 { 5166 if (info->default_imported_symver) 5167 /* Use the default symbol version created earlier. */ 5168 iver.vs_vers = elf_tdata (abfd)->cverdefs; 5169 else 5170 iver.vs_vers = 0; 5171 } 5172 else if (ever >= extversym_end) 5173 { 5174 /* xgettext:c-format */ 5175 _bfd_error_handler (_("%pB: not enough version information"), 5176 abfd); 5177 bfd_set_error (bfd_error_bad_value); 5178 goto error_free_vers; 5179 } 5180 else 5181 _bfd_elf_swap_versym_in (abfd, ever, &iver); 5182 5183 vernum = iver.vs_vers & VERSYM_VERSION; 5184 5185 /* If this is a hidden symbol, or if it is not version 5186 1, we append the version name to the symbol name. 5187 However, we do not modify a non-hidden absolute symbol 5188 if it is not a function, because it might be the version 5189 symbol itself. FIXME: What if it isn't? */ 5190 if ((iver.vs_vers & VERSYM_HIDDEN) != 0 5191 || (vernum > 1 5192 && (!bfd_is_abs_section (sec) 5193 || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) 5194 { 5195 const char *verstr; 5196 size_t namelen, verlen, newlen; 5197 char *newname, *p; 5198 5199 if (isym->st_shndx != SHN_UNDEF) 5200 { 5201 if (vernum > elf_tdata (abfd)->cverdefs) 5202 verstr = NULL; 5203 else if (vernum > 1) 5204 verstr = 5205 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 5206 else 5207 verstr = ""; 5208 5209 if (verstr == NULL) 5210 { 5211 _bfd_error_handler 5212 /* xgettext:c-format */ 5213 (_("%pB: %s: invalid version %u (max %d)"), 5214 abfd, name, vernum, 5215 elf_tdata (abfd)->cverdefs); 5216 bfd_set_error (bfd_error_bad_value); 5217 goto error_free_vers; 5218 } 5219 } 5220 else 5221 { 5222 /* We cannot simply test for the number of 5223 entries in the VERNEED section since the 5224 numbers for the needed versions do not start 5225 at 0. */ 5226 Elf_Internal_Verneed *t; 5227 5228 verstr = NULL; 5229 for (t = elf_tdata (abfd)->verref; 5230 t != NULL; 5231 t = t->vn_nextref) 5232 { 5233 Elf_Internal_Vernaux *a; 5234 5235 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 5236 { 5237 if (a->vna_other == vernum) 5238 { 5239 verstr = a->vna_nodename; 5240 break; 5241 } 5242 } 5243 if (a != NULL) 5244 break; 5245 } 5246 if (verstr == NULL) 5247 { 5248 _bfd_error_handler 5249 /* xgettext:c-format */ 5250 (_("%pB: %s: invalid needed version %d"), 5251 abfd, name, vernum); 5252 bfd_set_error (bfd_error_bad_value); 5253 goto error_free_vers; 5254 } 5255 } 5256 5257 namelen = strlen (name); 5258 verlen = strlen (verstr); 5259 newlen = namelen + verlen + 2; 5260 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 5261 && isym->st_shndx != SHN_UNDEF) 5262 ++newlen; 5263 5264 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); 5265 if (newname == NULL) 5266 goto error_free_vers; 5267 memcpy (newname, name, namelen); 5268 p = newname + namelen; 5269 *p++ = ELF_VER_CHR; 5270 /* If this is a defined non-hidden version symbol, 5271 we add another @ to the name. This indicates the 5272 default version of the symbol. */ 5273 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 5274 && isym->st_shndx != SHN_UNDEF) 5275 *p++ = ELF_VER_CHR, defvername = name; 5276 memcpy (p, verstr, verlen + 1); 5277 5278 name = newname; 5279 /* Since bfd_hash_alloc is used for "name", the string 5280 must be copied if added to first_hash. The string 5281 memory can be freed when an --as-needed library is 5282 not needed. */ 5283 must_copy_name = true; 5284 } 5285 5286 /* If this symbol has default visibility and the user has 5287 requested we not re-export it, then mark it as hidden. */ 5288 if (!bfd_is_und_section (sec) 5289 && !dynamic 5290 && abfd->no_export 5291 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) 5292 isym->st_other = (STV_HIDDEN 5293 | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); 5294 5295 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, 5296 sym_hash, &old_bfd, &old_weak, 5297 &old_alignment, &skip, &override, 5298 &type_change_ok, &size_change_ok, 5299 &matched)) 5300 goto error_free_vers; 5301 5302 if (skip) 5303 continue; 5304 5305 h = *sym_hash; 5306 while (h->root.type == bfd_link_hash_indirect 5307 || h->root.type == bfd_link_hash_warning) 5308 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5309 5310 /* Override a definition only if the new symbol matches the 5311 existing one. */ 5312 if (override && matched) 5313 { 5314 definition = false; 5315 if (htab->first_hash != NULL 5316 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 5317 && h->root.non_ir_ref_regular) 5318 { 5319 /* When reloading --as-needed shared objects for new 5320 symbols added from IR inputs, if this shared object 5321 has the first definition, use it. */ 5322 struct elf_link_first_hash_entry *e 5323 = ((struct elf_link_first_hash_entry *) 5324 bfd_hash_lookup (htab->first_hash, name, false, 5325 false)); 5326 if (e != NULL && e->abfd == abfd) 5327 definition = true; 5328 } 5329 } 5330 5331 if (h->versioned != unversioned 5332 && elf_tdata (abfd)->verdef != NULL 5333 && vernum > 1 5334 && definition) 5335 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; 5336 } 5337 5338 if (! (_bfd_generic_link_add_one_symbol 5339 (info, override ? override : abfd, name, flags, sec, value, 5340 NULL, false, bed->collect, 5341 (struct bfd_link_hash_entry **) sym_hash))) 5342 goto error_free_vers; 5343 5344 h = *sym_hash; 5345 /* We need to make sure that indirect symbol dynamic flags are 5346 updated. */ 5347 hi = h; 5348 while (h->root.type == bfd_link_hash_indirect 5349 || h->root.type == bfd_link_hash_warning) 5350 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5351 5352 *sym_hash = h; 5353 5354 /* Setting the index to -3 tells elf_link_output_extsym that 5355 this symbol is defined in a discarded section. */ 5356 if (discarded && is_elf_hash_table (&htab->root)) 5357 h->indx = -3; 5358 5359 new_weak = (flags & BSF_WEAK) != 0; 5360 if (dynamic 5361 && definition 5362 && new_weak 5363 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) 5364 && is_elf_hash_table (&htab->root) 5365 && h->u.alias == NULL) 5366 { 5367 /* Keep a list of all weak defined non function symbols from 5368 a dynamic object, using the alias field. Later in this 5369 function we will set the alias field to the correct 5370 value. We only put non-function symbols from dynamic 5371 objects on this list, because that happens to be the only 5372 time we need to know the normal symbol corresponding to a 5373 weak symbol, and the information is time consuming to 5374 figure out. If the alias field is not already NULL, 5375 then this symbol was already defined by some previous 5376 dynamic object, and we will be using that previous 5377 definition anyhow. */ 5378 5379 h->u.alias = weaks; 5380 weaks = h; 5381 } 5382 5383 /* Set the alignment of a common symbol. */ 5384 if ((common || bfd_is_com_section (sec)) 5385 && h->root.type == bfd_link_hash_common) 5386 { 5387 unsigned int align; 5388 5389 if (common) 5390 align = bfd_log2 (isym->st_value); 5391 else 5392 { 5393 /* The new symbol is a common symbol in a shared object. 5394 We need to get the alignment from the section. */ 5395 align = new_sec->alignment_power; 5396 } 5397 if (align > old_alignment) 5398 h->root.u.c.p->alignment_power = align; 5399 else 5400 h->root.u.c.p->alignment_power = old_alignment; 5401 } 5402 5403 if (is_elf_hash_table (&htab->root)) 5404 { 5405 /* Set a flag in the hash table entry indicating the type of 5406 reference or definition we just found. A dynamic symbol 5407 is one which is referenced or defined by both a regular 5408 object and a shared object. */ 5409 bool dynsym = false; 5410 5411 /* Plugin symbols aren't normal. Don't set def/ref flags. */ 5412 if ((abfd->flags & BFD_PLUGIN) != 0) 5413 { 5414 /* Except for this flag to track nonweak references. */ 5415 if (!definition 5416 && bind != STB_WEAK) 5417 h->ref_ir_nonweak = 1; 5418 } 5419 else if (!dynamic) 5420 { 5421 if (! definition) 5422 { 5423 h->ref_regular = 1; 5424 if (bind != STB_WEAK) 5425 h->ref_regular_nonweak = 1; 5426 } 5427 else 5428 { 5429 h->def_regular = 1; 5430 if (h->def_dynamic) 5431 { 5432 h->def_dynamic = 0; 5433 h->ref_dynamic = 1; 5434 } 5435 } 5436 } 5437 else 5438 { 5439 if (! definition) 5440 { 5441 h->ref_dynamic = 1; 5442 hi->ref_dynamic = 1; 5443 } 5444 else 5445 { 5446 h->def_dynamic = 1; 5447 hi->def_dynamic = 1; 5448 } 5449 } 5450 5451 /* If an indirect symbol has been forced local, don't 5452 make the real symbol dynamic. */ 5453 if (h != hi && hi->forced_local) 5454 ; 5455 else if (!dynamic) 5456 { 5457 if (bfd_link_dll (info) 5458 || h->def_dynamic 5459 || h->ref_dynamic) 5460 dynsym = true; 5461 } 5462 else 5463 { 5464 if (h->def_regular 5465 || h->ref_regular 5466 || (h->is_weakalias 5467 && weakdef (h)->dynindx != -1)) 5468 dynsym = true; 5469 } 5470 5471 /* Check to see if we need to add an indirect symbol for 5472 the default name. */ 5473 if ((definition 5474 || (!override && h->root.type == bfd_link_hash_common)) 5475 && !(hi != h 5476 && hi->versioned == versioned_hidden)) 5477 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, 5478 sec, value, &old_bfd, &dynsym)) 5479 goto error_free_vers; 5480 5481 /* Check the alignment when a common symbol is involved. This 5482 can change when a common symbol is overridden by a normal 5483 definition or a common symbol is ignored due to the old 5484 normal definition. We need to make sure the maximum 5485 alignment is maintained. */ 5486 if ((old_alignment || common) 5487 && h->root.type != bfd_link_hash_common) 5488 { 5489 unsigned int common_align; 5490 unsigned int normal_align; 5491 unsigned int symbol_align; 5492 bfd *normal_bfd; 5493 bfd *common_bfd; 5494 5495 BFD_ASSERT (h->root.type == bfd_link_hash_defined 5496 || h->root.type == bfd_link_hash_defweak); 5497 5498 symbol_align = ffs (h->root.u.def.value) - 1; 5499 if (h->root.u.def.section->owner != NULL 5500 && (h->root.u.def.section->owner->flags 5501 & (DYNAMIC | BFD_PLUGIN)) == 0) 5502 { 5503 normal_align = h->root.u.def.section->alignment_power; 5504 if (normal_align > symbol_align) 5505 normal_align = symbol_align; 5506 } 5507 else 5508 normal_align = symbol_align; 5509 5510 if (old_alignment) 5511 { 5512 common_align = old_alignment; 5513 common_bfd = old_bfd; 5514 normal_bfd = abfd; 5515 } 5516 else 5517 { 5518 common_align = bfd_log2 (isym->st_value); 5519 common_bfd = abfd; 5520 normal_bfd = old_bfd; 5521 } 5522 5523 if (normal_align < common_align) 5524 { 5525 /* PR binutils/2735 */ 5526 if (normal_bfd == NULL) 5527 _bfd_error_handler 5528 /* xgettext:c-format */ 5529 (_("warning: alignment %u of common symbol `%s' in %pB is" 5530 " greater than the alignment (%u) of its section %pA"), 5531 1 << common_align, name, common_bfd, 5532 1 << normal_align, h->root.u.def.section); 5533 else 5534 _bfd_error_handler 5535 /* xgettext:c-format */ 5536 (_("warning: alignment %u of normal symbol `%s' in %pB" 5537 " is smaller than %u used by the common definition in %pB"), 5538 1 << normal_align, name, normal_bfd, 5539 1 << common_align, common_bfd); 5540 5541 /* PR 30499: make sure that users understand that this warning is serious. */ 5542 _bfd_error_handler 5543 (_("warning: NOTE: alignment discrepancies can cause real problems. Investigation is advised.")); 5544 } 5545 } 5546 5547 /* Remember the symbol size if it isn't undefined. */ 5548 if (isym->st_size != 0 5549 && isym->st_shndx != SHN_UNDEF 5550 && (definition || h->size == 0)) 5551 { 5552 if (h->size != 0 5553 && h->size != isym->st_size 5554 && ! size_change_ok) 5555 { 5556 _bfd_error_handler 5557 /* xgettext:c-format */ 5558 (_("warning: size of symbol `%s' changed" 5559 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), 5560 name, (uint64_t) h->size, old_bfd, 5561 (uint64_t) isym->st_size, abfd); 5562 5563 /* PR 30499: make sure that users understand that this warning is serious. */ 5564 _bfd_error_handler 5565 (_("warning: NOTE: size discrepancies can cause real problems. Investigation is advised.")); 5566 } 5567 5568 h->size = isym->st_size; 5569 } 5570 5571 /* If this is a common symbol, then we always want H->SIZE 5572 to be the size of the common symbol. The code just above 5573 won't fix the size if a common symbol becomes larger. We 5574 don't warn about a size change here, because that is 5575 covered by --warn-common. Allow changes between different 5576 function types. */ 5577 if (h->root.type == bfd_link_hash_common) 5578 h->size = h->root.u.c.size; 5579 5580 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE 5581 && ((definition && !new_weak) 5582 || (old_weak && h->root.type == bfd_link_hash_common) 5583 || h->type == STT_NOTYPE)) 5584 { 5585 unsigned int type = ELF_ST_TYPE (isym->st_info); 5586 5587 /* Turn an IFUNC symbol from a DSO into a normal FUNC 5588 symbol. */ 5589 if (type == STT_GNU_IFUNC 5590 && (abfd->flags & DYNAMIC) != 0) 5591 type = STT_FUNC; 5592 5593 if (h->type != type) 5594 { 5595 if (h->type != STT_NOTYPE && ! type_change_ok) 5596 /* xgettext:c-format */ 5597 _bfd_error_handler 5598 (_("warning: type of symbol `%s' changed" 5599 " from %d to %d in %pB"), 5600 name, h->type, type, abfd); 5601 5602 h->type = type; 5603 } 5604 } 5605 5606 /* Merge st_other field. */ 5607 elf_merge_st_other (abfd, h, isym->st_other, sec, 5608 definition, dynamic); 5609 5610 /* We don't want to make debug symbol dynamic. */ 5611 if (definition 5612 && (sec->flags & SEC_DEBUGGING) 5613 && !bfd_link_relocatable (info)) 5614 dynsym = false; 5615 5616 /* Nor should we make plugin symbols dynamic. */ 5617 if ((abfd->flags & BFD_PLUGIN) != 0) 5618 dynsym = false; 5619 5620 if (definition) 5621 { 5622 h->target_internal = isym->st_target_internal; 5623 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; 5624 } 5625 5626 /* Don't add indirect symbols for .symver x, x@FOO aliases 5627 in IR. Since all data or text symbols in IR have the 5628 same type, value and section, we can't tell if a symbol 5629 is an alias of another symbol by their types, values and 5630 sections. */ 5631 if (definition 5632 && !dynamic 5633 && (abfd->flags & BFD_PLUGIN) == 0) 5634 { 5635 char *p = strchr (name, ELF_VER_CHR); 5636 if (p != NULL && p[1] != ELF_VER_CHR) 5637 { 5638 /* Queue non-default versions so that .symver x, x@FOO 5639 aliases can be checked. */ 5640 if (!nondeflt_vers) 5641 { 5642 size_t amt = ((isymend - isym + 1) 5643 * sizeof (struct elf_link_hash_entry *)); 5644 nondeflt_vers 5645 = (struct elf_link_hash_entry **) bfd_malloc (amt); 5646 if (!nondeflt_vers) 5647 goto error_free_vers; 5648 } 5649 nondeflt_vers[nondeflt_vers_cnt++] = h; 5650 } 5651 } 5652 5653 if (dynsym && h->dynindx == -1) 5654 { 5655 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 5656 goto error_free_vers; 5657 if (h->is_weakalias 5658 && weakdef (h)->dynindx == -1) 5659 { 5660 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) 5661 goto error_free_vers; 5662 } 5663 } 5664 else if (h->dynindx != -1) 5665 /* If the symbol already has a dynamic index, but 5666 visibility says it should not be visible, turn it into 5667 a local symbol. */ 5668 switch (ELF_ST_VISIBILITY (h->other)) 5669 { 5670 case STV_INTERNAL: 5671 case STV_HIDDEN: 5672 (*bed->elf_backend_hide_symbol) (info, h, true); 5673 dynsym = false; 5674 break; 5675 } 5676 5677 if (!add_needed 5678 && matched 5679 && definition 5680 && h->root.type != bfd_link_hash_indirect) 5681 { 5682 if ((dynsym 5683 && h->ref_regular_nonweak) 5684 || (old_bfd != NULL 5685 && (old_bfd->flags & BFD_PLUGIN) != 0 5686 && h->ref_ir_nonweak 5687 && !info->lto_all_symbols_read) 5688 || (h->ref_dynamic_nonweak 5689 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 5690 && !on_needed_list (elf_dt_name (abfd), 5691 htab->needed, NULL))) 5692 { 5693 const char *soname = elf_dt_name (abfd); 5694 5695 info->callbacks->minfo ("%!", soname, old_bfd, 5696 h->root.root.string); 5697 5698 /* A symbol from a library loaded via DT_NEEDED of some 5699 other library is referenced by a regular object. 5700 Add a DT_NEEDED entry for it. Issue an error if 5701 --no-add-needed is used and the reference was not 5702 a weak one. */ 5703 if (old_bfd != NULL 5704 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) 5705 { 5706 _bfd_error_handler 5707 /* xgettext:c-format */ 5708 (_("%pB: undefined reference to symbol '%s'"), 5709 old_bfd, name); 5710 bfd_set_error (bfd_error_missing_dso); 5711 goto error_free_vers; 5712 } 5713 5714 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) 5715 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); 5716 5717 /* Create dynamic sections for backends that require 5718 that be done before setup_gnu_properties. */ 5719 if (!_bfd_elf_link_create_dynamic_sections (abfd, info)) 5720 goto error_free_vers; 5721 add_needed = true; 5722 } 5723 else if (dynamic 5724 && h->root.u.def.section->owner == abfd) 5725 { 5726 /* Add this symbol to first hash if this shared 5727 object has the first definition. */ 5728 elf_link_add_to_first_hash (abfd, info, name, must_copy_name); 5729 /* And if it was the default symbol version definition, 5730 also add the short name. */ 5731 if (defvername) 5732 elf_link_add_to_first_hash (abfd, info, defvername, false); 5733 } 5734 } 5735 } 5736 } 5737 5738 if (info->lto_plugin_active 5739 && !bfd_link_relocatable (info) 5740 && (abfd->flags & BFD_PLUGIN) == 0 5741 && !just_syms 5742 && extsymcount != 0 5743 && is_elf_hash_table (&htab->root)) 5744 { 5745 int r_sym_shift; 5746 5747 if (bed->s->arch_size == 32) 5748 r_sym_shift = 8; 5749 else 5750 r_sym_shift = 32; 5751 5752 /* If linker plugin is enabled, set non_ir_ref_regular on symbols 5753 referenced in regular objects so that linker plugin will get 5754 the correct symbol resolution. */ 5755 5756 sym_hash = elf_sym_hashes (abfd); 5757 for (s = abfd->sections; s != NULL; s = s->next) 5758 { 5759 Elf_Internal_Rela *internal_relocs; 5760 Elf_Internal_Rela *rel, *relend; 5761 5762 /* Don't check relocations in excluded sections. */ 5763 if ((s->flags & SEC_RELOC) == 0 5764 || s->reloc_count == 0 5765 || (s->flags & SEC_EXCLUDE) != 0 5766 || (s->flags & SEC_DEBUGGING) != 0) 5767 continue; 5768 5769 internal_relocs = _bfd_elf_link_info_read_relocs 5770 (abfd, info, s, NULL, NULL, 5771 _bfd_elf_link_keep_memory (info)); 5772 if (internal_relocs == NULL) 5773 goto error_free_vers; 5774 5775 rel = internal_relocs; 5776 relend = rel + s->reloc_count; 5777 for ( ; rel < relend; rel++) 5778 { 5779 unsigned long r_symndx = rel->r_info >> r_sym_shift; 5780 struct elf_link_hash_entry *h; 5781 5782 /* Skip local symbols. */ 5783 if (r_symndx < extsymoff) 5784 continue; 5785 5786 h = sym_hash[r_symndx - extsymoff]; 5787 if (h != NULL) 5788 h->root.non_ir_ref_regular = 1; 5789 } 5790 5791 if (elf_section_data (s)->relocs != internal_relocs) 5792 free (internal_relocs); 5793 } 5794 } 5795 5796 free (extversym); 5797 extversym = NULL; 5798 free (isymbuf); 5799 isymbuf = NULL; 5800 5801 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 5802 { 5803 unsigned int i; 5804 5805 /* Restore the symbol table. */ 5806 old_ent = (char *) old_tab + tabsize; 5807 memset (elf_sym_hashes (abfd), 0, 5808 extsymcount * sizeof (struct elf_link_hash_entry *)); 5809 htab->root.table.table = old_table; 5810 htab->root.table.size = old_size; 5811 htab->root.table.count = old_count; 5812 memcpy (htab->root.table.table, old_tab, tabsize); 5813 htab->root.undefs = old_undefs; 5814 htab->root.undefs_tail = old_undefs_tail; 5815 if (htab->dynstr != NULL) 5816 _bfd_elf_strtab_restore (htab->dynstr, old_strtab); 5817 free (old_strtab); 5818 old_strtab = NULL; 5819 for (i = 0; i < htab->root.table.size; i++) 5820 { 5821 struct bfd_hash_entry *p; 5822 struct elf_link_hash_entry *h; 5823 unsigned int non_ir_ref_dynamic; 5824 5825 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 5826 { 5827 /* Preserve non_ir_ref_dynamic so that this symbol 5828 will be exported when the dynamic lib becomes needed 5829 in the second pass. */ 5830 h = (struct elf_link_hash_entry *) p; 5831 if (h->root.type == bfd_link_hash_warning) 5832 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5833 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; 5834 5835 h = (struct elf_link_hash_entry *) p; 5836 memcpy (h, old_ent, htab->root.table.entsize); 5837 old_ent = (char *) old_ent + htab->root.table.entsize; 5838 if (h->root.type == bfd_link_hash_warning) 5839 { 5840 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5841 memcpy (h, old_ent, htab->root.table.entsize); 5842 old_ent = (char *) old_ent + htab->root.table.entsize; 5843 } 5844 if (h->root.type == bfd_link_hash_common) 5845 { 5846 memcpy (h->root.u.c.p, old_ent, sizeof (*h->root.u.c.p)); 5847 old_ent = (char *) old_ent + sizeof (*h->root.u.c.p); 5848 } 5849 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; 5850 } 5851 } 5852 5853 /* Make a special call to the linker "notice" function to 5854 tell it that symbols added for crefs may need to be removed. */ 5855 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) 5856 goto error_free_vers; 5857 5858 free (old_tab); 5859 objalloc_free_block ((struct objalloc *) htab->root.table.memory, 5860 alloc_mark); 5861 free (nondeflt_vers); 5862 return true; 5863 } 5864 5865 free (old_strtab); 5866 old_strtab = NULL; 5867 if (old_tab != NULL) 5868 { 5869 if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) 5870 goto error_free_vers; 5871 free (old_tab); 5872 old_tab = NULL; 5873 } 5874 5875 /* Now that all the symbols from this input file are created, if 5876 not performing a relocatable link, handle .symver foo, foo@BAR 5877 such that any relocs against foo become foo@BAR. */ 5878 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) 5879 { 5880 size_t cnt, symidx; 5881 5882 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) 5883 { 5884 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; 5885 char *shortname, *p; 5886 size_t amt; 5887 5888 p = strchr (h->root.root.string, ELF_VER_CHR); 5889 if (p == NULL 5890 || (h->root.type != bfd_link_hash_defined 5891 && h->root.type != bfd_link_hash_defweak)) 5892 continue; 5893 5894 amt = p - h->root.root.string; 5895 shortname = (char *) bfd_malloc (amt + 1); 5896 if (!shortname) 5897 goto error_free_vers; 5898 memcpy (shortname, h->root.root.string, amt); 5899 shortname[amt] = '\0'; 5900 5901 hi = (struct elf_link_hash_entry *) 5902 bfd_link_hash_lookup (&htab->root, shortname, 5903 false, false, false); 5904 if (hi != NULL 5905 && hi->root.type == h->root.type 5906 && hi->root.u.def.value == h->root.u.def.value 5907 && hi->root.u.def.section == h->root.u.def.section) 5908 { 5909 (*bed->elf_backend_hide_symbol) (info, hi, true); 5910 hi->root.type = bfd_link_hash_indirect; 5911 hi->root.u.i.link = (struct bfd_link_hash_entry *) h; 5912 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 5913 sym_hash = elf_sym_hashes (abfd); 5914 if (sym_hash) 5915 for (symidx = 0; symidx < extsymcount; ++symidx) 5916 if (sym_hash[symidx] == hi) 5917 { 5918 sym_hash[symidx] = h; 5919 break; 5920 } 5921 } 5922 free (shortname); 5923 } 5924 } 5925 free (nondeflt_vers); 5926 nondeflt_vers = NULL; 5927 5928 /* Now set the alias field correctly for all the weak defined 5929 symbols we found. The only way to do this is to search all the 5930 symbols. Since we only need the information for non functions in 5931 dynamic objects, that's the only time we actually put anything on 5932 the list WEAKS. We need this information so that if a regular 5933 object refers to a symbol defined weakly in a dynamic object, the 5934 real symbol in the dynamic object is also put in the dynamic 5935 symbols; we also must arrange for both symbols to point to the 5936 same memory location. We could handle the general case of symbol 5937 aliasing, but a general symbol alias can only be generated in 5938 assembler code, handling it correctly would be very time 5939 consuming, and other ELF linkers don't handle general aliasing 5940 either. */ 5941 if (weaks != NULL) 5942 { 5943 struct elf_link_hash_entry **hpp; 5944 struct elf_link_hash_entry **hppend; 5945 struct elf_link_hash_entry **sorted_sym_hash; 5946 struct elf_link_hash_entry *h; 5947 size_t sym_count, amt; 5948 5949 /* Since we have to search the whole symbol list for each weak 5950 defined symbol, search time for N weak defined symbols will be 5951 O(N^2). Binary search will cut it down to O(NlogN). */ 5952 amt = extsymcount * sizeof (*sorted_sym_hash); 5953 sorted_sym_hash = bfd_malloc (amt); 5954 if (sorted_sym_hash == NULL) 5955 goto error_return; 5956 sym_hash = sorted_sym_hash; 5957 hpp = elf_sym_hashes (abfd); 5958 hppend = hpp + extsymcount; 5959 sym_count = 0; 5960 for (; hpp < hppend; hpp++) 5961 { 5962 h = *hpp; 5963 if (h != NULL 5964 && h->root.type == bfd_link_hash_defined 5965 && !bed->is_function_type (h->type)) 5966 { 5967 *sym_hash = h; 5968 sym_hash++; 5969 sym_count++; 5970 } 5971 } 5972 5973 qsort (sorted_sym_hash, sym_count, sizeof (*sorted_sym_hash), 5974 elf_sort_symbol); 5975 5976 while (weaks != NULL) 5977 { 5978 struct elf_link_hash_entry *hlook; 5979 asection *slook; 5980 bfd_vma vlook; 5981 size_t i, j, idx = 0; 5982 5983 hlook = weaks; 5984 weaks = hlook->u.alias; 5985 hlook->u.alias = NULL; 5986 5987 if (hlook->root.type != bfd_link_hash_defined 5988 && hlook->root.type != bfd_link_hash_defweak) 5989 continue; 5990 5991 slook = hlook->root.u.def.section; 5992 vlook = hlook->root.u.def.value; 5993 5994 i = 0; 5995 j = sym_count; 5996 while (i != j) 5997 { 5998 bfd_signed_vma vdiff; 5999 idx = (i + j) / 2; 6000 h = sorted_sym_hash[idx]; 6001 vdiff = vlook - h->root.u.def.value; 6002 if (vdiff < 0) 6003 j = idx; 6004 else if (vdiff > 0) 6005 i = idx + 1; 6006 else 6007 { 6008 int sdiff = slook->id - h->root.u.def.section->id; 6009 if (sdiff < 0) 6010 j = idx; 6011 else if (sdiff > 0) 6012 i = idx + 1; 6013 else 6014 break; 6015 } 6016 } 6017 6018 /* We didn't find a value/section match. */ 6019 if (i == j) 6020 continue; 6021 6022 /* With multiple aliases, or when the weak symbol is already 6023 strongly defined, we have multiple matching symbols and 6024 the binary search above may land on any of them. Step 6025 one past the matching symbol(s). */ 6026 while (++idx != j) 6027 { 6028 h = sorted_sym_hash[idx]; 6029 if (h->root.u.def.section != slook 6030 || h->root.u.def.value != vlook) 6031 break; 6032 } 6033 6034 /* Now look back over the aliases. Since we sorted by size 6035 as well as value and section, we'll choose the one with 6036 the largest size. */ 6037 while (idx-- != i) 6038 { 6039 h = sorted_sym_hash[idx]; 6040 6041 /* Stop if value or section doesn't match. */ 6042 if (h->root.u.def.section != slook 6043 || h->root.u.def.value != vlook) 6044 break; 6045 else if (h != hlook) 6046 { 6047 struct elf_link_hash_entry *t; 6048 6049 hlook->u.alias = h; 6050 hlook->is_weakalias = 1; 6051 t = h; 6052 if (t->u.alias != NULL) 6053 while (t->u.alias != h) 6054 t = t->u.alias; 6055 t->u.alias = hlook; 6056 6057 /* If the weak definition is in the list of dynamic 6058 symbols, make sure the real definition is put 6059 there as well. */ 6060 if (hlook->dynindx != -1 && h->dynindx == -1) 6061 { 6062 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6063 { 6064 err_free_sym_hash: 6065 free (sorted_sym_hash); 6066 goto error_return; 6067 } 6068 } 6069 6070 /* If the real definition is in the list of dynamic 6071 symbols, make sure the weak definition is put 6072 there as well. If we don't do this, then the 6073 dynamic loader might not merge the entries for the 6074 real definition and the weak definition. */ 6075 if (h->dynindx != -1 && hlook->dynindx == -1) 6076 { 6077 if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) 6078 goto err_free_sym_hash; 6079 } 6080 break; 6081 } 6082 } 6083 } 6084 6085 free (sorted_sym_hash); 6086 } 6087 6088 if (bed->check_directives 6089 && !(*bed->check_directives) (abfd, info)) 6090 goto error_return; 6091 6092 /* If this is a non-traditional link, try to optimize the handling 6093 of the .stab/.stabstr sections. */ 6094 if (! dynamic 6095 && ! info->traditional_format 6096 && is_elf_hash_table (&htab->root) 6097 && (info->strip != strip_all && info->strip != strip_debugger)) 6098 { 6099 asection *stabstr; 6100 6101 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 6102 if (stabstr != NULL) 6103 { 6104 bfd_size_type string_offset = 0; 6105 asection *stab; 6106 6107 for (stab = abfd->sections; stab; stab = stab->next) 6108 if (startswith (stab->name, ".stab") 6109 && (!stab->name[5] || 6110 (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) 6111 && (stab->flags & SEC_MERGE) == 0 6112 && !bfd_is_abs_section (stab->output_section)) 6113 { 6114 struct bfd_elf_section_data *secdata; 6115 6116 secdata = elf_section_data (stab); 6117 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, 6118 stabstr, &secdata->sec_info, 6119 &string_offset)) 6120 goto error_return; 6121 if (secdata->sec_info) 6122 stab->sec_info_type = SEC_INFO_TYPE_STABS; 6123 } 6124 } 6125 } 6126 6127 if (dynamic && add_needed) 6128 { 6129 /* Add this bfd to the loaded list. */ 6130 struct elf_link_loaded_list *n; 6131 6132 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); 6133 if (n == NULL) 6134 goto error_return; 6135 n->abfd = abfd; 6136 n->next = htab->dyn_loaded; 6137 htab->dyn_loaded = n; 6138 } 6139 if (dynamic && !add_needed 6140 && (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) != 0) 6141 elf_dyn_lib_class (abfd) |= DYN_NO_NEEDED; 6142 6143 return true; 6144 6145 error_free_vers: 6146 free (old_tab); 6147 free (old_strtab); 6148 free (nondeflt_vers); 6149 free (extversym); 6150 error_free_sym: 6151 free (isymbuf); 6152 error_return: 6153 return false; 6154 } 6155 6156 /* Return the linker hash table entry of a symbol that might be 6157 satisfied by an archive symbol. Return -1 on error. */ 6158 6159 struct bfd_link_hash_entry * 6160 _bfd_elf_archive_symbol_lookup (bfd *abfd, 6161 struct bfd_link_info *info, 6162 const char *name) 6163 { 6164 struct bfd_link_hash_entry *h; 6165 char *p, *copy; 6166 size_t len, first; 6167 6168 h = bfd_link_hash_lookup (info->hash, name, false, false, true); 6169 if (h != NULL) 6170 return h; 6171 6172 /* If this is a default version (the name contains @@), look up the 6173 symbol again with only one `@' as well as without the version. 6174 The effect is that references to the symbol with and without the 6175 version will be matched by the default symbol in the archive. */ 6176 6177 p = strchr (name, ELF_VER_CHR); 6178 if (p == NULL || p[1] != ELF_VER_CHR) 6179 { 6180 /* Add this symbol to first hash if this archive has the first 6181 definition. */ 6182 if (is_elf_hash_table (info->hash)) 6183 elf_link_add_to_first_hash (abfd, info, name, false); 6184 return h; 6185 } 6186 6187 /* First check with only one `@'. */ 6188 len = strlen (name); 6189 copy = (char *) bfd_alloc (abfd, len); 6190 if (copy == NULL) 6191 return (struct bfd_link_hash_entry *) -1; 6192 6193 first = p - name + 1; 6194 memcpy (copy, name, first); 6195 memcpy (copy + first, name + first + 1, len - first); 6196 6197 h = bfd_link_hash_lookup (info->hash, copy, false, false, true); 6198 if (h == NULL) 6199 { 6200 /* We also need to check references to the symbol without the 6201 version. */ 6202 copy[first - 1] = '\0'; 6203 h = bfd_link_hash_lookup (info->hash, copy, false, false, true); 6204 } 6205 6206 bfd_release (abfd, copy); 6207 return h; 6208 } 6209 6210 /* Add symbols from an ELF archive file to the linker hash table. We 6211 don't use _bfd_generic_link_add_archive_symbols because we need to 6212 handle versioned symbols. 6213 6214 Fortunately, ELF archive handling is simpler than that done by 6215 _bfd_generic_link_add_archive_symbols, which has to allow for a.out 6216 oddities. In ELF, if we find a symbol in the archive map, and the 6217 symbol is currently undefined, we know that we must pull in that 6218 object file. 6219 6220 Unfortunately, we do have to make multiple passes over the symbol 6221 table until nothing further is resolved. */ 6222 6223 static bool 6224 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 6225 { 6226 symindex c; 6227 unsigned char *included = NULL; 6228 carsym *symdefs; 6229 bool loop; 6230 size_t amt; 6231 const struct elf_backend_data *bed; 6232 struct bfd_link_hash_entry * (*archive_symbol_lookup) 6233 (bfd *, struct bfd_link_info *, const char *); 6234 6235 if (! bfd_has_map (abfd)) 6236 { 6237 /* An empty archive is a special case. */ 6238 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 6239 return true; 6240 bfd_set_error (bfd_error_no_armap); 6241 return false; 6242 } 6243 6244 /* Keep track of all symbols we know to be already defined, and all 6245 files we know to be already included. This is to speed up the 6246 second and subsequent passes. */ 6247 c = bfd_ardata (abfd)->symdef_count; 6248 if (c == 0) 6249 return true; 6250 amt = c * sizeof (*included); 6251 included = (unsigned char *) bfd_zmalloc (amt); 6252 if (included == NULL) 6253 return false; 6254 6255 symdefs = bfd_ardata (abfd)->symdefs; 6256 bed = get_elf_backend_data (abfd); 6257 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; 6258 6259 do 6260 { 6261 file_ptr last; 6262 symindex i; 6263 carsym *symdef; 6264 carsym *symdefend; 6265 6266 loop = false; 6267 last = -1; 6268 6269 symdef = symdefs; 6270 symdefend = symdef + c; 6271 for (i = 0; symdef < symdefend; symdef++, i++) 6272 { 6273 struct bfd_link_hash_entry *h; 6274 bfd *element; 6275 struct bfd_link_hash_entry *undefs_tail; 6276 symindex mark; 6277 6278 if (included[i]) 6279 continue; 6280 if (symdef->file_offset == last) 6281 { 6282 included[i] = true; 6283 continue; 6284 } 6285 6286 h = archive_symbol_lookup (abfd, info, symdef->name); 6287 if (h == (struct bfd_link_hash_entry *) -1) 6288 goto error_return; 6289 6290 if (h == NULL) 6291 continue; 6292 6293 if (h->type == bfd_link_hash_undefined) 6294 { 6295 if (is_elf_hash_table (info->hash)) 6296 { 6297 /* If the archive element has already been loaded then one 6298 of the symbols defined by that element might have been 6299 made undefined due to being in a discarded section. */ 6300 if (((struct elf_link_hash_entry *) h)->indx == -3) 6301 continue; 6302 6303 /* In the pre-LTO-plugin pass we must not mistakenly 6304 include this archive member if an earlier shared 6305 library defined this symbol. */ 6306 struct elf_link_hash_table *htab = elf_hash_table (info); 6307 if (htab->first_hash) 6308 { 6309 struct elf_link_first_hash_entry *e 6310 = ((struct elf_link_first_hash_entry *) 6311 bfd_hash_lookup (htab->first_hash, symdef->name, 6312 false, false)); 6313 if (e 6314 && (e->abfd->flags & DYNAMIC) != 0 6315 && e->abfd != abfd) 6316 continue; 6317 } 6318 } 6319 } 6320 else if (h->type == bfd_link_hash_common) 6321 { 6322 /* We currently have a common symbol. The archive map contains 6323 a reference to this symbol, so we may want to include it. We 6324 only want to include it however, if this archive element 6325 contains a definition of the symbol, not just another common 6326 declaration of it. 6327 6328 Unfortunately some archivers (including GNU ar) will put 6329 declarations of common symbols into their archive maps, as 6330 well as real definitions, so we cannot just go by the archive 6331 map alone. Instead we must read in the element's symbol 6332 table and check that to see what kind of symbol definition 6333 this is. */ 6334 if (! elf_link_is_defined_archive_symbol (abfd, symdef)) 6335 continue; 6336 } 6337 else 6338 { 6339 if (h->type != bfd_link_hash_undefweak) 6340 /* Symbol must be defined. Don't check it again. */ 6341 included[i] = true; 6342 6343 if (!is_elf_hash_table (info->hash)) 6344 continue; 6345 struct elf_link_hash_entry *eh 6346 = (struct elf_link_hash_entry *) h; 6347 /* Ignore the archive if the symbol isn't referenced by a 6348 regular object or isn't defined in a shared object. */ 6349 if (!eh->ref_regular || !eh->def_dynamic) 6350 continue; 6351 /* Ignore the dynamic definition if symbol is first 6352 defined in this archive. */ 6353 struct elf_link_hash_table *htab = elf_hash_table (info); 6354 if (htab->first_hash == NULL) 6355 continue; 6356 struct elf_link_first_hash_entry *e 6357 = ((struct elf_link_first_hash_entry *) 6358 bfd_hash_lookup (htab->first_hash, symdef->name, 6359 false, false)); 6360 if (e == NULL || e->abfd != abfd) 6361 continue; 6362 } 6363 6364 /* We need to include this archive member. */ 6365 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, 6366 info); 6367 if (element == NULL) 6368 goto error_return; 6369 6370 if (! bfd_check_format (element, bfd_object)) 6371 goto error_return; 6372 6373 undefs_tail = info->hash->undefs_tail; 6374 6375 if (!(*info->callbacks 6376 ->add_archive_element) (info, element, symdef->name, &element)) 6377 continue; 6378 if (!bfd_link_add_symbols (element, info)) 6379 goto error_return; 6380 6381 /* If there are any new undefined symbols, we need to make 6382 another pass through the archive in order to see whether 6383 they can be defined. FIXME: This isn't perfect, because 6384 common symbols wind up on undefs_tail and because an 6385 undefined symbol which is defined later on in this pass 6386 does not require another pass. This isn't a bug, but it 6387 does make the code less efficient than it could be. */ 6388 if (undefs_tail != info->hash->undefs_tail) 6389 loop = true; 6390 6391 /* Look backward to mark all symbols from this object file 6392 which we have already seen in this pass. */ 6393 mark = i; 6394 do 6395 { 6396 included[mark] = true; 6397 if (mark == 0) 6398 break; 6399 --mark; 6400 } 6401 while (symdefs[mark].file_offset == symdef->file_offset); 6402 6403 /* We mark subsequent symbols from this object file as we go 6404 on through the loop. */ 6405 last = symdef->file_offset; 6406 } 6407 } 6408 while (loop); 6409 6410 free (included); 6411 return true; 6412 6413 error_return: 6414 free (included); 6415 return false; 6416 } 6417 6418 /* Given an ELF BFD, add symbols to the global hash table as 6419 appropriate. */ 6420 6421 bool 6422 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 6423 { 6424 switch (bfd_get_format (abfd)) 6425 { 6426 case bfd_object: 6427 return elf_link_add_object_symbols (abfd, info); 6428 case bfd_archive: 6429 return elf_link_add_archive_symbols (abfd, info); 6430 default: 6431 bfd_set_error (bfd_error_wrong_format); 6432 return false; 6433 } 6434 } 6435 6436 struct hash_codes_info 6438 { 6439 unsigned long *hashcodes; 6440 bool error; 6441 }; 6442 6443 /* This function will be called though elf_link_hash_traverse to store 6444 all hash value of the exported symbols in an array. */ 6445 6446 static bool 6447 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) 6448 { 6449 struct hash_codes_info *inf = (struct hash_codes_info *) data; 6450 const char *name; 6451 unsigned long ha; 6452 char *alc = NULL; 6453 6454 /* Ignore indirect symbols. These are added by the versioning code. */ 6455 if (h->dynindx == -1) 6456 return true; 6457 6458 name = h->root.root.string; 6459 if (h->versioned >= versioned) 6460 { 6461 char *p = strchr (name, ELF_VER_CHR); 6462 if (p != NULL) 6463 { 6464 alc = (char *) bfd_malloc (p - name + 1); 6465 if (alc == NULL) 6466 { 6467 inf->error = true; 6468 return false; 6469 } 6470 memcpy (alc, name, p - name); 6471 alc[p - name] = '\0'; 6472 name = alc; 6473 } 6474 } 6475 6476 /* Compute the hash value. */ 6477 ha = bfd_elf_hash (name); 6478 6479 /* Store the found hash value in the array given as the argument. */ 6480 *(inf->hashcodes)++ = ha; 6481 6482 /* And store it in the struct so that we can put it in the hash table 6483 later. */ 6484 h->u.elf_hash_value = ha; 6485 6486 free (alc); 6487 return true; 6488 } 6489 6490 struct collect_gnu_hash_codes 6491 { 6492 bfd *output_bfd; 6493 const struct elf_backend_data *bed; 6494 unsigned long int nsyms; 6495 unsigned long int maskbits; 6496 unsigned long int *hashcodes; 6497 unsigned long int *hashval; 6498 unsigned long int *indx; 6499 unsigned long int *counts; 6500 bfd_vma *bitmask; 6501 bfd_byte *contents; 6502 bfd_size_type xlat; 6503 long int min_dynindx; 6504 unsigned long int bucketcount; 6505 unsigned long int symindx; 6506 long int local_indx; 6507 long int shift1, shift2; 6508 unsigned long int mask; 6509 bool error; 6510 }; 6511 6512 /* This function will be called though elf_link_hash_traverse to store 6513 all hash value of the exported symbols in an array. */ 6514 6515 static bool 6516 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) 6517 { 6518 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 6519 const char *name; 6520 unsigned long ha; 6521 char *alc = NULL; 6522 6523 /* Ignore indirect symbols. These are added by the versioning code. */ 6524 if (h->dynindx == -1) 6525 return true; 6526 6527 /* Ignore also local symbols and undefined symbols. */ 6528 if (! (*s->bed->elf_hash_symbol) (h)) 6529 return true; 6530 6531 name = h->root.root.string; 6532 if (h->versioned >= versioned) 6533 { 6534 char *p = strchr (name, ELF_VER_CHR); 6535 if (p != NULL) 6536 { 6537 alc = (char *) bfd_malloc (p - name + 1); 6538 if (alc == NULL) 6539 { 6540 s->error = true; 6541 return false; 6542 } 6543 memcpy (alc, name, p - name); 6544 alc[p - name] = '\0'; 6545 name = alc; 6546 } 6547 } 6548 6549 /* Compute the hash value. */ 6550 ha = bfd_elf_gnu_hash (name); 6551 6552 /* Store the found hash value in the array for compute_bucket_count, 6553 and also for .dynsym reordering purposes. */ 6554 s->hashcodes[s->nsyms] = ha; 6555 s->hashval[h->dynindx] = ha; 6556 ++s->nsyms; 6557 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) 6558 s->min_dynindx = h->dynindx; 6559 6560 free (alc); 6561 return true; 6562 } 6563 6564 /* This function will be called though elf_link_hash_traverse to do 6565 final dynamic symbol renumbering in case of .gnu.hash. 6566 If using .MIPS.xhash, invoke record_xhash_symbol to add symbol index 6567 to the translation table. */ 6568 6569 static bool 6570 elf_gnu_hash_process_symidx (struct elf_link_hash_entry *h, void *data) 6571 { 6572 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 6573 unsigned long int bucket; 6574 unsigned long int val; 6575 6576 /* Ignore indirect symbols. */ 6577 if (h->dynindx == -1) 6578 return true; 6579 6580 /* Ignore also local symbols and undefined symbols. */ 6581 if (! (*s->bed->elf_hash_symbol) (h)) 6582 { 6583 if (h->dynindx >= s->min_dynindx) 6584 { 6585 if (s->bed->record_xhash_symbol != NULL) 6586 { 6587 (*s->bed->record_xhash_symbol) (h, 0); 6588 s->local_indx++; 6589 } 6590 else 6591 h->dynindx = s->local_indx++; 6592 } 6593 return true; 6594 } 6595 6596 bucket = s->hashval[h->dynindx] % s->bucketcount; 6597 val = (s->hashval[h->dynindx] >> s->shift1) 6598 & ((s->maskbits >> s->shift1) - 1); 6599 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); 6600 s->bitmask[val] 6601 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); 6602 val = s->hashval[h->dynindx] & ~(unsigned long int) 1; 6603 if (s->counts[bucket] == 1) 6604 /* Last element terminates the chain. */ 6605 val |= 1; 6606 bfd_put_32 (s->output_bfd, val, 6607 s->contents + (s->indx[bucket] - s->symindx) * 4); 6608 --s->counts[bucket]; 6609 if (s->bed->record_xhash_symbol != NULL) 6610 { 6611 bfd_vma xlat_loc = s->xlat + (s->indx[bucket]++ - s->symindx) * 4; 6612 6613 (*s->bed->record_xhash_symbol) (h, xlat_loc); 6614 } 6615 else 6616 h->dynindx = s->indx[bucket]++; 6617 return true; 6618 } 6619 6620 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 6621 6622 bool 6623 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) 6624 { 6625 return !(h->forced_local 6626 || h->root.type == bfd_link_hash_undefined 6627 || h->root.type == bfd_link_hash_undefweak 6628 || ((h->root.type == bfd_link_hash_defined 6629 || h->root.type == bfd_link_hash_defweak) 6630 && h->root.u.def.section->output_section == NULL)); 6631 } 6632 6633 /* Array used to determine the number of hash table buckets to use 6634 based on the number of symbols there are. If there are fewer than 6635 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, 6636 fewer than 37 we use 17 buckets, and so forth. We never use more 6637 than 32771 buckets. */ 6638 6639 static const size_t elf_buckets[] = 6640 { 6641 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 6642 16411, 32771, 0 6643 }; 6644 6645 /* Compute bucket count for hashing table. We do not use a static set 6646 of possible tables sizes anymore. Instead we determine for all 6647 possible reasonable sizes of the table the outcome (i.e., the 6648 number of collisions etc) and choose the best solution. The 6649 weighting functions are not too simple to allow the table to grow 6650 without bounds. Instead one of the weighting factors is the size. 6651 Therefore the result is always a good payoff between few collisions 6652 (= short chain lengths) and table size. */ 6653 static size_t 6654 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, 6655 unsigned long int *hashcodes ATTRIBUTE_UNUSED, 6656 unsigned long int nsyms, 6657 int gnu_hash) 6658 { 6659 size_t best_size = 0; 6660 unsigned long int i; 6661 6662 if (info->optimize) 6663 { 6664 size_t minsize; 6665 size_t maxsize; 6666 uint64_t best_chlen = ~((uint64_t) 0); 6667 bfd *dynobj = elf_hash_table (info)->dynobj; 6668 size_t dynsymcount = elf_hash_table (info)->dynsymcount; 6669 const struct elf_backend_data *bed = get_elf_backend_data (dynobj); 6670 unsigned long int *counts; 6671 bfd_size_type amt; 6672 unsigned int no_improvement_count = 0; 6673 6674 /* Possible optimization parameters: if we have NSYMS symbols we say 6675 that the hashing table must at least have NSYMS/4 and at most 6676 2*NSYMS buckets. */ 6677 minsize = nsyms / 4; 6678 if (minsize == 0) 6679 minsize = 1; 6680 best_size = maxsize = nsyms * 2; 6681 if (gnu_hash) 6682 { 6683 if (minsize < 2) 6684 minsize = 2; 6685 if ((best_size & 31) == 0) 6686 ++best_size; 6687 } 6688 6689 /* Create array where we count the collisions in. We must use bfd_malloc 6690 since the size could be large. */ 6691 amt = maxsize; 6692 amt *= sizeof (unsigned long int); 6693 counts = (unsigned long int *) bfd_malloc (amt); 6694 if (counts == NULL) 6695 return 0; 6696 6697 /* Compute the "optimal" size for the hash table. The criteria is a 6698 minimal chain length. The minor criteria is (of course) the size 6699 of the table. */ 6700 for (i = minsize; i < maxsize; ++i) 6701 { 6702 /* Walk through the array of hashcodes and count the collisions. */ 6703 uint64_t max; 6704 unsigned long int j; 6705 unsigned long int fact; 6706 6707 if (gnu_hash && (i & 31) == 0) 6708 continue; 6709 6710 memset (counts, '\0', i * sizeof (unsigned long int)); 6711 6712 /* Determine how often each hash bucket is used. */ 6713 for (j = 0; j < nsyms; ++j) 6714 ++counts[hashcodes[j] % i]; 6715 6716 /* For the weight function we need some information about the 6717 pagesize on the target. This is information need not be 100% 6718 accurate. Since this information is not available (so far) we 6719 define it here to a reasonable default value. If it is crucial 6720 to have a better value some day simply define this value. */ 6721 # ifndef BFD_TARGET_PAGESIZE 6722 # define BFD_TARGET_PAGESIZE (4096) 6723 # endif 6724 6725 /* We in any case need 2 + DYNSYMCOUNT entries for the size values 6726 and the chains. */ 6727 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; 6728 6729 # if 1 6730 /* Variant 1: optimize for short chains. We add the squares 6731 of all the chain lengths (which favors many small chain 6732 over a few long chains). */ 6733 for (j = 0; j < i; ++j) 6734 max += counts[j] * counts[j]; 6735 6736 /* This adds penalties for the overall size of the table. */ 6737 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 6738 max *= fact * fact; 6739 # else 6740 /* Variant 2: Optimize a lot more for small table. Here we 6741 also add squares of the size but we also add penalties for 6742 empty slots (the +1 term). */ 6743 for (j = 0; j < i; ++j) 6744 max += (1 + counts[j]) * (1 + counts[j]); 6745 6746 /* The overall size of the table is considered, but not as 6747 strong as in variant 1, where it is squared. */ 6748 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 6749 max *= fact; 6750 # endif 6751 6752 /* Compare with current best results. */ 6753 if (max < best_chlen) 6754 { 6755 best_chlen = max; 6756 best_size = i; 6757 no_improvement_count = 0; 6758 } 6759 /* PR 11843: Avoid futile long searches for the best bucket size 6760 when there are a large number of symbols. */ 6761 else if (++no_improvement_count == 100) 6762 break; 6763 } 6764 6765 free (counts); 6766 } 6767 else 6768 { 6769 for (i = 0; elf_buckets[i] != 0; i++) 6770 { 6771 best_size = elf_buckets[i]; 6772 if (nsyms < elf_buckets[i + 1]) 6773 break; 6774 } 6775 if (gnu_hash && best_size < 2) 6776 best_size = 2; 6777 } 6778 6779 return best_size; 6780 } 6781 6782 /* Size any SHT_GROUP section for ld -r. */ 6783 6784 bool 6785 _bfd_elf_size_group_sections (struct bfd_link_info *info) 6786 { 6787 bfd *ibfd; 6788 asection *s; 6789 6790 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 6791 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour 6792 && (s = ibfd->sections) != NULL 6793 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS 6794 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) 6795 return false; 6796 return true; 6797 } 6798 6799 /* Set a default stack segment size. The value in INFO wins. If it 6800 is unset, LEGACY_SYMBOL's value is used, and if that symbol is 6801 undefined it is initialized. */ 6802 6803 bool 6804 bfd_elf_stack_segment_size (bfd *output_bfd, 6805 struct bfd_link_info *info, 6806 const char *legacy_symbol, 6807 bfd_vma default_size) 6808 { 6809 struct elf_link_hash_entry *h = NULL; 6810 6811 /* Look for legacy symbol. */ 6812 if (legacy_symbol) 6813 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, 6814 false, false, false); 6815 if (h && (h->root.type == bfd_link_hash_defined 6816 || h->root.type == bfd_link_hash_defweak) 6817 && h->def_regular 6818 && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) 6819 { 6820 /* The symbol has no type if specified on the command line. */ 6821 h->type = STT_OBJECT; 6822 if (info->stacksize) 6823 /* xgettext:c-format */ 6824 _bfd_error_handler (_("%pB: stack size specified and %s set"), 6825 output_bfd, legacy_symbol); 6826 else if (h->root.u.def.section != bfd_abs_section_ptr) 6827 /* xgettext:c-format */ 6828 _bfd_error_handler (_("%pB: %s not absolute"), 6829 output_bfd, legacy_symbol); 6830 else 6831 info->stacksize = h->root.u.def.value; 6832 } 6833 6834 if (!info->stacksize) 6835 /* If the user didn't set a size, or explicitly inhibit the 6836 size, set it now. */ 6837 info->stacksize = default_size; 6838 6839 /* Provide the legacy symbol, if it is referenced. */ 6840 if (h && (h->root.type == bfd_link_hash_undefined 6841 || h->root.type == bfd_link_hash_undefweak)) 6842 { 6843 struct bfd_link_hash_entry *bh = NULL; 6844 6845 if (!(_bfd_generic_link_add_one_symbol 6846 (info, output_bfd, legacy_symbol, 6847 BSF_GLOBAL, bfd_abs_section_ptr, 6848 info->stacksize >= 0 ? info->stacksize : 0, 6849 NULL, false, get_elf_backend_data (output_bfd)->collect, &bh))) 6850 return false; 6851 6852 h = (struct elf_link_hash_entry *) bh; 6853 h->def_regular = 1; 6854 h->type = STT_OBJECT; 6855 } 6856 6857 return true; 6858 } 6859 6860 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ 6861 6862 struct elf_gc_sweep_symbol_info 6863 { 6864 struct bfd_link_info *info; 6865 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, 6866 bool); 6867 }; 6868 6869 static bool 6870 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) 6871 { 6872 if (!h->mark 6873 && (((h->root.type == bfd_link_hash_defined 6874 || h->root.type == bfd_link_hash_defweak) 6875 && !((h->def_regular || ELF_COMMON_DEF_P (h)) 6876 && h->root.u.def.section->gc_mark)) 6877 || h->root.type == bfd_link_hash_undefined 6878 || h->root.type == bfd_link_hash_undefweak)) 6879 { 6880 struct elf_gc_sweep_symbol_info *inf; 6881 6882 inf = (struct elf_gc_sweep_symbol_info *) data; 6883 (*inf->hide_symbol) (inf->info, h, true); 6884 h->def_regular = 0; 6885 h->ref_regular = 0; 6886 h->ref_regular_nonweak = 0; 6887 } 6888 6889 return true; 6890 } 6891 6892 /* Set up the sizes and contents of the ELF dynamic sections. This is 6893 called by the ELF linker emulation before_allocation routine. We 6894 must set the sizes of the sections before the linker sets the 6895 addresses of the various sections. */ 6896 6897 bool 6898 bfd_elf_size_dynamic_sections (bfd *output_bfd, 6899 const char *soname, 6900 const char *rpath, 6901 const char *filter_shlib, 6902 const char *audit, 6903 const char *depaudit, 6904 const char * const *auxiliary_filters, 6905 struct bfd_link_info *info, 6906 asection **sinterpptr) 6907 { 6908 bfd *dynobj; 6909 const struct elf_backend_data *bed; 6910 6911 *sinterpptr = NULL; 6912 6913 if (!is_elf_hash_table (info->hash)) 6914 return true; 6915 6916 /* Any syms created from now on start with -1 in 6917 got.refcount/offset and plt.refcount/offset. */ 6918 elf_hash_table (info)->init_got_refcount 6919 = elf_hash_table (info)->init_got_offset; 6920 elf_hash_table (info)->init_plt_refcount 6921 = elf_hash_table (info)->init_plt_offset; 6922 6923 bed = get_elf_backend_data (output_bfd); 6924 6925 /* The backend may have to create some sections regardless of whether 6926 we're dynamic or not. */ 6927 if (bed->elf_backend_early_size_sections 6928 && !bed->elf_backend_early_size_sections (output_bfd, info)) 6929 return false; 6930 6931 dynobj = elf_hash_table (info)->dynobj; 6932 6933 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6934 { 6935 struct bfd_elf_version_tree *verdefs; 6936 struct elf_info_failed asvinfo; 6937 struct bfd_elf_version_tree *t; 6938 struct bfd_elf_version_expr *d; 6939 asection *s; 6940 size_t soname_indx; 6941 6942 /* If we are supposed to export all symbols into the dynamic symbol 6943 table (this is not the normal case), then do so. */ 6944 if (info->export_dynamic 6945 || (bfd_link_executable (info) && info->dynamic)) 6946 { 6947 struct elf_info_failed eif; 6948 6949 eif.info = info; 6950 eif.failed = false; 6951 elf_link_hash_traverse (elf_hash_table (info), 6952 _bfd_elf_export_symbol, 6953 &eif); 6954 if (eif.failed) 6955 return false; 6956 } 6957 6958 if (soname != NULL) 6959 { 6960 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6961 soname, true); 6962 if (soname_indx == (size_t) -1 6963 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) 6964 return false; 6965 } 6966 else 6967 soname_indx = (size_t) -1; 6968 6969 /* Make all global versions with definition. */ 6970 for (t = info->version_info; t != NULL; t = t->next) 6971 for (d = t->globals.list; d != NULL; d = d->next) 6972 if (!d->symver && d->literal) 6973 { 6974 const char *verstr, *name; 6975 size_t namelen, verlen, newlen; 6976 char *newname, *p, leading_char; 6977 struct elf_link_hash_entry *newh; 6978 6979 leading_char = bfd_get_symbol_leading_char (output_bfd); 6980 name = d->pattern; 6981 namelen = strlen (name) + (leading_char != '\0'); 6982 verstr = t->name; 6983 verlen = strlen (verstr); 6984 newlen = namelen + verlen + 3; 6985 6986 newname = (char *) bfd_malloc (newlen); 6987 if (newname == NULL) 6988 return false; 6989 newname[0] = leading_char; 6990 memcpy (newname + (leading_char != '\0'), name, namelen); 6991 6992 /* Check the hidden versioned definition. */ 6993 p = newname + namelen; 6994 *p++ = ELF_VER_CHR; 6995 memcpy (p, verstr, verlen + 1); 6996 newh = elf_link_hash_lookup (elf_hash_table (info), 6997 newname, false, false, 6998 false); 6999 if (newh == NULL 7000 || (newh->root.type != bfd_link_hash_defined 7001 && newh->root.type != bfd_link_hash_defweak)) 7002 { 7003 /* Check the default versioned definition. */ 7004 *p++ = ELF_VER_CHR; 7005 memcpy (p, verstr, verlen + 1); 7006 newh = elf_link_hash_lookup (elf_hash_table (info), 7007 newname, false, false, 7008 false); 7009 } 7010 free (newname); 7011 7012 /* Mark this version if there is a definition and it is 7013 not defined in a shared object. */ 7014 if (newh != NULL 7015 && !newh->def_dynamic 7016 && (newh->root.type == bfd_link_hash_defined 7017 || newh->root.type == bfd_link_hash_defweak)) 7018 d->symver = 1; 7019 } 7020 7021 /* Attach all the symbols to their version information. */ 7022 asvinfo.info = info; 7023 asvinfo.failed = false; 7024 7025 elf_link_hash_traverse (elf_hash_table (info), 7026 _bfd_elf_link_assign_sym_version, 7027 &asvinfo); 7028 if (asvinfo.failed) 7029 return false; 7030 7031 if (!info->allow_undefined_version) 7032 { 7033 /* Check if all global versions have a definition. */ 7034 bool all_defined = true; 7035 for (t = info->version_info; t != NULL; t = t->next) 7036 for (d = t->globals.list; d != NULL; d = d->next) 7037 if (d->literal && !d->symver && !d->script) 7038 { 7039 _bfd_error_handler 7040 (_("%s: undefined version: %s"), 7041 d->pattern, t->name); 7042 all_defined = false; 7043 } 7044 7045 if (!all_defined) 7046 { 7047 bfd_set_error (bfd_error_bad_value); 7048 return false; 7049 } 7050 } 7051 7052 /* Set up the version definition section. */ 7053 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 7054 BFD_ASSERT (s != NULL); 7055 7056 /* We may have created additional version definitions if we are 7057 just linking a regular application. */ 7058 verdefs = info->version_info; 7059 7060 /* Skip anonymous version tag. */ 7061 if (verdefs != NULL && verdefs->vernum == 0) 7062 verdefs = verdefs->next; 7063 7064 if (verdefs == NULL && !info->create_default_symver) 7065 s->flags |= SEC_EXCLUDE; 7066 else 7067 { 7068 unsigned int cdefs; 7069 bfd_size_type size; 7070 bfd_byte *p; 7071 Elf_Internal_Verdef def; 7072 Elf_Internal_Verdaux defaux; 7073 struct bfd_link_hash_entry *bh; 7074 struct elf_link_hash_entry *h; 7075 const char *name; 7076 7077 cdefs = 0; 7078 size = 0; 7079 7080 /* Make space for the base version. */ 7081 size += sizeof (Elf_External_Verdef); 7082 size += sizeof (Elf_External_Verdaux); 7083 ++cdefs; 7084 7085 /* Make space for the default version. */ 7086 if (info->create_default_symver) 7087 { 7088 size += sizeof (Elf_External_Verdef); 7089 ++cdefs; 7090 } 7091 7092 for (t = verdefs; t != NULL; t = t->next) 7093 { 7094 struct bfd_elf_version_deps *n; 7095 7096 /* Don't emit base version twice. */ 7097 if (t->vernum == 0) 7098 continue; 7099 7100 size += sizeof (Elf_External_Verdef); 7101 size += sizeof (Elf_External_Verdaux); 7102 ++cdefs; 7103 7104 for (n = t->deps; n != NULL; n = n->next) 7105 size += sizeof (Elf_External_Verdaux); 7106 } 7107 7108 s->size = size; 7109 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 7110 if (s->contents == NULL && s->size != 0) 7111 return false; 7112 s->alloced = 1; 7113 7114 /* Fill in the version definition section. */ 7115 7116 p = s->contents; 7117 7118 def.vd_version = VER_DEF_CURRENT; 7119 def.vd_flags = VER_FLG_BASE; 7120 def.vd_ndx = 1; 7121 def.vd_cnt = 1; 7122 if (info->create_default_symver) 7123 { 7124 def.vd_aux = 2 * sizeof (Elf_External_Verdef); 7125 def.vd_next = sizeof (Elf_External_Verdef); 7126 } 7127 else 7128 { 7129 def.vd_aux = sizeof (Elf_External_Verdef); 7130 def.vd_next = (sizeof (Elf_External_Verdef) 7131 + sizeof (Elf_External_Verdaux)); 7132 } 7133 7134 if (soname_indx != (size_t) -1) 7135 { 7136 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 7137 soname_indx); 7138 def.vd_hash = bfd_elf_hash (soname); 7139 defaux.vda_name = soname_indx; 7140 name = soname; 7141 } 7142 else 7143 { 7144 size_t indx; 7145 7146 name = lbasename (bfd_get_filename (output_bfd)); 7147 def.vd_hash = bfd_elf_hash (name); 7148 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 7149 name, false); 7150 if (indx == (size_t) -1) 7151 return false; 7152 defaux.vda_name = indx; 7153 } 7154 defaux.vda_next = 0; 7155 7156 _bfd_elf_swap_verdef_out (output_bfd, &def, 7157 (Elf_External_Verdef *) p); 7158 p += sizeof (Elf_External_Verdef); 7159 if (info->create_default_symver) 7160 { 7161 /* Add a symbol representing this version. */ 7162 bh = NULL; 7163 if (! (_bfd_generic_link_add_one_symbol 7164 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, 7165 0, NULL, false, 7166 get_elf_backend_data (dynobj)->collect, &bh))) 7167 return false; 7168 h = (struct elf_link_hash_entry *) bh; 7169 h->non_elf = 0; 7170 h->def_regular = 1; 7171 h->type = STT_OBJECT; 7172 h->verinfo.vertree = NULL; 7173 7174 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 7175 return false; 7176 7177 /* Create a duplicate of the base version with the same 7178 aux block, but different flags. */ 7179 def.vd_flags = 0; 7180 def.vd_ndx = 2; 7181 def.vd_aux = sizeof (Elf_External_Verdef); 7182 if (verdefs) 7183 def.vd_next = (sizeof (Elf_External_Verdef) 7184 + sizeof (Elf_External_Verdaux)); 7185 else 7186 def.vd_next = 0; 7187 _bfd_elf_swap_verdef_out (output_bfd, &def, 7188 (Elf_External_Verdef *) p); 7189 p += sizeof (Elf_External_Verdef); 7190 } 7191 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 7192 (Elf_External_Verdaux *) p); 7193 p += sizeof (Elf_External_Verdaux); 7194 7195 for (t = verdefs; t != NULL; t = t->next) 7196 { 7197 unsigned int cdeps; 7198 struct bfd_elf_version_deps *n; 7199 7200 /* Don't emit the base version twice. */ 7201 if (t->vernum == 0) 7202 continue; 7203 7204 cdeps = 0; 7205 for (n = t->deps; n != NULL; n = n->next) 7206 ++cdeps; 7207 7208 /* Add a symbol representing this version. */ 7209 bh = NULL; 7210 if (! (_bfd_generic_link_add_one_symbol 7211 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, 7212 0, NULL, false, 7213 get_elf_backend_data (dynobj)->collect, &bh))) 7214 return false; 7215 h = (struct elf_link_hash_entry *) bh; 7216 h->non_elf = 0; 7217 h->def_regular = 1; 7218 h->type = STT_OBJECT; 7219 h->verinfo.vertree = t; 7220 7221 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 7222 return false; 7223 7224 def.vd_version = VER_DEF_CURRENT; 7225 def.vd_flags = 0; 7226 if (t->globals.list == NULL 7227 && t->locals.list == NULL 7228 && ! t->used) 7229 def.vd_flags |= VER_FLG_WEAK; 7230 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); 7231 def.vd_cnt = cdeps + 1; 7232 def.vd_hash = bfd_elf_hash (t->name); 7233 def.vd_aux = sizeof (Elf_External_Verdef); 7234 def.vd_next = 0; 7235 7236 /* If a basever node is next, it *must* be the last node in 7237 the chain, otherwise Verdef construction breaks. */ 7238 if (t->next != NULL && t->next->vernum == 0) 7239 BFD_ASSERT (t->next->next == NULL); 7240 7241 if (t->next != NULL && t->next->vernum != 0) 7242 def.vd_next = (sizeof (Elf_External_Verdef) 7243 + (cdeps + 1) * sizeof (Elf_External_Verdaux)); 7244 7245 _bfd_elf_swap_verdef_out (output_bfd, &def, 7246 (Elf_External_Verdef *) p); 7247 p += sizeof (Elf_External_Verdef); 7248 7249 defaux.vda_name = h->dynstr_index; 7250 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 7251 h->dynstr_index); 7252 defaux.vda_next = 0; 7253 if (t->deps != NULL) 7254 defaux.vda_next = sizeof (Elf_External_Verdaux); 7255 t->name_indx = defaux.vda_name; 7256 7257 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 7258 (Elf_External_Verdaux *) p); 7259 p += sizeof (Elf_External_Verdaux); 7260 7261 for (n = t->deps; n != NULL; n = n->next) 7262 { 7263 if (n->version_needed == NULL) 7264 { 7265 /* This can happen if there was an error in the 7266 version script. */ 7267 defaux.vda_name = 0; 7268 } 7269 else 7270 { 7271 defaux.vda_name = n->version_needed->name_indx; 7272 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 7273 defaux.vda_name); 7274 } 7275 if (n->next == NULL) 7276 defaux.vda_next = 0; 7277 else 7278 defaux.vda_next = sizeof (Elf_External_Verdaux); 7279 7280 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 7281 (Elf_External_Verdaux *) p); 7282 p += sizeof (Elf_External_Verdaux); 7283 } 7284 } 7285 7286 elf_tdata (output_bfd)->cverdefs = cdefs; 7287 } 7288 } 7289 7290 if (info->gc_sections && bed->can_gc_sections) 7291 { 7292 struct elf_gc_sweep_symbol_info sweep_info; 7293 7294 /* Remove the symbols that were in the swept sections from the 7295 dynamic symbol table. */ 7296 sweep_info.info = info; 7297 sweep_info.hide_symbol = bed->elf_backend_hide_symbol; 7298 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, 7299 &sweep_info); 7300 } 7301 7302 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 7303 { 7304 asection *s; 7305 struct elf_find_verdep_info sinfo; 7306 7307 /* Work out the size of the version reference section. */ 7308 7309 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 7310 BFD_ASSERT (s != NULL); 7311 7312 sinfo.info = info; 7313 sinfo.vers = elf_tdata (output_bfd)->cverdefs; 7314 if (sinfo.vers == 0) 7315 sinfo.vers = 1; 7316 sinfo.failed = false; 7317 7318 elf_link_hash_traverse (elf_hash_table (info), 7319 _bfd_elf_link_find_version_dependencies, 7320 &sinfo); 7321 if (sinfo.failed) 7322 return false; 7323 7324 bed->elf_backend_add_glibc_version_dependency (&sinfo); 7325 if (sinfo.failed) 7326 return false; 7327 7328 if (elf_tdata (output_bfd)->verref == NULL) 7329 s->flags |= SEC_EXCLUDE; 7330 else 7331 { 7332 Elf_Internal_Verneed *vn; 7333 unsigned int size; 7334 unsigned int crefs; 7335 bfd_byte *p; 7336 7337 /* Build the version dependency section. */ 7338 size = 0; 7339 crefs = 0; 7340 for (vn = elf_tdata (output_bfd)->verref; 7341 vn != NULL; 7342 vn = vn->vn_nextref) 7343 { 7344 Elf_Internal_Vernaux *a; 7345 7346 size += sizeof (Elf_External_Verneed); 7347 ++crefs; 7348 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 7349 size += sizeof (Elf_External_Vernaux); 7350 } 7351 7352 s->size = size; 7353 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 7354 if (s->contents == NULL) 7355 return false; 7356 s->alloced = 1; 7357 7358 p = s->contents; 7359 for (vn = elf_tdata (output_bfd)->verref; 7360 vn != NULL; 7361 vn = vn->vn_nextref) 7362 { 7363 unsigned int caux; 7364 Elf_Internal_Vernaux *a; 7365 size_t indx; 7366 7367 caux = 0; 7368 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 7369 ++caux; 7370 7371 vn->vn_version = VER_NEED_CURRENT; 7372 vn->vn_cnt = caux; 7373 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 7374 elf_dt_name (vn->vn_bfd) != NULL 7375 ? elf_dt_name (vn->vn_bfd) 7376 : lbasename (bfd_get_filename 7377 (vn->vn_bfd)), 7378 false); 7379 if (indx == (size_t) -1) 7380 return false; 7381 vn->vn_file = indx; 7382 vn->vn_aux = sizeof (Elf_External_Verneed); 7383 if (vn->vn_nextref == NULL) 7384 vn->vn_next = 0; 7385 else 7386 vn->vn_next = (sizeof (Elf_External_Verneed) 7387 + caux * sizeof (Elf_External_Vernaux)); 7388 7389 _bfd_elf_swap_verneed_out (output_bfd, vn, 7390 (Elf_External_Verneed *) p); 7391 p += sizeof (Elf_External_Verneed); 7392 7393 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 7394 { 7395 a->vna_hash = bfd_elf_hash (a->vna_nodename); 7396 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 7397 a->vna_nodename, false); 7398 if (indx == (size_t) -1) 7399 return false; 7400 a->vna_name = indx; 7401 if (a->vna_nextptr == NULL) 7402 a->vna_next = 0; 7403 else 7404 a->vna_next = sizeof (Elf_External_Vernaux); 7405 7406 _bfd_elf_swap_vernaux_out (output_bfd, a, 7407 (Elf_External_Vernaux *) p); 7408 p += sizeof (Elf_External_Vernaux); 7409 } 7410 } 7411 7412 elf_tdata (output_bfd)->cverrefs = crefs; 7413 } 7414 } 7415 7416 if (bfd_link_relocatable (info) 7417 && !_bfd_elf_size_group_sections (info)) 7418 return false; 7419 7420 /* Determine any GNU_STACK segment requirements, after the backend 7421 has had a chance to set a default segment size. */ 7422 if (info->execstack) 7423 { 7424 /* If the user has explicitly requested warnings, then generate one even 7425 though the choice is the result of another command line option. */ 7426 if (info->warn_execstack == 1) 7427 { 7428 if (info->error_execstack) 7429 { 7430 _bfd_error_handler 7431 (_("\ 7432 error: creating an executable stack because of -z execstack command line option")); 7433 return false; 7434 } 7435 7436 _bfd_error_handler 7437 (_("\ 7438 warning: enabling an executable stack because of -z execstack command line option")); 7439 } 7440 7441 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; 7442 } 7443 else if (info->noexecstack) 7444 elf_stack_flags (output_bfd) = PF_R | PF_W; 7445 else 7446 { 7447 bfd *inputobj; 7448 asection *notesec = NULL; 7449 bfd *noteobj = NULL; 7450 bfd *emptyobj = NULL; 7451 int exec = 0; 7452 7453 for (inputobj = info->input_bfds; 7454 inputobj; 7455 inputobj = inputobj->link.next) 7456 { 7457 asection *s; 7458 7459 if (inputobj->flags 7460 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) 7461 continue; 7462 s = inputobj->sections; 7463 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 7464 continue; 7465 7466 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); 7467 if (s) 7468 { 7469 notesec = s; 7470 if (s->flags & SEC_CODE) 7471 { 7472 noteobj = inputobj; 7473 exec = PF_X; 7474 /* There is no point in scanning the remaining bfds. */ 7475 break; 7476 } 7477 } 7478 else if (bed->default_execstack && info->default_execstack) 7479 { 7480 exec = PF_X; 7481 emptyobj = inputobj; 7482 } 7483 } 7484 7485 if (notesec || info->stacksize > 0) 7486 { 7487 if (exec) 7488 { 7489 if (info->warn_execstack != 0) 7490 { 7491 /* PR 29072: Because an executable stack is a serious 7492 security risk, make sure that the user knows that it is 7493 being enabled despite the fact that it was not requested 7494 on the command line. */ 7495 if (noteobj) 7496 { 7497 if (info->error_execstack) 7498 { 7499 _bfd_error_handler (_("\ 7500 error: %s: is triggering the generation of an executable stack (because it has an executable .note.GNU-stack section)"), 7501 bfd_get_filename (noteobj)); 7502 return false; 7503 } 7504 7505 _bfd_error_handler (_("\ 7506 warning: %s: requires executable stack (because the .note.GNU-stack section is executable)"), 7507 bfd_get_filename (noteobj)); 7508 } 7509 else if (emptyobj) 7510 { 7511 if (info->error_execstack) 7512 { 7513 _bfd_error_handler (_("\ 7514 error: %s: is triggering the generation of an executable stack because it does not have a .note.GNU-stack section"), 7515 bfd_get_filename (emptyobj)); 7516 return false; 7517 } 7518 7519 _bfd_error_handler (_("\ 7520 warning: %s: missing .note.GNU-stack section implies executable stack"), 7521 bfd_get_filename (emptyobj)); 7522 _bfd_error_handler (_("\ 7523 NOTE: This behaviour is deprecated and will be removed in a future version of the linker")); 7524 } 7525 } 7526 } 7527 elf_stack_flags (output_bfd) = PF_R | PF_W | exec; 7528 } 7529 7530 if (notesec && exec && bfd_link_relocatable (info) 7531 && notesec->output_section != bfd_abs_section_ptr) 7532 notesec->output_section->flags |= SEC_CODE; 7533 } 7534 7535 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 7536 { 7537 struct elf_info_failed eif; 7538 struct elf_link_hash_entry *h; 7539 asection *dynstr; 7540 asection *s; 7541 7542 *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); 7543 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); 7544 7545 if (info->symbolic) 7546 { 7547 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) 7548 return false; 7549 info->flags |= DF_SYMBOLIC; 7550 } 7551 7552 if (rpath != NULL) 7553 { 7554 size_t indx; 7555 bfd_vma tag; 7556 7557 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, 7558 true); 7559 if (indx == (size_t) -1) 7560 return false; 7561 7562 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; 7563 if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) 7564 return false; 7565 } 7566 7567 if (filter_shlib != NULL) 7568 { 7569 size_t indx; 7570 7571 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 7572 filter_shlib, true); 7573 if (indx == (size_t) -1 7574 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) 7575 return false; 7576 } 7577 7578 if (auxiliary_filters != NULL) 7579 { 7580 const char * const *p; 7581 7582 for (p = auxiliary_filters; *p != NULL; p++) 7583 { 7584 size_t indx; 7585 7586 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 7587 *p, true); 7588 if (indx == (size_t) -1 7589 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) 7590 return false; 7591 } 7592 } 7593 7594 if (audit != NULL) 7595 { 7596 size_t indx; 7597 7598 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, 7599 true); 7600 if (indx == (size_t) -1 7601 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) 7602 return false; 7603 } 7604 7605 if (depaudit != NULL) 7606 { 7607 size_t indx; 7608 7609 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, 7610 true); 7611 if (indx == (size_t) -1 7612 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) 7613 return false; 7614 } 7615 7616 eif.info = info; 7617 eif.failed = false; 7618 7619 /* Find all symbols which were defined in a dynamic object and make 7620 the backend pick a reasonable value for them. */ 7621 elf_link_hash_traverse (elf_hash_table (info), 7622 _bfd_elf_adjust_dynamic_symbol, 7623 &eif); 7624 if (eif.failed) 7625 return false; 7626 7627 /* Add some entries to the .dynamic section. We fill in some of the 7628 values later, in bfd_elf_final_link, but we must add the entries 7629 now so that we know the final size of the .dynamic section. */ 7630 7631 /* If there are initialization and/or finalization functions to 7632 call then add the corresponding DT_INIT/DT_FINI entries. */ 7633 h = (info->init_function 7634 ? elf_link_hash_lookup (elf_hash_table (info), 7635 info->init_function, false, 7636 false, false) 7637 : NULL); 7638 if (h != NULL 7639 && (h->ref_regular 7640 || h->def_regular)) 7641 { 7642 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) 7643 return false; 7644 } 7645 h = (info->fini_function 7646 ? elf_link_hash_lookup (elf_hash_table (info), 7647 info->fini_function, false, 7648 false, false) 7649 : NULL); 7650 if (h != NULL 7651 && (h->ref_regular 7652 || h->def_regular)) 7653 { 7654 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) 7655 return false; 7656 } 7657 7658 s = bfd_get_section_by_name (output_bfd, ".preinit_array"); 7659 if (s != NULL && s->linker_has_input) 7660 { 7661 /* DT_PREINIT_ARRAY is not allowed in shared library. */ 7662 if (! bfd_link_executable (info)) 7663 { 7664 bfd *sub; 7665 asection *o; 7666 7667 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 7668 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 7669 && (o = sub->sections) != NULL 7670 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) 7671 for (o = sub->sections; o != NULL; o = o->next) 7672 if (elf_section_data (o)->this_hdr.sh_type 7673 == SHT_PREINIT_ARRAY) 7674 { 7675 _bfd_error_handler 7676 (_("%pB: .preinit_array section is not allowed in DSO"), 7677 sub); 7678 break; 7679 } 7680 7681 bfd_set_error (bfd_error_nonrepresentable_section); 7682 return false; 7683 } 7684 7685 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) 7686 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) 7687 return false; 7688 } 7689 s = bfd_get_section_by_name (output_bfd, ".init_array"); 7690 if (s != NULL && s->linker_has_input) 7691 { 7692 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) 7693 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) 7694 return false; 7695 } 7696 s = bfd_get_section_by_name (output_bfd, ".fini_array"); 7697 if (s != NULL && s->linker_has_input) 7698 { 7699 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) 7700 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) 7701 return false; 7702 } 7703 7704 dynstr = bfd_get_linker_section (dynobj, ".dynstr"); 7705 /* If .dynstr is excluded from the link, we don't want any of 7706 these tags. Strictly, we should be checking each section 7707 individually; This quick check covers for the case where 7708 someone does a /DISCARD/ : { *(*) }. */ 7709 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) 7710 { 7711 bfd_size_type strsize; 7712 7713 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 7714 if ((info->emit_hash 7715 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) 7716 || (info->emit_gnu_hash 7717 && (bed->record_xhash_symbol == NULL 7718 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0))) 7719 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) 7720 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) 7721 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) 7722 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, 7723 bed->s->sizeof_sym) 7724 || (info->gnu_flags_1 7725 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_FLAGS_1, 7726 info->gnu_flags_1))) 7727 return false; 7728 } 7729 } 7730 7731 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) 7732 return false; 7733 7734 /* The backend must work out the sizes of all the other dynamic 7735 sections. */ 7736 if (bed->elf_backend_late_size_sections != NULL 7737 && !bed->elf_backend_late_size_sections (output_bfd, info)) 7738 return false; 7739 7740 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 7741 { 7742 if (elf_tdata (output_bfd)->cverdefs) 7743 { 7744 unsigned int crefs = elf_tdata (output_bfd)->cverdefs; 7745 7746 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) 7747 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) 7748 return false; 7749 } 7750 7751 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) 7752 { 7753 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) 7754 return false; 7755 } 7756 else if (info->flags & DF_BIND_NOW) 7757 { 7758 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) 7759 return false; 7760 } 7761 7762 if (info->flags_1) 7763 { 7764 if (bfd_link_executable (info)) 7765 info->flags_1 &= ~ (DF_1_INITFIRST 7766 | DF_1_NODELETE 7767 | DF_1_NOOPEN); 7768 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) 7769 return false; 7770 } 7771 7772 if (elf_tdata (output_bfd)->cverrefs) 7773 { 7774 unsigned int crefs = elf_tdata (output_bfd)->cverrefs; 7775 7776 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) 7777 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) 7778 return false; 7779 } 7780 7781 if ((elf_tdata (output_bfd)->cverrefs == 0 7782 && elf_tdata (output_bfd)->cverdefs == 0) 7783 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) 7784 { 7785 asection *s; 7786 7787 s = bfd_get_linker_section (dynobj, ".gnu.version"); 7788 s->flags |= SEC_EXCLUDE; 7789 } 7790 } 7791 return true; 7792 } 7793 7794 /* Find the first non-excluded output section. We'll use its 7795 section symbol for some emitted relocs. */ 7796 void 7797 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) 7798 { 7799 asection *s; 7800 asection *found = NULL; 7801 7802 for (s = output_bfd->sections; s != NULL; s = s->next) 7803 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 7804 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) 7805 { 7806 found = s; 7807 if ((s->flags & SEC_THREAD_LOCAL) == 0) 7808 break; 7809 } 7810 elf_hash_table (info)->text_index_section = found; 7811 } 7812 7813 /* Find two non-excluded output sections, one for code, one for data. 7814 We'll use their section symbols for some emitted relocs. */ 7815 void 7816 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) 7817 { 7818 asection *s; 7819 asection *found = NULL; 7820 7821 /* Data first, since setting text_index_section changes 7822 _bfd_elf_omit_section_dynsym_default. */ 7823 for (s = output_bfd->sections; s != NULL; s = s->next) 7824 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 7825 && !(s->flags & SEC_READONLY) 7826 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) 7827 { 7828 found = s; 7829 if ((s->flags & SEC_THREAD_LOCAL) == 0) 7830 break; 7831 } 7832 elf_hash_table (info)->data_index_section = found; 7833 7834 for (s = output_bfd->sections; s != NULL; s = s->next) 7835 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 7836 && (s->flags & SEC_READONLY) 7837 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) 7838 { 7839 found = s; 7840 break; 7841 } 7842 elf_hash_table (info)->text_index_section = found; 7843 } 7844 7845 #define GNU_HASH_SECTION_NAME(bed) \ 7846 (bed)->record_xhash_symbol != NULL ? ".MIPS.xhash" : ".gnu.hash" 7847 7848 bool 7849 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) 7850 { 7851 const struct elf_backend_data *bed; 7852 unsigned long section_sym_count; 7853 bfd_size_type dynsymcount = 0; 7854 7855 if (!is_elf_hash_table (info->hash)) 7856 return true; 7857 7858 bed = get_elf_backend_data (output_bfd); 7859 (*bed->elf_backend_init_index_section) (output_bfd, info); 7860 7861 /* Assign dynsym indices. In a shared library we generate a section 7862 symbol for each output section, which come first. Next come all 7863 of the back-end allocated local dynamic syms, followed by the rest 7864 of the global symbols. 7865 7866 This is usually not needed for static binaries, however backends 7867 can request to always do it, e.g. the MIPS backend uses dynamic 7868 symbol counts to lay out GOT, which will be produced in the 7869 presence of GOT relocations even in static binaries (holding fixed 7870 data in that case, to satisfy those relocations). */ 7871 7872 if (elf_hash_table (info)->dynamic_sections_created 7873 || bed->always_renumber_dynsyms) 7874 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, 7875 §ion_sym_count); 7876 7877 if (elf_hash_table (info)->dynamic_sections_created) 7878 { 7879 bfd *dynobj; 7880 asection *s; 7881 unsigned int dtagcount; 7882 7883 dynobj = elf_hash_table (info)->dynobj; 7884 7885 /* Work out the size of the symbol version section. */ 7886 s = bfd_get_linker_section (dynobj, ".gnu.version"); 7887 BFD_ASSERT (s != NULL); 7888 if ((s->flags & SEC_EXCLUDE) == 0) 7889 { 7890 s->size = dynsymcount * sizeof (Elf_External_Versym); 7891 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7892 if (s->contents == NULL) 7893 return false; 7894 s->alloced = 1; 7895 7896 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) 7897 return false; 7898 } 7899 7900 /* Set the size of the .dynsym and .hash sections. We counted 7901 the number of dynamic symbols in elf_link_add_object_symbols. 7902 We will build the contents of .dynsym and .hash when we build 7903 the final symbol table, because until then we do not know the 7904 correct value to give the symbols. We built the .dynstr 7905 section as we went along in elf_link_add_object_symbols. */ 7906 s = elf_hash_table (info)->dynsym; 7907 BFD_ASSERT (s != NULL); 7908 s->size = dynsymcount * bed->s->sizeof_sym; 7909 7910 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 7911 if (s->contents == NULL) 7912 return false; 7913 s->alloced = 1; 7914 7915 /* The first entry in .dynsym is a dummy symbol. Clear all the 7916 section syms, in case we don't output them all. */ 7917 ++section_sym_count; 7918 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); 7919 7920 elf_hash_table (info)->bucketcount = 0; 7921 7922 /* Compute the size of the hashing table. As a side effect this 7923 computes the hash values for all the names we export. */ 7924 if (info->emit_hash) 7925 { 7926 unsigned long int *hashcodes; 7927 struct hash_codes_info hashinf; 7928 bfd_size_type amt; 7929 unsigned long int nsyms; 7930 size_t bucketcount; 7931 size_t hash_entry_size; 7932 7933 /* Compute the hash values for all exported symbols. At the same 7934 time store the values in an array so that we could use them for 7935 optimizations. */ 7936 amt = dynsymcount * sizeof (unsigned long int); 7937 hashcodes = (unsigned long int *) bfd_malloc (amt); 7938 if (hashcodes == NULL) 7939 return false; 7940 hashinf.hashcodes = hashcodes; 7941 hashinf.error = false; 7942 7943 /* Put all hash values in HASHCODES. */ 7944 elf_link_hash_traverse (elf_hash_table (info), 7945 elf_collect_hash_codes, &hashinf); 7946 if (hashinf.error) 7947 { 7948 free (hashcodes); 7949 return false; 7950 } 7951 7952 nsyms = hashinf.hashcodes - hashcodes; 7953 bucketcount 7954 = compute_bucket_count (info, hashcodes, nsyms, 0); 7955 free (hashcodes); 7956 7957 if (bucketcount == 0 && nsyms > 0) 7958 return false; 7959 7960 elf_hash_table (info)->bucketcount = bucketcount; 7961 7962 s = bfd_get_linker_section (dynobj, ".hash"); 7963 BFD_ASSERT (s != NULL); 7964 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; 7965 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); 7966 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7967 if (s->contents == NULL) 7968 return false; 7969 s->alloced = 1; 7970 7971 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); 7972 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, 7973 s->contents + hash_entry_size); 7974 } 7975 7976 if (info->emit_gnu_hash) 7977 { 7978 size_t i, cnt; 7979 unsigned char *contents; 7980 struct collect_gnu_hash_codes cinfo; 7981 bfd_size_type amt; 7982 size_t bucketcount; 7983 7984 memset (&cinfo, 0, sizeof (cinfo)); 7985 7986 /* Compute the hash values for all exported symbols. At the same 7987 time store the values in an array so that we could use them for 7988 optimizations. */ 7989 amt = dynsymcount * 2 * sizeof (unsigned long int); 7990 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); 7991 if (cinfo.hashcodes == NULL) 7992 return false; 7993 7994 cinfo.hashval = cinfo.hashcodes + dynsymcount; 7995 cinfo.min_dynindx = -1; 7996 cinfo.output_bfd = output_bfd; 7997 cinfo.bed = bed; 7998 7999 /* Put all hash values in HASHCODES. */ 8000 elf_link_hash_traverse (elf_hash_table (info), 8001 elf_collect_gnu_hash_codes, &cinfo); 8002 if (cinfo.error) 8003 { 8004 free (cinfo.hashcodes); 8005 return false; 8006 } 8007 8008 bucketcount 8009 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); 8010 8011 if (bucketcount == 0) 8012 { 8013 free (cinfo.hashcodes); 8014 return false; 8015 } 8016 8017 s = bfd_get_linker_section (dynobj, GNU_HASH_SECTION_NAME (bed)); 8018 BFD_ASSERT (s != NULL); 8019 8020 if (cinfo.nsyms == 0) 8021 { 8022 /* Empty .gnu.hash or .MIPS.xhash section is special. */ 8023 BFD_ASSERT (cinfo.min_dynindx == -1); 8024 free (cinfo.hashcodes); 8025 s->size = 5 * 4 + bed->s->arch_size / 8; 8026 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 8027 if (contents == NULL) 8028 return false; 8029 s->contents = contents; 8030 s->alloced = 1; 8031 /* 1 empty bucket. */ 8032 bfd_put_32 (output_bfd, 1, contents); 8033 /* SYMIDX above the special symbol 0. */ 8034 bfd_put_32 (output_bfd, 1, contents + 4); 8035 /* Just one word for bitmask. */ 8036 bfd_put_32 (output_bfd, 1, contents + 8); 8037 /* Only hash fn bloom filter. */ 8038 bfd_put_32 (output_bfd, 0, contents + 12); 8039 /* No hashes are valid - empty bitmask. */ 8040 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); 8041 /* No hashes in the only bucket. */ 8042 bfd_put_32 (output_bfd, 0, 8043 contents + 16 + bed->s->arch_size / 8); 8044 } 8045 else 8046 { 8047 unsigned long int maskwords, maskbitslog2, x; 8048 BFD_ASSERT (cinfo.min_dynindx != -1); 8049 8050 x = cinfo.nsyms; 8051 maskbitslog2 = 1; 8052 while ((x >>= 1) != 0) 8053 ++maskbitslog2; 8054 if (maskbitslog2 < 3) 8055 maskbitslog2 = 5; 8056 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) 8057 maskbitslog2 = maskbitslog2 + 3; 8058 else 8059 maskbitslog2 = maskbitslog2 + 2; 8060 if (bed->s->arch_size == 64) 8061 { 8062 if (maskbitslog2 == 5) 8063 maskbitslog2 = 6; 8064 cinfo.shift1 = 6; 8065 } 8066 else 8067 cinfo.shift1 = 5; 8068 cinfo.mask = (1 << cinfo.shift1) - 1; 8069 cinfo.shift2 = maskbitslog2; 8070 cinfo.maskbits = 1 << maskbitslog2; 8071 maskwords = 1 << (maskbitslog2 - cinfo.shift1); 8072 amt = bucketcount * sizeof (unsigned long int) * 2; 8073 amt += maskwords * sizeof (bfd_vma); 8074 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); 8075 if (cinfo.bitmask == NULL) 8076 { 8077 free (cinfo.hashcodes); 8078 return false; 8079 } 8080 8081 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); 8082 cinfo.indx = cinfo.counts + bucketcount; 8083 cinfo.symindx = dynsymcount - cinfo.nsyms; 8084 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); 8085 8086 /* Determine how often each hash bucket is used. */ 8087 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); 8088 for (i = 0; i < cinfo.nsyms; ++i) 8089 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; 8090 8091 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) 8092 if (cinfo.counts[i] != 0) 8093 { 8094 cinfo.indx[i] = cnt; 8095 cnt += cinfo.counts[i]; 8096 } 8097 BFD_ASSERT (cnt == dynsymcount); 8098 cinfo.bucketcount = bucketcount; 8099 cinfo.local_indx = cinfo.min_dynindx; 8100 8101 s->size = (4 + bucketcount + cinfo.nsyms) * 4; 8102 s->size += cinfo.maskbits / 8; 8103 if (bed->record_xhash_symbol != NULL) 8104 s->size += cinfo.nsyms * 4; 8105 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 8106 if (contents == NULL) 8107 { 8108 free (cinfo.bitmask); 8109 free (cinfo.hashcodes); 8110 return false; 8111 } 8112 8113 s->contents = contents; 8114 s->alloced = 1; 8115 bfd_put_32 (output_bfd, bucketcount, contents); 8116 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); 8117 bfd_put_32 (output_bfd, maskwords, contents + 8); 8118 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); 8119 contents += 16 + cinfo.maskbits / 8; 8120 8121 for (i = 0; i < bucketcount; ++i) 8122 { 8123 if (cinfo.counts[i] == 0) 8124 bfd_put_32 (output_bfd, 0, contents); 8125 else 8126 bfd_put_32 (output_bfd, cinfo.indx[i], contents); 8127 contents += 4; 8128 } 8129 8130 cinfo.contents = contents; 8131 8132 cinfo.xlat = contents + cinfo.nsyms * 4 - s->contents; 8133 /* Renumber dynamic symbols, if populating .gnu.hash section. 8134 If using .MIPS.xhash, populate the translation table. */ 8135 elf_link_hash_traverse (elf_hash_table (info), 8136 elf_gnu_hash_process_symidx, &cinfo); 8137 8138 contents = s->contents + 16; 8139 for (i = 0; i < maskwords; ++i) 8140 { 8141 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], 8142 contents); 8143 contents += bed->s->arch_size / 8; 8144 } 8145 8146 free (cinfo.bitmask); 8147 free (cinfo.hashcodes); 8148 } 8149 } 8150 8151 s = bfd_get_linker_section (dynobj, ".dynstr"); 8152 BFD_ASSERT (s != NULL); 8153 8154 elf_finalize_dynstr (output_bfd, info); 8155 8156 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 8157 8158 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) 8159 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) 8160 return false; 8161 } 8162 8163 return true; 8164 } 8165 8166 /* Make sure sec_info_type is cleared if sec_info is cleared too. */ 8168 8169 static void 8170 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, 8171 asection *sec) 8172 { 8173 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); 8174 sec->sec_info_type = SEC_INFO_TYPE_NONE; 8175 } 8176 8177 /* Finish SHF_MERGE section merging. */ 8178 8179 bool 8180 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) 8181 { 8182 bfd *ibfd; 8183 asection *sec; 8184 8185 if (ENABLE_CHECKING && !is_elf_hash_table (info->hash)) 8186 abort (); 8187 8188 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 8189 if ((ibfd->flags & DYNAMIC) == 0 8190 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour 8191 && (elf_elfheader (ibfd)->e_ident[EI_CLASS] 8192 == get_elf_backend_data (obfd)->s->elfclass)) 8193 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 8194 if ((sec->flags & SEC_MERGE) != 0 8195 && !bfd_is_abs_section (sec->output_section)) 8196 { 8197 struct bfd_elf_section_data *secdata; 8198 8199 secdata = elf_section_data (sec); 8200 if (! _bfd_add_merge_section (obfd, 8201 &elf_hash_table (info)->merge_info, 8202 sec, &secdata->sec_info)) 8203 return false; 8204 else if (secdata->sec_info) 8205 sec->sec_info_type = SEC_INFO_TYPE_MERGE; 8206 } 8207 8208 if (elf_hash_table (info)->merge_info != NULL) 8209 return _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, 8210 merge_sections_remove_hook); 8211 return true; 8212 } 8213 8214 /* Create an entry in an ELF linker hash table. */ 8215 8216 struct bfd_hash_entry * 8217 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 8218 struct bfd_hash_table *table, 8219 const char *string) 8220 { 8221 /* Allocate the structure if it has not already been allocated by a 8222 subclass. */ 8223 if (entry == NULL) 8224 { 8225 entry = (struct bfd_hash_entry *) 8226 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 8227 if (entry == NULL) 8228 return entry; 8229 } 8230 8231 /* Call the allocation method of the superclass. */ 8232 entry = _bfd_link_hash_newfunc (entry, table, string); 8233 if (entry != NULL) 8234 { 8235 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 8236 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 8237 8238 /* Set local fields. */ 8239 ret->indx = -1; 8240 ret->dynindx = -1; 8241 ret->got = htab->init_got_refcount; 8242 ret->plt = htab->init_plt_refcount; 8243 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) 8244 - offsetof (struct elf_link_hash_entry, size))); 8245 /* Assume that we have been called by a non-ELF symbol reader. 8246 This flag is then reset by the code which reads an ELF input 8247 file. This ensures that a symbol created by a non-ELF symbol 8248 reader will have the flag set correctly. */ 8249 ret->non_elf = 1; 8250 } 8251 8252 return entry; 8253 } 8254 8255 /* Copy data from an indirect symbol to its direct symbol, hiding the 8256 old indirect symbol. Also used for copying flags to a weakdef. */ 8257 8258 void 8259 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, 8260 struct elf_link_hash_entry *dir, 8261 struct elf_link_hash_entry *ind) 8262 { 8263 struct elf_link_hash_table *htab; 8264 8265 if (ind->dyn_relocs != NULL) 8266 { 8267 if (dir->dyn_relocs != NULL) 8268 { 8269 struct elf_dyn_relocs **pp; 8270 struct elf_dyn_relocs *p; 8271 8272 /* Add reloc counts against the indirect sym to the direct sym 8273 list. Merge any entries against the same section. */ 8274 for (pp = &ind->dyn_relocs; (p = *pp) != NULL; ) 8275 { 8276 struct elf_dyn_relocs *q; 8277 8278 for (q = dir->dyn_relocs; q != NULL; q = q->next) 8279 if (q->sec == p->sec) 8280 { 8281 q->pc_count += p->pc_count; 8282 q->count += p->count; 8283 *pp = p->next; 8284 break; 8285 } 8286 if (q == NULL) 8287 pp = &p->next; 8288 } 8289 *pp = dir->dyn_relocs; 8290 } 8291 8292 dir->dyn_relocs = ind->dyn_relocs; 8293 ind->dyn_relocs = NULL; 8294 } 8295 8296 /* Copy down any references that we may have already seen to the 8297 symbol which just became indirect. */ 8298 8299 if (dir->versioned != versioned_hidden) 8300 dir->ref_dynamic |= ind->ref_dynamic; 8301 dir->ref_regular |= ind->ref_regular; 8302 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 8303 dir->non_got_ref |= ind->non_got_ref; 8304 dir->needs_plt |= ind->needs_plt; 8305 dir->pointer_equality_needed |= ind->pointer_equality_needed; 8306 8307 if (ind->root.type != bfd_link_hash_indirect) 8308 return; 8309 8310 /* Copy over the global and procedure linkage table refcount entries. 8311 These may have been already set up by a check_relocs routine. */ 8312 htab = elf_hash_table (info); 8313 if (ind->got.refcount > htab->init_got_refcount.refcount) 8314 { 8315 if (dir->got.refcount < 0) 8316 dir->got.refcount = 0; 8317 dir->got.refcount += ind->got.refcount; 8318 ind->got.refcount = htab->init_got_refcount.refcount; 8319 } 8320 8321 if (ind->plt.refcount > htab->init_plt_refcount.refcount) 8322 { 8323 if (dir->plt.refcount < 0) 8324 dir->plt.refcount = 0; 8325 dir->plt.refcount += ind->plt.refcount; 8326 ind->plt.refcount = htab->init_plt_refcount.refcount; 8327 } 8328 8329 if (ind->dynindx != -1) 8330 { 8331 if (dir->dynindx != -1) 8332 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); 8333 dir->dynindx = ind->dynindx; 8334 dir->dynstr_index = ind->dynstr_index; 8335 ind->dynindx = -1; 8336 ind->dynstr_index = 0; 8337 } 8338 } 8339 8340 void 8341 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, 8342 struct elf_link_hash_entry *h, 8343 bool force_local) 8344 { 8345 /* STT_GNU_IFUNC symbol must go through PLT. */ 8346 if (h->type != STT_GNU_IFUNC) 8347 { 8348 h->plt = elf_hash_table (info)->init_plt_offset; 8349 h->needs_plt = 0; 8350 } 8351 if (force_local) 8352 { 8353 h->forced_local = 1; 8354 if (h->dynindx != -1) 8355 { 8356 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 8357 h->dynstr_index); 8358 h->dynindx = -1; 8359 h->dynstr_index = 0; 8360 } 8361 } 8362 } 8363 8364 /* Hide a symbol. */ 8365 8366 void 8367 _bfd_elf_link_hide_symbol (bfd *output_bfd, 8368 struct bfd_link_info *info, 8369 struct bfd_link_hash_entry *h) 8370 { 8371 if (is_elf_hash_table (info->hash)) 8372 { 8373 const struct elf_backend_data *bed 8374 = get_elf_backend_data (output_bfd); 8375 struct elf_link_hash_entry *eh 8376 = (struct elf_link_hash_entry *) h; 8377 bed->elf_backend_hide_symbol (info, eh, true); 8378 eh->def_dynamic = 0; 8379 eh->ref_dynamic = 0; 8380 eh->dynamic_def = 0; 8381 } 8382 } 8383 8384 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our 8385 caller. */ 8386 8387 bool 8388 _bfd_elf_link_hash_table_init 8389 (struct elf_link_hash_table *table, 8390 bfd *abfd, 8391 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 8392 struct bfd_hash_table *, 8393 const char *), 8394 unsigned int entsize) 8395 { 8396 bool ret; 8397 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8398 int can_refcount = bed->can_refcount; 8399 8400 table->init_got_refcount.refcount = can_refcount - 1; 8401 table->init_plt_refcount.refcount = can_refcount - 1; 8402 table->init_got_offset.offset = -(bfd_vma) 1; 8403 table->init_plt_offset.offset = -(bfd_vma) 1; 8404 /* The first dynamic symbol is a dummy. */ 8405 table->dynsymcount = 1; 8406 8407 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 8408 8409 table->root.type = bfd_link_elf_hash_table; 8410 table->hash_table_id = bed->target_id; 8411 table->target_os = bed->target_os; 8412 table->root.hash_table_free = _bfd_elf_link_hash_table_free; 8413 8414 return ret; 8415 } 8416 8417 /* Create an ELF linker hash table. */ 8418 8419 struct bfd_link_hash_table * 8420 _bfd_elf_link_hash_table_create (bfd *abfd) 8421 { 8422 struct elf_link_hash_table *ret; 8423 size_t amt = sizeof (struct elf_link_hash_table); 8424 8425 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); 8426 if (ret == NULL) 8427 return NULL; 8428 8429 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, 8430 sizeof (struct elf_link_hash_entry))) 8431 { 8432 free (ret); 8433 return NULL; 8434 } 8435 8436 return &ret->root; 8437 } 8438 8439 /* Destroy an ELF linker hash table. */ 8440 8441 void 8442 _bfd_elf_link_hash_table_free (bfd *obfd) 8443 { 8444 struct elf_link_hash_table *htab; 8445 8446 htab = (struct elf_link_hash_table *) obfd->link.hash; 8447 if (htab->dynstr != NULL) 8448 _bfd_elf_strtab_free (htab->dynstr); 8449 _bfd_merge_sections_free (htab->merge_info); 8450 /* NB: htab->dynamic->contents is always allocated by bfd_realloc. */ 8451 if (htab->dynamic != NULL) 8452 { 8453 free (htab->dynamic->contents); 8454 htab->dynamic->contents = NULL; 8455 } 8456 if (htab->first_hash != NULL) 8457 { 8458 bfd_hash_table_free (htab->first_hash); 8459 free (htab->first_hash); 8460 } 8461 if (htab->eh_info.frame_hdr_is_compact) 8462 free (htab->eh_info.u.compact.entries); 8463 else 8464 free (htab->eh_info.u.dwarf.array); 8465 _bfd_generic_link_hash_table_free (obfd); 8466 } 8467 8468 /* This is a hook for the ELF emulation code in the generic linker to 8469 tell the backend linker what file name to use for the DT_NEEDED 8470 entry for a dynamic object. */ 8471 8472 void 8473 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) 8474 { 8475 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 8476 && bfd_get_format (abfd) == bfd_object) 8477 elf_dt_name (abfd) = name; 8478 } 8479 8480 int 8481 bfd_elf_get_dyn_lib_class (bfd *abfd) 8482 { 8483 int lib_class; 8484 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 8485 && bfd_get_format (abfd) == bfd_object) 8486 lib_class = elf_dyn_lib_class (abfd); 8487 else 8488 lib_class = 0; 8489 return lib_class; 8490 } 8491 8492 void 8493 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) 8494 { 8495 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 8496 && bfd_get_format (abfd) == bfd_object) 8497 elf_dyn_lib_class (abfd) = lib_class; 8498 } 8499 8500 /* Get the list of DT_NEEDED entries for a link. This is a hook for 8501 the linker ELF emulation code. */ 8502 8503 struct bfd_link_needed_list * 8504 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 8505 struct bfd_link_info *info) 8506 { 8507 if (! is_elf_hash_table (info->hash)) 8508 return NULL; 8509 return elf_hash_table (info)->needed; 8510 } 8511 8512 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 8513 hook for the linker ELF emulation code. */ 8514 8515 struct bfd_link_needed_list * 8516 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, 8517 struct bfd_link_info *info) 8518 { 8519 if (! is_elf_hash_table (info->hash)) 8520 return NULL; 8521 return elf_hash_table (info)->runpath; 8522 } 8523 8524 /* Get the name actually used for a dynamic object for a link. This 8525 is the SONAME entry if there is one. Otherwise, it is the string 8526 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 8527 8528 const char * 8529 bfd_elf_get_dt_soname (bfd *abfd) 8530 { 8531 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 8532 && bfd_get_format (abfd) == bfd_object) 8533 return elf_dt_name (abfd); 8534 return NULL; 8535 } 8536 8537 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for 8538 the ELF linker emulation code. */ 8539 8540 bool 8541 bfd_elf_get_bfd_needed_list (bfd *abfd, 8542 struct bfd_link_needed_list **pneeded) 8543 { 8544 asection *s; 8545 bfd_byte *dynbuf = NULL; 8546 unsigned int elfsec; 8547 unsigned long shlink; 8548 bfd_byte *extdyn, *extdynend; 8549 size_t extdynsize; 8550 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 8551 8552 *pneeded = NULL; 8553 8554 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 8555 || bfd_get_format (abfd) != bfd_object) 8556 return true; 8557 8558 s = bfd_get_section_by_name (abfd, ".dynamic"); 8559 if (s == NULL || s->size == 0 || (s->flags & SEC_HAS_CONTENTS) == 0) 8560 return true; 8561 8562 if (!_bfd_elf_mmap_section_contents (abfd, s, &dynbuf)) 8563 goto error_return; 8564 8565 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 8566 if (elfsec == SHN_BAD) 8567 goto error_return; 8568 8569 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 8570 8571 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 8572 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 8573 8574 for (extdyn = dynbuf, extdynend = dynbuf + s->size; 8575 (size_t) (extdynend - extdyn) >= extdynsize; 8576 extdyn += extdynsize) 8577 { 8578 Elf_Internal_Dyn dyn; 8579 8580 (*swap_dyn_in) (abfd, extdyn, &dyn); 8581 8582 if (dyn.d_tag == DT_NULL) 8583 break; 8584 8585 if (dyn.d_tag == DT_NEEDED) 8586 { 8587 const char *string; 8588 struct bfd_link_needed_list *l; 8589 unsigned int tagv = dyn.d_un.d_val; 8590 size_t amt; 8591 8592 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 8593 if (string == NULL) 8594 goto error_return; 8595 8596 amt = sizeof *l; 8597 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 8598 if (l == NULL) 8599 goto error_return; 8600 8601 l->by = abfd; 8602 l->name = string; 8603 l->next = *pneeded; 8604 *pneeded = l; 8605 } 8606 } 8607 8608 _bfd_elf_munmap_section_contents (s, dynbuf); 8609 8610 return true; 8611 8612 error_return: 8613 _bfd_elf_munmap_section_contents (s, dynbuf); 8614 return false; 8615 } 8616 8617 struct elf_symbuf_symbol 8618 { 8619 unsigned long st_name; /* Symbol name, index in string tbl */ 8620 unsigned char st_info; /* Type and binding attributes */ 8621 unsigned char st_other; /* Visibilty, and target specific */ 8622 }; 8623 8624 struct elf_symbuf_head 8625 { 8626 struct elf_symbuf_symbol *ssym; 8627 size_t count; 8628 unsigned int st_shndx; 8629 }; 8630 8631 struct elf_symbol 8632 { 8633 union 8634 { 8635 Elf_Internal_Sym *isym; 8636 struct elf_symbuf_symbol *ssym; 8637 void *p; 8638 } u; 8639 const char *name; 8640 }; 8641 8642 /* Sort references to symbols by ascending section number. */ 8643 8644 static int 8645 elf_sort_elf_symbol (const void *arg1, const void *arg2) 8646 { 8647 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; 8648 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; 8649 8650 if (s1->st_shndx != s2->st_shndx) 8651 return s1->st_shndx > s2->st_shndx ? 1 : -1; 8652 /* Final sort by the address of the sym in the symbuf ensures 8653 a stable sort. */ 8654 if (s1 != s2) 8655 return s1 > s2 ? 1 : -1; 8656 return 0; 8657 } 8658 8659 static int 8660 elf_sym_name_compare (const void *arg1, const void *arg2) 8661 { 8662 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; 8663 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; 8664 int ret = strcmp (s1->name, s2->name); 8665 if (ret != 0) 8666 return ret; 8667 if (s1->u.p != s2->u.p) 8668 return s1->u.p > s2->u.p ? 1 : -1; 8669 return 0; 8670 } 8671 8672 static struct elf_symbuf_head * 8673 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) 8674 { 8675 Elf_Internal_Sym **ind, **indbufend, **indbuf; 8676 struct elf_symbuf_symbol *ssym; 8677 struct elf_symbuf_head *ssymbuf, *ssymhead; 8678 size_t i, shndx_count, total_size, amt; 8679 8680 amt = symcount * sizeof (*indbuf); 8681 indbuf = (Elf_Internal_Sym **) bfd_malloc (amt); 8682 if (indbuf == NULL) 8683 return NULL; 8684 8685 for (ind = indbuf, i = 0; i < symcount; i++) 8686 if (isymbuf[i].st_shndx != SHN_UNDEF) 8687 *ind++ = &isymbuf[i]; 8688 indbufend = ind; 8689 8690 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), 8691 elf_sort_elf_symbol); 8692 8693 shndx_count = 0; 8694 if (indbufend > indbuf) 8695 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) 8696 if (ind[0]->st_shndx != ind[1]->st_shndx) 8697 shndx_count++; 8698 8699 total_size = ((shndx_count + 1) * sizeof (*ssymbuf) 8700 + (indbufend - indbuf) * sizeof (*ssym)); 8701 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); 8702 if (ssymbuf == NULL) 8703 { 8704 free (indbuf); 8705 return NULL; 8706 } 8707 8708 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); 8709 ssymbuf->ssym = NULL; 8710 ssymbuf->count = shndx_count; 8711 ssymbuf->st_shndx = 0; 8712 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) 8713 { 8714 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) 8715 { 8716 ssymhead++; 8717 ssymhead->ssym = ssym; 8718 ssymhead->count = 0; 8719 ssymhead->st_shndx = (*ind)->st_shndx; 8720 } 8721 ssym->st_name = (*ind)->st_name; 8722 ssym->st_info = (*ind)->st_info; 8723 ssym->st_other = (*ind)->st_other; 8724 ssymhead->count++; 8725 } 8726 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count 8727 && (uintptr_t) ssym - (uintptr_t) ssymbuf == total_size); 8728 8729 free (indbuf); 8730 return ssymbuf; 8731 } 8732 8733 /* Check if 2 sections define the same set of local and global 8734 symbols. */ 8735 8736 static bool 8737 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, 8738 struct bfd_link_info *info) 8739 { 8740 bfd *bfd1, *bfd2; 8741 const struct elf_backend_data *bed1, *bed2; 8742 Elf_Internal_Shdr *hdr1, *hdr2; 8743 size_t symcount1, symcount2; 8744 Elf_Internal_Sym *isymbuf1, *isymbuf2; 8745 struct elf_symbuf_head *ssymbuf1, *ssymbuf2; 8746 Elf_Internal_Sym *isym, *isymend; 8747 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; 8748 size_t count1, count2, sec_count1, sec_count2, i; 8749 unsigned int shndx1, shndx2; 8750 bool result; 8751 bool ignore_section_symbol_p; 8752 8753 bfd1 = sec1->owner; 8754 bfd2 = sec2->owner; 8755 8756 /* Both sections have to be in ELF. */ 8757 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour 8758 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) 8759 return false; 8760 8761 if (elf_section_type (sec1) != elf_section_type (sec2)) 8762 return false; 8763 8764 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); 8765 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); 8766 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) 8767 return false; 8768 8769 bed1 = get_elf_backend_data (bfd1); 8770 bed2 = get_elf_backend_data (bfd2); 8771 hdr1 = &elf_tdata (bfd1)->symtab_hdr; 8772 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; 8773 hdr2 = &elf_tdata (bfd2)->symtab_hdr; 8774 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; 8775 8776 if (symcount1 == 0 || symcount2 == 0) 8777 return false; 8778 8779 result = false; 8780 isymbuf1 = NULL; 8781 isymbuf2 = NULL; 8782 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; 8783 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; 8784 8785 /* Ignore section symbols only when matching non-debugging sections 8786 or linkonce section with comdat section. */ 8787 ignore_section_symbol_p 8788 = ((sec1->flags & SEC_DEBUGGING) == 0 8789 || ((elf_section_flags (sec1) & SHF_GROUP) 8790 != (elf_section_flags (sec2) & SHF_GROUP))); 8791 8792 if (ssymbuf1 == NULL) 8793 { 8794 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, 8795 NULL, NULL, NULL); 8796 if (isymbuf1 == NULL) 8797 goto done; 8798 8799 if (info != NULL && !info->reduce_memory_overheads) 8800 { 8801 ssymbuf1 = elf_create_symbuf (symcount1, isymbuf1); 8802 elf_tdata (bfd1)->symbuf = ssymbuf1; 8803 } 8804 } 8805 8806 if (ssymbuf1 == NULL || ssymbuf2 == NULL) 8807 { 8808 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, 8809 NULL, NULL, NULL); 8810 if (isymbuf2 == NULL) 8811 goto done; 8812 8813 if (ssymbuf1 != NULL && info != NULL && !info->reduce_memory_overheads) 8814 { 8815 ssymbuf2 = elf_create_symbuf (symcount2, isymbuf2); 8816 elf_tdata (bfd2)->symbuf = ssymbuf2; 8817 } 8818 } 8819 8820 if (ssymbuf1 != NULL && ssymbuf2 != NULL) 8821 { 8822 /* Optimized faster version. */ 8823 size_t lo, hi, mid; 8824 struct elf_symbol *symp; 8825 struct elf_symbuf_symbol *ssym, *ssymend; 8826 8827 lo = 0; 8828 hi = ssymbuf1->count; 8829 ssymbuf1++; 8830 count1 = 0; 8831 sec_count1 = 0; 8832 while (lo < hi) 8833 { 8834 mid = (lo + hi) / 2; 8835 if (shndx1 < ssymbuf1[mid].st_shndx) 8836 hi = mid; 8837 else if (shndx1 > ssymbuf1[mid].st_shndx) 8838 lo = mid + 1; 8839 else 8840 { 8841 count1 = ssymbuf1[mid].count; 8842 ssymbuf1 += mid; 8843 break; 8844 } 8845 } 8846 if (ignore_section_symbol_p) 8847 { 8848 for (i = 0; i < count1; i++) 8849 if (ELF_ST_TYPE (ssymbuf1->ssym[i].st_info) == STT_SECTION) 8850 sec_count1++; 8851 count1 -= sec_count1; 8852 } 8853 8854 lo = 0; 8855 hi = ssymbuf2->count; 8856 ssymbuf2++; 8857 count2 = 0; 8858 sec_count2 = 0; 8859 while (lo < hi) 8860 { 8861 mid = (lo + hi) / 2; 8862 if (shndx2 < ssymbuf2[mid].st_shndx) 8863 hi = mid; 8864 else if (shndx2 > ssymbuf2[mid].st_shndx) 8865 lo = mid + 1; 8866 else 8867 { 8868 count2 = ssymbuf2[mid].count; 8869 ssymbuf2 += mid; 8870 break; 8871 } 8872 } 8873 if (ignore_section_symbol_p) 8874 { 8875 for (i = 0; i < count2; i++) 8876 if (ELF_ST_TYPE (ssymbuf2->ssym[i].st_info) == STT_SECTION) 8877 sec_count2++; 8878 count2 -= sec_count2; 8879 } 8880 8881 if (count1 == 0 || count2 == 0 || count1 != count2) 8882 goto done; 8883 8884 symtable1 8885 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); 8886 symtable2 8887 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); 8888 if (symtable1 == NULL || symtable2 == NULL) 8889 goto done; 8890 8891 symp = symtable1; 8892 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1 + sec_count1; 8893 ssym < ssymend; ssym++) 8894 if (sec_count1 == 0 8895 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION) 8896 { 8897 symp->u.ssym = ssym; 8898 symp->name = bfd_elf_string_from_elf_section (bfd1, 8899 hdr1->sh_link, 8900 ssym->st_name); 8901 if (symp->name == NULL) 8902 goto done; 8903 symp++; 8904 } 8905 8906 symp = symtable2; 8907 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2 + sec_count2; 8908 ssym < ssymend; ssym++) 8909 if (sec_count2 == 0 8910 || ELF_ST_TYPE (ssym->st_info) != STT_SECTION) 8911 { 8912 symp->u.ssym = ssym; 8913 symp->name = bfd_elf_string_from_elf_section (bfd2, 8914 hdr2->sh_link, 8915 ssym->st_name); 8916 if (symp->name == NULL) 8917 goto done; 8918 symp++; 8919 } 8920 8921 /* Sort symbol by name. */ 8922 qsort (symtable1, count1, sizeof (struct elf_symbol), 8923 elf_sym_name_compare); 8924 qsort (symtable2, count1, sizeof (struct elf_symbol), 8925 elf_sym_name_compare); 8926 8927 for (i = 0; i < count1; i++) 8928 /* Two symbols must have the same binding, type and name. */ 8929 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info 8930 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other 8931 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 8932 goto done; 8933 8934 result = true; 8935 goto done; 8936 } 8937 8938 symtable1 = (struct elf_symbol *) 8939 bfd_malloc (symcount1 * sizeof (struct elf_symbol)); 8940 symtable2 = (struct elf_symbol *) 8941 bfd_malloc (symcount2 * sizeof (struct elf_symbol)); 8942 if (symtable1 == NULL || symtable2 == NULL) 8943 goto done; 8944 8945 /* Count definitions in the section. */ 8946 count1 = 0; 8947 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) 8948 if (isym->st_shndx == shndx1 8949 && (!ignore_section_symbol_p 8950 || ELF_ST_TYPE (isym->st_info) != STT_SECTION)) 8951 symtable1[count1++].u.isym = isym; 8952 8953 count2 = 0; 8954 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) 8955 if (isym->st_shndx == shndx2 8956 && (!ignore_section_symbol_p 8957 || ELF_ST_TYPE (isym->st_info) != STT_SECTION)) 8958 symtable2[count2++].u.isym = isym; 8959 8960 if (count1 == 0 || count2 == 0 || count1 != count2) 8961 goto done; 8962 8963 for (i = 0; i < count1; i++) 8964 { 8965 symtable1[i].name 8966 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, 8967 symtable1[i].u.isym->st_name); 8968 if (symtable1[i].name == NULL) 8969 goto done; 8970 } 8971 8972 for (i = 0; i < count2; i++) 8973 { 8974 symtable2[i].name 8975 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, 8976 symtable2[i].u.isym->st_name); 8977 if (symtable2[i].name == NULL) 8978 goto done; 8979 } 8980 8981 /* Sort symbol by name. */ 8982 qsort (symtable1, count1, sizeof (struct elf_symbol), 8983 elf_sym_name_compare); 8984 qsort (symtable2, count1, sizeof (struct elf_symbol), 8985 elf_sym_name_compare); 8986 8987 for (i = 0; i < count1; i++) 8988 /* Two symbols must have the same binding, type and name. */ 8989 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info 8990 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other 8991 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 8992 goto done; 8993 8994 result = true; 8995 8996 done: 8997 free (symtable1); 8998 free (symtable2); 8999 free (isymbuf1); 9000 free (isymbuf2); 9001 9002 return result; 9003 } 9004 9005 /* Return TRUE if 2 section types are compatible. */ 9006 9007 bool 9008 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, 9009 bfd *bbfd, const asection *bsec) 9010 { 9011 if (asec == NULL 9012 || bsec == NULL 9013 || abfd->xvec->flavour != bfd_target_elf_flavour 9014 || bbfd->xvec->flavour != bfd_target_elf_flavour) 9015 return true; 9016 9017 return elf_section_type (asec) == elf_section_type (bsec); 9018 } 9019 9020 /* Final phase of ELF linker. */ 9022 9023 /* A structure we use to avoid passing large numbers of arguments. */ 9024 9025 struct elf_final_link_info 9026 { 9027 /* General link information. */ 9028 struct bfd_link_info *info; 9029 /* Output BFD. */ 9030 bfd *output_bfd; 9031 /* Symbol string table. */ 9032 struct elf_strtab_hash *symstrtab; 9033 /* .hash section. */ 9034 asection *hash_sec; 9035 /* symbol version section (.gnu.version). */ 9036 asection *symver_sec; 9037 /* Buffer large enough to hold contents of any section. */ 9038 bfd_byte *contents; 9039 /* Buffer large enough to hold external relocs of any section. */ 9040 void *external_relocs; 9041 /* Buffer large enough to hold internal relocs of any section. */ 9042 Elf_Internal_Rela *internal_relocs; 9043 /* Buffer large enough to hold external local symbols of any input 9044 BFD. */ 9045 bfd_byte *external_syms; 9046 /* And a buffer for symbol section indices. */ 9047 Elf_External_Sym_Shndx *locsym_shndx; 9048 /* Buffer large enough to hold internal local symbols of any input 9049 BFD. */ 9050 Elf_Internal_Sym *internal_syms; 9051 /* Array large enough to hold a symbol index for each local symbol 9052 of any input BFD. */ 9053 long *indices; 9054 /* Array large enough to hold a section pointer for each local 9055 symbol of any input BFD. */ 9056 asection **sections; 9057 /* Buffer for SHT_SYMTAB_SHNDX section. */ 9058 Elf_External_Sym_Shndx *symshndxbuf; 9059 /* Number of STT_FILE syms seen. */ 9060 size_t filesym_count; 9061 /* Local symbol hash table. */ 9062 struct bfd_hash_table local_hash_table; 9063 }; 9064 9065 struct local_hash_entry 9066 { 9067 /* Base hash table entry structure. */ 9068 struct bfd_hash_entry root; 9069 /* Size of the local symbol name. */ 9070 size_t size; 9071 /* Number of the duplicated local symbol names. */ 9072 long count; 9073 }; 9074 9075 /* Create an entry in the local symbol hash table. */ 9076 9077 static struct bfd_hash_entry * 9078 local_hash_newfunc (struct bfd_hash_entry *entry, 9079 struct bfd_hash_table *table, 9080 const char *string) 9081 { 9082 9083 /* Allocate the structure if it has not already been allocated by a 9084 subclass. */ 9085 if (entry == NULL) 9086 { 9087 entry = bfd_hash_allocate (table, 9088 sizeof (struct local_hash_entry)); 9089 if (entry == NULL) 9090 return entry; 9091 } 9092 9093 /* Call the allocation method of the superclass. */ 9094 entry = bfd_hash_newfunc (entry, table, string); 9095 if (entry != NULL) 9096 { 9097 ((struct local_hash_entry *) entry)->count = 0; 9098 ((struct local_hash_entry *) entry)->size = 0; 9099 } 9100 9101 return entry; 9102 } 9103 9104 /* This struct is used to pass information to elf_link_output_extsym. */ 9105 9106 struct elf_outext_info 9107 { 9108 bool failed; 9109 bool localsyms; 9110 bool file_sym_done; 9111 struct elf_final_link_info *flinfo; 9112 }; 9113 9114 9115 /* Support for evaluating a complex relocation. 9116 9117 Complex relocations are generalized, self-describing relocations. The 9118 implementation of them consists of two parts: complex symbols, and the 9119 relocations themselves. 9120 9121 The relocations use a reserved elf-wide relocation type code (R_RELC 9122 external / BFD_RELOC_RELC internal) and an encoding of relocation field 9123 information (start bit, end bit, word width, etc) into the addend. This 9124 information is extracted from CGEN-generated operand tables within gas. 9125 9126 Complex symbols are mangled symbols (STT_RELC external / BSF_RELC 9127 internal) representing prefix-notation expressions, including but not 9128 limited to those sorts of expressions normally encoded as addends in the 9129 addend field. The symbol mangling format is: 9130 9131 <node> := <literal> 9132 | <unary-operator> ':' <node> 9133 | <binary-operator> ':' <node> ':' <node> 9134 ; 9135 9136 <literal> := 's' <digits=N> ':' <N character symbol name> 9137 | 'S' <digits=N> ':' <N character section name> 9138 | '#' <hexdigits> 9139 ; 9140 9141 <binary-operator> := as in C 9142 <unary-operator> := as in C, plus "0-" for unambiguous negation. */ 9143 9144 static void 9145 set_symbol_value (bfd *bfd_with_globals, 9146 Elf_Internal_Sym *isymbuf, 9147 size_t locsymcount, 9148 size_t symidx, 9149 bfd_vma val) 9150 { 9151 struct elf_link_hash_entry *h; 9152 size_t extsymoff = locsymcount; 9153 9154 if (symidx < locsymcount) 9155 { 9156 Elf_Internal_Sym *sym; 9157 9158 sym = isymbuf + symidx; 9159 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) 9160 { 9161 /* It is a local symbol: move it to the 9162 "absolute" section and give it a value. */ 9163 sym->st_shndx = SHN_ABS; 9164 sym->st_value = val; 9165 return; 9166 } 9167 BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); 9168 extsymoff = 0; 9169 } 9170 9171 /* It is a global symbol: set its link type 9172 to "defined" and give it a value. */ 9173 h = get_link_hash_entry (elf_sym_hashes (bfd_with_globals), symidx, extsymoff); 9174 if (h == NULL) 9175 { 9176 /* FIXMEL What should we do ? */ 9177 return; 9178 } 9179 h->root.type = bfd_link_hash_defined; 9180 h->root.u.def.value = val; 9181 h->root.u.def.section = bfd_abs_section_ptr; 9182 } 9183 9184 static bool 9185 resolve_symbol (const char *name, 9186 bfd *input_bfd, 9187 struct elf_final_link_info *flinfo, 9188 bfd_vma *result, 9189 Elf_Internal_Sym *isymbuf, 9190 size_t locsymcount) 9191 { 9192 Elf_Internal_Sym *sym; 9193 struct bfd_link_hash_entry *global_entry; 9194 const char *candidate = NULL; 9195 Elf_Internal_Shdr *symtab_hdr; 9196 size_t i; 9197 9198 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 9199 9200 for (i = 0; i < locsymcount; ++ i) 9201 { 9202 sym = isymbuf + i; 9203 9204 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) 9205 continue; 9206 9207 candidate = bfd_elf_string_from_elf_section (input_bfd, 9208 symtab_hdr->sh_link, 9209 sym->st_name); 9210 #ifdef DEBUG 9211 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", 9212 name, candidate, (unsigned long) sym->st_value); 9213 #endif 9214 if (candidate && strcmp (candidate, name) == 0) 9215 { 9216 asection *sec = flinfo->sections [i]; 9217 9218 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); 9219 *result += sec->output_offset + sec->output_section->vma; 9220 #ifdef DEBUG 9221 printf ("Found symbol with value %8.8lx\n", 9222 (unsigned long) *result); 9223 #endif 9224 return true; 9225 } 9226 } 9227 9228 /* Hmm, haven't found it yet. perhaps it is a global. */ 9229 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, 9230 false, false, true); 9231 if (!global_entry) 9232 return false; 9233 9234 if (global_entry->type == bfd_link_hash_defined 9235 || global_entry->type == bfd_link_hash_defweak) 9236 { 9237 *result = (global_entry->u.def.value 9238 + global_entry->u.def.section->output_section->vma 9239 + global_entry->u.def.section->output_offset); 9240 #ifdef DEBUG 9241 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", 9242 global_entry->root.string, (unsigned long) *result); 9243 #endif 9244 return true; 9245 } 9246 9247 return false; 9248 } 9249 9250 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in 9251 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section 9252 names like "foo.end" which is the end address of section "foo". */ 9253 9254 static bool 9255 resolve_section (const char *name, 9256 asection *sections, 9257 bfd_vma *result, 9258 bfd * abfd) 9259 { 9260 asection *curr; 9261 unsigned int len; 9262 9263 for (curr = sections; curr; curr = curr->next) 9264 if (strcmp (curr->name, name) == 0) 9265 { 9266 *result = curr->vma; 9267 return true; 9268 } 9269 9270 /* Hmm. still haven't found it. try pseudo-section names. */ 9271 /* FIXME: This could be coded more efficiently... */ 9272 for (curr = sections; curr; curr = curr->next) 9273 { 9274 len = strlen (curr->name); 9275 if (len > strlen (name)) 9276 continue; 9277 9278 if (strncmp (curr->name, name, len) == 0) 9279 { 9280 if (startswith (name + len, ".end")) 9281 { 9282 *result = (curr->vma 9283 + curr->size / bfd_octets_per_byte (abfd, curr)); 9284 return true; 9285 } 9286 9287 /* Insert more pseudo-section names here, if you like. */ 9288 } 9289 } 9290 9291 return false; 9292 } 9293 9294 static void 9295 undefined_reference (const char *reftype, const char *name) 9296 { 9297 /* xgettext:c-format */ 9298 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), 9299 reftype, name); 9300 bfd_set_error (bfd_error_bad_value); 9301 } 9302 9303 static bool 9304 eval_symbol (bfd_vma *result, 9305 const char **symp, 9306 bfd *input_bfd, 9307 struct elf_final_link_info *flinfo, 9308 bfd_vma dot, 9309 Elf_Internal_Sym *isymbuf, 9310 size_t locsymcount, 9311 int signed_p) 9312 { 9313 size_t len; 9314 size_t symlen; 9315 bfd_vma a; 9316 bfd_vma b; 9317 char symbuf[4096]; 9318 const char *sym = *symp; 9319 const char *symend; 9320 bool symbol_is_section = false; 9321 9322 len = strlen (sym); 9323 symend = sym + len; 9324 9325 if (len < 1 || len > sizeof (symbuf)) 9326 { 9327 bfd_set_error (bfd_error_invalid_operation); 9328 return false; 9329 } 9330 9331 switch (* sym) 9332 { 9333 case '.': 9334 *result = dot; 9335 *symp = sym + 1; 9336 return true; 9337 9338 case '#': 9339 ++sym; 9340 *result = strtoul (sym, (char **) symp, 16); 9341 return true; 9342 9343 case 'S': 9344 symbol_is_section = true; 9345 /* Fall through. */ 9346 case 's': 9347 ++sym; 9348 symlen = strtol (sym, (char **) symp, 10); 9349 sym = *symp + 1; /* Skip the trailing ':'. */ 9350 9351 if (symend < sym || symlen + 1 > sizeof (symbuf)) 9352 { 9353 bfd_set_error (bfd_error_invalid_operation); 9354 return false; 9355 } 9356 9357 memcpy (symbuf, sym, symlen); 9358 symbuf[symlen] = '\0'; 9359 *symp = sym + symlen; 9360 9361 /* Is it always possible, with complex symbols, that gas "mis-guessed" 9362 the symbol as a section, or vice-versa. so we're pretty liberal in our 9363 interpretation here; section means "try section first", not "must be a 9364 section", and likewise with symbol. */ 9365 9366 if (symbol_is_section) 9367 { 9368 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) 9369 && !resolve_symbol (symbuf, input_bfd, flinfo, result, 9370 isymbuf, locsymcount)) 9371 { 9372 undefined_reference ("section", symbuf); 9373 return false; 9374 } 9375 } 9376 else 9377 { 9378 if (!resolve_symbol (symbuf, input_bfd, flinfo, result, 9379 isymbuf, locsymcount) 9380 && !resolve_section (symbuf, flinfo->output_bfd->sections, 9381 result, input_bfd)) 9382 { 9383 undefined_reference ("symbol", symbuf); 9384 return false; 9385 } 9386 } 9387 9388 return true; 9389 9390 /* All that remains are operators. */ 9391 9392 #define UNARY_OP(op) \ 9393 if (startswith (sym, #op)) \ 9394 { \ 9395 sym += strlen (#op); \ 9396 if (*sym == ':') \ 9397 ++sym; \ 9398 *symp = sym; \ 9399 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 9400 isymbuf, locsymcount, signed_p)) \ 9401 return false; \ 9402 if (signed_p) \ 9403 *result = op ((bfd_signed_vma) a); \ 9404 else \ 9405 *result = op a; \ 9406 return true; \ 9407 } 9408 9409 #define BINARY_OP_HEAD(op) \ 9410 if (startswith (sym, #op)) \ 9411 { \ 9412 sym += strlen (#op); \ 9413 if (*sym == ':') \ 9414 ++sym; \ 9415 *symp = sym; \ 9416 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 9417 isymbuf, locsymcount, signed_p)) \ 9418 return false; \ 9419 ++*symp; \ 9420 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ 9421 isymbuf, locsymcount, signed_p)) \ 9422 return false; 9423 #define BINARY_OP_TAIL(op) \ 9424 if (signed_p) \ 9425 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ 9426 else \ 9427 *result = a op b; \ 9428 return true; \ 9429 } 9430 #define BINARY_OP(op) BINARY_OP_HEAD(op) BINARY_OP_TAIL(op) 9431 9432 default: 9433 UNARY_OP (0-); 9434 BINARY_OP_HEAD (<<); 9435 if (b >= sizeof (a) * CHAR_BIT) 9436 { 9437 *result = 0; 9438 return true; 9439 } 9440 signed_p = 0; 9441 BINARY_OP_TAIL (<<); 9442 BINARY_OP_HEAD (>>); 9443 if (b >= sizeof (a) * CHAR_BIT) 9444 { 9445 *result = signed_p && (bfd_signed_vma) a < 0 ? -1 : 0; 9446 return true; 9447 } 9448 BINARY_OP_TAIL (>>); 9449 BINARY_OP (==); 9450 BINARY_OP (!=); 9451 BINARY_OP (<=); 9452 BINARY_OP (>=); 9453 BINARY_OP (&&); 9454 BINARY_OP (||); 9455 UNARY_OP (~); 9456 UNARY_OP (!); 9457 BINARY_OP (*); 9458 BINARY_OP_HEAD (/); 9459 if (b == 0) 9460 { 9461 _bfd_error_handler (_("division by zero")); 9462 bfd_set_error (bfd_error_bad_value); 9463 return false; 9464 } 9465 BINARY_OP_TAIL (/); 9466 BINARY_OP_HEAD (%); 9467 if (b == 0) 9468 { 9469 _bfd_error_handler (_("division by zero")); 9470 bfd_set_error (bfd_error_bad_value); 9471 return false; 9472 } 9473 BINARY_OP_TAIL (%); 9474 BINARY_OP (^); 9475 BINARY_OP (|); 9476 BINARY_OP (&); 9477 BINARY_OP (+); 9478 BINARY_OP (-); 9479 BINARY_OP (<); 9480 BINARY_OP (>); 9481 #undef UNARY_OP 9482 #undef BINARY_OP 9483 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); 9484 bfd_set_error (bfd_error_invalid_operation); 9485 return false; 9486 } 9487 } 9488 9489 static void 9490 put_value (bfd_vma size, 9491 unsigned long chunksz, 9492 bfd *input_bfd, 9493 bfd_vma x, 9494 bfd_byte *location) 9495 { 9496 location += (size - chunksz); 9497 9498 for (; size; size -= chunksz, location -= chunksz) 9499 { 9500 switch (chunksz) 9501 { 9502 case 1: 9503 bfd_put_8 (input_bfd, x, location); 9504 x >>= 8; 9505 break; 9506 case 2: 9507 bfd_put_16 (input_bfd, x, location); 9508 x >>= 16; 9509 break; 9510 case 4: 9511 bfd_put_32 (input_bfd, x, location); 9512 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ 9513 x >>= 16; 9514 x >>= 16; 9515 break; 9516 #ifdef BFD64 9517 case 8: 9518 bfd_put_64 (input_bfd, x, location); 9519 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ 9520 x >>= 32; 9521 x >>= 32; 9522 break; 9523 #endif 9524 default: 9525 abort (); 9526 break; 9527 } 9528 } 9529 } 9530 9531 static bfd_vma 9532 get_value (bfd_vma size, 9533 unsigned long chunksz, 9534 bfd *input_bfd, 9535 bfd_byte *location) 9536 { 9537 int shift; 9538 bfd_vma x = 0; 9539 9540 /* Sanity checks. */ 9541 BFD_ASSERT (chunksz <= sizeof (x) 9542 && size >= chunksz 9543 && chunksz != 0 9544 && (size % chunksz) == 0 9545 && input_bfd != NULL 9546 && location != NULL); 9547 9548 if (chunksz == sizeof (x)) 9549 { 9550 BFD_ASSERT (size == chunksz); 9551 9552 /* Make sure that we do not perform an undefined shift operation. 9553 We know that size == chunksz so there will only be one iteration 9554 of the loop below. */ 9555 shift = 0; 9556 } 9557 else 9558 shift = 8 * chunksz; 9559 9560 for (; size; size -= chunksz, location += chunksz) 9561 { 9562 switch (chunksz) 9563 { 9564 case 1: 9565 x = (x << shift) | bfd_get_8 (input_bfd, location); 9566 break; 9567 case 2: 9568 x = (x << shift) | bfd_get_16 (input_bfd, location); 9569 break; 9570 case 4: 9571 x = (x << shift) | bfd_get_32 (input_bfd, location); 9572 break; 9573 #ifdef BFD64 9574 case 8: 9575 x = (x << shift) | bfd_get_64 (input_bfd, location); 9576 break; 9577 #endif 9578 default: 9579 abort (); 9580 } 9581 } 9582 return x; 9583 } 9584 9585 static void 9586 decode_complex_addend (unsigned long *start, /* in bits */ 9587 unsigned long *oplen, /* in bits */ 9588 unsigned long *len, /* in bits */ 9589 unsigned long *wordsz, /* in bytes */ 9590 unsigned long *chunksz, /* in bytes */ 9591 unsigned long *lsb0_p, 9592 unsigned long *signed_p, 9593 unsigned long *trunc_p, 9594 unsigned long encoded) 9595 { 9596 * start = encoded & 0x3F; 9597 * len = (encoded >> 6) & 0x3F; 9598 * oplen = (encoded >> 12) & 0x3F; 9599 * wordsz = (encoded >> 18) & 0xF; 9600 * chunksz = (encoded >> 22) & 0xF; 9601 * lsb0_p = (encoded >> 27) & 1; 9602 * signed_p = (encoded >> 28) & 1; 9603 * trunc_p = (encoded >> 29) & 1; 9604 } 9605 9606 bfd_reloc_status_type 9607 bfd_elf_perform_complex_relocation (bfd *input_bfd, 9608 asection *input_section, 9609 bfd_byte *contents, 9610 Elf_Internal_Rela *rel, 9611 bfd_vma relocation) 9612 { 9613 bfd_vma shift, x, mask; 9614 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; 9615 bfd_reloc_status_type r; 9616 bfd_size_type octets; 9617 9618 /* Perform this reloc, since it is complex. 9619 (this is not to say that it necessarily refers to a complex 9620 symbol; merely that it is a self-describing CGEN based reloc. 9621 i.e. the addend has the complete reloc information (bit start, end, 9622 word size, etc) encoded within it.). */ 9623 9624 decode_complex_addend (&start, &oplen, &len, &wordsz, 9625 &chunksz, &lsb0_p, &signed_p, 9626 &trunc_p, rel->r_addend); 9627 9628 mask = (((1L << (len - 1)) - 1) << 1) | 1; 9629 9630 if (lsb0_p) 9631 shift = (start + 1) - len; 9632 else 9633 shift = (8 * wordsz) - (start + len); 9634 9635 octets = rel->r_offset * bfd_octets_per_byte (input_bfd, input_section); 9636 x = get_value (wordsz, chunksz, input_bfd, contents + octets); 9637 9638 #ifdef DEBUG 9639 printf ("Doing complex reloc: " 9640 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " 9641 "chunksz %ld, start %ld, len %ld, oplen %ld\n" 9642 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", 9643 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, 9644 oplen, (unsigned long) x, (unsigned long) mask, 9645 (unsigned long) relocation); 9646 #endif 9647 9648 r = bfd_reloc_ok; 9649 if (! trunc_p) 9650 /* Now do an overflow check. */ 9651 r = bfd_check_overflow ((signed_p 9652 ? complain_overflow_signed 9653 : complain_overflow_unsigned), 9654 len, 0, (8 * wordsz), 9655 relocation); 9656 9657 /* Do the deed. */ 9658 x = (x & ~(mask << shift)) | ((relocation & mask) << shift); 9659 9660 #ifdef DEBUG 9661 printf (" relocation: %8.8lx\n" 9662 " shifted mask: %8.8lx\n" 9663 " shifted/masked reloc: %8.8lx\n" 9664 " result: %8.8lx\n", 9665 (unsigned long) relocation, (unsigned long) (mask << shift), 9666 (unsigned long) ((relocation & mask) << shift), (unsigned long) x); 9667 #endif 9668 put_value (wordsz, chunksz, input_bfd, x, contents + octets); 9669 return r; 9670 } 9671 9672 /* Functions to read r_offset from external (target order) reloc 9673 entry. Faster than bfd_getl32 et al, because we let the compiler 9674 know the value is aligned. */ 9675 9676 static bfd_vma 9677 ext32l_r_offset (const void *p) 9678 { 9679 union aligned32 9680 { 9681 uint32_t v; 9682 unsigned char c[4]; 9683 }; 9684 const union aligned32 *a 9685 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 9686 9687 uint32_t aval = ( (uint32_t) a->c[0] 9688 | (uint32_t) a->c[1] << 8 9689 | (uint32_t) a->c[2] << 16 9690 | (uint32_t) a->c[3] << 24); 9691 return aval; 9692 } 9693 9694 static bfd_vma 9695 ext32b_r_offset (const void *p) 9696 { 9697 union aligned32 9698 { 9699 uint32_t v; 9700 unsigned char c[4]; 9701 }; 9702 const union aligned32 *a 9703 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 9704 9705 uint32_t aval = ( (uint32_t) a->c[0] << 24 9706 | (uint32_t) a->c[1] << 16 9707 | (uint32_t) a->c[2] << 8 9708 | (uint32_t) a->c[3]); 9709 return aval; 9710 } 9711 9712 static bfd_vma 9713 ext64l_r_offset (const void *p) 9714 { 9715 union aligned64 9716 { 9717 uint64_t v; 9718 unsigned char c[8]; 9719 }; 9720 const union aligned64 *a 9721 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 9722 9723 uint64_t aval = ( (uint64_t) a->c[0] 9724 | (uint64_t) a->c[1] << 8 9725 | (uint64_t) a->c[2] << 16 9726 | (uint64_t) a->c[3] << 24 9727 | (uint64_t) a->c[4] << 32 9728 | (uint64_t) a->c[5] << 40 9729 | (uint64_t) a->c[6] << 48 9730 | (uint64_t) a->c[7] << 56); 9731 return aval; 9732 } 9733 9734 static bfd_vma 9735 ext64b_r_offset (const void *p) 9736 { 9737 union aligned64 9738 { 9739 uint64_t v; 9740 unsigned char c[8]; 9741 }; 9742 const union aligned64 *a 9743 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 9744 9745 uint64_t aval = ( (uint64_t) a->c[0] << 56 9746 | (uint64_t) a->c[1] << 48 9747 | (uint64_t) a->c[2] << 40 9748 | (uint64_t) a->c[3] << 32 9749 | (uint64_t) a->c[4] << 24 9750 | (uint64_t) a->c[5] << 16 9751 | (uint64_t) a->c[6] << 8 9752 | (uint64_t) a->c[7]); 9753 return aval; 9754 } 9755 9756 /* When performing a relocatable link, the input relocations are 9757 preserved. But, if they reference global symbols, the indices 9758 referenced must be updated. Update all the relocations found in 9759 RELDATA. */ 9760 9761 static bool 9762 elf_link_adjust_relocs (bfd *abfd, 9763 asection *sec, 9764 struct bfd_elf_section_reloc_data *reldata, 9765 bool sort, 9766 struct bfd_link_info *info) 9767 { 9768 unsigned int i; 9769 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9770 bfd_byte *erela; 9771 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 9772 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 9773 bfd_vma r_type_mask; 9774 int r_sym_shift; 9775 unsigned int count = reldata->count; 9776 struct elf_link_hash_entry **rel_hash = reldata->hashes; 9777 9778 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) 9779 { 9780 swap_in = bed->s->swap_reloc_in; 9781 swap_out = bed->s->swap_reloc_out; 9782 } 9783 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) 9784 { 9785 swap_in = bed->s->swap_reloca_in; 9786 swap_out = bed->s->swap_reloca_out; 9787 } 9788 else 9789 abort (); 9790 9791 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) 9792 abort (); 9793 9794 if (bed->s->arch_size == 32) 9795 { 9796 r_type_mask = 0xff; 9797 r_sym_shift = 8; 9798 } 9799 else 9800 { 9801 r_type_mask = 0xffffffff; 9802 r_sym_shift = 32; 9803 } 9804 9805 erela = reldata->hdr->contents; 9806 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) 9807 { 9808 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; 9809 unsigned int j; 9810 9811 if (*rel_hash == NULL) 9812 continue; 9813 9814 if ((*rel_hash)->indx == -2 9815 && info->gc_sections 9816 && ! info->gc_keep_exported) 9817 { 9818 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ 9819 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), 9820 abfd, sec, 9821 (*rel_hash)->root.root.string); 9822 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), 9823 abfd, sec); 9824 bfd_set_error (bfd_error_invalid_operation); 9825 return false; 9826 } 9827 BFD_ASSERT ((*rel_hash)->indx >= 0); 9828 9829 (*swap_in) (abfd, erela, irela); 9830 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) 9831 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift 9832 | (irela[j].r_info & r_type_mask)); 9833 (*swap_out) (abfd, irela, erela); 9834 } 9835 9836 if (bed->elf_backend_update_relocs) 9837 (*bed->elf_backend_update_relocs) (sec, reldata); 9838 9839 if (sort && count != 0) 9840 { 9841 bfd_vma (*ext_r_off) (const void *); 9842 bfd_vma r_off; 9843 size_t elt_size; 9844 bfd_byte *base, *end, *p, *loc; 9845 bfd_byte *buf = NULL; 9846 9847 if (bed->s->arch_size == 32) 9848 { 9849 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 9850 ext_r_off = ext32l_r_offset; 9851 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 9852 ext_r_off = ext32b_r_offset; 9853 else 9854 abort (); 9855 } 9856 else 9857 { 9858 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 9859 ext_r_off = ext64l_r_offset; 9860 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 9861 ext_r_off = ext64b_r_offset; 9862 else 9863 abort (); 9864 } 9865 9866 /* Must use a stable sort here. A modified insertion sort, 9867 since the relocs are mostly sorted already. */ 9868 elt_size = reldata->hdr->sh_entsize; 9869 base = reldata->hdr->contents; 9870 end = base + count * elt_size; 9871 if (elt_size > sizeof (Elf64_External_Rela)) 9872 abort (); 9873 9874 /* Ensure the first element is lowest. This acts as a sentinel, 9875 speeding the main loop below. */ 9876 r_off = (*ext_r_off) (base); 9877 for (p = loc = base; (p += elt_size) < end; ) 9878 { 9879 bfd_vma r_off2 = (*ext_r_off) (p); 9880 if (r_off > r_off2) 9881 { 9882 r_off = r_off2; 9883 loc = p; 9884 } 9885 } 9886 if (loc != base) 9887 { 9888 /* Don't just swap *base and *loc as that changes the order 9889 of the original base[0] and base[1] if they happen to 9890 have the same r_offset. */ 9891 bfd_byte onebuf[sizeof (Elf64_External_Rela)]; 9892 memcpy (onebuf, loc, elt_size); 9893 memmove (base + elt_size, base, loc - base); 9894 memcpy (base, onebuf, elt_size); 9895 } 9896 9897 for (p = base + elt_size; (p += elt_size) < end; ) 9898 { 9899 /* base to p is sorted, *p is next to insert. */ 9900 r_off = (*ext_r_off) (p); 9901 /* Search the sorted region for location to insert. */ 9902 loc = p - elt_size; 9903 while (r_off < (*ext_r_off) (loc)) 9904 loc -= elt_size; 9905 loc += elt_size; 9906 if (loc != p) 9907 { 9908 /* Chances are there is a run of relocs to insert here, 9909 from one of more input files. Files are not always 9910 linked in order due to the way elf_link_input_bfd is 9911 called. See pr17666. */ 9912 size_t sortlen = p - loc; 9913 bfd_vma r_off2 = (*ext_r_off) (loc); 9914 size_t runlen = elt_size; 9915 bfd_vma r_off_runend = r_off; 9916 bfd_vma r_off_runend_next; 9917 size_t buf_size = 96 * 1024; 9918 while (p + runlen < end 9919 && (sortlen <= buf_size 9920 || runlen + elt_size <= buf_size) 9921 /* run must not break the ordering of base..loc+1 */ 9922 && r_off2 > (r_off_runend_next = (*ext_r_off) (p + runlen)) 9923 /* run must be already sorted */ 9924 && r_off_runend_next >= r_off_runend) 9925 { 9926 runlen += elt_size; 9927 r_off_runend = r_off_runend_next; 9928 } 9929 if (buf == NULL) 9930 { 9931 buf = bfd_malloc (buf_size); 9932 if (buf == NULL) 9933 return false; 9934 } 9935 if (runlen < sortlen) 9936 { 9937 memcpy (buf, p, runlen); 9938 memmove (loc + runlen, loc, sortlen); 9939 memcpy (loc, buf, runlen); 9940 } 9941 else 9942 { 9943 memcpy (buf, loc, sortlen); 9944 memmove (loc, p, runlen); 9945 memcpy (loc + runlen, buf, sortlen); 9946 } 9947 p += runlen - elt_size; 9948 } 9949 } 9950 /* Hashes are no longer valid. */ 9951 free (reldata->hashes); 9952 reldata->hashes = NULL; 9953 free (buf); 9954 } 9955 return true; 9956 } 9957 9958 struct elf_link_sort_rela 9959 { 9960 union { 9961 bfd_vma offset; 9962 bfd_vma sym_mask; 9963 } u; 9964 enum elf_reloc_type_class type; 9965 /* We use this as an array of size int_rels_per_ext_rel. */ 9966 Elf_Internal_Rela rela[1]; 9967 }; 9968 9969 /* qsort stability here and for cmp2 is only an issue if multiple 9970 dynamic relocations are emitted at the same address. But targets 9971 that apply a series of dynamic relocations each operating on the 9972 result of the prior relocation can't use -z combreloc as 9973 implemented anyway. Such schemes tend to be broken by sorting on 9974 symbol index. That leaves dynamic NONE relocs as the only other 9975 case where ld might emit multiple relocs at the same address, and 9976 those are only emitted due to target bugs. */ 9977 9978 static int 9979 elf_link_sort_cmp1 (const void *A, const void *B) 9980 { 9981 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 9982 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 9983 int relativea, relativeb; 9984 9985 relativea = a->type == reloc_class_relative; 9986 relativeb = b->type == reloc_class_relative; 9987 9988 if (relativea < relativeb) 9989 return 1; 9990 if (relativea > relativeb) 9991 return -1; 9992 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) 9993 return -1; 9994 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) 9995 return 1; 9996 if (a->rela->r_offset < b->rela->r_offset) 9997 return -1; 9998 if (a->rela->r_offset > b->rela->r_offset) 9999 return 1; 10000 return 0; 10001 } 10002 10003 static int 10004 elf_link_sort_cmp2 (const void *A, const void *B) 10005 { 10006 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 10007 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 10008 10009 if (a->type < b->type) 10010 return -1; 10011 if (a->type > b->type) 10012 return 1; 10013 if (a->u.offset < b->u.offset) 10014 return -1; 10015 if (a->u.offset > b->u.offset) 10016 return 1; 10017 if (a->rela->r_offset < b->rela->r_offset) 10018 return -1; 10019 if (a->rela->r_offset > b->rela->r_offset) 10020 return 1; 10021 return 0; 10022 } 10023 10024 static size_t 10025 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) 10026 { 10027 asection *dynamic_relocs; 10028 asection *rela_dyn; 10029 asection *rel_dyn; 10030 bfd_size_type count, size; 10031 size_t i, ret, sort_elt, ext_size; 10032 bfd_byte *sort, *s_non_relative, *p; 10033 struct elf_link_sort_rela *sq; 10034 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10035 int i2e = bed->s->int_rels_per_ext_rel; 10036 unsigned int opb = bfd_octets_per_byte (abfd, NULL); 10037 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 10038 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 10039 struct bfd_link_order *lo; 10040 bfd_vma r_sym_mask; 10041 bool use_rela; 10042 10043 /* Find a dynamic reloc section. */ 10044 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); 10045 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); 10046 if (rela_dyn != NULL && rela_dyn->size > 0 10047 && rel_dyn != NULL && rel_dyn->size > 0) 10048 { 10049 bool use_rela_initialised = false; 10050 10051 /* This is just here to stop gcc from complaining. 10052 Its initialization checking code is not perfect. */ 10053 use_rela = true; 10054 10055 /* Both sections are present. Examine the sizes 10056 of the indirect sections to help us choose. */ 10057 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) 10058 if (lo->type == bfd_indirect_link_order) 10059 { 10060 asection *o = lo->u.indirect.section; 10061 10062 if ((o->size % bed->s->sizeof_rela) == 0) 10063 { 10064 if ((o->size % bed->s->sizeof_rel) == 0) 10065 /* Section size is divisible by both rel and rela sizes. 10066 It is of no help to us. */ 10067 ; 10068 else 10069 { 10070 /* Section size is only divisible by rela. */ 10071 if (use_rela_initialised && !use_rela) 10072 { 10073 _bfd_error_handler (_("%pB: unable to sort relocs - " 10074 "they are in more than one size"), 10075 abfd); 10076 bfd_set_error (bfd_error_invalid_operation); 10077 return 0; 10078 } 10079 else 10080 { 10081 use_rela = true; 10082 use_rela_initialised = true; 10083 } 10084 } 10085 } 10086 else if ((o->size % bed->s->sizeof_rel) == 0) 10087 { 10088 /* Section size is only divisible by rel. */ 10089 if (use_rela_initialised && use_rela) 10090 { 10091 _bfd_error_handler (_("%pB: unable to sort relocs - " 10092 "they are in more than one size"), 10093 abfd); 10094 bfd_set_error (bfd_error_invalid_operation); 10095 return 0; 10096 } 10097 else 10098 { 10099 use_rela = false; 10100 use_rela_initialised = true; 10101 } 10102 } 10103 else 10104 { 10105 /* The section size is not divisible by either - 10106 something is wrong. */ 10107 _bfd_error_handler (_("%pB: unable to sort relocs - " 10108 "they are of an unknown size"), abfd); 10109 bfd_set_error (bfd_error_invalid_operation); 10110 return 0; 10111 } 10112 } 10113 10114 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) 10115 if (lo->type == bfd_indirect_link_order) 10116 { 10117 asection *o = lo->u.indirect.section; 10118 10119 if ((o->size % bed->s->sizeof_rela) == 0) 10120 { 10121 if ((o->size % bed->s->sizeof_rel) == 0) 10122 /* Section size is divisible by both rel and rela sizes. 10123 It is of no help to us. */ 10124 ; 10125 else 10126 { 10127 /* Section size is only divisible by rela. */ 10128 if (use_rela_initialised && !use_rela) 10129 { 10130 _bfd_error_handler (_("%pB: unable to sort relocs - " 10131 "they are in more than one size"), 10132 abfd); 10133 bfd_set_error (bfd_error_invalid_operation); 10134 return 0; 10135 } 10136 else 10137 { 10138 use_rela = true; 10139 use_rela_initialised = true; 10140 } 10141 } 10142 } 10143 else if ((o->size % bed->s->sizeof_rel) == 0) 10144 { 10145 /* Section size is only divisible by rel. */ 10146 if (use_rela_initialised && use_rela) 10147 { 10148 _bfd_error_handler (_("%pB: unable to sort relocs - " 10149 "they are in more than one size"), 10150 abfd); 10151 bfd_set_error (bfd_error_invalid_operation); 10152 return 0; 10153 } 10154 else 10155 { 10156 use_rela = false; 10157 use_rela_initialised = true; 10158 } 10159 } 10160 else 10161 { 10162 /* The section size is not divisible by either - 10163 something is wrong. */ 10164 _bfd_error_handler (_("%pB: unable to sort relocs - " 10165 "they are of an unknown size"), abfd); 10166 bfd_set_error (bfd_error_invalid_operation); 10167 return 0; 10168 } 10169 } 10170 10171 if (! use_rela_initialised) 10172 /* Make a guess. */ 10173 use_rela = true; 10174 } 10175 else if (rela_dyn != NULL && rela_dyn->size > 0) 10176 use_rela = true; 10177 else if (rel_dyn != NULL && rel_dyn->size > 0) 10178 use_rela = false; 10179 else 10180 return 0; 10181 10182 if (use_rela) 10183 { 10184 dynamic_relocs = rela_dyn; 10185 ext_size = bed->s->sizeof_rela; 10186 swap_in = bed->s->swap_reloca_in; 10187 swap_out = bed->s->swap_reloca_out; 10188 } 10189 else 10190 { 10191 dynamic_relocs = rel_dyn; 10192 ext_size = bed->s->sizeof_rel; 10193 swap_in = bed->s->swap_reloc_in; 10194 swap_out = bed->s->swap_reloc_out; 10195 } 10196 10197 size = 0; 10198 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 10199 if (lo->type == bfd_indirect_link_order) 10200 size += lo->u.indirect.section->size; 10201 10202 if (size != dynamic_relocs->size) 10203 return 0; 10204 10205 sort_elt = (sizeof (struct elf_link_sort_rela) 10206 + (i2e - 1) * sizeof (Elf_Internal_Rela)); 10207 10208 count = dynamic_relocs->size / ext_size; 10209 if (count == 0) 10210 return 0; 10211 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); 10212 10213 if (sort == NULL) 10214 { 10215 (*info->callbacks->warning) 10216 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); 10217 return 0; 10218 } 10219 10220 if (bed->s->arch_size == 32) 10221 r_sym_mask = ~(bfd_vma) 0xff; 10222 else 10223 r_sym_mask = ~(bfd_vma) 0xffffffff; 10224 10225 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 10226 if (lo->type == bfd_indirect_link_order) 10227 { 10228 bfd_byte *erel, *erelend; 10229 asection *o = lo->u.indirect.section; 10230 10231 if (o->contents == NULL && o->size != 0) 10232 { 10233 /* This is a reloc section that is being handled as a normal 10234 section. See bfd_section_from_shdr. We can't combine 10235 relocs in this case. */ 10236 free (sort); 10237 return 0; 10238 } 10239 erel = o->contents; 10240 erelend = o->contents + o->size; 10241 p = sort + o->output_offset * opb / ext_size * sort_elt; 10242 10243 while (erel < erelend) 10244 { 10245 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 10246 10247 (*swap_in) (abfd, erel, s->rela); 10248 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); 10249 s->u.sym_mask = r_sym_mask; 10250 p += sort_elt; 10251 erel += ext_size; 10252 } 10253 } 10254 10255 qsort (sort, count, sort_elt, elf_link_sort_cmp1); 10256 10257 for (i = 0, p = sort; i < count; i++, p += sort_elt) 10258 { 10259 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 10260 if (s->type != reloc_class_relative) 10261 break; 10262 } 10263 ret = i; 10264 s_non_relative = p; 10265 10266 sq = (struct elf_link_sort_rela *) s_non_relative; 10267 for (; i < count; i++, p += sort_elt) 10268 { 10269 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; 10270 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) 10271 sq = sp; 10272 sp->u.offset = sq->rela->r_offset; 10273 } 10274 10275 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); 10276 10277 struct elf_link_hash_table *htab = elf_hash_table (info); 10278 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) 10279 { 10280 /* We have plt relocs in .rela.dyn. */ 10281 sq = (struct elf_link_sort_rela *) sort; 10282 for (i = 0; i < count; i++) 10283 if (sq[count - i - 1].type != reloc_class_plt) 10284 break; 10285 if (i != 0 && htab->srelplt->size == i * ext_size) 10286 { 10287 struct bfd_link_order **plo; 10288 /* Put srelplt link_order last. This is so the output_offset 10289 set in the next loop is correct for DT_JMPREL. */ 10290 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) 10291 if ((*plo)->type == bfd_indirect_link_order 10292 && (*plo)->u.indirect.section == htab->srelplt) 10293 { 10294 lo = *plo; 10295 *plo = lo->next; 10296 } 10297 else 10298 plo = &(*plo)->next; 10299 *plo = lo; 10300 lo->next = NULL; 10301 dynamic_relocs->map_tail.link_order = lo; 10302 } 10303 } 10304 10305 p = sort; 10306 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 10307 if (lo->type == bfd_indirect_link_order) 10308 { 10309 bfd_byte *erel, *erelend; 10310 asection *o = lo->u.indirect.section; 10311 10312 erel = o->contents; 10313 erelend = o->contents + o->size; 10314 o->output_offset = (p - sort) / sort_elt * ext_size / opb; 10315 while (erel < erelend) 10316 { 10317 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 10318 (*swap_out) (abfd, s->rela, erel); 10319 p += sort_elt; 10320 erel += ext_size; 10321 } 10322 } 10323 10324 free (sort); 10325 *psec = dynamic_relocs; 10326 return ret; 10327 } 10328 10329 /* Add a symbol to the output symbol string table. */ 10330 10331 static int 10332 elf_link_output_symstrtab (void *finf, 10333 const char *name, 10334 Elf_Internal_Sym *elfsym, 10335 asection *input_sec, 10336 struct elf_link_hash_entry *h) 10337 { 10338 struct elf_final_link_info *flinfo = finf; 10339 int (*output_symbol_hook) 10340 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, 10341 struct elf_link_hash_entry *); 10342 struct elf_link_hash_table *hash_table; 10343 const struct elf_backend_data *bed; 10344 bfd_size_type strtabsize; 10345 10346 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 10347 10348 bed = get_elf_backend_data (flinfo->output_bfd); 10349 output_symbol_hook = bed->elf_backend_link_output_symbol_hook; 10350 if (output_symbol_hook != NULL) 10351 { 10352 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); 10353 if (ret != 1) 10354 return ret; 10355 } 10356 10357 if (ELF_ST_TYPE (elfsym->st_info) == STT_GNU_IFUNC) 10358 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_ifunc; 10359 if (ELF_ST_BIND (elfsym->st_info) == STB_GNU_UNIQUE) 10360 elf_tdata (flinfo->output_bfd)->has_gnu_osabi |= elf_gnu_osabi_unique; 10361 10362 if (name == NULL || *name == '\0') 10363 elfsym->st_name = (unsigned long) -1; 10364 else 10365 { 10366 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 10367 to get the final offset for st_name. */ 10368 char *versioned_name = (char *) name; 10369 if (h != NULL) 10370 { 10371 if (h->versioned == versioned && h->def_dynamic) 10372 { 10373 /* Keep only one '@' for versioned symbols defined in 10374 shared objects. */ 10375 char *version = strrchr (name, ELF_VER_CHR); 10376 char *base_end = strchr (name, ELF_VER_CHR); 10377 if (version != base_end) 10378 { 10379 size_t base_len; 10380 size_t len = strlen (name); 10381 versioned_name = bfd_alloc (flinfo->output_bfd, len); 10382 if (versioned_name == NULL) 10383 return 0; 10384 base_len = base_end - name; 10385 memcpy (versioned_name, name, base_len); 10386 memcpy (versioned_name + base_len, version, 10387 len - base_len); 10388 } 10389 } 10390 } 10391 else if (flinfo->info->unique_symbol 10392 && ELF_ST_BIND (elfsym->st_info) == STB_LOCAL) 10393 { 10394 struct local_hash_entry *lh; 10395 size_t count_len; 10396 size_t base_len; 10397 char buf[30]; 10398 switch (ELF_ST_TYPE (elfsym->st_info)) 10399 { 10400 case STT_FILE: 10401 case STT_SECTION: 10402 break; 10403 default: 10404 lh = (struct local_hash_entry *) bfd_hash_lookup 10405 (&flinfo->local_hash_table, name, true, false); 10406 if (lh == NULL) 10407 return 0; 10408 /* Always append ".COUNT" to local symbols to avoid 10409 potential conflicts with local symbol "XXX.COUNT". */ 10410 sprintf (buf, "%lx", lh->count); 10411 base_len = lh->size; 10412 if (!base_len) 10413 { 10414 base_len = strlen (name); 10415 lh->size = base_len; 10416 } 10417 count_len = strlen (buf); 10418 versioned_name = bfd_alloc (flinfo->output_bfd, 10419 base_len + count_len + 2); 10420 if (versioned_name == NULL) 10421 return 0; 10422 memcpy (versioned_name, name, base_len); 10423 versioned_name[base_len] = '.'; 10424 memcpy (versioned_name + base_len + 1, buf, 10425 count_len + 1); 10426 lh->count++; 10427 break; 10428 } 10429 } 10430 elfsym->st_name 10431 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, 10432 versioned_name, false); 10433 if (elfsym->st_name == (unsigned long) -1) 10434 return 0; 10435 } 10436 10437 hash_table = elf_hash_table (flinfo->info); 10438 strtabsize = hash_table->strtabsize; 10439 if (strtabsize <= flinfo->output_bfd->symcount) 10440 { 10441 strtabsize += strtabsize; 10442 hash_table->strtabsize = strtabsize; 10443 strtabsize *= sizeof (*hash_table->strtab); 10444 hash_table->strtab 10445 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, 10446 strtabsize); 10447 if (hash_table->strtab == NULL) 10448 return 0; 10449 } 10450 hash_table->strtab[flinfo->output_bfd->symcount].sym = *elfsym; 10451 hash_table->strtab[flinfo->output_bfd->symcount].dest_index 10452 = flinfo->output_bfd->symcount; 10453 flinfo->output_bfd->symcount += 1; 10454 10455 return 1; 10456 } 10457 10458 /* Swap symbols out to the symbol table and flush the output symbols to 10459 the file. */ 10460 10461 static bool 10462 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) 10463 { 10464 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); 10465 size_t amt; 10466 size_t i; 10467 const struct elf_backend_data *bed; 10468 bfd_byte *symbuf; 10469 Elf_Internal_Shdr *hdr; 10470 file_ptr pos; 10471 bool ret; 10472 10473 if (flinfo->output_bfd->symcount == 0) 10474 return true; 10475 10476 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 10477 10478 bed = get_elf_backend_data (flinfo->output_bfd); 10479 10480 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount; 10481 symbuf = (bfd_byte *) bfd_malloc (amt); 10482 if (symbuf == NULL) 10483 return false; 10484 10485 if (flinfo->symshndxbuf) 10486 { 10487 amt = sizeof (Elf_External_Sym_Shndx); 10488 amt *= bfd_get_symcount (flinfo->output_bfd); 10489 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); 10490 if (flinfo->symshndxbuf == NULL) 10491 { 10492 free (symbuf); 10493 return false; 10494 } 10495 } 10496 10497 /* Now swap out the symbols. */ 10498 for (i = 0; i < flinfo->output_bfd->symcount; i++) 10499 { 10500 struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; 10501 if (elfsym->sym.st_name == (unsigned long) -1) 10502 elfsym->sym.st_name = 0; 10503 else 10504 elfsym->sym.st_name 10505 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, 10506 elfsym->sym.st_name); 10507 10508 /* Inform the linker of the addition of this symbol. */ 10509 10510 if (flinfo->info->callbacks->ctf_new_symbol) 10511 flinfo->info->callbacks->ctf_new_symbol (elfsym->dest_index, 10512 &elfsym->sym); 10513 10514 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, 10515 ((bfd_byte *) symbuf 10516 + (elfsym->dest_index 10517 * bed->s->sizeof_sym)), 10518 NPTR_ADD (flinfo->symshndxbuf, 10519 elfsym->dest_index)); 10520 } 10521 10522 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; 10523 pos = hdr->sh_offset + hdr->sh_size; 10524 amt = bed->s->sizeof_sym * flinfo->output_bfd->symcount; 10525 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 10526 && bfd_write (symbuf, amt, flinfo->output_bfd) == amt) 10527 { 10528 hdr->sh_size += amt; 10529 ret = true; 10530 } 10531 else 10532 ret = false; 10533 10534 free (symbuf); 10535 return ret; 10536 } 10537 10538 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ 10539 10540 static bool 10541 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) 10542 { 10543 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) 10544 && sym->st_shndx < SHN_LORESERVE) 10545 { 10546 /* The gABI doesn't support dynamic symbols in output sections 10547 beyond 64k. */ 10548 _bfd_error_handler 10549 /* xgettext:c-format */ 10550 (_("%pB: too many sections: %d (>= %d)"), 10551 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); 10552 bfd_set_error (bfd_error_nonrepresentable_section); 10553 return false; 10554 } 10555 return true; 10556 } 10557 10558 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in 10559 allowing an unsatisfied unversioned symbol in the DSO to match a 10560 versioned symbol that would normally require an explicit version. 10561 We also handle the case that a DSO references a hidden symbol 10562 which may be satisfied by a versioned symbol in another DSO. */ 10563 10564 static bool 10565 elf_link_check_versioned_symbol (struct bfd_link_info *info, 10566 const struct elf_backend_data *bed, 10567 struct elf_link_hash_entry *h) 10568 { 10569 bfd *abfd; 10570 struct elf_link_loaded_list *loaded; 10571 10572 if (!is_elf_hash_table (info->hash)) 10573 return false; 10574 10575 /* Check indirect symbol. */ 10576 while (h->root.type == bfd_link_hash_indirect) 10577 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10578 10579 switch (h->root.type) 10580 { 10581 default: 10582 abfd = NULL; 10583 break; 10584 10585 case bfd_link_hash_undefined: 10586 case bfd_link_hash_undefweak: 10587 abfd = h->root.u.undef.abfd; 10588 if (abfd == NULL 10589 || (abfd->flags & DYNAMIC) == 0 10590 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) 10591 return false; 10592 break; 10593 10594 case bfd_link_hash_defined: 10595 case bfd_link_hash_defweak: 10596 abfd = h->root.u.def.section->owner; 10597 break; 10598 10599 case bfd_link_hash_common: 10600 abfd = h->root.u.c.p->section->owner; 10601 break; 10602 } 10603 BFD_ASSERT (abfd != NULL); 10604 10605 for (loaded = elf_hash_table (info)->dyn_loaded; 10606 loaded != NULL; 10607 loaded = loaded->next) 10608 { 10609 bfd *input; 10610 Elf_Internal_Shdr *hdr; 10611 size_t symcount; 10612 size_t extsymcount; 10613 size_t extsymoff; 10614 Elf_Internal_Shdr *versymhdr; 10615 Elf_Internal_Sym *isym; 10616 Elf_Internal_Sym *isymend; 10617 Elf_Internal_Sym *isymbuf; 10618 Elf_External_Versym *ever; 10619 Elf_External_Versym *extversym; 10620 10621 input = loaded->abfd; 10622 10623 /* We check each DSO for a possible hidden versioned definition. */ 10624 if (input == abfd 10625 || elf_dynversym (input) == 0) 10626 continue; 10627 10628 hdr = &elf_tdata (input)->dynsymtab_hdr; 10629 10630 symcount = hdr->sh_size / bed->s->sizeof_sym; 10631 if (elf_bad_symtab (input)) 10632 { 10633 extsymcount = symcount; 10634 extsymoff = 0; 10635 } 10636 else 10637 { 10638 extsymcount = symcount - hdr->sh_info; 10639 extsymoff = hdr->sh_info; 10640 } 10641 10642 if (extsymcount == 0) 10643 continue; 10644 10645 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, 10646 NULL, NULL, NULL); 10647 if (isymbuf == NULL) 10648 return false; 10649 10650 /* Read in any version definitions. */ 10651 versymhdr = &elf_tdata (input)->dynversym_hdr; 10652 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 10653 || (extversym = (Elf_External_Versym *) 10654 _bfd_malloc_and_read (input, versymhdr->sh_size, 10655 versymhdr->sh_size)) == NULL) 10656 { 10657 free (isymbuf); 10658 return false; 10659 } 10660 10661 ever = extversym + extsymoff; 10662 isymend = isymbuf + extsymcount; 10663 for (isym = isymbuf; isym < isymend; isym++, ever++) 10664 { 10665 const char *name; 10666 Elf_Internal_Versym iver; 10667 unsigned short version_index; 10668 10669 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL 10670 || isym->st_shndx == SHN_UNDEF) 10671 continue; 10672 10673 name = bfd_elf_string_from_elf_section (input, 10674 hdr->sh_link, 10675 isym->st_name); 10676 if (strcmp (name, h->root.root.string) != 0) 10677 continue; 10678 10679 _bfd_elf_swap_versym_in (input, ever, &iver); 10680 10681 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 10682 && !(h->def_regular 10683 && h->forced_local)) 10684 { 10685 /* If we have a non-hidden versioned sym, then it should 10686 have provided a definition for the undefined sym unless 10687 it is defined in a non-shared object and forced local. 10688 */ 10689 abort (); 10690 } 10691 10692 version_index = iver.vs_vers & VERSYM_VERSION; 10693 if (version_index == 1 || version_index == 2) 10694 { 10695 /* This is the base or first version. We can use it. */ 10696 free (extversym); 10697 free (isymbuf); 10698 return true; 10699 } 10700 } 10701 10702 free (extversym); 10703 free (isymbuf); 10704 } 10705 10706 return false; 10707 } 10708 10709 /* Convert ELF common symbol TYPE. */ 10710 10711 static int 10712 elf_link_convert_common_type (struct bfd_link_info *info, int type) 10713 { 10714 /* Commom symbol can only appear in relocatable link. */ 10715 if (!bfd_link_relocatable (info)) 10716 abort (); 10717 switch (info->elf_stt_common) 10718 { 10719 case unchanged: 10720 break; 10721 case elf_stt_common: 10722 type = STT_COMMON; 10723 break; 10724 case no_elf_stt_common: 10725 type = STT_OBJECT; 10726 break; 10727 } 10728 return type; 10729 } 10730 10731 /* Add an external symbol to the symbol table. This is called from 10732 the hash table traversal routine. When generating a shared object, 10733 we go through the symbol table twice. The first time we output 10734 anything that might have been forced to local scope in a version 10735 script. The second time we output the symbols that are still 10736 global symbols. */ 10737 10738 static bool 10739 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) 10740 { 10741 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; 10742 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; 10743 struct elf_final_link_info *flinfo = eoinfo->flinfo; 10744 bool strip; 10745 Elf_Internal_Sym sym; 10746 asection *input_sec; 10747 const struct elf_backend_data *bed; 10748 long indx; 10749 int ret; 10750 unsigned int type; 10751 10752 if (h->root.type == bfd_link_hash_warning) 10753 { 10754 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10755 if (h->root.type == bfd_link_hash_new) 10756 return true; 10757 } 10758 10759 /* Decide whether to output this symbol in this pass. */ 10760 if (eoinfo->localsyms) 10761 { 10762 if (!h->forced_local) 10763 return true; 10764 } 10765 else 10766 { 10767 if (h->forced_local) 10768 return true; 10769 } 10770 10771 bed = get_elf_backend_data (flinfo->output_bfd); 10772 10773 if (h->root.type == bfd_link_hash_undefined) 10774 { 10775 /* If we have an undefined symbol reference here then it must have 10776 come from a shared library that is being linked in. (Undefined 10777 references in regular files have already been handled unless 10778 they are in unreferenced sections which are removed by garbage 10779 collection). */ 10780 bool ignore_undef = false; 10781 10782 /* Some symbols may be special in that the fact that they're 10783 undefined can be safely ignored - let backend determine that. */ 10784 if (bed->elf_backend_ignore_undef_symbol) 10785 ignore_undef = bed->elf_backend_ignore_undef_symbol (h); 10786 10787 /* If we are reporting errors for this situation then do so now. */ 10788 if (!ignore_undef 10789 && h->ref_dynamic_nonweak 10790 && (!h->ref_regular || flinfo->info->gc_sections) 10791 && !elf_link_check_versioned_symbol (flinfo->info, bed, h) 10792 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) 10793 { 10794 flinfo->info->callbacks->undefined_symbol 10795 (flinfo->info, h->root.root.string, 10796 h->ref_regular ? NULL : h->root.u.undef.abfd, NULL, 0, 10797 flinfo->info->unresolved_syms_in_shared_libs == RM_DIAGNOSE 10798 && !flinfo->info->warn_unresolved_syms); 10799 } 10800 10801 /* Strip a global symbol defined in a discarded section. */ 10802 if (h->indx == -3) 10803 return true; 10804 } 10805 10806 /* We should also warn if a forced local symbol is referenced from 10807 shared libraries. */ 10808 if (bfd_link_executable (flinfo->info) 10809 && h->forced_local 10810 && h->ref_dynamic 10811 && h->def_regular 10812 && !h->dynamic_def 10813 && h->ref_dynamic_nonweak 10814 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) 10815 { 10816 bfd *def_bfd; 10817 const char *msg; 10818 struct elf_link_hash_entry *hi = h; 10819 10820 /* Check indirect symbol. */ 10821 while (hi->root.type == bfd_link_hash_indirect) 10822 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 10823 10824 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 10825 /* xgettext:c-format */ 10826 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); 10827 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) 10828 /* xgettext:c-format */ 10829 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); 10830 else 10831 /* xgettext:c-format */ 10832 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); 10833 def_bfd = flinfo->output_bfd; 10834 if (hi->root.u.def.section != bfd_abs_section_ptr) 10835 def_bfd = hi->root.u.def.section->owner; 10836 _bfd_error_handler (msg, flinfo->output_bfd, 10837 h->root.root.string, def_bfd); 10838 bfd_set_error (bfd_error_bad_value); 10839 eoinfo->failed = true; 10840 return false; 10841 } 10842 10843 /* We don't want to output symbols that have never been mentioned by 10844 a regular file, or that we have been told to strip. However, if 10845 h->indx is set to -2, the symbol is used by a reloc and we must 10846 output it. */ 10847 strip = false; 10848 if (h->indx == -2) 10849 ; 10850 else if ((h->def_dynamic 10851 || h->ref_dynamic 10852 || h->root.type == bfd_link_hash_new) 10853 && !h->def_regular 10854 && !h->ref_regular) 10855 strip = true; 10856 else if (flinfo->info->strip == strip_all) 10857 strip = true; 10858 else if (flinfo->info->strip == strip_some 10859 && bfd_hash_lookup (flinfo->info->keep_hash, 10860 h->root.root.string, false, false) == NULL) 10861 strip = true; 10862 else if ((h->root.type == bfd_link_hash_defined 10863 || h->root.type == bfd_link_hash_defweak) 10864 && ((flinfo->info->strip_discarded 10865 && discarded_section (h->root.u.def.section)) 10866 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 10867 && h->root.u.def.section->owner != NULL 10868 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) 10869 strip = true; 10870 else if ((h->root.type == bfd_link_hash_undefined 10871 || h->root.type == bfd_link_hash_undefweak) 10872 && h->root.u.undef.abfd != NULL 10873 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) 10874 strip = true; 10875 10876 /* Remember if this symbol should be stripped. */ 10877 bool should_strip = strip; 10878 10879 /* Strip undefined weak symbols link if they don't have relocation. */ 10880 if (!strip) 10881 strip = !h->has_reloc && h->root.type == bfd_link_hash_undefweak; 10882 10883 type = h->type; 10884 10885 /* If we're stripping it, and it's not a dynamic symbol, there's 10886 nothing else to do. However, if it is a forced local symbol or 10887 an ifunc symbol we need to give the backend finish_dynamic_symbol 10888 function a chance to make it dynamic. */ 10889 if (strip 10890 && h->dynindx == -1 10891 && type != STT_GNU_IFUNC 10892 && !h->forced_local) 10893 return true; 10894 10895 sym.st_value = 0; 10896 sym.st_size = h->size; 10897 sym.st_other = h->other; 10898 switch (h->root.type) 10899 { 10900 default: 10901 case bfd_link_hash_new: 10902 case bfd_link_hash_warning: 10903 abort (); 10904 return false; 10905 10906 case bfd_link_hash_undefined: 10907 case bfd_link_hash_undefweak: 10908 input_sec = bfd_und_section_ptr; 10909 sym.st_shndx = SHN_UNDEF; 10910 break; 10911 10912 case bfd_link_hash_defined: 10913 case bfd_link_hash_defweak: 10914 { 10915 input_sec = h->root.u.def.section; 10916 if (input_sec->output_section != NULL) 10917 { 10918 sym.st_shndx = 10919 _bfd_elf_section_from_bfd_section (flinfo->output_bfd, 10920 input_sec->output_section); 10921 if (sym.st_shndx == SHN_BAD) 10922 { 10923 _bfd_error_handler 10924 /* xgettext:c-format */ 10925 (_("%pB: could not find output section %pA for input section %pA"), 10926 flinfo->output_bfd, input_sec->output_section, input_sec); 10927 bfd_set_error (bfd_error_nonrepresentable_section); 10928 eoinfo->failed = true; 10929 return false; 10930 } 10931 10932 /* ELF symbols in relocatable files are section relative, 10933 but in nonrelocatable files they are virtual 10934 addresses. */ 10935 sym.st_value = h->root.u.def.value + input_sec->output_offset; 10936 if (!bfd_link_relocatable (flinfo->info)) 10937 { 10938 sym.st_value += input_sec->output_section->vma; 10939 if (h->type == STT_TLS) 10940 { 10941 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; 10942 if (tls_sec != NULL) 10943 sym.st_value -= tls_sec->vma; 10944 } 10945 } 10946 } 10947 else 10948 { 10949 BFD_ASSERT (input_sec->owner == NULL 10950 || (input_sec->owner->flags & DYNAMIC) != 0); 10951 sym.st_shndx = SHN_UNDEF; 10952 input_sec = bfd_und_section_ptr; 10953 } 10954 } 10955 break; 10956 10957 case bfd_link_hash_common: 10958 input_sec = h->root.u.c.p->section; 10959 sym.st_shndx = bed->common_section_index (input_sec); 10960 sym.st_value = 1 << h->root.u.c.p->alignment_power; 10961 break; 10962 10963 case bfd_link_hash_indirect: 10964 /* These symbols are created by symbol versioning. They point 10965 to the decorated version of the name. For example, if the 10966 symbol foo@@GNU_1.2 is the default, which should be used when 10967 foo is used with no version, then we add an indirect symbol 10968 foo which points to foo@@GNU_1.2. We ignore these symbols, 10969 since the indirected symbol is already in the hash table. */ 10970 return true; 10971 } 10972 10973 if (type == STT_COMMON || type == STT_OBJECT) 10974 switch (h->root.type) 10975 { 10976 case bfd_link_hash_common: 10977 type = elf_link_convert_common_type (flinfo->info, type); 10978 break; 10979 case bfd_link_hash_defined: 10980 case bfd_link_hash_defweak: 10981 if (bed->common_definition (&sym)) 10982 type = elf_link_convert_common_type (flinfo->info, type); 10983 else 10984 type = STT_OBJECT; 10985 break; 10986 case bfd_link_hash_undefined: 10987 case bfd_link_hash_undefweak: 10988 break; 10989 default: 10990 abort (); 10991 } 10992 10993 if (h->forced_local) 10994 { 10995 sym.st_info = ELF_ST_INFO (STB_LOCAL, type); 10996 /* Turn off visibility on local symbol. */ 10997 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 10998 } 10999 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ 11000 else if (h->unique_global && h->def_regular) 11001 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); 11002 else if (h->root.type == bfd_link_hash_undefweak 11003 || h->root.type == bfd_link_hash_defweak) 11004 sym.st_info = ELF_ST_INFO (STB_WEAK, type); 11005 else 11006 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 11007 sym.st_target_internal = h->target_internal; 11008 11009 /* Give the processor backend a chance to tweak the symbol value, 11010 and also to finish up anything that needs to be done for this 11011 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for 11012 forced local syms when non-shared is due to a historical quirk. 11013 STT_GNU_IFUNC symbol must go through PLT. */ 11014 if ((h->type == STT_GNU_IFUNC 11015 && h->def_regular 11016 && !bfd_link_relocatable (flinfo->info)) 11017 || ((h->dynindx != -1 11018 || h->forced_local) 11019 && ((bfd_link_pic (flinfo->info) 11020 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 11021 || h->root.type != bfd_link_hash_undefweak)) 11022 || !h->forced_local) 11023 && elf_hash_table (flinfo->info)->dynamic_sections_created)) 11024 { 11025 if (! ((*bed->elf_backend_finish_dynamic_symbol) 11026 (flinfo->output_bfd, flinfo->info, h, &sym))) 11027 { 11028 eoinfo->failed = true; 11029 return false; 11030 } 11031 /* If a symbol is in the dynamic symbol table and isn't a 11032 should-strip symbol, also keep it in the symbol table. */ 11033 if (!should_strip) 11034 strip = false; 11035 } 11036 11037 /* If we are marking the symbol as undefined, and there are no 11038 non-weak references to this symbol from a regular object, then 11039 mark the symbol as weak undefined; if there are non-weak 11040 references, mark the symbol as strong. We can't do this earlier, 11041 because it might not be marked as undefined until the 11042 finish_dynamic_symbol routine gets through with it. */ 11043 if (sym.st_shndx == SHN_UNDEF 11044 && h->ref_regular 11045 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL 11046 || ELF_ST_BIND (sym.st_info) == STB_WEAK)) 11047 { 11048 int bindtype; 11049 type = ELF_ST_TYPE (sym.st_info); 11050 11051 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ 11052 if (type == STT_GNU_IFUNC) 11053 type = STT_FUNC; 11054 11055 if (h->ref_regular_nonweak) 11056 bindtype = STB_GLOBAL; 11057 else 11058 bindtype = STB_WEAK; 11059 sym.st_info = ELF_ST_INFO (bindtype, type); 11060 } 11061 11062 /* If this is a symbol defined in a dynamic library, don't use the 11063 symbol size from the dynamic library. Relinking an executable 11064 against a new library may introduce gratuitous changes in the 11065 executable's symbols if we keep the size. */ 11066 if (sym.st_shndx == SHN_UNDEF 11067 && !h->def_regular 11068 && h->def_dynamic) 11069 sym.st_size = 0; 11070 11071 /* If a non-weak symbol with non-default visibility is not defined 11072 locally, it is a fatal error. */ 11073 if (!bfd_link_relocatable (flinfo->info) 11074 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT 11075 && ELF_ST_BIND (sym.st_info) != STB_WEAK 11076 && h->root.type == bfd_link_hash_undefined 11077 && !h->def_regular) 11078 { 11079 const char *msg; 11080 11081 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) 11082 /* xgettext:c-format */ 11083 msg = _("%pB: protected symbol `%s' isn't defined"); 11084 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) 11085 /* xgettext:c-format */ 11086 msg = _("%pB: internal symbol `%s' isn't defined"); 11087 else 11088 /* xgettext:c-format */ 11089 msg = _("%pB: hidden symbol `%s' isn't defined"); 11090 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); 11091 bfd_set_error (bfd_error_bad_value); 11092 eoinfo->failed = true; 11093 return false; 11094 } 11095 11096 /* If this symbol should be put in the .dynsym section, then put it 11097 there now. We already know the symbol index. We also fill in 11098 the entry in the .hash section. */ 11099 if (h->dynindx != -1 11100 && elf_hash_table (flinfo->info)->dynamic_sections_created 11101 && elf_hash_table (flinfo->info)->dynsym != NULL 11102 && !discarded_section (elf_hash_table (flinfo->info)->dynsym)) 11103 { 11104 bfd_byte *esym; 11105 11106 /* Since there is no version information in the dynamic string, 11107 if there is no version info in symbol version section, we will 11108 have a run-time problem if not linking executable, referenced 11109 by shared library, or not bound locally. */ 11110 if (h->verinfo.verdef == NULL 11111 && (!bfd_link_executable (flinfo->info) 11112 || h->ref_dynamic 11113 || !h->def_regular)) 11114 { 11115 char *p = strrchr (h->root.root.string, ELF_VER_CHR); 11116 11117 if (p && p [1] != '\0') 11118 { 11119 _bfd_error_handler 11120 /* xgettext:c-format */ 11121 (_("%pB: no symbol version section for versioned symbol `%s'"), 11122 flinfo->output_bfd, h->root.root.string); 11123 eoinfo->failed = true; 11124 return false; 11125 } 11126 } 11127 11128 sym.st_name = h->dynstr_index; 11129 esym = (elf_hash_table (flinfo->info)->dynsym->contents 11130 + h->dynindx * bed->s->sizeof_sym); 11131 if (!check_dynsym (flinfo->output_bfd, &sym)) 11132 { 11133 eoinfo->failed = true; 11134 return false; 11135 } 11136 11137 /* Inform the linker of the addition of this symbol. */ 11138 11139 if (flinfo->info->callbacks->ctf_new_dynsym) 11140 flinfo->info->callbacks->ctf_new_dynsym (h->dynindx, &sym); 11141 11142 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); 11143 11144 if (flinfo->hash_sec != NULL) 11145 { 11146 size_t hash_entry_size; 11147 bfd_byte *bucketpos; 11148 bfd_vma chain; 11149 size_t bucketcount; 11150 size_t bucket; 11151 11152 bucketcount = elf_hash_table (flinfo->info)->bucketcount; 11153 bucket = h->u.elf_hash_value % bucketcount; 11154 11155 hash_entry_size 11156 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; 11157 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents 11158 + (bucket + 2) * hash_entry_size); 11159 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); 11160 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, 11161 bucketpos); 11162 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, 11163 ((bfd_byte *) flinfo->hash_sec->contents 11164 + (bucketcount + 2 + h->dynindx) * hash_entry_size)); 11165 } 11166 11167 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) 11168 { 11169 Elf_Internal_Versym iversym; 11170 Elf_External_Versym *eversym; 11171 11172 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 11173 { 11174 if (h->verinfo.verdef == NULL 11175 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 11176 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 11177 iversym.vs_vers = 1; 11178 else 11179 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; 11180 } 11181 else 11182 { 11183 if (h->verinfo.vertree == NULL) 11184 iversym.vs_vers = 1; 11185 else 11186 iversym.vs_vers = h->verinfo.vertree->vernum + 1; 11187 if (flinfo->info->create_default_symver) 11188 iversym.vs_vers++; 11189 } 11190 11191 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is 11192 defined locally. */ 11193 if (h->versioned == versioned_hidden && h->def_regular) 11194 iversym.vs_vers |= VERSYM_HIDDEN; 11195 11196 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; 11197 eversym += h->dynindx; 11198 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); 11199 } 11200 } 11201 11202 /* If the symbol is undefined, and we didn't output it to .dynsym, 11203 strip it from .symtab too. Obviously we can't do this for 11204 relocatable output or when needed for --emit-relocs. */ 11205 else if (input_sec == bfd_und_section_ptr 11206 && h->indx != -2 11207 /* PR 22319 Do not strip global undefined symbols marked as being needed. */ 11208 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) 11209 && !bfd_link_relocatable (flinfo->info)) 11210 return true; 11211 11212 /* Also strip others that we couldn't earlier due to dynamic symbol 11213 processing. */ 11214 if (strip) 11215 return true; 11216 if ((input_sec->flags & SEC_EXCLUDE) != 0) 11217 return true; 11218 11219 /* Output a FILE symbol so that following locals are not associated 11220 with the wrong input file. We need one for forced local symbols 11221 if we've seen more than one FILE symbol or when we have exactly 11222 one FILE symbol but global symbols are present in a file other 11223 than the one with the FILE symbol. We also need one if linker 11224 defined symbols are present. In practice these conditions are 11225 always met, so just emit the FILE symbol unconditionally. */ 11226 if (eoinfo->localsyms 11227 && !eoinfo->file_sym_done 11228 && eoinfo->flinfo->filesym_count != 0) 11229 { 11230 Elf_Internal_Sym fsym; 11231 11232 memset (&fsym, 0, sizeof (fsym)); 11233 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 11234 fsym.st_shndx = SHN_ABS; 11235 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, 11236 bfd_und_section_ptr, NULL)) 11237 return false; 11238 11239 eoinfo->file_sym_done = true; 11240 } 11241 11242 indx = bfd_get_symcount (flinfo->output_bfd); 11243 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, 11244 input_sec, h); 11245 if (ret == 0) 11246 { 11247 eoinfo->failed = true; 11248 return false; 11249 } 11250 else if (ret == 1) 11251 h->indx = indx; 11252 else if (h->indx == -2) 11253 abort(); 11254 11255 return true; 11256 } 11257 11258 /* Return TRUE if special handling is done for relocs in SEC against 11259 symbols defined in discarded sections. */ 11260 11261 static bool 11262 elf_section_ignore_discarded_relocs (asection *sec) 11263 { 11264 const struct elf_backend_data *bed; 11265 11266 switch (sec->sec_info_type) 11267 { 11268 case SEC_INFO_TYPE_STABS: 11269 case SEC_INFO_TYPE_EH_FRAME: 11270 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 11271 case SEC_INFO_TYPE_SFRAME: 11272 return true; 11273 default: 11274 break; 11275 } 11276 11277 bed = get_elf_backend_data (sec->owner); 11278 if (bed->elf_backend_ignore_discarded_relocs != NULL 11279 && (*bed->elf_backend_ignore_discarded_relocs) (sec)) 11280 return true; 11281 11282 return false; 11283 } 11284 11285 /* Return a mask saying how ld should treat relocations in SEC against 11286 symbols defined in discarded sections. If this function returns 11287 COMPLAIN set, ld will issue a warning message. If this function 11288 returns PRETEND set, and the discarded section was link-once and the 11289 same size as the kept link-once section, ld will pretend that the 11290 symbol was actually defined in the kept section. Otherwise ld will 11291 zero the reloc (at least that is the intent, but some cooperation by 11292 the target dependent code is needed, particularly for REL targets). */ 11293 11294 unsigned int 11295 _bfd_elf_default_action_discarded (asection *sec) 11296 { 11297 const struct elf_backend_data *bed; 11298 bed = get_elf_backend_data (sec->owner); 11299 11300 if (sec->flags & SEC_DEBUGGING) 11301 return PRETEND; 11302 11303 if (strcmp (".eh_frame", sec->name) == 0) 11304 return 0; 11305 11306 if (bed->elf_backend_can_make_multiple_eh_frame 11307 && strncmp (sec->name, ".eh_frame.", 10) == 0) 11308 return 0; 11309 11310 if (elf_section_type (sec) == SHT_GNU_SFRAME) 11311 return 0; 11312 11313 if (strcmp (".gcc_except_table", sec->name) == 0) 11314 return 0; 11315 11316 return COMPLAIN | PRETEND; 11317 } 11318 11319 /* Find a match between a section and a member of a section group. */ 11320 11321 static asection * 11322 match_group_member (asection *sec, asection *group, 11323 struct bfd_link_info *info) 11324 { 11325 asection *first = elf_next_in_group (group); 11326 asection *s = first; 11327 11328 while (s != NULL) 11329 { 11330 if (bfd_elf_match_symbols_in_sections (s, sec, info)) 11331 return s; 11332 11333 s = elf_next_in_group (s); 11334 if (s == first) 11335 break; 11336 } 11337 11338 return NULL; 11339 } 11340 11341 /* Check if the kept section of a discarded section SEC can be used 11342 to replace it. Return the replacement if it is OK. Otherwise return 11343 NULL. */ 11344 11345 asection * 11346 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) 11347 { 11348 asection *kept; 11349 11350 kept = sec->kept_section; 11351 if (kept != NULL) 11352 { 11353 if ((kept->flags & SEC_GROUP) != 0) 11354 kept = match_group_member (sec, kept, info); 11355 if (kept != NULL) 11356 { 11357 if ((sec->rawsize != 0 ? sec->rawsize : sec->size) 11358 != (kept->rawsize != 0 ? kept->rawsize : kept->size)) 11359 kept = NULL; 11360 else 11361 { 11362 /* Get the real kept section. */ 11363 asection *next; 11364 for (next = kept->kept_section; 11365 next != NULL; 11366 next = next->kept_section) 11367 kept = next; 11368 } 11369 } 11370 sec->kept_section = kept; 11371 } 11372 return kept; 11373 } 11374 11375 /* Link an input file into the linker output file. This function 11376 handles all the sections and relocations of the input file at once. 11377 This is so that we only have to read the local symbols once, and 11378 don't have to keep them in memory. */ 11379 11380 static bool 11381 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) 11382 { 11383 int (*relocate_section) 11384 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 11385 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); 11386 bfd *output_bfd; 11387 Elf_Internal_Shdr *symtab_hdr; 11388 size_t locsymcount; 11389 size_t extsymoff; 11390 Elf_Internal_Sym *isymbuf; 11391 Elf_Internal_Sym *isym; 11392 Elf_Internal_Sym *isymend; 11393 long *pindex; 11394 asection **ppsection; 11395 asection *o; 11396 const struct elf_backend_data *bed; 11397 struct elf_link_hash_entry **sym_hashes; 11398 bfd_size_type address_size; 11399 bfd_vma r_type_mask; 11400 int r_sym_shift; 11401 bool have_file_sym = false; 11402 11403 output_bfd = flinfo->output_bfd; 11404 bed = get_elf_backend_data (output_bfd); 11405 relocate_section = bed->elf_backend_relocate_section; 11406 11407 /* If this is a dynamic object, we don't want to do anything here: 11408 we don't want the local symbols, and we don't want the section 11409 contents. */ 11410 if ((input_bfd->flags & DYNAMIC) != 0) 11411 return true; 11412 11413 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 11414 if (elf_bad_symtab (input_bfd)) 11415 { 11416 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 11417 extsymoff = 0; 11418 } 11419 else 11420 { 11421 locsymcount = symtab_hdr->sh_info; 11422 extsymoff = symtab_hdr->sh_info; 11423 } 11424 11425 /* Enable GNU OSABI features in the output BFD that are used in the input 11426 BFD. */ 11427 if (bed->elf_osabi == ELFOSABI_NONE 11428 || bed->elf_osabi == ELFOSABI_GNU 11429 || bed->elf_osabi == ELFOSABI_FREEBSD) 11430 elf_tdata (output_bfd)->has_gnu_osabi 11431 |= (elf_tdata (input_bfd)->has_gnu_osabi 11432 & (bfd_link_relocatable (flinfo->info) 11433 ? -1 : ~elf_gnu_osabi_retain)); 11434 11435 /* Read the local symbols. */ 11436 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 11437 if (isymbuf == NULL && locsymcount != 0) 11438 { 11439 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 11440 flinfo->internal_syms, 11441 flinfo->external_syms, 11442 flinfo->locsym_shndx); 11443 if (isymbuf == NULL) 11444 return false; 11445 } 11446 11447 /* Find local symbol sections and adjust values of symbols in 11448 SEC_MERGE sections. Write out those local symbols we know are 11449 going into the output file. */ 11450 isymend = PTR_ADD (isymbuf, locsymcount); 11451 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; 11452 isym < isymend; 11453 isym++, pindex++, ppsection++) 11454 { 11455 asection *isec; 11456 const char *name; 11457 Elf_Internal_Sym osym; 11458 long indx; 11459 int ret; 11460 11461 *pindex = -1; 11462 11463 if (elf_bad_symtab (input_bfd)) 11464 { 11465 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) 11466 { 11467 *ppsection = NULL; 11468 continue; 11469 } 11470 } 11471 11472 if (isym->st_shndx == SHN_UNDEF) 11473 isec = bfd_und_section_ptr; 11474 else if (isym->st_shndx == SHN_ABS) 11475 isec = bfd_abs_section_ptr; 11476 else if (isym->st_shndx == SHN_COMMON) 11477 isec = bfd_com_section_ptr; 11478 else 11479 { 11480 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 11481 if (isec == NULL) 11482 { 11483 /* Don't attempt to output symbols with st_shnx in the 11484 reserved range other than SHN_ABS and SHN_COMMON. */ 11485 isec = bfd_und_section_ptr; 11486 } 11487 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE 11488 && ELF_ST_TYPE (isym->st_info) != STT_SECTION) 11489 isym->st_value = 11490 _bfd_merged_section_offset (output_bfd, &isec, 11491 elf_section_data (isec)->sec_info, 11492 isym->st_value); 11493 } 11494 11495 *ppsection = isec; 11496 11497 /* Don't output the first, undefined, symbol. In fact, don't 11498 output any undefined local symbol. */ 11499 if (isec == bfd_und_section_ptr) 11500 continue; 11501 11502 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 11503 { 11504 /* We never output section symbols. Instead, we use the 11505 section symbol of the corresponding section in the output 11506 file. */ 11507 continue; 11508 } 11509 11510 /* If we are stripping all symbols, we don't want to output this 11511 one. */ 11512 if (flinfo->info->strip == strip_all) 11513 continue; 11514 11515 /* If we are discarding all local symbols, we don't want to 11516 output this one. If we are generating a relocatable output 11517 file, then some of the local symbols may be required by 11518 relocs; we output them below as we discover that they are 11519 needed. */ 11520 if (flinfo->info->discard == discard_all) 11521 continue; 11522 11523 /* If this symbol is defined in a section which we are 11524 discarding, we don't need to keep it. */ 11525 if (isym->st_shndx < SHN_LORESERVE 11526 && (isec->output_section == NULL 11527 || bfd_section_removed_from_list (output_bfd, 11528 isec->output_section))) 11529 continue; 11530 11531 /* Get the name of the symbol. */ 11532 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, 11533 isym->st_name); 11534 if (name == NULL) 11535 return false; 11536 11537 /* See if we are discarding symbols with this name. */ 11538 if ((flinfo->info->strip == strip_some 11539 && (bfd_hash_lookup (flinfo->info->keep_hash, name, false, false) 11540 == NULL)) 11541 || (((flinfo->info->discard == discard_sec_merge 11542 && (isec->flags & SEC_MERGE) 11543 && !bfd_link_relocatable (flinfo->info)) 11544 || flinfo->info->discard == discard_l) 11545 && bfd_is_local_label_name (input_bfd, name))) 11546 continue; 11547 11548 if (ELF_ST_TYPE (isym->st_info) == STT_FILE) 11549 { 11550 if (input_bfd->lto_output) 11551 /* -flto puts a temp file name here. This means builds 11552 are not reproducible. Discard the symbol. */ 11553 continue; 11554 have_file_sym = true; 11555 flinfo->filesym_count += 1; 11556 } 11557 if (!have_file_sym) 11558 { 11559 /* In the absence of debug info, bfd_find_nearest_line uses 11560 FILE symbols to determine the source file for local 11561 function symbols. Provide a FILE symbol here if input 11562 files lack such, so that their symbols won't be 11563 associated with a previous input file. It's not the 11564 source file, but the best we can do. */ 11565 const char *filename; 11566 have_file_sym = true; 11567 flinfo->filesym_count += 1; 11568 memset (&osym, 0, sizeof (osym)); 11569 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 11570 osym.st_shndx = SHN_ABS; 11571 if (input_bfd->lto_output) 11572 filename = NULL; 11573 else 11574 filename = lbasename (bfd_get_filename (input_bfd)); 11575 if (!elf_link_output_symstrtab (flinfo, filename, &osym, 11576 bfd_abs_section_ptr, NULL)) 11577 return false; 11578 } 11579 11580 osym = *isym; 11581 11582 /* Adjust the section index for the output file. */ 11583 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 11584 isec->output_section); 11585 if (osym.st_shndx == SHN_BAD) 11586 return false; 11587 11588 /* ELF symbols in relocatable files are section relative, but 11589 in executable files they are virtual addresses. Note that 11590 this code assumes that all ELF sections have an associated 11591 BFD section with a reasonable value for output_offset; below 11592 we assume that they also have a reasonable value for 11593 output_section. Any special sections must be set up to meet 11594 these requirements. */ 11595 osym.st_value += isec->output_offset; 11596 if (!bfd_link_relocatable (flinfo->info)) 11597 { 11598 osym.st_value += isec->output_section->vma; 11599 if (ELF_ST_TYPE (osym.st_info) == STT_TLS) 11600 { 11601 /* STT_TLS symbols are relative to PT_TLS segment base. */ 11602 if (elf_hash_table (flinfo->info)->tls_sec != NULL) 11603 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; 11604 else 11605 osym.st_info = ELF_ST_INFO (ELF_ST_BIND (osym.st_info), 11606 STT_NOTYPE); 11607 } 11608 } 11609 11610 indx = bfd_get_symcount (output_bfd); 11611 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); 11612 if (ret == 0) 11613 return false; 11614 else if (ret == 1) 11615 *pindex = indx; 11616 } 11617 11618 if (bed->s->arch_size == 32) 11619 { 11620 r_type_mask = 0xff; 11621 r_sym_shift = 8; 11622 address_size = 4; 11623 } 11624 else 11625 { 11626 r_type_mask = 0xffffffff; 11627 r_sym_shift = 32; 11628 address_size = 8; 11629 } 11630 11631 /* Relocate the contents of each section. */ 11632 sym_hashes = elf_sym_hashes (input_bfd); 11633 for (o = input_bfd->sections; o != NULL; o = o->next) 11634 { 11635 bfd_byte *contents; 11636 11637 if (! o->linker_mark) 11638 { 11639 /* This section was omitted from the link. */ 11640 continue; 11641 } 11642 11643 if (!flinfo->info->resolve_section_groups 11644 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) 11645 { 11646 /* Deal with the group signature symbol. */ 11647 struct bfd_elf_section_data *sec_data = elf_section_data (o); 11648 unsigned long symndx = sec_data->this_hdr.sh_info; 11649 asection *osec = o->output_section; 11650 11651 BFD_ASSERT (bfd_link_relocatable (flinfo->info)); 11652 if (symndx >= locsymcount 11653 || (elf_bad_symtab (input_bfd) 11654 && flinfo->sections[symndx] == NULL)) 11655 { 11656 struct elf_link_hash_entry *h; 11657 11658 h = get_link_hash_entry (sym_hashes, symndx, extsymoff); 11659 if (h == NULL) 11660 { 11661 _bfd_error_handler 11662 /* xgettext:c-format */ 11663 (_("error: %pB: unable to create group section symbol"), 11664 input_bfd); 11665 bfd_set_error (bfd_error_bad_value); 11666 return false; 11667 } 11668 11669 /* Arrange for symbol to be output. */ 11670 h->indx = -2; 11671 elf_section_data (osec)->this_hdr.sh_info = -2; 11672 } 11673 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) 11674 { 11675 /* We'll use the output section target_index. */ 11676 asection *sec = flinfo->sections[symndx]->output_section; 11677 elf_section_data (osec)->this_hdr.sh_info = sec->target_index; 11678 } 11679 else 11680 { 11681 if (flinfo->indices[symndx] == -1) 11682 { 11683 /* Otherwise output the local symbol now. */ 11684 Elf_Internal_Sym sym = isymbuf[symndx]; 11685 asection *sec = flinfo->sections[symndx]->output_section; 11686 const char *name; 11687 long indx; 11688 int ret; 11689 11690 name = bfd_elf_string_from_elf_section (input_bfd, 11691 symtab_hdr->sh_link, 11692 sym.st_name); 11693 if (name == NULL) 11694 return false; 11695 11696 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 11697 sec); 11698 if (sym.st_shndx == SHN_BAD) 11699 return false; 11700 11701 sym.st_value += o->output_offset; 11702 11703 indx = bfd_get_symcount (output_bfd); 11704 ret = elf_link_output_symstrtab (flinfo, name, &sym, o, 11705 NULL); 11706 if (ret == 0) 11707 return false; 11708 else if (ret == 1) 11709 flinfo->indices[symndx] = indx; 11710 else 11711 abort (); 11712 } 11713 elf_section_data (osec)->this_hdr.sh_info 11714 = flinfo->indices[symndx]; 11715 } 11716 } 11717 11718 if ((o->flags & SEC_HAS_CONTENTS) == 0 11719 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 11720 continue; 11721 11722 if ((o->flags & SEC_LINKER_CREATED) != 0) 11723 { 11724 /* Section was created by _bfd_elf_link_create_dynamic_sections 11725 or somesuch. */ 11726 continue; 11727 } 11728 11729 /* Get the contents of the section. They have been cached by a 11730 relaxation routine. Note that o is a section in an input 11731 file, so the contents field will not have been set by any of 11732 the routines which work on output files. */ 11733 if (elf_section_data (o)->this_hdr.contents != NULL) 11734 { 11735 contents = elf_section_data (o)->this_hdr.contents; 11736 if (bed->caches_rawsize 11737 && o->rawsize != 0 11738 && o->rawsize < o->size) 11739 { 11740 memcpy (flinfo->contents, contents, o->rawsize); 11741 contents = flinfo->contents; 11742 } 11743 } 11744 else if (!(o->flags & SEC_RELOC) 11745 && !bed->elf_backend_write_section 11746 && o->sec_info_type == SEC_INFO_TYPE_MERGE) 11747 /* A MERGE section that has no relocations doesn't need the 11748 contents anymore, they have been recorded earlier. Except 11749 if the backend has special provisions for writing sections. */ 11750 contents = NULL; 11751 else 11752 { 11753 contents = flinfo->contents; 11754 if (! _bfd_elf_link_mmap_section_contents (input_bfd, o, 11755 &contents)) 11756 return false; 11757 } 11758 11759 if ((o->flags & SEC_RELOC) != 0) 11760 { 11761 Elf_Internal_Rela *internal_relocs; 11762 Elf_Internal_Rela *rel, *relend; 11763 int action_discarded; 11764 int ret; 11765 11766 /* Get the swapped relocs. */ 11767 internal_relocs 11768 = _bfd_elf_link_info_read_relocs (input_bfd, flinfo->info, o, 11769 flinfo->external_relocs, 11770 flinfo->internal_relocs, 11771 false); 11772 if (internal_relocs == NULL 11773 && o->reloc_count > 0) 11774 return false; 11775 11776 action_discarded = -1; 11777 if (!elf_section_ignore_discarded_relocs (o)) 11778 action_discarded = (*bed->action_discarded) (o); 11779 11780 /* Run through the relocs evaluating complex reloc symbols and 11781 looking for relocs against symbols from discarded sections 11782 or section symbols from removed link-once sections. 11783 Complain about relocs against discarded sections. Zero 11784 relocs against removed link-once sections. */ 11785 11786 rel = internal_relocs; 11787 relend = rel + o->reloc_count; 11788 for ( ; rel < relend; rel++) 11789 { 11790 unsigned long r_symndx = rel->r_info >> r_sym_shift; 11791 unsigned int s_type; 11792 asection **ps, *sec; 11793 struct elf_link_hash_entry *h = NULL; 11794 const char *sym_name; 11795 11796 if (r_symndx == STN_UNDEF) 11797 continue; 11798 11799 if (r_symndx >= locsymcount 11800 || (elf_bad_symtab (input_bfd) 11801 && flinfo->sections[r_symndx] == NULL)) 11802 { 11803 h = get_link_hash_entry (sym_hashes, r_symndx, extsymoff); 11804 11805 /* Badly formatted input files can contain relocs that 11806 reference non-existant symbols. Check here so that 11807 we do not seg fault. */ 11808 if (h == NULL) 11809 { 11810 _bfd_error_handler 11811 /* xgettext:c-format */ 11812 (_("error: %pB contains a reloc (%#" PRIx64 ") for section '%pA' " 11813 "that references a non-existent global symbol"), 11814 input_bfd, (uint64_t) rel->r_info, o); 11815 bfd_set_error (bfd_error_bad_value); 11816 return false; 11817 } 11818 11819 s_type = h->type; 11820 11821 /* If a plugin symbol is referenced from a non-IR file, 11822 mark the symbol as undefined. Note that the 11823 linker may attach linker created dynamic sections 11824 to the plugin bfd. Symbols defined in linker 11825 created sections are not plugin symbols. */ 11826 if ((h->root.non_ir_ref_regular 11827 || h->root.non_ir_ref_dynamic) 11828 && (h->root.type == bfd_link_hash_defined 11829 || h->root.type == bfd_link_hash_defweak) 11830 && (h->root.u.def.section->flags 11831 & SEC_LINKER_CREATED) == 0 11832 && h->root.u.def.section->owner != NULL 11833 && (h->root.u.def.section->owner->flags 11834 & BFD_PLUGIN) != 0) 11835 { 11836 h->root.type = bfd_link_hash_undefined; 11837 h->root.u.undef.abfd = h->root.u.def.section->owner; 11838 } 11839 11840 ps = NULL; 11841 if (h->root.type == bfd_link_hash_defined 11842 || h->root.type == bfd_link_hash_defweak) 11843 ps = &h->root.u.def.section; 11844 11845 sym_name = h->root.root.string; 11846 } 11847 else 11848 { 11849 Elf_Internal_Sym *sym = isymbuf + r_symndx; 11850 11851 s_type = ELF_ST_TYPE (sym->st_info); 11852 ps = &flinfo->sections[r_symndx]; 11853 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, 11854 sym, *ps); 11855 } 11856 11857 if ((s_type == STT_RELC || s_type == STT_SRELC) 11858 && !bfd_link_relocatable (flinfo->info)) 11859 { 11860 bfd_vma val; 11861 bfd_vma dot = (rel->r_offset 11862 + o->output_offset + o->output_section->vma); 11863 #ifdef DEBUG 11864 printf ("Encountered a complex symbol!"); 11865 printf (" (input_bfd %s, section %s, reloc %ld\n", 11866 bfd_get_filename (input_bfd), o->name, 11867 (long) (rel - internal_relocs)); 11868 printf (" symbol: idx %8.8lx, name %s\n", 11869 r_symndx, sym_name); 11870 printf (" reloc : info %8.8lx, addr %8.8lx\n", 11871 (unsigned long) rel->r_info, 11872 (unsigned long) rel->r_offset); 11873 #endif 11874 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, 11875 isymbuf, locsymcount, s_type == STT_SRELC)) 11876 return false; 11877 11878 /* Symbol evaluated OK. Update to absolute value. */ 11879 set_symbol_value (input_bfd, isymbuf, locsymcount, 11880 r_symndx, val); 11881 continue; 11882 } 11883 11884 if (action_discarded != -1 && ps != NULL) 11885 { 11886 /* Complain if the definition comes from a 11887 discarded section. */ 11888 if ((sec = *ps) != NULL && discarded_section (sec)) 11889 { 11890 BFD_ASSERT (r_symndx != STN_UNDEF); 11891 if (action_discarded & COMPLAIN) 11892 (*flinfo->info->callbacks->einfo) 11893 /* xgettext:c-format */ 11894 (_("%X`%s' referenced in section `%pA' of %pB: " 11895 "defined in discarded section `%pA' of %pB\n"), 11896 sym_name, o, input_bfd, sec, sec->owner); 11897 11898 /* Try to do the best we can to support buggy old 11899 versions of gcc. Pretend that the symbol is 11900 really defined in the kept linkonce section. 11901 FIXME: This is quite broken. Modifying the 11902 symbol here means we will be changing all later 11903 uses of the symbol, not just in this section. */ 11904 if (action_discarded & PRETEND) 11905 { 11906 asection *kept; 11907 11908 kept = _bfd_elf_check_kept_section (sec, 11909 flinfo->info); 11910 if (kept != NULL) 11911 { 11912 *ps = kept; 11913 continue; 11914 } 11915 } 11916 } 11917 } 11918 } 11919 11920 /* Relocate the section by invoking a back end routine. 11921 11922 The back end routine is responsible for adjusting the 11923 section contents as necessary, and (if using Rela relocs 11924 and generating a relocatable output file) adjusting the 11925 reloc addend as necessary. 11926 11927 The back end routine does not have to worry about setting 11928 the reloc address or the reloc symbol index. 11929 11930 The back end routine is given a pointer to the swapped in 11931 internal symbols, and can access the hash table entries 11932 for the external symbols via elf_sym_hashes (input_bfd). 11933 11934 When generating relocatable output, the back end routine 11935 must handle STB_LOCAL/STT_SECTION symbols specially. The 11936 output symbol is going to be a section symbol 11937 corresponding to the output section, which will require 11938 the addend to be adjusted. */ 11939 11940 ret = (*relocate_section) (output_bfd, flinfo->info, 11941 input_bfd, o, contents, 11942 internal_relocs, 11943 isymbuf, 11944 flinfo->sections); 11945 if (!ret) 11946 return false; 11947 11948 if (ret == 2 11949 || bfd_link_relocatable (flinfo->info) 11950 || flinfo->info->emitrelocations) 11951 { 11952 Elf_Internal_Rela *irela; 11953 Elf_Internal_Rela *irelaend, *irelamid; 11954 bfd_vma last_offset; 11955 struct elf_link_hash_entry **rel_hash; 11956 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; 11957 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; 11958 unsigned int next_erel; 11959 bool rela_normal; 11960 struct bfd_elf_section_data *esdi, *esdo; 11961 11962 esdi = elf_section_data (o); 11963 esdo = elf_section_data (o->output_section); 11964 rela_normal = false; 11965 11966 /* Adjust the reloc addresses and symbol indices. */ 11967 11968 irela = internal_relocs; 11969 irelaend = irela + o->reloc_count; 11970 rel_hash = PTR_ADD (esdo->rel.hashes, esdo->rel.count); 11971 /* We start processing the REL relocs, if any. When we reach 11972 IRELAMID in the loop, we switch to the RELA relocs. */ 11973 irelamid = irela; 11974 if (esdi->rel.hdr != NULL) 11975 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) 11976 * bed->s->int_rels_per_ext_rel); 11977 rel_hash_list = rel_hash; 11978 rela_hash_list = NULL; 11979 last_offset = o->output_offset; 11980 if (!bfd_link_relocatable (flinfo->info)) 11981 last_offset += o->output_section->vma; 11982 for (next_erel = 0; irela < irelaend; irela++, next_erel++) 11983 { 11984 unsigned long r_symndx; 11985 asection *sec; 11986 Elf_Internal_Sym sym; 11987 11988 if (next_erel == bed->s->int_rels_per_ext_rel) 11989 { 11990 rel_hash++; 11991 next_erel = 0; 11992 } 11993 11994 if (irela == irelamid) 11995 { 11996 rel_hash = PTR_ADD (esdo->rela.hashes, esdo->rela.count); 11997 rela_hash_list = rel_hash; 11998 rela_normal = bed->rela_normal; 11999 } 12000 12001 irela->r_offset = _bfd_elf_section_offset (output_bfd, 12002 flinfo->info, o, 12003 irela->r_offset); 12004 if (irela->r_offset >= (bfd_vma) -2) 12005 { 12006 /* This is a reloc for a deleted entry or somesuch. 12007 Turn it into an R_*_NONE reloc, at the same 12008 offset as the last reloc. elf_eh_frame.c and 12009 bfd_elf_discard_info rely on reloc offsets 12010 being ordered. */ 12011 irela->r_offset = last_offset; 12012 irela->r_info = 0; 12013 irela->r_addend = 0; 12014 continue; 12015 } 12016 12017 irela->r_offset += o->output_offset; 12018 12019 /* Relocs in an executable have to be virtual addresses. */ 12020 if (!bfd_link_relocatable (flinfo->info)) 12021 irela->r_offset += o->output_section->vma; 12022 12023 last_offset = irela->r_offset; 12024 12025 r_symndx = irela->r_info >> r_sym_shift; 12026 if (r_symndx == STN_UNDEF) 12027 continue; 12028 12029 if (r_symndx >= locsymcount 12030 || (elf_bad_symtab (input_bfd) 12031 && flinfo->sections[r_symndx] == NULL)) 12032 { 12033 struct elf_link_hash_entry *rh; 12034 12035 /* This is a reloc against a global symbol. We 12036 have not yet output all the local symbols, so 12037 we do not know the symbol index of any global 12038 symbol. We set the rel_hash entry for this 12039 reloc to point to the global hash table entry 12040 for this symbol. The symbol index is then 12041 set at the end of bfd_elf_final_link. */ 12042 rh = get_link_hash_entry (elf_sym_hashes (input_bfd), 12043 r_symndx, extsymoff); 12044 if (rh == NULL) 12045 { 12046 /* FIXME: Generate an error ? */ 12047 continue; 12048 } 12049 12050 /* Setting the index to -2 tells elf_link_output_extsym 12051 that this symbol is used by a reloc. */ 12052 BFD_ASSERT (rh->indx < 0); 12053 rh->indx = -2; 12054 *rel_hash = rh; 12055 12056 continue; 12057 } 12058 12059 /* This is a reloc against a local symbol. */ 12060 12061 *rel_hash = NULL; 12062 sym = isymbuf[r_symndx]; 12063 sec = flinfo->sections[r_symndx]; 12064 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) 12065 { 12066 /* I suppose the backend ought to fill in the 12067 section of any STT_SECTION symbol against a 12068 processor specific section. */ 12069 r_symndx = STN_UNDEF; 12070 if (bfd_is_abs_section (sec)) 12071 ; 12072 else if (sec == NULL || sec->owner == NULL) 12073 { 12074 bfd_set_error (bfd_error_bad_value); 12075 return false; 12076 } 12077 else 12078 { 12079 asection *osec = sec->output_section; 12080 12081 /* If we have discarded a section, the output 12082 section will be the absolute section. In 12083 case of discarded SEC_MERGE sections, use 12084 the kept section. relocate_section should 12085 have already handled discarded linkonce 12086 sections. */ 12087 if (bfd_is_abs_section (osec) 12088 && sec->kept_section != NULL 12089 && sec->kept_section->output_section != NULL) 12090 { 12091 osec = sec->kept_section->output_section; 12092 irela->r_addend -= osec->vma; 12093 } 12094 12095 if (!bfd_is_abs_section (osec)) 12096 { 12097 r_symndx = osec->target_index; 12098 if (r_symndx == STN_UNDEF) 12099 { 12100 irela->r_addend += osec->vma; 12101 osec = _bfd_nearby_section (output_bfd, osec, 12102 osec->vma); 12103 irela->r_addend -= osec->vma; 12104 r_symndx = osec->target_index; 12105 } 12106 } 12107 } 12108 12109 /* Adjust the addend according to where the 12110 section winds up in the output section. */ 12111 if (rela_normal) 12112 irela->r_addend += sec->output_offset; 12113 } 12114 else 12115 { 12116 if (flinfo->indices[r_symndx] == -1) 12117 { 12118 unsigned long shlink; 12119 const char *name; 12120 asection *osec; 12121 long indx; 12122 12123 if (flinfo->info->strip == strip_all) 12124 { 12125 /* You can't do ld -r -s. */ 12126 bfd_set_error (bfd_error_invalid_operation); 12127 return false; 12128 } 12129 12130 /* This symbol was skipped earlier, but 12131 since it is needed by a reloc, we 12132 must output it now. */ 12133 shlink = symtab_hdr->sh_link; 12134 name = (bfd_elf_string_from_elf_section 12135 (input_bfd, shlink, sym.st_name)); 12136 if (name == NULL) 12137 return false; 12138 12139 osec = sec->output_section; 12140 sym.st_shndx = 12141 _bfd_elf_section_from_bfd_section (output_bfd, 12142 osec); 12143 if (sym.st_shndx == SHN_BAD) 12144 return false; 12145 12146 sym.st_value += sec->output_offset; 12147 if (!bfd_link_relocatable (flinfo->info)) 12148 { 12149 sym.st_value += osec->vma; 12150 if (ELF_ST_TYPE (sym.st_info) == STT_TLS) 12151 { 12152 struct elf_link_hash_table *htab 12153 = elf_hash_table (flinfo->info); 12154 12155 /* STT_TLS symbols are relative to PT_TLS 12156 segment base. */ 12157 if (htab->tls_sec != NULL) 12158 sym.st_value -= htab->tls_sec->vma; 12159 else 12160 sym.st_info 12161 = ELF_ST_INFO (ELF_ST_BIND (sym.st_info), 12162 STT_NOTYPE); 12163 } 12164 } 12165 12166 indx = bfd_get_symcount (output_bfd); 12167 ret = elf_link_output_symstrtab (flinfo, name, 12168 &sym, sec, 12169 NULL); 12170 if (ret == 0) 12171 return false; 12172 else if (ret == 1) 12173 flinfo->indices[r_symndx] = indx; 12174 else 12175 abort (); 12176 } 12177 12178 r_symndx = flinfo->indices[r_symndx]; 12179 } 12180 12181 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift 12182 | (irela->r_info & r_type_mask)); 12183 } 12184 12185 /* Swap out the relocs. */ 12186 input_rel_hdr = esdi->rel.hdr; 12187 if (input_rel_hdr && input_rel_hdr->sh_size != 0) 12188 { 12189 if (!bed->elf_backend_emit_relocs (output_bfd, o, 12190 input_rel_hdr, 12191 internal_relocs, 12192 rel_hash_list)) 12193 return false; 12194 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) 12195 * bed->s->int_rels_per_ext_rel); 12196 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); 12197 } 12198 12199 input_rela_hdr = esdi->rela.hdr; 12200 if (input_rela_hdr && input_rela_hdr->sh_size != 0) 12201 { 12202 if (!bed->elf_backend_emit_relocs (output_bfd, o, 12203 input_rela_hdr, 12204 internal_relocs, 12205 rela_hash_list)) 12206 return false; 12207 } 12208 } 12209 } 12210 12211 /* Write out the modified section contents. */ 12212 if (bed->elf_backend_write_section 12213 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, 12214 contents)) 12215 { 12216 /* Section written out. */ 12217 } 12218 else switch (o->sec_info_type) 12219 { 12220 case SEC_INFO_TYPE_STABS: 12221 if (! (_bfd_write_section_stabs 12222 (output_bfd, 12223 &elf_hash_table (flinfo->info)->stab_info, 12224 o, &elf_section_data (o)->sec_info, contents))) 12225 return false; 12226 break; 12227 case SEC_INFO_TYPE_MERGE: 12228 if (! _bfd_write_merged_section (output_bfd, o, 12229 elf_section_data (o)->sec_info)) 12230 return false; 12231 break; 12232 case SEC_INFO_TYPE_EH_FRAME: 12233 { 12234 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, 12235 o, contents)) 12236 return false; 12237 } 12238 break; 12239 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 12240 { 12241 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, 12242 flinfo->info, 12243 o, contents)) 12244 return false; 12245 } 12246 break; 12247 case SEC_INFO_TYPE_SFRAME: 12248 { 12249 /* Merge SFrame section into the SFrame encoder context of the 12250 output_bfd's section. The final .sframe output section will 12251 be written out later. */ 12252 if (!_bfd_elf_merge_section_sframe (output_bfd, flinfo->info, 12253 o, contents)) 12254 return false; 12255 } 12256 break; 12257 default: 12258 { 12259 if (! (o->flags & SEC_EXCLUDE)) 12260 { 12261 file_ptr offset = (file_ptr) o->output_offset; 12262 bfd_size_type todo = o->size; 12263 12264 offset *= bfd_octets_per_byte (output_bfd, o); 12265 12266 if ((o->flags & SEC_ELF_REVERSE_COPY) 12267 && o->size > address_size) 12268 { 12269 /* Reverse-copy input section to output. */ 12270 12271 if ((o->size & (address_size - 1)) != 0 12272 || (o->reloc_count != 0 12273 && (o->size * bed->s->int_rels_per_ext_rel 12274 != o->reloc_count * address_size))) 12275 { 12276 _bfd_error_handler 12277 /* xgettext:c-format */ 12278 (_("error: %pB: size of section %pA is not " 12279 "multiple of address size"), 12280 input_bfd, o); 12281 bfd_set_error (bfd_error_bad_value); 12282 return false; 12283 } 12284 12285 do 12286 { 12287 todo -= address_size; 12288 if (! bfd_set_section_contents (output_bfd, 12289 o->output_section, 12290 contents + todo, 12291 offset, 12292 address_size)) 12293 return false; 12294 if (todo == 0) 12295 break; 12296 offset += address_size; 12297 } 12298 while (1); 12299 } 12300 else if (! bfd_set_section_contents (output_bfd, 12301 o->output_section, 12302 contents, 12303 offset, todo)) 12304 return false; 12305 } 12306 } 12307 break; 12308 } 12309 12310 /* Munmap the section contents for each input section. */ 12311 _bfd_elf_link_munmap_section_contents (o); 12312 } 12313 12314 return true; 12315 } 12316 12317 /* Generate a reloc when linking an ELF file. This is a reloc 12318 requested by the linker, and does not come from any input file. This 12319 is used to build constructor and destructor tables when linking 12320 with -Ur. */ 12321 12322 static bool 12323 elf_reloc_link_order (bfd *output_bfd, 12324 struct bfd_link_info *info, 12325 asection *output_section, 12326 struct bfd_link_order *link_order) 12327 { 12328 reloc_howto_type *howto; 12329 long indx; 12330 bfd_vma offset; 12331 bfd_vma addend; 12332 struct bfd_elf_section_reloc_data *reldata; 12333 struct elf_link_hash_entry **rel_hash_ptr; 12334 Elf_Internal_Shdr *rel_hdr; 12335 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 12336 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; 12337 bfd_byte *erel; 12338 unsigned int i; 12339 struct bfd_elf_section_data *esdo = elf_section_data (output_section); 12340 12341 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 12342 if (howto == NULL) 12343 { 12344 bfd_set_error (bfd_error_bad_value); 12345 return false; 12346 } 12347 12348 addend = link_order->u.reloc.p->addend; 12349 12350 if (esdo->rel.hdr) 12351 reldata = &esdo->rel; 12352 else if (esdo->rela.hdr) 12353 reldata = &esdo->rela; 12354 else 12355 { 12356 reldata = NULL; 12357 BFD_ASSERT (0); 12358 } 12359 12360 /* Figure out the symbol index. */ 12361 rel_hash_ptr = reldata->hashes + reldata->count; 12362 if (link_order->type == bfd_section_reloc_link_order) 12363 { 12364 indx = link_order->u.reloc.p->u.section->target_index; 12365 BFD_ASSERT (indx != 0); 12366 *rel_hash_ptr = NULL; 12367 } 12368 else 12369 { 12370 struct elf_link_hash_entry *h; 12371 12372 /* Treat a reloc against a defined symbol as though it were 12373 actually against the section. */ 12374 h = ((struct elf_link_hash_entry *) 12375 bfd_wrapped_link_hash_lookup (output_bfd, info, 12376 link_order->u.reloc.p->u.name, 12377 false, false, true)); 12378 if (h != NULL 12379 && (h->root.type == bfd_link_hash_defined 12380 || h->root.type == bfd_link_hash_defweak)) 12381 { 12382 asection *section; 12383 12384 section = h->root.u.def.section; 12385 indx = section->output_section->target_index; 12386 *rel_hash_ptr = NULL; 12387 /* It seems that we ought to add the symbol value to the 12388 addend here, but in practice it has already been added 12389 because it was passed to constructor_callback. */ 12390 addend += section->output_section->vma + section->output_offset; 12391 } 12392 else if (h != NULL) 12393 { 12394 /* Setting the index to -2 tells elf_link_output_extsym that 12395 this symbol is used by a reloc. */ 12396 h->indx = -2; 12397 *rel_hash_ptr = h; 12398 indx = 0; 12399 } 12400 else 12401 { 12402 (*info->callbacks->unattached_reloc) 12403 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); 12404 indx = 0; 12405 } 12406 } 12407 12408 /* If this is an inplace reloc, we must write the addend into the 12409 object file. */ 12410 if (howto->partial_inplace && addend != 0) 12411 { 12412 bfd_size_type size; 12413 bfd_reloc_status_type rstat; 12414 bfd_byte *buf; 12415 bool ok; 12416 const char *sym_name; 12417 bfd_size_type octets; 12418 12419 size = (bfd_size_type) bfd_get_reloc_size (howto); 12420 buf = (bfd_byte *) bfd_zmalloc (size); 12421 if (buf == NULL && size != 0) 12422 return false; 12423 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 12424 switch (rstat) 12425 { 12426 case bfd_reloc_ok: 12427 break; 12428 12429 default: 12430 case bfd_reloc_outofrange: 12431 abort (); 12432 12433 case bfd_reloc_overflow: 12434 if (link_order->type == bfd_section_reloc_link_order) 12435 sym_name = bfd_section_name (link_order->u.reloc.p->u.section); 12436 else 12437 sym_name = link_order->u.reloc.p->u.name; 12438 (*info->callbacks->reloc_overflow) (info, NULL, sym_name, 12439 howto->name, addend, NULL, NULL, 12440 (bfd_vma) 0); 12441 break; 12442 } 12443 12444 octets = link_order->offset * bfd_octets_per_byte (output_bfd, 12445 output_section); 12446 ok = bfd_set_section_contents (output_bfd, output_section, buf, 12447 octets, size); 12448 free (buf); 12449 if (! ok) 12450 return false; 12451 } 12452 12453 /* The address of a reloc is relative to the section in a 12454 relocatable file, and is a virtual address in an executable 12455 file. */ 12456 offset = link_order->offset; 12457 if (! bfd_link_relocatable (info)) 12458 offset += output_section->vma; 12459 12460 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) 12461 { 12462 irel[i].r_offset = offset; 12463 irel[i].r_info = 0; 12464 irel[i].r_addend = 0; 12465 } 12466 if (bed->s->arch_size == 32) 12467 irel[0].r_info = ELF32_R_INFO (indx, howto->type); 12468 else 12469 #ifdef BFD64 12470 irel[0].r_info = ELF64_R_INFO (indx, howto->type); 12471 #else 12472 BFD_FAIL(); 12473 #endif 12474 12475 rel_hdr = reldata->hdr; 12476 erel = rel_hdr->contents; 12477 if (rel_hdr->sh_type == SHT_REL) 12478 { 12479 erel += reldata->count * bed->s->sizeof_rel; 12480 (*bed->s->swap_reloc_out) (output_bfd, irel, erel); 12481 } 12482 else 12483 { 12484 irel[0].r_addend = addend; 12485 erel += reldata->count * bed->s->sizeof_rela; 12486 (*bed->s->swap_reloca_out) (output_bfd, irel, erel); 12487 } 12488 12489 ++reldata->count; 12490 12491 return true; 12492 } 12493 12494 /* Generate an import library in INFO->implib_bfd from symbols in ABFD. 12495 Returns TRUE upon success, FALSE otherwise. */ 12496 12497 static bool 12498 elf_output_implib (bfd *abfd, struct bfd_link_info *info) 12499 { 12500 bool ret = false; 12501 bfd *implib_bfd; 12502 const struct elf_backend_data *bed; 12503 flagword flags; 12504 enum bfd_architecture arch; 12505 unsigned int mach; 12506 asymbol **sympp = NULL; 12507 long symsize; 12508 long symcount; 12509 long src_count; 12510 elf_symbol_type *osymbuf; 12511 size_t amt; 12512 12513 implib_bfd = info->out_implib_bfd; 12514 bed = get_elf_backend_data (abfd); 12515 12516 if (!bfd_set_format (implib_bfd, bfd_object)) 12517 return false; 12518 12519 /* Use flag from executable but make it a relocatable object. */ 12520 flags = bfd_get_file_flags (abfd); 12521 flags &= ~HAS_RELOC; 12522 if (!bfd_set_start_address (implib_bfd, 0) 12523 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) 12524 return false; 12525 12526 /* Copy architecture of output file to import library file. */ 12527 arch = bfd_get_arch (abfd); 12528 mach = bfd_get_mach (abfd); 12529 if (!bfd_set_arch_mach (implib_bfd, arch, mach) 12530 && (abfd->target_defaulted 12531 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) 12532 return false; 12533 12534 /* Get symbol table size. */ 12535 symsize = bfd_get_symtab_upper_bound (abfd); 12536 if (symsize < 0) 12537 return false; 12538 12539 /* Read in the symbol table. */ 12540 sympp = (asymbol **) bfd_malloc (symsize); 12541 if (sympp == NULL) 12542 return false; 12543 12544 symcount = bfd_canonicalize_symtab (abfd, sympp); 12545 if (symcount < 0) 12546 goto free_sym_buf; 12547 12548 /* Allow the BFD backend to copy any private header data it 12549 understands from the output BFD to the import library BFD. */ 12550 if (! bfd_copy_private_header_data (abfd, implib_bfd)) 12551 goto free_sym_buf; 12552 12553 /* Filter symbols to appear in the import library. */ 12554 if (bed->elf_backend_filter_implib_symbols) 12555 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, 12556 symcount); 12557 else 12558 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); 12559 if (symcount == 0) 12560 { 12561 bfd_set_error (bfd_error_no_symbols); 12562 _bfd_error_handler (_("%pB: no symbol found for import library"), 12563 implib_bfd); 12564 goto free_sym_buf; 12565 } 12566 12567 12568 /* Make symbols absolute. */ 12569 amt = symcount * sizeof (*osymbuf); 12570 osymbuf = (elf_symbol_type *) bfd_alloc (implib_bfd, amt); 12571 if (osymbuf == NULL) 12572 goto free_sym_buf; 12573 12574 for (src_count = 0; src_count < symcount; src_count++) 12575 { 12576 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], 12577 sizeof (*osymbuf)); 12578 osymbuf[src_count].symbol.section = bfd_abs_section_ptr; 12579 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; 12580 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; 12581 osymbuf[src_count].internal_elf_sym.st_value = 12582 osymbuf[src_count].symbol.value; 12583 sympp[src_count] = &osymbuf[src_count].symbol; 12584 } 12585 12586 bfd_set_symtab (implib_bfd, sympp, symcount); 12587 12588 /* Allow the BFD backend to copy any private data it understands 12589 from the output BFD to the import library BFD. This is done last 12590 to permit the routine to look at the filtered symbol table. */ 12591 if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) 12592 goto free_sym_buf; 12593 12594 if (!bfd_close (implib_bfd)) 12595 goto free_sym_buf; 12596 12597 ret = true; 12598 12599 free_sym_buf: 12600 free (sympp); 12601 return ret; 12602 } 12603 12604 static void 12605 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) 12606 { 12607 asection *o; 12608 12609 if (flinfo->symstrtab != NULL) 12610 _bfd_elf_strtab_free (flinfo->symstrtab); 12611 free (flinfo->contents); 12612 free (flinfo->external_relocs); 12613 free (flinfo->internal_relocs); 12614 free (flinfo->external_syms); 12615 free (flinfo->locsym_shndx); 12616 free (flinfo->internal_syms); 12617 free (flinfo->indices); 12618 free (flinfo->sections); 12619 if (flinfo->symshndxbuf != (Elf_External_Sym_Shndx *) -1) 12620 free (flinfo->symshndxbuf); 12621 for (o = obfd->sections; o != NULL; o = o->next) 12622 { 12623 struct bfd_elf_section_data *esdo = elf_section_data (o); 12624 free (esdo->rel.hashes); 12625 free (esdo->rela.hashes); 12626 } 12627 } 12628 12629 /* Do the final step of an ELF link. */ 12630 12631 bool 12632 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) 12633 { 12634 bool dynamic; 12635 bool emit_relocs; 12636 bfd *dynobj; 12637 struct elf_final_link_info flinfo; 12638 asection *o; 12639 struct bfd_link_order *p; 12640 bfd *sub; 12641 bfd_size_type max_contents_size; 12642 bfd_size_type max_external_reloc_size; 12643 bfd_size_type max_internal_reloc_count; 12644 bfd_size_type max_sym_count; 12645 bfd_size_type max_sym_shndx_count; 12646 Elf_Internal_Sym elfsym; 12647 unsigned int i; 12648 Elf_Internal_Shdr *symtab_hdr; 12649 Elf_Internal_Shdr *symtab_shndx_hdr; 12650 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12651 struct elf_outext_info eoinfo; 12652 bool merged; 12653 size_t relativecount; 12654 size_t relr_entsize; 12655 asection *reldyn = 0; 12656 bfd_size_type amt; 12657 struct elf_link_hash_table *htab = elf_hash_table (info); 12658 bool sections_removed; 12659 12660 if (!is_elf_hash_table (&htab->root)) 12661 return false; 12662 12663 if (bfd_link_pic (info)) 12664 abfd->flags |= DYNAMIC; 12665 12666 dynamic = htab->dynamic_sections_created; 12667 dynobj = htab->dynobj; 12668 12669 emit_relocs = (bfd_link_relocatable (info) 12670 || info->emitrelocations); 12671 12672 memset (&flinfo, 0, sizeof (flinfo)); 12673 flinfo.info = info; 12674 flinfo.output_bfd = abfd; 12675 flinfo.symstrtab = _bfd_elf_strtab_init (); 12676 if (flinfo.symstrtab == NULL) 12677 return false; 12678 12679 if (! dynamic) 12680 { 12681 flinfo.hash_sec = NULL; 12682 flinfo.symver_sec = NULL; 12683 } 12684 else 12685 { 12686 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); 12687 /* Note that dynsym_sec can be NULL (on VMS). */ 12688 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); 12689 /* Note that it is OK if symver_sec is NULL. */ 12690 } 12691 12692 if (info->unique_symbol 12693 && !bfd_hash_table_init (&flinfo.local_hash_table, 12694 local_hash_newfunc, 12695 sizeof (struct local_hash_entry))) 12696 return false; 12697 12698 /* The object attributes have been merged. Remove the input 12699 sections from the link, and set the contents of the output 12700 section. */ 12701 sections_removed = false; 12702 const char *obj_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; 12703 for (o = abfd->sections; o != NULL; o = o->next) 12704 { 12705 bool remove_section = false; 12706 12707 if ((obj_attrs_section && strcmp (o->name, obj_attrs_section) == 0) 12708 || strcmp (o->name, ".gnu.attributes") == 0) 12709 { 12710 for (p = o->map_head.link_order; p != NULL; p = p->next) 12711 { 12712 asection *input_section; 12713 12714 if (p->type != bfd_indirect_link_order) 12715 continue; 12716 input_section = p->u.indirect.section; 12717 /* Hack: reset the SEC_HAS_CONTENTS flag so that 12718 elf_link_input_bfd ignores this section. */ 12719 input_section->flags &= ~SEC_HAS_CONTENTS; 12720 } 12721 12722 /* Skip this section later on. */ 12723 o->map_head.link_order = NULL; 12724 12725 bfd_vma attr_size = bfd_elf_obj_attr_size (abfd); 12726 /* Once ELF headers have been written, the size of a section is 12727 frozen. We need to set the size of the attribute section before 12728 _bfd_elf_compute_section_file_positions. */ 12729 bfd_set_section_size (o, attr_size); 12730 if (attr_size > 0) 12731 elf_obj_build_attributes (abfd) = o; 12732 else 12733 remove_section = true; 12734 } 12735 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) 12736 { 12737 /* Remove empty group section from linker output. */ 12738 remove_section = true; 12739 } 12740 if (remove_section) 12741 { 12742 o->flags |= SEC_EXCLUDE; 12743 bfd_section_list_remove (abfd, o); 12744 abfd->section_count--; 12745 sections_removed = true; 12746 } 12747 } 12748 if (sections_removed) 12749 _bfd_fix_excluded_sec_syms (abfd, info); 12750 12751 /* Count up the number of relocations we will output for each output 12752 section, so that we know the sizes of the reloc sections. We 12753 also figure out some maximum sizes. */ 12754 #ifdef USE_MMAP 12755 if (bed->use_mmap) 12756 { 12757 /* Mmap is used only if section size >= the minimum mmap section 12758 size. The initial max_contents_size value covers all sections 12759 smaller than the minimum mmap section size. It may be increased 12760 for compressed or linker created sections or sections whose 12761 rawsize != size. max_external_reloc_size covers all relocation 12762 sections smaller than the minimum mmap section size. */ 12763 max_contents_size = _bfd_minimum_mmap_size; 12764 max_external_reloc_size = _bfd_minimum_mmap_size; 12765 } 12766 else 12767 #endif 12768 { 12769 max_contents_size = 0; 12770 max_external_reloc_size = 0; 12771 } 12772 max_internal_reloc_count = 0; 12773 max_sym_count = 0; 12774 max_sym_shndx_count = 0; 12775 merged = false; 12776 for (o = abfd->sections; o != NULL; o = o->next) 12777 { 12778 struct bfd_elf_section_data *esdo = elf_section_data (o); 12779 o->reloc_count = 0; 12780 12781 for (p = o->map_head.link_order; p != NULL; p = p->next) 12782 { 12783 unsigned int reloc_count = 0; 12784 unsigned int additional_reloc_count = 0; 12785 struct bfd_elf_section_data *esdi = NULL; 12786 12787 if (p->type == bfd_section_reloc_link_order 12788 || p->type == bfd_symbol_reloc_link_order) 12789 reloc_count = 1; 12790 else if (p->type == bfd_indirect_link_order) 12791 { 12792 asection *sec; 12793 12794 sec = p->u.indirect.section; 12795 12796 /* Mark all sections which are to be included in the 12797 link. This will normally be every section. We need 12798 to do this so that we can identify any sections which 12799 the linker has decided to not include. */ 12800 sec->linker_mark = true; 12801 12802 if (sec->flags & SEC_MERGE) 12803 merged = true; 12804 12805 #ifdef USE_MMAP 12806 /* Mmap is used only on non-compressed, non-linker created 12807 sections whose rawsize == size. */ 12808 if (!bed->use_mmap 12809 || sec->compress_status != COMPRESS_SECTION_NONE 12810 || (sec->flags & SEC_LINKER_CREATED) != 0 12811 || sec->rawsize != sec->size) 12812 #endif 12813 { 12814 if (sec->rawsize > max_contents_size) 12815 max_contents_size = sec->rawsize; 12816 if (sec->size > max_contents_size) 12817 max_contents_size = sec->size; 12818 } 12819 12820 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour 12821 && (sec->owner->flags & DYNAMIC) == 0) 12822 { 12823 size_t sym_count; 12824 12825 /* We are interested in just local symbols, not all 12826 symbols. */ 12827 if (elf_bad_symtab (sec->owner)) 12828 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size 12829 / bed->s->sizeof_sym); 12830 else 12831 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; 12832 12833 if (sym_count > max_sym_count) 12834 max_sym_count = sym_count; 12835 12836 if (sym_count > max_sym_shndx_count 12837 && elf_symtab_shndx_list (sec->owner) != NULL) 12838 max_sym_shndx_count = sym_count; 12839 12840 esdi = elf_section_data (sec); 12841 12842 if (esdi->this_hdr.sh_type == SHT_REL 12843 || esdi->this_hdr.sh_type == SHT_RELA) 12844 /* Some backends use reloc_count in relocation sections 12845 to count particular types of relocs. Of course, 12846 reloc sections themselves can't have relocations. */ 12847 ; 12848 else if (emit_relocs) 12849 { 12850 reloc_count = sec->reloc_count; 12851 if (bed->elf_backend_count_additional_relocs) 12852 { 12853 int c; 12854 c = (*bed->elf_backend_count_additional_relocs) (sec); 12855 additional_reloc_count += c; 12856 } 12857 } 12858 else if (bed->elf_backend_count_relocs) 12859 reloc_count = (*bed->elf_backend_count_relocs) (info, sec); 12860 12861 if ((sec->flags & SEC_RELOC) != 0) 12862 { 12863 #ifdef USE_MMAP 12864 if (!bed->use_mmap) 12865 #endif 12866 { 12867 size_t ext_size = 0; 12868 12869 if (esdi->rel.hdr != NULL) 12870 ext_size = esdi->rel.hdr->sh_size; 12871 if (esdi->rela.hdr != NULL) 12872 ext_size += esdi->rela.hdr->sh_size; 12873 12874 if (ext_size > max_external_reloc_size) 12875 max_external_reloc_size = ext_size; 12876 } 12877 if (sec->reloc_count > max_internal_reloc_count) 12878 max_internal_reloc_count = sec->reloc_count; 12879 } 12880 } 12881 } 12882 12883 if (reloc_count == 0) 12884 continue; 12885 12886 reloc_count += additional_reloc_count; 12887 o->reloc_count += reloc_count; 12888 12889 if (p->type == bfd_indirect_link_order && emit_relocs) 12890 { 12891 if (esdi->rel.hdr) 12892 { 12893 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); 12894 esdo->rel.count += additional_reloc_count; 12895 } 12896 if (esdi->rela.hdr) 12897 { 12898 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); 12899 esdo->rela.count += additional_reloc_count; 12900 } 12901 } 12902 else 12903 { 12904 if (o->use_rela_p) 12905 esdo->rela.count += reloc_count; 12906 else 12907 esdo->rel.count += reloc_count; 12908 } 12909 } 12910 12911 if (o->reloc_count > 0) 12912 o->flags |= SEC_RELOC; 12913 else 12914 { 12915 /* Explicitly clear the SEC_RELOC flag. The linker tends to 12916 set it (this is probably a bug) and if it is set 12917 assign_section_numbers will create a reloc section. */ 12918 o->flags &=~ SEC_RELOC; 12919 } 12920 12921 /* If the SEC_ALLOC flag is not set, force the section VMA to 12922 zero. This is done in elf_fake_sections as well, but forcing 12923 the VMA to 0 here will ensure that relocs against these 12924 sections are handled correctly. */ 12925 if ((o->flags & SEC_ALLOC) == 0 12926 && ! o->user_set_vma) 12927 o->vma = 0; 12928 } 12929 12930 if (! bfd_link_relocatable (info) && merged) 12931 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); 12932 12933 /* Figure out the file positions for everything but the symbol table 12934 and the relocs. We set symcount to force assign_section_numbers 12935 to create a symbol table. */ 12936 abfd->symcount = info->strip != strip_all || emit_relocs; 12937 BFD_ASSERT (! abfd->output_has_begun); 12938 if (! _bfd_elf_compute_section_file_positions (abfd, info)) 12939 goto error_return; 12940 12941 /* Set sizes, and assign file positions for reloc sections. */ 12942 for (o = abfd->sections; o != NULL; o = o->next) 12943 { 12944 struct bfd_elf_section_data *esdo = elf_section_data (o); 12945 if ((o->flags & SEC_RELOC) != 0) 12946 { 12947 if (esdo->rel.hdr 12948 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) 12949 goto error_return; 12950 12951 if (esdo->rela.hdr 12952 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) 12953 goto error_return; 12954 } 12955 12956 /* _bfd_elf_compute_section_file_positions makes temporary use 12957 of target_index. Reset it. */ 12958 o->target_index = 0; 12959 12960 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them 12961 to count upwards while actually outputting the relocations. */ 12962 esdo->rel.count = 0; 12963 esdo->rela.count = 0; 12964 12965 if ((esdo->this_hdr.sh_offset == (file_ptr) -1) 12966 && !bfd_section_is_ctf (o)) 12967 { 12968 /* Cache the section contents so that they can be compressed 12969 later. Use bfd_malloc since it will be freed by 12970 bfd_compress_section_contents. */ 12971 unsigned char *contents = esdo->this_hdr.contents; 12972 if (contents != NULL) 12973 abort (); 12974 contents 12975 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); 12976 if (contents == NULL) 12977 goto error_return; 12978 esdo->this_hdr.contents = contents; 12979 } 12980 } 12981 12982 /* We have now assigned file positions for all the sections except .symtab, 12983 .strtab, and non-loaded reloc and compressed debugging sections. We start 12984 the .symtab section at the current file position, and write directly to it. 12985 We build the .strtab section in memory. */ 12986 abfd->symcount = 0; 12987 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12988 /* sh_name is set in prep_headers. */ 12989 symtab_hdr->sh_type = SHT_SYMTAB; 12990 /* sh_flags, sh_addr and sh_size all start off zero. */ 12991 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 12992 /* sh_link is set in assign_section_numbers. */ 12993 /* sh_info is set below. */ 12994 /* sh_offset is set just below. */ 12995 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 12996 12997 if (max_sym_count < 20) 12998 max_sym_count = 20; 12999 htab->strtabsize = max_sym_count; 13000 amt = max_sym_count * sizeof (struct elf_sym_strtab); 13001 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); 13002 if (htab->strtab == NULL) 13003 goto error_return; 13004 /* The real buffer will be allocated in elf_link_swap_symbols_out. */ 13005 flinfo.symshndxbuf 13006 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) 13007 ? (Elf_External_Sym_Shndx *) -1 : NULL); 13008 13009 if (info->strip != strip_all || emit_relocs) 13010 { 13011 file_ptr off = elf_next_file_pos (abfd); 13012 13013 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, true, 0); 13014 13015 /* Note that at this point elf_next_file_pos (abfd) is 13016 incorrect. We do not yet know the size of the .symtab section. 13017 We correct next_file_pos below, after we do know the size. */ 13018 13019 /* Start writing out the symbol table. The first symbol is always a 13020 dummy symbol. */ 13021 elfsym.st_value = 0; 13022 elfsym.st_size = 0; 13023 elfsym.st_info = 0; 13024 elfsym.st_other = 0; 13025 elfsym.st_shndx = SHN_UNDEF; 13026 elfsym.st_target_internal = 0; 13027 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, 13028 bfd_und_section_ptr, NULL) != 1) 13029 goto error_return; 13030 13031 /* Output a symbol for each section if asked or they are used for 13032 relocs. These symbols usually have no names. We store the 13033 index of each one in the index field of the section, so that 13034 we can find it again when outputting relocs. */ 13035 13036 if (bfd_keep_unused_section_symbols (abfd) || emit_relocs) 13037 { 13038 bool name_local_sections 13039 = (bed->elf_backend_name_local_section_symbols 13040 && bed->elf_backend_name_local_section_symbols (abfd)); 13041 const char *name = NULL; 13042 13043 elfsym.st_size = 0; 13044 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 13045 elfsym.st_other = 0; 13046 elfsym.st_value = 0; 13047 elfsym.st_target_internal = 0; 13048 for (i = 1; i < elf_numsections (abfd); i++) 13049 { 13050 o = bfd_section_from_elf_index (abfd, i); 13051 if (o != NULL) 13052 { 13053 o->target_index = bfd_get_symcount (abfd); 13054 elfsym.st_shndx = i; 13055 if (!bfd_link_relocatable (info)) 13056 elfsym.st_value = o->vma; 13057 if (name_local_sections) 13058 name = o->name; 13059 if (elf_link_output_symstrtab (&flinfo, name, &elfsym, o, 13060 NULL) != 1) 13061 goto error_return; 13062 } 13063 } 13064 } 13065 } 13066 13067 /* On some targets like Irix 5 the symbol split between local and global 13068 ones recorded in the sh_info field needs to be done between section 13069 and all other symbols. */ 13070 if (bed->elf_backend_elfsym_local_is_section 13071 && bed->elf_backend_elfsym_local_is_section (abfd)) 13072 symtab_hdr->sh_info = bfd_get_symcount (abfd); 13073 13074 /* Allocate some memory to hold information read in from the input 13075 files. */ 13076 if (max_contents_size != 0) 13077 { 13078 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); 13079 if (flinfo.contents == NULL) 13080 goto error_return; 13081 } 13082 13083 if (max_external_reloc_size != 0) 13084 { 13085 flinfo.external_relocs = bfd_malloc (max_external_reloc_size); 13086 if (flinfo.external_relocs == NULL) 13087 goto error_return; 13088 } 13089 13090 if (max_internal_reloc_count != 0) 13091 { 13092 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); 13093 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); 13094 if (flinfo.internal_relocs == NULL) 13095 goto error_return; 13096 } 13097 13098 if (max_sym_count != 0) 13099 { 13100 amt = max_sym_count * bed->s->sizeof_sym; 13101 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); 13102 if (flinfo.external_syms == NULL) 13103 goto error_return; 13104 13105 amt = max_sym_count * sizeof (Elf_Internal_Sym); 13106 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); 13107 if (flinfo.internal_syms == NULL) 13108 goto error_return; 13109 13110 amt = max_sym_count * sizeof (long); 13111 flinfo.indices = (long int *) bfd_malloc (amt); 13112 if (flinfo.indices == NULL) 13113 goto error_return; 13114 13115 amt = max_sym_count * sizeof (asection *); 13116 flinfo.sections = (asection **) bfd_malloc (amt); 13117 if (flinfo.sections == NULL) 13118 goto error_return; 13119 } 13120 13121 if (max_sym_shndx_count != 0) 13122 { 13123 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); 13124 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 13125 if (flinfo.locsym_shndx == NULL) 13126 goto error_return; 13127 } 13128 13129 if (htab->tls_sec) 13130 { 13131 bfd_vma base, end = 0; /* Both bytes. */ 13132 asection *sec; 13133 13134 for (sec = htab->tls_sec; 13135 sec && (sec->flags & SEC_THREAD_LOCAL); 13136 sec = sec->next) 13137 { 13138 bfd_size_type size = sec->size; 13139 unsigned int opb = bfd_octets_per_byte (abfd, sec); 13140 13141 if (size == 0 13142 && (sec->flags & SEC_HAS_CONTENTS) == 0) 13143 { 13144 struct bfd_link_order *ord = sec->map_tail.link_order; 13145 13146 if (ord != NULL) 13147 size = ord->offset * opb + ord->size; 13148 } 13149 end = sec->vma + size / opb; 13150 } 13151 base = htab->tls_sec->vma; 13152 /* Only align end of TLS section if static TLS doesn't have special 13153 alignment requirements. */ 13154 if (bed->static_tls_alignment == 1) 13155 end = align_power (end, htab->tls_sec->alignment_power); 13156 htab->tls_size = end - base; 13157 } 13158 13159 if (!_bfd_elf_fixup_eh_frame_hdr (info)) 13160 return false; 13161 13162 /* Finish relative relocations here after regular symbol processing 13163 is finished if DT_RELR is enabled. */ 13164 if (info->enable_dt_relr 13165 && bed->finish_relative_relocs 13166 && !bed->finish_relative_relocs (info)) 13167 info->callbacks->fatal 13168 (_("%P: %pB: failed to finish relative relocations\n"), abfd); 13169 13170 /* Since ELF permits relocations to be against local symbols, we 13171 must have the local symbols available when we do the relocations. 13172 Since we would rather only read the local symbols once, and we 13173 would rather not keep them in memory, we handle all the 13174 relocations for a single input file at the same time. 13175 13176 Unfortunately, there is no way to know the total number of local 13177 symbols until we have seen all of them, and the local symbol 13178 indices precede the global symbol indices. This means that when 13179 we are generating relocatable output, and we see a reloc against 13180 a global symbol, we can not know the symbol index until we have 13181 finished examining all the local symbols to see which ones we are 13182 going to output. To deal with this, we keep the relocations in 13183 memory, and don't output them until the end of the link. This is 13184 an unfortunate waste of memory, but I don't see a good way around 13185 it. Fortunately, it only happens when performing a relocatable 13186 link, which is not the common case. FIXME: If keep_memory is set 13187 we could write the relocs out and then read them again; I don't 13188 know how bad the memory loss will be. */ 13189 13190 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13191 sub->output_has_begun = false; 13192 for (o = abfd->sections; o != NULL; o = o->next) 13193 { 13194 for (p = o->map_head.link_order; p != NULL; p = p->next) 13195 { 13196 if (p->type == bfd_indirect_link_order 13197 && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) 13198 == bfd_target_elf_flavour) 13199 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) 13200 { 13201 if (! sub->output_has_begun) 13202 { 13203 if (! elf_link_input_bfd (&flinfo, sub)) 13204 goto error_return; 13205 sub->output_has_begun = true; 13206 } 13207 } 13208 else if (p->type == bfd_section_reloc_link_order 13209 || p->type == bfd_symbol_reloc_link_order) 13210 { 13211 if (! elf_reloc_link_order (abfd, info, o, p)) 13212 goto error_return; 13213 } 13214 else 13215 { 13216 if (! _bfd_default_link_order (abfd, info, o, p)) 13217 { 13218 if (p->type == bfd_indirect_link_order 13219 && (bfd_get_flavour (sub) 13220 == bfd_target_elf_flavour) 13221 && (elf_elfheader (sub)->e_ident[EI_CLASS] 13222 != bed->s->elfclass)) 13223 { 13224 const char *iclass, *oclass; 13225 13226 switch (bed->s->elfclass) 13227 { 13228 case ELFCLASS64: oclass = "ELFCLASS64"; break; 13229 case ELFCLASS32: oclass = "ELFCLASS32"; break; 13230 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; 13231 default: abort (); 13232 } 13233 13234 switch (elf_elfheader (sub)->e_ident[EI_CLASS]) 13235 { 13236 case ELFCLASS64: iclass = "ELFCLASS64"; break; 13237 case ELFCLASS32: iclass = "ELFCLASS32"; break; 13238 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; 13239 default: abort (); 13240 } 13241 13242 bfd_set_error (bfd_error_wrong_format); 13243 _bfd_error_handler 13244 /* xgettext:c-format */ 13245 (_("%pB: file class %s incompatible with %s"), 13246 sub, iclass, oclass); 13247 } 13248 13249 goto error_return; 13250 } 13251 } 13252 } 13253 } 13254 13255 /* Free symbol buffer if needed. */ 13256 if (!info->reduce_memory_overheads) 13257 { 13258 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13259 if (bfd_get_flavour (sub) == bfd_target_elf_flavour) 13260 { 13261 free (elf_tdata (sub)->symbuf); 13262 elf_tdata (sub)->symbuf = NULL; 13263 } 13264 } 13265 13266 /* Output any global symbols that got converted to local in a 13267 version script or due to symbol visibility. We do this in a 13268 separate step since ELF requires all local symbols to appear 13269 prior to any global symbols. FIXME: We should only do this if 13270 some global symbols were, in fact, converted to become local. 13271 FIXME: Will this work correctly with the Irix 5 linker? */ 13272 eoinfo.failed = false; 13273 eoinfo.flinfo = &flinfo; 13274 eoinfo.localsyms = true; 13275 eoinfo.file_sym_done = false; 13276 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 13277 if (eoinfo.failed) 13278 goto error_return; 13279 13280 /* If backend needs to output some local symbols not present in the hash 13281 table, do it now. */ 13282 if (bed->elf_backend_output_arch_local_syms) 13283 { 13284 if (! ((*bed->elf_backend_output_arch_local_syms) 13285 (abfd, info, &flinfo, elf_link_output_symstrtab))) 13286 goto error_return; 13287 } 13288 13289 /* That wrote out all the local symbols. Finish up the symbol table 13290 with the global symbols. Even if we want to strip everything we 13291 can, we still need to deal with those global symbols that got 13292 converted to local in a version script. */ 13293 13294 /* The sh_info field records the index of the first non local symbol. */ 13295 if (!symtab_hdr->sh_info) 13296 symtab_hdr->sh_info = bfd_get_symcount (abfd); 13297 13298 if (dynamic 13299 && htab->dynsym != NULL 13300 && htab->dynsym->output_section != bfd_abs_section_ptr) 13301 { 13302 Elf_Internal_Sym sym; 13303 bfd_byte *dynsym = htab->dynsym->contents; 13304 13305 o = htab->dynsym->output_section; 13306 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; 13307 13308 /* Write out the section symbols for the output sections. */ 13309 if (bfd_link_pic (info) 13310 || htab->is_relocatable_executable) 13311 { 13312 asection *s; 13313 13314 sym.st_size = 0; 13315 sym.st_name = 0; 13316 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 13317 sym.st_other = 0; 13318 sym.st_target_internal = 0; 13319 13320 for (s = abfd->sections; s != NULL; s = s->next) 13321 { 13322 int indx; 13323 bfd_byte *dest; 13324 long dynindx; 13325 13326 dynindx = elf_section_data (s)->dynindx; 13327 if (dynindx <= 0) 13328 continue; 13329 indx = elf_section_data (s)->this_idx; 13330 BFD_ASSERT (indx > 0); 13331 sym.st_shndx = indx; 13332 if (! check_dynsym (abfd, &sym)) 13333 goto error_return; 13334 sym.st_value = s->vma; 13335 dest = dynsym + dynindx * bed->s->sizeof_sym; 13336 13337 /* Inform the linker of the addition of this symbol. */ 13338 13339 if (info->callbacks->ctf_new_dynsym) 13340 info->callbacks->ctf_new_dynsym (dynindx, &sym); 13341 13342 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 13343 } 13344 } 13345 13346 /* Write out the local dynsyms. */ 13347 if (htab->dynlocal) 13348 { 13349 struct elf_link_local_dynamic_entry *e; 13350 for (e = htab->dynlocal; e ; e = e->next) 13351 { 13352 asection *s; 13353 bfd_byte *dest; 13354 13355 /* Copy the internal symbol and turn off visibility. 13356 Note that we saved a word of storage and overwrote 13357 the original st_name with the dynstr_index. */ 13358 sym = e->isym; 13359 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 13360 sym.st_shndx = SHN_UNDEF; 13361 13362 s = bfd_section_from_elf_index (e->input_bfd, 13363 e->isym.st_shndx); 13364 if (s != NULL 13365 && s->output_section != NULL 13366 && elf_section_data (s->output_section) != NULL) 13367 { 13368 sym.st_shndx = 13369 elf_section_data (s->output_section)->this_idx; 13370 if (! check_dynsym (abfd, &sym)) 13371 goto error_return; 13372 sym.st_value = (s->output_section->vma 13373 + s->output_offset 13374 + e->isym.st_value); 13375 } 13376 13377 /* Inform the linker of the addition of this symbol. */ 13378 13379 if (info->callbacks->ctf_new_dynsym) 13380 info->callbacks->ctf_new_dynsym (e->dynindx, &sym); 13381 13382 dest = dynsym + e->dynindx * bed->s->sizeof_sym; 13383 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 13384 } 13385 } 13386 } 13387 13388 /* We get the global symbols from the hash table. */ 13389 eoinfo.failed = false; 13390 eoinfo.localsyms = false; 13391 eoinfo.flinfo = &flinfo; 13392 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 13393 if (eoinfo.failed) 13394 goto error_return; 13395 13396 /* If backend needs to output some symbols not present in the hash 13397 table, do it now. */ 13398 if (bed->elf_backend_output_arch_syms 13399 && (info->strip != strip_all || emit_relocs)) 13400 { 13401 if (! ((*bed->elf_backend_output_arch_syms) 13402 (abfd, info, &flinfo, elf_link_output_symstrtab))) 13403 goto error_return; 13404 } 13405 13406 /* Finalize the .strtab section. */ 13407 _bfd_elf_strtab_finalize (flinfo.symstrtab); 13408 13409 /* Swap out the .strtab section. */ 13410 if (!elf_link_swap_symbols_out (&flinfo)) 13411 goto error_return; 13412 free (htab->strtab); 13413 htab->strtab = NULL; 13414 13415 /* Now we know the size of the symtab section. */ 13416 if (bfd_get_symcount (abfd) > 0) 13417 { 13418 /* Finish up and write out the symbol string table (.strtab) 13419 section. */ 13420 Elf_Internal_Shdr *symstrtab_hdr = NULL; 13421 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; 13422 13423 if (elf_symtab_shndx_list (abfd)) 13424 { 13425 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 13426 13427 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) 13428 { 13429 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 13430 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 13431 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 13432 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); 13433 symtab_shndx_hdr->sh_size = amt; 13434 13435 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, 13436 off, true, 0); 13437 13438 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 13439 || (bfd_write (flinfo.symshndxbuf, amt, abfd) != amt)) 13440 goto error_return; 13441 } 13442 } 13443 13444 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 13445 /* sh_name was set in prep_headers. */ 13446 symstrtab_hdr->sh_type = SHT_STRTAB; 13447 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 13448 symstrtab_hdr->sh_addr = 0; 13449 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); 13450 symstrtab_hdr->sh_entsize = 0; 13451 symstrtab_hdr->sh_link = 0; 13452 symstrtab_hdr->sh_info = 0; 13453 /* sh_offset is set just below. */ 13454 symstrtab_hdr->sh_addralign = 1; 13455 13456 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, 13457 off, true, 0); 13458 elf_next_file_pos (abfd) = off; 13459 13460 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 13461 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) 13462 goto error_return; 13463 } 13464 13465 if (info->out_implib_bfd && !elf_output_implib (abfd, info)) 13466 { 13467 _bfd_error_handler (_("%pB: failed to generate import library"), 13468 info->out_implib_bfd); 13469 goto error_return; 13470 } 13471 13472 /* Adjust the relocs to have the correct symbol indices. */ 13473 for (o = abfd->sections; o != NULL; o = o->next) 13474 { 13475 struct bfd_elf_section_data *esdo = elf_section_data (o); 13476 bool sort; 13477 13478 if ((o->flags & SEC_RELOC) == 0) 13479 continue; 13480 13481 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); 13482 if (esdo->rel.hdr != NULL 13483 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) 13484 goto error_return; 13485 if (esdo->rela.hdr != NULL 13486 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) 13487 goto error_return; 13488 13489 /* Set the reloc_count field to 0 to prevent write_relocs from 13490 trying to swap the relocs out itself. */ 13491 o->reloc_count = 0; 13492 } 13493 13494 relativecount = 0; 13495 if (dynamic && info->combreloc && dynobj != NULL) 13496 relativecount = elf_link_sort_relocs (abfd, info, &reldyn); 13497 13498 relr_entsize = 0; 13499 if (htab->srelrdyn != NULL 13500 && htab->srelrdyn->output_section != NULL 13501 && htab->srelrdyn->size != 0) 13502 { 13503 asection *s = htab->srelrdyn->output_section; 13504 relr_entsize = elf_section_data (s)->this_hdr.sh_entsize; 13505 if (relr_entsize == 0) 13506 { 13507 relr_entsize = bed->s->arch_size / 8; 13508 elf_section_data (s)->this_hdr.sh_entsize = relr_entsize; 13509 } 13510 } 13511 13512 /* If we are linking against a dynamic object, or generating a 13513 shared library, finish up the dynamic linking information. */ 13514 if (dynamic) 13515 { 13516 bfd_byte *dyncon, *dynconend; 13517 13518 /* Fix up .dynamic entries. */ 13519 o = htab->dynamic; 13520 BFD_ASSERT (o != NULL); 13521 13522 dyncon = o->contents; 13523 dynconend = PTR_ADD (o->contents, o->size); 13524 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 13525 { 13526 Elf_Internal_Dyn dyn; 13527 const char *name; 13528 unsigned int type; 13529 bfd_size_type sh_size; 13530 bfd_vma sh_addr; 13531 13532 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 13533 13534 switch (dyn.d_tag) 13535 { 13536 default: 13537 continue; 13538 case DT_NULL: 13539 if (relativecount != 0) 13540 { 13541 switch (elf_section_data (reldyn)->this_hdr.sh_type) 13542 { 13543 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; 13544 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; 13545 } 13546 if (dyn.d_tag != DT_NULL 13547 && dynconend - dyncon >= bed->s->sizeof_dyn) 13548 { 13549 dyn.d_un.d_val = relativecount; 13550 relativecount = 0; 13551 break; 13552 } 13553 relativecount = 0; 13554 } 13555 if (relr_entsize != 0) 13556 { 13557 if (dynconend - dyncon >= 3 * bed->s->sizeof_dyn) 13558 { 13559 asection *s = htab->srelrdyn; 13560 dyn.d_tag = DT_RELR; 13561 dyn.d_un.d_ptr 13562 = s->output_section->vma + s->output_offset; 13563 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 13564 dyncon += bed->s->sizeof_dyn; 13565 13566 dyn.d_tag = DT_RELRSZ; 13567 dyn.d_un.d_val = s->size; 13568 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 13569 dyncon += bed->s->sizeof_dyn; 13570 13571 dyn.d_tag = DT_RELRENT; 13572 dyn.d_un.d_val = relr_entsize; 13573 relr_entsize = 0; 13574 break; 13575 } 13576 relr_entsize = 0; 13577 } 13578 continue; 13579 13580 case DT_INIT: 13581 name = info->init_function; 13582 goto get_sym; 13583 case DT_FINI: 13584 name = info->fini_function; 13585 get_sym: 13586 { 13587 struct elf_link_hash_entry *h; 13588 13589 h = elf_link_hash_lookup (htab, name, false, false, true); 13590 if (h != NULL 13591 && (h->root.type == bfd_link_hash_defined 13592 || h->root.type == bfd_link_hash_defweak)) 13593 { 13594 dyn.d_un.d_ptr = h->root.u.def.value; 13595 o = h->root.u.def.section; 13596 if (o->output_section != NULL) 13597 dyn.d_un.d_ptr += (o->output_section->vma 13598 + o->output_offset); 13599 else 13600 { 13601 /* The symbol is imported from another shared 13602 library and does not apply to this one. */ 13603 dyn.d_un.d_ptr = 0; 13604 } 13605 break; 13606 } 13607 } 13608 continue; 13609 13610 case DT_PREINIT_ARRAYSZ: 13611 name = ".preinit_array"; 13612 goto get_out_size; 13613 case DT_INIT_ARRAYSZ: 13614 name = ".init_array"; 13615 goto get_out_size; 13616 case DT_FINI_ARRAYSZ: 13617 name = ".fini_array"; 13618 get_out_size: 13619 o = bfd_get_section_by_name (abfd, name); 13620 if (o == NULL) 13621 { 13622 _bfd_error_handler 13623 (_("could not find section %s"), name); 13624 goto error_return; 13625 } 13626 if (o->size == 0) 13627 _bfd_error_handler 13628 (_("warning: %s section has zero size"), name); 13629 dyn.d_un.d_val = o->size; 13630 break; 13631 13632 case DT_PREINIT_ARRAY: 13633 name = ".preinit_array"; 13634 goto get_out_vma; 13635 case DT_INIT_ARRAY: 13636 name = ".init_array"; 13637 goto get_out_vma; 13638 case DT_FINI_ARRAY: 13639 name = ".fini_array"; 13640 get_out_vma: 13641 o = bfd_get_section_by_name (abfd, name); 13642 goto do_vma; 13643 13644 case DT_HASH: 13645 name = ".hash"; 13646 goto get_vma; 13647 case DT_GNU_HASH: 13648 name = ".gnu.hash"; 13649 goto get_vma; 13650 case DT_STRTAB: 13651 name = ".dynstr"; 13652 goto get_vma; 13653 case DT_SYMTAB: 13654 name = ".dynsym"; 13655 goto get_vma; 13656 case DT_VERDEF: 13657 name = ".gnu.version_d"; 13658 goto get_vma; 13659 case DT_VERNEED: 13660 name = ".gnu.version_r"; 13661 goto get_vma; 13662 case DT_VERSYM: 13663 name = ".gnu.version"; 13664 get_vma: 13665 o = bfd_get_linker_section (dynobj, name); 13666 do_vma: 13667 if (o == NULL || bfd_is_abs_section (o->output_section)) 13668 { 13669 _bfd_error_handler 13670 (_("could not find section %s"), name); 13671 goto error_return; 13672 } 13673 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) 13674 { 13675 _bfd_error_handler 13676 (_("warning: section '%s' is being made into a note"), name); 13677 bfd_set_error (bfd_error_nonrepresentable_section); 13678 goto error_return; 13679 } 13680 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; 13681 break; 13682 13683 case DT_REL: 13684 case DT_RELA: 13685 case DT_RELSZ: 13686 case DT_RELASZ: 13687 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) 13688 type = SHT_REL; 13689 else 13690 type = SHT_RELA; 13691 sh_size = 0; 13692 sh_addr = 0; 13693 for (i = 1; i < elf_numsections (abfd); i++) 13694 { 13695 Elf_Internal_Shdr *hdr; 13696 13697 hdr = elf_elfsections (abfd)[i]; 13698 if (hdr->sh_type == type 13699 && (hdr->sh_flags & SHF_ALLOC) != 0) 13700 { 13701 sh_size += hdr->sh_size; 13702 if (sh_addr == 0 13703 || sh_addr > hdr->sh_addr) 13704 sh_addr = hdr->sh_addr; 13705 } 13706 } 13707 13708 if (bed->dtrel_excludes_plt && htab->srelplt != NULL) 13709 { 13710 unsigned int opb = bfd_octets_per_byte (abfd, o); 13711 13712 /* Don't count procedure linkage table relocs in the 13713 overall reloc count. */ 13714 sh_size -= htab->srelplt->size; 13715 if (sh_size == 0) 13716 /* If the size is zero, make the address zero too. 13717 This is to avoid a glibc bug. If the backend 13718 emits DT_RELA/DT_RELASZ even when DT_RELASZ is 13719 zero, then we'll put DT_RELA at the end of 13720 DT_JMPREL. glibc will interpret the end of 13721 DT_RELA matching the end of DT_JMPREL as the 13722 case where DT_RELA includes DT_JMPREL, and for 13723 LD_BIND_NOW will decide that processing DT_RELA 13724 will process the PLT relocs too. Net result: 13725 No PLT relocs applied. */ 13726 sh_addr = 0; 13727 13728 /* If .rela.plt is the first .rela section, exclude 13729 it from DT_RELA. */ 13730 else if (sh_addr == (htab->srelplt->output_section->vma 13731 + htab->srelplt->output_offset) * opb) 13732 sh_addr += htab->srelplt->size; 13733 } 13734 13735 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) 13736 dyn.d_un.d_val = sh_size; 13737 else 13738 dyn.d_un.d_ptr = sh_addr; 13739 break; 13740 } 13741 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 13742 } 13743 } 13744 13745 /* If we have created any dynamic sections, then output them. */ 13746 if (dynobj != NULL) 13747 { 13748 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) 13749 goto error_return; 13750 13751 /* Check for DT_TEXTREL (late, in case the backend removes it). */ 13752 if (bfd_link_textrel_check (info) 13753 && (o = htab->dynamic) != NULL 13754 && o->size != 0) 13755 { 13756 bfd_byte *dyncon, *dynconend; 13757 13758 dyncon = o->contents; 13759 dynconend = o->contents + o->size; 13760 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 13761 { 13762 Elf_Internal_Dyn dyn; 13763 13764 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 13765 13766 if (dyn.d_tag == DT_TEXTREL) 13767 { 13768 if (info->textrel_check == textrel_check_error) 13769 info->callbacks->einfo 13770 (_("%P%X: read-only segment has dynamic relocations\n")); 13771 else if (bfd_link_dll (info)) 13772 info->callbacks->einfo 13773 (_("%P: warning: creating DT_TEXTREL in a shared object\n")); 13774 else if (bfd_link_pde (info)) 13775 info->callbacks->einfo 13776 (_("%P: warning: creating DT_TEXTREL in a PDE\n")); 13777 else 13778 info->callbacks->einfo 13779 (_("%P: warning: creating DT_TEXTREL in a PIE\n")); 13780 break; 13781 } 13782 } 13783 } 13784 13785 for (o = dynobj->sections; o != NULL; o = o->next) 13786 { 13787 if ((o->flags & SEC_HAS_CONTENTS) == 0 13788 || o->size == 0 13789 || o->output_section == bfd_abs_section_ptr) 13790 continue; 13791 if ((o->flags & SEC_LINKER_CREATED) == 0) 13792 { 13793 /* At this point, we are only interested in sections 13794 created by _bfd_elf_link_create_dynamic_sections. */ 13795 continue; 13796 } 13797 if (htab->stab_info.stabstr == o) 13798 continue; 13799 if (htab->eh_info.hdr_sec == o) 13800 continue; 13801 if (strcmp (o->name, ".dynstr") != 0) 13802 { 13803 bfd_size_type octets = ((file_ptr) o->output_offset 13804 * bfd_octets_per_byte (abfd, o)); 13805 if (!bfd_set_section_contents (abfd, o->output_section, 13806 o->contents, octets, o->size)) 13807 goto error_return; 13808 } 13809 else 13810 { 13811 /* The contents of the .dynstr section are actually in a 13812 stringtab. */ 13813 file_ptr off; 13814 13815 off = elf_section_data (o->output_section)->this_hdr.sh_offset; 13816 if (bfd_seek (abfd, off, SEEK_SET) != 0 13817 || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) 13818 goto error_return; 13819 } 13820 } 13821 } 13822 13823 if (!info->resolve_section_groups) 13824 { 13825 bool failed = false; 13826 13827 BFD_ASSERT (bfd_link_relocatable (info)); 13828 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 13829 if (failed) 13830 goto error_return; 13831 } 13832 13833 /* If we have optimized stabs strings, output them. */ 13834 if (htab->stab_info.stabstr != NULL) 13835 { 13836 if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) 13837 goto error_return; 13838 } 13839 13840 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) 13841 goto error_return; 13842 13843 if (! _bfd_elf_write_section_sframe (abfd, info)) 13844 goto error_return; 13845 13846 if (! _bfd_elf_write_section_build_attributes (abfd, info)) 13847 goto error_ret2; 13848 13849 if (info->callbacks->emit_ctf) 13850 info->callbacks->emit_ctf (); 13851 13852 elf_final_link_free (abfd, &flinfo); 13853 13854 if (info->unique_symbol) 13855 bfd_hash_table_free (&flinfo.local_hash_table); 13856 return true; 13857 13858 error_return: 13859 free (htab->strtab); 13860 htab->strtab = NULL; 13861 elf_final_link_free (abfd, &flinfo); 13862 error_ret2: 13863 if (info->unique_symbol) 13864 bfd_hash_table_free (&flinfo.local_hash_table); 13865 return false; 13866 } 13867 13868 /* Initialize COOKIE for input bfd ABFD. */ 13870 13871 static bool 13872 init_reloc_cookie (struct elf_reloc_cookie *cookie, 13873 struct bfd_link_info *info, bfd *abfd, 13874 bool keep_memory) 13875 { 13876 Elf_Internal_Shdr *symtab_hdr; 13877 const struct elf_backend_data *bed; 13878 13879 bed = get_elf_backend_data (abfd); 13880 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 13881 13882 cookie->abfd = abfd; 13883 cookie->sym_hashes = elf_sym_hashes (abfd); 13884 cookie->bad_symtab = elf_bad_symtab (abfd); 13885 if (cookie->bad_symtab) 13886 { 13887 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 13888 cookie->extsymoff = 0; 13889 } 13890 else 13891 { 13892 cookie->locsymcount = symtab_hdr->sh_info; 13893 cookie->extsymoff = symtab_hdr->sh_info; 13894 } 13895 13896 if (bed->s->arch_size == 32) 13897 cookie->r_sym_shift = 8; 13898 else 13899 cookie->r_sym_shift = 32; 13900 13901 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; 13902 if (cookie->locsyms == NULL && cookie->locsymcount != 0) 13903 { 13904 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, 13905 cookie->locsymcount, 0, 13906 NULL, NULL, NULL); 13907 if (cookie->locsyms == NULL) 13908 { 13909 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); 13910 return false; 13911 } 13912 if (keep_memory || _bfd_elf_link_keep_memory (info)) 13913 { 13914 symtab_hdr->contents = (bfd_byte *) cookie->locsyms; 13915 info->cache_size += (cookie->locsymcount 13916 * sizeof (Elf_Internal_Sym)); 13917 } 13918 } 13919 return true; 13920 } 13921 13922 /* Free the memory allocated by init_reloc_cookie, if appropriate. */ 13923 13924 static void 13925 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) 13926 { 13927 Elf_Internal_Shdr *symtab_hdr; 13928 13929 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 13930 if (symtab_hdr->contents != (unsigned char *) cookie->locsyms) 13931 free (cookie->locsyms); 13932 } 13933 13934 /* Initialize the relocation information in COOKIE for input section SEC 13935 of input bfd ABFD. */ 13936 13937 static bool 13938 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 13939 struct bfd_link_info *info, bfd *abfd, 13940 asection *sec, bool keep_memory) 13941 { 13942 if (sec->reloc_count == 0) 13943 { 13944 cookie->rels = NULL; 13945 cookie->relend = NULL; 13946 } 13947 else 13948 { 13949 cookie->rels = _bfd_elf_link_info_read_relocs 13950 (abfd, info, sec, NULL, NULL, 13951 keep_memory || _bfd_elf_link_keep_memory (info)); 13952 if (cookie->rels == NULL) 13953 return false; 13954 cookie->rel = cookie->rels; 13955 cookie->relend = cookie->rels + sec->reloc_count; 13956 } 13957 cookie->rel = cookie->rels; 13958 return true; 13959 } 13960 13961 /* Free the memory allocated by init_reloc_cookie_rels, 13962 if appropriate. */ 13963 13964 static void 13965 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 13966 asection *sec) 13967 { 13968 if (elf_section_data (sec)->relocs != cookie->rels) 13969 free (cookie->rels); 13970 } 13971 13972 /* Initialize the whole of COOKIE for input section SEC. */ 13973 13974 static bool 13975 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 13976 struct bfd_link_info *info, 13977 asection *sec, bool keep_memory) 13978 { 13979 if (!init_reloc_cookie (cookie, info, sec->owner, keep_memory)) 13980 goto error1; 13981 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec, 13982 keep_memory)) 13983 goto error2; 13984 return true; 13985 13986 error2: 13987 fini_reloc_cookie (cookie, sec->owner); 13988 error1: 13989 return false; 13990 } 13991 13992 /* Free the memory allocated by init_reloc_cookie_for_section, 13993 if appropriate. */ 13994 13995 static void 13996 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 13997 asection *sec) 13998 { 13999 fini_reloc_cookie_rels (cookie, sec); 14000 fini_reloc_cookie (cookie, sec->owner); 14001 } 14002 14003 /* Garbage collect unused sections. */ 14005 14006 /* Default gc_mark_hook. */ 14007 14008 asection * 14009 _bfd_elf_gc_mark_hook (asection *sec, 14010 struct bfd_link_info *info ATTRIBUTE_UNUSED, 14011 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 14012 struct elf_link_hash_entry *h, 14013 Elf_Internal_Sym *sym) 14014 { 14015 if (h == NULL) 14016 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 14017 14018 switch (h->root.type) 14019 { 14020 case bfd_link_hash_defined: 14021 case bfd_link_hash_defweak: 14022 return h->root.u.def.section; 14023 14024 case bfd_link_hash_common: 14025 return h->root.u.c.p->section; 14026 14027 default: 14028 return NULL; 14029 } 14030 } 14031 14032 /* Return the debug definition section. */ 14033 14034 static asection * 14035 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, 14036 struct bfd_link_info *info ATTRIBUTE_UNUSED, 14037 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 14038 struct elf_link_hash_entry *h, 14039 Elf_Internal_Sym *sym) 14040 { 14041 if (h != NULL) 14042 { 14043 /* Return the global debug definition section. */ 14044 if ((h->root.type == bfd_link_hash_defined 14045 || h->root.type == bfd_link_hash_defweak) 14046 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) 14047 return h->root.u.def.section; 14048 } 14049 else 14050 { 14051 /* Return the local debug definition section. */ 14052 asection *isec = bfd_section_from_elf_index (sec->owner, 14053 sym->st_shndx); 14054 if (isec != NULL && (isec->flags & SEC_DEBUGGING) != 0) 14055 return isec; 14056 } 14057 14058 return NULL; 14059 } 14060 14061 /* COOKIE->rel describes a relocation against section SEC, which is 14062 a section we've decided to keep. Return the section that contains 14063 the relocation symbol, or NULL if no section contains it. */ 14064 14065 asection * 14066 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, 14067 elf_gc_mark_hook_fn gc_mark_hook, 14068 struct elf_reloc_cookie *cookie, 14069 bool *start_stop) 14070 { 14071 unsigned long r_symndx; 14072 struct elf_link_hash_entry *h, *hw; 14073 14074 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; 14075 if (r_symndx == STN_UNDEF) 14076 return NULL; 14077 14078 h = get_ext_sym_hash_from_cookie (cookie, r_symndx); 14079 if (h == NULL) 14080 { 14081 /* A corrupt input file can lead to a situation where the index 14082 does not reference either a local or an external symbol. */ 14083 if (r_symndx >= cookie->locsymcount) 14084 return NULL; 14085 14086 return (*gc_mark_hook) (sec, info, cookie->rel, NULL, 14087 &cookie->locsyms[r_symndx]); 14088 } 14089 14090 bool was_marked = h->mark; 14091 14092 h->mark = 1; 14093 /* Keep all aliases of the symbol too. If an object symbol 14094 needs to be copied into .dynbss then all of its aliases 14095 should be present as dynamic symbols, not just the one used 14096 on the copy relocation. */ 14097 hw = h; 14098 while (hw->is_weakalias) 14099 { 14100 hw = hw->u.alias; 14101 hw->mark = 1; 14102 } 14103 14104 if (!was_marked && h->start_stop && !h->root.ldscript_def) 14105 { 14106 if (info->start_stop_gc) 14107 return NULL; 14108 14109 /* To work around a glibc bug, mark XXX input sections 14110 when there is a reference to __start_XXX or __stop_XXX 14111 symbols. */ 14112 else if (start_stop != NULL) 14113 { 14114 asection *s = h->u2.start_stop_section; 14115 *start_stop = true; 14116 return s; 14117 } 14118 } 14119 14120 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); 14121 } 14122 14123 /* COOKIE->rel describes a relocation against section SEC, which is 14124 a section we've decided to keep. Mark the section that contains 14125 the relocation symbol. */ 14126 14127 bool 14128 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, 14129 asection *sec, 14130 elf_gc_mark_hook_fn gc_mark_hook, 14131 struct elf_reloc_cookie *cookie) 14132 { 14133 asection *rsec; 14134 bool start_stop = false; 14135 14136 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); 14137 while (rsec != NULL) 14138 { 14139 if (!rsec->gc_mark) 14140 { 14141 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour 14142 || (rsec->owner->flags & DYNAMIC) != 0) 14143 rsec->gc_mark = 1; 14144 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) 14145 return false; 14146 } 14147 if (!start_stop) 14148 break; 14149 rsec = bfd_get_next_section_by_name (rsec->owner, rsec); 14150 } 14151 return true; 14152 } 14153 14154 /* The mark phase of garbage collection. For a given section, mark 14155 it and any sections in this section's group, and all the sections 14156 which define symbols to which it refers. */ 14157 14158 bool 14159 _bfd_elf_gc_mark (struct bfd_link_info *info, 14160 asection *sec, 14161 elf_gc_mark_hook_fn gc_mark_hook) 14162 { 14163 bool ret; 14164 asection *group_sec, *eh_frame; 14165 14166 sec->gc_mark = 1; 14167 14168 /* Mark all the sections in the group. */ 14169 group_sec = elf_section_data (sec)->next_in_group; 14170 if (group_sec && !group_sec->gc_mark) 14171 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) 14172 return false; 14173 14174 /* Look through the section relocs. */ 14175 ret = true; 14176 eh_frame = elf_eh_frame_section (sec->owner); 14177 if ((sec->flags & SEC_RELOC) != 0 14178 && sec->reloc_count > 0 14179 && sec != eh_frame) 14180 { 14181 struct elf_reloc_cookie cookie; 14182 14183 if (!init_reloc_cookie_for_section (&cookie, info, sec, false)) 14184 ret = false; 14185 else 14186 { 14187 for (; cookie.rel < cookie.relend; cookie.rel++) 14188 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) 14189 { 14190 ret = false; 14191 break; 14192 } 14193 fini_reloc_cookie_for_section (&cookie, sec); 14194 } 14195 } 14196 14197 if (ret && eh_frame && elf_fde_list (sec)) 14198 { 14199 struct elf_reloc_cookie cookie; 14200 14201 /* NB: When --no-keep-memory is used, the symbol table and 14202 relocation info for eh_frame are freed after they are retrieved 14203 for each text section in the input object. If an input object 14204 has many text sections, the same data is retrieved and freed 14205 many times which can take a very long time. Always keep the 14206 symbol table and relocation info for eh_frame to avoid it. */ 14207 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame, 14208 true)) 14209 ret = false; 14210 else 14211 { 14212 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, 14213 gc_mark_hook, &cookie)) 14214 ret = false; 14215 fini_reloc_cookie_for_section (&cookie, eh_frame); 14216 } 14217 } 14218 14219 eh_frame = elf_section_eh_frame_entry (sec); 14220 if (ret && eh_frame && !eh_frame->gc_mark) 14221 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) 14222 ret = false; 14223 14224 return ret; 14225 } 14226 14227 /* Scan and mark sections in a special or debug section group. */ 14228 14229 static void 14230 _bfd_elf_gc_mark_debug_special_section_group (asection *grp) 14231 { 14232 /* Point to first section of section group. */ 14233 asection *ssec; 14234 /* Used to iterate the section group. */ 14235 asection *msec; 14236 14237 bool is_special_grp = true; 14238 bool is_debug_grp = true; 14239 14240 /* First scan to see if group contains any section other than debug 14241 and special section. */ 14242 ssec = msec = elf_next_in_group (grp); 14243 do 14244 { 14245 if ((msec->flags & SEC_DEBUGGING) == 0) 14246 is_debug_grp = false; 14247 14248 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) 14249 is_special_grp = false; 14250 14251 msec = elf_next_in_group (msec); 14252 } 14253 while (msec != ssec); 14254 14255 /* If this is a pure debug section group or pure special section group, 14256 keep all sections in this group. */ 14257 if (is_debug_grp || is_special_grp) 14258 { 14259 do 14260 { 14261 msec->gc_mark = 1; 14262 msec = elf_next_in_group (msec); 14263 } 14264 while (msec != ssec); 14265 } 14266 } 14267 14268 /* Keep debug and special sections. */ 14269 14270 bool 14271 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, 14272 elf_gc_mark_hook_fn mark_hook) 14273 { 14274 bfd *ibfd; 14275 14276 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 14277 { 14278 asection *isec; 14279 bool some_kept; 14280 bool debug_frag_seen; 14281 bool has_kept_debug_info; 14282 14283 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 14284 continue; 14285 isec = ibfd->sections; 14286 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 14287 continue; 14288 14289 /* Ensure all linker created sections are kept, 14290 see if any other section is already marked, 14291 and note if we have any fragmented debug sections. */ 14292 debug_frag_seen = some_kept = has_kept_debug_info = false; 14293 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 14294 { 14295 if ((isec->flags & SEC_LINKER_CREATED) != 0) 14296 isec->gc_mark = 1; 14297 else if (isec->gc_mark 14298 && (isec->flags & SEC_ALLOC) != 0 14299 && elf_section_type (isec) != SHT_NOTE) 14300 some_kept = true; 14301 else 14302 { 14303 /* Since all sections, except for backend specific ones, 14304 have been garbage collected, call mark_hook on this 14305 section if any of its linked-to sections is marked. */ 14306 asection *linked_to_sec; 14307 for (linked_to_sec = elf_linked_to_section (isec); 14308 linked_to_sec != NULL && !linked_to_sec->linker_mark; 14309 linked_to_sec = elf_linked_to_section (linked_to_sec)) 14310 { 14311 if (linked_to_sec->gc_mark) 14312 { 14313 if (!_bfd_elf_gc_mark (info, isec, mark_hook)) 14314 return false; 14315 break; 14316 } 14317 linked_to_sec->linker_mark = 1; 14318 } 14319 for (linked_to_sec = elf_linked_to_section (isec); 14320 linked_to_sec != NULL && linked_to_sec->linker_mark; 14321 linked_to_sec = elf_linked_to_section (linked_to_sec)) 14322 linked_to_sec->linker_mark = 0; 14323 } 14324 14325 if (!debug_frag_seen 14326 && (isec->flags & SEC_DEBUGGING) 14327 && startswith (isec->name, ".debug_line.")) 14328 debug_frag_seen = true; 14329 else if (strcmp (bfd_section_name (isec), 14330 "__patchable_function_entries") == 0 14331 && elf_linked_to_section (isec) == NULL) 14332 info->callbacks->fatal (_("%P: %pB(%pA): error: " 14333 "need linked-to section " 14334 "for --gc-sections\n"), 14335 isec->owner, isec); 14336 } 14337 14338 /* If no non-note alloc section in this file will be kept, then 14339 we can toss out the debug and special sections. */ 14340 if (!some_kept) 14341 continue; 14342 14343 /* Keep debug and special sections like .comment when they are 14344 not part of a group. Also keep section groups that contain 14345 just debug sections or special sections. NB: Sections with 14346 linked-to section has been handled above. */ 14347 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 14348 { 14349 if ((isec->flags & SEC_GROUP) != 0) 14350 _bfd_elf_gc_mark_debug_special_section_group (isec); 14351 else if (((isec->flags & SEC_DEBUGGING) != 0 14352 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 14353 && elf_next_in_group (isec) == NULL 14354 && elf_linked_to_section (isec) == NULL) 14355 isec->gc_mark = 1; 14356 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) 14357 has_kept_debug_info = true; 14358 } 14359 14360 /* Look for CODE sections which are going to be discarded, 14361 and find and discard any fragmented debug sections which 14362 are associated with that code section. */ 14363 if (debug_frag_seen) 14364 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 14365 if ((isec->flags & SEC_CODE) != 0 14366 && isec->gc_mark == 0) 14367 { 14368 unsigned int ilen; 14369 asection *dsec; 14370 14371 ilen = strlen (isec->name); 14372 14373 /* Association is determined by the name of the debug 14374 section containing the name of the code section as 14375 a suffix. For example .debug_line.text.foo is a 14376 debug section associated with .text.foo. */ 14377 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) 14378 { 14379 unsigned int dlen; 14380 14381 if (dsec->gc_mark == 0 14382 || (dsec->flags & SEC_DEBUGGING) == 0) 14383 continue; 14384 14385 dlen = strlen (dsec->name); 14386 14387 if (dlen > ilen 14388 && strncmp (dsec->name + (dlen - ilen), 14389 isec->name, ilen) == 0) 14390 dsec->gc_mark = 0; 14391 } 14392 } 14393 14394 /* Mark debug sections referenced by kept debug sections. */ 14395 if (has_kept_debug_info) 14396 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 14397 if (isec->gc_mark 14398 && (isec->flags & SEC_DEBUGGING) != 0) 14399 if (!_bfd_elf_gc_mark (info, isec, 14400 elf_gc_mark_debug_section)) 14401 return false; 14402 } 14403 return true; 14404 } 14405 14406 static bool 14407 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) 14408 { 14409 bfd *sub; 14410 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14411 14412 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 14413 { 14414 asection *o; 14415 14416 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 14417 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) 14418 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 14419 continue; 14420 o = sub->sections; 14421 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 14422 continue; 14423 14424 for (o = sub->sections; o != NULL; o = o->next) 14425 { 14426 /* When any section in a section group is kept, we keep all 14427 sections in the section group. If the first member of 14428 the section group is excluded, we will also exclude the 14429 group section. */ 14430 if (o->flags & SEC_GROUP) 14431 { 14432 asection *first = elf_next_in_group (o); 14433 if (first != NULL) 14434 o->gc_mark = first->gc_mark; 14435 } 14436 14437 if (o->gc_mark) 14438 continue; 14439 14440 /* Skip sweeping sections already excluded. */ 14441 if (o->flags & SEC_EXCLUDE) 14442 continue; 14443 14444 /* Since this is early in the link process, it is simple 14445 to remove a section from the output. */ 14446 o->flags |= SEC_EXCLUDE; 14447 14448 if (info->print_gc_sections && o->size != 0) 14449 /* xgettext:c-format */ 14450 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), 14451 o, sub); 14452 } 14453 } 14454 14455 return true; 14456 } 14457 14458 /* Propagate collected vtable information. This is called through 14459 elf_link_hash_traverse. */ 14460 14461 static bool 14462 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) 14463 { 14464 /* Those that are not vtables. */ 14465 if (h->start_stop 14466 || h->u2.vtable == NULL 14467 || h->u2.vtable->parent == NULL) 14468 return true; 14469 14470 /* Those vtables that do not have parents, we cannot merge. */ 14471 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) 14472 return true; 14473 14474 /* If we've already been done, exit. */ 14475 if (h->u2.vtable->used && h->u2.vtable->used[-1]) 14476 return true; 14477 14478 /* Make sure the parent's table is up to date. */ 14479 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); 14480 14481 if (h->u2.vtable->used == NULL) 14482 { 14483 /* None of this table's entries were referenced. Re-use the 14484 parent's table. */ 14485 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; 14486 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; 14487 } 14488 else 14489 { 14490 size_t n; 14491 bool *cu, *pu; 14492 14493 /* Or the parent's entries into ours. */ 14494 cu = h->u2.vtable->used; 14495 cu[-1] = true; 14496 pu = h->u2.vtable->parent->u2.vtable->used; 14497 if (pu != NULL) 14498 { 14499 const struct elf_backend_data *bed; 14500 unsigned int log_file_align; 14501 14502 bed = get_elf_backend_data (h->root.u.def.section->owner); 14503 log_file_align = bed->s->log_file_align; 14504 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; 14505 while (n--) 14506 { 14507 if (*pu) 14508 *cu = true; 14509 pu++; 14510 cu++; 14511 } 14512 } 14513 } 14514 14515 return true; 14516 } 14517 14518 struct link_info_ok 14519 { 14520 struct bfd_link_info *info; 14521 bool ok; 14522 }; 14523 14524 static bool 14525 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, 14526 void *ptr) 14527 { 14528 asection *sec; 14529 bfd_vma hstart, hend; 14530 Elf_Internal_Rela *relstart, *relend, *rel; 14531 const struct elf_backend_data *bed; 14532 unsigned int log_file_align; 14533 struct link_info_ok *info = (struct link_info_ok *) ptr; 14534 14535 /* Take care of both those symbols that do not describe vtables as 14536 well as those that are not loaded. */ 14537 if (h->start_stop 14538 || h->u2.vtable == NULL 14539 || h->u2.vtable->parent == NULL) 14540 return true; 14541 14542 BFD_ASSERT (h->root.type == bfd_link_hash_defined 14543 || h->root.type == bfd_link_hash_defweak); 14544 14545 sec = h->root.u.def.section; 14546 hstart = h->root.u.def.value; 14547 hend = hstart + h->size; 14548 14549 relstart = _bfd_elf_link_info_read_relocs (sec->owner, info->info, 14550 sec, NULL, NULL, true); 14551 if (!relstart) 14552 return info->ok = false; 14553 bed = get_elf_backend_data (sec->owner); 14554 log_file_align = bed->s->log_file_align; 14555 14556 relend = relstart + sec->reloc_count; 14557 14558 for (rel = relstart; rel < relend; ++rel) 14559 if (rel->r_offset >= hstart && rel->r_offset < hend) 14560 { 14561 /* If the entry is in use, do nothing. */ 14562 if (h->u2.vtable->used 14563 && (rel->r_offset - hstart) < h->u2.vtable->size) 14564 { 14565 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; 14566 if (h->u2.vtable->used[entry]) 14567 continue; 14568 } 14569 /* Otherwise, kill it. */ 14570 rel->r_offset = rel->r_info = rel->r_addend = 0; 14571 } 14572 14573 return true; 14574 } 14575 14576 /* Mark sections containing dynamically referenced symbols. When 14577 building shared libraries, we must assume that any visible symbol is 14578 referenced. */ 14579 14580 bool 14581 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) 14582 { 14583 struct bfd_link_info *info = (struct bfd_link_info *) inf; 14584 struct bfd_elf_dynamic_list *d = info->dynamic_list; 14585 14586 if ((h->root.type == bfd_link_hash_defined 14587 || h->root.type == bfd_link_hash_defweak) 14588 && (!h->start_stop 14589 || h->root.ldscript_def 14590 || !info->start_stop_gc) 14591 && ((h->ref_dynamic && !h->forced_local) 14592 || ((h->def_regular || ELF_COMMON_DEF_P (h)) 14593 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 14594 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 14595 && (!bfd_link_executable (info) 14596 || info->gc_keep_exported 14597 || info->export_dynamic 14598 || (h->dynamic 14599 && d != NULL 14600 && (*d->match) (&d->head, NULL, h->root.root.string))) 14601 && (h->versioned >= versioned 14602 || !bfd_hide_sym_by_version (info->version_info, 14603 h->root.root.string))))) 14604 h->root.u.def.section->flags |= SEC_KEEP; 14605 14606 return true; 14607 } 14608 14609 /* Keep all sections containing symbols undefined on the command-line, 14610 and the section containing the entry symbol. */ 14611 14612 void 14613 _bfd_elf_gc_keep (struct bfd_link_info *info) 14614 { 14615 struct bfd_sym_chain *sym; 14616 14617 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) 14618 { 14619 struct elf_link_hash_entry *h; 14620 14621 h = elf_link_hash_lookup (elf_hash_table (info), sym->name, 14622 false, false, false); 14623 14624 if (h != NULL 14625 && (h->root.type == bfd_link_hash_defined 14626 || h->root.type == bfd_link_hash_defweak) 14627 && !bfd_is_const_section (h->root.u.def.section)) 14628 h->root.u.def.section->flags |= SEC_KEEP; 14629 } 14630 } 14631 14632 bool 14633 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, 14634 struct bfd_link_info *info) 14635 { 14636 bfd *ibfd = info->input_bfds; 14637 14638 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 14639 { 14640 asection *sec; 14641 struct elf_reloc_cookie cookie; 14642 14643 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 14644 continue; 14645 sec = ibfd->sections; 14646 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 14647 continue; 14648 14649 if (!init_reloc_cookie (&cookie, info, ibfd, false)) 14650 return false; 14651 14652 for (sec = ibfd->sections; sec; sec = sec->next) 14653 { 14654 if (startswith (bfd_section_name (sec), ".eh_frame_entry") 14655 && init_reloc_cookie_rels (&cookie, info, ibfd, sec, 14656 false)) 14657 { 14658 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); 14659 fini_reloc_cookie_rels (&cookie, sec); 14660 } 14661 } 14662 } 14663 return true; 14664 } 14665 14666 /* Do mark and sweep of unused sections. */ 14667 14668 bool 14669 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) 14670 { 14671 bool ok = true; 14672 bfd *sub; 14673 elf_gc_mark_hook_fn gc_mark_hook; 14674 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14675 struct elf_link_hash_table *htab; 14676 struct link_info_ok info_ok; 14677 14678 if (!bed->can_gc_sections 14679 || !is_elf_hash_table (info->hash)) 14680 { 14681 _bfd_error_handler(_("warning: gc-sections option ignored")); 14682 return true; 14683 } 14684 14685 bed->gc_keep (info); 14686 htab = elf_hash_table (info); 14687 14688 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section 14689 at the .eh_frame section if we can mark the FDEs individually. */ 14690 for (sub = info->input_bfds; 14691 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; 14692 sub = sub->link.next) 14693 { 14694 asection *sec; 14695 struct elf_reloc_cookie cookie; 14696 14697 sec = sub->sections; 14698 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 14699 continue; 14700 sec = bfd_get_section_by_name (sub, ".eh_frame"); 14701 while (sec && init_reloc_cookie_for_section (&cookie, info, sec, 14702 false)) 14703 { 14704 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); 14705 if (elf_section_data (sec)->sec_info 14706 && (sec->flags & SEC_LINKER_CREATED) == 0) 14707 elf_eh_frame_section (sub) = sec; 14708 fini_reloc_cookie_for_section (&cookie, sec); 14709 sec = bfd_get_next_section_by_name (NULL, sec); 14710 } 14711 } 14712 14713 /* Apply transitive closure to the vtable entry usage info. */ 14714 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); 14715 if (!ok) 14716 return false; 14717 14718 /* Kill the vtable relocations that were not used. */ 14719 info_ok.info = info; 14720 info_ok.ok = true; 14721 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &info_ok); 14722 if (!info_ok.ok) 14723 return false; 14724 14725 /* Mark dynamically referenced symbols. */ 14726 if (htab->dynamic_sections_created || info->gc_keep_exported) 14727 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); 14728 14729 /* Grovel through relocs to find out who stays ... */ 14730 gc_mark_hook = bed->gc_mark_hook; 14731 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 14732 { 14733 asection *o; 14734 14735 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 14736 || elf_object_id (sub) != elf_hash_table_id (htab) 14737 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 14738 continue; 14739 14740 o = sub->sections; 14741 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 14742 continue; 14743 14744 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). 14745 Also treat note sections as a root, if the section is not part 14746 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as 14747 well as FINI_ARRAY sections for ld -r. */ 14748 for (o = sub->sections; o != NULL; o = o->next) 14749 if (!o->gc_mark 14750 && (o->flags & SEC_EXCLUDE) == 0 14751 && ((o->flags & SEC_KEEP) != 0 14752 || (bfd_link_relocatable (info) 14753 && ((elf_section_data (o)->this_hdr.sh_type 14754 == SHT_PREINIT_ARRAY) 14755 || (elf_section_data (o)->this_hdr.sh_type 14756 == SHT_INIT_ARRAY) 14757 || (elf_section_data (o)->this_hdr.sh_type 14758 == SHT_FINI_ARRAY))) 14759 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE 14760 && elf_next_in_group (o) == NULL 14761 && elf_linked_to_section (o) == NULL) 14762 || ((elf_tdata (sub)->has_gnu_osabi & elf_gnu_osabi_retain) 14763 && (elf_section_flags (o) & SHF_GNU_RETAIN)))) 14764 { 14765 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 14766 return false; 14767 } 14768 } 14769 14770 /* Allow the backend to mark additional target specific sections. */ 14771 bed->gc_mark_extra_sections (info, gc_mark_hook); 14772 14773 /* ... and mark SEC_EXCLUDE for those that go. */ 14774 return elf_gc_sweep (abfd, info); 14775 } 14776 14777 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ 14779 14780 bool 14781 bfd_elf_gc_record_vtinherit (bfd *abfd, 14782 asection *sec, 14783 struct elf_link_hash_entry *h, 14784 bfd_vma offset) 14785 { 14786 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 14787 struct elf_link_hash_entry **search, *child; 14788 size_t extsymcount; 14789 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14790 14791 /* The sh_info field of the symtab header tells us where the 14792 external symbols start. We don't care about the local symbols at 14793 this point. */ 14794 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; 14795 if (!elf_bad_symtab (abfd)) 14796 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; 14797 14798 sym_hashes = elf_sym_hashes (abfd); 14799 sym_hashes_end = PTR_ADD (sym_hashes, extsymcount); 14800 14801 /* Hunt down the child symbol, which is in this section at the same 14802 offset as the relocation. */ 14803 for (search = sym_hashes; search != sym_hashes_end; ++search) 14804 { 14805 if ((child = *search) != NULL 14806 && (child->root.type == bfd_link_hash_defined 14807 || child->root.type == bfd_link_hash_defweak) 14808 && child->root.u.def.section == sec 14809 && child->root.u.def.value == offset) 14810 goto win; 14811 } 14812 14813 /* xgettext:c-format */ 14814 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), 14815 abfd, sec, (uint64_t) offset); 14816 bfd_set_error (bfd_error_invalid_operation); 14817 return false; 14818 14819 win: 14820 if (!child->u2.vtable) 14821 { 14822 child->u2.vtable = ((struct elf_link_virtual_table_entry *) 14823 bfd_zalloc (abfd, sizeof (*child->u2.vtable))); 14824 if (!child->u2.vtable) 14825 return false; 14826 } 14827 if (!h) 14828 { 14829 /* This *should* only be the absolute section. It could potentially 14830 be that someone has defined a non-global vtable though, which 14831 would be bad. It isn't worth paging in the local symbols to be 14832 sure though; that case should simply be handled by the assembler. */ 14833 14834 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; 14835 } 14836 else 14837 child->u2.vtable->parent = h; 14838 14839 return true; 14840 } 14841 14842 /* Called from check_relocs to record the existence of a VTENTRY reloc. */ 14843 14844 bool 14845 bfd_elf_gc_record_vtentry (bfd *abfd, asection *sec, 14846 struct elf_link_hash_entry *h, 14847 bfd_vma addend) 14848 { 14849 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14850 unsigned int log_file_align = bed->s->log_file_align; 14851 14852 if (!h) 14853 { 14854 /* xgettext:c-format */ 14855 _bfd_error_handler (_("%pB: section '%pA': corrupt VTENTRY entry"), 14856 abfd, sec); 14857 bfd_set_error (bfd_error_bad_value); 14858 return false; 14859 } 14860 14861 if (!h->u2.vtable) 14862 { 14863 h->u2.vtable = ((struct elf_link_virtual_table_entry *) 14864 bfd_zalloc (abfd, sizeof (*h->u2.vtable))); 14865 if (!h->u2.vtable) 14866 return false; 14867 } 14868 14869 if (addend >= h->u2.vtable->size) 14870 { 14871 size_t size, bytes, file_align; 14872 bool *ptr = h->u2.vtable->used; 14873 14874 /* While the symbol is undefined, we have to be prepared to handle 14875 a zero size. */ 14876 file_align = 1 << log_file_align; 14877 if (h->root.type == bfd_link_hash_undefined) 14878 size = addend + file_align; 14879 else 14880 { 14881 size = h->size; 14882 if (addend >= size) 14883 { 14884 /* Oops! We've got a reference past the defined end of 14885 the table. This is probably a bug -- shall we warn? */ 14886 size = addend + file_align; 14887 } 14888 } 14889 size = (size + file_align - 1) & -file_align; 14890 14891 /* Allocate one extra entry for use as a "done" flag for the 14892 consolidation pass. */ 14893 bytes = ((size >> log_file_align) + 1) * sizeof (bool); 14894 14895 if (ptr) 14896 { 14897 ptr = (bool *) bfd_realloc (ptr - 1, bytes); 14898 14899 if (ptr != NULL) 14900 { 14901 size_t oldbytes; 14902 14903 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) 14904 * sizeof (bool)); 14905 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); 14906 } 14907 } 14908 else 14909 ptr = (bool *) bfd_zmalloc (bytes); 14910 14911 if (ptr == NULL) 14912 return false; 14913 14914 /* And arrange for that done flag to be at index -1. */ 14915 h->u2.vtable->used = ptr + 1; 14916 h->u2.vtable->size = size; 14917 } 14918 14919 h->u2.vtable->used[addend >> log_file_align] = true; 14920 14921 return true; 14922 } 14923 14924 /* Map an ELF section header flag to its corresponding string. */ 14925 typedef struct 14926 { 14927 char *flag_name; 14928 flagword flag_value; 14929 } elf_flags_to_name_table; 14930 14931 static const elf_flags_to_name_table elf_flags_to_names [] = 14932 { 14933 { "SHF_WRITE", SHF_WRITE }, 14934 { "SHF_ALLOC", SHF_ALLOC }, 14935 { "SHF_EXECINSTR", SHF_EXECINSTR }, 14936 { "SHF_MERGE", SHF_MERGE }, 14937 { "SHF_STRINGS", SHF_STRINGS }, 14938 { "SHF_INFO_LINK", SHF_INFO_LINK}, 14939 { "SHF_LINK_ORDER", SHF_LINK_ORDER}, 14940 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, 14941 { "SHF_GROUP", SHF_GROUP }, 14942 { "SHF_TLS", SHF_TLS }, 14943 { "SHF_MASKOS", SHF_MASKOS }, 14944 { "SHF_EXCLUDE", SHF_EXCLUDE }, 14945 }; 14946 14947 /* Returns TRUE if the section is to be included, otherwise FALSE. */ 14948 bool 14949 bfd_elf_lookup_section_flags (struct bfd_link_info *info, 14950 struct flag_info *flaginfo, 14951 asection *section) 14952 { 14953 const bfd_vma sh_flags = elf_section_flags (section); 14954 14955 if (!flaginfo->flags_initialized) 14956 { 14957 bfd *obfd = info->output_bfd; 14958 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 14959 struct flag_info_list *tf = flaginfo->flag_list; 14960 int with_hex = 0; 14961 int without_hex = 0; 14962 14963 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) 14964 { 14965 unsigned i; 14966 flagword (*lookup) (char *); 14967 14968 lookup = bed->elf_backend_lookup_section_flags_hook; 14969 if (lookup != NULL) 14970 { 14971 flagword hexval = (*lookup) ((char *) tf->name); 14972 14973 if (hexval != 0) 14974 { 14975 if (tf->with == with_flags) 14976 with_hex |= hexval; 14977 else if (tf->with == without_flags) 14978 without_hex |= hexval; 14979 tf->valid = true; 14980 continue; 14981 } 14982 } 14983 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) 14984 { 14985 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) 14986 { 14987 if (tf->with == with_flags) 14988 with_hex |= elf_flags_to_names[i].flag_value; 14989 else if (tf->with == without_flags) 14990 without_hex |= elf_flags_to_names[i].flag_value; 14991 tf->valid = true; 14992 break; 14993 } 14994 } 14995 if (!tf->valid) 14996 { 14997 info->callbacks->einfo 14998 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); 14999 return false; 15000 } 15001 } 15002 flaginfo->flags_initialized = true; 15003 flaginfo->only_with_flags |= with_hex; 15004 flaginfo->not_with_flags |= without_hex; 15005 } 15006 15007 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) 15008 return false; 15009 15010 if ((flaginfo->not_with_flags & sh_flags) != 0) 15011 return false; 15012 15013 return true; 15014 } 15015 15016 struct alloc_got_off_arg { 15017 bfd_vma gotoff; 15018 struct bfd_link_info *info; 15019 }; 15020 15021 /* We need a special top-level link routine to convert got reference counts 15022 to real got offsets. */ 15023 15024 static bool 15025 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) 15026 { 15027 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; 15028 bfd *obfd = gofarg->info->output_bfd; 15029 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 15030 15031 if (h->got.refcount > 0) 15032 { 15033 h->got.offset = gofarg->gotoff; 15034 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); 15035 } 15036 else 15037 h->got.offset = (bfd_vma) -1; 15038 15039 return true; 15040 } 15041 15042 /* And an accompanying bit to work out final got entry offsets once 15043 we're done. Should be called from final_link. */ 15044 15045 bool 15046 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, 15047 struct bfd_link_info *info) 15048 { 15049 bfd *i; 15050 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 15051 bfd_vma gotoff; 15052 struct alloc_got_off_arg gofarg; 15053 15054 BFD_ASSERT (abfd == info->output_bfd); 15055 15056 if (! is_elf_hash_table (info->hash)) 15057 return false; 15058 15059 /* The GOT offset is relative to the .got section, but the GOT header is 15060 put into the .got.plt section, if the backend uses it. */ 15061 if (bed->want_got_plt) 15062 gotoff = 0; 15063 else 15064 gotoff = bed->got_header_size; 15065 15066 /* Do the local .got entries first. */ 15067 for (i = info->input_bfds; i; i = i->link.next) 15068 { 15069 bfd_signed_vma *local_got; 15070 size_t j, locsymcount; 15071 Elf_Internal_Shdr *symtab_hdr; 15072 15073 if (bfd_get_flavour (i) != bfd_target_elf_flavour) 15074 continue; 15075 15076 local_got = elf_local_got_refcounts (i); 15077 if (!local_got) 15078 continue; 15079 15080 symtab_hdr = &elf_tdata (i)->symtab_hdr; 15081 if (elf_bad_symtab (i)) 15082 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 15083 else 15084 locsymcount = symtab_hdr->sh_info; 15085 15086 for (j = 0; j < locsymcount; ++j) 15087 { 15088 if (local_got[j] > 0) 15089 { 15090 local_got[j] = gotoff; 15091 gotoff += bed->got_elt_size (abfd, info, NULL, i, j); 15092 } 15093 else 15094 local_got[j] = (bfd_vma) -1; 15095 } 15096 } 15097 15098 /* Then the global .got entries. .plt refcounts are handled by 15099 adjust_dynamic_symbol */ 15100 gofarg.gotoff = gotoff; 15101 gofarg.info = info; 15102 elf_link_hash_traverse (elf_hash_table (info), 15103 elf_gc_allocate_got_offsets, 15104 &gofarg); 15105 return true; 15106 } 15107 15108 /* Many folk need no more in the way of final link than this, once 15109 got entry reference counting is enabled. */ 15110 15111 bool 15112 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) 15113 { 15114 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) 15115 return false; 15116 15117 /* Invoke the regular ELF backend linker to do all the work. */ 15118 return bfd_elf_final_link (abfd, info); 15119 } 15120 15121 bool 15122 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) 15123 { 15124 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; 15125 15126 if (rcookie->bad_symtab) 15127 rcookie->rel = rcookie->rels; 15128 15129 for (; rcookie->rel < rcookie->relend; rcookie->rel++) 15130 { 15131 unsigned long r_symndx; 15132 15133 if (! rcookie->bad_symtab) 15134 if (rcookie->rel->r_offset > offset) 15135 return false; 15136 if (rcookie->rel->r_offset != offset) 15137 continue; 15138 15139 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; 15140 if (r_symndx == STN_UNDEF) 15141 return true; 15142 15143 struct elf_link_hash_entry *h; 15144 15145 h = get_ext_sym_hash_from_cookie (rcookie, r_symndx); 15146 15147 if (h != NULL) 15148 { 15149 if ((h->root.type == bfd_link_hash_defined 15150 || h->root.type == bfd_link_hash_defweak) 15151 && (h->root.u.def.section->owner != rcookie->abfd 15152 || h->root.u.def.section->kept_section != NULL 15153 || discarded_section (h->root.u.def.section))) 15154 return true; 15155 } 15156 else 15157 { 15158 if (r_symndx >= rcookie->locsymcount) 15159 /* This can happen with corrupt input. */ 15160 return false; 15161 15162 /* It's not a relocation against a global symbol, 15163 but it could be a relocation against a local 15164 symbol for a discarded section. */ 15165 asection *isec; 15166 Elf_Internal_Sym *isym; 15167 15168 /* Need to: get the symbol; get the section. */ 15169 isym = &rcookie->locsyms[r_symndx]; 15170 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); 15171 if (isec != NULL 15172 && (isec->kept_section != NULL 15173 || discarded_section (isec))) 15174 return true; 15175 } 15176 15177 return false; 15178 } 15179 return false; 15180 } 15181 15182 /* Discard unneeded references to discarded sections. 15183 Returns -1 on error, 1 if any section's size was changed, 0 if 15184 nothing changed. This function assumes that the relocations are in 15185 sorted order, which is true for all known assemblers. */ 15186 15187 int 15188 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) 15189 { 15190 struct elf_reloc_cookie cookie; 15191 asection *o; 15192 bfd *abfd; 15193 int changed = 0; 15194 15195 if (info->traditional_format 15196 || !is_elf_hash_table (info->hash)) 15197 return 0; 15198 15199 o = bfd_get_section_by_name (output_bfd, ".stab"); 15200 if (o != NULL) 15201 { 15202 asection *i; 15203 15204 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 15205 { 15206 if (i->size == 0 15207 || i->reloc_count == 0 15208 || i->sec_info_type != SEC_INFO_TYPE_STABS) 15209 continue; 15210 15211 abfd = i->owner; 15212 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 15213 continue; 15214 15215 if (!init_reloc_cookie_for_section (&cookie, info, i, false)) 15216 return -1; 15217 15218 if (_bfd_discard_section_stabs (abfd, i, 15219 elf_section_data (i)->sec_info, 15220 bfd_elf_reloc_symbol_deleted_p, 15221 &cookie)) 15222 changed = 1; 15223 15224 fini_reloc_cookie_for_section (&cookie, i); 15225 } 15226 } 15227 15228 o = NULL; 15229 if (info->eh_frame_hdr_type != COMPACT_EH_HDR) 15230 o = bfd_get_section_by_name (output_bfd, ".eh_frame"); 15231 if (o != NULL) 15232 { 15233 asection *i; 15234 int eh_changed = 0; 15235 unsigned int eh_alignment; /* Octets. */ 15236 15237 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 15238 { 15239 if (i->size == 0) 15240 continue; 15241 15242 abfd = i->owner; 15243 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 15244 continue; 15245 15246 if (!init_reloc_cookie_for_section (&cookie, info, i, false)) 15247 return -1; 15248 15249 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); 15250 if (_bfd_elf_discard_section_eh_frame (abfd, info, i, 15251 bfd_elf_reloc_symbol_deleted_p, 15252 &cookie)) 15253 { 15254 eh_changed = 1; 15255 if (i->size != i->rawsize) 15256 changed = 1; 15257 } 15258 15259 fini_reloc_cookie_for_section (&cookie, i); 15260 } 15261 15262 eh_alignment = ((1 << o->alignment_power) 15263 * bfd_octets_per_byte (output_bfd, o)); 15264 /* Skip over zero terminator, and prevent empty sections from 15265 adding alignment padding at the end. */ 15266 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) 15267 if (i->size == 0) 15268 i->flags |= SEC_EXCLUDE; 15269 else if (i->size > 4) 15270 break; 15271 /* The last non-empty eh_frame section doesn't need padding. */ 15272 if (i != NULL) 15273 i = i->map_tail.s; 15274 /* Any prior sections must pad the last FDE out to the output 15275 section alignment. Otherwise we might have zero padding 15276 between sections, which would be seen as a terminator. */ 15277 for (; i != NULL; i = i->map_tail.s) 15278 if (i->size == 4) 15279 /* All but the last zero terminator should have been removed. */ 15280 BFD_FAIL (); 15281 else 15282 { 15283 bfd_size_type size 15284 = (i->size + eh_alignment - 1) & -eh_alignment; 15285 if (i->size != size) 15286 { 15287 i->size = size; 15288 changed = 1; 15289 eh_changed = 1; 15290 } 15291 } 15292 if (eh_changed) 15293 elf_link_hash_traverse (elf_hash_table (info), 15294 _bfd_elf_adjust_eh_frame_global_symbol, NULL); 15295 } 15296 15297 o = bfd_get_section_by_name (output_bfd, ".sframe"); 15298 if (o != NULL) 15299 { 15300 asection *i; 15301 15302 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 15303 { 15304 if (i->size == 0) 15305 continue; 15306 15307 abfd = i->owner; 15308 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 15309 continue; 15310 15311 if (!init_reloc_cookie_for_section (&cookie, info, i, false)) 15312 return -1; 15313 15314 if (_bfd_elf_parse_sframe (abfd, info, i, &cookie)) 15315 { 15316 if (_bfd_elf_discard_section_sframe (i, 15317 bfd_elf_reloc_symbol_deleted_p, 15318 &cookie)) 15319 { 15320 if (i->size != i->rawsize) 15321 changed = 1; 15322 } 15323 } 15324 fini_reloc_cookie_for_section (&cookie, i); 15325 } 15326 /* Update the reference to the output .sframe section. Used to 15327 determine later if PT_GNU_SFRAME segment is to be generated. */ 15328 if (!_bfd_elf_set_section_sframe (output_bfd, info)) 15329 return -1; 15330 } 15331 15332 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) 15333 { 15334 const struct elf_backend_data *bed; 15335 asection *s; 15336 15337 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 15338 continue; 15339 s = abfd->sections; 15340 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 15341 continue; 15342 15343 bed = get_elf_backend_data (abfd); 15344 15345 if (bed->elf_backend_discard_info != NULL) 15346 { 15347 if (!init_reloc_cookie (&cookie, info, abfd, false)) 15348 return -1; 15349 15350 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) 15351 changed = 1; 15352 15353 fini_reloc_cookie (&cookie, abfd); 15354 } 15355 } 15356 15357 if (info->eh_frame_hdr_type == COMPACT_EH_HDR) 15358 _bfd_elf_end_eh_frame_parsing (info); 15359 15360 if (_bfd_elf_discard_section_eh_frame_hdr (info)) 15361 changed = 1; 15362 15363 return changed; 15364 } 15365 15366 bool 15367 _bfd_elf_section_already_linked (bfd *abfd, 15368 asection *sec, 15369 struct bfd_link_info *info) 15370 { 15371 flagword flags; 15372 const char *name, *key; 15373 struct bfd_section_already_linked *l; 15374 struct bfd_section_already_linked_hash_entry *already_linked_list; 15375 15376 if (sec->output_section == bfd_abs_section_ptr) 15377 return false; 15378 15379 flags = sec->flags; 15380 15381 /* Return if it isn't a linkonce section. A comdat group section 15382 also has SEC_LINK_ONCE set. */ 15383 if ((flags & SEC_LINK_ONCE) == 0) 15384 return false; 15385 15386 /* Don't put group member sections on our list of already linked 15387 sections. They are handled as a group via their group section. */ 15388 if (elf_sec_group (sec) != NULL) 15389 return false; 15390 15391 /* For a SHT_GROUP section, use the group signature as the key. */ 15392 name = sec->name; 15393 if ((flags & SEC_GROUP) != 0 15394 && elf_next_in_group (sec) != NULL 15395 && elf_group_name (elf_next_in_group (sec)) != NULL) 15396 key = elf_group_name (elf_next_in_group (sec)); 15397 else 15398 { 15399 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ 15400 if (startswith (name, ".gnu.linkonce.") 15401 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 15402 key++; 15403 else 15404 /* Must be a user linkonce section that doesn't follow gcc's 15405 naming convention. In this case we won't be matching 15406 single member groups. */ 15407 key = name; 15408 } 15409 15410 already_linked_list = bfd_section_already_linked_table_lookup (key); 15411 15412 for (l = already_linked_list->entry; l != NULL; l = l->next) 15413 { 15414 /* We may have 2 different types of sections on the list: group 15415 sections with a signature of <key> (<key> is some string), 15416 and linkonce sections named .gnu.linkonce.<type>.<key>. 15417 Match like sections. LTO plugin sections are an exception. 15418 They are always named .gnu.linkonce.t.<key> and match either 15419 type of section. */ 15420 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) 15421 && ((flags & SEC_GROUP) != 0 15422 || strcmp (name, l->sec->name) == 0)) 15423 || (l->sec->owner->flags & BFD_PLUGIN) != 0 15424 || (sec->owner->flags & BFD_PLUGIN) != 0) 15425 { 15426 /* The section has already been linked. See if we should 15427 issue a warning. */ 15428 if (!_bfd_handle_already_linked (sec, l, info)) 15429 return false; 15430 15431 if (flags & SEC_GROUP) 15432 { 15433 asection *first = elf_next_in_group (sec); 15434 asection *s = first; 15435 15436 while (s != NULL) 15437 { 15438 s->output_section = bfd_abs_section_ptr; 15439 /* Record which group discards it. */ 15440 s->kept_section = l->sec; 15441 s = elf_next_in_group (s); 15442 /* These lists are circular. */ 15443 if (s == first) 15444 break; 15445 } 15446 } 15447 15448 return true; 15449 } 15450 } 15451 15452 /* A single member comdat group section may be discarded by a 15453 linkonce section and vice versa. */ 15454 if ((flags & SEC_GROUP) != 0) 15455 { 15456 asection *first = elf_next_in_group (sec); 15457 15458 if (first != NULL && elf_next_in_group (first) == first) 15459 /* Check this single member group against linkonce sections. */ 15460 for (l = already_linked_list->entry; l != NULL; l = l->next) 15461 if ((l->sec->flags & SEC_GROUP) == 0 15462 && bfd_elf_match_symbols_in_sections (l->sec, first, info)) 15463 { 15464 first->output_section = bfd_abs_section_ptr; 15465 first->kept_section = l->sec; 15466 sec->output_section = bfd_abs_section_ptr; 15467 break; 15468 } 15469 } 15470 else 15471 /* Check this linkonce section against single member groups. */ 15472 for (l = already_linked_list->entry; l != NULL; l = l->next) 15473 if (l->sec->flags & SEC_GROUP) 15474 { 15475 asection *first = elf_next_in_group (l->sec); 15476 15477 if (first != NULL 15478 && elf_next_in_group (first) == first 15479 && bfd_elf_match_symbols_in_sections (first, sec, info)) 15480 { 15481 sec->output_section = bfd_abs_section_ptr; 15482 sec->kept_section = first; 15483 break; 15484 } 15485 } 15486 15487 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' 15488 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 15489 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' 15490 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its 15491 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded 15492 but its `.gnu.linkonce.t.F' is discarded means we chose one-only 15493 `.gnu.linkonce.t.F' section from a different bfd not requiring any 15494 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. 15495 The reverse order cannot happen as there is never a bfd with only the 15496 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not 15497 matter as here were are looking only for cross-bfd sections. */ 15498 15499 if ((flags & SEC_GROUP) == 0 && startswith (name, ".gnu.linkonce.r.")) 15500 for (l = already_linked_list->entry; l != NULL; l = l->next) 15501 if ((l->sec->flags & SEC_GROUP) == 0 15502 && startswith (l->sec->name, ".gnu.linkonce.t.")) 15503 { 15504 if (abfd != l->sec->owner) 15505 sec->output_section = bfd_abs_section_ptr; 15506 break; 15507 } 15508 15509 /* This is the first section with this name. Record it. */ 15510 if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) 15511 info->callbacks->fatal (_("%P: already_linked_table: %E\n")); 15512 return sec->output_section == bfd_abs_section_ptr; 15513 } 15514 15515 bool 15516 _bfd_elf_common_definition (Elf_Internal_Sym *sym) 15517 { 15518 return sym->st_shndx == SHN_COMMON; 15519 } 15520 15521 unsigned int 15522 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) 15523 { 15524 return SHN_COMMON; 15525 } 15526 15527 asection * 15528 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) 15529 { 15530 return bfd_com_section_ptr; 15531 } 15532 15533 bfd_vma 15534 _bfd_elf_default_got_elt_size (bfd *abfd, 15535 struct bfd_link_info *info ATTRIBUTE_UNUSED, 15536 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, 15537 bfd *ibfd ATTRIBUTE_UNUSED, 15538 unsigned long symndx ATTRIBUTE_UNUSED) 15539 { 15540 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 15541 return bed->s->arch_size / 8; 15542 } 15543 15544 /* Routines to support the creation of dynamic relocs. */ 15545 15546 /* Returns the name of the dynamic reloc section associated with SEC. */ 15547 15548 static const char * 15549 get_dynamic_reloc_section_name (bfd * abfd, 15550 asection * sec, 15551 bool is_rela) 15552 { 15553 char *name; 15554 const char *old_name = bfd_section_name (sec); 15555 const char *prefix = is_rela ? ".rela" : ".rel"; 15556 15557 if (old_name == NULL) 15558 return NULL; 15559 15560 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); 15561 sprintf (name, "%s%s", prefix, old_name); 15562 15563 return name; 15564 } 15565 15566 /* Returns the dynamic reloc section associated with SEC. 15567 If necessary compute the name of the dynamic reloc section based 15568 on SEC's name (looked up in ABFD's string table) and the setting 15569 of IS_RELA. */ 15570 15571 asection * 15572 _bfd_elf_get_dynamic_reloc_section (bfd *abfd, 15573 asection *sec, 15574 bool is_rela) 15575 { 15576 asection *reloc_sec = elf_section_data (sec)->sreloc; 15577 15578 if (reloc_sec == NULL) 15579 { 15580 const char *name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 15581 15582 if (name != NULL) 15583 { 15584 reloc_sec = bfd_get_linker_section (abfd, name); 15585 15586 if (reloc_sec != NULL) 15587 elf_section_data (sec)->sreloc = reloc_sec; 15588 } 15589 } 15590 15591 return reloc_sec; 15592 } 15593 15594 /* Returns the dynamic reloc section associated with SEC. If the 15595 section does not exist it is created and attached to the DYNOBJ 15596 bfd and stored in the SRELOC field of SEC's elf_section_data 15597 structure. 15598 15599 ALIGNMENT is the alignment for the newly created section and 15600 IS_RELA defines whether the name should be .rela.<SEC's name> 15601 or .rel.<SEC's name>. The section name is looked up in the 15602 string table associated with ABFD. */ 15603 15604 asection * 15605 _bfd_elf_make_dynamic_reloc_section (asection *sec, 15606 bfd *dynobj, 15607 unsigned int alignment, 15608 bfd *abfd, 15609 bool is_rela) 15610 { 15611 asection * reloc_sec = elf_section_data (sec)->sreloc; 15612 15613 if (reloc_sec == NULL) 15614 { 15615 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 15616 15617 if (name == NULL) 15618 return NULL; 15619 15620 reloc_sec = bfd_get_linker_section (dynobj, name); 15621 15622 if (reloc_sec == NULL) 15623 { 15624 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY 15625 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 15626 if ((sec->flags & SEC_ALLOC) != 0) 15627 flags |= SEC_ALLOC | SEC_LOAD; 15628 15629 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); 15630 if (reloc_sec != NULL) 15631 { 15632 /* _bfd_elf_get_sec_type_attr chooses a section type by 15633 name. Override as it may be wrong, eg. for a user 15634 section named "auto" we'll get ".relauto" which is 15635 seen to be a .rela section. */ 15636 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; 15637 if (!bfd_set_section_alignment (reloc_sec, alignment)) 15638 reloc_sec = NULL; 15639 } 15640 } 15641 15642 elf_section_data (sec)->sreloc = reloc_sec; 15643 } 15644 15645 return reloc_sec; 15646 } 15647 15648 /* Copy the ELF symbol type and other attributes for a linker script 15649 assignment from HSRC to HDEST. Generally this should be treated as 15650 if we found a strong non-dynamic definition for HDEST (except that 15651 ld ignores multiple definition errors). */ 15652 void 15653 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, 15654 struct bfd_link_hash_entry *hdest, 15655 struct bfd_link_hash_entry *hsrc) 15656 { 15657 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; 15658 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; 15659 Elf_Internal_Sym isym; 15660 15661 ehdest->type = ehsrc->type; 15662 ehdest->target_internal = ehsrc->target_internal; 15663 15664 isym.st_other = ehsrc->other; 15665 elf_merge_st_other (abfd, ehdest, isym.st_other, NULL, true, false); 15666 } 15667 15668 /* Append a RELA relocation REL to section S in BFD. */ 15669 15670 void 15671 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 15672 { 15673 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 15674 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); 15675 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); 15676 bed->s->swap_reloca_out (abfd, rel, loc); 15677 } 15678 15679 /* Append a REL relocation REL to section S in BFD. */ 15680 15681 void 15682 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 15683 { 15684 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 15685 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); 15686 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); 15687 bed->s->swap_reloc_out (abfd, rel, loc); 15688 } 15689 15690 /* Define __start, __stop, .startof. or .sizeof. symbol. */ 15691 15692 struct bfd_link_hash_entry * 15693 bfd_elf_define_start_stop (struct bfd_link_info *info, 15694 const char *symbol, asection *sec) 15695 { 15696 struct elf_link_hash_entry *h; 15697 15698 h = elf_link_hash_lookup (elf_hash_table (info), symbol, 15699 false, false, true); 15700 /* NB: Common symbols will be turned into definition later. */ 15701 if (h != NULL 15702 && !h->root.ldscript_def 15703 && (h->root.type == bfd_link_hash_undefined 15704 || h->root.type == bfd_link_hash_undefweak 15705 || ((h->ref_regular || h->def_dynamic) 15706 && !h->def_regular 15707 && h->root.type != bfd_link_hash_common))) 15708 { 15709 bool was_dynamic = h->ref_dynamic || h->def_dynamic; 15710 h->verinfo.verdef = NULL; 15711 h->root.type = bfd_link_hash_defined; 15712 h->root.u.def.section = sec; 15713 h->root.u.def.value = 0; 15714 h->def_regular = 1; 15715 h->def_dynamic = 0; 15716 h->start_stop = 1; 15717 h->u2.start_stop_section = sec; 15718 if (symbol[0] == '.') 15719 { 15720 /* .startof. and .sizeof. symbols are local. */ 15721 const struct elf_backend_data *bed; 15722 bed = get_elf_backend_data (info->output_bfd); 15723 (*bed->elf_backend_hide_symbol) (info, h, true); 15724 } 15725 else 15726 { 15727 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 15728 h->other = ((h->other & ~ELF_ST_VISIBILITY (-1)) 15729 | info->start_stop_visibility); 15730 if (was_dynamic) 15731 bfd_elf_link_record_dynamic_symbol (info, h); 15732 } 15733 return &h->root; 15734 } 15735 return NULL; 15736 } 15737 15738 /* Find dynamic relocs for H that apply to read-only sections. */ 15739 15740 asection * 15741 _bfd_elf_readonly_dynrelocs (struct elf_link_hash_entry *h) 15742 { 15743 struct elf_dyn_relocs *p; 15744 15745 for (p = h->dyn_relocs; p != NULL; p = p->next) 15746 { 15747 asection *s = p->sec->output_section; 15748 15749 if (s != NULL && (s->flags & SEC_READONLY) != 0) 15750 return p->sec; 15751 } 15752 return NULL; 15753 } 15754 15755 /* Set DF_TEXTREL if we find any dynamic relocs that apply to 15756 read-only sections. */ 15757 15758 bool 15759 _bfd_elf_maybe_set_textrel (struct elf_link_hash_entry *h, void *inf) 15760 { 15761 asection *sec; 15762 15763 if (h->root.type == bfd_link_hash_indirect) 15764 return true; 15765 15766 sec = _bfd_elf_readonly_dynrelocs (h); 15767 if (sec != NULL) 15768 { 15769 struct bfd_link_info *info = (struct bfd_link_info *) inf; 15770 15771 info->flags |= DF_TEXTREL; 15772 /* xgettext:c-format */ 15773 info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' " 15774 "in read-only section `%pA'\n"), 15775 sec->owner, h->root.root.string, sec); 15776 15777 if (bfd_link_textrel_check (info)) 15778 /* xgettext:c-format */ 15779 info->callbacks->einfo (_("%P: %pB: warning: relocation against `%s' " 15780 "in read-only section `%pA'\n"), 15781 sec->owner, h->root.root.string, sec); 15782 15783 /* Not an error, just cut short the traversal. */ 15784 return false; 15785 } 15786 return true; 15787 } 15788 15789 /* Add dynamic tags. */ 15790 15791 bool 15792 _bfd_elf_add_dynamic_tags (bfd *output_bfd, struct bfd_link_info *info, 15793 bool need_dynamic_reloc) 15794 { 15795 struct elf_link_hash_table *htab = elf_hash_table (info); 15796 15797 if (htab->dynamic_sections_created) 15798 { 15799 /* Add some entries to the .dynamic section. We fill in the 15800 values later, in finish_dynamic_sections, but we must add 15801 the entries now so that we get the correct size for the 15802 .dynamic section. The DT_DEBUG entry is filled in by the 15803 dynamic linker and used by the debugger. */ 15804 #define add_dynamic_entry(TAG, VAL) \ 15805 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 15806 15807 const struct elf_backend_data *bed 15808 = get_elf_backend_data (output_bfd); 15809 15810 if (bfd_link_executable (info)) 15811 { 15812 if (!add_dynamic_entry (DT_DEBUG, 0)) 15813 return false; 15814 } 15815 15816 if (htab->dt_pltgot_required || htab->splt->size != 0) 15817 { 15818 /* DT_PLTGOT is used by prelink even if there is no PLT 15819 relocation. */ 15820 if (!add_dynamic_entry (DT_PLTGOT, 0)) 15821 return false; 15822 } 15823 15824 if (htab->dt_jmprel_required || htab->srelplt->size != 0) 15825 { 15826 if (!add_dynamic_entry (DT_PLTRELSZ, 0) 15827 || !add_dynamic_entry (DT_PLTREL, 15828 (bed->rela_plts_and_copies_p 15829 ? DT_RELA : DT_REL)) 15830 || !add_dynamic_entry (DT_JMPREL, 0)) 15831 return false; 15832 } 15833 15834 if (htab->tlsdesc_plt 15835 && (!add_dynamic_entry (DT_TLSDESC_PLT, 0) 15836 || !add_dynamic_entry (DT_TLSDESC_GOT, 0))) 15837 return false; 15838 15839 if (need_dynamic_reloc) 15840 { 15841 if (bed->rela_plts_and_copies_p) 15842 { 15843 if (!add_dynamic_entry (DT_RELA, 0) 15844 || !add_dynamic_entry (DT_RELASZ, 0) 15845 || !add_dynamic_entry (DT_RELAENT, 15846 bed->s->sizeof_rela)) 15847 return false; 15848 } 15849 else 15850 { 15851 if (!add_dynamic_entry (DT_REL, 0) 15852 || !add_dynamic_entry (DT_RELSZ, 0) 15853 || !add_dynamic_entry (DT_RELENT, 15854 bed->s->sizeof_rel)) 15855 return false; 15856 } 15857 15858 /* If any dynamic relocs apply to a read-only section, 15859 then we need a DT_TEXTREL entry. */ 15860 if ((info->flags & DF_TEXTREL) == 0) 15861 elf_link_hash_traverse (htab, _bfd_elf_maybe_set_textrel, 15862 info); 15863 15864 if ((info->flags & DF_TEXTREL) != 0) 15865 { 15866 if (htab->ifunc_resolvers) 15867 info->callbacks->einfo 15868 (_("%P: warning: GNU indirect functions with DT_TEXTREL " 15869 "may result in a segfault at runtime; recompile with %s\n"), 15870 bfd_link_dll (info) ? "-fPIC" : "-fPIE"); 15871 15872 if (!add_dynamic_entry (DT_TEXTREL, 0)) 15873 return false; 15874 } 15875 } 15876 } 15877 #undef add_dynamic_entry 15878 15879 return true; 15880 } 15881