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