1 /* ELF linking support for BFD. 2 Copyright (C) 1995-2018 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 "bfd_stdint.h" 24 #include "bfdlink.h" 25 #include "libbfd.h" 26 #define ARCH_SIZE 0 27 #include "elf-bfd.h" 28 #include "safe-ctype.h" 29 #include "libiberty.h" 30 #include "objalloc.h" 31 #if BFD_SUPPORTS_PLUGINS 32 #include "plugin-api.h" 33 #include "plugin.h" 34 #endif 35 36 /* This struct is used to pass information to routines called via 37 elf_link_hash_traverse which must return failure. */ 38 39 struct elf_info_failed 40 { 41 struct bfd_link_info *info; 42 bfd_boolean failed; 43 }; 44 45 /* This structure is used to pass information to 46 _bfd_elf_link_find_version_dependencies. */ 47 48 struct elf_find_verdep_info 49 { 50 /* General link information. */ 51 struct bfd_link_info *info; 52 /* The number of dependencies. */ 53 unsigned int vers; 54 /* Whether we had a failure. */ 55 bfd_boolean failed; 56 }; 57 58 static bfd_boolean _bfd_elf_fix_symbol_flags 59 (struct elf_link_hash_entry *, struct elf_info_failed *); 60 61 asection * 62 _bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie, 63 unsigned long r_symndx, 64 bfd_boolean discard) 65 { 66 if (r_symndx >= cookie->locsymcount 67 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 68 { 69 struct elf_link_hash_entry *h; 70 71 h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; 72 73 while (h->root.type == bfd_link_hash_indirect 74 || h->root.type == bfd_link_hash_warning) 75 h = (struct elf_link_hash_entry *) h->root.u.i.link; 76 77 if ((h->root.type == bfd_link_hash_defined 78 || h->root.type == bfd_link_hash_defweak) 79 && discarded_section (h->root.u.def.section)) 80 return h->root.u.def.section; 81 else 82 return NULL; 83 } 84 else 85 { 86 /* It's not a relocation against a global symbol, 87 but it could be a relocation against a local 88 symbol for a discarded section. */ 89 asection *isec; 90 Elf_Internal_Sym *isym; 91 92 /* Need to: get the symbol; get the section. */ 93 isym = &cookie->locsyms[r_symndx]; 94 isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx); 95 if (isec != NULL 96 && discard ? discarded_section (isec) : 1) 97 return isec; 98 } 99 return NULL; 100 } 101 102 /* Define a symbol in a dynamic linkage section. */ 103 104 struct elf_link_hash_entry * 105 _bfd_elf_define_linkage_sym (bfd *abfd, 106 struct bfd_link_info *info, 107 asection *sec, 108 const char *name) 109 { 110 struct elf_link_hash_entry *h; 111 struct bfd_link_hash_entry *bh; 112 const struct elf_backend_data *bed; 113 114 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 115 if (h != NULL) 116 { 117 /* Zap symbol defined in an as-needed lib that wasn't linked. 118 This is a symptom of a larger problem: Absolute symbols 119 defined in shared libraries can't be overridden, because we 120 lose the link to the bfd which is via the symbol section. */ 121 h->root.type = bfd_link_hash_new; 122 bh = &h->root; 123 } 124 else 125 bh = NULL; 126 127 bed = get_elf_backend_data (abfd); 128 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, 129 sec, 0, NULL, FALSE, bed->collect, 130 &bh)) 131 return NULL; 132 h = (struct elf_link_hash_entry *) bh; 133 BFD_ASSERT (h != NULL); 134 h->def_regular = 1; 135 h->non_elf = 0; 136 h->root.linker_def = 1; 137 h->type = STT_OBJECT; 138 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 139 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 140 141 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 142 return h; 143 } 144 145 bfd_boolean 146 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) 147 { 148 flagword flags; 149 asection *s; 150 struct elf_link_hash_entry *h; 151 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 152 struct elf_link_hash_table *htab = elf_hash_table (info); 153 154 /* This function may be called more than once. */ 155 if (htab->sgot != NULL) 156 return TRUE; 157 158 flags = bed->dynamic_sec_flags; 159 160 s = bfd_make_section_anyway_with_flags (abfd, 161 (bed->rela_plts_and_copies_p 162 ? ".rela.got" : ".rel.got"), 163 (bed->dynamic_sec_flags 164 | SEC_READONLY)); 165 if (s == NULL 166 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 167 return FALSE; 168 htab->srelgot = s; 169 170 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 171 if (s == NULL 172 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 173 return FALSE; 174 htab->sgot = s; 175 176 if (bed->want_got_plt) 177 { 178 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); 179 if (s == NULL 180 || !bfd_set_section_alignment (abfd, s, 181 bed->s->log_file_align)) 182 return FALSE; 183 htab->sgotplt = s; 184 } 185 186 /* The first bit of the global offset table is the header. */ 187 s->size += bed->got_header_size; 188 189 if (bed->want_got_sym) 190 { 191 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 192 (or .got.plt) section. We don't do this in the linker script 193 because we don't want to define the symbol if we are not creating 194 a global offset table. */ 195 h = _bfd_elf_define_linkage_sym (abfd, info, s, 196 "_GLOBAL_OFFSET_TABLE_"); 197 elf_hash_table (info)->hgot = h; 198 if (h == NULL) 199 return FALSE; 200 } 201 202 return TRUE; 203 } 204 205 /* Create a strtab to hold the dynamic symbol names. */ 206 static bfd_boolean 207 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) 208 { 209 struct elf_link_hash_table *hash_table; 210 211 hash_table = elf_hash_table (info); 212 if (hash_table->dynobj == NULL) 213 { 214 /* We may not set dynobj, an input file holding linker created 215 dynamic sections to abfd, which may be a dynamic object with 216 its own dynamic sections. We need to find a normal input file 217 to hold linker created sections if possible. */ 218 if ((abfd->flags & (DYNAMIC | BFD_PLUGIN)) != 0) 219 { 220 bfd *ibfd; 221 asection *s; 222 for (ibfd = info->input_bfds; ibfd; ibfd = ibfd->link.next) 223 if ((ibfd->flags 224 & (DYNAMIC | BFD_LINKER_CREATED | BFD_PLUGIN)) == 0 225 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour 226 && !((s = ibfd->sections) != NULL 227 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS)) 228 { 229 abfd = ibfd; 230 break; 231 } 232 } 233 hash_table->dynobj = abfd; 234 } 235 236 if (hash_table->dynstr == NULL) 237 { 238 hash_table->dynstr = _bfd_elf_strtab_init (); 239 if (hash_table->dynstr == NULL) 240 return FALSE; 241 } 242 return TRUE; 243 } 244 245 /* Create some sections which will be filled in with dynamic linking 246 information. ABFD is an input file which requires dynamic sections 247 to be created. The dynamic sections take up virtual memory space 248 when the final executable is run, so we need to create them before 249 addresses are assigned to the output sections. We work out the 250 actual contents and size of these sections later. */ 251 252 bfd_boolean 253 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 254 { 255 flagword flags; 256 asection *s; 257 const struct elf_backend_data *bed; 258 struct elf_link_hash_entry *h; 259 260 if (! is_elf_hash_table (info->hash)) 261 return FALSE; 262 263 if (elf_hash_table (info)->dynamic_sections_created) 264 return TRUE; 265 266 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 267 return FALSE; 268 269 abfd = elf_hash_table (info)->dynobj; 270 bed = get_elf_backend_data (abfd); 271 272 flags = bed->dynamic_sec_flags; 273 274 /* A dynamically linked executable has a .interp section, but a 275 shared library does not. */ 276 if (bfd_link_executable (info) && !info->nointerp) 277 { 278 s = bfd_make_section_anyway_with_flags (abfd, ".interp", 279 flags | SEC_READONLY); 280 if (s == NULL) 281 return FALSE; 282 } 283 284 /* Create sections to hold version informations. These are removed 285 if they are not needed. */ 286 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", 287 flags | SEC_READONLY); 288 if (s == NULL 289 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 290 return FALSE; 291 292 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", 293 flags | SEC_READONLY); 294 if (s == NULL 295 || ! bfd_set_section_alignment (abfd, s, 1)) 296 return FALSE; 297 298 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", 299 flags | SEC_READONLY); 300 if (s == NULL 301 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 302 return FALSE; 303 304 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", 305 flags | SEC_READONLY); 306 if (s == NULL 307 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 308 return FALSE; 309 elf_hash_table (info)->dynsym = s; 310 311 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", 312 flags | SEC_READONLY); 313 if (s == NULL) 314 return FALSE; 315 316 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); 317 if (s == NULL 318 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 319 return FALSE; 320 321 /* The special symbol _DYNAMIC is always set to the start of the 322 .dynamic section. We could set _DYNAMIC in a linker script, but we 323 only want to define it if we are, in fact, creating a .dynamic 324 section. We don't want to define it if there is no .dynamic 325 section, since on some ELF platforms the start up code examines it 326 to decide how to initialize the process. */ 327 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC"); 328 elf_hash_table (info)->hdynamic = h; 329 if (h == NULL) 330 return FALSE; 331 332 if (info->emit_hash) 333 { 334 s = bfd_make_section_anyway_with_flags (abfd, ".hash", 335 flags | SEC_READONLY); 336 if (s == NULL 337 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 338 return FALSE; 339 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; 340 } 341 342 if (info->emit_gnu_hash) 343 { 344 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", 345 flags | SEC_READONLY); 346 if (s == NULL 347 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 348 return FALSE; 349 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: 350 4 32-bit words followed by variable count of 64-bit words, then 351 variable count of 32-bit words. */ 352 if (bed->s->arch_size == 64) 353 elf_section_data (s)->this_hdr.sh_entsize = 0; 354 else 355 elf_section_data (s)->this_hdr.sh_entsize = 4; 356 } 357 358 /* Let the backend create the rest of the sections. This lets the 359 backend set the right flags. The backend will normally create 360 the .got and .plt sections. */ 361 if (bed->elf_backend_create_dynamic_sections == NULL 362 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) 363 return FALSE; 364 365 elf_hash_table (info)->dynamic_sections_created = TRUE; 366 367 return TRUE; 368 } 369 370 /* Create dynamic sections when linking against a dynamic object. */ 371 372 bfd_boolean 373 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 374 { 375 flagword flags, pltflags; 376 struct elf_link_hash_entry *h; 377 asection *s; 378 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 379 struct elf_link_hash_table *htab = elf_hash_table (info); 380 381 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 382 .rel[a].bss sections. */ 383 flags = bed->dynamic_sec_flags; 384 385 pltflags = flags; 386 if (bed->plt_not_loaded) 387 /* We do not clear SEC_ALLOC here because we still want the OS to 388 allocate space for the section; it's just that there's nothing 389 to read in from the object file. */ 390 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); 391 else 392 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; 393 if (bed->plt_readonly) 394 pltflags |= SEC_READONLY; 395 396 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); 397 if (s == NULL 398 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 399 return FALSE; 400 htab->splt = s; 401 402 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 403 .plt section. */ 404 if (bed->want_plt_sym) 405 { 406 h = _bfd_elf_define_linkage_sym (abfd, info, s, 407 "_PROCEDURE_LINKAGE_TABLE_"); 408 elf_hash_table (info)->hplt = h; 409 if (h == NULL) 410 return FALSE; 411 } 412 413 s = bfd_make_section_anyway_with_flags (abfd, 414 (bed->rela_plts_and_copies_p 415 ? ".rela.plt" : ".rel.plt"), 416 flags | SEC_READONLY); 417 if (s == NULL 418 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 419 return FALSE; 420 htab->srelplt = s; 421 422 if (! _bfd_elf_create_got_section (abfd, info)) 423 return FALSE; 424 425 if (bed->want_dynbss) 426 { 427 /* The .dynbss section is a place to put symbols which are defined 428 by dynamic objects, are referenced by regular objects, and are 429 not functions. We must allocate space for them in the process 430 image and use a R_*_COPY reloc to tell the dynamic linker to 431 initialize them at run time. The linker script puts the .dynbss 432 section into the .bss section of the final image. */ 433 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", 434 SEC_ALLOC | SEC_LINKER_CREATED); 435 if (s == NULL) 436 return FALSE; 437 htab->sdynbss = s; 438 439 if (bed->want_dynrelro) 440 { 441 /* Similarly, but for symbols that were originally in read-only 442 sections. This section doesn't really need to have contents, 443 but make it like other .data.rel.ro sections. */ 444 s = bfd_make_section_anyway_with_flags (abfd, ".data.rel.ro", 445 flags); 446 if (s == NULL) 447 return FALSE; 448 htab->sdynrelro = s; 449 } 450 451 /* The .rel[a].bss section holds copy relocs. This section is not 452 normally needed. We need to create it here, though, so that the 453 linker will map it to an output section. We can't just create it 454 only if we need it, because we will not know whether we need it 455 until we have seen all the input files, and the first time the 456 main linker code calls BFD after examining all the input files 457 (size_dynamic_sections) the input sections have already been 458 mapped to the output sections. If the section turns out not to 459 be needed, we can discard it later. We will never need this 460 section when generating a shared object, since they do not use 461 copy relocs. */ 462 if (bfd_link_executable (info)) 463 { 464 s = bfd_make_section_anyway_with_flags (abfd, 465 (bed->rela_plts_and_copies_p 466 ? ".rela.bss" : ".rel.bss"), 467 flags | SEC_READONLY); 468 if (s == NULL 469 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 470 return FALSE; 471 htab->srelbss = s; 472 473 if (bed->want_dynrelro) 474 { 475 s = (bfd_make_section_anyway_with_flags 476 (abfd, (bed->rela_plts_and_copies_p 477 ? ".rela.data.rel.ro" : ".rel.data.rel.ro"), 478 flags | SEC_READONLY)); 479 if (s == NULL 480 || ! bfd_set_section_alignment (abfd, s, 481 bed->s->log_file_align)) 482 return FALSE; 483 htab->sreldynrelro = s; 484 } 485 } 486 } 487 488 return TRUE; 489 } 490 491 /* Record a new dynamic symbol. We record the dynamic symbols as we 492 read the input files, since we need to have a list of all of them 493 before we can determine the final sizes of the output sections. 494 Note that we may actually call this function even though we are not 495 going to output any dynamic symbols; in some cases we know that a 496 symbol should be in the dynamic symbol table, but only if there is 497 one. */ 498 499 bfd_boolean 500 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, 501 struct elf_link_hash_entry *h) 502 { 503 if (h->dynindx == -1) 504 { 505 struct elf_strtab_hash *dynstr; 506 char *p; 507 const char *name; 508 size_t indx; 509 510 /* XXX: The ABI draft says the linker must turn hidden and 511 internal symbols into STB_LOCAL symbols when producing the 512 DSO. However, if ld.so honors st_other in the dynamic table, 513 this would not be necessary. */ 514 switch (ELF_ST_VISIBILITY (h->other)) 515 { 516 case STV_INTERNAL: 517 case STV_HIDDEN: 518 if (h->root.type != bfd_link_hash_undefined 519 && h->root.type != bfd_link_hash_undefweak) 520 { 521 h->forced_local = 1; 522 if (!elf_hash_table (info)->is_relocatable_executable) 523 return TRUE; 524 } 525 526 default: 527 break; 528 } 529 530 h->dynindx = elf_hash_table (info)->dynsymcount; 531 ++elf_hash_table (info)->dynsymcount; 532 533 dynstr = elf_hash_table (info)->dynstr; 534 if (dynstr == NULL) 535 { 536 /* Create a strtab to hold the dynamic symbol names. */ 537 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 538 if (dynstr == NULL) 539 return FALSE; 540 } 541 542 /* We don't put any version information in the dynamic string 543 table. */ 544 name = h->root.root.string; 545 p = strchr (name, ELF_VER_CHR); 546 if (p != NULL) 547 /* We know that the p points into writable memory. In fact, 548 there are only a few symbols that have read-only names, being 549 those like _GLOBAL_OFFSET_TABLE_ that are created specially 550 by the backends. Most symbols will have names pointing into 551 an ELF string table read from a file, or to objalloc memory. */ 552 *p = 0; 553 554 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); 555 556 if (p != NULL) 557 *p = ELF_VER_CHR; 558 559 if (indx == (size_t) -1) 560 return FALSE; 561 h->dynstr_index = indx; 562 } 563 564 return TRUE; 565 } 566 567 /* Mark a symbol dynamic. */ 568 569 static void 570 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, 571 struct elf_link_hash_entry *h, 572 Elf_Internal_Sym *sym) 573 { 574 struct bfd_elf_dynamic_list *d = info->dynamic_list; 575 576 /* It may be called more than once on the same H. */ 577 if(h->dynamic || bfd_link_relocatable (info)) 578 return; 579 580 if ((info->dynamic_data 581 && (h->type == STT_OBJECT 582 || h->type == STT_COMMON 583 || (sym != NULL 584 && (ELF_ST_TYPE (sym->st_info) == STT_OBJECT 585 || ELF_ST_TYPE (sym->st_info) == STT_COMMON)))) 586 || (d != NULL 587 && h->non_elf 588 && (*d->match) (&d->head, NULL, h->root.root.string))) 589 { 590 h->dynamic = 1; 591 /* NB: If a symbol is made dynamic by --dynamic-list, it has 592 non-IR reference. */ 593 h->root.non_ir_ref_dynamic = 1; 594 } 595 } 596 597 /* Record an assignment to a symbol made by a linker script. We need 598 this in case some dynamic object refers to this symbol. */ 599 600 bfd_boolean 601 bfd_elf_record_link_assignment (bfd *output_bfd, 602 struct bfd_link_info *info, 603 const char *name, 604 bfd_boolean provide, 605 bfd_boolean hidden) 606 { 607 struct elf_link_hash_entry *h, *hv; 608 struct elf_link_hash_table *htab; 609 const struct elf_backend_data *bed; 610 611 if (!is_elf_hash_table (info->hash)) 612 return TRUE; 613 614 htab = elf_hash_table (info); 615 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); 616 if (h == NULL) 617 return provide; 618 619 if (h->root.type == bfd_link_hash_warning) 620 h = (struct elf_link_hash_entry *) h->root.u.i.link; 621 622 if (h->versioned == unknown) 623 { 624 /* Set versioned if symbol version is unknown. */ 625 char *version = strrchr (name, ELF_VER_CHR); 626 if (version) 627 { 628 if (version > name && version[-1] != ELF_VER_CHR) 629 h->versioned = versioned_hidden; 630 else 631 h->versioned = versioned; 632 } 633 } 634 635 /* Symbols defined in a linker script but not referenced anywhere 636 else will have non_elf set. */ 637 if (h->non_elf) 638 { 639 bfd_elf_link_mark_dynamic_symbol (info, h, NULL); 640 h->non_elf = 0; 641 } 642 643 switch (h->root.type) 644 { 645 case bfd_link_hash_defined: 646 case bfd_link_hash_defweak: 647 case bfd_link_hash_common: 648 break; 649 case bfd_link_hash_undefweak: 650 case bfd_link_hash_undefined: 651 /* Since we're defining the symbol, don't let it seem to have not 652 been defined. record_dynamic_symbol and size_dynamic_sections 653 may depend on this. */ 654 h->root.type = bfd_link_hash_new; 655 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) 656 bfd_link_repair_undef_list (&htab->root); 657 break; 658 case bfd_link_hash_new: 659 break; 660 case bfd_link_hash_indirect: 661 /* We had a versioned symbol in a dynamic library. We make the 662 the versioned symbol point to this one. */ 663 bed = get_elf_backend_data (output_bfd); 664 hv = h; 665 while (hv->root.type == bfd_link_hash_indirect 666 || hv->root.type == bfd_link_hash_warning) 667 hv = (struct elf_link_hash_entry *) hv->root.u.i.link; 668 /* We don't need to update h->root.u since linker will set them 669 later. */ 670 h->root.type = bfd_link_hash_undefined; 671 hv->root.type = bfd_link_hash_indirect; 672 hv->root.u.i.link = (struct bfd_link_hash_entry *) h; 673 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); 674 break; 675 default: 676 BFD_FAIL (); 677 return FALSE; 678 } 679 680 /* If this symbol is being provided by the linker script, and it is 681 currently defined by a dynamic object, but not by a regular 682 object, then mark it as undefined so that the generic linker will 683 force the correct value. */ 684 if (provide 685 && h->def_dynamic 686 && !h->def_regular) 687 h->root.type = bfd_link_hash_undefined; 688 689 /* If this symbol is not being provided by the linker script, and it is 690 currently defined by a dynamic object, but not by a regular object, 691 then clear out any version information because the symbol will not be 692 associated with the dynamic object any more. */ 693 if (!provide 694 && h->def_dynamic 695 && !h->def_regular) 696 h->verinfo.verdef = NULL; 697 698 /* Make sure this symbol is not garbage collected. */ 699 h->mark = 1; 700 701 h->def_regular = 1; 702 703 if (hidden) 704 { 705 bed = get_elf_backend_data (output_bfd); 706 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 707 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 708 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 709 } 710 711 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects 712 and executables. */ 713 if (!bfd_link_relocatable (info) 714 && h->dynindx != -1 715 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 716 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) 717 h->forced_local = 1; 718 719 if ((h->def_dynamic 720 || h->ref_dynamic 721 || bfd_link_dll (info) 722 || elf_hash_table (info)->is_relocatable_executable) 723 && !h->forced_local 724 && h->dynindx == -1) 725 { 726 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 727 return FALSE; 728 729 /* If this is a weak defined symbol, and we know a corresponding 730 real symbol from the same dynamic object, make sure the real 731 symbol is also made into a dynamic symbol. */ 732 if (h->is_weakalias) 733 { 734 struct elf_link_hash_entry *def = weakdef (h); 735 736 if (def->dynindx == -1 737 && !bfd_elf_link_record_dynamic_symbol (info, def)) 738 return FALSE; 739 } 740 } 741 742 return TRUE; 743 } 744 745 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on 746 success, and 2 on a failure caused by attempting to record a symbol 747 in a discarded section, eg. a discarded link-once section symbol. */ 748 749 int 750 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, 751 bfd *input_bfd, 752 long input_indx) 753 { 754 bfd_size_type amt; 755 struct elf_link_local_dynamic_entry *entry; 756 struct elf_link_hash_table *eht; 757 struct elf_strtab_hash *dynstr; 758 size_t dynstr_index; 759 char *name; 760 Elf_External_Sym_Shndx eshndx; 761 char esym[sizeof (Elf64_External_Sym)]; 762 763 if (! is_elf_hash_table (info->hash)) 764 return 0; 765 766 /* See if the entry exists already. */ 767 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) 768 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) 769 return 1; 770 771 amt = sizeof (*entry); 772 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); 773 if (entry == NULL) 774 return 0; 775 776 /* Go find the symbol, so that we can find it's name. */ 777 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, 778 1, input_indx, &entry->isym, esym, &eshndx)) 779 { 780 bfd_release (input_bfd, entry); 781 return 0; 782 } 783 784 if (entry->isym.st_shndx != SHN_UNDEF 785 && entry->isym.st_shndx < SHN_LORESERVE) 786 { 787 asection *s; 788 789 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); 790 if (s == NULL || bfd_is_abs_section (s->output_section)) 791 { 792 /* We can still bfd_release here as nothing has done another 793 bfd_alloc. We can't do this later in this function. */ 794 bfd_release (input_bfd, entry); 795 return 2; 796 } 797 } 798 799 name = (bfd_elf_string_from_elf_section 800 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, 801 entry->isym.st_name)); 802 803 dynstr = elf_hash_table (info)->dynstr; 804 if (dynstr == NULL) 805 { 806 /* Create a strtab to hold the dynamic symbol names. */ 807 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 808 if (dynstr == NULL) 809 return 0; 810 } 811 812 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); 813 if (dynstr_index == (size_t) -1) 814 return 0; 815 entry->isym.st_name = dynstr_index; 816 817 eht = elf_hash_table (info); 818 819 entry->next = eht->dynlocal; 820 eht->dynlocal = entry; 821 entry->input_bfd = input_bfd; 822 entry->input_indx = input_indx; 823 eht->dynsymcount++; 824 825 /* Whatever binding the symbol had before, it's now local. */ 826 entry->isym.st_info 827 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); 828 829 /* The dynindx will be set at the end of size_dynamic_sections. */ 830 831 return 1; 832 } 833 834 /* Return the dynindex of a local dynamic symbol. */ 835 836 long 837 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, 838 bfd *input_bfd, 839 long input_indx) 840 { 841 struct elf_link_local_dynamic_entry *e; 842 843 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 844 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 845 return e->dynindx; 846 return -1; 847 } 848 849 /* This function is used to renumber the dynamic symbols, if some of 850 them are removed because they are marked as local. This is called 851 via elf_link_hash_traverse. */ 852 853 static bfd_boolean 854 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, 855 void *data) 856 { 857 size_t *count = (size_t *) data; 858 859 if (h->forced_local) 860 return TRUE; 861 862 if (h->dynindx != -1) 863 h->dynindx = ++(*count); 864 865 return TRUE; 866 } 867 868 869 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with 870 STB_LOCAL binding. */ 871 872 static bfd_boolean 873 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, 874 void *data) 875 { 876 size_t *count = (size_t *) data; 877 878 if (!h->forced_local) 879 return TRUE; 880 881 if (h->dynindx != -1) 882 h->dynindx = ++(*count); 883 884 return TRUE; 885 } 886 887 /* Return true if the dynamic symbol for a given section should be 888 omitted when creating a shared library. */ 889 bfd_boolean 890 _bfd_elf_omit_section_dynsym_default (bfd *output_bfd ATTRIBUTE_UNUSED, 891 struct bfd_link_info *info, 892 asection *p) 893 { 894 struct elf_link_hash_table *htab; 895 asection *ip; 896 897 switch (elf_section_data (p)->this_hdr.sh_type) 898 { 899 case SHT_PROGBITS: 900 case SHT_NOBITS: 901 /* If sh_type is yet undecided, assume it could be 902 SHT_PROGBITS/SHT_NOBITS. */ 903 case SHT_NULL: 904 htab = elf_hash_table (info); 905 if (p == htab->tls_sec) 906 return FALSE; 907 908 if (htab->text_index_section != NULL) 909 return p != htab->text_index_section && p != htab->data_index_section; 910 911 return (htab->dynobj != NULL 912 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL 913 && ip->output_section == p); 914 915 /* There shouldn't be section relative relocations 916 against any other section. */ 917 default: 918 return TRUE; 919 } 920 } 921 922 bfd_boolean 923 _bfd_elf_omit_section_dynsym_all 924 (bfd *output_bfd ATTRIBUTE_UNUSED, 925 struct bfd_link_info *info ATTRIBUTE_UNUSED, 926 asection *p ATTRIBUTE_UNUSED) 927 { 928 return TRUE; 929 } 930 931 /* Assign dynsym indices. In a shared library we generate a section 932 symbol for each output section, which come first. Next come symbols 933 which have been forced to local binding. Then all of the back-end 934 allocated local dynamic syms, followed by the rest of the global 935 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. 936 (This prevents the early call before elf_backend_init_index_section 937 and strip_excluded_output_sections setting dynindx for sections 938 that are stripped.) */ 939 940 static unsigned long 941 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, 942 struct bfd_link_info *info, 943 unsigned long *section_sym_count) 944 { 945 unsigned long dynsymcount = 0; 946 bfd_boolean do_sec = section_sym_count != NULL; 947 948 if (bfd_link_pic (info) 949 || elf_hash_table (info)->is_relocatable_executable) 950 { 951 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 952 asection *p; 953 for (p = output_bfd->sections; p ; p = p->next) 954 if ((p->flags & SEC_EXCLUDE) == 0 955 && (p->flags & SEC_ALLOC) != 0 956 && elf_hash_table (info)->dynamic_relocs 957 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) 958 { 959 ++dynsymcount; 960 if (do_sec) 961 elf_section_data (p)->dynindx = dynsymcount; 962 } 963 else if (do_sec) 964 elf_section_data (p)->dynindx = 0; 965 } 966 if (do_sec) 967 *section_sym_count = dynsymcount; 968 969 elf_link_hash_traverse (elf_hash_table (info), 970 elf_link_renumber_local_hash_table_dynsyms, 971 &dynsymcount); 972 973 if (elf_hash_table (info)->dynlocal) 974 { 975 struct elf_link_local_dynamic_entry *p; 976 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 977 p->dynindx = ++dynsymcount; 978 } 979 elf_hash_table (info)->local_dynsymcount = dynsymcount; 980 981 elf_link_hash_traverse (elf_hash_table (info), 982 elf_link_renumber_hash_table_dynsyms, 983 &dynsymcount); 984 985 /* There is an unused NULL entry at the head of the table which we 986 must account for in our count even if the table is empty since it 987 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in 988 .dynamic section. */ 989 dynsymcount++; 990 991 elf_hash_table (info)->dynsymcount = dynsymcount; 992 return dynsymcount; 993 } 994 995 /* Merge st_other field. */ 996 997 static void 998 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, 999 const Elf_Internal_Sym *isym, asection *sec, 1000 bfd_boolean definition, bfd_boolean dynamic) 1001 { 1002 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 1003 1004 /* If st_other has a processor-specific meaning, specific 1005 code might be needed here. */ 1006 if (bed->elf_backend_merge_symbol_attribute) 1007 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, 1008 dynamic); 1009 1010 if (!dynamic) 1011 { 1012 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); 1013 unsigned hvis = ELF_ST_VISIBILITY (h->other); 1014 1015 /* Keep the most constraining visibility. Leave the remainder 1016 of the st_other field to elf_backend_merge_symbol_attribute. */ 1017 if (symvis - 1 < hvis - 1) 1018 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); 1019 } 1020 else if (definition 1021 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT 1022 && (sec->flags & SEC_READONLY) == 0) 1023 h->protected_def = 1; 1024 } 1025 1026 /* This function is called when we want to merge a new symbol with an 1027 existing symbol. It handles the various cases which arise when we 1028 find a definition in a dynamic object, or when there is already a 1029 definition in a dynamic object. The new symbol is described by 1030 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table 1031 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK 1032 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment 1033 of an old common symbol. We set OVERRIDE if the old symbol is 1034 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for 1035 the type to change. We set SIZE_CHANGE_OK if it is OK for the size 1036 to change. By OK to change, we mean that we shouldn't warn if the 1037 type or size does change. */ 1038 1039 static bfd_boolean 1040 _bfd_elf_merge_symbol (bfd *abfd, 1041 struct bfd_link_info *info, 1042 const char *name, 1043 Elf_Internal_Sym *sym, 1044 asection **psec, 1045 bfd_vma *pvalue, 1046 struct elf_link_hash_entry **sym_hash, 1047 bfd **poldbfd, 1048 bfd_boolean *pold_weak, 1049 unsigned int *pold_alignment, 1050 bfd_boolean *skip, 1051 bfd_boolean *override, 1052 bfd_boolean *type_change_ok, 1053 bfd_boolean *size_change_ok, 1054 bfd_boolean *matched) 1055 { 1056 asection *sec, *oldsec; 1057 struct elf_link_hash_entry *h; 1058 struct elf_link_hash_entry *hi; 1059 struct elf_link_hash_entry *flip; 1060 int bind; 1061 bfd *oldbfd; 1062 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; 1063 bfd_boolean newweak, oldweak, newfunc, oldfunc; 1064 const struct elf_backend_data *bed; 1065 char *new_version; 1066 bfd_boolean default_sym = *matched; 1067 1068 *skip = FALSE; 1069 *override = FALSE; 1070 1071 sec = *psec; 1072 bind = ELF_ST_BIND (sym->st_info); 1073 1074 if (! bfd_is_und_section (sec)) 1075 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); 1076 else 1077 h = ((struct elf_link_hash_entry *) 1078 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); 1079 if (h == NULL) 1080 return FALSE; 1081 *sym_hash = h; 1082 1083 bed = get_elf_backend_data (abfd); 1084 1085 /* NEW_VERSION is the symbol version of the new symbol. */ 1086 if (h->versioned != unversioned) 1087 { 1088 /* Symbol version is unknown or versioned. */ 1089 new_version = strrchr (name, ELF_VER_CHR); 1090 if (new_version) 1091 { 1092 if (h->versioned == unknown) 1093 { 1094 if (new_version > name && new_version[-1] != ELF_VER_CHR) 1095 h->versioned = versioned_hidden; 1096 else 1097 h->versioned = versioned; 1098 } 1099 new_version += 1; 1100 if (new_version[0] == '\0') 1101 new_version = NULL; 1102 } 1103 else 1104 h->versioned = unversioned; 1105 } 1106 else 1107 new_version = NULL; 1108 1109 /* For merging, we only care about real symbols. But we need to make 1110 sure that indirect symbol dynamic flags are updated. */ 1111 hi = h; 1112 while (h->root.type == bfd_link_hash_indirect 1113 || h->root.type == bfd_link_hash_warning) 1114 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1115 1116 if (!*matched) 1117 { 1118 if (hi == h || h->root.type == bfd_link_hash_new) 1119 *matched = TRUE; 1120 else 1121 { 1122 /* OLD_HIDDEN is true if the existing symbol is only visible 1123 to the symbol with the same symbol version. NEW_HIDDEN is 1124 true if the new symbol is only visible to the symbol with 1125 the same symbol version. */ 1126 bfd_boolean old_hidden = h->versioned == versioned_hidden; 1127 bfd_boolean new_hidden = hi->versioned == versioned_hidden; 1128 if (!old_hidden && !new_hidden) 1129 /* The new symbol matches the existing symbol if both 1130 aren't hidden. */ 1131 *matched = TRUE; 1132 else 1133 { 1134 /* OLD_VERSION is the symbol version of the existing 1135 symbol. */ 1136 char *old_version; 1137 1138 if (h->versioned >= versioned) 1139 old_version = strrchr (h->root.root.string, 1140 ELF_VER_CHR) + 1; 1141 else 1142 old_version = NULL; 1143 1144 /* The new symbol matches the existing symbol if they 1145 have the same symbol version. */ 1146 *matched = (old_version == new_version 1147 || (old_version != NULL 1148 && new_version != NULL 1149 && strcmp (old_version, new_version) == 0)); 1150 } 1151 } 1152 } 1153 1154 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the 1155 existing symbol. */ 1156 1157 oldbfd = NULL; 1158 oldsec = NULL; 1159 switch (h->root.type) 1160 { 1161 default: 1162 break; 1163 1164 case bfd_link_hash_undefined: 1165 case bfd_link_hash_undefweak: 1166 oldbfd = h->root.u.undef.abfd; 1167 break; 1168 1169 case bfd_link_hash_defined: 1170 case bfd_link_hash_defweak: 1171 oldbfd = h->root.u.def.section->owner; 1172 oldsec = h->root.u.def.section; 1173 break; 1174 1175 case bfd_link_hash_common: 1176 oldbfd = h->root.u.c.p->section->owner; 1177 oldsec = h->root.u.c.p->section; 1178 if (pold_alignment) 1179 *pold_alignment = h->root.u.c.p->alignment_power; 1180 break; 1181 } 1182 if (poldbfd && *poldbfd == NULL) 1183 *poldbfd = oldbfd; 1184 1185 /* Differentiate strong and weak symbols. */ 1186 newweak = bind == STB_WEAK; 1187 oldweak = (h->root.type == bfd_link_hash_defweak 1188 || h->root.type == bfd_link_hash_undefweak); 1189 if (pold_weak) 1190 *pold_weak = oldweak; 1191 1192 /* We have to check it for every instance since the first few may be 1193 references and not all compilers emit symbol type for undefined 1194 symbols. */ 1195 bfd_elf_link_mark_dynamic_symbol (info, h, sym); 1196 1197 /* NEWDYN and OLDDYN indicate whether the new or old symbol, 1198 respectively, is from a dynamic object. */ 1199 1200 newdyn = (abfd->flags & DYNAMIC) != 0; 1201 1202 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined 1203 syms and defined syms in dynamic libraries respectively. 1204 ref_dynamic on the other hand can be set for a symbol defined in 1205 a dynamic library, and def_dynamic may not be set; When the 1206 definition in a dynamic lib is overridden by a definition in the 1207 executable use of the symbol in the dynamic lib becomes a 1208 reference to the executable symbol. */ 1209 if (newdyn) 1210 { 1211 if (bfd_is_und_section (sec)) 1212 { 1213 if (bind != STB_WEAK) 1214 { 1215 h->ref_dynamic_nonweak = 1; 1216 hi->ref_dynamic_nonweak = 1; 1217 } 1218 } 1219 else 1220 { 1221 /* Update the existing symbol only if they match. */ 1222 if (*matched) 1223 h->dynamic_def = 1; 1224 hi->dynamic_def = 1; 1225 } 1226 } 1227 1228 /* If we just created the symbol, mark it as being an ELF symbol. 1229 Other than that, there is nothing to do--there is no merge issue 1230 with a newly defined symbol--so we just return. */ 1231 1232 if (h->root.type == bfd_link_hash_new) 1233 { 1234 h->non_elf = 0; 1235 return TRUE; 1236 } 1237 1238 /* In cases involving weak versioned symbols, we may wind up trying 1239 to merge a symbol with itself. Catch that here, to avoid the 1240 confusion that results if we try to override a symbol with 1241 itself. The additional tests catch cases like 1242 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a 1243 dynamic object, which we do want to handle here. */ 1244 if (abfd == oldbfd 1245 && (newweak || oldweak) 1246 && ((abfd->flags & DYNAMIC) == 0 1247 || !h->def_regular)) 1248 return TRUE; 1249 1250 olddyn = FALSE; 1251 if (oldbfd != NULL) 1252 olddyn = (oldbfd->flags & DYNAMIC) != 0; 1253 else if (oldsec != NULL) 1254 { 1255 /* This handles the special SHN_MIPS_{TEXT,DATA} section 1256 indices used by MIPS ELF. */ 1257 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; 1258 } 1259 1260 /* Handle a case where plugin_notice won't be called and thus won't 1261 set the non_ir_ref flags on the first pass over symbols. */ 1262 if (oldbfd != NULL 1263 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) 1264 && newdyn != olddyn) 1265 { 1266 h->root.non_ir_ref_dynamic = TRUE; 1267 hi->root.non_ir_ref_dynamic = TRUE; 1268 } 1269 1270 /* NEWDEF and OLDDEF indicate whether the new or old symbol, 1271 respectively, appear to be a definition rather than reference. */ 1272 1273 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); 1274 1275 olddef = (h->root.type != bfd_link_hash_undefined 1276 && h->root.type != bfd_link_hash_undefweak 1277 && h->root.type != bfd_link_hash_common); 1278 1279 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, 1280 respectively, appear to be a function. */ 1281 1282 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1283 && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); 1284 1285 oldfunc = (h->type != STT_NOTYPE 1286 && bed->is_function_type (h->type)); 1287 1288 if (!(newfunc && oldfunc) 1289 && ELF_ST_TYPE (sym->st_info) != h->type 1290 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1291 && h->type != STT_NOTYPE 1292 && (newdef || bfd_is_com_section (sec)) 1293 && (olddef || h->root.type == bfd_link_hash_common)) 1294 { 1295 /* If creating a default indirect symbol ("foo" or "foo@") from 1296 a dynamic versioned definition ("foo@@") skip doing so if 1297 there is an existing regular definition with a different 1298 type. We don't want, for example, a "time" variable in the 1299 executable overriding a "time" function in a shared library. */ 1300 if (newdyn 1301 && !olddyn) 1302 { 1303 *skip = TRUE; 1304 return TRUE; 1305 } 1306 1307 /* When adding a symbol from a regular object file after we have 1308 created indirect symbols, undo the indirection and any 1309 dynamic state. */ 1310 if (hi != h 1311 && !newdyn 1312 && olddyn) 1313 { 1314 h = hi; 1315 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1316 h->forced_local = 0; 1317 h->ref_dynamic = 0; 1318 h->def_dynamic = 0; 1319 h->dynamic_def = 0; 1320 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1321 { 1322 h->root.type = bfd_link_hash_undefined; 1323 h->root.u.undef.abfd = abfd; 1324 } 1325 else 1326 { 1327 h->root.type = bfd_link_hash_new; 1328 h->root.u.undef.abfd = NULL; 1329 } 1330 return TRUE; 1331 } 1332 } 1333 1334 /* Check TLS symbols. We don't check undefined symbols introduced 1335 by "ld -u" which have no type (and oldbfd NULL), and we don't 1336 check symbols from plugins because they also have no type. */ 1337 if (oldbfd != NULL 1338 && (oldbfd->flags & BFD_PLUGIN) == 0 1339 && (abfd->flags & BFD_PLUGIN) == 0 1340 && ELF_ST_TYPE (sym->st_info) != h->type 1341 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) 1342 { 1343 bfd *ntbfd, *tbfd; 1344 bfd_boolean ntdef, tdef; 1345 asection *ntsec, *tsec; 1346 1347 if (h->type == STT_TLS) 1348 { 1349 ntbfd = abfd; 1350 ntsec = sec; 1351 ntdef = newdef; 1352 tbfd = oldbfd; 1353 tsec = oldsec; 1354 tdef = olddef; 1355 } 1356 else 1357 { 1358 ntbfd = oldbfd; 1359 ntsec = oldsec; 1360 ntdef = olddef; 1361 tbfd = abfd; 1362 tsec = sec; 1363 tdef = newdef; 1364 } 1365 1366 if (tdef && ntdef) 1367 _bfd_error_handler 1368 /* xgettext:c-format */ 1369 (_("%s: TLS definition in %pB section %pA " 1370 "mismatches non-TLS definition in %pB section %pA"), 1371 h->root.root.string, tbfd, tsec, ntbfd, ntsec); 1372 else if (!tdef && !ntdef) 1373 _bfd_error_handler 1374 /* xgettext:c-format */ 1375 (_("%s: TLS reference in %pB " 1376 "mismatches non-TLS reference in %pB"), 1377 h->root.root.string, tbfd, ntbfd); 1378 else if (tdef) 1379 _bfd_error_handler 1380 /* xgettext:c-format */ 1381 (_("%s: TLS definition in %pB section %pA " 1382 "mismatches non-TLS reference in %pB"), 1383 h->root.root.string, tbfd, tsec, ntbfd); 1384 else 1385 _bfd_error_handler 1386 /* xgettext:c-format */ 1387 (_("%s: TLS reference in %pB " 1388 "mismatches non-TLS definition in %pB section %pA"), 1389 h->root.root.string, tbfd, ntbfd, ntsec); 1390 1391 bfd_set_error (bfd_error_bad_value); 1392 return FALSE; 1393 } 1394 1395 /* If the old symbol has non-default visibility, we ignore the new 1396 definition from a dynamic object. */ 1397 if (newdyn 1398 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 1399 && !bfd_is_und_section (sec)) 1400 { 1401 *skip = TRUE; 1402 /* Make sure this symbol is dynamic. */ 1403 h->ref_dynamic = 1; 1404 hi->ref_dynamic = 1; 1405 /* A protected symbol has external availability. Make sure it is 1406 recorded as dynamic. 1407 1408 FIXME: Should we check type and size for protected symbol? */ 1409 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 1410 return bfd_elf_link_record_dynamic_symbol (info, h); 1411 else 1412 return TRUE; 1413 } 1414 else if (!newdyn 1415 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT 1416 && h->def_dynamic) 1417 { 1418 /* If the new symbol with non-default visibility comes from a 1419 relocatable file and the old definition comes from a dynamic 1420 object, we remove the old definition. */ 1421 if (hi->root.type == bfd_link_hash_indirect) 1422 { 1423 /* Handle the case where the old dynamic definition is 1424 default versioned. We need to copy the symbol info from 1425 the symbol with default version to the normal one if it 1426 was referenced before. */ 1427 if (h->ref_regular) 1428 { 1429 hi->root.type = h->root.type; 1430 h->root.type = bfd_link_hash_indirect; 1431 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); 1432 1433 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1434 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1435 { 1436 /* If the new symbol is hidden or internal, completely undo 1437 any dynamic link state. */ 1438 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1439 h->forced_local = 0; 1440 h->ref_dynamic = 0; 1441 } 1442 else 1443 h->ref_dynamic = 1; 1444 1445 h->def_dynamic = 0; 1446 /* FIXME: Should we check type and size for protected symbol? */ 1447 h->size = 0; 1448 h->type = 0; 1449 1450 h = hi; 1451 } 1452 else 1453 h = hi; 1454 } 1455 1456 /* If the old symbol was undefined before, then it will still be 1457 on the undefs list. If the new symbol is undefined or 1458 common, we can't make it bfd_link_hash_new here, because new 1459 undefined or common symbols will be added to the undefs list 1460 by _bfd_generic_link_add_one_symbol. Symbols may not be 1461 added twice to the undefs list. Also, if the new symbol is 1462 undefweak then we don't want to lose the strong undef. */ 1463 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1464 { 1465 h->root.type = bfd_link_hash_undefined; 1466 h->root.u.undef.abfd = abfd; 1467 } 1468 else 1469 { 1470 h->root.type = bfd_link_hash_new; 1471 h->root.u.undef.abfd = NULL; 1472 } 1473 1474 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1475 { 1476 /* If the new symbol is hidden or internal, completely undo 1477 any dynamic link state. */ 1478 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1479 h->forced_local = 0; 1480 h->ref_dynamic = 0; 1481 } 1482 else 1483 h->ref_dynamic = 1; 1484 h->def_dynamic = 0; 1485 /* FIXME: Should we check type and size for protected symbol? */ 1486 h->size = 0; 1487 h->type = 0; 1488 return TRUE; 1489 } 1490 1491 /* If a new weak symbol definition comes from a regular file and the 1492 old symbol comes from a dynamic library, we treat the new one as 1493 strong. Similarly, an old weak symbol definition from a regular 1494 file is treated as strong when the new symbol comes from a dynamic 1495 library. Further, an old weak symbol from a dynamic library is 1496 treated as strong if the new symbol is from a dynamic library. 1497 This reflects the way glibc's ld.so works. 1498 1499 Also allow a weak symbol to override a linker script symbol 1500 defined by an early pass over the script. This is done so the 1501 linker knows the symbol is defined in an object file, for the 1502 DEFINED script function. 1503 1504 Do this before setting *type_change_ok or *size_change_ok so that 1505 we warn properly when dynamic library symbols are overridden. */ 1506 1507 if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) 1508 newweak = FALSE; 1509 if (olddef && newdyn) 1510 oldweak = FALSE; 1511 1512 /* Allow changes between different types of function symbol. */ 1513 if (newfunc && oldfunc) 1514 *type_change_ok = TRUE; 1515 1516 /* It's OK to change the type if either the existing symbol or the 1517 new symbol is weak. A type change is also OK if the old symbol 1518 is undefined and the new symbol is defined. */ 1519 1520 if (oldweak 1521 || newweak 1522 || (newdef 1523 && h->root.type == bfd_link_hash_undefined)) 1524 *type_change_ok = TRUE; 1525 1526 /* It's OK to change the size if either the existing symbol or the 1527 new symbol is weak, or if the old symbol is undefined. */ 1528 1529 if (*type_change_ok 1530 || h->root.type == bfd_link_hash_undefined) 1531 *size_change_ok = TRUE; 1532 1533 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old 1534 symbol, respectively, appears to be a common symbol in a dynamic 1535 object. If a symbol appears in an uninitialized section, and is 1536 not weak, and is not a function, then it may be a common symbol 1537 which was resolved when the dynamic object was created. We want 1538 to treat such symbols specially, because they raise special 1539 considerations when setting the symbol size: if the symbol 1540 appears as a common symbol in a regular object, and the size in 1541 the regular object is larger, we must make sure that we use the 1542 larger size. This problematic case can always be avoided in C, 1543 but it must be handled correctly when using Fortran shared 1544 libraries. 1545 1546 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and 1547 likewise for OLDDYNCOMMON and OLDDEF. 1548 1549 Note that this test is just a heuristic, and that it is quite 1550 possible to have an uninitialized symbol in a shared object which 1551 is really a definition, rather than a common symbol. This could 1552 lead to some minor confusion when the symbol really is a common 1553 symbol in some regular object. However, I think it will be 1554 harmless. */ 1555 1556 if (newdyn 1557 && newdef 1558 && !newweak 1559 && (sec->flags & SEC_ALLOC) != 0 1560 && (sec->flags & SEC_LOAD) == 0 1561 && sym->st_size > 0 1562 && !newfunc) 1563 newdyncommon = TRUE; 1564 else 1565 newdyncommon = FALSE; 1566 1567 if (olddyn 1568 && olddef 1569 && h->root.type == bfd_link_hash_defined 1570 && h->def_dynamic 1571 && (h->root.u.def.section->flags & SEC_ALLOC) != 0 1572 && (h->root.u.def.section->flags & SEC_LOAD) == 0 1573 && h->size > 0 1574 && !oldfunc) 1575 olddyncommon = TRUE; 1576 else 1577 olddyncommon = FALSE; 1578 1579 /* We now know everything about the old and new symbols. We ask the 1580 backend to check if we can merge them. */ 1581 if (bed->merge_symbol != NULL) 1582 { 1583 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) 1584 return FALSE; 1585 sec = *psec; 1586 } 1587 1588 /* There are multiple definitions of a normal symbol. Skip the 1589 default symbol as well as definition from an IR object. */ 1590 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak 1591 && !default_sym && h->def_regular 1592 && !(oldbfd != NULL 1593 && (oldbfd->flags & BFD_PLUGIN) != 0 1594 && (abfd->flags & BFD_PLUGIN) == 0)) 1595 { 1596 /* Handle a multiple definition. */ 1597 (*info->callbacks->multiple_definition) (info, &h->root, 1598 abfd, sec, *pvalue); 1599 *skip = TRUE; 1600 return TRUE; 1601 } 1602 1603 /* If both the old and the new symbols look like common symbols in a 1604 dynamic object, set the size of the symbol to the larger of the 1605 two. */ 1606 1607 if (olddyncommon 1608 && newdyncommon 1609 && sym->st_size != h->size) 1610 { 1611 /* Since we think we have two common symbols, issue a multiple 1612 common warning if desired. Note that we only warn if the 1613 size is different. If the size is the same, we simply let 1614 the old symbol override the new one as normally happens with 1615 symbols defined in dynamic objects. */ 1616 1617 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1618 bfd_link_hash_common, sym->st_size); 1619 if (sym->st_size > h->size) 1620 h->size = sym->st_size; 1621 1622 *size_change_ok = TRUE; 1623 } 1624 1625 /* If we are looking at a dynamic object, and we have found a 1626 definition, we need to see if the symbol was already defined by 1627 some other object. If so, we want to use the existing 1628 definition, and we do not want to report a multiple symbol 1629 definition error; we do this by clobbering *PSEC to be 1630 bfd_und_section_ptr. 1631 1632 We treat a common symbol as a definition if the symbol in the 1633 shared library is a function, since common symbols always 1634 represent variables; this can cause confusion in principle, but 1635 any such confusion would seem to indicate an erroneous program or 1636 shared library. We also permit a common symbol in a regular 1637 object to override a weak symbol in a shared object. */ 1638 1639 if (newdyn 1640 && newdef 1641 && (olddef 1642 || (h->root.type == bfd_link_hash_common 1643 && (newweak || newfunc)))) 1644 { 1645 *override = TRUE; 1646 newdef = FALSE; 1647 newdyncommon = FALSE; 1648 1649 *psec = sec = bfd_und_section_ptr; 1650 *size_change_ok = TRUE; 1651 1652 /* If we get here when the old symbol is a common symbol, then 1653 we are explicitly letting it override a weak symbol or 1654 function in a dynamic object, and we don't want to warn about 1655 a type change. If the old symbol is a defined symbol, a type 1656 change warning may still be appropriate. */ 1657 1658 if (h->root.type == bfd_link_hash_common) 1659 *type_change_ok = TRUE; 1660 } 1661 1662 /* Handle the special case of an old common symbol merging with a 1663 new symbol which looks like a common symbol in a shared object. 1664 We change *PSEC and *PVALUE to make the new symbol look like a 1665 common symbol, and let _bfd_generic_link_add_one_symbol do the 1666 right thing. */ 1667 1668 if (newdyncommon 1669 && h->root.type == bfd_link_hash_common) 1670 { 1671 *override = TRUE; 1672 newdef = FALSE; 1673 newdyncommon = FALSE; 1674 *pvalue = sym->st_size; 1675 *psec = sec = bed->common_section (oldsec); 1676 *size_change_ok = TRUE; 1677 } 1678 1679 /* Skip weak definitions of symbols that are already defined. */ 1680 if (newdef && olddef && newweak) 1681 { 1682 /* Don't skip new non-IR weak syms. */ 1683 if (!(oldbfd != NULL 1684 && (oldbfd->flags & BFD_PLUGIN) != 0 1685 && (abfd->flags & BFD_PLUGIN) == 0)) 1686 { 1687 newdef = FALSE; 1688 *skip = TRUE; 1689 } 1690 1691 /* Merge st_other. If the symbol already has a dynamic index, 1692 but visibility says it should not be visible, turn it into a 1693 local symbol. */ 1694 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); 1695 if (h->dynindx != -1) 1696 switch (ELF_ST_VISIBILITY (h->other)) 1697 { 1698 case STV_INTERNAL: 1699 case STV_HIDDEN: 1700 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1701 break; 1702 } 1703 } 1704 1705 /* If the old symbol is from a dynamic object, and the new symbol is 1706 a definition which is not from a dynamic object, then the new 1707 symbol overrides the old symbol. Symbols from regular files 1708 always take precedence over symbols from dynamic objects, even if 1709 they are defined after the dynamic object in the link. 1710 1711 As above, we again permit a common symbol in a regular object to 1712 override a definition in a shared object if the shared object 1713 symbol is a function or is weak. */ 1714 1715 flip = NULL; 1716 if (!newdyn 1717 && (newdef 1718 || (bfd_is_com_section (sec) 1719 && (oldweak || oldfunc))) 1720 && olddyn 1721 && olddef 1722 && h->def_dynamic) 1723 { 1724 /* Change the hash table entry to undefined, and let 1725 _bfd_generic_link_add_one_symbol do the right thing with the 1726 new definition. */ 1727 1728 h->root.type = bfd_link_hash_undefined; 1729 h->root.u.undef.abfd = h->root.u.def.section->owner; 1730 *size_change_ok = TRUE; 1731 1732 olddef = FALSE; 1733 olddyncommon = FALSE; 1734 1735 /* We again permit a type change when a common symbol may be 1736 overriding a function. */ 1737 1738 if (bfd_is_com_section (sec)) 1739 { 1740 if (oldfunc) 1741 { 1742 /* If a common symbol overrides a function, make sure 1743 that it isn't defined dynamically nor has type 1744 function. */ 1745 h->def_dynamic = 0; 1746 h->type = STT_NOTYPE; 1747 } 1748 *type_change_ok = TRUE; 1749 } 1750 1751 if (hi->root.type == bfd_link_hash_indirect) 1752 flip = hi; 1753 else 1754 /* This union may have been set to be non-NULL when this symbol 1755 was seen in a dynamic object. We must force the union to be 1756 NULL, so that it is correct for a regular symbol. */ 1757 h->verinfo.vertree = NULL; 1758 } 1759 1760 /* Handle the special case of a new common symbol merging with an 1761 old symbol that looks like it might be a common symbol defined in 1762 a shared object. Note that we have already handled the case in 1763 which a new common symbol should simply override the definition 1764 in the shared library. */ 1765 1766 if (! newdyn 1767 && bfd_is_com_section (sec) 1768 && olddyncommon) 1769 { 1770 /* It would be best if we could set the hash table entry to a 1771 common symbol, but we don't know what to use for the section 1772 or the alignment. */ 1773 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1774 bfd_link_hash_common, sym->st_size); 1775 1776 /* If the presumed common symbol in the dynamic object is 1777 larger, pretend that the new symbol has its size. */ 1778 1779 if (h->size > *pvalue) 1780 *pvalue = h->size; 1781 1782 /* We need to remember the alignment required by the symbol 1783 in the dynamic object. */ 1784 BFD_ASSERT (pold_alignment); 1785 *pold_alignment = h->root.u.def.section->alignment_power; 1786 1787 olddef = FALSE; 1788 olddyncommon = FALSE; 1789 1790 h->root.type = bfd_link_hash_undefined; 1791 h->root.u.undef.abfd = h->root.u.def.section->owner; 1792 1793 *size_change_ok = TRUE; 1794 *type_change_ok = TRUE; 1795 1796 if (hi->root.type == bfd_link_hash_indirect) 1797 flip = hi; 1798 else 1799 h->verinfo.vertree = NULL; 1800 } 1801 1802 if (flip != NULL) 1803 { 1804 /* Handle the case where we had a versioned symbol in a dynamic 1805 library and now find a definition in a normal object. In this 1806 case, we make the versioned symbol point to the normal one. */ 1807 flip->root.type = h->root.type; 1808 flip->root.u.undef.abfd = h->root.u.undef.abfd; 1809 h->root.type = bfd_link_hash_indirect; 1810 h->root.u.i.link = (struct bfd_link_hash_entry *) flip; 1811 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); 1812 if (h->def_dynamic) 1813 { 1814 h->def_dynamic = 0; 1815 flip->ref_dynamic = 1; 1816 } 1817 } 1818 1819 return TRUE; 1820 } 1821 1822 /* This function is called to create an indirect symbol from the 1823 default for the symbol with the default version if needed. The 1824 symbol is described by H, NAME, SYM, SEC, and VALUE. We 1825 set DYNSYM if the new indirect symbol is dynamic. */ 1826 1827 static bfd_boolean 1828 _bfd_elf_add_default_symbol (bfd *abfd, 1829 struct bfd_link_info *info, 1830 struct elf_link_hash_entry *h, 1831 const char *name, 1832 Elf_Internal_Sym *sym, 1833 asection *sec, 1834 bfd_vma value, 1835 bfd **poldbfd, 1836 bfd_boolean *dynsym) 1837 { 1838 bfd_boolean type_change_ok; 1839 bfd_boolean size_change_ok; 1840 bfd_boolean skip; 1841 char *shortname; 1842 struct elf_link_hash_entry *hi; 1843 struct bfd_link_hash_entry *bh; 1844 const struct elf_backend_data *bed; 1845 bfd_boolean collect; 1846 bfd_boolean dynamic; 1847 bfd_boolean override; 1848 char *p; 1849 size_t len, shortlen; 1850 asection *tmp_sec; 1851 bfd_boolean matched; 1852 1853 if (h->versioned == unversioned || h->versioned == versioned_hidden) 1854 return TRUE; 1855 1856 /* If this symbol has a version, and it is the default version, we 1857 create an indirect symbol from the default name to the fully 1858 decorated name. This will cause external references which do not 1859 specify a version to be bound to this version of the symbol. */ 1860 p = strchr (name, ELF_VER_CHR); 1861 if (h->versioned == unknown) 1862 { 1863 if (p == NULL) 1864 { 1865 h->versioned = unversioned; 1866 return TRUE; 1867 } 1868 else 1869 { 1870 if (p[1] != ELF_VER_CHR) 1871 { 1872 h->versioned = versioned_hidden; 1873 return TRUE; 1874 } 1875 else 1876 h->versioned = versioned; 1877 } 1878 } 1879 else 1880 { 1881 /* PR ld/19073: We may see an unversioned definition after the 1882 default version. */ 1883 if (p == NULL) 1884 return TRUE; 1885 } 1886 1887 bed = get_elf_backend_data (abfd); 1888 collect = bed->collect; 1889 dynamic = (abfd->flags & DYNAMIC) != 0; 1890 1891 shortlen = p - name; 1892 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); 1893 if (shortname == NULL) 1894 return FALSE; 1895 memcpy (shortname, name, shortlen); 1896 shortname[shortlen] = '\0'; 1897 1898 /* We are going to create a new symbol. Merge it with any existing 1899 symbol with this name. For the purposes of the merge, act as 1900 though we were defining the symbol we just defined, although we 1901 actually going to define an indirect symbol. */ 1902 type_change_ok = FALSE; 1903 size_change_ok = FALSE; 1904 matched = TRUE; 1905 tmp_sec = sec; 1906 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 1907 &hi, poldbfd, NULL, NULL, &skip, &override, 1908 &type_change_ok, &size_change_ok, &matched)) 1909 return FALSE; 1910 1911 if (skip) 1912 goto nondefault; 1913 1914 if (hi->def_regular) 1915 { 1916 /* If the undecorated symbol will have a version added by a 1917 script different to H, then don't indirect to/from the 1918 undecorated symbol. This isn't ideal because we may not yet 1919 have seen symbol versions, if given by a script on the 1920 command line rather than via --version-script. */ 1921 if (hi->verinfo.vertree == NULL && info->version_info != NULL) 1922 { 1923 bfd_boolean hide; 1924 1925 hi->verinfo.vertree 1926 = bfd_find_version_for_sym (info->version_info, 1927 hi->root.root.string, &hide); 1928 if (hi->verinfo.vertree != NULL && hide) 1929 { 1930 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 1931 goto nondefault; 1932 } 1933 } 1934 if (hi->verinfo.vertree != NULL 1935 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) 1936 goto nondefault; 1937 } 1938 1939 if (! override) 1940 { 1941 /* Add the default symbol if not performing a relocatable link. */ 1942 if (! bfd_link_relocatable (info)) 1943 { 1944 bh = &hi->root; 1945 if (! (_bfd_generic_link_add_one_symbol 1946 (info, abfd, shortname, BSF_INDIRECT, 1947 bfd_ind_section_ptr, 1948 0, name, FALSE, collect, &bh))) 1949 return FALSE; 1950 hi = (struct elf_link_hash_entry *) bh; 1951 } 1952 } 1953 else 1954 { 1955 /* In this case the symbol named SHORTNAME is overriding the 1956 indirect symbol we want to add. We were planning on making 1957 SHORTNAME an indirect symbol referring to NAME. SHORTNAME 1958 is the name without a version. NAME is the fully versioned 1959 name, and it is the default version. 1960 1961 Overriding means that we already saw a definition for the 1962 symbol SHORTNAME in a regular object, and it is overriding 1963 the symbol defined in the dynamic object. 1964 1965 When this happens, we actually want to change NAME, the 1966 symbol we just added, to refer to SHORTNAME. This will cause 1967 references to NAME in the shared object to become references 1968 to SHORTNAME in the regular object. This is what we expect 1969 when we override a function in a shared object: that the 1970 references in the shared object will be mapped to the 1971 definition in the regular object. */ 1972 1973 while (hi->root.type == bfd_link_hash_indirect 1974 || hi->root.type == bfd_link_hash_warning) 1975 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1976 1977 h->root.type = bfd_link_hash_indirect; 1978 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1979 if (h->def_dynamic) 1980 { 1981 h->def_dynamic = 0; 1982 hi->ref_dynamic = 1; 1983 if (hi->ref_regular 1984 || hi->def_regular) 1985 { 1986 if (! bfd_elf_link_record_dynamic_symbol (info, hi)) 1987 return FALSE; 1988 } 1989 } 1990 1991 /* Now set HI to H, so that the following code will set the 1992 other fields correctly. */ 1993 hi = h; 1994 } 1995 1996 /* Check if HI is a warning symbol. */ 1997 if (hi->root.type == bfd_link_hash_warning) 1998 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1999 2000 /* If there is a duplicate definition somewhere, then HI may not 2001 point to an indirect symbol. We will have reported an error to 2002 the user in that case. */ 2003 2004 if (hi->root.type == bfd_link_hash_indirect) 2005 { 2006 struct elf_link_hash_entry *ht; 2007 2008 ht = (struct elf_link_hash_entry *) hi->root.u.i.link; 2009 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); 2010 2011 /* A reference to the SHORTNAME symbol from a dynamic library 2012 will be satisfied by the versioned symbol at runtime. In 2013 effect, we have a reference to the versioned symbol. */ 2014 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 2015 hi->dynamic_def |= ht->dynamic_def; 2016 2017 /* See if the new flags lead us to realize that the symbol must 2018 be dynamic. */ 2019 if (! *dynsym) 2020 { 2021 if (! dynamic) 2022 { 2023 if (! bfd_link_executable (info) 2024 || hi->def_dynamic 2025 || hi->ref_dynamic) 2026 *dynsym = TRUE; 2027 } 2028 else 2029 { 2030 if (hi->ref_regular) 2031 *dynsym = TRUE; 2032 } 2033 } 2034 } 2035 2036 /* We also need to define an indirection from the nondefault version 2037 of the symbol. */ 2038 2039 nondefault: 2040 len = strlen (name); 2041 shortname = (char *) bfd_hash_allocate (&info->hash->table, len); 2042 if (shortname == NULL) 2043 return FALSE; 2044 memcpy (shortname, name, shortlen); 2045 memcpy (shortname + shortlen, p + 1, len - shortlen); 2046 2047 /* Once again, merge with any existing symbol. */ 2048 type_change_ok = FALSE; 2049 size_change_ok = FALSE; 2050 tmp_sec = sec; 2051 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 2052 &hi, poldbfd, NULL, NULL, &skip, &override, 2053 &type_change_ok, &size_change_ok, &matched)) 2054 return FALSE; 2055 2056 if (skip) 2057 return TRUE; 2058 2059 if (override) 2060 { 2061 /* Here SHORTNAME is a versioned name, so we don't expect to see 2062 the type of override we do in the case above unless it is 2063 overridden by a versioned definition. */ 2064 if (hi->root.type != bfd_link_hash_defined 2065 && hi->root.type != bfd_link_hash_defweak) 2066 _bfd_error_handler 2067 /* xgettext:c-format */ 2068 (_("%pB: unexpected redefinition of indirect versioned symbol `%s'"), 2069 abfd, shortname); 2070 } 2071 else 2072 { 2073 bh = &hi->root; 2074 if (! (_bfd_generic_link_add_one_symbol 2075 (info, abfd, shortname, BSF_INDIRECT, 2076 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) 2077 return FALSE; 2078 hi = (struct elf_link_hash_entry *) bh; 2079 2080 /* If there is a duplicate definition somewhere, then HI may not 2081 point to an indirect symbol. We will have reported an error 2082 to the user in that case. */ 2083 2084 if (hi->root.type == bfd_link_hash_indirect) 2085 { 2086 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 2087 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 2088 hi->dynamic_def |= h->dynamic_def; 2089 2090 /* See if the new flags lead us to realize that the symbol 2091 must be dynamic. */ 2092 if (! *dynsym) 2093 { 2094 if (! dynamic) 2095 { 2096 if (! bfd_link_executable (info) 2097 || hi->ref_dynamic) 2098 *dynsym = TRUE; 2099 } 2100 else 2101 { 2102 if (hi->ref_regular) 2103 *dynsym = TRUE; 2104 } 2105 } 2106 } 2107 } 2108 2109 return TRUE; 2110 } 2111 2112 /* This routine is used to export all defined symbols into the dynamic 2113 symbol table. It is called via elf_link_hash_traverse. */ 2114 2115 static bfd_boolean 2116 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) 2117 { 2118 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2119 2120 /* Ignore indirect symbols. These are added by the versioning code. */ 2121 if (h->root.type == bfd_link_hash_indirect) 2122 return TRUE; 2123 2124 /* Ignore this if we won't export it. */ 2125 if (!eif->info->export_dynamic && !h->dynamic) 2126 return TRUE; 2127 2128 if (h->dynindx == -1 2129 && (h->def_regular || h->ref_regular) 2130 && ! bfd_hide_sym_by_version (eif->info->version_info, 2131 h->root.root.string)) 2132 { 2133 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2134 { 2135 eif->failed = TRUE; 2136 return FALSE; 2137 } 2138 } 2139 2140 return TRUE; 2141 } 2142 2143 /* Look through the symbols which are defined in other shared 2144 libraries and referenced here. Update the list of version 2145 dependencies. This will be put into the .gnu.version_r section. 2146 This function is called via elf_link_hash_traverse. */ 2147 2148 static bfd_boolean 2149 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, 2150 void *data) 2151 { 2152 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; 2153 Elf_Internal_Verneed *t; 2154 Elf_Internal_Vernaux *a; 2155 bfd_size_type amt; 2156 2157 /* We only care about symbols defined in shared objects with version 2158 information. */ 2159 if (!h->def_dynamic 2160 || h->def_regular 2161 || h->dynindx == -1 2162 || h->verinfo.verdef == NULL 2163 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 2164 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 2165 return TRUE; 2166 2167 /* See if we already know about this version. */ 2168 for (t = elf_tdata (rinfo->info->output_bfd)->verref; 2169 t != NULL; 2170 t = t->vn_nextref) 2171 { 2172 if (t->vn_bfd != h->verinfo.verdef->vd_bfd) 2173 continue; 2174 2175 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2176 if (a->vna_nodename == h->verinfo.verdef->vd_nodename) 2177 return TRUE; 2178 2179 break; 2180 } 2181 2182 /* This is a new version. Add it to tree we are building. */ 2183 2184 if (t == NULL) 2185 { 2186 amt = sizeof *t; 2187 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); 2188 if (t == NULL) 2189 { 2190 rinfo->failed = TRUE; 2191 return FALSE; 2192 } 2193 2194 t->vn_bfd = h->verinfo.verdef->vd_bfd; 2195 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; 2196 elf_tdata (rinfo->info->output_bfd)->verref = t; 2197 } 2198 2199 amt = sizeof *a; 2200 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); 2201 if (a == NULL) 2202 { 2203 rinfo->failed = TRUE; 2204 return FALSE; 2205 } 2206 2207 /* Note that we are copying a string pointer here, and testing it 2208 above. If bfd_elf_string_from_elf_section is ever changed to 2209 discard the string data when low in memory, this will have to be 2210 fixed. */ 2211 a->vna_nodename = h->verinfo.verdef->vd_nodename; 2212 2213 a->vna_flags = h->verinfo.verdef->vd_flags; 2214 a->vna_nextptr = t->vn_auxptr; 2215 2216 h->verinfo.verdef->vd_exp_refno = rinfo->vers; 2217 ++rinfo->vers; 2218 2219 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; 2220 2221 t->vn_auxptr = a; 2222 2223 return TRUE; 2224 } 2225 2226 /* Return TRUE and set *HIDE to TRUE if the versioned symbol is 2227 hidden. Set *T_P to NULL if there is no match. */ 2228 2229 static bfd_boolean 2230 _bfd_elf_link_hide_versioned_symbol (struct bfd_link_info *info, 2231 struct elf_link_hash_entry *h, 2232 const char *version_p, 2233 struct bfd_elf_version_tree **t_p, 2234 bfd_boolean *hide) 2235 { 2236 struct bfd_elf_version_tree *t; 2237 2238 /* Look for the version. If we find it, it is no longer weak. */ 2239 for (t = info->version_info; t != NULL; t = t->next) 2240 { 2241 if (strcmp (t->name, version_p) == 0) 2242 { 2243 size_t len; 2244 char *alc; 2245 struct bfd_elf_version_expr *d; 2246 2247 len = version_p - h->root.root.string; 2248 alc = (char *) bfd_malloc (len); 2249 if (alc == NULL) 2250 return FALSE; 2251 memcpy (alc, h->root.root.string, len - 1); 2252 alc[len - 1] = '\0'; 2253 if (alc[len - 2] == ELF_VER_CHR) 2254 alc[len - 2] = '\0'; 2255 2256 h->verinfo.vertree = t; 2257 t->used = TRUE; 2258 d = NULL; 2259 2260 if (t->globals.list != NULL) 2261 d = (*t->match) (&t->globals, NULL, alc); 2262 2263 /* See if there is anything to force this symbol to 2264 local scope. */ 2265 if (d == NULL && t->locals.list != NULL) 2266 { 2267 d = (*t->match) (&t->locals, NULL, alc); 2268 if (d != NULL 2269 && h->dynindx != -1 2270 && ! info->export_dynamic) 2271 *hide = TRUE; 2272 } 2273 2274 free (alc); 2275 break; 2276 } 2277 } 2278 2279 *t_p = t; 2280 2281 return TRUE; 2282 } 2283 2284 /* Return TRUE if the symbol H is hidden by version script. */ 2285 2286 bfd_boolean 2287 _bfd_elf_link_hide_sym_by_version (struct bfd_link_info *info, 2288 struct elf_link_hash_entry *h) 2289 { 2290 const char *p; 2291 bfd_boolean hide = FALSE; 2292 const struct elf_backend_data *bed 2293 = get_elf_backend_data (info->output_bfd); 2294 2295 /* Version script only hides symbols defined in regular objects. */ 2296 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 2297 return TRUE; 2298 2299 p = strchr (h->root.root.string, ELF_VER_CHR); 2300 if (p != NULL && h->verinfo.vertree == NULL) 2301 { 2302 struct bfd_elf_version_tree *t; 2303 2304 ++p; 2305 if (*p == ELF_VER_CHR) 2306 ++p; 2307 2308 if (*p != '\0' 2309 && _bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide) 2310 && hide) 2311 { 2312 if (hide) 2313 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2314 return TRUE; 2315 } 2316 } 2317 2318 /* If we don't have a version for this symbol, see if we can find 2319 something. */ 2320 if (h->verinfo.vertree == NULL && info->version_info != NULL) 2321 { 2322 h->verinfo.vertree 2323 = bfd_find_version_for_sym (info->version_info, 2324 h->root.root.string, &hide); 2325 if (h->verinfo.vertree != NULL && hide) 2326 { 2327 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2328 return TRUE; 2329 } 2330 } 2331 2332 return FALSE; 2333 } 2334 2335 /* Figure out appropriate versions for all the symbols. We may not 2336 have the version number script until we have read all of the input 2337 files, so until that point we don't know which symbols should be 2338 local. This function is called via elf_link_hash_traverse. */ 2339 2340 static bfd_boolean 2341 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) 2342 { 2343 struct elf_info_failed *sinfo; 2344 struct bfd_link_info *info; 2345 const struct elf_backend_data *bed; 2346 struct elf_info_failed eif; 2347 char *p; 2348 bfd_boolean hide; 2349 2350 sinfo = (struct elf_info_failed *) data; 2351 info = sinfo->info; 2352 2353 /* Fix the symbol flags. */ 2354 eif.failed = FALSE; 2355 eif.info = info; 2356 if (! _bfd_elf_fix_symbol_flags (h, &eif)) 2357 { 2358 if (eif.failed) 2359 sinfo->failed = TRUE; 2360 return FALSE; 2361 } 2362 2363 /* We only need version numbers for symbols defined in regular 2364 objects. */ 2365 if (!h->def_regular) 2366 return TRUE; 2367 2368 hide = FALSE; 2369 bed = get_elf_backend_data (info->output_bfd); 2370 p = strchr (h->root.root.string, ELF_VER_CHR); 2371 if (p != NULL && h->verinfo.vertree == NULL) 2372 { 2373 struct bfd_elf_version_tree *t; 2374 2375 ++p; 2376 if (*p == ELF_VER_CHR) 2377 ++p; 2378 2379 /* If there is no version string, we can just return out. */ 2380 if (*p == '\0') 2381 return TRUE; 2382 2383 if (!_bfd_elf_link_hide_versioned_symbol (info, h, p, &t, &hide)) 2384 { 2385 sinfo->failed = TRUE; 2386 return FALSE; 2387 } 2388 2389 if (hide) 2390 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2391 2392 /* If we are building an application, we need to create a 2393 version node for this version. */ 2394 if (t == NULL && bfd_link_executable (info)) 2395 { 2396 struct bfd_elf_version_tree **pp; 2397 int version_index; 2398 2399 /* If we aren't going to export this symbol, we don't need 2400 to worry about it. */ 2401 if (h->dynindx == -1) 2402 return TRUE; 2403 2404 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, 2405 sizeof *t); 2406 if (t == NULL) 2407 { 2408 sinfo->failed = TRUE; 2409 return FALSE; 2410 } 2411 2412 t->name = p; 2413 t->name_indx = (unsigned int) -1; 2414 t->used = TRUE; 2415 2416 version_index = 1; 2417 /* Don't count anonymous version tag. */ 2418 if (sinfo->info->version_info != NULL 2419 && sinfo->info->version_info->vernum == 0) 2420 version_index = 0; 2421 for (pp = &sinfo->info->version_info; 2422 *pp != NULL; 2423 pp = &(*pp)->next) 2424 ++version_index; 2425 t->vernum = version_index; 2426 2427 *pp = t; 2428 2429 h->verinfo.vertree = t; 2430 } 2431 else if (t == NULL) 2432 { 2433 /* We could not find the version for a symbol when 2434 generating a shared archive. Return an error. */ 2435 _bfd_error_handler 2436 /* xgettext:c-format */ 2437 (_("%pB: version node not found for symbol %s"), 2438 info->output_bfd, h->root.root.string); 2439 bfd_set_error (bfd_error_bad_value); 2440 sinfo->failed = TRUE; 2441 return FALSE; 2442 } 2443 } 2444 2445 /* If we don't have a version for this symbol, see if we can find 2446 something. */ 2447 if (!hide 2448 && h->verinfo.vertree == NULL 2449 && sinfo->info->version_info != NULL) 2450 { 2451 h->verinfo.vertree 2452 = bfd_find_version_for_sym (sinfo->info->version_info, 2453 h->root.root.string, &hide); 2454 if (h->verinfo.vertree != NULL && hide) 2455 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2456 } 2457 2458 return TRUE; 2459 } 2460 2461 /* Read and swap the relocs from the section indicated by SHDR. This 2462 may be either a REL or a RELA section. The relocations are 2463 translated into RELA relocations and stored in INTERNAL_RELOCS, 2464 which should have already been allocated to contain enough space. 2465 The EXTERNAL_RELOCS are a buffer where the external form of the 2466 relocations should be stored. 2467 2468 Returns FALSE if something goes wrong. */ 2469 2470 static bfd_boolean 2471 elf_link_read_relocs_from_section (bfd *abfd, 2472 asection *sec, 2473 Elf_Internal_Shdr *shdr, 2474 void *external_relocs, 2475 Elf_Internal_Rela *internal_relocs) 2476 { 2477 const struct elf_backend_data *bed; 2478 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 2479 const bfd_byte *erela; 2480 const bfd_byte *erelaend; 2481 Elf_Internal_Rela *irela; 2482 Elf_Internal_Shdr *symtab_hdr; 2483 size_t nsyms; 2484 2485 /* Position ourselves at the start of the section. */ 2486 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) 2487 return FALSE; 2488 2489 /* Read the relocations. */ 2490 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) 2491 return FALSE; 2492 2493 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2494 nsyms = NUM_SHDR_ENTRIES (symtab_hdr); 2495 2496 bed = get_elf_backend_data (abfd); 2497 2498 /* Convert the external relocations to the internal format. */ 2499 if (shdr->sh_entsize == bed->s->sizeof_rel) 2500 swap_in = bed->s->swap_reloc_in; 2501 else if (shdr->sh_entsize == bed->s->sizeof_rela) 2502 swap_in = bed->s->swap_reloca_in; 2503 else 2504 { 2505 bfd_set_error (bfd_error_wrong_format); 2506 return FALSE; 2507 } 2508 2509 erela = (const bfd_byte *) external_relocs; 2510 erelaend = erela + shdr->sh_size; 2511 irela = internal_relocs; 2512 while (erela < erelaend) 2513 { 2514 bfd_vma r_symndx; 2515 2516 (*swap_in) (abfd, erela, irela); 2517 r_symndx = ELF32_R_SYM (irela->r_info); 2518 if (bed->s->arch_size == 64) 2519 r_symndx >>= 24; 2520 if (nsyms > 0) 2521 { 2522 if ((size_t) r_symndx >= nsyms) 2523 { 2524 _bfd_error_handler 2525 /* xgettext:c-format */ 2526 (_("%pB: bad reloc symbol index (%#" PRIx64 " >= %#lx)" 2527 " for offset %#" PRIx64 " in section `%pA'"), 2528 abfd, (uint64_t) r_symndx, (unsigned long) nsyms, 2529 (uint64_t) irela->r_offset, sec); 2530 bfd_set_error (bfd_error_bad_value); 2531 return FALSE; 2532 } 2533 } 2534 else if (r_symndx != STN_UNDEF) 2535 { 2536 _bfd_error_handler 2537 /* xgettext:c-format */ 2538 (_("%pB: non-zero symbol index (%#" PRIx64 ")" 2539 " for offset %#" PRIx64 " in section `%pA'" 2540 " when the object file has no symbol table"), 2541 abfd, (uint64_t) r_symndx, 2542 (uint64_t) irela->r_offset, sec); 2543 bfd_set_error (bfd_error_bad_value); 2544 return FALSE; 2545 } 2546 irela += bed->s->int_rels_per_ext_rel; 2547 erela += shdr->sh_entsize; 2548 } 2549 2550 return TRUE; 2551 } 2552 2553 /* Read and swap the relocs for a section O. They may have been 2554 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are 2555 not NULL, they are used as buffers to read into. They are known to 2556 be large enough. If the INTERNAL_RELOCS relocs argument is NULL, 2557 the return value is allocated using either malloc or bfd_alloc, 2558 according to the KEEP_MEMORY argument. If O has two relocation 2559 sections (both REL and RELA relocations), then the REL_HDR 2560 relocations will appear first in INTERNAL_RELOCS, followed by the 2561 RELA_HDR relocations. */ 2562 2563 Elf_Internal_Rela * 2564 _bfd_elf_link_read_relocs (bfd *abfd, 2565 asection *o, 2566 void *external_relocs, 2567 Elf_Internal_Rela *internal_relocs, 2568 bfd_boolean keep_memory) 2569 { 2570 void *alloc1 = NULL; 2571 Elf_Internal_Rela *alloc2 = NULL; 2572 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2573 struct bfd_elf_section_data *esdo = elf_section_data (o); 2574 Elf_Internal_Rela *internal_rela_relocs; 2575 2576 if (esdo->relocs != NULL) 2577 return esdo->relocs; 2578 2579 if (o->reloc_count == 0) 2580 return NULL; 2581 2582 if (internal_relocs == NULL) 2583 { 2584 bfd_size_type size; 2585 2586 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); 2587 if (keep_memory) 2588 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); 2589 else 2590 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); 2591 if (internal_relocs == NULL) 2592 goto error_return; 2593 } 2594 2595 if (external_relocs == NULL) 2596 { 2597 bfd_size_type size = 0; 2598 2599 if (esdo->rel.hdr) 2600 size += esdo->rel.hdr->sh_size; 2601 if (esdo->rela.hdr) 2602 size += esdo->rela.hdr->sh_size; 2603 2604 alloc1 = bfd_malloc (size); 2605 if (alloc1 == NULL) 2606 goto error_return; 2607 external_relocs = alloc1; 2608 } 2609 2610 internal_rela_relocs = internal_relocs; 2611 if (esdo->rel.hdr) 2612 { 2613 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, 2614 external_relocs, 2615 internal_relocs)) 2616 goto error_return; 2617 external_relocs = (((bfd_byte *) external_relocs) 2618 + esdo->rel.hdr->sh_size); 2619 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) 2620 * bed->s->int_rels_per_ext_rel); 2621 } 2622 2623 if (esdo->rela.hdr 2624 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, 2625 external_relocs, 2626 internal_rela_relocs))) 2627 goto error_return; 2628 2629 /* Cache the results for next time, if we can. */ 2630 if (keep_memory) 2631 esdo->relocs = internal_relocs; 2632 2633 if (alloc1 != NULL) 2634 free (alloc1); 2635 2636 /* Don't free alloc2, since if it was allocated we are passing it 2637 back (under the name of internal_relocs). */ 2638 2639 return internal_relocs; 2640 2641 error_return: 2642 if (alloc1 != NULL) 2643 free (alloc1); 2644 if (alloc2 != NULL) 2645 { 2646 if (keep_memory) 2647 bfd_release (abfd, alloc2); 2648 else 2649 free (alloc2); 2650 } 2651 return NULL; 2652 } 2653 2654 /* Compute the size of, and allocate space for, REL_HDR which is the 2655 section header for a section containing relocations for O. */ 2656 2657 static bfd_boolean 2658 _bfd_elf_link_size_reloc_section (bfd *abfd, 2659 struct bfd_elf_section_reloc_data *reldata) 2660 { 2661 Elf_Internal_Shdr *rel_hdr = reldata->hdr; 2662 2663 /* That allows us to calculate the size of the section. */ 2664 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; 2665 2666 /* The contents field must last into write_object_contents, so we 2667 allocate it with bfd_alloc rather than malloc. Also since we 2668 cannot be sure that the contents will actually be filled in, 2669 we zero the allocated space. */ 2670 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); 2671 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) 2672 return FALSE; 2673 2674 if (reldata->hashes == NULL && reldata->count) 2675 { 2676 struct elf_link_hash_entry **p; 2677 2678 p = ((struct elf_link_hash_entry **) 2679 bfd_zmalloc (reldata->count * sizeof (*p))); 2680 if (p == NULL) 2681 return FALSE; 2682 2683 reldata->hashes = p; 2684 } 2685 2686 return TRUE; 2687 } 2688 2689 /* Copy the relocations indicated by the INTERNAL_RELOCS (which 2690 originated from the section given by INPUT_REL_HDR) to the 2691 OUTPUT_BFD. */ 2692 2693 bfd_boolean 2694 _bfd_elf_link_output_relocs (bfd *output_bfd, 2695 asection *input_section, 2696 Elf_Internal_Shdr *input_rel_hdr, 2697 Elf_Internal_Rela *internal_relocs, 2698 struct elf_link_hash_entry **rel_hash 2699 ATTRIBUTE_UNUSED) 2700 { 2701 Elf_Internal_Rela *irela; 2702 Elf_Internal_Rela *irelaend; 2703 bfd_byte *erel; 2704 struct bfd_elf_section_reloc_data *output_reldata; 2705 asection *output_section; 2706 const struct elf_backend_data *bed; 2707 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 2708 struct bfd_elf_section_data *esdo; 2709 2710 output_section = input_section->output_section; 2711 2712 bed = get_elf_backend_data (output_bfd); 2713 esdo = elf_section_data (output_section); 2714 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2715 { 2716 output_reldata = &esdo->rel; 2717 swap_out = bed->s->swap_reloc_out; 2718 } 2719 else if (esdo->rela.hdr 2720 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2721 { 2722 output_reldata = &esdo->rela; 2723 swap_out = bed->s->swap_reloca_out; 2724 } 2725 else 2726 { 2727 _bfd_error_handler 2728 /* xgettext:c-format */ 2729 (_("%pB: relocation size mismatch in %pB section %pA"), 2730 output_bfd, input_section->owner, input_section); 2731 bfd_set_error (bfd_error_wrong_format); 2732 return FALSE; 2733 } 2734 2735 erel = output_reldata->hdr->contents; 2736 erel += output_reldata->count * input_rel_hdr->sh_entsize; 2737 irela = internal_relocs; 2738 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) 2739 * bed->s->int_rels_per_ext_rel); 2740 while (irela < irelaend) 2741 { 2742 (*swap_out) (output_bfd, irela, erel); 2743 irela += bed->s->int_rels_per_ext_rel; 2744 erel += input_rel_hdr->sh_entsize; 2745 } 2746 2747 /* Bump the counter, so that we know where to add the next set of 2748 relocations. */ 2749 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); 2750 2751 return TRUE; 2752 } 2753 2754 /* Make weak undefined symbols in PIE dynamic. */ 2755 2756 bfd_boolean 2757 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, 2758 struct elf_link_hash_entry *h) 2759 { 2760 if (bfd_link_pie (info) 2761 && h->dynindx == -1 2762 && h->root.type == bfd_link_hash_undefweak) 2763 return bfd_elf_link_record_dynamic_symbol (info, h); 2764 2765 return TRUE; 2766 } 2767 2768 /* Fix up the flags for a symbol. This handles various cases which 2769 can only be fixed after all the input files are seen. This is 2770 currently called by both adjust_dynamic_symbol and 2771 assign_sym_version, which is unnecessary but perhaps more robust in 2772 the face of future changes. */ 2773 2774 static bfd_boolean 2775 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, 2776 struct elf_info_failed *eif) 2777 { 2778 const struct elf_backend_data *bed; 2779 2780 /* If this symbol was mentioned in a non-ELF file, try to set 2781 DEF_REGULAR and REF_REGULAR correctly. This is the only way to 2782 permit a non-ELF file to correctly refer to a symbol defined in 2783 an ELF dynamic object. */ 2784 if (h->non_elf) 2785 { 2786 while (h->root.type == bfd_link_hash_indirect) 2787 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2788 2789 if (h->root.type != bfd_link_hash_defined 2790 && h->root.type != bfd_link_hash_defweak) 2791 { 2792 h->ref_regular = 1; 2793 h->ref_regular_nonweak = 1; 2794 } 2795 else 2796 { 2797 if (h->root.u.def.section->owner != NULL 2798 && (bfd_get_flavour (h->root.u.def.section->owner) 2799 == bfd_target_elf_flavour)) 2800 { 2801 h->ref_regular = 1; 2802 h->ref_regular_nonweak = 1; 2803 } 2804 else 2805 h->def_regular = 1; 2806 } 2807 2808 if (h->dynindx == -1 2809 && (h->def_dynamic 2810 || h->ref_dynamic)) 2811 { 2812 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2813 { 2814 eif->failed = TRUE; 2815 return FALSE; 2816 } 2817 } 2818 } 2819 else 2820 { 2821 /* Unfortunately, NON_ELF is only correct if the symbol 2822 was first seen in a non-ELF file. Fortunately, if the symbol 2823 was first seen in an ELF file, we're probably OK unless the 2824 symbol was defined in a non-ELF file. Catch that case here. 2825 FIXME: We're still in trouble if the symbol was first seen in 2826 a dynamic object, and then later in a non-ELF regular object. */ 2827 if ((h->root.type == bfd_link_hash_defined 2828 || h->root.type == bfd_link_hash_defweak) 2829 && !h->def_regular 2830 && (h->root.u.def.section->owner != NULL 2831 ? (bfd_get_flavour (h->root.u.def.section->owner) 2832 != bfd_target_elf_flavour) 2833 : (bfd_is_abs_section (h->root.u.def.section) 2834 && !h->def_dynamic))) 2835 h->def_regular = 1; 2836 } 2837 2838 /* Backend specific symbol fixup. */ 2839 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 2840 if (bed->elf_backend_fixup_symbol 2841 && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) 2842 return FALSE; 2843 2844 /* If this is a final link, and the symbol was defined as a common 2845 symbol in a regular object file, and there was no definition in 2846 any dynamic object, then the linker will have allocated space for 2847 the symbol in a common section but the DEF_REGULAR 2848 flag will not have been set. */ 2849 if (h->root.type == bfd_link_hash_defined 2850 && !h->def_regular 2851 && h->ref_regular 2852 && !h->def_dynamic 2853 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) 2854 h->def_regular = 1; 2855 2856 /* Symbols defined in discarded sections shouldn't be dynamic. */ 2857 if (h->root.type == bfd_link_hash_undefined && h->indx == -3) 2858 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2859 2860 /* If a weak undefined symbol has non-default visibility, we also 2861 hide it from the dynamic linker. */ 2862 else if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2863 && h->root.type == bfd_link_hash_undefweak) 2864 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2865 2866 /* A hidden versioned symbol in executable should be forced local if 2867 it is is locally defined, not referenced by shared library and not 2868 exported. */ 2869 else if (bfd_link_executable (eif->info) 2870 && h->versioned == versioned_hidden 2871 && !eif->info->export_dynamic 2872 && !h->dynamic 2873 && !h->ref_dynamic 2874 && h->def_regular) 2875 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2876 2877 /* If -Bsymbolic was used (which means to bind references to global 2878 symbols to the definition within the shared object), and this 2879 symbol was defined in a regular object, then it actually doesn't 2880 need a PLT entry. Likewise, if the symbol has non-default 2881 visibility. If the symbol has hidden or internal visibility, we 2882 will force it local. */ 2883 else if (h->needs_plt 2884 && bfd_link_pic (eif->info) 2885 && is_elf_hash_table (eif->info->hash) 2886 && (SYMBOLIC_BIND (eif->info, h) 2887 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2888 && h->def_regular) 2889 { 2890 bfd_boolean force_local; 2891 2892 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 2893 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); 2894 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); 2895 } 2896 2897 /* If this is a weak defined symbol in a dynamic object, and we know 2898 the real definition in the dynamic object, copy interesting flags 2899 over to the real definition. */ 2900 if (h->is_weakalias) 2901 { 2902 struct elf_link_hash_entry *def = weakdef (h); 2903 while (def->root.type == bfd_link_hash_indirect) 2904 def = (struct elf_link_hash_entry *) def->root.u.i.link; 2905 2906 /* If the real definition is defined by a regular object file, 2907 don't do anything special. See the longer description in 2908 _bfd_elf_adjust_dynamic_symbol, below. */ 2909 if (def->def_regular) 2910 { 2911 h = def; 2912 while ((h = h->u.alias) != def) 2913 h->is_weakalias = 0; 2914 } 2915 else 2916 { 2917 while (h->root.type == bfd_link_hash_indirect) 2918 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2919 BFD_ASSERT (h->root.type == bfd_link_hash_defined 2920 || h->root.type == bfd_link_hash_defweak); 2921 BFD_ASSERT (def->def_dynamic); 2922 BFD_ASSERT (def->root.type == bfd_link_hash_defined); 2923 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); 2924 } 2925 } 2926 2927 return TRUE; 2928 } 2929 2930 /* Make the backend pick a good value for a dynamic symbol. This is 2931 called via elf_link_hash_traverse, and also calls itself 2932 recursively. */ 2933 2934 static bfd_boolean 2935 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) 2936 { 2937 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2938 struct elf_link_hash_table *htab; 2939 const struct elf_backend_data *bed; 2940 2941 if (! is_elf_hash_table (eif->info->hash)) 2942 return FALSE; 2943 2944 /* Ignore indirect symbols. These are added by the versioning code. */ 2945 if (h->root.type == bfd_link_hash_indirect) 2946 return TRUE; 2947 2948 /* Fix the symbol flags. */ 2949 if (! _bfd_elf_fix_symbol_flags (h, eif)) 2950 return FALSE; 2951 2952 htab = elf_hash_table (eif->info); 2953 bed = get_elf_backend_data (htab->dynobj); 2954 2955 if (h->root.type == bfd_link_hash_undefweak) 2956 { 2957 if (eif->info->dynamic_undefined_weak == 0) 2958 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2959 else if (eif->info->dynamic_undefined_weak > 0 2960 && h->ref_regular 2961 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 2962 && !bfd_hide_sym_by_version (eif->info->version_info, 2963 h->root.root.string)) 2964 { 2965 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2966 { 2967 eif->failed = TRUE; 2968 return FALSE; 2969 } 2970 } 2971 } 2972 2973 /* If this symbol does not require a PLT entry, and it is not 2974 defined by a dynamic object, or is not referenced by a regular 2975 object, ignore it. We do have to handle a weak defined symbol, 2976 even if no regular object refers to it, if we decided to add it 2977 to the dynamic symbol table. FIXME: Do we normally need to worry 2978 about symbols which are defined by one dynamic object and 2979 referenced by another one? */ 2980 if (!h->needs_plt 2981 && h->type != STT_GNU_IFUNC 2982 && (h->def_regular 2983 || !h->def_dynamic 2984 || (!h->ref_regular 2985 && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) 2986 { 2987 h->plt = elf_hash_table (eif->info)->init_plt_offset; 2988 return TRUE; 2989 } 2990 2991 /* If we've already adjusted this symbol, don't do it again. This 2992 can happen via a recursive call. */ 2993 if (h->dynamic_adjusted) 2994 return TRUE; 2995 2996 /* Don't look at this symbol again. Note that we must set this 2997 after checking the above conditions, because we may look at a 2998 symbol once, decide not to do anything, and then get called 2999 recursively later after REF_REGULAR is set below. */ 3000 h->dynamic_adjusted = 1; 3001 3002 /* If this is a weak definition, and we know a real definition, and 3003 the real symbol is not itself defined by a regular object file, 3004 then get a good value for the real definition. We handle the 3005 real symbol first, for the convenience of the backend routine. 3006 3007 Note that there is a confusing case here. If the real definition 3008 is defined by a regular object file, we don't get the real symbol 3009 from the dynamic object, but we do get the weak symbol. If the 3010 processor backend uses a COPY reloc, then if some routine in the 3011 dynamic object changes the real symbol, we will not see that 3012 change in the corresponding weak symbol. This is the way other 3013 ELF linkers work as well, and seems to be a result of the shared 3014 library model. 3015 3016 I will clarify this issue. Most SVR4 shared libraries define the 3017 variable _timezone and define timezone as a weak synonym. The 3018 tzset call changes _timezone. If you write 3019 extern int timezone; 3020 int _timezone = 5; 3021 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } 3022 you might expect that, since timezone is a synonym for _timezone, 3023 the same number will print both times. However, if the processor 3024 backend uses a COPY reloc, then actually timezone will be copied 3025 into your process image, and, since you define _timezone 3026 yourself, _timezone will not. Thus timezone and _timezone will 3027 wind up at different memory locations. The tzset call will set 3028 _timezone, leaving timezone unchanged. */ 3029 3030 if (h->is_weakalias) 3031 { 3032 struct elf_link_hash_entry *def = weakdef (h); 3033 3034 /* If we get to this point, there is an implicit reference to 3035 the alias by a regular object file via the weak symbol H. */ 3036 def->ref_regular = 1; 3037 3038 /* Ensure that the backend adjust_dynamic_symbol function sees 3039 the strong alias before H by recursively calling ourselves. */ 3040 if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) 3041 return FALSE; 3042 } 3043 3044 /* If a symbol has no type and no size and does not require a PLT 3045 entry, then we are probably about to do the wrong thing here: we 3046 are probably going to create a COPY reloc for an empty object. 3047 This case can arise when a shared object is built with assembly 3048 code, and the assembly code fails to set the symbol type. */ 3049 if (h->size == 0 3050 && h->type == STT_NOTYPE 3051 && !h->needs_plt) 3052 _bfd_error_handler 3053 (_("warning: type and size of dynamic symbol `%s' are not defined"), 3054 h->root.root.string); 3055 3056 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) 3057 { 3058 eif->failed = TRUE; 3059 return FALSE; 3060 } 3061 3062 return TRUE; 3063 } 3064 3065 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, 3066 DYNBSS. */ 3067 3068 bfd_boolean 3069 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, 3070 struct elf_link_hash_entry *h, 3071 asection *dynbss) 3072 { 3073 unsigned int power_of_two; 3074 bfd_vma mask; 3075 asection *sec = h->root.u.def.section; 3076 3077 /* The section alignment of the definition is the maximum alignment 3078 requirement of symbols defined in the section. Since we don't 3079 know the symbol alignment requirement, we start with the 3080 maximum alignment and check low bits of the symbol address 3081 for the minimum alignment. */ 3082 power_of_two = bfd_get_section_alignment (sec->owner, sec); 3083 mask = ((bfd_vma) 1 << power_of_two) - 1; 3084 while ((h->root.u.def.value & mask) != 0) 3085 { 3086 mask >>= 1; 3087 --power_of_two; 3088 } 3089 3090 if (power_of_two > bfd_get_section_alignment (dynbss->owner, 3091 dynbss)) 3092 { 3093 /* Adjust the section alignment if needed. */ 3094 if (! bfd_set_section_alignment (dynbss->owner, dynbss, 3095 power_of_two)) 3096 return FALSE; 3097 } 3098 3099 /* We make sure that the symbol will be aligned properly. */ 3100 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); 3101 3102 /* Define the symbol as being at this point in DYNBSS. */ 3103 h->root.u.def.section = dynbss; 3104 h->root.u.def.value = dynbss->size; 3105 3106 /* Increment the size of DYNBSS to make room for the symbol. */ 3107 dynbss->size += h->size; 3108 3109 /* No error if extern_protected_data is true. */ 3110 if (h->protected_def 3111 && (!info->extern_protected_data 3112 || (info->extern_protected_data < 0 3113 && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) 3114 info->callbacks->einfo 3115 (_("%P: copy reloc against protected `%pT' is dangerous\n"), 3116 h->root.root.string); 3117 3118 return TRUE; 3119 } 3120 3121 /* Adjust all external symbols pointing into SEC_MERGE sections 3122 to reflect the object merging within the sections. */ 3123 3124 static bfd_boolean 3125 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) 3126 { 3127 asection *sec; 3128 3129 if ((h->root.type == bfd_link_hash_defined 3130 || h->root.type == bfd_link_hash_defweak) 3131 && ((sec = h->root.u.def.section)->flags & SEC_MERGE) 3132 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 3133 { 3134 bfd *output_bfd = (bfd *) data; 3135 3136 h->root.u.def.value = 3137 _bfd_merged_section_offset (output_bfd, 3138 &h->root.u.def.section, 3139 elf_section_data (sec)->sec_info, 3140 h->root.u.def.value); 3141 } 3142 3143 return TRUE; 3144 } 3145 3146 /* Returns false if the symbol referred to by H should be considered 3147 to resolve local to the current module, and true if it should be 3148 considered to bind dynamically. */ 3149 3150 bfd_boolean 3151 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, 3152 struct bfd_link_info *info, 3153 bfd_boolean not_local_protected) 3154 { 3155 bfd_boolean binding_stays_local_p; 3156 const struct elf_backend_data *bed; 3157 struct elf_link_hash_table *hash_table; 3158 3159 if (h == NULL) 3160 return FALSE; 3161 3162 while (h->root.type == bfd_link_hash_indirect 3163 || h->root.type == bfd_link_hash_warning) 3164 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3165 3166 /* If it was forced local, then clearly it's not dynamic. */ 3167 if (h->dynindx == -1) 3168 return FALSE; 3169 if (h->forced_local) 3170 return FALSE; 3171 3172 /* Identify the cases where name binding rules say that a 3173 visible symbol resolves locally. */ 3174 binding_stays_local_p = (bfd_link_executable (info) 3175 || SYMBOLIC_BIND (info, h)); 3176 3177 switch (ELF_ST_VISIBILITY (h->other)) 3178 { 3179 case STV_INTERNAL: 3180 case STV_HIDDEN: 3181 return FALSE; 3182 3183 case STV_PROTECTED: 3184 hash_table = elf_hash_table (info); 3185 if (!is_elf_hash_table (hash_table)) 3186 return FALSE; 3187 3188 bed = get_elf_backend_data (hash_table->dynobj); 3189 3190 /* Proper resolution for function pointer equality may require 3191 that these symbols perhaps be resolved dynamically, even though 3192 we should be resolving them to the current module. */ 3193 if (!not_local_protected || !bed->is_function_type (h->type)) 3194 binding_stays_local_p = TRUE; 3195 break; 3196 3197 default: 3198 break; 3199 } 3200 3201 /* If it isn't defined locally, then clearly it's dynamic. */ 3202 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 3203 return TRUE; 3204 3205 /* Otherwise, the symbol is dynamic if binding rules don't tell 3206 us that it remains local. */ 3207 return !binding_stays_local_p; 3208 } 3209 3210 /* Return true if the symbol referred to by H should be considered 3211 to resolve local to the current module, and false otherwise. Differs 3212 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of 3213 undefined symbols. The two functions are virtually identical except 3214 for the place where dynindx == -1 is tested. If that test is true, 3215 _bfd_elf_dynamic_symbol_p will say the symbol is local, while 3216 _bfd_elf_symbol_refs_local_p will say the symbol is local only for 3217 defined symbols. 3218 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as 3219 !_bfd_elf_symbol_refs_local_p, except that targets differ in their 3220 treatment of undefined weak symbols. For those that do not make 3221 undefined weak symbols dynamic, both functions may return false. */ 3222 3223 bfd_boolean 3224 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, 3225 struct bfd_link_info *info, 3226 bfd_boolean local_protected) 3227 { 3228 const struct elf_backend_data *bed; 3229 struct elf_link_hash_table *hash_table; 3230 3231 /* If it's a local sym, of course we resolve locally. */ 3232 if (h == NULL) 3233 return TRUE; 3234 3235 /* STV_HIDDEN or STV_INTERNAL ones must be local. */ 3236 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 3237 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 3238 return TRUE; 3239 3240 /* Forced local symbols resolve locally. */ 3241 if (h->forced_local) 3242 return TRUE; 3243 3244 /* Common symbols that become definitions don't get the DEF_REGULAR 3245 flag set, so test it first, and don't bail out. */ 3246 if (ELF_COMMON_DEF_P (h)) 3247 /* Do nothing. */; 3248 /* If we don't have a definition in a regular file, then we can't 3249 resolve locally. The sym is either undefined or dynamic. */ 3250 else if (!h->def_regular) 3251 return FALSE; 3252 3253 /* Non-dynamic symbols resolve locally. */ 3254 if (h->dynindx == -1) 3255 return TRUE; 3256 3257 /* At this point, we know the symbol is defined and dynamic. In an 3258 executable it must resolve locally, likewise when building symbolic 3259 shared libraries. */ 3260 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) 3261 return TRUE; 3262 3263 /* Now deal with defined dynamic symbols in shared libraries. Ones 3264 with default visibility might not resolve locally. */ 3265 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 3266 return FALSE; 3267 3268 hash_table = elf_hash_table (info); 3269 if (!is_elf_hash_table (hash_table)) 3270 return TRUE; 3271 3272 bed = get_elf_backend_data (hash_table->dynobj); 3273 3274 /* If extern_protected_data is false, STV_PROTECTED non-function 3275 symbols are local. */ 3276 if ((!info->extern_protected_data 3277 || (info->extern_protected_data < 0 3278 && !bed->extern_protected_data)) 3279 && !bed->is_function_type (h->type)) 3280 return TRUE; 3281 3282 /* Function pointer equality tests may require that STV_PROTECTED 3283 symbols be treated as dynamic symbols. If the address of a 3284 function not defined in an executable is set to that function's 3285 plt entry in the executable, then the address of the function in 3286 a shared library must also be the plt entry in the executable. */ 3287 return local_protected; 3288 } 3289 3290 /* Caches some TLS segment info, and ensures that the TLS segment vma is 3291 aligned. Returns the first TLS output section. */ 3292 3293 struct bfd_section * 3294 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) 3295 { 3296 struct bfd_section *sec, *tls; 3297 unsigned int align = 0; 3298 3299 for (sec = obfd->sections; sec != NULL; sec = sec->next) 3300 if ((sec->flags & SEC_THREAD_LOCAL) != 0) 3301 break; 3302 tls = sec; 3303 3304 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) 3305 if (sec->alignment_power > align) 3306 align = sec->alignment_power; 3307 3308 elf_hash_table (info)->tls_sec = tls; 3309 3310 /* Ensure the alignment of the first section is the largest alignment, 3311 so that the tls segment starts aligned. */ 3312 if (tls != NULL) 3313 tls->alignment_power = align; 3314 3315 return tls; 3316 } 3317 3318 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ 3319 static bfd_boolean 3320 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, 3321 Elf_Internal_Sym *sym) 3322 { 3323 const struct elf_backend_data *bed; 3324 3325 /* Local symbols do not count, but target specific ones might. */ 3326 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL 3327 && ELF_ST_BIND (sym->st_info) < STB_LOOS) 3328 return FALSE; 3329 3330 bed = get_elf_backend_data (abfd); 3331 /* Function symbols do not count. */ 3332 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) 3333 return FALSE; 3334 3335 /* If the section is undefined, then so is the symbol. */ 3336 if (sym->st_shndx == SHN_UNDEF) 3337 return FALSE; 3338 3339 /* If the symbol is defined in the common section, then 3340 it is a common definition and so does not count. */ 3341 if (bed->common_definition (sym)) 3342 return FALSE; 3343 3344 /* If the symbol is in a target specific section then we 3345 must rely upon the backend to tell us what it is. */ 3346 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) 3347 /* FIXME - this function is not coded yet: 3348 3349 return _bfd_is_global_symbol_definition (abfd, sym); 3350 3351 Instead for now assume that the definition is not global, 3352 Even if this is wrong, at least the linker will behave 3353 in the same way that it used to do. */ 3354 return FALSE; 3355 3356 return TRUE; 3357 } 3358 3359 /* Search the symbol table of the archive element of the archive ABFD 3360 whose archive map contains a mention of SYMDEF, and determine if 3361 the symbol is defined in this element. */ 3362 static bfd_boolean 3363 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) 3364 { 3365 Elf_Internal_Shdr * hdr; 3366 size_t symcount; 3367 size_t extsymcount; 3368 size_t extsymoff; 3369 Elf_Internal_Sym *isymbuf; 3370 Elf_Internal_Sym *isym; 3371 Elf_Internal_Sym *isymend; 3372 bfd_boolean result; 3373 3374 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 3375 if (abfd == NULL) 3376 return FALSE; 3377 3378 if (! bfd_check_format (abfd, bfd_object)) 3379 return FALSE; 3380 3381 /* Select the appropriate symbol table. If we don't know if the 3382 object file is an IR object, give linker LTO plugin a chance to 3383 get the correct symbol table. */ 3384 if (abfd->plugin_format == bfd_plugin_yes 3385 #if BFD_SUPPORTS_PLUGINS 3386 || (abfd->plugin_format == bfd_plugin_unknown 3387 && bfd_link_plugin_object_p (abfd)) 3388 #endif 3389 ) 3390 { 3391 /* Use the IR symbol table if the object has been claimed by 3392 plugin. */ 3393 abfd = abfd->plugin_dummy_bfd; 3394 hdr = &elf_tdata (abfd)->symtab_hdr; 3395 } 3396 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) 3397 hdr = &elf_tdata (abfd)->symtab_hdr; 3398 else 3399 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3400 3401 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 3402 3403 /* The sh_info field of the symtab header tells us where the 3404 external symbols start. We don't care about the local symbols. */ 3405 if (elf_bad_symtab (abfd)) 3406 { 3407 extsymcount = symcount; 3408 extsymoff = 0; 3409 } 3410 else 3411 { 3412 extsymcount = symcount - hdr->sh_info; 3413 extsymoff = hdr->sh_info; 3414 } 3415 3416 if (extsymcount == 0) 3417 return FALSE; 3418 3419 /* Read in the symbol table. */ 3420 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3421 NULL, NULL, NULL); 3422 if (isymbuf == NULL) 3423 return FALSE; 3424 3425 /* Scan the symbol table looking for SYMDEF. */ 3426 result = FALSE; 3427 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) 3428 { 3429 const char *name; 3430 3431 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3432 isym->st_name); 3433 if (name == NULL) 3434 break; 3435 3436 if (strcmp (name, symdef->name) == 0) 3437 { 3438 result = is_global_data_symbol_definition (abfd, isym); 3439 break; 3440 } 3441 } 3442 3443 free (isymbuf); 3444 3445 return result; 3446 } 3447 3448 /* Add an entry to the .dynamic table. */ 3449 3450 bfd_boolean 3451 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, 3452 bfd_vma tag, 3453 bfd_vma val) 3454 { 3455 struct elf_link_hash_table *hash_table; 3456 const struct elf_backend_data *bed; 3457 asection *s; 3458 bfd_size_type newsize; 3459 bfd_byte *newcontents; 3460 Elf_Internal_Dyn dyn; 3461 3462 hash_table = elf_hash_table (info); 3463 if (! is_elf_hash_table (hash_table)) 3464 return FALSE; 3465 3466 if (tag == DT_RELA || tag == DT_REL) 3467 hash_table->dynamic_relocs = TRUE; 3468 3469 bed = get_elf_backend_data (hash_table->dynobj); 3470 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3471 BFD_ASSERT (s != NULL); 3472 3473 newsize = s->size + bed->s->sizeof_dyn; 3474 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); 3475 if (newcontents == NULL) 3476 return FALSE; 3477 3478 dyn.d_tag = tag; 3479 dyn.d_un.d_val = val; 3480 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); 3481 3482 s->size = newsize; 3483 s->contents = newcontents; 3484 3485 return TRUE; 3486 } 3487 3488 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, 3489 otherwise just check whether one already exists. Returns -1 on error, 3490 1 if a DT_NEEDED tag already exists, and 0 on success. */ 3491 3492 static int 3493 elf_add_dt_needed_tag (bfd *abfd, 3494 struct bfd_link_info *info, 3495 const char *soname, 3496 bfd_boolean do_it) 3497 { 3498 struct elf_link_hash_table *hash_table; 3499 size_t strindex; 3500 3501 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 3502 return -1; 3503 3504 hash_table = elf_hash_table (info); 3505 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); 3506 if (strindex == (size_t) -1) 3507 return -1; 3508 3509 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) 3510 { 3511 asection *sdyn; 3512 const struct elf_backend_data *bed; 3513 bfd_byte *extdyn; 3514 3515 bed = get_elf_backend_data (hash_table->dynobj); 3516 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3517 if (sdyn != NULL) 3518 for (extdyn = sdyn->contents; 3519 extdyn < sdyn->contents + sdyn->size; 3520 extdyn += bed->s->sizeof_dyn) 3521 { 3522 Elf_Internal_Dyn dyn; 3523 3524 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 3525 if (dyn.d_tag == DT_NEEDED 3526 && dyn.d_un.d_val == strindex) 3527 { 3528 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3529 return 1; 3530 } 3531 } 3532 } 3533 3534 if (do_it) 3535 { 3536 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) 3537 return -1; 3538 3539 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) 3540 return -1; 3541 } 3542 else 3543 /* We were just checking for existence of the tag. */ 3544 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3545 3546 return 0; 3547 } 3548 3549 /* Return true if SONAME is on the needed list between NEEDED and STOP 3550 (or the end of list if STOP is NULL), and needed by a library that 3551 will be loaded. */ 3552 3553 static bfd_boolean 3554 on_needed_list (const char *soname, 3555 struct bfd_link_needed_list *needed, 3556 struct bfd_link_needed_list *stop) 3557 { 3558 struct bfd_link_needed_list *look; 3559 for (look = needed; look != stop; look = look->next) 3560 if (strcmp (soname, look->name) == 0 3561 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 3562 /* If needed by a library that itself is not directly 3563 needed, recursively check whether that library is 3564 indirectly needed. Since we add DT_NEEDED entries to 3565 the end of the list, library dependencies appear after 3566 the library. Therefore search prior to the current 3567 LOOK, preventing possible infinite recursion. */ 3568 || on_needed_list (elf_dt_name (look->by), needed, look))) 3569 return TRUE; 3570 3571 return FALSE; 3572 } 3573 3574 /* Sort symbol by value, section, and size. */ 3575 static int 3576 elf_sort_symbol (const void *arg1, const void *arg2) 3577 { 3578 const struct elf_link_hash_entry *h1; 3579 const struct elf_link_hash_entry *h2; 3580 bfd_signed_vma vdiff; 3581 3582 h1 = *(const struct elf_link_hash_entry **) arg1; 3583 h2 = *(const struct elf_link_hash_entry **) arg2; 3584 vdiff = h1->root.u.def.value - h2->root.u.def.value; 3585 if (vdiff != 0) 3586 return vdiff > 0 ? 1 : -1; 3587 else 3588 { 3589 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; 3590 if (sdiff != 0) 3591 return sdiff > 0 ? 1 : -1; 3592 } 3593 vdiff = h1->size - h2->size; 3594 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; 3595 } 3596 3597 /* This function is used to adjust offsets into .dynstr for 3598 dynamic symbols. This is called via elf_link_hash_traverse. */ 3599 3600 static bfd_boolean 3601 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) 3602 { 3603 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; 3604 3605 if (h->dynindx != -1) 3606 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); 3607 return TRUE; 3608 } 3609 3610 /* Assign string offsets in .dynstr, update all structures referencing 3611 them. */ 3612 3613 static bfd_boolean 3614 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) 3615 { 3616 struct elf_link_hash_table *hash_table = elf_hash_table (info); 3617 struct elf_link_local_dynamic_entry *entry; 3618 struct elf_strtab_hash *dynstr = hash_table->dynstr; 3619 bfd *dynobj = hash_table->dynobj; 3620 asection *sdyn; 3621 bfd_size_type size; 3622 const struct elf_backend_data *bed; 3623 bfd_byte *extdyn; 3624 3625 _bfd_elf_strtab_finalize (dynstr); 3626 size = _bfd_elf_strtab_size (dynstr); 3627 3628 bed = get_elf_backend_data (dynobj); 3629 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 3630 BFD_ASSERT (sdyn != NULL); 3631 3632 /* Update all .dynamic entries referencing .dynstr strings. */ 3633 for (extdyn = sdyn->contents; 3634 extdyn < sdyn->contents + sdyn->size; 3635 extdyn += bed->s->sizeof_dyn) 3636 { 3637 Elf_Internal_Dyn dyn; 3638 3639 bed->s->swap_dyn_in (dynobj, extdyn, &dyn); 3640 switch (dyn.d_tag) 3641 { 3642 case DT_STRSZ: 3643 dyn.d_un.d_val = size; 3644 break; 3645 case DT_NEEDED: 3646 case DT_SONAME: 3647 case DT_RPATH: 3648 case DT_RUNPATH: 3649 case DT_FILTER: 3650 case DT_AUXILIARY: 3651 case DT_AUDIT: 3652 case DT_DEPAUDIT: 3653 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); 3654 break; 3655 default: 3656 continue; 3657 } 3658 bed->s->swap_dyn_out (dynobj, &dyn, extdyn); 3659 } 3660 3661 /* Now update local dynamic symbols. */ 3662 for (entry = hash_table->dynlocal; entry ; entry = entry->next) 3663 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, 3664 entry->isym.st_name); 3665 3666 /* And the rest of dynamic symbols. */ 3667 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); 3668 3669 /* Adjust version definitions. */ 3670 if (elf_tdata (output_bfd)->cverdefs) 3671 { 3672 asection *s; 3673 bfd_byte *p; 3674 size_t i; 3675 Elf_Internal_Verdef def; 3676 Elf_Internal_Verdaux defaux; 3677 3678 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 3679 p = s->contents; 3680 do 3681 { 3682 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, 3683 &def); 3684 p += sizeof (Elf_External_Verdef); 3685 if (def.vd_aux != sizeof (Elf_External_Verdef)) 3686 continue; 3687 for (i = 0; i < def.vd_cnt; ++i) 3688 { 3689 _bfd_elf_swap_verdaux_in (output_bfd, 3690 (Elf_External_Verdaux *) p, &defaux); 3691 defaux.vda_name = _bfd_elf_strtab_offset (dynstr, 3692 defaux.vda_name); 3693 _bfd_elf_swap_verdaux_out (output_bfd, 3694 &defaux, (Elf_External_Verdaux *) p); 3695 p += sizeof (Elf_External_Verdaux); 3696 } 3697 } 3698 while (def.vd_next); 3699 } 3700 3701 /* Adjust version references. */ 3702 if (elf_tdata (output_bfd)->verref) 3703 { 3704 asection *s; 3705 bfd_byte *p; 3706 size_t i; 3707 Elf_Internal_Verneed need; 3708 Elf_Internal_Vernaux needaux; 3709 3710 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 3711 p = s->contents; 3712 do 3713 { 3714 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, 3715 &need); 3716 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); 3717 _bfd_elf_swap_verneed_out (output_bfd, &need, 3718 (Elf_External_Verneed *) p); 3719 p += sizeof (Elf_External_Verneed); 3720 for (i = 0; i < need.vn_cnt; ++i) 3721 { 3722 _bfd_elf_swap_vernaux_in (output_bfd, 3723 (Elf_External_Vernaux *) p, &needaux); 3724 needaux.vna_name = _bfd_elf_strtab_offset (dynstr, 3725 needaux.vna_name); 3726 _bfd_elf_swap_vernaux_out (output_bfd, 3727 &needaux, 3728 (Elf_External_Vernaux *) p); 3729 p += sizeof (Elf_External_Vernaux); 3730 } 3731 } 3732 while (need.vn_next); 3733 } 3734 3735 return TRUE; 3736 } 3737 3738 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3739 The default is to only match when the INPUT and OUTPUT are exactly 3740 the same target. */ 3741 3742 bfd_boolean 3743 _bfd_elf_default_relocs_compatible (const bfd_target *input, 3744 const bfd_target *output) 3745 { 3746 return input == output; 3747 } 3748 3749 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3750 This version is used when different targets for the same architecture 3751 are virtually identical. */ 3752 3753 bfd_boolean 3754 _bfd_elf_relocs_compatible (const bfd_target *input, 3755 const bfd_target *output) 3756 { 3757 const struct elf_backend_data *obed, *ibed; 3758 3759 if (input == output) 3760 return TRUE; 3761 3762 ibed = xvec_get_elf_backend_data (input); 3763 obed = xvec_get_elf_backend_data (output); 3764 3765 if (ibed->arch != obed->arch) 3766 return FALSE; 3767 3768 /* If both backends are using this function, deem them compatible. */ 3769 return ibed->relocs_compatible == obed->relocs_compatible; 3770 } 3771 3772 /* Make a special call to the linker "notice" function to tell it that 3773 we are about to handle an as-needed lib, or have finished 3774 processing the lib. */ 3775 3776 bfd_boolean 3777 _bfd_elf_notice_as_needed (bfd *ibfd, 3778 struct bfd_link_info *info, 3779 enum notice_asneeded_action act) 3780 { 3781 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); 3782 } 3783 3784 /* Check relocations an ELF object file. */ 3785 3786 bfd_boolean 3787 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) 3788 { 3789 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3790 struct elf_link_hash_table *htab = elf_hash_table (info); 3791 3792 /* If this object is the same format as the output object, and it is 3793 not a shared library, then let the backend look through the 3794 relocs. 3795 3796 This is required to build global offset table entries and to 3797 arrange for dynamic relocs. It is not required for the 3798 particular common case of linking non PIC code, even when linking 3799 against shared libraries, but unfortunately there is no way of 3800 knowing whether an object file has been compiled PIC or not. 3801 Looking through the relocs is not particularly time consuming. 3802 The problem is that we must either (1) keep the relocs in memory, 3803 which causes the linker to require additional runtime memory or 3804 (2) read the relocs twice from the input file, which wastes time. 3805 This would be a good case for using mmap. 3806 3807 I have no idea how to handle linking PIC code into a file of a 3808 different format. It probably can't be done. */ 3809 if ((abfd->flags & DYNAMIC) == 0 3810 && is_elf_hash_table (htab) 3811 && bed->check_relocs != NULL 3812 && elf_object_id (abfd) == elf_hash_table_id (htab) 3813 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 3814 { 3815 asection *o; 3816 3817 for (o = abfd->sections; o != NULL; o = o->next) 3818 { 3819 Elf_Internal_Rela *internal_relocs; 3820 bfd_boolean ok; 3821 3822 /* Don't check relocations in excluded sections. */ 3823 if ((o->flags & SEC_RELOC) == 0 3824 || (o->flags & SEC_EXCLUDE) != 0 3825 || o->reloc_count == 0 3826 || ((info->strip == strip_all || info->strip == strip_debugger) 3827 && (o->flags & SEC_DEBUGGING) != 0) 3828 || bfd_is_abs_section (o->output_section)) 3829 continue; 3830 3831 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, 3832 info->keep_memory); 3833 if (internal_relocs == NULL) 3834 return FALSE; 3835 3836 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); 3837 3838 if (elf_section_data (o)->relocs != internal_relocs) 3839 free (internal_relocs); 3840 3841 if (! ok) 3842 return FALSE; 3843 } 3844 } 3845 3846 return TRUE; 3847 } 3848 3849 /* Add symbols from an ELF object file to the linker hash table. */ 3850 3851 static bfd_boolean 3852 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 3853 { 3854 Elf_Internal_Ehdr *ehdr; 3855 Elf_Internal_Shdr *hdr; 3856 size_t symcount; 3857 size_t extsymcount; 3858 size_t extsymoff; 3859 struct elf_link_hash_entry **sym_hash; 3860 bfd_boolean dynamic; 3861 Elf_External_Versym *extversym = NULL; 3862 Elf_External_Versym *ever; 3863 struct elf_link_hash_entry *weaks; 3864 struct elf_link_hash_entry **nondeflt_vers = NULL; 3865 size_t nondeflt_vers_cnt = 0; 3866 Elf_Internal_Sym *isymbuf = NULL; 3867 Elf_Internal_Sym *isym; 3868 Elf_Internal_Sym *isymend; 3869 const struct elf_backend_data *bed; 3870 bfd_boolean add_needed; 3871 struct elf_link_hash_table *htab; 3872 bfd_size_type amt; 3873 void *alloc_mark = NULL; 3874 struct bfd_hash_entry **old_table = NULL; 3875 unsigned int old_size = 0; 3876 unsigned int old_count = 0; 3877 void *old_tab = NULL; 3878 void *old_ent; 3879 struct bfd_link_hash_entry *old_undefs = NULL; 3880 struct bfd_link_hash_entry *old_undefs_tail = NULL; 3881 void *old_strtab = NULL; 3882 size_t tabsize = 0; 3883 asection *s; 3884 bfd_boolean just_syms; 3885 3886 htab = elf_hash_table (info); 3887 bed = get_elf_backend_data (abfd); 3888 3889 if ((abfd->flags & DYNAMIC) == 0) 3890 dynamic = FALSE; 3891 else 3892 { 3893 dynamic = TRUE; 3894 3895 /* You can't use -r against a dynamic object. Also, there's no 3896 hope of using a dynamic object which does not exactly match 3897 the format of the output file. */ 3898 if (bfd_link_relocatable (info) 3899 || !is_elf_hash_table (htab) 3900 || info->output_bfd->xvec != abfd->xvec) 3901 { 3902 if (bfd_link_relocatable (info)) 3903 bfd_set_error (bfd_error_invalid_operation); 3904 else 3905 bfd_set_error (bfd_error_wrong_format); 3906 goto error_return; 3907 } 3908 } 3909 3910 ehdr = elf_elfheader (abfd); 3911 if (info->warn_alternate_em 3912 && bed->elf_machine_code != ehdr->e_machine 3913 && ((bed->elf_machine_alt1 != 0 3914 && ehdr->e_machine == bed->elf_machine_alt1) 3915 || (bed->elf_machine_alt2 != 0 3916 && ehdr->e_machine == bed->elf_machine_alt2))) 3917 _bfd_error_handler 3918 /* xgettext:c-format */ 3919 (_("alternate ELF machine code found (%d) in %pB, expecting %d"), 3920 ehdr->e_machine, abfd, bed->elf_machine_code); 3921 3922 /* As a GNU extension, any input sections which are named 3923 .gnu.warning.SYMBOL are treated as warning symbols for the given 3924 symbol. This differs from .gnu.warning sections, which generate 3925 warnings when they are included in an output file. */ 3926 /* PR 12761: Also generate this warning when building shared libraries. */ 3927 for (s = abfd->sections; s != NULL; s = s->next) 3928 { 3929 const char *name; 3930 3931 name = bfd_get_section_name (abfd, s); 3932 if (CONST_STRNEQ (name, ".gnu.warning.")) 3933 { 3934 char *msg; 3935 bfd_size_type sz; 3936 3937 name += sizeof ".gnu.warning." - 1; 3938 3939 /* If this is a shared object, then look up the symbol 3940 in the hash table. If it is there, and it is already 3941 been defined, then we will not be using the entry 3942 from this shared object, so we don't need to warn. 3943 FIXME: If we see the definition in a regular object 3944 later on, we will warn, but we shouldn't. The only 3945 fix is to keep track of what warnings we are supposed 3946 to emit, and then handle them all at the end of the 3947 link. */ 3948 if (dynamic) 3949 { 3950 struct elf_link_hash_entry *h; 3951 3952 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 3953 3954 /* FIXME: What about bfd_link_hash_common? */ 3955 if (h != NULL 3956 && (h->root.type == bfd_link_hash_defined 3957 || h->root.type == bfd_link_hash_defweak)) 3958 continue; 3959 } 3960 3961 sz = s->size; 3962 msg = (char *) bfd_alloc (abfd, sz + 1); 3963 if (msg == NULL) 3964 goto error_return; 3965 3966 if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) 3967 goto error_return; 3968 3969 msg[sz] = '\0'; 3970 3971 if (! (_bfd_generic_link_add_one_symbol 3972 (info, abfd, name, BSF_WARNING, s, 0, msg, 3973 FALSE, bed->collect, NULL))) 3974 goto error_return; 3975 3976 if (bfd_link_executable (info)) 3977 { 3978 /* Clobber the section size so that the warning does 3979 not get copied into the output file. */ 3980 s->size = 0; 3981 3982 /* Also set SEC_EXCLUDE, so that symbols defined in 3983 the warning section don't get copied to the output. */ 3984 s->flags |= SEC_EXCLUDE; 3985 } 3986 } 3987 } 3988 3989 just_syms = ((s = abfd->sections) != NULL 3990 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); 3991 3992 add_needed = TRUE; 3993 if (! dynamic) 3994 { 3995 /* If we are creating a shared library, create all the dynamic 3996 sections immediately. We need to attach them to something, 3997 so we attach them to this BFD, provided it is the right 3998 format and is not from ld --just-symbols. Always create the 3999 dynamic sections for -E/--dynamic-list. FIXME: If there 4000 are no input BFD's of the same format as the output, we can't 4001 make a shared library. */ 4002 if (!just_syms 4003 && (bfd_link_pic (info) 4004 || (!bfd_link_relocatable (info) 4005 && info->nointerp 4006 && (info->export_dynamic || info->dynamic))) 4007 && is_elf_hash_table (htab) 4008 && info->output_bfd->xvec == abfd->xvec 4009 && !htab->dynamic_sections_created) 4010 { 4011 if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) 4012 goto error_return; 4013 } 4014 } 4015 else if (!is_elf_hash_table (htab)) 4016 goto error_return; 4017 else 4018 { 4019 const char *soname = NULL; 4020 char *audit = NULL; 4021 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; 4022 const Elf_Internal_Phdr *phdr; 4023 int ret; 4024 4025 /* ld --just-symbols and dynamic objects don't mix very well. 4026 ld shouldn't allow it. */ 4027 if (just_syms) 4028 abort (); 4029 4030 /* If this dynamic lib was specified on the command line with 4031 --as-needed in effect, then we don't want to add a DT_NEEDED 4032 tag unless the lib is actually used. Similary for libs brought 4033 in by another lib's DT_NEEDED. When --no-add-needed is used 4034 on a dynamic lib, we don't want to add a DT_NEEDED entry for 4035 any dynamic library in DT_NEEDED tags in the dynamic lib at 4036 all. */ 4037 add_needed = (elf_dyn_lib_class (abfd) 4038 & (DYN_AS_NEEDED | DYN_DT_NEEDED 4039 | DYN_NO_NEEDED)) == 0; 4040 4041 s = bfd_get_section_by_name (abfd, ".dynamic"); 4042 if (s != NULL) 4043 { 4044 bfd_byte *dynbuf; 4045 bfd_byte *extdyn; 4046 unsigned int elfsec; 4047 unsigned long shlink; 4048 4049 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 4050 { 4051 error_free_dyn: 4052 free (dynbuf); 4053 goto error_return; 4054 } 4055 4056 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 4057 if (elfsec == SHN_BAD) 4058 goto error_free_dyn; 4059 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 4060 4061 for (extdyn = dynbuf; 4062 extdyn < dynbuf + s->size; 4063 extdyn += bed->s->sizeof_dyn) 4064 { 4065 Elf_Internal_Dyn dyn; 4066 4067 bed->s->swap_dyn_in (abfd, extdyn, &dyn); 4068 if (dyn.d_tag == DT_SONAME) 4069 { 4070 unsigned int tagv = dyn.d_un.d_val; 4071 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4072 if (soname == NULL) 4073 goto error_free_dyn; 4074 } 4075 if (dyn.d_tag == DT_NEEDED) 4076 { 4077 struct bfd_link_needed_list *n, **pn; 4078 char *fnm, *anm; 4079 unsigned int tagv = dyn.d_un.d_val; 4080 4081 amt = sizeof (struct bfd_link_needed_list); 4082 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4083 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4084 if (n == NULL || fnm == NULL) 4085 goto error_free_dyn; 4086 amt = strlen (fnm) + 1; 4087 anm = (char *) bfd_alloc (abfd, amt); 4088 if (anm == NULL) 4089 goto error_free_dyn; 4090 memcpy (anm, fnm, amt); 4091 n->name = anm; 4092 n->by = abfd; 4093 n->next = NULL; 4094 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) 4095 ; 4096 *pn = n; 4097 } 4098 if (dyn.d_tag == DT_RUNPATH) 4099 { 4100 struct bfd_link_needed_list *n, **pn; 4101 char *fnm, *anm; 4102 unsigned int tagv = dyn.d_un.d_val; 4103 4104 amt = sizeof (struct bfd_link_needed_list); 4105 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4106 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4107 if (n == NULL || fnm == NULL) 4108 goto error_free_dyn; 4109 amt = strlen (fnm) + 1; 4110 anm = (char *) bfd_alloc (abfd, amt); 4111 if (anm == NULL) 4112 goto error_free_dyn; 4113 memcpy (anm, fnm, amt); 4114 n->name = anm; 4115 n->by = abfd; 4116 n->next = NULL; 4117 for (pn = & runpath; 4118 *pn != NULL; 4119 pn = &(*pn)->next) 4120 ; 4121 *pn = n; 4122 } 4123 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ 4124 if (!runpath && dyn.d_tag == DT_RPATH) 4125 { 4126 struct bfd_link_needed_list *n, **pn; 4127 char *fnm, *anm; 4128 unsigned int tagv = dyn.d_un.d_val; 4129 4130 amt = sizeof (struct bfd_link_needed_list); 4131 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4132 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4133 if (n == NULL || fnm == NULL) 4134 goto error_free_dyn; 4135 amt = strlen (fnm) + 1; 4136 anm = (char *) bfd_alloc (abfd, amt); 4137 if (anm == NULL) 4138 goto error_free_dyn; 4139 memcpy (anm, fnm, amt); 4140 n->name = anm; 4141 n->by = abfd; 4142 n->next = NULL; 4143 for (pn = & rpath; 4144 *pn != NULL; 4145 pn = &(*pn)->next) 4146 ; 4147 *pn = n; 4148 } 4149 if (dyn.d_tag == DT_AUDIT) 4150 { 4151 unsigned int tagv = dyn.d_un.d_val; 4152 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4153 } 4154 } 4155 4156 free (dynbuf); 4157 } 4158 4159 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that 4160 frees all more recently bfd_alloc'd blocks as well. */ 4161 if (runpath) 4162 rpath = runpath; 4163 4164 if (rpath) 4165 { 4166 struct bfd_link_needed_list **pn; 4167 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) 4168 ; 4169 *pn = rpath; 4170 } 4171 4172 /* If we have a PT_GNU_RELRO program header, mark as read-only 4173 all sections contained fully therein. This makes relro 4174 shared library sections appear as they will at run-time. */ 4175 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; 4176 while (--phdr >= elf_tdata (abfd)->phdr) 4177 if (phdr->p_type == PT_GNU_RELRO) 4178 { 4179 for (s = abfd->sections; s != NULL; s = s->next) 4180 if ((s->flags & SEC_ALLOC) != 0 4181 && s->vma >= phdr->p_vaddr 4182 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) 4183 s->flags |= SEC_READONLY; 4184 break; 4185 } 4186 4187 /* We do not want to include any of the sections in a dynamic 4188 object in the output file. We hack by simply clobbering the 4189 list of sections in the BFD. This could be handled more 4190 cleanly by, say, a new section flag; the existing 4191 SEC_NEVER_LOAD flag is not the one we want, because that one 4192 still implies that the section takes up space in the output 4193 file. */ 4194 bfd_section_list_clear (abfd); 4195 4196 /* Find the name to use in a DT_NEEDED entry that refers to this 4197 object. If the object has a DT_SONAME entry, we use it. 4198 Otherwise, if the generic linker stuck something in 4199 elf_dt_name, we use that. Otherwise, we just use the file 4200 name. */ 4201 if (soname == NULL || *soname == '\0') 4202 { 4203 soname = elf_dt_name (abfd); 4204 if (soname == NULL || *soname == '\0') 4205 soname = bfd_get_filename (abfd); 4206 } 4207 4208 /* Save the SONAME because sometimes the linker emulation code 4209 will need to know it. */ 4210 elf_dt_name (abfd) = soname; 4211 4212 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4213 if (ret < 0) 4214 goto error_return; 4215 4216 /* If we have already included this dynamic object in the 4217 link, just ignore it. There is no reason to include a 4218 particular dynamic object more than once. */ 4219 if (ret > 0) 4220 return TRUE; 4221 4222 /* Save the DT_AUDIT entry for the linker emulation code. */ 4223 elf_dt_audit (abfd) = audit; 4224 } 4225 4226 /* If this is a dynamic object, we always link against the .dynsym 4227 symbol table, not the .symtab symbol table. The dynamic linker 4228 will only see the .dynsym symbol table, so there is no reason to 4229 look at .symtab for a dynamic object. */ 4230 4231 if (! dynamic || elf_dynsymtab (abfd) == 0) 4232 hdr = &elf_tdata (abfd)->symtab_hdr; 4233 else 4234 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 4235 4236 symcount = hdr->sh_size / bed->s->sizeof_sym; 4237 4238 /* The sh_info field of the symtab header tells us where the 4239 external symbols start. We don't care about the local symbols at 4240 this point. */ 4241 if (elf_bad_symtab (abfd)) 4242 { 4243 extsymcount = symcount; 4244 extsymoff = 0; 4245 } 4246 else 4247 { 4248 extsymcount = symcount - hdr->sh_info; 4249 extsymoff = hdr->sh_info; 4250 } 4251 4252 sym_hash = elf_sym_hashes (abfd); 4253 if (extsymcount != 0) 4254 { 4255 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 4256 NULL, NULL, NULL); 4257 if (isymbuf == NULL) 4258 goto error_return; 4259 4260 if (sym_hash == NULL) 4261 { 4262 /* We store a pointer to the hash table entry for each 4263 external symbol. */ 4264 amt = extsymcount; 4265 amt *= sizeof (struct elf_link_hash_entry *); 4266 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); 4267 if (sym_hash == NULL) 4268 goto error_free_sym; 4269 elf_sym_hashes (abfd) = sym_hash; 4270 } 4271 } 4272 4273 if (dynamic) 4274 { 4275 /* Read in any version definitions. */ 4276 if (!_bfd_elf_slurp_version_tables (abfd, 4277 info->default_imported_symver)) 4278 goto error_free_sym; 4279 4280 /* Read in the symbol versions, but don't bother to convert them 4281 to internal format. */ 4282 if (elf_dynversym (abfd) != 0) 4283 { 4284 Elf_Internal_Shdr *versymhdr; 4285 4286 versymhdr = &elf_tdata (abfd)->dynversym_hdr; 4287 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 4288 if (extversym == NULL) 4289 goto error_free_sym; 4290 amt = versymhdr->sh_size; 4291 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 4292 || bfd_bread (extversym, amt, abfd) != amt) 4293 goto error_free_vers; 4294 } 4295 } 4296 4297 /* If we are loading an as-needed shared lib, save the symbol table 4298 state before we start adding symbols. If the lib turns out 4299 to be unneeded, restore the state. */ 4300 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4301 { 4302 unsigned int i; 4303 size_t entsize; 4304 4305 for (entsize = 0, i = 0; i < htab->root.table.size; i++) 4306 { 4307 struct bfd_hash_entry *p; 4308 struct elf_link_hash_entry *h; 4309 4310 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4311 { 4312 h = (struct elf_link_hash_entry *) p; 4313 entsize += htab->root.table.entsize; 4314 if (h->root.type == bfd_link_hash_warning) 4315 entsize += htab->root.table.entsize; 4316 } 4317 } 4318 4319 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); 4320 old_tab = bfd_malloc (tabsize + entsize); 4321 if (old_tab == NULL) 4322 goto error_free_vers; 4323 4324 /* Remember the current objalloc pointer, so that all mem for 4325 symbols added can later be reclaimed. */ 4326 alloc_mark = bfd_hash_allocate (&htab->root.table, 1); 4327 if (alloc_mark == NULL) 4328 goto error_free_vers; 4329 4330 /* Make a special call to the linker "notice" function to 4331 tell it that we are about to handle an as-needed lib. */ 4332 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) 4333 goto error_free_vers; 4334 4335 /* Clone the symbol table. Remember some pointers into the 4336 symbol table, and dynamic symbol count. */ 4337 old_ent = (char *) old_tab + tabsize; 4338 memcpy (old_tab, htab->root.table.table, tabsize); 4339 old_undefs = htab->root.undefs; 4340 old_undefs_tail = htab->root.undefs_tail; 4341 old_table = htab->root.table.table; 4342 old_size = htab->root.table.size; 4343 old_count = htab->root.table.count; 4344 old_strtab = _bfd_elf_strtab_save (htab->dynstr); 4345 if (old_strtab == NULL) 4346 goto error_free_vers; 4347 4348 for (i = 0; i < htab->root.table.size; i++) 4349 { 4350 struct bfd_hash_entry *p; 4351 struct elf_link_hash_entry *h; 4352 4353 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4354 { 4355 memcpy (old_ent, p, htab->root.table.entsize); 4356 old_ent = (char *) old_ent + htab->root.table.entsize; 4357 h = (struct elf_link_hash_entry *) p; 4358 if (h->root.type == bfd_link_hash_warning) 4359 { 4360 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); 4361 old_ent = (char *) old_ent + htab->root.table.entsize; 4362 } 4363 } 4364 } 4365 } 4366 4367 weaks = NULL; 4368 ever = extversym != NULL ? extversym + extsymoff : NULL; 4369 for (isym = isymbuf, isymend = isymbuf + extsymcount; 4370 isym < isymend; 4371 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) 4372 { 4373 int bind; 4374 bfd_vma value; 4375 asection *sec, *new_sec; 4376 flagword flags; 4377 const char *name; 4378 struct elf_link_hash_entry *h; 4379 struct elf_link_hash_entry *hi; 4380 bfd_boolean definition; 4381 bfd_boolean size_change_ok; 4382 bfd_boolean type_change_ok; 4383 bfd_boolean new_weak; 4384 bfd_boolean old_weak; 4385 bfd_boolean override; 4386 bfd_boolean common; 4387 bfd_boolean discarded; 4388 unsigned int old_alignment; 4389 bfd *old_bfd; 4390 bfd_boolean matched; 4391 4392 override = FALSE; 4393 4394 flags = BSF_NO_FLAGS; 4395 sec = NULL; 4396 value = isym->st_value; 4397 common = bed->common_definition (isym); 4398 if (common && info->inhibit_common_definition) 4399 { 4400 /* Treat common symbol as undefined for --no-define-common. */ 4401 isym->st_shndx = SHN_UNDEF; 4402 common = FALSE; 4403 } 4404 discarded = FALSE; 4405 4406 bind = ELF_ST_BIND (isym->st_info); 4407 switch (bind) 4408 { 4409 case STB_LOCAL: 4410 /* This should be impossible, since ELF requires that all 4411 global symbols follow all local symbols, and that sh_info 4412 point to the first global symbol. Unfortunately, Irix 5 4413 screws this up. */ 4414 continue; 4415 4416 case STB_GLOBAL: 4417 if (isym->st_shndx != SHN_UNDEF && !common) 4418 flags = BSF_GLOBAL; 4419 break; 4420 4421 case STB_WEAK: 4422 flags = BSF_WEAK; 4423 break; 4424 4425 case STB_GNU_UNIQUE: 4426 flags = BSF_GNU_UNIQUE; 4427 break; 4428 4429 default: 4430 /* Leave it up to the processor backend. */ 4431 break; 4432 } 4433 4434 if (isym->st_shndx == SHN_UNDEF) 4435 sec = bfd_und_section_ptr; 4436 else if (isym->st_shndx == SHN_ABS) 4437 sec = bfd_abs_section_ptr; 4438 else if (isym->st_shndx == SHN_COMMON) 4439 { 4440 sec = bfd_com_section_ptr; 4441 /* What ELF calls the size we call the value. What ELF 4442 calls the value we call the alignment. */ 4443 value = isym->st_size; 4444 } 4445 else 4446 { 4447 sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 4448 if (sec == NULL) 4449 sec = bfd_abs_section_ptr; 4450 else if (discarded_section (sec)) 4451 { 4452 /* Symbols from discarded section are undefined. We keep 4453 its visibility. */ 4454 sec = bfd_und_section_ptr; 4455 discarded = TRUE; 4456 isym->st_shndx = SHN_UNDEF; 4457 } 4458 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 4459 value -= sec->vma; 4460 } 4461 4462 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 4463 isym->st_name); 4464 if (name == NULL) 4465 goto error_free_vers; 4466 4467 if (isym->st_shndx == SHN_COMMON 4468 && (abfd->flags & BFD_PLUGIN) != 0) 4469 { 4470 asection *xc = bfd_get_section_by_name (abfd, "COMMON"); 4471 4472 if (xc == NULL) 4473 { 4474 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP 4475 | SEC_EXCLUDE); 4476 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); 4477 if (xc == NULL) 4478 goto error_free_vers; 4479 } 4480 sec = xc; 4481 } 4482 else if (isym->st_shndx == SHN_COMMON 4483 && ELF_ST_TYPE (isym->st_info) == STT_TLS 4484 && !bfd_link_relocatable (info)) 4485 { 4486 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); 4487 4488 if (tcomm == NULL) 4489 { 4490 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON 4491 | SEC_LINKER_CREATED); 4492 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); 4493 if (tcomm == NULL) 4494 goto error_free_vers; 4495 } 4496 sec = tcomm; 4497 } 4498 else if (bed->elf_add_symbol_hook) 4499 { 4500 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, 4501 &sec, &value)) 4502 goto error_free_vers; 4503 4504 /* The hook function sets the name to NULL if this symbol 4505 should be skipped for some reason. */ 4506 if (name == NULL) 4507 continue; 4508 } 4509 4510 /* Sanity check that all possibilities were handled. */ 4511 if (sec == NULL) 4512 { 4513 bfd_set_error (bfd_error_bad_value); 4514 goto error_free_vers; 4515 } 4516 4517 /* Silently discard TLS symbols from --just-syms. There's 4518 no way to combine a static TLS block with a new TLS block 4519 for this executable. */ 4520 if (ELF_ST_TYPE (isym->st_info) == STT_TLS 4521 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 4522 continue; 4523 4524 if (bfd_is_und_section (sec) 4525 || bfd_is_com_section (sec)) 4526 definition = FALSE; 4527 else 4528 definition = TRUE; 4529 4530 size_change_ok = FALSE; 4531 type_change_ok = bed->type_change_ok; 4532 old_weak = FALSE; 4533 matched = FALSE; 4534 old_alignment = 0; 4535 old_bfd = NULL; 4536 new_sec = sec; 4537 4538 if (is_elf_hash_table (htab)) 4539 { 4540 Elf_Internal_Versym iver; 4541 unsigned int vernum = 0; 4542 bfd_boolean skip; 4543 4544 if (ever == NULL) 4545 { 4546 if (info->default_imported_symver) 4547 /* Use the default symbol version created earlier. */ 4548 iver.vs_vers = elf_tdata (abfd)->cverdefs; 4549 else 4550 iver.vs_vers = 0; 4551 } 4552 else 4553 _bfd_elf_swap_versym_in (abfd, ever, &iver); 4554 4555 vernum = iver.vs_vers & VERSYM_VERSION; 4556 4557 /* If this is a hidden symbol, or if it is not version 4558 1, we append the version name to the symbol name. 4559 However, we do not modify a non-hidden absolute symbol 4560 if it is not a function, because it might be the version 4561 symbol itself. FIXME: What if it isn't? */ 4562 if ((iver.vs_vers & VERSYM_HIDDEN) != 0 4563 || (vernum > 1 4564 && (!bfd_is_abs_section (sec) 4565 || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) 4566 { 4567 const char *verstr; 4568 size_t namelen, verlen, newlen; 4569 char *newname, *p; 4570 4571 if (isym->st_shndx != SHN_UNDEF) 4572 { 4573 if (vernum > elf_tdata (abfd)->cverdefs) 4574 verstr = NULL; 4575 else if (vernum > 1) 4576 verstr = 4577 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 4578 else 4579 verstr = ""; 4580 4581 if (verstr == NULL) 4582 { 4583 _bfd_error_handler 4584 /* xgettext:c-format */ 4585 (_("%pB: %s: invalid version %u (max %d)"), 4586 abfd, name, vernum, 4587 elf_tdata (abfd)->cverdefs); 4588 bfd_set_error (bfd_error_bad_value); 4589 goto error_free_vers; 4590 } 4591 } 4592 else 4593 { 4594 /* We cannot simply test for the number of 4595 entries in the VERNEED section since the 4596 numbers for the needed versions do not start 4597 at 0. */ 4598 Elf_Internal_Verneed *t; 4599 4600 verstr = NULL; 4601 for (t = elf_tdata (abfd)->verref; 4602 t != NULL; 4603 t = t->vn_nextref) 4604 { 4605 Elf_Internal_Vernaux *a; 4606 4607 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 4608 { 4609 if (a->vna_other == vernum) 4610 { 4611 verstr = a->vna_nodename; 4612 break; 4613 } 4614 } 4615 if (a != NULL) 4616 break; 4617 } 4618 if (verstr == NULL) 4619 { 4620 _bfd_error_handler 4621 /* xgettext:c-format */ 4622 (_("%pB: %s: invalid needed version %d"), 4623 abfd, name, vernum); 4624 bfd_set_error (bfd_error_bad_value); 4625 goto error_free_vers; 4626 } 4627 } 4628 4629 namelen = strlen (name); 4630 verlen = strlen (verstr); 4631 newlen = namelen + verlen + 2; 4632 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4633 && isym->st_shndx != SHN_UNDEF) 4634 ++newlen; 4635 4636 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); 4637 if (newname == NULL) 4638 goto error_free_vers; 4639 memcpy (newname, name, namelen); 4640 p = newname + namelen; 4641 *p++ = ELF_VER_CHR; 4642 /* If this is a defined non-hidden version symbol, 4643 we add another @ to the name. This indicates the 4644 default version of the symbol. */ 4645 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4646 && isym->st_shndx != SHN_UNDEF) 4647 *p++ = ELF_VER_CHR; 4648 memcpy (p, verstr, verlen + 1); 4649 4650 name = newname; 4651 } 4652 4653 /* If this symbol has default visibility and the user has 4654 requested we not re-export it, then mark it as hidden. */ 4655 if (!bfd_is_und_section (sec) 4656 && !dynamic 4657 && abfd->no_export 4658 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) 4659 isym->st_other = (STV_HIDDEN 4660 | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); 4661 4662 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, 4663 sym_hash, &old_bfd, &old_weak, 4664 &old_alignment, &skip, &override, 4665 &type_change_ok, &size_change_ok, 4666 &matched)) 4667 goto error_free_vers; 4668 4669 if (skip) 4670 continue; 4671 4672 /* Override a definition only if the new symbol matches the 4673 existing one. */ 4674 if (override && matched) 4675 definition = FALSE; 4676 4677 h = *sym_hash; 4678 while (h->root.type == bfd_link_hash_indirect 4679 || h->root.type == bfd_link_hash_warning) 4680 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4681 4682 if (elf_tdata (abfd)->verdef != NULL 4683 && vernum > 1 4684 && definition) 4685 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; 4686 } 4687 4688 if (! (_bfd_generic_link_add_one_symbol 4689 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, 4690 (struct bfd_link_hash_entry **) sym_hash))) 4691 goto error_free_vers; 4692 4693 if ((abfd->flags & DYNAMIC) == 0 4694 && (bfd_get_flavour (info->output_bfd) 4695 == bfd_target_elf_flavour)) 4696 { 4697 if (ELF_ST_TYPE (isym->st_info) == STT_GNU_IFUNC) 4698 elf_tdata (info->output_bfd)->has_gnu_symbols 4699 |= elf_gnu_symbol_ifunc; 4700 if ((flags & BSF_GNU_UNIQUE)) 4701 elf_tdata (info->output_bfd)->has_gnu_symbols 4702 |= elf_gnu_symbol_unique; 4703 } 4704 4705 h = *sym_hash; 4706 /* We need to make sure that indirect symbol dynamic flags are 4707 updated. */ 4708 hi = h; 4709 while (h->root.type == bfd_link_hash_indirect 4710 || h->root.type == bfd_link_hash_warning) 4711 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4712 4713 /* Setting the index to -3 tells elf_link_output_extsym that 4714 this symbol is defined in a discarded section. */ 4715 if (discarded) 4716 h->indx = -3; 4717 4718 *sym_hash = h; 4719 4720 new_weak = (flags & BSF_WEAK) != 0; 4721 if (dynamic 4722 && definition 4723 && new_weak 4724 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) 4725 && is_elf_hash_table (htab) 4726 && h->u.alias == NULL) 4727 { 4728 /* Keep a list of all weak defined non function symbols from 4729 a dynamic object, using the alias field. Later in this 4730 function we will set the alias field to the correct 4731 value. We only put non-function symbols from dynamic 4732 objects on this list, because that happens to be the only 4733 time we need to know the normal symbol corresponding to a 4734 weak symbol, and the information is time consuming to 4735 figure out. If the alias field is not already NULL, 4736 then this symbol was already defined by some previous 4737 dynamic object, and we will be using that previous 4738 definition anyhow. */ 4739 4740 h->u.alias = weaks; 4741 weaks = h; 4742 } 4743 4744 /* Set the alignment of a common symbol. */ 4745 if ((common || bfd_is_com_section (sec)) 4746 && h->root.type == bfd_link_hash_common) 4747 { 4748 unsigned int align; 4749 4750 if (common) 4751 align = bfd_log2 (isym->st_value); 4752 else 4753 { 4754 /* The new symbol is a common symbol in a shared object. 4755 We need to get the alignment from the section. */ 4756 align = new_sec->alignment_power; 4757 } 4758 if (align > old_alignment) 4759 h->root.u.c.p->alignment_power = align; 4760 else 4761 h->root.u.c.p->alignment_power = old_alignment; 4762 } 4763 4764 if (is_elf_hash_table (htab)) 4765 { 4766 /* Set a flag in the hash table entry indicating the type of 4767 reference or definition we just found. A dynamic symbol 4768 is one which is referenced or defined by both a regular 4769 object and a shared object. */ 4770 bfd_boolean dynsym = FALSE; 4771 4772 /* Plugin symbols aren't normal. Don't set def_regular or 4773 ref_regular for them, or make them dynamic. */ 4774 if ((abfd->flags & BFD_PLUGIN) != 0) 4775 ; 4776 else if (! dynamic) 4777 { 4778 if (! definition) 4779 { 4780 h->ref_regular = 1; 4781 if (bind != STB_WEAK) 4782 h->ref_regular_nonweak = 1; 4783 } 4784 else 4785 { 4786 h->def_regular = 1; 4787 if (h->def_dynamic) 4788 { 4789 h->def_dynamic = 0; 4790 h->ref_dynamic = 1; 4791 } 4792 } 4793 4794 /* If the indirect symbol has been forced local, don't 4795 make the real symbol dynamic. */ 4796 if ((h == hi || !hi->forced_local) 4797 && (bfd_link_dll (info) 4798 || h->def_dynamic 4799 || h->ref_dynamic)) 4800 dynsym = TRUE; 4801 } 4802 else 4803 { 4804 if (! definition) 4805 { 4806 h->ref_dynamic = 1; 4807 hi->ref_dynamic = 1; 4808 } 4809 else 4810 { 4811 h->def_dynamic = 1; 4812 hi->def_dynamic = 1; 4813 } 4814 4815 /* If the indirect symbol has been forced local, don't 4816 make the real symbol dynamic. */ 4817 if ((h == hi || !hi->forced_local) 4818 && (h->def_regular 4819 || h->ref_regular 4820 || (h->is_weakalias 4821 && weakdef (h)->dynindx != -1))) 4822 dynsym = TRUE; 4823 } 4824 4825 /* Check to see if we need to add an indirect symbol for 4826 the default name. */ 4827 if (definition 4828 || (!override && h->root.type == bfd_link_hash_common)) 4829 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, 4830 sec, value, &old_bfd, &dynsym)) 4831 goto error_free_vers; 4832 4833 /* Check the alignment when a common symbol is involved. This 4834 can change when a common symbol is overridden by a normal 4835 definition or a common symbol is ignored due to the old 4836 normal definition. We need to make sure the maximum 4837 alignment is maintained. */ 4838 if ((old_alignment || common) 4839 && h->root.type != bfd_link_hash_common) 4840 { 4841 unsigned int common_align; 4842 unsigned int normal_align; 4843 unsigned int symbol_align; 4844 bfd *normal_bfd; 4845 bfd *common_bfd; 4846 4847 BFD_ASSERT (h->root.type == bfd_link_hash_defined 4848 || h->root.type == bfd_link_hash_defweak); 4849 4850 symbol_align = ffs (h->root.u.def.value) - 1; 4851 if (h->root.u.def.section->owner != NULL 4852 && (h->root.u.def.section->owner->flags 4853 & (DYNAMIC | BFD_PLUGIN)) == 0) 4854 { 4855 normal_align = h->root.u.def.section->alignment_power; 4856 if (normal_align > symbol_align) 4857 normal_align = symbol_align; 4858 } 4859 else 4860 normal_align = symbol_align; 4861 4862 if (old_alignment) 4863 { 4864 common_align = old_alignment; 4865 common_bfd = old_bfd; 4866 normal_bfd = abfd; 4867 } 4868 else 4869 { 4870 common_align = bfd_log2 (isym->st_value); 4871 common_bfd = abfd; 4872 normal_bfd = old_bfd; 4873 } 4874 4875 if (normal_align < common_align) 4876 { 4877 /* PR binutils/2735 */ 4878 if (normal_bfd == NULL) 4879 _bfd_error_handler 4880 /* xgettext:c-format */ 4881 (_("warning: alignment %u of common symbol `%s' in %pB is" 4882 " greater than the alignment (%u) of its section %pA"), 4883 1 << common_align, name, common_bfd, 4884 1 << normal_align, h->root.u.def.section); 4885 else 4886 _bfd_error_handler 4887 /* xgettext:c-format */ 4888 (_("warning: alignment %u of symbol `%s' in %pB" 4889 " is smaller than %u in %pB"), 4890 1 << normal_align, name, normal_bfd, 4891 1 << common_align, common_bfd); 4892 } 4893 } 4894 4895 /* Remember the symbol size if it isn't undefined. */ 4896 if (isym->st_size != 0 4897 && isym->st_shndx != SHN_UNDEF 4898 && (definition || h->size == 0)) 4899 { 4900 if (h->size != 0 4901 && h->size != isym->st_size 4902 && ! size_change_ok) 4903 _bfd_error_handler 4904 /* xgettext:c-format */ 4905 (_("warning: size of symbol `%s' changed" 4906 " from %" PRIu64 " in %pB to %" PRIu64 " in %pB"), 4907 name, (uint64_t) h->size, old_bfd, 4908 (uint64_t) isym->st_size, abfd); 4909 4910 h->size = isym->st_size; 4911 } 4912 4913 /* If this is a common symbol, then we always want H->SIZE 4914 to be the size of the common symbol. The code just above 4915 won't fix the size if a common symbol becomes larger. We 4916 don't warn about a size change here, because that is 4917 covered by --warn-common. Allow changes between different 4918 function types. */ 4919 if (h->root.type == bfd_link_hash_common) 4920 h->size = h->root.u.c.size; 4921 4922 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE 4923 && ((definition && !new_weak) 4924 || (old_weak && h->root.type == bfd_link_hash_common) 4925 || h->type == STT_NOTYPE)) 4926 { 4927 unsigned int type = ELF_ST_TYPE (isym->st_info); 4928 4929 /* Turn an IFUNC symbol from a DSO into a normal FUNC 4930 symbol. */ 4931 if (type == STT_GNU_IFUNC 4932 && (abfd->flags & DYNAMIC) != 0) 4933 type = STT_FUNC; 4934 4935 if (h->type != type) 4936 { 4937 if (h->type != STT_NOTYPE && ! type_change_ok) 4938 /* xgettext:c-format */ 4939 _bfd_error_handler 4940 (_("warning: type of symbol `%s' changed" 4941 " from %d to %d in %pB"), 4942 name, h->type, type, abfd); 4943 4944 h->type = type; 4945 } 4946 } 4947 4948 /* Merge st_other field. */ 4949 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); 4950 4951 /* We don't want to make debug symbol dynamic. */ 4952 if (definition 4953 && (sec->flags & SEC_DEBUGGING) 4954 && !bfd_link_relocatable (info)) 4955 dynsym = FALSE; 4956 4957 /* Nor should we make plugin symbols dynamic. */ 4958 if ((abfd->flags & BFD_PLUGIN) != 0) 4959 dynsym = FALSE; 4960 4961 if (definition) 4962 { 4963 h->target_internal = isym->st_target_internal; 4964 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; 4965 } 4966 4967 if (definition && !dynamic) 4968 { 4969 char *p = strchr (name, ELF_VER_CHR); 4970 if (p != NULL && p[1] != ELF_VER_CHR) 4971 { 4972 /* Queue non-default versions so that .symver x, x@FOO 4973 aliases can be checked. */ 4974 if (!nondeflt_vers) 4975 { 4976 amt = ((isymend - isym + 1) 4977 * sizeof (struct elf_link_hash_entry *)); 4978 nondeflt_vers 4979 = (struct elf_link_hash_entry **) bfd_malloc (amt); 4980 if (!nondeflt_vers) 4981 goto error_free_vers; 4982 } 4983 nondeflt_vers[nondeflt_vers_cnt++] = h; 4984 } 4985 } 4986 4987 if (dynsym && h->dynindx == -1) 4988 { 4989 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4990 goto error_free_vers; 4991 if (h->is_weakalias 4992 && weakdef (h)->dynindx == -1) 4993 { 4994 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) 4995 goto error_free_vers; 4996 } 4997 } 4998 else if (h->dynindx != -1) 4999 /* If the symbol already has a dynamic index, but 5000 visibility says it should not be visible, turn it into 5001 a local symbol. */ 5002 switch (ELF_ST_VISIBILITY (h->other)) 5003 { 5004 case STV_INTERNAL: 5005 case STV_HIDDEN: 5006 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 5007 dynsym = FALSE; 5008 break; 5009 } 5010 5011 /* Don't add DT_NEEDED for references from the dummy bfd nor 5012 for unmatched symbol. */ 5013 if (!add_needed 5014 && matched 5015 && definition 5016 && ((dynsym 5017 && h->ref_regular_nonweak 5018 && (old_bfd == NULL 5019 || (old_bfd->flags & BFD_PLUGIN) == 0)) 5020 || (h->ref_dynamic_nonweak 5021 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 5022 && !on_needed_list (elf_dt_name (abfd), 5023 htab->needed, NULL)))) 5024 { 5025 int ret; 5026 const char *soname = elf_dt_name (abfd); 5027 5028 info->callbacks->minfo ("%!", soname, old_bfd, 5029 h->root.root.string); 5030 5031 /* A symbol from a library loaded via DT_NEEDED of some 5032 other library is referenced by a regular object. 5033 Add a DT_NEEDED entry for it. Issue an error if 5034 --no-add-needed is used and the reference was not 5035 a weak one. */ 5036 if (old_bfd != NULL 5037 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) 5038 { 5039 _bfd_error_handler 5040 /* xgettext:c-format */ 5041 (_("%pB: undefined reference to symbol '%s'"), 5042 old_bfd, name); 5043 bfd_set_error (bfd_error_missing_dso); 5044 goto error_free_vers; 5045 } 5046 5047 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) 5048 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); 5049 5050 add_needed = TRUE; 5051 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 5052 if (ret < 0) 5053 goto error_free_vers; 5054 5055 BFD_ASSERT (ret == 0); 5056 } 5057 } 5058 } 5059 5060 if (info->lto_plugin_active 5061 && !bfd_link_relocatable (info) 5062 && (abfd->flags & BFD_PLUGIN) == 0 5063 && !just_syms 5064 && extsymcount) 5065 { 5066 int r_sym_shift; 5067 5068 if (bed->s->arch_size == 32) 5069 r_sym_shift = 8; 5070 else 5071 r_sym_shift = 32; 5072 5073 /* If linker plugin is enabled, set non_ir_ref_regular on symbols 5074 referenced in regular objects so that linker plugin will get 5075 the correct symbol resolution. */ 5076 5077 sym_hash = elf_sym_hashes (abfd); 5078 for (s = abfd->sections; s != NULL; s = s->next) 5079 { 5080 Elf_Internal_Rela *internal_relocs; 5081 Elf_Internal_Rela *rel, *relend; 5082 5083 /* Don't check relocations in excluded sections. */ 5084 if ((s->flags & SEC_RELOC) == 0 5085 || s->reloc_count == 0 5086 || (s->flags & SEC_EXCLUDE) != 0 5087 || ((info->strip == strip_all 5088 || info->strip == strip_debugger) 5089 && (s->flags & SEC_DEBUGGING) != 0)) 5090 continue; 5091 5092 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, 5093 NULL, 5094 info->keep_memory); 5095 if (internal_relocs == NULL) 5096 goto error_free_vers; 5097 5098 rel = internal_relocs; 5099 relend = rel + s->reloc_count; 5100 for ( ; rel < relend; rel++) 5101 { 5102 unsigned long r_symndx = rel->r_info >> r_sym_shift; 5103 struct elf_link_hash_entry *h; 5104 5105 /* Skip local symbols. */ 5106 if (r_symndx < extsymoff) 5107 continue; 5108 5109 h = sym_hash[r_symndx - extsymoff]; 5110 if (h != NULL) 5111 h->root.non_ir_ref_regular = 1; 5112 } 5113 5114 if (elf_section_data (s)->relocs != internal_relocs) 5115 free (internal_relocs); 5116 } 5117 } 5118 5119 if (extversym != NULL) 5120 { 5121 free (extversym); 5122 extversym = NULL; 5123 } 5124 5125 if (isymbuf != NULL) 5126 { 5127 free (isymbuf); 5128 isymbuf = NULL; 5129 } 5130 5131 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 5132 { 5133 unsigned int i; 5134 5135 /* Restore the symbol table. */ 5136 old_ent = (char *) old_tab + tabsize; 5137 memset (elf_sym_hashes (abfd), 0, 5138 extsymcount * sizeof (struct elf_link_hash_entry *)); 5139 htab->root.table.table = old_table; 5140 htab->root.table.size = old_size; 5141 htab->root.table.count = old_count; 5142 memcpy (htab->root.table.table, old_tab, tabsize); 5143 htab->root.undefs = old_undefs; 5144 htab->root.undefs_tail = old_undefs_tail; 5145 _bfd_elf_strtab_restore (htab->dynstr, old_strtab); 5146 free (old_strtab); 5147 old_strtab = NULL; 5148 for (i = 0; i < htab->root.table.size; i++) 5149 { 5150 struct bfd_hash_entry *p; 5151 struct elf_link_hash_entry *h; 5152 bfd_size_type size; 5153 unsigned int alignment_power; 5154 unsigned int non_ir_ref_dynamic; 5155 5156 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 5157 { 5158 h = (struct elf_link_hash_entry *) p; 5159 if (h->root.type == bfd_link_hash_warning) 5160 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5161 5162 /* Preserve the maximum alignment and size for common 5163 symbols even if this dynamic lib isn't on DT_NEEDED 5164 since it can still be loaded at run time by another 5165 dynamic lib. */ 5166 if (h->root.type == bfd_link_hash_common) 5167 { 5168 size = h->root.u.c.size; 5169 alignment_power = h->root.u.c.p->alignment_power; 5170 } 5171 else 5172 { 5173 size = 0; 5174 alignment_power = 0; 5175 } 5176 /* Preserve non_ir_ref_dynamic so that this symbol 5177 will be exported when the dynamic lib becomes needed 5178 in the second pass. */ 5179 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; 5180 memcpy (p, old_ent, htab->root.table.entsize); 5181 old_ent = (char *) old_ent + htab->root.table.entsize; 5182 h = (struct elf_link_hash_entry *) p; 5183 if (h->root.type == bfd_link_hash_warning) 5184 { 5185 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); 5186 old_ent = (char *) old_ent + htab->root.table.entsize; 5187 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5188 } 5189 if (h->root.type == bfd_link_hash_common) 5190 { 5191 if (size > h->root.u.c.size) 5192 h->root.u.c.size = size; 5193 if (alignment_power > h->root.u.c.p->alignment_power) 5194 h->root.u.c.p->alignment_power = alignment_power; 5195 } 5196 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; 5197 } 5198 } 5199 5200 /* Make a special call to the linker "notice" function to 5201 tell it that symbols added for crefs may need to be removed. */ 5202 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) 5203 goto error_free_vers; 5204 5205 free (old_tab); 5206 objalloc_free_block ((struct objalloc *) htab->root.table.memory, 5207 alloc_mark); 5208 if (nondeflt_vers != NULL) 5209 free (nondeflt_vers); 5210 return TRUE; 5211 } 5212 5213 if (old_tab != NULL) 5214 { 5215 if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) 5216 goto error_free_vers; 5217 free (old_tab); 5218 old_tab = NULL; 5219 } 5220 5221 /* Now that all the symbols from this input file are created, if 5222 not performing a relocatable link, handle .symver foo, foo@BAR 5223 such that any relocs against foo become foo@BAR. */ 5224 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) 5225 { 5226 size_t cnt, symidx; 5227 5228 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) 5229 { 5230 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; 5231 char *shortname, *p; 5232 5233 p = strchr (h->root.root.string, ELF_VER_CHR); 5234 if (p == NULL 5235 || (h->root.type != bfd_link_hash_defined 5236 && h->root.type != bfd_link_hash_defweak)) 5237 continue; 5238 5239 amt = p - h->root.root.string; 5240 shortname = (char *) bfd_malloc (amt + 1); 5241 if (!shortname) 5242 goto error_free_vers; 5243 memcpy (shortname, h->root.root.string, amt); 5244 shortname[amt] = '\0'; 5245 5246 hi = (struct elf_link_hash_entry *) 5247 bfd_link_hash_lookup (&htab->root, shortname, 5248 FALSE, FALSE, FALSE); 5249 if (hi != NULL 5250 && hi->root.type == h->root.type 5251 && hi->root.u.def.value == h->root.u.def.value 5252 && hi->root.u.def.section == h->root.u.def.section) 5253 { 5254 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 5255 hi->root.type = bfd_link_hash_indirect; 5256 hi->root.u.i.link = (struct bfd_link_hash_entry *) h; 5257 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 5258 sym_hash = elf_sym_hashes (abfd); 5259 if (sym_hash) 5260 for (symidx = 0; symidx < extsymcount; ++symidx) 5261 if (sym_hash[symidx] == hi) 5262 { 5263 sym_hash[symidx] = h; 5264 break; 5265 } 5266 } 5267 free (shortname); 5268 } 5269 free (nondeflt_vers); 5270 nondeflt_vers = NULL; 5271 } 5272 5273 /* Now set the alias field correctly for all the weak defined 5274 symbols we found. The only way to do this is to search all the 5275 symbols. Since we only need the information for non functions in 5276 dynamic objects, that's the only time we actually put anything on 5277 the list WEAKS. We need this information so that if a regular 5278 object refers to a symbol defined weakly in a dynamic object, the 5279 real symbol in the dynamic object is also put in the dynamic 5280 symbols; we also must arrange for both symbols to point to the 5281 same memory location. We could handle the general case of symbol 5282 aliasing, but a general symbol alias can only be generated in 5283 assembler code, handling it correctly would be very time 5284 consuming, and other ELF linkers don't handle general aliasing 5285 either. */ 5286 if (weaks != NULL) 5287 { 5288 struct elf_link_hash_entry **hpp; 5289 struct elf_link_hash_entry **hppend; 5290 struct elf_link_hash_entry **sorted_sym_hash; 5291 struct elf_link_hash_entry *h; 5292 size_t sym_count; 5293 5294 /* Since we have to search the whole symbol list for each weak 5295 defined symbol, search time for N weak defined symbols will be 5296 O(N^2). Binary search will cut it down to O(NlogN). */ 5297 amt = extsymcount; 5298 amt *= sizeof (struct elf_link_hash_entry *); 5299 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); 5300 if (sorted_sym_hash == NULL) 5301 goto error_return; 5302 sym_hash = sorted_sym_hash; 5303 hpp = elf_sym_hashes (abfd); 5304 hppend = hpp + extsymcount; 5305 sym_count = 0; 5306 for (; hpp < hppend; hpp++) 5307 { 5308 h = *hpp; 5309 if (h != NULL 5310 && h->root.type == bfd_link_hash_defined 5311 && !bed->is_function_type (h->type)) 5312 { 5313 *sym_hash = h; 5314 sym_hash++; 5315 sym_count++; 5316 } 5317 } 5318 5319 qsort (sorted_sym_hash, sym_count, 5320 sizeof (struct elf_link_hash_entry *), 5321 elf_sort_symbol); 5322 5323 while (weaks != NULL) 5324 { 5325 struct elf_link_hash_entry *hlook; 5326 asection *slook; 5327 bfd_vma vlook; 5328 size_t i, j, idx = 0; 5329 5330 hlook = weaks; 5331 weaks = hlook->u.alias; 5332 hlook->u.alias = NULL; 5333 5334 if (hlook->root.type != bfd_link_hash_defined 5335 && hlook->root.type != bfd_link_hash_defweak) 5336 continue; 5337 5338 slook = hlook->root.u.def.section; 5339 vlook = hlook->root.u.def.value; 5340 5341 i = 0; 5342 j = sym_count; 5343 while (i != j) 5344 { 5345 bfd_signed_vma vdiff; 5346 idx = (i + j) / 2; 5347 h = sorted_sym_hash[idx]; 5348 vdiff = vlook - h->root.u.def.value; 5349 if (vdiff < 0) 5350 j = idx; 5351 else if (vdiff > 0) 5352 i = idx + 1; 5353 else 5354 { 5355 int sdiff = slook->id - h->root.u.def.section->id; 5356 if (sdiff < 0) 5357 j = idx; 5358 else if (sdiff > 0) 5359 i = idx + 1; 5360 else 5361 break; 5362 } 5363 } 5364 5365 /* We didn't find a value/section match. */ 5366 if (i == j) 5367 continue; 5368 5369 /* With multiple aliases, or when the weak symbol is already 5370 strongly defined, we have multiple matching symbols and 5371 the binary search above may land on any of them. Step 5372 one past the matching symbol(s). */ 5373 while (++idx != j) 5374 { 5375 h = sorted_sym_hash[idx]; 5376 if (h->root.u.def.section != slook 5377 || h->root.u.def.value != vlook) 5378 break; 5379 } 5380 5381 /* Now look back over the aliases. Since we sorted by size 5382 as well as value and section, we'll choose the one with 5383 the largest size. */ 5384 while (idx-- != i) 5385 { 5386 h = sorted_sym_hash[idx]; 5387 5388 /* Stop if value or section doesn't match. */ 5389 if (h->root.u.def.section != slook 5390 || h->root.u.def.value != vlook) 5391 break; 5392 else if (h != hlook) 5393 { 5394 struct elf_link_hash_entry *t; 5395 5396 hlook->u.alias = h; 5397 hlook->is_weakalias = 1; 5398 t = h; 5399 if (t->u.alias != NULL) 5400 while (t->u.alias != h) 5401 t = t->u.alias; 5402 t->u.alias = hlook; 5403 5404 /* If the weak definition is in the list of dynamic 5405 symbols, make sure the real definition is put 5406 there as well. */ 5407 if (hlook->dynindx != -1 && h->dynindx == -1) 5408 { 5409 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 5410 { 5411 err_free_sym_hash: 5412 free (sorted_sym_hash); 5413 goto error_return; 5414 } 5415 } 5416 5417 /* If the real definition is in the list of dynamic 5418 symbols, make sure the weak definition is put 5419 there as well. If we don't do this, then the 5420 dynamic loader might not merge the entries for the 5421 real definition and the weak definition. */ 5422 if (h->dynindx != -1 && hlook->dynindx == -1) 5423 { 5424 if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) 5425 goto err_free_sym_hash; 5426 } 5427 break; 5428 } 5429 } 5430 } 5431 5432 free (sorted_sym_hash); 5433 } 5434 5435 if (bed->check_directives 5436 && !(*bed->check_directives) (abfd, info)) 5437 return FALSE; 5438 5439 /* If this is a non-traditional link, try to optimize the handling 5440 of the .stab/.stabstr sections. */ 5441 if (! dynamic 5442 && ! info->traditional_format 5443 && is_elf_hash_table (htab) 5444 && (info->strip != strip_all && info->strip != strip_debugger)) 5445 { 5446 asection *stabstr; 5447 5448 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 5449 if (stabstr != NULL) 5450 { 5451 bfd_size_type string_offset = 0; 5452 asection *stab; 5453 5454 for (stab = abfd->sections; stab; stab = stab->next) 5455 if (CONST_STRNEQ (stab->name, ".stab") 5456 && (!stab->name[5] || 5457 (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) 5458 && (stab->flags & SEC_MERGE) == 0 5459 && !bfd_is_abs_section (stab->output_section)) 5460 { 5461 struct bfd_elf_section_data *secdata; 5462 5463 secdata = elf_section_data (stab); 5464 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, 5465 stabstr, &secdata->sec_info, 5466 &string_offset)) 5467 goto error_return; 5468 if (secdata->sec_info) 5469 stab->sec_info_type = SEC_INFO_TYPE_STABS; 5470 } 5471 } 5472 } 5473 5474 if (is_elf_hash_table (htab) && add_needed) 5475 { 5476 /* Add this bfd to the loaded list. */ 5477 struct elf_link_loaded_list *n; 5478 5479 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); 5480 if (n == NULL) 5481 goto error_return; 5482 n->abfd = abfd; 5483 n->next = htab->loaded; 5484 htab->loaded = n; 5485 } 5486 5487 return TRUE; 5488 5489 error_free_vers: 5490 if (old_tab != NULL) 5491 free (old_tab); 5492 if (old_strtab != NULL) 5493 free (old_strtab); 5494 if (nondeflt_vers != NULL) 5495 free (nondeflt_vers); 5496 if (extversym != NULL) 5497 free (extversym); 5498 error_free_sym: 5499 if (isymbuf != NULL) 5500 free (isymbuf); 5501 error_return: 5502 return FALSE; 5503 } 5504 5505 /* Return the linker hash table entry of a symbol that might be 5506 satisfied by an archive symbol. Return -1 on error. */ 5507 5508 struct elf_link_hash_entry * 5509 _bfd_elf_archive_symbol_lookup (bfd *abfd, 5510 struct bfd_link_info *info, 5511 const char *name) 5512 { 5513 struct elf_link_hash_entry *h; 5514 char *p, *copy; 5515 size_t len, first; 5516 5517 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); 5518 if (h != NULL) 5519 return h; 5520 5521 /* If this is a default version (the name contains @@), look up the 5522 symbol again with only one `@' as well as without the version. 5523 The effect is that references to the symbol with and without the 5524 version will be matched by the default symbol in the archive. */ 5525 5526 p = strchr (name, ELF_VER_CHR); 5527 if (p == NULL || p[1] != ELF_VER_CHR) 5528 return h; 5529 5530 /* First check with only one `@'. */ 5531 len = strlen (name); 5532 copy = (char *) bfd_alloc (abfd, len); 5533 if (copy == NULL) 5534 return (struct elf_link_hash_entry *) -1; 5535 5536 first = p - name + 1; 5537 memcpy (copy, name, first); 5538 memcpy (copy + first, name + first + 1, len - first); 5539 5540 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); 5541 if (h == NULL) 5542 { 5543 /* We also need to check references to the symbol without the 5544 version. */ 5545 copy[first - 1] = '\0'; 5546 h = elf_link_hash_lookup (elf_hash_table (info), copy, 5547 FALSE, FALSE, TRUE); 5548 } 5549 5550 bfd_release (abfd, copy); 5551 return h; 5552 } 5553 5554 /* Add symbols from an ELF archive file to the linker hash table. We 5555 don't use _bfd_generic_link_add_archive_symbols because we need to 5556 handle versioned symbols. 5557 5558 Fortunately, ELF archive handling is simpler than that done by 5559 _bfd_generic_link_add_archive_symbols, which has to allow for a.out 5560 oddities. In ELF, if we find a symbol in the archive map, and the 5561 symbol is currently undefined, we know that we must pull in that 5562 object file. 5563 5564 Unfortunately, we do have to make multiple passes over the symbol 5565 table until nothing further is resolved. */ 5566 5567 static bfd_boolean 5568 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 5569 { 5570 symindex c; 5571 unsigned char *included = NULL; 5572 carsym *symdefs; 5573 bfd_boolean loop; 5574 bfd_size_type amt; 5575 const struct elf_backend_data *bed; 5576 struct elf_link_hash_entry * (*archive_symbol_lookup) 5577 (bfd *, struct bfd_link_info *, const char *); 5578 5579 if (! bfd_has_map (abfd)) 5580 { 5581 /* An empty archive is a special case. */ 5582 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 5583 return TRUE; 5584 bfd_set_error (bfd_error_no_armap); 5585 return FALSE; 5586 } 5587 5588 /* Keep track of all symbols we know to be already defined, and all 5589 files we know to be already included. This is to speed up the 5590 second and subsequent passes. */ 5591 c = bfd_ardata (abfd)->symdef_count; 5592 if (c == 0) 5593 return TRUE; 5594 amt = c; 5595 amt *= sizeof (*included); 5596 included = (unsigned char *) bfd_zmalloc (amt); 5597 if (included == NULL) 5598 return FALSE; 5599 5600 symdefs = bfd_ardata (abfd)->symdefs; 5601 bed = get_elf_backend_data (abfd); 5602 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; 5603 5604 do 5605 { 5606 file_ptr last; 5607 symindex i; 5608 carsym *symdef; 5609 carsym *symdefend; 5610 5611 loop = FALSE; 5612 last = -1; 5613 5614 symdef = symdefs; 5615 symdefend = symdef + c; 5616 for (i = 0; symdef < symdefend; symdef++, i++) 5617 { 5618 struct elf_link_hash_entry *h; 5619 bfd *element; 5620 struct bfd_link_hash_entry *undefs_tail; 5621 symindex mark; 5622 5623 if (included[i]) 5624 continue; 5625 if (symdef->file_offset == last) 5626 { 5627 included[i] = TRUE; 5628 continue; 5629 } 5630 5631 h = archive_symbol_lookup (abfd, info, symdef->name); 5632 if (h == (struct elf_link_hash_entry *) -1) 5633 goto error_return; 5634 5635 if (h == NULL) 5636 continue; 5637 5638 if (h->root.type == bfd_link_hash_common) 5639 { 5640 /* We currently have a common symbol. The archive map contains 5641 a reference to this symbol, so we may want to include it. We 5642 only want to include it however, if this archive element 5643 contains a definition of the symbol, not just another common 5644 declaration of it. 5645 5646 Unfortunately some archivers (including GNU ar) will put 5647 declarations of common symbols into their archive maps, as 5648 well as real definitions, so we cannot just go by the archive 5649 map alone. Instead we must read in the element's symbol 5650 table and check that to see what kind of symbol definition 5651 this is. */ 5652 if (! elf_link_is_defined_archive_symbol (abfd, symdef)) 5653 continue; 5654 } 5655 else if (h->root.type != bfd_link_hash_undefined) 5656 { 5657 if (h->root.type != bfd_link_hash_undefweak) 5658 /* Symbol must be defined. Don't check it again. */ 5659 included[i] = TRUE; 5660 continue; 5661 } 5662 5663 /* We need to include this archive member. */ 5664 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 5665 if (element == NULL) 5666 goto error_return; 5667 5668 if (! bfd_check_format (element, bfd_object)) 5669 goto error_return; 5670 5671 undefs_tail = info->hash->undefs_tail; 5672 5673 if (!(*info->callbacks 5674 ->add_archive_element) (info, element, symdef->name, &element)) 5675 continue; 5676 if (!bfd_link_add_symbols (element, info)) 5677 goto error_return; 5678 5679 /* If there are any new undefined symbols, we need to make 5680 another pass through the archive in order to see whether 5681 they can be defined. FIXME: This isn't perfect, because 5682 common symbols wind up on undefs_tail and because an 5683 undefined symbol which is defined later on in this pass 5684 does not require another pass. This isn't a bug, but it 5685 does make the code less efficient than it could be. */ 5686 if (undefs_tail != info->hash->undefs_tail) 5687 loop = TRUE; 5688 5689 /* Look backward to mark all symbols from this object file 5690 which we have already seen in this pass. */ 5691 mark = i; 5692 do 5693 { 5694 included[mark] = TRUE; 5695 if (mark == 0) 5696 break; 5697 --mark; 5698 } 5699 while (symdefs[mark].file_offset == symdef->file_offset); 5700 5701 /* We mark subsequent symbols from this object file as we go 5702 on through the loop. */ 5703 last = symdef->file_offset; 5704 } 5705 } 5706 while (loop); 5707 5708 free (included); 5709 5710 return TRUE; 5711 5712 error_return: 5713 if (included != NULL) 5714 free (included); 5715 return FALSE; 5716 } 5717 5718 /* Given an ELF BFD, add symbols to the global hash table as 5719 appropriate. */ 5720 5721 bfd_boolean 5722 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 5723 { 5724 switch (bfd_get_format (abfd)) 5725 { 5726 case bfd_object: 5727 return elf_link_add_object_symbols (abfd, info); 5728 case bfd_archive: 5729 return elf_link_add_archive_symbols (abfd, info); 5730 default: 5731 bfd_set_error (bfd_error_wrong_format); 5732 return FALSE; 5733 } 5734 } 5735 5736 struct hash_codes_info 5737 { 5738 unsigned long *hashcodes; 5739 bfd_boolean error; 5740 }; 5741 5742 /* This function will be called though elf_link_hash_traverse to store 5743 all hash value of the exported symbols in an array. */ 5744 5745 static bfd_boolean 5746 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) 5747 { 5748 struct hash_codes_info *inf = (struct hash_codes_info *) data; 5749 const char *name; 5750 unsigned long ha; 5751 char *alc = NULL; 5752 5753 /* Ignore indirect symbols. These are added by the versioning code. */ 5754 if (h->dynindx == -1) 5755 return TRUE; 5756 5757 name = h->root.root.string; 5758 if (h->versioned >= versioned) 5759 { 5760 char *p = strchr (name, ELF_VER_CHR); 5761 if (p != NULL) 5762 { 5763 alc = (char *) bfd_malloc (p - name + 1); 5764 if (alc == NULL) 5765 { 5766 inf->error = TRUE; 5767 return FALSE; 5768 } 5769 memcpy (alc, name, p - name); 5770 alc[p - name] = '\0'; 5771 name = alc; 5772 } 5773 } 5774 5775 /* Compute the hash value. */ 5776 ha = bfd_elf_hash (name); 5777 5778 /* Store the found hash value in the array given as the argument. */ 5779 *(inf->hashcodes)++ = ha; 5780 5781 /* And store it in the struct so that we can put it in the hash table 5782 later. */ 5783 h->u.elf_hash_value = ha; 5784 5785 if (alc != NULL) 5786 free (alc); 5787 5788 return TRUE; 5789 } 5790 5791 struct collect_gnu_hash_codes 5792 { 5793 bfd *output_bfd; 5794 const struct elf_backend_data *bed; 5795 unsigned long int nsyms; 5796 unsigned long int maskbits; 5797 unsigned long int *hashcodes; 5798 unsigned long int *hashval; 5799 unsigned long int *indx; 5800 unsigned long int *counts; 5801 bfd_vma *bitmask; 5802 bfd_byte *contents; 5803 long int min_dynindx; 5804 unsigned long int bucketcount; 5805 unsigned long int symindx; 5806 long int local_indx; 5807 long int shift1, shift2; 5808 unsigned long int mask; 5809 bfd_boolean error; 5810 }; 5811 5812 /* This function will be called though elf_link_hash_traverse to store 5813 all hash value of the exported symbols in an array. */ 5814 5815 static bfd_boolean 5816 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) 5817 { 5818 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5819 const char *name; 5820 unsigned long ha; 5821 char *alc = NULL; 5822 5823 /* Ignore indirect symbols. These are added by the versioning code. */ 5824 if (h->dynindx == -1) 5825 return TRUE; 5826 5827 /* Ignore also local symbols and undefined symbols. */ 5828 if (! (*s->bed->elf_hash_symbol) (h)) 5829 return TRUE; 5830 5831 name = h->root.root.string; 5832 if (h->versioned >= versioned) 5833 { 5834 char *p = strchr (name, ELF_VER_CHR); 5835 if (p != NULL) 5836 { 5837 alc = (char *) bfd_malloc (p - name + 1); 5838 if (alc == NULL) 5839 { 5840 s->error = TRUE; 5841 return FALSE; 5842 } 5843 memcpy (alc, name, p - name); 5844 alc[p - name] = '\0'; 5845 name = alc; 5846 } 5847 } 5848 5849 /* Compute the hash value. */ 5850 ha = bfd_elf_gnu_hash (name); 5851 5852 /* Store the found hash value in the array for compute_bucket_count, 5853 and also for .dynsym reordering purposes. */ 5854 s->hashcodes[s->nsyms] = ha; 5855 s->hashval[h->dynindx] = ha; 5856 ++s->nsyms; 5857 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) 5858 s->min_dynindx = h->dynindx; 5859 5860 if (alc != NULL) 5861 free (alc); 5862 5863 return TRUE; 5864 } 5865 5866 /* This function will be called though elf_link_hash_traverse to do 5867 final dynaminc symbol renumbering. */ 5868 5869 static bfd_boolean 5870 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) 5871 { 5872 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5873 unsigned long int bucket; 5874 unsigned long int val; 5875 5876 /* Ignore indirect symbols. */ 5877 if (h->dynindx == -1) 5878 return TRUE; 5879 5880 /* Ignore also local symbols and undefined symbols. */ 5881 if (! (*s->bed->elf_hash_symbol) (h)) 5882 { 5883 if (h->dynindx >= s->min_dynindx) 5884 h->dynindx = s->local_indx++; 5885 return TRUE; 5886 } 5887 5888 bucket = s->hashval[h->dynindx] % s->bucketcount; 5889 val = (s->hashval[h->dynindx] >> s->shift1) 5890 & ((s->maskbits >> s->shift1) - 1); 5891 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); 5892 s->bitmask[val] 5893 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); 5894 val = s->hashval[h->dynindx] & ~(unsigned long int) 1; 5895 if (s->counts[bucket] == 1) 5896 /* Last element terminates the chain. */ 5897 val |= 1; 5898 bfd_put_32 (s->output_bfd, val, 5899 s->contents + (s->indx[bucket] - s->symindx) * 4); 5900 --s->counts[bucket]; 5901 h->dynindx = s->indx[bucket]++; 5902 return TRUE; 5903 } 5904 5905 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 5906 5907 bfd_boolean 5908 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) 5909 { 5910 return !(h->forced_local 5911 || h->root.type == bfd_link_hash_undefined 5912 || h->root.type == bfd_link_hash_undefweak 5913 || ((h->root.type == bfd_link_hash_defined 5914 || h->root.type == bfd_link_hash_defweak) 5915 && h->root.u.def.section->output_section == NULL)); 5916 } 5917 5918 /* Array used to determine the number of hash table buckets to use 5919 based on the number of symbols there are. If there are fewer than 5920 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, 5921 fewer than 37 we use 17 buckets, and so forth. We never use more 5922 than 32771 buckets. */ 5923 5924 static const size_t elf_buckets[] = 5925 { 5926 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 5927 16411, 32771, 0 5928 }; 5929 5930 /* Compute bucket count for hashing table. We do not use a static set 5931 of possible tables sizes anymore. Instead we determine for all 5932 possible reasonable sizes of the table the outcome (i.e., the 5933 number of collisions etc) and choose the best solution. The 5934 weighting functions are not too simple to allow the table to grow 5935 without bounds. Instead one of the weighting factors is the size. 5936 Therefore the result is always a good payoff between few collisions 5937 (= short chain lengths) and table size. */ 5938 static size_t 5939 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, 5940 unsigned long int *hashcodes ATTRIBUTE_UNUSED, 5941 unsigned long int nsyms, 5942 int gnu_hash) 5943 { 5944 size_t best_size = 0; 5945 unsigned long int i; 5946 5947 /* We have a problem here. The following code to optimize the table 5948 size requires an integer type with more the 32 bits. If 5949 BFD_HOST_U_64_BIT is set we know about such a type. */ 5950 #ifdef BFD_HOST_U_64_BIT 5951 if (info->optimize) 5952 { 5953 size_t minsize; 5954 size_t maxsize; 5955 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); 5956 bfd *dynobj = elf_hash_table (info)->dynobj; 5957 size_t dynsymcount = elf_hash_table (info)->dynsymcount; 5958 const struct elf_backend_data *bed = get_elf_backend_data (dynobj); 5959 unsigned long int *counts; 5960 bfd_size_type amt; 5961 unsigned int no_improvement_count = 0; 5962 5963 /* Possible optimization parameters: if we have NSYMS symbols we say 5964 that the hashing table must at least have NSYMS/4 and at most 5965 2*NSYMS buckets. */ 5966 minsize = nsyms / 4; 5967 if (minsize == 0) 5968 minsize = 1; 5969 best_size = maxsize = nsyms * 2; 5970 if (gnu_hash) 5971 { 5972 if (minsize < 2) 5973 minsize = 2; 5974 if ((best_size & 31) == 0) 5975 ++best_size; 5976 } 5977 5978 /* Create array where we count the collisions in. We must use bfd_malloc 5979 since the size could be large. */ 5980 amt = maxsize; 5981 amt *= sizeof (unsigned long int); 5982 counts = (unsigned long int *) bfd_malloc (amt); 5983 if (counts == NULL) 5984 return 0; 5985 5986 /* Compute the "optimal" size for the hash table. The criteria is a 5987 minimal chain length. The minor criteria is (of course) the size 5988 of the table. */ 5989 for (i = minsize; i < maxsize; ++i) 5990 { 5991 /* Walk through the array of hashcodes and count the collisions. */ 5992 BFD_HOST_U_64_BIT max; 5993 unsigned long int j; 5994 unsigned long int fact; 5995 5996 if (gnu_hash && (i & 31) == 0) 5997 continue; 5998 5999 memset (counts, '\0', i * sizeof (unsigned long int)); 6000 6001 /* Determine how often each hash bucket is used. */ 6002 for (j = 0; j < nsyms; ++j) 6003 ++counts[hashcodes[j] % i]; 6004 6005 /* For the weight function we need some information about the 6006 pagesize on the target. This is information need not be 100% 6007 accurate. Since this information is not available (so far) we 6008 define it here to a reasonable default value. If it is crucial 6009 to have a better value some day simply define this value. */ 6010 # ifndef BFD_TARGET_PAGESIZE 6011 # define BFD_TARGET_PAGESIZE (4096) 6012 # endif 6013 6014 /* We in any case need 2 + DYNSYMCOUNT entries for the size values 6015 and the chains. */ 6016 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; 6017 6018 # if 1 6019 /* Variant 1: optimize for short chains. We add the squares 6020 of all the chain lengths (which favors many small chain 6021 over a few long chains). */ 6022 for (j = 0; j < i; ++j) 6023 max += counts[j] * counts[j]; 6024 6025 /* This adds penalties for the overall size of the table. */ 6026 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 6027 max *= fact * fact; 6028 # else 6029 /* Variant 2: Optimize a lot more for small table. Here we 6030 also add squares of the size but we also add penalties for 6031 empty slots (the +1 term). */ 6032 for (j = 0; j < i; ++j) 6033 max += (1 + counts[j]) * (1 + counts[j]); 6034 6035 /* The overall size of the table is considered, but not as 6036 strong as in variant 1, where it is squared. */ 6037 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 6038 max *= fact; 6039 # endif 6040 6041 /* Compare with current best results. */ 6042 if (max < best_chlen) 6043 { 6044 best_chlen = max; 6045 best_size = i; 6046 no_improvement_count = 0; 6047 } 6048 /* PR 11843: Avoid futile long searches for the best bucket size 6049 when there are a large number of symbols. */ 6050 else if (++no_improvement_count == 100) 6051 break; 6052 } 6053 6054 free (counts); 6055 } 6056 else 6057 #endif /* defined (BFD_HOST_U_64_BIT) */ 6058 { 6059 /* This is the fallback solution if no 64bit type is available or if we 6060 are not supposed to spend much time on optimizations. We select the 6061 bucket count using a fixed set of numbers. */ 6062 for (i = 0; elf_buckets[i] != 0; i++) 6063 { 6064 best_size = elf_buckets[i]; 6065 if (nsyms < elf_buckets[i + 1]) 6066 break; 6067 } 6068 if (gnu_hash && best_size < 2) 6069 best_size = 2; 6070 } 6071 6072 return best_size; 6073 } 6074 6075 /* Size any SHT_GROUP section for ld -r. */ 6076 6077 bfd_boolean 6078 _bfd_elf_size_group_sections (struct bfd_link_info *info) 6079 { 6080 bfd *ibfd; 6081 asection *s; 6082 6083 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 6084 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour 6085 && (s = ibfd->sections) != NULL 6086 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS 6087 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) 6088 return FALSE; 6089 return TRUE; 6090 } 6091 6092 /* Set a default stack segment size. The value in INFO wins. If it 6093 is unset, LEGACY_SYMBOL's value is used, and if that symbol is 6094 undefined it is initialized. */ 6095 6096 bfd_boolean 6097 bfd_elf_stack_segment_size (bfd *output_bfd, 6098 struct bfd_link_info *info, 6099 const char *legacy_symbol, 6100 bfd_vma default_size) 6101 { 6102 struct elf_link_hash_entry *h = NULL; 6103 6104 /* Look for legacy symbol. */ 6105 if (legacy_symbol) 6106 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, 6107 FALSE, FALSE, FALSE); 6108 if (h && (h->root.type == bfd_link_hash_defined 6109 || h->root.type == bfd_link_hash_defweak) 6110 && h->def_regular 6111 && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) 6112 { 6113 /* The symbol has no type if specified on the command line. */ 6114 h->type = STT_OBJECT; 6115 if (info->stacksize) 6116 /* xgettext:c-format */ 6117 _bfd_error_handler (_("%pB: stack size specified and %s set"), 6118 output_bfd, legacy_symbol); 6119 else if (h->root.u.def.section != bfd_abs_section_ptr) 6120 /* xgettext:c-format */ 6121 _bfd_error_handler (_("%pB: %s not absolute"), 6122 output_bfd, legacy_symbol); 6123 else 6124 info->stacksize = h->root.u.def.value; 6125 } 6126 6127 if (!info->stacksize) 6128 /* If the user didn't set a size, or explicitly inhibit the 6129 size, set it now. */ 6130 info->stacksize = default_size; 6131 6132 /* Provide the legacy symbol, if it is referenced. */ 6133 if (h && (h->root.type == bfd_link_hash_undefined 6134 || h->root.type == bfd_link_hash_undefweak)) 6135 { 6136 struct bfd_link_hash_entry *bh = NULL; 6137 6138 if (!(_bfd_generic_link_add_one_symbol 6139 (info, output_bfd, legacy_symbol, 6140 BSF_GLOBAL, bfd_abs_section_ptr, 6141 info->stacksize >= 0 ? info->stacksize : 0, 6142 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) 6143 return FALSE; 6144 6145 h = (struct elf_link_hash_entry *) bh; 6146 h->def_regular = 1; 6147 h->type = STT_OBJECT; 6148 } 6149 6150 return TRUE; 6151 } 6152 6153 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ 6154 6155 struct elf_gc_sweep_symbol_info 6156 { 6157 struct bfd_link_info *info; 6158 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, 6159 bfd_boolean); 6160 }; 6161 6162 static bfd_boolean 6163 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) 6164 { 6165 if (!h->mark 6166 && (((h->root.type == bfd_link_hash_defined 6167 || h->root.type == bfd_link_hash_defweak) 6168 && !((h->def_regular || ELF_COMMON_DEF_P (h)) 6169 && h->root.u.def.section->gc_mark)) 6170 || h->root.type == bfd_link_hash_undefined 6171 || h->root.type == bfd_link_hash_undefweak)) 6172 { 6173 struct elf_gc_sweep_symbol_info *inf; 6174 6175 inf = (struct elf_gc_sweep_symbol_info *) data; 6176 (*inf->hide_symbol) (inf->info, h, TRUE); 6177 h->def_regular = 0; 6178 h->ref_regular = 0; 6179 h->ref_regular_nonweak = 0; 6180 } 6181 6182 return TRUE; 6183 } 6184 6185 /* Set up the sizes and contents of the ELF dynamic sections. This is 6186 called by the ELF linker emulation before_allocation routine. We 6187 must set the sizes of the sections before the linker sets the 6188 addresses of the various sections. */ 6189 6190 bfd_boolean 6191 bfd_elf_size_dynamic_sections (bfd *output_bfd, 6192 const char *soname, 6193 const char *rpath, 6194 const char *filter_shlib, 6195 const char *audit, 6196 const char *depaudit, 6197 const char * const *auxiliary_filters, 6198 struct bfd_link_info *info, 6199 asection **sinterpptr) 6200 { 6201 bfd *dynobj; 6202 const struct elf_backend_data *bed; 6203 6204 *sinterpptr = NULL; 6205 6206 if (!is_elf_hash_table (info->hash)) 6207 return TRUE; 6208 6209 dynobj = elf_hash_table (info)->dynobj; 6210 6211 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6212 { 6213 struct bfd_elf_version_tree *verdefs; 6214 struct elf_info_failed asvinfo; 6215 struct bfd_elf_version_tree *t; 6216 struct bfd_elf_version_expr *d; 6217 asection *s; 6218 size_t soname_indx; 6219 6220 /* If we are supposed to export all symbols into the dynamic symbol 6221 table (this is not the normal case), then do so. */ 6222 if (info->export_dynamic 6223 || (bfd_link_executable (info) && info->dynamic)) 6224 { 6225 struct elf_info_failed eif; 6226 6227 eif.info = info; 6228 eif.failed = FALSE; 6229 elf_link_hash_traverse (elf_hash_table (info), 6230 _bfd_elf_export_symbol, 6231 &eif); 6232 if (eif.failed) 6233 return FALSE; 6234 } 6235 6236 if (soname != NULL) 6237 { 6238 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6239 soname, TRUE); 6240 if (soname_indx == (size_t) -1 6241 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) 6242 return FALSE; 6243 } 6244 else 6245 soname_indx = (size_t) -1; 6246 6247 /* Make all global versions with definition. */ 6248 for (t = info->version_info; t != NULL; t = t->next) 6249 for (d = t->globals.list; d != NULL; d = d->next) 6250 if (!d->symver && d->literal) 6251 { 6252 const char *verstr, *name; 6253 size_t namelen, verlen, newlen; 6254 char *newname, *p, leading_char; 6255 struct elf_link_hash_entry *newh; 6256 6257 leading_char = bfd_get_symbol_leading_char (output_bfd); 6258 name = d->pattern; 6259 namelen = strlen (name) + (leading_char != '\0'); 6260 verstr = t->name; 6261 verlen = strlen (verstr); 6262 newlen = namelen + verlen + 3; 6263 6264 newname = (char *) bfd_malloc (newlen); 6265 if (newname == NULL) 6266 return FALSE; 6267 newname[0] = leading_char; 6268 memcpy (newname + (leading_char != '\0'), name, namelen); 6269 6270 /* Check the hidden versioned definition. */ 6271 p = newname + namelen; 6272 *p++ = ELF_VER_CHR; 6273 memcpy (p, verstr, verlen + 1); 6274 newh = elf_link_hash_lookup (elf_hash_table (info), 6275 newname, FALSE, FALSE, 6276 FALSE); 6277 if (newh == NULL 6278 || (newh->root.type != bfd_link_hash_defined 6279 && newh->root.type != bfd_link_hash_defweak)) 6280 { 6281 /* Check the default versioned definition. */ 6282 *p++ = ELF_VER_CHR; 6283 memcpy (p, verstr, verlen + 1); 6284 newh = elf_link_hash_lookup (elf_hash_table (info), 6285 newname, FALSE, FALSE, 6286 FALSE); 6287 } 6288 free (newname); 6289 6290 /* Mark this version if there is a definition and it is 6291 not defined in a shared object. */ 6292 if (newh != NULL 6293 && !newh->def_dynamic 6294 && (newh->root.type == bfd_link_hash_defined 6295 || newh->root.type == bfd_link_hash_defweak)) 6296 d->symver = 1; 6297 } 6298 6299 /* Attach all the symbols to their version information. */ 6300 asvinfo.info = info; 6301 asvinfo.failed = FALSE; 6302 6303 elf_link_hash_traverse (elf_hash_table (info), 6304 _bfd_elf_link_assign_sym_version, 6305 &asvinfo); 6306 if (asvinfo.failed) 6307 return FALSE; 6308 6309 if (!info->allow_undefined_version) 6310 { 6311 /* Check if all global versions have a definition. */ 6312 bfd_boolean all_defined = TRUE; 6313 for (t = info->version_info; t != NULL; t = t->next) 6314 for (d = t->globals.list; d != NULL; d = d->next) 6315 if (d->literal && !d->symver && !d->script) 6316 { 6317 _bfd_error_handler 6318 (_("%s: undefined version: %s"), 6319 d->pattern, t->name); 6320 all_defined = FALSE; 6321 } 6322 6323 if (!all_defined) 6324 { 6325 bfd_set_error (bfd_error_bad_value); 6326 return FALSE; 6327 } 6328 } 6329 6330 /* Set up the version definition section. */ 6331 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 6332 BFD_ASSERT (s != NULL); 6333 6334 /* We may have created additional version definitions if we are 6335 just linking a regular application. */ 6336 verdefs = info->version_info; 6337 6338 /* Skip anonymous version tag. */ 6339 if (verdefs != NULL && verdefs->vernum == 0) 6340 verdefs = verdefs->next; 6341 6342 if (verdefs == NULL && !info->create_default_symver) 6343 s->flags |= SEC_EXCLUDE; 6344 else 6345 { 6346 unsigned int cdefs; 6347 bfd_size_type size; 6348 bfd_byte *p; 6349 Elf_Internal_Verdef def; 6350 Elf_Internal_Verdaux defaux; 6351 struct bfd_link_hash_entry *bh; 6352 struct elf_link_hash_entry *h; 6353 const char *name; 6354 6355 cdefs = 0; 6356 size = 0; 6357 6358 /* Make space for the base version. */ 6359 size += sizeof (Elf_External_Verdef); 6360 size += sizeof (Elf_External_Verdaux); 6361 ++cdefs; 6362 6363 /* Make space for the default version. */ 6364 if (info->create_default_symver) 6365 { 6366 size += sizeof (Elf_External_Verdef); 6367 ++cdefs; 6368 } 6369 6370 for (t = verdefs; t != NULL; t = t->next) 6371 { 6372 struct bfd_elf_version_deps *n; 6373 6374 /* Don't emit base version twice. */ 6375 if (t->vernum == 0) 6376 continue; 6377 6378 size += sizeof (Elf_External_Verdef); 6379 size += sizeof (Elf_External_Verdaux); 6380 ++cdefs; 6381 6382 for (n = t->deps; n != NULL; n = n->next) 6383 size += sizeof (Elf_External_Verdaux); 6384 } 6385 6386 s->size = size; 6387 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6388 if (s->contents == NULL && s->size != 0) 6389 return FALSE; 6390 6391 /* Fill in the version definition section. */ 6392 6393 p = s->contents; 6394 6395 def.vd_version = VER_DEF_CURRENT; 6396 def.vd_flags = VER_FLG_BASE; 6397 def.vd_ndx = 1; 6398 def.vd_cnt = 1; 6399 if (info->create_default_symver) 6400 { 6401 def.vd_aux = 2 * sizeof (Elf_External_Verdef); 6402 def.vd_next = sizeof (Elf_External_Verdef); 6403 } 6404 else 6405 { 6406 def.vd_aux = sizeof (Elf_External_Verdef); 6407 def.vd_next = (sizeof (Elf_External_Verdef) 6408 + sizeof (Elf_External_Verdaux)); 6409 } 6410 6411 if (soname_indx != (size_t) -1) 6412 { 6413 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6414 soname_indx); 6415 def.vd_hash = bfd_elf_hash (soname); 6416 defaux.vda_name = soname_indx; 6417 name = soname; 6418 } 6419 else 6420 { 6421 size_t indx; 6422 6423 name = lbasename (output_bfd->filename); 6424 def.vd_hash = bfd_elf_hash (name); 6425 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6426 name, FALSE); 6427 if (indx == (size_t) -1) 6428 return FALSE; 6429 defaux.vda_name = indx; 6430 } 6431 defaux.vda_next = 0; 6432 6433 _bfd_elf_swap_verdef_out (output_bfd, &def, 6434 (Elf_External_Verdef *) p); 6435 p += sizeof (Elf_External_Verdef); 6436 if (info->create_default_symver) 6437 { 6438 /* Add a symbol representing this version. */ 6439 bh = NULL; 6440 if (! (_bfd_generic_link_add_one_symbol 6441 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, 6442 0, NULL, FALSE, 6443 get_elf_backend_data (dynobj)->collect, &bh))) 6444 return FALSE; 6445 h = (struct elf_link_hash_entry *) bh; 6446 h->non_elf = 0; 6447 h->def_regular = 1; 6448 h->type = STT_OBJECT; 6449 h->verinfo.vertree = NULL; 6450 6451 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6452 return FALSE; 6453 6454 /* Create a duplicate of the base version with the same 6455 aux block, but different flags. */ 6456 def.vd_flags = 0; 6457 def.vd_ndx = 2; 6458 def.vd_aux = sizeof (Elf_External_Verdef); 6459 if (verdefs) 6460 def.vd_next = (sizeof (Elf_External_Verdef) 6461 + sizeof (Elf_External_Verdaux)); 6462 else 6463 def.vd_next = 0; 6464 _bfd_elf_swap_verdef_out (output_bfd, &def, 6465 (Elf_External_Verdef *) p); 6466 p += sizeof (Elf_External_Verdef); 6467 } 6468 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6469 (Elf_External_Verdaux *) p); 6470 p += sizeof (Elf_External_Verdaux); 6471 6472 for (t = verdefs; t != NULL; t = t->next) 6473 { 6474 unsigned int cdeps; 6475 struct bfd_elf_version_deps *n; 6476 6477 /* Don't emit the base version twice. */ 6478 if (t->vernum == 0) 6479 continue; 6480 6481 cdeps = 0; 6482 for (n = t->deps; n != NULL; n = n->next) 6483 ++cdeps; 6484 6485 /* Add a symbol representing this version. */ 6486 bh = NULL; 6487 if (! (_bfd_generic_link_add_one_symbol 6488 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, 6489 0, NULL, FALSE, 6490 get_elf_backend_data (dynobj)->collect, &bh))) 6491 return FALSE; 6492 h = (struct elf_link_hash_entry *) bh; 6493 h->non_elf = 0; 6494 h->def_regular = 1; 6495 h->type = STT_OBJECT; 6496 h->verinfo.vertree = t; 6497 6498 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6499 return FALSE; 6500 6501 def.vd_version = VER_DEF_CURRENT; 6502 def.vd_flags = 0; 6503 if (t->globals.list == NULL 6504 && t->locals.list == NULL 6505 && ! t->used) 6506 def.vd_flags |= VER_FLG_WEAK; 6507 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); 6508 def.vd_cnt = cdeps + 1; 6509 def.vd_hash = bfd_elf_hash (t->name); 6510 def.vd_aux = sizeof (Elf_External_Verdef); 6511 def.vd_next = 0; 6512 6513 /* If a basever node is next, it *must* be the last node in 6514 the chain, otherwise Verdef construction breaks. */ 6515 if (t->next != NULL && t->next->vernum == 0) 6516 BFD_ASSERT (t->next->next == NULL); 6517 6518 if (t->next != NULL && t->next->vernum != 0) 6519 def.vd_next = (sizeof (Elf_External_Verdef) 6520 + (cdeps + 1) * sizeof (Elf_External_Verdaux)); 6521 6522 _bfd_elf_swap_verdef_out (output_bfd, &def, 6523 (Elf_External_Verdef *) p); 6524 p += sizeof (Elf_External_Verdef); 6525 6526 defaux.vda_name = h->dynstr_index; 6527 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6528 h->dynstr_index); 6529 defaux.vda_next = 0; 6530 if (t->deps != NULL) 6531 defaux.vda_next = sizeof (Elf_External_Verdaux); 6532 t->name_indx = defaux.vda_name; 6533 6534 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6535 (Elf_External_Verdaux *) p); 6536 p += sizeof (Elf_External_Verdaux); 6537 6538 for (n = t->deps; n != NULL; n = n->next) 6539 { 6540 if (n->version_needed == NULL) 6541 { 6542 /* This can happen if there was an error in the 6543 version script. */ 6544 defaux.vda_name = 0; 6545 } 6546 else 6547 { 6548 defaux.vda_name = n->version_needed->name_indx; 6549 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6550 defaux.vda_name); 6551 } 6552 if (n->next == NULL) 6553 defaux.vda_next = 0; 6554 else 6555 defaux.vda_next = sizeof (Elf_External_Verdaux); 6556 6557 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6558 (Elf_External_Verdaux *) p); 6559 p += sizeof (Elf_External_Verdaux); 6560 } 6561 } 6562 6563 elf_tdata (output_bfd)->cverdefs = cdefs; 6564 } 6565 } 6566 6567 bed = get_elf_backend_data (output_bfd); 6568 6569 if (info->gc_sections && bed->can_gc_sections) 6570 { 6571 struct elf_gc_sweep_symbol_info sweep_info; 6572 6573 /* Remove the symbols that were in the swept sections from the 6574 dynamic symbol table. */ 6575 sweep_info.info = info; 6576 sweep_info.hide_symbol = bed->elf_backend_hide_symbol; 6577 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, 6578 &sweep_info); 6579 } 6580 6581 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6582 { 6583 asection *s; 6584 struct elf_find_verdep_info sinfo; 6585 6586 /* Work out the size of the version reference section. */ 6587 6588 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 6589 BFD_ASSERT (s != NULL); 6590 6591 sinfo.info = info; 6592 sinfo.vers = elf_tdata (output_bfd)->cverdefs; 6593 if (sinfo.vers == 0) 6594 sinfo.vers = 1; 6595 sinfo.failed = FALSE; 6596 6597 elf_link_hash_traverse (elf_hash_table (info), 6598 _bfd_elf_link_find_version_dependencies, 6599 &sinfo); 6600 if (sinfo.failed) 6601 return FALSE; 6602 6603 if (elf_tdata (output_bfd)->verref == NULL) 6604 s->flags |= SEC_EXCLUDE; 6605 else 6606 { 6607 Elf_Internal_Verneed *vn; 6608 unsigned int size; 6609 unsigned int crefs; 6610 bfd_byte *p; 6611 6612 /* Build the version dependency section. */ 6613 size = 0; 6614 crefs = 0; 6615 for (vn = elf_tdata (output_bfd)->verref; 6616 vn != NULL; 6617 vn = vn->vn_nextref) 6618 { 6619 Elf_Internal_Vernaux *a; 6620 6621 size += sizeof (Elf_External_Verneed); 6622 ++crefs; 6623 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 6624 size += sizeof (Elf_External_Vernaux); 6625 } 6626 6627 s->size = size; 6628 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6629 if (s->contents == NULL) 6630 return FALSE; 6631 6632 p = s->contents; 6633 for (vn = elf_tdata (output_bfd)->verref; 6634 vn != NULL; 6635 vn = vn->vn_nextref) 6636 { 6637 unsigned int caux; 6638 Elf_Internal_Vernaux *a; 6639 size_t indx; 6640 6641 caux = 0; 6642 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 6643 ++caux; 6644 6645 vn->vn_version = VER_NEED_CURRENT; 6646 vn->vn_cnt = caux; 6647 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6648 elf_dt_name (vn->vn_bfd) != NULL 6649 ? elf_dt_name (vn->vn_bfd) 6650 : lbasename (vn->vn_bfd->filename), 6651 FALSE); 6652 if (indx == (size_t) -1) 6653 return FALSE; 6654 vn->vn_file = indx; 6655 vn->vn_aux = sizeof (Elf_External_Verneed); 6656 if (vn->vn_nextref == NULL) 6657 vn->vn_next = 0; 6658 else 6659 vn->vn_next = (sizeof (Elf_External_Verneed) 6660 + caux * sizeof (Elf_External_Vernaux)); 6661 6662 _bfd_elf_swap_verneed_out (output_bfd, vn, 6663 (Elf_External_Verneed *) p); 6664 p += sizeof (Elf_External_Verneed); 6665 6666 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 6667 { 6668 a->vna_hash = bfd_elf_hash (a->vna_nodename); 6669 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6670 a->vna_nodename, FALSE); 6671 if (indx == (size_t) -1) 6672 return FALSE; 6673 a->vna_name = indx; 6674 if (a->vna_nextptr == NULL) 6675 a->vna_next = 0; 6676 else 6677 a->vna_next = sizeof (Elf_External_Vernaux); 6678 6679 _bfd_elf_swap_vernaux_out (output_bfd, a, 6680 (Elf_External_Vernaux *) p); 6681 p += sizeof (Elf_External_Vernaux); 6682 } 6683 } 6684 6685 elf_tdata (output_bfd)->cverrefs = crefs; 6686 } 6687 } 6688 6689 /* Any syms created from now on start with -1 in 6690 got.refcount/offset and plt.refcount/offset. */ 6691 elf_hash_table (info)->init_got_refcount 6692 = elf_hash_table (info)->init_got_offset; 6693 elf_hash_table (info)->init_plt_refcount 6694 = elf_hash_table (info)->init_plt_offset; 6695 6696 if (bfd_link_relocatable (info) 6697 && !_bfd_elf_size_group_sections (info)) 6698 return FALSE; 6699 6700 /* The backend may have to create some sections regardless of whether 6701 we're dynamic or not. */ 6702 if (bed->elf_backend_always_size_sections 6703 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) 6704 return FALSE; 6705 6706 /* Determine any GNU_STACK segment requirements, after the backend 6707 has had a chance to set a default segment size. */ 6708 if (info->execstack) 6709 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; 6710 else if (info->noexecstack) 6711 elf_stack_flags (output_bfd) = PF_R | PF_W; 6712 else 6713 { 6714 bfd *inputobj; 6715 asection *notesec = NULL; 6716 int exec = 0; 6717 6718 for (inputobj = info->input_bfds; 6719 inputobj; 6720 inputobj = inputobj->link.next) 6721 { 6722 asection *s; 6723 6724 if (inputobj->flags 6725 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) 6726 continue; 6727 s = inputobj->sections; 6728 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 6729 continue; 6730 6731 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); 6732 if (s) 6733 { 6734 if (s->flags & SEC_CODE) 6735 exec = PF_X; 6736 notesec = s; 6737 } 6738 else if (bed->default_execstack) 6739 exec = PF_X; 6740 } 6741 if (notesec || info->stacksize > 0) 6742 elf_stack_flags (output_bfd) = PF_R | PF_W | exec; 6743 if (notesec && exec && bfd_link_relocatable (info) 6744 && notesec->output_section != bfd_abs_section_ptr) 6745 notesec->output_section->flags |= SEC_CODE; 6746 } 6747 6748 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6749 { 6750 struct elf_info_failed eif; 6751 struct elf_link_hash_entry *h; 6752 asection *dynstr; 6753 asection *s; 6754 6755 *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); 6756 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); 6757 6758 if (info->symbolic) 6759 { 6760 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) 6761 return FALSE; 6762 info->flags |= DF_SYMBOLIC; 6763 } 6764 6765 if (rpath != NULL) 6766 { 6767 size_t indx; 6768 bfd_vma tag; 6769 6770 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, 6771 TRUE); 6772 if (indx == (size_t) -1) 6773 return FALSE; 6774 6775 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; 6776 if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) 6777 return FALSE; 6778 } 6779 6780 if (filter_shlib != NULL) 6781 { 6782 size_t indx; 6783 6784 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6785 filter_shlib, TRUE); 6786 if (indx == (size_t) -1 6787 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) 6788 return FALSE; 6789 } 6790 6791 if (auxiliary_filters != NULL) 6792 { 6793 const char * const *p; 6794 6795 for (p = auxiliary_filters; *p != NULL; p++) 6796 { 6797 size_t indx; 6798 6799 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6800 *p, TRUE); 6801 if (indx == (size_t) -1 6802 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) 6803 return FALSE; 6804 } 6805 } 6806 6807 if (audit != NULL) 6808 { 6809 size_t indx; 6810 6811 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, 6812 TRUE); 6813 if (indx == (size_t) -1 6814 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) 6815 return FALSE; 6816 } 6817 6818 if (depaudit != NULL) 6819 { 6820 size_t indx; 6821 6822 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, 6823 TRUE); 6824 if (indx == (size_t) -1 6825 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) 6826 return FALSE; 6827 } 6828 6829 eif.info = info; 6830 eif.failed = FALSE; 6831 6832 /* Find all symbols which were defined in a dynamic object and make 6833 the backend pick a reasonable value for them. */ 6834 elf_link_hash_traverse (elf_hash_table (info), 6835 _bfd_elf_adjust_dynamic_symbol, 6836 &eif); 6837 if (eif.failed) 6838 return FALSE; 6839 6840 /* Add some entries to the .dynamic section. We fill in some of the 6841 values later, in bfd_elf_final_link, but we must add the entries 6842 now so that we know the final size of the .dynamic section. */ 6843 6844 /* If there are initialization and/or finalization functions to 6845 call then add the corresponding DT_INIT/DT_FINI entries. */ 6846 h = (info->init_function 6847 ? elf_link_hash_lookup (elf_hash_table (info), 6848 info->init_function, FALSE, 6849 FALSE, FALSE) 6850 : NULL); 6851 if (h != NULL 6852 && (h->ref_regular 6853 || h->def_regular)) 6854 { 6855 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) 6856 return FALSE; 6857 } 6858 h = (info->fini_function 6859 ? elf_link_hash_lookup (elf_hash_table (info), 6860 info->fini_function, FALSE, 6861 FALSE, FALSE) 6862 : NULL); 6863 if (h != NULL 6864 && (h->ref_regular 6865 || h->def_regular)) 6866 { 6867 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) 6868 return FALSE; 6869 } 6870 6871 s = bfd_get_section_by_name (output_bfd, ".preinit_array"); 6872 if (s != NULL && s->linker_has_input) 6873 { 6874 /* DT_PREINIT_ARRAY is not allowed in shared library. */ 6875 if (! bfd_link_executable (info)) 6876 { 6877 bfd *sub; 6878 asection *o; 6879 6880 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 6881 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 6882 && (o = sub->sections) != NULL 6883 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) 6884 for (o = sub->sections; o != NULL; o = o->next) 6885 if (elf_section_data (o)->this_hdr.sh_type 6886 == SHT_PREINIT_ARRAY) 6887 { 6888 _bfd_error_handler 6889 (_("%pB: .preinit_array section is not allowed in DSO"), 6890 sub); 6891 break; 6892 } 6893 6894 bfd_set_error (bfd_error_nonrepresentable_section); 6895 return FALSE; 6896 } 6897 6898 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) 6899 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) 6900 return FALSE; 6901 } 6902 s = bfd_get_section_by_name (output_bfd, ".init_array"); 6903 if (s != NULL && s->linker_has_input) 6904 { 6905 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) 6906 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) 6907 return FALSE; 6908 } 6909 s = bfd_get_section_by_name (output_bfd, ".fini_array"); 6910 if (s != NULL && s->linker_has_input) 6911 { 6912 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) 6913 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) 6914 return FALSE; 6915 } 6916 6917 dynstr = bfd_get_linker_section (dynobj, ".dynstr"); 6918 /* If .dynstr is excluded from the link, we don't want any of 6919 these tags. Strictly, we should be checking each section 6920 individually; This quick check covers for the case where 6921 someone does a /DISCARD/ : { *(*) }. */ 6922 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) 6923 { 6924 bfd_size_type strsize; 6925 6926 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 6927 if ((info->emit_hash 6928 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) 6929 || (info->emit_gnu_hash 6930 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) 6931 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) 6932 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) 6933 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) 6934 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, 6935 bed->s->sizeof_sym)) 6936 return FALSE; 6937 } 6938 } 6939 6940 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) 6941 return FALSE; 6942 6943 /* The backend must work out the sizes of all the other dynamic 6944 sections. */ 6945 if (dynobj != NULL 6946 && bed->elf_backend_size_dynamic_sections != NULL 6947 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) 6948 return FALSE; 6949 6950 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6951 { 6952 if (elf_tdata (output_bfd)->cverdefs) 6953 { 6954 unsigned int crefs = elf_tdata (output_bfd)->cverdefs; 6955 6956 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) 6957 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) 6958 return FALSE; 6959 } 6960 6961 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) 6962 { 6963 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) 6964 return FALSE; 6965 } 6966 else if (info->flags & DF_BIND_NOW) 6967 { 6968 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) 6969 return FALSE; 6970 } 6971 6972 if (info->flags_1) 6973 { 6974 if (bfd_link_executable (info)) 6975 info->flags_1 &= ~ (DF_1_INITFIRST 6976 | DF_1_NODELETE 6977 | DF_1_NOOPEN); 6978 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) 6979 return FALSE; 6980 } 6981 6982 if (elf_tdata (output_bfd)->cverrefs) 6983 { 6984 unsigned int crefs = elf_tdata (output_bfd)->cverrefs; 6985 6986 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) 6987 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) 6988 return FALSE; 6989 } 6990 6991 if ((elf_tdata (output_bfd)->cverrefs == 0 6992 && elf_tdata (output_bfd)->cverdefs == 0) 6993 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) 6994 { 6995 asection *s; 6996 6997 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6998 s->flags |= SEC_EXCLUDE; 6999 } 7000 } 7001 return TRUE; 7002 } 7003 7004 /* Find the first non-excluded output section. We'll use its 7005 section symbol for some emitted relocs. */ 7006 void 7007 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) 7008 { 7009 asection *s; 7010 7011 for (s = output_bfd->sections; s != NULL; s = s->next) 7012 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 7013 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) 7014 { 7015 elf_hash_table (info)->text_index_section = s; 7016 break; 7017 } 7018 } 7019 7020 /* Find two non-excluded output sections, one for code, one for data. 7021 We'll use their section symbols for some emitted relocs. */ 7022 void 7023 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) 7024 { 7025 asection *s; 7026 7027 /* Data first, since setting text_index_section changes 7028 _bfd_elf_omit_section_dynsym_default. */ 7029 for (s = output_bfd->sections; s != NULL; s = s->next) 7030 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) 7031 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) 7032 { 7033 elf_hash_table (info)->data_index_section = s; 7034 break; 7035 } 7036 7037 for (s = output_bfd->sections; s != NULL; s = s->next) 7038 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) 7039 == (SEC_ALLOC | SEC_READONLY)) 7040 && !_bfd_elf_omit_section_dynsym_default (output_bfd, info, s)) 7041 { 7042 elf_hash_table (info)->text_index_section = s; 7043 break; 7044 } 7045 7046 if (elf_hash_table (info)->text_index_section == NULL) 7047 elf_hash_table (info)->text_index_section 7048 = elf_hash_table (info)->data_index_section; 7049 } 7050 7051 bfd_boolean 7052 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) 7053 { 7054 const struct elf_backend_data *bed; 7055 unsigned long section_sym_count; 7056 bfd_size_type dynsymcount = 0; 7057 7058 if (!is_elf_hash_table (info->hash)) 7059 return TRUE; 7060 7061 bed = get_elf_backend_data (output_bfd); 7062 (*bed->elf_backend_init_index_section) (output_bfd, info); 7063 7064 /* Assign dynsym indices. In a shared library we generate a section 7065 symbol for each output section, which come first. Next come all 7066 of the back-end allocated local dynamic syms, followed by the rest 7067 of the global symbols. 7068 7069 This is usually not needed for static binaries, however backends 7070 can request to always do it, e.g. the MIPS backend uses dynamic 7071 symbol counts to lay out GOT, which will be produced in the 7072 presence of GOT relocations even in static binaries (holding fixed 7073 data in that case, to satisfy those relocations). */ 7074 7075 if (elf_hash_table (info)->dynamic_sections_created 7076 || bed->always_renumber_dynsyms) 7077 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, 7078 §ion_sym_count); 7079 7080 if (elf_hash_table (info)->dynamic_sections_created) 7081 { 7082 bfd *dynobj; 7083 asection *s; 7084 unsigned int dtagcount; 7085 7086 dynobj = elf_hash_table (info)->dynobj; 7087 7088 /* Work out the size of the symbol version section. */ 7089 s = bfd_get_linker_section (dynobj, ".gnu.version"); 7090 BFD_ASSERT (s != NULL); 7091 if ((s->flags & SEC_EXCLUDE) == 0) 7092 { 7093 s->size = dynsymcount * sizeof (Elf_External_Versym); 7094 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7095 if (s->contents == NULL) 7096 return FALSE; 7097 7098 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) 7099 return FALSE; 7100 } 7101 7102 /* Set the size of the .dynsym and .hash sections. We counted 7103 the number of dynamic symbols in elf_link_add_object_symbols. 7104 We will build the contents of .dynsym and .hash when we build 7105 the final symbol table, because until then we do not know the 7106 correct value to give the symbols. We built the .dynstr 7107 section as we went along in elf_link_add_object_symbols. */ 7108 s = elf_hash_table (info)->dynsym; 7109 BFD_ASSERT (s != NULL); 7110 s->size = dynsymcount * bed->s->sizeof_sym; 7111 7112 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 7113 if (s->contents == NULL) 7114 return FALSE; 7115 7116 /* The first entry in .dynsym is a dummy symbol. Clear all the 7117 section syms, in case we don't output them all. */ 7118 ++section_sym_count; 7119 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); 7120 7121 elf_hash_table (info)->bucketcount = 0; 7122 7123 /* Compute the size of the hashing table. As a side effect this 7124 computes the hash values for all the names we export. */ 7125 if (info->emit_hash) 7126 { 7127 unsigned long int *hashcodes; 7128 struct hash_codes_info hashinf; 7129 bfd_size_type amt; 7130 unsigned long int nsyms; 7131 size_t bucketcount; 7132 size_t hash_entry_size; 7133 7134 /* Compute the hash values for all exported symbols. At the same 7135 time store the values in an array so that we could use them for 7136 optimizations. */ 7137 amt = dynsymcount * sizeof (unsigned long int); 7138 hashcodes = (unsigned long int *) bfd_malloc (amt); 7139 if (hashcodes == NULL) 7140 return FALSE; 7141 hashinf.hashcodes = hashcodes; 7142 hashinf.error = FALSE; 7143 7144 /* Put all hash values in HASHCODES. */ 7145 elf_link_hash_traverse (elf_hash_table (info), 7146 elf_collect_hash_codes, &hashinf); 7147 if (hashinf.error) 7148 { 7149 free (hashcodes); 7150 return FALSE; 7151 } 7152 7153 nsyms = hashinf.hashcodes - hashcodes; 7154 bucketcount 7155 = compute_bucket_count (info, hashcodes, nsyms, 0); 7156 free (hashcodes); 7157 7158 if (bucketcount == 0 && nsyms > 0) 7159 return FALSE; 7160 7161 elf_hash_table (info)->bucketcount = bucketcount; 7162 7163 s = bfd_get_linker_section (dynobj, ".hash"); 7164 BFD_ASSERT (s != NULL); 7165 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; 7166 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); 7167 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7168 if (s->contents == NULL) 7169 return FALSE; 7170 7171 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); 7172 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, 7173 s->contents + hash_entry_size); 7174 } 7175 7176 if (info->emit_gnu_hash) 7177 { 7178 size_t i, cnt; 7179 unsigned char *contents; 7180 struct collect_gnu_hash_codes cinfo; 7181 bfd_size_type amt; 7182 size_t bucketcount; 7183 7184 memset (&cinfo, 0, sizeof (cinfo)); 7185 7186 /* Compute the hash values for all exported symbols. At the same 7187 time store the values in an array so that we could use them for 7188 optimizations. */ 7189 amt = dynsymcount * 2 * sizeof (unsigned long int); 7190 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); 7191 if (cinfo.hashcodes == NULL) 7192 return FALSE; 7193 7194 cinfo.hashval = cinfo.hashcodes + dynsymcount; 7195 cinfo.min_dynindx = -1; 7196 cinfo.output_bfd = output_bfd; 7197 cinfo.bed = bed; 7198 7199 /* Put all hash values in HASHCODES. */ 7200 elf_link_hash_traverse (elf_hash_table (info), 7201 elf_collect_gnu_hash_codes, &cinfo); 7202 if (cinfo.error) 7203 { 7204 free (cinfo.hashcodes); 7205 return FALSE; 7206 } 7207 7208 bucketcount 7209 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); 7210 7211 if (bucketcount == 0) 7212 { 7213 free (cinfo.hashcodes); 7214 return FALSE; 7215 } 7216 7217 s = bfd_get_linker_section (dynobj, ".gnu.hash"); 7218 BFD_ASSERT (s != NULL); 7219 7220 if (cinfo.nsyms == 0) 7221 { 7222 /* Empty .gnu.hash section is special. */ 7223 BFD_ASSERT (cinfo.min_dynindx == -1); 7224 free (cinfo.hashcodes); 7225 s->size = 5 * 4 + bed->s->arch_size / 8; 7226 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7227 if (contents == NULL) 7228 return FALSE; 7229 s->contents = contents; 7230 /* 1 empty bucket. */ 7231 bfd_put_32 (output_bfd, 1, contents); 7232 /* SYMIDX above the special symbol 0. */ 7233 bfd_put_32 (output_bfd, 1, contents + 4); 7234 /* Just one word for bitmask. */ 7235 bfd_put_32 (output_bfd, 1, contents + 8); 7236 /* Only hash fn bloom filter. */ 7237 bfd_put_32 (output_bfd, 0, contents + 12); 7238 /* No hashes are valid - empty bitmask. */ 7239 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); 7240 /* No hashes in the only bucket. */ 7241 bfd_put_32 (output_bfd, 0, 7242 contents + 16 + bed->s->arch_size / 8); 7243 } 7244 else 7245 { 7246 unsigned long int maskwords, maskbitslog2, x; 7247 BFD_ASSERT (cinfo.min_dynindx != -1); 7248 7249 x = cinfo.nsyms; 7250 maskbitslog2 = 1; 7251 while ((x >>= 1) != 0) 7252 ++maskbitslog2; 7253 if (maskbitslog2 < 3) 7254 maskbitslog2 = 5; 7255 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) 7256 maskbitslog2 = maskbitslog2 + 3; 7257 else 7258 maskbitslog2 = maskbitslog2 + 2; 7259 if (bed->s->arch_size == 64) 7260 { 7261 if (maskbitslog2 == 5) 7262 maskbitslog2 = 6; 7263 cinfo.shift1 = 6; 7264 } 7265 else 7266 cinfo.shift1 = 5; 7267 cinfo.mask = (1 << cinfo.shift1) - 1; 7268 cinfo.shift2 = maskbitslog2; 7269 cinfo.maskbits = 1 << maskbitslog2; 7270 maskwords = 1 << (maskbitslog2 - cinfo.shift1); 7271 amt = bucketcount * sizeof (unsigned long int) * 2; 7272 amt += maskwords * sizeof (bfd_vma); 7273 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); 7274 if (cinfo.bitmask == NULL) 7275 { 7276 free (cinfo.hashcodes); 7277 return FALSE; 7278 } 7279 7280 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); 7281 cinfo.indx = cinfo.counts + bucketcount; 7282 cinfo.symindx = dynsymcount - cinfo.nsyms; 7283 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); 7284 7285 /* Determine how often each hash bucket is used. */ 7286 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); 7287 for (i = 0; i < cinfo.nsyms; ++i) 7288 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; 7289 7290 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) 7291 if (cinfo.counts[i] != 0) 7292 { 7293 cinfo.indx[i] = cnt; 7294 cnt += cinfo.counts[i]; 7295 } 7296 BFD_ASSERT (cnt == dynsymcount); 7297 cinfo.bucketcount = bucketcount; 7298 cinfo.local_indx = cinfo.min_dynindx; 7299 7300 s->size = (4 + bucketcount + cinfo.nsyms) * 4; 7301 s->size += cinfo.maskbits / 8; 7302 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7303 if (contents == NULL) 7304 { 7305 free (cinfo.bitmask); 7306 free (cinfo.hashcodes); 7307 return FALSE; 7308 } 7309 7310 s->contents = contents; 7311 bfd_put_32 (output_bfd, bucketcount, contents); 7312 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); 7313 bfd_put_32 (output_bfd, maskwords, contents + 8); 7314 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); 7315 contents += 16 + cinfo.maskbits / 8; 7316 7317 for (i = 0; i < bucketcount; ++i) 7318 { 7319 if (cinfo.counts[i] == 0) 7320 bfd_put_32 (output_bfd, 0, contents); 7321 else 7322 bfd_put_32 (output_bfd, cinfo.indx[i], contents); 7323 contents += 4; 7324 } 7325 7326 cinfo.contents = contents; 7327 7328 /* Renumber dynamic symbols, populate .gnu.hash section. */ 7329 elf_link_hash_traverse (elf_hash_table (info), 7330 elf_renumber_gnu_hash_syms, &cinfo); 7331 7332 contents = s->contents + 16; 7333 for (i = 0; i < maskwords; ++i) 7334 { 7335 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], 7336 contents); 7337 contents += bed->s->arch_size / 8; 7338 } 7339 7340 free (cinfo.bitmask); 7341 free (cinfo.hashcodes); 7342 } 7343 } 7344 7345 s = bfd_get_linker_section (dynobj, ".dynstr"); 7346 BFD_ASSERT (s != NULL); 7347 7348 elf_finalize_dynstr (output_bfd, info); 7349 7350 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 7351 7352 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) 7353 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) 7354 return FALSE; 7355 } 7356 7357 return TRUE; 7358 } 7359 7360 /* Make sure sec_info_type is cleared if sec_info is cleared too. */ 7361 7362 static void 7363 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, 7364 asection *sec) 7365 { 7366 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); 7367 sec->sec_info_type = SEC_INFO_TYPE_NONE; 7368 } 7369 7370 /* Finish SHF_MERGE section merging. */ 7371 7372 bfd_boolean 7373 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) 7374 { 7375 bfd *ibfd; 7376 asection *sec; 7377 7378 if (!is_elf_hash_table (info->hash)) 7379 return FALSE; 7380 7381 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 7382 if ((ibfd->flags & DYNAMIC) == 0 7383 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour 7384 && (elf_elfheader (ibfd)->e_ident[EI_CLASS] 7385 == get_elf_backend_data (obfd)->s->elfclass)) 7386 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 7387 if ((sec->flags & SEC_MERGE) != 0 7388 && !bfd_is_abs_section (sec->output_section)) 7389 { 7390 struct bfd_elf_section_data *secdata; 7391 7392 secdata = elf_section_data (sec); 7393 if (! _bfd_add_merge_section (obfd, 7394 &elf_hash_table (info)->merge_info, 7395 sec, &secdata->sec_info)) 7396 return FALSE; 7397 else if (secdata->sec_info) 7398 sec->sec_info_type = SEC_INFO_TYPE_MERGE; 7399 } 7400 7401 if (elf_hash_table (info)->merge_info != NULL) 7402 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, 7403 merge_sections_remove_hook); 7404 return TRUE; 7405 } 7406 7407 /* Create an entry in an ELF linker hash table. */ 7408 7409 struct bfd_hash_entry * 7410 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 7411 struct bfd_hash_table *table, 7412 const char *string) 7413 { 7414 /* Allocate the structure if it has not already been allocated by a 7415 subclass. */ 7416 if (entry == NULL) 7417 { 7418 entry = (struct bfd_hash_entry *) 7419 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 7420 if (entry == NULL) 7421 return entry; 7422 } 7423 7424 /* Call the allocation method of the superclass. */ 7425 entry = _bfd_link_hash_newfunc (entry, table, string); 7426 if (entry != NULL) 7427 { 7428 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 7429 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 7430 7431 /* Set local fields. */ 7432 ret->indx = -1; 7433 ret->dynindx = -1; 7434 ret->got = htab->init_got_refcount; 7435 ret->plt = htab->init_plt_refcount; 7436 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) 7437 - offsetof (struct elf_link_hash_entry, size))); 7438 /* Assume that we have been called by a non-ELF symbol reader. 7439 This flag is then reset by the code which reads an ELF input 7440 file. This ensures that a symbol created by a non-ELF symbol 7441 reader will have the flag set correctly. */ 7442 ret->non_elf = 1; 7443 } 7444 7445 return entry; 7446 } 7447 7448 /* Copy data from an indirect symbol to its direct symbol, hiding the 7449 old indirect symbol. Also used for copying flags to a weakdef. */ 7450 7451 void 7452 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, 7453 struct elf_link_hash_entry *dir, 7454 struct elf_link_hash_entry *ind) 7455 { 7456 struct elf_link_hash_table *htab; 7457 7458 /* Copy down any references that we may have already seen to the 7459 symbol which just became indirect. */ 7460 7461 if (dir->versioned != versioned_hidden) 7462 dir->ref_dynamic |= ind->ref_dynamic; 7463 dir->ref_regular |= ind->ref_regular; 7464 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 7465 dir->non_got_ref |= ind->non_got_ref; 7466 dir->needs_plt |= ind->needs_plt; 7467 dir->pointer_equality_needed |= ind->pointer_equality_needed; 7468 7469 if (ind->root.type != bfd_link_hash_indirect) 7470 return; 7471 7472 /* Copy over the global and procedure linkage table refcount entries. 7473 These may have been already set up by a check_relocs routine. */ 7474 htab = elf_hash_table (info); 7475 if (ind->got.refcount > htab->init_got_refcount.refcount) 7476 { 7477 if (dir->got.refcount < 0) 7478 dir->got.refcount = 0; 7479 dir->got.refcount += ind->got.refcount; 7480 ind->got.refcount = htab->init_got_refcount.refcount; 7481 } 7482 7483 if (ind->plt.refcount > htab->init_plt_refcount.refcount) 7484 { 7485 if (dir->plt.refcount < 0) 7486 dir->plt.refcount = 0; 7487 dir->plt.refcount += ind->plt.refcount; 7488 ind->plt.refcount = htab->init_plt_refcount.refcount; 7489 } 7490 7491 if (ind->dynindx != -1) 7492 { 7493 if (dir->dynindx != -1) 7494 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); 7495 dir->dynindx = ind->dynindx; 7496 dir->dynstr_index = ind->dynstr_index; 7497 ind->dynindx = -1; 7498 ind->dynstr_index = 0; 7499 } 7500 } 7501 7502 void 7503 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, 7504 struct elf_link_hash_entry *h, 7505 bfd_boolean force_local) 7506 { 7507 /* STT_GNU_IFUNC symbol must go through PLT. */ 7508 if (h->type != STT_GNU_IFUNC) 7509 { 7510 h->plt = elf_hash_table (info)->init_plt_offset; 7511 h->needs_plt = 0; 7512 } 7513 if (force_local) 7514 { 7515 h->forced_local = 1; 7516 if (h->dynindx != -1) 7517 { 7518 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 7519 h->dynstr_index); 7520 h->dynindx = -1; 7521 h->dynstr_index = 0; 7522 } 7523 } 7524 } 7525 7526 /* Hide a symbol. */ 7527 7528 void 7529 _bfd_elf_link_hide_symbol (bfd *output_bfd, 7530 struct bfd_link_info *info, 7531 struct bfd_link_hash_entry *h) 7532 { 7533 if (is_elf_hash_table (info->hash)) 7534 { 7535 const struct elf_backend_data *bed 7536 = get_elf_backend_data (output_bfd); 7537 struct elf_link_hash_entry *eh 7538 = (struct elf_link_hash_entry *) h; 7539 bed->elf_backend_hide_symbol (info, eh, TRUE); 7540 eh->def_dynamic = 0; 7541 eh->ref_dynamic = 0; 7542 eh->dynamic_def = 0; 7543 } 7544 } 7545 7546 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our 7547 caller. */ 7548 7549 bfd_boolean 7550 _bfd_elf_link_hash_table_init 7551 (struct elf_link_hash_table *table, 7552 bfd *abfd, 7553 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 7554 struct bfd_hash_table *, 7555 const char *), 7556 unsigned int entsize, 7557 enum elf_target_id target_id) 7558 { 7559 bfd_boolean ret; 7560 int can_refcount = get_elf_backend_data (abfd)->can_refcount; 7561 7562 table->init_got_refcount.refcount = can_refcount - 1; 7563 table->init_plt_refcount.refcount = can_refcount - 1; 7564 table->init_got_offset.offset = -(bfd_vma) 1; 7565 table->init_plt_offset.offset = -(bfd_vma) 1; 7566 /* The first dynamic symbol is a dummy. */ 7567 table->dynsymcount = 1; 7568 7569 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 7570 7571 table->root.type = bfd_link_elf_hash_table; 7572 table->hash_table_id = target_id; 7573 7574 return ret; 7575 } 7576 7577 /* Create an ELF linker hash table. */ 7578 7579 struct bfd_link_hash_table * 7580 _bfd_elf_link_hash_table_create (bfd *abfd) 7581 { 7582 struct elf_link_hash_table *ret; 7583 bfd_size_type amt = sizeof (struct elf_link_hash_table); 7584 7585 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); 7586 if (ret == NULL) 7587 return NULL; 7588 7589 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, 7590 sizeof (struct elf_link_hash_entry), 7591 GENERIC_ELF_DATA)) 7592 { 7593 free (ret); 7594 return NULL; 7595 } 7596 ret->root.hash_table_free = _bfd_elf_link_hash_table_free; 7597 7598 return &ret->root; 7599 } 7600 7601 /* Destroy an ELF linker hash table. */ 7602 7603 void 7604 _bfd_elf_link_hash_table_free (bfd *obfd) 7605 { 7606 struct elf_link_hash_table *htab; 7607 7608 htab = (struct elf_link_hash_table *) obfd->link.hash; 7609 if (htab->dynstr != NULL) 7610 _bfd_elf_strtab_free (htab->dynstr); 7611 _bfd_merge_sections_free (htab->merge_info); 7612 _bfd_generic_link_hash_table_free (obfd); 7613 } 7614 7615 /* This is a hook for the ELF emulation code in the generic linker to 7616 tell the backend linker what file name to use for the DT_NEEDED 7617 entry for a dynamic object. */ 7618 7619 void 7620 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) 7621 { 7622 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7623 && bfd_get_format (abfd) == bfd_object) 7624 elf_dt_name (abfd) = name; 7625 } 7626 7627 int 7628 bfd_elf_get_dyn_lib_class (bfd *abfd) 7629 { 7630 int lib_class; 7631 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7632 && bfd_get_format (abfd) == bfd_object) 7633 lib_class = elf_dyn_lib_class (abfd); 7634 else 7635 lib_class = 0; 7636 return lib_class; 7637 } 7638 7639 void 7640 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) 7641 { 7642 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7643 && bfd_get_format (abfd) == bfd_object) 7644 elf_dyn_lib_class (abfd) = lib_class; 7645 } 7646 7647 /* Get the list of DT_NEEDED entries for a link. This is a hook for 7648 the linker ELF emulation code. */ 7649 7650 struct bfd_link_needed_list * 7651 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 7652 struct bfd_link_info *info) 7653 { 7654 if (! is_elf_hash_table (info->hash)) 7655 return NULL; 7656 return elf_hash_table (info)->needed; 7657 } 7658 7659 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 7660 hook for the linker ELF emulation code. */ 7661 7662 struct bfd_link_needed_list * 7663 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, 7664 struct bfd_link_info *info) 7665 { 7666 if (! is_elf_hash_table (info->hash)) 7667 return NULL; 7668 return elf_hash_table (info)->runpath; 7669 } 7670 7671 /* Get the name actually used for a dynamic object for a link. This 7672 is the SONAME entry if there is one. Otherwise, it is the string 7673 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 7674 7675 const char * 7676 bfd_elf_get_dt_soname (bfd *abfd) 7677 { 7678 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7679 && bfd_get_format (abfd) == bfd_object) 7680 return elf_dt_name (abfd); 7681 return NULL; 7682 } 7683 7684 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for 7685 the ELF linker emulation code. */ 7686 7687 bfd_boolean 7688 bfd_elf_get_bfd_needed_list (bfd *abfd, 7689 struct bfd_link_needed_list **pneeded) 7690 { 7691 asection *s; 7692 bfd_byte *dynbuf = NULL; 7693 unsigned int elfsec; 7694 unsigned long shlink; 7695 bfd_byte *extdyn, *extdynend; 7696 size_t extdynsize; 7697 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 7698 7699 *pneeded = NULL; 7700 7701 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 7702 || bfd_get_format (abfd) != bfd_object) 7703 return TRUE; 7704 7705 s = bfd_get_section_by_name (abfd, ".dynamic"); 7706 if (s == NULL || s->size == 0) 7707 return TRUE; 7708 7709 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 7710 goto error_return; 7711 7712 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 7713 if (elfsec == SHN_BAD) 7714 goto error_return; 7715 7716 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 7717 7718 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 7719 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 7720 7721 extdyn = dynbuf; 7722 extdynend = extdyn + s->size; 7723 for (; extdyn < extdynend; extdyn += extdynsize) 7724 { 7725 Elf_Internal_Dyn dyn; 7726 7727 (*swap_dyn_in) (abfd, extdyn, &dyn); 7728 7729 if (dyn.d_tag == DT_NULL) 7730 break; 7731 7732 if (dyn.d_tag == DT_NEEDED) 7733 { 7734 const char *string; 7735 struct bfd_link_needed_list *l; 7736 unsigned int tagv = dyn.d_un.d_val; 7737 bfd_size_type amt; 7738 7739 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 7740 if (string == NULL) 7741 goto error_return; 7742 7743 amt = sizeof *l; 7744 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 7745 if (l == NULL) 7746 goto error_return; 7747 7748 l->by = abfd; 7749 l->name = string; 7750 l->next = *pneeded; 7751 *pneeded = l; 7752 } 7753 } 7754 7755 free (dynbuf); 7756 7757 return TRUE; 7758 7759 error_return: 7760 if (dynbuf != NULL) 7761 free (dynbuf); 7762 return FALSE; 7763 } 7764 7765 struct elf_symbuf_symbol 7766 { 7767 unsigned long st_name; /* Symbol name, index in string tbl */ 7768 unsigned char st_info; /* Type and binding attributes */ 7769 unsigned char st_other; /* Visibilty, and target specific */ 7770 }; 7771 7772 struct elf_symbuf_head 7773 { 7774 struct elf_symbuf_symbol *ssym; 7775 size_t count; 7776 unsigned int st_shndx; 7777 }; 7778 7779 struct elf_symbol 7780 { 7781 union 7782 { 7783 Elf_Internal_Sym *isym; 7784 struct elf_symbuf_symbol *ssym; 7785 } u; 7786 const char *name; 7787 }; 7788 7789 /* Sort references to symbols by ascending section number. */ 7790 7791 static int 7792 elf_sort_elf_symbol (const void *arg1, const void *arg2) 7793 { 7794 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; 7795 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; 7796 7797 return s1->st_shndx - s2->st_shndx; 7798 } 7799 7800 static int 7801 elf_sym_name_compare (const void *arg1, const void *arg2) 7802 { 7803 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; 7804 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; 7805 return strcmp (s1->name, s2->name); 7806 } 7807 7808 static struct elf_symbuf_head * 7809 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) 7810 { 7811 Elf_Internal_Sym **ind, **indbufend, **indbuf; 7812 struct elf_symbuf_symbol *ssym; 7813 struct elf_symbuf_head *ssymbuf, *ssymhead; 7814 size_t i, shndx_count, total_size; 7815 7816 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); 7817 if (indbuf == NULL) 7818 return NULL; 7819 7820 for (ind = indbuf, i = 0; i < symcount; i++) 7821 if (isymbuf[i].st_shndx != SHN_UNDEF) 7822 *ind++ = &isymbuf[i]; 7823 indbufend = ind; 7824 7825 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), 7826 elf_sort_elf_symbol); 7827 7828 shndx_count = 0; 7829 if (indbufend > indbuf) 7830 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) 7831 if (ind[0]->st_shndx != ind[1]->st_shndx) 7832 shndx_count++; 7833 7834 total_size = ((shndx_count + 1) * sizeof (*ssymbuf) 7835 + (indbufend - indbuf) * sizeof (*ssym)); 7836 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); 7837 if (ssymbuf == NULL) 7838 { 7839 free (indbuf); 7840 return NULL; 7841 } 7842 7843 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); 7844 ssymbuf->ssym = NULL; 7845 ssymbuf->count = shndx_count; 7846 ssymbuf->st_shndx = 0; 7847 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) 7848 { 7849 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) 7850 { 7851 ssymhead++; 7852 ssymhead->ssym = ssym; 7853 ssymhead->count = 0; 7854 ssymhead->st_shndx = (*ind)->st_shndx; 7855 } 7856 ssym->st_name = (*ind)->st_name; 7857 ssym->st_info = (*ind)->st_info; 7858 ssym->st_other = (*ind)->st_other; 7859 ssymhead->count++; 7860 } 7861 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count 7862 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) 7863 == total_size)); 7864 7865 free (indbuf); 7866 return ssymbuf; 7867 } 7868 7869 /* Check if 2 sections define the same set of local and global 7870 symbols. */ 7871 7872 static bfd_boolean 7873 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, 7874 struct bfd_link_info *info) 7875 { 7876 bfd *bfd1, *bfd2; 7877 const struct elf_backend_data *bed1, *bed2; 7878 Elf_Internal_Shdr *hdr1, *hdr2; 7879 size_t symcount1, symcount2; 7880 Elf_Internal_Sym *isymbuf1, *isymbuf2; 7881 struct elf_symbuf_head *ssymbuf1, *ssymbuf2; 7882 Elf_Internal_Sym *isym, *isymend; 7883 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; 7884 size_t count1, count2, i; 7885 unsigned int shndx1, shndx2; 7886 bfd_boolean result; 7887 7888 bfd1 = sec1->owner; 7889 bfd2 = sec2->owner; 7890 7891 /* Both sections have to be in ELF. */ 7892 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour 7893 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) 7894 return FALSE; 7895 7896 if (elf_section_type (sec1) != elf_section_type (sec2)) 7897 return FALSE; 7898 7899 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); 7900 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); 7901 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) 7902 return FALSE; 7903 7904 bed1 = get_elf_backend_data (bfd1); 7905 bed2 = get_elf_backend_data (bfd2); 7906 hdr1 = &elf_tdata (bfd1)->symtab_hdr; 7907 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; 7908 hdr2 = &elf_tdata (bfd2)->symtab_hdr; 7909 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; 7910 7911 if (symcount1 == 0 || symcount2 == 0) 7912 return FALSE; 7913 7914 result = FALSE; 7915 isymbuf1 = NULL; 7916 isymbuf2 = NULL; 7917 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; 7918 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; 7919 7920 if (ssymbuf1 == NULL) 7921 { 7922 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, 7923 NULL, NULL, NULL); 7924 if (isymbuf1 == NULL) 7925 goto done; 7926 7927 if (!info->reduce_memory_overheads) 7928 elf_tdata (bfd1)->symbuf = ssymbuf1 7929 = elf_create_symbuf (symcount1, isymbuf1); 7930 } 7931 7932 if (ssymbuf1 == NULL || ssymbuf2 == NULL) 7933 { 7934 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, 7935 NULL, NULL, NULL); 7936 if (isymbuf2 == NULL) 7937 goto done; 7938 7939 if (ssymbuf1 != NULL && !info->reduce_memory_overheads) 7940 elf_tdata (bfd2)->symbuf = ssymbuf2 7941 = elf_create_symbuf (symcount2, isymbuf2); 7942 } 7943 7944 if (ssymbuf1 != NULL && ssymbuf2 != NULL) 7945 { 7946 /* Optimized faster version. */ 7947 size_t lo, hi, mid; 7948 struct elf_symbol *symp; 7949 struct elf_symbuf_symbol *ssym, *ssymend; 7950 7951 lo = 0; 7952 hi = ssymbuf1->count; 7953 ssymbuf1++; 7954 count1 = 0; 7955 while (lo < hi) 7956 { 7957 mid = (lo + hi) / 2; 7958 if (shndx1 < ssymbuf1[mid].st_shndx) 7959 hi = mid; 7960 else if (shndx1 > ssymbuf1[mid].st_shndx) 7961 lo = mid + 1; 7962 else 7963 { 7964 count1 = ssymbuf1[mid].count; 7965 ssymbuf1 += mid; 7966 break; 7967 } 7968 } 7969 7970 lo = 0; 7971 hi = ssymbuf2->count; 7972 ssymbuf2++; 7973 count2 = 0; 7974 while (lo < hi) 7975 { 7976 mid = (lo + hi) / 2; 7977 if (shndx2 < ssymbuf2[mid].st_shndx) 7978 hi = mid; 7979 else if (shndx2 > ssymbuf2[mid].st_shndx) 7980 lo = mid + 1; 7981 else 7982 { 7983 count2 = ssymbuf2[mid].count; 7984 ssymbuf2 += mid; 7985 break; 7986 } 7987 } 7988 7989 if (count1 == 0 || count2 == 0 || count1 != count2) 7990 goto done; 7991 7992 symtable1 7993 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); 7994 symtable2 7995 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); 7996 if (symtable1 == NULL || symtable2 == NULL) 7997 goto done; 7998 7999 symp = symtable1; 8000 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; 8001 ssym < ssymend; ssym++, symp++) 8002 { 8003 symp->u.ssym = ssym; 8004 symp->name = bfd_elf_string_from_elf_section (bfd1, 8005 hdr1->sh_link, 8006 ssym->st_name); 8007 } 8008 8009 symp = symtable2; 8010 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; 8011 ssym < ssymend; ssym++, symp++) 8012 { 8013 symp->u.ssym = ssym; 8014 symp->name = bfd_elf_string_from_elf_section (bfd2, 8015 hdr2->sh_link, 8016 ssym->st_name); 8017 } 8018 8019 /* Sort symbol by name. */ 8020 qsort (symtable1, count1, sizeof (struct elf_symbol), 8021 elf_sym_name_compare); 8022 qsort (symtable2, count1, sizeof (struct elf_symbol), 8023 elf_sym_name_compare); 8024 8025 for (i = 0; i < count1; i++) 8026 /* Two symbols must have the same binding, type and name. */ 8027 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info 8028 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other 8029 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 8030 goto done; 8031 8032 result = TRUE; 8033 goto done; 8034 } 8035 8036 symtable1 = (struct elf_symbol *) 8037 bfd_malloc (symcount1 * sizeof (struct elf_symbol)); 8038 symtable2 = (struct elf_symbol *) 8039 bfd_malloc (symcount2 * sizeof (struct elf_symbol)); 8040 if (symtable1 == NULL || symtable2 == NULL) 8041 goto done; 8042 8043 /* Count definitions in the section. */ 8044 count1 = 0; 8045 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) 8046 if (isym->st_shndx == shndx1) 8047 symtable1[count1++].u.isym = isym; 8048 8049 count2 = 0; 8050 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) 8051 if (isym->st_shndx == shndx2) 8052 symtable2[count2++].u.isym = isym; 8053 8054 if (count1 == 0 || count2 == 0 || count1 != count2) 8055 goto done; 8056 8057 for (i = 0; i < count1; i++) 8058 symtable1[i].name 8059 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, 8060 symtable1[i].u.isym->st_name); 8061 8062 for (i = 0; i < count2; i++) 8063 symtable2[i].name 8064 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, 8065 symtable2[i].u.isym->st_name); 8066 8067 /* Sort symbol by name. */ 8068 qsort (symtable1, count1, sizeof (struct elf_symbol), 8069 elf_sym_name_compare); 8070 qsort (symtable2, count1, sizeof (struct elf_symbol), 8071 elf_sym_name_compare); 8072 8073 for (i = 0; i < count1; i++) 8074 /* Two symbols must have the same binding, type and name. */ 8075 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info 8076 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other 8077 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 8078 goto done; 8079 8080 result = TRUE; 8081 8082 done: 8083 if (symtable1) 8084 free (symtable1); 8085 if (symtable2) 8086 free (symtable2); 8087 if (isymbuf1) 8088 free (isymbuf1); 8089 if (isymbuf2) 8090 free (isymbuf2); 8091 8092 return result; 8093 } 8094 8095 /* Return TRUE if 2 section types are compatible. */ 8096 8097 bfd_boolean 8098 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, 8099 bfd *bbfd, const asection *bsec) 8100 { 8101 if (asec == NULL 8102 || bsec == NULL 8103 || abfd->xvec->flavour != bfd_target_elf_flavour 8104 || bbfd->xvec->flavour != bfd_target_elf_flavour) 8105 return TRUE; 8106 8107 return elf_section_type (asec) == elf_section_type (bsec); 8108 } 8109 8110 /* Final phase of ELF linker. */ 8111 8112 /* A structure we use to avoid passing large numbers of arguments. */ 8113 8114 struct elf_final_link_info 8115 { 8116 /* General link information. */ 8117 struct bfd_link_info *info; 8118 /* Output BFD. */ 8119 bfd *output_bfd; 8120 /* Symbol string table. */ 8121 struct elf_strtab_hash *symstrtab; 8122 /* .hash section. */ 8123 asection *hash_sec; 8124 /* symbol version section (.gnu.version). */ 8125 asection *symver_sec; 8126 /* Buffer large enough to hold contents of any section. */ 8127 bfd_byte *contents; 8128 /* Buffer large enough to hold external relocs of any section. */ 8129 void *external_relocs; 8130 /* Buffer large enough to hold internal relocs of any section. */ 8131 Elf_Internal_Rela *internal_relocs; 8132 /* Buffer large enough to hold external local symbols of any input 8133 BFD. */ 8134 bfd_byte *external_syms; 8135 /* And a buffer for symbol section indices. */ 8136 Elf_External_Sym_Shndx *locsym_shndx; 8137 /* Buffer large enough to hold internal local symbols of any input 8138 BFD. */ 8139 Elf_Internal_Sym *internal_syms; 8140 /* Array large enough to hold a symbol index for each local symbol 8141 of any input BFD. */ 8142 long *indices; 8143 /* Array large enough to hold a section pointer for each local 8144 symbol of any input BFD. */ 8145 asection **sections; 8146 /* Buffer for SHT_SYMTAB_SHNDX section. */ 8147 Elf_External_Sym_Shndx *symshndxbuf; 8148 /* Number of STT_FILE syms seen. */ 8149 size_t filesym_count; 8150 }; 8151 8152 /* This struct is used to pass information to elf_link_output_extsym. */ 8153 8154 struct elf_outext_info 8155 { 8156 bfd_boolean failed; 8157 bfd_boolean localsyms; 8158 bfd_boolean file_sym_done; 8159 struct elf_final_link_info *flinfo; 8160 }; 8161 8162 8163 /* Support for evaluating a complex relocation. 8164 8165 Complex relocations are generalized, self-describing relocations. The 8166 implementation of them consists of two parts: complex symbols, and the 8167 relocations themselves. 8168 8169 The relocations are use a reserved elf-wide relocation type code (R_RELC 8170 external / BFD_RELOC_RELC internal) and an encoding of relocation field 8171 information (start bit, end bit, word width, etc) into the addend. This 8172 information is extracted from CGEN-generated operand tables within gas. 8173 8174 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC 8175 internal) representing prefix-notation expressions, including but not 8176 limited to those sorts of expressions normally encoded as addends in the 8177 addend field. The symbol mangling format is: 8178 8179 <node> := <literal> 8180 | <unary-operator> ':' <node> 8181 | <binary-operator> ':' <node> ':' <node> 8182 ; 8183 8184 <literal> := 's' <digits=N> ':' <N character symbol name> 8185 | 'S' <digits=N> ':' <N character section name> 8186 | '#' <hexdigits> 8187 ; 8188 8189 <binary-operator> := as in C 8190 <unary-operator> := as in C, plus "0-" for unambiguous negation. */ 8191 8192 static void 8193 set_symbol_value (bfd *bfd_with_globals, 8194 Elf_Internal_Sym *isymbuf, 8195 size_t locsymcount, 8196 size_t symidx, 8197 bfd_vma val) 8198 { 8199 struct elf_link_hash_entry **sym_hashes; 8200 struct elf_link_hash_entry *h; 8201 size_t extsymoff = locsymcount; 8202 8203 if (symidx < locsymcount) 8204 { 8205 Elf_Internal_Sym *sym; 8206 8207 sym = isymbuf + symidx; 8208 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) 8209 { 8210 /* It is a local symbol: move it to the 8211 "absolute" section and give it a value. */ 8212 sym->st_shndx = SHN_ABS; 8213 sym->st_value = val; 8214 return; 8215 } 8216 BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); 8217 extsymoff = 0; 8218 } 8219 8220 /* It is a global symbol: set its link type 8221 to "defined" and give it a value. */ 8222 8223 sym_hashes = elf_sym_hashes (bfd_with_globals); 8224 h = sym_hashes [symidx - extsymoff]; 8225 while (h->root.type == bfd_link_hash_indirect 8226 || h->root.type == bfd_link_hash_warning) 8227 h = (struct elf_link_hash_entry *) h->root.u.i.link; 8228 h->root.type = bfd_link_hash_defined; 8229 h->root.u.def.value = val; 8230 h->root.u.def.section = bfd_abs_section_ptr; 8231 } 8232 8233 static bfd_boolean 8234 resolve_symbol (const char *name, 8235 bfd *input_bfd, 8236 struct elf_final_link_info *flinfo, 8237 bfd_vma *result, 8238 Elf_Internal_Sym *isymbuf, 8239 size_t locsymcount) 8240 { 8241 Elf_Internal_Sym *sym; 8242 struct bfd_link_hash_entry *global_entry; 8243 const char *candidate = NULL; 8244 Elf_Internal_Shdr *symtab_hdr; 8245 size_t i; 8246 8247 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 8248 8249 for (i = 0; i < locsymcount; ++ i) 8250 { 8251 sym = isymbuf + i; 8252 8253 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) 8254 continue; 8255 8256 candidate = bfd_elf_string_from_elf_section (input_bfd, 8257 symtab_hdr->sh_link, 8258 sym->st_name); 8259 #ifdef DEBUG 8260 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", 8261 name, candidate, (unsigned long) sym->st_value); 8262 #endif 8263 if (candidate && strcmp (candidate, name) == 0) 8264 { 8265 asection *sec = flinfo->sections [i]; 8266 8267 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); 8268 *result += sec->output_offset + sec->output_section->vma; 8269 #ifdef DEBUG 8270 printf ("Found symbol with value %8.8lx\n", 8271 (unsigned long) *result); 8272 #endif 8273 return TRUE; 8274 } 8275 } 8276 8277 /* Hmm, haven't found it yet. perhaps it is a global. */ 8278 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, 8279 FALSE, FALSE, TRUE); 8280 if (!global_entry) 8281 return FALSE; 8282 8283 if (global_entry->type == bfd_link_hash_defined 8284 || global_entry->type == bfd_link_hash_defweak) 8285 { 8286 *result = (global_entry->u.def.value 8287 + global_entry->u.def.section->output_section->vma 8288 + global_entry->u.def.section->output_offset); 8289 #ifdef DEBUG 8290 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", 8291 global_entry->root.string, (unsigned long) *result); 8292 #endif 8293 return TRUE; 8294 } 8295 8296 return FALSE; 8297 } 8298 8299 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in 8300 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section 8301 names like "foo.end" which is the end address of section "foo". */ 8302 8303 static bfd_boolean 8304 resolve_section (const char *name, 8305 asection *sections, 8306 bfd_vma *result, 8307 bfd * abfd) 8308 { 8309 asection *curr; 8310 unsigned int len; 8311 8312 for (curr = sections; curr; curr = curr->next) 8313 if (strcmp (curr->name, name) == 0) 8314 { 8315 *result = curr->vma; 8316 return TRUE; 8317 } 8318 8319 /* Hmm. still haven't found it. try pseudo-section names. */ 8320 /* FIXME: This could be coded more efficiently... */ 8321 for (curr = sections; curr; curr = curr->next) 8322 { 8323 len = strlen (curr->name); 8324 if (len > strlen (name)) 8325 continue; 8326 8327 if (strncmp (curr->name, name, len) == 0) 8328 { 8329 if (strncmp (".end", name + len, 4) == 0) 8330 { 8331 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); 8332 return TRUE; 8333 } 8334 8335 /* Insert more pseudo-section names here, if you like. */ 8336 } 8337 } 8338 8339 return FALSE; 8340 } 8341 8342 static void 8343 undefined_reference (const char *reftype, const char *name) 8344 { 8345 /* xgettext:c-format */ 8346 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), 8347 reftype, name); 8348 } 8349 8350 static bfd_boolean 8351 eval_symbol (bfd_vma *result, 8352 const char **symp, 8353 bfd *input_bfd, 8354 struct elf_final_link_info *flinfo, 8355 bfd_vma dot, 8356 Elf_Internal_Sym *isymbuf, 8357 size_t locsymcount, 8358 int signed_p) 8359 { 8360 size_t len; 8361 size_t symlen; 8362 bfd_vma a; 8363 bfd_vma b; 8364 char symbuf[4096]; 8365 const char *sym = *symp; 8366 const char *symend; 8367 bfd_boolean symbol_is_section = FALSE; 8368 8369 len = strlen (sym); 8370 symend = sym + len; 8371 8372 if (len < 1 || len > sizeof (symbuf)) 8373 { 8374 bfd_set_error (bfd_error_invalid_operation); 8375 return FALSE; 8376 } 8377 8378 switch (* sym) 8379 { 8380 case '.': 8381 *result = dot; 8382 *symp = sym + 1; 8383 return TRUE; 8384 8385 case '#': 8386 ++sym; 8387 *result = strtoul (sym, (char **) symp, 16); 8388 return TRUE; 8389 8390 case 'S': 8391 symbol_is_section = TRUE; 8392 /* Fall through. */ 8393 case 's': 8394 ++sym; 8395 symlen = strtol (sym, (char **) symp, 10); 8396 sym = *symp + 1; /* Skip the trailing ':'. */ 8397 8398 if (symend < sym || symlen + 1 > sizeof (symbuf)) 8399 { 8400 bfd_set_error (bfd_error_invalid_operation); 8401 return FALSE; 8402 } 8403 8404 memcpy (symbuf, sym, symlen); 8405 symbuf[symlen] = '\0'; 8406 *symp = sym + symlen; 8407 8408 /* Is it always possible, with complex symbols, that gas "mis-guessed" 8409 the symbol as a section, or vice-versa. so we're pretty liberal in our 8410 interpretation here; section means "try section first", not "must be a 8411 section", and likewise with symbol. */ 8412 8413 if (symbol_is_section) 8414 { 8415 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) 8416 && !resolve_symbol (symbuf, input_bfd, flinfo, result, 8417 isymbuf, locsymcount)) 8418 { 8419 undefined_reference ("section", symbuf); 8420 return FALSE; 8421 } 8422 } 8423 else 8424 { 8425 if (!resolve_symbol (symbuf, input_bfd, flinfo, result, 8426 isymbuf, locsymcount) 8427 && !resolve_section (symbuf, flinfo->output_bfd->sections, 8428 result, input_bfd)) 8429 { 8430 undefined_reference ("symbol", symbuf); 8431 return FALSE; 8432 } 8433 } 8434 8435 return TRUE; 8436 8437 /* All that remains are operators. */ 8438 8439 #define UNARY_OP(op) \ 8440 if (strncmp (sym, #op, strlen (#op)) == 0) \ 8441 { \ 8442 sym += strlen (#op); \ 8443 if (*sym == ':') \ 8444 ++sym; \ 8445 *symp = sym; \ 8446 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 8447 isymbuf, locsymcount, signed_p)) \ 8448 return FALSE; \ 8449 if (signed_p) \ 8450 *result = op ((bfd_signed_vma) a); \ 8451 else \ 8452 *result = op a; \ 8453 return TRUE; \ 8454 } 8455 8456 #define BINARY_OP(op) \ 8457 if (strncmp (sym, #op, strlen (#op)) == 0) \ 8458 { \ 8459 sym += strlen (#op); \ 8460 if (*sym == ':') \ 8461 ++sym; \ 8462 *symp = sym; \ 8463 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 8464 isymbuf, locsymcount, signed_p)) \ 8465 return FALSE; \ 8466 ++*symp; \ 8467 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ 8468 isymbuf, locsymcount, signed_p)) \ 8469 return FALSE; \ 8470 if (signed_p) \ 8471 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ 8472 else \ 8473 *result = a op b; \ 8474 return TRUE; \ 8475 } 8476 8477 default: 8478 UNARY_OP (0-); 8479 BINARY_OP (<<); 8480 BINARY_OP (>>); 8481 BINARY_OP (==); 8482 BINARY_OP (!=); 8483 BINARY_OP (<=); 8484 BINARY_OP (>=); 8485 BINARY_OP (&&); 8486 BINARY_OP (||); 8487 UNARY_OP (~); 8488 UNARY_OP (!); 8489 BINARY_OP (*); 8490 BINARY_OP (/); 8491 BINARY_OP (%); 8492 BINARY_OP (^); 8493 BINARY_OP (|); 8494 BINARY_OP (&); 8495 BINARY_OP (+); 8496 BINARY_OP (-); 8497 BINARY_OP (<); 8498 BINARY_OP (>); 8499 #undef UNARY_OP 8500 #undef BINARY_OP 8501 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); 8502 bfd_set_error (bfd_error_invalid_operation); 8503 return FALSE; 8504 } 8505 } 8506 8507 static void 8508 put_value (bfd_vma size, 8509 unsigned long chunksz, 8510 bfd *input_bfd, 8511 bfd_vma x, 8512 bfd_byte *location) 8513 { 8514 location += (size - chunksz); 8515 8516 for (; size; size -= chunksz, location -= chunksz) 8517 { 8518 switch (chunksz) 8519 { 8520 case 1: 8521 bfd_put_8 (input_bfd, x, location); 8522 x >>= 8; 8523 break; 8524 case 2: 8525 bfd_put_16 (input_bfd, x, location); 8526 x >>= 16; 8527 break; 8528 case 4: 8529 bfd_put_32 (input_bfd, x, location); 8530 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ 8531 x >>= 16; 8532 x >>= 16; 8533 break; 8534 #ifdef BFD64 8535 case 8: 8536 bfd_put_64 (input_bfd, x, location); 8537 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ 8538 x >>= 32; 8539 x >>= 32; 8540 break; 8541 #endif 8542 default: 8543 abort (); 8544 break; 8545 } 8546 } 8547 } 8548 8549 static bfd_vma 8550 get_value (bfd_vma size, 8551 unsigned long chunksz, 8552 bfd *input_bfd, 8553 bfd_byte *location) 8554 { 8555 int shift; 8556 bfd_vma x = 0; 8557 8558 /* Sanity checks. */ 8559 BFD_ASSERT (chunksz <= sizeof (x) 8560 && size >= chunksz 8561 && chunksz != 0 8562 && (size % chunksz) == 0 8563 && input_bfd != NULL 8564 && location != NULL); 8565 8566 if (chunksz == sizeof (x)) 8567 { 8568 BFD_ASSERT (size == chunksz); 8569 8570 /* Make sure that we do not perform an undefined shift operation. 8571 We know that size == chunksz so there will only be one iteration 8572 of the loop below. */ 8573 shift = 0; 8574 } 8575 else 8576 shift = 8 * chunksz; 8577 8578 for (; size; size -= chunksz, location += chunksz) 8579 { 8580 switch (chunksz) 8581 { 8582 case 1: 8583 x = (x << shift) | bfd_get_8 (input_bfd, location); 8584 break; 8585 case 2: 8586 x = (x << shift) | bfd_get_16 (input_bfd, location); 8587 break; 8588 case 4: 8589 x = (x << shift) | bfd_get_32 (input_bfd, location); 8590 break; 8591 #ifdef BFD64 8592 case 8: 8593 x = (x << shift) | bfd_get_64 (input_bfd, location); 8594 break; 8595 #endif 8596 default: 8597 abort (); 8598 } 8599 } 8600 return x; 8601 } 8602 8603 static void 8604 decode_complex_addend (unsigned long *start, /* in bits */ 8605 unsigned long *oplen, /* in bits */ 8606 unsigned long *len, /* in bits */ 8607 unsigned long *wordsz, /* in bytes */ 8608 unsigned long *chunksz, /* in bytes */ 8609 unsigned long *lsb0_p, 8610 unsigned long *signed_p, 8611 unsigned long *trunc_p, 8612 unsigned long encoded) 8613 { 8614 * start = encoded & 0x3F; 8615 * len = (encoded >> 6) & 0x3F; 8616 * oplen = (encoded >> 12) & 0x3F; 8617 * wordsz = (encoded >> 18) & 0xF; 8618 * chunksz = (encoded >> 22) & 0xF; 8619 * lsb0_p = (encoded >> 27) & 1; 8620 * signed_p = (encoded >> 28) & 1; 8621 * trunc_p = (encoded >> 29) & 1; 8622 } 8623 8624 bfd_reloc_status_type 8625 bfd_elf_perform_complex_relocation (bfd *input_bfd, 8626 asection *input_section ATTRIBUTE_UNUSED, 8627 bfd_byte *contents, 8628 Elf_Internal_Rela *rel, 8629 bfd_vma relocation) 8630 { 8631 bfd_vma shift, x, mask; 8632 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; 8633 bfd_reloc_status_type r; 8634 8635 /* Perform this reloc, since it is complex. 8636 (this is not to say that it necessarily refers to a complex 8637 symbol; merely that it is a self-describing CGEN based reloc. 8638 i.e. the addend has the complete reloc information (bit start, end, 8639 word size, etc) encoded within it.). */ 8640 8641 decode_complex_addend (&start, &oplen, &len, &wordsz, 8642 &chunksz, &lsb0_p, &signed_p, 8643 &trunc_p, rel->r_addend); 8644 8645 mask = (((1L << (len - 1)) - 1) << 1) | 1; 8646 8647 if (lsb0_p) 8648 shift = (start + 1) - len; 8649 else 8650 shift = (8 * wordsz) - (start + len); 8651 8652 x = get_value (wordsz, chunksz, input_bfd, 8653 contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); 8654 8655 #ifdef DEBUG 8656 printf ("Doing complex reloc: " 8657 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " 8658 "chunksz %ld, start %ld, len %ld, oplen %ld\n" 8659 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", 8660 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, 8661 oplen, (unsigned long) x, (unsigned long) mask, 8662 (unsigned long) relocation); 8663 #endif 8664 8665 r = bfd_reloc_ok; 8666 if (! trunc_p) 8667 /* Now do an overflow check. */ 8668 r = bfd_check_overflow ((signed_p 8669 ? complain_overflow_signed 8670 : complain_overflow_unsigned), 8671 len, 0, (8 * wordsz), 8672 relocation); 8673 8674 /* Do the deed. */ 8675 x = (x & ~(mask << shift)) | ((relocation & mask) << shift); 8676 8677 #ifdef DEBUG 8678 printf (" relocation: %8.8lx\n" 8679 " shifted mask: %8.8lx\n" 8680 " shifted/masked reloc: %8.8lx\n" 8681 " result: %8.8lx\n", 8682 (unsigned long) relocation, (unsigned long) (mask << shift), 8683 (unsigned long) ((relocation & mask) << shift), (unsigned long) x); 8684 #endif 8685 put_value (wordsz, chunksz, input_bfd, x, 8686 contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); 8687 return r; 8688 } 8689 8690 /* Functions to read r_offset from external (target order) reloc 8691 entry. Faster than bfd_getl32 et al, because we let the compiler 8692 know the value is aligned. */ 8693 8694 static bfd_vma 8695 ext32l_r_offset (const void *p) 8696 { 8697 union aligned32 8698 { 8699 uint32_t v; 8700 unsigned char c[4]; 8701 }; 8702 const union aligned32 *a 8703 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 8704 8705 uint32_t aval = ( (uint32_t) a->c[0] 8706 | (uint32_t) a->c[1] << 8 8707 | (uint32_t) a->c[2] << 16 8708 | (uint32_t) a->c[3] << 24); 8709 return aval; 8710 } 8711 8712 static bfd_vma 8713 ext32b_r_offset (const void *p) 8714 { 8715 union aligned32 8716 { 8717 uint32_t v; 8718 unsigned char c[4]; 8719 }; 8720 const union aligned32 *a 8721 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 8722 8723 uint32_t aval = ( (uint32_t) a->c[0] << 24 8724 | (uint32_t) a->c[1] << 16 8725 | (uint32_t) a->c[2] << 8 8726 | (uint32_t) a->c[3]); 8727 return aval; 8728 } 8729 8730 #ifdef BFD_HOST_64_BIT 8731 static bfd_vma 8732 ext64l_r_offset (const void *p) 8733 { 8734 union aligned64 8735 { 8736 uint64_t v; 8737 unsigned char c[8]; 8738 }; 8739 const union aligned64 *a 8740 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 8741 8742 uint64_t aval = ( (uint64_t) a->c[0] 8743 | (uint64_t) a->c[1] << 8 8744 | (uint64_t) a->c[2] << 16 8745 | (uint64_t) a->c[3] << 24 8746 | (uint64_t) a->c[4] << 32 8747 | (uint64_t) a->c[5] << 40 8748 | (uint64_t) a->c[6] << 48 8749 | (uint64_t) a->c[7] << 56); 8750 return aval; 8751 } 8752 8753 static bfd_vma 8754 ext64b_r_offset (const void *p) 8755 { 8756 union aligned64 8757 { 8758 uint64_t v; 8759 unsigned char c[8]; 8760 }; 8761 const union aligned64 *a 8762 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 8763 8764 uint64_t aval = ( (uint64_t) a->c[0] << 56 8765 | (uint64_t) a->c[1] << 48 8766 | (uint64_t) a->c[2] << 40 8767 | (uint64_t) a->c[3] << 32 8768 | (uint64_t) a->c[4] << 24 8769 | (uint64_t) a->c[5] << 16 8770 | (uint64_t) a->c[6] << 8 8771 | (uint64_t) a->c[7]); 8772 return aval; 8773 } 8774 #endif 8775 8776 /* When performing a relocatable link, the input relocations are 8777 preserved. But, if they reference global symbols, the indices 8778 referenced must be updated. Update all the relocations found in 8779 RELDATA. */ 8780 8781 static bfd_boolean 8782 elf_link_adjust_relocs (bfd *abfd, 8783 asection *sec, 8784 struct bfd_elf_section_reloc_data *reldata, 8785 bfd_boolean sort, 8786 struct bfd_link_info *info) 8787 { 8788 unsigned int i; 8789 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8790 bfd_byte *erela; 8791 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 8792 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 8793 bfd_vma r_type_mask; 8794 int r_sym_shift; 8795 unsigned int count = reldata->count; 8796 struct elf_link_hash_entry **rel_hash = reldata->hashes; 8797 8798 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) 8799 { 8800 swap_in = bed->s->swap_reloc_in; 8801 swap_out = bed->s->swap_reloc_out; 8802 } 8803 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) 8804 { 8805 swap_in = bed->s->swap_reloca_in; 8806 swap_out = bed->s->swap_reloca_out; 8807 } 8808 else 8809 abort (); 8810 8811 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) 8812 abort (); 8813 8814 if (bed->s->arch_size == 32) 8815 { 8816 r_type_mask = 0xff; 8817 r_sym_shift = 8; 8818 } 8819 else 8820 { 8821 r_type_mask = 0xffffffff; 8822 r_sym_shift = 32; 8823 } 8824 8825 erela = reldata->hdr->contents; 8826 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) 8827 { 8828 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; 8829 unsigned int j; 8830 8831 if (*rel_hash == NULL) 8832 continue; 8833 8834 if ((*rel_hash)->indx == -2 8835 && info->gc_sections 8836 && ! info->gc_keep_exported) 8837 { 8838 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ 8839 _bfd_error_handler (_("%pB:%pA: error: relocation references symbol %s which was removed by garbage collection"), 8840 abfd, sec, 8841 (*rel_hash)->root.root.string); 8842 _bfd_error_handler (_("%pB:%pA: error: try relinking with --gc-keep-exported enabled"), 8843 abfd, sec); 8844 bfd_set_error (bfd_error_invalid_operation); 8845 return FALSE; 8846 } 8847 BFD_ASSERT ((*rel_hash)->indx >= 0); 8848 8849 (*swap_in) (abfd, erela, irela); 8850 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) 8851 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift 8852 | (irela[j].r_info & r_type_mask)); 8853 (*swap_out) (abfd, irela, erela); 8854 } 8855 8856 if (bed->elf_backend_update_relocs) 8857 (*bed->elf_backend_update_relocs) (sec, reldata); 8858 8859 if (sort && count != 0) 8860 { 8861 bfd_vma (*ext_r_off) (const void *); 8862 bfd_vma r_off; 8863 size_t elt_size; 8864 bfd_byte *base, *end, *p, *loc; 8865 bfd_byte *buf = NULL; 8866 8867 if (bed->s->arch_size == 32) 8868 { 8869 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 8870 ext_r_off = ext32l_r_offset; 8871 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 8872 ext_r_off = ext32b_r_offset; 8873 else 8874 abort (); 8875 } 8876 else 8877 { 8878 #ifdef BFD_HOST_64_BIT 8879 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 8880 ext_r_off = ext64l_r_offset; 8881 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 8882 ext_r_off = ext64b_r_offset; 8883 else 8884 #endif 8885 abort (); 8886 } 8887 8888 /* Must use a stable sort here. A modified insertion sort, 8889 since the relocs are mostly sorted already. */ 8890 elt_size = reldata->hdr->sh_entsize; 8891 base = reldata->hdr->contents; 8892 end = base + count * elt_size; 8893 if (elt_size > sizeof (Elf64_External_Rela)) 8894 abort (); 8895 8896 /* Ensure the first element is lowest. This acts as a sentinel, 8897 speeding the main loop below. */ 8898 r_off = (*ext_r_off) (base); 8899 for (p = loc = base; (p += elt_size) < end; ) 8900 { 8901 bfd_vma r_off2 = (*ext_r_off) (p); 8902 if (r_off > r_off2) 8903 { 8904 r_off = r_off2; 8905 loc = p; 8906 } 8907 } 8908 if (loc != base) 8909 { 8910 /* Don't just swap *base and *loc as that changes the order 8911 of the original base[0] and base[1] if they happen to 8912 have the same r_offset. */ 8913 bfd_byte onebuf[sizeof (Elf64_External_Rela)]; 8914 memcpy (onebuf, loc, elt_size); 8915 memmove (base + elt_size, base, loc - base); 8916 memcpy (base, onebuf, elt_size); 8917 } 8918 8919 for (p = base + elt_size; (p += elt_size) < end; ) 8920 { 8921 /* base to p is sorted, *p is next to insert. */ 8922 r_off = (*ext_r_off) (p); 8923 /* Search the sorted region for location to insert. */ 8924 loc = p - elt_size; 8925 while (r_off < (*ext_r_off) (loc)) 8926 loc -= elt_size; 8927 loc += elt_size; 8928 if (loc != p) 8929 { 8930 /* Chances are there is a run of relocs to insert here, 8931 from one of more input files. Files are not always 8932 linked in order due to the way elf_link_input_bfd is 8933 called. See pr17666. */ 8934 size_t sortlen = p - loc; 8935 bfd_vma r_off2 = (*ext_r_off) (loc); 8936 size_t runlen = elt_size; 8937 size_t buf_size = 96 * 1024; 8938 while (p + runlen < end 8939 && (sortlen <= buf_size 8940 || runlen + elt_size <= buf_size) 8941 && r_off2 > (*ext_r_off) (p + runlen)) 8942 runlen += elt_size; 8943 if (buf == NULL) 8944 { 8945 buf = bfd_malloc (buf_size); 8946 if (buf == NULL) 8947 return FALSE; 8948 } 8949 if (runlen < sortlen) 8950 { 8951 memcpy (buf, p, runlen); 8952 memmove (loc + runlen, loc, sortlen); 8953 memcpy (loc, buf, runlen); 8954 } 8955 else 8956 { 8957 memcpy (buf, loc, sortlen); 8958 memmove (loc, p, runlen); 8959 memcpy (loc + runlen, buf, sortlen); 8960 } 8961 p += runlen - elt_size; 8962 } 8963 } 8964 /* Hashes are no longer valid. */ 8965 free (reldata->hashes); 8966 reldata->hashes = NULL; 8967 free (buf); 8968 } 8969 return TRUE; 8970 } 8971 8972 struct elf_link_sort_rela 8973 { 8974 union { 8975 bfd_vma offset; 8976 bfd_vma sym_mask; 8977 } u; 8978 enum elf_reloc_type_class type; 8979 /* We use this as an array of size int_rels_per_ext_rel. */ 8980 Elf_Internal_Rela rela[1]; 8981 }; 8982 8983 static int 8984 elf_link_sort_cmp1 (const void *A, const void *B) 8985 { 8986 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8987 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8988 int relativea, relativeb; 8989 8990 relativea = a->type == reloc_class_relative; 8991 relativeb = b->type == reloc_class_relative; 8992 8993 if (relativea < relativeb) 8994 return 1; 8995 if (relativea > relativeb) 8996 return -1; 8997 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) 8998 return -1; 8999 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) 9000 return 1; 9001 if (a->rela->r_offset < b->rela->r_offset) 9002 return -1; 9003 if (a->rela->r_offset > b->rela->r_offset) 9004 return 1; 9005 return 0; 9006 } 9007 9008 static int 9009 elf_link_sort_cmp2 (const void *A, const void *B) 9010 { 9011 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 9012 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 9013 9014 if (a->type < b->type) 9015 return -1; 9016 if (a->type > b->type) 9017 return 1; 9018 if (a->u.offset < b->u.offset) 9019 return -1; 9020 if (a->u.offset > b->u.offset) 9021 return 1; 9022 if (a->rela->r_offset < b->rela->r_offset) 9023 return -1; 9024 if (a->rela->r_offset > b->rela->r_offset) 9025 return 1; 9026 return 0; 9027 } 9028 9029 static size_t 9030 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) 9031 { 9032 asection *dynamic_relocs; 9033 asection *rela_dyn; 9034 asection *rel_dyn; 9035 bfd_size_type count, size; 9036 size_t i, ret, sort_elt, ext_size; 9037 bfd_byte *sort, *s_non_relative, *p; 9038 struct elf_link_sort_rela *sq; 9039 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 9040 int i2e = bed->s->int_rels_per_ext_rel; 9041 unsigned int opb = bfd_octets_per_byte (abfd); 9042 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 9043 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 9044 struct bfd_link_order *lo; 9045 bfd_vma r_sym_mask; 9046 bfd_boolean use_rela; 9047 9048 /* Find a dynamic reloc section. */ 9049 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); 9050 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); 9051 if (rela_dyn != NULL && rela_dyn->size > 0 9052 && rel_dyn != NULL && rel_dyn->size > 0) 9053 { 9054 bfd_boolean use_rela_initialised = FALSE; 9055 9056 /* This is just here to stop gcc from complaining. 9057 Its initialization checking code is not perfect. */ 9058 use_rela = TRUE; 9059 9060 /* Both sections are present. Examine the sizes 9061 of the indirect sections to help us choose. */ 9062 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) 9063 if (lo->type == bfd_indirect_link_order) 9064 { 9065 asection *o = lo->u.indirect.section; 9066 9067 if ((o->size % bed->s->sizeof_rela) == 0) 9068 { 9069 if ((o->size % bed->s->sizeof_rel) == 0) 9070 /* Section size is divisible by both rel and rela sizes. 9071 It is of no help to us. */ 9072 ; 9073 else 9074 { 9075 /* Section size is only divisible by rela. */ 9076 if (use_rela_initialised && !use_rela) 9077 { 9078 _bfd_error_handler (_("%pB: unable to sort relocs - " 9079 "they are in more than one size"), 9080 abfd); 9081 bfd_set_error (bfd_error_invalid_operation); 9082 return 0; 9083 } 9084 else 9085 { 9086 use_rela = TRUE; 9087 use_rela_initialised = TRUE; 9088 } 9089 } 9090 } 9091 else if ((o->size % bed->s->sizeof_rel) == 0) 9092 { 9093 /* Section size is only divisible by rel. */ 9094 if (use_rela_initialised && use_rela) 9095 { 9096 _bfd_error_handler (_("%pB: unable to sort relocs - " 9097 "they are in more than one size"), 9098 abfd); 9099 bfd_set_error (bfd_error_invalid_operation); 9100 return 0; 9101 } 9102 else 9103 { 9104 use_rela = FALSE; 9105 use_rela_initialised = TRUE; 9106 } 9107 } 9108 else 9109 { 9110 /* The section size is not divisible by either - 9111 something is wrong. */ 9112 _bfd_error_handler (_("%pB: unable to sort relocs - " 9113 "they are of an unknown size"), abfd); 9114 bfd_set_error (bfd_error_invalid_operation); 9115 return 0; 9116 } 9117 } 9118 9119 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) 9120 if (lo->type == bfd_indirect_link_order) 9121 { 9122 asection *o = lo->u.indirect.section; 9123 9124 if ((o->size % bed->s->sizeof_rela) == 0) 9125 { 9126 if ((o->size % bed->s->sizeof_rel) == 0) 9127 /* Section size is divisible by both rel and rela sizes. 9128 It is of no help to us. */ 9129 ; 9130 else 9131 { 9132 /* Section size is only divisible by rela. */ 9133 if (use_rela_initialised && !use_rela) 9134 { 9135 _bfd_error_handler (_("%pB: unable to sort relocs - " 9136 "they are in more than one size"), 9137 abfd); 9138 bfd_set_error (bfd_error_invalid_operation); 9139 return 0; 9140 } 9141 else 9142 { 9143 use_rela = TRUE; 9144 use_rela_initialised = TRUE; 9145 } 9146 } 9147 } 9148 else if ((o->size % bed->s->sizeof_rel) == 0) 9149 { 9150 /* Section size is only divisible by rel. */ 9151 if (use_rela_initialised && use_rela) 9152 { 9153 _bfd_error_handler (_("%pB: unable to sort relocs - " 9154 "they are in more than one size"), 9155 abfd); 9156 bfd_set_error (bfd_error_invalid_operation); 9157 return 0; 9158 } 9159 else 9160 { 9161 use_rela = FALSE; 9162 use_rela_initialised = TRUE; 9163 } 9164 } 9165 else 9166 { 9167 /* The section size is not divisible by either - 9168 something is wrong. */ 9169 _bfd_error_handler (_("%pB: unable to sort relocs - " 9170 "they are of an unknown size"), abfd); 9171 bfd_set_error (bfd_error_invalid_operation); 9172 return 0; 9173 } 9174 } 9175 9176 if (! use_rela_initialised) 9177 /* Make a guess. */ 9178 use_rela = TRUE; 9179 } 9180 else if (rela_dyn != NULL && rela_dyn->size > 0) 9181 use_rela = TRUE; 9182 else if (rel_dyn != NULL && rel_dyn->size > 0) 9183 use_rela = FALSE; 9184 else 9185 return 0; 9186 9187 if (use_rela) 9188 { 9189 dynamic_relocs = rela_dyn; 9190 ext_size = bed->s->sizeof_rela; 9191 swap_in = bed->s->swap_reloca_in; 9192 swap_out = bed->s->swap_reloca_out; 9193 } 9194 else 9195 { 9196 dynamic_relocs = rel_dyn; 9197 ext_size = bed->s->sizeof_rel; 9198 swap_in = bed->s->swap_reloc_in; 9199 swap_out = bed->s->swap_reloc_out; 9200 } 9201 9202 size = 0; 9203 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 9204 if (lo->type == bfd_indirect_link_order) 9205 size += lo->u.indirect.section->size; 9206 9207 if (size != dynamic_relocs->size) 9208 return 0; 9209 9210 sort_elt = (sizeof (struct elf_link_sort_rela) 9211 + (i2e - 1) * sizeof (Elf_Internal_Rela)); 9212 9213 count = dynamic_relocs->size / ext_size; 9214 if (count == 0) 9215 return 0; 9216 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); 9217 9218 if (sort == NULL) 9219 { 9220 (*info->callbacks->warning) 9221 (info, _("not enough memory to sort relocations"), 0, abfd, 0, 0); 9222 return 0; 9223 } 9224 9225 if (bed->s->arch_size == 32) 9226 r_sym_mask = ~(bfd_vma) 0xff; 9227 else 9228 r_sym_mask = ~(bfd_vma) 0xffffffff; 9229 9230 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 9231 if (lo->type == bfd_indirect_link_order) 9232 { 9233 bfd_byte *erel, *erelend; 9234 asection *o = lo->u.indirect.section; 9235 9236 if (o->contents == NULL && o->size != 0) 9237 { 9238 /* This is a reloc section that is being handled as a normal 9239 section. See bfd_section_from_shdr. We can't combine 9240 relocs in this case. */ 9241 free (sort); 9242 return 0; 9243 } 9244 erel = o->contents; 9245 erelend = o->contents + o->size; 9246 p = sort + o->output_offset * opb / ext_size * sort_elt; 9247 9248 while (erel < erelend) 9249 { 9250 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 9251 9252 (*swap_in) (abfd, erel, s->rela); 9253 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); 9254 s->u.sym_mask = r_sym_mask; 9255 p += sort_elt; 9256 erel += ext_size; 9257 } 9258 } 9259 9260 qsort (sort, count, sort_elt, elf_link_sort_cmp1); 9261 9262 for (i = 0, p = sort; i < count; i++, p += sort_elt) 9263 { 9264 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 9265 if (s->type != reloc_class_relative) 9266 break; 9267 } 9268 ret = i; 9269 s_non_relative = p; 9270 9271 sq = (struct elf_link_sort_rela *) s_non_relative; 9272 for (; i < count; i++, p += sort_elt) 9273 { 9274 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; 9275 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) 9276 sq = sp; 9277 sp->u.offset = sq->rela->r_offset; 9278 } 9279 9280 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); 9281 9282 struct elf_link_hash_table *htab = elf_hash_table (info); 9283 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) 9284 { 9285 /* We have plt relocs in .rela.dyn. */ 9286 sq = (struct elf_link_sort_rela *) sort; 9287 for (i = 0; i < count; i++) 9288 if (sq[count - i - 1].type != reloc_class_plt) 9289 break; 9290 if (i != 0 && htab->srelplt->size == i * ext_size) 9291 { 9292 struct bfd_link_order **plo; 9293 /* Put srelplt link_order last. This is so the output_offset 9294 set in the next loop is correct for DT_JMPREL. */ 9295 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) 9296 if ((*plo)->type == bfd_indirect_link_order 9297 && (*plo)->u.indirect.section == htab->srelplt) 9298 { 9299 lo = *plo; 9300 *plo = lo->next; 9301 } 9302 else 9303 plo = &(*plo)->next; 9304 *plo = lo; 9305 lo->next = NULL; 9306 dynamic_relocs->map_tail.link_order = lo; 9307 } 9308 } 9309 9310 p = sort; 9311 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 9312 if (lo->type == bfd_indirect_link_order) 9313 { 9314 bfd_byte *erel, *erelend; 9315 asection *o = lo->u.indirect.section; 9316 9317 erel = o->contents; 9318 erelend = o->contents + o->size; 9319 o->output_offset = (p - sort) / sort_elt * ext_size / opb; 9320 while (erel < erelend) 9321 { 9322 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 9323 (*swap_out) (abfd, s->rela, erel); 9324 p += sort_elt; 9325 erel += ext_size; 9326 } 9327 } 9328 9329 free (sort); 9330 *psec = dynamic_relocs; 9331 return ret; 9332 } 9333 9334 /* Add a symbol to the output symbol string table. */ 9335 9336 static int 9337 elf_link_output_symstrtab (struct elf_final_link_info *flinfo, 9338 const char *name, 9339 Elf_Internal_Sym *elfsym, 9340 asection *input_sec, 9341 struct elf_link_hash_entry *h) 9342 { 9343 int (*output_symbol_hook) 9344 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, 9345 struct elf_link_hash_entry *); 9346 struct elf_link_hash_table *hash_table; 9347 const struct elf_backend_data *bed; 9348 bfd_size_type strtabsize; 9349 9350 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 9351 9352 bed = get_elf_backend_data (flinfo->output_bfd); 9353 output_symbol_hook = bed->elf_backend_link_output_symbol_hook; 9354 if (output_symbol_hook != NULL) 9355 { 9356 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); 9357 if (ret != 1) 9358 return ret; 9359 } 9360 9361 if (name == NULL 9362 || *name == '\0' 9363 || (input_sec->flags & SEC_EXCLUDE)) 9364 elfsym->st_name = (unsigned long) -1; 9365 else 9366 { 9367 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 9368 to get the final offset for st_name. */ 9369 elfsym->st_name 9370 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, 9371 name, FALSE); 9372 if (elfsym->st_name == (unsigned long) -1) 9373 return 0; 9374 } 9375 9376 hash_table = elf_hash_table (flinfo->info); 9377 strtabsize = hash_table->strtabsize; 9378 if (strtabsize <= hash_table->strtabcount) 9379 { 9380 strtabsize += strtabsize; 9381 hash_table->strtabsize = strtabsize; 9382 strtabsize *= sizeof (*hash_table->strtab); 9383 hash_table->strtab 9384 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, 9385 strtabsize); 9386 if (hash_table->strtab == NULL) 9387 return 0; 9388 } 9389 hash_table->strtab[hash_table->strtabcount].sym = *elfsym; 9390 hash_table->strtab[hash_table->strtabcount].dest_index 9391 = hash_table->strtabcount; 9392 hash_table->strtab[hash_table->strtabcount].destshndx_index 9393 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; 9394 9395 bfd_get_symcount (flinfo->output_bfd) += 1; 9396 hash_table->strtabcount += 1; 9397 9398 return 1; 9399 } 9400 9401 /* Swap symbols out to the symbol table and flush the output symbols to 9402 the file. */ 9403 9404 static bfd_boolean 9405 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) 9406 { 9407 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); 9408 bfd_size_type amt; 9409 size_t i; 9410 const struct elf_backend_data *bed; 9411 bfd_byte *symbuf; 9412 Elf_Internal_Shdr *hdr; 9413 file_ptr pos; 9414 bfd_boolean ret; 9415 9416 if (!hash_table->strtabcount) 9417 return TRUE; 9418 9419 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 9420 9421 bed = get_elf_backend_data (flinfo->output_bfd); 9422 9423 amt = bed->s->sizeof_sym * hash_table->strtabcount; 9424 symbuf = (bfd_byte *) bfd_malloc (amt); 9425 if (symbuf == NULL) 9426 return FALSE; 9427 9428 if (flinfo->symshndxbuf) 9429 { 9430 amt = sizeof (Elf_External_Sym_Shndx); 9431 amt *= bfd_get_symcount (flinfo->output_bfd); 9432 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); 9433 if (flinfo->symshndxbuf == NULL) 9434 { 9435 free (symbuf); 9436 return FALSE; 9437 } 9438 } 9439 9440 for (i = 0; i < hash_table->strtabcount; i++) 9441 { 9442 struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; 9443 if (elfsym->sym.st_name == (unsigned long) -1) 9444 elfsym->sym.st_name = 0; 9445 else 9446 elfsym->sym.st_name 9447 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, 9448 elfsym->sym.st_name); 9449 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, 9450 ((bfd_byte *) symbuf 9451 + (elfsym->dest_index 9452 * bed->s->sizeof_sym)), 9453 (flinfo->symshndxbuf 9454 + elfsym->destshndx_index)); 9455 } 9456 9457 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; 9458 pos = hdr->sh_offset + hdr->sh_size; 9459 amt = hash_table->strtabcount * bed->s->sizeof_sym; 9460 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 9461 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) 9462 { 9463 hdr->sh_size += amt; 9464 ret = TRUE; 9465 } 9466 else 9467 ret = FALSE; 9468 9469 free (symbuf); 9470 9471 free (hash_table->strtab); 9472 hash_table->strtab = NULL; 9473 9474 return ret; 9475 } 9476 9477 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ 9478 9479 static bfd_boolean 9480 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) 9481 { 9482 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) 9483 && sym->st_shndx < SHN_LORESERVE) 9484 { 9485 /* The gABI doesn't support dynamic symbols in output sections 9486 beyond 64k. */ 9487 _bfd_error_handler 9488 /* xgettext:c-format */ 9489 (_("%pB: too many sections: %d (>= %d)"), 9490 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); 9491 bfd_set_error (bfd_error_nonrepresentable_section); 9492 return FALSE; 9493 } 9494 return TRUE; 9495 } 9496 9497 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in 9498 allowing an unsatisfied unversioned symbol in the DSO to match a 9499 versioned symbol that would normally require an explicit version. 9500 We also handle the case that a DSO references a hidden symbol 9501 which may be satisfied by a versioned symbol in another DSO. */ 9502 9503 static bfd_boolean 9504 elf_link_check_versioned_symbol (struct bfd_link_info *info, 9505 const struct elf_backend_data *bed, 9506 struct elf_link_hash_entry *h) 9507 { 9508 bfd *abfd; 9509 struct elf_link_loaded_list *loaded; 9510 9511 if (!is_elf_hash_table (info->hash)) 9512 return FALSE; 9513 9514 /* Check indirect symbol. */ 9515 while (h->root.type == bfd_link_hash_indirect) 9516 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9517 9518 switch (h->root.type) 9519 { 9520 default: 9521 abfd = NULL; 9522 break; 9523 9524 case bfd_link_hash_undefined: 9525 case bfd_link_hash_undefweak: 9526 abfd = h->root.u.undef.abfd; 9527 if (abfd == NULL 9528 || (abfd->flags & DYNAMIC) == 0 9529 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) 9530 return FALSE; 9531 break; 9532 9533 case bfd_link_hash_defined: 9534 case bfd_link_hash_defweak: 9535 abfd = h->root.u.def.section->owner; 9536 break; 9537 9538 case bfd_link_hash_common: 9539 abfd = h->root.u.c.p->section->owner; 9540 break; 9541 } 9542 BFD_ASSERT (abfd != NULL); 9543 9544 for (loaded = elf_hash_table (info)->loaded; 9545 loaded != NULL; 9546 loaded = loaded->next) 9547 { 9548 bfd *input; 9549 Elf_Internal_Shdr *hdr; 9550 size_t symcount; 9551 size_t extsymcount; 9552 size_t extsymoff; 9553 Elf_Internal_Shdr *versymhdr; 9554 Elf_Internal_Sym *isym; 9555 Elf_Internal_Sym *isymend; 9556 Elf_Internal_Sym *isymbuf; 9557 Elf_External_Versym *ever; 9558 Elf_External_Versym *extversym; 9559 9560 input = loaded->abfd; 9561 9562 /* We check each DSO for a possible hidden versioned definition. */ 9563 if (input == abfd 9564 || (input->flags & DYNAMIC) == 0 9565 || elf_dynversym (input) == 0) 9566 continue; 9567 9568 hdr = &elf_tdata (input)->dynsymtab_hdr; 9569 9570 symcount = hdr->sh_size / bed->s->sizeof_sym; 9571 if (elf_bad_symtab (input)) 9572 { 9573 extsymcount = symcount; 9574 extsymoff = 0; 9575 } 9576 else 9577 { 9578 extsymcount = symcount - hdr->sh_info; 9579 extsymoff = hdr->sh_info; 9580 } 9581 9582 if (extsymcount == 0) 9583 continue; 9584 9585 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, 9586 NULL, NULL, NULL); 9587 if (isymbuf == NULL) 9588 return FALSE; 9589 9590 /* Read in any version definitions. */ 9591 versymhdr = &elf_tdata (input)->dynversym_hdr; 9592 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 9593 if (extversym == NULL) 9594 goto error_ret; 9595 9596 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 9597 || (bfd_bread (extversym, versymhdr->sh_size, input) 9598 != versymhdr->sh_size)) 9599 { 9600 free (extversym); 9601 error_ret: 9602 free (isymbuf); 9603 return FALSE; 9604 } 9605 9606 ever = extversym + extsymoff; 9607 isymend = isymbuf + extsymcount; 9608 for (isym = isymbuf; isym < isymend; isym++, ever++) 9609 { 9610 const char *name; 9611 Elf_Internal_Versym iver; 9612 unsigned short version_index; 9613 9614 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL 9615 || isym->st_shndx == SHN_UNDEF) 9616 continue; 9617 9618 name = bfd_elf_string_from_elf_section (input, 9619 hdr->sh_link, 9620 isym->st_name); 9621 if (strcmp (name, h->root.root.string) != 0) 9622 continue; 9623 9624 _bfd_elf_swap_versym_in (input, ever, &iver); 9625 9626 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 9627 && !(h->def_regular 9628 && h->forced_local)) 9629 { 9630 /* If we have a non-hidden versioned sym, then it should 9631 have provided a definition for the undefined sym unless 9632 it is defined in a non-shared object and forced local. 9633 */ 9634 abort (); 9635 } 9636 9637 version_index = iver.vs_vers & VERSYM_VERSION; 9638 if (version_index == 1 || version_index == 2) 9639 { 9640 /* This is the base or first version. We can use it. */ 9641 free (extversym); 9642 free (isymbuf); 9643 return TRUE; 9644 } 9645 } 9646 9647 free (extversym); 9648 free (isymbuf); 9649 } 9650 9651 return FALSE; 9652 } 9653 9654 /* Convert ELF common symbol TYPE. */ 9655 9656 static int 9657 elf_link_convert_common_type (struct bfd_link_info *info, int type) 9658 { 9659 /* Commom symbol can only appear in relocatable link. */ 9660 if (!bfd_link_relocatable (info)) 9661 abort (); 9662 switch (info->elf_stt_common) 9663 { 9664 case unchanged: 9665 break; 9666 case elf_stt_common: 9667 type = STT_COMMON; 9668 break; 9669 case no_elf_stt_common: 9670 type = STT_OBJECT; 9671 break; 9672 } 9673 return type; 9674 } 9675 9676 /* Add an external symbol to the symbol table. This is called from 9677 the hash table traversal routine. When generating a shared object, 9678 we go through the symbol table twice. The first time we output 9679 anything that might have been forced to local scope in a version 9680 script. The second time we output the symbols that are still 9681 global symbols. */ 9682 9683 static bfd_boolean 9684 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) 9685 { 9686 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; 9687 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; 9688 struct elf_final_link_info *flinfo = eoinfo->flinfo; 9689 bfd_boolean strip; 9690 Elf_Internal_Sym sym; 9691 asection *input_sec; 9692 const struct elf_backend_data *bed; 9693 long indx; 9694 int ret; 9695 unsigned int type; 9696 9697 if (h->root.type == bfd_link_hash_warning) 9698 { 9699 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9700 if (h->root.type == bfd_link_hash_new) 9701 return TRUE; 9702 } 9703 9704 /* Decide whether to output this symbol in this pass. */ 9705 if (eoinfo->localsyms) 9706 { 9707 if (!h->forced_local) 9708 return TRUE; 9709 } 9710 else 9711 { 9712 if (h->forced_local) 9713 return TRUE; 9714 } 9715 9716 bed = get_elf_backend_data (flinfo->output_bfd); 9717 9718 if (h->root.type == bfd_link_hash_undefined) 9719 { 9720 /* If we have an undefined symbol reference here then it must have 9721 come from a shared library that is being linked in. (Undefined 9722 references in regular files have already been handled unless 9723 they are in unreferenced sections which are removed by garbage 9724 collection). */ 9725 bfd_boolean ignore_undef = FALSE; 9726 9727 /* Some symbols may be special in that the fact that they're 9728 undefined can be safely ignored - let backend determine that. */ 9729 if (bed->elf_backend_ignore_undef_symbol) 9730 ignore_undef = bed->elf_backend_ignore_undef_symbol (h); 9731 9732 /* If we are reporting errors for this situation then do so now. */ 9733 if (!ignore_undef 9734 && h->ref_dynamic 9735 && (!h->ref_regular || flinfo->info->gc_sections) 9736 && !elf_link_check_versioned_symbol (flinfo->info, bed, h) 9737 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) 9738 (*flinfo->info->callbacks->undefined_symbol) 9739 (flinfo->info, h->root.root.string, 9740 h->ref_regular ? NULL : h->root.u.undef.abfd, 9741 NULL, 0, 9742 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); 9743 9744 /* Strip a global symbol defined in a discarded section. */ 9745 if (h->indx == -3) 9746 return TRUE; 9747 } 9748 9749 /* We should also warn if a forced local symbol is referenced from 9750 shared libraries. */ 9751 if (bfd_link_executable (flinfo->info) 9752 && h->forced_local 9753 && h->ref_dynamic 9754 && h->def_regular 9755 && !h->dynamic_def 9756 && h->ref_dynamic_nonweak 9757 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) 9758 { 9759 bfd *def_bfd; 9760 const char *msg; 9761 struct elf_link_hash_entry *hi = h; 9762 9763 /* Check indirect symbol. */ 9764 while (hi->root.type == bfd_link_hash_indirect) 9765 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 9766 9767 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 9768 /* xgettext:c-format */ 9769 msg = _("%pB: internal symbol `%s' in %pB is referenced by DSO"); 9770 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) 9771 /* xgettext:c-format */ 9772 msg = _("%pB: hidden symbol `%s' in %pB is referenced by DSO"); 9773 else 9774 /* xgettext:c-format */ 9775 msg = _("%pB: local symbol `%s' in %pB is referenced by DSO"); 9776 def_bfd = flinfo->output_bfd; 9777 if (hi->root.u.def.section != bfd_abs_section_ptr) 9778 def_bfd = hi->root.u.def.section->owner; 9779 _bfd_error_handler (msg, flinfo->output_bfd, 9780 h->root.root.string, def_bfd); 9781 bfd_set_error (bfd_error_bad_value); 9782 eoinfo->failed = TRUE; 9783 return FALSE; 9784 } 9785 9786 /* We don't want to output symbols that have never been mentioned by 9787 a regular file, or that we have been told to strip. However, if 9788 h->indx is set to -2, the symbol is used by a reloc and we must 9789 output it. */ 9790 strip = FALSE; 9791 if (h->indx == -2) 9792 ; 9793 else if ((h->def_dynamic 9794 || h->ref_dynamic 9795 || h->root.type == bfd_link_hash_new) 9796 && !h->def_regular 9797 && !h->ref_regular) 9798 strip = TRUE; 9799 else if (flinfo->info->strip == strip_all) 9800 strip = TRUE; 9801 else if (flinfo->info->strip == strip_some 9802 && bfd_hash_lookup (flinfo->info->keep_hash, 9803 h->root.root.string, FALSE, FALSE) == NULL) 9804 strip = TRUE; 9805 else if ((h->root.type == bfd_link_hash_defined 9806 || h->root.type == bfd_link_hash_defweak) 9807 && ((flinfo->info->strip_discarded 9808 && discarded_section (h->root.u.def.section)) 9809 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 9810 && h->root.u.def.section->owner != NULL 9811 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) 9812 strip = TRUE; 9813 else if ((h->root.type == bfd_link_hash_undefined 9814 || h->root.type == bfd_link_hash_undefweak) 9815 && h->root.u.undef.abfd != NULL 9816 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) 9817 strip = TRUE; 9818 9819 type = h->type; 9820 9821 /* If we're stripping it, and it's not a dynamic symbol, there's 9822 nothing else to do. However, if it is a forced local symbol or 9823 an ifunc symbol we need to give the backend finish_dynamic_symbol 9824 function a chance to make it dynamic. */ 9825 if (strip 9826 && h->dynindx == -1 9827 && type != STT_GNU_IFUNC 9828 && !h->forced_local) 9829 return TRUE; 9830 9831 sym.st_value = 0; 9832 sym.st_size = h->size; 9833 sym.st_other = h->other; 9834 switch (h->root.type) 9835 { 9836 default: 9837 case bfd_link_hash_new: 9838 case bfd_link_hash_warning: 9839 abort (); 9840 return FALSE; 9841 9842 case bfd_link_hash_undefined: 9843 case bfd_link_hash_undefweak: 9844 input_sec = bfd_und_section_ptr; 9845 sym.st_shndx = SHN_UNDEF; 9846 break; 9847 9848 case bfd_link_hash_defined: 9849 case bfd_link_hash_defweak: 9850 { 9851 input_sec = h->root.u.def.section; 9852 if (input_sec->output_section != NULL) 9853 { 9854 sym.st_shndx = 9855 _bfd_elf_section_from_bfd_section (flinfo->output_bfd, 9856 input_sec->output_section); 9857 if (sym.st_shndx == SHN_BAD) 9858 { 9859 _bfd_error_handler 9860 /* xgettext:c-format */ 9861 (_("%pB: could not find output section %pA for input section %pA"), 9862 flinfo->output_bfd, input_sec->output_section, input_sec); 9863 bfd_set_error (bfd_error_nonrepresentable_section); 9864 eoinfo->failed = TRUE; 9865 return FALSE; 9866 } 9867 9868 /* ELF symbols in relocatable files are section relative, 9869 but in nonrelocatable files they are virtual 9870 addresses. */ 9871 sym.st_value = h->root.u.def.value + input_sec->output_offset; 9872 if (!bfd_link_relocatable (flinfo->info)) 9873 { 9874 sym.st_value += input_sec->output_section->vma; 9875 if (h->type == STT_TLS) 9876 { 9877 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; 9878 if (tls_sec != NULL) 9879 sym.st_value -= tls_sec->vma; 9880 } 9881 } 9882 } 9883 else 9884 { 9885 BFD_ASSERT (input_sec->owner == NULL 9886 || (input_sec->owner->flags & DYNAMIC) != 0); 9887 sym.st_shndx = SHN_UNDEF; 9888 input_sec = bfd_und_section_ptr; 9889 } 9890 } 9891 break; 9892 9893 case bfd_link_hash_common: 9894 input_sec = h->root.u.c.p->section; 9895 sym.st_shndx = bed->common_section_index (input_sec); 9896 sym.st_value = 1 << h->root.u.c.p->alignment_power; 9897 break; 9898 9899 case bfd_link_hash_indirect: 9900 /* These symbols are created by symbol versioning. They point 9901 to the decorated version of the name. For example, if the 9902 symbol foo@@GNU_1.2 is the default, which should be used when 9903 foo is used with no version, then we add an indirect symbol 9904 foo which points to foo@@GNU_1.2. We ignore these symbols, 9905 since the indirected symbol is already in the hash table. */ 9906 return TRUE; 9907 } 9908 9909 if (type == STT_COMMON || type == STT_OBJECT) 9910 switch (h->root.type) 9911 { 9912 case bfd_link_hash_common: 9913 type = elf_link_convert_common_type (flinfo->info, type); 9914 break; 9915 case bfd_link_hash_defined: 9916 case bfd_link_hash_defweak: 9917 if (bed->common_definition (&sym)) 9918 type = elf_link_convert_common_type (flinfo->info, type); 9919 else 9920 type = STT_OBJECT; 9921 break; 9922 case bfd_link_hash_undefined: 9923 case bfd_link_hash_undefweak: 9924 break; 9925 default: 9926 abort (); 9927 } 9928 9929 if (h->forced_local) 9930 { 9931 sym.st_info = ELF_ST_INFO (STB_LOCAL, type); 9932 /* Turn off visibility on local symbol. */ 9933 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 9934 } 9935 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ 9936 else if (h->unique_global && h->def_regular) 9937 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); 9938 else if (h->root.type == bfd_link_hash_undefweak 9939 || h->root.type == bfd_link_hash_defweak) 9940 sym.st_info = ELF_ST_INFO (STB_WEAK, type); 9941 else 9942 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 9943 sym.st_target_internal = h->target_internal; 9944 9945 /* Give the processor backend a chance to tweak the symbol value, 9946 and also to finish up anything that needs to be done for this 9947 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for 9948 forced local syms when non-shared is due to a historical quirk. 9949 STT_GNU_IFUNC symbol must go through PLT. */ 9950 if ((h->type == STT_GNU_IFUNC 9951 && h->def_regular 9952 && !bfd_link_relocatable (flinfo->info)) 9953 || ((h->dynindx != -1 9954 || h->forced_local) 9955 && ((bfd_link_pic (flinfo->info) 9956 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 9957 || h->root.type != bfd_link_hash_undefweak)) 9958 || !h->forced_local) 9959 && elf_hash_table (flinfo->info)->dynamic_sections_created)) 9960 { 9961 if (! ((*bed->elf_backend_finish_dynamic_symbol) 9962 (flinfo->output_bfd, flinfo->info, h, &sym))) 9963 { 9964 eoinfo->failed = TRUE; 9965 return FALSE; 9966 } 9967 } 9968 9969 /* If we are marking the symbol as undefined, and there are no 9970 non-weak references to this symbol from a regular object, then 9971 mark the symbol as weak undefined; if there are non-weak 9972 references, mark the symbol as strong. We can't do this earlier, 9973 because it might not be marked as undefined until the 9974 finish_dynamic_symbol routine gets through with it. */ 9975 if (sym.st_shndx == SHN_UNDEF 9976 && h->ref_regular 9977 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL 9978 || ELF_ST_BIND (sym.st_info) == STB_WEAK)) 9979 { 9980 int bindtype; 9981 type = ELF_ST_TYPE (sym.st_info); 9982 9983 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ 9984 if (type == STT_GNU_IFUNC) 9985 type = STT_FUNC; 9986 9987 if (h->ref_regular_nonweak) 9988 bindtype = STB_GLOBAL; 9989 else 9990 bindtype = STB_WEAK; 9991 sym.st_info = ELF_ST_INFO (bindtype, type); 9992 } 9993 9994 /* If this is a symbol defined in a dynamic library, don't use the 9995 symbol size from the dynamic library. Relinking an executable 9996 against a new library may introduce gratuitous changes in the 9997 executable's symbols if we keep the size. */ 9998 if (sym.st_shndx == SHN_UNDEF 9999 && !h->def_regular 10000 && h->def_dynamic) 10001 sym.st_size = 0; 10002 10003 /* If a non-weak symbol with non-default visibility is not defined 10004 locally, it is a fatal error. */ 10005 if (!bfd_link_relocatable (flinfo->info) 10006 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT 10007 && ELF_ST_BIND (sym.st_info) != STB_WEAK 10008 && h->root.type == bfd_link_hash_undefined 10009 && !h->def_regular) 10010 { 10011 const char *msg; 10012 10013 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) 10014 /* xgettext:c-format */ 10015 msg = _("%pB: protected symbol `%s' isn't defined"); 10016 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) 10017 /* xgettext:c-format */ 10018 msg = _("%pB: internal symbol `%s' isn't defined"); 10019 else 10020 /* xgettext:c-format */ 10021 msg = _("%pB: hidden symbol `%s' isn't defined"); 10022 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); 10023 bfd_set_error (bfd_error_bad_value); 10024 eoinfo->failed = TRUE; 10025 return FALSE; 10026 } 10027 10028 /* If this symbol should be put in the .dynsym section, then put it 10029 there now. We already know the symbol index. We also fill in 10030 the entry in the .hash section. */ 10031 if (elf_hash_table (flinfo->info)->dynsym != NULL 10032 && h->dynindx != -1 10033 && elf_hash_table (flinfo->info)->dynamic_sections_created) 10034 { 10035 bfd_byte *esym; 10036 10037 /* Since there is no version information in the dynamic string, 10038 if there is no version info in symbol version section, we will 10039 have a run-time problem if not linking executable, referenced 10040 by shared library, or not bound locally. */ 10041 if (h->verinfo.verdef == NULL 10042 && (!bfd_link_executable (flinfo->info) 10043 || h->ref_dynamic 10044 || !h->def_regular)) 10045 { 10046 char *p = strrchr (h->root.root.string, ELF_VER_CHR); 10047 10048 if (p && p [1] != '\0') 10049 { 10050 _bfd_error_handler 10051 /* xgettext:c-format */ 10052 (_("%pB: no symbol version section for versioned symbol `%s'"), 10053 flinfo->output_bfd, h->root.root.string); 10054 eoinfo->failed = TRUE; 10055 return FALSE; 10056 } 10057 } 10058 10059 sym.st_name = h->dynstr_index; 10060 esym = (elf_hash_table (flinfo->info)->dynsym->contents 10061 + h->dynindx * bed->s->sizeof_sym); 10062 if (!check_dynsym (flinfo->output_bfd, &sym)) 10063 { 10064 eoinfo->failed = TRUE; 10065 return FALSE; 10066 } 10067 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); 10068 10069 if (flinfo->hash_sec != NULL) 10070 { 10071 size_t hash_entry_size; 10072 bfd_byte *bucketpos; 10073 bfd_vma chain; 10074 size_t bucketcount; 10075 size_t bucket; 10076 10077 bucketcount = elf_hash_table (flinfo->info)->bucketcount; 10078 bucket = h->u.elf_hash_value % bucketcount; 10079 10080 hash_entry_size 10081 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; 10082 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents 10083 + (bucket + 2) * hash_entry_size); 10084 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); 10085 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, 10086 bucketpos); 10087 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, 10088 ((bfd_byte *) flinfo->hash_sec->contents 10089 + (bucketcount + 2 + h->dynindx) * hash_entry_size)); 10090 } 10091 10092 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) 10093 { 10094 Elf_Internal_Versym iversym; 10095 Elf_External_Versym *eversym; 10096 10097 if (!h->def_regular) 10098 { 10099 if (h->verinfo.verdef == NULL 10100 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 10101 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 10102 iversym.vs_vers = 0; 10103 else 10104 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; 10105 } 10106 else 10107 { 10108 if (h->verinfo.vertree == NULL) 10109 iversym.vs_vers = 1; 10110 else 10111 iversym.vs_vers = h->verinfo.vertree->vernum + 1; 10112 if (flinfo->info->create_default_symver) 10113 iversym.vs_vers++; 10114 } 10115 10116 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is 10117 defined locally. */ 10118 if (h->versioned == versioned_hidden && h->def_regular) 10119 iversym.vs_vers |= VERSYM_HIDDEN; 10120 10121 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; 10122 eversym += h->dynindx; 10123 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); 10124 } 10125 } 10126 10127 /* If the symbol is undefined, and we didn't output it to .dynsym, 10128 strip it from .symtab too. Obviously we can't do this for 10129 relocatable output or when needed for --emit-relocs. */ 10130 else if (input_sec == bfd_und_section_ptr 10131 && h->indx != -2 10132 /* PR 22319 Do not strip global undefined symbols marked as being needed. */ 10133 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) 10134 && !bfd_link_relocatable (flinfo->info)) 10135 return TRUE; 10136 10137 /* Also strip others that we couldn't earlier due to dynamic symbol 10138 processing. */ 10139 if (strip) 10140 return TRUE; 10141 if ((input_sec->flags & SEC_EXCLUDE) != 0) 10142 return TRUE; 10143 10144 /* Output a FILE symbol so that following locals are not associated 10145 with the wrong input file. We need one for forced local symbols 10146 if we've seen more than one FILE symbol or when we have exactly 10147 one FILE symbol but global symbols are present in a file other 10148 than the one with the FILE symbol. We also need one if linker 10149 defined symbols are present. In practice these conditions are 10150 always met, so just emit the FILE symbol unconditionally. */ 10151 if (eoinfo->localsyms 10152 && !eoinfo->file_sym_done 10153 && eoinfo->flinfo->filesym_count != 0) 10154 { 10155 Elf_Internal_Sym fsym; 10156 10157 memset (&fsym, 0, sizeof (fsym)); 10158 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 10159 fsym.st_shndx = SHN_ABS; 10160 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, 10161 bfd_und_section_ptr, NULL)) 10162 return FALSE; 10163 10164 eoinfo->file_sym_done = TRUE; 10165 } 10166 10167 indx = bfd_get_symcount (flinfo->output_bfd); 10168 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, 10169 input_sec, h); 10170 if (ret == 0) 10171 { 10172 eoinfo->failed = TRUE; 10173 return FALSE; 10174 } 10175 else if (ret == 1) 10176 h->indx = indx; 10177 else if (h->indx == -2) 10178 abort(); 10179 10180 return TRUE; 10181 } 10182 10183 /* Return TRUE if special handling is done for relocs in SEC against 10184 symbols defined in discarded sections. */ 10185 10186 static bfd_boolean 10187 elf_section_ignore_discarded_relocs (asection *sec) 10188 { 10189 const struct elf_backend_data *bed; 10190 10191 switch (sec->sec_info_type) 10192 { 10193 case SEC_INFO_TYPE_STABS: 10194 case SEC_INFO_TYPE_EH_FRAME: 10195 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 10196 return TRUE; 10197 default: 10198 break; 10199 } 10200 10201 bed = get_elf_backend_data (sec->owner); 10202 if (bed->elf_backend_ignore_discarded_relocs != NULL 10203 && (*bed->elf_backend_ignore_discarded_relocs) (sec)) 10204 return TRUE; 10205 10206 return FALSE; 10207 } 10208 10209 /* Return a mask saying how ld should treat relocations in SEC against 10210 symbols defined in discarded sections. If this function returns 10211 COMPLAIN set, ld will issue a warning message. If this function 10212 returns PRETEND set, and the discarded section was link-once and the 10213 same size as the kept link-once section, ld will pretend that the 10214 symbol was actually defined in the kept section. Otherwise ld will 10215 zero the reloc (at least that is the intent, but some cooperation by 10216 the target dependent code is needed, particularly for REL targets). */ 10217 10218 unsigned int 10219 _bfd_elf_default_action_discarded (asection *sec) 10220 { 10221 if (sec->flags & SEC_DEBUGGING) 10222 return PRETEND; 10223 10224 if (strcmp (".eh_frame", sec->name) == 0) 10225 return 0; 10226 10227 if (strcmp (".gcc_except_table", sec->name) == 0) 10228 return 0; 10229 10230 return COMPLAIN | PRETEND; 10231 } 10232 10233 /* Find a match between a section and a member of a section group. */ 10234 10235 static asection * 10236 match_group_member (asection *sec, asection *group, 10237 struct bfd_link_info *info) 10238 { 10239 asection *first = elf_next_in_group (group); 10240 asection *s = first; 10241 10242 while (s != NULL) 10243 { 10244 if (bfd_elf_match_symbols_in_sections (s, sec, info)) 10245 return s; 10246 10247 s = elf_next_in_group (s); 10248 if (s == first) 10249 break; 10250 } 10251 10252 return NULL; 10253 } 10254 10255 /* Check if the kept section of a discarded section SEC can be used 10256 to replace it. Return the replacement if it is OK. Otherwise return 10257 NULL. */ 10258 10259 asection * 10260 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) 10261 { 10262 asection *kept; 10263 10264 kept = sec->kept_section; 10265 if (kept != NULL) 10266 { 10267 if ((kept->flags & SEC_GROUP) != 0) 10268 kept = match_group_member (sec, kept, info); 10269 if (kept != NULL 10270 && ((sec->rawsize != 0 ? sec->rawsize : sec->size) 10271 != (kept->rawsize != 0 ? kept->rawsize : kept->size))) 10272 kept = NULL; 10273 sec->kept_section = kept; 10274 } 10275 return kept; 10276 } 10277 10278 /* Link an input file into the linker output file. This function 10279 handles all the sections and relocations of the input file at once. 10280 This is so that we only have to read the local symbols once, and 10281 don't have to keep them in memory. */ 10282 10283 static bfd_boolean 10284 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) 10285 { 10286 int (*relocate_section) 10287 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 10288 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); 10289 bfd *output_bfd; 10290 Elf_Internal_Shdr *symtab_hdr; 10291 size_t locsymcount; 10292 size_t extsymoff; 10293 Elf_Internal_Sym *isymbuf; 10294 Elf_Internal_Sym *isym; 10295 Elf_Internal_Sym *isymend; 10296 long *pindex; 10297 asection **ppsection; 10298 asection *o; 10299 const struct elf_backend_data *bed; 10300 struct elf_link_hash_entry **sym_hashes; 10301 bfd_size_type address_size; 10302 bfd_vma r_type_mask; 10303 int r_sym_shift; 10304 bfd_boolean have_file_sym = FALSE; 10305 10306 output_bfd = flinfo->output_bfd; 10307 bed = get_elf_backend_data (output_bfd); 10308 relocate_section = bed->elf_backend_relocate_section; 10309 10310 /* If this is a dynamic object, we don't want to do anything here: 10311 we don't want the local symbols, and we don't want the section 10312 contents. */ 10313 if ((input_bfd->flags & DYNAMIC) != 0) 10314 return TRUE; 10315 10316 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 10317 if (elf_bad_symtab (input_bfd)) 10318 { 10319 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 10320 extsymoff = 0; 10321 } 10322 else 10323 { 10324 locsymcount = symtab_hdr->sh_info; 10325 extsymoff = symtab_hdr->sh_info; 10326 } 10327 10328 /* Read the local symbols. */ 10329 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 10330 if (isymbuf == NULL && locsymcount != 0) 10331 { 10332 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 10333 flinfo->internal_syms, 10334 flinfo->external_syms, 10335 flinfo->locsym_shndx); 10336 if (isymbuf == NULL) 10337 return FALSE; 10338 } 10339 10340 /* Find local symbol sections and adjust values of symbols in 10341 SEC_MERGE sections. Write out those local symbols we know are 10342 going into the output file. */ 10343 isymend = isymbuf + locsymcount; 10344 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; 10345 isym < isymend; 10346 isym++, pindex++, ppsection++) 10347 { 10348 asection *isec; 10349 const char *name; 10350 Elf_Internal_Sym osym; 10351 long indx; 10352 int ret; 10353 10354 *pindex = -1; 10355 10356 if (elf_bad_symtab (input_bfd)) 10357 { 10358 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) 10359 { 10360 *ppsection = NULL; 10361 continue; 10362 } 10363 } 10364 10365 if (isym->st_shndx == SHN_UNDEF) 10366 isec = bfd_und_section_ptr; 10367 else if (isym->st_shndx == SHN_ABS) 10368 isec = bfd_abs_section_ptr; 10369 else if (isym->st_shndx == SHN_COMMON) 10370 isec = bfd_com_section_ptr; 10371 else 10372 { 10373 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 10374 if (isec == NULL) 10375 { 10376 /* Don't attempt to output symbols with st_shnx in the 10377 reserved range other than SHN_ABS and SHN_COMMON. */ 10378 *ppsection = NULL; 10379 continue; 10380 } 10381 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE 10382 && ELF_ST_TYPE (isym->st_info) != STT_SECTION) 10383 isym->st_value = 10384 _bfd_merged_section_offset (output_bfd, &isec, 10385 elf_section_data (isec)->sec_info, 10386 isym->st_value); 10387 } 10388 10389 *ppsection = isec; 10390 10391 /* Don't output the first, undefined, symbol. In fact, don't 10392 output any undefined local symbol. */ 10393 if (isec == bfd_und_section_ptr) 10394 continue; 10395 10396 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 10397 { 10398 /* We never output section symbols. Instead, we use the 10399 section symbol of the corresponding section in the output 10400 file. */ 10401 continue; 10402 } 10403 10404 /* If we are stripping all symbols, we don't want to output this 10405 one. */ 10406 if (flinfo->info->strip == strip_all) 10407 continue; 10408 10409 /* If we are discarding all local symbols, we don't want to 10410 output this one. If we are generating a relocatable output 10411 file, then some of the local symbols may be required by 10412 relocs; we output them below as we discover that they are 10413 needed. */ 10414 if (flinfo->info->discard == discard_all) 10415 continue; 10416 10417 /* If this symbol is defined in a section which we are 10418 discarding, we don't need to keep it. */ 10419 if (isym->st_shndx != SHN_UNDEF 10420 && isym->st_shndx < SHN_LORESERVE 10421 && bfd_section_removed_from_list (output_bfd, 10422 isec->output_section)) 10423 continue; 10424 10425 /* Get the name of the symbol. */ 10426 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, 10427 isym->st_name); 10428 if (name == NULL) 10429 return FALSE; 10430 10431 /* See if we are discarding symbols with this name. */ 10432 if ((flinfo->info->strip == strip_some 10433 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) 10434 == NULL)) 10435 || (((flinfo->info->discard == discard_sec_merge 10436 && (isec->flags & SEC_MERGE) 10437 && !bfd_link_relocatable (flinfo->info)) 10438 || flinfo->info->discard == discard_l) 10439 && bfd_is_local_label_name (input_bfd, name))) 10440 continue; 10441 10442 if (ELF_ST_TYPE (isym->st_info) == STT_FILE) 10443 { 10444 if (input_bfd->lto_output) 10445 /* -flto puts a temp file name here. This means builds 10446 are not reproducible. Discard the symbol. */ 10447 continue; 10448 have_file_sym = TRUE; 10449 flinfo->filesym_count += 1; 10450 } 10451 if (!have_file_sym) 10452 { 10453 /* In the absence of debug info, bfd_find_nearest_line uses 10454 FILE symbols to determine the source file for local 10455 function symbols. Provide a FILE symbol here if input 10456 files lack such, so that their symbols won't be 10457 associated with a previous input file. It's not the 10458 source file, but the best we can do. */ 10459 have_file_sym = TRUE; 10460 flinfo->filesym_count += 1; 10461 memset (&osym, 0, sizeof (osym)); 10462 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 10463 osym.st_shndx = SHN_ABS; 10464 if (!elf_link_output_symstrtab (flinfo, 10465 (input_bfd->lto_output ? NULL 10466 : input_bfd->filename), 10467 &osym, bfd_abs_section_ptr, 10468 NULL)) 10469 return FALSE; 10470 } 10471 10472 osym = *isym; 10473 10474 /* Adjust the section index for the output file. */ 10475 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 10476 isec->output_section); 10477 if (osym.st_shndx == SHN_BAD) 10478 return FALSE; 10479 10480 /* ELF symbols in relocatable files are section relative, but 10481 in executable files they are virtual addresses. Note that 10482 this code assumes that all ELF sections have an associated 10483 BFD section with a reasonable value for output_offset; below 10484 we assume that they also have a reasonable value for 10485 output_section. Any special sections must be set up to meet 10486 these requirements. */ 10487 osym.st_value += isec->output_offset; 10488 if (!bfd_link_relocatable (flinfo->info)) 10489 { 10490 osym.st_value += isec->output_section->vma; 10491 if (ELF_ST_TYPE (osym.st_info) == STT_TLS) 10492 { 10493 /* STT_TLS symbols are relative to PT_TLS segment base. */ 10494 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); 10495 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; 10496 } 10497 } 10498 10499 indx = bfd_get_symcount (output_bfd); 10500 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); 10501 if (ret == 0) 10502 return FALSE; 10503 else if (ret == 1) 10504 *pindex = indx; 10505 } 10506 10507 if (bed->s->arch_size == 32) 10508 { 10509 r_type_mask = 0xff; 10510 r_sym_shift = 8; 10511 address_size = 4; 10512 } 10513 else 10514 { 10515 r_type_mask = 0xffffffff; 10516 r_sym_shift = 32; 10517 address_size = 8; 10518 } 10519 10520 /* Relocate the contents of each section. */ 10521 sym_hashes = elf_sym_hashes (input_bfd); 10522 for (o = input_bfd->sections; o != NULL; o = o->next) 10523 { 10524 bfd_byte *contents; 10525 10526 if (! o->linker_mark) 10527 { 10528 /* This section was omitted from the link. */ 10529 continue; 10530 } 10531 10532 if (!flinfo->info->resolve_section_groups 10533 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) 10534 { 10535 /* Deal with the group signature symbol. */ 10536 struct bfd_elf_section_data *sec_data = elf_section_data (o); 10537 unsigned long symndx = sec_data->this_hdr.sh_info; 10538 asection *osec = o->output_section; 10539 10540 BFD_ASSERT (bfd_link_relocatable (flinfo->info)); 10541 if (symndx >= locsymcount 10542 || (elf_bad_symtab (input_bfd) 10543 && flinfo->sections[symndx] == NULL)) 10544 { 10545 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; 10546 while (h->root.type == bfd_link_hash_indirect 10547 || h->root.type == bfd_link_hash_warning) 10548 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10549 /* Arrange for symbol to be output. */ 10550 h->indx = -2; 10551 elf_section_data (osec)->this_hdr.sh_info = -2; 10552 } 10553 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) 10554 { 10555 /* We'll use the output section target_index. */ 10556 asection *sec = flinfo->sections[symndx]->output_section; 10557 elf_section_data (osec)->this_hdr.sh_info = sec->target_index; 10558 } 10559 else 10560 { 10561 if (flinfo->indices[symndx] == -1) 10562 { 10563 /* Otherwise output the local symbol now. */ 10564 Elf_Internal_Sym sym = isymbuf[symndx]; 10565 asection *sec = flinfo->sections[symndx]->output_section; 10566 const char *name; 10567 long indx; 10568 int ret; 10569 10570 name = bfd_elf_string_from_elf_section (input_bfd, 10571 symtab_hdr->sh_link, 10572 sym.st_name); 10573 if (name == NULL) 10574 return FALSE; 10575 10576 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 10577 sec); 10578 if (sym.st_shndx == SHN_BAD) 10579 return FALSE; 10580 10581 sym.st_value += o->output_offset; 10582 10583 indx = bfd_get_symcount (output_bfd); 10584 ret = elf_link_output_symstrtab (flinfo, name, &sym, o, 10585 NULL); 10586 if (ret == 0) 10587 return FALSE; 10588 else if (ret == 1) 10589 flinfo->indices[symndx] = indx; 10590 else 10591 abort (); 10592 } 10593 elf_section_data (osec)->this_hdr.sh_info 10594 = flinfo->indices[symndx]; 10595 } 10596 } 10597 10598 if ((o->flags & SEC_HAS_CONTENTS) == 0 10599 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 10600 continue; 10601 10602 if ((o->flags & SEC_LINKER_CREATED) != 0) 10603 { 10604 /* Section was created by _bfd_elf_link_create_dynamic_sections 10605 or somesuch. */ 10606 continue; 10607 } 10608 10609 /* Get the contents of the section. They have been cached by a 10610 relaxation routine. Note that o is a section in an input 10611 file, so the contents field will not have been set by any of 10612 the routines which work on output files. */ 10613 if (elf_section_data (o)->this_hdr.contents != NULL) 10614 { 10615 contents = elf_section_data (o)->this_hdr.contents; 10616 if (bed->caches_rawsize 10617 && o->rawsize != 0 10618 && o->rawsize < o->size) 10619 { 10620 memcpy (flinfo->contents, contents, o->rawsize); 10621 contents = flinfo->contents; 10622 } 10623 } 10624 else 10625 { 10626 contents = flinfo->contents; 10627 if (! bfd_get_full_section_contents (input_bfd, o, &contents)) 10628 return FALSE; 10629 } 10630 10631 if ((o->flags & SEC_RELOC) != 0) 10632 { 10633 Elf_Internal_Rela *internal_relocs; 10634 Elf_Internal_Rela *rel, *relend; 10635 int action_discarded; 10636 int ret; 10637 10638 /* Get the swapped relocs. */ 10639 internal_relocs 10640 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, 10641 flinfo->internal_relocs, FALSE); 10642 if (internal_relocs == NULL 10643 && o->reloc_count > 0) 10644 return FALSE; 10645 10646 /* We need to reverse-copy input .ctors/.dtors sections if 10647 they are placed in .init_array/.finit_array for output. */ 10648 if (o->size > address_size 10649 && ((strncmp (o->name, ".ctors", 6) == 0 10650 && strcmp (o->output_section->name, 10651 ".init_array") == 0) 10652 || (strncmp (o->name, ".dtors", 6) == 0 10653 && strcmp (o->output_section->name, 10654 ".fini_array") == 0)) 10655 && (o->name[6] == 0 || o->name[6] == '.')) 10656 { 10657 if (o->size * bed->s->int_rels_per_ext_rel 10658 != o->reloc_count * address_size) 10659 { 10660 _bfd_error_handler 10661 /* xgettext:c-format */ 10662 (_("error: %pB: size of section %pA is not " 10663 "multiple of address size"), 10664 input_bfd, o); 10665 bfd_set_error (bfd_error_bad_value); 10666 return FALSE; 10667 } 10668 o->flags |= SEC_ELF_REVERSE_COPY; 10669 } 10670 10671 action_discarded = -1; 10672 if (!elf_section_ignore_discarded_relocs (o)) 10673 action_discarded = (*bed->action_discarded) (o); 10674 10675 /* Run through the relocs evaluating complex reloc symbols and 10676 looking for relocs against symbols from discarded sections 10677 or section symbols from removed link-once sections. 10678 Complain about relocs against discarded sections. Zero 10679 relocs against removed link-once sections. */ 10680 10681 rel = internal_relocs; 10682 relend = rel + o->reloc_count; 10683 for ( ; rel < relend; rel++) 10684 { 10685 unsigned long r_symndx = rel->r_info >> r_sym_shift; 10686 unsigned int s_type; 10687 asection **ps, *sec; 10688 struct elf_link_hash_entry *h = NULL; 10689 const char *sym_name; 10690 10691 if (r_symndx == STN_UNDEF) 10692 continue; 10693 10694 if (r_symndx >= locsymcount 10695 || (elf_bad_symtab (input_bfd) 10696 && flinfo->sections[r_symndx] == NULL)) 10697 { 10698 h = sym_hashes[r_symndx - extsymoff]; 10699 10700 /* Badly formatted input files can contain relocs that 10701 reference non-existant symbols. Check here so that 10702 we do not seg fault. */ 10703 if (h == NULL) 10704 { 10705 _bfd_error_handler 10706 /* xgettext:c-format */ 10707 (_("error: %pB contains a reloc (%#" PRIx64 ") for section %pA " 10708 "that references a non-existent global symbol"), 10709 input_bfd, (uint64_t) rel->r_info, o); 10710 bfd_set_error (bfd_error_bad_value); 10711 return FALSE; 10712 } 10713 10714 while (h->root.type == bfd_link_hash_indirect 10715 || h->root.type == bfd_link_hash_warning) 10716 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10717 10718 s_type = h->type; 10719 10720 /* If a plugin symbol is referenced from a non-IR file, 10721 mark the symbol as undefined. Note that the 10722 linker may attach linker created dynamic sections 10723 to the plugin bfd. Symbols defined in linker 10724 created sections are not plugin symbols. */ 10725 if ((h->root.non_ir_ref_regular 10726 || h->root.non_ir_ref_dynamic) 10727 && (h->root.type == bfd_link_hash_defined 10728 || h->root.type == bfd_link_hash_defweak) 10729 && (h->root.u.def.section->flags 10730 & SEC_LINKER_CREATED) == 0 10731 && h->root.u.def.section->owner != NULL 10732 && (h->root.u.def.section->owner->flags 10733 & BFD_PLUGIN) != 0) 10734 { 10735 h->root.type = bfd_link_hash_undefined; 10736 h->root.u.undef.abfd = h->root.u.def.section->owner; 10737 } 10738 10739 ps = NULL; 10740 if (h->root.type == bfd_link_hash_defined 10741 || h->root.type == bfd_link_hash_defweak) 10742 ps = &h->root.u.def.section; 10743 10744 sym_name = h->root.root.string; 10745 } 10746 else 10747 { 10748 Elf_Internal_Sym *sym = isymbuf + r_symndx; 10749 10750 s_type = ELF_ST_TYPE (sym->st_info); 10751 ps = &flinfo->sections[r_symndx]; 10752 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, 10753 sym, *ps); 10754 } 10755 10756 if ((s_type == STT_RELC || s_type == STT_SRELC) 10757 && !bfd_link_relocatable (flinfo->info)) 10758 { 10759 bfd_vma val; 10760 bfd_vma dot = (rel->r_offset 10761 + o->output_offset + o->output_section->vma); 10762 #ifdef DEBUG 10763 printf ("Encountered a complex symbol!"); 10764 printf (" (input_bfd %s, section %s, reloc %ld\n", 10765 input_bfd->filename, o->name, 10766 (long) (rel - internal_relocs)); 10767 printf (" symbol: idx %8.8lx, name %s\n", 10768 r_symndx, sym_name); 10769 printf (" reloc : info %8.8lx, addr %8.8lx\n", 10770 (unsigned long) rel->r_info, 10771 (unsigned long) rel->r_offset); 10772 #endif 10773 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, 10774 isymbuf, locsymcount, s_type == STT_SRELC)) 10775 return FALSE; 10776 10777 /* Symbol evaluated OK. Update to absolute value. */ 10778 set_symbol_value (input_bfd, isymbuf, locsymcount, 10779 r_symndx, val); 10780 continue; 10781 } 10782 10783 if (action_discarded != -1 && ps != NULL) 10784 { 10785 /* Complain if the definition comes from a 10786 discarded section. */ 10787 if ((sec = *ps) != NULL && discarded_section (sec)) 10788 { 10789 BFD_ASSERT (r_symndx != STN_UNDEF); 10790 if (action_discarded & COMPLAIN) 10791 (*flinfo->info->callbacks->einfo) 10792 /* xgettext:c-format */ 10793 (_("%X`%s' referenced in section `%pA' of %pB: " 10794 "defined in discarded section `%pA' of %pB\n"), 10795 sym_name, o, input_bfd, sec, sec->owner); 10796 10797 /* Try to do the best we can to support buggy old 10798 versions of gcc. Pretend that the symbol is 10799 really defined in the kept linkonce section. 10800 FIXME: This is quite broken. Modifying the 10801 symbol here means we will be changing all later 10802 uses of the symbol, not just in this section. */ 10803 if (action_discarded & PRETEND) 10804 { 10805 asection *kept; 10806 10807 kept = _bfd_elf_check_kept_section (sec, 10808 flinfo->info); 10809 if (kept != NULL) 10810 { 10811 *ps = kept; 10812 continue; 10813 } 10814 } 10815 } 10816 } 10817 } 10818 10819 /* Relocate the section by invoking a back end routine. 10820 10821 The back end routine is responsible for adjusting the 10822 section contents as necessary, and (if using Rela relocs 10823 and generating a relocatable output file) adjusting the 10824 reloc addend as necessary. 10825 10826 The back end routine does not have to worry about setting 10827 the reloc address or the reloc symbol index. 10828 10829 The back end routine is given a pointer to the swapped in 10830 internal symbols, and can access the hash table entries 10831 for the external symbols via elf_sym_hashes (input_bfd). 10832 10833 When generating relocatable output, the back end routine 10834 must handle STB_LOCAL/STT_SECTION symbols specially. The 10835 output symbol is going to be a section symbol 10836 corresponding to the output section, which will require 10837 the addend to be adjusted. */ 10838 10839 ret = (*relocate_section) (output_bfd, flinfo->info, 10840 input_bfd, o, contents, 10841 internal_relocs, 10842 isymbuf, 10843 flinfo->sections); 10844 if (!ret) 10845 return FALSE; 10846 10847 if (ret == 2 10848 || bfd_link_relocatable (flinfo->info) 10849 || flinfo->info->emitrelocations) 10850 { 10851 Elf_Internal_Rela *irela; 10852 Elf_Internal_Rela *irelaend, *irelamid; 10853 bfd_vma last_offset; 10854 struct elf_link_hash_entry **rel_hash; 10855 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; 10856 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; 10857 unsigned int next_erel; 10858 bfd_boolean rela_normal; 10859 struct bfd_elf_section_data *esdi, *esdo; 10860 10861 esdi = elf_section_data (o); 10862 esdo = elf_section_data (o->output_section); 10863 rela_normal = FALSE; 10864 10865 /* Adjust the reloc addresses and symbol indices. */ 10866 10867 irela = internal_relocs; 10868 irelaend = irela + o->reloc_count; 10869 rel_hash = esdo->rel.hashes + esdo->rel.count; 10870 /* We start processing the REL relocs, if any. When we reach 10871 IRELAMID in the loop, we switch to the RELA relocs. */ 10872 irelamid = irela; 10873 if (esdi->rel.hdr != NULL) 10874 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) 10875 * bed->s->int_rels_per_ext_rel); 10876 rel_hash_list = rel_hash; 10877 rela_hash_list = NULL; 10878 last_offset = o->output_offset; 10879 if (!bfd_link_relocatable (flinfo->info)) 10880 last_offset += o->output_section->vma; 10881 for (next_erel = 0; irela < irelaend; irela++, next_erel++) 10882 { 10883 unsigned long r_symndx; 10884 asection *sec; 10885 Elf_Internal_Sym sym; 10886 10887 if (next_erel == bed->s->int_rels_per_ext_rel) 10888 { 10889 rel_hash++; 10890 next_erel = 0; 10891 } 10892 10893 if (irela == irelamid) 10894 { 10895 rel_hash = esdo->rela.hashes + esdo->rela.count; 10896 rela_hash_list = rel_hash; 10897 rela_normal = bed->rela_normal; 10898 } 10899 10900 irela->r_offset = _bfd_elf_section_offset (output_bfd, 10901 flinfo->info, o, 10902 irela->r_offset); 10903 if (irela->r_offset >= (bfd_vma) -2) 10904 { 10905 /* This is a reloc for a deleted entry or somesuch. 10906 Turn it into an R_*_NONE reloc, at the same 10907 offset as the last reloc. elf_eh_frame.c and 10908 bfd_elf_discard_info rely on reloc offsets 10909 being ordered. */ 10910 irela->r_offset = last_offset; 10911 irela->r_info = 0; 10912 irela->r_addend = 0; 10913 continue; 10914 } 10915 10916 irela->r_offset += o->output_offset; 10917 10918 /* Relocs in an executable have to be virtual addresses. */ 10919 if (!bfd_link_relocatable (flinfo->info)) 10920 irela->r_offset += o->output_section->vma; 10921 10922 last_offset = irela->r_offset; 10923 10924 r_symndx = irela->r_info >> r_sym_shift; 10925 if (r_symndx == STN_UNDEF) 10926 continue; 10927 10928 if (r_symndx >= locsymcount 10929 || (elf_bad_symtab (input_bfd) 10930 && flinfo->sections[r_symndx] == NULL)) 10931 { 10932 struct elf_link_hash_entry *rh; 10933 unsigned long indx; 10934 10935 /* This is a reloc against a global symbol. We 10936 have not yet output all the local symbols, so 10937 we do not know the symbol index of any global 10938 symbol. We set the rel_hash entry for this 10939 reloc to point to the global hash table entry 10940 for this symbol. The symbol index is then 10941 set at the end of bfd_elf_final_link. */ 10942 indx = r_symndx - extsymoff; 10943 rh = elf_sym_hashes (input_bfd)[indx]; 10944 while (rh->root.type == bfd_link_hash_indirect 10945 || rh->root.type == bfd_link_hash_warning) 10946 rh = (struct elf_link_hash_entry *) rh->root.u.i.link; 10947 10948 /* Setting the index to -2 tells 10949 elf_link_output_extsym that this symbol is 10950 used by a reloc. */ 10951 BFD_ASSERT (rh->indx < 0); 10952 rh->indx = -2; 10953 *rel_hash = rh; 10954 10955 continue; 10956 } 10957 10958 /* This is a reloc against a local symbol. */ 10959 10960 *rel_hash = NULL; 10961 sym = isymbuf[r_symndx]; 10962 sec = flinfo->sections[r_symndx]; 10963 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) 10964 { 10965 /* I suppose the backend ought to fill in the 10966 section of any STT_SECTION symbol against a 10967 processor specific section. */ 10968 r_symndx = STN_UNDEF; 10969 if (bfd_is_abs_section (sec)) 10970 ; 10971 else if (sec == NULL || sec->owner == NULL) 10972 { 10973 bfd_set_error (bfd_error_bad_value); 10974 return FALSE; 10975 } 10976 else 10977 { 10978 asection *osec = sec->output_section; 10979 10980 /* If we have discarded a section, the output 10981 section will be the absolute section. In 10982 case of discarded SEC_MERGE sections, use 10983 the kept section. relocate_section should 10984 have already handled discarded linkonce 10985 sections. */ 10986 if (bfd_is_abs_section (osec) 10987 && sec->kept_section != NULL 10988 && sec->kept_section->output_section != NULL) 10989 { 10990 osec = sec->kept_section->output_section; 10991 irela->r_addend -= osec->vma; 10992 } 10993 10994 if (!bfd_is_abs_section (osec)) 10995 { 10996 r_symndx = osec->target_index; 10997 if (r_symndx == STN_UNDEF) 10998 { 10999 irela->r_addend += osec->vma; 11000 osec = _bfd_nearby_section (output_bfd, osec, 11001 osec->vma); 11002 irela->r_addend -= osec->vma; 11003 r_symndx = osec->target_index; 11004 } 11005 } 11006 } 11007 11008 /* Adjust the addend according to where the 11009 section winds up in the output section. */ 11010 if (rela_normal) 11011 irela->r_addend += sec->output_offset; 11012 } 11013 else 11014 { 11015 if (flinfo->indices[r_symndx] == -1) 11016 { 11017 unsigned long shlink; 11018 const char *name; 11019 asection *osec; 11020 long indx; 11021 11022 if (flinfo->info->strip == strip_all) 11023 { 11024 /* You can't do ld -r -s. */ 11025 bfd_set_error (bfd_error_invalid_operation); 11026 return FALSE; 11027 } 11028 11029 /* This symbol was skipped earlier, but 11030 since it is needed by a reloc, we 11031 must output it now. */ 11032 shlink = symtab_hdr->sh_link; 11033 name = (bfd_elf_string_from_elf_section 11034 (input_bfd, shlink, sym.st_name)); 11035 if (name == NULL) 11036 return FALSE; 11037 11038 osec = sec->output_section; 11039 sym.st_shndx = 11040 _bfd_elf_section_from_bfd_section (output_bfd, 11041 osec); 11042 if (sym.st_shndx == SHN_BAD) 11043 return FALSE; 11044 11045 sym.st_value += sec->output_offset; 11046 if (!bfd_link_relocatable (flinfo->info)) 11047 { 11048 sym.st_value += osec->vma; 11049 if (ELF_ST_TYPE (sym.st_info) == STT_TLS) 11050 { 11051 /* STT_TLS symbols are relative to PT_TLS 11052 segment base. */ 11053 BFD_ASSERT (elf_hash_table (flinfo->info) 11054 ->tls_sec != NULL); 11055 sym.st_value -= (elf_hash_table (flinfo->info) 11056 ->tls_sec->vma); 11057 } 11058 } 11059 11060 indx = bfd_get_symcount (output_bfd); 11061 ret = elf_link_output_symstrtab (flinfo, name, 11062 &sym, sec, 11063 NULL); 11064 if (ret == 0) 11065 return FALSE; 11066 else if (ret == 1) 11067 flinfo->indices[r_symndx] = indx; 11068 else 11069 abort (); 11070 } 11071 11072 r_symndx = flinfo->indices[r_symndx]; 11073 } 11074 11075 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift 11076 | (irela->r_info & r_type_mask)); 11077 } 11078 11079 /* Swap out the relocs. */ 11080 input_rel_hdr = esdi->rel.hdr; 11081 if (input_rel_hdr && input_rel_hdr->sh_size != 0) 11082 { 11083 if (!bed->elf_backend_emit_relocs (output_bfd, o, 11084 input_rel_hdr, 11085 internal_relocs, 11086 rel_hash_list)) 11087 return FALSE; 11088 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) 11089 * bed->s->int_rels_per_ext_rel); 11090 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); 11091 } 11092 11093 input_rela_hdr = esdi->rela.hdr; 11094 if (input_rela_hdr && input_rela_hdr->sh_size != 0) 11095 { 11096 if (!bed->elf_backend_emit_relocs (output_bfd, o, 11097 input_rela_hdr, 11098 internal_relocs, 11099 rela_hash_list)) 11100 return FALSE; 11101 } 11102 } 11103 } 11104 11105 /* Write out the modified section contents. */ 11106 if (bed->elf_backend_write_section 11107 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, 11108 contents)) 11109 { 11110 /* Section written out. */ 11111 } 11112 else switch (o->sec_info_type) 11113 { 11114 case SEC_INFO_TYPE_STABS: 11115 if (! (_bfd_write_section_stabs 11116 (output_bfd, 11117 &elf_hash_table (flinfo->info)->stab_info, 11118 o, &elf_section_data (o)->sec_info, contents))) 11119 return FALSE; 11120 break; 11121 case SEC_INFO_TYPE_MERGE: 11122 if (! _bfd_write_merged_section (output_bfd, o, 11123 elf_section_data (o)->sec_info)) 11124 return FALSE; 11125 break; 11126 case SEC_INFO_TYPE_EH_FRAME: 11127 { 11128 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, 11129 o, contents)) 11130 return FALSE; 11131 } 11132 break; 11133 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 11134 { 11135 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, 11136 flinfo->info, 11137 o, contents)) 11138 return FALSE; 11139 } 11140 break; 11141 default: 11142 { 11143 if (! (o->flags & SEC_EXCLUDE)) 11144 { 11145 file_ptr offset = (file_ptr) o->output_offset; 11146 bfd_size_type todo = o->size; 11147 11148 offset *= bfd_octets_per_byte (output_bfd); 11149 11150 if ((o->flags & SEC_ELF_REVERSE_COPY)) 11151 { 11152 /* Reverse-copy input section to output. */ 11153 do 11154 { 11155 todo -= address_size; 11156 if (! bfd_set_section_contents (output_bfd, 11157 o->output_section, 11158 contents + todo, 11159 offset, 11160 address_size)) 11161 return FALSE; 11162 if (todo == 0) 11163 break; 11164 offset += address_size; 11165 } 11166 while (1); 11167 } 11168 else if (! bfd_set_section_contents (output_bfd, 11169 o->output_section, 11170 contents, 11171 offset, todo)) 11172 return FALSE; 11173 } 11174 } 11175 break; 11176 } 11177 } 11178 11179 return TRUE; 11180 } 11181 11182 /* Generate a reloc when linking an ELF file. This is a reloc 11183 requested by the linker, and does not come from any input file. This 11184 is used to build constructor and destructor tables when linking 11185 with -Ur. */ 11186 11187 static bfd_boolean 11188 elf_reloc_link_order (bfd *output_bfd, 11189 struct bfd_link_info *info, 11190 asection *output_section, 11191 struct bfd_link_order *link_order) 11192 { 11193 reloc_howto_type *howto; 11194 long indx; 11195 bfd_vma offset; 11196 bfd_vma addend; 11197 struct bfd_elf_section_reloc_data *reldata; 11198 struct elf_link_hash_entry **rel_hash_ptr; 11199 Elf_Internal_Shdr *rel_hdr; 11200 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 11201 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; 11202 bfd_byte *erel; 11203 unsigned int i; 11204 struct bfd_elf_section_data *esdo = elf_section_data (output_section); 11205 11206 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 11207 if (howto == NULL) 11208 { 11209 bfd_set_error (bfd_error_bad_value); 11210 return FALSE; 11211 } 11212 11213 addend = link_order->u.reloc.p->addend; 11214 11215 if (esdo->rel.hdr) 11216 reldata = &esdo->rel; 11217 else if (esdo->rela.hdr) 11218 reldata = &esdo->rela; 11219 else 11220 { 11221 reldata = NULL; 11222 BFD_ASSERT (0); 11223 } 11224 11225 /* Figure out the symbol index. */ 11226 rel_hash_ptr = reldata->hashes + reldata->count; 11227 if (link_order->type == bfd_section_reloc_link_order) 11228 { 11229 indx = link_order->u.reloc.p->u.section->target_index; 11230 BFD_ASSERT (indx != 0); 11231 *rel_hash_ptr = NULL; 11232 } 11233 else 11234 { 11235 struct elf_link_hash_entry *h; 11236 11237 /* Treat a reloc against a defined symbol as though it were 11238 actually against the section. */ 11239 h = ((struct elf_link_hash_entry *) 11240 bfd_wrapped_link_hash_lookup (output_bfd, info, 11241 link_order->u.reloc.p->u.name, 11242 FALSE, FALSE, TRUE)); 11243 if (h != NULL 11244 && (h->root.type == bfd_link_hash_defined 11245 || h->root.type == bfd_link_hash_defweak)) 11246 { 11247 asection *section; 11248 11249 section = h->root.u.def.section; 11250 indx = section->output_section->target_index; 11251 *rel_hash_ptr = NULL; 11252 /* It seems that we ought to add the symbol value to the 11253 addend here, but in practice it has already been added 11254 because it was passed to constructor_callback. */ 11255 addend += section->output_section->vma + section->output_offset; 11256 } 11257 else if (h != NULL) 11258 { 11259 /* Setting the index to -2 tells elf_link_output_extsym that 11260 this symbol is used by a reloc. */ 11261 h->indx = -2; 11262 *rel_hash_ptr = h; 11263 indx = 0; 11264 } 11265 else 11266 { 11267 (*info->callbacks->unattached_reloc) 11268 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); 11269 indx = 0; 11270 } 11271 } 11272 11273 /* If this is an inplace reloc, we must write the addend into the 11274 object file. */ 11275 if (howto->partial_inplace && addend != 0) 11276 { 11277 bfd_size_type size; 11278 bfd_reloc_status_type rstat; 11279 bfd_byte *buf; 11280 bfd_boolean ok; 11281 const char *sym_name; 11282 11283 size = (bfd_size_type) bfd_get_reloc_size (howto); 11284 buf = (bfd_byte *) bfd_zmalloc (size); 11285 if (buf == NULL && size != 0) 11286 return FALSE; 11287 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 11288 switch (rstat) 11289 { 11290 case bfd_reloc_ok: 11291 break; 11292 11293 default: 11294 case bfd_reloc_outofrange: 11295 abort (); 11296 11297 case bfd_reloc_overflow: 11298 if (link_order->type == bfd_section_reloc_link_order) 11299 sym_name = bfd_section_name (output_bfd, 11300 link_order->u.reloc.p->u.section); 11301 else 11302 sym_name = link_order->u.reloc.p->u.name; 11303 (*info->callbacks->reloc_overflow) (info, NULL, sym_name, 11304 howto->name, addend, NULL, NULL, 11305 (bfd_vma) 0); 11306 break; 11307 } 11308 11309 ok = bfd_set_section_contents (output_bfd, output_section, buf, 11310 link_order->offset 11311 * bfd_octets_per_byte (output_bfd), 11312 size); 11313 free (buf); 11314 if (! ok) 11315 return FALSE; 11316 } 11317 11318 /* The address of a reloc is relative to the section in a 11319 relocatable file, and is a virtual address in an executable 11320 file. */ 11321 offset = link_order->offset; 11322 if (! bfd_link_relocatable (info)) 11323 offset += output_section->vma; 11324 11325 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) 11326 { 11327 irel[i].r_offset = offset; 11328 irel[i].r_info = 0; 11329 irel[i].r_addend = 0; 11330 } 11331 if (bed->s->arch_size == 32) 11332 irel[0].r_info = ELF32_R_INFO (indx, howto->type); 11333 else 11334 #ifdef BFD64 11335 { 11336 bfd_uint64_t indx64 = indx; 11337 irel[0].r_info = ELF64_R_INFO (indx64, howto->type); 11338 } 11339 #else 11340 BFD_FAIL(); 11341 #endif 11342 11343 rel_hdr = reldata->hdr; 11344 erel = rel_hdr->contents; 11345 if (rel_hdr->sh_type == SHT_REL) 11346 { 11347 erel += reldata->count * bed->s->sizeof_rel; 11348 (*bed->s->swap_reloc_out) (output_bfd, irel, erel); 11349 } 11350 else 11351 { 11352 irel[0].r_addend = addend; 11353 erel += reldata->count * bed->s->sizeof_rela; 11354 (*bed->s->swap_reloca_out) (output_bfd, irel, erel); 11355 } 11356 11357 ++reldata->count; 11358 11359 return TRUE; 11360 } 11361 11362 11363 /* Get the output vma of the section pointed to by the sh_link field. */ 11364 11365 static bfd_vma 11366 elf_get_linked_section_vma (struct bfd_link_order *p) 11367 { 11368 Elf_Internal_Shdr **elf_shdrp; 11369 asection *s; 11370 int elfsec; 11371 11372 s = p->u.indirect.section; 11373 elf_shdrp = elf_elfsections (s->owner); 11374 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); 11375 elfsec = elf_shdrp[elfsec]->sh_link; 11376 /* PR 290: 11377 The Intel C compiler generates SHT_IA_64_UNWIND with 11378 SHF_LINK_ORDER. But it doesn't set the sh_link or 11379 sh_info fields. Hence we could get the situation 11380 where elfsec is 0. */ 11381 if (elfsec == 0) 11382 { 11383 const struct elf_backend_data *bed 11384 = get_elf_backend_data (s->owner); 11385 if (bed->link_order_error_handler) 11386 bed->link_order_error_handler 11387 /* xgettext:c-format */ 11388 (_("%pB: warning: sh_link not set for section `%pA'"), s->owner, s); 11389 return 0; 11390 } 11391 else 11392 { 11393 s = elf_shdrp[elfsec]->bfd_section; 11394 return s->output_section->vma + s->output_offset; 11395 } 11396 } 11397 11398 11399 /* Compare two sections based on the locations of the sections they are 11400 linked to. Used by elf_fixup_link_order. */ 11401 11402 static int 11403 compare_link_order (const void * a, const void * b) 11404 { 11405 bfd_vma apos; 11406 bfd_vma bpos; 11407 11408 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); 11409 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); 11410 if (apos < bpos) 11411 return -1; 11412 return apos > bpos; 11413 } 11414 11415 11416 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same 11417 order as their linked sections. Returns false if this could not be done 11418 because an output section includes both ordered and unordered 11419 sections. Ideally we'd do this in the linker proper. */ 11420 11421 static bfd_boolean 11422 elf_fixup_link_order (bfd *abfd, asection *o) 11423 { 11424 int seen_linkorder; 11425 int seen_other; 11426 int n; 11427 struct bfd_link_order *p; 11428 bfd *sub; 11429 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11430 unsigned elfsec; 11431 struct bfd_link_order **sections; 11432 asection *s, *other_sec, *linkorder_sec; 11433 bfd_vma offset; 11434 11435 other_sec = NULL; 11436 linkorder_sec = NULL; 11437 seen_other = 0; 11438 seen_linkorder = 0; 11439 for (p = o->map_head.link_order; p != NULL; p = p->next) 11440 { 11441 if (p->type == bfd_indirect_link_order) 11442 { 11443 s = p->u.indirect.section; 11444 sub = s->owner; 11445 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 11446 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass 11447 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) 11448 && elfsec < elf_numsections (sub) 11449 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER 11450 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) 11451 { 11452 seen_linkorder++; 11453 linkorder_sec = s; 11454 } 11455 else 11456 { 11457 seen_other++; 11458 other_sec = s; 11459 } 11460 } 11461 else 11462 seen_other++; 11463 11464 if (seen_other && seen_linkorder) 11465 { 11466 if (other_sec && linkorder_sec) 11467 _bfd_error_handler 11468 /* xgettext:c-format */ 11469 (_("%pA has both ordered [`%pA' in %pB] " 11470 "and unordered [`%pA' in %pB] sections"), 11471 o, linkorder_sec, linkorder_sec->owner, 11472 other_sec, other_sec->owner); 11473 else 11474 _bfd_error_handler 11475 (_("%pA has both ordered and unordered sections"), o); 11476 bfd_set_error (bfd_error_bad_value); 11477 return FALSE; 11478 } 11479 } 11480 11481 if (!seen_linkorder) 11482 return TRUE; 11483 11484 sections = (struct bfd_link_order **) 11485 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); 11486 if (sections == NULL) 11487 return FALSE; 11488 seen_linkorder = 0; 11489 11490 for (p = o->map_head.link_order; p != NULL; p = p->next) 11491 { 11492 sections[seen_linkorder++] = p; 11493 } 11494 /* Sort the input sections in the order of their linked section. */ 11495 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), 11496 compare_link_order); 11497 11498 /* Change the offsets of the sections. */ 11499 offset = 0; 11500 for (n = 0; n < seen_linkorder; n++) 11501 { 11502 s = sections[n]->u.indirect.section; 11503 offset &= ~(bfd_vma) 0 << s->alignment_power; 11504 s->output_offset = offset / bfd_octets_per_byte (abfd); 11505 sections[n]->offset = offset; 11506 offset += sections[n]->size; 11507 } 11508 11509 free (sections); 11510 return TRUE; 11511 } 11512 11513 /* Generate an import library in INFO->implib_bfd from symbols in ABFD. 11514 Returns TRUE upon success, FALSE otherwise. */ 11515 11516 static bfd_boolean 11517 elf_output_implib (bfd *abfd, struct bfd_link_info *info) 11518 { 11519 bfd_boolean ret = FALSE; 11520 bfd *implib_bfd; 11521 const struct elf_backend_data *bed; 11522 flagword flags; 11523 enum bfd_architecture arch; 11524 unsigned int mach; 11525 asymbol **sympp = NULL; 11526 long symsize; 11527 long symcount; 11528 long src_count; 11529 elf_symbol_type *osymbuf; 11530 11531 implib_bfd = info->out_implib_bfd; 11532 bed = get_elf_backend_data (abfd); 11533 11534 if (!bfd_set_format (implib_bfd, bfd_object)) 11535 return FALSE; 11536 11537 /* Use flag from executable but make it a relocatable object. */ 11538 flags = bfd_get_file_flags (abfd); 11539 flags &= ~HAS_RELOC; 11540 if (!bfd_set_start_address (implib_bfd, 0) 11541 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) 11542 return FALSE; 11543 11544 /* Copy architecture of output file to import library file. */ 11545 arch = bfd_get_arch (abfd); 11546 mach = bfd_get_mach (abfd); 11547 if (!bfd_set_arch_mach (implib_bfd, arch, mach) 11548 && (abfd->target_defaulted 11549 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) 11550 return FALSE; 11551 11552 /* Get symbol table size. */ 11553 symsize = bfd_get_symtab_upper_bound (abfd); 11554 if (symsize < 0) 11555 return FALSE; 11556 11557 /* Read in the symbol table. */ 11558 sympp = (asymbol **) xmalloc (symsize); 11559 symcount = bfd_canonicalize_symtab (abfd, sympp); 11560 if (symcount < 0) 11561 goto free_sym_buf; 11562 11563 /* Allow the BFD backend to copy any private header data it 11564 understands from the output BFD to the import library BFD. */ 11565 if (! bfd_copy_private_header_data (abfd, implib_bfd)) 11566 goto free_sym_buf; 11567 11568 /* Filter symbols to appear in the import library. */ 11569 if (bed->elf_backend_filter_implib_symbols) 11570 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, 11571 symcount); 11572 else 11573 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); 11574 if (symcount == 0) 11575 { 11576 bfd_set_error (bfd_error_no_symbols); 11577 _bfd_error_handler (_("%pB: no symbol found for import library"), 11578 implib_bfd); 11579 goto free_sym_buf; 11580 } 11581 11582 11583 /* Make symbols absolute. */ 11584 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, 11585 sizeof (*osymbuf)); 11586 for (src_count = 0; src_count < symcount; src_count++) 11587 { 11588 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], 11589 sizeof (*osymbuf)); 11590 osymbuf[src_count].symbol.section = bfd_abs_section_ptr; 11591 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; 11592 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; 11593 osymbuf[src_count].internal_elf_sym.st_value = 11594 osymbuf[src_count].symbol.value; 11595 sympp[src_count] = &osymbuf[src_count].symbol; 11596 } 11597 11598 bfd_set_symtab (implib_bfd, sympp, symcount); 11599 11600 /* Allow the BFD backend to copy any private data it understands 11601 from the output BFD to the import library BFD. This is done last 11602 to permit the routine to look at the filtered symbol table. */ 11603 if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) 11604 goto free_sym_buf; 11605 11606 if (!bfd_close (implib_bfd)) 11607 goto free_sym_buf; 11608 11609 ret = TRUE; 11610 11611 free_sym_buf: 11612 free (sympp); 11613 return ret; 11614 } 11615 11616 static void 11617 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) 11618 { 11619 asection *o; 11620 11621 if (flinfo->symstrtab != NULL) 11622 _bfd_elf_strtab_free (flinfo->symstrtab); 11623 if (flinfo->contents != NULL) 11624 free (flinfo->contents); 11625 if (flinfo->external_relocs != NULL) 11626 free (flinfo->external_relocs); 11627 if (flinfo->internal_relocs != NULL) 11628 free (flinfo->internal_relocs); 11629 if (flinfo->external_syms != NULL) 11630 free (flinfo->external_syms); 11631 if (flinfo->locsym_shndx != NULL) 11632 free (flinfo->locsym_shndx); 11633 if (flinfo->internal_syms != NULL) 11634 free (flinfo->internal_syms); 11635 if (flinfo->indices != NULL) 11636 free (flinfo->indices); 11637 if (flinfo->sections != NULL) 11638 free (flinfo->sections); 11639 if (flinfo->symshndxbuf != NULL) 11640 free (flinfo->symshndxbuf); 11641 for (o = obfd->sections; o != NULL; o = o->next) 11642 { 11643 struct bfd_elf_section_data *esdo = elf_section_data (o); 11644 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) 11645 free (esdo->rel.hashes); 11646 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) 11647 free (esdo->rela.hashes); 11648 } 11649 } 11650 11651 /* Do the final step of an ELF link. */ 11652 11653 bfd_boolean 11654 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) 11655 { 11656 bfd_boolean dynamic; 11657 bfd_boolean emit_relocs; 11658 bfd *dynobj; 11659 struct elf_final_link_info flinfo; 11660 asection *o; 11661 struct bfd_link_order *p; 11662 bfd *sub; 11663 bfd_size_type max_contents_size; 11664 bfd_size_type max_external_reloc_size; 11665 bfd_size_type max_internal_reloc_count; 11666 bfd_size_type max_sym_count; 11667 bfd_size_type max_sym_shndx_count; 11668 Elf_Internal_Sym elfsym; 11669 unsigned int i; 11670 Elf_Internal_Shdr *symtab_hdr; 11671 Elf_Internal_Shdr *symtab_shndx_hdr; 11672 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11673 struct elf_outext_info eoinfo; 11674 bfd_boolean merged; 11675 size_t relativecount = 0; 11676 asection *reldyn = 0; 11677 bfd_size_type amt; 11678 asection *attr_section = NULL; 11679 bfd_vma attr_size = 0; 11680 const char *std_attrs_section; 11681 struct elf_link_hash_table *htab = elf_hash_table (info); 11682 11683 if (!is_elf_hash_table (htab)) 11684 return FALSE; 11685 11686 if (bfd_link_pic (info)) 11687 abfd->flags |= DYNAMIC; 11688 11689 dynamic = htab->dynamic_sections_created; 11690 dynobj = htab->dynobj; 11691 11692 emit_relocs = (bfd_link_relocatable (info) 11693 || info->emitrelocations); 11694 11695 flinfo.info = info; 11696 flinfo.output_bfd = abfd; 11697 flinfo.symstrtab = _bfd_elf_strtab_init (); 11698 if (flinfo.symstrtab == NULL) 11699 return FALSE; 11700 11701 if (! dynamic) 11702 { 11703 flinfo.hash_sec = NULL; 11704 flinfo.symver_sec = NULL; 11705 } 11706 else 11707 { 11708 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); 11709 /* Note that dynsym_sec can be NULL (on VMS). */ 11710 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); 11711 /* Note that it is OK if symver_sec is NULL. */ 11712 } 11713 11714 flinfo.contents = NULL; 11715 flinfo.external_relocs = NULL; 11716 flinfo.internal_relocs = NULL; 11717 flinfo.external_syms = NULL; 11718 flinfo.locsym_shndx = NULL; 11719 flinfo.internal_syms = NULL; 11720 flinfo.indices = NULL; 11721 flinfo.sections = NULL; 11722 flinfo.symshndxbuf = NULL; 11723 flinfo.filesym_count = 0; 11724 11725 /* The object attributes have been merged. Remove the input 11726 sections from the link, and set the contents of the output 11727 secton. */ 11728 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; 11729 for (o = abfd->sections; o != NULL; o = o->next) 11730 { 11731 bfd_boolean remove_section = FALSE; 11732 11733 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) 11734 || strcmp (o->name, ".gnu.attributes") == 0) 11735 { 11736 for (p = o->map_head.link_order; p != NULL; p = p->next) 11737 { 11738 asection *input_section; 11739 11740 if (p->type != bfd_indirect_link_order) 11741 continue; 11742 input_section = p->u.indirect.section; 11743 /* Hack: reset the SEC_HAS_CONTENTS flag so that 11744 elf_link_input_bfd ignores this section. */ 11745 input_section->flags &= ~SEC_HAS_CONTENTS; 11746 } 11747 11748 attr_size = bfd_elf_obj_attr_size (abfd); 11749 bfd_set_section_size (abfd, o, attr_size); 11750 /* Skip this section later on. */ 11751 o->map_head.link_order = NULL; 11752 if (attr_size) 11753 attr_section = o; 11754 else 11755 remove_section = TRUE; 11756 } 11757 else if ((o->flags & SEC_GROUP) != 0 && o->size == 0) 11758 { 11759 /* Remove empty group section from linker output. */ 11760 remove_section = TRUE; 11761 } 11762 if (remove_section) 11763 { 11764 o->flags |= SEC_EXCLUDE; 11765 bfd_section_list_remove (abfd, o); 11766 abfd->section_count--; 11767 } 11768 } 11769 11770 /* Count up the number of relocations we will output for each output 11771 section, so that we know the sizes of the reloc sections. We 11772 also figure out some maximum sizes. */ 11773 max_contents_size = 0; 11774 max_external_reloc_size = 0; 11775 max_internal_reloc_count = 0; 11776 max_sym_count = 0; 11777 max_sym_shndx_count = 0; 11778 merged = FALSE; 11779 for (o = abfd->sections; o != NULL; o = o->next) 11780 { 11781 struct bfd_elf_section_data *esdo = elf_section_data (o); 11782 o->reloc_count = 0; 11783 11784 for (p = o->map_head.link_order; p != NULL; p = p->next) 11785 { 11786 unsigned int reloc_count = 0; 11787 unsigned int additional_reloc_count = 0; 11788 struct bfd_elf_section_data *esdi = NULL; 11789 11790 if (p->type == bfd_section_reloc_link_order 11791 || p->type == bfd_symbol_reloc_link_order) 11792 reloc_count = 1; 11793 else if (p->type == bfd_indirect_link_order) 11794 { 11795 asection *sec; 11796 11797 sec = p->u.indirect.section; 11798 11799 /* Mark all sections which are to be included in the 11800 link. This will normally be every section. We need 11801 to do this so that we can identify any sections which 11802 the linker has decided to not include. */ 11803 sec->linker_mark = TRUE; 11804 11805 if (sec->flags & SEC_MERGE) 11806 merged = TRUE; 11807 11808 if (sec->rawsize > max_contents_size) 11809 max_contents_size = sec->rawsize; 11810 if (sec->size > max_contents_size) 11811 max_contents_size = sec->size; 11812 11813 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour 11814 && (sec->owner->flags & DYNAMIC) == 0) 11815 { 11816 size_t sym_count; 11817 11818 /* We are interested in just local symbols, not all 11819 symbols. */ 11820 if (elf_bad_symtab (sec->owner)) 11821 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size 11822 / bed->s->sizeof_sym); 11823 else 11824 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; 11825 11826 if (sym_count > max_sym_count) 11827 max_sym_count = sym_count; 11828 11829 if (sym_count > max_sym_shndx_count 11830 && elf_symtab_shndx_list (sec->owner) != NULL) 11831 max_sym_shndx_count = sym_count; 11832 11833 if (esdo->this_hdr.sh_type == SHT_REL 11834 || esdo->this_hdr.sh_type == SHT_RELA) 11835 /* Some backends use reloc_count in relocation sections 11836 to count particular types of relocs. Of course, 11837 reloc sections themselves can't have relocations. */ 11838 ; 11839 else if (emit_relocs) 11840 { 11841 reloc_count = sec->reloc_count; 11842 if (bed->elf_backend_count_additional_relocs) 11843 { 11844 int c; 11845 c = (*bed->elf_backend_count_additional_relocs) (sec); 11846 additional_reloc_count += c; 11847 } 11848 } 11849 else if (bed->elf_backend_count_relocs) 11850 reloc_count = (*bed->elf_backend_count_relocs) (info, sec); 11851 11852 esdi = elf_section_data (sec); 11853 11854 if ((sec->flags & SEC_RELOC) != 0) 11855 { 11856 size_t ext_size = 0; 11857 11858 if (esdi->rel.hdr != NULL) 11859 ext_size = esdi->rel.hdr->sh_size; 11860 if (esdi->rela.hdr != NULL) 11861 ext_size += esdi->rela.hdr->sh_size; 11862 11863 if (ext_size > max_external_reloc_size) 11864 max_external_reloc_size = ext_size; 11865 if (sec->reloc_count > max_internal_reloc_count) 11866 max_internal_reloc_count = sec->reloc_count; 11867 } 11868 } 11869 } 11870 11871 if (reloc_count == 0) 11872 continue; 11873 11874 reloc_count += additional_reloc_count; 11875 o->reloc_count += reloc_count; 11876 11877 if (p->type == bfd_indirect_link_order && emit_relocs) 11878 { 11879 if (esdi->rel.hdr) 11880 { 11881 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); 11882 esdo->rel.count += additional_reloc_count; 11883 } 11884 if (esdi->rela.hdr) 11885 { 11886 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); 11887 esdo->rela.count += additional_reloc_count; 11888 } 11889 } 11890 else 11891 { 11892 if (o->use_rela_p) 11893 esdo->rela.count += reloc_count; 11894 else 11895 esdo->rel.count += reloc_count; 11896 } 11897 } 11898 11899 if (o->reloc_count > 0) 11900 o->flags |= SEC_RELOC; 11901 else 11902 { 11903 /* Explicitly clear the SEC_RELOC flag. The linker tends to 11904 set it (this is probably a bug) and if it is set 11905 assign_section_numbers will create a reloc section. */ 11906 o->flags &=~ SEC_RELOC; 11907 } 11908 11909 /* If the SEC_ALLOC flag is not set, force the section VMA to 11910 zero. This is done in elf_fake_sections as well, but forcing 11911 the VMA to 0 here will ensure that relocs against these 11912 sections are handled correctly. */ 11913 if ((o->flags & SEC_ALLOC) == 0 11914 && ! o->user_set_vma) 11915 o->vma = 0; 11916 } 11917 11918 if (! bfd_link_relocatable (info) && merged) 11919 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); 11920 11921 /* Figure out the file positions for everything but the symbol table 11922 and the relocs. We set symcount to force assign_section_numbers 11923 to create a symbol table. */ 11924 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; 11925 BFD_ASSERT (! abfd->output_has_begun); 11926 if (! _bfd_elf_compute_section_file_positions (abfd, info)) 11927 goto error_return; 11928 11929 /* Set sizes, and assign file positions for reloc sections. */ 11930 for (o = abfd->sections; o != NULL; o = o->next) 11931 { 11932 struct bfd_elf_section_data *esdo = elf_section_data (o); 11933 if ((o->flags & SEC_RELOC) != 0) 11934 { 11935 if (esdo->rel.hdr 11936 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) 11937 goto error_return; 11938 11939 if (esdo->rela.hdr 11940 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) 11941 goto error_return; 11942 } 11943 11944 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them 11945 to count upwards while actually outputting the relocations. */ 11946 esdo->rel.count = 0; 11947 esdo->rela.count = 0; 11948 11949 if (esdo->this_hdr.sh_offset == (file_ptr) -1) 11950 { 11951 /* Cache the section contents so that they can be compressed 11952 later. Use bfd_malloc since it will be freed by 11953 bfd_compress_section_contents. */ 11954 unsigned char *contents = esdo->this_hdr.contents; 11955 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) 11956 abort (); 11957 contents 11958 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); 11959 if (contents == NULL) 11960 goto error_return; 11961 esdo->this_hdr.contents = contents; 11962 } 11963 } 11964 11965 /* We have now assigned file positions for all the sections except 11966 .symtab, .strtab, and non-loaded reloc sections. We start the 11967 .symtab section at the current file position, and write directly 11968 to it. We build the .strtab section in memory. */ 11969 bfd_get_symcount (abfd) = 0; 11970 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 11971 /* sh_name is set in prep_headers. */ 11972 symtab_hdr->sh_type = SHT_SYMTAB; 11973 /* sh_flags, sh_addr and sh_size all start off zero. */ 11974 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 11975 /* sh_link is set in assign_section_numbers. */ 11976 /* sh_info is set below. */ 11977 /* sh_offset is set just below. */ 11978 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 11979 11980 if (max_sym_count < 20) 11981 max_sym_count = 20; 11982 htab->strtabsize = max_sym_count; 11983 amt = max_sym_count * sizeof (struct elf_sym_strtab); 11984 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); 11985 if (htab->strtab == NULL) 11986 goto error_return; 11987 /* The real buffer will be allocated in elf_link_swap_symbols_out. */ 11988 flinfo.symshndxbuf 11989 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) 11990 ? (Elf_External_Sym_Shndx *) -1 : NULL); 11991 11992 if (info->strip != strip_all || emit_relocs) 11993 { 11994 file_ptr off = elf_next_file_pos (abfd); 11995 11996 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); 11997 11998 /* Note that at this point elf_next_file_pos (abfd) is 11999 incorrect. We do not yet know the size of the .symtab section. 12000 We correct next_file_pos below, after we do know the size. */ 12001 12002 /* Start writing out the symbol table. The first symbol is always a 12003 dummy symbol. */ 12004 elfsym.st_value = 0; 12005 elfsym.st_size = 0; 12006 elfsym.st_info = 0; 12007 elfsym.st_other = 0; 12008 elfsym.st_shndx = SHN_UNDEF; 12009 elfsym.st_target_internal = 0; 12010 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, 12011 bfd_und_section_ptr, NULL) != 1) 12012 goto error_return; 12013 12014 /* Output a symbol for each section. We output these even if we are 12015 discarding local symbols, since they are used for relocs. These 12016 symbols have no names. We store the index of each one in the 12017 index field of the section, so that we can find it again when 12018 outputting relocs. */ 12019 12020 elfsym.st_size = 0; 12021 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 12022 elfsym.st_other = 0; 12023 elfsym.st_value = 0; 12024 elfsym.st_target_internal = 0; 12025 for (i = 1; i < elf_numsections (abfd); i++) 12026 { 12027 o = bfd_section_from_elf_index (abfd, i); 12028 if (o != NULL) 12029 { 12030 o->target_index = bfd_get_symcount (abfd); 12031 elfsym.st_shndx = i; 12032 if (!bfd_link_relocatable (info)) 12033 elfsym.st_value = o->vma; 12034 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, 12035 NULL) != 1) 12036 goto error_return; 12037 } 12038 } 12039 } 12040 12041 /* Allocate some memory to hold information read in from the input 12042 files. */ 12043 if (max_contents_size != 0) 12044 { 12045 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); 12046 if (flinfo.contents == NULL) 12047 goto error_return; 12048 } 12049 12050 if (max_external_reloc_size != 0) 12051 { 12052 flinfo.external_relocs = bfd_malloc (max_external_reloc_size); 12053 if (flinfo.external_relocs == NULL) 12054 goto error_return; 12055 } 12056 12057 if (max_internal_reloc_count != 0) 12058 { 12059 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); 12060 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); 12061 if (flinfo.internal_relocs == NULL) 12062 goto error_return; 12063 } 12064 12065 if (max_sym_count != 0) 12066 { 12067 amt = max_sym_count * bed->s->sizeof_sym; 12068 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); 12069 if (flinfo.external_syms == NULL) 12070 goto error_return; 12071 12072 amt = max_sym_count * sizeof (Elf_Internal_Sym); 12073 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); 12074 if (flinfo.internal_syms == NULL) 12075 goto error_return; 12076 12077 amt = max_sym_count * sizeof (long); 12078 flinfo.indices = (long int *) bfd_malloc (amt); 12079 if (flinfo.indices == NULL) 12080 goto error_return; 12081 12082 amt = max_sym_count * sizeof (asection *); 12083 flinfo.sections = (asection **) bfd_malloc (amt); 12084 if (flinfo.sections == NULL) 12085 goto error_return; 12086 } 12087 12088 if (max_sym_shndx_count != 0) 12089 { 12090 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); 12091 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 12092 if (flinfo.locsym_shndx == NULL) 12093 goto error_return; 12094 } 12095 12096 if (htab->tls_sec) 12097 { 12098 bfd_vma base, end = 0; 12099 asection *sec; 12100 12101 for (sec = htab->tls_sec; 12102 sec && (sec->flags & SEC_THREAD_LOCAL); 12103 sec = sec->next) 12104 { 12105 bfd_size_type size = sec->size; 12106 12107 if (size == 0 12108 && (sec->flags & SEC_HAS_CONTENTS) == 0) 12109 { 12110 struct bfd_link_order *ord = sec->map_tail.link_order; 12111 12112 if (ord != NULL) 12113 size = ord->offset + ord->size; 12114 } 12115 end = sec->vma + size; 12116 } 12117 base = htab->tls_sec->vma; 12118 /* Only align end of TLS section if static TLS doesn't have special 12119 alignment requirements. */ 12120 if (bed->static_tls_alignment == 1) 12121 end = align_power (end, htab->tls_sec->alignment_power); 12122 htab->tls_size = end - base; 12123 } 12124 12125 /* Reorder SHF_LINK_ORDER sections. */ 12126 for (o = abfd->sections; o != NULL; o = o->next) 12127 { 12128 if (!elf_fixup_link_order (abfd, o)) 12129 return FALSE; 12130 } 12131 12132 if (!_bfd_elf_fixup_eh_frame_hdr (info)) 12133 return FALSE; 12134 12135 /* Since ELF permits relocations to be against local symbols, we 12136 must have the local symbols available when we do the relocations. 12137 Since we would rather only read the local symbols once, and we 12138 would rather not keep them in memory, we handle all the 12139 relocations for a single input file at the same time. 12140 12141 Unfortunately, there is no way to know the total number of local 12142 symbols until we have seen all of them, and the local symbol 12143 indices precede the global symbol indices. This means that when 12144 we are generating relocatable output, and we see a reloc against 12145 a global symbol, we can not know the symbol index until we have 12146 finished examining all the local symbols to see which ones we are 12147 going to output. To deal with this, we keep the relocations in 12148 memory, and don't output them until the end of the link. This is 12149 an unfortunate waste of memory, but I don't see a good way around 12150 it. Fortunately, it only happens when performing a relocatable 12151 link, which is not the common case. FIXME: If keep_memory is set 12152 we could write the relocs out and then read them again; I don't 12153 know how bad the memory loss will be. */ 12154 12155 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 12156 sub->output_has_begun = FALSE; 12157 for (o = abfd->sections; o != NULL; o = o->next) 12158 { 12159 for (p = o->map_head.link_order; p != NULL; p = p->next) 12160 { 12161 if (p->type == bfd_indirect_link_order 12162 && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) 12163 == bfd_target_elf_flavour) 12164 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) 12165 { 12166 if (! sub->output_has_begun) 12167 { 12168 if (! elf_link_input_bfd (&flinfo, sub)) 12169 goto error_return; 12170 sub->output_has_begun = TRUE; 12171 } 12172 } 12173 else if (p->type == bfd_section_reloc_link_order 12174 || p->type == bfd_symbol_reloc_link_order) 12175 { 12176 if (! elf_reloc_link_order (abfd, info, o, p)) 12177 goto error_return; 12178 } 12179 else 12180 { 12181 if (! _bfd_default_link_order (abfd, info, o, p)) 12182 { 12183 if (p->type == bfd_indirect_link_order 12184 && (bfd_get_flavour (sub) 12185 == bfd_target_elf_flavour) 12186 && (elf_elfheader (sub)->e_ident[EI_CLASS] 12187 != bed->s->elfclass)) 12188 { 12189 const char *iclass, *oclass; 12190 12191 switch (bed->s->elfclass) 12192 { 12193 case ELFCLASS64: oclass = "ELFCLASS64"; break; 12194 case ELFCLASS32: oclass = "ELFCLASS32"; break; 12195 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; 12196 default: abort (); 12197 } 12198 12199 switch (elf_elfheader (sub)->e_ident[EI_CLASS]) 12200 { 12201 case ELFCLASS64: iclass = "ELFCLASS64"; break; 12202 case ELFCLASS32: iclass = "ELFCLASS32"; break; 12203 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; 12204 default: abort (); 12205 } 12206 12207 bfd_set_error (bfd_error_wrong_format); 12208 _bfd_error_handler 12209 /* xgettext:c-format */ 12210 (_("%pB: file class %s incompatible with %s"), 12211 sub, iclass, oclass); 12212 } 12213 12214 goto error_return; 12215 } 12216 } 12217 } 12218 } 12219 12220 /* Free symbol buffer if needed. */ 12221 if (!info->reduce_memory_overheads) 12222 { 12223 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 12224 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 12225 && elf_tdata (sub)->symbuf) 12226 { 12227 free (elf_tdata (sub)->symbuf); 12228 elf_tdata (sub)->symbuf = NULL; 12229 } 12230 } 12231 12232 /* Output any global symbols that got converted to local in a 12233 version script or due to symbol visibility. We do this in a 12234 separate step since ELF requires all local symbols to appear 12235 prior to any global symbols. FIXME: We should only do this if 12236 some global symbols were, in fact, converted to become local. 12237 FIXME: Will this work correctly with the Irix 5 linker? */ 12238 eoinfo.failed = FALSE; 12239 eoinfo.flinfo = &flinfo; 12240 eoinfo.localsyms = TRUE; 12241 eoinfo.file_sym_done = FALSE; 12242 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 12243 if (eoinfo.failed) 12244 return FALSE; 12245 12246 /* If backend needs to output some local symbols not present in the hash 12247 table, do it now. */ 12248 if (bed->elf_backend_output_arch_local_syms 12249 && (info->strip != strip_all || emit_relocs)) 12250 { 12251 typedef int (*out_sym_func) 12252 (void *, const char *, Elf_Internal_Sym *, asection *, 12253 struct elf_link_hash_entry *); 12254 12255 if (! ((*bed->elf_backend_output_arch_local_syms) 12256 (abfd, info, &flinfo, 12257 (out_sym_func) elf_link_output_symstrtab))) 12258 return FALSE; 12259 } 12260 12261 /* That wrote out all the local symbols. Finish up the symbol table 12262 with the global symbols. Even if we want to strip everything we 12263 can, we still need to deal with those global symbols that got 12264 converted to local in a version script. */ 12265 12266 /* The sh_info field records the index of the first non local symbol. */ 12267 symtab_hdr->sh_info = bfd_get_symcount (abfd); 12268 12269 if (dynamic 12270 && htab->dynsym != NULL 12271 && htab->dynsym->output_section != bfd_abs_section_ptr) 12272 { 12273 Elf_Internal_Sym sym; 12274 bfd_byte *dynsym = htab->dynsym->contents; 12275 12276 o = htab->dynsym->output_section; 12277 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; 12278 12279 /* Write out the section symbols for the output sections. */ 12280 if (bfd_link_pic (info) 12281 || htab->is_relocatable_executable) 12282 { 12283 asection *s; 12284 12285 sym.st_size = 0; 12286 sym.st_name = 0; 12287 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 12288 sym.st_other = 0; 12289 sym.st_target_internal = 0; 12290 12291 for (s = abfd->sections; s != NULL; s = s->next) 12292 { 12293 int indx; 12294 bfd_byte *dest; 12295 long dynindx; 12296 12297 dynindx = elf_section_data (s)->dynindx; 12298 if (dynindx <= 0) 12299 continue; 12300 indx = elf_section_data (s)->this_idx; 12301 BFD_ASSERT (indx > 0); 12302 sym.st_shndx = indx; 12303 if (! check_dynsym (abfd, &sym)) 12304 return FALSE; 12305 sym.st_value = s->vma; 12306 dest = dynsym + dynindx * bed->s->sizeof_sym; 12307 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 12308 } 12309 } 12310 12311 /* Write out the local dynsyms. */ 12312 if (htab->dynlocal) 12313 { 12314 struct elf_link_local_dynamic_entry *e; 12315 for (e = htab->dynlocal; e ; e = e->next) 12316 { 12317 asection *s; 12318 bfd_byte *dest; 12319 12320 /* Copy the internal symbol and turn off visibility. 12321 Note that we saved a word of storage and overwrote 12322 the original st_name with the dynstr_index. */ 12323 sym = e->isym; 12324 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 12325 12326 s = bfd_section_from_elf_index (e->input_bfd, 12327 e->isym.st_shndx); 12328 if (s != NULL) 12329 { 12330 sym.st_shndx = 12331 elf_section_data (s->output_section)->this_idx; 12332 if (! check_dynsym (abfd, &sym)) 12333 return FALSE; 12334 sym.st_value = (s->output_section->vma 12335 + s->output_offset 12336 + e->isym.st_value); 12337 } 12338 12339 dest = dynsym + e->dynindx * bed->s->sizeof_sym; 12340 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 12341 } 12342 } 12343 } 12344 12345 /* We get the global symbols from the hash table. */ 12346 eoinfo.failed = FALSE; 12347 eoinfo.localsyms = FALSE; 12348 eoinfo.flinfo = &flinfo; 12349 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 12350 if (eoinfo.failed) 12351 return FALSE; 12352 12353 /* If backend needs to output some symbols not present in the hash 12354 table, do it now. */ 12355 if (bed->elf_backend_output_arch_syms 12356 && (info->strip != strip_all || emit_relocs)) 12357 { 12358 typedef int (*out_sym_func) 12359 (void *, const char *, Elf_Internal_Sym *, asection *, 12360 struct elf_link_hash_entry *); 12361 12362 if (! ((*bed->elf_backend_output_arch_syms) 12363 (abfd, info, &flinfo, 12364 (out_sym_func) elf_link_output_symstrtab))) 12365 return FALSE; 12366 } 12367 12368 /* Finalize the .strtab section. */ 12369 _bfd_elf_strtab_finalize (flinfo.symstrtab); 12370 12371 /* Swap out the .strtab section. */ 12372 if (!elf_link_swap_symbols_out (&flinfo)) 12373 return FALSE; 12374 12375 /* Now we know the size of the symtab section. */ 12376 if (bfd_get_symcount (abfd) > 0) 12377 { 12378 /* Finish up and write out the symbol string table (.strtab) 12379 section. */ 12380 Elf_Internal_Shdr *symstrtab_hdr = NULL; 12381 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; 12382 12383 if (elf_symtab_shndx_list (abfd)) 12384 { 12385 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 12386 12387 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) 12388 { 12389 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 12390 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 12391 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 12392 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); 12393 symtab_shndx_hdr->sh_size = amt; 12394 12395 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, 12396 off, TRUE); 12397 12398 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 12399 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) 12400 return FALSE; 12401 } 12402 } 12403 12404 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 12405 /* sh_name was set in prep_headers. */ 12406 symstrtab_hdr->sh_type = SHT_STRTAB; 12407 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 12408 symstrtab_hdr->sh_addr = 0; 12409 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); 12410 symstrtab_hdr->sh_entsize = 0; 12411 symstrtab_hdr->sh_link = 0; 12412 symstrtab_hdr->sh_info = 0; 12413 /* sh_offset is set just below. */ 12414 symstrtab_hdr->sh_addralign = 1; 12415 12416 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, 12417 off, TRUE); 12418 elf_next_file_pos (abfd) = off; 12419 12420 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 12421 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) 12422 return FALSE; 12423 } 12424 12425 if (info->out_implib_bfd && !elf_output_implib (abfd, info)) 12426 { 12427 _bfd_error_handler (_("%pB: failed to generate import library"), 12428 info->out_implib_bfd); 12429 return FALSE; 12430 } 12431 12432 /* Adjust the relocs to have the correct symbol indices. */ 12433 for (o = abfd->sections; o != NULL; o = o->next) 12434 { 12435 struct bfd_elf_section_data *esdo = elf_section_data (o); 12436 bfd_boolean sort; 12437 12438 if ((o->flags & SEC_RELOC) == 0) 12439 continue; 12440 12441 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); 12442 if (esdo->rel.hdr != NULL 12443 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) 12444 return FALSE; 12445 if (esdo->rela.hdr != NULL 12446 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) 12447 return FALSE; 12448 12449 /* Set the reloc_count field to 0 to prevent write_relocs from 12450 trying to swap the relocs out itself. */ 12451 o->reloc_count = 0; 12452 } 12453 12454 if (dynamic && info->combreloc && dynobj != NULL) 12455 relativecount = elf_link_sort_relocs (abfd, info, &reldyn); 12456 12457 /* If we are linking against a dynamic object, or generating a 12458 shared library, finish up the dynamic linking information. */ 12459 if (dynamic) 12460 { 12461 bfd_byte *dyncon, *dynconend; 12462 12463 /* Fix up .dynamic entries. */ 12464 o = bfd_get_linker_section (dynobj, ".dynamic"); 12465 BFD_ASSERT (o != NULL); 12466 12467 dyncon = o->contents; 12468 dynconend = o->contents + o->size; 12469 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 12470 { 12471 Elf_Internal_Dyn dyn; 12472 const char *name; 12473 unsigned int type; 12474 bfd_size_type sh_size; 12475 bfd_vma sh_addr; 12476 12477 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 12478 12479 switch (dyn.d_tag) 12480 { 12481 default: 12482 continue; 12483 case DT_NULL: 12484 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) 12485 { 12486 switch (elf_section_data (reldyn)->this_hdr.sh_type) 12487 { 12488 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; 12489 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; 12490 default: continue; 12491 } 12492 dyn.d_un.d_val = relativecount; 12493 relativecount = 0; 12494 break; 12495 } 12496 continue; 12497 12498 case DT_INIT: 12499 name = info->init_function; 12500 goto get_sym; 12501 case DT_FINI: 12502 name = info->fini_function; 12503 get_sym: 12504 { 12505 struct elf_link_hash_entry *h; 12506 12507 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 12508 if (h != NULL 12509 && (h->root.type == bfd_link_hash_defined 12510 || h->root.type == bfd_link_hash_defweak)) 12511 { 12512 dyn.d_un.d_ptr = h->root.u.def.value; 12513 o = h->root.u.def.section; 12514 if (o->output_section != NULL) 12515 dyn.d_un.d_ptr += (o->output_section->vma 12516 + o->output_offset); 12517 else 12518 { 12519 /* The symbol is imported from another shared 12520 library and does not apply to this one. */ 12521 dyn.d_un.d_ptr = 0; 12522 } 12523 break; 12524 } 12525 } 12526 continue; 12527 12528 case DT_PREINIT_ARRAYSZ: 12529 name = ".preinit_array"; 12530 goto get_out_size; 12531 case DT_INIT_ARRAYSZ: 12532 name = ".init_array"; 12533 goto get_out_size; 12534 case DT_FINI_ARRAYSZ: 12535 name = ".fini_array"; 12536 get_out_size: 12537 o = bfd_get_section_by_name (abfd, name); 12538 if (o == NULL) 12539 { 12540 _bfd_error_handler 12541 (_("could not find section %s"), name); 12542 goto error_return; 12543 } 12544 if (o->size == 0) 12545 _bfd_error_handler 12546 (_("warning: %s section has zero size"), name); 12547 dyn.d_un.d_val = o->size; 12548 break; 12549 12550 case DT_PREINIT_ARRAY: 12551 name = ".preinit_array"; 12552 goto get_out_vma; 12553 case DT_INIT_ARRAY: 12554 name = ".init_array"; 12555 goto get_out_vma; 12556 case DT_FINI_ARRAY: 12557 name = ".fini_array"; 12558 get_out_vma: 12559 o = bfd_get_section_by_name (abfd, name); 12560 goto do_vma; 12561 12562 case DT_HASH: 12563 name = ".hash"; 12564 goto get_vma; 12565 case DT_GNU_HASH: 12566 name = ".gnu.hash"; 12567 goto get_vma; 12568 case DT_STRTAB: 12569 name = ".dynstr"; 12570 goto get_vma; 12571 case DT_SYMTAB: 12572 name = ".dynsym"; 12573 goto get_vma; 12574 case DT_VERDEF: 12575 name = ".gnu.version_d"; 12576 goto get_vma; 12577 case DT_VERNEED: 12578 name = ".gnu.version_r"; 12579 goto get_vma; 12580 case DT_VERSYM: 12581 name = ".gnu.version"; 12582 get_vma: 12583 o = bfd_get_linker_section (dynobj, name); 12584 do_vma: 12585 if (o == NULL || bfd_is_abs_section (o->output_section)) 12586 { 12587 _bfd_error_handler 12588 (_("could not find section %s"), name); 12589 goto error_return; 12590 } 12591 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) 12592 { 12593 _bfd_error_handler 12594 (_("warning: section '%s' is being made into a note"), name); 12595 bfd_set_error (bfd_error_nonrepresentable_section); 12596 goto error_return; 12597 } 12598 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; 12599 break; 12600 12601 case DT_REL: 12602 case DT_RELA: 12603 case DT_RELSZ: 12604 case DT_RELASZ: 12605 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) 12606 type = SHT_REL; 12607 else 12608 type = SHT_RELA; 12609 sh_size = 0; 12610 sh_addr = 0; 12611 for (i = 1; i < elf_numsections (abfd); i++) 12612 { 12613 Elf_Internal_Shdr *hdr; 12614 12615 hdr = elf_elfsections (abfd)[i]; 12616 if (hdr->sh_type == type 12617 && (hdr->sh_flags & SHF_ALLOC) != 0) 12618 { 12619 sh_size += hdr->sh_size; 12620 if (sh_addr == 0 12621 || sh_addr > hdr->sh_addr) 12622 sh_addr = hdr->sh_addr; 12623 } 12624 } 12625 12626 if (bed->dtrel_excludes_plt && htab->srelplt != NULL) 12627 { 12628 /* Don't count procedure linkage table relocs in the 12629 overall reloc count. */ 12630 sh_size -= htab->srelplt->size; 12631 if (sh_size == 0) 12632 /* If the size is zero, make the address zero too. 12633 This is to avoid a glibc bug. If the backend 12634 emits DT_RELA/DT_RELASZ even when DT_RELASZ is 12635 zero, then we'll put DT_RELA at the end of 12636 DT_JMPREL. glibc will interpret the end of 12637 DT_RELA matching the end of DT_JMPREL as the 12638 case where DT_RELA includes DT_JMPREL, and for 12639 LD_BIND_NOW will decide that processing DT_RELA 12640 will process the PLT relocs too. Net result: 12641 No PLT relocs applied. */ 12642 sh_addr = 0; 12643 12644 /* If .rela.plt is the first .rela section, exclude 12645 it from DT_RELA. */ 12646 else if (sh_addr == (htab->srelplt->output_section->vma 12647 + htab->srelplt->output_offset)) 12648 sh_addr += htab->srelplt->size; 12649 } 12650 12651 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) 12652 dyn.d_un.d_val = sh_size; 12653 else 12654 dyn.d_un.d_ptr = sh_addr; 12655 break; 12656 } 12657 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 12658 } 12659 } 12660 12661 /* If we have created any dynamic sections, then output them. */ 12662 if (dynobj != NULL) 12663 { 12664 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) 12665 goto error_return; 12666 12667 /* Check for DT_TEXTREL (late, in case the backend removes it). */ 12668 if (((info->warn_shared_textrel && bfd_link_pic (info)) 12669 || info->error_textrel) 12670 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) 12671 { 12672 bfd_byte *dyncon, *dynconend; 12673 12674 dyncon = o->contents; 12675 dynconend = o->contents + o->size; 12676 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 12677 { 12678 Elf_Internal_Dyn dyn; 12679 12680 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 12681 12682 if (dyn.d_tag == DT_TEXTREL) 12683 { 12684 if (info->error_textrel) 12685 info->callbacks->einfo 12686 (_("%P%X: read-only segment has dynamic relocations\n")); 12687 else 12688 info->callbacks->einfo 12689 (_("%P: warning: creating a DT_TEXTREL in a shared object\n")); 12690 break; 12691 } 12692 } 12693 } 12694 12695 for (o = dynobj->sections; o != NULL; o = o->next) 12696 { 12697 if ((o->flags & SEC_HAS_CONTENTS) == 0 12698 || o->size == 0 12699 || o->output_section == bfd_abs_section_ptr) 12700 continue; 12701 if ((o->flags & SEC_LINKER_CREATED) == 0) 12702 { 12703 /* At this point, we are only interested in sections 12704 created by _bfd_elf_link_create_dynamic_sections. */ 12705 continue; 12706 } 12707 if (htab->stab_info.stabstr == o) 12708 continue; 12709 if (htab->eh_info.hdr_sec == o) 12710 continue; 12711 if (strcmp (o->name, ".dynstr") != 0) 12712 { 12713 if (! bfd_set_section_contents (abfd, o->output_section, 12714 o->contents, 12715 (file_ptr) o->output_offset 12716 * bfd_octets_per_byte (abfd), 12717 o->size)) 12718 goto error_return; 12719 } 12720 else 12721 { 12722 /* The contents of the .dynstr section are actually in a 12723 stringtab. */ 12724 file_ptr off; 12725 12726 off = elf_section_data (o->output_section)->this_hdr.sh_offset; 12727 if (bfd_seek (abfd, off, SEEK_SET) != 0 12728 || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) 12729 goto error_return; 12730 } 12731 } 12732 } 12733 12734 if (!info->resolve_section_groups) 12735 { 12736 bfd_boolean failed = FALSE; 12737 12738 BFD_ASSERT (bfd_link_relocatable (info)); 12739 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 12740 if (failed) 12741 goto error_return; 12742 } 12743 12744 /* If we have optimized stabs strings, output them. */ 12745 if (htab->stab_info.stabstr != NULL) 12746 { 12747 if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) 12748 goto error_return; 12749 } 12750 12751 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) 12752 goto error_return; 12753 12754 elf_final_link_free (abfd, &flinfo); 12755 12756 elf_linker (abfd) = TRUE; 12757 12758 if (attr_section) 12759 { 12760 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); 12761 if (contents == NULL) 12762 return FALSE; /* Bail out and fail. */ 12763 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); 12764 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); 12765 free (contents); 12766 } 12767 12768 return TRUE; 12769 12770 error_return: 12771 elf_final_link_free (abfd, &flinfo); 12772 return FALSE; 12773 } 12774 12775 /* Initialize COOKIE for input bfd ABFD. */ 12776 12777 static bfd_boolean 12778 init_reloc_cookie (struct elf_reloc_cookie *cookie, 12779 struct bfd_link_info *info, bfd *abfd) 12780 { 12781 Elf_Internal_Shdr *symtab_hdr; 12782 const struct elf_backend_data *bed; 12783 12784 bed = get_elf_backend_data (abfd); 12785 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12786 12787 cookie->abfd = abfd; 12788 cookie->sym_hashes = elf_sym_hashes (abfd); 12789 cookie->bad_symtab = elf_bad_symtab (abfd); 12790 if (cookie->bad_symtab) 12791 { 12792 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 12793 cookie->extsymoff = 0; 12794 } 12795 else 12796 { 12797 cookie->locsymcount = symtab_hdr->sh_info; 12798 cookie->extsymoff = symtab_hdr->sh_info; 12799 } 12800 12801 if (bed->s->arch_size == 32) 12802 cookie->r_sym_shift = 8; 12803 else 12804 cookie->r_sym_shift = 32; 12805 12806 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; 12807 if (cookie->locsyms == NULL && cookie->locsymcount != 0) 12808 { 12809 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, 12810 cookie->locsymcount, 0, 12811 NULL, NULL, NULL); 12812 if (cookie->locsyms == NULL) 12813 { 12814 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); 12815 return FALSE; 12816 } 12817 if (info->keep_memory) 12818 symtab_hdr->contents = (bfd_byte *) cookie->locsyms; 12819 } 12820 return TRUE; 12821 } 12822 12823 /* Free the memory allocated by init_reloc_cookie, if appropriate. */ 12824 12825 static void 12826 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) 12827 { 12828 Elf_Internal_Shdr *symtab_hdr; 12829 12830 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12831 if (cookie->locsyms != NULL 12832 && symtab_hdr->contents != (unsigned char *) cookie->locsyms) 12833 free (cookie->locsyms); 12834 } 12835 12836 /* Initialize the relocation information in COOKIE for input section SEC 12837 of input bfd ABFD. */ 12838 12839 static bfd_boolean 12840 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 12841 struct bfd_link_info *info, bfd *abfd, 12842 asection *sec) 12843 { 12844 if (sec->reloc_count == 0) 12845 { 12846 cookie->rels = NULL; 12847 cookie->relend = NULL; 12848 } 12849 else 12850 { 12851 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 12852 info->keep_memory); 12853 if (cookie->rels == NULL) 12854 return FALSE; 12855 cookie->rel = cookie->rels; 12856 cookie->relend = cookie->rels + sec->reloc_count; 12857 } 12858 cookie->rel = cookie->rels; 12859 return TRUE; 12860 } 12861 12862 /* Free the memory allocated by init_reloc_cookie_rels, 12863 if appropriate. */ 12864 12865 static void 12866 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 12867 asection *sec) 12868 { 12869 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) 12870 free (cookie->rels); 12871 } 12872 12873 /* Initialize the whole of COOKIE for input section SEC. */ 12874 12875 static bfd_boolean 12876 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 12877 struct bfd_link_info *info, 12878 asection *sec) 12879 { 12880 if (!init_reloc_cookie (cookie, info, sec->owner)) 12881 goto error1; 12882 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) 12883 goto error2; 12884 return TRUE; 12885 12886 error2: 12887 fini_reloc_cookie (cookie, sec->owner); 12888 error1: 12889 return FALSE; 12890 } 12891 12892 /* Free the memory allocated by init_reloc_cookie_for_section, 12893 if appropriate. */ 12894 12895 static void 12896 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 12897 asection *sec) 12898 { 12899 fini_reloc_cookie_rels (cookie, sec); 12900 fini_reloc_cookie (cookie, sec->owner); 12901 } 12902 12903 /* Garbage collect unused sections. */ 12904 12905 /* Default gc_mark_hook. */ 12906 12907 asection * 12908 _bfd_elf_gc_mark_hook (asection *sec, 12909 struct bfd_link_info *info ATTRIBUTE_UNUSED, 12910 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 12911 struct elf_link_hash_entry *h, 12912 Elf_Internal_Sym *sym) 12913 { 12914 if (h != NULL) 12915 { 12916 switch (h->root.type) 12917 { 12918 case bfd_link_hash_defined: 12919 case bfd_link_hash_defweak: 12920 return h->root.u.def.section; 12921 12922 case bfd_link_hash_common: 12923 return h->root.u.c.p->section; 12924 12925 default: 12926 break; 12927 } 12928 } 12929 else 12930 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 12931 12932 return NULL; 12933 } 12934 12935 /* Return the debug definition section. */ 12936 12937 static asection * 12938 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, 12939 struct bfd_link_info *info ATTRIBUTE_UNUSED, 12940 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 12941 struct elf_link_hash_entry *h, 12942 Elf_Internal_Sym *sym) 12943 { 12944 if (h != NULL) 12945 { 12946 /* Return the global debug definition section. */ 12947 if ((h->root.type == bfd_link_hash_defined 12948 || h->root.type == bfd_link_hash_defweak) 12949 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) 12950 return h->root.u.def.section; 12951 } 12952 else 12953 { 12954 /* Return the local debug definition section. */ 12955 asection *isec = bfd_section_from_elf_index (sec->owner, 12956 sym->st_shndx); 12957 if ((isec->flags & SEC_DEBUGGING) != 0) 12958 return isec; 12959 } 12960 12961 return NULL; 12962 } 12963 12964 /* COOKIE->rel describes a relocation against section SEC, which is 12965 a section we've decided to keep. Return the section that contains 12966 the relocation symbol, or NULL if no section contains it. */ 12967 12968 asection * 12969 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, 12970 elf_gc_mark_hook_fn gc_mark_hook, 12971 struct elf_reloc_cookie *cookie, 12972 bfd_boolean *start_stop) 12973 { 12974 unsigned long r_symndx; 12975 struct elf_link_hash_entry *h; 12976 12977 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; 12978 if (r_symndx == STN_UNDEF) 12979 return NULL; 12980 12981 if (r_symndx >= cookie->locsymcount 12982 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 12983 { 12984 h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; 12985 if (h == NULL) 12986 { 12987 info->callbacks->einfo (_("%F%P: corrupt input: %pB\n"), 12988 sec->owner); 12989 return NULL; 12990 } 12991 while (h->root.type == bfd_link_hash_indirect 12992 || h->root.type == bfd_link_hash_warning) 12993 h = (struct elf_link_hash_entry *) h->root.u.i.link; 12994 h->mark = 1; 12995 /* If this symbol is weak and there is a non-weak definition, we 12996 keep the non-weak definition because many backends put 12997 dynamic reloc info on the non-weak definition for code 12998 handling copy relocs. */ 12999 if (h->is_weakalias) 13000 weakdef (h)->mark = 1; 13001 13002 if (start_stop != NULL) 13003 { 13004 /* To work around a glibc bug, mark XXX input sections 13005 when there is a reference to __start_XXX or __stop_XXX 13006 symbols. */ 13007 if (h->start_stop) 13008 { 13009 asection *s = h->u2.start_stop_section; 13010 *start_stop = !s->gc_mark; 13011 return s; 13012 } 13013 } 13014 13015 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); 13016 } 13017 13018 return (*gc_mark_hook) (sec, info, cookie->rel, NULL, 13019 &cookie->locsyms[r_symndx]); 13020 } 13021 13022 /* COOKIE->rel describes a relocation against section SEC, which is 13023 a section we've decided to keep. Mark the section that contains 13024 the relocation symbol. */ 13025 13026 bfd_boolean 13027 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, 13028 asection *sec, 13029 elf_gc_mark_hook_fn gc_mark_hook, 13030 struct elf_reloc_cookie *cookie) 13031 { 13032 asection *rsec; 13033 bfd_boolean start_stop = FALSE; 13034 13035 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); 13036 while (rsec != NULL) 13037 { 13038 if (!rsec->gc_mark) 13039 { 13040 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour 13041 || (rsec->owner->flags & DYNAMIC) != 0) 13042 rsec->gc_mark = 1; 13043 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) 13044 return FALSE; 13045 } 13046 if (!start_stop) 13047 break; 13048 rsec = bfd_get_next_section_by_name (rsec->owner, rsec); 13049 } 13050 return TRUE; 13051 } 13052 13053 /* The mark phase of garbage collection. For a given section, mark 13054 it and any sections in this section's group, and all the sections 13055 which define symbols to which it refers. */ 13056 13057 bfd_boolean 13058 _bfd_elf_gc_mark (struct bfd_link_info *info, 13059 asection *sec, 13060 elf_gc_mark_hook_fn gc_mark_hook) 13061 { 13062 bfd_boolean ret; 13063 asection *group_sec, *eh_frame; 13064 13065 sec->gc_mark = 1; 13066 13067 /* Mark all the sections in the group. */ 13068 group_sec = elf_section_data (sec)->next_in_group; 13069 if (group_sec && !group_sec->gc_mark) 13070 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) 13071 return FALSE; 13072 13073 /* Look through the section relocs. */ 13074 ret = TRUE; 13075 eh_frame = elf_eh_frame_section (sec->owner); 13076 if ((sec->flags & SEC_RELOC) != 0 13077 && sec->reloc_count > 0 13078 && sec != eh_frame) 13079 { 13080 struct elf_reloc_cookie cookie; 13081 13082 if (!init_reloc_cookie_for_section (&cookie, info, sec)) 13083 ret = FALSE; 13084 else 13085 { 13086 for (; cookie.rel < cookie.relend; cookie.rel++) 13087 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) 13088 { 13089 ret = FALSE; 13090 break; 13091 } 13092 fini_reloc_cookie_for_section (&cookie, sec); 13093 } 13094 } 13095 13096 if (ret && eh_frame && elf_fde_list (sec)) 13097 { 13098 struct elf_reloc_cookie cookie; 13099 13100 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) 13101 ret = FALSE; 13102 else 13103 { 13104 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, 13105 gc_mark_hook, &cookie)) 13106 ret = FALSE; 13107 fini_reloc_cookie_for_section (&cookie, eh_frame); 13108 } 13109 } 13110 13111 eh_frame = elf_section_eh_frame_entry (sec); 13112 if (ret && eh_frame && !eh_frame->gc_mark) 13113 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) 13114 ret = FALSE; 13115 13116 return ret; 13117 } 13118 13119 /* Scan and mark sections in a special or debug section group. */ 13120 13121 static void 13122 _bfd_elf_gc_mark_debug_special_section_group (asection *grp) 13123 { 13124 /* Point to first section of section group. */ 13125 asection *ssec; 13126 /* Used to iterate the section group. */ 13127 asection *msec; 13128 13129 bfd_boolean is_special_grp = TRUE; 13130 bfd_boolean is_debug_grp = TRUE; 13131 13132 /* First scan to see if group contains any section other than debug 13133 and special section. */ 13134 ssec = msec = elf_next_in_group (grp); 13135 do 13136 { 13137 if ((msec->flags & SEC_DEBUGGING) == 0) 13138 is_debug_grp = FALSE; 13139 13140 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) 13141 is_special_grp = FALSE; 13142 13143 msec = elf_next_in_group (msec); 13144 } 13145 while (msec != ssec); 13146 13147 /* If this is a pure debug section group or pure special section group, 13148 keep all sections in this group. */ 13149 if (is_debug_grp || is_special_grp) 13150 { 13151 do 13152 { 13153 msec->gc_mark = 1; 13154 msec = elf_next_in_group (msec); 13155 } 13156 while (msec != ssec); 13157 } 13158 } 13159 13160 /* Keep debug and special sections. */ 13161 13162 bfd_boolean 13163 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, 13164 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) 13165 { 13166 bfd *ibfd; 13167 13168 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 13169 { 13170 asection *isec; 13171 bfd_boolean some_kept; 13172 bfd_boolean debug_frag_seen; 13173 bfd_boolean has_kept_debug_info; 13174 13175 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 13176 continue; 13177 isec = ibfd->sections; 13178 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13179 continue; 13180 13181 /* Ensure all linker created sections are kept, 13182 see if any other section is already marked, 13183 and note if we have any fragmented debug sections. */ 13184 debug_frag_seen = some_kept = has_kept_debug_info = FALSE; 13185 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13186 { 13187 if ((isec->flags & SEC_LINKER_CREATED) != 0) 13188 isec->gc_mark = 1; 13189 else if (isec->gc_mark 13190 && (isec->flags & SEC_ALLOC) != 0 13191 && elf_section_type (isec) != SHT_NOTE) 13192 some_kept = TRUE; 13193 13194 if (!debug_frag_seen 13195 && (isec->flags & SEC_DEBUGGING) 13196 && CONST_STRNEQ (isec->name, ".debug_line.")) 13197 debug_frag_seen = TRUE; 13198 } 13199 13200 /* If no non-note alloc section in this file will be kept, then 13201 we can toss out the debug and special sections. */ 13202 if (!some_kept) 13203 continue; 13204 13205 /* Keep debug and special sections like .comment when they are 13206 not part of a group. Also keep section groups that contain 13207 just debug sections or special sections. */ 13208 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13209 { 13210 if ((isec->flags & SEC_GROUP) != 0) 13211 _bfd_elf_gc_mark_debug_special_section_group (isec); 13212 else if (((isec->flags & SEC_DEBUGGING) != 0 13213 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 13214 && elf_next_in_group (isec) == NULL) 13215 isec->gc_mark = 1; 13216 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) 13217 has_kept_debug_info = TRUE; 13218 } 13219 13220 /* Look for CODE sections which are going to be discarded, 13221 and find and discard any fragmented debug sections which 13222 are associated with that code section. */ 13223 if (debug_frag_seen) 13224 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13225 if ((isec->flags & SEC_CODE) != 0 13226 && isec->gc_mark == 0) 13227 { 13228 unsigned int ilen; 13229 asection *dsec; 13230 13231 ilen = strlen (isec->name); 13232 13233 /* Association is determined by the name of the debug 13234 section containing the name of the code section as 13235 a suffix. For example .debug_line.text.foo is a 13236 debug section associated with .text.foo. */ 13237 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) 13238 { 13239 unsigned int dlen; 13240 13241 if (dsec->gc_mark == 0 13242 || (dsec->flags & SEC_DEBUGGING) == 0) 13243 continue; 13244 13245 dlen = strlen (dsec->name); 13246 13247 if (dlen > ilen 13248 && strncmp (dsec->name + (dlen - ilen), 13249 isec->name, ilen) == 0) 13250 dsec->gc_mark = 0; 13251 } 13252 } 13253 13254 /* Mark debug sections referenced by kept debug sections. */ 13255 if (has_kept_debug_info) 13256 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13257 if (isec->gc_mark 13258 && (isec->flags & SEC_DEBUGGING) != 0) 13259 if (!_bfd_elf_gc_mark (info, isec, 13260 elf_gc_mark_debug_section)) 13261 return FALSE; 13262 } 13263 return TRUE; 13264 } 13265 13266 static bfd_boolean 13267 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) 13268 { 13269 bfd *sub; 13270 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13271 13272 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13273 { 13274 asection *o; 13275 13276 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 13277 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) 13278 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 13279 continue; 13280 o = sub->sections; 13281 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13282 continue; 13283 13284 for (o = sub->sections; o != NULL; o = o->next) 13285 { 13286 /* When any section in a section group is kept, we keep all 13287 sections in the section group. If the first member of 13288 the section group is excluded, we will also exclude the 13289 group section. */ 13290 if (o->flags & SEC_GROUP) 13291 { 13292 asection *first = elf_next_in_group (o); 13293 o->gc_mark = first->gc_mark; 13294 } 13295 13296 if (o->gc_mark) 13297 continue; 13298 13299 /* Skip sweeping sections already excluded. */ 13300 if (o->flags & SEC_EXCLUDE) 13301 continue; 13302 13303 /* Since this is early in the link process, it is simple 13304 to remove a section from the output. */ 13305 o->flags |= SEC_EXCLUDE; 13306 13307 if (info->print_gc_sections && o->size != 0) 13308 /* xgettext:c-format */ 13309 _bfd_error_handler (_("removing unused section '%pA' in file '%pB'"), 13310 o, sub); 13311 } 13312 } 13313 13314 return TRUE; 13315 } 13316 13317 /* Propagate collected vtable information. This is called through 13318 elf_link_hash_traverse. */ 13319 13320 static bfd_boolean 13321 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) 13322 { 13323 /* Those that are not vtables. */ 13324 if (h->start_stop 13325 || h->u2.vtable == NULL 13326 || h->u2.vtable->parent == NULL) 13327 return TRUE; 13328 13329 /* Those vtables that do not have parents, we cannot merge. */ 13330 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) 13331 return TRUE; 13332 13333 /* If we've already been done, exit. */ 13334 if (h->u2.vtable->used && h->u2.vtable->used[-1]) 13335 return TRUE; 13336 13337 /* Make sure the parent's table is up to date. */ 13338 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); 13339 13340 if (h->u2.vtable->used == NULL) 13341 { 13342 /* None of this table's entries were referenced. Re-use the 13343 parent's table. */ 13344 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; 13345 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; 13346 } 13347 else 13348 { 13349 size_t n; 13350 bfd_boolean *cu, *pu; 13351 13352 /* Or the parent's entries into ours. */ 13353 cu = h->u2.vtable->used; 13354 cu[-1] = TRUE; 13355 pu = h->u2.vtable->parent->u2.vtable->used; 13356 if (pu != NULL) 13357 { 13358 const struct elf_backend_data *bed; 13359 unsigned int log_file_align; 13360 13361 bed = get_elf_backend_data (h->root.u.def.section->owner); 13362 log_file_align = bed->s->log_file_align; 13363 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; 13364 while (n--) 13365 { 13366 if (*pu) 13367 *cu = TRUE; 13368 pu++; 13369 cu++; 13370 } 13371 } 13372 } 13373 13374 return TRUE; 13375 } 13376 13377 static bfd_boolean 13378 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) 13379 { 13380 asection *sec; 13381 bfd_vma hstart, hend; 13382 Elf_Internal_Rela *relstart, *relend, *rel; 13383 const struct elf_backend_data *bed; 13384 unsigned int log_file_align; 13385 13386 /* Take care of both those symbols that do not describe vtables as 13387 well as those that are not loaded. */ 13388 if (h->start_stop 13389 || h->u2.vtable == NULL 13390 || h->u2.vtable->parent == NULL) 13391 return TRUE; 13392 13393 BFD_ASSERT (h->root.type == bfd_link_hash_defined 13394 || h->root.type == bfd_link_hash_defweak); 13395 13396 sec = h->root.u.def.section; 13397 hstart = h->root.u.def.value; 13398 hend = hstart + h->size; 13399 13400 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); 13401 if (!relstart) 13402 return *(bfd_boolean *) okp = FALSE; 13403 bed = get_elf_backend_data (sec->owner); 13404 log_file_align = bed->s->log_file_align; 13405 13406 relend = relstart + sec->reloc_count; 13407 13408 for (rel = relstart; rel < relend; ++rel) 13409 if (rel->r_offset >= hstart && rel->r_offset < hend) 13410 { 13411 /* If the entry is in use, do nothing. */ 13412 if (h->u2.vtable->used 13413 && (rel->r_offset - hstart) < h->u2.vtable->size) 13414 { 13415 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; 13416 if (h->u2.vtable->used[entry]) 13417 continue; 13418 } 13419 /* Otherwise, kill it. */ 13420 rel->r_offset = rel->r_info = rel->r_addend = 0; 13421 } 13422 13423 return TRUE; 13424 } 13425 13426 /* Mark sections containing dynamically referenced symbols. When 13427 building shared libraries, we must assume that any visible symbol is 13428 referenced. */ 13429 13430 bfd_boolean 13431 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) 13432 { 13433 struct bfd_link_info *info = (struct bfd_link_info *) inf; 13434 struct bfd_elf_dynamic_list *d = info->dynamic_list; 13435 13436 if ((h->root.type == bfd_link_hash_defined 13437 || h->root.type == bfd_link_hash_defweak) 13438 && ((h->ref_dynamic && !h->forced_local) 13439 || ((h->def_regular || ELF_COMMON_DEF_P (h)) 13440 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 13441 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 13442 && (!bfd_link_executable (info) 13443 || info->gc_keep_exported 13444 || info->export_dynamic 13445 || (h->dynamic 13446 && d != NULL 13447 && (*d->match) (&d->head, NULL, h->root.root.string))) 13448 && (h->versioned >= versioned 13449 || !bfd_hide_sym_by_version (info->version_info, 13450 h->root.root.string))))) 13451 h->root.u.def.section->flags |= SEC_KEEP; 13452 13453 return TRUE; 13454 } 13455 13456 /* Keep all sections containing symbols undefined on the command-line, 13457 and the section containing the entry symbol. */ 13458 13459 void 13460 _bfd_elf_gc_keep (struct bfd_link_info *info) 13461 { 13462 struct bfd_sym_chain *sym; 13463 13464 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) 13465 { 13466 struct elf_link_hash_entry *h; 13467 13468 h = elf_link_hash_lookup (elf_hash_table (info), sym->name, 13469 FALSE, FALSE, FALSE); 13470 13471 if (h != NULL 13472 && (h->root.type == bfd_link_hash_defined 13473 || h->root.type == bfd_link_hash_defweak) 13474 && !bfd_is_abs_section (h->root.u.def.section) 13475 && !bfd_is_und_section (h->root.u.def.section)) 13476 h->root.u.def.section->flags |= SEC_KEEP; 13477 } 13478 } 13479 13480 bfd_boolean 13481 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, 13482 struct bfd_link_info *info) 13483 { 13484 bfd *ibfd = info->input_bfds; 13485 13486 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 13487 { 13488 asection *sec; 13489 struct elf_reloc_cookie cookie; 13490 13491 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 13492 continue; 13493 sec = ibfd->sections; 13494 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13495 continue; 13496 13497 if (!init_reloc_cookie (&cookie, info, ibfd)) 13498 return FALSE; 13499 13500 for (sec = ibfd->sections; sec; sec = sec->next) 13501 { 13502 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") 13503 && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) 13504 { 13505 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); 13506 fini_reloc_cookie_rels (&cookie, sec); 13507 } 13508 } 13509 } 13510 return TRUE; 13511 } 13512 13513 /* Do mark and sweep of unused sections. */ 13514 13515 bfd_boolean 13516 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) 13517 { 13518 bfd_boolean ok = TRUE; 13519 bfd *sub; 13520 elf_gc_mark_hook_fn gc_mark_hook; 13521 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13522 struct elf_link_hash_table *htab; 13523 13524 if (!bed->can_gc_sections 13525 || !is_elf_hash_table (info->hash)) 13526 { 13527 _bfd_error_handler(_("warning: gc-sections option ignored")); 13528 return TRUE; 13529 } 13530 13531 bed->gc_keep (info); 13532 htab = elf_hash_table (info); 13533 13534 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section 13535 at the .eh_frame section if we can mark the FDEs individually. */ 13536 for (sub = info->input_bfds; 13537 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; 13538 sub = sub->link.next) 13539 { 13540 asection *sec; 13541 struct elf_reloc_cookie cookie; 13542 13543 sec = sub->sections; 13544 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13545 continue; 13546 sec = bfd_get_section_by_name (sub, ".eh_frame"); 13547 while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) 13548 { 13549 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); 13550 if (elf_section_data (sec)->sec_info 13551 && (sec->flags & SEC_LINKER_CREATED) == 0) 13552 elf_eh_frame_section (sub) = sec; 13553 fini_reloc_cookie_for_section (&cookie, sec); 13554 sec = bfd_get_next_section_by_name (NULL, sec); 13555 } 13556 } 13557 13558 /* Apply transitive closure to the vtable entry usage info. */ 13559 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); 13560 if (!ok) 13561 return FALSE; 13562 13563 /* Kill the vtable relocations that were not used. */ 13564 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); 13565 if (!ok) 13566 return FALSE; 13567 13568 /* Mark dynamically referenced symbols. */ 13569 if (htab->dynamic_sections_created || info->gc_keep_exported) 13570 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); 13571 13572 /* Grovel through relocs to find out who stays ... */ 13573 gc_mark_hook = bed->gc_mark_hook; 13574 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13575 { 13576 asection *o; 13577 13578 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 13579 || elf_object_id (sub) != elf_hash_table_id (htab) 13580 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 13581 continue; 13582 13583 o = sub->sections; 13584 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13585 continue; 13586 13587 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). 13588 Also treat note sections as a root, if the section is not part 13589 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as 13590 well as FINI_ARRAY sections for ld -r. */ 13591 for (o = sub->sections; o != NULL; o = o->next) 13592 if (!o->gc_mark 13593 && (o->flags & SEC_EXCLUDE) == 0 13594 && ((o->flags & SEC_KEEP) != 0 13595 || (bfd_link_relocatable (info) 13596 && ((elf_section_data (o)->this_hdr.sh_type 13597 == SHT_PREINIT_ARRAY) 13598 || (elf_section_data (o)->this_hdr.sh_type 13599 == SHT_INIT_ARRAY) 13600 || (elf_section_data (o)->this_hdr.sh_type 13601 == SHT_FINI_ARRAY))) 13602 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE 13603 && elf_next_in_group (o) == NULL ))) 13604 { 13605 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 13606 return FALSE; 13607 } 13608 } 13609 13610 /* Allow the backend to mark additional target specific sections. */ 13611 bed->gc_mark_extra_sections (info, gc_mark_hook); 13612 13613 /* ... and mark SEC_EXCLUDE for those that go. */ 13614 return elf_gc_sweep (abfd, info); 13615 } 13616 13617 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ 13618 13619 bfd_boolean 13620 bfd_elf_gc_record_vtinherit (bfd *abfd, 13621 asection *sec, 13622 struct elf_link_hash_entry *h, 13623 bfd_vma offset) 13624 { 13625 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 13626 struct elf_link_hash_entry **search, *child; 13627 size_t extsymcount; 13628 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13629 13630 /* The sh_info field of the symtab header tells us where the 13631 external symbols start. We don't care about the local symbols at 13632 this point. */ 13633 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; 13634 if (!elf_bad_symtab (abfd)) 13635 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; 13636 13637 sym_hashes = elf_sym_hashes (abfd); 13638 sym_hashes_end = sym_hashes + extsymcount; 13639 13640 /* Hunt down the child symbol, which is in this section at the same 13641 offset as the relocation. */ 13642 for (search = sym_hashes; search != sym_hashes_end; ++search) 13643 { 13644 if ((child = *search) != NULL 13645 && (child->root.type == bfd_link_hash_defined 13646 || child->root.type == bfd_link_hash_defweak) 13647 && child->root.u.def.section == sec 13648 && child->root.u.def.value == offset) 13649 goto win; 13650 } 13651 13652 /* xgettext:c-format */ 13653 _bfd_error_handler (_("%pB: %pA+%#" PRIx64 ": no symbol found for INHERIT"), 13654 abfd, sec, (uint64_t) offset); 13655 bfd_set_error (bfd_error_invalid_operation); 13656 return FALSE; 13657 13658 win: 13659 if (!child->u2.vtable) 13660 { 13661 child->u2.vtable = ((struct elf_link_virtual_table_entry *) 13662 bfd_zalloc (abfd, sizeof (*child->u2.vtable))); 13663 if (!child->u2.vtable) 13664 return FALSE; 13665 } 13666 if (!h) 13667 { 13668 /* This *should* only be the absolute section. It could potentially 13669 be that someone has defined a non-global vtable though, which 13670 would be bad. It isn't worth paging in the local symbols to be 13671 sure though; that case should simply be handled by the assembler. */ 13672 13673 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; 13674 } 13675 else 13676 child->u2.vtable->parent = h; 13677 13678 return TRUE; 13679 } 13680 13681 /* Called from check_relocs to record the existence of a VTENTRY reloc. */ 13682 13683 bfd_boolean 13684 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, 13685 asection *sec ATTRIBUTE_UNUSED, 13686 struct elf_link_hash_entry *h, 13687 bfd_vma addend) 13688 { 13689 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13690 unsigned int log_file_align = bed->s->log_file_align; 13691 13692 if (!h->u2.vtable) 13693 { 13694 h->u2.vtable = ((struct elf_link_virtual_table_entry *) 13695 bfd_zalloc (abfd, sizeof (*h->u2.vtable))); 13696 if (!h->u2.vtable) 13697 return FALSE; 13698 } 13699 13700 if (addend >= h->u2.vtable->size) 13701 { 13702 size_t size, bytes, file_align; 13703 bfd_boolean *ptr = h->u2.vtable->used; 13704 13705 /* While the symbol is undefined, we have to be prepared to handle 13706 a zero size. */ 13707 file_align = 1 << log_file_align; 13708 if (h->root.type == bfd_link_hash_undefined) 13709 size = addend + file_align; 13710 else 13711 { 13712 size = h->size; 13713 if (addend >= size) 13714 { 13715 /* Oops! We've got a reference past the defined end of 13716 the table. This is probably a bug -- shall we warn? */ 13717 size = addend + file_align; 13718 } 13719 } 13720 size = (size + file_align - 1) & -file_align; 13721 13722 /* Allocate one extra entry for use as a "done" flag for the 13723 consolidation pass. */ 13724 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); 13725 13726 if (ptr) 13727 { 13728 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); 13729 13730 if (ptr != NULL) 13731 { 13732 size_t oldbytes; 13733 13734 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) 13735 * sizeof (bfd_boolean)); 13736 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); 13737 } 13738 } 13739 else 13740 ptr = (bfd_boolean *) bfd_zmalloc (bytes); 13741 13742 if (ptr == NULL) 13743 return FALSE; 13744 13745 /* And arrange for that done flag to be at index -1. */ 13746 h->u2.vtable->used = ptr + 1; 13747 h->u2.vtable->size = size; 13748 } 13749 13750 h->u2.vtable->used[addend >> log_file_align] = TRUE; 13751 13752 return TRUE; 13753 } 13754 13755 /* Map an ELF section header flag to its corresponding string. */ 13756 typedef struct 13757 { 13758 char *flag_name; 13759 flagword flag_value; 13760 } elf_flags_to_name_table; 13761 13762 static elf_flags_to_name_table elf_flags_to_names [] = 13763 { 13764 { "SHF_WRITE", SHF_WRITE }, 13765 { "SHF_ALLOC", SHF_ALLOC }, 13766 { "SHF_EXECINSTR", SHF_EXECINSTR }, 13767 { "SHF_MERGE", SHF_MERGE }, 13768 { "SHF_STRINGS", SHF_STRINGS }, 13769 { "SHF_INFO_LINK", SHF_INFO_LINK}, 13770 { "SHF_LINK_ORDER", SHF_LINK_ORDER}, 13771 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, 13772 { "SHF_GROUP", SHF_GROUP }, 13773 { "SHF_TLS", SHF_TLS }, 13774 { "SHF_MASKOS", SHF_MASKOS }, 13775 { "SHF_EXCLUDE", SHF_EXCLUDE }, 13776 }; 13777 13778 /* Returns TRUE if the section is to be included, otherwise FALSE. */ 13779 bfd_boolean 13780 bfd_elf_lookup_section_flags (struct bfd_link_info *info, 13781 struct flag_info *flaginfo, 13782 asection *section) 13783 { 13784 const bfd_vma sh_flags = elf_section_flags (section); 13785 13786 if (!flaginfo->flags_initialized) 13787 { 13788 bfd *obfd = info->output_bfd; 13789 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 13790 struct flag_info_list *tf = flaginfo->flag_list; 13791 int with_hex = 0; 13792 int without_hex = 0; 13793 13794 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) 13795 { 13796 unsigned i; 13797 flagword (*lookup) (char *); 13798 13799 lookup = bed->elf_backend_lookup_section_flags_hook; 13800 if (lookup != NULL) 13801 { 13802 flagword hexval = (*lookup) ((char *) tf->name); 13803 13804 if (hexval != 0) 13805 { 13806 if (tf->with == with_flags) 13807 with_hex |= hexval; 13808 else if (tf->with == without_flags) 13809 without_hex |= hexval; 13810 tf->valid = TRUE; 13811 continue; 13812 } 13813 } 13814 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) 13815 { 13816 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) 13817 { 13818 if (tf->with == with_flags) 13819 with_hex |= elf_flags_to_names[i].flag_value; 13820 else if (tf->with == without_flags) 13821 without_hex |= elf_flags_to_names[i].flag_value; 13822 tf->valid = TRUE; 13823 break; 13824 } 13825 } 13826 if (!tf->valid) 13827 { 13828 info->callbacks->einfo 13829 (_("unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); 13830 return FALSE; 13831 } 13832 } 13833 flaginfo->flags_initialized = TRUE; 13834 flaginfo->only_with_flags |= with_hex; 13835 flaginfo->not_with_flags |= without_hex; 13836 } 13837 13838 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) 13839 return FALSE; 13840 13841 if ((flaginfo->not_with_flags & sh_flags) != 0) 13842 return FALSE; 13843 13844 return TRUE; 13845 } 13846 13847 struct alloc_got_off_arg { 13848 bfd_vma gotoff; 13849 struct bfd_link_info *info; 13850 }; 13851 13852 /* We need a special top-level link routine to convert got reference counts 13853 to real got offsets. */ 13854 13855 static bfd_boolean 13856 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) 13857 { 13858 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; 13859 bfd *obfd = gofarg->info->output_bfd; 13860 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 13861 13862 if (h->got.refcount > 0) 13863 { 13864 h->got.offset = gofarg->gotoff; 13865 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); 13866 } 13867 else 13868 h->got.offset = (bfd_vma) -1; 13869 13870 return TRUE; 13871 } 13872 13873 /* And an accompanying bit to work out final got entry offsets once 13874 we're done. Should be called from final_link. */ 13875 13876 bfd_boolean 13877 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, 13878 struct bfd_link_info *info) 13879 { 13880 bfd *i; 13881 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13882 bfd_vma gotoff; 13883 struct alloc_got_off_arg gofarg; 13884 13885 BFD_ASSERT (abfd == info->output_bfd); 13886 13887 if (! is_elf_hash_table (info->hash)) 13888 return FALSE; 13889 13890 /* The GOT offset is relative to the .got section, but the GOT header is 13891 put into the .got.plt section, if the backend uses it. */ 13892 if (bed->want_got_plt) 13893 gotoff = 0; 13894 else 13895 gotoff = bed->got_header_size; 13896 13897 /* Do the local .got entries first. */ 13898 for (i = info->input_bfds; i; i = i->link.next) 13899 { 13900 bfd_signed_vma *local_got; 13901 size_t j, locsymcount; 13902 Elf_Internal_Shdr *symtab_hdr; 13903 13904 if (bfd_get_flavour (i) != bfd_target_elf_flavour) 13905 continue; 13906 13907 local_got = elf_local_got_refcounts (i); 13908 if (!local_got) 13909 continue; 13910 13911 symtab_hdr = &elf_tdata (i)->symtab_hdr; 13912 if (elf_bad_symtab (i)) 13913 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 13914 else 13915 locsymcount = symtab_hdr->sh_info; 13916 13917 for (j = 0; j < locsymcount; ++j) 13918 { 13919 if (local_got[j] > 0) 13920 { 13921 local_got[j] = gotoff; 13922 gotoff += bed->got_elt_size (abfd, info, NULL, i, j); 13923 } 13924 else 13925 local_got[j] = (bfd_vma) -1; 13926 } 13927 } 13928 13929 /* Then the global .got entries. .plt refcounts are handled by 13930 adjust_dynamic_symbol */ 13931 gofarg.gotoff = gotoff; 13932 gofarg.info = info; 13933 elf_link_hash_traverse (elf_hash_table (info), 13934 elf_gc_allocate_got_offsets, 13935 &gofarg); 13936 return TRUE; 13937 } 13938 13939 /* Many folk need no more in the way of final link than this, once 13940 got entry reference counting is enabled. */ 13941 13942 bfd_boolean 13943 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) 13944 { 13945 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) 13946 return FALSE; 13947 13948 /* Invoke the regular ELF backend linker to do all the work. */ 13949 return bfd_elf_final_link (abfd, info); 13950 } 13951 13952 bfd_boolean 13953 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) 13954 { 13955 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; 13956 13957 if (rcookie->bad_symtab) 13958 rcookie->rel = rcookie->rels; 13959 13960 for (; rcookie->rel < rcookie->relend; rcookie->rel++) 13961 { 13962 unsigned long r_symndx; 13963 13964 if (! rcookie->bad_symtab) 13965 if (rcookie->rel->r_offset > offset) 13966 return FALSE; 13967 if (rcookie->rel->r_offset != offset) 13968 continue; 13969 13970 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; 13971 if (r_symndx == STN_UNDEF) 13972 return TRUE; 13973 13974 if (r_symndx >= rcookie->locsymcount 13975 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) 13976 { 13977 struct elf_link_hash_entry *h; 13978 13979 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; 13980 13981 while (h->root.type == bfd_link_hash_indirect 13982 || h->root.type == bfd_link_hash_warning) 13983 h = (struct elf_link_hash_entry *) h->root.u.i.link; 13984 13985 if ((h->root.type == bfd_link_hash_defined 13986 || h->root.type == bfd_link_hash_defweak) 13987 && (h->root.u.def.section->owner != rcookie->abfd 13988 || h->root.u.def.section->kept_section != NULL 13989 || discarded_section (h->root.u.def.section))) 13990 return TRUE; 13991 } 13992 else 13993 { 13994 /* It's not a relocation against a global symbol, 13995 but it could be a relocation against a local 13996 symbol for a discarded section. */ 13997 asection *isec; 13998 Elf_Internal_Sym *isym; 13999 14000 /* Need to: get the symbol; get the section. */ 14001 isym = &rcookie->locsyms[r_symndx]; 14002 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); 14003 if (isec != NULL 14004 && (isec->kept_section != NULL 14005 || discarded_section (isec))) 14006 return TRUE; 14007 } 14008 return FALSE; 14009 } 14010 return FALSE; 14011 } 14012 14013 /* Discard unneeded references to discarded sections. 14014 Returns -1 on error, 1 if any section's size was changed, 0 if 14015 nothing changed. This function assumes that the relocations are in 14016 sorted order, which is true for all known assemblers. */ 14017 14018 int 14019 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) 14020 { 14021 struct elf_reloc_cookie cookie; 14022 asection *o; 14023 bfd *abfd; 14024 int changed = 0; 14025 14026 if (info->traditional_format 14027 || !is_elf_hash_table (info->hash)) 14028 return 0; 14029 14030 o = bfd_get_section_by_name (output_bfd, ".stab"); 14031 if (o != NULL) 14032 { 14033 asection *i; 14034 14035 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 14036 { 14037 if (i->size == 0 14038 || i->reloc_count == 0 14039 || i->sec_info_type != SEC_INFO_TYPE_STABS) 14040 continue; 14041 14042 abfd = i->owner; 14043 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 14044 continue; 14045 14046 if (!init_reloc_cookie_for_section (&cookie, info, i)) 14047 return -1; 14048 14049 if (_bfd_discard_section_stabs (abfd, i, 14050 elf_section_data (i)->sec_info, 14051 bfd_elf_reloc_symbol_deleted_p, 14052 &cookie)) 14053 changed = 1; 14054 14055 fini_reloc_cookie_for_section (&cookie, i); 14056 } 14057 } 14058 14059 o = NULL; 14060 if (info->eh_frame_hdr_type != COMPACT_EH_HDR) 14061 o = bfd_get_section_by_name (output_bfd, ".eh_frame"); 14062 if (o != NULL) 14063 { 14064 asection *i; 14065 int eh_changed = 0; 14066 unsigned int eh_alignment; 14067 14068 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 14069 { 14070 if (i->size == 0) 14071 continue; 14072 14073 abfd = i->owner; 14074 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 14075 continue; 14076 14077 if (!init_reloc_cookie_for_section (&cookie, info, i)) 14078 return -1; 14079 14080 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); 14081 if (_bfd_elf_discard_section_eh_frame (abfd, info, i, 14082 bfd_elf_reloc_symbol_deleted_p, 14083 &cookie)) 14084 { 14085 eh_changed = 1; 14086 if (i->size != i->rawsize) 14087 changed = 1; 14088 } 14089 14090 fini_reloc_cookie_for_section (&cookie, i); 14091 } 14092 14093 eh_alignment = 1 << o->alignment_power; 14094 /* Skip over zero terminator, and prevent empty sections from 14095 adding alignment padding at the end. */ 14096 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) 14097 if (i->size == 0) 14098 i->flags |= SEC_EXCLUDE; 14099 else if (i->size > 4) 14100 break; 14101 /* The last non-empty eh_frame section doesn't need padding. */ 14102 if (i != NULL) 14103 i = i->map_tail.s; 14104 /* Any prior sections must pad the last FDE out to the output 14105 section alignment. Otherwise we might have zero padding 14106 between sections, which would be seen as a terminator. */ 14107 for (; i != NULL; i = i->map_tail.s) 14108 if (i->size == 4) 14109 /* All but the last zero terminator should have been removed. */ 14110 BFD_FAIL (); 14111 else 14112 { 14113 bfd_size_type size 14114 = (i->size + eh_alignment - 1) & -eh_alignment; 14115 if (i->size != size) 14116 { 14117 i->size = size; 14118 changed = 1; 14119 eh_changed = 1; 14120 } 14121 } 14122 if (eh_changed) 14123 elf_link_hash_traverse (elf_hash_table (info), 14124 _bfd_elf_adjust_eh_frame_global_symbol, NULL); 14125 } 14126 14127 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) 14128 { 14129 const struct elf_backend_data *bed; 14130 asection *s; 14131 14132 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 14133 continue; 14134 s = abfd->sections; 14135 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 14136 continue; 14137 14138 bed = get_elf_backend_data (abfd); 14139 14140 if (bed->elf_backend_discard_info != NULL) 14141 { 14142 if (!init_reloc_cookie (&cookie, info, abfd)) 14143 return -1; 14144 14145 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) 14146 changed = 1; 14147 14148 fini_reloc_cookie (&cookie, abfd); 14149 } 14150 } 14151 14152 if (info->eh_frame_hdr_type == COMPACT_EH_HDR) 14153 _bfd_elf_end_eh_frame_parsing (info); 14154 14155 if (info->eh_frame_hdr_type 14156 && !bfd_link_relocatable (info) 14157 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) 14158 changed = 1; 14159 14160 return changed; 14161 } 14162 14163 bfd_boolean 14164 _bfd_elf_section_already_linked (bfd *abfd, 14165 asection *sec, 14166 struct bfd_link_info *info) 14167 { 14168 flagword flags; 14169 const char *name, *key; 14170 struct bfd_section_already_linked *l; 14171 struct bfd_section_already_linked_hash_entry *already_linked_list; 14172 14173 if (sec->output_section == bfd_abs_section_ptr) 14174 return FALSE; 14175 14176 flags = sec->flags; 14177 14178 /* Return if it isn't a linkonce section. A comdat group section 14179 also has SEC_LINK_ONCE set. */ 14180 if ((flags & SEC_LINK_ONCE) == 0) 14181 return FALSE; 14182 14183 /* Don't put group member sections on our list of already linked 14184 sections. They are handled as a group via their group section. */ 14185 if (elf_sec_group (sec) != NULL) 14186 return FALSE; 14187 14188 /* For a SHT_GROUP section, use the group signature as the key. */ 14189 name = sec->name; 14190 if ((flags & SEC_GROUP) != 0 14191 && elf_next_in_group (sec) != NULL 14192 && elf_group_name (elf_next_in_group (sec)) != NULL) 14193 key = elf_group_name (elf_next_in_group (sec)); 14194 else 14195 { 14196 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ 14197 if (CONST_STRNEQ (name, ".gnu.linkonce.") 14198 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 14199 key++; 14200 else 14201 /* Must be a user linkonce section that doesn't follow gcc's 14202 naming convention. In this case we won't be matching 14203 single member groups. */ 14204 key = name; 14205 } 14206 14207 already_linked_list = bfd_section_already_linked_table_lookup (key); 14208 14209 for (l = already_linked_list->entry; l != NULL; l = l->next) 14210 { 14211 /* We may have 2 different types of sections on the list: group 14212 sections with a signature of <key> (<key> is some string), 14213 and linkonce sections named .gnu.linkonce.<type>.<key>. 14214 Match like sections. LTO plugin sections are an exception. 14215 They are always named .gnu.linkonce.t.<key> and match either 14216 type of section. */ 14217 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) 14218 && ((flags & SEC_GROUP) != 0 14219 || strcmp (name, l->sec->name) == 0)) 14220 || (l->sec->owner->flags & BFD_PLUGIN) != 0) 14221 { 14222 /* The section has already been linked. See if we should 14223 issue a warning. */ 14224 if (!_bfd_handle_already_linked (sec, l, info)) 14225 return FALSE; 14226 14227 if (flags & SEC_GROUP) 14228 { 14229 asection *first = elf_next_in_group (sec); 14230 asection *s = first; 14231 14232 while (s != NULL) 14233 { 14234 s->output_section = bfd_abs_section_ptr; 14235 /* Record which group discards it. */ 14236 s->kept_section = l->sec; 14237 s = elf_next_in_group (s); 14238 /* These lists are circular. */ 14239 if (s == first) 14240 break; 14241 } 14242 } 14243 14244 return TRUE; 14245 } 14246 } 14247 14248 /* A single member comdat group section may be discarded by a 14249 linkonce section and vice versa. */ 14250 if ((flags & SEC_GROUP) != 0) 14251 { 14252 asection *first = elf_next_in_group (sec); 14253 14254 if (first != NULL && elf_next_in_group (first) == first) 14255 /* Check this single member group against linkonce sections. */ 14256 for (l = already_linked_list->entry; l != NULL; l = l->next) 14257 if ((l->sec->flags & SEC_GROUP) == 0 14258 && bfd_elf_match_symbols_in_sections (l->sec, first, info)) 14259 { 14260 first->output_section = bfd_abs_section_ptr; 14261 first->kept_section = l->sec; 14262 sec->output_section = bfd_abs_section_ptr; 14263 break; 14264 } 14265 } 14266 else 14267 /* Check this linkonce section against single member groups. */ 14268 for (l = already_linked_list->entry; l != NULL; l = l->next) 14269 if (l->sec->flags & SEC_GROUP) 14270 { 14271 asection *first = elf_next_in_group (l->sec); 14272 14273 if (first != NULL 14274 && elf_next_in_group (first) == first 14275 && bfd_elf_match_symbols_in_sections (first, sec, info)) 14276 { 14277 sec->output_section = bfd_abs_section_ptr; 14278 sec->kept_section = first; 14279 break; 14280 } 14281 } 14282 14283 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' 14284 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 14285 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' 14286 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its 14287 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded 14288 but its `.gnu.linkonce.t.F' is discarded means we chose one-only 14289 `.gnu.linkonce.t.F' section from a different bfd not requiring any 14290 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. 14291 The reverse order cannot happen as there is never a bfd with only the 14292 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not 14293 matter as here were are looking only for cross-bfd sections. */ 14294 14295 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) 14296 for (l = already_linked_list->entry; l != NULL; l = l->next) 14297 if ((l->sec->flags & SEC_GROUP) == 0 14298 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) 14299 { 14300 if (abfd != l->sec->owner) 14301 sec->output_section = bfd_abs_section_ptr; 14302 break; 14303 } 14304 14305 /* This is the first section with this name. Record it. */ 14306 if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) 14307 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); 14308 return sec->output_section == bfd_abs_section_ptr; 14309 } 14310 14311 bfd_boolean 14312 _bfd_elf_common_definition (Elf_Internal_Sym *sym) 14313 { 14314 return sym->st_shndx == SHN_COMMON; 14315 } 14316 14317 unsigned int 14318 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) 14319 { 14320 return SHN_COMMON; 14321 } 14322 14323 asection * 14324 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) 14325 { 14326 return bfd_com_section_ptr; 14327 } 14328 14329 bfd_vma 14330 _bfd_elf_default_got_elt_size (bfd *abfd, 14331 struct bfd_link_info *info ATTRIBUTE_UNUSED, 14332 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, 14333 bfd *ibfd ATTRIBUTE_UNUSED, 14334 unsigned long symndx ATTRIBUTE_UNUSED) 14335 { 14336 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14337 return bed->s->arch_size / 8; 14338 } 14339 14340 /* Routines to support the creation of dynamic relocs. */ 14341 14342 /* Returns the name of the dynamic reloc section associated with SEC. */ 14343 14344 static const char * 14345 get_dynamic_reloc_section_name (bfd * abfd, 14346 asection * sec, 14347 bfd_boolean is_rela) 14348 { 14349 char *name; 14350 const char *old_name = bfd_get_section_name (NULL, sec); 14351 const char *prefix = is_rela ? ".rela" : ".rel"; 14352 14353 if (old_name == NULL) 14354 return NULL; 14355 14356 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); 14357 sprintf (name, "%s%s", prefix, old_name); 14358 14359 return name; 14360 } 14361 14362 /* Returns the dynamic reloc section associated with SEC. 14363 If necessary compute the name of the dynamic reloc section based 14364 on SEC's name (looked up in ABFD's string table) and the setting 14365 of IS_RELA. */ 14366 14367 asection * 14368 _bfd_elf_get_dynamic_reloc_section (bfd * abfd, 14369 asection * sec, 14370 bfd_boolean is_rela) 14371 { 14372 asection * reloc_sec = elf_section_data (sec)->sreloc; 14373 14374 if (reloc_sec == NULL) 14375 { 14376 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 14377 14378 if (name != NULL) 14379 { 14380 reloc_sec = bfd_get_linker_section (abfd, name); 14381 14382 if (reloc_sec != NULL) 14383 elf_section_data (sec)->sreloc = reloc_sec; 14384 } 14385 } 14386 14387 return reloc_sec; 14388 } 14389 14390 /* Returns the dynamic reloc section associated with SEC. If the 14391 section does not exist it is created and attached to the DYNOBJ 14392 bfd and stored in the SRELOC field of SEC's elf_section_data 14393 structure. 14394 14395 ALIGNMENT is the alignment for the newly created section and 14396 IS_RELA defines whether the name should be .rela.<SEC's name> 14397 or .rel.<SEC's name>. The section name is looked up in the 14398 string table associated with ABFD. */ 14399 14400 asection * 14401 _bfd_elf_make_dynamic_reloc_section (asection *sec, 14402 bfd *dynobj, 14403 unsigned int alignment, 14404 bfd *abfd, 14405 bfd_boolean is_rela) 14406 { 14407 asection * reloc_sec = elf_section_data (sec)->sreloc; 14408 14409 if (reloc_sec == NULL) 14410 { 14411 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 14412 14413 if (name == NULL) 14414 return NULL; 14415 14416 reloc_sec = bfd_get_linker_section (dynobj, name); 14417 14418 if (reloc_sec == NULL) 14419 { 14420 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY 14421 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 14422 if ((sec->flags & SEC_ALLOC) != 0) 14423 flags |= SEC_ALLOC | SEC_LOAD; 14424 14425 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); 14426 if (reloc_sec != NULL) 14427 { 14428 /* _bfd_elf_get_sec_type_attr chooses a section type by 14429 name. Override as it may be wrong, eg. for a user 14430 section named "auto" we'll get ".relauto" which is 14431 seen to be a .rela section. */ 14432 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; 14433 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) 14434 reloc_sec = NULL; 14435 } 14436 } 14437 14438 elf_section_data (sec)->sreloc = reloc_sec; 14439 } 14440 14441 return reloc_sec; 14442 } 14443 14444 /* Copy the ELF symbol type and other attributes for a linker script 14445 assignment from HSRC to HDEST. Generally this should be treated as 14446 if we found a strong non-dynamic definition for HDEST (except that 14447 ld ignores multiple definition errors). */ 14448 void 14449 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, 14450 struct bfd_link_hash_entry *hdest, 14451 struct bfd_link_hash_entry *hsrc) 14452 { 14453 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; 14454 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; 14455 Elf_Internal_Sym isym; 14456 14457 ehdest->type = ehsrc->type; 14458 ehdest->target_internal = ehsrc->target_internal; 14459 14460 isym.st_other = ehsrc->other; 14461 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); 14462 } 14463 14464 /* Append a RELA relocation REL to section S in BFD. */ 14465 14466 void 14467 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 14468 { 14469 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14470 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); 14471 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); 14472 bed->s->swap_reloca_out (abfd, rel, loc); 14473 } 14474 14475 /* Append a REL relocation REL to section S in BFD. */ 14476 14477 void 14478 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 14479 { 14480 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14481 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); 14482 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); 14483 bed->s->swap_reloc_out (abfd, rel, loc); 14484 } 14485 14486 /* Define __start, __stop, .startof. or .sizeof. symbol. */ 14487 14488 struct bfd_link_hash_entry * 14489 bfd_elf_define_start_stop (struct bfd_link_info *info, 14490 const char *symbol, asection *sec) 14491 { 14492 struct elf_link_hash_entry *h; 14493 14494 h = elf_link_hash_lookup (elf_hash_table (info), symbol, 14495 FALSE, FALSE, TRUE); 14496 if (h != NULL 14497 && (h->root.type == bfd_link_hash_undefined 14498 || h->root.type == bfd_link_hash_undefweak 14499 || ((h->ref_regular || h->def_dynamic) && !h->def_regular))) 14500 { 14501 bfd_boolean was_dynamic = h->ref_dynamic || h->def_dynamic; 14502 h->root.type = bfd_link_hash_defined; 14503 h->root.u.def.section = sec; 14504 h->root.u.def.value = 0; 14505 h->def_regular = 1; 14506 h->def_dynamic = 0; 14507 h->start_stop = 1; 14508 h->u2.start_stop_section = sec; 14509 if (symbol[0] == '.') 14510 { 14511 /* .startof. and .sizeof. symbols are local. */ 14512 const struct elf_backend_data *bed; 14513 bed = get_elf_backend_data (info->output_bfd); 14514 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 14515 } 14516 else 14517 { 14518 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 14519 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; 14520 if (was_dynamic) 14521 bfd_elf_link_record_dynamic_symbol (info, h); 14522 } 14523 return &h->root; 14524 } 14525 return NULL; 14526 } 14527