1 /* ELF linking support for BFD. 2 Copyright 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 3 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 4 Free Software Foundation, Inc. 5 6 This file is part of BFD, the Binary File Descriptor library. 7 8 This program is free software; you can redistribute it and/or modify 9 it under the terms of the GNU General Public License as published by 10 the Free Software Foundation; either version 3 of the License, or 11 (at your option) any later version. 12 13 This program is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 GNU General Public License for more details. 17 18 You should have received a copy of the GNU General Public License 19 along with this program; if not, write to the Free Software 20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 21 MA 02110-1301, USA. */ 22 23 #include "sysdep.h" 24 #include "bfd.h" 25 #include "bfdlink.h" 26 #include "libbfd.h" 27 #define ARCH_SIZE 0 28 #include "elf-bfd.h" 29 #include "safe-ctype.h" 30 #include "libiberty.h" 31 #include "objalloc.h" 32 33 /* This struct is used to pass information to routines called via 34 elf_link_hash_traverse which must return failure. */ 35 36 struct elf_info_failed 37 { 38 struct bfd_link_info *info; 39 bfd_boolean failed; 40 }; 41 42 /* This structure is used to pass information to 43 _bfd_elf_link_find_version_dependencies. */ 44 45 struct elf_find_verdep_info 46 { 47 /* General link information. */ 48 struct bfd_link_info *info; 49 /* The number of dependencies. */ 50 unsigned int vers; 51 /* Whether we had a failure. */ 52 bfd_boolean failed; 53 }; 54 55 static bfd_boolean _bfd_elf_fix_symbol_flags 56 (struct elf_link_hash_entry *, struct elf_info_failed *); 57 58 /* Define a symbol in a dynamic linkage section. */ 59 60 struct elf_link_hash_entry * 61 _bfd_elf_define_linkage_sym (bfd *abfd, 62 struct bfd_link_info *info, 63 asection *sec, 64 const char *name) 65 { 66 struct elf_link_hash_entry *h; 67 struct bfd_link_hash_entry *bh; 68 const struct elf_backend_data *bed; 69 70 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 71 if (h != NULL) 72 { 73 /* Zap symbol defined in an as-needed lib that wasn't linked. 74 This is a symptom of a larger problem: Absolute symbols 75 defined in shared libraries can't be overridden, because we 76 lose the link to the bfd which is via the symbol section. */ 77 h->root.type = bfd_link_hash_new; 78 } 79 80 bh = &h->root; 81 if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL, 82 sec, 0, NULL, FALSE, 83 get_elf_backend_data (abfd)->collect, 84 &bh)) 85 return NULL; 86 h = (struct elf_link_hash_entry *) bh; 87 h->def_regular = 1; 88 h->non_elf = 0; 89 h->type = STT_OBJECT; 90 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 91 92 bed = get_elf_backend_data (abfd); 93 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 94 return h; 95 } 96 97 bfd_boolean 98 _bfd_elf_create_got_section (bfd *abfd, struct bfd_link_info *info) 99 { 100 flagword flags; 101 asection *s; 102 struct elf_link_hash_entry *h; 103 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 104 struct elf_link_hash_table *htab = elf_hash_table (info); 105 106 /* This function may be called more than once. */ 107 s = bfd_get_linker_section (abfd, ".got"); 108 if (s != NULL) 109 return TRUE; 110 111 flags = bed->dynamic_sec_flags; 112 113 s = bfd_make_section_anyway_with_flags (abfd, 114 (bed->rela_plts_and_copies_p 115 ? ".rela.got" : ".rel.got"), 116 (bed->dynamic_sec_flags 117 | SEC_READONLY)); 118 if (s == NULL 119 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 120 return FALSE; 121 htab->srelgot = s; 122 123 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 124 if (s == NULL 125 || !bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 126 return FALSE; 127 htab->sgot = s; 128 129 if (bed->want_got_plt) 130 { 131 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); 132 if (s == NULL 133 || !bfd_set_section_alignment (abfd, s, 134 bed->s->log_file_align)) 135 return FALSE; 136 htab->sgotplt = s; 137 } 138 139 /* The first bit of the global offset table is the header. */ 140 s->size += bed->got_header_size; 141 142 if (bed->want_got_sym) 143 { 144 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 145 (or .got.plt) section. We don't do this in the linker script 146 because we don't want to define the symbol if we are not creating 147 a global offset table. */ 148 h = _bfd_elf_define_linkage_sym (abfd, info, s, 149 "_GLOBAL_OFFSET_TABLE_"); 150 elf_hash_table (info)->hgot = h; 151 if (h == NULL) 152 return FALSE; 153 } 154 155 return TRUE; 156 } 157 158 /* Create a strtab to hold the dynamic symbol names. */ 159 static bfd_boolean 160 _bfd_elf_link_create_dynstrtab (bfd *abfd, struct bfd_link_info *info) 161 { 162 struct elf_link_hash_table *hash_table; 163 164 hash_table = elf_hash_table (info); 165 if (hash_table->dynobj == NULL) 166 hash_table->dynobj = abfd; 167 168 if (hash_table->dynstr == NULL) 169 { 170 hash_table->dynstr = _bfd_elf_strtab_init (); 171 if (hash_table->dynstr == NULL) 172 return FALSE; 173 } 174 return TRUE; 175 } 176 177 /* Create some sections which will be filled in with dynamic linking 178 information. ABFD is an input file which requires dynamic sections 179 to be created. The dynamic sections take up virtual memory space 180 when the final executable is run, so we need to create them before 181 addresses are assigned to the output sections. We work out the 182 actual contents and size of these sections later. */ 183 184 bfd_boolean 185 _bfd_elf_link_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 186 { 187 flagword flags; 188 asection *s; 189 const struct elf_backend_data *bed; 190 191 if (! is_elf_hash_table (info->hash)) 192 return FALSE; 193 194 if (elf_hash_table (info)->dynamic_sections_created) 195 return TRUE; 196 197 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 198 return FALSE; 199 200 abfd = elf_hash_table (info)->dynobj; 201 bed = get_elf_backend_data (abfd); 202 203 flags = bed->dynamic_sec_flags; 204 205 /* A dynamically linked executable has a .interp section, but a 206 shared library does not. */ 207 if (info->executable) 208 { 209 s = bfd_make_section_anyway_with_flags (abfd, ".interp", 210 flags | SEC_READONLY); 211 if (s == NULL) 212 return FALSE; 213 } 214 215 /* Create sections to hold version informations. These are removed 216 if they are not needed. */ 217 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_d", 218 flags | SEC_READONLY); 219 if (s == NULL 220 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 221 return FALSE; 222 223 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version", 224 flags | SEC_READONLY); 225 if (s == NULL 226 || ! bfd_set_section_alignment (abfd, s, 1)) 227 return FALSE; 228 229 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.version_r", 230 flags | SEC_READONLY); 231 if (s == NULL 232 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 233 return FALSE; 234 235 s = bfd_make_section_anyway_with_flags (abfd, ".dynsym", 236 flags | SEC_READONLY); 237 if (s == NULL 238 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 239 return FALSE; 240 241 s = bfd_make_section_anyway_with_flags (abfd, ".dynstr", 242 flags | SEC_READONLY); 243 if (s == NULL) 244 return FALSE; 245 246 s = bfd_make_section_anyway_with_flags (abfd, ".dynamic", flags); 247 if (s == NULL 248 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 249 return FALSE; 250 251 /* The special symbol _DYNAMIC is always set to the start of the 252 .dynamic section. We could set _DYNAMIC in a linker script, but we 253 only want to define it if we are, in fact, creating a .dynamic 254 section. We don't want to define it if there is no .dynamic 255 section, since on some ELF platforms the start up code examines it 256 to decide how to initialize the process. */ 257 if (!_bfd_elf_define_linkage_sym (abfd, info, s, "_DYNAMIC")) 258 return FALSE; 259 260 if (info->emit_hash) 261 { 262 s = bfd_make_section_anyway_with_flags (abfd, ".hash", 263 flags | SEC_READONLY); 264 if (s == NULL 265 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 266 return FALSE; 267 elf_section_data (s)->this_hdr.sh_entsize = bed->s->sizeof_hash_entry; 268 } 269 270 if (info->emit_gnu_hash) 271 { 272 s = bfd_make_section_anyway_with_flags (abfd, ".gnu.hash", 273 flags | SEC_READONLY); 274 if (s == NULL 275 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 276 return FALSE; 277 /* For 64-bit ELF, .gnu.hash is a non-uniform entity size section: 278 4 32-bit words followed by variable count of 64-bit words, then 279 variable count of 32-bit words. */ 280 if (bed->s->arch_size == 64) 281 elf_section_data (s)->this_hdr.sh_entsize = 0; 282 else 283 elf_section_data (s)->this_hdr.sh_entsize = 4; 284 } 285 286 /* Let the backend create the rest of the sections. This lets the 287 backend set the right flags. The backend will normally create 288 the .got and .plt sections. */ 289 if (bed->elf_backend_create_dynamic_sections == NULL 290 || ! (*bed->elf_backend_create_dynamic_sections) (abfd, info)) 291 return FALSE; 292 293 elf_hash_table (info)->dynamic_sections_created = TRUE; 294 295 return TRUE; 296 } 297 298 /* Create dynamic sections when linking against a dynamic object. */ 299 300 bfd_boolean 301 _bfd_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 302 { 303 flagword flags, pltflags; 304 struct elf_link_hash_entry *h; 305 asection *s; 306 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 307 struct elf_link_hash_table *htab = elf_hash_table (info); 308 309 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 310 .rel[a].bss sections. */ 311 flags = bed->dynamic_sec_flags; 312 313 pltflags = flags; 314 if (bed->plt_not_loaded) 315 /* We do not clear SEC_ALLOC here because we still want the OS to 316 allocate space for the section; it's just that there's nothing 317 to read in from the object file. */ 318 pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS); 319 else 320 pltflags |= SEC_ALLOC | SEC_CODE | SEC_LOAD; 321 if (bed->plt_readonly) 322 pltflags |= SEC_READONLY; 323 324 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); 325 if (s == NULL 326 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 327 return FALSE; 328 htab->splt = s; 329 330 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 331 .plt section. */ 332 if (bed->want_plt_sym) 333 { 334 h = _bfd_elf_define_linkage_sym (abfd, info, s, 335 "_PROCEDURE_LINKAGE_TABLE_"); 336 elf_hash_table (info)->hplt = h; 337 if (h == NULL) 338 return FALSE; 339 } 340 341 s = bfd_make_section_anyway_with_flags (abfd, 342 (bed->rela_plts_and_copies_p 343 ? ".rela.plt" : ".rel.plt"), 344 flags | SEC_READONLY); 345 if (s == NULL 346 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 347 return FALSE; 348 htab->srelplt = s; 349 350 if (! _bfd_elf_create_got_section (abfd, info)) 351 return FALSE; 352 353 if (bed->want_dynbss) 354 { 355 /* The .dynbss section is a place to put symbols which are defined 356 by dynamic objects, are referenced by regular objects, and are 357 not functions. We must allocate space for them in the process 358 image and use a R_*_COPY reloc to tell the dynamic linker to 359 initialize them at run time. The linker script puts the .dynbss 360 section into the .bss section of the final image. */ 361 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", 362 (SEC_ALLOC | SEC_LINKER_CREATED)); 363 if (s == NULL) 364 return FALSE; 365 366 /* The .rel[a].bss section holds copy relocs. This section is not 367 normally needed. We need to create it here, though, so that the 368 linker will map it to an output section. We can't just create it 369 only if we need it, because we will not know whether we need it 370 until we have seen all the input files, and the first time the 371 main linker code calls BFD after examining all the input files 372 (size_dynamic_sections) the input sections have already been 373 mapped to the output sections. If the section turns out not to 374 be needed, we can discard it later. We will never need this 375 section when generating a shared object, since they do not use 376 copy relocs. */ 377 if (! info->shared) 378 { 379 s = bfd_make_section_anyway_with_flags (abfd, 380 (bed->rela_plts_and_copies_p 381 ? ".rela.bss" : ".rel.bss"), 382 flags | SEC_READONLY); 383 if (s == NULL 384 || ! bfd_set_section_alignment (abfd, s, bed->s->log_file_align)) 385 return FALSE; 386 } 387 } 388 389 return TRUE; 390 } 391 392 /* Record a new dynamic symbol. We record the dynamic symbols as we 393 read the input files, since we need to have a list of all of them 394 before we can determine the final sizes of the output sections. 395 Note that we may actually call this function even though we are not 396 going to output any dynamic symbols; in some cases we know that a 397 symbol should be in the dynamic symbol table, but only if there is 398 one. */ 399 400 bfd_boolean 401 bfd_elf_link_record_dynamic_symbol (struct bfd_link_info *info, 402 struct elf_link_hash_entry *h) 403 { 404 if (h->dynindx == -1) 405 { 406 struct elf_strtab_hash *dynstr; 407 char *p; 408 const char *name; 409 bfd_size_type indx; 410 411 /* XXX: The ABI draft says the linker must turn hidden and 412 internal symbols into STB_LOCAL symbols when producing the 413 DSO. However, if ld.so honors st_other in the dynamic table, 414 this would not be necessary. */ 415 switch (ELF_ST_VISIBILITY (h->other)) 416 { 417 case STV_INTERNAL: 418 case STV_HIDDEN: 419 if (h->root.type != bfd_link_hash_undefined 420 && h->root.type != bfd_link_hash_undefweak) 421 { 422 h->forced_local = 1; 423 if (!elf_hash_table (info)->is_relocatable_executable) 424 return TRUE; 425 } 426 427 default: 428 break; 429 } 430 431 h->dynindx = elf_hash_table (info)->dynsymcount; 432 ++elf_hash_table (info)->dynsymcount; 433 434 dynstr = elf_hash_table (info)->dynstr; 435 if (dynstr == NULL) 436 { 437 /* Create a strtab to hold the dynamic symbol names. */ 438 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 439 if (dynstr == NULL) 440 return FALSE; 441 } 442 443 /* We don't put any version information in the dynamic string 444 table. */ 445 name = h->root.root.string; 446 p = strchr (name, ELF_VER_CHR); 447 if (p != NULL) 448 /* We know that the p points into writable memory. In fact, 449 there are only a few symbols that have read-only names, being 450 those like _GLOBAL_OFFSET_TABLE_ that are created specially 451 by the backends. Most symbols will have names pointing into 452 an ELF string table read from a file, or to objalloc memory. */ 453 *p = 0; 454 455 indx = _bfd_elf_strtab_add (dynstr, name, p != NULL); 456 457 if (p != NULL) 458 *p = ELF_VER_CHR; 459 460 if (indx == (bfd_size_type) -1) 461 return FALSE; 462 h->dynstr_index = indx; 463 } 464 465 return TRUE; 466 } 467 468 /* Mark a symbol dynamic. */ 469 470 static void 471 bfd_elf_link_mark_dynamic_symbol (struct bfd_link_info *info, 472 struct elf_link_hash_entry *h, 473 Elf_Internal_Sym *sym) 474 { 475 struct bfd_elf_dynamic_list *d = info->dynamic_list; 476 477 /* It may be called more than once on the same H. */ 478 if(h->dynamic || info->relocatable) 479 return; 480 481 if ((info->dynamic_data 482 && (h->type == STT_OBJECT 483 || (sym != NULL 484 && ELF_ST_TYPE (sym->st_info) == STT_OBJECT))) 485 || (d != NULL 486 && h->root.type == bfd_link_hash_new 487 && (*d->match) (&d->head, NULL, h->root.root.string))) 488 h->dynamic = 1; 489 } 490 491 /* Record an assignment to a symbol made by a linker script. We need 492 this in case some dynamic object refers to this symbol. */ 493 494 bfd_boolean 495 bfd_elf_record_link_assignment (bfd *output_bfd, 496 struct bfd_link_info *info, 497 const char *name, 498 bfd_boolean provide, 499 bfd_boolean hidden) 500 { 501 struct elf_link_hash_entry *h, *hv; 502 struct elf_link_hash_table *htab; 503 const struct elf_backend_data *bed; 504 505 if (!is_elf_hash_table (info->hash)) 506 return TRUE; 507 508 htab = elf_hash_table (info); 509 h = elf_link_hash_lookup (htab, name, !provide, TRUE, FALSE); 510 if (h == NULL) 511 return provide; 512 513 switch (h->root.type) 514 { 515 case bfd_link_hash_defined: 516 case bfd_link_hash_defweak: 517 case bfd_link_hash_common: 518 break; 519 case bfd_link_hash_undefweak: 520 case bfd_link_hash_undefined: 521 /* Since we're defining the symbol, don't let it seem to have not 522 been defined. record_dynamic_symbol and size_dynamic_sections 523 may depend on this. */ 524 h->root.type = bfd_link_hash_new; 525 if (h->root.u.undef.next != NULL || htab->root.undefs_tail == &h->root) 526 bfd_link_repair_undef_list (&htab->root); 527 break; 528 case bfd_link_hash_new: 529 bfd_elf_link_mark_dynamic_symbol (info, h, NULL); 530 h->non_elf = 0; 531 break; 532 case bfd_link_hash_indirect: 533 /* We had a versioned symbol in a dynamic library. We make the 534 the versioned symbol point to this one. */ 535 bed = get_elf_backend_data (output_bfd); 536 hv = h; 537 while (hv->root.type == bfd_link_hash_indirect 538 || hv->root.type == bfd_link_hash_warning) 539 hv = (struct elf_link_hash_entry *) hv->root.u.i.link; 540 /* We don't need to update h->root.u since linker will set them 541 later. */ 542 h->root.type = bfd_link_hash_undefined; 543 hv->root.type = bfd_link_hash_indirect; 544 hv->root.u.i.link = (struct bfd_link_hash_entry *) h; 545 (*bed->elf_backend_copy_indirect_symbol) (info, h, hv); 546 break; 547 case bfd_link_hash_warning: 548 abort (); 549 break; 550 } 551 552 /* If this symbol is being provided by the linker script, and it is 553 currently defined by a dynamic object, but not by a regular 554 object, then mark it as undefined so that the generic linker will 555 force the correct value. */ 556 if (provide 557 && h->def_dynamic 558 && !h->def_regular) 559 h->root.type = bfd_link_hash_undefined; 560 561 /* If this symbol is not being provided by the linker script, and it is 562 currently defined by a dynamic object, but not by a regular object, 563 then clear out any version information because the symbol will not be 564 associated with the dynamic object any more. */ 565 if (!provide 566 && h->def_dynamic 567 && !h->def_regular) 568 h->verinfo.verdef = NULL; 569 570 h->def_regular = 1; 571 572 if (hidden) 573 { 574 bed = get_elf_backend_data (output_bfd); 575 h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN; 576 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 577 } 578 579 /* STV_HIDDEN and STV_INTERNAL symbols must be STB_LOCAL in shared objects 580 and executables. */ 581 if (!info->relocatable 582 && h->dynindx != -1 583 && (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 584 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL)) 585 h->forced_local = 1; 586 587 if ((h->def_dynamic 588 || h->ref_dynamic 589 || info->shared 590 || (info->executable && elf_hash_table (info)->is_relocatable_executable)) 591 && h->dynindx == -1) 592 { 593 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 594 return FALSE; 595 596 /* If this is a weak defined symbol, and we know a corresponding 597 real symbol from the same dynamic object, make sure the real 598 symbol is also made into a dynamic symbol. */ 599 if (h->u.weakdef != NULL 600 && h->u.weakdef->dynindx == -1) 601 { 602 if (! bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) 603 return FALSE; 604 } 605 } 606 607 return TRUE; 608 } 609 610 /* Record a new local dynamic symbol. Returns 0 on failure, 1 on 611 success, and 2 on a failure caused by attempting to record a symbol 612 in a discarded section, eg. a discarded link-once section symbol. */ 613 614 int 615 bfd_elf_link_record_local_dynamic_symbol (struct bfd_link_info *info, 616 bfd *input_bfd, 617 long input_indx) 618 { 619 bfd_size_type amt; 620 struct elf_link_local_dynamic_entry *entry; 621 struct elf_link_hash_table *eht; 622 struct elf_strtab_hash *dynstr; 623 unsigned long dynstr_index; 624 char *name; 625 Elf_External_Sym_Shndx eshndx; 626 char esym[sizeof (Elf64_External_Sym)]; 627 628 if (! is_elf_hash_table (info->hash)) 629 return 0; 630 631 /* See if the entry exists already. */ 632 for (entry = elf_hash_table (info)->dynlocal; entry ; entry = entry->next) 633 if (entry->input_bfd == input_bfd && entry->input_indx == input_indx) 634 return 1; 635 636 amt = sizeof (*entry); 637 entry = (struct elf_link_local_dynamic_entry *) bfd_alloc (input_bfd, amt); 638 if (entry == NULL) 639 return 0; 640 641 /* Go find the symbol, so that we can find it's name. */ 642 if (!bfd_elf_get_elf_syms (input_bfd, &elf_tdata (input_bfd)->symtab_hdr, 643 1, input_indx, &entry->isym, esym, &eshndx)) 644 { 645 bfd_release (input_bfd, entry); 646 return 0; 647 } 648 649 if (entry->isym.st_shndx != SHN_UNDEF 650 && entry->isym.st_shndx < SHN_LORESERVE) 651 { 652 asection *s; 653 654 s = bfd_section_from_elf_index (input_bfd, entry->isym.st_shndx); 655 if (s == NULL || bfd_is_abs_section (s->output_section)) 656 { 657 /* We can still bfd_release here as nothing has done another 658 bfd_alloc. We can't do this later in this function. */ 659 bfd_release (input_bfd, entry); 660 return 2; 661 } 662 } 663 664 name = (bfd_elf_string_from_elf_section 665 (input_bfd, elf_tdata (input_bfd)->symtab_hdr.sh_link, 666 entry->isym.st_name)); 667 668 dynstr = elf_hash_table (info)->dynstr; 669 if (dynstr == NULL) 670 { 671 /* Create a strtab to hold the dynamic symbol names. */ 672 elf_hash_table (info)->dynstr = dynstr = _bfd_elf_strtab_init (); 673 if (dynstr == NULL) 674 return 0; 675 } 676 677 dynstr_index = _bfd_elf_strtab_add (dynstr, name, FALSE); 678 if (dynstr_index == (unsigned long) -1) 679 return 0; 680 entry->isym.st_name = dynstr_index; 681 682 eht = elf_hash_table (info); 683 684 entry->next = eht->dynlocal; 685 eht->dynlocal = entry; 686 entry->input_bfd = input_bfd; 687 entry->input_indx = input_indx; 688 eht->dynsymcount++; 689 690 /* Whatever binding the symbol had before, it's now local. */ 691 entry->isym.st_info 692 = ELF_ST_INFO (STB_LOCAL, ELF_ST_TYPE (entry->isym.st_info)); 693 694 /* The dynindx will be set at the end of size_dynamic_sections. */ 695 696 return 1; 697 } 698 699 /* Return the dynindex of a local dynamic symbol. */ 700 701 long 702 _bfd_elf_link_lookup_local_dynindx (struct bfd_link_info *info, 703 bfd *input_bfd, 704 long input_indx) 705 { 706 struct elf_link_local_dynamic_entry *e; 707 708 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 709 if (e->input_bfd == input_bfd && e->input_indx == input_indx) 710 return e->dynindx; 711 return -1; 712 } 713 714 /* This function is used to renumber the dynamic symbols, if some of 715 them are removed because they are marked as local. This is called 716 via elf_link_hash_traverse. */ 717 718 static bfd_boolean 719 elf_link_renumber_hash_table_dynsyms (struct elf_link_hash_entry *h, 720 void *data) 721 { 722 size_t *count = (size_t *) data; 723 724 if (h->forced_local) 725 return TRUE; 726 727 if (h->dynindx != -1) 728 h->dynindx = ++(*count); 729 730 return TRUE; 731 } 732 733 734 /* Like elf_link_renumber_hash_table_dynsyms, but just number symbols with 735 STB_LOCAL binding. */ 736 737 static bfd_boolean 738 elf_link_renumber_local_hash_table_dynsyms (struct elf_link_hash_entry *h, 739 void *data) 740 { 741 size_t *count = (size_t *) data; 742 743 if (!h->forced_local) 744 return TRUE; 745 746 if (h->dynindx != -1) 747 h->dynindx = ++(*count); 748 749 return TRUE; 750 } 751 752 /* Return true if the dynamic symbol for a given section should be 753 omitted when creating a shared library. */ 754 bfd_boolean 755 _bfd_elf_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED, 756 struct bfd_link_info *info, 757 asection *p) 758 { 759 struct elf_link_hash_table *htab; 760 761 switch (elf_section_data (p)->this_hdr.sh_type) 762 { 763 case SHT_PROGBITS: 764 case SHT_NOBITS: 765 /* If sh_type is yet undecided, assume it could be 766 SHT_PROGBITS/SHT_NOBITS. */ 767 case SHT_NULL: 768 htab = elf_hash_table (info); 769 if (p == htab->tls_sec) 770 return FALSE; 771 772 if (htab->text_index_section != NULL) 773 return p != htab->text_index_section && p != htab->data_index_section; 774 775 if (strcmp (p->name, ".got") == 0 776 || strcmp (p->name, ".got.plt") == 0 777 || strcmp (p->name, ".plt") == 0) 778 { 779 asection *ip; 780 781 if (htab->dynobj != NULL 782 && (ip = bfd_get_linker_section (htab->dynobj, p->name)) != NULL 783 && ip->output_section == p) 784 return TRUE; 785 } 786 return FALSE; 787 788 /* There shouldn't be section relative relocations 789 against any other section. */ 790 default: 791 return TRUE; 792 } 793 } 794 795 /* Assign dynsym indices. In a shared library we generate a section 796 symbol for each output section, which come first. Next come symbols 797 which have been forced to local binding. Then all of the back-end 798 allocated local dynamic syms, followed by the rest of the global 799 symbols. */ 800 801 static unsigned long 802 _bfd_elf_link_renumber_dynsyms (bfd *output_bfd, 803 struct bfd_link_info *info, 804 unsigned long *section_sym_count) 805 { 806 unsigned long dynsymcount = 0; 807 808 if (info->shared || elf_hash_table (info)->is_relocatable_executable) 809 { 810 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 811 asection *p; 812 for (p = output_bfd->sections; p ; p = p->next) 813 if ((p->flags & SEC_EXCLUDE) == 0 814 && (p->flags & SEC_ALLOC) != 0 815 && !(*bed->elf_backend_omit_section_dynsym) (output_bfd, info, p)) 816 elf_section_data (p)->dynindx = ++dynsymcount; 817 else 818 elf_section_data (p)->dynindx = 0; 819 } 820 *section_sym_count = dynsymcount; 821 822 elf_link_hash_traverse (elf_hash_table (info), 823 elf_link_renumber_local_hash_table_dynsyms, 824 &dynsymcount); 825 826 if (elf_hash_table (info)->dynlocal) 827 { 828 struct elf_link_local_dynamic_entry *p; 829 for (p = elf_hash_table (info)->dynlocal; p ; p = p->next) 830 p->dynindx = ++dynsymcount; 831 } 832 833 elf_link_hash_traverse (elf_hash_table (info), 834 elf_link_renumber_hash_table_dynsyms, 835 &dynsymcount); 836 837 /* There is an unused NULL entry at the head of the table which 838 we must account for in our count. Unless there weren't any 839 symbols, which means we'll have no table at all. */ 840 if (dynsymcount != 0) 841 ++dynsymcount; 842 843 elf_hash_table (info)->dynsymcount = dynsymcount; 844 return dynsymcount; 845 } 846 847 /* Merge st_other field. */ 848 849 static void 850 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h, 851 Elf_Internal_Sym *isym, bfd_boolean definition, 852 bfd_boolean dynamic) 853 { 854 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 855 856 /* If st_other has a processor-specific meaning, specific 857 code might be needed here. We never merge the visibility 858 attribute with the one from a dynamic object. */ 859 if (bed->elf_backend_merge_symbol_attribute) 860 (*bed->elf_backend_merge_symbol_attribute) (h, isym, definition, 861 dynamic); 862 863 /* If this symbol has default visibility and the user has requested 864 we not re-export it, then mark it as hidden. */ 865 if (definition 866 && !dynamic 867 && (abfd->no_export 868 || (abfd->my_archive && abfd->my_archive->no_export)) 869 && ELF_ST_VISIBILITY (isym->st_other) != STV_INTERNAL) 870 isym->st_other = (STV_HIDDEN 871 | (isym->st_other & ~ELF_ST_VISIBILITY (-1))); 872 873 if (!dynamic && ELF_ST_VISIBILITY (isym->st_other) != 0) 874 { 875 unsigned char hvis, symvis, other, nvis; 876 877 /* Only merge the visibility. Leave the remainder of the 878 st_other field to elf_backend_merge_symbol_attribute. */ 879 other = h->other & ~ELF_ST_VISIBILITY (-1); 880 881 /* Combine visibilities, using the most constraining one. */ 882 hvis = ELF_ST_VISIBILITY (h->other); 883 symvis = ELF_ST_VISIBILITY (isym->st_other); 884 if (! hvis) 885 nvis = symvis; 886 else if (! symvis) 887 nvis = hvis; 888 else 889 nvis = hvis < symvis ? hvis : symvis; 890 891 h->other = other | nvis; 892 } 893 } 894 895 /* This function is called when we want to define a new symbol. It 896 handles the various cases which arise when we find a definition in 897 a dynamic object, or when there is already a definition in a 898 dynamic object. The new symbol is described by NAME, SYM, PSEC, 899 and PVALUE. We set SYM_HASH to the hash table entry. We set 900 OVERRIDE if the old symbol is overriding a new definition. We set 901 TYPE_CHANGE_OK if it is OK for the type to change. We set 902 SIZE_CHANGE_OK if it is OK for the size to change. By OK to 903 change, we mean that we shouldn't warn if the type or size does 904 change. We set POLD_ALIGNMENT if an old common symbol in a dynamic 905 object is overridden by a regular object. */ 906 907 bfd_boolean 908 _bfd_elf_merge_symbol (bfd *abfd, 909 struct bfd_link_info *info, 910 const char *name, 911 Elf_Internal_Sym *sym, 912 asection **psec, 913 bfd_vma *pvalue, 914 bfd_boolean *pold_weak, 915 unsigned int *pold_alignment, 916 struct elf_link_hash_entry **sym_hash, 917 bfd_boolean *skip, 918 bfd_boolean *override, 919 bfd_boolean *type_change_ok, 920 bfd_boolean *size_change_ok) 921 { 922 asection *sec, *oldsec; 923 struct elf_link_hash_entry *h; 924 struct elf_link_hash_entry *hi; 925 struct elf_link_hash_entry *flip; 926 int bind; 927 bfd *oldbfd; 928 bfd_boolean newdyn, olddyn, olddef, newdef, newdyncommon, olddyncommon; 929 bfd_boolean newweak, oldweak, newfunc, oldfunc; 930 const struct elf_backend_data *bed; 931 932 *skip = FALSE; 933 *override = FALSE; 934 935 sec = *psec; 936 bind = ELF_ST_BIND (sym->st_info); 937 938 /* Silently discard TLS symbols from --just-syms. There's no way to 939 combine a static TLS block with a new TLS block for this executable. */ 940 if (ELF_ST_TYPE (sym->st_info) == STT_TLS 941 && sec->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 942 { 943 *skip = TRUE; 944 return TRUE; 945 } 946 947 if (! bfd_is_und_section (sec)) 948 h = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, FALSE, FALSE); 949 else 950 h = ((struct elf_link_hash_entry *) 951 bfd_wrapped_link_hash_lookup (abfd, info, name, TRUE, FALSE, FALSE)); 952 if (h == NULL) 953 return FALSE; 954 *sym_hash = h; 955 956 bed = get_elf_backend_data (abfd); 957 958 /* This code is for coping with dynamic objects, and is only useful 959 if we are doing an ELF link. */ 960 if (!(*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 961 return TRUE; 962 963 /* For merging, we only care about real symbols. But we need to make 964 sure that indirect symbol dynamic flags are updated. */ 965 hi = h; 966 while (h->root.type == bfd_link_hash_indirect 967 || h->root.type == bfd_link_hash_warning) 968 h = (struct elf_link_hash_entry *) h->root.u.i.link; 969 970 /* We have to check it for every instance since the first few may be 971 references and not all compilers emit symbol type for undefined 972 symbols. */ 973 bfd_elf_link_mark_dynamic_symbol (info, h, sym); 974 975 /* NEWDYN and OLDDYN indicate whether the new or old symbol, 976 respectively, is from a dynamic object. */ 977 978 newdyn = (abfd->flags & DYNAMIC) != 0; 979 980 /* ref_dynamic_nonweak and dynamic_def flags track actual undefined 981 syms and defined syms in dynamic libraries respectively. 982 ref_dynamic on the other hand can be set for a symbol defined in 983 a dynamic library, and def_dynamic may not be set; When the 984 definition in a dynamic lib is overridden by a definition in the 985 executable use of the symbol in the dynamic lib becomes a 986 reference to the executable symbol. */ 987 if (newdyn) 988 { 989 if (bfd_is_und_section (sec)) 990 { 991 if (bind != STB_WEAK) 992 { 993 h->ref_dynamic_nonweak = 1; 994 hi->ref_dynamic_nonweak = 1; 995 } 996 } 997 else 998 { 999 h->dynamic_def = 1; 1000 hi->dynamic_def = 1; 1001 } 1002 } 1003 1004 /* If we just created the symbol, mark it as being an ELF symbol. 1005 Other than that, there is nothing to do--there is no merge issue 1006 with a newly defined symbol--so we just return. */ 1007 1008 if (h->root.type == bfd_link_hash_new) 1009 { 1010 h->non_elf = 0; 1011 return TRUE; 1012 } 1013 1014 /* OLDBFD and OLDSEC are a BFD and an ASECTION associated with the 1015 existing symbol. */ 1016 1017 switch (h->root.type) 1018 { 1019 default: 1020 oldbfd = NULL; 1021 oldsec = NULL; 1022 break; 1023 1024 case bfd_link_hash_undefined: 1025 case bfd_link_hash_undefweak: 1026 oldbfd = h->root.u.undef.abfd; 1027 oldsec = NULL; 1028 break; 1029 1030 case bfd_link_hash_defined: 1031 case bfd_link_hash_defweak: 1032 oldbfd = h->root.u.def.section->owner; 1033 oldsec = h->root.u.def.section; 1034 break; 1035 1036 case bfd_link_hash_common: 1037 oldbfd = h->root.u.c.p->section->owner; 1038 oldsec = h->root.u.c.p->section; 1039 break; 1040 } 1041 1042 /* Differentiate strong and weak symbols. */ 1043 newweak = bind == STB_WEAK; 1044 oldweak = (h->root.type == bfd_link_hash_defweak 1045 || h->root.type == bfd_link_hash_undefweak); 1046 if (pold_weak) 1047 *pold_weak = oldweak; 1048 1049 /* In cases involving weak versioned symbols, we may wind up trying 1050 to merge a symbol with itself. Catch that here, to avoid the 1051 confusion that results if we try to override a symbol with 1052 itself. The additional tests catch cases like 1053 _GLOBAL_OFFSET_TABLE_, which are regular symbols defined in a 1054 dynamic object, which we do want to handle here. */ 1055 if (abfd == oldbfd 1056 && (newweak || oldweak) 1057 && ((abfd->flags & DYNAMIC) == 0 1058 || !h->def_regular)) 1059 return TRUE; 1060 1061 olddyn = FALSE; 1062 if (oldbfd != NULL) 1063 olddyn = (oldbfd->flags & DYNAMIC) != 0; 1064 else if (oldsec != NULL) 1065 { 1066 /* This handles the special SHN_MIPS_{TEXT,DATA} section 1067 indices used by MIPS ELF. */ 1068 olddyn = (oldsec->symbol->flags & BSF_DYNAMIC) != 0; 1069 } 1070 1071 /* NEWDEF and OLDDEF indicate whether the new or old symbol, 1072 respectively, appear to be a definition rather than reference. */ 1073 1074 newdef = !bfd_is_und_section (sec) && !bfd_is_com_section (sec); 1075 1076 olddef = (h->root.type != bfd_link_hash_undefined 1077 && h->root.type != bfd_link_hash_undefweak 1078 && h->root.type != bfd_link_hash_common); 1079 1080 /* NEWFUNC and OLDFUNC indicate whether the new or old symbol, 1081 respectively, appear to be a function. */ 1082 1083 newfunc = (ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1084 && bed->is_function_type (ELF_ST_TYPE (sym->st_info))); 1085 1086 oldfunc = (h->type != STT_NOTYPE 1087 && bed->is_function_type (h->type)); 1088 1089 /* When we try to create a default indirect symbol from the dynamic 1090 definition with the default version, we skip it if its type and 1091 the type of existing regular definition mismatch. We only do it 1092 if the existing regular definition won't be dynamic. */ 1093 if (pold_alignment == NULL 1094 && !info->shared 1095 && !info->export_dynamic 1096 && !h->ref_dynamic 1097 && newdyn 1098 && newdef 1099 && !olddyn 1100 && (olddef || h->root.type == bfd_link_hash_common) 1101 && ELF_ST_TYPE (sym->st_info) != h->type 1102 && ELF_ST_TYPE (sym->st_info) != STT_NOTYPE 1103 && h->type != STT_NOTYPE 1104 && !(newfunc && oldfunc)) 1105 { 1106 *skip = TRUE; 1107 return TRUE; 1108 } 1109 1110 /* Plugin symbol type isn't currently set. Stop bogus errors. */ 1111 if (oldbfd != NULL && (oldbfd->flags & BFD_PLUGIN) != 0) 1112 *type_change_ok = TRUE; 1113 1114 /* Check TLS symbol. We don't check undefined symbol introduced by 1115 "ld -u". */ 1116 else if (oldbfd != NULL 1117 && ELF_ST_TYPE (sym->st_info) != h->type 1118 && (ELF_ST_TYPE (sym->st_info) == STT_TLS || h->type == STT_TLS)) 1119 { 1120 bfd *ntbfd, *tbfd; 1121 bfd_boolean ntdef, tdef; 1122 asection *ntsec, *tsec; 1123 1124 if (h->type == STT_TLS) 1125 { 1126 ntbfd = abfd; 1127 ntsec = sec; 1128 ntdef = newdef; 1129 tbfd = oldbfd; 1130 tsec = oldsec; 1131 tdef = olddef; 1132 } 1133 else 1134 { 1135 ntbfd = oldbfd; 1136 ntsec = oldsec; 1137 ntdef = olddef; 1138 tbfd = abfd; 1139 tsec = sec; 1140 tdef = newdef; 1141 } 1142 1143 if (tdef && ntdef) 1144 (*_bfd_error_handler) 1145 (_("%s: TLS definition in %B section %A mismatches non-TLS definition in %B section %A"), 1146 tbfd, tsec, ntbfd, ntsec, h->root.root.string); 1147 else if (!tdef && !ntdef) 1148 (*_bfd_error_handler) 1149 (_("%s: TLS reference in %B mismatches non-TLS reference in %B"), 1150 tbfd, ntbfd, h->root.root.string); 1151 else if (tdef) 1152 (*_bfd_error_handler) 1153 (_("%s: TLS definition in %B section %A mismatches non-TLS reference in %B"), 1154 tbfd, tsec, ntbfd, h->root.root.string); 1155 else 1156 (*_bfd_error_handler) 1157 (_("%s: TLS reference in %B mismatches non-TLS definition in %B section %A"), 1158 tbfd, ntbfd, ntsec, h->root.root.string); 1159 1160 bfd_set_error (bfd_error_bad_value); 1161 return FALSE; 1162 } 1163 1164 /* If the old symbol has non-default visibility, we ignore the new 1165 definition from a dynamic object. */ 1166 if (newdyn 1167 && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 1168 && !bfd_is_und_section (sec)) 1169 { 1170 *skip = TRUE; 1171 /* Make sure this symbol is dynamic. */ 1172 h->ref_dynamic = 1; 1173 hi->ref_dynamic = 1; 1174 /* A protected symbol has external availability. Make sure it is 1175 recorded as dynamic. 1176 1177 FIXME: Should we check type and size for protected symbol? */ 1178 if (ELF_ST_VISIBILITY (h->other) == STV_PROTECTED) 1179 return bfd_elf_link_record_dynamic_symbol (info, h); 1180 else 1181 return TRUE; 1182 } 1183 else if (!newdyn 1184 && ELF_ST_VISIBILITY (sym->st_other) != STV_DEFAULT 1185 && h->def_dynamic) 1186 { 1187 /* If the new symbol with non-default visibility comes from a 1188 relocatable file and the old definition comes from a dynamic 1189 object, we remove the old definition. */ 1190 if ((*sym_hash)->root.type == bfd_link_hash_indirect) 1191 { 1192 /* Handle the case where the old dynamic definition is 1193 default versioned. We need to copy the symbol info from 1194 the symbol with default version to the normal one if it 1195 was referenced before. */ 1196 if (h->ref_regular) 1197 { 1198 struct elf_link_hash_entry *vh = *sym_hash; 1199 1200 vh->root.type = h->root.type; 1201 h->root.type = bfd_link_hash_indirect; 1202 (*bed->elf_backend_copy_indirect_symbol) (info, vh, h); 1203 1204 h->root.u.i.link = (struct bfd_link_hash_entry *) vh; 1205 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1206 { 1207 /* If the new symbol is hidden or internal, completely undo 1208 any dynamic link state. */ 1209 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1210 h->forced_local = 0; 1211 h->ref_dynamic = 0; 1212 } 1213 else 1214 h->ref_dynamic = 1; 1215 1216 h->def_dynamic = 0; 1217 /* FIXME: Should we check type and size for protected symbol? */ 1218 h->size = 0; 1219 h->type = 0; 1220 1221 h = vh; 1222 } 1223 else 1224 h = *sym_hash; 1225 } 1226 1227 /* If the old symbol was undefined before, then it will still be 1228 on the undefs list. If the new symbol is undefined or 1229 common, we can't make it bfd_link_hash_new here, because new 1230 undefined or common symbols will be added to the undefs list 1231 by _bfd_generic_link_add_one_symbol. Symbols may not be 1232 added twice to the undefs list. Also, if the new symbol is 1233 undefweak then we don't want to lose the strong undef. */ 1234 if (h->root.u.undef.next || info->hash->undefs_tail == &h->root) 1235 { 1236 h->root.type = bfd_link_hash_undefined; 1237 h->root.u.undef.abfd = abfd; 1238 } 1239 else 1240 { 1241 h->root.type = bfd_link_hash_new; 1242 h->root.u.undef.abfd = NULL; 1243 } 1244 1245 if (ELF_ST_VISIBILITY (sym->st_other) != STV_PROTECTED) 1246 { 1247 /* If the new symbol is hidden or internal, completely undo 1248 any dynamic link state. */ 1249 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1250 h->forced_local = 0; 1251 h->ref_dynamic = 0; 1252 } 1253 else 1254 h->ref_dynamic = 1; 1255 h->def_dynamic = 0; 1256 /* FIXME: Should we check type and size for protected symbol? */ 1257 h->size = 0; 1258 h->type = 0; 1259 return TRUE; 1260 } 1261 1262 if (bind == STB_GNU_UNIQUE) 1263 h->unique_global = 1; 1264 1265 /* If a new weak symbol definition comes from a regular file and the 1266 old symbol comes from a dynamic library, we treat the new one as 1267 strong. Similarly, an old weak symbol definition from a regular 1268 file is treated as strong when the new symbol comes from a dynamic 1269 library. Further, an old weak symbol from a dynamic library is 1270 treated as strong if the new symbol is from a dynamic library. 1271 This reflects the way glibc's ld.so works. 1272 1273 Do this before setting *type_change_ok or *size_change_ok so that 1274 we warn properly when dynamic library symbols are overridden. */ 1275 1276 if (newdef && !newdyn && olddyn) 1277 newweak = FALSE; 1278 if (olddef && newdyn) 1279 oldweak = FALSE; 1280 1281 /* Allow changes between different types of function symbol. */ 1282 if (newfunc && oldfunc) 1283 *type_change_ok = TRUE; 1284 1285 /* It's OK to change the type if either the existing symbol or the 1286 new symbol is weak. A type change is also OK if the old symbol 1287 is undefined and the new symbol is defined. */ 1288 1289 if (oldweak 1290 || newweak 1291 || (newdef 1292 && h->root.type == bfd_link_hash_undefined)) 1293 *type_change_ok = TRUE; 1294 1295 /* It's OK to change the size if either the existing symbol or the 1296 new symbol is weak, or if the old symbol is undefined. */ 1297 1298 if (*type_change_ok 1299 || h->root.type == bfd_link_hash_undefined) 1300 *size_change_ok = TRUE; 1301 1302 /* NEWDYNCOMMON and OLDDYNCOMMON indicate whether the new or old 1303 symbol, respectively, appears to be a common symbol in a dynamic 1304 object. If a symbol appears in an uninitialized section, and is 1305 not weak, and is not a function, then it may be a common symbol 1306 which was resolved when the dynamic object was created. We want 1307 to treat such symbols specially, because they raise special 1308 considerations when setting the symbol size: if the symbol 1309 appears as a common symbol in a regular object, and the size in 1310 the regular object is larger, we must make sure that we use the 1311 larger size. This problematic case can always be avoided in C, 1312 but it must be handled correctly when using Fortran shared 1313 libraries. 1314 1315 Note that if NEWDYNCOMMON is set, NEWDEF will be set, and 1316 likewise for OLDDYNCOMMON and OLDDEF. 1317 1318 Note that this test is just a heuristic, and that it is quite 1319 possible to have an uninitialized symbol in a shared object which 1320 is really a definition, rather than a common symbol. This could 1321 lead to some minor confusion when the symbol really is a common 1322 symbol in some regular object. However, I think it will be 1323 harmless. */ 1324 1325 if (newdyn 1326 && newdef 1327 && !newweak 1328 && (sec->flags & SEC_ALLOC) != 0 1329 && (sec->flags & SEC_LOAD) == 0 1330 && sym->st_size > 0 1331 && !newfunc) 1332 newdyncommon = TRUE; 1333 else 1334 newdyncommon = FALSE; 1335 1336 if (olddyn 1337 && olddef 1338 && h->root.type == bfd_link_hash_defined 1339 && h->def_dynamic 1340 && (h->root.u.def.section->flags & SEC_ALLOC) != 0 1341 && (h->root.u.def.section->flags & SEC_LOAD) == 0 1342 && h->size > 0 1343 && !oldfunc) 1344 olddyncommon = TRUE; 1345 else 1346 olddyncommon = FALSE; 1347 1348 /* We now know everything about the old and new symbols. We ask the 1349 backend to check if we can merge them. */ 1350 if (bed->merge_symbol 1351 && !bed->merge_symbol (info, sym_hash, h, sym, psec, pvalue, 1352 pold_alignment, skip, override, 1353 type_change_ok, size_change_ok, 1354 &newdyn, &newdef, &newdyncommon, &newweak, 1355 abfd, &sec, 1356 &olddyn, &olddef, &olddyncommon, &oldweak, 1357 oldbfd, &oldsec)) 1358 return FALSE; 1359 1360 /* If both the old and the new symbols look like common symbols in a 1361 dynamic object, set the size of the symbol to the larger of the 1362 two. */ 1363 1364 if (olddyncommon 1365 && newdyncommon 1366 && sym->st_size != h->size) 1367 { 1368 /* Since we think we have two common symbols, issue a multiple 1369 common warning if desired. Note that we only warn if the 1370 size is different. If the size is the same, we simply let 1371 the old symbol override the new one as normally happens with 1372 symbols defined in dynamic objects. */ 1373 1374 if (! ((*info->callbacks->multiple_common) 1375 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size))) 1376 return FALSE; 1377 1378 if (sym->st_size > h->size) 1379 h->size = sym->st_size; 1380 1381 *size_change_ok = TRUE; 1382 } 1383 1384 /* If we are looking at a dynamic object, and we have found a 1385 definition, we need to see if the symbol was already defined by 1386 some other object. If so, we want to use the existing 1387 definition, and we do not want to report a multiple symbol 1388 definition error; we do this by clobbering *PSEC to be 1389 bfd_und_section_ptr. 1390 1391 We treat a common symbol as a definition if the symbol in the 1392 shared library is a function, since common symbols always 1393 represent variables; this can cause confusion in principle, but 1394 any such confusion would seem to indicate an erroneous program or 1395 shared library. We also permit a common symbol in a regular 1396 object to override a weak symbol in a shared object. */ 1397 1398 if (newdyn 1399 && newdef 1400 && (olddef 1401 || (h->root.type == bfd_link_hash_common 1402 && (newweak || newfunc)))) 1403 { 1404 *override = TRUE; 1405 newdef = FALSE; 1406 newdyncommon = FALSE; 1407 1408 *psec = sec = bfd_und_section_ptr; 1409 *size_change_ok = TRUE; 1410 1411 /* If we get here when the old symbol is a common symbol, then 1412 we are explicitly letting it override a weak symbol or 1413 function in a dynamic object, and we don't want to warn about 1414 a type change. If the old symbol is a defined symbol, a type 1415 change warning may still be appropriate. */ 1416 1417 if (h->root.type == bfd_link_hash_common) 1418 *type_change_ok = TRUE; 1419 } 1420 1421 /* Handle the special case of an old common symbol merging with a 1422 new symbol which looks like a common symbol in a shared object. 1423 We change *PSEC and *PVALUE to make the new symbol look like a 1424 common symbol, and let _bfd_generic_link_add_one_symbol do the 1425 right thing. */ 1426 1427 if (newdyncommon 1428 && h->root.type == bfd_link_hash_common) 1429 { 1430 *override = TRUE; 1431 newdef = FALSE; 1432 newdyncommon = FALSE; 1433 *pvalue = sym->st_size; 1434 *psec = sec = bed->common_section (oldsec); 1435 *size_change_ok = TRUE; 1436 } 1437 1438 /* Skip weak definitions of symbols that are already defined. */ 1439 if (newdef && olddef && newweak) 1440 { 1441 /* Don't skip new non-IR weak syms. */ 1442 if (!(oldbfd != NULL 1443 && (oldbfd->flags & BFD_PLUGIN) != 0 1444 && (abfd->flags & BFD_PLUGIN) == 0)) 1445 *skip = TRUE; 1446 1447 /* Merge st_other. If the symbol already has a dynamic index, 1448 but visibility says it should not be visible, turn it into a 1449 local symbol. */ 1450 elf_merge_st_other (abfd, h, sym, newdef, newdyn); 1451 if (h->dynindx != -1) 1452 switch (ELF_ST_VISIBILITY (h->other)) 1453 { 1454 case STV_INTERNAL: 1455 case STV_HIDDEN: 1456 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 1457 break; 1458 } 1459 } 1460 1461 /* If the old symbol is from a dynamic object, and the new symbol is 1462 a definition which is not from a dynamic object, then the new 1463 symbol overrides the old symbol. Symbols from regular files 1464 always take precedence over symbols from dynamic objects, even if 1465 they are defined after the dynamic object in the link. 1466 1467 As above, we again permit a common symbol in a regular object to 1468 override a definition in a shared object if the shared object 1469 symbol is a function or is weak. */ 1470 1471 flip = NULL; 1472 if (!newdyn 1473 && (newdef 1474 || (bfd_is_com_section (sec) 1475 && (oldweak || oldfunc))) 1476 && olddyn 1477 && olddef 1478 && h->def_dynamic) 1479 { 1480 /* Change the hash table entry to undefined, and let 1481 _bfd_generic_link_add_one_symbol do the right thing with the 1482 new definition. */ 1483 1484 h->root.type = bfd_link_hash_undefined; 1485 h->root.u.undef.abfd = h->root.u.def.section->owner; 1486 *size_change_ok = TRUE; 1487 1488 olddef = FALSE; 1489 olddyncommon = FALSE; 1490 1491 /* We again permit a type change when a common symbol may be 1492 overriding a function. */ 1493 1494 if (bfd_is_com_section (sec)) 1495 { 1496 if (oldfunc) 1497 { 1498 /* If a common symbol overrides a function, make sure 1499 that it isn't defined dynamically nor has type 1500 function. */ 1501 h->def_dynamic = 0; 1502 h->type = STT_NOTYPE; 1503 } 1504 *type_change_ok = TRUE; 1505 } 1506 1507 if ((*sym_hash)->root.type == bfd_link_hash_indirect) 1508 flip = *sym_hash; 1509 else 1510 /* This union may have been set to be non-NULL when this symbol 1511 was seen in a dynamic object. We must force the union to be 1512 NULL, so that it is correct for a regular symbol. */ 1513 h->verinfo.vertree = NULL; 1514 } 1515 1516 /* Handle the special case of a new common symbol merging with an 1517 old symbol that looks like it might be a common symbol defined in 1518 a shared object. Note that we have already handled the case in 1519 which a new common symbol should simply override the definition 1520 in the shared library. */ 1521 1522 if (! newdyn 1523 && bfd_is_com_section (sec) 1524 && olddyncommon) 1525 { 1526 /* It would be best if we could set the hash table entry to a 1527 common symbol, but we don't know what to use for the section 1528 or the alignment. */ 1529 if (! ((*info->callbacks->multiple_common) 1530 (info, &h->root, abfd, bfd_link_hash_common, sym->st_size))) 1531 return FALSE; 1532 1533 /* If the presumed common symbol in the dynamic object is 1534 larger, pretend that the new symbol has its size. */ 1535 1536 if (h->size > *pvalue) 1537 *pvalue = h->size; 1538 1539 /* We need to remember the alignment required by the symbol 1540 in the dynamic object. */ 1541 BFD_ASSERT (pold_alignment); 1542 *pold_alignment = h->root.u.def.section->alignment_power; 1543 1544 olddef = FALSE; 1545 olddyncommon = FALSE; 1546 1547 h->root.type = bfd_link_hash_undefined; 1548 h->root.u.undef.abfd = h->root.u.def.section->owner; 1549 1550 *size_change_ok = TRUE; 1551 *type_change_ok = TRUE; 1552 1553 if ((*sym_hash)->root.type == bfd_link_hash_indirect) 1554 flip = *sym_hash; 1555 else 1556 h->verinfo.vertree = NULL; 1557 } 1558 1559 if (flip != NULL) 1560 { 1561 /* Handle the case where we had a versioned symbol in a dynamic 1562 library and now find a definition in a normal object. In this 1563 case, we make the versioned symbol point to the normal one. */ 1564 flip->root.type = h->root.type; 1565 flip->root.u.undef.abfd = h->root.u.undef.abfd; 1566 h->root.type = bfd_link_hash_indirect; 1567 h->root.u.i.link = (struct bfd_link_hash_entry *) flip; 1568 (*bed->elf_backend_copy_indirect_symbol) (info, flip, h); 1569 if (h->def_dynamic) 1570 { 1571 h->def_dynamic = 0; 1572 flip->ref_dynamic = 1; 1573 } 1574 } 1575 1576 return TRUE; 1577 } 1578 1579 /* This function is called to create an indirect symbol from the 1580 default for the symbol with the default version if needed. The 1581 symbol is described by H, NAME, SYM, PSEC, VALUE, and OVERRIDE. We 1582 set DYNSYM if the new indirect symbol is dynamic. */ 1583 1584 static bfd_boolean 1585 _bfd_elf_add_default_symbol (bfd *abfd, 1586 struct bfd_link_info *info, 1587 struct elf_link_hash_entry *h, 1588 const char *name, 1589 Elf_Internal_Sym *sym, 1590 asection **psec, 1591 bfd_vma *value, 1592 bfd_boolean *dynsym, 1593 bfd_boolean override) 1594 { 1595 bfd_boolean type_change_ok; 1596 bfd_boolean size_change_ok; 1597 bfd_boolean skip; 1598 char *shortname; 1599 struct elf_link_hash_entry *hi; 1600 struct bfd_link_hash_entry *bh; 1601 const struct elf_backend_data *bed; 1602 bfd_boolean collect; 1603 bfd_boolean dynamic; 1604 char *p; 1605 size_t len, shortlen; 1606 asection *sec; 1607 1608 /* If this symbol has a version, and it is the default version, we 1609 create an indirect symbol from the default name to the fully 1610 decorated name. This will cause external references which do not 1611 specify a version to be bound to this version of the symbol. */ 1612 p = strchr (name, ELF_VER_CHR); 1613 if (p == NULL || p[1] != ELF_VER_CHR) 1614 return TRUE; 1615 1616 if (override) 1617 { 1618 /* We are overridden by an old definition. We need to check if we 1619 need to create the indirect symbol from the default name. */ 1620 hi = elf_link_hash_lookup (elf_hash_table (info), name, TRUE, 1621 FALSE, FALSE); 1622 BFD_ASSERT (hi != NULL); 1623 if (hi == h) 1624 return TRUE; 1625 while (hi->root.type == bfd_link_hash_indirect 1626 || hi->root.type == bfd_link_hash_warning) 1627 { 1628 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1629 if (hi == h) 1630 return TRUE; 1631 } 1632 } 1633 1634 bed = get_elf_backend_data (abfd); 1635 collect = bed->collect; 1636 dynamic = (abfd->flags & DYNAMIC) != 0; 1637 1638 shortlen = p - name; 1639 shortname = (char *) bfd_hash_allocate (&info->hash->table, shortlen + 1); 1640 if (shortname == NULL) 1641 return FALSE; 1642 memcpy (shortname, name, shortlen); 1643 shortname[shortlen] = '\0'; 1644 1645 /* We are going to create a new symbol. Merge it with any existing 1646 symbol with this name. For the purposes of the merge, act as 1647 though we were defining the symbol we just defined, although we 1648 actually going to define an indirect symbol. */ 1649 type_change_ok = FALSE; 1650 size_change_ok = FALSE; 1651 sec = *psec; 1652 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, 1653 NULL, NULL, &hi, &skip, &override, 1654 &type_change_ok, &size_change_ok)) 1655 return FALSE; 1656 1657 if (skip) 1658 goto nondefault; 1659 1660 if (! override) 1661 { 1662 bh = &hi->root; 1663 if (! (_bfd_generic_link_add_one_symbol 1664 (info, abfd, shortname, BSF_INDIRECT, bfd_ind_section_ptr, 1665 0, name, FALSE, collect, &bh))) 1666 return FALSE; 1667 hi = (struct elf_link_hash_entry *) bh; 1668 } 1669 else 1670 { 1671 /* In this case the symbol named SHORTNAME is overriding the 1672 indirect symbol we want to add. We were planning on making 1673 SHORTNAME an indirect symbol referring to NAME. SHORTNAME 1674 is the name without a version. NAME is the fully versioned 1675 name, and it is the default version. 1676 1677 Overriding means that we already saw a definition for the 1678 symbol SHORTNAME in a regular object, and it is overriding 1679 the symbol defined in the dynamic object. 1680 1681 When this happens, we actually want to change NAME, the 1682 symbol we just added, to refer to SHORTNAME. This will cause 1683 references to NAME in the shared object to become references 1684 to SHORTNAME in the regular object. This is what we expect 1685 when we override a function in a shared object: that the 1686 references in the shared object will be mapped to the 1687 definition in the regular object. */ 1688 1689 while (hi->root.type == bfd_link_hash_indirect 1690 || hi->root.type == bfd_link_hash_warning) 1691 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1692 1693 h->root.type = bfd_link_hash_indirect; 1694 h->root.u.i.link = (struct bfd_link_hash_entry *) hi; 1695 if (h->def_dynamic) 1696 { 1697 h->def_dynamic = 0; 1698 hi->ref_dynamic = 1; 1699 if (hi->ref_regular 1700 || hi->def_regular) 1701 { 1702 if (! bfd_elf_link_record_dynamic_symbol (info, hi)) 1703 return FALSE; 1704 } 1705 } 1706 1707 /* Now set HI to H, so that the following code will set the 1708 other fields correctly. */ 1709 hi = h; 1710 } 1711 1712 /* Check if HI is a warning symbol. */ 1713 if (hi->root.type == bfd_link_hash_warning) 1714 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 1715 1716 /* If there is a duplicate definition somewhere, then HI may not 1717 point to an indirect symbol. We will have reported an error to 1718 the user in that case. */ 1719 1720 if (hi->root.type == bfd_link_hash_indirect) 1721 { 1722 struct elf_link_hash_entry *ht; 1723 1724 ht = (struct elf_link_hash_entry *) hi->root.u.i.link; 1725 (*bed->elf_backend_copy_indirect_symbol) (info, ht, hi); 1726 1727 /* See if the new flags lead us to realize that the symbol must 1728 be dynamic. */ 1729 if (! *dynsym) 1730 { 1731 if (! dynamic) 1732 { 1733 if (! info->executable 1734 || hi->def_dynamic 1735 || hi->ref_dynamic) 1736 *dynsym = TRUE; 1737 } 1738 else 1739 { 1740 if (hi->ref_regular) 1741 *dynsym = TRUE; 1742 } 1743 } 1744 } 1745 1746 /* We also need to define an indirection from the nondefault version 1747 of the symbol. */ 1748 1749 nondefault: 1750 len = strlen (name); 1751 shortname = (char *) bfd_hash_allocate (&info->hash->table, len); 1752 if (shortname == NULL) 1753 return FALSE; 1754 memcpy (shortname, name, shortlen); 1755 memcpy (shortname + shortlen, p + 1, len - shortlen); 1756 1757 /* Once again, merge with any existing symbol. */ 1758 type_change_ok = FALSE; 1759 size_change_ok = FALSE; 1760 sec = *psec; 1761 if (!_bfd_elf_merge_symbol (abfd, info, shortname, sym, &sec, value, 1762 NULL, NULL, &hi, &skip, &override, 1763 &type_change_ok, &size_change_ok)) 1764 return FALSE; 1765 1766 if (skip) 1767 return TRUE; 1768 1769 if (override) 1770 { 1771 /* Here SHORTNAME is a versioned name, so we don't expect to see 1772 the type of override we do in the case above unless it is 1773 overridden by a versioned definition. */ 1774 if (hi->root.type != bfd_link_hash_defined 1775 && hi->root.type != bfd_link_hash_defweak) 1776 (*_bfd_error_handler) 1777 (_("%B: unexpected redefinition of indirect versioned symbol `%s'"), 1778 abfd, shortname); 1779 } 1780 else 1781 { 1782 bh = &hi->root; 1783 if (! (_bfd_generic_link_add_one_symbol 1784 (info, abfd, shortname, BSF_INDIRECT, 1785 bfd_ind_section_ptr, 0, name, FALSE, collect, &bh))) 1786 return FALSE; 1787 hi = (struct elf_link_hash_entry *) bh; 1788 1789 /* If there is a duplicate definition somewhere, then HI may not 1790 point to an indirect symbol. We will have reported an error 1791 to the user in that case. */ 1792 1793 if (hi->root.type == bfd_link_hash_indirect) 1794 { 1795 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 1796 1797 /* See if the new flags lead us to realize that the symbol 1798 must be dynamic. */ 1799 if (! *dynsym) 1800 { 1801 if (! dynamic) 1802 { 1803 if (! info->executable 1804 || hi->ref_dynamic) 1805 *dynsym = TRUE; 1806 } 1807 else 1808 { 1809 if (hi->ref_regular) 1810 *dynsym = TRUE; 1811 } 1812 } 1813 } 1814 } 1815 1816 return TRUE; 1817 } 1818 1819 /* This routine is used to export all defined symbols into the dynamic 1820 symbol table. It is called via elf_link_hash_traverse. */ 1821 1822 static bfd_boolean 1823 _bfd_elf_export_symbol (struct elf_link_hash_entry *h, void *data) 1824 { 1825 struct elf_info_failed *eif = (struct elf_info_failed *) data; 1826 1827 /* Ignore indirect symbols. These are added by the versioning code. */ 1828 if (h->root.type == bfd_link_hash_indirect) 1829 return TRUE; 1830 1831 /* Ignore this if we won't export it. */ 1832 if (!eif->info->export_dynamic && !h->dynamic) 1833 return TRUE; 1834 1835 if (h->dynindx == -1 1836 && (h->def_regular || h->ref_regular) 1837 && ! bfd_hide_sym_by_version (eif->info->version_info, 1838 h->root.root.string)) 1839 { 1840 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 1841 { 1842 eif->failed = TRUE; 1843 return FALSE; 1844 } 1845 } 1846 1847 return TRUE; 1848 } 1849 1850 /* Look through the symbols which are defined in other shared 1851 libraries and referenced here. Update the list of version 1852 dependencies. This will be put into the .gnu.version_r section. 1853 This function is called via elf_link_hash_traverse. */ 1854 1855 static bfd_boolean 1856 _bfd_elf_link_find_version_dependencies (struct elf_link_hash_entry *h, 1857 void *data) 1858 { 1859 struct elf_find_verdep_info *rinfo = (struct elf_find_verdep_info *) data; 1860 Elf_Internal_Verneed *t; 1861 Elf_Internal_Vernaux *a; 1862 bfd_size_type amt; 1863 1864 /* We only care about symbols defined in shared objects with version 1865 information. */ 1866 if (!h->def_dynamic 1867 || h->def_regular 1868 || h->dynindx == -1 1869 || h->verinfo.verdef == NULL) 1870 return TRUE; 1871 1872 /* See if we already know about this version. */ 1873 for (t = elf_tdata (rinfo->info->output_bfd)->verref; 1874 t != NULL; 1875 t = t->vn_nextref) 1876 { 1877 if (t->vn_bfd != h->verinfo.verdef->vd_bfd) 1878 continue; 1879 1880 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 1881 if (a->vna_nodename == h->verinfo.verdef->vd_nodename) 1882 return TRUE; 1883 1884 break; 1885 } 1886 1887 /* This is a new version. Add it to tree we are building. */ 1888 1889 if (t == NULL) 1890 { 1891 amt = sizeof *t; 1892 t = (Elf_Internal_Verneed *) bfd_zalloc (rinfo->info->output_bfd, amt); 1893 if (t == NULL) 1894 { 1895 rinfo->failed = TRUE; 1896 return FALSE; 1897 } 1898 1899 t->vn_bfd = h->verinfo.verdef->vd_bfd; 1900 t->vn_nextref = elf_tdata (rinfo->info->output_bfd)->verref; 1901 elf_tdata (rinfo->info->output_bfd)->verref = t; 1902 } 1903 1904 amt = sizeof *a; 1905 a = (Elf_Internal_Vernaux *) bfd_zalloc (rinfo->info->output_bfd, amt); 1906 if (a == NULL) 1907 { 1908 rinfo->failed = TRUE; 1909 return FALSE; 1910 } 1911 1912 /* Note that we are copying a string pointer here, and testing it 1913 above. If bfd_elf_string_from_elf_section is ever changed to 1914 discard the string data when low in memory, this will have to be 1915 fixed. */ 1916 a->vna_nodename = h->verinfo.verdef->vd_nodename; 1917 1918 a->vna_flags = h->verinfo.verdef->vd_flags; 1919 a->vna_nextptr = t->vn_auxptr; 1920 1921 h->verinfo.verdef->vd_exp_refno = rinfo->vers; 1922 ++rinfo->vers; 1923 1924 a->vna_other = h->verinfo.verdef->vd_exp_refno + 1; 1925 1926 t->vn_auxptr = a; 1927 1928 return TRUE; 1929 } 1930 1931 /* Figure out appropriate versions for all the symbols. We may not 1932 have the version number script until we have read all of the input 1933 files, so until that point we don't know which symbols should be 1934 local. This function is called via elf_link_hash_traverse. */ 1935 1936 static bfd_boolean 1937 _bfd_elf_link_assign_sym_version (struct elf_link_hash_entry *h, void *data) 1938 { 1939 struct elf_info_failed *sinfo; 1940 struct bfd_link_info *info; 1941 const struct elf_backend_data *bed; 1942 struct elf_info_failed eif; 1943 char *p; 1944 bfd_size_type amt; 1945 1946 sinfo = (struct elf_info_failed *) data; 1947 info = sinfo->info; 1948 1949 /* Fix the symbol flags. */ 1950 eif.failed = FALSE; 1951 eif.info = info; 1952 if (! _bfd_elf_fix_symbol_flags (h, &eif)) 1953 { 1954 if (eif.failed) 1955 sinfo->failed = TRUE; 1956 return FALSE; 1957 } 1958 1959 /* We only need version numbers for symbols defined in regular 1960 objects. */ 1961 if (!h->def_regular) 1962 return TRUE; 1963 1964 bed = get_elf_backend_data (info->output_bfd); 1965 p = strchr (h->root.root.string, ELF_VER_CHR); 1966 if (p != NULL && h->verinfo.vertree == NULL) 1967 { 1968 struct bfd_elf_version_tree *t; 1969 bfd_boolean hidden; 1970 1971 hidden = TRUE; 1972 1973 /* There are two consecutive ELF_VER_CHR characters if this is 1974 not a hidden symbol. */ 1975 ++p; 1976 if (*p == ELF_VER_CHR) 1977 { 1978 hidden = FALSE; 1979 ++p; 1980 } 1981 1982 /* If there is no version string, we can just return out. */ 1983 if (*p == '\0') 1984 { 1985 if (hidden) 1986 h->hidden = 1; 1987 return TRUE; 1988 } 1989 1990 /* Look for the version. If we find it, it is no longer weak. */ 1991 for (t = sinfo->info->version_info; t != NULL; t = t->next) 1992 { 1993 if (strcmp (t->name, p) == 0) 1994 { 1995 size_t len; 1996 char *alc; 1997 struct bfd_elf_version_expr *d; 1998 1999 len = p - h->root.root.string; 2000 alc = (char *) bfd_malloc (len); 2001 if (alc == NULL) 2002 { 2003 sinfo->failed = TRUE; 2004 return FALSE; 2005 } 2006 memcpy (alc, h->root.root.string, len - 1); 2007 alc[len - 1] = '\0'; 2008 if (alc[len - 2] == ELF_VER_CHR) 2009 alc[len - 2] = '\0'; 2010 2011 h->verinfo.vertree = t; 2012 t->used = TRUE; 2013 d = NULL; 2014 2015 if (t->globals.list != NULL) 2016 d = (*t->match) (&t->globals, NULL, alc); 2017 2018 /* See if there is anything to force this symbol to 2019 local scope. */ 2020 if (d == NULL && t->locals.list != NULL) 2021 { 2022 d = (*t->match) (&t->locals, NULL, alc); 2023 if (d != NULL 2024 && h->dynindx != -1 2025 && ! info->export_dynamic) 2026 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2027 } 2028 2029 free (alc); 2030 break; 2031 } 2032 } 2033 2034 /* If we are building an application, we need to create a 2035 version node for this version. */ 2036 if (t == NULL && info->executable) 2037 { 2038 struct bfd_elf_version_tree **pp; 2039 int version_index; 2040 2041 /* If we aren't going to export this symbol, we don't need 2042 to worry about it. */ 2043 if (h->dynindx == -1) 2044 return TRUE; 2045 2046 amt = sizeof *t; 2047 t = (struct bfd_elf_version_tree *) bfd_zalloc (info->output_bfd, amt); 2048 if (t == NULL) 2049 { 2050 sinfo->failed = TRUE; 2051 return FALSE; 2052 } 2053 2054 t->name = p; 2055 t->name_indx = (unsigned int) -1; 2056 t->used = TRUE; 2057 2058 version_index = 1; 2059 /* Don't count anonymous version tag. */ 2060 if (sinfo->info->version_info != NULL 2061 && sinfo->info->version_info->vernum == 0) 2062 version_index = 0; 2063 for (pp = &sinfo->info->version_info; 2064 *pp != NULL; 2065 pp = &(*pp)->next) 2066 ++version_index; 2067 t->vernum = version_index; 2068 2069 *pp = t; 2070 2071 h->verinfo.vertree = t; 2072 } 2073 else if (t == NULL) 2074 { 2075 /* We could not find the version for a symbol when 2076 generating a shared archive. Return an error. */ 2077 (*_bfd_error_handler) 2078 (_("%B: version node not found for symbol %s"), 2079 info->output_bfd, h->root.root.string); 2080 bfd_set_error (bfd_error_bad_value); 2081 sinfo->failed = TRUE; 2082 return FALSE; 2083 } 2084 2085 if (hidden) 2086 h->hidden = 1; 2087 } 2088 2089 /* If we don't have a version for this symbol, see if we can find 2090 something. */ 2091 if (h->verinfo.vertree == NULL && sinfo->info->version_info != NULL) 2092 { 2093 bfd_boolean hide; 2094 2095 h->verinfo.vertree 2096 = bfd_find_version_for_sym (sinfo->info->version_info, 2097 h->root.root.string, &hide); 2098 if (h->verinfo.vertree != NULL && hide) 2099 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 2100 } 2101 2102 return TRUE; 2103 } 2104 2105 /* Read and swap the relocs from the section indicated by SHDR. This 2106 may be either a REL or a RELA section. The relocations are 2107 translated into RELA relocations and stored in INTERNAL_RELOCS, 2108 which should have already been allocated to contain enough space. 2109 The EXTERNAL_RELOCS are a buffer where the external form of the 2110 relocations should be stored. 2111 2112 Returns FALSE if something goes wrong. */ 2113 2114 static bfd_boolean 2115 elf_link_read_relocs_from_section (bfd *abfd, 2116 asection *sec, 2117 Elf_Internal_Shdr *shdr, 2118 void *external_relocs, 2119 Elf_Internal_Rela *internal_relocs) 2120 { 2121 const struct elf_backend_data *bed; 2122 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 2123 const bfd_byte *erela; 2124 const bfd_byte *erelaend; 2125 Elf_Internal_Rela *irela; 2126 Elf_Internal_Shdr *symtab_hdr; 2127 size_t nsyms; 2128 2129 /* Position ourselves at the start of the section. */ 2130 if (bfd_seek (abfd, shdr->sh_offset, SEEK_SET) != 0) 2131 return FALSE; 2132 2133 /* Read the relocations. */ 2134 if (bfd_bread (external_relocs, shdr->sh_size, abfd) != shdr->sh_size) 2135 return FALSE; 2136 2137 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2138 nsyms = NUM_SHDR_ENTRIES (symtab_hdr); 2139 2140 bed = get_elf_backend_data (abfd); 2141 2142 /* Convert the external relocations to the internal format. */ 2143 if (shdr->sh_entsize == bed->s->sizeof_rel) 2144 swap_in = bed->s->swap_reloc_in; 2145 else if (shdr->sh_entsize == bed->s->sizeof_rela) 2146 swap_in = bed->s->swap_reloca_in; 2147 else 2148 { 2149 bfd_set_error (bfd_error_wrong_format); 2150 return FALSE; 2151 } 2152 2153 erela = (const bfd_byte *) external_relocs; 2154 erelaend = erela + shdr->sh_size; 2155 irela = internal_relocs; 2156 while (erela < erelaend) 2157 { 2158 bfd_vma r_symndx; 2159 2160 (*swap_in) (abfd, erela, irela); 2161 r_symndx = ELF32_R_SYM (irela->r_info); 2162 if (bed->s->arch_size == 64) 2163 r_symndx >>= 24; 2164 if (nsyms > 0) 2165 { 2166 if ((size_t) r_symndx >= nsyms) 2167 { 2168 (*_bfd_error_handler) 2169 (_("%B: bad reloc symbol index (0x%lx >= 0x%lx)" 2170 " for offset 0x%lx in section `%A'"), 2171 abfd, sec, 2172 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); 2173 bfd_set_error (bfd_error_bad_value); 2174 return FALSE; 2175 } 2176 } 2177 else if (r_symndx != STN_UNDEF) 2178 { 2179 (*_bfd_error_handler) 2180 (_("%B: non-zero symbol index (0x%lx) for offset 0x%lx in section `%A'" 2181 " when the object file has no symbol table"), 2182 abfd, sec, 2183 (unsigned long) r_symndx, (unsigned long) nsyms, irela->r_offset); 2184 bfd_set_error (bfd_error_bad_value); 2185 return FALSE; 2186 } 2187 irela += bed->s->int_rels_per_ext_rel; 2188 erela += shdr->sh_entsize; 2189 } 2190 2191 return TRUE; 2192 } 2193 2194 /* Read and swap the relocs for a section O. They may have been 2195 cached. If the EXTERNAL_RELOCS and INTERNAL_RELOCS arguments are 2196 not NULL, they are used as buffers to read into. They are known to 2197 be large enough. If the INTERNAL_RELOCS relocs argument is NULL, 2198 the return value is allocated using either malloc or bfd_alloc, 2199 according to the KEEP_MEMORY argument. If O has two relocation 2200 sections (both REL and RELA relocations), then the REL_HDR 2201 relocations will appear first in INTERNAL_RELOCS, followed by the 2202 RELA_HDR relocations. */ 2203 2204 Elf_Internal_Rela * 2205 _bfd_elf_link_read_relocs (bfd *abfd, 2206 asection *o, 2207 void *external_relocs, 2208 Elf_Internal_Rela *internal_relocs, 2209 bfd_boolean keep_memory) 2210 { 2211 void *alloc1 = NULL; 2212 Elf_Internal_Rela *alloc2 = NULL; 2213 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2214 struct bfd_elf_section_data *esdo = elf_section_data (o); 2215 Elf_Internal_Rela *internal_rela_relocs; 2216 2217 if (esdo->relocs != NULL) 2218 return esdo->relocs; 2219 2220 if (o->reloc_count == 0) 2221 return NULL; 2222 2223 if (internal_relocs == NULL) 2224 { 2225 bfd_size_type size; 2226 2227 size = o->reloc_count; 2228 size *= bed->s->int_rels_per_ext_rel * sizeof (Elf_Internal_Rela); 2229 if (keep_memory) 2230 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_alloc (abfd, size); 2231 else 2232 internal_relocs = alloc2 = (Elf_Internal_Rela *) bfd_malloc (size); 2233 if (internal_relocs == NULL) 2234 goto error_return; 2235 } 2236 2237 if (external_relocs == NULL) 2238 { 2239 bfd_size_type size = 0; 2240 2241 if (esdo->rel.hdr) 2242 size += esdo->rel.hdr->sh_size; 2243 if (esdo->rela.hdr) 2244 size += esdo->rela.hdr->sh_size; 2245 2246 alloc1 = bfd_malloc (size); 2247 if (alloc1 == NULL) 2248 goto error_return; 2249 external_relocs = alloc1; 2250 } 2251 2252 internal_rela_relocs = internal_relocs; 2253 if (esdo->rel.hdr) 2254 { 2255 if (!elf_link_read_relocs_from_section (abfd, o, esdo->rel.hdr, 2256 external_relocs, 2257 internal_relocs)) 2258 goto error_return; 2259 external_relocs = (((bfd_byte *) external_relocs) 2260 + esdo->rel.hdr->sh_size); 2261 internal_rela_relocs += (NUM_SHDR_ENTRIES (esdo->rel.hdr) 2262 * bed->s->int_rels_per_ext_rel); 2263 } 2264 2265 if (esdo->rela.hdr 2266 && (!elf_link_read_relocs_from_section (abfd, o, esdo->rela.hdr, 2267 external_relocs, 2268 internal_rela_relocs))) 2269 goto error_return; 2270 2271 /* Cache the results for next time, if we can. */ 2272 if (keep_memory) 2273 esdo->relocs = internal_relocs; 2274 2275 if (alloc1 != NULL) 2276 free (alloc1); 2277 2278 /* Don't free alloc2, since if it was allocated we are passing it 2279 back (under the name of internal_relocs). */ 2280 2281 return internal_relocs; 2282 2283 error_return: 2284 if (alloc1 != NULL) 2285 free (alloc1); 2286 if (alloc2 != NULL) 2287 { 2288 if (keep_memory) 2289 bfd_release (abfd, alloc2); 2290 else 2291 free (alloc2); 2292 } 2293 return NULL; 2294 } 2295 2296 /* Compute the size of, and allocate space for, REL_HDR which is the 2297 section header for a section containing relocations for O. */ 2298 2299 static bfd_boolean 2300 _bfd_elf_link_size_reloc_section (bfd *abfd, 2301 struct bfd_elf_section_reloc_data *reldata) 2302 { 2303 Elf_Internal_Shdr *rel_hdr = reldata->hdr; 2304 2305 /* That allows us to calculate the size of the section. */ 2306 rel_hdr->sh_size = rel_hdr->sh_entsize * reldata->count; 2307 2308 /* The contents field must last into write_object_contents, so we 2309 allocate it with bfd_alloc rather than malloc. Also since we 2310 cannot be sure that the contents will actually be filled in, 2311 we zero the allocated space. */ 2312 rel_hdr->contents = (unsigned char *) bfd_zalloc (abfd, rel_hdr->sh_size); 2313 if (rel_hdr->contents == NULL && rel_hdr->sh_size != 0) 2314 return FALSE; 2315 2316 if (reldata->hashes == NULL && reldata->count) 2317 { 2318 struct elf_link_hash_entry **p; 2319 2320 p = (struct elf_link_hash_entry **) 2321 bfd_zmalloc (reldata->count * sizeof (struct elf_link_hash_entry *)); 2322 if (p == NULL) 2323 return FALSE; 2324 2325 reldata->hashes = p; 2326 } 2327 2328 return TRUE; 2329 } 2330 2331 /* Copy the relocations indicated by the INTERNAL_RELOCS (which 2332 originated from the section given by INPUT_REL_HDR) to the 2333 OUTPUT_BFD. */ 2334 2335 bfd_boolean 2336 _bfd_elf_link_output_relocs (bfd *output_bfd, 2337 asection *input_section, 2338 Elf_Internal_Shdr *input_rel_hdr, 2339 Elf_Internal_Rela *internal_relocs, 2340 struct elf_link_hash_entry **rel_hash 2341 ATTRIBUTE_UNUSED) 2342 { 2343 Elf_Internal_Rela *irela; 2344 Elf_Internal_Rela *irelaend; 2345 bfd_byte *erel; 2346 struct bfd_elf_section_reloc_data *output_reldata; 2347 asection *output_section; 2348 const struct elf_backend_data *bed; 2349 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 2350 struct bfd_elf_section_data *esdo; 2351 2352 output_section = input_section->output_section; 2353 2354 bed = get_elf_backend_data (output_bfd); 2355 esdo = elf_section_data (output_section); 2356 if (esdo->rel.hdr && esdo->rel.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2357 { 2358 output_reldata = &esdo->rel; 2359 swap_out = bed->s->swap_reloc_out; 2360 } 2361 else if (esdo->rela.hdr 2362 && esdo->rela.hdr->sh_entsize == input_rel_hdr->sh_entsize) 2363 { 2364 output_reldata = &esdo->rela; 2365 swap_out = bed->s->swap_reloca_out; 2366 } 2367 else 2368 { 2369 (*_bfd_error_handler) 2370 (_("%B: relocation size mismatch in %B section %A"), 2371 output_bfd, input_section->owner, input_section); 2372 bfd_set_error (bfd_error_wrong_format); 2373 return FALSE; 2374 } 2375 2376 erel = output_reldata->hdr->contents; 2377 erel += output_reldata->count * input_rel_hdr->sh_entsize; 2378 irela = internal_relocs; 2379 irelaend = irela + (NUM_SHDR_ENTRIES (input_rel_hdr) 2380 * bed->s->int_rels_per_ext_rel); 2381 while (irela < irelaend) 2382 { 2383 (*swap_out) (output_bfd, irela, erel); 2384 irela += bed->s->int_rels_per_ext_rel; 2385 erel += input_rel_hdr->sh_entsize; 2386 } 2387 2388 /* Bump the counter, so that we know where to add the next set of 2389 relocations. */ 2390 output_reldata->count += NUM_SHDR_ENTRIES (input_rel_hdr); 2391 2392 return TRUE; 2393 } 2394 2395 /* Make weak undefined symbols in PIE dynamic. */ 2396 2397 bfd_boolean 2398 _bfd_elf_link_hash_fixup_symbol (struct bfd_link_info *info, 2399 struct elf_link_hash_entry *h) 2400 { 2401 if (info->pie 2402 && h->dynindx == -1 2403 && h->root.type == bfd_link_hash_undefweak) 2404 return bfd_elf_link_record_dynamic_symbol (info, h); 2405 2406 return TRUE; 2407 } 2408 2409 /* Fix up the flags for a symbol. This handles various cases which 2410 can only be fixed after all the input files are seen. This is 2411 currently called by both adjust_dynamic_symbol and 2412 assign_sym_version, which is unnecessary but perhaps more robust in 2413 the face of future changes. */ 2414 2415 static bfd_boolean 2416 _bfd_elf_fix_symbol_flags (struct elf_link_hash_entry *h, 2417 struct elf_info_failed *eif) 2418 { 2419 const struct elf_backend_data *bed; 2420 2421 /* If this symbol was mentioned in a non-ELF file, try to set 2422 DEF_REGULAR and REF_REGULAR correctly. This is the only way to 2423 permit a non-ELF file to correctly refer to a symbol defined in 2424 an ELF dynamic object. */ 2425 if (h->non_elf) 2426 { 2427 while (h->root.type == bfd_link_hash_indirect) 2428 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2429 2430 if (h->root.type != bfd_link_hash_defined 2431 && h->root.type != bfd_link_hash_defweak) 2432 { 2433 h->ref_regular = 1; 2434 h->ref_regular_nonweak = 1; 2435 } 2436 else 2437 { 2438 if (h->root.u.def.section->owner != NULL 2439 && (bfd_get_flavour (h->root.u.def.section->owner) 2440 == bfd_target_elf_flavour)) 2441 { 2442 h->ref_regular = 1; 2443 h->ref_regular_nonweak = 1; 2444 } 2445 else 2446 h->def_regular = 1; 2447 } 2448 2449 if (h->dynindx == -1 2450 && (h->def_dynamic 2451 || h->ref_dynamic)) 2452 { 2453 if (! bfd_elf_link_record_dynamic_symbol (eif->info, h)) 2454 { 2455 eif->failed = TRUE; 2456 return FALSE; 2457 } 2458 } 2459 } 2460 else 2461 { 2462 /* Unfortunately, NON_ELF is only correct if the symbol 2463 was first seen in a non-ELF file. Fortunately, if the symbol 2464 was first seen in an ELF file, we're probably OK unless the 2465 symbol was defined in a non-ELF file. Catch that case here. 2466 FIXME: We're still in trouble if the symbol was first seen in 2467 a dynamic object, and then later in a non-ELF regular object. */ 2468 if ((h->root.type == bfd_link_hash_defined 2469 || h->root.type == bfd_link_hash_defweak) 2470 && !h->def_regular 2471 && (h->root.u.def.section->owner != NULL 2472 ? (bfd_get_flavour (h->root.u.def.section->owner) 2473 != bfd_target_elf_flavour) 2474 : (bfd_is_abs_section (h->root.u.def.section) 2475 && !h->def_dynamic))) 2476 h->def_regular = 1; 2477 } 2478 2479 /* Backend specific symbol fixup. */ 2480 bed = get_elf_backend_data (elf_hash_table (eif->info)->dynobj); 2481 if (bed->elf_backend_fixup_symbol 2482 && !(*bed->elf_backend_fixup_symbol) (eif->info, h)) 2483 return FALSE; 2484 2485 /* If this is a final link, and the symbol was defined as a common 2486 symbol in a regular object file, and there was no definition in 2487 any dynamic object, then the linker will have allocated space for 2488 the symbol in a common section but the DEF_REGULAR 2489 flag will not have been set. */ 2490 if (h->root.type == bfd_link_hash_defined 2491 && !h->def_regular 2492 && h->ref_regular 2493 && !h->def_dynamic 2494 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) 2495 h->def_regular = 1; 2496 2497 /* If -Bsymbolic was used (which means to bind references to global 2498 symbols to the definition within the shared object), and this 2499 symbol was defined in a regular object, then it actually doesn't 2500 need a PLT entry. Likewise, if the symbol has non-default 2501 visibility. If the symbol has hidden or internal visibility, we 2502 will force it local. */ 2503 if (h->needs_plt 2504 && eif->info->shared 2505 && is_elf_hash_table (eif->info->hash) 2506 && (SYMBOLIC_BIND (eif->info, h) 2507 || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2508 && h->def_regular) 2509 { 2510 bfd_boolean force_local; 2511 2512 force_local = (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 2513 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN); 2514 (*bed->elf_backend_hide_symbol) (eif->info, h, force_local); 2515 } 2516 2517 /* If a weak undefined symbol has non-default visibility, we also 2518 hide it from the dynamic linker. */ 2519 if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT 2520 && h->root.type == bfd_link_hash_undefweak) 2521 (*bed->elf_backend_hide_symbol) (eif->info, h, TRUE); 2522 2523 /* If this is a weak defined symbol in a dynamic object, and we know 2524 the real definition in the dynamic object, copy interesting flags 2525 over to the real definition. */ 2526 if (h->u.weakdef != NULL) 2527 { 2528 /* If the real definition is defined by a regular object file, 2529 don't do anything special. See the longer description in 2530 _bfd_elf_adjust_dynamic_symbol, below. */ 2531 if (h->u.weakdef->def_regular) 2532 h->u.weakdef = NULL; 2533 else 2534 { 2535 struct elf_link_hash_entry *weakdef = h->u.weakdef; 2536 2537 while (h->root.type == bfd_link_hash_indirect) 2538 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2539 2540 BFD_ASSERT (h->root.type == bfd_link_hash_defined 2541 || h->root.type == bfd_link_hash_defweak); 2542 BFD_ASSERT (weakdef->def_dynamic); 2543 BFD_ASSERT (weakdef->root.type == bfd_link_hash_defined 2544 || weakdef->root.type == bfd_link_hash_defweak); 2545 (*bed->elf_backend_copy_indirect_symbol) (eif->info, weakdef, h); 2546 } 2547 } 2548 2549 return TRUE; 2550 } 2551 2552 /* Make the backend pick a good value for a dynamic symbol. This is 2553 called via elf_link_hash_traverse, and also calls itself 2554 recursively. */ 2555 2556 static bfd_boolean 2557 _bfd_elf_adjust_dynamic_symbol (struct elf_link_hash_entry *h, void *data) 2558 { 2559 struct elf_info_failed *eif = (struct elf_info_failed *) data; 2560 bfd *dynobj; 2561 const struct elf_backend_data *bed; 2562 2563 if (! is_elf_hash_table (eif->info->hash)) 2564 return FALSE; 2565 2566 /* Ignore indirect symbols. These are added by the versioning code. */ 2567 if (h->root.type == bfd_link_hash_indirect) 2568 return TRUE; 2569 2570 /* Fix the symbol flags. */ 2571 if (! _bfd_elf_fix_symbol_flags (h, eif)) 2572 return FALSE; 2573 2574 /* If this symbol does not require a PLT entry, and it is not 2575 defined by a dynamic object, or is not referenced by a regular 2576 object, ignore it. We do have to handle a weak defined symbol, 2577 even if no regular object refers to it, if we decided to add it 2578 to the dynamic symbol table. FIXME: Do we normally need to worry 2579 about symbols which are defined by one dynamic object and 2580 referenced by another one? */ 2581 if (!h->needs_plt 2582 && h->type != STT_GNU_IFUNC 2583 && (h->def_regular 2584 || !h->def_dynamic 2585 || (!h->ref_regular 2586 && (h->u.weakdef == NULL || h->u.weakdef->dynindx == -1)))) 2587 { 2588 h->plt = elf_hash_table (eif->info)->init_plt_offset; 2589 return TRUE; 2590 } 2591 2592 /* If we've already adjusted this symbol, don't do it again. This 2593 can happen via a recursive call. */ 2594 if (h->dynamic_adjusted) 2595 return TRUE; 2596 2597 /* Don't look at this symbol again. Note that we must set this 2598 after checking the above conditions, because we may look at a 2599 symbol once, decide not to do anything, and then get called 2600 recursively later after REF_REGULAR is set below. */ 2601 h->dynamic_adjusted = 1; 2602 2603 /* If this is a weak definition, and we know a real definition, and 2604 the real symbol is not itself defined by a regular object file, 2605 then get a good value for the real definition. We handle the 2606 real symbol first, for the convenience of the backend routine. 2607 2608 Note that there is a confusing case here. If the real definition 2609 is defined by a regular object file, we don't get the real symbol 2610 from the dynamic object, but we do get the weak symbol. If the 2611 processor backend uses a COPY reloc, then if some routine in the 2612 dynamic object changes the real symbol, we will not see that 2613 change in the corresponding weak symbol. This is the way other 2614 ELF linkers work as well, and seems to be a result of the shared 2615 library model. 2616 2617 I will clarify this issue. Most SVR4 shared libraries define the 2618 variable _timezone and define timezone as a weak synonym. The 2619 tzset call changes _timezone. If you write 2620 extern int timezone; 2621 int _timezone = 5; 2622 int main () { tzset (); printf ("%d %d\n", timezone, _timezone); } 2623 you might expect that, since timezone is a synonym for _timezone, 2624 the same number will print both times. However, if the processor 2625 backend uses a COPY reloc, then actually timezone will be copied 2626 into your process image, and, since you define _timezone 2627 yourself, _timezone will not. Thus timezone and _timezone will 2628 wind up at different memory locations. The tzset call will set 2629 _timezone, leaving timezone unchanged. */ 2630 2631 if (h->u.weakdef != NULL) 2632 { 2633 /* If we get to this point, there is an implicit reference to 2634 H->U.WEAKDEF by a regular object file via the weak symbol H. */ 2635 h->u.weakdef->ref_regular = 1; 2636 2637 /* Ensure that the backend adjust_dynamic_symbol function sees 2638 H->U.WEAKDEF before H by recursively calling ourselves. */ 2639 if (! _bfd_elf_adjust_dynamic_symbol (h->u.weakdef, eif)) 2640 return FALSE; 2641 } 2642 2643 /* If a symbol has no type and no size and does not require a PLT 2644 entry, then we are probably about to do the wrong thing here: we 2645 are probably going to create a COPY reloc for an empty object. 2646 This case can arise when a shared object is built with assembly 2647 code, and the assembly code fails to set the symbol type. */ 2648 if (h->size == 0 2649 && h->type == STT_NOTYPE 2650 && !h->needs_plt) 2651 (*_bfd_error_handler) 2652 (_("warning: type and size of dynamic symbol `%s' are not defined"), 2653 h->root.root.string); 2654 2655 dynobj = elf_hash_table (eif->info)->dynobj; 2656 bed = get_elf_backend_data (dynobj); 2657 2658 if (! (*bed->elf_backend_adjust_dynamic_symbol) (eif->info, h)) 2659 { 2660 eif->failed = TRUE; 2661 return FALSE; 2662 } 2663 2664 return TRUE; 2665 } 2666 2667 /* Adjust the dynamic symbol, H, for copy in the dynamic bss section, 2668 DYNBSS. */ 2669 2670 bfd_boolean 2671 _bfd_elf_adjust_dynamic_copy (struct elf_link_hash_entry *h, 2672 asection *dynbss) 2673 { 2674 unsigned int power_of_two; 2675 bfd_vma mask; 2676 asection *sec = h->root.u.def.section; 2677 2678 /* The section aligment of definition is the maximum alignment 2679 requirement of symbols defined in the section. Since we don't 2680 know the symbol alignment requirement, we start with the 2681 maximum alignment and check low bits of the symbol address 2682 for the minimum alignment. */ 2683 power_of_two = bfd_get_section_alignment (sec->owner, sec); 2684 mask = ((bfd_vma) 1 << power_of_two) - 1; 2685 while ((h->root.u.def.value & mask) != 0) 2686 { 2687 mask >>= 1; 2688 --power_of_two; 2689 } 2690 2691 if (power_of_two > bfd_get_section_alignment (dynbss->owner, 2692 dynbss)) 2693 { 2694 /* Adjust the section alignment if needed. */ 2695 if (! bfd_set_section_alignment (dynbss->owner, dynbss, 2696 power_of_two)) 2697 return FALSE; 2698 } 2699 2700 /* We make sure that the symbol will be aligned properly. */ 2701 dynbss->size = BFD_ALIGN (dynbss->size, mask + 1); 2702 2703 /* Define the symbol as being at this point in DYNBSS. */ 2704 h->root.u.def.section = dynbss; 2705 h->root.u.def.value = dynbss->size; 2706 2707 /* Increment the size of DYNBSS to make room for the symbol. */ 2708 dynbss->size += h->size; 2709 2710 return TRUE; 2711 } 2712 2713 /* Adjust all external symbols pointing into SEC_MERGE sections 2714 to reflect the object merging within the sections. */ 2715 2716 static bfd_boolean 2717 _bfd_elf_link_sec_merge_syms (struct elf_link_hash_entry *h, void *data) 2718 { 2719 asection *sec; 2720 2721 if ((h->root.type == bfd_link_hash_defined 2722 || h->root.type == bfd_link_hash_defweak) 2723 && ((sec = h->root.u.def.section)->flags & SEC_MERGE) 2724 && sec->sec_info_type == SEC_INFO_TYPE_MERGE) 2725 { 2726 bfd *output_bfd = (bfd *) data; 2727 2728 h->root.u.def.value = 2729 _bfd_merged_section_offset (output_bfd, 2730 &h->root.u.def.section, 2731 elf_section_data (sec)->sec_info, 2732 h->root.u.def.value); 2733 } 2734 2735 return TRUE; 2736 } 2737 2738 /* Returns false if the symbol referred to by H should be considered 2739 to resolve local to the current module, and true if it should be 2740 considered to bind dynamically. */ 2741 2742 bfd_boolean 2743 _bfd_elf_dynamic_symbol_p (struct elf_link_hash_entry *h, 2744 struct bfd_link_info *info, 2745 bfd_boolean not_local_protected) 2746 { 2747 bfd_boolean binding_stays_local_p; 2748 const struct elf_backend_data *bed; 2749 struct elf_link_hash_table *hash_table; 2750 2751 if (h == NULL) 2752 return FALSE; 2753 2754 while (h->root.type == bfd_link_hash_indirect 2755 || h->root.type == bfd_link_hash_warning) 2756 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2757 2758 /* If it was forced local, then clearly it's not dynamic. */ 2759 if (h->dynindx == -1) 2760 return FALSE; 2761 if (h->forced_local) 2762 return FALSE; 2763 2764 /* Identify the cases where name binding rules say that a 2765 visible symbol resolves locally. */ 2766 binding_stays_local_p = info->executable || SYMBOLIC_BIND (info, h); 2767 2768 switch (ELF_ST_VISIBILITY (h->other)) 2769 { 2770 case STV_INTERNAL: 2771 case STV_HIDDEN: 2772 return FALSE; 2773 2774 case STV_PROTECTED: 2775 hash_table = elf_hash_table (info); 2776 if (!is_elf_hash_table (hash_table)) 2777 return FALSE; 2778 2779 bed = get_elf_backend_data (hash_table->dynobj); 2780 2781 /* Proper resolution for function pointer equality may require 2782 that these symbols perhaps be resolved dynamically, even though 2783 we should be resolving them to the current module. */ 2784 if (!not_local_protected || !bed->is_function_type (h->type)) 2785 binding_stays_local_p = TRUE; 2786 break; 2787 2788 default: 2789 break; 2790 } 2791 2792 /* If it isn't defined locally, then clearly it's dynamic. */ 2793 if (!h->def_regular && !ELF_COMMON_DEF_P (h)) 2794 return TRUE; 2795 2796 /* Otherwise, the symbol is dynamic if binding rules don't tell 2797 us that it remains local. */ 2798 return !binding_stays_local_p; 2799 } 2800 2801 /* Return true if the symbol referred to by H should be considered 2802 to resolve local to the current module, and false otherwise. Differs 2803 from (the inverse of) _bfd_elf_dynamic_symbol_p in the treatment of 2804 undefined symbols. The two functions are virtually identical except 2805 for the place where forced_local and dynindx == -1 are tested. If 2806 either of those tests are true, _bfd_elf_dynamic_symbol_p will say 2807 the symbol is local, while _bfd_elf_symbol_refs_local_p will say 2808 the symbol is local only for defined symbols. 2809 It might seem that _bfd_elf_dynamic_symbol_p could be rewritten as 2810 !_bfd_elf_symbol_refs_local_p, except that targets differ in their 2811 treatment of undefined weak symbols. For those that do not make 2812 undefined weak symbols dynamic, both functions may return false. */ 2813 2814 bfd_boolean 2815 _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h, 2816 struct bfd_link_info *info, 2817 bfd_boolean local_protected) 2818 { 2819 const struct elf_backend_data *bed; 2820 struct elf_link_hash_table *hash_table; 2821 2822 /* If it's a local sym, of course we resolve locally. */ 2823 if (h == NULL) 2824 return TRUE; 2825 2826 /* STV_HIDDEN or STV_INTERNAL ones must be local. */ 2827 if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN 2828 || ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 2829 return TRUE; 2830 2831 /* Common symbols that become definitions don't get the DEF_REGULAR 2832 flag set, so test it first, and don't bail out. */ 2833 if (ELF_COMMON_DEF_P (h)) 2834 /* Do nothing. */; 2835 /* If we don't have a definition in a regular file, then we can't 2836 resolve locally. The sym is either undefined or dynamic. */ 2837 else if (!h->def_regular) 2838 return FALSE; 2839 2840 /* Forced local symbols resolve locally. */ 2841 if (h->forced_local) 2842 return TRUE; 2843 2844 /* As do non-dynamic symbols. */ 2845 if (h->dynindx == -1) 2846 return TRUE; 2847 2848 /* At this point, we know the symbol is defined and dynamic. In an 2849 executable it must resolve locally, likewise when building symbolic 2850 shared libraries. */ 2851 if (info->executable || SYMBOLIC_BIND (info, h)) 2852 return TRUE; 2853 2854 /* Now deal with defined dynamic symbols in shared libraries. Ones 2855 with default visibility might not resolve locally. */ 2856 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 2857 return FALSE; 2858 2859 hash_table = elf_hash_table (info); 2860 if (!is_elf_hash_table (hash_table)) 2861 return TRUE; 2862 2863 bed = get_elf_backend_data (hash_table->dynobj); 2864 2865 /* STV_PROTECTED non-function symbols are local. */ 2866 if (!bed->is_function_type (h->type)) 2867 return TRUE; 2868 2869 /* Function pointer equality tests may require that STV_PROTECTED 2870 symbols be treated as dynamic symbols. If the address of a 2871 function not defined in an executable is set to that function's 2872 plt entry in the executable, then the address of the function in 2873 a shared library must also be the plt entry in the executable. */ 2874 return local_protected; 2875 } 2876 2877 /* Caches some TLS segment info, and ensures that the TLS segment vma is 2878 aligned. Returns the first TLS output section. */ 2879 2880 struct bfd_section * 2881 _bfd_elf_tls_setup (bfd *obfd, struct bfd_link_info *info) 2882 { 2883 struct bfd_section *sec, *tls; 2884 unsigned int align = 0; 2885 2886 for (sec = obfd->sections; sec != NULL; sec = sec->next) 2887 if ((sec->flags & SEC_THREAD_LOCAL) != 0) 2888 break; 2889 tls = sec; 2890 2891 for (; sec != NULL && (sec->flags & SEC_THREAD_LOCAL) != 0; sec = sec->next) 2892 if (sec->alignment_power > align) 2893 align = sec->alignment_power; 2894 2895 elf_hash_table (info)->tls_sec = tls; 2896 2897 /* Ensure the alignment of the first section is the largest alignment, 2898 so that the tls segment starts aligned. */ 2899 if (tls != NULL) 2900 tls->alignment_power = align; 2901 2902 return tls; 2903 } 2904 2905 /* Return TRUE iff this is a non-common, definition of a non-function symbol. */ 2906 static bfd_boolean 2907 is_global_data_symbol_definition (bfd *abfd ATTRIBUTE_UNUSED, 2908 Elf_Internal_Sym *sym) 2909 { 2910 const struct elf_backend_data *bed; 2911 2912 /* Local symbols do not count, but target specific ones might. */ 2913 if (ELF_ST_BIND (sym->st_info) != STB_GLOBAL 2914 && ELF_ST_BIND (sym->st_info) < STB_LOOS) 2915 return FALSE; 2916 2917 bed = get_elf_backend_data (abfd); 2918 /* Function symbols do not count. */ 2919 if (bed->is_function_type (ELF_ST_TYPE (sym->st_info))) 2920 return FALSE; 2921 2922 /* If the section is undefined, then so is the symbol. */ 2923 if (sym->st_shndx == SHN_UNDEF) 2924 return FALSE; 2925 2926 /* If the symbol is defined in the common section, then 2927 it is a common definition and so does not count. */ 2928 if (bed->common_definition (sym)) 2929 return FALSE; 2930 2931 /* If the symbol is in a target specific section then we 2932 must rely upon the backend to tell us what it is. */ 2933 if (sym->st_shndx >= SHN_LORESERVE && sym->st_shndx < SHN_ABS) 2934 /* FIXME - this function is not coded yet: 2935 2936 return _bfd_is_global_symbol_definition (abfd, sym); 2937 2938 Instead for now assume that the definition is not global, 2939 Even if this is wrong, at least the linker will behave 2940 in the same way that it used to do. */ 2941 return FALSE; 2942 2943 return TRUE; 2944 } 2945 2946 /* Search the symbol table of the archive element of the archive ABFD 2947 whose archive map contains a mention of SYMDEF, and determine if 2948 the symbol is defined in this element. */ 2949 static bfd_boolean 2950 elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef) 2951 { 2952 Elf_Internal_Shdr * hdr; 2953 bfd_size_type symcount; 2954 bfd_size_type extsymcount; 2955 bfd_size_type extsymoff; 2956 Elf_Internal_Sym *isymbuf; 2957 Elf_Internal_Sym *isym; 2958 Elf_Internal_Sym *isymend; 2959 bfd_boolean result; 2960 2961 abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 2962 if (abfd == NULL) 2963 return FALSE; 2964 2965 if (! bfd_check_format (abfd, bfd_object)) 2966 return FALSE; 2967 2968 /* If we have already included the element containing this symbol in the 2969 link then we do not need to include it again. Just claim that any symbol 2970 it contains is not a definition, so that our caller will not decide to 2971 (re)include this element. */ 2972 if (abfd->archive_pass) 2973 return FALSE; 2974 2975 /* Select the appropriate symbol table. */ 2976 if ((abfd->flags & DYNAMIC) == 0 || elf_dynsymtab (abfd) == 0) 2977 hdr = &elf_tdata (abfd)->symtab_hdr; 2978 else 2979 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 2980 2981 symcount = hdr->sh_size / get_elf_backend_data (abfd)->s->sizeof_sym; 2982 2983 /* The sh_info field of the symtab header tells us where the 2984 external symbols start. We don't care about the local symbols. */ 2985 if (elf_bad_symtab (abfd)) 2986 { 2987 extsymcount = symcount; 2988 extsymoff = 0; 2989 } 2990 else 2991 { 2992 extsymcount = symcount - hdr->sh_info; 2993 extsymoff = hdr->sh_info; 2994 } 2995 2996 if (extsymcount == 0) 2997 return FALSE; 2998 2999 /* Read in the symbol table. */ 3000 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3001 NULL, NULL, NULL); 3002 if (isymbuf == NULL) 3003 return FALSE; 3004 3005 /* Scan the symbol table looking for SYMDEF. */ 3006 result = FALSE; 3007 for (isym = isymbuf, isymend = isymbuf + extsymcount; isym < isymend; isym++) 3008 { 3009 const char *name; 3010 3011 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3012 isym->st_name); 3013 if (name == NULL) 3014 break; 3015 3016 if (strcmp (name, symdef->name) == 0) 3017 { 3018 result = is_global_data_symbol_definition (abfd, isym); 3019 break; 3020 } 3021 } 3022 3023 free (isymbuf); 3024 3025 return result; 3026 } 3027 3028 /* Add an entry to the .dynamic table. */ 3029 3030 bfd_boolean 3031 _bfd_elf_add_dynamic_entry (struct bfd_link_info *info, 3032 bfd_vma tag, 3033 bfd_vma val) 3034 { 3035 struct elf_link_hash_table *hash_table; 3036 const struct elf_backend_data *bed; 3037 asection *s; 3038 bfd_size_type newsize; 3039 bfd_byte *newcontents; 3040 Elf_Internal_Dyn dyn; 3041 3042 hash_table = elf_hash_table (info); 3043 if (! is_elf_hash_table (hash_table)) 3044 return FALSE; 3045 3046 bed = get_elf_backend_data (hash_table->dynobj); 3047 s = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3048 BFD_ASSERT (s != NULL); 3049 3050 newsize = s->size + bed->s->sizeof_dyn; 3051 newcontents = (bfd_byte *) bfd_realloc (s->contents, newsize); 3052 if (newcontents == NULL) 3053 return FALSE; 3054 3055 dyn.d_tag = tag; 3056 dyn.d_un.d_val = val; 3057 bed->s->swap_dyn_out (hash_table->dynobj, &dyn, newcontents + s->size); 3058 3059 s->size = newsize; 3060 s->contents = newcontents; 3061 3062 return TRUE; 3063 } 3064 3065 /* Add a DT_NEEDED entry for this dynamic object if DO_IT is true, 3066 otherwise just check whether one already exists. Returns -1 on error, 3067 1 if a DT_NEEDED tag already exists, and 0 on success. */ 3068 3069 static int 3070 elf_add_dt_needed_tag (bfd *abfd, 3071 struct bfd_link_info *info, 3072 const char *soname, 3073 bfd_boolean do_it) 3074 { 3075 struct elf_link_hash_table *hash_table; 3076 bfd_size_type strindex; 3077 3078 if (!_bfd_elf_link_create_dynstrtab (abfd, info)) 3079 return -1; 3080 3081 hash_table = elf_hash_table (info); 3082 strindex = _bfd_elf_strtab_add (hash_table->dynstr, soname, FALSE); 3083 if (strindex == (bfd_size_type) -1) 3084 return -1; 3085 3086 if (_bfd_elf_strtab_refcount (hash_table->dynstr, strindex) != 1) 3087 { 3088 asection *sdyn; 3089 const struct elf_backend_data *bed; 3090 bfd_byte *extdyn; 3091 3092 bed = get_elf_backend_data (hash_table->dynobj); 3093 sdyn = bfd_get_linker_section (hash_table->dynobj, ".dynamic"); 3094 if (sdyn != NULL) 3095 for (extdyn = sdyn->contents; 3096 extdyn < sdyn->contents + sdyn->size; 3097 extdyn += bed->s->sizeof_dyn) 3098 { 3099 Elf_Internal_Dyn dyn; 3100 3101 bed->s->swap_dyn_in (hash_table->dynobj, extdyn, &dyn); 3102 if (dyn.d_tag == DT_NEEDED 3103 && dyn.d_un.d_val == strindex) 3104 { 3105 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3106 return 1; 3107 } 3108 } 3109 } 3110 3111 if (do_it) 3112 { 3113 if (!_bfd_elf_link_create_dynamic_sections (hash_table->dynobj, info)) 3114 return -1; 3115 3116 if (!_bfd_elf_add_dynamic_entry (info, DT_NEEDED, strindex)) 3117 return -1; 3118 } 3119 else 3120 /* We were just checking for existence of the tag. */ 3121 _bfd_elf_strtab_delref (hash_table->dynstr, strindex); 3122 3123 return 0; 3124 } 3125 3126 static bfd_boolean 3127 on_needed_list (const char *soname, struct bfd_link_needed_list *needed) 3128 { 3129 for (; needed != NULL; needed = needed->next) 3130 if (strcmp (soname, needed->name) == 0) 3131 return TRUE; 3132 3133 return FALSE; 3134 } 3135 3136 /* Sort symbol by value, section, and size. */ 3137 static int 3138 elf_sort_symbol (const void *arg1, const void *arg2) 3139 { 3140 const struct elf_link_hash_entry *h1; 3141 const struct elf_link_hash_entry *h2; 3142 bfd_signed_vma vdiff; 3143 3144 h1 = *(const struct elf_link_hash_entry **) arg1; 3145 h2 = *(const struct elf_link_hash_entry **) arg2; 3146 vdiff = h1->root.u.def.value - h2->root.u.def.value; 3147 if (vdiff != 0) 3148 return vdiff > 0 ? 1 : -1; 3149 else 3150 { 3151 long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id; 3152 if (sdiff != 0) 3153 return sdiff > 0 ? 1 : -1; 3154 } 3155 vdiff = h1->size - h2->size; 3156 return vdiff == 0 ? 0 : vdiff > 0 ? 1 : -1; 3157 } 3158 3159 /* This function is used to adjust offsets into .dynstr for 3160 dynamic symbols. This is called via elf_link_hash_traverse. */ 3161 3162 static bfd_boolean 3163 elf_adjust_dynstr_offsets (struct elf_link_hash_entry *h, void *data) 3164 { 3165 struct elf_strtab_hash *dynstr = (struct elf_strtab_hash *) data; 3166 3167 if (h->dynindx != -1) 3168 h->dynstr_index = _bfd_elf_strtab_offset (dynstr, h->dynstr_index); 3169 return TRUE; 3170 } 3171 3172 /* Assign string offsets in .dynstr, update all structures referencing 3173 them. */ 3174 3175 static bfd_boolean 3176 elf_finalize_dynstr (bfd *output_bfd, struct bfd_link_info *info) 3177 { 3178 struct elf_link_hash_table *hash_table = elf_hash_table (info); 3179 struct elf_link_local_dynamic_entry *entry; 3180 struct elf_strtab_hash *dynstr = hash_table->dynstr; 3181 bfd *dynobj = hash_table->dynobj; 3182 asection *sdyn; 3183 bfd_size_type size; 3184 const struct elf_backend_data *bed; 3185 bfd_byte *extdyn; 3186 3187 _bfd_elf_strtab_finalize (dynstr); 3188 size = _bfd_elf_strtab_size (dynstr); 3189 3190 bed = get_elf_backend_data (dynobj); 3191 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 3192 BFD_ASSERT (sdyn != NULL); 3193 3194 /* Update all .dynamic entries referencing .dynstr strings. */ 3195 for (extdyn = sdyn->contents; 3196 extdyn < sdyn->contents + sdyn->size; 3197 extdyn += bed->s->sizeof_dyn) 3198 { 3199 Elf_Internal_Dyn dyn; 3200 3201 bed->s->swap_dyn_in (dynobj, extdyn, &dyn); 3202 switch (dyn.d_tag) 3203 { 3204 case DT_STRSZ: 3205 dyn.d_un.d_val = size; 3206 break; 3207 case DT_NEEDED: 3208 case DT_SONAME: 3209 case DT_RPATH: 3210 case DT_RUNPATH: 3211 case DT_FILTER: 3212 case DT_AUXILIARY: 3213 case DT_AUDIT: 3214 case DT_DEPAUDIT: 3215 dyn.d_un.d_val = _bfd_elf_strtab_offset (dynstr, dyn.d_un.d_val); 3216 break; 3217 default: 3218 continue; 3219 } 3220 bed->s->swap_dyn_out (dynobj, &dyn, extdyn); 3221 } 3222 3223 /* Now update local dynamic symbols. */ 3224 for (entry = hash_table->dynlocal; entry ; entry = entry->next) 3225 entry->isym.st_name = _bfd_elf_strtab_offset (dynstr, 3226 entry->isym.st_name); 3227 3228 /* And the rest of dynamic symbols. */ 3229 elf_link_hash_traverse (hash_table, elf_adjust_dynstr_offsets, dynstr); 3230 3231 /* Adjust version definitions. */ 3232 if (elf_tdata (output_bfd)->cverdefs) 3233 { 3234 asection *s; 3235 bfd_byte *p; 3236 bfd_size_type i; 3237 Elf_Internal_Verdef def; 3238 Elf_Internal_Verdaux defaux; 3239 3240 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 3241 p = s->contents; 3242 do 3243 { 3244 _bfd_elf_swap_verdef_in (output_bfd, (Elf_External_Verdef *) p, 3245 &def); 3246 p += sizeof (Elf_External_Verdef); 3247 if (def.vd_aux != sizeof (Elf_External_Verdef)) 3248 continue; 3249 for (i = 0; i < def.vd_cnt; ++i) 3250 { 3251 _bfd_elf_swap_verdaux_in (output_bfd, 3252 (Elf_External_Verdaux *) p, &defaux); 3253 defaux.vda_name = _bfd_elf_strtab_offset (dynstr, 3254 defaux.vda_name); 3255 _bfd_elf_swap_verdaux_out (output_bfd, 3256 &defaux, (Elf_External_Verdaux *) p); 3257 p += sizeof (Elf_External_Verdaux); 3258 } 3259 } 3260 while (def.vd_next); 3261 } 3262 3263 /* Adjust version references. */ 3264 if (elf_tdata (output_bfd)->verref) 3265 { 3266 asection *s; 3267 bfd_byte *p; 3268 bfd_size_type i; 3269 Elf_Internal_Verneed need; 3270 Elf_Internal_Vernaux needaux; 3271 3272 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 3273 p = s->contents; 3274 do 3275 { 3276 _bfd_elf_swap_verneed_in (output_bfd, (Elf_External_Verneed *) p, 3277 &need); 3278 need.vn_file = _bfd_elf_strtab_offset (dynstr, need.vn_file); 3279 _bfd_elf_swap_verneed_out (output_bfd, &need, 3280 (Elf_External_Verneed *) p); 3281 p += sizeof (Elf_External_Verneed); 3282 for (i = 0; i < need.vn_cnt; ++i) 3283 { 3284 _bfd_elf_swap_vernaux_in (output_bfd, 3285 (Elf_External_Vernaux *) p, &needaux); 3286 needaux.vna_name = _bfd_elf_strtab_offset (dynstr, 3287 needaux.vna_name); 3288 _bfd_elf_swap_vernaux_out (output_bfd, 3289 &needaux, 3290 (Elf_External_Vernaux *) p); 3291 p += sizeof (Elf_External_Vernaux); 3292 } 3293 } 3294 while (need.vn_next); 3295 } 3296 3297 return TRUE; 3298 } 3299 3300 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3301 The default is to only match when the INPUT and OUTPUT are exactly 3302 the same target. */ 3303 3304 bfd_boolean 3305 _bfd_elf_default_relocs_compatible (const bfd_target *input, 3306 const bfd_target *output) 3307 { 3308 return input == output; 3309 } 3310 3311 /* Return TRUE iff relocations for INPUT are compatible with OUTPUT. 3312 This version is used when different targets for the same architecture 3313 are virtually identical. */ 3314 3315 bfd_boolean 3316 _bfd_elf_relocs_compatible (const bfd_target *input, 3317 const bfd_target *output) 3318 { 3319 const struct elf_backend_data *obed, *ibed; 3320 3321 if (input == output) 3322 return TRUE; 3323 3324 ibed = xvec_get_elf_backend_data (input); 3325 obed = xvec_get_elf_backend_data (output); 3326 3327 if (ibed->arch != obed->arch) 3328 return FALSE; 3329 3330 /* If both backends are using this function, deem them compatible. */ 3331 return ibed->relocs_compatible == obed->relocs_compatible; 3332 } 3333 3334 /* Add symbols from an ELF object file to the linker hash table. */ 3335 3336 static bfd_boolean 3337 elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info) 3338 { 3339 Elf_Internal_Ehdr *ehdr; 3340 Elf_Internal_Shdr *hdr; 3341 bfd_size_type symcount; 3342 bfd_size_type extsymcount; 3343 bfd_size_type extsymoff; 3344 struct elf_link_hash_entry **sym_hash; 3345 bfd_boolean dynamic; 3346 Elf_External_Versym *extversym = NULL; 3347 Elf_External_Versym *ever; 3348 struct elf_link_hash_entry *weaks; 3349 struct elf_link_hash_entry **nondeflt_vers = NULL; 3350 bfd_size_type nondeflt_vers_cnt = 0; 3351 Elf_Internal_Sym *isymbuf = NULL; 3352 Elf_Internal_Sym *isym; 3353 Elf_Internal_Sym *isymend; 3354 const struct elf_backend_data *bed; 3355 bfd_boolean add_needed; 3356 struct elf_link_hash_table *htab; 3357 bfd_size_type amt; 3358 void *alloc_mark = NULL; 3359 struct bfd_hash_entry **old_table = NULL; 3360 unsigned int old_size = 0; 3361 unsigned int old_count = 0; 3362 void *old_tab = NULL; 3363 void *old_hash; 3364 void *old_ent; 3365 struct bfd_link_hash_entry *old_undefs = NULL; 3366 struct bfd_link_hash_entry *old_undefs_tail = NULL; 3367 long old_dynsymcount = 0; 3368 bfd_size_type old_dynstr_size = 0; 3369 size_t tabsize = 0; 3370 size_t hashsize = 0; 3371 3372 htab = elf_hash_table (info); 3373 bed = get_elf_backend_data (abfd); 3374 3375 if ((abfd->flags & DYNAMIC) == 0) 3376 dynamic = FALSE; 3377 else 3378 { 3379 dynamic = TRUE; 3380 3381 /* You can't use -r against a dynamic object. Also, there's no 3382 hope of using a dynamic object which does not exactly match 3383 the format of the output file. */ 3384 if (info->relocatable 3385 || !is_elf_hash_table (htab) 3386 || info->output_bfd->xvec != abfd->xvec) 3387 { 3388 if (info->relocatable) 3389 bfd_set_error (bfd_error_invalid_operation); 3390 else 3391 bfd_set_error (bfd_error_wrong_format); 3392 goto error_return; 3393 } 3394 } 3395 3396 ehdr = elf_elfheader (abfd); 3397 if (info->warn_alternate_em 3398 && bed->elf_machine_code != ehdr->e_machine 3399 && ((bed->elf_machine_alt1 != 0 3400 && ehdr->e_machine == bed->elf_machine_alt1) 3401 || (bed->elf_machine_alt2 != 0 3402 && ehdr->e_machine == bed->elf_machine_alt2))) 3403 info->callbacks->einfo 3404 (_("%P: alternate ELF machine code found (%d) in %B, expecting %d\n"), 3405 ehdr->e_machine, abfd, bed->elf_machine_code); 3406 3407 /* As a GNU extension, any input sections which are named 3408 .gnu.warning.SYMBOL are treated as warning symbols for the given 3409 symbol. This differs from .gnu.warning sections, which generate 3410 warnings when they are included in an output file. */ 3411 /* PR 12761: Also generate this warning when building shared libraries. */ 3412 if (info->executable || info->shared) 3413 { 3414 asection *s; 3415 3416 for (s = abfd->sections; s != NULL; s = s->next) 3417 { 3418 const char *name; 3419 3420 name = bfd_get_section_name (abfd, s); 3421 if (CONST_STRNEQ (name, ".gnu.warning.")) 3422 { 3423 char *msg; 3424 bfd_size_type sz; 3425 3426 name += sizeof ".gnu.warning." - 1; 3427 3428 /* If this is a shared object, then look up the symbol 3429 in the hash table. If it is there, and it is already 3430 been defined, then we will not be using the entry 3431 from this shared object, so we don't need to warn. 3432 FIXME: If we see the definition in a regular object 3433 later on, we will warn, but we shouldn't. The only 3434 fix is to keep track of what warnings we are supposed 3435 to emit, and then handle them all at the end of the 3436 link. */ 3437 if (dynamic) 3438 { 3439 struct elf_link_hash_entry *h; 3440 3441 h = elf_link_hash_lookup (htab, name, FALSE, FALSE, TRUE); 3442 3443 /* FIXME: What about bfd_link_hash_common? */ 3444 if (h != NULL 3445 && (h->root.type == bfd_link_hash_defined 3446 || h->root.type == bfd_link_hash_defweak)) 3447 { 3448 /* We don't want to issue this warning. Clobber 3449 the section size so that the warning does not 3450 get copied into the output file. */ 3451 s->size = 0; 3452 continue; 3453 } 3454 } 3455 3456 sz = s->size; 3457 msg = (char *) bfd_alloc (abfd, sz + 1); 3458 if (msg == NULL) 3459 goto error_return; 3460 3461 if (! bfd_get_section_contents (abfd, s, msg, 0, sz)) 3462 goto error_return; 3463 3464 msg[sz] = '\0'; 3465 3466 if (! (_bfd_generic_link_add_one_symbol 3467 (info, abfd, name, BSF_WARNING, s, 0, msg, 3468 FALSE, bed->collect, NULL))) 3469 goto error_return; 3470 3471 if (! info->relocatable) 3472 { 3473 /* Clobber the section size so that the warning does 3474 not get copied into the output file. */ 3475 s->size = 0; 3476 3477 /* Also set SEC_EXCLUDE, so that symbols defined in 3478 the warning section don't get copied to the output. */ 3479 s->flags |= SEC_EXCLUDE; 3480 } 3481 } 3482 } 3483 } 3484 3485 add_needed = TRUE; 3486 if (! dynamic) 3487 { 3488 /* If we are creating a shared library, create all the dynamic 3489 sections immediately. We need to attach them to something, 3490 so we attach them to this BFD, provided it is the right 3491 format. FIXME: If there are no input BFD's of the same 3492 format as the output, we can't make a shared library. */ 3493 if (info->shared 3494 && is_elf_hash_table (htab) 3495 && info->output_bfd->xvec == abfd->xvec 3496 && !htab->dynamic_sections_created) 3497 { 3498 if (! _bfd_elf_link_create_dynamic_sections (abfd, info)) 3499 goto error_return; 3500 } 3501 } 3502 else if (!is_elf_hash_table (htab)) 3503 goto error_return; 3504 else 3505 { 3506 asection *s; 3507 const char *soname = NULL; 3508 char *audit = NULL; 3509 struct bfd_link_needed_list *rpath = NULL, *runpath = NULL; 3510 int ret; 3511 3512 /* ld --just-symbols and dynamic objects don't mix very well. 3513 ld shouldn't allow it. */ 3514 if ((s = abfd->sections) != NULL 3515 && s->sec_info_type == SEC_INFO_TYPE_JUST_SYMS) 3516 abort (); 3517 3518 /* If this dynamic lib was specified on the command line with 3519 --as-needed in effect, then we don't want to add a DT_NEEDED 3520 tag unless the lib is actually used. Similary for libs brought 3521 in by another lib's DT_NEEDED. When --no-add-needed is used 3522 on a dynamic lib, we don't want to add a DT_NEEDED entry for 3523 any dynamic library in DT_NEEDED tags in the dynamic lib at 3524 all. */ 3525 add_needed = (elf_dyn_lib_class (abfd) 3526 & (DYN_AS_NEEDED | DYN_DT_NEEDED 3527 | DYN_NO_NEEDED)) == 0; 3528 3529 s = bfd_get_section_by_name (abfd, ".dynamic"); 3530 if (s != NULL) 3531 { 3532 bfd_byte *dynbuf; 3533 bfd_byte *extdyn; 3534 unsigned int elfsec; 3535 unsigned long shlink; 3536 3537 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 3538 { 3539 error_free_dyn: 3540 free (dynbuf); 3541 goto error_return; 3542 } 3543 3544 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 3545 if (elfsec == SHN_BAD) 3546 goto error_free_dyn; 3547 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 3548 3549 for (extdyn = dynbuf; 3550 extdyn < dynbuf + s->size; 3551 extdyn += bed->s->sizeof_dyn) 3552 { 3553 Elf_Internal_Dyn dyn; 3554 3555 bed->s->swap_dyn_in (abfd, extdyn, &dyn); 3556 if (dyn.d_tag == DT_SONAME) 3557 { 3558 unsigned int tagv = dyn.d_un.d_val; 3559 soname = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3560 if (soname == NULL) 3561 goto error_free_dyn; 3562 } 3563 if (dyn.d_tag == DT_NEEDED) 3564 { 3565 struct bfd_link_needed_list *n, **pn; 3566 char *fnm, *anm; 3567 unsigned int tagv = dyn.d_un.d_val; 3568 3569 amt = sizeof (struct bfd_link_needed_list); 3570 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3571 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3572 if (n == NULL || fnm == NULL) 3573 goto error_free_dyn; 3574 amt = strlen (fnm) + 1; 3575 anm = (char *) bfd_alloc (abfd, amt); 3576 if (anm == NULL) 3577 goto error_free_dyn; 3578 memcpy (anm, fnm, amt); 3579 n->name = anm; 3580 n->by = abfd; 3581 n->next = NULL; 3582 for (pn = &htab->needed; *pn != NULL; pn = &(*pn)->next) 3583 ; 3584 *pn = n; 3585 } 3586 if (dyn.d_tag == DT_RUNPATH) 3587 { 3588 struct bfd_link_needed_list *n, **pn; 3589 char *fnm, *anm; 3590 unsigned int tagv = dyn.d_un.d_val; 3591 3592 amt = sizeof (struct bfd_link_needed_list); 3593 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3594 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3595 if (n == NULL || fnm == NULL) 3596 goto error_free_dyn; 3597 amt = strlen (fnm) + 1; 3598 anm = (char *) bfd_alloc (abfd, amt); 3599 if (anm == NULL) 3600 goto error_free_dyn; 3601 memcpy (anm, fnm, amt); 3602 n->name = anm; 3603 n->by = abfd; 3604 n->next = NULL; 3605 for (pn = & runpath; 3606 *pn != NULL; 3607 pn = &(*pn)->next) 3608 ; 3609 *pn = n; 3610 } 3611 /* Ignore DT_RPATH if we have seen DT_RUNPATH. */ 3612 if (!runpath && dyn.d_tag == DT_RPATH) 3613 { 3614 struct bfd_link_needed_list *n, **pn; 3615 char *fnm, *anm; 3616 unsigned int tagv = dyn.d_un.d_val; 3617 3618 amt = sizeof (struct bfd_link_needed_list); 3619 n = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 3620 fnm = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3621 if (n == NULL || fnm == NULL) 3622 goto error_free_dyn; 3623 amt = strlen (fnm) + 1; 3624 anm = (char *) bfd_alloc (abfd, amt); 3625 if (anm == NULL) 3626 goto error_free_dyn; 3627 memcpy (anm, fnm, amt); 3628 n->name = anm; 3629 n->by = abfd; 3630 n->next = NULL; 3631 for (pn = & rpath; 3632 *pn != NULL; 3633 pn = &(*pn)->next) 3634 ; 3635 *pn = n; 3636 } 3637 if (dyn.d_tag == DT_AUDIT) 3638 { 3639 unsigned int tagv = dyn.d_un.d_val; 3640 audit = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 3641 } 3642 } 3643 3644 free (dynbuf); 3645 } 3646 3647 /* DT_RUNPATH overrides DT_RPATH. Do _NOT_ bfd_release, as that 3648 frees all more recently bfd_alloc'd blocks as well. */ 3649 if (runpath) 3650 rpath = runpath; 3651 3652 if (rpath) 3653 { 3654 struct bfd_link_needed_list **pn; 3655 for (pn = &htab->runpath; *pn != NULL; pn = &(*pn)->next) 3656 ; 3657 *pn = rpath; 3658 } 3659 3660 /* We do not want to include any of the sections in a dynamic 3661 object in the output file. We hack by simply clobbering the 3662 list of sections in the BFD. This could be handled more 3663 cleanly by, say, a new section flag; the existing 3664 SEC_NEVER_LOAD flag is not the one we want, because that one 3665 still implies that the section takes up space in the output 3666 file. */ 3667 bfd_section_list_clear (abfd); 3668 3669 /* Find the name to use in a DT_NEEDED entry that refers to this 3670 object. If the object has a DT_SONAME entry, we use it. 3671 Otherwise, if the generic linker stuck something in 3672 elf_dt_name, we use that. Otherwise, we just use the file 3673 name. */ 3674 if (soname == NULL || *soname == '\0') 3675 { 3676 soname = elf_dt_name (abfd); 3677 if (soname == NULL || *soname == '\0') 3678 soname = bfd_get_filename (abfd); 3679 } 3680 3681 /* Save the SONAME because sometimes the linker emulation code 3682 will need to know it. */ 3683 elf_dt_name (abfd) = soname; 3684 3685 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 3686 if (ret < 0) 3687 goto error_return; 3688 3689 /* If we have already included this dynamic object in the 3690 link, just ignore it. There is no reason to include a 3691 particular dynamic object more than once. */ 3692 if (ret > 0) 3693 return TRUE; 3694 3695 /* Save the DT_AUDIT entry for the linker emulation code. */ 3696 elf_dt_audit (abfd) = audit; 3697 } 3698 3699 /* If this is a dynamic object, we always link against the .dynsym 3700 symbol table, not the .symtab symbol table. The dynamic linker 3701 will only see the .dynsym symbol table, so there is no reason to 3702 look at .symtab for a dynamic object. */ 3703 3704 if (! dynamic || elf_dynsymtab (abfd) == 0) 3705 hdr = &elf_tdata (abfd)->symtab_hdr; 3706 else 3707 hdr = &elf_tdata (abfd)->dynsymtab_hdr; 3708 3709 symcount = hdr->sh_size / bed->s->sizeof_sym; 3710 3711 /* The sh_info field of the symtab header tells us where the 3712 external symbols start. We don't care about the local symbols at 3713 this point. */ 3714 if (elf_bad_symtab (abfd)) 3715 { 3716 extsymcount = symcount; 3717 extsymoff = 0; 3718 } 3719 else 3720 { 3721 extsymcount = symcount - hdr->sh_info; 3722 extsymoff = hdr->sh_info; 3723 } 3724 3725 sym_hash = NULL; 3726 if (extsymcount != 0) 3727 { 3728 isymbuf = bfd_elf_get_elf_syms (abfd, hdr, extsymcount, extsymoff, 3729 NULL, NULL, NULL); 3730 if (isymbuf == NULL) 3731 goto error_return; 3732 3733 /* We store a pointer to the hash table entry for each external 3734 symbol. */ 3735 amt = extsymcount * sizeof (struct elf_link_hash_entry *); 3736 sym_hash = (struct elf_link_hash_entry **) bfd_alloc (abfd, amt); 3737 if (sym_hash == NULL) 3738 goto error_free_sym; 3739 elf_sym_hashes (abfd) = sym_hash; 3740 } 3741 3742 if (dynamic) 3743 { 3744 /* Read in any version definitions. */ 3745 if (!_bfd_elf_slurp_version_tables (abfd, 3746 info->default_imported_symver)) 3747 goto error_free_sym; 3748 3749 /* Read in the symbol versions, but don't bother to convert them 3750 to internal format. */ 3751 if (elf_dynversym (abfd) != 0) 3752 { 3753 Elf_Internal_Shdr *versymhdr; 3754 3755 versymhdr = &elf_tdata (abfd)->dynversym_hdr; 3756 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 3757 if (extversym == NULL) 3758 goto error_free_sym; 3759 amt = versymhdr->sh_size; 3760 if (bfd_seek (abfd, versymhdr->sh_offset, SEEK_SET) != 0 3761 || bfd_bread (extversym, amt, abfd) != amt) 3762 goto error_free_vers; 3763 } 3764 } 3765 3766 /* If we are loading an as-needed shared lib, save the symbol table 3767 state before we start adding symbols. If the lib turns out 3768 to be unneeded, restore the state. */ 3769 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 3770 { 3771 unsigned int i; 3772 size_t entsize; 3773 3774 for (entsize = 0, i = 0; i < htab->root.table.size; i++) 3775 { 3776 struct bfd_hash_entry *p; 3777 struct elf_link_hash_entry *h; 3778 3779 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 3780 { 3781 h = (struct elf_link_hash_entry *) p; 3782 entsize += htab->root.table.entsize; 3783 if (h->root.type == bfd_link_hash_warning) 3784 entsize += htab->root.table.entsize; 3785 } 3786 } 3787 3788 tabsize = htab->root.table.size * sizeof (struct bfd_hash_entry *); 3789 hashsize = extsymcount * sizeof (struct elf_link_hash_entry *); 3790 old_tab = bfd_malloc (tabsize + entsize + hashsize); 3791 if (old_tab == NULL) 3792 goto error_free_vers; 3793 3794 /* Remember the current objalloc pointer, so that all mem for 3795 symbols added can later be reclaimed. */ 3796 alloc_mark = bfd_hash_allocate (&htab->root.table, 1); 3797 if (alloc_mark == NULL) 3798 goto error_free_vers; 3799 3800 /* Make a special call to the linker "notice" function to 3801 tell it that we are about to handle an as-needed lib. */ 3802 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, 3803 notice_as_needed, 0, NULL)) 3804 goto error_free_vers; 3805 3806 /* Clone the symbol table and sym hashes. Remember some 3807 pointers into the symbol table, and dynamic symbol count. */ 3808 old_hash = (char *) old_tab + tabsize; 3809 old_ent = (char *) old_hash + hashsize; 3810 memcpy (old_tab, htab->root.table.table, tabsize); 3811 memcpy (old_hash, sym_hash, hashsize); 3812 old_undefs = htab->root.undefs; 3813 old_undefs_tail = htab->root.undefs_tail; 3814 old_table = htab->root.table.table; 3815 old_size = htab->root.table.size; 3816 old_count = htab->root.table.count; 3817 old_dynsymcount = htab->dynsymcount; 3818 old_dynstr_size = _bfd_elf_strtab_size (htab->dynstr); 3819 3820 for (i = 0; i < htab->root.table.size; i++) 3821 { 3822 struct bfd_hash_entry *p; 3823 struct elf_link_hash_entry *h; 3824 3825 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 3826 { 3827 memcpy (old_ent, p, htab->root.table.entsize); 3828 old_ent = (char *) old_ent + htab->root.table.entsize; 3829 h = (struct elf_link_hash_entry *) p; 3830 if (h->root.type == bfd_link_hash_warning) 3831 { 3832 memcpy (old_ent, h->root.u.i.link, htab->root.table.entsize); 3833 old_ent = (char *) old_ent + htab->root.table.entsize; 3834 } 3835 } 3836 } 3837 } 3838 3839 weaks = NULL; 3840 ever = extversym != NULL ? extversym + extsymoff : NULL; 3841 for (isym = isymbuf, isymend = isymbuf + extsymcount; 3842 isym < isymend; 3843 isym++, sym_hash++, ever = (ever != NULL ? ever + 1 : NULL)) 3844 { 3845 int bind; 3846 bfd_vma value; 3847 asection *sec, *new_sec; 3848 flagword flags; 3849 const char *name; 3850 struct elf_link_hash_entry *h; 3851 struct elf_link_hash_entry *hi; 3852 bfd_boolean definition; 3853 bfd_boolean size_change_ok; 3854 bfd_boolean type_change_ok; 3855 bfd_boolean new_weakdef; 3856 bfd_boolean new_weak; 3857 bfd_boolean old_weak; 3858 bfd_boolean override; 3859 bfd_boolean common; 3860 unsigned int old_alignment; 3861 bfd *old_bfd; 3862 bfd * undef_bfd = NULL; 3863 3864 override = FALSE; 3865 3866 flags = BSF_NO_FLAGS; 3867 sec = NULL; 3868 value = isym->st_value; 3869 *sym_hash = NULL; 3870 common = bed->common_definition (isym); 3871 3872 bind = ELF_ST_BIND (isym->st_info); 3873 switch (bind) 3874 { 3875 case STB_LOCAL: 3876 /* This should be impossible, since ELF requires that all 3877 global symbols follow all local symbols, and that sh_info 3878 point to the first global symbol. Unfortunately, Irix 5 3879 screws this up. */ 3880 continue; 3881 3882 case STB_GLOBAL: 3883 if (isym->st_shndx != SHN_UNDEF && !common) 3884 flags = BSF_GLOBAL; 3885 break; 3886 3887 case STB_WEAK: 3888 flags = BSF_WEAK; 3889 break; 3890 3891 case STB_GNU_UNIQUE: 3892 flags = BSF_GNU_UNIQUE; 3893 break; 3894 3895 default: 3896 /* Leave it up to the processor backend. */ 3897 break; 3898 } 3899 3900 if (isym->st_shndx == SHN_UNDEF) 3901 sec = bfd_und_section_ptr; 3902 else if (isym->st_shndx == SHN_ABS) 3903 sec = bfd_abs_section_ptr; 3904 else if (isym->st_shndx == SHN_COMMON) 3905 { 3906 sec = bfd_com_section_ptr; 3907 /* What ELF calls the size we call the value. What ELF 3908 calls the value we call the alignment. */ 3909 value = isym->st_size; 3910 } 3911 else 3912 { 3913 sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 3914 if (sec == NULL) 3915 sec = bfd_abs_section_ptr; 3916 else if (discarded_section (sec)) 3917 { 3918 /* Symbols from discarded section are undefined. We keep 3919 its visibility. */ 3920 sec = bfd_und_section_ptr; 3921 isym->st_shndx = SHN_UNDEF; 3922 } 3923 else if ((abfd->flags & (EXEC_P | DYNAMIC)) != 0) 3924 value -= sec->vma; 3925 } 3926 3927 name = bfd_elf_string_from_elf_section (abfd, hdr->sh_link, 3928 isym->st_name); 3929 if (name == NULL) 3930 goto error_free_vers; 3931 3932 if (isym->st_shndx == SHN_COMMON 3933 && (abfd->flags & BFD_PLUGIN) != 0) 3934 { 3935 asection *xc = bfd_get_section_by_name (abfd, "COMMON"); 3936 3937 if (xc == NULL) 3938 { 3939 flagword sflags = (SEC_ALLOC | SEC_IS_COMMON | SEC_KEEP 3940 | SEC_EXCLUDE); 3941 xc = bfd_make_section_with_flags (abfd, "COMMON", sflags); 3942 if (xc == NULL) 3943 goto error_free_vers; 3944 } 3945 sec = xc; 3946 } 3947 else if (isym->st_shndx == SHN_COMMON 3948 && ELF_ST_TYPE (isym->st_info) == STT_TLS 3949 && !info->relocatable) 3950 { 3951 asection *tcomm = bfd_get_section_by_name (abfd, ".tcommon"); 3952 3953 if (tcomm == NULL) 3954 { 3955 flagword sflags = (SEC_ALLOC | SEC_THREAD_LOCAL | SEC_IS_COMMON 3956 | SEC_LINKER_CREATED); 3957 tcomm = bfd_make_section_with_flags (abfd, ".tcommon", sflags); 3958 if (tcomm == NULL) 3959 goto error_free_vers; 3960 } 3961 sec = tcomm; 3962 } 3963 else if (bed->elf_add_symbol_hook) 3964 { 3965 if (! (*bed->elf_add_symbol_hook) (abfd, info, isym, &name, &flags, 3966 &sec, &value)) 3967 goto error_free_vers; 3968 3969 /* The hook function sets the name to NULL if this symbol 3970 should be skipped for some reason. */ 3971 if (name == NULL) 3972 continue; 3973 } 3974 3975 /* Sanity check that all possibilities were handled. */ 3976 if (sec == NULL) 3977 { 3978 bfd_set_error (bfd_error_bad_value); 3979 goto error_free_vers; 3980 } 3981 3982 if (bfd_is_und_section (sec) 3983 || bfd_is_com_section (sec)) 3984 definition = FALSE; 3985 else 3986 definition = TRUE; 3987 3988 size_change_ok = FALSE; 3989 type_change_ok = bed->type_change_ok; 3990 old_weak = FALSE; 3991 old_alignment = 0; 3992 old_bfd = NULL; 3993 new_sec = sec; 3994 3995 if (is_elf_hash_table (htab)) 3996 { 3997 Elf_Internal_Versym iver; 3998 unsigned int vernum = 0; 3999 bfd_boolean skip; 4000 4001 /* If this is a definition of a symbol which was previously 4002 referenced in a non-weak manner then make a note of the bfd 4003 that contained the reference. This is used if we need to 4004 refer to the source of the reference later on. */ 4005 if (! bfd_is_und_section (sec)) 4006 { 4007 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 4008 4009 if (h != NULL 4010 && h->root.type == bfd_link_hash_undefined 4011 && h->root.u.undef.abfd) 4012 undef_bfd = h->root.u.undef.abfd; 4013 } 4014 4015 if (ever == NULL) 4016 { 4017 if (info->default_imported_symver) 4018 /* Use the default symbol version created earlier. */ 4019 iver.vs_vers = elf_tdata (abfd)->cverdefs; 4020 else 4021 iver.vs_vers = 0; 4022 } 4023 else 4024 _bfd_elf_swap_versym_in (abfd, ever, &iver); 4025 4026 vernum = iver.vs_vers & VERSYM_VERSION; 4027 4028 /* If this is a hidden symbol, or if it is not version 4029 1, we append the version name to the symbol name. 4030 However, we do not modify a non-hidden absolute symbol 4031 if it is not a function, because it might be the version 4032 symbol itself. FIXME: What if it isn't? */ 4033 if ((iver.vs_vers & VERSYM_HIDDEN) != 0 4034 || (vernum > 1 4035 && (!bfd_is_abs_section (sec) 4036 || bed->is_function_type (ELF_ST_TYPE (isym->st_info))))) 4037 { 4038 const char *verstr; 4039 size_t namelen, verlen, newlen; 4040 char *newname, *p; 4041 4042 if (isym->st_shndx != SHN_UNDEF) 4043 { 4044 if (vernum > elf_tdata (abfd)->cverdefs) 4045 verstr = NULL; 4046 else if (vernum > 1) 4047 verstr = 4048 elf_tdata (abfd)->verdef[vernum - 1].vd_nodename; 4049 else 4050 verstr = ""; 4051 4052 if (verstr == NULL) 4053 { 4054 (*_bfd_error_handler) 4055 (_("%B: %s: invalid version %u (max %d)"), 4056 abfd, name, vernum, 4057 elf_tdata (abfd)->cverdefs); 4058 bfd_set_error (bfd_error_bad_value); 4059 goto error_free_vers; 4060 } 4061 } 4062 else 4063 { 4064 /* We cannot simply test for the number of 4065 entries in the VERNEED section since the 4066 numbers for the needed versions do not start 4067 at 0. */ 4068 Elf_Internal_Verneed *t; 4069 4070 verstr = NULL; 4071 for (t = elf_tdata (abfd)->verref; 4072 t != NULL; 4073 t = t->vn_nextref) 4074 { 4075 Elf_Internal_Vernaux *a; 4076 4077 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 4078 { 4079 if (a->vna_other == vernum) 4080 { 4081 verstr = a->vna_nodename; 4082 break; 4083 } 4084 } 4085 if (a != NULL) 4086 break; 4087 } 4088 if (verstr == NULL) 4089 { 4090 (*_bfd_error_handler) 4091 (_("%B: %s: invalid needed version %d"), 4092 abfd, name, vernum); 4093 bfd_set_error (bfd_error_bad_value); 4094 goto error_free_vers; 4095 } 4096 } 4097 4098 namelen = strlen (name); 4099 verlen = strlen (verstr); 4100 newlen = namelen + verlen + 2; 4101 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4102 && isym->st_shndx != SHN_UNDEF) 4103 ++newlen; 4104 4105 newname = (char *) bfd_hash_allocate (&htab->root.table, newlen); 4106 if (newname == NULL) 4107 goto error_free_vers; 4108 memcpy (newname, name, namelen); 4109 p = newname + namelen; 4110 *p++ = ELF_VER_CHR; 4111 /* If this is a defined non-hidden version symbol, 4112 we add another @ to the name. This indicates the 4113 default version of the symbol. */ 4114 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 4115 && isym->st_shndx != SHN_UNDEF) 4116 *p++ = ELF_VER_CHR; 4117 memcpy (p, verstr, verlen + 1); 4118 4119 name = newname; 4120 } 4121 4122 /* If necessary, make a second attempt to locate the bfd 4123 containing an unresolved, non-weak reference to the 4124 current symbol. */ 4125 if (! bfd_is_und_section (sec) && undef_bfd == NULL) 4126 { 4127 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, FALSE); 4128 4129 if (h != NULL 4130 && h->root.type == bfd_link_hash_undefined 4131 && h->root.u.undef.abfd) 4132 undef_bfd = h->root.u.undef.abfd; 4133 } 4134 4135 if (!_bfd_elf_merge_symbol (abfd, info, name, isym, &sec, 4136 &value, &old_weak, &old_alignment, 4137 sym_hash, &skip, &override, 4138 &type_change_ok, &size_change_ok)) 4139 goto error_free_vers; 4140 4141 if (skip) 4142 continue; 4143 4144 if (override) 4145 definition = FALSE; 4146 4147 h = *sym_hash; 4148 while (h->root.type == bfd_link_hash_indirect 4149 || h->root.type == bfd_link_hash_warning) 4150 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4151 4152 /* Remember the old alignment if this is a common symbol, so 4153 that we don't reduce the alignment later on. We can't 4154 check later, because _bfd_generic_link_add_one_symbol 4155 will set a default for the alignment which we want to 4156 override. We also remember the old bfd where the existing 4157 definition comes from. */ 4158 switch (h->root.type) 4159 { 4160 default: 4161 break; 4162 4163 case bfd_link_hash_defined: 4164 case bfd_link_hash_defweak: 4165 old_bfd = h->root.u.def.section->owner; 4166 break; 4167 4168 case bfd_link_hash_common: 4169 old_bfd = h->root.u.c.p->section->owner; 4170 old_alignment = h->root.u.c.p->alignment_power; 4171 break; 4172 } 4173 4174 if (elf_tdata (abfd)->verdef != NULL 4175 && vernum > 1 4176 && definition) 4177 h->verinfo.verdef = &elf_tdata (abfd)->verdef[vernum - 1]; 4178 } 4179 4180 if (! (_bfd_generic_link_add_one_symbol 4181 (info, abfd, name, flags, sec, value, NULL, FALSE, bed->collect, 4182 (struct bfd_link_hash_entry **) sym_hash))) 4183 goto error_free_vers; 4184 4185 h = *sym_hash; 4186 /* We need to make sure that indirect symbol dynamic flags are 4187 updated. */ 4188 hi = h; 4189 while (h->root.type == bfd_link_hash_indirect 4190 || h->root.type == bfd_link_hash_warning) 4191 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4192 4193 *sym_hash = h; 4194 if (is_elf_hash_table (htab)) 4195 h->unique_global = (flags & BSF_GNU_UNIQUE) != 0; 4196 4197 new_weak = (flags & BSF_WEAK) != 0; 4198 new_weakdef = FALSE; 4199 if (dynamic 4200 && definition 4201 && new_weak 4202 && !bed->is_function_type (ELF_ST_TYPE (isym->st_info)) 4203 && is_elf_hash_table (htab) 4204 && h->u.weakdef == NULL) 4205 { 4206 /* Keep a list of all weak defined non function symbols from 4207 a dynamic object, using the weakdef field. Later in this 4208 function we will set the weakdef field to the correct 4209 value. We only put non-function symbols from dynamic 4210 objects on this list, because that happens to be the only 4211 time we need to know the normal symbol corresponding to a 4212 weak symbol, and the information is time consuming to 4213 figure out. If the weakdef field is not already NULL, 4214 then this symbol was already defined by some previous 4215 dynamic object, and we will be using that previous 4216 definition anyhow. */ 4217 4218 h->u.weakdef = weaks; 4219 weaks = h; 4220 new_weakdef = TRUE; 4221 } 4222 4223 /* Set the alignment of a common symbol. */ 4224 if ((common || bfd_is_com_section (sec)) 4225 && h->root.type == bfd_link_hash_common) 4226 { 4227 unsigned int align; 4228 4229 if (common) 4230 align = bfd_log2 (isym->st_value); 4231 else 4232 { 4233 /* The new symbol is a common symbol in a shared object. 4234 We need to get the alignment from the section. */ 4235 align = new_sec->alignment_power; 4236 } 4237 if (align > old_alignment) 4238 h->root.u.c.p->alignment_power = align; 4239 else 4240 h->root.u.c.p->alignment_power = old_alignment; 4241 } 4242 4243 if (is_elf_hash_table (htab)) 4244 { 4245 bfd_boolean dynsym; 4246 4247 /* Check the alignment when a common symbol is involved. This 4248 can change when a common symbol is overridden by a normal 4249 definition or a common symbol is ignored due to the old 4250 normal definition. We need to make sure the maximum 4251 alignment is maintained. */ 4252 if ((old_alignment || common) 4253 && h->root.type != bfd_link_hash_common) 4254 { 4255 unsigned int common_align; 4256 unsigned int normal_align; 4257 unsigned int symbol_align; 4258 bfd *normal_bfd; 4259 bfd *common_bfd; 4260 4261 symbol_align = ffs (h->root.u.def.value) - 1; 4262 if (h->root.u.def.section->owner != NULL 4263 && (h->root.u.def.section->owner->flags & DYNAMIC) == 0) 4264 { 4265 normal_align = h->root.u.def.section->alignment_power; 4266 if (normal_align > symbol_align) 4267 normal_align = symbol_align; 4268 } 4269 else 4270 normal_align = symbol_align; 4271 4272 if (old_alignment) 4273 { 4274 common_align = old_alignment; 4275 common_bfd = old_bfd; 4276 normal_bfd = abfd; 4277 } 4278 else 4279 { 4280 common_align = bfd_log2 (isym->st_value); 4281 common_bfd = abfd; 4282 normal_bfd = old_bfd; 4283 } 4284 4285 if (normal_align < common_align) 4286 { 4287 /* PR binutils/2735 */ 4288 if (normal_bfd == NULL) 4289 (*_bfd_error_handler) 4290 (_("Warning: alignment %u of common symbol `%s' in %B" 4291 " is greater than the alignment (%u) of its section %A"), 4292 common_bfd, h->root.u.def.section, 4293 1 << common_align, name, 1 << normal_align); 4294 else 4295 (*_bfd_error_handler) 4296 (_("Warning: alignment %u of symbol `%s' in %B" 4297 " is smaller than %u in %B"), 4298 normal_bfd, common_bfd, 4299 1 << normal_align, name, 1 << common_align); 4300 } 4301 } 4302 4303 /* Remember the symbol size if it isn't undefined. */ 4304 if ((isym->st_size != 0 && isym->st_shndx != SHN_UNDEF) 4305 && (definition || h->size == 0)) 4306 { 4307 if (h->size != 0 4308 && h->size != isym->st_size 4309 && ! size_change_ok) 4310 (*_bfd_error_handler) 4311 (_("Warning: size of symbol `%s' changed" 4312 " from %lu in %B to %lu in %B"), 4313 old_bfd, abfd, 4314 name, (unsigned long) h->size, 4315 (unsigned long) isym->st_size); 4316 4317 h->size = isym->st_size; 4318 } 4319 4320 /* If this is a common symbol, then we always want H->SIZE 4321 to be the size of the common symbol. The code just above 4322 won't fix the size if a common symbol becomes larger. We 4323 don't warn about a size change here, because that is 4324 covered by --warn-common. Allow changed between different 4325 function types. */ 4326 if (h->root.type == bfd_link_hash_common) 4327 h->size = h->root.u.c.size; 4328 4329 if (ELF_ST_TYPE (isym->st_info) != STT_NOTYPE 4330 && ((definition && !new_weak) 4331 || (old_weak && h->root.type == bfd_link_hash_common) 4332 || h->type == STT_NOTYPE)) 4333 { 4334 unsigned int type = ELF_ST_TYPE (isym->st_info); 4335 4336 /* Turn an IFUNC symbol from a DSO into a normal FUNC 4337 symbol. */ 4338 if (type == STT_GNU_IFUNC 4339 && (abfd->flags & DYNAMIC) != 0) 4340 type = STT_FUNC; 4341 4342 if (h->type != type) 4343 { 4344 if (h->type != STT_NOTYPE && ! type_change_ok) 4345 (*_bfd_error_handler) 4346 (_("Warning: type of symbol `%s' changed" 4347 " from %d to %d in %B"), 4348 abfd, name, h->type, type); 4349 4350 h->type = type; 4351 } 4352 } 4353 4354 /* Merge st_other field. */ 4355 elf_merge_st_other (abfd, h, isym, definition, dynamic); 4356 4357 /* Set a flag in the hash table entry indicating the type of 4358 reference or definition we just found. Keep a count of 4359 the number of dynamic symbols we find. A dynamic symbol 4360 is one which is referenced or defined by both a regular 4361 object and a shared object. */ 4362 dynsym = FALSE; 4363 if (! dynamic) 4364 { 4365 if (! definition) 4366 { 4367 h->ref_regular = 1; 4368 if (bind != STB_WEAK) 4369 h->ref_regular_nonweak = 1; 4370 } 4371 else 4372 { 4373 h->def_regular = 1; 4374 if (h->def_dynamic) 4375 { 4376 h->def_dynamic = 0; 4377 h->ref_dynamic = 1; 4378 } 4379 } 4380 4381 /* If the indirect symbol has been forced local, don't 4382 make the real symbol dynamic. */ 4383 if ((h == hi || !hi->forced_local) 4384 && (! info->executable 4385 || h->def_dynamic 4386 || h->ref_dynamic)) 4387 dynsym = TRUE; 4388 } 4389 else 4390 { 4391 if (! definition) 4392 { 4393 h->ref_dynamic = 1; 4394 hi->ref_dynamic = 1; 4395 } 4396 else 4397 { 4398 h->def_dynamic = 1; 4399 hi->def_dynamic = 1; 4400 } 4401 4402 /* If the indirect symbol has been forced local, don't 4403 make the real symbol dynamic. */ 4404 if ((h == hi || !hi->forced_local) 4405 && (h->def_regular 4406 || h->ref_regular 4407 || (h->u.weakdef != NULL 4408 && ! new_weakdef 4409 && h->u.weakdef->dynindx != -1))) 4410 dynsym = TRUE; 4411 } 4412 4413 /* We don't want to make debug symbol dynamic. */ 4414 if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable) 4415 dynsym = FALSE; 4416 4417 /* Nor should we make plugin symbols dynamic. */ 4418 if ((abfd->flags & BFD_PLUGIN) != 0) 4419 dynsym = FALSE; 4420 4421 if (definition) 4422 h->target_internal = isym->st_target_internal; 4423 4424 /* Check to see if we need to add an indirect symbol for 4425 the default name. */ 4426 if (definition || h->root.type == bfd_link_hash_common) 4427 if (!_bfd_elf_add_default_symbol (abfd, info, h, name, isym, 4428 &sec, &value, &dynsym, 4429 override)) 4430 goto error_free_vers; 4431 4432 if (definition && !dynamic) 4433 { 4434 char *p = strchr (name, ELF_VER_CHR); 4435 if (p != NULL && p[1] != ELF_VER_CHR) 4436 { 4437 /* Queue non-default versions so that .symver x, x@FOO 4438 aliases can be checked. */ 4439 if (!nondeflt_vers) 4440 { 4441 amt = ((isymend - isym + 1) 4442 * sizeof (struct elf_link_hash_entry *)); 4443 nondeflt_vers = 4444 (struct elf_link_hash_entry **) bfd_malloc (amt); 4445 if (!nondeflt_vers) 4446 goto error_free_vers; 4447 } 4448 nondeflt_vers[nondeflt_vers_cnt++] = h; 4449 } 4450 } 4451 4452 if (dynsym && h->dynindx == -1) 4453 { 4454 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4455 goto error_free_vers; 4456 if (h->u.weakdef != NULL 4457 && ! new_weakdef 4458 && h->u.weakdef->dynindx == -1) 4459 { 4460 if (!bfd_elf_link_record_dynamic_symbol (info, h->u.weakdef)) 4461 goto error_free_vers; 4462 } 4463 } 4464 else if (dynsym && h->dynindx != -1) 4465 /* If the symbol already has a dynamic index, but 4466 visibility says it should not be visible, turn it into 4467 a local symbol. */ 4468 switch (ELF_ST_VISIBILITY (h->other)) 4469 { 4470 case STV_INTERNAL: 4471 case STV_HIDDEN: 4472 (*bed->elf_backend_hide_symbol) (info, h, TRUE); 4473 dynsym = FALSE; 4474 break; 4475 } 4476 4477 if (!add_needed 4478 && definition 4479 && ((dynsym 4480 && h->ref_regular) 4481 || (h->ref_dynamic 4482 && (elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0 4483 && !on_needed_list (elf_dt_name (abfd), htab->needed)))) 4484 { 4485 int ret; 4486 const char *soname = elf_dt_name (abfd); 4487 4488 /* A symbol from a library loaded via DT_NEEDED of some 4489 other library is referenced by a regular object. 4490 Add a DT_NEEDED entry for it. Issue an error if 4491 --no-add-needed is used and the reference was not 4492 a weak one. */ 4493 if (undef_bfd != NULL 4494 && (elf_dyn_lib_class (abfd) & DYN_NO_NEEDED) != 0) 4495 { 4496 (*_bfd_error_handler) 4497 (_("%B: undefined reference to symbol '%s'"), 4498 undef_bfd, name); 4499 (*_bfd_error_handler) 4500 (_("note: '%s' is defined in DSO %B so try adding it to the linker command line"), 4501 abfd, name); 4502 bfd_set_error (bfd_error_invalid_operation); 4503 goto error_free_vers; 4504 } 4505 4506 elf_dyn_lib_class (abfd) = (enum dynamic_lib_link_class) 4507 (elf_dyn_lib_class (abfd) & ~DYN_AS_NEEDED); 4508 4509 add_needed = TRUE; 4510 ret = elf_add_dt_needed_tag (abfd, info, soname, add_needed); 4511 if (ret < 0) 4512 goto error_free_vers; 4513 4514 BFD_ASSERT (ret == 0); 4515 } 4516 } 4517 } 4518 4519 if (extversym != NULL) 4520 { 4521 free (extversym); 4522 extversym = NULL; 4523 } 4524 4525 if (isymbuf != NULL) 4526 { 4527 free (isymbuf); 4528 isymbuf = NULL; 4529 } 4530 4531 if ((elf_dyn_lib_class (abfd) & DYN_AS_NEEDED) != 0) 4532 { 4533 unsigned int i; 4534 4535 /* Restore the symbol table. */ 4536 if (bed->as_needed_cleanup) 4537 (*bed->as_needed_cleanup) (abfd, info); 4538 old_hash = (char *) old_tab + tabsize; 4539 old_ent = (char *) old_hash + hashsize; 4540 sym_hash = elf_sym_hashes (abfd); 4541 htab->root.table.table = old_table; 4542 htab->root.table.size = old_size; 4543 htab->root.table.count = old_count; 4544 memcpy (htab->root.table.table, old_tab, tabsize); 4545 memcpy (sym_hash, old_hash, hashsize); 4546 htab->root.undefs = old_undefs; 4547 htab->root.undefs_tail = old_undefs_tail; 4548 _bfd_elf_strtab_restore_size (htab->dynstr, old_dynstr_size); 4549 for (i = 0; i < htab->root.table.size; i++) 4550 { 4551 struct bfd_hash_entry *p; 4552 struct elf_link_hash_entry *h; 4553 bfd_size_type size; 4554 unsigned int alignment_power; 4555 4556 for (p = htab->root.table.table[i]; p != NULL; p = p->next) 4557 { 4558 h = (struct elf_link_hash_entry *) p; 4559 if (h->root.type == bfd_link_hash_warning) 4560 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4561 if (h->dynindx >= old_dynsymcount 4562 && h->dynstr_index < old_dynstr_size) 4563 _bfd_elf_strtab_delref (htab->dynstr, h->dynstr_index); 4564 4565 /* Preserve the maximum alignment and size for common 4566 symbols even if this dynamic lib isn't on DT_NEEDED 4567 since it can still be loaded at run time by another 4568 dynamic lib. */ 4569 if (h->root.type == bfd_link_hash_common) 4570 { 4571 size = h->root.u.c.size; 4572 alignment_power = h->root.u.c.p->alignment_power; 4573 } 4574 else 4575 { 4576 size = 0; 4577 alignment_power = 0; 4578 } 4579 memcpy (p, old_ent, htab->root.table.entsize); 4580 old_ent = (char *) old_ent + htab->root.table.entsize; 4581 h = (struct elf_link_hash_entry *) p; 4582 if (h->root.type == bfd_link_hash_warning) 4583 { 4584 memcpy (h->root.u.i.link, old_ent, htab->root.table.entsize); 4585 old_ent = (char *) old_ent + htab->root.table.entsize; 4586 h = (struct elf_link_hash_entry *) h->root.u.i.link; 4587 } 4588 if (h->root.type == bfd_link_hash_common) 4589 { 4590 if (size > h->root.u.c.size) 4591 h->root.u.c.size = size; 4592 if (alignment_power > h->root.u.c.p->alignment_power) 4593 h->root.u.c.p->alignment_power = alignment_power; 4594 } 4595 } 4596 } 4597 4598 /* Make a special call to the linker "notice" function to 4599 tell it that symbols added for crefs may need to be removed. */ 4600 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, 4601 notice_not_needed, 0, NULL)) 4602 goto error_free_vers; 4603 4604 free (old_tab); 4605 objalloc_free_block ((struct objalloc *) htab->root.table.memory, 4606 alloc_mark); 4607 if (nondeflt_vers != NULL) 4608 free (nondeflt_vers); 4609 return TRUE; 4610 } 4611 4612 if (old_tab != NULL) 4613 { 4614 if (!(*info->callbacks->notice) (info, NULL, abfd, NULL, 4615 notice_needed, 0, NULL)) 4616 goto error_free_vers; 4617 free (old_tab); 4618 old_tab = NULL; 4619 } 4620 4621 /* Now that all the symbols from this input file are created, handle 4622 .symver foo, foo@BAR such that any relocs against foo become foo@BAR. */ 4623 if (nondeflt_vers != NULL) 4624 { 4625 bfd_size_type cnt, symidx; 4626 4627 for (cnt = 0; cnt < nondeflt_vers_cnt; ++cnt) 4628 { 4629 struct elf_link_hash_entry *h = nondeflt_vers[cnt], *hi; 4630 char *shortname, *p; 4631 4632 p = strchr (h->root.root.string, ELF_VER_CHR); 4633 if (p == NULL 4634 || (h->root.type != bfd_link_hash_defined 4635 && h->root.type != bfd_link_hash_defweak)) 4636 continue; 4637 4638 amt = p - h->root.root.string; 4639 shortname = (char *) bfd_malloc (amt + 1); 4640 if (!shortname) 4641 goto error_free_vers; 4642 memcpy (shortname, h->root.root.string, amt); 4643 shortname[amt] = '\0'; 4644 4645 hi = (struct elf_link_hash_entry *) 4646 bfd_link_hash_lookup (&htab->root, shortname, 4647 FALSE, FALSE, FALSE); 4648 if (hi != NULL 4649 && hi->root.type == h->root.type 4650 && hi->root.u.def.value == h->root.u.def.value 4651 && hi->root.u.def.section == h->root.u.def.section) 4652 { 4653 (*bed->elf_backend_hide_symbol) (info, hi, TRUE); 4654 hi->root.type = bfd_link_hash_indirect; 4655 hi->root.u.i.link = (struct bfd_link_hash_entry *) h; 4656 (*bed->elf_backend_copy_indirect_symbol) (info, h, hi); 4657 sym_hash = elf_sym_hashes (abfd); 4658 if (sym_hash) 4659 for (symidx = 0; symidx < extsymcount; ++symidx) 4660 if (sym_hash[symidx] == hi) 4661 { 4662 sym_hash[symidx] = h; 4663 break; 4664 } 4665 } 4666 free (shortname); 4667 } 4668 free (nondeflt_vers); 4669 nondeflt_vers = NULL; 4670 } 4671 4672 /* Now set the weakdefs field correctly for all the weak defined 4673 symbols we found. The only way to do this is to search all the 4674 symbols. Since we only need the information for non functions in 4675 dynamic objects, that's the only time we actually put anything on 4676 the list WEAKS. We need this information so that if a regular 4677 object refers to a symbol defined weakly in a dynamic object, the 4678 real symbol in the dynamic object is also put in the dynamic 4679 symbols; we also must arrange for both symbols to point to the 4680 same memory location. We could handle the general case of symbol 4681 aliasing, but a general symbol alias can only be generated in 4682 assembler code, handling it correctly would be very time 4683 consuming, and other ELF linkers don't handle general aliasing 4684 either. */ 4685 if (weaks != NULL) 4686 { 4687 struct elf_link_hash_entry **hpp; 4688 struct elf_link_hash_entry **hppend; 4689 struct elf_link_hash_entry **sorted_sym_hash; 4690 struct elf_link_hash_entry *h; 4691 size_t sym_count; 4692 4693 /* Since we have to search the whole symbol list for each weak 4694 defined symbol, search time for N weak defined symbols will be 4695 O(N^2). Binary search will cut it down to O(NlogN). */ 4696 amt = extsymcount * sizeof (struct elf_link_hash_entry *); 4697 sorted_sym_hash = (struct elf_link_hash_entry **) bfd_malloc (amt); 4698 if (sorted_sym_hash == NULL) 4699 goto error_return; 4700 sym_hash = sorted_sym_hash; 4701 hpp = elf_sym_hashes (abfd); 4702 hppend = hpp + extsymcount; 4703 sym_count = 0; 4704 for (; hpp < hppend; hpp++) 4705 { 4706 h = *hpp; 4707 if (h != NULL 4708 && h->root.type == bfd_link_hash_defined 4709 && !bed->is_function_type (h->type)) 4710 { 4711 *sym_hash = h; 4712 sym_hash++; 4713 sym_count++; 4714 } 4715 } 4716 4717 qsort (sorted_sym_hash, sym_count, 4718 sizeof (struct elf_link_hash_entry *), 4719 elf_sort_symbol); 4720 4721 while (weaks != NULL) 4722 { 4723 struct elf_link_hash_entry *hlook; 4724 asection *slook; 4725 bfd_vma vlook; 4726 size_t i, j, idx; 4727 4728 hlook = weaks; 4729 weaks = hlook->u.weakdef; 4730 hlook->u.weakdef = NULL; 4731 4732 BFD_ASSERT (hlook->root.type == bfd_link_hash_defined 4733 || hlook->root.type == bfd_link_hash_defweak 4734 || hlook->root.type == bfd_link_hash_common 4735 || hlook->root.type == bfd_link_hash_indirect); 4736 slook = hlook->root.u.def.section; 4737 vlook = hlook->root.u.def.value; 4738 4739 i = 0; 4740 j = sym_count; 4741 while (i != j) 4742 { 4743 bfd_signed_vma vdiff; 4744 idx = (i + j) / 2; 4745 h = sorted_sym_hash[idx]; 4746 vdiff = vlook - h->root.u.def.value; 4747 if (vdiff < 0) 4748 j = idx; 4749 else if (vdiff > 0) 4750 i = idx + 1; 4751 else 4752 { 4753 long sdiff = slook->id - h->root.u.def.section->id; 4754 if (sdiff < 0) 4755 j = idx; 4756 else if (sdiff > 0) 4757 i = idx + 1; 4758 else 4759 break; 4760 } 4761 } 4762 4763 /* We didn't find a value/section match. */ 4764 if (i == j) 4765 continue; 4766 4767 /* With multiple aliases, or when the weak symbol is already 4768 strongly defined, we have multiple matching symbols and 4769 the binary search above may land on any of them. Step 4770 one past the matching symbol(s). */ 4771 while (++idx != j) 4772 { 4773 h = sorted_sym_hash[idx]; 4774 if (h->root.u.def.section != slook 4775 || h->root.u.def.value != vlook) 4776 break; 4777 } 4778 4779 /* Now look back over the aliases. Since we sorted by size 4780 as well as value and section, we'll choose the one with 4781 the largest size. */ 4782 while (idx-- != i) 4783 { 4784 h = sorted_sym_hash[idx]; 4785 4786 /* Stop if value or section doesn't match. */ 4787 if (h->root.u.def.section != slook 4788 || h->root.u.def.value != vlook) 4789 break; 4790 else if (h != hlook) 4791 { 4792 hlook->u.weakdef = h; 4793 4794 /* If the weak definition is in the list of dynamic 4795 symbols, make sure the real definition is put 4796 there as well. */ 4797 if (hlook->dynindx != -1 && h->dynindx == -1) 4798 { 4799 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4800 { 4801 err_free_sym_hash: 4802 free (sorted_sym_hash); 4803 goto error_return; 4804 } 4805 } 4806 4807 /* If the real definition is in the list of dynamic 4808 symbols, make sure the weak definition is put 4809 there as well. If we don't do this, then the 4810 dynamic loader might not merge the entries for the 4811 real definition and the weak definition. */ 4812 if (h->dynindx != -1 && hlook->dynindx == -1) 4813 { 4814 if (! bfd_elf_link_record_dynamic_symbol (info, hlook)) 4815 goto err_free_sym_hash; 4816 } 4817 break; 4818 } 4819 } 4820 } 4821 4822 free (sorted_sym_hash); 4823 } 4824 4825 if (bed->check_directives 4826 && !(*bed->check_directives) (abfd, info)) 4827 return FALSE; 4828 4829 /* If this object is the same format as the output object, and it is 4830 not a shared library, then let the backend look through the 4831 relocs. 4832 4833 This is required to build global offset table entries and to 4834 arrange for dynamic relocs. It is not required for the 4835 particular common case of linking non PIC code, even when linking 4836 against shared libraries, but unfortunately there is no way of 4837 knowing whether an object file has been compiled PIC or not. 4838 Looking through the relocs is not particularly time consuming. 4839 The problem is that we must either (1) keep the relocs in memory, 4840 which causes the linker to require additional runtime memory or 4841 (2) read the relocs twice from the input file, which wastes time. 4842 This would be a good case for using mmap. 4843 4844 I have no idea how to handle linking PIC code into a file of a 4845 different format. It probably can't be done. */ 4846 if (! dynamic 4847 && is_elf_hash_table (htab) 4848 && bed->check_relocs != NULL 4849 && elf_object_id (abfd) == elf_hash_table_id (htab) 4850 && (*bed->relocs_compatible) (abfd->xvec, info->output_bfd->xvec)) 4851 { 4852 asection *o; 4853 4854 for (o = abfd->sections; o != NULL; o = o->next) 4855 { 4856 Elf_Internal_Rela *internal_relocs; 4857 bfd_boolean ok; 4858 4859 if ((o->flags & SEC_RELOC) == 0 4860 || o->reloc_count == 0 4861 || ((info->strip == strip_all || info->strip == strip_debugger) 4862 && (o->flags & SEC_DEBUGGING) != 0) 4863 || bfd_is_abs_section (o->output_section)) 4864 continue; 4865 4866 internal_relocs = _bfd_elf_link_read_relocs (abfd, o, NULL, NULL, 4867 info->keep_memory); 4868 if (internal_relocs == NULL) 4869 goto error_return; 4870 4871 ok = (*bed->check_relocs) (abfd, info, o, internal_relocs); 4872 4873 if (elf_section_data (o)->relocs != internal_relocs) 4874 free (internal_relocs); 4875 4876 if (! ok) 4877 goto error_return; 4878 } 4879 } 4880 4881 /* If this is a non-traditional link, try to optimize the handling 4882 of the .stab/.stabstr sections. */ 4883 if (! dynamic 4884 && ! info->traditional_format 4885 && is_elf_hash_table (htab) 4886 && (info->strip != strip_all && info->strip != strip_debugger)) 4887 { 4888 asection *stabstr; 4889 4890 stabstr = bfd_get_section_by_name (abfd, ".stabstr"); 4891 if (stabstr != NULL) 4892 { 4893 bfd_size_type string_offset = 0; 4894 asection *stab; 4895 4896 for (stab = abfd->sections; stab; stab = stab->next) 4897 if (CONST_STRNEQ (stab->name, ".stab") 4898 && (!stab->name[5] || 4899 (stab->name[5] == '.' && ISDIGIT (stab->name[6]))) 4900 && (stab->flags & SEC_MERGE) == 0 4901 && !bfd_is_abs_section (stab->output_section)) 4902 { 4903 struct bfd_elf_section_data *secdata; 4904 4905 secdata = elf_section_data (stab); 4906 if (! _bfd_link_section_stabs (abfd, &htab->stab_info, stab, 4907 stabstr, &secdata->sec_info, 4908 &string_offset)) 4909 goto error_return; 4910 if (secdata->sec_info) 4911 stab->sec_info_type = SEC_INFO_TYPE_STABS; 4912 } 4913 } 4914 } 4915 4916 if (is_elf_hash_table (htab) && add_needed) 4917 { 4918 /* Add this bfd to the loaded list. */ 4919 struct elf_link_loaded_list *n; 4920 4921 n = (struct elf_link_loaded_list *) 4922 bfd_alloc (abfd, sizeof (struct elf_link_loaded_list)); 4923 if (n == NULL) 4924 goto error_return; 4925 n->abfd = abfd; 4926 n->next = htab->loaded; 4927 htab->loaded = n; 4928 } 4929 4930 return TRUE; 4931 4932 error_free_vers: 4933 if (old_tab != NULL) 4934 free (old_tab); 4935 if (nondeflt_vers != NULL) 4936 free (nondeflt_vers); 4937 if (extversym != NULL) 4938 free (extversym); 4939 error_free_sym: 4940 if (isymbuf != NULL) 4941 free (isymbuf); 4942 error_return: 4943 return FALSE; 4944 } 4945 4946 /* Return the linker hash table entry of a symbol that might be 4947 satisfied by an archive symbol. Return -1 on error. */ 4948 4949 struct elf_link_hash_entry * 4950 _bfd_elf_archive_symbol_lookup (bfd *abfd, 4951 struct bfd_link_info *info, 4952 const char *name) 4953 { 4954 struct elf_link_hash_entry *h; 4955 char *p, *copy; 4956 size_t len, first; 4957 4958 h = elf_link_hash_lookup (elf_hash_table (info), name, FALSE, FALSE, TRUE); 4959 if (h != NULL) 4960 return h; 4961 4962 /* If this is a default version (the name contains @@), look up the 4963 symbol again with only one `@' as well as without the version. 4964 The effect is that references to the symbol with and without the 4965 version will be matched by the default symbol in the archive. */ 4966 4967 p = strchr (name, ELF_VER_CHR); 4968 if (p == NULL || p[1] != ELF_VER_CHR) 4969 return h; 4970 4971 /* First check with only one `@'. */ 4972 len = strlen (name); 4973 copy = (char *) bfd_alloc (abfd, len); 4974 if (copy == NULL) 4975 return (struct elf_link_hash_entry *) 0 - 1; 4976 4977 first = p - name + 1; 4978 memcpy (copy, name, first); 4979 memcpy (copy + first, name + first + 1, len - first); 4980 4981 h = elf_link_hash_lookup (elf_hash_table (info), copy, FALSE, FALSE, TRUE); 4982 if (h == NULL) 4983 { 4984 /* We also need to check references to the symbol without the 4985 version. */ 4986 copy[first - 1] = '\0'; 4987 h = elf_link_hash_lookup (elf_hash_table (info), copy, 4988 FALSE, FALSE, TRUE); 4989 } 4990 4991 bfd_release (abfd, copy); 4992 return h; 4993 } 4994 4995 /* Add symbols from an ELF archive file to the linker hash table. We 4996 don't use _bfd_generic_link_add_archive_symbols because of a 4997 problem which arises on UnixWare. The UnixWare libc.so is an 4998 archive which includes an entry libc.so.1 which defines a bunch of 4999 symbols. The libc.so archive also includes a number of other 5000 object files, which also define symbols, some of which are the same 5001 as those defined in libc.so.1. Correct linking requires that we 5002 consider each object file in turn, and include it if it defines any 5003 symbols we need. _bfd_generic_link_add_archive_symbols does not do 5004 this; it looks through the list of undefined symbols, and includes 5005 any object file which defines them. When this algorithm is used on 5006 UnixWare, it winds up pulling in libc.so.1 early and defining a 5007 bunch of symbols. This means that some of the other objects in the 5008 archive are not included in the link, which is incorrect since they 5009 precede libc.so.1 in the archive. 5010 5011 Fortunately, ELF archive handling is simpler than that done by 5012 _bfd_generic_link_add_archive_symbols, which has to allow for a.out 5013 oddities. In ELF, if we find a symbol in the archive map, and the 5014 symbol is currently undefined, we know that we must pull in that 5015 object file. 5016 5017 Unfortunately, we do have to make multiple passes over the symbol 5018 table until nothing further is resolved. */ 5019 5020 static bfd_boolean 5021 elf_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info) 5022 { 5023 symindex c; 5024 bfd_boolean *defined = NULL; 5025 bfd_boolean *included = NULL; 5026 carsym *symdefs; 5027 bfd_boolean loop; 5028 bfd_size_type amt; 5029 const struct elf_backend_data *bed; 5030 struct elf_link_hash_entry * (*archive_symbol_lookup) 5031 (bfd *, struct bfd_link_info *, const char *); 5032 5033 if (! bfd_has_map (abfd)) 5034 { 5035 /* An empty archive is a special case. */ 5036 if (bfd_openr_next_archived_file (abfd, NULL) == NULL) 5037 return TRUE; 5038 bfd_set_error (bfd_error_no_armap); 5039 return FALSE; 5040 } 5041 5042 /* Keep track of all symbols we know to be already defined, and all 5043 files we know to be already included. This is to speed up the 5044 second and subsequent passes. */ 5045 c = bfd_ardata (abfd)->symdef_count; 5046 if (c == 0) 5047 return TRUE; 5048 amt = c; 5049 amt *= sizeof (bfd_boolean); 5050 defined = (bfd_boolean *) bfd_zmalloc (amt); 5051 included = (bfd_boolean *) bfd_zmalloc (amt); 5052 if (defined == NULL || included == NULL) 5053 goto error_return; 5054 5055 symdefs = bfd_ardata (abfd)->symdefs; 5056 bed = get_elf_backend_data (abfd); 5057 archive_symbol_lookup = bed->elf_backend_archive_symbol_lookup; 5058 5059 do 5060 { 5061 file_ptr last; 5062 symindex i; 5063 carsym *symdef; 5064 carsym *symdefend; 5065 5066 loop = FALSE; 5067 last = -1; 5068 5069 symdef = symdefs; 5070 symdefend = symdef + c; 5071 for (i = 0; symdef < symdefend; symdef++, i++) 5072 { 5073 struct elf_link_hash_entry *h; 5074 bfd *element; 5075 struct bfd_link_hash_entry *undefs_tail; 5076 symindex mark; 5077 5078 if (defined[i] || included[i]) 5079 continue; 5080 if (symdef->file_offset == last) 5081 { 5082 included[i] = TRUE; 5083 continue; 5084 } 5085 5086 h = archive_symbol_lookup (abfd, info, symdef->name); 5087 if (h == (struct elf_link_hash_entry *) 0 - 1) 5088 goto error_return; 5089 5090 if (h == NULL) 5091 continue; 5092 5093 if (h->root.type == bfd_link_hash_common) 5094 { 5095 /* We currently have a common symbol. The archive map contains 5096 a reference to this symbol, so we may want to include it. We 5097 only want to include it however, if this archive element 5098 contains a definition of the symbol, not just another common 5099 declaration of it. 5100 5101 Unfortunately some archivers (including GNU ar) will put 5102 declarations of common symbols into their archive maps, as 5103 well as real definitions, so we cannot just go by the archive 5104 map alone. Instead we must read in the element's symbol 5105 table and check that to see what kind of symbol definition 5106 this is. */ 5107 if (! elf_link_is_defined_archive_symbol (abfd, symdef)) 5108 continue; 5109 } 5110 else if (h->root.type != bfd_link_hash_undefined) 5111 { 5112 if (h->root.type != bfd_link_hash_undefweak) 5113 defined[i] = TRUE; 5114 continue; 5115 } 5116 5117 /* We need to include this archive member. */ 5118 element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset); 5119 if (element == NULL) 5120 goto error_return; 5121 5122 if (! bfd_check_format (element, bfd_object)) 5123 goto error_return; 5124 5125 /* Doublecheck that we have not included this object 5126 already--it should be impossible, but there may be 5127 something wrong with the archive. */ 5128 if (element->archive_pass != 0) 5129 { 5130 bfd_set_error (bfd_error_bad_value); 5131 goto error_return; 5132 } 5133 element->archive_pass = 1; 5134 5135 undefs_tail = info->hash->undefs_tail; 5136 5137 if (!(*info->callbacks 5138 ->add_archive_element) (info, element, symdef->name, &element)) 5139 goto error_return; 5140 if (!bfd_link_add_symbols (element, info)) 5141 goto error_return; 5142 5143 /* If there are any new undefined symbols, we need to make 5144 another pass through the archive in order to see whether 5145 they can be defined. FIXME: This isn't perfect, because 5146 common symbols wind up on undefs_tail and because an 5147 undefined symbol which is defined later on in this pass 5148 does not require another pass. This isn't a bug, but it 5149 does make the code less efficient than it could be. */ 5150 if (undefs_tail != info->hash->undefs_tail) 5151 loop = TRUE; 5152 5153 /* Look backward to mark all symbols from this object file 5154 which we have already seen in this pass. */ 5155 mark = i; 5156 do 5157 { 5158 included[mark] = TRUE; 5159 if (mark == 0) 5160 break; 5161 --mark; 5162 } 5163 while (symdefs[mark].file_offset == symdef->file_offset); 5164 5165 /* We mark subsequent symbols from this object file as we go 5166 on through the loop. */ 5167 last = symdef->file_offset; 5168 } 5169 } 5170 while (loop); 5171 5172 free (defined); 5173 free (included); 5174 5175 return TRUE; 5176 5177 error_return: 5178 if (defined != NULL) 5179 free (defined); 5180 if (included != NULL) 5181 free (included); 5182 return FALSE; 5183 } 5184 5185 /* Given an ELF BFD, add symbols to the global hash table as 5186 appropriate. */ 5187 5188 bfd_boolean 5189 bfd_elf_link_add_symbols (bfd *abfd, struct bfd_link_info *info) 5190 { 5191 switch (bfd_get_format (abfd)) 5192 { 5193 case bfd_object: 5194 return elf_link_add_object_symbols (abfd, info); 5195 case bfd_archive: 5196 return elf_link_add_archive_symbols (abfd, info); 5197 default: 5198 bfd_set_error (bfd_error_wrong_format); 5199 return FALSE; 5200 } 5201 } 5202 5203 struct hash_codes_info 5204 { 5205 unsigned long *hashcodes; 5206 bfd_boolean error; 5207 }; 5208 5209 /* This function will be called though elf_link_hash_traverse to store 5210 all hash value of the exported symbols in an array. */ 5211 5212 static bfd_boolean 5213 elf_collect_hash_codes (struct elf_link_hash_entry *h, void *data) 5214 { 5215 struct hash_codes_info *inf = (struct hash_codes_info *) data; 5216 const char *name; 5217 char *p; 5218 unsigned long ha; 5219 char *alc = NULL; 5220 5221 /* Ignore indirect symbols. These are added by the versioning code. */ 5222 if (h->dynindx == -1) 5223 return TRUE; 5224 5225 name = h->root.root.string; 5226 p = strchr (name, ELF_VER_CHR); 5227 if (p != NULL) 5228 { 5229 alc = (char *) bfd_malloc (p - name + 1); 5230 if (alc == NULL) 5231 { 5232 inf->error = TRUE; 5233 return FALSE; 5234 } 5235 memcpy (alc, name, p - name); 5236 alc[p - name] = '\0'; 5237 name = alc; 5238 } 5239 5240 /* Compute the hash value. */ 5241 ha = bfd_elf_hash (name); 5242 5243 /* Store the found hash value in the array given as the argument. */ 5244 *(inf->hashcodes)++ = ha; 5245 5246 /* And store it in the struct so that we can put it in the hash table 5247 later. */ 5248 h->u.elf_hash_value = ha; 5249 5250 if (alc != NULL) 5251 free (alc); 5252 5253 return TRUE; 5254 } 5255 5256 struct collect_gnu_hash_codes 5257 { 5258 bfd *output_bfd; 5259 const struct elf_backend_data *bed; 5260 unsigned long int nsyms; 5261 unsigned long int maskbits; 5262 unsigned long int *hashcodes; 5263 unsigned long int *hashval; 5264 unsigned long int *indx; 5265 unsigned long int *counts; 5266 bfd_vma *bitmask; 5267 bfd_byte *contents; 5268 long int min_dynindx; 5269 unsigned long int bucketcount; 5270 unsigned long int symindx; 5271 long int local_indx; 5272 long int shift1, shift2; 5273 unsigned long int mask; 5274 bfd_boolean error; 5275 }; 5276 5277 /* This function will be called though elf_link_hash_traverse to store 5278 all hash value of the exported symbols in an array. */ 5279 5280 static bfd_boolean 5281 elf_collect_gnu_hash_codes (struct elf_link_hash_entry *h, void *data) 5282 { 5283 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5284 const char *name; 5285 char *p; 5286 unsigned long ha; 5287 char *alc = NULL; 5288 5289 /* Ignore indirect symbols. These are added by the versioning code. */ 5290 if (h->dynindx == -1) 5291 return TRUE; 5292 5293 /* Ignore also local symbols and undefined symbols. */ 5294 if (! (*s->bed->elf_hash_symbol) (h)) 5295 return TRUE; 5296 5297 name = h->root.root.string; 5298 p = strchr (name, ELF_VER_CHR); 5299 if (p != NULL) 5300 { 5301 alc = (char *) bfd_malloc (p - name + 1); 5302 if (alc == NULL) 5303 { 5304 s->error = TRUE; 5305 return FALSE; 5306 } 5307 memcpy (alc, name, p - name); 5308 alc[p - name] = '\0'; 5309 name = alc; 5310 } 5311 5312 /* Compute the hash value. */ 5313 ha = bfd_elf_gnu_hash (name); 5314 5315 /* Store the found hash value in the array for compute_bucket_count, 5316 and also for .dynsym reordering purposes. */ 5317 s->hashcodes[s->nsyms] = ha; 5318 s->hashval[h->dynindx] = ha; 5319 ++s->nsyms; 5320 if (s->min_dynindx < 0 || s->min_dynindx > h->dynindx) 5321 s->min_dynindx = h->dynindx; 5322 5323 if (alc != NULL) 5324 free (alc); 5325 5326 return TRUE; 5327 } 5328 5329 /* This function will be called though elf_link_hash_traverse to do 5330 final dynaminc symbol renumbering. */ 5331 5332 static bfd_boolean 5333 elf_renumber_gnu_hash_syms (struct elf_link_hash_entry *h, void *data) 5334 { 5335 struct collect_gnu_hash_codes *s = (struct collect_gnu_hash_codes *) data; 5336 unsigned long int bucket; 5337 unsigned long int val; 5338 5339 /* Ignore indirect symbols. */ 5340 if (h->dynindx == -1) 5341 return TRUE; 5342 5343 /* Ignore also local symbols and undefined symbols. */ 5344 if (! (*s->bed->elf_hash_symbol) (h)) 5345 { 5346 if (h->dynindx >= s->min_dynindx) 5347 h->dynindx = s->local_indx++; 5348 return TRUE; 5349 } 5350 5351 bucket = s->hashval[h->dynindx] % s->bucketcount; 5352 val = (s->hashval[h->dynindx] >> s->shift1) 5353 & ((s->maskbits >> s->shift1) - 1); 5354 s->bitmask[val] |= ((bfd_vma) 1) << (s->hashval[h->dynindx] & s->mask); 5355 s->bitmask[val] 5356 |= ((bfd_vma) 1) << ((s->hashval[h->dynindx] >> s->shift2) & s->mask); 5357 val = s->hashval[h->dynindx] & ~(unsigned long int) 1; 5358 if (s->counts[bucket] == 1) 5359 /* Last element terminates the chain. */ 5360 val |= 1; 5361 bfd_put_32 (s->output_bfd, val, 5362 s->contents + (s->indx[bucket] - s->symindx) * 4); 5363 --s->counts[bucket]; 5364 h->dynindx = s->indx[bucket]++; 5365 return TRUE; 5366 } 5367 5368 /* Return TRUE if symbol should be hashed in the `.gnu.hash' section. */ 5369 5370 bfd_boolean 5371 _bfd_elf_hash_symbol (struct elf_link_hash_entry *h) 5372 { 5373 return !(h->forced_local 5374 || h->root.type == bfd_link_hash_undefined 5375 || h->root.type == bfd_link_hash_undefweak 5376 || ((h->root.type == bfd_link_hash_defined 5377 || h->root.type == bfd_link_hash_defweak) 5378 && h->root.u.def.section->output_section == NULL)); 5379 } 5380 5381 /* Array used to determine the number of hash table buckets to use 5382 based on the number of symbols there are. If there are fewer than 5383 3 symbols we use 1 bucket, fewer than 17 symbols we use 3 buckets, 5384 fewer than 37 we use 17 buckets, and so forth. We never use more 5385 than 32771 buckets. */ 5386 5387 static const size_t elf_buckets[] = 5388 { 5389 1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209, 5390 16411, 32771, 0 5391 }; 5392 5393 /* Compute bucket count for hashing table. We do not use a static set 5394 of possible tables sizes anymore. Instead we determine for all 5395 possible reasonable sizes of the table the outcome (i.e., the 5396 number of collisions etc) and choose the best solution. The 5397 weighting functions are not too simple to allow the table to grow 5398 without bounds. Instead one of the weighting factors is the size. 5399 Therefore the result is always a good payoff between few collisions 5400 (= short chain lengths) and table size. */ 5401 static size_t 5402 compute_bucket_count (struct bfd_link_info *info ATTRIBUTE_UNUSED, 5403 unsigned long int *hashcodes ATTRIBUTE_UNUSED, 5404 unsigned long int nsyms, 5405 int gnu_hash) 5406 { 5407 size_t best_size = 0; 5408 unsigned long int i; 5409 5410 /* We have a problem here. The following code to optimize the table 5411 size requires an integer type with more the 32 bits. If 5412 BFD_HOST_U_64_BIT is set we know about such a type. */ 5413 #ifdef BFD_HOST_U_64_BIT 5414 if (info->optimize) 5415 { 5416 size_t minsize; 5417 size_t maxsize; 5418 BFD_HOST_U_64_BIT best_chlen = ~((BFD_HOST_U_64_BIT) 0); 5419 bfd *dynobj = elf_hash_table (info)->dynobj; 5420 size_t dynsymcount = elf_hash_table (info)->dynsymcount; 5421 const struct elf_backend_data *bed = get_elf_backend_data (dynobj); 5422 unsigned long int *counts; 5423 bfd_size_type amt; 5424 unsigned int no_improvement_count = 0; 5425 5426 /* Possible optimization parameters: if we have NSYMS symbols we say 5427 that the hashing table must at least have NSYMS/4 and at most 5428 2*NSYMS buckets. */ 5429 minsize = nsyms / 4; 5430 if (minsize == 0) 5431 minsize = 1; 5432 best_size = maxsize = nsyms * 2; 5433 if (gnu_hash) 5434 { 5435 if (minsize < 2) 5436 minsize = 2; 5437 if ((best_size & 31) == 0) 5438 ++best_size; 5439 } 5440 5441 /* Create array where we count the collisions in. We must use bfd_malloc 5442 since the size could be large. */ 5443 amt = maxsize; 5444 amt *= sizeof (unsigned long int); 5445 counts = (unsigned long int *) bfd_malloc (amt); 5446 if (counts == NULL) 5447 return 0; 5448 5449 /* Compute the "optimal" size for the hash table. The criteria is a 5450 minimal chain length. The minor criteria is (of course) the size 5451 of the table. */ 5452 for (i = minsize; i < maxsize; ++i) 5453 { 5454 /* Walk through the array of hashcodes and count the collisions. */ 5455 BFD_HOST_U_64_BIT max; 5456 unsigned long int j; 5457 unsigned long int fact; 5458 5459 if (gnu_hash && (i & 31) == 0) 5460 continue; 5461 5462 memset (counts, '\0', i * sizeof (unsigned long int)); 5463 5464 /* Determine how often each hash bucket is used. */ 5465 for (j = 0; j < nsyms; ++j) 5466 ++counts[hashcodes[j] % i]; 5467 5468 /* For the weight function we need some information about the 5469 pagesize on the target. This is information need not be 100% 5470 accurate. Since this information is not available (so far) we 5471 define it here to a reasonable default value. If it is crucial 5472 to have a better value some day simply define this value. */ 5473 # ifndef BFD_TARGET_PAGESIZE 5474 # define BFD_TARGET_PAGESIZE (4096) 5475 # endif 5476 5477 /* We in any case need 2 + DYNSYMCOUNT entries for the size values 5478 and the chains. */ 5479 max = (2 + dynsymcount) * bed->s->sizeof_hash_entry; 5480 5481 # if 1 5482 /* Variant 1: optimize for short chains. We add the squares 5483 of all the chain lengths (which favors many small chain 5484 over a few long chains). */ 5485 for (j = 0; j < i; ++j) 5486 max += counts[j] * counts[j]; 5487 5488 /* This adds penalties for the overall size of the table. */ 5489 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 5490 max *= fact * fact; 5491 # else 5492 /* Variant 2: Optimize a lot more for small table. Here we 5493 also add squares of the size but we also add penalties for 5494 empty slots (the +1 term). */ 5495 for (j = 0; j < i; ++j) 5496 max += (1 + counts[j]) * (1 + counts[j]); 5497 5498 /* The overall size of the table is considered, but not as 5499 strong as in variant 1, where it is squared. */ 5500 fact = i / (BFD_TARGET_PAGESIZE / bed->s->sizeof_hash_entry) + 1; 5501 max *= fact; 5502 # endif 5503 5504 /* Compare with current best results. */ 5505 if (max < best_chlen) 5506 { 5507 best_chlen = max; 5508 best_size = i; 5509 no_improvement_count = 0; 5510 } 5511 /* PR 11843: Avoid futile long searches for the best bucket size 5512 when there are a large number of symbols. */ 5513 else if (++no_improvement_count == 100) 5514 break; 5515 } 5516 5517 free (counts); 5518 } 5519 else 5520 #endif /* defined (BFD_HOST_U_64_BIT) */ 5521 { 5522 /* This is the fallback solution if no 64bit type is available or if we 5523 are not supposed to spend much time on optimizations. We select the 5524 bucket count using a fixed set of numbers. */ 5525 for (i = 0; elf_buckets[i] != 0; i++) 5526 { 5527 best_size = elf_buckets[i]; 5528 if (nsyms < elf_buckets[i + 1]) 5529 break; 5530 } 5531 if (gnu_hash && best_size < 2) 5532 best_size = 2; 5533 } 5534 5535 return best_size; 5536 } 5537 5538 /* Size any SHT_GROUP section for ld -r. */ 5539 5540 bfd_boolean 5541 _bfd_elf_size_group_sections (struct bfd_link_info *info) 5542 { 5543 bfd *ibfd; 5544 5545 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 5546 if (bfd_get_flavour (ibfd) == bfd_target_elf_flavour 5547 && !_bfd_elf_fixup_group_sections (ibfd, bfd_abs_section_ptr)) 5548 return FALSE; 5549 return TRUE; 5550 } 5551 5552 /* Set up the sizes and contents of the ELF dynamic sections. This is 5553 called by the ELF linker emulation before_allocation routine. We 5554 must set the sizes of the sections before the linker sets the 5555 addresses of the various sections. */ 5556 5557 bfd_boolean 5558 bfd_elf_size_dynamic_sections (bfd *output_bfd, 5559 const char *soname, 5560 const char *rpath, 5561 const char *filter_shlib, 5562 const char *audit, 5563 const char *depaudit, 5564 const char * const *auxiliary_filters, 5565 struct bfd_link_info *info, 5566 asection **sinterpptr) 5567 { 5568 bfd_size_type soname_indx; 5569 bfd *dynobj; 5570 const struct elf_backend_data *bed; 5571 struct elf_info_failed asvinfo; 5572 5573 *sinterpptr = NULL; 5574 5575 soname_indx = (bfd_size_type) -1; 5576 5577 if (!is_elf_hash_table (info->hash)) 5578 return TRUE; 5579 5580 bed = get_elf_backend_data (output_bfd); 5581 if (info->execstack) 5582 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | PF_X; 5583 else if (info->noexecstack) 5584 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W; 5585 else 5586 { 5587 bfd *inputobj; 5588 asection *notesec = NULL; 5589 int exec = 0; 5590 5591 for (inputobj = info->input_bfds; 5592 inputobj; 5593 inputobj = inputobj->link_next) 5594 { 5595 asection *s; 5596 5597 if (inputobj->flags 5598 & (DYNAMIC | EXEC_P | BFD_PLUGIN | BFD_LINKER_CREATED)) 5599 continue; 5600 s = bfd_get_section_by_name (inputobj, ".note.GNU-stack"); 5601 if (s) 5602 { 5603 if (s->flags & SEC_CODE) 5604 exec = PF_X; 5605 notesec = s; 5606 } 5607 else if (bed->default_execstack) 5608 exec = PF_X; 5609 } 5610 if (notesec) 5611 { 5612 elf_tdata (output_bfd)->stack_flags = PF_R | PF_W | exec; 5613 if (exec && info->relocatable 5614 && notesec->output_section != bfd_abs_section_ptr) 5615 notesec->output_section->flags |= SEC_CODE; 5616 } 5617 } 5618 5619 /* Any syms created from now on start with -1 in 5620 got.refcount/offset and plt.refcount/offset. */ 5621 elf_hash_table (info)->init_got_refcount 5622 = elf_hash_table (info)->init_got_offset; 5623 elf_hash_table (info)->init_plt_refcount 5624 = elf_hash_table (info)->init_plt_offset; 5625 5626 if (info->relocatable 5627 && !_bfd_elf_size_group_sections (info)) 5628 return FALSE; 5629 5630 /* The backend may have to create some sections regardless of whether 5631 we're dynamic or not. */ 5632 if (bed->elf_backend_always_size_sections 5633 && ! (*bed->elf_backend_always_size_sections) (output_bfd, info)) 5634 return FALSE; 5635 5636 dynobj = elf_hash_table (info)->dynobj; 5637 5638 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 5639 { 5640 struct elf_info_failed eif; 5641 struct elf_link_hash_entry *h; 5642 asection *dynstr; 5643 struct bfd_elf_version_tree *t; 5644 struct bfd_elf_version_expr *d; 5645 asection *s; 5646 bfd_boolean all_defined; 5647 5648 *sinterpptr = bfd_get_linker_section (dynobj, ".interp"); 5649 BFD_ASSERT (*sinterpptr != NULL || !info->executable); 5650 5651 if (soname != NULL) 5652 { 5653 soname_indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5654 soname, TRUE); 5655 if (soname_indx == (bfd_size_type) -1 5656 || !_bfd_elf_add_dynamic_entry (info, DT_SONAME, soname_indx)) 5657 return FALSE; 5658 } 5659 5660 if (info->symbolic) 5661 { 5662 if (!_bfd_elf_add_dynamic_entry (info, DT_SYMBOLIC, 0)) 5663 return FALSE; 5664 info->flags |= DF_SYMBOLIC; 5665 } 5666 5667 if (rpath != NULL) 5668 { 5669 bfd_size_type indx; 5670 5671 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, rpath, 5672 TRUE); 5673 if (indx == (bfd_size_type) -1 5674 || !_bfd_elf_add_dynamic_entry (info, DT_RPATH, indx)) 5675 return FALSE; 5676 5677 if (info->new_dtags) 5678 { 5679 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, indx); 5680 if (!_bfd_elf_add_dynamic_entry (info, DT_RUNPATH, indx)) 5681 return FALSE; 5682 } 5683 } 5684 5685 if (filter_shlib != NULL) 5686 { 5687 bfd_size_type indx; 5688 5689 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5690 filter_shlib, TRUE); 5691 if (indx == (bfd_size_type) -1 5692 || !_bfd_elf_add_dynamic_entry (info, DT_FILTER, indx)) 5693 return FALSE; 5694 } 5695 5696 if (auxiliary_filters != NULL) 5697 { 5698 const char * const *p; 5699 5700 for (p = auxiliary_filters; *p != NULL; p++) 5701 { 5702 bfd_size_type indx; 5703 5704 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 5705 *p, TRUE); 5706 if (indx == (bfd_size_type) -1 5707 || !_bfd_elf_add_dynamic_entry (info, DT_AUXILIARY, indx)) 5708 return FALSE; 5709 } 5710 } 5711 5712 if (audit != NULL) 5713 { 5714 bfd_size_type indx; 5715 5716 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, audit, 5717 TRUE); 5718 if (indx == (bfd_size_type) -1 5719 || !_bfd_elf_add_dynamic_entry (info, DT_AUDIT, indx)) 5720 return FALSE; 5721 } 5722 5723 if (depaudit != NULL) 5724 { 5725 bfd_size_type indx; 5726 5727 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, depaudit, 5728 TRUE); 5729 if (indx == (bfd_size_type) -1 5730 || !_bfd_elf_add_dynamic_entry (info, DT_DEPAUDIT, indx)) 5731 return FALSE; 5732 } 5733 5734 eif.info = info; 5735 eif.failed = FALSE; 5736 5737 /* If we are supposed to export all symbols into the dynamic symbol 5738 table (this is not the normal case), then do so. */ 5739 if (info->export_dynamic 5740 || (info->executable && info->dynamic)) 5741 { 5742 elf_link_hash_traverse (elf_hash_table (info), 5743 _bfd_elf_export_symbol, 5744 &eif); 5745 if (eif.failed) 5746 return FALSE; 5747 } 5748 5749 /* Make all global versions with definition. */ 5750 for (t = info->version_info; t != NULL; t = t->next) 5751 for (d = t->globals.list; d != NULL; d = d->next) 5752 if (!d->symver && d->literal) 5753 { 5754 const char *verstr, *name; 5755 size_t namelen, verlen, newlen; 5756 char *newname, *p, leading_char; 5757 struct elf_link_hash_entry *newh; 5758 5759 leading_char = bfd_get_symbol_leading_char (output_bfd); 5760 name = d->pattern; 5761 namelen = strlen (name) + (leading_char != '\0'); 5762 verstr = t->name; 5763 verlen = strlen (verstr); 5764 newlen = namelen + verlen + 3; 5765 5766 newname = (char *) bfd_malloc (newlen); 5767 if (newname == NULL) 5768 return FALSE; 5769 newname[0] = leading_char; 5770 memcpy (newname + (leading_char != '\0'), name, namelen); 5771 5772 /* Check the hidden versioned definition. */ 5773 p = newname + namelen; 5774 *p++ = ELF_VER_CHR; 5775 memcpy (p, verstr, verlen + 1); 5776 newh = elf_link_hash_lookup (elf_hash_table (info), 5777 newname, FALSE, FALSE, 5778 FALSE); 5779 if (newh == NULL 5780 || (newh->root.type != bfd_link_hash_defined 5781 && newh->root.type != bfd_link_hash_defweak)) 5782 { 5783 /* Check the default versioned definition. */ 5784 *p++ = ELF_VER_CHR; 5785 memcpy (p, verstr, verlen + 1); 5786 newh = elf_link_hash_lookup (elf_hash_table (info), 5787 newname, FALSE, FALSE, 5788 FALSE); 5789 } 5790 free (newname); 5791 5792 /* Mark this version if there is a definition and it is 5793 not defined in a shared object. */ 5794 if (newh != NULL 5795 && !newh->def_dynamic 5796 && (newh->root.type == bfd_link_hash_defined 5797 || newh->root.type == bfd_link_hash_defweak)) 5798 d->symver = 1; 5799 } 5800 5801 /* Attach all the symbols to their version information. */ 5802 asvinfo.info = info; 5803 asvinfo.failed = FALSE; 5804 5805 elf_link_hash_traverse (elf_hash_table (info), 5806 _bfd_elf_link_assign_sym_version, 5807 &asvinfo); 5808 if (asvinfo.failed) 5809 return FALSE; 5810 5811 if (!info->allow_undefined_version) 5812 { 5813 /* Check if all global versions have a definition. */ 5814 all_defined = TRUE; 5815 for (t = info->version_info; t != NULL; t = t->next) 5816 for (d = t->globals.list; d != NULL; d = d->next) 5817 if (d->literal && !d->symver && !d->script) 5818 { 5819 (*_bfd_error_handler) 5820 (_("%s: undefined version: %s"), 5821 d->pattern, t->name); 5822 all_defined = FALSE; 5823 } 5824 5825 if (!all_defined) 5826 { 5827 bfd_set_error (bfd_error_bad_value); 5828 return FALSE; 5829 } 5830 } 5831 5832 /* Find all symbols which were defined in a dynamic object and make 5833 the backend pick a reasonable value for them. */ 5834 elf_link_hash_traverse (elf_hash_table (info), 5835 _bfd_elf_adjust_dynamic_symbol, 5836 &eif); 5837 if (eif.failed) 5838 return FALSE; 5839 5840 /* Add some entries to the .dynamic section. We fill in some of the 5841 values later, in bfd_elf_final_link, but we must add the entries 5842 now so that we know the final size of the .dynamic section. */ 5843 5844 /* If there are initialization and/or finalization functions to 5845 call then add the corresponding DT_INIT/DT_FINI entries. */ 5846 h = (info->init_function 5847 ? elf_link_hash_lookup (elf_hash_table (info), 5848 info->init_function, FALSE, 5849 FALSE, FALSE) 5850 : NULL); 5851 if (h != NULL 5852 && (h->ref_regular 5853 || h->def_regular)) 5854 { 5855 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT, 0)) 5856 return FALSE; 5857 } 5858 h = (info->fini_function 5859 ? elf_link_hash_lookup (elf_hash_table (info), 5860 info->fini_function, FALSE, 5861 FALSE, FALSE) 5862 : NULL); 5863 if (h != NULL 5864 && (h->ref_regular 5865 || h->def_regular)) 5866 { 5867 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI, 0)) 5868 return FALSE; 5869 } 5870 5871 s = bfd_get_section_by_name (output_bfd, ".preinit_array"); 5872 if (s != NULL && s->linker_has_input) 5873 { 5874 /* DT_PREINIT_ARRAY is not allowed in shared library. */ 5875 if (! info->executable) 5876 { 5877 bfd *sub; 5878 asection *o; 5879 5880 for (sub = info->input_bfds; sub != NULL; 5881 sub = sub->link_next) 5882 if (bfd_get_flavour (sub) == bfd_target_elf_flavour) 5883 for (o = sub->sections; o != NULL; o = o->next) 5884 if (elf_section_data (o)->this_hdr.sh_type 5885 == SHT_PREINIT_ARRAY) 5886 { 5887 (*_bfd_error_handler) 5888 (_("%B: .preinit_array section is not allowed in DSO"), 5889 sub); 5890 break; 5891 } 5892 5893 bfd_set_error (bfd_error_nonrepresentable_section); 5894 return FALSE; 5895 } 5896 5897 if (!_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAY, 0) 5898 || !_bfd_elf_add_dynamic_entry (info, DT_PREINIT_ARRAYSZ, 0)) 5899 return FALSE; 5900 } 5901 s = bfd_get_section_by_name (output_bfd, ".init_array"); 5902 if (s != NULL && s->linker_has_input) 5903 { 5904 if (!_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAY, 0) 5905 || !_bfd_elf_add_dynamic_entry (info, DT_INIT_ARRAYSZ, 0)) 5906 return FALSE; 5907 } 5908 s = bfd_get_section_by_name (output_bfd, ".fini_array"); 5909 if (s != NULL && s->linker_has_input) 5910 { 5911 if (!_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAY, 0) 5912 || !_bfd_elf_add_dynamic_entry (info, DT_FINI_ARRAYSZ, 0)) 5913 return FALSE; 5914 } 5915 5916 dynstr = bfd_get_linker_section (dynobj, ".dynstr"); 5917 /* If .dynstr is excluded from the link, we don't want any of 5918 these tags. Strictly, we should be checking each section 5919 individually; This quick check covers for the case where 5920 someone does a /DISCARD/ : { *(*) }. */ 5921 if (dynstr != NULL && dynstr->output_section != bfd_abs_section_ptr) 5922 { 5923 bfd_size_type strsize; 5924 5925 strsize = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 5926 if ((info->emit_hash 5927 && !_bfd_elf_add_dynamic_entry (info, DT_HASH, 0)) 5928 || (info->emit_gnu_hash 5929 && !_bfd_elf_add_dynamic_entry (info, DT_GNU_HASH, 0)) 5930 || !_bfd_elf_add_dynamic_entry (info, DT_STRTAB, 0) 5931 || !_bfd_elf_add_dynamic_entry (info, DT_SYMTAB, 0) 5932 || !_bfd_elf_add_dynamic_entry (info, DT_STRSZ, strsize) 5933 || !_bfd_elf_add_dynamic_entry (info, DT_SYMENT, 5934 bed->s->sizeof_sym)) 5935 return FALSE; 5936 } 5937 } 5938 5939 /* The backend must work out the sizes of all the other dynamic 5940 sections. */ 5941 if (dynobj != NULL 5942 && bed->elf_backend_size_dynamic_sections != NULL 5943 && ! (*bed->elf_backend_size_dynamic_sections) (output_bfd, info)) 5944 return FALSE; 5945 5946 if (! _bfd_elf_maybe_strip_eh_frame_hdr (info)) 5947 return FALSE; 5948 5949 if (dynobj != NULL && elf_hash_table (info)->dynamic_sections_created) 5950 { 5951 unsigned long section_sym_count; 5952 struct bfd_elf_version_tree *verdefs; 5953 asection *s; 5954 5955 /* Set up the version definition section. */ 5956 s = bfd_get_linker_section (dynobj, ".gnu.version_d"); 5957 BFD_ASSERT (s != NULL); 5958 5959 /* We may have created additional version definitions if we are 5960 just linking a regular application. */ 5961 verdefs = info->version_info; 5962 5963 /* Skip anonymous version tag. */ 5964 if (verdefs != NULL && verdefs->vernum == 0) 5965 verdefs = verdefs->next; 5966 5967 if (verdefs == NULL && !info->create_default_symver) 5968 s->flags |= SEC_EXCLUDE; 5969 else 5970 { 5971 unsigned int cdefs; 5972 bfd_size_type size; 5973 struct bfd_elf_version_tree *t; 5974 bfd_byte *p; 5975 Elf_Internal_Verdef def; 5976 Elf_Internal_Verdaux defaux; 5977 struct bfd_link_hash_entry *bh; 5978 struct elf_link_hash_entry *h; 5979 const char *name; 5980 5981 cdefs = 0; 5982 size = 0; 5983 5984 /* Make space for the base version. */ 5985 size += sizeof (Elf_External_Verdef); 5986 size += sizeof (Elf_External_Verdaux); 5987 ++cdefs; 5988 5989 /* Make space for the default version. */ 5990 if (info->create_default_symver) 5991 { 5992 size += sizeof (Elf_External_Verdef); 5993 ++cdefs; 5994 } 5995 5996 for (t = verdefs; t != NULL; t = t->next) 5997 { 5998 struct bfd_elf_version_deps *n; 5999 6000 /* Don't emit base version twice. */ 6001 if (t->vernum == 0) 6002 continue; 6003 6004 size += sizeof (Elf_External_Verdef); 6005 size += sizeof (Elf_External_Verdaux); 6006 ++cdefs; 6007 6008 for (n = t->deps; n != NULL; n = n->next) 6009 size += sizeof (Elf_External_Verdaux); 6010 } 6011 6012 s->size = size; 6013 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6014 if (s->contents == NULL && s->size != 0) 6015 return FALSE; 6016 6017 /* Fill in the version definition section. */ 6018 6019 p = s->contents; 6020 6021 def.vd_version = VER_DEF_CURRENT; 6022 def.vd_flags = VER_FLG_BASE; 6023 def.vd_ndx = 1; 6024 def.vd_cnt = 1; 6025 if (info->create_default_symver) 6026 { 6027 def.vd_aux = 2 * sizeof (Elf_External_Verdef); 6028 def.vd_next = sizeof (Elf_External_Verdef); 6029 } 6030 else 6031 { 6032 def.vd_aux = sizeof (Elf_External_Verdef); 6033 def.vd_next = (sizeof (Elf_External_Verdef) 6034 + sizeof (Elf_External_Verdaux)); 6035 } 6036 6037 if (soname_indx != (bfd_size_type) -1) 6038 { 6039 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6040 soname_indx); 6041 def.vd_hash = bfd_elf_hash (soname); 6042 defaux.vda_name = soname_indx; 6043 name = soname; 6044 } 6045 else 6046 { 6047 bfd_size_type indx; 6048 6049 name = lbasename (output_bfd->filename); 6050 def.vd_hash = bfd_elf_hash (name); 6051 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6052 name, FALSE); 6053 if (indx == (bfd_size_type) -1) 6054 return FALSE; 6055 defaux.vda_name = indx; 6056 } 6057 defaux.vda_next = 0; 6058 6059 _bfd_elf_swap_verdef_out (output_bfd, &def, 6060 (Elf_External_Verdef *) p); 6061 p += sizeof (Elf_External_Verdef); 6062 if (info->create_default_symver) 6063 { 6064 /* Add a symbol representing this version. */ 6065 bh = NULL; 6066 if (! (_bfd_generic_link_add_one_symbol 6067 (info, dynobj, name, BSF_GLOBAL, bfd_abs_section_ptr, 6068 0, NULL, FALSE, 6069 get_elf_backend_data (dynobj)->collect, &bh))) 6070 return FALSE; 6071 h = (struct elf_link_hash_entry *) bh; 6072 h->non_elf = 0; 6073 h->def_regular = 1; 6074 h->type = STT_OBJECT; 6075 h->verinfo.vertree = NULL; 6076 6077 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6078 return FALSE; 6079 6080 /* Create a duplicate of the base version with the same 6081 aux block, but different flags. */ 6082 def.vd_flags = 0; 6083 def.vd_ndx = 2; 6084 def.vd_aux = sizeof (Elf_External_Verdef); 6085 if (verdefs) 6086 def.vd_next = (sizeof (Elf_External_Verdef) 6087 + sizeof (Elf_External_Verdaux)); 6088 else 6089 def.vd_next = 0; 6090 _bfd_elf_swap_verdef_out (output_bfd, &def, 6091 (Elf_External_Verdef *) p); 6092 p += sizeof (Elf_External_Verdef); 6093 } 6094 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6095 (Elf_External_Verdaux *) p); 6096 p += sizeof (Elf_External_Verdaux); 6097 6098 for (t = verdefs; t != NULL; t = t->next) 6099 { 6100 unsigned int cdeps; 6101 struct bfd_elf_version_deps *n; 6102 6103 /* Don't emit the base version twice. */ 6104 if (t->vernum == 0) 6105 continue; 6106 6107 cdeps = 0; 6108 for (n = t->deps; n != NULL; n = n->next) 6109 ++cdeps; 6110 6111 /* Add a symbol representing this version. */ 6112 bh = NULL; 6113 if (! (_bfd_generic_link_add_one_symbol 6114 (info, dynobj, t->name, BSF_GLOBAL, bfd_abs_section_ptr, 6115 0, NULL, FALSE, 6116 get_elf_backend_data (dynobj)->collect, &bh))) 6117 return FALSE; 6118 h = (struct elf_link_hash_entry *) bh; 6119 h->non_elf = 0; 6120 h->def_regular = 1; 6121 h->type = STT_OBJECT; 6122 h->verinfo.vertree = t; 6123 6124 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 6125 return FALSE; 6126 6127 def.vd_version = VER_DEF_CURRENT; 6128 def.vd_flags = 0; 6129 if (t->globals.list == NULL 6130 && t->locals.list == NULL 6131 && ! t->used) 6132 def.vd_flags |= VER_FLG_WEAK; 6133 def.vd_ndx = t->vernum + (info->create_default_symver ? 2 : 1); 6134 def.vd_cnt = cdeps + 1; 6135 def.vd_hash = bfd_elf_hash (t->name); 6136 def.vd_aux = sizeof (Elf_External_Verdef); 6137 def.vd_next = 0; 6138 6139 /* If a basever node is next, it *must* be the last node in 6140 the chain, otherwise Verdef construction breaks. */ 6141 if (t->next != NULL && t->next->vernum == 0) 6142 BFD_ASSERT (t->next->next == NULL); 6143 6144 if (t->next != NULL && t->next->vernum != 0) 6145 def.vd_next = (sizeof (Elf_External_Verdef) 6146 + (cdeps + 1) * sizeof (Elf_External_Verdaux)); 6147 6148 _bfd_elf_swap_verdef_out (output_bfd, &def, 6149 (Elf_External_Verdef *) p); 6150 p += sizeof (Elf_External_Verdef); 6151 6152 defaux.vda_name = h->dynstr_index; 6153 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6154 h->dynstr_index); 6155 defaux.vda_next = 0; 6156 if (t->deps != NULL) 6157 defaux.vda_next = sizeof (Elf_External_Verdaux); 6158 t->name_indx = defaux.vda_name; 6159 6160 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6161 (Elf_External_Verdaux *) p); 6162 p += sizeof (Elf_External_Verdaux); 6163 6164 for (n = t->deps; n != NULL; n = n->next) 6165 { 6166 if (n->version_needed == NULL) 6167 { 6168 /* This can happen if there was an error in the 6169 version script. */ 6170 defaux.vda_name = 0; 6171 } 6172 else 6173 { 6174 defaux.vda_name = n->version_needed->name_indx; 6175 _bfd_elf_strtab_addref (elf_hash_table (info)->dynstr, 6176 defaux.vda_name); 6177 } 6178 if (n->next == NULL) 6179 defaux.vda_next = 0; 6180 else 6181 defaux.vda_next = sizeof (Elf_External_Verdaux); 6182 6183 _bfd_elf_swap_verdaux_out (output_bfd, &defaux, 6184 (Elf_External_Verdaux *) p); 6185 p += sizeof (Elf_External_Verdaux); 6186 } 6187 } 6188 6189 if (!_bfd_elf_add_dynamic_entry (info, DT_VERDEF, 0) 6190 || !_bfd_elf_add_dynamic_entry (info, DT_VERDEFNUM, cdefs)) 6191 return FALSE; 6192 6193 elf_tdata (output_bfd)->cverdefs = cdefs; 6194 } 6195 6196 if ((info->new_dtags && info->flags) || (info->flags & DF_STATIC_TLS)) 6197 { 6198 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS, info->flags)) 6199 return FALSE; 6200 } 6201 else if (info->flags & DF_BIND_NOW) 6202 { 6203 if (!_bfd_elf_add_dynamic_entry (info, DT_BIND_NOW, 0)) 6204 return FALSE; 6205 } 6206 6207 if (info->flags_1) 6208 { 6209 if (info->executable) 6210 info->flags_1 &= ~ (DF_1_INITFIRST 6211 | DF_1_NODELETE 6212 | DF_1_NOOPEN); 6213 if (!_bfd_elf_add_dynamic_entry (info, DT_FLAGS_1, info->flags_1)) 6214 return FALSE; 6215 } 6216 6217 /* Work out the size of the version reference section. */ 6218 6219 s = bfd_get_linker_section (dynobj, ".gnu.version_r"); 6220 BFD_ASSERT (s != NULL); 6221 { 6222 struct elf_find_verdep_info sinfo; 6223 6224 sinfo.info = info; 6225 sinfo.vers = elf_tdata (output_bfd)->cverdefs; 6226 if (sinfo.vers == 0) 6227 sinfo.vers = 1; 6228 sinfo.failed = FALSE; 6229 6230 elf_link_hash_traverse (elf_hash_table (info), 6231 _bfd_elf_link_find_version_dependencies, 6232 &sinfo); 6233 if (sinfo.failed) 6234 return FALSE; 6235 6236 if (elf_tdata (output_bfd)->verref == NULL) 6237 s->flags |= SEC_EXCLUDE; 6238 else 6239 { 6240 Elf_Internal_Verneed *t; 6241 unsigned int size; 6242 unsigned int crefs; 6243 bfd_byte *p; 6244 6245 /* Build the version dependency section. */ 6246 size = 0; 6247 crefs = 0; 6248 for (t = elf_tdata (output_bfd)->verref; 6249 t != NULL; 6250 t = t->vn_nextref) 6251 { 6252 Elf_Internal_Vernaux *a; 6253 6254 size += sizeof (Elf_External_Verneed); 6255 ++crefs; 6256 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 6257 size += sizeof (Elf_External_Vernaux); 6258 } 6259 6260 s->size = size; 6261 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6262 if (s->contents == NULL) 6263 return FALSE; 6264 6265 p = s->contents; 6266 for (t = elf_tdata (output_bfd)->verref; 6267 t != NULL; 6268 t = t->vn_nextref) 6269 { 6270 unsigned int caux; 6271 Elf_Internal_Vernaux *a; 6272 bfd_size_type indx; 6273 6274 caux = 0; 6275 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 6276 ++caux; 6277 6278 t->vn_version = VER_NEED_CURRENT; 6279 t->vn_cnt = caux; 6280 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6281 elf_dt_name (t->vn_bfd) != NULL 6282 ? elf_dt_name (t->vn_bfd) 6283 : lbasename (t->vn_bfd->filename), 6284 FALSE); 6285 if (indx == (bfd_size_type) -1) 6286 return FALSE; 6287 t->vn_file = indx; 6288 t->vn_aux = sizeof (Elf_External_Verneed); 6289 if (t->vn_nextref == NULL) 6290 t->vn_next = 0; 6291 else 6292 t->vn_next = (sizeof (Elf_External_Verneed) 6293 + caux * sizeof (Elf_External_Vernaux)); 6294 6295 _bfd_elf_swap_verneed_out (output_bfd, t, 6296 (Elf_External_Verneed *) p); 6297 p += sizeof (Elf_External_Verneed); 6298 6299 for (a = t->vn_auxptr; a != NULL; a = a->vna_nextptr) 6300 { 6301 a->vna_hash = bfd_elf_hash (a->vna_nodename); 6302 indx = _bfd_elf_strtab_add (elf_hash_table (info)->dynstr, 6303 a->vna_nodename, FALSE); 6304 if (indx == (bfd_size_type) -1) 6305 return FALSE; 6306 a->vna_name = indx; 6307 if (a->vna_nextptr == NULL) 6308 a->vna_next = 0; 6309 else 6310 a->vna_next = sizeof (Elf_External_Vernaux); 6311 6312 _bfd_elf_swap_vernaux_out (output_bfd, a, 6313 (Elf_External_Vernaux *) p); 6314 p += sizeof (Elf_External_Vernaux); 6315 } 6316 } 6317 6318 if (!_bfd_elf_add_dynamic_entry (info, DT_VERNEED, 0) 6319 || !_bfd_elf_add_dynamic_entry (info, DT_VERNEEDNUM, crefs)) 6320 return FALSE; 6321 6322 elf_tdata (output_bfd)->cverrefs = crefs; 6323 } 6324 } 6325 6326 if ((elf_tdata (output_bfd)->cverrefs == 0 6327 && elf_tdata (output_bfd)->cverdefs == 0) 6328 || _bfd_elf_link_renumber_dynsyms (output_bfd, info, 6329 §ion_sym_count) == 0) 6330 { 6331 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6332 s->flags |= SEC_EXCLUDE; 6333 } 6334 } 6335 return TRUE; 6336 } 6337 6338 /* Find the first non-excluded output section. We'll use its 6339 section symbol for some emitted relocs. */ 6340 void 6341 _bfd_elf_init_1_index_section (bfd *output_bfd, struct bfd_link_info *info) 6342 { 6343 asection *s; 6344 6345 for (s = output_bfd->sections; s != NULL; s = s->next) 6346 if ((s->flags & (SEC_EXCLUDE | SEC_ALLOC)) == SEC_ALLOC 6347 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6348 { 6349 elf_hash_table (info)->text_index_section = s; 6350 break; 6351 } 6352 } 6353 6354 /* Find two non-excluded output sections, one for code, one for data. 6355 We'll use their section symbols for some emitted relocs. */ 6356 void 6357 _bfd_elf_init_2_index_sections (bfd *output_bfd, struct bfd_link_info *info) 6358 { 6359 asection *s; 6360 6361 /* Data first, since setting text_index_section changes 6362 _bfd_elf_link_omit_section_dynsym. */ 6363 for (s = output_bfd->sections; s != NULL; s = s->next) 6364 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) == SEC_ALLOC) 6365 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6366 { 6367 elf_hash_table (info)->data_index_section = s; 6368 break; 6369 } 6370 6371 for (s = output_bfd->sections; s != NULL; s = s->next) 6372 if (((s->flags & (SEC_EXCLUDE | SEC_ALLOC | SEC_READONLY)) 6373 == (SEC_ALLOC | SEC_READONLY)) 6374 && !_bfd_elf_link_omit_section_dynsym (output_bfd, info, s)) 6375 { 6376 elf_hash_table (info)->text_index_section = s; 6377 break; 6378 } 6379 6380 if (elf_hash_table (info)->text_index_section == NULL) 6381 elf_hash_table (info)->text_index_section 6382 = elf_hash_table (info)->data_index_section; 6383 } 6384 6385 bfd_boolean 6386 bfd_elf_size_dynsym_hash_dynstr (bfd *output_bfd, struct bfd_link_info *info) 6387 { 6388 const struct elf_backend_data *bed; 6389 6390 if (!is_elf_hash_table (info->hash)) 6391 return TRUE; 6392 6393 bed = get_elf_backend_data (output_bfd); 6394 (*bed->elf_backend_init_index_section) (output_bfd, info); 6395 6396 if (elf_hash_table (info)->dynamic_sections_created) 6397 { 6398 bfd *dynobj; 6399 asection *s; 6400 bfd_size_type dynsymcount; 6401 unsigned long section_sym_count; 6402 unsigned int dtagcount; 6403 6404 dynobj = elf_hash_table (info)->dynobj; 6405 6406 /* Assign dynsym indicies. In a shared library we generate a 6407 section symbol for each output section, which come first. 6408 Next come all of the back-end allocated local dynamic syms, 6409 followed by the rest of the global symbols. */ 6410 6411 dynsymcount = _bfd_elf_link_renumber_dynsyms (output_bfd, info, 6412 §ion_sym_count); 6413 6414 /* Work out the size of the symbol version section. */ 6415 s = bfd_get_linker_section (dynobj, ".gnu.version"); 6416 BFD_ASSERT (s != NULL); 6417 if (dynsymcount != 0 6418 && (s->flags & SEC_EXCLUDE) == 0) 6419 { 6420 s->size = dynsymcount * sizeof (Elf_External_Versym); 6421 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6422 if (s->contents == NULL) 6423 return FALSE; 6424 6425 if (!_bfd_elf_add_dynamic_entry (info, DT_VERSYM, 0)) 6426 return FALSE; 6427 } 6428 6429 /* Set the size of the .dynsym and .hash sections. We counted 6430 the number of dynamic symbols in elf_link_add_object_symbols. 6431 We will build the contents of .dynsym and .hash when we build 6432 the final symbol table, because until then we do not know the 6433 correct value to give the symbols. We built the .dynstr 6434 section as we went along in elf_link_add_object_symbols. */ 6435 s = bfd_get_linker_section (dynobj, ".dynsym"); 6436 BFD_ASSERT (s != NULL); 6437 s->size = dynsymcount * bed->s->sizeof_sym; 6438 6439 if (dynsymcount != 0) 6440 { 6441 s->contents = (unsigned char *) bfd_alloc (output_bfd, s->size); 6442 if (s->contents == NULL) 6443 return FALSE; 6444 6445 /* The first entry in .dynsym is a dummy symbol. 6446 Clear all the section syms, in case we don't output them all. */ 6447 ++section_sym_count; 6448 memset (s->contents, 0, section_sym_count * bed->s->sizeof_sym); 6449 } 6450 6451 elf_hash_table (info)->bucketcount = 0; 6452 6453 /* Compute the size of the hashing table. As a side effect this 6454 computes the hash values for all the names we export. */ 6455 if (info->emit_hash) 6456 { 6457 unsigned long int *hashcodes; 6458 struct hash_codes_info hashinf; 6459 bfd_size_type amt; 6460 unsigned long int nsyms; 6461 size_t bucketcount; 6462 size_t hash_entry_size; 6463 6464 /* Compute the hash values for all exported symbols. At the same 6465 time store the values in an array so that we could use them for 6466 optimizations. */ 6467 amt = dynsymcount * sizeof (unsigned long int); 6468 hashcodes = (unsigned long int *) bfd_malloc (amt); 6469 if (hashcodes == NULL) 6470 return FALSE; 6471 hashinf.hashcodes = hashcodes; 6472 hashinf.error = FALSE; 6473 6474 /* Put all hash values in HASHCODES. */ 6475 elf_link_hash_traverse (elf_hash_table (info), 6476 elf_collect_hash_codes, &hashinf); 6477 if (hashinf.error) 6478 { 6479 free (hashcodes); 6480 return FALSE; 6481 } 6482 6483 nsyms = hashinf.hashcodes - hashcodes; 6484 bucketcount 6485 = compute_bucket_count (info, hashcodes, nsyms, 0); 6486 free (hashcodes); 6487 6488 if (bucketcount == 0) 6489 return FALSE; 6490 6491 elf_hash_table (info)->bucketcount = bucketcount; 6492 6493 s = bfd_get_linker_section (dynobj, ".hash"); 6494 BFD_ASSERT (s != NULL); 6495 hash_entry_size = elf_section_data (s)->this_hdr.sh_entsize; 6496 s->size = ((2 + bucketcount + dynsymcount) * hash_entry_size); 6497 s->contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6498 if (s->contents == NULL) 6499 return FALSE; 6500 6501 bfd_put (8 * hash_entry_size, output_bfd, bucketcount, s->contents); 6502 bfd_put (8 * hash_entry_size, output_bfd, dynsymcount, 6503 s->contents + hash_entry_size); 6504 } 6505 6506 if (info->emit_gnu_hash) 6507 { 6508 size_t i, cnt; 6509 unsigned char *contents; 6510 struct collect_gnu_hash_codes cinfo; 6511 bfd_size_type amt; 6512 size_t bucketcount; 6513 6514 memset (&cinfo, 0, sizeof (cinfo)); 6515 6516 /* Compute the hash values for all exported symbols. At the same 6517 time store the values in an array so that we could use them for 6518 optimizations. */ 6519 amt = dynsymcount * 2 * sizeof (unsigned long int); 6520 cinfo.hashcodes = (long unsigned int *) bfd_malloc (amt); 6521 if (cinfo.hashcodes == NULL) 6522 return FALSE; 6523 6524 cinfo.hashval = cinfo.hashcodes + dynsymcount; 6525 cinfo.min_dynindx = -1; 6526 cinfo.output_bfd = output_bfd; 6527 cinfo.bed = bed; 6528 6529 /* Put all hash values in HASHCODES. */ 6530 elf_link_hash_traverse (elf_hash_table (info), 6531 elf_collect_gnu_hash_codes, &cinfo); 6532 if (cinfo.error) 6533 { 6534 free (cinfo.hashcodes); 6535 return FALSE; 6536 } 6537 6538 bucketcount 6539 = compute_bucket_count (info, cinfo.hashcodes, cinfo.nsyms, 1); 6540 6541 if (bucketcount == 0) 6542 { 6543 free (cinfo.hashcodes); 6544 return FALSE; 6545 } 6546 6547 s = bfd_get_linker_section (dynobj, ".gnu.hash"); 6548 BFD_ASSERT (s != NULL); 6549 6550 if (cinfo.nsyms == 0) 6551 { 6552 /* Empty .gnu.hash section is special. */ 6553 BFD_ASSERT (cinfo.min_dynindx == -1); 6554 free (cinfo.hashcodes); 6555 s->size = 5 * 4 + bed->s->arch_size / 8; 6556 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6557 if (contents == NULL) 6558 return FALSE; 6559 s->contents = contents; 6560 /* 1 empty bucket. */ 6561 bfd_put_32 (output_bfd, 1, contents); 6562 /* SYMIDX above the special symbol 0. */ 6563 bfd_put_32 (output_bfd, 1, contents + 4); 6564 /* Just one word for bitmask. */ 6565 bfd_put_32 (output_bfd, 1, contents + 8); 6566 /* Only hash fn bloom filter. */ 6567 bfd_put_32 (output_bfd, 0, contents + 12); 6568 /* No hashes are valid - empty bitmask. */ 6569 bfd_put (bed->s->arch_size, output_bfd, 0, contents + 16); 6570 /* No hashes in the only bucket. */ 6571 bfd_put_32 (output_bfd, 0, 6572 contents + 16 + bed->s->arch_size / 8); 6573 } 6574 else 6575 { 6576 unsigned long int maskwords, maskbitslog2, x; 6577 BFD_ASSERT (cinfo.min_dynindx != -1); 6578 6579 x = cinfo.nsyms; 6580 maskbitslog2 = 1; 6581 while ((x >>= 1) != 0) 6582 ++maskbitslog2; 6583 if (maskbitslog2 < 3) 6584 maskbitslog2 = 5; 6585 else if ((1 << (maskbitslog2 - 2)) & cinfo.nsyms) 6586 maskbitslog2 = maskbitslog2 + 3; 6587 else 6588 maskbitslog2 = maskbitslog2 + 2; 6589 if (bed->s->arch_size == 64) 6590 { 6591 if (maskbitslog2 == 5) 6592 maskbitslog2 = 6; 6593 cinfo.shift1 = 6; 6594 } 6595 else 6596 cinfo.shift1 = 5; 6597 cinfo.mask = (1 << cinfo.shift1) - 1; 6598 cinfo.shift2 = maskbitslog2; 6599 cinfo.maskbits = 1 << maskbitslog2; 6600 maskwords = 1 << (maskbitslog2 - cinfo.shift1); 6601 amt = bucketcount * sizeof (unsigned long int) * 2; 6602 amt += maskwords * sizeof (bfd_vma); 6603 cinfo.bitmask = (bfd_vma *) bfd_malloc (amt); 6604 if (cinfo.bitmask == NULL) 6605 { 6606 free (cinfo.hashcodes); 6607 return FALSE; 6608 } 6609 6610 cinfo.counts = (long unsigned int *) (cinfo.bitmask + maskwords); 6611 cinfo.indx = cinfo.counts + bucketcount; 6612 cinfo.symindx = dynsymcount - cinfo.nsyms; 6613 memset (cinfo.bitmask, 0, maskwords * sizeof (bfd_vma)); 6614 6615 /* Determine how often each hash bucket is used. */ 6616 memset (cinfo.counts, 0, bucketcount * sizeof (cinfo.counts[0])); 6617 for (i = 0; i < cinfo.nsyms; ++i) 6618 ++cinfo.counts[cinfo.hashcodes[i] % bucketcount]; 6619 6620 for (i = 0, cnt = cinfo.symindx; i < bucketcount; ++i) 6621 if (cinfo.counts[i] != 0) 6622 { 6623 cinfo.indx[i] = cnt; 6624 cnt += cinfo.counts[i]; 6625 } 6626 BFD_ASSERT (cnt == dynsymcount); 6627 cinfo.bucketcount = bucketcount; 6628 cinfo.local_indx = cinfo.min_dynindx; 6629 6630 s->size = (4 + bucketcount + cinfo.nsyms) * 4; 6631 s->size += cinfo.maskbits / 8; 6632 contents = (unsigned char *) bfd_zalloc (output_bfd, s->size); 6633 if (contents == NULL) 6634 { 6635 free (cinfo.bitmask); 6636 free (cinfo.hashcodes); 6637 return FALSE; 6638 } 6639 6640 s->contents = contents; 6641 bfd_put_32 (output_bfd, bucketcount, contents); 6642 bfd_put_32 (output_bfd, cinfo.symindx, contents + 4); 6643 bfd_put_32 (output_bfd, maskwords, contents + 8); 6644 bfd_put_32 (output_bfd, cinfo.shift2, contents + 12); 6645 contents += 16 + cinfo.maskbits / 8; 6646 6647 for (i = 0; i < bucketcount; ++i) 6648 { 6649 if (cinfo.counts[i] == 0) 6650 bfd_put_32 (output_bfd, 0, contents); 6651 else 6652 bfd_put_32 (output_bfd, cinfo.indx[i], contents); 6653 contents += 4; 6654 } 6655 6656 cinfo.contents = contents; 6657 6658 /* Renumber dynamic symbols, populate .gnu.hash section. */ 6659 elf_link_hash_traverse (elf_hash_table (info), 6660 elf_renumber_gnu_hash_syms, &cinfo); 6661 6662 contents = s->contents + 16; 6663 for (i = 0; i < maskwords; ++i) 6664 { 6665 bfd_put (bed->s->arch_size, output_bfd, cinfo.bitmask[i], 6666 contents); 6667 contents += bed->s->arch_size / 8; 6668 } 6669 6670 free (cinfo.bitmask); 6671 free (cinfo.hashcodes); 6672 } 6673 } 6674 6675 s = bfd_get_linker_section (dynobj, ".dynstr"); 6676 BFD_ASSERT (s != NULL); 6677 6678 elf_finalize_dynstr (output_bfd, info); 6679 6680 s->size = _bfd_elf_strtab_size (elf_hash_table (info)->dynstr); 6681 6682 for (dtagcount = 0; dtagcount <= info->spare_dynamic_tags; ++dtagcount) 6683 if (!_bfd_elf_add_dynamic_entry (info, DT_NULL, 0)) 6684 return FALSE; 6685 } 6686 6687 return TRUE; 6688 } 6689 6690 /* Make sure sec_info_type is cleared if sec_info is cleared too. */ 6691 6692 static void 6693 merge_sections_remove_hook (bfd *abfd ATTRIBUTE_UNUSED, 6694 asection *sec) 6695 { 6696 BFD_ASSERT (sec->sec_info_type == SEC_INFO_TYPE_MERGE); 6697 sec->sec_info_type = SEC_INFO_TYPE_NONE; 6698 } 6699 6700 /* Finish SHF_MERGE section merging. */ 6701 6702 bfd_boolean 6703 _bfd_elf_merge_sections (bfd *abfd, struct bfd_link_info *info) 6704 { 6705 bfd *ibfd; 6706 asection *sec; 6707 6708 if (!is_elf_hash_table (info->hash)) 6709 return FALSE; 6710 6711 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 6712 if ((ibfd->flags & DYNAMIC) == 0) 6713 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 6714 if ((sec->flags & SEC_MERGE) != 0 6715 && !bfd_is_abs_section (sec->output_section)) 6716 { 6717 struct bfd_elf_section_data *secdata; 6718 6719 secdata = elf_section_data (sec); 6720 if (! _bfd_add_merge_section (abfd, 6721 &elf_hash_table (info)->merge_info, 6722 sec, &secdata->sec_info)) 6723 return FALSE; 6724 else if (secdata->sec_info) 6725 sec->sec_info_type = SEC_INFO_TYPE_MERGE; 6726 } 6727 6728 if (elf_hash_table (info)->merge_info != NULL) 6729 _bfd_merge_sections (abfd, info, elf_hash_table (info)->merge_info, 6730 merge_sections_remove_hook); 6731 return TRUE; 6732 } 6733 6734 /* Create an entry in an ELF linker hash table. */ 6735 6736 struct bfd_hash_entry * 6737 _bfd_elf_link_hash_newfunc (struct bfd_hash_entry *entry, 6738 struct bfd_hash_table *table, 6739 const char *string) 6740 { 6741 /* Allocate the structure if it has not already been allocated by a 6742 subclass. */ 6743 if (entry == NULL) 6744 { 6745 entry = (struct bfd_hash_entry *) 6746 bfd_hash_allocate (table, sizeof (struct elf_link_hash_entry)); 6747 if (entry == NULL) 6748 return entry; 6749 } 6750 6751 /* Call the allocation method of the superclass. */ 6752 entry = _bfd_link_hash_newfunc (entry, table, string); 6753 if (entry != NULL) 6754 { 6755 struct elf_link_hash_entry *ret = (struct elf_link_hash_entry *) entry; 6756 struct elf_link_hash_table *htab = (struct elf_link_hash_table *) table; 6757 6758 /* Set local fields. */ 6759 ret->indx = -1; 6760 ret->dynindx = -1; 6761 ret->got = htab->init_got_refcount; 6762 ret->plt = htab->init_plt_refcount; 6763 memset (&ret->size, 0, (sizeof (struct elf_link_hash_entry) 6764 - offsetof (struct elf_link_hash_entry, size))); 6765 /* Assume that we have been called by a non-ELF symbol reader. 6766 This flag is then reset by the code which reads an ELF input 6767 file. This ensures that a symbol created by a non-ELF symbol 6768 reader will have the flag set correctly. */ 6769 ret->non_elf = 1; 6770 } 6771 6772 return entry; 6773 } 6774 6775 /* Copy data from an indirect symbol to its direct symbol, hiding the 6776 old indirect symbol. Also used for copying flags to a weakdef. */ 6777 6778 void 6779 _bfd_elf_link_hash_copy_indirect (struct bfd_link_info *info, 6780 struct elf_link_hash_entry *dir, 6781 struct elf_link_hash_entry *ind) 6782 { 6783 struct elf_link_hash_table *htab; 6784 6785 /* Copy down any references that we may have already seen to the 6786 symbol which just became indirect. */ 6787 6788 dir->ref_dynamic |= ind->ref_dynamic; 6789 dir->ref_regular |= ind->ref_regular; 6790 dir->ref_regular_nonweak |= ind->ref_regular_nonweak; 6791 dir->non_got_ref |= ind->non_got_ref; 6792 dir->needs_plt |= ind->needs_plt; 6793 dir->pointer_equality_needed |= ind->pointer_equality_needed; 6794 6795 if (ind->root.type != bfd_link_hash_indirect) 6796 return; 6797 6798 /* Copy over the global and procedure linkage table refcount entries. 6799 These may have been already set up by a check_relocs routine. */ 6800 htab = elf_hash_table (info); 6801 if (ind->got.refcount > htab->init_got_refcount.refcount) 6802 { 6803 if (dir->got.refcount < 0) 6804 dir->got.refcount = 0; 6805 dir->got.refcount += ind->got.refcount; 6806 ind->got.refcount = htab->init_got_refcount.refcount; 6807 } 6808 6809 if (ind->plt.refcount > htab->init_plt_refcount.refcount) 6810 { 6811 if (dir->plt.refcount < 0) 6812 dir->plt.refcount = 0; 6813 dir->plt.refcount += ind->plt.refcount; 6814 ind->plt.refcount = htab->init_plt_refcount.refcount; 6815 } 6816 6817 if (ind->dynindx != -1) 6818 { 6819 if (dir->dynindx != -1) 6820 _bfd_elf_strtab_delref (htab->dynstr, dir->dynstr_index); 6821 dir->dynindx = ind->dynindx; 6822 dir->dynstr_index = ind->dynstr_index; 6823 ind->dynindx = -1; 6824 ind->dynstr_index = 0; 6825 } 6826 } 6827 6828 void 6829 _bfd_elf_link_hash_hide_symbol (struct bfd_link_info *info, 6830 struct elf_link_hash_entry *h, 6831 bfd_boolean force_local) 6832 { 6833 /* STT_GNU_IFUNC symbol must go through PLT. */ 6834 if (h->type != STT_GNU_IFUNC) 6835 { 6836 h->plt = elf_hash_table (info)->init_plt_offset; 6837 h->needs_plt = 0; 6838 } 6839 if (force_local) 6840 { 6841 h->forced_local = 1; 6842 if (h->dynindx != -1) 6843 { 6844 h->dynindx = -1; 6845 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 6846 h->dynstr_index); 6847 } 6848 } 6849 } 6850 6851 /* Initialize an ELF linker hash table. */ 6852 6853 bfd_boolean 6854 _bfd_elf_link_hash_table_init 6855 (struct elf_link_hash_table *table, 6856 bfd *abfd, 6857 struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *, 6858 struct bfd_hash_table *, 6859 const char *), 6860 unsigned int entsize, 6861 enum elf_target_id target_id) 6862 { 6863 bfd_boolean ret; 6864 int can_refcount = get_elf_backend_data (abfd)->can_refcount; 6865 6866 memset (table, 0, sizeof * table); 6867 table->init_got_refcount.refcount = can_refcount - 1; 6868 table->init_plt_refcount.refcount = can_refcount - 1; 6869 table->init_got_offset.offset = -(bfd_vma) 1; 6870 table->init_plt_offset.offset = -(bfd_vma) 1; 6871 /* The first dynamic symbol is a dummy. */ 6872 table->dynsymcount = 1; 6873 6874 ret = _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize); 6875 6876 table->root.type = bfd_link_elf_hash_table; 6877 table->hash_table_id = target_id; 6878 6879 return ret; 6880 } 6881 6882 /* Create an ELF linker hash table. */ 6883 6884 struct bfd_link_hash_table * 6885 _bfd_elf_link_hash_table_create (bfd *abfd) 6886 { 6887 struct elf_link_hash_table *ret; 6888 bfd_size_type amt = sizeof (struct elf_link_hash_table); 6889 6890 ret = (struct elf_link_hash_table *) bfd_malloc (amt); 6891 if (ret == NULL) 6892 return NULL; 6893 6894 if (! _bfd_elf_link_hash_table_init (ret, abfd, _bfd_elf_link_hash_newfunc, 6895 sizeof (struct elf_link_hash_entry), 6896 GENERIC_ELF_DATA)) 6897 { 6898 free (ret); 6899 return NULL; 6900 } 6901 6902 return &ret->root; 6903 } 6904 6905 /* This is a hook for the ELF emulation code in the generic linker to 6906 tell the backend linker what file name to use for the DT_NEEDED 6907 entry for a dynamic object. */ 6908 6909 void 6910 bfd_elf_set_dt_needed_name (bfd *abfd, const char *name) 6911 { 6912 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 6913 && bfd_get_format (abfd) == bfd_object) 6914 elf_dt_name (abfd) = name; 6915 } 6916 6917 int 6918 bfd_elf_get_dyn_lib_class (bfd *abfd) 6919 { 6920 int lib_class; 6921 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 6922 && bfd_get_format (abfd) == bfd_object) 6923 lib_class = elf_dyn_lib_class (abfd); 6924 else 6925 lib_class = 0; 6926 return lib_class; 6927 } 6928 6929 void 6930 bfd_elf_set_dyn_lib_class (bfd *abfd, enum dynamic_lib_link_class lib_class) 6931 { 6932 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 6933 && bfd_get_format (abfd) == bfd_object) 6934 elf_dyn_lib_class (abfd) = lib_class; 6935 } 6936 6937 /* Get the list of DT_NEEDED entries for a link. This is a hook for 6938 the linker ELF emulation code. */ 6939 6940 struct bfd_link_needed_list * 6941 bfd_elf_get_needed_list (bfd *abfd ATTRIBUTE_UNUSED, 6942 struct bfd_link_info *info) 6943 { 6944 if (! is_elf_hash_table (info->hash)) 6945 return NULL; 6946 return elf_hash_table (info)->needed; 6947 } 6948 6949 /* Get the list of DT_RPATH/DT_RUNPATH entries for a link. This is a 6950 hook for the linker ELF emulation code. */ 6951 6952 struct bfd_link_needed_list * 6953 bfd_elf_get_runpath_list (bfd *abfd ATTRIBUTE_UNUSED, 6954 struct bfd_link_info *info) 6955 { 6956 if (! is_elf_hash_table (info->hash)) 6957 return NULL; 6958 return elf_hash_table (info)->runpath; 6959 } 6960 6961 /* Get the name actually used for a dynamic object for a link. This 6962 is the SONAME entry if there is one. Otherwise, it is the string 6963 passed to bfd_elf_set_dt_needed_name, or it is the filename. */ 6964 6965 const char * 6966 bfd_elf_get_dt_soname (bfd *abfd) 6967 { 6968 if (bfd_get_flavour (abfd) == bfd_target_elf_flavour 6969 && bfd_get_format (abfd) == bfd_object) 6970 return elf_dt_name (abfd); 6971 return NULL; 6972 } 6973 6974 /* Get the list of DT_NEEDED entries from a BFD. This is a hook for 6975 the ELF linker emulation code. */ 6976 6977 bfd_boolean 6978 bfd_elf_get_bfd_needed_list (bfd *abfd, 6979 struct bfd_link_needed_list **pneeded) 6980 { 6981 asection *s; 6982 bfd_byte *dynbuf = NULL; 6983 unsigned int elfsec; 6984 unsigned long shlink; 6985 bfd_byte *extdyn, *extdynend; 6986 size_t extdynsize; 6987 void (*swap_dyn_in) (bfd *, const void *, Elf_Internal_Dyn *); 6988 6989 *pneeded = NULL; 6990 6991 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour 6992 || bfd_get_format (abfd) != bfd_object) 6993 return TRUE; 6994 6995 s = bfd_get_section_by_name (abfd, ".dynamic"); 6996 if (s == NULL || s->size == 0) 6997 return TRUE; 6998 6999 if (!bfd_malloc_and_get_section (abfd, s, &dynbuf)) 7000 goto error_return; 7001 7002 elfsec = _bfd_elf_section_from_bfd_section (abfd, s); 7003 if (elfsec == SHN_BAD) 7004 goto error_return; 7005 7006 shlink = elf_elfsections (abfd)[elfsec]->sh_link; 7007 7008 extdynsize = get_elf_backend_data (abfd)->s->sizeof_dyn; 7009 swap_dyn_in = get_elf_backend_data (abfd)->s->swap_dyn_in; 7010 7011 extdyn = dynbuf; 7012 extdynend = extdyn + s->size; 7013 for (; extdyn < extdynend; extdyn += extdynsize) 7014 { 7015 Elf_Internal_Dyn dyn; 7016 7017 (*swap_dyn_in) (abfd, extdyn, &dyn); 7018 7019 if (dyn.d_tag == DT_NULL) 7020 break; 7021 7022 if (dyn.d_tag == DT_NEEDED) 7023 { 7024 const char *string; 7025 struct bfd_link_needed_list *l; 7026 unsigned int tagv = dyn.d_un.d_val; 7027 bfd_size_type amt; 7028 7029 string = bfd_elf_string_from_elf_section (abfd, shlink, tagv); 7030 if (string == NULL) 7031 goto error_return; 7032 7033 amt = sizeof *l; 7034 l = (struct bfd_link_needed_list *) bfd_alloc (abfd, amt); 7035 if (l == NULL) 7036 goto error_return; 7037 7038 l->by = abfd; 7039 l->name = string; 7040 l->next = *pneeded; 7041 *pneeded = l; 7042 } 7043 } 7044 7045 free (dynbuf); 7046 7047 return TRUE; 7048 7049 error_return: 7050 if (dynbuf != NULL) 7051 free (dynbuf); 7052 return FALSE; 7053 } 7054 7055 struct elf_symbuf_symbol 7056 { 7057 unsigned long st_name; /* Symbol name, index in string tbl */ 7058 unsigned char st_info; /* Type and binding attributes */ 7059 unsigned char st_other; /* Visibilty, and target specific */ 7060 }; 7061 7062 struct elf_symbuf_head 7063 { 7064 struct elf_symbuf_symbol *ssym; 7065 bfd_size_type count; 7066 unsigned int st_shndx; 7067 }; 7068 7069 struct elf_symbol 7070 { 7071 union 7072 { 7073 Elf_Internal_Sym *isym; 7074 struct elf_symbuf_symbol *ssym; 7075 } u; 7076 const char *name; 7077 }; 7078 7079 /* Sort references to symbols by ascending section number. */ 7080 7081 static int 7082 elf_sort_elf_symbol (const void *arg1, const void *arg2) 7083 { 7084 const Elf_Internal_Sym *s1 = *(const Elf_Internal_Sym **) arg1; 7085 const Elf_Internal_Sym *s2 = *(const Elf_Internal_Sym **) arg2; 7086 7087 return s1->st_shndx - s2->st_shndx; 7088 } 7089 7090 static int 7091 elf_sym_name_compare (const void *arg1, const void *arg2) 7092 { 7093 const struct elf_symbol *s1 = (const struct elf_symbol *) arg1; 7094 const struct elf_symbol *s2 = (const struct elf_symbol *) arg2; 7095 return strcmp (s1->name, s2->name); 7096 } 7097 7098 static struct elf_symbuf_head * 7099 elf_create_symbuf (bfd_size_type symcount, Elf_Internal_Sym *isymbuf) 7100 { 7101 Elf_Internal_Sym **ind, **indbufend, **indbuf; 7102 struct elf_symbuf_symbol *ssym; 7103 struct elf_symbuf_head *ssymbuf, *ssymhead; 7104 bfd_size_type i, shndx_count, total_size; 7105 7106 indbuf = (Elf_Internal_Sym **) bfd_malloc2 (symcount, sizeof (*indbuf)); 7107 if (indbuf == NULL) 7108 return NULL; 7109 7110 for (ind = indbuf, i = 0; i < symcount; i++) 7111 if (isymbuf[i].st_shndx != SHN_UNDEF) 7112 *ind++ = &isymbuf[i]; 7113 indbufend = ind; 7114 7115 qsort (indbuf, indbufend - indbuf, sizeof (Elf_Internal_Sym *), 7116 elf_sort_elf_symbol); 7117 7118 shndx_count = 0; 7119 if (indbufend > indbuf) 7120 for (ind = indbuf, shndx_count++; ind < indbufend - 1; ind++) 7121 if (ind[0]->st_shndx != ind[1]->st_shndx) 7122 shndx_count++; 7123 7124 total_size = ((shndx_count + 1) * sizeof (*ssymbuf) 7125 + (indbufend - indbuf) * sizeof (*ssym)); 7126 ssymbuf = (struct elf_symbuf_head *) bfd_malloc (total_size); 7127 if (ssymbuf == NULL) 7128 { 7129 free (indbuf); 7130 return NULL; 7131 } 7132 7133 ssym = (struct elf_symbuf_symbol *) (ssymbuf + shndx_count + 1); 7134 ssymbuf->ssym = NULL; 7135 ssymbuf->count = shndx_count; 7136 ssymbuf->st_shndx = 0; 7137 for (ssymhead = ssymbuf, ind = indbuf; ind < indbufend; ssym++, ind++) 7138 { 7139 if (ind == indbuf || ssymhead->st_shndx != (*ind)->st_shndx) 7140 { 7141 ssymhead++; 7142 ssymhead->ssym = ssym; 7143 ssymhead->count = 0; 7144 ssymhead->st_shndx = (*ind)->st_shndx; 7145 } 7146 ssym->st_name = (*ind)->st_name; 7147 ssym->st_info = (*ind)->st_info; 7148 ssym->st_other = (*ind)->st_other; 7149 ssymhead->count++; 7150 } 7151 BFD_ASSERT ((bfd_size_type) (ssymhead - ssymbuf) == shndx_count 7152 && (((bfd_hostptr_t) ssym - (bfd_hostptr_t) ssymbuf) 7153 == total_size)); 7154 7155 free (indbuf); 7156 return ssymbuf; 7157 } 7158 7159 /* Check if 2 sections define the same set of local and global 7160 symbols. */ 7161 7162 static bfd_boolean 7163 bfd_elf_match_symbols_in_sections (asection *sec1, asection *sec2, 7164 struct bfd_link_info *info) 7165 { 7166 bfd *bfd1, *bfd2; 7167 const struct elf_backend_data *bed1, *bed2; 7168 Elf_Internal_Shdr *hdr1, *hdr2; 7169 bfd_size_type symcount1, symcount2; 7170 Elf_Internal_Sym *isymbuf1, *isymbuf2; 7171 struct elf_symbuf_head *ssymbuf1, *ssymbuf2; 7172 Elf_Internal_Sym *isym, *isymend; 7173 struct elf_symbol *symtable1 = NULL, *symtable2 = NULL; 7174 bfd_size_type count1, count2, i; 7175 unsigned int shndx1, shndx2; 7176 bfd_boolean result; 7177 7178 bfd1 = sec1->owner; 7179 bfd2 = sec2->owner; 7180 7181 /* Both sections have to be in ELF. */ 7182 if (bfd_get_flavour (bfd1) != bfd_target_elf_flavour 7183 || bfd_get_flavour (bfd2) != bfd_target_elf_flavour) 7184 return FALSE; 7185 7186 if (elf_section_type (sec1) != elf_section_type (sec2)) 7187 return FALSE; 7188 7189 shndx1 = _bfd_elf_section_from_bfd_section (bfd1, sec1); 7190 shndx2 = _bfd_elf_section_from_bfd_section (bfd2, sec2); 7191 if (shndx1 == SHN_BAD || shndx2 == SHN_BAD) 7192 return FALSE; 7193 7194 bed1 = get_elf_backend_data (bfd1); 7195 bed2 = get_elf_backend_data (bfd2); 7196 hdr1 = &elf_tdata (bfd1)->symtab_hdr; 7197 symcount1 = hdr1->sh_size / bed1->s->sizeof_sym; 7198 hdr2 = &elf_tdata (bfd2)->symtab_hdr; 7199 symcount2 = hdr2->sh_size / bed2->s->sizeof_sym; 7200 7201 if (symcount1 == 0 || symcount2 == 0) 7202 return FALSE; 7203 7204 result = FALSE; 7205 isymbuf1 = NULL; 7206 isymbuf2 = NULL; 7207 ssymbuf1 = (struct elf_symbuf_head *) elf_tdata (bfd1)->symbuf; 7208 ssymbuf2 = (struct elf_symbuf_head *) elf_tdata (bfd2)->symbuf; 7209 7210 if (ssymbuf1 == NULL) 7211 { 7212 isymbuf1 = bfd_elf_get_elf_syms (bfd1, hdr1, symcount1, 0, 7213 NULL, NULL, NULL); 7214 if (isymbuf1 == NULL) 7215 goto done; 7216 7217 if (!info->reduce_memory_overheads) 7218 elf_tdata (bfd1)->symbuf = ssymbuf1 7219 = elf_create_symbuf (symcount1, isymbuf1); 7220 } 7221 7222 if (ssymbuf1 == NULL || ssymbuf2 == NULL) 7223 { 7224 isymbuf2 = bfd_elf_get_elf_syms (bfd2, hdr2, symcount2, 0, 7225 NULL, NULL, NULL); 7226 if (isymbuf2 == NULL) 7227 goto done; 7228 7229 if (ssymbuf1 != NULL && !info->reduce_memory_overheads) 7230 elf_tdata (bfd2)->symbuf = ssymbuf2 7231 = elf_create_symbuf (symcount2, isymbuf2); 7232 } 7233 7234 if (ssymbuf1 != NULL && ssymbuf2 != NULL) 7235 { 7236 /* Optimized faster version. */ 7237 bfd_size_type lo, hi, mid; 7238 struct elf_symbol *symp; 7239 struct elf_symbuf_symbol *ssym, *ssymend; 7240 7241 lo = 0; 7242 hi = ssymbuf1->count; 7243 ssymbuf1++; 7244 count1 = 0; 7245 while (lo < hi) 7246 { 7247 mid = (lo + hi) / 2; 7248 if (shndx1 < ssymbuf1[mid].st_shndx) 7249 hi = mid; 7250 else if (shndx1 > ssymbuf1[mid].st_shndx) 7251 lo = mid + 1; 7252 else 7253 { 7254 count1 = ssymbuf1[mid].count; 7255 ssymbuf1 += mid; 7256 break; 7257 } 7258 } 7259 7260 lo = 0; 7261 hi = ssymbuf2->count; 7262 ssymbuf2++; 7263 count2 = 0; 7264 while (lo < hi) 7265 { 7266 mid = (lo + hi) / 2; 7267 if (shndx2 < ssymbuf2[mid].st_shndx) 7268 hi = mid; 7269 else if (shndx2 > ssymbuf2[mid].st_shndx) 7270 lo = mid + 1; 7271 else 7272 { 7273 count2 = ssymbuf2[mid].count; 7274 ssymbuf2 += mid; 7275 break; 7276 } 7277 } 7278 7279 if (count1 == 0 || count2 == 0 || count1 != count2) 7280 goto done; 7281 7282 symtable1 = (struct elf_symbol *) 7283 bfd_malloc (count1 * sizeof (struct elf_symbol)); 7284 symtable2 = (struct elf_symbol *) 7285 bfd_malloc (count2 * sizeof (struct elf_symbol)); 7286 if (symtable1 == NULL || symtable2 == NULL) 7287 goto done; 7288 7289 symp = symtable1; 7290 for (ssym = ssymbuf1->ssym, ssymend = ssym + count1; 7291 ssym < ssymend; ssym++, symp++) 7292 { 7293 symp->u.ssym = ssym; 7294 symp->name = bfd_elf_string_from_elf_section (bfd1, 7295 hdr1->sh_link, 7296 ssym->st_name); 7297 } 7298 7299 symp = symtable2; 7300 for (ssym = ssymbuf2->ssym, ssymend = ssym + count2; 7301 ssym < ssymend; ssym++, symp++) 7302 { 7303 symp->u.ssym = ssym; 7304 symp->name = bfd_elf_string_from_elf_section (bfd2, 7305 hdr2->sh_link, 7306 ssym->st_name); 7307 } 7308 7309 /* Sort symbol by name. */ 7310 qsort (symtable1, count1, sizeof (struct elf_symbol), 7311 elf_sym_name_compare); 7312 qsort (symtable2, count1, sizeof (struct elf_symbol), 7313 elf_sym_name_compare); 7314 7315 for (i = 0; i < count1; i++) 7316 /* Two symbols must have the same binding, type and name. */ 7317 if (symtable1 [i].u.ssym->st_info != symtable2 [i].u.ssym->st_info 7318 || symtable1 [i].u.ssym->st_other != symtable2 [i].u.ssym->st_other 7319 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 7320 goto done; 7321 7322 result = TRUE; 7323 goto done; 7324 } 7325 7326 symtable1 = (struct elf_symbol *) 7327 bfd_malloc (symcount1 * sizeof (struct elf_symbol)); 7328 symtable2 = (struct elf_symbol *) 7329 bfd_malloc (symcount2 * sizeof (struct elf_symbol)); 7330 if (symtable1 == NULL || symtable2 == NULL) 7331 goto done; 7332 7333 /* Count definitions in the section. */ 7334 count1 = 0; 7335 for (isym = isymbuf1, isymend = isym + symcount1; isym < isymend; isym++) 7336 if (isym->st_shndx == shndx1) 7337 symtable1[count1++].u.isym = isym; 7338 7339 count2 = 0; 7340 for (isym = isymbuf2, isymend = isym + symcount2; isym < isymend; isym++) 7341 if (isym->st_shndx == shndx2) 7342 symtable2[count2++].u.isym = isym; 7343 7344 if (count1 == 0 || count2 == 0 || count1 != count2) 7345 goto done; 7346 7347 for (i = 0; i < count1; i++) 7348 symtable1[i].name 7349 = bfd_elf_string_from_elf_section (bfd1, hdr1->sh_link, 7350 symtable1[i].u.isym->st_name); 7351 7352 for (i = 0; i < count2; i++) 7353 symtable2[i].name 7354 = bfd_elf_string_from_elf_section (bfd2, hdr2->sh_link, 7355 symtable2[i].u.isym->st_name); 7356 7357 /* Sort symbol by name. */ 7358 qsort (symtable1, count1, sizeof (struct elf_symbol), 7359 elf_sym_name_compare); 7360 qsort (symtable2, count1, sizeof (struct elf_symbol), 7361 elf_sym_name_compare); 7362 7363 for (i = 0; i < count1; i++) 7364 /* Two symbols must have the same binding, type and name. */ 7365 if (symtable1 [i].u.isym->st_info != symtable2 [i].u.isym->st_info 7366 || symtable1 [i].u.isym->st_other != symtable2 [i].u.isym->st_other 7367 || strcmp (symtable1 [i].name, symtable2 [i].name) != 0) 7368 goto done; 7369 7370 result = TRUE; 7371 7372 done: 7373 if (symtable1) 7374 free (symtable1); 7375 if (symtable2) 7376 free (symtable2); 7377 if (isymbuf1) 7378 free (isymbuf1); 7379 if (isymbuf2) 7380 free (isymbuf2); 7381 7382 return result; 7383 } 7384 7385 /* Return TRUE if 2 section types are compatible. */ 7386 7387 bfd_boolean 7388 _bfd_elf_match_sections_by_type (bfd *abfd, const asection *asec, 7389 bfd *bbfd, const asection *bsec) 7390 { 7391 if (asec == NULL 7392 || bsec == NULL 7393 || abfd->xvec->flavour != bfd_target_elf_flavour 7394 || bbfd->xvec->flavour != bfd_target_elf_flavour) 7395 return TRUE; 7396 7397 return elf_section_type (asec) == elf_section_type (bsec); 7398 } 7399 7400 /* Final phase of ELF linker. */ 7401 7402 /* A structure we use to avoid passing large numbers of arguments. */ 7403 7404 struct elf_final_link_info 7405 { 7406 /* General link information. */ 7407 struct bfd_link_info *info; 7408 /* Output BFD. */ 7409 bfd *output_bfd; 7410 /* Symbol string table. */ 7411 struct bfd_strtab_hash *symstrtab; 7412 /* .dynsym section. */ 7413 asection *dynsym_sec; 7414 /* .hash section. */ 7415 asection *hash_sec; 7416 /* symbol version section (.gnu.version). */ 7417 asection *symver_sec; 7418 /* Buffer large enough to hold contents of any section. */ 7419 bfd_byte *contents; 7420 /* Buffer large enough to hold external relocs of any section. */ 7421 void *external_relocs; 7422 /* Buffer large enough to hold internal relocs of any section. */ 7423 Elf_Internal_Rela *internal_relocs; 7424 /* Buffer large enough to hold external local symbols of any input 7425 BFD. */ 7426 bfd_byte *external_syms; 7427 /* And a buffer for symbol section indices. */ 7428 Elf_External_Sym_Shndx *locsym_shndx; 7429 /* Buffer large enough to hold internal local symbols of any input 7430 BFD. */ 7431 Elf_Internal_Sym *internal_syms; 7432 /* Array large enough to hold a symbol index for each local symbol 7433 of any input BFD. */ 7434 long *indices; 7435 /* Array large enough to hold a section pointer for each local 7436 symbol of any input BFD. */ 7437 asection **sections; 7438 /* Buffer to hold swapped out symbols. */ 7439 bfd_byte *symbuf; 7440 /* And one for symbol section indices. */ 7441 Elf_External_Sym_Shndx *symshndxbuf; 7442 /* Number of swapped out symbols in buffer. */ 7443 size_t symbuf_count; 7444 /* Number of symbols which fit in symbuf. */ 7445 size_t symbuf_size; 7446 /* And same for symshndxbuf. */ 7447 size_t shndxbuf_size; 7448 /* Number of STT_FILE syms seen. */ 7449 size_t filesym_count; 7450 }; 7451 7452 /* This struct is used to pass information to elf_link_output_extsym. */ 7453 7454 struct elf_outext_info 7455 { 7456 bfd_boolean failed; 7457 bfd_boolean localsyms; 7458 bfd_boolean need_second_pass; 7459 bfd_boolean second_pass; 7460 struct elf_final_link_info *flinfo; 7461 }; 7462 7463 7464 /* Support for evaluating a complex relocation. 7465 7466 Complex relocations are generalized, self-describing relocations. The 7467 implementation of them consists of two parts: complex symbols, and the 7468 relocations themselves. 7469 7470 The relocations are use a reserved elf-wide relocation type code (R_RELC 7471 external / BFD_RELOC_RELC internal) and an encoding of relocation field 7472 information (start bit, end bit, word width, etc) into the addend. This 7473 information is extracted from CGEN-generated operand tables within gas. 7474 7475 Complex symbols are mangled symbols (BSF_RELC external / STT_RELC 7476 internal) representing prefix-notation expressions, including but not 7477 limited to those sorts of expressions normally encoded as addends in the 7478 addend field. The symbol mangling format is: 7479 7480 <node> := <literal> 7481 | <unary-operator> ':' <node> 7482 | <binary-operator> ':' <node> ':' <node> 7483 ; 7484 7485 <literal> := 's' <digits=N> ':' <N character symbol name> 7486 | 'S' <digits=N> ':' <N character section name> 7487 | '#' <hexdigits> 7488 ; 7489 7490 <binary-operator> := as in C 7491 <unary-operator> := as in C, plus "0-" for unambiguous negation. */ 7492 7493 static void 7494 set_symbol_value (bfd *bfd_with_globals, 7495 Elf_Internal_Sym *isymbuf, 7496 size_t locsymcount, 7497 size_t symidx, 7498 bfd_vma val) 7499 { 7500 struct elf_link_hash_entry **sym_hashes; 7501 struct elf_link_hash_entry *h; 7502 size_t extsymoff = locsymcount; 7503 7504 if (symidx < locsymcount) 7505 { 7506 Elf_Internal_Sym *sym; 7507 7508 sym = isymbuf + symidx; 7509 if (ELF_ST_BIND (sym->st_info) == STB_LOCAL) 7510 { 7511 /* It is a local symbol: move it to the 7512 "absolute" section and give it a value. */ 7513 sym->st_shndx = SHN_ABS; 7514 sym->st_value = val; 7515 return; 7516 } 7517 BFD_ASSERT (elf_bad_symtab (bfd_with_globals)); 7518 extsymoff = 0; 7519 } 7520 7521 /* It is a global symbol: set its link type 7522 to "defined" and give it a value. */ 7523 7524 sym_hashes = elf_sym_hashes (bfd_with_globals); 7525 h = sym_hashes [symidx - extsymoff]; 7526 while (h->root.type == bfd_link_hash_indirect 7527 || h->root.type == bfd_link_hash_warning) 7528 h = (struct elf_link_hash_entry *) h->root.u.i.link; 7529 h->root.type = bfd_link_hash_defined; 7530 h->root.u.def.value = val; 7531 h->root.u.def.section = bfd_abs_section_ptr; 7532 } 7533 7534 static bfd_boolean 7535 resolve_symbol (const char *name, 7536 bfd *input_bfd, 7537 struct elf_final_link_info *flinfo, 7538 bfd_vma *result, 7539 Elf_Internal_Sym *isymbuf, 7540 size_t locsymcount) 7541 { 7542 Elf_Internal_Sym *sym; 7543 struct bfd_link_hash_entry *global_entry; 7544 const char *candidate = NULL; 7545 Elf_Internal_Shdr *symtab_hdr; 7546 size_t i; 7547 7548 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 7549 7550 for (i = 0; i < locsymcount; ++ i) 7551 { 7552 sym = isymbuf + i; 7553 7554 if (ELF_ST_BIND (sym->st_info) != STB_LOCAL) 7555 continue; 7556 7557 candidate = bfd_elf_string_from_elf_section (input_bfd, 7558 symtab_hdr->sh_link, 7559 sym->st_name); 7560 #ifdef DEBUG 7561 printf ("Comparing string: '%s' vs. '%s' = 0x%lx\n", 7562 name, candidate, (unsigned long) sym->st_value); 7563 #endif 7564 if (candidate && strcmp (candidate, name) == 0) 7565 { 7566 asection *sec = flinfo->sections [i]; 7567 7568 *result = _bfd_elf_rel_local_sym (input_bfd, sym, &sec, 0); 7569 *result += sec->output_offset + sec->output_section->vma; 7570 #ifdef DEBUG 7571 printf ("Found symbol with value %8.8lx\n", 7572 (unsigned long) *result); 7573 #endif 7574 return TRUE; 7575 } 7576 } 7577 7578 /* Hmm, haven't found it yet. perhaps it is a global. */ 7579 global_entry = bfd_link_hash_lookup (flinfo->info->hash, name, 7580 FALSE, FALSE, TRUE); 7581 if (!global_entry) 7582 return FALSE; 7583 7584 if (global_entry->type == bfd_link_hash_defined 7585 || global_entry->type == bfd_link_hash_defweak) 7586 { 7587 *result = (global_entry->u.def.value 7588 + global_entry->u.def.section->output_section->vma 7589 + global_entry->u.def.section->output_offset); 7590 #ifdef DEBUG 7591 printf ("Found GLOBAL symbol '%s' with value %8.8lx\n", 7592 global_entry->root.string, (unsigned long) *result); 7593 #endif 7594 return TRUE; 7595 } 7596 7597 return FALSE; 7598 } 7599 7600 static bfd_boolean 7601 resolve_section (const char *name, 7602 asection *sections, 7603 bfd_vma *result) 7604 { 7605 asection *curr; 7606 unsigned int len; 7607 7608 for (curr = sections; curr; curr = curr->next) 7609 if (strcmp (curr->name, name) == 0) 7610 { 7611 *result = curr->vma; 7612 return TRUE; 7613 } 7614 7615 /* Hmm. still haven't found it. try pseudo-section names. */ 7616 for (curr = sections; curr; curr = curr->next) 7617 { 7618 len = strlen (curr->name); 7619 if (len > strlen (name)) 7620 continue; 7621 7622 if (strncmp (curr->name, name, len) == 0) 7623 { 7624 if (strncmp (".end", name + len, 4) == 0) 7625 { 7626 *result = curr->vma + curr->size; 7627 return TRUE; 7628 } 7629 7630 /* Insert more pseudo-section names here, if you like. */ 7631 } 7632 } 7633 7634 return FALSE; 7635 } 7636 7637 static void 7638 undefined_reference (const char *reftype, const char *name) 7639 { 7640 _bfd_error_handler (_("undefined %s reference in complex symbol: %s"), 7641 reftype, name); 7642 } 7643 7644 static bfd_boolean 7645 eval_symbol (bfd_vma *result, 7646 const char **symp, 7647 bfd *input_bfd, 7648 struct elf_final_link_info *flinfo, 7649 bfd_vma dot, 7650 Elf_Internal_Sym *isymbuf, 7651 size_t locsymcount, 7652 int signed_p) 7653 { 7654 size_t len; 7655 size_t symlen; 7656 bfd_vma a; 7657 bfd_vma b; 7658 char symbuf[4096]; 7659 const char *sym = *symp; 7660 const char *symend; 7661 bfd_boolean symbol_is_section = FALSE; 7662 7663 len = strlen (sym); 7664 symend = sym + len; 7665 7666 if (len < 1 || len > sizeof (symbuf)) 7667 { 7668 bfd_set_error (bfd_error_invalid_operation); 7669 return FALSE; 7670 } 7671 7672 switch (* sym) 7673 { 7674 case '.': 7675 *result = dot; 7676 *symp = sym + 1; 7677 return TRUE; 7678 7679 case '#': 7680 ++sym; 7681 *result = strtoul (sym, (char **) symp, 16); 7682 return TRUE; 7683 7684 case 'S': 7685 symbol_is_section = TRUE; 7686 case 's': 7687 ++sym; 7688 symlen = strtol (sym, (char **) symp, 10); 7689 sym = *symp + 1; /* Skip the trailing ':'. */ 7690 7691 if (symend < sym || symlen + 1 > sizeof (symbuf)) 7692 { 7693 bfd_set_error (bfd_error_invalid_operation); 7694 return FALSE; 7695 } 7696 7697 memcpy (symbuf, sym, symlen); 7698 symbuf[symlen] = '\0'; 7699 *symp = sym + symlen; 7700 7701 /* Is it always possible, with complex symbols, that gas "mis-guessed" 7702 the symbol as a section, or vice-versa. so we're pretty liberal in our 7703 interpretation here; section means "try section first", not "must be a 7704 section", and likewise with symbol. */ 7705 7706 if (symbol_is_section) 7707 { 7708 if (!resolve_section (symbuf, flinfo->output_bfd->sections, result) 7709 && !resolve_symbol (symbuf, input_bfd, flinfo, result, 7710 isymbuf, locsymcount)) 7711 { 7712 undefined_reference ("section", symbuf); 7713 return FALSE; 7714 } 7715 } 7716 else 7717 { 7718 if (!resolve_symbol (symbuf, input_bfd, flinfo, result, 7719 isymbuf, locsymcount) 7720 && !resolve_section (symbuf, flinfo->output_bfd->sections, 7721 result)) 7722 { 7723 undefined_reference ("symbol", symbuf); 7724 return FALSE; 7725 } 7726 } 7727 7728 return TRUE; 7729 7730 /* All that remains are operators. */ 7731 7732 #define UNARY_OP(op) \ 7733 if (strncmp (sym, #op, strlen (#op)) == 0) \ 7734 { \ 7735 sym += strlen (#op); \ 7736 if (*sym == ':') \ 7737 ++sym; \ 7738 *symp = sym; \ 7739 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 7740 isymbuf, locsymcount, signed_p)) \ 7741 return FALSE; \ 7742 if (signed_p) \ 7743 *result = op ((bfd_signed_vma) a); \ 7744 else \ 7745 *result = op a; \ 7746 return TRUE; \ 7747 } 7748 7749 #define BINARY_OP(op) \ 7750 if (strncmp (sym, #op, strlen (#op)) == 0) \ 7751 { \ 7752 sym += strlen (#op); \ 7753 if (*sym == ':') \ 7754 ++sym; \ 7755 *symp = sym; \ 7756 if (!eval_symbol (&a, symp, input_bfd, flinfo, dot, \ 7757 isymbuf, locsymcount, signed_p)) \ 7758 return FALSE; \ 7759 ++*symp; \ 7760 if (!eval_symbol (&b, symp, input_bfd, flinfo, dot, \ 7761 isymbuf, locsymcount, signed_p)) \ 7762 return FALSE; \ 7763 if (signed_p) \ 7764 *result = ((bfd_signed_vma) a) op ((bfd_signed_vma) b); \ 7765 else \ 7766 *result = a op b; \ 7767 return TRUE; \ 7768 } 7769 7770 default: 7771 UNARY_OP (0-); 7772 BINARY_OP (<<); 7773 BINARY_OP (>>); 7774 BINARY_OP (==); 7775 BINARY_OP (!=); 7776 BINARY_OP (<=); 7777 BINARY_OP (>=); 7778 BINARY_OP (&&); 7779 BINARY_OP (||); 7780 UNARY_OP (~); 7781 UNARY_OP (!); 7782 BINARY_OP (*); 7783 BINARY_OP (/); 7784 BINARY_OP (%); 7785 BINARY_OP (^); 7786 BINARY_OP (|); 7787 BINARY_OP (&); 7788 BINARY_OP (+); 7789 BINARY_OP (-); 7790 BINARY_OP (<); 7791 BINARY_OP (>); 7792 #undef UNARY_OP 7793 #undef BINARY_OP 7794 _bfd_error_handler (_("unknown operator '%c' in complex symbol"), * sym); 7795 bfd_set_error (bfd_error_invalid_operation); 7796 return FALSE; 7797 } 7798 } 7799 7800 static void 7801 put_value (bfd_vma size, 7802 unsigned long chunksz, 7803 bfd *input_bfd, 7804 bfd_vma x, 7805 bfd_byte *location) 7806 { 7807 location += (size - chunksz); 7808 7809 for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8)) 7810 { 7811 switch (chunksz) 7812 { 7813 default: 7814 case 0: 7815 abort (); 7816 case 1: 7817 bfd_put_8 (input_bfd, x, location); 7818 break; 7819 case 2: 7820 bfd_put_16 (input_bfd, x, location); 7821 break; 7822 case 4: 7823 bfd_put_32 (input_bfd, x, location); 7824 break; 7825 case 8: 7826 #ifdef BFD64 7827 bfd_put_64 (input_bfd, x, location); 7828 #else 7829 abort (); 7830 #endif 7831 break; 7832 } 7833 } 7834 } 7835 7836 static bfd_vma 7837 get_value (bfd_vma size, 7838 unsigned long chunksz, 7839 bfd *input_bfd, 7840 bfd_byte *location) 7841 { 7842 bfd_vma x = 0; 7843 7844 for (; size; size -= chunksz, location += chunksz) 7845 { 7846 switch (chunksz) 7847 { 7848 default: 7849 case 0: 7850 abort (); 7851 case 1: 7852 x = (x << (8 * chunksz)) | bfd_get_8 (input_bfd, location); 7853 break; 7854 case 2: 7855 x = (x << (8 * chunksz)) | bfd_get_16 (input_bfd, location); 7856 break; 7857 case 4: 7858 x = (x << (8 * chunksz)) | bfd_get_32 (input_bfd, location); 7859 break; 7860 case 8: 7861 #ifdef BFD64 7862 x = (x << (8 * chunksz)) | bfd_get_64 (input_bfd, location); 7863 #else 7864 abort (); 7865 #endif 7866 break; 7867 } 7868 } 7869 return x; 7870 } 7871 7872 static void 7873 decode_complex_addend (unsigned long *start, /* in bits */ 7874 unsigned long *oplen, /* in bits */ 7875 unsigned long *len, /* in bits */ 7876 unsigned long *wordsz, /* in bytes */ 7877 unsigned long *chunksz, /* in bytes */ 7878 unsigned long *lsb0_p, 7879 unsigned long *signed_p, 7880 unsigned long *trunc_p, 7881 unsigned long encoded) 7882 { 7883 * start = encoded & 0x3F; 7884 * len = (encoded >> 6) & 0x3F; 7885 * oplen = (encoded >> 12) & 0x3F; 7886 * wordsz = (encoded >> 18) & 0xF; 7887 * chunksz = (encoded >> 22) & 0xF; 7888 * lsb0_p = (encoded >> 27) & 1; 7889 * signed_p = (encoded >> 28) & 1; 7890 * trunc_p = (encoded >> 29) & 1; 7891 } 7892 7893 bfd_reloc_status_type 7894 bfd_elf_perform_complex_relocation (bfd *input_bfd, 7895 asection *input_section ATTRIBUTE_UNUSED, 7896 bfd_byte *contents, 7897 Elf_Internal_Rela *rel, 7898 bfd_vma relocation) 7899 { 7900 bfd_vma shift, x, mask; 7901 unsigned long start, oplen, len, wordsz, chunksz, lsb0_p, signed_p, trunc_p; 7902 bfd_reloc_status_type r; 7903 7904 /* Perform this reloc, since it is complex. 7905 (this is not to say that it necessarily refers to a complex 7906 symbol; merely that it is a self-describing CGEN based reloc. 7907 i.e. the addend has the complete reloc information (bit start, end, 7908 word size, etc) encoded within it.). */ 7909 7910 decode_complex_addend (&start, &oplen, &len, &wordsz, 7911 &chunksz, &lsb0_p, &signed_p, 7912 &trunc_p, rel->r_addend); 7913 7914 mask = (((1L << (len - 1)) - 1) << 1) | 1; 7915 7916 if (lsb0_p) 7917 shift = (start + 1) - len; 7918 else 7919 shift = (8 * wordsz) - (start + len); 7920 7921 /* FIXME: octets_per_byte. */ 7922 x = get_value (wordsz, chunksz, input_bfd, contents + rel->r_offset); 7923 7924 #ifdef DEBUG 7925 printf ("Doing complex reloc: " 7926 "lsb0? %ld, signed? %ld, trunc? %ld, wordsz %ld, " 7927 "chunksz %ld, start %ld, len %ld, oplen %ld\n" 7928 " dest: %8.8lx, mask: %8.8lx, reloc: %8.8lx\n", 7929 lsb0_p, signed_p, trunc_p, wordsz, chunksz, start, len, 7930 oplen, (unsigned long) x, (unsigned long) mask, 7931 (unsigned long) relocation); 7932 #endif 7933 7934 r = bfd_reloc_ok; 7935 if (! trunc_p) 7936 /* Now do an overflow check. */ 7937 r = bfd_check_overflow ((signed_p 7938 ? complain_overflow_signed 7939 : complain_overflow_unsigned), 7940 len, 0, (8 * wordsz), 7941 relocation); 7942 7943 /* Do the deed. */ 7944 x = (x & ~(mask << shift)) | ((relocation & mask) << shift); 7945 7946 #ifdef DEBUG 7947 printf (" relocation: %8.8lx\n" 7948 " shifted mask: %8.8lx\n" 7949 " shifted/masked reloc: %8.8lx\n" 7950 " result: %8.8lx\n", 7951 (unsigned long) relocation, (unsigned long) (mask << shift), 7952 (unsigned long) ((relocation & mask) << shift), (unsigned long) x); 7953 #endif 7954 /* FIXME: octets_per_byte. */ 7955 put_value (wordsz, chunksz, input_bfd, x, contents + rel->r_offset); 7956 return r; 7957 } 7958 7959 /* When performing a relocatable link, the input relocations are 7960 preserved. But, if they reference global symbols, the indices 7961 referenced must be updated. Update all the relocations found in 7962 RELDATA. */ 7963 7964 static void 7965 elf_link_adjust_relocs (bfd *abfd, 7966 struct bfd_elf_section_reloc_data *reldata) 7967 { 7968 unsigned int i; 7969 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 7970 bfd_byte *erela; 7971 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 7972 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 7973 bfd_vma r_type_mask; 7974 int r_sym_shift; 7975 unsigned int count = reldata->count; 7976 struct elf_link_hash_entry **rel_hash = reldata->hashes; 7977 7978 if (reldata->hdr->sh_entsize == bed->s->sizeof_rel) 7979 { 7980 swap_in = bed->s->swap_reloc_in; 7981 swap_out = bed->s->swap_reloc_out; 7982 } 7983 else if (reldata->hdr->sh_entsize == bed->s->sizeof_rela) 7984 { 7985 swap_in = bed->s->swap_reloca_in; 7986 swap_out = bed->s->swap_reloca_out; 7987 } 7988 else 7989 abort (); 7990 7991 if (bed->s->int_rels_per_ext_rel > MAX_INT_RELS_PER_EXT_REL) 7992 abort (); 7993 7994 if (bed->s->arch_size == 32) 7995 { 7996 r_type_mask = 0xff; 7997 r_sym_shift = 8; 7998 } 7999 else 8000 { 8001 r_type_mask = 0xffffffff; 8002 r_sym_shift = 32; 8003 } 8004 8005 erela = reldata->hdr->contents; 8006 for (i = 0; i < count; i++, rel_hash++, erela += reldata->hdr->sh_entsize) 8007 { 8008 Elf_Internal_Rela irela[MAX_INT_RELS_PER_EXT_REL]; 8009 unsigned int j; 8010 8011 if (*rel_hash == NULL) 8012 continue; 8013 8014 BFD_ASSERT ((*rel_hash)->indx >= 0); 8015 8016 (*swap_in) (abfd, erela, irela); 8017 for (j = 0; j < bed->s->int_rels_per_ext_rel; j++) 8018 irela[j].r_info = ((bfd_vma) (*rel_hash)->indx << r_sym_shift 8019 | (irela[j].r_info & r_type_mask)); 8020 (*swap_out) (abfd, irela, erela); 8021 } 8022 } 8023 8024 struct elf_link_sort_rela 8025 { 8026 union { 8027 bfd_vma offset; 8028 bfd_vma sym_mask; 8029 } u; 8030 enum elf_reloc_type_class type; 8031 /* We use this as an array of size int_rels_per_ext_rel. */ 8032 Elf_Internal_Rela rela[1]; 8033 }; 8034 8035 static int 8036 elf_link_sort_cmp1 (const void *A, const void *B) 8037 { 8038 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8039 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8040 int relativea, relativeb; 8041 8042 relativea = a->type == reloc_class_relative; 8043 relativeb = b->type == reloc_class_relative; 8044 8045 if (relativea < relativeb) 8046 return 1; 8047 if (relativea > relativeb) 8048 return -1; 8049 if ((a->rela->r_info & a->u.sym_mask) < (b->rela->r_info & b->u.sym_mask)) 8050 return -1; 8051 if ((a->rela->r_info & a->u.sym_mask) > (b->rela->r_info & b->u.sym_mask)) 8052 return 1; 8053 if (a->rela->r_offset < b->rela->r_offset) 8054 return -1; 8055 if (a->rela->r_offset > b->rela->r_offset) 8056 return 1; 8057 return 0; 8058 } 8059 8060 static int 8061 elf_link_sort_cmp2 (const void *A, const void *B) 8062 { 8063 const struct elf_link_sort_rela *a = (const struct elf_link_sort_rela *) A; 8064 const struct elf_link_sort_rela *b = (const struct elf_link_sort_rela *) B; 8065 int copya, copyb; 8066 8067 if (a->u.offset < b->u.offset) 8068 return -1; 8069 if (a->u.offset > b->u.offset) 8070 return 1; 8071 copya = (a->type == reloc_class_copy) * 2 + (a->type == reloc_class_plt); 8072 copyb = (b->type == reloc_class_copy) * 2 + (b->type == reloc_class_plt); 8073 if (copya < copyb) 8074 return -1; 8075 if (copya > copyb) 8076 return 1; 8077 if (a->rela->r_offset < b->rela->r_offset) 8078 return -1; 8079 if (a->rela->r_offset > b->rela->r_offset) 8080 return 1; 8081 return 0; 8082 } 8083 8084 static size_t 8085 elf_link_sort_relocs (bfd *abfd, struct bfd_link_info *info, asection **psec) 8086 { 8087 asection *dynamic_relocs; 8088 asection *rela_dyn; 8089 asection *rel_dyn; 8090 bfd_size_type count, size; 8091 size_t i, ret, sort_elt, ext_size; 8092 bfd_byte *sort, *s_non_relative, *p; 8093 struct elf_link_sort_rela *sq; 8094 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 8095 int i2e = bed->s->int_rels_per_ext_rel; 8096 void (*swap_in) (bfd *, const bfd_byte *, Elf_Internal_Rela *); 8097 void (*swap_out) (bfd *, const Elf_Internal_Rela *, bfd_byte *); 8098 struct bfd_link_order *lo; 8099 bfd_vma r_sym_mask; 8100 bfd_boolean use_rela; 8101 8102 /* Find a dynamic reloc section. */ 8103 rela_dyn = bfd_get_section_by_name (abfd, ".rela.dyn"); 8104 rel_dyn = bfd_get_section_by_name (abfd, ".rel.dyn"); 8105 if (rela_dyn != NULL && rela_dyn->size > 0 8106 && rel_dyn != NULL && rel_dyn->size > 0) 8107 { 8108 bfd_boolean use_rela_initialised = FALSE; 8109 8110 /* This is just here to stop gcc from complaining. 8111 It's initialization checking code is not perfect. */ 8112 use_rela = TRUE; 8113 8114 /* Both sections are present. Examine the sizes 8115 of the indirect sections to help us choose. */ 8116 for (lo = rela_dyn->map_head.link_order; lo != NULL; lo = lo->next) 8117 if (lo->type == bfd_indirect_link_order) 8118 { 8119 asection *o = lo->u.indirect.section; 8120 8121 if ((o->size % bed->s->sizeof_rela) == 0) 8122 { 8123 if ((o->size % bed->s->sizeof_rel) == 0) 8124 /* Section size is divisible by both rel and rela sizes. 8125 It is of no help to us. */ 8126 ; 8127 else 8128 { 8129 /* Section size is only divisible by rela. */ 8130 if (use_rela_initialised && (use_rela == FALSE)) 8131 { 8132 _bfd_error_handler 8133 (_("%B: Unable to sort relocs - they are in more than one size"), abfd); 8134 bfd_set_error (bfd_error_invalid_operation); 8135 return 0; 8136 } 8137 else 8138 { 8139 use_rela = TRUE; 8140 use_rela_initialised = TRUE; 8141 } 8142 } 8143 } 8144 else if ((o->size % bed->s->sizeof_rel) == 0) 8145 { 8146 /* Section size is only divisible by rel. */ 8147 if (use_rela_initialised && (use_rela == TRUE)) 8148 { 8149 _bfd_error_handler 8150 (_("%B: Unable to sort relocs - they are in more than one size"), abfd); 8151 bfd_set_error (bfd_error_invalid_operation); 8152 return 0; 8153 } 8154 else 8155 { 8156 use_rela = FALSE; 8157 use_rela_initialised = TRUE; 8158 } 8159 } 8160 else 8161 { 8162 /* The section size is not divisible by either - something is wrong. */ 8163 _bfd_error_handler 8164 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); 8165 bfd_set_error (bfd_error_invalid_operation); 8166 return 0; 8167 } 8168 } 8169 8170 for (lo = rel_dyn->map_head.link_order; lo != NULL; lo = lo->next) 8171 if (lo->type == bfd_indirect_link_order) 8172 { 8173 asection *o = lo->u.indirect.section; 8174 8175 if ((o->size % bed->s->sizeof_rela) == 0) 8176 { 8177 if ((o->size % bed->s->sizeof_rel) == 0) 8178 /* Section size is divisible by both rel and rela sizes. 8179 It is of no help to us. */ 8180 ; 8181 else 8182 { 8183 /* Section size is only divisible by rela. */ 8184 if (use_rela_initialised && (use_rela == FALSE)) 8185 { 8186 _bfd_error_handler 8187 (_("%B: Unable to sort relocs - they are in more than one size"), abfd); 8188 bfd_set_error (bfd_error_invalid_operation); 8189 return 0; 8190 } 8191 else 8192 { 8193 use_rela = TRUE; 8194 use_rela_initialised = TRUE; 8195 } 8196 } 8197 } 8198 else if ((o->size % bed->s->sizeof_rel) == 0) 8199 { 8200 /* Section size is only divisible by rel. */ 8201 if (use_rela_initialised && (use_rela == TRUE)) 8202 { 8203 _bfd_error_handler 8204 (_("%B: Unable to sort relocs - they are in more than one size"), abfd); 8205 bfd_set_error (bfd_error_invalid_operation); 8206 return 0; 8207 } 8208 else 8209 { 8210 use_rela = FALSE; 8211 use_rela_initialised = TRUE; 8212 } 8213 } 8214 else 8215 { 8216 /* The section size is not divisible by either - something is wrong. */ 8217 _bfd_error_handler 8218 (_("%B: Unable to sort relocs - they are of an unknown size"), abfd); 8219 bfd_set_error (bfd_error_invalid_operation); 8220 return 0; 8221 } 8222 } 8223 8224 if (! use_rela_initialised) 8225 /* Make a guess. */ 8226 use_rela = TRUE; 8227 } 8228 else if (rela_dyn != NULL && rela_dyn->size > 0) 8229 use_rela = TRUE; 8230 else if (rel_dyn != NULL && rel_dyn->size > 0) 8231 use_rela = FALSE; 8232 else 8233 return 0; 8234 8235 if (use_rela) 8236 { 8237 dynamic_relocs = rela_dyn; 8238 ext_size = bed->s->sizeof_rela; 8239 swap_in = bed->s->swap_reloca_in; 8240 swap_out = bed->s->swap_reloca_out; 8241 } 8242 else 8243 { 8244 dynamic_relocs = rel_dyn; 8245 ext_size = bed->s->sizeof_rel; 8246 swap_in = bed->s->swap_reloc_in; 8247 swap_out = bed->s->swap_reloc_out; 8248 } 8249 8250 size = 0; 8251 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 8252 if (lo->type == bfd_indirect_link_order) 8253 size += lo->u.indirect.section->size; 8254 8255 if (size != dynamic_relocs->size) 8256 return 0; 8257 8258 sort_elt = (sizeof (struct elf_link_sort_rela) 8259 + (i2e - 1) * sizeof (Elf_Internal_Rela)); 8260 8261 count = dynamic_relocs->size / ext_size; 8262 if (count == 0) 8263 return 0; 8264 sort = (bfd_byte *) bfd_zmalloc (sort_elt * count); 8265 8266 if (sort == NULL) 8267 { 8268 (*info->callbacks->warning) 8269 (info, _("Not enough memory to sort relocations"), 0, abfd, 0, 0); 8270 return 0; 8271 } 8272 8273 if (bed->s->arch_size == 32) 8274 r_sym_mask = ~(bfd_vma) 0xff; 8275 else 8276 r_sym_mask = ~(bfd_vma) 0xffffffff; 8277 8278 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 8279 if (lo->type == bfd_indirect_link_order) 8280 { 8281 bfd_byte *erel, *erelend; 8282 asection *o = lo->u.indirect.section; 8283 8284 if (o->contents == NULL && o->size != 0) 8285 { 8286 /* This is a reloc section that is being handled as a normal 8287 section. See bfd_section_from_shdr. We can't combine 8288 relocs in this case. */ 8289 free (sort); 8290 return 0; 8291 } 8292 erel = o->contents; 8293 erelend = o->contents + o->size; 8294 /* FIXME: octets_per_byte. */ 8295 p = sort + o->output_offset / ext_size * sort_elt; 8296 8297 while (erel < erelend) 8298 { 8299 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 8300 8301 (*swap_in) (abfd, erel, s->rela); 8302 s->type = (*bed->elf_backend_reloc_type_class) (s->rela); 8303 s->u.sym_mask = r_sym_mask; 8304 p += sort_elt; 8305 erel += ext_size; 8306 } 8307 } 8308 8309 qsort (sort, count, sort_elt, elf_link_sort_cmp1); 8310 8311 for (i = 0, p = sort; i < count; i++, p += sort_elt) 8312 { 8313 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 8314 if (s->type != reloc_class_relative) 8315 break; 8316 } 8317 ret = i; 8318 s_non_relative = p; 8319 8320 sq = (struct elf_link_sort_rela *) s_non_relative; 8321 for (; i < count; i++, p += sort_elt) 8322 { 8323 struct elf_link_sort_rela *sp = (struct elf_link_sort_rela *) p; 8324 if (((sp->rela->r_info ^ sq->rela->r_info) & r_sym_mask) != 0) 8325 sq = sp; 8326 sp->u.offset = sq->rela->r_offset; 8327 } 8328 8329 qsort (s_non_relative, count - ret, sort_elt, elf_link_sort_cmp2); 8330 8331 for (lo = dynamic_relocs->map_head.link_order; lo != NULL; lo = lo->next) 8332 if (lo->type == bfd_indirect_link_order) 8333 { 8334 bfd_byte *erel, *erelend; 8335 asection *o = lo->u.indirect.section; 8336 8337 erel = o->contents; 8338 erelend = o->contents + o->size; 8339 /* FIXME: octets_per_byte. */ 8340 p = sort + o->output_offset / ext_size * sort_elt; 8341 while (erel < erelend) 8342 { 8343 struct elf_link_sort_rela *s = (struct elf_link_sort_rela *) p; 8344 (*swap_out) (abfd, s->rela, erel); 8345 p += sort_elt; 8346 erel += ext_size; 8347 } 8348 } 8349 8350 free (sort); 8351 *psec = dynamic_relocs; 8352 return ret; 8353 } 8354 8355 /* Flush the output symbols to the file. */ 8356 8357 static bfd_boolean 8358 elf_link_flush_output_syms (struct elf_final_link_info *flinfo, 8359 const struct elf_backend_data *bed) 8360 { 8361 if (flinfo->symbuf_count > 0) 8362 { 8363 Elf_Internal_Shdr *hdr; 8364 file_ptr pos; 8365 bfd_size_type amt; 8366 8367 hdr = &elf_tdata (flinfo->output_bfd)->symtab_hdr; 8368 pos = hdr->sh_offset + hdr->sh_size; 8369 amt = flinfo->symbuf_count * bed->s->sizeof_sym; 8370 if (bfd_seek (flinfo->output_bfd, pos, SEEK_SET) != 0 8371 || bfd_bwrite (flinfo->symbuf, amt, flinfo->output_bfd) != amt) 8372 return FALSE; 8373 8374 hdr->sh_size += amt; 8375 flinfo->symbuf_count = 0; 8376 } 8377 8378 return TRUE; 8379 } 8380 8381 /* Add a symbol to the output symbol table. */ 8382 8383 static int 8384 elf_link_output_sym (struct elf_final_link_info *flinfo, 8385 const char *name, 8386 Elf_Internal_Sym *elfsym, 8387 asection *input_sec, 8388 struct elf_link_hash_entry *h) 8389 { 8390 bfd_byte *dest; 8391 Elf_External_Sym_Shndx *destshndx; 8392 int (*output_symbol_hook) 8393 (struct bfd_link_info *, const char *, Elf_Internal_Sym *, asection *, 8394 struct elf_link_hash_entry *); 8395 const struct elf_backend_data *bed; 8396 8397 bed = get_elf_backend_data (flinfo->output_bfd); 8398 output_symbol_hook = bed->elf_backend_link_output_symbol_hook; 8399 if (output_symbol_hook != NULL) 8400 { 8401 int ret = (*output_symbol_hook) (flinfo->info, name, elfsym, input_sec, h); 8402 if (ret != 1) 8403 return ret; 8404 } 8405 8406 if (name == NULL || *name == '\0') 8407 elfsym->st_name = 0; 8408 else if (input_sec->flags & SEC_EXCLUDE) 8409 elfsym->st_name = 0; 8410 else 8411 { 8412 elfsym->st_name = (unsigned long) _bfd_stringtab_add (flinfo->symstrtab, 8413 name, TRUE, FALSE); 8414 if (elfsym->st_name == (unsigned long) -1) 8415 return 0; 8416 } 8417 8418 if (flinfo->symbuf_count >= flinfo->symbuf_size) 8419 { 8420 if (! elf_link_flush_output_syms (flinfo, bed)) 8421 return 0; 8422 } 8423 8424 dest = flinfo->symbuf + flinfo->symbuf_count * bed->s->sizeof_sym; 8425 destshndx = flinfo->symshndxbuf; 8426 if (destshndx != NULL) 8427 { 8428 if (bfd_get_symcount (flinfo->output_bfd) >= flinfo->shndxbuf_size) 8429 { 8430 bfd_size_type amt; 8431 8432 amt = flinfo->shndxbuf_size * sizeof (Elf_External_Sym_Shndx); 8433 destshndx = (Elf_External_Sym_Shndx *) bfd_realloc (destshndx, 8434 amt * 2); 8435 if (destshndx == NULL) 8436 return 0; 8437 flinfo->symshndxbuf = destshndx; 8438 memset ((char *) destshndx + amt, 0, amt); 8439 flinfo->shndxbuf_size *= 2; 8440 } 8441 destshndx += bfd_get_symcount (flinfo->output_bfd); 8442 } 8443 8444 bed->s->swap_symbol_out (flinfo->output_bfd, elfsym, dest, destshndx); 8445 flinfo->symbuf_count += 1; 8446 bfd_get_symcount (flinfo->output_bfd) += 1; 8447 8448 return 1; 8449 } 8450 8451 /* Return TRUE if the dynamic symbol SYM in ABFD is supported. */ 8452 8453 static bfd_boolean 8454 check_dynsym (bfd *abfd, Elf_Internal_Sym *sym) 8455 { 8456 if (sym->st_shndx >= (SHN_LORESERVE & 0xffff) 8457 && sym->st_shndx < SHN_LORESERVE) 8458 { 8459 /* The gABI doesn't support dynamic symbols in output sections 8460 beyond 64k. */ 8461 (*_bfd_error_handler) 8462 (_("%B: Too many sections: %d (>= %d)"), 8463 abfd, bfd_count_sections (abfd), SHN_LORESERVE & 0xffff); 8464 bfd_set_error (bfd_error_nonrepresentable_section); 8465 return FALSE; 8466 } 8467 return TRUE; 8468 } 8469 8470 /* For DSOs loaded in via a DT_NEEDED entry, emulate ld.so in 8471 allowing an unsatisfied unversioned symbol in the DSO to match a 8472 versioned symbol that would normally require an explicit version. 8473 We also handle the case that a DSO references a hidden symbol 8474 which may be satisfied by a versioned symbol in another DSO. */ 8475 8476 static bfd_boolean 8477 elf_link_check_versioned_symbol (struct bfd_link_info *info, 8478 const struct elf_backend_data *bed, 8479 struct elf_link_hash_entry *h) 8480 { 8481 bfd *abfd; 8482 struct elf_link_loaded_list *loaded; 8483 8484 if (!is_elf_hash_table (info->hash)) 8485 return FALSE; 8486 8487 /* Check indirect symbol. */ 8488 while (h->root.type == bfd_link_hash_indirect) 8489 h = (struct elf_link_hash_entry *) h->root.u.i.link; 8490 8491 switch (h->root.type) 8492 { 8493 default: 8494 abfd = NULL; 8495 break; 8496 8497 case bfd_link_hash_undefined: 8498 case bfd_link_hash_undefweak: 8499 abfd = h->root.u.undef.abfd; 8500 if ((abfd->flags & DYNAMIC) == 0 8501 || (elf_dyn_lib_class (abfd) & DYN_DT_NEEDED) == 0) 8502 return FALSE; 8503 break; 8504 8505 case bfd_link_hash_defined: 8506 case bfd_link_hash_defweak: 8507 abfd = h->root.u.def.section->owner; 8508 break; 8509 8510 case bfd_link_hash_common: 8511 abfd = h->root.u.c.p->section->owner; 8512 break; 8513 } 8514 BFD_ASSERT (abfd != NULL); 8515 8516 for (loaded = elf_hash_table (info)->loaded; 8517 loaded != NULL; 8518 loaded = loaded->next) 8519 { 8520 bfd *input; 8521 Elf_Internal_Shdr *hdr; 8522 bfd_size_type symcount; 8523 bfd_size_type extsymcount; 8524 bfd_size_type extsymoff; 8525 Elf_Internal_Shdr *versymhdr; 8526 Elf_Internal_Sym *isym; 8527 Elf_Internal_Sym *isymend; 8528 Elf_Internal_Sym *isymbuf; 8529 Elf_External_Versym *ever; 8530 Elf_External_Versym *extversym; 8531 8532 input = loaded->abfd; 8533 8534 /* We check each DSO for a possible hidden versioned definition. */ 8535 if (input == abfd 8536 || (input->flags & DYNAMIC) == 0 8537 || elf_dynversym (input) == 0) 8538 continue; 8539 8540 hdr = &elf_tdata (input)->dynsymtab_hdr; 8541 8542 symcount = hdr->sh_size / bed->s->sizeof_sym; 8543 if (elf_bad_symtab (input)) 8544 { 8545 extsymcount = symcount; 8546 extsymoff = 0; 8547 } 8548 else 8549 { 8550 extsymcount = symcount - hdr->sh_info; 8551 extsymoff = hdr->sh_info; 8552 } 8553 8554 if (extsymcount == 0) 8555 continue; 8556 8557 isymbuf = bfd_elf_get_elf_syms (input, hdr, extsymcount, extsymoff, 8558 NULL, NULL, NULL); 8559 if (isymbuf == NULL) 8560 return FALSE; 8561 8562 /* Read in any version definitions. */ 8563 versymhdr = &elf_tdata (input)->dynversym_hdr; 8564 extversym = (Elf_External_Versym *) bfd_malloc (versymhdr->sh_size); 8565 if (extversym == NULL) 8566 goto error_ret; 8567 8568 if (bfd_seek (input, versymhdr->sh_offset, SEEK_SET) != 0 8569 || (bfd_bread (extversym, versymhdr->sh_size, input) 8570 != versymhdr->sh_size)) 8571 { 8572 free (extversym); 8573 error_ret: 8574 free (isymbuf); 8575 return FALSE; 8576 } 8577 8578 ever = extversym + extsymoff; 8579 isymend = isymbuf + extsymcount; 8580 for (isym = isymbuf; isym < isymend; isym++, ever++) 8581 { 8582 const char *name; 8583 Elf_Internal_Versym iver; 8584 unsigned short version_index; 8585 8586 if (ELF_ST_BIND (isym->st_info) == STB_LOCAL 8587 || isym->st_shndx == SHN_UNDEF) 8588 continue; 8589 8590 name = bfd_elf_string_from_elf_section (input, 8591 hdr->sh_link, 8592 isym->st_name); 8593 if (strcmp (name, h->root.root.string) != 0) 8594 continue; 8595 8596 _bfd_elf_swap_versym_in (input, ever, &iver); 8597 8598 if ((iver.vs_vers & VERSYM_HIDDEN) == 0 8599 && !(h->def_regular 8600 && h->forced_local)) 8601 { 8602 /* If we have a non-hidden versioned sym, then it should 8603 have provided a definition for the undefined sym unless 8604 it is defined in a non-shared object and forced local. 8605 */ 8606 abort (); 8607 } 8608 8609 version_index = iver.vs_vers & VERSYM_VERSION; 8610 if (version_index == 1 || version_index == 2) 8611 { 8612 /* This is the base or first version. We can use it. */ 8613 free (extversym); 8614 free (isymbuf); 8615 return TRUE; 8616 } 8617 } 8618 8619 free (extversym); 8620 free (isymbuf); 8621 } 8622 8623 return FALSE; 8624 } 8625 8626 /* Add an external symbol to the symbol table. This is called from 8627 the hash table traversal routine. When generating a shared object, 8628 we go through the symbol table twice. The first time we output 8629 anything that might have been forced to local scope in a version 8630 script. The second time we output the symbols that are still 8631 global symbols. */ 8632 8633 static bfd_boolean 8634 elf_link_output_extsym (struct bfd_hash_entry *bh, void *data) 8635 { 8636 struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) bh; 8637 struct elf_outext_info *eoinfo = (struct elf_outext_info *) data; 8638 struct elf_final_link_info *flinfo = eoinfo->flinfo; 8639 bfd_boolean strip; 8640 Elf_Internal_Sym sym; 8641 asection *input_sec; 8642 const struct elf_backend_data *bed; 8643 long indx; 8644 int ret; 8645 8646 if (h->root.type == bfd_link_hash_warning) 8647 { 8648 h = (struct elf_link_hash_entry *) h->root.u.i.link; 8649 if (h->root.type == bfd_link_hash_new) 8650 return TRUE; 8651 } 8652 8653 /* Decide whether to output this symbol in this pass. */ 8654 if (eoinfo->localsyms) 8655 { 8656 if (!h->forced_local) 8657 return TRUE; 8658 if (eoinfo->second_pass 8659 && !((h->root.type == bfd_link_hash_defined 8660 || h->root.type == bfd_link_hash_defweak) 8661 && h->root.u.def.section->output_section != NULL)) 8662 return TRUE; 8663 } 8664 else 8665 { 8666 if (h->forced_local) 8667 return TRUE; 8668 } 8669 8670 bed = get_elf_backend_data (flinfo->output_bfd); 8671 8672 if (h->root.type == bfd_link_hash_undefined) 8673 { 8674 /* If we have an undefined symbol reference here then it must have 8675 come from a shared library that is being linked in. (Undefined 8676 references in regular files have already been handled unless 8677 they are in unreferenced sections which are removed by garbage 8678 collection). */ 8679 bfd_boolean ignore_undef = FALSE; 8680 8681 /* Some symbols may be special in that the fact that they're 8682 undefined can be safely ignored - let backend determine that. */ 8683 if (bed->elf_backend_ignore_undef_symbol) 8684 ignore_undef = bed->elf_backend_ignore_undef_symbol (h); 8685 8686 /* If we are reporting errors for this situation then do so now. */ 8687 if (!ignore_undef 8688 && h->ref_dynamic 8689 && (!h->ref_regular || flinfo->info->gc_sections) 8690 && !elf_link_check_versioned_symbol (flinfo->info, bed, h) 8691 && flinfo->info->unresolved_syms_in_shared_libs != RM_IGNORE) 8692 { 8693 if (!(flinfo->info->callbacks->undefined_symbol 8694 (flinfo->info, h->root.root.string, 8695 h->ref_regular ? NULL : h->root.u.undef.abfd, 8696 NULL, 0, 8697 (flinfo->info->unresolved_syms_in_shared_libs 8698 == RM_GENERATE_ERROR)))) 8699 { 8700 bfd_set_error (bfd_error_bad_value); 8701 eoinfo->failed = TRUE; 8702 return FALSE; 8703 } 8704 } 8705 } 8706 8707 /* We should also warn if a forced local symbol is referenced from 8708 shared libraries. */ 8709 if (!flinfo->info->relocatable 8710 && flinfo->info->executable 8711 && h->forced_local 8712 && h->ref_dynamic 8713 && h->def_regular 8714 && !h->dynamic_def 8715 && h->ref_dynamic_nonweak 8716 && !elf_link_check_versioned_symbol (flinfo->info, bed, h)) 8717 { 8718 bfd *def_bfd; 8719 const char *msg; 8720 struct elf_link_hash_entry *hi = h; 8721 8722 /* Check indirect symbol. */ 8723 while (hi->root.type == bfd_link_hash_indirect) 8724 hi = (struct elf_link_hash_entry *) hi->root.u.i.link; 8725 8726 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL) 8727 msg = _("%B: internal symbol `%s' in %B is referenced by DSO"); 8728 else if (ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) 8729 msg = _("%B: hidden symbol `%s' in %B is referenced by DSO"); 8730 else 8731 msg = _("%B: local symbol `%s' in %B is referenced by DSO"); 8732 def_bfd = flinfo->output_bfd; 8733 if (hi->root.u.def.section != bfd_abs_section_ptr) 8734 def_bfd = hi->root.u.def.section->owner; 8735 (*_bfd_error_handler) (msg, flinfo->output_bfd, def_bfd, 8736 h->root.root.string); 8737 bfd_set_error (bfd_error_bad_value); 8738 eoinfo->failed = TRUE; 8739 return FALSE; 8740 } 8741 8742 /* We don't want to output symbols that have never been mentioned by 8743 a regular file, or that we have been told to strip. However, if 8744 h->indx is set to -2, the symbol is used by a reloc and we must 8745 output it. */ 8746 if (h->indx == -2) 8747 strip = FALSE; 8748 else if ((h->def_dynamic 8749 || h->ref_dynamic 8750 || h->root.type == bfd_link_hash_new) 8751 && !h->def_regular 8752 && !h->ref_regular) 8753 strip = TRUE; 8754 else if (flinfo->info->strip == strip_all) 8755 strip = TRUE; 8756 else if (flinfo->info->strip == strip_some 8757 && bfd_hash_lookup (flinfo->info->keep_hash, 8758 h->root.root.string, FALSE, FALSE) == NULL) 8759 strip = TRUE; 8760 else if ((h->root.type == bfd_link_hash_defined 8761 || h->root.type == bfd_link_hash_defweak) 8762 && ((flinfo->info->strip_discarded 8763 && discarded_section (h->root.u.def.section)) 8764 || (h->root.u.def.section->owner != NULL 8765 && (h->root.u.def.section->owner->flags & BFD_PLUGIN) != 0))) 8766 strip = TRUE; 8767 else if ((h->root.type == bfd_link_hash_undefined 8768 || h->root.type == bfd_link_hash_undefweak) 8769 && h->root.u.undef.abfd != NULL 8770 && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0) 8771 strip = TRUE; 8772 else 8773 strip = FALSE; 8774 8775 /* If we're stripping it, and it's not a dynamic symbol, there's 8776 nothing else to do unless it is a forced local symbol or a 8777 STT_GNU_IFUNC symbol. */ 8778 if (strip 8779 && h->dynindx == -1 8780 && h->type != STT_GNU_IFUNC 8781 && !h->forced_local) 8782 return TRUE; 8783 8784 sym.st_value = 0; 8785 sym.st_size = h->size; 8786 sym.st_other = h->other; 8787 if (h->forced_local) 8788 { 8789 sym.st_info = ELF_ST_INFO (STB_LOCAL, h->type); 8790 /* Turn off visibility on local symbol. */ 8791 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 8792 } 8793 else if (h->unique_global) 8794 sym.st_info = ELF_ST_INFO (STB_GNU_UNIQUE, h->type); 8795 else if (h->root.type == bfd_link_hash_undefweak 8796 || h->root.type == bfd_link_hash_defweak) 8797 sym.st_info = ELF_ST_INFO (STB_WEAK, h->type); 8798 else 8799 sym.st_info = ELF_ST_INFO (STB_GLOBAL, h->type); 8800 sym.st_target_internal = h->target_internal; 8801 8802 switch (h->root.type) 8803 { 8804 default: 8805 case bfd_link_hash_new: 8806 case bfd_link_hash_warning: 8807 abort (); 8808 return FALSE; 8809 8810 case bfd_link_hash_undefined: 8811 case bfd_link_hash_undefweak: 8812 input_sec = bfd_und_section_ptr; 8813 sym.st_shndx = SHN_UNDEF; 8814 break; 8815 8816 case bfd_link_hash_defined: 8817 case bfd_link_hash_defweak: 8818 { 8819 input_sec = h->root.u.def.section; 8820 if (input_sec->output_section != NULL) 8821 { 8822 if (eoinfo->localsyms && flinfo->filesym_count == 1) 8823 { 8824 bfd_boolean second_pass_sym 8825 = (input_sec->owner == flinfo->output_bfd 8826 || input_sec->owner == NULL 8827 || (input_sec->flags & SEC_LINKER_CREATED) != 0 8828 || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0); 8829 8830 eoinfo->need_second_pass |= second_pass_sym; 8831 if (eoinfo->second_pass != second_pass_sym) 8832 return TRUE; 8833 } 8834 8835 sym.st_shndx = 8836 _bfd_elf_section_from_bfd_section (flinfo->output_bfd, 8837 input_sec->output_section); 8838 if (sym.st_shndx == SHN_BAD) 8839 { 8840 (*_bfd_error_handler) 8841 (_("%B: could not find output section %A for input section %A"), 8842 flinfo->output_bfd, input_sec->output_section, input_sec); 8843 bfd_set_error (bfd_error_nonrepresentable_section); 8844 eoinfo->failed = TRUE; 8845 return FALSE; 8846 } 8847 8848 /* ELF symbols in relocatable files are section relative, 8849 but in nonrelocatable files they are virtual 8850 addresses. */ 8851 sym.st_value = h->root.u.def.value + input_sec->output_offset; 8852 if (!flinfo->info->relocatable) 8853 { 8854 sym.st_value += input_sec->output_section->vma; 8855 if (h->type == STT_TLS) 8856 { 8857 asection *tls_sec = elf_hash_table (flinfo->info)->tls_sec; 8858 if (tls_sec != NULL) 8859 sym.st_value -= tls_sec->vma; 8860 else 8861 { 8862 /* The TLS section may have been garbage collected. */ 8863 BFD_ASSERT (flinfo->info->gc_sections 8864 && !input_sec->gc_mark); 8865 } 8866 } 8867 } 8868 } 8869 else 8870 { 8871 BFD_ASSERT (input_sec->owner == NULL 8872 || (input_sec->owner->flags & DYNAMIC) != 0); 8873 sym.st_shndx = SHN_UNDEF; 8874 input_sec = bfd_und_section_ptr; 8875 } 8876 } 8877 break; 8878 8879 case bfd_link_hash_common: 8880 input_sec = h->root.u.c.p->section; 8881 sym.st_shndx = bed->common_section_index (input_sec); 8882 sym.st_value = 1 << h->root.u.c.p->alignment_power; 8883 break; 8884 8885 case bfd_link_hash_indirect: 8886 /* These symbols are created by symbol versioning. They point 8887 to the decorated version of the name. For example, if the 8888 symbol foo@@GNU_1.2 is the default, which should be used when 8889 foo is used with no version, then we add an indirect symbol 8890 foo which points to foo@@GNU_1.2. We ignore these symbols, 8891 since the indirected symbol is already in the hash table. */ 8892 return TRUE; 8893 } 8894 8895 /* Give the processor backend a chance to tweak the symbol value, 8896 and also to finish up anything that needs to be done for this 8897 symbol. FIXME: Not calling elf_backend_finish_dynamic_symbol for 8898 forced local syms when non-shared is due to a historical quirk. 8899 STT_GNU_IFUNC symbol must go through PLT. */ 8900 if ((h->type == STT_GNU_IFUNC 8901 && h->def_regular 8902 && !flinfo->info->relocatable) 8903 || ((h->dynindx != -1 8904 || h->forced_local) 8905 && ((flinfo->info->shared 8906 && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 8907 || h->root.type != bfd_link_hash_undefweak)) 8908 || !h->forced_local) 8909 && elf_hash_table (flinfo->info)->dynamic_sections_created)) 8910 { 8911 if (! ((*bed->elf_backend_finish_dynamic_symbol) 8912 (flinfo->output_bfd, flinfo->info, h, &sym))) 8913 { 8914 eoinfo->failed = TRUE; 8915 return FALSE; 8916 } 8917 } 8918 8919 /* If we are marking the symbol as undefined, and there are no 8920 non-weak references to this symbol from a regular object, then 8921 mark the symbol as weak undefined; if there are non-weak 8922 references, mark the symbol as strong. We can't do this earlier, 8923 because it might not be marked as undefined until the 8924 finish_dynamic_symbol routine gets through with it. */ 8925 if (sym.st_shndx == SHN_UNDEF 8926 && h->ref_regular 8927 && (ELF_ST_BIND (sym.st_info) == STB_GLOBAL 8928 || ELF_ST_BIND (sym.st_info) == STB_WEAK)) 8929 { 8930 int bindtype; 8931 unsigned int type = ELF_ST_TYPE (sym.st_info); 8932 8933 /* Turn an undefined IFUNC symbol into a normal FUNC symbol. */ 8934 if (type == STT_GNU_IFUNC) 8935 type = STT_FUNC; 8936 8937 if (h->ref_regular_nonweak) 8938 bindtype = STB_GLOBAL; 8939 else 8940 bindtype = STB_WEAK; 8941 sym.st_info = ELF_ST_INFO (bindtype, type); 8942 } 8943 8944 /* If this is a symbol defined in a dynamic library, don't use the 8945 symbol size from the dynamic library. Relinking an executable 8946 against a new library may introduce gratuitous changes in the 8947 executable's symbols if we keep the size. */ 8948 if (sym.st_shndx == SHN_UNDEF 8949 && !h->def_regular 8950 && h->def_dynamic) 8951 sym.st_size = 0; 8952 8953 /* If a non-weak symbol with non-default visibility is not defined 8954 locally, it is a fatal error. */ 8955 if (!flinfo->info->relocatable 8956 && ELF_ST_VISIBILITY (sym.st_other) != STV_DEFAULT 8957 && ELF_ST_BIND (sym.st_info) != STB_WEAK 8958 && h->root.type == bfd_link_hash_undefined 8959 && !h->def_regular) 8960 { 8961 const char *msg; 8962 8963 if (ELF_ST_VISIBILITY (sym.st_other) == STV_PROTECTED) 8964 msg = _("%B: protected symbol `%s' isn't defined"); 8965 else if (ELF_ST_VISIBILITY (sym.st_other) == STV_INTERNAL) 8966 msg = _("%B: internal symbol `%s' isn't defined"); 8967 else 8968 msg = _("%B: hidden symbol `%s' isn't defined"); 8969 (*_bfd_error_handler) (msg, flinfo->output_bfd, h->root.root.string); 8970 bfd_set_error (bfd_error_bad_value); 8971 eoinfo->failed = TRUE; 8972 return FALSE; 8973 } 8974 8975 /* If this symbol should be put in the .dynsym section, then put it 8976 there now. We already know the symbol index. We also fill in 8977 the entry in the .hash section. */ 8978 if (flinfo->dynsym_sec != NULL 8979 && h->dynindx != -1 8980 && elf_hash_table (flinfo->info)->dynamic_sections_created) 8981 { 8982 bfd_byte *esym; 8983 8984 /* Since there is no version information in the dynamic string, 8985 if there is no version info in symbol version section, we will 8986 have a run-time problem. */ 8987 if (h->verinfo.verdef == NULL) 8988 { 8989 char *p = strrchr (h->root.root.string, ELF_VER_CHR); 8990 8991 if (p && p [1] != '\0') 8992 { 8993 (*_bfd_error_handler) 8994 (_("%B: No symbol version section for versioned symbol `%s'"), 8995 flinfo->output_bfd, h->root.root.string); 8996 eoinfo->failed = TRUE; 8997 return FALSE; 8998 } 8999 } 9000 9001 sym.st_name = h->dynstr_index; 9002 esym = flinfo->dynsym_sec->contents + h->dynindx * bed->s->sizeof_sym; 9003 if (!check_dynsym (flinfo->output_bfd, &sym)) 9004 { 9005 eoinfo->failed = TRUE; 9006 return FALSE; 9007 } 9008 bed->s->swap_symbol_out (flinfo->output_bfd, &sym, esym, 0); 9009 9010 if (flinfo->hash_sec != NULL) 9011 { 9012 size_t hash_entry_size; 9013 bfd_byte *bucketpos; 9014 bfd_vma chain; 9015 size_t bucketcount; 9016 size_t bucket; 9017 9018 bucketcount = elf_hash_table (flinfo->info)->bucketcount; 9019 bucket = h->u.elf_hash_value % bucketcount; 9020 9021 hash_entry_size 9022 = elf_section_data (flinfo->hash_sec)->this_hdr.sh_entsize; 9023 bucketpos = ((bfd_byte *) flinfo->hash_sec->contents 9024 + (bucket + 2) * hash_entry_size); 9025 chain = bfd_get (8 * hash_entry_size, flinfo->output_bfd, bucketpos); 9026 bfd_put (8 * hash_entry_size, flinfo->output_bfd, h->dynindx, 9027 bucketpos); 9028 bfd_put (8 * hash_entry_size, flinfo->output_bfd, chain, 9029 ((bfd_byte *) flinfo->hash_sec->contents 9030 + (bucketcount + 2 + h->dynindx) * hash_entry_size)); 9031 } 9032 9033 if (flinfo->symver_sec != NULL && flinfo->symver_sec->contents != NULL) 9034 { 9035 Elf_Internal_Versym iversym; 9036 Elf_External_Versym *eversym; 9037 9038 if (!h->def_regular) 9039 { 9040 if (h->verinfo.verdef == NULL) 9041 iversym.vs_vers = 0; 9042 else 9043 iversym.vs_vers = h->verinfo.verdef->vd_exp_refno + 1; 9044 } 9045 else 9046 { 9047 if (h->verinfo.vertree == NULL) 9048 iversym.vs_vers = 1; 9049 else 9050 iversym.vs_vers = h->verinfo.vertree->vernum + 1; 9051 if (flinfo->info->create_default_symver) 9052 iversym.vs_vers++; 9053 } 9054 9055 if (h->hidden) 9056 iversym.vs_vers |= VERSYM_HIDDEN; 9057 9058 eversym = (Elf_External_Versym *) flinfo->symver_sec->contents; 9059 eversym += h->dynindx; 9060 _bfd_elf_swap_versym_out (flinfo->output_bfd, &iversym, eversym); 9061 } 9062 } 9063 9064 /* If we're stripping it, then it was just a dynamic symbol, and 9065 there's nothing else to do. */ 9066 if (strip || (input_sec->flags & SEC_EXCLUDE) != 0) 9067 return TRUE; 9068 9069 indx = bfd_get_symcount (flinfo->output_bfd); 9070 ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h); 9071 if (ret == 0) 9072 { 9073 eoinfo->failed = TRUE; 9074 return FALSE; 9075 } 9076 else if (ret == 1) 9077 h->indx = indx; 9078 else if (h->indx == -2) 9079 abort(); 9080 9081 return TRUE; 9082 } 9083 9084 /* Return TRUE if special handling is done for relocs in SEC against 9085 symbols defined in discarded sections. */ 9086 9087 static bfd_boolean 9088 elf_section_ignore_discarded_relocs (asection *sec) 9089 { 9090 const struct elf_backend_data *bed; 9091 9092 switch (sec->sec_info_type) 9093 { 9094 case SEC_INFO_TYPE_STABS: 9095 case SEC_INFO_TYPE_EH_FRAME: 9096 return TRUE; 9097 default: 9098 break; 9099 } 9100 9101 bed = get_elf_backend_data (sec->owner); 9102 if (bed->elf_backend_ignore_discarded_relocs != NULL 9103 && (*bed->elf_backend_ignore_discarded_relocs) (sec)) 9104 return TRUE; 9105 9106 return FALSE; 9107 } 9108 9109 /* Return a mask saying how ld should treat relocations in SEC against 9110 symbols defined in discarded sections. If this function returns 9111 COMPLAIN set, ld will issue a warning message. If this function 9112 returns PRETEND set, and the discarded section was link-once and the 9113 same size as the kept link-once section, ld will pretend that the 9114 symbol was actually defined in the kept section. Otherwise ld will 9115 zero the reloc (at least that is the intent, but some cooperation by 9116 the target dependent code is needed, particularly for REL targets). */ 9117 9118 unsigned int 9119 _bfd_elf_default_action_discarded (asection *sec) 9120 { 9121 if (sec->flags & SEC_DEBUGGING) 9122 return PRETEND; 9123 9124 if (strcmp (".eh_frame", sec->name) == 0) 9125 return 0; 9126 9127 if (strcmp (".gcc_except_table", sec->name) == 0) 9128 return 0; 9129 9130 return COMPLAIN | PRETEND; 9131 } 9132 9133 /* Find a match between a section and a member of a section group. */ 9134 9135 static asection * 9136 match_group_member (asection *sec, asection *group, 9137 struct bfd_link_info *info) 9138 { 9139 asection *first = elf_next_in_group (group); 9140 asection *s = first; 9141 9142 while (s != NULL) 9143 { 9144 if (bfd_elf_match_symbols_in_sections (s, sec, info)) 9145 return s; 9146 9147 s = elf_next_in_group (s); 9148 if (s == first) 9149 break; 9150 } 9151 9152 return NULL; 9153 } 9154 9155 /* Check if the kept section of a discarded section SEC can be used 9156 to replace it. Return the replacement if it is OK. Otherwise return 9157 NULL. */ 9158 9159 asection * 9160 _bfd_elf_check_kept_section (asection *sec, struct bfd_link_info *info) 9161 { 9162 asection *kept; 9163 9164 kept = sec->kept_section; 9165 if (kept != NULL) 9166 { 9167 if ((kept->flags & SEC_GROUP) != 0) 9168 kept = match_group_member (sec, kept, info); 9169 if (kept != NULL 9170 && ((sec->rawsize != 0 ? sec->rawsize : sec->size) 9171 != (kept->rawsize != 0 ? kept->rawsize : kept->size))) 9172 kept = NULL; 9173 sec->kept_section = kept; 9174 } 9175 return kept; 9176 } 9177 9178 /* Link an input file into the linker output file. This function 9179 handles all the sections and relocations of the input file at once. 9180 This is so that we only have to read the local symbols once, and 9181 don't have to keep them in memory. */ 9182 9183 static bfd_boolean 9184 elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd) 9185 { 9186 int (*relocate_section) 9187 (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 9188 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); 9189 bfd *output_bfd; 9190 Elf_Internal_Shdr *symtab_hdr; 9191 size_t locsymcount; 9192 size_t extsymoff; 9193 Elf_Internal_Sym *isymbuf; 9194 Elf_Internal_Sym *isym; 9195 Elf_Internal_Sym *isymend; 9196 long *pindex; 9197 asection **ppsection; 9198 asection *o; 9199 const struct elf_backend_data *bed; 9200 struct elf_link_hash_entry **sym_hashes; 9201 bfd_size_type address_size; 9202 bfd_vma r_type_mask; 9203 int r_sym_shift; 9204 bfd_boolean have_file_sym = FALSE; 9205 9206 output_bfd = flinfo->output_bfd; 9207 bed = get_elf_backend_data (output_bfd); 9208 relocate_section = bed->elf_backend_relocate_section; 9209 9210 /* If this is a dynamic object, we don't want to do anything here: 9211 we don't want the local symbols, and we don't want the section 9212 contents. */ 9213 if ((input_bfd->flags & DYNAMIC) != 0) 9214 return TRUE; 9215 9216 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 9217 if (elf_bad_symtab (input_bfd)) 9218 { 9219 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 9220 extsymoff = 0; 9221 } 9222 else 9223 { 9224 locsymcount = symtab_hdr->sh_info; 9225 extsymoff = symtab_hdr->sh_info; 9226 } 9227 9228 /* Read the local symbols. */ 9229 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 9230 if (isymbuf == NULL && locsymcount != 0) 9231 { 9232 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 9233 flinfo->internal_syms, 9234 flinfo->external_syms, 9235 flinfo->locsym_shndx); 9236 if (isymbuf == NULL) 9237 return FALSE; 9238 } 9239 9240 /* Find local symbol sections and adjust values of symbols in 9241 SEC_MERGE sections. Write out those local symbols we know are 9242 going into the output file. */ 9243 isymend = isymbuf + locsymcount; 9244 for (isym = isymbuf, pindex = flinfo->indices, ppsection = flinfo->sections; 9245 isym < isymend; 9246 isym++, pindex++, ppsection++) 9247 { 9248 asection *isec; 9249 const char *name; 9250 Elf_Internal_Sym osym; 9251 long indx; 9252 int ret; 9253 9254 *pindex = -1; 9255 9256 if (elf_bad_symtab (input_bfd)) 9257 { 9258 if (ELF_ST_BIND (isym->st_info) != STB_LOCAL) 9259 { 9260 *ppsection = NULL; 9261 continue; 9262 } 9263 } 9264 9265 if (isym->st_shndx == SHN_UNDEF) 9266 isec = bfd_und_section_ptr; 9267 else if (isym->st_shndx == SHN_ABS) 9268 isec = bfd_abs_section_ptr; 9269 else if (isym->st_shndx == SHN_COMMON) 9270 isec = bfd_com_section_ptr; 9271 else 9272 { 9273 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 9274 if (isec == NULL) 9275 { 9276 /* Don't attempt to output symbols with st_shnx in the 9277 reserved range other than SHN_ABS and SHN_COMMON. */ 9278 *ppsection = NULL; 9279 continue; 9280 } 9281 else if (isec->sec_info_type == SEC_INFO_TYPE_MERGE 9282 && ELF_ST_TYPE (isym->st_info) != STT_SECTION) 9283 isym->st_value = 9284 _bfd_merged_section_offset (output_bfd, &isec, 9285 elf_section_data (isec)->sec_info, 9286 isym->st_value); 9287 } 9288 9289 *ppsection = isec; 9290 9291 /* Don't output the first, undefined, symbol. */ 9292 if (ppsection == flinfo->sections) 9293 continue; 9294 9295 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 9296 { 9297 /* We never output section symbols. Instead, we use the 9298 section symbol of the corresponding section in the output 9299 file. */ 9300 continue; 9301 } 9302 9303 /* If we are stripping all symbols, we don't want to output this 9304 one. */ 9305 if (flinfo->info->strip == strip_all) 9306 continue; 9307 9308 /* If we are discarding all local symbols, we don't want to 9309 output this one. If we are generating a relocatable output 9310 file, then some of the local symbols may be required by 9311 relocs; we output them below as we discover that they are 9312 needed. */ 9313 if (flinfo->info->discard == discard_all) 9314 continue; 9315 9316 /* If this symbol is defined in a section which we are 9317 discarding, we don't need to keep it. */ 9318 if (isym->st_shndx != SHN_UNDEF 9319 && isym->st_shndx < SHN_LORESERVE 9320 && bfd_section_removed_from_list (output_bfd, 9321 isec->output_section)) 9322 continue; 9323 9324 /* Get the name of the symbol. */ 9325 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, 9326 isym->st_name); 9327 if (name == NULL) 9328 return FALSE; 9329 9330 /* See if we are discarding symbols with this name. */ 9331 if ((flinfo->info->strip == strip_some 9332 && (bfd_hash_lookup (flinfo->info->keep_hash, name, FALSE, FALSE) 9333 == NULL)) 9334 || (((flinfo->info->discard == discard_sec_merge 9335 && (isec->flags & SEC_MERGE) && !flinfo->info->relocatable) 9336 || flinfo->info->discard == discard_l) 9337 && bfd_is_local_label_name (input_bfd, name))) 9338 continue; 9339 9340 if (ELF_ST_TYPE (isym->st_info) == STT_FILE) 9341 { 9342 have_file_sym = TRUE; 9343 flinfo->filesym_count += 1; 9344 } 9345 if (!have_file_sym) 9346 { 9347 /* In the absence of debug info, bfd_find_nearest_line uses 9348 FILE symbols to determine the source file for local 9349 function symbols. Provide a FILE symbol here if input 9350 files lack such, so that their symbols won't be 9351 associated with a previous input file. It's not the 9352 source file, but the best we can do. */ 9353 have_file_sym = TRUE; 9354 flinfo->filesym_count += 1; 9355 memset (&osym, 0, sizeof (osym)); 9356 osym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 9357 osym.st_shndx = SHN_ABS; 9358 if (!elf_link_output_sym (flinfo, input_bfd->filename, &osym, 9359 bfd_abs_section_ptr, NULL)) 9360 return FALSE; 9361 } 9362 9363 osym = *isym; 9364 9365 /* Adjust the section index for the output file. */ 9366 osym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 9367 isec->output_section); 9368 if (osym.st_shndx == SHN_BAD) 9369 return FALSE; 9370 9371 /* ELF symbols in relocatable files are section relative, but 9372 in executable files they are virtual addresses. Note that 9373 this code assumes that all ELF sections have an associated 9374 BFD section with a reasonable value for output_offset; below 9375 we assume that they also have a reasonable value for 9376 output_section. Any special sections must be set up to meet 9377 these requirements. */ 9378 osym.st_value += isec->output_offset; 9379 if (!flinfo->info->relocatable) 9380 { 9381 osym.st_value += isec->output_section->vma; 9382 if (ELF_ST_TYPE (osym.st_info) == STT_TLS) 9383 { 9384 /* STT_TLS symbols are relative to PT_TLS segment base. */ 9385 BFD_ASSERT (elf_hash_table (flinfo->info)->tls_sec != NULL); 9386 osym.st_value -= elf_hash_table (flinfo->info)->tls_sec->vma; 9387 } 9388 } 9389 9390 indx = bfd_get_symcount (output_bfd); 9391 ret = elf_link_output_sym (flinfo, name, &osym, isec, NULL); 9392 if (ret == 0) 9393 return FALSE; 9394 else if (ret == 1) 9395 *pindex = indx; 9396 } 9397 9398 if (bed->s->arch_size == 32) 9399 { 9400 r_type_mask = 0xff; 9401 r_sym_shift = 8; 9402 address_size = 4; 9403 } 9404 else 9405 { 9406 r_type_mask = 0xffffffff; 9407 r_sym_shift = 32; 9408 address_size = 8; 9409 } 9410 9411 /* Relocate the contents of each section. */ 9412 sym_hashes = elf_sym_hashes (input_bfd); 9413 for (o = input_bfd->sections; o != NULL; o = o->next) 9414 { 9415 bfd_byte *contents; 9416 9417 if (! o->linker_mark) 9418 { 9419 /* This section was omitted from the link. */ 9420 continue; 9421 } 9422 9423 if (flinfo->info->relocatable 9424 && (o->flags & (SEC_LINKER_CREATED | SEC_GROUP)) == SEC_GROUP) 9425 { 9426 /* Deal with the group signature symbol. */ 9427 struct bfd_elf_section_data *sec_data = elf_section_data (o); 9428 unsigned long symndx = sec_data->this_hdr.sh_info; 9429 asection *osec = o->output_section; 9430 9431 if (symndx >= locsymcount 9432 || (elf_bad_symtab (input_bfd) 9433 && flinfo->sections[symndx] == NULL)) 9434 { 9435 struct elf_link_hash_entry *h = sym_hashes[symndx - extsymoff]; 9436 while (h->root.type == bfd_link_hash_indirect 9437 || h->root.type == bfd_link_hash_warning) 9438 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9439 /* Arrange for symbol to be output. */ 9440 h->indx = -2; 9441 elf_section_data (osec)->this_hdr.sh_info = -2; 9442 } 9443 else if (ELF_ST_TYPE (isymbuf[symndx].st_info) == STT_SECTION) 9444 { 9445 /* We'll use the output section target_index. */ 9446 asection *sec = flinfo->sections[symndx]->output_section; 9447 elf_section_data (osec)->this_hdr.sh_info = sec->target_index; 9448 } 9449 else 9450 { 9451 if (flinfo->indices[symndx] == -1) 9452 { 9453 /* Otherwise output the local symbol now. */ 9454 Elf_Internal_Sym sym = isymbuf[symndx]; 9455 asection *sec = flinfo->sections[symndx]->output_section; 9456 const char *name; 9457 long indx; 9458 int ret; 9459 9460 name = bfd_elf_string_from_elf_section (input_bfd, 9461 symtab_hdr->sh_link, 9462 sym.st_name); 9463 if (name == NULL) 9464 return FALSE; 9465 9466 sym.st_shndx = _bfd_elf_section_from_bfd_section (output_bfd, 9467 sec); 9468 if (sym.st_shndx == SHN_BAD) 9469 return FALSE; 9470 9471 sym.st_value += o->output_offset; 9472 9473 indx = bfd_get_symcount (output_bfd); 9474 ret = elf_link_output_sym (flinfo, name, &sym, o, NULL); 9475 if (ret == 0) 9476 return FALSE; 9477 else if (ret == 1) 9478 flinfo->indices[symndx] = indx; 9479 else 9480 abort (); 9481 } 9482 elf_section_data (osec)->this_hdr.sh_info 9483 = flinfo->indices[symndx]; 9484 } 9485 } 9486 9487 if ((o->flags & SEC_HAS_CONTENTS) == 0 9488 || (o->size == 0 && (o->flags & SEC_RELOC) == 0)) 9489 continue; 9490 9491 if ((o->flags & SEC_LINKER_CREATED) != 0) 9492 { 9493 /* Section was created by _bfd_elf_link_create_dynamic_sections 9494 or somesuch. */ 9495 continue; 9496 } 9497 9498 /* Get the contents of the section. They have been cached by a 9499 relaxation routine. Note that o is a section in an input 9500 file, so the contents field will not have been set by any of 9501 the routines which work on output files. */ 9502 if (elf_section_data (o)->this_hdr.contents != NULL) 9503 contents = elf_section_data (o)->this_hdr.contents; 9504 else 9505 { 9506 contents = flinfo->contents; 9507 if (! bfd_get_full_section_contents (input_bfd, o, &contents)) 9508 return FALSE; 9509 } 9510 9511 if ((o->flags & SEC_RELOC) != 0) 9512 { 9513 Elf_Internal_Rela *internal_relocs; 9514 Elf_Internal_Rela *rel, *relend; 9515 int action_discarded; 9516 int ret; 9517 9518 /* Get the swapped relocs. */ 9519 internal_relocs 9520 = _bfd_elf_link_read_relocs (input_bfd, o, flinfo->external_relocs, 9521 flinfo->internal_relocs, FALSE); 9522 if (internal_relocs == NULL 9523 && o->reloc_count > 0) 9524 return FALSE; 9525 9526 /* We need to reverse-copy input .ctors/.dtors sections if 9527 they are placed in .init_array/.finit_array for output. */ 9528 if (o->size > address_size 9529 && ((strncmp (o->name, ".ctors", 6) == 0 9530 && strcmp (o->output_section->name, 9531 ".init_array") == 0) 9532 || (strncmp (o->name, ".dtors", 6) == 0 9533 && strcmp (o->output_section->name, 9534 ".fini_array") == 0)) 9535 && (o->name[6] == 0 || o->name[6] == '.')) 9536 { 9537 if (o->size != o->reloc_count * address_size) 9538 { 9539 (*_bfd_error_handler) 9540 (_("error: %B: size of section %A is not " 9541 "multiple of address size"), 9542 input_bfd, o); 9543 bfd_set_error (bfd_error_on_input); 9544 return FALSE; 9545 } 9546 o->flags |= SEC_ELF_REVERSE_COPY; 9547 } 9548 9549 action_discarded = -1; 9550 if (!elf_section_ignore_discarded_relocs (o)) 9551 action_discarded = (*bed->action_discarded) (o); 9552 9553 /* Run through the relocs evaluating complex reloc symbols and 9554 looking for relocs against symbols from discarded sections 9555 or section symbols from removed link-once sections. 9556 Complain about relocs against discarded sections. Zero 9557 relocs against removed link-once sections. */ 9558 9559 rel = internal_relocs; 9560 relend = rel + o->reloc_count * bed->s->int_rels_per_ext_rel; 9561 for ( ; rel < relend; rel++) 9562 { 9563 unsigned long r_symndx = rel->r_info >> r_sym_shift; 9564 unsigned int s_type; 9565 asection **ps, *sec; 9566 struct elf_link_hash_entry *h = NULL; 9567 const char *sym_name; 9568 9569 if (r_symndx == STN_UNDEF) 9570 continue; 9571 9572 if (r_symndx >= locsymcount 9573 || (elf_bad_symtab (input_bfd) 9574 && flinfo->sections[r_symndx] == NULL)) 9575 { 9576 h = sym_hashes[r_symndx - extsymoff]; 9577 9578 /* Badly formatted input files can contain relocs that 9579 reference non-existant symbols. Check here so that 9580 we do not seg fault. */ 9581 if (h == NULL) 9582 { 9583 char buffer [32]; 9584 9585 sprintf_vma (buffer, rel->r_info); 9586 (*_bfd_error_handler) 9587 (_("error: %B contains a reloc (0x%s) for section %A " 9588 "that references a non-existent global symbol"), 9589 input_bfd, o, buffer); 9590 bfd_set_error (bfd_error_bad_value); 9591 return FALSE; 9592 } 9593 9594 while (h->root.type == bfd_link_hash_indirect 9595 || h->root.type == bfd_link_hash_warning) 9596 h = (struct elf_link_hash_entry *) h->root.u.i.link; 9597 9598 s_type = h->type; 9599 9600 ps = NULL; 9601 if (h->root.type == bfd_link_hash_defined 9602 || h->root.type == bfd_link_hash_defweak) 9603 ps = &h->root.u.def.section; 9604 9605 sym_name = h->root.root.string; 9606 } 9607 else 9608 { 9609 Elf_Internal_Sym *sym = isymbuf + r_symndx; 9610 9611 s_type = ELF_ST_TYPE (sym->st_info); 9612 ps = &flinfo->sections[r_symndx]; 9613 sym_name = bfd_elf_sym_name (input_bfd, symtab_hdr, 9614 sym, *ps); 9615 } 9616 9617 if ((s_type == STT_RELC || s_type == STT_SRELC) 9618 && !flinfo->info->relocatable) 9619 { 9620 bfd_vma val; 9621 bfd_vma dot = (rel->r_offset 9622 + o->output_offset + o->output_section->vma); 9623 #ifdef DEBUG 9624 printf ("Encountered a complex symbol!"); 9625 printf (" (input_bfd %s, section %s, reloc %ld\n", 9626 input_bfd->filename, o->name, 9627 (long) (rel - internal_relocs)); 9628 printf (" symbol: idx %8.8lx, name %s\n", 9629 r_symndx, sym_name); 9630 printf (" reloc : info %8.8lx, addr %8.8lx\n", 9631 (unsigned long) rel->r_info, 9632 (unsigned long) rel->r_offset); 9633 #endif 9634 if (!eval_symbol (&val, &sym_name, input_bfd, flinfo, dot, 9635 isymbuf, locsymcount, s_type == STT_SRELC)) 9636 return FALSE; 9637 9638 /* Symbol evaluated OK. Update to absolute value. */ 9639 set_symbol_value (input_bfd, isymbuf, locsymcount, 9640 r_symndx, val); 9641 continue; 9642 } 9643 9644 if (action_discarded != -1 && ps != NULL) 9645 { 9646 /* Complain if the definition comes from a 9647 discarded section. */ 9648 if ((sec = *ps) != NULL && discarded_section (sec)) 9649 { 9650 BFD_ASSERT (r_symndx != STN_UNDEF); 9651 if (action_discarded & COMPLAIN) 9652 (*flinfo->info->callbacks->einfo) 9653 (_("%X`%s' referenced in section `%A' of %B: " 9654 "defined in discarded section `%A' of %B\n"), 9655 sym_name, o, input_bfd, sec, sec->owner); 9656 9657 /* Try to do the best we can to support buggy old 9658 versions of gcc. Pretend that the symbol is 9659 really defined in the kept linkonce section. 9660 FIXME: This is quite broken. Modifying the 9661 symbol here means we will be changing all later 9662 uses of the symbol, not just in this section. */ 9663 if (action_discarded & PRETEND) 9664 { 9665 asection *kept; 9666 9667 kept = _bfd_elf_check_kept_section (sec, 9668 flinfo->info); 9669 if (kept != NULL) 9670 { 9671 *ps = kept; 9672 continue; 9673 } 9674 } 9675 } 9676 } 9677 } 9678 9679 /* Relocate the section by invoking a back end routine. 9680 9681 The back end routine is responsible for adjusting the 9682 section contents as necessary, and (if using Rela relocs 9683 and generating a relocatable output file) adjusting the 9684 reloc addend as necessary. 9685 9686 The back end routine does not have to worry about setting 9687 the reloc address or the reloc symbol index. 9688 9689 The back end routine is given a pointer to the swapped in 9690 internal symbols, and can access the hash table entries 9691 for the external symbols via elf_sym_hashes (input_bfd). 9692 9693 When generating relocatable output, the back end routine 9694 must handle STB_LOCAL/STT_SECTION symbols specially. The 9695 output symbol is going to be a section symbol 9696 corresponding to the output section, which will require 9697 the addend to be adjusted. */ 9698 9699 ret = (*relocate_section) (output_bfd, flinfo->info, 9700 input_bfd, o, contents, 9701 internal_relocs, 9702 isymbuf, 9703 flinfo->sections); 9704 if (!ret) 9705 return FALSE; 9706 9707 if (ret == 2 9708 || flinfo->info->relocatable 9709 || flinfo->info->emitrelocations) 9710 { 9711 Elf_Internal_Rela *irela; 9712 Elf_Internal_Rela *irelaend, *irelamid; 9713 bfd_vma last_offset; 9714 struct elf_link_hash_entry **rel_hash; 9715 struct elf_link_hash_entry **rel_hash_list, **rela_hash_list; 9716 Elf_Internal_Shdr *input_rel_hdr, *input_rela_hdr; 9717 unsigned int next_erel; 9718 bfd_boolean rela_normal; 9719 struct bfd_elf_section_data *esdi, *esdo; 9720 9721 esdi = elf_section_data (o); 9722 esdo = elf_section_data (o->output_section); 9723 rela_normal = FALSE; 9724 9725 /* Adjust the reloc addresses and symbol indices. */ 9726 9727 irela = internal_relocs; 9728 irelaend = irela + o->reloc_count * bed->s->int_rels_per_ext_rel; 9729 rel_hash = esdo->rel.hashes + esdo->rel.count; 9730 /* We start processing the REL relocs, if any. When we reach 9731 IRELAMID in the loop, we switch to the RELA relocs. */ 9732 irelamid = irela; 9733 if (esdi->rel.hdr != NULL) 9734 irelamid += (NUM_SHDR_ENTRIES (esdi->rel.hdr) 9735 * bed->s->int_rels_per_ext_rel); 9736 rel_hash_list = rel_hash; 9737 rela_hash_list = NULL; 9738 last_offset = o->output_offset; 9739 if (!flinfo->info->relocatable) 9740 last_offset += o->output_section->vma; 9741 for (next_erel = 0; irela < irelaend; irela++, next_erel++) 9742 { 9743 unsigned long r_symndx; 9744 asection *sec; 9745 Elf_Internal_Sym sym; 9746 9747 if (next_erel == bed->s->int_rels_per_ext_rel) 9748 { 9749 rel_hash++; 9750 next_erel = 0; 9751 } 9752 9753 if (irela == irelamid) 9754 { 9755 rel_hash = esdo->rela.hashes + esdo->rela.count; 9756 rela_hash_list = rel_hash; 9757 rela_normal = bed->rela_normal; 9758 } 9759 9760 irela->r_offset = _bfd_elf_section_offset (output_bfd, 9761 flinfo->info, o, 9762 irela->r_offset); 9763 if (irela->r_offset >= (bfd_vma) -2) 9764 { 9765 /* This is a reloc for a deleted entry or somesuch. 9766 Turn it into an R_*_NONE reloc, at the same 9767 offset as the last reloc. elf_eh_frame.c and 9768 bfd_elf_discard_info rely on reloc offsets 9769 being ordered. */ 9770 irela->r_offset = last_offset; 9771 irela->r_info = 0; 9772 irela->r_addend = 0; 9773 continue; 9774 } 9775 9776 irela->r_offset += o->output_offset; 9777 9778 /* Relocs in an executable have to be virtual addresses. */ 9779 if (!flinfo->info->relocatable) 9780 irela->r_offset += o->output_section->vma; 9781 9782 last_offset = irela->r_offset; 9783 9784 r_symndx = irela->r_info >> r_sym_shift; 9785 if (r_symndx == STN_UNDEF) 9786 continue; 9787 9788 if (r_symndx >= locsymcount 9789 || (elf_bad_symtab (input_bfd) 9790 && flinfo->sections[r_symndx] == NULL)) 9791 { 9792 struct elf_link_hash_entry *rh; 9793 unsigned long indx; 9794 9795 /* This is a reloc against a global symbol. We 9796 have not yet output all the local symbols, so 9797 we do not know the symbol index of any global 9798 symbol. We set the rel_hash entry for this 9799 reloc to point to the global hash table entry 9800 for this symbol. The symbol index is then 9801 set at the end of bfd_elf_final_link. */ 9802 indx = r_symndx - extsymoff; 9803 rh = elf_sym_hashes (input_bfd)[indx]; 9804 while (rh->root.type == bfd_link_hash_indirect 9805 || rh->root.type == bfd_link_hash_warning) 9806 rh = (struct elf_link_hash_entry *) rh->root.u.i.link; 9807 9808 /* Setting the index to -2 tells 9809 elf_link_output_extsym that this symbol is 9810 used by a reloc. */ 9811 BFD_ASSERT (rh->indx < 0); 9812 rh->indx = -2; 9813 9814 *rel_hash = rh; 9815 9816 continue; 9817 } 9818 9819 /* This is a reloc against a local symbol. */ 9820 9821 *rel_hash = NULL; 9822 sym = isymbuf[r_symndx]; 9823 sec = flinfo->sections[r_symndx]; 9824 if (ELF_ST_TYPE (sym.st_info) == STT_SECTION) 9825 { 9826 /* I suppose the backend ought to fill in the 9827 section of any STT_SECTION symbol against a 9828 processor specific section. */ 9829 r_symndx = STN_UNDEF; 9830 if (bfd_is_abs_section (sec)) 9831 ; 9832 else if (sec == NULL || sec->owner == NULL) 9833 { 9834 bfd_set_error (bfd_error_bad_value); 9835 return FALSE; 9836 } 9837 else 9838 { 9839 asection *osec = sec->output_section; 9840 9841 /* If we have discarded a section, the output 9842 section will be the absolute section. In 9843 case of discarded SEC_MERGE sections, use 9844 the kept section. relocate_section should 9845 have already handled discarded linkonce 9846 sections. */ 9847 if (bfd_is_abs_section (osec) 9848 && sec->kept_section != NULL 9849 && sec->kept_section->output_section != NULL) 9850 { 9851 osec = sec->kept_section->output_section; 9852 irela->r_addend -= osec->vma; 9853 } 9854 9855 if (!bfd_is_abs_section (osec)) 9856 { 9857 r_symndx = osec->target_index; 9858 if (r_symndx == STN_UNDEF) 9859 { 9860 irela->r_addend += osec->vma; 9861 osec = _bfd_nearby_section (output_bfd, osec, 9862 osec->vma); 9863 irela->r_addend -= osec->vma; 9864 r_symndx = osec->target_index; 9865 } 9866 } 9867 } 9868 9869 /* Adjust the addend according to where the 9870 section winds up in the output section. */ 9871 if (rela_normal) 9872 irela->r_addend += sec->output_offset; 9873 } 9874 else 9875 { 9876 if (flinfo->indices[r_symndx] == -1) 9877 { 9878 unsigned long shlink; 9879 const char *name; 9880 asection *osec; 9881 long indx; 9882 9883 if (flinfo->info->strip == strip_all) 9884 { 9885 /* You can't do ld -r -s. */ 9886 bfd_set_error (bfd_error_invalid_operation); 9887 return FALSE; 9888 } 9889 9890 /* This symbol was skipped earlier, but 9891 since it is needed by a reloc, we 9892 must output it now. */ 9893 shlink = symtab_hdr->sh_link; 9894 name = (bfd_elf_string_from_elf_section 9895 (input_bfd, shlink, sym.st_name)); 9896 if (name == NULL) 9897 return FALSE; 9898 9899 osec = sec->output_section; 9900 sym.st_shndx = 9901 _bfd_elf_section_from_bfd_section (output_bfd, 9902 osec); 9903 if (sym.st_shndx == SHN_BAD) 9904 return FALSE; 9905 9906 sym.st_value += sec->output_offset; 9907 if (!flinfo->info->relocatable) 9908 { 9909 sym.st_value += osec->vma; 9910 if (ELF_ST_TYPE (sym.st_info) == STT_TLS) 9911 { 9912 /* STT_TLS symbols are relative to PT_TLS 9913 segment base. */ 9914 BFD_ASSERT (elf_hash_table (flinfo->info) 9915 ->tls_sec != NULL); 9916 sym.st_value -= (elf_hash_table (flinfo->info) 9917 ->tls_sec->vma); 9918 } 9919 } 9920 9921 indx = bfd_get_symcount (output_bfd); 9922 ret = elf_link_output_sym (flinfo, name, &sym, sec, 9923 NULL); 9924 if (ret == 0) 9925 return FALSE; 9926 else if (ret == 1) 9927 flinfo->indices[r_symndx] = indx; 9928 else 9929 abort (); 9930 } 9931 9932 r_symndx = flinfo->indices[r_symndx]; 9933 } 9934 9935 irela->r_info = ((bfd_vma) r_symndx << r_sym_shift 9936 | (irela->r_info & r_type_mask)); 9937 } 9938 9939 /* Swap out the relocs. */ 9940 input_rel_hdr = esdi->rel.hdr; 9941 if (input_rel_hdr && input_rel_hdr->sh_size != 0) 9942 { 9943 if (!bed->elf_backend_emit_relocs (output_bfd, o, 9944 input_rel_hdr, 9945 internal_relocs, 9946 rel_hash_list)) 9947 return FALSE; 9948 internal_relocs += (NUM_SHDR_ENTRIES (input_rel_hdr) 9949 * bed->s->int_rels_per_ext_rel); 9950 rel_hash_list += NUM_SHDR_ENTRIES (input_rel_hdr); 9951 } 9952 9953 input_rela_hdr = esdi->rela.hdr; 9954 if (input_rela_hdr && input_rela_hdr->sh_size != 0) 9955 { 9956 if (!bed->elf_backend_emit_relocs (output_bfd, o, 9957 input_rela_hdr, 9958 internal_relocs, 9959 rela_hash_list)) 9960 return FALSE; 9961 } 9962 } 9963 } 9964 9965 /* Write out the modified section contents. */ 9966 if (bed->elf_backend_write_section 9967 && (*bed->elf_backend_write_section) (output_bfd, flinfo->info, o, 9968 contents)) 9969 { 9970 /* Section written out. */ 9971 } 9972 else switch (o->sec_info_type) 9973 { 9974 case SEC_INFO_TYPE_STABS: 9975 if (! (_bfd_write_section_stabs 9976 (output_bfd, 9977 &elf_hash_table (flinfo->info)->stab_info, 9978 o, &elf_section_data (o)->sec_info, contents))) 9979 return FALSE; 9980 break; 9981 case SEC_INFO_TYPE_MERGE: 9982 if (! _bfd_write_merged_section (output_bfd, o, 9983 elf_section_data (o)->sec_info)) 9984 return FALSE; 9985 break; 9986 case SEC_INFO_TYPE_EH_FRAME: 9987 { 9988 if (! _bfd_elf_write_section_eh_frame (output_bfd, flinfo->info, 9989 o, contents)) 9990 return FALSE; 9991 } 9992 break; 9993 default: 9994 { 9995 /* FIXME: octets_per_byte. */ 9996 if (! (o->flags & SEC_EXCLUDE)) 9997 { 9998 file_ptr offset = (file_ptr) o->output_offset; 9999 bfd_size_type todo = o->size; 10000 if ((o->flags & SEC_ELF_REVERSE_COPY)) 10001 { 10002 /* Reverse-copy input section to output. */ 10003 do 10004 { 10005 todo -= address_size; 10006 if (! bfd_set_section_contents (output_bfd, 10007 o->output_section, 10008 contents + todo, 10009 offset, 10010 address_size)) 10011 return FALSE; 10012 if (todo == 0) 10013 break; 10014 offset += address_size; 10015 } 10016 while (1); 10017 } 10018 else if (! bfd_set_section_contents (output_bfd, 10019 o->output_section, 10020 contents, 10021 offset, todo)) 10022 return FALSE; 10023 } 10024 } 10025 break; 10026 } 10027 } 10028 10029 return TRUE; 10030 } 10031 10032 /* Generate a reloc when linking an ELF file. This is a reloc 10033 requested by the linker, and does not come from any input file. This 10034 is used to build constructor and destructor tables when linking 10035 with -Ur. */ 10036 10037 static bfd_boolean 10038 elf_reloc_link_order (bfd *output_bfd, 10039 struct bfd_link_info *info, 10040 asection *output_section, 10041 struct bfd_link_order *link_order) 10042 { 10043 reloc_howto_type *howto; 10044 long indx; 10045 bfd_vma offset; 10046 bfd_vma addend; 10047 struct bfd_elf_section_reloc_data *reldata; 10048 struct elf_link_hash_entry **rel_hash_ptr; 10049 Elf_Internal_Shdr *rel_hdr; 10050 const struct elf_backend_data *bed = get_elf_backend_data (output_bfd); 10051 Elf_Internal_Rela irel[MAX_INT_RELS_PER_EXT_REL]; 10052 bfd_byte *erel; 10053 unsigned int i; 10054 struct bfd_elf_section_data *esdo = elf_section_data (output_section); 10055 10056 howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc); 10057 if (howto == NULL) 10058 { 10059 bfd_set_error (bfd_error_bad_value); 10060 return FALSE; 10061 } 10062 10063 addend = link_order->u.reloc.p->addend; 10064 10065 if (esdo->rel.hdr) 10066 reldata = &esdo->rel; 10067 else if (esdo->rela.hdr) 10068 reldata = &esdo->rela; 10069 else 10070 { 10071 reldata = NULL; 10072 BFD_ASSERT (0); 10073 } 10074 10075 /* Figure out the symbol index. */ 10076 rel_hash_ptr = reldata->hashes + reldata->count; 10077 if (link_order->type == bfd_section_reloc_link_order) 10078 { 10079 indx = link_order->u.reloc.p->u.section->target_index; 10080 BFD_ASSERT (indx != 0); 10081 *rel_hash_ptr = NULL; 10082 } 10083 else 10084 { 10085 struct elf_link_hash_entry *h; 10086 10087 /* Treat a reloc against a defined symbol as though it were 10088 actually against the section. */ 10089 h = ((struct elf_link_hash_entry *) 10090 bfd_wrapped_link_hash_lookup (output_bfd, info, 10091 link_order->u.reloc.p->u.name, 10092 FALSE, FALSE, TRUE)); 10093 if (h != NULL 10094 && (h->root.type == bfd_link_hash_defined 10095 || h->root.type == bfd_link_hash_defweak)) 10096 { 10097 asection *section; 10098 10099 section = h->root.u.def.section; 10100 indx = section->output_section->target_index; 10101 *rel_hash_ptr = NULL; 10102 /* It seems that we ought to add the symbol value to the 10103 addend here, but in practice it has already been added 10104 because it was passed to constructor_callback. */ 10105 addend += section->output_section->vma + section->output_offset; 10106 } 10107 else if (h != NULL) 10108 { 10109 /* Setting the index to -2 tells elf_link_output_extsym that 10110 this symbol is used by a reloc. */ 10111 h->indx = -2; 10112 *rel_hash_ptr = h; 10113 indx = 0; 10114 } 10115 else 10116 { 10117 if (! ((*info->callbacks->unattached_reloc) 10118 (info, link_order->u.reloc.p->u.name, NULL, NULL, 0))) 10119 return FALSE; 10120 indx = 0; 10121 } 10122 } 10123 10124 /* If this is an inplace reloc, we must write the addend into the 10125 object file. */ 10126 if (howto->partial_inplace && addend != 0) 10127 { 10128 bfd_size_type size; 10129 bfd_reloc_status_type rstat; 10130 bfd_byte *buf; 10131 bfd_boolean ok; 10132 const char *sym_name; 10133 10134 size = (bfd_size_type) bfd_get_reloc_size (howto); 10135 buf = (bfd_byte *) bfd_zmalloc (size); 10136 if (buf == NULL) 10137 return FALSE; 10138 rstat = _bfd_relocate_contents (howto, output_bfd, addend, buf); 10139 switch (rstat) 10140 { 10141 case bfd_reloc_ok: 10142 break; 10143 10144 default: 10145 case bfd_reloc_outofrange: 10146 abort (); 10147 10148 case bfd_reloc_overflow: 10149 if (link_order->type == bfd_section_reloc_link_order) 10150 sym_name = bfd_section_name (output_bfd, 10151 link_order->u.reloc.p->u.section); 10152 else 10153 sym_name = link_order->u.reloc.p->u.name; 10154 if (! ((*info->callbacks->reloc_overflow) 10155 (info, NULL, sym_name, howto->name, addend, NULL, 10156 NULL, (bfd_vma) 0))) 10157 { 10158 free (buf); 10159 return FALSE; 10160 } 10161 break; 10162 } 10163 ok = bfd_set_section_contents (output_bfd, output_section, buf, 10164 link_order->offset, size); 10165 free (buf); 10166 if (! ok) 10167 return FALSE; 10168 } 10169 10170 /* The address of a reloc is relative to the section in a 10171 relocatable file, and is a virtual address in an executable 10172 file. */ 10173 offset = link_order->offset; 10174 if (! info->relocatable) 10175 offset += output_section->vma; 10176 10177 for (i = 0; i < bed->s->int_rels_per_ext_rel; i++) 10178 { 10179 irel[i].r_offset = offset; 10180 irel[i].r_info = 0; 10181 irel[i].r_addend = 0; 10182 } 10183 if (bed->s->arch_size == 32) 10184 irel[0].r_info = ELF32_R_INFO (indx, howto->type); 10185 else 10186 #ifdef BFD64 10187 { 10188 bfd_uint64_t indx64 = indx; 10189 irel[0].r_info = ELF64_R_INFO (indx64, howto->type); 10190 } 10191 #else 10192 BFD_FAIL(); 10193 #endif 10194 10195 rel_hdr = reldata->hdr; 10196 erel = rel_hdr->contents; 10197 if (rel_hdr->sh_type == SHT_REL) 10198 { 10199 erel += reldata->count * bed->s->sizeof_rel; 10200 (*bed->s->swap_reloc_out) (output_bfd, irel, erel); 10201 } 10202 else 10203 { 10204 irel[0].r_addend = addend; 10205 erel += reldata->count * bed->s->sizeof_rela; 10206 (*bed->s->swap_reloca_out) (output_bfd, irel, erel); 10207 } 10208 10209 ++reldata->count; 10210 10211 return TRUE; 10212 } 10213 10214 10215 /* Get the output vma of the section pointed to by the sh_link field. */ 10216 10217 static bfd_vma 10218 elf_get_linked_section_vma (struct bfd_link_order *p) 10219 { 10220 Elf_Internal_Shdr **elf_shdrp; 10221 asection *s; 10222 int elfsec; 10223 10224 s = p->u.indirect.section; 10225 elf_shdrp = elf_elfsections (s->owner); 10226 elfsec = _bfd_elf_section_from_bfd_section (s->owner, s); 10227 elfsec = elf_shdrp[elfsec]->sh_link; 10228 /* PR 290: 10229 The Intel C compiler generates SHT_IA_64_UNWIND with 10230 SHF_LINK_ORDER. But it doesn't set the sh_link or 10231 sh_info fields. Hence we could get the situation 10232 where elfsec is 0. */ 10233 if (elfsec == 0) 10234 { 10235 const struct elf_backend_data *bed 10236 = get_elf_backend_data (s->owner); 10237 if (bed->link_order_error_handler) 10238 bed->link_order_error_handler 10239 (_("%B: warning: sh_link not set for section `%A'"), s->owner, s); 10240 return 0; 10241 } 10242 else 10243 { 10244 s = elf_shdrp[elfsec]->bfd_section; 10245 return s->output_section->vma + s->output_offset; 10246 } 10247 } 10248 10249 10250 /* Compare two sections based on the locations of the sections they are 10251 linked to. Used by elf_fixup_link_order. */ 10252 10253 static int 10254 compare_link_order (const void * a, const void * b) 10255 { 10256 bfd_vma apos; 10257 bfd_vma bpos; 10258 10259 apos = elf_get_linked_section_vma (*(struct bfd_link_order **)a); 10260 bpos = elf_get_linked_section_vma (*(struct bfd_link_order **)b); 10261 if (apos < bpos) 10262 return -1; 10263 return apos > bpos; 10264 } 10265 10266 10267 /* Looks for sections with SHF_LINK_ORDER set. Rearranges them into the same 10268 order as their linked sections. Returns false if this could not be done 10269 because an output section includes both ordered and unordered 10270 sections. Ideally we'd do this in the linker proper. */ 10271 10272 static bfd_boolean 10273 elf_fixup_link_order (bfd *abfd, asection *o) 10274 { 10275 int seen_linkorder; 10276 int seen_other; 10277 int n; 10278 struct bfd_link_order *p; 10279 bfd *sub; 10280 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10281 unsigned elfsec; 10282 struct bfd_link_order **sections; 10283 asection *s, *other_sec, *linkorder_sec; 10284 bfd_vma offset; 10285 10286 other_sec = NULL; 10287 linkorder_sec = NULL; 10288 seen_other = 0; 10289 seen_linkorder = 0; 10290 for (p = o->map_head.link_order; p != NULL; p = p->next) 10291 { 10292 if (p->type == bfd_indirect_link_order) 10293 { 10294 s = p->u.indirect.section; 10295 sub = s->owner; 10296 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 10297 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass 10298 && (elfsec = _bfd_elf_section_from_bfd_section (sub, s)) 10299 && elfsec < elf_numsections (sub) 10300 && elf_elfsections (sub)[elfsec]->sh_flags & SHF_LINK_ORDER 10301 && elf_elfsections (sub)[elfsec]->sh_link < elf_numsections (sub)) 10302 { 10303 seen_linkorder++; 10304 linkorder_sec = s; 10305 } 10306 else 10307 { 10308 seen_other++; 10309 other_sec = s; 10310 } 10311 } 10312 else 10313 seen_other++; 10314 10315 if (seen_other && seen_linkorder) 10316 { 10317 if (other_sec && linkorder_sec) 10318 (*_bfd_error_handler) (_("%A has both ordered [`%A' in %B] and unordered [`%A' in %B] sections"), 10319 o, linkorder_sec, 10320 linkorder_sec->owner, other_sec, 10321 other_sec->owner); 10322 else 10323 (*_bfd_error_handler) (_("%A has both ordered and unordered sections"), 10324 o); 10325 bfd_set_error (bfd_error_bad_value); 10326 return FALSE; 10327 } 10328 } 10329 10330 if (!seen_linkorder) 10331 return TRUE; 10332 10333 sections = (struct bfd_link_order **) 10334 bfd_malloc (seen_linkorder * sizeof (struct bfd_link_order *)); 10335 if (sections == NULL) 10336 return FALSE; 10337 seen_linkorder = 0; 10338 10339 for (p = o->map_head.link_order; p != NULL; p = p->next) 10340 { 10341 sections[seen_linkorder++] = p; 10342 } 10343 /* Sort the input sections in the order of their linked section. */ 10344 qsort (sections, seen_linkorder, sizeof (struct bfd_link_order *), 10345 compare_link_order); 10346 10347 /* Change the offsets of the sections. */ 10348 offset = 0; 10349 for (n = 0; n < seen_linkorder; n++) 10350 { 10351 s = sections[n]->u.indirect.section; 10352 offset &= ~(bfd_vma) 0 << s->alignment_power; 10353 s->output_offset = offset; 10354 sections[n]->offset = offset; 10355 /* FIXME: octets_per_byte. */ 10356 offset += sections[n]->size; 10357 } 10358 10359 free (sections); 10360 return TRUE; 10361 } 10362 10363 10364 /* Do the final step of an ELF link. */ 10365 10366 bfd_boolean 10367 bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info) 10368 { 10369 bfd_boolean dynamic; 10370 bfd_boolean emit_relocs; 10371 bfd *dynobj; 10372 struct elf_final_link_info flinfo; 10373 asection *o; 10374 struct bfd_link_order *p; 10375 bfd *sub; 10376 bfd_size_type max_contents_size; 10377 bfd_size_type max_external_reloc_size; 10378 bfd_size_type max_internal_reloc_count; 10379 bfd_size_type max_sym_count; 10380 bfd_size_type max_sym_shndx_count; 10381 file_ptr off; 10382 Elf_Internal_Sym elfsym; 10383 unsigned int i; 10384 Elf_Internal_Shdr *symtab_hdr; 10385 Elf_Internal_Shdr *symtab_shndx_hdr; 10386 Elf_Internal_Shdr *symstrtab_hdr; 10387 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 10388 struct elf_outext_info eoinfo; 10389 bfd_boolean merged; 10390 size_t relativecount = 0; 10391 asection *reldyn = 0; 10392 bfd_size_type amt; 10393 asection *attr_section = NULL; 10394 bfd_vma attr_size = 0; 10395 const char *std_attrs_section; 10396 10397 if (! is_elf_hash_table (info->hash)) 10398 return FALSE; 10399 10400 if (info->shared) 10401 abfd->flags |= DYNAMIC; 10402 10403 dynamic = elf_hash_table (info)->dynamic_sections_created; 10404 dynobj = elf_hash_table (info)->dynobj; 10405 10406 emit_relocs = (info->relocatable 10407 || info->emitrelocations); 10408 10409 flinfo.info = info; 10410 flinfo.output_bfd = abfd; 10411 flinfo.symstrtab = _bfd_elf_stringtab_init (); 10412 if (flinfo.symstrtab == NULL) 10413 return FALSE; 10414 10415 if (! dynamic) 10416 { 10417 flinfo.dynsym_sec = NULL; 10418 flinfo.hash_sec = NULL; 10419 flinfo.symver_sec = NULL; 10420 } 10421 else 10422 { 10423 flinfo.dynsym_sec = bfd_get_linker_section (dynobj, ".dynsym"); 10424 flinfo.hash_sec = bfd_get_linker_section (dynobj, ".hash"); 10425 /* Note that dynsym_sec can be NULL (on VMS). */ 10426 flinfo.symver_sec = bfd_get_linker_section (dynobj, ".gnu.version"); 10427 /* Note that it is OK if symver_sec is NULL. */ 10428 } 10429 10430 flinfo.contents = NULL; 10431 flinfo.external_relocs = NULL; 10432 flinfo.internal_relocs = NULL; 10433 flinfo.external_syms = NULL; 10434 flinfo.locsym_shndx = NULL; 10435 flinfo.internal_syms = NULL; 10436 flinfo.indices = NULL; 10437 flinfo.sections = NULL; 10438 flinfo.symbuf = NULL; 10439 flinfo.symshndxbuf = NULL; 10440 flinfo.symbuf_count = 0; 10441 flinfo.shndxbuf_size = 0; 10442 flinfo.filesym_count = 0; 10443 10444 /* The object attributes have been merged. Remove the input 10445 sections from the link, and set the contents of the output 10446 secton. */ 10447 std_attrs_section = get_elf_backend_data (abfd)->obj_attrs_section; 10448 for (o = abfd->sections; o != NULL; o = o->next) 10449 { 10450 if ((std_attrs_section && strcmp (o->name, std_attrs_section) == 0) 10451 || strcmp (o->name, ".gnu.attributes") == 0) 10452 { 10453 for (p = o->map_head.link_order; p != NULL; p = p->next) 10454 { 10455 asection *input_section; 10456 10457 if (p->type != bfd_indirect_link_order) 10458 continue; 10459 input_section = p->u.indirect.section; 10460 /* Hack: reset the SEC_HAS_CONTENTS flag so that 10461 elf_link_input_bfd ignores this section. */ 10462 input_section->flags &= ~SEC_HAS_CONTENTS; 10463 } 10464 10465 attr_size = bfd_elf_obj_attr_size (abfd); 10466 if (attr_size) 10467 { 10468 bfd_set_section_size (abfd, o, attr_size); 10469 attr_section = o; 10470 /* Skip this section later on. */ 10471 o->map_head.link_order = NULL; 10472 } 10473 else 10474 o->flags |= SEC_EXCLUDE; 10475 } 10476 } 10477 10478 /* Count up the number of relocations we will output for each output 10479 section, so that we know the sizes of the reloc sections. We 10480 also figure out some maximum sizes. */ 10481 max_contents_size = 0; 10482 max_external_reloc_size = 0; 10483 max_internal_reloc_count = 0; 10484 max_sym_count = 0; 10485 max_sym_shndx_count = 0; 10486 merged = FALSE; 10487 for (o = abfd->sections; o != NULL; o = o->next) 10488 { 10489 struct bfd_elf_section_data *esdo = elf_section_data (o); 10490 o->reloc_count = 0; 10491 10492 for (p = o->map_head.link_order; p != NULL; p = p->next) 10493 { 10494 unsigned int reloc_count = 0; 10495 struct bfd_elf_section_data *esdi = NULL; 10496 10497 if (p->type == bfd_section_reloc_link_order 10498 || p->type == bfd_symbol_reloc_link_order) 10499 reloc_count = 1; 10500 else if (p->type == bfd_indirect_link_order) 10501 { 10502 asection *sec; 10503 10504 sec = p->u.indirect.section; 10505 esdi = elf_section_data (sec); 10506 10507 /* Mark all sections which are to be included in the 10508 link. This will normally be every section. We need 10509 to do this so that we can identify any sections which 10510 the linker has decided to not include. */ 10511 sec->linker_mark = TRUE; 10512 10513 if (sec->flags & SEC_MERGE) 10514 merged = TRUE; 10515 10516 if (esdo->this_hdr.sh_type == SHT_REL 10517 || esdo->this_hdr.sh_type == SHT_RELA) 10518 /* Some backends use reloc_count in relocation sections 10519 to count particular types of relocs. Of course, 10520 reloc sections themselves can't have relocations. */ 10521 reloc_count = 0; 10522 else if (info->relocatable || info->emitrelocations) 10523 reloc_count = sec->reloc_count; 10524 else if (bed->elf_backend_count_relocs) 10525 reloc_count = (*bed->elf_backend_count_relocs) (info, sec); 10526 10527 if (sec->rawsize > max_contents_size) 10528 max_contents_size = sec->rawsize; 10529 if (sec->size > max_contents_size) 10530 max_contents_size = sec->size; 10531 10532 /* We are interested in just local symbols, not all 10533 symbols. */ 10534 if (bfd_get_flavour (sec->owner) == bfd_target_elf_flavour 10535 && (sec->owner->flags & DYNAMIC) == 0) 10536 { 10537 size_t sym_count; 10538 10539 if (elf_bad_symtab (sec->owner)) 10540 sym_count = (elf_tdata (sec->owner)->symtab_hdr.sh_size 10541 / bed->s->sizeof_sym); 10542 else 10543 sym_count = elf_tdata (sec->owner)->symtab_hdr.sh_info; 10544 10545 if (sym_count > max_sym_count) 10546 max_sym_count = sym_count; 10547 10548 if (sym_count > max_sym_shndx_count 10549 && elf_symtab_shndx (sec->owner) != 0) 10550 max_sym_shndx_count = sym_count; 10551 10552 if ((sec->flags & SEC_RELOC) != 0) 10553 { 10554 size_t ext_size = 0; 10555 10556 if (esdi->rel.hdr != NULL) 10557 ext_size = esdi->rel.hdr->sh_size; 10558 if (esdi->rela.hdr != NULL) 10559 ext_size += esdi->rela.hdr->sh_size; 10560 10561 if (ext_size > max_external_reloc_size) 10562 max_external_reloc_size = ext_size; 10563 if (sec->reloc_count > max_internal_reloc_count) 10564 max_internal_reloc_count = sec->reloc_count; 10565 } 10566 } 10567 } 10568 10569 if (reloc_count == 0) 10570 continue; 10571 10572 o->reloc_count += reloc_count; 10573 10574 if (p->type == bfd_indirect_link_order 10575 && (info->relocatable || info->emitrelocations)) 10576 { 10577 if (esdi->rel.hdr) 10578 esdo->rel.count += NUM_SHDR_ENTRIES (esdi->rel.hdr); 10579 if (esdi->rela.hdr) 10580 esdo->rela.count += NUM_SHDR_ENTRIES (esdi->rela.hdr); 10581 } 10582 else 10583 { 10584 if (o->use_rela_p) 10585 esdo->rela.count += reloc_count; 10586 else 10587 esdo->rel.count += reloc_count; 10588 } 10589 } 10590 10591 if (o->reloc_count > 0) 10592 o->flags |= SEC_RELOC; 10593 else 10594 { 10595 /* Explicitly clear the SEC_RELOC flag. The linker tends to 10596 set it (this is probably a bug) and if it is set 10597 assign_section_numbers will create a reloc section. */ 10598 o->flags &=~ SEC_RELOC; 10599 } 10600 10601 /* If the SEC_ALLOC flag is not set, force the section VMA to 10602 zero. This is done in elf_fake_sections as well, but forcing 10603 the VMA to 0 here will ensure that relocs against these 10604 sections are handled correctly. */ 10605 if ((o->flags & SEC_ALLOC) == 0 10606 && ! o->user_set_vma) 10607 o->vma = 0; 10608 } 10609 10610 if (! info->relocatable && merged) 10611 elf_link_hash_traverse (elf_hash_table (info), 10612 _bfd_elf_link_sec_merge_syms, abfd); 10613 10614 /* Figure out the file positions for everything but the symbol table 10615 and the relocs. We set symcount to force assign_section_numbers 10616 to create a symbol table. */ 10617 bfd_get_symcount (abfd) = info->strip == strip_all ? 0 : 1; 10618 BFD_ASSERT (! abfd->output_has_begun); 10619 if (! _bfd_elf_compute_section_file_positions (abfd, info)) 10620 goto error_return; 10621 10622 /* Set sizes, and assign file positions for reloc sections. */ 10623 for (o = abfd->sections; o != NULL; o = o->next) 10624 { 10625 struct bfd_elf_section_data *esdo = elf_section_data (o); 10626 if ((o->flags & SEC_RELOC) != 0) 10627 { 10628 if (esdo->rel.hdr 10629 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rel))) 10630 goto error_return; 10631 10632 if (esdo->rela.hdr 10633 && !(_bfd_elf_link_size_reloc_section (abfd, &esdo->rela))) 10634 goto error_return; 10635 } 10636 10637 /* Now, reset REL_COUNT and REL_COUNT2 so that we can use them 10638 to count upwards while actually outputting the relocations. */ 10639 esdo->rel.count = 0; 10640 esdo->rela.count = 0; 10641 } 10642 10643 _bfd_elf_assign_file_positions_for_relocs (abfd); 10644 10645 /* We have now assigned file positions for all the sections except 10646 .symtab and .strtab. We start the .symtab section at the current 10647 file position, and write directly to it. We build the .strtab 10648 section in memory. */ 10649 bfd_get_symcount (abfd) = 0; 10650 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 10651 /* sh_name is set in prep_headers. */ 10652 symtab_hdr->sh_type = SHT_SYMTAB; 10653 /* sh_flags, sh_addr and sh_size all start off zero. */ 10654 symtab_hdr->sh_entsize = bed->s->sizeof_sym; 10655 /* sh_link is set in assign_section_numbers. */ 10656 /* sh_info is set below. */ 10657 /* sh_offset is set just below. */ 10658 symtab_hdr->sh_addralign = (bfd_vma) 1 << bed->s->log_file_align; 10659 10660 off = elf_tdata (abfd)->next_file_pos; 10661 off = _bfd_elf_assign_file_position_for_section (symtab_hdr, off, TRUE); 10662 10663 /* Note that at this point elf_tdata (abfd)->next_file_pos is 10664 incorrect. We do not yet know the size of the .symtab section. 10665 We correct next_file_pos below, after we do know the size. */ 10666 10667 /* Allocate a buffer to hold swapped out symbols. This is to avoid 10668 continuously seeking to the right position in the file. */ 10669 if (! info->keep_memory || max_sym_count < 20) 10670 flinfo.symbuf_size = 20; 10671 else 10672 flinfo.symbuf_size = max_sym_count; 10673 amt = flinfo.symbuf_size; 10674 amt *= bed->s->sizeof_sym; 10675 flinfo.symbuf = (bfd_byte *) bfd_malloc (amt); 10676 if (flinfo.symbuf == NULL) 10677 goto error_return; 10678 if (elf_numsections (abfd) > (SHN_LORESERVE & 0xFFFF)) 10679 { 10680 /* Wild guess at number of output symbols. realloc'd as needed. */ 10681 amt = 2 * max_sym_count + elf_numsections (abfd) + 1000; 10682 flinfo.shndxbuf_size = amt; 10683 amt *= sizeof (Elf_External_Sym_Shndx); 10684 flinfo.symshndxbuf = (Elf_External_Sym_Shndx *) bfd_zmalloc (amt); 10685 if (flinfo.symshndxbuf == NULL) 10686 goto error_return; 10687 } 10688 10689 /* Start writing out the symbol table. The first symbol is always a 10690 dummy symbol. */ 10691 if (info->strip != strip_all 10692 || emit_relocs) 10693 { 10694 elfsym.st_value = 0; 10695 elfsym.st_size = 0; 10696 elfsym.st_info = 0; 10697 elfsym.st_other = 0; 10698 elfsym.st_shndx = SHN_UNDEF; 10699 elfsym.st_target_internal = 0; 10700 if (elf_link_output_sym (&flinfo, NULL, &elfsym, bfd_und_section_ptr, 10701 NULL) != 1) 10702 goto error_return; 10703 } 10704 10705 /* Output a symbol for each section. We output these even if we are 10706 discarding local symbols, since they are used for relocs. These 10707 symbols have no names. We store the index of each one in the 10708 index field of the section, so that we can find it again when 10709 outputting relocs. */ 10710 if (info->strip != strip_all 10711 || emit_relocs) 10712 { 10713 elfsym.st_size = 0; 10714 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 10715 elfsym.st_other = 0; 10716 elfsym.st_value = 0; 10717 elfsym.st_target_internal = 0; 10718 for (i = 1; i < elf_numsections (abfd); i++) 10719 { 10720 o = bfd_section_from_elf_index (abfd, i); 10721 if (o != NULL) 10722 { 10723 o->target_index = bfd_get_symcount (abfd); 10724 elfsym.st_shndx = i; 10725 if (!info->relocatable) 10726 elfsym.st_value = o->vma; 10727 if (elf_link_output_sym (&flinfo, NULL, &elfsym, o, NULL) != 1) 10728 goto error_return; 10729 } 10730 } 10731 } 10732 10733 /* Allocate some memory to hold information read in from the input 10734 files. */ 10735 if (max_contents_size != 0) 10736 { 10737 flinfo.contents = (bfd_byte *) bfd_malloc (max_contents_size); 10738 if (flinfo.contents == NULL) 10739 goto error_return; 10740 } 10741 10742 if (max_external_reloc_size != 0) 10743 { 10744 flinfo.external_relocs = bfd_malloc (max_external_reloc_size); 10745 if (flinfo.external_relocs == NULL) 10746 goto error_return; 10747 } 10748 10749 if (max_internal_reloc_count != 0) 10750 { 10751 amt = max_internal_reloc_count * bed->s->int_rels_per_ext_rel; 10752 amt *= sizeof (Elf_Internal_Rela); 10753 flinfo.internal_relocs = (Elf_Internal_Rela *) bfd_malloc (amt); 10754 if (flinfo.internal_relocs == NULL) 10755 goto error_return; 10756 } 10757 10758 if (max_sym_count != 0) 10759 { 10760 amt = max_sym_count * bed->s->sizeof_sym; 10761 flinfo.external_syms = (bfd_byte *) bfd_malloc (amt); 10762 if (flinfo.external_syms == NULL) 10763 goto error_return; 10764 10765 amt = max_sym_count * sizeof (Elf_Internal_Sym); 10766 flinfo.internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt); 10767 if (flinfo.internal_syms == NULL) 10768 goto error_return; 10769 10770 amt = max_sym_count * sizeof (long); 10771 flinfo.indices = (long int *) bfd_malloc (amt); 10772 if (flinfo.indices == NULL) 10773 goto error_return; 10774 10775 amt = max_sym_count * sizeof (asection *); 10776 flinfo.sections = (asection **) bfd_malloc (amt); 10777 if (flinfo.sections == NULL) 10778 goto error_return; 10779 } 10780 10781 if (max_sym_shndx_count != 0) 10782 { 10783 amt = max_sym_shndx_count * sizeof (Elf_External_Sym_Shndx); 10784 flinfo.locsym_shndx = (Elf_External_Sym_Shndx *) bfd_malloc (amt); 10785 if (flinfo.locsym_shndx == NULL) 10786 goto error_return; 10787 } 10788 10789 if (elf_hash_table (info)->tls_sec) 10790 { 10791 bfd_vma base, end = 0; 10792 asection *sec; 10793 10794 for (sec = elf_hash_table (info)->tls_sec; 10795 sec && (sec->flags & SEC_THREAD_LOCAL); 10796 sec = sec->next) 10797 { 10798 bfd_size_type size = sec->size; 10799 10800 if (size == 0 10801 && (sec->flags & SEC_HAS_CONTENTS) == 0) 10802 { 10803 struct bfd_link_order *ord = sec->map_tail.link_order; 10804 10805 if (ord != NULL) 10806 size = ord->offset + ord->size; 10807 } 10808 end = sec->vma + size; 10809 } 10810 base = elf_hash_table (info)->tls_sec->vma; 10811 /* Only align end of TLS section if static TLS doesn't have special 10812 alignment requirements. */ 10813 if (bed->static_tls_alignment == 1) 10814 end = align_power (end, 10815 elf_hash_table (info)->tls_sec->alignment_power); 10816 elf_hash_table (info)->tls_size = end - base; 10817 } 10818 10819 /* Reorder SHF_LINK_ORDER sections. */ 10820 for (o = abfd->sections; o != NULL; o = o->next) 10821 { 10822 if (!elf_fixup_link_order (abfd, o)) 10823 return FALSE; 10824 } 10825 10826 /* Since ELF permits relocations to be against local symbols, we 10827 must have the local symbols available when we do the relocations. 10828 Since we would rather only read the local symbols once, and we 10829 would rather not keep them in memory, we handle all the 10830 relocations for a single input file at the same time. 10831 10832 Unfortunately, there is no way to know the total number of local 10833 symbols until we have seen all of them, and the local symbol 10834 indices precede the global symbol indices. This means that when 10835 we are generating relocatable output, and we see a reloc against 10836 a global symbol, we can not know the symbol index until we have 10837 finished examining all the local symbols to see which ones we are 10838 going to output. To deal with this, we keep the relocations in 10839 memory, and don't output them until the end of the link. This is 10840 an unfortunate waste of memory, but I don't see a good way around 10841 it. Fortunately, it only happens when performing a relocatable 10842 link, which is not the common case. FIXME: If keep_memory is set 10843 we could write the relocs out and then read them again; I don't 10844 know how bad the memory loss will be. */ 10845 10846 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 10847 sub->output_has_begun = FALSE; 10848 for (o = abfd->sections; o != NULL; o = o->next) 10849 { 10850 for (p = o->map_head.link_order; p != NULL; p = p->next) 10851 { 10852 if (p->type == bfd_indirect_link_order 10853 && (bfd_get_flavour ((sub = p->u.indirect.section->owner)) 10854 == bfd_target_elf_flavour) 10855 && elf_elfheader (sub)->e_ident[EI_CLASS] == bed->s->elfclass) 10856 { 10857 if (! sub->output_has_begun) 10858 { 10859 if (! elf_link_input_bfd (&flinfo, sub)) 10860 goto error_return; 10861 sub->output_has_begun = TRUE; 10862 } 10863 } 10864 else if (p->type == bfd_section_reloc_link_order 10865 || p->type == bfd_symbol_reloc_link_order) 10866 { 10867 if (! elf_reloc_link_order (abfd, info, o, p)) 10868 goto error_return; 10869 } 10870 else 10871 { 10872 if (! _bfd_default_link_order (abfd, info, o, p)) 10873 { 10874 if (p->type == bfd_indirect_link_order 10875 && (bfd_get_flavour (sub) 10876 == bfd_target_elf_flavour) 10877 && (elf_elfheader (sub)->e_ident[EI_CLASS] 10878 != bed->s->elfclass)) 10879 { 10880 const char *iclass, *oclass; 10881 10882 if (bed->s->elfclass == ELFCLASS64) 10883 { 10884 iclass = "ELFCLASS32"; 10885 oclass = "ELFCLASS64"; 10886 } 10887 else 10888 { 10889 iclass = "ELFCLASS64"; 10890 oclass = "ELFCLASS32"; 10891 } 10892 10893 bfd_set_error (bfd_error_wrong_format); 10894 (*_bfd_error_handler) 10895 (_("%B: file class %s incompatible with %s"), 10896 sub, iclass, oclass); 10897 } 10898 10899 goto error_return; 10900 } 10901 } 10902 } 10903 } 10904 10905 /* Free symbol buffer if needed. */ 10906 if (!info->reduce_memory_overheads) 10907 { 10908 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 10909 if (bfd_get_flavour (sub) == bfd_target_elf_flavour 10910 && elf_tdata (sub)->symbuf) 10911 { 10912 free (elf_tdata (sub)->symbuf); 10913 elf_tdata (sub)->symbuf = NULL; 10914 } 10915 } 10916 10917 /* Output a FILE symbol so that following locals are not associated 10918 with the wrong input file. */ 10919 memset (&elfsym, 0, sizeof (elfsym)); 10920 elfsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE); 10921 elfsym.st_shndx = SHN_ABS; 10922 10923 if (flinfo.filesym_count > 1 10924 && !elf_link_output_sym (&flinfo, NULL, &elfsym, 10925 bfd_und_section_ptr, NULL)) 10926 return FALSE; 10927 10928 /* Output any global symbols that got converted to local in a 10929 version script or due to symbol visibility. We do this in a 10930 separate step since ELF requires all local symbols to appear 10931 prior to any global symbols. FIXME: We should only do this if 10932 some global symbols were, in fact, converted to become local. 10933 FIXME: Will this work correctly with the Irix 5 linker? */ 10934 eoinfo.failed = FALSE; 10935 eoinfo.flinfo = &flinfo; 10936 eoinfo.localsyms = TRUE; 10937 eoinfo.need_second_pass = FALSE; 10938 eoinfo.second_pass = FALSE; 10939 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 10940 if (eoinfo.failed) 10941 return FALSE; 10942 10943 if (flinfo.filesym_count == 1 10944 && !elf_link_output_sym (&flinfo, NULL, &elfsym, 10945 bfd_und_section_ptr, NULL)) 10946 return FALSE; 10947 10948 if (eoinfo.need_second_pass) 10949 { 10950 eoinfo.second_pass = TRUE; 10951 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 10952 if (eoinfo.failed) 10953 return FALSE; 10954 } 10955 10956 /* If backend needs to output some local symbols not present in the hash 10957 table, do it now. */ 10958 if (bed->elf_backend_output_arch_local_syms) 10959 { 10960 typedef int (*out_sym_func) 10961 (void *, const char *, Elf_Internal_Sym *, asection *, 10962 struct elf_link_hash_entry *); 10963 10964 if (! ((*bed->elf_backend_output_arch_local_syms) 10965 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym))) 10966 return FALSE; 10967 } 10968 10969 /* That wrote out all the local symbols. Finish up the symbol table 10970 with the global symbols. Even if we want to strip everything we 10971 can, we still need to deal with those global symbols that got 10972 converted to local in a version script. */ 10973 10974 /* The sh_info field records the index of the first non local symbol. */ 10975 symtab_hdr->sh_info = bfd_get_symcount (abfd); 10976 10977 if (dynamic 10978 && flinfo.dynsym_sec != NULL 10979 && flinfo.dynsym_sec->output_section != bfd_abs_section_ptr) 10980 { 10981 Elf_Internal_Sym sym; 10982 bfd_byte *dynsym = flinfo.dynsym_sec->contents; 10983 long last_local = 0; 10984 10985 /* Write out the section symbols for the output sections. */ 10986 if (info->shared || elf_hash_table (info)->is_relocatable_executable) 10987 { 10988 asection *s; 10989 10990 sym.st_size = 0; 10991 sym.st_name = 0; 10992 sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_SECTION); 10993 sym.st_other = 0; 10994 sym.st_target_internal = 0; 10995 10996 for (s = abfd->sections; s != NULL; s = s->next) 10997 { 10998 int indx; 10999 bfd_byte *dest; 11000 long dynindx; 11001 11002 dynindx = elf_section_data (s)->dynindx; 11003 if (dynindx <= 0) 11004 continue; 11005 indx = elf_section_data (s)->this_idx; 11006 BFD_ASSERT (indx > 0); 11007 sym.st_shndx = indx; 11008 if (! check_dynsym (abfd, &sym)) 11009 return FALSE; 11010 sym.st_value = s->vma; 11011 dest = dynsym + dynindx * bed->s->sizeof_sym; 11012 if (last_local < dynindx) 11013 last_local = dynindx; 11014 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 11015 } 11016 } 11017 11018 /* Write out the local dynsyms. */ 11019 if (elf_hash_table (info)->dynlocal) 11020 { 11021 struct elf_link_local_dynamic_entry *e; 11022 for (e = elf_hash_table (info)->dynlocal; e ; e = e->next) 11023 { 11024 asection *s; 11025 bfd_byte *dest; 11026 11027 /* Copy the internal symbol and turn off visibility. 11028 Note that we saved a word of storage and overwrote 11029 the original st_name with the dynstr_index. */ 11030 sym = e->isym; 11031 sym.st_other &= ~ELF_ST_VISIBILITY (-1); 11032 11033 s = bfd_section_from_elf_index (e->input_bfd, 11034 e->isym.st_shndx); 11035 if (s != NULL) 11036 { 11037 sym.st_shndx = 11038 elf_section_data (s->output_section)->this_idx; 11039 if (! check_dynsym (abfd, &sym)) 11040 return FALSE; 11041 sym.st_value = (s->output_section->vma 11042 + s->output_offset 11043 + e->isym.st_value); 11044 } 11045 11046 if (last_local < e->dynindx) 11047 last_local = e->dynindx; 11048 11049 dest = dynsym + e->dynindx * bed->s->sizeof_sym; 11050 bed->s->swap_symbol_out (abfd, &sym, dest, 0); 11051 } 11052 } 11053 11054 elf_section_data (flinfo.dynsym_sec->output_section)->this_hdr.sh_info = 11055 last_local + 1; 11056 } 11057 11058 /* We get the global symbols from the hash table. */ 11059 eoinfo.failed = FALSE; 11060 eoinfo.localsyms = FALSE; 11061 eoinfo.flinfo = &flinfo; 11062 bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo); 11063 if (eoinfo.failed) 11064 return FALSE; 11065 11066 /* If backend needs to output some symbols not present in the hash 11067 table, do it now. */ 11068 if (bed->elf_backend_output_arch_syms) 11069 { 11070 typedef int (*out_sym_func) 11071 (void *, const char *, Elf_Internal_Sym *, asection *, 11072 struct elf_link_hash_entry *); 11073 11074 if (! ((*bed->elf_backend_output_arch_syms) 11075 (abfd, info, &flinfo, (out_sym_func) elf_link_output_sym))) 11076 return FALSE; 11077 } 11078 11079 /* Flush all symbols to the file. */ 11080 if (! elf_link_flush_output_syms (&flinfo, bed)) 11081 return FALSE; 11082 11083 /* Now we know the size of the symtab section. */ 11084 off += symtab_hdr->sh_size; 11085 11086 symtab_shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; 11087 if (symtab_shndx_hdr->sh_name != 0) 11088 { 11089 symtab_shndx_hdr->sh_type = SHT_SYMTAB_SHNDX; 11090 symtab_shndx_hdr->sh_entsize = sizeof (Elf_External_Sym_Shndx); 11091 symtab_shndx_hdr->sh_addralign = sizeof (Elf_External_Sym_Shndx); 11092 amt = bfd_get_symcount (abfd) * sizeof (Elf_External_Sym_Shndx); 11093 symtab_shndx_hdr->sh_size = amt; 11094 11095 off = _bfd_elf_assign_file_position_for_section (symtab_shndx_hdr, 11096 off, TRUE); 11097 11098 if (bfd_seek (abfd, symtab_shndx_hdr->sh_offset, SEEK_SET) != 0 11099 || (bfd_bwrite (flinfo.symshndxbuf, amt, abfd) != amt)) 11100 return FALSE; 11101 } 11102 11103 11104 /* Finish up and write out the symbol string table (.strtab) 11105 section. */ 11106 symstrtab_hdr = &elf_tdata (abfd)->strtab_hdr; 11107 /* sh_name was set in prep_headers. */ 11108 symstrtab_hdr->sh_type = SHT_STRTAB; 11109 symstrtab_hdr->sh_flags = 0; 11110 symstrtab_hdr->sh_addr = 0; 11111 symstrtab_hdr->sh_size = _bfd_stringtab_size (flinfo.symstrtab); 11112 symstrtab_hdr->sh_entsize = 0; 11113 symstrtab_hdr->sh_link = 0; 11114 symstrtab_hdr->sh_info = 0; 11115 /* sh_offset is set just below. */ 11116 symstrtab_hdr->sh_addralign = 1; 11117 11118 off = _bfd_elf_assign_file_position_for_section (symstrtab_hdr, off, TRUE); 11119 elf_tdata (abfd)->next_file_pos = off; 11120 11121 if (bfd_get_symcount (abfd) > 0) 11122 { 11123 if (bfd_seek (abfd, symstrtab_hdr->sh_offset, SEEK_SET) != 0 11124 || ! _bfd_stringtab_emit (abfd, flinfo.symstrtab)) 11125 return FALSE; 11126 } 11127 11128 /* Adjust the relocs to have the correct symbol indices. */ 11129 for (o = abfd->sections; o != NULL; o = o->next) 11130 { 11131 struct bfd_elf_section_data *esdo = elf_section_data (o); 11132 if ((o->flags & SEC_RELOC) == 0) 11133 continue; 11134 11135 if (esdo->rel.hdr != NULL) 11136 elf_link_adjust_relocs (abfd, &esdo->rel); 11137 if (esdo->rela.hdr != NULL) 11138 elf_link_adjust_relocs (abfd, &esdo->rela); 11139 11140 /* Set the reloc_count field to 0 to prevent write_relocs from 11141 trying to swap the relocs out itself. */ 11142 o->reloc_count = 0; 11143 } 11144 11145 if (dynamic && info->combreloc && dynobj != NULL) 11146 relativecount = elf_link_sort_relocs (abfd, info, &reldyn); 11147 11148 /* If we are linking against a dynamic object, or generating a 11149 shared library, finish up the dynamic linking information. */ 11150 if (dynamic) 11151 { 11152 bfd_byte *dyncon, *dynconend; 11153 11154 /* Fix up .dynamic entries. */ 11155 o = bfd_get_linker_section (dynobj, ".dynamic"); 11156 BFD_ASSERT (o != NULL); 11157 11158 dyncon = o->contents; 11159 dynconend = o->contents + o->size; 11160 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 11161 { 11162 Elf_Internal_Dyn dyn; 11163 const char *name; 11164 unsigned int type; 11165 11166 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 11167 11168 switch (dyn.d_tag) 11169 { 11170 default: 11171 continue; 11172 case DT_NULL: 11173 if (relativecount > 0 && dyncon + bed->s->sizeof_dyn < dynconend) 11174 { 11175 switch (elf_section_data (reldyn)->this_hdr.sh_type) 11176 { 11177 case SHT_REL: dyn.d_tag = DT_RELCOUNT; break; 11178 case SHT_RELA: dyn.d_tag = DT_RELACOUNT; break; 11179 default: continue; 11180 } 11181 dyn.d_un.d_val = relativecount; 11182 relativecount = 0; 11183 break; 11184 } 11185 continue; 11186 11187 case DT_INIT: 11188 name = info->init_function; 11189 goto get_sym; 11190 case DT_FINI: 11191 name = info->fini_function; 11192 get_sym: 11193 { 11194 struct elf_link_hash_entry *h; 11195 11196 h = elf_link_hash_lookup (elf_hash_table (info), name, 11197 FALSE, FALSE, TRUE); 11198 if (h != NULL 11199 && (h->root.type == bfd_link_hash_defined 11200 || h->root.type == bfd_link_hash_defweak)) 11201 { 11202 dyn.d_un.d_ptr = h->root.u.def.value; 11203 o = h->root.u.def.section; 11204 if (o->output_section != NULL) 11205 dyn.d_un.d_ptr += (o->output_section->vma 11206 + o->output_offset); 11207 else 11208 { 11209 /* The symbol is imported from another shared 11210 library and does not apply to this one. */ 11211 dyn.d_un.d_ptr = 0; 11212 } 11213 break; 11214 } 11215 } 11216 continue; 11217 11218 case DT_PREINIT_ARRAYSZ: 11219 name = ".preinit_array"; 11220 goto get_size; 11221 case DT_INIT_ARRAYSZ: 11222 name = ".init_array"; 11223 goto get_size; 11224 case DT_FINI_ARRAYSZ: 11225 name = ".fini_array"; 11226 get_size: 11227 o = bfd_get_section_by_name (abfd, name); 11228 if (o == NULL) 11229 { 11230 (*_bfd_error_handler) 11231 (_("%B: could not find output section %s"), abfd, name); 11232 goto error_return; 11233 } 11234 if (o->size == 0) 11235 (*_bfd_error_handler) 11236 (_("warning: %s section has zero size"), name); 11237 dyn.d_un.d_val = o->size; 11238 break; 11239 11240 case DT_PREINIT_ARRAY: 11241 name = ".preinit_array"; 11242 goto get_vma; 11243 case DT_INIT_ARRAY: 11244 name = ".init_array"; 11245 goto get_vma; 11246 case DT_FINI_ARRAY: 11247 name = ".fini_array"; 11248 goto get_vma; 11249 11250 case DT_HASH: 11251 name = ".hash"; 11252 goto get_vma; 11253 case DT_GNU_HASH: 11254 name = ".gnu.hash"; 11255 goto get_vma; 11256 case DT_STRTAB: 11257 name = ".dynstr"; 11258 goto get_vma; 11259 case DT_SYMTAB: 11260 name = ".dynsym"; 11261 goto get_vma; 11262 case DT_VERDEF: 11263 name = ".gnu.version_d"; 11264 goto get_vma; 11265 case DT_VERNEED: 11266 name = ".gnu.version_r"; 11267 goto get_vma; 11268 case DT_VERSYM: 11269 name = ".gnu.version"; 11270 get_vma: 11271 o = bfd_get_section_by_name (abfd, name); 11272 if (o == NULL) 11273 { 11274 (*_bfd_error_handler) 11275 (_("%B: could not find output section %s"), abfd, name); 11276 goto error_return; 11277 } 11278 if (elf_section_data (o->output_section)->this_hdr.sh_type == SHT_NOTE) 11279 { 11280 (*_bfd_error_handler) 11281 (_("warning: section '%s' is being made into a note"), name); 11282 bfd_set_error (bfd_error_nonrepresentable_section); 11283 goto error_return; 11284 } 11285 dyn.d_un.d_ptr = o->vma; 11286 break; 11287 11288 case DT_REL: 11289 case DT_RELA: 11290 case DT_RELSZ: 11291 case DT_RELASZ: 11292 if (dyn.d_tag == DT_REL || dyn.d_tag == DT_RELSZ) 11293 type = SHT_REL; 11294 else 11295 type = SHT_RELA; 11296 dyn.d_un.d_val = 0; 11297 dyn.d_un.d_ptr = 0; 11298 for (i = 1; i < elf_numsections (abfd); i++) 11299 { 11300 Elf_Internal_Shdr *hdr; 11301 11302 hdr = elf_elfsections (abfd)[i]; 11303 if (hdr->sh_type == type 11304 && (hdr->sh_flags & SHF_ALLOC) != 0) 11305 { 11306 if (dyn.d_tag == DT_RELSZ || dyn.d_tag == DT_RELASZ) 11307 dyn.d_un.d_val += hdr->sh_size; 11308 else 11309 { 11310 if (dyn.d_un.d_ptr == 0 11311 || hdr->sh_addr < dyn.d_un.d_ptr) 11312 dyn.d_un.d_ptr = hdr->sh_addr; 11313 } 11314 } 11315 } 11316 break; 11317 } 11318 bed->s->swap_dyn_out (dynobj, &dyn, dyncon); 11319 } 11320 } 11321 11322 /* If we have created any dynamic sections, then output them. */ 11323 if (dynobj != NULL) 11324 { 11325 if (! (*bed->elf_backend_finish_dynamic_sections) (abfd, info)) 11326 goto error_return; 11327 11328 /* Check for DT_TEXTREL (late, in case the backend removes it). */ 11329 if (((info->warn_shared_textrel && info->shared) 11330 || info->error_textrel) 11331 && (o = bfd_get_linker_section (dynobj, ".dynamic")) != NULL) 11332 { 11333 bfd_byte *dyncon, *dynconend; 11334 11335 dyncon = o->contents; 11336 dynconend = o->contents + o->size; 11337 for (; dyncon < dynconend; dyncon += bed->s->sizeof_dyn) 11338 { 11339 Elf_Internal_Dyn dyn; 11340 11341 bed->s->swap_dyn_in (dynobj, dyncon, &dyn); 11342 11343 if (dyn.d_tag == DT_TEXTREL) 11344 { 11345 if (info->error_textrel) 11346 info->callbacks->einfo 11347 (_("%P%X: read-only segment has dynamic relocations.\n")); 11348 else 11349 info->callbacks->einfo 11350 (_("%P: warning: creating a DT_TEXTREL in a shared object.\n")); 11351 break; 11352 } 11353 } 11354 } 11355 11356 for (o = dynobj->sections; o != NULL; o = o->next) 11357 { 11358 if ((o->flags & SEC_HAS_CONTENTS) == 0 11359 || o->size == 0 11360 || o->output_section == bfd_abs_section_ptr) 11361 continue; 11362 if ((o->flags & SEC_LINKER_CREATED) == 0) 11363 { 11364 /* At this point, we are only interested in sections 11365 created by _bfd_elf_link_create_dynamic_sections. */ 11366 continue; 11367 } 11368 if (elf_hash_table (info)->stab_info.stabstr == o) 11369 continue; 11370 if (elf_hash_table (info)->eh_info.hdr_sec == o) 11371 continue; 11372 if (strcmp (o->name, ".dynstr") != 0) 11373 { 11374 /* FIXME: octets_per_byte. */ 11375 if (! bfd_set_section_contents (abfd, o->output_section, 11376 o->contents, 11377 (file_ptr) o->output_offset, 11378 o->size)) 11379 goto error_return; 11380 } 11381 else 11382 { 11383 /* The contents of the .dynstr section are actually in a 11384 stringtab. */ 11385 off = elf_section_data (o->output_section)->this_hdr.sh_offset; 11386 if (bfd_seek (abfd, off, SEEK_SET) != 0 11387 || ! _bfd_elf_strtab_emit (abfd, 11388 elf_hash_table (info)->dynstr)) 11389 goto error_return; 11390 } 11391 } 11392 } 11393 11394 if (info->relocatable) 11395 { 11396 bfd_boolean failed = FALSE; 11397 11398 bfd_map_over_sections (abfd, bfd_elf_set_group_contents, &failed); 11399 if (failed) 11400 goto error_return; 11401 } 11402 11403 /* If we have optimized stabs strings, output them. */ 11404 if (elf_hash_table (info)->stab_info.stabstr != NULL) 11405 { 11406 if (! _bfd_write_stab_strings (abfd, &elf_hash_table (info)->stab_info)) 11407 goto error_return; 11408 } 11409 11410 if (info->eh_frame_hdr) 11411 { 11412 if (! _bfd_elf_write_section_eh_frame_hdr (abfd, info)) 11413 goto error_return; 11414 } 11415 11416 if (flinfo.symstrtab != NULL) 11417 _bfd_stringtab_free (flinfo.symstrtab); 11418 if (flinfo.contents != NULL) 11419 free (flinfo.contents); 11420 if (flinfo.external_relocs != NULL) 11421 free (flinfo.external_relocs); 11422 if (flinfo.internal_relocs != NULL) 11423 free (flinfo.internal_relocs); 11424 if (flinfo.external_syms != NULL) 11425 free (flinfo.external_syms); 11426 if (flinfo.locsym_shndx != NULL) 11427 free (flinfo.locsym_shndx); 11428 if (flinfo.internal_syms != NULL) 11429 free (flinfo.internal_syms); 11430 if (flinfo.indices != NULL) 11431 free (flinfo.indices); 11432 if (flinfo.sections != NULL) 11433 free (flinfo.sections); 11434 if (flinfo.symbuf != NULL) 11435 free (flinfo.symbuf); 11436 if (flinfo.symshndxbuf != NULL) 11437 free (flinfo.symshndxbuf); 11438 for (o = abfd->sections; o != NULL; o = o->next) 11439 { 11440 struct bfd_elf_section_data *esdo = elf_section_data (o); 11441 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) 11442 free (esdo->rel.hashes); 11443 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) 11444 free (esdo->rela.hashes); 11445 } 11446 11447 elf_tdata (abfd)->linker = TRUE; 11448 11449 if (attr_section) 11450 { 11451 bfd_byte *contents = (bfd_byte *) bfd_malloc (attr_size); 11452 if (contents == NULL) 11453 return FALSE; /* Bail out and fail. */ 11454 bfd_elf_set_obj_attr_contents (abfd, contents, attr_size); 11455 bfd_set_section_contents (abfd, attr_section, contents, 0, attr_size); 11456 free (contents); 11457 } 11458 11459 return TRUE; 11460 11461 error_return: 11462 if (flinfo.symstrtab != NULL) 11463 _bfd_stringtab_free (flinfo.symstrtab); 11464 if (flinfo.contents != NULL) 11465 free (flinfo.contents); 11466 if (flinfo.external_relocs != NULL) 11467 free (flinfo.external_relocs); 11468 if (flinfo.internal_relocs != NULL) 11469 free (flinfo.internal_relocs); 11470 if (flinfo.external_syms != NULL) 11471 free (flinfo.external_syms); 11472 if (flinfo.locsym_shndx != NULL) 11473 free (flinfo.locsym_shndx); 11474 if (flinfo.internal_syms != NULL) 11475 free (flinfo.internal_syms); 11476 if (flinfo.indices != NULL) 11477 free (flinfo.indices); 11478 if (flinfo.sections != NULL) 11479 free (flinfo.sections); 11480 if (flinfo.symbuf != NULL) 11481 free (flinfo.symbuf); 11482 if (flinfo.symshndxbuf != NULL) 11483 free (flinfo.symshndxbuf); 11484 for (o = abfd->sections; o != NULL; o = o->next) 11485 { 11486 struct bfd_elf_section_data *esdo = elf_section_data (o); 11487 if ((o->flags & SEC_RELOC) != 0 && esdo->rel.hashes != NULL) 11488 free (esdo->rel.hashes); 11489 if ((o->flags & SEC_RELOC) != 0 && esdo->rela.hashes != NULL) 11490 free (esdo->rela.hashes); 11491 } 11492 11493 return FALSE; 11494 } 11495 11496 /* Initialize COOKIE for input bfd ABFD. */ 11497 11498 static bfd_boolean 11499 init_reloc_cookie (struct elf_reloc_cookie *cookie, 11500 struct bfd_link_info *info, bfd *abfd) 11501 { 11502 Elf_Internal_Shdr *symtab_hdr; 11503 const struct elf_backend_data *bed; 11504 11505 bed = get_elf_backend_data (abfd); 11506 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 11507 11508 cookie->abfd = abfd; 11509 cookie->sym_hashes = elf_sym_hashes (abfd); 11510 cookie->bad_symtab = elf_bad_symtab (abfd); 11511 if (cookie->bad_symtab) 11512 { 11513 cookie->locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 11514 cookie->extsymoff = 0; 11515 } 11516 else 11517 { 11518 cookie->locsymcount = symtab_hdr->sh_info; 11519 cookie->extsymoff = symtab_hdr->sh_info; 11520 } 11521 11522 if (bed->s->arch_size == 32) 11523 cookie->r_sym_shift = 8; 11524 else 11525 cookie->r_sym_shift = 32; 11526 11527 cookie->locsyms = (Elf_Internal_Sym *) symtab_hdr->contents; 11528 if (cookie->locsyms == NULL && cookie->locsymcount != 0) 11529 { 11530 cookie->locsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, 11531 cookie->locsymcount, 0, 11532 NULL, NULL, NULL); 11533 if (cookie->locsyms == NULL) 11534 { 11535 info->callbacks->einfo (_("%P%X: can not read symbols: %E\n")); 11536 return FALSE; 11537 } 11538 if (info->keep_memory) 11539 symtab_hdr->contents = (bfd_byte *) cookie->locsyms; 11540 } 11541 return TRUE; 11542 } 11543 11544 /* Free the memory allocated by init_reloc_cookie, if appropriate. */ 11545 11546 static void 11547 fini_reloc_cookie (struct elf_reloc_cookie *cookie, bfd *abfd) 11548 { 11549 Elf_Internal_Shdr *symtab_hdr; 11550 11551 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 11552 if (cookie->locsyms != NULL 11553 && symtab_hdr->contents != (unsigned char *) cookie->locsyms) 11554 free (cookie->locsyms); 11555 } 11556 11557 /* Initialize the relocation information in COOKIE for input section SEC 11558 of input bfd ABFD. */ 11559 11560 static bfd_boolean 11561 init_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 11562 struct bfd_link_info *info, bfd *abfd, 11563 asection *sec) 11564 { 11565 const struct elf_backend_data *bed; 11566 11567 if (sec->reloc_count == 0) 11568 { 11569 cookie->rels = NULL; 11570 cookie->relend = NULL; 11571 } 11572 else 11573 { 11574 bed = get_elf_backend_data (abfd); 11575 11576 cookie->rels = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 11577 info->keep_memory); 11578 if (cookie->rels == NULL) 11579 return FALSE; 11580 cookie->rel = cookie->rels; 11581 cookie->relend = (cookie->rels 11582 + sec->reloc_count * bed->s->int_rels_per_ext_rel); 11583 } 11584 cookie->rel = cookie->rels; 11585 return TRUE; 11586 } 11587 11588 /* Free the memory allocated by init_reloc_cookie_rels, 11589 if appropriate. */ 11590 11591 static void 11592 fini_reloc_cookie_rels (struct elf_reloc_cookie *cookie, 11593 asection *sec) 11594 { 11595 if (cookie->rels && elf_section_data (sec)->relocs != cookie->rels) 11596 free (cookie->rels); 11597 } 11598 11599 /* Initialize the whole of COOKIE for input section SEC. */ 11600 11601 static bfd_boolean 11602 init_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 11603 struct bfd_link_info *info, 11604 asection *sec) 11605 { 11606 if (!init_reloc_cookie (cookie, info, sec->owner)) 11607 goto error1; 11608 if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec)) 11609 goto error2; 11610 return TRUE; 11611 11612 error2: 11613 fini_reloc_cookie (cookie, sec->owner); 11614 error1: 11615 return FALSE; 11616 } 11617 11618 /* Free the memory allocated by init_reloc_cookie_for_section, 11619 if appropriate. */ 11620 11621 static void 11622 fini_reloc_cookie_for_section (struct elf_reloc_cookie *cookie, 11623 asection *sec) 11624 { 11625 fini_reloc_cookie_rels (cookie, sec); 11626 fini_reloc_cookie (cookie, sec->owner); 11627 } 11628 11629 /* Garbage collect unused sections. */ 11630 11631 /* Default gc_mark_hook. */ 11632 11633 asection * 11634 _bfd_elf_gc_mark_hook (asection *sec, 11635 struct bfd_link_info *info ATTRIBUTE_UNUSED, 11636 Elf_Internal_Rela *rel ATTRIBUTE_UNUSED, 11637 struct elf_link_hash_entry *h, 11638 Elf_Internal_Sym *sym) 11639 { 11640 const char *sec_name; 11641 11642 if (h != NULL) 11643 { 11644 switch (h->root.type) 11645 { 11646 case bfd_link_hash_defined: 11647 case bfd_link_hash_defweak: 11648 return h->root.u.def.section; 11649 11650 case bfd_link_hash_common: 11651 return h->root.u.c.p->section; 11652 11653 case bfd_link_hash_undefined: 11654 case bfd_link_hash_undefweak: 11655 /* To work around a glibc bug, keep all XXX input sections 11656 when there is an as yet undefined reference to __start_XXX 11657 or __stop_XXX symbols. The linker will later define such 11658 symbols for orphan input sections that have a name 11659 representable as a C identifier. */ 11660 if (strncmp (h->root.root.string, "__start_", 8) == 0) 11661 sec_name = h->root.root.string + 8; 11662 else if (strncmp (h->root.root.string, "__stop_", 7) == 0) 11663 sec_name = h->root.root.string + 7; 11664 else 11665 sec_name = NULL; 11666 11667 if (sec_name && *sec_name != '\0') 11668 { 11669 bfd *i; 11670 11671 for (i = info->input_bfds; i; i = i->link_next) 11672 { 11673 sec = bfd_get_section_by_name (i, sec_name); 11674 if (sec) 11675 sec->flags |= SEC_KEEP; 11676 } 11677 } 11678 break; 11679 11680 default: 11681 break; 11682 } 11683 } 11684 else 11685 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 11686 11687 return NULL; 11688 } 11689 11690 /* COOKIE->rel describes a relocation against section SEC, which is 11691 a section we've decided to keep. Return the section that contains 11692 the relocation symbol, or NULL if no section contains it. */ 11693 11694 asection * 11695 _bfd_elf_gc_mark_rsec (struct bfd_link_info *info, asection *sec, 11696 elf_gc_mark_hook_fn gc_mark_hook, 11697 struct elf_reloc_cookie *cookie) 11698 { 11699 unsigned long r_symndx; 11700 struct elf_link_hash_entry *h; 11701 11702 r_symndx = cookie->rel->r_info >> cookie->r_sym_shift; 11703 if (r_symndx == STN_UNDEF) 11704 return NULL; 11705 11706 if (r_symndx >= cookie->locsymcount 11707 || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL) 11708 { 11709 h = cookie->sym_hashes[r_symndx - cookie->extsymoff]; 11710 while (h->root.type == bfd_link_hash_indirect 11711 || h->root.type == bfd_link_hash_warning) 11712 h = (struct elf_link_hash_entry *) h->root.u.i.link; 11713 h->mark = 1; 11714 /* If this symbol is weak and there is a non-weak definition, we 11715 keep the non-weak definition because many backends put 11716 dynamic reloc info on the non-weak definition for code 11717 handling copy relocs. */ 11718 if (h->u.weakdef != NULL) 11719 h->u.weakdef->mark = 1; 11720 return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL); 11721 } 11722 11723 return (*gc_mark_hook) (sec, info, cookie->rel, NULL, 11724 &cookie->locsyms[r_symndx]); 11725 } 11726 11727 /* COOKIE->rel describes a relocation against section SEC, which is 11728 a section we've decided to keep. Mark the section that contains 11729 the relocation symbol. */ 11730 11731 bfd_boolean 11732 _bfd_elf_gc_mark_reloc (struct bfd_link_info *info, 11733 asection *sec, 11734 elf_gc_mark_hook_fn gc_mark_hook, 11735 struct elf_reloc_cookie *cookie) 11736 { 11737 asection *rsec; 11738 11739 rsec = _bfd_elf_gc_mark_rsec (info, sec, gc_mark_hook, cookie); 11740 if (rsec && !rsec->gc_mark) 11741 { 11742 if (bfd_get_flavour (rsec->owner) != bfd_target_elf_flavour 11743 || (rsec->owner->flags & DYNAMIC) != 0) 11744 rsec->gc_mark = 1; 11745 else if (!_bfd_elf_gc_mark (info, rsec, gc_mark_hook)) 11746 return FALSE; 11747 } 11748 return TRUE; 11749 } 11750 11751 /* The mark phase of garbage collection. For a given section, mark 11752 it and any sections in this section's group, and all the sections 11753 which define symbols to which it refers. */ 11754 11755 bfd_boolean 11756 _bfd_elf_gc_mark (struct bfd_link_info *info, 11757 asection *sec, 11758 elf_gc_mark_hook_fn gc_mark_hook) 11759 { 11760 bfd_boolean ret; 11761 asection *group_sec, *eh_frame; 11762 11763 sec->gc_mark = 1; 11764 11765 /* Mark all the sections in the group. */ 11766 group_sec = elf_section_data (sec)->next_in_group; 11767 if (group_sec && !group_sec->gc_mark) 11768 if (!_bfd_elf_gc_mark (info, group_sec, gc_mark_hook)) 11769 return FALSE; 11770 11771 /* Look through the section relocs. */ 11772 ret = TRUE; 11773 eh_frame = elf_eh_frame_section (sec->owner); 11774 if ((sec->flags & SEC_RELOC) != 0 11775 && sec->reloc_count > 0 11776 && sec != eh_frame) 11777 { 11778 struct elf_reloc_cookie cookie; 11779 11780 if (!init_reloc_cookie_for_section (&cookie, info, sec)) 11781 ret = FALSE; 11782 else 11783 { 11784 for (; cookie.rel < cookie.relend; cookie.rel++) 11785 if (!_bfd_elf_gc_mark_reloc (info, sec, gc_mark_hook, &cookie)) 11786 { 11787 ret = FALSE; 11788 break; 11789 } 11790 fini_reloc_cookie_for_section (&cookie, sec); 11791 } 11792 } 11793 11794 if (ret && eh_frame && elf_fde_list (sec)) 11795 { 11796 struct elf_reloc_cookie cookie; 11797 11798 if (!init_reloc_cookie_for_section (&cookie, info, eh_frame)) 11799 ret = FALSE; 11800 else 11801 { 11802 if (!_bfd_elf_gc_mark_fdes (info, sec, eh_frame, 11803 gc_mark_hook, &cookie)) 11804 ret = FALSE; 11805 fini_reloc_cookie_for_section (&cookie, eh_frame); 11806 } 11807 } 11808 11809 return ret; 11810 } 11811 11812 /* Keep debug and special sections. */ 11813 11814 bfd_boolean 11815 _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info, 11816 elf_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED) 11817 { 11818 bfd *ibfd; 11819 11820 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next) 11821 { 11822 asection *isec; 11823 bfd_boolean some_kept; 11824 11825 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 11826 continue; 11827 11828 /* Ensure all linker created sections are kept, and see whether 11829 any other section is already marked. */ 11830 some_kept = FALSE; 11831 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 11832 { 11833 if ((isec->flags & SEC_LINKER_CREATED) != 0) 11834 isec->gc_mark = 1; 11835 else if (isec->gc_mark) 11836 some_kept = TRUE; 11837 } 11838 11839 /* If no section in this file will be kept, then we can 11840 toss out debug sections. */ 11841 if (!some_kept) 11842 continue; 11843 11844 /* Keep debug and special sections like .comment when they are 11845 not part of a group, or when we have single-member groups. */ 11846 for (isec = ibfd->sections; isec != NULL; isec = isec->next) 11847 if ((elf_next_in_group (isec) == NULL 11848 || elf_next_in_group (isec) == isec) 11849 && ((isec->flags & SEC_DEBUGGING) != 0 11850 || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)) 11851 isec->gc_mark = 1; 11852 } 11853 return TRUE; 11854 } 11855 11856 /* Sweep symbols in swept sections. Called via elf_link_hash_traverse. */ 11857 11858 struct elf_gc_sweep_symbol_info 11859 { 11860 struct bfd_link_info *info; 11861 void (*hide_symbol) (struct bfd_link_info *, struct elf_link_hash_entry *, 11862 bfd_boolean); 11863 }; 11864 11865 static bfd_boolean 11866 elf_gc_sweep_symbol (struct elf_link_hash_entry *h, void *data) 11867 { 11868 if (!h->mark 11869 && (((h->root.type == bfd_link_hash_defined 11870 || h->root.type == bfd_link_hash_defweak) 11871 && !(h->def_regular 11872 && h->root.u.def.section->gc_mark)) 11873 || h->root.type == bfd_link_hash_undefined 11874 || h->root.type == bfd_link_hash_undefweak)) 11875 { 11876 struct elf_gc_sweep_symbol_info *inf; 11877 11878 inf = (struct elf_gc_sweep_symbol_info *) data; 11879 (*inf->hide_symbol) (inf->info, h, TRUE); 11880 h->def_regular = 0; 11881 h->ref_regular = 0; 11882 h->ref_regular_nonweak = 0; 11883 } 11884 11885 return TRUE; 11886 } 11887 11888 /* The sweep phase of garbage collection. Remove all garbage sections. */ 11889 11890 typedef bfd_boolean (*gc_sweep_hook_fn) 11891 (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); 11892 11893 static bfd_boolean 11894 elf_gc_sweep (bfd *abfd, struct bfd_link_info *info) 11895 { 11896 bfd *sub; 11897 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 11898 gc_sweep_hook_fn gc_sweep_hook = bed->gc_sweep_hook; 11899 unsigned long section_sym_count; 11900 struct elf_gc_sweep_symbol_info sweep_info; 11901 11902 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 11903 { 11904 asection *o; 11905 11906 if (bfd_get_flavour (sub) != bfd_target_elf_flavour) 11907 continue; 11908 11909 for (o = sub->sections; o != NULL; o = o->next) 11910 { 11911 /* When any section in a section group is kept, we keep all 11912 sections in the section group. If the first member of 11913 the section group is excluded, we will also exclude the 11914 group section. */ 11915 if (o->flags & SEC_GROUP) 11916 { 11917 asection *first = elf_next_in_group (o); 11918 o->gc_mark = first->gc_mark; 11919 } 11920 11921 if (o->gc_mark) 11922 continue; 11923 11924 /* Skip sweeping sections already excluded. */ 11925 if (o->flags & SEC_EXCLUDE) 11926 continue; 11927 11928 /* Since this is early in the link process, it is simple 11929 to remove a section from the output. */ 11930 o->flags |= SEC_EXCLUDE; 11931 11932 if (info->print_gc_sections && o->size != 0) 11933 _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name); 11934 11935 /* But we also have to update some of the relocation 11936 info we collected before. */ 11937 if (gc_sweep_hook 11938 && (o->flags & SEC_RELOC) != 0 11939 && o->reloc_count > 0 11940 && !bfd_is_abs_section (o->output_section)) 11941 { 11942 Elf_Internal_Rela *internal_relocs; 11943 bfd_boolean r; 11944 11945 internal_relocs 11946 = _bfd_elf_link_read_relocs (o->owner, o, NULL, NULL, 11947 info->keep_memory); 11948 if (internal_relocs == NULL) 11949 return FALSE; 11950 11951 r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs); 11952 11953 if (elf_section_data (o)->relocs != internal_relocs) 11954 free (internal_relocs); 11955 11956 if (!r) 11957 return FALSE; 11958 } 11959 } 11960 } 11961 11962 /* Remove the symbols that were in the swept sections from the dynamic 11963 symbol table. GCFIXME: Anyone know how to get them out of the 11964 static symbol table as well? */ 11965 sweep_info.info = info; 11966 sweep_info.hide_symbol = bed->elf_backend_hide_symbol; 11967 elf_link_hash_traverse (elf_hash_table (info), elf_gc_sweep_symbol, 11968 &sweep_info); 11969 11970 _bfd_elf_link_renumber_dynsyms (abfd, info, §ion_sym_count); 11971 return TRUE; 11972 } 11973 11974 /* Propagate collected vtable information. This is called through 11975 elf_link_hash_traverse. */ 11976 11977 static bfd_boolean 11978 elf_gc_propagate_vtable_entries_used (struct elf_link_hash_entry *h, void *okp) 11979 { 11980 /* Those that are not vtables. */ 11981 if (h->vtable == NULL || h->vtable->parent == NULL) 11982 return TRUE; 11983 11984 /* Those vtables that do not have parents, we cannot merge. */ 11985 if (h->vtable->parent == (struct elf_link_hash_entry *) -1) 11986 return TRUE; 11987 11988 /* If we've already been done, exit. */ 11989 if (h->vtable->used && h->vtable->used[-1]) 11990 return TRUE; 11991 11992 /* Make sure the parent's table is up to date. */ 11993 elf_gc_propagate_vtable_entries_used (h->vtable->parent, okp); 11994 11995 if (h->vtable->used == NULL) 11996 { 11997 /* None of this table's entries were referenced. Re-use the 11998 parent's table. */ 11999 h->vtable->used = h->vtable->parent->vtable->used; 12000 h->vtable->size = h->vtable->parent->vtable->size; 12001 } 12002 else 12003 { 12004 size_t n; 12005 bfd_boolean *cu, *pu; 12006 12007 /* Or the parent's entries into ours. */ 12008 cu = h->vtable->used; 12009 cu[-1] = TRUE; 12010 pu = h->vtable->parent->vtable->used; 12011 if (pu != NULL) 12012 { 12013 const struct elf_backend_data *bed; 12014 unsigned int log_file_align; 12015 12016 bed = get_elf_backend_data (h->root.u.def.section->owner); 12017 log_file_align = bed->s->log_file_align; 12018 n = h->vtable->parent->vtable->size >> log_file_align; 12019 while (n--) 12020 { 12021 if (*pu) 12022 *cu = TRUE; 12023 pu++; 12024 cu++; 12025 } 12026 } 12027 } 12028 12029 return TRUE; 12030 } 12031 12032 static bfd_boolean 12033 elf_gc_smash_unused_vtentry_relocs (struct elf_link_hash_entry *h, void *okp) 12034 { 12035 asection *sec; 12036 bfd_vma hstart, hend; 12037 Elf_Internal_Rela *relstart, *relend, *rel; 12038 const struct elf_backend_data *bed; 12039 unsigned int log_file_align; 12040 12041 /* Take care of both those symbols that do not describe vtables as 12042 well as those that are not loaded. */ 12043 if (h->vtable == NULL || h->vtable->parent == NULL) 12044 return TRUE; 12045 12046 BFD_ASSERT (h->root.type == bfd_link_hash_defined 12047 || h->root.type == bfd_link_hash_defweak); 12048 12049 sec = h->root.u.def.section; 12050 hstart = h->root.u.def.value; 12051 hend = hstart + h->size; 12052 12053 relstart = _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, TRUE); 12054 if (!relstart) 12055 return *(bfd_boolean *) okp = FALSE; 12056 bed = get_elf_backend_data (sec->owner); 12057 log_file_align = bed->s->log_file_align; 12058 12059 relend = relstart + sec->reloc_count * bed->s->int_rels_per_ext_rel; 12060 12061 for (rel = relstart; rel < relend; ++rel) 12062 if (rel->r_offset >= hstart && rel->r_offset < hend) 12063 { 12064 /* If the entry is in use, do nothing. */ 12065 if (h->vtable->used 12066 && (rel->r_offset - hstart) < h->vtable->size) 12067 { 12068 bfd_vma entry = (rel->r_offset - hstart) >> log_file_align; 12069 if (h->vtable->used[entry]) 12070 continue; 12071 } 12072 /* Otherwise, kill it. */ 12073 rel->r_offset = rel->r_info = rel->r_addend = 0; 12074 } 12075 12076 return TRUE; 12077 } 12078 12079 /* Mark sections containing dynamically referenced symbols. When 12080 building shared libraries, we must assume that any visible symbol is 12081 referenced. */ 12082 12083 bfd_boolean 12084 bfd_elf_gc_mark_dynamic_ref_symbol (struct elf_link_hash_entry *h, void *inf) 12085 { 12086 struct bfd_link_info *info = (struct bfd_link_info *) inf; 12087 12088 if ((h->root.type == bfd_link_hash_defined 12089 || h->root.type == bfd_link_hash_defweak) 12090 && (h->ref_dynamic 12091 || ((!info->executable || info->export_dynamic) 12092 && h->def_regular 12093 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 12094 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 12095 && (strchr (h->root.root.string, ELF_VER_CHR) != NULL 12096 || !bfd_hide_sym_by_version (info->version_info, 12097 h->root.root.string))))) 12098 h->root.u.def.section->flags |= SEC_KEEP; 12099 12100 return TRUE; 12101 } 12102 12103 /* Keep all sections containing symbols undefined on the command-line, 12104 and the section containing the entry symbol. */ 12105 12106 void 12107 _bfd_elf_gc_keep (struct bfd_link_info *info) 12108 { 12109 struct bfd_sym_chain *sym; 12110 12111 for (sym = info->gc_sym_list; sym != NULL; sym = sym->next) 12112 { 12113 struct elf_link_hash_entry *h; 12114 12115 h = elf_link_hash_lookup (elf_hash_table (info), sym->name, 12116 FALSE, FALSE, FALSE); 12117 12118 if (h != NULL 12119 && (h->root.type == bfd_link_hash_defined 12120 || h->root.type == bfd_link_hash_defweak) 12121 && !bfd_is_abs_section (h->root.u.def.section)) 12122 h->root.u.def.section->flags |= SEC_KEEP; 12123 } 12124 } 12125 12126 /* Do mark and sweep of unused sections. */ 12127 12128 bfd_boolean 12129 bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info) 12130 { 12131 bfd_boolean ok = TRUE; 12132 bfd *sub; 12133 elf_gc_mark_hook_fn gc_mark_hook; 12134 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12135 12136 if (!bed->can_gc_sections 12137 || !is_elf_hash_table (info->hash)) 12138 { 12139 (*_bfd_error_handler)(_("Warning: gc-sections option ignored")); 12140 return TRUE; 12141 } 12142 12143 bed->gc_keep (info); 12144 12145 /* Try to parse each bfd's .eh_frame section. Point elf_eh_frame_section 12146 at the .eh_frame section if we can mark the FDEs individually. */ 12147 _bfd_elf_begin_eh_frame_parsing (info); 12148 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 12149 { 12150 asection *sec; 12151 struct elf_reloc_cookie cookie; 12152 12153 sec = bfd_get_section_by_name (sub, ".eh_frame"); 12154 while (sec && init_reloc_cookie_for_section (&cookie, info, sec)) 12155 { 12156 _bfd_elf_parse_eh_frame (sub, info, sec, &cookie); 12157 if (elf_section_data (sec)->sec_info 12158 && (sec->flags & SEC_LINKER_CREATED) == 0) 12159 elf_eh_frame_section (sub) = sec; 12160 fini_reloc_cookie_for_section (&cookie, sec); 12161 sec = bfd_get_next_section_by_name (sec); 12162 } 12163 } 12164 _bfd_elf_end_eh_frame_parsing (info); 12165 12166 /* Apply transitive closure to the vtable entry usage info. */ 12167 elf_link_hash_traverse (elf_hash_table (info), 12168 elf_gc_propagate_vtable_entries_used, 12169 &ok); 12170 if (!ok) 12171 return FALSE; 12172 12173 /* Kill the vtable relocations that were not used. */ 12174 elf_link_hash_traverse (elf_hash_table (info), 12175 elf_gc_smash_unused_vtentry_relocs, 12176 &ok); 12177 if (!ok) 12178 return FALSE; 12179 12180 /* Mark dynamically referenced symbols. */ 12181 if (elf_hash_table (info)->dynamic_sections_created) 12182 elf_link_hash_traverse (elf_hash_table (info), 12183 bed->gc_mark_dynamic_ref, 12184 info); 12185 12186 /* Grovel through relocs to find out who stays ... */ 12187 gc_mark_hook = bed->gc_mark_hook; 12188 for (sub = info->input_bfds; sub != NULL; sub = sub->link_next) 12189 { 12190 asection *o; 12191 12192 if (bfd_get_flavour (sub) != bfd_target_elf_flavour) 12193 continue; 12194 12195 /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep). 12196 Also treat note sections as a root, if the section is not part 12197 of a group. */ 12198 for (o = sub->sections; o != NULL; o = o->next) 12199 if (!o->gc_mark 12200 && (o->flags & SEC_EXCLUDE) == 0 12201 && ((o->flags & SEC_KEEP) != 0 12202 || (elf_section_data (o)->this_hdr.sh_type == SHT_NOTE 12203 && elf_next_in_group (o) == NULL ))) 12204 { 12205 if (!_bfd_elf_gc_mark (info, o, gc_mark_hook)) 12206 return FALSE; 12207 } 12208 } 12209 12210 /* Allow the backend to mark additional target specific sections. */ 12211 bed->gc_mark_extra_sections (info, gc_mark_hook); 12212 12213 /* ... and mark SEC_EXCLUDE for those that go. */ 12214 return elf_gc_sweep (abfd, info); 12215 } 12216 12217 /* Called from check_relocs to record the existence of a VTINHERIT reloc. */ 12218 12219 bfd_boolean 12220 bfd_elf_gc_record_vtinherit (bfd *abfd, 12221 asection *sec, 12222 struct elf_link_hash_entry *h, 12223 bfd_vma offset) 12224 { 12225 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 12226 struct elf_link_hash_entry **search, *child; 12227 bfd_size_type extsymcount; 12228 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12229 12230 /* The sh_info field of the symtab header tells us where the 12231 external symbols start. We don't care about the local symbols at 12232 this point. */ 12233 extsymcount = elf_tdata (abfd)->symtab_hdr.sh_size / bed->s->sizeof_sym; 12234 if (!elf_bad_symtab (abfd)) 12235 extsymcount -= elf_tdata (abfd)->symtab_hdr.sh_info; 12236 12237 sym_hashes = elf_sym_hashes (abfd); 12238 sym_hashes_end = sym_hashes + extsymcount; 12239 12240 /* Hunt down the child symbol, which is in this section at the same 12241 offset as the relocation. */ 12242 for (search = sym_hashes; search != sym_hashes_end; ++search) 12243 { 12244 if ((child = *search) != NULL 12245 && (child->root.type == bfd_link_hash_defined 12246 || child->root.type == bfd_link_hash_defweak) 12247 && child->root.u.def.section == sec 12248 && child->root.u.def.value == offset) 12249 goto win; 12250 } 12251 12252 (*_bfd_error_handler) ("%B: %A+%lu: No symbol found for INHERIT", 12253 abfd, sec, (unsigned long) offset); 12254 bfd_set_error (bfd_error_invalid_operation); 12255 return FALSE; 12256 12257 win: 12258 if (!child->vtable) 12259 { 12260 child->vtable = (struct elf_link_virtual_table_entry *) 12261 bfd_zalloc (abfd, sizeof (*child->vtable)); 12262 if (!child->vtable) 12263 return FALSE; 12264 } 12265 if (!h) 12266 { 12267 /* This *should* only be the absolute section. It could potentially 12268 be that someone has defined a non-global vtable though, which 12269 would be bad. It isn't worth paging in the local symbols to be 12270 sure though; that case should simply be handled by the assembler. */ 12271 12272 child->vtable->parent = (struct elf_link_hash_entry *) -1; 12273 } 12274 else 12275 child->vtable->parent = h; 12276 12277 return TRUE; 12278 } 12279 12280 /* Called from check_relocs to record the existence of a VTENTRY reloc. */ 12281 12282 bfd_boolean 12283 bfd_elf_gc_record_vtentry (bfd *abfd ATTRIBUTE_UNUSED, 12284 asection *sec ATTRIBUTE_UNUSED, 12285 struct elf_link_hash_entry *h, 12286 bfd_vma addend) 12287 { 12288 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12289 unsigned int log_file_align = bed->s->log_file_align; 12290 12291 if (!h->vtable) 12292 { 12293 h->vtable = (struct elf_link_virtual_table_entry *) 12294 bfd_zalloc (abfd, sizeof (*h->vtable)); 12295 if (!h->vtable) 12296 return FALSE; 12297 } 12298 12299 if (addend >= h->vtable->size) 12300 { 12301 size_t size, bytes, file_align; 12302 bfd_boolean *ptr = h->vtable->used; 12303 12304 /* While the symbol is undefined, we have to be prepared to handle 12305 a zero size. */ 12306 file_align = 1 << log_file_align; 12307 if (h->root.type == bfd_link_hash_undefined) 12308 size = addend + file_align; 12309 else 12310 { 12311 size = h->size; 12312 if (addend >= size) 12313 { 12314 /* Oops! We've got a reference past the defined end of 12315 the table. This is probably a bug -- shall we warn? */ 12316 size = addend + file_align; 12317 } 12318 } 12319 size = (size + file_align - 1) & -file_align; 12320 12321 /* Allocate one extra entry for use as a "done" flag for the 12322 consolidation pass. */ 12323 bytes = ((size >> log_file_align) + 1) * sizeof (bfd_boolean); 12324 12325 if (ptr) 12326 { 12327 ptr = (bfd_boolean *) bfd_realloc (ptr - 1, bytes); 12328 12329 if (ptr != NULL) 12330 { 12331 size_t oldbytes; 12332 12333 oldbytes = (((h->vtable->size >> log_file_align) + 1) 12334 * sizeof (bfd_boolean)); 12335 memset (((char *) ptr) + oldbytes, 0, bytes - oldbytes); 12336 } 12337 } 12338 else 12339 ptr = (bfd_boolean *) bfd_zmalloc (bytes); 12340 12341 if (ptr == NULL) 12342 return FALSE; 12343 12344 /* And arrange for that done flag to be at index -1. */ 12345 h->vtable->used = ptr + 1; 12346 h->vtable->size = size; 12347 } 12348 12349 h->vtable->used[addend >> log_file_align] = TRUE; 12350 12351 return TRUE; 12352 } 12353 12354 /* Map an ELF section header flag to its corresponding string. */ 12355 typedef struct 12356 { 12357 char *flag_name; 12358 flagword flag_value; 12359 } elf_flags_to_name_table; 12360 12361 static elf_flags_to_name_table elf_flags_to_names [] = 12362 { 12363 { "SHF_WRITE", SHF_WRITE }, 12364 { "SHF_ALLOC", SHF_ALLOC }, 12365 { "SHF_EXECINSTR", SHF_EXECINSTR }, 12366 { "SHF_MERGE", SHF_MERGE }, 12367 { "SHF_STRINGS", SHF_STRINGS }, 12368 { "SHF_INFO_LINK", SHF_INFO_LINK}, 12369 { "SHF_LINK_ORDER", SHF_LINK_ORDER}, 12370 { "SHF_OS_NONCONFORMING", SHF_OS_NONCONFORMING}, 12371 { "SHF_GROUP", SHF_GROUP }, 12372 { "SHF_TLS", SHF_TLS }, 12373 { "SHF_MASKOS", SHF_MASKOS }, 12374 { "SHF_EXCLUDE", SHF_EXCLUDE }, 12375 }; 12376 12377 /* Returns TRUE if the section is to be included, otherwise FALSE. */ 12378 bfd_boolean 12379 bfd_elf_lookup_section_flags (struct bfd_link_info *info, 12380 struct flag_info *flaginfo, 12381 asection *section) 12382 { 12383 const bfd_vma sh_flags = elf_section_flags (section); 12384 12385 if (!flaginfo->flags_initialized) 12386 { 12387 bfd *obfd = info->output_bfd; 12388 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 12389 struct flag_info_list *tf = flaginfo->flag_list; 12390 int with_hex = 0; 12391 int without_hex = 0; 12392 12393 for (tf = flaginfo->flag_list; tf != NULL; tf = tf->next) 12394 { 12395 unsigned i; 12396 flagword (*lookup) (char *); 12397 12398 lookup = bed->elf_backend_lookup_section_flags_hook; 12399 if (lookup != NULL) 12400 { 12401 flagword hexval = (*lookup) ((char *) tf->name); 12402 12403 if (hexval != 0) 12404 { 12405 if (tf->with == with_flags) 12406 with_hex |= hexval; 12407 else if (tf->with == without_flags) 12408 without_hex |= hexval; 12409 tf->valid = TRUE; 12410 continue; 12411 } 12412 } 12413 for (i = 0; i < ARRAY_SIZE (elf_flags_to_names); ++i) 12414 { 12415 if (strcmp (tf->name, elf_flags_to_names[i].flag_name) == 0) 12416 { 12417 if (tf->with == with_flags) 12418 with_hex |= elf_flags_to_names[i].flag_value; 12419 else if (tf->with == without_flags) 12420 without_hex |= elf_flags_to_names[i].flag_value; 12421 tf->valid = TRUE; 12422 break; 12423 } 12424 } 12425 if (!tf->valid) 12426 { 12427 info->callbacks->einfo 12428 (_("Unrecognized INPUT_SECTION_FLAG %s\n"), tf->name); 12429 return FALSE; 12430 } 12431 } 12432 flaginfo->flags_initialized = TRUE; 12433 flaginfo->only_with_flags |= with_hex; 12434 flaginfo->not_with_flags |= without_hex; 12435 } 12436 12437 if ((flaginfo->only_with_flags & sh_flags) != flaginfo->only_with_flags) 12438 return FALSE; 12439 12440 if ((flaginfo->not_with_flags & sh_flags) != 0) 12441 return FALSE; 12442 12443 return TRUE; 12444 } 12445 12446 struct alloc_got_off_arg { 12447 bfd_vma gotoff; 12448 struct bfd_link_info *info; 12449 }; 12450 12451 /* We need a special top-level link routine to convert got reference counts 12452 to real got offsets. */ 12453 12454 static bfd_boolean 12455 elf_gc_allocate_got_offsets (struct elf_link_hash_entry *h, void *arg) 12456 { 12457 struct alloc_got_off_arg *gofarg = (struct alloc_got_off_arg *) arg; 12458 bfd *obfd = gofarg->info->output_bfd; 12459 const struct elf_backend_data *bed = get_elf_backend_data (obfd); 12460 12461 if (h->got.refcount > 0) 12462 { 12463 h->got.offset = gofarg->gotoff; 12464 gofarg->gotoff += bed->got_elt_size (obfd, gofarg->info, h, NULL, 0); 12465 } 12466 else 12467 h->got.offset = (bfd_vma) -1; 12468 12469 return TRUE; 12470 } 12471 12472 /* And an accompanying bit to work out final got entry offsets once 12473 we're done. Should be called from final_link. */ 12474 12475 bfd_boolean 12476 bfd_elf_gc_common_finalize_got_offsets (bfd *abfd, 12477 struct bfd_link_info *info) 12478 { 12479 bfd *i; 12480 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12481 bfd_vma gotoff; 12482 struct alloc_got_off_arg gofarg; 12483 12484 BFD_ASSERT (abfd == info->output_bfd); 12485 12486 if (! is_elf_hash_table (info->hash)) 12487 return FALSE; 12488 12489 /* The GOT offset is relative to the .got section, but the GOT header is 12490 put into the .got.plt section, if the backend uses it. */ 12491 if (bed->want_got_plt) 12492 gotoff = 0; 12493 else 12494 gotoff = bed->got_header_size; 12495 12496 /* Do the local .got entries first. */ 12497 for (i = info->input_bfds; i; i = i->link_next) 12498 { 12499 bfd_signed_vma *local_got; 12500 bfd_size_type j, locsymcount; 12501 Elf_Internal_Shdr *symtab_hdr; 12502 12503 if (bfd_get_flavour (i) != bfd_target_elf_flavour) 12504 continue; 12505 12506 local_got = elf_local_got_refcounts (i); 12507 if (!local_got) 12508 continue; 12509 12510 symtab_hdr = &elf_tdata (i)->symtab_hdr; 12511 if (elf_bad_symtab (i)) 12512 locsymcount = symtab_hdr->sh_size / bed->s->sizeof_sym; 12513 else 12514 locsymcount = symtab_hdr->sh_info; 12515 12516 for (j = 0; j < locsymcount; ++j) 12517 { 12518 if (local_got[j] > 0) 12519 { 12520 local_got[j] = gotoff; 12521 gotoff += bed->got_elt_size (abfd, info, NULL, i, j); 12522 } 12523 else 12524 local_got[j] = (bfd_vma) -1; 12525 } 12526 } 12527 12528 /* Then the global .got entries. .plt refcounts are handled by 12529 adjust_dynamic_symbol */ 12530 gofarg.gotoff = gotoff; 12531 gofarg.info = info; 12532 elf_link_hash_traverse (elf_hash_table (info), 12533 elf_gc_allocate_got_offsets, 12534 &gofarg); 12535 return TRUE; 12536 } 12537 12538 /* Many folk need no more in the way of final link than this, once 12539 got entry reference counting is enabled. */ 12540 12541 bfd_boolean 12542 bfd_elf_gc_common_final_link (bfd *abfd, struct bfd_link_info *info) 12543 { 12544 if (!bfd_elf_gc_common_finalize_got_offsets (abfd, info)) 12545 return FALSE; 12546 12547 /* Invoke the regular ELF backend linker to do all the work. */ 12548 return bfd_elf_final_link (abfd, info); 12549 } 12550 12551 bfd_boolean 12552 bfd_elf_reloc_symbol_deleted_p (bfd_vma offset, void *cookie) 12553 { 12554 struct elf_reloc_cookie *rcookie = (struct elf_reloc_cookie *) cookie; 12555 12556 if (rcookie->bad_symtab) 12557 rcookie->rel = rcookie->rels; 12558 12559 for (; rcookie->rel < rcookie->relend; rcookie->rel++) 12560 { 12561 unsigned long r_symndx; 12562 12563 if (! rcookie->bad_symtab) 12564 if (rcookie->rel->r_offset > offset) 12565 return FALSE; 12566 if (rcookie->rel->r_offset != offset) 12567 continue; 12568 12569 r_symndx = rcookie->rel->r_info >> rcookie->r_sym_shift; 12570 if (r_symndx == STN_UNDEF) 12571 return TRUE; 12572 12573 if (r_symndx >= rcookie->locsymcount 12574 || ELF_ST_BIND (rcookie->locsyms[r_symndx].st_info) != STB_LOCAL) 12575 { 12576 struct elf_link_hash_entry *h; 12577 12578 h = rcookie->sym_hashes[r_symndx - rcookie->extsymoff]; 12579 12580 while (h->root.type == bfd_link_hash_indirect 12581 || h->root.type == bfd_link_hash_warning) 12582 h = (struct elf_link_hash_entry *) h->root.u.i.link; 12583 12584 if ((h->root.type == bfd_link_hash_defined 12585 || h->root.type == bfd_link_hash_defweak) 12586 && discarded_section (h->root.u.def.section)) 12587 return TRUE; 12588 else 12589 return FALSE; 12590 } 12591 else 12592 { 12593 /* It's not a relocation against a global symbol, 12594 but it could be a relocation against a local 12595 symbol for a discarded section. */ 12596 asection *isec; 12597 Elf_Internal_Sym *isym; 12598 12599 /* Need to: get the symbol; get the section. */ 12600 isym = &rcookie->locsyms[r_symndx]; 12601 isec = bfd_section_from_elf_index (rcookie->abfd, isym->st_shndx); 12602 if (isec != NULL && discarded_section (isec)) 12603 return TRUE; 12604 } 12605 return FALSE; 12606 } 12607 return FALSE; 12608 } 12609 12610 /* Discard unneeded references to discarded sections. 12611 Returns TRUE if any section's size was changed. */ 12612 /* This function assumes that the relocations are in sorted order, 12613 which is true for all known assemblers. */ 12614 12615 bfd_boolean 12616 bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info) 12617 { 12618 struct elf_reloc_cookie cookie; 12619 asection *stab, *eh; 12620 const struct elf_backend_data *bed; 12621 bfd *abfd; 12622 bfd_boolean ret = FALSE; 12623 12624 if (info->traditional_format 12625 || !is_elf_hash_table (info->hash)) 12626 return FALSE; 12627 12628 _bfd_elf_begin_eh_frame_parsing (info); 12629 for (abfd = info->input_bfds; abfd != NULL; abfd = abfd->link_next) 12630 { 12631 if (bfd_get_flavour (abfd) != bfd_target_elf_flavour) 12632 continue; 12633 12634 bed = get_elf_backend_data (abfd); 12635 12636 eh = NULL; 12637 if (!info->relocatable) 12638 { 12639 eh = bfd_get_section_by_name (abfd, ".eh_frame"); 12640 while (eh != NULL 12641 && (eh->size == 0 12642 || bfd_is_abs_section (eh->output_section))) 12643 eh = bfd_get_next_section_by_name (eh); 12644 } 12645 12646 stab = bfd_get_section_by_name (abfd, ".stab"); 12647 if (stab != NULL 12648 && (stab->size == 0 12649 || bfd_is_abs_section (stab->output_section) 12650 || stab->sec_info_type != SEC_INFO_TYPE_STABS)) 12651 stab = NULL; 12652 12653 if (stab == NULL 12654 && eh == NULL 12655 && bed->elf_backend_discard_info == NULL) 12656 continue; 12657 12658 if (!init_reloc_cookie (&cookie, info, abfd)) 12659 return FALSE; 12660 12661 if (stab != NULL 12662 && stab->reloc_count > 0 12663 && init_reloc_cookie_rels (&cookie, info, abfd, stab)) 12664 { 12665 if (_bfd_discard_section_stabs (abfd, stab, 12666 elf_section_data (stab)->sec_info, 12667 bfd_elf_reloc_symbol_deleted_p, 12668 &cookie)) 12669 ret = TRUE; 12670 fini_reloc_cookie_rels (&cookie, stab); 12671 } 12672 12673 while (eh != NULL 12674 && init_reloc_cookie_rels (&cookie, info, abfd, eh)) 12675 { 12676 _bfd_elf_parse_eh_frame (abfd, info, eh, &cookie); 12677 if (_bfd_elf_discard_section_eh_frame (abfd, info, eh, 12678 bfd_elf_reloc_symbol_deleted_p, 12679 &cookie)) 12680 ret = TRUE; 12681 fini_reloc_cookie_rels (&cookie, eh); 12682 eh = bfd_get_next_section_by_name (eh); 12683 } 12684 12685 if (bed->elf_backend_discard_info != NULL 12686 && (*bed->elf_backend_discard_info) (abfd, &cookie, info)) 12687 ret = TRUE; 12688 12689 fini_reloc_cookie (&cookie, abfd); 12690 } 12691 _bfd_elf_end_eh_frame_parsing (info); 12692 12693 if (info->eh_frame_hdr 12694 && !info->relocatable 12695 && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info)) 12696 ret = TRUE; 12697 12698 return ret; 12699 } 12700 12701 bfd_boolean 12702 _bfd_elf_section_already_linked (bfd *abfd, 12703 asection *sec, 12704 struct bfd_link_info *info) 12705 { 12706 flagword flags; 12707 const char *name, *key; 12708 struct bfd_section_already_linked *l; 12709 struct bfd_section_already_linked_hash_entry *already_linked_list; 12710 12711 if (sec->output_section == bfd_abs_section_ptr) 12712 return FALSE; 12713 12714 flags = sec->flags; 12715 12716 /* Return if it isn't a linkonce section. A comdat group section 12717 also has SEC_LINK_ONCE set. */ 12718 if ((flags & SEC_LINK_ONCE) == 0) 12719 return FALSE; 12720 12721 /* Don't put group member sections on our list of already linked 12722 sections. They are handled as a group via their group section. */ 12723 if (elf_sec_group (sec) != NULL) 12724 return FALSE; 12725 12726 /* For a SHT_GROUP section, use the group signature as the key. */ 12727 name = sec->name; 12728 if ((flags & SEC_GROUP) != 0 12729 && elf_next_in_group (sec) != NULL 12730 && elf_group_name (elf_next_in_group (sec)) != NULL) 12731 key = elf_group_name (elf_next_in_group (sec)); 12732 else 12733 { 12734 /* Otherwise we should have a .gnu.linkonce.<type>.<key> section. */ 12735 if (CONST_STRNEQ (name, ".gnu.linkonce.") 12736 && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL) 12737 key++; 12738 else 12739 /* Must be a user linkonce section that doesn't follow gcc's 12740 naming convention. In this case we won't be matching 12741 single member groups. */ 12742 key = name; 12743 } 12744 12745 already_linked_list = bfd_section_already_linked_table_lookup (key); 12746 12747 for (l = already_linked_list->entry; l != NULL; l = l->next) 12748 { 12749 /* We may have 2 different types of sections on the list: group 12750 sections with a signature of <key> (<key> is some string), 12751 and linkonce sections named .gnu.linkonce.<type>.<key>. 12752 Match like sections. LTO plugin sections are an exception. 12753 They are always named .gnu.linkonce.t.<key> and match either 12754 type of section. */ 12755 if (((flags & SEC_GROUP) == (l->sec->flags & SEC_GROUP) 12756 && ((flags & SEC_GROUP) != 0 12757 || strcmp (name, l->sec->name) == 0)) 12758 || (l->sec->owner->flags & BFD_PLUGIN) != 0) 12759 { 12760 /* The section has already been linked. See if we should 12761 issue a warning. */ 12762 if (!_bfd_handle_already_linked (sec, l, info)) 12763 return FALSE; 12764 12765 if (flags & SEC_GROUP) 12766 { 12767 asection *first = elf_next_in_group (sec); 12768 asection *s = first; 12769 12770 while (s != NULL) 12771 { 12772 s->output_section = bfd_abs_section_ptr; 12773 /* Record which group discards it. */ 12774 s->kept_section = l->sec; 12775 s = elf_next_in_group (s); 12776 /* These lists are circular. */ 12777 if (s == first) 12778 break; 12779 } 12780 } 12781 12782 return TRUE; 12783 } 12784 } 12785 12786 /* A single member comdat group section may be discarded by a 12787 linkonce section and vice versa. */ 12788 if ((flags & SEC_GROUP) != 0) 12789 { 12790 asection *first = elf_next_in_group (sec); 12791 12792 if (first != NULL && elf_next_in_group (first) == first) 12793 /* Check this single member group against linkonce sections. */ 12794 for (l = already_linked_list->entry; l != NULL; l = l->next) 12795 if ((l->sec->flags & SEC_GROUP) == 0 12796 && bfd_elf_match_symbols_in_sections (l->sec, first, info)) 12797 { 12798 first->output_section = bfd_abs_section_ptr; 12799 first->kept_section = l->sec; 12800 sec->output_section = bfd_abs_section_ptr; 12801 break; 12802 } 12803 } 12804 else 12805 /* Check this linkonce section against single member groups. */ 12806 for (l = already_linked_list->entry; l != NULL; l = l->next) 12807 if (l->sec->flags & SEC_GROUP) 12808 { 12809 asection *first = elf_next_in_group (l->sec); 12810 12811 if (first != NULL 12812 && elf_next_in_group (first) == first 12813 && bfd_elf_match_symbols_in_sections (first, sec, info)) 12814 { 12815 sec->output_section = bfd_abs_section_ptr; 12816 sec->kept_section = first; 12817 break; 12818 } 12819 } 12820 12821 /* Do not complain on unresolved relocations in `.gnu.linkonce.r.F' 12822 referencing its discarded `.gnu.linkonce.t.F' counterpart - g++-3.4 12823 specific as g++-4.x is using COMDAT groups (without the `.gnu.linkonce' 12824 prefix) instead. `.gnu.linkonce.r.*' were the `.rodata' part of its 12825 matching `.gnu.linkonce.t.*'. If `.gnu.linkonce.r.F' is not discarded 12826 but its `.gnu.linkonce.t.F' is discarded means we chose one-only 12827 `.gnu.linkonce.t.F' section from a different bfd not requiring any 12828 `.gnu.linkonce.r.F'. Thus `.gnu.linkonce.r.F' should be discarded. 12829 The reverse order cannot happen as there is never a bfd with only the 12830 `.gnu.linkonce.r.F' section. The order of sections in a bfd does not 12831 matter as here were are looking only for cross-bfd sections. */ 12832 12833 if ((flags & SEC_GROUP) == 0 && CONST_STRNEQ (name, ".gnu.linkonce.r.")) 12834 for (l = already_linked_list->entry; l != NULL; l = l->next) 12835 if ((l->sec->flags & SEC_GROUP) == 0 12836 && CONST_STRNEQ (l->sec->name, ".gnu.linkonce.t.")) 12837 { 12838 if (abfd != l->sec->owner) 12839 sec->output_section = bfd_abs_section_ptr; 12840 break; 12841 } 12842 12843 /* This is the first section with this name. Record it. */ 12844 if (!bfd_section_already_linked_table_insert (already_linked_list, sec)) 12845 info->callbacks->einfo (_("%F%P: already_linked_table: %E\n")); 12846 return sec->output_section == bfd_abs_section_ptr; 12847 } 12848 12849 bfd_boolean 12850 _bfd_elf_common_definition (Elf_Internal_Sym *sym) 12851 { 12852 return sym->st_shndx == SHN_COMMON; 12853 } 12854 12855 unsigned int 12856 _bfd_elf_common_section_index (asection *sec ATTRIBUTE_UNUSED) 12857 { 12858 return SHN_COMMON; 12859 } 12860 12861 asection * 12862 _bfd_elf_common_section (asection *sec ATTRIBUTE_UNUSED) 12863 { 12864 return bfd_com_section_ptr; 12865 } 12866 12867 bfd_vma 12868 _bfd_elf_default_got_elt_size (bfd *abfd, 12869 struct bfd_link_info *info ATTRIBUTE_UNUSED, 12870 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED, 12871 bfd *ibfd ATTRIBUTE_UNUSED, 12872 unsigned long symndx ATTRIBUTE_UNUSED) 12873 { 12874 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12875 return bed->s->arch_size / 8; 12876 } 12877 12878 /* Routines to support the creation of dynamic relocs. */ 12879 12880 /* Returns the name of the dynamic reloc section associated with SEC. */ 12881 12882 static const char * 12883 get_dynamic_reloc_section_name (bfd * abfd, 12884 asection * sec, 12885 bfd_boolean is_rela) 12886 { 12887 char *name; 12888 const char *old_name = bfd_get_section_name (NULL, sec); 12889 const char *prefix = is_rela ? ".rela" : ".rel"; 12890 12891 if (old_name == NULL) 12892 return NULL; 12893 12894 name = bfd_alloc (abfd, strlen (prefix) + strlen (old_name) + 1); 12895 sprintf (name, "%s%s", prefix, old_name); 12896 12897 return name; 12898 } 12899 12900 /* Returns the dynamic reloc section associated with SEC. 12901 If necessary compute the name of the dynamic reloc section based 12902 on SEC's name (looked up in ABFD's string table) and the setting 12903 of IS_RELA. */ 12904 12905 asection * 12906 _bfd_elf_get_dynamic_reloc_section (bfd * abfd, 12907 asection * sec, 12908 bfd_boolean is_rela) 12909 { 12910 asection * reloc_sec = elf_section_data (sec)->sreloc; 12911 12912 if (reloc_sec == NULL) 12913 { 12914 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 12915 12916 if (name != NULL) 12917 { 12918 reloc_sec = bfd_get_linker_section (abfd, name); 12919 12920 if (reloc_sec != NULL) 12921 elf_section_data (sec)->sreloc = reloc_sec; 12922 } 12923 } 12924 12925 return reloc_sec; 12926 } 12927 12928 /* Returns the dynamic reloc section associated with SEC. If the 12929 section does not exist it is created and attached to the DYNOBJ 12930 bfd and stored in the SRELOC field of SEC's elf_section_data 12931 structure. 12932 12933 ALIGNMENT is the alignment for the newly created section and 12934 IS_RELA defines whether the name should be .rela.<SEC's name> 12935 or .rel.<SEC's name>. The section name is looked up in the 12936 string table associated with ABFD. */ 12937 12938 asection * 12939 _bfd_elf_make_dynamic_reloc_section (asection * sec, 12940 bfd * dynobj, 12941 unsigned int alignment, 12942 bfd * abfd, 12943 bfd_boolean is_rela) 12944 { 12945 asection * reloc_sec = elf_section_data (sec)->sreloc; 12946 12947 if (reloc_sec == NULL) 12948 { 12949 const char * name = get_dynamic_reloc_section_name (abfd, sec, is_rela); 12950 12951 if (name == NULL) 12952 return NULL; 12953 12954 reloc_sec = bfd_get_linker_section (dynobj, name); 12955 12956 if (reloc_sec == NULL) 12957 { 12958 flagword flags = (SEC_HAS_CONTENTS | SEC_READONLY 12959 | SEC_IN_MEMORY | SEC_LINKER_CREATED); 12960 if ((sec->flags & SEC_ALLOC) != 0) 12961 flags |= SEC_ALLOC | SEC_LOAD; 12962 12963 reloc_sec = bfd_make_section_anyway_with_flags (dynobj, name, flags); 12964 if (reloc_sec != NULL) 12965 { 12966 if (! bfd_set_section_alignment (dynobj, reloc_sec, alignment)) 12967 reloc_sec = NULL; 12968 } 12969 } 12970 12971 elf_section_data (sec)->sreloc = reloc_sec; 12972 } 12973 12974 return reloc_sec; 12975 } 12976 12977 /* Copy the ELF symbol type associated with a linker hash entry. */ 12978 void 12979 _bfd_elf_copy_link_hash_symbol_type (bfd *abfd ATTRIBUTE_UNUSED, 12980 struct bfd_link_hash_entry * hdest, 12981 struct bfd_link_hash_entry * hsrc) 12982 { 12983 struct elf_link_hash_entry *ehdest = (struct elf_link_hash_entry *)hdest; 12984 struct elf_link_hash_entry *ehsrc = (struct elf_link_hash_entry *)hsrc; 12985 12986 ehdest->type = ehsrc->type; 12987 ehdest->target_internal = ehsrc->target_internal; 12988 } 12989 12990 /* Append a RELA relocation REL to section S in BFD. */ 12991 12992 void 12993 elf_append_rela (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 12994 { 12995 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 12996 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rela); 12997 BFD_ASSERT (loc + bed->s->sizeof_rela <= s->contents + s->size); 12998 bed->s->swap_reloca_out (abfd, rel, loc); 12999 } 13000 13001 /* Append a REL relocation REL to section S in BFD. */ 13002 13003 void 13004 elf_append_rel (bfd *abfd, asection *s, Elf_Internal_Rela *rel) 13005 { 13006 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 13007 bfd_byte *loc = s->contents + (s->reloc_count++ * bed->s->sizeof_rel); 13008 BFD_ASSERT (loc + bed->s->sizeof_rel <= s->contents + s->size); 13009 bed->s->swap_reloc_out (abfd, rel, loc); 13010 } 13011