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 h->dynamic = 1; 590 } 591 592 /* Record an assignment to a symbol made by a linker script. We need 593 this in case some dynamic object refers to this symbol. */ 594 595 bfd_boolean 596 bfd_elf_record_link_assignment (bfd *output_bfd, 597 struct bfd_link_info *info, 598 const char *name, 599 bfd_boolean provide, 600 bfd_boolean hidden) 601 { 602 struct elf_link_hash_entry *h, *hv; 603 struct elf_link_hash_table *htab; 604 const struct elf_backend_data *bed; 605 606 if (!is_elf_hash_table (info->hash)) 607 return TRUE; 608 609 htab = elf_hash_table (info); 610 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); 611 if (h == NULL) 612 return provide; 613 614 if (h->root.type == bfd_link_hash_warning) 615 h = (struct elf_link_hash_entry *) h->root.u.i.link; 616 617 if (h->versioned == unknown) 618 { 619 /* Set versioned if symbol version is unknown. */ 620 char *version = strrchr (name, ELF_VER_CHR); 621 if (version) 622 { 623 if (version > name && version[-1] != ELF_VER_CHR) 624 h->versioned = versioned_hidden; 625 else 626 h->versioned = versioned; 627 } 628 } 629 630 /* Symbols defined in a linker script but not referenced anywhere 631 else will have non_elf set. */ 632 if (h->non_elf) 633 { 634 bfd_elf_link_mark_dynamic_symbol (info, h, NULL); 635 h->non_elf = 0; 636 } 637 638 switch (h->root.type) 639 { 640 case bfd_link_hash_defined: 641 case bfd_link_hash_defweak: 642 case bfd_link_hash_common: 643 break; 644 case bfd_link_hash_undefweak: 645 case bfd_link_hash_undefined: 646 /* Since we're defining the symbol, don't let it seem to have not 647 been defined. record_dynamic_symbol and size_dynamic_sections 648 may depend on this. */ 649 h->root.type = bfd_link_hash_new; 650 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) 651 bfd_link_repair_undef_list (&htab->root); 652 break; 653 case bfd_link_hash_new: 654 break; 655 case bfd_link_hash_indirect: 656 /* We had a versioned symbol in a dynamic library. We make the 657 the versioned symbol point to this one. */ 658 bed = get_elf_backend_data (output_bfd); 659 hv = h; 660 while (hv->root.type == bfd_link_hash_indirect 661 || hv->root.type == bfd_link_hash_warning) 662 hv = (struct elf_link_hash_entry *) hv->root.u.i.link; 663 /* We don't need to update h->root.u since linker will set them 664 later. */ 665 h->root.type = bfd_link_hash_undefined; 666 hv->root.type = bfd_link_hash_indirect; 667 hv->root.u.i.link = (struct bfd_link_hash_entry *) h; 668 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); 669 break; 670 default: 671 BFD_FAIL (); 672 return FALSE; 673 } 674 675 /* If this symbol is being provided by the linker script, and it is 676 currently defined by a dynamic object, but not by a regular 677 object, then mark it as undefined so that the generic linker will 678 force the correct value. */ 679 if (provide 680 && h->def_dynamic 681 && !h->def_regular) 682 h->root.type = bfd_link_hash_undefined; 683 684 /* If this symbol is not being provided by the linker script, and it is 685 currently defined by a dynamic object, but not by a regular object, 686 then clear out any version information because the symbol will not be 687 associated with the dynamic object any more. */ 688 if (!provide 689 && h->def_dynamic 690 && !h->def_regular) 691 h->verinfo.verdef = NULL; 692 693 /* Make sure this symbol is not garbage collected. */ 694 h->mark = 1; 695 696 h->def_regular = 1; 697 698 if (hidden) 699 { 700 bed = get_elf_backend_data (output_bfd); 701 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL) 702 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 703 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 704 } 705 706 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects 707 and executables. */ 708 if (!bfd_link_relocatable (info) 709 && h->dynindx != -1 710 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 711 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) 712 h->forced_local = 1; 713 714 if ((h->def_dynamic 715 || h->ref_dynamic 716 || bfd_link_dll (info) 717 || elf_hash_table (info)->is_relocatable_executable) 718 && h->dynindx == -1) 719 { 720 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 721 return FALSE; 722 723 /* If this is a weak defined symbol, and we know a corresponding 724 real symbol from the same dynamic object, make sure the real 725 symbol is also made into a dynamic symbol. */ 726 if (h->is_weakalias) 727 { 728 struct elf_link_hash_entry *def = weakdef (h); 729 730 if (def->dynindx == -1 731 && !bfd_elf_link_record_dynamic_symbol (info, def)) 732 return FALSE; 733 } 734 } 735 736 return TRUE; 737 } 738 739 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on 740 success, and 2 on a failure caused by attempting to record a symbol 741 in a discarded section, eg. a discarded link-once section symbol. */ 742 743 int 744 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, 745 bfd *input_bfd, 746 long input_indx) 747 { 748 bfd_size_type amt; 749 struct elf_link_local_dynamic_entry *entry; 750 struct elf_link_hash_table *eht; 751 struct elf_strtab_hash *dynstr; 752 size_t dynstr_index; 753 char *name; 754 Elf_External_Sym_Shndx eshndx; 755 char esym[sizeof (Elf64_External_Sym)]; 756 757 if (! is_elf_hash_table (info->hash)) 758 return 0; 759 760 /* See if the entry exists already. */ 761 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) 762 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) 763 return 1; 764 765 amt = sizeof (*entry); 766 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); 767 if (entry == NULL) 768 return 0; 769 770 /* Go find the symbol, so that we can find it's name. */ 771 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, 772 1, input_indx, &entry->isym, esym, &eshndx)) 773 { 774 bfd_release (input_bfd, entry); 775 return 0; 776 } 777 778 if (entry->isym.st_shndx != SHN_UNDEF 779 && entry->isym.st_shndx < SHN_LORESERVE) 780 { 781 asection *s; 782 783 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); 784 if (s == NULL || bfd_is_abs_section (s->output_section)) 785 { 786 /* We can still bfd_release here as nothing has done another 787 bfd_alloc. We can't do this later in this function. */ 788 bfd_release (input_bfd, entry); 789 return 2; 790 } 791 } 792 793 name = (bfd_elf_string_from_elf_section 794 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, 795 entry->isym.st_name)); 796 797 dynstr = elf_hash_table (info)->dynstr; 798 if (dynstr == NULL) 799 { 800 /* Create a strtab to hold the dynamic symbol names. */ 801 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 802 if (dynstr == NULL) 803 return 0; 804 } 805 806 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); 807 if (dynstr_index == (size_t) -1) 808 return 0; 809 entry->isym.st_name = dynstr_index; 810 811 eht = elf_hash_table (info); 812 813 entry->next = eht->dynlocal; 814 eht->dynlocal = entry; 815 entry->input_bfd = input_bfd; 816 entry->input_indx = input_indx; 817 eht->dynsymcount++; 818 819 /* Whatever binding the symbol had before, it's now local. */ 820 entry->isym.st_info 821 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); 822 823 /* The dynindx will be set at the end of size_dynamic_sections. */ 824 825 return 1; 826 } 827 828 /* Return the dynindex of a local dynamic symbol. */ 829 830 long 831 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, 832 bfd *input_bfd, 833 long input_indx) 834 { 835 struct elf_link_local_dynamic_entry *e; 836 837 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 838 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 839 return e->dynindx; 840 return -1; 841 } 842 843 /* This function is used to renumber the dynamic symbols, if some of 844 them are removed because they are marked as local. This is called 845 via elf_link_hash_traverse. */ 846 847 static bfd_boolean 848 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, 849 void *data) 850 { 851 size_t *count = (size_t *) data; 852 853 if (h->forced_local) 854 return TRUE; 855 856 if (h->dynindx != -1) 857 h->dynindx = ++(*count); 858 859 return TRUE; 860 } 861 862 863 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with 864 STB_LOCAL binding. */ 865 866 static bfd_boolean 867 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, 868 void *data) 869 { 870 size_t *count = (size_t *) data; 871 872 if (!h->forced_local) 873 return TRUE; 874 875 if (h->dynindx != -1) 876 h->dynindx = ++(*count); 877 878 return TRUE; 879 } 880 881 /* Return true if the dynamic symbol for a given section should be 882 omitted when creating a shared library. */ 883 bfd_boolean 884 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, 885 struct bfd_link_info *info, 886 asection *p) 887 { 888 struct elf_link_hash_table *htab; 889 asection *ip; 890 891 switch (elf_section_data (p)->this_hdr.sh_type) 892 { 893 case SHT_PROGBITS: 894 case SHT_NOBITS: 895 /* If sh_type is yet undecided, assume it could be 896 SHT_PROGBITS/SHT_NOBITS. */ 897 case SHT_NULL: 898 htab = elf_hash_table (info); 899 if (p == htab->tls_sec) 900 return FALSE; 901 902 if (htab->text_index_section != NULL) 903 return p != htab->text_index_section && p != htab->data_index_section; 904 905 return (htab->dynobj != NULL 906 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL 907 && ip->output_section == p); 908 909 /* There shouldn't be section relative relocations 910 against any other section. */ 911 default: 912 return TRUE; 913 } 914 } 915 916 /* Assign dynsym indices. In a shared library we generate a section 917 symbol for each output section, which come first. Next come symbols 918 which have been forced to local binding. Then all of the back-end 919 allocated local dynamic syms, followed by the rest of the global 920 symbols. If SECTION_SYM_COUNT is NULL, section dynindx is not set. 921 (This prevents the early call before elf_backend_init_index_section 922 and strip_excluded_output_sections setting dynindx for sections 923 that are stripped.) */ 924 925 static unsigned long 926 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, 927 struct bfd_link_info *info, 928 unsigned long *section_sym_count) 929 { 930 unsigned long dynsymcount = 0; 931 bfd_boolean do_sec = section_sym_count != NULL; 932 933 if (bfd_link_pic (info) 934 || elf_hash_table (info)->is_relocatable_executable) 935 { 936 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 937 asection *p; 938 for (p = output_bfd->sections; p ; p = p->next) 939 if ((p->flags & SEC_EXCLUDE) == 0 940 && (p->flags & SEC_ALLOC) != 0 941 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) 942 { 943 ++dynsymcount; 944 if (do_sec) 945 elf_section_data (p)->dynindx = dynsymcount; 946 } 947 else if (do_sec) 948 elf_section_data (p)->dynindx = 0; 949 } 950 if (do_sec) 951 *section_sym_count = dynsymcount; 952 953 elf_link_hash_traverse (elf_hash_table (info), 954 elf_link_renumber_local_hash_table_dynsyms, 955 &dynsymcount); 956 957 if (elf_hash_table (info)->dynlocal) 958 { 959 struct elf_link_local_dynamic_entry *p; 960 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 961 p->dynindx = ++dynsymcount; 962 } 963 elf_hash_table (info)->local_dynsymcount = dynsymcount; 964 965 elf_link_hash_traverse (elf_hash_table (info), 966 elf_link_renumber_hash_table_dynsyms, 967 &dynsymcount); 968 969 /* There is an unused NULL entry at the head of the table which we 970 must account for in our count even if the table is empty since it 971 is intended for the mandatory DT_SYMTAB tag (.dynsym section) in 972 .dynamic section. */ 973 dynsymcount++; 974 975 elf_hash_table (info)->dynsymcount = dynsymcount; 976 return dynsymcount; 977 } 978 979 /* Merge st_other field. */ 980 981 static void 982 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, 983 const Elf_Internal_Sym *isym, asection *sec, 984 bfd_boolean definition, bfd_boolean dynamic) 985 { 986 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 987 988 /* If st_other has a processor-specific meaning, specific 989 code might be needed here. */ 990 if (bed->elf_backend_merge_symbol_attribute) 991 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, 992 dynamic); 993 994 if (!dynamic) 995 { 996 unsigned symvis = ELF_ST_VISIBILITY (isym->st_other); 997 unsigned hvis = ELF_ST_VISIBILITY (h->other); 998 999 /* Keep the most constraining visibility. Leave the remainder 1000 of the st_other field to elf_backend_merge_symbol_attribute. */ 1001 if (symvis - 1 < hvis - 1) 1002 h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1)); 1003 } 1004 else if (definition 1005 && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT 1006 && (sec->flags & SEC_READONLY) == 0) 1007 h->protected_def = 1; 1008 } 1009 1010 /* This function is called when we want to merge a new symbol with an 1011 existing symbol. It handles the various cases which arise when we 1012 find a definition in a dynamic object, or when there is already a 1013 definition in a dynamic object. The new symbol is described by 1014 NAME, SYM, PSEC, and PVALUE. We set SYM_HASH to the hash table 1015 entry. We set POLDBFD to the old symbol's BFD. We set POLD_WEAK 1016 if the old symbol was weak. We set POLD_ALIGNMENT to the alignment 1017 of an old common symbol. We set OVERRIDE if the old symbol is 1018 overriding a new definition. We set TYPE_CHANGE_OK if it is OK for 1019 the type to change. We set SIZE_CHANGE_OK if it is OK for the size 1020 to change. By OK to change, we mean that we shouldn't warn if the 1021 type or size does change. */ 1022 1023 static bfd_boolean 1024 _bfd_elf_merge_symbol (bfd *abfd, 1025 struct bfd_link_info *info, 1026 const char *name, 1027 Elf_Internal_Sym *sym, 1028 asection **psec, 1029 bfd_vma *pvalue, 1030 struct elf_link_hash_entry **sym_hash, 1031 bfd **poldbfd, 1032 bfd_boolean *pold_weak, 1033 unsigned int *pold_alignment, 1034 bfd_boolean *skip, 1035 bfd_boolean *override, 1036 bfd_boolean *type_change_ok, 1037 bfd_boolean *size_change_ok, 1038 bfd_boolean *matched) 1039 { 1040 asection *sec, *oldsec; 1041 struct elf_link_hash_entry *h; 1042 struct elf_link_hash_entry *hi; 1043 struct elf_link_hash_entry *flip; 1044 int bind; 1045 bfd *oldbfd; 1046 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; 1047 bfd_boolean newweak, oldweak, newfunc, oldfunc; 1048 const struct elf_backend_data *bed; 1049 char *new_version; 1050 bfd_boolean default_sym = *matched; 1051 1052 *skip = FALSE; 1053 *override = FALSE; 1054 1055 sec = *psec; 1056 bind = ELF_ST_BIND (sym->st_info); 1057 1058 if (! bfd_is_und_section (sec)) 1059 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); 1060 else 1061 h = ((struct elf_link_hash_entry *) 1062 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); 1063 if (h == NULL) 1064 return FALSE; 1065 *sym_hash = h; 1066 1067 bed = get_elf_backend_data (abfd); 1068 1069 /* NEW_VERSION is the symbol version of the new symbol. */ 1070 if (h->versioned != unversioned) 1071 { 1072 /* Symbol version is unknown or versioned. */ 1073 new_version = strrchr (name, ELF_VER_CHR); 1074 if (new_version) 1075 { 1076 if (h->versioned == unknown) 1077 { 1078 if (new_version > name && new_version[-1] != ELF_VER_CHR) 1079 h->versioned = versioned_hidden; 1080 else 1081 h->versioned = versioned; 1082 } 1083 new_version += 1; 1084 if (new_version[0] == '\0') 1085 new_version = NULL; 1086 } 1087 else 1088 h->versioned = unversioned; 1089 } 1090 else 1091 new_version = NULL; 1092 1093 /* For merging, we only care about real symbols. But we need to make 1094 sure that indirect symbol dynamic flags are updated. */ 1095 hi = h; 1096 while (h->root.type == bfd_link_hash_indirect 1097 || h->root.type == bfd_link_hash_warning) 1098 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1099 1100 if (!*matched) 1101 { 1102 if (hi == h || h->root.type == bfd_link_hash_new) 1103 *matched = TRUE; 1104 else 1105 { 1106 /* OLD_HIDDEN is true if the existing symbol is only visible 1107 to the symbol with the same symbol version. NEW_HIDDEN is 1108 true if the new symbol is only visible to the symbol with 1109 the same symbol version. */ 1110 bfd_boolean old_hidden = h->versioned == versioned_hidden; 1111 bfd_boolean new_hidden = hi->versioned == versioned_hidden; 1112 if (!old_hidden && !new_hidden) 1113 /* The new symbol matches the existing symbol if both 1114 aren't hidden. */ 1115 *matched = TRUE; 1116 else 1117 { 1118 /* OLD_VERSION is the symbol version of the existing 1119 symbol. */ 1120 char *old_version; 1121 1122 if (h->versioned >= versioned) 1123 old_version = strrchr (h->root.root.string, 1124 ELF_VER_CHR) + 1; 1125 else 1126 old_version = NULL; 1127 1128 /* The new symbol matches the existing symbol if they 1129 have the same symbol version. */ 1130 *matched = (old_version == new_version 1131 || (old_version != NULL 1132 && new_version != NULL 1133 && strcmp (old_version, new_version) == 0)); 1134 } 1135 } 1136 } 1137 1138 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the 1139 existing symbol. */ 1140 1141 oldbfd = NULL; 1142 oldsec = NULL; 1143 switch (h->root.type) 1144 { 1145 default: 1146 break; 1147 1148 case bfd_link_hash_undefined: 1149 case bfd_link_hash_undefweak: 1150 oldbfd = h->root.u.undef.abfd; 1151 break; 1152 1153 case bfd_link_hash_defined: 1154 case bfd_link_hash_defweak: 1155 oldbfd = h->root.u.def.section->owner; 1156 oldsec = h->root.u.def.section; 1157 break; 1158 1159 case bfd_link_hash_common: 1160 oldbfd = h->root.u.c.p->section->owner; 1161 oldsec = h->root.u.c.p->section; 1162 if (pold_alignment) 1163 *pold_alignment = h->root.u.c.p->alignment_power; 1164 break; 1165 } 1166 if (poldbfd && *poldbfd == NULL) 1167 *poldbfd = oldbfd; 1168 1169 /* Differentiate strong and weak symbols. */ 1170 newweak = bind == STB_WEAK; 1171 oldweak = (h->root.type == bfd_link_hash_defweak 1172 || h->root.type == bfd_link_hash_undefweak); 1173 if (pold_weak) 1174 *pold_weak = oldweak; 1175 1176 /* We have to check it for every instance since the first few may be 1177 references and not all compilers emit symbol type for undefined 1178 symbols. */ 1179 bfd_elf_link_mark_dynamic_symbol (info, h, sym); 1180 1181 /* NEWDYN and OLDDYN indicate whether the new or old symbol, 1182 respectively, is from a dynamic object. */ 1183 1184 newdyn = (abfd->flags & DYNAMIC) != 0; 1185 1186 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined 1187 syms and defined syms in dynamic libraries respectively. 1188 ref_dynamic on the other hand can be set for a symbol defined in 1189 a dynamic library, and def_dynamic may not be set; When the 1190 definition in a dynamic lib is overridden by a definition in the 1191 executable use of the symbol in the dynamic lib becomes a 1192 reference to the executable symbol. */ 1193 if (newdyn) 1194 { 1195 if (bfd_is_und_section (sec)) 1196 { 1197 if (bind != STB_WEAK) 1198 { 1199 h->ref_dynamic_nonweak = 1; 1200 hi->ref_dynamic_nonweak = 1; 1201 } 1202 } 1203 else 1204 { 1205 /* Update the existing symbol only if they match. */ 1206 if (*matched) 1207 h->dynamic_def = 1; 1208 hi->dynamic_def = 1; 1209 } 1210 } 1211 1212 /* If we just created the symbol, mark it as being an ELF symbol. 1213 Other than that, there is nothing to do--there is no merge issue 1214 with a newly defined symbol--so we just return. */ 1215 1216 if (h->root.type == bfd_link_hash_new) 1217 { 1218 h->non_elf = 0; 1219 return TRUE; 1220 } 1221 1222 /* In cases involving weak versioned symbols, we may wind up trying 1223 to merge a symbol with itself. Catch that here, to avoid the 1224 confusion that results if we try to override a symbol with 1225 itself. The additional tests catch cases like 1226 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a 1227 dynamic object, which we do want to handle here. */ 1228 if (abfd == oldbfd 1229 && (newweak || oldweak) 1230 && ((abfd->flags & DYNAMIC) == 0 1231 || !h->def_regular)) 1232 return TRUE; 1233 1234 olddyn = FALSE; 1235 if (oldbfd != NULL) 1236 olddyn = (oldbfd->flags & DYNAMIC) != 0; 1237 else if (oldsec != NULL) 1238 { 1239 /* This handles the special SHN_MIPS_{TEXT,DATA} section 1240 indices used by MIPS ELF. */ 1241 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; 1242 } 1243 1244 /* Handle a case where plugin_notice won't be called and thus won't 1245 set the non_ir_ref flags on the first pass over symbols. */ 1246 if (oldbfd != NULL 1247 && (oldbfd->flags & BFD_PLUGIN) != (abfd->flags & BFD_PLUGIN) 1248 && newdyn != olddyn) 1249 { 1250 h->root.non_ir_ref_dynamic = TRUE; 1251 hi->root.non_ir_ref_dynamic = TRUE; 1252 } 1253 1254 /* NEWDEF and OLDDEF indicate whether the new or old symbol, 1255 respectively, appear to be a definition rather than reference. */ 1256 1257 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); 1258 1259 olddef = (h->root.type != bfd_link_hash_undefined 1260 && h->root.type != bfd_link_hash_undefweak 1261 && h->root.type != bfd_link_hash_common); 1262 1263 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, 1264 respectively, appear to be a function. */ 1265 1266 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1267 && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); 1268 1269 oldfunc = (h->type != STT_NOTYPE 1270 && bed->is_function_type (h->type)); 1271 1272 if (!(newfunc && oldfunc) 1273 && ELF_ST_TYPE (sym->st_info) != h->type 1274 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1275 && h->type != STT_NOTYPE 1276 && (newdef || bfd_is_com_section (sec)) 1277 && (olddef || h->root.type == bfd_link_hash_common)) 1278 { 1279 /* If creating a default indirect symbol ("foo" or "foo@") from 1280 a dynamic versioned definition ("foo@@") skip doing so if 1281 there is an existing regular definition with a different 1282 type. We don't want, for example, a "time" variable in the 1283 executable overriding a "time" function in a shared library. */ 1284 if (newdyn 1285 && !olddyn) 1286 { 1287 *skip = TRUE; 1288 return TRUE; 1289 } 1290 1291 /* When adding a symbol from a regular object file after we have 1292 created indirect symbols, undo the indirection and any 1293 dynamic state. */ 1294 if (hi != h 1295 && !newdyn 1296 && olddyn) 1297 { 1298 h = hi; 1299 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1300 h->forced_local = 0; 1301 h->ref_dynamic = 0; 1302 h->def_dynamic = 0; 1303 h->dynamic_def = 0; 1304 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1305 { 1306 h->root.type = bfd_link_hash_undefined; 1307 h->root.u.undef.abfd = abfd; 1308 } 1309 else 1310 { 1311 h->root.type = bfd_link_hash_new; 1312 h->root.u.undef.abfd = NULL; 1313 } 1314 return TRUE; 1315 } 1316 } 1317 1318 /* Check TLS symbols. We don't check undefined symbols introduced 1319 by "ld -u" which have no type (and oldbfd NULL), and we don't 1320 check symbols from plugins because they also have no type. */ 1321 if (oldbfd != NULL 1322 && (oldbfd->flags & BFD_PLUGIN) == 0 1323 && (abfd->flags & BFD_PLUGIN) == 0 1324 && ELF_ST_TYPE (sym->st_info) != h->type 1325 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) 1326 { 1327 bfd *ntbfd, *tbfd; 1328 bfd_boolean ntdef, tdef; 1329 asection *ntsec, *tsec; 1330 1331 if (h->type == STT_TLS) 1332 { 1333 ntbfd = abfd; 1334 ntsec = sec; 1335 ntdef = newdef; 1336 tbfd = oldbfd; 1337 tsec = oldsec; 1338 tdef = olddef; 1339 } 1340 else 1341 { 1342 ntbfd = oldbfd; 1343 ntsec = oldsec; 1344 ntdef = olddef; 1345 tbfd = abfd; 1346 tsec = sec; 1347 tdef = newdef; 1348 } 1349 1350 if (tdef && ntdef) 1351 _bfd_error_handler 1352 /* xgettext:c-format */ 1353 (_("%s: TLS definition in %B section %A " 1354 "mismatches non-TLS definition in %B section %A"), 1355 h->root.root.string, tbfd, tsec, ntbfd, ntsec); 1356 else if (!tdef && !ntdef) 1357 _bfd_error_handler 1358 /* xgettext:c-format */ 1359 (_("%s: TLS reference in %B " 1360 "mismatches non-TLS reference in %B"), 1361 h->root.root.string, tbfd, ntbfd); 1362 else if (tdef) 1363 _bfd_error_handler 1364 /* xgettext:c-format */ 1365 (_("%s: TLS definition in %B section %A " 1366 "mismatches non-TLS reference in %B"), 1367 h->root.root.string, tbfd, tsec, ntbfd); 1368 else 1369 _bfd_error_handler 1370 /* xgettext:c-format */ 1371 (_("%s: TLS reference in %B " 1372 "mismatches non-TLS definition in %B section %A"), 1373 h->root.root.string, tbfd, ntbfd, ntsec); 1374 1375 bfd_set_error (bfd_error_bad_value); 1376 return FALSE; 1377 } 1378 1379 /* If the old symbol has non-default visibility, we ignore the new 1380 definition from a dynamic object. */ 1381 if (newdyn 1382 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 1383 && !bfd_is_und_section (sec)) 1384 { 1385 *skip = TRUE; 1386 /* Make sure this symbol is dynamic. */ 1387 h->ref_dynamic = 1; 1388 hi->ref_dynamic = 1; 1389 /* A protected symbol has external availability. Make sure it is 1390 recorded as dynamic. 1391 1392 FIXME: Should we check type and size for protected symbol? */ 1393 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 1394 return bfd_elf_link_record_dynamic_symbol (info, h); 1395 else 1396 return TRUE; 1397 } 1398 else if (!newdyn 1399 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT 1400 && h->def_dynamic) 1401 { 1402 /* If the new symbol with non-default visibility comes from a 1403 relocatable file and the old definition comes from a dynamic 1404 object, we remove the old definition. */ 1405 if (hi->root.type == bfd_link_hash_indirect) 1406 { 1407 /* Handle the case where the old dynamic definition is 1408 default versioned. We need to copy the symbol info from 1409 the symbol with default version to the normal one if it 1410 was referenced before. */ 1411 if (h->ref_regular) 1412 { 1413 hi->root.type = h->root.type; 1414 h->root.type = bfd_link_hash_indirect; 1415 (*bed->elf_backend_copy_indirect_symbol) (info, hi, h); 1416 1417 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1418 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1419 { 1420 /* If the new symbol is hidden or internal, completely undo 1421 any dynamic link state. */ 1422 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1423 h->forced_local = 0; 1424 h->ref_dynamic = 0; 1425 } 1426 else 1427 h->ref_dynamic = 1; 1428 1429 h->def_dynamic = 0; 1430 /* FIXME: Should we check type and size for protected symbol? */ 1431 h->size = 0; 1432 h->type = 0; 1433 1434 h = hi; 1435 } 1436 else 1437 h = hi; 1438 } 1439 1440 /* If the old symbol was undefined before, then it will still be 1441 on the undefs list. If the new symbol is undefined or 1442 common, we can't make it bfd_link_hash_new here, because new 1443 undefined or common symbols will be added to the undefs list 1444 by _bfd_generic_link_add_one_symbol. Symbols may not be 1445 added twice to the undefs list. Also, if the new symbol is 1446 undefweak then we don't want to lose the strong undef. */ 1447 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1448 { 1449 h->root.type = bfd_link_hash_undefined; 1450 h->root.u.undef.abfd = abfd; 1451 } 1452 else 1453 { 1454 h->root.type = bfd_link_hash_new; 1455 h->root.u.undef.abfd = NULL; 1456 } 1457 1458 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1459 { 1460 /* If the new symbol is hidden or internal, completely undo 1461 any dynamic link state. */ 1462 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1463 h->forced_local = 0; 1464 h->ref_dynamic = 0; 1465 } 1466 else 1467 h->ref_dynamic = 1; 1468 h->def_dynamic = 0; 1469 /* FIXME: Should we check type and size for protected symbol? */ 1470 h->size = 0; 1471 h->type = 0; 1472 return TRUE; 1473 } 1474 1475 /* If a new weak symbol definition comes from a regular file and the 1476 old symbol comes from a dynamic library, we treat the new one as 1477 strong. Similarly, an old weak symbol definition from a regular 1478 file is treated as strong when the new symbol comes from a dynamic 1479 library. Further, an old weak symbol from a dynamic library is 1480 treated as strong if the new symbol is from a dynamic library. 1481 This reflects the way glibc's ld.so works. 1482 1483 Also allow a weak symbol to override a linker script symbol 1484 defined by an early pass over the script. This is done so the 1485 linker knows the symbol is defined in an object file, for the 1486 DEFINED script function. 1487 1488 Do this before setting *type_change_ok or *size_change_ok so that 1489 we warn properly when dynamic library symbols are overridden. */ 1490 1491 if (newdef && !newdyn && (olddyn || h->root.ldscript_def)) 1492 newweak = FALSE; 1493 if (olddef && newdyn) 1494 oldweak = FALSE; 1495 1496 /* Allow changes between different types of function symbol. */ 1497 if (newfunc && oldfunc) 1498 *type_change_ok = TRUE; 1499 1500 /* It's OK to change the type if either the existing symbol or the 1501 new symbol is weak. A type change is also OK if the old symbol 1502 is undefined and the new symbol is defined. */ 1503 1504 if (oldweak 1505 || newweak 1506 || (newdef 1507 && h->root.type == bfd_link_hash_undefined)) 1508 *type_change_ok = TRUE; 1509 1510 /* It's OK to change the size if either the existing symbol or the 1511 new symbol is weak, or if the old symbol is undefined. */ 1512 1513 if (*type_change_ok 1514 || h->root.type == bfd_link_hash_undefined) 1515 *size_change_ok = TRUE; 1516 1517 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old 1518 symbol, respectively, appears to be a common symbol in a dynamic 1519 object. If a symbol appears in an uninitialized section, and is 1520 not weak, and is not a function, then it may be a common symbol 1521 which was resolved when the dynamic object was created. We want 1522 to treat such symbols specially, because they raise special 1523 considerations when setting the symbol size: if the symbol 1524 appears as a common symbol in a regular object, and the size in 1525 the regular object is larger, we must make sure that we use the 1526 larger size. This problematic case can always be avoided in C, 1527 but it must be handled correctly when using Fortran shared 1528 libraries. 1529 1530 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and 1531 likewise for OLDDYNCOMMON and OLDDEF. 1532 1533 Note that this test is just a heuristic, and that it is quite 1534 possible to have an uninitialized symbol in a shared object which 1535 is really a definition, rather than a common symbol. This could 1536 lead to some minor confusion when the symbol really is a common 1537 symbol in some regular object. However, I think it will be 1538 harmless. */ 1539 1540 if (newdyn 1541 && newdef 1542 && !newweak 1543 && (sec->flags & SEC_ALLOC) != 0 1544 && (sec->flags & SEC_LOAD) == 0 1545 && sym->st_size > 0 1546 && !newfunc) 1547 newdyncommon = TRUE; 1548 else 1549 newdyncommon = FALSE; 1550 1551 if (olddyn 1552 && olddef 1553 && h->root.type == bfd_link_hash_defined 1554 && h->def_dynamic 1555 && (h->root.u.def.section->flags & SEC_ALLOC) != 0 1556 && (h->root.u.def.section->flags & SEC_LOAD) == 0 1557 && h->size > 0 1558 && !oldfunc) 1559 olddyncommon = TRUE; 1560 else 1561 olddyncommon = FALSE; 1562 1563 /* We now know everything about the old and new symbols. We ask the 1564 backend to check if we can merge them. */ 1565 if (bed->merge_symbol != NULL) 1566 { 1567 if (!bed->merge_symbol (h, sym, psec, newdef, olddef, oldbfd, oldsec)) 1568 return FALSE; 1569 sec = *psec; 1570 } 1571 1572 /* There are multiple definitions of a normal symbol. Skip the 1573 default symbol as well as definition from an IR object. */ 1574 if (olddef && !olddyn && !oldweak && newdef && !newdyn && !newweak 1575 && !default_sym && h->def_regular 1576 && !(oldbfd != NULL 1577 && (oldbfd->flags & BFD_PLUGIN) != 0 1578 && (abfd->flags & BFD_PLUGIN) == 0)) 1579 { 1580 /* Handle a multiple definition. */ 1581 (*info->callbacks->multiple_definition) (info, &h->root, 1582 abfd, sec, *pvalue); 1583 *skip = TRUE; 1584 return TRUE; 1585 } 1586 1587 /* If both the old and the new symbols look like common symbols in a 1588 dynamic object, set the size of the symbol to the larger of the 1589 two. */ 1590 1591 if (olddyncommon 1592 && newdyncommon 1593 && sym->st_size != h->size) 1594 { 1595 /* Since we think we have two common symbols, issue a multiple 1596 common warning if desired. Note that we only warn if the 1597 size is different. If the size is the same, we simply let 1598 the old symbol override the new one as normally happens with 1599 symbols defined in dynamic objects. */ 1600 1601 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1602 bfd_link_hash_common, sym->st_size); 1603 if (sym->st_size > h->size) 1604 h->size = sym->st_size; 1605 1606 *size_change_ok = TRUE; 1607 } 1608 1609 /* If we are looking at a dynamic object, and we have found a 1610 definition, we need to see if the symbol was already defined by 1611 some other object. If so, we want to use the existing 1612 definition, and we do not want to report a multiple symbol 1613 definition error; we do this by clobbering *PSEC to be 1614 bfd_und_section_ptr. 1615 1616 We treat a common symbol as a definition if the symbol in the 1617 shared library is a function, since common symbols always 1618 represent variables; this can cause confusion in principle, but 1619 any such confusion would seem to indicate an erroneous program or 1620 shared library. We also permit a common symbol in a regular 1621 object to override a weak symbol in a shared object. */ 1622 1623 if (newdyn 1624 && newdef 1625 && (olddef 1626 || (h->root.type == bfd_link_hash_common 1627 && (newweak || newfunc)))) 1628 { 1629 *override = TRUE; 1630 newdef = FALSE; 1631 newdyncommon = FALSE; 1632 1633 *psec = sec = bfd_und_section_ptr; 1634 *size_change_ok = TRUE; 1635 1636 /* If we get here when the old symbol is a common symbol, then 1637 we are explicitly letting it override a weak symbol or 1638 function in a dynamic object, and we don't want to warn about 1639 a type change. If the old symbol is a defined symbol, a type 1640 change warning may still be appropriate. */ 1641 1642 if (h->root.type == bfd_link_hash_common) 1643 *type_change_ok = TRUE; 1644 } 1645 1646 /* Handle the special case of an old common symbol merging with a 1647 new symbol which looks like a common symbol in a shared object. 1648 We change *PSEC and *PVALUE to make the new symbol look like a 1649 common symbol, and let _bfd_generic_link_add_one_symbol do the 1650 right thing. */ 1651 1652 if (newdyncommon 1653 && h->root.type == bfd_link_hash_common) 1654 { 1655 *override = TRUE; 1656 newdef = FALSE; 1657 newdyncommon = FALSE; 1658 *pvalue = sym->st_size; 1659 *psec = sec = bed->common_section (oldsec); 1660 *size_change_ok = TRUE; 1661 } 1662 1663 /* Skip weak definitions of symbols that are already defined. */ 1664 if (newdef && olddef && newweak) 1665 { 1666 /* Don't skip new non-IR weak syms. */ 1667 if (!(oldbfd != NULL 1668 && (oldbfd->flags & BFD_PLUGIN) != 0 1669 && (abfd->flags & BFD_PLUGIN) == 0)) 1670 { 1671 newdef = FALSE; 1672 *skip = TRUE; 1673 } 1674 1675 /* Merge st_other. If the symbol already has a dynamic index, 1676 but visibility says it should not be visible, turn it into a 1677 local symbol. */ 1678 elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn); 1679 if (h->dynindx != -1) 1680 switch (ELF_ST_VISIBILITY (h->other)) 1681 { 1682 case STV_INTERNAL: 1683 case STV_HIDDEN: 1684 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1685 break; 1686 } 1687 } 1688 1689 /* If the old symbol is from a dynamic object, and the new symbol is 1690 a definition which is not from a dynamic object, then the new 1691 symbol overrides the old symbol. Symbols from regular files 1692 always take precedence over symbols from dynamic objects, even if 1693 they are defined after the dynamic object in the link. 1694 1695 As above, we again permit a common symbol in a regular object to 1696 override a definition in a shared object if the shared object 1697 symbol is a function or is weak. */ 1698 1699 flip = NULL; 1700 if (!newdyn 1701 && (newdef 1702 || (bfd_is_com_section (sec) 1703 && (oldweak || oldfunc))) 1704 && olddyn 1705 && olddef 1706 && h->def_dynamic) 1707 { 1708 /* Change the hash table entry to undefined, and let 1709 _bfd_generic_link_add_one_symbol do the right thing with the 1710 new definition. */ 1711 1712 h->root.type = bfd_link_hash_undefined; 1713 h->root.u.undef.abfd = h->root.u.def.section->owner; 1714 *size_change_ok = TRUE; 1715 1716 olddef = FALSE; 1717 olddyncommon = FALSE; 1718 1719 /* We again permit a type change when a common symbol may be 1720 overriding a function. */ 1721 1722 if (bfd_is_com_section (sec)) 1723 { 1724 if (oldfunc) 1725 { 1726 /* If a common symbol overrides a function, make sure 1727 that it isn't defined dynamically nor has type 1728 function. */ 1729 h->def_dynamic = 0; 1730 h->type = STT_NOTYPE; 1731 } 1732 *type_change_ok = TRUE; 1733 } 1734 1735 if (hi->root.type == bfd_link_hash_indirect) 1736 flip = hi; 1737 else 1738 /* This union may have been set to be non-NULL when this symbol 1739 was seen in a dynamic object. We must force the union to be 1740 NULL, so that it is correct for a regular symbol. */ 1741 h->verinfo.vertree = NULL; 1742 } 1743 1744 /* Handle the special case of a new common symbol merging with an 1745 old symbol that looks like it might be a common symbol defined in 1746 a shared object. Note that we have already handled the case in 1747 which a new common symbol should simply override the definition 1748 in the shared library. */ 1749 1750 if (! newdyn 1751 && bfd_is_com_section (sec) 1752 && olddyncommon) 1753 { 1754 /* It would be best if we could set the hash table entry to a 1755 common symbol, but we don't know what to use for the section 1756 or the alignment. */ 1757 (*info->callbacks->multiple_common) (info, &h->root, abfd, 1758 bfd_link_hash_common, sym->st_size); 1759 1760 /* If the presumed common symbol in the dynamic object is 1761 larger, pretend that the new symbol has its size. */ 1762 1763 if (h->size > *pvalue) 1764 *pvalue = h->size; 1765 1766 /* We need to remember the alignment required by the symbol 1767 in the dynamic object. */ 1768 BFD_ASSERT (pold_alignment); 1769 *pold_alignment = h->root.u.def.section->alignment_power; 1770 1771 olddef = FALSE; 1772 olddyncommon = FALSE; 1773 1774 h->root.type = bfd_link_hash_undefined; 1775 h->root.u.undef.abfd = h->root.u.def.section->owner; 1776 1777 *size_change_ok = TRUE; 1778 *type_change_ok = TRUE; 1779 1780 if (hi->root.type == bfd_link_hash_indirect) 1781 flip = hi; 1782 else 1783 h->verinfo.vertree = NULL; 1784 } 1785 1786 if (flip != NULL) 1787 { 1788 /* Handle the case where we had a versioned symbol in a dynamic 1789 library and now find a definition in a normal object. In this 1790 case, we make the versioned symbol point to the normal one. */ 1791 flip->root.type = h->root.type; 1792 flip->root.u.undef.abfd = h->root.u.undef.abfd; 1793 h->root.type = bfd_link_hash_indirect; 1794 h->root.u.i.link = (struct bfd_link_hash_entry *) flip; 1795 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); 1796 if (h->def_dynamic) 1797 { 1798 h->def_dynamic = 0; 1799 flip->ref_dynamic = 1; 1800 } 1801 } 1802 1803 return TRUE; 1804 } 1805 1806 /* This function is called to create an indirect symbol from the 1807 default for the symbol with the default version if needed. The 1808 symbol is described by H, NAME, SYM, SEC, and VALUE. We 1809 set DYNSYM if the new indirect symbol is dynamic. */ 1810 1811 static bfd_boolean 1812 _bfd_elf_add_default_symbol (bfd *abfd, 1813 struct bfd_link_info *info, 1814 struct elf_link_hash_entry *h, 1815 const char *name, 1816 Elf_Internal_Sym *sym, 1817 asection *sec, 1818 bfd_vma value, 1819 bfd **poldbfd, 1820 bfd_boolean *dynsym) 1821 { 1822 bfd_boolean type_change_ok; 1823 bfd_boolean size_change_ok; 1824 bfd_boolean skip; 1825 char *shortname; 1826 struct elf_link_hash_entry *hi; 1827 struct bfd_link_hash_entry *bh; 1828 const struct elf_backend_data *bed; 1829 bfd_boolean collect; 1830 bfd_boolean dynamic; 1831 bfd_boolean override; 1832 char *p; 1833 size_t len, shortlen; 1834 asection *tmp_sec; 1835 bfd_boolean matched; 1836 1837 if (h->versioned == unversioned || h->versioned == versioned_hidden) 1838 return TRUE; 1839 1840 /* If this symbol has a version, and it is the default version, we 1841 create an indirect symbol from the default name to the fully 1842 decorated name. This will cause external references which do not 1843 specify a version to be bound to this version of the symbol. */ 1844 p = strchr (name, ELF_VER_CHR); 1845 if (h->versioned == unknown) 1846 { 1847 if (p == NULL) 1848 { 1849 h->versioned = unversioned; 1850 return TRUE; 1851 } 1852 else 1853 { 1854 if (p[1] != ELF_VER_CHR) 1855 { 1856 h->versioned = versioned_hidden; 1857 return TRUE; 1858 } 1859 else 1860 h->versioned = versioned; 1861 } 1862 } 1863 else 1864 { 1865 /* PR ld/19073: We may see an unversioned definition after the 1866 default version. */ 1867 if (p == NULL) 1868 return TRUE; 1869 } 1870 1871 bed = get_elf_backend_data (abfd); 1872 collect = bed->collect; 1873 dynamic = (abfd->flags & DYNAMIC) != 0; 1874 1875 shortlen = p - name; 1876 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); 1877 if (shortname == NULL) 1878 return FALSE; 1879 memcpy (shortname, name, shortlen); 1880 shortname[shortlen] = '\0'; 1881 1882 /* We are going to create a new symbol. Merge it with any existing 1883 symbol with this name. For the purposes of the merge, act as 1884 though we were defining the symbol we just defined, although we 1885 actually going to define an indirect symbol. */ 1886 type_change_ok = FALSE; 1887 size_change_ok = FALSE; 1888 matched = TRUE; 1889 tmp_sec = sec; 1890 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 1891 &hi, poldbfd, NULL, NULL, &skip, &override, 1892 &type_change_ok, &size_change_ok, &matched)) 1893 return FALSE; 1894 1895 if (skip) 1896 goto nondefault; 1897 1898 if (hi->def_regular) 1899 { 1900 /* If the undecorated symbol will have a version added by a 1901 script different to H, then don't indirect to/from the 1902 undecorated symbol. This isn't ideal because we may not yet 1903 have seen symbol versions, if given by a script on the 1904 command line rather than via --version-script. */ 1905 if (hi->verinfo.vertree == NULL && info->version_info != NULL) 1906 { 1907 bfd_boolean hide; 1908 1909 hi->verinfo.vertree 1910 = bfd_find_version_for_sym (info->version_info, 1911 hi->root.root.string, &hide); 1912 if (hi->verinfo.vertree != NULL && hide) 1913 { 1914 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 1915 goto nondefault; 1916 } 1917 } 1918 if (hi->verinfo.vertree != NULL 1919 && strcmp (p + 1 + (p[1] == '@'), hi->verinfo.vertree->name) != 0) 1920 goto nondefault; 1921 } 1922 1923 if (! override) 1924 { 1925 /* Add the default symbol if not performing a relocatable link. */ 1926 if (! bfd_link_relocatable (info)) 1927 { 1928 bh = &hi->root; 1929 if (! (_bfd_generic_link_add_one_symbol 1930 (info, abfd, shortname, BSF_INDIRECT, 1931 bfd_ind_section_ptr, 1932 0, name, FALSE, collect, &bh))) 1933 return FALSE; 1934 hi = (struct elf_link_hash_entry *) bh; 1935 } 1936 } 1937 else 1938 { 1939 /* In this case the symbol named SHORTNAME is overriding the 1940 indirect symbol we want to add. We were planning on making 1941 SHORTNAME an indirect symbol referring to NAME. SHORTNAME 1942 is the name without a version. NAME is the fully versioned 1943 name, and it is the default version. 1944 1945 Overriding means that we already saw a definition for the 1946 symbol SHORTNAME in a regular object, and it is overriding 1947 the symbol defined in the dynamic object. 1948 1949 When this happens, we actually want to change NAME, the 1950 symbol we just added, to refer to SHORTNAME. This will cause 1951 references to NAME in the shared object to become references 1952 to SHORTNAME in the regular object. This is what we expect 1953 when we override a function in a shared object: that the 1954 references in the shared object will be mapped to the 1955 definition in the regular object. */ 1956 1957 while (hi->root.type == bfd_link_hash_indirect 1958 || hi->root.type == bfd_link_hash_warning) 1959 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1960 1961 h->root.type = bfd_link_hash_indirect; 1962 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1963 if (h->def_dynamic) 1964 { 1965 h->def_dynamic = 0; 1966 hi->ref_dynamic = 1; 1967 if (hi->ref_regular 1968 || hi->def_regular) 1969 { 1970 if (! bfd_elf_link_record_dynamic_symbol (info, hi)) 1971 return FALSE; 1972 } 1973 } 1974 1975 /* Now set HI to H, so that the following code will set the 1976 other fields correctly. */ 1977 hi = h; 1978 } 1979 1980 /* Check if HI is a warning symbol. */ 1981 if (hi->root.type == bfd_link_hash_warning) 1982 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1983 1984 /* If there is a duplicate definition somewhere, then HI may not 1985 point to an indirect symbol. We will have reported an error to 1986 the user in that case. */ 1987 1988 if (hi->root.type == bfd_link_hash_indirect) 1989 { 1990 struct elf_link_hash_entry *ht; 1991 1992 ht = (struct elf_link_hash_entry *) hi->root.u.i.link; 1993 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); 1994 1995 /* A reference to the SHORTNAME symbol from a dynamic library 1996 will be satisfied by the versioned symbol at runtime. In 1997 effect, we have a reference to the versioned symbol. */ 1998 ht->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 1999 hi->dynamic_def |= ht->dynamic_def; 2000 2001 /* See if the new flags lead us to realize that the symbol must 2002 be dynamic. */ 2003 if (! *dynsym) 2004 { 2005 if (! dynamic) 2006 { 2007 if (! bfd_link_executable (info) 2008 || hi->def_dynamic 2009 || hi->ref_dynamic) 2010 *dynsym = TRUE; 2011 } 2012 else 2013 { 2014 if (hi->ref_regular) 2015 *dynsym = TRUE; 2016 } 2017 } 2018 } 2019 2020 /* We also need to define an indirection from the nondefault version 2021 of the symbol. */ 2022 2023 nondefault: 2024 len = strlen (name); 2025 shortname = (char *) bfd_hash_allocate (&info->hash->table, len); 2026 if (shortname == NULL) 2027 return FALSE; 2028 memcpy (shortname, name, shortlen); 2029 memcpy (shortname + shortlen, p + 1, len - shortlen); 2030 2031 /* Once again, merge with any existing symbol. */ 2032 type_change_ok = FALSE; 2033 size_change_ok = FALSE; 2034 tmp_sec = sec; 2035 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &tmp_sec, &value, 2036 &hi, poldbfd, NULL, NULL, &skip, &override, 2037 &type_change_ok, &size_change_ok, &matched)) 2038 return FALSE; 2039 2040 if (skip) 2041 return TRUE; 2042 2043 if (override) 2044 { 2045 /* Here SHORTNAME is a versioned name, so we don't expect to see 2046 the type of override we do in the case above unless it is 2047 overridden by a versioned definition. */ 2048 if (hi->root.type != bfd_link_hash_defined 2049 && hi->root.type != bfd_link_hash_defweak) 2050 _bfd_error_handler 2051 /* xgettext:c-format */ 2052 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), 2053 abfd, shortname); 2054 } 2055 else 2056 { 2057 bh = &hi->root; 2058 if (! (_bfd_generic_link_add_one_symbol 2059 (info, abfd, shortname, BSF_INDIRECT, 2060 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) 2061 return FALSE; 2062 hi = (struct elf_link_hash_entry *) bh; 2063 2064 /* If there is a duplicate definition somewhere, then HI may not 2065 point to an indirect symbol. We will have reported an error 2066 to the user in that case. */ 2067 2068 if (hi->root.type == bfd_link_hash_indirect) 2069 { 2070 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 2071 h->ref_dynamic_nonweak |= hi->ref_dynamic_nonweak; 2072 hi->dynamic_def |= h->dynamic_def; 2073 2074 /* See if the new flags lead us to realize that the symbol 2075 must be dynamic. */ 2076 if (! *dynsym) 2077 { 2078 if (! dynamic) 2079 { 2080 if (! bfd_link_executable (info) 2081 || hi->ref_dynamic) 2082 *dynsym = TRUE; 2083 } 2084 else 2085 { 2086 if (hi->ref_regular) 2087 *dynsym = TRUE; 2088 } 2089 } 2090 } 2091 } 2092 2093 return TRUE; 2094 } 2095 2096 /* This routine is used to export all defined symbols into the dynamic 2097 symbol table. It is called via elf_link_hash_traverse. */ 2098 2099 static bfd_boolean 2100 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) 2101 { 2102 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2103 2104 /* Ignore indirect symbols. These are added by the versioning code. */ 2105 if (h->root.type == bfd_link_hash_indirect) 2106 return TRUE; 2107 2108 /* Ignore this if we won't export it. */ 2109 if (!eif->info->export_dynamic && !h->dynamic) 2110 return TRUE; 2111 2112 if (h->dynindx == -1 2113 && (h->def_regular || h->ref_regular) 2114 && ! bfd_hide_sym_by_version (eif->info->version_info, 2115 h->root.root.string)) 2116 { 2117 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2118 { 2119 eif->failed = TRUE; 2120 return FALSE; 2121 } 2122 } 2123 2124 return TRUE; 2125 } 2126 2127 /* Look through the symbols which are defined in other shared 2128 libraries and referenced here. Update the list of version 2129 dependencies. This will be put into the .gnu.version_r section. 2130 This function is called via elf_link_hash_traverse. */ 2131 2132 static bfd_boolean 2133 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, 2134 void *data) 2135 { 2136 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; 2137 Elf_Internal_Verneed *t; 2138 Elf_Internal_Vernaux *a; 2139 bfd_size_type amt; 2140 2141 /* We only care about symbols defined in shared objects with version 2142 information. */ 2143 if (!h->def_dynamic 2144 || h->def_regular 2145 || h->dynindx == -1 2146 || h->verinfo.verdef == NULL 2147 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 2148 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 2149 return TRUE; 2150 2151 /* See if we already know about this version. */ 2152 for (t = elf_tdata (rinfo->info->output_bfd)->verref; 2153 t != NULL; 2154 t = t->vn_nextref) 2155 { 2156 if (t->vn_bfd != h->verinfo.verdef->vd_bfd) 2157 continue; 2158 2159 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 2160 if (a->vna_nodename == h->verinfo.verdef->vd_nodename) 2161 return TRUE; 2162 2163 break; 2164 } 2165 2166 /* This is a new version. Add it to tree we are building. */ 2167 2168 if (t == NULL) 2169 { 2170 amt = sizeof *t; 2171 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); 2172 if (t == NULL) 2173 { 2174 rinfo->failed = TRUE; 2175 return FALSE; 2176 } 2177 2178 t->vn_bfd = h->verinfo.verdef->vd_bfd; 2179 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; 2180 elf_tdata (rinfo->info->output_bfd)->verref = t; 2181 } 2182 2183 amt = sizeof *a; 2184 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); 2185 if (a == NULL) 2186 { 2187 rinfo->failed = TRUE; 2188 return FALSE; 2189 } 2190 2191 /* Note that we are copying a string pointer here, and testing it 2192 above. If bfd_elf_string_from_elf_section is ever changed to 2193 discard the string data when low in memory, this will have to be 2194 fixed. */ 2195 a->vna_nodename = h->verinfo.verdef->vd_nodename; 2196 2197 a->vna_flags = h->verinfo.verdef->vd_flags; 2198 a->vna_nextptr = t->vn_auxptr; 2199 2200 h->verinfo.verdef->vd_exp_refno = rinfo->vers; 2201 ++rinfo->vers; 2202 2203 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; 2204 2205 t->vn_auxptr = a; 2206 2207 return TRUE; 2208 } 2209 2210 /* Figure out appropriate versions for all the symbols. We may not 2211 have the version number script until we have read all of the input 2212 files, so until that point we don't know which symbols should be 2213 local. This function is called via elf_link_hash_traverse. */ 2214 2215 static bfd_boolean 2216 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) 2217 { 2218 struct elf_info_failed *sinfo; 2219 struct bfd_link_info *info; 2220 const struct elf_backend_data *bed; 2221 struct elf_info_failed eif; 2222 char *p; 2223 2224 sinfo = (struct elf_info_failed *) data; 2225 info = sinfo->info; 2226 2227 /* Fix the symbol flags. */ 2228 eif.failed = FALSE; 2229 eif.info = info; 2230 if (! _bfd_elf_fix_symbol_flags (h, &eif)) 2231 { 2232 if (eif.failed) 2233 sinfo->failed = TRUE; 2234 return FALSE; 2235 } 2236 2237 /* We only need version numbers for symbols defined in regular 2238 objects. */ 2239 if (!h->def_regular) 2240 return TRUE; 2241 2242 bed = get_elf_backend_data (info->output_bfd); 2243 p = strchr (h->root.root.string, ELF_VER_CHR); 2244 if (p != NULL && h->verinfo.vertree == NULL) 2245 { 2246 struct bfd_elf_version_tree *t; 2247 2248 ++p; 2249 if (*p == ELF_VER_CHR) 2250 ++p; 2251 2252 /* If there is no version string, we can just return out. */ 2253 if (*p == '\0') 2254 return TRUE; 2255 2256 /* Look for the version. If we find it, it is no longer weak. */ 2257 for (t = sinfo->info->version_info; t != NULL; t = t->next) 2258 { 2259 if (strcmp (t->name, p) == 0) 2260 { 2261 size_t len; 2262 char *alc; 2263 struct bfd_elf_version_expr *d; 2264 2265 len = p - h->root.root.string; 2266 alc = (char *) bfd_malloc (len); 2267 if (alc == NULL) 2268 { 2269 sinfo->failed = TRUE; 2270 return FALSE; 2271 } 2272 memcpy (alc, h->root.root.string, len - 1); 2273 alc[len - 1] = '\0'; 2274 if (alc[len - 2] == ELF_VER_CHR) 2275 alc[len - 2] = '\0'; 2276 2277 h->verinfo.vertree = t; 2278 t->used = TRUE; 2279 d = NULL; 2280 2281 if (t->globals.list != NULL) 2282 d = (*t->match) (&t->globals, NULL, alc); 2283 2284 /* See if there is anything to force this symbol to 2285 local scope. */ 2286 if (d == NULL && t->locals.list != NULL) 2287 { 2288 d = (*t->match) (&t->locals, NULL, alc); 2289 if (d != NULL 2290 && h->dynindx != -1 2291 && ! info->export_dynamic) 2292 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2293 } 2294 2295 free (alc); 2296 break; 2297 } 2298 } 2299 2300 /* If we are building an application, we need to create a 2301 version node for this version. */ 2302 if (t == NULL && bfd_link_executable (info)) 2303 { 2304 struct bfd_elf_version_tree **pp; 2305 int version_index; 2306 2307 /* If we aren't going to export this symbol, we don't need 2308 to worry about it. */ 2309 if (h->dynindx == -1) 2310 return TRUE; 2311 2312 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, 2313 sizeof *t); 2314 if (t == NULL) 2315 { 2316 sinfo->failed = TRUE; 2317 return FALSE; 2318 } 2319 2320 t->name = p; 2321 t->name_indx = (unsigned int) -1; 2322 t->used = TRUE; 2323 2324 version_index = 1; 2325 /* Don't count anonymous version tag. */ 2326 if (sinfo->info->version_info != NULL 2327 && sinfo->info->version_info->vernum == 0) 2328 version_index = 0; 2329 for (pp = &sinfo->info->version_info; 2330 *pp != NULL; 2331 pp = &(*pp)->next) 2332 ++version_index; 2333 t->vernum = version_index; 2334 2335 *pp = t; 2336 2337 h->verinfo.vertree = t; 2338 } 2339 else if (t == NULL) 2340 { 2341 /* We could not find the version for a symbol when 2342 generating a shared archive. Return an error. */ 2343 _bfd_error_handler 2344 /* xgettext:c-format */ 2345 (_("%B: version node not found for symbol %s"), 2346 info->output_bfd, h->root.root.string); 2347 bfd_set_error (bfd_error_bad_value); 2348 sinfo->failed = TRUE; 2349 return FALSE; 2350 } 2351 } 2352 2353 /* If we don't have a version for this symbol, see if we can find 2354 something. */ 2355 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) 2356 { 2357 bfd_boolean hide; 2358 2359 h->verinfo.vertree 2360 = bfd_find_version_for_sym (sinfo->info->version_info, 2361 h->root.root.string, &hide); 2362 if (h->verinfo.vertree != NULL && hide) 2363 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2364 } 2365 2366 return TRUE; 2367 } 2368 2369 /* Read and swap the relocs from the section indicated by SHDR. This 2370 may be either a REL or a RELA section. The relocations are 2371 translated into RELA relocations and stored in INTERNAL_RELOCS, 2372 which should have already been allocated to contain enough space. 2373 The EXTERNAL_RELOCS are a buffer where the external form of the 2374 relocations should be stored. 2375 2376 Returns FALSE if something goes wrong. */ 2377 2378 static bfd_boolean 2379 elf_link_read_relocs_from_section (bfd *abfd, 2380 asection *sec, 2381 Elf_Internal_Shdr *shdr, 2382 void *external_relocs, 2383 Elf_Internal_Rela *internal_relocs) 2384 { 2385 const struct elf_backend_data *bed; 2386 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 2387 const bfd_byte *erela; 2388 const bfd_byte *erelaend; 2389 Elf_Internal_Rela *irela; 2390 Elf_Internal_Shdr *symtab_hdr; 2391 size_t nsyms; 2392 2393 /* Position ourselves at the start of the section. */ 2394 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) 2395 return FALSE; 2396 2397 /* Read the relocations. */ 2398 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) 2399 return FALSE; 2400 2401 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2402 nsyms = NUM_SHDR_ENTRIES (symtab_hdr); 2403 2404 bed = get_elf_backend_data (abfd); 2405 2406 /* Convert the external relocations to the internal format. */ 2407 if (shdr->sh_entsize == bed->s->sizeof_rel) 2408 swap_in = bed->s->swap_reloc_in; 2409 else if (shdr->sh_entsize == bed->s->sizeof_rela) 2410 swap_in = bed->s->swap_reloca_in; 2411 else 2412 { 2413 bfd_set_error (bfd_error_wrong_format); 2414 return FALSE; 2415 } 2416 2417 erela = (const bfd_byte *) external_relocs; 2418 erelaend = erela + shdr->sh_size; 2419 irela = internal_relocs; 2420 while (erela < erelaend) 2421 { 2422 bfd_vma r_symndx; 2423 2424 (*swap_in) (abfd, erela, irela); 2425 r_symndx = ELF32_R_SYM (irela->r_info); 2426 if (bed->s->arch_size == 64) 2427 r_symndx >>= 24; 2428 if (nsyms > 0) 2429 { 2430 if ((size_t) r_symndx >= nsyms) 2431 { 2432 _bfd_error_handler 2433 /* xgettext:c-format */ 2434 (_("%B: bad reloc symbol index (%#Lx >= %#lx)" 2435 " for offset %#Lx in section `%A'"), 2436 abfd, r_symndx, (unsigned long) nsyms, 2437 irela->r_offset, sec); 2438 bfd_set_error (bfd_error_bad_value); 2439 return FALSE; 2440 } 2441 } 2442 else if (r_symndx != STN_UNDEF) 2443 { 2444 _bfd_error_handler 2445 /* xgettext:c-format */ 2446 (_("%B: non-zero symbol index (%#Lx)" 2447 " for offset %#Lx in section `%A'" 2448 " when the object file has no symbol table"), 2449 abfd, r_symndx, 2450 irela->r_offset, sec); 2451 bfd_set_error (bfd_error_bad_value); 2452 return FALSE; 2453 } 2454 irela += bed->s->int_rels_per_ext_rel; 2455 erela += shdr->sh_entsize; 2456 } 2457 2458 return TRUE; 2459 } 2460 2461 /* Read and swap the relocs for a section O. They may have been 2462 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are 2463 not NULL, they are used as buffers to read into. They are known to 2464 be large enough. If the INTERNAL_RELOCS relocs argument is NULL, 2465 the return value is allocated using either malloc or bfd_alloc, 2466 according to the KEEP_MEMORY argument. If O has two relocation 2467 sections (both REL and RELA relocations), then the REL_HDR 2468 relocations will appear first in INTERNAL_RELOCS, followed by the 2469 RELA_HDR relocations. */ 2470 2471 Elf_Internal_Rela * 2472 _bfd_elf_link_read_relocs (bfd *abfd, 2473 asection *o, 2474 void *external_relocs, 2475 Elf_Internal_Rela *internal_relocs, 2476 bfd_boolean keep_memory) 2477 { 2478 void *alloc1 = NULL; 2479 Elf_Internal_Rela *alloc2 = NULL; 2480 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2481 struct bfd_elf_section_data *esdo = elf_section_data (o); 2482 Elf_Internal_Rela *internal_rela_relocs; 2483 2484 if (esdo->relocs != NULL) 2485 return esdo->relocs; 2486 2487 if (o->reloc_count == 0) 2488 return NULL; 2489 2490 if (internal_relocs == NULL) 2491 { 2492 bfd_size_type size; 2493 2494 size = (bfd_size_type) o->reloc_count * sizeof (Elf_Internal_Rela); 2495 if (keep_memory) 2496 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); 2497 else 2498 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); 2499 if (internal_relocs == NULL) 2500 goto error_return; 2501 } 2502 2503 if (external_relocs == NULL) 2504 { 2505 bfd_size_type size = 0; 2506 2507 if (esdo->rel.hdr) 2508 size += esdo->rel.hdr->sh_size; 2509 if (esdo->rela.hdr) 2510 size += esdo->rela.hdr->sh_size; 2511 2512 alloc1 = bfd_malloc (size); 2513 if (alloc1 == NULL) 2514 goto error_return; 2515 external_relocs = alloc1; 2516 } 2517 2518 internal_rela_relocs = internal_relocs; 2519 if (esdo->rel.hdr) 2520 { 2521 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, 2522 external_relocs, 2523 internal_relocs)) 2524 goto error_return; 2525 external_relocs = (((bfd_byte *) external_relocs) 2526 + esdo->rel.hdr->sh_size); 2527 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) 2528 * bed->s->int_rels_per_ext_rel); 2529 } 2530 2531 if (esdo->rela.hdr 2532 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, 2533 external_relocs, 2534 internal_rela_relocs))) 2535 goto error_return; 2536 2537 /* Cache the results for next time, if we can. */ 2538 if (keep_memory) 2539 esdo->relocs = internal_relocs; 2540 2541 if (alloc1 != NULL) 2542 free (alloc1); 2543 2544 /* Don't free alloc2, since if it was allocated we are passing it 2545 back (under the name of internal_relocs). */ 2546 2547 return internal_relocs; 2548 2549 error_return: 2550 if (alloc1 != NULL) 2551 free (alloc1); 2552 if (alloc2 != NULL) 2553 { 2554 if (keep_memory) 2555 bfd_release (abfd, alloc2); 2556 else 2557 free (alloc2); 2558 } 2559 return NULL; 2560 } 2561 2562 /* Compute the size of, and allocate space for, REL_HDR which is the 2563 section header for a section containing relocations for O. */ 2564 2565 static bfd_boolean 2566 _bfd_elf_link_size_reloc_section (bfd *abfd, 2567 struct bfd_elf_section_reloc_data *reldata) 2568 { 2569 Elf_Internal_Shdr *rel_hdr = reldata->hdr; 2570 2571 /* That allows us to calculate the size of the section. */ 2572 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; 2573 2574 /* The contents field must last into write_object_contents, so we 2575 allocate it with bfd_alloc rather than malloc. Also since we 2576 cannot be sure that the contents will actually be filled in, 2577 we zero the allocated space. */ 2578 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); 2579 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) 2580 return FALSE; 2581 2582 if (reldata->hashes == NULL && reldata->count) 2583 { 2584 struct elf_link_hash_entry **p; 2585 2586 p = ((struct elf_link_hash_entry **) 2587 bfd_zmalloc (reldata->count * sizeof (*p))); 2588 if (p == NULL) 2589 return FALSE; 2590 2591 reldata->hashes = p; 2592 } 2593 2594 return TRUE; 2595 } 2596 2597 /* Copy the relocations indicated by the INTERNAL_RELOCS (which 2598 originated from the section given by INPUT_REL_HDR) to the 2599 OUTPUT_BFD. */ 2600 2601 bfd_boolean 2602 _bfd_elf_link_output_relocs (bfd *output_bfd, 2603 asection *input_section, 2604 Elf_Internal_Shdr *input_rel_hdr, 2605 Elf_Internal_Rela *internal_relocs, 2606 struct elf_link_hash_entry **rel_hash 2607 ATTRIBUTE_UNUSED) 2608 { 2609 Elf_Internal_Rela *irela; 2610 Elf_Internal_Rela *irelaend; 2611 bfd_byte *erel; 2612 struct bfd_elf_section_reloc_data *output_reldata; 2613 asection *output_section; 2614 const struct elf_backend_data *bed; 2615 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 2616 struct bfd_elf_section_data *esdo; 2617 2618 output_section = input_section->output_section; 2619 2620 bed = get_elf_backend_data (output_bfd); 2621 esdo = elf_section_data (output_section); 2622 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2623 { 2624 output_reldata = &esdo->rel; 2625 swap_out = bed->s->swap_reloc_out; 2626 } 2627 else if (esdo->rela.hdr 2628 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2629 { 2630 output_reldata = &esdo->rela; 2631 swap_out = bed->s->swap_reloca_out; 2632 } 2633 else 2634 { 2635 _bfd_error_handler 2636 /* xgettext:c-format */ 2637 (_("%B: relocation size mismatch in %B section %A"), 2638 output_bfd, input_section->owner, input_section); 2639 bfd_set_error (bfd_error_wrong_format); 2640 return FALSE; 2641 } 2642 2643 erel = output_reldata->hdr->contents; 2644 erel += output_reldata->count * input_rel_hdr->sh_entsize; 2645 irela = internal_relocs; 2646 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) 2647 * bed->s->int_rels_per_ext_rel); 2648 while (irela < irelaend) 2649 { 2650 (*swap_out) (output_bfd, irela, erel); 2651 irela += bed->s->int_rels_per_ext_rel; 2652 erel += input_rel_hdr->sh_entsize; 2653 } 2654 2655 /* Bump the counter, so that we know where to add the next set of 2656 relocations. */ 2657 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); 2658 2659 return TRUE; 2660 } 2661 2662 /* Make weak undefined symbols in PIE dynamic. */ 2663 2664 bfd_boolean 2665 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, 2666 struct elf_link_hash_entry *h) 2667 { 2668 if (bfd_link_pie (info) 2669 && h->dynindx == -1 2670 && h->root.type == bfd_link_hash_undefweak) 2671 return bfd_elf_link_record_dynamic_symbol (info, h); 2672 2673 return TRUE; 2674 } 2675 2676 /* Fix up the flags for a symbol. This handles various cases which 2677 can only be fixed after all the input files are seen. This is 2678 currently called by both adjust_dynamic_symbol and 2679 assign_sym_version, which is unnecessary but perhaps more robust in 2680 the face of future changes. */ 2681 2682 static bfd_boolean 2683 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, 2684 struct elf_info_failed *eif) 2685 { 2686 const struct elf_backend_data *bed; 2687 2688 /* If this symbol was mentioned in a non-ELF file, try to set 2689 DEF_REGULAR and REF_REGULAR correctly. This is the only way to 2690 permit a non-ELF file to correctly refer to a symbol defined in 2691 an ELF dynamic object. */ 2692 if (h->non_elf) 2693 { 2694 while (h->root.type == bfd_link_hash_indirect) 2695 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2696 2697 if (h->root.type != bfd_link_hash_defined 2698 && h->root.type != bfd_link_hash_defweak) 2699 { 2700 h->ref_regular = 1; 2701 h->ref_regular_nonweak = 1; 2702 } 2703 else 2704 { 2705 if (h->root.u.def.section->owner != NULL 2706 && (bfd_get_flavour (h->root.u.def.section->owner) 2707 == bfd_target_elf_flavour)) 2708 { 2709 h->ref_regular = 1; 2710 h->ref_regular_nonweak = 1; 2711 } 2712 else 2713 h->def_regular = 1; 2714 } 2715 2716 if (h->dynindx == -1 2717 && (h->def_dynamic 2718 || h->ref_dynamic)) 2719 { 2720 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2721 { 2722 eif->failed = TRUE; 2723 return FALSE; 2724 } 2725 } 2726 } 2727 else 2728 { 2729 /* Unfortunately, NON_ELF is only correct if the symbol 2730 was first seen in a non-ELF file. Fortunately, if the symbol 2731 was first seen in an ELF file, we're probably OK unless the 2732 symbol was defined in a non-ELF file. Catch that case here. 2733 FIXME: We're still in trouble if the symbol was first seen in 2734 a dynamic object, and then later in a non-ELF regular object. */ 2735 if ((h->root.type == bfd_link_hash_defined 2736 || h->root.type == bfd_link_hash_defweak) 2737 && !h->def_regular 2738 && (h->root.u.def.section->owner != NULL 2739 ? (bfd_get_flavour (h->root.u.def.section->owner) 2740 != bfd_target_elf_flavour) 2741 : (bfd_is_abs_section (h->root.u.def.section) 2742 && !h->def_dynamic))) 2743 h->def_regular = 1; 2744 } 2745 2746 /* Backend specific symbol fixup. */ 2747 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 2748 if (bed->elf_backend_fixup_symbol 2749 && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) 2750 return FALSE; 2751 2752 /* If this is a final link, and the symbol was defined as a common 2753 symbol in a regular object file, and there was no definition in 2754 any dynamic object, then the linker will have allocated space for 2755 the symbol in a common section but the DEF_REGULAR 2756 flag will not have been set. */ 2757 if (h->root.type == bfd_link_hash_defined 2758 && !h->def_regular 2759 && h->ref_regular 2760 && !h->def_dynamic 2761 && (h->root.u.def.section->owner->flags & (DYNAMIC | BFD_PLUGIN)) == 0) 2762 h->def_regular = 1; 2763 2764 /* If a weak undefined symbol has non-default visibility, we also 2765 hide it from the dynamic linker. */ 2766 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2767 && h->root.type == bfd_link_hash_undefweak) 2768 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2769 2770 /* A hidden versioned symbol in executable should be forced local if 2771 it is is locally defined, not referenced by shared library and not 2772 exported. */ 2773 else if (bfd_link_executable (eif->info) 2774 && h->versioned == versioned_hidden 2775 && !eif->info->export_dynamic 2776 && !h->dynamic 2777 && !h->ref_dynamic 2778 && h->def_regular) 2779 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2780 2781 /* If -Bsymbolic was used (which means to bind references to global 2782 symbols to the definition within the shared object), and this 2783 symbol was defined in a regular object, then it actually doesn't 2784 need a PLT entry. Likewise, if the symbol has non-default 2785 visibility. If the symbol has hidden or internal visibility, we 2786 will force it local. */ 2787 else if (h->needs_plt 2788 && bfd_link_pic (eif->info) 2789 && is_elf_hash_table (eif->info->hash) 2790 && (SYMBOLIC_BIND (eif->info, h) 2791 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2792 && h->def_regular) 2793 { 2794 bfd_boolean force_local; 2795 2796 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 2797 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); 2798 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); 2799 } 2800 2801 /* If this is a weak defined symbol in a dynamic object, and we know 2802 the real definition in the dynamic object, copy interesting flags 2803 over to the real definition. */ 2804 if (h->is_weakalias) 2805 { 2806 struct elf_link_hash_entry *def = weakdef (h); 2807 while (def->root.type == bfd_link_hash_indirect) 2808 def = (struct elf_link_hash_entry *) def->root.u.i.link; 2809 2810 /* If the real definition is defined by a regular object file, 2811 don't do anything special. See the longer description in 2812 _bfd_elf_adjust_dynamic_symbol, below. */ 2813 if (def->def_regular) 2814 { 2815 h = def; 2816 while ((h = h->u.alias) != def) 2817 h->is_weakalias = 0; 2818 } 2819 else 2820 { 2821 while (h->root.type == bfd_link_hash_indirect) 2822 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2823 BFD_ASSERT (h->root.type == bfd_link_hash_defined 2824 || h->root.type == bfd_link_hash_defweak); 2825 BFD_ASSERT (def->def_dynamic); 2826 BFD_ASSERT (def->root.type == bfd_link_hash_defined); 2827 (*bed->elf_backend_copy_indirect_symbol) (eif->info, def, h); 2828 } 2829 } 2830 2831 return TRUE; 2832 } 2833 2834 /* Make the backend pick a good value for a dynamic symbol. This is 2835 called via elf_link_hash_traverse, and also calls itself 2836 recursively. */ 2837 2838 static bfd_boolean 2839 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) 2840 { 2841 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2842 struct elf_link_hash_table *htab; 2843 const struct elf_backend_data *bed; 2844 2845 if (! is_elf_hash_table (eif->info->hash)) 2846 return FALSE; 2847 2848 /* Ignore indirect symbols. These are added by the versioning code. */ 2849 if (h->root.type == bfd_link_hash_indirect) 2850 return TRUE; 2851 2852 /* Fix the symbol flags. */ 2853 if (! _bfd_elf_fix_symbol_flags (h, eif)) 2854 return FALSE; 2855 2856 htab = elf_hash_table (eif->info); 2857 bed = get_elf_backend_data (htab->dynobj); 2858 2859 if (h->root.type == bfd_link_hash_undefweak) 2860 { 2861 if (eif->info->dynamic_undefined_weak == 0) 2862 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2863 else if (eif->info->dynamic_undefined_weak > 0 2864 && h->ref_regular 2865 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 2866 && !bfd_hide_sym_by_version (eif->info->version_info, 2867 h->root.root.string)) 2868 { 2869 if (!bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2870 { 2871 eif->failed = TRUE; 2872 return FALSE; 2873 } 2874 } 2875 } 2876 2877 /* If this symbol does not require a PLT entry, and it is not 2878 defined by a dynamic object, or is not referenced by a regular 2879 object, ignore it. We do have to handle a weak defined symbol, 2880 even if no regular object refers to it, if we decided to add it 2881 to the dynamic symbol table. FIXME: Do we normally need to worry 2882 about symbols which are defined by one dynamic object and 2883 referenced by another one? */ 2884 if (!h->needs_plt 2885 && h->type != STT_GNU_IFUNC 2886 && (h->def_regular 2887 || !h->def_dynamic 2888 || (!h->ref_regular 2889 && (!h->is_weakalias || weakdef (h)->dynindx == -1)))) 2890 { 2891 h->plt = elf_hash_table (eif->info)->init_plt_offset; 2892 return TRUE; 2893 } 2894 2895 /* If we've already adjusted this symbol, don't do it again. This 2896 can happen via a recursive call. */ 2897 if (h->dynamic_adjusted) 2898 return TRUE; 2899 2900 /* Don't look at this symbol again. Note that we must set this 2901 after checking the above conditions, because we may look at a 2902 symbol once, decide not to do anything, and then get called 2903 recursively later after REF_REGULAR is set below. */ 2904 h->dynamic_adjusted = 1; 2905 2906 /* If this is a weak definition, and we know a real definition, and 2907 the real symbol is not itself defined by a regular object file, 2908 then get a good value for the real definition. We handle the 2909 real symbol first, for the convenience of the backend routine. 2910 2911 Note that there is a confusing case here. If the real definition 2912 is defined by a regular object file, we don't get the real symbol 2913 from the dynamic object, but we do get the weak symbol. If the 2914 processor backend uses a COPY reloc, then if some routine in the 2915 dynamic object changes the real symbol, we will not see that 2916 change in the corresponding weak symbol. This is the way other 2917 ELF linkers work as well, and seems to be a result of the shared 2918 library model. 2919 2920 I will clarify this issue. Most SVR4 shared libraries define the 2921 variable _timezone and define timezone as a weak synonym. The 2922 tzset call changes _timezone. If you write 2923 extern int timezone; 2924 int _timezone = 5; 2925 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } 2926 you might expect that, since timezone is a synonym for _timezone, 2927 the same number will print both times. However, if the processor 2928 backend uses a COPY reloc, then actually timezone will be copied 2929 into your process image, and, since you define _timezone 2930 yourself, _timezone will not. Thus timezone and _timezone will 2931 wind up at different memory locations. The tzset call will set 2932 _timezone, leaving timezone unchanged. */ 2933 2934 if (h->is_weakalias) 2935 { 2936 struct elf_link_hash_entry *def = weakdef (h); 2937 2938 /* If we get to this point, there is an implicit reference to 2939 the alias by a regular object file via the weak symbol H. */ 2940 def->ref_regular = 1; 2941 2942 /* Ensure that the backend adjust_dynamic_symbol function sees 2943 the strong alias before H by recursively calling ourselves. */ 2944 if (!_bfd_elf_adjust_dynamic_symbol (def, eif)) 2945 return FALSE; 2946 } 2947 2948 /* If a symbol has no type and no size and does not require a PLT 2949 entry, then we are probably about to do the wrong thing here: we 2950 are probably going to create a COPY reloc for an empty object. 2951 This case can arise when a shared object is built with assembly 2952 code, and the assembly code fails to set the symbol type. */ 2953 if (h->size == 0 2954 && h->type == STT_NOTYPE 2955 && !h->needs_plt) 2956 _bfd_error_handler 2957 (_("warning: type and size of dynamic symbol `%s' are not defined"), 2958 h->root.root.string); 2959 2960 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) 2961 { 2962 eif->failed = TRUE; 2963 return FALSE; 2964 } 2965 2966 return TRUE; 2967 } 2968 2969 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, 2970 DYNBSS. */ 2971 2972 bfd_boolean 2973 _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info, 2974 struct elf_link_hash_entry *h, 2975 asection *dynbss) 2976 { 2977 unsigned int power_of_two; 2978 bfd_vma mask; 2979 asection *sec = h->root.u.def.section; 2980 2981 /* The section alignment of the definition is the maximum alignment 2982 requirement of symbols defined in the section. Since we don't 2983 know the symbol alignment requirement, we start with the 2984 maximum alignment and check low bits of the symbol address 2985 for the minimum alignment. */ 2986 power_of_two = bfd_get_section_alignment (sec->owner, sec); 2987 mask = ((bfd_vma) 1 << power_of_two) - 1; 2988 while ((h->root.u.def.value & mask) != 0) 2989 { 2990 mask >>= 1; 2991 --power_of_two; 2992 } 2993 2994 if (power_of_two > bfd_get_section_alignment (dynbss->owner, 2995 dynbss)) 2996 { 2997 /* Adjust the section alignment if needed. */ 2998 if (! bfd_set_section_alignment (dynbss->owner, dynbss, 2999 power_of_two)) 3000 return FALSE; 3001 } 3002 3003 /* We make sure that the symbol will be aligned properly. */ 3004 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); 3005 3006 /* Define the symbol as being at this point in DYNBSS. */ 3007 h->root.u.def.section = dynbss; 3008 h->root.u.def.value = dynbss->size; 3009 3010 /* Increment the size of DYNBSS to make room for the symbol. */ 3011 dynbss->size += h->size; 3012 3013 /* No error if extern_protected_data is true. */ 3014 if (h->protected_def 3015 && (!info->extern_protected_data 3016 || (info->extern_protected_data < 0 3017 && !get_elf_backend_data (dynbss->owner)->extern_protected_data))) 3018 info->callbacks->einfo 3019 (_("%P: copy reloc against protected `%T' is dangerous\n"), 3020 h->root.root.string); 3021 3022 return TRUE; 3023 } 3024 3025 /* Adjust all external symbols pointing into SEC_MERGE sections 3026 to reflect the object merging within the sections. */ 3027 3028 static bfd_boolean 3029 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) 3030 { 3031 asection *sec; 3032 3033 if ((h->root.type == bfd_link_hash_defined 3034 || h->root.type == bfd_link_hash_defweak) 3035 && ((sec = h->root.u.def.section)->flags & SEC_MERGE) 3036 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 3037 { 3038 bfd *output_bfd = (bfd *) data; 3039 3040 h->root.u.def.value = 3041 _bfd_merged_section_offset (output_bfd, 3042 &h->root.u.def.section, 3043 elf_section_data (sec)->sec_info, 3044 h->root.u.def.value); 3045 } 3046 3047 return TRUE; 3048 } 3049 3050 /* Returns false if the symbol referred to by H should be considered 3051 to resolve local to the current module, and true if it should be 3052 considered to bind dynamically. */ 3053 3054 bfd_boolean 3055 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, 3056 struct bfd_link_info *info, 3057 bfd_boolean not_local_protected) 3058 { 3059 bfd_boolean binding_stays_local_p; 3060 const struct elf_backend_data *bed; 3061 struct elf_link_hash_table *hash_table; 3062 3063 if (h == NULL) 3064 return FALSE; 3065 3066 while (h->root.type == bfd_link_hash_indirect 3067 || h->root.type == bfd_link_hash_warning) 3068 h = (struct elf_link_hash_entry *) h->root.u.i.link; 3069 3070 /* If it was forced local, then clearly it's not dynamic. */ 3071 if (h->dynindx == -1) 3072 return FALSE; 3073 if (h->forced_local) 3074 return FALSE; 3075 3076 /* Identify the cases where name binding rules say that a 3077 visible symbol resolves locally. */ 3078 binding_stays_local_p = (bfd_link_executable (info) 3079 || SYMBOLIC_BIND (info, h)); 3080 3081 switch (ELF_ST_VISIBILITY (h->other)) 3082 { 3083 case STV_INTERNAL: 3084 case STV_HIDDEN: 3085 return FALSE; 3086 3087 case STV_PROTECTED: 3088 hash_table = elf_hash_table (info); 3089 if (!is_elf_hash_table (hash_table)) 3090 return FALSE; 3091 3092 bed = get_elf_backend_data (hash_table->dynobj); 3093 3094 /* Proper resolution for function pointer equality may require 3095 that these symbols perhaps be resolved dynamically, even though 3096 we should be resolving them to the current module. */ 3097 if (!not_local_protected || !bed->is_function_type (h->type)) 3098 binding_stays_local_p = TRUE; 3099 break; 3100 3101 default: 3102 break; 3103 } 3104 3105 /* If it isn't defined locally, then clearly it's dynamic. */ 3106 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 3107 return TRUE; 3108 3109 /* Otherwise, the symbol is dynamic if binding rules don't tell 3110 us that it remains local. */ 3111 return !binding_stays_local_p; 3112 } 3113 3114 /* Return true if the symbol referred to by H should be considered 3115 to resolve local to the current module, and false otherwise. Differs 3116 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of 3117 undefined symbols. The two functions are virtually identical except 3118 for the place where dynindx == -1 is tested. If that test is true, 3119 _bfd_elf_dynamic_symbol_p will say the symbol is local, while 3120 _bfd_elf_symbol_refs_local_p will say the symbol is local only for 3121 defined symbols. 3122 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as 3123 !_bfd_elf_symbol_refs_local_p, except that targets differ in their 3124 treatment of undefined weak symbols. For those that do not make 3125 undefined weak symbols dynamic, both functions may return false. */ 3126 3127 bfd_boolean 3128 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, 3129 struct bfd_link_info *info, 3130 bfd_boolean local_protected) 3131 { 3132 const struct elf_backend_data *bed; 3133 struct elf_link_hash_table *hash_table; 3134 3135 /* If it's a local sym, of course we resolve locally. */ 3136 if (h == NULL) 3137 return TRUE; 3138 3139 /* STV_HIDDEN or STV_INTERNAL ones must be local. */ 3140 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 3141 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 3142 return TRUE; 3143 3144 /* Forced local symbols resolve locally. */ 3145 if (h->forced_local) 3146 return TRUE; 3147 3148 /* Common symbols that become definitions don't get the DEF_REGULAR 3149 flag set, so test it first, and don't bail out. */ 3150 if (ELF_COMMON_DEF_P (h)) 3151 /* Do nothing. */; 3152 /* If we don't have a definition in a regular file, then we can't 3153 resolve locally. The sym is either undefined or dynamic. */ 3154 else if (!h->def_regular) 3155 return FALSE; 3156 3157 /* Non-dynamic symbols resolve locally. */ 3158 if (h->dynindx == -1) 3159 return TRUE; 3160 3161 /* At this point, we know the symbol is defined and dynamic. In an 3162 executable it must resolve locally, likewise when building symbolic 3163 shared libraries. */ 3164 if (bfd_link_executable (info) || SYMBOLIC_BIND (info, h)) 3165 return TRUE; 3166 3167 /* Now deal with defined dynamic symbols in shared libraries. Ones 3168 with default visibility might not resolve locally. */ 3169 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 3170 return FALSE; 3171 3172 hash_table = elf_hash_table (info); 3173 if (!is_elf_hash_table (hash_table)) 3174 return TRUE; 3175 3176 bed = get_elf_backend_data (hash_table->dynobj); 3177 3178 /* If extern_protected_data is false, STV_PROTECTED non-function 3179 symbols are local. */ 3180 if ((!info->extern_protected_data 3181 || (info->extern_protected_data < 0 3182 && !bed->extern_protected_data)) 3183 && !bed->is_function_type (h->type)) 3184 return TRUE; 3185 3186 /* Function pointer equality tests may require that STV_PROTECTED 3187 symbols be treated as dynamic symbols. If the address of a 3188 function not defined in an executable is set to that function's 3189 plt entry in the executable, then the address of the function in 3190 a shared library must also be the plt entry in the executable. */ 3191 return local_protected; 3192 } 3193 3194 /* Caches some TLS segment info, and ensures that the TLS segment vma is 3195 aligned. Returns the first TLS output section. */ 3196 3197 struct bfd_section * 3198 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) 3199 { 3200 struct bfd_section *sec, *tls; 3201 unsigned int align = 0; 3202 3203 for (sec = obfd->sections; sec != NULL; sec = sec->next) 3204 if ((sec->flags & SEC_THREAD_LOCAL) != 0) 3205 break; 3206 tls = sec; 3207 3208 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) 3209 if (sec->alignment_power > align) 3210 align = sec->alignment_power; 3211 3212 elf_hash_table (info)->tls_sec = tls; 3213 3214 /* Ensure the alignment of the first section is the largest alignment, 3215 so that the tls segment starts aligned. */ 3216 if (tls != NULL) 3217 tls->alignment_power = align; 3218 3219 return tls; 3220 } 3221 3222 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ 3223 static bfd_boolean 3224 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, 3225 Elf_Internal_Sym *sym) 3226 { 3227 const struct elf_backend_data *bed; 3228 3229 /* Local symbols do not count, but target specific ones might. */ 3230 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL 3231 && ELF_ST_BIND (sym->st_info) < STB_LOOS) 3232 return FALSE; 3233 3234 bed = get_elf_backend_data (abfd); 3235 /* Function symbols do not count. */ 3236 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) 3237 return FALSE; 3238 3239 /* If the section is undefined, then so is the symbol. */ 3240 if (sym->st_shndx == SHN_UNDEF) 3241 return FALSE; 3242 3243 /* If the symbol is defined in the common section, then 3244 it is a common definition and so does not count. */ 3245 if (bed->common_definition (sym)) 3246 return FALSE; 3247 3248 /* If the symbol is in a target specific section then we 3249 must rely upon the backend to tell us what it is. */ 3250 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) 3251 /* FIXME - this function is not coded yet: 3252 3253 return _bfd_is_global_symbol_definition (abfd, sym); 3254 3255 Instead for now assume that the definition is not global, 3256 Even if this is wrong, at least the linker will behave 3257 in the same way that it used to do. */ 3258 return FALSE; 3259 3260 return TRUE; 3261 } 3262 3263 /* Search the symbol table of the archive element of the archive ABFD 3264 whose archive map contains a mention of SYMDEF, and determine if 3265 the symbol is defined in this element. */ 3266 static bfd_boolean 3267 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) 3268 { 3269 Elf_Internal_Shdr * hdr; 3270 size_t symcount; 3271 size_t extsymcount; 3272 size_t extsymoff; 3273 Elf_Internal_Sym *isymbuf; 3274 Elf_Internal_Sym *isym; 3275 Elf_Internal_Sym *isymend; 3276 bfd_boolean result; 3277 3278 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 3279 if (abfd == NULL) 3280 return FALSE; 3281 3282 if (! bfd_check_format (abfd, bfd_object)) 3283 return FALSE; 3284 3285 /* Select the appropriate symbol table. If we don't know if the 3286 object file is an IR object, give linker LTO plugin a chance to 3287 get the correct symbol table. */ 3288 if (abfd->plugin_format == bfd_plugin_yes 3289 #if BFD_SUPPORTS_PLUGINS 3290 || (abfd->plugin_format == bfd_plugin_unknown 3291 && bfd_link_plugin_object_p (abfd)) 3292 #endif 3293 ) 3294 { 3295 /* Use the IR symbol table if the object has been claimed by 3296 plugin. */ 3297 abfd = abfd->plugin_dummy_bfd; 3298 hdr = &elf_tdata (abfd)->symtab_hdr; 3299 } 3300 else if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) 3301 hdr = &elf_tdata (abfd)->symtab_hdr; 3302 else 3303 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3304 3305 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 3306 3307 /* The sh_info field of the symtab header tells us where the 3308 external symbols start. We don't care about the local symbols. */ 3309 if (elf_bad_symtab (abfd)) 3310 { 3311 extsymcount = symcount; 3312 extsymoff = 0; 3313 } 3314 else 3315 { 3316 extsymcount = symcount - hdr->sh_info; 3317 extsymoff = hdr->sh_info; 3318 } 3319 3320 if (extsymcount == 0) 3321 return FALSE; 3322 3323 /* Read in the symbol table. */ 3324 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3325 NULL, NULL, NULL); 3326 if (isymbuf == NULL) 3327 return FALSE; 3328 3329 /* Scan the symbol table looking for SYMDEF. */ 3330 result = FALSE; 3331 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) 3332 { 3333 const char *name; 3334 3335 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3336 isym->st_name); 3337 if (name == NULL) 3338 break; 3339 3340 if (strcmp (name, symdef->name) == 0) 3341 { 3342 result = is_global_data_symbol_definition (abfd, isym); 3343 break; 3344 } 3345 } 3346 3347 free (isymbuf); 3348 3349 return result; 3350 } 3351 3352 /* Add an entry to the .dynamic table. */ 3353 3354 bfd_boolean 3355 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, 3356 bfd_vma tag, 3357 bfd_vma val) 3358 { 3359 struct elf_link_hash_table *hash_table; 3360 const struct elf_backend_data *bed; 3361 asection *s; 3362 bfd_size_type newsize; 3363 bfd_byte *newcontents; 3364 Elf_Internal_Dyn dyn; 3365 3366 hash_table = elf_hash_table (info); 3367 if (! is_elf_hash_table (hash_table)) 3368 return FALSE; 3369 3370 bed = get_elf_backend_data (hash_table->dynobj); 3371 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3372 BFD_ASSERT (s != NULL); 3373 3374 newsize = s->size + bed->s->sizeof_dyn; 3375 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); 3376 if (newcontents == NULL) 3377 return FALSE; 3378 3379 dyn.d_tag = tag; 3380 dyn.d_un.d_val = val; 3381 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); 3382 3383 s->size = newsize; 3384 s->contents = newcontents; 3385 3386 return TRUE; 3387 } 3388 3389 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, 3390 otherwise just check whether one already exists. Returns -1 on error, 3391 1 if a DT_NEEDED tag already exists, and 0 on success. */ 3392 3393 static int 3394 elf_add_dt_needed_tag (bfd *abfd, 3395 struct bfd_link_info *info, 3396 const char *soname, 3397 bfd_boolean do_it) 3398 { 3399 struct elf_link_hash_table *hash_table; 3400 size_t strindex; 3401 3402 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 3403 return -1; 3404 3405 hash_table = elf_hash_table (info); 3406 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); 3407 if (strindex == (size_t) -1) 3408 return -1; 3409 3410 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) 3411 { 3412 asection *sdyn; 3413 const struct elf_backend_data *bed; 3414 bfd_byte *extdyn; 3415 3416 bed = get_elf_backend_data (hash_table->dynobj); 3417 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3418 if (sdyn != NULL) 3419 for (extdyn = sdyn->contents; 3420 extdyn < sdyn->contents + sdyn->size; 3421 extdyn += bed->s->sizeof_dyn) 3422 { 3423 Elf_Internal_Dyn dyn; 3424 3425 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 3426 if (dyn.d_tag == DT_NEEDED 3427 && dyn.d_un.d_val == strindex) 3428 { 3429 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3430 return 1; 3431 } 3432 } 3433 } 3434 3435 if (do_it) 3436 { 3437 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) 3438 return -1; 3439 3440 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) 3441 return -1; 3442 } 3443 else 3444 /* We were just checking for existence of the tag. */ 3445 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3446 3447 return 0; 3448 } 3449 3450 /* Return true if SONAME is on the needed list between NEEDED and STOP 3451 (or the end of list if STOP is NULL), and needed by a library that 3452 will be loaded. */ 3453 3454 static bfd_boolean 3455 on_needed_list (const char *soname, 3456 struct bfd_link_needed_list *needed, 3457 struct bfd_link_needed_list *stop) 3458 { 3459 struct bfd_link_needed_list *look; 3460 for (look = needed; look != stop; look = look->next) 3461 if (strcmp (soname, look->name) == 0 3462 && ((elf_dyn_lib_class (look->by) & DYN_AS_NEEDED) == 0 3463 /* If needed by a library that itself is not directly 3464 needed, recursively check whether that library is 3465 indirectly needed. Since we add DT_NEEDED entries to 3466 the end of the list, library dependencies appear after 3467 the library. Therefore search prior to the current 3468 LOOK, preventing possible infinite recursion. */ 3469 || on_needed_list (elf_dt_name (look->by), needed, look))) 3470 return TRUE; 3471 3472 return FALSE; 3473 } 3474 3475 /* Sort symbol by value, section, and size. */ 3476 static int 3477 elf_sort_symbol (const void *arg1, const void *arg2) 3478 { 3479 const struct elf_link_hash_entry *h1; 3480 const struct elf_link_hash_entry *h2; 3481 bfd_signed_vma vdiff; 3482 3483 h1 = *(const struct elf_link_hash_entry **) arg1; 3484 h2 = *(const struct elf_link_hash_entry **) arg2; 3485 vdiff = h1->root.u.def.value - h2->root.u.def.value; 3486 if (vdiff != 0) 3487 return vdiff > 0 ? 1 : -1; 3488 else 3489 { 3490 int sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; 3491 if (sdiff != 0) 3492 return sdiff > 0 ? 1 : -1; 3493 } 3494 vdiff = h1->size - h2->size; 3495 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; 3496 } 3497 3498 /* This function is used to adjust offsets into .dynstr for 3499 dynamic symbols. This is called via elf_link_hash_traverse. */ 3500 3501 static bfd_boolean 3502 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) 3503 { 3504 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; 3505 3506 if (h->dynindx != -1) 3507 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); 3508 return TRUE; 3509 } 3510 3511 /* Assign string offsets in .dynstr, update all structures referencing 3512 them. */ 3513 3514 static bfd_boolean 3515 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) 3516 { 3517 struct elf_link_hash_table *hash_table = elf_hash_table (info); 3518 struct elf_link_local_dynamic_entry *entry; 3519 struct elf_strtab_hash *dynstr = hash_table->dynstr; 3520 bfd *dynobj = hash_table->dynobj; 3521 asection *sdyn; 3522 bfd_size_type size; 3523 const struct elf_backend_data *bed; 3524 bfd_byte *extdyn; 3525 3526 _bfd_elf_strtab_finalize (dynstr); 3527 size = _bfd_elf_strtab_size (dynstr); 3528 3529 bed = get_elf_backend_data (dynobj); 3530 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 3531 BFD_ASSERT (sdyn != NULL); 3532 3533 /* Update all .dynamic entries referencing .dynstr strings. */ 3534 for (extdyn = sdyn->contents; 3535 extdyn < sdyn->contents + sdyn->size; 3536 extdyn += bed->s->sizeof_dyn) 3537 { 3538 Elf_Internal_Dyn dyn; 3539 3540 bed->s->swap_dyn_in (dynobj, extdyn, &dyn); 3541 switch (dyn.d_tag) 3542 { 3543 case DT_STRSZ: 3544 dyn.d_un.d_val = size; 3545 break; 3546 case DT_NEEDED: 3547 case DT_SONAME: 3548 case DT_RPATH: 3549 case DT_RUNPATH: 3550 case DT_FILTER: 3551 case DT_AUXILIARY: 3552 case DT_AUDIT: 3553 case DT_DEPAUDIT: 3554 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); 3555 break; 3556 default: 3557 continue; 3558 } 3559 bed->s->swap_dyn_out (dynobj, &dyn, extdyn); 3560 } 3561 3562 /* Now update local dynamic symbols. */ 3563 for (entry = hash_table->dynlocal; entry ; entry = entry->next) 3564 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, 3565 entry->isym.st_name); 3566 3567 /* And the rest of dynamic symbols. */ 3568 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); 3569 3570 /* Adjust version definitions. */ 3571 if (elf_tdata (output_bfd)->cverdefs) 3572 { 3573 asection *s; 3574 bfd_byte *p; 3575 size_t i; 3576 Elf_Internal_Verdef def; 3577 Elf_Internal_Verdaux defaux; 3578 3579 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 3580 p = s->contents; 3581 do 3582 { 3583 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, 3584 &def); 3585 p += sizeof (Elf_External_Verdef); 3586 if (def.vd_aux != sizeof (Elf_External_Verdef)) 3587 continue; 3588 for (i = 0; i < def.vd_cnt; ++i) 3589 { 3590 _bfd_elf_swap_verdaux_in (output_bfd, 3591 (Elf_External_Verdaux *) p, &defaux); 3592 defaux.vda_name = _bfd_elf_strtab_offset (dynstr, 3593 defaux.vda_name); 3594 _bfd_elf_swap_verdaux_out (output_bfd, 3595 &defaux, (Elf_External_Verdaux *) p); 3596 p += sizeof (Elf_External_Verdaux); 3597 } 3598 } 3599 while (def.vd_next); 3600 } 3601 3602 /* Adjust version references. */ 3603 if (elf_tdata (output_bfd)->verref) 3604 { 3605 asection *s; 3606 bfd_byte *p; 3607 size_t i; 3608 Elf_Internal_Verneed need; 3609 Elf_Internal_Vernaux needaux; 3610 3611 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 3612 p = s->contents; 3613 do 3614 { 3615 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, 3616 &need); 3617 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); 3618 _bfd_elf_swap_verneed_out (output_bfd, &need, 3619 (Elf_External_Verneed *) p); 3620 p += sizeof (Elf_External_Verneed); 3621 for (i = 0; i < need.vn_cnt; ++i) 3622 { 3623 _bfd_elf_swap_vernaux_in (output_bfd, 3624 (Elf_External_Vernaux *) p, &needaux); 3625 needaux.vna_name = _bfd_elf_strtab_offset (dynstr, 3626 needaux.vna_name); 3627 _bfd_elf_swap_vernaux_out (output_bfd, 3628 &needaux, 3629 (Elf_External_Vernaux *) p); 3630 p += sizeof (Elf_External_Vernaux); 3631 } 3632 } 3633 while (need.vn_next); 3634 } 3635 3636 return TRUE; 3637 } 3638 3639 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3640 The default is to only match when the INPUT and OUTPUT are exactly 3641 the same target. */ 3642 3643 bfd_boolean 3644 _bfd_elf_default_relocs_compatible (const bfd_target *input, 3645 const bfd_target *output) 3646 { 3647 return input == output; 3648 } 3649 3650 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3651 This version is used when different targets for the same architecture 3652 are virtually identical. */ 3653 3654 bfd_boolean 3655 _bfd_elf_relocs_compatible (const bfd_target *input, 3656 const bfd_target *output) 3657 { 3658 const struct elf_backend_data *obed, *ibed; 3659 3660 if (input == output) 3661 return TRUE; 3662 3663 ibed = xvec_get_elf_backend_data (input); 3664 obed = xvec_get_elf_backend_data (output); 3665 3666 if (ibed->arch != obed->arch) 3667 return FALSE; 3668 3669 /* If both backends are using this function, deem them compatible. */ 3670 return ibed->relocs_compatible == obed->relocs_compatible; 3671 } 3672 3673 /* Make a special call to the linker "notice" function to tell it that 3674 we are about to handle an as-needed lib, or have finished 3675 processing the lib. */ 3676 3677 bfd_boolean 3678 _bfd_elf_notice_as_needed (bfd *ibfd, 3679 struct bfd_link_info *info, 3680 enum notice_asneeded_action act) 3681 { 3682 return (*info->callbacks->notice) (info, NULL, NULL, ibfd, NULL, act, 0); 3683 } 3684 3685 /* Check relocations an ELF object file. */ 3686 3687 bfd_boolean 3688 _bfd_elf_link_check_relocs (bfd *abfd, struct bfd_link_info *info) 3689 { 3690 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 3691 struct elf_link_hash_table *htab = elf_hash_table (info); 3692 3693 /* If this object is the same format as the output object, and it is 3694 not a shared library, then let the backend look through the 3695 relocs. 3696 3697 This is required to build global offset table entries and to 3698 arrange for dynamic relocs. It is not required for the 3699 particular common case of linking non PIC code, even when linking 3700 against shared libraries, but unfortunately there is no way of 3701 knowing whether an object file has been compiled PIC or not. 3702 Looking through the relocs is not particularly time consuming. 3703 The problem is that we must either (1) keep the relocs in memory, 3704 which causes the linker to require additional runtime memory or 3705 (2) read the relocs twice from the input file, which wastes time. 3706 This would be a good case for using mmap. 3707 3708 I have no idea how to handle linking PIC code into a file of a 3709 different format. It probably can't be done. */ 3710 if ((abfd->flags & DYNAMIC) == 0 3711 && is_elf_hash_table (htab) 3712 && bed->check_relocs != NULL 3713 && elf_object_id (abfd) == elf_hash_table_id (htab) 3714 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 3715 { 3716 asection *o; 3717 3718 for (o = abfd->sections; o != NULL; o = o->next) 3719 { 3720 Elf_Internal_Rela *internal_relocs; 3721 bfd_boolean ok; 3722 3723 /* Don't check relocations in excluded sections. */ 3724 if ((o->flags & SEC_RELOC) == 0 3725 || (o->flags & SEC_EXCLUDE) != 0 3726 || o->reloc_count == 0 3727 || ((info->strip == strip_all || info->strip == strip_debugger) 3728 && (o->flags & SEC_DEBUGGING) != 0) 3729 || bfd_is_abs_section (o->output_section)) 3730 continue; 3731 3732 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, 3733 info->keep_memory); 3734 if (internal_relocs == NULL) 3735 return FALSE; 3736 3737 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); 3738 3739 if (elf_section_data (o)->relocs != internal_relocs) 3740 free (internal_relocs); 3741 3742 if (! ok) 3743 return FALSE; 3744 } 3745 } 3746 3747 return TRUE; 3748 } 3749 3750 /* Add symbols from an ELF object file to the linker hash table. */ 3751 3752 static bfd_boolean 3753 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 3754 { 3755 Elf_Internal_Ehdr *ehdr; 3756 Elf_Internal_Shdr *hdr; 3757 size_t symcount; 3758 size_t extsymcount; 3759 size_t extsymoff; 3760 struct elf_link_hash_entry **sym_hash; 3761 bfd_boolean dynamic; 3762 Elf_External_Versym *extversym = NULL; 3763 Elf_External_Versym *ever; 3764 struct elf_link_hash_entry *weaks; 3765 struct elf_link_hash_entry **nondeflt_vers = NULL; 3766 size_t nondeflt_vers_cnt = 0; 3767 Elf_Internal_Sym *isymbuf = NULL; 3768 Elf_Internal_Sym *isym; 3769 Elf_Internal_Sym *isymend; 3770 const struct elf_backend_data *bed; 3771 bfd_boolean add_needed; 3772 struct elf_link_hash_table *htab; 3773 bfd_size_type amt; 3774 void *alloc_mark = NULL; 3775 struct bfd_hash_entry **old_table = NULL; 3776 unsigned int old_size = 0; 3777 unsigned int old_count = 0; 3778 void *old_tab = NULL; 3779 void *old_ent; 3780 struct bfd_link_hash_entry *old_undefs = NULL; 3781 struct bfd_link_hash_entry *old_undefs_tail = NULL; 3782 void *old_strtab = NULL; 3783 size_t tabsize = 0; 3784 asection *s; 3785 bfd_boolean just_syms; 3786 3787 htab = elf_hash_table (info); 3788 bed = get_elf_backend_data (abfd); 3789 3790 if ((abfd->flags & DYNAMIC) == 0) 3791 dynamic = FALSE; 3792 else 3793 { 3794 dynamic = TRUE; 3795 3796 /* You can't use -r against a dynamic object. Also, there's no 3797 hope of using a dynamic object which does not exactly match 3798 the format of the output file. */ 3799 if (bfd_link_relocatable (info) 3800 || !is_elf_hash_table (htab) 3801 || info->output_bfd->xvec != abfd->xvec) 3802 { 3803 if (bfd_link_relocatable (info)) 3804 bfd_set_error (bfd_error_invalid_operation); 3805 else 3806 bfd_set_error (bfd_error_wrong_format); 3807 goto error_return; 3808 } 3809 } 3810 3811 ehdr = elf_elfheader (abfd); 3812 if (info->warn_alternate_em 3813 && bed->elf_machine_code != ehdr->e_machine 3814 && ((bed->elf_machine_alt1 != 0 3815 && ehdr->e_machine == bed->elf_machine_alt1) 3816 || (bed->elf_machine_alt2 != 0 3817 && ehdr->e_machine == bed->elf_machine_alt2))) 3818 info->callbacks->einfo 3819 /* xgettext:c-format */ 3820 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), 3821 ehdr->e_machine, abfd, bed->elf_machine_code); 3822 3823 /* As a GNU extension, any input sections which are named 3824 .gnu.warning.SYMBOL are treated as warning symbols for the given 3825 symbol. This differs from .gnu.warning sections, which generate 3826 warnings when they are included in an output file. */ 3827 /* PR 12761: Also generate this warning when building shared libraries. */ 3828 for (s = abfd->sections; s != NULL; s = s->next) 3829 { 3830 const char *name; 3831 3832 name = bfd_get_section_name (abfd, s); 3833 if (CONST_STRNEQ (name, ".gnu.warning.")) 3834 { 3835 char *msg; 3836 bfd_size_type sz; 3837 3838 name += sizeof ".gnu.warning." - 1; 3839 3840 /* If this is a shared object, then look up the symbol 3841 in the hash table. If it is there, and it is already 3842 been defined, then we will not be using the entry 3843 from this shared object, so we don't need to warn. 3844 FIXME: If we see the definition in a regular object 3845 later on, we will warn, but we shouldn't. The only 3846 fix is to keep track of what warnings we are supposed 3847 to emit, and then handle them all at the end of the 3848 link. */ 3849 if (dynamic) 3850 { 3851 struct elf_link_hash_entry *h; 3852 3853 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 3854 3855 /* FIXME: What about bfd_link_hash_common? */ 3856 if (h != NULL 3857 && (h->root.type == bfd_link_hash_defined 3858 || h->root.type == bfd_link_hash_defweak)) 3859 continue; 3860 } 3861 3862 sz = s->size; 3863 msg = (char *) bfd_alloc (abfd, sz + 1); 3864 if (msg == NULL) 3865 goto error_return; 3866 3867 if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) 3868 goto error_return; 3869 3870 msg[sz] = '\0'; 3871 3872 if (! (_bfd_generic_link_add_one_symbol 3873 (info, abfd, name, BSF_WARNING, s, 0, msg, 3874 FALSE, bed->collect, NULL))) 3875 goto error_return; 3876 3877 if (bfd_link_executable (info)) 3878 { 3879 /* Clobber the section size so that the warning does 3880 not get copied into the output file. */ 3881 s->size = 0; 3882 3883 /* Also set SEC_EXCLUDE, so that symbols defined in 3884 the warning section don't get copied to the output. */ 3885 s->flags |= SEC_EXCLUDE; 3886 } 3887 } 3888 } 3889 3890 just_syms = ((s = abfd->sections) != NULL 3891 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS); 3892 3893 add_needed = TRUE; 3894 if (! dynamic) 3895 { 3896 /* If we are creating a shared library, create all the dynamic 3897 sections immediately. We need to attach them to something, 3898 so we attach them to this BFD, provided it is the right 3899 format and is not from ld --just-symbols. Always create the 3900 dynamic sections for -E/--dynamic-list. FIXME: If there 3901 are no input BFD's of the same format as the output, we can't 3902 make a shared library. */ 3903 if (!just_syms 3904 && (bfd_link_pic (info) 3905 || (!bfd_link_relocatable (info) 3906 && info->nointerp 3907 && (info->export_dynamic || info->dynamic))) 3908 && is_elf_hash_table (htab) 3909 && info->output_bfd->xvec == abfd->xvec 3910 && !htab->dynamic_sections_created) 3911 { 3912 if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) 3913 goto error_return; 3914 } 3915 } 3916 else if (!is_elf_hash_table (htab)) 3917 goto error_return; 3918 else 3919 { 3920 const char *soname = NULL; 3921 char *audit = NULL; 3922 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; 3923 const Elf_Internal_Phdr *phdr; 3924 int ret; 3925 3926 /* ld --just-symbols and dynamic objects don't mix very well. 3927 ld shouldn't allow it. */ 3928 if (just_syms) 3929 abort (); 3930 3931 /* If this dynamic lib was specified on the command line with 3932 --as-needed in effect, then we don't want to add a DT_NEEDED 3933 tag unless the lib is actually used. Similary for libs brought 3934 in by another lib's DT_NEEDED. When --no-add-needed is used 3935 on a dynamic lib, we don't want to add a DT_NEEDED entry for 3936 any dynamic library in DT_NEEDED tags in the dynamic lib at 3937 all. */ 3938 add_needed = (elf_dyn_lib_class (abfd) 3939 & (DYN_AS_NEEDED | DYN_DT_NEEDED 3940 | DYN_NO_NEEDED)) == 0; 3941 3942 s = bfd_get_section_by_name (abfd, ".dynamic"); 3943 if (s != NULL) 3944 { 3945 bfd_byte *dynbuf; 3946 bfd_byte *extdyn; 3947 unsigned int elfsec; 3948 unsigned long shlink; 3949 3950 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 3951 { 3952 error_free_dyn: 3953 free (dynbuf); 3954 goto error_return; 3955 } 3956 3957 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 3958 if (elfsec == SHN_BAD) 3959 goto error_free_dyn; 3960 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 3961 3962 for (extdyn = dynbuf; 3963 extdyn < dynbuf + s->size; 3964 extdyn += bed->s->sizeof_dyn) 3965 { 3966 Elf_Internal_Dyn dyn; 3967 3968 bed->s->swap_dyn_in (abfd, extdyn, &dyn); 3969 if (dyn.d_tag == DT_SONAME) 3970 { 3971 unsigned int tagv = dyn.d_un.d_val; 3972 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3973 if (soname == NULL) 3974 goto error_free_dyn; 3975 } 3976 if (dyn.d_tag == DT_NEEDED) 3977 { 3978 struct bfd_link_needed_list *n, **pn; 3979 char *fnm, *anm; 3980 unsigned int tagv = dyn.d_un.d_val; 3981 3982 amt = sizeof (struct bfd_link_needed_list); 3983 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3984 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3985 if (n == NULL || fnm == NULL) 3986 goto error_free_dyn; 3987 amt = strlen (fnm) + 1; 3988 anm = (char *) bfd_alloc (abfd, amt); 3989 if (anm == NULL) 3990 goto error_free_dyn; 3991 memcpy (anm, fnm, amt); 3992 n->name = anm; 3993 n->by = abfd; 3994 n->next = NULL; 3995 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) 3996 ; 3997 *pn = n; 3998 } 3999 if (dyn.d_tag == DT_RUNPATH) 4000 { 4001 struct bfd_link_needed_list *n, **pn; 4002 char *fnm, *anm; 4003 unsigned int tagv = dyn.d_un.d_val; 4004 4005 amt = sizeof (struct bfd_link_needed_list); 4006 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4007 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4008 if (n == NULL || fnm == NULL) 4009 goto error_free_dyn; 4010 amt = strlen (fnm) + 1; 4011 anm = (char *) bfd_alloc (abfd, amt); 4012 if (anm == NULL) 4013 goto error_free_dyn; 4014 memcpy (anm, fnm, amt); 4015 n->name = anm; 4016 n->by = abfd; 4017 n->next = NULL; 4018 for (pn = & runpath; 4019 *pn != NULL; 4020 pn = &(*pn)->next) 4021 ; 4022 *pn = n; 4023 } 4024 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ 4025 if (!runpath && dyn.d_tag == DT_RPATH) 4026 { 4027 struct bfd_link_needed_list *n, **pn; 4028 char *fnm, *anm; 4029 unsigned int tagv = dyn.d_un.d_val; 4030 4031 amt = sizeof (struct bfd_link_needed_list); 4032 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 4033 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4034 if (n == NULL || fnm == NULL) 4035 goto error_free_dyn; 4036 amt = strlen (fnm) + 1; 4037 anm = (char *) bfd_alloc (abfd, amt); 4038 if (anm == NULL) 4039 goto error_free_dyn; 4040 memcpy (anm, fnm, amt); 4041 n->name = anm; 4042 n->by = abfd; 4043 n->next = NULL; 4044 for (pn = & rpath; 4045 *pn != NULL; 4046 pn = &(*pn)->next) 4047 ; 4048 *pn = n; 4049 } 4050 if (dyn.d_tag == DT_AUDIT) 4051 { 4052 unsigned int tagv = dyn.d_un.d_val; 4053 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 4054 } 4055 } 4056 4057 free (dynbuf); 4058 } 4059 4060 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that 4061 frees all more recently bfd_alloc'd blocks as well. */ 4062 if (runpath) 4063 rpath = runpath; 4064 4065 if (rpath) 4066 { 4067 struct bfd_link_needed_list **pn; 4068 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) 4069 ; 4070 *pn = rpath; 4071 } 4072 4073 /* If we have a PT_GNU_RELRO program header, mark as read-only 4074 all sections contained fully therein. This makes relro 4075 shared library sections appear as they will at run-time. */ 4076 phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum; 4077 while (--phdr >= elf_tdata (abfd)->phdr) 4078 if (phdr->p_type == PT_GNU_RELRO) 4079 { 4080 for (s = abfd->sections; s != NULL; s = s->next) 4081 if ((s->flags & SEC_ALLOC) != 0 4082 && s->vma >= phdr->p_vaddr 4083 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz) 4084 s->flags |= SEC_READONLY; 4085 break; 4086 } 4087 4088 /* We do not want to include any of the sections in a dynamic 4089 object in the output file. We hack by simply clobbering the 4090 list of sections in the BFD. This could be handled more 4091 cleanly by, say, a new section flag; the existing 4092 SEC_NEVER_LOAD flag is not the one we want, because that one 4093 still implies that the section takes up space in the output 4094 file. */ 4095 bfd_section_list_clear (abfd); 4096 4097 /* Find the name to use in a DT_NEEDED entry that refers to this 4098 object. If the object has a DT_SONAME entry, we use it. 4099 Otherwise, if the generic linker stuck something in 4100 elf_dt_name, we use that. Otherwise, we just use the file 4101 name. */ 4102 if (soname == NULL || *soname == '\0') 4103 { 4104 soname = elf_dt_name (abfd); 4105 if (soname == NULL || *soname == '\0') 4106 soname = bfd_get_filename (abfd); 4107 } 4108 4109 /* Save the SONAME because sometimes the linker emulation code 4110 will need to know it. */ 4111 elf_dt_name (abfd) = soname; 4112 4113 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4114 if (ret < 0) 4115 goto error_return; 4116 4117 /* If we have already included this dynamic object in the 4118 link, just ignore it. There is no reason to include a 4119 particular dynamic object more than once. */ 4120 if (ret > 0) 4121 return TRUE; 4122 4123 /* Save the DT_AUDIT entry for the linker emulation code. */ 4124 elf_dt_audit (abfd) = audit; 4125 } 4126 4127 /* If this is a dynamic object, we always link against the .dynsym 4128 symbol table, not the .symtab symbol table. The dynamic linker 4129 will only see the .dynsym symbol table, so there is no reason to 4130 look at .symtab for a dynamic object. */ 4131 4132 if (! dynamic || elf_dynsymtab (abfd) == 0) 4133 hdr = &elf_tdata (abfd)->symtab_hdr; 4134 else 4135 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 4136 4137 symcount = hdr->sh_size / bed->s->sizeof_sym; 4138 4139 /* The sh_info field of the symtab header tells us where the 4140 external symbols start. We don't care about the local symbols at 4141 this point. */ 4142 if (elf_bad_symtab (abfd)) 4143 { 4144 extsymcount = symcount; 4145 extsymoff = 0; 4146 } 4147 else 4148 { 4149 extsymcount = symcount - hdr->sh_info; 4150 extsymoff = hdr->sh_info; 4151 } 4152 4153 sym_hash = elf_sym_hashes (abfd); 4154 if (extsymcount != 0) 4155 { 4156 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 4157 NULL, NULL, NULL); 4158 if (isymbuf == NULL) 4159 goto error_return; 4160 4161 if (sym_hash == NULL) 4162 { 4163 /* We store a pointer to the hash table entry for each 4164 external symbol. */ 4165 amt = extsymcount; 4166 amt *= sizeof (struct elf_link_hash_entry *); 4167 sym_hash = (struct elf_link_hash_entry **) bfd_zalloc (abfd, amt); 4168 if (sym_hash == NULL) 4169 goto error_free_sym; 4170 elf_sym_hashes (abfd) = sym_hash; 4171 } 4172 } 4173 4174 if (dynamic) 4175 { 4176 /* Read in any version definitions. */ 4177 if (!_bfd_elf_slurp_version_tables (abfd, 4178 info->default_imported_symver)) 4179 goto error_free_sym; 4180 4181 /* Read in the symbol versions, but don't bother to convert them 4182 to internal format. */ 4183 if (elf_dynversym (abfd) != 0) 4184 { 4185 Elf_Internal_Shdr *versymhdr; 4186 4187 versymhdr = &elf_tdata (abfd)->dynversym_hdr; 4188 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 4189 if (extversym == NULL) 4190 goto error_free_sym; 4191 amt = versymhdr->sh_size; 4192 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 4193 || bfd_bread (extversym, amt, abfd) != amt) 4194 goto error_free_vers; 4195 } 4196 } 4197 4198 /* If we are loading an as-needed shared lib, save the symbol table 4199 state before we start adding symbols. If the lib turns out 4200 to be unneeded, restore the state. */ 4201 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4202 { 4203 unsigned int i; 4204 size_t entsize; 4205 4206 for (entsize = 0, i = 0; i < htab->root.table.size; i++) 4207 { 4208 struct bfd_hash_entry *p; 4209 struct elf_link_hash_entry *h; 4210 4211 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4212 { 4213 h = (struct elf_link_hash_entry *) p; 4214 entsize += htab->root.table.entsize; 4215 if (h->root.type == bfd_link_hash_warning) 4216 entsize += htab->root.table.entsize; 4217 } 4218 } 4219 4220 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); 4221 old_tab = bfd_malloc (tabsize + entsize); 4222 if (old_tab == NULL) 4223 goto error_free_vers; 4224 4225 /* Remember the current objalloc pointer, so that all mem for 4226 symbols added can later be reclaimed. */ 4227 alloc_mark = bfd_hash_allocate (&htab->root.table, 1); 4228 if (alloc_mark == NULL) 4229 goto error_free_vers; 4230 4231 /* Make a special call to the linker "notice" function to 4232 tell it that we are about to handle an as-needed lib. */ 4233 if (!(*bed->notice_as_needed) (abfd, info, notice_as_needed)) 4234 goto error_free_vers; 4235 4236 /* Clone the symbol table. Remember some pointers into the 4237 symbol table, and dynamic symbol count. */ 4238 old_ent = (char *) old_tab + tabsize; 4239 memcpy (old_tab, htab->root.table.table, tabsize); 4240 old_undefs = htab->root.undefs; 4241 old_undefs_tail = htab->root.undefs_tail; 4242 old_table = htab->root.table.table; 4243 old_size = htab->root.table.size; 4244 old_count = htab->root.table.count; 4245 old_strtab = _bfd_elf_strtab_save (htab->dynstr); 4246 if (old_strtab == NULL) 4247 goto error_free_vers; 4248 4249 for (i = 0; i < htab->root.table.size; i++) 4250 { 4251 struct bfd_hash_entry *p; 4252 struct elf_link_hash_entry *h; 4253 4254 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4255 { 4256 memcpy (old_ent, p, htab->root.table.entsize); 4257 old_ent = (char *) old_ent + htab->root.table.entsize; 4258 h = (struct elf_link_hash_entry *) p; 4259 if (h->root.type == bfd_link_hash_warning) 4260 { 4261 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); 4262 old_ent = (char *) old_ent + htab->root.table.entsize; 4263 } 4264 } 4265 } 4266 } 4267 4268 weaks = NULL; 4269 ever = extversym != NULL ? extversym + extsymoff : NULL; 4270 for (isym = isymbuf, isymend = isymbuf + extsymcount; 4271 isym < isymend; 4272 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) 4273 { 4274 int bind; 4275 bfd_vma value; 4276 asection *sec, *new_sec; 4277 flagword flags; 4278 const char *name; 4279 struct elf_link_hash_entry *h; 4280 struct elf_link_hash_entry *hi; 4281 bfd_boolean definition; 4282 bfd_boolean size_change_ok; 4283 bfd_boolean type_change_ok; 4284 bfd_boolean new_weak; 4285 bfd_boolean old_weak; 4286 bfd_boolean override; 4287 bfd_boolean common; 4288 bfd_boolean discarded; 4289 unsigned int old_alignment; 4290 bfd *old_bfd; 4291 bfd_boolean matched; 4292 4293 override = FALSE; 4294 4295 flags = BSF_NO_FLAGS; 4296 sec = NULL; 4297 value = isym->st_value; 4298 common = bed->common_definition (isym); 4299 if (common && info->inhibit_common_definition) 4300 { 4301 /* Treat common symbol as undefined for --no-define-common. */ 4302 isym->st_shndx = SHN_UNDEF; 4303 common = FALSE; 4304 } 4305 discarded = FALSE; 4306 4307 bind = ELF_ST_BIND (isym->st_info); 4308 switch (bind) 4309 { 4310 case STB_LOCAL: 4311 /* This should be impossible, since ELF requires that all 4312 global symbols follow all local symbols, and that sh_info 4313 point to the first global symbol. Unfortunately, Irix 5 4314 screws this up. */ 4315 continue; 4316 4317 case STB_GLOBAL: 4318 if (isym->st_shndx != SHN_UNDEF && !common) 4319 flags = BSF_GLOBAL; 4320 break; 4321 4322 case STB_WEAK: 4323 flags = BSF_WEAK; 4324 break; 4325 4326 case STB_GNU_UNIQUE: 4327 flags = BSF_GNU_UNIQUE; 4328 break; 4329 4330 default: 4331 /* Leave it up to the processor backend. */ 4332 break; 4333 } 4334 4335 if (isym->st_shndx == SHN_UNDEF) 4336 sec = bfd_und_section_ptr; 4337 else if (isym->st_shndx == SHN_ABS) 4338 sec = bfd_abs_section_ptr; 4339 else if (isym->st_shndx == SHN_COMMON) 4340 { 4341 sec = bfd_com_section_ptr; 4342 /* What ELF calls the size we call the value. What ELF 4343 calls the value we call the alignment. */ 4344 value = isym->st_size; 4345 } 4346 else 4347 { 4348 sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 4349 if (sec == NULL) 4350 sec = bfd_abs_section_ptr; 4351 else if (discarded_section (sec)) 4352 { 4353 /* Symbols from discarded section are undefined. We keep 4354 its visibility. */ 4355 sec = bfd_und_section_ptr; 4356 discarded = TRUE; 4357 isym->st_shndx = SHN_UNDEF; 4358 } 4359 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 4360 value -= sec->vma; 4361 } 4362 4363 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 4364 isym->st_name); 4365 if (name == NULL) 4366 goto error_free_vers; 4367 4368 if (isym->st_shndx == SHN_COMMON 4369 && (abfd->flags & BFD_PLUGIN) != 0) 4370 { 4371 asection *xc = bfd_get_section_by_name (abfd, "COMMON"); 4372 4373 if (xc == NULL) 4374 { 4375 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP 4376 | SEC_EXCLUDE); 4377 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); 4378 if (xc == NULL) 4379 goto error_free_vers; 4380 } 4381 sec = xc; 4382 } 4383 else if (isym->st_shndx == SHN_COMMON 4384 && ELF_ST_TYPE (isym->st_info) == STT_TLS 4385 && !bfd_link_relocatable (info)) 4386 { 4387 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); 4388 4389 if (tcomm == NULL) 4390 { 4391 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON 4392 | SEC_LINKER_CREATED); 4393 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); 4394 if (tcomm == NULL) 4395 goto error_free_vers; 4396 } 4397 sec = tcomm; 4398 } 4399 else if (bed->elf_add_symbol_hook) 4400 { 4401 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, 4402 &sec, &value)) 4403 goto error_free_vers; 4404 4405 /* The hook function sets the name to NULL if this symbol 4406 should be skipped for some reason. */ 4407 if (name == NULL) 4408 continue; 4409 } 4410 4411 /* Sanity check that all possibilities were handled. */ 4412 if (sec == NULL) 4413 { 4414 bfd_set_error (bfd_error_bad_value); 4415 goto error_free_vers; 4416 } 4417 4418 /* Silently discard TLS symbols from --just-syms. There's 4419 no way to combine a static TLS block with a new TLS block 4420 for this executable. */ 4421 if (ELF_ST_TYPE (isym->st_info) == STT_TLS 4422 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 4423 continue; 4424 4425 if (bfd_is_und_section (sec) 4426 || bfd_is_com_section (sec)) 4427 definition = FALSE; 4428 else 4429 definition = TRUE; 4430 4431 size_change_ok = FALSE; 4432 type_change_ok = bed->type_change_ok; 4433 old_weak = FALSE; 4434 matched = FALSE; 4435 old_alignment = 0; 4436 old_bfd = NULL; 4437 new_sec = sec; 4438 4439 if (is_elf_hash_table (htab)) 4440 { 4441 Elf_Internal_Versym iver; 4442 unsigned int vernum = 0; 4443 bfd_boolean skip; 4444 4445 if (ever == NULL) 4446 { 4447 if (info->default_imported_symver) 4448 /* Use the default symbol version created earlier. */ 4449 iver.vs_vers = elf_tdata (abfd)->cverdefs; 4450 else 4451 iver.vs_vers = 0; 4452 } 4453 else 4454 _bfd_elf_swap_versym_in (abfd, ever, &iver); 4455 4456 vernum = iver.vs_vers & VERSYM_VERSION; 4457 4458 /* If this is a hidden symbol, or if it is not version 4459 1, we append the version name to the symbol name. 4460 However, we do not modify a non-hidden absolute symbol 4461 if it is not a function, because it might be the version 4462 symbol itself. FIXME: What if it isn't? */ 4463 if ((iver.vs_vers & VERSYM_HIDDEN) != 0 4464 || (vernum > 1 4465 && (!bfd_is_abs_section (sec) 4466 || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) 4467 { 4468 const char *verstr; 4469 size_t namelen, verlen, newlen; 4470 char *newname, *p; 4471 4472 if (isym->st_shndx != SHN_UNDEF) 4473 { 4474 if (vernum > elf_tdata (abfd)->cverdefs) 4475 verstr = NULL; 4476 else if (vernum > 1) 4477 verstr = 4478 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 4479 else 4480 verstr = ""; 4481 4482 if (verstr == NULL) 4483 { 4484 _bfd_error_handler 4485 /* xgettext:c-format */ 4486 (_("%B: %s: invalid version %u (max %d)"), 4487 abfd, name, vernum, 4488 elf_tdata (abfd)->cverdefs); 4489 bfd_set_error (bfd_error_bad_value); 4490 goto error_free_vers; 4491 } 4492 } 4493 else 4494 { 4495 /* We cannot simply test for the number of 4496 entries in the VERNEED section since the 4497 numbers for the needed versions do not start 4498 at 0. */ 4499 Elf_Internal_Verneed *t; 4500 4501 verstr = NULL; 4502 for (t = elf_tdata (abfd)->verref; 4503 t != NULL; 4504 t = t->vn_nextref) 4505 { 4506 Elf_Internal_Vernaux *a; 4507 4508 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 4509 { 4510 if (a->vna_other == vernum) 4511 { 4512 verstr = a->vna_nodename; 4513 break; 4514 } 4515 } 4516 if (a != NULL) 4517 break; 4518 } 4519 if (verstr == NULL) 4520 { 4521 _bfd_error_handler 4522 /* xgettext:c-format */ 4523 (_("%B: %s: invalid needed version %d"), 4524 abfd, name, vernum); 4525 bfd_set_error (bfd_error_bad_value); 4526 goto error_free_vers; 4527 } 4528 } 4529 4530 namelen = strlen (name); 4531 verlen = strlen (verstr); 4532 newlen = namelen + verlen + 2; 4533 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4534 && isym->st_shndx != SHN_UNDEF) 4535 ++newlen; 4536 4537 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); 4538 if (newname == NULL) 4539 goto error_free_vers; 4540 memcpy (newname, name, namelen); 4541 p = newname + namelen; 4542 *p++ = ELF_VER_CHR; 4543 /* If this is a defined non-hidden version symbol, 4544 we add another @ to the name. This indicates the 4545 default version of the symbol. */ 4546 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4547 && isym->st_shndx != SHN_UNDEF) 4548 *p++ = ELF_VER_CHR; 4549 memcpy (p, verstr, verlen + 1); 4550 4551 name = newname; 4552 } 4553 4554 /* If this symbol has default visibility and the user has 4555 requested we not re-export it, then mark it as hidden. */ 4556 if (!bfd_is_und_section (sec) 4557 && !dynamic 4558 && abfd->no_export 4559 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) 4560 isym->st_other = (STV_HIDDEN 4561 | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); 4562 4563 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, &value, 4564 sym_hash, &old_bfd, &old_weak, 4565 &old_alignment, &skip, &override, 4566 &type_change_ok, &size_change_ok, 4567 &matched)) 4568 goto error_free_vers; 4569 4570 if (skip) 4571 continue; 4572 4573 /* Override a definition only if the new symbol matches the 4574 existing one. */ 4575 if (override && matched) 4576 definition = FALSE; 4577 4578 h = *sym_hash; 4579 while (h->root.type == bfd_link_hash_indirect 4580 || h->root.type == bfd_link_hash_warning) 4581 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4582 4583 if (elf_tdata (abfd)->verdef != NULL 4584 && vernum > 1 4585 && definition) 4586 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; 4587 } 4588 4589 if (! (_bfd_generic_link_add_one_symbol 4590 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, 4591 (struct bfd_link_hash_entry **) sym_hash))) 4592 goto error_free_vers; 4593 4594 if ((flags & BSF_GNU_UNIQUE) 4595 && (abfd->flags & DYNAMIC) == 0 4596 && bfd_get_flavour (info->output_bfd) == bfd_target_elf_flavour) 4597 elf_tdata (info->output_bfd)->has_gnu_symbols |= elf_gnu_symbol_unique; 4598 4599 h = *sym_hash; 4600 /* We need to make sure that indirect symbol dynamic flags are 4601 updated. */ 4602 hi = h; 4603 while (h->root.type == bfd_link_hash_indirect 4604 || h->root.type == bfd_link_hash_warning) 4605 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4606 4607 /* Setting the index to -3 tells elf_link_output_extsym that 4608 this symbol is defined in a discarded section. */ 4609 if (discarded) 4610 h->indx = -3; 4611 4612 *sym_hash = h; 4613 4614 new_weak = (flags & BSF_WEAK) != 0; 4615 if (dynamic 4616 && definition 4617 && new_weak 4618 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) 4619 && is_elf_hash_table (htab) 4620 && h->u.alias == NULL) 4621 { 4622 /* Keep a list of all weak defined non function symbols from 4623 a dynamic object, using the alias field. Later in this 4624 function we will set the alias field to the correct 4625 value. We only put non-function symbols from dynamic 4626 objects on this list, because that happens to be the only 4627 time we need to know the normal symbol corresponding to a 4628 weak symbol, and the information is time consuming to 4629 figure out. If the alias field is not already NULL, 4630 then this symbol was already defined by some previous 4631 dynamic object, and we will be using that previous 4632 definition anyhow. */ 4633 4634 h->u.alias = weaks; 4635 weaks = h; 4636 } 4637 4638 /* Set the alignment of a common symbol. */ 4639 if ((common || bfd_is_com_section (sec)) 4640 && h->root.type == bfd_link_hash_common) 4641 { 4642 unsigned int align; 4643 4644 if (common) 4645 align = bfd_log2 (isym->st_value); 4646 else 4647 { 4648 /* The new symbol is a common symbol in a shared object. 4649 We need to get the alignment from the section. */ 4650 align = new_sec->alignment_power; 4651 } 4652 if (align > old_alignment) 4653 h->root.u.c.p->alignment_power = align; 4654 else 4655 h->root.u.c.p->alignment_power = old_alignment; 4656 } 4657 4658 if (is_elf_hash_table (htab)) 4659 { 4660 /* Set a flag in the hash table entry indicating the type of 4661 reference or definition we just found. A dynamic symbol 4662 is one which is referenced or defined by both a regular 4663 object and a shared object. */ 4664 bfd_boolean dynsym = FALSE; 4665 4666 /* Plugin symbols aren't normal. Don't set def_regular or 4667 ref_regular for them, or make them dynamic. */ 4668 if ((abfd->flags & BFD_PLUGIN) != 0) 4669 ; 4670 else if (! dynamic) 4671 { 4672 if (! definition) 4673 { 4674 h->ref_regular = 1; 4675 if (bind != STB_WEAK) 4676 h->ref_regular_nonweak = 1; 4677 } 4678 else 4679 { 4680 h->def_regular = 1; 4681 if (h->def_dynamic) 4682 { 4683 h->def_dynamic = 0; 4684 h->ref_dynamic = 1; 4685 } 4686 } 4687 4688 /* If the indirect symbol has been forced local, don't 4689 make the real symbol dynamic. */ 4690 if ((h == hi || !hi->forced_local) 4691 && (bfd_link_dll (info) 4692 || h->def_dynamic 4693 || h->ref_dynamic)) 4694 dynsym = TRUE; 4695 } 4696 else 4697 { 4698 if (! definition) 4699 { 4700 h->ref_dynamic = 1; 4701 hi->ref_dynamic = 1; 4702 } 4703 else 4704 { 4705 h->def_dynamic = 1; 4706 hi->def_dynamic = 1; 4707 } 4708 4709 /* If the indirect symbol has been forced local, don't 4710 make the real symbol dynamic. */ 4711 if ((h == hi || !hi->forced_local) 4712 && (h->def_regular 4713 || h->ref_regular 4714 || (h->is_weakalias 4715 && weakdef (h)->dynindx != -1))) 4716 dynsym = TRUE; 4717 } 4718 4719 /* Check to see if we need to add an indirect symbol for 4720 the default name. */ 4721 if (definition 4722 || (!override && h->root.type == bfd_link_hash_common)) 4723 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, 4724 sec, value, &old_bfd, &dynsym)) 4725 goto error_free_vers; 4726 4727 /* Check the alignment when a common symbol is involved. This 4728 can change when a common symbol is overridden by a normal 4729 definition or a common symbol is ignored due to the old 4730 normal definition. We need to make sure the maximum 4731 alignment is maintained. */ 4732 if ((old_alignment || common) 4733 && h->root.type != bfd_link_hash_common) 4734 { 4735 unsigned int common_align; 4736 unsigned int normal_align; 4737 unsigned int symbol_align; 4738 bfd *normal_bfd; 4739 bfd *common_bfd; 4740 4741 BFD_ASSERT (h->root.type == bfd_link_hash_defined 4742 || h->root.type == bfd_link_hash_defweak); 4743 4744 symbol_align = ffs (h->root.u.def.value) - 1; 4745 if (h->root.u.def.section->owner != NULL 4746 && (h->root.u.def.section->owner->flags 4747 & (DYNAMIC | BFD_PLUGIN)) == 0) 4748 { 4749 normal_align = h->root.u.def.section->alignment_power; 4750 if (normal_align > symbol_align) 4751 normal_align = symbol_align; 4752 } 4753 else 4754 normal_align = symbol_align; 4755 4756 if (old_alignment) 4757 { 4758 common_align = old_alignment; 4759 common_bfd = old_bfd; 4760 normal_bfd = abfd; 4761 } 4762 else 4763 { 4764 common_align = bfd_log2 (isym->st_value); 4765 common_bfd = abfd; 4766 normal_bfd = old_bfd; 4767 } 4768 4769 if (normal_align < common_align) 4770 { 4771 /* PR binutils/2735 */ 4772 if (normal_bfd == NULL) 4773 _bfd_error_handler 4774 /* xgettext:c-format */ 4775 (_("Warning: alignment %u of common symbol `%s' in %B is" 4776 " greater than the alignment (%u) of its section %A"), 4777 1 << common_align, name, common_bfd, 4778 1 << normal_align, h->root.u.def.section); 4779 else 4780 _bfd_error_handler 4781 /* xgettext:c-format */ 4782 (_("Warning: alignment %u of symbol `%s' in %B" 4783 " is smaller than %u in %B"), 4784 1 << normal_align, name, normal_bfd, 4785 1 << common_align, common_bfd); 4786 } 4787 } 4788 4789 /* Remember the symbol size if it isn't undefined. */ 4790 if (isym->st_size != 0 4791 && isym->st_shndx != SHN_UNDEF 4792 && (definition || h->size == 0)) 4793 { 4794 if (h->size != 0 4795 && h->size != isym->st_size 4796 && ! size_change_ok) 4797 _bfd_error_handler 4798 /* xgettext:c-format */ 4799 (_("Warning: size of symbol `%s' changed" 4800 " from %Lu in %B to %Lu in %B"), 4801 name, h->size, old_bfd, isym->st_size, abfd); 4802 4803 h->size = isym->st_size; 4804 } 4805 4806 /* If this is a common symbol, then we always want H->SIZE 4807 to be the size of the common symbol. The code just above 4808 won't fix the size if a common symbol becomes larger. We 4809 don't warn about a size change here, because that is 4810 covered by --warn-common. Allow changes between different 4811 function types. */ 4812 if (h->root.type == bfd_link_hash_common) 4813 h->size = h->root.u.c.size; 4814 4815 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE 4816 && ((definition && !new_weak) 4817 || (old_weak && h->root.type == bfd_link_hash_common) 4818 || h->type == STT_NOTYPE)) 4819 { 4820 unsigned int type = ELF_ST_TYPE (isym->st_info); 4821 4822 /* Turn an IFUNC symbol from a DSO into a normal FUNC 4823 symbol. */ 4824 if (type == STT_GNU_IFUNC 4825 && (abfd->flags & DYNAMIC) != 0) 4826 type = STT_FUNC; 4827 4828 if (h->type != type) 4829 { 4830 if (h->type != STT_NOTYPE && ! type_change_ok) 4831 /* xgettext:c-format */ 4832 _bfd_error_handler 4833 (_("Warning: type of symbol `%s' changed" 4834 " from %d to %d in %B"), 4835 name, h->type, type, abfd); 4836 4837 h->type = type; 4838 } 4839 } 4840 4841 /* Merge st_other field. */ 4842 elf_merge_st_other (abfd, h, isym, sec, definition, dynamic); 4843 4844 /* We don't want to make debug symbol dynamic. */ 4845 if (definition 4846 && (sec->flags & SEC_DEBUGGING) 4847 && !bfd_link_relocatable (info)) 4848 dynsym = FALSE; 4849 4850 /* Nor should we make plugin symbols dynamic. */ 4851 if ((abfd->flags & BFD_PLUGIN) != 0) 4852 dynsym = FALSE; 4853 4854 if (definition) 4855 { 4856 h->target_internal = isym->st_target_internal; 4857 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; 4858 } 4859 4860 if (definition && !dynamic) 4861 { 4862 char *p = strchr (name, ELF_VER_CHR); 4863 if (p != NULL && p[1] != ELF_VER_CHR) 4864 { 4865 /* Queue non-default versions so that .symver x, x@FOO 4866 aliases can be checked. */ 4867 if (!nondeflt_vers) 4868 { 4869 amt = ((isymend - isym + 1) 4870 * sizeof (struct elf_link_hash_entry *)); 4871 nondeflt_vers 4872 = (struct elf_link_hash_entry **) bfd_malloc (amt); 4873 if (!nondeflt_vers) 4874 goto error_free_vers; 4875 } 4876 nondeflt_vers[nondeflt_vers_cnt++] = h; 4877 } 4878 } 4879 4880 if (dynsym && h->dynindx == -1) 4881 { 4882 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4883 goto error_free_vers; 4884 if (h->is_weakalias 4885 && weakdef (h)->dynindx == -1) 4886 { 4887 if (!bfd_elf_link_record_dynamic_symbol (info, weakdef (h))) 4888 goto error_free_vers; 4889 } 4890 } 4891 else if (h->dynindx != -1) 4892 /* If the symbol already has a dynamic index, but 4893 visibility says it should not be visible, turn it into 4894 a local symbol. */ 4895 switch (ELF_ST_VISIBILITY (h->other)) 4896 { 4897 case STV_INTERNAL: 4898 case STV_HIDDEN: 4899 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 4900 dynsym = FALSE; 4901 break; 4902 } 4903 4904 /* Don't add DT_NEEDED for references from the dummy bfd nor 4905 for unmatched symbol. */ 4906 if (!add_needed 4907 && matched 4908 && definition 4909 && ((dynsym 4910 && h->ref_regular_nonweak 4911 && (old_bfd == NULL 4912 || (old_bfd->flags & BFD_PLUGIN) == 0)) 4913 || (h->ref_dynamic_nonweak 4914 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 4915 && !on_needed_list (elf_dt_name (abfd), 4916 htab->needed, NULL)))) 4917 { 4918 int ret; 4919 const char *soname = elf_dt_name (abfd); 4920 4921 info->callbacks->minfo ("%!", soname, old_bfd, 4922 h->root.root.string); 4923 4924 /* A symbol from a library loaded via DT_NEEDED of some 4925 other library is referenced by a regular object. 4926 Add a DT_NEEDED entry for it. Issue an error if 4927 --no-add-needed is used and the reference was not 4928 a weak one. */ 4929 if (old_bfd != NULL 4930 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) 4931 { 4932 _bfd_error_handler 4933 /* xgettext:c-format */ 4934 (_("%B: undefined reference to symbol '%s'"), 4935 old_bfd, name); 4936 bfd_set_error (bfd_error_missing_dso); 4937 goto error_free_vers; 4938 } 4939 4940 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) 4941 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); 4942 4943 add_needed = TRUE; 4944 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4945 if (ret < 0) 4946 goto error_free_vers; 4947 4948 BFD_ASSERT (ret == 0); 4949 } 4950 } 4951 } 4952 4953 if (info->lto_plugin_active 4954 && !bfd_link_relocatable (info) 4955 && (abfd->flags & BFD_PLUGIN) == 0 4956 && !just_syms 4957 && extsymcount) 4958 { 4959 int r_sym_shift; 4960 4961 if (bed->s->arch_size == 32) 4962 r_sym_shift = 8; 4963 else 4964 r_sym_shift = 32; 4965 4966 /* If linker plugin is enabled, set non_ir_ref_regular on symbols 4967 referenced in regular objects so that linker plugin will get 4968 the correct symbol resolution. */ 4969 4970 sym_hash = elf_sym_hashes (abfd); 4971 for (s = abfd->sections; s != NULL; s = s->next) 4972 { 4973 Elf_Internal_Rela *internal_relocs; 4974 Elf_Internal_Rela *rel, *relend; 4975 4976 /* Don't check relocations in excluded sections. */ 4977 if ((s->flags & SEC_RELOC) == 0 4978 || s->reloc_count == 0 4979 || (s->flags & SEC_EXCLUDE) != 0 4980 || ((info->strip == strip_all 4981 || info->strip == strip_debugger) 4982 && (s->flags & SEC_DEBUGGING) != 0)) 4983 continue; 4984 4985 internal_relocs = _bfd_elf_link_read_relocs (abfd, s, NULL, 4986 NULL, 4987 info->keep_memory); 4988 if (internal_relocs == NULL) 4989 goto error_free_vers; 4990 4991 rel = internal_relocs; 4992 relend = rel + s->reloc_count; 4993 for ( ; rel < relend; rel++) 4994 { 4995 unsigned long r_symndx = rel->r_info >> r_sym_shift; 4996 struct elf_link_hash_entry *h; 4997 4998 /* Skip local symbols. */ 4999 if (r_symndx < extsymoff) 5000 continue; 5001 5002 h = sym_hash[r_symndx - extsymoff]; 5003 if (h != NULL) 5004 h->root.non_ir_ref_regular = 1; 5005 } 5006 5007 if (elf_section_data (s)->relocs != internal_relocs) 5008 free (internal_relocs); 5009 } 5010 } 5011 5012 if (extversym != NULL) 5013 { 5014 free (extversym); 5015 extversym = NULL; 5016 } 5017 5018 if (isymbuf != NULL) 5019 { 5020 free (isymbuf); 5021 isymbuf = NULL; 5022 } 5023 5024 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 5025 { 5026 unsigned int i; 5027 5028 /* Restore the symbol table. */ 5029 old_ent = (char *) old_tab + tabsize; 5030 memset (elf_sym_hashes (abfd), 0, 5031 extsymcount * sizeof (struct elf_link_hash_entry *)); 5032 htab->root.table.table = old_table; 5033 htab->root.table.size = old_size; 5034 htab->root.table.count = old_count; 5035 memcpy (htab->root.table.table, old_tab, tabsize); 5036 htab->root.undefs = old_undefs; 5037 htab->root.undefs_tail = old_undefs_tail; 5038 _bfd_elf_strtab_restore (htab->dynstr, old_strtab); 5039 free (old_strtab); 5040 old_strtab = NULL; 5041 for (i = 0; i < htab->root.table.size; i++) 5042 { 5043 struct bfd_hash_entry *p; 5044 struct elf_link_hash_entry *h; 5045 bfd_size_type size; 5046 unsigned int alignment_power; 5047 unsigned int non_ir_ref_dynamic; 5048 5049 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 5050 { 5051 h = (struct elf_link_hash_entry *) p; 5052 if (h->root.type == bfd_link_hash_warning) 5053 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5054 5055 /* Preserve the maximum alignment and size for common 5056 symbols even if this dynamic lib isn't on DT_NEEDED 5057 since it can still be loaded at run time by another 5058 dynamic lib. */ 5059 if (h->root.type == bfd_link_hash_common) 5060 { 5061 size = h->root.u.c.size; 5062 alignment_power = h->root.u.c.p->alignment_power; 5063 } 5064 else 5065 { 5066 size = 0; 5067 alignment_power = 0; 5068 } 5069 /* Preserve non_ir_ref_dynamic so that this symbol 5070 will be exported when the dynamic lib becomes needed 5071 in the second pass. */ 5072 non_ir_ref_dynamic = h->root.non_ir_ref_dynamic; 5073 memcpy (p, old_ent, htab->root.table.entsize); 5074 old_ent = (char *) old_ent + htab->root.table.entsize; 5075 h = (struct elf_link_hash_entry *) p; 5076 if (h->root.type == bfd_link_hash_warning) 5077 { 5078 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); 5079 old_ent = (char *) old_ent + htab->root.table.entsize; 5080 h = (struct elf_link_hash_entry *) h->root.u.i.link; 5081 } 5082 if (h->root.type == bfd_link_hash_common) 5083 { 5084 if (size > h->root.u.c.size) 5085 h->root.u.c.size = size; 5086 if (alignment_power > h->root.u.c.p->alignment_power) 5087 h->root.u.c.p->alignment_power = alignment_power; 5088 } 5089 h->root.non_ir_ref_dynamic = non_ir_ref_dynamic; 5090 } 5091 } 5092 5093 /* Make a special call to the linker "notice" function to 5094 tell it that symbols added for crefs may need to be removed. */ 5095 if (!(*bed->notice_as_needed) (abfd, info, notice_not_needed)) 5096 goto error_free_vers; 5097 5098 free (old_tab); 5099 objalloc_free_block ((struct objalloc *) htab->root.table.memory, 5100 alloc_mark); 5101 if (nondeflt_vers != NULL) 5102 free (nondeflt_vers); 5103 return TRUE; 5104 } 5105 5106 if (old_tab != NULL) 5107 { 5108 if (!(*bed->notice_as_needed) (abfd, info, notice_needed)) 5109 goto error_free_vers; 5110 free (old_tab); 5111 old_tab = NULL; 5112 } 5113 5114 /* Now that all the symbols from this input file are created, if 5115 not performing a relocatable link, handle .symver foo, foo@BAR 5116 such that any relocs against foo become foo@BAR. */ 5117 if (!bfd_link_relocatable (info) && nondeflt_vers != NULL) 5118 { 5119 size_t cnt, symidx; 5120 5121 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) 5122 { 5123 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; 5124 char *shortname, *p; 5125 5126 p = strchr (h->root.root.string, ELF_VER_CHR); 5127 if (p == NULL 5128 || (h->root.type != bfd_link_hash_defined 5129 && h->root.type != bfd_link_hash_defweak)) 5130 continue; 5131 5132 amt = p - h->root.root.string; 5133 shortname = (char *) bfd_malloc (amt + 1); 5134 if (!shortname) 5135 goto error_free_vers; 5136 memcpy (shortname, h->root.root.string, amt); 5137 shortname[amt] = '\0'; 5138 5139 hi = (struct elf_link_hash_entry *) 5140 bfd_link_hash_lookup (&htab->root, shortname, 5141 FALSE, FALSE, FALSE); 5142 if (hi != NULL 5143 && hi->root.type == h->root.type 5144 && hi->root.u.def.value == h->root.u.def.value 5145 && hi->root.u.def.section == h->root.u.def.section) 5146 { 5147 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 5148 hi->root.type = bfd_link_hash_indirect; 5149 hi->root.u.i.link = (struct bfd_link_hash_entry *) h; 5150 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 5151 sym_hash = elf_sym_hashes (abfd); 5152 if (sym_hash) 5153 for (symidx = 0; symidx < extsymcount; ++symidx) 5154 if (sym_hash[symidx] == hi) 5155 { 5156 sym_hash[symidx] = h; 5157 break; 5158 } 5159 } 5160 free (shortname); 5161 } 5162 free (nondeflt_vers); 5163 nondeflt_vers = NULL; 5164 } 5165 5166 /* Now set the alias field correctly for all the weak defined 5167 symbols we found. The only way to do this is to search all the 5168 symbols. Since we only need the information for non functions in 5169 dynamic objects, that's the only time we actually put anything on 5170 the list WEAKS. We need this information so that if a regular 5171 object refers to a symbol defined weakly in a dynamic object, the 5172 real symbol in the dynamic object is also put in the dynamic 5173 symbols; we also must arrange for both symbols to point to the 5174 same memory location. We could handle the general case of symbol 5175 aliasing, but a general symbol alias can only be generated in 5176 assembler code, handling it correctly would be very time 5177 consuming, and other ELF linkers don't handle general aliasing 5178 either. */ 5179 if (weaks != NULL) 5180 { 5181 struct elf_link_hash_entry **hpp; 5182 struct elf_link_hash_entry **hppend; 5183 struct elf_link_hash_entry **sorted_sym_hash; 5184 struct elf_link_hash_entry *h; 5185 size_t sym_count; 5186 5187 /* Since we have to search the whole symbol list for each weak 5188 defined symbol, search time for N weak defined symbols will be 5189 O(N^2). Binary search will cut it down to O(NlogN). */ 5190 amt = extsymcount; 5191 amt *= sizeof (struct elf_link_hash_entry *); 5192 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); 5193 if (sorted_sym_hash == NULL) 5194 goto error_return; 5195 sym_hash = sorted_sym_hash; 5196 hpp = elf_sym_hashes (abfd); 5197 hppend = hpp + extsymcount; 5198 sym_count = 0; 5199 for (; hpp < hppend; hpp++) 5200 { 5201 h = *hpp; 5202 if (h != NULL 5203 && h->root.type == bfd_link_hash_defined 5204 && !bed->is_function_type (h->type)) 5205 { 5206 *sym_hash = h; 5207 sym_hash++; 5208 sym_count++; 5209 } 5210 } 5211 5212 qsort (sorted_sym_hash, sym_count, 5213 sizeof (struct elf_link_hash_entry *), 5214 elf_sort_symbol); 5215 5216 while (weaks != NULL) 5217 { 5218 struct elf_link_hash_entry *hlook; 5219 asection *slook; 5220 bfd_vma vlook; 5221 size_t i, j, idx = 0; 5222 5223 hlook = weaks; 5224 weaks = hlook->u.alias; 5225 hlook->u.alias = NULL; 5226 5227 if (hlook->root.type != bfd_link_hash_defined 5228 && hlook->root.type != bfd_link_hash_defweak) 5229 continue; 5230 5231 slook = hlook->root.u.def.section; 5232 vlook = hlook->root.u.def.value; 5233 5234 i = 0; 5235 j = sym_count; 5236 while (i != j) 5237 { 5238 bfd_signed_vma vdiff; 5239 idx = (i + j) / 2; 5240 h = sorted_sym_hash[idx]; 5241 vdiff = vlook - h->root.u.def.value; 5242 if (vdiff < 0) 5243 j = idx; 5244 else if (vdiff > 0) 5245 i = idx + 1; 5246 else 5247 { 5248 int sdiff = slook->id - h->root.u.def.section->id; 5249 if (sdiff < 0) 5250 j = idx; 5251 else if (sdiff > 0) 5252 i = idx + 1; 5253 else 5254 break; 5255 } 5256 } 5257 5258 /* We didn't find a value/section match. */ 5259 if (i == j) 5260 continue; 5261 5262 /* With multiple aliases, or when the weak symbol is already 5263 strongly defined, we have multiple matching symbols and 5264 the binary search above may land on any of them. Step 5265 one past the matching symbol(s). */ 5266 while (++idx != j) 5267 { 5268 h = sorted_sym_hash[idx]; 5269 if (h->root.u.def.section != slook 5270 || h->root.u.def.value != vlook) 5271 break; 5272 } 5273 5274 /* Now look back over the aliases. Since we sorted by size 5275 as well as value and section, we'll choose the one with 5276 the largest size. */ 5277 while (idx-- != i) 5278 { 5279 h = sorted_sym_hash[idx]; 5280 5281 /* Stop if value or section doesn't match. */ 5282 if (h->root.u.def.section != slook 5283 || h->root.u.def.value != vlook) 5284 break; 5285 else if (h != hlook) 5286 { 5287 struct elf_link_hash_entry *t; 5288 5289 hlook->u.alias = h; 5290 hlook->is_weakalias = 1; 5291 t = h; 5292 if (t->u.alias != NULL) 5293 while (t->u.alias != h) 5294 t = t->u.alias; 5295 t->u.alias = hlook; 5296 5297 /* If the weak definition is in the list of dynamic 5298 symbols, make sure the real definition is put 5299 there as well. */ 5300 if (hlook->dynindx != -1 && h->dynindx == -1) 5301 { 5302 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 5303 { 5304 err_free_sym_hash: 5305 free (sorted_sym_hash); 5306 goto error_return; 5307 } 5308 } 5309 5310 /* If the real definition is in the list of dynamic 5311 symbols, make sure the weak definition is put 5312 there as well. If we don't do this, then the 5313 dynamic loader might not merge the entries for the 5314 real definition and the weak definition. */ 5315 if (h->dynindx != -1 && hlook->dynindx == -1) 5316 { 5317 if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) 5318 goto err_free_sym_hash; 5319 } 5320 break; 5321 } 5322 } 5323 } 5324 5325 free (sorted_sym_hash); 5326 } 5327 5328 if (bed->check_directives 5329 && !(*bed->check_directives) (abfd, info)) 5330 return FALSE; 5331 5332 /* If this is a non-traditional link, try to optimize the handling 5333 of the .stab/.stabstr sections. */ 5334 if (! dynamic 5335 && ! info->traditional_format 5336 && is_elf_hash_table (htab) 5337 && (info->strip != strip_all && info->strip != strip_debugger)) 5338 { 5339 asection *stabstr; 5340 5341 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 5342 if (stabstr != NULL) 5343 { 5344 bfd_size_type string_offset = 0; 5345 asection *stab; 5346 5347 for (stab = abfd->sections; stab; stab = stab->next) 5348 if (CONST_STRNEQ (stab->name, ".stab") 5349 && (!stab->name[5] || 5350 (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) 5351 && (stab->flags & SEC_MERGE) == 0 5352 && !bfd_is_abs_section (stab->output_section)) 5353 { 5354 struct bfd_elf_section_data *secdata; 5355 5356 secdata = elf_section_data (stab); 5357 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, 5358 stabstr, &secdata->sec_info, 5359 &string_offset)) 5360 goto error_return; 5361 if (secdata->sec_info) 5362 stab->sec_info_type = SEC_INFO_TYPE_STABS; 5363 } 5364 } 5365 } 5366 5367 if (is_elf_hash_table (htab) && add_needed) 5368 { 5369 /* Add this bfd to the loaded list. */ 5370 struct elf_link_loaded_list *n; 5371 5372 n = (struct elf_link_loaded_list *) bfd_alloc (abfd, sizeof (*n)); 5373 if (n == NULL) 5374 goto error_return; 5375 n->abfd = abfd; 5376 n->next = htab->loaded; 5377 htab->loaded = n; 5378 } 5379 5380 return TRUE; 5381 5382 error_free_vers: 5383 if (old_tab != NULL) 5384 free (old_tab); 5385 if (old_strtab != NULL) 5386 free (old_strtab); 5387 if (nondeflt_vers != NULL) 5388 free (nondeflt_vers); 5389 if (extversym != NULL) 5390 free (extversym); 5391 error_free_sym: 5392 if (isymbuf != NULL) 5393 free (isymbuf); 5394 error_return: 5395 return FALSE; 5396 } 5397 5398 /* Return the linker hash table entry of a symbol that might be 5399 satisfied by an archive symbol. Return -1 on error. */ 5400 5401 struct elf_link_hash_entry * 5402 _bfd_elf_archive_symbol_lookup (bfd *abfd, 5403 struct bfd_link_info *info, 5404 const char *name) 5405 { 5406 struct elf_link_hash_entry *h; 5407 char *p, *copy; 5408 size_t len, first; 5409 5410 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); 5411 if (h != NULL) 5412 return h; 5413 5414 /* If this is a default version (the name contains @@), look up the 5415 symbol again with only one `@' as well as without the version. 5416 The effect is that references to the symbol with and without the 5417 version will be matched by the default symbol in the archive. */ 5418 5419 p = strchr (name, ELF_VER_CHR); 5420 if (p == NULL || p[1] != ELF_VER_CHR) 5421 return h; 5422 5423 /* First check with only one `@'. */ 5424 len = strlen (name); 5425 copy = (char *) bfd_alloc (abfd, len); 5426 if (copy == NULL) 5427 return (struct elf_link_hash_entry *) 0 - 1; 5428 5429 first = p - name + 1; 5430 memcpy (copy, name, first); 5431 memcpy (copy + first, name + first + 1, len - first); 5432 5433 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); 5434 if (h == NULL) 5435 { 5436 /* We also need to check references to the symbol without the 5437 version. */ 5438 copy[first - 1] = '\0'; 5439 h = elf_link_hash_lookup (elf_hash_table (info), copy, 5440 FALSE, FALSE, TRUE); 5441 } 5442 5443 bfd_release (abfd, copy); 5444 return h; 5445 } 5446 5447 /* Add symbols from an ELF archive file to the linker hash table. We 5448 don't use _bfd_generic_link_add_archive_symbols because we need to 5449 handle versioned symbols. 5450 5451 Fortunately, ELF archive handling is simpler than that done by 5452 _bfd_generic_link_add_archive_symbols, which has to allow for a.out 5453 oddities. In ELF, if we find a symbol in the archive map, and the 5454 symbol is currently undefined, we know that we must pull in that 5455 object file. 5456 5457 Unfortunately, we do have to make multiple passes over the symbol 5458 table until nothing further is resolved. */ 5459 5460 static bfd_boolean 5461 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 5462 { 5463 symindex c; 5464 unsigned char *included = NULL; 5465 carsym *symdefs; 5466 bfd_boolean loop; 5467 bfd_size_type amt; 5468 const struct elf_backend_data *bed; 5469 struct elf_link_hash_entry * (*archive_symbol_lookup) 5470 (bfd *, struct bfd_link_info *, const char *); 5471 5472 if (! bfd_has_map (abfd)) 5473 { 5474 /* An empty archive is a special case. */ 5475 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 5476 return TRUE; 5477 bfd_set_error (bfd_error_no_armap); 5478 return FALSE; 5479 } 5480 5481 /* Keep track of all symbols we know to be already defined, and all 5482 files we know to be already included. This is to speed up the 5483 second and subsequent passes. */ 5484 c = bfd_ardata (abfd)->symdef_count; 5485 if (c == 0) 5486 return TRUE; 5487 amt = c; 5488 amt *= sizeof (*included); 5489 included = (unsigned char *) bfd_zmalloc (amt); 5490 if (included == NULL) 5491 return FALSE; 5492 5493 symdefs = bfd_ardata (abfd)->symdefs; 5494 bed = get_elf_backend_data (abfd); 5495 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; 5496 5497 do 5498 { 5499 file_ptr last; 5500 symindex i; 5501 carsym *symdef; 5502 carsym *symdefend; 5503 5504 loop = FALSE; 5505 last = -1; 5506 5507 symdef = symdefs; 5508 symdefend = symdef + c; 5509 for (i = 0; symdef < symdefend; symdef++, i++) 5510 { 5511 struct elf_link_hash_entry *h; 5512 bfd *element; 5513 struct bfd_link_hash_entry *undefs_tail; 5514 symindex mark; 5515 5516 if (included[i]) 5517 continue; 5518 if (symdef->file_offset == last) 5519 { 5520 included[i] = TRUE; 5521 continue; 5522 } 5523 5524 h = archive_symbol_lookup (abfd, info, symdef->name); 5525 if (h == (struct elf_link_hash_entry *) 0 - 1) 5526 goto error_return; 5527 5528 if (h == NULL) 5529 continue; 5530 5531 if (h->root.type == bfd_link_hash_common) 5532 { 5533 /* We currently have a common symbol. The archive map contains 5534 a reference to this symbol, so we may want to include it. We 5535 only want to include it however, if this archive element 5536 contains a definition of the symbol, not just another common 5537 declaration of it. 5538 5539 Unfortunately some archivers (including GNU ar) will put 5540 declarations of common symbols into their archive maps, as 5541 well as real definitions, so we cannot just go by the archive 5542 map alone. Instead we must read in the element's symbol 5543 table and check that to see what kind of symbol definition 5544 this is. */ 5545 if (! elf_link_is_defined_archive_symbol (abfd, symdef)) 5546 continue; 5547 } 5548 else if (h->root.type != bfd_link_hash_undefined) 5549 { 5550 if (h->root.type != bfd_link_hash_undefweak) 5551 /* Symbol must be defined. Don't check it again. */ 5552 included[i] = TRUE; 5553 continue; 5554 } 5555 5556 /* We need to include this archive member. */ 5557 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 5558 if (element == NULL) 5559 goto error_return; 5560 5561 if (! bfd_check_format (element, bfd_object)) 5562 goto error_return; 5563 5564 undefs_tail = info->hash->undefs_tail; 5565 5566 if (!(*info->callbacks 5567 ->add_archive_element) (info, element, symdef->name, &element)) 5568 continue; 5569 if (!bfd_link_add_symbols (element, info)) 5570 goto error_return; 5571 5572 /* If there are any new undefined symbols, we need to make 5573 another pass through the archive in order to see whether 5574 they can be defined. FIXME: This isn't perfect, because 5575 common symbols wind up on undefs_tail and because an 5576 undefined symbol which is defined later on in this pass 5577 does not require another pass. This isn't a bug, but it 5578 does make the code less efficient than it could be. */ 5579 if (undefs_tail != info->hash->undefs_tail) 5580 loop = TRUE; 5581 5582 /* Look backward to mark all symbols from this object file 5583 which we have already seen in this pass. */ 5584 mark = i; 5585 do 5586 { 5587 included[mark] = TRUE; 5588 if (mark == 0) 5589 break; 5590 --mark; 5591 } 5592 while (symdefs[mark].file_offset == symdef->file_offset); 5593 5594 /* We mark subsequent symbols from this object file as we go 5595 on through the loop. */ 5596 last = symdef->file_offset; 5597 } 5598 } 5599 while (loop); 5600 5601 free (included); 5602 5603 return TRUE; 5604 5605 error_return: 5606 if (included != NULL) 5607 free (included); 5608 return FALSE; 5609 } 5610 5611 /* Given an ELF BFD, add symbols to the global hash table as 5612 appropriate. */ 5613 5614 bfd_boolean 5615 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 5616 { 5617 switch (bfd_get_format (abfd)) 5618 { 5619 case bfd_object: 5620 return elf_link_add_object_symbols (abfd, info); 5621 case bfd_archive: 5622 return elf_link_add_archive_symbols (abfd, info); 5623 default: 5624 bfd_set_error (bfd_error_wrong_format); 5625 return FALSE; 5626 } 5627 } 5628 5629 struct hash_codes_info 5630 { 5631 unsigned long *hashcodes; 5632 bfd_boolean error; 5633 }; 5634 5635 /* This function will be called though elf_link_hash_traverse to store 5636 all hash value of the exported symbols in an array. */ 5637 5638 static bfd_boolean 5639 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) 5640 { 5641 struct hash_codes_info *inf = (struct hash_codes_info *) data; 5642 const char *name; 5643 unsigned long ha; 5644 char *alc = NULL; 5645 5646 /* Ignore indirect symbols. These are added by the versioning code. */ 5647 if (h->dynindx == -1) 5648 return TRUE; 5649 5650 name = h->root.root.string; 5651 if (h->versioned >= versioned) 5652 { 5653 char *p = strchr (name, ELF_VER_CHR); 5654 if (p != NULL) 5655 { 5656 alc = (char *) bfd_malloc (p - name + 1); 5657 if (alc == NULL) 5658 { 5659 inf->error = TRUE; 5660 return FALSE; 5661 } 5662 memcpy (alc, name, p - name); 5663 alc[p - name] = '\0'; 5664 name = alc; 5665 } 5666 } 5667 5668 /* Compute the hash value. */ 5669 ha = bfd_elf_hash (name); 5670 5671 /* Store the found hash value in the array given as the argument. */ 5672 *(inf->hashcodes)++ = ha; 5673 5674 /* And store it in the struct so that we can put it in the hash table 5675 later. */ 5676 h->u.elf_hash_value = ha; 5677 5678 if (alc != NULL) 5679 free (alc); 5680 5681 return TRUE; 5682 } 5683 5684 struct collect_gnu_hash_codes 5685 { 5686 bfd *output_bfd; 5687 const struct elf_backend_data *bed; 5688 unsigned long int nsyms; 5689 unsigned long int maskbits; 5690 unsigned long int *hashcodes; 5691 unsigned long int *hashval; 5692 unsigned long int *indx; 5693 unsigned long int *counts; 5694 bfd_vma *bitmask; 5695 bfd_byte *contents; 5696 long int min_dynindx; 5697 unsigned long int bucketcount; 5698 unsigned long int symindx; 5699 long int local_indx; 5700 long int shift1, shift2; 5701 unsigned long int mask; 5702 bfd_boolean error; 5703 }; 5704 5705 /* This function will be called though elf_link_hash_traverse to store 5706 all hash value of the exported symbols in an array. */ 5707 5708 static bfd_boolean 5709 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) 5710 { 5711 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5712 const char *name; 5713 unsigned long ha; 5714 char *alc = NULL; 5715 5716 /* Ignore indirect symbols. These are added by the versioning code. */ 5717 if (h->dynindx == -1) 5718 return TRUE; 5719 5720 /* Ignore also local symbols and undefined symbols. */ 5721 if (! (*s->bed->elf_hash_symbol) (h)) 5722 return TRUE; 5723 5724 name = h->root.root.string; 5725 if (h->versioned >= versioned) 5726 { 5727 char *p = strchr (name, ELF_VER_CHR); 5728 if (p != NULL) 5729 { 5730 alc = (char *) bfd_malloc (p - name + 1); 5731 if (alc == NULL) 5732 { 5733 s->error = TRUE; 5734 return FALSE; 5735 } 5736 memcpy (alc, name, p - name); 5737 alc[p - name] = '\0'; 5738 name = alc; 5739 } 5740 } 5741 5742 /* Compute the hash value. */ 5743 ha = bfd_elf_gnu_hash (name); 5744 5745 /* Store the found hash value in the array for compute_bucket_count, 5746 and also for .dynsym reordering purposes. */ 5747 s->hashcodes[s->nsyms] = ha; 5748 s->hashval[h->dynindx] = ha; 5749 ++s->nsyms; 5750 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) 5751 s->min_dynindx = h->dynindx; 5752 5753 if (alc != NULL) 5754 free (alc); 5755 5756 return TRUE; 5757 } 5758 5759 /* This function will be called though elf_link_hash_traverse to do 5760 final dynaminc symbol renumbering. */ 5761 5762 static bfd_boolean 5763 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) 5764 { 5765 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5766 unsigned long int bucket; 5767 unsigned long int val; 5768 5769 /* Ignore indirect symbols. */ 5770 if (h->dynindx == -1) 5771 return TRUE; 5772 5773 /* Ignore also local symbols and undefined symbols. */ 5774 if (! (*s->bed->elf_hash_symbol) (h)) 5775 { 5776 if (h->dynindx >= s->min_dynindx) 5777 h->dynindx = s->local_indx++; 5778 return TRUE; 5779 } 5780 5781 bucket = s->hashval[h->dynindx] % s->bucketcount; 5782 val = (s->hashval[h->dynindx] >> s->shift1) 5783 & ((s->maskbits >> s->shift1) - 1); 5784 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); 5785 s->bitmask[val] 5786 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); 5787 val = s->hashval[h->dynindx] & ~(unsigned long int) 1; 5788 if (s->counts[bucket] == 1) 5789 /* Last element terminates the chain. */ 5790 val |= 1; 5791 bfd_put_32 (s->output_bfd, val, 5792 s->contents + (s->indx[bucket] - s->symindx) * 4); 5793 --s->counts[bucket]; 5794 h->dynindx = s->indx[bucket]++; 5795 return TRUE; 5796 } 5797 5798 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 5799 5800 bfd_boolean 5801 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) 5802 { 5803 return !(h->forced_local 5804 || h->root.type == bfd_link_hash_undefined 5805 || h->root.type == bfd_link_hash_undefweak 5806 || ((h->root.type == bfd_link_hash_defined 5807 || h->root.type == bfd_link_hash_defweak) 5808 && h->root.u.def.section->output_section == NULL)); 5809 } 5810 5811 /* Array used to determine the number of hash table buckets to use 5812 based on the number of symbols there are. If there are fewer than 5813 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, 5814 fewer than 37 we use 17 buckets, and so forth. We never use more 5815 than 32771 buckets. */ 5816 5817 static const size_t elf_buckets[] = 5818 { 5819 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 5820 16411, 32771, 0 5821 }; 5822 5823 /* Compute bucket count for hashing table. We do not use a static set 5824 of possible tables sizes anymore. Instead we determine for all 5825 possible reasonable sizes of the table the outcome (i.e., the 5826 number of collisions etc) and choose the best solution. The 5827 weighting functions are not too simple to allow the table to grow 5828 without bounds. Instead one of the weighting factors is the size. 5829 Therefore the result is always a good payoff between few collisions 5830 (= short chain lengths) and table size. */ 5831 static size_t 5832 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, 5833 unsigned long int *hashcodes ATTRIBUTE_UNUSED, 5834 unsigned long int nsyms, 5835 int gnu_hash) 5836 { 5837 size_t best_size = 0; 5838 unsigned long int i; 5839 5840 /* We have a problem here. The following code to optimize the table 5841 size requires an integer type with more the 32 bits. If 5842 BFD_HOST_U_64_BIT is set we know about such a type. */ 5843 #ifdef BFD_HOST_U_64_BIT 5844 if (info->optimize) 5845 { 5846 size_t minsize; 5847 size_t maxsize; 5848 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); 5849 bfd *dynobj = elf_hash_table (info)->dynobj; 5850 size_t dynsymcount = elf_hash_table (info)->dynsymcount; 5851 const struct elf_backend_data *bed = get_elf_backend_data (dynobj); 5852 unsigned long int *counts; 5853 bfd_size_type amt; 5854 unsigned int no_improvement_count = 0; 5855 5856 /* Possible optimization parameters: if we have NSYMS symbols we say 5857 that the hashing table must at least have NSYMS/4 and at most 5858 2*NSYMS buckets. */ 5859 minsize = nsyms / 4; 5860 if (minsize == 0) 5861 minsize = 1; 5862 best_size = maxsize = nsyms * 2; 5863 if (gnu_hash) 5864 { 5865 if (minsize < 2) 5866 minsize = 2; 5867 if ((best_size & 31) == 0) 5868 ++best_size; 5869 } 5870 5871 /* Create array where we count the collisions in. We must use bfd_malloc 5872 since the size could be large. */ 5873 amt = maxsize; 5874 amt *= sizeof (unsigned long int); 5875 counts = (unsigned long int *) bfd_malloc (amt); 5876 if (counts == NULL) 5877 return 0; 5878 5879 /* Compute the "optimal" size for the hash table. The criteria is a 5880 minimal chain length. The minor criteria is (of course) the size 5881 of the table. */ 5882 for (i = minsize; i < maxsize; ++i) 5883 { 5884 /* Walk through the array of hashcodes and count the collisions. */ 5885 BFD_HOST_U_64_BIT max; 5886 unsigned long int j; 5887 unsigned long int fact; 5888 5889 if (gnu_hash && (i & 31) == 0) 5890 continue; 5891 5892 memset (counts, '\0', i * sizeof (unsigned long int)); 5893 5894 /* Determine how often each hash bucket is used. */ 5895 for (j = 0; j < nsyms; ++j) 5896 ++counts[hashcodes[j] % i]; 5897 5898 /* For the weight function we need some information about the 5899 pagesize on the target. This is information need not be 100% 5900 accurate. Since this information is not available (so far) we 5901 define it here to a reasonable default value. If it is crucial 5902 to have a better value some day simply define this value. */ 5903 # ifndef BFD_TARGET_PAGESIZE 5904 # define BFD_TARGET_PAGESIZE (4096) 5905 # endif 5906 5907 /* We in any case need 2 + DYNSYMCOUNT entries for the size values 5908 and the chains. */ 5909 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; 5910 5911 # if 1 5912 /* Variant 1: optimize for short chains. We add the squares 5913 of all the chain lengths (which favors many small chain 5914 over a few long chains). */ 5915 for (j = 0; j < i; ++j) 5916 max += counts[j] * counts[j]; 5917 5918 /* This adds penalties for the overall size of the table. */ 5919 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 5920 max *= fact * fact; 5921 # else 5922 /* Variant 2: Optimize a lot more for small table. Here we 5923 also add squares of the size but we also add penalties for 5924 empty slots (the +1 term). */ 5925 for (j = 0; j < i; ++j) 5926 max += (1 + counts[j]) * (1 + counts[j]); 5927 5928 /* The overall size of the table is considered, but not as 5929 strong as in variant 1, where it is squared. */ 5930 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 5931 max *= fact; 5932 # endif 5933 5934 /* Compare with current best results. */ 5935 if (max < best_chlen) 5936 { 5937 best_chlen = max; 5938 best_size = i; 5939 no_improvement_count = 0; 5940 } 5941 /* PR 11843: Avoid futile long searches for the best bucket size 5942 when there are a large number of symbols. */ 5943 else if (++no_improvement_count == 100) 5944 break; 5945 } 5946 5947 free (counts); 5948 } 5949 else 5950 #endif /* defined (BFD_HOST_U_64_BIT) */ 5951 { 5952 /* This is the fallback solution if no 64bit type is available or if we 5953 are not supposed to spend much time on optimizations. We select the 5954 bucket count using a fixed set of numbers. */ 5955 for (i = 0; elf_buckets[i] != 0; i++) 5956 { 5957 best_size = elf_buckets[i]; 5958 if (nsyms < elf_buckets[i + 1]) 5959 break; 5960 } 5961 if (gnu_hash && best_size < 2) 5962 best_size = 2; 5963 } 5964 5965 return best_size; 5966 } 5967 5968 /* Size any SHT_GROUP section for ld -r. */ 5969 5970 bfd_boolean 5971 _bfd_elf_size_group_sections (struct bfd_link_info *info) 5972 { 5973 bfd *ibfd; 5974 asection *s; 5975 5976 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 5977 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour 5978 && (s = ibfd->sections) != NULL 5979 && s->sec_info_type != SEC_INFO_TYPE_JUST_SYMS 5980 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) 5981 return FALSE; 5982 return TRUE; 5983 } 5984 5985 /* Set a default stack segment size. The value in INFO wins. If it 5986 is unset, LEGACY_SYMBOL's value is used, and if that symbol is 5987 undefined it is initialized. */ 5988 5989 bfd_boolean 5990 bfd_elf_stack_segment_size (bfd *output_bfd, 5991 struct bfd_link_info *info, 5992 const char *legacy_symbol, 5993 bfd_vma default_size) 5994 { 5995 struct elf_link_hash_entry *h = NULL; 5996 5997 /* Look for legacy symbol. */ 5998 if (legacy_symbol) 5999 h = elf_link_hash_lookup (elf_hash_table (info), legacy_symbol, 6000 FALSE, FALSE, FALSE); 6001 if (h && (h->root.type == bfd_link_hash_defined 6002 || h->root.type == bfd_link_hash_defweak) 6003 && h->def_regular 6004 && (h->type == STT_NOTYPE || h->type == STT_OBJECT)) 6005 { 6006 /* The symbol has no type if specified on the command line. */ 6007 h->type = STT_OBJECT; 6008 if (info->stacksize) 6009 /* xgettext:c-format */ 6010 _bfd_error_handler (_("%B: stack size specified and %s set"), 6011 output_bfd, legacy_symbol); 6012 else if (h->root.u.def.section != bfd_abs_section_ptr) 6013 /* xgettext:c-format */ 6014 _bfd_error_handler (_("%B: %s not absolute"), 6015 output_bfd, legacy_symbol); 6016 else 6017 info->stacksize = h->root.u.def.value; 6018 } 6019 6020 if (!info->stacksize) 6021 /* If the user didn't set a size, or explicitly inhibit the 6022 size, set it now. */ 6023 info->stacksize = default_size; 6024 6025 /* Provide the legacy symbol, if it is referenced. */ 6026 if (h && (h->root.type == bfd_link_hash_undefined 6027 || h->root.type == bfd_link_hash_undefweak)) 6028 { 6029 struct bfd_link_hash_entry *bh = NULL; 6030 6031 if (!(_bfd_generic_link_add_one_symbol 6032 (info, output_bfd, legacy_symbol, 6033 BSF_GLOBAL, bfd_abs_section_ptr, 6034 info->stacksize >= 0 ? info->stacksize : 0, 6035 NULL, FALSE, get_elf_backend_data (output_bfd)->collect, &bh))) 6036 return FALSE; 6037 6038 h = (struct elf_link_hash_entry *) bh; 6039 h->def_regular = 1; 6040 h->type = STT_OBJECT; 6041 } 6042 6043 return TRUE; 6044 } 6045 6046 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ 6047 6048 struct elf_gc_sweep_symbol_info 6049 { 6050 struct bfd_link_info *info; 6051 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, 6052 bfd_boolean); 6053 }; 6054 6055 static bfd_boolean 6056 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) 6057 { 6058 if (!h->mark 6059 && (((h->root.type == bfd_link_hash_defined 6060 || h->root.type == bfd_link_hash_defweak) 6061 && !((h->def_regular || ELF_COMMON_DEF_P (h)) 6062 && h->root.u.def.section->gc_mark)) 6063 || h->root.type == bfd_link_hash_undefined 6064 || h->root.type == bfd_link_hash_undefweak)) 6065 { 6066 struct elf_gc_sweep_symbol_info *inf; 6067 6068 inf = (struct elf_gc_sweep_symbol_info *) data; 6069 (*inf->hide_symbol) (inf->info, h, TRUE); 6070 h->def_regular = 0; 6071 h->ref_regular = 0; 6072 h->ref_regular_nonweak = 0; 6073 } 6074 6075 return TRUE; 6076 } 6077 6078 /* Set up the sizes and contents of the ELF dynamic sections. This is 6079 called by the ELF linker emulation before_allocation routine. We 6080 must set the sizes of the sections before the linker sets the 6081 addresses of the various sections. */ 6082 6083 bfd_boolean 6084 bfd_elf_size_dynamic_sections (bfd *output_bfd, 6085 const char *soname, 6086 const char *rpath, 6087 const char *filter_shlib, 6088 const char *audit, 6089 const char *depaudit, 6090 const char * const *auxiliary_filters, 6091 struct bfd_link_info *info, 6092 asection **sinterpptr) 6093 { 6094 bfd *dynobj; 6095 const struct elf_backend_data *bed; 6096 6097 *sinterpptr = NULL; 6098 6099 if (!is_elf_hash_table (info->hash)) 6100 return TRUE; 6101 6102 dynobj = elf_hash_table (info)->dynobj; 6103 6104 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6105 { 6106 struct bfd_elf_version_tree *verdefs; 6107 struct elf_info_failed asvinfo; 6108 struct bfd_elf_version_tree *t; 6109 struct bfd_elf_version_expr *d; 6110 asection *s; 6111 size_t soname_indx; 6112 6113 /* If we are supposed to export all symbols into the dynamic symbol 6114 table (this is not the normal case), then do so. */ 6115 if (info->export_dynamic 6116 || (bfd_link_executable (info) && info->dynamic)) 6117 { 6118 struct elf_info_failed eif; 6119 6120 eif.info = info; 6121 eif.failed = FALSE; 6122 elf_link_hash_traverse (elf_hash_table (info), 6123 _bfd_elf_export_symbol, 6124 &eif); 6125 if (eif.failed) 6126 return FALSE; 6127 } 6128 6129 if (soname != NULL) 6130 { 6131 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6132 soname, TRUE); 6133 if (soname_indx == (size_t) -1 6134 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) 6135 return FALSE; 6136 } 6137 else 6138 soname_indx = (size_t) -1; 6139 6140 /* Make all global versions with definition. */ 6141 for (t = info->version_info; t != NULL; t = t->next) 6142 for (d = t->globals.list; d != NULL; d = d->next) 6143 if (!d->symver && d->literal) 6144 { 6145 const char *verstr, *name; 6146 size_t namelen, verlen, newlen; 6147 char *newname, *p, leading_char; 6148 struct elf_link_hash_entry *newh; 6149 6150 leading_char = bfd_get_symbol_leading_char (output_bfd); 6151 name = d->pattern; 6152 namelen = strlen (name) + (leading_char != '\0'); 6153 verstr = t->name; 6154 verlen = strlen (verstr); 6155 newlen = namelen + verlen + 3; 6156 6157 newname = (char *) bfd_malloc (newlen); 6158 if (newname == NULL) 6159 return FALSE; 6160 newname[0] = leading_char; 6161 memcpy (newname + (leading_char != '\0'), name, namelen); 6162 6163 /* Check the hidden versioned definition. */ 6164 p = newname + namelen; 6165 *p++ = ELF_VER_CHR; 6166 memcpy (p, verstr, verlen + 1); 6167 newh = elf_link_hash_lookup (elf_hash_table (info), 6168 newname, FALSE, FALSE, 6169 FALSE); 6170 if (newh == NULL 6171 || (newh->root.type != bfd_link_hash_defined 6172 && newh->root.type != bfd_link_hash_defweak)) 6173 { 6174 /* Check the default versioned definition. */ 6175 *p++ = ELF_VER_CHR; 6176 memcpy (p, verstr, verlen + 1); 6177 newh = elf_link_hash_lookup (elf_hash_table (info), 6178 newname, FALSE, FALSE, 6179 FALSE); 6180 } 6181 free (newname); 6182 6183 /* Mark this version if there is a definition and it is 6184 not defined in a shared object. */ 6185 if (newh != NULL 6186 && !newh->def_dynamic 6187 && (newh->root.type == bfd_link_hash_defined 6188 || newh->root.type == bfd_link_hash_defweak)) 6189 d->symver = 1; 6190 } 6191 6192 /* Attach all the symbols to their version information. */ 6193 asvinfo.info = info; 6194 asvinfo.failed = FALSE; 6195 6196 elf_link_hash_traverse (elf_hash_table (info), 6197 _bfd_elf_link_assign_sym_version, 6198 &asvinfo); 6199 if (asvinfo.failed) 6200 return FALSE; 6201 6202 if (!info->allow_undefined_version) 6203 { 6204 /* Check if all global versions have a definition. */ 6205 bfd_boolean all_defined = TRUE; 6206 for (t = info->version_info; t != NULL; t = t->next) 6207 for (d = t->globals.list; d != NULL; d = d->next) 6208 if (d->literal && !d->symver && !d->script) 6209 { 6210 _bfd_error_handler 6211 (_("%s: undefined version: %s"), 6212 d->pattern, t->name); 6213 all_defined = FALSE; 6214 } 6215 6216 if (!all_defined) 6217 { 6218 bfd_set_error (bfd_error_bad_value); 6219 return FALSE; 6220 } 6221 } 6222 6223 /* Set up the version definition section. */ 6224 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 6225 BFD_ASSERT (s != NULL); 6226 6227 /* We may have created additional version definitions if we are 6228 just linking a regular application. */ 6229 verdefs = info->version_info; 6230 6231 /* Skip anonymous version tag. */ 6232 if (verdefs != NULL && verdefs->vernum == 0) 6233 verdefs = verdefs->next; 6234 6235 if (verdefs == NULL && !info->create_default_symver) 6236 s->flags |= SEC_EXCLUDE; 6237 else 6238 { 6239 unsigned int cdefs; 6240 bfd_size_type size; 6241 bfd_byte *p; 6242 Elf_Internal_Verdef def; 6243 Elf_Internal_Verdaux defaux; 6244 struct bfd_link_hash_entry *bh; 6245 struct elf_link_hash_entry *h; 6246 const char *name; 6247 6248 cdefs = 0; 6249 size = 0; 6250 6251 /* Make space for the base version. */ 6252 size += sizeof (Elf_External_Verdef); 6253 size += sizeof (Elf_External_Verdaux); 6254 ++cdefs; 6255 6256 /* Make space for the default version. */ 6257 if (info->create_default_symver) 6258 { 6259 size += sizeof (Elf_External_Verdef); 6260 ++cdefs; 6261 } 6262 6263 for (t = verdefs; t != NULL; t = t->next) 6264 { 6265 struct bfd_elf_version_deps *n; 6266 6267 /* Don't emit base version twice. */ 6268 if (t->vernum == 0) 6269 continue; 6270 6271 size += sizeof (Elf_External_Verdef); 6272 size += sizeof (Elf_External_Verdaux); 6273 ++cdefs; 6274 6275 for (n = t->deps; n != NULL; n = n->next) 6276 size += sizeof (Elf_External_Verdaux); 6277 } 6278 6279 s->size = size; 6280 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6281 if (s->contents == NULL && s->size != 0) 6282 return FALSE; 6283 6284 /* Fill in the version definition section. */ 6285 6286 p = s->contents; 6287 6288 def.vd_version = VER_DEF_CURRENT; 6289 def.vd_flags = VER_FLG_BASE; 6290 def.vd_ndx = 1; 6291 def.vd_cnt = 1; 6292 if (info->create_default_symver) 6293 { 6294 def.vd_aux = 2 * sizeof (Elf_External_Verdef); 6295 def.vd_next = sizeof (Elf_External_Verdef); 6296 } 6297 else 6298 { 6299 def.vd_aux = sizeof (Elf_External_Verdef); 6300 def.vd_next = (sizeof (Elf_External_Verdef) 6301 + sizeof (Elf_External_Verdaux)); 6302 } 6303 6304 if (soname_indx != (size_t) -1) 6305 { 6306 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6307 soname_indx); 6308 def.vd_hash = bfd_elf_hash (soname); 6309 defaux.vda_name = soname_indx; 6310 name = soname; 6311 } 6312 else 6313 { 6314 size_t indx; 6315 6316 name = lbasename (output_bfd->filename); 6317 def.vd_hash = bfd_elf_hash (name); 6318 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6319 name, FALSE); 6320 if (indx == (size_t) -1) 6321 return FALSE; 6322 defaux.vda_name = indx; 6323 } 6324 defaux.vda_next = 0; 6325 6326 _bfd_elf_swap_verdef_out (output_bfd, &def, 6327 (Elf_External_Verdef *) p); 6328 p += sizeof (Elf_External_Verdef); 6329 if (info->create_default_symver) 6330 { 6331 /* Add a symbol representing this version. */ 6332 bh = NULL; 6333 if (! (_bfd_generic_link_add_one_symbol 6334 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, 6335 0, NULL, FALSE, 6336 get_elf_backend_data (dynobj)->collect, &bh))) 6337 return FALSE; 6338 h = (struct elf_link_hash_entry *) bh; 6339 h->non_elf = 0; 6340 h->def_regular = 1; 6341 h->type = STT_OBJECT; 6342 h->verinfo.vertree = NULL; 6343 6344 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6345 return FALSE; 6346 6347 /* Create a duplicate of the base version with the same 6348 aux block, but different flags. */ 6349 def.vd_flags = 0; 6350 def.vd_ndx = 2; 6351 def.vd_aux = sizeof (Elf_External_Verdef); 6352 if (verdefs) 6353 def.vd_next = (sizeof (Elf_External_Verdef) 6354 + sizeof (Elf_External_Verdaux)); 6355 else 6356 def.vd_next = 0; 6357 _bfd_elf_swap_verdef_out (output_bfd, &def, 6358 (Elf_External_Verdef *) p); 6359 p += sizeof (Elf_External_Verdef); 6360 } 6361 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6362 (Elf_External_Verdaux *) p); 6363 p += sizeof (Elf_External_Verdaux); 6364 6365 for (t = verdefs; t != NULL; t = t->next) 6366 { 6367 unsigned int cdeps; 6368 struct bfd_elf_version_deps *n; 6369 6370 /* Don't emit the base version twice. */ 6371 if (t->vernum == 0) 6372 continue; 6373 6374 cdeps = 0; 6375 for (n = t->deps; n != NULL; n = n->next) 6376 ++cdeps; 6377 6378 /* Add a symbol representing this version. */ 6379 bh = NULL; 6380 if (! (_bfd_generic_link_add_one_symbol 6381 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, 6382 0, NULL, FALSE, 6383 get_elf_backend_data (dynobj)->collect, &bh))) 6384 return FALSE; 6385 h = (struct elf_link_hash_entry *) bh; 6386 h->non_elf = 0; 6387 h->def_regular = 1; 6388 h->type = STT_OBJECT; 6389 h->verinfo.vertree = t; 6390 6391 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6392 return FALSE; 6393 6394 def.vd_version = VER_DEF_CURRENT; 6395 def.vd_flags = 0; 6396 if (t->globals.list == NULL 6397 && t->locals.list == NULL 6398 && ! t->used) 6399 def.vd_flags |= VER_FLG_WEAK; 6400 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); 6401 def.vd_cnt = cdeps + 1; 6402 def.vd_hash = bfd_elf_hash (t->name); 6403 def.vd_aux = sizeof (Elf_External_Verdef); 6404 def.vd_next = 0; 6405 6406 /* If a basever node is next, it *must* be the last node in 6407 the chain, otherwise Verdef construction breaks. */ 6408 if (t->next != NULL && t->next->vernum == 0) 6409 BFD_ASSERT (t->next->next == NULL); 6410 6411 if (t->next != NULL && t->next->vernum != 0) 6412 def.vd_next = (sizeof (Elf_External_Verdef) 6413 + (cdeps + 1) * sizeof (Elf_External_Verdaux)); 6414 6415 _bfd_elf_swap_verdef_out (output_bfd, &def, 6416 (Elf_External_Verdef *) p); 6417 p += sizeof (Elf_External_Verdef); 6418 6419 defaux.vda_name = h->dynstr_index; 6420 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6421 h->dynstr_index); 6422 defaux.vda_next = 0; 6423 if (t->deps != NULL) 6424 defaux.vda_next = sizeof (Elf_External_Verdaux); 6425 t->name_indx = defaux.vda_name; 6426 6427 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6428 (Elf_External_Verdaux *) p); 6429 p += sizeof (Elf_External_Verdaux); 6430 6431 for (n = t->deps; n != NULL; n = n->next) 6432 { 6433 if (n->version_needed == NULL) 6434 { 6435 /* This can happen if there was an error in the 6436 version script. */ 6437 defaux.vda_name = 0; 6438 } 6439 else 6440 { 6441 defaux.vda_name = n->version_needed->name_indx; 6442 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6443 defaux.vda_name); 6444 } 6445 if (n->next == NULL) 6446 defaux.vda_next = 0; 6447 else 6448 defaux.vda_next = sizeof (Elf_External_Verdaux); 6449 6450 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6451 (Elf_External_Verdaux *) p); 6452 p += sizeof (Elf_External_Verdaux); 6453 } 6454 } 6455 6456 elf_tdata (output_bfd)->cverdefs = cdefs; 6457 } 6458 } 6459 6460 bed = get_elf_backend_data (output_bfd); 6461 6462 if (info->gc_sections && bed->can_gc_sections) 6463 { 6464 struct elf_gc_sweep_symbol_info sweep_info; 6465 6466 /* Remove the symbols that were in the swept sections from the 6467 dynamic symbol table. */ 6468 sweep_info.info = info; 6469 sweep_info.hide_symbol = bed->elf_backend_hide_symbol; 6470 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, 6471 &sweep_info); 6472 } 6473 6474 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6475 { 6476 asection *s; 6477 struct elf_find_verdep_info sinfo; 6478 6479 /* Work out the size of the version reference section. */ 6480 6481 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 6482 BFD_ASSERT (s != NULL); 6483 6484 sinfo.info = info; 6485 sinfo.vers = elf_tdata (output_bfd)->cverdefs; 6486 if (sinfo.vers == 0) 6487 sinfo.vers = 1; 6488 sinfo.failed = FALSE; 6489 6490 elf_link_hash_traverse (elf_hash_table (info), 6491 _bfd_elf_link_find_version_dependencies, 6492 &sinfo); 6493 if (sinfo.failed) 6494 return FALSE; 6495 6496 if (elf_tdata (output_bfd)->verref == NULL) 6497 s->flags |= SEC_EXCLUDE; 6498 else 6499 { 6500 Elf_Internal_Verneed *vn; 6501 unsigned int size; 6502 unsigned int crefs; 6503 bfd_byte *p; 6504 6505 /* Build the version dependency section. */ 6506 size = 0; 6507 crefs = 0; 6508 for (vn = elf_tdata (output_bfd)->verref; 6509 vn != NULL; 6510 vn = vn->vn_nextref) 6511 { 6512 Elf_Internal_Vernaux *a; 6513 6514 size += sizeof (Elf_External_Verneed); 6515 ++crefs; 6516 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 6517 size += sizeof (Elf_External_Vernaux); 6518 } 6519 6520 s->size = size; 6521 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6522 if (s->contents == NULL) 6523 return FALSE; 6524 6525 p = s->contents; 6526 for (vn = elf_tdata (output_bfd)->verref; 6527 vn != NULL; 6528 vn = vn->vn_nextref) 6529 { 6530 unsigned int caux; 6531 Elf_Internal_Vernaux *a; 6532 size_t indx; 6533 6534 caux = 0; 6535 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 6536 ++caux; 6537 6538 vn->vn_version = VER_NEED_CURRENT; 6539 vn->vn_cnt = caux; 6540 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6541 elf_dt_name (vn->vn_bfd) != NULL 6542 ? elf_dt_name (vn->vn_bfd) 6543 : lbasename (vn->vn_bfd->filename), 6544 FALSE); 6545 if (indx == (size_t) -1) 6546 return FALSE; 6547 vn->vn_file = indx; 6548 vn->vn_aux = sizeof (Elf_External_Verneed); 6549 if (vn->vn_nextref == NULL) 6550 vn->vn_next = 0; 6551 else 6552 vn->vn_next = (sizeof (Elf_External_Verneed) 6553 + caux * sizeof (Elf_External_Vernaux)); 6554 6555 _bfd_elf_swap_verneed_out (output_bfd, vn, 6556 (Elf_External_Verneed *) p); 6557 p += sizeof (Elf_External_Verneed); 6558 6559 for (a = vn->vn_auxptr; a != NULL; a = a->vna_nextptr) 6560 { 6561 a->vna_hash = bfd_elf_hash (a->vna_nodename); 6562 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6563 a->vna_nodename, FALSE); 6564 if (indx == (size_t) -1) 6565 return FALSE; 6566 a->vna_name = indx; 6567 if (a->vna_nextptr == NULL) 6568 a->vna_next = 0; 6569 else 6570 a->vna_next = sizeof (Elf_External_Vernaux); 6571 6572 _bfd_elf_swap_vernaux_out (output_bfd, a, 6573 (Elf_External_Vernaux *) p); 6574 p += sizeof (Elf_External_Vernaux); 6575 } 6576 } 6577 6578 elf_tdata (output_bfd)->cverrefs = crefs; 6579 } 6580 } 6581 6582 /* Any syms created from now on start with -1 in 6583 got.refcount/offset and plt.refcount/offset. */ 6584 elf_hash_table (info)->init_got_refcount 6585 = elf_hash_table (info)->init_got_offset; 6586 elf_hash_table (info)->init_plt_refcount 6587 = elf_hash_table (info)->init_plt_offset; 6588 6589 if (bfd_link_relocatable (info) 6590 && !_bfd_elf_size_group_sections (info)) 6591 return FALSE; 6592 6593 /* The backend may have to create some sections regardless of whether 6594 we're dynamic or not. */ 6595 if (bed->elf_backend_always_size_sections 6596 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) 6597 return FALSE; 6598 6599 /* Determine any GNU_STACK segment requirements, after the backend 6600 has had a chance to set a default segment size. */ 6601 if (info->execstack) 6602 elf_stack_flags (output_bfd) = PF_R | PF_W | PF_X; 6603 else if (info->noexecstack) 6604 elf_stack_flags (output_bfd) = PF_R | PF_W; 6605 else 6606 { 6607 bfd *inputobj; 6608 asection *notesec = NULL; 6609 int exec = 0; 6610 6611 for (inputobj = info->input_bfds; 6612 inputobj; 6613 inputobj = inputobj->link.next) 6614 { 6615 asection *s; 6616 6617 if (inputobj->flags 6618 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) 6619 continue; 6620 s = inputobj->sections; 6621 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 6622 continue; 6623 6624 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); 6625 if (s) 6626 { 6627 if (s->flags & SEC_CODE) 6628 exec = PF_X; 6629 notesec = s; 6630 } 6631 else if (bed->default_execstack) 6632 exec = PF_X; 6633 } 6634 if (notesec || info->stacksize > 0) 6635 elf_stack_flags (output_bfd) = PF_R | PF_W | exec; 6636 if (notesec && exec && bfd_link_relocatable (info) 6637 && notesec->output_section != bfd_abs_section_ptr) 6638 notesec->output_section->flags |= SEC_CODE; 6639 } 6640 6641 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6642 { 6643 struct elf_info_failed eif; 6644 struct elf_link_hash_entry *h; 6645 asection *dynstr; 6646 asection *s; 6647 6648 *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); 6649 BFD_ASSERT (*sinterpptr != NULL || !bfd_link_executable (info) || info->nointerp); 6650 6651 if (info->symbolic) 6652 { 6653 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) 6654 return FALSE; 6655 info->flags |= DF_SYMBOLIC; 6656 } 6657 6658 if (rpath != NULL) 6659 { 6660 size_t indx; 6661 bfd_vma tag; 6662 6663 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, 6664 TRUE); 6665 if (indx == (size_t) -1) 6666 return FALSE; 6667 6668 tag = info->new_dtags ? DT_RUNPATH : DT_RPATH; 6669 if (!_bfd_elf_add_dynamic_entry (info, tag, indx)) 6670 return FALSE; 6671 } 6672 6673 if (filter_shlib != NULL) 6674 { 6675 size_t indx; 6676 6677 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6678 filter_shlib, TRUE); 6679 if (indx == (size_t) -1 6680 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) 6681 return FALSE; 6682 } 6683 6684 if (auxiliary_filters != NULL) 6685 { 6686 const char * const *p; 6687 6688 for (p = auxiliary_filters; *p != NULL; p++) 6689 { 6690 size_t indx; 6691 6692 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6693 *p, TRUE); 6694 if (indx == (size_t) -1 6695 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) 6696 return FALSE; 6697 } 6698 } 6699 6700 if (audit != NULL) 6701 { 6702 size_t indx; 6703 6704 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, 6705 TRUE); 6706 if (indx == (size_t) -1 6707 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) 6708 return FALSE; 6709 } 6710 6711 if (depaudit != NULL) 6712 { 6713 size_t indx; 6714 6715 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, 6716 TRUE); 6717 if (indx == (size_t) -1 6718 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) 6719 return FALSE; 6720 } 6721 6722 eif.info = info; 6723 eif.failed = FALSE; 6724 6725 /* Find all symbols which were defined in a dynamic object and make 6726 the backend pick a reasonable value for them. */ 6727 elf_link_hash_traverse (elf_hash_table (info), 6728 _bfd_elf_adjust_dynamic_symbol, 6729 &eif); 6730 if (eif.failed) 6731 return FALSE; 6732 6733 /* Add some entries to the .dynamic section. We fill in some of the 6734 values later, in bfd_elf_final_link, but we must add the entries 6735 now so that we know the final size of the .dynamic section. */ 6736 6737 /* If there are initialization and/or finalization functions to 6738 call then add the corresponding DT_INIT/DT_FINI entries. */ 6739 h = (info->init_function 6740 ? elf_link_hash_lookup (elf_hash_table (info), 6741 info->init_function, FALSE, 6742 FALSE, FALSE) 6743 : NULL); 6744 if (h != NULL 6745 && (h->ref_regular 6746 || h->def_regular)) 6747 { 6748 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) 6749 return FALSE; 6750 } 6751 h = (info->fini_function 6752 ? elf_link_hash_lookup (elf_hash_table (info), 6753 info->fini_function, FALSE, 6754 FALSE, FALSE) 6755 : NULL); 6756 if (h != NULL 6757 && (h->ref_regular 6758 || h->def_regular)) 6759 { 6760 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) 6761 return FALSE; 6762 } 6763 6764 s = bfd_get_section_by_name (output_bfd, ".preinit_array"); 6765 if (s != NULL && s->linker_has_input) 6766 { 6767 /* DT_PREINIT_ARRAY is not allowed in shared library. */ 6768 if (! bfd_link_executable (info)) 6769 { 6770 bfd *sub; 6771 asection *o; 6772 6773 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 6774 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 6775 && (o = sub->sections) != NULL 6776 && o->sec_info_type != SEC_INFO_TYPE_JUST_SYMS) 6777 for (o = sub->sections; o != NULL; o = o->next) 6778 if (elf_section_data (o)->this_hdr.sh_type 6779 == SHT_PREINIT_ARRAY) 6780 { 6781 _bfd_error_handler 6782 (_("%B: .preinit_array section is not allowed in DSO"), 6783 sub); 6784 break; 6785 } 6786 6787 bfd_set_error (bfd_error_nonrepresentable_section); 6788 return FALSE; 6789 } 6790 6791 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) 6792 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) 6793 return FALSE; 6794 } 6795 s = bfd_get_section_by_name (output_bfd, ".init_array"); 6796 if (s != NULL && s->linker_has_input) 6797 { 6798 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) 6799 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) 6800 return FALSE; 6801 } 6802 s = bfd_get_section_by_name (output_bfd, ".fini_array"); 6803 if (s != NULL && s->linker_has_input) 6804 { 6805 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) 6806 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) 6807 return FALSE; 6808 } 6809 6810 dynstr = bfd_get_linker_section (dynobj, ".dynstr"); 6811 /* If .dynstr is excluded from the link, we don't want any of 6812 these tags. Strictly, we should be checking each section 6813 individually; This quick check covers for the case where 6814 someone does a /DISCARD/ : { *(*) }. */ 6815 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) 6816 { 6817 bfd_size_type strsize; 6818 6819 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 6820 if ((info->emit_hash 6821 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) 6822 || (info->emit_gnu_hash 6823 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) 6824 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) 6825 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) 6826 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) 6827 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, 6828 bed->s->sizeof_sym)) 6829 return FALSE; 6830 } 6831 } 6832 6833 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) 6834 return FALSE; 6835 6836 /* The backend must work out the sizes of all the other dynamic 6837 sections. */ 6838 if (dynobj != NULL 6839 && bed->elf_backend_size_dynamic_sections != NULL 6840 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) 6841 return FALSE; 6842 6843 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 6844 { 6845 if (elf_tdata (output_bfd)->cverdefs) 6846 { 6847 unsigned int crefs = elf_tdata (output_bfd)->cverdefs; 6848 6849 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) 6850 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, crefs)) 6851 return FALSE; 6852 } 6853 6854 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) 6855 { 6856 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) 6857 return FALSE; 6858 } 6859 else if (info->flags & DF_BIND_NOW) 6860 { 6861 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) 6862 return FALSE; 6863 } 6864 6865 if (info->flags_1) 6866 { 6867 if (bfd_link_executable (info)) 6868 info->flags_1 &= ~ (DF_1_INITFIRST 6869 | DF_1_NODELETE 6870 | DF_1_NOOPEN); 6871 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) 6872 return FALSE; 6873 } 6874 6875 if (elf_tdata (output_bfd)->cverrefs) 6876 { 6877 unsigned int crefs = elf_tdata (output_bfd)->cverrefs; 6878 6879 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) 6880 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) 6881 return FALSE; 6882 } 6883 6884 if ((elf_tdata (output_bfd)->cverrefs == 0 6885 && elf_tdata (output_bfd)->cverdefs == 0) 6886 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, NULL) <= 1) 6887 { 6888 asection *s; 6889 6890 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6891 s->flags |= SEC_EXCLUDE; 6892 } 6893 } 6894 return TRUE; 6895 } 6896 6897 /* Find the first non-excluded output section. We'll use its 6898 section symbol for some emitted relocs. */ 6899 void 6900 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) 6901 { 6902 asection *s; 6903 6904 for (s = output_bfd->sections; s != NULL; s = s->next) 6905 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 6906 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6907 { 6908 elf_hash_table (info)->text_index_section = s; 6909 break; 6910 } 6911 } 6912 6913 /* Find two non-excluded output sections, one for code, one for data. 6914 We'll use their section symbols for some emitted relocs. */ 6915 void 6916 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) 6917 { 6918 asection *s; 6919 6920 /* Data first, since setting text_index_section changes 6921 _bfd_elf_link_omit_section_dynsym. */ 6922 for (s = output_bfd->sections; s != NULL; s = s->next) 6923 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) 6924 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6925 { 6926 elf_hash_table (info)->data_index_section = s; 6927 break; 6928 } 6929 6930 for (s = output_bfd->sections; s != NULL; s = s->next) 6931 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) 6932 == (SEC_ALLOC | SEC_READONLY)) 6933 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6934 { 6935 elf_hash_table (info)->text_index_section = s; 6936 break; 6937 } 6938 6939 if (elf_hash_table (info)->text_index_section == NULL) 6940 elf_hash_table (info)->text_index_section 6941 = elf_hash_table (info)->data_index_section; 6942 } 6943 6944 bfd_boolean 6945 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) 6946 { 6947 const struct elf_backend_data *bed; 6948 unsigned long section_sym_count; 6949 bfd_size_type dynsymcount = 0; 6950 6951 if (!is_elf_hash_table (info->hash)) 6952 return TRUE; 6953 6954 bed = get_elf_backend_data (output_bfd); 6955 (*bed->elf_backend_init_index_section) (output_bfd, info); 6956 6957 /* Assign dynsym indices. In a shared library we generate a section 6958 symbol for each output section, which come first. Next come all 6959 of the back-end allocated local dynamic syms, followed by the rest 6960 of the global symbols. 6961 6962 This is usually not needed for static binaries, however backends 6963 can request to always do it, e.g. the MIPS backend uses dynamic 6964 symbol counts to lay out GOT, which will be produced in the 6965 presence of GOT relocations even in static binaries (holding fixed 6966 data in that case, to satisfy those relocations). */ 6967 6968 if (elf_hash_table (info)->dynamic_sections_created 6969 || bed->always_renumber_dynsyms) 6970 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, 6971 §ion_sym_count); 6972 6973 if (elf_hash_table (info)->dynamic_sections_created) 6974 { 6975 bfd *dynobj; 6976 asection *s; 6977 unsigned int dtagcount; 6978 6979 dynobj = elf_hash_table (info)->dynobj; 6980 6981 /* Work out the size of the symbol version section. */ 6982 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6983 BFD_ASSERT (s != NULL); 6984 if ((s->flags & SEC_EXCLUDE) == 0) 6985 { 6986 s->size = dynsymcount * sizeof (Elf_External_Versym); 6987 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6988 if (s->contents == NULL) 6989 return FALSE; 6990 6991 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) 6992 return FALSE; 6993 } 6994 6995 /* Set the size of the .dynsym and .hash sections. We counted 6996 the number of dynamic symbols in elf_link_add_object_symbols. 6997 We will build the contents of .dynsym and .hash when we build 6998 the final symbol table, because until then we do not know the 6999 correct value to give the symbols. We built the .dynstr 7000 section as we went along in elf_link_add_object_symbols. */ 7001 s = elf_hash_table (info)->dynsym; 7002 BFD_ASSERT (s != NULL); 7003 s->size = dynsymcount * bed->s->sizeof_sym; 7004 7005 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 7006 if (s->contents == NULL) 7007 return FALSE; 7008 7009 /* The first entry in .dynsym is a dummy symbol. Clear all the 7010 section syms, in case we don't output them all. */ 7011 ++section_sym_count; 7012 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); 7013 7014 elf_hash_table (info)->bucketcount = 0; 7015 7016 /* Compute the size of the hashing table. As a side effect this 7017 computes the hash values for all the names we export. */ 7018 if (info->emit_hash) 7019 { 7020 unsigned long int *hashcodes; 7021 struct hash_codes_info hashinf; 7022 bfd_size_type amt; 7023 unsigned long int nsyms; 7024 size_t bucketcount; 7025 size_t hash_entry_size; 7026 7027 /* Compute the hash values for all exported symbols. At the same 7028 time store the values in an array so that we could use them for 7029 optimizations. */ 7030 amt = dynsymcount * sizeof (unsigned long int); 7031 hashcodes = (unsigned long int *) bfd_malloc (amt); 7032 if (hashcodes == NULL) 7033 return FALSE; 7034 hashinf.hashcodes = hashcodes; 7035 hashinf.error = FALSE; 7036 7037 /* Put all hash values in HASHCODES. */ 7038 elf_link_hash_traverse (elf_hash_table (info), 7039 elf_collect_hash_codes, &hashinf); 7040 if (hashinf.error) 7041 { 7042 free (hashcodes); 7043 return FALSE; 7044 } 7045 7046 nsyms = hashinf.hashcodes - hashcodes; 7047 bucketcount 7048 = compute_bucket_count (info, hashcodes, nsyms, 0); 7049 free (hashcodes); 7050 7051 if (bucketcount == 0 && nsyms > 0) 7052 return FALSE; 7053 7054 elf_hash_table (info)->bucketcount = bucketcount; 7055 7056 s = bfd_get_linker_section (dynobj, ".hash"); 7057 BFD_ASSERT (s != NULL); 7058 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; 7059 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); 7060 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7061 if (s->contents == NULL) 7062 return FALSE; 7063 7064 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); 7065 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, 7066 s->contents + hash_entry_size); 7067 } 7068 7069 if (info->emit_gnu_hash) 7070 { 7071 size_t i, cnt; 7072 unsigned char *contents; 7073 struct collect_gnu_hash_codes cinfo; 7074 bfd_size_type amt; 7075 size_t bucketcount; 7076 7077 memset (&cinfo, 0, sizeof (cinfo)); 7078 7079 /* Compute the hash values for all exported symbols. At the same 7080 time store the values in an array so that we could use them for 7081 optimizations. */ 7082 amt = dynsymcount * 2 * sizeof (unsigned long int); 7083 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); 7084 if (cinfo.hashcodes == NULL) 7085 return FALSE; 7086 7087 cinfo.hashval = cinfo.hashcodes + dynsymcount; 7088 cinfo.min_dynindx = -1; 7089 cinfo.output_bfd = output_bfd; 7090 cinfo.bed = bed; 7091 7092 /* Put all hash values in HASHCODES. */ 7093 elf_link_hash_traverse (elf_hash_table (info), 7094 elf_collect_gnu_hash_codes, &cinfo); 7095 if (cinfo.error) 7096 { 7097 free (cinfo.hashcodes); 7098 return FALSE; 7099 } 7100 7101 bucketcount 7102 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); 7103 7104 if (bucketcount == 0) 7105 { 7106 free (cinfo.hashcodes); 7107 return FALSE; 7108 } 7109 7110 s = bfd_get_linker_section (dynobj, ".gnu.hash"); 7111 BFD_ASSERT (s != NULL); 7112 7113 if (cinfo.nsyms == 0) 7114 { 7115 /* Empty .gnu.hash section is special. */ 7116 BFD_ASSERT (cinfo.min_dynindx == -1); 7117 free (cinfo.hashcodes); 7118 s->size = 5 * 4 + bed->s->arch_size / 8; 7119 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7120 if (contents == NULL) 7121 return FALSE; 7122 s->contents = contents; 7123 /* 1 empty bucket. */ 7124 bfd_put_32 (output_bfd, 1, contents); 7125 /* SYMIDX above the special symbol 0. */ 7126 bfd_put_32 (output_bfd, 1, contents + 4); 7127 /* Just one word for bitmask. */ 7128 bfd_put_32 (output_bfd, 1, contents + 8); 7129 /* Only hash fn bloom filter. */ 7130 bfd_put_32 (output_bfd, 0, contents + 12); 7131 /* No hashes are valid - empty bitmask. */ 7132 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); 7133 /* No hashes in the only bucket. */ 7134 bfd_put_32 (output_bfd, 0, 7135 contents + 16 + bed->s->arch_size / 8); 7136 } 7137 else 7138 { 7139 unsigned long int maskwords, maskbitslog2, x; 7140 BFD_ASSERT (cinfo.min_dynindx != -1); 7141 7142 x = cinfo.nsyms; 7143 maskbitslog2 = 1; 7144 while ((x >>= 1) != 0) 7145 ++maskbitslog2; 7146 if (maskbitslog2 < 3) 7147 maskbitslog2 = 5; 7148 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) 7149 maskbitslog2 = maskbitslog2 + 3; 7150 else 7151 maskbitslog2 = maskbitslog2 + 2; 7152 if (bed->s->arch_size == 64) 7153 { 7154 if (maskbitslog2 == 5) 7155 maskbitslog2 = 6; 7156 cinfo.shift1 = 6; 7157 } 7158 else 7159 cinfo.shift1 = 5; 7160 cinfo.mask = (1 << cinfo.shift1) - 1; 7161 cinfo.shift2 = maskbitslog2; 7162 cinfo.maskbits = 1 << maskbitslog2; 7163 maskwords = 1 << (maskbitslog2 - cinfo.shift1); 7164 amt = bucketcount * sizeof (unsigned long int) * 2; 7165 amt += maskwords * sizeof (bfd_vma); 7166 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); 7167 if (cinfo.bitmask == NULL) 7168 { 7169 free (cinfo.hashcodes); 7170 return FALSE; 7171 } 7172 7173 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); 7174 cinfo.indx = cinfo.counts + bucketcount; 7175 cinfo.symindx = dynsymcount - cinfo.nsyms; 7176 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); 7177 7178 /* Determine how often each hash bucket is used. */ 7179 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); 7180 for (i = 0; i < cinfo.nsyms; ++i) 7181 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; 7182 7183 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) 7184 if (cinfo.counts[i] != 0) 7185 { 7186 cinfo.indx[i] = cnt; 7187 cnt += cinfo.counts[i]; 7188 } 7189 BFD_ASSERT (cnt == dynsymcount); 7190 cinfo.bucketcount = bucketcount; 7191 cinfo.local_indx = cinfo.min_dynindx; 7192 7193 s->size = (4 + bucketcount + cinfo.nsyms) * 4; 7194 s->size += cinfo.maskbits / 8; 7195 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 7196 if (contents == NULL) 7197 { 7198 free (cinfo.bitmask); 7199 free (cinfo.hashcodes); 7200 return FALSE; 7201 } 7202 7203 s->contents = contents; 7204 bfd_put_32 (output_bfd, bucketcount, contents); 7205 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); 7206 bfd_put_32 (output_bfd, maskwords, contents + 8); 7207 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); 7208 contents += 16 + cinfo.maskbits / 8; 7209 7210 for (i = 0; i < bucketcount; ++i) 7211 { 7212 if (cinfo.counts[i] == 0) 7213 bfd_put_32 (output_bfd, 0, contents); 7214 else 7215 bfd_put_32 (output_bfd, cinfo.indx[i], contents); 7216 contents += 4; 7217 } 7218 7219 cinfo.contents = contents; 7220 7221 /* Renumber dynamic symbols, populate .gnu.hash section. */ 7222 elf_link_hash_traverse (elf_hash_table (info), 7223 elf_renumber_gnu_hash_syms, &cinfo); 7224 7225 contents = s->contents + 16; 7226 for (i = 0; i < maskwords; ++i) 7227 { 7228 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], 7229 contents); 7230 contents += bed->s->arch_size / 8; 7231 } 7232 7233 free (cinfo.bitmask); 7234 free (cinfo.hashcodes); 7235 } 7236 } 7237 7238 s = bfd_get_linker_section (dynobj, ".dynstr"); 7239 BFD_ASSERT (s != NULL); 7240 7241 elf_finalize_dynstr (output_bfd, info); 7242 7243 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 7244 7245 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) 7246 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) 7247 return FALSE; 7248 } 7249 7250 return TRUE; 7251 } 7252 7253 /* Make sure sec_info_type is cleared if sec_info is cleared too. */ 7254 7255 static void 7256 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, 7257 asection *sec) 7258 { 7259 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); 7260 sec->sec_info_type = SEC_INFO_TYPE_NONE; 7261 } 7262 7263 /* Finish SHF_MERGE section merging. */ 7264 7265 bfd_boolean 7266 _bfd_elf_merge_sections (bfd *obfd, struct bfd_link_info *info) 7267 { 7268 bfd *ibfd; 7269 asection *sec; 7270 7271 if (!is_elf_hash_table (info->hash)) 7272 return FALSE; 7273 7274 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 7275 if ((ibfd->flags & DYNAMIC) == 0 7276 && bfd_get_flavour (ibfd) == bfd_target_elf_flavour 7277 && (elf_elfheader (ibfd)->e_ident[EI_CLASS] 7278 == get_elf_backend_data (obfd)->s->elfclass)) 7279 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 7280 if ((sec->flags & SEC_MERGE) != 0 7281 && !bfd_is_abs_section (sec->output_section)) 7282 { 7283 struct bfd_elf_section_data *secdata; 7284 7285 secdata = elf_section_data (sec); 7286 if (! _bfd_add_merge_section (obfd, 7287 &elf_hash_table (info)->merge_info, 7288 sec, &secdata->sec_info)) 7289 return FALSE; 7290 else if (secdata->sec_info) 7291 sec->sec_info_type = SEC_INFO_TYPE_MERGE; 7292 } 7293 7294 if (elf_hash_table (info)->merge_info != NULL) 7295 _bfd_merge_sections (obfd, info, elf_hash_table (info)->merge_info, 7296 merge_sections_remove_hook); 7297 return TRUE; 7298 } 7299 7300 /* Create an entry in an ELF linker hash table. */ 7301 7302 struct bfd_hash_entry * 7303 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 7304 struct bfd_hash_table *table, 7305 const char *string) 7306 { 7307 /* Allocate the structure if it has not already been allocated by a 7308 subclass. */ 7309 if (entry == NULL) 7310 { 7311 entry = (struct bfd_hash_entry *) 7312 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 7313 if (entry == NULL) 7314 return entry; 7315 } 7316 7317 /* Call the allocation method of the superclass. */ 7318 entry = _bfd_link_hash_newfunc (entry, table, string); 7319 if (entry != NULL) 7320 { 7321 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 7322 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 7323 7324 /* Set local fields. */ 7325 ret->indx = -1; 7326 ret->dynindx = -1; 7327 ret->got = htab->init_got_refcount; 7328 ret->plt = htab->init_plt_refcount; 7329 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) 7330 - offsetof (struct elf_link_hash_entry, size))); 7331 /* Assume that we have been called by a non-ELF symbol reader. 7332 This flag is then reset by the code which reads an ELF input 7333 file. This ensures that a symbol created by a non-ELF symbol 7334 reader will have the flag set correctly. */ 7335 ret->non_elf = 1; 7336 } 7337 7338 return entry; 7339 } 7340 7341 /* Copy data from an indirect symbol to its direct symbol, hiding the 7342 old indirect symbol. Also used for copying flags to a weakdef. */ 7343 7344 void 7345 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, 7346 struct elf_link_hash_entry *dir, 7347 struct elf_link_hash_entry *ind) 7348 { 7349 struct elf_link_hash_table *htab; 7350 7351 /* Copy down any references that we may have already seen to the 7352 symbol which just became indirect. */ 7353 7354 if (dir->versioned != versioned_hidden) 7355 dir->ref_dynamic |= ind->ref_dynamic; 7356 dir->ref_regular |= ind->ref_regular; 7357 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 7358 dir->non_got_ref |= ind->non_got_ref; 7359 dir->needs_plt |= ind->needs_plt; 7360 dir->pointer_equality_needed |= ind->pointer_equality_needed; 7361 7362 if (ind->root.type != bfd_link_hash_indirect) 7363 return; 7364 7365 /* Copy over the global and procedure linkage table refcount entries. 7366 These may have been already set up by a check_relocs routine. */ 7367 htab = elf_hash_table (info); 7368 if (ind->got.refcount > htab->init_got_refcount.refcount) 7369 { 7370 if (dir->got.refcount < 0) 7371 dir->got.refcount = 0; 7372 dir->got.refcount += ind->got.refcount; 7373 ind->got.refcount = htab->init_got_refcount.refcount; 7374 } 7375 7376 if (ind->plt.refcount > htab->init_plt_refcount.refcount) 7377 { 7378 if (dir->plt.refcount < 0) 7379 dir->plt.refcount = 0; 7380 dir->plt.refcount += ind->plt.refcount; 7381 ind->plt.refcount = htab->init_plt_refcount.refcount; 7382 } 7383 7384 if (ind->dynindx != -1) 7385 { 7386 if (dir->dynindx != -1) 7387 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); 7388 dir->dynindx = ind->dynindx; 7389 dir->dynstr_index = ind->dynstr_index; 7390 ind->dynindx = -1; 7391 ind->dynstr_index = 0; 7392 } 7393 } 7394 7395 void 7396 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, 7397 struct elf_link_hash_entry *h, 7398 bfd_boolean force_local) 7399 { 7400 /* STT_GNU_IFUNC symbol must go through PLT. */ 7401 if (h->type != STT_GNU_IFUNC) 7402 { 7403 h->plt = elf_hash_table (info)->init_plt_offset; 7404 h->needs_plt = 0; 7405 } 7406 if (force_local) 7407 { 7408 h->forced_local = 1; 7409 if (h->dynindx != -1) 7410 { 7411 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 7412 h->dynstr_index); 7413 h->dynindx = -1; 7414 h->dynstr_index = 0; 7415 } 7416 } 7417 } 7418 7419 /* Initialize an ELF linker hash table. *TABLE has been zeroed by our 7420 caller. */ 7421 7422 bfd_boolean 7423 _bfd_elf_link_hash_table_init 7424 (struct elf_link_hash_table *table, 7425 bfd *abfd, 7426 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 7427 struct bfd_hash_table *, 7428 const char *), 7429 unsigned int entsize, 7430 enum elf_target_id target_id) 7431 { 7432 bfd_boolean ret; 7433 int can_refcount = get_elf_backend_data (abfd)->can_refcount; 7434 7435 table->init_got_refcount.refcount = can_refcount - 1; 7436 table->init_plt_refcount.refcount = can_refcount - 1; 7437 table->init_got_offset.offset = -(bfd_vma) 1; 7438 table->init_plt_offset.offset = -(bfd_vma) 1; 7439 /* The first dynamic symbol is a dummy. */ 7440 table->dynsymcount = 1; 7441 7442 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 7443 7444 table->root.type = bfd_link_elf_hash_table; 7445 table->hash_table_id = target_id; 7446 7447 return ret; 7448 } 7449 7450 /* Create an ELF linker hash table. */ 7451 7452 struct bfd_link_hash_table * 7453 _bfd_elf_link_hash_table_create (bfd *abfd) 7454 { 7455 struct elf_link_hash_table *ret; 7456 bfd_size_type amt = sizeof (struct elf_link_hash_table); 7457 7458 ret = (struct elf_link_hash_table *) bfd_zmalloc (amt); 7459 if (ret == NULL) 7460 return NULL; 7461 7462 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, 7463 sizeof (struct elf_link_hash_entry), 7464 GENERIC_ELF_DATA)) 7465 { 7466 free (ret); 7467 return NULL; 7468 } 7469 ret->root.hash_table_free = _bfd_elf_link_hash_table_free; 7470 7471 return &ret->root; 7472 } 7473 7474 /* Destroy an ELF linker hash table. */ 7475 7476 void 7477 _bfd_elf_link_hash_table_free (bfd *obfd) 7478 { 7479 struct elf_link_hash_table *htab; 7480 7481 htab = (struct elf_link_hash_table *) obfd->link.hash; 7482 if (htab->dynstr != NULL) 7483 _bfd_elf_strtab_free (htab->dynstr); 7484 _bfd_merge_sections_free (htab->merge_info); 7485 _bfd_generic_link_hash_table_free (obfd); 7486 } 7487 7488 /* This is a hook for the ELF emulation code in the generic linker to 7489 tell the backend linker what file name to use for the DT_NEEDED 7490 entry for a dynamic object. */ 7491 7492 void 7493 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) 7494 { 7495 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7496 && bfd_get_format (abfd) == bfd_object) 7497 elf_dt_name (abfd) = name; 7498 } 7499 7500 int 7501 bfd_elf_get_dyn_lib_class (bfd *abfd) 7502 { 7503 int lib_class; 7504 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7505 && bfd_get_format (abfd) == bfd_object) 7506 lib_class = elf_dyn_lib_class (abfd); 7507 else 7508 lib_class = 0; 7509 return lib_class; 7510 } 7511 7512 void 7513 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) 7514 { 7515 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7516 && bfd_get_format (abfd) == bfd_object) 7517 elf_dyn_lib_class (abfd) = lib_class; 7518 } 7519 7520 /* Get the list of DT_NEEDED entries for a link. This is a hook for 7521 the linker ELF emulation code. */ 7522 7523 struct bfd_link_needed_list * 7524 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 7525 struct bfd_link_info *info) 7526 { 7527 if (! is_elf_hash_table (info->hash)) 7528 return NULL; 7529 return elf_hash_table (info)->needed; 7530 } 7531 7532 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 7533 hook for the linker ELF emulation code. */ 7534 7535 struct bfd_link_needed_list * 7536 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, 7537 struct bfd_link_info *info) 7538 { 7539 if (! is_elf_hash_table (info->hash)) 7540 return NULL; 7541 return elf_hash_table (info)->runpath; 7542 } 7543 7544 /* Get the name actually used for a dynamic object for a link. This 7545 is the SONAME entry if there is one. Otherwise, it is the string 7546 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 7547 7548 const char * 7549 bfd_elf_get_dt_soname (bfd *abfd) 7550 { 7551 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 7552 && bfd_get_format (abfd) == bfd_object) 7553 return elf_dt_name (abfd); 7554 return NULL; 7555 } 7556 7557 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for 7558 the ELF linker emulation code. */ 7559 7560 bfd_boolean 7561 bfd_elf_get_bfd_needed_list (bfd *abfd, 7562 struct bfd_link_needed_list **pneeded) 7563 { 7564 asection *s; 7565 bfd_byte *dynbuf = NULL; 7566 unsigned int elfsec; 7567 unsigned long shlink; 7568 bfd_byte *extdyn, *extdynend; 7569 size_t extdynsize; 7570 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 7571 7572 *pneeded = NULL; 7573 7574 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 7575 || bfd_get_format (abfd) != bfd_object) 7576 return TRUE; 7577 7578 s = bfd_get_section_by_name (abfd, ".dynamic"); 7579 if (s == NULL || s->size == 0) 7580 return TRUE; 7581 7582 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 7583 goto error_return; 7584 7585 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 7586 if (elfsec == SHN_BAD) 7587 goto error_return; 7588 7589 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 7590 7591 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 7592 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 7593 7594 extdyn = dynbuf; 7595 extdynend = extdyn + s->size; 7596 for (; extdyn < extdynend; extdyn += extdynsize) 7597 { 7598 Elf_Internal_Dyn dyn; 7599 7600 (*swap_dyn_in) (abfd, extdyn, &dyn); 7601 7602 if (dyn.d_tag == DT_NULL) 7603 break; 7604 7605 if (dyn.d_tag == DT_NEEDED) 7606 { 7607 const char *string; 7608 struct bfd_link_needed_list *l; 7609 unsigned int tagv = dyn.d_un.d_val; 7610 bfd_size_type amt; 7611 7612 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 7613 if (string == NULL) 7614 goto error_return; 7615 7616 amt = sizeof *l; 7617 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 7618 if (l == NULL) 7619 goto error_return; 7620 7621 l->by = abfd; 7622 l->name = string; 7623 l->next = *pneeded; 7624 *pneeded = l; 7625 } 7626 } 7627 7628 free (dynbuf); 7629 7630 return TRUE; 7631 7632 error_return: 7633 if (dynbuf != NULL) 7634 free (dynbuf); 7635 return FALSE; 7636 } 7637 7638 struct elf_symbuf_symbol 7639 { 7640 unsigned long st_name; /* Symbol name, index in string tbl */ 7641 unsigned char st_info; /* Type and binding attributes */ 7642 unsigned char st_other; /* Visibilty, and target specific */ 7643 }; 7644 7645 struct elf_symbuf_head 7646 { 7647 struct elf_symbuf_symbol *ssym; 7648 size_t count; 7649 unsigned int st_shndx; 7650 }; 7651 7652 struct elf_symbol 7653 { 7654 union 7655 { 7656 Elf_Internal_Sym *isym; 7657 struct elf_symbuf_symbol *ssym; 7658 } u; 7659 const char *name; 7660 }; 7661 7662 /* Sort references to symbols by ascending section number. */ 7663 7664 static int 7665 elf_sort_elf_symbol (const void *arg1, const void *arg2) 7666 { 7667 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; 7668 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; 7669 7670 return s1->st_shndx - s2->st_shndx; 7671 } 7672 7673 static int 7674 elf_sym_name_compare (const void *arg1, const void *arg2) 7675 { 7676 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; 7677 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; 7678 return strcmp (s1->name, s2->name); 7679 } 7680 7681 static struct elf_symbuf_head * 7682 elf_create_symbuf (size_t symcount, Elf_Internal_Sym *isymbuf) 7683 { 7684 Elf_Internal_Sym **ind, **indbufend, **indbuf; 7685 struct elf_symbuf_symbol *ssym; 7686 struct elf_symbuf_head *ssymbuf, *ssymhead; 7687 size_t i, shndx_count, total_size; 7688 7689 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); 7690 if (indbuf == NULL) 7691 return NULL; 7692 7693 for (ind = indbuf, i = 0; i < symcount; i++) 7694 if (isymbuf[i].st_shndx != SHN_UNDEF) 7695 *ind++ = &isymbuf[i]; 7696 indbufend = ind; 7697 7698 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), 7699 elf_sort_elf_symbol); 7700 7701 shndx_count = 0; 7702 if (indbufend > indbuf) 7703 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) 7704 if (ind[0]->st_shndx != ind[1]->st_shndx) 7705 shndx_count++; 7706 7707 total_size = ((shndx_count + 1) * sizeof (*ssymbuf) 7708 + (indbufend - indbuf) * sizeof (*ssym)); 7709 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); 7710 if (ssymbuf == NULL) 7711 { 7712 free (indbuf); 7713 return NULL; 7714 } 7715 7716 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); 7717 ssymbuf->ssym = NULL; 7718 ssymbuf->count = shndx_count; 7719 ssymbuf->st_shndx = 0; 7720 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) 7721 { 7722 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) 7723 { 7724 ssymhead++; 7725 ssymhead->ssym = ssym; 7726 ssymhead->count = 0; 7727 ssymhead->st_shndx = (*ind)->st_shndx; 7728 } 7729 ssym->st_name = (*ind)->st_name; 7730 ssym->st_info = (*ind)->st_info; 7731 ssym->st_other = (*ind)->st_other; 7732 ssymhead->count++; 7733 } 7734 BFD_ASSERT ((size_t) (ssymhead - ssymbuf) == shndx_count 7735 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) 7736 == total_size)); 7737 7738 free (indbuf); 7739 return ssymbuf; 7740 } 7741 7742 /* Check if 2 sections define the same set of local and global 7743 symbols. */ 7744 7745 static bfd_boolean 7746 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, 7747 struct bfd_link_info *info) 7748 { 7749 bfd *bfd1, *bfd2; 7750 const struct elf_backend_data *bed1, *bed2; 7751 Elf_Internal_Shdr *hdr1, *hdr2; 7752 size_t symcount1, symcount2; 7753 Elf_Internal_Sym *isymbuf1, *isymbuf2; 7754 struct elf_symbuf_head *ssymbuf1, *ssymbuf2; 7755 Elf_Internal_Sym *isym, *isymend; 7756 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; 7757 size_t count1, count2, i; 7758 unsigned int shndx1, shndx2; 7759 bfd_boolean result; 7760 7761 bfd1 = sec1->owner; 7762 bfd2 = sec2->owner; 7763 7764 /* Both sections have to be in ELF. */ 7765 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour 7766 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) 7767 return FALSE; 7768 7769 if (elf_section_type (sec1) != elf_section_type (sec2)) 7770 return FALSE; 7771 7772 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); 7773 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); 7774 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) 7775 return FALSE; 7776 7777 bed1 = get_elf_backend_data (bfd1); 7778 bed2 = get_elf_backend_data (bfd2); 7779 hdr1 = &elf_tdata (bfd1)->symtab_hdr; 7780 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; 7781 hdr2 = &elf_tdata (bfd2)->symtab_hdr; 7782 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; 7783 7784 if (symcount1 == 0 || symcount2 == 0) 7785 return FALSE; 7786 7787 result = FALSE; 7788 isymbuf1 = NULL; 7789 isymbuf2 = NULL; 7790 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; 7791 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; 7792 7793 if (ssymbuf1 == NULL) 7794 { 7795 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, 7796 NULL, NULL, NULL); 7797 if (isymbuf1 == NULL) 7798 goto done; 7799 7800 if (!info->reduce_memory_overheads) 7801 elf_tdata (bfd1)->symbuf = ssymbuf1 7802 = elf_create_symbuf (symcount1, isymbuf1); 7803 } 7804 7805 if (ssymbuf1 == NULL || ssymbuf2 == NULL) 7806 { 7807 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, 7808 NULL, NULL, NULL); 7809 if (isymbuf2 == NULL) 7810 goto done; 7811 7812 if (ssymbuf1 != NULL && !info->reduce_memory_overheads) 7813 elf_tdata (bfd2)->symbuf = ssymbuf2 7814 = elf_create_symbuf (symcount2, isymbuf2); 7815 } 7816 7817 if (ssymbuf1 != NULL && ssymbuf2 != NULL) 7818 { 7819 /* Optimized faster version. */ 7820 size_t lo, hi, mid; 7821 struct elf_symbol *symp; 7822 struct elf_symbuf_symbol *ssym, *ssymend; 7823 7824 lo = 0; 7825 hi = ssymbuf1->count; 7826 ssymbuf1++; 7827 count1 = 0; 7828 while (lo < hi) 7829 { 7830 mid = (lo + hi) / 2; 7831 if (shndx1 < ssymbuf1[mid].st_shndx) 7832 hi = mid; 7833 else if (shndx1 > ssymbuf1[mid].st_shndx) 7834 lo = mid + 1; 7835 else 7836 { 7837 count1 = ssymbuf1[mid].count; 7838 ssymbuf1 += mid; 7839 break; 7840 } 7841 } 7842 7843 lo = 0; 7844 hi = ssymbuf2->count; 7845 ssymbuf2++; 7846 count2 = 0; 7847 while (lo < hi) 7848 { 7849 mid = (lo + hi) / 2; 7850 if (shndx2 < ssymbuf2[mid].st_shndx) 7851 hi = mid; 7852 else if (shndx2 > ssymbuf2[mid].st_shndx) 7853 lo = mid + 1; 7854 else 7855 { 7856 count2 = ssymbuf2[mid].count; 7857 ssymbuf2 += mid; 7858 break; 7859 } 7860 } 7861 7862 if (count1 == 0 || count2 == 0 || count1 != count2) 7863 goto done; 7864 7865 symtable1 7866 = (struct elf_symbol *) bfd_malloc (count1 * sizeof (*symtable1)); 7867 symtable2 7868 = (struct elf_symbol *) bfd_malloc (count2 * sizeof (*symtable2)); 7869 if (symtable1 == NULL || symtable2 == NULL) 7870 goto done; 7871 7872 symp = symtable1; 7873 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; 7874 ssym < ssymend; ssym++, symp++) 7875 { 7876 symp->u.ssym = ssym; 7877 symp->name = bfd_elf_string_from_elf_section (bfd1, 7878 hdr1->sh_link, 7879 ssym->st_name); 7880 } 7881 7882 symp = symtable2; 7883 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; 7884 ssym < ssymend; ssym++, symp++) 7885 { 7886 symp->u.ssym = ssym; 7887 symp->name = bfd_elf_string_from_elf_section (bfd2, 7888 hdr2->sh_link, 7889 ssym->st_name); 7890 } 7891 7892 /* Sort symbol by name. */ 7893 qsort (symtable1, count1, sizeof (struct elf_symbol), 7894 elf_sym_name_compare); 7895 qsort (symtable2, count1, sizeof (struct elf_symbol), 7896 elf_sym_name_compare); 7897 7898 for (i = 0; i < count1; i++) 7899 /* Two symbols must have the same binding, type and name. */ 7900 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info 7901 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other 7902 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 7903 goto done; 7904 7905 result = TRUE; 7906 goto done; 7907 } 7908 7909 symtable1 = (struct elf_symbol *) 7910 bfd_malloc (symcount1 * sizeof (struct elf_symbol)); 7911 symtable2 = (struct elf_symbol *) 7912 bfd_malloc (symcount2 * sizeof (struct elf_symbol)); 7913 if (symtable1 == NULL || symtable2 == NULL) 7914 goto done; 7915 7916 /* Count definitions in the section. */ 7917 count1 = 0; 7918 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) 7919 if (isym->st_shndx == shndx1) 7920 symtable1[count1++].u.isym = isym; 7921 7922 count2 = 0; 7923 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) 7924 if (isym->st_shndx == shndx2) 7925 symtable2[count2++].u.isym = isym; 7926 7927 if (count1 == 0 || count2 == 0 || count1 != count2) 7928 goto done; 7929 7930 for (i = 0; i < count1; i++) 7931 symtable1[i].name 7932 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, 7933 symtable1[i].u.isym->st_name); 7934 7935 for (i = 0; i < count2; i++) 7936 symtable2[i].name 7937 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, 7938 symtable2[i].u.isym->st_name); 7939 7940 /* Sort symbol by name. */ 7941 qsort (symtable1, count1, sizeof (struct elf_symbol), 7942 elf_sym_name_compare); 7943 qsort (symtable2, count1, sizeof (struct elf_symbol), 7944 elf_sym_name_compare); 7945 7946 for (i = 0; i < count1; i++) 7947 /* Two symbols must have the same binding, type and name. */ 7948 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info 7949 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other 7950 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 7951 goto done; 7952 7953 result = TRUE; 7954 7955 done: 7956 if (symtable1) 7957 free (symtable1); 7958 if (symtable2) 7959 free (symtable2); 7960 if (isymbuf1) 7961 free (isymbuf1); 7962 if (isymbuf2) 7963 free (isymbuf2); 7964 7965 return result; 7966 } 7967 7968 /* Return TRUE if 2 section types are compatible. */ 7969 7970 bfd_boolean 7971 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, 7972 bfd *bbfd, const asection *bsec) 7973 { 7974 if (asec == NULL 7975 || bsec == NULL 7976 || abfd->xvec->flavour != bfd_target_elf_flavour 7977 || bbfd->xvec->flavour != bfd_target_elf_flavour) 7978 return TRUE; 7979 7980 return elf_section_type (asec) == elf_section_type (bsec); 7981 } 7982 7983 /* Final phase of ELF linker. */ 7984 7985 /* A structure we use to avoid passing large numbers of arguments. */ 7986 7987 struct elf_final_link_info 7988 { 7989 /* General link information. */ 7990 struct bfd_link_info *info; 7991 /* Output BFD. */ 7992 bfd *output_bfd; 7993 /* Symbol string table. */ 7994 struct elf_strtab_hash *symstrtab; 7995 /* .hash section. */ 7996 asection *hash_sec; 7997 /* symbol version section (.gnu.version). */ 7998 asection *symver_sec; 7999 /* Buffer large enough to hold contents of any section. */ 8000 bfd_byte *contents; 8001 /* Buffer large enough to hold external relocs of any section. */ 8002 void *external_relocs; 8003 /* Buffer large enough to hold internal relocs of any section. */ 8004 Elf_Internal_Rela *internal_relocs; 8005 /* Buffer large enough to hold external local symbols of any input 8006 BFD. */ 8007 bfd_byte *external_syms; 8008 /* And a buffer for symbol section indices. */ 8009 Elf_External_Sym_Shndx *locsym_shndx; 8010 /* Buffer large enough to hold internal local symbols of any input 8011 BFD. */ 8012 Elf_Internal_Sym *internal_syms; 8013 /* Array large enough to hold a symbol index for each local symbol 8014 of any input BFD. */ 8015 long *indices; 8016 /* Array large enough to hold a section pointer for each local 8017 symbol of any input BFD. */ 8018 asection **sections; 8019 /* Buffer for SHT_SYMTAB_SHNDX section. */ 8020 Elf_External_Sym_Shndx *symshndxbuf; 8021 /* Number of STT_FILE syms seen. */ 8022 size_t filesym_count; 8023 }; 8024 8025 /* This struct is used to pass information to elf_link_output_extsym. */ 8026 8027 struct elf_outext_info 8028 { 8029 bfd_boolean failed; 8030 bfd_boolean localsyms; 8031 bfd_boolean file_sym_done; 8032 struct elf_final_link_info *flinfo; 8033 }; 8034 8035 8036 /* Support for evaluating a complex relocation. 8037 8038 Complex relocations are generalized, self-describing relocations. The 8039 implementation of them consists of two parts: complex symbols, and the 8040 relocations themselves. 8041 8042 The relocations are use a reserved elf-wide relocation type code (R_RELC 8043 external / BFD_RELOC_RELC internal) and an encoding of relocation field 8044 information (start bit, end bit, word width, etc) into the addend. This 8045 information is extracted from CGEN-generated operand tables within gas. 8046 8047 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC 8048 internal) representing prefix-notation expressions, including but not 8049 limited to those sorts of expressions normally encoded as addends in the 8050 addend field. The symbol mangling format is: 8051 8052 <node> := <literal> 8053 | <unary-operator> ':' <node> 8054 | <binary-operator> ':' <node> ':' <node> 8055 ; 8056 8057 <literal> := 's' <digits=N> ':' <N character symbol name> 8058 | 'S' <digits=N> ':' <N character section name> 8059 | '#' <hexdigits> 8060 ; 8061 8062 <binary-operator> := as in C 8063 <unary-operator> := as in C, plus "0-" for unambiguous negation. */ 8064 8065 static void 8066 set_symbol_value (bfd *bfd_with_globals, 8067 Elf_Internal_Sym *isymbuf, 8068 size_t locsymcount, 8069 size_t symidx, 8070 bfd_vma val) 8071 { 8072 struct elf_link_hash_entry **sym_hashes; 8073 struct elf_link_hash_entry *h; 8074 size_t extsymoff = locsymcount; 8075 8076 if (symidx < locsymcount) 8077 { 8078 Elf_Internal_Sym *sym; 8079 8080 sym = isymbuf + symidx; 8081 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) 8082 { 8083 /* It is a local symbol: move it to the 8084 "absolute" section and give it a value. */ 8085 sym->st_shndx = SHN_ABS; 8086 sym->st_value = val; 8087 return; 8088 } 8089 BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); 8090 extsymoff = 0; 8091 } 8092 8093 /* It is a global symbol: set its link type 8094 to "defined" and give it a value. */ 8095 8096 sym_hashes = elf_sym_hashes (bfd_with_globals); 8097 h = sym_hashes [symidx - extsymoff]; 8098 while (h->root.type == bfd_link_hash_indirect 8099 || h->root.type == bfd_link_hash_warning) 8100 h = (struct elf_link_hash_entry *) h->root.u.i.link; 8101 h->root.type = bfd_link_hash_defined; 8102 h->root.u.def.value = val; 8103 h->root.u.def.section = bfd_abs_section_ptr; 8104 } 8105 8106 static bfd_boolean 8107 resolve_symbol (const char *name, 8108 bfd *input_bfd, 8109 struct elf_final_link_info *flinfo, 8110 bfd_vma *result, 8111 Elf_Internal_Sym *isymbuf, 8112 size_t locsymcount) 8113 { 8114 Elf_Internal_Sym *sym; 8115 struct bfd_link_hash_entry *global_entry; 8116 const char *candidate = NULL; 8117 Elf_Internal_Shdr *symtab_hdr; 8118 size_t i; 8119 8120 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 8121 8122 for (i = 0; i < locsymcount; ++ i) 8123 { 8124 sym = isymbuf + i; 8125 8126 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) 8127 continue; 8128 8129 candidate = bfd_elf_string_from_elf_section (input_bfd, 8130 symtab_hdr->sh_link, 8131 sym->st_name); 8132 #ifdef DEBUG 8133 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", 8134 name, candidate, (unsigned long) sym->st_value); 8135 #endif 8136 if (candidate && strcmp (candidate, name) == 0) 8137 { 8138 asection *sec = flinfo->sections [i]; 8139 8140 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); 8141 *result += sec->output_offset + sec->output_section->vma; 8142 #ifdef DEBUG 8143 printf ("Found symbol with value %8.8lx\n", 8144 (unsigned long) *result); 8145 #endif 8146 return TRUE; 8147 } 8148 } 8149 8150 /* Hmm, haven't found it yet. perhaps it is a global. */ 8151 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, 8152 FALSE, FALSE, TRUE); 8153 if (!global_entry) 8154 return FALSE; 8155 8156 if (global_entry->type == bfd_link_hash_defined 8157 || global_entry->type == bfd_link_hash_defweak) 8158 { 8159 *result = (global_entry->u.def.value 8160 + global_entry->u.def.section->output_section->vma 8161 + global_entry->u.def.section->output_offset); 8162 #ifdef DEBUG 8163 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", 8164 global_entry->root.string, (unsigned long) *result); 8165 #endif 8166 return TRUE; 8167 } 8168 8169 return FALSE; 8170 } 8171 8172 /* Looks up NAME in SECTIONS. If found sets RESULT to NAME's address (in 8173 bytes) and returns TRUE, otherwise returns FALSE. Accepts pseudo-section 8174 names like "foo.end" which is the end address of section "foo". */ 8175 8176 static bfd_boolean 8177 resolve_section (const char *name, 8178 asection *sections, 8179 bfd_vma *result, 8180 bfd * abfd) 8181 { 8182 asection *curr; 8183 unsigned int len; 8184 8185 for (curr = sections; curr; curr = curr->next) 8186 if (strcmp (curr->name, name) == 0) 8187 { 8188 *result = curr->vma; 8189 return TRUE; 8190 } 8191 8192 /* Hmm. still haven't found it. try pseudo-section names. */ 8193 /* FIXME: This could be coded more efficiently... */ 8194 for (curr = sections; curr; curr = curr->next) 8195 { 8196 len = strlen (curr->name); 8197 if (len > strlen (name)) 8198 continue; 8199 8200 if (strncmp (curr->name, name, len) == 0) 8201 { 8202 if (strncmp (".end", name + len, 4) == 0) 8203 { 8204 *result = curr->vma + curr->size / bfd_octets_per_byte (abfd); 8205 return TRUE; 8206 } 8207 8208 /* Insert more pseudo-section names here, if you like. */ 8209 } 8210 } 8211 8212 return FALSE; 8213 } 8214 8215 static void 8216 undefined_reference (const char *reftype, const char *name) 8217 { 8218 /* xgettext:c-format */ 8219 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), 8220 reftype, name); 8221 } 8222 8223 static bfd_boolean 8224 eval_symbol (bfd_vma *result, 8225 const char **symp, 8226 bfd *input_bfd, 8227 struct elf_final_link_info *flinfo, 8228 bfd_vma dot, 8229 Elf_Internal_Sym *isymbuf, 8230 size_t locsymcount, 8231 int signed_p) 8232 { 8233 size_t len; 8234 size_t symlen; 8235 bfd_vma a; 8236 bfd_vma b; 8237 char symbuf[4096]; 8238 const char *sym = *symp; 8239 const char *symend; 8240 bfd_boolean symbol_is_section = FALSE; 8241 8242 len = strlen (sym); 8243 symend = sym + len; 8244 8245 if (len < 1 || len > sizeof (symbuf)) 8246 { 8247 bfd_set_error (bfd_error_invalid_operation); 8248 return FALSE; 8249 } 8250 8251 switch (* sym) 8252 { 8253 case '.': 8254 *result = dot; 8255 *symp = sym + 1; 8256 return TRUE; 8257 8258 case '#': 8259 ++sym; 8260 *result = strtoul (sym, (char **) symp, 16); 8261 return TRUE; 8262 8263 case 'S': 8264 symbol_is_section = TRUE; 8265 /* Fall through. */ 8266 case 's': 8267 ++sym; 8268 symlen = strtol (sym, (char **) symp, 10); 8269 sym = *symp + 1; /* Skip the trailing ':'. */ 8270 8271 if (symend < sym || symlen + 1 > sizeof (symbuf)) 8272 { 8273 bfd_set_error (bfd_error_invalid_operation); 8274 return FALSE; 8275 } 8276 8277 memcpy (symbuf, sym, symlen); 8278 symbuf[symlen] = '\0'; 8279 *symp = sym + symlen; 8280 8281 /* Is it always possible, with complex symbols, that gas "mis-guessed" 8282 the symbol as a section, or vice-versa. so we're pretty liberal in our 8283 interpretation here; section means "try section first", not "must be a 8284 section", and likewise with symbol. */ 8285 8286 if (symbol_is_section) 8287 { 8288 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result, input_bfd) 8289 && !resolve_symbol (symbuf, input_bfd, flinfo, result, 8290 isymbuf, locsymcount)) 8291 { 8292 undefined_reference ("section", symbuf); 8293 return FALSE; 8294 } 8295 } 8296 else 8297 { 8298 if (!resolve_symbol (symbuf, input_bfd, flinfo, result, 8299 isymbuf, locsymcount) 8300 && !resolve_section (symbuf, flinfo->output_bfd->sections, 8301 result, input_bfd)) 8302 { 8303 undefined_reference ("symbol", symbuf); 8304 return FALSE; 8305 } 8306 } 8307 8308 return TRUE; 8309 8310 /* All that remains are operators. */ 8311 8312 #define UNARY_OP(op) \ 8313 if (strncmp (sym, #op, strlen (#op)) == 0) \ 8314 { \ 8315 sym += strlen (#op); \ 8316 if (*sym == ':') \ 8317 ++sym; \ 8318 *symp = sym; \ 8319 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 8320 isymbuf, locsymcount, signed_p)) \ 8321 return FALSE; \ 8322 if (signed_p) \ 8323 *result = op ((bfd_signed_vma) a); \ 8324 else \ 8325 *result = op a; \ 8326 return TRUE; \ 8327 } 8328 8329 #define BINARY_OP(op) \ 8330 if (strncmp (sym, #op, strlen (#op)) == 0) \ 8331 { \ 8332 sym += strlen (#op); \ 8333 if (*sym == ':') \ 8334 ++sym; \ 8335 *symp = sym; \ 8336 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 8337 isymbuf, locsymcount, signed_p)) \ 8338 return FALSE; \ 8339 ++*symp; \ 8340 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ 8341 isymbuf, locsymcount, signed_p)) \ 8342 return FALSE; \ 8343 if (signed_p) \ 8344 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ 8345 else \ 8346 *result = a op b; \ 8347 return TRUE; \ 8348 } 8349 8350 default: 8351 UNARY_OP (0-); 8352 BINARY_OP (<<); 8353 BINARY_OP (>>); 8354 BINARY_OP (==); 8355 BINARY_OP (!=); 8356 BINARY_OP (<=); 8357 BINARY_OP (>=); 8358 BINARY_OP (&&); 8359 BINARY_OP (||); 8360 UNARY_OP (~); 8361 UNARY_OP (!); 8362 BINARY_OP (*); 8363 BINARY_OP (/); 8364 BINARY_OP (%); 8365 BINARY_OP (^); 8366 BINARY_OP (|); 8367 BINARY_OP (&); 8368 BINARY_OP (+); 8369 BINARY_OP (-); 8370 BINARY_OP (<); 8371 BINARY_OP (>); 8372 #undef UNARY_OP 8373 #undef BINARY_OP 8374 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); 8375 bfd_set_error (bfd_error_invalid_operation); 8376 return FALSE; 8377 } 8378 } 8379 8380 static void 8381 put_value (bfd_vma size, 8382 unsigned long chunksz, 8383 bfd *input_bfd, 8384 bfd_vma x, 8385 bfd_byte *location) 8386 { 8387 location += (size - chunksz); 8388 8389 for (; size; size -= chunksz, location -= chunksz) 8390 { 8391 switch (chunksz) 8392 { 8393 case 1: 8394 bfd_put_8 (input_bfd, x, location); 8395 x >>= 8; 8396 break; 8397 case 2: 8398 bfd_put_16 (input_bfd, x, location); 8399 x >>= 16; 8400 break; 8401 case 4: 8402 bfd_put_32 (input_bfd, x, location); 8403 /* Computed this way because x >>= 32 is undefined if x is a 32-bit value. */ 8404 x >>= 16; 8405 x >>= 16; 8406 break; 8407 #ifdef BFD64 8408 case 8: 8409 bfd_put_64 (input_bfd, x, location); 8410 /* Computed this way because x >>= 64 is undefined if x is a 64-bit value. */ 8411 x >>= 32; 8412 x >>= 32; 8413 break; 8414 #endif 8415 default: 8416 abort (); 8417 break; 8418 } 8419 } 8420 } 8421 8422 static bfd_vma 8423 get_value (bfd_vma size, 8424 unsigned long chunksz, 8425 bfd *input_bfd, 8426 bfd_byte *location) 8427 { 8428 int shift; 8429 bfd_vma x = 0; 8430 8431 /* Sanity checks. */ 8432 BFD_ASSERT (chunksz <= sizeof (x) 8433 && size >= chunksz 8434 && chunksz != 0 8435 && (size % chunksz) == 0 8436 && input_bfd != NULL 8437 && location != NULL); 8438 8439 if (chunksz == sizeof (x)) 8440 { 8441 BFD_ASSERT (size == chunksz); 8442 8443 /* Make sure that we do not perform an undefined shift operation. 8444 We know that size == chunksz so there will only be one iteration 8445 of the loop below. */ 8446 shift = 0; 8447 } 8448 else 8449 shift = 8 * chunksz; 8450 8451 for (; size; size -= chunksz, location += chunksz) 8452 { 8453 switch (chunksz) 8454 { 8455 case 1: 8456 x = (x << shift) | bfd_get_8 (input_bfd, location); 8457 break; 8458 case 2: 8459 x = (x << shift) | bfd_get_16 (input_bfd, location); 8460 break; 8461 case 4: 8462 x = (x << shift) | bfd_get_32 (input_bfd, location); 8463 break; 8464 #ifdef BFD64 8465 case 8: 8466 x = (x << shift) | bfd_get_64 (input_bfd, location); 8467 break; 8468 #endif 8469 default: 8470 abort (); 8471 } 8472 } 8473 return x; 8474 } 8475 8476 static void 8477 decode_complex_addend (unsigned long *start, /* in bits */ 8478 unsigned long *oplen, /* in bits */ 8479 unsigned long *len, /* in bits */ 8480 unsigned long *wordsz, /* in bytes */ 8481 unsigned long *chunksz, /* in bytes */ 8482 unsigned long *lsb0_p, 8483 unsigned long *signed_p, 8484 unsigned long *trunc_p, 8485 unsigned long encoded) 8486 { 8487 * start = encoded & 0x3F; 8488 * len = (encoded >> 6) & 0x3F; 8489 * oplen = (encoded >> 12) & 0x3F; 8490 * wordsz = (encoded >> 18) & 0xF; 8491 * chunksz = (encoded >> 22) & 0xF; 8492 * lsb0_p = (encoded >> 27) & 1; 8493 * signed_p = (encoded >> 28) & 1; 8494 * trunc_p = (encoded >> 29) & 1; 8495 } 8496 8497 bfd_reloc_status_type 8498 bfd_elf_perform_complex_relocation (bfd *input_bfd, 8499 asection *input_section ATTRIBUTE_UNUSED, 8500 bfd_byte *contents, 8501 Elf_Internal_Rela *rel, 8502 bfd_vma relocation) 8503 { 8504 bfd_vma shift, x, mask; 8505 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; 8506 bfd_reloc_status_type r; 8507 8508 /* Perform this reloc, since it is complex. 8509 (this is not to say that it necessarily refers to a complex 8510 symbol; merely that it is a self-describing CGEN based reloc. 8511 i.e. the addend has the complete reloc information (bit start, end, 8512 word size, etc) encoded within it.). */ 8513 8514 decode_complex_addend (&start, &oplen, &len, &wordsz, 8515 &chunksz, &lsb0_p, &signed_p, 8516 &trunc_p, rel->r_addend); 8517 8518 mask = (((1L << (len - 1)) - 1) << 1) | 1; 8519 8520 if (lsb0_p) 8521 shift = (start + 1) - len; 8522 else 8523 shift = (8 * wordsz) - (start + len); 8524 8525 x = get_value (wordsz, chunksz, input_bfd, 8526 contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); 8527 8528 #ifdef DEBUG 8529 printf ("Doing complex reloc: " 8530 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " 8531 "chunksz %ld, start %ld, len %ld, oplen %ld\n" 8532 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", 8533 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, 8534 oplen, (unsigned long) x, (unsigned long) mask, 8535 (unsigned long) relocation); 8536 #endif 8537 8538 r = bfd_reloc_ok; 8539 if (! trunc_p) 8540 /* Now do an overflow check. */ 8541 r = bfd_check_overflow ((signed_p 8542 ? complain_overflow_signed 8543 : complain_overflow_unsigned), 8544 len, 0, (8 * wordsz), 8545 relocation); 8546 8547 /* Do the deed. */ 8548 x = (x & ~(mask << shift)) | ((relocation & mask) << shift); 8549 8550 #ifdef DEBUG 8551 printf (" relocation: %8.8lx\n" 8552 " shifted mask: %8.8lx\n" 8553 " shifted/masked reloc: %8.8lx\n" 8554 " result: %8.8lx\n", 8555 (unsigned long) relocation, (unsigned long) (mask << shift), 8556 (unsigned long) ((relocation & mask) << shift), (unsigned long) x); 8557 #endif 8558 put_value (wordsz, chunksz, input_bfd, x, 8559 contents + rel->r_offset * bfd_octets_per_byte (input_bfd)); 8560 return r; 8561 } 8562 8563 /* Functions to read r_offset from external (target order) reloc 8564 entry. Faster than bfd_getl32 et al, because we let the compiler 8565 know the value is aligned. */ 8566 8567 static bfd_vma 8568 ext32l_r_offset (const void *p) 8569 { 8570 union aligned32 8571 { 8572 uint32_t v; 8573 unsigned char c[4]; 8574 }; 8575 const union aligned32 *a 8576 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 8577 8578 uint32_t aval = ( (uint32_t) a->c[0] 8579 | (uint32_t) a->c[1] << 8 8580 | (uint32_t) a->c[2] << 16 8581 | (uint32_t) a->c[3] << 24); 8582 return aval; 8583 } 8584 8585 static bfd_vma 8586 ext32b_r_offset (const void *p) 8587 { 8588 union aligned32 8589 { 8590 uint32_t v; 8591 unsigned char c[4]; 8592 }; 8593 const union aligned32 *a 8594 = (const union aligned32 *) &((const Elf32_External_Rel *) p)->r_offset; 8595 8596 uint32_t aval = ( (uint32_t) a->c[0] << 24 8597 | (uint32_t) a->c[1] << 16 8598 | (uint32_t) a->c[2] << 8 8599 | (uint32_t) a->c[3]); 8600 return aval; 8601 } 8602 8603 #ifdef BFD_HOST_64_BIT 8604 static bfd_vma 8605 ext64l_r_offset (const void *p) 8606 { 8607 union aligned64 8608 { 8609 uint64_t v; 8610 unsigned char c[8]; 8611 }; 8612 const union aligned64 *a 8613 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 8614 8615 uint64_t aval = ( (uint64_t) a->c[0] 8616 | (uint64_t) a->c[1] << 8 8617 | (uint64_t) a->c[2] << 16 8618 | (uint64_t) a->c[3] << 24 8619 | (uint64_t) a->c[4] << 32 8620 | (uint64_t) a->c[5] << 40 8621 | (uint64_t) a->c[6] << 48 8622 | (uint64_t) a->c[7] << 56); 8623 return aval; 8624 } 8625 8626 static bfd_vma 8627 ext64b_r_offset (const void *p) 8628 { 8629 union aligned64 8630 { 8631 uint64_t v; 8632 unsigned char c[8]; 8633 }; 8634 const union aligned64 *a 8635 = (const union aligned64 *) &((const Elf64_External_Rel *) p)->r_offset; 8636 8637 uint64_t aval = ( (uint64_t) a->c[0] << 56 8638 | (uint64_t) a->c[1] << 48 8639 | (uint64_t) a->c[2] << 40 8640 | (uint64_t) a->c[3] << 32 8641 | (uint64_t) a->c[4] << 24 8642 | (uint64_t) a->c[5] << 16 8643 | (uint64_t) a->c[6] << 8 8644 | (uint64_t) a->c[7]); 8645 return aval; 8646 } 8647 #endif 8648 8649 /* When performing a relocatable link, the input relocations are 8650 preserved. But, if they reference global symbols, the indices 8651 referenced must be updated. Update all the relocations found in 8652 RELDATA. */ 8653 8654 static bfd_boolean 8655 elf_link_adjust_relocs (bfd *abfd, 8656 asection *sec, 8657 struct bfd_elf_section_reloc_data *reldata, 8658 bfd_boolean sort, 8659 struct bfd_link_info *info) 8660 { 8661 unsigned int i; 8662 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8663 bfd_byte *erela; 8664 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 8665 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 8666 bfd_vma r_type_mask; 8667 int r_sym_shift; 8668 unsigned int count = reldata->count; 8669 struct elf_link_hash_entry **rel_hash = reldata->hashes; 8670 8671 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) 8672 { 8673 swap_in = bed->s->swap_reloc_in; 8674 swap_out = bed->s->swap_reloc_out; 8675 } 8676 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) 8677 { 8678 swap_in = bed->s->swap_reloca_in; 8679 swap_out = bed->s->swap_reloca_out; 8680 } 8681 else 8682 abort (); 8683 8684 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) 8685 abort (); 8686 8687 if (bed->s->arch_size == 32) 8688 { 8689 r_type_mask = 0xff; 8690 r_sym_shift = 8; 8691 } 8692 else 8693 { 8694 r_type_mask = 0xffffffff; 8695 r_sym_shift = 32; 8696 } 8697 8698 erela = reldata->hdr->contents; 8699 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) 8700 { 8701 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; 8702 unsigned int j; 8703 8704 if (*rel_hash == NULL) 8705 continue; 8706 8707 if ((*rel_hash)->indx == -2 8708 && info->gc_sections 8709 && ! info->gc_keep_exported) 8710 { 8711 /* PR 21524: Let the user know if a symbol was removed by garbage collection. */ 8712 _bfd_error_handler (_("%B:%A: error: relocation references symbol %s which was removed by garbage collection."), 8713 abfd, sec, 8714 (*rel_hash)->root.root.string); 8715 _bfd_error_handler (_("%B:%A: error: try relinking with --gc-keep-exported enabled."), 8716 abfd, sec); 8717 bfd_set_error (bfd_error_invalid_operation); 8718 return FALSE; 8719 } 8720 BFD_ASSERT ((*rel_hash)->indx >= 0); 8721 8722 (*swap_in) (abfd, erela, irela); 8723 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) 8724 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift 8725 | (irela[j].r_info & r_type_mask)); 8726 (*swap_out) (abfd, irela, erela); 8727 } 8728 8729 if (bed->elf_backend_update_relocs) 8730 (*bed->elf_backend_update_relocs) (sec, reldata); 8731 8732 if (sort && count != 0) 8733 { 8734 bfd_vma (*ext_r_off) (const void *); 8735 bfd_vma r_off; 8736 size_t elt_size; 8737 bfd_byte *base, *end, *p, *loc; 8738 bfd_byte *buf = NULL; 8739 8740 if (bed->s->arch_size == 32) 8741 { 8742 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 8743 ext_r_off = ext32l_r_offset; 8744 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 8745 ext_r_off = ext32b_r_offset; 8746 else 8747 abort (); 8748 } 8749 else 8750 { 8751 #ifdef BFD_HOST_64_BIT 8752 if (abfd->xvec->header_byteorder == BFD_ENDIAN_LITTLE) 8753 ext_r_off = ext64l_r_offset; 8754 else if (abfd->xvec->header_byteorder == BFD_ENDIAN_BIG) 8755 ext_r_off = ext64b_r_offset; 8756 else 8757 #endif 8758 abort (); 8759 } 8760 8761 /* Must use a stable sort here. A modified insertion sort, 8762 since the relocs are mostly sorted already. */ 8763 elt_size = reldata->hdr->sh_entsize; 8764 base = reldata->hdr->contents; 8765 end = base + count * elt_size; 8766 if (elt_size > sizeof (Elf64_External_Rela)) 8767 abort (); 8768 8769 /* Ensure the first element is lowest. This acts as a sentinel, 8770 speeding the main loop below. */ 8771 r_off = (*ext_r_off) (base); 8772 for (p = loc = base; (p += elt_size) < end; ) 8773 { 8774 bfd_vma r_off2 = (*ext_r_off) (p); 8775 if (r_off > r_off2) 8776 { 8777 r_off = r_off2; 8778 loc = p; 8779 } 8780 } 8781 if (loc != base) 8782 { 8783 /* Don't just swap *base and *loc as that changes the order 8784 of the original base[0] and base[1] if they happen to 8785 have the same r_offset. */ 8786 bfd_byte onebuf[sizeof (Elf64_External_Rela)]; 8787 memcpy (onebuf, loc, elt_size); 8788 memmove (base + elt_size, base, loc - base); 8789 memcpy (base, onebuf, elt_size); 8790 } 8791 8792 for (p = base + elt_size; (p += elt_size) < end; ) 8793 { 8794 /* base to p is sorted, *p is next to insert. */ 8795 r_off = (*ext_r_off) (p); 8796 /* Search the sorted region for location to insert. */ 8797 loc = p - elt_size; 8798 while (r_off < (*ext_r_off) (loc)) 8799 loc -= elt_size; 8800 loc += elt_size; 8801 if (loc != p) 8802 { 8803 /* Chances are there is a run of relocs to insert here, 8804 from one of more input files. Files are not always 8805 linked in order due to the way elf_link_input_bfd is 8806 called. See pr17666. */ 8807 size_t sortlen = p - loc; 8808 bfd_vma r_off2 = (*ext_r_off) (loc); 8809 size_t runlen = elt_size; 8810 size_t buf_size = 96 * 1024; 8811 while (p + runlen < end 8812 && (sortlen <= buf_size 8813 || runlen + elt_size <= buf_size) 8814 && r_off2 > (*ext_r_off) (p + runlen)) 8815 runlen += elt_size; 8816 if (buf == NULL) 8817 { 8818 buf = bfd_malloc (buf_size); 8819 if (buf == NULL) 8820 return FALSE; 8821 } 8822 if (runlen < sortlen) 8823 { 8824 memcpy (buf, p, runlen); 8825 memmove (loc + runlen, loc, sortlen); 8826 memcpy (loc, buf, runlen); 8827 } 8828 else 8829 { 8830 memcpy (buf, loc, sortlen); 8831 memmove (loc, p, runlen); 8832 memcpy (loc + runlen, buf, sortlen); 8833 } 8834 p += runlen - elt_size; 8835 } 8836 } 8837 /* Hashes are no longer valid. */ 8838 free (reldata->hashes); 8839 reldata->hashes = NULL; 8840 free (buf); 8841 } 8842 return TRUE; 8843 } 8844 8845 struct elf_link_sort_rela 8846 { 8847 union { 8848 bfd_vma offset; 8849 bfd_vma sym_mask; 8850 } u; 8851 enum elf_reloc_type_class type; 8852 /* We use this as an array of size int_rels_per_ext_rel. */ 8853 Elf_Internal_Rela rela[1]; 8854 }; 8855 8856 static int 8857 elf_link_sort_cmp1 (const void *A, const void *B) 8858 { 8859 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8860 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8861 int relativea, relativeb; 8862 8863 relativea = a->type == reloc_class_relative; 8864 relativeb = b->type == reloc_class_relative; 8865 8866 if (relativea < relativeb) 8867 return 1; 8868 if (relativea > relativeb) 8869 return -1; 8870 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) 8871 return -1; 8872 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) 8873 return 1; 8874 if (a->rela->r_offset < b->rela->r_offset) 8875 return -1; 8876 if (a->rela->r_offset > b->rela->r_offset) 8877 return 1; 8878 return 0; 8879 } 8880 8881 static int 8882 elf_link_sort_cmp2 (const void *A, const void *B) 8883 { 8884 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8885 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8886 8887 if (a->type < b->type) 8888 return -1; 8889 if (a->type > b->type) 8890 return 1; 8891 if (a->u.offset < b->u.offset) 8892 return -1; 8893 if (a->u.offset > b->u.offset) 8894 return 1; 8895 if (a->rela->r_offset < b->rela->r_offset) 8896 return -1; 8897 if (a->rela->r_offset > b->rela->r_offset) 8898 return 1; 8899 return 0; 8900 } 8901 8902 static size_t 8903 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) 8904 { 8905 asection *dynamic_relocs; 8906 asection *rela_dyn; 8907 asection *rel_dyn; 8908 bfd_size_type count, size; 8909 size_t i, ret, sort_elt, ext_size; 8910 bfd_byte *sort, *s_non_relative, *p; 8911 struct elf_link_sort_rela *sq; 8912 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8913 int i2e = bed->s->int_rels_per_ext_rel; 8914 unsigned int opb = bfd_octets_per_byte (abfd); 8915 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 8916 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 8917 struct bfd_link_order *lo; 8918 bfd_vma r_sym_mask; 8919 bfd_boolean use_rela; 8920 8921 /* Find a dynamic reloc section. */ 8922 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); 8923 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); 8924 if (rela_dyn != NULL && rela_dyn->size > 0 8925 && rel_dyn != NULL && rel_dyn->size > 0) 8926 { 8927 bfd_boolean use_rela_initialised = FALSE; 8928 8929 /* This is just here to stop gcc from complaining. 8930 Its initialization checking code is not perfect. */ 8931 use_rela = TRUE; 8932 8933 /* Both sections are present. Examine the sizes 8934 of the indirect sections to help us choose. */ 8935 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) 8936 if (lo->type == bfd_indirect_link_order) 8937 { 8938 asection *o = lo->u.indirect.section; 8939 8940 if ((o->size % bed->s->sizeof_rela) == 0) 8941 { 8942 if ((o->size % bed->s->sizeof_rel) == 0) 8943 /* Section size is divisible by both rel and rela sizes. 8944 It is of no help to us. */ 8945 ; 8946 else 8947 { 8948 /* Section size is only divisible by rela. */ 8949 if (use_rela_initialised && !use_rela) 8950 { 8951 _bfd_error_handler (_("%B: Unable to sort relocs - " 8952 "they are in more than one size"), 8953 abfd); 8954 bfd_set_error (bfd_error_invalid_operation); 8955 return 0; 8956 } 8957 else 8958 { 8959 use_rela = TRUE; 8960 use_rela_initialised = TRUE; 8961 } 8962 } 8963 } 8964 else if ((o->size % bed->s->sizeof_rel) == 0) 8965 { 8966 /* Section size is only divisible by rel. */ 8967 if (use_rela_initialised && use_rela) 8968 { 8969 _bfd_error_handler (_("%B: Unable to sort relocs - " 8970 "they are in more than one size"), 8971 abfd); 8972 bfd_set_error (bfd_error_invalid_operation); 8973 return 0; 8974 } 8975 else 8976 { 8977 use_rela = FALSE; 8978 use_rela_initialised = TRUE; 8979 } 8980 } 8981 else 8982 { 8983 /* The section size is not divisible by either - 8984 something is wrong. */ 8985 _bfd_error_handler (_("%B: Unable to sort relocs - " 8986 "they are of an unknown size"), abfd); 8987 bfd_set_error (bfd_error_invalid_operation); 8988 return 0; 8989 } 8990 } 8991 8992 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) 8993 if (lo->type == bfd_indirect_link_order) 8994 { 8995 asection *o = lo->u.indirect.section; 8996 8997 if ((o->size % bed->s->sizeof_rela) == 0) 8998 { 8999 if ((o->size % bed->s->sizeof_rel) == 0) 9000 /* Section size is divisible by both rel and rela sizes. 9001 It is of no help to us. */ 9002 ; 9003 else 9004 { 9005 /* Section size is only divisible by rela. */ 9006 if (use_rela_initialised && !use_rela) 9007 { 9008 _bfd_error_handler (_("%B: Unable to sort relocs - " 9009 "they are in more than one size"), 9010 abfd); 9011 bfd_set_error (bfd_error_invalid_operation); 9012 return 0; 9013 } 9014 else 9015 { 9016 use_rela = TRUE; 9017 use_rela_initialised = TRUE; 9018 } 9019 } 9020 } 9021 else if ((o->size % bed->s->sizeof_rel) == 0) 9022 { 9023 /* Section size is only divisible by rel. */ 9024 if (use_rela_initialised && use_rela) 9025 { 9026 _bfd_error_handler (_("%B: Unable to sort relocs - " 9027 "they are in more than one size"), 9028 abfd); 9029 bfd_set_error (bfd_error_invalid_operation); 9030 return 0; 9031 } 9032 else 9033 { 9034 use_rela = FALSE; 9035 use_rela_initialised = TRUE; 9036 } 9037 } 9038 else 9039 { 9040 /* The section size is not divisible by either - 9041 something is wrong. */ 9042 _bfd_error_handler (_("%B: Unable to sort relocs - " 9043 "they are of an unknown size"), abfd); 9044 bfd_set_error (bfd_error_invalid_operation); 9045 return 0; 9046 } 9047 } 9048 9049 if (! use_rela_initialised) 9050 /* Make a guess. */ 9051 use_rela = TRUE; 9052 } 9053 else if (rela_dyn != NULL && rela_dyn->size > 0) 9054 use_rela = TRUE; 9055 else if (rel_dyn != NULL && rel_dyn->size > 0) 9056 use_rela = FALSE; 9057 else 9058 return 0; 9059 9060 if (use_rela) 9061 { 9062 dynamic_relocs = rela_dyn; 9063 ext_size = bed->s->sizeof_rela; 9064 swap_in = bed->s->swap_reloca_in; 9065 swap_out = bed->s->swap_reloca_out; 9066 } 9067 else 9068 { 9069 dynamic_relocs = rel_dyn; 9070 ext_size = bed->s->sizeof_rel; 9071 swap_in = bed->s->swap_reloc_in; 9072 swap_out = bed->s->swap_reloc_out; 9073 } 9074 9075 size = 0; 9076 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 9077 if (lo->type == bfd_indirect_link_order) 9078 size += lo->u.indirect.section->size; 9079 9080 if (size != dynamic_relocs->size) 9081 return 0; 9082 9083 sort_elt = (sizeof (struct elf_link_sort_rela) 9084 + (i2e - 1) * sizeof (Elf_Internal_Rela)); 9085 9086 count = dynamic_relocs->size / ext_size; 9087 if (count == 0) 9088 return 0; 9089 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); 9090 9091 if (sort == NULL) 9092 { 9093 (*info->callbacks->warning) 9094 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); 9095 return 0; 9096 } 9097 9098 if (bed->s->arch_size == 32) 9099 r_sym_mask = ~(bfd_vma) 0xff; 9100 else 9101 r_sym_mask = ~(bfd_vma) 0xffffffff; 9102 9103 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 9104 if (lo->type == bfd_indirect_link_order) 9105 { 9106 bfd_byte *erel, *erelend; 9107 asection *o = lo->u.indirect.section; 9108 9109 if (o->contents == NULL && o->size != 0) 9110 { 9111 /* This is a reloc section that is being handled as a normal 9112 section. See bfd_section_from_shdr. We can't combine 9113 relocs in this case. */ 9114 free (sort); 9115 return 0; 9116 } 9117 erel = o->contents; 9118 erelend = o->contents + o->size; 9119 p = sort + o->output_offset * opb / ext_size * sort_elt; 9120 9121 while (erel < erelend) 9122 { 9123 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 9124 9125 (*swap_in) (abfd, erel, s->rela); 9126 s->type = (*bed->elf_backend_reloc_type_class) (info, o, s->rela); 9127 s->u.sym_mask = r_sym_mask; 9128 p += sort_elt; 9129 erel += ext_size; 9130 } 9131 } 9132 9133 qsort (sort, count, sort_elt, elf_link_sort_cmp1); 9134 9135 for (i = 0, p = sort; i < count; i++, p += sort_elt) 9136 { 9137 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 9138 if (s->type != reloc_class_relative) 9139 break; 9140 } 9141 ret = i; 9142 s_non_relative = p; 9143 9144 sq = (struct elf_link_sort_rela *) s_non_relative; 9145 for (; i < count; i++, p += sort_elt) 9146 { 9147 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; 9148 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) 9149 sq = sp; 9150 sp->u.offset = sq->rela->r_offset; 9151 } 9152 9153 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); 9154 9155 struct elf_link_hash_table *htab = elf_hash_table (info); 9156 if (htab->srelplt && htab->srelplt->output_section == dynamic_relocs) 9157 { 9158 /* We have plt relocs in .rela.dyn. */ 9159 sq = (struct elf_link_sort_rela *) sort; 9160 for (i = 0; i < count; i++) 9161 if (sq[count - i - 1].type != reloc_class_plt) 9162 break; 9163 if (i != 0 && htab->srelplt->size == i * ext_size) 9164 { 9165 struct bfd_link_order **plo; 9166 /* Put srelplt link_order last. This is so the output_offset 9167 set in the next loop is correct for DT_JMPREL. */ 9168 for (plo = &dynamic_relocs->map_head.link_order; *plo != NULL; ) 9169 if ((*plo)->type == bfd_indirect_link_order 9170 && (*plo)->u.indirect.section == htab->srelplt) 9171 { 9172 lo = *plo; 9173 *plo = lo->next; 9174 } 9175 else 9176 plo = &(*plo)->next; 9177 *plo = lo; 9178 lo->next = NULL; 9179 dynamic_relocs->map_tail.link_order = lo; 9180 } 9181 } 9182 9183 p = sort; 9184 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 9185 if (lo->type == bfd_indirect_link_order) 9186 { 9187 bfd_byte *erel, *erelend; 9188 asection *o = lo->u.indirect.section; 9189 9190 erel = o->contents; 9191 erelend = o->contents + o->size; 9192 o->output_offset = (p - sort) / sort_elt * ext_size / opb; 9193 while (erel < erelend) 9194 { 9195 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 9196 (*swap_out) (abfd, s->rela, erel); 9197 p += sort_elt; 9198 erel += ext_size; 9199 } 9200 } 9201 9202 free (sort); 9203 *psec = dynamic_relocs; 9204 return ret; 9205 } 9206 9207 /* Add a symbol to the output symbol string table. */ 9208 9209 static int 9210 elf_link_output_symstrtab (struct elf_final_link_info *flinfo, 9211 const char *name, 9212 Elf_Internal_Sym *elfsym, 9213 asection *input_sec, 9214 struct elf_link_hash_entry *h) 9215 { 9216 int (*output_symbol_hook) 9217 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, 9218 struct elf_link_hash_entry *); 9219 struct elf_link_hash_table *hash_table; 9220 const struct elf_backend_data *bed; 9221 bfd_size_type strtabsize; 9222 9223 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 9224 9225 bed = get_elf_backend_data (flinfo->output_bfd); 9226 output_symbol_hook = bed->elf_backend_link_output_symbol_hook; 9227 if (output_symbol_hook != NULL) 9228 { 9229 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); 9230 if (ret != 1) 9231 return ret; 9232 } 9233 9234 if (name == NULL 9235 || *name == '\0' 9236 || (input_sec->flags & SEC_EXCLUDE)) 9237 elfsym->st_name = (unsigned long) -1; 9238 else 9239 { 9240 /* Call _bfd_elf_strtab_offset after _bfd_elf_strtab_finalize 9241 to get the final offset for st_name. */ 9242 elfsym->st_name 9243 = (unsigned long) _bfd_elf_strtab_add (flinfo->symstrtab, 9244 name, FALSE); 9245 if (elfsym->st_name == (unsigned long) -1) 9246 return 0; 9247 } 9248 9249 hash_table = elf_hash_table (flinfo->info); 9250 strtabsize = hash_table->strtabsize; 9251 if (strtabsize <= hash_table->strtabcount) 9252 { 9253 strtabsize += strtabsize; 9254 hash_table->strtabsize = strtabsize; 9255 strtabsize *= sizeof (*hash_table->strtab); 9256 hash_table->strtab 9257 = (struct elf_sym_strtab *) bfd_realloc (hash_table->strtab, 9258 strtabsize); 9259 if (hash_table->strtab == NULL) 9260 return 0; 9261 } 9262 hash_table->strtab[hash_table->strtabcount].sym = *elfsym; 9263 hash_table->strtab[hash_table->strtabcount].dest_index 9264 = hash_table->strtabcount; 9265 hash_table->strtab[hash_table->strtabcount].destshndx_index 9266 = flinfo->symshndxbuf ? bfd_get_symcount (flinfo->output_bfd) : 0; 9267 9268 bfd_get_symcount (flinfo->output_bfd) += 1; 9269 hash_table->strtabcount += 1; 9270 9271 return 1; 9272 } 9273 9274 /* Swap symbols out to the symbol table and flush the output symbols to 9275 the file. */ 9276 9277 static bfd_boolean 9278 elf_link_swap_symbols_out (struct elf_final_link_info *flinfo) 9279 { 9280 struct elf_link_hash_table *hash_table = elf_hash_table (flinfo->info); 9281 bfd_size_type amt; 9282 size_t i; 9283 const struct elf_backend_data *bed; 9284 bfd_byte *symbuf; 9285 Elf_Internal_Shdr *hdr; 9286 file_ptr pos; 9287 bfd_boolean ret; 9288 9289 if (!hash_table->strtabcount) 9290 return TRUE; 9291 9292 BFD_ASSERT (elf_onesymtab (flinfo->output_bfd)); 9293 9294 bed = get_elf_backend_data (flinfo->output_bfd); 9295 9296 amt = bed->s->sizeof_sym * hash_table->strtabcount; 9297 symbuf = (bfd_byte *) bfd_malloc (amt); 9298 if (symbuf == NULL) 9299 return FALSE; 9300 9301 if (flinfo->symshndxbuf) 9302 { 9303 amt = sizeof (Elf_External_Sym_Shndx); 9304 amt *= bfd_get_symcount (flinfo->output_bfd); 9305 flinfo->symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); 9306 if (flinfo->symshndxbuf == NULL) 9307 { 9308 free (symbuf); 9309 return FALSE; 9310 } 9311 } 9312 9313 for (i = 0; i < hash_table->strtabcount; i++) 9314 { 9315 struct elf_sym_strtab *elfsym = &hash_table->strtab[i]; 9316 if (elfsym->sym.st_name == (unsigned long) -1) 9317 elfsym->sym.st_name = 0; 9318 else 9319 elfsym->sym.st_name 9320 = (unsigned long) _bfd_elf_strtab_offset (flinfo->symstrtab, 9321 elfsym->sym.st_name); 9322 bed->s->swap_symbol_out (flinfo->output_bfd, &elfsym->sym, 9323 ((bfd_byte *) symbuf 9324 + (elfsym->dest_index 9325 * bed->s->sizeof_sym)), 9326 (flinfo->symshndxbuf 9327 + elfsym->destshndx_index)); 9328 } 9329 9330 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; 9331 pos = hdr->sh_offset + hdr->sh_size; 9332 amt = hash_table->strtabcount * bed->s->sizeof_sym; 9333 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) == 0 9334 && bfd_bwrite (symbuf, amt, flinfo->output_bfd) == amt) 9335 { 9336 hdr->sh_size += amt; 9337 ret = TRUE; 9338 } 9339 else 9340 ret = FALSE; 9341 9342 free (symbuf); 9343 9344 free (hash_table->strtab); 9345 hash_table->strtab = NULL; 9346 9347 return ret; 9348 } 9349 9350 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ 9351 9352 static bfd_boolean 9353 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) 9354 { 9355 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) 9356 && sym->st_shndx < SHN_LORESERVE) 9357 { 9358 /* The gABI doesn't support dynamic symbols in output sections 9359 beyond 64k. */ 9360 _bfd_error_handler 9361 /* xgettext:c-format */ 9362 (_("%B: Too many sections: %d (>= %d)"), 9363 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); 9364 bfd_set_error (bfd_error_nonrepresentable_section); 9365 return FALSE; 9366 } 9367 return TRUE; 9368 } 9369 9370 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in 9371 allowing an unsatisfied unversioned symbol in the DSO to match a 9372 versioned symbol that would normally require an explicit version. 9373 We also handle the case that a DSO references a hidden symbol 9374 which may be satisfied by a versioned symbol in another DSO. */ 9375 9376 static bfd_boolean 9377 elf_link_check_versioned_symbol (struct bfd_link_info *info, 9378 const struct elf_backend_data *bed, 9379 struct elf_link_hash_entry *h) 9380 { 9381 bfd *abfd; 9382 struct elf_link_loaded_list *loaded; 9383 9384 if (!is_elf_hash_table (info->hash)) 9385 return FALSE; 9386 9387 /* Check indirect symbol. */ 9388 while (h->root.type == bfd_link_hash_indirect) 9389 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9390 9391 switch (h->root.type) 9392 { 9393 default: 9394 abfd = NULL; 9395 break; 9396 9397 case bfd_link_hash_undefined: 9398 case bfd_link_hash_undefweak: 9399 abfd = h->root.u.undef.abfd; 9400 if (abfd == NULL 9401 || (abfd->flags & DYNAMIC) == 0 9402 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) 9403 return FALSE; 9404 break; 9405 9406 case bfd_link_hash_defined: 9407 case bfd_link_hash_defweak: 9408 abfd = h->root.u.def.section->owner; 9409 break; 9410 9411 case bfd_link_hash_common: 9412 abfd = h->root.u.c.p->section->owner; 9413 break; 9414 } 9415 BFD_ASSERT (abfd != NULL); 9416 9417 for (loaded = elf_hash_table (info)->loaded; 9418 loaded != NULL; 9419 loaded = loaded->next) 9420 { 9421 bfd *input; 9422 Elf_Internal_Shdr *hdr; 9423 size_t symcount; 9424 size_t extsymcount; 9425 size_t extsymoff; 9426 Elf_Internal_Shdr *versymhdr; 9427 Elf_Internal_Sym *isym; 9428 Elf_Internal_Sym *isymend; 9429 Elf_Internal_Sym *isymbuf; 9430 Elf_External_Versym *ever; 9431 Elf_External_Versym *extversym; 9432 9433 input = loaded->abfd; 9434 9435 /* We check each DSO for a possible hidden versioned definition. */ 9436 if (input == abfd 9437 || (input->flags & DYNAMIC) == 0 9438 || elf_dynversym (input) == 0) 9439 continue; 9440 9441 hdr = &elf_tdata (input)->dynsymtab_hdr; 9442 9443 symcount = hdr->sh_size / bed->s->sizeof_sym; 9444 if (elf_bad_symtab (input)) 9445 { 9446 extsymcount = symcount; 9447 extsymoff = 0; 9448 } 9449 else 9450 { 9451 extsymcount = symcount - hdr->sh_info; 9452 extsymoff = hdr->sh_info; 9453 } 9454 9455 if (extsymcount == 0) 9456 continue; 9457 9458 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, 9459 NULL, NULL, NULL); 9460 if (isymbuf == NULL) 9461 return FALSE; 9462 9463 /* Read in any version definitions. */ 9464 versymhdr = &elf_tdata (input)->dynversym_hdr; 9465 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 9466 if (extversym == NULL) 9467 goto error_ret; 9468 9469 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 9470 || (bfd_bread (extversym, versymhdr->sh_size, input) 9471 != versymhdr->sh_size)) 9472 { 9473 free (extversym); 9474 error_ret: 9475 free (isymbuf); 9476 return FALSE; 9477 } 9478 9479 ever = extversym + extsymoff; 9480 isymend = isymbuf + extsymcount; 9481 for (isym = isymbuf; isym < isymend; isym++, ever++) 9482 { 9483 const char *name; 9484 Elf_Internal_Versym iver; 9485 unsigned short version_index; 9486 9487 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL 9488 || isym->st_shndx == SHN_UNDEF) 9489 continue; 9490 9491 name = bfd_elf_string_from_elf_section (input, 9492 hdr->sh_link, 9493 isym->st_name); 9494 if (strcmp (name, h->root.root.string) != 0) 9495 continue; 9496 9497 _bfd_elf_swap_versym_in (input, ever, &iver); 9498 9499 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 9500 && !(h->def_regular 9501 && h->forced_local)) 9502 { 9503 /* If we have a non-hidden versioned sym, then it should 9504 have provided a definition for the undefined sym unless 9505 it is defined in a non-shared object and forced local. 9506 */ 9507 abort (); 9508 } 9509 9510 version_index = iver.vs_vers & VERSYM_VERSION; 9511 if (version_index == 1 || version_index == 2) 9512 { 9513 /* This is the base or first version. We can use it. */ 9514 free (extversym); 9515 free (isymbuf); 9516 return TRUE; 9517 } 9518 } 9519 9520 free (extversym); 9521 free (isymbuf); 9522 } 9523 9524 return FALSE; 9525 } 9526 9527 /* Convert ELF common symbol TYPE. */ 9528 9529 static int 9530 elf_link_convert_common_type (struct bfd_link_info *info, int type) 9531 { 9532 /* Commom symbol can only appear in relocatable link. */ 9533 if (!bfd_link_relocatable (info)) 9534 abort (); 9535 switch (info->elf_stt_common) 9536 { 9537 case unchanged: 9538 break; 9539 case elf_stt_common: 9540 type = STT_COMMON; 9541 break; 9542 case no_elf_stt_common: 9543 type = STT_OBJECT; 9544 break; 9545 } 9546 return type; 9547 } 9548 9549 /* Add an external symbol to the symbol table. This is called from 9550 the hash table traversal routine. When generating a shared object, 9551 we go through the symbol table twice. The first time we output 9552 anything that might have been forced to local scope in a version 9553 script. The second time we output the symbols that are still 9554 global symbols. */ 9555 9556 static bfd_boolean 9557 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) 9558 { 9559 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; 9560 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; 9561 struct elf_final_link_info *flinfo = eoinfo->flinfo; 9562 bfd_boolean strip; 9563 Elf_Internal_Sym sym; 9564 asection *input_sec; 9565 const struct elf_backend_data *bed; 9566 long indx; 9567 int ret; 9568 unsigned int type; 9569 9570 if (h->root.type == bfd_link_hash_warning) 9571 { 9572 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9573 if (h->root.type == bfd_link_hash_new) 9574 return TRUE; 9575 } 9576 9577 /* Decide whether to output this symbol in this pass. */ 9578 if (eoinfo->localsyms) 9579 { 9580 if (!h->forced_local) 9581 return TRUE; 9582 } 9583 else 9584 { 9585 if (h->forced_local) 9586 return TRUE; 9587 } 9588 9589 bed = get_elf_backend_data (flinfo->output_bfd); 9590 9591 if (h->root.type == bfd_link_hash_undefined) 9592 { 9593 /* If we have an undefined symbol reference here then it must have 9594 come from a shared library that is being linked in. (Undefined 9595 references in regular files have already been handled unless 9596 they are in unreferenced sections which are removed by garbage 9597 collection). */ 9598 bfd_boolean ignore_undef = FALSE; 9599 9600 /* Some symbols may be special in that the fact that they're 9601 undefined can be safely ignored - let backend determine that. */ 9602 if (bed->elf_backend_ignore_undef_symbol) 9603 ignore_undef = bed->elf_backend_ignore_undef_symbol (h); 9604 9605 /* If we are reporting errors for this situation then do so now. */ 9606 if (!ignore_undef 9607 && h->ref_dynamic 9608 && (!h->ref_regular || flinfo->info->gc_sections) 9609 && !elf_link_check_versioned_symbol (flinfo->info, bed, h) 9610 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) 9611 (*flinfo->info->callbacks->undefined_symbol) 9612 (flinfo->info, h->root.root.string, 9613 h->ref_regular ? NULL : h->root.u.undef.abfd, 9614 NULL, 0, 9615 flinfo->info->unresolved_syms_in_shared_libs == RM_GENERATE_ERROR); 9616 9617 /* Strip a global symbol defined in a discarded section. */ 9618 if (h->indx == -3) 9619 return TRUE; 9620 } 9621 9622 /* We should also warn if a forced local symbol is referenced from 9623 shared libraries. */ 9624 if (bfd_link_executable (flinfo->info) 9625 && h->forced_local 9626 && h->ref_dynamic 9627 && h->def_regular 9628 && !h->dynamic_def 9629 && h->ref_dynamic_nonweak 9630 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) 9631 { 9632 bfd *def_bfd; 9633 const char *msg; 9634 struct elf_link_hash_entry *hi = h; 9635 9636 /* Check indirect symbol. */ 9637 while (hi->root.type == bfd_link_hash_indirect) 9638 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 9639 9640 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 9641 /* xgettext:c-format */ 9642 msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); 9643 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) 9644 /* xgettext:c-format */ 9645 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); 9646 else 9647 /* xgettext:c-format */ 9648 msg = _("%B: local symbol `%s' in %B is referenced by DSO"); 9649 def_bfd = flinfo->output_bfd; 9650 if (hi->root.u.def.section != bfd_abs_section_ptr) 9651 def_bfd = hi->root.u.def.section->owner; 9652 _bfd_error_handler (msg, flinfo->output_bfd, 9653 h->root.root.string, def_bfd); 9654 bfd_set_error (bfd_error_bad_value); 9655 eoinfo->failed = TRUE; 9656 return FALSE; 9657 } 9658 9659 /* We don't want to output symbols that have never been mentioned by 9660 a regular file, or that we have been told to strip. However, if 9661 h->indx is set to -2, the symbol is used by a reloc and we must 9662 output it. */ 9663 strip = FALSE; 9664 if (h->indx == -2) 9665 ; 9666 else if ((h->def_dynamic 9667 || h->ref_dynamic 9668 || h->root.type == bfd_link_hash_new) 9669 && !h->def_regular 9670 && !h->ref_regular) 9671 strip = TRUE; 9672 else if (flinfo->info->strip == strip_all) 9673 strip = TRUE; 9674 else if (flinfo->info->strip == strip_some 9675 && bfd_hash_lookup (flinfo->info->keep_hash, 9676 h->root.root.string, FALSE, FALSE) == NULL) 9677 strip = TRUE; 9678 else if ((h->root.type == bfd_link_hash_defined 9679 || h->root.type == bfd_link_hash_defweak) 9680 && ((flinfo->info->strip_discarded 9681 && discarded_section (h->root.u.def.section)) 9682 || ((h->root.u.def.section->flags & SEC_LINKER_CREATED) == 0 9683 && h->root.u.def.section->owner != NULL 9684 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) 9685 strip = TRUE; 9686 else if ((h->root.type == bfd_link_hash_undefined 9687 || h->root.type == bfd_link_hash_undefweak) 9688 && h->root.u.undef.abfd != NULL 9689 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) 9690 strip = TRUE; 9691 9692 type = h->type; 9693 9694 /* If we're stripping it, and it's not a dynamic symbol, there's 9695 nothing else to do. However, if it is a forced local symbol or 9696 an ifunc symbol we need to give the backend finish_dynamic_symbol 9697 function a chance to make it dynamic. */ 9698 if (strip 9699 && h->dynindx == -1 9700 && type != STT_GNU_IFUNC 9701 && !h->forced_local) 9702 return TRUE; 9703 9704 sym.st_value = 0; 9705 sym.st_size = h->size; 9706 sym.st_other = h->other; 9707 switch (h->root.type) 9708 { 9709 default: 9710 case bfd_link_hash_new: 9711 case bfd_link_hash_warning: 9712 abort (); 9713 return FALSE; 9714 9715 case bfd_link_hash_undefined: 9716 case bfd_link_hash_undefweak: 9717 input_sec = bfd_und_section_ptr; 9718 sym.st_shndx = SHN_UNDEF; 9719 break; 9720 9721 case bfd_link_hash_defined: 9722 case bfd_link_hash_defweak: 9723 { 9724 input_sec = h->root.u.def.section; 9725 if (input_sec->output_section != NULL) 9726 { 9727 sym.st_shndx = 9728 _bfd_elf_section_from_bfd_section (flinfo->output_bfd, 9729 input_sec->output_section); 9730 if (sym.st_shndx == SHN_BAD) 9731 { 9732 _bfd_error_handler 9733 /* xgettext:c-format */ 9734 (_("%B: could not find output section %A for input section %A"), 9735 flinfo->output_bfd, input_sec->output_section, input_sec); 9736 bfd_set_error (bfd_error_nonrepresentable_section); 9737 eoinfo->failed = TRUE; 9738 return FALSE; 9739 } 9740 9741 /* ELF symbols in relocatable files are section relative, 9742 but in nonrelocatable files they are virtual 9743 addresses. */ 9744 sym.st_value = h->root.u.def.value + input_sec->output_offset; 9745 if (!bfd_link_relocatable (flinfo->info)) 9746 { 9747 sym.st_value += input_sec->output_section->vma; 9748 if (h->type == STT_TLS) 9749 { 9750 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; 9751 if (tls_sec != NULL) 9752 sym.st_value -= tls_sec->vma; 9753 } 9754 } 9755 } 9756 else 9757 { 9758 BFD_ASSERT (input_sec->owner == NULL 9759 || (input_sec->owner->flags & DYNAMIC) != 0); 9760 sym.st_shndx = SHN_UNDEF; 9761 input_sec = bfd_und_section_ptr; 9762 } 9763 } 9764 break; 9765 9766 case bfd_link_hash_common: 9767 input_sec = h->root.u.c.p->section; 9768 sym.st_shndx = bed->common_section_index (input_sec); 9769 sym.st_value = 1 << h->root.u.c.p->alignment_power; 9770 break; 9771 9772 case bfd_link_hash_indirect: 9773 /* These symbols are created by symbol versioning. They point 9774 to the decorated version of the name. For example, if the 9775 symbol foo@@GNU_1.2 is the default, which should be used when 9776 foo is used with no version, then we add an indirect symbol 9777 foo which points to foo@@GNU_1.2. We ignore these symbols, 9778 since the indirected symbol is already in the hash table. */ 9779 return TRUE; 9780 } 9781 9782 if (type == STT_COMMON || type == STT_OBJECT) 9783 switch (h->root.type) 9784 { 9785 case bfd_link_hash_common: 9786 type = elf_link_convert_common_type (flinfo->info, type); 9787 break; 9788 case bfd_link_hash_defined: 9789 case bfd_link_hash_defweak: 9790 if (bed->common_definition (&sym)) 9791 type = elf_link_convert_common_type (flinfo->info, type); 9792 else 9793 type = STT_OBJECT; 9794 break; 9795 case bfd_link_hash_undefined: 9796 case bfd_link_hash_undefweak: 9797 break; 9798 default: 9799 abort (); 9800 } 9801 9802 if (h->forced_local) 9803 { 9804 sym.st_info = ELF_ST_INFO (STB_LOCAL, type); 9805 /* Turn off visibility on local symbol. */ 9806 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 9807 } 9808 /* Set STB_GNU_UNIQUE only if symbol is defined in regular object. */ 9809 else if (h->unique_global && h->def_regular) 9810 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, type); 9811 else if (h->root.type == bfd_link_hash_undefweak 9812 || h->root.type == bfd_link_hash_defweak) 9813 sym.st_info = ELF_ST_INFO (STB_WEAK, type); 9814 else 9815 sym.st_info = ELF_ST_INFO (STB_GLOBAL, type); 9816 sym.st_target_internal = h->target_internal; 9817 9818 /* Give the processor backend a chance to tweak the symbol value, 9819 and also to finish up anything that needs to be done for this 9820 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for 9821 forced local syms when non-shared is due to a historical quirk. 9822 STT_GNU_IFUNC symbol must go through PLT. */ 9823 if ((h->type == STT_GNU_IFUNC 9824 && h->def_regular 9825 && !bfd_link_relocatable (flinfo->info)) 9826 || ((h->dynindx != -1 9827 || h->forced_local) 9828 && ((bfd_link_pic (flinfo->info) 9829 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 9830 || h->root.type != bfd_link_hash_undefweak)) 9831 || !h->forced_local) 9832 && elf_hash_table (flinfo->info)->dynamic_sections_created)) 9833 { 9834 if (! ((*bed->elf_backend_finish_dynamic_symbol) 9835 (flinfo->output_bfd, flinfo->info, h, &sym))) 9836 { 9837 eoinfo->failed = TRUE; 9838 return FALSE; 9839 } 9840 } 9841 9842 /* If we are marking the symbol as undefined, and there are no 9843 non-weak references to this symbol from a regular object, then 9844 mark the symbol as weak undefined; if there are non-weak 9845 references, mark the symbol as strong. We can't do this earlier, 9846 because it might not be marked as undefined until the 9847 finish_dynamic_symbol routine gets through with it. */ 9848 if (sym.st_shndx == SHN_UNDEF 9849 && h->ref_regular 9850 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL 9851 || ELF_ST_BIND (sym.st_info) == STB_WEAK)) 9852 { 9853 int bindtype; 9854 type = ELF_ST_TYPE (sym.st_info); 9855 9856 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ 9857 if (type == STT_GNU_IFUNC) 9858 type = STT_FUNC; 9859 9860 if (h->ref_regular_nonweak) 9861 bindtype = STB_GLOBAL; 9862 else 9863 bindtype = STB_WEAK; 9864 sym.st_info = ELF_ST_INFO (bindtype, type); 9865 } 9866 9867 /* If this is a symbol defined in a dynamic library, don't use the 9868 symbol size from the dynamic library. Relinking an executable 9869 against a new library may introduce gratuitous changes in the 9870 executable's symbols if we keep the size. */ 9871 if (sym.st_shndx == SHN_UNDEF 9872 && !h->def_regular 9873 && h->def_dynamic) 9874 sym.st_size = 0; 9875 9876 /* If a non-weak symbol with non-default visibility is not defined 9877 locally, it is a fatal error. */ 9878 if (!bfd_link_relocatable (flinfo->info) 9879 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT 9880 && ELF_ST_BIND (sym.st_info) != STB_WEAK 9881 && h->root.type == bfd_link_hash_undefined 9882 && !h->def_regular) 9883 { 9884 const char *msg; 9885 9886 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) 9887 /* xgettext:c-format */ 9888 msg = _("%B: protected symbol `%s' isn't defined"); 9889 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) 9890 /* xgettext:c-format */ 9891 msg = _("%B: internal symbol `%s' isn't defined"); 9892 else 9893 /* xgettext:c-format */ 9894 msg = _("%B: hidden symbol `%s' isn't defined"); 9895 _bfd_error_handler (msg, flinfo->output_bfd, h->root.root.string); 9896 bfd_set_error (bfd_error_bad_value); 9897 eoinfo->failed = TRUE; 9898 return FALSE; 9899 } 9900 9901 /* If this symbol should be put in the .dynsym section, then put it 9902 there now. We already know the symbol index. We also fill in 9903 the entry in the .hash section. */ 9904 if (elf_hash_table (flinfo->info)->dynsym != NULL 9905 && h->dynindx != -1 9906 && elf_hash_table (flinfo->info)->dynamic_sections_created) 9907 { 9908 bfd_byte *esym; 9909 9910 /* Since there is no version information in the dynamic string, 9911 if there is no version info in symbol version section, we will 9912 have a run-time problem if not linking executable, referenced 9913 by shared library, or not bound locally. */ 9914 if (h->verinfo.verdef == NULL 9915 && (!bfd_link_executable (flinfo->info) 9916 || h->ref_dynamic 9917 || !h->def_regular)) 9918 { 9919 char *p = strrchr (h->root.root.string, ELF_VER_CHR); 9920 9921 if (p && p [1] != '\0') 9922 { 9923 _bfd_error_handler 9924 /* xgettext:c-format */ 9925 (_("%B: No symbol version section for versioned symbol `%s'"), 9926 flinfo->output_bfd, h->root.root.string); 9927 eoinfo->failed = TRUE; 9928 return FALSE; 9929 } 9930 } 9931 9932 sym.st_name = h->dynstr_index; 9933 esym = (elf_hash_table (flinfo->info)->dynsym->contents 9934 + h->dynindx * bed->s->sizeof_sym); 9935 if (!check_dynsym (flinfo->output_bfd, &sym)) 9936 { 9937 eoinfo->failed = TRUE; 9938 return FALSE; 9939 } 9940 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); 9941 9942 if (flinfo->hash_sec != NULL) 9943 { 9944 size_t hash_entry_size; 9945 bfd_byte *bucketpos; 9946 bfd_vma chain; 9947 size_t bucketcount; 9948 size_t bucket; 9949 9950 bucketcount = elf_hash_table (flinfo->info)->bucketcount; 9951 bucket = h->u.elf_hash_value % bucketcount; 9952 9953 hash_entry_size 9954 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; 9955 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents 9956 + (bucket + 2) * hash_entry_size); 9957 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); 9958 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, 9959 bucketpos); 9960 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, 9961 ((bfd_byte *) flinfo->hash_sec->contents 9962 + (bucketcount + 2 + h->dynindx) * hash_entry_size)); 9963 } 9964 9965 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) 9966 { 9967 Elf_Internal_Versym iversym; 9968 Elf_External_Versym *eversym; 9969 9970 if (!h->def_regular) 9971 { 9972 if (h->verinfo.verdef == NULL 9973 || (elf_dyn_lib_class (h->verinfo.verdef->vd_bfd) 9974 & (DYN_AS_NEEDED | DYN_DT_NEEDED | DYN_NO_NEEDED))) 9975 iversym.vs_vers = 0; 9976 else 9977 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; 9978 } 9979 else 9980 { 9981 if (h->verinfo.vertree == NULL) 9982 iversym.vs_vers = 1; 9983 else 9984 iversym.vs_vers = h->verinfo.vertree->vernum + 1; 9985 if (flinfo->info->create_default_symver) 9986 iversym.vs_vers++; 9987 } 9988 9989 /* Turn on VERSYM_HIDDEN only if the hidden versioned symbol is 9990 defined locally. */ 9991 if (h->versioned == versioned_hidden && h->def_regular) 9992 iversym.vs_vers |= VERSYM_HIDDEN; 9993 9994 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; 9995 eversym += h->dynindx; 9996 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); 9997 } 9998 } 9999 10000 /* If the symbol is undefined, and we didn't output it to .dynsym, 10001 strip it from .symtab too. Obviously we can't do this for 10002 relocatable output or when needed for --emit-relocs. */ 10003 else if (input_sec == bfd_und_section_ptr 10004 && h->indx != -2 10005 /* PR 22319 Do not strip global undefined symbols marked as being needed. */ 10006 && (h->mark != 1 || ELF_ST_BIND (sym.st_info) != STB_GLOBAL) 10007 && !bfd_link_relocatable (flinfo->info)) 10008 return TRUE; 10009 10010 /* Also strip others that we couldn't earlier due to dynamic symbol 10011 processing. */ 10012 if (strip) 10013 return TRUE; 10014 if ((input_sec->flags & SEC_EXCLUDE) != 0) 10015 return TRUE; 10016 10017 /* Output a FILE symbol so that following locals are not associated 10018 with the wrong input file. We need one for forced local symbols 10019 if we've seen more than one FILE symbol or when we have exactly 10020 one FILE symbol but global symbols are present in a file other 10021 than the one with the FILE symbol. We also need one if linker 10022 defined symbols are present. In practice these conditions are 10023 always met, so just emit the FILE symbol unconditionally. */ 10024 if (eoinfo->localsyms 10025 && !eoinfo->file_sym_done 10026 && eoinfo->flinfo->filesym_count != 0) 10027 { 10028 Elf_Internal_Sym fsym; 10029 10030 memset (&fsym, 0, sizeof (fsym)); 10031 fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 10032 fsym.st_shndx = SHN_ABS; 10033 if (!elf_link_output_symstrtab (eoinfo->flinfo, NULL, &fsym, 10034 bfd_und_section_ptr, NULL)) 10035 return FALSE; 10036 10037 eoinfo->file_sym_done = TRUE; 10038 } 10039 10040 indx = bfd_get_symcount (flinfo->output_bfd); 10041 ret = elf_link_output_symstrtab (flinfo, h->root.root.string, &sym, 10042 input_sec, h); 10043 if (ret == 0) 10044 { 10045 eoinfo->failed = TRUE; 10046 return FALSE; 10047 } 10048 else if (ret == 1) 10049 h->indx = indx; 10050 else if (h->indx == -2) 10051 abort(); 10052 10053 return TRUE; 10054 } 10055 10056 /* Return TRUE if special handling is done for relocs in SEC against 10057 symbols defined in discarded sections. */ 10058 10059 static bfd_boolean 10060 elf_section_ignore_discarded_relocs (asection *sec) 10061 { 10062 const struct elf_backend_data *bed; 10063 10064 switch (sec->sec_info_type) 10065 { 10066 case SEC_INFO_TYPE_STABS: 10067 case SEC_INFO_TYPE_EH_FRAME: 10068 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 10069 return TRUE; 10070 default: 10071 break; 10072 } 10073 10074 bed = get_elf_backend_data (sec->owner); 10075 if (bed->elf_backend_ignore_discarded_relocs != NULL 10076 && (*bed->elf_backend_ignore_discarded_relocs) (sec)) 10077 return TRUE; 10078 10079 return FALSE; 10080 } 10081 10082 /* Return a mask saying how ld should treat relocations in SEC against 10083 symbols defined in discarded sections. If this function returns 10084 COMPLAIN set, ld will issue a warning message. If this function 10085 returns PRETEND set, and the discarded section was link-once and the 10086 same size as the kept link-once section, ld will pretend that the 10087 symbol was actually defined in the kept section. Otherwise ld will 10088 zero the reloc (at least that is the intent, but some cooperation by 10089 the target dependent code is needed, particularly for REL targets). */ 10090 10091 unsigned int 10092 _bfd_elf_default_action_discarded (asection *sec) 10093 { 10094 if (sec->flags & SEC_DEBUGGING) 10095 return PRETEND; 10096 10097 if (strcmp (".eh_frame", sec->name) == 0) 10098 return 0; 10099 10100 if (strcmp (".gcc_except_table", sec->name) == 0) 10101 return 0; 10102 10103 return COMPLAIN | PRETEND; 10104 } 10105 10106 /* Find a match between a section and a member of a section group. */ 10107 10108 static asection * 10109 match_group_member (asection *sec, asection *group, 10110 struct bfd_link_info *info) 10111 { 10112 asection *first = elf_next_in_group (group); 10113 asection *s = first; 10114 10115 while (s != NULL) 10116 { 10117 if (bfd_elf_match_symbols_in_sections (s, sec, info)) 10118 return s; 10119 10120 s = elf_next_in_group (s); 10121 if (s == first) 10122 break; 10123 } 10124 10125 return NULL; 10126 } 10127 10128 /* Check if the kept section of a discarded section SEC can be used 10129 to replace it. Return the replacement if it is OK. Otherwise return 10130 NULL. */ 10131 10132 asection * 10133 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) 10134 { 10135 asection *kept; 10136 10137 kept = sec->kept_section; 10138 if (kept != NULL) 10139 { 10140 if ((kept->flags & SEC_GROUP) != 0) 10141 kept = match_group_member (sec, kept, info); 10142 if (kept != NULL 10143 && ((sec->rawsize != 0 ? sec->rawsize : sec->size) 10144 != (kept->rawsize != 0 ? kept->rawsize : kept->size))) 10145 kept = NULL; 10146 sec->kept_section = kept; 10147 } 10148 return kept; 10149 } 10150 10151 /* Link an input file into the linker output file. This function 10152 handles all the sections and relocations of the input file at once. 10153 This is so that we only have to read the local symbols once, and 10154 don't have to keep them in memory. */ 10155 10156 static bfd_boolean 10157 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) 10158 { 10159 int (*relocate_section) 10160 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 10161 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); 10162 bfd *output_bfd; 10163 Elf_Internal_Shdr *symtab_hdr; 10164 size_t locsymcount; 10165 size_t extsymoff; 10166 Elf_Internal_Sym *isymbuf; 10167 Elf_Internal_Sym *isym; 10168 Elf_Internal_Sym *isymend; 10169 long *pindex; 10170 asection **ppsection; 10171 asection *o; 10172 const struct elf_backend_data *bed; 10173 struct elf_link_hash_entry **sym_hashes; 10174 bfd_size_type address_size; 10175 bfd_vma r_type_mask; 10176 int r_sym_shift; 10177 bfd_boolean have_file_sym = FALSE; 10178 10179 output_bfd = flinfo->output_bfd; 10180 bed = get_elf_backend_data (output_bfd); 10181 relocate_section = bed->elf_backend_relocate_section; 10182 10183 /* If this is a dynamic object, we don't want to do anything here: 10184 we don't want the local symbols, and we don't want the section 10185 contents. */ 10186 if ((input_bfd->flags & DYNAMIC) != 0) 10187 return TRUE; 10188 10189 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 10190 if (elf_bad_symtab (input_bfd)) 10191 { 10192 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 10193 extsymoff = 0; 10194 } 10195 else 10196 { 10197 locsymcount = symtab_hdr->sh_info; 10198 extsymoff = symtab_hdr->sh_info; 10199 } 10200 10201 /* Read the local symbols. */ 10202 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 10203 if (isymbuf == NULL && locsymcount != 0) 10204 { 10205 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 10206 flinfo->internal_syms, 10207 flinfo->external_syms, 10208 flinfo->locsym_shndx); 10209 if (isymbuf == NULL) 10210 return FALSE; 10211 } 10212 10213 /* Find local symbol sections and adjust values of symbols in 10214 SEC_MERGE sections. Write out those local symbols we know are 10215 going into the output file. */ 10216 isymend = isymbuf + locsymcount; 10217 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; 10218 isym < isymend; 10219 isym++, pindex++, ppsection++) 10220 { 10221 asection *isec; 10222 const char *name; 10223 Elf_Internal_Sym osym; 10224 long indx; 10225 int ret; 10226 10227 *pindex = -1; 10228 10229 if (elf_bad_symtab (input_bfd)) 10230 { 10231 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) 10232 { 10233 *ppsection = NULL; 10234 continue; 10235 } 10236 } 10237 10238 if (isym->st_shndx == SHN_UNDEF) 10239 isec = bfd_und_section_ptr; 10240 else if (isym->st_shndx == SHN_ABS) 10241 isec = bfd_abs_section_ptr; 10242 else if (isym->st_shndx == SHN_COMMON) 10243 isec = bfd_com_section_ptr; 10244 else 10245 { 10246 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 10247 if (isec == NULL) 10248 { 10249 /* Don't attempt to output symbols with st_shnx in the 10250 reserved range other than SHN_ABS and SHN_COMMON. */ 10251 *ppsection = NULL; 10252 continue; 10253 } 10254 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE 10255 && ELF_ST_TYPE (isym->st_info) != STT_SECTION) 10256 isym->st_value = 10257 _bfd_merged_section_offset (output_bfd, &isec, 10258 elf_section_data (isec)->sec_info, 10259 isym->st_value); 10260 } 10261 10262 *ppsection = isec; 10263 10264 /* Don't output the first, undefined, symbol. In fact, don't 10265 output any undefined local symbol. */ 10266 if (isec == bfd_und_section_ptr) 10267 continue; 10268 10269 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 10270 { 10271 /* We never output section symbols. Instead, we use the 10272 section symbol of the corresponding section in the output 10273 file. */ 10274 continue; 10275 } 10276 10277 /* If we are stripping all symbols, we don't want to output this 10278 one. */ 10279 if (flinfo->info->strip == strip_all) 10280 continue; 10281 10282 /* If we are discarding all local symbols, we don't want to 10283 output this one. If we are generating a relocatable output 10284 file, then some of the local symbols may be required by 10285 relocs; we output them below as we discover that they are 10286 needed. */ 10287 if (flinfo->info->discard == discard_all) 10288 continue; 10289 10290 /* If this symbol is defined in a section which we are 10291 discarding, we don't need to keep it. */ 10292 if (isym->st_shndx != SHN_UNDEF 10293 && isym->st_shndx < SHN_LORESERVE 10294 && bfd_section_removed_from_list (output_bfd, 10295 isec->output_section)) 10296 continue; 10297 10298 /* Get the name of the symbol. */ 10299 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, 10300 isym->st_name); 10301 if (name == NULL) 10302 return FALSE; 10303 10304 /* See if we are discarding symbols with this name. */ 10305 if ((flinfo->info->strip == strip_some 10306 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) 10307 == NULL)) 10308 || (((flinfo->info->discard == discard_sec_merge 10309 && (isec->flags & SEC_MERGE) 10310 && !bfd_link_relocatable (flinfo->info)) 10311 || flinfo->info->discard == discard_l) 10312 && bfd_is_local_label_name (input_bfd, name))) 10313 continue; 10314 10315 if (ELF_ST_TYPE (isym->st_info) == STT_FILE) 10316 { 10317 if (input_bfd->lto_output) 10318 /* -flto puts a temp file name here. This means builds 10319 are not reproducible. Discard the symbol. */ 10320 continue; 10321 have_file_sym = TRUE; 10322 flinfo->filesym_count += 1; 10323 } 10324 if (!have_file_sym) 10325 { 10326 /* In the absence of debug info, bfd_find_nearest_line uses 10327 FILE symbols to determine the source file for local 10328 function symbols. Provide a FILE symbol here if input 10329 files lack such, so that their symbols won't be 10330 associated with a previous input file. It's not the 10331 source file, but the best we can do. */ 10332 have_file_sym = TRUE; 10333 flinfo->filesym_count += 1; 10334 memset (&osym, 0, sizeof (osym)); 10335 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 10336 osym.st_shndx = SHN_ABS; 10337 if (!elf_link_output_symstrtab (flinfo, 10338 (input_bfd->lto_output ? NULL 10339 : input_bfd->filename), 10340 &osym, bfd_abs_section_ptr, 10341 NULL)) 10342 return FALSE; 10343 } 10344 10345 osym = *isym; 10346 10347 /* Adjust the section index for the output file. */ 10348 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 10349 isec->output_section); 10350 if (osym.st_shndx == SHN_BAD) 10351 return FALSE; 10352 10353 /* ELF symbols in relocatable files are section relative, but 10354 in executable files they are virtual addresses. Note that 10355 this code assumes that all ELF sections have an associated 10356 BFD section with a reasonable value for output_offset; below 10357 we assume that they also have a reasonable value for 10358 output_section. Any special sections must be set up to meet 10359 these requirements. */ 10360 osym.st_value += isec->output_offset; 10361 if (!bfd_link_relocatable (flinfo->info)) 10362 { 10363 osym.st_value += isec->output_section->vma; 10364 if (ELF_ST_TYPE (osym.st_info) == STT_TLS) 10365 { 10366 /* STT_TLS symbols are relative to PT_TLS segment base. */ 10367 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); 10368 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; 10369 } 10370 } 10371 10372 indx = bfd_get_symcount (output_bfd); 10373 ret = elf_link_output_symstrtab (flinfo, name, &osym, isec, NULL); 10374 if (ret == 0) 10375 return FALSE; 10376 else if (ret == 1) 10377 *pindex = indx; 10378 } 10379 10380 if (bed->s->arch_size == 32) 10381 { 10382 r_type_mask = 0xff; 10383 r_sym_shift = 8; 10384 address_size = 4; 10385 } 10386 else 10387 { 10388 r_type_mask = 0xffffffff; 10389 r_sym_shift = 32; 10390 address_size = 8; 10391 } 10392 10393 /* Relocate the contents of each section. */ 10394 sym_hashes = elf_sym_hashes (input_bfd); 10395 for (o = input_bfd->sections; o != NULL; o = o->next) 10396 { 10397 bfd_byte *contents; 10398 10399 if (! o->linker_mark) 10400 { 10401 /* This section was omitted from the link. */ 10402 continue; 10403 } 10404 10405 if (!flinfo->info->resolve_section_groups 10406 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) 10407 { 10408 /* Deal with the group signature symbol. */ 10409 struct bfd_elf_section_data *sec_data = elf_section_data (o); 10410 unsigned long symndx = sec_data->this_hdr.sh_info; 10411 asection *osec = o->output_section; 10412 10413 BFD_ASSERT (bfd_link_relocatable (flinfo->info)); 10414 if (symndx >= locsymcount 10415 || (elf_bad_symtab (input_bfd) 10416 && flinfo->sections[symndx] == NULL)) 10417 { 10418 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; 10419 while (h->root.type == bfd_link_hash_indirect 10420 || h->root.type == bfd_link_hash_warning) 10421 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10422 /* Arrange for symbol to be output. */ 10423 h->indx = -2; 10424 elf_section_data (osec)->this_hdr.sh_info = -2; 10425 } 10426 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) 10427 { 10428 /* We'll use the output section target_index. */ 10429 asection *sec = flinfo->sections[symndx]->output_section; 10430 elf_section_data (osec)->this_hdr.sh_info = sec->target_index; 10431 } 10432 else 10433 { 10434 if (flinfo->indices[symndx] == -1) 10435 { 10436 /* Otherwise output the local symbol now. */ 10437 Elf_Internal_Sym sym = isymbuf[symndx]; 10438 asection *sec = flinfo->sections[symndx]->output_section; 10439 const char *name; 10440 long indx; 10441 int ret; 10442 10443 name = bfd_elf_string_from_elf_section (input_bfd, 10444 symtab_hdr->sh_link, 10445 sym.st_name); 10446 if (name == NULL) 10447 return FALSE; 10448 10449 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 10450 sec); 10451 if (sym.st_shndx == SHN_BAD) 10452 return FALSE; 10453 10454 sym.st_value += o->output_offset; 10455 10456 indx = bfd_get_symcount (output_bfd); 10457 ret = elf_link_output_symstrtab (flinfo, name, &sym, o, 10458 NULL); 10459 if (ret == 0) 10460 return FALSE; 10461 else if (ret == 1) 10462 flinfo->indices[symndx] = indx; 10463 else 10464 abort (); 10465 } 10466 elf_section_data (osec)->this_hdr.sh_info 10467 = flinfo->indices[symndx]; 10468 } 10469 } 10470 10471 if ((o->flags & SEC_HAS_CONTENTS) == 0 10472 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 10473 continue; 10474 10475 if ((o->flags & SEC_LINKER_CREATED) != 0) 10476 { 10477 /* Section was created by _bfd_elf_link_create_dynamic_sections 10478 or somesuch. */ 10479 continue; 10480 } 10481 10482 /* Get the contents of the section. They have been cached by a 10483 relaxation routine. Note that o is a section in an input 10484 file, so the contents field will not have been set by any of 10485 the routines which work on output files. */ 10486 if (elf_section_data (o)->this_hdr.contents != NULL) 10487 { 10488 contents = elf_section_data (o)->this_hdr.contents; 10489 if (bed->caches_rawsize 10490 && o->rawsize != 0 10491 && o->rawsize < o->size) 10492 { 10493 memcpy (flinfo->contents, contents, o->rawsize); 10494 contents = flinfo->contents; 10495 } 10496 } 10497 else 10498 { 10499 contents = flinfo->contents; 10500 if (! bfd_get_full_section_contents (input_bfd, o, &contents)) 10501 return FALSE; 10502 } 10503 10504 if ((o->flags & SEC_RELOC) != 0) 10505 { 10506 Elf_Internal_Rela *internal_relocs; 10507 Elf_Internal_Rela *rel, *relend; 10508 int action_discarded; 10509 int ret; 10510 10511 /* Get the swapped relocs. */ 10512 internal_relocs 10513 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, 10514 flinfo->internal_relocs, FALSE); 10515 if (internal_relocs == NULL 10516 && o->reloc_count > 0) 10517 return FALSE; 10518 10519 /* We need to reverse-copy input .ctors/.dtors sections if 10520 they are placed in .init_array/.finit_array for output. */ 10521 if (o->size > address_size 10522 && ((strncmp (o->name, ".ctors", 6) == 0 10523 && strcmp (o->output_section->name, 10524 ".init_array") == 0) 10525 || (strncmp (o->name, ".dtors", 6) == 0 10526 && strcmp (o->output_section->name, 10527 ".fini_array") == 0)) 10528 && (o->name[6] == 0 || o->name[6] == '.')) 10529 { 10530 if (o->size * bed->s->int_rels_per_ext_rel 10531 != o->reloc_count * address_size) 10532 { 10533 _bfd_error_handler 10534 /* xgettext:c-format */ 10535 (_("error: %B: size of section %A is not " 10536 "multiple of address size"), 10537 input_bfd, o); 10538 bfd_set_error (bfd_error_bad_value); 10539 return FALSE; 10540 } 10541 o->flags |= SEC_ELF_REVERSE_COPY; 10542 } 10543 10544 action_discarded = -1; 10545 if (!elf_section_ignore_discarded_relocs (o)) 10546 action_discarded = (*bed->action_discarded) (o); 10547 10548 /* Run through the relocs evaluating complex reloc symbols and 10549 looking for relocs against symbols from discarded sections 10550 or section symbols from removed link-once sections. 10551 Complain about relocs against discarded sections. Zero 10552 relocs against removed link-once sections. */ 10553 10554 rel = internal_relocs; 10555 relend = rel + o->reloc_count; 10556 for ( ; rel < relend; rel++) 10557 { 10558 unsigned long r_symndx = rel->r_info >> r_sym_shift; 10559 unsigned int s_type; 10560 asection **ps, *sec; 10561 struct elf_link_hash_entry *h = NULL; 10562 const char *sym_name; 10563 10564 if (r_symndx == STN_UNDEF) 10565 continue; 10566 10567 if (r_symndx >= locsymcount 10568 || (elf_bad_symtab (input_bfd) 10569 && flinfo->sections[r_symndx] == NULL)) 10570 { 10571 h = sym_hashes[r_symndx - extsymoff]; 10572 10573 /* Badly formatted input files can contain relocs that 10574 reference non-existant symbols. Check here so that 10575 we do not seg fault. */ 10576 if (h == NULL) 10577 { 10578 _bfd_error_handler 10579 /* xgettext:c-format */ 10580 (_("error: %B contains a reloc (%#Lx) for section %A " 10581 "that references a non-existent global symbol"), 10582 input_bfd, rel->r_info, o); 10583 bfd_set_error (bfd_error_bad_value); 10584 return FALSE; 10585 } 10586 10587 while (h->root.type == bfd_link_hash_indirect 10588 || h->root.type == bfd_link_hash_warning) 10589 h = (struct elf_link_hash_entry *) h->root.u.i.link; 10590 10591 s_type = h->type; 10592 10593 /* If a plugin symbol is referenced from a non-IR file, 10594 mark the symbol as undefined. Note that the 10595 linker may attach linker created dynamic sections 10596 to the plugin bfd. Symbols defined in linker 10597 created sections are not plugin symbols. */ 10598 if ((h->root.non_ir_ref_regular 10599 || h->root.non_ir_ref_dynamic) 10600 && (h->root.type == bfd_link_hash_defined 10601 || h->root.type == bfd_link_hash_defweak) 10602 && (h->root.u.def.section->flags 10603 & SEC_LINKER_CREATED) == 0 10604 && h->root.u.def.section->owner != NULL 10605 && (h->root.u.def.section->owner->flags 10606 & BFD_PLUGIN) != 0) 10607 { 10608 h->root.type = bfd_link_hash_undefined; 10609 h->root.u.undef.abfd = h->root.u.def.section->owner; 10610 } 10611 10612 ps = NULL; 10613 if (h->root.type == bfd_link_hash_defined 10614 || h->root.type == bfd_link_hash_defweak) 10615 ps = &h->root.u.def.section; 10616 10617 sym_name = h->root.root.string; 10618 } 10619 else 10620 { 10621 Elf_Internal_Sym *sym = isymbuf + r_symndx; 10622 10623 s_type = ELF_ST_TYPE (sym->st_info); 10624 ps = &flinfo->sections[r_symndx]; 10625 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, 10626 sym, *ps); 10627 } 10628 10629 if ((s_type == STT_RELC || s_type == STT_SRELC) 10630 && !bfd_link_relocatable (flinfo->info)) 10631 { 10632 bfd_vma val; 10633 bfd_vma dot = (rel->r_offset 10634 + o->output_offset + o->output_section->vma); 10635 #ifdef DEBUG 10636 printf ("Encountered a complex symbol!"); 10637 printf (" (input_bfd %s, section %s, reloc %ld\n", 10638 input_bfd->filename, o->name, 10639 (long) (rel - internal_relocs)); 10640 printf (" symbol: idx %8.8lx, name %s\n", 10641 r_symndx, sym_name); 10642 printf (" reloc : info %8.8lx, addr %8.8lx\n", 10643 (unsigned long) rel->r_info, 10644 (unsigned long) rel->r_offset); 10645 #endif 10646 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, 10647 isymbuf, locsymcount, s_type == STT_SRELC)) 10648 return FALSE; 10649 10650 /* Symbol evaluated OK. Update to absolute value. */ 10651 set_symbol_value (input_bfd, isymbuf, locsymcount, 10652 r_symndx, val); 10653 continue; 10654 } 10655 10656 if (action_discarded != -1 && ps != NULL) 10657 { 10658 /* Complain if the definition comes from a 10659 discarded section. */ 10660 if ((sec = *ps) != NULL && discarded_section (sec)) 10661 { 10662 BFD_ASSERT (r_symndx != STN_UNDEF); 10663 if (action_discarded & COMPLAIN) 10664 (*flinfo->info->callbacks->einfo) 10665 /* xgettext:c-format */ 10666 (_("%X`%s' referenced in section `%A' of %B: " 10667 "defined in discarded section `%A' of %B\n"), 10668 sym_name, o, input_bfd, sec, sec->owner); 10669 10670 /* Try to do the best we can to support buggy old 10671 versions of gcc. Pretend that the symbol is 10672 really defined in the kept linkonce section. 10673 FIXME: This is quite broken. Modifying the 10674 symbol here means we will be changing all later 10675 uses of the symbol, not just in this section. */ 10676 if (action_discarded & PRETEND) 10677 { 10678 asection *kept; 10679 10680 kept = _bfd_elf_check_kept_section (sec, 10681 flinfo->info); 10682 if (kept != NULL) 10683 { 10684 *ps = kept; 10685 continue; 10686 } 10687 } 10688 } 10689 } 10690 } 10691 10692 /* Relocate the section by invoking a back end routine. 10693 10694 The back end routine is responsible for adjusting the 10695 section contents as necessary, and (if using Rela relocs 10696 and generating a relocatable output file) adjusting the 10697 reloc addend as necessary. 10698 10699 The back end routine does not have to worry about setting 10700 the reloc address or the reloc symbol index. 10701 10702 The back end routine is given a pointer to the swapped in 10703 internal symbols, and can access the hash table entries 10704 for the external symbols via elf_sym_hashes (input_bfd). 10705 10706 When generating relocatable output, the back end routine 10707 must handle STB_LOCAL/STT_SECTION symbols specially. The 10708 output symbol is going to be a section symbol 10709 corresponding to the output section, which will require 10710 the addend to be adjusted. */ 10711 10712 ret = (*relocate_section) (output_bfd, flinfo->info, 10713 input_bfd, o, contents, 10714 internal_relocs, 10715 isymbuf, 10716 flinfo->sections); 10717 if (!ret) 10718 return FALSE; 10719 10720 if (ret == 2 10721 || bfd_link_relocatable (flinfo->info) 10722 || flinfo->info->emitrelocations) 10723 { 10724 Elf_Internal_Rela *irela; 10725 Elf_Internal_Rela *irelaend, *irelamid; 10726 bfd_vma last_offset; 10727 struct elf_link_hash_entry **rel_hash; 10728 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; 10729 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; 10730 unsigned int next_erel; 10731 bfd_boolean rela_normal; 10732 struct bfd_elf_section_data *esdi, *esdo; 10733 10734 esdi = elf_section_data (o); 10735 esdo = elf_section_data (o->output_section); 10736 rela_normal = FALSE; 10737 10738 /* Adjust the reloc addresses and symbol indices. */ 10739 10740 irela = internal_relocs; 10741 irelaend = irela + o->reloc_count; 10742 rel_hash = esdo->rel.hashes + esdo->rel.count; 10743 /* We start processing the REL relocs, if any. When we reach 10744 IRELAMID in the loop, we switch to the RELA relocs. */ 10745 irelamid = irela; 10746 if (esdi->rel.hdr != NULL) 10747 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) 10748 * bed->s->int_rels_per_ext_rel); 10749 rel_hash_list = rel_hash; 10750 rela_hash_list = NULL; 10751 last_offset = o->output_offset; 10752 if (!bfd_link_relocatable (flinfo->info)) 10753 last_offset += o->output_section->vma; 10754 for (next_erel = 0; irela < irelaend; irela++, next_erel++) 10755 { 10756 unsigned long r_symndx; 10757 asection *sec; 10758 Elf_Internal_Sym sym; 10759 10760 if (next_erel == bed->s->int_rels_per_ext_rel) 10761 { 10762 rel_hash++; 10763 next_erel = 0; 10764 } 10765 10766 if (irela == irelamid) 10767 { 10768 rel_hash = esdo->rela.hashes + esdo->rela.count; 10769 rela_hash_list = rel_hash; 10770 rela_normal = bed->rela_normal; 10771 } 10772 10773 irela->r_offset = _bfd_elf_section_offset (output_bfd, 10774 flinfo->info, o, 10775 irela->r_offset); 10776 if (irela->r_offset >= (bfd_vma) -2) 10777 { 10778 /* This is a reloc for a deleted entry or somesuch. 10779 Turn it into an R_*_NONE reloc, at the same 10780 offset as the last reloc. elf_eh_frame.c and 10781 bfd_elf_discard_info rely on reloc offsets 10782 being ordered. */ 10783 irela->r_offset = last_offset; 10784 irela->r_info = 0; 10785 irela->r_addend = 0; 10786 continue; 10787 } 10788 10789 irela->r_offset += o->output_offset; 10790 10791 /* Relocs in an executable have to be virtual addresses. */ 10792 if (!bfd_link_relocatable (flinfo->info)) 10793 irela->r_offset += o->output_section->vma; 10794 10795 last_offset = irela->r_offset; 10796 10797 r_symndx = irela->r_info >> r_sym_shift; 10798 if (r_symndx == STN_UNDEF) 10799 continue; 10800 10801 if (r_symndx >= locsymcount 10802 || (elf_bad_symtab (input_bfd) 10803 && flinfo->sections[r_symndx] == NULL)) 10804 { 10805 struct elf_link_hash_entry *rh; 10806 unsigned long indx; 10807 10808 /* This is a reloc against a global symbol. We 10809 have not yet output all the local symbols, so 10810 we do not know the symbol index of any global 10811 symbol. We set the rel_hash entry for this 10812 reloc to point to the global hash table entry 10813 for this symbol. The symbol index is then 10814 set at the end of bfd_elf_final_link. */ 10815 indx = r_symndx - extsymoff; 10816 rh = elf_sym_hashes (input_bfd)[indx]; 10817 while (rh->root.type == bfd_link_hash_indirect 10818 || rh->root.type == bfd_link_hash_warning) 10819 rh = (struct elf_link_hash_entry *) rh->root.u.i.link; 10820 10821 /* Setting the index to -2 tells 10822 elf_link_output_extsym that this symbol is 10823 used by a reloc. */ 10824 BFD_ASSERT (rh->indx < 0); 10825 rh->indx = -2; 10826 *rel_hash = rh; 10827 10828 continue; 10829 } 10830 10831 /* This is a reloc against a local symbol. */ 10832 10833 *rel_hash = NULL; 10834 sym = isymbuf[r_symndx]; 10835 sec = flinfo->sections[r_symndx]; 10836 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) 10837 { 10838 /* I suppose the backend ought to fill in the 10839 section of any STT_SECTION symbol against a 10840 processor specific section. */ 10841 r_symndx = STN_UNDEF; 10842 if (bfd_is_abs_section (sec)) 10843 ; 10844 else if (sec == NULL || sec->owner == NULL) 10845 { 10846 bfd_set_error (bfd_error_bad_value); 10847 return FALSE; 10848 } 10849 else 10850 { 10851 asection *osec = sec->output_section; 10852 10853 /* If we have discarded a section, the output 10854 section will be the absolute section. In 10855 case of discarded SEC_MERGE sections, use 10856 the kept section. relocate_section should 10857 have already handled discarded linkonce 10858 sections. */ 10859 if (bfd_is_abs_section (osec) 10860 && sec->kept_section != NULL 10861 && sec->kept_section->output_section != NULL) 10862 { 10863 osec = sec->kept_section->output_section; 10864 irela->r_addend -= osec->vma; 10865 } 10866 10867 if (!bfd_is_abs_section (osec)) 10868 { 10869 r_symndx = osec->target_index; 10870 if (r_symndx == STN_UNDEF) 10871 { 10872 irela->r_addend += osec->vma; 10873 osec = _bfd_nearby_section (output_bfd, osec, 10874 osec->vma); 10875 irela->r_addend -= osec->vma; 10876 r_symndx = osec->target_index; 10877 } 10878 } 10879 } 10880 10881 /* Adjust the addend according to where the 10882 section winds up in the output section. */ 10883 if (rela_normal) 10884 irela->r_addend += sec->output_offset; 10885 } 10886 else 10887 { 10888 if (flinfo->indices[r_symndx] == -1) 10889 { 10890 unsigned long shlink; 10891 const char *name; 10892 asection *osec; 10893 long indx; 10894 10895 if (flinfo->info->strip == strip_all) 10896 { 10897 /* You can't do ld -r -s. */ 10898 bfd_set_error (bfd_error_invalid_operation); 10899 return FALSE; 10900 } 10901 10902 /* This symbol was skipped earlier, but 10903 since it is needed by a reloc, we 10904 must output it now. */ 10905 shlink = symtab_hdr->sh_link; 10906 name = (bfd_elf_string_from_elf_section 10907 (input_bfd, shlink, sym.st_name)); 10908 if (name == NULL) 10909 return FALSE; 10910 10911 osec = sec->output_section; 10912 sym.st_shndx = 10913 _bfd_elf_section_from_bfd_section (output_bfd, 10914 osec); 10915 if (sym.st_shndx == SHN_BAD) 10916 return FALSE; 10917 10918 sym.st_value += sec->output_offset; 10919 if (!bfd_link_relocatable (flinfo->info)) 10920 { 10921 sym.st_value += osec->vma; 10922 if (ELF_ST_TYPE (sym.st_info) == STT_TLS) 10923 { 10924 /* STT_TLS symbols are relative to PT_TLS 10925 segment base. */ 10926 BFD_ASSERT (elf_hash_table (flinfo->info) 10927 ->tls_sec != NULL); 10928 sym.st_value -= (elf_hash_table (flinfo->info) 10929 ->tls_sec->vma); 10930 } 10931 } 10932 10933 indx = bfd_get_symcount (output_bfd); 10934 ret = elf_link_output_symstrtab (flinfo, name, 10935 &sym, sec, 10936 NULL); 10937 if (ret == 0) 10938 return FALSE; 10939 else if (ret == 1) 10940 flinfo->indices[r_symndx] = indx; 10941 else 10942 abort (); 10943 } 10944 10945 r_symndx = flinfo->indices[r_symndx]; 10946 } 10947 10948 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift 10949 | (irela->r_info & r_type_mask)); 10950 } 10951 10952 /* Swap out the relocs. */ 10953 input_rel_hdr = esdi->rel.hdr; 10954 if (input_rel_hdr && input_rel_hdr->sh_size != 0) 10955 { 10956 if (!bed->elf_backend_emit_relocs (output_bfd, o, 10957 input_rel_hdr, 10958 internal_relocs, 10959 rel_hash_list)) 10960 return FALSE; 10961 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) 10962 * bed->s->int_rels_per_ext_rel); 10963 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); 10964 } 10965 10966 input_rela_hdr = esdi->rela.hdr; 10967 if (input_rela_hdr && input_rela_hdr->sh_size != 0) 10968 { 10969 if (!bed->elf_backend_emit_relocs (output_bfd, o, 10970 input_rela_hdr, 10971 internal_relocs, 10972 rela_hash_list)) 10973 return FALSE; 10974 } 10975 } 10976 } 10977 10978 /* Write out the modified section contents. */ 10979 if (bed->elf_backend_write_section 10980 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, 10981 contents)) 10982 { 10983 /* Section written out. */ 10984 } 10985 else switch (o->sec_info_type) 10986 { 10987 case SEC_INFO_TYPE_STABS: 10988 if (! (_bfd_write_section_stabs 10989 (output_bfd, 10990 &elf_hash_table (flinfo->info)->stab_info, 10991 o, &elf_section_data (o)->sec_info, contents))) 10992 return FALSE; 10993 break; 10994 case SEC_INFO_TYPE_MERGE: 10995 if (! _bfd_write_merged_section (output_bfd, o, 10996 elf_section_data (o)->sec_info)) 10997 return FALSE; 10998 break; 10999 case SEC_INFO_TYPE_EH_FRAME: 11000 { 11001 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, 11002 o, contents)) 11003 return FALSE; 11004 } 11005 break; 11006 case SEC_INFO_TYPE_EH_FRAME_ENTRY: 11007 { 11008 if (! _bfd_elf_write_section_eh_frame_entry (output_bfd, 11009 flinfo->info, 11010 o, contents)) 11011 return FALSE; 11012 } 11013 break; 11014 default: 11015 { 11016 if (! (o->flags & SEC_EXCLUDE)) 11017 { 11018 file_ptr offset = (file_ptr) o->output_offset; 11019 bfd_size_type todo = o->size; 11020 11021 offset *= bfd_octets_per_byte (output_bfd); 11022 11023 if ((o->flags & SEC_ELF_REVERSE_COPY)) 11024 { 11025 /* Reverse-copy input section to output. */ 11026 do 11027 { 11028 todo -= address_size; 11029 if (! bfd_set_section_contents (output_bfd, 11030 o->output_section, 11031 contents + todo, 11032 offset, 11033 address_size)) 11034 return FALSE; 11035 if (todo == 0) 11036 break; 11037 offset += address_size; 11038 } 11039 while (1); 11040 } 11041 else if (! bfd_set_section_contents (output_bfd, 11042 o->output_section, 11043 contents, 11044 offset, todo)) 11045 return FALSE; 11046 } 11047 } 11048 break; 11049 } 11050 } 11051 11052 return TRUE; 11053 } 11054 11055 /* Generate a reloc when linking an ELF file. This is a reloc 11056 requested by the linker, and does not come from any input file. This 11057 is used to build constructor and destructor tables when linking 11058 with -Ur. */ 11059 11060 static bfd_boolean 11061 elf_reloc_link_order (bfd *output_bfd, 11062 struct bfd_link_info *info, 11063 asection *output_section, 11064 struct bfd_link_order *link_order) 11065 { 11066 reloc_howto_type *howto; 11067 long indx; 11068 bfd_vma offset; 11069 bfd_vma addend; 11070 struct bfd_elf_section_reloc_data *reldata; 11071 struct elf_link_hash_entry **rel_hash_ptr; 11072 Elf_Internal_Shdr *rel_hdr; 11073 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 11074 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; 11075 bfd_byte *erel; 11076 unsigned int i; 11077 struct bfd_elf_section_data *esdo = elf_section_data (output_section); 11078 11079 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 11080 if (howto == NULL) 11081 { 11082 bfd_set_error (bfd_error_bad_value); 11083 return FALSE; 11084 } 11085 11086 addend = link_order->u.reloc.p->addend; 11087 11088 if (esdo->rel.hdr) 11089 reldata = &esdo->rel; 11090 else if (esdo->rela.hdr) 11091 reldata = &esdo->rela; 11092 else 11093 { 11094 reldata = NULL; 11095 BFD_ASSERT (0); 11096 } 11097 11098 /* Figure out the symbol index. */ 11099 rel_hash_ptr = reldata->hashes + reldata->count; 11100 if (link_order->type == bfd_section_reloc_link_order) 11101 { 11102 indx = link_order->u.reloc.p->u.section->target_index; 11103 BFD_ASSERT (indx != 0); 11104 *rel_hash_ptr = NULL; 11105 } 11106 else 11107 { 11108 struct elf_link_hash_entry *h; 11109 11110 /* Treat a reloc against a defined symbol as though it were 11111 actually against the section. */ 11112 h = ((struct elf_link_hash_entry *) 11113 bfd_wrapped_link_hash_lookup (output_bfd, info, 11114 link_order->u.reloc.p->u.name, 11115 FALSE, FALSE, TRUE)); 11116 if (h != NULL 11117 && (h->root.type == bfd_link_hash_defined 11118 || h->root.type == bfd_link_hash_defweak)) 11119 { 11120 asection *section; 11121 11122 section = h->root.u.def.section; 11123 indx = section->output_section->target_index; 11124 *rel_hash_ptr = NULL; 11125 /* It seems that we ought to add the symbol value to the 11126 addend here, but in practice it has already been added 11127 because it was passed to constructor_callback. */ 11128 addend += section->output_section->vma + section->output_offset; 11129 } 11130 else if (h != NULL) 11131 { 11132 /* Setting the index to -2 tells elf_link_output_extsym that 11133 this symbol is used by a reloc. */ 11134 h->indx = -2; 11135 *rel_hash_ptr = h; 11136 indx = 0; 11137 } 11138 else 11139 { 11140 (*info->callbacks->unattached_reloc) 11141 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0); 11142 indx = 0; 11143 } 11144 } 11145 11146 /* If this is an inplace reloc, we must write the addend into the 11147 object file. */ 11148 if (howto->partial_inplace && addend != 0) 11149 { 11150 bfd_size_type size; 11151 bfd_reloc_status_type rstat; 11152 bfd_byte *buf; 11153 bfd_boolean ok; 11154 const char *sym_name; 11155 11156 size = (bfd_size_type) bfd_get_reloc_size (howto); 11157 buf = (bfd_byte *) bfd_zmalloc (size); 11158 if (buf == NULL && size != 0) 11159 return FALSE; 11160 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 11161 switch (rstat) 11162 { 11163 case bfd_reloc_ok: 11164 break; 11165 11166 default: 11167 case bfd_reloc_outofrange: 11168 abort (); 11169 11170 case bfd_reloc_overflow: 11171 if (link_order->type == bfd_section_reloc_link_order) 11172 sym_name = bfd_section_name (output_bfd, 11173 link_order->u.reloc.p->u.section); 11174 else 11175 sym_name = link_order->u.reloc.p->u.name; 11176 (*info->callbacks->reloc_overflow) (info, NULL, sym_name, 11177 howto->name, addend, NULL, NULL, 11178 (bfd_vma) 0); 11179 break; 11180 } 11181 11182 ok = bfd_set_section_contents (output_bfd, output_section, buf, 11183 link_order->offset 11184 * bfd_octets_per_byte (output_bfd), 11185 size); 11186 free (buf); 11187 if (! ok) 11188 return FALSE; 11189 } 11190 11191 /* The address of a reloc is relative to the section in a 11192 relocatable file, and is a virtual address in an executable 11193 file. */ 11194 offset = link_order->offset; 11195 if (! bfd_link_relocatable (info)) 11196 offset += output_section->vma; 11197 11198 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) 11199 { 11200 irel[i].r_offset = offset; 11201 irel[i].r_info = 0; 11202 irel[i].r_addend = 0; 11203 } 11204 if (bed->s->arch_size == 32) 11205 irel[0].r_info = ELF32_R_INFO (indx, howto->type); 11206 else 11207 #ifdef BFD64 11208 { 11209 bfd_uint64_t indx64 = indx; 11210 irel[0].r_info = ELF64_R_INFO (indx64, howto->type); 11211 } 11212 #else 11213 BFD_FAIL(); 11214 #endif 11215 11216 rel_hdr = reldata->hdr; 11217 erel = rel_hdr->contents; 11218 if (rel_hdr->sh_type == SHT_REL) 11219 { 11220 erel += reldata->count * bed->s->sizeof_rel; 11221 (*bed->s->swap_reloc_out) (output_bfd, irel, erel); 11222 } 11223 else 11224 { 11225 irel[0].r_addend = addend; 11226 erel += reldata->count * bed->s->sizeof_rela; 11227 (*bed->s->swap_reloca_out) (output_bfd, irel, erel); 11228 } 11229 11230 ++reldata->count; 11231 11232 return TRUE; 11233 } 11234 11235 11236 /* Get the output vma of the section pointed to by the sh_link field. */ 11237 11238 static bfd_vma 11239 elf_get_linked_section_vma (struct bfd_link_order *p) 11240 { 11241 Elf_Internal_Shdr **elf_shdrp; 11242 asection *s; 11243 int elfsec; 11244 11245 s = p->u.indirect.section; 11246 elf_shdrp = elf_elfsections (s->owner); 11247 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); 11248 elfsec = elf_shdrp[elfsec]->sh_link; 11249 /* PR 290: 11250 The Intel C compiler generates SHT_IA_64_UNWIND with 11251 SHF_LINK_ORDER. But it doesn't set the sh_link or 11252 sh_info fields. Hence we could get the situation 11253 where elfsec is 0. */ 11254 if (elfsec == 0) 11255 { 11256 const struct elf_backend_data *bed 11257 = get_elf_backend_data (s->owner); 11258 if (bed->link_order_error_handler) 11259 bed->link_order_error_handler 11260 /* xgettext:c-format */ 11261 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); 11262 return 0; 11263 } 11264 else 11265 { 11266 s = elf_shdrp[elfsec]->bfd_section; 11267 return s->output_section->vma + s->output_offset; 11268 } 11269 } 11270 11271 11272 /* Compare two sections based on the locations of the sections they are 11273 linked to. Used by elf_fixup_link_order. */ 11274 11275 static int 11276 compare_link_order (const void * a, const void * b) 11277 { 11278 bfd_vma apos; 11279 bfd_vma bpos; 11280 11281 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); 11282 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); 11283 if (apos < bpos) 11284 return -1; 11285 return apos > bpos; 11286 } 11287 11288 11289 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same 11290 order as their linked sections. Returns false if this could not be done 11291 because an output section includes both ordered and unordered 11292 sections. Ideally we'd do this in the linker proper. */ 11293 11294 static bfd_boolean 11295 elf_fixup_link_order (bfd *abfd, asection *o) 11296 { 11297 int seen_linkorder; 11298 int seen_other; 11299 int n; 11300 struct bfd_link_order *p; 11301 bfd *sub; 11302 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11303 unsigned elfsec; 11304 struct bfd_link_order **sections; 11305 asection *s, *other_sec, *linkorder_sec; 11306 bfd_vma offset; 11307 11308 other_sec = NULL; 11309 linkorder_sec = NULL; 11310 seen_other = 0; 11311 seen_linkorder = 0; 11312 for (p = o->map_head.link_order; p != NULL; p = p->next) 11313 { 11314 if (p->type == bfd_indirect_link_order) 11315 { 11316 s = p->u.indirect.section; 11317 sub = s->owner; 11318 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 11319 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass 11320 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) 11321 && elfsec < elf_numsections (sub) 11322 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER 11323 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) 11324 { 11325 seen_linkorder++; 11326 linkorder_sec = s; 11327 } 11328 else 11329 { 11330 seen_other++; 11331 other_sec = s; 11332 } 11333 } 11334 else 11335 seen_other++; 11336 11337 if (seen_other && seen_linkorder) 11338 { 11339 if (other_sec && linkorder_sec) 11340 _bfd_error_handler 11341 /* xgettext:c-format */ 11342 (_("%A has both ordered [`%A' in %B] " 11343 "and unordered [`%A' in %B] sections"), 11344 o, linkorder_sec, linkorder_sec->owner, 11345 other_sec, other_sec->owner); 11346 else 11347 _bfd_error_handler 11348 (_("%A has both ordered and unordered sections"), o); 11349 bfd_set_error (bfd_error_bad_value); 11350 return FALSE; 11351 } 11352 } 11353 11354 if (!seen_linkorder) 11355 return TRUE; 11356 11357 sections = (struct bfd_link_order **) 11358 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); 11359 if (sections == NULL) 11360 return FALSE; 11361 seen_linkorder = 0; 11362 11363 for (p = o->map_head.link_order; p != NULL; p = p->next) 11364 { 11365 sections[seen_linkorder++] = p; 11366 } 11367 /* Sort the input sections in the order of their linked section. */ 11368 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), 11369 compare_link_order); 11370 11371 /* Change the offsets of the sections. */ 11372 offset = 0; 11373 for (n = 0; n < seen_linkorder; n++) 11374 { 11375 s = sections[n]->u.indirect.section; 11376 offset &= ~(bfd_vma) 0 << s->alignment_power; 11377 s->output_offset = offset / bfd_octets_per_byte (abfd); 11378 sections[n]->offset = offset; 11379 offset += sections[n]->size; 11380 } 11381 11382 free (sections); 11383 return TRUE; 11384 } 11385 11386 /* Generate an import library in INFO->implib_bfd from symbols in ABFD. 11387 Returns TRUE upon success, FALSE otherwise. */ 11388 11389 static bfd_boolean 11390 elf_output_implib (bfd *abfd, struct bfd_link_info *info) 11391 { 11392 bfd_boolean ret = FALSE; 11393 bfd *implib_bfd; 11394 const struct elf_backend_data *bed; 11395 flagword flags; 11396 enum bfd_architecture arch; 11397 unsigned int mach; 11398 asymbol **sympp = NULL; 11399 long symsize; 11400 long symcount; 11401 long src_count; 11402 elf_symbol_type *osymbuf; 11403 11404 implib_bfd = info->out_implib_bfd; 11405 bed = get_elf_backend_data (abfd); 11406 11407 if (!bfd_set_format (implib_bfd, bfd_object)) 11408 return FALSE; 11409 11410 /* Use flag from executable but make it a relocatable object. */ 11411 flags = bfd_get_file_flags (abfd); 11412 flags &= ~HAS_RELOC; 11413 if (!bfd_set_start_address (implib_bfd, 0) 11414 || !bfd_set_file_flags (implib_bfd, flags & ~EXEC_P)) 11415 return FALSE; 11416 11417 /* Copy architecture of output file to import library file. */ 11418 arch = bfd_get_arch (abfd); 11419 mach = bfd_get_mach (abfd); 11420 if (!bfd_set_arch_mach (implib_bfd, arch, mach) 11421 && (abfd->target_defaulted 11422 || bfd_get_arch (abfd) != bfd_get_arch (implib_bfd))) 11423 return FALSE; 11424 11425 /* Get symbol table size. */ 11426 symsize = bfd_get_symtab_upper_bound (abfd); 11427 if (symsize < 0) 11428 return FALSE; 11429 11430 /* Read in the symbol table. */ 11431 sympp = (asymbol **) xmalloc (symsize); 11432 symcount = bfd_canonicalize_symtab (abfd, sympp); 11433 if (symcount < 0) 11434 goto free_sym_buf; 11435 11436 /* Allow the BFD backend to copy any private header data it 11437 understands from the output BFD to the import library BFD. */ 11438 if (! bfd_copy_private_header_data (abfd, implib_bfd)) 11439 goto free_sym_buf; 11440 11441 /* Filter symbols to appear in the import library. */ 11442 if (bed->elf_backend_filter_implib_symbols) 11443 symcount = bed->elf_backend_filter_implib_symbols (abfd, info, sympp, 11444 symcount); 11445 else 11446 symcount = _bfd_elf_filter_global_symbols (abfd, info, sympp, symcount); 11447 if (symcount == 0) 11448 { 11449 bfd_set_error (bfd_error_no_symbols); 11450 _bfd_error_handler (_("%B: no symbol found for import library"), 11451 implib_bfd); 11452 goto free_sym_buf; 11453 } 11454 11455 11456 /* Make symbols absolute. */ 11457 osymbuf = (elf_symbol_type *) bfd_alloc2 (implib_bfd, symcount, 11458 sizeof (*osymbuf)); 11459 for (src_count = 0; src_count < symcount; src_count++) 11460 { 11461 memcpy (&osymbuf[src_count], (elf_symbol_type *) sympp[src_count], 11462 sizeof (*osymbuf)); 11463 osymbuf[src_count].symbol.section = bfd_abs_section_ptr; 11464 osymbuf[src_count].internal_elf_sym.st_shndx = SHN_ABS; 11465 osymbuf[src_count].symbol.value += sympp[src_count]->section->vma; 11466 osymbuf[src_count].internal_elf_sym.st_value = 11467 osymbuf[src_count].symbol.value; 11468 sympp[src_count] = &osymbuf[src_count].symbol; 11469 } 11470 11471 bfd_set_symtab (implib_bfd, sympp, symcount); 11472 11473 /* Allow the BFD backend to copy any private data it understands 11474 from the output BFD to the import library BFD. This is done last 11475 to permit the routine to look at the filtered symbol table. */ 11476 if (! bfd_copy_private_bfd_data (abfd, implib_bfd)) 11477 goto free_sym_buf; 11478 11479 if (!bfd_close (implib_bfd)) 11480 goto free_sym_buf; 11481 11482 ret = TRUE; 11483 11484 free_sym_buf: 11485 free (sympp); 11486 return ret; 11487 } 11488 11489 static void 11490 elf_final_link_free (bfd *obfd, struct elf_final_link_info *flinfo) 11491 { 11492 asection *o; 11493 11494 if (flinfo->symstrtab != NULL) 11495 _bfd_elf_strtab_free (flinfo->symstrtab); 11496 if (flinfo->contents != NULL) 11497 free (flinfo->contents); 11498 if (flinfo->external_relocs != NULL) 11499 free (flinfo->external_relocs); 11500 if (flinfo->internal_relocs != NULL) 11501 free (flinfo->internal_relocs); 11502 if (flinfo->external_syms != NULL) 11503 free (flinfo->external_syms); 11504 if (flinfo->locsym_shndx != NULL) 11505 free (flinfo->locsym_shndx); 11506 if (flinfo->internal_syms != NULL) 11507 free (flinfo->internal_syms); 11508 if (flinfo->indices != NULL) 11509 free (flinfo->indices); 11510 if (flinfo->sections != NULL) 11511 free (flinfo->sections); 11512 if (flinfo->symshndxbuf != NULL) 11513 free (flinfo->symshndxbuf); 11514 for (o = obfd->sections; o != NULL; o = o->next) 11515 { 11516 struct bfd_elf_section_data *esdo = elf_section_data (o); 11517 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) 11518 free (esdo->rel.hashes); 11519 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) 11520 free (esdo->rela.hashes); 11521 } 11522 } 11523 11524 /* Do the final step of an ELF link. */ 11525 11526 bfd_boolean 11527 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) 11528 { 11529 bfd_boolean dynamic; 11530 bfd_boolean emit_relocs; 11531 bfd *dynobj; 11532 struct elf_final_link_info flinfo; 11533 asection *o; 11534 struct bfd_link_order *p; 11535 bfd *sub; 11536 bfd_size_type max_contents_size; 11537 bfd_size_type max_external_reloc_size; 11538 bfd_size_type max_internal_reloc_count; 11539 bfd_size_type max_sym_count; 11540 bfd_size_type max_sym_shndx_count; 11541 Elf_Internal_Sym elfsym; 11542 unsigned int i; 11543 Elf_Internal_Shdr *symtab_hdr; 11544 Elf_Internal_Shdr *symtab_shndx_hdr; 11545 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11546 struct elf_outext_info eoinfo; 11547 bfd_boolean merged; 11548 size_t relativecount = 0; 11549 asection *reldyn = 0; 11550 bfd_size_type amt; 11551 asection *attr_section = NULL; 11552 bfd_vma attr_size = 0; 11553 const char *std_attrs_section; 11554 struct elf_link_hash_table *htab = elf_hash_table (info); 11555 11556 if (!is_elf_hash_table (htab)) 11557 return FALSE; 11558 11559 if (bfd_link_pic (info)) 11560 abfd->flags |= DYNAMIC; 11561 11562 dynamic = htab->dynamic_sections_created; 11563 dynobj = htab->dynobj; 11564 11565 emit_relocs = (bfd_link_relocatable (info) 11566 || info->emitrelocations); 11567 11568 flinfo.info = info; 11569 flinfo.output_bfd = abfd; 11570 flinfo.symstrtab = _bfd_elf_strtab_init (); 11571 if (flinfo.symstrtab == NULL) 11572 return FALSE; 11573 11574 if (! dynamic) 11575 { 11576 flinfo.hash_sec = NULL; 11577 flinfo.symver_sec = NULL; 11578 } 11579 else 11580 { 11581 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); 11582 /* Note that dynsym_sec can be NULL (on VMS). */ 11583 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); 11584 /* Note that it is OK if symver_sec is NULL. */ 11585 } 11586 11587 flinfo.contents = NULL; 11588 flinfo.external_relocs = NULL; 11589 flinfo.internal_relocs = NULL; 11590 flinfo.external_syms = NULL; 11591 flinfo.locsym_shndx = NULL; 11592 flinfo.internal_syms = NULL; 11593 flinfo.indices = NULL; 11594 flinfo.sections = NULL; 11595 flinfo.symshndxbuf = NULL; 11596 flinfo.filesym_count = 0; 11597 11598 /* The object attributes have been merged. Remove the input 11599 sections from the link, and set the contents of the output 11600 secton. */ 11601 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; 11602 for (o = abfd->sections; o != NULL; o = o->next) 11603 { 11604 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) 11605 || strcmp (o->name, ".gnu.attributes") == 0) 11606 { 11607 for (p = o->map_head.link_order; p != NULL; p = p->next) 11608 { 11609 asection *input_section; 11610 11611 if (p->type != bfd_indirect_link_order) 11612 continue; 11613 input_section = p->u.indirect.section; 11614 /* Hack: reset the SEC_HAS_CONTENTS flag so that 11615 elf_link_input_bfd ignores this section. */ 11616 input_section->flags &= ~SEC_HAS_CONTENTS; 11617 } 11618 11619 attr_size = bfd_elf_obj_attr_size (abfd); 11620 if (attr_size) 11621 { 11622 bfd_set_section_size (abfd, o, attr_size); 11623 attr_section = o; 11624 /* Skip this section later on. */ 11625 o->map_head.link_order = NULL; 11626 } 11627 else 11628 o->flags |= SEC_EXCLUDE; 11629 } 11630 } 11631 11632 /* Count up the number of relocations we will output for each output 11633 section, so that we know the sizes of the reloc sections. We 11634 also figure out some maximum sizes. */ 11635 max_contents_size = 0; 11636 max_external_reloc_size = 0; 11637 max_internal_reloc_count = 0; 11638 max_sym_count = 0; 11639 max_sym_shndx_count = 0; 11640 merged = FALSE; 11641 for (o = abfd->sections; o != NULL; o = o->next) 11642 { 11643 struct bfd_elf_section_data *esdo = elf_section_data (o); 11644 o->reloc_count = 0; 11645 11646 for (p = o->map_head.link_order; p != NULL; p = p->next) 11647 { 11648 unsigned int reloc_count = 0; 11649 unsigned int additional_reloc_count = 0; 11650 struct bfd_elf_section_data *esdi = NULL; 11651 11652 if (p->type == bfd_section_reloc_link_order 11653 || p->type == bfd_symbol_reloc_link_order) 11654 reloc_count = 1; 11655 else if (p->type == bfd_indirect_link_order) 11656 { 11657 asection *sec; 11658 11659 sec = p->u.indirect.section; 11660 11661 /* Mark all sections which are to be included in the 11662 link. This will normally be every section. We need 11663 to do this so that we can identify any sections which 11664 the linker has decided to not include. */ 11665 sec->linker_mark = TRUE; 11666 11667 if (sec->flags & SEC_MERGE) 11668 merged = TRUE; 11669 11670 if (sec->rawsize > max_contents_size) 11671 max_contents_size = sec->rawsize; 11672 if (sec->size > max_contents_size) 11673 max_contents_size = sec->size; 11674 11675 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour 11676 && (sec->owner->flags & DYNAMIC) == 0) 11677 { 11678 size_t sym_count; 11679 11680 /* We are interested in just local symbols, not all 11681 symbols. */ 11682 if (elf_bad_symtab (sec->owner)) 11683 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size 11684 / bed->s->sizeof_sym); 11685 else 11686 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; 11687 11688 if (sym_count > max_sym_count) 11689 max_sym_count = sym_count; 11690 11691 if (sym_count > max_sym_shndx_count 11692 && elf_symtab_shndx_list (sec->owner) != NULL) 11693 max_sym_shndx_count = sym_count; 11694 11695 if (esdo->this_hdr.sh_type == SHT_REL 11696 || esdo->this_hdr.sh_type == SHT_RELA) 11697 /* Some backends use reloc_count in relocation sections 11698 to count particular types of relocs. Of course, 11699 reloc sections themselves can't have relocations. */ 11700 ; 11701 else if (emit_relocs) 11702 { 11703 reloc_count = sec->reloc_count; 11704 if (bed->elf_backend_count_additional_relocs) 11705 { 11706 int c; 11707 c = (*bed->elf_backend_count_additional_relocs) (sec); 11708 additional_reloc_count += c; 11709 } 11710 } 11711 else if (bed->elf_backend_count_relocs) 11712 reloc_count = (*bed->elf_backend_count_relocs) (info, sec); 11713 11714 esdi = elf_section_data (sec); 11715 11716 if ((sec->flags & SEC_RELOC) != 0) 11717 { 11718 size_t ext_size = 0; 11719 11720 if (esdi->rel.hdr != NULL) 11721 ext_size = esdi->rel.hdr->sh_size; 11722 if (esdi->rela.hdr != NULL) 11723 ext_size += esdi->rela.hdr->sh_size; 11724 11725 if (ext_size > max_external_reloc_size) 11726 max_external_reloc_size = ext_size; 11727 if (sec->reloc_count > max_internal_reloc_count) 11728 max_internal_reloc_count = sec->reloc_count; 11729 } 11730 } 11731 } 11732 11733 if (reloc_count == 0) 11734 continue; 11735 11736 reloc_count += additional_reloc_count; 11737 o->reloc_count += reloc_count; 11738 11739 if (p->type == bfd_indirect_link_order && emit_relocs) 11740 { 11741 if (esdi->rel.hdr) 11742 { 11743 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); 11744 esdo->rel.count += additional_reloc_count; 11745 } 11746 if (esdi->rela.hdr) 11747 { 11748 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); 11749 esdo->rela.count += additional_reloc_count; 11750 } 11751 } 11752 else 11753 { 11754 if (o->use_rela_p) 11755 esdo->rela.count += reloc_count; 11756 else 11757 esdo->rel.count += reloc_count; 11758 } 11759 } 11760 11761 if (o->reloc_count > 0) 11762 o->flags |= SEC_RELOC; 11763 else 11764 { 11765 /* Explicitly clear the SEC_RELOC flag. The linker tends to 11766 set it (this is probably a bug) and if it is set 11767 assign_section_numbers will create a reloc section. */ 11768 o->flags &=~ SEC_RELOC; 11769 } 11770 11771 /* If the SEC_ALLOC flag is not set, force the section VMA to 11772 zero. This is done in elf_fake_sections as well, but forcing 11773 the VMA to 0 here will ensure that relocs against these 11774 sections are handled correctly. */ 11775 if ((o->flags & SEC_ALLOC) == 0 11776 && ! o->user_set_vma) 11777 o->vma = 0; 11778 } 11779 11780 if (! bfd_link_relocatable (info) && merged) 11781 elf_link_hash_traverse (htab, _bfd_elf_link_sec_merge_syms, abfd); 11782 11783 /* Figure out the file positions for everything but the symbol table 11784 and the relocs. We set symcount to force assign_section_numbers 11785 to create a symbol table. */ 11786 bfd_get_symcount (abfd) = info->strip != strip_all || emit_relocs; 11787 BFD_ASSERT (! abfd->output_has_begun); 11788 if (! _bfd_elf_compute_section_file_positions (abfd, info)) 11789 goto error_return; 11790 11791 /* Set sizes, and assign file positions for reloc sections. */ 11792 for (o = abfd->sections; o != NULL; o = o->next) 11793 { 11794 struct bfd_elf_section_data *esdo = elf_section_data (o); 11795 if ((o->flags & SEC_RELOC) != 0) 11796 { 11797 if (esdo->rel.hdr 11798 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) 11799 goto error_return; 11800 11801 if (esdo->rela.hdr 11802 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) 11803 goto error_return; 11804 } 11805 11806 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them 11807 to count upwards while actually outputting the relocations. */ 11808 esdo->rel.count = 0; 11809 esdo->rela.count = 0; 11810 11811 if (esdo->this_hdr.sh_offset == (file_ptr) -1) 11812 { 11813 /* Cache the section contents so that they can be compressed 11814 later. Use bfd_malloc since it will be freed by 11815 bfd_compress_section_contents. */ 11816 unsigned char *contents = esdo->this_hdr.contents; 11817 if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL) 11818 abort (); 11819 contents 11820 = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size); 11821 if (contents == NULL) 11822 goto error_return; 11823 esdo->this_hdr.contents = contents; 11824 } 11825 } 11826 11827 /* We have now assigned file positions for all the sections except 11828 .symtab, .strtab, and non-loaded reloc sections. We start the 11829 .symtab section at the current file position, and write directly 11830 to it. We build the .strtab section in memory. */ 11831 bfd_get_symcount (abfd) = 0; 11832 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 11833 /* sh_name is set in prep_headers. */ 11834 symtab_hdr->sh_type = SHT_SYMTAB; 11835 /* sh_flags, sh_addr and sh_size all start off zero. */ 11836 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 11837 /* sh_link is set in assign_section_numbers. */ 11838 /* sh_info is set below. */ 11839 /* sh_offset is set just below. */ 11840 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 11841 11842 if (max_sym_count < 20) 11843 max_sym_count = 20; 11844 htab->strtabsize = max_sym_count; 11845 amt = max_sym_count * sizeof (struct elf_sym_strtab); 11846 htab->strtab = (struct elf_sym_strtab *) bfd_malloc (amt); 11847 if (htab->strtab == NULL) 11848 goto error_return; 11849 /* The real buffer will be allocated in elf_link_swap_symbols_out. */ 11850 flinfo.symshndxbuf 11851 = (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF) 11852 ? (Elf_External_Sym_Shndx *) -1 : NULL); 11853 11854 if (info->strip != strip_all || emit_relocs) 11855 { 11856 file_ptr off = elf_next_file_pos (abfd); 11857 11858 _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); 11859 11860 /* Note that at this point elf_next_file_pos (abfd) is 11861 incorrect. We do not yet know the size of the .symtab section. 11862 We correct next_file_pos below, after we do know the size. */ 11863 11864 /* Start writing out the symbol table. The first symbol is always a 11865 dummy symbol. */ 11866 elfsym.st_value = 0; 11867 elfsym.st_size = 0; 11868 elfsym.st_info = 0; 11869 elfsym.st_other = 0; 11870 elfsym.st_shndx = SHN_UNDEF; 11871 elfsym.st_target_internal = 0; 11872 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, 11873 bfd_und_section_ptr, NULL) != 1) 11874 goto error_return; 11875 11876 /* Output a symbol for each section. We output these even if we are 11877 discarding local symbols, since they are used for relocs. These 11878 symbols have no names. We store the index of each one in the 11879 index field of the section, so that we can find it again when 11880 outputting relocs. */ 11881 11882 elfsym.st_size = 0; 11883 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 11884 elfsym.st_other = 0; 11885 elfsym.st_value = 0; 11886 elfsym.st_target_internal = 0; 11887 for (i = 1; i < elf_numsections (abfd); i++) 11888 { 11889 o = bfd_section_from_elf_index (abfd, i); 11890 if (o != NULL) 11891 { 11892 o->target_index = bfd_get_symcount (abfd); 11893 elfsym.st_shndx = i; 11894 if (!bfd_link_relocatable (info)) 11895 elfsym.st_value = o->vma; 11896 if (elf_link_output_symstrtab (&flinfo, NULL, &elfsym, o, 11897 NULL) != 1) 11898 goto error_return; 11899 } 11900 } 11901 } 11902 11903 /* Allocate some memory to hold information read in from the input 11904 files. */ 11905 if (max_contents_size != 0) 11906 { 11907 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); 11908 if (flinfo.contents == NULL) 11909 goto error_return; 11910 } 11911 11912 if (max_external_reloc_size != 0) 11913 { 11914 flinfo.external_relocs = bfd_malloc (max_external_reloc_size); 11915 if (flinfo.external_relocs == NULL) 11916 goto error_return; 11917 } 11918 11919 if (max_internal_reloc_count != 0) 11920 { 11921 amt = max_internal_reloc_count * sizeof (Elf_Internal_Rela); 11922 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); 11923 if (flinfo.internal_relocs == NULL) 11924 goto error_return; 11925 } 11926 11927 if (max_sym_count != 0) 11928 { 11929 amt = max_sym_count * bed->s->sizeof_sym; 11930 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); 11931 if (flinfo.external_syms == NULL) 11932 goto error_return; 11933 11934 amt = max_sym_count * sizeof (Elf_Internal_Sym); 11935 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); 11936 if (flinfo.internal_syms == NULL) 11937 goto error_return; 11938 11939 amt = max_sym_count * sizeof (long); 11940 flinfo.indices = (long int *) bfd_malloc (amt); 11941 if (flinfo.indices == NULL) 11942 goto error_return; 11943 11944 amt = max_sym_count * sizeof (asection *); 11945 flinfo.sections = (asection **) bfd_malloc (amt); 11946 if (flinfo.sections == NULL) 11947 goto error_return; 11948 } 11949 11950 if (max_sym_shndx_count != 0) 11951 { 11952 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); 11953 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 11954 if (flinfo.locsym_shndx == NULL) 11955 goto error_return; 11956 } 11957 11958 if (htab->tls_sec) 11959 { 11960 bfd_vma base, end = 0; 11961 asection *sec; 11962 11963 for (sec = htab->tls_sec; 11964 sec && (sec->flags & SEC_THREAD_LOCAL); 11965 sec = sec->next) 11966 { 11967 bfd_size_type size = sec->size; 11968 11969 if (size == 0 11970 && (sec->flags & SEC_HAS_CONTENTS) == 0) 11971 { 11972 struct bfd_link_order *ord = sec->map_tail.link_order; 11973 11974 if (ord != NULL) 11975 size = ord->offset + ord->size; 11976 } 11977 end = sec->vma + size; 11978 } 11979 base = htab->tls_sec->vma; 11980 /* Only align end of TLS section if static TLS doesn't have special 11981 alignment requirements. */ 11982 if (bed->static_tls_alignment == 1) 11983 end = align_power (end, htab->tls_sec->alignment_power); 11984 htab->tls_size = end - base; 11985 } 11986 11987 /* Reorder SHF_LINK_ORDER sections. */ 11988 for (o = abfd->sections; o != NULL; o = o->next) 11989 { 11990 if (!elf_fixup_link_order (abfd, o)) 11991 return FALSE; 11992 } 11993 11994 if (!_bfd_elf_fixup_eh_frame_hdr (info)) 11995 return FALSE; 11996 11997 /* Since ELF permits relocations to be against local symbols, we 11998 must have the local symbols available when we do the relocations. 11999 Since we would rather only read the local symbols once, and we 12000 would rather not keep them in memory, we handle all the 12001 relocations for a single input file at the same time. 12002 12003 Unfortunately, there is no way to know the total number of local 12004 symbols until we have seen all of them, and the local symbol 12005 indices precede the global symbol indices. This means that when 12006 we are generating relocatable output, and we see a reloc against 12007 a global symbol, we can not know the symbol index until we have 12008 finished examining all the local symbols to see which ones we are 12009 going to output. To deal with this, we keep the relocations in 12010 memory, and don't output them until the end of the link. This is 12011 an unfortunate waste of memory, but I don't see a good way around 12012 it. Fortunately, it only happens when performing a relocatable 12013 link, which is not the common case. FIXME: If keep_memory is set 12014 we could write the relocs out and then read them again; I don't 12015 know how bad the memory loss will be. */ 12016 12017 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 12018 sub->output_has_begun = FALSE; 12019 for (o = abfd->sections; o != NULL; o = o->next) 12020 { 12021 for (p = o->map_head.link_order; p != NULL; p = p->next) 12022 { 12023 if (p->type == bfd_indirect_link_order 12024 && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) 12025 == bfd_target_elf_flavour) 12026 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) 12027 { 12028 if (! sub->output_has_begun) 12029 { 12030 if (! elf_link_input_bfd (&flinfo, sub)) 12031 goto error_return; 12032 sub->output_has_begun = TRUE; 12033 } 12034 } 12035 else if (p->type == bfd_section_reloc_link_order 12036 || p->type == bfd_symbol_reloc_link_order) 12037 { 12038 if (! elf_reloc_link_order (abfd, info, o, p)) 12039 goto error_return; 12040 } 12041 else 12042 { 12043 if (! _bfd_default_link_order (abfd, info, o, p)) 12044 { 12045 if (p->type == bfd_indirect_link_order 12046 && (bfd_get_flavour (sub) 12047 == bfd_target_elf_flavour) 12048 && (elf_elfheader (sub)->e_ident[EI_CLASS] 12049 != bed->s->elfclass)) 12050 { 12051 const char *iclass, *oclass; 12052 12053 switch (bed->s->elfclass) 12054 { 12055 case ELFCLASS64: oclass = "ELFCLASS64"; break; 12056 case ELFCLASS32: oclass = "ELFCLASS32"; break; 12057 case ELFCLASSNONE: oclass = "ELFCLASSNONE"; break; 12058 default: abort (); 12059 } 12060 12061 switch (elf_elfheader (sub)->e_ident[EI_CLASS]) 12062 { 12063 case ELFCLASS64: iclass = "ELFCLASS64"; break; 12064 case ELFCLASS32: iclass = "ELFCLASS32"; break; 12065 case ELFCLASSNONE: iclass = "ELFCLASSNONE"; break; 12066 default: abort (); 12067 } 12068 12069 bfd_set_error (bfd_error_wrong_format); 12070 _bfd_error_handler 12071 /* xgettext:c-format */ 12072 (_("%B: file class %s incompatible with %s"), 12073 sub, iclass, oclass); 12074 } 12075 12076 goto error_return; 12077 } 12078 } 12079 } 12080 } 12081 12082 /* Free symbol buffer if needed. */ 12083 if (!info->reduce_memory_overheads) 12084 { 12085 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 12086 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 12087 && elf_tdata (sub)->symbuf) 12088 { 12089 free (elf_tdata (sub)->symbuf); 12090 elf_tdata (sub)->symbuf = NULL; 12091 } 12092 } 12093 12094 /* Output any global symbols that got converted to local in a 12095 version script or due to symbol visibility. We do this in a 12096 separate step since ELF requires all local symbols to appear 12097 prior to any global symbols. FIXME: We should only do this if 12098 some global symbols were, in fact, converted to become local. 12099 FIXME: Will this work correctly with the Irix 5 linker? */ 12100 eoinfo.failed = FALSE; 12101 eoinfo.flinfo = &flinfo; 12102 eoinfo.localsyms = TRUE; 12103 eoinfo.file_sym_done = FALSE; 12104 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 12105 if (eoinfo.failed) 12106 return FALSE; 12107 12108 /* If backend needs to output some local symbols not present in the hash 12109 table, do it now. */ 12110 if (bed->elf_backend_output_arch_local_syms 12111 && (info->strip != strip_all || emit_relocs)) 12112 { 12113 typedef int (*out_sym_func) 12114 (void *, const char *, Elf_Internal_Sym *, asection *, 12115 struct elf_link_hash_entry *); 12116 12117 if (! ((*bed->elf_backend_output_arch_local_syms) 12118 (abfd, info, &flinfo, 12119 (out_sym_func) elf_link_output_symstrtab))) 12120 return FALSE; 12121 } 12122 12123 /* That wrote out all the local symbols. Finish up the symbol table 12124 with the global symbols. Even if we want to strip everything we 12125 can, we still need to deal with those global symbols that got 12126 converted to local in a version script. */ 12127 12128 /* The sh_info field records the index of the first non local symbol. */ 12129 symtab_hdr->sh_info = bfd_get_symcount (abfd); 12130 12131 if (dynamic 12132 && htab->dynsym != NULL 12133 && htab->dynsym->output_section != bfd_abs_section_ptr) 12134 { 12135 Elf_Internal_Sym sym; 12136 bfd_byte *dynsym = htab->dynsym->contents; 12137 12138 o = htab->dynsym->output_section; 12139 elf_section_data (o)->this_hdr.sh_info = htab->local_dynsymcount + 1; 12140 12141 /* Write out the section symbols for the output sections. */ 12142 if (bfd_link_pic (info) 12143 || htab->is_relocatable_executable) 12144 { 12145 asection *s; 12146 12147 sym.st_size = 0; 12148 sym.st_name = 0; 12149 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 12150 sym.st_other = 0; 12151 sym.st_target_internal = 0; 12152 12153 for (s = abfd->sections; s != NULL; s = s->next) 12154 { 12155 int indx; 12156 bfd_byte *dest; 12157 long dynindx; 12158 12159 dynindx = elf_section_data (s)->dynindx; 12160 if (dynindx <= 0) 12161 continue; 12162 indx = elf_section_data (s)->this_idx; 12163 BFD_ASSERT (indx > 0); 12164 sym.st_shndx = indx; 12165 if (! check_dynsym (abfd, &sym)) 12166 return FALSE; 12167 sym.st_value = s->vma; 12168 dest = dynsym + dynindx * bed->s->sizeof_sym; 12169 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 12170 } 12171 } 12172 12173 /* Write out the local dynsyms. */ 12174 if (htab->dynlocal) 12175 { 12176 struct elf_link_local_dynamic_entry *e; 12177 for (e = htab->dynlocal; e ; e = e->next) 12178 { 12179 asection *s; 12180 bfd_byte *dest; 12181 12182 /* Copy the internal symbol and turn off visibility. 12183 Note that we saved a word of storage and overwrote 12184 the original st_name with the dynstr_index. */ 12185 sym = e->isym; 12186 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 12187 12188 s = bfd_section_from_elf_index (e->input_bfd, 12189 e->isym.st_shndx); 12190 if (s != NULL) 12191 { 12192 sym.st_shndx = 12193 elf_section_data (s->output_section)->this_idx; 12194 if (! check_dynsym (abfd, &sym)) 12195 return FALSE; 12196 sym.st_value = (s->output_section->vma 12197 + s->output_offset 12198 + e->isym.st_value); 12199 } 12200 12201 dest = dynsym + e->dynindx * bed->s->sizeof_sym; 12202 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 12203 } 12204 } 12205 } 12206 12207 /* We get the global symbols from the hash table. */ 12208 eoinfo.failed = FALSE; 12209 eoinfo.localsyms = FALSE; 12210 eoinfo.flinfo = &flinfo; 12211 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 12212 if (eoinfo.failed) 12213 return FALSE; 12214 12215 /* If backend needs to output some symbols not present in the hash 12216 table, do it now. */ 12217 if (bed->elf_backend_output_arch_syms 12218 && (info->strip != strip_all || emit_relocs)) 12219 { 12220 typedef int (*out_sym_func) 12221 (void *, const char *, Elf_Internal_Sym *, asection *, 12222 struct elf_link_hash_entry *); 12223 12224 if (! ((*bed->elf_backend_output_arch_syms) 12225 (abfd, info, &flinfo, 12226 (out_sym_func) elf_link_output_symstrtab))) 12227 return FALSE; 12228 } 12229 12230 /* Finalize the .strtab section. */ 12231 _bfd_elf_strtab_finalize (flinfo.symstrtab); 12232 12233 /* Swap out the .strtab section. */ 12234 if (!elf_link_swap_symbols_out (&flinfo)) 12235 return FALSE; 12236 12237 /* Now we know the size of the symtab section. */ 12238 if (bfd_get_symcount (abfd) > 0) 12239 { 12240 /* Finish up and write out the symbol string table (.strtab) 12241 section. */ 12242 Elf_Internal_Shdr *symstrtab_hdr = NULL; 12243 file_ptr off = symtab_hdr->sh_offset + symtab_hdr->sh_size; 12244 12245 if (elf_symtab_shndx_list (abfd)) 12246 { 12247 symtab_shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr; 12248 12249 if (symtab_shndx_hdr != NULL && symtab_shndx_hdr->sh_name != 0) 12250 { 12251 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 12252 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 12253 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 12254 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); 12255 symtab_shndx_hdr->sh_size = amt; 12256 12257 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, 12258 off, TRUE); 12259 12260 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 12261 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) 12262 return FALSE; 12263 } 12264 } 12265 12266 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 12267 /* sh_name was set in prep_headers. */ 12268 symstrtab_hdr->sh_type = SHT_STRTAB; 12269 symstrtab_hdr->sh_flags = bed->elf_strtab_flags; 12270 symstrtab_hdr->sh_addr = 0; 12271 symstrtab_hdr->sh_size = _bfd_elf_strtab_size (flinfo.symstrtab); 12272 symstrtab_hdr->sh_entsize = 0; 12273 symstrtab_hdr->sh_link = 0; 12274 symstrtab_hdr->sh_info = 0; 12275 /* sh_offset is set just below. */ 12276 symstrtab_hdr->sh_addralign = 1; 12277 12278 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, 12279 off, TRUE); 12280 elf_next_file_pos (abfd) = off; 12281 12282 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 12283 || ! _bfd_elf_strtab_emit (abfd, flinfo.symstrtab)) 12284 return FALSE; 12285 } 12286 12287 if (info->out_implib_bfd && !elf_output_implib (abfd, info)) 12288 { 12289 _bfd_error_handler (_("%B: failed to generate import library"), 12290 info->out_implib_bfd); 12291 return FALSE; 12292 } 12293 12294 /* Adjust the relocs to have the correct symbol indices. */ 12295 for (o = abfd->sections; o != NULL; o = o->next) 12296 { 12297 struct bfd_elf_section_data *esdo = elf_section_data (o); 12298 bfd_boolean sort; 12299 12300 if ((o->flags & SEC_RELOC) == 0) 12301 continue; 12302 12303 sort = bed->sort_relocs_p == NULL || (*bed->sort_relocs_p) (o); 12304 if (esdo->rel.hdr != NULL 12305 && !elf_link_adjust_relocs (abfd, o, &esdo->rel, sort, info)) 12306 return FALSE; 12307 if (esdo->rela.hdr != NULL 12308 && !elf_link_adjust_relocs (abfd, o, &esdo->rela, sort, info)) 12309 return FALSE; 12310 12311 /* Set the reloc_count field to 0 to prevent write_relocs from 12312 trying to swap the relocs out itself. */ 12313 o->reloc_count = 0; 12314 } 12315 12316 if (dynamic && info->combreloc && dynobj != NULL) 12317 relativecount = elf_link_sort_relocs (abfd, info, &reldyn); 12318 12319 /* If we are linking against a dynamic object, or generating a 12320 shared library, finish up the dynamic linking information. */ 12321 if (dynamic) 12322 { 12323 bfd_byte *dyncon, *dynconend; 12324 12325 /* Fix up .dynamic entries. */ 12326 o = bfd_get_linker_section (dynobj, ".dynamic"); 12327 BFD_ASSERT (o != NULL); 12328 12329 dyncon = o->contents; 12330 dynconend = o->contents + o->size; 12331 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 12332 { 12333 Elf_Internal_Dyn dyn; 12334 const char *name; 12335 unsigned int type; 12336 bfd_size_type sh_size; 12337 bfd_vma sh_addr; 12338 12339 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 12340 12341 switch (dyn.d_tag) 12342 { 12343 default: 12344 continue; 12345 case DT_NULL: 12346 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) 12347 { 12348 switch (elf_section_data (reldyn)->this_hdr.sh_type) 12349 { 12350 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; 12351 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; 12352 default: continue; 12353 } 12354 dyn.d_un.d_val = relativecount; 12355 relativecount = 0; 12356 break; 12357 } 12358 continue; 12359 12360 case DT_INIT: 12361 name = info->init_function; 12362 goto get_sym; 12363 case DT_FINI: 12364 name = info->fini_function; 12365 get_sym: 12366 { 12367 struct elf_link_hash_entry *h; 12368 12369 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 12370 if (h != NULL 12371 && (h->root.type == bfd_link_hash_defined 12372 || h->root.type == bfd_link_hash_defweak)) 12373 { 12374 dyn.d_un.d_ptr = h->root.u.def.value; 12375 o = h->root.u.def.section; 12376 if (o->output_section != NULL) 12377 dyn.d_un.d_ptr += (o->output_section->vma 12378 + o->output_offset); 12379 else 12380 { 12381 /* The symbol is imported from another shared 12382 library and does not apply to this one. */ 12383 dyn.d_un.d_ptr = 0; 12384 } 12385 break; 12386 } 12387 } 12388 continue; 12389 12390 case DT_PREINIT_ARRAYSZ: 12391 name = ".preinit_array"; 12392 goto get_out_size; 12393 case DT_INIT_ARRAYSZ: 12394 name = ".init_array"; 12395 goto get_out_size; 12396 case DT_FINI_ARRAYSZ: 12397 name = ".fini_array"; 12398 get_out_size: 12399 o = bfd_get_section_by_name (abfd, name); 12400 if (o == NULL) 12401 { 12402 _bfd_error_handler 12403 (_("could not find section %s"), name); 12404 goto error_return; 12405 } 12406 if (o->size == 0) 12407 _bfd_error_handler 12408 (_("warning: %s section has zero size"), name); 12409 dyn.d_un.d_val = o->size; 12410 break; 12411 12412 case DT_PREINIT_ARRAY: 12413 name = ".preinit_array"; 12414 goto get_out_vma; 12415 case DT_INIT_ARRAY: 12416 name = ".init_array"; 12417 goto get_out_vma; 12418 case DT_FINI_ARRAY: 12419 name = ".fini_array"; 12420 get_out_vma: 12421 o = bfd_get_section_by_name (abfd, name); 12422 goto do_vma; 12423 12424 case DT_HASH: 12425 name = ".hash"; 12426 goto get_vma; 12427 case DT_GNU_HASH: 12428 name = ".gnu.hash"; 12429 goto get_vma; 12430 case DT_STRTAB: 12431 name = ".dynstr"; 12432 goto get_vma; 12433 case DT_SYMTAB: 12434 name = ".dynsym"; 12435 goto get_vma; 12436 case DT_VERDEF: 12437 name = ".gnu.version_d"; 12438 goto get_vma; 12439 case DT_VERNEED: 12440 name = ".gnu.version_r"; 12441 goto get_vma; 12442 case DT_VERSYM: 12443 name = ".gnu.version"; 12444 get_vma: 12445 o = bfd_get_linker_section (dynobj, name); 12446 do_vma: 12447 if (o == NULL || bfd_is_abs_section (o->output_section)) 12448 { 12449 _bfd_error_handler 12450 (_("could not find section %s"), name); 12451 goto error_return; 12452 } 12453 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) 12454 { 12455 _bfd_error_handler 12456 (_("warning: section '%s' is being made into a note"), name); 12457 bfd_set_error (bfd_error_nonrepresentable_section); 12458 goto error_return; 12459 } 12460 dyn.d_un.d_ptr = o->output_section->vma + o->output_offset; 12461 break; 12462 12463 case DT_REL: 12464 case DT_RELA: 12465 case DT_RELSZ: 12466 case DT_RELASZ: 12467 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) 12468 type = SHT_REL; 12469 else 12470 type = SHT_RELA; 12471 sh_size = 0; 12472 sh_addr = 0; 12473 for (i = 1; i < elf_numsections (abfd); i++) 12474 { 12475 Elf_Internal_Shdr *hdr; 12476 12477 hdr = elf_elfsections (abfd)[i]; 12478 if (hdr->sh_type == type 12479 && (hdr->sh_flags & SHF_ALLOC) != 0) 12480 { 12481 sh_size += hdr->sh_size; 12482 if (sh_addr == 0 12483 || sh_addr > hdr->sh_addr) 12484 sh_addr = hdr->sh_addr; 12485 } 12486 } 12487 12488 if (bed->dtrel_excludes_plt && htab->srelplt != NULL) 12489 { 12490 /* Don't count procedure linkage table relocs in the 12491 overall reloc count. */ 12492 sh_size -= htab->srelplt->size; 12493 if (sh_size == 0) 12494 /* If the size is zero, make the address zero too. 12495 This is to avoid a glibc bug. If the backend 12496 emits DT_RELA/DT_RELASZ even when DT_RELASZ is 12497 zero, then we'll put DT_RELA at the end of 12498 DT_JMPREL. glibc will interpret the end of 12499 DT_RELA matching the end of DT_JMPREL as the 12500 case where DT_RELA includes DT_JMPREL, and for 12501 LD_BIND_NOW will decide that processing DT_RELA 12502 will process the PLT relocs too. Net result: 12503 No PLT relocs applied. */ 12504 sh_addr = 0; 12505 12506 /* If .rela.plt is the first .rela section, exclude 12507 it from DT_RELA. */ 12508 else if (sh_addr == (htab->srelplt->output_section->vma 12509 + htab->srelplt->output_offset)) 12510 sh_addr += htab->srelplt->size; 12511 } 12512 12513 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) 12514 dyn.d_un.d_val = sh_size; 12515 else 12516 dyn.d_un.d_ptr = sh_addr; 12517 break; 12518 } 12519 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 12520 } 12521 } 12522 12523 /* If we have created any dynamic sections, then output them. */ 12524 if (dynobj != NULL) 12525 { 12526 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) 12527 goto error_return; 12528 12529 /* Check for DT_TEXTREL (late, in case the backend removes it). */ 12530 if (((info->warn_shared_textrel && bfd_link_pic (info)) 12531 || info->error_textrel) 12532 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) 12533 { 12534 bfd_byte *dyncon, *dynconend; 12535 12536 dyncon = o->contents; 12537 dynconend = o->contents + o->size; 12538 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 12539 { 12540 Elf_Internal_Dyn dyn; 12541 12542 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 12543 12544 if (dyn.d_tag == DT_TEXTREL) 12545 { 12546 if (info->error_textrel) 12547 info->callbacks->einfo 12548 (_("%P%X: read-only segment has dynamic relocations.\n")); 12549 else 12550 info->callbacks->einfo 12551 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); 12552 break; 12553 } 12554 } 12555 } 12556 12557 for (o = dynobj->sections; o != NULL; o = o->next) 12558 { 12559 if ((o->flags & SEC_HAS_CONTENTS) == 0 12560 || o->size == 0 12561 || o->output_section == bfd_abs_section_ptr) 12562 continue; 12563 if ((o->flags & SEC_LINKER_CREATED) == 0) 12564 { 12565 /* At this point, we are only interested in sections 12566 created by _bfd_elf_link_create_dynamic_sections. */ 12567 continue; 12568 } 12569 if (htab->stab_info.stabstr == o) 12570 continue; 12571 if (htab->eh_info.hdr_sec == o) 12572 continue; 12573 if (strcmp (o->name, ".dynstr") != 0) 12574 { 12575 if (! bfd_set_section_contents (abfd, o->output_section, 12576 o->contents, 12577 (file_ptr) o->output_offset 12578 * bfd_octets_per_byte (abfd), 12579 o->size)) 12580 goto error_return; 12581 } 12582 else 12583 { 12584 /* The contents of the .dynstr section are actually in a 12585 stringtab. */ 12586 file_ptr off; 12587 12588 off = elf_section_data (o->output_section)->this_hdr.sh_offset; 12589 if (bfd_seek (abfd, off, SEEK_SET) != 0 12590 || !_bfd_elf_strtab_emit (abfd, htab->dynstr)) 12591 goto error_return; 12592 } 12593 } 12594 } 12595 12596 if (!info->resolve_section_groups) 12597 { 12598 bfd_boolean failed = FALSE; 12599 12600 BFD_ASSERT (bfd_link_relocatable (info)); 12601 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 12602 if (failed) 12603 goto error_return; 12604 } 12605 12606 /* If we have optimized stabs strings, output them. */ 12607 if (htab->stab_info.stabstr != NULL) 12608 { 12609 if (!_bfd_write_stab_strings (abfd, &htab->stab_info)) 12610 goto error_return; 12611 } 12612 12613 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) 12614 goto error_return; 12615 12616 elf_final_link_free (abfd, &flinfo); 12617 12618 elf_linker (abfd) = TRUE; 12619 12620 if (attr_section) 12621 { 12622 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); 12623 if (contents == NULL) 12624 return FALSE; /* Bail out and fail. */ 12625 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); 12626 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); 12627 free (contents); 12628 } 12629 12630 return TRUE; 12631 12632 error_return: 12633 elf_final_link_free (abfd, &flinfo); 12634 return FALSE; 12635 } 12636 12637 /* Initialize COOKIE for input bfd ABFD. */ 12638 12639 static bfd_boolean 12640 init_reloc_cookie (struct elf_reloc_cookie *cookie, 12641 struct bfd_link_info *info, bfd *abfd) 12642 { 12643 Elf_Internal_Shdr *symtab_hdr; 12644 const struct elf_backend_data *bed; 12645 12646 bed = get_elf_backend_data (abfd); 12647 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12648 12649 cookie->abfd = abfd; 12650 cookie->sym_hashes = elf_sym_hashes (abfd); 12651 cookie->bad_symtab = elf_bad_symtab (abfd); 12652 if (cookie->bad_symtab) 12653 { 12654 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 12655 cookie->extsymoff = 0; 12656 } 12657 else 12658 { 12659 cookie->locsymcount = symtab_hdr->sh_info; 12660 cookie->extsymoff = symtab_hdr->sh_info; 12661 } 12662 12663 if (bed->s->arch_size == 32) 12664 cookie->r_sym_shift = 8; 12665 else 12666 cookie->r_sym_shift = 32; 12667 12668 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; 12669 if (cookie->locsyms == NULL && cookie->locsymcount != 0) 12670 { 12671 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, 12672 cookie->locsymcount, 0, 12673 NULL, NULL, NULL); 12674 if (cookie->locsyms == NULL) 12675 { 12676 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); 12677 return FALSE; 12678 } 12679 if (info->keep_memory) 12680 symtab_hdr->contents = (bfd_byte *) cookie->locsyms; 12681 } 12682 return TRUE; 12683 } 12684 12685 /* Free the memory allocated by init_reloc_cookie, if appropriate. */ 12686 12687 static void 12688 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) 12689 { 12690 Elf_Internal_Shdr *symtab_hdr; 12691 12692 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 12693 if (cookie->locsyms != NULL 12694 && symtab_hdr->contents != (unsigned char *) cookie->locsyms) 12695 free (cookie->locsyms); 12696 } 12697 12698 /* Initialize the relocation information in COOKIE for input section SEC 12699 of input bfd ABFD. */ 12700 12701 static bfd_boolean 12702 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 12703 struct bfd_link_info *info, bfd *abfd, 12704 asection *sec) 12705 { 12706 if (sec->reloc_count == 0) 12707 { 12708 cookie->rels = NULL; 12709 cookie->relend = NULL; 12710 } 12711 else 12712 { 12713 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 12714 info->keep_memory); 12715 if (cookie->rels == NULL) 12716 return FALSE; 12717 cookie->rel = cookie->rels; 12718 cookie->relend = cookie->rels + sec->reloc_count; 12719 } 12720 cookie->rel = cookie->rels; 12721 return TRUE; 12722 } 12723 12724 /* Free the memory allocated by init_reloc_cookie_rels, 12725 if appropriate. */ 12726 12727 static void 12728 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 12729 asection *sec) 12730 { 12731 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) 12732 free (cookie->rels); 12733 } 12734 12735 /* Initialize the whole of COOKIE for input section SEC. */ 12736 12737 static bfd_boolean 12738 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 12739 struct bfd_link_info *info, 12740 asection *sec) 12741 { 12742 if (!init_reloc_cookie (cookie, info, sec->owner)) 12743 goto error1; 12744 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) 12745 goto error2; 12746 return TRUE; 12747 12748 error2: 12749 fini_reloc_cookie (cookie, sec->owner); 12750 error1: 12751 return FALSE; 12752 } 12753 12754 /* Free the memory allocated by init_reloc_cookie_for_section, 12755 if appropriate. */ 12756 12757 static void 12758 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 12759 asection *sec) 12760 { 12761 fini_reloc_cookie_rels (cookie, sec); 12762 fini_reloc_cookie (cookie, sec->owner); 12763 } 12764 12765 /* Garbage collect unused sections. */ 12766 12767 /* Default gc_mark_hook. */ 12768 12769 asection * 12770 _bfd_elf_gc_mark_hook (asection *sec, 12771 struct bfd_link_info *info ATTRIBUTE_UNUSED, 12772 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 12773 struct elf_link_hash_entry *h, 12774 Elf_Internal_Sym *sym) 12775 { 12776 if (h != NULL) 12777 { 12778 switch (h->root.type) 12779 { 12780 case bfd_link_hash_defined: 12781 case bfd_link_hash_defweak: 12782 return h->root.u.def.section; 12783 12784 case bfd_link_hash_common: 12785 return h->root.u.c.p->section; 12786 12787 default: 12788 break; 12789 } 12790 } 12791 else 12792 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 12793 12794 return NULL; 12795 } 12796 12797 /* Return the global debug definition section. */ 12798 12799 static asection * 12800 elf_gc_mark_debug_section (asection *sec ATTRIBUTE_UNUSED, 12801 struct bfd_link_info *info ATTRIBUTE_UNUSED, 12802 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 12803 struct elf_link_hash_entry *h, 12804 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED) 12805 { 12806 if (h != NULL 12807 && (h->root.type == bfd_link_hash_defined 12808 || h->root.type == bfd_link_hash_defweak) 12809 && (h->root.u.def.section->flags & SEC_DEBUGGING) != 0) 12810 return h->root.u.def.section; 12811 12812 return NULL; 12813 } 12814 12815 /* COOKIE->rel describes a relocation against section SEC, which is 12816 a section we've decided to keep. Return the section that contains 12817 the relocation symbol, or NULL if no section contains it. */ 12818 12819 asection * 12820 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, 12821 elf_gc_mark_hook_fn gc_mark_hook, 12822 struct elf_reloc_cookie *cookie, 12823 bfd_boolean *start_stop) 12824 { 12825 unsigned long r_symndx; 12826 struct elf_link_hash_entry *h; 12827 12828 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; 12829 if (r_symndx == STN_UNDEF) 12830 return NULL; 12831 12832 if (r_symndx >= cookie->locsymcount 12833 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 12834 { 12835 h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; 12836 if (h == NULL) 12837 { 12838 info->callbacks->einfo (_("%F%P: corrupt input: %B\n"), 12839 sec->owner); 12840 return NULL; 12841 } 12842 while (h->root.type == bfd_link_hash_indirect 12843 || h->root.type == bfd_link_hash_warning) 12844 h = (struct elf_link_hash_entry *) h->root.u.i.link; 12845 h->mark = 1; 12846 /* If this symbol is weak and there is a non-weak definition, we 12847 keep the non-weak definition because many backends put 12848 dynamic reloc info on the non-weak definition for code 12849 handling copy relocs. */ 12850 if (h->is_weakalias) 12851 weakdef (h)->mark = 1; 12852 12853 if (start_stop != NULL) 12854 { 12855 /* To work around a glibc bug, mark XXX input sections 12856 when there is a reference to __start_XXX or __stop_XXX 12857 symbols. */ 12858 if (h->start_stop) 12859 { 12860 asection *s = h->u2.start_stop_section; 12861 *start_stop = !s->gc_mark; 12862 return s; 12863 } 12864 } 12865 12866 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); 12867 } 12868 12869 return (*gc_mark_hook) (sec, info, cookie->rel, NULL, 12870 &cookie->locsyms[r_symndx]); 12871 } 12872 12873 /* COOKIE->rel describes a relocation against section SEC, which is 12874 a section we've decided to keep. Mark the section that contains 12875 the relocation symbol. */ 12876 12877 bfd_boolean 12878 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, 12879 asection *sec, 12880 elf_gc_mark_hook_fn gc_mark_hook, 12881 struct elf_reloc_cookie *cookie) 12882 { 12883 asection *rsec; 12884 bfd_boolean start_stop = FALSE; 12885 12886 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie, &start_stop); 12887 while (rsec != NULL) 12888 { 12889 if (!rsec->gc_mark) 12890 { 12891 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour 12892 || (rsec->owner->flags & DYNAMIC) != 0) 12893 rsec->gc_mark = 1; 12894 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) 12895 return FALSE; 12896 } 12897 if (!start_stop) 12898 break; 12899 rsec = bfd_get_next_section_by_name (rsec->owner, rsec); 12900 } 12901 return TRUE; 12902 } 12903 12904 /* The mark phase of garbage collection. For a given section, mark 12905 it and any sections in this section's group, and all the sections 12906 which define symbols to which it refers. */ 12907 12908 bfd_boolean 12909 _bfd_elf_gc_mark (struct bfd_link_info *info, 12910 asection *sec, 12911 elf_gc_mark_hook_fn gc_mark_hook) 12912 { 12913 bfd_boolean ret; 12914 asection *group_sec, *eh_frame; 12915 12916 sec->gc_mark = 1; 12917 12918 /* Mark all the sections in the group. */ 12919 group_sec = elf_section_data (sec)->next_in_group; 12920 if (group_sec && !group_sec->gc_mark) 12921 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) 12922 return FALSE; 12923 12924 /* Look through the section relocs. */ 12925 ret = TRUE; 12926 eh_frame = elf_eh_frame_section (sec->owner); 12927 if ((sec->flags & SEC_RELOC) != 0 12928 && sec->reloc_count > 0 12929 && sec != eh_frame) 12930 { 12931 struct elf_reloc_cookie cookie; 12932 12933 if (!init_reloc_cookie_for_section (&cookie, info, sec)) 12934 ret = FALSE; 12935 else 12936 { 12937 for (; cookie.rel < cookie.relend; cookie.rel++) 12938 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) 12939 { 12940 ret = FALSE; 12941 break; 12942 } 12943 fini_reloc_cookie_for_section (&cookie, sec); 12944 } 12945 } 12946 12947 if (ret && eh_frame && elf_fde_list (sec)) 12948 { 12949 struct elf_reloc_cookie cookie; 12950 12951 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) 12952 ret = FALSE; 12953 else 12954 { 12955 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, 12956 gc_mark_hook, &cookie)) 12957 ret = FALSE; 12958 fini_reloc_cookie_for_section (&cookie, eh_frame); 12959 } 12960 } 12961 12962 eh_frame = elf_section_eh_frame_entry (sec); 12963 if (ret && eh_frame && !eh_frame->gc_mark) 12964 if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook)) 12965 ret = FALSE; 12966 12967 return ret; 12968 } 12969 12970 /* Scan and mark sections in a special or debug section group. */ 12971 12972 static void 12973 _bfd_elf_gc_mark_debug_special_section_group (asection *grp) 12974 { 12975 /* Point to first section of section group. */ 12976 asection *ssec; 12977 /* Used to iterate the section group. */ 12978 asection *msec; 12979 12980 bfd_boolean is_special_grp = TRUE; 12981 bfd_boolean is_debug_grp = TRUE; 12982 12983 /* First scan to see if group contains any section other than debug 12984 and special section. */ 12985 ssec = msec = elf_next_in_group (grp); 12986 do 12987 { 12988 if ((msec->flags & SEC_DEBUGGING) == 0) 12989 is_debug_grp = FALSE; 12990 12991 if ((msec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) != 0) 12992 is_special_grp = FALSE; 12993 12994 msec = elf_next_in_group (msec); 12995 } 12996 while (msec != ssec); 12997 12998 /* If this is a pure debug section group or pure special section group, 12999 keep all sections in this group. */ 13000 if (is_debug_grp || is_special_grp) 13001 { 13002 do 13003 { 13004 msec->gc_mark = 1; 13005 msec = elf_next_in_group (msec); 13006 } 13007 while (msec != ssec); 13008 } 13009 } 13010 13011 /* Keep debug and special sections. */ 13012 13013 bfd_boolean 13014 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, 13015 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) 13016 { 13017 bfd *ibfd; 13018 13019 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 13020 { 13021 asection *isec; 13022 bfd_boolean some_kept; 13023 bfd_boolean debug_frag_seen; 13024 bfd_boolean has_kept_debug_info; 13025 13026 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 13027 continue; 13028 isec = ibfd->sections; 13029 if (isec == NULL || isec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13030 continue; 13031 13032 /* Ensure all linker created sections are kept, 13033 see if any other section is already marked, 13034 and note if we have any fragmented debug sections. */ 13035 debug_frag_seen = some_kept = has_kept_debug_info = FALSE; 13036 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13037 { 13038 if ((isec->flags & SEC_LINKER_CREATED) != 0) 13039 isec->gc_mark = 1; 13040 else if (isec->gc_mark 13041 && (isec->flags & SEC_ALLOC) != 0 13042 && elf_section_type (isec) != SHT_NOTE) 13043 some_kept = TRUE; 13044 13045 if (!debug_frag_seen 13046 && (isec->flags & SEC_DEBUGGING) 13047 && CONST_STRNEQ (isec->name, ".debug_line.")) 13048 debug_frag_seen = TRUE; 13049 } 13050 13051 /* If no non-note alloc section in this file will be kept, then 13052 we can toss out the debug and special sections. */ 13053 if (!some_kept) 13054 continue; 13055 13056 /* Keep debug and special sections like .comment when they are 13057 not part of a group. Also keep section groups that contain 13058 just debug sections or special sections. */ 13059 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13060 { 13061 if ((isec->flags & SEC_GROUP) != 0) 13062 _bfd_elf_gc_mark_debug_special_section_group (isec); 13063 else if (((isec->flags & SEC_DEBUGGING) != 0 13064 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0) 13065 && elf_next_in_group (isec) == NULL) 13066 isec->gc_mark = 1; 13067 if (isec->gc_mark && (isec->flags & SEC_DEBUGGING) != 0) 13068 has_kept_debug_info = TRUE; 13069 } 13070 13071 /* Look for CODE sections which are going to be discarded, 13072 and find and discard any fragmented debug sections which 13073 are associated with that code section. */ 13074 if (debug_frag_seen) 13075 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13076 if ((isec->flags & SEC_CODE) != 0 13077 && isec->gc_mark == 0) 13078 { 13079 unsigned int ilen; 13080 asection *dsec; 13081 13082 ilen = strlen (isec->name); 13083 13084 /* Association is determined by the name of the debug 13085 section containing the name of the code section as 13086 a suffix. For example .debug_line.text.foo is a 13087 debug section associated with .text.foo. */ 13088 for (dsec = ibfd->sections; dsec != NULL; dsec = dsec->next) 13089 { 13090 unsigned int dlen; 13091 13092 if (dsec->gc_mark == 0 13093 || (dsec->flags & SEC_DEBUGGING) == 0) 13094 continue; 13095 13096 dlen = strlen (dsec->name); 13097 13098 if (dlen > ilen 13099 && strncmp (dsec->name + (dlen - ilen), 13100 isec->name, ilen) == 0) 13101 dsec->gc_mark = 0; 13102 } 13103 } 13104 13105 /* Mark debug sections referenced by kept debug sections. */ 13106 if (has_kept_debug_info) 13107 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 13108 if (isec->gc_mark 13109 && (isec->flags & SEC_DEBUGGING) != 0) 13110 if (!_bfd_elf_gc_mark (info, isec, 13111 elf_gc_mark_debug_section)) 13112 return FALSE; 13113 } 13114 return TRUE; 13115 } 13116 13117 static bfd_boolean 13118 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) 13119 { 13120 bfd *sub; 13121 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13122 13123 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13124 { 13125 asection *o; 13126 13127 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 13128 || elf_object_id (sub) != elf_hash_table_id (elf_hash_table (info)) 13129 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 13130 continue; 13131 o = sub->sections; 13132 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13133 continue; 13134 13135 for (o = sub->sections; o != NULL; o = o->next) 13136 { 13137 /* When any section in a section group is kept, we keep all 13138 sections in the section group. If the first member of 13139 the section group is excluded, we will also exclude the 13140 group section. */ 13141 if (o->flags & SEC_GROUP) 13142 { 13143 asection *first = elf_next_in_group (o); 13144 o->gc_mark = first->gc_mark; 13145 } 13146 13147 if (o->gc_mark) 13148 continue; 13149 13150 /* Skip sweeping sections already excluded. */ 13151 if (o->flags & SEC_EXCLUDE) 13152 continue; 13153 13154 /* Since this is early in the link process, it is simple 13155 to remove a section from the output. */ 13156 o->flags |= SEC_EXCLUDE; 13157 13158 if (info->print_gc_sections && o->size != 0) 13159 /* xgettext:c-format */ 13160 _bfd_error_handler (_("Removing unused section '%A' in file '%B'"), 13161 o, sub); 13162 } 13163 } 13164 13165 return TRUE; 13166 } 13167 13168 /* Propagate collected vtable information. This is called through 13169 elf_link_hash_traverse. */ 13170 13171 static bfd_boolean 13172 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) 13173 { 13174 /* Those that are not vtables. */ 13175 if (h->start_stop 13176 || h->u2.vtable == NULL 13177 || h->u2.vtable->parent == NULL) 13178 return TRUE; 13179 13180 /* Those vtables that do not have parents, we cannot merge. */ 13181 if (h->u2.vtable->parent == (struct elf_link_hash_entry *) -1) 13182 return TRUE; 13183 13184 /* If we've already been done, exit. */ 13185 if (h->u2.vtable->used && h->u2.vtable->used[-1]) 13186 return TRUE; 13187 13188 /* Make sure the parent's table is up to date. */ 13189 elf_gc_propagate_vtable_entries_used (h->u2.vtable->parent, okp); 13190 13191 if (h->u2.vtable->used == NULL) 13192 { 13193 /* None of this table's entries were referenced. Re-use the 13194 parent's table. */ 13195 h->u2.vtable->used = h->u2.vtable->parent->u2.vtable->used; 13196 h->u2.vtable->size = h->u2.vtable->parent->u2.vtable->size; 13197 } 13198 else 13199 { 13200 size_t n; 13201 bfd_boolean *cu, *pu; 13202 13203 /* Or the parent's entries into ours. */ 13204 cu = h->u2.vtable->used; 13205 cu[-1] = TRUE; 13206 pu = h->u2.vtable->parent->u2.vtable->used; 13207 if (pu != NULL) 13208 { 13209 const struct elf_backend_data *bed; 13210 unsigned int log_file_align; 13211 13212 bed = get_elf_backend_data (h->root.u.def.section->owner); 13213 log_file_align = bed->s->log_file_align; 13214 n = h->u2.vtable->parent->u2.vtable->size >> log_file_align; 13215 while (n--) 13216 { 13217 if (*pu) 13218 *cu = TRUE; 13219 pu++; 13220 cu++; 13221 } 13222 } 13223 } 13224 13225 return TRUE; 13226 } 13227 13228 static bfd_boolean 13229 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) 13230 { 13231 asection *sec; 13232 bfd_vma hstart, hend; 13233 Elf_Internal_Rela *relstart, *relend, *rel; 13234 const struct elf_backend_data *bed; 13235 unsigned int log_file_align; 13236 13237 /* Take care of both those symbols that do not describe vtables as 13238 well as those that are not loaded. */ 13239 if (h->start_stop 13240 || h->u2.vtable == NULL 13241 || h->u2.vtable->parent == NULL) 13242 return TRUE; 13243 13244 BFD_ASSERT (h->root.type == bfd_link_hash_defined 13245 || h->root.type == bfd_link_hash_defweak); 13246 13247 sec = h->root.u.def.section; 13248 hstart = h->root.u.def.value; 13249 hend = hstart + h->size; 13250 13251 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); 13252 if (!relstart) 13253 return *(bfd_boolean *) okp = FALSE; 13254 bed = get_elf_backend_data (sec->owner); 13255 log_file_align = bed->s->log_file_align; 13256 13257 relend = relstart + sec->reloc_count; 13258 13259 for (rel = relstart; rel < relend; ++rel) 13260 if (rel->r_offset >= hstart && rel->r_offset < hend) 13261 { 13262 /* If the entry is in use, do nothing. */ 13263 if (h->u2.vtable->used 13264 && (rel->r_offset - hstart) < h->u2.vtable->size) 13265 { 13266 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; 13267 if (h->u2.vtable->used[entry]) 13268 continue; 13269 } 13270 /* Otherwise, kill it. */ 13271 rel->r_offset = rel->r_info = rel->r_addend = 0; 13272 } 13273 13274 return TRUE; 13275 } 13276 13277 /* Mark sections containing dynamically referenced symbols. When 13278 building shared libraries, we must assume that any visible symbol is 13279 referenced. */ 13280 13281 bfd_boolean 13282 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) 13283 { 13284 struct bfd_link_info *info = (struct bfd_link_info *) inf; 13285 struct bfd_elf_dynamic_list *d = info->dynamic_list; 13286 13287 if ((h->root.type == bfd_link_hash_defined 13288 || h->root.type == bfd_link_hash_defweak) 13289 && ((h->ref_dynamic && !h->forced_local) 13290 || ((h->def_regular || ELF_COMMON_DEF_P (h)) 13291 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 13292 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 13293 && (!bfd_link_executable (info) 13294 || info->gc_keep_exported 13295 || info->export_dynamic 13296 || (h->dynamic 13297 && d != NULL 13298 && (*d->match) (&d->head, NULL, h->root.root.string))) 13299 && (h->versioned >= versioned 13300 || !bfd_hide_sym_by_version (info->version_info, 13301 h->root.root.string))))) 13302 h->root.u.def.section->flags |= SEC_KEEP; 13303 13304 return TRUE; 13305 } 13306 13307 /* Keep all sections containing symbols undefined on the command-line, 13308 and the section containing the entry symbol. */ 13309 13310 void 13311 _bfd_elf_gc_keep (struct bfd_link_info *info) 13312 { 13313 struct bfd_sym_chain *sym; 13314 13315 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) 13316 { 13317 struct elf_link_hash_entry *h; 13318 13319 h = elf_link_hash_lookup (elf_hash_table (info), sym->name, 13320 FALSE, FALSE, FALSE); 13321 13322 if (h != NULL 13323 && (h->root.type == bfd_link_hash_defined 13324 || h->root.type == bfd_link_hash_defweak) 13325 && !bfd_is_abs_section (h->root.u.def.section) 13326 && !bfd_is_und_section (h->root.u.def.section)) 13327 h->root.u.def.section->flags |= SEC_KEEP; 13328 } 13329 } 13330 13331 bfd_boolean 13332 bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED, 13333 struct bfd_link_info *info) 13334 { 13335 bfd *ibfd = info->input_bfds; 13336 13337 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 13338 { 13339 asection *sec; 13340 struct elf_reloc_cookie cookie; 13341 13342 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 13343 continue; 13344 sec = ibfd->sections; 13345 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13346 continue; 13347 13348 if (!init_reloc_cookie (&cookie, info, ibfd)) 13349 return FALSE; 13350 13351 for (sec = ibfd->sections; sec; sec = sec->next) 13352 { 13353 if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry") 13354 && init_reloc_cookie_rels (&cookie, info, ibfd, sec)) 13355 { 13356 _bfd_elf_parse_eh_frame_entry (info, sec, &cookie); 13357 fini_reloc_cookie_rels (&cookie, sec); 13358 } 13359 } 13360 } 13361 return TRUE; 13362 } 13363 13364 /* Do mark and sweep of unused sections. */ 13365 13366 bfd_boolean 13367 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) 13368 { 13369 bfd_boolean ok = TRUE; 13370 bfd *sub; 13371 elf_gc_mark_hook_fn gc_mark_hook; 13372 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13373 struct elf_link_hash_table *htab; 13374 13375 if (!bed->can_gc_sections 13376 || !is_elf_hash_table (info->hash)) 13377 { 13378 _bfd_error_handler(_("Warning: gc-sections option ignored")); 13379 return TRUE; 13380 } 13381 13382 bed->gc_keep (info); 13383 htab = elf_hash_table (info); 13384 13385 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section 13386 at the .eh_frame section if we can mark the FDEs individually. */ 13387 for (sub = info->input_bfds; 13388 info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL; 13389 sub = sub->link.next) 13390 { 13391 asection *sec; 13392 struct elf_reloc_cookie cookie; 13393 13394 sec = sub->sections; 13395 if (sec == NULL || sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13396 continue; 13397 sec = bfd_get_section_by_name (sub, ".eh_frame"); 13398 while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) 13399 { 13400 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); 13401 if (elf_section_data (sec)->sec_info 13402 && (sec->flags & SEC_LINKER_CREATED) == 0) 13403 elf_eh_frame_section (sub) = sec; 13404 fini_reloc_cookie_for_section (&cookie, sec); 13405 sec = bfd_get_next_section_by_name (NULL, sec); 13406 } 13407 } 13408 13409 /* Apply transitive closure to the vtable entry usage info. */ 13410 elf_link_hash_traverse (htab, elf_gc_propagate_vtable_entries_used, &ok); 13411 if (!ok) 13412 return FALSE; 13413 13414 /* Kill the vtable relocations that were not used. */ 13415 elf_link_hash_traverse (htab, elf_gc_smash_unused_vtentry_relocs, &ok); 13416 if (!ok) 13417 return FALSE; 13418 13419 /* Mark dynamically referenced symbols. */ 13420 if (htab->dynamic_sections_created || info->gc_keep_exported) 13421 elf_link_hash_traverse (htab, bed->gc_mark_dynamic_ref, info); 13422 13423 /* Grovel through relocs to find out who stays ... */ 13424 gc_mark_hook = bed->gc_mark_hook; 13425 for (sub = info->input_bfds; sub != NULL; sub = sub->link.next) 13426 { 13427 asection *o; 13428 13429 if (bfd_get_flavour (sub) != bfd_target_elf_flavour 13430 || elf_object_id (sub) != elf_hash_table_id (htab) 13431 || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec)) 13432 continue; 13433 13434 o = sub->sections; 13435 if (o == NULL || o->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13436 continue; 13437 13438 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). 13439 Also treat note sections as a root, if the section is not part 13440 of a group. We must keep all PREINIT_ARRAY, INIT_ARRAY as 13441 well as FINI_ARRAY sections for ld -r. */ 13442 for (o = sub->sections; o != NULL; o = o->next) 13443 if (!o->gc_mark 13444 && (o->flags & SEC_EXCLUDE) == 0 13445 && ((o->flags & SEC_KEEP) != 0 13446 || (bfd_link_relocatable (info) 13447 && ((elf_section_data (o)->this_hdr.sh_type 13448 == SHT_PREINIT_ARRAY) 13449 || (elf_section_data (o)->this_hdr.sh_type 13450 == SHT_INIT_ARRAY) 13451 || (elf_section_data (o)->this_hdr.sh_type 13452 == SHT_FINI_ARRAY))) 13453 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE 13454 && elf_next_in_group (o) == NULL ))) 13455 { 13456 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 13457 return FALSE; 13458 } 13459 } 13460 13461 /* Allow the backend to mark additional target specific sections. */ 13462 bed->gc_mark_extra_sections (info, gc_mark_hook); 13463 13464 /* ... and mark SEC_EXCLUDE for those that go. */ 13465 return elf_gc_sweep (abfd, info); 13466 } 13467 13468 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ 13469 13470 bfd_boolean 13471 bfd_elf_gc_record_vtinherit (bfd *abfd, 13472 asection *sec, 13473 struct elf_link_hash_entry *h, 13474 bfd_vma offset) 13475 { 13476 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 13477 struct elf_link_hash_entry **search, *child; 13478 size_t extsymcount; 13479 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13480 13481 /* The sh_info field of the symtab header tells us where the 13482 external symbols start. We don't care about the local symbols at 13483 this point. */ 13484 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; 13485 if (!elf_bad_symtab (abfd)) 13486 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; 13487 13488 sym_hashes = elf_sym_hashes (abfd); 13489 sym_hashes_end = sym_hashes + extsymcount; 13490 13491 /* Hunt down the child symbol, which is in this section at the same 13492 offset as the relocation. */ 13493 for (search = sym_hashes; search != sym_hashes_end; ++search) 13494 { 13495 if ((child = *search) != NULL 13496 && (child->root.type == bfd_link_hash_defined 13497 || child->root.type == bfd_link_hash_defweak) 13498 && child->root.u.def.section == sec 13499 && child->root.u.def.value == offset) 13500 goto win; 13501 } 13502 13503 /* xgettext:c-format */ 13504 _bfd_error_handler (_("%B: %A+%#Lx: No symbol found for INHERIT"), 13505 abfd, sec, offset); 13506 bfd_set_error (bfd_error_invalid_operation); 13507 return FALSE; 13508 13509 win: 13510 if (!child->u2.vtable) 13511 { 13512 child->u2.vtable = ((struct elf_link_virtual_table_entry *) 13513 bfd_zalloc (abfd, sizeof (*child->u2.vtable))); 13514 if (!child->u2.vtable) 13515 return FALSE; 13516 } 13517 if (!h) 13518 { 13519 /* This *should* only be the absolute section. It could potentially 13520 be that someone has defined a non-global vtable though, which 13521 would be bad. It isn't worth paging in the local symbols to be 13522 sure though; that case should simply be handled by the assembler. */ 13523 13524 child->u2.vtable->parent = (struct elf_link_hash_entry *) -1; 13525 } 13526 else 13527 child->u2.vtable->parent = h; 13528 13529 return TRUE; 13530 } 13531 13532 /* Called from check_relocs to record the existence of a VTENTRY reloc. */ 13533 13534 bfd_boolean 13535 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, 13536 asection *sec ATTRIBUTE_UNUSED, 13537 struct elf_link_hash_entry *h, 13538 bfd_vma addend) 13539 { 13540 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13541 unsigned int log_file_align = bed->s->log_file_align; 13542 13543 if (!h->u2.vtable) 13544 { 13545 h->u2.vtable = ((struct elf_link_virtual_table_entry *) 13546 bfd_zalloc (abfd, sizeof (*h->u2.vtable))); 13547 if (!h->u2.vtable) 13548 return FALSE; 13549 } 13550 13551 if (addend >= h->u2.vtable->size) 13552 { 13553 size_t size, bytes, file_align; 13554 bfd_boolean *ptr = h->u2.vtable->used; 13555 13556 /* While the symbol is undefined, we have to be prepared to handle 13557 a zero size. */ 13558 file_align = 1 << log_file_align; 13559 if (h->root.type == bfd_link_hash_undefined) 13560 size = addend + file_align; 13561 else 13562 { 13563 size = h->size; 13564 if (addend >= size) 13565 { 13566 /* Oops! We've got a reference past the defined end of 13567 the table. This is probably a bug -- shall we warn? */ 13568 size = addend + file_align; 13569 } 13570 } 13571 size = (size + file_align - 1) & -file_align; 13572 13573 /* Allocate one extra entry for use as a "done" flag for the 13574 consolidation pass. */ 13575 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); 13576 13577 if (ptr) 13578 { 13579 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); 13580 13581 if (ptr != NULL) 13582 { 13583 size_t oldbytes; 13584 13585 oldbytes = (((h->u2.vtable->size >> log_file_align) + 1) 13586 * sizeof (bfd_boolean)); 13587 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); 13588 } 13589 } 13590 else 13591 ptr = (bfd_boolean *) bfd_zmalloc (bytes); 13592 13593 if (ptr == NULL) 13594 return FALSE; 13595 13596 /* And arrange for that done flag to be at index -1. */ 13597 h->u2.vtable->used = ptr + 1; 13598 h->u2.vtable->size = size; 13599 } 13600 13601 h->u2.vtable->used[addend >> log_file_align] = TRUE; 13602 13603 return TRUE; 13604 } 13605 13606 /* Map an ELF section header flag to its corresponding string. */ 13607 typedef struct 13608 { 13609 char *flag_name; 13610 flagword flag_value; 13611 } elf_flags_to_name_table; 13612 13613 static elf_flags_to_name_table elf_flags_to_names [] = 13614 { 13615 { "SHF_WRITE", SHF_WRITE }, 13616 { "SHF_ALLOC", SHF_ALLOC }, 13617 { "SHF_EXECINSTR", SHF_EXECINSTR }, 13618 { "SHF_MERGE", SHF_MERGE }, 13619 { "SHF_STRINGS", SHF_STRINGS }, 13620 { "SHF_INFO_LINK", SHF_INFO_LINK}, 13621 { "SHF_LINK_ORDER", SHF_LINK_ORDER}, 13622 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, 13623 { "SHF_GROUP", SHF_GROUP }, 13624 { "SHF_TLS", SHF_TLS }, 13625 { "SHF_MASKOS", SHF_MASKOS }, 13626 { "SHF_EXCLUDE", SHF_EXCLUDE }, 13627 }; 13628 13629 /* Returns TRUE if the section is to be included, otherwise FALSE. */ 13630 bfd_boolean 13631 bfd_elf_lookup_section_flags (struct bfd_link_info *info, 13632 struct flag_info *flaginfo, 13633 asection *section) 13634 { 13635 const bfd_vma sh_flags = elf_section_flags (section); 13636 13637 if (!flaginfo->flags_initialized) 13638 { 13639 bfd *obfd = info->output_bfd; 13640 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 13641 struct flag_info_list *tf = flaginfo->flag_list; 13642 int with_hex = 0; 13643 int without_hex = 0; 13644 13645 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) 13646 { 13647 unsigned i; 13648 flagword (*lookup) (char *); 13649 13650 lookup = bed->elf_backend_lookup_section_flags_hook; 13651 if (lookup != NULL) 13652 { 13653 flagword hexval = (*lookup) ((char *) tf->name); 13654 13655 if (hexval != 0) 13656 { 13657 if (tf->with == with_flags) 13658 with_hex |= hexval; 13659 else if (tf->with == without_flags) 13660 without_hex |= hexval; 13661 tf->valid = TRUE; 13662 continue; 13663 } 13664 } 13665 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) 13666 { 13667 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) 13668 { 13669 if (tf->with == with_flags) 13670 with_hex |= elf_flags_to_names[i].flag_value; 13671 else if (tf->with == without_flags) 13672 without_hex |= elf_flags_to_names[i].flag_value; 13673 tf->valid = TRUE; 13674 break; 13675 } 13676 } 13677 if (!tf->valid) 13678 { 13679 info->callbacks->einfo 13680 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); 13681 return FALSE; 13682 } 13683 } 13684 flaginfo->flags_initialized = TRUE; 13685 flaginfo->only_with_flags |= with_hex; 13686 flaginfo->not_with_flags |= without_hex; 13687 } 13688 13689 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) 13690 return FALSE; 13691 13692 if ((flaginfo->not_with_flags & sh_flags) != 0) 13693 return FALSE; 13694 13695 return TRUE; 13696 } 13697 13698 struct alloc_got_off_arg { 13699 bfd_vma gotoff; 13700 struct bfd_link_info *info; 13701 }; 13702 13703 /* We need a special top-level link routine to convert got reference counts 13704 to real got offsets. */ 13705 13706 static bfd_boolean 13707 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) 13708 { 13709 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; 13710 bfd *obfd = gofarg->info->output_bfd; 13711 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 13712 13713 if (h->got.refcount > 0) 13714 { 13715 h->got.offset = gofarg->gotoff; 13716 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); 13717 } 13718 else 13719 h->got.offset = (bfd_vma) -1; 13720 13721 return TRUE; 13722 } 13723 13724 /* And an accompanying bit to work out final got entry offsets once 13725 we're done. Should be called from final_link. */ 13726 13727 bfd_boolean 13728 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, 13729 struct bfd_link_info *info) 13730 { 13731 bfd *i; 13732 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13733 bfd_vma gotoff; 13734 struct alloc_got_off_arg gofarg; 13735 13736 BFD_ASSERT (abfd == info->output_bfd); 13737 13738 if (! is_elf_hash_table (info->hash)) 13739 return FALSE; 13740 13741 /* The GOT offset is relative to the .got section, but the GOT header is 13742 put into the .got.plt section, if the backend uses it. */ 13743 if (bed->want_got_plt) 13744 gotoff = 0; 13745 else 13746 gotoff = bed->got_header_size; 13747 13748 /* Do the local .got entries first. */ 13749 for (i = info->input_bfds; i; i = i->link.next) 13750 { 13751 bfd_signed_vma *local_got; 13752 size_t j, locsymcount; 13753 Elf_Internal_Shdr *symtab_hdr; 13754 13755 if (bfd_get_flavour (i) != bfd_target_elf_flavour) 13756 continue; 13757 13758 local_got = elf_local_got_refcounts (i); 13759 if (!local_got) 13760 continue; 13761 13762 symtab_hdr = &elf_tdata (i)->symtab_hdr; 13763 if (elf_bad_symtab (i)) 13764 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 13765 else 13766 locsymcount = symtab_hdr->sh_info; 13767 13768 for (j = 0; j < locsymcount; ++j) 13769 { 13770 if (local_got[j] > 0) 13771 { 13772 local_got[j] = gotoff; 13773 gotoff += bed->got_elt_size (abfd, info, NULL, i, j); 13774 } 13775 else 13776 local_got[j] = (bfd_vma) -1; 13777 } 13778 } 13779 13780 /* Then the global .got entries. .plt refcounts are handled by 13781 adjust_dynamic_symbol */ 13782 gofarg.gotoff = gotoff; 13783 gofarg.info = info; 13784 elf_link_hash_traverse (elf_hash_table (info), 13785 elf_gc_allocate_got_offsets, 13786 &gofarg); 13787 return TRUE; 13788 } 13789 13790 /* Many folk need no more in the way of final link than this, once 13791 got entry reference counting is enabled. */ 13792 13793 bfd_boolean 13794 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) 13795 { 13796 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) 13797 return FALSE; 13798 13799 /* Invoke the regular ELF backend linker to do all the work. */ 13800 return bfd_elf_final_link (abfd, info); 13801 } 13802 13803 bfd_boolean 13804 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) 13805 { 13806 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; 13807 13808 if (rcookie->bad_symtab) 13809 rcookie->rel = rcookie->rels; 13810 13811 for (; rcookie->rel < rcookie->relend; rcookie->rel++) 13812 { 13813 unsigned long r_symndx; 13814 13815 if (! rcookie->bad_symtab) 13816 if (rcookie->rel->r_offset > offset) 13817 return FALSE; 13818 if (rcookie->rel->r_offset != offset) 13819 continue; 13820 13821 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; 13822 if (r_symndx == STN_UNDEF) 13823 return TRUE; 13824 13825 if (r_symndx >= rcookie->locsymcount 13826 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) 13827 { 13828 struct elf_link_hash_entry *h; 13829 13830 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; 13831 13832 while (h->root.type == bfd_link_hash_indirect 13833 || h->root.type == bfd_link_hash_warning) 13834 h = (struct elf_link_hash_entry *) h->root.u.i.link; 13835 13836 if ((h->root.type == bfd_link_hash_defined 13837 || h->root.type == bfd_link_hash_defweak) 13838 && (h->root.u.def.section->owner != rcookie->abfd 13839 || h->root.u.def.section->kept_section != NULL 13840 || discarded_section (h->root.u.def.section))) 13841 return TRUE; 13842 } 13843 else 13844 { 13845 /* It's not a relocation against a global symbol, 13846 but it could be a relocation against a local 13847 symbol for a discarded section. */ 13848 asection *isec; 13849 Elf_Internal_Sym *isym; 13850 13851 /* Need to: get the symbol; get the section. */ 13852 isym = &rcookie->locsyms[r_symndx]; 13853 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); 13854 if (isec != NULL 13855 && (isec->kept_section != NULL 13856 || discarded_section (isec))) 13857 return TRUE; 13858 } 13859 return FALSE; 13860 } 13861 return FALSE; 13862 } 13863 13864 /* Discard unneeded references to discarded sections. 13865 Returns -1 on error, 1 if any section's size was changed, 0 if 13866 nothing changed. This function assumes that the relocations are in 13867 sorted order, which is true for all known assemblers. */ 13868 13869 int 13870 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) 13871 { 13872 struct elf_reloc_cookie cookie; 13873 asection *o; 13874 bfd *abfd; 13875 int changed = 0; 13876 13877 if (info->traditional_format 13878 || !is_elf_hash_table (info->hash)) 13879 return 0; 13880 13881 o = bfd_get_section_by_name (output_bfd, ".stab"); 13882 if (o != NULL) 13883 { 13884 asection *i; 13885 13886 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 13887 { 13888 if (i->size == 0 13889 || i->reloc_count == 0 13890 || i->sec_info_type != SEC_INFO_TYPE_STABS) 13891 continue; 13892 13893 abfd = i->owner; 13894 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 13895 continue; 13896 13897 if (!init_reloc_cookie_for_section (&cookie, info, i)) 13898 return -1; 13899 13900 if (_bfd_discard_section_stabs (abfd, i, 13901 elf_section_data (i)->sec_info, 13902 bfd_elf_reloc_symbol_deleted_p, 13903 &cookie)) 13904 changed = 1; 13905 13906 fini_reloc_cookie_for_section (&cookie, i); 13907 } 13908 } 13909 13910 o = NULL; 13911 if (info->eh_frame_hdr_type != COMPACT_EH_HDR) 13912 o = bfd_get_section_by_name (output_bfd, ".eh_frame"); 13913 if (o != NULL) 13914 { 13915 asection *i; 13916 int eh_changed = 0; 13917 unsigned int eh_alignment; 13918 13919 for (i = o->map_head.s; i != NULL; i = i->map_head.s) 13920 { 13921 if (i->size == 0) 13922 continue; 13923 13924 abfd = i->owner; 13925 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 13926 continue; 13927 13928 if (!init_reloc_cookie_for_section (&cookie, info, i)) 13929 return -1; 13930 13931 _bfd_elf_parse_eh_frame (abfd, info, i, &cookie); 13932 if (_bfd_elf_discard_section_eh_frame (abfd, info, i, 13933 bfd_elf_reloc_symbol_deleted_p, 13934 &cookie)) 13935 { 13936 eh_changed = 1; 13937 if (i->size != i->rawsize) 13938 changed = 1; 13939 } 13940 13941 fini_reloc_cookie_for_section (&cookie, i); 13942 } 13943 13944 eh_alignment = 1 << o->alignment_power; 13945 /* Skip over zero terminator, and prevent empty sections from 13946 adding alignment padding at the end. */ 13947 for (i = o->map_tail.s; i != NULL; i = i->map_tail.s) 13948 if (i->size == 0) 13949 i->flags |= SEC_EXCLUDE; 13950 else if (i->size > 4) 13951 break; 13952 /* The last non-empty eh_frame section doesn't need padding. */ 13953 if (i != NULL) 13954 i = i->map_tail.s; 13955 /* Any prior sections must pad the last FDE out to the output 13956 section alignment. Otherwise we might have zero padding 13957 between sections, which would be seen as a terminator. */ 13958 for (; i != NULL; i = i->map_tail.s) 13959 if (i->size == 4) 13960 /* All but the last zero terminator should have been removed. */ 13961 BFD_FAIL (); 13962 else 13963 { 13964 bfd_size_type size 13965 = (i->size + eh_alignment - 1) & -eh_alignment; 13966 if (i->size != size) 13967 { 13968 i->size = size; 13969 changed = 1; 13970 eh_changed = 1; 13971 } 13972 } 13973 if (eh_changed) 13974 elf_link_hash_traverse (elf_hash_table (info), 13975 _bfd_elf_adjust_eh_frame_global_symbol, NULL); 13976 } 13977 13978 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link.next) 13979 { 13980 const struct elf_backend_data *bed; 13981 asection *s; 13982 13983 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 13984 continue; 13985 s = abfd->sections; 13986 if (s == NULL || s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 13987 continue; 13988 13989 bed = get_elf_backend_data (abfd); 13990 13991 if (bed->elf_backend_discard_info != NULL) 13992 { 13993 if (!init_reloc_cookie (&cookie, info, abfd)) 13994 return -1; 13995 13996 if ((*bed->elf_backend_discard_info) (abfd, &cookie, info)) 13997 changed = 1; 13998 13999 fini_reloc_cookie (&cookie, abfd); 14000 } 14001 } 14002 14003 if (info->eh_frame_hdr_type == COMPACT_EH_HDR) 14004 _bfd_elf_end_eh_frame_parsing (info); 14005 14006 if (info->eh_frame_hdr_type 14007 && !bfd_link_relocatable (info) 14008 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) 14009 changed = 1; 14010 14011 return changed; 14012 } 14013 14014 bfd_boolean 14015 _bfd_elf_section_already_linked (bfd *abfd, 14016 asection *sec, 14017 struct bfd_link_info *info) 14018 { 14019 flagword flags; 14020 const char *name, *key; 14021 struct bfd_section_already_linked *l; 14022 struct bfd_section_already_linked_hash_entry *already_linked_list; 14023 14024 if (sec->output_section == bfd_abs_section_ptr) 14025 return FALSE; 14026 14027 flags = sec->flags; 14028 14029 /* Return if it isn't a linkonce section. A comdat group section 14030 also has SEC_LINK_ONCE set. */ 14031 if ((flags & SEC_LINK_ONCE) == 0) 14032 return FALSE; 14033 14034 /* Don't put group member sections on our list of already linked 14035 sections. They are handled as a group via their group section. */ 14036 if (elf_sec_group (sec) != NULL) 14037 return FALSE; 14038 14039 /* For a SHT_GROUP section, use the group signature as the key. */ 14040 name = sec->name; 14041 if ((flags & SEC_GROUP) != 0 14042 && elf_next_in_group (sec) != NULL 14043 && elf_group_name (elf_next_in_group (sec)) != NULL) 14044 key = elf_group_name (elf_next_in_group (sec)); 14045 else 14046 { 14047 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ 14048 if (CONST_STRNEQ (name, ".gnu.linkonce.") 14049 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 14050 key++; 14051 else 14052 /* Must be a user linkonce section that doesn't follow gcc's 14053 naming convention. In this case we won't be matching 14054 single member groups. */ 14055 key = name; 14056 } 14057 14058 already_linked_list = bfd_section_already_linked_table_lookup (key); 14059 14060 for (l = already_linked_list->entry; l != NULL; l = l->next) 14061 { 14062 /* We may have 2 different types of sections on the list: group 14063 sections with a signature of <key> (<key> is some string), 14064 and linkonce sections named .gnu.linkonce.<type>.<key>. 14065 Match like sections. LTO plugin sections are an exception. 14066 They are always named .gnu.linkonce.t.<key> and match either 14067 type of section. */ 14068 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) 14069 && ((flags & SEC_GROUP) != 0 14070 || strcmp (name, l->sec->name) == 0)) 14071 || (l->sec->owner->flags & BFD_PLUGIN) != 0) 14072 { 14073 /* The section has already been linked. See if we should 14074 issue a warning. */ 14075 if (!_bfd_handle_already_linked (sec, l, info)) 14076 return FALSE; 14077 14078 if (flags & SEC_GROUP) 14079 { 14080 asection *first = elf_next_in_group (sec); 14081 asection *s = first; 14082 14083 while (s != NULL) 14084 { 14085 s->output_section = bfd_abs_section_ptr; 14086 /* Record which group discards it. */ 14087 s->kept_section = l->sec; 14088 s = elf_next_in_group (s); 14089 /* These lists are circular. */ 14090 if (s == first) 14091 break; 14092 } 14093 } 14094 14095 return TRUE; 14096 } 14097 } 14098 14099 /* A single member comdat group section may be discarded by a 14100 linkonce section and vice versa. */ 14101 if ((flags & SEC_GROUP) != 0) 14102 { 14103 asection *first = elf_next_in_group (sec); 14104 14105 if (first != NULL && elf_next_in_group (first) == first) 14106 /* Check this single member group against linkonce sections. */ 14107 for (l = already_linked_list->entry; l != NULL; l = l->next) 14108 if ((l->sec->flags & SEC_GROUP) == 0 14109 && bfd_elf_match_symbols_in_sections (l->sec, first, info)) 14110 { 14111 first->output_section = bfd_abs_section_ptr; 14112 first->kept_section = l->sec; 14113 sec->output_section = bfd_abs_section_ptr; 14114 break; 14115 } 14116 } 14117 else 14118 /* Check this linkonce section against single member groups. */ 14119 for (l = already_linked_list->entry; l != NULL; l = l->next) 14120 if (l->sec->flags & SEC_GROUP) 14121 { 14122 asection *first = elf_next_in_group (l->sec); 14123 14124 if (first != NULL 14125 && elf_next_in_group (first) == first 14126 && bfd_elf_match_symbols_in_sections (first, sec, info)) 14127 { 14128 sec->output_section = bfd_abs_section_ptr; 14129 sec->kept_section = first; 14130 break; 14131 } 14132 } 14133 14134 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' 14135 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 14136 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' 14137 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its 14138 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded 14139 but its `.gnu.linkonce.t.F' is discarded means we chose one-only 14140 `.gnu.linkonce.t.F' section from a different bfd not requiring any 14141 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. 14142 The reverse order cannot happen as there is never a bfd with only the 14143 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not 14144 matter as here were are looking only for cross-bfd sections. */ 14145 14146 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) 14147 for (l = already_linked_list->entry; l != NULL; l = l->next) 14148 if ((l->sec->flags & SEC_GROUP) == 0 14149 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) 14150 { 14151 if (abfd != l->sec->owner) 14152 sec->output_section = bfd_abs_section_ptr; 14153 break; 14154 } 14155 14156 /* This is the first section with this name. Record it. */ 14157 if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) 14158 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); 14159 return sec->output_section == bfd_abs_section_ptr; 14160 } 14161 14162 bfd_boolean 14163 _bfd_elf_common_definition (Elf_Internal_Sym *sym) 14164 { 14165 return sym->st_shndx == SHN_COMMON; 14166 } 14167 14168 unsigned int 14169 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) 14170 { 14171 return SHN_COMMON; 14172 } 14173 14174 asection * 14175 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) 14176 { 14177 return bfd_com_section_ptr; 14178 } 14179 14180 bfd_vma 14181 _bfd_elf_default_got_elt_size (bfd *abfd, 14182 struct bfd_link_info *info ATTRIBUTE_UNUSED, 14183 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, 14184 bfd *ibfd ATTRIBUTE_UNUSED, 14185 unsigned long symndx ATTRIBUTE_UNUSED) 14186 { 14187 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14188 return bed->s->arch_size / 8; 14189 } 14190 14191 /* Routines to support the creation of dynamic relocs. */ 14192 14193 /* Returns the name of the dynamic reloc section associated with SEC. */ 14194 14195 static const char * 14196 get_dynamic_reloc_section_name (bfd * abfd, 14197 asection * sec, 14198 bfd_boolean is_rela) 14199 { 14200 char *name; 14201 const char *old_name = bfd_get_section_name (NULL, sec); 14202 const char *prefix = is_rela ? ".rela" : ".rel"; 14203 14204 if (old_name == NULL) 14205 return NULL; 14206 14207 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); 14208 sprintf (name, "%s%s", prefix, old_name); 14209 14210 return name; 14211 } 14212 14213 /* Returns the dynamic reloc section associated with SEC. 14214 If necessary compute the name of the dynamic reloc section based 14215 on SEC's name (looked up in ABFD's string table) and the setting 14216 of IS_RELA. */ 14217 14218 asection * 14219 _bfd_elf_get_dynamic_reloc_section (bfd * abfd, 14220 asection * sec, 14221 bfd_boolean is_rela) 14222 { 14223 asection * reloc_sec = elf_section_data (sec)->sreloc; 14224 14225 if (reloc_sec == NULL) 14226 { 14227 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 14228 14229 if (name != NULL) 14230 { 14231 reloc_sec = bfd_get_linker_section (abfd, name); 14232 14233 if (reloc_sec != NULL) 14234 elf_section_data (sec)->sreloc = reloc_sec; 14235 } 14236 } 14237 14238 return reloc_sec; 14239 } 14240 14241 /* Returns the dynamic reloc section associated with SEC. If the 14242 section does not exist it is created and attached to the DYNOBJ 14243 bfd and stored in the SRELOC field of SEC's elf_section_data 14244 structure. 14245 14246 ALIGNMENT is the alignment for the newly created section and 14247 IS_RELA defines whether the name should be .rela.<SEC's name> 14248 or .rel.<SEC's name>. The section name is looked up in the 14249 string table associated with ABFD. */ 14250 14251 asection * 14252 _bfd_elf_make_dynamic_reloc_section (asection *sec, 14253 bfd *dynobj, 14254 unsigned int alignment, 14255 bfd *abfd, 14256 bfd_boolean is_rela) 14257 { 14258 asection * reloc_sec = elf_section_data (sec)->sreloc; 14259 14260 if (reloc_sec == NULL) 14261 { 14262 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 14263 14264 if (name == NULL) 14265 return NULL; 14266 14267 reloc_sec = bfd_get_linker_section (dynobj, name); 14268 14269 if (reloc_sec == NULL) 14270 { 14271 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY 14272 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 14273 if ((sec->flags & SEC_ALLOC) != 0) 14274 flags |= SEC_ALLOC | SEC_LOAD; 14275 14276 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); 14277 if (reloc_sec != NULL) 14278 { 14279 /* _bfd_elf_get_sec_type_attr chooses a section type by 14280 name. Override as it may be wrong, eg. for a user 14281 section named "auto" we'll get ".relauto" which is 14282 seen to be a .rela section. */ 14283 elf_section_type (reloc_sec) = is_rela ? SHT_RELA : SHT_REL; 14284 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) 14285 reloc_sec = NULL; 14286 } 14287 } 14288 14289 elf_section_data (sec)->sreloc = reloc_sec; 14290 } 14291 14292 return reloc_sec; 14293 } 14294 14295 /* Copy the ELF symbol type and other attributes for a linker script 14296 assignment from HSRC to HDEST. Generally this should be treated as 14297 if we found a strong non-dynamic definition for HDEST (except that 14298 ld ignores multiple definition errors). */ 14299 void 14300 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd, 14301 struct bfd_link_hash_entry *hdest, 14302 struct bfd_link_hash_entry *hsrc) 14303 { 14304 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *) hdest; 14305 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *) hsrc; 14306 Elf_Internal_Sym isym; 14307 14308 ehdest->type = ehsrc->type; 14309 ehdest->target_internal = ehsrc->target_internal; 14310 14311 isym.st_other = ehsrc->other; 14312 elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE); 14313 } 14314 14315 /* Append a RELA relocation REL to section S in BFD. */ 14316 14317 void 14318 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 14319 { 14320 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14321 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); 14322 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); 14323 bed->s->swap_reloca_out (abfd, rel, loc); 14324 } 14325 14326 /* Append a REL relocation REL to section S in BFD. */ 14327 14328 void 14329 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 14330 { 14331 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 14332 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); 14333 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); 14334 bed->s->swap_reloc_out (abfd, rel, loc); 14335 } 14336 14337 /* Define __start, __stop, .startof. or .sizeof. symbol. */ 14338 14339 struct bfd_link_hash_entry * 14340 bfd_elf_define_start_stop (struct bfd_link_info *info, 14341 const char *symbol, asection *sec) 14342 { 14343 struct elf_link_hash_entry *h; 14344 14345 h = elf_link_hash_lookup (elf_hash_table (info), symbol, 14346 FALSE, FALSE, TRUE); 14347 if (h != NULL 14348 && (h->root.type == bfd_link_hash_undefined 14349 || h->root.type == bfd_link_hash_undefweak 14350 || (h->ref_regular && !h->def_regular))) 14351 { 14352 h->root.type = bfd_link_hash_defined; 14353 h->root.u.def.section = sec; 14354 h->root.u.def.value = 0; 14355 h->def_regular = 1; 14356 h->def_dynamic = 0; 14357 h->start_stop = 1; 14358 h->u2.start_stop_section = sec; 14359 if (symbol[0] == '.') 14360 { 14361 /* .startof. and .sizeof. symbols are local. */ 14362 const struct elf_backend_data *bed; 14363 bed = get_elf_backend_data (info->output_bfd); 14364 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 14365 } 14366 else if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 14367 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_PROTECTED; 14368 return &h->root; 14369 } 14370 return NULL; 14371 } 14372