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